import java.io.*;
import java.awt.*;
import java.awt.image.*;

// affiche une image PPM en couleur

class DisplayImage extends Canvas {
  
  Image img;

  public DisplayImage(Image img) {
    this.img = img;
    setSize(img.getWidth(this), img.getHeight(this));
  }

  public void paint(Graphics gr) {
    gr.drawImage(img, 0, 0, this);
  }

}

public class DisplayRGBPixmapAWT extends Frame {
  
  // constructeur pour un argument Pixmap
  public DisplayRGBPixmapAWT(String name, ByteRGBPixmap rgb) {
    super(name);
    setLocation(50, 50);
    // fabrication des pixels couleur au format usuel AWT : ColorModel.RGBdefault
    int[] pixels = new int[rgb.size];
    for (int i = 0; i < pixels.length; i++)
      pixels[i] = 0xFF000000 + (Pixmap.intValue(rgb.r.data[i]) << 16) | (Pixmap.intValue(rgb.g.data[i]) << 8) | Pixmap.intValue(rgb.b.data[i]);
    // construit une image avec ces pixels
    MemoryImageSource source = new MemoryImageSource(rgb.width, rgb.height, pixels, 0, rgb.width);
    Image img = Toolkit.getDefaultToolkit().createImage(source);
    add(new DisplayImage(img));
    pack();
    show();
  }

  // constructeur pour un argument fichier PPM
  public DisplayRGBPixmapAWT(String filename) throws IOException {
    this(filename, new ByteRGBPixmap(filename));
  }

  public static void main(String[] args) {
    try {
      new DisplayRGBPixmapAWT(args[0]);
    }
    catch (IOException e) {System.err.println(e);}
  }

}

