public int[] getBlock(int[] into, int x, int y, int dx, int dy, int offset) { return _i.getRGB(x, y, dx, dy, into, offset, 0); }
public static int[] decodeImage(BufferedImage img) { /* finds the hidden values in the text file */ int[] a_b, a, b; a = new int[8]; b = new int[8]; a_b = new int[2]; int i; for (i = 0; i < a.length; i++) { int p = img.getRGB(i, i); int[] bin = convertToBinary(p, 32); a[i] = bin[0]; } for (; i < (a.length + b.length); i++) { int p = img.getRGB(i, i); int[] bin = convertToBinary(p, 32); b[i - b.length] = bin[0]; } a_b[0] = convertToDigit(a, 8); a_b[1] = convertToDigit(b, 8); return a_b; }
static BufferedImage addMagnitudes(BufferedImage img1, BufferedImage img2) { BufferedImage dst = new BufferedImage(img1.getWidth(), img1.getHeight(), img1.getType()); for (int y = 0; y < img1.getHeight(); ++y) { for (int x = 0; x < img1.getWidth(); ++x) { int rgb1 = img1.getRGB(x, y); int rgb2 = img2.getRGB(x, y); int r1 = (rgb1 & 0x00FF0000) >> 16; int r2 = (rgb2 & 0x00FF0000) >> 16; int r = (int) (Math.sqrt(r1 * r1 + r2 * r2) / Math.sqrt(2.0)); if (r > 255) r = 255; if (r < 0) r = 0; int g1 = (rgb1 & 0x0000FF00) >> 8; int g2 = (rgb2 & 0x0000FF00) >> 8; int g = (int) (Math.sqrt(g1 * g1 + g2 * g2) / Math.sqrt(2.0)); if (g > 255) g = 255; if (g < 0) g = 0; int b1 = rgb1 & 0x000000FF; int b2 = rgb2 & 0x000000FF; int b = (int) (Math.sqrt(b1 * b1 + b2 * b2) / Math.sqrt(2.0)); if (b > 255) b = 255; if (b < 0) b = 0; int rgb = b + (g << 8) + (r << 16); dst.setRGB(x, y, rgb); } } return dst; }
public static int[] calculateVectorSpace(BufferedImage image) { clusters = createClusters(image); int[] vectorSpace = new int[IMAGE_WIDTH * IMAGE_HEIGHT]; Arrays.fill(vectorSpace, -1); boolean refineNeeded = true; int loops = 0; while (refineNeeded) { refineNeeded = false; loops++; for (int y = 0; y < IMAGE_HEIGHT; y++) { for (int x = 0; x < IMAGE_WIDTH; x++) { int pixel = image.getRGB(x, y); Cluster cluster = getMinCluster(pixel); if (vectorSpace[IMAGE_WIDTH * y + x] != cluster.getId()) { if (vectorSpace[IMAGE_WIDTH * y + x] != -1) { clusters[vectorSpace[IMAGE_WIDTH * y + x]].removePixel(pixel); } cluster.addPixel(pixel); refineNeeded = true; vectorSpace[IMAGE_WIDTH * y + x] = cluster.getId(); } } } } System.out.println("Took " + loops + " loops."); return vectorSpace; }
public static BufferedImage encodeImage(String filename, int[] primes) { /* encodes a and b in the image using a sequence. * We are assuming that the image would be big enough * to hold the 16 bits */ BufferedImage img, newimg = null; int[] a = convertToBinary(primes[0], 8); int[] b = convertToBinary(primes[1], 8); int[] a_b = copyBits(a, b); // copy all bits into one array try { img = ImageIO.read(new File(imagePath + filename)); for (int i = 0; i < a_b.length; i++) { int p = img.getRGB(i, i); int[] bin = convertToBinary(p, 32); bin[0] = a_b[i]; int d = convertToDigit(bin, 32); img.setRGB(i, i, d); } ImageIO.write(img, "png", new File(imagePath + "new_" + filename)); newimg = ImageIO.read(new File(imagePath + "new_" + filename)); } catch (IOException e) { System.out.println("ERROR WRITING IMAGE...\n" + e.toString()); System.exit(1); } return newimg; }
public Color getColorAt(int x, int y) { int col = img.getRGB(x, y); int r = col >> 16 & 0xff; int g = col >> 8 & 0xff; int b = col & 0xff; return new Color(r, g, b); }
private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.writeFloat(_goodness); out.writeInt(getWidth()); out.writeInt(getHeight()); int[] row = new int[getWidth()]; for (int y = 0; y < getHeight(); y++) { _i.getRGB(0, y, row.length, 1, row, 0, 0); out.writeObject(row); } }
public void drawTileNumC(int tileNum, int x, int y, Color c, Graphics g) { BufferedImage coloredTile = tiles[tileNum]; for (int i = 0; i < this.tW; i++) { for (int j = 0; j < this.tH; j++) { Color originalColor = new Color(coloredTile.getRGB(i, j), true); Color nc = new Color(c.getRed(), c.getGreen(), c.getBlue(), originalColor.getAlpha()); coloredTile.setRGB(i, j, nc.getRGB()); } } g.drawImage(tiles[tileNum], x, y, null); }
public Color getColorAt(double x, double y) { int col = img.getRGB((int) x, (int) y); double r = col >> 16 & 0xff; double g = col >> 8 & 0xff; double b = col & 0xff; int col_r = img.getRGB((int) x + 1, (int) y); double rr = col_r >> 16 & 0xff; double gr = col_r >> 8 & 0xff; double br = col_r & 0xff; double fact = x - Math.floor(x); double rf = r + (rr - r) * fact; double gf = g + (gr - g) * fact; double bf = b + (br - b) * fact; col = img.getRGB((int) x, (int) y + 1); r = col >> 16 & 0xff; g = col >> 8 & 0xff; b = col & 0xff; col_r = img.getRGB((int) x + 1, (int) y + 1); rr = col_r >> 16 & 0xff; gr = col_r >> 8 & 0xff; br = col_r & 0xff; double rf2 = r + (rr - r) * fact; double gf2 = g + (gr - g) * fact; double bf2 = b + (br - b) * fact; fact = y - Math.floor(y); double rff = rf + (rf2 - rf) * fact; double gff = gf + (gf2 - gf) * fact; double bff = bf + (bf2 - bf) * fact; return new Color((int) rff, (int) gff, (int) bff); }
// create N numbers of clusters public static Cluster[] createClusters(BufferedImage image) { Cluster[] result = new Cluster[N]; int x = 0; int y = 0; int dx = IMAGE_WIDTH / N; int dy = IMAGE_HEIGHT / N; for (int i = 0; i < N; i++) { result[i] = new Cluster(i, image.getRGB(x, y)); x += dx; y += dy; } return result; }
/* * Method takes a parameter of a BufferedImage * and returns a 2d array */ public static int[][] readIntoArray(BufferedImage x) { int width = x.getWidth(); int height = x.getHeight(); // initialize the 2d array to the size of the picture int[][] imagePixels = new int[width][height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { imagePixels[i][j] = x.getRGB(i, j); // store RGB value in array // printPixelARGB(x.getRGB(i,j)); } } return imagePixels; }
public static void main(String[] args) throws Exception { Robot robot = new Robot(args[0]); BufferedImage oimg = robot.cam.capture(true); // BufferedImage img = maslab.camera.ImageUtil.convertImage(oimg, BufferedImage.TYPE_INT_RGB); BufferedImage img = new BufferedImage(oimg.getWidth(), oimg.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < oimg.getHeight(); ++y) { for (int x = 0; x < oimg.getWidth(); ++x) { img.setRGB(x, y, oimg.getRGB(x, y)); } } BufferedImage blurred = gaussianBlur(img); ImageIO.write(blurred, "bmp", new File("gaussian.bmp")); BufferedImage xe = sobelX(blurred); ImageIO.write(xe, "bmp", new File("sobelx.bmp")); BufferedImage ye = sobelY(blurred); ImageIO.write(ye, "bmp", new File("sobely.bmp")); BufferedImage edges = addMagnitudes(xe, ye); BufferedImage dst = edges; ImageIO.write(dst, "bmp", new File("edges.bmp")); }
public void write() { int navg = 8; // int nshift=3; bookend = 8; numPgs = pdffile.getNumPages(); files = new File[numPgs]; int[] pixelsi = null; long[] sum = null; long[][] hist = null; BufferedImage bimage = null; // BufferedImage simage = null; // float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f, // 0.0625f, 0.125f, 0.0625f }; // Kernel kernel = new Kernel(3, 3, data); // ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); done(0); for (int i = 0; i < numPgs; i++) { if (i > MAXPAGE) break; PDFPage page = getPage(i); if (i == 0) { w = (int) page.getBBox().getWidth(); h = (int) page.getBBox().getHeight(); rect = new Rectangle(0, 0, w, h); // w /= 2; h /=2; bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); // simage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); pixelsi = new int[h * w]; sum = new long[h * w]; hist = new long[navg][h * w]; for (int j = 0; j < h * w; j++) { sum[j] = 0; } page1.countDown(); } // generate page image Image image = pdffile.getPage(i).getImage(w, h, rect, null, true, true); // force complete loading image = new ImageIcon(image).getImage(); // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); // extract pixels into array bimage.getRGB(0, 0, w, h, pixelsi, 0, w); // Accumulate rolling averages int im = i % navg; int ii = i - navg / 2; // middle of averaging range for (int j = 0; j < h * w; j++) { int p = pixelsi[j], q = 0; // Expand packed 8x3 pixel to 16x3 long r = 0, r2 = 0; r |= (p & 0xff); r <<= 16; p >>= 8; r |= (p & 0xff); r <<= 16; p >>= 8; r |= (p & 0xff); r = ~r; sum[j] += r; // rolling sum if (i >= navg) { // we have enough to average sum[j] -= hist[im][j]; // roll off the old hist[im][j] = r; r2 = (3 * sum[j] / navg + r) / 4; r = ~r2; } else { hist[im][j] = r; } // Repack averaged pixel q |= (r & 0xff); q <<= 8; r >>= 16; q |= (r & 0xff); q <<= 8; r >>= 16; q |= (r & 0xff); // Average over number of images frames with a non-background pixel in this location. // Note that we sum in complement space, so background is zero. // if(i>=navg) // nfg[j] -= fghist[im][j]; // nfg[j] += (fghist[im][j] = (q==-1) ? 0 : 1); // // // If all pixels in history were background, this is easy... // if(nfg[j]==0) // q = -1; // // else { // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~(sum[k]/nfg[j] + hist[i-navg/2][k])>>1; // else // q += ~(sum[k]/nfg[j]); // k++; q<<=8; p>>=8; // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1); // else // q += ~(sum[k]/nfg[j]); // k++; q<<=8; p>>=8; // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1); // else // q += ~(sum[k]/nfg[j]); // k++; // } pixelsi[j] = q; } bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); bimage.setRGB(0, 0, w, h, pixelsi, 0, w); // save it as a file if (i >= navg) { try { ImageIO.write(bimage, "png", imageFile(0, ii + 1)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } done(ii + 1); // System.err.println("Page " + i + " " + (System.currentTimeMillis()-t0)/1000.); if (terminated) { System.err.println("Prematurely terminated"); for (File f : files) f.delete(); tmpdir.delete(); break; } } }
private IFD writeBModHufImage( ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException { try { int width = image.getWidth(); int height = image.getHeight(); IFD ifd = new IFD(); // entries need to be in tag order ! ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file ifd.add(new DEFactory.ImageWidthDE(width)); // 256 ifd.add(new DEFactory.ImageLengthDE(height)); // 257 ifd.add(new DEFactory.CompressionDE(CCITTGROUP3MODHUFFMAN)); // 259 ifd.add(new DEFactory.PhotometricInterpretationDE(WhiteIsZero)); // 262 int maxrps, maxstripes; // max RowsPerStrip if ((1 << 13) <= width) { maxrps = 1; maxstripes = height; // one row per stripe } else { maxrps = (1 << 13) / width; maxstripes = (height + maxrps - 1) / maxrps; } DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes); ifd.add(offsets); // 273 ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278 DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes); ifd.add(counts); // 279 if (param == null) { ifd.add(new DEFactory.XResolutionDE(72.0)); // 282 ifd.add(new DEFactory.YResolutionDE(72.0)); // 283 } else { ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282 ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283 } ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296 int index = 0; for (int y = 0; y < height; y += maxrps) { /* Assume bilevel image (black/white[=-1]) Each strip: count run length encode into modified hufman codes swap bits save in byte array write to image file */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); BitSwapOutputStream bsos = new BitSwapOutputStream(baos); ModHuffmanOutputStream mhos = new ModHuffmanOutputStream(bsos); RLEOutputStream rlos = new RLEOutputStream(mhos, 3); // rgb = 3 bytes per sample code word (not needed here) for (int i = 0; i < maxrps; i++) { if ((y + i) == height) { break; } // last strip might have less rows rlos.setStartCodeWord(-1); // white run first for (int x = 0; x < width; x++) { rlos.write(image.getRGB(x, y + i)); } rlos.flush(); // write padding after ever image row } rlos.close(); byte[] data = baos.toByteArray(); counts.setCount(index, data.length); // update ifd strip counter array offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array out.write(data); // write to image stream index++; } return ifd; } catch (Exception e) { e.printStackTrace(); throw new IOException(getClass().getName() + ".writeBModHufImage:\n\t" + e.getMessage()); } }
private IFD writeCMYKImage(ImageOutputStream out, BufferedImage image, TIFFImageWriteParam param) throws IOException { try { int width = image.getWidth(); int height = image.getHeight(); IFD ifd = new IFD(); // entries need to be in tag order ! ifd.add(new DEFactory.NewSubfileTypeDE(2)); // 254 single page of multipage file ifd.add(new DEFactory.ImageWidthDE(width)); // 256 ifd.add(new DEFactory.ImageLengthDE(height)); // 257 DEFactory.BitsPerSampleDE bpss = new DEFactory.BitsPerSampleDE(4); bpss.setBitsPerSample(0, 8); // cyan bpss.setBitsPerSample(1, 8); // magneta bpss.setBitsPerSample(2, 8); // yellow bpss.setBitsPerSample(3, 8); // key (black) ifd.add(bpss); // 258 ifd.add(new DEFactory.CompressionDE(NOCOMPRESSION)); // 259 ifd.add(new DEFactory.PhotometricInterpretationDE(CMYK)); // 262 int maxrps, maxstripes; // max RowsPerStrip if ((1 << 13) <= width) { maxrps = 1; maxstripes = height; // one row per strip } else { maxrps = (1 << 13) / width; maxstripes = (height + maxrps - 1) / maxrps; } DEFactory.StripOffsetsDE offsets = new DEFactory.StripOffsetsDE(maxstripes); ifd.add(offsets); // 273 ifd.add(new DEFactory.SamplesPerPixelDE(4)); // 277 ifd.add(new DEFactory.RowsPerStripDE(maxrps)); // 278 DEFactory.StripByteCountsDE counts = new DEFactory.StripByteCountsDE(maxstripes); ifd.add(counts); // 279 if (param == null) { ifd.add(new DEFactory.XResolutionDE(72.0)); // 282 ifd.add(new DEFactory.YResolutionDE(72.0)); // 283 } else { ifd.add(new DEFactory.XResolutionDE(param.getXResolution())); // 282 ifd.add(new DEFactory.YResolutionDE(param.getYResolution())); // 283 } ifd.add(new DEFactory.ResolutionUnitDE(Inch)); // 296 int index = 0; for (int y = 0; y < height; y += maxrps) { /* Assume rgb image. Each strip: evaluate c m y k colour save in byte array write to image file */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < maxrps; i++) { if ((y + i) == height) { break; } // last strip might have less rows for (int x = 0; x < width; x++) { int c = image.getRGB(x, y + i); int R = (c >> 16) & 0x00FF; int G = (c >> 8) & 0x00FF; int B = (c) & 0x00FF; if ((R == 255) && (G == 255) && (B == 255)) { baos.write(0); baos.write(0); baos.write(0); baos.write(0); } else { double C = 1.0 - R / 255.0; double M = 1.0 - G / 255.0; double Y = 1.0 - B / 255.0; double K = C; if (M < K) { K = M; } if (Y < K) { K = Y; } C = ((C - K) / (1.0 - K)) * 255.0; M = ((M - K) / (1.0 - K)) * 255.0; Y = ((Y - K) / (1.0 - K)) * 255.0; K *= 255.0; baos.write((int) C); baos.write((int) M); baos.write((int) Y); baos.write((int) K); } } } baos.close(); byte[] data = baos.toByteArray(); counts.setCount(index, data.length); // update ifd strip counter array offsets.setOffset(index, out.getStreamPosition()); // update ifd image data offset array out.write(data); // write to image stream index++; } return ifd; } catch (Exception e) { e.printStackTrace(); throw new IOException(getClass().getName() + ".writeCMYKImage:\n\t" + e.getMessage()); } }
public erosion(String nombre) { imageName = nombre; // BufferedImage image2; String aux; // int ancho=image.getWidth(); // BufferedImage image = ImageIO.read( new File( imageName ) ); try { image = ImageIO.read(new File(imageName)); } catch (IOException e) { System.out.println("image missing"); } try { image2 = ImageIO.read(new File(imageName)); } catch (IOException e) { System.out.println("image missing"); } int k; int a; int b; int i; int j; int rojo; int verde; int azul; int promedio; try { int ancho, alto; alto = image.getHeight(); ancho = image.getWidth(); for (i = 0; i < ancho; i++) { for (j = 0; j < alto; j++) { k = image.getRGB(i, j); k = 0xFFFFFF + k; rojo = k / 0x10000; k = k % 0x10000; verde = k / 0x100; k = k % 0x100; azul = k; if (rojo < 0) rojo = 255 + rojo; if (verde < 0) verde = 255 + verde; if (azul < 0) azul = 255 + azul; promedio = azul + verde + rojo; promedio = promedio / 3; /*if(promedio<0) promedio=promedio+128;*/ rojo = promedio; verde = promedio; azul = promedio; // printf("") if (promedio >= 128) promedio = 255; else promedio = 0; k = promedio + promedio * 0x100 + promedio * 0x10000; this.image.setRGB(i, j, k); } } } catch (Exception e) { System.out.printf("n"); } // try { int ancho, alto; alto = image2.getHeight(); ancho = image2.getWidth(); for (i = 0; i < ancho; i++) { for (j = 0; j < alto; j++) { k = image2.getRGB(i, j); k = 0xFFFFFF + k; rojo = k / 0x10000; k = k % 0x10000; verde = k / 0x100; k = k % 0x100; azul = k; if (rojo < 0) rojo = 255 + rojo; if (verde < 0) verde = 255 + verde; if (azul < 0) azul = 255 + azul; promedio = azul + verde + rojo; promedio = promedio / 3; /*if(promedio<0) promedio=promedio+128;*/ rojo = promedio; verde = promedio; azul = promedio; // printf("") k = promedio + promedio * 0x100 + promedio * 0x10000; this.image2.setRGB(i, j, k); } } } catch (Exception e) { System.out.printf("n"); } // try { int ancho, alto; alto = image.getHeight(); ancho = image.getWidth(); for (i = 0; i < ancho; i++) { for (j = 0; j < alto; j++) { k = image.getRGB(i, j); k = 0xFFFFFF + k; rojo = k / 0x10000; k = k % 0x10000; verde = k / 0x100; k = k % 0x100; azul = k; if (rojo < 0) rojo = 255 + rojo; if (verde < 0) verde = 255 + verde; if (azul < 0) azul = 255 + azul; /*promedio=azul+verde+rojo; promedio=promedio/3; /*if(promedio<0) promedio=promedio+128;*/ if (i < ancho - 2) { if (rojo >= 128) { k = image.getRGB(i + 1, j); k = 0xFFFFFF + k; rojo = k / 0x10000; k = k % 0x10000; verde = k / 0x100; k = k % 0x100; azul = k; if (rojo < 0) rojo = 255 + rojo; if (verde < 0) verde = 255 + verde; if (azul < 0) azul = 255 + azul; if (rojo >= 128) { k = image.getRGB(i + 2, j); k = 0xFFFFFF + k; rojo = k / 0x10000; k = k % 0x10000; verde = k / 0x100; k = k % 0x100; azul = k; if (rojo < 0) rojo = 255 + rojo; if (verde < 0) verde = 255 + verde; if (azul < 0) azul = 255 + azul; if (rojo < 128) { k = 0; this.image2.setRGB(i, j, k); this.image2.setRGB(i + 1, j, k); this.image2.setRGB(i + 2, j, k); } } } else { k = rojo + rojo * 0x100 + rojo * 0x10000; this.image2.setRGB(i, j, k); } } } } } catch (Exception e) { System.out.printf("n"); } // /*else if(l==4) posterizacion();*/ // Toolkit tool = Toolkit.getDefaultToolkit(); // image = tool.getImage(imageName); // dialogo.setLocationRelativeTo(f); }
public int[] getRow(int[] into, int y, int offset) { return _i.getRGB(0, y, into.length - 2 * offset, 1, into, offset, 0); }
public int[] getRow(int[] into, int y) { return _i.getRGB(0, y, into.length, 1, into, 0, 0); }
public int get(int x, int y) { x = boundx(x); y = boundy(y); return _i.getRGB(x, y); }
/** * Loads PNG files and returns the result as an int[][]. The only PNG formats permitted are those * with up to 256 grays (including simple black and white) or indexed colors from an up to * 256-sized color table. Each integer value represents the gray level or the color table index * value of the pixel. The Y dimension is not flipped. */ public static int[][] loadPNGFile(InputStream str) throws IOException { // read the bytes into a byte array BufferedInputStream stream = new BufferedInputStream(str); ArrayList list = new ArrayList(); int count = 0; while (true) { byte[] buffer = new byte[16384 * 16]; int len = stream.read(buffer); if (len <= 0) // all done break; else if (len < buffer.length) { byte[] buf2 = new byte[len]; System.arraycopy(buffer, 0, buf2, 0, len); buffer = buf2; } count += len; list.add(buffer); } byte[] data = new byte[count]; int cur = 0; for (int i = 0; i < list.size(); i++) { byte[] b = (byte[]) (list.get(i)); System.arraycopy(b, 0, data, cur, b.length); cur += b.length; } // Next convert the byte array to a buffered image BufferedImage image = ((ToolkitImage) (new ImageIcon(data).getImage())).getBufferedImage(); // Is the color model something we can use? int type = image.getType(); if (type == BufferedImage.TYPE_BYTE_BINARY || type == BufferedImage.TYPE_BYTE_GRAY) { int w = image.getWidth(); int h = image.getHeight(); int[][] result = new int[w][h]; // obviously this could be done more efficiently for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) result[i][j] = (image.getRGB(i, j) & 0xFF); return result; } else if (type == BufferedImage.TYPE_BYTE_INDEXED) { Raster raster = image.getRaster(); if (raster.getTransferType() != DataBuffer.TYPE_BYTE) // uh oh throw new IOException("Input Stream must contain an image with byte data if indexed."); byte[] pixel = new byte[1]; int w = image.getWidth(); int h = image.getHeight(); int[][] result = new int[w][h]; // obviously this could be done more efficiently for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) { result[i][j] = ((byte[]) (raster.getDataElements(i, j, pixel)))[0]; if (result[i][j] < 0) result[i][j] += 256; } return result; } // else if (type == TYPE_USHORT_GRAY) // at present we don't handle shorts // { // } else throw new IOException( "Input Stream must contain a binary, byte-sized grayscale, or byte-sized indexed color scheme: " + image); }