Java Code Examples for java.awt.image.Raster#createPackedRaster()
The following examples show how to use
java.awt.image.Raster#createPackedRaster() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteRaster.java From osp with GNU General Public License v3.0 | 6 votes |
/** * Constructs a byte raster with the given size. * * Unsigned cell values are 0 to 255. Signed cell values are -128 to 127. * * @param _nx the number of values in x direction * @param _ny the number of values in y direction */ public ByteRaster(int _nx, int _ny) { ny = _ny; nx = _nx; dimension = new Dimension(nx-1, ny-1); // decrease by one to fit inside axes int len = nx*ny; packedData = new byte[len]; DataBuffer databuffer = new DataBufferByte(packedData, len); raster = Raster.createPackedRaster(databuffer, nx, ny, 8, null); colorModel = createColorModel(); image = new BufferedImage(colorModel, raster, false, null); xmin = 0; xmax = nx; ymin = 0; ymax = ny; }
Example 2
Source File: IMGUtils.java From icafe with Eclipse Public License 1.0 | 6 votes |
public static void RGB2CMYK(ICC_ColorSpace cmykColorSpace, int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) { DataBuffer db = new DataBufferInt(rgb, rgb.length); WritableRaster raster = Raster.createPackedRaster(db, imageWidth, imageHeight, imageWidth, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff}, null); ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorConvertOp cco = new ColorConvertOp(sRGB, cmykColorSpace, null); BufferedImage rgbImage = new BufferedImage(new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff), raster, false, null); BufferedImage cmykImage = cco.filter(rgbImage, null); WritableRaster cmykRaster = cmykImage.getRaster(); byte[] cmyk = (byte[])cmykRaster.getDataElements(0, 0, imageWidth, imageHeight, null); for(int i = 0, index = 0; i < imageHeight; i++) { for(int j = 0; j < imageWidth; j++) { C[i][j] = (cmyk[index++]&0xff) - 128.0f; M[i][j] = (cmyk[index++]&0xff) - 128.0f; Y[i][j] = (cmyk[index++]&0xff) - 128.0f; K[i][j] = (cmyk[index++]&0xff) - 128.0f; } } }
Example 3
Source File: BinaryLattice.java From osp with GNU General Public License v3.0 | 6 votes |
/** * Constructs a binary lattice with the given size. * @param _nx the number of values in x direction * @param _ny the number of values in y direction */ public BinaryLattice(int _nx, int _ny) { ny = _ny; nx = _nx; int len = ((nx+7)/8)*ny; // each row starts on a byte boundary packedData = new byte[len]; DataBuffer databuffer = new DataBufferByte(packedData, len); raster = Raster.createPackedRaster(databuffer, nx, ny, 1, null); // default colors are red and blue ColorModel colorModel = new IndexColorModel(1, 2, new byte[] {(byte) 255, (byte) 0}, new byte[] {(byte) 0, (byte) 0}, new byte[] {(byte) 0, (byte) 255}); image = new BufferedImage(colorModel, raster, false, null); xmin = 0; xmax = nx; ymin = 0; ymax = ny; grid = new Grid(nx, ny, xmin, xmax, ymin, ymax); grid.setColor(Color.lightGray); }
Example 4
Source File: IMGUtils.java From icafe with Eclipse Public License 1.0 | 6 votes |
public static void RGB2CMYK_Inverted(ICC_ColorSpace cmykColorSpace, int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) { DataBuffer db = new DataBufferInt(rgb, rgb.length); WritableRaster raster = Raster.createPackedRaster(db, imageWidth, imageHeight, imageWidth, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff}, null); ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorConvertOp cco = new ColorConvertOp(sRGB, cmykColorSpace, null); BufferedImage rgbImage = new BufferedImage(new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff), raster, false, null); BufferedImage cmykImage = cco.filter(rgbImage, null); WritableRaster cmykRaster = cmykImage.getRaster(); byte[] cmyk = (byte[])cmykRaster.getDataElements(0, 0, imageWidth, imageHeight, null); for(int i = 0, index = 0; i < imageHeight; i++) { for(int j = 0; j < imageWidth; j++) { C[i][j] = 128.0f - (cmyk[index++]&0xff); M[i][j] = 128.0f - (cmyk[index++]&0xff); Y[i][j] = 128.0f - (cmyk[index++]&0xff); K[i][j] = 128.0f - (cmyk[index++]&0xff); } } }
Example 5
Source File: PCXReader.java From icafe with Eclipse Public License 1.0 | 6 votes |
private BufferedImage readOnePlaneEgaPcx(InputStream is) throws Exception { // Try to decode 2, 4 and 16 color images as implemented // by using 1, 2 and 4 bits per pixel and one color plane byte brgb[] = IOUtils.readFully(is, 4096); byte[] pixels = new byte[bytesPerLine*height]; readScanLines(brgb, brgb.length, pixels); is.close(); DataBuffer db = new DataBufferByte(pixels, pixels.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, bitsPerPixel, null); if(bitsPerPixel == 1) { int BW_palette[] = new int[2]; BW_palette[0] = 0xff000000; BW_palette[1] =0xff000000|0xff0000|0xff00|0xff; rgbColorPalette = BW_palette; } ColorModel cm = new IndexColorModel(bitsPerPixel, rgbColorPalette.length, rgbColorPalette, 0, false, -1, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); }
Example 6
Source File: ImageRepresentation.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example 7
Source File: ImageRepresentation.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example 8
Source File: WDataTransferer.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ @Override protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { String mimeType = null; if (format == CF_PNG) { mimeType = "image/png"; } else if (format == CF_JFIF) { mimeType = "image/jpeg"; } if (mimeType != null) { return standardImageBytesToImage(bytes, mimeType); } int[] imageData = platformImageBytesToImageData(bytes, format); if (imageData == null) { throw new IOException("data translation failed"); } int len = imageData.length - 2; int width = imageData[len]; int height = imageData[len + 1]; DataBufferInt buffer = new DataBufferInt(imageData, len); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandmasks, null); return new BufferedImage(directColorModel, raster, false, null); }
Example 9
Source File: ImageRepresentation.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example 10
Source File: ImageRepresentation.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example 11
Source File: ImageRepresentation.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public BufferedImage getOpaqueRGBImage() { if (bimage.getType() == BufferedImage.TYPE_INT_ARGB) { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w * h; // Note that we steal the data array here, but only for reading... DataBufferInt db = (DataBufferInt)biRaster.getDataBuffer(); int[] pixels = SunWritableRaster.stealData(db, 0); for (int i = 0; i < size; i++) { if ((pixels[i] >>> 24) != 0xff) { return bimage; } } ColorModel opModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff); int bandmasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff}; WritableRaster opRaster = Raster.createPackedRaster(db, w, h, w, bandmasks, null); try { BufferedImage opImage = createImage(opModel, opRaster, false, null); return opImage; } catch (Exception e) { return bimage; } } return bimage; }
Example 12
Source File: SaneImage.java From jfreesane with Apache License 2.0 | 5 votes |
private BufferedImage decodeSingleBitGrayscaleImage(DataBuffer buffer) { WritableRaster raster = Raster.createPackedRaster(buffer, width, height, 1, new Point(0, 0)); return new BufferedImage( new IndexColorModel( 1, 2, new byte[] {(byte) 0xff, 0}, new byte[] {(byte) 0xff, 0}, new byte[] {(byte) 0xff, 0}), raster, false, null); }
Example 13
Source File: WDataTransferer.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ @Override protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { String mimeType = null; if (format == CF_PNG) { mimeType = "image/png"; } else if (format == CF_JFIF) { mimeType = "image/jpeg"; } if (mimeType != null) { return standardImageBytesToImage(bytes, mimeType); } int[] imageData = platformImageBytesToImageData(bytes, format); if (imageData == null) { throw new IOException("data translation failed"); } int len = imageData.length - 2; int width = imageData[len]; int height = imageData[len + 1]; DataBufferInt buffer = new DataBufferInt(imageData, len); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandmasks, null); return new BufferedImage(directColorModel, raster, false, null); }
Example 14
Source File: WDataTransferer.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Translates either a byte array or an input stream which contain * platform-specific image data in the given format into an Image. */ protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { String mimeType = null; if (format == CF_PNG) { mimeType = "image/png"; } else if (format == CF_JFIF) { mimeType = "image/jpeg"; } if (mimeType != null) { return standardImageBytesToImage(bytes, mimeType); } int[] imageData = platformImageBytesToImageData(bytes, format); if (imageData == null) { throw new IOException("data translation failed"); } int len = imageData.length - 2; int width = imageData[len]; int height = imageData[len + 1]; DataBufferInt buffer = new DataBufferInt(imageData, len); WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, bandmasks, null); return new BufferedImage(directColorModel, raster, false, null); }
Example 15
Source File: ImageRepresentation.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private void convertToRGB() { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w*h; DataBufferInt dbi = new DataBufferInt(size); // Note that stealData() requires a markDirty() afterwards // since we modify the data in it. int newpixels[] = SunWritableRaster.stealData(dbi, 0); if (cmodel instanceof IndexColorModel && biRaster instanceof ByteComponentRaster && biRaster.getNumDataElements() == 1) { ByteComponentRaster bct = (ByteComponentRaster) biRaster; byte[] data = bct.getDataStorage(); int coff = bct.getDataOffset(0); for (int i=0; i < size; i++) { newpixels[i] = srcLUT[data[coff+i]&0xff]; } } else { Object srcpixels = null; int off=0; for (int y=0; y < h; y++) { for (int x=0; x < w; x++) { srcpixels=biRaster.getDataElements(x, y, srcpixels); newpixels[off++] = cmodel.getRGB(srcpixels); } } } // We modified the data array directly above so mark it as dirty now... SunWritableRaster.markDirty(dbi); isSameCM = false; cmodel = ColorModel.getRGBdefault(); int bandMasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}; biRaster = Raster.createPackedRaster(dbi,w,h,w, bandMasks,null); bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null); srcLUT = null; isDefaultBI = true; }
Example 16
Source File: ImageRepresentation.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private void convertToRGB() { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w*h; DataBufferInt dbi = new DataBufferInt(size); // Note that stealData() requires a markDirty() afterwards // since we modify the data in it. int newpixels[] = SunWritableRaster.stealData(dbi, 0); if (cmodel instanceof IndexColorModel && biRaster instanceof ByteComponentRaster && biRaster.getNumDataElements() == 1) { ByteComponentRaster bct = (ByteComponentRaster) biRaster; byte[] data = bct.getDataStorage(); int coff = bct.getDataOffset(0); for (int i=0; i < size; i++) { newpixels[i] = srcLUT[data[coff+i]&0xff]; } } else { Object srcpixels = null; int off=0; for (int y=0; y < h; y++) { for (int x=0; x < w; x++) { srcpixels=biRaster.getDataElements(x, y, srcpixels); newpixels[off++] = cmodel.getRGB(srcpixels); } } } // We modified the data array directly above so mark it as dirty now... SunWritableRaster.markDirty(dbi); isSameCM = false; cmodel = ColorModel.getRGBdefault(); int bandMasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}; biRaster = Raster.createPackedRaster(dbi,w,h,w, bandMasks,null); bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null); srcLUT = null; isDefaultBI = true; }
Example 17
Source File: TGAReader.java From icafe with Eclipse Public License 1.0 | 4 votes |
public BufferedImage read(InputStream is) throws Exception { tgaHeader = new TgaHeader(); tgaHeader.readHeader(is); bitsPerPixel = tgaHeader.bits_per_pixel; width = tgaHeader.width; height = tgaHeader.height; pix = new int[width*height]; if (tgaHeader.colourmap_type != 0 && tgaHeader.colourmap_type != 1) { LOGGER.error("Can only handle colour map types of 0 and 1"); return null; } scanMode = ((tgaHeader.image_descriptor&0x30)>>4); switch (scanMode) { case SCAN_MODE_BOTTOM_LEFT: l = height-1; m = -1; n = 0; o = 1; break; case SCAN_MODE_BOTTOM_RIGHT: l = height-1; m = -1; n = width-1; o = -1; break; case SCAN_MODE_TOP_LEFT: l = 0; m = 1; n = 0; o = 1; break; case SCAN_MODE_TOP_RIGHT: l = 0; m = 1; n = width-1; o = -1; break; default: } LOGGER.info("Image x_origin: {}", tgaHeader.x_origin); LOGGER.info("Image y_origin: {}", tgaHeader.y_origin); switch (tgaHeader.image_type) { case 0: LOGGER.info("There are no data in the image file"); System.exit(1); case 1: readCMPTga(is); break; case 2: readTrueColorTga(is); break; case 3: read_BW_Tga(is); break; case 9: read_RLE_CMP_Tga(is); break; case 10: read_RLE_TrueColor_Tga(is); break; case 11: read_RLE_BW_Tga(is); break; case 32: case 33: LOGGER.error("Not implemented for compressed color mapped images"); return null; default: LOGGER.error("I can't find a type matches this"); return null; } //Create a BufferedImage DataBuffer db = new DataBufferInt(pix, pix.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, width, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, null); ColorModel cm = new DirectColorModel(32, 0x00FF0000, 0x0000ff00, 0x000000ff, 0xff000000); return new BufferedImage(cm, raster, false, null); }
Example 18
Source File: PCXReader.java From icafe with Eclipse Public License 1.0 | 4 votes |
private BufferedImage readOneBitEgaPcx(InputStream is) throws Exception { int index = 0, counter = 0, abyte = 0, nindex = 0; int bt = 0, bt1 = 0; int totalBytes = 0, num_of_rep = 0; int buf[]; byte brgb[] = IOUtils.readFully(is, 4096); int buf_len = brgb.length; totalBytes = bytesPerLine*NPlanes; buf = new int[totalBytes]; byte[] pixels; BytePacker bytePacker = new BytePacker(bitsPerPixel, width, width*height); image: for(int i = 0; i < height; i++, index = 0) { do { bt = brgb[nindex++]&0xff; if((bt&0xC0) == 0xC0) { num_of_rep = bt&0x3F; bt1 = brgb[nindex++]&0xff; for(int k = 0; k < num_of_rep && index < totalBytes; k++) { buf[index++] = bt1; } if (nindex >= buf_len) { break image; } } else { buf[index++] = bt; if (nindex >= buf_len) { break image; } } } while(index < totalBytes); scanLine: for(int k = 0; k < bytesPerLine; k++) { for(int l = 7; l >= 0; l--) { for(int m = 0; m < NPlanes; m++) { abyte |= (((buf[k + bytesPerLine*m]>>l)&0x01)<<m); } bytePacker.packByte(abyte); abyte = 0; // Must reset here if(++counter%width == 0) break scanLine; } } } is.close(); pixels = bytePacker.getPackedBytes(); DataBuffer db = new DataBufferByte(pixels, pixels.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, bitsPerPixel, null); ColorModel cm = new IndexColorModel(bitsPerPixel, rgbColorPalette.length, rgbColorPalette, 0, false, -1, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); }
Example 19
Source File: ImageRepresentation.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
private void convertToRGB() { int w = bimage.getWidth(); int h = bimage.getHeight(); int size = w*h; DataBufferInt dbi = new DataBufferInt(size); // Note that stealData() requires a markDirty() afterwards // since we modify the data in it. int newpixels[] = SunWritableRaster.stealData(dbi, 0); if (cmodel instanceof IndexColorModel && biRaster instanceof ByteComponentRaster && biRaster.getNumDataElements() == 1) { ByteComponentRaster bct = (ByteComponentRaster) biRaster; byte[] data = bct.getDataStorage(); int coff = bct.getDataOffset(0); for (int i=0; i < size; i++) { newpixels[i] = srcLUT[data[coff+i]&0xff]; } } else { Object srcpixels = null; int off=0; for (int y=0; y < h; y++) { for (int x=0; x < w; x++) { srcpixels=biRaster.getDataElements(x, y, srcpixels); newpixels[off++] = cmodel.getRGB(srcpixels); } } } // We modified the data array directly above so mark it as dirty now... SunWritableRaster.markDirty(dbi); isSameCM = false; cmodel = ColorModel.getRGBdefault(); int bandMasks[] = {0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}; biRaster = Raster.createPackedRaster(dbi,w,h,w, bandMasks,null); bimage = createImage(cmodel, biRaster, cmodel.isAlphaPremultiplied(), null); srcLUT = null; isDefaultBI = true; }
Example 20
Source File: BMPReader.java From icafe with Eclipse Public License 1.0 | 4 votes |
private BufferedImage readIndexColorBitmap(InputStream is) throws Exception { LOGGER.info("{} color bitmap color image!", (1<<bitsPerPixel)); readPalette(is); int npad = 0; switch(bitsPerPixel) { case 1: npad = (32-(width%32))/8; break; case 4: npad = (32-((width*4)%32))/8; break; case 8: npad = bytePerScanLine - width; break; default: throw new IllegalArgumentException("Invalid bitsPerPixel: " + bitsPerPixel + " for BMP indexColor image!"); } if(npad == 4) npad = 0; int bytePerWidth = bytePerScanLine - npad; byte[] buffer = new byte[bytePerScanLine]; byte[] pixels = new byte[bytePerWidth * height]; LOGGER.info("Scanline padding: {}", npad); if(alignment == BMPOptions.ALIGN_BOTTOM_UP) { for(int i = 0, startIndex = (height-1)*bytePerWidth; i < height; i++, startIndex -= bytePerWidth) { IOUtils.readFully(is, buffer); System.arraycopy(buffer, 0, pixels, startIndex, bytePerWidth); } } else { for(int i = 0, startIndex = 0; i < height; i++, startIndex += bytePerWidth) { IOUtils.readFully(is, buffer); System.arraycopy(buffer, 0, pixels, startIndex, bytePerWidth); } } is.close(); // Create BufferedImage DataBuffer db = new DataBufferByte(pixels, pixels.length); WritableRaster raster = null; if(bitsPerPixel != 8) { raster = Raster.createPackedRaster(db, width, height, bitsPerPixel, null); } else { int[] off = {0};//band offset, we have only one band start at 0 raster = Raster.createInterleavedRaster(db, width, height, width, 1, off, null); } ColorModel cm = new IndexColorModel(bitsPerPixel, rgbColorPalette.length, rgbColorPalette, 0, false, -1, DataBuffer.TYPE_BYTE); return new BufferedImage(cm, raster, false, null); }