File: CustomSourceCM.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (142 lines) | stat: -rw-r--r-- 5,621 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 4192756
 * @summary Tests that using a non-default colormodel generates correct images under 16/24 bit mode
 */

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.MemoryImageSource;
import java.util.Arrays;

/*
 * NOTE: This bug only appears under specific conditions.  If the background of
 * the surface is red, then you are not running under the conditions necessary
 * to test for the regression so the results of this test will be inconclusive.
 *
 * The test should be run under any of the following screen depths/surfaces:
 *
 * 15-bit, otherwise known as 555 RGB or 32768 (thousands) colors
 * 16-bit, otherwise known as 565 RGB or 65536 (thousands) colors
 * 24-bit, otherwise known as 16777216 (millions) colors
 *
 * The test draws 2 rectangles.  Both rectangles should be half black (left)
 * and half blue (right).  If the top rectangle is all black, the test fails.
 * If the background is red, the results are inconclusive (see above).
*/

public class CustomSourceCM extends Component {

    public static int IMG_W = 80;
    public static int IMG_H = 30;

    static void test(int imageType) {

        int w = IMG_W + 20;
        int h = IMG_H * 2 + 40;
        BufferedImage bi = new BufferedImage(w, h, imageType);

        DirectColorModel dcm;

        /* the next dozen lines or so are intended to help
         * ascertain if the destination surface is of the type
         * that exhibited the original bug, making the background
         * white in those cases. It is not strictly necessary.
         * It is only for a manual tester to be able to tell by looking.
         * The real test is the check for black and blue later on.
         */
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, w, h);

        ColorModel cm = bi.getColorModel();
        if (cm instanceof ComponentColorModel) {
            g.setColor(Color.white);
            g.fillRect(0, 0, w, h);
        } else if (cm instanceof DirectColorModel) {
            dcm = (DirectColorModel) cm;
            if (dcm.getPixelSize() < 24) {
                g.setColor(Color.white);
                g.fillRect(0, 0, w, h);
            }
        }

        // Construct a ColorModel and data for a 16-bit 565 image...
        dcm = new DirectColorModel(16, 0x1f, 0x7e0, 0xf800);

        // Create an image which is black on the left, blue on the right.
        int[] pixels = new int[IMG_W * IMG_H];
        int blue = dcm.getBlueMask();
        int off = 0;
        for (int y = 0; y < IMG_H; y++) {
            Arrays.fill(pixels, off, off+IMG_W/2, 0);
            Arrays.fill(pixels, off+IMG_W/2, off+IMG_W, blue);
            off += IMG_W;
        }
        MemoryImageSource mis = new MemoryImageSource(IMG_W, IMG_H, dcm,
                                                      pixels, 0, IMG_W);
        CustomSourceCM comp = new CustomSourceCM();
        Image img = comp.createImage(mis);

        // Draw the image on to the surface.
        g.drawImage(img, 10, 10, null);

        // Create a similar effect with 2 fillrects, below the image.
        g.setColor(Color.black);
        g.fillRect(10, 60, IMG_W/2, IMG_H);
        g.setColor(Color.blue);
        g.fillRect(10+IMG_W/2, 60, IMG_W/2, IMG_H);

        // Now sample points in the image to confirm they are the expected color.
        int bluePix = Color.blue.getRGB();
        int blackPix = Color.black.getRGB();
        int black_topLeft = bi.getRGB(10+IMG_W/4, 10+IMG_H/2);
        int blue_topRight = bi.getRGB(10+IMG_W*3/4, 10+IMG_H/2);
        int black_bottomLeft = bi.getRGB(10+IMG_W/4, 60+IMG_H/2);
        int blue_bottomRight = bi.getRGB(10+IMG_W*3/4, 60+IMG_H/2);
        if ((black_topLeft != blackPix) || (black_bottomLeft != blackPix) ||
            (blue_topRight != bluePix) ||  (blue_bottomRight != bluePix)) {

            String fileName = "failed " + imageType + ".png";
            try {
                javax.imageio.ImageIO.write(bi, "png", new java.io.File(fileName));
            } catch (Exception e) { };
            throw new RuntimeException("unexpected colors");
        }
    }

    public static void main(String argv[]) {
        test(BufferedImage.TYPE_USHORT_555_RGB);
        test(BufferedImage.TYPE_USHORT_565_RGB);
        test(BufferedImage.TYPE_3BYTE_BGR);
    }
}