/** * Creates an palette representing colors from given image * <code>img</code>. If number of colors in the given image exceeds * maximum palette size closest colors would be merged. * * @exception IllegalArgumentException if <code>img</code> is * <code>null</code>. * * @exception UnsupportedOperationException if implemented method * is unable to create approximation of <code>img</code> * and <code>canCreatePalette</code> returns <code>false</code>. * * @see #createIndexedImage(RenderedImage) * * @see #canCreatePalette(RenderedImage) * */ public static IndexColorModel createIndexColorModel(RenderedImage img) { PaletteBuilder pb = new PaletteBuilder(img); pb.buildPalette(); return pb.getIndexColorModel(); }
protected int findColorIndex(ColorNode aNode, Color aColor) { if (transparency != Transparency.OPAQUE && aColor.getAlpha() != 0xff) { return 0; // default transparnt pixel } if (aNode.isLeaf) { return aNode.paletteIndex; } else { int childIndex = getBranchIndex(aColor, aNode.level); return findColorIndex(aNode.children[childIndex], aColor); } }
protected int findPaletteEntry(ColorNode aNode, int index, byte[] red, byte[] green, byte[] blue) { if (aNode.isLeaf) { red[index] = (byte)(aNode.red/aNode.colorCount); green[index] = (byte)(aNode.green/aNode.colorCount); blue[index] = (byte)(aNode.blue/aNode.colorCount); aNode.paletteIndex = index; palette[index] = aNode; index++; } else { for (int i = 0; i < 8; i++) { if (aNode.children[i] != null) { index = findPaletteEntry(aNode.children[i], index, red, green, blue); } } } return index; }
/** * Creates an image representing given image * <code>src</code> using <code>IndexColorModel<code>. * * Lossless conversion is not always possible (e.g. if number * of colors in the given image exceeds maximum palette size). * Result image then is an approximation constructed by octree * quantization method. * * @exception IllegalArgumentException if <code>src</code> is * <code>null</code>. * * @exception UnsupportedOperationException if implemented method * is unable to create approximation of <code>src</code> * and <code>canCreatePalette</code> returns <code>false</code>. * * @see createIndexColorModel * * @see canCreatePalette * */ public static RenderedImage createIndexedImage(RenderedImage src) { PaletteBuilder pb = new PaletteBuilder(src); pb.buildPalette(); return pb.getIndexedImage(); }
protected RenderedImage getIndexedImage() { IndexColorModel icm = getIndexColorModel(); BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm); WritableRaster wr = dst.getRaster(); int minX = src.getMinX(); int minY = src.getMinY(); for (int y =0; y < dst.getHeight(); y++) { for (int x = 0; x < dst.getWidth(); x++) { Color aColor = getSrcColor(x + minX, y + minY); wr.setSample(x, y, 0, findColorIndex(root, aColor)); } } return dst; }
for (int x = 0; x < w; x++) { Color aColor = getSrcColor(w - x + minX - 1, h - y + minY - 1); aColor.getAlpha() != 0xff) transColor = insertNode(transColor, aColor, 0); } else { root = insertNode(root, aColor, 0); reduceTree();
int branchIndex = getBranchIndex(aColor, aLevel); if (aNode.children[branchIndex] == null) { aNode.childCount++; insertNode(aNode.children[branchIndex], aColor, aLevel + 1);
protected ColorNode freeTree(ColorNode aNode) { if (aNode == null) { return null; } for (int i = 0; i < 8; i++) { aNode.children[i] = freeTree(aNode.children[i]); } numNodes--; return null; }
/** * Returns <code>true</code> if PaletteBuilder is able to create * palette for given rendered image. * * @param image an instance of <code>RenderedImage</code> to be * indexed. * * @return <code>true</code> if the <code>PaletteBuilder</code> * is likely to be able to create palette for this image type. * * @exception IllegalArgumentException if <code>image</code> * is <code>null</code>. */ public static boolean canCreatePalette(RenderedImage image) { if (image == null) { throw new IllegalArgumentException("image == null"); } ImageTypeSpecifier type = new ImageTypeSpecifier(image); return canCreatePalette(type); }
protected RenderedImage getIndexedImage() { IndexColorModel icm = getIndexColorModel(); BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm); WritableRaster wr = dst.getRaster(); int minX = src.getMinX(); int minY = src.getMinY(); for (int y =0; y < dst.getHeight(); y++) { for (int x = 0; x < dst.getWidth(); x++) { Color aColor = getSrcColor(x + minX, y + minY); wr.setSample(x, y, 0, findColorIndex(root, aColor)); } } return dst; }
/** * Creates an image representing given image * <code>src</code> using <code>IndexColorModel<code>. * * Lossless conversion is not always possible (e.g. if number * of colors in the given image exceeds maximum palette size). * Result image then is an approximation constructed by octree * quantization method. * * @exception IllegalArgumentException if <code>src</code> is * <code>null</code>. * * @exception UnsupportedOperationException if implemented method * is unable to create approximation of <code>src</code> * and <code>canCreatePalette</code> returns <code>false</code>. * * @see #createIndexColorModel(RenderedImage) * * @see #canCreatePalette(RenderedImage) * */ public static RenderedImage createIndexedImage(RenderedImage src) { PaletteBuilder pb = new PaletteBuilder(src); pb.buildPalette(); return pb.getIndexedImage(); }
for (int x = 0; x < w; x++) { Color aColor = getSrcColor(w - x + minX - 1, h - y + minY - 1); aColor.getAlpha() != 0xff) transColor = insertNode(transColor, aColor, 0); } else { root = insertNode(root, aColor, 0); reduceTree();
int branchIndex = getBranchIndex(aColor, aLevel); if (aNode.children[branchIndex] == null) { aNode.childCount++; insertNode(aNode.children[branchIndex], aColor, aLevel + 1);
protected ColorNode freeTree(ColorNode aNode) { if (aNode == null) { return null; } for (int i = 0; i < 8; i++) { aNode.children[i] = freeTree(aNode.children[i]); } numNodes--; return null; }
/** * Returns <code>true</code> if PaletteBuilder is able to create * palette for given rendered image. * * @param image an instance of <code>RenderedImage</code> to be * indexed. * * @return <code>true</code> if the <code>PaletteBuilder</code> * is likely to be able to create palette for this image type. * * @exception IllegalArgumentException if <code>image</code> * is <code>null</code>. */ public static boolean canCreatePalette(RenderedImage image) { if (image == null) { throw new IllegalArgumentException("image == null"); } ImageTypeSpecifier type = new ImageTypeSpecifier(image); return canCreatePalette(type); }
protected RenderedImage getIndexedImage() { IndexColorModel icm = getIndexColorModel(); BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, icm); WritableRaster wr = dst.getRaster(); int minX = src.getMinX(); int minY = src.getMinY(); for (int y =0; y < dst.getHeight(); y++) { for (int x = 0; x < dst.getWidth(); x++) { Color aColor = getSrcColor(x + minX, y + minY); wr.setSample(x, y, 0, findColorIndex(root, aColor)); } } return dst; }
/** * Creates an palette representing colors from given image * <code>img</code>. If number of colors in the given image exceeds * maximum palette size closest colors would be merged. * * @exception IllegalArgumentException if <code>img</code> is * <code>null</code>. * * @exception UnsupportedOperationException if implemented method * is unable to create approximation of <code>img</code> * and <code>canCreatePalette</code> returns <code>false</code>. * * @see createIndexedImage * * @see canCreatePalette * */ public static IndexColorModel createIndexColorModel(RenderedImage img) { PaletteBuilder pb = new PaletteBuilder(img); pb.buildPalette(); return pb.getIndexColorModel(); }
/** * Creates an image representing given image * <code>src</code> using <code>IndexColorModel<code>. * * Lossless conversion is not always possible (e.g. if number * of colors in the given image exceeds maximum palette size). * Result image then is an approximation constructed by octree * quantization method. * * @exception IllegalArgumentException if <code>src</code> is * <code>null</code>. * * @exception UnsupportedOperationException if implemented method * is unable to create approximation of <code>src</code> * and <code>canCreatePalette</code> returns <code>false</code>. * * @see createIndexColorModel * * @see canCreatePalette * */ public static RenderedImage createIndexedImage(RenderedImage src) { PaletteBuilder pb = new PaletteBuilder(src); pb.buildPalette(); return pb.getIndexedImage(); }
for (int x = 0; x < w; x++) { Color aColor = getSrcColor(w - x + minX - 1, h - y + minY - 1); aColor.getAlpha() != 0xff) transColor = insertNode(transColor, aColor, 0); } else { root = insertNode(root, aColor, 0); reduceTree();
protected int findColorIndex(ColorNode aNode, Color aColor) { if (transparency != Transparency.OPAQUE && aColor.getAlpha() != 0xff) { return 0; // default transparnt pixel } if (aNode.isLeaf) { return aNode.paletteIndex; } else { int childIndex = getBranchIndex(aColor, aNode.level); return findColorIndex(aNode.children[childIndex], aColor); } }