Krolyxon
Commit a4b90ae

Branches

Refs →

nsxivimage.c

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