import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class Img extends JFrame{ private Image img = null; private int width, height; private int rgbarray[][]; private int imgarray[]; public void ShowImage() { JFrame frame = new JFrame("Podglad obrazka"); JLabel label = new JLabel(new ImageIcon(img)); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public Img(String filename) { LoadImage(filename); AllocateArrays(); FillRGBArray(); } public Img(int imgwidth, int imgheight) { setWidth(imgwidth); setHeight(imgheight); AllocateArrays(); FillRGBArray(); } public Image getImage() { return img; } public int getWidth() { return width; } public int getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public void setWidth(int newWidth) { width = newWidth; } public void LoadImage(String filename) { ImageIcon icon = new ImageIcon(filename); img = icon.getImage(); width = img.getWidth(null); height = img.getHeight(null); } public void AllocateArrays() { imgarray = new int [width * height]; rgbarray = new int[width * height][3]; } public void FillRGBArray() { if (img == null) Build(); PixelGrabber pg = new PixelGrabber (img, 0, 0, width, height, imgarray, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color c = GetPixelColor(x, y); rgbarray[y * width + x][0] = (int)c.getRed(); rgbarray[y * width + x][1] = (int)c.getGreen(); rgbarray[y * width + x][2] = (int)c.getBlue(); } } } public void Build() { img = createImage (new MemoryImageSource(width, height, imgarray, 0, width)); } public Color GetPixelColor (int x, int y) { int pixel = imgarray [y * width + x]; int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; return (new Color (red, green, blue, alpha)); } public void SetPixelColor(int x, int y, Color color) { imgarray[y * width + x] = ((int)color.getAlpha() << 24) | ((int)color.getRed() << 16) | ((int)color.getGreen() << 8) | (int)color.getBlue(); rgbarray[y * width + x][0] = (int)color.getRed(); rgbarray[y * width + x][1] = (int)color.getGreen(); rgbarray[y * width + x][2] = (int)color.getBlue(); } }