OLD | NEW |
(Empty) | |
| 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 "SkStream.h" |
| 11 #include "SkStreamHelpers.h" |
| 12 #include "SkTypes.h" |
| 13 |
| 14 #include "etc1.h" |
| 15 |
| 16 class SkPKMImageDecoder : public SkImageDecoder { |
| 17 public: |
| 18 SkPKMImageDecoder() { } |
| 19 |
| 20 virtual Format getFormat() const SK_OVERRIDE { |
| 21 return kPKM_Format; |
| 22 } |
| 23 |
| 24 protected: |
| 25 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
| 26 |
| 27 private: |
| 28 typedef SkImageDecoder INHERITED; |
| 29 }; |
| 30 |
| 31 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 32 |
| 33 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
| 34 SkAutoMalloc autoMal; |
| 35 const size_t length = CopyStreamToStorage(&autoMal, stream); |
| 36 if (0 == length) { |
| 37 return false; |
| 38 } |
| 39 |
| 40 unsigned char* buf = (unsigned char*)autoMal.get(); |
| 41 |
| 42 // Make sure original PKM header is there... |
| 43 SkASSERT(etc1_pkm_is_valid(buf)); |
| 44 |
| 45 const unsigned short width = etc1_pkm_get_width(buf); |
| 46 const unsigned short height = etc1_pkm_get_height(buf); |
| 47 |
| 48 // should we allow the Chooser (if present) to pick a config for us??? |
| 49 if (!this->chooseFromOneChoice(SkBitmap::kARGB_8888_Config, width, height))
{ |
| 50 return false; |
| 51 } |
| 52 |
| 53 bm->setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, kOpaque_SkAlpha
Type); |
| 54 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 55 return true; |
| 56 } |
| 57 |
| 58 if (!this->allocPixelRef(bm, NULL)) { |
| 59 return false; |
| 60 } |
| 61 |
| 62 // Lock the pixels, since we're about to write to them... |
| 63 SkAutoLockPixels alp(*bm); |
| 64 |
| 65 // Advance buffer past the header |
| 66 buf += ETC_PKM_HEADER_SIZE; |
| 67 |
| 68 // ETC1 Data is encoded as RGB pixels, so we should extract it as such |
| 69 int nPixels = width * height; |
| 70 SkAutoMalloc outRGBData(nPixels * 3); |
| 71 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get()); |
| 72 |
| 73 // Decode ETC1 |
| 74 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) { |
| 75 return false; |
| 76 } |
| 77 |
| 78 // Set each of the pixels... |
| 79 const uint8_t *src = reinterpret_cast<uint8_t *>(outRGBDataPtr); |
| 80 uint8_t *dst = reinterpret_cast<uint8_t *>(bm->getPixels()); |
| 81 for (int i = 0; i < width*height; ++i) { |
| 82 *dst++ = src[2]; // B |
| 83 *dst++ = src[1]; // G |
| 84 *dst++ = src[0]; // R |
| 85 *dst++ = 0xFF; // Opaque alpha... |
| 86 src += 3; |
| 87 } |
| 88 |
| 89 return true; |
| 90 } |
| 91 |
| 92 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 93 DEFINE_DECODER_CREATOR(PKMImageDecoder); |
| 94 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 95 |
| 96 static bool is_pkm(SkStreamRewindable* stream) { |
| 97 // Read the PKM header and make sure it's valid. |
| 98 unsigned char buf[ETC_PKM_HEADER_SIZE]; |
| 99 if (stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) { |
| 100 return false; |
| 101 } |
| 102 |
| 103 return etc1_pkm_is_valid(buf); |
| 104 } |
| 105 |
| 106 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) { |
| 107 if (is_pkm(stream)) { |
| 108 return SkNEW(SkPKMImageDecoder); |
| 109 } |
| 110 return NULL; |
| 111 } |
| 112 |
| 113 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory); |
| 114 |
| 115 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) { |
| 116 if (is_pkm(stream)) { |
| 117 return SkImageDecoder::kPKM_Format; |
| 118 } |
| 119 return SkImageDecoder::kUnknown_Format; |
| 120 } |
| 121 |
| 122 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm); |
OLD | NEW |