reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 The Android Open Source Project |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Derek Sollenberger | d68c41d | 2017-03-22 16:19:49 +0000 | [diff] [blame] | 8 | |
msarett | c1d0312 | 2016-03-25 08:58:55 -0700 | [diff] [blame] | 9 | #include "SkJPEGWriteUtility.h" |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 10 | |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 11 | /////////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | static void sk_init_destination(j_compress_ptr cinfo) { |
| 14 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 15 | |
| 16 | dest->next_output_byte = dest->fBuffer; |
| 17 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 18 | } |
| 19 | |
| 20 | static boolean sk_empty_output_buffer(j_compress_ptr cinfo) { |
| 21 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 22 | |
| 23 | // if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer)) |
| 24 | if (!dest->fStream->write(dest->fBuffer, |
| 25 | skjpeg_destination_mgr::kBufferSize)) { |
| 26 | ERREXIT(cinfo, JERR_FILE_WRITE); |
Mike Reed | 46ee3f7 | 2019-01-02 00:00:59 -0500 | [diff] [blame] | 27 | return FALSE; |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | dest->next_output_byte = dest->fBuffer; |
| 31 | dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize; |
| 32 | return TRUE; |
| 33 | } |
| 34 | |
| 35 | static void sk_term_destination (j_compress_ptr cinfo) { |
| 36 | skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest; |
| 37 | |
| 38 | size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer; |
| 39 | if (size > 0) { |
| 40 | if (!dest->fStream->write(dest->fBuffer, size)) { |
| 41 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 42 | return; |
| 43 | } |
| 44 | } |
| 45 | dest->fStream->flush(); |
| 46 | } |
| 47 | |
| 48 | skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream) |
| 49 | : fStream(stream) { |
| 50 | this->init_destination = sk_init_destination; |
| 51 | this->empty_output_buffer = sk_empty_output_buffer; |
| 52 | this->term_destination = sk_term_destination; |
| 53 | } |
| 54 | |
Derek Sollenberger | d68c41d | 2017-03-22 16:19:49 +0000 | [diff] [blame] | 55 | void skjpeg_error_exit(j_common_ptr cinfo) { |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 56 | skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err; |
| 57 | |
| 58 | (*error->output_message) (cinfo); |
| 59 | |
| 60 | /* Let the memory manager delete any temp files before we die */ |
| 61 | jpeg_destroy(cinfo); |
| 62 | |
Chris Dalton | 3e79459 | 2017-12-01 13:11:09 -0700 | [diff] [blame] | 63 | if (error->fJmpBufStack.empty()) { |
| 64 | SK_ABORT("JPEG error with no jmp_buf set."); |
| 65 | } |
| 66 | longjmp(*error->fJmpBufStack.back(), -1); |
reed@android.com | 6f59815 | 2010-01-21 15:34:19 +0000 | [diff] [blame] | 67 | } |