~ubuntu-branches/debian/squeeze/camlimages/squeeze

« back to all changes in this revision

Viewing changes to jpeg/jpegread.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Le Gall, Ralf Treinen, Sylvain Le Gall
  • Date: 2009-03-05 00:19:32 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090305001932-f0hstlmun8hxvs0r
Tags: 1:3.0.1-1
[ Ralf Treinen ]
* Updated debian/watch.

[ Sylvain Le Gall ]
* New upstream version:
  * Remove useless patches
  * Adapt debian/rules and other debhelper files
  * Add debian/patches/fix_3_0_1 to fix various problem (probably due to
    OCaml 3.11 migration)
* Depends on version 2.12 of lablgtk2
* Add dh-ocaml build-dependency (rules/ocaml.mk)
* Add ${misc:Depends} to dependencies
* Update Homepage field into debian/control and debian/copyright
* Add license version for debian packaging
* Directly use eng.html rather than creating a linked index.html file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***********************************************************************/
2
 
/*                                                                     */
3
 
/*                           Objective Caml                            */
4
 
/*                                                                     */
5
 
/*            Fran�ois Pessaux, projet Cristal, INRIA Rocquencourt     */
6
 
/*            Pierre Weis, projet Cristal, INRIA Rocquencourt          */
7
 
/*            Jun Furuse, projet Cristal, INRIA Rocquencourt           */
8
 
/*                                                                     */
9
 
/*  Copyright 1999,2000                                                */
10
 
/*  Institut National de Recherche en Informatique et en Automatique.  */
11
 
/*  Distributed only by permission.                                    */
12
 
/*                                                                     */
13
 
/***********************************************************************/
14
 
#include <config.h>
15
 
#include <caml/mlvalues.h>
16
 
#include <caml/alloc.h>
17
 
#include <caml/memory.h>
18
 
#include <caml/fail.h>
19
 
 
20
 
#define jpeg_not_supported() \
21
 
 failwith( "jpeg is not supported" ); \
22
 
 return (Val_unit)
23
 
 
24
 
#if HAVE_JPEG
25
 
 
26
 
#include <stdio.h>
27
 
#include <string.h>
28
 
 
29
 
/*
30
 
 * Include file for users of JPEG library.
31
 
 * You will need to have included system headers that define at least
32
 
 * the typedefs FILE and size_t before you can include jpeglib.h.
33
 
 * (stdio.h is sufficient on ANSI-conforming systems.)
34
 
 * You may also wish to include "jerror.h".
35
 
 */
36
 
 
37
 
#include "jpeglib.h"
38
 
 
39
 
/*
40
 
 * <setjmp.h> is used for the optional error recovery mechanism shown in
41
 
 * the second part of the example.
42
 
 */
43
 
 
44
 
#include <setjmp.h>
45
 
 
46
 
char jpg_error_message[JMSG_LENGTH_MAX];
47
 
 
48
 
struct my_error_mgr {
49
 
  struct jpeg_error_mgr pub;    /* "public" fields */
50
 
 
51
 
  jmp_buf setjmp_buffer;        /* for return to caller */
52
 
};
53
 
 
54
 
typedef struct my_error_mgr * my_error_ptr;
55
 
 
56
 
void
57
 
my_error_exit (j_common_ptr cinfo)
58
 
{
59
 
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
60
 
  my_error_ptr myerr = (my_error_ptr) cinfo->err;
61
 
 
62
 
  /* Always display the message. */
63
 
  /* We could postpone this until after returning, if we chose. */
64
 
  fprintf(stderr,"setting message\n");
65
 
  (*cinfo->err->format_message) (cinfo, jpg_error_message );
66
 
 
67
 
  /* Return control to the setjmp point */
68
 
  longjmp(myerr->setjmp_buffer, 1);
69
 
}
70
 
 
71
 
GLOBAL(value)
72
 
read_JPEG_file (value name)
73
 
