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.
 
 
 
 
 
 

705 lines
15 KiB

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