1. /*
  2. * @(#)AlphaCompositeContext.java 1.11 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.awt.image.ColorModel;
  12. import java.awt.image.IndexColorModel;
  13. import java.awt.image.BufferedImage;
  14. import java.awt.image.Raster;
  15. import java.awt.image.WritableRaster;
  16. import sun.awt.image.IntegerComponentRaster;
  17. class AlphaCompositeContext implements CompositeContext {
  18. ColorModel srcCM;
  19. ColorModel dstCM;
  20. boolean srcNeedConvert;
  21. boolean dstNeedConvert;
  22. int rule;
  23. float extraAlpha;
  24. public AlphaCompositeContext(ColorModel s, ColorModel d, int rule,
  25. float extraAlpha) {
  26. srcCM = s;
  27. if (srcCM.equals(ColorModel.getRGBdefault())) {
  28. srcNeedConvert = false;
  29. } else {
  30. srcNeedConvert = true;
  31. }
  32. dstCM = d;
  33. if (dstCM.equals(ColorModel.getRGBdefault())) {
  34. dstNeedConvert = false;
  35. } else {
  36. dstNeedConvert = true;
  37. }
  38. this.rule = rule;
  39. this.extraAlpha = extraAlpha;
  40. }
  41. /*
  42. * Convert a given Raster to the desired data format.
  43. */
  44. WritableRaster convertRaster(Raster inRaster, ColorModel inCM, ColorModel outCM) {
  45. // Use a faster conversion if this is an IndexColorModel
  46. if (inCM instanceof IndexColorModel &&
  47. outCM.equals(ColorModel.getRGBdefault())) {
  48. IndexColorModel icm = (IndexColorModel) inCM;
  49. BufferedImage dbi = icm.convertToIntDiscrete(inRaster, false);
  50. return dbi.getRaster();
  51. }
  52. BufferedImage dbi =
  53. new BufferedImage(outCM,
  54. outCM.createCompatibleWritableRaster(inRaster.getWidth(),
  55. inRaster.getHeight()),
  56. outCM.isAlphaPremultiplied(),
  57. null);
  58. // ColorSpace[] cs = {inCM.getColorSpace(), outCM.getColorSpace()};
  59. // ColorConvertOp cOp = new ColorConvertOp(cs);
  60. // cOp.filter(sbi, dbi);
  61. // use this slow method to convert untill ColorConvertOp is available.
  62. // Does not take in to account quality dithering if applicable.
  63. for (int i = 0 ; i < inRaster.getHeight() ; i++) {
  64. for (int j = 0 ; j < inRaster.getWidth() ; j++) {
  65. dbi.setRGB(j, i,
  66. inCM.getRGB(inRaster.getDataElements(j,i,null)));
  67. }
  68. }
  69. return dbi.getRaster();
  70. }
  71. /**
  72. * Release resources allocated for context.
  73. */
  74. public void dispose() {
  75. }
  76. // native void alphaComposite(Raster s, Raster d,
  77. // int width, int height,
  78. // int rule, float extraAlpha);
  79. /**
  80. * This method composes the two source tiles
  81. * and places the result in the destination tile. Note that
  82. * the destination can be the same object as either
  83. * the first or second source.
  84. * @param src1 The first source tile for the compositing operation.
  85. * @param src2 The second source tile for the compositing operation.
  86. * @param dst The tile where the result of the operation is stored.
  87. */
  88. public void compose(Raster src1, Raster src2, WritableRaster dst) {
  89. IntegerComponentRaster s;
  90. IntegerComponentRaster d;
  91. WritableRaster dstOrg = dst;
  92. int w;
  93. int h;
  94. if (srcNeedConvert) {
  95. src1 = convertRaster(src1, srcCM, ColorModel.getRGBdefault());
  96. src2 = convertRaster(src2, srcCM, ColorModel.getRGBdefault());
  97. }
  98. if (dstNeedConvert && !(dst == src1 || dst == src2)) {
  99. dst = convertRaster(dst, dstCM, ColorModel.getRGBdefault());
  100. }
  101. if (dst == src1) {
  102. s = (IntegerComponentRaster)src2;
  103. } else if (dst == src2) {
  104. s = (IntegerComponentRaster)src1;
  105. } else {
  106. dst.setDataElements(0, 0, src2);
  107. s = (IntegerComponentRaster)src1;
  108. }
  109. d = (IntegerComponentRaster)dst;
  110. w = Math.min(s.getWidth(), d.getWidth());
  111. h = Math.min(s.getHeight(), d.getHeight());
  112. // REMIND: May need a wrapper in RasterOutputManager.c to avoid
  113. // reference to sun.awt.image package from here:
  114. // alphaComposite(s, d, w, h, rule, extraAlpha);
  115. sun.java2d.loops.RasterOutputManager.ARGBpaintARGB(s, false, d, rule,
  116. extraAlpha, null,
  117. 0, 0, 0, 0, 0, 0,
  118. w, h, 0);
  119. if (dstNeedConvert) {
  120. dst = convertRaster(dst, ColorModel.getRGBdefault(), dstCM);
  121. dstOrg.setDataElements(0, 0, dst);
  122. }
  123. }
  124. }