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

  1. /*
  2.  * mac.c:
  3.  *
  4.  * adapted from code by Patrick Naughton (naughton@sun.soe.clarkson.edu)
  5.  *
  6.  * macin.c
  7.  * Mark Majhor
  8.  * August 1990
  9.  *
  10.  * routines for reading MAC files
  11.  *
  12.  * Copyright 1990 Mark Majhor (see the included file
  13.  * "mrmcpyrght.h" for complete copyright information)
  14.  */
  15.  
  16. /* Edit History
  17.  
  18. 04/15/91   2 nazgul    Check for end of file instead of using -1 as a runlength!
  19.  
  20. */
  21. # include <stdio.h>
  22. # include <math.h>
  23. # include <ctype.h>
  24. # include "image.h"
  25. # include "mac.h"
  26.  
  27. /****
  28.  **
  29.  ** local variables
  30.  **
  31.  ****/
  32.  
  33. static BYTE file_open = 0;    /* status flags */
  34. static BYTE image_open = 0;
  35.  
  36. static ZFILE *ins;        /* input stream */
  37.  
  38. /****
  39.  **
  40.  ** global variables
  41.  **
  42.  ****/
  43.  
  44. static int  macin_img_width;           /* image width */
  45. static int  macin_img_height;          /* image height */
  46. static int  macin_img_depth;           /* image depth */
  47. static int  macin_img_planes;           /* image planes */
  48. static int  macin_img_BPL;           /* image bytes per line */
  49.  
  50. /*
  51.  * open MAC image in the input stream; returns MACIN_SUCCESS if
  52.  * successful. (might also return various MACIN_ERR codes.)
  53.  */
  54. /* ARGSUSED */
  55. static int macin_open_image(s)
  56. ZFILE *s;
  57. {
  58.   BYTE mhdr[MAC_HDR_LEN];
  59.   char *hp;        /* header pointer */
  60.  
  61.   /* make sure there isn't already a file open */
  62.   if (file_open)
  63.     return(MACIN_ERR_FAO);
  64.  
  65.   /* remember that we've got this file open */
  66.   file_open = 1;
  67.   ins = s;
  68.  
  69.   /*
  70.    * the mac paint files that came with xmac had an extra
  71.    * 128 byte header on the front, with a image name in it.
  72.    * true mac paint images don't seem to have this extra
  73.    * header.  The following code tries to figure out what
  74.    * type the image is and read the right amount of file
  75.    * header (512 or 640 bytes).
  76.    */
  77.   /* read in the mac file header */
  78.   hp = (char *) mhdr;
  79.   if (zread(ins, (byte *)hp, ADD_HDR_LEN) != ADD_HDR_LEN)
  80.     return MACIN_ERR_EOF;
  81.  
  82.   if (mhdr[0] != MAC_MAGIC)
  83.     return MACIN_ERR_BAD_SD;
  84.  
  85.   /* Get image name  (if available) */
  86.   if (mhdr[1] != 0) {                /* if name header */
  87.     if (zread(ins, (byte *)hp, MAC_HDR_LEN) != MAC_HDR_LEN)
  88.       return MACIN_ERR_EOF;
  89.   } else
  90.     /* else read rest of header */
  91.     if (zread(ins, (byte *)hp, MAC_HDR_LEN - ADD_HDR_LEN) != MAC_HDR_LEN - ADD_HDR_LEN)
  92.       return MACIN_ERR_EOF;
  93.  
  94.   /* Now set relevant values */
  95.   macin_img_width  = BYTES_LINE * 8;
  96.   macin_img_height = MAX_LINES;
  97.   macin_img_depth  = 1;        /* always monochrome */
  98.   macin_img_planes = 1;        /* always 1 */
  99.   macin_img_BPL    = BYTES_LINE;
  100.  
  101.   return MACIN_SUCCESS;
  102. }
  103.  
  104. /*
  105.  * close an open MAC file
  106.  */
  107.  
  108. static int macin_close_file()
  109. {
  110.   /* make sure there's a file open */
  111.   if (!file_open)
  112.     return MACIN_ERR_NFO;
  113.  
  114.   /* mark file (and image) as closed */
  115.   file_open  = 0;
  116.   image_open = 0;
  117.  
  118.   /* done! */
  119.   return MACIN_SUCCESS;
  120. }
  121.  
  122. #if 0
  123. /*
  124.  * semi-graceful fatal error mechanism
  125.  */
  126.  
  127. static macin_fatal(msg)
  128.      char *msg;
  129. {
  130.   printf("Error reading MacPaint file: %s\n", msg);
  131.   exit(0);
  132. }
  133. #endif
  134.  
  135. /*
  136.  * these are the routines added for interfacing to xloadimage
  137.  */
  138.  
  139. /*
  140.  * tell someone what the image we're loading is.  this could be a little more
  141.  * descriptive but I don't care
  142.  */
  143.  
  144. static void tellAboutImage(name)
  145. char *name;
  146. {
  147.   printf("%s is a %dx%d MacPaint image\n",
  148.     name, macin_img_width, macin_img_height);
  149. }
  150.  
  151. Image *macLoad(fullname, name, verbose)
  152.      char         *fullname, *name;
  153.      unsigned int  verbose;
  154.   ZFILE *zf;
  155.   Image *image;
  156.   BYTE *pixptr, ch;
  157.   int    eof;
  158.   register int scanLine;
  159.   register unsigned int i, j, k;
  160.  
  161.   if (! (zf = zopen(fullname)))
  162.     return(NULL);
  163.   if (macin_open_image(zf) != MACIN_SUCCESS) {  /* read image header */
  164.     macin_close_file();
  165.     zclose(zf);
  166.     return(NULL);
  167.   }
  168.  
  169.   image = newBitImage(macin_img_width, macin_img_height);
  170.  
  171.   pixptr = &(image->data[0]);
  172.   scanLine = 0; k = 0;
  173.  
  174.   while (scanLine < macin_img_height) {
  175.       if ((eof = zgetc(zf)) == -1) break;
  176.       ch = (BYTE) eof;    /* Count byte */
  177.       i = (unsigned int) ch;
  178.       if (ch < 0x80) {    /* Unpack next (I+1) chars as is */
  179.       for (j = 0; j <= i; j++) {
  180.           if (scanLine < macin_img_height) {
  181.           if ((eof = zgetc(zf)) == -1) break;
  182.           *pixptr++ = (BYTE) eof;
  183.           k++;
  184.           if (!(k %= BYTES_LINE)) {
  185.               scanLine++;
  186.           }
  187.           }
  188.       }
  189.       } else {    /* Repeat next char (2's comp I) times */
  190.       if ((eof = zgetc(zf)) == -1) break;
  191.       ch = (BYTE) eof;
  192.       for (j = 0; j <= 256 - i; j++) {
  193.           if (scanLine < macin_img_height) {
  194.           *pixptr++ = (BYTE) ch;
  195.           k++;
  196.           if (!(k %= BYTES_LINE)) {
  197.               scanLine++;
  198.           }
  199.           }
  200.       }
  201.       }
  202.   }
  203.   if (scanLine < macin_img_height) {
  204.       zclose(zf);
  205.       return NULL;
  206.   }
  207.   macin_close_file();
  208.  
  209.   if (verbose)
  210.     tellAboutImage(name);
  211.  
  212.   zclose(zf);
  213.   image->title = dupString(name);
  214.   return(image);
  215. }
  216.  
  217. int macIdent(fullname, name)
  218. char *fullname, *name;
  219. {
  220.   ZFILE        *zf;
  221.   unsigned int  ret;
  222.  
  223.   if (! (zf = zopen(fullname)))
  224.     return(0);
  225.   if (macin_open_image(zf) == MACIN_SUCCESS) {
  226.     tellAboutImage(name);
  227.     ret = 1;
  228.   } else
  229.     ret = 0;
  230.   macin_close_file();
  231.   zclose(zf);
  232.   return(ret);
  233. }
  234.