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

Unified 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, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/codec/SkBmpCodec.cpp ('k') | src/codec/SkCodec_libgif.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/codec/SkCodec.cpp
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index 11eb1f98479461b7307695ac49d44cde6f85cecb..e483c484eef6e7da5aba9f74dba114118cabfafe 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -20,7 +20,7 @@
#include "SkWebpCodec.h"
struct DecoderProc {
- bool (*IsFormat)(SkStream*);
+ bool (*IsFormat)(const char*, size_t);
SkCodec* (*NewFromStream)(SkStream*);
};
@@ -42,23 +42,31 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream,
}
SkAutoTDelete<SkStream> streamDeleter(stream);
-
+
+ // 14 is enough to read all of the supported types.
+ size_t bytesRead = 14;
+ char buffer[bytesRead];
+ if (!stream->peek(buffer, bytesRead)) {
+ SkCodecPrintf("Could not peek!\n");
+ // It is possible the stream does not support peeking, but does support
+ // rewinding.
+ // It is also possible to have a complete image less than 14 bytes
+ // (e.g. a 1 x 1 wbmp), meaning peek() would fail, and read() may read
+ // 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
+ // Attempt to read() and pass the actual amount read to the decoder.
+ bytesRead = stream->read(buffer, bytesRead);
+ if (!stream->rewind()) {
+ return nullptr;
+ }
+ }
SkAutoTDelete<SkCodec> codec(nullptr);
// PNG is special, since we want to be able to supply an SkPngChunkReader.
// But this code follows the same pattern as the loop.
- const bool isPng = SkPngCodec::IsPng(stream);
- if (!stream->rewind()) {
- return NULL;
- }
- if (isPng) {
+ if (SkPngCodec::IsPng((const char*)buffer, bytesRead)) {
codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReader));
} else {
for (DecoderProc proc : gDecoderProcs) {
- const bool correctFormat = proc.IsFormat(stream);
- if (!stream->rewind()) {
- return nullptr;
- }
- if (correctFormat) {
+ if (proc.IsFormat(buffer, bytesRead)) {
codec.reset(proc.NewFromStream(streamDeleter.detach()));
break;
}
« 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