448c201999-04-13Mirar (Pontus Hagland) /* $Id: orient.c,v 1.11 1999/04/13 12:32:29 mirar Exp $ */
286fef1998-02-15Henrik Wallin  /* **! module Image **! note
448c201999-04-13Mirar (Pontus Hagland) **! $Id: orient.c,v 1.11 1999/04/13 12:32:29 mirar Exp $ **! class Image
286fef1998-02-15Henrik Wallin */ #include "global.h" #include <math.h> #include <ctype.h> #include "stralloc.h" #include "global.h" #include "pike_macros.h" #include "object.h" #include "constants.h" #include "interpret.h" #include "svalue.h"
3cf2691998-03-11Mirar (Pontus Hagland) #include "threads.h"
286fef1998-02-15Henrik Wallin #include "array.h" #include "error.h" #include "image.h" #include <builtin_functions.h> extern struct program *image_program; #ifdef THIS #undef THIS /* Needed for NT */ #endif #define THIS ((struct image *)(fp->current_storage)) #define THISOBJ (fp->current_object) #define testrange(x) MAXIMUM(MINIMUM((x),255),0) static const double c0=0.70710678118654752440;
d0224e1998-02-15Mirar (Pontus Hagland) static const double my_PI=3.14159265358979323846;
286fef1998-02-15Henrik Wallin 
3b8dcd1998-03-19Fredrik Hübinette (Hubbe) #if 0
3cf2691998-03-11Mirar (Pontus Hagland) #include <sys/resource.h> #define CHRONO(X) chrono(X); static void chrono(char *x) { struct rusage r; static struct rusage rold; getrusage(RUSAGE_SELF,&r); fprintf(stderr,"%s: %ld.%06ld - %ld.%06ld\n",x, (long)r.ru_utime.tv_sec,(long)r.ru_utime.tv_usec, (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?-1:0) +r.ru_utime.tv_sec-rold.ru_utime.tv_sec), (long)(((r.ru_utime.tv_usec-rold.ru_utime.tv_usec<0)?1000000:0) + r.ru_utime.tv_usec-rold.ru_utime.tv_usec) ); rold=r; } #else #define CHRONO(X) #endif
286fef1998-02-15Henrik Wallin /*
e3e3821998-03-23Henrik Wallin **! method object orient(void|array(object))
3cf2691998-03-11Mirar (Pontus Hagland) **! method array(object) orient4() **! Draws images describing the orientation **! of the current image.
286fef1998-02-15Henrik Wallin **!
e3e3821998-03-23Henrik Wallin **! <tt>orient</tt> gives an HSV image
3cf2691998-03-11Mirar (Pontus Hagland) **! (run a <ref>hsv_to_rgb</ref> pass on it **! to get a viewable image). **! corresponding to the angle of the **! orientation: **! <pre> | / - \
e3e3821998-03-23Henrik Wallin **! hue= 0 64 128 192 (=red in an hsv image)
3cf2691998-03-11Mirar (Pontus Hagland) **! purple cyan green red **! </pre> **! Red, green and blue channels are added **! and not compared separately.
e3e3821998-03-23Henrik Wallin **! **! If you first use orient4 you can give its **! output as input to this function.
286fef1998-02-15Henrik Wallin **!
3cf2691998-03-11Mirar (Pontus Hagland) **! The <tt>orient4</tt> function gives back **! 4 image objects, corresponding to the **! amount of different directions, see above.
286fef1998-02-15Henrik Wallin **!
e3e3821998-03-23Henrik Wallin **! returns an image or an array of the four new image objects
286fef1998-02-15Henrik Wallin **!
3cf2691998-03-11Mirar (Pontus Hagland) **! note **! experimental status; may not be exact the same **! output in later versions
286fef1998-02-15Henrik Wallin */ static INLINE int sq(int a) { return a*a; }
3cf2691998-03-11Mirar (Pontus Hagland) static INLINE int abs(int a) { return (a<0)?-a:a; } static void _image_orient(struct image *source, struct object *o[4], struct image *img[4]) { int i; struct { int x,y; } or[4]={ {1,0}, {1,1}, {0,1}, {-1,1} }; int x,y; for (i=0; i<5; i++) { push_int(source->xsize); push_int(source->ysize); o[i]=clone_object(image_program,2); img[i]=(struct image*)get_storage(o[i],image_program); push_object(o[i]); } THREADS_ALLOW(); CHRONO("start"); for (i=0; i<4; i++) /* four directions */ { rgb_group *d=img[i]->img; rgb_group *s=source->img; int xz=source->xsize; int yz=source->ysize; int xd=or[i].x; int yd=or[i].y; for(x=1; x<xz-1; x++) for(y=1; y<yz-1; y++) { #define FOOBAR(CO) \ d[x+y*xz].CO \ = \ (COLORTYPE) \ abs( s[(x+xd)+(y+yd)*xz].CO - s[(x-xd)+(y-yd)*xz].CO ) /* sqrt( ( sq( s[(x-xd)+(y-yd)*xz].CO - s[x+y*xz].CO ) + \ sq( s[(x+xd)+(y+yd)*xz].CO - s[x+y*xz].CO ) ) / 2.0 ) */ FOOBAR(r); FOOBAR(g); FOOBAR(b); #undef FOOBAR } } CHRONO("end"); THREADS_DISALLOW(); }
286fef1998-02-15Henrik Wallin  void image_orient(INT32 args) {
3cf2691998-03-11Mirar (Pontus Hagland)  struct object *o[5];
e3e3821998-03-23Henrik Wallin  struct image *img[5],*this,*img1;
3cf2691998-03-11Mirar (Pontus Hagland)  int n; rgb_group *d,*s1,*s2,*s3,*s0; float mag;
e3e3821998-03-23Henrik Wallin  int i, w, h;
286fef1998-02-15Henrik Wallin  if (!THIS->img) { error("no image\n"); return; }
3cf2691998-03-11Mirar (Pontus Hagland)  this=THIS;
e3e3821998-03-23Henrik Wallin  if (args) { if (sp[-args].type==T_INT) mag=sp[-args].u.integer; else if (sp[-args].type==T_FLOAT) mag=sp[-args].u.float_number;
1783ea1998-04-13Henrik Grubbström (Grubba)  else {
e3e3821998-03-23Henrik Wallin  error("Illegal argument 1 to image->orient\n");
1783ea1998-04-13Henrik Grubbström (Grubba)  /* Not reached, but keeps the compiler happy. */ mag = 0.0; }
e3e3821998-03-23Henrik Wallin  } else mag=1.0; if (args==1) pop_n_elems(args);
3cf2691998-03-11Mirar (Pontus Hagland) 
e3e3821998-03-23Henrik Wallin  if (args>1) { if (sp[1-args].type!=T_ARRAY) error("Illegal argument 2 to image->orient\n"); if (sp[1-args].u.array->size!=4) error("The array given as argument 2 to image->orient do not have size 4\n"); for(i=0; i<4; i++) if ((sp[1-args].u.array->item[i].type!=T_OBJECT) || (!(sp[1-args].u.array->item[i].u.object)) || (sp[1-args].u.array->item[i].u.object->prog!=image_program)) error("The array given as argument 2 to image->orient do not contain images\n"); img1=(struct image*)sp[1-args].u.array->item[0].u.object->storage; w=this->xsize; h=this->ysize; for(i=0; i<4; i++) { img1=(struct image*)sp[1-args].u.array->item[i].u.object->storage; if ((img1->xsize!=w)|| (img1->ysize!=h)) error("The images in the array given as argument 2 to image->orient have different sizes\n"); } for(i=0; i<4; i++) img[i]=(struct image*)sp[1-args].u.array->item[i].u.object->storage; pop_n_elems(args); push_int(this->xsize); push_int(this->ysize); o[4]=clone_object(image_program,2); img[4]=(struct image*)get_storage(o[4],image_program); push_object(o[4]); w=1; } else { _image_orient(this,o,img); w=0; }
3cf2691998-03-11Mirar (Pontus Hagland)  s0=img[0]->img; s1=img[1]->img; s2=img[2]->img; s3=img[3]->img; d=img[4]->img; THREADS_ALLOW(); CHRONO("begin hsv..."); n=this->xsize*this->ysize; while (n--) { /* Första färg, sista mörkhet */ float j=(s0->r+s0->g+s0->b-s2->r-s2->g-s2->b)/3.0; /* riktning - - riktning | */ float h=(s1->r+s1->g+s1->b-s3->r-s3->g-s3->b)/3.0; /* riktning \ - riktning / */ int z,w; if (abs(h)>abs(j)) if (h) z=-(int)(32*(j/h)+(h>0)*128+64),w=abs(h); else z=0,w=0; else z=-(int)(-32*(h/j)+(j>0)*128+128),w=abs(j); d->r=(COLORTYPE)z; d->g=255; d->b=(COLORTYPE)MINIMUM(w*mag,255); d++; s0++; s1++; s2++; s3++; } CHRONO("end hsv..."); THREADS_DISALLOW();
e3e3821998-03-23Henrik Wallin  if (!w) {
a2acd71998-04-20Mirar (Pontus Hagland)  o[4]->refs++;
e3e3821998-03-23Henrik Wallin  pop_n_elems(5); push_object(o[4]); }
3cf2691998-03-11Mirar (Pontus Hagland) }
286fef1998-02-15Henrik Wallin 
3cf2691998-03-11Mirar (Pontus Hagland)  void image_orient4(INT32 args) { struct object *o[5]; struct image *img[5];
286fef1998-02-15Henrik Wallin 
3cf2691998-03-11Mirar (Pontus Hagland)  if (!THIS->img) { error("no image\n"); return; } pop_n_elems(args); _image_orient(THIS,o,img); pop_n_elems(1); f_aggregate(4);
286fef1998-02-15Henrik Wallin }
3cf2691998-03-11Mirar (Pontus Hagland)