1. /*
  2. * @(#)LookupOp.java 1.44 01/11/29
  3. *
  4. * Copyright 2002 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 LookupOp object given the lookup table and
  61. * a RenderingHints object (which may be null).
  62. */
  63. public LookupOp(LookupTable lookup, RenderingHints hints) {
  64. this.ltable = lookup;
  65. this.hints = hints;
  66. numComponents = ltable.getNumComponents();
  67. }
  68. /**
  69. * Returns the LookupTable.
  70. */
  71. public final LookupTable getTable() {
  72. return ltable;
  73. }
  74. /**
  75. * Performs a lookup operation on a BufferedImage.
  76. * If the color model in the source image is not the same as that
  77. * in the destination image, the pixels will be converted
  78. * in the destination. If the destination image is null,
  79. * a BufferedImage will be created with an appropriate ColorModel.
  80. * An IllegalArgumentException may be thrown if the number of
  81. * arrays in the LookupTable does not meet the restrictions
  82. * stated in the class comment above, or if the source image
  83. * has an IndexColorModel.
  84. */
  85. public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
  86. ColorModel srcCM = src.getColorModel();
  87. int numBands = srcCM.getNumColorComponents();
  88. ColorModel dstCM;
  89. if (srcCM instanceof IndexColorModel) {
  90. throw new
  91. IllegalArgumentException("LookupOp cannot be "+
  92. "performed on an indexed image");
  93. }
  94. int numComponents = ltable.getNumComponents();
  95. if (numComponents != 1 &&
  96. numComponents != srcCM.getNumComponents() &&
  97. numComponents != srcCM.getNumColorComponents())
  98. {
  99. throw new IllegalArgumentException("Number of arrays in the "+
  100. " lookup table ("+
  101. numComponents+
  102. " is not compatible with the "+
  103. " src image: "+src);
  104. }
  105. boolean needToConvert = false;
  106. int width = src.getWidth();
  107. int height = src.getHeight();
  108. if (dst == null) {
  109. dst = createCompatibleDestImage(src, null);
  110. dstCM = srcCM;
  111. }
  112. else {
  113. if (width != dst.getWidth()) {
  114. throw new
  115. IllegalArgumentException("Src width ("+width+
  116. ") not equal to dst width ("+
  117. dst.getWidth()+")");
  118. }
  119. if (height != dst.getHeight()) {
  120. throw new
  121. IllegalArgumentException("Src height ("+height+
  122. ") not equal to dst height ("+
  123. dst.getHeight()+")");
  124. }
  125. dstCM = dst.getColorModel();
  126. if (srcCM.getColorSpace().getType() !=
  127. dstCM.getColorSpace().getType())
  128. {
  129. needToConvert = true;
  130. dst = createCompatibleDestImage(src, null);
  131. }
  132. }
  133. BufferedImage origDst = dst;
  134. if (ImagingLib.filter(this, src, dst) == null) {
  135. // Do it the slow way
  136. WritableRaster srcRaster = src.getRaster();
  137. WritableRaster dstRaster = dst.getRaster();
  138. if (srcCM.hasAlpha()) {
  139. if (numBands-1 == numComponents || numComponents == 1) {
  140. int minx = srcRaster.getMinX();
  141. int miny = srcRaster.getMinY();
  142. int[] bands = new int[numBands-1];
  143. for (int i=0; i < numBands-1; i++) {
  144. bands[i] = i;
  145. }
  146. srcRaster =
  147. srcRaster.createWritableChild(minx, miny,
  148. srcRaster.getWidth(),
  149. srcRaster.getHeight(),
  150. minx, miny,
  151. bands);
  152. }
  153. }
  154. if (dstCM.hasAlpha()) {
  155. int dstNumBands = dstRaster.getNumBands();
  156. if (dstNumBands-1 == numComponents || numComponents == 1) {
  157. int minx = dstRaster.getMinX();
  158. int miny = dstRaster.getMinY();
  159. int[] bands = new int[numBands-1];
  160. for (int i=0; i < numBands-1; i++) {
  161. bands[i] = i;
  162. }
  163. dstRaster =
  164. dstRaster.createWritableChild(minx, miny,
  165. dstRaster.getWidth(),
  166. dstRaster.getHeight(),
  167. minx, miny,
  168. bands);
  169. }
  170. }
  171. filter(srcRaster, dstRaster);
  172. }
  173. if (needToConvert) {
  174. // ColorModels are not the same
  175. ColorConvertOp ccop = new ColorConvertOp(hints);
  176. ccop.filter(dst, origDst);
  177. }
  178. return origDst;
  179. }
  180. /**
  181. * Performs a lookup operation on a Raster. If the destination Raster is
  182. * null, a new Raster will be created.
  183. * The IllegalArgumentException may be thrown if the source and
  184. * destination Rasters have different numbers of bands or if
  185. * the number of arrays in the LookupTable does not meet the
  186. * restrictions stated in the class comment above.
  187. */
  188. public final WritableRaster filter (Raster src, WritableRaster dst) {
  189. int numBands = src.getNumBands();
  190. int dstLength = dst.getNumBands();
  191. int height = src.getHeight();
  192. int width = src.getWidth();
  193. int srcPix[] = new int[numBands];
  194. // Create a new destination Raster, if needed
  195. if (dst == null) {
  196. dst = createCompatibleDestRaster(src);
  197. }
  198. else if (height != dst.getHeight() || width != dst.getWidth()) {
  199. throw new
  200. IllegalArgumentException ("Width or height of Rasters do not "+
  201. "match");
  202. }
  203. dstLength = dst.getNumBands();
  204. if (numBands != dstLength) {
  205. throw new
  206. IllegalArgumentException ("Number of channels in the src ("
  207. + numBands +
  208. ") does not match number of channels"
  209. + " in the destination ("
  210. + dstLength + ")");
  211. }
  212. int numComponents = ltable.getNumComponents();
  213. if (numComponents != 1 && numComponents != src.getNumBands()) {
  214. throw new IllegalArgumentException("Number of arrays in the "+
  215. " lookup table ("+
  216. numComponents+
  217. " is not compatible with the "+
  218. " src Raster: "+src);
  219. }
  220. if (ImagingLib.filter(this, src, dst) != null) {
  221. return dst;
  222. }
  223. // Optimize for cases we know about
  224. if (ltable instanceof ByteLookupTable) {
  225. byteFilter ((ByteLookupTable) ltable, src, dst,
  226. width, height, numBands);
  227. }
  228. else if (ltable instanceof ShortLookupTable) {
  229. shortFilter ((ShortLookupTable) ltable, src, dst, width,
  230. height, numBands);
  231. }
  232. else {
  233. // Not one we recognize so do it slowly
  234. int sminX = src.getMinX();
  235. int sY = src.getMinY();
  236. int dminX = dst.getMinX();
  237. int dY = dst.getMinY();
  238. for (int y=0; y < height; y++, sY++, dY++) {
  239. int sX = sminX;
  240. int dX = dminX;
  241. for (int x=0; x < width; x++, sX++, dX++) {
  242. // Find data for all bands at this x,y position
  243. src.getPixel(sX, sY, srcPix);
  244. // Lookup the data for all bands at this x,y position
  245. ltable.lookupPixel(srcPix, srcPix);
  246. // Put it back for all bands
  247. dst.setPixel(dX, dY, srcPix);
  248. }
  249. }
  250. }
  251. return dst;
  252. }
  253. /**
  254. * Returns the bounding box of the filtered destination image. Since
  255. * this is not a geometric operation, the bounding box does not
  256. * change.
  257. */
  258. public final Rectangle2D getBounds2D (BufferedImage src) {
  259. return getBounds2D(src.getRaster());
  260. }
  261. /**
  262. * Returns the bounding box of the filtered destination Raster. Since
  263. * this is not a geometric operation, the bounding box does not
  264. * change.
  265. */
  266. public final Rectangle2D getBounds2D (Raster src) {
  267. return src.getBounds();
  268. }
  269. /**
  270. * Creates a zeroed destination image with the correct size and number of
  271. * bands. If destCM is null, an appropriate ColorModel will be used.
  272. * @param src Source image for the filter operation.
  273. * @param destCM ColorModel of the destination. Can be null.
  274. */
  275. public BufferedImage createCompatibleDestImage (BufferedImage src,
  276. ColorModel destCM) {
  277. BufferedImage image;
  278. int w = src.getWidth();
  279. int h = src.getHeight();
  280. int transferType = DataBuffer.TYPE_BYTE;
  281. if (destCM == null) {
  282. ColorModel cm = src.getColorModel();
  283. Raster raster = src.getRaster();
  284. if (cm instanceof ComponentColorModel) {
  285. DataBuffer db = raster.getDataBuffer();
  286. boolean hasAlpha = cm.hasAlpha();
  287. boolean isPre = cm.isAlphaPremultiplied();
  288. int trans = cm.getTransparency();
  289. int[] nbits = null;
  290. if (ltable instanceof ByteLookupTable) {
  291. if (db.getDataType() == db.TYPE_USHORT) {
  292. // Dst raster should be of type byte
  293. if (hasAlpha) {
  294. nbits = new int[2];
  295. if (trans == cm.BITMASK) {
  296. nbits[1] = 1;
  297. }
  298. else {
  299. nbits[1] = 8;
  300. }
  301. }
  302. else {
  303. nbits = new int[1];
  304. }
  305. nbits[0] = 8;
  306. }
  307. // For byte, no need to change the cm
  308. }
  309. else if (ltable instanceof ShortLookupTable) {
  310. transferType = DataBuffer.TYPE_USHORT;
  311. if (db.getDataType() == db.TYPE_BYTE) {
  312. if (hasAlpha) {
  313. nbits = new int[2];
  314. if (trans == cm.BITMASK) {
  315. nbits[1] = 1;
  316. }
  317. else {
  318. nbits[1] = 16;
  319. }
  320. }
  321. else {
  322. nbits = new int[1];
  323. }
  324. nbits[0] = 16;
  325. }
  326. }
  327. if (nbits != null) {
  328. cm = new ComponentColorModel(cm.getColorSpace(),
  329. nbits, hasAlpha, isPre,
  330. trans, transferType);
  331. }
  332. }
  333. image = new BufferedImage(cm,
  334. cm.createCompatibleWritableRaster(w, h),
  335. cm.isAlphaPremultiplied(),
  336. null);
  337. }
  338. else {
  339. image = new BufferedImage(destCM,
  340. destCM.createCompatibleWritableRaster(w,
  341. h),
  342. destCM.isAlphaPremultiplied(),
  343. null);
  344. }
  345. return image;
  346. }
  347. /**
  348. * Creates a zeroed destination Raster with the correct size and number of
  349. * bands, given this source.
  350. */
  351. public WritableRaster createCompatibleDestRaster (Raster src) {
  352. return src.createCompatibleWritableRaster();
  353. }
  354. /**
  355. * Returns the location of the destination point given a
  356. * point in the source. If dstPt is non-null, it will
  357. * be used to hold the return value. Since this is not a geometric
  358. * operation, the srcPt will equal the dstPt.
  359. */
  360. public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
  361. if (dstPt == null) {
  362. dstPt = new Point2D.Float();
  363. }
  364. dstPt.setLocation(srcPt.getX(), srcPt.getY());
  365. return dstPt;
  366. }
  367. /**
  368. * Returns the rendering hints for this op.
  369. */
  370. public final RenderingHints getRenderingHints() {
  371. return hints;
  372. }
  373. private final void byteFilter(ByteLookupTable lookup, Raster src,
  374. WritableRaster dst,
  375. int width, int height, int numBands) {
  376. int[] srcPix = null;
  377. // Find the ref to the table and the offset
  378. byte[][] table = lookup.getTable();
  379. int offset = lookup.getOffset();
  380. int tidx;
  381. int step=1;
  382. // Check if it is one lookup applied to all bands
  383. if (table.length == 1) {
  384. step=0;
  385. }
  386. int x;
  387. int y;
  388. int band;
  389. int len = table[0].length;
  390. // Loop through the data
  391. for ( y=0; y < height; y++) {
  392. tidx = 0;
  393. for ( band=0; band < numBands; band++, tidx+=step) {
  394. // Find data for this band, scanline
  395. srcPix = src.getSamples(0, y, width, 1, band, srcPix);
  396. for ( x=0; x < width; x++) {
  397. int index = srcPix[x]-offset;
  398. if (index < 0 || index > len) {
  399. throw new
  400. IllegalArgumentException("index ("+index+
  401. "(out of range: "+
  402. " srcPix["+x+
  403. "]="+ srcPix[x]+
  404. " offset="+ offset);
  405. }
  406. // Do the lookup
  407. srcPix[x] = table[tidx][index];
  408. }
  409. // Put it back
  410. dst.setSamples(0, y, width, 1, band, srcPix);
  411. }
  412. }
  413. }
  414. private final void shortFilter(ShortLookupTable lookup, Raster src,
  415. WritableRaster dst,
  416. int width, int height, int numBands) {
  417. int band;
  418. int[] srcPix = null;
  419. // Find the ref to the table and the offset
  420. short[][] table = lookup.getTable();
  421. int offset = lookup.getOffset();
  422. int tidx;
  423. int step=1;
  424. // Check if it is one lookup applied to all bands
  425. if (table.length == 1) {
  426. step=0;
  427. }
  428. int x = 0;
  429. int y = 0;
  430. int index;
  431. int maxShort = (1<<16)-1;
  432. // Loop through the data
  433. for (y=0; y < height; y++) {
  434. tidx = 0;
  435. for ( band=0; band < numBands; band++, tidx+=step) {
  436. // Find data for this band, scanline
  437. srcPix = src.getSamples(0, y, width, 1, band, srcPix);
  438. for ( x=0; x < width; x++) {
  439. index = srcPix[x]-offset;
  440. if (index < 0 || index > maxShort) {
  441. throw new
  442. IllegalArgumentException("index out of range "+
  443. index+" x is "+x+
  444. "srcPix[x]="+srcPix[x]
  445. +" offset="+ offset);
  446. }
  447. // Do the lookup
  448. srcPix[x] = table[tidx][index];
  449. }
  450. // Put it back
  451. dst.setSamples(0, y, width, 1, band, srcPix);
  452. }
  453. }
  454. }
  455. }