home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / fbm / part04 / flrot.c < prev    next >
C/C++ Source or Header  |  1989-06-08  |  5KB  |  202 lines

  1. /*****************************************************************
  2.  * flrot.c: FBM Library 0.9 (Beta test) 07-Mar-89  Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989 by Michael Mauldin.  Permission is granted to
  5.  * use this file in whole or in part provided that you do not sell it
  6.  * for profit and that this copyright notice is retained unchanged.
  7.  *
  8.  * flrot.c: 
  9.  *
  10.  * CONTENTS
  11.  *    rotate_fbm (input, output, rot)
  12.  *
  13.  * EDITLOG
  14.  *    LastEditDate = Tue Mar  7 19:57:29 1989 - Michael Mauldin
  15.  *    LastFileName = /usr2/mlm/src/misc/fbm/flrot.c
  16.  *
  17.  * HISTORY
  18.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  19.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  20.  *
  21.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  22.  *    Created.
  23.  *****************************************************************/
  24.  
  25. # include <stdio.h>
  26. # include <math.h>
  27. # include <ctype.h>
  28. # include "fbm.h"
  29.  
  30. /****************************************************************
  31.  * rotate_fbm: Rotate input bitmap
  32.  ****************************************************************/
  33.  
  34. #ifndef lint
  35. static char *fbmid =
  36.     "$FBM flrot.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  37. #endif
  38.  
  39. rotate_fbm (input, output, rot)
  40. FBM *input, *output;
  41. int rot;
  42. {
  43.   switch (rot)
  44.   { case 90:    return (rot90_fbm  (input, output));
  45.     case 180:    return (rot180_fbm (input, output));
  46.     case 270:    return (rot270_fbm (input, output));
  47.     default:    fprintf (stderr, "%s %d degrees, must be 90, 180, or 270\n",
  48.              "invalid rotation", rot);
  49.         return (0);
  50.   }
  51. }
  52.  
  53. /****************************************************************
  54.  * rot90_fbm: Rotate input bitmap 90 degrees clockwise
  55.  ****************************************************************/
  56.  
  57. rot90_fbm (input, output)
  58. FBM *input, *output;
  59. { register int i, j, k, oi, oj;
  60.   int iw, ow, ih, oh, irow, orow, ipln, opln;
  61.  
  62.   if (input->hdr.physbits != 8)
  63.   { fprintf (stderr,
  64.         "Can't handle images %d physical bits per pixel\n",
  65.         input->hdr.physbits);
  66.     exit (1);
  67.   }
  68.  
  69.   oh = iw = input->hdr.cols;
  70.   ow = ih = input->hdr.rows;
  71.   
  72.   irow = input->hdr.rowlen;
  73.   ipln = input->hdr.plnlen;
  74.  
  75.   /* Calculate row length (input height padded to even byte boundary) */
  76.   if (input->hdr.bits == 1)
  77.   { orow = 16 * ((ow + 15) / 16); }
  78.   else
  79.   { orow = 2 * ((ow * input->hdr.physbits + 15) / 16); }
  80.  
  81.   opln = orow * oh; 
  82.  
  83.   /* Now build header for output bit map */
  84.   output->hdr = input->hdr;
  85.   output->hdr.cols = ow;
  86.   output->hdr.rows = oh;
  87.   output->hdr.rowlen = orow;
  88.   output->hdr.plnlen = opln;
  89.   output->hdr.aspect = 1.0 / input->hdr.aspect;
  90.   
  91.   /* Allocate space for output bits */
  92.   alloc_fbm (output);
  93.   
  94.   copy_clr (input, output);
  95.  
  96.   for (k=0; k<output->hdr.planes; k++)
  97.   { for (j=0; j<ih; j++)
  98.     { for (i=0; i<iw; i++)
  99.       { oj = i; oi = ih - (j+1);
  100.         output->bm[k*opln + oj*orow + oi] = input->bm[k*ipln + j*irow + i];
  101.       }
  102.     }
  103.   }
  104.  
  105.   return (1);
  106. }
  107.  
  108. /****************************************************************
  109.  * rot180_fbm: Rotate input bitmap 180 degrees clockwise
  110.  ****************************************************************/
  111.  
  112. rot180_fbm (input, output)
  113. FBM *input, *output;
  114. { register int i, j, k, oi, oj;
  115.   int w, h, row, pln;
  116.  
  117.   if (input->hdr.physbits != 8)
  118.   { fprintf (stderr,
  119.          "Can't handle images %d physical bits per pixel\n",
  120.          input->hdr.physbits);
  121.     exit (1);
  122.   }
  123.  
  124.   /* Now build header for output bit map */
  125.   output->hdr = input->hdr;
  126.   w = input->hdr.cols;
  127.   h = input->hdr.rows;
  128.   row = input->hdr.rowlen;
  129.   pln = input->hdr.plnlen;
  130.   
  131.   /* Allocate space for output bits */
  132.   alloc_fbm (output);
  133.   
  134.   copy_clr (input, output);
  135.  
  136.   for (k=0; k<output->hdr.planes; k++)
  137.   { for (j=0; j<h; j++)
  138.     { for (i=0; i<w; i++)
  139.       { oj = h - (j+1); oi = w - (i+1);
  140.         output->bm[k*pln + oj*row + oi] = input->bm[k*pln + j*row + i];
  141.       }
  142.     }
  143.   }
  144.  
  145.   return (1);
  146. }
  147.  
  148. /****************************************************************
  149.  * rot270_fbm: Rotate input bitmap 270 degrees clockwise
  150.  ****************************************************************/
  151.  
  152. rot270_fbm (input, output)
  153. FBM *input, *output;
  154. { register int i, j, k, oi, oj;
  155.   int iw, ow, ih, oh, irow, orow, ipln, opln;
  156.  
  157.   if (input->hdr.physbits != 8)
  158.   { fprintf (stderr,
  159.         "Can't handle images %d physical bits per pixel\n",
  160.         input->hdr.physbits);
  161.     exit (1);
  162.   }
  163.  
  164.   oh = iw = input->hdr.cols;
  165.   ow = ih = input->hdr.rows;
  166.   
  167.   irow = input->hdr.rowlen;
  168.   ipln = input->hdr.plnlen;
  169.  
  170.   /* Calculate row length (input height padded to even byte boundary) */
  171.   if (input->hdr.bits == 1)
  172.   { orow = 16 * ((ow + 15) / 16); }
  173.   else
  174.   { orow = 2 * ((ow * input->hdr.physbits + 15) / 16); }
  175.  
  176.   opln = orow * oh;
  177.   
  178.   /* Now build header for output bit map */
  179.   output->hdr = input->hdr;
  180.   output->hdr.cols = ow;
  181.   output->hdr.rows = oh;
  182.   output->hdr.rowlen = orow;
  183.   output->hdr.plnlen = opln;
  184.   output->hdr.aspect = 1.0 / input->hdr.aspect;
  185.   
  186.   /* Allocate space for output bits */
  187.   alloc_fbm (output);
  188.   
  189.   copy_clr (input, output);
  190.  
  191.   for (k=0; k<output->hdr.planes; k++)
  192.   { for (j=0; j<ih; j++)
  193.     { for (i=0; i<iw; i++)
  194.       { oj = iw - (i+1); oi = j;
  195.         output->bm[k*opln + oj*orow + oi] = input->bm[k*ipln + j*irow + i];
  196.       }
  197.     }
  198.   }
  199.  
  200.   return (1);
  201. }
  202.