001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.draw; 003 004import java.awt.Composite; 005import java.awt.CompositeContext; 006import java.awt.RenderingHints; 007import java.awt.image.ColorModel; 008import java.awt.image.Raster; 009import java.awt.image.WritableRaster; 010import java.util.EnumMap; 011import java.util.Map; 012 013/** 014 * A {@link Composite} implementing the common "blend modes" of image editors (multiply, burn, hard light, 015 * difference, divide), which the standard {@link java.awt.AlphaComposite} does not provide. 016 * <p> 017 * The source alpha (including the coverage produced by antialiasing) controls how strongly the blended color 018 * replaces the destination, following the "source over" rule of the W3C compositing model, so that a 019 * translucent destination is handled correctly as well. It works on any color model, but is implemented per 020 * pixel and thus only meant for drawing thin shapes such as grid lines. It must be used on 021 * {@link java.awt.image.BufferedImage} backed graphics, since hardware accelerated pipelines do not support 022 * custom composites. 023 * @since 19640 024 */ 025public final class BlendComposite implements Composite { 026 027 /** 028 * The supported blend modes. 029 */ 030 public enum Mode { 031 /** Normal alpha blending (like {@link java.awt.AlphaComposite#SrcOver}) */ 032 NORMAL, 033 /** Multiplies source and destination: darkens, white is neutral */ 034 MULTIPLY, 035 /** Color burn: darkens and increases contrast, white is neutral */ 036 BURN, 037 /** Hard light: multiplies for dark sources, screens for light sources; strong contrast */ 038 HARD_LIGHT, 039 /** Absolute difference between source and destination: always visible on any background */ 040 DIFFERENCE, 041 /** Divides destination by source: lightens, white is neutral, dark sources give bright lines */ 042 DIVIDE 043 } 044 045 private static final Map<Mode, BlendComposite> INSTANCES = new EnumMap<>(Mode.class); 046 047 private final Mode mode; 048 049 private BlendComposite(Mode mode) { 050 this.mode = mode; 051 } 052 053 /** 054 * Returns the composite for the given mode. 055 * @param mode the blend mode 056 * @return the composite 057 */ 058 public static synchronized BlendComposite getInstance(Mode mode) { 059 return INSTANCES.computeIfAbsent(mode, BlendComposite::new); 060 } 061 062 /** 063 * Returns the blend mode of this composite. 064 * @return the blend mode 065 */ 066 public Mode getMode() { 067 return mode; 068 } 069 070 @Override 071 public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) { 072 return new BlendContext(mode, srcColorModel, dstColorModel); 073 } 074 075 /** 076 * Blends one color channel. 077 * @param mode blend mode 078 * @param s source channel value (0-255) 079 * @param d destination channel value (0-255) 080 * @return blended value (0-255) 081 */ 082 static int blend(Mode mode, int s, int d) { 083 switch (mode) { 084 case MULTIPLY: 085 return s * d / 255; 086 case BURN: 087 return s == 0 ? 0 : 255 - Math.min(255, (255 - d) * 255 / s); 088 case HARD_LIGHT: 089 return s < 128 ? 2 * s * d / 255 : 255 - (255 - d) * (510 - 2 * s) / 255; 090 case DIFFERENCE: 091 return Math.abs(s - d); 092 case DIVIDE: 093 return s == 0 ? 255 : Math.min(255, d * 255 / s); 094 case NORMAL: 095 default: 096 return s; 097 } 098 } 099 100 /** 101 * Composes one pixel: blends the source color with the destination and combines the two with the 102 * "source over" rule, so that both the source alpha (including the coverage produced by antialiasing) 103 * and a translucent destination are handled correctly. 104 * @param mode blend mode 105 * @param s source color (ARGB, not premultiplied) 106 * @param d destination color (ARGB, not premultiplied) 107 * @return the resulting color (ARGB, not premultiplied) 108 */ 109 static int composePixel(Mode mode, int s, int d) { 110 int sa = s >>> 24; 111 if (sa == 0) { 112 return d; 113 } 114 int da = d >>> 24; 115 if (da == 0xff) { 116 // opaque destination (the map view): the result is the destination moved towards the blended color 117 return 0xff000000 118 | (mix(blend(mode, (s >> 16) & 0xff, (d >> 16) & 0xff), (d >> 16) & 0xff, sa) << 16) 119 | (mix(blend(mode, (s >> 8) & 0xff, (d >> 8) & 0xff), (d >> 8) & 0xff, sa) << 8) 120 | mix(blend(mode, s & 0xff, d & 0xff), d & 0xff, sa); 121 } 122 int a = sa + da * (0xff - sa) / 0xff; 123 if (a == 0) { 124 return 0; 125 } 126 return (a << 24) 127 | (composeChannel(mode, (s >> 16) & 0xff, (d >> 16) & 0xff, sa, da, a) << 16) 128 | (composeChannel(mode, (s >> 8) & 0xff, (d >> 8) & 0xff, sa, da, a) << 8) 129 | composeChannel(mode, s & 0xff, d & 0xff, sa, da, a); 130 } 131 132 /** 133 * Composes one color channel of a translucent destination, see 134 * <a href="https://www.w3.org/TR/compositing-1/#blending">the W3C compositing model</a>: 135 * {@code co = as*(1-ab)*Cs + as*ab*B(Cb,Cs) + (1-as)*ab*Cb} and {@code Co = co/ao}. 136 * @param mode blend mode 137 * @param cs source channel value (0-255) 138 * @param cb destination (backdrop) channel value (0-255) 139 * @param sa source alpha (0-255) 140 * @param da destination alpha (0-255) 141 * @param a the resulting alpha (0-255), must not be 0 142 * @return the resulting channel value (0-255) 143 */ 144 private static int composeChannel(Mode mode, int cs, int cb, int sa, int da, int a) { 145 int co = sa * (0xff - da) * cs + sa * da * blend(mode, cs, cb) + (0xff - sa) * da * cb; 146 return co / (0xff * a); 147 } 148 149 /** linear interpolation between d (alpha 0) and s (alpha 255) */ 150 private static int mix(int s, int d, int alpha) { 151 return d + (s - d) * alpha / 0xff; 152 } 153 154 private static final class BlendContext implements CompositeContext { 155 private final Mode mode; 156 private final ColorModel srcColorModel; 157 private final ColorModel dstColorModel; 158 159 BlendContext(Mode mode, ColorModel srcColorModel, ColorModel dstColorModel) { 160 this.mode = mode; 161 this.srcColorModel = srcColorModel; 162 this.dstColorModel = dstColorModel; 163 } 164 165 @Override 166 public void compose(Raster src, Raster dstIn, WritableRaster dstOut) { 167 int w = Math.min(Math.min(src.getWidth(), dstIn.getWidth()), dstOut.getWidth()); 168 int h = Math.min(Math.min(src.getHeight(), dstIn.getHeight()), dstOut.getHeight()); 169 Object srcPixel = null; 170 Object dstPixel = null; 171 Object outPixel = null; 172 for (int y = 0; y < h; y++) { 173 for (int x = 0; x < w; x++) { 174 srcPixel = src.getDataElements(x, y, srcPixel); 175 dstPixel = dstIn.getDataElements(x, y, dstPixel); 176 int result = composePixel(mode, srcColorModel.getRGB(srcPixel), dstColorModel.getRGB(dstPixel)); 177 outPixel = dstColorModel.getDataElements(result, outPixel); 178 dstOut.setDataElements(x, y, outPixel); 179 } 180 } 181 } 182 183 @Override 184 public void dispose() { 185 // nothing to dispose 186 } 187 } 188}