A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

736 linhas
16 KiB

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