blob: 7b7ce9d9cab02ee9dcd73c179c07893e38119da0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-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 4405224
27 * @summary Test color conversion for images with alpha
28 */
29
30import java.awt.Color;
31import java.awt.Transparency;
32import java.awt.Point;
33import java.awt.image.BufferedImage;
34import java.awt.image.ColorConvertOp;
35import java.awt.image.ColorModel;
36import java.awt.image.ComponentColorModel;
37import java.awt.image.SampleModel;
38import java.awt.image.Raster;
39import java.awt.image.WritableRaster;
40import java.awt.image.PixelInterleavedSampleModel;
41import java.awt.image.DataBuffer;
42import java.awt.color.ColorSpace;
43
44
45public class ColCvtAlpha {
46
47 public static void main(String args[]) {
48 BufferedImage src
49 = new BufferedImage(1, 10, BufferedImage.TYPE_INT_ARGB);
50
51 // Set src pixel values
52 Color pelColor = new Color(100, 100, 100, 128);
53 for (int i = 0; i < 10; i++) {
54 src.setRGB(0, i, pelColor.getRGB());
55 }
56
57 ColorModel cm = new ComponentColorModel
58 (ColorSpace.getInstance(ColorSpace.CS_GRAY),
59 new int [] {8,8}, true,
60 src.getColorModel().isAlphaPremultiplied(),
61 Transparency.TRANSLUCENT,
62 DataBuffer.TYPE_BYTE);
63
64 SampleModel sm = new PixelInterleavedSampleModel
65 (DataBuffer.TYPE_BYTE, 100, 100, 2, 200,
66 new int [] { 0, 1 });
67
68 WritableRaster wr = Raster.createWritableRaster(sm, new Point(0,0));
69
70 BufferedImage dst =
71 new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
72 dst = dst.getSubimage(0, 0, 1, 10);
73
74 ColorConvertOp op = new ColorConvertOp(null);
75
76 op.filter(src, dst);
77
78 for (int i = 0; i < 10; i++) {
79 if (((dst.getRGB(0, i) >> 24) & 0xff) != 128) {
80 throw new RuntimeException(
81 "Incorrect destination alpha value.");
82 }
83 }
84
85 }
86}