Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip image #158

Open
GuckTubeYT opened this issue Feb 1, 2022 · 7 comments
Open

Flip image #158

GuckTubeYT opened this issue Feb 1, 2022 · 7 comments

Comments

@GuckTubeYT
Copy link

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
Copy link
Contributor

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.

@GuckTubeYT
Copy link
Author

GuckTubeYT commented Feb 1, 2022

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
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

GuckTubeYT commented Feb 3, 2022

Please anyone help me

@AllocatedArtist
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants