home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / scribepi.sha / extract_top.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-29  |  4.8 KB  |  185 lines

  1. /*
  2.    Yet another hack with P. Rowley's (U Toronto) macpaint code - this 
  3.    extracts the top left portion and spits it out in hex, for putting illo's
  4.    in Scribe files.
  5.  
  6.    J.W. Peterson
  7.    Computer Science Department
  8.    University of Utah.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #define and &&
  14. #define or ||
  15.  
  16. /*  Flags & defaults for user */
  17.  
  18. #define FFLAG          'f'    /* -f macpaint_file (input macpaint file) */
  19. #define COLFLAG        'c'    /* -c col_width (column width to center on) */
  20. #define HEIGHTFLAG     'h'      /* -h height    (height of Scribe picture) */
  21. #define SIZEFLAG       's'      /* -s x y   (Size of macpaint picture (in pixels)) */
  22. #define UPFLAG         'u'    /* -u updist (amount to translate up) */
  23. #define POSTFLAG       'P'      /* -P postfile (PostScript file to use) */
  24.  
  25. /* Default values */
  26.  
  27. /* These determine the size of the picture (values here are for exactly the
  28.  * size of the window MacPaint displays (416 x 240)) */
  29.  
  30. /* This defines the width of the scanline accepted, IN BYTES (which equals
  31.  * the number of pixels / 8)
  32.  */
  33. #define LINE_WIDTH  52
  34. #define DEF_SCANLINES      240     /* This defines the height (# of scanlines) */
  35.  
  36. #define OUTDEF         stdout
  37. #define POSTDEF        "/v/misc/mac/lw/scribe_pics/scribepic.ps"
  38.  
  39. #define COLUMNDEF      6.5     /*  Assume 6.5" text column */
  40. #define HEIGHTDEF      2       /*  Assume 2" height */
  41. #define UPDEF          0       /*  Assume 0" move UP */
  42.  
  43. int line_width, scanlines;
  44.  
  45. char in_line[72];
  46.  
  47. FILE *infil, *fopen();
  48.  
  49. main(argc,argv) 
  50. int argc;
  51. char *argv[];
  52. {
  53.     int i, j, c;
  54.     int size_x, size_y;            /* Size of MacPaint picture grabbed*/
  55.     float columnwidth, height, up, atof();
  56.     FILE *fpost;
  57.     char postfile[255], filename[255];
  58.  
  59.     infil = stdin;      /* assume stdin */
  60.     filename[0] = '\0';
  61.     strcpy(postfile, POSTDEF);
  62.  
  63.     /* Scribe defaults */
  64.     height = HEIGHTDEF;
  65.     columnwidth = COLUMNDEF;
  66.     up = UPDEF;
  67.  
  68.     /* Default size clipped from MacPaint page */
  69.     size_x = LINE_WIDTH * 8;
  70.     size_y = DEF_SCANLINES;
  71.  
  72.     /* Command line parsing */
  73.  
  74.     for (i=1; i<argc; i++)
  75.     {
  76.     if (argv[i][0] == '-')
  77.     {
  78.         switch (argv[i][1])
  79.         {
  80.         case FFLAG:    strcpy(filename, argv[i+1]);
  81.                        break;
  82.              
  83.                 case POSTFLAG: strcpy(postfile, argv[i+1]);
  84.                                break;
  85.  
  86.         case COLFLAG: columnwidth = atof( argv[i+1] );
  87.                        break;
  88.  
  89.         case HEIGHTFLAG: height = atof( argv[i+1] );
  90.                          break;
  91.  
  92.         case UPFLAG:   up = atof( argv[i+1] );
  93.                        break;
  94.  
  95.         case SIZEFLAG: size_x = atoi( argv[i+1] );
  96.                        size_y = atoi( argv[i+2] );
  97.                    break;
  98.         default:
  99.                                printf(
  100. "Usage: extract_top [-f macpaintfile] [-P header.ps] [-c width] [-h height] \n                        [-s x_size y_size] [-u up]\n");
  101.                        exit(1);
  102.                    break;
  103.         }
  104.     }
  105.     }
  106.  
  107.     if ((strlen(filename) > 0) and ((infil = fopen(filename, "r")) == NULL)) {
  108.         printf("Can't open input file %s\n", filename);
  109.         exit(1);
  110.     }
  111.  
  112.     if ((fpost = fopen(postfile, "r")) == NULL) {
  113.         printf("Can't open PostScript header file %s\n", postfile);
  114.         exit(1);
  115.     }
  116.  
  117.     /* read and discard 512 byte MacPaint header */
  118.     for (i=0; i<512; i++)
  119.         getc(infil);
  120.  
  121.     /* Copy the postscript header to stdout */
  122.     while ((c = getc(fpost)) != EOF)
  123.         putc(c, stdout);
  124.  
  125.     line_width = (int) size_x / 8;    /* Convert to number of bytes */
  126.     scanlines = size_y;
  127.  
  128.     /* Print a preamble describing size and shape */
  129.     printf( "1 %d %d %f %f %f\n", line_width, scanlines,
  130.              columnwidth, height, up );
  131.  
  132.     /* read and process each of scanlines scan lines (others get tossed) */
  133.     for (i=0; i < scanlines; i++)    
  134.     {
  135.         do_scan();
  136.     }
  137. }
  138.  
  139. do_scan() {
  140.     int in_pos, count, data_byte, mcount;
  141.     unsigned int raw_data;
  142.  
  143.     in_pos = 0;
  144.     while (in_pos < 72) {
  145.         count = getc(infil);
  146.  
  147.          if (count > 127) count -= 256;
  148.  
  149.         /* Prevent computed count from exceding line_width */
  150.         mcount = count;        /* Modified count */
  151.  
  152.         /* run too long */
  153.                 if ((count >= 0) and ((count + 1 + in_pos) > line_width))
  154.             mcount = line_width - 1 - in_pos;
  155.  
  156.         /* Data string too long */
  157.         if ((count < 0) and ((-count + 1 + in_pos) > line_width))
  158.             mcount = -( line_width - in_pos -1 );
  159.             
  160.         if (in_pos < line_width)
  161.             printf ( "%02X", (mcount+256) & 0xFF );
  162.  
  163.         if (count >= 0) {        /* run of raw bytes */
  164.             count++;        /* # of bytes to read */
  165.             while (count--) {
  166.                 raw_data = getc(infil);
  167.                 if (in_pos < line_width)
  168.                     printf ( "%02X", raw_data );
  169.                 in_pos++;
  170.             }
  171.         }
  172.         else {                /* run of repeated byte */
  173.             count = -count+1;    /* repetition factor */
  174.             data_byte = getc(infil);  /* byte to repeat */
  175.             if (in_pos < line_width)
  176.                 printf ( "%02X", data_byte );
  177.  
  178.             while (count--)        /* Kinda dumb... */
  179.                 in_pos++;
  180.         }
  181.     }
  182.     printf ("\n");
  183. }
  184.  
  185.