| @@ -18,7 +18,6 @@ |
| 18 | 18 | | |
| 19 | 19 | | #include <stdlib.h> |
| 20 | 20 | | #include <string.h> |
| 21 | | − | #include <pthread.h> |
| 22 | 21 | | |
| 23 | 22 | | #include <Imlib2.h> |
| 24 | 23 | | |
| @@ -26,75 +25,14 @@ |
| 26 | 25 | | #include "thumbs.h" |
| 27 | 26 | | #include "util.h" |
| 28 | 27 | | |
| 29 | | − | typedef struct tload_s { |
| 30 | | − | const char **filenames; |
| 31 | | − | tns_t *tns; |
| 32 | | − | win_t *win; |
| 33 | | − | } tload_t; |
| 34 | | − | |
| 35 | 28 | | const int thumb_dim = THUMB_SIZE + 10; |
| 36 | 29 | | |
| 37 | | − | pthread_t loader; |
| 38 | | − | tload_t tinfo; |
| 39 | | − | |
| 40 | | − | void* thread_load(void *arg) { |
| 41 | | − | int i, w, h; |
| 42 | | − | float z, zw, zh; |
| 43 | | − | tload_t *tl; |
| 44 | | − | thumb_t *t; |
| 45 | | − | Imlib_Image *im; |
| 46 | | − | |
| 47 | | − | tl = (tload_t*) arg; |
| 48 | | − | |
| 49 | | − | for (i = 0; i < tl->tns->cnt; ++i) { |
| 50 | | − | if (!(im = imlib_load_image(tl->filenames[i]))) |
| 51 | | − | continue; |
| 52 | | − | |
| 53 | | − | imlib_context_set_image(im); |
| 54 | | − | |
| 55 | | − | w = imlib_image_get_width(); |
| 56 | | − | h = imlib_image_get_height(); |
| 57 | | − | zw = (float) THUMB_SIZE / (float) w; |
| 58 | | − | zh = (float) THUMB_SIZE / (float) h; |
| 59 | | − | z = MIN(zw, zh); |
| 60 | | − | |
| 61 | | − | t = &tl->tns->thumbs[i]; |
| 62 | | − | t->w = z * w; |
| 63 | | − | t->h = z * h; |
| 64 | | − | |
| 65 | | − | t->pm = win_create_pixmap(tl->win, t->w, t->h); |
| 66 | | − | imlib_context_set_drawable(t->pm); |
| 67 | | − | imlib_render_image_part_on_drawable_at_size(0, 0, w, h, |
| 68 | | − | 0, 0, t->w, t->h); |
| 69 | | − | t->loaded = 1; |
| 70 | | − | imlib_free_image(); |
| 71 | | − | } |
| 72 | | − | |
| 73 | | − | tl->tns->loaded = 1; |
| 74 | | − | |
| 75 | | − | return 0; |
| 76 | | − | } |
| 77 | | − | |
| 78 | | − | void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { |
| 79 | | − | pthread_attr_t tattr; |
| 80 | | − | |
| 81 | | − | if (!tns || !win || !fnames || !fcnt) |
| 30 | + | void tns_init(tns_t *tns, int cnt) { |
| 31 | + | if (!tns) |
| 82 | 32 | | return; |
| 83 | | − | |
| 84 | | − | tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t)); |
| 85 | | − | memset(tns->thumbs, 0, fcnt * sizeof(thumb_t)); |
| 86 | | − | tns->cnt = fcnt; |
| 87 | | − | tns->loaded = 0; |
| 88 | | − | |
| 89 | | − | tinfo.filenames = fnames; |
| 90 | | − | tinfo.tns = tns; |
| 91 | | − | tinfo.win = win; |
| 92 | | − | |
| 93 | | − | pthread_attr_init(&tattr); |
| 94 | | − | pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE); |
| 95 | 33 | | |
| 96 | | − | if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo)) |
| 97 | | − | die("could not create thread"); |
| 34 | + | tns->cnt = tns->first = tns->sel = 0; |
| 35 | + | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 98 | 36 | | } |
| 99 | 37 | | |
| 100 | 38 | | void tns_free(tns_t *tns, win_t *win) { |
| @@ -110,6 +48,37 @@ void tns_free(tns_t *tns, win_t *win) { |
| 110 | 48 | | tns->thumbs = NULL; |
| 111 | 49 | | } |
| 112 | 50 | | |
| 51 | + | void tns_load(tns_t *tns, win_t *win, const char *filename) { |
| 52 | + | int w, h; |
| 53 | + | float z, zw, zh; |
| 54 | + | thumb_t *t; |
| 55 | + | Imlib_Image *im; |
| 56 | + | |
| 57 | + | if (!tns || !win || !filename) |
| 58 | + | return; |
| 59 | + | |
| 60 | + | if (!(im = imlib_load_image(filename))) |
| 61 | + | return; |
| 62 | + | |
| 63 | + | imlib_context_set_image(im); |
| 64 | + | |
| 65 | + | w = imlib_image_get_width(); |
| 66 | + | h = imlib_image_get_height(); |
| 67 | + | zw = (float) THUMB_SIZE / (float) w; |
| 68 | + | zh = (float) THUMB_SIZE / (float) h; |
| 69 | + | z = MIN(zw, zh); |
| 70 | + | |
| 71 | + | t = &tns->thumbs[tns->cnt++]; |
| 72 | + | t->w = z * w; |
| 73 | + | t->h = z * h; |
| 74 | + | |
| 75 | + | t->pm = win_create_pixmap(win, t->w, t->h); |
| 76 | + | imlib_context_set_drawable(t->pm); |
| 77 | + | imlib_render_image_part_on_drawable_at_size(0, 0, w, h, |
| 78 | + | 0, 0, t->w, t->h); |
| 79 | + | imlib_free_image(); |
| 80 | + | } |
| 81 | + | |
| 113 | 82 | | void tns_render(tns_t *tns, win_t *win) { |
| 114 | 83 | | int i, cnt, x, y; |
| 115 | 84 | | |
| @@ -130,9 +99,6 @@ void tns_render(tns_t *tns, win_t *win) { |
| 130 | 99 | | i = tns->first; |
| 131 | 100 | | |
| 132 | 101 | | while (i < cnt) { |
| 133 | | − | if (!tns->thumbs[i].loaded) |
| 134 | | − | continue; |
| 135 | | − | |
| 136 | 102 | | tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2; |
| 137 | 103 | | tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2; |
| 138 | 104 | | win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x, |