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