Import STB for rl::image::Image.

This commit is contained in:
Chinmay Garde
2021-05-01 17:37:33 -07:00
committed by Dan Field
parent fb7f373866
commit c73b643cce
9 changed files with 36 additions and 228 deletions

View File

@@ -47,8 +47,6 @@ source_set("impeller") {
image_sources = [
"image/Image.cc",
"image/Image.h",
"image/ImageEncoder.cc",
"image/ImageEncoder.h",
"image/ImageResult.cc",
"image/ImageResult.h",
]
@@ -57,6 +55,7 @@ source_set("impeller") {
deps = [
"//flutter/fml",
"//flutter/impeller/third_party/stb",
"//flutter/runtime:libdart", # For tracing, seems to be pulled in by FML.
]
}

View File

@@ -121,14 +121,6 @@ void Entity::setStrokeSize(double strokeSize) {
_strokeSize = strokeSize;
}
const image::Image& Entity::contents() const {
return _contents;
}
void Entity::setContents(image::Image image) {
_contents = std::move(image);
}
const geom::Path& Entity::path() const {
return _path;
}

View File

@@ -180,10 +180,6 @@ class Entity {
void setStrokeSize(double strokeSize);
const image::Image& contents() const;
void setContents(image::Image image);
const geom::Path& path() const;
void setPath(geom::Path path);
@@ -194,7 +190,7 @@ class Entity {
geom::Point _anchorPoint;
geom::Matrix _transformation;
Color _backgroundColor;
image::Image _contents;
geom::Path _path;
double _opacity;
Color _strokeColor;

View File

@@ -5,30 +5,17 @@
#include "Image.h"
#include <stb_image.h>
#include "ImageSource.h"
namespace rl {
namespace image {
Image::Image() = default;
Image::Image(fml::RefPtr<const fml::Mapping> sourceAllocation)
: _source(ImageSource::Create(std::move(sourceAllocation))) {}
Image::Image(core::FileHandle sourceFile)
: _source(ImageSource::Create(std::move(sourceFile))) {}
Image::Image(std::shared_ptr<const fml::Mapping> sourceAllocation)
: _source(std::move(sourceAllocation)) {}
Image::~Image() = default;
ImageResult Image::decode() const {
if (_source == nullptr) {
return {};
}
_source->prepareForUse();
if (_source->sourceDataSize() == 0) {
RL_LOG("Source data for image decoding was zero sized.");
if (!_source) {
return {};
}
@@ -37,27 +24,26 @@ ImageResult Image::decode() const {
int comps = 0;
stbi_uc* decoded =
stbi_load_from_memory(_source->sourceData(), // Source Data
_source->sourceDataSize(), // Source Data Size
&width, // Out: Width
&height, // Out: Height
&comps, // Out: Components
stbi_load_from_memory(_source->GetMapping(), // Source Data
_source->GetSize(), // Source Data Size
&width, // Out: Width
&height, // Out: Height
&comps, // Out: Components
STBI_default);
auto destinationAllocation = fml::RefPtr<const fml::Mapping>{
decoded, width * height * comps * sizeof(stbi_uc)};
/*
* If either the decoded allocation is null or the size works out to be zero,
* the allocation will mark itself as not ready and we know that the decode
* job failed.
*/
if (!destinationAllocation.isReady()) {
RL_LOG("Destination allocation for image decoding was null.");
if (decoded == nullptr) {
FML_LOG(ERROR) << "Could not decode image from host memory.";
return {};
}
auto destinationAllocation = std::make_shared<const fml::NonOwnedMapping>(
decoded, // bytes
width * height * comps * sizeof(stbi_uc), // byte size
[](const uint8_t* data, size_t size) {
::stbi_image_free(const_cast<uint8_t*>(data));
} // release proc
);
/*
* Make sure we got a valid component set.
*/
@@ -82,7 +68,7 @@ ImageResult Image::decode() const {
}
if (components == ImageResult::Components::Invalid) {
RL_LOG("Could not detect image components when decoding.");
FML_LOG(ERROR) << "Could not detect image components when decoding.";
return {};
}
@@ -95,15 +81,7 @@ ImageResult Image::decode() const {
}
bool Image::isValid() const {
return _source != nullptr;
}
std::size_t Image::Hash::operator()(const Image& key) const {
return std::hash<decltype(key._source)>()(key._source);
}
bool Image::Equal::operator()(const Image& lhs, const Image& rhs) const {
return lhs._source == rhs._source;
return static_cast<bool>(_source);
}
} // namespace image

View File

@@ -17,9 +17,7 @@ class ImageSource;
class Image {
public:
Image();
Image(fml::RefPtr<const fml::Mapping> sourceAllocation);
Image(std::shared_ptr<const fml::Mapping> sourceAllocation);
~Image();
@@ -27,16 +25,8 @@ class Image {
bool isValid() const;
struct Hash {
std::size_t operator()(const Image& key) const;
};
struct Equal {
bool operator()(const Image& lhs, const Image& rhs) const;
};
private:
std::shared_ptr<ImageSource> _source;
std::shared_ptr<const fml::Mapping> _source;
};
} // namespace image

View File

@@ -1,86 +0,0 @@
/*
* This source file is part of the Radar project.
* Licensed under the MIT License. See LICENSE file for details.
*/
#include "ImageEncoder.h"
#include <stb_image_write.h>
namespace rl {
namespace image {
ImageEncoder::ImageEncoder(Type type, core::FileHandle handle)
: _isReady(false), _type(type), _adapter(std::move(handle)) {
if (!_adapter.isValid()) {
return;
}
_isReady = true;
}
ImageEncoder::~ImageEncoder() = default;
bool ImageEncoder::isReady() const {
return _isReady;
}
bool ImageEncoder::encode(ImageResult image) {
if (!_isReady) {
return false;
}
if (!image.wasSuccessful()) {
return false;
}
switch (_type) {
case Type::PNG:
return encodePNG(std::move(image));
}
return false;
}
int ComponentsToSize(ImageResult::Components components) {
switch (components) {
case ImageResult::Components::Invalid:
return 0;
case ImageResult::Components::Grey:
return 1;
case ImageResult::Components::GreyAlpha:
return 2;
case ImageResult::Components::RGB:
return 3;
case ImageResult::Components::RGBA:
return 4;
}
return 0;
}
bool ImageEncoder::encodePNG(ImageResult image) {
auto size = image.size();
int componentSize = ComponentsToSize(image.components());
auto callback = [](void* context, void* data, int size) {
reinterpret_cast<ImageEncoder*>(context)->write(
reinterpret_cast<const uint8_t*>(data), size);
};
return stbi_write_png_to_func(callback, //
this, //
size.width, //
size.height, //
componentSize, //
image.allocation().data(), //
componentSize //
) == 1;
}
void ImageEncoder::write(const uint8_t* data, size_t size) {
RL_UNUSED(_adapter.write(data, size));
}
} // namespace image
} // namespace rl

View File

@@ -1,41 +0,0 @@
/*
* This source file is part of the Radar project.
* Licensed under the MIT License. See LICENSE file for details.
*/
#pragma once
#include "ImageResult.h"
#include "Size.h"
namespace rl {
namespace image {
class ImageEncoder {
public:
enum class Type {
PNG,
};
ImageEncoder(Type type, core::FileHandle handle);
~ImageEncoder();
bool isReady() const;
bool encode(ImageResult image);
private:
bool _isReady;
Type _type;
core::FileIOAdapter _adapter;
bool encodePNG(ImageResult image);
void write(const uint8_t* data, size_t size);
FML_DISALLOW_COPY_AND_ASSIGN(ImageEncoder);
};
} // namespace image
} // namespace rl

View File

@@ -8,32 +8,17 @@
namespace rl {
namespace image {
ImageResult::ImageResult() = default;
ImageResult::ImageResult(geom::Size size,
Components components,
fml::RefPtr<const fml::Mapping> allocation)
std::shared_ptr<const fml::Mapping> allocation)
: _success(true),
_size(size),
_components(components),
_allocation(std::move(allocation)) {}
ImageResult::ImageResult() : _success(false) {}
ImageResult::ImageResult(ImageResult&&) = default;
ImageResult& ImageResult::operator=(ImageResult&& other) {
_success = other._success;
other._success = false;
_size = other._size;
other._size = geom::Size{};
_components = other._components;
other._components = Components::Invalid;
_allocation = std::move(other._allocation);
return *this;
}
ImageResult::~ImageResult() = default;
bool ImageResult::wasSuccessful() const {
return _success;
@@ -47,11 +32,9 @@ ImageResult::Components ImageResult::components() const {
return _components;
}
const fml::RefPtr<const fml::Mapping>& ImageResult::allocation() const {
const std::shared_ptr<const fml::Mapping>& ImageResult::allocation() const {
return _allocation;
}
ImageResult::~ImageResult() = default;
} // namespace image
} // namespace rl

View File

@@ -5,10 +5,11 @@
#pragma once
#include <memory>
#include "Size.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/memory/ref_counted.h"
namespace rl {
namespace image {
@@ -27,11 +28,7 @@ class ImageResult {
ImageResult(geom::Size size,
Components components,
fml::RefPtr<const fml::Mapping> allocation);
ImageResult(ImageResult&&);
ImageResult& operator=(ImageResult&&);
std::shared_ptr<const fml::Mapping> allocation);
~ImageResult();
@@ -41,13 +38,13 @@ class ImageResult {
Components components() const;
const fml::RefPtr<const fml::Mapping>& allocation() const;
const std::shared_ptr<const fml::Mapping>& allocation() const;
private:
bool _success;
bool _success = false;
geom::Size _size;
Components _components;
fml::RefPtr<const fml::Mapping> _allocation;
Components _components = Components::Invalid;
std::shared_ptr<const fml::Mapping> _allocation;
FML_DISALLOW_COPY_AND_ASSIGN(ImageResult);
};