Cross Reference: GrayPngTest.java
xref
: /
openjdk7
/
jdk
/
test
/
javax
/
imageio
/
plugins
/
png
/
GrayPngTest.java
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
0
N/A
/*
2362
N/A
* Copyright (c) 2007, Oracle
and
/
or
its affiliates. All rights reserved.
0
N/A
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0
N/A
*
0
N/A
* This code is free software; you can redistribute it
and
/
or
modify it
0
N/A
* under the terms of the GNU General Public License version 2 only, as
0
N/A
* published by the Free Software Foundation.
0
N/A
*
0
N/A
* This code is distributed in the hope that it will be useful, but WITHOUT
0
N/A
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0
N/A
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0
N/A
* version 2 for more details (a copy is included in the LICENSE file that
0
N/A
* accompanied this code).
0
N/A
*
0
N/A
* You should have received a copy of the GNU General Public License version
0
N/A
* 2 along with this work; if not, write to the Free Software Foundation,
0
N/A
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0
N/A
*
2362
N/A
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362
N/A
* or visit www.oracle.com if you need additional information or have any
2362
N/A
* questions.
0
N/A
*/
0
N/A
0
N/A
/**
0
N/A
*
@test
0
N/A
*
@bug
6557713
0
N/A
*
@summary
Test verifies that PNG image writer correctly handles indexed images with
0
N/A
* various types of transparency.
0
N/A
*
0
N/A
* Test for 4bpp OPAQUE image
0
N/A
*
@run
main GrayPngTest 4 1 3
0
N/A
*
0
N/A
* Test for 4bpp BITMASK image with transparent pixel 3
0
N/A
*
@run
main GrayPngTest 4 2 3
0
N/A
*
0
N/A
* Test for 4bpp TRANSLUCENT image
0
N/A
*
@run
main GrayPngTest 4 3 3
0
N/A
*
0
N/A
* Test for 8bpp OPAQUE image
0
N/A
*
@run
main GrayPngTest 8 1 127
0
N/A
*
0
N/A
* Test for 8bpp BITMASK image with transparent pixel 127
0
N/A
*
@run
main GrayPngTest 8 2 127
0
N/A
*
0
N/A
* Test for 8bpp TRANSLUCENT image
0
N/A
*
@run
main GrayPngTest 8 3 127
0
N/A
*
0
N/A
*/
0
N/A
0
N/A
import
java
.
awt
.
Color
;
0
N/A
import
java
.
awt
.
Transparency
;
0
N/A
import
java
.
awt
.
image
.
BufferedImage
;
0
N/A
import
java
.
awt
.
image
.
IndexColorModel
;
0
N/A
import
java
.
awt
.
image
.
WritableRaster
;
0
N/A
import
java
.
io
.
File
;
0
N/A
import
java
.
io
.
IOException
;
0
N/A
import
java
.
util
.
Arrays
;
0
N/A
import
javax
.
imageio
.
ImageIO
;
0
N/A
0
N/A
public
class
GrayPngTest
{
0
N/A
0
N/A
public
static
void
main
(
String
[]
args
)
throws
IOException
{
0
N/A
/*
0
N/A
* Expected argiments:
0
N/A
* args[0] - bits per pixel. Supported range: [1, 8]
0
N/A
* args[1] - transparency type. Should be one form
0
N/A
* java.awt.Transparency type constants.
0
N/A
* args[2] - transparent pixel for BITMASK transparency type,
0
N/A
* otherwise is ignored.
0
N/A
*/
0
N/A
int
bpp
=
4
;
0
N/A
int
trans_type
=
Transparency
.
BITMASK
;
0
N/A
int
trans_pixel
=
3
;
0
N/A
try
{
0
N/A
bpp
=
Integer
.
parseInt
(
args
[
0
]);
0
N/A
trans_type
=
Integer
.
parseInt
(
args
[
1
]);
0
N/A
trans_pixel
=
Integer
.
parseInt
(
args
[
2
]);
0
N/A
}
catch
(
NumberFormatException
e) {
0
N/A
System
.
out
.
println
(
"Ignore ncorrect bpp value: "
+
args
[
0
]);
0
N/A
}
catch
(
ArrayIndexOutOfBoundsException
e) {
0
N/A
System
.
out
.
println
(
"Default test argumens."
);
0
N/A
}
0
N/A
0
N/A
new
GrayPngTest
(
bpp
).
doTest
(
trans_type
,
trans_pixel
);
0
N/A
}
0
N/A
0
N/A
0
N/A
0
N/A
private
BufferedImage
getTestImage
(
int
trans_type
,
int
trans_pixel
) {
0
N/A
0
N/A
IndexColorModel
icm
=
null
;
0
N/A
switch
(
trans_type
) {
0
N/A
case
Transparency
.
OPAQUE
:
0
N/A
icm
=
new
IndexColorModel
(
bpp
,
numColors
, r, g, b);
0
N/A
break
;
0
N/A
case
Transparency
.
BITMASK
:
0
N/A
icm
=
new
IndexColorModel
(
bpp
,
numColors
, r, g, b,
trans_pixel
);
0
N/A
break
;
0
N/A
case
Transparency
.
TRANSLUCENT
:
0
N/A
a =
Arrays
.
copyOf
(r, r.
length
);
0
N/A
icm
=
new
IndexColorModel
(
bpp
,
numColors
, r, g, b, a);
0
N/A
break
;
0
N/A
default
:
0
N/A
throw
new
RuntimeException
(
"Invalid transparency: "
+
trans_type
);
0
N/A
}
0
N/A
0
N/A
int
w =
256
*
2
;
0
N/A
int
h =
200
;
0
N/A
0
N/A
dx
= w / (
numColors
);
0
N/A
0
N/A
WritableRaster
wr
=
icm
.
createCompatibleWritableRaster
(w, h);
0
N/A
for
(
int
i =
0
; i <
numColors
; i ++) {
0
N/A
int
rx
= i *
dx
;
0
N/A
0
N/A
int
[]
samples
=
new
int
[h *
dx
];
0
N/A
Arrays
.
fill
(
samples
, i);
0
N/A
wr
.
setPixels
(
rx
,
0
,
dx
, h,
samples
);
0
N/A
}
0
N/A
0
N/A
// horizontal line with transparent color
0
N/A
int
[]
samples
=
new
int
[w *
10
];
0
N/A
Arrays
.
fill
(
samples
,
trans_pixel
);
0
N/A
wr
.
setPixels
(
0
, h /
2
-
5
, w,
10
,
samples
);
0
N/A
0
N/A
// create index color model
0
N/A
return
new
BufferedImage
(
icm
,
wr
,
false
,
null
);
0
N/A
}
0
N/A
0
N/A
static
File
pwd
=
new
File
(
"."
);
0
N/A
0
N/A
private
BufferedImage
src
;
0
N/A
private
BufferedImage
dst
;
0
N/A
private
int
bpp
;
0
N/A
private
int
numColors
;
0
N/A
0
N/A
private
int
dx
;
0
N/A
0
N/A
private
byte
[] r;
0
N/A
private
byte
[] g;
0
N/A
private
byte
[] b;
0
N/A
0
N/A
private
byte
[] a;
0
N/A
0
N/A
protected
GrayPngTest
(
int
bpp
) {
0
N/A
if
(
0
>
bpp
||
bpp
>
8
) {
0
N/A
throw
new
RuntimeException
(
"Invalid bpp: "
+
bpp
);
0
N/A
}
0
N/A
this
.
bpp
=
bpp
;
0
N/A
numColors
= (
1
<<
bpp
);
0
N/A
System
.
out
.
println
(
"Num colors: "
+
numColors
);
0
N/A
0
N/A
// create palette
0
N/A
r =
new
byte
[
numColors
];
0
N/A
g =
new
byte
[
numColors
];
0
N/A
b =
new
byte
[
numColors
];
0
N/A
0
N/A
int
dc
=
0xff
/ (
numColors
-
1
);
0
N/A
System
.
out
.
println
(
"dc = "
+
dc
);
0
N/A
0
N/A
for
(
int
i =
0
; i <
numColors
; i ++) {
0
N/A
byte
l = (
byte
)(i *
dc
);
0
N/A
r[i] = l; g[i] = l; b[i] = l;
0
N/A
}
0
N/A
}
0
N/A
0
N/A
public
void
doTest
()
throws
IOException
{
0
N/A
for
(
int
i =
0
; i <
numColors
; i++) {
0
N/A
doTest
(
Transparency
.
BITMASK
, i);
0
N/A
}
0
N/A
}
0
N/A
0
N/A
public
void
doTest
(
int
trans_type
,
int
trans_index
)
throws
IOException
{
0
N/A
src
=
getTestImage
(
trans_type
,
trans_index
);
0
N/A
0
N/A
System
.
out
.
println
(
"src: "
+
src
);
0
N/A
0
N/A
File
f =
File
.
createTempFile
(
"gray_png_"
+
bpp
+
"bpp_"
+
0
N/A
trans_type
+
"tt_"
+
0
N/A
trans_index
+
"tp_"
,
".png"
,
pwd
);
0
N/A
System
.
out
.
println
(
"File: "
+ f.
getAbsolutePath
());
0
N/A
if
(!
ImageIO
.
write
(
src
,
"png"
, f)) {
0
N/A
throw
new
RuntimeException
(
"Writing failed!"
);
0
N/A
};
0
N/A
0
N/A
try
{
0
N/A
dst
=
ImageIO
.
read
(f);
0
N/A
System
.
out
.
println
(
"dst: "
+
dst
);
0
N/A
}
catch
(
Exception
e) {
0
N/A
throw
new
RuntimeException
(
"Test FAILED."
, e);
0
N/A
}
0
N/A
0
N/A
checkImages
();
0
N/A
}
0
N/A
0
N/A
private
void
checkImages
() {
0
N/A
for
(
int
i =
0
; i <
numColors
; i++) {
0
N/A
int
src_rgb
=
src
.
getRGB
(i *
dx
,
5
);
0
N/A
int
dst_rgb
=
dst
.
getRGB
(i *
dx
,
5
);
0
N/A
0
N/A
// here we check transparency only due to possible colors space
0
N/A
// differences (sRGB in indexed source and Gray in gray+alpha destination)
0
N/A
if
((
0xff000000
&
src_rgb
) != (
0xff000000
&
dst_rgb
)) {
0
N/A
throw
new
RuntimeException
(
"Test FAILED. Color difference detected: "
+
0
N/A
Integer
.
toHexString
(
dst_rgb
) +
" instead of "
+
0
N/A
Integer
.
toHexString
(
src_rgb
) +
" for index "
+ i);
0
N/A
0
N/A
}
0
N/A
}
0
N/A
}
0
N/A
}