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 "SkData.h"
10#include "SkImageDecoder.h"
11#include "SkScaledBitmapSampler.h"
12#include "SkStream.h"
13#include "SkStreamPriv.h"
14#include "SkTextureCompressor.h"
15#include "SkTypes.h"
16
17#include "etc1.h"
18
19class SkPKMImageDecoder : public SkImageDecoder {
20public:
21    SkPKMImageDecoder() { }
22
23    Format getFormat() const override {
24        return kPKM_Format;
25    }
26
27protected:
28    Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override;
29
30private:
31    typedef SkImageDecoder INHERITED;
32};
33
34/////////////////////////////////////////////////////////////////////////////////////////
35
36SkImageDecoder::Result SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
37    SkAutoTUnref<SkData> data(SkCopyStreamToData(stream));
38    if (!data || !data->size()) {
39        return kFailure;
40    }
41
42    unsigned char* buf = (unsigned char*) data->data();
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    // Setup the sampler...
51    SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
52
53    // Set the config...
54    bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(),
55                                     kOpaque_SkAlphaType));
56    if (SkImageDecoder::kDecodeBounds_Mode == mode) {
57        return kSuccess;
58    }
59
60    if (!this->allocPixelRef(bm, nullptr)) {
61        return kFailure;
62    }
63
64    // Lock the pixels, since we're about to write to them...
65    SkAutoLockPixels alp(*bm);
66
67    if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
68        return kFailure;
69    }
70
71    // Advance buffer past the header
72    buf += ETC_PKM_HEADER_SIZE;
73
74    // ETC1 Data is encoded as RGB pixels, so we should extract it as such
75    int nPixels = width * height;
76    SkAutoMalloc outRGBData(nPixels * 3);
77    uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get());
78
79    // Decode ETC1
80    if (!SkTextureCompressor::DecompressBufferFromFormat(
81            outRGBDataPtr, width*3, buf, width, height, SkTextureCompressor::kETC1_Format)) {
82        return kFailure;
83    }
84
85    // Set each of the pixels...
86    const int srcRowBytes = width * 3;
87    const int dstHeight = sampler.scaledHeight();
88    const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr);
89    srcRow += sampler.srcY0() * srcRowBytes;
90    for (int y = 0; y < dstHeight; ++y) {
91        sampler.next(srcRow);
92        srcRow += sampler.srcDY() * srcRowBytes;
93    }
94
95    return kSuccess;
96}
97
98/////////////////////////////////////////////////////////////////////////////////////////
99DEFINE_DECODER_CREATOR(PKMImageDecoder);
100/////////////////////////////////////////////////////////////////////////////////////////
101
102static bool is_pkm(SkStreamRewindable* stream) {
103    // Read the PKM header and make sure it's valid.
104    unsigned char buf[ETC_PKM_HEADER_SIZE];
105    if (stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) {
106        return false;
107    }
108
109    return SkToBool(etc1_pkm_is_valid(buf));
110}
111
112static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
113    if (is_pkm(stream)) {
114        return new SkPKMImageDecoder;
115    }
116    return nullptr;
117}
118
119static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
120
121static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) {
122    if (is_pkm(stream)) {
123        return SkImageDecoder::kPKM_Format;
124    }
125    return SkImageDecoder::kUnknown_Format;
126}
127
128static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);
129