home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / AlphaCompositeContext.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.1 KB  |  153 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AlphaCompositeContext.java    1.9 98/05/11
  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.                               null);
  73.         
  74. //         ColorSpace[] cs = {inCM.getColorSpace(), outCM.getColorSpace()};
  75. //         ColorConvertOp cOp = new ColorConvertOp(cs);
  76. //         cOp.filter(sbi, dbi);
  77.         // use this slow method to convert untill ColorConvertOp is available.
  78.         // Does not take in to account quality dithering if applicable.
  79.         
  80.         for (int i = 0 ; i < inRaster.getHeight() ; i++) {
  81.             for (int j = 0 ; j < inRaster.getWidth() ; j++) {
  82.                 dbi.setRGB(j, i,
  83.                            inCM.getRGB(inRaster.getDataElements(j,i,null)));
  84.             }
  85.         }
  86.         return dbi.getRaster();
  87.     }
  88.  
  89.     /**
  90.      * Release resources allocated for context.
  91.      */
  92.     public void dispose() {
  93.     }
  94.  
  95. //     native void alphaComposite(Raster s, Raster d,
  96. //                                int width, int height,
  97. //                                int rule, float extraAlpha);
  98.     
  99.     /**
  100.      * This method composes the two source tiles
  101.      * and places the result in the destination tile. Note that
  102.      * the destination can be the same object as either
  103.      * the first or second source.
  104.      * @param src1 The first source tile for the compositing operation.
  105.      * @param src2 The second source tile for the compositing operation.
  106.      * @param dst The tile where the result of the operation is stored.
  107.      */
  108.     public void compose(Raster src1, Raster src2, WritableRaster dst) {
  109.         IntegerComponentRaster s;
  110.         IntegerComponentRaster d;
  111.         WritableRaster dstOrg = dst;
  112.         int w;
  113.         int h;
  114.         
  115.         if (srcNeedConvert) {
  116.             src1 = convertRaster(src1, srcCM, ColorModel.getRGBdefault());
  117.             src2 = convertRaster(src2, srcCM, ColorModel.getRGBdefault());
  118.         }
  119.         if (dstNeedConvert && !(dst == src1 || dst == src2)) {
  120.             dst = convertRaster(dst, dstCM, ColorModel.getRGBdefault());
  121.         }
  122.  
  123.         if (dst == src1) {
  124.             s = (IntegerComponentRaster)src2;
  125.         } else if (dst == src2) {
  126.             s = (IntegerComponentRaster)src1;
  127.         } else {
  128.             dst.setDataElements(0, 0, src2);
  129.             s = (IntegerComponentRaster)src1;
  130.         }
  131.         d = (IntegerComponentRaster)dst;
  132.  
  133.         w = Math.min(s.getWidth(), d.getWidth());
  134.         h = Math.min(s.getHeight(), d.getHeight());
  135.         
  136.         // REMIND: May need a wrapper in RasterOutputManager.c to avoid
  137.         // reference to sun.awt.image package from here:
  138. //         alphaComposite(s, d, w, h, rule, extraAlpha);
  139.         
  140.         sun.java2d.loops.RasterOutputManager.ARGBpaintARGB(s, false, d, rule,
  141.                                                         extraAlpha, null,
  142.                                                         0, 0, 0, 0, 0, 0,
  143.                                                         w, h, 0);
  144.  
  145.         if (dstNeedConvert) {
  146.             dst = convertRaster(dst, ColorModel.getRGBdefault(), dstCM);
  147.             dstOrg.setDataElements(0, 0, dst);
  148.         }
  149.         
  150.     }
  151. }
  152.  
  153.