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

Unified Diff: src/images/SkImageDecoder.cpp

Issue 23330002: Avoid getLength in ico decoder. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 4 months 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 | « no previous file | src/images/SkImageDecoder_libbmp.cpp » ('j') | src/images/SkImageDecoder_libbmp.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/images/SkImageDecoder.cpp
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp
index a671607ef36f811695fd8fbe4831749a294634d6..a68e324d07e88833915e853cce1cdee5a47aa8d9 100644
--- a/src/images/SkImageDecoder.cpp
+++ b/src/images/SkImageDecoder.cpp
@@ -467,3 +467,44 @@ bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm,
}
return success;
}
+
+/**
+ * Copy the provided stream to memory allocated by storage.
+ * Used by SkImageDecoder_libbmp and SkImageDecoder_libico.
+ * @param storage Allocator to hold the memory. Will be reset to be large
+ * enough to hold the entire stream. Upon successful return,
+ * storage->get() will point to data holding the streams entire
+ * contents.
+ * @param stream SkStream to be copied into storage.
+ * @return size_t Total number of bytes in the SkStream, which is also the
+ * number of bytes pointed to by storage->get(). Returns 0 on failure.
+ */
+size_t copy_stream_to_storage(SkAutoMalloc* storage, SkStream* stream) {
+ SkASSERT(storage != NULL);
+ SkASSERT(stream != NULL);
+
+ if (stream->hasLength()) {
+ const size_t length = stream->getLength();
+ void* dst = storage->reset(length);
+ if (stream->read(dst, length) != length) {
+ return 0;
+ }
+ return length;
+ }
+
+ SkDynamicMemoryWStream tempStream;
+ // Arbitrary buffer size.
+ const size_t bufferSize = 256 * 1024; // 256KB
+ char buffer[bufferSize];
+ SkDEBUGCODE(size_t debugLength = 0;)
+ do {
+ size_t bytesRead = stream->read(buffer, bufferSize);
+ tempStream.write(buffer, bytesRead);
+ SkDEBUGCODE(debugLength += bytesRead);
+ SkASSERT(tempStream.bytesWritten() == debugLength);
+ } while (!stream->isAtEnd());
+ const size_t length = tempStream.bytesWritten();
+ void* dst = storage->reset(length);
+ tempStream.copyTo(dst);
+ return length;
+}
« no previous file with comments | « no previous file | src/images/SkImageDecoder_libbmp.cpp » ('j') | src/images/SkImageDecoder_libbmp.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698