AbstractBufferedImageOp.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. ** Copyright 2005 Huxtable.com. All rights reserved.
  3. */
  4. package com.cvte.blurfilter;
  5. import java.awt.Rectangle;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.Point2D;
  8. import java.awt.geom.Rectangle2D;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.BufferedImageOp;
  11. import java.awt.image.ColorModel;
  12. /**
  13. * A convenience class which implements those methods of BufferedImageOp which are rarely changed.
  14. */
  15. public abstract class AbstractBufferedImageOp implements BufferedImageOp {
  16. public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
  17. if ( dstCM == null )
  18. dstCM = src.getColorModel();
  19. return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()), dstCM.isAlphaPremultiplied(), null);
  20. }
  21. public Rectangle2D getBounds2D( BufferedImage src ) {
  22. return new Rectangle(0, 0, src.getWidth(), src.getHeight());
  23. }
  24. public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
  25. if ( dstPt == null )
  26. dstPt = new Point2D.Double();
  27. dstPt.setLocation( srcPt.getX(), srcPt.getY() );
  28. return dstPt;
  29. }
  30. public RenderingHints getRenderingHints() {
  31. return null;
  32. }
  33. /**
  34. * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance
  35. * penalty of BufferedImage.getRGB unmanaging the image.
  36. */
  37. public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
  38. int type = image.getType();
  39. if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
  40. return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
  41. return image.getRGB( x, y, width, height, pixels, 0, width );
  42. }
  43. /**
  44. * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
  45. * penalty of BufferedImage.setRGB unmanaging the image.
  46. */
  47. public void setRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
  48. int type = image.getType();
  49. if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
  50. image.getRaster().setDataElements( x, y, width, height, pixels );
  51. else
  52. image.setRGB( x, y, width, height, pixels, 0, width );
  53. }
  54. }