forked from kidanger/vpv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.cpp
More file actions
523 lines (445 loc) · 14.1 KB
/
Sequence.cpp
File metadata and controls
523 lines (445 loc) · 14.1 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <cstdlib>
#include <cstring>
#include <glob.h>
#include <algorithm>
#include <limits>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <sys/types.h> // stat
#include <sys/stat.h> // stat
#include <SFML/OpenGL.hpp>
#include "Sequence.hpp"
#include "Player.hpp"
#include "View.hpp"
#include "Colormap.hpp"
#include "Image.hpp"
#include "alphanum.hpp"
#include "globals.hpp"
#include "plambda.h"
#include "gmic/gmic.h"
#ifdef USE_OCTAVE
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <octave/builtin-defun-decls.h>
#endif
const char* getGLError(GLenum error)
{
#define casereturn(x) case x: return #x
switch (error) {
casereturn(GL_INVALID_ENUM);
casereturn(GL_INVALID_VALUE);
casereturn(GL_INVALID_OPERATION);
casereturn(GL_INVALID_FRAMEBUFFER_OPERATION);
casereturn(GL_OUT_OF_MEMORY);
default:
case GL_NO_ERROR:
return "";
}
#undef casereturn
}
#define GLDEBUG(x) \
x; \
{ \
GLenum e; \
while((e = glGetError()) != GL_NO_ERROR) \
{ \
printf("%s:%s:%d for call %s", getGLError(e), __FILE__, __LINE__, #x); \
} \
}
Sequence::Sequence()
{
static int id = 0;
id++;
ID = "Sequence " + std::to_string(id);
view = nullptr;
player = nullptr;
colormap = nullptr;
image = nullptr;
valid = false;
force_reupload = false;
loadedFrame = -1;
loadedRect = ImRect();
glob.reserve(4096);
glob_.reserve(4096);
glob = "";
glob_ = "";
editprog[0] = 0;
}
// from https://stackoverflow.com/a/236803
template<typename Out>
static void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
bool is_file(const std::string& filename)
{
struct stat info;
return !stat(filename.c_str(), &info) && !(info.st_mode & S_IFDIR); // let's assume any non-dir is a file
}
static void recursive_collect(std::vector<std::string>& filenames, std::string glob)
{
std::vector<std::string> collected;
glob_t res;
::glob(glob.c_str(), GLOB_TILDE | GLOB_NOSORT | GLOB_BRACE, NULL, &res);
bool found = false;
for(unsigned int j = 0; j < res.gl_pathc; j++) {
std::string file(res.gl_pathv[j]);
collected.push_back(file);
found = found || is_file(file);
}
globfree(&res);
if (collected.size() == 1 && !found /* it's a directory */) {
recursive_collect(filenames, collected[0] + '/' + '*');
found = true;
} else {
std::sort(collected.begin(), collected.end(), doj::alphanum_less<std::string>());
for (auto str : collected)
filenames.push_back(str);
}
if (!found) {
std::vector<std::string> substr;
split(glob, ':', std::back_inserter(substr));
if (substr.size() >= 2) {
for (const std::string& s : substr) {
recursive_collect(filenames, s);
}
}
}
}
void Sequence::loadFilenames() {
filenames.resize(0);
recursive_collect(filenames, std::string(glob.c_str()));
if (filenames.empty() && !strcmp(glob.c_str(), "-")) {
filenames.push_back("-");
}
valid = filenames.size() > 0;
strcpy(&glob_[0], &glob[0]);
loadedFrame = -1;
if (player)
player->reconfigureBounds();
}
void Sequence::loadTextureIfNeeded()
{
if (valid && player) {
if (loadedFrame != player->frame || force_reupload) {
forgetImage();
}
}
}
void Sequence::forgetImage()
{
loadedRect = ImRect();
if (image && !image->is_cached) {
delete image;
}
image = nullptr;
force_reupload = true;
}
void Sequence::requestTextureArea(ImRect rect)
{
int frame = player->frame;
assert(frame > 0);
if (frame != loadedFrame) {
forgetImage();
}
const Image* img = getCurrentImage();
if (!img)
return;
int w = img->w;
int h = img->h;
rect.Clip(ImRect(0, 0, w, h));
bool reupload = force_reupload;
if (!loadedRect.ContainsInclusive(rect)) {
loadedRect.Add(rect);
reupload = true;
loadedRect.Expand(128); // to avoid multiple uploads during zoom-out
loadedRect.Clip(ImRect(0, 0, w, h));
}
if (reupload) {
unsigned int gltype;
if (img->type == Image::UINT8)
gltype = GL_UNSIGNED_BYTE;
else if (img->type == Image::FLOAT)
gltype = GL_FLOAT;
else
assert(0);
unsigned int glformat;
if (img->format == Image::R)
glformat = GL_RED;
else if (img->format == Image::RG)
glformat = GL_RG;
else if (img->format == Image::RGB)
glformat = GL_RGB;
else if (img->format == Image::RGBA)
glformat = GL_RGBA;
else
assert(0);
if (texture.id == -1 || texture.size.x != w || texture.size.y != h
|| texture.type != gltype || texture.format != glformat) {
texture.create(w, h, gltype, glformat);
}
size_t elsize;
if (img->type == Image::UINT8) elsize = sizeof(uint8_t);
else if (img->type == Image::FLOAT) elsize = sizeof(float);
else assert(0);
auto area = loadedRect;
const uint8_t* data = (uint8_t*) img->pixels + elsize*(w * (int)area.Min.y + (int)area.Min.x)*img->format;
if (area.GetWidth() > 0 && area.GetHeight() > 0) {
glBindTexture(GL_TEXTURE_2D, texture.id);
GLDEBUG();
glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
GLDEBUG();
glTexSubImage2D(GL_TEXTURE_2D, 0, area.Min.x, area.Min.y, area.GetWidth(), area.GetHeight(),
glformat, gltype, data);
GLDEBUG();
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
GLDEBUG();
}
loadedFrame = frame;
force_reupload = false;
}
}
void Sequence::autoScaleAndBias()
{
for (int i = 0; i < 3; i++)
colormap->center[i] = .5f;
colormap->radius = .5f;
const Image* img = getCurrentImage();
if (!img)
return;
colormap->autoCenterAndRadius(img->min, img->max);
}
void Sequence::smartAutoScaleAndBias(ImVec2 p1, ImVec2 p2)
{
for (int i = 0; i < 3; i++)
colormap->center[i] = .5f;
colormap->radius = .5f;
const Image* img = getCurrentImage();
if (!img)
return;
if (p1.x < 0) p1.x = 0;
if (p1.y < 0) p1.y = 0;
if (p2.x >= img->w-1) p2.x = img->w - 1;
if (p2.y >= img->h-1) p2.y = img->h - 1;
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::min();
const float* data = (const float*) img->pixels;
for (int y = p1.y; y < p2.y; y++) {
for (int x = p1.x; x < p2.x; x++) {
for (int d = 0; d < img->format; d++) {
float v = data[d + img->format*(x+y*img->w)];
if (std::isfinite(v)) {
min = std::min(min, v);
max = std::max(max, v);
}
}
}
}
colormap->autoCenterAndRadius(min, max);
}
Image* run_edit_program(char* prog, Sequence::EditType edittype)
{
std::vector<Sequence*> seq;
while (*prog && *prog != ' ') {
char* old = prog;
int a = strtol(prog, &prog, 10);
if (prog == old) break;
if (a >= gSequences.size()) return 0;
seq.push_back(gSequences[a]);
if (*prog == ' ') break;
if (*prog) prog++;
}
while (*prog == ' ') prog++;
int n = seq.size();
float* x[n];
int w[n];
int h[n];
int d[n];
for (int i = 0; i < n; i++) {
const Image* img = seq[i]->getCurrentImage(true);
x[i] = (float*) img->pixels;
w[i] = img->w;
h[i] = img->h;
d[i] = img->format;
}
if (edittype == Sequence::PLAMBDA) {
int dd;
float* pixels = execute_plambda(n, x, w, h, d, prog, &dd);
if (!pixels)
return 0;
Image* img = new Image(pixels, *w, *h, (Image::Format) dd);
return img;
} else if (edittype == Sequence::GMIC) {
gmic_list<char> images_names;
gmic_list<float> images;
images.assign(n);
for (int i = 0; i < n; i++) {
gmic_image<float>& img = images[i];
img.assign(w[i], h[i], 1, d[i]);
float* xptr = x[i];
for (int y = 0; y < h[i]; y++) {
for (int x = 0; x < w[i]; x++) {
for (int z = 0; z < d[i]; z++) {
img(x, y, 0, z) = *(xptr++);
}
}
}
}
try {
gmic(prog, images, images_names);
} catch (gmic_exception &e) {
std::fprintf(stderr,"\n- Error encountered when calling G'MIC : '%s'\n", e.what());
return 0;
}
gmic_image<float>& image = images[0];
size_t size = image._width * image._height * image._spectrum;
float* data = (float*) malloc(sizeof(float) * size);
float* ptrdata = data;
for (int y = 0; y < image._height; y++) {
for (int x = 0; x < image._width; x++) {
for (int z = 0; z < image._spectrum; z++) {
*(ptrdata++) = image(x, y, 0, z);
}
}
}
Image* img = new Image(data, image._width, image._height, (Image::Format) image._spectrum);
return img;
#ifdef USE_OCTAVE
} else if (edittype == Sequence::OCTAVE) {
static octave::embedded_application* app;
if (!app) {
string_vector octave_argv(2);
octave_argv(0) = "embedded";
octave_argv(1) = "-q";
app = new octave::embedded_application(2, octave_argv.c_str_vec());
if (!app->execute()) {
std::cerr << "creating embedded Octave interpreter failed!" << std::endl;
return 0;
}
}
try {
octave_value_list in;
// create the function
octave_value_list in2;
in2(0) = octave_value(std::string(prog));
octave_value_list fs = Fstr2func(in2);
octave_function* f = fs(0).function_value();
// create the matrices
for (octave_idx_type i = 0; i < n; i++) {
dim_vector size(h[i], w[i], d[i]);
NDArray m(size);
float* xptr = x[i];
for (int y = 0; y < h[i]; y++) {
for (int x = 0; x < w[i]; x++) {
for (int z = 0; z < d[i]; z++) {
m(y, x, z) = *(xptr++);
}
}
}
in(i) = octave_value(m);
}
// eval
octave_value_list out = feval(f, in, 1);
if (out.length() > 0) {
std::cout << out.length() << " results" << std::endl;
NDArray m = out(0).array_value();
int w = m.cols();
int h = m.rows();
int d = m.pages();
size_t size = w * h * d;
float* data = (float*) malloc(sizeof(float) * size);
float* ptrdata = data;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
for (int z = 0; z < d; z++) {
*(ptrdata++) = m(y, x, z);
}
}
}
Image* img = new Image(data, w, h, (Image::Format) d);
return img;
} else {
std::cerr << "no image returned from octave\n";
}
} catch (const octave::exit_exception& ex) {
exit (ex.exit_status());
return 0;
} catch (const octave::execution_exception&) {
std::cerr << "error evaluating Octave code!" << std::endl;
return 0;
}
#endif
}
return 0;
}
const Image* Sequence::getCurrentImage(bool noedit) {
if (!valid || !player) {
return 0;
}
if (!image || noedit) {
int frame = player->frame - 1;
if (frame < 0 || frame >= filenames.size())
return 0;
const Image* img = Image::load(filenames[frame]);
image = img;
if (!noedit && editprog[0]) {
image = run_edit_program(editprog, edittype);
if (!image)
image = img;
}
}
return image;
}
const std::string Sequence::getTitle() const
{
std::string seqname = std::string(glob.c_str());
if (!valid)
return "(the sequence '" + seqname + "' contains no images)";
if (!player)
return "(no player associated with the sequence '" + seqname + "')";
if (!colormap)
return "(no colormap associated with the sequence '" + seqname + "')";
std::string title;
int id = 0;
while (gSequences[id] != this && id < gSequences.size())
id++;
title += "#" + std::to_string(id) + " ";
title += "[" + std::to_string(player->frame) + '/' + std::to_string(filenames.size()) + "]";
title += " " + filenames[player->frame - 1];
if (!image) {
title += " cannot be loaded";
}
return title;
}
void Sequence::showInfo() const
{
if (!valid || !player || !colormap)
return;
std::string seqname = std::string(glob.c_str());
if (image) {
ImGui::Text("Size: %dx%dx%d", image->w, image->h, image->format);
ImGui::Text("Range: %g..%g", image->min, image->max);
ImGui::Text("Zoom: %d%%", (int)(view->zoom*100));
ImGui::Separator();
float cmin, cmax;
colormap->getRange(cmin, cmax, image->format);
ImGui::Text("Displayed: %g..%g", cmin, cmax);
ImGui::Text("Shader: %s", colormap->getShaderName().c_str());
if (*editprog) {
const char* name;
switch (edittype) {
case PLAMBDA: name = "plambda"; break;
case GMIC: name = "gmic"; break;
case OCTAVE: name = "octave"; break;
}
ImGui::Text("Edited with %s", name);
}
}
}