1 /* 2 * Copyright (c) 2016, 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 8151269 27 * @author a.stepanov 28 * @summary Multiresolution image: check that the base resolution variant 29 * source is passed to the corresponding ImageConsumer 30 * @run main MultiresolutionSourceTest 31 */ 32 33 import java.awt.*; 34 import java.awt.image.*; 35 36 public class MultiresolutionSourceTest { 37 38 private static class Checker implements ImageConsumer { 39 40 private final int refW, refH, refType; 41 private final boolean refHasAlpha; 42 private final Color refColor; 43 44 public Checker(int w, 45 int h, 46 Color c, 47 boolean hasAlpha, 48 int transferType) { 49 refW = w; 50 refH = h; 51 refColor = c; 52 refHasAlpha = hasAlpha; 53 refType = transferType; 54 } 55 56 @Override 57 public void imageComplete(int status) {} 58 59 @Override 60 public void setColorModel(ColorModel model) { 61 62 boolean a = model.hasAlpha(); 63 if (a != refHasAlpha) { 64 throw new RuntimeException("invalid hasAlpha: " + a); 65 } 66 67 int tt = model.getTransferType(); 68 if (tt != refType) { 69 throw new RuntimeException("invalid transfer type: " + tt); 70 } 71 } 72 73 @Override 74 public void setDimensions(int w, int h) { 75 76 if (w != refW) { throw new RuntimeException("invalid width: " + w + 77 ", expected: " + refW); } 78 79 if (h != refH) { throw new RuntimeException("invalid height: " + h + 80 ", expected: " + refH); } 81 } 82 83 @Override 84 public void setHints(int flags) {} 85 86 @Override 87 public void setPixels(int x, int y, int w, int h, ColorModel model, 88 byte pixels[], int offset, int scansize) { 89 90 for (int i = 0; i < pixels.length; i++) { 91 int p = pixels[i]; 92 // just in case... 93 Color c = model.hasAlpha() ? 94 new Color(model.getRed (p), 95 model.getGreen(p), 96 model.getBlue (p), 97 model.getAlpha(p)) : 98 new Color(model.getRGB(p)); 99 100 if (!c.equals(refColor)) { 101 throw new RuntimeException("invalid color: " + c + 102 ", expected: " + refColor); 103 } 104 } 105 } 106 107 @Override 108 public void setPixels(int x, int y, int w, int h, ColorModel model, 109 int pixels[], int offset, int scansize) { 110 111 for (int i = 0; i < pixels.length; i++) { 112 int p = pixels[i]; 113 Color c = model.hasAlpha() ? 114 new Color(model.getRed (p), 115 model.getGreen(p), 116 model.getBlue (p), 117 model.getAlpha(p)) : 118 new Color(model.getRGB(p)); 119 120 if (!c.equals(refColor)) { 121 throw new RuntimeException("invalid color: " + c + 122 ", expected: " + refColor); 123 } 124 } 125 } 126 127 @Override 128 public void setProperties(java.util.Hashtable props) {} 129 } 130 131 private static BufferedImage generateImage(int w, int h, Color c, int type) { 132 133 BufferedImage img = new BufferedImage(w, h, type); 134 Graphics g = img.getGraphics(); 135 g.setColor(c); 136 g.fillRect(0, 0, w, h); 137 return img; 138 } 139 140 public static void main(String[] args) { 141 142 final int w1 = 20, w2 = 100, h1 = 30, h2 = 50; 143 final Color 144 c1 = new Color(255, 0, 0, 100), c2 = Color.BLACK, gray = Color.GRAY; 145 146 BufferedImage img1 = 147 generateImage(w1, h1, c1, BufferedImage.TYPE_INT_ARGB); 148 149 BufferedImage dummy = 150 generateImage(w1 + 5, h1 + 5, gray, BufferedImage.TYPE_BYTE_GRAY); 151 152 BufferedImage img2 = 153 generateImage(w2, h2, c2, BufferedImage.TYPE_BYTE_BINARY); 154 155 BufferedImage vars[] = new BufferedImage[] {img1, dummy, img2}; 156 157 // default base image index (zero) 158 BaseMultiResolutionImage mri1 = new BaseMultiResolutionImage(vars); 159 // base image index = 2 160 BaseMultiResolutionImage mri2 = new BaseMultiResolutionImage(2, vars); 161 162 // do checks 163 mri1.getSource().startProduction( 164 new Checker(w1, h1, c1, true, DataBuffer.TYPE_INT)); 165 166 mri2.getSource().startProduction( 167 new Checker(w2, h2, c2, false, DataBuffer.TYPE_BYTE)); 168 } 169 }