Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.cropresize.resizers

File MultiStepResizer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
42% of files have more coverage

Code metrics

22
37
1
1
111
59
13
0.35
37
1
13

Classes

Class Line # Actions
MultiStepResizer 51 37 0% 13 10
0.833333383.3%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.cropresize.resizers;
35   
36    import info.magnolia.imaging.operations.cropresize.Coords;
37    import info.magnolia.imaging.operations.cropresize.Size;
38    import info.magnolia.imaging.util.ImageUtil;
39   
40    import java.awt.Graphics2D;
41    import java.awt.RenderingHints;
42    import java.awt.image.BufferedImage;
43   
44    /**
45    * Highly inspired by http://today.java.net/lpt/a/362
46    *
47    * This method will use a multi-step scaling technique that provides higher quality than the usual
48    * one-step technique (only useful in downscaling cases, where {@code targetWidth} or {@code targetHeight} is
49    * smaller than the original dimensions, and generally only when the {@code BILINEAR} hint is specified).
50    */
 
51    public class MultiStepResizer extends BasicResizer {
 
52  6 toggle @Override
53    public BufferedImage resize(BufferedImage src, Coords srcCoords, Size targetSize) {
54  6 final int targetWidth = targetSize.getWidth();
55  6 final int targetHeight = targetSize.getHeight();
56  6 final int type = ImageUtil.getImageType(src);
57  6 BufferedImage ret = src;
58  6 int w, h;
59   
60    // Use multi-step technique: start with original size, then
61    // scale down in multiple passes with drawImage()
62    // until the target size is reached
63  6 w = srcCoords.getWidth();
64  6 h = srcCoords.getHeight();
65   
66  6 boolean firstStep = true;
67  6 do {
68  6 if (w > targetWidth) {
69  3 w /= 2;
70  3 if (w < targetWidth) {
71  3 w = targetWidth;
72    }
73  3 } else if (w < targetWidth) {
74  3 w = targetWidth * 2;
75  3 if (w > targetWidth) {
76  3 w = targetWidth;
77    }
78    }
79   
80  6 if (h > targetHeight) {
81  3 h /= 2;
82  3 if (h < targetHeight) {
83  3 h = targetHeight;
84    }
85  3 } else if (h < targetHeight) {
86  3 h = targetHeight * 2;
87  3 if (h > targetHeight) {
88  3 h = targetHeight;
89    }
90    }
91   
92  6 final BufferedImage tmp = new BufferedImage(w, h, type);
93  6 final Graphics2D g2 = tmp.createGraphics();
94  6 g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, getInterpolationHint());
95  6 if (firstStep) {
96  6 g2.drawImage(ret, 0, 0, w, h, srcCoords.getX1(), srcCoords.getY1(), srcCoords.getX2(), srcCoords.getY2(), null);
97    } else {
98  0 g2.drawImage(ret, 0, 0, w, h, null);
99    }
100  6 g2.dispose();
101  6 if (ret != null) {
102    // if we are generating many images, flush native resources w/o waiting for GC to do this for you
103  6 ret.flush();
104    }
105  6 ret = tmp;
106  6 firstStep = false;
107  6 } while (w != targetWidth || h != targetHeight);
108   
109  6 return ret;
110    }
111    }