1 /*
2 * Copyright 2014 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 "SkColorPriv.h"
9 #include "SkImageDecoder.h"
10 #include "SkScaledBitmapSampler.h"
11 #include "SkStream.h"
12 #include "SkStreamHelpers.h"
13 #include "SkTypes.h"
14
15 #include "etc1.h"
16
17 class SkPKMImageDecoder : public SkImageDecoder {
18 public:
SkPKMImageDecoder()19 SkPKMImageDecoder() { }
20
getFormat() const21 virtual Format getFormat() const SK_OVERRIDE {
22 return kPKM_Format;
23 }
24
25 protected:
26 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
27
28 private:
29 typedef SkImageDecoder INHERITED;
30 };
31
32 /////////////////////////////////////////////////////////////////////////////////////////
33
onDecode(SkStream * stream,SkBitmap * bm,Mode mode)34 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
35 SkAutoMalloc autoMal;
36 const size_t length = CopyStreamToStorage(&autoMal, stream);
37 if (0 == length) {
38 return false;
39 }
40
41 unsigned char* buf = (unsigned char*)autoMal.get();
42
43 // Make sure original PKM header is there...
44 SkASSERT(etc1_pkm_is_valid(buf));
45
46 const unsigned short width = etc1_pkm_get_width(buf);
47 const unsigned short height = etc1_pkm_get_height(buf);
48
49 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
50 // should we allow the Chooser (if present) to pick a config for us???
51 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) {
52 return false;
53 }
54 #endif
55
56 // Setup the sampler...
57 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
58
59 // Set the config...
60 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(),
61 kOpaque_SkAlphaType));
62 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
63 return true;
64 }
65
66 if (!this->allocPixelRef(bm, NULL)) {
67 return false;
68 }
69
70 // Lock the pixels, since we're about to write to them...
71 SkAutoLockPixels alp(*bm);
72
73 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
74 return false;
75 }
76
77 // Advance buffer past the header
78 buf += ETC_PKM_HEADER_SIZE;
79
80 // ETC1 Data is encoded as RGB pixels, so we should extract it as such
81 int nPixels = width * height;
82 SkAutoMalloc outRGBData(nPixels * 3);
83 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get());
84
85 // Decode ETC1
86 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
87 return false;
88 }
89
90 // Set each of the pixels...
91 const int srcRowBytes = width * 3;
92 const int dstHeight = sampler.scaledHeight();
93 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr);
94 srcRow += sampler.srcY0() * srcRowBytes;
95 for (int y = 0; y < dstHeight; ++y) {
96 sampler.next(srcRow);
97 srcRow += sampler.srcDY() * srcRowBytes;
98 }
99
100 return true;
101 }
102
103 /////////////////////////////////////////////////////////////////////////////////////////
104 DEFINE_DECODER_CREATOR(PKMImageDecoder);
105 /////////////////////////////////////////////////////////////////////////////////////////
106
is_pkm(SkStreamRewindable * stream)107 static bool is_pkm(SkStreamRewindable* stream) {
108 // Read the PKM header and make sure it's valid.
109 unsigned char buf[ETC_PKM_HEADER_SIZE];
110 if (stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) {
111 return false;
112 }
113
114 return SkToBool(etc1_pkm_is_valid(buf));
115 }
116
sk_libpkm_dfactory(SkStreamRewindable * stream)117 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
118 if (is_pkm(stream)) {
119 return SkNEW(SkPKMImageDecoder);
120 }
121 return NULL;
122 }
123
124 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
125
get_format_pkm(SkStreamRewindable * stream)126 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) {
127 if (is_pkm(stream)) {
128 return SkImageDecoder::kPKM_Format;
129 }
130 return SkImageDecoder::kUnknown_Format;
131 }
132
133 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);
134