param = getDefaultWriteParam();
RenderedImage input = null;
Raster inputRaster = null;
boolean writeRaster = image.hasRaster();
Rectangle sourceRegion = param.getSourceRegion();
SampleModel sampleModel = null;
if (writeRaster) {
inputRaster = image.getRaster();
sampleModel = inputRaster.getSampleModel();
} else {
input = image.getRenderedImage();
sampleModel = input.getSampleModel();
inputRaster = input.getData();
}
checkSampleModel(sampleModel);
if (sourceRegion == null)
sourceRegion = inputRaster.getBounds();
else
sourceRegion = sourceRegion.intersection(inputRaster.getBounds());
if (sourceRegion.isEmpty())
throw new RuntimeException(I18N.getString("WBMPImageWriter1"));
int scaleX = param.getSourceXSubsampling();
int scaleY = param.getSourceYSubsampling();
int xOffset = param.getSubsamplingXOffset();
int yOffset = param.getSubsamplingYOffset();
sourceRegion.translate(xOffset, yOffset);
sourceRegion.width -= xOffset;
sourceRegion.height -= yOffset;
int minX = sourceRegion.x / scaleX;
int minY = sourceRegion.y / scaleY;
int w = (sourceRegion.width + scaleX - 1) / scaleX;
int h = (sourceRegion.height + scaleY - 1) / scaleY;
Rectangle destinationRegion = new Rectangle(minX, minY, w, h);
sampleModel = sampleModel.createCompatibleSampleModel(w, h);
SampleModel destSM= sampleModel;
// If the data are not formatted nominally then reformat.
if(sampleModel.getDataType() != DataBuffer.TYPE_BYTE ||
!(sampleModel instanceof MultiPixelPackedSampleModel) ||
((MultiPixelPackedSampleModel)sampleModel).getDataBitOffset() != 0) {
destSM =
new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
w, h, 1,
w + 7 >> 3, 0);
}
if (!destinationRegion.equals(sourceRegion)) {
if (scaleX == 1 && scaleY == 1)
inputRaster = inputRaster.createChild(inputRaster.getMinX(),
inputRaster.getMinY(),
w, h, minX, minY, null);
else {