From 286fef63f2a5d9a2f02613904f926b2015078702 Mon Sep 17 00:00:00 2001
From: Henrik Wallin <hedda@lysator.liu.se>
Date: Sun, 15 Feb 1998 15:54:14 +0100
Subject: [PATCH] orient.c Adderad

Rev: src/modules/Image/Makefile.in:1.11
Rev: src/modules/Image/image.c:1.83
Rev: src/modules/Image/image.h:1.18
Rev: src/modules/Image/orient.c:1.1
---
 .gitattributes                |   1 +
 src/modules/Image/Makefile.in |   3 +-
 src/modules/Image/image.c     |  39 +++--
 src/modules/Image/image.h     |   4 +-
 src/modules/Image/orient.c    | 311 ++++++++++++++++++++++++++++++++++
 5 files changed, 343 insertions(+), 15 deletions(-)
 create mode 100644 src/modules/Image/orient.c

diff --git a/.gitattributes b/.gitattributes
index db95198945..c762a3c3f6 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -92,6 +92,7 @@ testfont binary
 /src/modules/Image/matrix.c foreign_ident
 /src/modules/Image/mkwmml.pike foreign_ident
 /src/modules/Image/operator.c foreign_ident
+/src/modules/Image/orient.c foreign_ident
 /src/modules/Image/pattern.c foreign_ident
 /src/modules/Image/pnm.c foreign_ident
 /src/modules/Image/polyfill.c foreign_ident
diff --git a/src/modules/Image/Makefile.in b/src/modules/Image/Makefile.in
index 7aa03bc5a2..7de10136b5 100644
--- a/src/modules/Image/Makefile.in
+++ b/src/modules/Image/Makefile.in
@@ -1,7 +1,8 @@
 SRCDIR=@srcdir@
 VPATH=@srcdir@:@srcdir@/../..:../..
 OBJS = image.o font.o togif.o matrix.o pnm.o blit.o \
-	pattern.o dct.o operator.o x.o colortable.o polyfill.o 
+	pattern.o dct.o operator.o x.o colortable.o polyfill.o \
+	orient.o
 MODNAME=image
 MODULE_SUBDIRS=encodings
 MODULE_ARCHIVES=encodings/gif.a encodings/pnm.a encodings/png.a encodings/x.a
diff --git a/src/modules/Image/image.c b/src/modules/Image/image.c
index 8e2df0d109..43ddc74bca 100644
--- a/src/modules/Image/image.c
+++ b/src/modules/Image/image.c
@@ -1,9 +1,9 @@
-/* $Id: image.c,v 1.82 1998/02/13 18:57:58 marcus Exp $ */
+/* $Id: image.c,v 1.83 1998/02/15 14:53:50 hedda Exp $ */
 
 /*
 **! module Image
 **! note
-**!	$Id: image.c,v 1.82 1998/02/13 18:57:58 marcus Exp $
+**!	$Id: image.c,v 1.83 1998/02/15 14:53:50 hedda Exp $
 **! class image
 **!
 **!	The main object of the <ref>Image</ref> module, this object
@@ -82,7 +82,7 @@
 
 #include "stralloc.h"
 #include "global.h"
-RCSID("$Id: image.c,v 1.82 1998/02/13 18:57:58 marcus Exp $");
+RCSID("$Id: image.c,v 1.83 1998/02/15 14:53:50 hedda Exp $");
 #include "pike_macros.h"
 #include "object.h"
 #include "constants.h"
@@ -296,7 +296,7 @@ static INLINE rgb_group _pixel_apply_matrix(struct image *img,
 
    bx=width/2;
    by=height/2;
-   
+
    for (xp=x-bx,i=0; i<width; i++,xp++)
       for (yp=y-by,j=0; j<height; j++,yp++)
 	 if (xp>=0 && xp<img->xsize && yp>=0 && yp<img->ysize)
@@ -339,20 +339,21 @@ void img_apply_matrix(struct image *dest,
    rgb_group *d,*ip,*dp;
    rgbd_group *mp;
    int i,j,x,y,bx,by,ex,ey,yp;
+   int widthheight;
    double sumr,sumg,sumb;
    double qr,qg,qb;
    register double r=0,g=0,b=0;
 
 THREADS_ALLOW();
 
+   widthheight=width*height;
    sumr=sumg=sumb=0;
-   for (i=0; i<width; i++)
-      for (j=0; j<height; j++)
-      {
-	 sumr+=matrix[i+j*width].r;
-	 sumg+=matrix[i+j*width].g;
-	 sumb+=matrix[i+j*width].b;
-      }
+   for (i=0; i<widthheight;)
+     {
+       sumr+=matrix[i].r;
+       sumg+=matrix[i].g;
+       sumb+=matrix[i++].b;
+     }
 
    if (!sumr) sumr=1; sumr*=div; qr=1.0/sumr;
    if (!sumg) sumg=1; sumg*=div; qg=1.0/sumg;
@@ -378,9 +379,16 @@ CHRONO("apply_matrix, one");
       {
 	 r=g=b=0;
 	 mp=matrix;
-	 for (yp=y-by,j=0; j<height; j++,yp++)
+	 ip=img->img+(x-bx)+(y-by)*img->xsize;
+	 //for (yp=y-by,j=0; j<height; j++,yp++)
+#ifdef MATRIX_DEBUG
+j=-1;
+#endif
+	 for (yp=y-by; yp<height+y-by; yp++)
 	 {
-	    ip=img->img+(x-bx)+yp*img->xsize;
+#ifdef MATRIX_DEBUG
+j++;
+#endif
 	    for (i=0; i<width; i++)
 	    {
 	       r+=ip->r*mp->r;
@@ -396,6 +404,7 @@ CHRONO("apply_matrix, one");
 	       mp++;
 	       ip++;
 	    }
+	    ip+=img->xsize-width;
 	 }
 #ifdef MATRIX_DEBUG
 	 fprintf(stderr,"->%d,%d,%d\n",r/sumr,g/sumg,b/sumb);
@@ -3226,6 +3235,10 @@ void pike_module_init(void)
    add_function("write_lsb_grey",image_write_lsb_rgb,
 		"function(:object)",0);
 
+   add_function("orient",image_orient,
+                "function(:array(object))",0);
+
+
    set_init_callback(init_image_struct);
    set_exit_callback(exit_image_struct);
   
diff --git a/src/modules/Image/image.h b/src/modules/Image/image.h
index 8432ab1ea3..4ce137366b 100644
--- a/src/modules/Image/image.h
+++ b/src/modules/Image/image.h
@@ -1,7 +1,7 @@
 /*
 **! module Image
 **! note
-**!	$Id: image.h,v 1.17 1998/01/08 20:51:38 mirar Exp $
+**!	$Id: image.h,v 1.18 1998/02/15 14:53:56 hedda Exp $
 */
 
 #ifdef PIKE_IMAGE_IMAGE_H
