1 /*
2  * $RCSfile: MlibXorConstOpImage.java,v $
3  *
4  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5  *
6  * Use is subject to license terms.
7  *
8  * $Revision: 1.1 $
9  * $Date: 2005/02/11 04:56:09 $
10  * $State: Exp $
11  */
12 package com.lightcrafts.media.jai.mlib;
13 import java.awt.Rectangle;
14 import java.awt.image.DataBuffer;
15 import java.awt.image.Raster;
16 import java.awt.image.RenderedImage;
17 import java.awt.image.WritableRaster;
18 import com.lightcrafts.mediax.jai.ImageLayout;
19 import com.lightcrafts.mediax.jai.PointOpImage;
20 import java.util.Map;
21 import com.sun.medialib.mlib.*;
22 // import com.lightcrafts.media.jai.test.OpImageTester;
23 
24 /**
25  * A mediaLib implementation of "XorConst" operator.
26  *
27  */
28 final class MlibXorConstOpImage extends PointOpImage {
29     int[] constants;
30 
31     /**
32      * Constructs an MlibXorConstOpImage. The image dimensions are copied
33      * from the source image.  The tile grid layout, SampleModel, and
34      * ColorModel may optionally be specified by an ImageLayout object.
35      *
36      * @param source    a RenderedImage.
37      * @param layout    an ImageLayout optionally containing the tile
38      *                  grid layout, SampleModel, and ColorModel, or null.
39      */
MlibXorConstOpImage(RenderedImage source, Map config, ImageLayout layout, int[] constants)40     public MlibXorConstOpImage(RenderedImage source,
41                                Map config,
42                                ImageLayout layout,
43                                int[] constants) {
44         super(source, layout, config, true);
45         this.constants = MlibUtils.initConstants(constants,
46                                             getSampleModel().getNumBands());
47         // Set flag to permit in-place operation.
48         permitInPlaceOperation();
49     }
50 
51     /**
52      * Xor the pixel values of a rectangle with a given constant.
53      * The sources are cobbled.
54      *
55      * @param sources   an array of sources, guarantee to provide all
56      *                  necessary source data for computing the rectangle.
57      * @param dest      a tile that contains the rectangle to be computed.
58      * @param destRect  the rectangle within this OpImage to be processed.
59      */
computeRect(Raster[] sources, WritableRaster dest, Rectangle destRect)60     protected void computeRect(Raster[] sources,
61                                WritableRaster dest,
62                                Rectangle destRect) {
63         Raster source = sources[0];
64         int formatTag = MediaLibAccessor.findCompatibleTag(sources, dest);
65 
66 	// For PointOpImages, srcRect is the same as destRect
67         MediaLibAccessor srcAccessor = new MediaLibAccessor(source, destRect,
68 							    formatTag);
69         MediaLibAccessor dstAccessor = new MediaLibAccessor(dest, destRect,
70 							    formatTag);
71 
72         switch (dstAccessor.getDataType()) {
73         case DataBuffer.TYPE_BYTE:
74         case DataBuffer.TYPE_USHORT:
75         case DataBuffer.TYPE_SHORT:
76         case DataBuffer.TYPE_INT:
77             mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
78             mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
79             for (int i = 0 ; i < dstML.length; i++) {
80                 int mlconstants[] = dstAccessor.getIntParameters(i, constants);
81                 Image.ConstXor(dstML[i], srcML[i], mlconstants);
82             }
83             break;
84         default:
85             String className = this.getClass().getName();
86             throw new RuntimeException(JaiI18N.getString("Generic2"));
87         }
88 
89         if (dstAccessor.isDataCopy()) {
90             dstAccessor.clampDataArrays();
91             dstAccessor.copyDataToRaster();
92         }
93     }
94 
95 //     public static OpImage createTestImage(OpImageTester oit) {
96 //         int[] consts = {5, 5, 5};
97 //         return new MlibXorConstOpImage(oit.getSource(), null,
98 //                                        new ImageLayout(oit.getSource()),
99 //                                        consts);
100 //     }
101 
102 //     // Calls a method on OpImage that uses introspection, to make this
103 //     // class, discover it's createTestImage() call, call it and then
104 //     // benchmark the performance of the created OpImage chain.
105 //     public static void main (String args[]) {
106 //         String classname = "com.lightcrafts.media.jai.mlib.MlibXorConstOpImage";
107 //         OpImageTester.performDiagnostics(classname,args);
108 //     }
109 }
110