HACKING 120% {Hacking, programmazione, computer & molto altro}

[Java][Source] PPM Image Generator

« Older   Newer »
  Share  
RootkitNeo
view post Posted on 1/3/2014, 15:38     +1   -1




Avevo scritto questo source tempo fa per generare immagini PPM, lo condiviso anche con voi.

CODICE
import java.io.*;

/*
 * Description: PPM image generator
 * Author     : RootkitNeo
 *
*/

class ImagePpm {
  private byte data[];
  private int width,height;
   
  ImagePpm(int w, int h) {
    width = w;
    height = h;
    data = new byte[3+(width*height*3)];
  }
   
  void setPixel(int x,int y, int r, int g, int b) {
    if(x >=0 && y>=0 && x<=width && y<=height) {
      data[3*(x+y*width)  ] = (byte)r;
      data[3*(x+y*width)+1] = (byte)g;
      data[3*(x+y*width)+2] = (byte)b;
    }
  }
   
  void saveImage(String name) {
    try {
      FileOutputStream fos = new FileOutputStream(name+".ppm");
     
      String header="P6\n"+"#Immagine generata da ImagePpm\n"+width+" "+height+"\n255\n";
     
      try {
        fos.write(header.getBytes("US-ASCII"));
        fos.write(data);
      } finally {
        fos.close();
      }
    } catch(IOException ex) { ex.printStackTrace();}
  }
}
 
Top
0 replies since 1/3/2014, 15:38   46 views
  Share