From f458a6c23e5146bb46edc2cc5f0cdf81e312fc7d Mon Sep 17 00:00:00 2001
From: Per Hedbor <ph@opera.com>
Date: Fri, 9 Apr 1999 16:16:11 +0200
Subject: [PATCH] optimizations <tm> for rigid and ordered dither

Rev: src/modules/Image/colortable.c:1.55
Rev: src/modules/Image/colortable.h:1.14
Rev: src/modules/Image/colortable_lookup.h:1.7
---
 src/modules/Image/colortable.c        | 78 +++++++++++++++++----------
 src/modules/Image/colortable.h        |  4 +-
 src/modules/Image/colortable_lookup.h | 58 ++++++++++----------
 3 files changed, 83 insertions(+), 57 deletions(-)

diff --git a/src/modules/Image/colortable.c b/src/modules/Image/colortable.c
index a10a53605b..78b5cc0d46 100644
--- a/src/modules/Image/colortable.c
+++ b/src/modules/Image/colortable.c
@@ -1,12 +1,12 @@
 #include "global.h"
 #include <config.h>
 
-/* $Id: colortable.c,v 1.54 1999/04/08 22:20:49 hubbe Exp $ */
+/* $Id: colortable.c,v 1.55 1999/04/09 14:16:08 per Exp $ */
 
 /*
 **! module Image
 **! note
-**!	$Id: colortable.c,v 1.54 1999/04/08 22:20:49 hubbe Exp $
+**!	$Id: colortable.c,v 1.55 1999/04/09 14:16:08 per Exp $
 **! class colortable
 **!
 **!	This object keeps colortable information,
@@ -21,7 +21,7 @@
 #undef COLORTABLE_DEBUG
 #undef COLORTABLE_REDUCE_DEBUG
 
-RCSID("$Id: colortable.c,v 1.54 1999/04/08 22:20:49 hubbe Exp $");
+RCSID("$Id: colortable.c,v 1.55 1999/04/09 14:16:08 per Exp $");
 
 #include <math.h> /* fabs() */
 
@@ -185,25 +185,20 @@ static void exit_colortable_struct(struct object *obj)
 
 /***************** internal stuff ******************************/
 
-#if 0
+#if 1
 #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;
+  static long long first;
+  static long long last;
+  long long cur;
+  if(!first)
+    last = first = gethrtime();
+  cur=gethrtime();
+  fprintf(stderr, "CHRONO %-20s ... %4.4f (%4.4f)\n",x,(cur-last)/1000000000.0,(cur-first)/1000000000.0);
+  last=cur;
 }
 #else
 #define CHRONO(X)
@@ -1809,13 +1804,30 @@ static rgbl_group dither_ordered_encode(struct nct_dither *dith,
    return rgb;
 }
 
-
-static void dither_dummy_got(struct nct_dither *dith,
-			     int rowpos,
-			     rgb_group s,
-			     rgb_group d)
+static rgbl_group dither_ordered_encode_same(struct nct_dither *dith,
+                                             int rowpos,
+                                             rgb_group s)
 {
-   /* do-nothing */
+   rgbl_group rgb={1,2,3};
+   int i;
+
+   i=(int)(dith->u.ordered.rdiff
+           [((rowpos+dith->u.ordered.rx)&dith->u.ordered.xa)+
+           ((dith->u.ordered.row+dith->u.ordered.ry)&dith->u.ordered.ya)*dith->u.ordered.xs]); 
+   if (i<0)
+   {
+     rgb.r=s.r+i; rgb.r=rgb.r<0?0:rgb.r;
+     rgb.g=s.g+i; rgb.g=rgb.g<0?0:rgb.g;
+     rgb.b=s.b+i; rgb.b=rgb.b<0?0:rgb.b;
+   }
+   else
+   {
+     rgb.r=s.r+i; rgb.r=rgb.r>255?255:rgb.r;
+     rgb.g=s.g+i; rgb.g=rgb.g>255?255:rgb.g;
+     rgb.b=s.b+i; rgb.b=rgb.b>255?255:rgb.b;
+   }
+   return rgb;
+   return rgb;
 }
 
 
