Ciao a tutti,
sto cercando di fare un programmino che tratta i pixel delle immagini per modificarle.
questo programma dovrebbe visualizzare un'immagine, la stessa immagine girata di 180 gradi e la stessa immagine in "bianco e nero".
viene visualizzato tutto tranne l'immagine in bianco e nero.
il database che contiene i dati sui pixel dell'immagine in bianco e nero è corretto, i colori sono giusti, ma non riesce a visualizzare l'immagine.. invece l'immagine della figura ruotata si vede..
ecco il codice completo:
codice:import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.image.ImageObserver; import java.awt.image.MemoryImageSource; import java.awt.image.PixelGrabber; public class MainClass extends Applet { Image i, j, k; public void init() { MediaTracker mt = new MediaTracker(this); // image file name i = getImage(getDocumentBase(), "img.gif"); mt.addImage(i, 0); try { mt.waitForAll(); int width = i.getWidth(this); int height = i.getHeight(this); int pixels[] = new int[width * height]; PixelGrabber pg = new PixelGrabber(i, 0, 0, width, height, pixels, 0, width); if (pg.grabPixels() && ((pg.getStatus() & ImageObserver.ALLBITS) != 0)) { j = createImage(new MemoryImageSource(width, height, rowFlipPixels(pixels, width, height), 0, width)); k = createImage(new MemoryImageSource(width, height, blackAndWhite(pixels, width, height), 0, width)); } else System.out.println("invalid image : MainClass.init()"); } catch (InterruptedException e) { System.out.println("Eccezione Try InterrupterException"); } setSize(750,550); } public void paint(Graphics g) { g.drawImage(i, 10, 10, this); // regular if (j != null) g.drawImage(j, 300, 10, this); // rowFlip if (k != null) g.drawImage(k, 10, 300, this); // black and white else System.out.println("K = NULL"); } // turns the image 180° private int[] rowFlipPixels(int pixels[], int width, int height) { int newPixels[] = null; if ((width * height) == pixels.length) { newPixels = new int[width * height]; int newIndex = 0; for (int y = height - 1; y >= 0; y--) for (int x = width - 1; x >= 0; x--) newPixels[newIndex++] = pixels[y * width + x]; } return newPixels; } // makes the image black and white private int[] blackAndWhite(int pixels[], int width, int height) { int newPixels[] = null; if ((width * height) == pixels.length) { newPixels = new int[width * height]; // this is the only difference with the method above for (int i=0; i<pixels.length; i++) newPixels[i] = grigio(coloreArray(pixels[i])); } return newPixels; } // returns the color (int) in grey scale public static int grigio(int[] color) { int valore = (int) ((0.299*color[0]) + (0.587*color[1]) + (0.114*color[2])); return (valore & 0xff) + ((valore << 8) & 0xff00) + ((valore << 16) & 0xff0000); } // returns a new array with parameter's RGB value public static int[] coloreArray(int i) { int blu = (i & 0xff); int green = ((i & 0xff00) >> 8); int red = ((i & 0xff0000) >> 16); return new int[]{red, green, blu}; } }
se voleste provarlo vi basterebbe aggiungere alla cartella dove compilate il programma un file immagine chiamato "img.gif".
qualche idea sul perchè non funziona?
grazie in anticipo

Rispondi quotando