| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The Android Open Source Project | 2 * Copyright 2015 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkData.h" | 8 #include "SkData.h" |
| 9 #include "SkImageDecoder.h" | 9 #include "SkCodecImageGenerator.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) 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, nullptr, nullp
tr); | |
| 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 : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data)) | |
| 43 {} | |
| 44 | |
| 45 protected: | |
| 46 SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override { | |
| 47 return SkRef(fData.get()); | |
| 48 } | |
| 49 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, | |
| 50 SkPMColor ctableEntries[], int* ctableCount) override { | |
| 51 SkMemoryStream stream(fData->data(), fData->size(), false); | |
| 52 SkAutoTUnref<BareMemoryAllocator> allocator( | |
| 53 new BareMemoryAllocator(info, pixels, rowBytes)); | |
| 54 fDecoder->setAllocator(allocator); | |
| 55 fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.
alphaType()); | |
| 56 | |
| 57 SkBitmap bm; | |
| 58 const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, inf
o.colorType(), | |
| 59 SkImageDecoder::k
DecodePixels_Mode); | |
| 60 if (SkImageDecoder::kFailure == result) { | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 SkASSERT(info.colorType() == bm.info().colorType()); | |
| 65 | |
| 66 if (kIndex_8_SkColorType == info.colorType()) { | |
| 67 SkASSERT(ctableEntries); | |
| 68 | |
| 69 SkColorTable* ctable = bm.getColorTable(); | |
| 70 if (nullptr == ctable) { | |
| 71 return false; | |
| 72 } | |
| 73 const int count = ctable->count(); | |
| 74 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor
)); | |
| 75 *ctableCount = count; | |
| 76 } | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], | |
| 81 SkYUVColorSpace* colorSpace) override { | |
| 82 SkMemoryStream stream(fData->data(), fData->size(), false); | |
| 83 return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colo
rSpace); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 typedef SkImageGenerator INHERITED; | |
| 88 }; | |
| 89 | 10 |
| 90 SkImageGenerator* SkImageGenerator::NewFromEncodedImpl(SkData* data) { | 11 SkImageGenerator* SkImageGenerator::NewFromEncodedImpl(SkData* data) { |
| 91 SkMemoryStream stream(data->data(), data->size(), false); | 12 return SkCodecImageGenerator::NewFromEncodedCodec(data); |
| 92 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); | |
| 93 if (nullptr == decoder) { | |
| 94 return nullptr; | |
| 95 } | |
| 96 | |
| 97 SkBitmap bm; | |
| 98 stream.rewind(); | |
| 99 if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDe
codeBounds_Mode)) { | |
| 100 delete decoder; | |
| 101 return nullptr; | |
| 102 } | |
| 103 | |
| 104 return new SkImageDecoderGenerator(bm.info(), decoder, data); | |
| 105 } | 13 } |
| OLD | NEW |