| 1 | /* sxiv: thumbs.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 |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <Imlib2.h> |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "thumbs.h" |
| 25 | #include "util.h" |
| 26 | |
| 27 | const int thumb_dim = THUMB_SIZE + 10; |
| 28 | |
| 29 | void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { |
| 30 | int i, w, h; |
| 31 | float z, zw, zh; |
| 32 | thumb_t *t; |
| 33 | Imlib_Image *im; |
| 34 | |
| 35 | if (!tns || !win || !fnames || !fcnt) |
| 36 | return; |
| 37 | |
| 38 | tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t)); |
| 39 | tns->cnt = fcnt; |
| 40 | tns->loaded = 0; |
| 41 | |
| 42 | for (i = 0; i < fcnt; ++i) { |
| 43 | if (!(im = imlib_load_image(fnames[i]))) |
| 44 | continue; |
| 45 | |
| 46 | imlib_context_set_image(im); |
| 47 | |
| 48 | w = imlib_image_get_width(); |
| 49 | h = imlib_image_get_height(); |
| 50 | zw = (float) THUMB_SIZE / (float) w; |
| 51 | zh = (float) THUMB_SIZE / (float) h; |
| 52 | z = MIN(zw, zh); |
| 53 | |
| 54 | t = &tns->thumbs[i]; |
| 55 | t->w = z * w; |
| 56 | t->h = z * h; |
| 57 | |
| 58 | t->pm = win_create_pixmap(win, t->w, t->h); |
| 59 | imlib_context_set_drawable(t->pm); |
| 60 | imlib_render_image_part_on_drawable_at_size(0, 0, w, h, |
| 61 | 0, 0, t->w, t->h); |
| 62 | imlib_free_image(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void tns_free(tns_t *tns, win_t *win) { |
| 67 | int i; |
| 68 | |
| 69 | if (!tns) |
| 70 | return; |
| 71 | |
| 72 | for (i = 0; i < tns->cnt; ++i) |
| 73 | win_free_pixmap(win, tns->thumbs[i].pm); |
| 74 | |
| 75 | free(tns->thumbs); |
| 76 | tns->thumbs = NULL; |
| 77 | } |
| 78 | |
| 79 | void tns_render(tns_t *tns, win_t *win) { |
| 80 | int i, cnt, x, y; |
| 81 | |
| 82 | if (!tns || !win) |
| 83 | return; |
| 84 | |
| 85 | tns->cols = win->w / thumb_dim; |
| 86 | tns->rows = win->h / thumb_dim; |
| 87 | |
| 88 | cnt = tns->cols * tns->rows; |
| 89 | if (tns->first && tns->first + cnt > tns->cnt) |
| 90 | tns->first = MAX(0, tns->cnt - cnt); |
| 91 | cnt = MIN(tns->first + cnt, tns->cnt); |
| 92 | |
| 93 | win_clear(win); |
| 94 | |
| 95 | x = y = 5; |
| 96 | i = tns->first; |
| 97 | |
| 98 | while (i < cnt) { |
| 99 | tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2; |
| 100 | tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2; |
| 101 | win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x, |
| 102 | tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h); |
| 103 | if (++i % tns->cols == 0) { |
| 104 | x = 5; |
| 105 | y += thumb_dim; |
| 106 | } else { |
| 107 | x += thumb_dim; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | win_draw(win); |
| 112 | } |
| 113 | |