@@ -1860,7 +1872,6 @@ int image_colortable_initiate_dither(struct neo_colortable *nct,
 	 dith->u.randomcube=THIS->du.randomcube;
 
 	 dith->encode=dither_randomcube_encode;
-	 dith->got=dither_dummy_got;
 
 	 return 1;
 
@@ -1868,7 +1879,6 @@ int image_colortable_initiate_dither(struct neo_colortable *nct,
 	 dith->u.randomcube=THIS->du.randomcube;
 
 	 dith->encode=dither_randomgrey_encode;
-	 dith->got=dither_dummy_got;
 
 	 return 1;
 
@@ -1901,8 +1911,14 @@ int image_colortable_initiate_dither(struct neo_colortable *nct,
 
 	 dith->u.ordered.row=0;
 
-	 dith->encode=dither_ordered_encode;
-	 dith->got=dither_dummy_got;
+         if (nct->du.ordered.same)
+         {
+           dith->encode=dither_ordered_encode_same;
+           dith->u.ordered.xa=dith->u.ordered.xs-1;
+           dith->u.ordered.ya=dith->u.ordered.ys-1;
+         }
+         else
+           dith->encode=dither_ordered_encode;
 	 dith->newline=dither_ordered_newline;
 
 	 return 1;
@@ -4153,6 +4169,14 @@ void image_colortable_ordered(INT32 args)
    THIS->du.ordered.gdiff=ordered_make_diff(errors,xsize*ysize,g);
    THIS->du.ordered.bdiff=ordered_make_diff(errors,xsize*ysize,b);
 
+   if (r==g && g==b && 
+       THIS->du.ordered.rx==THIS->du.ordered.gx && THIS->du.ordered.gx==THIS->du.ordered.bx) 
+   {
+     THIS->du.ordered.same=1;
+   }
+   else
+     THIS->du.ordered.same=0;
+
    free(errors);
 
    if (!THIS->du.ordered.rdiff||
diff --git a/src/modules/Image/colortable.h b/src/modules/Image/colortable.h
index 98e27145f6..652b4fc5dc 100644
--- a/src/modules/Image/colortable.h
+++ b/src/modules/Image/colortable.h
@@ -1,7 +1,7 @@
 /*
 **! module Image
 **! note
-**!	$Id: colortable.h,v 1.13 1999/04/07 22:22:14 mirar Exp $
+**!	$Id: colortable.h,v 1.14 1999/04/09 14:16:09 per Exp $
 */
 
 #ifdef PIKE_IMAGE_COLORTABLE_H
@@ -127,9 +127,11 @@ struct neo_colortable
       struct nctd_ordered
       {
 	 int xs,ys;
+	 int xa,ya;
 	 int *rdiff,*gdiff,*bdiff;
 	 int rx,ry,gx,gy,bx,by;
 	 int row;
+         int same; /* true if rdiff, gdiff & bdiff is the same */
       } ordered;
    } du;
 };
diff --git a/src/modules/Image/colortable_lookup.h b/src/modules/Image/colortable_lookup.h
index 4d716772d8..fc481e1d39 100644
--- a/src/modules/Image/colortable_lookup.h
+++ b/src/modules/Image/colortable_lookup.h
@@ -1,10 +1,10 @@
-/* $Id: colortable_lookup.h,v 1.6 1999/04/07 22:22:15 mirar Exp $ */
+/* $Id: colortable_lookup.h,v 1.7 1999/04/09 14:16:11 per Exp $ */
 /* included w/ defines in colortable.c */
 
 /*
 **! module Image
 **! note
-**!	$Id: colortable_lookup.h,v 1.6 1999/04/07 22:22:15 mirar Exp $
+**!	$Id: colortable_lookup.h,v 1.7 1999/04/09 14:16:11 per Exp $
 **! class colortable
 */
 
@@ -129,9 +129,10 @@ CHRONO("begin flat/cubicles");
       }
 
 done_pixel:
-      if (dither_got)
+      if (dither_encode)
       {
-	 (dither_got)(dith,rowpos,*s,NCTLU_DITHER_GOT);
+         if (dither_got)
+	   dither_got(dith,rowpos,*s,NCTLU_DITHER_GOT);
 	 s+=cd; d+=cd; rowpos+=cd;
 	 if (++rowcount==rowlen)
 	 {
@@ -235,9 +236,10 @@ static void NCTLU_FLAT_FULL_NAME(rgb_group *s,
 	 else fe++;
 
 done_pixel:
-      if (dither_got)
+      if (dither_encode)
       {
-	 dither_got(dith,rowpos,*s,NCTLU_DITHER_GOT);
+         if (dither_got)
+	   dither_got(dith,rowpos,*s,NCTLU_DITHER_GOT);
 	 s+=cd; d+=cd; rowpos+=cd;
 	 if (++rowcount==rowlen)
 	 {
@@ -292,42 +294,39 @@ static void NCTLU_FLAT_RIGID_NAME(rgb_group *s,
 
    while (n--)
    {
-      int rgbr,rgbg,rgbb;
       int mindist;
       int m;
       struct nct_flat_entry *fe;
       struct lookupcache lc;
+      rgbl_group val;
 	 
       if (dither_encode)
       {
-	 rgbl_group val;
 	 val=dither_encode(dith,rowpos,*s);
-	 rgbr=val.r;
-	 rgbg=val.g;
-	 rgbb=val.b;
       }
       else
       {
-	 rgbr=s->r;
-	 rgbg=s->g;
-	 rgbb=s->b;
+	 val.r=s->r;
+	 val.g=s->g;
+	 val.b=s->b;
       }
 
-      i=index[((rgbr*r)>>8)+
-	     r*(((rgbg*g)>>8)+
-		((rgbb*b)>>8)*g)];
+      i=index[((val.r*r)>>8)+
+	     r*(((val.g*g)>>8)+
+		((val.b*b)>>8)*g)];
       NCTLU_RIGID_WRITE;
 
-      if (dither_got)
+      if (dither_encode)
       {
-	 dither_got(dith,rowpos,*s,NCTLU_DITHER_RIGID_GOT);
-	 s+=cd; d+=cd; rowpos+=cd;
-	 if (++rowcount==rowlen)
-	 {
-	    rowcount=0;
-	    if (dither_newline) 
-	       (dither_newline)NCTLU_LINE_ARGS;
-	 }
+        if (dither_got)
+          dither_got(dith,rowpos,*s,NCTLU_DITHER_RIGID_GOT);
+        s+=cd; d+=cd; rowpos+=cd;
+        if (++rowcount==rowlen)
+        {
+          rowcount=0;
+          if (dither_newline) 
+            (dither_newline)NCTLU_LINE_ARGS;
+        }
       }
       else
       {
@@ -506,9 +505,10 @@ static void NCTLU_CUBE_NAME(rgb_group *s,
 	    }
 	 }
 done_pixel:
-	 if (dither_got)
-	 {
-	    dither_got(dith,rowpos,*s,NCTLU_DITHER_GOT);
+         if (dither_encode)
+         {
+            if (dither_got)
+               dither_got(dith,rowpos,*s,NCTLU_DITHER_GOT);
 	    s+=cd; d+=cd; rowpos+=cd;
 	    if (++rowcount==rowlen)
 	    {
-- 
GitLab