2
This file is part of darktable,
3
copyright (c) 2009--2010 johannes hanika.
5
darktable is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
10
darktable is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with darktable. If not, see <http://www.gnu.org/licenses/>.
24
#include "common/colorspaces.h"
25
#include "common/imageio_jpeg.h"
30
struct dt_imageio_jpeg_error_mgr
32
struct jpeg_error_mgr pub;
33
jmp_buf setjmp_buffer;
35
dt_imageio_jpeg_error_mgr;
37
typedef struct dt_imageio_jpeg_error_mgr *dt_imageio_jpeg_error_ptr;
39
void dt_imageio_jpeg_error_exit (j_common_ptr cinfo)
41
dt_imageio_jpeg_error_ptr myerr = (dt_imageio_jpeg_error_ptr) cinfo->err;
42
(*cinfo->err->output_message) (cinfo);
43
longjmp(myerr->setjmp_buffer, 1);
46
// destination functions
47
void dt_imageio_jpeg_init_destination(j_compress_ptr cinfo) {}
48
boolean dt_imageio_jpeg_empty_output_buffer(j_compress_ptr cinfo)
50
fprintf(stderr, "[imageio_jpeg] output buffer full!\n");
53
void dt_imageio_jpeg_term_destination(j_compress_ptr cinfo) {}
56
void dt_imageio_jpeg_init_source(j_decompress_ptr cinfo) {}
57
boolean dt_imageio_jpeg_fill_input_buffer(j_decompress_ptr cinfo)
61
void dt_imageio_jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
63
int i = cinfo->src->bytes_in_buffer - num_bytes;
65
cinfo->src->bytes_in_buffer = i;
66
cinfo->src->next_input_byte += num_bytes;
68
void dt_imageio_jpeg_term_source(j_decompress_ptr cinfo) {}
71
int dt_imageio_jpeg_decompress_header(const void *in, size_t length, dt_imageio_jpeg_t *jpg)
73
jpeg_create_decompress(&(jpg->dinfo));
74
jpg->src.init_source = dt_imageio_jpeg_init_source;
75
jpg->src.fill_input_buffer = dt_imageio_jpeg_fill_input_buffer;
76
jpg->src.skip_input_data = dt_imageio_jpeg_skip_input_data;
77
jpg->src.resync_to_restart = jpeg_resync_to_restart;
78
jpg->src.term_source = dt_imageio_jpeg_term_source;
79
jpg->src.next_input_byte = (JOCTET*)in;
80
jpg->src.bytes_in_buffer = length;
82
struct dt_imageio_jpeg_error_mgr jerr;
83
jpg->dinfo.err = jpeg_std_error(&jerr.pub);
84
jerr.pub.error_exit = dt_imageio_jpeg_error_exit;
85
if (setjmp(jerr.setjmp_buffer))
87
jpeg_destroy_decompress(&(jpg->dinfo));
91
jpg->dinfo.src = &(jpg->src);
92
// jpg->dinfo.buffered_image = TRUE;
93
jpeg_read_header(&(jpg->dinfo), TRUE);
94
jpg->width = jpg->dinfo.image_width;
95
jpg->height = jpg->dinfo.image_height;
99
int dt_imageio_jpeg_decompress(dt_imageio_jpeg_t *jpg, uint8_t *out)
101
struct dt_imageio_jpeg_error_mgr jerr;
102
jpg->dinfo.err = jpeg_std_error(&jerr.pub);
103
if (setjmp(jerr.setjmp_buffer))
105
jpeg_destroy_decompress(&(jpg->dinfo));
108
(void)jpeg_start_decompress(&(jpg->dinfo));
109
JSAMPROW row_pointer[1];
110
row_pointer[0] = (uint8_t *)malloc(jpg->dinfo.output_width*jpg->dinfo.num_components);
112
while(jpg->dinfo.output_scanline < jpg->dinfo.image_height)
114
if(jpeg_read_scanlines(&(jpg->dinfo), row_pointer, 1) != 1)
116
free(row_pointer[0]);
119
for(int i=0; i<jpg->dinfo.image_width; i++) for(int k=0; k<3; k++)
120
tmp[4*i+k] = row_pointer[0][3*i+k];
123
// jpg->dinfo.src = NULL;
124
// (void)jpeg_finish_decompress(&(jpg->dinfo)); // ???
125
jpeg_destroy_decompress(&(jpg->dinfo));
126
free(row_pointer[0]);
130
int dt_imageio_jpeg_compress(const uint8_t *in, uint8_t *out, const int width, const int height, const int quality)
132
struct dt_imageio_jpeg_error_mgr jerr;
133
dt_imageio_jpeg_t jpg;
134
jpg.dest.init_destination = dt_imageio_jpeg_init_destination;
135
jpg.dest.empty_output_buffer= dt_imageio_jpeg_empty_output_buffer;
136
jpg.dest.term_destination = dt_imageio_jpeg_term_destination;
137
jpg.dest.next_output_byte = (JOCTET *)out;
138
jpg.dest.free_in_buffer = 4*width*height*sizeof(uint8_t);
140
jpg.cinfo.err = jpeg_std_error(&jerr.pub);
141
jerr.pub.error_exit = dt_imageio_jpeg_error_exit;
142
if (setjmp(jerr.setjmp_buffer))
144
jpeg_destroy_compress(&(jpg.cinfo));
147
jpeg_create_compress(&(jpg.cinfo));
148
jpg.cinfo.dest = &(jpg.dest);
150
jpg.cinfo.image_width = width;
151
jpg.cinfo.image_height = height;
152
jpg.cinfo.input_components = 3;
153
jpg.cinfo.in_color_space = JCS_RGB;
154
jpeg_set_defaults(&(jpg.cinfo));
155
jpeg_set_quality(&(jpg.cinfo), quality, TRUE);
156
if(quality > 90) jpg.cinfo.comp_info[0].v_samp_factor = 1;
157
if(quality > 92) jpg.cinfo.comp_info[0].h_samp_factor = 1;
158
jpeg_start_compress(&(jpg.cinfo), TRUE);
159
uint8_t row[3*width];
161
while(jpg.cinfo.next_scanline < jpg.cinfo.image_height)
164
buf = in + jpg.cinfo.next_scanline * jpg.cinfo.image_width * 4;
165
for(int i=0; i<width; i++) for(int k=0; k<3; k++) row[3*i+k] = buf[4*i+k];
167
jpeg_write_scanlines(&(jpg.cinfo), tmp, 1);
169
jpeg_finish_compress (&(jpg.cinfo));
170
jpeg_destroy_compress(&(jpg.cinfo));
171
return 4*width*height*sizeof(uint8_t) - jpg.dest.free_in_buffer;
175
* Since an ICC profile can be larger than the maximum size of a JPEG marker
176
* (64K), we need provisions to split it into multiple markers. The format
177
* defined by the ICC specifies one or more APP2 markers containing the
179
* Identifying string ASCII "ICC_PROFILE\0" (12 bytes)
180
* Marker sequence number 1 for first APP2, 2 for next, etc (1 byte)
181
* Number of markers Total number of APP2's used (1 byte)
182
* Profile data (remainder of APP2 data)
183
* Decoders should use the marker sequence numbers to reassemble the profile,
184
* rather than assuming that the APP2 markers appear in the correct sequence.
187
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
188
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
189
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */
190
#define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
194
* This routine writes the given ICC profile data into a JPEG file.
195
* It *must* be called AFTER calling jpeg_start_compress() and BEFORE
196
* the first call to jpeg_write_scanlines().
197
* (This ordering ensures that the APP2 marker(s) will appear after the
198
* SOI and JFIF or Adobe markers, but before all else.)
202
write_icc_profile (j_compress_ptr cinfo,
203
const JOCTET *icc_data_ptr,
204
unsigned int icc_data_len)
206
unsigned int num_markers; /* total number of markers we'll write */
207
int cur_marker = 1; /* per spec, counting starts at 1 */
208
unsigned int length; /* number of bytes to write in this marker */
210
/* Calculate the number of markers we'll need, rounding up of course */
211
num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER;
212
if (num_markers * MAX_DATA_BYTES_IN_MARKER != icc_data_len)
215
while (icc_data_len > 0)
217
/* length of profile to put in this marker */
218
length = icc_data_len;
219
if (length > MAX_DATA_BYTES_IN_MARKER)
220
length = MAX_DATA_BYTES_IN_MARKER;
221
icc_data_len -= length;
223
/* Write the JPEG marker header (APP2 code and marker length) */
224
jpeg_write_m_header(cinfo, ICC_MARKER,
225
(unsigned int) (length + ICC_OVERHEAD_LEN));
227
/* Write the marker identifying string "ICC_PROFILE" (null-terminated).
228
* We code it in this less-than-transparent way so that the code works
229
* even if the local character set is not ASCII.
231
jpeg_write_m_byte(cinfo, 0x49);
232
jpeg_write_m_byte(cinfo, 0x43);
233
jpeg_write_m_byte(cinfo, 0x43);
234
jpeg_write_m_byte(cinfo, 0x5F);
235
jpeg_write_m_byte(cinfo, 0x50);
236
jpeg_write_m_byte(cinfo, 0x52);
237
jpeg_write_m_byte(cinfo, 0x4F);
238
jpeg_write_m_byte(cinfo, 0x46);
239
jpeg_write_m_byte(cinfo, 0x49);
240
jpeg_write_m_byte(cinfo, 0x4C);
241
jpeg_write_m_byte(cinfo, 0x45);
242
jpeg_write_m_byte(cinfo, 0x0);
244
/* Add the sequencing info */
245
jpeg_write_m_byte(cinfo, cur_marker);
246
jpeg_write_m_byte(cinfo, (int) num_markers);
248
/* Add the profile data */
251
jpeg_write_m_byte(cinfo, *icc_data_ptr);
261
* Prepare for reading an ICC profile
265
setup_read_icc_profile (j_decompress_ptr cinfo)
267
/* Tell the library to keep any APP2 data it may find */
268
jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
273
* Handy subroutine to test whether a saved marker is an ICC profile marker.
277
marker_is_icc (jpeg_saved_marker_ptr marker)
280
marker->marker == ICC_MARKER &&
281
marker->data_length >= ICC_OVERHEAD_LEN &&
282
/* verify the identifying string */
283
GETJOCTET(marker->data[0]) == 0x49 &&
284
GETJOCTET(marker->data[1]) == 0x43 &&
285
GETJOCTET(marker->data[2]) == 0x43 &&
286
GETJOCTET(marker->data[3]) == 0x5F &&
287
GETJOCTET(marker->data[4]) == 0x50 &&
288
GETJOCTET(marker->data[5]) == 0x52 &&
289
GETJOCTET(marker->data[6]) == 0x4F &&
290
GETJOCTET(marker->data[7]) == 0x46 &&
291
GETJOCTET(marker->data[8]) == 0x49 &&
292
GETJOCTET(marker->data[9]) == 0x4C &&
293
GETJOCTET(marker->data[10]) == 0x45 &&
294
GETJOCTET(marker->data[11]) == 0x0;
299
* See if there was an ICC profile in the JPEG file being read;
300
* if so, reassemble and return the profile data.
302
* TRUE is returned if an ICC profile was found, FALSE if not.
303
* If TRUE is returned, *icc_data_ptr is set to point to the
304
* returned data, and *icc_data_len is set to its length.
306
* IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
307
* and must be freed by the caller with free() when the caller no longer
308
* needs it. (Alternatively, we could write this routine to use the
309
* IJG library's memory allocator, so that the data would be freed implicitly
310
* at jpeg_finish_decompress() time. But it seems likely that many apps
311
* will prefer to have the data stick around after decompression finishes.)
313
* NOTE: if the file contains invalid ICC APP2 markers, we just silently
314
* return FALSE. You might want to issue an error message instead.
318
read_icc_profile (j_decompress_ptr cinfo,
319
JOCTET **icc_data_ptr,
320
unsigned int *icc_data_len)
322
jpeg_saved_marker_ptr marker;
326
unsigned int total_length;
327
#define MAX_SEQ_NO 255 /* sufficient since marker numbers are bytes */
328
char marker_present[MAX_SEQ_NO+1]; /* 1 if marker found */
329
unsigned int data_length[MAX_SEQ_NO+1]; /* size of profile data in marker */
330
unsigned int data_offset[MAX_SEQ_NO+1]; /* offset for data in marker */
332
*icc_data_ptr = NULL; /* avoid confusion if FALSE return */
335
/* This first pass over the saved markers discovers whether there are
336
* any ICC markers and verifies the consistency of the marker numbering.
339
for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
340
marker_present[seq_no] = 0;
342
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next)
344
if (marker_is_icc(marker))
346
if (num_markers == 0)
347
num_markers = GETJOCTET(marker->data[13]);
348
else if (num_markers != GETJOCTET(marker->data[13]))
349
return FALSE; /* inconsistent num_markers fields */
350
seq_no = GETJOCTET(marker->data[12]);
351
if (seq_no <= 0 || seq_no > num_markers)
352
return FALSE; /* bogus sequence number */
353
if (marker_present[seq_no])
354
return FALSE; /* duplicate sequence numbers */
355
marker_present[seq_no] = 1;
356
data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
360
if (num_markers == 0)
363
/* Check for missing markers, count total space needed,
364
* compute offset of each marker's part of the data.
368
for (seq_no = 1; seq_no <= num_markers; seq_no++)
370
if (marker_present[seq_no] == 0)
371
return FALSE; /* missing sequence number */
372
data_offset[seq_no] = total_length;
373
total_length += data_length[seq_no];
376
if (total_length <= 0)
377
return FALSE; /* found only empty markers? */
379
/* Allocate space for assembled data */
380
icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
381
if (icc_data == NULL)
382
return FALSE; /* oops, out of memory */
385
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next)
387
if (marker_is_icc(marker))
392
seq_no = GETJOCTET(marker->data[12]);
393
dst_ptr = icc_data + data_offset[seq_no];
394
src_ptr = marker->data + ICC_OVERHEAD_LEN;
395
length = data_length[seq_no];
398
*dst_ptr++ = *src_ptr++;
403
*icc_data_ptr = icc_data;
404
*icc_data_len = total_length;
410
#undef ICC_OVERHEAD_LEN
411
#undef MAX_BYTES_IN_MARKER
412
#undef MAX_DATA_BYTES_IN_MARKER
416
int dt_imageio_jpeg_write_with_icc_profile(const char *filename, const uint8_t *in, const int width, const int height, const int quality, void *exif, int exif_len, int imgid)
418
struct dt_imageio_jpeg_error_mgr jerr;
419
dt_imageio_jpeg_t jpg;
421
jpg.cinfo.err = jpeg_std_error(&jerr.pub);
422
jerr.pub.error_exit = dt_imageio_jpeg_error_exit;
423
if (setjmp(jerr.setjmp_buffer))
425
jpeg_destroy_compress(&(jpg.cinfo));
428
jpeg_create_compress(&(jpg.cinfo));
429
FILE *f = fopen(filename, "wb");
431
jpeg_stdio_dest(&(jpg.cinfo), f);
433
jpg.cinfo.image_width = width;
434
jpg.cinfo.image_height = height;
435
jpg.cinfo.input_components = 3;
436
jpg.cinfo.in_color_space = JCS_RGB;
437
jpeg_set_defaults(&(jpg.cinfo));
438
jpeg_set_quality(&(jpg.cinfo), quality, TRUE);
439
if(quality > 90) jpg.cinfo.comp_info[0].v_samp_factor = 1;
440
if(quality > 92) jpg.cinfo.comp_info[0].h_samp_factor = 1;
441
jpeg_start_compress(&(jpg.cinfo), TRUE);
445
cmsHPROFILE out_profile = dt_colorspaces_create_output_profile(imgid);
447
cmsSaveProfileToMem(out_profile, 0, &len);
450
unsigned char buf[len];
451
cmsSaveProfileToMem(out_profile, buf, &len);
452
write_icc_profile(&(jpg.cinfo), buf, len);
454
dt_colorspaces_cleanup_profile(out_profile);
457
if(exif && exif_len > 0 && exif_len < 65534)
458
jpeg_write_marker(&(jpg.cinfo), JPEG_APP0+1, exif, exif_len);
460
uint8_t row[3*width];
462
while(jpg.cinfo.next_scanline < jpg.cinfo.image_height)
465
buf = in + jpg.cinfo.next_scanline * jpg.cinfo.image_width * 4;
466
for(int i=0; i<width; i++) for(int k=0; k<3; k++) row[3*i+k] = buf[4*i+k];
468
jpeg_write_scanlines(&(jpg.cinfo), tmp, 1);
470
jpeg_finish_compress (&(jpg.cinfo));
471
jpeg_destroy_compress(&(jpg.cinfo));
476
int dt_imageio_jpeg_write(const char *filename, const uint8_t *in, const int width, const int height, const int quality, void *exif, int exif_len)
478
return dt_imageio_jpeg_write_with_icc_profile(filename, in, width, height, quality, exif, exif_len, -1);
481
int dt_imageio_jpeg_read_header(const char *filename, dt_imageio_jpeg_t *jpg)
483
jpg->f = fopen(filename, "rb");
484
if(!jpg->f) return 1;
486
struct dt_imageio_jpeg_error_mgr jerr;
487
jpg->dinfo.err = jpeg_std_error(&jerr.pub);
488
jerr.pub.error_exit = dt_imageio_jpeg_error_exit;
489
if (setjmp(jerr.setjmp_buffer))
491
jpeg_destroy_decompress(&(jpg->dinfo));
495
jpeg_create_decompress(&(jpg->dinfo));
496
jpeg_stdio_src(&(jpg->dinfo), jpg->f);
497
// jpg->dinfo.buffered_image = TRUE;
498
jpeg_read_header(&(jpg->dinfo), TRUE);
499
jpg->width = jpg->dinfo.image_width;
500
jpg->height = jpg->dinfo.image_height;
504
int dt_imageio_jpeg_read(dt_imageio_jpeg_t *jpg, uint8_t *out)
506
struct dt_imageio_jpeg_error_mgr jerr;
507
jpg->dinfo.err = jpeg_std_error(&jerr.pub);
508
if (setjmp(jerr.setjmp_buffer))
510
jpeg_destroy_decompress(&(jpg->dinfo));
514
(void)jpeg_start_decompress(&(jpg->dinfo));
515
JSAMPROW row_pointer[1];
516
row_pointer[0] = (uint8_t *)malloc(jpg->dinfo.output_width*jpg->dinfo.num_components);
518
while(jpg->dinfo.output_scanline < jpg->dinfo.image_height)
520
if(jpeg_read_scanlines(&(jpg->dinfo), row_pointer, 1) != 1)
522
jpeg_destroy_decompress(&(jpg->dinfo));
523
free(row_pointer[0]);
527
if(jpg->dinfo.num_components < 3)
528
for(int i=0; i<jpg->dinfo.image_width; i++) for(int k=0; k<3; k++)
529
tmp[4*i+k] = row_pointer[0][jpg->dinfo.num_components*i+0];
531
for(int i=0; i<jpg->dinfo.image_width; i++) for(int k=0; k<3; k++)
532
tmp[4*i+k] = row_pointer[0][3*i+k];
535
// (void)jpeg_finish_decompress(&(jpg->dinfo));
536
jpeg_destroy_decompress(&(jpg->dinfo));
537
free(row_pointer[0]);