[BACK]Return to smoothing.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / plot

Diff for /OpenXM_contrib2/asir2000/plot/smoothing.c between version 1.1 and 1.5

version 1.1, 2000/10/15 06:56:53 version 1.5, 2001/04/07 10:53:15
Line 1 
Line 1 
 /* $OpenXM$ */  /* $OpenXM: OpenXM_contrib2/asir2000/plot/smoothing.c,v 1.4 2000/11/08 05:49:35 takayama Exp $ */
 #include "ca.h"  #include "ca.h"
 #include "parse.h"  #include "parse.h"
 #include "ox.h"  #include "ox.h"
Line 9 
Line 9 
 #define PRINT_XOFFSET   100  #define PRINT_XOFFSET   100
 #define PRINT_YOFFSET   100  #define PRINT_YOFFSET   100
   
 struct polyLine {  
   int numberOfSegments;  
   int limit;  
   int *x;  
   int *y;  
 };  
   
 static FILE *Fp = NULL;  static FILE *Fp = NULL;
   
 static void *gcmalloc(int a);  
 static struct polyLine *polyLine_new(void);  
 static struct polyLine *polyLine_addNewSegment(struct polyLine *pl,  
                                                                                            int x,int y);  
 static int polyLine_lastY(struct polyLine *pl);  
 static void polyLine_outputPS(struct polyLine *pl);  
 static void polyLine_outputProlog(int xmin, int ymin, int xmax, int ymax);  static void polyLine_outputProlog(int xmin, int ymin, int xmax, int ymax);
 static void polyLine_outputEpilog(void);  static void polyLine_outputEpilog(void);
 static void polyLine_error(char *s);  static void polyLine_error(char *s);
