KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > CompositeImageDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import org.eclipse.swt.graphics.ImageData;
14 import org.eclipse.swt.graphics.PaletteData;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.graphics.RGB;
17
18 /**
19  * Abstract base class for image descriptors that synthesize an image from other
20  * images in order to simulate the effect of custom drawing. For example, this
21  * could be used to superimpose a red bar dexter symbol across an image to
22  * indicate that something was disallowed.
23  * <p>
24  * Subclasses must implement the <code>getSize</code> and <code>fill</code>
25  * methods. Little or no work happens until the image descriptor's image is
26  * actually requested by a call to <code>createImage</code> (or to
27  * <code>getImageData</code> directly).
28  * </p>
29  */

30 public abstract class CompositeImageDescriptor extends ImageDescriptor {
31
32     /**
33      * The image data for this composite image.
34      */

35     private ImageData imageData;
36
37     /**
38      * Constructs an uninitialized composite image.
39      */

40     protected CompositeImageDescriptor() {
41     }
42
43     /**
44      * Draw the composite images.
45      * <p>
46      * Subclasses must implement this framework method to paint images within
47      * the given bounds using one or more calls to the <code>drawImage</code>
48      * framework method.
49      * </p>
50      *
51      * @param width
52      * the width
53      * @param height
54      * the height
55      */

56     protected abstract void drawCompositeImage(int width, int height);
57
58     /**
59      * Draws the given source image data into this composite image at the given
60      * position.
61      * <p>
62      * Call this internal framework method to superimpose another image atop
63      * this composite image.
64      * </p>
65      *
66      * @param src
67      * the source image data
68      * @param ox
69      * the x position
70      * @param oy
71      * the y position
72      */

73     final protected void drawImage(ImageData src, int ox, int oy) {
74         ImageData dst = imageData;
75         PaletteData srcPalette = src.palette;
76         ImageData srcMask = null;
77         int alphaMask = 0, alphaShift = 0;
78         if (src.maskData != null) {
79             srcMask = src.getTransparencyMask ();
80             if (src.depth == 32) {
81                 alphaMask = ~(srcPalette.redMask | srcPalette.greenMask | srcPalette.blueMask);
82                 while (alphaMask != 0 && ((alphaMask >>> alphaShift) & 1) == 0) alphaShift++;
83             }
84         }
85         for (int srcY = 0, dstY = srcY + oy; srcY < src.height; srcY++, dstY++) {
86             for (int srcX = 0, dstX = srcX + ox; srcX < src.width; srcX++, dstX++) {
87                 if (!(0 <= dstX && dstX < dst.width && 0 <= dstY && dstY < dst.height)) continue;
88                 int srcPixel = src.getPixel(srcX, srcY);
89                 int srcAlpha = 255;
90                 if (src.maskData != null) {
91                     if (src.depth == 32) {
92                         srcAlpha = (srcPixel & alphaMask) >>> alphaShift;
93                         if (srcAlpha == 0) {
94                             srcAlpha = srcMask.getPixel(srcX, srcY) != 0 ? 255 : 0;
95                         }
96                     } else {
97                         if (srcMask.getPixel(srcX, srcY) == 0) srcAlpha = 0;
98                     }
99                 } else if (src.transparentPixel != -1) {
100                     if (src.transparentPixel == srcPixel) srcAlpha = 0;
101                 } else if (src.alpha != -1) {
102                     srcAlpha = src.alpha;
103                 } else if (src.alphaData != null) {
104                     srcAlpha = src.getAlpha(srcX, srcY);
105                 }
106                 if (srcAlpha == 0) continue;
107                 int srcRed, srcGreen, srcBlue;
108                 if (srcPalette.isDirect) {
109                     srcRed = srcPixel & srcPalette.redMask;
110                     srcRed = (srcPalette.redShift < 0) ? srcRed >>> -srcPalette.redShift : srcRed << srcPalette.redShift;
111                     srcGreen = srcPixel & srcPalette.greenMask;
112                     srcGreen = (srcPalette.greenShift < 0) ? srcGreen >>> -srcPalette.greenShift : srcGreen << srcPalette.greenShift;
113                     srcBlue = srcPixel & srcPalette.blueMask;
114                     srcBlue = (srcPalette.blueShift < 0) ? srcBlue >>> -srcPalette.blueShift : srcBlue << srcPalette.blueShift;
115                 } else {
116                     RGB rgb = srcPalette.getRGB(srcPixel);
117                     srcRed = rgb.red;
118                     srcGreen = rgb.green;
119                     srcBlue = rgb.blue;
120                 }
121                 int dstRed, dstGreen, dstBlue, dstAlpha;
122                 if (srcAlpha == 255) {
123                     dstRed = srcRed;
124                     dstGreen = srcGreen;
125                     dstBlue= srcBlue;
126                     dstAlpha = srcAlpha;
127                 } else {
128                     int dstPixel = dst.getPixel(dstX, dstY);
129                     dstAlpha = dst.getAlpha(dstX, dstY);
130                     dstRed = (dstPixel & 0xFF) >>> 0;
131                     dstGreen = (dstPixel & 0xFF00) >>> 8;
132                     dstBlue = (dstPixel & 0xFF0000) >>> 16;
133                     dstRed += (srcRed - dstRed) * srcAlpha / 255;
134                     dstGreen += (srcGreen - dstGreen) * srcAlpha / 255;
135                     dstBlue += (srcBlue - dstBlue) * srcAlpha / 255;
136                     dstAlpha += (srcAlpha - dstAlpha) * srcAlpha / 255;
137                 }
138                 dst.setPixel(dstX, dstY, ((dstRed & 0xFF) << 0) | ((dstGreen & 0xFF) << 8) | ((dstBlue & 0xFF) << 16));
139                 dst.setAlpha(dstX, dstY, dstAlpha);
140             }
141         }
142     }
143
144     /*
145      * (non-Javadoc) Method declared on ImageDesciptor.
146      */

147     public ImageData getImageData() {
148         Point size = getSize();
149         
150         /* Create a 24 bit image data with alpha channel */
151         imageData = new ImageData(size.x, size.y, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000));
152         imageData.alphaData = new byte[imageData.width * imageData.height];
153         
154         drawCompositeImage(size.x, size.y);
155         
156         /* Detect minimum transparency */
157         boolean transparency = false;
158         byte[] alphaData = imageData.alphaData;
159         for (int i = 0; i < alphaData.length; i++) {
160             int alpha = alphaData[i] & 0xFF;
161             if (!(alpha == 0 || alpha == 255)) {
162                 /* Full alpha channel transparency */
163                 return imageData;
164             }
165             if (!transparency && alpha == 0) transparency = true;
166         }
167         if (transparency) {
168             /* Reduce to 1-bit alpha channel transparency */
169             PaletteData palette = new PaletteData(new RGB[]{new RGB(0, 0, 0), new RGB(255, 255, 255)});
170             ImageData mask = new ImageData(imageData.width, imageData.height, 1, palette);
171             for (int y = 0; y < mask.height; y++) {
172                 for (int x = 0; x < mask.width; x++) {
173                     mask.setPixel(x, y, imageData.getAlpha(x, y) == 255 ? 1 : 0);
174                 }
175             }
176         } else {
177             /* no transparency */
178             imageData.alphaData = null;
179         }
180         return imageData;
181     }
182     
183
184     /**
185      * Return the transparent pixel for the receiver.
186      * <strong>NOTE</strong> This value is not currently in use in the
187      * default implementation.
188      * @return int
189      * @since 3.3
190      */

191     protected int getTransparentPixel() {
192         return 0;
193     }
194
195     /**
196      * Return the size of this composite image.
197      * <p>
198      * Subclasses must implement this framework method.
199      * </p>
200      *
201      * @return the x and y size of the image expressed as a point object
202      */

203     protected abstract Point getSize();
204
205     /**
206      * @param imageData The imageData to set.
207      * @since 3.3
208      */

209     protected void setImageData(ImageData imageData) {
210         this.imageData = imageData;
211     }
212 }
213
Popular Tags