Last Updated: February 25, 2016
·
720
· phaus

x2pgm

import java.util.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;

/*
 x2pgm - converts various file formats to pgm
 Copyright (C) 2006 Philipp Haussleiter

 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General 
 Public License as published by the Free Software Foundation; either version 2 of the License, or (at your 
 option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 
 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write to 
 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
 */

class Color2GrayFilter extends RGBImageFilter {

 int red,green,blue;
 int A=100;int B=100;int C=100;

 public Color2GrayFilter () {
 canFilterIndexColorModel = true; 
 }
 public int filterRGB (int x, int y, int rgb) {
 red = (rgb & 0xff0000) >>16;
 green = (rgb & 0xff00) >>8;
 blue = (rgb & 0xff);
 red =red*A/100;green = green*B/100; blue = blue*C/100;
 return (0xff000000 | red <<16| green << 8| blue); 
 } 
} 

public class x2pgm extends Frame{

 /**
 * @param args
 */
 Color2GrayFilter filter;
 String[] name, size;
 Image SrcImage, gray;
 int w, h, pw, ph;
 PixelGrabber grab;
 boolean debug = false;
 boolean header = true;

 public static void main(String[] args) {
 x2pgm Loader = new x2pgm(args);
 }

 public void writeString(String wort, OutputStream out){
 try{
 for(int i=0; i< wort.length(); i++)
 out.write((int)wort.charAt(i));
 out.write((int)'\n');
 }catch(Exception e){
 System.out.println("Cannot Write to File");
 }
 }
 public x2pgm(String[] args){
 // TODO Auto-generated method stub
 System.out.println("x2pgm v 0.0.0.1");
 System.out.println("==================================================================");
 if(args.length < 2){
 System.out.println("Usage:");
 System.out.println("x2pgm <inputfile [.png|.gif|.bmp|.jpeg]> <output_file> [debug] [noheader]");

 }else{
 filter = new Color2GrayFilter ();
 name = args[0].split("\\.");
 String ext = "pgm";
 //size = args[2].split("x");

 int i = 0;
 try{
 if(args[2].equals("debug") || args[3].equals("debug")) debug = true;
 if(args[3].equals("noheader") || args[2].equals("noheader")) header = false;
 }catch(Exception e){}

 try{
 File IN;

 IN = new File( args[0] );
 SrcImage = ImageIO.read( IN );
 /*
 w = Integer.parseInt(size[0]);
 h = Integer.parseInt(size[1]);
 */
 ph = SrcImage.getHeight(this);
 pw = SrcImage.getWidth(this);

 gray = createImage (new FilteredImageSource (SrcImage.getSource(), filter));
 /*
 System.out.println("->Schreibe Bild als: "+size[0]+"/"+pw+"px ("+((float)w/(float)pw*100)+"%) x "+size[1]+"/"+ph+"px ("+((float)h/(float)ph*100)+"%)");
 */
 final int[] pixels = new int[pw * ph];
 grab = new PixelGrabber( SrcImage, 0, 0, pw, ph, pixels, 0, pw );
 try{
 grab.grabPixels();
 }catch ( InterruptedException e ) {
 System.err.println( "Error getting pixels" );
 }
 if(header == true)
 ext = "pgm";
 else
 ext = "raw";
 OutputStream out = new FileOutputStream(args[1]+"."+ext) ;
 if(header == true){
 writeString("P5",out);
 writeString(pw+" "+ph,out);
 writeString("255",out);
 }
 for(i = 0; i < pixels.length; i++){
 if(debug && i%pw == 0)
 System.out.print("#");
 out.write(pixels[i]);
 }
 out.flush();
 out.close();

 }
 catch(IOException e){
 System.out.println("Fehler: "+e.toString());
 }
 System.out.println("");
 System.out.println("->"+i+" Zeilen geschrieben !");
 }
 System.out.println("==================================================================");
 System.out.println("(c) Philipp Haussleiter 2006");
 System.out.println("Skalierung geht noch nicht :-)");
 System.out.println("==================================================================");
 }
}