home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / misc.zoo / misc / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  3.6 KB  |  143 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /* rotate a bitmap 90 deg clockwise (mgr format) */
  9.  
  10. /*    $Header: rotate.c,v 4.1 88/08/24 14:39:17 bianchi Exp $
  11.     $Source: /tmp/mgrsrc/misc/RCS/rotate.c,v $
  12. */
  13. static char    RCSid_[] = "$Source: /tmp/mgrsrc/misc/RCS/rotate.c,v $$Revision: 4.1 $";
  14.  
  15. #include <stdio.h>
  16. #include "dump.h"
  17. #include "bitmap.h"
  18.  
  19. #define MAX    2400        /* max bitmap size */
  20.  
  21. #define GET_OPT(i)    \
  22.     strlen(argv[i])>2 ? argv[i]+2 : argv[++i]
  23.  
  24. unsigned char buff[MAX];    /* output row buffer */
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char **argv;
  29.    {
  30.    char *malloc();
  31.    struct b_header b_buff, *head = &b_buff;
  32.    int w=0,h=0,d;
  33.    int inbytes,outbytes,size;
  34.    int reverse = 0;
  35.    int x_flag=0;
  36.  
  37.    register unsigned char *data;
  38.    register unsigned char *src;
  39.    register int bit,word;
  40.    register int col,row;
  41.    int ok;
  42.    int verbose = 0;
  43.    int i;
  44.  
  45.    /* check arguments */
  46.  
  47.    for(i=1;i<argc;i++) {
  48.       if (*argv[i] == '-')
  49.          switch (argv[i][1]) {
  50.             case 'w':                /* specify width */
  51.                w = atoi(GET_OPT(i));
  52.                break;
  53.             case 'h':                /* specify height */
  54.                h = atoi(GET_OPT(i));
  55.                break; 
  56.             case 'x':                /* don't output header */
  57.                x_flag++;
  58.                break; 
  59.             case 'r':                /* reverse bits */
  60.                reverse++;
  61.                break; 
  62.             case 'v':                /* verbose */
  63.                verbose++;
  64.                break; 
  65.             default:
  66.                fprintf(stderr,"%s: invalid flag %c ignored\n",argv[0],argv[i][1]);
  67.             }
  68.       else
  69.          fprintf(stderr,"%s: invalid argument %s ignored\n",argv[0],argv[i]);
  70.       }
  71.  
  72.  
  73.    if (w==0 && h==0) {
  74.  
  75.       /* read in bitmap header */
  76.  
  77.       if (!bitmaphead( stdin, &w, &h, &d, &inbytes )) {
  78.          fprintf(stderr,"%s: invalid bitmap format \n",*argv);
  79.          exit(1);
  80.          }
  81.       }
  82.  
  83.    else if (w==0 || h==0) {
  84.       fprintf(stderr,"%s:  Both -h and -w must be specified\n",*argv);
  85.       exit(1);
  86.       }
  87.       
  88.    if (w > MAX) {
  89.       fprintf(stderr,"%s:  bitmap too big\n",*argv);
  90.       exit(1);
  91.       }
  92.  
  93.    size = inbytes * h;            /* total bytes in bitmap */
  94.    outbytes = ((h+7)&~7)/8;        /* bytes/row (output) */
  95.    
  96.    if ((data= (unsigned char *) malloc(size)) == NULL) {
  97.       fprintf(stderr,"%s: can't malloc space\n",*argv);
  98.       exit(2);
  99.       }
  100.       
  101.    for(ok=1,col=0;ok>0 && col<size;col += (ok=Read(data+col,size-col)))
  102.       if (verbose) write(2,">",1);
  103.  
  104.    /* write new header */
  105.  
  106.    if (!x_flag) {
  107.       B_PUTHDR8(head,h,w,d);
  108.       fwrite(head,sizeof b_buff,1,stdout);
  109.       }
  110.  
  111.    /* rotate and output new bitmap */
  112.  
  113.    data += size - inbytes;        /* start at end */
  114.    for (col = 0;col< w; col++) {
  115.       bit = 0x80 >> (col&0x7);
  116.       word = col/8;
  117.       if (reverse)
  118.          for(src = &buff[outbytes]; src >= buff; src-- )
  119.         *src = ~0;
  120.       else
  121.          bzero(buff,outbytes);
  122.       for(src=data+word,row = 0; row< h; row++,src -= inbytes) 
  123.          if (*src & bit)
  124.             buff[row>>3] ^= 0x80 >> (row&0x7);
  125.       fwrite(buff,outbytes,1,stdout);
  126.       if (verbose) write(2,">",1);
  127.       }
  128.    exit(0);
  129.    }
  130.  
  131. /* do multiple passes for read */
  132.  
  133. Read(buff,count)
  134. register char *buff;
  135. int count;
  136.    {
  137.    register int sum=0,current=0;
  138.  
  139.    while((current = fread(buff+sum,1,count-sum,stdin))>0)
  140.       sum += current;
  141.    return(sum);
  142.    }
  143.