Krolyxon
Commit 090ee54

Branches

Refs →

nsxiv090ee54

Save rotated png-files with S-key

Author
Bert
Date
2011-02-27 01:48 +0100
Parent
dc68127
Commit
090ee5405b70d6dfa16a8ca3a691dbd556c41bdb

+81 −50

6 files changed

  1. M Makefile +1 −1
  2. M image.c +33 −4
  3. M image.h +3 −0
  4. M main.c +28 −40
  5. M thumbs.c +14 −4
  6. M thumbs.h +2 −1
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=0.7
3+VERSION=git-20110227
44
55 CC?=gcc
66 PREFIX?=/usr/local
M image.c View
@@ -16,6 +16,7 @@
1616 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717 */
1818
19+#include <string.h>
1920 #include <unistd.h>
2021
2122 #include "config.h"
@@ -28,6 +29,9 @@ int zl_cnt;
2829 float zoom_min;
2930 float zoom_max;
3031
32+const short ori_left[8] = { 8, 7, 6, 5, 2, 1, 4, 3 };
33+const short ori_right[8] = { 6, 5, 8, 7, 4, 3, 2, 1 };
34+
3135 Imlib_Image *im_broken;
3236
3337 void img_init(img_t *img, win_t *win) {
@@ -90,8 +94,8 @@ int img_load(img_t *img, const char *filename) {
9094 img->scalemode = SCALE_DOWN;
9195 }
9296
93− img->re = 0;
94− img->checkpan = 0;
97+ img->ori = img->o_ori = 1;
98+ img->re = img->checkpan = 0;
9599
96100 img->w = imlib_image_get_width();
97101 img->h = imlib_image_get_height();
@@ -99,6 +103,25 @@ int img_load(img_t *img, const char *filename) {
99103 return 1;
100104 }
101105
106+int img_save(img_t *img) {
107+ const char *fmt;
108+
109+ if (!img || !img->im)
110+ return 0;
111+
112+ imlib_context_set_image(img->im);
113+
114+ if (img->ori != img->o_ori) {
115+ fmt = imlib_image_format();
116+ if (strcmp(fmt, "png") == 0) {
117+ imlib_save_image(imlib_image_get_filename());
118+ return 1;
119+ }
120+ }
121+
122+ return 0;
123+}
124+
102125 void img_close(img_t *img) {
103126 if (img && img->im) {
104127 imlib_context_set_image(img->im);
@@ -341,11 +364,17 @@ void img_rotate(img_t *img, win_t *win, int d) {
341364 }
342365
343366 void img_rotate_left(img_t *img, win_t *win) {
344− img_rotate(img, win, 3);
367+ if (img) {
368+ img_rotate(img, win, 3);
369+ img->ori = ori_left[img->ori];
370+ }
345371 }
346372
347373 void img_rotate_right(img_t *img, win_t *win) {
348− img_rotate(img, win, 1);
374+ if (img) {
375+ img_rotate(img, win, 1);
376+ img->ori = ori_right[img->ori];
377+ }
349378 }
350379
351380 void img_toggle_antialias(img_t *img) {
M image.h View
@@ -41,6 +41,8 @@ typedef struct img_s {
4141
4242 float zoom;
4343 scalemode_t scalemode;
44+ short ori;
45+ short o_ori;
4446
4547 unsigned char re;
4648 unsigned char checkpan;
@@ -57,6 +59,7 @@ void img_free(img_t*);
5759
5860 int img_check(const char*);
5961 int img_load(img_t*, const char*);
62+int img_save(img_t*);
6063 void img_close(img_t*);
6164
6265 void img_render(img_t*, win_t*);
M main.c View
@@ -69,10 +69,11 @@ void cleanup() {
6969 }
7070 }
7171
72−int load_image() {
72+int load_image(int new) {
7373 struct stat fstats;
7474
7575 img_close(&img);
76+ fileidx = new;
7677
7778 if (!stat(filenames[fileidx], &fstats))
7879 filesize = fstats.st_size;
@@ -140,7 +141,7 @@ int main(int argc, char **argv) {
140141 } else {
141142 mode = MODE_NORMAL;
142143 tns.thumbs = NULL;
143− load_image();
144+ load_image(fileidx);
144145 img_render(&img, &win);
145146 }
146147
@@ -307,41 +308,29 @@ void on_keypress(XKeyEvent *kev) {
307308 /* navigate image list */
308309 case XK_n:
309310 case XK_space:
310− if (fileidx + 1 < filecnt) {
311− ++fileidx;
312− changed = load_image();
313− }
311+ if (fileidx + 1 < filecnt)
312+ changed = load_image(fileidx + 1);
314313 break;
315314 case XK_p:
316315 case XK_BackSpace:
317− if (fileidx > 0) {
318− --fileidx;
319− changed = load_image();
320− }
316+ if (fileidx > 0)
317+ changed = load_image(fileidx - 1);
321318 break;
322319 case XK_bracketleft:
323− if (fileidx != 0) {
324− fileidx = MAX(0, fileidx - 10);
325− changed = load_image();
326− }
320+ if (fileidx != 0)
321+ changed = load_image(MAX(0, fileidx - 10));
327322 break;
328323 case XK_bracketright:
329− if (fileidx != filecnt - 1) {
330− fileidx = MIN(fileidx + 10, filecnt - 1);
331− changed = load_image();
332− }
324+ if (fileidx != filecnt - 1)
325+ changed = load_image(MIN(fileidx + 10, filecnt - 1));
333326 break;
334327 case XK_g:
335− if (fileidx != 0) {
336− fileidx = 0;
337− changed = load_image();
338− }
328+ if (fileidx != 0)
329+ changed = load_image(0);
339330 break;
340331 case XK_G:
341− if (fileidx != filecnt - 1) {
342− fileidx = filecnt - 1;
343− changed = load_image();
344− }
332+ if (fileidx != filecnt - 1)
333+ changed = load_image(filecnt - 1);
345334 break;
346335
347336 /* zooming */
@@ -418,7 +407,11 @@ void on_keypress(XKeyEvent *kev) {
418407 changed = 1;
419408 break;
420409 case XK_r:
421− changed = load_image();
410+ changed = load_image(fileidx);
411+ break;
412+ case XK_S:
413+ if (img_save(&img))
414+ tns_load(&tns, &win, fileidx, filenames[fileidx]);
422415 break;
423416 }
424417 } else {
@@ -426,8 +419,7 @@ void on_keypress(XKeyEvent *kev) {
426419 switch (ksym) {
427420 /* open selected image */
428421 case XK_Return:
429− fileidx = tns.sel;
430− load_image();
422+ load_image(tns.sel);
431423 mode = MODE_NORMAL;
432424 win_set_cursor(&win, CURSOR_NONE);
433425 changed = 1;
@@ -496,10 +488,8 @@ void on_buttonpress(XButtonEvent *bev) {
496488 if (mode == MODE_NORMAL) {
497489 switch (bev->button) {
498490 case Button1:
499− if (fileidx + 1 < filecnt) {
500− ++fileidx;
501− changed = load_image();
502− }
491+ if (fileidx + 1 < filecnt)
492+ changed = load_image(fileidx + 1);
503493 break;
504494 case Button2:
505495 mox = bev->x;
@@ -509,10 +499,8 @@ void on_buttonpress(XButtonEvent *bev) {
509499 drag = 1;
510500 break;
511501 case Button3:
512− if (fileidx > 0) {
513− --fileidx;
514− changed = load_image();
515− }
502+ if (fileidx > 0)
503+ changed = load_image(fileidx - 1);
516504 break;
517505 case Button4:
518506 if (mask == ControlMask)
@@ -543,8 +531,7 @@ void on_buttonpress(XButtonEvent *bev) {
543531 case Button1:
544532 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
545533 if (sel == tns.sel) {
546− fileidx = tns.sel;
547− load_image();
534+ load_image(tns.sel);
548535 mode = MODE_NORMAL;
549536 timo_cursor = TO_CURSOR_HIDE;
550537 } else {
@@ -600,7 +587,8 @@ void run() {
600587 gettimeofday(&t0, 0);
601588
602589 while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
603− tns_load(&tns, &win, filenames[tns.cnt]);
590+ /* tns.cnt is increased inside tns_load */
591+ tns_load(&tns, &win, tns.cnt, filenames[tns.cnt]);
604592 gettimeofday(&t1, 0);
605593 if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25)
606594 break;
M thumbs.c View
@@ -35,6 +35,7 @@ void tns_init(tns_t *tns, int cnt) {
3535 tns->cnt = tns->first = tns->sel = 0;
3636 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
3737 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
38+ tns->cap = cnt;
3839 tns->dirty = 0;
3940 }
4041
@@ -51,7 +52,7 @@ void tns_free(tns_t *tns, win_t *win) {
5152 tns->thumbs = NULL;
5253 }
5354
54−void tns_load(tns_t *tns, win_t *win, const char *filename) {
55+void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
5556 int w, h;
5657 float z, zw, zh;
5758 thumb_t *t;
@@ -60,10 +61,17 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
6061 if (!tns || !win || !filename)
6162 return;
6263
63− if ((im = imlib_load_image(filename)))
64+ if (n >= tns->cap)
65+ return;
66+ else if (n >= tns->cnt)
67+ tns->cnt = n + 1;
68+
69+ if ((im = imlib_load_image(filename))) {
6470 imlib_context_set_image(im);
65− else
71+ imlib_image_set_changes_on_disk();
72+ } else {
6673 imlib_context_set_image(im_broken);
74+ }
6775
6876 w = imlib_image_get_width();
6977 h = imlib_image_get_height();
@@ -73,10 +81,12 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
7381 if (!im && z > 1.0)
7482 z = 1.0;
7583
76− t = &tns->thumbs[tns->cnt++];
84+ t = &tns->thumbs[n];
7785 t->w = z * w;
7886 t->h = z * h;
7987
88+ if (t->pm)
89+ win_free_pixmap(win, t->pm);
8090 t->pm = win_create_pixmap(win, t->w, t->h);
8191 imlib_context_set_drawable(t->pm);
8292 imlib_context_set_anti_alias(1);
M thumbs.h View
@@ -38,6 +38,7 @@ typedef struct thumb_s {
3838
3939 typedef struct tns_s {
4040 thumb_t *thumbs;
41+ int cap;
4142 int cnt;
4243 int x;
4344 int y;
@@ -51,7 +52,7 @@ typedef struct tns_s {
5152 void tns_init(tns_t*, int);
5253 void tns_free(tns_t*, win_t*);
5354
54−void tns_load(tns_t*, win_t*, const char*);
55+void tns_load(tns_t*, win_t*, int, const char*);
5556
5657 void tns_render(tns_t*, win_t*);
5758 void tns_highlight(tns_t*, win_t*, int, Bool);