J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 30 | import java.awt.Color; |
| 31 | import java.awt.Transparency; |
| 32 | import java.awt.Point; |
| 33 | import java.awt.image.BufferedImage; |
| 34 | import java.awt.image.ColorConvertOp; |
| 35 | import java.awt.image.ColorModel; |
| 36 | import java.awt.image.ComponentColorModel; |
| 37 | import java.awt.image.SampleModel; |
| 38 | import java.awt.image.Raster; |
| 39 | import java.awt.image.WritableRaster; |
| 40 | import java.awt.image.PixelInterleavedSampleModel; |
| 41 | import java.awt.image.DataBuffer; |
| 42 | import java.awt.color.ColorSpace; |
| 43 | |
| 44 | |
| 45 | public 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 | } |