OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkScaledBitmapSampler.h" | 10 #include "SkScaledBitmapSampler.h" |
11 #include "SkStream.h" | 11 #include "SkStream.h" |
12 #include "SkStreamPriv.h" | 12 #include "SkStreamPriv.h" |
13 #include "SkTextureCompressor.h" | 13 #include "SkTextureCompressor.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 | 15 |
16 #include "etc1.h" | 16 #include "etc1.h" |
17 | 17 |
18 class SkPKMImageDecoder : public SkImageDecoder { | 18 class SkPKMImageDecoder : public SkImageDecoder { |
19 public: | 19 public: |
20 SkPKMImageDecoder() { } | 20 SkPKMImageDecoder() { } |
21 | 21 |
22 virtual Format getFormat() const SK_OVERRIDE { | 22 virtual Format getFormat() const SK_OVERRIDE { |
23 return kPKM_Format; | 23 return kPKM_Format; |
24 } | 24 } |
25 | 25 |
26 protected: | 26 protected: |
27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 27 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
28 | 28 |
29 private: | 29 private: |
30 typedef SkImageDecoder INHERITED; | 30 typedef SkImageDecoder INHERITED; |
31 }; | 31 }; |
32 | 32 |
33 ////////////////////////////////////////////////////////////////////////////////
///////// | 33 ////////////////////////////////////////////////////////////////////////////////
///////// |
34 | 34 |
35 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { | 35 SkImageDecoder::Result SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* b
m, Mode mode) { |
36 SkAutoMalloc autoMal; | 36 SkAutoMalloc autoMal; |
37 const size_t length = SkCopyStreamToStorage(&autoMal, stream); | 37 const size_t length = SkCopyStreamToStorage(&autoMal, stream); |
38 if (0 == length) { | 38 if (0 == length) { |
39 return false; | 39 return kFailure; |
40 } | 40 } |
41 | 41 |
42 unsigned char* buf = (unsigned char*)autoMal.get(); | 42 unsigned char* buf = (unsigned char*)autoMal.get(); |
43 | 43 |
44 // Make sure original PKM header is there... | 44 // Make sure original PKM header is there... |
45 SkASSERT(etc1_pkm_is_valid(buf)); | 45 SkASSERT(etc1_pkm_is_valid(buf)); |
46 | 46 |
47 const unsigned short width = etc1_pkm_get_width(buf); | 47 const unsigned short width = etc1_pkm_get_width(buf); |
48 const unsigned short height = etc1_pkm_get_height(buf); | 48 const unsigned short height = etc1_pkm_get_height(buf); |
49 | 49 |
50 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER | 50 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
51 // should we allow the Chooser (if present) to pick a config for us??? | 51 // should we allow the Chooser (if present) to pick a config for us??? |
52 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) { | 52 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) { |
53 return false; | 53 return kFailure; |
54 } | 54 } |
55 #endif | 55 #endif |
56 | 56 |
57 // Setup the sampler... | 57 // Setup the sampler... |
58 SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); | 58 SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); |
59 | 59 |
60 // Set the config... | 60 // Set the config... |
61 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight
(), | 61 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight
(), |
62 kOpaque_SkAlphaType)); | 62 kOpaque_SkAlphaType)); |
63 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 63 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
64 return true; | 64 return kSuccess; |
65 } | 65 } |
66 | 66 |
67 if (!this->allocPixelRef(bm, NULL)) { | 67 if (!this->allocPixelRef(bm, NULL)) { |
68 return false; | 68 return kFailure; |
69 } | 69 } |
70 | 70 |
71 // Lock the pixels, since we're about to write to them... | 71 // Lock the pixels, since we're about to write to them... |
72 SkAutoLockPixels alp(*bm); | 72 SkAutoLockPixels alp(*bm); |
73 | 73 |
74 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) { | 74 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) { |
75 return false; | 75 return kFailure; |
76 } | 76 } |
77 | 77 |
78 // Advance buffer past the header | 78 // Advance buffer past the header |
79 buf += ETC_PKM_HEADER_SIZE; | 79 buf += ETC_PKM_HEADER_SIZE; |
80 | 80 |
81 // ETC1 Data is encoded as RGB pixels, so we should extract it as such | 81 // ETC1 Data is encoded as RGB pixels, so we should extract it as such |
82 int nPixels = width * height; | 82 int nPixels = width * height; |
83 SkAutoMalloc outRGBData(nPixels * 3); | 83 SkAutoMalloc outRGBData(nPixels * 3); |
84 uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get()); | 84 uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get()); |
85 | 85 |
86 // Decode ETC1 | 86 // Decode ETC1 |
87 if (!SkTextureCompressor::DecompressBufferFromFormat( | 87 if (!SkTextureCompressor::DecompressBufferFromFormat( |
88 outRGBDataPtr, width*3, buf, width, height, SkTextureCompressor::kET
C1_Format)) { | 88 outRGBDataPtr, width*3, buf, width, height, SkTextureCompressor::kET
C1_Format)) { |
89 return false; | 89 return kFailure; |
90 } | 90 } |
91 | 91 |
92 // Set each of the pixels... | 92 // Set each of the pixels... |
93 const int srcRowBytes = width * 3; | 93 const int srcRowBytes = width * 3; |
94 const int dstHeight = sampler.scaledHeight(); | 94 const int dstHeight = sampler.scaledHeight(); |
95 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr); | 95 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr); |
96 srcRow += sampler.srcY0() * srcRowBytes; | 96 srcRow += sampler.srcY0() * srcRowBytes; |
97 for (int y = 0; y < dstHeight; ++y) { | 97 for (int y = 0; y < dstHeight; ++y) { |
98 sampler.next(srcRow); | 98 sampler.next(srcRow); |
99 srcRow += sampler.srcDY() * srcRowBytes; | 99 srcRow += sampler.srcDY() * srcRowBytes; |
100 } | 100 } |
101 | 101 |
102 return true; | 102 return kSuccess; |
103 } | 103 } |
104 | 104 |
105 ////////////////////////////////////////////////////////////////////////////////
///////// | 105 ////////////////////////////////////////////////////////////////////////////////
///////// |
106 DEFINE_DECODER_CREATOR(PKMImageDecoder); | 106 DEFINE_DECODER_CREATOR(PKMImageDecoder); |
107 ////////////////////////////////////////////////////////////////////////////////
///////// | 107 ////////////////////////////////////////////////////////////////////////////////
///////// |
108 | 108 |
109 static bool is_pkm(SkStreamRewindable* stream) { | 109 static bool is_pkm(SkStreamRewindable* stream) { |
110 // Read the PKM header and make sure it's valid. | 110 // Read the PKM header and make sure it's valid. |
111 unsigned char buf[ETC_PKM_HEADER_SIZE]; | 111 unsigned char buf[ETC_PKM_HEADER_SIZE]; |
112 if (stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) { | 112 if (stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) { |
(...skipping 13 matching lines...) Expand all Loading... |
126 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory); | 126 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory); |
127 | 127 |
128 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) { | 128 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) { |
129 if (is_pkm(stream)) { | 129 if (is_pkm(stream)) { |
130 return SkImageDecoder::kPKM_Format; | 130 return SkImageDecoder::kPKM_Format; |
131 } | 131 } |
132 return SkImageDecoder::kUnknown_Format; | 132 return SkImageDecoder::kUnknown_Format; |
133 } | 133 } |
134 | 134 |
135 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm); | 135 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm); |
OLD | NEW |