• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
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 package com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics;
18 
19 import org.eclipse.swt.graphics.ImageData;
20 
21 /**
22  * The utility class for SWT Image and ImageData manipulation.
23  */
24 public class GraphicsUtilities {
25 
26     /**
27      * Convert normal image to 9-patched.
28      * @return Returns 9-patched ImageData object. If image is null, returns null.
29      */
convertToNinePatch(ImageData image)30     public static ImageData convertToNinePatch(ImageData image) {
31         if (image == null) {
32             return null;
33         }
34         ImageData result = new ImageData(image.width + 2, image.height + 2, image.depth,
35                 image.palette);
36 
37         final int[] colors = new int[image.width];
38         final byte[] alpha = new byte[image.width];
39 
40         for (int y = 0; y < image.height; y++) {
41 
42             // Copy pixels
43             image.getPixels(0, y, image.width, colors, 0);
44             result.setPixels(1, y + 1, image.width, colors, 0);
45 
46             // Copy alpha
47             image.getAlphas(0, y, image.width, alpha, 0);
48             result.setAlphas(1, y + 1, image.width, alpha, 0);
49         }
50 
51         return result;
52     }
53 
54     /**
55      * Wipe all color and alpha pixels.
56      */
clearImageData(ImageData imageData)57     public static void clearImageData(ImageData imageData) {
58         if (imageData == null) {
59             throw new IllegalArgumentException("image data must not be null");
60         }
61         int width = imageData.width;
62         int height = imageData.height;
63         for (int y = 0; y < height; y++) {
64             for (int x = 0; x < width; x++) {
65                 imageData.setPixel(x, y, 0x00000000);
66                 imageData.setAlpha(x, y, 0x00);
67             }
68         }
69     }
70 
71     /**
72      * Duplicate the image data.
73      * @return If image is null, return null.
74      */
copy(ImageData image)75     public static ImageData copy(ImageData image) {
76         if (image == null) {
77             return null;
78         }
79         ImageData result = new ImageData(image.width, image.height, image.depth,
80                 image.palette);
81 
82         final int[] colors = new int[image.width];
83         final byte[] alpha = new byte[image.width];
84 
85         for (int y = 0; y < image.height; y++) {
86 
87             // Copy pixels
88             image.getPixels(0, y, image.width, colors, 0);
89             result.setPixels(0, y, image.width, colors, 0);
90 
91             // Copy alpha
92             image.getAlphas(0, y, image.width, alpha, 0);
93             result.setAlphas(0, y, image.width, alpha, 0);
94         }
95 
96         return result;
97     }
98 
99     /**
100      * Get column pixels.
101      * @return length of obtained pixels.
102      */
getVerticalPixels(ImageData data, int x, int y, int height, int[] out)103     public static int getVerticalPixels(ImageData data, int x, int y, int height, int[] out) {
104         if (data == null) {
105             throw new IllegalArgumentException("data must not be null");
106         }
107         if (out == null) {
108             throw new IllegalArgumentException("out array must not be null");
109         }
110         if (height > out.length) {
111             throw new IllegalArgumentException("out array length must be > height");
112         }
113         if (data.height < (y + height)) {
114             throw new IllegalArgumentException("image height must be > (y + height)");
115         }
116         if (x < 0 || y < 0) {
117             throw new IllegalArgumentException("argument x, y must be >= 0");
118         }
119         if (x >= data.width) {
120             throw new IllegalArgumentException("argument x must be < data.width");
121         }
122         if (y >= data.height) {
123             throw new IllegalArgumentException("argument y must be < data.height");
124         }
125         if (height <= 0) {
126             throw new IllegalArgumentException("argument height must be > 0");
127         }
128 
129         int idx = 0;
130         while (idx < height) {
131             data.getPixels(x, (y + idx), 1, out, idx);
132             idx++;
133         }
134         return idx;
135     }
136 
137     /**
138      * Get row pixels.
139      */
getHorizontalPixels(ImageData data, int x, int y, int width, int[] out)140     public static void getHorizontalPixels(ImageData data, int x, int y, int width, int[] out) {
141         if (data == null) {
142             throw new IllegalArgumentException("data must not be null");
143         }
144         if (out == null) {
145             throw new IllegalArgumentException("out array must not be null");
146         }
147         if (width > out.length) {
148             throw new IllegalArgumentException("out array length must be > width");
149         }
150         if (data.width < (x + width)) {
151             throw new IllegalArgumentException("image height must be > (x + width)");
152         }
153         if (x < 0 || y < 0) {
154             throw new IllegalArgumentException("argument x, y must be >= 0");
155         }
156         if (x >= data.width) {
157             throw new IllegalArgumentException("argument x must be < data.width");
158         }
159         if (y >= data.height) {
160             throw new IllegalArgumentException("argument y must be < data.height");
161         }
162         if (width <= 0) {
163             throw new IllegalArgumentException("argument width must be > 0");
164         }
165 
166         data.getPixels(x, y, width, out, 0);
167     }
168 
169 }
170