File: jpeg_loader.c

package info (click to toggle)
gimageview 0.2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 7,880 kB
  • ctags: 8,193
  • sloc: ansic: 80,389; sh: 8,625; perl: 1,167; makefile: 914; yacc: 318
file content (430 lines) | stat: -rw-r--r-- 10,874 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */

/*
 * gnome-thumbnail-pixbuf-utils.c: Utilities for handling pixbufs when thumbnailing
 *
 * Copyright (C) 2002 Red Hat, Inc.
 *
 * This file is part of the Gnome Library.
 *
 * The Gnome Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Gnome Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

/*
 *  2003-05-14 Takuro Ashie <ashie@homa.ne.jp>
 *
 *      fetched from of libgnomeui-2.2.0.1
 *      (libgnomeui/gnome-thumbnail-pixbuf-utils.c) and adapt to GImageView
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */


#ifdef ENABLE_JPEG

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <setjmp.h>
#include <stdio.h>

#include <jpeglib.h>

/*
 * Workaround broken libjpeg defining these that may
 * collide w/ the ones in config.h
 */
#undef HAVE_STDDEF_H
#undef HAVE_STDLIB_H
#include "gimv_image_loader.h"
#include "gimv_mime_types.h"
#include "gimv_plugin.h"

#define BUFFER_SIZE 16384

typedef struct
{
   struct jpeg_source_mgr pub;	/* public fields */
   GimvIO *gio;
   JOCTET buffer[BUFFER_SIZE];
   guint bytes_read;
} Source;


typedef struct
{
   struct jpeg_error_mgr pub;
   jmp_buf setjmp_buffer;
} ErrorHandlerData;


static GimvImage *jpeg_loader_load (GimvImageLoader *loader,
                                    gpointer         data);

static GimvImageLoaderPlugin gimv_jpeg_loader[] =
{
   {
      if_version:    GIMV_IMAGE_LOADER_IF_VERSION,
      id:            "JPEG",
      priority_hint: GIMV_IMAGE_LOADER_PRIORITY_CAN_CANCEL,
      check_type:    NULL,
      get_info:      NULL,
      loader:        jpeg_loader_load,
   }
};

static const gchar *jpeg_extensions[] =
{
   "jpg", "jpeg", "jpg",
};

static GimvMimeTypeEntry jpeg_mime_types[] =
{
   {
      mime_type:      "image/jpeg",
      description:    N_("The JPEG image format"),
      extensions:     jpeg_extensions,
      extensions_len: sizeof (jpeg_extensions) / sizeof (gchar *),
      icon:           NULL,
   },
};

GIMV_PLUGIN_GET_IMPL (gimv_jpeg_loader, GIMV_PLUGIN_IMAGE_LOADER)
GIMV_PLUGIN_GET_MIME_TYPE(jpeg_mime_types)

GimvPluginInfo gimv_plugin_info =
{
   if_version:    GIMV_PLUGIN_IF_VERSION,
   name:          N_("JPEG Image Loader"),
   version:       "0.0.3",
   author:        N_("Takuro Ashie"),
   description:   NULL,
   get_implement: gimv_plugin_get_impl,
   get_mime_type: gimv_plugin_get_mime_type,
   get_prefs_ui:  NULL,
};


static void
fatal_error_handler (j_common_ptr cinfo)
{
   ErrorHandlerData *data;

   data = (ErrorHandlerData *) cinfo->err;
   longjmp (data->setjmp_buffer, 1);
}


static void
output_message_handler (j_common_ptr cinfo)
{
   /* If we don't supply this handler, libjpeg reports errors
    * directly to stderr.
    */
}


static void
init_source (j_decompress_ptr cinfo)
{
}


