[Solved] C++ Inverting all bits in a fstream


This is an X-Y problem. I really doubt you want to flip all the bits in a PNG format file, simply because there are other fields in the file besides the bitmap bits. Also, unless the image is pure black & white, there is more to the color bits than inverting the bits.

That said, here’s how to flip the bits.

While not the end of the file, do:  
  read a block of bytes (uint8_t)  
  for each byte read do:  
     read the byte  
     invert the byte (a.k.a. using `~` operator)  
     store the byte  
  end-for  
  write block to new file.  
end-while  

Side effects of inverting pixels
Most images are made up of pixels, or picture elements. These elements are usually represented by a number of bits per pixel and if multiple colors, bits per color.

Let us take for example an RGB image with 24 bits per pixel. This means that there are 8 bits to represent the red, 8 bits for green and 8 bits for blue. Each color has a value range of 0 to 255. This represents the amount of color.

Let us take 1 color, green, with a value of 0x55 or in binary 0101 0101. Inverting (flipping) the bits will yield a value of 0xAA or 1010 1010. So after flipping the green value is now 0xAA.

If this is what you want to happen, changing the amount of color on each pixel, then you will need to extract the color quantities from the PNG file for the image and invert them.

solved C++ Inverting all bits in a fstream