-
Notifications
You must be signed in to change notification settings - Fork 5
/
detrack.cpp
330 lines (287 loc) · 6.55 KB
/
detrack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <string>
#include <gtk/gtk.h>
#include "debug.h"
#include "util.h"
#include "imagesource.h"
#include "pixbuf_from_imagesource.h"
#include "imagesource_util.h"
#include "cachedimage.h"
#include "tiffsave.h"
#include "pixbufview.h"
#include "generaldialogs.h"
#include "progressbar.h"
#include "config.h"
#include "gettext.h"
#define _(x) gettext(x)
using namespace std;
struct PixTally
{
PixTally()
{
for(int i=0;i<8;++i)
tally[i]=0;
}
int &operator[](int i)
{
return(tally[i]);
}
int tally[8];
};
class ImageSource_HTally : public ImageSource
{
public:
ImageSource_HTally(ImageSource *src) : ImageSource(src), src(src), tally(NULL)
{
randomaccess=false;
tally=new PixTally[src->height];
currentrow=-1;
}
~ImageSource_HTally()
{
if(src)
delete(src);
if(tally)
delete[] tally;
}
ISDataType *GetRow(int row)
{
ISDataType *srcdata=src->GetRow(row);
if(row!=currentrow)
{
PixTally tallytmp;
for(int x=0; x<width; ++x)
{
for(int s=0; s<samplesperpixel; ++s)
{
tallytmp[s]+=ISTOEIGHT(srcdata[x*samplesperpixel+s]);
}
}
tally[row]=tallytmp;
}
return(srcdata);
}
int Min(int channel)
{
int min=src->height*src->width*255;
for(int y=0;y<src->height;++y)
{
if(tally[y][channel]<min)
min=tally[y][channel];
}
return(min);
}
int Max(int channel)
{
int max=0;
for(int y=0;y<src->height;++y)
{
if(tally[y][channel]>max)
max=tally[y][channel];
}
return(max);
}
PixTally &operator[](int i)
{
if(i<0) i=0;
if(i>=src->height)
i=src->height-1;
return(tally[i]);
}
protected:
ImageSource *src;
PixTally *tally;
};
class ImageSource_HMakeWeight : public ImageSource
{
public:
ImageSource_HMakeWeight(ImageSource *src,int windowsize=300) : ImageSource(src), source(NULL), img(src), windowsize(windowsize)
{
randomaccess=false;
ImageSource_HTally t(img.GetImageSource());
for(int y=0;y<src->height;++y)
t.GetRow(y);
makeweights=new PixTally[t.height];
PixTally runningavg;
for(int y=0;y<windowsize;++y)
{
for(int s=0;s<t.samplesperpixel;++s)
runningavg[s]=windowsize*t[y-windowsize-2][s];
}
int maxdiff=0;
for(int y=0;y<src->height;++y)
{
// cout << y << ": ";
for(int s=0;s<t.samplesperpixel;++s)
{
runningavg[s]-=t[y-windowsize/2][s];
runningavg[s]+=t[y+windowsize/2][s];
if(runningavg[s]/windowsize<t[y][s])
runningavg[s]=windowsize*t[y][s];
// cout << t[y][s] << ", " << runningavg[s]/windowsize;
int diff=runningavg[s]/windowsize-t[y][s];
makeweights[y][s]=diff;
if(diff>maxdiff)
maxdiff=diff;
}
// cout << endl;
}
Debug[TRACE] << " Max diff: " << maxdiff << ", requiring " << maxdiff/255 << " pixels" << endl;
width+=maxdiff/255;
source=img.GetImageSource();
MakeRowBuffer();
currentrow=-1;
}
~ImageSource_HMakeWeight()
{
if(source)
delete source;
}
ISDataType *GetRow(int row)
{
if(row==currentrow)
return(rowbuffer);
ISDataType *srcdata=source->GetRow(row);
Debug[TRACE] << "Got srcdata " << endl;
for(int x=0;x<source->width;++x)
{
for(int s=0;s<source->samplesperpixel;++s)
{
rowbuffer[x*samplesperpixel+s]=srcdata[x*samplesperpixel+s];
}
}
Debug[TRACE] << "Copied source data " << endl;
int xw=width-source->width;
for(int x=source->width;x<width;++x)
{
for(int s=0;s<source->samplesperpixel;++s)
{
rowbuffer[x*samplesperpixel+s]=EIGHTTOIS(makeweights[row][s]/xw);
}
}
currentrow=row;
return(rowbuffer);
}
protected:
ImageSource *source;
CachedImage img;
int windowsize;
PixTally *makeweights;
};
class DeTrackUI
{
public:
DeTrackUI() : img(NULL), smoothingwindowsize(150)
{
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window),800,600);
gtk_window_set_title (GTK_WINDOW (window),_("PixBufView Test"));
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
(GtkSignalFunc) gtk_main_quit, NULL);
GtkWidget *vbox=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_widget_show(GTK_WIDGET(vbox));
GtkWidget *hbox=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,0);
gtk_widget_show(hbox);
GtkWidget *tmp;
tmp=gtk_label_new(_("Smoothing:"));
gtk_box_pack_start(GTK_BOX(hbox),tmp,FALSE,FALSE,8);
gtk_widget_show(tmp);
smoothing=gtk_spin_button_new_with_range(50,1000,25);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(smoothing),smoothingwindowsize);
gtk_box_pack_start(GTK_BOX(hbox),smoothing,FALSE,FALSE,8);
g_signal_connect(G_OBJECT(smoothing),"value-changed",G_CALLBACK(smoothing_changed),this);
gtk_widget_show(smoothing);
tmp=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(hbox),tmp,TRUE,TRUE,0);
tmp=gtk_button_new_with_label(_("Save..."));
gtk_box_pack_start(GTK_BOX(hbox),tmp,FALSE,FALSE,8);
g_signal_connect(G_OBJECT(tmp),"clicked",G_CALLBACK(save_clicked),this);
gtk_widget_show(tmp);
pview=pixbufview_new(NULL,true);
gtk_box_pack_start(GTK_BOX(vbox),pview,TRUE,TRUE,0);
gtk_widget_show(pview);
gtk_widget_show(window);
}
~DeTrackUI()
{
if(img)
delete img;
}
static void smoothing_changed(GtkWidget *wid,gpointer ud)
{
DeTrackUI *ui=(DeTrackUI *)ud;
ui->smoothingwindowsize=gtk_spin_button_get_value(GTK_SPIN_BUTTON(ui->smoothing));
ui->Update();
}
static void save_clicked(GtkWidget *wid,gpointer ud)
{
DeTrackUI *ui=(DeTrackUI *)ud;
ui->Save();
}
void SetFile(const char *fn)
{
char *tmp=BuildFilename(fn,"-detracked","tif");
filename=tmp;
free(tmp);
ImageSource *is=ISLoadImage(fn);
img=new CachedImage(is);
Update();
}
void Update()
{
if(img)
{
ImageSource *is=img->GetImageSource();
is=new ImageSource_HMakeWeight(is,smoothingwindowsize);
GdkPixbuf *pb=pixbuf_from_imagesource(is);
delete is;
pixbufview_set_pixbuf(PIXBUFVIEW(pview),pb);
g_object_unref(G_OBJECT(pb));
}
}
void Save()
{
if(img)
{
char *fn=File_Save_Dialog(_("Please choose a filename for saving..."),filename.c_str(),window);
if(fn)
{
filename=fn;
ImageSource *is=img->GetImageSource();
is=new ImageSource_HMakeWeight(is,smoothingwindowsize);
TIFFSaver ts(fn,is);
ts.Save();
delete is;
free(fn);
}
}
}
protected:
std::string filename;
CachedImage *img;
GtkWidget *window;
GtkWidget *pview;
GtkWidget *smoothing;
int smoothingwindowsize;
};
int main(int argc, char **argv)
{
Debug.SetLevel(TRACE);
gtk_init(&argc,&argv);
try
{
if(argc>1)
{
DeTrackUI ui;
ui.SetFile(argv[1]);
gtk_main();
}
}
catch(const char *err)
{
Debug[ERROR] << err << endl;
}
return(0);
}