|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2024, Oracle and/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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 4184283 |
| 27 | + * @summary Checks rendering of dithered byte packed image does not crash. |
| 28 | + */ |
| 29 | + |
| 30 | +import java.awt.Color; |
| 31 | +import java.awt.Component; |
| 32 | +import java.awt.Graphics2D; |
| 33 | +import java.awt.Image; |
| 34 | +import java.awt.image.BufferedImage; |
| 35 | +import java.awt.image.ColorModel; |
| 36 | +import java.awt.image.IndexColorModel; |
| 37 | +import java.awt.image.MemoryImageSource; |
| 38 | +import java.awt.image.WritableRaster; |
| 39 | + |
| 40 | +public class DitherTest extends Component { |
| 41 | + |
| 42 | + final static int NOOP = 0; |
| 43 | + final static int RED = 1; |
| 44 | + final static int GREEN = 2; |
| 45 | + final static int BLUE = 3; |
| 46 | + final static int ALPHA = 4; |
| 47 | + final static int SATURATION = 5; |
| 48 | + |
| 49 | + final static byte red[] = {(byte)0, (byte)132, (byte)0, (byte)132, (byte)0, (byte)132, |
| 50 | + (byte)0, (byte)198, (byte)198, (byte)165, (byte)255, (byte)165, (byte)132, |
| 51 | + (byte)255, (byte)0, (byte)255}; |
| 52 | + |
| 53 | + final static byte green[] = {(byte)0, (byte)0, (byte)130, (byte)130, (byte)0, |
| 54 | + (byte)0, (byte)130, (byte)195, (byte)223, (byte)203, (byte)251, (byte)162, |
| 55 | + (byte)132, (byte)0, (byte)255, (byte)255}; |
| 56 | + |
| 57 | + final static byte blue[] = {(byte)0, (byte)0, (byte)0, (byte)0, (byte)132, (byte)132, |
| 58 | + (byte)132, (byte)198, (byte)198, (byte)247, (byte)247, (byte)165, (byte)132, |
| 59 | + (byte)0, (byte)0, (byte)0}; |
| 60 | + |
| 61 | + static IndexColorModel cm16 = new IndexColorModel( 4, 16, red, green, blue); |
| 62 | + |
| 63 | + |
| 64 | + public static void main(String args[]) { |
| 65 | + |
| 66 | + int imageWidth = 256; |
| 67 | + int imageHeight = 256; |
| 68 | + WritableRaster raster = cm16.createCompatibleWritableRaster(imageWidth, imageHeight); |
| 69 | + BufferedImage intermediateImage = new BufferedImage(cm16, raster, false, null); |
| 70 | + Image calculatedImage = calculateImage(); |
| 71 | + |
| 72 | + Graphics2D ig = intermediateImage.createGraphics(); |
| 73 | + // Clear background and fill a red rectangle just to prove that we can draw on intermediateImage |
| 74 | + ig.setColor(Color.white); |
| 75 | + ig.fillRect(0,0,imageWidth,imageHeight); |
| 76 | + ig.drawImage(calculatedImage, 0, 0, imageWidth, imageHeight, null); |
| 77 | + ig.setColor(Color.red); |
| 78 | + ig.fillRect(0,0,5,5); |
| 79 | + |
| 80 | + BufferedImage destImage = new BufferedImage(imageWidth, imageWidth, BufferedImage.TYPE_INT_RGB); |
| 81 | + Graphics2D dg = destImage.createGraphics(); |
| 82 | + dg.drawImage(intermediateImage, 0, 0, imageWidth, imageHeight, null); |
| 83 | + } |
| 84 | + |
| 85 | + private static void applymethod(int c[], int method, int step, int total, int vals[]) { |
| 86 | + if (method == NOOP) |
| 87 | + return; |
| 88 | + int val = ((total < 2) |
| 89 | + ? vals[0] |
| 90 | + : vals[0] + ((vals[1] - vals[0]) * step / (total - 1))); |
| 91 | + switch (method) { |
| 92 | + case RED: |
| 93 | + c[0] = val; |
| 94 | + break; |
| 95 | + case GREEN: |
| 96 | + c[1] = val; |
| 97 | + break; |
| 98 | + case BLUE: |
| 99 | + c[2] = val; |
| 100 | + break; |
| 101 | + case ALPHA: |
| 102 | + c[3] = val; |
| 103 | + break; |
| 104 | + case SATURATION: |
| 105 | + int max = Math.max(Math.max(c[0], c[1]), c[2]); |
| 106 | + int min = max * (255 - val) / 255; |
| 107 | + if (c[0] == 0) c[0] = min; |
| 108 | + if (c[1] == 0) c[1] = min; |
| 109 | + if (c[2] == 0) c[2] = min; |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static Image calculateImage() { |
| 115 | + |
| 116 | + int xvals[] = { 0, 255 }; |
| 117 | + int yvals[] = { 0, 255 }; |
| 118 | + int xmethod = RED; |
| 119 | + int ymethod = BLUE; |
| 120 | + int width = 256; |
| 121 | + int height = 256; |
| 122 | + int pixels[] = new int[width * height]; |
| 123 | + int c[] = new int[4]; |
| 124 | + int index = 0; |
| 125 | + for (int j = 0; j < height; j++) { |
| 126 | + for (int i = 0; i < width; i++) { |
| 127 | + c[0] = c[1] = c[2] = 0; |
| 128 | + c[3] = 255; |
| 129 | + if (xmethod < ymethod) { |
| 130 | + applymethod(c, xmethod, i, width, xvals); |
| 131 | + applymethod(c, ymethod, j, height, yvals); |
| 132 | + } else { |
| 133 | + applymethod(c, ymethod, j, height, yvals); |
| 134 | + applymethod(c, xmethod, i, width, xvals); |
| 135 | + } |
| 136 | + pixels[index++] = ((c[3] << 24) | |
| 137 | + (c[0] << 16) | |
| 138 | + (c[1] << 8) | |
| 139 | + (c[2] << 0)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + DitherTest dt = new DitherTest(); |
| 144 | + return dt.createImage(new MemoryImageSource(width, height, ColorModel.getRGBdefault(), pixels, 0, width)); |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments