loaders: Remove the color space conversion on the loader side.

Now, the job is completely delegated to the backend engine.
This commit is contained in:
Hermet Park 2023-04-27 17:36:12 +09:00
parent b733030357
commit c710af2560
17 changed files with 37 additions and 167 deletions

View file

@ -123,13 +123,6 @@ bool GlRenderer::endComposite(TVG_UNUSED Compositor* cmp)
} }
ColorSpace GlRenderer::colorSpace()
{
//TODO: return a proper color space value.
return ColorSpace::Unsupported;
}
bool GlRenderer::renderImage(TVG_UNUSED void* data) bool GlRenderer::renderImage(TVG_UNUSED void* data)
{ {
//TODO: render requested images //TODO: render requested images

View file

@ -50,8 +50,6 @@ public:
bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) override; bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) override;
bool endComposite(Compositor* cmp) override; bool endComposite(Compositor* cmp) override;
ColorSpace colorSpace() override;
static GlRenderer* gen(); static GlRenderer* gen();
static int init(TVG_UNUSED uint32_t threads); static int init(TVG_UNUSED uint32_t threads);
static int32_t init(); static int32_t init();

View file

@ -761,13 +761,6 @@ SwRenderer::SwRenderer():mpool(globalMpool)
} }
ColorSpace SwRenderer::colorSpace()
{
if (surface) return surface->cs;
return ColorSpace::Unsupported;
}
bool SwRenderer::init(uint32_t threads) bool SwRenderer::init(uint32_t threads)
{ {
if ((initEngineCnt++) > 0) return true; if ((initEngineCnt++) > 0) return true;

View file

@ -58,8 +58,6 @@ public:
bool endComposite(Compositor* cmp) override; bool endComposite(Compositor* cmp) override;
void clearCompositors(); void clearCompositors();
ColorSpace colorSpace() override;
static SwRenderer* gen(); static SwRenderer* gen();
static bool init(uint32_t threads); static bool init(uint32_t threads);
static int32_t init(); static int32_t init();

View file

@ -37,7 +37,7 @@ public:
float vw = 0; float vw = 0;
float vh = 0; float vh = 0;
float w = 0, h = 0; //default image size float w = 0, h = 0; //default image size
ColorSpace cs = ColorSpace::ARGB8888; ColorSpace cs = ColorSpace::Unsupported; //must be clarified at open()
virtual ~LoadModule() {} virtual ~LoadModule() {}
@ -50,7 +50,7 @@ public:
virtual bool read() = 0; virtual bool read() = 0;
virtual bool close() = 0; virtual bool close() = 0;
virtual unique_ptr<Surface> bitmap(ColorSpace cs) { return nullptr; } virtual unique_ptr<Surface> bitmap() { return nullptr; }
virtual unique_ptr<Paint> paint() { return nullptr; } virtual unique_ptr<Paint> paint() { return nullptr; }
}; };

View file

@ -66,7 +66,6 @@ struct Picture::Impl
Surface* surface = nullptr; //bitmap picture uses Surface* surface = nullptr; //bitmap picture uses
RenderData rd = nullptr; //engine data RenderData rd = nullptr; //engine data
float w = 0, h = 0; float w = 0, h = 0;
ColorSpace rendererColorSpace = ColorSpace::Unsupported;
RenderMesh rm; //mesh data RenderMesh rm; //mesh data
bool resizing = false; bool resizing = false;
@ -105,7 +104,7 @@ struct Picture::Impl
} }
} }
free(surface); free(surface);
if ((surface = loader->bitmap(rendererColorSpace).release())) { if ((surface = loader->bitmap().release())) {
loader->close(); loader->close();
return RenderUpdateFlag::Image; return RenderUpdateFlag::Image;
} }
@ -129,7 +128,6 @@ struct Picture::Impl
RenderData update(RenderMethod &renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag, bool clipper) RenderData update(RenderMethod &renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag, bool clipper)
{ {
rendererColorSpace = renderer.colorSpace();
auto flag = reload(); auto flag = reload();
if (surface) { if (surface) {

View file

@ -226,8 +226,6 @@ public:
virtual Compositor* target(const RenderRegion& region) = 0; virtual Compositor* target(const RenderRegion& region) = 0;
virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) = 0; virtual bool beginComposite(Compositor* cmp, CompositeMethod method, uint32_t opacity) = 0;
virtual bool endComposite(Compositor* cmp) = 0; virtual bool endComposite(Compositor* cmp) = 0;
virtual ColorSpace colorSpace() = 0;
}; };
} }

View file

