Index: gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessor.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessor.java (revision 35415) +++ gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessor.java (revision ) @@ -38,7 +38,7 @@ */ public class ScalingGalleryProcessor extends AbstractGalleryProcessor { @SuppressWarnings("unused") - private final static String SVN_ID = "$Id: ScalingGalleryProcessor.java 33439 2012-07-24 08:34:42Z mdenburger $"; + private final static String SVN_ID = "$Id: ScalingGalleryProcessor.java 33439 2012-03-13 16:26:55Z mdenburger $"; private static final Logger log = LoggerFactory.getLogger(ScalingGalleryProcessor.class); private static final long serialVersionUID = 1L; @@ -97,7 +97,7 @@ if (p != null) { try { final ScaleImageOperation scaleOperation = new ScaleImageOperation(p.getWidth(), p.getHeight(), - p.getUpscaling(), p.getStrategy()); + p.getUpscaling(), p.getStrategy(), p.getCompressionQuality()); scaleOperation.execute(data, mimeType); Index: gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingParameters.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingParameters.java (revision 35415) +++ gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingParameters.java (revision ) @@ -25,7 +25,7 @@ */ public class ScalingParameters implements Serializable { @SuppressWarnings("unused") - private final static String SVN_ID = "$Id: ScalingParameters.java 33439 2012-07-24 08:34:42Z mdenburger $"; + private final static String SVN_ID = "$Id: ScalingParameters.java 33439 2012-03-13 16:26:55Z mdenburger $"; private static final long serialVersionUID = 1L; @@ -33,6 +33,7 @@ private final int height; private final boolean upscaling; private final ImageUtils.ScalingStrategy strategy; + private final float compressionQuality; /** * Creates a set of scaling parameters: the width and height of the bounding box, and whether to @@ -43,7 +44,7 @@ * @param upscaling whether to do upscaling of images that are smaller than the bounding box */ public ScalingParameters(int width, int height, boolean upscaling) { - this(width, height, upscaling, ImageUtils.ScalingStrategy.QUALITY); + this(width, height, upscaling, ImageUtils.ScalingStrategy.QUALITY, 1f); } /** @@ -60,9 +61,28 @@ this.height = height; this.upscaling = upscaling; this.strategy = strategy; + this.compressionQuality = 1f; } /** + * Creates a set of scaling parameters: the width and height of the bounding box, and whether to do upscaling. A + * width or height of 0 or less means 'unspecified'. + * + * @param width the width of the bounding box + * @param height the height of the bounding box + * @param upscaling whether to do upscaling of images that are smaller than the bounding box + * @param strategy the scaling strategy to use + * @param compressionQuality compression quality + */ + public ScalingParameters(int width, int height, boolean upscaling, ImageUtils.ScalingStrategy strategy, float compressionQuality) { + this.width = width; + this.height = height; + this.upscaling = upscaling; + this.strategy = strategy; + this.compressionQuality = compressionQuality; + } + + /** * @return the width of the bounding box */ public int getWidth() { @@ -102,6 +122,10 @@ final ScalingParameters other = (ScalingParameters) o; return width == other.width && height == other.height && upscaling == other.upscaling && strategy == other.strategy; + } + + public float getCompressionQuality() { + return compressionQuality; } @Override Index: gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessorPlugin.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessorPlugin.java (revision 35415) +++ gallery/frontend/src/main/java/org/hippoecm/frontend/plugins/gallery/processor/ScalingGalleryProcessorPlugin.java (revision ) @@ -66,7 +66,7 @@ */ public class ScalingGalleryProcessorPlugin extends Plugin { @SuppressWarnings("unused") - private final static String SVN_ID = "$Id: ScalingGalleryProcessorPlugin.java 33439 2012-07-24 08:34:42Z mdenburger $"; + private final static String SVN_ID = "$Id: ScalingGalleryProcessorPlugin.java 33439 2012-03-13 16:26:55Z mdenburger $"; private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(ScalingGalleryProcessorPlugin.class); @@ -75,12 +75,14 @@ protected static final String CONFIG_PARAM_HEIGHT = "height"; protected static final String CONFIG_PARAM_UPSCALING = "upscaling"; protected static final String CONFIG_PARAM_OPTIMIZE = "optimize"; + protected static final String CONFIG_PARAM_COMPRESSION = "compression"; - + protected static final int DEFAULT_WIDTH = 0; protected static final int DEFAULT_HEIGHT = 0; protected static final boolean DEFAULT_UPSCALING = false; protected static final String DEFAULT_OPTIMIZE = "quality"; + protected static final double DEFAULT_COMPRESSION = 1.0; - + private static final Map SCALING_STRATEGY_MAP = new LinkedHashMap(); static { SCALING_STRATEGY_MAP.put("auto", ImageUtils.ScalingStrategy.AUTO); @@ -109,7 +111,8 @@ final int width = scaleConfig.getAsInteger(CONFIG_PARAM_WIDTH, DEFAULT_WIDTH); final int height = scaleConfig.getAsInteger(CONFIG_PARAM_HEIGHT, DEFAULT_HEIGHT); final boolean upscaling = scaleConfig.getAsBoolean(CONFIG_PARAM_UPSCALING, DEFAULT_UPSCALING); + final float compressionQuality = (float) scaleConfig.getAsDouble(CONFIG_PARAM_COMPRESSION, DEFAULT_COMPRESSION); - + final String strategyName = scaleConfig.getString(CONFIG_PARAM_OPTIMIZE, DEFAULT_OPTIMIZE); ImageUtils.ScalingStrategy strategy = SCALING_STRATEGY_MAP.get(strategyName); if (strategy == null) { @@ -118,7 +121,7 @@ strategy = SCALING_STRATEGY_MAP.get(DEFAULT_OPTIMIZE); } - final ScalingParameters parameters = new ScalingParameters(width, height, upscaling, strategy); + final ScalingParameters parameters = new ScalingParameters(width, height, upscaling, strategy, compressionQuality); log.debug("Scaling parameters for {}: {}", nodeName, parameters); processor.addScalingParameters(nodeName, parameters); } Index: api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ImageUtils.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ImageUtils.java (revision 35415) +++ api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ImageUtils.java (revision ) @@ -49,7 +49,7 @@ */ public class ImageUtils { @SuppressWarnings("unused") - private final static String SVN_ID = "$Id: ImageUtils.java 35411 2012-07-24 08:35:00Z mdenburger $"; + private final static String SVN_ID = "$Id: ImageUtils.java 35411 2012-07-23 11:56:41Z mdenburger $"; /** * The available strategies for scaling images. @@ -103,7 +103,6 @@ * Fixes issues for certain image MIME types. * * @param mimeType the MIME type to fix - * * @return the fixed MIME type */ public static String fixMimeType(String mimeType) { @@ -162,7 +161,7 @@ * * @throws IOException when creating the binary output stream failed. */ - public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image) throws IOException { + public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image, float compressionQuality) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (image != null) { @@ -178,7 +177,7 @@ if (compressionTypes != null && compressionTypes.length > 0) { writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType(compressionTypes[0]); - writeParam.setCompressionQuality(1f); + writeParam.setCompressionQuality(compressionQuality); } } @@ -193,6 +192,19 @@ } return out; + } + + /** + * Returns the data of a {@link BufferedImage} as a binary output stream. If the image is null, a + * stream of zero bytes is returned. + * + * @param writer the writer to use for writing the image data. + * @param image the image to write. + * @return an output stream with the data of the given image. + * @throws IOException when creating the binary output stream failed. + */ + public static ByteArrayOutputStream writeImage(ImageWriter writer, BufferedImage image) throws IOException { + return writeImage(writer, image, 1f); } /** Index: api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java (revision 35415) +++ api/src/main/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperation.java (revision ) @@ -52,7 +52,7 @@ */ public class ScaleImageOperation extends AbstractImageOperation { @SuppressWarnings("unused") - private final static String SVN_ID = "$Id: ScaleImageOperation.java 33440 2012-07-24 08:35:00Z mdenburger $"; + private final static String SVN_ID = "$Id: ScaleImageOperation.java 33440 2012-03-13 16:29:33Z mdenburger $"; private static final Logger log = LoggerFactory.getLogger(ScaleImageOperation.class); private static final Object scalingLock = new Object(); @@ -64,6 +64,7 @@ private InputStream scaledData; private int scaledWidth; private int scaledHeight; + private float compressionQuality = 1f; /** * Creates a image scaling operation, defined by the bounding box of a certain width and height. @@ -95,6 +96,23 @@ } /** + * Creates a image scaling operation, defined by the bounding box of a certain width and height. + * + * @param width the width of the bounding box in pixels + * @param height the height of the bounding box in pixels + * @param upscaling whether to enlarge images that are smaller than the bounding box + * @param strategy the strategy to use for scaling the image (e.g. optimize for speed, quality, a trade-off between + * these two, etc.) + */ + public ScaleImageOperation(int width, int height, boolean upscaling, ImageUtils.ScalingStrategy strategy, float compressionQuality) { + this.width = width; + this.height = height; + this.upscaling = upscaling; + this.strategy = strategy; + this.compressionQuality = compressionQuality; + } + + /** * Creates a scaled version of an image. The given scaling parameters define a bounding box with * a certain width and height. Images that do not fit in this box (i.e. are too large) are always scaled * down such that they do fit. If the aspect ratio of the original image differs from that of the @@ -165,7 +183,7 @@ scaledWidth = scaledImage.getWidth(); scaledHeight = scaledImage.getHeight(); - ByteArrayOutputStream scaledOutputStream = ImageUtils.writeImage(writer, scaledImage); + ByteArrayOutputStream scaledOutputStream = ImageUtils.writeImage(writer, scaledImage, compressionQuality); scaledData = new ByteArrayInputStream(scaledOutputStream.toByteArray()); } } finally { @@ -231,4 +249,7 @@ return scaledHeight; } + public float getCompressionQuality() { + return compressionQuality; + } } Index: api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java (revision 35415) +++ api/src/test/java/org/hippoecm/frontend/plugins/gallery/imageutil/ScaleImageOperationTest.java (revision ) @@ -128,6 +128,14 @@ checkImageDimensions(scaleOp, "image/gif", 88, 100); } + @Test + public void scaleGifWithCompression() throws GalleryException, IOException { + InputStream data = getClass().getResourceAsStream("/test-380x428.gif"); + ScaleImageOperation scaleOp = new ScaleImageOperation(200, 100, true, ImageUtils.ScalingStrategy.SPEED, 0.8f); + scaleOp.execute(data, "image/gif"); + checkImageDimensions(scaleOp, "image/gif", 88, 100); + } + private void checkImageDimensions(ScaleImageOperation scaleOp, String mimeType, int expectedWidth, int expectedHeight) throws IOException { assertEquals(expectedWidth, scaleOp.getScaledWidth()); assertEquals(expectedHeight, scaleOp.getScaledHeight());