一、前言
关于apache的commons-imaging的org.apache.commons.imaging.ColorTools颜色工具类,对java.awt.image.BufferedImage校正correctImage处理、转java.awt.image.ColorModel处理deriveColorModel等操作。
二、源码说明
package org.apache.commons.imaging;@b@@b@import java.awt.RenderingHints;@b@import java.awt.color.ColorSpace;@b@import java.awt.color.ICC_ColorSpace;@b@import java.awt.color.ICC_Profile;@b@import java.awt.image.BufferedImage;@b@import java.awt.image.ColorConvertOp;@b@import java.awt.image.ColorModel;@b@import java.awt.image.ComponentColorModel;@b@import java.awt.image.DirectColorModel;@b@import java.awt.image.ImagingOpException;@b@import java.io.File;@b@import java.io.IOException;@b@@b@public class ColorTools@b@{@b@ public BufferedImage correctImage(BufferedImage src, File file)@b@ throws ImageReadException, IOException@b@ {@b@ ICC_Profile icc = Imaging.getICCProfile(file);@b@ if (icc == null) {@b@ return src;@b@ }@b@@b@ ICC_ColorSpace cs = new ICC_ColorSpace(icc);@b@@b@ BufferedImage dst = convertFromColorSpace(src, cs);@b@ return dst;@b@ }@b@@b@ public BufferedImage relabelColorSpace(BufferedImage bi, ICC_Profile profile) throws ImagingOpException@b@ {@b@ ICC_ColorSpace cs = new ICC_ColorSpace(profile);@b@@b@ return relabelColorSpace(bi, cs);@b@ }@b@@b@ public BufferedImage relabelColorSpace(BufferedImage bi, ColorSpace cs)@b@ throws ImagingOpException@b@ {@b@ ColorModel cm = deriveColorModel(bi, cs);@b@@b@ return relabelColorSpace(bi, cm);@b@ }@b@@b@ public BufferedImage relabelColorSpace(BufferedImage bi, ColorModel cm)@b@ throws ImagingOpException@b@ {@b@ BufferedImage result = new BufferedImage(cm, bi.getRaster(), false, null);@b@@b@ return result;@b@ }@b@@b@ public ColorModel deriveColorModel(BufferedImage bi, ColorSpace cs)@b@ throws ImagingOpException@b@ {@b@ return deriveColorModel(bi, cs, false);@b@ }@b@@b@ public ColorModel deriveColorModel(BufferedImage bi, ColorSpace cs, boolean force_no_alpha) throws ImagingOpException@b@ {@b@ return deriveColorModel(bi.getColorModel(), cs, force_no_alpha);@b@ }@b@@b@ public ColorModel deriveColorModel(ColorModel old_cm, ColorSpace cs, boolean force_no_alpha)@b@ throws ImagingOpException@b@ {@b@ if (old_cm instanceof ComponentColorModel) {@b@ ComponentColorModel ccm = (ComponentColorModel)old_cm;@b@@b@ if (force_no_alpha) {@b@ return new ComponentColorModel(cs, false, false, 1, ccm.getTransferType());@b@ }@b@@b@ return new ComponentColorModel(cs, ccm.hasAlpha(), ccm.isAlphaPremultiplied(), ccm.getTransparency(), ccm.getTransferType());@b@ }@b@@b@ if (old_cm instanceof DirectColorModel) {@b@ DirectColorModel dcm = (DirectColorModel)old_cm;@b@@b@ int old_mask = dcm.getRedMask() | dcm.getGreenMask() | dcm.getBlueMask() | dcm.getAlphaMask();@b@@b@ int old_bits = count_bits_in_mask(old_mask);@b@@b@ return new DirectColorModel(cs, old_bits, dcm.getRedMask(), dcm.getGreenMask(), dcm.getBlueMask(), dcm.getAlphaMask(), dcm.isAlphaPremultiplied(), dcm.getTransferType());@b@ }@b@@b@ throw new ImagingOpException("Could not clone unknown ColorModel Type.");@b@ }@b@@b@ private int count_bits_in_mask(int i) {@b@ int count = 0;@b@ while (i != 0) {@b@ count += (i & 0x1);@b@@b@ i >>>= 1;@b@ }@b@ return count;@b@ }@b@@b@ public BufferedImage convertToColorSpace(BufferedImage bi, ColorSpace to) {@b@ ColorSpace from = bi.getColorModel().getColorSpace();@b@@b@ RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);@b@@b@ ColorConvertOp op = new ColorConvertOp(from, to, hints);@b@@b@ BufferedImage result = op.filter(bi, null);@b@@b@ result = relabelColorSpace(result, to);@b@@b@ return result;@b@ }@b@@b@ public BufferedImage convertTosRGB(BufferedImage bi) {@b@ ColorSpace cs_sRGB = ColorSpace.getInstance(1000);@b@@b@ ColorModel srgbCM = ColorModel.getRGBdefault();@b@ cs_sRGB = srgbCM.getColorSpace();@b@@b@ return convertToColorSpace(bi, cs_sRGB);@b@ }@b@@b@ protected BufferedImage convertFromColorSpace(BufferedImage bi, ColorSpace from)@b@ {@b@ ColorModel srgbCM = ColorModel.getRGBdefault();@b@ ColorSpace cs_sRGB = srgbCM.getColorSpace();@b@@b@ return convertBetweenColorSpaces(bi, from, cs_sRGB);@b@ }@b@@b@ public BufferedImage convertBetweenICCProfiles(BufferedImage bi, ICC_Profile from, ICC_Profile to)@b@ {@b@ ICC_ColorSpace cs_from = new ICC_ColorSpace(from);@b@ ICC_ColorSpace cs_to = new ICC_ColorSpace(to);@b@@b@ return convertBetweenColorSpaces(bi, cs_from, cs_to);@b@ }@b@@b@ public BufferedImage convertToICCProfile(BufferedImage bi, ICC_Profile to) {@b@ ICC_ColorSpace cs_to = new ICC_ColorSpace(to);@b@@b@ return convertToColorSpace(bi, cs_to);@b@ }@b@@b@ public BufferedImage convertBetweenColorSpacesX2(BufferedImage bi, ColorSpace from, ColorSpace to)@b@ {@b@ RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);@b@@b@ bi = relabelColorSpace(bi, from);@b@ ColorConvertOp op = new ColorConvertOp(from, to, hints);@b@ bi = op.filter(bi, null);@b@@b@ bi = relabelColorSpace(bi, from);@b@@b@ bi = op.filter(bi, null);@b@@b@ bi = relabelColorSpace(bi, to);@b@@b@ return bi;@b@ }@b@@b@ public BufferedImage convertBetweenColorSpaces(BufferedImage bi, ColorSpace from, ColorSpace to)@b@ {@b@ RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);@b@@b@ hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);@b@@b@ ColorConvertOp op = new ColorConvertOp(from, to, hints);@b@@b@ bi = relabelColorSpace(bi, from);@b@@b@ BufferedImage result = op.filter(bi, null);@b@@b@ result = relabelColorSpace(result, to);@b@@b@ return result;@b@ }@b@}