{
createExtraHalfbritePalette();
}
int numBytesPerPlane = (width + 7) / 8;
PixelImage image = null;
Paletted8Image palettedImage = null;
RGB24Image rgbImage = null;
if (numPlanes == 24 || ham)
{
rgbImage = new MemoryRGB24Image(getBoundsWidth(), getBoundsHeight());
image = rgbImage;
}
else
{
palettedImage = new MemoryPaletted8Image(getBoundsWidth(), getBoundsHeight(), palette);
image = palettedImage;
}
/* only matters for uncompressed files;
will be true if the number of bytes is odd;
is computed differently for PBM and ILBM types
*/
boolean oddBytesPerRow = (((numBytesPerPlane * numPlanes) % 2) != 0);
if (type == MAGIC_PBM)
{
oddBytesPerRow = ((width % 2) == 1);
}
// plane data will have numPlanes planes for ILBM and 1 plane for PBM
byte[][] planes = null;
int numChannels = 1;
if (type == MAGIC_ILBM)
{
int allocBytes = numBytesPerPlane;
if ((numBytesPerPlane % 2) == 1)
{
allocBytes++;
}
// allocate numPlanes byte arrays
planes = new byte[numPlanes][];
if (rgb24 || ham)
{
numChannels = 3;
}
// for each of these byte arrays allocate numBytesPerPlane bytes
for (int i = 0; i < numPlanes; i++)
{
planes[i] = new byte[allocBytes];
}
}
else
{
// only one plane, but each plane has width bytes instead of
// numBytesPerPlane
planes = new byte[1][];
planes[0] = new byte[width];
}
byte[][] dest = new byte[numChannels][];
for (int i = 0; i < numChannels; i++)
{
dest[i] = new byte[width];
}
for (int y = 0, destY = 0 - getBoundsY1(); y <= getBoundsY2(); y++, destY++)
{
// load one row, different approach for PBM and ILBM
if (type == MAGIC_ILBM)
{
// decode all planes for a complete row
for (int p = 0; p < numPlanes; p++)
{
loadBytes(in, planes[p], numBytesPerPlane, y);
}
}
else
if (type == MAGIC_PBM)
{
loadBytes(in, planes[0], numBytesPerPlane, y);
}
/* all uncompressed rows must have an even number of bytes
so in case the number of bytes per row is odd, one byte
is read and dropped */
if (compression == COMPRESSION_NONE && oddBytesPerRow)
{
in.readByte();
}
setProgress(y, getBoundsY2() + 1);
// if we do not need the row we just loaded we continue loading
// the next row
if (!isRowRequired(y))
{
continue;
}
//System.out.println("storing row " + y + " as " + destY + ", numPlanes="+ numPlanes + ",type=" + type);
// compute offset into pixel data array
if (type == MAGIC_ILBM)
{
convertRow(planes, dest);
if (rgb24 || ham)
{
rgbImage.putByteSamples(RGB24Image.INDEX_RED, 0, destY,
getBoundsWidth(), 1, dest[0], getBoundsX1());
rgbImage.putByteSamples(RGB24Image.INDEX_GREEN, 0, destY,
getBoundsWidth(), 1, dest[1], getBoundsX1());
rgbImage.putByteSamples(RGB24Image.INDEX_BLUE, 0, destY,
getBoundsWidth(), 1, dest[2], getBoundsX1());
}
else
{
palettedImage.putByteSamples(0, 0, destY,
getBoundsWidth(), 1, dest[0], getBoundsX1());
}
}
else
if (type == MAGIC_PBM)
{
palettedImage.putByteSamples(0, 0, destY, getBoundsWidth(), 1,
planes[0], getBoundsX1());
}
}
return image;
}