Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mstr2 committed Oct 22, 2024
1 parent 7be3d8a commit 40e960d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public final class ImageFrame {
* @param imageData The image data.
* @param width The image width.
* @param height The image height.
* @param stride The stride from a pixel position in one row to the same
* horizontal position in the next row.
* @param stride The stride from a pixel position in one row to the same horizontal position in the next row,
* in data elements (not necessarily bytes).
* @param metadata The image metadata.
*/
public ImageFrame(ImageType imageType, Buffer imageData,
Expand All @@ -67,7 +67,8 @@ public ImageFrame(ImageType imageType, Buffer imageData,
* @param imageData The image data.
* @param width The image width.
* @param height The image height.
* @param stride The stride from a pixel position in one row to the same horizontal position in the next row.
* @param stride The stride from a pixel position in one row to the same horizontal position in the next row,
* in data elements (not necessarily bytes).
* @param pixelScale The scale of a 72DPI virtual pixel in the resolution of the image
* (1.0f for 72DPI images, 2.0f for 144DPI images, etc.).
* @param metadata The image metadata.
Expand All @@ -86,7 +87,8 @@ public ImageFrame(ImageType imageType, Buffer imageData,
* @param imageData The image data.
* @param width The image width.
* @param height The image height.
* @param stride The stride from a pixel position in one row to the same horizontal position in the next row.
* @param stride The stride from a pixel position in one row to the same horizontal position in the next row,
* in data elements (not necessarily bytes).
* @param palette The image palette. This is ignored unless the type is one of the palette types.
* @param paletteIndexBits The size of a palette index, in bits.
* @param pixelScale The scale of a 72DPI virtual pixel in the resolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.sun.javafx.iio.ImageLoadListener;
import com.sun.javafx.iio.ImageLoader;
import com.sun.javafx.iio.ImageMetadata;
import com.sun.javafx.iio.ImageStorage;
import com.sun.javafx.iio.ImageStorage.ImageType;
import com.sun.javafx.iio.ImageStorageException;
import com.sun.javafx.iio.common.ImageDescriptor;
import com.sun.javafx.iio.common.ImageTools;
Expand Down Expand Up @@ -154,64 +154,65 @@ public ImageFrame load(int imageIndex, double w, double h, boolean preserveAspec
null, null, image.getWidth(), image.getHeight(), null,
null, null);

// Scanline stride is measured in elements of the underlying buffer, which can be bytes or ints.
int scanlineStride = switch(image.getSampleModel()) {
case ComponentSampleModel m -> m.getScanlineStride();
case MultiPixelPackedSampleModel m -> m.getScanlineStride();
case SinglePixelPackedSampleModel m -> m.getScanlineStride();
default -> throw new IllegalStateException("Unsupported sample model: " + image.getSampleModel());
};

return switch (image.getType()) {
case TYPE_BYTE_GRAY -> new ImageFrame(ImageStorage.ImageType.GRAY,
case TYPE_BYTE_GRAY -> new ImageFrame(ImageType.GRAY,
getByteBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth(),
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_3BYTE_BGR -> new ImageFrame(ImageStorage.ImageType.BGR,
case TYPE_3BYTE_BGR -> new ImageFrame(ImageType.BGR,
getByteBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 3,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_4BYTE_ABGR -> new ImageFrame(ImageStorage.ImageType.ABGR,
case TYPE_4BYTE_ABGR -> new ImageFrame(ImageType.ABGR,
getByteBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_4BYTE_ABGR_PRE -> new ImageFrame(ImageStorage.ImageType.ABGR_PRE,
case TYPE_4BYTE_ABGR_PRE -> new ImageFrame(ImageType.ABGR_PRE,
getByteBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_INT_RGB -> new ImageFrame(ImageStorage.ImageType.INT_RGB,
case TYPE_INT_RGB -> new ImageFrame(ImageType.INT_RGB,
getIntBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_INT_BGR -> new ImageFrame(ImageStorage.ImageType.INT_BGR,
case TYPE_INT_BGR -> new ImageFrame(ImageType.INT_BGR,
getIntBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_INT_ARGB -> new ImageFrame(ImageStorage.ImageType.INT_ARGB,
case TYPE_INT_ARGB -> new ImageFrame(ImageType.INT_ARGB,
getIntBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_INT_ARGB_PRE -> new ImageFrame(ImageStorage.ImageType.INT_ARGB_PRE,
case TYPE_INT_ARGB_PRE -> new ImageFrame(ImageType.INT_ARGB_PRE,
getIntBuffer(image.getRaster().getDataBuffer()),
image.getWidth(), image.getHeight(), image.getWidth() * 4,
image.getWidth(), image.getHeight(), scanlineStride,
pixelScale, metadata);

case TYPE_BYTE_BINARY, TYPE_BYTE_INDEXED -> {
var colorModel = (IndexColorModel)image.getColorModel();
var palette = new int[colorModel.getMapSize()];
IndexColorModel colorModel = (IndexColorModel)image.getColorModel();
int[] palette = new int[colorModel.getMapSize()];
colorModel.getRGBs(palette);

var imageType = colorModel.hasAlpha()
ImageType imageType = colorModel.hasAlpha()
? colorModel.isAlphaPremultiplied()
? ImageStorage.ImageType.PALETTE_ALPHA_PRE
: ImageStorage.ImageType.PALETTE_ALPHA
: ImageStorage.ImageType.PALETTE;

var scanlineStride = switch(image.getSampleModel()) {
case ComponentSampleModel m -> m.getScanlineStride();
case MultiPixelPackedSampleModel m -> m.getScanlineStride();
case SinglePixelPackedSampleModel m -> m.getScanlineStride();
default -> throw new IllegalStateException("Unsupported sample model: " + image.getSampleModel());
};
? ImageType.PALETTE_ALPHA_PRE
: ImageType.PALETTE_ALPHA
: ImageType.PALETTE;

yield new ImageFrame(
imageType, getByteBuffer(image.getRaster().getDataBuffer()),
Expand Down
19 changes: 11 additions & 8 deletions modules/javafx.graphics/src/main/java/com/sun/prism/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ public static Image convertImageFrame(ImageFrame frame) {
case GRAY -> fromByteGrayData((ByteBuffer)frame.getImageData(), w, h, stride, ps);
case RGB -> fromByteRgbData((ByteBuffer)frame.getImageData(), w, h, stride, ps);
case BGRA_PRE -> fromByteBgraPreData((ByteBuffer)frame.getImageData(), w, h, stride, ps);
case INT_ARGB_PRE -> fromIntArgbPreData((IntBuffer)frame.getImageData(), w, h, stride, ps);

case INT_ARGB_PRE ->
// Note: from here on, stride is measured in bytes.
fromIntArgbPreData((IntBuffer)frame.getImageData(), w, h, stride * 4, ps);

case GRAY_ALPHA -> {
byte[] buffer = new byte[w * h * 4];
Expand Down Expand Up @@ -289,20 +292,20 @@ public static Image convertImageFrame(ImageFrame frame) {

case INT_RGB -> {
IntBuffer imageData = (IntBuffer)frame.getImageData();
IntRgb.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
yield fromIntArgbPreData(imageData, w, h, stride, ps);
IntRgb.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
yield fromIntArgbPreData(imageData, w, h, stride * 4, ps); // Note: from here on, stride is measured in bytes.
}

case INT_BGR -> {
IntBuffer imageData = (IntBuffer)frame.getImageData();
IntBgr.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
yield fromIntArgbPreData(imageData, w, h, stride, ps);
IntBgr.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
yield fromIntArgbPreData(imageData, w, h, stride * 4, ps); // Note: from here on, stride is measured in bytes.
}

case INT_ARGB -> {
IntBuffer imageData = (IntBuffer)frame.getImageData();
IntArgb.ToIntArgbPreConverter().convert(imageData, 0, stride / 4, imageData, 0, stride / 4, w, h);
yield fromIntArgbPreData(imageData, w, h, stride, ps);
IntArgb.ToIntArgbPreConverter().convert(imageData, 0, stride, imageData, 0, stride, w, h);
yield fromIntArgbPreData(imageData, w, h, stride * 4, ps); // Note: from here on, stride is measured in bytes.
}

case PALETTE, PALETTE_ALPHA, PALETTE_ALPHA_PRE -> {
Expand All @@ -312,7 +315,7 @@ public static Image convertImageFrame(ImageFrame frame) {
default -> AlphaType.OPAQUE; // PALETTE
};

var converter = switch (frame.getPaletteIndexBits()) {
ByteToBytePixelConverter converter = switch (frame.getPaletteIndexBits()) {
case 1 -> {
var getter = OneBitIndexed.createGetter(frame.getPalette(), alphaType);
yield alphaType == AlphaType.OPAQUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ enum SupportedConversions {
255, 127, 0, 127, 50, 50, 50, 0
)),

INT_RGB(ImageType.INT_RGB, PixelFormat.INT_ARGB_PRE, 8,
INT_RGB(ImageType.INT_RGB, PixelFormat.INT_ARGB_PRE, 2,
new int[] {
rgb(50, 100, 150), rgb(10, 20, 30),
rgb(40, 50, 60), rgb(255, 255, 255)
Expand All @@ -164,7 +164,7 @@ enum SupportedConversions {
argb(255, 40, 50, 60), argb(255, 255, 255, 255)
}),

INT_BGR(ImageType.INT_BGR, PixelFormat.INT_ARGB_PRE, 8,
INT_BGR(ImageType.INT_BGR, PixelFormat.INT_ARGB_PRE, 2,
new int[] {
rgb(50, 100, 150), rgb(10, 20, 30),
rgb(40, 50, 60), rgb(255, 255, 255)
Expand All @@ -174,7 +174,7 @@ enum SupportedConversions {
argb(255, 60, 50, 40), argb(255, 255, 255, 255)
}),

INT_ARGB(ImageType.INT_ARGB, PixelFormat.INT_ARGB_PRE, 8,
INT_ARGB(ImageType.INT_ARGB, PixelFormat.INT_ARGB_PRE, 2,
new int[] {
argb(127, 50, 100, 150), argb(127, 10, 20, 30),
argb(255, 40, 50, 60), argb(0, 255, 255, 255)
Expand All @@ -184,7 +184,7 @@ enum SupportedConversions {
argb(255, 40, 50, 60), argb(0, 0, 0, 0)
}),

INT_ARGB_PRE(ImageType.INT_ARGB_PRE, PixelFormat.INT_ARGB_PRE, 8,
INT_ARGB_PRE(ImageType.INT_ARGB_PRE, PixelFormat.INT_ARGB_PRE, 2,
new int[] {
argb(127, 50, 100, 150), argb(127, 10, 20, 30),
argb(255, 0, 0, 0), argb(0, 255, 255, 255)
Expand Down

0 comments on commit 40e960d

Please sign in to comment.