Skip to content

Commit ea48ded

Browse files
committed
Unify drawIconImage to one place
1 parent 48bf244 commit ea48ded

File tree

2 files changed

+52
-64
lines changed

2 files changed

+52
-64
lines changed

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskGeneratePWAIcons.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
import javax.imageio.ImageIO;
1919

20-
import java.awt.Color;
21-
import java.awt.Graphics2D;
22-
import java.awt.Image;
2320
import java.awt.image.BufferedImage;
2421
import java.io.File;
2522
import java.io.IOException;
@@ -188,7 +185,7 @@ private Runnable generateIconTask(PwaIcon icon, BufferedImage baseImage) {
188185
int targetWidth = icon.getWidth();
189186
int targetHeight = icon.getHeight();
190187
return () -> {
191-
BufferedImage scaled = drawIconImage(baseImage, targetWidth,
188+
BufferedImage scaled = PwaIcon.drawIconImage(baseImage, targetWidth,
192189
targetHeight);
193190
try (OutputStream os = Files.newOutputStream(iconPath)) {
194191
ImageIO.write(scaled, "png", os);
@@ -199,33 +196,4 @@ private Runnable generateIconTask(PwaIcon icon, BufferedImage baseImage) {
199196
}
200197
};
201198
}
202-
203-
private static BufferedImage drawIconImage(BufferedImage baseImage,
204-
int targetWidth, int targetHeight) {
205-
int bgColor = baseImage.getRGB(0, 0);
206-
207-
BufferedImage bimage = new BufferedImage(targetWidth, targetHeight,
208-
BufferedImage.TYPE_INT_ARGB);
209-
Graphics2D graphics = bimage.createGraphics();
210-
try {
211-
graphics.setBackground(new Color(bgColor, true));
212-
graphics.clearRect(0, 0, targetWidth, targetHeight);
213-
214-
float ratio = Math.max((float) baseImage.getWidth() / targetWidth,
215-
(float) baseImage.getHeight() / targetHeight);
216-
ratio = Math.max(ratio, 1.0f);
217-
218-
int newWidth = Math.round(baseImage.getHeight() / ratio);
219-
int newHeight = Math.round(baseImage.getWidth() / ratio);
220-
221-
graphics.drawImage(
222-
baseImage.getScaledInstance(newWidth, newHeight,
223-
Image.SCALE_SMOOTH),
224-
(targetWidth - newWidth) / 2,
225-
(targetHeight - newHeight) / 2, null);
226-
} finally {
227-
graphics.dispose();
228-
}
229-
return bimage;
230-
}
231199
}

flow-server/src/main/java/com/vaadin/flow/server/PwaIcon.java

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)