Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/png/PngPremultAlphaTest.java
41155 views
1
/*
2
* Copyright (c) 2003, 2017, 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 4888478
27
* @summary Test that colors do not distort when buffered image with
28
* premultiplied alpha is encoded to png format
29
*/
30
31
import java.awt.AlphaComposite;
32
import java.awt.Color;
33
import java.awt.Graphics2D;
34
import java.awt.image.BufferedImage;
35
import java.io.ByteArrayInputStream;
36
import java.io.ByteArrayOutputStream;
37
38
import javax.imageio.ImageIO;
39
40
public class PngPremultAlphaTest {
41
protected static int width = 100;
42
protected static int height = 100;
43
44
protected static String format = "png";
45
46
protected static int[] iBufferedImageTypes = {
47
BufferedImage.TYPE_INT_RGB,
48
BufferedImage.TYPE_INT_ARGB,
49
BufferedImage.TYPE_4BYTE_ABGR_PRE,
50
BufferedImage.TYPE_INT_ARGB_PRE
51
};
52
53
protected static String[] strBufferedImageTypes = {
54
"TYPE_INT_RGB",
55
"TYPE_INT_ARGB",
56
"BufferedImage.TYPE_4BYTE_ABGR_PRE",
57
"BufferedImage.TYPE_INT_ARGB_PRE"
58
};
59
60
public static void main(String[] arg) {
61
for(int i=0; i<iBufferedImageTypes.length; i++) {
62
System.out.println("Test for " + strBufferedImageTypes[i]);
63
doTest(iBufferedImageTypes[i]);
64
}
65
}
66
67
public static void doTest(int type) {
68
try {
69
BufferedImage src = new BufferedImage(100, 100,
70
type);
71
Graphics2D g = src.createGraphics();
72
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));
73
g.setColor(new Color(0x20, 0x40, 0x60));
74
g.fillRect(0,0,100,100);
75
76
int[] samples = new int[src.getData().getNumBands()];
77
src.getData().getPixels(0,0,1,1,samples);
78
for(int i=0; i<samples.length; i++) {
79
System.out.println("sample["+i+"]="+Integer.toHexString(samples[i]));
80
}
81
82
ByteArrayOutputStream baos = new ByteArrayOutputStream();
83
ImageIO.write(src, format, baos);
84
baos.close();
85
86
87
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
88
BufferedImage dst = ImageIO.read(bais);
89
90
isSameColors(src, dst);
91
} catch (Exception e) {
92
e.printStackTrace();
93
throw new RuntimeException("Test failed.");
94
}
95
}
96
97
private static boolean isSameColors(BufferedImage src, BufferedImage dst) {
98
Object dstPixel = dst.getRaster().getDataElements(width/2, height/2, null);
99
Object srcPixel = src.getRaster().getDataElements(width/2, height/2, null);
100
101
// take into account the rounding error
102
if ( Math.abs(src.getColorModel().getRed(srcPixel) - dst.getColorModel().getRed(dstPixel)) > 1
103
|| Math.abs(src.getColorModel().getGreen(srcPixel) - dst.getColorModel().getGreen(dstPixel)) > 1
104
|| Math.abs(src.getColorModel().getBlue(srcPixel) - dst.getColorModel().getBlue(dstPixel)) > 1) {
105
showPixel(src, width/2, height/2);
106
showPixel(dst, width/2, height/2);
107
108
throw new RuntimeException( "Colors are different: "
109
+ Integer.toHexString(src.getColorModel().getRGB(srcPixel))
110
+ " and "
111
+ Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
112
}
113
return true;
114
}
115
116
private static void showPixel(BufferedImage src, int x, int y) {
117
System.out.println("Img is " + src);
118
System.out.println("CM is " + src.getColorModel().getClass().getName());
119
Object p = src.getRaster().getDataElements(x, y, null);
120
System.out.println("RGB: " +
121
Integer.toHexString(src.getColorModel().getRGB(p)));
122
System.out.println("Red: " +
123
Integer.toHexString(src.getColorModel().getRed(p)));
124
System.out.println("Green: " +
125
Integer.toHexString(src.getColorModel().getGreen(p)));
126
System.out.println("Blue: " +
127
Integer.toHexString(src.getColorModel().getBlue(p)));
128
System.out.println("Alpha: " +
129
Integer.toHexString(src.getColorModel().getAlpha(p)));
130
}
131
}
132
133