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