< prev index next >

test/jdk/java/awt/image/BufferedImage/ICMColorDataTest/ICMColorDataTest.java

Print this page


   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  * @requires os.maxMemory >= 2G
  40  * @run main/othervm/timeout=300000 -Xms1000m -Xmx1000m ICMColorDataTest
  41  */
  42 public class ICMColorDataTest {
  43     private static final int WIDTH  = 90;
  44     private static final int HEIGHT = 90;
  45     private static final int BITS_PER_PIXEL = 1;
  46     private static final int PIXELS_IN_BYTE = 8;
  47 
  48     // Color model components
  49     private static final byte[] RED   = { (byte) 255, 0 };
  50     private static final byte[] GREEN = { (byte) 255, 0 };
  51     private static final byte[] BLUE  = { (byte) 255, 0 };
  52 







  53     public static void main(String[] args) {
  54         try {
  55             for (long i = 0; i < 300_000; i++) {




  56                 makeImage();
  57             }
  58         } catch (OutOfMemoryError | NullPointerException e) {
  59             System.err.println("Caught expected exception:\n" +
  60                     e.getClass() + ": " + e.getMessage());
  61         }
  62         System.err.println("Test passed");
  63     }
  64 
  65     private static void makeImage() {
  66         int scanLineBytes = WIDTH / PIXELS_IN_BYTE;
  67         if ((WIDTH & (PIXELS_IN_BYTE - 1)) != 0) {
  68             // Make sure all the pixels in a scan line fit
  69             scanLineBytes += 1;
  70         }
  71 
  72         byte[]     bits    = new byte[scanLineBytes * HEIGHT];
  73         DataBuffer dataBuf = new DataBufferByte(bits, bits.length, 0);
  74         SampleModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  75                                                                   WIDTH, HEIGHT, BITS_PER_PIXEL);
  76         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuf, null);
  77         IndexColorModel indexModel = new IndexColorModel(2, 2, RED, GREEN, BLUE);
  78         BufferedImage bufImage = new BufferedImage(indexModel, raster,
  79                                                    indexModel.isAlphaPremultiplied(), null);
  80 
  81         Graphics g = bufImage.getGraphics();
  82         g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
  83         g.dispose();




  84     }
  85 }
   1 /*
   2  * Copyright (c) 2018, 2019, 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 import java.lang.ref.PhantomReference;
  34 import java.lang.ref.Reference;
  35 import java.lang.ref.ReferenceQueue;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 
  39 /*
  40  * @test
  41  * @bug 8201433 8232634
  42  * @summary This test may throw OOME or NPE from native code,
  43  *          it should not crash
  44  * @requires os.maxMemory >= 2G
  45  * @run main/othervm/timeout=600 -Xms1000m -Xmx1000m -XX:+UseSerialGC ICMColorDataTest
  46  */
  47 public class ICMColorDataTest {
  48     private static final int WIDTH  = 90;
  49     private static final int HEIGHT = 90;
  50     private static final int BITS_PER_PIXEL = 1;
  51     private static final int PIXELS_IN_BYTE = 8;
  52 
  53     // Color model components
  54     private static final byte[] RED   = { (byte) 255, 0 };
  55     private static final byte[] GREEN = { (byte) 255, 0 };
  56     private static final byte[] BLUE  = { (byte) 255, 0 };
  57 
  58     private static final int MAX_SIZE = 10;
  59 
  60     private static final ReferenceQueue<Object> refQueue =
  61             new ReferenceQueue<>();
  62     private static final List<Reference<Object>> refList =
  63             new ArrayList<>(MAX_SIZE);
  64 
  65     public static void main(String[] args) {
  66         try {
  67             for (long i = 0; i < 30_000; i++) {
  68                 if (refQueue.poll() != null) {
  69                     System.err.println("Garbage collector was invoked");
  70                     break;
  71                 }
  72                 makeImage();
  73             }
  74         } catch (OutOfMemoryError | NullPointerException e) {
  75             System.err.println("Caught expected exception:\n" +
  76                     e.getClass() + ": " + e.getMessage());
  77         }
  78         System.err.println("Test passed");
  79     }
  80 
  81     private static void makeImage() {
  82         int scanLineBytes = WIDTH / PIXELS_IN_BYTE;
  83         if ((WIDTH & (PIXELS_IN_BYTE - 1)) != 0) {
  84             // Make sure all the pixels in a scan line fit
  85             scanLineBytes += 1;
  86         }
  87 
  88         byte[]     bits    = new byte[scanLineBytes * HEIGHT];
  89         DataBuffer dataBuf = new DataBufferByte(bits, bits.length, 0);
  90         SampleModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  91                                                                   WIDTH, HEIGHT, BITS_PER_PIXEL);
  92         WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuf, null);
  93         IndexColorModel indexModel = new IndexColorModel(2, 2, RED, GREEN, BLUE);
  94         BufferedImage bufImage = new BufferedImage(indexModel, raster,
  95                                                    indexModel.isAlphaPremultiplied(), null);
  96 
  97         Graphics g = bufImage.getGraphics();
  98         g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
  99         g.dispose();
 100 
 101         if (refList.size() < MAX_SIZE) {
 102             refList.add(new PhantomReference<>(bufImage, refQueue));
 103         }
 104     }
 105 }
< prev index next >