From 8786dfe8a702c6eb543f2dd16ecaba4ba443406d Mon Sep 17 00:00:00 2001
From: William Joye <wjoye@cfa.harvard.edu>
Date: Thu, 28 Mar 2019 15:05:05 -0400
Subject: fix contour thread issue

---
 tksao/frame/contour.C   | 28 +++++++++++++++++++++++++---
 tksao/frame/contour.h   |  1 +
 tksao/frame/fvcontour.C | 48 ++++++++++++++++++++++++++++++++++++++----------
 tksao/frame/fvcontour.h |  8 ++++++++
 4 files changed, 72 insertions(+), 13 deletions(-)

diff --git a/tksao/frame/contour.C b/tksao/frame/contour.C
index 0c76d4a..1233b91 100644
--- a/tksao/frame/contour.C
+++ b/tksao/frame/contour.C
@@ -7,14 +7,15 @@
 #include "context.h"
 
 // ContourLevel
-ContourLevel::ContourLevel(Base* pp, double lev, const char* cc, int ww, 
-			   int dd, int* dl) 
+ContourLevel::ContourLevel(Base* pp, double lev,
+			   const char* cn,
+			   int ww, int dd, int* dl) 
 {
   parent_ = pp;
   gc_ = parent_->contourGC_;
 
   level_ = lev;
-  colorName_ = dupstr(cc);
+  colorName_ = dupstr(cn);
   color_ = parent_->getColor(colorName_);
   lineWidth_ = ww;
   dash_ = dd;
@@ -25,6 +26,27 @@ ContourLevel::ContourLevel(Base* pp, double lev, const char* cc, int ww,
   next_ = NULL;
 }
 
+// used in threads, so need to resolve color/colorName before
+ContourLevel::ContourLevel(Base* pp, double lev,
+			   const char* cn, unsigned long cc,
+			   int ww, int dd, int* dl) 
+{
+  parent_ = pp;
+  gc_ = parent_->contourGC_;
+
+  level_ = lev;
+  colorName_ = dupstr(cn);
+  //  color_ = parent_->getColor(colorName_);
+  color_ = cc;
+  lineWidth_ = ww;
+  dash_ = dd;
+  dlist_[0] = dl[0];
+  dlist_[1] = dl[1];
+
+  previous_ = NULL;
+  next_ = NULL;
+}
+
 ContourLevel::~ContourLevel()
 {
   if (colorName_)
diff --git a/tksao/frame/contour.h b/tksao/frame/contour.h
index af06dae..d01fe0b 100644
--- a/tksao/frame/contour.h
+++ b/tksao/frame/contour.h
@@ -35,6 +35,7 @@ class ContourLevel {
 
  public:
   ContourLevel(Base*, double, const char*, int, int, int*);
+  ContourLevel(Base*, double, const char*, unsigned long, int, int, int*);
   virtual ~ContourLevel();
 
   List<Contour>& lcontour() {return lcontour_;}
diff --git a/tksao/frame/fvcontour.C b/tksao/frame/fvcontour.C
index ab09d53..35ba452 100644
--- a/tksao/frame/fvcontour.C
+++ b/tksao/frame/fvcontour.C
@@ -22,8 +22,7 @@ static const char* methodName_[] = {
 
 enum Edge {TOP, RIGHT, BOTTOM, LEFT, NONE};
 
-static void build(long xdim, long ydim, double *image, Matrix& mx,
-		  FVContour* fv, List<ContourLevel>* lcl);
+static void build(t_fvcontour_arg* tt);
 static void trace(long xdim, long ydim, double cntr,
 		  long xCell, long yCell, int side, 
 		  double** rows, char* usedGrid, 
@@ -198,7 +197,7 @@ void FVContour::append(FitsImage* fits, pthread_t* thread, void* targ)
 static void* fvUnityThread(void*vv)
 {
   t_fvcontour_arg* tt = (t_fvcontour_arg*)vv;
-  build(tt->width, tt->height, tt->dest, tt->mm, tt->fv, tt->lcl);
+  build(tt);
   return NULL;
 }
 
@@ -244,6 +243,12 @@ void FVContour::unity(FitsImage* fits, pthread_t* thread, void* targ)
   tt->r = 0;
   tt->mm = mm;
   tt->fv = this;
+  tt->parent = parent_;
+  tt->colorName = colorName_;
+  tt->color = parent_->getColor(colorName_);
+  tt->lineWidth = lineWidth_;
+  tt->dash = dash_;
+  tt->dlist = dlist_;
   tt->lcl = new List<ContourLevel>;
 
   int result = pthread_create(thread, NULL, fvUnityThread, targ);
@@ -257,7 +262,7 @@ static void* fvSmoothThread(void*vv)
   convolve(tt->kernel, tt->src, tt->dest,
 	   tt->xmin, tt->ymin, tt->xmax, tt->ymax,
 	   tt->width, tt->r);
-  build(tt->width, tt->height, tt->dest, tt->mm, tt->fv, tt->lcl);
+  build(tt);
   return NULL;
 }
 
@@ -311,6 +316,12 @@ void FVContour::smooth(FitsImage* fits, pthread_t* thread, void* targ)
   tt->r = smooth_-1;
   tt->mm = mm;
   tt->fv = this;
+  tt->parent = parent_;
+  tt->colorName = colorName_;
+  tt->color = parent_->getColor(colorName_);
+  tt->lineWidth = lineWidth_;
+  tt->dash = dash_;
+  tt->dlist = dlist_;
   tt->lcl = new List<ContourLevel>;
 
   int result = pthread_create(thread, NULL, fvSmoothThread, targ);
@@ -321,7 +332,7 @@ void FVContour::smooth(FitsImage* fits, pthread_t* thread, void* targ)
 static void* fvBlockThread(void*vv)
 {
   t_fvcontour_arg* tt = (t_fvcontour_arg*)vv;
-  build(tt->width, tt->height, tt->dest, tt->mm, tt->fv, tt->lcl);
+  build(tt);
   return NULL;
 }
 
@@ -400,6 +411,12 @@ void FVContour::block(FitsImage* fits, pthread_t* thread, void* targ)
   tt->r = 0;
   tt->mm = mm;
   tt->fv = this;
+  tt->parent = parent_;
+  tt->colorName = colorName_;
+  tt->color = parent_->getColor(colorName_);
+  tt->lineWidth = lineWidth_;
+  tt->dash = dash_;
+  tt->dlist = dlist_;
   tt->lcl = new List<ContourLevel>;
 
   int result = pthread_create(thread, NULL, fvBlockThread, targ);
@@ -407,9 +424,21 @@ void FVContour::block(FitsImage* fits, pthread_t* thread, void* targ)
     internalError("Unable to Create Thread");
 }
 
-void build(long xdim, long ydim, double *image, Matrix& mx,
-	   FVContour* fv, List<ContourLevel>* lcl)
+void build(t_fvcontour_arg* tt)
 {
+  long xdim = tt->width;
+  long ydim = tt->height;
+  double* image = tt->dest;
+  Matrix& mx = tt->mm;
+  FVContour* fv = tt->fv;
+  Base* parent = tt->parent;
+  char* colorName = tt->colorName;
+  unsigned long color = tt->color;
+  int lineWidth = tt->lineWidth;
+  int dash = tt->dash;
+  int* dlist = tt->dlist;
+  List<ContourLevel>* lcl = tt->lcl;
+
   long nelem = xdim*ydim;
   char* usedGrid = new char[nelem];
   double** rows = new double*[ydim];
@@ -420,9 +449,8 @@ void build(long xdim, long ydim, double *image, Matrix& mx,
   for (long c=0; c<fv->scale()->size(); c++) {
     double cntour = fv->scale()->level(c);
 
-    ContourLevel* cl =new ContourLevel(fv->parent(), cntour,
-				       fv->colorName(), fv->lineWidth(), 
-				       fv->dash(), fv->dlist());
+    ContourLevel* cl =new ContourLevel(parent, cntour, colorName, color,
+				       lineWidth, dash, dlist);
     memset(usedGrid,0,nelem);
 
     //  Search outer edge
diff --git a/tksao/frame/fvcontour.h b/tksao/frame/fvcontour.h
index ff8555b..214d834 100644
--- a/tksao/frame/fvcontour.h
+++ b/tksao/frame/fvcontour.h
@@ -25,6 +25,14 @@ typedef struct {
   int r;
   Matrix mm;
   FVContour* fv;
+
+  Base* parent;
+  unsigned long color;
+  char* colorName;
+  int lineWidth;
+  int dash;
+  int* dlist;
+  
   List<ContourLevel>* lcl;
 } t_fvcontour_arg;
 
-- 
cgit v0.12