Home | History | Annotate | Download | only in images
      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     Format getFormat() const override {
     23         return kPKM_Format;
     24     }
     25 
     26 protected:
     27     Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override;
     28 
     29 private:
     30     typedef SkImageDecoder INHERITED;
     31 };
     32 
     33 /////////////////////////////////////////////////////////////////////////////////////////
     34 
     35 SkImageDecoder::Result 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 kFailure;
     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     // 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, NULL)) {
     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 /////////////////////////////////////////////////////////////////////////////////////////
     99 DEFINE_DECODER_CREATOR(PKMImageDecoder);
    100 /////////////////////////////////////////////////////////////////////////////////////////
    101 
    102 static 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 
    112 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
    113     if (is_pkm(stream)) {
    114         return SkNEW(SkPKMImageDecoder);
    115     }
    116     return NULL;
    117 }
    118 
    119 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
    120 
    121 static 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 
    128 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);
    129