Krolyxon
Commit 7e7eaf0

Branches

Refs →

nsxivimage.c

925 lines · 21.2 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
384 if (img->multi.cap == 0) {
385 img->multi.cap = info.frame_count;
386 img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
387 } else if (info.frame_count > img->multi.cap) {
388 img->multi.cap = info.frame_count;
389 img->multi.frames = erealloc(img->multi.frames,
390 img->multi.cap * sizeof(img_frame_t));
391 }
392
393 /* Load and decode frames (also works on images with only 1 frame) */
394 img->multi.cnt = img->multi.sel = 0;
395 while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
396 im = imlib_create_image_using_copied_data(
397 info.canvas_width, info.canvas_height, (DATA32*)buf);
398 imlib_context_set_image(im);
399 imlib_image_set_format("webp");
400 /* Get an iterator of this frame - used for frame info (duration, etc.) */
401 WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
402 imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
403 /* Store info for this frame */
404 img->multi.frames[img->multi.cnt].im = im;
405 delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
406 img->multi.frames[img->multi.cnt].delay = delay;
407 img->multi.length += img->multi.frames[img->multi.cnt].delay;
408 img->multi.cnt++;
409 }
410 WebPDemuxReleaseIterator(&iter);
411
412 if (img->multi.cnt > 1) {
413 imlib_context_set_image(img->im);
414 imlib_free_image();
415 img->im = img->multi.frames[0].im;
416 } else if (img->multi.cnt == 1) {
417 imlib_context_set_image(img->multi.frames[0].im);
418 imlib_free_image();
419 img->multi.cnt = 0;
420 }
421 imlib_context_set_image(img->im);
422 }
423 imlib_image_set_format("webp");
424fail:
425 if (dec != NULL)
426 WebPAnimDecoderDelete(dec);
427 free((unsigned char *)data.bytes);
428 return !err;
429}
430#endif /* HAVE_LIBWEBP */
431
432Imlib_Image img_open(const fileinfo_t *file)
433{
434 struct stat st;
435 Imlib_Image im = NULL;
436
437 if (access(file->path, R_OK) == 0 &&
438 stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
439 {
440#if HAVE_LIBWEBP
441 if (is_webp(file->path))
442 img_load_webp(file, &im, NULL);
443 else
444#endif
445 im = imlib_load_image(file->path);
446 if (im != NULL) {
447 imlib_context_set_image(im);
448#if HAVE_LIBWEBP
449 const char *fmt;
450 if ((fmt = imlib_image_format()) != NULL && !STREQ(fmt, "webp") &&
451 imlib_image_get_data_for_reading_only() == NULL) {
452#else
453 if (imlib_image_get_data_for_reading_only() == NULL) {
454#endif
455 imlib_free_image();
456 im = NULL;
457 }
458 }
459 }
460 if (im == NULL && (file->flags & FF_WARN))
461 error(0, 0, "%s: Error opening image", file->name);
462 return im;
463}
464
465bool img_load(img_t *img, const fileinfo_t *file)
466{
467 const char *fmt;
468
469 if ((img->im = img_open(file)) == NULL)
470 return false;
471
472 imlib_image_set_changes_on_disk();
473
474#if HAVE_LIBEXIF
475 exif_auto_orientate(file);
476#endif
477
478 if ((fmt = imlib_image_format()) != NULL) {
479#if HAVE_LIBGIF
480 if (STREQ(fmt, "gif"))
481 img_load_gif(img, file);
482#endif
483#if HAVE_LIBWEBP
484 if (STREQ(fmt, "webp"))
485 img_load_webp(file, NULL, img);
486#endif
487 }
488 img->w = imlib_image_get_width();
489 img->h = imlib_image_get_height();
490 img->checkpan = true;
491 img->dirty = true;
492
493 return true;
494}
495
496CLEANUP void img_close(img_t *img, bool decache)
497{
498 int i;
499
500 if (img->multi.cnt > 0) {
501 for (i = 0; i < img->multi.cnt; i++) {
502 imlib_context_set_image(img->multi.frames[i].im);
503 imlib_free_image();
504 }
505 img->multi.cnt = 0;
506 img->im = NULL;
507 } else if (img->im != NULL) {
508 imlib_context_set_image(img->im);
509 if (decache)
510 imlib_free_image_and_decache();
511 else
512 imlib_free_image();
513 img->im = NULL;
514 }
515}
516
517void img_check_pan(img_t *img, bool moved)
518{
519 win_t *win;
520 float w, h, ox, oy;
521
522 win = img->win;
523 w = img->w * img->zoom;
524 h = img->h * img->zoom;
525 ox = img->x;
526 oy = img->y;
527
528 if (w < win->w)
529 img->x = (win->w - w) / 2;
530 else if (img->x > 0)
531 img->x = 0;
532 else if (img->x + w < win->w)
533 img->x = win->w - w;
534 if (h < win->h)
535 img->y = (win->h - h) / 2;
536 else if (img->y > 0)
537 img->y = 0;
538 else if (img->y + h < win->h)
539 img->y = win->h - h;
540
541 if (!moved && (ox != img->x || oy != img->y))
542 img->dirty = true;
543}
544
545bool img_fit(img_t *img)
546{
547 float z, zw, zh;
548
549 if (img->scalemode == SCALE_ZOOM)
550 return false;
551
552 zw = (float) img->win->w / (float) img->w;
553 zh = (float) img->win->h / (float) img->h;
554
555 switch (img->scalemode) {
556 case SCALE_FILL:
557 z = MAX(zw, zh);
558 break;
559 case SCALE_WIDTH:
560 z = zw;
561 break;
562 case SCALE_HEIGHT:
563 z = zh;
564 break;
565 default:
566 z = MIN(zw, zh);
567 break;
568 }
569 z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : ZOOM_MAX);
570
571 if (ABS(img->zoom - z) > 1.0/MAX(img->w, img->h)) {
572 img->zoom = z;
573 img->dirty = true;
574 return true;
575 } else {
576 return false;
577 }
578}
579
580void img_render(img_t *img)
581{
582 win_t *win;
583 int sx, sy, sw, sh;
584 int dx, dy, dw, dh;
585 Imlib_Image bg;
586 XColor c;
587
588 win = img->win;
589 img_fit(img);
590
591 if (img->checkpan) {
592 img_check_pan(img, false);
593 img->checkpan = false;
594 }
595
596 if (!img->dirty)
597 return;
598
599 /* calculate source and destination offsets:
600 * - part of image drawn on full window, or
601 * - full image drawn on part of window
602 */
603 if (img->x <= 0) {
604 sx = -img->x / img->zoom + 0.5;
605 sw = win->w / img->zoom;
606 dx = 0;
607 dw = win->w;
608 } else {
609 sx = 0;
610 sw = img->w;
611 dx = img->x;
612 dw = img->w * img->zoom;
613 }
614 if (img->y <= 0) {
615 sy = -img->y / img->zoom + 0.5;
616 sh = win->h / img->zoom;
617 dy = 0;
618 dh = win->h;
619 } else {
620 sy = 0;
621 sh = img->h;
622 dy = img->y;
623 dh = img->h * img->zoom;
624 }
625
626 win_clear(win);
627
628 imlib_context_set_image(img->im);
629 imlib_context_set_anti_alias(img->aa);
630 imlib_context_set_drawable(win->buf.pm);
631
632 if (imlib_image_has_alpha()) {
633 if ((bg = imlib_create_image(dw, dh)) == NULL)
634 error(EXIT_FAILURE, ENOMEM, NULL);
635 imlib_context_set_image(bg);
636 imlib_image_set_has_alpha(0);
637
638 if (img->alpha) {
639 int i, c, r;
640 DATA32 col[2] = { 0xFF666666, 0xFF999999 };
641 DATA32 * data = imlib_image_get_data();
642
643 for (r = 0; r < dh; r++) {
644 i = r * dw;
645 if (r == 0 || r == 8) {
646 for (c = 0; c < dw; c++)
647 data[i++] = col[!(c & 8) ^ !r];
648 } else {
649 memcpy(&data[i], &data[(r & 8) * dw], dw * sizeof(data[0]));
650 }
651 }
652 imlib_image_put_back_data(data);
653 } else {
654 c = win->win_bg;
655 imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF);
656 imlib_image_fill_rectangle(0, 0, dw, dh);
657 }
658 imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh);
659 imlib_context_set_color_modifier(NULL);
660 imlib_render_image_on_drawable(dx, dy);
661 imlib_free_image();
662 imlib_context_set_color_modifier(img->cmod);
663 } else {
664 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
665 }
666 img->dirty = false;
667}
668
669bool img_fit_win(img_t *img, scalemode_t sm)
670{
671 float oz;
672
673 oz = img->zoom;
674 img->scalemode = sm;
675
676 if (img_fit(img)) {
677 img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * img->zoom / oz;
678 img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * img->zoom / oz;
679 img->checkpan = true;
680 return true;
681 } else {
682 return false;
683 }
684}
685
686bool img_zoom_to(img_t *img, float z)
687{
688 int x, y;
689 if (ZOOM_MIN <= z && z <= ZOOM_MAX) {
690 win_cursor_pos(img->win, &x, &y);
691 if (x < 0 || x >= img->win->w || y < 0 || y >= img->win->h) {
692 x = img->win->w / 2;
693 y = img->win->h / 2;
694 }
695 img->x = x - (x - img->x) * z / img->zoom;
696 img->y = y - (y - img->y) * z / img->zoom;
697 img->zoom = z;
698 img->scalemode = SCALE_ZOOM;
699 img->checkpan = true;
700 img->dirty = true;
701 return true;
702 } else {
703 return false;
704 }
705}
706
707bool img_zoom(img_t *img, int d)
708{
709 const float z = img->zoom * (d > 0 ? ZOOM_STEP : 1/ZOOM_STEP);
710 return img_zoom_to(img, z);
711}
712
713bool img_pos(img_t *img, float x, float y)
714{
715 float ox, oy;
716
717 ox = img->x;
718 oy = img->y;
719
720 img->x = x;
721 img->y = y;
722
723 img_check_pan(img, true);
724
725 if (ox != img->x || oy != img->y) {
726 img->dirty = true;
727 return true;
728 } else {
729 return false;
730 }
731}
732
733bool img_move(img_t *img, float dx, float dy)
734{
735 return img_pos(img, img->x + dx, img->y + dy);
736}
737
738bool img_pan(img_t *img, direction_t dir, int d)
739{
740 /* d < 0: screen-wise
741 * d = 0: 1/PAN_FRACTION of screen
742 * d > 0: num of pixels
743 */
744 float x, y;
745
746 if (d > 0) {
747 x = y = MAX(1, (float) d * img->zoom);
748 } else {
749 x = img->win->w / (d < 0 ? 1 : PAN_FRACTION);
750 y = img->win->h / (d < 0 ? 1 : PAN_FRACTION);
751 }
752
753 switch (dir) {
754 case DIR_LEFT:
755 return img_move(img, x, 0.0);
756 case DIR_RIGHT:
757 return img_move(img, -x, 0.0);
758 case DIR_UP:
759 return img_move(img, 0.0, y);
760 case DIR_DOWN:
761 return img_move(img, 0.0, -y);
762 }
763 return false;
764}
765
766bool img_pan_edge(img_t *img, direction_t dir)
767{
768 float ox, oy;
769
770 ox = img->x;
771 oy = img->y;
772
773 if (dir & DIR_LEFT)
774 img->x = 0;
775 if (dir & DIR_RIGHT)
776 img->x = img->win->w - img->w * img->zoom;
777 if (dir & DIR_UP)
778 img->y = 0;
779 if (dir & DIR_DOWN)
780 img->y = img->win->h - img->h * img->zoom;
781
782 img_check_pan(img, true);
783
784 if (ox != img->x || oy != img->y) {
785 img->dirty = true;
786 return true;
787 } else {
788 return false;
789 }
790}
791
792void img_rotate(img_t *img, degree_t d)
793{
794 int i, tmp;
795 float ox, oy;
796
797 imlib_context_set_image(img->im);
798 imlib_image_orientate(d);
799
800 for (i = 0; i < img->multi.cnt; i++) {
801 if (i != img->multi.sel) {
802 imlib_context_set_image(img->multi.frames[i].im);
803 imlib_image_orientate(d);
804 }
805 }
806 if (d == DEGREE_90 || d == DEGREE_270) {
807 ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom;
808 oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom;
809
810 img->x = oy + (img->win->w - img->win->h) / 2;
811 img->y = ox + (img->win->h - img->win->w) / 2;
812
813 tmp = img->w;
814 img->w = img->h;
815 img->h = tmp;
816 img->checkpan = true;
817 }
818 img->dirty = true;
819}
820
821void img_flip(img_t *img, flipdir_t d)
822{
823 int i;
824 void (*imlib_flip_op[3])(void) = {
825 imlib_image_flip_horizontal,
826 imlib_image_flip_vertical,
827 imlib_image_flip_diagonal
828 };
829
830 d = (d & (FLIP_HORIZONTAL | FLIP_VERTICAL)) - 1;
831
832 if (d < 0 || d >= ARRLEN(imlib_flip_op))
833 return;
834
835 imlib_context_set_image(img->im);
836 imlib_flip_op[d]();
837
838 for (i = 0; i < img->multi.cnt; i++) {
839 if (i != img->multi.sel) {
840 imlib_context_set_image(img->multi.frames[i].im);
841 imlib_flip_op[d]();
842 }
843 }
844 img->dirty = true;
845}
846
847void img_toggle_antialias(img_t *img)
848{
849 img->aa = !img->aa;
850 imlib_context_set_image(img->im);
851 imlib_context_set_anti_alias(img->aa);
852 img->dirty = true;
853}
854
855bool img_change_gamma(img_t *img, int d)
856{
857 /* d < 0: decrease gamma
858 * d = 0: reset gamma
859 * d > 0: increase gamma
860 */
861 int gamma;
862 double range;
863
864 if (d == 0)
865 gamma = 0;
866 else
867 gamma = MIN(MAX(img->gamma + d, -GAMMA_RANGE), GAMMA_RANGE);
868
869 if (img->gamma != gamma) {
870 imlib_reset_color_modifier();
871 if (gamma != 0) {
872 range = gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
873 imlib_modify_color_modifier_gamma(1.0 + gamma * (range / GAMMA_RANGE));
874 }
875 img->gamma = gamma;
876 img->dirty = true;
877 return true;
878 } else {
879 return false;
880 }
881}
882
883bool img_frame_goto(img_t *img, int n)
884{
885 if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
886 return false;
887
888 img->multi.sel = n;
889 img->im = img->multi.frames[n].im;
890
891 imlib_context_set_image(img->im);
892 img->w = imlib_image_get_width();
893 img->h = imlib_image_get_height();
894 img->checkpan = true;
895 img->dirty = true;
896
897 return true;
898}
899
900bool img_frame_navigate(img_t *img, int d)
901{
902 if (img->multi.cnt == 0 || d == 0)
903 return false;
904
905 d += img->multi.sel;
906 if (d < 0)
907 d = 0;
908 else if (d >= img->multi.cnt)
909 d = img->multi.cnt - 1;
910
911 return img_frame_goto(img, d);
912}
913
914bool img_frame_animate(img_t *img)
915{
916 if (img->multi.cnt == 0)
917 return false;
918
919 if (img->multi.sel + 1 >= img->multi.cnt)
920 img_frame_goto(img, 0);
921 else
922 img_frame_goto(img, img->multi.sel + 1);
923 img->dirty = true;
924 return true;
925}