Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBmpCodec.h" | 8 #include "SkBmpCodec.h" |
| 9 #include "SkCodec.h" | 9 #include "SkCodec.h" |
| 10 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkCodec_libgif.h" | 11 #include "SkCodec_libgif.h" |
| 12 #include "SkCodec_libico.h" | 12 #include "SkCodec_libico.h" |
| 13 #include "SkCodec_libpng.h" | 13 #include "SkCodec_libpng.h" |
| 14 #include "SkCodec_wbmp.h" | 14 #include "SkCodec_wbmp.h" |
| 15 #include "SkCodecPriv.h" | 15 #include "SkCodecPriv.h" |
| 16 #if !defined(GOOGLE3) | 16 #if !defined(GOOGLE3) |
| 17 #include "SkJpegCodec.h" | 17 #include "SkJpegCodec.h" |
| 18 #endif | 18 #endif |
| 19 #include "SkStream.h" | 19 #include "SkStream.h" |
| 20 #include "SkWebpCodec.h" | 20 #include "SkWebpCodec.h" |
| 21 | 21 |
| 22 struct DecoderProc { | 22 struct DecoderProc { |
| 23 bool (*IsFormat)(SkStream*); | 23 bool (*IsFormat)(const char*, size_t); |
| 24 SkCodec* (*NewFromStream)(SkStream*); | 24 SkCodec* (*NewFromStream)(SkStream*); |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 static const DecoderProc gDecoderProcs[] = { | 27 static const DecoderProc gDecoderProcs[] = { |
| 28 #if !defined(GOOGLE3) | 28 #if !defined(GOOGLE3) |
| 29 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, | 29 { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
| 30 #endif | 30 #endif |
| 31 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, | 31 { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
| 32 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, | 32 { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
| 33 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, | 33 { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
| 34 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, | 34 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 35 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } | 35 { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 size_t SkCodec::BufferedBytesNeeded() { | |
| 39 return WEBP_VP8_HEADER_SIZE; | |
| 40 } | |
| 41 | |
| 38 SkCodec* SkCodec::NewFromStream(SkStream* stream, | 42 SkCodec* SkCodec::NewFromStream(SkStream* stream, |
| 39 SkPngChunkReader* chunkReader) { | 43 SkPngChunkReader* chunkReader) { |
| 40 if (!stream) { | 44 if (!stream) { |
| 41 return nullptr; | 45 return nullptr; |
| 42 } | 46 } |
| 43 | 47 |
| 44 SkAutoTDelete<SkStream> streamDeleter(stream); | 48 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 45 | 49 |
| 50 // 14 is enough to read all of the supported types. | |
| 51 const size_t bytesToRead = 14; | |
| 52 SkASSERT(bytesToRead <= BufferedBytesNeeded()); | |
|
msarett
2015/12/07 15:29:36
So we only need 14 bytes to determine the image ty
scroggo
2015/12/07 16:06:28
That's correct. I almost called that out, and ment
| |
| 53 | |
| 54 char buffer[bytesToRead]; | |
| 55 size_t bytesRead = stream->peek(buffer, bytesToRead); | |
| 56 | |
| 57 // It is also possible to have a complete image less than bytesToRead bytes | |
| 58 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead. | |
| 59 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter | |
| 60 // than bytesToRead, so pass that directly to the decoder. | |
| 61 // It also is possible the stream uses too small a buffer for peeking, but | |
| 62 // we trust the caller to use a large enough buffer. | |
| 63 | |
| 64 if (0 == bytesRead) { | |
| 65 SkCodecPrintf("Could not peek!\n"); | |
| 66 // It is possible the stream does not support peeking, but does support | |
| 67 // rewinding. | |
| 68 // Attempt to read() and pass the actual amount read to the decoder. | |
| 69 bytesRead = stream->read(buffer, bytesToRead); | |
| 70 if (!stream->rewind()) { | |
| 71 SkCodecPrintf("Could not rewind!\n"); | |
| 72 return nullptr; | |
| 73 } | |
| 74 } | |
| 75 | |
| 46 SkAutoTDelete<SkCodec> codec(nullptr); | 76 SkAutoTDelete<SkCodec> codec(nullptr); |
| 47 // PNG is special, since we want to be able to supply an SkPngChunkReader. | 77 // PNG is special, since we want to be able to supply an SkPngChunkReader. |
| 48 // But this code follows the same pattern as the loop. | 78 // But this code follows the same pattern as the loop. |
| 49 const bool isPng = SkPngCodec::IsPng(stream); | 79 if (SkPngCodec::IsPng((const char*)buffer, bytesRead)) { |
| 50 if (!stream->rewind()) { | |
| 51 return NULL; | |
| 52 } | |
| 53 if (isPng) { | |
| 54 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade r)); | 80 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade r)); |
| 55 } else { | 81 } else { |
| 56 for (DecoderProc proc : gDecoderProcs) { | 82 for (DecoderProc proc : gDecoderProcs) { |
| 57 const bool correctFormat = proc.IsFormat(stream); | 83 if (proc.IsFormat(buffer, bytesRead)) { |
| 58 if (!stream->rewind()) { | |
| 59 return nullptr; | |
| 60 } | |
| 61 if (correctFormat) { | |
| 62 codec.reset(proc.NewFromStream(streamDeleter.detach())); | 84 codec.reset(proc.NewFromStream(streamDeleter.detach())); |
| 63 break; | 85 break; |
| 64 } | 86 } |
| 65 } | 87 } |
| 66 } | 88 } |
| 67 | 89 |
| 68 // Set the max size at 128 megapixels (512 MB for kN32). | 90 // Set the max size at 128 megapixels (512 MB for kN32). |
| 69 // This is about 4x smaller than a test image that takes a few minutes for | 91 // This is about 4x smaller than a test image that takes a few minutes for |
| 70 // dm to decode and draw. | 92 // dm to decode and draw. |
| 71 const int32_t maxSize = 1 << 27; | 93 const int32_t maxSize = 1 << 27; |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested); | 374 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested); |
| 353 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); | 375 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); |
| 354 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { | 376 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { |
| 355 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes); | 377 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes); |
| 356 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler); | 378 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler); |
| 357 } | 379 } |
| 358 break; | 380 break; |
| 359 } | 381 } |
| 360 } | 382 } |
| 361 } | 383 } |
| OLD | NEW |