aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images/SkJPEGImageEncoder.cpp
blob: 845673421ccbbbbfc5bda7c771e47bb9bf079392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright 2007 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkImageEncoderPriv.h"

#ifdef SK_HAS_JPEG_LIBRARY

#include "SkColorPriv.h"
#include "SkImageEncoderFns.h"
#include "SkJPEGWriteUtility.h"
#include "SkStream.h"
#include "SkTemplates.h"

#include <stdio.h>

extern "C" {
    #include "jpeglib.h"
    #include "jerror.h"
}

/**
 *  Returns true if |info| is supported by the jpeg encoder and false otherwise.
 *  |jpegColorType| will be set to the proper libjpeg-turbo type for input to the library.
 *  |numComponents| will be set to the number of components in the |jpegColorType|.
 *  |proc|          will be set if we need to pre-convert the input before passing to
 *                  libjpeg-turbo.  Otherwise will be set to nullptr.
 */
// TODO (skbug.com/1501):
// Should we fail on non-opaque encodes?
// Or should we change alpha behavior (ex: unpremultiply when the input is premul)?
// Or is ignoring the alpha type and alpha channel ok here?
static bool set_encode_config(J_COLOR_SPACE* jpegColorType, int* numComponents,
                              transform_scanline_proc* proc, const SkImageInfo& info) {
    *proc = nullptr;
    switch (info.colorType()) {
        case kRGBA_8888_SkColorType:
            *jpegColorType = JCS_EXT_RGBA;
            *numComponents = 4;
            return true;
        case kBGRA_8888_SkColorType:
            *jpegColorType = JCS_EXT_BGRA;
            *numComponents = 4;
            return true;
        case kRGB_565_SkColorType:
            *proc = transform_scanline_565;
            *jpegColorType = JCS_RGB;
            *numComponents = 3;
            return true;
        case kARGB_4444_SkColorType:
            *proc = transform_scanline_444;
            *jpegColorType = JCS_RGB;
            *numComponents = 3;
            return true;
        case kIndex_8_SkColorType:
            *proc = transform_scanline_index8_opaque;
            *jpegColorType = JCS_RGB;
            *numComponents = 3;
            return true;
        case kGray_8_SkColorType:
            SkASSERT(info.isOpaque());
            *jpegColorType = JCS_GRAYSCALE;
            *numComponents = 1;
            return true;
        case kRGBA_F16_SkColorType:
            if (!info.colorSpace() || !info.colorSpace()->gammaIsLinear()) {
                return false;
            }

            *proc = transform_scanline_F16_to_8888;
            *jpegColorType = JCS_EXT_RGBA;
            *numComponents = 4;
            return true;
        default:
            return false;
    }


}

bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeOptions& opts) {
    SkASSERT(!pixmap.colorSpace() || pixmap.colorSpace()->gammaCloseToSRGB() ||
            pixmap.colorSpace()->gammaIsLinear());

    SkPixmap src = pixmap;
    if (SkTransferFunctionBehavior::kIgnore == opts.fUnpremulBehavior) {
        src.setColorSpace(nullptr);
    } else {
        // kCorrect behavior requires a color space.  It's not actually critical in the
        // jpeg case (since jpegs are opaque), but Skia color correct behavior generally
        // requires pixels to be tagged with color spaces.
        if (!src.colorSpace()) {
            return false;
        }
    }

    return SkEncodeImageAsJPEG(stream, src, 100);
}

bool SkEncodeImageAsJPEG(SkWStream* stream, const SkPixmap& pixmap, int quality) {
    if (!pixmap.addr()) {
        return false;
    }
    jpeg_compress_struct    cinfo;
    skjpeg_error_mgr        sk_err;
    skjpeg_destination_mgr  sk_wstream(stream);

    // Declare before calling setjmp.
    SkAutoTMalloc<uint8_t>  storage;

    cinfo.err = jpeg_std_error(&sk_err);
    sk_err.error_exit = skjpeg_error_exit;
    if (setjmp(sk_err.fJmpBuf)) {
        return false;
    }

    J_COLOR_SPACE jpegColorSpace;
    int numComponents;
    transform_scanline_proc proc;
    if (!set_encode_config(&jpegColorSpace, &numComponents, &proc, pixmap.info())) {
        return false;
    }

    jpeg_create_compress(&cinfo);
    cinfo.dest = &sk_wstream;
    cinfo.image_width = pixmap.width();
    cinfo.image_height = pixmap.height();
    cinfo.input_components = numComponents;
    cinfo.in_color_space = jpegColorSpace;

    jpeg_set_defaults(&cinfo);

    // Tells libjpeg-turbo to compute optimal Huffman coding tables
    // for the image.  This improves compression at the cost of
    // slower encode performance.
    cinfo.optimize_coding = TRUE;
    jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

    jpeg_start_compress(&cinfo, TRUE);

    if (pixmap.colorSpace()) {
        sk_sp<SkData> icc = icc_from_color_space(*pixmap.colorSpace());
        if (icc) {
            // Create a contiguous block of memory with the icc signature followed by the profile.
            sk_sp<SkData> markerData =
                    SkData::MakeUninitialized(kICCMarkerHeaderSize + icc->size());
            uint8_t* ptr = (uint8_t*) markerData->writable_data();
            memcpy(ptr, kICCSig, sizeof(kICCSig));
            ptr += sizeof(kICCSig);
            *ptr++ = 1; // This is the first marker.
            *ptr++ = 1; // Out of one total markers.
            memcpy(ptr, icc->data(), icc->size());

            jpeg_write_marker(&cinfo, kICCMarker, markerData->bytes(), markerData->size());
        }
    }

    if (proc) {
        storage.reset(numComponents * pixmap.width());
    }

    const void* srcRow = pixmap.addr();
    const SkPMColor* colors = pixmap.ctable() ? pixmap.ctable()->readColors() : nullptr;
    while (cinfo.next_scanline < cinfo.image_height) {
        JSAMPLE* jpegSrcRow = (JSAMPLE*) srcRow;
        if (proc) {
            proc((char*)storage.get(), (const char*)srcRow, pixmap.width(), numComponents, colors);
            jpegSrcRow = storage.get();
        }

        (void) jpeg_write_scanlines(&cinfo, &jpegSrcRow, 1);
        srcRow = SkTAddOffset<const void>(srcRow, pixmap.rowBytes());
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    return true;
}
#endif