A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

716 lines
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 || !win)
  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)))
  67. return;
  68. entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_ORIENTATION);
  69. if (entry) {
  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)
  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) {
  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)
  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) {
  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 || !file || !file->name || !file->path)
  248. return false;
  249. if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) {
  250. warn("could not open image: %s", file->name);
  251. return false;
  252. }
  253. imlib_context_set_image(img->im);
  254. imlib_image_set_changes_on_disk();
  255. imlib_context_set_anti_alias(img->aa);
  256. fmt = imlib_image_format();
  257. /* avoid unused-but-set-variable warning */
  258. (void) fmt;
  259. #if EXIF_SUPPORT
  260. if (streq(fmt, "jpeg"))
  261. exif_auto_orientate(file);
  262. #endif
  263. #if GIF_SUPPORT
  264. if (streq(fmt, "gif"))
  265. img_load_gif(img, file);
  266. #endif
  267. img->w = imlib_image_get_width();
  268. img->h = imlib_image_get_height();
  269. img->scalemode = options->scalemode;
  270. img->re = false;
  271. img->checkpan = false;
  272. img->dirty = true;
  273. return true;
  274. }
  275. void img_close(img_t *img, bool decache) {
  276. int i;
  277. if (!img)
  278. return;
  279. if (img->multi.cnt) {
  280. for (i = 0; i < img->multi.cnt; i++) {
  281. imlib_context_set_image(img->multi.frames[i].im);
  282. imlib_free_image();
  283. }
  284. img->multi.cnt = 0;
  285. img->im = NULL;
  286. } else if (img->im) {
  287. imlib_context_set_image(img->im);
  288. if (decache)
  289. imlib_free_image_and_decache();
  290. else
  291. imlib_free_image();
  292. img->im = NULL;
  293. }
  294. }
  295. void img_check_pan(img_t *img, bool moved) {
  296. win_t *win;
  297. int ox, oy;
  298. if (!img || !img->im || !img->win)
  299. return;
  300. win = img->win;
  301. ox = img->x;
  302. oy = img->y;
  303. if (img->w * img->zoom > win->w) {
  304. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  305. img->x = 0;
  306. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  307. img->x = win->w - img->w * img->zoom;
  308. } else {
  309. img->x = (win->w - img->w * img->zoom) / 2;
  310. }
  311. if (img->h * img->zoom > win->h) {
  312. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  313. img->y = 0;
  314. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  315. img->y = win->h - img->h * img->zoom;
  316. } else {
  317. img->y = (win->h - img->h * img->zoom) / 2;
  318. }
  319. if (!moved && (ox != img->x || oy != img->y))
  320. img->dirty = true;
  321. }
  322. bool img_fit(img_t *img) {
  323. float z, zmax, zw, zh;
  324. if (!img || !img->im || !img->win)
  325. return false;
  326. if (img->scalemode == SCALE_ZOOM)
  327. return false;
  328. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  329. zw = (float) img->win->w / (float) img->w;
  330. zh = (float) img->win->h / (float) img->h;
  331. z = MIN(zw, zh);
  332. z = MAX(z, zoom_min);
  333. z = MIN(z, zmax);
  334. if (ZOOMDIFF(z, img->zoom)) {
  335. img->zoom = z;
  336. img->dirty = true;
  337. return true;
  338. } else {
  339. return false;
  340. }
  341. }
  342. void img_render(img_t *img) {
  343. win_t *win;
  344. int sx, sy, sw, sh;
  345. int dx, dy, dw, dh;
  346. if (!img || !img->im || !img->win)
  347. return;
  348. win = img->win;
  349. img_fit(img);
  350. if (!img->re) {
  351. /* rendered for the first time */
  352. img->re = true;
  353. if (img->zoom * img->w <= win->w)
  354. img->x = (win->w - img->w * img->zoom) / 2;
  355. else
  356. img->x = 0;
  357. if (img->zoom * img->h <= win->h)
  358. img->y = (win->h - img->h * img->zoom) / 2;
  359. else
  360. img->y = 0;
  361. }
  362. if (img->checkpan) {
  363. img_check_pan(img, false);
  364. img->checkpan = false;
  365. }
  366. if (!img->dirty)
  367. return;
  368. /* calculate source and destination offsets */
  369. if (img->x < 0) {
  370. sx = -img->x / img->zoom;
  371. sw = win->w / img->zoom;
  372. dx = 0;
  373. dw = win->w;
  374. } else {
  375. sx = 0;
  376. sw = img->w;
  377. dx = img->x;
  378. dw = img->w * img->zoom;
  379. }
  380. if (img->y < 0) {
  381. sy = -img->y / img->zoom;
  382. sh = win->h / img->zoom;
  383. dy = 0;
  384. dh = win->h;
  385. } else {
  386. sy = 0;
  387. sh = img->h;
  388. dy = img->y;
  389. dh = img->h * img->zoom;
  390. }
  391. win_clear(win);
  392. imlib_context_set_image(img->im);
  393. if (imlib_image_has_alpha() && !img->alpha)
  394. win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
  395. imlib_context_set_drawable(win->pm);
  396. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  397. win_draw(win);
  398. img->dirty = false;
  399. }
  400. bool img_fit_win(img_t *img) {
  401. if (!img || !img->im)
  402. return false;
  403. img->scalemode = SCALE_FIT;
  404. return img_fit(img);
  405. }
  406. bool img_center(img_t *img) {
  407. int ox, oy;
  408. if (!img || !img->im || !img->win)
  409. return false;
  410. ox = img->x;
  411. oy = img->y;
  412. img->x = (img->win->w - img->w * img->zoom) / 2;
  413. img->y = (img->win->h - img->h * img->zoom) / 2;
  414. if (ox != img->x || oy != img->y) {
  415. img->dirty = true;
  416. return true;
  417. } else {
  418. return false;
  419. }
  420. }
  421. bool img_zoom(img_t *img, float z) {
  422. if (!img || !img->im || !img->win)
  423. return false;
  424. z = MAX(z, zoom_min);
  425. z = MIN(z, zoom_max);
  426. img->scalemode = SCALE_ZOOM;
  427. if (ZOOMDIFF(z, img->zoom)) {
  428. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
  429. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
  430. img->zoom = z;
  431. img->checkpan = true;
  432. img->dirty = true;
  433. return true;
  434. } else {
  435. return false;
  436. }
  437. }
  438. bool img_zoom_in(img_t *img) {
  439. int i;
  440. if (!img || !img->im)
  441. return false;
  442. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  443. if (zoom_levels[i] > img->zoom * 100.0)
  444. return img_zoom(img, zoom_levels[i] / 100.0);
  445. }
  446. return false;
  447. }
  448. bool img_zoom_out(img_t *img) {
  449. int i;
  450. if (!img || !img->im)
  451. return false;
  452. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  453. if (zoom_levels[i] < img->zoom * 100.0)
  454. return img_zoom(img, zoom_levels[i] / 100.0);
  455. }
  456. return false;
  457. }
  458. bool img_move(img_t *img, int dx, int dy) {
  459. int ox, oy;
  460. if (!img || !img->im)
  461. return false;
  462. ox = img->x;
  463. oy = img->y;
  464. img->x += dx;
  465. img->y += dy;
  466. img_check_pan(img, true);
  467. if (ox != img->x || oy != img->y) {
  468. img->dirty = true;
  469. return true;
  470. } else {
  471. return false;
  472. }
  473. }
  474. bool img_pan(img_t *img, direction_t dir, bool screen) {
  475. if (!img || !img->im || !img->win)
  476. return false;
  477. switch (dir) {
  478. case DIR_LEFT:
  479. return img_move(img, img->win->w / (screen ? 1 : 5), 0);
  480. case DIR_RIGHT:
  481. return img_move(img, img->win->w / (screen ? 1 : 5) * -1, 0);
  482. case DIR_UP:
  483. return img_move(img, 0, img->win->h / (screen ? 1 : 5));
  484. case DIR_DOWN:
  485. return img_move(img, 0, img->win->h / (screen ? 1 : 5) * -1);
  486. }
  487. return false;
  488. }
  489. bool img_pan_edge(img_t *img, direction_t dir) {
  490. int ox, oy;
  491. if (!img || !img->im || !img->win)
  492. return false;
  493. ox = img->x;
  494. oy = img->y;
  495. switch (dir) {
  496. case DIR_LEFT:
  497. img->x = 0;
  498. break;
  499. case DIR_RIGHT:
  500. img->x = img->win->w - img->w * img->zoom;
  501. break;
  502. case DIR_UP:
  503. img->y = 0;
  504. break;
  505. case DIR_DOWN:
  506. img->y = img->win->h - img->h * img->zoom;
  507. break;
  508. }
  509. img_check_pan(img, true);
  510. if (ox != img->x || oy != img->y) {
  511. img->dirty = true;
  512. return true;
  513. } else {
  514. return false;
  515. }
  516. }
  517. void img_rotate(img_t *img, int d) {
  518. win_t *win;
  519. int ox, oy, tmp;
  520. if (!img || !img->im || !img->win)
  521. return;
  522. win = img->win;
  523. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  524. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  525. imlib_context_set_image(img->im);
  526. imlib_image_orientate(d);
  527. img->x = oy + (win->w - win->h) / 2;
  528. img->y = ox + (win->h - win->w) / 2;
  529. tmp = img->w;
  530. img->w = img->h;
  531. img->h = tmp;
  532. img->checkpan = true;
  533. img->dirty = true;
  534. }
  535. void img_rotate_left(img_t *img) {
  536. img_rotate(img, 3);
  537. }
  538. void img_rotate_right(img_t *img) {
  539. img_rotate(img, 1);
  540. }
  541. void img_toggle_antialias(img_t *img) {
  542. if (!img || !img->im)
  543. return;
  544. img->aa = !img->aa;
  545. imlib_context_set_image(img->im);
  546. imlib_context_set_anti_alias(img->aa);
  547. img->dirty = true;
  548. }
  549. bool img_frame_goto(img_t *img, int n) {
  550. if (!img || !img->im)
  551. return false;
  552. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  553. return false;
  554. img->multi.sel = n;
  555. img->im = img->multi.frames[n].im;
  556. imlib_context_set_image(img->im);
  557. img->w = imlib_image_get_width();
  558. img->h = imlib_image_get_height();
  559. img->checkpan = true;
  560. img->dirty = true;
  561. return true;
  562. }
  563. bool img_frame_navigate(img_t *img, int d) {
  564. if (!img || !img->im || !img->multi.cnt || !d)
  565. return false;
  566. d += img->multi.sel;
  567. if (d < 0)
  568. d = 0;
  569. else if (d >= img->multi.cnt)
  570. d = img->multi.cnt - 1;
  571. return img_frame_goto(img, d);
  572. }
  573. bool img_frame_animate(img_t *img, bool restart) {
  574. if (!img || !img->im || !img->multi.cnt)
  575. return false;
  576. if (img->multi.sel + 1 >= img->multi.cnt) {
  577. if (restart || (GIF_LOOP && !img->slideshow)) {
  578. img_frame_goto(img, 0);
  579. } else {
  580. img->multi.animate = false;
  581. return false;
  582. }
  583. } else if (!restart) {
  584. img_frame_goto(img, img->multi.sel + 1);
  585. }
  586. img->multi.animate = true;
  587. img->dirty = true;
  588. return true;
  589. }