{
74
 
  CAMLparam1(name);
75
 
  CAMLlocal1(res);
76
 
 
77
 
  char *filename;
78
 
  /* This struct contains the JPEG decompression parameters and pointers to
79
 
   * working space (which is allocated as needed by the JPEG library).
80
 
   */
81
 
  struct jpeg_decompress_struct cinfo;
82
 
  /* We use our private extension JPEG error handler.
83
 
   * Note that this struct must live as long as the main JPEG parameter
84
 
   * struct, to avoid dangling-pointer problems.
85
 
   */
86
 
  struct my_error_mgr jerr;
87
 
  /* More stuff */
88
 
  FILE * infile;                /* source file */
89
 
  JSAMPARRAY buffer;            /* Output row buffer */
90
 
  int row_stride;               /* physical row width in output buffer */
91
 
  int i;
92
 
 
93
 
  filename= String_val( name );
94
 
 
95
 
  /* In this example we want to open the input file before doing anything else,
96
 
   * so that the setjmp() error recovery below can assume the file is open.
97
 
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
98
 
   * requires it in order to read binary files.
99
 
   */
100
 
 
101
 
  if ((infile = fopen(filename, "rb")) == NULL) {
102
 
    failwith("failed to open jpeg file");
103
 
  }
104
 
 
105
 
  /* Step 1: allocate and initialize JPEG decompression object */
106
 
 
107
 
  /* We set up the normal JPEG error routines, then override error_exit. */
108
 
  cinfo.err = jpeg_std_error(&jerr.pub);
109
 
  jerr.pub.error_exit = my_error_exit;
110
 
 
111
 
  /* Establish the setjmp return context for my_error_exit to use. */
112
 
  if (setjmp(jerr.setjmp_buffer)) {
113
 
    /* If we get here, the JPEG code has signaled an error.
114
 
     * We need to clean up the JPEG object, close the input file, and return.
115
 
     */
116
 
    fprintf(stderr, "Exiting...");
117
 
    jpeg_destroy_decompress(&cinfo);
118
 
    fclose(infile);
119
 
    exit(-1);
120
 
    failwith(jpg_error_message);
121
 
  }
122
 
  /* Now we can initialize the JPEG decompression object. */
123
 
  jpeg_create_decompress(&cinfo);
124
 
 
125
 
  /* Step 2: specify data source (eg, a file) */
126
 
 
127
 
  jpeg_stdio_src(&cinfo, infile);
128
 
 
129
 
  /* Step 3: read file parameters with jpeg_read_header() */
130
 
 
131
 
  (void) jpeg_read_header(&cinfo, TRUE);
132
 
 
133
 
  /* We can ignore the return value from jpeg_read_header since
134
 
   *   (a) suspension is not possible with the stdio data source, and
135
 
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
136
 
   * See libjpeg.doc for more info.
137
 
   */
138
 
 
139
 
  /* Step 4: set parameters for decompression */
140
 
 
141
 
  /* In this example, we don't need to change any of the defaults set by
142
 
   * jpeg_read_header(), so we do nothing here.
143
 
   */
144
 
 
145
 
  cinfo.out_color_space = JCS_RGB;
146
 
 
147
 
  /* Step 5: Start decompressor */
148
 
 
149
 
  (void) jpeg_start_decompress(&cinfo);
150
 
  /* We can ignore the return value since suspension is not possible
151
 
   * with the stdio data source.
152
 
   */
153
 
 
154
 
  /* We may need to do some setup of our own at this point before reading
155
 
   * the data.  After jpeg_start_decompress() we have the correct scaled
156
 
   * output image dimensions available, as well as the output colormap
157
 
   * if we asked for color quantization.
158
 
   * In this example, we need to make an output work buffer of the right size.
159
 
   */ 
160
 
  /* JSAMPLEs per row in output buffer */
161
 
 
162
 
  row_stride = cinfo.output_width * cinfo.output_components;
163
 
 
164
 
  /* Make a one-row-high sample array that will go away when done with image */
165
 
  buffer = (*cinfo.mem->alloc_sarray)
166
 
                ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 
167
 
                 cinfo.output_height );
168
 
 
169
 
  /* Step 6: while (scan lines remain to be read) */
170
 
  /*           jpeg_read_scanlines(...); */
171
 
 
172
 
  /* Here we use the library's state variable cinfo.output_scanline as the
173
 
   * loop counter, so that we don't have to keep track ourselves.
174
 
   */
