summaryrefslogtreecommitdiff
path: root/src/bjc/imgchain/pipeline/stages/AbstractPixelStage.java
blob: b93f09237c991372f45b6387cd3825f75b78f337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package bjc.imgchain.pipeline.stages;

import java.awt.Image;
import java.awt.image.BufferedImage;

import bjc.imgchain.pipeline.StageType;
import bjc.imgchain.utils.Utils;

/**
 * An abstract stage that processes images pixel-by-pixel.
 * 
 * @author bjculkin
 *
 */
public abstract class AbstractPixelStage extends AbstractPipelineStage {

	/**
	 * Create a new abstract pixel stage.
	 * 
	 * @param type
	 *             The type of this stage.
	 */
	protected AbstractPixelStage(StageType type) {
		super(type);
	}

	@Override
	public Image process(Image inp) {
		BufferedImage buf = (BufferedImage) inp;

		BufferedImage res = new BufferedImage(buf.getWidth(), buf.getHeight(),
				BufferedImage.TYPE_INT_ARGB);

		for (int y = 0; y < buf.getHeight(); y++) {
			for (int x = 0; x < buf.getWidth(); x++) {
				int[] pix = Utils.toARGBQuad(buf.getRGB(x, y));

				int[] processedPixel = processPixel(pix);

				res.setRGB(x, y, Utils.fromARGBQuad(processedPixel));
			}
		}

		return res;
	}

	/**
	 * Process a pixel of data.
	 * 
	 * @param pix
	 *            The pixel, as an array in the form (A, R, G, B)
	 * 
	 * @return The new pixel, as an array in the form (A, R, G, B)
	 */
	public abstract int[] processPixel(int[] pix);
}