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

Unified Diff: src/codec/SkJpegUtility.cpp

Issue 1076923002: SkJpegCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@gif-real
Patch Set: SkJpegCodec Created 5 years, 8 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
Index: src/codec/SkJpegUtility.cpp
diff --git a/src/codec/SkJpegUtility.cpp b/src/codec/SkJpegUtility.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..dfef83afcf6789437dc6287919fffd5dbe3af334
--- /dev/null
+++ b/src/codec/SkJpegUtility.cpp
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCodecPriv.h"
+#include "SkJpegUtility.h"
+
+/*
+ * Initialize the source manager
+ */
+static void sk_init_source(j_decompress_ptr jpegInfo) {
+ skjpeg_source_mgr* src = (skjpeg_source_mgr*) jpegInfo->src;
+ src->next_input_byte = (const JOCTET*) src->fBuffer;
+ src->bytes_in_buffer = 0;
+#ifdef SK_BUILD_FOR_ANDROID
+ src->current_offset = 0;
+#endif
+ if (!src->fStream->rewind()) {
scroggo 2015/04/10 17:19:07 We should not need to rewind here?
msarett 2015/04/13 20:54:05 Yes agreed, this is not necessary here. This was
+ SkCodecPrintf("Failure to rewind.\n");
+ jpegInfo->err->error_exit((j_common_ptr) jpegInfo);
+ }
+}
+
+/*
+ * Seek to a specific offset in the stream
+ */
+#ifdef SK_BUILD_FOR_ANDROID
+static boolean sk_seek_input_data(j_decompress_ptr jpegInfo, long byteOffset) {
scroggo 2015/04/10 17:19:07 This function (and the rest of the SK_BUILD_FOR_AN
msarett 2015/04/13 20:54:05 Done.
+ skjpeg_source_mgr* src = (skjpeg_source_mgr*) jpegInfo->src;
+ size_t offset = (size_t) byteOffset;
+
+ if (offset > src->current_offset) {
+ size_t bytesToSkip = offset - src->current_offset;
+ if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
+ SkCodecPrintf("Failure to skip.\n");
+ jpegInfo->err->error_exit((j_common_ptr) jpegInfo);
+ return false;
+ }
+ } else {
+ if (!src->fStream->rewind()) {
+ SkCodecPrintf("Failure to rewind.\n");
+ jpegInfo->err->error_exit((j_common_ptr) jpegInfo);
+ return false;
+ }
+ if (offset != src->fStream->skip(offset)) {
+ SkCodecPrintf("Failure to skip.\n");
+ jpegInfo->err->error_exit((j_common_ptr) jpegInfo);
+ return false;
+ }
+ }
+
+ src->current_offset = offset;
+ src->next_input_byte = (const JOCTET*) src->fBuffer;
+ src->bytes_in_buffer = 0;
+ return true;
+}
+#endif
+
+/*
+ * Fill the input buffer from the stream
+ */
+static boolean sk_fill_input_buffer(j_decompress_ptr jpegInfo) {
+ skjpeg_source_mgr* src = (skjpeg_source_mgr*) jpegInfo->src;
+ size_t bytes = src->fStream->read(src->fBuffer, skjpeg_source_mgr::kBufferSize);
+
+ // libjpeg is still happy with a less than full read, as long as the result is non-zero
+ if (bytes == 0) {
+ return false;
+ }
+
+#ifdef SK_BUILD_FOR_ANDROID
+ src->current_offset += bytes;
+#endif
+ src->next_input_byte = (const JOCTET*) src->fBuffer;
+ src->bytes_in_buffer = bytes;
+ return true;
+}
+
+/*
+ * Skip a certain number of bytes in the stream
+ */
+static void sk_skip_input_data(j_decompress_ptr jpegInfo, long numBytes) {
+ skjpeg_source_mgr* src = (skjpeg_source_mgr*) jpegInfo->src;
+ size_t bytes = (size_t) numBytes;
+
+ if (bytes > src->bytes_in_buffer) {
+ size_t bytesToSkip = bytes - src->bytes_in_buffer;
+ if (bytesToSkip != src->fStream->skip(bytesToSkip)) {
+ SkCodecPrintf("Failure to skip.\n");
+ jpegInfo->err->error_exit((j_common_ptr)jpegInfo);
+ return;
+ }
+#ifdef SK_BUILD_FOR_ANDROID
+ src->current_offset += bytesToSkip;
+#endif
+ src->next_input_byte = (const JOCTET*) src->fBuffer;
+ src->bytes_in_buffer = 0;
+ } else {
+ src->next_input_byte += numBytes;
+ src->bytes_in_buffer -= numBytes;
+ }
+}
+
+/*
+ * We do not need to do anything to terminate our stream
+ */
+static void sk_term_source(j_decompress_ptr jpegInfo)
+{}
+
+/*
+ * 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)
+ : fStream(stream)
+{
+ init_source = sk_init_source;
+ fill_input_buffer = sk_fill_input_buffer;
+ skip_input_data = sk_skip_input_data;
+ resync_to_restart = jpeg_resync_to_restart;
+ term_source = sk_term_source;
+#ifdef SK_BUILD_FOR_ANDROID
+ seek_input_data = sk_seek_input_data;
+#endif
+}
+
+/*
+ * Exit on an error
+ */
+void skjpeg_err_exit(j_common_ptr jpegInfo) {
+ // Simply return to Skia client code
+ // JpegAutoClean should take care of freeing memory
+ skjpeg_error_mgr* error = (skjpeg_error_mgr*) jpegInfo->err;
+ (*error->output_message) (jpegInfo);
+ longjmp(error->fJmpBuf, 1);
+}

Powered by Google App Engine
This is Rietveld 408576698