static gboolean
fill_input_buffer (j_decompress_ptr cinfo)
{
   Source *src;
   GimvIOStatus status;
   guint nbytes;
	
   src = (Source *) cinfo->src;
   status = gimv_io_read (src->gio,
                          src->buffer,
                          sizeof (src->buffer) / sizeof (JOCTET),
                          &nbytes);

   if (status != GIMV_IO_STATUS_NORMAL || nbytes == 0) {
      /* return a fake EOI marker so we will eventually terminate */
      src->buffer[0] = (JOCTET) 0xFF;
      src->buffer[1] = (JOCTET) JPEG_EOI;
      nbytes = 2;
   }

   src->pub.next_input_byte = src->buffer;
   src->pub.bytes_in_buffer = nbytes;
   src->bytes_read += nbytes;

   return TRUE;
}


static void
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
   Source *src;

   src = (Source *) cinfo->src;
   if (num_bytes > 0) {
      while (num_bytes > (long) src->pub.bytes_in_buffer) {
         num_bytes -= (long) src->pub.bytes_in_buffer;
         fill_input_buffer (cinfo);
      }
      src->pub.next_input_byte += (size_t) num_bytes;
      src->pub.bytes_in_buffer -= (size_t) num_bytes;
   }
}


static void
term_source (j_decompress_ptr cinfo)
{
}


static void
set_src (j_decompress_ptr cinfo, Source *src, GimvIO *gio)
{
   src->pub.init_source = init_source;
   src->pub.fill_input_buffer = fill_input_buffer;
   src->pub.skip_input_data = skip_input_data;
   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   src->pub.term_source = term_source;
   src->gio = gio;
   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   src->pub.next_input_byte = NULL; /* until buffer loaded */
   src->bytes_read = 0;
}


static int
calculate_divisor (int width,
                   int height,
                   int target_width,
                   int target_height)
{
   if (target_width < 0 || target_height < 0) {
      return 1;
   }
   if (width / 8 > target_width && height / 8 > target_height) {
      return 8;
   }
   if (width / 4 > target_width && height / 4 > target_height) {
      return 4;
   }
   if (width / 2 > target_width && height / 2 > target_height) {
      return 2;
   }
   return 1;
}


static void
convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
                     guchar *line) 
{
   guint j;
   guchar *p;

   g_return_if_fail (cinfo != NULL);
   g_return_if_fail (cinfo->output_components == 4);
   g_return_if_fail (cinfo->out_color_space == JCS_CMYK);

   p = line;
   for (j = 0; j < cinfo->output_width; j++) {
      int c, m, y, k;
      c = p[0];
      m = p[1];
      y = p[2];
      k = p[3];
      if (cinfo->saw_Adobe_marker) {
         p[0] = k*c / 255;
         p[1] = k*m / 255;
         p[2] = k*y / 255;
      } else {
         p[0] = (255 - k)*(255 - c) / 255;
         p[1] = (255 - k)*(255 - m) / 255;
         p[2] = (255 - k)*(255 - y) / 255;
      }
      p[3] = 255;
      p += 4;
   }
}


