Krolyxon
Commit 12efa0e

Branches

Refs →

nsxivimage.c

920 lines · 21.0 KB · C

1/* Copyright 2011, 2012 Bert Muennich
2 *
3 * This file is a part of nsxiv.
4 *
5 * nsxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * nsxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "nsxiv.h"
20#define _IMAGE_CONFIG
21#include "config.h"
22
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30#if HAVE_LIBEXIF
31#include <libexif/exif-data.h>
32#endif
33
34#if HAVE_LIBGIF
35#include <gif_lib.h>
36enum { DEF_GIF_DELAY = 75 };
37#endif
38
39#if HAVE_LIBWEBP
40#include <webp/decode.h>
41#include <webp/demux.h>
42enum { DEF_WEBP_DELAY = 75 };
43#endif
44
45void img_init(img_t *img, win_t *win)
46{
47 imlib_context_set_display(win->env.dpy);
48 imlib_context_set_visual(win->env.vis);
49 imlib_context_set_colormap(win->env.cmap);
50
51 img->im = NULL;
52 img->win = win;
53 img->scalemode = options->scalemode;
54 img->zoom = options->zoom;
55 img->zoom = MAX(img->zoom, ZOOM_MIN);
56 img->zoom = MIN(img->zoom, ZOOM_MAX);
57 img->checkpan = false;
58 img->dirty = false;
59 img->aa = ANTI_ALIAS;
60 img->alpha = ALPHA_LAYER;
61 img->multi.cap = img->multi.cnt = 0;
62 img->multi.animate = options->animate;
63 img->multi.framedelay = options->framerate > 0 ? 1000 / options->framerate : 0;
64 img->multi.length = 0;
65
66 img->cmod = imlib_create_color_modifier();
67 imlib_context_set_color_modifier(img->cmod);
68 img_change_gamma(img, options->gamma);
69
70 img->ss.on = options->slideshow > 0;
71 img->ss.delay = options->slideshow > 0 ? options->slideshow : SLIDESHOW_DELAY * 10;
72}
73
74#if HAVE_LIBEXIF
75void exif_auto_orientate(const fileinfo_t *file)
76{
77 ExifData *ed;
78 ExifEntry *entry;
79 int byte_order, orientation = 0;
80
81 if ((ed = exif_data_new_from_file(file->path)) == NULL)
82 return;
83 byte_order = exif_data_get_byte_order(ed);
84 entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
85 if (entry != NULL)
86 orientation = exif_get_short(entry->data, byte_order);
87 exif_data_unref(ed);
88
89 switch (orientation) {
90 case 5:
91 imlib_image_orientate(1);
92 /* fall through */
93 case 2:
94 imlib_image_flip_vertical();
95 break;
96 case 3:
97 imlib_image_orientate(2);
98 break;
99 case 7:
100 imlib_image_orientate(1);
101 /* fall through */
102 case 4:
103 imlib_image_flip_horizontal();
104 break;
105 case 6:
106 imlib_image_orientate(1);
107 break;
108 case 8:
109 imlib_image_orientate(3);
110 break;
111 }
112}
113#endif
114
115#if HAVE_LIBGIF
116bool img_load_gif(img_t *img, const fileinfo_t *file)
117{
118 GifFileType *gif;
119 GifRowType *rows = NULL;
120 GifRecordType rec;
121 ColorMapObject *cmap;
122 DATA32 bgpixel, *data, *ptr;
123 DATA32 *prev_frame = NULL;
124 Imlib_Image im;
125 int i, j, bg, r, g, b;
126 int x, y, w, h, sw, sh;
127 int px, py, pw, ph;
128 int intoffset[] = { 0, 4, 2, 1 };
129 int intjump[] = { 8, 8, 4, 2 };
130 int transp = -1;
131 unsigned int disposal = 0, prev_disposal = 0;
132 unsigned int delay = 0;
133 bool err = false;
134
135 if (img->multi.cap == 0) {
136 img->multi.cap = 8;
137 img->multi.frames = (img_frame_t*)
138 emalloc(sizeof(img_frame_t) * img->multi.cap);
139 }
140 img->multi.cnt = img->multi.sel = 0;
141 img->multi.length = 0;
142
143#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5
144 gif = DGifOpenFileName(file->path, NULL);
145#else
146 gif = DGifOpenFileName(file->path);
147#endif
148 if (gif == NULL) {
149 error(0, 0, "%s: Error opening gif image", file->name);
150 return false;
151 }
152 bg = gif->SBackGroundColor;
153 sw = gif->SWidth;
154 sh = gif->SHeight;
155 px = py = pw = ph = 0;
156
157 do {
158 if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
159 err = true;
160 break;
161 }
162 if (rec == EXTENSION_RECORD_TYPE) {
163 int ext_code;
164 GifByteType *ext = NULL;
165
166 DGifGetExtension(gif, &ext_code, &ext);
167 while (ext) {
168 if (ext_code == GRAPHICS_EXT_FUNC_CODE) {
169 if (ext[1] & 1)
170 transp = (int) ext[4];
171 else
172 transp = -1;
173
174 delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]);
175 disposal = (unsigned int) ext[1] >> 2 & 0x7;
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*) emalloc(h * sizeof(GifRowType));
191 for (i = 0; i < h; i++)
192 rows[i] = (GifRowType) emalloc(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*) emalloc(sizeof(DATA32) * sw * sh);
204 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
205 if (bg >= cmap->ColorCount) {
206 err = true;
207 break;
208 }
209 r = cmap->Colors[bg].Red;
210 g = cmap->Colors[bg].Green;
211 b = cmap->Colors[bg].Blue;
212 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
213
214 for (i = 0; i < sh; i++) {
215 for (j = 0; j < sw; j++) {
216 if (i < y || i >= y + h || j < x || j >= x + w ||
217 rows[i-y][j-x] == transp)
218 {
219 if (prev_frame != NULL && (prev_disposal != 2 ||
220 i < py || i >= py + ph || j < px || j >= px + pw))
221 {
222 *ptr = prev_frame[i * sw + j];
223 } else {
224 *ptr = bgpixel;
225 }
226 } else {
227 r = cmap->Colors[rows[i-y][j-x]].Red;
228 g = cmap->Colors[rows[i-y][j-x]].Green;
229 b = cmap->Colors[rows[i-y][j-x]].Blue;
230 *ptr = 0xffu << 24 | r << 16 | g << 8 | b;
231 }
232 ptr++;
233 }
234 }
235
236 im = imlib_create_image_using_copied_data(sw, sh, data);
237
238 for (i = 0; i < h; i++)
239 free(rows[i]);
240 free(rows);
241 free(data);
242
243 if (im == NULL) {
244 err = true;
245 break;
246 }
247
248 imlib_context_set_image(im);
249 imlib_image_set_format("gif");
250 if (transp >= 0)
251 imlib_image_set_has_alpha(1);
252
253 if (disposal != 3)
254 prev_frame = imlib_image_get_data_for_reading_only();
255 prev_disposal = disposal;
256 px = x, py = y, pw = w, ph = h;
257
258 if (img->multi.cnt == img->multi.cap) {
259 img->multi.cap *= 2;
260 img->multi.frames = (img_frame_t*)
261 erealloc(img->multi.frames,
262 img->multi.cap * sizeof(img_frame_t));
263 }
264 img->multi.frames[img->multi.cnt].im = im;
265 delay = img->multi.framedelay > 0 ? img->multi.framedelay : delay;
266 img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
267 img->multi.length += img->multi.frames[img->multi.cnt].delay;
268 img->multi.cnt++;
269 }
270 } while (rec != TERMINATE_RECORD_TYPE);
271
272#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 && GIFLIB_MINOR >= 1
273 DGifCloseFile(gif, NULL);
274#else
275 DGifCloseFile(gif);
276#endif
277
278 if (err && (file->flags & FF_WARN))
279 error(0, 0, "%s: Corrupted gif file", file->name);
280
281 if (img->multi.cnt > 1) {
282 imlib_context_set_image(img->im);
283 imlib_free_image();
284 img->im = img->multi.frames[0].im;
285 } else if (img->multi.cnt == 1) {
286 imlib_context_set_image(img->multi.frames[0].im);
287 imlib_free_image();
288 img->multi.cnt = 0;
289 }
290
291 imlib_context_set_image(img->im);
292
293 return !err;
294}
295#endif /* HAVE_LIBGIF */
296
297
298#if HAVE_LIBWEBP
299bool is_webp(const char *path)
300{
301 /* The size (in bytes) of the largest amount of data required to verify a WebP image. */
302 enum { max = 30 };
303 const unsigned char fmt[max];
304 bool ret = false;
305 FILE *f;
306
307 if ((f = fopen(path, "rb")) != NULL) {
308 if (fread((unsigned char *) fmt, 1, max, f) == max)
309 ret = WebPGetInfo(fmt, max, NULL, NULL);
310 fclose(f);
311 }
312 return ret;
313}
314
315/* fframe img
316 * NULL NULL = do nothing
317 * x NULL = load the first frame as an Imlib_Image
318 * NULL x = load all frames into img->multi.
319 */
320bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
321{
322 FILE *webp_file;
323 WebPData data;
324 data.bytes = NULL;
325
326 Imlib_Image im = NULL;
327 struct WebPAnimDecoderOptions opts;
328 WebPAnimDecoder *dec = NULL;
329 struct WebPAnimInfo info;
330 unsigned char *buf = NULL;
331 int ts;
332 const WebPDemuxer *demux;
333 WebPIterator iter;
334 unsigned long flags;
335 unsigned int delay;
336 bool err = false;
337
338 if ((err = fframe == NULL && img == NULL))
339 goto fail;
340
341 if ((err = (webp_file = fopen(file->path, "rb")) == NULL)) {
342 error(0, 0, "%s: Error opening webp image", file->name);
343 goto fail;
344 }
345 fseek(webp_file, 0L, SEEK_END);
346 data.size = ftell(webp_file);
347 rewind(webp_file);
348 data.bytes = emalloc(data.size);
349 if ((err = fread((unsigned char *)data.bytes, 1, data.size, webp_file) != data.size)) {
350 error(0, 0, "%s: Error reading webp image", file->name);
351 goto fail;
352 }
353
354 /* Setup the WebP Animation Decoder */
355 if ((err = !WebPAnimDecoderOptionsInit(&opts))) {
356 error(0, 0, "%s: WebP library version mismatch", file->name);
357 goto fail;
358 }
359 opts.color_mode = MODE_BGRA;
360 /* NOTE: Multi-threaded decoding may cause problems on some system */
361 opts.use_threads = true;
362 dec = WebPAnimDecoderNew(&data, &opts);
363 if ((err = (dec == NULL) || !WebPAnimDecoderGetInfo(dec, &info))) {
364 error(0, 0, "%s: WebP parsing or memory error (file is corrupt?)", file->name);
365 goto fail;
366 }
367 demux = WebPAnimDecoderGetDemuxer(dec);
368
369 if (img == NULL) { /* Only get the first frame and put it into fframe. */
370 if ((err = !WebPAnimDecoderGetNext(dec, &buf, &ts))) {
371 error(0, 0, "%s: Error loading first frame", file->name);
372 goto fail;
373 }
374 *fframe = imlib_create_image_using_copied_data(
375 info.canvas_width, info.canvas_height, (DATA32*)buf);
376 imlib_context_set_image(*fframe);
377 imlib_image_set_has_alpha(WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS) & ALPHA_FLAG);
378 } else {
379 /* Get global information for the image */
380 flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
381 img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
382 img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
383 img->multi.cap = info.frame_count;
384 img->multi.sel = 0;
385 img->multi.frames = emalloc(info.frame_count * sizeof(img_frame_t));
386
387 /* Load and decode frames (also works on images with only 1 frame) */
388 img->multi.cnt = 0;
389 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
390 im = imlib_create_image_using_copied_data(
391 info.canvas_width, info.canvas_height, (DATA32*)buf);
392 imlib_context_set_image(im);
393 imlib_image_set_format("webp");
394 /* Get an iterator of this frame - used for frame info (duration, etc.) */
395 WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
396 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
397 /* Store info for this frame */
398 img->multi.frames[img->multi.cnt].im = im;
399 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
400 img->multi.frames[img->multi.cnt].delay = delay;
401 img->multi.length += img->multi.frames[img->multi.cnt].delay;
402 img->multi.cnt++;
403 }
404 WebPDemuxReleaseIterator(&iter);
405
406 if (img->multi.cnt > 1) {
407 imlib_context_set_image(img->im);
408 imlib_free_image();
409 img->im = img->multi.frames[0].im;
410 } else if (img->multi.cnt == 1) {
411 imlib_context_set_image(img->multi.frames[0].im);
412 imlib_free_image();
413 img->multi.cnt = 0;
414 }
415 imlib_context_set_image(img->im);
416 }
417 imlib_image_set_format("webp");
418fail:
419 if (dec != NULL)
420 WebPAnimDecoderDelete(dec);
421 free((unsigned char *)data.bytes);
422 return !err;
423}
424#endif /* HAVE_LIBWEBP */
425
426Imlib_Image img_open(const fileinfo_t *file)
427{
428 struct stat st;
429 Imlib_Image im = NULL;
430
431 if (access(file->path, R_OK) == 0 &&
432 stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
433 {
434#if HAVE_LIBWEBP
435 if (is_webp(file->path))
436 img_load_webp(file, &im, NULL);
437 else
438#endif
439 im = imlib_load_image(file->path);
440 if (im != NULL) {
441 imlib_context_set_image(im);
442#if HAVE_LIBWEBP
443 const char *fmt;
444 if ((fmt = imlib_image_format()) != NULL && !STREQ(fmt, "webp") &&
445 imlib_image_get_data_for_reading_only() == NULL) {
446#else
447 if (imlib_image_get_data_for_reading_only() == NULL) {
448#endif
449 imlib_free_image();
450 im = NULL;
451 }
452 }
453 }
454 if (im == NULL && (file->flags & FF_WARN))
455 error(0, 0, "%s: Error opening image", file->name);
456 return im;
457}
458
459bool img_load(img_t *img, const fileinfo_t *file)
460{
461 const char *fmt;
462
463 if ((img->im = img_open(file)) == NULL)
464 return false;
465
466 imlib_image_set_changes_on_disk();
467
468#if HAVE_LIBEXIF
469 exif_auto_orientate(file);
470#endif
471
472 if ((fmt = imlib_image_format()) != NULL) {
473#if HAVE_LIBGIF
474 if (STREQ(fmt, "gif"))
475 img_load_gif(img, file);
476#endif
477#if HAVE_LIBWEBP
478 if (STREQ(fmt, "webp"))
479 img_load_webp(file, NULL, img);
480#endif
481 }
482 img->w = imlib_image_get_width();
483 img->h = imlib_image_get_height();
484 img->checkpan = true;
485 img->dirty = true;
486
487 return true;
488}
489
490CLEANUP void img_close(img_t *img, bool decache)
491{
492 int i;
493
494 if (img->multi.cnt > 0) {
495 for (i = 0; i < img->multi.cnt; i++) {
496 imlib_context_set_image(img->multi.frames[i].im);
497 imlib_free_image();
498 }
499 img->multi.cnt = 0;
500 img->im = NULL;
501 } else if (img->im != NULL) {
502 imlib_context_set_image(img->im);
503 if (decache)
504 imlib_free_image_and_decache();
505 else
506 imlib_free_image();
507 img->im = NULL;
508 }
509}
510
511void img_check_pan(img_t *img, bool moved)
512{
513 win_t *win;
514 float w, h, ox, oy;
515
516 win = img->win;
517 w = img->w * img->zoom;
518 h = img->h * img->zoom;
519 ox = img->x;
520 oy = img->y;
521
522 if (w < win->w)
523 img->x = (win->w - w) / 2;
524 else if (img->x > 0)
525 img->x = 0;
526 else if (img->x + w < win->w)
527 img->x = win->w - w;
528 if (h < win->h)
529 img->y = (win->h - h) / 2;
530 else if (img->y > 0)
531 img->y = 0;
532 else if (img->y + h < win->h)
533 img->y = win->h - h;
534
535 if (!moved && (ox != img->x || oy != img->y))
536 img->dirty = true;
537}
538
539bool img_fit(img_t *img)
540{
541 float z, zw, zh;
542
543 if (img->scalemode == SCALE_ZOOM)
544 return false;
545
546 zw = (float) img->win->w / (float) img->w;
547 zh = (float) img->win->h / (float) img->h;
548
549 switch (img->scalemode) {
550 case SCALE_FILL:
551 z = MAX(zw, zh);
552 break;
553 case SCALE_WIDTH:
554 z = zw;
555 break;
556 case SCALE_HEIGHT:
557 z = zh;
558 break;
559 default:
560 z = MIN(zw, zh);
561 break;
562 }
563 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
564
565 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
566 img->zoom = z;
567 img->dirty = true;
568 return true;
569 } else {
570 return false;
571 }
572}
573
574void img_render(img_t *img)
575{
576 win_t *win;
577 int sx, sy, sw, sh;
578 int dx, dy, dw, dh;
579 Imlib_Image bg;
580 XColor c;
581
582 win = img->win;
583 img_fit(img);
584
585 if (img->checkpan) {
586 img_check_pan(img, false);
587 img->checkpan = false;
588 }
589
590 if (!img->dirty)
591 return;
592
593 /* calculate source and destination offsets:
594 * - part of image drawn on full window, or
595 * - full image drawn on part of window
596 */
597 if (img->x <= 0) {
598 sx = -img->x / img->zoom + 0.5;
599 sw = win->w / img->zoom;
600 dx = 0;
601 dw = win->w;
602 } else {
603 sx = 0;
604 sw = img->w;
605 dx = img->x;
606 dw = img->w * img->zoom;
607 }
608 if (img->y <= 0) {
609 sy = -img->y / img->zoom + 0.5;
610 sh = win->h / img->zoom;
611 dy = 0;
612 dh = win->h;
613 } else {
614 sy = 0;
615 sh = img->h;
616 dy = img->y;
617 dh = img->h * img->zoom;
618 }
619
620 win_clear(win);
621
622 imlib_context_set_image(img->im);
623 imlib_context_set_anti_alias(img->aa);
624 imlib_context_set_drawable(win->buf.pm);
625
626 if (imlib_image_has_alpha()) {
627 if ((bg = imlib_create_image(dw, dh)) == NULL)
628 error(EXIT_FAILURE, ENOMEM, NULL);
629 imlib_context_set_image(bg);
630 imlib_image_set_has_alpha(1);
631
632 if (img->alpha) {
633 int i, c, r;
634 DATA32 col[2] = { 0xFF666666, 0xFF999999 };
635 DATA32 * data = imlib_image_get_data();
636
637 for (r = 0; r < dh; r++) {
638 i = r * dw;
639 if (r == 0 || r == 8) {
640 for (c = 0; c < dw; c++)
641 data[i++] = col[!(c & 8) ^ !r];
642 } else {
643 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
644 }
645 }
646 imlib_image_put_back_data(data);
647 } else {
648 c = win->win_bg;
649 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
650 imlib_image_fill_rectangle(0, 0, dw, dh);
651 }
652 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
653 imlib_context_set_color_modifier(NULL);
654 imlib_render_image_on_drawable(dx, dy);
655 imlib_free_image();
656 imlib_context_set_color_modifier(img->cmod);
657 } else {
658 imlib_image_set_has_alpha(1);
659 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
660 }
661 img->dirty = false;
662}
663
664bool img_fit_win(img_t *img, scalemode_t sm)
665{
666 float oz;
667
668 oz = img->zoom;
669 img->scalemode = sm;
670
671 if (img_fit(img)) {
672 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
673 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
674 img->checkpan = true;
675 return true;
676 } else {
677 return false;
678 }
679}
680
681bool img_zoom_to(img_t *img, float z)
682{
683 int x, y;
684 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
685 win_cursor_pos(img->win, &x, &y);
686 if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
687 x = img->win->w / 2;
688 y = img->win->h / 2;
689 }
690 img->x = x - (x - img->x) * z / img->zoom;
691 img->y = y - (y - img->y) * z / img->zoom;
692 img->zoom = z;
693 img->scalemode = SCALE_ZOOM;
694 img->checkpan = true;
695 img->dirty = true;
696 return true;
697 } else {
698 return false;
699 }
700}
701
702bool img_zoom(img_t *img, int d)
703{
704 const float z = img->zoom * (d > 0 ? ZOOM_STEP : 1/ZOOM_STEP);
705 return img_zoom_to(img, z);
706}
707
708bool img_pos(img_t *img, float x, float y)
709{
710 float ox, oy;
711
712 ox = img->x;
713 oy = img->y;
714
715 img->x = x;
716 img->y = y;
717
718 img_check_pan(img, true);
719
720 if (ox != img->x || oy != img->y) {
721 img->dirty = true;
722 return true;
723 } else {
724 return false;
725 }
726}
727
728bool img_move(img_t *img, float dx, float dy)
729{
730 return img_pos(img, img->x + dx, img->y + dy);
731}
732
733bool img_pan(img_t *img, direction_t dir, int d)
734{
735 /* d < 0: screen-wise
736 * d = 0: 1/PAN_FRACTION of screen
737 * d > 0: num of pixels
738 */
739 float x, y;
740
741 if (d > 0) {
742 x = y = MAX(1, (float) d * img->zoom);
743 } else {
744 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
745 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
746 }
747
748 switch (dir) {
749 case DIR_LEFT:
750 return img_move(img, x, 0.0);
751 case DIR_RIGHT:
752 return img_move(img, -x, 0.0);
753 case DIR_UP:
754 return img_move(img, 0.0, y);
755 case DIR_DOWN:
756 return img_move(img, 0.0, -y);
757 }
758 return false;
759}
760
761bool img_pan_edge(img_t *img, direction_t dir)
762{
763 float ox, oy;
764
765 ox = img->x;
766 oy = img->y;
767
768 if (dir & DIR_LEFT)
769 img->x = 0;
770 if (dir & DIR_RIGHT)
771 img->x = img->win->w - img->w * img->zoom;
772 if (dir & DIR_UP)
773 img->y = 0;
774 if (dir & DIR_DOWN)
775 img->y = img->win->h - img->h * img->zoom;
776
777 img_check_pan(img, true);
778
779 if (ox != img->x || oy != img->y) {
780 img->dirty = true;
781 return true;
782 } else {
783 return false;
784 }
785}
786
787void img_rotate(img_t *img, degree_t d)
788{
789 int i, tmp;
790 float ox, oy;
791
792 imlib_context_set_image(img->im);
793 imlib_image_orientate(d);
794
795 for (i = 0; i < img->multi.cnt; i++) {
796 if (i != img->multi.sel) {
797 imlib_context_set_image(img->multi.frames[i].im);
798 imlib_image_orientate(d);
799 }
800 }
801 if (d == DEGREE_90 || d == DEGREE_270) {
802 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
803 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
804
805 img->x = oy + (img->win->w - img->win->h) / 2;
806 img->y = ox + (img->win->h - img->win->w) / 2;
807
808 tmp = img->w;
809 img->w = img->h;
810 img->h = tmp;
811 img->checkpan = true;
812 }
813 img->dirty = true;
814}
815
816void img_flip(img_t *img, flipdir_t d)
817{
818 int i;
819 void (*imlib_flip_op[3])(void) = {
820 imlib_image_flip_horizontal,
821 imlib_image_flip_vertical,
822 imlib_image_flip_diagonal
823 };
824
825 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
826
827 if (d < 0 || d >= ARRLEN(imlib_flip_op))
828 return;
829
830 imlib_context_set_image(img->im);
831 imlib_flip_op[d]();
832
833 for (i = 0; i < img->multi.cnt; i++) {
834 if (i != img->multi.sel) {
835 imlib_context_set_image(img->multi.frames[i].im);
836 imlib_flip_op[d]();
837 }
838 }
839 img->dirty = true;
840}
841
842void img_toggle_antialias(img_t *img)
843{
844 img->aa = !img->aa;
845 imlib_context_set_image(img->im);
846 imlib_context_set_anti_alias(img->aa);
847 img->dirty = true;
848}
849
850bool img_change_gamma(img_t *img, int d)
851{
852 /* d < 0: decrease gamma
853 * d = 0: reset gamma
854 * d > 0: increase gamma
855 */
856 int gamma;
857 double range;
858
859 if (d == 0)
860 gamma = 0;
861 else
862 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
863
864 if (img->gamma != gamma) {
865 imlib_reset_color_modifier();
866 if (gamma != 0) {
867 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
868 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
869 }
870 img->gamma = gamma;
871 img->dirty = true;
872 return true;
873 } else {
874 return false;
875 }
876}
877
878bool img_frame_goto(img_t *img, int n)
879{
880 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
881 return false;
882
883 img->multi.sel = n;
884 img->im = img->multi.frames[n].im;
885
886 imlib_context_set_image(img->im);
887 img->w = imlib_image_get_width();
888 img->h = imlib_image_get_height();
889 img->checkpan = true;
890 img->dirty = true;
891
892 return true;
893}
894
895bool img_frame_navigate(img_t *img, int d)
896{
897 if (img->multi.cnt == 0 || d == 0)
898 return false;
899
900 d += img->multi.sel;
901 if (d < 0)
902 d = 0;
903 else if (d >= img->multi.cnt)
904 d = img->multi.cnt - 1;
905
906 return img_frame_goto(img, d);
907}
908
909bool img_frame_animate(img_t *img)
910{
911 if (img->multi.cnt == 0)
912 return false;
913
914 if (img->multi.sel + 1 >= img->multi.cnt)
915 img_frame_goto(img, 0);
916 else
917 img_frame_goto(img, img->multi.sel + 1);
918 img->dirty = true;
919 return true;
920}