Since the color space is set at the time of specifying the target buffer of the canvas,
there is no way to know the color space when the picture is loaded.
So, check the color space applied to SwCanvas at the time of reload()
and change the color space.
There is an issue of BGR color space support for each loader.
The external_jpg loader resets the TJPF color space and calls read() to get a new buffer.
In the case of external_png, we need to change the color value directly
because it have to start over from begin_read_*.
This solution can affect performance as much as it access again image buffer
that have already been `read()` done. However, this only happens once.
Change-Id: Ib7f25b5bc5871cfe59ad3963d26536269b48a3f5
}
+int32_t GlRenderer::colorSpace()
+{
+ //TODO: return a proper color space value.
+ return -1;
+}
+
+
bool GlRenderer::renderImage(TVG_UNUSED void* data)
{
return false;
bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) override;
bool endComposite(Compositor* cmp) override;
+ uint32_t colorSpace() override;
+
static GlRenderer* gen();
static int init(TVG_UNUSED uint32_t threads);
static int32_t init();
}
-bool SwRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, uint32_t cs)
+bool SwRenderer::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, uint32_t colorSpace)
{
if (!buffer || stride == 0 || w == 0 || h == 0 || w > stride) return false;
surface->stride = stride;
surface->w = w;
surface->h = h;
- surface->cs = cs;
+ surface->cs = colorSpace;
vport.x = vport.y = 0;
vport.w = surface->w;
}
+uint32_t SwRenderer::colorSpace()
+{
+ if (surface) return surface->cs;
+ return tvg::SwCanvas::ARGB8888;
+}
+
+
bool SwRenderer::init(uint32_t threads)
{
if ((initEngineCnt++) > 0) return true;
bool clear() override;
bool sync() override;
- bool target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, uint32_t cs);
+ bool target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t h, uint32_t colorSpace);
bool mempool(bool shared);
Compositor* target(const RenderRegion& region) override;
bool endComposite(Compositor* cmp) override;
void clearCompositors();
+ uint32_t colorSpace() override;
+
static SwRenderer* gen();
static bool init(uint32_t threads);
static int32_t init();
float vw = 0;
float vh = 0;
float w = 0, h = 0; //default image size
+ uint32_t colorSpace = SwCanvas::ARGB8888;
virtual ~LoadModule() {}
virtual bool read() = 0;
virtual bool close() = 0;
- virtual unique_ptr<Surface> bitmap() { return nullptr; }
+ virtual unique_ptr<Surface> bitmap(uint32_t colorSpace) { return nullptr; }
virtual unique_ptr<Paint> paint() { return nullptr; }
};
void* rdata = nullptr; //engine data
float w = 0, h = 0;
bool resizing = false;
+ uint32_t rendererColorSpace = 0;
~Impl()
{
}
}
free(surface);
- if ((surface = loader->bitmap().release())) {
+ if ((surface = loader->bitmap(rendererColorSpace).release())) {
loader->close();
return RenderUpdateFlag::Image;
}
void* update(RenderMethod &renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag, bool clipper)
{
+ rendererColorSpace = renderer.colorSpace();
auto flag = reload();
if (surface) {
virtual Compositor* target(const RenderRegion& region) = 0;
virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) = 0;
virtual bool endComposite(Compositor* cmp) = 0;
+
+ virtual uint32_t colorSpace() = 0;
};
}
freeData = false;
}
+uint32_t convertColorSpaceType(uint32_t colorSpace)
+{
+ uint32_t tjpfColorSpace = TJPF_RGBX;
+ switch (colorSpace)
+ {
+ case SwCanvas::ARGB8888:
+ case SwCanvas::ARGB8888_STRAIGHT:
+ default:
+ tjpfColorSpace = TJPF_BGRX;
+ break;
+ case SwCanvas::ABGR8888:
+ case SwCanvas::ABGR8888_STRAIGHT:
+ tjpfColorSpace = TJPF_RGBX;
+ break;
+ }
+ return tjpfColorSpace;
+}
+
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
bool JpgLoader::read()
{
if (image) tjFree(image);
- image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[TJPF_BGRX]);
+ image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[convertColorSpaceType(colorSpace)]);
if (!image) return false;
//decompress jpg image
- if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), TJPF_BGRX, 0) < 0) {
+ if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), convertColorSpaceType(colorSpace), 0) < 0) {
TVGERR("JPG LOADER", "%s", tjGetErrorStr());
tjFree(image);
image = nullptr;
}
-unique_ptr<Surface> JpgLoader::bitmap()
+unique_ptr<Surface> JpgLoader::bitmap(uint32_t colorSpace)
{
if (!image) return nullptr;
+ if (this->colorSpace != colorSpace) {
+ this->colorSpace = colorSpace;
+ read();
+ }
auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = (uint32_t*)(image);
surface->stride = w;
surface->w = w;
surface->h = h;
- surface->cs = SwCanvas::ARGB8888;
+ surface->cs = colorSpace;
return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- unique_ptr<Surface> bitmap() override;
+ unique_ptr<Surface> bitmap(uint32_t colorSpace) override;
private:
void clear();
}
+static inline uint32_t CHANGE_COLORSPACE(uint32_t c)
+{
+ return (c & 0xff000000) + ((c & 0x00ff0000)>>16) + (c & 0x0000ff00) + ((c & 0x000000ff)<<16);
+}
+
+
+static void _changeColorSpace(uint32_t* data, uint32_t w, uint32_t h)
+{
+ auto buffer = data;
+ for (uint32_t y = 0; y < h; ++y, buffer += w) {
+ auto src = buffer;
+ for (uint32_t x = 0; x < w; ++x, ++src) {
+ *src = CHANGE_COLORSPACE(*src);
+ }
+ }
+}
+
+
PngLoader::PngLoader()
{
image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
return true;
}
-unique_ptr<Surface> PngLoader::bitmap()
+unique_ptr<Surface> PngLoader::bitmap(uint32_t colorSpace)
{
if (!content) return nullptr;
+ if (this->colorSpace != colorSpace) {
+ this->colorSpace = colorSpace;
+ _changeColorSpace(content, w, h);
+ }
auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
- surface->buffer = (uint32_t*)(content);
+ surface->buffer = content;
surface->stride = w;
surface->w = w;
surface->h = h;
- surface->cs = SwCanvas::ARGB8888;
+ surface->cs = colorSpace;
return unique_ptr<Surface>(surface);
}
+
bool read() override;
bool close() override;
- unique_ptr<Surface> bitmap() override;
+ unique_ptr<Surface> bitmap(uint32_t colorSpace) override;
private:
png_imagep image = nullptr;
- const uint32_t* content = nullptr;
+ uint32_t* content = nullptr;
};
#endif //_TVG_PNG_LOADER_H_
/* Internal Class Implementation */
/************************************************************************/
+static inline uint32_t CHANGE_COLORSPACE(uint32_t c)
+{
+ return (c & 0xff000000) + ((c & 0x00ff0000)>>16) + (c & 0x0000ff00) + ((c & 0x000000ff)<<16);
+}
+
+
+static void _changeColorSpace(uint32_t* data, uint32_t w, uint32_t h)
+{
+ auto buffer = data;
+ for (uint32_t y = 0; y < h; ++y, buffer += w) {
+ auto src = buffer;
+ for (uint32_t x = 0; x < w; ++x, ++src) {
+ *src = CHANGE_COLORSPACE(*src);
+ }
+ }
+}
+
+
void JpgLoader::clear()
{
jpgdDelete(decoder);
}
-unique_ptr<Surface> JpgLoader::bitmap()
+unique_ptr<Surface> JpgLoader::bitmap(uint32_t colorSpace)
{
this->done();
if (!image) return nullptr;
+ if (this->colorSpace != colorSpace) {
+ this->colorSpace = colorSpace;
+ _changeColorSpace(reinterpret_cast<uint32_t*>(image), w, h);
+ }
auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
- surface->buffer = (uint32_t*)(image);
+ surface->buffer = reinterpret_cast<uint32_t*>(image);
surface->stride = static_cast<uint32_t>(w);
surface->w = static_cast<uint32_t>(w);
surface->h = static_cast<uint32_t>(h);
- surface->cs = SwCanvas::ARGB8888;
+ surface->cs = colorSpace;
return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- unique_ptr<Surface> bitmap() override;
+ unique_ptr<Surface> bitmap(uint32_t colorSpace) override;
void run(unsigned tid) override;
};
}
+static inline uint32_t CHANGE_COLORSPACE(uint32_t c)
+{
+ return (c & 0xff000000) + ((c & 0x00ff0000)>>16) + (c & 0x0000ff00) + ((c & 0x000000ff)<<16);
+}
+
+
+static void _changeColorSpace(uint32_t* data, uint32_t w, uint32_t h)
+{
+ auto buffer = data;
+ for (uint32_t y = 0; y < h; ++y, buffer += w) {
+ auto src = buffer;
+ for (uint32_t x = 0; x < w; ++x, ++src) {
+ *src = CHANGE_COLORSPACE(*src);
+ }
+ }
+}
+
+
void PngLoader::clear()
{
lodepng_state_cleanup(&state);
}
-unique_ptr<Surface> PngLoader::bitmap()
+unique_ptr<Surface> PngLoader::bitmap(uint32_t colorSpace)
{
this->done();
if (!image) return nullptr;
+ if (this->colorSpace != colorSpace) {
+ this->colorSpace = colorSpace;
+ _changeColorSpace(reinterpret_cast<uint32_t*>(image), w, h);
+ }
auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
- surface->buffer = (uint32_t*)(image);
+ surface->buffer = reinterpret_cast<uint32_t*>(image);
surface->stride = static_cast<uint32_t>(w);
surface->w = static_cast<uint32_t>(w);
surface->h = static_cast<uint32_t>(h);
- surface->cs = SwCanvas::ARGB8888;
+ surface->cs = colorSpace;
return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- unique_ptr<Surface> bitmap() override;
+ unique_ptr<Surface> bitmap(uint32_t colorSpace) override;
void run(unsigned tid) override;
};
/* Internal Class Implementation */
/************************************************************************/
+static inline uint32_t CHANGE_COLORSPACE(uint32_t c)
+{
+ return (c & 0xff000000) + ((c & 0x00ff0000)>>16) + (c & 0x0000ff00) + ((c & 0x000000ff)<<16);
+}
+
+
+static void _changeColorSpace(uint32_t* data, uint32_t w, uint32_t h)
+{
+ auto buffer = data;
+ for (uint32_t y = 0; y < h; ++y, buffer += w) {
+ auto src = buffer;
+ for (uint32_t x = 0; x < w; ++x, ++src) {
+ *src = CHANGE_COLORSPACE(*src);
+ }
+ }
+}
+
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
if (!content) return false;
memcpy((void*)content, data, sizeof(uint32_t) * w * h);
}
- else content = data;
+ else content = const_cast<uint32_t*>(data);
return true;
}
}
-unique_ptr<Surface> RawLoader::bitmap()
+unique_ptr<Surface> RawLoader::bitmap(uint32_t colorSpace)
{
if (!content) return nullptr;
+ if (this->colorSpace != colorSpace) {
+ this->colorSpace = colorSpace;
+ _changeColorSpace(content, w, h);
+ }
auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
- surface->buffer = (uint32_t*)(content);
- surface->stride = (uint32_t)w;
- surface->w = (uint32_t)w;
- surface->h = (uint32_t)h;
- surface->cs = SwCanvas::ARGB8888;
+ surface->buffer = content;
+ surface->stride = static_cast<uint32_t>(w);
+ surface->w = static_cast<uint32_t>(w);
+ surface->h = static_cast<uint32_t>(h);
+ surface->cs = colorSpace;
return unique_ptr<Surface>(surface);
}
class RawLoader : public LoadModule
{
public:
- const uint32_t* content = nullptr;
+ uint32_t* content = nullptr;
bool copy = false;
~RawLoader();
bool read() override;
bool close() override;
- unique_ptr<Surface> bitmap() override;
+ unique_ptr<Surface> bitmap(uint32_t colorSpace) override;
};