Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: src/codec/SkCodec.cpp

Issue 1472123002: Make SkCodec support peek() and read() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Update comment. Add tests Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/codec/SkBmpCodec.cpp ('k') | src/codec/SkCodec_libgif.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 SkCodec* SkCodec::NewFromStream(SkStream* stream, 38 SkCodec* SkCodec::NewFromStream(SkStream* stream,
39 SkPngChunkReader* chunkReader) { 39 SkPngChunkReader* chunkReader) {
40 if (!stream) { 40 if (!stream) {
41 return nullptr; 41 return nullptr;
42 } 42 }
43 43
44 SkAutoTDelete<SkStream> streamDeleter(stream); 44 SkAutoTDelete<SkStream> streamDeleter(stream);
45 45
46 // 14 is enough to read all of the supported types.
47 size_t bytesRead = 14;
48 char buffer[bytesRead];
49 if (!stream->peek(buffer, bytesRead)) {
50 SkCodecPrintf("Could not peek!\n");
51 // It is possible the stream does not support peeking, but does support
52 // rewinding.
53 // It is also possible to have a complete image less than 14 bytes
54 // (e.g. a 1 x 1 wbmp), meaning peek() would fail, and read() may read
55 // less than 14 bytes.
scroggo 2015/11/30 20:02:30 Some thoughts on the overall goal here: We need t
scroggo 2015/12/01 22:33:49 You've convinced me to do what I should've done al
56 // Attempt to read() and pass the actual amount read to the decoder.
57 bytesRead = stream->read(buffer, bytesRead);
58 if (!stream->rewind()) {
59 return nullptr;
60 }
61 }
46 SkAutoTDelete<SkCodec> codec(nullptr); 62 SkAutoTDelete<SkCodec> codec(nullptr);
47 // PNG is special, since we want to be able to supply an SkPngChunkReader. 63 // PNG is special, since we want to be able to supply an SkPngChunkReader.
48 // But this code follows the same pattern as the loop. 64 // But this code follows the same pattern as the loop.
49 const bool isPng = SkPngCodec::IsPng(stream); 65 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)); 66 codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReade r));
55 } else { 67 } else {
56 for (DecoderProc proc : gDecoderProcs) { 68 for (DecoderProc proc : gDecoderProcs) {
57 const bool correctFormat = proc.IsFormat(stream); 69 if (proc.IsFormat(buffer, bytesRead)) {
58 if (!stream->rewind()) {
59 return nullptr;
60 }
61 if (correctFormat) {
62 codec.reset(proc.NewFromStream(streamDeleter.detach())); 70 codec.reset(proc.NewFromStream(streamDeleter.detach()));
63 break; 71 break;
64 } 72 }
65 } 73 }
66 } 74 }
67 75
68 // Set the max size at 128 megapixels (512 MB for kN32). 76 // 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 77 // This is about 4x smaller than a test image that takes a few minutes for
70 // dm to decode and draw. 78 // dm to decode and draw.
71 const int32_t maxSize = 1 << 27; 79 const int32_t maxSize = 1 << 27;
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested); 360 SkASSERT(1 == linesRequested || this->getInfo().height() == linesReq uested);
353 const SkImageInfo fillInfo = info.makeWH(info.width(), 1); 361 const SkImageInfo fillInfo = info.makeWH(info.width(), 1);
354 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { 362 for (int srcY = linesDecoded; srcY < linesRequested; srcY++) {
355 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes); 363 fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * r owBytes);
356 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler); 364 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, samp ler);
357 } 365 }
358 break; 366 break;
359 } 367 }
360 } 368 }
361 } 369 }
OLDNEW
« no previous file with comments | « src/codec/SkBmpCodec.cpp ('k') | src/codec/SkCodec_libgif.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698