Page 1 of 1

How do I force Image type in write?

Posted: 2015-02-05T13:48:10-07:00
by CBueche
I'm unable to find any useful, current information on forcing the Image type when I save a file, via write("filename"). My output file is being color-optimized, but I don't want it to be.

Here's a short example:

Code: Select all

  Image *pImage = new Image("d:\\temp\\Tile.png");
  Pixels *pCache = new Pixels(*pImage);
  PixelPacket *pPixels = pCache->get(0, 0, TILE_SIZE, TILE_SIZE);
  pImage->modifyImage();
  
  PixelPacket *pDst = pPixels;
  for(int i=0; i<TILE_SIZE * TILE_SIZE; i++) {
    pDst->red   = QuantumRange;
    pDst->green = 0;
    pDst->blue  = 0;
    pDst++;
  }
  
  pCache->sync();
  pImage->write("d:\\temp\\Test1.png");

  SAFE_DELETE(pCache);
  SAFE_DELETE(pImage);
Tile.png is a 16-bit-per-channel RGB file. Unfortunately, Test1.png ends up being 8-bit indexed color. I assume this is due to automatic optimization, since if I only modify one channel (say, red), the image has enough colors remaining that Test1.png is saved as 16-bit-per-channel RGB.

I need Test1.png to always have the exact same format as Tile.png.

I've tried adding pImage->type(TrueColorType) and pImage->depth(16), but they have no apparent effect. The library is compiled for 16-bit channels, x64, Visual Studio 2012.

Any thoughts or comments welcome.

Re: How do I force Image type in write?

Posted: 2015-02-05T16:23:37-07:00
by CBueche
After stepping through the source code (file png.c) in the debugger, I found a solution. A solution that works is to prepend "PNG48:" to the output file name. For example:

pImage->write("PNG48:d:\\temp\\Test1.png");

Using this method, I was able to dispense with creating the Image as a copy of a file and just use Image(Geometry, Color). I imagine it would be wise to designate the Image type and depth, so that data isn't lost as the Image pixels are populated.

An additional filename prefix, "PNG00:", was mentioned, which should save the Image in the same format as the original file, but I haven't checked out the ins and outs of that option.