static GimvImage *
jpeg_loader_load (GimvImageLoader *loader,
                  gpointer data)
{
   struct jpeg_decompress_struct cinfo;
   Source src;
   ErrorHandlerData jerr;
   GimvIO *gio;
   unsigned char *lines[1];
   guchar *buffer = NULL;
   guchar *pixels = NULL;
   guchar *ptr;
   int out_n_components;
   gboolean has_alpha;
   int target_width;
   int target_height;
   gboolean keep_aspect;
   gint bytes_count, bytes_count_tmp = 0;
   guint step = 65536;

   static guchar *buffer_prv = NULL;

   g_return_val_if_fail (GIMV_IS_IMAGE_LOADER (loader), NULL);

   gio = gimv_image_loader_get_gio (loader);
   if (!gio) return NULL;

   if (!gimv_image_loader_get_size_request (loader,
                                            &target_width,
                                            &target_height,
                                            &keep_aspect))
   {
      target_width  = -1;
      target_height = -1;
      keep_aspect   = TRUE;
   }

   cinfo.src = NULL;
   cinfo.err = jpeg_std_error (&jerr.pub);
   jerr.pub.error_exit = fatal_error_handler;
   jerr.pub.output_message = output_message_handler;

#warning FIXME!
   if (setjmp (jerr.setjmp_buffer)) {
      /* Handle a JPEG error. */
      jpeg_destroy_decompress (&cinfo);
      if(buffer != buffer_prv){
         buffer_prv = buffer;
         g_free (buffer);
      }
      g_free (pixels);
      return NULL;
   }

   jpeg_create_decompress (&cinfo);

   cinfo.src = &src.pub;
   set_src (&cinfo, &src, gio);

   jpeg_read_header (&cinfo, TRUE);

   /* set image info */
   if (loader->info) {
      GimvImageInfoFlags flags = GIMV_IMAGE_INFO_SYNCED_FLAG;

      gimv_image_info_set_size (loader->info,
                                cinfo.image_width, cinfo.image_height);
      gimv_image_info_set_flags (loader->info, flags);
   }

   cinfo.scale_num = 1;
   cinfo.scale_denom = calculate_divisor (cinfo.image_width,
                                          cinfo.image_height,
                                          target_width,
                                          target_height);

   if (gimv_image_loader_get_load_type (loader)
          == GIMV_IMAGE_LOADER_LOAD_THUMBNAIL)
   {
      cinfo.dct_method = JDCT_FASTEST;
      cinfo.do_fancy_upsampling = FALSE;
   }

   jpeg_calc_output_dimensions(&cinfo);

   if (cinfo.out_color_space != JCS_GRAYSCALE &&
       cinfo.out_color_space != JCS_RGB &&
       cinfo.out_color_space != JCS_CMYK)
   {
      jpeg_destroy_decompress (&cinfo);
      return NULL;
   }

   jpeg_start_decompress (&cinfo);

   if (cinfo.num_components == 1)
      out_n_components = 3;
   else
      out_n_components = cinfo.num_components;

   g_return_val_if_fail (out_n_components <= 3, NULL);

   pixels = g_malloc (cinfo.output_width * cinfo.output_height * out_n_components);

   ptr = pixels;
   if (cinfo.num_components == 1) {
      /* Allocate extra buffer for grayscale data */
      buffer = g_malloc (cinfo.output_width);
      lines[0] = buffer;
   } else {
      lines[0] = pixels;
   }

   bytes_count = 0;

   while (cinfo.output_scanline < cinfo.output_height) {
      guint i;

      jpeg_read_scanlines (&cinfo, lines, 1);

      bytes_count_tmp = ((Source *) cinfo.src)->bytes_read / step;
      if (bytes_count_tmp > bytes_count) {
         bytes_count = bytes_count_tmp;
         if (!gimv_image_loader_progress_update (loader))
            break;
      }

      if (cinfo.num_components == 1) {
         /* Convert grayscale to rgb */
         for (i = 0; i < cinfo.output_width; i++) {
            ptr[i * 3]     = buffer[i];
            ptr[i * 3 + 1] = buffer[i];
            ptr[i * 3 + 2] = buffer[i];
         }
         ptr += cinfo.output_width * 3;
      } else {
         if (cinfo.out_color_space == JCS_CMYK) {
            convert_cmyk_to_rgb (&cinfo, 
                                 lines[0]);
         }
         lines[0] += cinfo.output_width * out_n_components;
      }
   }

   buffer_prv = buffer;
   g_free (buffer);
   buffer = NULL;

   jpeg_finish_decompress (&cinfo);
   jpeg_destroy_decompress (&cinfo);

   has_alpha = cinfo.out_color_components == 4 ? TRUE : FALSE;

   return gimv_image_create_from_data (pixels,
                                       cinfo.output_width,
                                       cinfo.output_height,
                                       has_alpha);
}

#endif /* ENABLE_JPEG */