1 /*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkData.h"
9 #include "SkImageDecoder.h"
10 #include "SkImageGenerator.h"
11 #include "SkStream.h"
12
13 class BareMemoryAllocator : public SkBitmap::Allocator {
14 const SkImageInfo fInfo;
15 void* const fMemory;
16 const size_t fRowBytes;
17
18 public:
BareMemoryAllocator(const SkImageInfo & info,void * memory,size_t rowBytes)19 BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes)
20 : fInfo(info), fMemory(memory), fRowBytes(rowBytes)
21 {}
22
23 protected:
allocPixelRef(SkBitmap * bm,SkColorTable * ctable)24 bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) override {
25 const SkImageInfo bmi = bm->info();
26 if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() ||
27 bmi.colorType() != fInfo.colorType())
28 {
29 return false;
30 }
31 return bm->installPixels(bmi, fMemory, fRowBytes, ctable, NULL, NULL);
32 }
33 };
34
35 class SkImageDecoderGenerator : public SkImageGenerator {
36 const SkImageInfo fInfo;
37 SkAutoTDelete<SkImageDecoder> fDecoder;
38 SkAutoTUnref<SkData> fData;
39
40 public:
SkImageDecoderGenerator(const SkImageInfo & info,SkImageDecoder * decoder,SkData * data)41 SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, SkData* data)
42 : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data))
43 {}
44
45 protected:
onRefEncodedData()46 SkData* onRefEncodedData() override {
47 return SkRef(fData.get());
48 }
49
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options &,SkPMColor ctableEntries[],int * ctableCount)50 virtual Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
51 const Options&,
52 SkPMColor ctableEntries[], int* ctableCount) override {
53 SkMemoryStream stream(fData->data(), fData->size(), false);
54 SkAutoTUnref<BareMemoryAllocator> allocator(SkNEW_ARGS(BareMemoryAllocator,
55 (info, pixels, rowBytes)));
56 fDecoder->setAllocator(allocator);
57 fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.alphaType());
58
59 SkBitmap bm;
60 const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colorType(),
61 SkImageDecoder::kDecodePixels_Mode);
62 if (SkImageDecoder::kFailure == result) {
63 return kInvalidInput;
64 }
65
66 SkASSERT(info.colorType() == bm.info().colorType());
67
68 if (kIndex_8_SkColorType == info.colorType()) {
69 SkASSERT(ctableEntries);
70
71 SkColorTable* ctable = bm.getColorTable();
72 if (NULL == ctable) {
73 return kInvalidConversion;
74 }
75 const int count = ctable->count();
76 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor));
77 *ctableCount = count;
78 }
79 if (SkImageDecoder::kPartialSuccess == result) {
80 return kIncompleteInput;
81 }
82 return kSuccess;
83 }
84
onGetYUV8Planes(SkISize sizes[3],void * planes[3],size_t rowBytes[3],SkYUVColorSpace * colorSpace)85 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
86 SkYUVColorSpace* colorSpace) override {
87 SkMemoryStream stream(fData->data(), fData->size(), false);
88 return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colorSpace);
89 }
90
91 private:
92 typedef SkImageGenerator INHERITED;
93 };
94
NewFromData(SkData * data)95 SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) {
96 if (NULL == data) {
97 return NULL;
98 }
99
100 SkMemoryStream stream(data->data(), data->size(), false);
101 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream);
102 if (NULL == decoder) {
103 return NULL;
104 }
105
106 SkBitmap bm;
107 stream.rewind();
108 if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDecodeBounds_Mode)) {
109 SkDELETE(decoder);
110 return NULL;
111 }
112
113 return SkNEW_ARGS(SkImageDecoderGenerator, (bm.info(), decoder, data));
114 }
115