1/* 2 * Copyright (c) 2006, 2011, 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 * 26 * @test 27 * @bug 4521945 7006865 28 * @summary Test printing images of different types. 29 * @author prr 30 * @run main/manual=yesno/timeout=900 ImageTypes 31 */ 32 33import java.io.*; 34import static java.awt.Color.*; 35import java.awt.*; 36import java.awt.geom.*; 37import java.awt.event.*; 38import java.awt.print.*; 39import java.awt.image.*; 40import static java.awt.image.BufferedImage.*; 41import javax.print.*; 42import javax.print.attribute.*; 43import javax.print.attribute.standard.*; 44 45public class ImageTypes extends Frame implements ActionListener { 46 47 private ImageCanvas c; 48 49 public static void main(String args[]) { 50 51 ImageTypes f = new ImageTypes(); 52 f.show(); 53 } 54 55 public ImageTypes () { 56 super("Image Types Printing Test"); 57 c = new ImageCanvas(); 58 add("Center", c); 59 60 Button printThisButton = new Button("Print"); 61 printThisButton.addActionListener(this); 62 Panel p = new Panel(); 63 p.add(printThisButton); 64 add("South", p); 65 add("North", getInstructions()); 66 addWindowListener(new WindowAdapter() { 67 public void windowClosing(WindowEvent e) { 68 System.exit(0); 69 } 70 }); 71 72 pack(); 73 } 74 75 private TextArea getInstructions() { 76 TextArea ta = new TextArea(10, 60); 77 ta.setFont(new Font("Dialog", Font.PLAIN, 11)); 78 ta.setText 79 ("This is a manual test as it requires that you compare "+ 80 "the on-screen rendering with the printed output.\n"+ 81 "Select the 'Print' button to print out the test.\n"+ 82 "For each image compare the printed one to the on-screen one.\n"+ 83 "The test PASSES if the onscreen and printed rendering match."); 84 return ta; 85 } 86 87 public void actionPerformed(ActionEvent e) { 88 PrinterJob pj = PrinterJob.getPrinterJob(); 89 90 PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); 91 if (pj != null && pj.printDialog(attrs)) { 92 pj.setPrintable(c); 93 try { 94 pj.print(attrs); 95 } catch (PrinterException pe) { 96 pe.printStackTrace(); 97 throw new RuntimeException("Exception whilst printing."); 98 } finally { 99 System.out.println("PRINT RETURNED OK."); 100 } 101 } 102 } 103} 104 105class ImageCanvas extends Component implements Printable { 106 107 IndexColorModel icm2 = null; 108 IndexColorModel icm4 = null; 109 BufferedImage opaqueImg = null; 110 BufferedImage transImg = null; 111 int sw=99, sh=99; 112 113 void paintImage(BufferedImage bi, Color c1, Color c2) { 114 115 GradientPaint tp= new GradientPaint(0.0f, 0.0f, c1, 10f, 8f, c2, true); 116 Graphics2D g2d = (Graphics2D)bi.getGraphics(); 117 g2d.setPaint(tp); 118 g2d.fillRect(0, 0, sw, sh); 119 g2d.setColor(gray); 120 int cnt=0; 121 Font font = new Font("Serif", Font.PLAIN, 11); 122 g2d.setFont(font); 123 FontMetrics fm = g2d.getFontMetrics(); 124 for (int y=12;y<sh;y+=12) { 125 int x = 0; 126 while (x < sw) { 127 String s = (new Integer(++cnt)).toString(); 128 g2d.drawString(s, x, y); 129 x+= fm.stringWidth(s); 130 } 131 } 132 } 133 134 ImageCanvas() { 135 136 opaqueImg = new BufferedImage(sw, sh, TYPE_INT_RGB); 137 Color o1 = new Color(0, 0, 0); 138 Color o2 = new Color(255, 255, 255); 139 paintImage(opaqueImg, o1, o2); 140 141 transImg = new BufferedImage(sw, sh, TYPE_INT_ARGB); 142 Color t1 = new Color(255, 255, 255, 220); 143 Color t2 = new Color(255, 200, 0, 220); 144 paintImage(transImg, t1, t2); 145 146 /* greyscale 2bpp */ 147 byte[] arr2bpp = {(byte)0, (byte)0x55, (byte)0xaa, (byte)0xff}; 148 icm2 = new IndexColorModel(2, 4, arr2bpp, arr2bpp, arr2bpp); 149 150 /* color 4bpp */ 151 int[] cmap = new int[16]; 152 cmap[0] = black.getRGB(); 153 cmap[1] = white.getRGB(); 154 cmap[2] = gray.getRGB(); 155 cmap[3] = lightGray.getRGB(); 156 cmap[4] = red.getRGB(); 157 cmap[5] = green.getRGB(); 158 cmap[6] = blue.getRGB(); 159 cmap[7] = yellow.getRGB(); 160 cmap[8] = cyan.getRGB(); 161 cmap[9] = magenta.getRGB(); 162 cmap[10] = orange.getRGB(); 163 cmap[11] = pink.getRGB(); 164 cmap[12] = darkGray.getRGB(); 165 cmap[13] = 192 << 16 ; // dark red. 166 cmap[14] = 192 << 8; // dark green 167 cmap[15] = 192; // dark blue 168 169 icm4 = new IndexColorModel(4, 16, cmap, 0, false, -1, 170 DataBuffer.TYPE_BYTE); 171 172 } 173 174 175 public int print(Graphics g, PageFormat pgFmt, int pgIndex) { 176 177 if (pgIndex > 0) { 178 return Printable.NO_SUCH_PAGE; 179 } 180 Graphics2D g2d = (Graphics2D)g; 181 g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); 182 paint(g2d); 183 return Printable.PAGE_EXISTS; 184 } 185 186 private void drawImage(Graphics g, int biType, IndexColorModel icm) { 187 188 BufferedImage bi; 189 if (icm != null) { 190 bi = new BufferedImage(sw, sh, biType, icm); 191 } else { 192 bi = new BufferedImage(sw, sh, biType); 193 } 194 195 Graphics big = bi.getGraphics(); 196 if (bi.getColorModel().getPixelSize() <=2) { 197 big.drawImage(opaqueImg, 0, 0, null); 198 } else { 199 big.drawImage(transImg, 0, 0, null); 200 } 201 g.drawImage(bi, 0, 0, null); 202 } 203 204 public void paint(Graphics g) { 205 206 int incX = sw+10, incY = sh+10; 207 208 g.translate(10, 10); 209 210 drawImage(g, TYPE_INT_RGB, null); 211 g.translate(incX, 0); 212 213 drawImage(g, TYPE_INT_BGR, null); 214 g.translate(incX, 0); 215 216 drawImage(g, TYPE_INT_ARGB, null); 217 g.translate(incX, 0); 218 219 drawImage(g, TYPE_INT_ARGB_PRE, null); 220 g.translate(-3*incX, incY); 221 222 drawImage(g, TYPE_3BYTE_BGR, null); 223 g.translate(incX, 0); 224 225 drawImage(g, TYPE_4BYTE_ABGR, null); 226 g.translate(incX, 0); 227 228 drawImage(g, TYPE_4BYTE_ABGR_PRE, null); 229 g.translate(incX, 0); 230 231 drawImage(g, TYPE_USHORT_555_RGB, null); 232 g.translate(-3*incX, incY); 233 234 drawImage(g, TYPE_USHORT_555_RGB, null); 235 g.translate(incX, 0); 236 237 drawImage(g, TYPE_USHORT_GRAY, null); 238 g.translate(incX, 0); 239 240 drawImage(g, TYPE_BYTE_GRAY, null); 241 g.translate(incX, 0); 242 243 drawImage(g, TYPE_BYTE_INDEXED, null); 244 g.translate(-3*incX, incY); 245 246 drawImage(g, TYPE_BYTE_BINARY, null); 247 g.translate(incX, 0); 248 249 drawImage(g, TYPE_BYTE_BINARY, icm2); 250 g.translate(incX, 0); 251 252 drawImage(g, TYPE_BYTE_BINARY, icm4); 253 g.translate(incX, 0); 254 255 drawImage(g, TYPE_BYTE_INDEXED, icm2); 256 g.translate(-3*incX, incY); 257 258 drawImage(g, TYPE_BYTE_INDEXED, icm4); 259 g.translate(incX, 0); 260 } 261 262 263 264 /* Size is chosen to match default imageable width of a NA letter 265 * page. This means there will be clipping, what is clipped will 266 * depend on PageFormat orientation. 267 */ 268 public Dimension getPreferredSize() { 269 return new Dimension(468, 600); 270 } 271 272} 273