• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.example.android.apis.graphics;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.graphics.*;
24 import android.graphics.drawable.*;
25 import android.os.Bundle;
26 import android.view.KeyEvent;
27 import android.view.*;
28 
29 import java.nio.IntBuffer;
30 import java.nio.ShortBuffer;
31 
32 public class BitmapPixels extends GraphicsActivity {
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         setContentView(new SampleView(this));
38     }
39 
40     private static class SampleView extends View {
41         private Bitmap mBitmap1;
42         private Bitmap mBitmap2;
43         private Bitmap mBitmap3;
44         private Bitmap mBitmap4;
45 
46         // access the red component from a premultiplied color
getR32(int c)47         private static int getR32(int c) { return (c >>  0) & 0xFF; }
48         // access the red component from a premultiplied color
getG32(int c)49         private static int getG32(int c) { return (c >>  8) & 0xFF; }
50         // access the red component from a premultiplied color
getB32(int c)51         private static int getB32(int c) { return (c >> 16) & 0xFF; }
52         // access the red component from a premultiplied color
getA32(int c)53         private static int getA32(int c) { return (c >> 24) & 0xFF; }
54 
55         /**
56          * This takes components that are already in premultiplied form, and
57          * packs them into an int in the correct device order.
58          */
pack8888(int r, int g, int b, int a)59         private static int pack8888(int r, int g, int b, int a) {
60             return (r << 0) | ( g << 8) | (b << 16) | (a << 24);
61         }
62 
pack565(int r, int g, int b)63         private static short pack565(int r, int g, int b) {
64             return (short)((r << 11) | ( g << 5) | (b << 0));
65         }
66 
pack4444(int r, int g, int b, int a)67         private static short pack4444(int r, int g, int b, int a) {
68             return (short)((a << 0) | ( b << 4) | (g << 8) | (r << 12));
69         }
70 
mul255(int c, int a)71         private static int mul255(int c, int a) {
72             int prod = c * a + 128;
73             return (prod + (prod >> 8)) >> 8;
74         }
75 
76         /**
77          * Turn a color int into a premultiplied device color
78          */
premultiplyColor(int c)79         private static int premultiplyColor(int c) {
80             int r = Color.red(c);
81             int g = Color.green(c);
82             int b = Color.blue(c);
83             int a = Color.alpha(c);
84             // now apply the alpha to r, g, b
85             r = mul255(r, a);
86             g = mul255(g, a);
87             b = mul255(b, a);
88             // now pack it in the correct order
89             return pack8888(r, g, b, a);
90         }
91 
makeRamp(int from, int to, int n, int[] ramp8888, short[] ramp565, short[] ramp4444)92         private static void makeRamp(int from, int to, int n,
93                                      int[] ramp8888, short[] ramp565,
94                                      short[] ramp4444) {
95             int r = getR32(from) << 23;
96             int g = getG32(from) << 23;
97             int b = getB32(from) << 23;
98             int a = getA32(from) << 23;
99             // now compute our step amounts per componenet (biased by 23 bits)
100             int dr = ((getR32(to) << 23) - r) / (n - 1);
101             int dg = ((getG32(to) << 23) - g) / (n - 1);
102             int db = ((getB32(to) << 23) - b) / (n - 1);
103             int da = ((getA32(to) << 23) - a) / (n - 1);
104 
105             for (int i = 0; i < n; i++) {
106                 ramp8888[i] = pack8888(r >> 23, g >> 23, b >> 23, a >> 23);
107                 ramp565[i] = pack565(r >> (23+3), g >> (23+2), b >> (23+3));
108                 ramp4444[i] = pack4444(r >> (23+4), g >> (23+4), b >> (23+4),
109                                        a >> (23+4));
110                 r += dr;
111                 g += dg;
112                 b += db;
113                 a += da;
114             }
115         }
116 
makeBuffer(int[] src, int n)117         private static IntBuffer makeBuffer(int[] src, int n) {
118             IntBuffer dst = IntBuffer.allocate(n*n);
119             for (int i = 0; i < n; i++) {
120                 dst.put(src);
121             }
122             dst.rewind();
123             return dst;
124         }
125 
makeBuffer(short[] src, int n)126         private static ShortBuffer makeBuffer(short[] src, int n) {
127             ShortBuffer dst = ShortBuffer.allocate(n*n);
128             for (int i = 0; i < n; i++) {
129                 dst.put(src);
130             }
131             dst.rewind();
132             return dst;
133         }
134 
SampleView(Context context)135         public SampleView(Context context) {
136             super(context);
137             setFocusable(true);
138 
139             final int N = 100;
140             int[] data8888 = new int[N];
141             short[] data565 = new short[N];
142             short[] data4444 = new short[N];
143 
144             makeRamp(premultiplyColor(Color.RED), premultiplyColor(Color.GREEN),
145                      N, data8888, data565, data4444);
146 
147             mBitmap1 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_8888);
148             mBitmap2 = Bitmap.createBitmap(N, N, Bitmap.Config.RGB_565);
149             mBitmap3 = Bitmap.createBitmap(N, N, Bitmap.Config.ARGB_4444);
150 
151             mBitmap1.copyPixelsFromBuffer(makeBuffer(data8888, N));
152             mBitmap2.copyPixelsFromBuffer(makeBuffer(data565, N));
153             mBitmap3.copyPixelsFromBuffer(makeBuffer(data4444, N));
154         }
155 
onDraw(Canvas canvas)156         @Override protected void onDraw(Canvas canvas) {
157             canvas.drawColor(0xFFCCCCCC);
158 
159             int y = 10;
160             canvas.drawBitmap(mBitmap1, 10, y, null);
161             y += mBitmap1.getHeight() + 10;
162             canvas.drawBitmap(mBitmap2, 10, y, null);
163             y += mBitmap2.getHeight() + 10;
164             canvas.drawBitmap(mBitmap3, 10, y, null);
165         }
166     }
167 }
168 
169