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; |
255 | 256 |
256 private: | 257 private: |
257 virtual bool encodePKM(SkWStream* stream, const SkData *data); | 258 virtual bool encodePKM(SkWStream* stream, const SkData *data); |
258 typedef SkImageEncoder INHERITED; | 259 typedef SkImageEncoder INHERITED; |
259 }; | 260 }; |
260 | 261 |
| 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 |
261 bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int)
{ | 277 bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int)
{ |
262 if (!bitmap.pixelRef()) { | 278 if (!bitmap.pixelRef()) { |
263 return false; | 279 return false; |
264 } | 280 } |
265 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); | |
266 | 281 |
267 // Is this even encoded data? | 282 SkAutoDataUnref encoded(bitmap.pixelRef()->refEncodedData()); |
268 if (data) { | 283 if (encoded) { |
269 const uint8_t *bytes = data->bytes(); | 284 SkAutoDataUnref reencoded(this->onReencodeData(encoded)); |
270 if (etc1_pkm_is_valid(bytes)) { | 285 if (reencoded) { |
271 return this->encodePKM(stream, data); | 286 return stream->write(reencoded->bytes(), reencoded->size()); |
272 } | 287 } |
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... | |
281 } | 288 } |
282 | |
283 return SkKTXFile::WriteBitmapToKTX(stream, bitmap); | 289 return SkKTXFile::WriteBitmapToKTX(stream, bitmap); |
284 } | 290 } |
285 | 291 |
286 bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) { | 292 bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) { |
287 const uint8_t* bytes = data->bytes(); | 293 const uint8_t* bytes = data->bytes(); |
288 SkASSERT(etc1_pkm_is_valid(bytes)); | 294 SkASSERT(etc1_pkm_is_valid(bytes)); |
289 | 295 |
290 etc1_uint32 width = etc1_pkm_get_width(bytes); | 296 etc1_uint32 width = etc1_pkm_get_width(bytes); |
291 etc1_uint32 height = etc1_pkm_get_height(bytes); | 297 etc1_uint32 height = etc1_pkm_get_height(bytes); |
292 | 298 |
(...skipping 28 matching lines...) Expand all Loading... |
321 return SkImageDecoder::kUnknown_Format; | 327 return SkImageDecoder::kUnknown_Format; |
322 } | 328 } |
323 | 329 |
324 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { | 330 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { |
325 return (SkImageEncoder::kKTX_Type == t) ? new SkKTXImageEncoder : nullptr; | 331 return (SkImageEncoder::kKTX_Type == t) ? new SkKTXImageEncoder : nullptr; |
326 } | 332 } |
327 | 333 |
328 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); | 334 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); |
329 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); | 335 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); |
330 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); | 336 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); |
OLD | NEW |