More refactorings.

This commit is contained in:
Harald Kuhr 2009-09-25 20:43:34 +02:00
parent 0bc38328de
commit eea6fd38fc

View File

@ -259,17 +259,16 @@ public class PSDImageReader extends ImageReaderBase {
// TODO: Bitmap (depth = 1) and 16 bit (depth = 16) must be read differently, obviously... // TODO: Bitmap (depth = 1) and 16 bit (depth = 16) must be read differently, obviously...
// This code works fine for images with channel depth = 8 // This code works fine for images with channel depth = 8
switch (compression) { switch (compression) {
// TODO: This entire reading block is duplicated and should be replaced with the one for RLE! case PSD.COMPRESSION_NONE:
read8bitData(raster, image.getColorModel(), source, dest, xSub, ySub, offsets, false);
break;
case PSD.COMPRESSION_RLE: case PSD.COMPRESSION_RLE:
// NOTE: Offsets will allow us to easily skip rows before AOI // NOTE: Offsets will allow us to easily skip rows before AOI
offsets = new int[mHeader.mChannels * mHeader.mHeight]; offsets = new int[mHeader.mChannels * mHeader.mHeight];
for (int i = 0; i < offsets.length; i++) { for (int i = 0; i < offsets.length; i++) {
offsets[i] = mImageInput.readUnsignedShort(); offsets[i] = mImageInput.readUnsignedShort();
} }
// Fall through read8bitData(raster, image.getColorModel(), source, dest, xSub, ySub, offsets, true);
case PSD.COMPRESSION_NONE:
read8bitData(raster, image.getColorModel(), source, dest, xSub, ySub, offsets, compression == PSD.COMPRESSION_RLE);
break; break;
case PSD.COMPRESSION_ZIP: case PSD.COMPRESSION_ZIP:
// TODO: Could probably use the ZIPDecoder (DeflateDecoder) here.. // TODO: Could probably use the ZIPDecoder (DeflateDecoder) here..
@ -281,9 +280,6 @@ public class PSDImageReader extends ImageReaderBase {
throw new IIOException("Unknown compression type: " + compression); throw new IIOException("Unknown compression type: " + compression);
} }
// Compose out the background of the semi-transparent pixels, as PS somehow has the background composed in
decomposeAlpha(image);
if (abortRequested()) { if (abortRequested()) {
processReadAborted(); processReadAborted();
} }
@ -324,7 +320,6 @@ public class PSDImageReader extends ImageReaderBase {
for (x = 0; x < mHeader.mWidth; x++) { for (x = 0; x < mHeader.mWidth; x++) {
byte value = input.readByte(); byte value = input.readByte();
// CMYK values are stored inverted, but alpha is not // CMYK values are stored inverted, but alpha is not
if (isCMYK && c < colorComponents) { if (isCMYK && c < colorComponents) {
value = (byte) (255 - value & 0xff); value = (byte) (255 - value & 0xff);
@ -373,44 +368,39 @@ public class PSDImageReader extends ImageReaderBase {
System.err.println("x: " + x); System.err.println("x: " + x);
throw e; throw e;
} }
// Compose out the background of the semi-transparent pixels, as PS somehow has the background composed in
decomposeAlpha(sourceColorModel, data, pDest.width, pDest.height, channels);
} }
private void decomposeAlpha(final BufferedImage pImage) throws IOException { private void decomposeAlpha(final ColorModel pModel, final byte[] pData,
ColorModel cm = pImage.getColorModel(); final int pWidth, final int pHeight, final int pChannels) throws IOException
{
// TODO: What about CMYK + alpha? // TODO: What about CMYK + alpha?
if (cm.hasAlpha() && cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) { if (pModel.hasAlpha() && pModel.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
WritableRaster raster = pImage.getRaster();
// TODO: Probably faster to do this in line.. // TODO: Probably faster to do this in line..
// TODO: This is not so good, as it might break acceleration... for (int y = 0; y < pHeight; y++) {
byte[] data = ((DataBufferByte) raster.getDataBuffer()).getData(); for (int x = 0; x < pWidth; x++) {
int offset = (x + y * pWidth) * pChannels;
final int w = pImage.getWidth();
final int channels = raster.getNumBands();
for (int y = 0; y < pImage.getHeight(); y++) {
for (int x = 0; x < w; x++) {
int offset = (x + y * w) * channels;
// TODO: Is the document background always white!? // TODO: Is the document background always white!?
// ABGR format // ABGR format
int alpha = data[offset] & 0xff; int alpha = pData[offset] & 0xff;
if (alpha != 0) { if (alpha != 0) {
double normalizedAlpha = alpha / 255.0; double normalizedAlpha = alpha / 255.0;
for (int i = 1; i < channels; i++) {
data[offset + i] = decompose(data[offset + i] & 0xff, normalizedAlpha); for (int i = 1; i < pChannels; i++) {
pData[offset + i] = decompose(pData[offset + i] & 0xff, normalizedAlpha);
} }
} }
else { else {
for (int i = 1; i < channels; i++) { for (int i = 1; i < pChannels; i++) {
data[offset + i] = 0; pData[offset + i] = 0;
} }
} }
} }
} }
} }
// System.out.println("PSDImageReader.coerceData: " + cm.getClass());
// System.out.println("other.equals(cm): " + (other == cm));
} }
private static byte decompose(final int pColor, final double pAlpha) { private static byte decompose(final int pColor, final double pAlpha) {