-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpg.c
117 lines (93 loc) · 3.09 KB
/
jpg.c
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
#include "jpg.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <setjmp.h>
#include <jpeglib.h>
#define LOG_MODULE "jpg"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "stride.h"
struct my_error_mgr {
struct jpeg_error_mgr mgr;
jmp_buf setjmp_buffer;
};
static void
error_exit(j_common_ptr cinfo)
{
struct my_error_mgr *err_handler = (struct my_error_mgr *)cinfo->err;
longjmp(err_handler->setjmp_buffer, 1);
}
pixman_image_t *
jpg_load(FILE *fp, const char *path)
{
struct jpeg_decompress_struct cinfo = {0};
struct my_error_mgr err_handler;
uint8_t *image_data = NULL;
pixman_image_t *pix = NULL;
if (fseek(fp, 0, SEEK_SET) < 0) {
LOG_ERRNO("%s: failed to seek to beginning of file", path);
return NULL;
}
cinfo.err = jpeg_std_error(&err_handler.mgr);
err_handler.mgr.error_exit = &error_exit;
if (setjmp(err_handler.setjmp_buffer)) {
char err_string[JMSG_LENGTH_MAX];
cinfo.err->format_message((j_common_ptr)&cinfo, err_string);
LOG_ERR("%s: %s", path, err_string);
goto err;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, true);
jpeg_calc_output_dimensions(&cinfo);
if (cinfo.output_components != 1 && cinfo.output_components != 3) {
LOG_ERR("%s: unsupported number of color components: %d",
path, cinfo.output_components);
goto err;
}
pixman_format_code_t format = PIXMAN_b8g8r8;
int width = cinfo.output_width;
int height = cinfo.output_height;
int stride = stride_for_format_and_width(format, width);
LOG_DBG("width=%d, height=%d, stride=%d, components=%d",
width, height, stride, cinfo.output_components);
image_data = malloc(height * stride);
if (image_data == NULL)
goto err;
jpeg_start_decompress(&cinfo);
for (int row_no = 0; cinfo.output_scanline < height; row_no++) {
uint8_t *row = &image_data[row_no * stride];
if (cinfo.output_components == 3) {
/* Read directly info our to-be pixman image buffer */
jpeg_read_scanlines(&cinfo, (JSAMPARRAY)&row, 1);
}
else {
/* Read into temporary buffer, then expand to rgb */
assert(cinfo.output_components == 1);
uint8_t buf[cinfo.output_width];
uint8_t *scanline = &buf[0];
jpeg_read_scanlines(&cinfo, (JSAMPARRAY)&scanline, 1);
for (size_t i = 0; i < cinfo.output_width; i++) {
row[i * 3 + 0] = buf[i];
row[i * 3 + 1] = buf[i];
row[i * 3 + 2] = buf[i];
}
}
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
pix = pixman_image_create_bits_no_clear(
format, width, height, (uint32_t *)image_data, stride);
if (pix == NULL) {
LOG_ERR("%s: failed to instantiate pixman image", path);
goto err;
}
return pix;
err:
if (pix != NULL)
pixman_image_unref(pix);
free(image_data);
jpeg_destroy_decompress(&cinfo);
return NULL;
}