File: image_jpeg.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (472 lines) | stat: -rw-r--r-- 12,226 bytes parent folder | download | duplicates (2)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms. If not, see <http://www.gnu.org/licenses/>.
 */


/*
 *  This file is part of the XForms library package.
 *  Copyright (c) 1993, 1998-2002  T.C. Zhao
 *  All rights reserved.
 *
 *  JFIF file IO support.
 *  Based mostly on Independent JPEG Groups' example code.
 *
 *  Except for a couple of output control parameters, there are no
 *  static variables.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "include/forms.h"
#include "flimage.h"
#include "flimage_int.h"


/* jpeglib.h includes jconfig.h which has a old-style definition of
 * HAVE_STDLIB_H. This breaks compilation with compilers like IBM's
 * xlc. */

#ifdef HAVE_STDLIB_H
#undef HAVE_STDLIB_H
#endif

#include "jpeglib.h"
#include "jerror.h"
#include <setjmp.h>

/* For some reason, jpeg 6.1 does not work well */

#if JPEG_LIB_VERSION < 62
#error does not work well with jpeg lib v6.a
#endif

/* options supported by jpeg reader/writer */

static int do_quantization = 0;
static int quality_factor = 75;
static int smoothing_factor = 0;


/***************************************
 ***************************************/

void
flimage_jpeg_output_options( FLIMAGE_JPEG_OPTION * op )
{
    quality_factor = op->quality;
    smoothing_factor = op->smoothing;
}


static unsigned int jpeg_getc( j_decompress_ptr cinfo );


/***************************************
 ***************************************/

static int
JPEG_identify( FILE * fp )
{
    unsigned char buf[ 129 ];
    size_t i;

    i = fread( buf, 1, sizeof buf - 1, fp );
    rewind( fp );
    buf[ i ] = '\0';

    /* Clive Stubbings.
     * Test for a JPEG SOI code (0xff, 0xd8) followed by the start of
     * APP0 segement (0xff).
     * A 'raw' JPEG will not have the JFIF (JPEG file interchange format)
     * header but is still readable
     */

    if ( buf[ 0 ] == 0xff && buf[ 1 ] == 0xd8 && buf[ 2 ] == 0xff )
        return 1;

    for ( i = 0; i < sizeof buf - 3 && buf[ i ] != 'J'; i++ )
        /* empty */ ;

    return ! strncmp( ( char * ) buf + i, "JFIF", 4 );
}


/* cinfo->client_data member only exists in V6B, have to stuff image into
   error handler. Yuck! */

typedef struct
{
    struct jpeg_error_mgr         pub;
    jmp_buf                       jmp_buffer;
    struct jpeg_decompress_struct dinfo;
    struct jpeg_compress_struct   cinfo;
    FL_IMAGE *image;
} SPEC;


/***************************************
 ***************************************/

static void
error_exit( j_common_ptr cinfo )
{
    SPEC *spec = ( SPEC * ) cinfo->err;
    static char buf[ 1024 ];

    cinfo->err->format_message( cinfo, buf );
    spec->image->error_message( spec->image, buf );
    longjmp( spec->jmp_buffer, 1 );
}


/***************************************
 ***************************************/

static boolean
gather_comments( j_decompress_ptr cinfo )
{
    SPEC *spec = ( SPEC * ) cinfo->err;
    FL_IMAGE *image = spec->image;
    int length,
        ch;
    char *s;

    length  = jpeg_getc( cinfo ) << 8;
    length += jpeg_getc( cinfo ) - 2;

    image->comments = fl_realloc( image->comments, length + 1 );

    image->comments[ length ] = '\0';
    image->comments_len = length;

    for ( s = image->comments; --length >= 0; )
    {
        ch = jpeg_getc( cinfo );
        *s++ = ch;
    }

    return FL_TRUE;
}


/***************************************
 ***************************************/

static boolean
gather_text( j_decompress_ptr cinfo )
{
    SPEC *spec = ( SPEC * ) cinfo->err;
    FL_IMAGE *image = spec->image;
    int length,
        ch;
    char *s;

    length = jpeg_getc( cinfo ) << 8;
    length += jpeg_getc( cinfo ) - 2;

    if ( image->comments )
        image->comments = fl_realloc( image->comments, length + 1 );
    else
        image->comments = fl_malloc( length + 1 );

    image->comments[ length ] = '\0';
    image->comments_len = length;


    for ( s = image->comments; --length >= 0; )
    {
        ch = jpeg_getc( cinfo );
        *s++ = ch;
    }

    if ( image->comments[ image->comments_len - 1 ] == '\n' )
        image->comments[ image->comments_len - 1 ] = ' ';

    return FL_TRUE;
}


