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 "SkEndian.h" | |
9 #include "SkColorPriv.h" | |
10 #include "SkImageDecoder.h" | |
11 #include "SkScaledBitmapSampler.h" | |
12 #include "SkStream.h" | |
13 #include "SkStreamPriv.h" | |
14 #include "SkTypes.h" | |
15 | |
16 #include "SkTextureCompressor.h" | |
17 | |
18 class SkASTCImageDecoder : public SkImageDecoder { | |
19 public: | |
20 SkASTCImageDecoder() { } | |
21 | |
22 virtual Format getFormat() const SK_OVERRIDE { | |
23 return kASTC_Format; | |
24 } | |
25 | |
26 protected: | |
27 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | |
28 | |
29 private: | |
30 typedef SkImageDecoder INHERITED; | |
31 }; | |
32 | |
33 //////////////////////////////////////////////////////////////////////////////// ///////// | |
34 | |
35 static inline int read_24bit(const uint8_t* buf) { | |
36 // Assume everything is little endian... | |
37 return | |
38 static_cast<int>(buf[0]) | | |
39 (static_cast<int>(buf[1]) << 8) | | |
40 (static_cast<int>(buf[2]) << 16); | |
41 } | |
42 | |
43 bool SkASTCImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { | |
44 SkAutoMalloc autoMal; | |
45 const size_t length = SkCopyStreamToStorage(&autoMal, stream); | |
46 if (0 == length) { | |
47 return false; | |
48 } | |
49 | |
50 unsigned char* buf = (unsigned char*)autoMal.get(); | |
51 | |
robertphillips
2014/08/06 21:13:15
Since the magic number is used twice can we define
krajcevski
2014/08/06 22:31:00
Done.
| |
52 // Make sure that the magic header is there... | |
53 SkASSERT(SkEndian_SwapLE32(*(reinterpret_cast<uint32_t*>(buf))) == 0x5CA1AB1 3); | |
54 | |
55 // Advance past the magic header | |
56 buf += 4; | |
57 | |
58 const int blockDimX = buf[0]; | |
59 const int blockDimY = buf[1]; | |
60 const int blockDimZ = buf[2]; | |
61 | |
62 if (1 != blockDimZ) { | |
63 // We don't support decoding 3D | |
64 return false; | |
65 } | |
66 | |
67 // Choose the proper ASTC format | |
68 SkTextureCompressor::Format astcFormat; | |
69 if (4 == blockDimX && 4 == blockDimY) { | |
70 astcFormat = SkTextureCompressor::kASTC_4x4_Format; | |
71 } else if (5 == blockDimX && 4 == blockDimY) { | |
72 astcFormat = SkTextureCompressor::kASTC_5x4_Format; | |
73 } else if (5 == blockDimX && 5 == blockDimY) { | |
74 astcFormat = SkTextureCompressor::kASTC_5x5_Format; | |
75 } else if (6 == blockDimX && 5 == blockDimY) { | |
76 astcFormat = SkTextureCompressor::kASTC_6x5_Format; | |
77 } else if (6 == blockDimX && 6 == blockDimY) { | |
78 astcFormat = SkTextureCompressor::kASTC_6x6_Format; | |
79 } else if (8 == blockDimX && 5 == blockDimY) { | |
80 astcFormat = SkTextureCompressor::kASTC_8x5_Format; | |
81 } else if (8 == blockDimX && 6 == blockDimY) { | |
82 astcFormat = SkTextureCompressor::kASTC_8x6_Format; | |
83 } else if (8 == blockDimX && 8 == blockDimY) { | |
84 astcFormat = SkTextureCompressor::kASTC_8x8_Format; | |
85 } else if (10 == blockDimX && 5 == blockDimY) { | |
86 astcFormat = SkTextureCompressor::kASTC_10x5_Format; | |
87 } else if (10 == blockDimX && 6 == blockDimY) { | |
88 astcFormat = SkTextureCompressor::kASTC_10x6_Format; | |
89 } else if (10 == blockDimX && 8 == blockDimY) { | |
90 astcFormat = SkTextureCompressor::kASTC_10x8_Format; | |
91 } else if (10 == blockDimX && 10 == blockDimY) { | |
92 astcFormat = SkTextureCompressor::kASTC_10x10_Format; | |
93 } else if (12 == blockDimX && 10 == blockDimY) { | |
94 astcFormat = SkTextureCompressor::kASTC_12x10_Format; | |
95 } else if (12 == blockDimX && 12 == blockDimY) { | |
96 astcFormat = SkTextureCompressor::kASTC_12x12_Format; | |
97 } else { | |
98 // We don't support any other block dimensions.. | |
99 return false; | |
100 } | |
101 | |
102 // Advance buf past the block dimensions | |
103 buf += 3; | |
104 | |
105 // Read the width/height/depth from the buffer... | |
106 const int width = read_24bit(buf); | |
107 const int height = read_24bit(buf + 3); | |
108 const int depth = read_24bit(buf + 6); | |
109 | |
110 if (1 != depth) { | |
111 // We don't support decoding 3D. | |
112 return false; | |
113 } | |
114 | |
115 // Advance the buffer past the image dimensions | |
116 buf += 9; | |
117 | |
118 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER | |
119 // should we allow the Chooser (if present) to pick a config for us??? | |
120 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) { | |
121 return false; | |
122 } | |
123 #endif | |
124 | |
125 // Setup the sampler... | |
126 SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); | |
127 | |
128 // Set the config... | |
129 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight (), | |
130 kPremul_SkAlphaType)); | |
131 | |
132 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | |
133 return true; | |
134 } | |
135 | |
136 if (!this->allocPixelRef(bm, NULL)) { | |
137 return false; | |
138 } | |
139 | |
140 // Lock the pixels, since we're about to write to them... | |
141 SkAutoLockPixels alp(*bm); | |
142 | |
143 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) { | |
144 return false; | |
145 } | |
146 | |
147 // ASTC Data is encoded as RGBA pixels, so we should extract it as such | |
148 int nPixels = width * height; | |
149 SkAutoMalloc outRGBAData(nPixels * 4); | |
150 uint8_t *outRGBADataPtr = reinterpret_cast<uint8_t *>(outRGBAData.get()); | |
151 | |
152 // Decode ASTC | |
153 if (!SkTextureCompressor::DecompressBufferFromFormat( | |
154 outRGBADataPtr, width*4, buf, width, height, astcFormat)) { | |
155 } | |
156 | |
157 // Set each of the pixels... | |
158 const int srcRowBytes = width * 4; | |
159 const int dstHeight = sampler.scaledHeight(); | |
160 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBADataPtr); | |
161 srcRow += sampler.srcY0() * srcRowBytes; | |
162 for (int y = 0; y < dstHeight; ++y) { | |
163 sampler.next(srcRow); | |
164 srcRow += sampler.srcDY() * srcRowBytes; | |
165 } | |
166 | |
167 return true; | |
168 } | |
169 | |
170 //////////////////////////////////////////////////////////////////////////////// ///////// | |
171 DEFINE_DECODER_CREATOR(ASTCImageDecoder); | |
172 //////////////////////////////////////////////////////////////////////////////// ///////// | |
173 | |
174 static bool is_astc(SkStreamRewindable* stream) { | |
175 // Read the ASTC header and make sure it's valid. | |
176 uint32_t magic; | |
177 if (stream->read((void*)&magic, 4) != 4) { | |
178 return false; | |
179 } | |
180 | |
181 return 0x5CA1AB13 == SkEndian_SwapLE32(magic); | |
182 } | |
183 | |
184 static SkImageDecoder* sk_libastc_dfactory(SkStreamRewindable* stream) { | |
185 if (is_astc(stream)) { | |
186 return SkNEW(SkASTCImageDecoder); | |
187 } | |
188 return NULL; | |
189 } | |
190 | |
191 static SkImageDecoder_DecodeReg gReg(sk_libastc_dfactory); | |
192 | |
193 static SkImageDecoder::Format get_format_astc(SkStreamRewindable* stream) { | |
194 if (is_astc(stream)) { | |
195 return SkImageDecoder::kASTC_Format; | |
196 } | |
197 return SkImageDecoder::kUnknown_Format; | |
198 } | |
199 | |
200 static SkImageDecoder_FormatReg gFormatReg(get_format_astc); | |
OLD | NEW |