Bitmap.cpp revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1#include "SkBitmap.h" 2#include "SkImageDecoder.h" 3#include "SkColorPriv.h" 4#include "GraphicsJNI.h" 5#include "SkDither.h" 6#include "SkUnPreMultiply.h" 7 8#include "Parcel.h" 9#include "android_util_Binder.h" 10#include "android_nio_utils.h" 11#include "CreateJavaOutputStreamAdaptor.h" 12 13#include <jni.h> 14 15#if 0 16 #define TRACE_BITMAP(code) code 17#else 18 #define TRACE_BITMAP(code) 19#endif 20 21/////////////////////////////////////////////////////////////////////////////// 22// Conversions to/from SkColor, for get/setPixels, and the create method, which 23// is basically like setPixels 24 25typedef void (*FromColorProc)(void* dst, const SkColor src[], int width, 26 int x, int y); 27 28static void FromColor_D32(void* dst, const SkColor src[], int width, 29 int, int) { 30 SkPMColor* d = (SkPMColor*)dst; 31 32 for (int i = 0; i < width; i++) { 33 *d++ = SkPreMultiplyColor(*src++); 34 } 35} 36 37static void FromColor_D565(void* dst, const SkColor src[], int width, 38 int x, int y) { 39 uint16_t* d = (uint16_t*)dst; 40 41 DITHER_565_SCAN(y); 42 for (int stop = x + width; x < stop; x++) { 43 SkColor c = *src++; 44 *d++ = SkDitherRGBTo565(SkColorGetR(c), SkColorGetG(c), SkColorGetB(c), 45 DITHER_VALUE(x)); 46 } 47} 48 49static void FromColor_D4444(void* dst, const SkColor src[], int width, 50 int x, int y) { 51 SkPMColor16* d = (SkPMColor16*)dst; 52 53 DITHER_4444_SCAN(y); 54 for (int stop = x + width; x < stop; x++) { 55 SkPMColor c = SkPreMultiplyColor(*src++); 56 *d++ = SkDitherARGB32To4444(c, DITHER_VALUE(x)); 57// *d++ = SkPixel32ToPixel4444(c); 58 } 59} 60 61// can return NULL 62static FromColorProc ChooseFromColorProc(SkBitmap::Config config) { 63 switch (config) { 64 case SkBitmap::kARGB_8888_Config: 65 return FromColor_D32; 66 case SkBitmap::kARGB_4444_Config: 67 return FromColor_D4444; 68 case SkBitmap::kRGB_565_Config: 69 return FromColor_D565; 70 default: 71 break; 72 } 73 return NULL; 74} 75 76bool GraphicsJNI::SetPixels(JNIEnv* env, jintArray srcColors, 77 int srcOffset, int srcStride, 78 int x, int y, int width, int height, 79 const SkBitmap& dstBitmap) { 80 SkAutoLockPixels alp(dstBitmap); 81 void* dst = dstBitmap.getPixels(); 82 FromColorProc proc = ChooseFromColorProc(dstBitmap.config()); 83 84 if (NULL == dst || NULL == proc) { 85 return false; 86 } 87 88 const jint* array = env->GetIntArrayElements(srcColors, NULL); 89 const SkColor* src = (const SkColor*)array + srcOffset; 90 91 // reset to to actual choice from caller 92 dst = dstBitmap.getAddr(x, y); 93 // now copy/convert each scanline 94 for (int y = 0; y < height; y++) { 95 proc(dst, src, width, x, y); 96 src += srcStride; 97 dst = (char*)dst + dstBitmap.rowBytes(); 98 } 99 100 env->ReleaseIntArrayElements(srcColors, const_cast<jint*>(array), 101 JNI_ABORT); 102 return true; 103} 104 105//////////////////// ToColor procs 106 107typedef void (*ToColorProc)(SkColor dst[], const void* src, int width, 108 SkColorTable*); 109 110static void ToColor_S32_Alpha(SkColor dst[], const void* src, int width, 111 SkColorTable*) { 112 SkASSERT(width > 0); 113 const SkPMColor* s = (const SkPMColor*)src; 114 do { 115 *dst++ = SkUnPreMultiply::PMColorToColor(*s++); 116 } while (--width != 0); 117} 118 119static void ToColor_S32_Opaque(SkColor dst[], const void* src, int width, 120 SkColorTable*) { 121 SkASSERT(width > 0); 122 const SkPMColor* s = (const SkPMColor*)src; 123 do { 124 SkPMColor c = *s++; 125 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 126 SkGetPackedB32(c)); 127 } while (--width != 0); 128} 129 130static void ToColor_S4444_Alpha(SkColor dst[], const void* src, int width, 131 SkColorTable*) { 132 SkASSERT(width > 0); 133 const SkPMColor16* s = (const SkPMColor16*)src; 134 do { 135 *dst++ = SkUnPreMultiply::PMColorToColor(SkPixel4444ToPixel32(*s++)); 136 } while (--width != 0); 137} 138 139static void ToColor_S4444_Opaque(SkColor dst[], const void* src, int width, 140 SkColorTable*) { 141 SkASSERT(width > 0); 142 const SkPMColor* s = (const SkPMColor*)src; 143 do { 144 SkPMColor c = SkPixel4444ToPixel32(*s++); 145 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 146 SkGetPackedB32(c)); 147 } while (--width != 0); 148} 149 150static void ToColor_S565(SkColor dst[], const void* src, int width, 151 SkColorTable*) { 152 SkASSERT(width > 0); 153 const uint16_t* s = (const uint16_t*)src; 154 do { 155 uint16_t c = *s++; 156 *dst++ = SkColorSetRGB(SkPacked16ToR32(c), SkPacked16ToG32(c), 157 SkPacked16ToB32(c)); 158 } while (--width != 0); 159} 160 161static void ToColor_SI8_Alpha(SkColor dst[], const void* src, int width, 162 SkColorTable* ctable) { 163 SkASSERT(width > 0); 164 const uint8_t* s = (const uint8_t*)src; 165 const SkPMColor* colors = ctable->lockColors(); 166 do { 167 *dst++ = SkUnPreMultiply::PMColorToColor(colors[*s++]); 168 } while (--width != 0); 169 ctable->unlockColors(false); 170} 171 172static void ToColor_SI8_Opaque(SkColor dst[], const void* src, int width, 173 SkColorTable* ctable) { 174 SkASSERT(width > 0); 175 const uint8_t* s = (const uint8_t*)src; 176 const SkPMColor* colors = ctable->lockColors(); 177 do { 178 SkPMColor c = colors[*s++]; 179 *dst++ = SkColorSetRGB(SkGetPackedR32(c), SkGetPackedG32(c), 180 SkGetPackedB32(c)); 181 } while (--width != 0); 182 ctable->unlockColors(false); 183} 184 185// can return NULL 186static ToColorProc ChooseToColorProc(const SkBitmap& src) { 187 switch (src.config()) { 188 case SkBitmap::kARGB_8888_Config: 189 return src.isOpaque() ? ToColor_S32_Opaque : ToColor_S32_Alpha; 190 case SkBitmap::kARGB_4444_Config: 191 return src.isOpaque() ? ToColor_S4444_Opaque : ToColor_S4444_Alpha; 192 case SkBitmap::kRGB_565_Config: 193 return ToColor_S565; 194 case SkBitmap::kIndex8_Config: 195 if (src.getColorTable() == NULL) { 196 return NULL; 197 } 198 return src.isOpaque() ? ToColor_SI8_Opaque : ToColor_SI8_Alpha; 199 default: 200 break; 201 } 202 return NULL; 203} 204 205/////////////////////////////////////////////////////////////////////////////// 206/////////////////////////////////////////////////////////////////////////////// 207 208static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors, 209 int offset, int stride, int width, int height, 210 SkBitmap::Config config, jboolean isMutable) { 211 if (width <= 0 || height <= 0) { 212 doThrowIAE(env, "width and height must be > 0"); 213 return NULL; 214 } 215 216 if (NULL != jColors) { 217 size_t n = env->GetArrayLength(jColors); 218 if (n < SkAbs32(stride) * (size_t)height) { 219 doThrowAIOOBE(env); 220 return NULL; 221 } 222 } 223 224 SkBitmap bitmap; 225 226 bitmap.setConfig(config, width, height); 227 if (!GraphicsJNI::setJavaPixelRef(env, &bitmap, NULL)) { 228 return NULL; 229 } 230 231 if (jColors != NULL) { 232 GraphicsJNI::SetPixels(env, jColors, offset, stride, 233 0, 0, width, height, bitmap); 234 } 235 236 return GraphicsJNI::createBitmap(env, new SkBitmap(bitmap), isMutable, 237 NULL); 238} 239 240static jobject Bitmap_copy(JNIEnv* env, jobject, const SkBitmap* src, 241 SkBitmap::Config dstConfig, jboolean isMutable) { 242 SkBitmap result; 243 JavaPixelAllocator allocator(env); 244 245 if (!src->copyTo(&result, dstConfig, &allocator)) { 246 return NULL; 247 } 248 249 return GraphicsJNI::createBitmap(env, new SkBitmap(result), isMutable, 250 NULL); 251} 252 253static void Bitmap_destructor(JNIEnv* env, jobject, SkBitmap* bitmap) { 254 delete bitmap; 255} 256 257static void Bitmap_recycle(JNIEnv* env, jobject, SkBitmap* bitmap) { 258 bitmap->setPixels(NULL, NULL); 259} 260 261// These must match the int values in Bitmap.java 262enum JavaEncodeFormat { 263 kJPEG_JavaEncodeFormat = 0, 264 kPNG_JavaEncodeFormat = 1 265}; 266 267static bool Bitmap_compress(JNIEnv* env, jobject clazz, SkBitmap* bitmap, 268 int format, int quality, 269 jobject jstream, jbyteArray jstorage) { 270 SkImageEncoder::Type fm; 271 272 switch (format) { 273 case kJPEG_JavaEncodeFormat: 274 fm = SkImageEncoder::kJPEG_Type; 275 break; 276 case kPNG_JavaEncodeFormat: 277 fm = SkImageEncoder::kPNG_Type; 278 break; 279 default: 280 return false; 281 } 282 283 bool success = false; 284 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); 285 if (NULL != strm) { 286 SkImageEncoder* encoder = SkImageEncoder::Create(fm); 287 if (NULL != encoder) { 288 success = encoder->encodeStream(strm, *bitmap, quality); 289 delete encoder; 290 } 291 delete strm; 292 } 293 return success; 294} 295 296static void Bitmap_erase(JNIEnv* env, jobject, SkBitmap* bitmap, jint color) { 297 bitmap->eraseColor(color); 298} 299 300static int Bitmap_width(JNIEnv* env, jobject, SkBitmap* bitmap) { 301 return bitmap->width(); 302} 303 304static int Bitmap_height(JNIEnv* env, jobject, SkBitmap* bitmap) { 305 return bitmap->height(); 306} 307 308static int Bitmap_rowBytes(JNIEnv* env, jobject, SkBitmap* bitmap) { 309 return bitmap->rowBytes(); 310} 311 312static int Bitmap_config(JNIEnv* env, jobject, SkBitmap* bitmap) { 313 return bitmap->config(); 314} 315 316static jboolean Bitmap_hasAlpha(JNIEnv* env, jobject, SkBitmap* bitmap) { 317 return !bitmap->isOpaque(); 318} 319 320/////////////////////////////////////////////////////////////////////////////// 321 322static jobject Bitmap_createFromParcel(JNIEnv* env, jobject, jobject parcel) { 323 if (parcel == NULL) { 324 SkDebugf("-------- unparcel parcel is NULL\n"); 325 return NULL; 326 } 327 328 android::Parcel* p = android::parcelForJavaObject(env, parcel); 329 330 const bool isMutable = p->readInt32() != 0; 331 const SkBitmap::Config config = (SkBitmap::Config)p->readInt32(); 332 const int width = p->readInt32(); 333 const int height = p->readInt32(); 334 const int rowBytes = p->readInt32(); 335 336 if (SkBitmap::kARGB_8888_Config != config && 337 SkBitmap::kRGB_565_Config != config && 338 SkBitmap::kARGB_4444_Config != config && 339 SkBitmap::kIndex8_Config != config && 340 SkBitmap::kA8_Config != config) { 341 SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config); 342 return NULL; 343 } 344 345 SkBitmap* bitmap = new SkBitmap; 346 347 bitmap->setConfig(config, width, height, rowBytes); 348 349 SkColorTable* ctable = NULL; 350 if (config == SkBitmap::kIndex8_Config) { 351 int count = p->readInt32(); 352 if (count > 0) { 353 size_t size = count * sizeof(SkPMColor); 354 const SkPMColor* src = (const SkPMColor*)p->readInplace(size); 355 ctable = new SkColorTable(src, count); 356 } 357 } 358 359 if (!GraphicsJNI::setJavaPixelRef(env, bitmap, ctable)) { 360 ctable->safeUnref(); 361 delete bitmap; 362 return NULL; 363 } 364 365 ctable->safeUnref(); 366 367 size_t size = bitmap->getSize(); 368 bitmap->lockPixels(); 369 memcpy(bitmap->getPixels(), p->readInplace(size), size); 370 bitmap->unlockPixels(); 371 372 return GraphicsJNI::createBitmap(env, bitmap, isMutable, NULL); 373} 374 375static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, 376 const SkBitmap* bitmap, 377 jboolean isMutable, jobject parcel) { 378 if (parcel == NULL) { 379 SkDebugf("------- writeToParcel null parcel\n"); 380 return false; 381 } 382 383 android::Parcel* p = android::parcelForJavaObject(env, parcel); 384 385 p->writeInt32(isMutable); 386 p->writeInt32(bitmap->config()); 387 p->writeInt32(bitmap->width()); 388 p->writeInt32(bitmap->height()); 389 p->writeInt32(bitmap->rowBytes()); 390 391 if (bitmap->getConfig() == SkBitmap::kIndex8_Config) { 392 SkColorTable* ctable = bitmap->getColorTable(); 393 if (ctable != NULL) { 394 int count = ctable->count(); 395 p->writeInt32(count); 396 memcpy(p->writeInplace(count * sizeof(SkPMColor)), 397 ctable->lockColors(), count * sizeof(SkPMColor)); 398 ctable->unlockColors(false); 399 } else { 400 p->writeInt32(0); // indicate no ctable 401 } 402 } 403 404 size_t size = bitmap->getSize(); 405 bitmap->lockPixels(); 406 memcpy(p->writeInplace(size), bitmap->getPixels(), size); 407 bitmap->unlockPixels(); 408 return true; 409} 410 411static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz, 412 const SkBitmap* src, const SkPaint* paint, 413 jintArray offsetXY) { 414 SkIPoint offset; 415 SkBitmap* dst = new SkBitmap; 416 417 src->extractAlpha(dst, paint, &offset); 418 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) { 419 int* array = env->GetIntArrayElements(offsetXY, NULL); 420 array[0] = offset.fX; 421 array[1] = offset.fY; 422 env->ReleaseIntArrayElements(offsetXY, array, 0); 423 } 424 425 return GraphicsJNI::createBitmap(env, dst, true, NULL); 426} 427 428/////////////////////////////////////////////////////////////////////////////// 429 430static int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap, 431 int x, int y) { 432 SkAutoLockPixels alp(*bitmap); 433 434 ToColorProc proc = ChooseToColorProc(*bitmap); 435 if (NULL == proc) { 436 return 0; 437 } 438 const void* src = bitmap->getAddr(x, y); 439 if (NULL == src) { 440 return 0; 441 } 442 443 SkColor dst[1]; 444 proc(dst, src, 1, bitmap->getColorTable()); 445 return dst[0]; 446} 447 448static void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap, 449 jintArray pixelArray, int offset, int stride, 450 int x, int y, int width, int height) { 451 SkAutoLockPixels alp(*bitmap); 452 453 ToColorProc proc = ChooseToColorProc(*bitmap); 454 if (NULL == proc) { 455 return; 456 } 457 const void* src = bitmap->getAddr(x, y); 458 if (NULL == src) { 459 return; 460 } 461 462 SkColorTable* ctable = bitmap->getColorTable(); 463 jint* dst = env->GetIntArrayElements(pixelArray, NULL); 464 SkColor* d = (SkColor*)dst + offset; 465 while (--height >= 0) { 466 proc(d, src, width, ctable); 467 d += stride; 468 src = (void*)((const char*)src + bitmap->rowBytes()); 469 } 470 env->ReleaseIntArrayElements(pixelArray, dst, 0); 471} 472 473/////////////////////////////////////////////////////////////////////////////// 474 475static void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap, 476 int x, int y, SkColor color) { 477 SkAutoLockPixels alp(*bitmap); 478 if (NULL == bitmap->getPixels()) { 479 return; 480 } 481 482 FromColorProc proc = ChooseFromColorProc(bitmap->config()); 483 if (NULL == proc) { 484 return; 485 } 486 487 proc(bitmap->getAddr(x, y), &color, 1, x, y); 488} 489 490static void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap, 491 jintArray pixelArray, int offset, int stride, 492 int x, int y, int width, int height) { 493 GraphicsJNI::SetPixels(env, pixelArray, offset, stride, 494 x, y, width, height, *bitmap); 495} 496 497static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject, 498 const SkBitmap* bitmap, jobject jbuffer) { 499 SkAutoLockPixels alp(*bitmap); 500 const void* src = bitmap->getPixels(); 501 502 if (NULL != src) { 503 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE); 504 505 // the java side has already checked that buffer is large enough 506 memcpy(abp.pointer(), src, bitmap->getSize()); 507 } 508} 509 510/////////////////////////////////////////////////////////////////////////////// 511 512#include <android_runtime/AndroidRuntime.h> 513 514static JNINativeMethod gBitmapMethods[] = { 515 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;", 516 (void*)Bitmap_creator }, 517 { "nativeCopy", "(IIZ)Landroid/graphics/Bitmap;", 518 (void*)Bitmap_copy }, 519 { "nativeDestructor", "(I)V", (void*)Bitmap_destructor }, 520 { "nativeRecycle", "(I)V", (void*)Bitmap_recycle }, 521 { "nativeCompress", "(IIILjava/io/OutputStream;[B)Z", 522 (void*)Bitmap_compress }, 523 { "nativeErase", "(II)V", (void*)Bitmap_erase }, 524 { "nativeWidth", "(I)I", (void*)Bitmap_width }, 525 { "nativeHeight", "(I)I", (void*)Bitmap_height }, 526 { "nativeRowBytes", "(I)I", (void*)Bitmap_rowBytes }, 527 { "nativeConfig", "(I)I", (void*)Bitmap_config }, 528 { "nativeHasAlpha", "(I)Z", (void*)Bitmap_hasAlpha }, 529 { "nativeCreateFromParcel", 530 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;", 531 (void*)Bitmap_createFromParcel }, 532 { "nativeWriteToParcel", "(IZLandroid/os/Parcel;)Z", 533 (void*)Bitmap_writeToParcel }, 534 { "nativeExtractAlpha", "(II[I)Landroid/graphics/Bitmap;", 535 (void*)Bitmap_extractAlpha }, 536 { "nativeGetPixel", "(III)I", (void*)Bitmap_getPixel }, 537 { "nativeGetPixels", "(I[IIIIIII)V", (void*)Bitmap_getPixels }, 538 { "nativeSetPixel", "(IIII)V", (void*)Bitmap_setPixel }, 539 { "nativeSetPixels", "(I[IIIIIII)V", (void*)Bitmap_setPixels }, 540 { "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V", 541 (void*)Bitmap_copyPixelsToBuffer } 542}; 543 544#define kClassPathName "android/graphics/Bitmap" 545 546int register_android_graphics_Bitmap(JNIEnv* env); 547int register_android_graphics_Bitmap(JNIEnv* env) 548{ 549#if 1 550 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, 551 gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods)); 552#else 553 short n = 0; 554 int limit = (char*)env - (char*)0; 555 for (int i = 0; i < limit; i++) { 556 n += i*i; 557 } 558 return n; 559#endif 560} 561 562