A Simple X Image Viewer
 
 
 
 
 
 

718 line
15 KiB

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