Skip to content

Commit

Permalink
8318850: Duplicate code in the LCMSImageLayout
Browse files Browse the repository at this point in the history
Reviewed-by: prr
  • Loading branch information
mrserb committed Oct 27, 2023
1 parent c593f8b commit d226014
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,30 @@

final class LCMSImageLayout {

static int BYTES_SH(int x) {
private static int BYTES_SH(int x) {
return x;
}

private static int EXTRA_SH(int x) {
return x << 7;
}

static int CHANNELS_SH(int x) {
private static int CHANNELS_SH(int x) {
return x << 3;
}

static int PREMUL_SH(int x) {
return x << 23;
}
private static final int PREMUL = 1 << 23;
private static final int SWAPFIRST = 1 << 14;
private static final int DOSWAP = 1 << 10;
private static final int PT_GRAY_8 = CHANNELS_SH(1) | BYTES_SH(1);
private static final int PT_GRAY_16 = CHANNELS_SH(1) | BYTES_SH(2);
private static final int PT_RGB_8 = CHANNELS_SH(3) | BYTES_SH(1);
private static final int PT_RGBA_8 = PT_RGB_8 | EXTRA_SH(1);
private static final int PT_ARGB_8 = PT_RGBA_8 | SWAPFIRST;
private static final int PT_ARGB_8_PREMUL = PT_ARGB_8 | PREMUL_SH(1);
private static final int PT_ARGB_8_PREMUL = PT_ARGB_8 | PREMUL;
private static final int PT_BGR_8 = PT_RGB_8 | DOSWAP;
private static final int PT_ABGR_8 = PT_BGR_8 | EXTRA_SH(1);
private static final int PT_ABGR_8_PREMUL = PT_ABGR_8 | PREMUL_SH(1);
private static final int PT_ABGR_8_PREMUL = PT_ABGR_8 | PREMUL;
// private static final int PT_BGRA_8 = PT_ABGR_8 | SWAPFIRST;
private static final int SWAP_ENDIAN =
ByteOrder.nativeOrder() == LITTLE_ENDIAN ? DOSWAP : 0;
Expand Down Expand Up @@ -186,7 +184,8 @@ static LCMSImageLayout createImageLayout(BufferedImage image) {
l.dataArrayLength = 4 * intRaster.getDataStorage().length;
l.dataType = DT_INT;
}
case BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_4BYTE_ABGR,
case BufferedImage.TYPE_BYTE_GRAY, BufferedImage.TYPE_3BYTE_BGR,
BufferedImage.TYPE_4BYTE_ABGR,
BufferedImage.TYPE_4BYTE_ABGR_PRE ->
{
var byteRaster = (ByteComponentRaster) image.getRaster();
Expand All @@ -198,15 +197,6 @@ static LCMSImageLayout createImageLayout(BufferedImage image) {
l.dataArrayLength = byteRaster.getDataStorage().length;
l.dataType = DT_BYTE;
}
case BufferedImage.TYPE_BYTE_GRAY -> {
var byteRaster = (ByteComponentRaster) image.getRaster();
l.nextRowOffset = byteRaster.getScanlineStride();
l.nextPixelOffset = byteRaster.getPixelStride();
l.offset = byteRaster.getDataOffset(0);
l.dataArray = byteRaster.getDataStorage();
l.dataArrayLength = byteRaster.getDataStorage().length;
l.dataType = DT_BYTE;
}
case BufferedImage.TYPE_USHORT_GRAY -> {
var shortRaster = (ShortComponentRaster) image.getRaster();
l.nextRowOffset = safeMult(2, shortRaster.getScanlineStride());
Expand Down Expand Up @@ -297,7 +287,7 @@ static LCMSImageLayout createImageLayout(Raster r, ColorModel cm) {
l.pixelType = (hasAlpha ? CHANNELS_SH(numBands - 1) | EXTRA_SH(1)
: CHANNELS_SH(numBands)) | BYTES_SH(1);
if (hasAlpha && cm.isAlphaPremultiplied()) {
l.pixelType |= PREMUL_SH(1);
l.pixelType |= PREMUL;
}

int[] bandOffsets = csm.getBandOffsets();
Expand Down
56 changes: 56 additions & 0 deletions test/jdk/java/awt/image/BufferedImage/VerifyNumBands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.image.BufferedImage;

/**
* @test
* @bug 8318850
* @summary Checks the total number of bands of image data
*/
public final class VerifyNumBands {

public static void main(String[] args) {
test(BufferedImage.TYPE_INT_RGB, 3);
test(BufferedImage.TYPE_INT_ARGB, 4);
test(BufferedImage.TYPE_INT_ARGB_PRE, 4);
test(BufferedImage.TYPE_INT_BGR, 3);
test(BufferedImage.TYPE_3BYTE_BGR, 3);
test(BufferedImage.TYPE_4BYTE_ABGR, 4);
test(BufferedImage.TYPE_4BYTE_ABGR_PRE, 4);
test(BufferedImage.TYPE_USHORT_565_RGB, 3);
test(BufferedImage.TYPE_USHORT_555_RGB, 3);
test(BufferedImage.TYPE_BYTE_GRAY, 1);
test(BufferedImage.TYPE_USHORT_GRAY, 1);
}

private static void test(int type, int expected) {
BufferedImage bi = new BufferedImage(1, 1, type);
int numBands = bi.getRaster().getSampleModel().getNumBands();
if (numBands != expected) {
System.err.println("Expected: " + expected);
System.err.println("Actual: " + numBands);
throw new RuntimeException("wrong number of bands");
}
}
}

1 comment on commit d226014

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.