home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
580b.lha
/
Wasp_v1.23
/
src.LZH
/
src
/
operations.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-11-15
|
6KB
|
318 lines
/* wasp - copyright Steven Reiz 1990, 1991
* see wasp.c for further info
* operations.c, 4/12/90 - 23/6/91
*/
#include "wasp.h"
#ifndef NOSH
#include "operations.sh"
#endif
#ifdef __STDC__
scalex(int t, int n)
#else
scalex(t, n)
int t, n;
#endif
{
NON_REG u_long newxsz;
REG u_short **newrgb;
REG long x, y, xtot, newx;
if (t==n || t<=0 || n<=0)
return;
init_counter(0, (int)xsz, 20, "x scaling %d/%d", t, n);
newxsz=xsz*t/n;
newrgb=Malloc(ysz*sizeof(u_short *));
for (y=0; y<ysz; ++y)
newrgb[y]=Malloc(newxsz*sizeof(u_short));
xtot=0; newx=0;
for (x=0; x<xsz; ++x) {
counter();
xtot+=t;
while (xtot>=n) {
xtot-=n;
y=ysz-1;
do {
newrgb[y][newx]=rgb[y][x];
} while (--y>=0);
++newx;
}
}
for (y=0; y<ysz; ++y)
free(rgb[y]);
free(rgb);
rgb=newrgb;
xsz=newxsz;
erase_counter("x scaling done; %ld x %ld", xsz, ysz);
}
#ifdef __STDC__
scaley(int t, int n)
#else
scaley(t, n)
int t, n;
#endif
{
NON_REG u_long newysz;
NON_REG u_short **newrgb;
NON_REG short y, ytot, newy;
REG u_short *p, *q;
REG short i;
if (t==n || t<=0 || n<=0)
return;
init_counter(0, (int)ysz, 20, "y scaling %d/%d", t, n);
newysz=ysz*t/n;
newrgb=Malloc(newysz*sizeof(u_short *));
ytot=0; newy=0;
for (y=0; y<ysz; ++y) {
counter();
ytot+=t;
while (ytot>=n) {
ytot-=n;
newrgb[newy]=Malloc(xsz*sizeof(u_short));
p=newrgb[newy];
q=rgb[y];
i=xsz-1;
do {
*p++ = *q++;
} while (--i>=0);
++newy;
}
free(rgb[y]);
}
free(rgb);
rgb=newrgb;
ysz=newysz;
erase_counter("y scaling done; %ld x %ld", xsz, ysz);
}
struct fac_t {
int t, n;
} facs[]={
4, 1,
3, 1,
2, 1,
1, 1,
1, 2,
1, 3,
2, 3,
1, 4,
3, 4,
1, 5,
4, 5,
1, 6,
0, 0
};
#ifdef __STDC__
scalef(int yflag, float factor)
#else
scalef(yflag, factor)
int yflag;
float factor;
#endif
{
int t, n;
int i, besti;
float error, besterror, tf;
besterror=1000.0;
for (i=0; facs[i].n; ++i) {
tf=(float)facs[i].t/(float)facs[i].n;
if (tf<=factor) {
error=factor-tf;
if (error<besterror) {
besti=i;
besterror=error;
}
}
}
t=facs[besti].t;
n=facs[besti].n;
if (t==4 && n==1)
t=(int)(factor+0.5);
else if (t==1 && n==6)
n=(int)(1/factor+0.5);
if (yflag)
scaley(t, n);
else
scalex(t, n);
}
#ifdef __STDC__
do_clipping(void)
#else
do_clipping()
#endif
{
int minx, miny, maxx, maxy, newxsz, newysz;
u_short **newrgb;
short x, y;
u_short *p, *q;
char lin[40]; int wrong;
printe("clipping; width: %ld, height: %ld\n", xsz, ysz);
printe("please enter the region you want to keep\n");
do {
wrong=0;
printe("minx? "); fflush(stderr); gets(lin); minx=atol(lin);
printe("maxx? "); fflush(stderr); gets(lin); maxx=atol(lin);
printe("miny? "); fflush(stderr); gets(lin); miny=atol(lin);
printe("maxy? "); fflush(stderr); gets(lin); maxy=atol(lin);
if (minx<0 || miny<0 || maxx>=xsz || maxy>=ysz || maxx<minx || maxy<miny) {
printe("%d-%d x %d-%d is an illegal region, try again\n",
minx, maxx, miny, maxy);
wrong=1;
}
} while (wrong);
newxsz=maxx-minx+1;
newysz=maxy-miny+1;
for (y=0; y<miny; ++y)
free(rgb[y]);
for (y=maxy+1; y<ysz; ++y)
free(rgb[y]);
newrgb=Malloc(newysz*sizeof(u_short *));
init_counter(0, (int)newysz, 20, NULL);
for (y=0; y<newysz; ++y) {
counter();
newrgb[y]=Malloc(newxsz*sizeof(u_short));
p=newrgb[y];
q=rgb[y+miny]+minx;
x=newxsz-1;
do {
*p++ = *q++;
} while (--x>=0);
free(rgb[y+miny]);
}
free(rgb);
rgb=newrgb;
xsz=newxsz;
ysz=newysz;
erase_counter("clipping done; %ld x %ld", xsz, ysz);
}
#ifdef __STDC__
xaverage(void)
#else
xaverage()
#endif
{
NON_REG int y;
REG short x, *p, *q;
NON_REG u_short *oldrgby;
REG long c1, c2, mask1, mask2;
NON_REG long newxsz;
init_counter(0, (int)ysz, 20, "xaverage");
newxsz=xsz/2;
mask1=0xeef;
mask2=0x110;
for (y=0; y<ysz; ++y) {
counter();
oldrgby=rgb[y];
p=(short *)oldrgby;
rgb[y]=Malloc(newxsz*sizeof(u_short));
q=(short *)rgb[y];
x=newxsz-1;
do {
c1= *p++;
c2= *p++;
*q++ =(((c1&mask1)+(c2&mask1))>>1)+(c1&c2&mask2);
} while (--x>=0);
free(oldrgby);
}
xsz=newxsz;
erase_counter("xaverage done; %ld x %ld", xsz, ysz);
}
#ifdef __STDC__
xmirror(void)
#else
xmirror()
#endif
{
u_short *line;
short y, x;
u_short *p, *q;
init_counter(0, (int)ysz, 20, "xmirror");
line=Malloc(xsz*sizeof(u_short));
for (y=0; y<ysz; ++y) {
counter();
x=xsz-1;
p=line;
q=rgb[y]+xsz;
do {
*p++ = *--q;
} while (--x>=0);
p=rgb[y];
rgb[y]=line;
line=p;
}
erase_counter("xmirror done");
free(line);
}
#ifdef __STDC__
ymirror(void)
#else
ymirror()
#endif
{
short i, j;
u_short *p;
i=0;
j=ysz-1;
while (i<j) {
p=rgb[i];
rgb[i]=rgb[j];
rgb[j]=p;
++i;
--j;
}
printe("ymirror done\n");
}
#ifdef __STDC__
transpose(void)
#else
transpose()
#endif
{
u_short **newrgb;
short x, y;
u_short *p;
long t;
init_counter(0, (int)ysz, 10, "transpose");
newrgb=Malloc(xsz*sizeof(u_short *));
for (y=0; y<xsz; ++y)
newrgb[y]=Malloc(ysz*sizeof(u_short));
for (y=0; y<ysz; ++y) {
counter();
x=xsz-1;
p=rgb[y]+xsz;
do {
newrgb[x][y]= *--p;
} while (--x>=0);
free(rgb[y]);
}
free(rgb);
rgb=newrgb;
t=xsz;
xsz=ysz;
ysz=t;
erase_counter("transpose done; %ld x %ld", xsz, ysz);
}