lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.03k stars 420 forks source link

Flip image #158

Open GuckTubeYT opened 2 years ago

GuckTubeYT commented 2 years ago

So, i want to flip image, but how do i flip the image? what is the function of flip image?

Sorry if my grammar is bad

cosinekitty commented 2 years ago

I don't have much time right now, but the basic outline will be:

GuckTubeYT commented 2 years ago

I don't have much time right now, but the basic outline will be:

* Load the PNG image from a file into an array buffer using `lodepng_load_file` (C) or `lodepng::load_file` (C++). C++ will be easier to deal with memory because it uses `vector`.

* Swap pairs of pixels inside the array buffer. The array will be quadruplets of numbers (red, green, blue, alpha).

* Save the array buffer back to another PNG file using `lodepng_encode_file` or `lodepng::encode`.
  I hope this helps.

i still dont understand about Swap pairs of pixels inside the array buffer. The array will be quadruplets of numbers (red, green, blue, alpha). , but thanks for the answer

nigels-com commented 2 years ago

Can be done in place, using std::swap Iterate line-by-line from the start, and line-by-line from the last line. Swap each channel for each pixel in each line.

GuckTubeYT commented 2 years ago

Can be done in place, using std::swap Iterate line-by-line from the start, and line-by-line from the last line. Swap each channel for each pixel in each line.

so, it will like this? std::swap(vectorresult, vectorflip);

GuckTubeYT commented 2 years ago

Can be done in place, using std::swap Iterate line-by-line from the start, and line-by-line from the last line. Swap each channel for each pixel in each line.

btw, can you make the example?

GuckTubeYT commented 2 years ago

Please anyone help me

AllocatedArtist commented 12 months ago

It's a little late, but for anyone struggling with figuring out how to flip an image, you can take a look at the vertical flip function in stb_image: stbi__vertical_flip, which can be ported to your use case pretty easily. For a more C++ oriented version:

//where bytes_per_pixel is sizeof(unsigned char) * number_of_channels (usually 4)
size_t bytes_per_row = static_cast<size_t>(width) * bytes_per_pixel;

for (int row = 0; row < (h>>1); ++row) {
  auto row0 = image.begin() + row * bytes_per_row;
  auto row1 = image.begin() + (height - row - 1) * bytes_per_row;

  std::swap_ranges(row0, row0 + bytes_per_row, row1); 
}

At the moment I'm not sure if there is already a function to do this within lodepng itself, but the above solution works for me.