setBoundsIfNecessary(image.getWidth(), image.getHeight());
int width = getBoundsWidth();
int height = getBoundsHeight();
ByteChannelImage bcimg = null;
BilevelImage bilevelImage = null;
RGB24Image rgbimg = null;
int bytesPerRow = 0;
int offset = 54;
int numBits = 0;
int numPackedBytes = 0;
if (image instanceof Paletted8Image ||
image instanceof Gray8Image)
{
bcimg = (ByteChannelImage)image;
bytesPerRow = width;
offset += 1024;
numBits = 8;
}
else
if (image instanceof BilevelImage)
{
bilevelImage = (BilevelImage)image;
numPackedBytes = (width + 7) / 8;
bytesPerRow = numPackedBytes;
offset += 8;
numBits = 1;
}
else
if (image instanceof RGB24Image)
{
rgbimg = (RGB24Image)image;
bytesPerRow = width * 3;
numBits = 24;
}
if ((bytesPerRow % 4) != 0)
{
bytesPerRow = ((bytesPerRow + 3) / 4) * 4;
}
int filesize = offset + bytesPerRow * height;
writeHeader(image, filesize, offset, numBits);
writePalette();
byte[] row = new byte[bytesPerRow];
final int X1 = getBoundsX1();
for (int y = getBoundsY2(), processed = 0; processed < height; y--, processed++)
{
if (bilevelImage != null)
{
bilevelImage.getPackedBytes(X1, y, width, row, 0, 0);
}
else
if (bcimg != null)
{
bcimg.getByteSamples(0, 0, y, width, 1, row, 0);
}
else
if (rgbimg != null)
{
int offs = 0;
for (int x = X1; x < X1 + width; x++)
{
row[offs++] = rgbimg.getByteSample(RGBIndex.INDEX_BLUE, x, y);
row[offs++] = rgbimg.getByteSample(RGBIndex.INDEX_GREEN, x, y);
row[offs++] = rgbimg.getByteSample(RGBIndex.INDEX_RED, x, y);
}
}
else
{
// error