aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/draw9patch/graphics/GraphicsUtilities.java
blob: 74c2f043e5c281edd496d5b9a979a630ea03d2cd (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * 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.
 */

package com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics;

import org.eclipse.swt.graphics.ImageData;

/**
 * The utility class for SWT Image and ImageData manipulation.
 */
public class GraphicsUtilities {

    /**
     * Convert normal image to 9-patched.
     * @return Returns 9-patched ImageData object. If image is null, returns null.
     */
    public static ImageData convertToNinePatch(ImageData image) {
        if (image == null) {
            return null;
        }
        ImageData result = new ImageData(image.width + 2, image.height + 2, image.depth,
                image.palette);

        final int[] colors = new int[image.width];
        final byte[] alpha = new byte[image.width];

        for (int y = 0; y < image.height; y++) {

            // Copy pixels
            image.getPixels(0, y, image.width, colors, 0);
            result.setPixels(1, y + 1, image.width, colors, 0);

            // Copy alpha
            image.getAlphas(0, y, image.width, alpha, 0);
            result.setAlphas(1, y + 1, image.width, alpha, 0);
        }

        return result;
    }

    /**
     * Wipe all color and alpha pixels.
     */
    public static void clearImageData(ImageData imageData) {
        if (imageData == null) {
            throw new IllegalArgumentException("image data must not be null");
        }
        int width = imageData.width;
        int height = imageData.height;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                imageData.setPixel(x, y, 0x00000000);
                imageData.setAlpha(x, y, 0x00);
            }
        }
    }

    /**
     * Duplicate the image data.
     * @return If image is null, return null.
     */
    public static ImageData copy(ImageData image) {
        if (image == null) {
            return null;
        }
        ImageData result = new ImageData(image.width, image.height, image.depth,
                image.palette);

        final int[] colors = new int[image.width];
        final byte[] alpha = new byte[image.width];

        for (int y = 0; y < image.height; y++) {

            // Copy pixels
            image.getPixels(0, y, image.width, colors, 0);
            result.setPixels(0, y, image.width, colors, 0);

            // Copy alpha
            image.getAlphas(0, y, image.width, alpha, 0);
            result.setAlphas(0, y, image.width, alpha, 0);
        }

        return result;
    }

    /**
     * Get column pixels.
     * @return length of obtained pixels.
     */
    public static int getVerticalPixels(ImageData data, int x, int y, int height, int[] out) {
        if (data == null) {
            throw new IllegalArgumentException("data must not be null");
        }
        if (out == null) {
            throw new IllegalArgumentException("out array must not be null");
        }
        if (height > out.length) {
            throw new IllegalArgumentException("out array length must be > height");
        }
        if (data.height < (y + height)) {
            throw new IllegalArgumentException("image height must be > (y + height)");
        }
        if (x < 0 || y < 0) {
            throw new IllegalArgumentException("argument x, y must be >= 0");
        }
        if (x >= data.width) {
            throw new IllegalArgumentException("argument x must be < data.width");
        }
        if (y >= data.height) {
            throw new IllegalArgumentException("argument y must be < data.height");
        }
        if (height <= 0) {
            throw new IllegalArgumentException("argument height must be > 0");
        }

        int idx = 0;
        while (idx < height) {
            data.getPixels(x, (y + idx), 1, out, idx);
            idx++;
        }
        return idx;
    }

    /**
     * Get row pixels.
     */
    public static void getHorizontalPixels(ImageData data, int x, int y, int width, int[] out) {
        if (data == null) {
            throw new IllegalArgumentException("data must not be null");
        }
        if (out == null) {
            throw new IllegalArgumentException("out array must not be null");
        }
        if (width > out.length) {
            throw new IllegalArgumentException("out array length must be > width");
        }
        if (data.width < (x + width)) {
            throw new IllegalArgumentException("image height must be > (x + width)");
        }
        if (x < 0 || y < 0) {
            throw new IllegalArgumentException("argument x, y must be >= 0");
        }
        if (x >= data.width) {
            throw new IllegalArgumentException("argument x must be < data.width");
        }
        if (y >= data.height) {
            throw new IllegalArgumentException("argument y must be < data.height");
        }
        if (width <= 0) {
            throw new IllegalArgumentException("argument width must be > 0");
        }

        data.getPixels(x, y, width, out, 0);
    }

}