Mar 06

Often when doing image processing we need to change the color of the image pixels. This ultimately involves iterating through all the pixels of an image changing values as required. This can be a slow process, especially for large images.

When you want to change all pixels of one colour to another then you can avoid iterating over the entire image by instead changing the ColorModel. The ColorModel specifies the mapping between pixel values and their color. Changing this mapping allows us to change the color of pixels without manipulating the actual pixel value.

The following gives a simple example to demonstrate the concept by changing the ColorModel of the displayed images as the mouse is moved over it.

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import java.awt.image.WritableRaster;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageExample extends JLabel
	implements MouseMotionListener {

	private BufferedImage image = createImage();
	
	public ImageExample() {
		setIcon(new ImageIcon(image));
		
		addMouseMotionListener(this);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		
		// here we want to change the color model
		// to do that we create a new image using a new color model
		// nb. there is no need to change the source pixels
		
		image = new BufferedImage(createColorModel(e.getX()), 
				image.getRaster(), false, null);
		setIcon(new ImageIcon(image));
	}

	@Override
	public void mouseDragged(MouseEvent e) {
	}

	private static BufferedImage createImage() {

		int width = 200;
		int height = 200;
		
		// Generate the source pixels for our image
		// Lets just keep it to a simple blank image for now
		
		byte[] pixels = new byte[width * height];
		DataBuffer dataBuffer = new DataBufferByte(pixels, width*height, 0);
		SampleModel sampleModel = new SinglePixelPackedSampleModel(
        	DataBuffer.TYPE_BYTE, width, height, new int[] {(byte)0xf});
		WritableRaster raster = Raster.createWritableRaster(
			sampleModel, dataBuffer, null);

		return new BufferedImage(createColorModel(0), raster, false, null);
	}

	private static ColorModel createColorModel(int n) {

		// Create a simple color model with all values mapping to
		// a single shade of gray
		// nb. this could be improved by reusing the byte arrays
		
		byte[] r = new byte[16];
		byte[] g = new byte[16];
		byte[] b = new byte[16];

		for (int i = 0; i < r.length; i++) {
			r[i] = (byte) n;
			g[i] = (byte) n;
			b[i] = (byte) n;
		}
		return new IndexColorModel(4, 16, r, g, b);
	}

	public static void main(String[] args) {

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new ImageExample());
		frame.pack();
		frame.setVisible(true);
	}
}

written by objects \\ tags: , , , ,

Mar 04

Java provides a convenient Collections.sort() method to sort a List of Object’s. The Objects in the list are required to implement the Comparable interface and the compareTo() method is used to compare Objects.

When an alternate sort order is needed you can do that using a Comparator which is passed to the sort() call.

The following example shows how you could sort a list of strings where the string contained a number (that should be sorted numerically) and a string (to be sorted alphabetically).

List<String> list = Arrays.asList(
   new String[] { "34 abc", "123 dfd", "34 xyz", "12 xxx" });

Collections.sort(list, new Comparator<String>() {

	@Override
	public int compare(String s1, String s2) {
		
		// separate the number and string
		
		String[] tokens1 = s1.split(" ");
		String[] tokens2 = s2.split(" ");
		
		// compare the number from each item
		
		int compare = Integer.parseInt(tokens1[0]) - Integer.parseInt(tokens2[0]);
		
		// If number same compare the string
		
		return compare==0 ? tokens1[1].compareTo(tokens2[1]) : compare;
	}
});

written by objects \\ tags: , , ,

Mar 04

Java provides a convenient Arrays.sort() method to sort an array of Object’s. The Objects in the array are required to implement the Comparable interface and the compareTo() method is used to compare Objects.

When an alternate sort order is needed you can do that using a Comparator which is passed to the sort() call.

The following example shows how you could sort an array of strings where the string contained a number (that should be sorted numerically) and a string (to be sorted alphabetically).


String[] array = new String[] { "34 abc", "123 dfd", "34 xyz", "12 xxx" };

Arrays.sort(array, new Comparator<String>() {

	@Override
	public int compare(String s1, String s2) {
		
		// separate the number and string
		
		String[] tokens1 = s1.split(" ");
		String[] tokens2 = s2.split(" ");
		
		// compare the number from each item
		
		int compare = Integer.parseInt(tokens1[0]) - Integer.parseInt(tokens2[0]);
		
		// If number same compare the string
		
		return compare==0 ? tokens1[1].compareTo(tokens2[1]) : compare;
	}
});

written by objects \\ tags: , , ,