sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

Pixel array to 2d array representation #122

Closed Bk8 closed 1 month ago

Bk8 commented 1 year ago

I'm trying to integrate the lunasvg to a juce project, but I'm getting a weird behavior while trying to set the pixels to the 2d array representation in Juce. The color seems ok but not the order of the pixels, could someone help me please? The image renders corrrectly with the png demo.

`

auto document = Document::loadFromFile("focus.svg");
auto bitmap = document->renderToBitmap();
int height = document->height();
int width = document->width();

juce::Image img(juce::Image::PixelFormat::ARGB, width, height, true);

std::uint8_t* data = bitmap.data();

//RGBA to  ARGB 

for (int i = 0; i < width * height; i++)
{
    int x = i % width;
    int y = i / width;
    img.setPixelAt(x, y, juce::Colour(data[(4 * i) + 2], data[(4 * i) + 1], data[(4 * i) + 0], data[(4 * i) + 3]));
}

g.drawImageAt(img, 0, 0);`

image

sammycage commented 1 year ago

lunasvg default pixel format is ARGB Premultiplied alpha

Bk8 commented 1 year ago

Thank you!

Also the width and height of the document was different to the bitmap.

I changed this: int height = document->height(); int width = document->width(); to this: int height = bitmap.height(); int width =bitmap.width();

It worked but it's this ok? why are this values different in the document and the bitmap?

sammycage commented 1 year ago

why are this values different in the document and the bitmap?

Document::width Returns the width of the document as specified in the file Document::height Returns the height of the document as specified in the file

Bitmap::width Returns the width of the rendered document Bitmap::height Returns the height of the rendered document

Document::renderToBitmap can return a 'Bitmap' size that is different from the 'Document' size

So use this to be safe while handling Bitmap


int height = bitmap.height();
int width = bitmap.width()