/***************************************
 ***************************************/

static int
JPEG_description( FL_IMAGE * im )
{
    SPEC *spec = fl_malloc( sizeof *spec );
    struct jpeg_decompress_struct *cinfo;

    cinfo = &spec->dinfo;
    cinfo->err = jpeg_std_error( &spec->pub );
    spec->pub.error_exit = error_exit;
    spec->image = im;
    im->io_spec = spec;

    jpeg_create_decompress( cinfo );
    jpeg_set_marker_processor( cinfo, JPEG_COM, gather_comments );
    jpeg_set_marker_processor( cinfo, JPEG_APP0 + 12, gather_text );

    jpeg_stdio_src( cinfo, im->fpin );
    jpeg_read_header( cinfo, FL_TRUE );

    /* decompressison options such as quantization here */
    if (do_quantization)
    {
        cinfo->desired_number_of_colors = 215;
        cinfo->quantize_colors = FL_TRUE;
        cinfo->enable_2pass_quant = FL_TRUE;
        cinfo->two_pass_quantize = FL_TRUE;
        cinfo->dither_mode = JDITHER_FS;
    }

    jpeg_start_decompress( cinfo );

    im->w = cinfo->output_width;
    im->h = cinfo->output_height;
    im->map_len = cinfo->desired_number_of_colors;

    if ( cinfo->out_color_space == JCS_GRAYSCALE )
        im->type = FL_IMAGE_GRAY;
    else if ( cinfo->out_color_space == JCS_RGB )
        im->type = cinfo->output_components == 3 ? FL_IMAGE_RGB : FL_IMAGE_CI;
    else
    {
        im->error_message( im, "unhandled colorspace" );
        return -1;
    }

    im->original_type = im->type;
    return 0;
}


/***************************************
 ***************************************/

static int
JPEG_read_pixels( FL_IMAGE * im )
{
    SPEC *spec = im->io_spec;
    struct jpeg_decompress_struct *cinfo = &spec->dinfo;
    int i,
        j,
        stride,
        err;
    JSAMPARRAY buf;
    unsigned short *gray,
                   *ci;

    if ( setjmp( spec->jmp_buffer ) )
    {
        jpeg_destroy_decompress( cinfo );
        /* keep what we'be got */
        return ( im->completed > im->w / 2 ) ? 1 : -1;
    }

    err = 0;
    stride = cinfo->output_width * cinfo->output_components;

    buf = cinfo->mem->alloc_sarray( ( j_common_ptr ) cinfo, JPOOL_IMAGE,
                                    stride, 1 /* cinfo->rec_outbuf_height */ );

    while ( cinfo->output_scanline < cinfo->output_height && ! err )
    {
        jpeg_read_scanlines( cinfo, buf, 1 /* cinfo->rec_out_height  */ );

        if ( ! ( cinfo->output_scanline & FLIMAGE_REPFREQ ) )
        {
            im->completed = cinfo->output_scanline;
            im->visual_cue( im, "Reading JPEG" );
        }

        if ( im->type == FL_IMAGE_RGB )
        {
            for ( i = j = 0; i < ( int ) cinfo->output_width; i++, j += 3 )
            {
                im->red[   cinfo->output_scanline - 1 ][ i ] =
                                                             buf[ 0 ][ j ];
                im->green[ cinfo->output_scanline - 1 ][ i ] =
                                                             buf[ 0 ][ j + 1 ];
                im->blue[  cinfo->output_scanline - 1 ][ i ] =
                                                             buf[ 0 ][ j + 2 ];
            }
        }
        else if ( im->type == FL_IMAGE_CI )
        {
            im->map_len = cinfo->actual_number_of_colors;
            for ( i = 0; i < cinfo->actual_number_of_colors; i++ )
            {
                im->red_lut[   i ] = cinfo->colormap[ 0 ][ i ];
                im->green_lut[ i ] = cinfo->colormap[ 1 ][ i ];
                im->blue_lut[  i ] = cinfo->colormap[ 2 ][ i ];
            }

            ci = im->ci[ cinfo->output_scanline - 1 ];
            for ( i = 0; i < ( int ) cinfo->output_width; i++ )
                ci[ i ] = buf[ 0 ][ i ];
        }
        else if ( im->type == FL_IMAGE_GRAY )
        {
            gray = im->gray[ cinfo->output_scanline - 1 ];
            for ( i = j = 0; i < im->w; i++ )
                gray[ i ] = buf[ 0 ][ i ];
        }
        else
        {
            flimage_error( im, "%s: unknown color space", im->infile );
            err = 1;
        }
    }

    jpeg_finish_decompress( cinfo );
    jpeg_destroy_decompress( cinfo );

    return im->completed > im->h / 3 ? 1 : -1;
}


