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