/**
* @brief Gets the pixels information of the picture.
*
+ * @note The data must be pre-multiplied by the alpha channels.
+ *
* @warning Please do not use it, this API is not official one. It could be modified in the next version.
*
* @BETA_API
}
-RenderData GlRenderer::prepare(TVG_UNUSED const Picture& picture, TVG_UNUSED RenderData data, TVG_UNUSED const RenderTransform* transform, TVG_UNUSED uint32_t opacity, TVG_UNUSED Array<RenderData>& clips, TVG_UNUSED RenderUpdateFlag flags)
+RenderData GlRenderer::prepare(TVG_UNUSED Surface* image, TVG_UNUSED RenderData data, TVG_UNUSED const RenderTransform* transform, TVG_UNUSED uint32_t opacity, TVG_UNUSED Array<RenderData>& clips, TVG_UNUSED RenderUpdateFlag flags)
{
//TODO:
return nullptr;
Surface surface = {nullptr, 0, 0, 0};
RenderData prepare(const Shape& shape, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
- RenderData prepare(const Picture& picture, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
+ RenderData prepare(Surface* image, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
bool preRender() override;
bool renderShape(RenderData data) override;
bool renderImage(RenderData data) override;
void strokeFree(SwStroke* stroke);
bool imagePrepare(SwImage* image, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid);
-bool imageGenRle(SwImage* image, TVG_UNUSED const Picture* pdata, const SwBBox& renderRegion, bool antiAlias);
+bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias);
void imageDelOutline(SwImage* image, SwMpool* mpool, uint32_t tid);
void imageReset(SwImage* image);
void imageFree(SwImage* image);
}
-bool imageGenRle(SwImage* image, TVG_UNUSED const Picture* pdata, const SwBBox& renderRegion, bool antiAlias)
+bool imageGenRle(SwImage* image, const SwBBox& renderRegion, bool antiAlias)
{
if ((image->rle = rleRender(image->rle, image->outline, renderRegion, antiAlias))) return true;
void rasterUnpremultiply(SwSurface* surface)
{
- //TODO: Create simd avx and neon version
+ //OPTIMIZE_ME: +SIMD
for (uint32_t y = 0; y < surface->h; y++) {
auto buffer = surface->buffer + surface->stride * y;
for (uint32_t x = 0; x < surface->w; ++x) {
//TODO: case: _rasterRGBImage()
//TODO: case: _rasterGrayscaleImage()
//TODO: case: _rasterAlphaImage()
-
return _rasterRGBAImage(surface, image, transform, bbox, opacity);
-}
+}
\ No newline at end of file
struct SwImageTask : SwTask
{
SwImage image;
- const Picture* pdata = nullptr;
void run(unsigned tid) override
{
//Invisible shape turned to visible by alpha.
if ((flags & (RenderUpdateFlag::Image | RenderUpdateFlag::Transform | RenderUpdateFlag::Color)) && (opacity > 0)) {
imageReset(&image);
-
- image.data = const_cast<uint32_t*>(pdata->data(&image.w, &image.h));
if (!image.data || image.w == 0 || image.h == 0) goto end;
- image.stride = image.w; //same, pixel buffer size.
if (!imagePrepare(&image, transform, clipRegion, bbox, mpool, tid)) goto end;
if (clips.count > 0) {
- if (!imageGenRle(&image, pdata, bbox, false)) goto end;
+ if (!imageGenRle(&image, bbox, false)) goto end;
if (image.rle) {
for (auto clip = clips.data; clip < (clips.data + clips.count); ++clip) {
auto clipper = &static_cast<SwShapeTask*>(*clip)->shape;
}
-RenderData SwRenderer::prepare(const Picture& pdata, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags)
+RenderData SwRenderer::prepare(Surface* image, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags)
{
//prepare task
auto task = static_cast<SwImageTask*>(data);
if (!task) {
task = new SwImageTask;
- if (!task) return nullptr;
- task->pdata = &pdata;
+ if (flags & RenderUpdateFlag::Image) {
+ task->image.data = image->buffer;
+ task->image.w = image->w;
+ task->image.h = image->h;
+ task->image.stride = image->stride;
+ }
}
return prepareCommon(task, transform, opacity, clips, flags);
}
auto task = static_cast<SwShapeTask*>(data);
if (!task) {
task = new SwShapeTask;
- if (!task) return nullptr;
task->sdata = &sdata;
}
return prepareCommon(task, transform, opacity, clips, flags);
{
public:
RenderData prepare(const Shape& shape, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
- RenderData prepare(const Picture& picture, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
+ RenderData prepare(Surface* image, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) override;
bool preRender() override;
bool renderShape(RenderData data) override;
bool renderImage(RenderData data) override;
#ifndef _TVG_LOAD_MODULE_H_
#define _TVG_LOAD_MODULE_H_
-#include "tvgCommon.h"
+#include "tvgRender.h"
namespace tvg
{
virtual ~LoadModule() {}
- virtual bool open(const string& path) { return false; };
- virtual bool open(const char* data, uint32_t size, bool copy) { return false; };
- virtual bool open(const uint32_t* data, uint32_t w, uint32_t h, bool copy) { return false; };
+ virtual bool open(const string& path) { return false; }
+ virtual bool open(const char* data, uint32_t size, bool copy) { return false; }
+ virtual bool open(const uint32_t* data, uint32_t w, uint32_t h, bool copy) { return false; }
//Override this if the vector-format has own resizing policy.
- virtual bool resize(Paint* paint, float w, float h) { return false; };
+ virtual bool resize(Paint* paint, float w, float h) { return false; }
virtual bool read() = 0;
virtual bool close() = 0;
- virtual const uint32_t* pixels() { return nullptr; };
- virtual unique_ptr<Paint> paint() { return nullptr; };
+ virtual unique_ptr<Surface> bitmap() { return nullptr; }
+ virtual unique_ptr<Paint> paint() { return nullptr; }
};
}
if (w) *w = 0;
if (h) *h = 0;
}
- return pImpl->pixels;
+ if (pImpl->surface) return pImpl->surface->buffer;
+ else return nullptr;
}
\ No newline at end of file
struct Picture::Impl
{
shared_ptr<LoadModule> loader = nullptr;
- Paint* paint = nullptr;
- uint32_t* pixels = nullptr;
+
+ Paint* paint = nullptr; //vector picture uses
+ Surface* surface = nullptr; //bitmap picture uses
+
Picture* picture = nullptr;
void* rdata = nullptr; //engine data
float w = 0, h = 0;
~Impl()
{
if (paint) delete(paint);
+ free(surface);
}
bool dispose(RenderMethod& renderer)
bool ret = true;
if (paint) {
ret = paint->pImpl->dispose(renderer);
- } else if (pixels) {
+ } else if (surface) {
ret = renderer.dispose(rdata);
rdata = nullptr;
}
if (paint) return RenderUpdateFlag::None;
}
}
- if (!pixels) {
- pixels = const_cast<uint32_t*>(loader->pixels());
+ free(surface);
+ if ((surface = loader->bitmap().release())) {
loader->close();
- if (pixels) return RenderUpdateFlag::Image;
+ return RenderUpdateFlag::Image;
}
}
return RenderUpdateFlag::None;
{
auto flag = reload();
- if (pixels) {
+ if (surface) {
auto transform = resizeTransform(pTransform);
- rdata = renderer.prepare(*picture, rdata, &transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
+ rdata = renderer.prepare(surface, rdata, &transform, opacity, clips, static_cast<RenderUpdateFlag>(pFlag | flag));
} else if (paint) {
if (resizing) {
loader->resize(paint, w, h);
bool render(RenderMethod &renderer)
{
- if (pixels) return renderer.renderImage(rdata);
+ if (surface) return renderer.renderImage(rdata);
else if (paint) return paint->pImpl->render(renderer);
return false;
}
Result load(const string& path)
{
- if (paint || pixels) return Result::InsufficientCondition;
+ if (paint || surface) return Result::InsufficientCondition;
if (loader) loader->close();
bool invalid; //Invalid Path
loader = LoaderMgr::loader(path, &invalid);
Result load(const char* data, uint32_t size, const string& mimeType, bool copy)
{
- if (paint || pixels) return Result::InsufficientCondition;
+ if (paint || surface) return Result::InsufficientCondition;
if (loader) loader->close();
loader = LoaderMgr::loader(data, size, mimeType, copy);
if (!loader) return Result::NonSupport;
Result load(uint32_t* data, uint32_t w, uint32_t h, bool copy)
{
- if (paint || pixels) return Result::InsufficientCondition;
+ if (paint || surface) return Result::InsufficientCondition;
if (loader) loader->close();
loader = LoaderMgr::loader(data, w, h, copy);
if (!loader) return Result::NonSupport;
if (paint) dup->paint = paint->duplicate();
dup->loader = loader;
- dup->pixels = pixels;
+ if (surface) {
+ dup->surface = static_cast<Surface*>(malloc(sizeof(Surface)));
+ *dup->surface = *surface;
+ }
dup->w = w;
dup->h = h;
dup->resizing = resizing;
RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs);
};
+
class RenderMethod
{
public:
virtual ~RenderMethod() {}
virtual RenderData prepare(const Shape& shape, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) = 0;
- virtual RenderData prepare(const Picture& picture, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) = 0;
+ virtual RenderData prepare(Surface* image, RenderData data, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flags) = 0;
virtual bool preRender() = 0;
virtual bool renderShape(RenderData data) = 0;
virtual bool renderImage(RenderData data) = 0;
}
-const uint32_t* JpgLoader::pixels()
+unique_ptr<Surface> JpgLoader::bitmap()
{
- return (const uint32_t*) image;
+ if (!image) return nullptr;
+
+ 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;
+
+ return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- const uint32_t* pixels() override;
+ unique_ptr<Surface> bitmap() override;
private:
void clear();
#include "tvgLoader.h"
#include "tvgPngLoader.h"
+static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
+{
+ return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) +
+ ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff));
+}
+
+
+static void _premultiply(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 = ALPHA_BLEND(*src, (*src >> 24));
+ }
+ }
+}
+
+
PngLoader::PngLoader()
{
image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
return true;
}
+
bool PngLoader::read()
{
png_bytep buffer;
if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) return false;
content = reinterpret_cast<uint32_t*>(buffer);
+ _premultiply(reinterpret_cast<uint32_t*>(buffer), image->width, image->height);
+
return true;
}
return true;
}
-const uint32_t* PngLoader::pixels()
+unique_ptr<Surface> PngLoader::bitmap()
{
- return this->content;
+ if (!content) return nullptr;
+
+ auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
+ surface->buffer = (uint32_t*)(content);
+ surface->stride = w;
+ surface->w = w;
+ surface->h = h;
+ surface->cs = SwCanvas::ARGB8888;
+
+ return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- const uint32_t* pixels() override;
+ unique_ptr<Surface> bitmap() override;
private:
png_imagep image = nullptr;
}
-const uint32_t* JpgLoader::pixels()
+unique_ptr<Surface> JpgLoader::bitmap()
{
this->done();
- return (const uint32_t*)image;
+ if (!image) return nullptr;
+
+ 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;
+
+ return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- const uint32_t* pixels() override;
+ unique_ptr<Surface> bitmap() override;
void run(unsigned tid) override;
};
/* Internal Class Implementation */
/************************************************************************/
+
+static inline uint32_t ALPHA_BLEND(uint32_t c, uint32_t a)
+{
+ return (((((c >> 8) & 0x00ff00ff) * a + 0x00ff00ff) & 0xff00ff00) +
+ ((((c & 0x00ff00ff) * a + 0x00ff00ff) >> 8) & 0x00ff00ff));
+}
+
+
+static void _premultiply(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 = ALPHA_BLEND(*src, (*src >> 24));
+ }
+ }
+}
+
+
void PngLoader::clear()
{
lodepng_state_cleanup(&state);
}
-const uint32_t* PngLoader::pixels()
+unique_ptr<Surface> PngLoader::bitmap()
{
this->done();
- return (const uint32_t*) image;
+ if (!image) return nullptr;
+
+ 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;
+
+ return unique_ptr<Surface>(surface);
}
auto height = static_cast<unsigned>(h);
lodepng_decode(&image, &width, &height, &state, data, size);
+
+ _premultiply((uint32_t*)(image), width, height);
}
\ No newline at end of file
bool read() override;
bool close() override;
- const uint32_t* pixels() override;
+ unique_ptr<Surface> bitmap() override;
void run(unsigned tid) override;
};
}
-const uint32_t* RawLoader::pixels()
+unique_ptr<Surface> RawLoader::bitmap()
{
- return this->content;
+ if (!content) return nullptr;
+
+ auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
+ surface->buffer = (uint32_t*)(content);
+ surface->stride = w;
+ surface->w = w;
+ surface->h = h;
+ surface->cs = SwCanvas::ARGB8888;
+
+ return unique_ptr<Surface>(surface);
}
bool read() override;
bool close() override;
- const uint32_t* pixels() override;
+ unique_ptr<Surface> bitmap() override;
};