View Javadoc
1   /*
2    * Copyright 2015 Jin Kwon <jinahya_at_gmail.com>.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package com.github.jinahya.imgscalr;
19  
20  
21  import java.awt.Graphics2D;
22  import java.awt.image.BufferedImage;
23  import java.awt.image.BufferedImageOp;
24  import static org.imgscalr.Scalr.Method;
25  import static org.imgscalr.Scalr.Mode;
26  
27  
28  /**
29   *
30   * @author Jin Kwon <jinahya_at_gmail.com>
31   */
32  public strictfp class Scalr extends org.imgscalr.Scalr {
33  
34  
35      /**
36       * Resizes specified image preserving the proportion and relocates by given
37       * arguments.
38       *
39       * @param sourceImage the source image to resize.
40       * @param scalingMethod the scaling method.
41       * @param targetWidth the target width.
42       * @param targetHeight the target height.
43       * @param magnificationFactor the magnification factor. {@code 1.0f} for
44       * fitting into the box, {@code 1.0f} for fit to max width or height.
45       * @param horizontalWeigth the horizontal weight. {@code 1.0f} for aligning
46       * to the left align, {@code 1.0f} for aligning to the right.
47       * @param verticalWeight the vertical weight. {@code 1.0f} for aligning to
48       * the top, {@code 1.0f} for aligning to the bottom.
49       * @param ops
50       *
51       * @return a resized image.
52       */
53      public static BufferedImage resize(final BufferedImage sourceImage,
54                                         final Method scalingMethod,
55                                         final int targetWidth,
56                                         final int targetHeight,
57                                         final float magnificationFactor,
58                                         final float horizontalWeigth,
59                                         final float verticalWeight,
60                                         final BufferedImageOp... ops) {
61  
62          final int sourceWidth = sourceImage.getWidth();
63          final int sourceHeight = sourceImage.getHeight();
64  
65          final double widthRatio = targetWidth / (double) sourceWidth;
66          final double heightRatio = targetHeight / (double) sourceHeight;
67  
68          final double minRatio = Math.min(widthRatio, heightRatio);
69          final double maxRatio = Math.max(widthRatio, heightRatio);
70  
71          final int auxiliaryWidth
72              = (int) (magnificationFactor == .0f
73                       ? sourceWidth * minRatio
74                       : (sourceWidth * maxRatio) * magnificationFactor);
75          final int auxiliaryHeight
76              = (int) (magnificationFactor == .0f
77                       ? sourceHeight * minRatio
78                       : (sourceHeight * maxRatio) * magnificationFactor);
79  
80  //        final int imageType
81  //            = auxiliaryImage.getTransparency() == Transparency.OPAQUE
82  //              ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
83          final int imageType = BufferedImage.TYPE_INT_ARGB;
84          final BufferedImage targetImage = new BufferedImage(
85              targetWidth, targetHeight, imageType);
86  
87          final BufferedImage auxiliaryImage = resize(
88              sourceImage, scalingMethod, Mode.FIT_EXACT, auxiliaryWidth,
89              auxiliaryHeight);
90  
91          final int x = (int) ((targetWidth - auxiliaryWidth) * horizontalWeigth);
92          final int y = (int) ((targetHeight - auxiliaryHeight) * verticalWeight);
93          final Graphics2D graphics = targetImage.createGraphics();
94          graphics.drawImage(auxiliaryImage, x, y, null);
95          graphics.dispose();
96  
97          auxiliaryImage.flush();
98  
99          return targetImage;
100     }
101 
102 
103     /**
104      *
105      * @param sourceImage
106      * @param scalingMethod
107      * @param targetWidth
108      * @param targetHeight
109      * @param horizontalWeigth
110      * @param verticalWeight
111      * @param ops
112      *
113      * @return a resized image.
114      *
115      * @see #resize(java.awt.image.BufferedImage, org.imgscalr.Scalr.Method,
116      * int, int, float, float, float, java.awt.image.BufferedImageOp...)
117      */
118     public static BufferedImage resizeInbox(final BufferedImage sourceImage,
119                                             final Method scalingMethod,
120                                             final int targetWidth,
121                                             final int targetHeight,
122                                             final float horizontalWeigth,
123                                             final float verticalWeight,
124                                             final BufferedImageOp... ops) {
125 
126         return resize(sourceImage, scalingMethod, targetWidth, targetHeight,
127                       .0f, horizontalWeigth, verticalWeight, ops);
128     }
129 
130 
131     /**
132      *
133      * @param sourceImage
134      * @param scalingMethod
135      * @param targetWidth
136      * @param targetHeight
137      * @param ops
138      *
139      * @return
140      *
141      * @see #resizeInbox(java.awt.image.BufferedImage,
142      * org.imgscalr.Scalr.Method, int, int, float, float,
143      * java.awt.image.BufferedImageOp...)
144      */
145     public static BufferedImage resizeInboxUpperLeft(
146         final BufferedImage sourceImage, final Method scalingMethod,
147         final int targetWidth, final int targetHeight,
148         final BufferedImageOp... ops) {
149 
150         return resizeInbox(sourceImage, scalingMethod, targetWidth,
151                            targetHeight, .0f, .0f, ops);
152     }
153 
154 
155     /**
156      *
157      * @param sourceImage
158      * @param scalingMethod
159      * @param targetWidth
160      * @param targetHeight
161      * @param ops
162      *
163      * @return
164      *
165      * @see #resizeInbox(java.awt.image.BufferedImage,
166      * org.imgscalr.Scalr.Method, int, int, float, float,
167      * java.awt.image.BufferedImageOp...)
168      */
169     public static BufferedImage resizeInboxCenter(
170         final BufferedImage sourceImage, final Method scalingMethod,
171         final int targetWidth, final int targetHeight,
172         final BufferedImageOp... ops) {
173 
174         return resizeInbox(sourceImage, scalingMethod, targetWidth,
175                            targetHeight, .5f, .5f, ops);
176     }
177 
178 
179     /**
180      *
181      * @param sourceImage
182      * @param scalingMethod
183      * @param targetWidth
184      * @param targetHeight
185      * @param ops
186      *
187      * @return
188      *
189      * @see #resizeInbox(java.awt.image.BufferedImage,
190      * org.imgscalr.Scalr.Method, int, int, float, float,
191      * java.awt.image.BufferedImageOp...)
192      */
193     public static BufferedImage resizeInboxLowerRight(
194         final BufferedImage sourceImage, final Method scalingMethod,
195         final int targetWidth, final int targetHeight,
196         final BufferedImageOp... ops) {
197 
198         return resizeInbox(sourceImage, scalingMethod, targetWidth,
199                            targetHeight, 1.0f, 1.0f, ops);
200     }
201 
202 
203     public static BufferedImage resizeOutbox(final BufferedImage sourceImage,
204                                              final Method scalingMethod,
205                                              final int targetWidth,
206                                              final int targetHeight,
207                                              final float horizontalWeigth,
208                                              final float verticalWeight,
209                                              final BufferedImageOp... ops) {
210 
211         return resize(sourceImage, scalingMethod, targetWidth, targetHeight,
212                       1.0f, horizontalWeigth, verticalWeight, ops);
213     }
214 
215 
216     public static BufferedImage resizeOutboxUpperLeft(
217         final BufferedImage sourceImage, final Method scalingMethod,
218         final int targetWidth, final int targetHeight,
219         final BufferedImageOp... ops) {
220 
221         return resizeOutbox(sourceImage, scalingMethod, targetWidth,
222                             targetHeight, .0f, .0f, ops);
223     }
224 
225 
226     public static BufferedImage resizeOutboxCenter(
227         final BufferedImage sourceImage, final Method scalingMethod,
228         final int targetWidth, final int targetHeight,
229         final BufferedImageOp... ops) {
230 
231         return resizeOutbox(sourceImage, scalingMethod, targetWidth,
232                             targetHeight, .5f, .5f, ops);
233     }
234 
235 
236     public static BufferedImage resizeOutboxLowerRight(
237         final BufferedImage sourceImage, final Method scalingMethod,
238         final int targetWidth, final int targetHeight,
239         final BufferedImageOp... ops) {
240 
241         return resizeOutbox(sourceImage, scalingMethod, targetWidth,
242                             targetHeight, 1.0f, 1.0f, ops);
243     }
244 
245 
246     private Scalr() {
247 
248         super();
249     }
250 
251 
252 }
253