* Example Code *

Simple Example
import com.sixlegs.image.png.*;
import java.io.IOException;
import java.awt.*;

public class SimpleExample {
  public static void main (String[] args) {
    try {
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      Image img = Toolkit.getDefaultToolkit().createImage(png);
      // Do something with image here
    } catch (IOException e) { }
  }
}

Properties Example
import com.sixlegs.image.png.*;
import java.util.Enumeration;
import java.io.IOException;

public class PropertiesExample {
  public static void main (String[] args) {
    try {
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      // Ensures that entire PNG image has been read
      png.getEverything();
      // Print all available properties
      for (Enumeration e = png.getProperties(); e.hasMoreElements();) {
        String key = (String)e.nextElement();
        System.out.println(key + '\t' + png.getProperty(key));
      }
    } catch (IOException e) { }
  }
}

Error Example
import com.sixlegs.image.png.*;
import java.util.Enumeration;
import java.io.IOException;

public class ErrorExample {
  public static void main (String[] args) {
    try {
      // Makes errors in ancillary chunks fatal
      PngImage.setAllErrorsFatal(true);
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      // Read entire PNG image (doesn't throw exceptions)
      png.getEverything();
      // Print all errors
      if (png.hasErrors()) {
        System.err.println("Errors in PNG processing:");
        for (Enumeration e = png.getErrors(); e.hasMoreElements();) {
          // Objects returned by getErrors derive from IOException,
          // but you usually only want to print them
          System.err.println("  " + e.nextElement());
        }
      }
    } catch (IOException e) { }
  }
}

Chunk Handler Example
import com.sixlegs.image.png.*;
import java.io.IOException;

public class ChunkHandlerExample implements ChunkHandler {

  public static void main (String[] args) {
    try {
      // Register instance of this class as "heLo" handler
      PngImage.registerChunk(new ChunkHandlerExample(), "heLo");
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      // Ensures that entire PNG image has been read,
      // triggering handleChunk when a "heLo" chunk is found
      png.getEverything();
    } catch (IOException e) { }
  }

  public void handleChunk (String type, byte[] data) {
    if (type.equals("heLo")) {
      // do something with data here
    }
  }
}

Grayscale Example
import com.sixlegs.image.png.*;
import java.awt.image.*;
import java.awt.*;
import java.io.IOException;

public class GrayscaleExample {
  public static void main (String[] args) {
    try {
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      // Get chromaticity property if available
      double[][] chrom = (double[][])png.getProperty("chromaticity xyz");
      // Use luminance info from chromaticity array, or default
      ImageFilter gray = (chrom == null ?
                          new GrayscaleFilter() : 
                          new GrayscaleFilter(chrom[1][1], 
                                              chrom[2][1], 
                                              chrom[3][1]));
      // Chain PngImage to be filtered through GrayscaleFilter
      FilteredImageSource gray_src = new FilteredImageSource(png, gray);
      Image img = Toolkit.getDefaultToolkit().createImage(gray_src);
      // Do something with image here
    } catch (IOException e) { }
  }
}

Image Observer Example
import com.sixlegs.image.png.*;
import java.awt.image.*;
import java.awt.*;
import java.io.IOException;

public class ObserverExample implements ImageObserver {

  public static void main (String[] args) {
    try {
      // Update ImageObservers after each interlace pass
      PngImage.setProgressiveDisplay(true);
      // Read PNG image from file
      PngImage png = new PngImage(args[0]);
      Toolkit tk = Toolkit.getDefaultToolkit();
      Image img = tk.createImage(png);
      int w = png.getWidth();
      int h = png.getHeight();
      // Call prepareImage using an instance of this class
      // as the ImageObserver; triggers imageUpdate (below)
      tk.prepareImage(img, w, h, new ObserverExample());
    } catch (IOException e) { }
  }

  public boolean imageUpdate (Image img, 
                              int infoflags, 
                              int x, int y, int w, int h) {
    if ((infoflags & ALLBITS) != 0 || 
        (infoflags & FRAMEBITS) != 0) {
      // We have a frame, do something with image here
    }
    return true;
  }
}

Applet Example
import com.sixlegs.image.png.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;

public final class PngApplet extends Applet {
  private Color imgbg;
  private Image img;
  private PngImage png;
  private int width;
  private int height;

  public PngApplet () { }

  public void init () {
    String loc = getParameter("URL");
    try {
      if (loc != null) {
        URL url = new URL(getCodeBase(), loc);
        // Read PNG image from URL
        png = new PngImage(url);
        width  = png.getWidth();
        height = png.getHeight();
        // Use bgcolor applet param, or bKGD chunk, or white
        String bgparam = getParameter("bgcolor");
        if (bgparam != null)
          imgbg = new Color(Integer.valueOf(bgparam, 16).intValue());
        if (imgbg == null)
          imgbg = png.getBackgroundColor();
        if (imgbg == null)
          imgbg = Color.white;
        // Composite image against background color
        RemoveAlphaFilter remover = new RemoveAlphaFilter(imgbg);
        ImageProducer src = new FilteredImageSource(png, remover);
        img = Toolkit.getDefaultToolkit().createImage(src);
      }
    } catch (Exception e) { System.err.println(e); }
  }

  // Override update to not clear screen before repaint
  public void update (Graphics g) {
    paint(g);
  }

  public void paint (Graphics g) {
    try {
      if (img != null) {
        g.drawImage(img, 0, 0, width, height, imgbg, null);
      }
    } catch (Exception e) { }
  }
}

Applet HTML Example
<title>PNG AlphaSnakes</title>
<body bgcolor="0066CC">
<center>
<applet archive="png.jar" code="PngApplet.class" width=504 height=557>
<param name="URL" value="AlphaSnakes8.png">
<param name="bgcolor" value="0066CC">
</applet>
</center>