home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / imgflip.c < prev    next >
C/C++ Source or Header  |  1991-01-07  |  5KB  |  199 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    imgflip.c (Image Flip)
  6.  * Purpose:    Rotate buffer 180 degrees on any orthogonal axis
  7.  * Subroutine:    xflip_buf()            returns: void
  8.  * Subroutine:    yflip_buf()            returns: void
  9.  * Subroutine:    zflip_buf()            returns: void
  10.  * Subroutine:    transfer_buf()            returns: void
  11.  * Copyright:    1988 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} Michael VanHilst    initial version         8 December 1988
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21.  
  22. /*  Subroutine:    xflip_buf
  23.  *  Purpose:    Flip buf to make Y coordinates run in opposite direction
  24.  *        (upside down)
  25.  */
  26. #ifdef ANSIC
  27. void xflip_buf ( short *buf, int cols, int rows )
  28. #else
  29. void xflip_buf ( buf, cols, rows )
  30.      short *buf;
  31.      int cols, rows;
  32. #endif
  33. {
  34.   register int bytes; 
  35.   short *linebuf;
  36.   short *toprow, *botrow;
  37.   char *calloc_errchk();
  38.  
  39.   linebuf = (short *)calloc_errchk(cols, sizeof(short), "Temp line");
  40.   toprow = buf;
  41.   botrow = buf + ((rows - 1) * cols);
  42.   bytes = cols * sizeof(short);
  43.   while( toprow < botrow ) {
  44. #ifdef ANSIC
  45.     (void)memcpy((void *)linebuf, (void *)botrow, (size_t)bytes);
  46.     (void)memcpy((void *)botrow, (void *)toprow, (size_t)bytes);
  47.     (void)memcpy((void *)toprow, (void *)linebuf, (size_t)bytes);
  48. #else
  49.     bcopy((char *)botrow, (char *)linebuf, bytes);
  50.     bcopy((char *)toprow, (char *)botrow, bytes);
  51.     bcopy((char *)linebuf, (char *)toprow, bytes);
  52. #endif
  53.     toprow += cols;
  54.     botrow -= cols;
  55.   }
  56.   free((char *)linebuf);
  57. }
  58.  
  59.  
  60. /*  Subroutine:    yflip_buf
  61.  *  Purpose:    Mirror buf to make x coordinates run in opposite direction
  62.  */
  63. #ifdef ANSIC
  64. void yflip_buf ( short *buf, int cols, int rows )
  65. #else
  66. void yflip_buf ( buf, cols, rows )
  67.      short *buf;
  68.      int cols, rows;
  69. #endif
  70. {
  71.   short *row;
  72.   register short *right, *left;
  73.   int wdth;
  74.   int i;
  75.   short temp;
  76.  
  77.   row = buf;
  78.   wdth = cols - 1;
  79.   for( i=0; i<rows; i++ ) {
  80.     right = row;
  81.     left = right + wdth;
  82.     do {
  83.       temp = *right;
  84.       *right = *left;
  85.       *left = temp;
  86.     } while( ++right < --left );
  87.     row += cols;
  88.   }
  89. }
  90.  
  91.  
  92. /* Subroutine:    zflip_buf
  93.  * Purpose:    Turn buf upside down (180 degree rotation on image plane)
  94.  */
  95. #ifdef ANSIC
  96. void zflip_buf ( short *buf, int cols, int rows )
  97. #else
  98. void zflip_buf ( buf, cols, rows )
  99.      short *buf;
  100.      int cols, rows;
  101. #endif
  102. {
  103.   register short *top, *bottom;
  104.   short temp;
  105.  
  106.   top = buf;
  107.   bottom = buf + (cols * rows) - 1;
  108.   do {
  109.     temp = *top;
  110.     *top = *bottom;
  111.     *bottom = temp;
  112.   } while( ++top < --bottom );
  113. }
  114.  
  115.  
  116. /* Subroutine:    transfer_buf
  117.  * Purpose:    Move short values to another buffer with optional reorientation
  118.  */
  119. #ifdef ANSIC
  120. void transfer_buf ( register short *src, register short *dst,
  121.             int cols, int rows, int rotcode )
  122. #else
  123. void transfer_buf ( src, dst, cols, rows, rotcode )
  124.      register short *src;
  125.      register short *dst;
  126.      int cols, rows;
  127.      int rotcode;
  128. #endif
  129. {
  130.   register short *src_row_end;
  131.   short *src_buf_end;
  132.   int dst_inc;
  133.   int dst_next_row;
  134.  
  135.   switch( rotcode ) {
  136.   case 0:
  137. #ifdef ANSIC
  138.     (void)memcpy((void *)dst, (void *)src,
  139.          (size_t)(sizeof(short) * (cols * rows)));
  140. #else
  141.     bcopy((char *)src, (char *)dst, sizeof(short) * (cols * rows));
  142. #endif
  143.     return;
  144.   case 1:
  145.     /*  Top right corner, down  */
  146.     dst += (rows - 1);
  147.     dst_inc = rows;
  148.     dst_next_row = -(1 + (cols * rows));
  149.     break;
  150.   case 2:
  151.     /*  Lower right corner, back  */
  152.     dst += ((rows * cols) - 1);
  153.     dst_inc = -1;
  154.     dst_next_row = 0;
  155.     break;
  156.   case 3:
  157.     /*  Lower left corner, up  */
  158.     dst += ((cols - 1) * rows);
  159.     dst_inc = -rows;
  160.     dst_next_row = 1 + (cols * rows);
  161.     break;
  162.   case 4:
  163.     /*  Top right corner, back  */
  164.     dst += (cols - 1);
  165.     dst_inc = -1;
  166.     dst_next_row = rows + rows - 1;
  167.     break;
  168.   case 5:
  169.     /*  Lower right corner, up  */
  170.     dst += ((cols * rows) - 1);
  171.     dst_inc = -rows;
  172.     dst_next_row = (cols * rows) - 1;
  173.     break;
  174.   case 6:
  175.     /*  Lower left corner, forward  */
  176.     dst += ((rows - 1) * cols);
  177.     dst_inc = 1;
  178.     dst_next_row = 1 - (rows + rows);
  179.     break;
  180.   case 7:
  181.     /*  Upper left, down  */
  182.     dst_inc = rows;
  183.     dst_next_row = 1 -(cols * rows);
  184.     break;
  185.   default:
  186.     return;
  187.   } 
  188.   src_buf_end = src + (cols * rows);
  189.   src_row_end = src + cols;
  190.   while( src < src_buf_end ) {
  191.     do {
  192.       *dst = *src;
  193.       dst += dst_inc;
  194.     } while( ++src < src_row_end );
  195.     src_row_end += cols;
  196.     dst += dst_next_row;
  197.   }
  198. }
  199.