1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <setjmp.h>
19#include <stdio.h>
20
21extern "C" {
22#include "jpeglib.h"
23}
24
25static inline uint8_t from565to8(uint16_t p, int start, int bits) {
26    uint8_t c = (p >> start) & ((1 << bits) - 1);
27    return (c << (8 - bits)) | (c >> (bits - (8 - bits)));
28}
29
30struct sf_jpeg_error_mgr {
31    struct jpeg_error_mgr jerr;
32    jmp_buf longjmp_buffer;
33};
34
35void sf_jpeg_error_exit(j_common_ptr cinfo) {
36    struct sf_jpeg_error_mgr *sf_err = (struct sf_jpeg_error_mgr *)cinfo->err;
37    longjmp(sf_err->longjmp_buffer, 0);
38}
39
40int writeJpegFile(const char *filename, uint8_t *frame, int width, int height) {
41    struct sf_jpeg_error_mgr sf_err;
42    struct jpeg_compress_struct cinfo;
43    uint8_t row_data[width * 3];
44    JSAMPROW row_pointer = row_data;
45    FILE *f;
46
47    f = fopen(filename, "w");
48    if (!f) {
49        return -errno;
50    }
51
52    cinfo.err = jpeg_std_error(&sf_err.jerr);
53    sf_err.jerr.error_exit = sf_jpeg_error_exit;
54    if (setjmp(sf_err.longjmp_buffer)) {
55        jpeg_destroy_compress(&cinfo);
56        fclose(f);
57        return -1;
58    }
59
60    jpeg_create_compress(&cinfo);
61    jpeg_stdio_dest(&cinfo, f);
62
63    cinfo.image_width = width;
64    cinfo.image_height = height;
65    cinfo.input_components = 3;
66    cinfo.in_color_space = JCS_RGB;
67
68    jpeg_set_defaults(&cinfo);
69    jpeg_set_quality(&cinfo, 80, TRUE);
70
71    jpeg_start_compress(&cinfo, TRUE);
72
73    for (int row = 0; row < height; row++) {
74        uint16_t *src = (uint16_t *)(frame + row * width * 2);
75        uint8_t *dst = row_data;
76        for (int col = 0; col < width; col++) {
77            dst[0] = from565to8(*src, 11, 5);
78            dst[1] = from565to8(*src, 5, 6);
79            dst[2] = from565to8(*src, 0, 5);
80            dst += 3;
81            src++;
82        }
83        jpeg_write_scanlines(&cinfo, &row_pointer, 1);
84    }
85
86    jpeg_finish_compress(&cinfo);
87    jpeg_destroy_compress(&cinfo);
88
89    fclose(f);
90    return 0;
91}
92