• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 
9 #include "src/encode/SkJPEGWriteUtility.h"
10 
11 #include "include/core/SkStream.h"
12 #include "include/private/base/SkTArray.h"
13 #include "src/codec/SkJpegPriv.h"
14 
15 #include <csetjmp>
16 #include <cstddef>
17 
18 extern "C" {
19     #include "jerror.h"
20     #include "jmorecfg.h"
21 }
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 
sk_init_destination(j_compress_ptr cinfo)25 static void sk_init_destination(j_compress_ptr cinfo) {
26     skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
27 
28     dest->next_output_byte = dest->fBuffer;
29     dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
30 }
31 
sk_empty_output_buffer(j_compress_ptr cinfo)32 static boolean sk_empty_output_buffer(j_compress_ptr cinfo) {
33     skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
34 
35 //  if (!dest->fStream->write(dest->fBuffer, skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer))
36     if (!dest->fStream->write(dest->fBuffer,
37             skjpeg_destination_mgr::kBufferSize)) {
38         ERREXIT(cinfo, JERR_FILE_WRITE);
39         return FALSE;
40     }
41 
42     dest->next_output_byte = dest->fBuffer;
43     dest->free_in_buffer = skjpeg_destination_mgr::kBufferSize;
44     return TRUE;
45 }
46 
sk_term_destination(j_compress_ptr cinfo)47 static void sk_term_destination (j_compress_ptr cinfo) {
48     skjpeg_destination_mgr* dest = (skjpeg_destination_mgr*)cinfo->dest;
49 
50     size_t size = skjpeg_destination_mgr::kBufferSize - dest->free_in_buffer;
51     if (size > 0) {
52         if (!dest->fStream->write(dest->fBuffer, size)) {
53             ERREXIT(cinfo, JERR_FILE_WRITE);
54             return;
55         }
56     }
57 
58     dest->fStream->flush();
59 }
60 
skjpeg_destination_mgr(SkWStream * stream)61 skjpeg_destination_mgr::skjpeg_destination_mgr(SkWStream* stream) : fStream(stream) {
62     this->init_destination = sk_init_destination;
63     this->empty_output_buffer = sk_empty_output_buffer;
64     this->term_destination = sk_term_destination;
65 }
66 
skjpeg_error_exit(j_common_ptr cinfo)67 void skjpeg_error_exit(j_common_ptr cinfo) {
68     skjpeg_error_mgr* error = (skjpeg_error_mgr*)cinfo->err;
69 
70     (*error->output_message) (cinfo);
71 
72     /* Let the memory manager delete any temp files before we die */
73     jpeg_destroy(cinfo);
74 
75     if (error->fJmpBufStack.empty()) {
76         SK_ABORT("JPEG error with no jmp_buf set.");
77     }
78     longjmp(*error->fJmpBufStack.back(), -1);
79 }
80