title: LibPng Background
body: <p>Ok so in photoshop, I created an 8-bit Colour image with a transparent background and some text. I then created an image that is 16-bit Colour with transparent background and some text.</p>
  
  <p>When I right click both images and go to the properties, it shows 32 bitdepth for both :l
  So I decide to read the image with LibPng using the following settings:</p>
  
  <pre><code>typedef union RGB     //Holds all the pixels..
  {
      uint32_t Color;
      struct
      {
          unsigned char B, G, R, A;
      } RGBA;
  } *PRGB;
  
  
  channels = png_get_channels(PngPointer, InfoPointer);
      png_get_IHDR(PngPointer, InfoPointer, &amp;width, &amp;height, &amp;bitdepth, &amp;colortype, &amp;interlacetype, nullptr, nullptr);
      png_set_interlace_handling(PngPointer);
      png_set_strip_16(PngPointer);
      png_set_packing(PngPointer);
  
      switch (colortype)
      {
          case PNG_COLOR_TYPE_GRAY:
          {
              png_set_expand_gray_1_2_4_to_8(PngPointer);
              png_set_expand(PngPointer);
              png_set_bgr(PngPointer);
              break;
          }
  
          case PNG_COLOR_TYPE_PALETTE:
          {
              png_set_palette_to_rgb(PngPointer);
              if (png_get_valid(PngPointer, InfoPointer, PNG_INFO_tRNS))
                  png_set_tRNS_to_alpha(PngPointer);
              png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
              png_set_bgr(PngPointer);
          BitsPerPixel = 24;
              break;
          }
  
          case PNG_COLOR_TYPE_GRAY_ALPHA:
          {
              png_set_gray_to_rgb(PngPointer);
              break;
          }
  
          case PNG_COLOR_TYPE_RGB:
          {
              png_set_bgr(PngPointer);
              png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
              BitsPerPixel = 24;
              break;
          }
  
          case PNG_COLOR_TYPE_RGBA:
          {
              png_set_bgr(PngPointer);
              BitsPerPixel = 32;
              break;
          }
  
          default: png_destroy_read_struct(&amp;PngPointer, &amp;InfoPointer, nullptr); throw std::runtime_error("Error: Png Type not supported."); break;
      }
  
      //SET BACKGROUND
      /*png_color_16 my_background, *image_background;
      my_background.red = 255;
      my_background.green = 255;
      my_background.blue = 255;
      image_background = &amp;my_background;
  
      if (png_get_bKGD(PngPointer, InfoPointer, &amp;image_background))
      {
          png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
          png_set_background(PngPointer, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
      }
      else
      {
          png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
          png_set_background(PngPointer, &amp;my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
      }*/
      //END SET BACKGROUND
  
      png_read_update_info(PngPointer, InfoPointer);
      channels = png_get_channels(PngPointer, InfoPointer);
      png_get_IHDR(PngPointer, InfoPointer, &amp;width, &amp;height, &amp;bitdepth, &amp;colortype, &amp;interlacetype, nullptr, nullptr);
  </code></pre>
  
  <p>My settings for saving are:</p>
  
  <pre><code>png_set_write_fn(PngPointer, reinterpret_cast&lt;void*&gt;(&amp;hFile), WriteToStream, nullptr);
      png_set_IHDR (PngPointer, InfoPointer, width, height, bitdepth, BitsPerPixel == 24 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
      png_write_info(PngPointer, InfoPointer);
  
      png_set_bgr(PngPointer);
      png_set_packing(PngPointer);
      png_set_interlace_handling(PngPointer);
      if (colortype == PNG_COLOR_TYPE_RGB) png_set_filler(PngPointer, 0xFF, PNG_FILLER_AFTER);
  </code></pre>
  
  <p>The PNG looks like:
  <img src="https://i.stack.imgur.com/ZV39R.png" alt="enter image description here"></p>
  
  <p>When I save it as a bitmap via C++, it looks like (8 bit one does this): <img src="https://i.stack.imgur.com/tzsMl.png" alt="enter image description here"></p>
  
  <p>Saving it back as a PNG, it looks exactly like the original. Both the 8 and 16 save back fine.</p>
  
  <p>The thing is, if I Uncomment that "SetBackground" Section, it works perfectly saving as bitmap EXCEPT that when I save it back as PNG, the background is no longer transparent due to the code setting it to white (255, 255, 255).</p>
  
  <p>How can I fix this?</p>