Krolyxon
Commit 14d1daf

Branches

Refs →

nsxivimage.c

614 lines · 13.3 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#include <unistd.h>
20
21#ifdef HAVE_GIFLIB
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <gif_lib.h>
26#endif
27
28#include "image.h"
29#include "options.h"
30#include "util.h"
31
32#define _IMAGE_CONFIG
33#include "config.h"
34
35enum { MIN_GIF_DELAY = 50 };
36
37float zoom_min;
38float zoom_max;
39
40void img_init(img_t *img, win_t *win) {
41 zoom_min = zoom_levels[0] / 100.0;
42 zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
43
44 if (img) {
45 img->im = NULL;
46 img->multi.cap = img->multi.cnt = 0;
47 img->multi.animate = 0;
48 img->zoom = options->zoom;
49 img->zoom = MAX(img->zoom, zoom_min);
50 img->zoom = MIN(img->zoom, zoom_max);
51 img->aa = options->aa;
52 img->alpha = 1;
53 }
54
55 if (win) {
56 imlib_context_set_display(win->env.dpy);
57 imlib_context_set_visual(win->env.vis);
58 imlib_context_set_colormap(win->env.cmap);
59 }
60}
61
62#ifdef HAVE_GIFLIB
63/* originally based on, but in it's current form merely inspired by Imlib2's
64 * src/modules/loaders/loader_gif.c:load(), written by Carsten Haitzler.
65 */
66int img_load_gif(img_t *img, const fileinfo_t *file) {
67 GifFileType *gif;
68 GifRowType *rows = NULL;
69 GifRecordType rec;
70 ColorMapObject *cmap;
71 DATA32 bgpixel, *data, *ptr;
72 DATA32 *prev_frame = NULL;
73 Imlib_Image *im;
74 int i, j, bg, r, g, b;
75 int x, y, w, h, sw, sh;
76 int intoffset[] = { 0, 4, 2, 1 };
77 int intjump[] = { 8, 8, 4, 2 };
78 int err = 0, transp = -1;
79 unsigned int delay = 0;
80
81 if (img->multi.cap == 0) {
82 img->multi.cap = 8;
83 img->multi.frames = (img_frame_t*)
84 s_malloc(sizeof(img_frame_t) * img->multi.cap);
85 }
86 img->multi.cnt = 0;
87 img->multi.sel = 0;
88
89 gif = DGifOpenFileName(file->path);
90 if (!gif) {
91 warn("could not open gif file: %s", file->name);
92 return 0;
93 }
94 bg = gif->SBackGroundColor;
95 sw = gif->SWidth;
96 sh = gif->SHeight;
97
98 do {
99 if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
100 warn("could not open gif file: %s", file->name);
101 err = 1;
102 break;
103 }
104 if (rec == EXTENSION_RECORD_TYPE) {
105 int ext_code;
106 GifByteType *ext = NULL;
107
108 DGifGetExtension(gif, &ext_code, &ext);
109 while (ext) {
110 if (ext_code == 0xf9) {
111 if (ext[1] & 1)
112 transp = (int) ext[4];
113 else
114 transp = -1;
115
116 delay = 10 * ((unsigned int) ext[3] << 8 | (unsigned int) ext[2]);
117 if (delay)
118 delay = MAX(delay, MIN_GIF_DELAY);
119 }
120 ext = NULL;
121 DGifGetExtensionNext(gif, &ext);
122 }
123 } else if (rec == IMAGE_DESC_RECORD_TYPE) {
124 if (DGifGetImageDesc(gif) == GIF_ERROR) {
125 warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
126 err = 1;
127 break;
128 }
129 x = gif->Image.Left;
130 y = gif->Image.Top;
131 w = gif->Image.Width;
132 h = gif->Image.Height;
133
134 rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
135 for (i = 0; i < h; i++)
136 rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
137 if (gif->Image.Interlace) {
138 for (i = 0; i < 4; i++) {
139 for (j = intoffset[i]; j < h; j += intjump[i])
140 DGifGetLine(gif, rows[j], w);
141 }
142 } else {
143 for (i = 0; i < h; i++)
144 DGifGetLine(gif, rows[i], w);
145 }
146
147 ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
148 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
149 r = cmap->Colors[bg].Red;
150 g = cmap->Colors[bg].Green;
151 b = cmap->Colors[bg].Blue;
152 bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
153
154 for (i = 0; i < sh; i++) {
155 for (j = 0; j < sw; j++) {
156 if (i < y || i >= y + h || j < x || j >= x + w) {
157 if (transp >= 0 && prev_frame)
158 *ptr = prev_frame[i * sw + j];
159 else
160 *ptr = bgpixel;
161 } else if (rows[i-y][j-x] == transp) {
162 if (prev_frame)
163 *ptr = prev_frame[i * sw + j];
164 else
165 *ptr = bgpixel;
166 } else {
167 r = cmap->Colors[rows[i-y][j-x]].Red;
168 g = cmap->Colors[rows[i-y][j-x]].Green;
169 b = cmap->Colors[rows[i-y][j-x]].Blue;
170 *ptr = 0xff << 24 | r << 16 | g << 8 | b;
171 }
172 ptr++;
173 }
174 }
175
176 im = imlib_create_image_using_copied_data(sw, sh, data);
177
178 for (i = 0; i < h; i++)
179 free(rows[i]);
180 free(rows);
181 free(data);
182
183 if (!im) {
184 warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
185 err = 1;
186 break;
187 }
188
189 imlib_context_set_image(im);
190 prev_frame = imlib_image_get_data_for_reading_only();
191
192 imlib_image_set_format("gif");
193 if (transp >= 0)
194 imlib_image_set_has_alpha(1);
195
196 if (img->multi.cnt == img->multi.cap) {
197 img->multi.cap *= 2;
198 img->multi.frames = (img_frame_t*)
199 s_realloc(img->multi.frames,
200 img->multi.cap * sizeof(img_frame_t));
201 }
202 img->multi.frames[img->multi.cnt].im = im;
203 img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY;
204 img->multi.cnt++;
205 }
206 } while (rec != TERMINATE_RECORD_TYPE);
207
208 DGifCloseFile(gif);
209
210 if (!err && img->multi.cnt > 1) {
211 imlib_context_set_image(img->im);
212 imlib_free_image();
213 img->im = img->multi.frames[0].im;
214 img->multi.animate = GIF_AUTOPLAY;
215 } else {
216 for (i = 0; i < img->multi.cnt; i++) {
217 imlib_context_set_image(img->multi.frames[i].im);
218 imlib_free_image();
219 }
220 img->multi.cnt = 0;
221 img->multi.animate = 0;
222 }
223
224 imlib_context_set_image(img->im);
225
226 return !err;
227}
228#endif /* HAVE_GIFLIB */
229
230int img_load(img_t *img, const fileinfo_t *file) {
231 const char *fmt;
232
233 if (!img || !file || !file->name || !file->path)
234 return 0;
235
236 if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) {
237 warn("could not open image: %s", file->name);
238 return 0;
239 }
240
241 imlib_context_set_image(img->im);
242 imlib_image_set_changes_on_disk();
243 imlib_context_set_anti_alias(img->aa);
244
245 fmt = imlib_image_format();
246#ifdef HAVE_GIFLIB
247 if (!strcmp(fmt, "gif"))
248 img_load_gif(img, file);
249#else
250 /* avoid unused-but-set-variable warning */
251 (void) fmt;
252#endif
253
254 img->scalemode = options->scalemode;
255 img->re = 0;
256 img->checkpan = 0;
257
258 img->w = imlib_image_get_width();
259 img->h = imlib_image_get_height();
260
261 return 1;
262}
263
264void img_close(img_t *img, int decache) {
265 int i;
266
267 if (!img)
268 return;
269
270 if (img->multi.cnt) {
271 for (i = 0; i < img->multi.cnt; i++) {
272 imlib_context_set_image(img->multi.frames[i].im);
273 imlib_free_image();
274 }
275 img->multi.cnt = 0;
276 img->im = NULL;
277 } else if (img->im) {
278 imlib_context_set_image(img->im);
279 if (decache)
280 imlib_free_image_and_decache();
281 else
282 imlib_free_image();
283 img->im = NULL;
284 }
285}
286
287void img_check_pan(img_t *img, win_t *win) {
288 if (!img || !win)
289 return;
290
291 if (img->w * img->zoom > win->w) {
292 if (img->x > 0 && img->x + img->w * img->zoom > win->w)
293 img->x = 0;
294 if (img->x < 0 && img->x + img->w * img->zoom < win->w)
295 img->x = win->w - img->w * img->zoom;
296 } else {
297 img->x = (win->w - img->w * img->zoom) / 2;
298 }
299 if (img->h * img->zoom > win->h) {
300 if (img->y > 0 && img->y + img->h * img->zoom > win->h)
301 img->y = 0;
302 if (img->y < 0 && img->y + img->h * img->zoom < win->h)
303 img->y = win->h - img->h * img->zoom;
304 } else {
305 img->y = (win->h - img->h * img->zoom) / 2;
306 }
307}
308
309int img_fit(img_t *img, win_t *win) {
310 float oz, zw, zh;
311
312 if (!img || !win)
313 return 0;
314
315 oz = img->zoom;
316 zw = (float) win->w / (float) img->w;
317 zh = (float) win->h / (float) img->h;
318
319 img->zoom = MIN(zw, zh);
320 img->zoom = MAX(img->zoom, zoom_min);
321 img->zoom = MIN(img->zoom, zoom_max);
322
323 return oz != img->zoom;
324}
325
326void img_render(img_t *img, win_t *win) {
327 int sx, sy, sw, sh;
328 int dx, dy, dw, dh;
329
330 if (!img || !img->im || !win)
331 return;
332
333 if (img->scalemode != SCALE_ZOOM) {
334 img_fit(img, win);
335 if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
336 img->zoom = 1.0;
337 }
338
339 if (!img->re) {
340 /* rendered for the first time */
341 img->re = 1;
342 if (img->zoom * img->w <= win->w)
343 img->x = (win->w - img->w * img->zoom) / 2;
344 else
345 img->x = 0;
346 if (img->zoom * img->h <= win->h)
347 img->y = (win->h - img->h * img->zoom) / 2;
348 else
349 img->y = 0;
350 }
351
352 if (img->checkpan) {
353 img_check_pan(img, win);
354 img->checkpan = 0;
355 }
356
357 /* calculate source and destination offsets */
358 if (img->x < 0) {
359 sx = -img->x / img->zoom;
360 sw = win->w / img->zoom;
361 dx = 0;
362 dw = win->w;
363 } else {
364 sx = 0;
365 sw = img->w;
366 dx = img->x;
367 dw = img->w * img->zoom;
368 }
369 if (img->y < 0) {
370 sy = -img->y / img->zoom;
371 sh = win->h / img->zoom;
372 dy = 0;
373 dh = win->h;
374 } else {
375 sy = 0;
376 sh = img->h;
377 dy = img->y;
378 dh = img->h * img->zoom;
379 }
380
381 win_clear(win);
382
383 imlib_context_set_image(img->im);
384
385 if (imlib_image_has_alpha() && !img->alpha)
386 win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
387
388 imlib_context_set_drawable(win->pm);
389 imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
390
391 win_draw(win);
392}
393
394int img_fit_win(img_t *img, win_t *win) {
395 if (!img || !img->im || !win)
396 return 0;
397
398 img->scalemode = SCALE_FIT;
399 return img_fit(img, win);
400}
401
402int img_center(img_t *img, win_t *win) {
403 int ox, oy;
404
405 if (!img || !win)
406 return 0;
407
408 ox = img->x;
409 oy = img->y;
410
411 img->x = (win->w - img->w * img->zoom) / 2;
412 img->y = (win->h - img->h * img->zoom) / 2;
413
414 return ox != img->x || oy != img->y;
415}
416
417int img_zoom(img_t *img, win_t *win, float z) {
418 if (!img || !img->im || !win)
419 return 0;
420
421 z = MAX(z, zoom_min);
422 z = MIN(z, zoom_max);
423
424 img->scalemode = SCALE_ZOOM;
425
426 if (z != img->zoom) {
427 img->x = win->w / 2 - (win->w / 2 - img->x) * z / img->zoom;
428 img->y = win->h / 2 - (win->h / 2 - img->y) * z / img->zoom;
429 img->zoom = z;
430 img->checkpan = 1;
431 return 1;
432 } else {
433 return 0;
434 }
435}
436
437int img_zoom_in(img_t *img, win_t *win) {
438 int i;
439
440 if (!img || !img->im || !win)
441 return 0;
442
443 for (i = 1; i < ARRLEN(zoom_levels); i++) {
444 if (zoom_levels[i] > img->zoom * 100.0)
445 return img_zoom(img, win, zoom_levels[i] / 100.0);
446 }
447 return 0;
448}
449
450int img_zoom_out(img_t *img, win_t *win) {
451 int i;
452
453 if (!img || !img->im || !win)
454 return 0;
455
456 for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
457 if (zoom_levels[i] < img->zoom * 100.0)
458 return img_zoom(img, win, zoom_levels[i] / 100.0);
459 }
460 return 0;
461}
462
463int img_move(img_t *img, win_t *win, int dx, int dy) {
464 int ox, oy;
465
466 if (!img || !img->im || !win)
467 return 0;
468
469 ox = img->x;
470 oy = img->y;
471
472 img->x += dx;
473 img->y += dy;
474
475 img_check_pan(img, win);
476
477 return ox != img->x || oy != img->y;
478}
479
480int img_pan(img_t *img, win_t *win, direction_t dir, int screen) {
481 if (!img || !img->im || !win)
482 return 0;
483
484 switch (dir) {
485 case DIR_LEFT:
486 return img_move(img, win, win->w / (screen ? 1 : 5), 0);
487 case DIR_RIGHT:
488 return img_move(img, win, win->w / (screen ? 1 : 5) * -1, 0);
489 case DIR_UP:
490 return img_move(img, win, 0, win->h / (screen ? 1 : 5));
491 case DIR_DOWN:
492 return img_move(img, win, 0, win->h / (screen ? 1 : 5) * -1);
493 }
494
495 return 0;
496}
497
498int img_pan_edge(img_t *img, win_t *win, direction_t dir) {
499 int ox, oy;
500
501 if (!img || !img->im || !win)
502 return 0;
503
504 ox = img->x;
505 oy = img->y;
506
507 switch (dir) {
508 case DIR_LEFT:
509 img->x = 0;
510 break;
511 case DIR_RIGHT:
512 img->x = win->w - img->w * img->zoom;
513 break;
514 case DIR_UP:
515 img->y = 0;
516 break;
517 case DIR_DOWN:
518 img->y = win->h - img->h * img->zoom;
519 break;
520 }
521
522 img_check_pan(img, win);
523
524 return ox != img->x || oy != img->y;
525}
526
527void img_rotate(img_t *img, win_t *win, int d) {
528 int ox, oy, tmp;
529
530 if (!img || !img->im || !win)
531 return;
532
533 ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
534 oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
535
536 imlib_context_set_image(img->im);
537 imlib_image_orientate(d);
538
539 img->x = oy + (win->w - win->h) / 2;
540 img->y = ox + (win->h - win->w) / 2;
541
542 tmp = img->w;
543 img->w = img->h;
544 img->h = tmp;
545
546 img->checkpan = 1;
547}
548
549void img_rotate_left(img_t *img, win_t *win) {
550 img_rotate(img, win, 3);
551}
552
553void img_rotate_right(img_t *img, win_t *win) {
554 img_rotate(img, win, 1);
555}
556
557void img_toggle_antialias(img_t *img) {
558 if (img && img->im) {
559 img->aa ^= 1;
560 imlib_context_set_image(img->im);
561 imlib_context_set_anti_alias(img->aa);
562 }
563}
564
565int img_frame_goto(img_t *img, int n) {
566 if (!img || n < 0 || n >= img->multi.cnt)
567 return 0;
568
569 if (n == img->multi.sel)
570 return 0;
571
572 img->multi.sel = n;
573 img->im = img->multi.frames[n].im;
574
575 imlib_context_set_image(img->im);
576 img->w = imlib_image_get_width();
577 img->h = imlib_image_get_height();
578 img->checkpan = 1;
579
580 return 1;
581}
582
583int img_frame_navigate(img_t *img, int d) {
584 if (!img || !img->multi.cnt || !d)
585 return 0;
586
587 d += img->multi.sel;
588 if (d < 0)
589 d = 0;
590 else if (d >= img->multi.cnt)
591 d = img->multi.cnt - 1;
592
593 return img_frame_goto(img, d);
594}
595
596int img_frame_animate(img_t *img, int restart) {
597 if (!img || !img->multi.cnt)
598 return 0;
599
600 if (img->multi.sel + 1 >= img->multi.cnt) {
601 if (restart || GIF_LOOP) {
602 img_frame_goto(img, 0);
603 } else {
604 img->multi.animate = 0;
605 return 0;
606 }
607 } else if (!restart) {
608 img_frame_goto(img, img->multi.sel + 1);
609 }
610
611 img->multi.animate = 1;
612
613 return img->multi.frames[img->multi.sel].delay;
614}