@ -29,43 +29,6 @@
/* Internal Class Implementation */ /* Internal Class Implementation */
/************************************************************************/ /************************************************************************/
static uint32_t convertColorSpaceType(ColorSpace cs)
{
uint32_t tjpfColorSpace = TJPF_RGBX;
switch (cs) {
case ColorSpace::ARGB8888:
case ColorSpace::ARGB8888S:
default:
tjpfColorSpace = TJPF_BGRX;
break;
case ColorSpace::ABGR8888:
case ColorSpace::ABGR8888S:
tjpfColorSpace = TJPF_RGBX;
break;
}
return tjpfColorSpace;
}
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() void JpgLoader::clear()
{ {
if (freeData) free(data); if (freeData) free(data);
@ -124,6 +87,7 @@ bool JpgLoader::open(const string& path)
w = static_cast<float>(width); w = static_cast<float>(width);
h = static_cast<float>(height); h = static_cast<float>(height);
cs = ColorSpace::ARGB8888;
ret = true; ret = true;
goto finalize; goto finalize;
@ -156,6 +120,7 @@ bool JpgLoader::open(const char* data, uint32_t size, bool copy)
w = static_cast<float>(width); w = static_cast<float>(width);
h = static_cast<float>(height); h = static_cast<float>(height);
cs = ColorSpace::ARGB8888;
this->size = size; this->size = size;
return true; return true;
@ -164,12 +129,14 @@ bool JpgLoader::open(const char* data, uint32_t size, bool copy)
bool JpgLoader::read() bool JpgLoader::read()
{ {
/* OPTIMIZE: We assume the desired colorspace is ColorSpace::ARGB
How could we notice the renderer colorspace at this time? */
if (image) tjFree(image); if (image) tjFree(image);
image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[convertColorSpaceType(cs)]); image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[TJPF_BGRX]);
if (!image) return false; if (!image) return false;
//decompress jpg image //decompress jpg image
if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), convertColorSpaceType(cs), 0) < 0) { if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), TJPF_BGRX, 0) < 0) {
TVGERR("JPG LOADER", "%s", tjGetErrorStr()); TVGERR("JPG LOADER", "%s", tjGetErrorStr());
tjFree(image); tjFree(image);
image = nullptr; image = nullptr;
@ -187,13 +154,9 @@ bool JpgLoader::close()
} }
unique_ptr<Surface> JpgLoader::bitmap(ColorSpace cs) unique_ptr<Surface> JpgLoader::bitmap()
{ {
if (!image) return nullptr; if (!image) return nullptr;
if (this->cs != cs) {
this->cs = cs;
_changeColorSpace(reinterpret_cast<uint32_t*>(image), w, h);
}
auto surface = static_cast<Surface*>(malloc(sizeof(Surface))); auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = (uint32_t*)(image); surface->buffer = (uint32_t*)(image);

View file

@ -38,7 +38,7 @@ public:
bool read() override; bool read() override;
bool close() override; bool close() override;
unique_ptr<Surface> bitmap(ColorSpace cs) override; unique_ptr<Surface> bitmap() override;
private: private:
void clear(); void clear();

View file

@ -23,6 +23,10 @@
#include "tvgLoader.h" #include "tvgLoader.h"
#include "tvgPngLoader.h" #include "tvgPngLoader.h"
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
static inline uint32_t PREMULTIPLY(uint32_t c) static inline uint32_t PREMULTIPLY(uint32_t c)
{ {
auto a = (c >> 24); auto a = (c >> 24);
@ -42,23 +46,9 @@ static void _premultiply(uint32_t* data, uint32_t w, uint32_t h)
} }
static inline uint32_t CHANGE_COLORSPACE(uint32_t c) /************************************************************************/
{ /* External Class Implementation */
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() PngLoader::PngLoader()
{ {
@ -84,6 +74,7 @@ bool PngLoader::open(const string& path)
w = (float)image->width; w = (float)image->width;
h = (float)image->height; h = (float)image->height;
cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -96,6 +87,7 @@ bool PngLoader::open(const char* data, uint32_t size, bool copy)
w = (float)image->width; w = (float)image->width;
h = (float)image->height; h = (float)image->height;
cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -128,13 +120,9 @@ bool PngLoader::close()
return true; return true;
} }
unique_ptr<Surface> PngLoader::bitmap(ColorSpace cs) unique_ptr<Surface> PngLoader::bitmap()
{ {
if (!content) return nullptr; if (!content) return nullptr;
if (this->cs != cs) {
this->cs = cs;
_changeColorSpace(content, w, h);
}
auto surface = static_cast<Surface*>(malloc(sizeof(Surface))); auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = content; surface->buffer = content;

View file

@ -37,7 +37,7 @@ public:
bool read() override; bool read() override;
bool close() override; bool close() override;
unique_ptr<Surface> bitmap(ColorSpace cs) override; unique_ptr<Surface> bitmap() override;
private: private:
png_imagep image = nullptr; png_imagep image = nullptr;

View file

@ -28,24 +28,6 @@
/* Internal Class Implementation */ /* 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() void JpgLoader::clear()
{ {
jpgdDelete(decoder); jpgdDelete(decoder);
@ -79,6 +61,7 @@ bool JpgLoader::open(const string& path)
w = static_cast<float>(width); w = static_cast<float>(width);
h = static_cast<float>(height); h = static_cast<float>(height);
cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -104,6 +87,7 @@ bool JpgLoader::open(const char* data, uint32_t size, bool copy)
w = static_cast<float>(width); w = static_cast<float>(width);
h = static_cast<float>(height); h = static_cast<float>(height);
cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -128,15 +112,11 @@ bool JpgLoader::close()
} }
unique_ptr<Surface> JpgLoader::bitmap(ColorSpace cs) unique_ptr<Surface> JpgLoader::bitmap()
{ {
this->done(); this->done();
if (!image) return nullptr; if (!image) return nullptr;
if (this->cs != cs) {
this->cs = cs;
_changeColorSpace(reinterpret_cast<uint32_t*>(image), static_cast<uint32_t>(w), static_cast<uint32_t>(h));
}
auto surface = static_cast<Surface*>(malloc(sizeof(Surface))); auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = reinterpret_cast<uint32_t*>(image); surface->buffer = reinterpret_cast<uint32_t*>(image);

View file

@ -45,7 +45,7 @@ public:
bool read() override; bool read() override;
bool close() override; bool close() override;
unique_ptr<Surface> bitmap(ColorSpace cs) override; unique_ptr<Surface> bitmap() override;
void run(unsigned tid) override; void run(unsigned tid) override;
}; };

View file

@ -49,24 +49,6 @@ static void _premultiply(uint32_t* data, uint32_t w, uint32_t h)
} }
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() void PngLoader::clear()
{ {
lodepng_state_cleanup(&state); lodepng_state_cleanup(&state);
@ -123,9 +105,11 @@ bool PngLoader::open(const string& path)
w = static_cast<float>(width); w = static_cast<float>(width);
h = static_cast<float>(height); h = static_cast<float>(height);
ret = true;
if (state.info_png.color.colortype == LCT_RGBA) cs = ColorSpace::ABGR8888; if (state.info_png.color.colortype == LCT_RGBA) cs = ColorSpace::ABGR8888;
else cs = ColorSpace::ARGB8888;
ret = true;
goto finalize; goto finalize;
@ -162,6 +146,7 @@ bool PngLoader::open(const char* data, uint32_t size, bool copy)
this->size = size; this->size = size;
if (state.info_png.color.colortype == LCT_RGBA) cs = ColorSpace::ABGR8888; if (state.info_png.color.colortype == LCT_RGBA) cs = ColorSpace::ABGR8888;
else cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -185,16 +170,10 @@ bool PngLoader::close()
} }
unique_ptr<Surface> PngLoader::bitmap(ColorSpace cs) unique_ptr<Surface> PngLoader::bitmap()
{ {
this->done(); this->done();
if (!image) return nullptr;
if (this->cs != cs) {
this->cs = cs;
_changeColorSpace(reinterpret_cast<uint32_t*>(image), static_cast<uint32_t>(w), static_cast<uint32_t>(h));
}
auto surface = static_cast<Surface*>(malloc(sizeof(Surface))); auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = reinterpret_cast<uint32_t*>(image); surface->buffer = reinterpret_cast<uint32_t*>(image);
surface->stride = static_cast<uint32_t>(w); surface->stride = static_cast<uint32_t>(w);

View file

@ -48,7 +48,7 @@ public:
bool read() override; bool read() override;
bool close() override; bool close() override;
unique_ptr<Surface> bitmap(ColorSpace cs) override; unique_ptr<Surface> bitmap() override;
void run(unsigned tid) override; void run(unsigned tid) override;
}; };

View file

@ -29,22 +29,6 @@
/* Internal Class Implementation */ /* 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 */ /* External Class Implementation */
@ -74,6 +58,8 @@ bool RawLoader::open(const uint32_t* data, uint32_t w, uint32_t h, bool copy)
} }
else content = const_cast<uint32_t*>(data); else content = const_cast<uint32_t*>(data);
cs = ColorSpace::ARGB8888;
return true; return true;
} }
@ -90,13 +76,9 @@ bool RawLoader::close()
} }
unique_ptr<Surface> RawLoader::bitmap(ColorSpace cs) unique_ptr<Surface> RawLoader::bitmap()
{ {
if (!content) return nullptr; if (!content) return nullptr;
if (this->cs != cs) {
this->cs = cs;
_changeColorSpace(content, static_cast<uint32_t>(w), static_cast<uint32_t>(h));
}
auto surface = static_cast<Surface*>(malloc(sizeof(Surface))); auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
surface->buffer = content; surface->buffer = content;

View file

@ -36,7 +36,7 @@ public:
bool read() override; bool read() override;
bool close() override; bool close() override;
unique_ptr<Surface> bitmap(ColorSpace cs) override; unique_ptr<Surface> bitmap() override;
}; };