2 * Copyright 2015 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkImageDecoder.h"
10 #include "SkImageGenerator.h"
13 class BareMemoryAllocator : public SkBitmap::Allocator {
14 const SkImageInfo fInfo;
16 const size_t fRowBytes;
19 BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes)
20 : fInfo(info), fMemory(memory), fRowBytes(rowBytes)
24 bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) SK_OVERRIDE {
25 const SkImageInfo bmi = bm->info();
26 if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() ||
27 bmi.colorType() != fInfo.colorType())
31 return bm->installPixels(bmi, fMemory, fRowBytes, ctable, NULL, NULL);
35 class SkImageDecoderGenerator : public SkImageGenerator {
36 const SkImageInfo fInfo;
37 SkAutoTDelete<SkImageDecoder> fDecoder;
38 SkAutoTUnref<SkData> fData;
41 SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data)
42 : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data))
46 SkData* onRefEncodedData() SK_OVERRIDE {
47 return SkRef(fData.get());
50 #ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
51 virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
57 virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
59 SkPMColor ctableEntries[], int* ctableCount) SK_OVERRIDE {
60 SkMemoryStream stream(fData->data(), fData->size(), false);
61 SkAutoTUnref<BareMemoryAllocator> allocator(SkNEW_ARGS(BareMemoryAllocator,
62 (info, pixels, rowBytes)));
63 fDecoder->setAllocator(allocator);
64 fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType());
67 const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(),
68 SkImageDecoder::kDecodePixels_Mode);
69 if (SkImageDecoder::kFailure == result) {
73 SkASSERT(info.colorType() == bm.info().colorType());
75 if (kIndex_8_SkColorType == info.colorType()) {
76 SkASSERT(ctableEntries);
78 SkColorTable* ctable = bm.getColorTable();
80 return kInvalidConversion;
82 const int count = ctable->count();
83 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor));
86 if (SkImageDecoder::kPartialSuccess == result) {
87 return kIncompleteInput;
92 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
93 SkYUVColorSpace* colorSpace) SK_OVERRIDE {
94 SkMemoryStream stream(fData->data(), fData->size(), false);
95 return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace);
99 typedef SkImageGenerator INHERITED;
102 SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) {
107 SkMemoryStream stream(data->data(), data->size(), false);
108 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream);
109 if (NULL == decoder) {
115 if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDecodeBounds_Mode)) {
120 return SkNEW_ARGS(SkImageDecoderGenerator, (bm.info(), decoder, data));