1. /*
  2. * @(#)LookupOp.java 1.48 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. import java.awt.color.ColorSpace;
  9. import java.awt.geom.Rectangle2D;
  10. import java.awt.Rectangle;
  11. import java.awt.RenderingHints;
  12. import java.awt.geom.Point2D;
  13. import sun.awt.image.ImagingLib;
  14. /**
  15. * This class implements a lookup operation from the source
  16. * to the destination. The LookupTable object may contain a single array
  17. * or multiple arrays, subject to the restrictions below.
  18. * <p>
  19. * For Rasters, the lookup operates on bands. The number of
  20. * lookup arrays may be one, in which case the same array is
  21. * applied to all bands, or it must equal the number of Source
  22. * Raster bands.
  23. * <p>
  24. * For BufferedImages, the lookup operates on color and alpha components.
  25. * The number of lookup arrays may be one, in which case the
  26. * same array is applied to all color (but not alpha) components.
  27. * Otherwise, the number of lookup arrays may
  28. * equal the number of Source color components, in which case no
  29. * lookup of the alpha component (if present) is performed.
  30. * If neither of these cases apply, the number of lookup arrays
  31. * must equal the number of Source color components plus alpha components,
  32. * in which case lookup is performed for all color and alpha components.
  33. * This allows non-uniform rescaling of multi-band BufferedImages.
  34. * <p>
  35. * BufferedImage sources with premultiplied alpha data are treated in the same
  36. * manner as non-premultiplied images for purposes of the lookup. That is,
  37. * the lookup is done per band on the raw data of the BufferedImage source
  38. * without regard to whether the data is premultiplied. If a color conversion
  39. * is required to the destination ColorModel, the premultiplied state of
  40. * both source and destination will be taken into account for this step.
  41. * <p>
  42. * Images with an IndexColorModel cannot be used.
  43. * <p>
  44. * If a RenderingHints object is specified in the constructor, the
  45. * color rendering hint and the dithering hint may be used when color
  46. * conversion is required.
  47. * <p>
  48. * This class allows the Source to be the same as the Destination.
  49. *
  50. * @version 10 Feb 1997
  51. * @see LookupTable
  52. * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
  53. * @see java.awt.RenderingHints#KEY_DITHERING
  54. */
  55. public class LookupOp implements BufferedImageOp, RasterOp {
  56. private LookupTable ltable;
  57. private int numComponents;
  58. RenderingHints hints;
  59. /**
  60. * Constructs a <code>LookupOp</code> object given the lookup
  61. * table and a <code>RenderingHints</code> object, which might
  62. * be <code>null</code>.
  63. * @param lookup the specified <code>LookupTable</code>
  64. * @param hints the specified <code>RenderingHints</code>,
  65. * or <code>null</code>
  66. */
  67. public LookupOp(LookupTable lookup, RenderingHints hints) {
  68. this.ltable = lookup;
  69. this.hints = hints;
  70. numComponents = ltable.getNumComponents();
  71. }
  72. /**
  73. * Returns the <code>LookupTable</code>.
  74. * @return the <code>LookupTable</code> of this
  75. * <code>LookupOp</code>.
  76. */
  77. public final LookupTable getTable() {
  78. return ltable;
  79. }
  80. /**
  81. * Performs a lookup operation on a <code>BufferedImage</code>.
  82. * If the color model in the source image is not the same as that
  83. * in the destination image, the pixels will be converted
  84. * in the destination. If the destination image is <code>null</code>,
  85. * a <code>BufferedImage</code> will be created with an appropriate
  86. * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
  87. * might be thrown if the number of arrays in the
  88. * <code>LookupTable</code> does not meet the restrictions
  89. * stated in the class comment above, or if the source image
  90. * has an <code>IndexColorModel</code>.
  91. * @param src the <code>BufferedImage</code> to be filtered
  92. * @param dst the <code>BufferedImage</code> in which to
  93. * store the results of the filter operation
  94. * @return the filtered <code>BufferedImage</code>.
  95. * @throws IllegalArgumentException if the number of arrays in the
  96. * <code>LookupTable</code> does not meet the restrictions
  97. * described in the class comments, or if the source image
  98. * has an <code>IndexColorModel</code>.
  99. */
  100. public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
  101. ColorModel srcCM = src.getColorModel();
  102. int numBands = srcCM.getNumColorComponents();
  103. ColorModel dstCM;
  104. if (srcCM instanceof IndexColorModel) {
  105. throw new
  106. IllegalArgumentException("LookupOp cannot be "+
  107. "performed on an indexed image");
  108. }
  109. int numComponents = ltable.getNumComponents();
  110. if (numComponents != 1 &&
  111. numComponents != srcCM.getNumComponents() &&
  112. numComponents != srcCM.getNumColorComponents())
  113. {
  114. throw new IllegalArgumentException("Number of arrays in the "+
  115. " lookup table ("+
  116. numComponents+
  117. " is not compatible with the "+
  118. " src image: "+src);
  119. }
  120. boolean needToConvert = false;
  121. int width = src.getWidth();
  122. int height = src.getHeight();
  123. if (dst == null) {
  124. dst = createCompatibleDestImage(src, null);
  125. dstCM = srcCM;
  126. }
  127. else {
  128. if (width != dst.getWidth()) {
  129. throw new
  130. IllegalArgumentException("Src width ("+width+
  131. ") not equal to dst width ("+
  132. dst.getWidth()+")");
  133. }
  134. if (height != dst.getHeight()) {
  135. throw new
  136. IllegalArgumentException("Src height ("+height+
  137. ") not equal to dst height ("+
  138. dst.getHeight()+")");
  139. }
  140. dstCM = dst.getColorModel();
  141. if (srcCM.getColorSpace().getType() !=
  142. dstCM.getColorSpace().getType())
  143. {
  144. needToConvert = true;
  145. dst = createCompatibleDestImage(src, null);
  146. }
  147. }
  148. BufferedImage origDst = dst;
  149. if (ImagingLib.filter(this, src, dst) == null) {
  150. // Do it the slow way
  151. WritableRaster srcRaster = src.getRaster();
  152. WritableRaster dstRaster = dst.getRaster();
  153. if (srcCM.hasAlpha()) {
  154. if (numBands-1 == numComponents || numComponents == 1) {
  155. int minx = srcRaster.getMinX();
  156. int miny = srcRaster.getMinY();
  157. int[] bands = new int[numBands-1];
  158. for (int i=0; i < numBands-1; i++) {
  159. bands[i] = i;
  160. }
  161. srcRaster =
  162. srcRaster.createWritableChild(minx, miny,
  163. srcRaster.getWidth(),
  164. srcRaster.getHeight(),
  165. minx, miny,
  166. bands);
  167. }
  168. }
  169. if (dstCM.hasAlpha()) {
  170. int dstNumBands = dstRaster.getNumBands();
  171. if (dstNumBands-1 == numComponents || numComponents == 1) {
  172. int minx = dstRaster.getMinX();
  173. int miny = dstRaster.getMinY();
  174. int[] bands = new int[numBands-1];
  175. for (int i=0; i < numBands-1; i++) {
  176. bands[i] = i;
  177. }
  178. dstRaster =
  179. dstRaster.createWritableChild(minx, miny,
  180. dstRaster.getWidth(),
  181. dstRaster.getHeight(),
  182. minx, miny,
  183. bands);
  184. }
  185. }
  186. filter(srcRaster, dstRaster);
  187. }
  188. if (needToConvert) {
  189. // ColorModels are not the same
  190. ColorConvertOp ccop = new ColorConvertOp(hints);
  191. ccop.filter(dst, origDst);
  192. }
  193. return origDst;
  194. }
  195. /**
  196. * Performs a lookup operation on a <code>Raster</code>.
  197. * If the destination <code>Raster</code> is <code>null</code>,
  198. * a new <code>Raster</code> will be created.
  199. * The <code>IllegalArgumentException</code> might be thrown
  200. * if the source <code>Raster</code> and the destination
  201. * <code>Raster</code> do not have the same
  202. * number of bands or if the number of arrays in the
  203. * <code>LookupTable</code> does not meet the
  204. * restrictions stated in the class comment above.
  205. * @param src the source <code>Raster</code> to filter
  206. * @param dst the destination <code>WritableRaster</code> for the
  207. * filtered <code>src</code>
  208. * @return the filtered <code>WritableRaster</code>.
  209. * @throws IllegalArgumentException if the source and destinations
  210. * rasters do not have the same number of bands, or the
  211. * number of arrays in the <code>LookupTable</code> does
  212. * not meet the restrictions described in the class comments.
  213. *
  214. */
  215. public final WritableRaster filter (Raster src, WritableRaster dst) {
  216. int numBands = src.getNumBands();
  217. int dstLength = dst.getNumBands();
  218. int height = src.getHeight();
  219. int width = src.getWidth();
  220. int srcPix[] = new int[numBands];
  221. // Create a new destination Raster, if needed
  222. if (dst == null) {
  223. dst = createCompatibleDestRaster(src);
  224. }
  225. else if (height != dst.getHeight() || width != dst.getWidth()) {
  226. throw new
  227. IllegalArgumentException ("Width or height of Rasters do not "+
  228. "match");
  229. }
  230. dstLength = dst.getNumBands();
  231. if (numBands != dstLength) {
  232. throw new
  233. IllegalArgumentException ("Number of channels in the src ("
  234. + numBands +
  235. ") does not match number of channels"
  236. + " in the destination ("
  237. + dstLength + ")");
  238. }
  239. int numComponents = ltable.getNumComponents();
  240. if (numComponents != 1 && numComponents != src.getNumBands()) {
  241. throw new IllegalArgumentException("Number of arrays in the "+
  242. " lookup table ("+
  243. numComponents+
  244. " is not compatible with the "+
  245. " src Raster: "+src);
  246. }
  247. if (ImagingLib.filter(this, src, dst) != null) {
  248. return dst;
  249. }
  250. // Optimize for cases we know about
  251. if (ltable instanceof ByteLookupTable) {
  252. byteFilter ((ByteLookupTable) ltable, src, dst,
  253. width, height, numBands);
  254. }
  255. else if (ltable instanceof ShortLookupTable) {
  256. shortFilter ((ShortLookupTable) ltable, src, dst, width,
  257. height, numBands);
  258. }
  259. else {
  260. // Not one we recognize so do it slowly
  261. int sminX = src.getMinX();
  262. int sY = src.getMinY();
  263. int dminX = dst.getMinX();
  264. int dY = dst.getMinY();
  265. for (int y=0; y < height; y++, sY++, dY++) {
  266. int sX = sminX;
  267. int dX = dminX;
  268. for (int x=0; x < width; x++, sX++, dX++) {
  269. // Find data for all bands at this x,y position
  270. src.getPixel(sX, sY, srcPix);
  271. // Lookup the data for all bands at this x,y position
  272. ltable.lookupPixel(srcPix, srcPix);
  273. // Put it back for all bands
  274. dst.setPixel(dX, dY, srcPix);
  275. }
  276. }
  277. }
  278. return dst;
  279. }
  280. /**
  281. * Returns the bounding box of the filtered destination image. Since
  282. * this is not a geometric operation, the bounding box does not
  283. * change.
  284. * @param src the <code>BufferedImage</code> to be filtered
  285. * @return the bounds of the filtered definition image.
  286. */
  287. public final Rectangle2D getBounds2D (BufferedImage src) {
  288. return getBounds2D(src.getRaster());
  289. }
  290. /**
  291. * Returns the bounding box of the filtered destination Raster. Since
  292. * this is not a geometric operation, the bounding box does not
  293. * change.
  294. * @param src the <code>Raster</code> to be filtered
  295. * @return the bounds of the filtered definition <code>Raster</code>.
  296. */
  297. public final Rectangle2D getBounds2D (Raster src) {
  298. return src.getBounds();
  299. }
  300. /**
  301. * Creates a zeroed destination image with the correct size and number of
  302. * bands. If destCM is <code>null</code>, an appropriate
  303. * <code>ColorModel</code> will be used.
  304. * @param src Source image for the filter operation.
  305. * @param destCM the destination's <code>ColorModel</code>, which
  306. * can be <code>null</code>.
  307. * @return a filtered destination <code>BufferedImage</code>.
  308. */
  309. public BufferedImage createCompatibleDestImage (BufferedImage src,
  310. ColorModel destCM) {
  311. BufferedImage image;
  312. int w = src.getWidth();
  313. int h = src.getHeight();
  314. int transferType = DataBuffer.TYPE_BYTE;
  315. if (destCM == null) {
  316. ColorModel cm = src.getColorModel();
  317. Raster raster = src.getRaster();
  318. if (cm instanceof ComponentColorModel) {
  319. DataBuffer db = raster.getDataBuffer();
  320. boolean hasAlpha = cm.hasAlpha();
  321. boolean isPre = cm.isAlphaPremultiplied();
  322. int trans = cm.getTransparency();
  323. int[] nbits = null;
  324. if (ltable instanceof ByteLookupTable) {
  325. if (db.getDataType() == db.TYPE_USHORT) {
  326. // Dst raster should be of type byte
  327. if (hasAlpha) {
  328. nbits = new int[2];
  329. if (trans == cm.BITMASK) {
  330. nbits[1] = 1;
  331. }
  332. else {
  333. nbits[1] = 8;
  334. }
  335. }
  336. else {
  337. nbits = new int[1];
  338. }
  339. nbits[0] = 8;
  340. }
  341. // For byte, no need to change the cm
  342. }
  343. else if (ltable instanceof ShortLookupTable) {
  344. transferType = DataBuffer.TYPE_USHORT;
  345. if (db.getDataType() == db.TYPE_BYTE) {
  346. if (hasAlpha) {
  347. nbits = new int[2];
  348. if (trans == cm.BITMASK) {
  349. nbits[1] = 1;
  350. }
  351. else {
  352. nbits[1] = 16;
  353. }
  354. }
  355. else {
  356. nbits = new int[1];
  357. }
  358. nbits[0] = 16;
  359. }
  360. }
  361. if (nbits != null) {
  362. cm = new ComponentColorModel(cm.getColorSpace(),
  363. nbits, hasAlpha, isPre,
  364. trans, transferType);
  365. }
  366. }
  367. image = new BufferedImage(cm,
  368. cm.createCompatibleWritableRaster(w, h),
  369. cm.isAlphaPremultiplied(),
  370. null);
  371. }
  372. else {
  373. image = new BufferedImage(destCM,
  374. destCM.createCompatibleWritableRaster(w,
  375. h),
  376. destCM.isAlphaPremultiplied(),
  377. null);
  378. }
  379. return image;
  380. }
  381. /**
  382. * Creates a zeroed-destination <code>Raster</code> with the
  383. * correct size and number of bands, given this source.
  384. * @param src the <code>Raster</code> to be transformed
  385. * @return the zeroed-destination <code>Raster</code>.
  386. */
  387. public WritableRaster createCompatibleDestRaster (Raster src) {
  388. return src.createCompatibleWritableRaster();
  389. }
  390. /**
  391. * Returns the location of the destination point given a
  392. * point in the source. If <code>dstPt</code> is not
  393. * <code>null</code>, it will be used to hold the return value.
  394. * Since this is not a geometric operation, the <code>srcPt</code>
  395. * will equal the <code>dstPt</code>.
  396. * @param srcPt a <code>Point2D</code> that represents a point
  397. * in the source image
  398. * @param dstPt a <code>Point2D</code>that represents the location
  399. * in the destination
  400. * @return the <code>Point2D</code> in the destination that
  401. * corresponds to the specified point in the source.
  402. */
  403. public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
  404. if (dstPt == null) {
  405. dstPt = new Point2D.Float();
  406. }
  407. dstPt.setLocation(srcPt.getX(), srcPt.getY());
  408. return dstPt;
  409. }
  410. /**
  411. * Returns the rendering hints for this op.
  412. * @return the <code>RenderingHints</code> object associated
  413. * with this op.
  414. */
  415. public final RenderingHints getRenderingHints() {
  416. return hints;
  417. }
  418. private final void byteFilter(ByteLookupTable lookup, Raster src,
  419. WritableRaster dst,
  420. int width, int height, int numBands) {
  421. int[] srcPix = null;
  422. // Find the ref to the table and the offset
  423. byte[][] table = lookup.getTable();
  424. int offset = lookup.getOffset();
  425. int tidx;
  426. int step=1;
  427. // Check if it is one lookup applied to all bands
  428. if (table.length == 1) {
  429. step=0;
  430. }
  431. int x;
  432. int y;
  433. int band;
  434. int len = table[0].length;
  435. // Loop through the data
  436. for ( y=0; y < height; y++) {
  437. tidx = 0;
  438. for ( band=0; band < numBands; band++, tidx+=step) {
  439. // Find data for this band, scanline
  440. srcPix = src.getSamples(0, y, width, 1, band, srcPix);
  441. for ( x=0; x < width; x++) {
  442. int index = srcPix[x]-offset;
  443. if (index < 0 || index > len) {
  444. throw new
  445. IllegalArgumentException("index ("+index+
  446. "(out of range: "+
  447. " srcPix["+x+
  448. "]="+ srcPix[x]+
  449. " offset="+ offset);
  450. }
  451. // Do the lookup
  452. srcPix[x] = table[tidx][index];
  453. }
  454. // Put it back
  455. dst.setSamples(0, y, width, 1, band, srcPix);
  456. }
  457. }
  458. }
  459. private final void shortFilter(ShortLookupTable lookup, Raster src,
  460. WritableRaster dst,
  461. int width, int height, int numBands) {
  462. int band;
  463. int[] srcPix = null;
  464. // Find the ref to the table and the offset
  465. short[][] table = lookup.getTable();
  466. int offset = lookup.getOffset();
  467. int tidx;
  468. int step=1;
  469. // Check if it is one lookup applied to all bands
  470. if (table.length == 1) {
  471. step=0;
  472. }
  473. int x = 0;
  474. int y = 0;
  475. int index;
  476. int maxShort = (1<<16)-1;
  477. // Loop through the data
  478. for (y=0; y < height; y++) {
  479. tidx = 0;
  480. for ( band=0; band < numBands; band++, tidx+=step) {
  481. // Find data for this band, scanline
  482. srcPix = src.getSamples(0, y, width, 1, band, srcPix);
  483. for ( x=0; x < width; x++) {
  484. index = srcPix[x]-offset;
  485. if (index < 0 || index > maxShort) {
  486. throw new
  487. IllegalArgumentException("index out of range "+
  488. index+" x is "+x+
  489. "srcPix[x]="+srcPix[x]
  490. +" offset="+ offset);
  491. }
  492. // Do the lookup
  493. srcPix[x] = table[tidx][index];
  494. }
  495. // Put it back
  496. dst.setSamples(0, y, width, 1, band, srcPix);
  497. }
  498. }
  499. }
  500. }