/***************************************
 ***************************************/

static int
JPEG_write( FL_IMAGE * im )
{
    SPEC *spec = fl_calloc( 1, sizeof *spec );
    struct jpeg_compress_struct *cinfo;
/*    JSAMPROW rowptr[ 1 ]; */
    JSAMPARRAY rowptr;
    unsigned char *buf;
    int i;

    cinfo = &spec->cinfo;
    cinfo->err = jpeg_std_error( &spec->pub );
    spec->pub.error_exit = error_exit;

    if ( setjmp( spec->jmp_buffer ) )
    {
        jpeg_destroy_compress( cinfo );
        fl_free( spec );
        return -1;
    }

    jpeg_create_compress( cinfo );
    jpeg_stdio_dest( cinfo, im->fpout );

    cinfo->image_width = im->w;
    cinfo->image_height = im->h;

    if ( im->type == FL_IMAGE_RGB )
    {
        cinfo->input_components = 3;
        cinfo->in_color_space = JCS_RGB;
    }
    else if ( im->type == FL_IMAGE_GRAY )
    {
        cinfo->input_components = 1;
        cinfo->in_color_space = JCS_GRAYSCALE;
    }

    jpeg_set_defaults( cinfo );
    jpeg_set_quality( cinfo, quality_factor, FL_TRUE );
    cinfo->smoothing_factor = smoothing_factor;

    jpeg_start_compress( cinfo, FL_TRUE );

    if ( im->comments )
        jpeg_write_marker( cinfo, JPEG_COM, (void *) im->comments,
                           im->comments_len );

    rowptr = cinfo->mem->alloc_sarray( ( j_common_ptr ) cinfo, JPOOL_IMAGE,
                                       cinfo->input_components * im->w, 1 );

    while ( cinfo->next_scanline < cinfo->image_height )
    {
        if ( ! ( cinfo->next_scanline & FLIMAGE_REPFREQ ) )
        {
            im->completed = cinfo->next_scanline;
            im->visual_cue( im, "Writing JPEG " );
        }

        for ( buf = rowptr[ 0 ], i = 0; i < im->w; i++ )
        {
            if ( cinfo->input_components == 3 )
            {
                *buf++ = im->red[   cinfo->next_scanline ][ i ];
                *buf++ = im->green[ cinfo->next_scanline ][ i ];
                *buf++ = im->blue[  cinfo->next_scanline ][ i ];
            }
            else
                *buf++ =
                        ( unsigned char ) im->gray[ cinfo->next_scanline ][ i ];
        }

        jpeg_write_scanlines( cinfo, rowptr, 1 );
    }

    jpeg_finish_compress( cinfo );
    fflush( im->fpout );

    jpeg_destroy_compress( cinfo );
    fl_free( spec );

    return 1;
}


/***************************************
 ***************************************/

static unsigned int
jpeg_getc( j_decompress_ptr cinfo )
{
    struct jpeg_source_mgr *datasrc = cinfo->src;

    if (datasrc->bytes_in_buffer == 0)
    {

    if (!datasrc->fill_input_buffer(cinfo))
        ERREXIT(cinfo, JERR_CANT_SUSPEND);
    }

    datasrc->bytes_in_buffer--;

    return GETJOCTET(*datasrc->next_input_byte++);
}


/***************************************
 ***************************************/

void
flimage_enable_jpeg( void )
{
    flimage_add_format( "JPEG/JFIF format", "jpeg", "jpg",
                        FL_IMAGE_RGB | FL_IMAGE_GRAY,
                        JPEG_identify,
                        JPEG_description,
                        JPEG_read_pixels,
                        JPEG_write );
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */