001 /*
002 * $Id: BlendComposite.java 2464 2007-11-20 21:36:13Z rah003 $
003 *
004 * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
005 *
006 * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
007 * Santa Clara, California 95054, U.S.A. All rights reserved.
008 *
009 * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
010 * All rights reserved.
011 *
012 * Redistribution and use in source and binary forms, with or without
013 * modification, are permitted provided that the following conditions
014 * are met:
015 * 1. Redistributions of source code must retain the above copyright
016 * notice, this list of conditions and the following disclaimer.
017 * 2. Redistributions in binary form must reproduce the above copyright
018 * notice, this list of conditions and the following disclaimer in the
019 * documentation and/or other materials provided with the distribution.
020 * 3. The name of the author may not be used to endorse or promote products
021 * derived from this software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
024 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
025 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
026 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
028 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
032 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034
035 package org.jdesktop.swingx.graphics;
036
037 import java.awt.Composite;
038 import java.awt.CompositeContext;
039 import java.awt.RenderingHints;
040 import java.awt.image.ColorModel;
041 import java.awt.image.DataBuffer;
042 import java.awt.image.DirectColorModel;
043 import java.awt.image.Raster;
044 import java.awt.image.WritableRaster;
045 import java.awt.image.RasterFormatException;
046
047 /**
048 * <p>A blend composite defines the rule according to which a drawing primitive
049 * (known as the source) is mixed with existing graphics (know as the
050 * destination.)</p>
051 * <p><code>BlendComposite</code> is an implementation of the
052 * {@link java.awt.Composite} interface and must therefore be set as a state on
053 * a {@link java.awt.Graphics2D} surface.</p>
054 * <p>Please refer to {@link java.awt.Graphics2D#setComposite(java.awt.Composite)}
055 * for more information on how to use this class with a graphics surface.</p>
056 * <h2>Blending Modes</h2>
057 * <p>This class offers a certain number of blending modes, or compositing
058 * rules. These rules are inspired from graphics editing software packages,
059 * like <em>Adobe Photoshop</em> or <em>The GIMP</em>.</p>
060 * <p>Given the wide variety of implemented blending modes and the difficulty
061 * to describe them with words, please refer to those tools to visually see
062 * the result of these blending modes.</p>
063 * <h2>Opacity</h2>
064 * <p>Each blending mode has an associated opacity, defined as a float value
065 * between 0.0 and 1.0. Changing the opacity controls the force with which the
066 * compositing operation is applied. For instance, a composite with an opacity
067 * of 0.0 will not draw the source onto the destination. With an opacity of
068 * 1.0, the source will be fully drawn onto the destination, according to the
069 * selected blending mode rule.</p>
070 * <p>The opacity, or alpha value, is used by the composite instance to mutiply
071 * the alpha value of each pixel of the source when being composited over the
072 * destination.</p>
073 * <h2>Creating a Blend Composite</h2>
074 * <p>Blend composites can be created in various manners:</p>
075 * <ul>
076 * <li>Use one of the pre-defined instance. Example:
077 * <code>BlendComposite.Average</code>.</li>
078 * <li>Derive one of the pre-defined instances by calling
079 * {@link #derive(float)} or {@link #derive(BlendingMode)}. Deriving allows
080 * you to change either the opacity or the blending mode. Example:
081 * <code>BlendComposite.Average.derive(0.5f)</code>.</li>
082 * <li>Use a factory method: {@link #getInstance(BlendingMode)} or
083 * {@link #getInstance(BlendingMode, float)}.</li>
084 * </ul>
085 * <h2>Implementation Caveat</h2>
086 * <p>TThe blending mode <em>SoftLight</em> has not been implemented yet.</p>
087 *
088 * @see org.jdesktop.swingx.graphics.BlendComposite.BlendingMode
089 * @see java.awt.Graphics2D
090 * @see java.awt.Composite
091 * @see java.awt.AlphaComposite
092 * @author Romain Guy <romain.guy@mac.com>
093 */
094 public final class BlendComposite implements Composite {
095 /**
096 * <p>A blending mode defines the compositing rule of a
097 * {@link org.jdesktop.swingx.graphics.BlendComposite}.</p>
098 *
099 * @author Romain Guy <romain.guy@mac.com>
100 */
101 public enum BlendingMode {
102 AVERAGE,
103 MULTIPLY,
104 SCREEN,
105 DARKEN,
106 LIGHTEN,
107 OVERLAY,
108 HARD_LIGHT,
109 SOFT_LIGHT,
110 DIFFERENCE,
111 NEGATION,
112 EXCLUSION,
113 COLOR_DODGE,
114 INVERSE_COLOR_DODGE,
115 SOFT_DODGE,
116 COLOR_BURN,
117 INVERSE_COLOR_BURN,
118 SOFT_BURN,
119 REFLECT,
120 GLOW,
121 FREEZE,
122 HEAT,
123 ADD,
124 SUBTRACT,
125 STAMP,
126 RED,
127 GREEN,
128 BLUE,
129 HUE,
130 SATURATION,
131 COLOR,
132 LUMINOSITY
133 }
134
135 public static final BlendComposite Average = new BlendComposite(BlendingMode.AVERAGE);
136 public static final BlendComposite Multiply = new BlendComposite(BlendingMode.MULTIPLY);
137 public static final BlendComposite Screen = new BlendComposite(BlendingMode.SCREEN);
138 public static final BlendComposite Darken = new BlendComposite(BlendingMode.DARKEN);
139 public static final BlendComposite Lighten = new BlendComposite(BlendingMode.LIGHTEN);
140 public static final BlendComposite Overlay = new BlendComposite(BlendingMode.OVERLAY);
141 public static final BlendComposite HardLight = new BlendComposite(BlendingMode.HARD_LIGHT);
142 public static final BlendComposite SoftLight = new BlendComposite(BlendingMode.SOFT_LIGHT);
143 public static final BlendComposite Difference = new BlendComposite(BlendingMode.DIFFERENCE);
144 public static final BlendComposite Negation = new BlendComposite(BlendingMode.NEGATION);
145 public static final BlendComposite Exclusion = new BlendComposite(BlendingMode.EXCLUSION);
146 public static final BlendComposite ColorDodge = new BlendComposite(BlendingMode.COLOR_DODGE);
147 public static final BlendComposite InverseColorDodge = new BlendComposite(BlendingMode.INVERSE_COLOR_DODGE);
148 public static final BlendComposite SoftDodge = new BlendComposite(BlendingMode.SOFT_DODGE);
149 public static final BlendComposite ColorBurn = new BlendComposite(BlendingMode.COLOR_BURN);
150 public static final BlendComposite InverseColorBurn = new BlendComposite(BlendingMode.INVERSE_COLOR_BURN);
151 public static final BlendComposite SoftBurn = new BlendComposite(BlendingMode.SOFT_BURN);
152 public static final BlendComposite Reflect = new BlendComposite(BlendingMode.REFLECT);
153 public static final BlendComposite Glow = new BlendComposite(BlendingMode.GLOW);
154 public static final BlendComposite Freeze = new BlendComposite(BlendingMode.FREEZE);
155 public static final BlendComposite Heat = new BlendComposite(BlendingMode.HEAT);
156 public static final BlendComposite Add = new BlendComposite(BlendingMode.ADD);
157 public static final BlendComposite Subtract = new BlendComposite(BlendingMode.SUBTRACT);
158 public static final BlendComposite Stamp = new BlendComposite(BlendingMode.STAMP);
159 public static final BlendComposite Red = new BlendComposite(BlendingMode.RED);
160 public static final BlendComposite Green = new BlendComposite(BlendingMode.GREEN);
161 public static final BlendComposite Blue = new BlendComposite(BlendingMode.BLUE);
162 public static final BlendComposite Hue = new BlendComposite(BlendingMode.HUE);
163 public static final BlendComposite Saturation = new BlendComposite(BlendingMode.SATURATION);
164 public static final BlendComposite Color = new BlendComposite(BlendingMode.COLOR);
165 public static final BlendComposite Luminosity = new BlendComposite(BlendingMode.LUMINOSITY);
166
167 private final float alpha;
168 private final BlendingMode mode;
169
170 private BlendComposite(BlendingMode mode) {
171 this(mode, 1.0f);
172 }
173
174 private BlendComposite(BlendingMode mode, float alpha) {
175 this.mode = mode;
176
177 if (alpha < 0.0f || alpha > 1.0f) {
178 throw new IllegalArgumentException(
179 "alpha must be comprised between 0.0f and 1.0f");
180 }
181 this.alpha = alpha;
182 }
183
184 /**
185 * <p>Creates a new composite based on the blending mode passed
186 * as a parameter. A default opacity of 1.0 is applied.</p>
187 *
188 * @param mode the blending mode defining the compositing rule
189 * @return a new <code>BlendComposite</code> based on the selected blending
190 * mode, with an opacity of 1.0
191 */
192 public static BlendComposite getInstance(BlendingMode mode) {
193 return new BlendComposite(mode);
194 }
195
196 /**
197 * <p>Creates a new composite based on the blending mode and opacity passed
198 * as parameters. The opacity must be a value between 0.0 and 1.0.</p>
199 *
200 * @param mode the blending mode defining the compositing rule
201 * @param alpha the constant alpha to be multiplied with the alpha of the
202 * source. <code>alpha</code> must be a floating point between 0.0 and 1.0.
203 * @throws IllegalArgumentException if the opacity is less than 0.0 or
204 * greater than 1.0
205 * @return a new <code>BlendComposite</code> based on the selected blending
206 * mode and opacity
207 */
208 public static BlendComposite getInstance(BlendingMode mode, float alpha) {
209 return new BlendComposite(mode, alpha);
210 }
211
212 /**
213 * <p>Returns a <code>BlendComposite</code> object that uses the specified
214 * blending mode and this object's alpha value. If the newly specified
215 * blending mode is the same as this object's, this object is returned.</p>
216 *
217 * @param mode the blending mode defining the compositing rule
218 * @return a <code>BlendComposite</code> object derived from this object,
219 * that uses the specified blending mode
220 */
221 public BlendComposite derive(BlendingMode mode) {
222 return this.mode == mode ? this : new BlendComposite(mode, getAlpha());
223 }
224
225 /**
226 * <p>Returns a <code>BlendComposite</code> object that uses the specified
227 * opacity, or alpha, and this object's blending mode. If the newly specified
228 * opacity is the same as this object's, this object is returned.</p>
229 *
230 * @param alpha the constant alpha to be multiplied with the alpha of the
231 * source. <code>alpha</code> must be a floating point between 0.0 and 1.0.
232 * @throws IllegalArgumentException if the opacity is less than 0.0 or
233 * greater than 1.0
234 * @return a <code>BlendComposite</code> object derived from this object,
235 * that uses the specified blending mode
236 */
237 public BlendComposite derive(float alpha) {
238 return this.alpha == alpha ? this : new BlendComposite(getMode(), alpha);
239 }
240
241 /**
242 * <p>Returns the opacity of this composite. If no opacity has been defined,
243 * 1.0 is returned.</p>
244 *
245 * @return the alpha value, or opacity, of this object
246 */
247 public float getAlpha() {
248 return alpha;
249 }
250
251 /**
252 * <p>Returns the blending mode of this composite.</p>
253 *
254 * @return the blending mode used by this object
255 */
256 public BlendingMode getMode() {
257 return mode;
258 }
259
260 /**
261 * {@inheritDoc}
262 */
263 @Override
264 public int hashCode() {
265 return Float.floatToIntBits(alpha) * 31 + mode.ordinal();
266 }
267
268 /**
269 * {@inheritDoc}
270 */
271 @Override
272 public boolean equals(Object obj) {
273 if (!(obj instanceof BlendComposite)) {
274 return false;
275 }
276
277 BlendComposite bc = (BlendComposite) obj;
278 return mode == bc.mode && alpha == bc.alpha;
279 }
280
281 private static boolean isRgbColorModel(ColorModel cm) {
282 if (cm instanceof DirectColorModel &&
283 cm.getTransferType() == DataBuffer.TYPE_INT) {
284 DirectColorModel directCM = (DirectColorModel) cm;
285
286 return directCM.getRedMask() == 0x00FF0000 &&
287 directCM.getGreenMask() == 0x0000FF00 &&
288 directCM.getBlueMask() == 0x000000FF &&
289 (directCM.getNumComponents() == 3 ||
290 directCM.getAlphaMask() == 0xFF000000);
291 }
292
293 return false;
294 }
295
296 private static boolean isBgrColorModel(ColorModel cm) {
297 if (cm instanceof DirectColorModel &&
298 cm.getTransferType() == DataBuffer.TYPE_INT) {
299 DirectColorModel directCM = (DirectColorModel) cm;
300
301 return directCM.getRedMask() == 0x000000FF &&
302 directCM.getGreenMask() == 0x0000FF00 &&
303 directCM.getBlueMask() == 0x00FF0000 &&
304 (directCM.getNumComponents() == 3 ||
305 directCM.getAlphaMask() == 0xFF000000);
306 }
307
308 return false;
309 }
310
311 /**
312 * {@inheritDoc}
313 */
314 public CompositeContext createContext(ColorModel srcColorModel,
315 ColorModel dstColorModel,
316 RenderingHints hints) {
317 if (isRgbColorModel(srcColorModel) && isRgbColorModel(dstColorModel)) {
318 return new BlendingRgbContext(this);
319 } else if (isBgrColorModel(srcColorModel) && isBgrColorModel(dstColorModel)) {
320 return new BlendingBgrContext(this);
321 }
322
323 throw new RasterFormatException("Incompatible color models");
324 }
325
326 private static abstract class BlendingContext implements CompositeContext {
327 protected final Blender blender;
328 protected final BlendComposite composite;
329
330 private BlendingContext(BlendComposite composite) {
331 this.composite = composite;
332 this.blender = Blender.getBlenderFor(composite);
333 }
334
335 public void dispose() {
336 }
337 }
338
339 private static class BlendingRgbContext extends BlendingContext {
340 private BlendingRgbContext(BlendComposite composite) {
341 super(composite);
342 }
343
344 public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
345 int width = Math.min(src.getWidth(), dstIn.getWidth());
346 int height = Math.min(src.getHeight(), dstIn.getHeight());
347
348 float alpha = composite.getAlpha();
349
350 int[] result = new int[4];
351 int[] srcPixel = new int[4];
352 int[] dstPixel = new int[4];
353 int[] srcPixels = new int[width];
354 int[] dstPixels = new int[width];
355
356 for (int y = 0; y < height; y++) {
357 src.getDataElements(0, y, width, 1, srcPixels);
358 dstIn.getDataElements(0, y, width, 1, dstPixels);
359 for (int x = 0; x < width; x++) {
360 // pixels are stored as INT_ARGB
361 // our arrays are [R, G, B, A]
362 int pixel = srcPixels[x];
363 srcPixel[0] = (pixel >> 16) & 0xFF;
364 srcPixel[1] = (pixel >> 8) & 0xFF;
365 srcPixel[2] = (pixel ) & 0xFF;
366 srcPixel[3] = (pixel >> 24) & 0xFF;
367
368 pixel = dstPixels[x];
369 dstPixel[0] = (pixel >> 16) & 0xFF;
370 dstPixel[1] = (pixel >> 8) & 0xFF;
371 dstPixel[2] = (pixel ) & 0xFF;
372 dstPixel[3] = (pixel >> 24) & 0xFF;
373
374 blender.blend(srcPixel, dstPixel, result);
375
376 // mixes the result with the opacity
377 dstPixels[x] = ((int) (dstPixel[3] + (result[3] - dstPixel[3]) * alpha) & 0xFF) << 24 |
378 ((int) (dstPixel[0] + (result[0] - dstPixel[0]) * alpha) & 0xFF) << 16 |
379 ((int) (dstPixel[1] + (result[1] - dstPixel[1]) * alpha) & 0xFF) << 8 |
380 (int) (dstPixel[2] + (result[2] - dstPixel[2]) * alpha) & 0xFF;
381 }
382 dstOut.setDataElements(0, y, width, 1, dstPixels);
383 }
384 }
385 }
386
387 private static class BlendingBgrContext extends BlendingContext {
388 private BlendingBgrContext(BlendComposite composite) {
389 super(composite);
390 }
391
392 public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
393 int width = Math.min(src.getWidth(), dstIn.getWidth());
394 int height = Math.min(src.getHeight(), dstIn.getHeight());
395
396 float alpha = composite.getAlpha();
397
398 int[] result = new int[4];
399 int[] srcPixel = new int[4];
400 int[] dstPixel = new int[4];
401 int[] srcPixels = new int[width];
402 int[] dstPixels = new int[width];
403
404 for (int y = 0; y < height; y++) {
405 src.getDataElements(0, y, width, 1, srcPixels);
406 dstIn.getDataElements(0, y, width, 1, dstPixels);
407 for (int x = 0; x < width; x++) {
408 // pixels are stored as INT_ABGR
409 // our arrays are [R, G, B, A]
410 int pixel = srcPixels[x];
411 srcPixel[0] = (pixel ) & 0xFF;
412 srcPixel[1] = (pixel >> 8) & 0xFF;
413 srcPixel[2] = (pixel >> 16) & 0xFF;
414 srcPixel[3] = (pixel >> 24) & 0xFF;
415
416 pixel = dstPixels[x];
417 dstPixel[0] = (pixel ) & 0xFF;
418 dstPixel[1] = (pixel >> 8) & 0xFF;
419 dstPixel[2] = (pixel >> 16) & 0xFF;
420 dstPixel[3] = (pixel >> 24) & 0xFF;
421
422 blender.blend(srcPixel, dstPixel, result);
423
424 // mixes the result with the opacity
425 dstPixels[x] = ((int) (dstPixel[3] + (result[3] - dstPixel[3]) * alpha) & 0xFF) << 24 |
426 ((int) (dstPixel[0] + (result[0] - dstPixel[0]) * alpha) & 0xFF) |
427 ((int) (dstPixel[1] + (result[1] - dstPixel[1]) * alpha) & 0xFF) << 8 |
428 ((int) (dstPixel[2] + (result[2] - dstPixel[2]) * alpha) & 0xFF) << 16;
429 }
430 dstOut.setDataElements(0, y, width, 1, dstPixels);
431 }
432 }
433 }
434
435 private static abstract class Blender {
436 public abstract void blend(int[] src, int[] dst, int[] result);
437
438 public static Blender getBlenderFor(BlendComposite composite) {
439 switch (composite.getMode()) {
440 case ADD:
441 return new Blender() {
442 @Override
443 public void blend(int[] src, int[] dst, int[] result) {
444 result[0] = Math.min(255, src[0] + dst[0]);
445 result[1] = Math.min(255, src[1] + dst[1]);
446 result[2] = Math.min(255, src[2] + dst[2]);
447 result[3] = Math.min(255, src[3] + dst[3]);
448 }
449 };
450 case AVERAGE:
451 return new Blender() {
452 @Override
453 public void blend(int[] src, int[] dst, int[] result) {
454 result[0] = (src[0] + dst[0]) >> 1;
455 result[1] = (src[1] + dst[1]) >> 1;
456 result[2] = (src[2] + dst[2]) >> 1;
457 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
458 }
459 };
460 case BLUE:
461 return new Blender() {
462 @Override
463 public void blend(int[] src, int[] dst, int[] result) {
464 result[0] = dst[0];
465 result[1] = src[1];
466 result[2] = dst[2];
467 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
468 }
469 };
470 case COLOR:
471 return new Blender() {
472 @Override
473 public void blend(int[] src, int[] dst, int[] result) {
474 float[] srcHSL = new float[3];
475 ColorUtilities.RGBtoHSL(src[0], src[1], src[2], srcHSL);
476 float[] dstHSL = new float[3];
477 ColorUtilities.RGBtoHSL(dst[0], dst[1], dst[2], dstHSL);
478
479 ColorUtilities.HSLtoRGB(srcHSL[0], srcHSL[1], dstHSL[2], result);
480 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
481 }
482 };
483 case COLOR_BURN:
484 return new Blender() {
485 @Override
486 public void blend(int[] src, int[] dst, int[] result) {
487 result[0] = src[0] == 0 ? 0 :
488 Math.max(0, 255 - (((255 - dst[0]) << 8) / src[0]));
489 result[1] = src[1] == 0 ? 0 :
490 Math.max(0, 255 - (((255 - dst[1]) << 8) / src[1]));
491 result[2] = src[2] == 0 ? 0 :
492 Math.max(0, 255 - (((255 - dst[2]) << 8) / src[2]));
493 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
494 }
495 };
496 case COLOR_DODGE:
497 return new Blender() {
498 @Override
499 public void blend(int[] src, int[] dst, int[] result) {
500 result[0] = src[0] == 255 ? 255 :
501 Math.min((dst[0] << 8) / (255 - src[0]), 255);
502 result[1] = src[1] == 255 ? 255 :
503 Math.min((dst[1] << 8) / (255 - src[1]), 255);
504 result[2] = src[2] == 255 ? 255 :
505 Math.min((dst[2] << 8) / (255 - src[2]), 255);
506 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
507 }
508 };
509 case DARKEN:
510 return new Blender() {
511 @Override
512 public void blend(int[] src, int[] dst, int[] result) {
513 result[0] = Math.min(src[0], dst[0]);
514 result[1] = Math.min(src[1], dst[1]);
515 result[2] = Math.min(src[2], dst[2]);
516 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
517 }
518 };
519 case DIFFERENCE:
520 return new Blender() {
521 @Override
522 public void blend(int[] src, int[] dst, int[] result) {
523 result[0] = Math.abs(dst[0] - src[0]);
524 result[1] = Math.abs(dst[1] - src[1]);
525 result[2] = Math.abs(dst[2] - src[2]);
526 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
527 }
528 };
529 case EXCLUSION:
530 return new Blender() {
531 @Override
532 public void blend(int[] src, int[] dst, int[] result) {
533 result[0] = dst[0] + src[0] - (dst[0] * src[0] >> 7);
534 result[1] = dst[1] + src[1] - (dst[1] * src[1] >> 7);
535 result[2] = dst[2] + src[2] - (dst[2] * src[2] >> 7);
536 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
537 }
538 };
539 case FREEZE:
540 return new Blender() {
541 @Override
542 public void blend(int[] src, int[] dst, int[] result) {
543 result[0] = src[0] == 0 ? 0 :
544 Math.max(0, 255 - (255 - dst[0]) * (255 - dst[0]) / src[0]);
545 result[1] = src[1] == 0 ? 0 :
546 Math.max(0, 255 - (255 - dst[1]) * (255 - dst[1]) / src[1]);
547 result[2] = src[2] == 0 ? 0 :
548 Math.max(0, 255 - (255 - dst[2]) * (255 - dst[2]) / src[2]);
549 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
550 }
551 };
552 case GLOW:
553 return new Blender() {
554 @Override
555 public void blend(int[] src, int[] dst, int[] result) {
556 result[0] = dst[0] == 255 ? 255 :
557 Math.min(255, src[0] * src[0] / (255 - dst[0]));
558 result[1] = dst[1] == 255 ? 255 :
559 Math.min(255, src[1] * src[1] / (255 - dst[1]));
560 result[2] = dst[2] == 255 ? 255 :
561 Math.min(255, src[2] * src[2] / (255 - dst[2]));
562 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
563 }
564 };
565 case GREEN:
566 return new Blender() {
567 @Override
568 public void blend(int[] src, int[] dst, int[] result) {
569 result[0] = dst[0];
570 result[1] = dst[1];
571 result[2] = src[2];
572 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
573 }
574 };
575 case HARD_LIGHT:
576 return new Blender() {
577 @Override
578 public void blend(int[] src, int[] dst, int[] result) {
579 result[0] = src[0] < 128 ? dst[0] * src[0] >> 7 :
580 255 - ((255 - src[0]) * (255 - dst[0]) >> 7);
581 result[1] = src[1] < 128 ? dst[1] * src[1] >> 7 :
582 255 - ((255 - src[1]) * (255 - dst[1]) >> 7);
583 result[2] = src[2] < 128 ? dst[2] * src[2] >> 7 :
584 255 - ((255 - src[2]) * (255 - dst[2]) >> 7);
585 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
586 }
587 };
588 case HEAT:
589 return new Blender() {
590 @Override
591 public void blend(int[] src, int[] dst, int[] result) {
592 result[0] = dst[0] == 0 ? 0 :
593 Math.max(0, 255 - (255 - src[0]) * (255 - src[0]) / dst[0]);
594 result[1] = dst[1] == 0 ? 0 :
595 Math.max(0, 255 - (255 - src[1]) * (255 - src[1]) / dst[1]);
596 result[2] = dst[2] == 0 ? 0 :
597 Math.max(0, 255 - (255 - src[2]) * (255 - src[2]) / dst[2]);
598 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
599 }
600 };
601 case HUE:
602 return new Blender() {
603 @Override
604 public void blend(int[] src, int[] dst, int[] result) {
605 float[] srcHSL = new float[3];
606 ColorUtilities.RGBtoHSL(src[0], src[1], src[2], srcHSL);
607 float[] dstHSL = new float[3];
608 ColorUtilities.RGBtoHSL(dst[0], dst[1], dst[2], dstHSL);
609
610 ColorUtilities.HSLtoRGB(srcHSL[0], dstHSL[1], dstHSL[2], result);
611 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
612 }
613 };
614 case INVERSE_COLOR_BURN:
615 return new Blender() {
616 @Override
617 public void blend(int[] src, int[] dst, int[] result) {
618 result[0] = dst[0] == 0 ? 0 :
619 Math.max(0, 255 - (((255 - src[0]) << 8) / dst[0]));
620 result[1] = dst[1] == 0 ? 0 :
621 Math.max(0, 255 - (((255 - src[1]) << 8) / dst[1]));
622 result[2] = dst[2] == 0 ? 0 :
623 Math.max(0, 255 - (((255 - src[2]) << 8) / dst[2]));
624 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
625 }
626 };
627 case INVERSE_COLOR_DODGE:
628 return new Blender() {
629 @Override
630 public void blend(int[] src, int[] dst, int[] result) {
631 result[0] = dst[0] == 255 ? 255 :
632 Math.min((src[0] << 8) / (255 - dst[0]), 255);
633 result[1] = dst[1] == 255 ? 255 :
634 Math.min((src[1] << 8) / (255 - dst[1]), 255);
635 result[2] = dst[2] == 255 ? 255 :
636 Math.min((src[2] << 8) / (255 - dst[2]), 255);
637 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
638 }
639 };
640 case LIGHTEN:
641 return new Blender() {
642 @Override
643 public void blend(int[] src, int[] dst, int[] result) {
644 result[0] = Math.max(src[0], dst[0]);
645 result[1] = Math.max(src[1], dst[1]);
646 result[2] = Math.max(src[2], dst[2]);
647 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
648 }
649 };
650 case LUMINOSITY:
651 return new Blender() {
652 @Override
653 public void blend(int[] src, int[] dst, int[] result) {
654 float[] srcHSL = new float[3];
655 ColorUtilities.RGBtoHSL(src[0], src[1], src[2], srcHSL);
656 float[] dstHSL = new float[3];
657 ColorUtilities.RGBtoHSL(dst[0], dst[1], dst[2], dstHSL);
658
659 ColorUtilities.HSLtoRGB(dstHSL[0], dstHSL[1], srcHSL[2], result);
660 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
661 }
662 };
663 case MULTIPLY:
664 return new Blender() {
665 @Override
666 public void blend(int[] src, int[] dst, int[] result) {
667 result[0] = (src[0] * dst[0]) >> 8;
668 result[1] = (src[1] * dst[1]) >> 8;
669 result[2] = (src[2] * dst[2]) >> 8;
670 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
671 }
672 };
673 case NEGATION:
674 return new Blender() {
675 @Override
676 public void blend(int[] src, int[] dst, int[] result) {
677 result[0] = 255 - Math.abs(255 - dst[0] - src[0]);
678 result[1] = 255 - Math.abs(255 - dst[1] - src[1]);
679 result[2] = 255 - Math.abs(255 - dst[2] - src[2]);
680 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
681 }
682 };
683 case OVERLAY:
684 return new Blender() {
685 @Override
686 public void blend(int[] src, int[] dst, int[] result) {
687 result[0] = dst[0] < 128 ? dst[0] * src[0] >> 7 :
688 255 - ((255 - dst[0]) * (255 - src[0]) >> 7);
689 result[1] = dst[1] < 128 ? dst[1] * src[1] >> 7 :
690 255 - ((255 - dst[1]) * (255 - src[1]) >> 7);
691 result[2] = dst[2] < 128 ? dst[2] * src[2] >> 7 :
692 255 - ((255 - dst[2]) * (255 - src[2]) >> 7);
693 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
694 }
695 };
696 case RED:
697 return new Blender() {
698 @Override
699 public void blend(int[] src, int[] dst, int[] result) {
700 result[0] = src[0];
701 result[1] = dst[1];
702 result[2] = dst[2];
703 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
704 }
705 };
706 case REFLECT:
707 return new Blender() {
708 @Override
709 public void blend(int[] src, int[] dst, int[] result) {
710 result[0] = src[0] == 255 ? 255 :
711 Math.min(255, dst[0] * dst[0] / (255 - src[0]));
712 result[1] = src[1] == 255 ? 255 :
713 Math.min(255, dst[1] * dst[1] / (255 - src[1]));
714 result[2] = src[2] == 255 ? 255 :
715 Math.min(255, dst[2] * dst[2] / (255 - src[2]));
716 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
717 }
718 };
719 case SATURATION:
720 return new Blender() {
721 @Override
722 public void blend(int[] src, int[] dst, int[] result) {
723 float[] srcHSL = new float[3];
724 ColorUtilities.RGBtoHSL(src[0], src[1], src[2], srcHSL);
725 float[] dstHSL = new float[3];
726 ColorUtilities.RGBtoHSL(dst[0], dst[1], dst[2], dstHSL);
727
728 ColorUtilities.HSLtoRGB(dstHSL[0], srcHSL[1], dstHSL[2], result);
729 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
730 }
731 };
732 case SCREEN:
733 return new Blender() {
734 @Override
735 public void blend(int[] src, int[] dst, int[] result) {
736 result[0] = 255 - ((255 - src[0]) * (255 - dst[0]) >> 8);
737 result[1] = 255 - ((255 - src[1]) * (255 - dst[1]) >> 8);
738 result[2] = 255 - ((255 - src[2]) * (255 - dst[2]) >> 8);
739 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
740 }
741 };
742 case SOFT_BURN:
743 return new Blender() {
744 @Override
745 public void blend(int[] src, int[] dst, int[] result) {
746 result[0] = dst[0] + src[0] < 256 ?
747 (dst[0] == 255 ? 255 :
748 Math.min(255, (src[0] << 7) / (255 - dst[0]))) :
749 Math.max(0, 255 - (((255 - dst[0]) << 7) / src[0]));
750 result[1] = dst[1] + src[1] < 256 ?
751 (dst[1] == 255 ? 255 :
752 Math.min(255, (src[1] << 7) / (255 - dst[1]))) :
753 Math.max(0, 255 - (((255 - dst[1]) << 7) / src[1]));
754 result[2] = dst[2] + src[2] < 256 ?
755 (dst[2] == 255 ? 255 :
756 Math.min(255, (src[2] << 7) / (255 - dst[2]))) :
757 Math.max(0, 255 - (((255 - dst[2]) << 7) / src[2]));
758 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
759 }
760 };
761 case SOFT_DODGE:
762 return new Blender() {
763 @Override
764 public void blend(int[] src, int[] dst, int[] result) {
765 result[0] = dst[0] + src[0] < 256 ?
766 (src[0] == 255 ? 255 :
767 Math.min(255, (dst[0] << 7) / (255 - src[0]))) :
768 Math.max(0, 255 - (((255 - src[0]) << 7) / dst[0]));
769 result[1] = dst[1] + src[1] < 256 ?
770 (src[1] == 255 ? 255 :
771 Math.min(255, (dst[1] << 7) / (255 - src[1]))) :
772 Math.max(0, 255 - (((255 - src[1]) << 7) / dst[1]));
773 result[2] = dst[2] + src[2] < 256 ?
774 (src[2] == 255 ? 255 :
775 Math.min(255, (dst[2] << 7) / (255 - src[2]))) :
776 Math.max(0, 255 - (((255 - src[2]) << 7) / dst[2]));
777 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
778 }
779 };
780 case SOFT_LIGHT:
781 return new Blender() {
782 @Override
783 public void blend(int[] src, int[] dst, int[] result) {
784 int mRed = src[0] * dst[0] / 255;
785 int mGreen = src[1] * dst[1] / 255;
786 int mBlue = src[2] * dst[2] / 255;
787 result[0] = mRed + src[0] * (255 - ((255 - src[0]) * (255 - dst[0]) / 255) - mRed) / 255;
788 result[1] = mGreen + src[1] * (255 - ((255 - src[1]) * (255 - dst[1]) / 255) - mGreen) / 255;
789 result[2] = mBlue + src[2] * (255 - ((255 - src[2]) * (255 - dst[2]) / 255) - mBlue) / 255;
790 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
791 }
792 };
793 case STAMP:
794 return new Blender() {
795 @Override
796 public void blend(int[] src, int[] dst, int[] result) {
797 result[0] = Math.max(0, Math.min(255, dst[0] + 2 * src[0] - 256));
798 result[1] = Math.max(0, Math.min(255, dst[1] + 2 * src[1] - 256));
799 result[2] = Math.max(0, Math.min(255, dst[2] + 2 * src[2] - 256));
800 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
801 }
802 };
803 case SUBTRACT:
804 return new Blender() {
805 @Override
806 public void blend(int[] src, int[] dst, int[] result) {
807 result[0] = Math.max(0, src[0] + dst[0] - 256);
808 result[1] = Math.max(0, src[1] + dst[1] - 256);
809 result[2] = Math.max(0, src[2] + dst[2] - 256);
810 result[3] = Math.min(255, src[3] + dst[3] - (src[3] * dst[3]) / 255);
811 }
812 };
813 }
814 throw new IllegalArgumentException("Blender not implemented for " +
815 composite.getMode().name());
816 }
817 }
818 }