2011-02-16 21:40:20 +01:00
|
|
|
/* sxiv: thumbs.c
|
|
|
|
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-02-16 23:03:42 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
2011-02-16 21:40:20 +01:00
|
|
|
|
|
|
|
#include <Imlib2.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "thumbs.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
typedef struct tload_s {
|
|
|
|
const char **filenames;
|
|
|
|
tns_t *tns;
|
|
|
|
win_t *win;
|
|
|
|
} tload_t;
|
|
|
|
|
2011-02-16 21:40:20 +01:00
|
|
|
const int thumb_dim = THUMB_SIZE + 10;
|
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
pthread_t loader;
|
|
|
|
tload_t tinfo;
|
|
|
|
|
|
|
|
void* thread_load(void *arg) {
|
2011-02-16 21:40:20 +01:00
|
|
|
int i, w, h;
|
|
|
|
float z, zw, zh;
|
2011-02-16 23:03:42 +01:00
|
|
|
tload_t *tl;
|
2011-02-16 21:40:20 +01:00
|
|
|
thumb_t *t;
|
|
|
|
Imlib_Image *im;
|
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
tl = (tload_t*) arg;
|
2011-02-16 21:40:20 +01:00
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
for (i = 0; i < tl->tns->cnt; ++i) {
|
|
|
|
if (!(im = imlib_load_image(tl->filenames[i])))
|
2011-02-16 21:40:20 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
imlib_context_set_image(im);
|
|
|
|
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
zw = (float) THUMB_SIZE / (float) w;
|
|
|
|
zh = (float) THUMB_SIZE / (float) h;
|
|
|
|
z = MIN(zw, zh);
|
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
t = &tl->tns->thumbs[i];
|
2011-02-16 21:40:20 +01:00
|
|
|
t->w = z * w;
|
|
|
|
t->h = z * h;
|
|
|
|
|
2011-02-16 23:03:42 +01:00
|
|
|
t->pm = win_create_pixmap(tl->win, t->w, t->h);
|
2011-02-16 21:40:20 +01:00
|
|
|
imlib_context_set_drawable(t->pm);
|
|
|
|
imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
|
|
|
|
0, 0, t->w, t->h);
|
2011-02-16 23:03:42 +01:00
|
|
|
t->loaded = 1;
|
2011-02-16 21:40:20 +01:00
|
|
|
imlib_free_image();
|
|
|
|
}
|
2011-02-16 23:03:42 +01:00
|
|
|
|
|
|
|
tl->tns->loaded = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
|
|
|
|
pthread_attr_t tattr;
|
|
|
|
|
|
|
|
if (!tns || !win || !fnames || !fcnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
|
|
|
|
memset(tns->thumbs, 0, fcnt * sizeof(thumb_t));
|
|
|
|
tns->cnt = fcnt;
|
|
|
|
tns->loaded = 0;
|
|
|
|
|
|
|
|
tinfo.filenames = fnames;
|
|
|
|
tinfo.tns = tns;
|
|
|
|
tinfo.win = win;
|
|
|
|
|
|
|
|
pthread_attr_init(&tattr);
|
|
|
|
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
|
|
|
|
|
|
|
|
if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo))
|
|
|
|
die("could not create thread");
|
2011-02-16 21:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tns_free(tns_t *tns, win_t *win) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!tns)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < tns->cnt; ++i)
|
|
|
|
win_free_pixmap(win, tns->thumbs[i].pm);
|
|
|
|
|
|
|
|
free(tns->thumbs);
|
|
|
|
tns->thumbs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tns_render(tns_t *tns, win_t *win) {
|
|
|
|
int i, cnt, x, y;
|
|
|
|
|
|
|
|
if (!tns || !win)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tns->cols = win->w / thumb_dim;
|
|
|
|
tns->rows = win->h / thumb_dim;
|
|
|
|
|
|
|
|
cnt = tns->cols * tns->rows;
|
|
|
|
if (tns->first && tns->first + cnt > tns->cnt)
|
|
|
|
tns->first = MAX(0, tns->cnt - cnt);
|
|
|
|
cnt = MIN(tns->first + cnt, tns->cnt);
|
|
|
|
|
|
|
|
win_clear(win);
|
|
|
|
|
|
|
|
x = y = 5;
|
|
|
|
i = tns->first;
|
|
|
|
|
|
|
|
while (i < cnt) {
|
2011-02-16 23:03:42 +01:00
|
|
|
if (!tns->thumbs[i].loaded)
|
|
|
|
continue;
|
|
|
|
|
2011-02-16 21:40:20 +01:00
|
|
|
tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
|
|
|
|
tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
|
|
|
|
win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
|
|
|
|
tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
|
|
|
|
if (++i % tns->cols == 0) {
|
|
|
|
x = 5;
|
|
|
|
y += thumb_dim;
|
|
|
|
} else {
|
|
|
|
x += thumb_dim;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
win_draw(win);
|
|
|
|
}
|
|
|
|
|