Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ij.fach.FilterValue;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.awt.image.IndexColorModel;
- import java.awt.image.WritableRaster;
- import java.util.ArrayList;
- /**
- * @author Florian Thiel
- *
- * Superklasse die die Operation z.b. Zugriff auf die Pixel �bernimmt.
- * Die Subklassen sind jeweils eine Spezialisierung dieser Klasse.
- * */
- public abstract class ImageProcessing {
- protected int[] pixelOrg;
- protected int[] pixelNew;
- // Breite und H�he vom Bild
- protected int height;
- protected int width;
- protected int type;
- protected int maxValue;
- // nimmt die Parameter f�r den ausgew�hlten Algorithmus entgegen
- protected ArrayList<FilterValue> para;
- /**
- * Dies sind drei Korrekturfaktoren die bei der Konvertierung ben�tigt
- * werden, da das menschliche Auge gr�ne Farbanteile intensiver wahrnimmt
- * als rote und rote Farbanteile st�rker als blaue.
- */
- protected float r = 0.229f, g = 0.587f, b = 0.114f;
- public ImageProcessing(BufferedImage input, ArrayList<FilterValue> para) {
- width = input.getWidth();
- height = input.getHeight();
- type = input.getType();
- /**
- * Pr�fe ob das IndexColorModel verwendet wird( z.B. gif-Bilder), dann
- * muss der Farb/Grauwert aus einer Tabelle ermittelt werden.
- * */
- if (input.getColorModel() instanceof IndexColorModel) {
- IndexColorModel model = (IndexColorModel) input.getColorModel();
- pixelOrg = new int[input.getWidth() * input.getHeight()];
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int key = input.getRaster().getSample(x, y, 0);
- pixelOrg[y * width + x] = new Color(model.getRed(key),
- model.getGreen(key), model.getBlue(key),
- model.getAlpha(key)).getRGB();
- }
- }
- type = BufferedImage.TYPE_INT_RGB;
- } else {
- /**
- * Bei der Verwendung des DirektColorModels, kann der Farbwert
- * direkt ausgelesen werden.
- * */
- if (type == BufferedImage.TYPE_INT_RGB) {
- pixelOrg = input.getRGB(0, 0, input.getWidth(),
- input.getHeight(), null, 0, input.getWidth());
- } else {
- pixelOrg = new int[input.getWidth() * input.getHeight()];
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- pixelOrg[y * width + x] = input.getRaster().getSample(
- x, y, 0);
- }
- }
- }
- }
- maxValue = 255;
- pixelNew = new int[pixelOrg.length];
- System.arraycopy(pixelOrg, 0, pixelNew, 0, pixelOrg.length);
- this.para = para;
- }
- public ImageProcessing(int width, int height, int type, int maxValue,
- int[] input) {
- this.width = width;
- this.height = height;
- this.type = type;
- this.maxValue = maxValue;
- pixelOrg = input;
- pixelNew = new int[input.length];
- System.arraycopy(pixelOrg, 0, pixelNew, 0, pixelOrg.length);
- }
- // Diese Methode f�hrt die eigentliche Bildberechnung durch.
- public abstract void processing();
- protected void convertToGray() {
- if (type != BufferedImage.TYPE_BYTE_GRAY) {
- type = BufferedImage.TYPE_BYTE_GRAY;
- for (int i = 0; i < pixelOrg.length; i++) {
- Color c = new Color(pixelOrg[i]);
- pixelOrg[i] = (int) (r * c.getRed() + g * c.getGreen() + b
- * c.getBlue());
- c = new Color(pixelNew[i]);
- pixelNew[i] = (int) (r * c.getRed() + g * c.getGreen() + b
- * c.getBlue());
- }
- }
- }
- /**
- * Konvertiert das
- * */
- protected void convertToBin(int threshold) {
- if (type == BufferedImage.TYPE_BYTE_GRAY) {
- for (int i = 0; i < pixelOrg.length; i++) {
- if (threshold > pixelOrg[i])
- pixelOrg[i] = 0;
- else
- pixelOrg[i] = maxValue;
- }
- } else {
- for (int i = 0; i < pixelOrg.length; i++) {
- Color c = new Color(pixelOrg[i]);
- if (threshold > (r * c.getRed() + g * c.getGreen() + b
- * c.getBlue()))
- pixelOrg[i] = 0;
- else
- pixelOrg[i] = maxValue;
- }
- }
- type = BufferedImage.TYPE_BYTE_BINARY;
- }
- protected void convertToColor() {
- if (type != BufferedImage.TYPE_INT_RGB) {
- for (int i = 0; i < pixelNew.length; i++) {
- int tmp = pixelOrg[i];
- pixelOrg[i] = new Color(tmp, tmp, tmp).getRGB();
- tmp = pixelNew[i];
- pixelNew[i] = new Color(tmp, tmp, tmp).getRGB();
- }
- type = BufferedImage.TYPE_INT_RGB;
- }
- }
- protected Object getParameter(String s) {
- if (para != null)
- for (FilterValue f : para) {
- if (f.getName().equals(s))
- return f.getValue();
- }
- return null;
- }
- /**
- * Erstellt ein neues BufferedImage und f�llt es mit den Array pixelNew
- * */
- public BufferedImage getProcessedImage() {
- BufferedImage output = new BufferedImage(width, height, type);
- if (type == BufferedImage.TYPE_BYTE_BINARY
- || type == BufferedImage.TYPE_BYTE_GRAY) {
- WritableRaster raster = output.getRaster();
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- raster.setPixel(x, y, new int[] { pixelNew[y * width + x] });
- }
- }
- output.setData(raster);
- } else {
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- output.setRGB(x, y, pixelNew[y * width + x]);
- }
- }
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement