Source Code Cross Referenced for BandCombineOp.java in  » 6.0-JDK-Core » AWT » java » awt » image » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » AWT » java.awt.image 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.awt.image;
027
028        import java.awt.GraphicsEnvironment;
029        import java.awt.color.ICC_Profile;
030        import java.awt.geom.Rectangle2D;
031        import java.awt.Rectangle;
032        import java.awt.geom.Point2D;
033        import java.awt.RenderingHints;
034        import sun.awt.image.ImagingLib;
035        import java.util.Arrays;
036
037        /**
038         * This class performs an arbitrary linear combination of the bands 
039         * in a <CODE>Raster</CODE>, using a specified matrix.  
040         * <p>
041         * The width of the matrix must be equal to the number of bands in the
042         * source <CODE>Raster</CODE>, optionally plus one.  If there is one more 
043         * column in the matrix than the number of bands, there is an implied 1 at the
044         * end of the vector of band samples representing a pixel.  The height
045         * of the matrix must be equal to the number of bands in the destination.
046         * <p>
047         * For example, a 3-banded <CODE>Raster</CODE> might have the following 
048         * transformation applied to each pixel in order to invert the second band of 
049         * the <CODE>Raster</CODE>.
050         * <pre>
051         *   [ 1.0   0.0   0.0    0.0  ]     [ b1 ]    
052         *   [ 0.0  -1.0   0.0  255.0  ]  x  [ b2 ]
053         *   [ 0.0   0.0   1.0    0.0  ]     [ b3 ]
054         *                                   [ 1 ]
055         * </pre>
056         *
057         * <p>
058         * Note that the source and destination can be the same object.
059         * @version 10 Feb 1997
060         */
061        public class BandCombineOp implements  RasterOp {
062            float[][] matrix;
063            int nrows = 0;
064            int ncols = 0;
065            RenderingHints hints;
066
067            /**
068             * Constructs a <CODE>BandCombineOp</CODE> with the specified matrix.
069             * The width of the matrix must be equal to the number of bands in 
070             * the source <CODE>Raster</CODE>, optionally plus one.  If there is one 
071             * more column in the matrix than the number of bands, there is an implied 
072             * 1 at the end of the vector of band samples representing a pixel.  The 
073             * height of the matrix must be equal to the number of bands in the 
074             * destination.
075             * <p>
076             * The first subscript is the row index and the second
077             * is the column index.  This operation uses none of the currently
078             * defined rendering hints; the <CODE>RenderingHints</CODE> argument can be 
079             * null.
080             * 
081             * @param matrix The matrix to use for the band combine operation.
082             * @param hints The <CODE>RenderingHints</CODE> object for this operation. 
083             * Not currently used so it can be null.
084             */
085            public BandCombineOp(float[][] matrix, RenderingHints hints) {
086                nrows = matrix.length;
087                ncols = matrix[0].length;
088                this .matrix = new float[nrows][];
089                for (int i = 0; i < nrows; i++) {
090                    /* Arrays.copyOf is forgiving of the source array being
091                     * too short, but it is also faster than other cloning
092                     * methods, so we provide our own protection for short
093                     * matrix rows.
094                     */
095                    if (ncols > matrix[i].length) {
096                        throw new IndexOutOfBoundsException("row " + i
097                                + " too short");
098                    }
099                    this .matrix[i] = Arrays.copyOf(matrix[i], ncols);
100                }
101                this .hints = hints;
102            }
103
104            /**
105             * Returns a copy of the linear combination matrix.
106             *
107             * @return The matrix associated with this band combine operation.
108             */
109            public final float[][] getMatrix() {
110                float[][] ret = new float[nrows][];
111                for (int i = 0; i < nrows; i++) {
112                    ret[i] = Arrays.copyOf(matrix[i], ncols);
113                }
114                return ret;
115            }
116
117            /**
118             * Transforms the <CODE>Raster</CODE> using the matrix specified in the 
119             * constructor. An <CODE>IllegalArgumentException</CODE> may be thrown if 
120             * the number of bands in the source or destination is incompatible with 
121             * the matrix.  See the class comments for more details.  
122             * <p>
123             * If the destination is null, it will be created with a number of bands 
124             * equalling the number of rows in the matrix. No exception is thrown
125             * if the operation causes a data overflow.
126             *
127             * @param src The <CODE>Raster</CODE> to be filtered.
128             * @param dst The <CODE>Raster</CODE> in which to store the results 
129             * of the filter operation.
130             *
131             * @return The filtered <CODE>Raster</CODE>.
132             *
133             * @throws IllegalArgumentException If the number of bands in the 
134             * source or destination is incompatible with the matrix.
135             */
136            public WritableRaster filter(Raster src, WritableRaster dst) {
137                int nBands = src.getNumBands();
138                if (ncols != nBands && ncols != (nBands + 1)) {
139                    throw new IllegalArgumentException(
140                            "Number of columns in the " + "matrix (" + ncols
141                                    + ") must be equal to the number"
142                                    + " of bands ([+1]) in src (" + nBands
143                                    + ").");
144                }
145                if (dst == null) {
146                    dst = createCompatibleDestRaster(src);
147                } else if (nrows != dst.getNumBands()) {
148                    throw new IllegalArgumentException("Number of rows in the "
149                            + "matrix (" + nrows
150                            + ") must be equal to the number"
151                            + " of bands ([+1]) in dst (" + nBands + ").");
152                }
153
154                if (ImagingLib.filter(this , src, dst) != null) {
155                    return dst;
156                }
157
158                int[] pixel = null;
159                int[] dstPixel = new int[dst.getNumBands()];
160                float accum;
161                int sminX = src.getMinX();
162                int sY = src.getMinY();
163                int dminX = dst.getMinX();
164                int dY = dst.getMinY();
165                int sX;
166                int dX;
167                if (ncols == nBands) {
168                    for (int y = 0; y < src.getHeight(); y++, sY++, dY++) {
169                        dX = dminX;
170                        sX = sminX;
171                        for (int x = 0; x < src.getWidth(); x++, sX++, dX++) {
172                            pixel = src.getPixel(sX, sY, pixel);
173                            for (int r = 0; r < nrows; r++) {
174                                accum = 0.f;
175                                for (int c = 0; c < ncols; c++) {
176                                    accum += matrix[r][c] * pixel[c];
177                                }
178                                dstPixel[r] = (int) accum;
179                            }
180                            dst.setPixel(dX, dY, dstPixel);
181                        }
182                    }
183                } else {
184                    // Need to add constant
185                    for (int y = 0; y < src.getHeight(); y++, sY++, dY++) {
186                        dX = dminX;
187                        sX = sminX;
188                        for (int x = 0; x < src.getWidth(); x++, sX++, dX++) {
189                            pixel = src.getPixel(sX, sY, pixel);
190                            for (int r = 0; r < nrows; r++) {
191                                accum = 0.f;
192                                for (int c = 0; c < nBands; c++) {
193                                    accum += matrix[r][c] * pixel[c];
194                                }
195                                dstPixel[r] = (int) (accum + matrix[r][nBands]);
196                            }
197                            dst.setPixel(dX, dY, dstPixel);
198                        }
199                    }
200                }
201
202                return dst;
203            }
204
205            /**
206             * Returns the bounding box of the transformed destination.  Since
207             * this is not a geometric operation, the bounding box is the same for
208             * the source and destination.
209             * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of
210             * bands in the source is incompatible with the matrix.  See
211             * the class comments for more details.
212             *
213             * @param src The <CODE>Raster</CODE> to be filtered.
214             *
215             * @return The <CODE>Rectangle2D</CODE> representing the destination 
216             * image's bounding box.
217             *
218             * @throws IllegalArgumentException If the number of bands in the source
219             * is incompatible with the matrix.
220             */
221            public final Rectangle2D getBounds2D(Raster src) {
222                return src.getBounds();
223            }
224
225            /**
226             * Creates a zeroed destination <CODE>Raster</CODE> with the correct size 
227             * and number of bands.
228             * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of
229             * bands in the source is incompatible with the matrix.  See
230             * the class comments for more details.
231             *
232             * @param src The <CODE>Raster</CODE> to be filtered.
233             *
234             * @return The zeroed destination <CODE>Raster</CODE>.
235             */
236            public WritableRaster createCompatibleDestRaster(Raster src) {
237                int nBands = src.getNumBands();
238                if ((ncols != nBands) && (ncols != (nBands + 1))) {
239                    throw new IllegalArgumentException(
240                            "Number of columns in the " + "matrix (" + ncols
241                                    + ") must be equal to the number"
242                                    + " of bands ([+1]) in src (" + nBands
243                                    + ").");
244                }
245                if (src.getNumBands() == nrows) {
246                    return src.createCompatibleWritableRaster();
247                } else {
248                    throw new IllegalArgumentException(
249                            "Don't know how to create a "
250                                    + " compatible Raster with " + nrows
251                                    + " bands.");
252                }
253            }
254
255            /**
256             * Returns the location of the corresponding destination point given a
257             * point in the source <CODE>Raster</CODE>.  If <CODE>dstPt</CODE> is 
258             * specified, it is used to hold the return value.
259             * Since this is not a geometric operation, the point returned 
260             * is the same as the specified <CODE>srcPt</CODE>.
261             *
262             * @param srcPt The <code>Point2D</code> that represents the point in
263             *              the source <code>Raster</code>
264             * @param dstPt The <CODE>Point2D</CODE> in which to store the result.
265             *
266             * @return The <CODE>Point2D</CODE> in the destination image that 
267             * corresponds to the specified point in the source image.     
268             */
269            public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
270                if (dstPt == null) {
271                    dstPt = new Point2D.Float();
272                }
273                dstPt.setLocation(srcPt.getX(), srcPt.getY());
274
275                return dstPt;
276            }
277
278            /**
279             * Returns the rendering hints for this operation. 
280             *
281             * @return The <CODE>RenderingHints</CODE> object associated with this
282             * operation.  Returns null if no hints have been set.
283             */
284            public final RenderingHints getRenderingHints() {
285                return hints;
286            }
287        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.