@@ -276,7 +276,8 @@ public void write(OutputStream outputStream) {
276276 if (data == null ) {
277277 // New image with wanted size
278278 // Store byte array and hashcode of image (GeneratedImage)
279- setImage (drawIconImage (getBaseImage ()));
279+ setImage (drawIconImage (getBaseImage (), this .getWidth (),
280+ this .getHeight ()));
280281 }
281282 try {
282283 outputStream .write (data );
@@ -292,43 +293,62 @@ protected BufferedImage getBaseImage() {
292293 return registry .getBaseImage ();
293294 }
294295
295- private BufferedImage drawIconImage (BufferedImage baseImage ) {
296+ /**
297+ * Draws a resized version of the given base image centered on a new image
298+ * of the specified dimensions. The top-left pixel of the base image is used
299+ * as the background fill color. The image is scaled down to fit within the
300+ * target dimensions while preserving its aspect ratio; upscaling is not
301+ * performed.
302+ *
303+ * @param baseImage
304+ * the source image to resize
305+ * @param targetWidth
306+ * the width of the resulting image in pixels
307+ * @param targetHeight
308+ * the height of the resulting image in pixels
309+ * @return a new {@link BufferedImage} with the resized icon drawn centered
310+ */
311+ public static BufferedImage drawIconImage (BufferedImage baseImage ,
312+ int targetWidth , int targetHeight ) {
296313 // Pick top-left pixel as fill color if needed for image
297314 // resizing
298315 int bgColor = baseImage .getRGB (0 , 0 );
299316
300- BufferedImage bimage = new BufferedImage (this . getWidth () ,
301- this . getHeight (), BufferedImage .TYPE_INT_ARGB );
317+ BufferedImage bimage = new BufferedImage (targetWidth , targetHeight ,
318+ BufferedImage .TYPE_INT_ARGB );
302319 // Draw the image on to the buffered image
303320 Graphics2D graphics = bimage .createGraphics ();
304321
305- // fill bg with fill-color
306- graphics .setBackground (new Color (bgColor , true ));
307- graphics .clearRect (0 , 0 , this .getWidth (), this .getHeight ());
308-
309- // calculate ratio (bigger ratio) for resize
310- float ratio = (float ) baseImage .getWidth ()
311- / (float ) this .getWidth () > (float ) baseImage .getHeight ()
312- / (float ) this .getHeight ()
313- ? (float ) baseImage .getWidth ()
314- / (float ) this .getWidth ()
315- : (float ) baseImage .getHeight ()
316- / (float ) this .getHeight ();
317-
318- // Forbid upscaling of image
319- ratio = ratio > 1.0f ? ratio : 1.0f ;
320-
321- // calculate sizes with ratio
322- int newWidth = Math .round (baseImage .getHeight () / ratio );
323- int newHeight = Math .round (baseImage .getWidth () / ratio );
324-
325- // draw rescaled img in the center of created image
326- graphics .drawImage (
327- baseImage .getScaledInstance (newWidth , newHeight ,
328- Image .SCALE_SMOOTH ),
329- (this .getWidth () - newWidth ) / 2 ,
330- (this .getHeight () - newHeight ) / 2 , null );
331- graphics .dispose ();
322+ try {
323+ // fill bg with fill-color
324+ graphics .setBackground (new Color (bgColor , true ));
325+ graphics .clearRect (0 , 0 , targetWidth , targetHeight );
326+
327+ // calculate ratio (bigger ratio) for resize
328+ float ratio = (float ) baseImage .getWidth ()
329+ / (float ) targetWidth > (float ) baseImage .getHeight ()
330+ / (float ) targetHeight
331+ ? (float ) baseImage .getWidth ()
332+ / (float ) targetWidth
333+ : (float ) baseImage .getHeight ()
334+ / (float ) targetHeight ;
335+
336+ // Forbid upscaling of image
337+ ratio = ratio > 1.0f ? ratio : 1.0f ;
338+
339+ // calculate sizes with ratio
340+ int newWidth = Math .round (baseImage .getHeight () / ratio );
341+ int newHeight = Math .round (baseImage .getWidth () / ratio );
342+
343+ // draw rescaled img in the center of created image
344+ graphics .drawImage (
345+ baseImage .getScaledInstance (newWidth , newHeight ,
346+ Image .SCALE_SMOOTH ),
347+ (targetWidth - newWidth ) / 2 ,
348+ (targetHeight - newHeight ) / 2 , null );
349+ } finally {
350+ graphics .dispose ();
351+ }
332352 return bimage ;
333353 }
334354
0 commit comments