virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options&, SkPMColor ctableEntries[], int* ctableCount) override { SkMemoryStream stream(fData->data(), fData->size(), false); SkAutoTUnref<BareMemoryAllocator> allocator(SkNEW_ARGS(BareMemoryAllocator, (info, pixels, rowBytes))); fDecoder->setAllocator(allocator); fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType()); SkBitmap bm; const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(), SkImageDecoder::kDecodePixels_Mode); if (SkImageDecoder::kFailure == result) { return kInvalidInput; } SkASSERT(info.colorType() == bm.info().colorType()); if (kIndex_8_SkColorType == info.colorType()) { SkASSERT(ctableEntries); SkColorTable* ctable = bm.getColorTable(); if (NULL == ctable) { return kInvalidConversion; } const int count = ctable->count(); memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor)); *ctableCount = count; } if (SkImageDecoder::kPartialSuccess == result) { return kIncompleteInput; } return kSuccess; }
/* Fill out buffer with the compressed format Ganesh expects from a colortable based bitmap. [palette (colortable) + indices]. At the moment Ganesh only supports 8bit version. If Ganesh allowed we others we could detect that the colortable.count is <= 16, and then repack the indices as nibbles to save RAM, but it would take more time (i.e. a lot slower than memcpy), so skipping that for now. Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big as the colortable.count says it is. */ static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); SkAutoLockPixels apl(bitmap); if (!bitmap.readyToDraw()) { SkASSERT(!"bitmap not ready to draw!"); return; } SkColorTable* ctable = bitmap.getColorTable(); char* dst = (char*)buffer; memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor)); ctable->unlockColors(false); // always skip a full 256 number of entries, even if we memcpy'd fewer dst += kGrColorTableSize; if (bitmap.width() == bitmap.rowBytes()) { memcpy(dst, bitmap.getPixels(), bitmap.getSize()); } else { // need to trim off the extra bytes per row size_t width = bitmap.width(); size_t rowBytes = bitmap.rowBytes(); const char* src = (const char*)bitmap.getPixels(); for (int y = 0; y < bitmap.height(); y++) { memcpy(dst, src, width); src += rowBytes; dst += width; } } }
// As copy constructor is hidden in the class hierarchy, we need to call // default constructor explicitly to suppress a compiler warning. SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { f16BitCache = NULL; fAlphaType = src.fAlphaType; int count = src.count(); fCount = SkToU16(count); fColors = reinterpret_cast<SkPMColor*>( sk_malloc_throw(count * sizeof(SkPMColor))); memcpy(fColors, src.fColors, count * sizeof(SkPMColor)); SkDEBUGCODE(fColorLockCount = 0;)
static jboolean Bitmap_writeToParcel(JNIEnv* env, jobject, jlong bitmapHandle, jboolean isMutable, jint density, jobject parcel) { const SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); if (parcel == NULL) { SkDebugf("------- writeToParcel null parcel\n"); return JNI_FALSE; } android::Parcel* p = android::parcelForJavaObject(env, parcel); p->writeInt32(isMutable); p->writeInt32(bitmap->colorType()); p->writeInt32(bitmap->alphaType()); p->writeInt32(bitmap->width()); p->writeInt32(bitmap->height()); p->writeInt32(bitmap->rowBytes()); p->writeInt32(density); if (bitmap->colorType() == kIndex_8_SkColorType) { SkColorTable* ctable = bitmap->getColorTable(); if (ctable != NULL) { int count = ctable->count(); p->writeInt32(count); memcpy(p->writeInplace(count * sizeof(SkPMColor)), ctable->lockColors(), count * sizeof(SkPMColor)); ctable->unlockColors(); } else { p->writeInt32(0); // indicate no ctable } } size_t size = bitmap->getSize(); android::Parcel::WritableBlob blob; android::status_t status = p->writeBlob(size, &blob); if (status) { doThrowRE(env, "Could not write bitmap to parcel blob."); return JNI_FALSE; } bitmap->lockPixels(); const void* pSrc = bitmap->getPixels(); if (pSrc == NULL) { memset(blob.data(), 0, size); } else { memcpy(blob.data(), pSrc, size); } bitmap->unlockPixels(); blob.release(); return JNI_TRUE; }
/* Fill out buffer with the compressed format GL expects from a colortable based bitmap. [palette (colortable) + indices]. At the moment I always take the 8bit version, since that's what my data is. I could detect that the colortable.count is <= 16, and then repack the indices as nibbles to save RAM, but it would take more time (i.e. a lot slower than memcpy), so I'm skipping that for now. GL wants a full 256 palette entry, even though my ctable is only as big as the colortable.count says it is. I presume it is OK to leave any trailing entries uninitialized, since none of my indices should exceed ctable->count(). */ static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); SkColorTable* ctable = bitmap.getColorTable(); uint8_t* dst = (uint8_t*)buffer; memcpy(dst, ctable->lockColors(), ctable->count() * sizeof(SkPMColor)); ctable->unlockColors(false); // always skip a full 256 number of entries, even if we memcpy'd fewer dst += SK_GL_SIZE_OF_PALETTE; memcpy(dst, bitmap.getPixels(), bitmap.getSize()); }
/* Fill out buffer with the compressed format Ganesh expects from a colortable based bitmap. [palette (colortable) + indices]. At the moment Ganesh only supports 8bit version. If Ganesh allowed we others we could detect that the colortable.count is <= 16, and then repack the indices as nibbles to save RAM, but it would take more time (i.e. a lot slower than memcpy), so skipping that for now. Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big as the colortable.count says it is. */ static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); SkAutoLockPixels alp(bitmap); if (!bitmap.readyToDraw()) { SkDEBUGFAIL("bitmap not ready to draw!"); return; } SkColorTable* ctable = bitmap.getColorTable(); char* dst = (char*)buffer; const int count = ctable->count(); SkDstPixelInfo dstPI; dstPI.fColorType = kRGBA_8888_SkColorType; dstPI.fAlphaType = kPremul_SkAlphaType; dstPI.fPixels = buffer; dstPI.fRowBytes = count * sizeof(SkPMColor); SkSrcPixelInfo srcPI; srcPI.fColorType = kPMColor_SkColorType; srcPI.fAlphaType = kPremul_SkAlphaType; srcPI.fPixels = ctable->lockColors(); srcPI.fRowBytes = count * sizeof(SkPMColor); srcPI.convertPixelsTo(&dstPI, count, 1); ctable->unlockColors(); // always skip a full 256 number of entries, even if we memcpy'd fewer dst += kGrColorTableSize; if ((unsigned)bitmap.width() == bitmap.rowBytes()) { memcpy(dst, bitmap.getPixels(), bitmap.getSize()); } else { // need to trim off the extra bytes per row size_t width = bitmap.width(); size_t rowBytes = bitmap.rowBytes(); const char* src = (const char*)bitmap.getPixels(); for (int y = 0; y < bitmap.height(); y++) { memcpy(dst, src, width); src += rowBytes; dst += width; } } }
/* Fill out buffer with the compressed format Ganesh expects from a colortable based bitmap. [palette (colortable) + indices]. At the moment Ganesh only supports 8bit version. If Ganesh allowed we others we could detect that the colortable.count is <= 16, and then repack the indices as nibbles to save RAM, but it would take more time (i.e. a lot slower than memcpy), so skipping that for now. Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big as the colortable.count says it is. */ static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); SkAutoLockPixels alp(bitmap); if (!bitmap.readyToDraw()) { SkDEBUGFAIL("bitmap not ready to draw!"); return; } SkColorTable* ctable = bitmap.getColorTable(); char* dst = (char*)buffer; uint32_t* colorTableDst = reinterpret_cast<uint32_t*>(dst); const uint32_t* colorTableSrc = reinterpret_cast<const uint32_t*>(ctable->lockColors()); SkConvertConfig8888Pixels(colorTableDst, 0, SkCanvas::kRGBA_Premul_Config8888, colorTableSrc, 0, SkCanvas::kNative_Premul_Config8888, ctable->count(), 1); ctable->unlockColors(); // always skip a full 256 number of entries, even if we memcpy'd fewer dst += kGrColorTableSize; if ((unsigned)bitmap.width() == bitmap.rowBytes()) { memcpy(dst, bitmap.getPixels(), bitmap.getSize()); } else { // need to trim off the extra bytes per row size_t width = bitmap.width(); size_t rowBytes = bitmap.rowBytes(); const char* src = (const char*)bitmap.getPixels(); for (int y = 0; y < bitmap.height(); y++) { memcpy(dst, src, width); src += rowBytes; dst += width; } } }
QImage toQImage(const SkBitmap &bitmap) { QImage image; switch (bitmap.colorType()) { case kUnknown_SkColorType: break; case kAlpha_8_SkColorType: image = toQImage(bitmap, QImage::Format_Alpha8); break; case kRGB_565_SkColorType: image = toQImage(bitmap, QImage::Format_RGB16); break; case kARGB_4444_SkColorType: switch (bitmap.alphaType()) { case kUnknown_SkAlphaType: break; case kUnpremul_SkAlphaType: // not supported - treat as opaque case kOpaque_SkAlphaType: image = toQImage(bitmap, QImage::Format_RGB444); break; case kPremul_SkAlphaType: image = toQImage(bitmap, QImage::Format_ARGB4444_Premultiplied); break; } break; case kRGBA_8888_SkColorType: switch (bitmap.alphaType()) { case kUnknown_SkAlphaType: break; case kOpaque_SkAlphaType: image = toQImage(bitmap, QImage::Format_RGBX8888); break; case kPremul_SkAlphaType: image = toQImage(bitmap, QImage::Format_RGBA8888_Premultiplied); break; case kUnpremul_SkAlphaType: image = toQImage(bitmap, QImage::Format_RGBA8888); break; } break; case kBGRA_8888_SkColorType: // we are assuming little-endian arch here. switch (bitmap.alphaType()) { case kUnknown_SkAlphaType: break; case kOpaque_SkAlphaType: image = toQImage(bitmap, QImage::Format_RGB32); break; case kPremul_SkAlphaType: image = toQImage(bitmap, QImage::Format_ARGB32_Premultiplied); break; case kUnpremul_SkAlphaType: image = toQImage(bitmap, QImage::Format_ARGB32); break; } break; case kIndex_8_SkColorType: { image = toQImage(bitmap, QImage::Format_Indexed8); SkColorTable *skTable = bitmap.getColorTable(); if (skTable) { QVector<QRgb> qTable(skTable->count()); for (int i = 0; i < skTable->count(); ++i) qTable[i] = (*skTable)[i]; image.setColorTable(qTable); } break; } case kGray_8_SkColorType: image = toQImage(bitmap, QImage::Format_Grayscale8); break; } return image; }