home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 564a.lha / wasp_v1.21 / Src.LZH / Src / operations.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  6KB  |  318 lines

  1. /* wasp - copyright Steven Reiz 1990, 1991
  2.  * see wasp.c for further info
  3.  * operations.c, 4/12/90 - 23/6/91
  4.  */
  5.  
  6. #include "wasp.h"
  7. #ifndef NOSH
  8. #include "operations.sh"
  9. #endif
  10.  
  11. #ifdef __STDC__
  12. scalex(int t, int n)
  13. #else
  14. scalex(t, n)
  15. int t, n;
  16. #endif
  17. {
  18.     NON_REG u_long newxsz;
  19.     REG u_short **newrgb;
  20.     REG long x, y, xtot, newx;
  21.  
  22.     if (t==n || t<=0 || n<=0)
  23.         return;
  24.     init_counter(0, (int)xsz, 20, "x scaling %d/%d", t, n);
  25.     newxsz=xsz*t/n;
  26.     newrgb=Malloc(ysz*sizeof(u_short *));
  27.     for (y=0; y<ysz; ++y)
  28.         newrgb[y]=Malloc(newxsz*sizeof(u_short));
  29.     xtot=0; newx=0;
  30.     for (x=0; x<xsz; ++x) {
  31.         counter();
  32.         xtot+=t;
  33.         while (xtot>=n) {
  34.             xtot-=n;
  35.             y=ysz-1;
  36.             do {
  37.                 newrgb[y][newx]=rgb[y][x];
  38.             } while (--y>=0);
  39.             ++newx;
  40.         }
  41.     }
  42.     for (y=0; y<ysz; ++y)
  43.         free(rgb[y]);
  44.     free(rgb);
  45.     rgb=newrgb;
  46.     xsz=newxsz;
  47.     erase_counter("x scaling done; %ld x %ld", xsz, ysz);
  48. }
  49.  
  50.  
  51. #ifdef __STDC__
  52. scaley(int t, int n)
  53. #else
  54. scaley(t, n)
  55. int t, n;
  56. #endif
  57. {
  58.     NON_REG u_long newysz;
  59.     NON_REG u_short **newrgb;
  60.     NON_REG short y, ytot, newy;
  61.     REG u_short *p, *q;
  62.     REG short i;
  63.     
  64.     if (t==n || t<=0 || n<=0)
  65.         return;
  66.     init_counter(0, (int)ysz, 20, "y scaling %d/%d", t, n);
  67.     newysz=ysz*t/n;
  68.     newrgb=Malloc(newysz*sizeof(u_short *));
  69.     ytot=0; newy=0;
  70.     for (y=0; y<ysz; ++y) {
  71.         counter();
  72.         ytot+=t;
  73.         while (ytot>=n) {
  74.             ytot-=n;
  75.             newrgb[newy]=Malloc(xsz*sizeof(u_short));
  76.             p=newrgb[newy];
  77.             q=rgb[y];
  78.             i=xsz-1;
  79.             do {
  80.                 *p++ = *q++;
  81.             } while (--i>=0);
  82.             ++newy;
  83.         }
  84.         free(rgb[y]);
  85.     }
  86.     free(rgb);
  87.     rgb=newrgb;
  88.     ysz=newysz;
  89.     erase_counter("y scaling done; %ld x %ld", xsz, ysz);
  90. }
  91.  
  92.  
  93. struct fac_t {
  94.     int t, n;
  95. } facs[]={
  96.     4, 1,
  97.     3, 1,
  98.     2, 1,
  99.     1, 1,
  100.     1, 2,
  101.     1, 3,
  102.     2, 3,
  103.     1, 4,
  104.     3, 4,
  105.     1, 5,
  106.     4, 5,
  107.     1, 6,
  108.     0, 0
  109. };
  110.  
  111.  
  112. #ifdef __STDC__
  113. scalef(int yflag, float factor)
  114. #else
  115. scalef(yflag, factor)
  116. int yflag;
  117. float factor;
  118. #endif
  119. {
  120.     int t, n;
  121.     int i, besti;
  122.     float error, besterror, tf;
  123.  
  124.     besterror=1000.0;
  125.     for (i=0; facs[i].n; ++i) {
  126.         tf=(float)facs[i].t/(float)facs[i].n;
  127.         if (tf<=factor) {
  128.             error=factor-tf;
  129.             if (error<besterror) {
  130.                 besti=i;
  131.                 besterror=error;
  132.             }
  133.         }
  134.     }
  135.     t=facs[besti].t;
  136.     n=facs[besti].n;
  137.     if (t==4 && n==1)
  138.         t=(int)(factor+0.5);
  139.     else if (t==1 && n==6)
  140.         n=(int)(1/factor+0.5);
  141.     if (yflag)
  142.         scaley(t, n);
  143.     else
  144.         scalex(t, n);
  145. }
  146.  
  147.  
  148. #ifdef __STDC__
  149. do_clipping(void)
  150. #else
  151. do_clipping()
  152. #endif
  153. {
  154.     int minx, miny, maxx, maxy, newxsz, newysz;
  155.     u_short **newrgb;
  156.     short x, y;
  157.     u_short *p, *q;
  158.     char lin[40]; int wrong;
  159.  
  160.     printe("clipping; width: %ld, height: %ld\n", xsz, ysz);
  161.     printe("please enter the region you want to keep\n");
  162.     do {
  163.         wrong=0;
  164.         printe("minx? "); fflush(stderr); gets(lin); minx=atol(lin);
  165.         printe("maxx? "); fflush(stderr); gets(lin); maxx=atol(lin);
  166.         printe("miny? "); fflush(stderr); gets(lin); miny=atol(lin);
  167.         printe("maxy? "); fflush(stderr); gets(lin); maxy=atol(lin);
  168.         if (minx<0 || miny<0 || maxx>=xsz || maxy>=ysz || maxx<minx || maxy<miny) {
  169.             printe("%d-%d x %d-%d is an illegal region, try again\n", 
  170.              minx, maxx, miny, maxy);
  171.             wrong=1;
  172.         }
  173.     } while (wrong);
  174.     newxsz=maxx-minx+1;
  175.     newysz=maxy-miny+1;
  176.     for (y=0; y<miny; ++y)
  177.         free(rgb[y]);
  178.     for (y=maxy+1; y<ysz; ++y)
  179.         free(rgb[y]);
  180.     newrgb=Malloc(newysz*sizeof(u_short *));
  181.     init_counter(0, (int)newysz, 20, NULL);
  182.     for (y=0; y<newysz; ++y) {
  183.         counter();
  184.         newrgb[y]=Malloc(newxsz*sizeof(u_short));
  185.         p=newrgb[y];
  186.         q=rgb[y+miny]+minx;
  187.         x=newxsz-1;
  188.         do {
  189.             *p++ = *q++;
  190.         } while (--x>=0);        
  191.         free(rgb[y+miny]);
  192.     }
  193.     free(rgb);
  194.     rgb=newrgb;
  195.     xsz=newxsz;
  196.     ysz=newysz;
  197.     erase_counter("clipping done; %ld x %ld", xsz, ysz);
  198. }
  199.  
  200.  
  201. #ifdef __STDC__
  202. xaverage(void)
  203. #else
  204. xaverage()
  205. #endif
  206. {
  207.     NON_REG int y;
  208.     REG short x, *p, *q;
  209.     NON_REG u_short *oldrgby;
  210.     REG long c1, c2, mask1, mask2;
  211.     NON_REG long newxsz;
  212.  
  213.     init_counter(0, (int)ysz, 20, "xaverage");
  214.     newxsz=xsz/2;
  215.     mask1=0xeef;
  216.     mask2=0x110;
  217.     for (y=0; y<ysz; ++y) {
  218.         counter();
  219.         oldrgby=rgb[y];
  220.         p=(short *)oldrgby;
  221.         rgb[y]=Malloc(newxsz*sizeof(u_short));
  222.         q=(short *)rgb[y];
  223.         x=newxsz-1;
  224.         do {
  225.             c1= *p++;
  226.             c2= *p++;
  227.             *q++ =(((c1&mask1)+(c2&mask1))>>1)+(c1&c2&mask2);
  228.         } while (--x>=0);
  229.         free(oldrgby);
  230.     }
  231.     xsz=newxsz;
  232.     erase_counter("xaverage done; %ld x %ld", xsz, ysz);
  233. }
  234.  
  235.  
  236. #ifdef __STDC__
  237. xmirror(void)
  238. #else
  239. xmirror()
  240. #endif
  241. {
  242.     u_short *line;
  243.     short y, x;
  244.     u_short *p, *q;
  245.  
  246.     init_counter(0, (int)ysz, 20, "xmirror");
  247.     line=Malloc(xsz*sizeof(u_short));
  248.     for (y=0; y<ysz; ++y) {
  249.         counter();
  250.         x=xsz-1;
  251.         p=line;
  252.         q=rgb[y]+xsz;
  253.         do {
  254.             *p++ = *--q;
  255.         } while (--x>=0);
  256.         p=rgb[y];
  257.         rgb[y]=line;
  258.         line=p;
  259.     }
  260.     erase_counter("xmirror done");
  261.     free(line);
  262. }
  263.  
  264.  
  265. #ifdef __STDC__
  266. ymirror(void)
  267. #else
  268. ymirror()
  269. #endif
  270. {
  271.     short i, j;
  272.     u_short *p;
  273.  
  274.     i=0;
  275.     j=ysz-1;
  276.     while (i<j) {
  277.         p=rgb[i];
  278.         rgb[i]=rgb[j];
  279.         rgb[j]=p;
  280.         ++i;
  281.         --j;
  282.     }
  283.     printe("ymirror done\n");
  284. }
  285.  
  286.  
  287. #ifdef __STDC__
  288. transpose(void)
  289. #else
  290. transpose()
  291. #endif
  292. {
  293.     u_short **newrgb;
  294.     short x, y;
  295.     u_short *p;
  296.     long t;
  297.  
  298.     init_counter(0, (int)ysz, 10, "transpose");
  299.     newrgb=Malloc(xsz*sizeof(u_short *));
  300.     for (y=0; y<xsz; ++y)
  301.         newrgb[y]=Malloc(ysz*sizeof(u_short));
  302.     for (y=0; y<ysz; ++y) {
  303.         counter();
  304.         x=xsz-1;
  305.         p=rgb[y]+xsz;
  306.         do {
  307.             newrgb[x][y]= *--p;
  308.         } while (--x>=0);
  309.         free(rgb[y]);
  310.     }
  311.     free(rgb);
  312.     rgb=newrgb;
  313.     t=xsz;
  314.     xsz=ysz;
  315.     ysz=t;
  316.     erase_counter("transpose done; %ld x %ld", xsz, ysz);
  317. }
  318.