Bitmap.cpp revision de0dfb7b65a02d4dd74c271b558adee0973fc267
1#include "SkBitmap.h" 2#include "SkImageEncoder.h" 3#include "SkColorPriv.h" 4#include "GraphicsJNI.h" 5#include "SkDither.h" 6#include "SkUnPreMultiply.h" 7 8#include <binder/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, true)) { 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, true); 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 const int density = p->readInt32(); 336 337 if (SkBitmap::kARGB_8888_Config != config && 338 SkBitmap::kRGB_565_Config != config && 339 SkBitmap::kARGB_4444_Config != config && 340 SkBitmap::kIndex8_Config != config && 341 SkBitmap::kA8_Config != config) { 342 SkDebugf("Bitmap_createFromParcel unknown config: %d\n", config); 343 return NULL; 344 } 345 346 SkBitmap* bitmap = new SkBitmap; 347 348 bitmap->setConfig(config, width, height, rowBytes); 349 350 SkColorTable* ctable = NULL; 351 if (config == SkBitmap::kIndex8_Config) { 352 int count = p->readInt32(); 353 if (count > 0) { 354 size_t size = count * sizeof(SkPMColor); 355 const SkPMColor* src = (const SkPMColor*)p->readInplace(size); 356 ctable = new SkColorTable(src, count); 357 } 358 } 359 360 if (!GraphicsJNI::setJavaPixelRef(env, bitmap, ctable, true)) { 361 ctable->safeUnref(); 362 delete bitmap; 363 return NULL; 364 } 365 366 ctable->safeUnref(); 367 368 size_t size = bitmap->getSize(); 369 bitmap->lockPixels(); 370 memcpy(bitmap->getPixels(), p->readInplace(size), size); 371 bitmap->unlockPixels(); 372 373 return GraphicsJNI::createBitmap(env, bitmap, isMutable, NULL, density); 374} 375 376static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, 377 const SkBitmap* bitmap, 378 jboolean isMutable, jint density, 379 jobject parcel) { 380 if (parcel == NULL) { 381 SkDebugf("------- writeToParcel null parcel\n"); 382 return false; 383 } 384 385 android::Parcel* p = android::parcelForJavaObject(env, parcel); 386 387 p->writeInt32(isMutable); 388 p->writeInt32(bitmap->config()); 389 p->writeInt32(bitmap->width()); 390 p->writeInt32(bitmap->height()); 391 p->writeInt32(bitmap->rowBytes()); 392 p->writeInt32(density); 393 394 if (bitmap->getConfig() == SkBitmap::kIndex8_Config) { 395 SkColorTable* ctable = bitmap->getColorTable(); 396 if (ctable != NULL) { 397 int count = ctable->count(); 398 p->writeInt32(count); 399 memcpy(p->writeInplace(count * sizeof(SkPMColor)), 400 ctable->lockColors(), count * sizeof(SkPMColor)); 401 ctable->unlockColors(false); 402 } else { 403 p->writeInt32(0); // indicate no ctable 404 } 405 } 406 407 size_t size = bitmap->getSize(); 408 bitmap->lockPixels(); 409 memcpy(p->writeInplace(size), bitmap->getPixels(), size); 410 bitmap->unlockPixels(); 411 return true; 412} 413 414static jobject Bitmap_extractAlpha(JNIEnv* env, jobject clazz, 415 const SkBitmap* src, const SkPaint* paint, 416 jintArray offsetXY) { 417 SkIPoint offset; 418 SkBitmap* dst = new SkBitmap; 419 420 src->extractAlpha(dst, paint, &offset); 421 if (offsetXY != 0 && env->GetArrayLength(offsetXY) >= 2) { 422 int* array = env->GetIntArrayElements(offsetXY, NULL); 423 array[0] = offset.fX; 424 array[1] = offset.fY; 425 env->ReleaseIntArrayElements(offsetXY, array, 0); 426 } 427 428 return GraphicsJNI::createBitmap(env, dst, true, NULL); 429} 430 431/////////////////////////////////////////////////////////////////////////////// 432 433static int Bitmap_getPixel(JNIEnv* env, jobject, const SkBitmap* bitmap, 434 int x, int y) { 435 SkAutoLockPixels alp(*bitmap); 436 437 ToColorProc proc = ChooseToColorProc(*bitmap); 438 if (NULL == proc) { 439 return 0; 440 } 441 const void* src = bitmap->getAddr(x, y); 442 if (NULL == src) { 443 return 0; 444 } 445 446 SkColor dst[1]; 447 proc(dst, src, 1, bitmap->getColorTable()); 448 return dst[0]; 449} 450 451static void Bitmap_getPixels(JNIEnv* env, jobject, const SkBitmap* bitmap, 452 jintArray pixelArray, int offset, int stride, 453 int x, int y, int width, int height) { 454 SkAutoLockPixels alp(*bitmap); 455 456 ToColorProc proc = ChooseToColorProc(*bitmap); 457 if (NULL == proc) { 458 return; 459 } 460 const void* src = bitmap->getAddr(x, y); 461 if (NULL == src) { 462 return; 463 } 464 465 SkColorTable* ctable = bitmap->getColorTable(); 466 jint* dst = env->GetIntArrayElements(pixelArray, NULL); 467 SkColor* d = (SkColor*)dst + offset; 468 while (--height >= 0) { 469 proc(d, src, width, ctable); 470 d += stride; 471 src = (void*)((const char*)src + bitmap->rowBytes()); 472 } 473 env->ReleaseIntArrayElements(pixelArray, dst, 0); 474} 475 476/////////////////////////////////////////////////////////////////////////////// 477 478static void Bitmap_setPixel(JNIEnv* env, jobject, const SkBitmap* bitmap, 479 int x, int y, SkColor color) { 480 SkAutoLockPixels alp(*bitmap); 481 if (NULL == bitmap->getPixels()) { 482 return; 483 } 484 485 FromColorProc proc = ChooseFromColorProc(bitmap->config()); 486 if (NULL == proc) { 487 return; 488 } 489 490 proc(bitmap->getAddr(x, y), &color, 1, x, y); 491} 492 493static void Bitmap_setPixels(JNIEnv* env, jobject, const SkBitmap* bitmap, 494 jintArray pixelArray, int offset, int stride, 495 int x, int y, int width, int height) { 496 GraphicsJNI::SetPixels(env, pixelArray, offset, stride, 497 x, y, width, height, *bitmap); 498} 499 500static void Bitmap_copyPixelsToBuffer(JNIEnv* env, jobject, 501 const SkBitmap* bitmap, jobject jbuffer) { 502 SkAutoLockPixels alp(*bitmap); 503 const void* src = bitmap->getPixels(); 504 505 if (NULL != src) { 506 android::AutoBufferPointer abp(env, jbuffer, JNI_TRUE); 507 508 // the java side has already checked that buffer is large enough 509 memcpy(abp.pointer(), src, bitmap->getSize()); 510 } 511} 512 513static void Bitmap_copyPixelsFromBuffer(JNIEnv* env, jobject, 514 const SkBitmap* bitmap, jobject jbuffer) { 515 SkAutoLockPixels alp(*bitmap); 516 void* dst = bitmap->getPixels(); 517 518 if (NULL != dst) { 519 android::AutoBufferPointer abp(env, jbuffer, JNI_FALSE); 520 // the java side has already checked that buffer is large enough 521 memcpy(dst, abp.pointer(), bitmap->getSize()); 522 } 523} 524 525static void Bitmap_prepareToDraw(JNIEnv* env, jobject, SkBitmap* bitmap) { 526 bitmap->lockPixels(); 527 bitmap->unlockPixels(); 528} 529 530/////////////////////////////////////////////////////////////////////////////// 531 532#include <android_runtime/AndroidRuntime.h> 533 534static JNINativeMethod gBitmapMethods[] = { 535 { "nativeCreate", "([IIIIIIZ)Landroid/graphics/Bitmap;", 536 (void*)Bitmap_creator }, 537 { "nativeCopy", "(IIZ)Landroid/graphics/Bitmap;", 538 (void*)Bitmap_copy }, 539 { "nativeDestructor", "(I)V", (void*)Bitmap_destructor }, 540 { "nativeRecycle", "(I)V", (void*)Bitmap_recycle }, 541 { "nativeCompress", "(IIILjava/io/OutputStream;[B)Z", 542 (void*)Bitmap_compress }, 543 { "nativeErase", "(II)V", (void*)Bitmap_erase }, 544 { "nativeWidth", "(I)I", (void*)Bitmap_width }, 545 { "nativeHeight", "(I)I", (void*)Bitmap_height }, 546 { "nativeRowBytes", "(I)I", (void*)Bitmap_rowBytes }, 547 { "nativeConfig", "(I)I", (void*)Bitmap_config }, 548 { "nativeHasAlpha", "(I)Z", (void*)Bitmap_hasAlpha }, 549 { "nativeCreateFromParcel", 550 "(Landroid/os/Parcel;)Landroid/graphics/Bitmap;", 551 (void*)Bitmap_createFromParcel }, 552 { "nativeWriteToParcel", "(IZILandroid/os/Parcel;)Z", 553 (void*)Bitmap_writeToParcel }, 554 { "nativeExtractAlpha", "(II[I)Landroid/graphics/Bitmap;", 555 (void*)Bitmap_extractAlpha }, 556 { "nativeGetPixel", "(III)I", (void*)Bitmap_getPixel }, 557 { "nativeGetPixels", "(I[IIIIIII)V", (void*)Bitmap_getPixels }, 558 { "nativeSetPixel", "(IIII)V", (void*)Bitmap_setPixel }, 559 { "nativeSetPixels", "(I[IIIIIII)V", (void*)Bitmap_setPixels }, 560 { "nativeCopyPixelsToBuffer", "(ILjava/nio/Buffer;)V", 561 (void*)Bitmap_copyPixelsToBuffer }, 562 { "nativeCopyPixelsFromBuffer", "(ILjava/nio/Buffer;)V", 563 (void*)Bitmap_copyPixelsFromBuffer }, 564 { "nativePrepareToDraw", "(I)V", (void*)Bitmap_prepareToDraw } 565}; 566 567#define kClassPathName "android/graphics/Bitmap" 568 569int register_android_graphics_Bitmap(JNIEnv* env); 570int register_android_graphics_Bitmap(JNIEnv* env) 571{ 572 return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, 573 gBitmapMethods, SK_ARRAY_COUNT(gBitmapMethods)); 574} 575 576