11
11
import javax .imageio .ImageIO ;
12
12
import javax .servlet .ServletContext ;
13
13
14
- import java .awt .Color ;
15
- import java .awt .Graphics2D ;
16
- import java .awt .Image ;
17
14
import java .awt .image .BufferedImage ;
18
15
import java .io .BufferedReader ;
19
16
import java .io .IOException ;
@@ -75,6 +72,7 @@ public class PwaRegistry implements Serializable {
75
72
private long offlineHash ;
76
73
private List <PwaIcon > icons = new ArrayList <>();
77
74
private final PwaConfiguration pwaConfiguration ;
75
+ private BufferedImage baseImage ;
78
76
79
77
/**
80
78
* Creates a new PwaRegistry instance.
@@ -109,6 +107,10 @@ public PwaRegistry(PWA pwa, ServletContext servletContext)
109
107
initializeResources (servletContext );
110
108
}
111
109
110
+ BufferedImage getBaseImage () {
111
+ return baseImage ;
112
+ }
113
+
112
114
private void initializeResources (ServletContext servletContext )
113
115
throws MalformedURLException , IOException {
114
116
if (!pwaConfiguration .isEnabled ()) {
@@ -126,17 +128,13 @@ private void initializeResources(ServletContext servletContext)
126
128
127
129
// Load base logo from servlet context if available
128
130
// fall back to local image if unavailable
129
- BufferedImage baseImage = getBaseImage (logo );
131
+ baseImage = getBaseImage (logo );
130
132
131
133
if (baseImage == null ) {
132
134
getLogger ().error ("Image is not found or can't be loaded: " + logo );
133
135
} else {
134
- // Pick top-left pixel as fill color if needed for image
135
- // resizing
136
- int bgColor = baseImage .getRGB (0 , 0 );
137
-
138
136
// initialize icons
139
- icons = initializeIcons (baseImage , bgColor );
137
+ icons = initializeIcons ();
140
138
}
141
139
142
140
// Load offline page as string, from servlet context if
@@ -171,54 +169,14 @@ private URL getResourceUrl(ServletContext context, String path)
171
169
return resourceUrl ;
172
170
}
173
171
174
- private List <PwaIcon > initializeIcons (BufferedImage baseImage ,
175
- int bgColor ) {
172
+ private List <PwaIcon > initializeIcons () {
176
173
for (PwaIcon icon : getIconTemplates (pwaConfiguration .getIconPath ())) {
177
- // New image with wanted size
178
- icon .setImage (drawIconImage (baseImage , bgColor , icon ));
179
- // Store byte array and hashcode of image (GeneratedImage)
174
+ icon .setRegistry (this );
180
175
icons .add (icon );
181
176
}
182
177
return icons ;
183
178
}
184
179
185
- private BufferedImage drawIconImage (BufferedImage baseImage , int bgColor ,
186
- PwaIcon icon ) {
187
- BufferedImage bimage = new BufferedImage (icon .getWidth (),
188
- icon .getHeight (), BufferedImage .TYPE_INT_ARGB );
189
- // Draw the image on to the buffered image
190
- Graphics2D graphics = bimage .createGraphics ();
191
-
192
- // fill bg with fill-color
193
- graphics .setBackground (new Color (bgColor , true ));
194
- graphics .clearRect (0 , 0 , icon .getWidth (), icon .getHeight ());
195
-
196
- // calculate ratio (bigger ratio) for resize
197
- float ratio = (float ) baseImage .getWidth ()
198
- / (float ) icon .getWidth () > (float ) baseImage .getHeight ()
199
- / (float ) icon .getHeight ()
200
- ? (float ) baseImage .getWidth ()
201
- / (float ) icon .getWidth ()
202
- : (float ) baseImage .getHeight ()
203
- / (float ) icon .getHeight ();
204
-
205
- // Forbid upscaling of image
206
- ratio = ratio > 1.0f ? ratio : 1.0f ;
207
-
208
- // calculate sizes with ratio
209
- int newWidth = Math .round (baseImage .getHeight () / ratio );
210
- int newHeight = Math .round (baseImage .getWidth () / ratio );
211
-
212
- // draw rescaled img in the center of created image
213
- graphics .drawImage (
214
- baseImage .getScaledInstance (newWidth , newHeight ,
215
- Image .SCALE_SMOOTH ),
216
- (icon .getWidth () - newWidth ) / 2 ,
217
- (icon .getHeight () - newHeight ) / 2 , null );
218
- graphics .dispose ();
219
- return bimage ;
220
- }
221
-
222
180
/**
223
181
* Creates manifest.webmanifest json object.
224
182
*
0 commit comments