1. /*
  2. * @(#)AffineTransformOp.java 1.46 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.geom.AffineTransform;
  9. import java.awt.geom.Rectangle2D;
  10. import java.awt.geom.Point2D;
  11. import java.awt.GraphicsEnvironment;
  12. import java.awt.Rectangle;
  13. import java.awt.RenderingHints;
  14. import java.awt.Transparency;
  15. import sun.awt.image.ImagingLib;
  16. /**
  17. * This class uses an affine transform to perform a linear mapping from
  18. * 2D coordinates in the source image or <CODE>Raster</CODE> to 2D coordinates
  19. * in the destination image or <CODE>Raster</CODE>.
  20. * The type of interpolation that is used is specified through a constructor,
  21. * either by a <CODE>RenderingHints</CODE> object or by one of the integer
  22. * interpolation types defined in this class.
  23. * <p>
  24. * If a <CODE>RenderingHints</CODE> object is specified in the constructor, the
  25. * interpolation hint and the rendering quality hint are used to set
  26. * the interpolation type for this operation. The color rendering hint
  27. * and the dithering hint can be used when color conversion is required.
  28. * <p>
  29. * Note that the following constraints have to be met:
  30. * <ul>
  31. * <li>The source and destination must be different.
  32. * <li>For <CODE>Raster</CODE> objects, the number of bands in the source must
  33. * be equal to the number of bands in the destination.
  34. * </ul>
  35. * @see AffineTransform
  36. * @see BufferedImageFilter
  37. * @see java.awt.RenderingHints#KEY_INTERPOLATION
  38. * @see java.awt.RenderingHints#KEY_RENDERING
  39. * @see java.awt.RenderingHints#KEY_COLOR_RENDERING
  40. * @see java.awt.RenderingHints#KEY_DITHERING
  41. * @version 16 Apr 1998
  42. */
  43. public class AffineTransformOp implements BufferedImageOp, RasterOp {
  44. private AffineTransform xform;
  45. RenderingHints hints;
  46. /**
  47. * Nearest-neighbor interpolation type.
  48. */
  49. public static final int TYPE_NEAREST_NEIGHBOR = 1;
  50. /**
  51. * Bilinear interpolation type.
  52. */
  53. public static final int TYPE_BILINEAR = 2;
  54. /**
  55. * Bicubic interpolation type.
  56. */
  57. private static final int TYPE_BICUBIC = 3;
  58. int interpolationType = TYPE_NEAREST_NEIGHBOR;
  59. /**
  60. * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform.
  61. * The interpolation type is determined from the
  62. * <CODE>RenderingHints</CODE> object. If the interpolation hint is
  63. * defined, it will be used. Otherwise, if the rendering quality hint is
  64. * defined, the interpolation type is determined from its value. If no
  65. * hints are is specified (<CODE>hints</CODE> is null),
  66. * the interpolation type is {@link #TYPE_NEAREST_NEIGHBOR
  67. * TYPE_NEAREST_NEIGHBOR}.
  68. *
  69. * @param xform The <CODE>AffineTransform</CODE> to use for the operation.
  70. *
  71. * @param hints The <CODE>RenderingHints</CODE> object used to specify the
  72. * interpolation type for the operation.
  73. *
  74. * @see java.awt.RenderingHints#KEY_INTERPOLATION
  75. * @see java.awt.RenderingHints#KEY_RENDERING
  76. */
  77. public AffineTransformOp(AffineTransform xform, RenderingHints hints) {
  78. this.xform = (AffineTransform) xform.clone();
  79. this.hints = hints;
  80. if (hints != null) {
  81. Object value = hints.get(hints.KEY_INTERPOLATION);
  82. if (value == null) {
  83. value = hints.get(hints.KEY_RENDERING);
  84. if (value == hints.VALUE_RENDER_SPEED) {
  85. interpolationType = TYPE_NEAREST_NEIGHBOR;
  86. }
  87. else if (value == hints.VALUE_RENDER_QUALITY) {
  88. interpolationType = TYPE_BILINEAR;
  89. }
  90. }
  91. else if (value == hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
  92. interpolationType = TYPE_NEAREST_NEIGHBOR;
  93. }
  94. else if (value == hints.VALUE_INTERPOLATION_BILINEAR) {
  95. interpolationType = TYPE_BILINEAR;
  96. }
  97. else if (value == hints.VALUE_INTERPOLATION_BICUBIC) {
  98. interpolationType = TYPE_BICUBIC;
  99. }
  100. }
  101. else {
  102. interpolationType = TYPE_NEAREST_NEIGHBOR;
  103. }
  104. }
  105. /**
  106. * Constructs an <CODE>AffineTransformOp</CODE> given an affine transform
  107. * and the interpolation type.
  108. *
  109. * @param xform The <CODE>AffineTransform</CODE> to use for the operation.
  110. * @param interpolationType One of the integer
  111. * interpolation type constants defined by this class: {@link
  112. * #TYPE_NEAREST_NEIGHBOR TYPE_NEAREST_NEIGHBOR},
  113. * {@link #TYPE_BILINEAR TYPE_BILINEAR},
  114. */
  115. public AffineTransformOp(AffineTransform xform, int interpolationType) {
  116. this.xform = (AffineTransform)xform.clone();
  117. this.interpolationType = interpolationType;
  118. }
  119. /**
  120. * Returns the interpolation type used by this op.
  121. * @see #TYPE_NEAREST_NEIGHBOR
  122. * @see #TYPE_BILINEAR
  123. */
  124. public final int getInterpolationType() {
  125. return interpolationType;
  126. }
  127. /**
  128. * Transforms the source <CODE>BufferedImage</CODE> and stores the results
  129. * in the destination <CODE>BufferedImage</CODE>.
  130. * If the color models for the two images do not match, a color
  131. * conversion into the destination color model is performed.
  132. * If the destination image is null,
  133. * a <CODE>BufferedImage</CODE> is created with the source
  134. * <CODE>ColorModel</CODE>.
  135. * An <CODE>IllegalArgumentException</CODE> may be thrown if the source is
  136. * the same as the destination.
  137. *
  138. * @param src The <CODE>BufferedImage</CODE> to transform.
  139. * @param dst The <CODE>BufferedImage</CODE> in which to store the results
  140. * of the transformation.
  141. *
  142. * @return The filtered <CODE>BufferedImage</CODE>.
  143. * @throws ImagingOpException If the image cannot be transformed.
  144. */
  145. public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
  146. if (src == null) {
  147. throw new NullPointerException("src image is null");
  148. }
  149. if (src == dst) {
  150. throw new IllegalArgumentException("src image cannot be the "+
  151. "same as the dst image");
  152. }
  153. boolean needToConvert = false;
  154. ColorModel srcCM = src.getColorModel();
  155. ColorModel dstCM;
  156. BufferedImage origDst = dst;
  157. if (dst == null) {
  158. dst = createCompatibleDestImage(src, null);
  159. dstCM = srcCM;
  160. origDst = dst;
  161. }
  162. else {
  163. dstCM = dst.getColorModel();
  164. if (srcCM.getColorSpace().getType() !=
  165. dstCM.getColorSpace().getType())
  166. {
  167. int type = xform.getType();
  168. boolean needTrans = ((type&
  169. (xform.TYPE_MASK_ROTATION|
  170. xform.TYPE_GENERAL_TRANSFORM))
  171. != 0);
  172. if (! needTrans && type != xform.TYPE_TRANSLATION && type != xform.TYPE_IDENTITY)
  173. {
  174. double[] mtx = new double[4];
  175. xform.getMatrix(mtx);
  176. // Check out the matrix. A non-integral scale will force ARGB
  177. // since the edge conditions can't be guaranteed.
  178. needTrans = (mtx[0] != (int)mtx[0] || mtx[3] != (int)mtx[3]);
  179. }
  180. if (needTrans &&
  181. srcCM.getTransparency() == Transparency.OPAQUE)
  182. {
  183. // Need to convert first
  184. ColorConvertOp ccop = new ColorConvertOp(hints);
  185. BufferedImage tmpSrc = null;
  186. int sw = src.getWidth();
  187. int sh = src.getHeight();
  188. if (dstCM.getTransparency() == Transparency.OPAQUE) {
  189. tmpSrc = new BufferedImage(sw, sh,
  190. BufferedImage.TYPE_INT_ARGB);
  191. }
  192. else {
  193. WritableRaster r =
  194. dstCM.createCompatibleWritableRaster(sw, sh);
  195. tmpSrc = new BufferedImage(dstCM, r,
  196. dstCM.isAlphaPremultiplied(),
  197. null);
  198. }
  199. src = ccop.filter(src, tmpSrc);
  200. }
  201. else {
  202. needToConvert = true;
  203. dst = createCompatibleDestImage(src, null);
  204. }
  205. }
  206. }
  207. if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
  208. dst.getColorModel() instanceof IndexColorModel) {
  209. dst = new BufferedImage(dst.getWidth(), dst.getHeight(),
  210. BufferedImage.TYPE_INT_ARGB);
  211. }
  212. if (ImagingLib.filter(this, src, dst) == null) {
  213. throw new ImagingOpException ("Unable to transform src image");
  214. }
  215. if (needToConvert) {
  216. ColorConvertOp ccop = new ColorConvertOp(hints);
  217. ccop.filter(dst, origDst);
  218. }
  219. else if (origDst != dst) {
  220. java.awt.Graphics2D g = origDst.createGraphics();
  221. g.drawImage(dst, 0, 0, null);
  222. }
  223. return origDst;
  224. }
  225. /**
  226. * Transforms the source <CODE>Raster</CODE> and stores the results in
  227. * the destination <CODE>Raster</CODE>. This operation performs the
  228. * transform band by band.
  229. * <p>
  230. * If the destination <CODE>Raster</CODE> is null, a new
  231. * <CODE>Raster</CODE> is created.
  232. * An <CODE>IllegalArgumentException</CODE> may be thrown if the source is
  233. * the same as the destination or if the number of bands in
  234. * the source is not equal to the number of bands in the
  235. * destination.
  236. *
  237. * @param src The <CODE>Raster</CODE> to transform.
  238. * @param dst The <CODE>Raster</CODE> in which to store the results of the
  239. * transformation.
  240. *
  241. * @return The transformed <CODE>Raster</CODE>.
  242. *
  243. * @throws ImagingOpException If the <CODE>Raster</CODE> cannot be
  244. * transformed.
  245. */
  246. public final WritableRaster filter(Raster src, WritableRaster dst) {
  247. if (src == null) {
  248. throw new NullPointerException("src image is null");
  249. }
  250. if (dst == null) {
  251. dst = createCompatibleDestRaster(src);
  252. }
  253. if (src == dst) {
  254. throw new IllegalArgumentException("src image cannot be the "+
  255. "same as the dst image");
  256. }
  257. if (src.getNumBands() != dst.getNumBands()) {
  258. throw new IllegalArgumentException("Number of src bands ("+
  259. src.getNumBands()+
  260. ") does not match number of "+
  261. " dst bands ("+
  262. dst.getNumBands()+")");
  263. }
  264. if (ImagingLib.filter(this, src, dst) == null) {
  265. throw new ImagingOpException ("Unable to transform src image");
  266. }
  267. return dst;
  268. }
  269. /**
  270. * Returns the bounding box of the transformed destination. The
  271. * rectangle returned is the actual bounding box of the
  272. * transformed points.
  273. *
  274. * @param src The <CODE>BufferedImage</CODE> to be transformed.
  275. *
  276. * @return The <CODE>Rectangle2D</CODE> representing the destination's
  277. * bounding box.
  278. */
  279. public final Rectangle2D getBounds2D (BufferedImage src) {
  280. return getBounds2D(src.getRaster());
  281. }
  282. /**
  283. * Returns the bounding box of the transformed destination. The
  284. * rectangle returned will be the actual bounding box of the
  285. * transformed points.
  286. *
  287. * @param src The <CODE>Raster</CODE> to be transformed.
  288. *
  289. * @return The <CODE>Rectangle2D</CODE> representing the destination's
  290. * bounding box.
  291. */
  292. public final Rectangle2D getBounds2D (Raster src) {
  293. int w = src.getWidth();
  294. int h = src.getHeight();
  295. // Get the bounding box of the src and transform the corners
  296. float[] pts = {0, 0, w, 0, w, h, 0, h};
  297. xform.transform(pts, 0, pts, 0, 4);
  298. // Get the min, max of the dst
  299. float fmaxX = pts[0];
  300. float fmaxY = pts[1];
  301. float fminX = pts[0];
  302. float fminY = pts[1];
  303. int maxX;
  304. int maxY;
  305. for (int i=2; i < 8; i+=2) {
  306. if (pts[i] > fmaxX) {
  307. fmaxX = pts[i];
  308. }
  309. else if (pts[i] < fminX) {
  310. fminX = pts[i];
  311. }
  312. if (pts[i+1] > fmaxY) {
  313. fmaxY = pts[i+1];
  314. }
  315. else if (pts[i+1] < fminY) {
  316. fminY = pts[i+1];
  317. }
  318. }
  319. return new Rectangle2D.Float(fminX, fminY, fmaxX-fminX, fmaxY-fminY);
  320. }
  321. /**
  322. * Creates a zeroed destination image with the correct size and number of
  323. * bands. A <CODE>RasterFormatException</CODE> may be thrown if the
  324. * transformed width or height is equal to 0.
  325. * <p>
  326. * If <CODE>destCM</CODE> is null,
  327. * an appropriate <CODE>ColorModel</CODE> is used; this
  328. * <CODE>ColorModel</CODE> may have
  329. * an alpha channel even if the source <CODE>ColorModel</CODE> is opaque.
  330. *
  331. * @param src The <CODE>BufferedImage</CODE> to be transformed.
  332. * @param destCM <CODE>ColorModel</CODE> of the destination. If null,
  333. * an appropriate <CODE>ColorModel</CODE> is used.
  334. *
  335. * @return The zeroed destination image.
  336. */
  337. public BufferedImage createCompatibleDestImage (BufferedImage src,
  338. ColorModel destCM) {
  339. BufferedImage image;
  340. Rectangle r = getBounds2D(src).getBounds();
  341. // If r.x (or r.y) is < 0, then we want to only create an image
  342. // that is in the positive range.
  343. // If r.x (or r.y) is > 0, then we need to create an image that
  344. // includes the translation.
  345. int w = r.x + r.width;
  346. int h = r.y + r.height;
  347. if (w <= 0) {
  348. throw new RasterFormatException("Transformed width ("+w+
  349. ") is less than or equal to 0.");
  350. }
  351. if (h <= 0) {
  352. throw new RasterFormatException("Transformed height ("+h+
  353. ") is less than or equal to 0.");
  354. }
  355. if (destCM == null) {
  356. ColorModel cm = src.getColorModel();
  357. if (interpolationType != TYPE_NEAREST_NEIGHBOR &&
  358. (cm instanceof IndexColorModel ||
  359. cm.getTransparency() == Transparency.OPAQUE))
  360. {
  361. image = new BufferedImage(w, h,
  362. BufferedImage.TYPE_INT_ARGB);
  363. }
  364. else {
  365. image = new BufferedImage(cm,
  366. src.getRaster().createCompatibleWritableRaster(w,h),
  367. cm.isAlphaPremultiplied(), null);
  368. }
  369. }
  370. else {
  371. image = new BufferedImage(destCM,
  372. destCM.createCompatibleWritableRaster(w,h),
  373. destCM.isAlphaPremultiplied(), null);
  374. }
  375. return image;
  376. }
  377. /**
  378. * Creates a zeroed destination <CODE>Raster</CODE> with the correct size
  379. * and number of bands. A <CODE>RasterFormatException</CODE> may be thrown
  380. * if the transformed width or height is equal to 0.
  381. *
  382. * @param src The <CODE>Raster</CODE> to be transformed.
  383. *
  384. * @return The zeroed destination <CODE>Raster</CODE>.
  385. */
  386. public WritableRaster createCompatibleDestRaster (Raster src) {
  387. Rectangle2D r = getBounds2D(src);
  388. return src.createCompatibleWritableRaster((int)r.getX(),
  389. (int)r.getY(),
  390. (int)r.getWidth(),
  391. (int)r.getHeight());
  392. }
  393. /**
  394. * Returns the location of the corresponding destination point given a
  395. * point in the source. If <CODE>dstPt</CODE> is specified, it
  396. * is used to hold the return value.
  397. *
  398. * @param dstPt The <CODE>Point2D</CODE> in which to store the result.
  399. *
  400. * @return The <CODE>Point2D</CODE> in the destination that corresponds to
  401. * the specified point in the source.
  402. */
  403. public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
  404. return xform.transform (srcPt, dstPt);
  405. }
  406. /**
  407. * Returns the affine transform used by this transform operation.
  408. *
  409. * @return The <CODE>AffineTransform</CODE> associated with this op.
  410. */
  411. public final AffineTransform getTransform() {
  412. return (AffineTransform) xform.clone();
  413. }
  414. /**
  415. * Returns the rendering hints used by this transform operation.
  416. *
  417. * @return The <CODE>RenderingHints</CODE> object associated with this op.
  418. */
  419. public final RenderingHints getRenderingHints() {
  420. if (hints == null) {
  421. Object val;
  422. switch(interpolationType) {
  423. case TYPE_NEAREST_NEIGHBOR:
  424. val = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
  425. break;
  426. case TYPE_BILINEAR:
  427. val = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
  428. break;
  429. case TYPE_BICUBIC:
  430. val = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
  431. break;
  432. default:
  433. throw new InternalError("Unknown interpolation type "+
  434. interpolationType);
  435. }
  436. hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, val);
  437. }
  438. return hints;
  439. }
  440. }