1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.images
;
7 import com
.google
.appengine
.api
.images
.ImagesServicePb
.ImagesCompositeRequest
;
8 import com
.google
.appengine
.api
.images
.ImagesServicePb
.CompositeImageOptions
;
11 * Implementation of Composite using alpha blending.
14 final class CompositeImpl
extends Composite
{
16 private final Image image
;
17 private final Anchor anchor
;
18 private final int xOffset
;
19 private final int yOffset
;
20 private final float opacity
;
23 * Creates a compositing operation with the supplied parameters.
24 * @param image Image to be placed onto the canvas.
25 * @param xOffset X offset from anchor position.
26 * @param yOffset X offset from anchor position.
27 * @param opacity Opacity of the image as a float in range [0.0, 1.0].
28 * @param anchor Anchor position of the image on the canvas.
30 CompositeImpl(Image image
, int xOffset
, int yOffset
, float opacity
,
33 throw new IllegalArgumentException("The image must not be null");
35 if (xOffset
> ImagesService
.MAX_RESIZE_DIMENSIONS
36 || xOffset
< -ImagesService
.MAX_RESIZE_DIMENSIONS
37 || yOffset
> ImagesService
.MAX_RESIZE_DIMENSIONS
38 || yOffset
< -ImagesService
.MAX_RESIZE_DIMENSIONS
) {
39 throw new IllegalArgumentException("Images must fit on the canvas");
41 if (opacity
< 0.0f
|| opacity
> 1.0f
) {
42 throw new IllegalArgumentException("Opacity must be in range [0, 1]");
45 throw new IllegalArgumentException("Anchor must not be null");
49 this.xOffset
= xOffset
;
50 this.yOffset
= yOffset
;
51 this.opacity
= opacity
;
56 void apply(ImagesCompositeRequest
.Builder request
, Map
<Image
, Integer
> imageIndexMap
) {
57 if (!imageIndexMap
.containsKey(image
)) {
58 imageIndexMap
.put(image
, request
.build().getImageCount());
59 request
.addImage(ImagesServiceImpl
.convertImageData(image
));
61 CompositeImageOptions
.Builder options
= CompositeImageOptions
.newBuilder();
62 int sourceId
= imageIndexMap
.get(image
);
63 options
.setSourceIndex(sourceId
);
64 options
.setXOffset(xOffset
);
65 options
.setYOffset(yOffset
);
66 options
.setOpacity(opacity
);
67 options
.setAnchor(convertAnchor(anchor
));
68 request
.addOptions(options
);
71 static CompositeImageOptions
.ANCHOR
convertAnchor(Anchor anchor
) {
74 return CompositeImageOptions
.ANCHOR
.TOP_LEFT
;
76 return CompositeImageOptions
.ANCHOR
.TOP
;
78 return CompositeImageOptions
.ANCHOR
.TOP_RIGHT
;
80 return CompositeImageOptions
.ANCHOR
.LEFT
;
82 return CompositeImageOptions
.ANCHOR
.CENTER
;
84 return CompositeImageOptions
.ANCHOR
.RIGHT
;
86 return CompositeImageOptions
.ANCHOR
.BOTTOM_LEFT
;
88 return CompositeImageOptions
.ANCHOR
.BOTTOM
;
90 return CompositeImageOptions
.ANCHOR
.BOTTOM_RIGHT
;
92 throw new IllegalArgumentException("Unexpected anchor value: " + anchor
);