aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/ImageResizeUtil.java
blob: 4db8c72e5a6ea3e922dcf98fa858468bfd881715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

/**
 * Utility to resize images.
 * 
 * @author luc boutier
 */
public final class ImageResizeUtil {
	private ImageResizeUtil() {
	}

	/**
	 * Resize an image with default quality settings.
	 * 
	 * @param originalImage
	 *            The image to resize.
	 * @param width
	 *            The target width.
	 * @param height
	 *            The target height.
	 * @param preserveDimensions
	 *            Flag to know if we should preserve original image dimensions.
	 * @return The resized image.
	 */
	public static BufferedImage resizeImage(final BufferedImage originalImage, final int width, final int height,
			final boolean preserveDimensions) {
		return resizeImage(originalImage, width, height, preserveDimensions, false);
	}

	/**
	 * <p>
	 * Resize an image with high quality settings.
	 * </p>
	 * <ul>
	 * <li>g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
	 * RenderingHints.VALUE_INTERPOLATION_BILINEAR);</li>
	 * <li>g.setRenderingHint(RenderingHints.KEY_RENDERING,
	 * RenderingHints.VALUE_RENDER_QUALITY);</li>
	 * <li>g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	 * RenderingHints.VALUE_ANTIALIAS_ON);</li>
	 * </ul>
	 * 
	 * @param originalImage
	 *            The image to resize.
	 * @param width
	 *            The target width.
	 * @param height
	 *            The target height.
	 * @param preserveDimensions
	 *            Flag to know if we should preserve original image dimensions.
	 * @return The resized image.
	 */
	public static BufferedImage resizeImageWithHint(BufferedImage originalImage, final int width, final int height,
			final boolean preserveDimensions) {
		return resizeImage(originalImage, width, height, preserveDimensions, true);
	}

	private static BufferedImage resizeImage(BufferedImage originalImage, final int width, final int height,
			final boolean preserveDimensions, final boolean enableHighQuality) {
		int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

		int targetWidth = width;
		int targetHeight = height;

		if (preserveDimensions) {
			int[] targetDimentions = computeDimensions(width, height, originalImage.getWidth(),
					originalImage.getHeight());
			targetWidth = targetDimentions[0];
			targetHeight = targetDimentions[1];
		}

		BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, type);

		Graphics2D g = resizedImage.createGraphics();
		if (enableHighQuality) {
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		}

		g.drawImage(originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0, 0, null);
		g.dispose();

		return resizedImage;
	}

	/**
	 * Compute target width and height based on requested width and height but
	 * making sure the original dimensions of the image will be preserved.
	 * 
	 * @param width
	 *            The ideal (and max) target width.
	 * @param height
	 *            The ideal (and max) target height.
	 * @param originalWidth
	 *            The original width.
	 * @param originalHeight
	 *            The original height.
	 * @return An array of int that contains the ideal width and height to
	 *         preserve dimensions.
	 */
	public static int[] computeDimensions(final int width, final int height, final int originalWidth,
			final int originalHeight) {
		int targetWidth = width;
		int targetHeight = height;

		float targetDimensions = Float.valueOf(width).floatValue() / Float.valueOf(height).floatValue();
		float sourceDimensions = Float.valueOf(originalWidth).floatValue() / Float.valueOf(originalHeight).floatValue();
		if (targetDimensions > sourceDimensions) {
			targetWidth = Float.valueOf(width * sourceDimensions / targetDimensions).intValue();
		} else {
			targetHeight = Float.valueOf(height * targetDimensions / sourceDimensions).intValue();
		}

		return new int[] { targetWidth, targetHeight };
	}
}