home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / ImageOpr.bproj / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  3.6 KB  |  146 lines

  1. #include  <libc.h>
  2. #include  <math.h>
  3. #include  "../common.h"
  4. #include  "../getpixel.h"
  5.  
  6.  
  7. static double cosine, sine;
  8. static int xdiff, ydiff;
  9.  
  10. void rotate_size(int angle, commonInfo *cinf, commonInfo *newinf)
  11. {
  12.     if (angle == 90 || angle == 270) {
  13.         newinf->width = cinf->height;
  14.         newinf->height = cinf->width;
  15.     }else if (angle == 0 || angle == 180) {
  16.         newinf->width = cinf->width;
  17.         newinf->height = cinf->height;
  18.     }else {
  19.         double th = ((double)angle * 3.14159265) / 180.0;
  20.         double co = cos(th);
  21.         double si = sin(th);
  22.         if (co < 0) co = -co;
  23.         if (si < 0) si = -si;
  24.         newinf->width = cinf->width * co + cinf->height * si + 0.5;
  25.         newinf->height = cinf->width * si + cinf->height * co + 0.5;
  26.         if (angle > 270) {
  27.             xdiff = cinf->height * si - 0.5;
  28.             ydiff = 0.0;
  29.         }else if (angle > 180) {
  30.             xdiff = newinf->width - 1;
  31.             ydiff = cinf->height * co - 0.5;
  32.         }else if (angle > 90) {
  33.             xdiff = cinf->width * co - 0.5;
  34.             ydiff = newinf->height - 1;
  35.         }else {
  36.             xdiff = 0.0;
  37.             ydiff = cinf->width * si - 0.5;
  38.         }
  39.         cosine = cos(th);
  40.         sine = sin(th);
  41.     }
  42. }
  43.  
  44.  
  45. int sub_rotate(int op, int angle, commonInfo *cinf, commonInfo *newinf,
  46.             int idx[], unsigned char **working)
  47. {
  48.     int    x, y;
  49.     int    i, pidx, ptr;
  50.     int    pix[MAXPLANE];
  51.  
  52.     if (op == Horizontal) {
  53.         for (y = 0; y < newinf->height; y++) {
  54.             ptr = y * newinf->width;
  55.             for (x = newinf->width - 1; x >= 0; x--) {
  56.                 getPixelA(pix);
  57.                 for (i = 0; i <= ALPHA; i++) {
  58.                     if ((pidx = idx[i]) < 0) continue;
  59.                     working[pidx][ptr + x] = pix[i];
  60.                 }
  61.             }
  62.         }
  63.     }else if (op == Vertical) {
  64.         for (y = newinf->height - 1; y >= 0; y--) {
  65.             ptr = y * newinf->width;
  66.             for (x = 0; x < newinf->width; x++) {
  67.                 getPixelA(pix);
  68.                 for (i = 0; i <= ALPHA; i++) {
  69.                     if ((pidx = idx[i]) < 0) continue;
  70.                     working[pidx][ptr + x] = pix[i];
  71.                 }
  72.             }
  73.         }
  74.     }else if (angle == 90) {
  75.         for (x = 0; x < newinf->width; x++) {
  76.             for (y = newinf->height - 1; y >= 0; y--) {
  77.                 ptr = y * newinf->width;
  78.                 getPixelA(pix);
  79.                 for (i = 0; i <= ALPHA; i++) {
  80.                     if ((pidx = idx[i]) < 0) continue;
  81.                     working[pidx][ptr + x] = pix[i];
  82.                 }
  83.             }
  84.         }
  85.     }else if (angle == 270) {
  86.         for (x = newinf->width - 1; x >= 0; x--) {
  87.             for (y = 0; y < newinf->height; y++) {
  88.                 ptr = y * newinf->width;
  89.                 getPixelA(pix);
  90.                 for (i = 0; i <= ALPHA; i++) {
  91.                     if ((pidx = idx[i]) < 0) continue;
  92.                     working[pidx][ptr + x] = pix[i];
  93.                 }
  94.             }
  95.         }
  96.     }else if (angle == 180) {
  97.         for (y = newinf->height - 1; y >= 0; y--) {
  98.             ptr = y * newinf->width;
  99.             for (x = newinf->width - 1; x >= 0; x--) {
  100.                 getPixelA(pix);
  101.                 for (i = 0; i <= ALPHA; i++) {
  102.                     if ((pidx = idx[i]) < 0) continue;
  103.                     working[pidx][ptr + x] = pix[i];
  104.                 }
  105.             }
  106.         }
  107.     }else /* any */ {
  108.         unsigned char *tmp[MAXPLANE];
  109.         int dx, dy, ox, oy, pty;
  110.         int pl = cinf->numcolors;
  111.         if (newinf->alpha) pl++;
  112.         if (allocImage(tmp, cinf->width, cinf->height, 8, pl))
  113.             return Err_MEMORY;
  114.         for (y = 0; y < cinf->height; y++) {
  115.             ptr = y * cinf->width;
  116.             for (x = 0; x < cinf->width; x++) {
  117.                 getPixelA(pix);
  118.                 for (i = 0; i <= ALPHA; i++) {
  119.                     if ((pidx = idx[i]) < 0) continue;
  120.                     tmp[pidx][ptr + x] = pix[i];
  121.                 }
  122.             }
  123.         }
  124.         for (y = 0; y < newinf->height; y++) {
  125.             dy = y - ydiff;
  126.             ptr = y * newinf->width;
  127.             for (x = 0; x < newinf->width; x++) {
  128.                 dx = x - xdiff;
  129.                 ox = dx * cosine - dy * sine + 0.5;
  130.                 oy = dx * sine + dy * cosine + 0.5;
  131.                 if (ox < 0 || ox >= cinf->width
  132.                  || oy < 0 || oy >= cinf->height) {
  133.                     for (i = 0; i < pl; i++)
  134.                     working[i][ptr + x] = 0;
  135.                 }else {
  136.                     pty = oy * cinf->width + ox;
  137.                     for (i = 0; i < pl; i++)
  138.                     working[i][ptr + x] = tmp[i][pty];
  139.                 }
  140.             }
  141.         }
  142.         free((void *)tmp[0]);
  143.     }
  144.     return 0;
  145. }
  146.