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