J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6476665 |
| 27 | * @summary Verifies MT safety of color conversions |
| 28 | * @run main MTSafetyTest |
| 29 | */ |
| 30 | |
| 31 | import java.util.Vector; |
| 32 | import java.awt.*; |
| 33 | import java.awt.color.*; |
| 34 | import java.awt.image.*; |
| 35 | |
| 36 | public class MTSafetyTest { |
| 37 | |
| 38 | static boolean failed = false; |
| 39 | |
| 40 | static int[] colorSpaceType = { |
| 41 | ColorSpace.CS_CIEXYZ, |
| 42 | ColorSpace.CS_GRAY, |
| 43 | ColorSpace.CS_LINEAR_RGB, |
| 44 | ColorSpace.CS_PYCC, |
| 45 | ColorSpace.CS_sRGB |
| 46 | }; |
| 47 | |
| 48 | static private final int[] imageTypes = new int[] { |
| 49 | BufferedImage.TYPE_INT_RGB, |
| 50 | BufferedImage.TYPE_INT_ARGB, |
| 51 | BufferedImage.TYPE_INT_ARGB_PRE, |
| 52 | BufferedImage.TYPE_INT_BGR, |
| 53 | BufferedImage.TYPE_3BYTE_BGR, |
| 54 | BufferedImage.TYPE_4BYTE_ABGR, |
| 55 | BufferedImage.TYPE_4BYTE_ABGR_PRE, |
| 56 | BufferedImage.TYPE_USHORT_565_RGB, |
| 57 | BufferedImage.TYPE_USHORT_555_RGB, |
| 58 | BufferedImage.TYPE_BYTE_GRAY, |
| 59 | BufferedImage.TYPE_USHORT_GRAY, |
| 60 | BufferedImage.TYPE_BYTE_BINARY, |
| 61 | BufferedImage.TYPE_BYTE_INDEXED |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | public static void main(String[] args) { |
| 66 | int nImgTypes = imageTypes.length; |
| 67 | int nCSTypes = colorSpaceType.length; |
| 68 | Vector<Thread> threads = |
| 69 | new Vector<Thread>(nImgTypes*nCSTypes*nCSTypes); |
| 70 | |
| 71 | for (int i = 0; i < nImgTypes; i++) { |
| 72 | BufferedImage origImage = |
| 73 | new BufferedImage(300, 300, imageTypes[i]); |
| 74 | |
| 75 | for (int j = 0; j < nCSTypes; j++) { |
| 76 | for (int k = 0; k < nCSTypes; k++) { |
| 77 | |
| 78 | Graphics2D g2 = (Graphics2D) origImage.getGraphics(); |
| 79 | g2.fillRect(0, 0, 300, 150); |
| 80 | |
| 81 | ColorConvertOp colorOp = getColorConvertOp(j,k); |
| 82 | ColorConvert cc = new ColorConvert(origImage, colorOp); |
| 83 | |
| 84 | Thread colorThread = new Thread(cc); |
| 85 | threads.add(colorThread); |
| 86 | colorThread.start(); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | for (Thread thread : threads) { |
| 93 | thread.join(); |
| 94 | } |
| 95 | } catch (InterruptedException e) { |
| 96 | throw new RuntimeException("Unexpected exception" + e); |
| 97 | } |
| 98 | |
| 99 | if (failed) { |
| 100 | throw new RuntimeException("Unexpected exception"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private static ColorConvertOp getColorConvertOp(int srcIndex, |
| 105 | int destIndex) |
| 106 | { |
| 107 | ColorSpace srcColorSpace = ColorSpace.getInstance( |
| 108 | colorSpaceType[srcIndex]); |
| 109 | ColorSpace destColorSpace = ColorSpace.getInstance( |
| 110 | colorSpaceType[destIndex]); |
| 111 | return new ColorConvertOp(srcColorSpace, destColorSpace, null); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static class ColorConvert implements Runnable { |
| 116 | |
| 117 | BufferedImage original = null; |
| 118 | ColorConvertOp colorOp = null; |
| 119 | |
| 120 | public ColorConvert(BufferedImage orig, ColorConvertOp ccOp) { |
| 121 | original = orig; |
| 122 | colorOp = ccOp; |
| 123 | } |
| 124 | |
| 125 | public void run() { |
| 126 | try { |
| 127 | colorOp.filter(original, null); |
| 128 | } catch (OutOfMemoryError e) { |
| 129 | /* Skipping OOM exception. We cannot just enlarge stack and heap |
| 130 | * because it causes problem to disappear |
| 131 | */ |
| 132 | |
| 133 | e.printStackTrace(); |
| 134 | |
| 135 | } catch (Exception e) { |
| 136 | e.printStackTrace(); |
| 137 | failed = true; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |