home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / imagetypes.c < prev    next >
C/C++ Source or Header  |  1993-10-28  |  4KB  |  178 lines

  1. /* imagetypes.c:
  2.  *
  3.  * this contains things which reference the global ImageTypes array
  4.  *
  5.  * jim frost 09.27.89
  6.  *
  7.  * Copyright 1989, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13. #include "imagetypes.h"
  14. #include "options.h"
  15. #include <errno.h>
  16.  
  17. /* SUPPRESS 560 */
  18.  
  19. extern int errno;
  20.  
  21. /* load a named image
  22.  */
  23.  
  24. Image *loadImage(globalopts, options, name, verbose)
  25.      OptionSet    *globalopts;
  26.      OptionSet    *options;
  27.      char         *name;
  28.      unsigned int  verbose;
  29. { char    fullname[BUFSIZ];
  30.   Option *opt;
  31.   Image  *image;
  32.   int     a;
  33.  
  34.   if (findImage(name, fullname) < 0) {
  35.     if (errno == ENOENT)
  36.       fprintf(stderr, "%s: image not found\n", name);
  37.     else
  38.       perror(fullname);
  39.     return(NULL);
  40.   }
  41.  
  42.   /* see if there's a "type" option and use it if there is
  43.    */
  44.   if (! (opt= getOption(globalopts, TYPE)))
  45.     opt= getOption(options, TYPE);
  46.   if (opt) {
  47.     for (a= 0; ImageTypes[a].loader; a++)
  48.       if (!strncmp(ImageTypes[a].type, opt->info.type, strlen(opt->info.type))) {
  49.     if (image= ImageTypes[a].loader(fullname, name, verbose)) {
  50.       zreset(NULL);
  51.  
  52.       /* this converts a 1-bit RGB image to a bitmap prior to blitting
  53.        * it across.  it's a transmission efficiency hack, and has the
  54.        * beneficial side-effect of eliminating dithering on RGB images
  55.        * that don't really need it.
  56.        */
  57.       if (RGBP(image) && (image->rgb.used <= 2)) {
  58.           Image *new_image;
  59.           new_image = flatten(image);
  60.           if (new_image != image) {
  61.           freeImage(image);
  62.           image = new_image;
  63.           }
  64.       }
  65.       return(image);
  66.     }
  67.     fprintf(stderr, "%s does not look like a \"%s\" image (skipping).\n",
  68.         fullname, opt->info.type); 
  69.     zreset(NULL);
  70.     return(NULL);
  71.       }
  72.     fprintf(stderr, "\"%s\" is not a supported image type (will try to guess the type)\n", opt->info.type);
  73.  
  74.     /* fall through into the type determination loop
  75.      */
  76.   }
  77.  
  78.   /* try to pick out the image type
  79.    */
  80.   for (a= 0; ImageTypes[a].loader; a++) {
  81.     debug(("Checking %s against loader for %s\n", fullname, ImageTypes[a].name));
  82.     if (image= ImageTypes[a].loader(fullname, name, verbose)) {
  83.       zreset(NULL);
  84.  
  85.       /* this does the 1-bit conversion as above.
  86.        */
  87.       if (RGBP(image) && (image->rgb.used <= 2)) {
  88.       Image *new_image;
  89.       new_image = flatten(image);
  90.       if (new_image != image) {
  91.           freeImage(image);
  92.           image = new_image;
  93.       }
  94.       }
  95.       return(image);
  96.     }
  97.   }
  98.   fprintf(stderr, "%s: unknown or unsupported image type\n", fullname);
  99.   zreset(NULL);
  100.   return(NULL);
  101. }
  102.  
  103. /* identify what kind of image a named image is
  104.  */
  105.  
  106. void identifyImage(name)
  107.      char *name;
  108. { char fullname[BUFSIZ];
  109.   int  a;
  110.  
  111.   if (findImage(name, fullname) < 0) {
  112.     if (errno == ENOENT)
  113.       fprintf(stderr, "%s: image not found\n", name);
  114.     else
  115.       perror(fullname);
  116.     return;
  117.   }
  118.   for (a= 0; ImageTypes[a].identifier; a++) {
  119.     if (ImageTypes[a].identifier(fullname, name)) {
  120.       zreset(NULL);
  121.       return;
  122.     }
  123.   }
  124.   zreset(NULL);
  125.   fprintf(stderr, "%s: unknown or unsupported image type\n", fullname);
  126. }
  127.  
  128. /* dump an image into an image file of the specified type
  129.  */
  130. void dumpImage(image, type, filename, verbose)
  131.      Image *image;
  132.      char *type;
  133.      char *filename;
  134.      int verbose;
  135. { int a;
  136.   char typename[32];
  137.   char *optptr;
  138.  
  139.   /* find options if there are any
  140.    */
  141.   optptr = index(type, ',');
  142.   if (optptr) {
  143.     strncpy(typename, type, optptr - type);
  144.     typename[optptr - type] = '\0';
  145.     optptr++; /* skip comma */
  146.   }
  147.   else
  148.     strcpy(typename, type);
  149.  
  150.   for (a= 0; ImageTypes[a].loader; a++)
  151.     if (!strncmp(ImageTypes[a].type, typename, strlen(typename))) {
  152.       if (ImageTypes[a].dumper)
  153.     ImageTypes[a].dumper(image, optptr, filename, verbose);
  154.       else
  155.     fprintf(stderr, "\
  156. %s: I can read this image type but cannot write it (sorry).\n\
  157. If you need a list of supported image types, use the -supported option.\n",
  158.         type);
  159.       return;
  160.     }
  161.   fprintf(stderr, "\
  162. %s: unknown or unsupported image type.  If you need a list\n\
  163. of supported image types, use the -supported option.\n", type);
  164. }
  165.  
  166. /* tell user what image types we support
  167.  */
  168. void supportedImageTypes()
  169. { int a;
  170.  
  171.   printf("Type Name  Can Dump Description\n");
  172.   printf("---------- -------- -----------\n");
  173.   for (a= 0; ImageTypes[a].name; a++)
  174.     printf("%-10s %-8s %s\n", ImageTypes[a].type, 
  175.        (ImageTypes[a].dumper ? "Yes" : "No"),
  176.        ImageTypes[a].name);
  177. }
  178.