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 "SkStreamPriv.h"
13 #include "SkTextureCompressor.h"
18 class SkPKMImageDecoder : public SkImageDecoder {
20 SkPKMImageDecoder() { }
22 virtual Format getFormat() const SK_OVERRIDE {
27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
30 typedef SkImageDecoder INHERITED;
33 /////////////////////////////////////////////////////////////////////////////////////////
35 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
37 const size_t length = SkCopyStreamToStorage(&autoMal, stream);
42 unsigned char* buf = (unsigned char*)autoMal.get();
44 // Make sure original PKM header is there...
45 SkASSERT(etc1_pkm_is_valid(buf));
47 const unsigned short width = etc1_pkm_get_width(buf);
48 const unsigned short height = etc1_pkm_get_height(buf);
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)) {
57 // Setup the sampler...
58 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
61 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(),
62 kOpaque_SkAlphaType));
63 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
67 if (!this->allocPixelRef(bm, NULL)) {
71 // Lock the pixels, since we're about to write to them...
72 SkAutoLockPixels alp(*bm);
74 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
78 // Advance buffer past the header
79 buf += ETC_PKM_HEADER_SIZE;
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());
87 if (!SkTextureCompressor::DecompressBufferFromFormat(
88 outRGBDataPtr, width*3, buf, width, height, SkTextureCompressor::kETC1_Format)) {
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) {
99 srcRow += sampler.srcDY() * srcRowBytes;
105 /////////////////////////////////////////////////////////////////////////////////////////
106 DEFINE_DECODER_CREATOR(PKMImageDecoder);
107 /////////////////////////////////////////////////////////////////////////////////////////
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) {
116 return SkToBool(etc1_pkm_is_valid(buf));
119 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
120 if (is_pkm(stream)) {
121 return SkNEW(SkPKMImageDecoder);
126 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
128 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) {
129 if (is_pkm(stream)) {
130 return SkImageDecoder::kPKM_Format;
132 return SkImageDecoder::kUnknown_Format;
135 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);