53 protected Raster srcRaster; 54 55 protected int requiredSize; 56 57 protected ColorNode root; 58 59 protected int numNodes; 60 protected int maxNodes; 61 protected int currLevel; 62 protected int currSize; 63 64 protected ColorNode[] reduceList; 65 protected ColorNode[] palette; 66 67 protected int transparency; 68 protected ColorNode transColor; 69 70 71 /** 72 * Creates an image representing given image 73 * <code>src</code> using <code>IndexColorModel</code>. 74 * 75 * Lossless conversion is not always possible (e.g. if number 76 * of colors in the given image exceeds maximum palette size). 77 * Result image then is an approximation constructed by octree 78 * quantization method. 79 * 80 * @exception IllegalArgumentException if <code>src</code> is 81 * <code>null</code>. 82 * 83 * @exception UnsupportedOperationException if implemented method 84 * is unable to create approximation of <code>src</code> 85 * and <code>canCreatePalette</code> returns <code>false</code>. 86 * 87 * @see createIndexColorModel 88 * 89 * @see canCreatePalette 90 * 91 */ 92 public static RenderedImage createIndexedImage(RenderedImage src) { 93 PaletteBuilder pb = new PaletteBuilder(src); 94 pb.buildPalette(); 95 return pb.getIndexedImage(); 96 } 97 98 /** 99 * Creates an palette representing colors from given image 100 * <code>img</code>. If number of colors in the given image exceeds 101 * maximum palette size closest colors would be merged. 102 * 103 * @exception IllegalArgumentException if <code>img</code> is 104 * <code>null</code>. 105 * 106 * @exception UnsupportedOperationException if implemented method 107 * is unable to create approximation of <code>img</code> 108 * and <code>canCreatePalette</code> returns <code>false</code>. 109 * 110 * @see createIndexedImage 111 * 112 * @see canCreatePalette 113 * 114 */ 115 public static IndexColorModel createIndexColorModel(RenderedImage img) { 116 PaletteBuilder pb = new PaletteBuilder(img); 117 pb.buildPalette(); 118 return pb.getIndexColorModel(); 119 } 120 121 /** 122 * Returns <code>true</code> if PaletteBuilder is able to create 123 * palette for given image type. 124 * 125 * @param type an instance of <code>ImageTypeSpecifier</code> to be 126 * indexed. 127 * 128 * @return <code>true</code> if the <code>PaletteBuilder</code> 129 * is likely to be able to create palette for this image type. 130 * 131 * @exception IllegalArgumentException if <code>type</code> 132 * is <code>null</code>. 133 */ 134 public static boolean canCreatePalette(ImageTypeSpecifier type) { 135 if (type == null) { 136 throw new IllegalArgumentException("type == null"); 137 } 138 return true; 139 } 140 141 /** 142 * Returns <code>true</code> if PaletteBuilder is able to create 143 * palette for given rendered image. 144 * 145 * @param image an instance of <code>RenderedImage</code> to be 146 * indexed. 147 * 148 * @return <code>true</code> if the <code>PaletteBuilder</code> 149 * is likely to be able to create palette for this image type. 150 * 151 * @exception IllegalArgumentException if <code>image</code> 152 * is <code>null</code>. 153 */ 154 public static boolean canCreatePalette(RenderedImage image) { 155 if (image == null) { 156 throw new IllegalArgumentException("image == null"); 157 } 158 ImageTypeSpecifier type = new ImageTypeSpecifier(image); 159 return canCreatePalette(type); 160 } 161 162 protected RenderedImage getIndexedImage() { 163 IndexColorModel icm = getIndexColorModel(); 164 165 BufferedImage dst = 166 new BufferedImage(src.getWidth(), src.getHeight(), 167 BufferedImage.TYPE_BYTE_INDEXED, icm); 168 169 WritableRaster wr = dst.getRaster(); 170 for (int y =0; y < dst.getHeight(); y++) { 171 for (int x = 0; x < dst.getWidth(); x++) { 172 Color aColor = getSrcColor(x,y); | 53 protected Raster srcRaster; 54 55 protected int requiredSize; 56 57 protected ColorNode root; 58 59 protected int numNodes; 60 protected int maxNodes; 61 protected int currLevel; 62 protected int currSize; 63 64 protected ColorNode[] reduceList; 65 protected ColorNode[] palette; 66 67 protected int transparency; 68 protected ColorNode transColor; 69 70 71 /** 72 * Creates an image representing given image 73 * {@code src} using {@code IndexColorModel}. 74 * 75 * Lossless conversion is not always possible (e.g. if number 76 * of colors in the given image exceeds maximum palette size). 77 * Result image then is an approximation constructed by octree 78 * quantization method. 79 * 80 * @exception IllegalArgumentException if {@code src} is 81 * {@code null}. 82 * 83 * @exception UnsupportedOperationException if implemented method 84 * is unable to create approximation of {@code src} 85 * and {@code canCreatePalette} returns {@code false}. 86 * 87 * @see createIndexColorModel 88 * 89 * @see canCreatePalette 90 * 91 */ 92 public static RenderedImage createIndexedImage(RenderedImage src) { 93 PaletteBuilder pb = new PaletteBuilder(src); 94 pb.buildPalette(); 95 return pb.getIndexedImage(); 96 } 97 98 /** 99 * Creates an palette representing colors from given image 100 * {@code img}. If number of colors in the given image exceeds 101 * maximum palette size closest colors would be merged. 102 * 103 * @exception IllegalArgumentException if {@code img} is 104 * {@code null}. 105 * 106 * @exception UnsupportedOperationException if implemented method 107 * is unable to create approximation of {@code img} 108 * and {@code canCreatePalette} returns {@code false}. 109 * 110 * @see createIndexedImage 111 * 112 * @see canCreatePalette 113 * 114 */ 115 public static IndexColorModel createIndexColorModel(RenderedImage img) { 116 PaletteBuilder pb = new PaletteBuilder(img); 117 pb.buildPalette(); 118 return pb.getIndexColorModel(); 119 } 120 121 /** 122 * Returns {@code true} if PaletteBuilder is able to create 123 * palette for given image type. 124 * 125 * @param type an instance of {@code ImageTypeSpecifier} to be 126 * indexed. 127 * 128 * @return {@code true} if the {@code PaletteBuilder} 129 * is likely to be able to create palette for this image type. 130 * 131 * @exception IllegalArgumentException if {@code type} 132 * is {@code null}. 133 */ 134 public static boolean canCreatePalette(ImageTypeSpecifier type) { 135 if (type == null) { 136 throw new IllegalArgumentException("type == null"); 137 } 138 return true; 139 } 140 141 /** 142 * Returns {@code true} if PaletteBuilder is able to create 143 * palette for given rendered image. 144 * 145 * @param image an instance of {@code RenderedImage} to be 146 * indexed. 147 * 148 * @return {@code true} if the {@code PaletteBuilder} 149 * is likely to be able to create palette for this image type. 150 * 151 * @exception IllegalArgumentException if {@code image} 152 * is {@code null}. 153 */ 154 public static boolean canCreatePalette(RenderedImage image) { 155 if (image == null) { 156 throw new IllegalArgumentException("image == null"); 157 } 158 ImageTypeSpecifier type = new ImageTypeSpecifier(image); 159 return canCreatePalette(type); 160 } 161 162 protected RenderedImage getIndexedImage() { 163 IndexColorModel icm = getIndexColorModel(); 164 165 BufferedImage dst = 166 new BufferedImage(src.getWidth(), src.getHeight(), 167 BufferedImage.TYPE_BYTE_INDEXED, icm); 168 169 WritableRaster wr = dst.getRaster(); 170 for (int y =0; y < dst.getHeight(); y++) { 171 for (int x = 0; x < dst.getWidth(); x++) { 172 Color aColor = getSrcColor(x,y); |