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 "SkPixelRef.h" | 10 #include "SkPixelRef.h" |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // will repurpose the existing ETC1 data into a KTX file. If the data contains | 245 // will repurpose the existing ETC1 data into a KTX file. If the data contains |
246 // KTX data, then we simply return a copy of the same data. For all other files, | 246 // KTX data, then we simply return a copy of the same data. For all other files, |
247 // the underlying KTX library tries to do its best to encode the appropriate | 247 // the underlying KTX library tries to do its best to encode the appropriate |
248 // data specified by the bitmap based on the config. (i.e. kAlpha8_Config will | 248 // data specified by the bitmap based on the config. (i.e. kAlpha8_Config will |
249 // be represented as a full resolution 8-bit image dump with the appropriate | 249 // be represented as a full resolution 8-bit image dump with the appropriate |
250 // OpenGL defines in the header). | 250 // OpenGL defines in the header). |
251 | 251 |
252 class SkKTXImageEncoder : public SkImageEncoder { | 252 class SkKTXImageEncoder : public SkImageEncoder { |
253 protected: | 253 protected: |
254 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; | 254 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; |
255 SkData* onReencodeData(SkData*) override; | |
256 | 255 |
257 private: | 256 private: |
258 virtual bool encodePKM(SkWStream* stream, const SkData *data); | 257 virtual bool encodePKM(SkWStream* stream, const SkData *data); |
259 typedef SkImageEncoder INHERITED; | 258 typedef SkImageEncoder INHERITED; |
260 }; | 259 }; |
261 | 260 |
262 SkData* SkKTXImageEncoder::onReencodeData(SkData* encoded) { | |
263 const uint8_t* bytes = encoded->bytes(); | |
264 if (etc1_pkm_is_valid(bytes)) { | |
265 SkDynamicMemoryWStream stream; | |
266 if (this->encodePKM(&stream, encoded)) { | |
267 return stream.copyToData(); | |
268 } | |
269 } | |
270 // Is it a KTX file?? | |
271 if (SkKTXFile::is_ktx(bytes)) { | |
272 return SkRef(encoded); | |
273 } | |
274 return nullptr; | |
275 } | |
276 | |
277 bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int)
{ | 261 bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int)
{ |
278 if (!bitmap.pixelRef()) { | 262 if (!bitmap.pixelRef()) { |
279 return false; | 263 return false; |
280 } | 264 } |
| 265 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); |
281 | 266 |
282 SkAutoDataUnref encoded(bitmap.pixelRef()->refEncodedData()); | 267 // Is this even encoded data? |
283 if (encoded) { | 268 if (data) { |
284 SkAutoDataUnref reencoded(this->onReencodeData(encoded)); | 269 const uint8_t *bytes = data->bytes(); |
285 if (reencoded) { | 270 if (etc1_pkm_is_valid(bytes)) { |
286 return stream->write(reencoded->bytes(), reencoded->size()); | 271 return this->encodePKM(stream, data); |
287 } | 272 } |
| 273 |
| 274 // Is it a KTX file?? |
| 275 if (SkKTXFile::is_ktx(bytes)) { |
| 276 return stream->write(bytes, data->size()); |
| 277 } |
| 278 |
| 279 // If it's neither a KTX nor a PKM, then we need to |
| 280 // get at the actual pixels, so fall through and decompress... |
288 } | 281 } |
| 282 |
289 return SkKTXFile::WriteBitmapToKTX(stream, bitmap); | 283 return SkKTXFile::WriteBitmapToKTX(stream, bitmap); |
290 } | 284 } |
291 | 285 |
292 bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) { | 286 bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) { |
293 const uint8_t* bytes = data->bytes(); | 287 const uint8_t* bytes = data->bytes(); |
294 SkASSERT(etc1_pkm_is_valid(bytes)); | 288 SkASSERT(etc1_pkm_is_valid(bytes)); |
295 | 289 |
296 etc1_uint32 width = etc1_pkm_get_width(bytes); | 290 etc1_uint32 width = etc1_pkm_get_width(bytes); |
297 etc1_uint32 height = etc1_pkm_get_height(bytes); | 291 etc1_uint32 height = etc1_pkm_get_height(bytes); |
298 | 292 |
(...skipping 28 matching lines...) Expand all Loading... |
327 return SkImageDecoder::kUnknown_Format; | 321 return SkImageDecoder::kUnknown_Format; |
328 } | 322 } |
329 | 323 |
330 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { | 324 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { |
331 return (SkImageEncoder::kKTX_Type == t) ? new SkKTXImageEncoder : nullptr; | 325 return (SkImageEncoder::kKTX_Type == t) ? new SkKTXImageEncoder : nullptr; |
332 } | 326 } |
333 | 327 |
334 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); | 328 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); |
335 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); | 329 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); |
336 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); | 330 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); |
OLD | NEW |