LookupOp.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* LookupOp.java -- Filter that converts each pixel using a lookup table.
  2. Copyright (C) 2004 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt.image;
  32. import java.awt.RenderingHints;
  33. import java.awt.geom.Point2D;
  34. import java.awt.geom.Rectangle2D;
  35. /**
  36. * LookupOp is a filter that converts each pixel using a lookup table.
  37. *
  38. * For filtering Rasters, the lookup table must have either one component
  39. * that is applied to all bands, or one component for every band in the
  40. * Rasters.
  41. *
  42. * For BufferedImages, the lookup table may apply to both color and alpha
  43. * components. If the lookup table contains one component, or if there are
  44. * the same number of components as color components in the source, the table
  45. * applies to all color components. Otherwise the table applies to all
  46. * components including alpha. Alpha premultiplication is ignored during the
  47. * lookup filtering.
  48. *
  49. * After filtering, if color conversion is necessary, the conversion happens,
  50. * taking alpha premultiplication into account.
  51. *
  52. * @author jlquinn
  53. */
  54. public class LookupOp implements BufferedImageOp, RasterOp
  55. {
  56. private LookupTable lut;
  57. private RenderingHints hints;
  58. /**
  59. * Construct a new LookupOp using the given LookupTable.
  60. *
  61. * @param lookup LookupTable to use.
  62. * @param hints Rendering hints (can be null).
  63. */
  64. public LookupOp(LookupTable lookup, RenderingHints hints)
  65. {
  66. lut = lookup;
  67. this.hints = hints;
  68. }
  69. /**
  70. * Converts the source image using the lookup table specified in the
  71. * constructor. The resulting image is stored in the destination image if one
  72. * is provided; otherwise a new BufferedImage is created and returned.
  73. *
  74. * The source image cannot use an IndexColorModel, and the destination image
  75. * (if one is provided) must have the same size.
  76. *
  77. * @param src The source image.
  78. * @param dst The destination image.
  79. * @throws IllegalArgumentException if the rasters and/or color spaces are
  80. * incompatible.
  81. * @throws ArrayIndexOutOfBoundsException if a pixel in the source is not
  82. * contained in the LookupTable.
  83. * @return The convolved image.
  84. */
  85. public final BufferedImage filter(BufferedImage src, BufferedImage dst)
  86. {
  87. if (src.getColorModel() instanceof IndexColorModel)
  88. throw new IllegalArgumentException("LookupOp.filter: IndexColorModel "
  89. + "not allowed");
  90. if (lut.getNumComponents() != 1
  91. && lut.getNumComponents() != src.getColorModel().getNumComponents()
  92. && lut.getNumComponents() != src.getColorModel().getNumColorComponents())
  93. throw new IllegalArgumentException("LookupOp.filter: Incompatible " +
  94. "lookup table and source image");
  95. if (dst == null)
  96. dst = createCompatibleDestImage(src, null);
  97. else if (src.getHeight() != dst.getHeight() || src.getWidth() != dst.getWidth())
  98. throw new IllegalArgumentException("Source and destination images are " +
  99. "different sizes.");
  100. // Set up for potential colormodel mismatch
  101. BufferedImage tgt;
  102. if (dst.getColorModel().equals(src.getColorModel()))
  103. tgt = dst;
  104. else
  105. tgt = createCompatibleDestImage(src, src.getColorModel());
  106. Raster sr = src.getRaster();
  107. WritableRaster dr = tgt.getRaster();
  108. if (src.getColorModel().hasAlpha() &&
  109. (lut.getNumComponents() == 1 ||
  110. lut.getNumComponents() == src.getColorModel().getNumColorComponents()))
  111. {
  112. // Need to ignore alpha for lookup
  113. int[] dbuf = new int[src.getColorModel().getNumComponents()];
  114. int tmpBands = src.getColorModel().getNumColorComponents();
  115. int[] tmp = new int[tmpBands];
  116. // Filter the pixels
  117. for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
  118. for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
  119. {
  120. // Filter only color components, but also copy alpha
  121. sr.getPixel(x, y, dbuf);
  122. System.arraycopy(dbuf, 0, tmp, 0, tmpBands);
  123. dr.setPixel(x, y, lut.lookupPixel(tmp, dbuf));
  124. /* The reference implementation does not use LookupTable.lookupPixel,
  125. * but rather it seems to copy the table into a native array. The
  126. * effect of this (a probable bug in their implementation) is that
  127. * an out-of-bounds lookup on a ByteLookupTable will *not* throw an
  128. * out of bounds exception, but will instead return random garbage.
  129. * A bad lookup on a ShortLookupTable, however, will throw an
  130. * exception.
  131. *
  132. * Instead of mimicing this behaviour, we always throw an
  133. * ArrayOutofBoundsException by virtue of using
  134. * LookupTable.lookupPixle.
  135. */
  136. }
  137. }
  138. else
  139. {
  140. // No alpha to ignore
  141. int[] dbuf = new int[src.getColorModel().getNumComponents()];
  142. // Filter the pixels
  143. for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
  144. for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
  145. dr.setPixel(x, y, lut.lookupPixel(sr.getPixel(x, y, dbuf), dbuf));
  146. }
  147. if (tgt != dst)
  148. new ColorConvertOp(hints).filter(tgt, dst);
  149. return dst;
  150. }
  151. /* (non-Javadoc)
  152. * @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
  153. */
  154. public final Rectangle2D getBounds2D(BufferedImage src)
  155. {
  156. return src.getRaster().getBounds();
  157. }
  158. /* (non-Javadoc)
  159. * @see java.awt.image.BufferedImageOp#createCompatibleDestImage(java.awt.image.BufferedImage, java.awt.image.ColorModel)
  160. */
  161. public BufferedImage createCompatibleDestImage(BufferedImage src,
  162. ColorModel dstCM)
  163. {
  164. if (dstCM != null)
  165. return new BufferedImage(dstCM,
  166. src.getRaster().createCompatibleWritableRaster(),
  167. src.isAlphaPremultiplied(), null);
  168. // This is a strange exception, done for compatibility with the reference
  169. // (as demonstrated by a mauve testcase)
  170. int imgType = src.getType();
  171. if (imgType == BufferedImage.TYPE_USHORT_GRAY)
  172. imgType = BufferedImage.TYPE_BYTE_GRAY;
  173. return new BufferedImage(src.getWidth(), src.getHeight(), imgType);
  174. }
  175. /**
  176. * Returns the corresponding destination point for a given source point.
  177. *
  178. * This Op will return the source point unchanged.
  179. *
  180. * @param src The source point.
  181. * @param dst The destination point.
  182. */
  183. public final Point2D getPoint2D(Point2D src, Point2D dst)
  184. {
  185. if (dst == null)
  186. return (Point2D) src.clone();
  187. dst.setLocation(src);
  188. return dst;
  189. }
  190. /**
  191. * Return the LookupTable for this op.
  192. *
  193. * @return The lookup table.
  194. */
  195. public final LookupTable getTable()
  196. {
  197. return lut;
  198. }
  199. /* (non-Javadoc)
  200. * @see java.awt.image.RasterOp#getRenderingHints()
  201. */
  202. public final RenderingHints getRenderingHints()
  203. {
  204. return hints;
  205. }
  206. /**
  207. * Filter a raster through a lookup table.
  208. *
  209. * Applies the lookup table for this Rasterop to each pixel of src and
  210. * puts the results in dest. If dest is null, a new Raster is created and
  211. * returned.
  212. *
  213. * @param src The source raster.
  214. * @param dest The destination raster.
  215. * @return The WritableRaster with the filtered pixels.
  216. * @throws IllegalArgumentException if lookup table has more than one
  217. * component but not the same as src and dest.
  218. * @throws ArrayIndexOutOfBoundsException if a pixel in the source is not
  219. * contained in the LookupTable.
  220. */
  221. public final WritableRaster filter(Raster src, WritableRaster dest)
  222. {
  223. if (dest == null)
  224. // Allocate a raster if needed
  225. dest = createCompatibleDestRaster(src);
  226. else
  227. if (src.getNumBands() != dest.getNumBands())
  228. throw new IllegalArgumentException("Source and destination rasters " +
  229. "are incompatible.");
  230. if (lut.getNumComponents() != 1
  231. && lut.getNumComponents() != src.getNumBands())
  232. throw new IllegalArgumentException("Lookup table is incompatible with " +
  233. "this raster.");
  234. // Allocate pixel storage.
  235. int[] tmp = new int[src.getNumBands()];
  236. // Filter the pixels
  237. for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
  238. for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
  239. dest.setPixel(x, y, lut.lookupPixel(src.getPixel(x, y, tmp), tmp));
  240. /* The reference implementation does not use LookupTable.lookupPixel,
  241. * but rather it seems to copy the table into a native array. The
  242. * effect of this (a probable bug in their implementation) is that
  243. * an out-of-bounds lookup on a ByteLookupTable will *not* throw an
  244. * out of bounds exception, but will instead return random garbage.
  245. * A bad lookup on a ShortLookupTable, however, will throw an
  246. * exception.
  247. *
  248. * Instead of mimicing this behaviour, we always throw an
  249. * ArrayOutofBoundsException by virtue of using
  250. * LookupTable.lookupPixle.
  251. */
  252. return dest;
  253. }
  254. /* (non-Javadoc)
  255. * @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
  256. */
  257. public final Rectangle2D getBounds2D(Raster src)
  258. {
  259. return src.getBounds();
  260. }
  261. /* (non-Javadoc)
  262. * @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
  263. */
  264. public WritableRaster createCompatibleDestRaster(Raster src)
  265. {
  266. return src.createCompatibleWritableRaster();
  267. }
  268. }