jdk/test/java/awt/image/DrawImage/IncorrectAlphaConversionBicubic.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 27511 4b0eecf677c0
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27511
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 27511
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27511
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     4
 *
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     8
 *
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    13
 * accompanied this code).
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    14
 *
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    18
 *
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    21
 * questions.
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    22
 */
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    23
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    24
import java.awt.AlphaComposite;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    25
import java.awt.Color;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    26
import java.awt.Graphics2D;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    27
import java.awt.GraphicsConfiguration;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    28
import java.awt.GraphicsDevice;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    29
import java.awt.GraphicsEnvironment;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    30
import java.awt.RenderingHints;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    31
import java.awt.image.BufferedImage;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    32
import java.awt.image.DataBuffer;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    33
import java.awt.image.DataBufferByte;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    34
import java.awt.image.DataBufferInt;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    35
import java.awt.image.DataBufferShort;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    36
import java.awt.image.VolatileImage;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    37
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    38
import static java.awt.Transparency.TRANSLUCENT;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    39
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    40
/**
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    41
 * @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 27511
diff changeset
    42
 * @key headful
27511
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    43
 * @bug 8062164
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    44
 * @summary We should get correct alpha, when we draw to/from VolatileImage and
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    45
 *          bicubic interpolation is enabled
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    46
 * @author Sergey Bylokhov
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    47
 */
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    48
public final class IncorrectAlphaConversionBicubic {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    49
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    50
    private static final Color RGB = new Color(200, 255, 7, 123);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    51
    private static final int SIZE = 100;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    52
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    53
    public static void main(final String[] args) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    54
        final GraphicsEnvironment ge =
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    55
                GraphicsEnvironment.getLocalGraphicsEnvironment();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    56
        final GraphicsDevice gd = ge.getDefaultScreenDevice();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    57
        final GraphicsConfiguration gc = gd.getDefaultConfiguration();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    58
        final VolatileImage vi =
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    59
                gc.createCompatibleVolatileImage(SIZE, SIZE, TRANSLUCENT);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    60
        final BufferedImage bi = makeUnmanagedBI(gc, TRANSLUCENT);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    61
        final int expected = bi.getRGB(2, 2);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    62
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    63
        int attempt = 0;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    64
        BufferedImage snapshot;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    65
        while (true) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    66
            if (++attempt > 10) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    67
                throw new RuntimeException("Too many attempts: " + attempt);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    68
            }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    69
            vi.validate(gc);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    70
            final Graphics2D g2d = vi.createGraphics();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    71
            g2d.setComposite(AlphaComposite.Src);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    72
            g2d.scale(2, 2);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    73
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    74
                                 RenderingHints.VALUE_INTERPOLATION_BICUBIC);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    75
            g2d.drawImage(bi, 0, 0, null);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    76
            g2d.dispose();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    77
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    78
            snapshot = vi.getSnapshot();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    79
            if (vi.contentsLost()) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    80
                continue;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    81
            }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    82
            break;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    83
        }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    84
        final int actual = snapshot.getRGB(2, 2);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    85
        if (actual != expected) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    86
            System.err.println("Actual: " + Integer.toHexString(actual));
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    87
            System.err.println("Expected: " + Integer.toHexString(expected));
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    88
            throw new RuntimeException("Test failed");
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    89
        }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    90
    }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    91
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    92
    private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    93
                                                 int type) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    94
        BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    95
        Graphics2D g2d = img.createGraphics();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    96
        g2d.setColor(RGB);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    97
        g2d.fillRect(0, 0, SIZE, SIZE);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    98
        g2d.dispose();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
    99
        final DataBuffer db = img.getRaster().getDataBuffer();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   100
        if (db instanceof DataBufferInt) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   101
            ((DataBufferInt) db).getData();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   102
        } else if (db instanceof DataBufferShort) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   103
            ((DataBufferShort) db).getData();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   104
        } else if (db instanceof DataBufferByte) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   105
            ((DataBufferByte) db).getData();
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   106
        } else {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   107
            try {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   108
                img.setAccelerationPriority(0.0f);
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   109
            } catch (final Throwable ignored) {
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   110
            }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   111
        }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   112
        return img;
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   113
    }
4b0eecf677c0 8062164: Incorrect color conversion, when bicubic interpolation is used
serb
parents:
diff changeset
   114
}