OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 The Android Open Source Project | |
hal.canary
2015/01/06 20:06:40
* Copyright 2015 Google Inc.
| |
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: | |
19 BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes) | |
20 : fInfo(info), fMemory(memory), fRowBytes(rowBytes) | |
21 {} | |
22 | |
23 protected: | |
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()) | |
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: | |
41 SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, Sk Data* data) | |
42 : fInfo(info), fDecoder(decoder), fData(SkRef(data)) | |
43 {} | |
44 | |
45 protected: | |
46 SkData* onRefEncodedData() SK_OVERRIDE { | |
47 return SkRef(fData.get()); | |
48 } | |
49 | |
50 virtual bool onGetInfo(SkImageInfo* info) { | |
51 *info = fInfo; | |
52 return true; | |
53 } | |
54 | |
55 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBy tes, | |
56 SkPMColor ctableEntries[], int* ctableCount) { | |
57 SkMemoryStream stream(fData->data(), fData->size(), false); | |
58 SkAutoTUnref<BareMemoryAllocator> allocator(SkNEW_ARGS(BareMemoryAllocat or, | |
scroggo
2015/01/06 19:50:15
You could put the BareMemoryAllocator on the stack
| |
59 (info, pixels, ro wBytes))); | |
60 fDecoder->setAllocator(allocator); | |
61 fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info. alphaType()); | |
62 | |
63 SkBitmap bm; | |
64 SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, info.colo rType(), | |
65 SkImageDecoder::kDecode Pixels_Mode); | |
66 switch (result) { | |
67 case SkImageDecoder::kSuccess: | |
68 break; | |
69 default: | |
70 return false; | |
71 } | |
72 | |
73 SkASSERT(info.colorType() == bm.info().colorType()); | |
74 | |
75 if (kIndex_8_SkColorType == info.colorType()) { | |
76 SkASSERT(ctableEntries); | |
77 | |
78 SkColorTable* ctable = bm.getColorTable(); | |
79 if (NULL == ctable) { | |
80 return false; | |
81 } | |
82 const int count = ctable->count(); | |
83 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor )); | |
84 *ctableCount = count; | |
85 } | |
86 return true; | |
87 } | |
88 | |
89 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], | |
90 SkYUVColorSpace* colorSpace) SK_OVERRIDE { | |
91 SkMemoryStream stream(fData->data(), fData->size(), false); | |
hal.canary
2015/01/06 20:06:40
SkMemoryStream stream(fData);
is simpler.
| |
92 return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colo rSpace); | |
93 } | |
94 | |
95 }; | |
96 | |
97 SkImageGenerator* SkImageGenerator::NewFromData(SkData* data) { | |
98 if (NULL == data) { | |
99 return NULL; | |
100 } | |
101 | |
102 SkMemoryStream stream(data->data(), data->size(), false); | |
103 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); | |
104 if (NULL == decoder) { | |
105 return NULL; | |
106 } | |
107 | |
108 SkBitmap bm; | |
109 stream.rewind(); | |
110 if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDe codeBounds_Mode)) { | |
111 SkDELETE(decoder); | |
112 return NULL; | |
113 } | |
114 | |
115 return SkNEW_ARGS(SkImageDecoderGenerator, (bm.info(), decoder, data)); | |
116 } | |
OLD | NEW |