Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / images / CompositeImpl.java
blobbae94b31d9e647ff25f99a9884418f8ab8e4e19d
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.images;
5 import java.util.Map;
7 import com.google.appengine.api.images.ImagesServicePb.ImagesCompositeRequest;
8 import com.google.appengine.api.images.ImagesServicePb.CompositeImageOptions;
10 /**
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;
22 /**
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,
31 Anchor anchor) {
32 if (image == null) {
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]");
44 if (anchor == null) {
45 throw new IllegalArgumentException("Anchor must not be null");
47 this.image = image;
48 this.anchor = anchor;
49 this.xOffset = xOffset;
50 this.yOffset = yOffset;
51 this.opacity = opacity;
54 /** {@inheritDoc} */
55 @Override
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) {
72 switch (anchor) {
73 case TOP_LEFT:
74 return CompositeImageOptions.ANCHOR.TOP_LEFT;
75 case TOP_CENTER:
76 return CompositeImageOptions.ANCHOR.TOP;
77 case TOP_RIGHT:
78 return CompositeImageOptions.ANCHOR.TOP_RIGHT;
79 case CENTER_LEFT:
80 return CompositeImageOptions.ANCHOR.LEFT;
81 case CENTER_CENTER:
82 return CompositeImageOptions.ANCHOR.CENTER;
83 case CENTER_RIGHT:
84 return CompositeImageOptions.ANCHOR.RIGHT;
85 case BOTTOM_LEFT:
86 return CompositeImageOptions.ANCHOR.BOTTOM_LEFT;
87 case BOTTOM_CENTER:
88 return CompositeImageOptions.ANCHOR.BOTTOM;
89 case BOTTOM_RIGHT:
90 return CompositeImageOptions.ANCHOR.BOTTOM_RIGHT;
92 throw new IllegalArgumentException("Unexpected anchor value: " + anchor);