|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 3 | + * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +import java.awt.AlphaComposite; |
| 26 | +import java.awt.Color; |
| 27 | +import java.awt.Graphics2D; |
| 28 | +import java.awt.Image; |
| 29 | +import java.awt.Transparency; |
| 30 | +import java.awt.color.ColorSpace; |
| 31 | +import java.awt.image.BufferedImage; |
| 32 | +import java.awt.image.ColorConvertOp; |
| 33 | +import java.awt.image.ColorModel; |
| 34 | +import java.awt.image.ComponentColorModel; |
| 35 | +import java.awt.image.DataBuffer; |
| 36 | +import java.awt.image.Raster; |
| 37 | +import java.awt.image.WritableRaster; |
| 38 | +import java.io.File; |
| 39 | + |
| 40 | +import javax.imageio.ImageIO; |
| 41 | + |
| 42 | +import static java.awt.image.BufferedImage.TYPE_3BYTE_BGR; |
| 43 | +import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR; |
| 44 | +import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE; |
| 45 | +import static java.awt.image.BufferedImage.TYPE_BYTE_BINARY; |
| 46 | +import static java.awt.image.BufferedImage.TYPE_BYTE_GRAY; |
| 47 | +import static java.awt.image.BufferedImage.TYPE_BYTE_INDEXED; |
| 48 | +import static java.awt.image.BufferedImage.TYPE_INT_ARGB; |
| 49 | +import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE; |
| 50 | +import static java.awt.image.BufferedImage.TYPE_INT_BGR; |
| 51 | +import static java.awt.image.BufferedImage.TYPE_INT_RGB; |
| 52 | +import static java.awt.image.BufferedImage.TYPE_USHORT_555_RGB; |
| 53 | +import static java.awt.image.BufferedImage.TYPE_USHORT_565_RGB; |
| 54 | +import static java.awt.image.BufferedImage.TYPE_USHORT_GRAY; |
| 55 | + |
| 56 | +/** |
| 57 | + * @test |
| 58 | + * @bug 8295430 |
| 59 | + * @summary Tests various conversions from/to custom 3BYTE_BGR image |
| 60 | + */ |
| 61 | +public final class FilterImageLineGap { |
| 62 | + |
| 63 | + private static final int[] TYPES = { |
| 64 | + TYPE_INT_RGB, TYPE_INT_ARGB, TYPE_INT_ARGB_PRE, TYPE_INT_BGR, |
| 65 | + TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE, |
| 66 | + TYPE_USHORT_565_RGB, TYPE_USHORT_555_RGB, TYPE_BYTE_GRAY, |
| 67 | + TYPE_USHORT_GRAY, TYPE_BYTE_BINARY, TYPE_BYTE_INDEXED |
| 68 | + }; |
| 69 | + |
| 70 | + private static final int[] CSS = { |
| 71 | + ColorSpace.CS_CIEXYZ, ColorSpace.CS_GRAY, ColorSpace.CS_LINEAR_RGB, |
| 72 | + ColorSpace.CS_PYCC, ColorSpace.CS_sRGB |
| 73 | + }; |
| 74 | + |
| 75 | + private static final int W = 511; |
| 76 | + private static final int H = 255; |
| 77 | + |
| 78 | + public static void main(String[] args) throws Exception { |
| 79 | + for (int fromIndex : CSS) { |
| 80 | + for (int toIndex : CSS) { |
| 81 | + customToCustom(fromIndex, toIndex); |
| 82 | + customToAny(fromIndex, toIndex); |
| 83 | + anytoCustom(fromIndex, toIndex); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void customToCustom(int fromIndex, int toIndex) |
| 89 | + throws Exception |
| 90 | + { |
| 91 | + ColorSpace toCS = ColorSpace.getInstance(fromIndex); |
| 92 | + ColorSpace fromCS = ColorSpace.getInstance(toIndex); |
| 93 | + ColorConvertOp op = new ColorConvertOp(fromCS, toCS, null); |
| 94 | + BufferedImage src = makeCustom3BYTE_BGR(); |
| 95 | + BufferedImage dst = makeCustom3BYTE_BGR(); |
| 96 | + |
| 97 | + BufferedImage srcGold = new BufferedImage(W, H, TYPE_3BYTE_BGR); |
| 98 | + BufferedImage dstGold = new BufferedImage(W, H, TYPE_3BYTE_BGR); |
| 99 | + fill(src); |
| 100 | + fill(srcGold); |
| 101 | + |
| 102 | + op.filter(src, dst); |
| 103 | + op.filter(srcGold, dstGold); |
| 104 | + // validate images |
| 105 | + for (int x = 0; x < W; ++x) { |
| 106 | + for (int y = 0; y < H; ++y) { |
| 107 | + if (dst.getRGB(x, y) != dstGold.getRGB(x, y)) { |
| 108 | + ImageIO.write(dst, "png", new File("custom.png")); |
| 109 | + ImageIO.write(dstGold, "png", new File("gold.png")); |
| 110 | + throw new RuntimeException("Test failed."); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void customToAny(int fromIndex, int toIndex) throws Exception |
| 117 | + { |
| 118 | + ColorSpace toCS = ColorSpace.getInstance(fromIndex); |
| 119 | + ColorSpace fromCS = ColorSpace.getInstance(toIndex); |
| 120 | + ColorConvertOp op = new ColorConvertOp(fromCS, toCS, null); |
| 121 | + for (int type : TYPES) { |
| 122 | + BufferedImage src = makeCustom3BYTE_BGR(); |
| 123 | + BufferedImage dst = new BufferedImage(W, H, type); |
| 124 | + |
| 125 | + BufferedImage srcGold = new BufferedImage(W, H, TYPE_3BYTE_BGR); |
| 126 | + BufferedImage dstGold = new BufferedImage(W, H, type); |
| 127 | + fill(src); |
| 128 | + fill(srcGold); |
| 129 | + |
| 130 | + op.filter(src, dst); |
| 131 | + op.filter(srcGold, dstGold); |
| 132 | + // validate images |
| 133 | + for (int x = 0; x < W; ++x) { |
| 134 | + for (int y = 0; y < H; ++y) { |
| 135 | + if (dst.getRGB(x, y) != dstGold.getRGB(x, y)) { |
| 136 | + ImageIO.write(dst, "png", new File("custom.png")); |
| 137 | + ImageIO.write(dstGold, "png", new File("gold.png")); |
| 138 | + throw new RuntimeException("Test failed."); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static void anytoCustom(int fromIndex, int toIndex) throws Exception |
| 146 | + { |
| 147 | + ColorSpace toCS = ColorSpace.getInstance(fromIndex); |
| 148 | + ColorSpace fromCS = ColorSpace.getInstance(toIndex); |
| 149 | + ColorConvertOp op = new ColorConvertOp(fromCS, toCS, null); |
| 150 | + for (int type : TYPES) { |
| 151 | + BufferedImage src = new BufferedImage(W, H, type); |
| 152 | + BufferedImage dst = makeCustom3BYTE_BGR(); |
| 153 | + |
| 154 | + BufferedImage srcGold = new BufferedImage(W, H, type); |
| 155 | + BufferedImage dstGold = new BufferedImage(W, H, TYPE_3BYTE_BGR); |
| 156 | + fill(src); |
| 157 | + fill(srcGold); |
| 158 | + |
| 159 | + op.filter(src, dst); |
| 160 | + op.filter(srcGold, dstGold); |
| 161 | + // validate images |
| 162 | + for (int x = 0; x < W; ++x) { |
| 163 | + for (int y = 0; y < H; ++y) { |
| 164 | + if (dst.getRGB(x, y) != dstGold.getRGB(x, y)) { |
| 165 | + ImageIO.write(dst, "png", new File("custom.png")); |
| 166 | + ImageIO.write(dstGold, "png", new File("gold.png")); |
| 167 | + throw new RuntimeException("Test failed."); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns the custom buffered image, which mostly identical to |
| 176 | + * BufferedImage.(w,h,TYPE_3BYTE_BGR), but uses the bigger scanlineStride. |
| 177 | + * This means that the raster will have gaps, between the rows. |
| 178 | + */ |
| 179 | + private static BufferedImage makeCustom3BYTE_BGR() { |
| 180 | + ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB); |
| 181 | + int[] nBits = {8, 8, 8}; |
| 182 | + int[] bOffs = {2, 1, 0}; |
| 183 | + ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false, |
| 184 | + Transparency.OPAQUE, |
| 185 | + DataBuffer.TYPE_BYTE); |
| 186 | + WritableRaster raster = Raster.createInterleavedRaster( |
| 187 | + DataBuffer.TYPE_BYTE, W, H, W * 3 + 2, 3, bOffs, null); |
| 188 | + return new BufferedImage(colorModel, raster, true, null); |
| 189 | + } |
| 190 | + |
| 191 | + private static void fill(Image image) { |
| 192 | + Graphics2D graphics = (Graphics2D) image.getGraphics(); |
| 193 | + graphics.setComposite(AlphaComposite.Src); |
| 194 | + for (int i = 0; i < image.getHeight(null); ++i) { |
| 195 | + graphics.setColor(new Color(i, 0, 255 - i)); |
| 196 | + graphics.fillRect(0, i, image.getWidth(null), 1); |
| 197 | + } |
| 198 | + graphics.dispose(); |
| 199 | + } |
| 200 | +} |
0 commit comments