/*
* Degrade the image by reducing the image quality until it either
* fits in the required memory or it has reached the minimum
* quality allowed.
*/
ImageConvertor cvt =
ImageConvertorFactory.getInstance().getImageConvertor(
ImageRule.TRUECOLOUR);
/**
* This converts the color model to linear RGB. This causes the
* JPEG writer to NOT write out a ICC color profile as Linear RGB
* is recognized as a standard. This is not necessary on JDK 1.6
* and later. VBM.2008011813. Not writing the ICC color profile
* saves about 3.5Kb in file size allowing us to produce larger
* images for a given maxfilesize
*/
if (src.getColorModel().getNumColorComponents() > 1) {
SampleModel sm = src.getColorModel().
createCompatibleSampleModel(src.getWidth(), src.getHeight());
ColorSpace sc = ColorSpaceJAI.getInstance(ColorSpaceJAI.CS_sRGB);
ColorModel cm = new ComponentColorModel(sc, sm.getSampleSize(),
src.getColorModel().hasAlpha(),
src.getColorModel().isAlphaPremultiplied(),
src.getColorModel().getTransparency(),
src.getColorModel().getTransferType());
src = ColorConvertDescriptor.create(src, cm, null);
}
// ensure the minimum quality is never more then the default as
// otherwise we won't terminate
float minQuality =
(float) (params.
getInteger(ParameterNames.MINIMUM_JPEG_QUALITY));
if (minQuality >= DEFAULT_QUALITY * 100f) {
LOGGER.warn("invalid-parameter-value", new Object[] {
ParameterNames.MINIMUM_JPEG_QUALITY,
Math.round(DEFAULT_QUALITY * 100f)});
params.setObject(ParameterNames.MINIMUM_JPEG_QUALITY,
Math.round(DEFAULT_QUALITY * 100f));
}
RenderedOp resizedSrc = src;
while (!degradeImage(os, resizedSrc, params, quality)) {
/*
* Degradation did not get the image small enough so scale it
* and try again.
*/
quality = params.getInteger(ParameterNames.MINIMUM_JPEG_QUALITY) / 100F;
Dimension d = new Dimension(src.getWidth(), src.getHeight());
int maxImageSize = params.getInteger(ParameterNames.MAX_IMAGE_SIZE);
Dimension newD = cvt.calcScale(d, lastLength, maxImageSize);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scaling image to width " + newD.width);
}
RenderedOp[] img = new RenderedOp[1];
img[0] = src;