Line 35  static void polyLine_error(char *s);
Line 23  static void polyLine_error(char *s);
 #define YES 1  #define YES 1
 #define NO  0  #define NO  0
   
   
 static int Xsize = 0;  static int Xsize = 0;
 static int Ysize = 0;  static int Ysize = 0;
 static int updatePolyLine(struct polyLine *pl[], int plSize,  
                                    int prev[], int Curr[], int x);  
   
 static void *gcmalloc(a)  {  static void polyLine_outputPS_dashed_line(int x0,int y0,int x1,int y1);
   void *m;  static int polyLine_pline(struct canvas *can);
   /* BUG:  interruption must be locked. */  static int Strategy_generate_PS = 0;
   m = (void *) GC_malloc(a);  
   if (m == NULL) polyLine_error("no memory");  
   return(m);  
 }  
   
 #define MINIMAL_POLYLINE_SIZE  10  
 static struct polyLine *polyLine_new_with_size(int size) {  
   struct polyLine *pl;  
   pl = (struct polyLine *)gcmalloc(sizeof(struct polyLine));  
   pl-> limit = (size > 0? size: 1);  
   pl-> numberOfSegments = 0;  
   pl->x = (int *)gcmalloc(sizeof(int)*(pl->limit));  
   pl->y = (int *)gcmalloc(sizeof(int)*(pl->limit));  
   return(pl);  
 }  
   
 static struct polyLine *polyLine_new(void) {  
   return( polyLine_new_with_size(MINIMAL_POLYLINE_SIZE));  
 }  
   
 static struct polyLine *polyLine_addNewSegment(struct polyLine *pl,  
                                                                                                 int x,int y) {  
   int n,limit,i;  
   struct polyLine *new_pl;  
   limit = pl->limit;  
   n = pl->numberOfSegments;  
   if (n >= limit) {  
         new_pl = polyLine_new_with_size( 2*limit );  
         new_pl -> numberOfSegments = pl->numberOfSegments;  
         for (i=0; i<pl->numberOfSegments; i++) {  
           new_pl->x[i] = pl->x[i];  
           new_pl->y[i] = pl->y[i];  
         }  
         pl = new_pl;  
   }  
   pl->numberOfSegments++;  
   pl->x[n] = x;  
   pl->y[n] = y;  
   return(pl);  
 }  
   
 static int polyLine_lastY(struct polyLine *pl) {  
   int n;  
   n = pl->numberOfSegments;  
   if (n == 0) {  
         polyLine_error("polyLine_lastY:  empty polyLine.");  
   }  
   return(pl->y[n-1]);  
 }  
   
   
 static void polyLine_outputPS(struct polyLine *pl) {  
   int n,i;  
   n = pl->numberOfSegments;  
   if (n == 1) {  
         fprintf(Fp," %d %d ifplot_putpixel\n",translateX(pl->x[0]),translateY(pl->y[0]));  
   }else if (n > 1) {  
         fprintf(Fp," newpath ");  
         for (i=0; i<n; i++) {  
           fprintf(Fp," %d %d ",translateX(pl->x[i]),translateY(pl->y[i]));  
           if (i==0) fprintf(Fp," moveto ");  
           else fprintf(Fp," lineto ");  
         }  
         fprintf(Fp," stroke\n");  
   }  
   fflush(Fp);  
 }  
 static void polyLine_outputProlog(int xmin, int ymin,int xmax, int ymax) {  static void polyLine_outputProlog(int xmin, int ymin,int xmax, int ymax) {
   fprintf(Fp,"%%!PS-Adobe-1.0\n");    fprintf(Fp,"%%!PS-Adobe-1.0\n");
   fprintf(Fp,"%%%%BoundingBox: %d %d %d %d \n",    fprintf(Fp,"%%%%BoundingBox: %d %d %d %d \n",
Line 119  static void polyLine_outputProlog(int xmin, int ymin,i
Line 39  static void polyLine_outputProlog(int xmin, int ymin,i
   fprintf(Fp,"%%%%Creator: This is generated by ifplot\n");    fprintf(Fp,"%%%%Creator: This is generated by ifplot\n");
   fprintf(Fp,"%%%%Title: ifplot\n");    fprintf(Fp,"%%%%Title: ifplot\n");
   fprintf(Fp,"%%%%EndComments: \n");    fprintf(Fp,"%%%%EndComments: \n");
     fprintf(Fp,"0.1 setlinewidth \n");
     fprintf(Fp,"2 setlinecap \n");
     fprintf(Fp,"2 setlinejoin \n");
   fprintf(Fp,"/ifplot_putpixel {  \n");    fprintf(Fp,"/ifplot_putpixel {  \n");
   fprintf(Fp,"    /yyy 2 1 roll def /xxx 2 1 roll def \n");    fprintf(Fp,"    /yyy 2 1 roll def /xxx 2 1 roll def \n");
   fprintf(Fp,"    gsave newpath xxx yyy .5 0 360 arc \n");    fprintf(Fp,"    gsave newpath xxx yyy .5 0 360 arc \n");
Line 133  static void polyLine_error(char *s) {
Line 56  static void polyLine_error(char *s) {
   fprintf(stderr,"Error in smoothing: %s\n",s);    fprintf(stderr,"Error in smoothing: %s\n",s);
   exit(-1);    exit(-1);
 }  }
 static int updatePolyLine(struct polyLine *pl[], int plSize,  
                                                   int prev[], int curr[],int x) {  
   /*  1 <= y <= Ysize ,  curr[0], curr[Ysize+1]  = 0 ,  i=y .  
           prev = {-1, -1, -1, 0, 1, -1, 2, -1}  -1: no polyLine. 0:pl[0], 1:pl[1],  
           curr = {0,  1,  0,  1, 1, 1,  0, 0}    0: no dot, 1: dot.  
   */  
   /* Return Value:  plSize, pl, prev */  
   int i;  
   static int *prevHasNext = NULL;  
   static struct polyLine **tmpPl = NULL;  
   static struct polyLine **newPl = NULL;  
   int tmpPlSize = 0;  
   static int *tmpPrev = NULL;  
   int newPlSize = 0;  
   extern int Ysize;  
   if (prevHasNext == NULL) prevHasNext = (int *)gcmalloc(sizeof(int)*(Ysize+2));  
   if (tmpPl == NULL)  
         tmpPl = (struct polyLine **)gcmalloc(sizeof(struct polyLine *)*(Ysize+2));  
   if (newPl == NULL)  
         newPl = (struct polyLine **)gcmalloc(sizeof(struct polyLine *)*(Ysize+2));  
   if (tmpPrev == NULL) tmpPrev = (int *)gcmalloc(sizeof(int)*(Ysize+2));  
   for (i=0; i<Ysize+2; i++) {  
         prevHasNext[i] = NO; newPl[i] = NULL; tmpPl[i] = NULL;  
         tmpPrev[i] = -1;  
   }  
   prev[0] = prev[Ysize+1] = -1;  
   for (i=1; i <= Ysize; i++) {  
         if (curr[i] IS_DOT) {  
           if (prev[i-1] IS_POLYLINE && !prevHasNext[i-1]) {  
                 pl[prev[i-1]] = polyLine_addNewSegment(pl[prev[i-1]],x,i);  
                 prevHasNext[i-1] = YES;  
           }else if (prev[i] IS_POLYLINE && !prevHasNext[i]) {  
                 pl[prev[i]] = polyLine_addNewSegment(pl[prev[i]],x,i);  
                 prevHasNext[i] = YES;  
           }else if (prev[i+1] IS_POLYLINE && !prevHasNext[i+1]) {  
                 pl[prev[i+1]] = polyLine_addNewSegment(pl[prev[i+1]],x,i);  
                 prevHasNext[i+1] = YES;  
           }else{  
                 newPl[newPlSize] = polyLine_new();  
                 newPl[newPlSize] = polyLine_addNewSegment(newPl[newPlSize],x,i);  
                 newPlSize++;  
           }  
         }  
   }  
   for (i=1; i<=Ysize; i++) {  
         if (prevHasNext[i] == NO && prev[i] IS_POLYLINE) {  
           polyLine_outputPS(pl[prev[i]]);  
         }  
   }  
   
   /* generate new pl and new prev */  static void polyLine_outputPS_dashed_line(int x0,int y0,int x1,int y1) {
   tmpPlSize = 0; tmpPrev[0] = tmpPrev[Ysize+1] = -1;    extern FILE *Fp;
   for (i = 1; i<=Ysize; i++) {    fprintf(Fp," gsave [3] 0 setdash newpath \n");
         if (prevHasNext[i] == YES) {    fprintf(Fp," %d %d moveto %d %d lineto stroke \n",x0,y0,x1,y1);
           tmpPl[tmpPlSize] = pl[prev[i]];    fprintf(Fp," stroke grestore \n");
           tmpPrev[polyLine_lastY(pl[prev[i]])] = tmpPlSize;  
           tmpPlSize++;  
         }  
   }  
   for (i=0; i<newPlSize; i++) {  
         tmpPl[tmpPlSize] = newPl[i];  
         tmpPrev[polyLine_lastY(newPl[i])] = tmpPlSize;  
         tmpPlSize++;  
   }  
   
   /* copy and update for the next step.  */  
   for (i=0; i< Ysize+2; i++) {  
         prev[i] = tmpPrev[i];  
   }  
   for (i=0; i<tmpPlSize; i++) {  
         pl[i] = tmpPl[i];  
   }  
   return(tmpPlSize);  
 }  }
   
 /*  #define D 5
 #define TEST_YSIZE 12  static int polyLine_pline(can)
 #define TEST_XSIZE 5  struct canvas *can;
 generatePS_from_image(FILE *fp) {  {
   int image_test[TEST_XSIZE][TEST_YSIZE] =          double w,w1,k,e,n;
                           {{0,0,1,0,0,0,0,0,0,0,0,0},          int x0,y0,x,y,xadj,yadj;
                            {0,1,0,1,0,0,0,0,0,1,0,0},          char buf[BUFSIZ];
                            {0,1,0,1,1,0,0,0,1,0,1,0},          double adjust_scale();
                            {0,0,1,0,0,0,0,0,0,1,0,1},  
                            {0,0,0,0,0,0,1,0,0,1,0,0}};  
   struct polyLine **pl;  
   int plSize = 0;  
   int *prev;  
   int *curr;  
   int i,x,y;  
   
   Xsize = TEST_XSIZE;          if ( can->noaxis )
   Ysize = TEST_YSIZE;                  return;
   pl = (struct polyLine **)gcmalloc(sizeof(struct polyLine *)*(Ysize+2));  
   prev = (int *)gcmalloc(sizeof(int)*(Ysize+2));          xadj = yadj = 0;
   curr = (int *)gcmalloc(sizeof(int)*(Ysize+2));          if ( (can->xmin < 0) && (can->xmax > 0) ) {
   Fp = fp;                  x0 = (int)((can->width-1)*(-can->xmin/(can->xmax-can->xmin)));
   polyLine_outputProlog(0,0,Xsize,Ysize);                  polyLine_outputPS_dashed_line(translateX(x0),translateY(0),
   for (i=0; i<= Ysize+1; i++) {                                                                            translateX(x0),translateY(can->height));
         prev[i] = -1;          } else if ( can->xmin >= 0 )
   }                  x0 = 0;
   for (x=0; x<Xsize; x++) {          else
         curr[0] = curr[Ysize+1] = 0;                  x0 = can->width-1-D;
         for (y=0; y<Ysize; y++) {          if ( (can->ymin < 0) && (can->ymax > 0) ) {
           if (image_test[x][y]) curr[y+1]=1;                  y0 = (int)((can->height-1)*(can->ymax/(can->ymax-can->ymin)));
           else curr[y+1] = 0;                  polyLine_outputPS_dashed_line(translateX(0),translateY(y0),
                                                                             translateX(can->width),translateY(y0));
           } else if ( can->ymin >= 0 )
                   y0 = can->height-1;
           else
                   y0 = D;
           /* BUG:  not written yet a code for PS.
           w = can->xmax-can->xmin;
           w1 = w * DEFAULTWIDTH/can->width;
           e = adjust_scale(EXP10(floor(log10(w1))),w1);
           for ( n = ceil(can->xmin/e); n*e<= can->xmax; n++ ) {
                   x = (int)can->width*(n*e-can->xmin)/w;
                   DRAWLINE(display,d,drawGC,x,y0,x,y0-D);
                   sprintf(buf,"%g",n*e);
                   DRAWSTRING(display,d,scaleGC,x+2,y0,buf,strlen(buf));
         }          }
         plSize = updatePolyLine(pl,plSize,prev,curr,x);          w = can->ymax-can->ymin;
   }          w1 = w * DEFAULTHEIGHT/can->height;
   for (y=0; y<Ysize+2; y++) {          e = adjust_scale(EXP10(floor(log10(w1))),w1);
         curr[y] = 0;          for ( n = ceil(can->ymin/e); n*e<= can->ymax; n++ ) {
   }                  y = (int)can->height*(1-(n*e-can->ymin)/w);
   plSize = updatePolyLine(pl,plSize,prev,curr,Xsize);                  DRAWLINE(display,d,drawGC,x0,y,x0+D,y);
   polyLine_outputEpilog();                  sprintf(buf,"%g",n*e);
                   if ( can->xmax <= 0 )
                           xadj = TEXTWIDTH(sffs,buf,strlen(buf));
                   DRAWSTRING(display,d,scaleGC,x0-xadj,y,buf,strlen(buf));
           }
           */
 }  }
 main() {  
   generatePS_from_image(stdout);  
 }  
   
 */  
   
 generatePS_from_image(FILE *fp,XImage *image,int xsize, int ysize,  generatePS_from_image(FILE *fp,XImage *image,int xsize, int ysize,
                                           int color[],int colorSize) {                                            int color[],int colorSize,
                                             struct canvas *can) {
   struct polyLine **pl;    struct polyLine **pl;
   int plSize = 0;    int plSize = 0;
   int *prev;    int *prev;
   int *curr;    int *curr;
   int i,x,y,c;    int i,x,y,c;
     extern int Strategy_generate_PS;
   
   Xsize = xsize;    Xsize = xsize;
   Ysize = ysize;    Ysize = ysize;
   pl = (struct polyLine **)gcmalloc(sizeof(struct polyLine *)*(Ysize+2));  
   prev = (int *)gcmalloc(sizeof(int)*(Ysize+2));  
   curr = (int *)gcmalloc(sizeof(int)*(Ysize+2));  
   Fp = fp;    Fp = fp;
   polyLine_outputProlog(0,0,Xsize,Ysize);    polyLine_outputProlog(0,0,Xsize,Ysize);
   for (c=0; c<colorSize; c++) {    switch(Strategy_generate_PS) {
         /* Set color if necessary */    default:
         for (i=0; i<= Ysize+1; i++) {          for (c=0; c<colorSize; c++) {
           prev[i] = -1;            /* Set color if necessary */
         }            for (x=0; x<Xsize; x++) {
         for (x=0; x<Xsize; x++) {                  for (y=0; y<Ysize; y++) {
           curr[0] = curr[Ysize+1] = 0;                    if ((int) XGetPixel(image,x,y) == color[c]){
           for (y=0; y<Ysize; y++) {                          fprintf(Fp," %d %d ", translateX(x),translateY(y) );
                 if ((int) XGetPixel(image,x,y) == color[c]) curr[y+1]=1;                          fprintf(Fp," ifplot_putpixel\n");
                 else curr[y+1] = 0;                    }
                   }
           }            }
           plSize = updatePolyLine(pl,plSize,prev,curr,x);  
         }          }
         for (y=0; y<Ysize+2; y++) {          break;
           curr[y] = 0;  
         }  
         plSize = updatePolyLine(pl,plSize,prev,curr,Xsize);  
   }    }
     polyLine_pline(can);
   polyLine_outputEpilog();    polyLine_outputEpilog();
 }  }
   
   
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>