Handle different types of streams in different jpeg source managers
For streams that are memory backed (stream->getMemoryBase() returns
a non-null ptr and hasLength() returns true), handle the stream
with skjpeg_mem_source_mgr, which directly assigns memory base to
the source manager. For other non memory backed streams, handle the
stream with skjpeg_buffered_source_mgr, which is renamed from
the old skjpeg_source_mgr with no implementation change.
Signed-off-by: cjbao <cathy.bao@intel.com>
Bug: skia:
Change-Id: I748de0bdba726bbb318922c08497135e73e37329
Reviewed-on: https://skia-review.googlesource.com/17296
Reviewed-by: Leon Scroggins <scroggo@google.com>
Reviewed-by: Matt Sarett <msarett@google.com>
Commit-Queue: Matt Sarett <msarett@google.com>
diff --git a/src/codec/SkJpegUtility.cpp b/src/codec/SkJpegUtility.cpp
index 2cf36ba..b2284c8 100644
--- a/src/codec/SkJpegUtility.cpp
+++ b/src/codec/SkJpegUtility.cpp
@@ -10,10 +10,10 @@
#include "SkCodecPriv.h"
/*
- * Initialize the source manager
+ * Initialize the buffered source manager
*/
-static void sk_init_source(j_decompress_ptr dinfo) {
- skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
+static void sk_init_buffered_source(j_decompress_ptr dinfo) {
+ skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
src->next_input_byte = (const JOCTET*) src->fBuffer;
src->bytes_in_buffer = 0;
}
@@ -21,9 +21,9 @@
/*
* Fill the input buffer from the stream
*/
-static boolean sk_fill_input_buffer(j_decompress_ptr dinfo) {
- skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
- size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
+static boolean sk_fill_buffered_input_buffer(j_decompress_ptr dinfo) {
+ skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
+ size_t bytes = src->fStream->read(src->fBuffer, skjpeg_buffered_source_mgr::kBufferSize);
// libjpeg is still happy with a less than full read, as long as the result is non-zero
if (bytes == 0) {
@@ -38,8 +38,8 @@
/*
* Skip a certain number of bytes in the stream
*/
-static void sk_skip_input_data(j_decompress_ptr dinfo, long numBytes) {
- skjpeg_source_mgr* src = (skjpeg_source_mgr*) dinfo->src;
+static void sk_skip_buffered_input_data(j_decompress_ptr dinfo, long numBytes) {
+ skjpeg_buffered_source_mgr* src = (skjpeg_buffered_source_mgr*) dinfo->src;
size_t bytes = (size_t) numBytes;
if (bytes > src->bytes_in_buffer) {
@@ -73,12 +73,12 @@
* Constructor for the source manager that we provide to libjpeg
* We provide skia implementations of all of the stream processing functions required by libjpeg
*/
-skjpeg_source_mgr::skjpeg_source_mgr(SkStream* stream)
+skjpeg_buffered_source_mgr::skjpeg_buffered_source_mgr(SkStream* stream)
: fStream(stream)
{
- init_source = sk_init_source;
- fill_input_buffer = sk_fill_input_buffer;
- skip_input_data = sk_skip_input_data;
+ init_source = sk_init_buffered_source;
+ fill_input_buffer = sk_fill_buffered_input_buffer;
+ skip_input_data = sk_skip_buffered_input_data;
resync_to_restart = jpeg_resync_to_restart;
term_source = sk_term_source;
}
@@ -93,3 +93,46 @@
(*error->output_message) (dinfo);
longjmp(error->fJmpBuf, 1);
}
+
+/* memory backed source manager */
+/*
+ * Initialize the mem backed source manager
+ */
+static void sk_init_mem_source(j_decompress_ptr dinfo) {
+ /* no work necessary here, all things are done in constructor */
+}
+
+static void sk_skip_mem_input_data (j_decompress_ptr cinfo, long num_bytes) {
+ jpeg_source_mgr* src = cinfo->src;
+ size_t bytes = static_cast<size_t>(num_bytes);
+ if(bytes > src->bytes_in_buffer) {
+ src->next_input_byte = nullptr;
+ src->bytes_in_buffer = 0;
+ } else {
+ src->next_input_byte += bytes;
+ src->bytes_in_buffer -= bytes;
+ }
+}
+
+static boolean sk_fill_mem_input_buffer (j_decompress_ptr cinfo) {
+ /* The whole JPEG data is expected to reside in the supplied memory,
+ * buffer, so any request for more data beyond the given buffer size
+ * is treated as an error.
+ */
+ return false;
+}
+
+skjpeg_mem_source_mgr::skjpeg_mem_source_mgr(SkStream* stream)
+{
+ SkASSERT(stream->hasLength());
+ SkASSERT(stream->getMemoryBase());
+
+ init_source = sk_init_mem_source;
+ fill_input_buffer = sk_fill_mem_input_buffer;
+ skip_input_data = sk_skip_mem_input_data;
+ resync_to_restart = jpeg_resync_to_restart;
+ term_source = sk_term_source;
+ bytes_in_buffer = static_cast<size_t>(stream->getLength());
+ next_input_byte = static_cast<const JOCTET*>(stream->getMemoryBase());
+}
+