Skip to content

Commit 7d28555

Browse files
Extract createAndDrawImage method
1 parent b274488 commit 7d28555

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/main/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessor.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -190,40 +190,39 @@ public static boolean imagesAreEqualsWithDiff(BufferedImage image1, BufferedImag
190190
}
191191

192192
public static BufferedImage scale(BufferedImage source, double ratio) {
193-
return cropAndScale(source, ratio, 1.0,1.0);
193+
return cropAndScale(source, ratio, 1.0, 1.0);
194194
}
195-
196-
public static BufferedImage cropAndScale(BufferedImage source,double ratio, double cropWidth, double cropHeight) {
195+
196+
public static BufferedImage cropAndScale(BufferedImage source, double ratio, double cropWidth, double cropHeight) {
197197
int w = (int) (source.getWidth() * ratio);
198198
int h = (int) (source.getHeight() * ratio);
199-
BufferedImage scaledImage = getCompatibleImage(w, h, source);
200-
Graphics2D resultGraphics = scaledImage.createGraphics();
201-
resultGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
202-
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
203-
resultGraphics.drawImage(source, 0, 0, w, h, null);
204-
resultGraphics.dispose();
205-
return scaledImage.getSubimage(0, 0, (int)(w*cropWidth),(int)(h*cropHeight));
199+
BufferedImage scaledImage = createAndDrawImage(source, w, h);
200+
return scaledImage.getSubimage(0, 0, (int) (w * cropWidth), (int) (h * cropHeight));
206201
}
207-
208-
public static BufferedImage cropAndScale(BufferedImage source,double ratio, int maxWidth, int maxHeight) {
202+
203+
public static BufferedImage cropAndScale(BufferedImage source, double ratio, int maxWidth, int maxHeight) {
209204
int w = (int) (source.getWidth() * ratio);
210205
int h = (int) (source.getHeight() * ratio);
206+
BufferedImage scaledImage = createAndDrawImage(source, w, h);
207+
if (maxWidth != -1 && w > maxWidth) {
208+
w = maxWidth;
209+
}
210+
if (maxHeight != -1 && h > maxHeight) {
211+
h = maxHeight;
212+
}
213+
return scaledImage.getSubimage(0, 0, w, h);
214+
}
215+
216+
private static BufferedImage createAndDrawImage(BufferedImage source, int w, int h) {
211217
BufferedImage scaledImage = getCompatibleImage(w, h, source);
212218
Graphics2D resultGraphics = scaledImage.createGraphics();
213219
resultGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
214220
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
215221
resultGraphics.drawImage(source, 0, 0, w, h, null);
216222
resultGraphics.dispose();
217-
if(maxWidth != -1 && w > maxWidth){
218-
w = maxWidth;
219-
}
220-
if(maxHeight != -1 && h > maxHeight){
221-
h = maxHeight;
222-
}
223-
return scaledImage.getSubimage(0, 0, w,h);
223+
return scaledImage;
224224
}
225-
226-
225+
227226
private static BufferedImage getCompatibleImage(int w, int h, BufferedImage source) {
228227
BufferedImage bImage = null;
229228
try {

0 commit comments

Comments
 (0)