175
 
  while (cinfo.output_scanline < cinfo.output_height) {
176
 
    /* jpeg_read_scanlines expects an array of pointers to scanlines.
177
 
     * Here the array is only one element long, but you could ask for
178
 
     * more than one scanline at a time if that's more convenient.
179
 
     */
180
 
    jpeg_read_scanlines(&cinfo, buffer + cinfo.output_scanline, 1); 
181
 
  }
182
 
 
183
 
  {
184
 
    CAMLlocalN(r,3);
185
 
    r[0] = Val_int(cinfo.output_width);
186
 
    r[1] = Val_int(cinfo.output_height);
187
 
    r[2] = alloc_string ( row_stride * cinfo.output_height );
188
 
    for(i=0; i<cinfo.output_height; i++){
189
 
      memcpy( String_val(r[2]) + i * row_stride, 
190
 
               buffer[i], row_stride);
191
 
    }
192
 
    res = alloc_tuple(3);
193
 
    for(i=0; i<3; i++) Field(res, i) = r[i];
194
 
  }
195
 
 
196
 
  /* Step 7: Finish decompression */
197
 
 
198
 
  (void) jpeg_finish_decompress(&cinfo);
199
 
  /* We can ignore the return value since suspension is not possible
200
 
   * with the stdio data source.
201
 
   */
202
 
 
203
 
  /* Step 8: Release JPEG decompression object */
204
 
 
205
 
  /* This is an important step since it will release a good deal of memory. */
206
 
  jpeg_destroy_decompress(&cinfo);
207
 
 
208
 
  /* After finish_decompress, we can close the input file.
209
 
   * Here we postpone it until after no more JPEG errors are possible,
210
 
   * so as to simplify the setjmp error logic above.  (Actually, I don't
211
 
   * think that jpeg_destroy can do an error exit, but why assume anything...)
212
 
   */
213
 
  fclose(infile);
214
 
 
215
 
  /* At this point you may want to check to see whether any corrupt-data
216
 
   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
217
 
   */
218
 
 
219
 
  /* And we're done! */
220
 
  CAMLreturn(res);
221
 
}
222
 
 
223
 
value jpeg_set_scale_denom( jpegh, denom )
224
 
     value jpegh;
225
 
     value denom;
226
 
{
227
 
  CAMLparam2(jpegh,denom);
228
 
  struct jpeg_decompress_struct *cinfop;
229
 
 
230
 
  cinfop = (struct jpeg_decompress_struct *) Field ( jpegh, 0 );
231
 
  cinfop->scale_denom = Int_val( denom );
232
 
  CAMLreturn(Val_unit);
233
 
}
234
 
 
235
 
value open_jpeg_file_for_read( name )
236
 
     value name;
237
 
{
238
 
  CAMLparam1(name);
239
 
  CAMLlocal1(res);
240
 
 
241
 
  char *filename;
242
 
  /* This struct contains the JPEG decompression parameters and pointers to
243
 
   * working space (which is allocated as needed by the JPEG library).
244
 
   */
245
 
  struct jpeg_decompress_struct* cinfop;
246
 
  /* We use our private extension JPEG error handler.
247
 
   * Note that this struct must live as long as the main JPEG parameter
248
 
   * struct, to avoid dangling-pointer problems.
249
 
   */
250
 
  struct my_error_mgr *jerrp;
251
 
  /* More stuff */
252
 
  FILE * infile;                /* source file */
253
 
  int i;
254
 
 
255
 
  filename= String_val( name );
256
 
 
257
 
  if ((infile = fopen(filename, "rb")) == NULL) {
258
 
    failwith("failed to open jpeg file");
259
 
  }
260
 
 
261
 
  cinfop = malloc(sizeof (struct jpeg_decompress_struct));
262
 
  jerrp = malloc(sizeof (struct my_error_mgr));
263
 
 
264
 
  /* In this example we want to open the input file before doing anything else,
265
 
   * so that the setjmp() error recovery below can assume the file is open.
266
 
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
267
 
   * requires it in order to read binary files.
268
 
   */
269
 
 
270
 
 
271
 
  /* Step 1: allocate and initialize JPEG decompression object */
272
 
 
273
 
  /* We set up the normal JPEG error routines, then override error_exit. */
274
 
  cinfop->err = jpeg_std_error(&jerrp->pub);
275
 
  jerrp->pub.error_exit = my_error_exit;
276
 
  /* Establish the setjmp return context for my_error_exit to use. */
277
 
  if (setjmp(jerrp->setjmp_buffer)) {
278
 
    /* If we get here, the JPEG code has signaled an error.
279
 
     * We need to clean up the JPEG object, close the input file, and return.
280
 
     */
281
 
    jpeg_destroy_decompress(cinfop);
282
 
    free(jerrp);
283
 
    fclose(infile);
284
 
    failwith(jpg_error_message);
285
 
  }
286
 
  /* Now we can initialize the JPEG decompression object. */
287
 
  jpeg_create_decompress(cinfop);
288
 
 
289
 
  /* Step 2: specify data source (eg, a file) */
290
 
 
291
 
  jpeg_stdio_src(cinfop, infile);
292
 
 
293
 
  /* Step 3: read file parameters with jpeg_read_header() */
294
 
 
295
 
  (void) jpeg_read_header(cinfop, TRUE);
296
 
 
297
 
  { 
298
 
    CAMLlocalN(r,3);
299
 
    r[0] = Val_int(cinfop->image_width);
300
 
    r[1] = Val_int(cinfop->image_height);
301
 
    r[2] = alloc_tuple(3);
302
 
    Field(r[2], 0) = (value)cinfop;
303
 
    Field(r[2], 1) = (value)infile;
304
 
    Field(r[2], 2) = (value)jerrp;
305
 
    res = alloc_tuple(3);
306
 
    for(i=0; i<3; i++) Field(res, i) = r[i];
307
 
  }
308
 
  CAMLreturn(res);
309
 
}
310
 
 
311
 
