1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.images
;
6 * A transform that will resize an image to fit within a bounding box.
9 final class Resize
extends Transform
{
11 private static final long serialVersionUID
= -889209644904728094L;
13 private final int width
;
14 private final int height
;
15 private final boolean cropToFit
;
16 private final float cropOffsetX
;
17 private final float cropOffsetY
;
18 private final boolean allowStretch
;
21 * Creates a transform that will resize an image to fit within a rectangle
22 * with the given dimensions. If {@code allowStretch} is true, then the image
23 * is resized without maintaining the original aspect ratio.
24 * @param width width of the bounding box
25 * @param height height of the bounding box
26 * @param allowStretch resize the image without maintaining the aspect ratio.
27 * @throws IllegalArgumentException If {@code width} or {@code height} are
28 * negative or greater than {@code MAX_RESIZE_DIMENSIONS}, if both
29 * {@code width} and {@code height} are 0 or if {@code allowStretch} is
30 * set and {@code width} or {@code height} is 0.
32 Resize(int width
, int height
, boolean allowStretch
) {
33 this(width
, height
, false, 0, 0, allowStretch
);
37 * Creates a transform that will resize an image to fit within a rectangle
38 * with the given dimensions. If {@code cropToFit} is true, then the image is
39 * cropped to fit, with the center specified by {@code cropOffsetX} and
40 * {@code cropOffsetY}.
41 * @param width width of the bounding box
42 * @param height height of the bounding box
43 * @param cropToFit whether the image should be cropped to fit
44 * @param cropOffsetX the relative horizontal position of the center
45 * @param cropOffsetY the relative vertical position of the center
46 * @throws IllegalArgumentException If {@code width} or {@code height} are
47 * negative or greater than {@code MAX_RESIZE_DIMENSIONS}, if both
48 * {@code width} and {@code height} are 0 or if {@code cropToFit} is
49 * set and {@code width} or {@code height} is 0 or {@code cropOffsetX} or
50 * {@code cropOffsetY} is outside the range 0.0 to 1.0.
52 Resize(int width
, int height
, boolean cropToFit
, float cropOffsetX
, float cropOffsetY
) {
53 this(width
, height
, cropToFit
, cropOffsetX
, cropOffsetY
, false);
57 * Creates a transform that will resize an image to fit within a rectangle
58 * with the given dimensions. If {@code cropToFit} is true, then the image is
59 * cropped to fit, with the center specified by {@code cropOffsetX} and
60 * {@code cropOffsetY}.
61 * @param width width of the bounding box
62 * @param height height of the bounding box
63 * @param cropToFit whether the image should be cropped to fit
64 * @param cropOffsetX the relative horizontal position of the center
65 * @param cropOffsetY the relative vertical position of the center
66 * @param allowStretch resize the image without maintaining the aspect ratio.
67 * @throws IllegalArgumentException If {@code width} or {@code height} are
68 * negative or greater than {@code MAX_RESIZE_DIMENSIONS}, if both
69 * {@code width} and {@code height} are 0, if {@code allowStretch} is set and
70 * and {@code width} or {@code height} is 0 or if {@code cropToFit} is
71 * set and {@code width} or {@code height} is 0 or {@code cropOffsetX} or
72 * {@code cropOffsetY} is outside the range 0.0 to 1.0.
74 Resize(int width
, int height
, boolean cropToFit
, float cropOffsetX
,
75 float cropOffsetY
, boolean allowStretch
) {
76 if (width
> ImagesService
.MAX_RESIZE_DIMENSIONS
77 || height
> ImagesService
.MAX_RESIZE_DIMENSIONS
) {
78 throw new IllegalArgumentException("width and height must be <= "
79 + ImagesService
.MAX_RESIZE_DIMENSIONS
);
81 if (width
< 0 || height
< 0) {
82 throw new IllegalArgumentException("width and height must be >= 0");
84 if (width
== 0 && height
== 0) {
85 throw new IllegalArgumentException("width and height must not both be == 0");
88 if (width
== 0 || height
== 0) {
89 throw new IllegalArgumentException(
90 "neither of width and height can be == 0 with crop to fit enabled");
92 checkCropArgument(cropOffsetX
);
93 checkCropArgument(cropOffsetY
);
96 if (width
== 0 || height
== 0) {
97 throw new IllegalArgumentException(
98 "Resize requests with allowStretch as true require that both "
99 + "width and hight are non zero");
103 this.height
= height
;
104 this.cropToFit
= cropToFit
;
105 this.cropOffsetX
= cropOffsetX
;
106 this.cropOffsetY
= cropOffsetY
;
107 this.allowStretch
= allowStretch
;
112 void apply(ImagesServicePb
.ImagesTransformRequest
.Builder request
) {
113 request
.addTransform(
114 ImagesServicePb
.Transform
.newBuilder()
117 .setCropToFit(cropToFit
)
118 .setCropOffsetX(cropOffsetX
)
119 .setCropOffsetY(cropOffsetY
)
120 .setAllowStretch(allowStretch
));
124 * Checks that a crop argument is in the valid range.
125 * @param arg crop argument
127 private void checkCropArgument(float arg
) {
129 throw new IllegalArgumentException("Crop offsets must be >= 0");
132 throw new IllegalArgumentException("Crop offsets must be <= 1");