|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.color.ColorSpace; |
| 25 | +import java.awt.image.BufferedImage; |
| 26 | +import java.awt.image.ColorConvertOp; |
| 27 | + |
| 28 | +/** |
| 29 | + * @test |
| 30 | + * @bug 8273972 |
| 31 | + * @summary Verifies that ColorConvertOp works fine if shared between threads |
| 32 | + * @run main/othervm/timeout=600 MTTransformValidation |
| 33 | + */ |
| 34 | +public final class MTPerLineTransformValidation { |
| 35 | + |
| 36 | + private volatile static BufferedImage[] lines; |
| 37 | + |
| 38 | + public static final int SIZE = 255; |
| 39 | + private static volatile boolean failed = false; |
| 40 | + |
| 41 | + private static final int[] spaces = { |
| 42 | + ColorSpace.CS_CIEXYZ, ColorSpace.CS_GRAY, ColorSpace.CS_LINEAR_RGB, |
| 43 | + ColorSpace.CS_PYCC, ColorSpace.CS_sRGB |
| 44 | + }; |
| 45 | + |
| 46 | + private static final int[] types = new int[]{ |
| 47 | + BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB, |
| 48 | + BufferedImage.TYPE_INT_ARGB_PRE, BufferedImage.TYPE_INT_BGR, |
| 49 | + BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR, |
| 50 | + BufferedImage.TYPE_4BYTE_ABGR_PRE, |
| 51 | + BufferedImage.TYPE_USHORT_565_RGB, |
| 52 | + BufferedImage.TYPE_USHORT_555_RGB, BufferedImage.TYPE_BYTE_GRAY, |
| 53 | + BufferedImage.TYPE_USHORT_GRAY, BufferedImage.TYPE_BYTE_BINARY, |
| 54 | + BufferedImage.TYPE_BYTE_INDEXED |
| 55 | + }; |
| 56 | + |
| 57 | + /** |
| 58 | + * For all possible combinations of color spaces and image types, convert |
| 59 | + * the source image using one shared ColorConvertOp per line on the |
| 60 | + * different threads. The result is validated against images converted on |
| 61 | + * one thread only. |
| 62 | + */ |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + for (int srcCS : spaces) { |
| 65 | + for (int dstCS : spaces) { |
| 66 | + if(srcCS != dstCS) { |
| 67 | + for (int type : types) { |
| 68 | + checkTypes(ColorSpace.getInstance(srcCS), |
| 69 | + ColorSpace.getInstance(dstCS), type); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static void checkTypes(ColorSpace srcCS, ColorSpace dstCS, int type) |
| 77 | + throws Exception { |
| 78 | + lines = new BufferedImage[SIZE]; |
| 79 | + ColorConvertOp goldOp = new ColorConvertOp(srcCS, dstCS, null); |
| 80 | + BufferedImage src = createSrc(type); |
| 81 | + BufferedImage gold = goldOp.filter(src, null); |
| 82 | + |
| 83 | + // we do not share the goldOp since it is already initialized and used |
| 84 | + // for the whole image, instead we will create a separate sharedOp and |
| 85 | + // use it for each line of a different threads |
| 86 | + ColorConvertOp sharedOp = new ColorConvertOp(srcCS, dstCS, null); |
| 87 | + Thread[] threads = new Thread[SIZE]; |
| 88 | + for (int y = 0; y < SIZE; ++y) { |
| 89 | + BufferedImage line = src.getSubimage(0, y, SIZE, 1); |
| 90 | + threads[y] = test(sharedOp, line, y); |
| 91 | + } |
| 92 | + |
| 93 | + for (Thread t: threads) { |
| 94 | + t.start(); |
| 95 | + } |
| 96 | + for (Thread t: threads) { |
| 97 | + t.join(); |
| 98 | + } |
| 99 | + for (int y = 0; y < SIZE; ++y) { |
| 100 | + validate(gold, lines[y], y); |
| 101 | + } |
| 102 | + if (failed) { |
| 103 | + throw new RuntimeException("Unexpected exception"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static Thread test(ColorConvertOp sharedOp, |
| 108 | + BufferedImage line, int y){ |
| 109 | + return new Thread(() -> { |
| 110 | + try { |
| 111 | + BufferedImage image = sharedOp.filter(line, null); |
| 112 | + lines[y] = image; |
| 113 | + } catch (Throwable t) { |
| 114 | + t.printStackTrace(); |
| 115 | + failed = true; |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + private static BufferedImage createSrc(int type) { |
| 121 | + BufferedImage img = new BufferedImage(SIZE, SIZE, type); |
| 122 | + fill(img); |
| 123 | + return img; |
| 124 | + } |
| 125 | + |
| 126 | + private static void fill(BufferedImage image) { |
| 127 | + for (int i = 0; i < SIZE; i++) { |
| 128 | + for (int j = 0; j < SIZE; j++) { |
| 129 | + image.setRGB(i, j, |
| 130 | + (i << 24) | (i << 16) | (j << 8) | ((i + j) >> 1)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void validate(BufferedImage full, BufferedImage line, int y) { |
| 136 | + for (int i = 0; i < SIZE; i++) { |
| 137 | + int rgb1 = full.getRGB(i, y); |
| 138 | + int rgb2 = line.getRGB(i, 0); |
| 139 | + if (rgb1 != rgb2) { |
| 140 | + System.err.println("rgb1 = " + Integer.toHexString(rgb1)); |
| 141 | + System.err.println("rgb2 = " + Integer.toHexString(rgb2)); |
| 142 | + throw new RuntimeException(); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments