• NOW LIVE! Into the Woods--new character species, eerie monsters, and haunting villains to populate the woodlands of your D&D games.

Looking for a C/Java library that will read in Greyscale TIFFs and produce a matrix

Kalanyr

Explorer
I'm looking for a C or Java library that will let me read in a Greyscale TIFF and produce a matrix that has each Pixels Greyscale intensity. I need it for some image processing stuff I have to do.

(I know Matlab does this but its not currently an option for me)

If anyone could make some suggestions I'd be greatful. (Ah and I use windows so if its in C and the library uses concurrency it will die.)
 

log in or register to remove this ad

try using java advanced image api (jai) jai

i dont have any grayscale tiff images, so i cant verify this but try something like:

Code:
RenderedImage ri = JAI.createImage("loadfile", path_to_file);
Raster raster = ri.getData();
int[] p = raster.getPixel(0, 0, new int[1]); // get data at one point.

you'll have to create the matrix to hold the data yourself.

/f
 


libtiff.org had som sample images, so here is some code that works :D

Code:
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!");
	}
}

graphics have never been my strong side, so you could probably create the new image easier (using a colormap).

/f
 


Into the Woods

Remove ads

Top