import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.jai.*;
public class TiffReader {
private int _width, _height;
private int[] _buffer;
private void loadBuffer(String sourcepath) {
RenderedImage ri = JAI.create("fileload", sourcepath);
java.awt.image.Raster raster = ri.getData();
int miny = raster.getMinY();
int maxy = miny + raster.getHeight();
int minx = raster.getMinX();
int maxx = minx + raster.getWidth();
_width = maxx-minx;
_height = maxy - miny;
int[] p = new int[1];
_buffer = new int[_width * _height];
for (int x = minx; x < maxx; x++) {
for (int y = miny; y < maxy; y++) {
p = raster.getPixel(x, y, p);
int pos = (x - minx) * _height + y - miny;
_buffer[pos] = p[0];
}
}
}
private RenderedImage createImage() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage bi = gc.createCompatibleImage(_width, _height, Transparency.OPAQUE);
Graphics2D g2d = bi.createGraphics();
Color[] colors = new Color[256];
for (int c=0; c<256; c++)
colors[c] = new Color(c, c, c);
for (int x = 0; x < _width; x++)
for (int y = 0; y < _height; y++) {
g2d.setColor(colors[_buffer[x * _height + y]]);
g2d.drawLine(x,y, x,y);
}
g2d.dispose();
return bi;
}
private void saveImage(RenderedImage image, String destinationpath) {
try {
File f = new File(destinationpath);
ImageIO.write(image, "jpg", f);
} catch (IOException e) {
}
}
public void Transform(String sourcepath, String destinationpath) {
loadBuffer(sourcepath);
saveImage(createImage(), destinationpath);
}
public static void main(String[] args) {
TiffReader tr = new TiffReader();
switch (args.length) {
case 0:
System.exit(-1);
break;
case 1:
tr.Transform(args[0], args[0]+".jpg");
break;
default:
tr.Transform(args[0], args[1]);
}
System.out.println("Done!");
}
}