sammycage / lunasvg

lunasvg is a standalone SVG rendering library in C++
MIT License
818 stars 115 forks source link

'Tiger' renders with the wrong colours? #144

Closed g40 closed 6 months ago

g40 commented 7 months ago

Building the latest using VS2022 on Windows 10x64, svg2png produces the image below. Most odd, any thoughts?

tiger svg

igagis commented 7 months ago

BGR -> RGB

g40 commented 7 months ago

Indeed. That could explain it. But there is not much space in this code in which to make such a mistake:

filename = "tiger.svg";
auto document = lunasvg::Document::loadFromFile(filename);
auto bitmap = document->renderToBitmap(width, height, bgColor);
filename += _T(".png");
stbi_write_png(filename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);
sammycage commented 7 months ago

Certainly, it looks like adding bitmap.convertToRGBA(); before saving the PNG would ensure the correct format. Here's the adjusted code:

filename = "tiger.svg";
auto document = lunasvg::Document::loadFromFile(filename);
auto bitmap = document->renderToBitmap(width, height, bgColor);
bitmap.convertToRGBA(); // Add this line to ensure correct format
filename += _T(".png");
stbi_write_png(filename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);

This should help ensure that the bitmap is in the RGBA format before saving it as a PNG.

g40 commented 7 months ago

@sammycage Thank you. It's just slightly confusing.

What is the native internal color format then? Does it vary according to platform?

For example, https://github.com/g40/lunasvg/blob/master/svg2png.cpp calls bitmap.convertToRGBA() which implies something else.