home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / contrib / seejpeg / seejpeg-.4 / seejpeg- / seejpeg-1.4.3 / image.c < prev    next >
C/C++ Source or Header  |  1994-03-19  |  4KB  |  134 lines

  1. /*
  2.  * image.c
  3.  *
  4.  * Copyright (C) 1993, 1994 Evan Harris
  5.  *
  6.  * Permission is granted to freely redistribute and modify this code,
  7.  * providing the author(s) get credit for having written it.
  8.  */
  9.  
  10. #include "seejpeg.h"
  11. #include <setjmp.h>
  12.  
  13.  
  14. /* These extern variables are needed by the error routines. */
  15. extern jmp_buf setjmp_buffer;    /* for return to caller */
  16. extern external_methods_ptr emethods; /* needed for access to message_parm */
  17.  
  18.  
  19. GLOBAL int
  20. read_image_file(char * filename)
  21. {
  22.   struct Compress_info_struct cinfo;
  23.   struct Compress_methods_struct c_methods;
  24.   struct External_methods_struct e_methods;
  25.   JSAMPARRAY pixel_row, cmap_row;
  26.   JSAMPIMAGE pixel_data;
  27.   char *type;
  28.   int c, row, components, translate;
  29.  
  30.   if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
  31.     fprintf(stderr, "can't open %s\n", filename);
  32.     return 0;
  33.   }
  34.   if ((c = getc(cinfo.input_file)) == EOF)
  35.     error_exit("Empty input file");
  36.   fseek(cinfo.input_file, 0, 0);
  37.  
  38.   cinfo.methods = &c_methods;    /* links to method structs */
  39.   cinfo.emethods = &e_methods;
  40.   emethods = &e_methods;    /* save struct addr for possible access */
  41.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  42.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  43.   e_methods.trace_level = 0;    /* default = no tracing */
  44.   e_methods.num_warnings = 0;    /* no warnings emitted yet */
  45.   e_methods.first_warning_level = 0; /* display first corrupt-data warning */
  46.   e_methods.more_warning_level = 3; /* but suppress additional ones */
  47.  
  48.   switch (c) {
  49.   case 'G':
  50.     type = "GIF";
  51.     jselrgif(&cinfo);
  52.     break;
  53.   case 'P':
  54.     type = "PPM";
  55.     jselrppm(&cinfo);
  56.     break;
  57. #if 0
  58.   case 'R':
  59.     jselrrle(&cinfo);
  60.     break;
  61. #endif
  62.   case 0x00:
  63.     type = "TARGA";
  64.     jselrtarga(&cinfo);
  65.     break;
  66.   default:
  67.     fclose(cinfo.input_file);
  68.     return read_JPEG_file(filename);
  69.     break;
  70.   }
  71.  
  72.   if (opt_verbose) {
  73.     printf("Type   : %s\n", type);
  74.   }
  75.  
  76.   if (setjmp(setjmp_buffer)) {
  77.     fclose(cinfo.input_file);
  78.     return 0;
  79.   }
  80.   
  81.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  82.  
  83.   (cinfo.methods->input_init) (&cinfo);
  84.   components = cinfo.input_components;
  85.   if (c == 'G' && components == 3) { /* GIF */
  86.     components = 1;
  87.     translate = 1;
  88.     translate_init();
  89.   } else {
  90.     translate = 0;
  91.   }
  92.   display_init(cinfo.image_width, cinfo.image_height, components);
  93.   
  94.   if (components == 1 && !translate) {
  95.     display_set_greyscale_palette();
  96.   }
  97.  
  98.   pixel_row = (*cinfo.emethods->alloc_small_sarray)
  99.     (cinfo.image_width, (long) cinfo.input_components);
  100.   if (translate) {
  101.     cmap_row = (*cinfo.emethods->alloc_small_sarray)
  102.     (cinfo.image_width, (long) components);
  103.     pixel_data = (JSAMPIMAGE)(*cinfo.emethods->alloc_small_sarray)
  104.       (sizeof(JSAMPROW *), (long) components);
  105.     for (c = 0; c < components; c++) {
  106.       pixel_data[c][0] = cmap_row[c];
  107.     }
  108.   } else {
  109.     cmap_row = NULL;
  110.     pixel_data = (JSAMPIMAGE)(*cinfo.emethods->alloc_small_sarray)
  111.       (sizeof(JSAMPROW *), (long) cinfo.input_components);
  112.     for (c = 0; c < cinfo.input_components; c++) {
  113.       pixel_data[c][0] = pixel_row[c];
  114.     }
  115.   }
  116.  
  117.   for (row = 0; row < cinfo.image_height; row++) {
  118.     (*cinfo.methods->get_input_row) (&cinfo, pixel_row);
  119.     if (translate) {
  120.       translate_row(cinfo.image_width, pixel_row, cmap_row);
  121.     }
  122.     display_rows(1, pixel_data, cinfo.image_width, components);
  123.   }
  124.  
  125.   (*cinfo.methods->input_term) (&cinfo);
  126.   scroll_until_end();
  127.   
  128.   (*cinfo.emethods->free_all) ();
  129.  
  130.   fclose(cinfo.input_file);
  131.  
  132.   return 1;
  133. }
  134.