You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/05/23 18:00:40 UTC
svn commit: r1832118 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend:
BlendComposite.java BlendMode.java
Author: tilman
Date: Wed May 23 18:00:39 2018
New Revision: 1832118
URL: http://svn.apache.org/viewvc?rev=1832118&view=rev
Log:
PDFBOX-4095: implement non separable blend modes (hue, saturation, color, luminosity), by Savan Patel and Jani Pehkonen
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendComposite.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendMode.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendComposite.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendComposite.java?rev=1832118&r1=1832117&r2=1832118&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendComposite.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendComposite.java Wed May 23 18:00:39 2018
@@ -1,228 +1,274 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.pdfbox.pdmodel.graphics.blend;
-
-import java.awt.AlphaComposite;
-import java.awt.Composite;
-import java.awt.CompositeContext;
-import java.awt.RenderingHints;
-import java.awt.color.ColorSpace;
-import java.awt.image.ColorModel;
-import java.awt.image.Raster;
-import java.awt.image.WritableRaster;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- * AWT composite for blend modes.
- *
- * @author Kühn & Weyh Software GmbH
- */
-public final class BlendComposite implements Composite
-{
- /**
- * Log instance.
- */
- private static final Log LOG = LogFactory.getLog(BlendComposite.class);
-
- /**
- * Creates a blend composite
- *
- * @param blendMode Desired blend mode
- * @param constantAlpha Constant alpha, must be in the inclusive range
- * [0.0...1.0] or it will be clipped.
- * @return a blend composite.
- */
- public static Composite getInstance(BlendMode blendMode, float constantAlpha)
- {
- if (constantAlpha < 0)
- {
- LOG.warn("using 0 instead of incorrect Alpha " + constantAlpha);
- constantAlpha = 0;
- }
- else if (constantAlpha > 1)
- {
- LOG.warn("using 1 instead of incorrect Alpha " + constantAlpha);
- constantAlpha = 1;
- }
- if (blendMode == BlendMode.NORMAL)
- {
- return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, constantAlpha);
- }
- else
- {
- return new BlendComposite(blendMode, constantAlpha);
- }
- }
-
- // TODO - non-separable blending modes
-
- private final BlendMode blendMode;
- private final float constantAlpha;
-
- private BlendComposite(BlendMode blendMode, float constantAlpha)
- {
- super();
- this.blendMode = blendMode;
- this.constantAlpha = constantAlpha;
- }
-
- @Override
- public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel,
- RenderingHints hints)
- {
- return new BlendCompositeContext(srcColorModel, dstColorModel, hints);
- }
-
- class BlendCompositeContext implements CompositeContext
- {
- private final ColorModel srcColorModel;
- private final ColorModel dstColorModel;
- private final RenderingHints hints;
-
- BlendCompositeContext(ColorModel srcColorModel, ColorModel dstColorModel,
- RenderingHints hints)
- {
- this.srcColorModel = srcColorModel;
- this.dstColorModel = dstColorModel;
- this.hints = hints;
- }
-
- @Override
- public void dispose()
- {
- // nothing needed
- }
-
- @Override
- public void compose(Raster src, Raster dstIn, WritableRaster dstOut)
- {
- int x0 = src.getMinX();
- int y0 = src.getMinY();
- int width = Math.min(Math.min(src.getWidth(), dstIn.getWidth()), dstOut.getWidth());
- int height = Math.min(Math.min(src.getHeight(), dstIn.getHeight()), dstOut.getHeight());
- int x1 = x0 + width;
- int y1 = y0 + height;
- int dstInXShift = dstIn.getMinX() - x0;
- int dstInYShift = dstIn.getMinY() - y0;
- int dstOutXShift = dstOut.getMinX() - x0;
- int dstOutYShift = dstOut.getMinY() - y0;
-
- ColorSpace srcColorSpace = srcColorModel.getColorSpace();
- int numSrcColorComponents = srcColorModel.getNumColorComponents();
- int numSrcComponents = src.getNumBands();
- boolean srcHasAlpha = (numSrcComponents > numSrcColorComponents);
- ColorSpace dstColorSpace = dstColorModel.getColorSpace();
- int numDstColorComponents = dstColorModel.getNumColorComponents();
- int numDstComponents = dstIn.getNumBands();
- boolean dstHasAlpha = (numDstComponents > numDstColorComponents);
-
- int colorSpaceType = dstColorSpace.getType();
- boolean subtractive = (colorSpaceType != ColorSpace.TYPE_RGB)
- && (colorSpaceType != ColorSpace.TYPE_GRAY);
-
- boolean blendModeIsSeparable = blendMode instanceof SeparableBlendMode;
- SeparableBlendMode separableBlendMode = blendModeIsSeparable ?
- (SeparableBlendMode) blendMode : null;
-
- boolean needsColorConversion = !srcColorSpace.equals(dstColorSpace);
-
- Object srcPixel = null;
- Object dstPixel = null;
- float[] srcComponents = new float[numSrcComponents];
- // PDFBOX-3501 let getNormalizedComponents allocate to avoid
- // ArrayIndexOutOfBoundsException for bitonal target
- float[] dstComponents = null;
-
- float[] srcColor = new float[numSrcColorComponents];
- float[] srcConverted;
-
- for (int y = y0; y < y1; y++)
- {
- for (int x = x0; x < x1; x++)
- {
- srcPixel = src.getDataElements(x, y, srcPixel);
- dstPixel = dstIn.getDataElements(dstInXShift + x, dstInYShift + y, dstPixel);
-
- srcComponents = srcColorModel.getNormalizedComponents(srcPixel, srcComponents,
- 0);
- dstComponents = dstColorModel.getNormalizedComponents(dstPixel, dstComponents,
- 0);
-
- float srcAlpha = srcHasAlpha ? srcComponents[numSrcColorComponents] : 1.0f;
- float dstAlpha = dstHasAlpha ? dstComponents[numDstColorComponents] : 1.0f;
-
- srcAlpha = srcAlpha * constantAlpha;
-
- float resultAlpha = dstAlpha + srcAlpha - srcAlpha * dstAlpha;
- float srcAlphaRatio = (resultAlpha > 0) ? srcAlpha / resultAlpha : 0;
-
- // convert color
- System.arraycopy(srcComponents, 0, srcColor, 0, numSrcColorComponents);
- if (needsColorConversion)
- {
- // TODO - very very slow - Hash results???
- float[] cieXYZ = srcColorSpace.toCIEXYZ(srcColor);
- srcConverted = dstColorSpace.fromCIEXYZ(cieXYZ);
- }
- else
- {
- srcConverted = srcColor;
- }
-
- if (separableBlendMode != null)
- {
- for (int k = 0; k < numDstColorComponents; k++)
- {
- float srcValue = srcConverted[k];
- float dstValue = dstComponents[k];
-
- if (subtractive)
- {
- srcValue = 1 - srcValue;
- dstValue = 1 - dstValue;
- }
-
- float value = separableBlendMode.blendChannel(srcValue, dstValue);
- value = srcValue + dstAlpha * (value - srcValue);
- value = dstValue + srcAlphaRatio * (value - dstValue);
-
- if (subtractive)
- {
- value = 1 - value;
- }
-
- dstComponents[k] = value;
- }
- }
- else
- {
- // TODO - nonseparable modes
- }
-
- if (dstHasAlpha)
- {
- dstComponents[numDstColorComponents] = resultAlpha;
- }
-
- dstPixel = dstColorModel.getDataElements(dstComponents, 0, dstPixel);
- dstOut.setDataElements(dstOutXShift + x, dstOutYShift + y, dstPixel);
- }
- }
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.graphics.blend;
+
+import java.awt.AlphaComposite;
+import java.awt.Composite;
+import java.awt.CompositeContext;
+import java.awt.RenderingHints;
+import java.awt.color.ColorSpace;
+import java.awt.image.ColorModel;
+import java.awt.image.Raster;
+import java.awt.image.WritableRaster;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * AWT composite for blend modes.
+ *
+ * @author Kühn & Weyh Software GmbH
+ */
+public final class BlendComposite implements Composite
+{
+ /**
+ * Log instance.
+ */
+ private static final Log LOG = LogFactory.getLog(BlendComposite.class);
+
+ /**
+ * Creates a blend composite
+ *
+ * @param blendMode Desired blend mode
+ * @param constantAlpha Constant alpha, must be in the inclusive range
+ * [0.0...1.0] or it will be clipped.
+ * @return a blend composite.
+ */
+ public static Composite getInstance(BlendMode blendMode, float constantAlpha)
+ {
+ if (constantAlpha < 0)
+ {
+ LOG.warn("using 0 instead of incorrect Alpha " + constantAlpha);
+ constantAlpha = 0;
+ }
+ else if (constantAlpha > 1)
+ {
+ LOG.warn("using 1 instead of incorrect Alpha " + constantAlpha);
+ constantAlpha = 1;
+ }
+ if (blendMode == BlendMode.NORMAL)
+ {
+ return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, constantAlpha);
+ }
+ else
+ {
+ return new BlendComposite(blendMode, constantAlpha);
+ }
+ }
+
+ private final BlendMode blendMode;
+ private final float constantAlpha;
+
+ private BlendComposite(BlendMode blendMode, float constantAlpha)
+ {
+ super();
+ this.blendMode = blendMode;
+ this.constantAlpha = constantAlpha;
+ }
+
+ @Override
+ public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel,
+ RenderingHints hints)
+ {
+ return new BlendCompositeContext(srcColorModel, dstColorModel, hints);
+ }
+
+ class BlendCompositeContext implements CompositeContext
+ {
+ private final ColorModel srcColorModel;
+ private final ColorModel dstColorModel;
+ private final RenderingHints hints;
+
+ BlendCompositeContext(ColorModel srcColorModel, ColorModel dstColorModel,
+ RenderingHints hints)
+ {
+ this.srcColorModel = srcColorModel;
+ this.dstColorModel = dstColorModel;
+ this.hints = hints;
+ }
+
+ @Override
+ public void dispose()
+ {
+ // nothing needed
+ }
+
+ @Override
+ public void compose(Raster src, Raster dstIn, WritableRaster dstOut)
+ {
+ int x0 = src.getMinX();
+ int y0 = src.getMinY();
+ int width = Math.min(Math.min(src.getWidth(), dstIn.getWidth()), dstOut.getWidth());
+ int height = Math.min(Math.min(src.getHeight(), dstIn.getHeight()), dstOut.getHeight());
+ int x1 = x0 + width;
+ int y1 = y0 + height;
+ int dstInXShift = dstIn.getMinX() - x0;
+ int dstInYShift = dstIn.getMinY() - y0;
+ int dstOutXShift = dstOut.getMinX() - x0;
+ int dstOutYShift = dstOut.getMinY() - y0;
+
+ ColorSpace srcColorSpace = srcColorModel.getColorSpace();
+ int numSrcColorComponents = srcColorModel.getNumColorComponents();
+ int numSrcComponents = src.getNumBands();
+ boolean srcHasAlpha = (numSrcComponents > numSrcColorComponents);
+ ColorSpace dstColorSpace = dstColorModel.getColorSpace();
+ int numDstColorComponents = dstColorModel.getNumColorComponents();
+ int numDstComponents = dstIn.getNumBands();
+ boolean dstHasAlpha = (numDstComponents > numDstColorComponents);
+
+ int srcColorSpaceType = srcColorSpace.getType();
+ int dstColorSpaceType = dstColorSpace.getType();
+ boolean subtractive = (dstColorSpaceType != ColorSpace.TYPE_RGB)
+ && (dstColorSpaceType != ColorSpace.TYPE_GRAY);
+
+ boolean blendModeIsSeparable = blendMode instanceof SeparableBlendMode;
+ SeparableBlendMode separableBlendMode = blendModeIsSeparable ?
+ (SeparableBlendMode) blendMode : null;
+ NonSeparableBlendMode nonSeparableBlendMode = !blendModeIsSeparable ?
+ (NonSeparableBlendMode) blendMode : null;
+
+ boolean needsColorConversion = !srcColorSpace.equals(dstColorSpace);
+
+ Object srcPixel = null;
+ Object dstPixel = null;
+ float[] srcComponents = new float[numSrcComponents];
+ // PDFBOX-3501 let getNormalizedComponents allocate to avoid
+ // ArrayIndexOutOfBoundsException for bitonal target
+ float[] dstComponents = null;
+
+ float[] srcColor = new float[numSrcColorComponents];
+ float[] srcConverted;
+ float[] dstConverted;
+ float[] rgbResult = blendModeIsSeparable ? null : new float[dstHasAlpha ? 4 : 3];
+
+ for (int y = y0; y < y1; y++)
+ {
+ for (int x = x0; x < x1; x++)
+ {
+ srcPixel = src.getDataElements(x, y, srcPixel);
+ dstPixel = dstIn.getDataElements(dstInXShift + x, dstInYShift + y, dstPixel);
+
+ srcComponents = srcColorModel.getNormalizedComponents(srcPixel, srcComponents,
+ 0);
+ dstComponents = dstColorModel.getNormalizedComponents(dstPixel, dstComponents,
+ 0);
+
+ float srcAlpha = srcHasAlpha ? srcComponents[numSrcColorComponents] : 1.0f;
+ float dstAlpha = dstHasAlpha ? dstComponents[numDstColorComponents] : 1.0f;
+
+ srcAlpha = srcAlpha * constantAlpha;
+
+ float resultAlpha = dstAlpha + srcAlpha - srcAlpha * dstAlpha;
+ float srcAlphaRatio = (resultAlpha > 0) ? srcAlpha / resultAlpha : 0;
+
+ if (separableBlendMode != null)
+ {
+ // convert color
+ System.arraycopy(srcComponents, 0, srcColor, 0, numSrcColorComponents);
+ if (needsColorConversion)
+ {
+ // TODO - very very slow - Hash results???
+ float[] cieXYZ = srcColorSpace.toCIEXYZ(srcColor);
+ srcConverted = dstColorSpace.fromCIEXYZ(cieXYZ);
+ }
+ else
+ {
+ srcConverted = srcColor;
+ }
+
+ for (int k = 0; k < numDstColorComponents; k++)
+ {
+ float srcValue = srcConverted[k];
+ float dstValue = dstComponents[k];
+
+ if (subtractive)
+ {
+ srcValue = 1 - srcValue;
+ dstValue = 1 - dstValue;
+ }
+
+ float value = separableBlendMode.blendChannel(srcValue, dstValue);
+ value = srcValue + dstAlpha * (value - srcValue);
+ value = dstValue + srcAlphaRatio * (value - dstValue);
+
+ if (subtractive)
+ {
+ value = 1 - value;
+ }
+
+ dstComponents[k] = value;
+ }
+ }
+ else
+ {
+ // Nonseparable blend modes are computed in RGB color space.
+ // TODO - CMYK color spaces need special treatment.
+
+ if (srcColorSpaceType == ColorSpace.TYPE_RGB)
+ {
+ srcConverted = srcComponents;
+ }
+ else
+ {
+ srcConverted = srcColorSpace.toRGB(srcComponents);
+ }
+
+ if (dstColorSpaceType == ColorSpace.TYPE_RGB)
+ {
+ dstConverted = dstComponents;
+ }
+ else
+ {
+ dstConverted = dstColorSpace.toRGB(dstComponents);
+ }
+
+ nonSeparableBlendMode.blend(srcConverted, dstConverted, rgbResult);
+
+ for (int k = 0; k < 3; k++)
+ {
+ float srcValue = srcConverted[k];
+ float dstValue = dstConverted[k];
+ float value = rgbResult[k];
+ value = Math.max(Math.min(value, 1.0f), 0.0f);
+ value = srcValue + dstAlpha * (value - srcValue);
+ value = dstValue + srcAlphaRatio * (value - dstValue);
+ rgbResult[k] = value;
+ }
+
+ if (dstColorSpaceType == ColorSpace.TYPE_RGB)
+ {
+ System.arraycopy(rgbResult, 0, dstComponents, 0, dstComponents.length);
+ }
+ else
+ {
+ float[] temp = dstColorSpace.fromRGB(rgbResult);
+ System.arraycopy(temp, 0, dstComponents, 0,
+ Math.min(dstComponents.length, temp.length));
+ }
+ }
+
+ if (dstHasAlpha)
+ {
+ dstComponents[numDstColorComponents] = resultAlpha;
+ }
+
+ dstPixel = dstColorModel.getDataElements(dstComponents, 0, dstPixel);
+ dstOut.setDataElements(dstOutXShift + x, dstOutYShift + y, dstPixel);
+ }
+ }
+ }
+ }
+}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendMode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendMode.java?rev=1832118&r1=1832117&r2=1832118&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendMode.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/blend/BlendMode.java Wed May 23 18:00:39 2018
@@ -213,6 +213,169 @@ public abstract class BlendMode
}
};
+ public static final NonSeparableBlendMode HUE = new NonSeparableBlendMode()
+ {
+ @Override
+ public void blend(float[] srcValues, float[] dstValues, float[] result)
+ {
+ float[] temp = new float[3];
+ getSaturationRGB(dstValues, srcValues, temp);
+ getLuminosityRGB(dstValues, temp, result);
+ }
+ };
+
+ public static final NonSeparableBlendMode SATURATION = new NonSeparableBlendMode()
+ {
+ @Override
+ public void blend(float[] srcValues, float[] dstValues, float[] result)
+ {
+ getSaturationRGB(srcValues, dstValues, result);
+ }
+ };
+
+ public static final NonSeparableBlendMode COLOR = new NonSeparableBlendMode()
+ {
+ @Override
+ public void blend(float[] srcValues, float[] dstValues, float[] result)
+ {
+ getLuminosityRGB(dstValues, srcValues, result);
+ }
+ };
+
+ public static final NonSeparableBlendMode LUMINOSITY = new NonSeparableBlendMode()
+ {
+ @Override
+ public void blend(float[] srcValues, float[] dstValues, float[] result)
+ {
+ getLuminosityRGB(srcValues, dstValues, result);
+ }
+ };
+
+ private static int get255Value(float val)
+ {
+ return (int) Math.floor(val >= 1.0 ? 255 : val * 255.0);
+ }
+
+ private static void getSaturationRGB(float[] srcValues, float[] dstValues, float[] result)
+ {
+ int minb;
+ int maxb;
+ int mins;
+ int maxs;
+ int y;
+ int scale;
+ int r;
+ int g;
+ int b;
+
+ int rd = get255Value(dstValues[0]);
+ int gd = get255Value(dstValues[1]);
+ int bd = get255Value(dstValues[2]);
+ int rs = get255Value(srcValues[0]);
+ int gs = get255Value(srcValues[1]);
+ int bs = get255Value(srcValues[2]);
+
+ minb = Math.min(rd, Math.min(gd, bd));
+ maxb = Math.max(rd, Math.max(gd, bd));
+ if (minb == maxb)
+ {
+ /* backdrop has zero saturation, avoid divide by 0 */
+ result[0] = gd / 255.0f;
+ result[1] = gd / 255.0f;
+ result[2] = gd / 255.0f;
+ return;
+ }
+
+ mins = Math.min(rs, Math.min(gs, bs));
+ maxs = Math.max(rs, Math.max(gs, bs));
+
+ scale = ((maxs - mins) << 16) / (maxb - minb);
+ y = (rd * 77 + gd * 151 + bd * 28 + 0x80) >> 8;
+ r = y + ((((rd - y) * scale) + 0x8000) >> 16);
+ g = y + ((((gd - y) * scale) + 0x8000) >> 16);
+ b = y + ((((bd - y) * scale) + 0x8000) >> 16);
+
+ if (((r | g | b) & 0x100) == 0x100)
+ {
+ int scalemin;
+ int scalemax;
+ int min;
+ int max;
+
+ min = Math.min(r, Math.min(g, b));
+ max = Math.max(r, Math.max(g, b));
+
+ if (min < 0)
+ {
+ scalemin = (y << 16) / (y - min);
+ }
+ else
+ {
+ scalemin = 0x10000;
+ }
+
+ if (max > 255)
+ {
+ scalemax = ((255 - y) << 16) / (max - y);
+ }
+ else
+ {
+ scalemax = 0x10000;
+ }
+
+ scale = Math.min(scalemin, scalemax);
+ r = y + (((r - y) * scale + 0x8000) >> 16);
+ g = y + (((g - y) * scale + 0x8000) >> 16);
+ b = y + (((b - y) * scale + 0x8000) >> 16);
+ }
+ result[0] = r / 255.0f;
+ result[1] = g / 255.0f;
+ result[2] = b / 255.0f;
+ }
+
+ private static void getLuminosityRGB(float[] srcValues, float[] dstValues, float[] result)
+ {
+ int delta;
+ int scale;
+ int r;
+ int g;
+ int b;
+ int y;
+ int rd = get255Value(dstValues[0]);
+ int gd = get255Value(dstValues[1]);
+ int bd = get255Value(dstValues[2]);
+ int rs = get255Value(srcValues[0]);
+ int gs = get255Value(srcValues[1]);
+ int bs = get255Value(srcValues[2]);
+ delta = ((rs - rd) * 77 + (gs - gd) * 151 + (bs - bd) * 28 + 0x80) >> 8;
+ r = (rd + delta);
+ g = (gd + delta);
+ b = (bd + delta);
+
+ if (((r | g | b) & 0x100) == 0x100)
+ {
+ y = (rs * 77 + gs * 151 + bs * 28 + 0x80) >> 8;
+ if (delta > 0)
+ {
+ int max;
+ max = Math.max(r, Math.max(g, b));
+ scale = (max == y ? 0 : ((255 - y) << 16) / (max - y));
+ }
+ else
+ {
+ int min;
+ min = Math.min(r, Math.min(g, b));
+ scale = (y == min ? 0 : (y << 16) / (y - min));
+ }
+ r = y + (((r - y) * scale + 0x8000) >> 16);
+ g = y + (((g - y) * scale + 0x8000) >> 16);
+ b = y + (((b - y) * scale + 0x8000) >> 16);
+ }
+ result[0] = r / 255.0f;
+ result[1] = g / 255.0f;
+ result[2] = b / 255.0f;
+ }
+
// these maps *must* come after the declarations above, otherwise its values will be null
private static final Map<COSName, BlendMode> BLEND_MODES = createBlendModeMap();
private static final Map<BlendMode, COSName> BLEND_MODE_NAMES = createBlendModeNamesMap();
@@ -233,8 +396,10 @@ public abstract class BlendMode
map.put(COSName.SOFT_LIGHT, BlendMode.SOFT_LIGHT);
map.put(COSName.DIFFERENCE, BlendMode.DIFFERENCE);
map.put(COSName.EXCLUSION, BlendMode.EXCLUSION);
- // TODO - non-separable blending modes
- // hue saturation color luminosity
+ map.put(COSName.HUE, BlendMode.HUE);
+ map.put(COSName.SATURATION, BlendMode.SATURATION);
+ map.put(COSName.LUMINOSITY, BlendMode.LUMINOSITY);
+ map.put(COSName.COLOR, BlendMode.COLOR);
return map;
}
@@ -254,7 +419,10 @@ public abstract class BlendMode
map.put(BlendMode.SOFT_LIGHT, COSName.SOFT_LIGHT);
map.put(BlendMode.DIFFERENCE, COSName.DIFFERENCE);
map.put(BlendMode.EXCLUSION, COSName.EXCLUSION);
- // TODO - non-separable blending modes
+ map.put(BlendMode.HUE, COSName.HUE);
+ map.put(BlendMode.SATURATION, COSName.SATURATION);
+ map.put(BlendMode.LUMINOSITY, COSName.LUMINOSITY);
+ map.put(BlendMode.COLOR, COSName.COLOR);
return map;
}