Package ar.com.hjg.pngj

PNGJ main package

See: Description

Package ar.com.hjg.pngj Description

PNGJ main package

Users of this library should rarely need more than the public members of this package.
Newcomers: start with PngReader and PngWriter.

Example of use: this code reads a true colour PNG image (RGB8 or RGBA8) and reduces the red channel by half, increasing the green by 20. It copies all the "safe" metadata from the original image, and adds a textual metadata.

   public static void convert(String origFilename, String destFilename) {
      PngReader pngr = new PngReader(new File(origFilename));
      System.out.println(pngr.toString());
      int channels = pngr.imgInfo.channels;
      if (channels < 3 || pngr.imgInfo.bitDepth != 8)
         throw new RuntimeException("This method is for RGB8/RGBA8 images");
      PngWriter pngw = new PngWriter(new File(destFilename), pngr.imgInfo, true);
      pngw.copyChunksFrom(pngr.getChunksList(), ChunkCopyBehaviour.COPY_ALL_SAFE);
      pngw.getMetadata().setText(PngChunkTextVar.KEY_Description, "Decreased red and increased green");
      for (int row = 0; row < pngr.imgInfo.rows; row++) { // also: while(pngr.hasMoreRows()) 
         IImageLine l1 = pngr.readRow();
         int[] scanline = ((ImageLineInt) l1).getScanline(); // to save typing
         for (int j = 0; j < pngr.imgInfo.cols; j++) {
            scanline[j * channels] /= 2;
            scanline[j * channels + 1] = ImageLineHelper.clampTo_0_255(scanline[j * channels + 1] + 20);
         }
         pngw.writeRow(l1);
      }
      pngr.end(); // it's recommended to end the reader first, in case there are trailing chunks to read
      pngw.end();
   }

For more examples, see the tests and samples.

Copyright © 2013. All rights reserved.