1 /*
   2  * Copyright (c) 2014, 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     8038000
  27  *
  28  * @summary Verifies that we could create different type of Rasters with height 1
  29  * and strideline which exceeds raster width.
  30  * Also checks that a set of RasterOp work correctly with such kind of Rasters.
  31  *
  32  * @run     main bug8038000
  33  */
  34 
  35 import java.awt.*;
  36 import java.awt.color.ColorSpace;
  37 import java.awt.geom.AffineTransform;
  38 import java.awt.image.*;
  39 import java.util.Arrays;
  40 
  41 public class bug8038000 {
  42 
  43     public static void main(String[] args) throws Exception {
  44         new bug8038000().checkOps();
  45         // No exceptions - Passed
  46     }
  47 
  48     private void checkOps() throws Exception {
  49 
  50         RasterOp[] ops = new RasterOp[] {
  51                 new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB),
  52                         ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), null),
  53                 new AffineTransformOp(AffineTransform.getScaleInstance(1, 1.1), null)
  54         };
  55 
  56 
  57         for (RasterOp op: ops) {
  58             // Banded rasters
  59             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_BYTE, 10, 1, 10,
  60                             new int[] {0, 1, 2}, new int[]{2,1,0}, null),
  61                     Raster.createBandedRaster(DataBuffer.TYPE_BYTE, 10, 1, 1001,
  62                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
  63             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_USHORT, 10, 1, 10,
  64                     new int[] {0, 1, 2}, new int[]{2,1,0}, null),
  65                     Raster.createBandedRaster(DataBuffer.TYPE_USHORT, 10, 1, 1001,
  66                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
  67             checkOp(Raster.createBandedRaster(DataBuffer.TYPE_INT, 10, 1, 10,
  68                     new int[] {0, 1, 2}, new int[]{2,1,0}, null),
  69                     Raster.createBandedRaster(DataBuffer.TYPE_INT, 10, 1, 1001,
  70                             new int[] {0, 1, 2}, new int[]{2,1,0}, null), op);
  71 
  72             // Interleaved rasters
  73             checkOp(Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
  74                             10, 1, 30, 3, new int[]{0, 1, 2}, null),
  75                     Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
  76                             10, 1, 1001, 3, new int[]{0, 1, 2}, null),
  77                     op);
  78 
  79             checkOp(Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
  80                             10, 1, 30, 3, new int[]{0, 1, 2}, null),
  81                     Raster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
  82                             10, 1, 1001, 3, new int[]{0, 1, 2}, null),
  83                     op);
  84 
  85             // Packed rasters
  86             checkOp(Raster.createPackedRaster(new DataBufferByte(10), 10, 1, 10,
  87                             new int[] {0x01, 0x02, 0x04}, null),
  88                     Raster.createPackedRaster(new DataBufferByte(10), 10, 1, 2000,
  89                             new int[] {0x01, 0x02, 0x04}, null),
  90                     op);
  91             checkOp(Raster.createPackedRaster(new DataBufferInt(10), 10, 1, 10,
  92                         new int[] {0xff0000, 0x00ff00, 0x0000ff}, null),
  93                     Raster.createPackedRaster(new DataBufferInt(10), 10, 1, 20,
  94                             new int[] {0xff0000, 0x00ff00, 0x0000ff}, null),
  95                     op);
  96 
  97         }
  98     }
  99 
 100     private void checkOp(WritableRaster wr1, WritableRaster wr2, RasterOp op) {
 101         System.out.println("Checking " + op + " with rasters: \n    " + wr1 +
 102                 "\n    " + wr2);
 103         try {
 104             WritableRaster r1 = op.filter(fillRaster(wr1), null);
 105             WritableRaster r2 = op.filter(fillRaster(wr2), null);
 106             compareRasters(r1, r2);
 107         } catch (ImagingOpException e) {
 108             System.out.println("    Skip: Op is not supported: " + e);
 109         }
 110     }
 111 
 112     private WritableRaster fillRaster(WritableRaster wr) {
 113         int c = 0;
 114         for(int x = wr.getMinX(); x < wr.getMinX() + wr.getWidth(); x++) {
 115             for(int y = wr.getMinY(); y < wr.getMinY() + wr.getHeight(); y++) {
 116                 for (int b = 0; b < wr.getNumBands(); b++) {
 117                     wr.setSample(x, y, b, c++);
 118                 }
 119             }
 120         }
 121         return wr;
 122     }
 123 
 124     private void compareRasters(Raster r1, Raster r2) {
 125         Rectangle bounds = r1.getBounds();
 126         if (!bounds.equals(r2.getBounds())) {
 127             throw new RuntimeException("Bounds differ.");
 128         }
 129 
 130         if (r1.getNumBands() != r2.getNumBands()) {
 131             throw new RuntimeException("Bands differ.");
 132         }
 133 
 134         int[] b1 = new int[r1.getNumBands()];
 135         int[] b2 = new int[r1.getNumBands()];
 136 
 137         for (int x = (int) bounds.getX(); x < bounds.getMaxX(); x++) {
 138             for (int y = (int) bounds.getY(); y < bounds.getMaxY(); y++) {
 139                 r1.getPixel(x,y, b1);
 140                 r2.getPixel(x,y, b2);
 141                 if (!Arrays.equals(b1, b2)) {
 142                     throw new RuntimeException("Pixels differ.");
 143                 }
 144             }
 145         }
 146     }
 147 }