value open_jpeg_file_for_read_start( jpegh )
312
 
     value jpegh;
313
 
{
314
 
  CAMLparam1(jpegh);
315
 
  CAMLlocal1(res);
316
 
  struct jpeg_decompress_struct* cinfop;
317
 
  struct my_error_mgr *jerrp;
318
 
  FILE *infile;
319
 
  int i;
320
 
 
321
 
  cinfop = (struct jpeg_decompress_struct *) Field( jpegh, 0 );
322
 
  infile = (FILE *) Field( jpegh, 1 );
323
 
  jerrp = (struct my_error_mgr *) Field( jpegh, 2 );
324
 
 
325
 
  /* We can ignore the return value from jpeg_read_header since
326
 
   *   (a) suspension is not possible with the stdio data source, and
327
 
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
328
 
   * See libjpeg.doc for more info.
329
 
   */
330
 
 
331
 
  /* Step 4: set parameters for decompression */
332
 
 
333
 
  /* In this example, we don't need to change any of the defaults set by
334
 
   * jpeg_read_header(), so we do nothing here.
335
 
   */
336
 
 
337
 
  cinfop->out_color_space = JCS_RGB;
338
 
 
339
 
  /* Step 5: Start decompressor */
340
 
 
341
 
  (void) jpeg_start_decompress(cinfop);
342
 
  /* We can ignore the return value since suspension is not possible
343
 
   * with the stdio data source.
344
 
   */
345
 
 
346
 
  /* We may need to do some setup of our own at this point before reading
347
 
   * the data.  After jpeg_start_decompress() we have the correct scaled
348
 
   * output image dimensions available, as well as the output colormap
349
 
   * if we asked for color quantization.
350
 
   * In this example, we need to make an output work buffer of the right size.
351
 
   */ 
352
 
  /* JSAMPLEs per row in output buffer */
353
 
 
354
 
  /* row_stride = cinfop->output_width * cinfop->output_components; */
355
 
 
356
 
  { 
357
 
    CAMLlocalN(r,3);
358
 
    r[0] = Val_int(cinfop->output_width);
359
 
    r[1] = Val_int(cinfop->output_height);
360
 
    r[2] = alloc_tuple(3);
361
 
    Field(r[2], 0) = (value)cinfop;
362
 
    Field(r[2], 1) = (value)infile;
363
 
    Field(r[2], 2) = (value)jerrp;
364
 
    res = alloc_tuple(3);
365
 
    for(i=0; i<3; i++) Field(res, i) = r[i];
366
 
  }
367
 
#ifdef DEBUG_JPEG
368
 
  fprintf(stderr, "cinfop= %d infile= %d %d %d \n", cinfop, infile, cinfop->output_scanline, cinfop->output_height); 
369
 
  fflush(stderr);
370
 
#endif
371
 
  CAMLreturn(res);
372
 
}
373
 
 
374
 
