001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.imaging;
019
020import java.awt.RenderingHints;
021import java.awt.Transparency;
022import java.awt.color.ColorSpace;
023import java.awt.color.ICC_ColorSpace;
024import java.awt.color.ICC_Profile;
025import java.awt.image.BufferedImage;
026import java.awt.image.ColorConvertOp;
027import java.awt.image.ColorModel;
028import java.awt.image.ComponentColorModel;
029import java.awt.image.DirectColorModel;
030import java.awt.image.ImagingOpException;
031import java.io.File;
032import java.io.IOException;
033
034/**
035 * A selection of tools for evaluating and manipulating color
036 * spaces, color values, etc.
037 * <p>The Javadoc provided in the original code gave the
038 * following notation:<br><br>
039 *
040 * &nbsp;&nbsp;&nbsp; "This class is a mess and needs to be cleaned up."
041 */
042public class ColorTools {
043
044    public BufferedImage correctImage(final BufferedImage src, final File file)
045            throws ImageReadException, IOException {
046        final ICC_Profile icc = Imaging.getICCProfile(file);
047        if (icc == null) {
048            return src;
049        }
050
051        final ICC_ColorSpace cs = new ICC_ColorSpace(icc);
052
053        return convertFromColorSpace(src, cs);
054    }
055
056    public BufferedImage relabelColorSpace(final BufferedImage bi, final ICC_Profile profile)
057            throws ImagingOpException {
058        final ICC_ColorSpace cs = new ICC_ColorSpace(profile);
059
060        return relabelColorSpace(bi, cs);
061    }
062
063    public BufferedImage relabelColorSpace(final BufferedImage bi, final ColorSpace cs)
064            throws ImagingOpException {
065        // This does not do the conversion. It tries to relabel the
066        // BufferedImage
067        // with its actual (presumably correct) Colorspace.
068        // use this when the image is mislabeled, presumably having been
069        // wrongly assumed to be sRGB
070
071        final ColorModel cm = deriveColorModel(bi, cs);
072
073        return relabelColorSpace(bi, cm);
074
075    }
076
077    public BufferedImage relabelColorSpace(final BufferedImage bi, final ColorModel cm)
078            throws ImagingOpException {
079        // This does not do the conversion. It tries to relabel the
080        // BufferedImage
081        // with its actual (presumably correct) Colorspace.
082        // use this when the image is mislabeled, presumably having been
083        // wrongly assumed to be sRGB
084
085        return new BufferedImage(cm, bi.getRaster(), false, null);
086    }
087
088    public ColorModel deriveColorModel(final BufferedImage bi, final ColorSpace cs)
089            throws ImagingOpException {
090        // boolean hasAlpha = (bi.getAlphaRaster() != null);
091        return deriveColorModel(bi, cs, false);
092    }
093
094    public ColorModel deriveColorModel(final BufferedImage bi, final ColorSpace cs,
095            final boolean forceNoAlpha) throws ImagingOpException {
096        return deriveColorModel(bi.getColorModel(), cs, forceNoAlpha);
097    }
098
099    public ColorModel deriveColorModel(final ColorModel colorModel, final ColorSpace cs,
100            final boolean forceNoAlpha) throws ImagingOpException {
101
102        if (colorModel instanceof ComponentColorModel) {
103            final ComponentColorModel ccm = (ComponentColorModel) colorModel;
104            // ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
105            if (forceNoAlpha) {
106                return new ComponentColorModel(cs, false, false,
107                        Transparency.OPAQUE, ccm.getTransferType());
108            }
109            return new ComponentColorModel(cs, ccm.hasAlpha(),
110                    ccm.isAlphaPremultiplied(), ccm.getTransparency(),
111                    ccm.getTransferType());
112        }
113        if (colorModel instanceof DirectColorModel) {
114            final DirectColorModel dcm = (DirectColorModel) colorModel;
115
116            final int oldMask = dcm.getRedMask() | dcm.getGreenMask()
117                    | dcm.getBlueMask() | dcm.getAlphaMask();
118
119            final int oldBits = countBitsInMask(oldMask);
120
121            return new DirectColorModel(cs, oldBits, dcm.getRedMask(),
122                    dcm.getGreenMask(), dcm.getBlueMask(), dcm.getAlphaMask(),
123                    dcm.isAlphaPremultiplied(), dcm.getTransferType());
124        }
125        // else if (old_cm instanceof PackedColorModel)
126        // {
127        // PackedColorModel pcm = (PackedColorModel) old_cm;
128        //
129        // // int old_mask = dcm.getRedMask() | dcm.getGreenMask()
130        // // | dcm.getBlueMask() | dcm.getAlphaMask();
131        //
132        // int old_masks[] = pcm.getMasks();
133        // // System.out.println("old_mask: " + old_mask);
134        // int old_bits = countBitsInMask(old_masks);
135        // // System.out.println("old_bits: " + old_bits);
136        //
137        // // PackedColorModel(ColorSpace space, int bits, int rmask, int gmask,
138        // int bmask, int amask, boolean isAlphaPremultiplied, int trans, int
139        // transferType)
140        // cm = new PackedColorModel(cs, old_bits, pcm.getMasks(),
141        //
142        // pcm.isAlphaPremultiplied(), pcm.getTransparency(), pcm
143        // .getTransferType());
144        // }
145
146        throw new ImagingOpException("Could not clone unknown ColorModel Type.");
147    }
148
149    private int countBitsInMask(int i) {
150        int count = 0;
151        while (i != 0) {
152            count += (i & 1);
153            // uses the unsigned version of java's right shift operator,
154            // so that left hand bits are zeroed.
155            i >>>= 1;
156        }
157        return count;
158    }
159
160    public BufferedImage convertToColorSpace(final BufferedImage bi, final ColorSpace to) {
161        final ColorSpace from = bi.getColorModel().getColorSpace();
162
163        final RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
164                RenderingHints.VALUE_RENDER_QUALITY);
165        hints.put(RenderingHints.KEY_COLOR_RENDERING,
166                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
167        hints.put(RenderingHints.KEY_DITHERING,
168                RenderingHints.VALUE_DITHER_ENABLE);
169
170        final ColorConvertOp op = new ColorConvertOp(from, to, hints);
171
172        BufferedImage result = op.filter(bi, null);
173
174        result = relabelColorSpace(result, to);
175
176        return result;
177    }
178
179    public BufferedImage convertTosRGB(final BufferedImage bi) {
180        final ColorModel srgbCM = ColorModel.getRGBdefault();
181        return convertToColorSpace(bi, srgbCM.getColorSpace());
182    }
183
184    protected BufferedImage convertFromColorSpace(final BufferedImage bi, final ColorSpace from) {
185        final ColorModel srgbCM = ColorModel.getRGBdefault();
186        return convertBetweenColorSpaces(bi, from, srgbCM.getColorSpace());
187    }
188
189    public BufferedImage convertBetweenICCProfiles(final BufferedImage bi, final ICC_Profile from, final ICC_Profile to) {
190        final ICC_ColorSpace csFrom = new ICC_ColorSpace(from);
191        final ICC_ColorSpace csTo = new ICC_ColorSpace(to);
192
193        return convertBetweenColorSpaces(bi, csFrom, csTo);
194    }
195
196    public BufferedImage convertToICCProfile(final BufferedImage bi, final ICC_Profile to) {
197        final ICC_ColorSpace csTo = new ICC_ColorSpace(to);
198        return convertToColorSpace(bi, csTo);
199    }
200
201    public BufferedImage convertBetweenColorSpacesX2(BufferedImage bi,
202            final ColorSpace from, final ColorSpace to) {
203        final RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
204                RenderingHints.VALUE_RENDER_QUALITY);
205        hints.put(RenderingHints.KEY_COLOR_RENDERING,
206                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
207        hints.put(RenderingHints.KEY_DITHERING,
208                RenderingHints.VALUE_DITHER_ENABLE);
209
210        // bi = relabelColorSpace(bi, cs);
211        // dumpColorSpace("\tcs_sRGB", cs_sRGB);
212        // dumpColorSpace("\tColorModel.getRGBdefaultc",
213        // ColorModel.getRGBdefault().getColorSpace());
214
215        bi = relabelColorSpace(bi, from);
216        final ColorConvertOp op = new ColorConvertOp(from, to, hints);
217        bi = op.filter(bi, null);
218
219        bi = relabelColorSpace(bi, from);
220
221        bi = op.filter(bi, null);
222
223        bi = relabelColorSpace(bi, to);
224
225        return bi;
226
227    }
228
229    public BufferedImage convertBetweenColorSpaces(BufferedImage bi,
230            final ColorSpace from, final ColorSpace to) {
231        final RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
232                RenderingHints.VALUE_RENDER_QUALITY);
233        hints.put(RenderingHints.KEY_COLOR_RENDERING,
234                RenderingHints.VALUE_COLOR_RENDER_QUALITY);
235        hints.put(RenderingHints.KEY_DITHERING,
236                RenderingHints.VALUE_DITHER_ENABLE);
237
238        final ColorConvertOp op = new ColorConvertOp(from, to, hints);
239
240        bi = relabelColorSpace(bi, from);
241
242        BufferedImage result = op.filter(bi, null);
243
244        result = relabelColorSpace(result, to);
245
246        return result;
247    }
248
249}