/
image.c
1463 lines (1327 loc) · 49 KB
/
image.c
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Module : image.c
Purpose : Routines dealing with image display
More : see qiv README
Policy : GNU GPL
Homepage : http://qiv.spiegl.de/
Original : http://www.klografx.net/qiv/
*/
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <gdk/gdkx.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "qiv.h"
#include "xmalloc.h"
static void setup_win(qiv_image *, GdkColor *win_bg);
//static void setup_magnify(qiv_image *, qiv_mgl *); // [lc]
static int used_masks_before=0;
static double load_elapsed;
static GdkCursor *cursor, *visible_cursor, *invisible_cursor;
#ifdef HAVE_EXIF
//////////// these taken from gwenview/src/imageutils/ //////////////////
//
/* Explanation extracted from http://sylvana.net/jpegcrop/exif_orientation.html
For convenience, here is what the letter F would look like if it were tagged
correctly and displayed by a program that ignores the orientation tag (thus
showing the stored image):
1 2 3 4 5 6 7 8
888888 888888 88 88 8888888888 88 88 8888888888
88 88 88 88 88 88 88 88 88 88 88 88
8888 8888 8888 8888 88 8888888888 8888888888 88
88 88 88 88
88 88 888888 888888
NORMAL HFLIP ROT_180 VFLIP TRANSPOSE ROT_90 TRANSVERSE ROT_270
*/
enum Orientation {
NOT_AVAILABLE=0,
NORMAL =1,
HFLIP =2,
ROT_180 =3,
VFLIP =4,
TRANSPOSE =5,
ROT_90 =6,
TRANSVERSE =7,
ROT_270 =8
};
// Exif
#include "libexif/exif-data.h"
#define flipH(q) imlib_image_flip_horizontal();
#define flipV(q) imlib_image_flip_vertical();
#define transpose(q) imlib_image_flip_diagonal();
#define rot90(q) imlib_image_orientate(1);
#define rot180(q) imlib_image_orientate(2);
#define rot270(q) imlib_image_orientate(3);
#define swapWH(q) swap(&q->orig_w, &q->orig_h); swap(&q->win_w, &q->win_h);
void transform( qiv_image *q, enum Orientation orientation) {
switch (orientation) {
default: return;
case HFLIP: flipH(q); q->infotext = ("(Flipped horizontally)"); break;
case VFLIP: flipV(q); q->infotext = ("(Flipped vertically)"); break;
case ROT_180: rot180(q); q->infotext = ("(Turned upside down)"); break;
case TRANSPOSE: transpose(q); swapWH(q); q->infotext = ("(Transposed)"); break;
case ROT_90: rot90(q); swapWH(q); q->infotext = ("(Rotated left)"); break;
case TRANSVERSE: transpose(q); rot180(q); swapWH(q); q->infotext = ("(Transversed)"); break;
case ROT_270: rot270(q); swapWH(q); q->infotext = ("(Rotated left)"); break;
}
}
//#include "libexif/exif-tag.h" //EXIF_TAG_ORIENTATION
enum Orientation orient( const char * path) {
enum Orientation orientation = NOT_AVAILABLE;
ExifData * mExifData = exif_data_new_from_file( path);
if (mExifData) {
ExifEntry * mOrientationEntry = exif_content_get_entry( mExifData->ifd[ EXIF_IFD_0], EXIF_TAG_ORIENTATION);
if (mOrientationEntry) {
ExifByteOrder mByteOrder = exif_data_get_byte_order( mExifData);
short value=exif_get_short( mOrientationEntry->data, mByteOrder);
if (value>=NORMAL && value<=ROT_270)
orientation = value; //Orientation( value);
}
exif_data_unref( mExifData );
}
return orientation;
}
#endif //HAVE_EXIF
/* The caller takes ownership of the returned value. */
static char* get_thumbnail_filename(const char *filename, char *is_maybe_image_file_out) {
const char *r;
const char *p;
char *thumbnail_filename;
char *tmp_filename = NULL;
size_t prefixlen;
struct stat st;
char link_target[512];
ssize_t readlink_result;
char do_try_readlink = 1;
again:
p = r = filename + strlen(filename);
/* Replace image extension with .th.jpg, save result to thumbnail_filename */
while (p != filename && p[-1] != '/' && p[-1] != '.') {
--p;
}
prefixlen = (p != filename && p[-1] == '.') ?
p - filename - 1 : r - filename;
thumbnail_filename = xmalloc(prefixlen + 8);
memcpy(thumbnail_filename, filename, prefixlen);
strcpy(thumbnail_filename + prefixlen, ".th.jpg");
free(tmp_filename); /* From this point `filename' is useless */
if (0 == stat(thumbnail_filename, &st) && S_ISREG(st.st_mode)) {
return thumbnail_filename;
}
free(thumbnail_filename);
if (!do_try_readlink) {
/* Already followed the .git/annex/object symlink, don't try to follow another symlink. */
} else if ((readlink_result = readlink(filename, link_target, sizeof(link_target))) > 0 &&
(size_t)readlink_result < sizeof(link_target)) {
/* It's a symlink and it's not too long. For example (198 bytes):
* ../.git/annex/objects/G1/mX/SHA256E-s12345--aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg/SHA256E-s12345--aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg
*/
static const char prefix[] = ".git/annex/objects/";
char *q;
do_try_readlink = 0;
link_target[readlink_result] = '\0';
for (q = link_target; q[0] == '.' && q[1] == '.' && q[2] == '/'; q += 3) {}
/* Found an object in the git-annex annex? */
if (0 == strncmp(q, prefix, sizeof(prefix) - 1)) {
size_t i;
/* Strip the basename from the end: filename[:i]. */
for (i = strlen(filename); i > 0 && filename[i - 1] != '/'; --i) {}
tmp_filename = xmalloc(i + strlen(link_target) + 1);
memcpy(tmp_filename, filename, i);
strcpy(tmp_filename + i, link_target);
filename = tmp_filename; /* Look for the thumbnail there. */
goto again;
}
} else if (readlink_result < 0 && errno == ENOENT) {
*is_maybe_image_file_out = 0;
}
return NULL;
}
/* Get the dimensionsions from the REALDIMEN: comment in the thumbnail
* image.
*
* It's very fast, because it reads only the image headers quickly.
* Currently it supports only JPEG thumbnails. It may have to read 30 kB of
* data to find the COM segment.
*/
static int get_real_dimensions_fast(
FILE *f, gint *w_out, gint *h_out) {
static const char realdimen_prefix[] = "REALDIMEN:";
char comment[256];
int c, m;
unsigned i, ss;
if ((c = getc(f)) < 0) return -2; /* Truncated (empty). */
if (c != 0xff) return -3;
if ((c = getc(f)) < 0) return -2; /* Truncated. */
if (c != 0xd8) return -3; /* Not a JPEG file, SOI expected. */
for (;;) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
if (c != 0xff) return -3; /* Not a JPEG file, marker expected. */
if ((m = getc(f)) < 0) return -2; /* Truncated. */
while (m == 0xff) { /* Padding. */
if ((m = getc(f)) < 0) return -2; /* Truncated. */
}
if (m == 0xd8) return -4; /* SOI unexpected. */
if (m == 0xd9) break; /* EOI. */
if (m == 0xda) break; /* SOS. Would need special escaping to process. */
if ((c = getc(f)) < 0) return -2; /* Truncated. */
ss = (c + 0U) << 8;
if ((c = getc(f)) < 0) return -2; /* Truncated. */
ss += c;
if (ss < 2) return -5; /* Segment too short. */
ss -= 2;
if (m == 0xfe && ss < sizeof(comment)) {
for (i = 0; i < ss; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
comment[i] = c;
}
comment[i] = '\0';
if (strncmp(comment, realdimen_prefix,
sizeof(realdimen_prefix) - 1) == 0) {
int w, h;
if (sscanf(comment + sizeof(realdimen_prefix) - 1,
"%dx%d", &w, &h) == 2) {
*w_out = w;
*h_out = h;
return 0;
} else {
return -7; /* Bad REALDIMEN: syntax. */
}
}
*w_out = *h_out = -2;
} else {
for (; ss > 0; --ss) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
}
}
}
return -6; /* REALDIMEN: comment not found. */
}
/* Populates *w_out and *h_out with the dimensions of the image file
* specified in image_name. On an error, returns a negative number and keeps
* w_out and h_out unset.
*
* It's very fast, because it reads only the image headers quickly. Currently
* it supports JPEG, PNG, GIF and BMP. For other file formats, it returns -6.
* (For JPEG it may have to read 30 kB of data to find the SOF segment.)
*
* As a side effect, reads sequentially from f.
*/
static int get_image_dimensions_fast(
FILE *f, gint *w_out, gint *h_out) {
/* We could read the beginning of the image file to memory, but we'd need
* at least 30 kB of memory for JPEG files, because they have the SOF0 (C0)
* marker after comments. So we don't do that, but we use getc instead.
*/
int c, m;
unsigned ss, i;
char head[26];
for (i = 0; i < 4; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated in header. */
head[i] = c;
}
if (0 == memcmp(head, "\xff\xd8\xff", 3)) { /* JPEG. */
/* A typical JPEG file has markers in these order:
* d8 e0_JFIF e1 e1 e2 db db fe fe c0 c4 c4 c4 c4 da d9.
* The first fe marker (COM, comment) was near offset 30000.
* A typical JPEG file after filtering through jpegtran:
* d8 e0_JFIF fe fe db db c0 c4 c4 c4 c4 da d9.
* The first fe marker (COM, comment) was at offset 20.
*/
m = head[3];
goto start_jpeg;
for (;;) {
/* printf("@%ld\n", ftell(f)); */
if ((c = getc(f)) < 0) return -2; /* Truncated. */
if (c != 0xff) return -3; /* Not a JPEG file, marker expected. */
read_m:
if ((m = getc(f)) < 0) return -2; /* Truncated. */
start_jpeg:
while (m == 0xff) { /* Padding. */
if ((m = getc(f)) < 0) return -2; /* Truncated. */
}
if (m == 0xd8) return -4; /* SOI unexpected. */
if (m == 0xd9) return -8; /* EOI unexpected before SOF. */
if (m == 0xda) return -9; /* SOS unexpected before SOF. */
/* printf("MARKER 0x%02x\n", m); */
if ((c = getc(f)) < 0) return -2; /* Truncated. */
ss = (c + 0U) << 8;
if ((c = getc(f)) < 0) return -2; /* Truncated. */
ss += c;
if (ss < 2) return -5; /* Segment too short. */
/* SOF0 ... SOF15. */
if (0xc0 <= m && m <= 0xcf && m != 0xc4 && m != 0xc8 && m != 0xcc) {
ss -= 2;
if (ss < 5) return -7; /* SOF segment too short. */
for (i = 0; i < 5; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
head[i] = c;
}
*w_out = (unsigned char)head[3] << 8 | (unsigned char)head[4];
*h_out = (unsigned char)head[1] << 8 | (unsigned char)head[2];
return 0;
} else {
for (ss -= 2; ss > 0; --ss) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
}
if (m == 0xfe) { /* After COM segment. */
if ((c = getc(f)) < 0) return -2; /* Truncated. */
if (c == 0xff) goto read_m;
/* Some buggy JPEG encoders add ? or \0\0 after the 0xfe (COM)
* marker. We will ignore those extra bytes.
*/
if ((m = getc(f)) < 0) return -2; /* Truncated. */
if (m == 0xff) goto start_jpeg; /* Skip extra byte. */
if (c != 0 || m != 0) return -11; /* Unexpected byte after extra byte. */
/* Skip 2 extra NUL bytes (c and m). */
}
}
}
return -10; /* Internal error, shouldn't be reached. */
} else if (0 == memcmp(head, "BM", 2)) { /* BMP. */
for (; i < 26; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
head[i] = c;
}
switch ((unsigned char)head[14]) {
case 12: case 64:
*w_out = (unsigned char)head[18] | (unsigned char)head[19] << 8;
*h_out = (unsigned char)head[20] | (unsigned char)head[21] << 8;
break;
case 40: case 124:
*w_out = (unsigned char)head[18] | (unsigned char)head[19] << 8 |
(unsigned char)head[20] << 16 | (unsigned char)head[21] << 24;
*h_out = (unsigned char)head[22] | (unsigned char)head[23] << 8 |
(unsigned char)head[24] << 16 | (unsigned char)head[25] << 8;
break;
default: /* case 0x7C: BMP with colorspace info. */
return -12; /* Unrecognized BMP. */
}
return 0;
} else if (0 == memcmp(head, "GIF8", 4)) { /* GIF. */
for (; i < 10; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
head[i] = c;
}
if (0 != memcmp(head, "GIF87a", 6) && 0 != memcmp(head, "GIF89a", 6)) {
return -13; /* Invalid GIF header. */
}
*w_out = (unsigned char)head[6] | (unsigned char)head[7] << 8;
*h_out = (unsigned char)head[8] | (unsigned char)head[9] << 8;
return 0;
} else if (0 == memcmp(head, "\211PNG", 4)) { /* GIF. */
for (; i < 24; ++i) {
if ((c = getc(f)) < 0) return -2; /* Truncated. */
head[i] = c;
}
if ((unsigned char)head[11] - 015U > 062U) {
return -14; /* Invalid PNG header. */
}
head[11] = 0;
if (0 != memcmp(head, "\211PNG\r\n\032\n\0\0\0\0IHDR", 16)) {
return -14; /* Invalid PNG header. */
}
*w_out = (unsigned char)head[19] | (unsigned char)head[18] << 8 |
(unsigned char)head[17] << 16 | (unsigned char)head[16] << 24;
*h_out = (unsigned char)head[23] | (unsigned char)head[22] << 8 |
(unsigned char)head[21] << 16 | (unsigned char)head[20] << 8;
return 0;
}
return -6; /* Unrecognized file format. */
}
/* Populates *w_out and *h_out with the dimensions of the image file
* specified in image_name. On an error, sets *w_out = *h_out = -1.
*
* If th_image_name is not NULL, then before reading image_name, it tries to
* get the dimensions of the real image from the REALDIMEN: comment in the
* thumbnail image th_image_name.
*
* It's usually quite fast, because it reads only the image headers (with
* imlib_load_image, or with a custom, faster code for JPEG files).
*
* As a side effect, may destroy the image in the Imlib2 image context (by
* calling imlib_free_image()).
*/
static void get_image_dimensions(
const char *image_name, const char *th_image_name,
gint *w_out, gint *h_out) {
Imlib_Image *im_orig;
FILE *f;
if (th_image_name && (f = fopen(th_image_name, "rb")) != NULL) {
if (get_real_dimensions_fast(f, w_out, h_out) == 0) { fclose(f); return; }
fclose(f);
}
if ((f = fopen(image_name, "rb")) != NULL) {
/* Faster than imlib_load_image on same (rare) JPEG files. */
if (get_image_dimensions_fast(f, w_out, h_out) == 0) { fclose(f); return; }
fclose(f);
}
if ((im_orig = imlib_load_image(image_name)) != NULL) {
imlib_context_set_image(im_orig);
*w_out = imlib_image_get_width();
*h_out = imlib_image_get_height();
imlib_free_image();
} else {
*w_out = *h_out = -1;
}
}
static void update_image_on_error(qiv_image *q);
/*
* Load & display image
*/
void qiv_load_image(qiv_image *q) {
/* Don't initialize most variables here, initialize them after load_next_image: */
struct stat st;
const char *image_name;
Imlib_Image *im;
struct timeval load_before, load_after;
char is_stat_ok;
/* Used to omit slow disk operations if image_file doesn't exist or isn't
* a file.
*/
char is_maybe_image_file;
char is_first_error = 1;
load_next_image:
is_stat_ok = 0;
is_maybe_image_file = 1;
image_name = image_names[image_idx];
gettimeofday(&load_before, 0);
if (imlib_context_get_image())
imlib_free_image();
q->real_w = q->real_h = -2;
q->has_thumbnail = FALSE;
if (!do_omit_load_stat) {
is_stat_ok = 0 == stat(image_name, &st);
is_maybe_image_file = is_stat_ok && S_ISREG(st.st_mode);
}
current_mtime = is_stat_ok ? st.st_mtime : 0;
im = NULL;
if (thumbnail && fullscreen && (is_stat_ok || maxpect)) {
char *th_image_name =
is_maybe_image_file ?
get_thumbnail_filename(image_name, &is_maybe_image_file) : NULL;
if (th_image_name) {
im = imlib_load_image(th_image_name);
if (im && maxpect) {
get_image_dimensions(image_name, th_image_name,
&q->real_w, &q->real_h);
}
free(th_image_name);
th_image_name = NULL;
}
}
if (im) { /* We have a dumb thumbnail in im. */
if (maxpect) {
current_mtime = 0;
q->has_thumbnail = TRUE;
/* Now im still has the thumbnail image. Keep it. */
} else { /* Use the real, non-thumbnail image instead. */
imlib_context_set_image(im);
imlib_free_image();
im = is_maybe_image_file ? imlib_load_image((char*)image_name) : NULL;
}
} else {
im = is_maybe_image_file ? imlib_load_image((char*)image_name) : NULL;
}
if (!im) { /* error */
q->error = 1;
q->orig_w = 400;
q->orig_h = 300;
if (to_root || to_root_t || to_root_s) {
fprintf(stderr, "qiv: cannot load background_image\n");
qiv_exit(1);
}
/* Shortcut to speed up lots of subsequent load failures. */
if (is_first_error) {
check_size(q, TRUE);
if (first) {
setup_win(q, &error_bg);
first = 0;
}
gdk_flush();
gdk_beep();
is_first_error = 0;
}
/* TODO(pts): Avoid the slow loop of copying pointers around in update_image_on_error. */
update_image_on_error(q);
/* This is a shortcut to avoid stack overflow in the recursion of
* qiv_load_image -> update_image -> qiv_load_image -> update_image -> ...
* if there are errors loading many subsequent images.
*/
goto load_next_image;
}
if (thumbnail && !q->has_thumbnail && q->real_w < 0 && is_maybe_image_file) {
FILE *f = fopen(image_name, "rb");
if (f) {
get_real_dimensions_fast(f, &q->real_w, &q->real_h);
fclose(f);
}
}
/* Retrieve image properties */
imlib_context_set_image(im);
q->error = 0;
q->orig_w = imlib_image_get_width();
q->orig_h = imlib_image_get_height();
if (q->orig_w >= (1 << 23) / q->orig_h) {
/* Workaround for Imlib2 1.4.6 on Ubuntu Trusty: PNG images with an
* alpha channel and pixel count >= (1 << 23) are displayed as black.
* imlib_image_query_pixel returns the correct value, but
* imlib_render_pixmaps_for_whole_image_at_size renders only black
* pixels if unzoomed.
*/
Imlib_Color c;
/* Without this call, imlib_image_set_has_alpha(0) is too early, and
* it has no effect.
*/
imlib_image_query_pixel(0, 0, &c);
imlib_image_set_has_alpha(0);
}
#ifdef HAVE_EXIF
if (autorotate) {
transform( q, orient( image_name));
}
#endif
check_size(q, TRUE);
gettimeofday(&load_after, 0);
/* load_elapsed used by update_image. */
load_elapsed = ((load_after.tv_sec + load_after.tv_usec / 1.0e6) -
(load_before.tv_sec + load_before.tv_usec / 1.0e6));
if (first) {
setup_win(q, &image_bg);
first = 0;
}
/* desktop-background -> exit */
if (to_root || to_root_t || to_root_s) {
set_desktop_image(q);
if(slide)
return;
else
qiv_exit(0);
}
update_image(q, REDRAW);
// if (magnify && !fullscreen) { // [lc]
// setup_magnify(q, &magnify_img);
// update_magnify(q, &magnify_img, FULL_REDRAW, 0, 0);
// }
}
static gchar blank_cursor[1];
static void setup_imlib_for_drawable(GdkDrawable * d)
{
imlib_context_set_dither(1); /* dither for depths < 24bpp */
imlib_context_set_display(
gdk_x11_drawable_get_xdisplay(d));
imlib_context_set_visual(
gdk_x11_visual_get_xvisual(gdk_drawable_get_visual(d)));
imlib_context_set_colormap(
gdk_x11_colormap_get_xcolormap(gdk_drawable_get_colormap(d)));
imlib_context_set_drawable(
gdk_x11_drawable_get_xid(d));
}
static void setup_imlib_color_modifier(qiv_color_modifier q)
{
if (q.gamma == DEFAULT_GAMMA &&
q.brightness == DEFAULT_BRIGHTNESS &&
q.contrast == DEFAULT_CONTRAST) {
if (imlib_context_get_color_modifier())
imlib_free_color_modifier();
return;
}
if (imlib_context_get_color_modifier())
imlib_reset_color_modifier();
else
imlib_context_set_color_modifier(imlib_create_color_modifier());
imlib_modify_color_modifier_gamma(q.gamma / 256.0);
imlib_modify_color_modifier_brightness((q.brightness - 256) / 256.0);
imlib_modify_color_modifier_contrast(q.contrast / 256.0);
}
static void setup_win(qiv_image *q, GdkColor *win_bg)
{
GdkWindowAttr attr;
/* To get gdk_window_clear called in update_window. It's also important
* when toggling fullscreen.
*/
q->win_ow = q->win_oh = -1;
if (!fullscreen) {
attr.window_type=GDK_WINDOW_TOPLEVEL;
attr.wclass=GDK_INPUT_OUTPUT;
attr.event_mask=GDK_ALL_EVENTS_MASK;
attr.x = center ? q->win_x : 0;
attr.y = center ? q->win_y : 0;
attr.width = q->win_w;
attr.height = q->win_h;
q->win = gdk_window_new(NULL, &attr, GDK_WA_X|GDK_WA_Y);
if (center) {
GdkGeometry geometry = {
.min_width = q->win_w,
.min_height = q->win_h,
.max_width = q->win_w,
.max_height = q->win_h,
.win_gravity = GDK_GRAVITY_STATIC
};
gdk_window_set_geometry_hints(q->win, &geometry,
GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_WIN_GRAVITY);
gdk_window_move_resize(q->win, q->win_x, q->win_y, q->win_w, q->win_h);
} else {
GdkGeometry geometry = {
.min_width = q->win_w,
.min_height = q->win_h,
.max_width = q->win_w,
.max_height = q->win_h,
};
gdk_window_set_geometry_hints(q->win, &geometry,
GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
gdk_window_resize(q->win, q->win_w, q->win_h);
}
} else { /* fullscreen */
attr.window_type = disable_grab ? GDK_WINDOW_TOPLEVEL : GDK_WINDOW_TEMP;
attr.wclass=GDK_INPUT_OUTPUT;
attr.event_mask=GDK_ALL_EVENTS_MASK;
attr.x = attr.y = 0;
attr.width=screen_x;
attr.height=screen_y;
q->win = gdk_window_new(NULL, &attr, GDK_WA_X|GDK_WA_Y);
gdk_window_set_cursor(q->win, cursor);
if (disable_grab) {
const GdkGeometry geometry = {
.min_width = screen_x,
.min_height = screen_y,
.max_width = screen_x,
.max_height = screen_y,
.win_gravity = GDK_GRAVITY_STATIC
};
gdk_window_set_decorations(q->win, 0); /* GDK_DECOR_... */
gdk_window_set_functions(q->win, GDK_FUNC_MINIMIZE); /* GDK_FUNC_... */
gdk_window_set_geometry_hints(q->win, &geometry,
GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_WIN_GRAVITY);
gdk_window_move_resize(q->win, 0, 0, screen_x, screen_y);
/* Even without the gdk_window_fullscreen and gdk_window_raise lines
* below, it (qiv -f -G) works in Ubuntu 14.04 gnome-fallback, and it
* covers the gnome-panel.
*
* However, even with these calls, it just almost works in Ubuntu 18.04
* gnome-fallback, because it doesn't cover the gnome-panel (but draws
* some of the image behind the panel). gedit(1) in Ubuntu 18.04 just
* works (it covers the panel) by calling gtk_window_fullscreen
* (in GTK3). What is the difference? Is it GTK2 vs GTK3? Or is it GDK
* vs GTK?
*
* Please note that focus passing from a fullscreen window in
* gnome-panel in Ubuntu 18.04 is buggy: it transfers focus at Alt-<Tab>,
* but it doesn't expose the target window.
*/
gdk_window_fullscreen(q->win);
gdk_window_raise(q->win);
gdk_window_fullscreen(q->win);
}
}
gdk_window_set_background(q->win, win_bg);
q->bg_gc = gdk_gc_new(q->win);
q->text_gc = gdk_gc_new(q->win); /* black is default */
q->status_gc = gdk_gc_new(q->win);
gdk_gc_set_foreground(q->bg_gc, &image_bg);
gdk_gc_set_foreground(q->status_gc, &text_bg);
{
GdkPixmap *cursor_pixmap = gdk_bitmap_create_from_data(q->win, blank_cursor, 1, 1);
invisible_cursor = gdk_cursor_new_from_pixmap(cursor_pixmap, cursor_pixmap,
&text_bg, &text_bg, 0, 0);
gdk_bitmap_unref(cursor_pixmap); /* invisible_cursor holds a reference. */
}
if (!visible_cursor) visible_cursor = gdk_cursor_new(CURSOR);
cursor = NULL;
setup_imlib_for_drawable(GDK_DRAWABLE(q->win));
gdk_window_show(q->win);
if (do_grab || (fullscreen && !disable_grab) ) {
/* These must be called after gdk_window_show. */
gdk_keyboard_grab(q->win, FALSE, CurrentTime);
gdk_pointer_grab(q->win, FALSE,
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK |
GDK_LEAVE_NOTIFY_MASK | GDK_POINTER_MOTION_MASK, NULL, NULL, CurrentTime);
}
show_cursor(q);
}
void hide_cursor(qiv_image *q)
{
if (cursor != invisible_cursor)
gdk_window_set_cursor(q->win, cursor = invisible_cursor);
}
void show_cursor(qiv_image *q)
{
if (cursor != visible_cursor)
gdk_window_set_cursor(q->win, cursor = visible_cursor);
}
/* set image as background */
void set_desktop_image(qiv_image *q)
{
GdkWindow *root_win = gdk_get_default_root_window();
GdkVisual *gvis = gdk_drawable_get_visual(root_win);
GdkPixmap *temp;
gchar *buffer;
gint root_w = screen_x, root_h = screen_y;
gint root_x = 0, root_y = 0;
Pixmap x_pixmap, x_mask;
if (to_root || to_root_t) {
root_w = q->win_w;
root_h = q->win_h;
}
if (to_root) {
root_x = (screen_x - root_w) / 2;
root_y = (screen_y - root_h) / 2;
}
setup_imlib_for_drawable(GDK_DRAWABLE(root_win));
imlib_render_pixmaps_for_whole_image_at_size(&x_pixmap, &x_mask, root_w, root_h);
#ifdef DEBUG
if (x_mask) g_print("*** image has transparency\n");
#endif
if(x_pixmap) {
GdkPixmap * p = gdk_pixmap_foreign_new(x_pixmap);
gdk_drawable_set_colormap(GDK_DRAWABLE(p),
gdk_drawable_get_colormap(GDK_DRAWABLE(root_win)));
if (to_root_t) {
gdk_window_set_back_pixmap(root_win, p, FALSE);
} else {
GdkGC *rootGC;
buffer = xcalloc(1, screen_x * screen_y * gvis->depth / 8);
rootGC = gdk_gc_new(root_win);
temp = gdk_pixmap_create_from_data(root_win, buffer, screen_x,
screen_y, gvis->depth, &image_bg, &image_bg);
gdk_drawable_set_colormap(GDK_DRAWABLE(temp),
gdk_drawable_get_colormap(GDK_DRAWABLE(root_win)));
gdk_draw_drawable(temp, rootGC, p, 0, 0, root_x, root_y, root_w, root_h);
gdk_window_set_back_pixmap(root_win, temp, FALSE);
g_object_unref(temp);
g_object_unref(rootGC);
free(buffer);
}
g_object_unref(p);
imlib_free_pixmap_and_mask(x_pixmap);
}
gdk_window_clear(root_win);
gdk_flush();
setup_imlib_for_drawable(q->win);
}
void zoom_in(qiv_image *q)
{
int zoom_percentage;
int w_old, h_old;
/* first compute current zoom_factor */
if (maxpect || scale_down || fixed_window_size) {
zoom_percentage=myround((1.0-(q->orig_w - q->win_w)/(double)q->orig_w)*100);
zoom_factor=(zoom_percentage - 100) / 10;
}
maxpect = scale_down = 0;
zoom_factor++;
w_old = q->win_w;
h_old = q->win_h;
q->win_w = (gint)(q->orig_w * (1 + zoom_factor * 0.1));
q->win_h = (gint)(q->orig_h * (1 + zoom_factor * 0.1));
/* adapt image position */
q->win_x -= (q->win_w - w_old) / 2;
q->win_y -= (q->win_h - h_old) / 2;
if (fullscreen) {
if (center)
center_image(q);
else
correct_image_position(q);
} else {
correct_image_position(q);
}
}
void zoom_out(qiv_image *q)
{
int zoom_percentage;
int w_old, h_old;
/* first compute current zoom_factor */
if (maxpect || scale_down || fixed_window_size) {
zoom_percentage=myround((1.0-(q->orig_w - q->win_w)/(double)q->orig_w)*100);
zoom_factor=(zoom_percentage - 100) / 10;
}
maxpect = scale_down = 0;
w_old = q->win_w;
h_old = q->win_h;
if(zoom_factor > -9 && q->win_w > MIN(64, q->orig_w) && q->win_h > MIN(64, q->orig_h)) {
zoom_factor--;
q->win_w = (gint)(q->orig_w * (1 + zoom_factor * 0.1));
q->win_h = (gint)(q->orig_h * (1 + zoom_factor * 0.1));
/* adapt image position */
q->win_x -= (q->win_w - w_old) / 2;
q->win_y -= (q->win_h - h_old) / 2;
if (fullscreen) {
if (center)
center_image(q);
else
correct_image_position(q);
} else {
correct_image_position(q);
}
} else {
q->infotext = ("(Cannot zoom out anymore)");
fprintf(stderr, "qiv: cannot zoom out anymore\n");
}
}
void zoom_maxpect(qiv_image *q)
{
#ifdef GTD_XINERAMA
double zx = (double)preferred_screen->width / (double)q->orig_w;
double zy = (double)preferred_screen->height / (double)q->orig_h;
#else
double zx = (double)screen_x / (double)q->orig_w;
double zy = (double)screen_y / (double)q->orig_h;
#endif
/* titlebar and frames ignored on purpose to use full height/width of screen */
q->win_w = (gint)(q->orig_w * MIN(zx, zy));
q->win_h = (gint)(q->orig_h * MIN(zx, zy));
center_image(q);
}
/*
Set display settings to startup values
which are used whenever a new image is loaded.
*/
void reload_image(qiv_image *q)
{
imlib_image_set_changes_on_disk();
Imlib_Image *im = imlib_load_image(image_names[image_idx]);
if (!im && watch_file)
return;
struct stat statbuf;
stat(image_names[image_idx], &statbuf);
current_mtime = statbuf.st_mtime;
if (imlib_context_get_image())
imlib_free_image();
if (!im)
{
q->error = 1;
q->orig_w = 400;
q->orig_h = 300;
} else { /* Retrieve image properties */
q->error = 0;
imlib_context_set_image(im);
q->orig_w = imlib_image_get_width();
q->orig_h = imlib_image_get_height();
}
q->win_w = (gint)(q->orig_w * (1 + zoom_factor * 0.1));
q->win_h = (gint)(q->orig_h * (1 + zoom_factor * 0.1));
reset_mod(q);
center_image(q);
}
void check_size(qiv_image *q, gint reset) {
/* printf("maxpect=%d thumbnail=%d scale_down=%d reset=%d\n", maxpect, thumbnail, scale_down, reset); */
if ((maxpect && !thumbnail) ||
(maxpect && thumbnail && (q->orig_w>screen_x || q->orig_h>screen_y)) ||
(scale_down && (q->orig_w>screen_x || q->orig_h>screen_y))) {
zoom_maxpect(q);
} else if (reset || (scale_down && (q->win_w<q->orig_w || q->win_h<q->orig_h))) {
reset_coords(q);
}
if (center) center_image(q);
}
void reset_coords(qiv_image *q)
{
if (fixed_window_size) {
double w_o_ratio = (double)(fixed_window_size) / q->orig_w;
q->win_w = fixed_window_size;
q->win_h = q->orig_h * w_o_ratio;
} else {
if (fixed_zoom_factor) {
zoom_factor = fixed_zoom_factor; /* reset zoom */
}
q->win_w = (gint)(q->orig_w * (1 + zoom_factor * 0.1));
q->win_h = (gint)(q->orig_h * (1 + zoom_factor * 0.1));
}
}
static void update_win_title(qiv_image *q) {
char dimen_msg[sizeof(gint) * 6 + 8];
char elapsed_msg[16];
if (isnan(q->elapsed)) {
*elapsed_msg = '\0';
} else {
g_snprintf(elapsed_msg, sizeof elapsed_msg, "%1.03fs ", q->elapsed);
}
if (q->real_w == -1) {
/* Showing thumbnail, real image missing or unloadable. */
strcpy(dimen_msg, "(?x?)--");
} else {
g_snprintf(dimen_msg, sizeof dimen_msg, "(%dx%d)%s",
q->real_w >= 0 ? q->real_w : q->orig_w,
q->real_h >= 0 ? q->real_h : q->orig_h,
q->real_w >= 0 && q->real_h >= 0 ?
(q->has_thumbnail ? "-" : "%") :
thumbnail && !maxpect ? "+" : "");
}
g_snprintf(q->win_title_no_infotext, sizeof q->win_title_no_infotext,
"qiv: %s %s %s%d%% [%d/%d] b%d/c%d/g%d ",
image_names[image_idx], dimen_msg, elapsed_msg,
myround((1.0-(q->orig_w - q->win_w)/(double)q->orig_w)*100), image_idx+1, images,
q->mod.brightness/8-32, q->mod.contrast/8-32, q->mod.gamma/8-32);
}
static void update_image_on_error(qiv_image *q) {
int i;
if (!q->error) return; /* Unexpected. */
g_snprintf(q->win_title_no_infotext, sizeof q->win_title_no_infotext,
"qiv: ERROR! cannot load image: %s", image_names[image_idx]);
q->infotext = NULL;
gdk_beep();
/* take this image out of the file list */
--images;
for(i=image_idx;i<images;++i) {
image_names[i] = image_names[i+1];
}
/* If deleting the last file out of x */
if(images == image_idx)
image_idx = 0;
/* If deleting the only file left */
if(!images) {
#ifdef DEBUG
g_print("*** deleted last file in list. Exiting.\n");
#endif
exit(0);
}
/* The caller should continue with the call to: qiv_load_image(q); */
}
#define MMIN(a, b) ((a) < (b) ? (a) : (b))
#define MMAX(a, b) ((a) > (b) ? (a) : (b))
/* Redraws the rectangle (x, y, x + w, y + h) in the main window, with portions of the image, or with the background (q->bg_gc), or with a combination of the two. */
static void draw_image_or_background(qiv_image *q, gint x, gint y, gint w, gint h) {
if (w > 0 && h > 0 && !q->error) {
if (x < 0) { w += x; x = 0; }
if (y < 0) { h += y; y = 0; }
if (w > 0 && h > 0) {
const gint ix = fullscreen ? q->win_x : 0, iy = fullscreen ? q->win_y : 0, iw = q->win_w, ih = q->win_h;
const gint sx = MMAX(x, ix), sy = MMAX(y, iy), sw = MMIN(x + w, ix + iw) - sx, sh = MMIN(y + h, iy + ih) - sy; /* Calculate intersection. */
if (sw > 0 && sh > 0) { /* Draw intersection (from image) and portions of background (from q->bg-gc). */
/* We draw 5 regions: Top, Left, interSection, Right, Bottom:
* TTTTT
* LSSSR
* BBBBB
*/
gdk_draw_drawable(q->win, q->bg_gc, q->p, sx - ix, sy - iy, sx, sy, sw, sh); /* S. */
if (sy > y) gdk_draw_rectangle(q->win, q->bg_gc, 1, x, y, w, sy - y); /* T. */
if (sx > x) gdk_draw_rectangle(q->win, q->bg_gc, 1, x, sy, sx - x, sh); /* L. */
if (x + w > sx + sw) gdk_draw_rectangle(q->win, q->bg_gc, 1, sx + sw, sy, x + w - sx - sw, sh); /* R. */
if (y + h > sy + sh) gdk_draw_rectangle(q->win, q->bg_gc, 1, x, sy + sh, w, y + h - sy - sh); /* B. */
#if DEBUG
if (sy > y) putc('T', stderr);
if (sx > x) putc('L', stderr);
if (x + w > sx + sw) putc('R', stderr);
if (y + h > sy + sh) putc('B', stderr);
putc('.', stderr);