value read_jpeg_scanline( jpegh, buf )
375
 
value jpegh, buf;
376
 
{
377
 
  CAMLparam2(jpegh,buf);
378
 
  
379
 
  struct jpeg_decompress_struct *cinfop;
380
 
  JSAMPROW row[1];
381
 
 
382
 
  cinfop = (struct jpeg_decompress_struct *) Field( jpegh, 0 );
383
 
 
384
 
  row[0] = String_val( buf );
385
 
 
386
 
  /*
387
 
  fprintf(stderr, "scan %d %d ...\n", cinfop->output_scanline, cinfop->output_height);
388
 
  fflush(stderr);
389
 
  */
390
 
  jpeg_read_scanlines( cinfop, row, 1 );
391
 
  CAMLreturn(Val_unit);
392
 
}
393
 
 
394
 
value close_jpeg_file_for_read( jpegh )
395
 
     value jpegh;
396
 
{
397
 
  CAMLparam1(jpegh);
398
 
 
399
 
  struct jpeg_decompress_struct *cinfop;
400
 
  struct my_error_mgr *jerrp;
401
 
  FILE *infile;
402
 
 
403
 
#ifdef DEBUG_JPEG
404
 
  fprintf(stderr, "closing\n");
405
 
  fflush(stderr);
406
 
#endif
407
 
 
408
 
  cinfop = (struct jpeg_decompress_struct *) Field( jpegh, 0 );
409
 
  infile = (FILE *) Field( jpegh, 1 );
410
 
  jerrp = (struct my_error_mgr *) Field( jpegh, 2 );
411
 
 
412
 
#ifdef DEBUG_JPEG
413
 
  fprintf(stderr, "cinfop= %d infile= %d %d %d \n", cinfop, infile, cinfop->output_scanline, cinfop->output_height); 
414
 
#endif
415
 
 
416
 
  if( cinfop->output_scanline >= cinfop->output_height ){ 
417
 
#ifdef DEBUG_JPEG
418
 
    fprintf(stderr, "finish\n");
419
 
    fflush(stderr);
420
 
#endif
421
 
    jpeg_finish_decompress( cinfop );
422
 
  }
423
 
#ifdef DEBUG_JPEG
424
 
  fprintf(stderr, "destroy\n");
425
 
  fflush(stderr);
426
 
#endif
427
 
  jpeg_destroy_decompress( cinfop ); 
428
 
  
429
 
  free(cinfop);
430
 
  free(jerrp);
431
 
#ifdef DEBUG_JPEG
432
 
  fprintf(stderr, "file close\n");
433
 
  fflush(stderr);
434
 
#endif
435
 
  fclose(infile);
436
 
 
437
 
  CAMLreturn(Val_unit);
438
 
}
439
 
 
440
 
#else
441
 
 
442
 
value read_JPEG_file (value name) 
443
 
{
444
 
  CAMLparam1(name);
445
 
  jpeg_not_supported();
446
 
}
447
 
 
448
 
value open_jpeg_file_for_read( name )
449
 
     value name;
450
 
{
451
 
  CAMLparam1(name);
452
 
  jpeg_not_supported();
453
 
}
454
 
 
455
 
value open_jpeg_file_for_read_start( jpegh )
456
 
     value jpegh;
457
 
{
458
 
  CAMLparam1(jpegh);
459
 
  jpeg_not_supported();
460
 
}
461
 
 
462
 
value read_jpeg_scanline( jpegh, buf )
463
 
     value jpegh, buf;
464
 
{
465
 
  CAMLparam2(jpegh,buf);
466
 
  jpeg_not_supported();
467
 
}
468
 
 
469
 
value close_jpeg_file_for_read (value jpegh) 
470
 
{
471
 
  CAMLparam1(jpegh);
472
 
  jpeg_not_supported();
473
 
}
474
 
 
475
 
value jpeg_set_scale_denom( jpegh, denom )
476
 
     value jpegh;
477
 
     value denom;
478
 
{
479
 
  CAMLparam2(jpegh,denom);
480
 
  jpeg_not_supported();
481
 
}
482
 
 
483
 
#endif /* HAVE_JPEG */