24
23
* Contributor(s): none yet.
26
25
* ***** END GPL LICENSE BLOCK *****
29
* $Id: jpeg.c 28651 2010-05-07 15:18:04Z blendix $
28
/** \file blender/imbuf/intern/jpeg.c
33
34
/* This little block needed for linking to Blender... */
58
59
static boolean fill_input_buffer(j_decompress_ptr cinfo);
59
60
static void skip_input_data(j_decompress_ptr cinfo, long num_bytes);
60
61
static void term_source(j_decompress_ptr cinfo);
61
static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, int size);
62
static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, size_t size);
62
63
static boolean handle_app1 (j_decompress_ptr cinfo);
63
64
static ImBuf * ibJpegImageFromCinfo(struct jpeg_decompress_struct * cinfo, int flags);
67
68
* In principle there are 4 jpeg formats.
69
70
* 1. jpeg - standard printing, u & v at quarter of resulution
70
71
* 2. jvid - standaard video, u & v half resolution, frame not interlaced
72
type 3 is unsupported as of jul 05 2000 Frank.
73
* type 3 is unsupported as of jul 05 2000 Frank.
74
75
* 3. jstr - as 2, but written in 2 separate fields
76
77
* 4. jmax - no scaling in the components
79
80
static int jpeg_default_quality;
80
81
static int ibuf_ftype;
82
int imb_is_a_jpeg(unsigned char *mem) {
83
int imb_is_a_jpeg(unsigned char *mem)
84
85
if ((mem[0]== 0xFF) && (mem[1] == 0xD8))return 1;
90
91
//----------------------------------------------------------
92
93
typedef struct my_error_mgr {
93
struct jpeg_error_mgr pub; /* "public" fields */
94
struct jpeg_error_mgr pub; /* "public" fields */
95
jmp_buf setjmp_buffer; /* for return to caller */
96
jmp_buf setjmp_buffer; /* for return to caller */
98
99
typedef my_error_mgr * my_error_ptr;
140
142
my_src_ptr src = (my_src_ptr) cinfo->src;
142
144
/* Since we have given all we have got already
143
* we simply fake an end of file
145
* we simply fake an end of file
146
148
src->pub.next_input_byte = src->terminal;
147
149
src->pub.bytes_in_buffer = 2;
157
159
my_src_ptr src = (my_src_ptr) cinfo->src;
159
src->pub.next_input_byte = src->pub.next_input_byte + num_bytes;
162
// prevent skipping over file end
163
size_t skip_size = (size_t)num_bytes <= src->pub.bytes_in_buffer ? num_bytes : src->pub.bytes_in_buffer;
165
src->pub.next_input_byte = src->pub.next_input_byte + skip_size;
166
src->pub.bytes_in_buffer = src->pub.bytes_in_buffer - skip_size;
163
171
static void term_source(j_decompress_ptr cinfo)
173
(void)cinfo; /* unused */
167
static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, int size)
176
static void memory_source(j_decompress_ptr cinfo, unsigned char *buffer, size_t size)
171
if (cinfo->src == NULL)
172
{ /* first time for this JPEG object? */
180
if (cinfo->src == NULL) { /* first time for this JPEG object? */
173
181
cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small)
174
182
((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_source_mgr));
178
186
src->pub.init_source = init_source;
179
187
src->pub.fill_input_buffer = fill_input_buffer;
180
188
src->pub.skip_input_data = skip_input_data;
181
src->pub.resync_to_restart = jpeg_resync_to_restart;
189
src->pub.resync_to_restart = jpeg_resync_to_restart;
182
190
src->pub.term_source = term_source;
184
192
src->pub.bytes_in_buffer = size;
210
218
* Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
211
219
* but we must reload the local copies after a successful fill.
213
#define MAKE_BYTE_AVAIL(cinfo,action) \
214
if (bytes_in_buffer == 0) { \
215
if (! (*datasrc->fill_input_buffer) (cinfo)) \
217
INPUT_RELOAD(cinfo); \
221
#define MAKE_BYTE_AVAIL(cinfo, action) \
222
if (bytes_in_buffer == 0) { \
223
if (! (*datasrc->fill_input_buffer) (cinfo)) \
225
INPUT_RELOAD(cinfo); \
221
229
/* Read a byte into variable V.
222
230
* If must suspend, take the specified action (typically "return FALSE").
224
232
#define INPUT_BYTE(cinfo,V,action) \
225
233
MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
226
235
V = GETJOCTET(*next_input_byte++); )
228
237
/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
231
240
#define INPUT_2BYTES(cinfo,V,action) \
232
241
MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
233
243
V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
234
244
MAKE_BYTE_AVAIL(cinfo,action); \
235
246
V += GETJOCTET(*next_input_byte++); )
239
250
handle_app1 (j_decompress_ptr cinfo)
252
INT32 length; /* initialized by the macro */
242
254
char neogeo[128];
244
256
INPUT_VARS(cinfo);
261
273
static ImBuf * ibJpegImageFromCinfo(struct jpeg_decompress_struct * cinfo, int flags)
263
275
JSAMPARRAY row_pointer;
264
JSAMPLE * buffer = 0;
276
JSAMPLE * buffer = NULL;
266
278
int x, y, depth, r, g, b, k;
267
struct ImBuf * ibuf = 0;
279
struct ImBuf * ibuf = NULL;
269
281
jpeg_saved_marker_ptr marker;
270
282
char *str, *key, *value;
295
307
if (flags & IB_test) {
296
308
jpeg_abort_decompress(cinfo);
297
ibuf = IMB_allocImBuf(x, y, 8 * depth, 0, 0);
299
ibuf = IMB_allocImBuf(x, y, 8 * depth, IB_rect, 0);
309
ibuf = IMB_allocImBuf(x, y, 8 * depth, 0);
311
else if ((ibuf = IMB_allocImBuf(x, y, 8 * depth, IB_rect)) == NULL) {
312
jpeg_abort_decompress(cinfo);
301
315
row_stride = cinfo->output_width * depth;
303
317
row_pointer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, row_stride, 1);
371
385
* That is why we need split it to the
372
386
* common key/value here.
374
if(strncmp((char *) marker->data, "Blender", 7)) {
388
if (strncmp((char *) marker->data, "Blender", 7)) {
376
390
* Maybe the file have text that
377
391
* we don't know "what it's", in that
420
434
jpeg_destroy((j_common_ptr) cinfo);
421
ibuf->ftype = ibuf_ftype;
422
ibuf->profile = IB_PROFILE_SRGB;
428
ImBuf * imb_ibJpegImageFromFilename (const char * filename, int flags)
430
struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
431
struct my_error_mgr jerr;
435
if ((infile = fopen(filename, "rb")) == NULL) return 0;
437
cinfo->err = jpeg_std_error(&jerr.pub);
438
jerr.pub.error_exit = jpeg_error;
440
/* Establish the setjmp return context for my_error_exit to use. */
441
if (setjmp(jerr.setjmp_buffer)) {
442
/* If we get here, the JPEG code has signaled an error.
443
* We need to clean up the JPEG object, close the input file, and return.
445
jpeg_destroy_decompress(cinfo);
450
jpeg_create_decompress(cinfo);
451
jpeg_stdio_src(cinfo, infile);
453
ibuf = ibJpegImageFromCinfo(cinfo, flags);
459
ImBuf * imb_load_jpeg (unsigned char * buffer, int size, int flags)
461
struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
462
struct my_error_mgr jerr;
465
if(!imb_is_a_jpeg(buffer)) return NULL;
436
ibuf->ftype = ibuf_ftype;
437
ibuf->profile = IB_PROFILE_SRGB;
444
ImBuf * imb_load_jpeg (unsigned char * buffer, size_t size, int flags)
446
struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
447
struct my_error_mgr jerr;
450
if (!imb_is_a_jpeg(buffer)) return NULL;
467
452
cinfo->err = jpeg_std_error(&jerr.pub);
468
453
jerr.pub.error_exit = jpeg_error;
503
488
memcpy(neogeo + 6, &ibuf_ftype, 4);
504
489
jpeg_write_marker(cinfo, 0xe1, (JOCTET*) neogeo, 10);
491
if (ibuf->metadata) {
507
492
/* key + max value + "Blender" */
508
493
text= MEM_mallocN(530, "stamp info read");
509
494
iptr= ibuf->metadata;
511
496
if (!strcmp (iptr->key, "None")) {
512
497
jpeg_write_marker(cinfo, JPEG_COM, (JOCTET *) iptr->value, strlen (iptr->value) + 1);
513
498
goto next_stamp_info;
535
520
cinfo->input_components *
536
521
cinfo->image_width, "jpeg row_pointer");
538
for(y = ibuf->y - 1; y >= 0; y--){
523
for (y = ibuf->y - 1; y >= 0; y--) {
539
524
rect = (uchar *) (ibuf->rect + y * ibuf->x);
540
525
buffer = row_pointer[0];
542
switch(cinfo->in_color_space){
544
for (x = 0; x < ibuf->x; x++) {
552
for (x = 0; x < ibuf->x; x++) {
558
memcpy(buffer, rect, 4 * ibuf->x);
560
/* default was missing... intentional ? */
527
switch(cinfo->in_color_space) {
529
for (x = 0; x < ibuf->x; x++) {
537
for (x = 0; x < ibuf->x; x++) {
543
memcpy(buffer, rect, 4 * ibuf->x);
545
/* default was missing... intentional ? */
565
550
jpeg_write_scanlines(cinfo, row_pointer, 1);
585
570
cinfo->image_height = ibuf->y;
587
572
cinfo->in_color_space = JCS_RGB;
588
if (ibuf->depth == 8) cinfo->in_color_space = JCS_GRAYSCALE;
589
if (ibuf->depth == 32) cinfo->in_color_space = JCS_UNKNOWN;
591
switch(cinfo->in_color_space){
593
cinfo->input_components = 3;
596
cinfo->input_components = 1;
599
cinfo->input_components = 4;
601
/* default was missing... intentional ? */
573
if (ibuf->planes == 8) cinfo->in_color_space = JCS_GRAYSCALE;
575
/* just write RGBA as RGB,
576
* unsupported feature only confuses other s/w */
578
if (ibuf->planes == 32) cinfo->in_color_space = JCS_UNKNOWN;
580
switch(cinfo->in_color_space) {
582
cinfo->input_components = 3;
585
cinfo->input_components = 1;
588
cinfo->input_components = 4;
590
/* default was missing... intentional ? */
605
594
jpeg_set_defaults(cinfo);
616
static int save_stdjpeg(char * name, struct ImBuf * ibuf)
605
static int save_stdjpeg(const char *name, struct ImBuf *ibuf)
619
608
struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
620
609
struct my_error_mgr jerr;
622
if ((outfile = fopen(name, "wb")) == NULL) return 0;
611
if ((outfile = BLI_fopen(name, "wb")) == NULL) return 0;
623
612
jpeg_default_quality = 75;
625
614
cinfo->err = jpeg_std_error(&jerr.pub);
650
static int save_vidjpeg(char * name, struct ImBuf * ibuf)
639
static int save_vidjpeg(const char *name, struct ImBuf *ibuf)
653
642
struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
654
643
struct my_error_mgr jerr;
656
if ((outfile = fopen(name, "wb")) == NULL) return 0;
645
if ((outfile = BLI_fopen(name, "wb")) == NULL) return 0;
657
646
jpeg_default_quality = 90;
659
648
cinfo->err = jpeg_std_error(&jerr.pub);
689
static int save_jstjpeg(char * name, struct ImBuf * ibuf)
678
static int save_jstjpeg(const char *name, struct ImBuf *ibuf)
691
680
char fieldname[1024];
692
681
struct ImBuf * tbuf;
693
682
int oldy, returnval;
695
tbuf = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 24, IB_rect, 0);
684
tbuf = IMB_allocImBuf(ibuf->x, ibuf->y / 2, 24, IB_rect);
696
685
tbuf->ftype = ibuf->ftype;
697
686
tbuf->flags = ibuf->flags;
703
692
IMB_rectcpy(tbuf, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
704
693
sprintf(fieldname, "%s.jf0", name);
706
returnval = save_vidjpeg(fieldname, tbuf) ;
707
if (returnval == 1) {
695
returnval = save_vidjpeg(fieldname, tbuf);
696
if (returnval == 1) {
708
697
IMB_rectcpy(tbuf, ibuf, 0, 0, tbuf->x, 0, ibuf->x, ibuf->y);
709
698
sprintf(fieldname, "%s.jf1", name);
710
699
returnval = save_vidjpeg(fieldname, tbuf);
717
706
return returnval;
720
static int save_maxjpeg(char * name, struct ImBuf * ibuf)
709
static int save_maxjpeg(const char *name, struct ImBuf *ibuf)
723
712
struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
724
713
struct my_error_mgr jerr;
726
if ((outfile = fopen(name, "wb")) == NULL) return 0;
715
if ((outfile = BLI_fopen(name, "wb")) == NULL) return 0;
727
716
jpeg_default_quality = 100;
729
718
cinfo->err = jpeg_std_error(&jerr.pub);