jdk/test/javax/imageio/plugins/png/ShortHistogramTest.java
author kvn
Thu, 21 Feb 2008 14:03:41 -0800
changeset 199 fb51d01039ff
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6621084: ciMethodBlocks::split_block_at() is broken for methods with exception handler Summary: After an exception handler block is split the exception information is not moved to the new block which starts in exception handler BCI. Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     6198111
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test verifies that PNG image reader correctly handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          hIST chunk if length of image palette in not power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run     main ShortHistogramTest 15
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.imageio.IIOImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.imageio.ImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.imageio.metadata.IIOInvalidTreeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.imageio.metadata.IIOMetadata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.imageio.metadata.IIOMetadataNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class ShortHistogramTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int numColors = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                numColors = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                System.out.println("Invalid number of colors: " + args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.out.println("Test number of colors: " + numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ShortHistogramTest t = new ShortHistogramTest(numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        t.doTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int numColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ShortHistogramTest(int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.numColors = numColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void doTest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        BufferedImage bi = createTestImage(numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        File f = writeImageWithHist(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.println("Test file is " + f.getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            ImageIO.read(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException("Test FAILED!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.println("Test PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected File writeImageWithHist(BufferedImage bi) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        File f = File.createTempFile("hist_", ".png", new File("."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        writer.setOutput(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ImageWriteParam param = writer.getDefaultWriteParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ImageTypeSpecifier type = new ImageTypeSpecifier(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        IIOMetadata imgMetadata = writer.getDefaultImageMetadata(type, param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* add hIST node to image metadata */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        imgMetadata = upgradeMetadata(imgMetadata, bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        IIOImage iio_img = new IIOImage(bi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                        null, // no thumbnails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                        imgMetadata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        writer.write(iio_img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ios.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ios.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String format = src.getNativeMetadataFormatName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Native format: " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Node root = src.getAsTree(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // add hIST node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Node n = lookupChildNode(root, "hIST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (n == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("Appending new hIST node...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Node hIST = gethISTNode(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            root.appendChild(hIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Upgraded metadata tree:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        dump(root, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println("Merging metadata...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            src.mergeTree(format, root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IIOInvalidTreeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new RuntimeException("Test FAILED!", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private IIOMetadataNode gethISTNode(BufferedImage bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        IndexColorModel icm = (IndexColorModel)bi.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int mapSize = icm.getMapSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int[] hist = new int[mapSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Arrays.fill(hist, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Raster r = bi.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int y = 0; y < bi.getHeight(); y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            for (int x = 0; x < bi.getWidth(); x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                int s = r.getSample(x, y, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                hist[s] ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        IIOMetadataNode hIST = new IIOMetadataNode("hIST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        for (int i = 0; i < hist.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            n.setAttribute("index", "" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            n.setAttribute("value", "" + hist[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            hIST.appendChild(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return hIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static Node lookupChildNode(Node root, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Node n = root.getFirstChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        while (n != null && !name.equals(n.getNodeName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            n = n.getNextSibling();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static void dump(Node node, String ident) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (node == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        System.out.printf("%s%s\n", ident, node.getNodeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // dump node attributes...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        NamedNodeMap attribs = node.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (attribs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (int i = 0; i < attribs.getLength(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                Node a = attribs.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                System.out.printf("%s  %s: %s\n", ident,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        a.getNodeName(), a.getNodeValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // dump node children...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        dump(node.getFirstChild(), ident + "    ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        dump(node.getNextSibling(), ident);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected BufferedImage createTestImage(int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        IndexColorModel icm = createTestICM(numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int w = numColors * 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int h = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        BufferedImage img = new BufferedImage(w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                BufferedImage.TYPE_BYTE_INDEXED, icm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Graphics2D g = img.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < numColors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            int rgb = icm.getRGB(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            //System.out.printf("pixel %d, rgb %x\n", i, rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            g.setColor(new Color(rgb));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            g.fillRect(i * 10, 0, w - i * 10, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
       return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    protected IndexColorModel createTestICM(int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int[] palette = createTestPalette(numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int numBits = getNumBits(numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        IndexColorModel icm = new IndexColorModel(numBits, numColors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                palette, 0, false, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                DataBuffer.TYPE_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return icm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    protected static int getNumBits(int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (numColors < 0 || 256 < numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new RuntimeException("Unsupported number of colors: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                       numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int numBits = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        int limit = 1 << numBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        while (numColors > limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            numBits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            limit = 1 << numBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return numBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    protected static int[] createTestPalette(int numColors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        int[] palette = new int[numColors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < numColors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int r = rnd.nextInt(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int g = rnd.nextInt(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int b = rnd.nextInt(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            palette[i] = 0xff000000 | (r << 16) | (g << 8) | b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return palette;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}