void write(ImageOutputStream ios, JPEGImageWriter writer) throws IOException { super.write(ios, writer); // width and height // Write the palette (must be 768 bytes) byte [] palette = new byte[768]; IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel(); byte [] reds = new byte [256]; byte [] greens = new byte [256]; byte [] blues = new byte [256]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); for (int i = 0; i < 256; i++) { palette[i*3] = reds[i]; palette[i*3+1] = greens[i]; palette[i*3+2] = blues[i]; } ios.write(palette); writePixels(ios, writer); }
private IIOMetadataNode gethISTNode(BufferedImage bi) { IndexColorModel icm = (IndexColorModel)bi.getColorModel(); int mapSize = icm.getMapSize(); int[] hist = new int[mapSize]; Arrays.fill(hist, 0); Raster r = bi.getData(); for (int y = 0; y < bi.getHeight(); y++) { for (int x = 0; x < bi.getWidth(); x++) { int s = r.getSample(x, y, 0); hist[s] ++; } } IIOMetadataNode hIST = new IIOMetadataNode("hIST"); for (int i = 0; i < hist.length; i++) { IIOMetadataNode n = new IIOMetadataNode("hISTEntry"); n.setAttribute("index", "" + i); n.setAttribute("value", "" + hist[i]); hIST.appendChild(n); } return hIST; }
private static void encodeRLE4Test() throws IOException { // create 4bpp image byte[] r = new byte[16]; r[0] = (byte)0xff; byte[] g = new byte[16]; g[1] = (byte)0xff; byte[] b = new byte[16]; b[2] = (byte)0xff; IndexColorModel icm = new IndexColorModel(4, 16, r, g, b); BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY, icm); Graphics gr = bimg.getGraphics(); gr.setColor(Color.green); gr.fillRect(0, 0, 100, 100); doTest(bimg, "BI_RLE4", ImageWriteParam.MODE_EXPLICIT); }
/** * Constructs a color map with more than one color components. * @param f arrays of floats, one array for each color component. * @param ic array index of the component for the index color model. * @param icm the index color model corresponding to one component. */ public FloatColorMap(float[][][] f, int ic, IndexColorModel icm) { super(icm); Check.argument( f.length==1 || f.length==3 || f.length==4, "number of arrays (color components) equals 1, 3, or 4"); int nc = f.length; FloatByteMap[] fbm = new FloatByteMap[nc]; for (int jc=0; jc<nc; ++jc) fbm[jc] = new FloatByteMap(f[jc]); _fbmi0 = fbm[0]; _fbmi1 = (nc>1)?fbm[1]:fbm[0]; _fbmi2 = (nc>1)?fbm[2]:fbm[0]; _fbmi3 = (nc>3)?fbm[3]:null; _fbmic = fbm[ic]; }
private BufferedImage createIndexImage(int bpp) { // calculate palette size int psize = (1 << bpp); // prepare palette; byte[] r = new byte[psize]; byte[] g = new byte[psize]; byte[] b = new byte[psize]; for (int i = 0; i < colors.length; i++) { r[i] = (byte)(0xff & colors[i].getRed()); g[i] = (byte)(0xff & colors[i].getGreen()); b[i] = (byte)(0xff & colors[i].getBlue()); } // now prepare appropriate index clor model IndexColorModel icm = new IndexColorModel(bpp, psize, r, g, b); return new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm); }
private void Setup() { balls = new Image[nBalls]; byte red[] = new byte[256]; red[0] = (byte) bgGrey; byte green[] = new byte[256]; green[0] = (byte) bgGrey; byte blue[] = new byte[256]; blue[0] = (byte) bgGrey; for (int r = 0; r < nBalls; r++) { float b = (float) (r + 1) / nBalls; for (int i = maxr; i >= 1; --i) { float d = (float) i / maxr; red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b); green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b); blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b); } IndexColorModel model = new IndexColorModel(8, maxr + 1, red, green, blue, 0); balls[r] = applet.createImage( new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2)); } }
protected static IndexColorModel createIndexedBitmaskColorModel() { int paletteSize = 8; byte[] red = new byte[paletteSize]; byte[] green = new byte[paletteSize]; byte[] blue = new byte[paletteSize]; red[0] = (byte)0xff; green[0] = (byte)0x00; blue[0] = (byte)0x00; red[1] = (byte)0x00; green[1] = (byte)0xff; blue[1] = (byte)0x00; red[2] = (byte)0x00; green[2] = (byte)0x00; blue[2] = (byte)0xff; red[3] = (byte)0xff; green[3] = (byte)0xff; blue[3] = (byte)0xff; red[4] = (byte)0x00; green[4] = (byte)0x00; blue[4] = (byte)0x00; red[5] = (byte)0x80; green[5] = (byte)0x80; blue[5] = (byte)0x80; red[6] = (byte)0xff; green[6] = (byte)0xff; blue[6] = (byte)0x00; red[7] = (byte)0x00; green[7] = (byte)0xff; blue[7] = (byte)0xff; int numBits = 3; IndexColorModel icm = new IndexColorModel(numBits, paletteSize, red, green, blue, 5); return icm; }
/** * Returns an index color model with specified opacity (alpha). * @param icm an index color model from which to copy RGBs. * @param alpha opacity in the range [0.0,1.0]. * @return the index color model with alpha. */ public static IndexColorModel setAlpha(IndexColorModel icm, double alpha) { int bits = icm.getPixelSize(); int size = icm.getMapSize(); byte[] r = new byte[size]; byte[] g = new byte[size]; byte[] b = new byte[size]; byte[] a = new byte[size]; icm.getReds(r); icm.getGreens(g); icm.getBlues(b); byte ia = (byte)(255.0*alpha+0.5); for (int i=0; i<size; ++i) a[i] = ia; return new IndexColorModel(bits,size,r,g,b,a); }
/** * Returns an index color model with specified opacities (alphas). * @param icm an index color model from which to copy RGBs. * @param alpha array of opacities in the range [0.0,1.0]. * @return the index color model with alphas. */ public static IndexColorModel setAlpha(IndexColorModel icm, float[] alpha) { int bits = icm.getPixelSize(); int size = icm.getMapSize(); byte[] r = new byte[size]; byte[] g = new byte[size]; byte[] b = new byte[size]; byte[] a = new byte[size]; icm.getReds(r); icm.getGreens(g); icm.getBlues(b); int n = min(size,alpha.length); for (int i=0; i<n; ++i) a[i] = (byte)(255.0f*alpha[i]+0.5f); return new IndexColorModel(bits,size,r,g,b,a); }
private static void verifyEquals(IndexColorModel m1, IndexColorModel m2) { if (m1.equals(null)) { throw new RuntimeException("equals(null) returns true"); } if (!(m1.equals(m2))) { throw new RuntimeException("equals() method is not working" + " properly"); } if (!(m2.equals(m1))) { throw new RuntimeException("equals() method is not working" + " properly"); } if (m1.hashCode() != m2.hashCode()) { throw new RuntimeException("HashCode is not same for same" + " IndexColorModels"); } }
public static SurfaceData createDataBC(BufferedImage bImg, SurfaceType sType, int primaryBank, double scaleX, double scaleY) { ByteComponentRaster bcRaster = (ByteComponentRaster)bImg.getRaster(); BufImgSurfaceData bisd = new BufImgSurfaceData(bcRaster.getDataBuffer(), bImg, sType, scaleX, scaleY); ColorModel cm = bImg.getColorModel(); IndexColorModel icm = ((cm instanceof IndexColorModel) ? (IndexColorModel) cm : null); bisd.initRaster(bcRaster.getDataStorage(), bcRaster.getDataOffset(primaryBank), 0, bcRaster.getWidth(), bcRaster.getHeight(), bcRaster.getPixelStride(), bcRaster.getScanlineStride(), icm); return bisd; }
private static synchronized byte[] getDefaultPalette() { if (defaultPalette == null) { BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED); IndexColorModel icm = (IndexColorModel) img.getColorModel(); final int size = icm.getMapSize(); byte[] r = new byte[size]; byte[] g = new byte[size]; byte[] b = new byte[size]; icm.getReds(r); icm.getGreens(g); icm.getBlues(b); defaultPalette = new byte[size * 3]; for (int i = 0; i < size; i++) { defaultPalette[3 * i + 0] = r[i]; defaultPalette[3 * i + 1] = g[i]; defaultPalette[3 * i + 2] = b[i]; } } return defaultPalette; }
public static IndexColorModel createColorModel() { // Create a 6x6x6 color cube int[] cmap = new int[256]; int i = 0; for (int r = 0; r < 256; r += 51) { for (int g = 0; g < 256; g += 51) { for (int b = 0; b < 256; b += 51) { cmap[i++] = (r << 16) | (g << 8) | b; } } } // And populate the rest of the cmap with gray values int grayIncr = 256 / (256 - i); // The gray ramp will be between 18 and 252 int gray = grayIncr * 3; for (; i < 256; i++) { cmap[i] = (gray << 16) | (gray << 8) | gray; gray += grayIncr; } return new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE); }
public static SurfaceData createDataSC(BufferedImage bImg, SurfaceType sType, IndexColorModel icm) { ShortComponentRaster scRaster = (ShortComponentRaster)bImg.getRaster(); BufImgSurfaceData bisd = new BufImgSurfaceData(scRaster.getDataBuffer(), bImg, sType); bisd.initRaster(scRaster.getDataStorage(), scRaster.getDataOffset(0) * 2, 0, scRaster.getWidth(), scRaster.getHeight(), scRaster.getPixelStride() * 2, scRaster.getScanlineStride() * 2, icm); return bisd; }
public static SurfaceData createDataBC(BufferedImage bImg, SurfaceType sType, int primaryBank) { ByteComponentRaster bcRaster = (ByteComponentRaster)bImg.getRaster(); BufImgSurfaceData bisd = new BufImgSurfaceData(bcRaster.getDataBuffer(), bImg, sType); ColorModel cm = bImg.getColorModel(); IndexColorModel icm = ((cm instanceof IndexColorModel) ? (IndexColorModel) cm : null); bisd.initRaster(bcRaster.getDataStorage(), bcRaster.getDataOffset(primaryBank), 0, bcRaster.getWidth(), bcRaster.getHeight(), bcRaster.getPixelStride(), bcRaster.getScanlineStride(), icm); return bisd; }
public static SurfaceData createDataBP(BufferedImage bImg, SurfaceType sType) { BytePackedRaster bpRaster = (BytePackedRaster)bImg.getRaster(); BufImgSurfaceData bisd = new BufImgSurfaceData(bpRaster.getDataBuffer(), bImg, sType); ColorModel cm = bImg.getColorModel(); IndexColorModel icm = ((cm instanceof IndexColorModel) ? (IndexColorModel) cm : null); bisd.initRaster(bpRaster.getDataStorage(), bpRaster.getDataBitOffset() / 8, bpRaster.getDataBitOffset() & 7, bpRaster.getWidth(), bpRaster.getHeight(), 0, bpRaster.getScanlineStride(), icm); return bisd; }
JFIFExtensionMarkerSegment(BufferedImage thumbnail) throws IllegalThumbException { super(JPEG.APP0); ColorModel cm = thumbnail.getColorModel(); int csType = cm.getColorSpace().getType(); if (cm.hasAlpha()) { throw new IllegalThumbException(); } if (cm instanceof IndexColorModel) { code = THUMB_PALETTE; thumb = new JFIFThumbPalette(thumbnail); } else if (csType == ColorSpace.TYPE_RGB) { code = THUMB_RGB; thumb = new JFIFThumbRGB(thumbnail); } else if (csType == ColorSpace.TYPE_GRAY) { code = THUMB_JPEG; thumb = new JFIFThumbJPEG(thumbnail); } else { throw new IllegalThumbException(); } }
private static void testConstructor7() { /* * verify equality with constructor * IndexColorModel(int bits, int size, int[] cmap, int start, * int transferType, BigInteger validBits) */ /* * In setRGBs() function of IndexColorModel we override * transparent_index value to map to pixel value if alpha is 0x00 * so we should have atleast minimum alpha value to keep * both model1 and model2 same. */ int color = 16777216; IndexColorModel model1 = new IndexColorModel(8, 3, new int[] {color, color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1")); IndexColorModel model2 = new IndexColorModel(8, 3, new int[] {color, color, color}, 0, DataBuffer.TYPE_BYTE, new BigInteger("1")); verifyEquals(model1, model2); }
protected static BufferedImage createIndexedImage(int w, int h, IndexColorModel icm) { BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm); int mapSize = icm.getMapSize(); int width = w / mapSize; WritableRaster wr = img.getRaster(); for (int i = 0; i < mapSize; i++) { for (int y = 0; y < h; y++) { for (int x = 0; x < width; x++) { wr.setSample(i * width + x, y, 0, i); } } } return img; }
/** * Modifies the lookup table to display a bright image with gray values * in the range minGray ... 255. Does nothing if ip is of type * ColorProcessor. * * @param ip The target image. * @param minGray Minimum gray value. */ public static void brightLut(ImageProcessor ip, int minGray) { if (minGray < 0 || minGray >= 255) return; ColorModel cm = ip.getColorModel(); if (!(cm instanceof IndexColorModel)) return; IndexColorModel icm = (IndexColorModel) cm; int mapSize = icm.getMapSize(); byte[] reds = new byte[mapSize]; byte[] grns = new byte[mapSize]; byte[] blus = new byte[mapSize]; float scale = (255 - minGray) / 255f; for (int i = 0; i < mapSize; i++) { byte g = (byte) (Math.round(minGray + scale * i) & 0xFF); reds[i] = g; grns[i] = g; blus[i] = g; } ip.setColorModel(new IndexColorModel(8, mapSize, reds, grns, blus)); }
/** * Returns an instance of <code>PNGEncodeParam.Palette</code>, * <code>PNGEncodeParam.Gray</code>, or * <code>PNGEncodeParam.RGB</code> appropriate for encoding * the given image. * * <p> If the image has an <code>IndexColorModel</code>, an * instance of <code>PNGEncodeParam.Palette</code> is returned. * Otherwise, if the image has 1 or 2 bands an instance of * <code>PNGEncodeParam.Gray</code> is returned. In all other * cases an instance of <code>PNGEncodeParam.RGB</code> is * returned. * * <p> Note that this method does not provide any guarantee that * the given image will be successfully encoded by the PNG * encoder, as it only performs a very superficial analysis of * the image structure. */ public static mxPngEncodeParam getDefaultEncodeParam(RenderedImage im) { ColorModel colorModel = im.getColorModel(); if (colorModel instanceof IndexColorModel) { return new mxPngEncodeParam.Palette(); } SampleModel sampleModel = im.getSampleModel(); int numBands = sampleModel.getNumBands(); if (numBands == 1 || numBands == 2) { return new mxPngEncodeParam.Gray(); } else { return new mxPngEncodeParam.RGB(); } }
protected BufferedImage createTestImage(int numColors) { IndexColorModel icm = createTestICM(numColors); int w = numColors * 10; int h = 20; BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm); Graphics2D g = img.createGraphics(); for (int i = 0; i < numColors; i++) { int rgb = icm.getRGB(i); //System.out.printf("pixel %d, rgb %x\n", i, rgb); g.setColor(new Color(rgb)); g.fillRect(i * 10, 0, w - i * 10, h); } g.dispose(); return img; }
protected RenderedImage getIndexedImage() { IndexColorModel icm = getIndexColorModel(); BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm); WritableRaster wr = dst.getRaster(); for (int y =0; y < dst.getHeight(); y++) { for (int x = 0; x < dst.getWidth(); x++) { Color aColor = getSrcColor(x,y); wr.setSample(x, y, 0, findColorIndex(root, aColor)); } } return dst; }
protected void drawDIBImage(byte[] image, float destX, float destY, float destWidth, float destHeight, float srcX, float srcY, float srcWidth, float srcHeight, int sampleBitsPerPixel, IndexColorModel icm) { int bitCount = 24; byte[] bmiColors = null; if (icm != null) { bitCount = sampleBitsPerPixel; bmiColors = new byte[(1<<icm.getPixelSize())*4]; for (int i=0;i<icm.getMapSize(); i++) { bmiColors[i*4+0]=(byte)(icm.getBlue(i)&0xff); bmiColors[i*4+1]=(byte)(icm.getGreen(i)&0xff); bmiColors[i*4+2]=(byte)(icm.getRed(i)&0xff); } } drawDIBImage(getPrintDC(), image, destX, destY, destWidth, destHeight, srcX, srcY, srcWidth, srcHeight, bitCount, bmiColors); }
private void createImage() { if (this.gc != null) { this.image = this.gc.createCompatibleImage(IMAGE_SIZE, IMAGE_SIZE, Transparency.BITMASK); } else { int cmap[] = { this.topColor.getRGB(), this.shadowColor.getRGB() }; IndexColorModel icm = new IndexColorModel(8, 3, cmap, 0, false, -1, DataBuffer.TYPE_BYTE); this.image = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_BYTE_INDEXED, icm); } }
public void colorMapChanged(ColorMap cm) { float vmin = (float)cm.getMinValue(); float vmax = (float)cm.getMaxValue(); if (vmin==vmax) { vmin -= Math.ulp(vmin); vmax += Math.ulp(vmax); } int nv = 256; double dv = (vmax-vmin)/(nv-1); double fv = vmin; Sampling vs = new Sampling(nv,dv,fv); float[][] va = new float[nv][1]; Color[] ca = new Color[nv]; for (int iv=0; iv<nv; ++iv) { float vi = (float)vs.getValue(iv); va[iv][0] = vi; ca[iv] = cm.getColor(vi); } if (_pixels==null) { _pixels = new PixelsView(va); _pixels.setOrientation(PixelsView.Orientation.X1RIGHT_X2UP); _pixels.setInterpolation(PixelsView.Interpolation.LINEAR); _tile.addTiledView(_pixels); } IndexColorModel icm = ColorMap.makeIndexColorModel(ca); _pixels.setClips(vmin,vmax); _pixels.setColorModel(icm); Sampling s1 = new Sampling(1); Sampling s2 = vs; _pixels.set(s1,s2,va); }
/** * Determines if a specified color map has more than one color. * Note that we ignore any variation in alpha. */ private static boolean isMultiColor(ColorMapped cm) { ColorMap cmap = cm.getColorMap(); IndexColorModel icm = cmap.getColorModel(); int n = icm.getMapSize(); int rgb = icm.getRGB(0)&0x00ffffff; for (int i=1; i<n; ++i) if (rgb!=(icm.getRGB(i)&0x00ffffff)) return true; return false; }
protected IndexColorModel createTestICM(int numColors) { int[] palette = createTestPalette(numColors); int numBits = getNumBits(numColors); IndexColorModel icm = new IndexColorModel(numBits, numColors, palette, 0, false, -1, DataBuffer.TYPE_BYTE); return icm; }
/** * Constructs a color map for specified values. * The integers 0 and 255 must be valid pixels for the color model. * @param vmin the minimum value. * @param vmax the maximum value. * @param colorModel the index color model. */ public ColorMap(double vmin, double vmax, IndexColorModel colorModel) { Check.argument(colorModel.isValid(0),"0 is valid for color model"); Check.argument(colorModel.isValid(255),"255 is valid for color model"); _vmin = vmin; _vmax = vmax; _colorModel = colorModel; cacheColors(); }
public ByteFilter(ByteInterleavedRaster srcRas, ColorModel cm, AffineTransform xform, int maxw) { super((cm.getTransparency() == Transparency.OPAQUE ? xrgbmodel : argbmodel), xform, srcRas.getWidth(), srcRas.getHeight(), maxw); this.inPalette = new int[256]; ((IndexColorModel) cm).getRGBs(this.inPalette); this.srcRas = srcRas; this.inData = srcRas.getDataStorage(); this.inSpan = srcRas.getScanlineStride(); this.inOff = srcRas.getDataOffset(0); }
private static BufferedImage createTestImage(int paletteSize) { byte[] r = new byte[paletteSize]; byte[] g = new byte[paletteSize]; byte[] b = new byte[paletteSize]; int shift = 256 / paletteSize; for (int i = 0; i < paletteSize; i++) { r[i] = g[i] = b[i] = (byte)(shift * i); } int numBits = getNumBits(paletteSize); System.out.println("num of bits " + numBits); IndexColorModel icm = new IndexColorModel(numBits, paletteSize, r, g, b); BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm); Graphics2D g2d = img.createGraphics(); g2d.setColor(Color.white); g2d.fillRect(0, 0, w, h); g2d.setColor(Color.black); g2d.drawLine(0, 0, w, h); g2d.drawLine(0, h, w, 0); return img; }
/**************************** RescaleOp support *****************************/ public static boolean isRescaleOpValid(RescaleOp rop, BufferedImage srcImg) { int numFactors = rop.getNumFactors(); ColorModel srcCM = srcImg.getColorModel(); if (srcCM instanceof IndexColorModel) { throw new IllegalArgumentException("Rescaling cannot be "+ "performed on an indexed image"); } if (numFactors != 1 && numFactors != srcCM.getNumColorComponents() && numFactors != srcCM.getNumComponents()) { throw new IllegalArgumentException("Number of scaling constants "+ "does not equal the number of"+ " of color or color/alpha "+ " components"); } int csType = srcCM.getColorSpace().getType(); if (csType != ColorSpace.TYPE_RGB && csType != ColorSpace.TYPE_GRAY) { // Not prepared to deal with other color spaces return false; } if (numFactors == 2 || numFactors > 4) { // Not really prepared to handle this at the native level, so... return false; } return true; }
private static Image createTestImage() { byte[] r = new byte[]{(byte)0x00, (byte)0x80, (byte)0xff, (byte)0xff}; byte[] g = new byte[]{(byte)0x00, (byte)0x80, (byte)0xff, (byte)0x00}; byte[] b = new byte[]{(byte)0x00, (byte)0x80, (byte)0xff, (byte)0x00}; IndexColorModel icm = new IndexColorModel(2, 4, r, g, b, 3); BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED, icm); return img; }