1 /*
   2  * Copyright (c) 2013, 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     8004801
  27  * @summary Test verifies that byte lookup table with single lookup
  28  *          affects only color components in buffered images with
  29  *          integer data type, and that this operation does not distort
  30  *          colors in the destination image.
  31  * @run     main IntImageReverseTest
  32  */
  33 
  34 import java.awt.image.BufferedImage;
  35 import java.awt.image.ByteLookupTable;
  36 import java.awt.image.LookupOp;
  37 import java.awt.image.LookupTable;
  38 
  39 public class IntImageReverseTest {
  40 
  41     public static void main(String[] args) {
  42         LookupTable tbl = createReverseTable();
  43         LookupOp op = new LookupOp(tbl, null);
  44 
  45         for (ImageType t : ImageType.values()) {
  46             System.out.print(t);
  47 
  48             BufferedImage src = createSourceImage(t);
  49 
  50             BufferedImage dst = op.filter(src, null);
  51 
  52             int rgb = dst.getRGB(0, 0);
  53 
  54             System.out.printf(" Result: 0x%X ", rgb);
  55 
  56             if (rgb != argbReverse) {
  57                 throw new RuntimeException("Test failed.");
  58             }
  59             System.out.println("Passed.");
  60         }
  61     }
  62 
  63     /**
  64      * Reverse image color components, leave alpha unchanged.
  65      */
  66     private static LookupTable createReverseTable() {
  67         byte[] data = new byte[256];
  68 
  69         for (int i = 0; i < 256; i++) {
  70             data[i] = (byte) (255 - i);
  71         }
  72 
  73 
  74         return new ByteLookupTable(0, data);
  75     }
  76 
  77     private static BufferedImage createSourceImage(ImageType type) {
  78         BufferedImage img = new BufferedImage(1, 1, type.bi_type);
  79 
  80         img.setRGB(0, 0, argbTest);
  81 
  82         return img;
  83     }
  84     private static final int argbTest = 0xFFDDAA77;
  85     private static final int argbReverse = 0xFF225588;
  86 
  87     private static enum ImageType {
  88 
  89         INT_ARGB(BufferedImage.TYPE_INT_ARGB),
  90         INT_ARGB_PRE(BufferedImage.TYPE_INT_ARGB_PRE),
  91         INT_RGB(BufferedImage.TYPE_INT_BGR),
  92         INT_BGR(BufferedImage.TYPE_INT_BGR);
  93 
  94         private ImageType(int bi_type) {
  95             this.bi_type = bi_type;
  96         }
  97         public final int bi_type;
  98     }
  99 }