image = PaletteBuilder.createIndexedImage(image);
iioimage.setRenderedImage(image);
}
ColorModel colorModel = image.getColorModel();
SampleModel sampleModel = image.getSampleModel();
// Determine source region and destination dimensions.
Rectangle sourceBounds = new Rectangle(image.getMinX(),
image.getMinY(),
image.getWidth(),
image.getHeight());
Dimension destSize = new Dimension();
computeRegions(sourceBounds, destSize, p);
// Convert any provided image metadata.
GIFWritableImageMetadata imageMetadata = null;
if (iioimage.getMetadata() != null) {
imageMetadata = new GIFWritableImageMetadata();
convertMetadata(IMAGE_METADATA_NAME, iioimage.getMetadata(),
imageMetadata);
// Converted rgb image can use palette different from global.
// In order to avoid color artefacts we want to be sure we use
// appropriate palette. For this we initialize local color table
// from current color and sample models.
// At this point we can guarantee that local color table can be
// build because image was already converted to indexed or
// gray-scale representations
if (imageMetadata.localColorTable == null) {
imageMetadata.localColorTable =
createColorTable(colorModel, sampleModel);
// in case of indexed image we should take care of
// transparent pixels
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm =
(IndexColorModel)colorModel;
int index = icm.getTransparentPixel();
imageMetadata.transparentColorFlag = (index != -1);
if (imageMetadata.transparentColorFlag) {
imageMetadata.transparentColorIndex = index;
}
/* NB: transparentColorFlag might have not beed reset for
greyscale images but explicitly reseting it here
is potentially not right thing to do until we have way
to find whether current value was explicitly set by
the user.
*/
}
}
}
// Global color table values.
byte[] globalColorTable = null;
// Write the header (Signature+Logical Screen Descriptor+
// Global Color Table).
if (writeHeader) {
if (sm == null) {
throw new IllegalArgumentException("Cannot write null header!");
}
GIFWritableStreamMetadata streamMetadata =
(GIFWritableStreamMetadata)sm;
// Set the version if not set.
if (streamMetadata.version == null) {
streamMetadata.version = "89a";
}
// Set the Logical Screen Desriptor if not set.
if (streamMetadata.logicalScreenWidth ==
GIFMetadata.UNDEFINED_INTEGER_VALUE)
{
streamMetadata.logicalScreenWidth = destSize.width;
}
if (streamMetadata.logicalScreenHeight ==
GIFMetadata.UNDEFINED_INTEGER_VALUE)
{
streamMetadata.logicalScreenHeight = destSize.height;
}
if (streamMetadata.colorResolution ==
GIFMetadata.UNDEFINED_INTEGER_VALUE)
{
streamMetadata.colorResolution = colorModel != null ?
colorModel.getComponentSize()[0] :
sampleModel.getSampleSize()[0];
}
// Set the Global Color Table if not set, i.e., if not
// provided in the stream metadata.
if (streamMetadata.globalColorTable == null) {
if (isWritingSequence && imageMetadata != null &&
imageMetadata.localColorTable != null) {
// Writing a sequence and a local color table was
// provided in the metadata of the first image: use it.
streamMetadata.globalColorTable =
imageMetadata.localColorTable;
} else if (imageMetadata == null ||
imageMetadata.localColorTable == null) {
// Create a color table.
streamMetadata.globalColorTable =
createColorTable(colorModel, sampleModel);
}
}
// Set the Global Color Table. At this point it should be
// A) the global color table provided in stream metadata, if any;
// B) the local color table of the image metadata, if any, if
// writing a sequence;
// C) a table created on the basis of the first image ColorModel
// and SampleModel if no local color table is available; or
// D) null if none of the foregoing conditions obtain (which
// should only be if a sequence is not being written and
// a local color table is provided in image metadata).
globalColorTable = streamMetadata.globalColorTable;
// Write the header.
int bitsPerPixel;
if (globalColorTable != null) {
bitsPerPixel = getNumBits(globalColorTable.length/3);
} else if (imageMetadata != null &&
imageMetadata.localColorTable != null) {
bitsPerPixel =
getNumBits(imageMetadata.localColorTable.length/3);
} else {
bitsPerPixel = sampleModel.getSampleSize(0);
}
writeHeader(streamMetadata, bitsPerPixel);
} else if (isWritingSequence) {
globalColorTable = theStreamMetadata.globalColorTable;
} else {