-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_viewer.h
618 lines (517 loc) · 16.6 KB
/
image_viewer.h
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#pragma once
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <string>
#include <string_view>
#include <vector>
#include <SFML/Graphics.hpp>
#include <Magick++.h>
enum class ViewMode { SinglePage, DoublePage, DoublePageManga, ContinuousVert, Invalid };
class ImageViewerApp
{
private:
sf::RenderWindow window;
sf::View window_view;
ViewMode mode = ViewMode::SinglePage;
std::vector<sf::Sprite> sprites;
std::map<int, std::vector<std::string>> images;
std::map<std::string, int> image_tags;
std::map<std::string, int> texture_wide;
std::map<std::string, sf::Vector2f> texture_size;
std::map<std::string, sf::Texture> loaded_textures;
std::map<int, std::vector<std::string>>::iterator curr_tag_images;
int curr_image_index = 0;
int last_render_tag = 0;
int last_render_image_index = -1;
ViewMode last_render_mode = ViewMode::Invalid;
std::map<int, std::vector<std::vector<int>>> double_pages;
bool double_paging_change = false;
float wide_factor;
std::string repage_save_file;
bool reset_view = true;
std::map<int, std::string> user_bindings;
void load_config(std::string_view config_path)
{
char binding;
std::string command;
std::ifstream stream = std::ifstream(std::string(config_path));
while (stream >> binding)
{
std::getline(stream, command);
auto str_begin = command.find_first_not_of(" ");
command = command.substr(str_begin);
int key = sf::Keyboard::Key::A + (binding - 'a');
user_bindings[key] = command;
}
}
void load_repage_save(std::string_view save)
{
repage_save_file = save;
std::ifstream stream(repage_save_file);
std::string image;
while (std::getline(stream, image))
texture_wide[image] = 2;
}
int get_double_page_index(int image_index, int tag) const
{
auto it = double_pages.find(tag);
if (it == double_pages.end())
return 0;
const auto& curr_double_pages = it->second;
auto curr_double_page_it = std::find_if(curr_double_pages.begin(), curr_double_pages.end(), [image_index](const auto& double_indices)
{
return std::find(double_indices.begin(), double_indices.end(), image_index) != double_indices.end();
});
return curr_double_page_it - curr_double_pages.begin();
}
void update_double_pages(int tag)
{
const auto& tag_images_it = images.find(tag);
if (tag_images_it == images.end())
return;
const auto& tag_images = tag_images_it->second;
auto& tag_double_pages = double_pages[tag];
tag_double_pages.clear();
int is_prev_wide = true;
for (int i = 0; i < (int)tag_images.size(); ++i)
{
int is_wide = texture_wide[tag_images[i]];
if (is_wide || is_prev_wide || tag_double_pages.back().size() == 2)
tag_double_pages.emplace_back();
tag_double_pages.back().push_back(i);
is_prev_wide = is_wide;
}
if (tag == curr_tag())
double_paging_change = true;
}
void fix_double_pages(int image_index, int tag)
{
const auto& tag_images_it = images.find(tag);
if (tag_images_it == images.end())
return;
const auto& tag_images = images[tag];
auto& tag_double_pages = double_pages[tag];
if (texture_wide[tag_images[image_index]] == 1)
return;
int begin_change_page;
for (begin_change_page = get_double_page_index(image_index, tag); begin_change_page >= 0; --begin_change_page)
{
const auto& image = tag_images[tag_double_pages[begin_change_page][0]];
if (texture_wide[image] == 1)
{
begin_change_page++;
break;
}
}
begin_change_page = std::max(0, begin_change_page);
int first_changed_index = tag_double_pages[begin_change_page][0];
auto first_changed_image = tag_images[first_changed_index];
if (texture_wide[first_changed_image] == 2)
texture_wide[first_changed_image] = 0;
else
texture_wide[first_changed_image] = 2;
update_double_pages(tag);
}
sf::Texture& load_texture(const std::string& image_path, float scale = 1.f)
{
Magick::Image image(image_path);
image.magick("RGBA");
image.resize(Magick::Geometry(image.columns() * scale, image.rows() * scale));
Magick::Blob blob;
image.write(&blob);
sf::Image sf_image;
sf_image.create(image.columns(), image.rows(), (sf::Uint8*)blob.data());
auto& tex = loaded_textures[image_path];
tex.loadFromImage(sf_image);
return tex;
}
int curr_tag() const
{
if (images.empty())
return -1;
return curr_tag_images->first;
}
const std::string& curr_image() const
{
return curr_tag_images->second[curr_image_index];
}
void prepare_render()
{
bool image_changed = curr_image_index != last_render_image_index || curr_tag() != last_render_tag;
bool mode_changed = mode != last_render_mode;
if (mode == ViewMode::SinglePage)
{
if (image_changed || reset_view || mode_changed)
{
auto tex_size = texture_size[curr_image()];
float scale_x = window_view.getSize().x / tex_size.x;
float scale_y = window_view.getSize().y / tex_size.y;
float scale = std::min(scale_x, scale_y);
window_view.setCenter(tex_size * scale / 2.f);
window.setView(window_view);
loaded_textures.clear();
sprites.clear();
sprites.emplace_back(load_texture(curr_image(), scale));
}
}
else if (mode == ViewMode::DoublePage || mode == ViewMode::DoublePageManga)
{
if (mode_changed)
for (auto[tag, images_vec] : images)
update_double_pages(tag);
if (image_changed || mode_changed || reset_view || double_paging_change)
{
const auto& curr_double_pages = double_pages[curr_tag()];
int curr_double_index = get_double_page_index(curr_image_index, curr_tag());
int first_image_index = curr_double_pages[curr_double_index][0];
float scale2 = 1.f;
sf::Vector2f double_tex_size;
double_tex_size = texture_size[images[curr_tag()][first_image_index]];
if (curr_double_pages[curr_double_index].size() == 2)
{
int second_image_index = curr_double_pages[curr_double_index][1];
scale2 = double_tex_size.y / texture_size[images[curr_tag()][second_image_index]].y;
double_tex_size.x += texture_size[images[curr_tag()][second_image_index]].x * scale2;
}
float scale_x = window_view.getSize().x / double_tex_size.x;
float scale_y = window_view.getSize().y / double_tex_size.y;
float scale = std::min(scale_x, scale_y);
window_view.setCenter(double_tex_size * scale / 2.f);
window.setView(window_view);
loaded_textures.clear();
sprites.clear();
sprites.emplace_back(load_texture(images[curr_tag()][first_image_index], scale));
if (curr_double_pages[curr_double_index].size() == 2)
{
int second_image_index = curr_double_pages[curr_double_index][1];
sprites.emplace_back(load_texture(images[curr_tag()][second_image_index], scale * scale2));
if (mode == ViewMode::DoublePageManga)
sprites[0].move(sprites[1].getGlobalBounds().width, 0);
else
sprites[1].move(sprites[0].getGlobalBounds().width, 0);
}
}
}
else if (mode == ViewMode::ContinuousVert)
{
}
}
void render()
{
if (curr_image_index != last_render_image_index || curr_tag() != last_render_tag)
{
if (images.empty())
window.setTitle("no images loaded");
else
{
std::string title = std::to_string(curr_tag()) + " - " + curr_image() + " [" + std::to_string(curr_image_index + 1) + "/" + std::to_string(images[curr_tag()].size()) + "]";
window.setTitle(title);
std::cout << "current_image=\"" << curr_image() << '"' << std::endl;
}
}
if (mode != last_render_mode)
{
std::string mode_str;
if (mode == ViewMode::SinglePage)
mode_str = "single";
else if (mode == ViewMode::DoublePage)
mode_str = "double";
else if (mode == ViewMode::DoublePageManga)
mode_str = "manga";
std::cout << "current_mode=" << mode_str << std::endl;
}
if (!images.empty())
{
prepare_render();
for (const auto& sprite : sprites)
window.draw(sprite);
}
last_render_image_index = curr_image_index;
last_render_tag = curr_tag();
last_render_mode = mode;
reset_view = false;
double_paging_change = false;
}
void poll_events(float dt)
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::Resized)
{
window_view.setSize(event.size.width, event.size.height);
reset_view = true;
}
else if (event.type == sf::Event::KeyPressed)
{
if ((event.key.code == sf::Keyboard::Space ||
event.key.code == sf::Keyboard::BackSpace) && !images.empty())
{
if (mode == ViewMode::DoublePage || mode == ViewMode::DoublePageManga)
{
int offset = 1;
if (event.key.code == sf::Keyboard::BackSpace)
offset = -1;
int double_index = get_double_page_index(curr_image_index, curr_tag());
double_index += offset;
int corrected_index = std::clamp(double_index, 0, (int)double_pages[curr_tag()].size() - 1);
if (double_index != corrected_index)
{
if ((offset == 1 && std::next(curr_tag_images) == images.end()) || (offset == -1 && curr_tag_images == images.begin()))
std::cout << "last_in_dir=" << (offset > 0) - (offset < 0) << std::endl;
else
{
std::advance(curr_tag_images, offset);
if (offset == 1)
corrected_index = 0;
else
corrected_index = double_pages[curr_tag()].size() - 1;
}
}
curr_image_index = double_pages[curr_tag()][corrected_index][0];
}
else
{
int offset = 1;
if (event.key.code == sf::Keyboard::BackSpace)
offset = -1;
curr_image_index += offset;
int corrected_index = std::clamp(curr_image_index, 0, (int)curr_tag_images->second.size() - 1);
if (curr_image_index != corrected_index)
{
if ((offset == 1 && curr_tag_images == std::prev(images.end())) || (offset == -1 && curr_tag_images == images.begin()))
std::cout << "last_in_dir=" << (offset > 0) - (offset < 0) << std::endl;
else
{
std::advance(curr_tag_images, offset);
if (offset == 1)
corrected_index = 0;
else
corrected_index = curr_tag_images->second.size() - 1;
}
}
curr_image_index = corrected_index;
}
}
else
{
auto it = user_bindings.find((int)event.key.code);
if (it != user_bindings.end())
run_command(it->second);
}
}
}
}
void run_command(std::string_view cmd)
{
std::string_view action(cmd.begin(), cmd.begin() + cmd.find('('));
std::vector<std::string> args;
auto arg_begin = cmd.begin() + action.length() + 1;
while (arg_begin != cmd.end())
{
auto next_sep_iter = std::find(arg_begin, cmd.end(), ',');
if (next_sep_iter == cmd.end())
next_sep_iter--;
if (next_sep_iter - arg_begin > 0)
args.emplace_back(arg_begin, next_sep_iter);
arg_begin = next_sep_iter + 1;
}
if (action == "add_images")
{
int tag = 0;
if (args.size() > 1)
tag = std::stoi(args[0]);
for (int i = (args.size() > 1); i < (int)args.size(); ++i)
{
auto image_path = std::regex_replace(args[i], std::regex("^ +| +$"), "$1");
if (image_path.empty())
continue;
if (sf::Texture tex; tex.loadFromFile(image_path))
{
tex.setSmooth(true);
bool is_wide = tex.getSize().x > (tex.getSize().y * wide_factor);
if (!texture_wide.contains(image_path) || is_wide)
texture_wide[image_path] = is_wide;
texture_size[image_path] = (sf::Vector2f)tex.getSize();
auto& tag_images_vec = images[tag];
auto inserted_it = tag_images_vec.insert(std::upper_bound(tag_images_vec.begin(), tag_images_vec.end(), image_path), image_path);
int new_index = inserted_it - tag_images_vec.begin();
if (images.size() == 1 && tag_images_vec.size() == 1)
{
curr_image_index = 0;
curr_tag_images = images.find(tag);
}
else if (tag == curr_tag() && new_index <= curr_image_index)
{
curr_image_index++;
last_render_image_index++;
}
}
}
if (mode == ViewMode::DoublePage || mode == ViewMode::DoublePageManga)
update_double_pages(tag);
}
else if (action == "goto_image_byindex")
{
int new_index = std::stoi(args[0]);
int tag = curr_tag();
if (args.size() == 2)
tag = std::stoi(args[1]);
auto tag_images_iter = images.find(tag);
if (tag_images_iter != images.end())
{
curr_tag_images = tag_images_iter;
auto& tag_images_vec = tag_images_iter->second;
if (new_index < 0)
new_index = (int)tag_images_vec.size() + new_index;
if (new_index >= 0 && new_index < (int)tag_images_vec.size())
curr_image_index = new_index;
else
std::cerr << "index " << new_index << " not present on tag " << tag << std::endl;
}
else
std::cerr << "tag " << args[0] << " not present" << std::endl;
}
else if (action == "goto_image_byname")
{
int tag = curr_tag();
if (args.size() == 2)
tag = std::stoi(args[1]);
auto tag_images_iter = images.find(tag);
if (tag_images_iter != images.end())
{
curr_tag_images = tag_images_iter;
auto& tag_images_vec = tag_images_iter->second;
auto image_iter = std::find(tag_images_vec.begin(), tag_images_vec.end(), args[0]);
if (image_iter != tag_images_vec.end())
curr_image_index = image_iter - tag_images_vec.begin();
else
std::cerr << args[0] << " not present on tag " << tag << std::endl;
}
else
std::cerr << "tag " << tag << " not present" << std::endl;
}
else if (action == "goto_tag" || action == "remove_tag")
{
int tag = std::stoi(args[0]);
auto tag_images_iter = images.find(tag);
if (tag_images_iter != images.end())
{
if (action == "goto_tag")
{
curr_tag_images = tag_images_iter;
curr_image_index = 0;
}
else
{
if (tag == curr_tag())
{
curr_image_index = 0;
if (std::next(curr_tag_images) != images.end())
std::advance(curr_tag_images, 1);
else if (curr_tag_images != images.begin())
std::advance(curr_tag_images, -1);
else curr_image_index = -1;
}
images.erase(tag);
}
}
else
std::cerr << "tag " << tag << " not present" << std::endl;
}
else if (action == "change_mode")
{
if (args[0] == "single")
mode = ViewMode::SinglePage;
else if (args[0] == "double")
mode = ViewMode::DoublePage;
else if (args[0] == "manga")
mode = ViewMode::DoublePageManga;
else if (args[0] == "vert")
mode = ViewMode::ContinuousVert;
else
std::cerr << args[0] << "is not a valid mode" << std::endl;
}
else if (action == "repage")
{
if (mode != ViewMode::DoublePage && mode != ViewMode::DoublePageManga)
std::cerr << "repaging only with double page layout" << std::endl;
else
{
int change_index = curr_image_index;
int change_tag = curr_tag();
if (!args.empty())
{
change_index = std::stoi(args[0]);
change_tag = std::stoi(args[1]);
}
fix_double_pages(change_index, change_tag);
}
}
else if (action == "output_image_list")
{
for (const auto& [tag, vec] : images)
{
for (const auto& image : vec)
{
std::cout << "tag " << tag << " - " << image << std::endl;
}
}
}
else if (action == "output_string")
std::cout << args[0] << std::endl;
else if (action == "quit")
window.close();
else
{
std::cerr << action << " is not a valid command" << std::endl;
}
}
void check_stdin()
{
if (std::cin.rdbuf()->in_avail())
{
std::string cmd;
std::getline(std::cin, cmd);
run_command(cmd);
}
}
public:
ImageViewerApp(std::string_view config_path, std::string_view save_path, float wide_fact)
{
window.create(sf::VideoMode(800, 600), "image viewer", sf::Style::Default);
window.setKeyRepeatEnabled(false);
window.setFramerateLimit(60);
if (!config_path.empty())
load_config(config_path);
if (!save_path.empty())
load_repage_save(save_path);
wide_factor = wide_fact;
std::cin.sync_with_stdio(false);
}
~ImageViewerApp()
{
std::ofstream repage_file(repage_save_file);
for (const auto&[image, wide] : texture_wide)
if (wide == 2)
repage_file << image << std::endl;
}
void run()
{
sf::Clock clock;
float dt = 0.f;
while (window.isOpen())
{
check_stdin();
poll_events(dt);
window.clear();
render();
window.display();
dt = clock.restart().asSeconds();
}
}
};