@@ -145,4 +145,6 @@ void image_tobitmap(INT32 args);
 
 void image_polyfill(INT32 args);
 
+/* orient.c */
 
+void image_orient(INT32 args);
diff --git a/src/modules/Image/orient.c b/src/modules/Image/orient.c
new file mode 100644
index 0000000000..dcfc43660c
--- /dev/null
+++ b/src/modules/Image/orient.c
@@ -0,0 +1,311 @@
+/* $Id: orient.c,v 1.1 1998/02/15 14:54:14 hedda Exp $ */
+
+/*
+**! module Image
+**! note
+**!	$Id: orient.c,v 1.1 1998/02/15 14:54:14 hedda Exp $
+**! class image
+*/
+
+#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"
+#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;
+static const double PI=3.14159265358979323846;
+
+/*
+**! method object orient( foo bar args?)
+**!	Draws four images describing the orientation.
+**!	
+**!	
+**!	
+**!
+**!	
+**!	
+**!	
+**!
+**!	
+**!	
+**!
+**!	
+**!	
+**!
+**!
+**!
+**!
+**! returns an array of four new image objects
+**! arg int newx
+**! arg int newy
+**!	new image size in pixels
+**!
+*/
+static INLINE int sq(int a) { return a*a; }
+
+void image_orient(INT32 args)
+{
+  int x,y;
+  int h,j;
+  struct object *o1;
+  struct object *o2;
+  struct object *o3;
+  struct object *o4;
+  struct object *o5;
+  struct image *o1img;
+  struct image *o2img;
+  struct image *o3img;
+  struct image *o4img;
+  struct image *o5img;
+  
+  if (!THIS->img) { error("no image\n");  return; }
+  
+  push_int(THIS->xsize);
+  push_int(THIS->ysize);
+  o1=clone_object(image_program,2);
+  o1img=(struct image*)get_storage(o1,image_program);
+
+  push_int(THIS->xsize);
+  push_int(THIS->ysize);
+  o2=clone_object(image_program,2);
+  o2img=(struct image*)get_storage(o2,image_program);
+
+  push_int(THIS->xsize);
+  push_int(THIS->ysize);
+  o3=clone_object(image_program,2);
+  o3img=(struct image*)get_storage(o3,image_program);
+
+  push_int(THIS->xsize);
+  push_int(THIS->ysize);
+  o4=clone_object(image_program,2);
+  o4img=(struct image*)get_storage(o4,image_program);
+
+  push_int(THIS->xsize);
+  push_int(THIS->ysize);
+  o5=clone_object(image_program,2);
+  o5img=(struct image*)get_storage(o5,image_program);
+
+#define A o1img
+#define B THIS
+
+  //Create image 1
+  for(x=1; x<A->xsize-1; x++)
+    for(y=1; y<A->ysize-1; y++)
+      {
+#define CO r
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+y*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+y*B->xsize].CO ))
+				      )/2.0);
+	
+#undef CO
+#define CO g
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+y*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+y*B->xsize].CO ))
+				      )/2.0);
+	
+#undef CO
+#define CO b
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+y*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+y*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+	
+      }
+
+#undef A
+#define A o2img
+
+  //Create image 2
+  for(x=1; x<A->xsize-1; x++)
+    for(y=1; y<A->ysize-1; y++)
+      {
+#define CO r
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y+1)*B->xsize].CO ))
+				      )/2.0);
+	
+#undef CO
+#define CO g
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y+1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+#define CO b
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y+1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+	
+      }
+
+
+#undef A
+#define A o3img
+
+  //Create image 3
+  for(x=1; x<A->xsize-1; x++)
+    for(y=1; y<A->ysize-1; y++)
+      {
+#define CO r
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[x+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[x+(y+1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+#define CO g
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[x+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[x+(y+1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+#define CO b
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[x+(y-1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[x+(y+1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+	
+      }
+
+
+#undef A
+#define A o4img
+
+  //Create image 4
+  for(x=1; x<A->xsize-1; x++)
+    for(y=1; y<A->ysize-1; y++)
+      {
+#define CO r
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y+1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y-1)*B->xsize].CO ))
+				      )/2.0);
+	
+#undef CO
+#define CO g
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y+1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y-1)*B->xsize].CO ))
+				      )/2.0);
+#undef CO
+#define CO b
+	A->img[x+y*B->xsize].CO=sqrt((sq((B->img[(x-1)+(y+1)*B->xsize].CO-B->
+					  img[x+y*B->xsize].CO ))+
+				      sq((B->img[x+y*B->xsize].CO-
+					  B->img[(x+1)+(y-1)*B->xsize].CO ))
+				      )/2.0);
+	
+#undef CO
+	
+      }
+
+#undef A
+#define A o5img
+
+  //Create image 5, the hsv-thing...
+  for(x=1; x<A->xsize-1; x++)
+    for(y=1; y<A->ysize-1; y++)
+      {
+	//Första färg, sista mörkhet
+	j=o1img->img[x+y*B->xsize].r+
+	  o1img->img[x+y*B->xsize].g+
+	  o1img->img[x+y*B->xsize].b-
+	  o3img->img[x+y*B->xsize].r-
+	  o3img->img[x+y*B->xsize].g-
+	  o3img->img[x+y*B->xsize].b;
+	
+	h=o2img->img[x+y*B->xsize].r+
+	  o2img->img[x+y*B->xsize].g+
+	  o2img->img[x+y*B->xsize].b-
+	  o4img->img[x+y*B->xsize].r-
+	  o4img->img[x+y*B->xsize].g-
+	  o4img->img[x+y*B->xsize].b;
+	
+	if (h>0)
+	  if (j>0)
+	    A->img[x+y*B->xsize].r=(int)(0.5+(255/(2*PI))*
+					 atan((float)h/(float)j));
+	  else
+	    if (j<0)
+	      A->img[x+y*B->xsize].r=(int)(0.5+(255/(2*PI))*
+					   (PI+atan((float)h/(float)j)));
+	    else
+	      A->img[x+y*B->xsize].r=255/4;
+	else
+	  if (h<0)
+	    if (j>0)
+	      A->img[x+y*B->xsize].r=(int)(0.5+(255/(2*PI))*
+					   (2*PI+atan((float)h/(float)j)));
+	    else
+	      if (j<0)
+		A->img[x+y*B->xsize].r=(int)(0.5+(255/(2*PI))*
+					     (PI+atan((float)h/(float)j)));
+	      else
+		A->img[x+y*B->xsize].r=(3*255)/4;
+	  else
+	    if (j<0)
+	      A->img[x+y*B->xsize].r=255/2;
+	    else
+	      A->img[x+y*B->xsize].r=0;
+	
+	
+	A->img[x+y*B->xsize].g=255;
+
+	A->img[x+y*B->xsize].b=MINIMUM(255, sqrt((sq(j)+sq(h))));
+
+      }
+
+
+
+
+  //Och så fylla ut de andra bilderna med lite junk.
+
+  pop_n_elems(args);
+  push_object(o1);
+  push_object(o2);
+  push_object(o3);
+  push_object(o4);
+  push_object(o5);
+  
+  f_aggregate(5);
+}
-- 
GitLab