2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkColorPriv.h"
9 #include "SkImageDecoder.h"
10 #include "SkScaledBitmapSampler.h"
12 #include "SkStreamHelpers.h"
17 class SkPKMImageDecoder : public SkImageDecoder {
19 SkPKMImageDecoder() { }
21 virtual Format getFormat() const SK_OVERRIDE {
26 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
29 typedef SkImageDecoder INHERITED;
32 /////////////////////////////////////////////////////////////////////////////////////////
34 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
36 const size_t length = CopyStreamToStorage(&autoMal, stream);
41 unsigned char* buf = (unsigned char*)autoMal.get();
43 // Make sure original PKM header is there...
44 SkASSERT(etc1_pkm_is_valid(buf));
46 const unsigned short width = etc1_pkm_get_width(buf);
47 const unsigned short height = etc1_pkm_get_height(buf);
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)) {
56 // Setup the sampler...
57 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
60 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(),
61 kOpaque_SkAlphaType));
62 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
66 if (!this->allocPixelRef(bm, NULL)) {
70 // Lock the pixels, since we're about to write to them...
71 SkAutoLockPixels alp(*bm);
73 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
77 // Advance buffer past the header
78 buf += ETC_PKM_HEADER_SIZE;
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());
86 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
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) {
97 srcRow += sampler.srcDY() * srcRowBytes;
103 /////////////////////////////////////////////////////////////////////////////////////////
104 DEFINE_DECODER_CREATOR(PKMImageDecoder);
105 /////////////////////////////////////////////////////////////////////////////////////////
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) {
114 return SkToBool(etc1_pkm_is_valid(buf));
117 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
118 if (is_pkm(stream)) {
119 return SkNEW(SkPKMImageDecoder);
124 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
126 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) {
127 if (is_pkm(stream)) {
128 return SkImageDecoder::kPKM_Format;
130 return SkImageDecoder::kUnknown_Format;
133 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);