home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Utilities / wwwcount-2.3 / combine / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-02  |  4.0 KB  |  220 lines

  1. /*
  2.  *    MrotateImage()    - rotates image an integral of 90 degrees
  3.  *                    
  4.  *
  5.  *    RCS:
  6.  *        $Revision: 2.3 $
  7.  *        $Date: 1996/05/03 02:21:34 $
  8.  *
  9.  *    Security:
  10.  *        Unclassified
  11.  *
  12.  *    Description:
  13.  *        From ImageMagick
  14.  *
  15.  *    Input Parameters:
  16.  *        type    identifier    description
  17.  *
  18.  *        Image   *image      image to rotate
  19.  *      int     degress     degrees to rotate (intergral of 90)
  20.  *
  21.  *    Output Parameters:
  22.  *        type    identifier    description
  23.  *
  24.  *        text
  25.  *
  26.  *    Return Values:
  27.  *        value    description
  28.  *
  29.  *    Side Effects:
  30.  *        text
  31.  *
  32.  *    Limitations and Comments:
  33.  *        rotation is performed on a copy of the passed image. the caller
  34.  *      shuld free the passed copy after obtaing the rotated copy of the
  35.  *      image.
  36.  *
  37.  *    Development History:
  38.  *        when    who        why
  39.  *    07/31/94    muquit    first cut
  40.  */
  41.  
  42. #include "combine.h"
  43.  
  44. Image *RotateImage(image, degrees)
  45. Image
  46.     *image;
  47. int
  48.     degrees;
  49. {
  50.     Image
  51.         *rotated_image;
  52.  
  53.     register Runlength
  54.         *p,
  55.         *q;
  56.             
  57.     register int
  58.         x,
  59.         y;
  60.  
  61.     unsigned int
  62.         rotations;
  63.  
  64.     if (degrees%90 != 0)
  65.     {
  66.         (void) fprintf (stderr," %d is not multiple of 90\n",degrees);
  67.         return ((Image *) NULL);
  68.     }
  69.  
  70.     if (degrees == 360)
  71.     {
  72.         (void) fprintf (stderr," No need to rotate %d\n",degrees);
  73.         return ((Image *) NULL);
  74.     }
  75.  
  76.     if(degrees < 0)
  77.     {
  78.         degrees+=360;
  79.     }
  80.  
  81.     for (rotations=0; degrees > 45; rotations++)
  82.     {
  83.         degrees-=90;
  84.     }
  85.  
  86.     rotations%=4;
  87.  
  88.     if ((rotations == 1) || (rotations == 3))
  89.     {
  90.         rotated_image=DuplicateImage(image, image->rows, image->columns,
  91.             False);
  92.     }
  93.     else
  94.     {
  95.         rotated_image =DuplicateImage(image, image->columns, image->rows,
  96.             False);
  97.     }
  98.  
  99.     if (rotated_image == (Image *) NULL)
  100.     {
  101.         (void) fprintf (stderr,
  102.             "Unable to rotate image,Memory allocation failed\n");
  103.         return ((Image *) NULL);
  104.     }
  105.  
  106.     p = image->pixels;
  107.     image->runlength = p->length+1;
  108.  
  109.     switch (rotations)
  110.     {
  111.         case 0:
  112.         {
  113.             /*
  114.             ** 0 degree rotation
  115.             */
  116.  
  117.             q=rotated_image->pixels;
  118.             for (y=0; y < image->rows; y++)
  119.             {
  120.                 for (x=0; x < image->columns; x++)
  121.                 {
  122.                     if (image->runlength != 0)
  123.                         image->runlength--;
  124.                     else
  125.                     {
  126.                         p++;
  127.                         image->runlength=p->length;
  128.                     }
  129.                     *q=(*p);
  130.                     q->length=0;
  131.                     q++;
  132.                 }
  133.             }
  134.             break;
  135.         }
  136.  
  137.         case 1:    
  138.         {
  139.             /*
  140.             ** 90 degrees
  141.             */
  142.         
  143.             for(x=0; x < rotated_image->columns; x++)
  144.             {
  145.                 q = rotated_image->pixels+(rotated_image->columns-x)-1;
  146.                 for (y=0; y < rotated_image->rows; y++)
  147.                 {
  148.                     if (image->runlength != 0)
  149.                         image->runlength--;
  150.                     else
  151.                     {
  152.                         p++;
  153.                         image->runlength = p->length;
  154.                     }
  155.  
  156.                 *q = (*p);
  157.                 q->length = 0;
  158.                 q+=rotated_image->columns;
  159.                 }
  160.             }
  161.             break;
  162.         }
  163.  
  164.         case 2:
  165.         {
  166.             /*
  167.             ** rotate 180 degrees
  168.             */
  169.            
  170.             q=rotated_image->pixels+
  171.                 (rotated_image->columns*rotated_image->rows)-1;
  172.             for (y=image->rows-1; y >=0; y--)
  173.             {
  174.                 for (x=0; x < image->columns; x++)
  175.                 {
  176.                     if (image->runlength != 0)
  177.                         image->runlength--;
  178.                     else
  179.                     {
  180.                         p++;
  181.                         image->runlength=p->length;
  182.                     }
  183.                     *q=(*p);
  184.                     q->length=0;
  185.                     q--;
  186.                 }
  187.             }
  188.             break;
  189.         }
  190.         case 3:
  191.         {
  192.             /*
  193.             ** 270 degress
  194.             */
  195.  
  196.             for(x=rotated_image->columns-1; x >=0; x--)
  197.             {
  198.                 q = rotated_image->pixels+(rotated_image->columns*
  199.                     rotated_image->rows)-x-1;
  200.                 for (y=0; y < rotated_image->rows; y++)
  201.                 {
  202.                     if (image->runlength != 0)
  203.                         image->runlength--;
  204.                     else
  205.                     {
  206.                         p++;
  207.                         image->runlength = p->length;
  208.                     }
  209.                 *q = (*p);
  210.                 q->length = 0;
  211.                 q-=rotated_image->columns;
  212.                 }
  213.             }
  214.             break;
  215.         }
  216.     }
  217.  
  218. return(rotated_image);
  219. }
  220.