~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/imbuf/intern/jpeg.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
 
1
/*
3
2
 * ***** BEGIN GPL LICENSE BLOCK *****
4
3
 *
5
4
 * This program is free software; you can redistribute it and/or
24
23
 * Contributor(s): none yet.
25
24
 *
26
25
 * ***** END GPL LICENSE BLOCK *****
27
 
 * jpeg.c
28
 
 *
29
 
 * $Id: jpeg.c 28651 2010-05-07 15:18:04Z blendix $
30
 
 */
 
26
 */
 
27
 
 
28
/** \file blender/imbuf/intern/jpeg.c
 
29
 *  \ingroup imbuf
 
30
 */
 
31
 
31
32
 
32
33
 
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);
64
65
 
65
66
 
66
67
/*
67
68
 * In principle there are 4 jpeg formats.
68
 
 * 
 
69
 *
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
71
 
 
72
 
type 3 is unsupported as of jul 05 2000 Frank.
73
 
 
 
72
 *
 
73
 * type 3 is unsupported as of jul 05 2000 Frank.
 
74
 *
74
75
 * 3. jstr - as 2, but written in 2 separate fields
75
 
 
 
76
 *
76
77
 * 4. jmax - no scaling in the components
77
78
 */
78
79
 
79
80
static int jpeg_default_quality;
80
81
static int ibuf_ftype;
81
82
 
82
 
int imb_is_a_jpeg(unsigned char *mem) {
83
 
 
 
83
int imb_is_a_jpeg(unsigned char *mem)
 
84
{
84
85
        if ((mem[0]== 0xFF) && (mem[1] == 0xD8))return 1;
85
86
        return 0;
86
87
}
90
91
//----------------------------------------------------------
91
92
 
92
93
typedef struct my_error_mgr {
93
 
  struct jpeg_error_mgr pub;    /* "public" fields */
 
94
        struct jpeg_error_mgr pub;      /* "public" fields */
94
95
 
95
 
  jmp_buf setjmp_buffer;        /* for return to caller */
 
96
        jmp_buf setjmp_buffer;  /* for return to caller */
96
97
} my_error_mgr;
97
98
 
98
99
typedef my_error_mgr * my_error_ptr;
132
133
 
133
134
static void init_source(j_decompress_ptr cinfo)
134
135
{
 
136
        (void)cinfo; /* unused */
135
137
}
136
138
 
137
139
 
140
142
        my_src_ptr src = (my_src_ptr) cinfo->src;
141
143
 
142
144
        /* Since we have given all we have got already
143
 
        * we simply fake an end of file
144
 
        */
 
145
         * we simply fake an end of file
 
146
         */
145
147
 
146
148
        src->pub.next_input_byte = src->terminal;
147
149
        src->pub.bytes_in_buffer = 2;
156
158
{
157
159
        my_src_ptr src = (my_src_ptr) cinfo->src;
158
160
 
159
 
        src->pub.next_input_byte = src->pub.next_input_byte + num_bytes;
 
161
        if (num_bytes > 0) {
 
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;
 
164
 
 
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;
 
167
        }
160
168
}
161
169
 
162
170
 
163
171
static void term_source(j_decompress_ptr cinfo)
164
172
{
 
173
        (void)cinfo; /* unused */
165
174
}
166
175
 
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)
168
177
{
169
178
        my_src_ptr src;
170
179
 
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));
175
183
        }
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;
183
191
 
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.
212
220
 */
213
 
#define MAKE_BYTE_AVAIL(cinfo,action)  \
214
 
        if (bytes_in_buffer == 0) {  \
215
 
                if (! (*datasrc->fill_input_buffer) (cinfo))  \
216
 
                        { action; }  \
217
 
                  INPUT_RELOAD(cinfo);  \
218
 
        }  \
219
 
        bytes_in_buffer--
 
221
#define MAKE_BYTE_AVAIL(cinfo, action)                                        \
 
222
        if (bytes_in_buffer == 0) {                                               \
 
223
                if (! (*datasrc->fill_input_buffer) (cinfo))                          \
 
224
                        { action; }                                                       \
 
225
                INPUT_RELOAD(cinfo);  \
 
226
        }
 
227
 
220
228
 
221
229
/* Read a byte into variable V.
222
230
 * If must suspend, take the specified action (typically "return FALSE").
223
231
 */
224
232
#define INPUT_BYTE(cinfo,V,action)  \
225
233
        MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
 
234
                  bytes_in_buffer--; \
226
235
                  V = GETJOCTET(*next_input_byte++); )
