// check the number is >1
if (numfiles <= 0) {
throw new ServiceException("The number of frames for this GIF is less than 1");
}
final GIFImageWriter gifWriter = new GIFImageWriter(ORIGINATING_PROVIDER);
// write param
final ImageWriteParam param = gifWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("LZW");
param.setCompressionQuality(0.75f);
ImageOutputStream otStream = null;
List<RenderedImage> images = new ArrayList<RenderedImage>();
try {
otStream = ImageIO.createImageOutputStream(outStream);
gifWriter.setOutput(otStream);
gifWriter.prepareWriteSequence(null);
// gif params
final GetMapRequest request = (GetMapRequest) mapContent.getRequest();
final Boolean loopContinuosly = (request.getFormatOptions().get("gif_loop_continuosly") != null ?
Boolean.valueOf((String)request.getFormatOptions().get("gif_loop_continuosly")) : wms.getLoopContinuously());
final Integer delay = (request.getFormatOptions().get("gif_frames_delay") != null ?
Integer.valueOf((String) request.getFormatOptions().get("gif_frames_delay")) : wms.getFramesDelay());
// check value
if (delay <= 0)
throw new ServiceException("Animate GIF delay invalid: " + delay);
//
// Getting input files
//
for (int i = 0; i < numfiles; i++) {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine("Writing image " + i);
// get the image
RenderedImage ri = (RenderedImage) ril.get(i);
// convert it to gif compatible
ri = applyPalette(ri, mapContent, MIME_TYPE, false);
if (ri != null) {
// prepare metadata and write param
final IIOMetadata imageMetadata = gifWriter.getDefaultImageMetadata(
new ImageTypeSpecifier(ri), param);
prepareMetadata(ri, imageMetadata, loopContinuosly, delay);
// write
gifWriter.writeToSequence(new IIOImage(ri, null, imageMetadata), param);
images.add(ri);
}
}
// close writing sequence
gifWriter.endWriteSequence();
} catch (IOException e) {
throw new ServiceException(e);
} finally {
try {
otStream.flush();
} catch (Exception e) {
// swallow
}
try {
otStream.close();
} catch (Exception e) {
// swallow
}
try {
gifWriter.dispose();
} catch (Exception e) {
// swallow
}
// let go of the image chain as soon as possible to free memory