001/*
002 * $Id: ColorTintFilter.java 4082 2011-11-15 18:39:43Z kschaefe $
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
035package org.jdesktop.swingx.image;
036
037import java.awt.Color;
038import java.awt.image.BufferedImage;
039
040import org.jdesktop.swingx.util.GraphicsUtilities;
041
042/**
043 * <p>A color tint filter can be used to mix a solid color to an image. The
044 * result is an image tinted by the specified color. The force of the effect
045 * can be controlled with the <code>mixValue</code>, a number between  0.0 and
046 * 1.0 that can be seen as the percentage of the mix (0.0 does not affect the
047 * source image and 1.0 replaces all the pixels by the solid color).</p>
048 * <p>The color of the pixels in the resulting image is computed as follows:</p>
049 * <pre>
050 * cR = cS * (1 - mixValue) + cM * mixValue
051 * </pre>
052 * <p>Definition of the parameters:</p>
053 * <ul>
054 *   <li><code>cR</code>: color of the resulting pixel</li>
055 *   <li><code>cS</code>: color of the source pixel</li>
056 *   <li><code>cM</code>: the solid color to mix with the source image</li>
057 *   <li><code>mixValue</code>: strength of the mix, a value between 0.0 and 1.0</li>
058 * </ul>
059 *
060 * @author Romain Guy <romain.guy@mac.com>
061 */
062
063public class ColorTintFilter extends AbstractFilter {
064    private final Color mixColor;
065    private final float mixValue;
066
067    private int[] preMultipliedRed;
068    private int[] preMultipliedGreen;
069    private int[] preMultipliedBlue;
070
071    /**
072     * <p>Creates a new color mixer filter. The specified color will be used
073     * to tint the source image, with a mixing strength defined by
074     * <code>mixValue</code>.</p>
075     *
076     * @param mixColor the solid color to mix with the source image
077     * @param mixValue the strength of the mix, between 0.0 and 1.0; if the
078     *   specified value lies outside this range, it is clamped
079     * @throws IllegalArgumentException if <code>mixColor</code> is null
080     */
081    public ColorTintFilter(Color mixColor, float mixValue) {
082        if (mixColor == null) {
083            throw new IllegalArgumentException("mixColor cannot be null");
084        }
085
086        this.mixColor = mixColor;
087        if (mixValue < 0.0f) {
088            mixValue = 0.0f;
089        } else if (mixValue > 1.0f) {
090            mixValue = 1.0f;
091        }
092        this.mixValue = mixValue;
093
094        int mix_r = (int) (mixColor.getRed()   * mixValue);
095        int mix_g = (int) (mixColor.getGreen() * mixValue);
096        int mix_b = (int) (mixColor.getBlue()  * mixValue);
097
098        // Since we use only lookup tables to apply the filter, this filter
099        // could be implemented as a LookupOp.
100        float factor = 1.0f - mixValue;
101        preMultipliedRed   = new int[256];
102        preMultipliedGreen = new int[256];
103        preMultipliedBlue  = new int[256];
104
105        for (int i = 0; i < 256; i++) {
106            int value = (int) (i * factor);
107            preMultipliedRed[i]   = value + mix_r;
108            preMultipliedGreen[i] = value + mix_g;
109            preMultipliedBlue[i]  = value + mix_b;
110        }
111    }
112
113    /**
114     * <p>Returns the mix value of this filter.</p>
115     *
116     * @return the mix value, between 0.0 and 1.0
117     */
118    public float getMixValue() {
119        return mixValue;
120    }
121
122    /**
123     * <p>Returns the solid mix color of this filter.</p>
124     *
125     * @return the solid color used for mixing
126     */
127    public Color getMixColor() {
128        return mixColor;
129    }
130
131    /**
132     * {@inheritDoc}
133     */
134    @Override
135    public BufferedImage filter(BufferedImage src, BufferedImage dst) {
136        if (dst == null) {
137            dst = createCompatibleDestImage(src, null);
138        }
139
140        int width = src.getWidth();
141        int height = src.getHeight();
142
143        int[] pixels = new int[width * height];
144        GraphicsUtilities.getPixels(src, 0, 0, width, height, pixels);
145        mixColor(pixels);
146        GraphicsUtilities.setPixels(dst, 0, 0, width, height, pixels);
147
148        return dst;
149    }
150
151    private void mixColor(int[] pixels) {
152        for (int i = 0; i < pixels.length; i++) {
153            int argb = pixels[i];
154            pixels[i] = (argb & 0xFF000000) |
155                        preMultipliedRed[(argb >> 16)   & 0xFF] << 16 |
156                        preMultipliedGreen[(argb >> 8)  & 0xFF] <<  8 |
157                        preMultipliedBlue[argb & 0xFF];
158        }
159    }
160}