home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / flrot.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  6KB  |  208 lines

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