libspng icon indicating copy to clipboard operation
libspng copied to clipboard

Added option to flip image on encode

Open nedelec opened this issue 2 years ago • 8 comments

This is adding an option to flip the image vertically upon encoding. I do not understand the structure of the code, but the modifications are local to spng_encode_image(), and they work for me. All the best!

nedelec avatar Mar 21 '22 13:03 nedelec

FYI this (and the other vertical flip PR) need more work to be merged: the flag should be rejected in progressive mode, updates to documentation and at least one unit test (annoying I know).

randy408 avatar Sep 16 '22 20:09 randy408

Now it is possible to flip the image, if yes, how? And then when saving an OGL-screenshot you have to flip the image manually:

for (int i = 0; i < Screenshot.screenHeight; ++i)
   for (int j = 0; j < Screenshot.screenWidth * 3; ++j)
      image[i * Screenshot.screenWidth * 3 + j] = Screenshot.dataBuffer[image_size - (i + 1) * Screenshot.screenWidth * 3 + j];

native-engine avatar Jun 10 '23 21:06 native-engine

@daniilpetrov without this feature you can still flip it using progressive encode by starting with the last row and going backwards

randy408 avatar Jun 12 '23 05:06 randy408

Yes, that is true, but it will be slow and is more lines of code than the modification I did on your code. Besides, I can see that other people are possibly using OpenGL and would benefit from this, so from my point of view that seems like an interesting feature to have. Sorry I have little time

nedelec avatar Jun 12 '23 05:06 nedelec

I agree it is a useful feature but right now I'm working on APNG support, I'll try to include this some time after.

randy408 avatar Jun 12 '23 05:06 randy408

That is reasonable. I will try to find time to document the feature later this summer. Thank you very much for libspng!

nedelec avatar Jun 12 '23 05:06 nedelec

@daniilpetrov without this feature you can still flip it using progressive encode by starting with the last row and going backwards

How to do this?

native-engine avatar Jun 12 '23 21:06 native-engine

@daniilpetrov without this feature you can still flip it using progressive encode by starting with the last row and going backwards

How to do this?

You have to add the SPNG_ENCODE_PROGRESSIVE flag for spng_encode_image() and call spng_encode_row() for each row, it would look something like this:

spng_encode_image(ctx, NULL, 0, fmt, SPNG_ENCODE_PROGRESSIVE | SPNG_ENCODE_FINALIZE);

size_t image_width = image_size / Screenshot.screenHeight;
size_t image_offset = image_size;

do
{
    image_offset -= image_width;

    ret = spng_encode_row(ctx, image + image_offset, image_width);
}while(!ret)

if(ret != SPNG_EOI) /* error */

Note this code assumes you're not setting ihdr.interlace_method to 1, with interlaced images it's slightly more complicated.

randy408 avatar Jun 13 '23 05:06 randy408