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

// affiche une image PPM en couleur
public class DisplayRGBPixmap {
  
  public static void main(String[] args) {
    ByteRGBPixmap rgb;
    try {
      rgb = new ByteRGBPixmap(args[0]);     // lecture du fichier
    } catch (IOException e) { System.out.println(e); return; }

    GrafPort frame = new GrafPort(args[0], rgb.width, rgb.height, rgb.width, rgb.height);
    // fabrication des pixels 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);
    source.setAnimated(true); // si on veut modifier les pixels par la suite
    Image img = frame.createImage(source);
    
    frame.drawImage(img, 0, 0);
    
    frame.getClick(); // attente

    // recommence en permutant les plans
    for (int i = 0; i < pixels.length; i++)
      pixels[i] = 0xFF000000 + (Pixmap.intValue(rgb.g.data[i]) << 16) | (Pixmap.intValue(rgb.b.data[i]) << 8) | Pixmap.intValue(rgb.r.data[i]);
    source.newPixels(0, 0, rgb.width, rgb.height);   // signale la zone modifiée
    frame.drawImage(img, 0, 0);
  }

}

