home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / AlphaCompositeContext.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.1 KB  |  151 lines

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