home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / printers / magimprs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  7.8 KB  |  269 lines

  1. 12-Jan-86 08:58:08-MST,8176;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Sun 12 Jan 86 08:57:59-MST
  4. Received: from usenet by TGR.BRL.ARPA id a021853; 11 Jan 86 16:34 EST
  5. From: Geoffrey Cooper <geof@imagen.uucp>
  6. Newsgroups: net.sources
  7. Subject: Pgm to manip. IMPRESS bitmaps - good for screen dumps
  8. Message-ID: <173@imagen.UUCP>
  9. Date: 8 Jan 86 21:22:45 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. /*
  13.  * Magbitmap.c - Program to change the magnification of an IMPRESS bitmap.
  14.  * 
  15.  * This program searches the argument file for IMPRESS magnification or
  16.  * bitmap commands.  It interprets the commands and produces an output
  17.  * impress file which contains a compatible magnification command and
  18.  * a set of bitmap commands that divide the bitmap across multiple pages
  19.  * of output, as necessary.
  20.  * 
  21.  * An argument allows the magnification of the bitmap to be changed.  In
  22.  * this mode, the program makes it possible to scale a bitmap to a size
  23.  * larger than 8.5X11 inches.  One use of the program is to scale screen
  24.  * dumps to magnification level 2 (four times the original size).
  25.  * 
  26.  * The program was written for Apollo DOMAIN/IX 4.2 emulation.  It relies
  27.  * only on the stdio package, and should run under any C runtime system.
  28.  * The input file is scanned in multiple passes.  This is a feature,
  29.  * since it allows the program to run on machines with a small address
  30.  * space.
  31.  * 
  32.  * Written January 8, 1985 by Geoffrey H. Cooper of IMAGEN
  33.  * 
  34.  * Copyright (C) 1985, IMAGEN Corporation
  35.  * This software may be copied, modified, and put to any non-commercial
  36.  * use so long as the copyright notice is preserved.
  37.  *
  38.  * Bugs: - Should be interpreting impress to find the bitmap
  39.  *       - Will only find one bitmap in a file - not self recursive
  40.  *       - Should allow for cropping of the bitmap on patch boundaries
  41.  */
  42.  
  43. #include <stdio.h>
  44.  
  45. int columns, rows;              /* # columns and rows in the bitmap */
  46. int magnification;              /* bitmap magnification */
  47. int operation;                  /* bitmap operation */
  48. long bitmap_pos;                /* fseek position of bitmap */
  49. int ask = 1;                    /* interactive mode if -1 */
  50. int overlap = 1;                /* # patches of overlap, default = 1 */
  51.  
  52. main( argc, argv )
  53.     int argc;
  54.     char **argv;
  55. {
  56.     char *ms;
  57.     char **av = argv;
  58.     FILE *f, *outf;
  59.     char *opt;
  60.     int mag = -1;
  61.  
  62.     while ( argv[1] && argv[1][0] == '-' ) {
  63.         opt = &argv[1][1];
  64.         while ( *opt ) {
  65.             switch ( *opt ) {
  66.               case 'i':
  67.                 ask = -1;
  68.                 break;
  69.  
  70.               case 'n':
  71.                 ask = 0;
  72.                 break;
  73.  
  74.               case 'm':
  75.                 if ( opt[1] )
  76.                     mag = atoi(opt+1);
  77.                 else {
  78.                         argv++;
  79.                         argc--;
  80.                         mag = atoi(argv[1]);
  81.                 }
  82.                 goto nextarg;
  83.  
  84.               case 'o':
  85.                 if ( opt[1] )
  86.                     overlap = atoi(opt+1);
  87.                 else {
  88.                         argv++;
  89.                         argc--;
  90.                         overlap = atoi(argv[1]);
  91.                 }
  92.                 goto nextarg;
  93.  
  94.               default:
  95.                 printf("unknown flag '%c'\n", *opt);
  96.                 goto usage;
  97.             }
  98.             opt++;
  99.         }
  100. nextarg:
  101.         argc--; argv++;
  102.     }
  103.  
  104.     if ( ! ((argc == 3) || (ask == 0 && argc == 2)) ) {
  105. usage:  printf("usage: %s [-n -i -m magnification -o overlap_patches] input_fname output_fname\n", av[0]);
  106.         exit(1);
  107.     }
  108.     if ( mag >= 0 ) 
  109.         printf("Forcing magnification to %d\n", mag);
  110.     f = fopen(argv[1], "r+");
  111.     if ( f == NULL ) {
  112.         perror( argv[1] );
  113.         exit(2);
  114.     }
  115.     if ( ask == 0 ) argv[2] = "/dev/null";
  116.     outf = fopen(argv[2], "w");
  117.     if ( outf == NULL ) {
  118.         perror( argv[2] );
  119.         exit(3);
  120.     }
  121.     ScaleBM(f, outf, mag);
  122.     fclose(f);
  123.     fclose(outf);
  124.     exit(0);
  125. }
  126.  
  127. ScaleBM(inf, outf, use_magnification)
  128.     FILE *inf, *outf;
  129. {
  130.     register c, i, j, r;
  131.     int columnsPerPage, rowsPerPage;
  132.     int page_num;
  133.     float inches;
  134.  
  135.     magnification = 1;
  136.     i = 0;
  137.     for (;;) {
  138.         /* Find the impress magnification command */
  139.         while ( (c = getc(inf)) != EOF && c != 236 && c != 235 );
  140.         switch (c) {
  141.           case EOF:
  142.             printf("Early EOF\n");
  143.             return;
  144.           case 236:
  145.             magnification = getc(inf);
  146.             break;
  147.           case 235:
  148.             operation = getc(inf);
  149.             columns = getc(inf);
  150.             rows = getc(inf);
  151.             goto break2;
  152.         }
  153.     }
  154. break2:
  155.     bitmap_pos = ftell(inf);
  156.     printf("Bitmap command found at byte %d.\n", ftell(inf));
  157.     printf("Operation: %d, Horizontal patches: %d, Vertical Patches: %d\n",
  158.             operation, columns, rows);
  159.  
  160.     if ( use_magnification >= 0 )
  161.         magnification = use_magnification;
  162.  
  163.     columnsPerPage = 70 >> magnification;
  164.     rowsPerPage =    94 >> magnification;
  165.  
  166.     Setup(outf);
  167.     page_num = 1;
  168.     for ( i = 0; i < rows; i += rowsPerPage ) {
  169.         r = rows - i;
  170.         if ( r > rowsPerPage ) r = rowsPerPage + overlap;
  171.         for ( j = 0; j < columns; j += columnsPerPage ) {
  172.             c = columns - j;
  173.             if ( c > columnsPerPage ) c = columnsPerPage + overlap;
  174.             printf("Page %d, patches (%d,%d) -> (%d,%d)\n",
  175.                     page_num++, j, i, j+c, i+r);
  176.             if ( Confirm("Produce page") )
  177.                 PadBitmap(inf, outf, j, c, i, r);
  178.         }
  179.     }
  180.     putc(255, outf);    /* EOF */
  181.     fflush(outf);
  182. }
  183.  
  184. PadBitmap(inf, outf, start_h, nh, start_v, nv)
  185.     FILE *inf, *outf;
  186.     int start_h, nh, start_v, nv;
  187. {
  188.     register int h, v, i, c;
  189.     long seek;
  190.  
  191.     Bitmap(outf, nh, nv);
  192.  
  193.     for ( v = 0; v < nv; v++ ) {
  194.         seek = bitmap_pos + 
  195.                 ((long)(v + start_v) * columns * (32L*32L/8L)) +
  196.                   ((long)(start_h * (32*32/8)));
  197.         fseek(inf, seek, 0);
  198.         for ( h = 0; h < nh; h++ )
  199.             for ( i = 0; i < (32*32/8); i++ ) {
  200.                 if ( (c = getc(inf)) == EOF ) c = 0;
  201.                 putc(c, outf);
  202.             }
  203.     }
  204.     putc(219, outf);    /* ENDPAGE */
  205. }
  206.  
  207.  
  208. Setup(f)
  209.     FILE *f;
  210. {
  211.     fprintf (f, "@document(language impress)");
  212. }
  213.  
  214. Confirm(s)
  215.     char *s;
  216. {
  217.     int c, x;
  218.  
  219.     if ( ask != -1 ) return ( ask );
  220.     printf("%s? [yngq] ", s);
  221.     for (;;) {
  222.         c = getchar();
  223.         for ( x = c; x != '\n'; x = getchar() );
  224.         switch (c) {
  225.           case 'Y':   case 'y':
  226.           case '\r':  case '\n':
  227.             return ( 1 );
  228.  
  229.           case 'n':   case 'N':
  230.             return ( 0 );
  231.  
  232.           case 'g':
  233.             ask = 1;
  234.             printf("OK, will assume YES from now on.\n");
  235.             return ( 1 );
  236.  
  237.           case 'q':
  238.             ask = 0;
  239.             printf("OK, will assume NO from now on.\n");
  240.             return ( 0 );
  241.  
  242.           default:
  243.             printf("You must specify yes (y), no (n), quit (q), or go (g).\n");
  244.         }
  245.     }
  246. }
  247.  
  248. Bitmap(f, h, v)
  249.     FILE *f;
  250. {
  251.     putc(213, f);       /* PAGE message- sets up parameters for Impress pg. */
  252.  
  253.     putc(135, f);       /* SET-ABS-H: horiz. offset in points-req. 2 bytes */
  254.     putc(00, f) ;           /*offset currently set to 150/300 of in. in */
  255.     putc(150, f);           /*consideration of printer's margin error.  */
  256.  
  257.     putc(137, f);       /* SET-ABS-V: horiz. offset in points-req. 2 bytes */
  258.     putc(00, f) ;
  259.     putc(150, f);           /* offset set to 150/300 of an in. */
  260.  
  261.     putc(236, f);      /* Magnify following bitmap by power of 2 */
  262.     putc(magnification, f);
  263.  
  264.     putc(235, f);       /* Impress code for BITMAP */
  265.     putc(operation, f); /* operation-type = 'OR' mapping of bits */
  266.     putc(h, f);         /* HPATCHES = no. of patches horizontally */
  267.     putc(v, f);         /* no. of vertical lines of HPATCHES */
  268. }
  269.