227
236
 
228
237
/* As above, but read two bytes interpreted as an unsigned 16-bit integer.
230
239
 */
231
240
#define INPUT_2BYTES(cinfo,V,action)  \
232
241
        MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
 
242
                  bytes_in_buffer--; \
233
243
                  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
234
244
                  MAKE_BYTE_AVAIL(cinfo,action); \
 
245
                  bytes_in_buffer--; \
235
246
                  V += GETJOCTET(*next_input_byte++); )
236
247
 
237
248
 
238
249
static boolean
239
250
handle_app1 (j_decompress_ptr cinfo)
240
251
{
241
 
        INT32 length, i;
 
252
        INT32 length; /* initialized by the macro */
 
253
        INT32 i;
242
254
        char neogeo[128];
243
255
        
244
256
        INPUT_VARS(cinfo);
261
273
static ImBuf * ibJpegImageFromCinfo(struct jpeg_decompress_struct * cinfo, int flags)
262
274
{
263
275
        JSAMPARRAY row_pointer;
264
 
        JSAMPLE * buffer = 0;
 
276
        JSAMPLE * buffer = NULL;
265
277
        int row_stride;
266
278
        int x, y, depth, r, g, b, k;
267
 
        struct ImBuf * ibuf = 0;
 
279
        struct ImBuf * ibuf = NULL;
268
280
        uchar * rect;
269
281
        jpeg_saved_marker_ptr marker;
270
282
        char *str, *key, *value;
294
306
 
295
307
                if (flags & IB_test) {
296
308
                        jpeg_abort_decompress(cinfo);
297
 
                        ibuf = IMB_allocImBuf(x, y, 8 * depth, 0, 0);
298
 
                } else {
299
 
                        ibuf = IMB_allocImBuf(x, y, 8 * depth, IB_rect, 0);
300
 
 
 
309
                        ibuf = IMB_allocImBuf(x, y, 8 * depth, 0);
 
310
                }
 
311
                else if ((ibuf = IMB_allocImBuf(x, y, 8 * depth, IB_rect)) == NULL) {
 
312
                        jpeg_abort_decompress(cinfo);
 
313
                }
 
314
                else {
301
315
                        row_stride = cinfo->output_width * depth;
302
316
 
303
317
                        row_pointer = (*cinfo->mem->alloc_sarray) ((j_common_ptr) cinfo, JPOOL_IMAGE, row_stride, 1);
314
328
                                                        rect[0] = rect[1] = rect[2] = *buffer++;
315
329
                                                        rect += 4;
316
330
                                                }
317
 
                                                        break;
 
331
                                                break;
318
332
                                        case 3:
319
333
                                                for (x=ibuf->x; x >0; x--) {
320
334
                                                        rect[3] = 255;
323
337
                                                        rect[2] = *buffer++;
324
338
                                                        rect += 4;
325
339
                                                }
326
 
                                                        break;
 
340
                                                break;
327
341
                                        case 4:
328
342
                                                for (x=ibuf->x; x >0; x--) {
329
343
                                                        r = *buffer++;
346
360
                                                        if (b & 0xffffff00) {
347
361
                                                                if (b < 0) b = 0;
348
362
                                                                else b = 255;
349
 
                                                        }                                                       
 
363
                                                        }
350
364
                                                        
351
365
                                                        rect[3] = 255 - k;
352
366
                                                        rect[2] = b;
358
372
                        }
359
373
 
360
374
                        marker= cinfo->marker_list;
361
 
                        while(marker) {
362
 
                                if(marker->marker != JPEG_COM)
 
375
                        while (marker) {
 
376
                                if (marker->marker != JPEG_COM)
363
377
                                        goto next_stamp_marker;
364
378
 
365
379
                                /*
371
385
                                 * That is why we need split it to the
372
386
                                 * common key/value here.
373
387
                                 */
374
 
                                if(strncmp((char *) marker->data, "Blender", 7)) {
 
388
                                if (strncmp((char *) marker->data, "Blender", 7)) {
375
389
                                        /*
376
390
                                         * Maybe the file have text that
377
391
                                         * we don't know "what it's", in that
391
405
                                /*
392
406
                                 * A little paranoid, but the file maybe
393
407
                                 * is broken... and a "extra" check is better
394
 
                                 * that a segfaul ;)
 
408
                                 * then segfault ;)
395
409
                                 */
396
410
                                if (!key) {
397
411
                                        MEM_freeN(str);
418
432
                }
419
433
                
420
434
                jpeg_destroy((j_common_ptr) cinfo);
421
 
                ibuf->ftype = ibuf_ftype;
422
 
                ibuf->profile = IB_PROFILE_SRGB;
423
 
        }
424
 
        
425
 
        return(ibuf);
426
 
}
427
 
 
428
 
ImBuf * imb_ibJpegImageFromFilename (const char * filename, int flags)
429
 
{
430
 
        struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
431
 
        struct my_error_mgr jerr;
432
 
        FILE * infile;
433
 
        ImBuf * ibuf;
434
 
        
435
 
        if ((infile = fopen(filename, "rb")) == NULL) return 0;
436
 
 
437
 
        cinfo->err = jpeg_std_error(&jerr.pub);
438
 
        jerr.pub.error_exit = jpeg_error;
439
 
 
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.
444
 
                 */
445
 
                jpeg_destroy_decompress(cinfo);
446
 
                fclose(infile);
447
 
                return NULL;
448
 
        }
449
 
 
450
 
        jpeg_create_decompress(cinfo);
451
 
        jpeg_stdio_src(cinfo, infile);
452
 
 
453
 
        ibuf = ibJpegImageFromCinfo(cinfo, flags);
454
 
        
455
 
        fclose(infile);
456
 
        return(ibuf);
457
 
}
458
 
 
459
 
ImBuf * imb_load_jpeg (unsigned char * buffer, int size, int flags)
460
 
{
461
 
        struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
462
 
        struct my_error_mgr jerr;
463
 
        ImBuf * ibuf;
464
 
 
465
 
        if(!imb_is_a_jpeg(buffer)) return NULL;
 
435
                if (ibuf) {
 
436
                        ibuf->ftype = ibuf_ftype;
 
437
                        ibuf->profile = IB_PROFILE_SRGB;
 
438
                }
 
439
        }
 
440
 
 
441
        return(ibuf);
 
442
}
 
443
 
 
444
ImBuf * imb_load_jpeg (unsigned char * buffer, size_t size, int flags)
 
445
{
 
446
        struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo;
 
447
        struct my_error_mgr jerr;
 
448
        ImBuf * ibuf;
 
449
 
 
450
        if (!imb_is_a_jpeg(buffer)) return NULL;
466
451
        
467
452
        cinfo->err = jpeg_std_error(&jerr.pub);
468
453
        jerr.pub.error_exit = jpeg_error;
487
472
 
488
473
static void write_jpeg(struct jpeg_compress_struct * cinfo, struct ImBuf * ibuf)
489
474
{
490
 
        JSAMPLE * buffer = 0;
 
475
        JSAMPLE * buffer = NULL;
491
476
        JSAMPROW row_pointer[1];
492
477
        uchar * rect;
493
478
        int x, y;
503
488
        memcpy(neogeo + 6, &ibuf_ftype, 4);
504
489
        jpeg_write_marker(cinfo, 0xe1, (JOCTET*) neogeo, 10);
505
490
 
506
 
        if(ibuf->metadata) {
 
491
        if (ibuf->metadata) {
507
492
                /* key + max value + "Blender" */
508
493
                text= MEM_mallocN(530, "stamp info read");
509
494
                iptr= ibuf->metadata;
510
 
                while(iptr) {
 
495
                while (iptr) {
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");
537
522
 
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];
541
526
 
542
 
                switch(cinfo->in_color_space){
543
 
                case JCS_RGB:
544
 
                        for (x = 0; x < ibuf->x; x++) {
545
 
                                *buffer++ = rect[0];
546
 
                                *buffer++ = rect[1];
547
 
                                *buffer++ = rect[2];
548
 
                                rect += 4;
549
 
                        }
550
 
                        break;
551
 
                case JCS_GRAYSCALE:
552
 
                        for (x = 0; x < ibuf->x; x++) {
553
 
                                *buffer++ = rect[0];
554
 
                                rect += 4;
555
 
                        }
556
 
                        break;
557
 
                case JCS_UNKNOWN:
558
 
                        memcpy(buffer, rect, 4 * ibuf->x);
559
 
                        break;
560
 
                        /* default was missing... intentional ? */
561
 
                default:
562
 
                        ; /* do nothing */
 
527
                switch(cinfo->in_color_space) {
 
528
                        case JCS_RGB:
 
529
                                for (x = 0; x < ibuf->x; x++) {
 
530
                                        *buffer++ = rect[0];
 
531
                                        *buffer++ = rect[1];
 
532
                                        *buffer++ = rect[2];
 
533
                                        rect += 4;
 
534
                                }
 
535
                                break;
 
536
                        case JCS_GRAYSCALE:
 
537
                                for (x = 0; x < ibuf->x; x++) {
 
538
                                        *buffer++ = rect[0];
 
539
                                        rect += 4;
 
540
                                }
 
541
                                break;
 
542
                        case JCS_UNKNOWN:
 
543
                                memcpy(buffer, rect, 4 * ibuf->x);
 
544
                                break;
 
545
                                /* default was missing... intentional ? */
 
546
                        default:
 
547
                                ; /* do nothing */
563
548
                }
564
549
 
565
550
                jpeg_write_scanlines(cinfo, row_pointer, 1);
585
570
        cinfo->image_height = ibuf->y;
586
571
 
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;
590
 
        
591
 
        switch(cinfo->in_color_space){
592
 
        case JCS_RGB:
593
 
                cinfo->input_components = 3;
594
 
                break;
595
 
        case JCS_GRAYSCALE:
596
 
                cinfo->input_components = 1;
597
 
                break;
598
 
        case JCS_UNKNOWN:
599
 
                cinfo->input_components = 4;
600
 
                break;
601
 
                /* default was missing... intentional ? */
602
 
        default:
603
 
                ; /* do nothing */
 
573
        if (ibuf->planes == 8) cinfo->in_color_space = JCS_GRAYSCALE;
 
574
#if 0
 
575
        /* just write RGBA as RGB,
 
576
         * unsupported feature only confuses other s/w */
 
577
 
 
578
        if (ibuf->planes == 32) cinfo->in_color_space = JCS_UNKNOWN;
 
579
#endif
 
580
        switch(cinfo->in_color_space) {
 
581
                case JCS_RGB:
 
582
                        cinfo->input_components = 3;
 
583
                        break;
 
584
                case JCS_GRAYSCALE:
 
585
                        cinfo->input_components = 1;
 
586
                        break;
 
587
                case JCS_UNKNOWN:
 
588
                        cinfo->input_components = 4;
 
589
                        break;
 
590
                        /* default was missing... intentional ? */
 
591
                default:
 
592
                        ; /* do nothing */
604
593
        }
605
594
        jpeg_set_defaults(cinfo);
606
595
        
613
602
}
614
603
 
615
604
 
616
 
static int save_stdjpeg(char * name, struct ImBuf * ibuf)
 
605
static int save_stdjpeg(const char *name, struct ImBuf *ibuf)
617
606
{
618
607
        FILE * outfile;
619
608
        struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
620
609
        struct my_error_mgr jerr;
621
610
 
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;
624
613
 
625
614
        cinfo->err = jpeg_std_error(&jerr.pub);
647
636
}
648
637
 
649
638
 
650
 
static int save_vidjpeg(char * name, struct ImBuf * ibuf)
 
639
static int save_vidjpeg(const char *name, struct ImBuf *ibuf)
651
640
{
652
641
        FILE * outfile;
653
642
        struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
654
643
        struct my_error_mgr jerr;
655
644
 
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;
658
647
 
659
648
        cinfo->err = jpeg_std_error(&jerr.pub);
686
675
        return 1;
687
676
}
688
677
 
689
 
static int save_jstjpeg(char * name, struct ImBuf * ibuf)
 
678
static int save_jstjpeg(const char *name, struct ImBuf *ibuf)
690
679
{
691
680
        char fieldname[1024];
692
681
        struct ImBuf * tbuf;
693
682
        int oldy, returnval;
694
683
 
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;
698
687
        
703
692
        IMB_rectcpy(tbuf, ibuf, 0, 0, 0, 0, ibuf->x, ibuf->y);
704
693
        sprintf(fieldname, "%s.jf0", name);
705
694
 
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;
718
707
}
719
708
 
720
 
static int save_maxjpeg(char * name, struct ImBuf * ibuf)
 
709
static int save_maxjpeg(const char *name, struct ImBuf *ibuf)
721
710
{
722
711
        FILE * outfile;
723
712
        struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo;
724
713
        struct my_error_mgr jerr;
725
714
 
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;
728
717
 
729
718
        cinfo->err = jpeg_std_error(&jerr.pub);
756
745
        return 1;
757
746
}
758
747
 
759
 
int imb_savejpeg(struct ImBuf * ibuf, char * name, int flags)
 
748
int imb_savejpeg(struct ImBuf *ibuf, const char *name, int flags)
760
749
{
761
750
        
762
751
        ibuf->flags = flags;