1 /*
2  * $RCSfile: MlibAndConstOpImage.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:55:49 $
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 "AndConst" operator.
26  *
27  */
28 final class MlibAndConstOpImage extends PointOpImage {
29     int[] constants;
30 
31     /**
32      * Constructs an MlibAndConstOpImage. 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      */
MlibAndConstOpImage(RenderedImage source, Map config, ImageLayout layout, int[] constants)40     public MlibAndConstOpImage(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      * And 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 
64         Raster source = sources[0];
65         int formatTag = MediaLibAccessor.findCompatibleTag(sources, dest);
66 
67 	// For PointOpImages, the srcRect and the destRect are the same.
68         MediaLibAccessor srcAccessor = new MediaLibAccessor(source, destRect,
69 							    formatTag);
70         MediaLibAccessor dstAccessor = new MediaLibAccessor(dest, destRect,
71 							    formatTag);
72 
73         switch (dstAccessor.getDataType()) {
74         case DataBuffer.TYPE_BYTE:
75         case DataBuffer.TYPE_USHORT:
76         case DataBuffer.TYPE_SHORT:
77         case DataBuffer.TYPE_INT:
78             mediaLibImage[] srcML = srcAccessor.getMediaLibImages();
79             mediaLibImage[] dstML = dstAccessor.getMediaLibImages();
80             for (int i = 0 ; i < dstML.length; i++) {
81                 int mlconstants[] = dstAccessor.getIntParameters(i, constants);
82                 Image.ConstAnd(dstML[i], srcML[i], mlconstants);
83             }
84             break;
85         default:
86             String className = this.getClass().getName();
87             throw new RuntimeException(className + JaiI18N.getString("Generic2"));
88         }
89 
90         if (dstAccessor.isDataCopy()) {
91             dstAccessor.clampDataArrays();
92             dstAccessor.copyDataToRaster();
93         }
94     }
95 
96 //     public static OpImage createTestImage(OpImageTester oit) {
97 //         int[] consts = {5, 5, 5};
98 //         return new MlibAndConstOpImage(oit.getSource(), null,
99 //                                        new ImageLayout(oit.getSource()),
100 //                                        consts);
101 //     }
102 
103 //     // Calls a method on OpImage that uses introspection, to make this
104 //     // class, discover it's createTestImage() call, call it and then
105 //     // benchmark the performance of the created OpImage chain.
106 //     public static void main (String args[]) {
107 //         String classname = "com.lightcrafts.media.jai.mlib.MlibAndConstOpImage";
108 //         OpImageTester.performDiagnostics(classname,args);
109 //     }
110 }
111