BitmapTest.java revision 1a44d5dcabc18cd5ef111f732ccff91683a1a093
1/*
2 * Copyright (C) 2006 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
17package android.graphics;
18
19import android.graphics.Bitmap;
20import android.graphics.Color;
21import android.test.suitebuilder.annotation.SmallTest;
22import junit.framework.TestCase;
23
24
25public class BitmapTest extends TestCase {
26
27    @SmallTest
28    public void testBasic() throws Exception {
29        Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
30        Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
31        Bitmap bm3 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_4444);
32
33        assertTrue("mutability", bm1.isMutable());
34        assertTrue("mutability", bm2.isMutable());
35        assertTrue("mutability", bm3.isMutable());
36
37        assertEquals("width", 100, bm1.getWidth());
38        assertEquals("width", 100, bm2.getWidth());
39        assertEquals("width", 100, bm3.getWidth());
40
41        assertEquals("rowbytes", 400, bm1.getRowBytes());
42        assertEquals("rowbytes", 200, bm2.getRowBytes());
43        assertEquals("rowbytes", 200, bm3.getRowBytes());
44
45        assertEquals("height", 200, bm1.getHeight());
46        assertEquals("height", 200, bm2.getHeight());
47        assertEquals("height", 200, bm3.getHeight());
48
49        assertTrue("hasAlpha", bm1.hasAlpha());
50        assertFalse("hasAlpha", bm2.hasAlpha());
51        assertTrue("hasAlpha", bm3.hasAlpha());
52
53        assertTrue("getConfig", bm1.getConfig() == Bitmap.Config.ARGB_8888);
54        assertTrue("getConfig", bm2.getConfig() == Bitmap.Config.RGB_565);
55        assertTrue("getConfig", bm3.getConfig() == Bitmap.Config.ARGB_4444);
56    }
57
58    @SmallTest
59    public void testMutability() throws Exception {
60        Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
61        Bitmap bm2 = Bitmap.createBitmap(new int[100 * 200], 100, 200,
62                                         Bitmap.Config.ARGB_8888);
63
64        assertTrue("mutability", bm1.isMutable());
65        assertFalse("mutability", bm2.isMutable());
66
67        bm1.eraseColor(0);
68
69        try {
70            bm2.eraseColor(0);
71            fail("eraseColor should throw exception");
72        } catch (IllegalStateException ex) {
73            // safe to catch and ignore this
74        }
75    }
76
77    @SmallTest
78    public void testGetPixelsWithAlpha() throws Exception {
79        int[] colors = new int[100];
80        for (int i = 0; i < 100; i++) {
81            colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
82        }
83
84        Bitmap bm = Bitmap.createBitmap(colors, 10, 10,
85                                        Bitmap.Config.ARGB_8888);
86
87        int[] pixels = new int[100];
88        bm.getPixels(pixels, 0, 10, 0, 0, 10, 10);
89        for (int i = 0; i < 100; i++) {
90            int p = bm.getPixel(i % 10, i / 10);
91            assertEquals("getPixels", p, pixels[i]);
92        }
93
94        for (int i = 0; i < 100; i++) {
95            int p = bm.getPixel(i % 10, i / 10);
96            assertEquals("getPixel", p, colors[i]);
97            assertEquals("pixel value", p,
98                         ((0xFF << 24) | (i << 16) | (i << 8) | i));
99        }
100
101    }
102
103    @SmallTest
104    public void testGetPixelsWithoutAlpha() throws Exception {
105        int[] colors = new int[100];
106        for (int i = 0; i < 100; i++) {
107            colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
108        }
109
110        Bitmap bm = Bitmap.createBitmap(colors, 10, 10, Bitmap.Config.RGB_565);
111
112        int[] pixels = new int[100];
113        bm.getPixels(pixels, 0, 10, 0, 0, 10, 10);
114        for (int i = 0; i < 100; i++) {
115            int p = bm.getPixel(i % 10, i / 10);
116            assertEquals("getPixels", p, pixels[i]);
117        }
118    }
119
120    @SmallTest
121    public void testSetPixelsWithAlpha() throws Exception {
122        int[] colors = new int[100];
123        for (int i = 0; i < 100; i++) {
124            colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
125        }
126
127        Bitmap.Config config = Bitmap.Config.ARGB_8888;
128        Bitmap bm1 = Bitmap.createBitmap(colors, 10, 10, config);
129        Bitmap bm2 = Bitmap.createBitmap(10, 10, config);
130
131        for (int i = 0; i < 100; i++) {
132            bm2.setPixel(i % 10, i / 10, colors[i]);
133        }
134
135        for (int i = 0; i < 100; i++) {
136            assertEquals("setPixel",
137                    bm1.getPixel(i % 10, i / 10), bm2.getPixel(i % 10, i / 10));
138        }
139
140        for (int i = 0; i < 100; i++) {
141            assertEquals("setPixel value",
142                         bm1.getPixel(i % 10, i / 10), colors[i]);
143        }
144    }
145
146    @SmallTest
147    public void testSetPixelsWithoutAlpha() throws Exception {
148        int[] colors = new int[100];
149        for (int i = 0; i < 100; i++) {
150            colors[i] = (0xFF << 24) | (i << 16) | (i << 8) | i;
151        }
152
153        Bitmap.Config config = Bitmap.Config.RGB_565;
154        Bitmap bm1 = Bitmap.createBitmap(colors, 10, 10, config);
155        Bitmap bm2 = Bitmap.createBitmap(10, 10, config);
156
157        for (int i = 0; i < 100; i++) {
158            bm2.setPixel(i % 10, i / 10, colors[i]);
159        }
160
161        for (int i = 0; i < 100; i++) {
162            assertEquals("setPixel", bm1.getPixel(i % 10, i / 10),
163                         bm2.getPixel(i % 10, i / 10));
164        }
165    }
166
167    private static int computePrePostMul(int alpha, int comp) {
168        if (alpha == 0) {
169            return 0;
170        }
171        int premul = Math.round(alpha * comp / 255.f);
172        int unpre = Math.round(255.0f * premul / alpha);
173        return unpre;
174    }
175
176    @SmallTest
177    public void testSetPixelsWithNonOpaqueAlpha() throws Exception {
178        int[] colors = new int[256];
179        for (int i = 0; i < 256; i++) {
180            colors[i] = (i << 24) | (0xFF << 16) | (0x80 << 8) | 0;
181        }
182
183        Bitmap.Config config = Bitmap.Config.ARGB_8888;
184
185        // create a bitmap with the color array specified
186        Bitmap bm1 = Bitmap.createBitmap(colors, 16, 16, config);
187
188        // create a bitmap with no colors, but then call setPixels
189        Bitmap bm2 = Bitmap.createBitmap(16, 16, config);
190        bm2.setPixels(colors, 0, 16, 0, 0, 16, 16);
191
192        // now check that we did a good job returning the unpremultiplied alpha
193        final int tolerance = 1;
194        for (int i = 0; i < 256; i++) {
195            int c0 = colors[i];
196            int c1 = bm1.getPixel(i % 16, i / 16);
197            int c2 = bm2.getPixel(i % 16, i / 16);
198
199            // these two should always be identical
200            assertEquals("getPixel", c1, c2);
201
202            // comparing the original (c0) with the returned color is tricky,
203            // since it gets premultiplied during the set(), and unpremultiplied
204            // by the get().
205            int a0 = Color.alpha(c0);
206            int a1 = Color.alpha(c1);
207            assertEquals("alpha", a0, a1);
208
209            int r0 = Color.red(c0);
210            int r1 = Color.red(c1);
211            int rr = computePrePostMul(a0, r0);
212            assertTrue("red", Math.abs(rr - r1) <= tolerance);
213
214            int g0 = Color.green(c0);
215            int g1 = Color.green(c1);
216            int gg = computePrePostMul(a0, g0);
217            assertTrue("green", Math.abs(gg - g1) <= tolerance);
218
219            int b0 = Color.blue(c0);
220            int b1 = Color.blue(c1);
221            int bb = computePrePostMul(a0, b0);
222            assertTrue("blue", Math.abs(bb - b1) <= tolerance);
223
224            if (false) {
225                int cc = Color.argb(a0, rr, gg, bb);
226                android.util.Log.d("skia", "original " + Integer.toHexString(c0) +
227                                " set+get " + Integer.toHexString(c1) +
228                               " local " + Integer.toHexString(cc));
229            }
230        }
231    }
232}
233