All pastes #206717 Raw Edit

Miscellany

public text v1 · immutable
#206717 ·published 2006-10-17 15:10 UTC
rendered paste body
import ij.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;

/**
 * Graubildtest
 * 
 * Klasse zum Testen von BV-Algorithmen in Graubildern
 */
public class Graubild_Test implements PlugInFilter {

	/** Initialisierung in ImageJ */
	public int setup(String arg, ImagePlus imp) {
		if (arg.equals("about")) {
			showAbout();
			return DONE;
		}

		/* Zugelassen nur für 8-Bit Graubilder */
		return DOES_8G + NO_CHANGES;
	}

	/** About Message zu diesem Plug-In. */
	void showAbout() {
		IJ.showMessage("Graubildtest", "Testprogramm");
	}

	/**
	 * Ausführende Funktion
	 * 
	 * @param ip
	 *            Image Processor. Klasse in ImageJ, beinhaltet das Bild und
	 *            zugehörige Metadaten.
	 */
	public void run(ImageProcessor ip) {

		/* get width, height and the region of interest */
		int w = ip.getWidth();
		int h = ip.getHeight();
		Rectangle roi = ip.getRoi();

		/*
		 * create a new image with the same size and copy the pixels of the
		 * original image
		 */
		ImagePlus corrected = NewImage.createByteImage("Corrected image", w, h, 1, NewImage.FILL_BLACK);
		ImageProcessor cor_ip = corrected.getProcessor();
		cor_ip.copyBits(ip, 0, 0, Blitter.COPY);

		/* Pixel-Array des Eingabebildes */
		byte[] pixelsin = (byte[]) ip.getPixels();

		/* Pixelarray des neuen Bildes */
		byte[] pixelsout = (byte[]) cor_ip.getPixels();

		/* Bit Invertierung */
		//doInvert(w, h, pixelsout, roi);
		
		/* Reduce Colors */
		//doReduceColors(4, w, h, pixelsout, roi);
		
		/* Reduce Resolution */
		doReduceResolution(cor_ip, roi);

		corrected.show();
		corrected.updateAndDraw();
	}

	private int[] getColors(int maxColor, int colors) {
		if (colors <= 1)
			return new int[] { maxColor };

		int resColors[] = new int[colors];
		resColors[0] = 0;
		int step = maxColor / (colors - 1);
		for (int i = 0; i < (colors - 1); i++) {
			resColors[i + 1] = (i + 1) * step;
		}

		return resColors;
	}

	/* Aufgabe 3) */
	private void doReduceResolution(ImageProcessor ip, Rectangle roi) {

		/* Rows */
		for (int x = roi.x; x < (roi.x + roi.width); x += 2) {

			/* Cols */
			for (int y = roi.y; y < (roi.y + roi.height); y += 2) {

				/* Get 2*2 Pixels */
				int x1y1 = ip.get(x, y);
				int x2y1 = ip.get(x + 1, y);
				int x1y2 = ip.get(x, y + 1);
				int x2y2 = ip.get(x + 1, y + 1);

				/* Change to match */
				int median = (x1y1 + x2y1 + x1y2 + x2y2) / 4;

				/* Put 2*2 Pixels */
				ip.putPixel(x, y, median);
				ip.putPixel(x + 1, y, median);
				ip.putPixel(x, y + 1, median);
				ip.putPixel(x + 1, y + 1, median);
			}
		}
	}

	/* Aufgabe 2) */
	private void doReduceColors(int numColors, int srcWidth, int srcHeight, byte src[], Rectangle roi) {
		int y = -1;
		int x = 0;

		for (int i = 0; i < src.length; i++) {
			x = i % srcWidth;
			if (i % srcHeight == 0)
				y++;

			/* Part of Region-of-Interest */
			if (roi.contains(x, y)) {
				int val = (int) src[i];
				val = (val & 0xFF);

				if (val < 42)
					val = 0;
				else if (val < 127)
					val = 85;
				else if (val < 212)
					val = 170;
				else
					val = 255;

				src[i] = (byte) val;
			}
		}
	}

	/* Aufgabe 1) */
	private void doInvert(int srcWidth, int srcHeight, byte src[], Rectangle roi) {
		int y = -1;
		int x = 0;
		for (int i = 0; i < src.length; i++) {
			x = i % srcWidth;
			if (i % srcHeight == 0)
				y++;

			/* Part of Region-of-Interest */
			if (roi.contains(x, y))
				src[i] = (byte) ~(src[i]);
		}
	}
}