mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-05-01 00:00:02 -04:00
#483 Initial PSD Write support
This commit is contained in:
-90
@@ -1,90 +0,0 @@
|
||||
package com.twelvemonkeys.imageio.plugins.webp;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
|
||||
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||
|
||||
/**
|
||||
* RasterUtils
|
||||
*/
|
||||
public final class RasterUtils {
|
||||
// TODO: Generalize and move to common util package
|
||||
|
||||
private RasterUtils() {}
|
||||
|
||||
public static WritableRaster asByteRaster(final WritableRaster raster, final ColorModel colorModel) {
|
||||
switch (raster.getTransferType()) {
|
||||
case DataBuffer.TYPE_BYTE:
|
||||
return raster;
|
||||
case DataBuffer.TYPE_INT:
|
||||
final int bands = colorModel.getNumComponents();
|
||||
final DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer();
|
||||
|
||||
int w = raster.getWidth();
|
||||
int h = raster.getHeight();
|
||||
int size = buffer.getSize();
|
||||
|
||||
return new WritableRaster(
|
||||
new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, w, h, bands, w * bands, createBandOffsets(colorModel)),
|
||||
new DataBuffer(DataBuffer.TYPE_BYTE, size * bands) {
|
||||
// TODO: These masks should probably not be hardcoded
|
||||
final int[] MASKS = {
|
||||
0xffffff00,
|
||||
0xffff00ff,
|
||||
0xff00ffff,
|
||||
0x00ffffff,
|
||||
};
|
||||
|
||||
@Override
|
||||
public int getElem(int bank, int i) {
|
||||
int index = i / bands;
|
||||
int shift = (i % bands) * 8;
|
||||
|
||||
return (buffer.getElem(index) >>> shift) & 0xff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setElem(int bank, int i, int val) {
|
||||
int index = i / bands;
|
||||
int element = i % bands;
|
||||
int shift = element * 8;
|
||||
|
||||
int value = (buffer.getElem(index) & MASKS[element]) | ((val & 0xff) << shift);
|
||||
buffer.setElem(index, value);
|
||||
}
|
||||
}, new Point()) {};
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("Raster type %d not supported", raster.getTransferType()));
|
||||
}
|
||||
}
|
||||
|
||||
private static int[] createBandOffsets(final ColorModel colorModel) {
|
||||
notNull(colorModel, "colorModel");
|
||||
|
||||
if (colorModel instanceof DirectColorModel) {
|
||||
DirectColorModel dcm = (DirectColorModel) colorModel;
|
||||
int[] masks = dcm.getMasks();
|
||||
int[] offs = new int[masks.length];
|
||||
|
||||
for (int i = 0; i < masks.length; i++) {
|
||||
int mask = masks[i];
|
||||
int off = 0;
|
||||
|
||||
// TODO: FixMe! This only works for standard 8 bit masks (0xFF)
|
||||
if (mask != 0) {
|
||||
while ((mask & 0xFF) == 0) {
|
||||
mask >>>= 8;
|
||||
off++;
|
||||
}
|
||||
}
|
||||
|
||||
offs[i] = off;
|
||||
}
|
||||
|
||||
return offs;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(String.format("%s not supported", colorModel.getClass().getSimpleName()));
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -42,6 +42,7 @@ import com.twelvemonkeys.imageio.stream.SubImageInputStream;
|
||||
import com.twelvemonkeys.imageio.util.IIOUtil;
|
||||
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;
|
||||
import com.twelvemonkeys.imageio.util.ProgressListenerBase;
|
||||
import com.twelvemonkeys.imageio.util.RasterUtils;
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.ImageReadParam;
|
||||
@@ -299,6 +300,7 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
List<ImageTypeSpecifier> types = new ArrayList<>();
|
||||
types.add(rawImageType);
|
||||
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB));
|
||||
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_BGR));
|
||||
|
||||
return types.iterator();
|
||||
}
|
||||
@@ -314,13 +316,13 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
switch (header.fourCC) {
|
||||
case WebP.CHUNK_VP8_:
|
||||
imageInput.seek(header.offset);
|
||||
readVP8(RasterUtils.asByteRaster(destination.getRaster(), destination.getColorModel()), param);
|
||||
readVP8(RasterUtils.asByteRaster(destination.getRaster()), param);
|
||||
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_VP8L:
|
||||
imageInput.seek(header.offset);
|
||||
readVP8Lossless(RasterUtils.asByteRaster(destination.getRaster(), destination.getColorModel()), param);
|
||||
readVP8Lossless(RasterUtils.asByteRaster(destination.getRaster()), param);
|
||||
|
||||
break;
|
||||
|
||||
@@ -373,13 +375,13 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_VP8_:
|
||||
readVP8(RasterUtils.asByteRaster(destination.getRaster(), destination.getColorModel())
|
||||
readVP8(RasterUtils.asByteRaster(destination.getRaster())
|
||||
.createWritableChild(0, 0, width, height, 0, 0, new int[]{0, 1, 2}), param);
|
||||
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_VP8L:
|
||||
readVP8Lossless(RasterUtils.asByteRaster(destination.getRaster(), destination.getColorModel()), param);
|
||||
readVP8Lossless(RasterUtils.asByteRaster(destination.getRaster()), param);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
+2
-3
@@ -40,7 +40,7 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.twelvemonkeys.imageio.plugins.webp.RasterUtils.asByteRaster;
|
||||
import static com.twelvemonkeys.imageio.util.RasterUtils.asByteRaster;
|
||||
import static java.lang.Math.*;
|
||||
|
||||
/**
|
||||
@@ -180,8 +180,7 @@ public final class VP8LDecoder {
|
||||
new DataBufferInt(colorTable, colorTableSize),
|
||||
colorTableSize, 1, colorTableSize,
|
||||
new int[] {0}, null
|
||||
),
|
||||
ColorModel.getRGBdefault()
|
||||
)
|
||||
), false);
|
||||
|
||||
// TODO: We may not really need this value...
|
||||
|
||||
Reference in New Issue
Block a user