Krolyxon
Commit 515e410

Branches

Refs →

nsxivimage.c

715 lines · 15.0 KB · C

1/* sxiv: image.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20#define _FEATURE_CONFIG
21#define _IMAGE_CONFIG
22
23#include <string.h>
24#include <unistd.h>
25
26#include "image.h"
27#include "options.h"
28#include "util.h"
29#include "config.h"
30
31#if EXIF_SUPPORT
32#include <libexif/exif-data.h>
33#endif
34
35#if GIF_SUPPORT
36#include <stdlib.h>
37#include <sys/types.h>
38#include <gif_lib.h>
39#endif
40
41#define ZOOMDIFF(z1,z2) ((z1) - (z2) > 0.001 || (z1) - (z2) < -0.001)
42
43enum { MIN_GIF_DELAY = 50 };
44
45float zoom_min;
46float zoom_max;
47
48void img_init(img_t *img, win_t *win) {
49 zoom_min = zoom_levels[0] / 100.0;
50 zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
51
52 if (!img || !win)
53 return;
54
55 imlib_context_set_display(win->env.dpy);
56 imlib_context_set_visual(win->env.vis);
57 imlib_context_set_colormap(win->env.cmap);
58
59 img->im = NULL;
60 img->win = win;
61 img->zoom = options->zoom;
62 img->zoom = MAX(img->zoom, zoom_min);
63 img->zoom = MIN(img->zoom, zoom_max);
64 img->checkpan = false;
65 img->dirty = false;
66 img->aa = options->aa;
67 img->alpha = true;
68 img->slideshow = false;
69 img->ss_delay = SLIDESHOW_DELAY * 1000;
70 img->multi.cap = img->multi.cnt = 0;
71 img->multi.animate = false;
72}
73
74#if EXIF_SUPPORT
75void exif_auto_orientate(const fileinfo_t *file) {
76 ExifData *ed;
77 ExifEntry *entry;
78 int byte_order, orientation;
79
80 if (!(ed = exif_data_new_from_file(file->path)))
81 return;
82 entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
83 if (entry) {
84 byte_order = exif_data_get_byte_order(ed);
85 orientation = exif_get_short(entry->data, byte_order);
86 }
87 exif_data_unref(ed);
88 if (!entry)
89 return;
90
91 switch (orientation) {
92 case 5:
93 imlib_image_orientate(1);
94 case 2:
95 imlib_image_flip_vertical();
96 break;
97
98 case 3:
99 imlib_image_orientate(2);
100 break;
101
102 case 7:
103 imlib_image_orientate(1);
104 case 4:
105 imlib_image_flip_horizontal();
106 break;
107
108 case 6:
109 imlib_image_orientate(1);
110 break;
111
112 case 8:
113 imlib_image_orientate(3);
114 break;
115 }
116}
117#endif /* EXIF_SUPPORT */
118
119#if GIF_SUPPORT
120/* Originally based on, but in its current form merely inspired by Imlib2's
121 * src/modules/loaders/loader_gif.c:load(), written by Carsten Haitzler.
122 */
123bool img_load_gif(img_t *img, const fileinfo_t *file) {
124 GifFileType *gif;
125 GifRowType *rows = NULL;
126 GifRecordType rec;
127 ColorMapObject *cmap;
128 DATA32 bgpixel, *data, *ptr;
129 DATA32 *prev_frame = NULL;
130 Imlib_Image *im;
131 int i, j, bg, r, g, b;
132 int x, y, w, h, sw, sh;
133 int intoffset[] = { 0, 4, 2, 1 };
134 int intjump[] = { 8, 8, 4, 2 };
135 int transp = -1;
136 unsigned int delay = 0;
137 bool err = false;
138
139 if (img->multi.cap == 0) {
140 img->multi.cap = 8;
141 img->multi.frames = (img_frame_t*)
142 s_malloc(sizeof(img_frame_t) * img->multi.cap);
143 }
144 img->multi.cnt = 0;
145 img->multi.sel = 0;
146
147 gif = DGifOpenFileName(file->path);
148 if (!gif) {
149 warn("could not open gif file: %s", file->name);
150 return false;
151 }
152 bg = gif->SBackGroundColor;
153 sw = gif->SWidth;
154 sh = gif->SHeight;
155
156 do {
157 if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
158 err = true;
159 break;
160 }
161 if (rec == EXTENSION_RECORD_TYPE) {
162 int ext_code;
163 GifByteType *ext = NULL;
164
165 DGifGetExtension(gif, &ext_code, &ext);
166 while (ext) {
167 if (ext_code == 0xf9) {
168 if (ext[1] & 1)
169 transp = (int) ext[4];
170 else
171 transp = -1;
172
173 delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]);
174 if (delay)
175 delay = MAX(delay, MIN_GIF_DELAY);
176 }
177 ext = NULL;
178 DGifGetExtensionNext(gif, &ext);
179 }
180 } else if (rec == IMAGE_DESC_RECORD_TYPE) {
181 if (DGifGetImageDesc(gif) == GIF_ERROR) {
182 err = true;
183 break;
184 }
185 x = gif->Image.Left;
186 y = gif->Image.Top;
187 w = gif->Image.Width;
188 h = gif->Image.Height;
189
190 rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
191 for (i = 0; i < h; i++)
192 rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
193 if (gif->Image.Interlace) {
194 for (i = 0; i < 4; i++) {
195 for (j = intoffset[i]; j < h; j += intjump[i])
196 DGifGetLine(gif, rows[j], w);
197 }
198 } else {
199 for (i = 0; i < h; i++)
200 DGifGetLine(gif, rows[i], w);
201 }
202
203 ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
204 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
205 r = cmap->Colors[bg].Red;
206 g = cmap->Colors[bg].Green;
207 b = cmap->Colors[bg].Blue;
208 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
209
210 for (i = 0; i < sh; i++) {
211 for (j = 0; j < sw; j++) {
212 if (i < y || i >= y + h || j < x || j >= x + w) {
213 if (transp >= 0 && prev_frame)
214 *ptr = prev_frame[i * sw + j];
215 else
216 *ptr = bgpixel;
217 } else if (rows[i-y][j-x] == transp) {
218 if (prev_frame)
219 *ptr = prev_frame[i * sw + j];
220 else
221 *ptr = bgpixel;
222 } else {
223 r = cmap->Colors[rows[i-y][j-x]].Red;
224 g = cmap->Colors[rows[i-y][j-x]].Green;
225 b = cmap->Colors[rows[i-y][j-x]].Blue;
226 *ptr = 0xff << 24 | r << 16 | g << 8 | b;
227 }
228 ptr++;
229 }
230 }
231
232 im = imlib_create_image_using_copied_data(sw, sh, data);
233
234 for (i = 0; i < h; i++)
235 free(rows[i]);
236 free(rows);
237 free(data);
238
239 if (!im) {
240 err = true;
241 break;
242 }
243
244 imlib_context_set_image(im);
245 prev_frame = imlib_image_get_data_for_reading_only();
246
247 imlib_image_set_format("gif");
248 if (transp >= 0)
249 imlib_image_set_has_alpha(1);
250
251 if (img->multi.cnt == img->multi.cap) {
252 img->multi.cap *= 2;
253 img->multi.frames = (img_frame_t*)
254 s_realloc(img->multi.frames,
255 img->multi.cap * sizeof(img_frame_t));
256 }
257 img->multi.frames[img->multi.cnt].im = im;
258 img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY;
259 img->multi.cnt++;
260 }
261 } while (rec != TERMINATE_RECORD_TYPE);
262
263 DGifCloseFile(gif);
264
265 if (err && !file->loaded)
266 warn("corrupted gif file: %s", file->name);
267
268 if (img->multi.cnt > 1) {
269 imlib_context_set_image(img->im);
270 imlib_free_image();
271 img->im = img->multi.frames[0].im;
272 img->multi.animate = GIF_AUTOPLAY;
273 } else if (img->multi.cnt == 1) {
274 imlib_context_set_image(img->multi.frames[0].im);
275 imlib_free_image();
276 img->multi.cnt = 0;
277 img->multi.animate = false;
278 }
279
280 imlib_context_set_image(img->im);
281
282 return !err;
283}
284#endif /* GIF_SUPPORT */
285
286bool img_load(img_t *img, const fileinfo_t *file) {
287 const char *fmt;
288
289 if (!img || !file || !file->name || !file->path)
290 return false;
291
292 if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) {
293 warn("could not open image: %s", file->name);
294 return false;
295 }
296
297 imlib_context_set_image(img->im);
298 imlib_image_set_changes_on_disk();
299 imlib_context_set_anti_alias(img->aa);
300
301 fmt = imlib_image_format();
302 /* avoid unused-but-set-variable warning */
303 (void) fmt;
304
305#if EXIF_SUPPORT
306 if (!strcmp(fmt, "jpeg"))
307 exif_auto_orientate(file);
308#endif
309#if GIF_SUPPORT
310 if (!strcmp(fmt, "gif"))
311 img_load_gif(img, file);
312#endif
313
314 img->w = imlib_image_get_width();
315 img->h = imlib_image_get_height();
316 img->scalemode = options->scalemode;
317 img->re = false;
318 img->checkpan = false;
319 img->dirty = true;
320
321 return true;
322}
323
324void img_close(img_t *img, bool decache) {
325 int i;
326
327 if (!img)
328 return;
329
330 if (img->multi.cnt) {
331 for (i = 0; i < img->multi.cnt; i++) {
332 imlib_context_set_image(img->multi.frames[i].im);
333 imlib_free_image();
334 }
335 img->multi.cnt = 0;
336 img->im = NULL;
337 } else if (img->im) {
338 imlib_context_set_image(img->im);
339 if (decache)
340 imlib_free_image_and_decache();
341 else
342 imlib_free_image();
343 img->im = NULL;
344 }
345}
346
347void img_check_pan(img_t *img, bool moved) {
348 win_t *win;
349 int ox, oy;
350
351 if (!img || !img->im || !img->win)
352 return;
353
354 win = img->win;
355 ox = img->x;
356 oy = img->y;
357
358 if (img->w * img->zoom > win->w) {
359 if (img->x > 0 && img->x + img->w * img->zoom > win->w)
360 img->x = 0;
361 if (img->x < 0 && img->x + img->w * img->zoom < win->w)
362 img->x = win->w - img->w * img->zoom;
363 } else {
364 img->x = (win->w - img->w * img->zoom) / 2;
365 }
366 if (img->h * img->zoom > win->h) {
367 if (img->y > 0 && img->y + img->h * img->zoom > win->h)
368 img->y = 0;
369 if (img->y < 0 && img->y + img->h * img->zoom < win->h)
370 img->y = win->h - img->h * img->zoom;
371 } else {
372 img->y = (win->h - img->h * img->zoom) / 2;
373 }
374
375 if (!moved && (ox != img->x || oy != img->y))
376 img->dirty = true;
377}
378
379bool img_fit(img_t *img) {
380 float z, zmax, zw, zh;
381
382 if (!img || !img->im || !img->win)
383 return false;
384 if (img->scalemode == SCALE_ZOOM)
385 return false;
386
387 zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
388 zw = (float) img->win->w / (float) img->w;
389 zh = (float) img->win->h / (float) img->h;
390
391 z = MIN(zw, zh);
392 z = MAX(z, zoom_min);
393 z = MIN(z, zmax);
394
395 if (ZOOMDIFF(z, img->zoom)) {
396 img->zoom = z;
397 img->dirty = true;
398 return true;
399 } else {
400 return false;
401 }
402}
403
404void img_render(img_t *img) {
405 win_t *win;
406 int sx, sy, sw, sh;
407 int dx, dy, dw, dh;
408
409 if (!img || !img->im || !img->win)
410 return;
411
412 win = img->win;
413 img_fit(img);
414
415 if (!img->re) {
416 /* rendered for the first time */
417 img->re = true;
418 if (img->zoom * img->w <= win->w)
419 img->x = (win->w - img->w * img->zoom) / 2;
420 else
421 img->x = 0;
422 if (img->zoom * img->h <= win->h)
423 img->y = (win->h - img->h * img->zoom) / 2;
424 else
425 img->y = 0;
426 }
427
428 if (img->checkpan) {
429 img_check_pan(img, false);
430 img->checkpan = false;
431 }
432
433 if (!img->dirty)
434 return;
435
436 /* calculate source and destination offsets */
437 if (img->x < 0) {
438 sx = -img->x / img->zoom;
439 sw = win->w / img->zoom;
440 dx = 0;
441 dw = win->w;
442 } else {
443 sx = 0;
444 sw = img->w;
445 dx = img->x;
446 dw = img->w * img->zoom;
447 }
448 if (img->y < 0) {
449 sy = -img->y / img->zoom;
450 sh = win->h / img->zoom;
451 dy = 0;
452 dh = win->h;
453 } else {
454 sy = 0;
455 sh = img->h;
456 dy = img->y;
457 dh = img->h * img->zoom;
458 }
459
460 win_clear(win);
461
462 imlib_context_set_image(img->im);
463
464 if (imlib_image_has_alpha() && !img->alpha)
465 win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
466
467 imlib_context_set_drawable(win->pm);
468 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
469
470 win_draw(win);
471
472 img->dirty = false;
473}
474
475bool img_fit_win(img_t *img) {
476 if (!img || !img->im)
477 return false;
478
479 img->scalemode = SCALE_FIT;
480 return img_fit(img);
481}
482
483bool img_center(img_t *img) {
484 int ox, oy;
485
486 if (!img || !img->im || !img->win)
487 return false;
488
489 ox = img->x;
490 oy = img->y;
491
492 img->x = (img->win->w - img->w * img->zoom) / 2;
493 img->y = (img->win->h - img->h * img->zoom) / 2;
494
495 if (ox != img->x || oy != img->y) {
496 img->dirty = true;
497 return true;
498 } else {
499 return false;
500 }
501}
502
503bool img_zoom(img_t *img, float z) {
504 if (!img || !img->im || !img->win)
505 return false;
506
507 z = MAX(z, zoom_min);
508 z = MIN(z, zoom_max);
509
510 img->scalemode = SCALE_ZOOM;
511
512 if (ZOOMDIFF(z, img->zoom)) {
513 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
514 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
515 img->zoom = z;
516 img->checkpan = true;
517 img->dirty = true;
518 return true;
519 } else {
520 return false;
521 }
522}
523
524bool img_zoom_in(img_t *img) {
525 int i;
526
527 if (!img || !img->im)
528 return false;
529
530 for (i = 1; i < ARRLEN(zoom_levels); i++) {
531 if (zoom_levels[i] > img->zoom * 100.0)
532 return img_zoom(img, zoom_levels[i] / 100.0);
533 }
534 return false;
535}
536
537bool img_zoom_out(img_t *img) {
538 int i;
539
540 if (!img || !img->im)
541 return false;
542
543 for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
544 if (zoom_levels[i] < img->zoom * 100.0)
545 return img_zoom(img, zoom_levels[i] / 100.0);
546 }
547 return false;
548}
549
550bool img_move(img_t *img, int dx, int dy) {
551 int ox, oy;
552
553 if (!img || !img->im)
554 return false;
555
556 ox = img->x;
557 oy = img->y;
558
559 img->x += dx;
560 img->y += dy;
561
562 img_check_pan(img, true);
563
564 if (ox != img->x || oy != img->y) {
565 img->dirty = true;
566 return true;
567 } else {
568 return false;
569 }
570}
571
572bool img_pan(img_t *img, direction_t dir, bool screen) {
573 if (!img || !img->im || !img->win)
574 return false;
575
576 switch (dir) {
577 case DIR_LEFT:
578 return img_move(img, img->win->w / (screen ? 1 : 5), 0);
579 case DIR_RIGHT:
580 return img_move(img, img->win->w / (screen ? 1 : 5) * -1, 0);
581 case DIR_UP:
582 return img_move(img, 0, img->win->h / (screen ? 1 : 5));
583 case DIR_DOWN:
584 return img_move(img, 0, img->win->h / (screen ? 1 : 5) * -1);
585 }
586 return false;
587}
588
589bool img_pan_edge(img_t *img, direction_t dir) {
590 int ox, oy;
591
592 if (!img || !img->im || !img->win)
593 return false;
594
595 ox = img->x;
596 oy = img->y;
597
598 switch (dir) {
599 case DIR_LEFT:
600 img->x = 0;
601 break;
602 case DIR_RIGHT:
603 img->x = img->win->w - img->w * img->zoom;
604 break;
605 case DIR_UP:
606 img->y = 0;
607 break;
608 case DIR_DOWN:
609 img->y = img->win->h - img->h * img->zoom;
610 break;
611 }
612
613 img_check_pan(img, true);
614
615 if (ox != img->x || oy != img->y) {
616 img->dirty = true;
617 return true;
618 } else {
619 return false;
620 }
621}
622
623void img_rotate(img_t *img, int d) {
624 win_t *win;
625 int ox, oy, tmp;
626
627 if (!img || !img->im || !img->win)
628 return;
629
630 win = img->win;
631 ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
632 oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
633
634 imlib_context_set_image(img->im);
635 imlib_image_orientate(d);
636
637 img->x = oy + (win->w - win->h) / 2;
638 img->y = ox + (win->h - win->w) / 2;
639
640 tmp = img->w;
641 img->w = img->h;
642 img->h = tmp;
643
644 img->checkpan = true;
645 img->dirty = true;
646}
647
648void img_rotate_left(img_t *img) {
649 img_rotate(img, 3);
650}
651
652void img_rotate_right(img_t *img) {
653 img_rotate(img, 1);
654}
655
656void img_toggle_antialias(img_t *img) {
657 if (!img || !img->im)
658 return;
659
660 img->aa = !img->aa;
661 imlib_context_set_image(img->im);
662 imlib_context_set_anti_alias(img->aa);
663 img->dirty = true;
664}
665
666bool img_frame_goto(img_t *img, int n) {
667 if (!img || !img->im)
668 return false;
669 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
670 return false;
671
672 img->multi.sel = n;
673 img->im = img->multi.frames[n].im;
674
675 imlib_context_set_image(img->im);
676 img->w = imlib_image_get_width();
677 img->h = imlib_image_get_height();
678 img->checkpan = true;
679 img->dirty = true;
680
681 return true;
682}
683
684bool img_frame_navigate(img_t *img, int d) {
685 if (!img || !img->im || !img->multi.cnt || !d)
686 return false;
687
688 d += img->multi.sel;
689 if (d < 0)
690 d = 0;
691 else if (d >= img->multi.cnt)
692 d = img->multi.cnt - 1;
693
694 return img_frame_goto(img, d);
695}
696
697bool img_frame_animate(img_t *img, bool restart) {
698 if (!img || !img->im || !img->multi.cnt)
699 return false;
700
701 if (img->multi.sel + 1 >= img->multi.cnt) {
702 if (restart || (GIF_LOOP && !img->slideshow)) {
703 img_frame_goto(img, 0);
704 } else {
705 img->multi.animate = false;
706 return false;
707 }
708 } else if (!restart) {
709 img_frame_goto(img, img->multi.sel + 1);
710 }
711 img->multi.animate = true;
712 img->dirty = true;
713
714 return true;
715}