1 /*
   2  * Copyright (c) 2011, 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  * @summary Basic raster test
  27  * @summary com.apple.junit.java.graphics.images
  28  * @run junit WritableRaster02
  29  */
  30 
  31 import junit.framework.*;
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.image.*;
  35 
  36 public class WritableRaster02 extends TestCase {
  37 
  38     public static Test suite() {
  39         return new TestSuite(WritableRaster02.class);
  40     }
  41 
  42     public static void main (String[] args) throws RuntimeException {
  43         TestResult tr = junit.textui.TestRunner.run(suite());
  44         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  45             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
  46         }
  47     }
  48 
  49     // Lookup tables for BYTE_BINARY 1, 2 and 4 bits.
  50     static byte[] lut1 = new byte[] {0, (byte)255 };
  51     static byte[] lut2 = new byte[] {0, (byte)85, (byte)170, (byte)255};
  52 
  53     static byte[] lut4 = new byte[] {0, (byte)17, (byte)34, (byte)51,
  54                                   (byte)68, (byte)85,(byte) 102, (byte)119,
  55                                   (byte)136, (byte)153, (byte)170, (byte)187,
  56                                   (byte)204, (byte)221, (byte)238, (byte)255};
  57 
  58     static byte[] lut4r = new byte[] {0, 0, 0, 0,
  59                                     0, 0, 0, 0,
  60                                     (byte)255, (byte)255, (byte)255, (byte)255,
  61                                     (byte)255, (byte)255, (byte)255, (byte)255 };
  62 
  63 
  64 
  65 
  66     class RasterFrame extends JFrame {
  67         static final int w = 200;
  68         static final int h = 500;
  69 
  70         BufferedImage bi1 = null;
  71         BufferedImage bi2 = null;
  72         BufferedImage bi4 = null;
  73 
  74         boolean initialized = false;
  75         boolean painted1 = false;
  76         boolean painted2 = false;
  77         boolean painted4 = false;
  78 
  79         void initBaseImage1() {
  80             int bytesPerRow = w / 8;
  81             if (w  % 8 != 0) {
  82                 bytesPerRow++;
  83             }
  84 
  85             byte[] imageData = new byte[h * bytesPerRow];
  86 
  87             IndexColorModel cm = new IndexColorModel(1, 2, lut1, lut1, lut1);
  88             DataBuffer db = new DataBufferByte( imageData, imageData.length );
  89             WritableRaster wr = Raster.createPackedRaster(db, w, h, 1, null);
  90             bi1 = new BufferedImage( cm, wr, false, null );
  91 
  92             // Intialize the base image with a gradient fill.  We could use anything here.
  93             Graphics2D ig = (Graphics2D) bi1.createGraphics();
  94             GradientPaint gp = new GradientPaint( 0, 0, Color.white, w, h, Color.black, false );
  95             ig.setPaint( gp );
  96             ig.fillRect( 0, 0, w, h );
  97             ig.setColor( Color.black );
  98             ig.drawString( "1 bit IndexColorModel", 30, 30 );
  99             ig.dispose();
 100         }
 101 
 102         void initBaseImage2() {
 103             int bytesPerRow = (w * 2) / 8;
 104             if ((w * 2) % 8 != 0) {
 105                 bytesPerRow++;
 106             }
 107 
 108             byte[] imageData = new byte[h * bytesPerRow];
 109 
 110             IndexColorModel cm = new IndexColorModel(2, 4, lut2, lut2, lut2);
 111             DataBuffer db = new DataBufferByte( imageData, imageData.length );
 112             WritableRaster wr = Raster.createPackedRaster(db, w, h, 2, null);
 113             bi2 = new BufferedImage( cm, wr, false, null );
 114 
 115             // Intialize the base image with a gradient fill.  We could use anything here.
 116             Graphics2D ig = (Graphics2D) bi2.createGraphics();
 117             GradientPaint gp = new GradientPaint( 0, 0, Color.white, w, h, Color.black, false );
 118             ig.setPaint( gp );
 119             ig.fillRect( 0, 0, w, h );
 120             ig.setColor( Color.black );
 121             ig.drawString( "2 bit IndexColorModel", 30, 30 );
 122             ig.dispose();
 123         }
 124 
 125         void initBaseImage4() {
 126             int bytesPerRow = (w * 4) / 8;
 127             if ((w * 4) % 8 != 0) {
 128                 bytesPerRow++;
 129             }
 130 
 131             byte[] imageData = new byte[h * bytesPerRow];
 132 
 133             IndexColorModel cm = new IndexColorModel(4, 16, lut4r, lut4, lut4);
 134             DataBuffer db = new DataBufferByte( imageData, imageData.length );
 135             WritableRaster wr = Raster.createPackedRaster(db, w, h, 4, null);
 136             bi4 = new BufferedImage( cm, wr, false, null );
 137 
 138             // Intialize the base image with a gradient fill.  We could use anything here.
 139             Graphics2D ig = (Graphics2D) bi4.createGraphics();
 140             GradientPaint gp = new GradientPaint( 0, 0, Color.white, w, h, Color.black, false );
 141             ig.setPaint( gp );
 142             ig.fillRect( 0, 0, w, h );
 143             ig.setColor( Color.black );
 144             ig.drawString( "4 bit IndexColorModel", 30, 30 );
 145             ig.dispose();
 146         }
 147 
 148 
 149         class TestPanel1 extends JPanel {
 150             public TestPanel1() {
 151                 setPreferredSize( new Dimension( w, h ) );
 152             }
 153 
 154             public void paint( Graphics g ) {
 155                 super.paint( g );
 156                 Dimension dim = getSize();
 157                 if ((initialized) && (bi1 != null)) {
 158                     g.drawImage( bi1, 0, 0, dim.width - 1, dim.height - 1, null );
 159                     if (!painted1) {
 160                         painted1 = true;
 161                     }
 162                 }
 163                 else {
 164                     g.setColor( Color.red );
 165                     g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 166                 }
 167             }
 168         }
 169 
 170         class TestPanel2 extends JPanel {
 171             public TestPanel2() {
 172                 setPreferredSize( new Dimension( w, h ) );
 173             }
 174 
 175             public void paint( Graphics g ) {
 176                 super.paint( g );
 177                 Dimension dim = getSize();
 178                 if ((initialized) && (bi2 != null)) {
 179                     g.drawImage( bi2, 0, 0, dim.width - 1, dim.height - 1, null );
 180                     if (!painted2) {
 181                         painted2 = true;
 182                     }
 183                 }
 184                 else {
 185                     g.setColor( Color.red );
 186                     g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 187                 }
 188             }
 189         }
 190 
 191         class TestPanel4 extends JPanel {
 192             public TestPanel4() {
 193                 setPreferredSize( new Dimension( w, h ) );
 194             }
 195 
 196             public void paint( Graphics g ) {
 197                 super.paint( g );
 198                 Dimension dim = getSize();
 199                 if ((initialized) && (bi4 != null)) {
 200                     g.drawImage( bi4, 0, 0, dim.width - 1, dim.height - 1, null );
 201                     if (!painted4) {
 202                         painted4 = true;
 203                     }
 204                 }
 205                 else {
 206                     g.setColor( Color.red );
 207                     g.drawRect( 0, 0, dim.width - 1, dim.height - 1 );
 208                 }
 209             }
 210         }
 211 
 212 
 213         public RasterFrame() throws Exception {
 214             super( "RasterFrame" );
 215             initBaseImage1();
 216             initBaseImage2();
 217             initBaseImage4();
 218             addPanels();
 219             pack();
 220             initialized = true;
 221             setVisible( true );
 222             int count = 0;
 223             while (((painted1 == false) || (painted2 == false) || (painted4 == false)) && count < 50) {
 224                 Thread.sleep( 500 );
 225                 count++;
 226                 repaint();
 227             }
 228             assertTrue("Should have painted a 1-bit indexed image", painted1);
 229             assertTrue("Should have painted a 2-bit indexed image", painted2);
 230             assertTrue("Should have painted a 4-bit indexed image", painted4);
 231         }
 232 
 233         void addPanels() {
 234             getContentPane().setLayout( new GridLayout( 1, 2, 10, 10 ) );
 235             getContentPane().add( new TestPanel1() );
 236             getContentPane().add( new TestPanel2() );
 237             getContentPane().add( new TestPanel4() );
 238         }
 239     }
 240 
 241     public void testRaster() throws Exception {
 242         RasterFrame rf = null;
 243         try {
 244             rf = new RasterFrame();
 245             Thread.sleep( 500 );
 246         } finally {
 247             assertNotNull(rf);
 248             rf.dispose();
 249         }
 250     }
 251 
 252 }