1 /*
   2  * Copyright (c) 2018, 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 import java.awt.Graphics;
  25 import java.awt.image.BufferedImage;
  26 import java.awt.image.DataBuffer;
  27 import java.awt.image.DataBufferByte;
  28 import java.awt.image.IndexColorModel;
  29 import java.awt.image.MultiPixelPackedSampleModel;
  30 import java.awt.image.Raster;
  31 import java.awt.image.SampleModel;
  32 import java.awt.image.WritableRaster;
  33 
  34 /*
  35  * @test
  36  * @bug 8201433
  37  * @summary This test may throw OOME or NPE from native code,
  38  *          it should not crash
  39  * @run main/othervm/timeout=300000 -Xms1000m -Xmx1000m ICMColorDataTest
  40  */
  41 public class ICMColorDataTest {
  42     private static final int WIDTH  = 90;
  43     private static final int HEIGHT = 90;
  44     private static final int BITS_PER_PIXEL = 1;
  45     private static final int PIXELS_IN_BYTE = 8;
  46 
  47     // Color model components
  48     private static final byte[] RED   = { (byte) 255, 0 };
  49     private static final byte[] GREEN = { (byte) 255, 0 };
  50     private static final byte[] BLUE  = { (byte) 255, 0 };
  51 
  52     public static void main(String[] args) {
  53         try {
  54             for (long i = 0; i < 300_000; i++) {
  55                 makeImage();
  56             }
  57         } catch (OutOfMemoryError | NullPointerException e) {
  58             System.err.println("Caught expected exception:\n" +
  59                     e.getClass() + ": " + e.getMessage());
  60         }
  61         System.err.println("Test passed");
  62     }
  63 
  64     private static void makeImage() {
  65         int scanLineBytes = WIDTH / PIXELS_IN_BYTE;
  66         if ((WIDTH & (PIXELS_IN_BYTE - 1)) != 0) {
  67             // Make sure all the pixels in a scan line fit
  68             scanLineBytes += 1;
  69         }
  70 
  71         byte[]     bits    = new byte[scanLineBytes * HEIGHT];
  72         DataBuffer dataBuf = new DataBufferByte(bits, bits.length, 0);
  73         SampleModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  74                                                                   WIDTH, HEIGHT, BITS_PER_PIXEL);
  75         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuf, null);
  76         IndexColorModel indexModel = new IndexColorModel(2, 2, RED, GREEN, BLUE);
  77         BufferedImage bufImage = new BufferedImage(indexModel, raster,
  78                                                    indexModel.isAlphaPremultiplied(), null);
  79 
  80         Graphics g = bufImage.getGraphics();
  81         g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
  82         g.dispose();
  83     }
  84 }