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.
 
 
 
 
 
 

716 linhas
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. }
  152. ext = NULL;
  153. DGifGetExtensionNext(gif, &ext);
  154. }
  155. } else if (rec == IMAGE_DESC_RECORD_TYPE) {
  156. if (DGifGetImageDesc(gif) == GIF_ERROR) {
  157. err = true;
  158. break;
  159. }
  160. x = gif->Image.Left;
  161. y = gif->Image.Top;
  162. w = gif->Image.Width;
  163. h = gif->Image.Height;
  164. rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
  165. for (i = 0; i < h; i++)
  166. rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
  167. if (gif->Image.Interlace) {
  168. for (i = 0; i < 4; i++) {
  169. for (j = intoffset[i]; j < h; j += intjump[i])
  170. DGifGetLine(gif, rows[j], w);
  171. }
  172. } else {
  173. for (i = 0; i < h; i++)
  174. DGifGetLine(gif, rows[i], w);
  175. }
  176. ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
  177. cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
  178. r = cmap->Colors[bg].Red;
  179. g = cmap->Colors[bg].Green;
  180. b = cmap->Colors[bg].Blue;
  181. bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
  182. for (i = 0; i < sh; i++) {
  183. for (j = 0; j < sw; j++) {
  184. if (i < y || i >= y + h || j < x || j >= x + w) {
  185. if (transp >= 0 && prev_frame)
  186. *ptr = prev_frame[i * sw + j];
  187. else
  188. *ptr = bgpixel;
  189. } else if (rows[i-y][j-x] == transp) {
  190. if (prev_frame)
  191. *ptr = prev_frame[i * sw + j];
  192. else
  193. *ptr = bgpixel;
  194. } else {
  195. r = cmap->Colors[rows[i-y][j-x]].Red;
  196. g = cmap->Colors[rows[i-y][j-x]].Green;
  197. b = cmap->Colors[rows[i-y][j-x]].Blue;
  198. *ptr = 0xff << 24 | r << 16 | g << 8 | b;
  199. }
  200. ptr++;
  201. }
  202. }
  203. im = imlib_create_image_using_copied_data(sw, sh, data);
  204. for (i = 0; i < h; i++)
  205. free(rows[i]);
  206. free(rows);
  207. free(data);
  208. if (!im) {
  209. err = true;
  210. break;
  211. }
  212. imlib_context_set_image(im);
  213. prev_frame = imlib_image_get_data_for_reading_only();
  214. imlib_image_set_format("gif");
  215. if (transp >= 0)
  216. imlib_image_set_has_alpha(1);
  217. if (img->multi.cnt == img->multi.cap) {
  218. img->multi.cap *= 2;
  219. img->multi.frames = (img_frame_t*)
  220. s_realloc(img->multi.frames,
  221. img->multi.cap * sizeof(img_frame_t));
  222. }
  223. img->multi.frames[img->multi.cnt].im = im;
  224. img->multi.frames[img->multi.cnt].delay = delay ? delay : GIF_DELAY;
  225. img->multi.cnt++;
  226. }
  227. } while (rec != TERMINATE_RECORD_TYPE);
  228. DGifCloseFile(gif);
  229. if (err && !file->loaded)
  230. warn("corrupted gif file: %s", file->name);
  231. if (img->multi.cnt > 1) {
  232. imlib_context_set_image(img->im);
  233. imlib_free_image();
  234. img->im = img->multi.frames[0].im;
  235. img->multi.animate = GIF_AUTOPLAY;
  236. } else if (img->multi.cnt == 1) {
  237. imlib_context_set_image(img->multi.frames[0].im);
  238. imlib_free_image();
  239. img->multi.cnt = 0;
  240. img->multi.animate = false;
  241. }
  242. imlib_context_set_image(img->im);
  243. return !err;
  244. }
  245. #endif /* GIF_SUPPORT */
  246. bool img_load(img_t *img, const fileinfo_t *file) {
  247. const char *fmt;
  248. if (!img || !file || !file->name || !file->path)
  249. return false;
  250. if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) {
  251. warn("could not open image: %s", file->name);
  252. return false;
  253. }
  254. imlib_context_set_image(img->im);
  255. imlib_image_set_changes_on_disk();
  256. imlib_context_set_anti_alias(img->aa);
  257. fmt = imlib_image_format();
  258. /* avoid unused-but-set-variable warning */
  259. (void) fmt;
  260. #if EXIF_SUPPORT
  261. if (!strcmp(fmt, "jpeg"))
  262. exif_auto_orientate(file);
  263. #endif
  264. #if GIF_SUPPORT
  265. if (!strcmp(fmt, "gif"))
  266. img_load_gif(img, file);
  267. #endif
  268. img->w = imlib_image_get_width();
  269. img->h = imlib_image_get_height();
  270. img->scalemode = options->scalemode;
  271. img->re = false;
  272. img->checkpan = false;
  273. img->dirty = true;
  274. return true;
  275. }
  276. void img_close(img_t *img, bool decache) {
  277. int i;
  278. if (!img)
  279. return;
  280. if (img->multi.cnt) {
  281. for (i = 0; i < img->multi.cnt; i++) {
  282. imlib_context_set_image(img->multi.frames[i].im);
  283. imlib_free_image();
  284. }
  285. img->multi.cnt = 0;
  286. img->im = NULL;
  287. } else if (img->im) {
  288. imlib_context_set_image(img->im);
  289. if (decache)
  290. imlib_free_image_and_decache();
  291. else
  292. imlib_free_image();
  293. img->im = NULL;
  294. }
  295. }
  296. void img_check_pan(img_t *img, bool moved) {
  297. win_t *win;
  298. int ox, oy;
  299. if (!img || !img->im || !img->win)
  300. return;
  301. win = img->win;
  302. ox = img->x;
  303. oy = img->y;
  304. if (img->w * img->zoom > win->w) {
  305. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  306. img->x = 0;
  307. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  308. img->x = win->w - img->w * img->zoom;
  309. } else {
  310. img->x = (win->w - img->w * img->zoom) / 2;
  311. }
  312. if (img->h * img->zoom > win->h) {
  313. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  314. img->y = 0;
  315. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  316. img->y = win->h - img->h * img->zoom;
  317. } else {
  318. img->y = (win->h - img->h * img->zoom) / 2;
  319. }
  320. if (!moved && (ox != img->x || oy != img->y))
  321. img->dirty = true;
  322. }
  323. bool img_fit(img_t *img) {
  324. float z, zmax, zw, zh;
  325. if (!img || !img->im || !img->win)
  326. return false;
  327. if (img->scalemode == SCALE_ZOOM)
  328. return false;
  329. zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
  330. zw = (float) img->win->w / (float) img->w;
  331. zh = (float) img->win->h / (float) img->h;
  332. z = MIN(zw, zh);
  333. z = MAX(z, zoom_min);
  334. z = MIN(z, zmax);
  335. if (ZOOMDIFF(z, img->zoom)) {
  336. img->zoom = z;
  337. img->dirty = true;
  338. return true;
  339. } else {
  340. return false;
  341. }
  342. }
  343. void img_render(img_t *img) {
  344. win_t *win;
  345. int sx, sy, sw, sh;
  346. int dx, dy, dw, dh;
  347. if (!img || !img->im || !img->win)
  348. return;
  349. win = img->win;
  350. img_fit(img);
  351. if (!img->re) {
  352. /* rendered for the first time */
  353. img->re = true;
  354. if (img->zoom * img->w <= win->w)
  355. img->x = (win->w - img->w * img->zoom) / 2;
  356. else
  357. img->x = 0;
  358. if (img->zoom * img->h <= win->h)
  359. img->y = (win->h - img->h * img->zoom) / 2;
  360. else
  361. img->y = 0;
  362. }
  363. if (img->checkpan) {
  364. img_check_pan(img, false);
  365. img->checkpan = false;
  366. }
  367. if (!img->dirty)
  368. return;
  369. /* calculate source and destination offsets */
  370. if (img->x < 0) {
  371. sx = -img->x / img->zoom;
  372. sw = win->w / img->zoom;
  373. dx = 0;
  374. dw = win->w;
  375. } else {
  376. sx = 0;
  377. sw = img->w;
  378. dx = img->x;
  379. dw = img->w * img->zoom;
  380. }
  381. if (img->y < 0) {
  382. sy = -img->y / img->zoom;
  383. sh = win->h / img->zoom;
  384. dy = 0;
  385. dh = win->h;
  386. } else {
  387. sy = 0;
  388. sh = img->h;
  389. dy = img->y;
  390. dh = img->h * img->zoom;
  391. }
  392. win_clear(win);
  393. imlib_context_set_image(img->im);
  394. if (imlib_image_has_alpha() && !img->alpha)
  395. win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
  396. imlib_context_set_drawable(win->pm);
  397. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  398. win_draw(win);
  399. img->dirty = false;
  400. }
  401. bool img_fit_win(img_t *img) {
  402. if (!img || !img->im)
  403. return false;
  404. img->scalemode = SCALE_FIT;
  405. return img_fit(img);
  406. }
  407. bool img_center(img_t *img) {
  408. int ox, oy;
  409. if (!img || !img->im || !img->win)
  410. return false;
  411. ox = img->x;
  412. oy = img->y;
  413. img->x = (img->win->w - img->w * img->zoom) / 2;
  414. img->y = (img->win->h - img->h * img->zoom) / 2;
  415. if (ox != img->x || oy != img->y) {
  416. img->dirty = true;
  417. return true;
  418. } else {
  419. return false;
  420. }
  421. }
  422. bool img_zoom(img_t *img, float z) {
  423. if (!img || !img->im || !img->win)
  424. return false;
  425. z = MAX(z, zoom_min);
  426. z = MIN(z, zoom_max);
  427. img->scalemode = SCALE_ZOOM;
  428. if (ZOOMDIFF(z, img->zoom)) {
  429. img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
  430. img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
  431. img->zoom = z;
  432. img->checkpan = true;
  433. img->dirty = true;
  434. return true;
  435. } else {
  436. return false;
  437. }
  438. }
  439. bool img_zoom_in(img_t *img) {
  440. int i;
  441. if (!img || !img->im)
  442. return false;
  443. for (i = 1; i < ARRLEN(zoom_levels); i++) {
  444. if (zoom_levels[i] > img->zoom * 100.0)
  445. return img_zoom(img, zoom_levels[i] / 100.0);
  446. }
  447. return false;
  448. }
  449. bool img_zoom_out(img_t *img) {
  450. int i;
  451. if (!img || !img->im)
  452. return false;
  453. for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
  454. if (zoom_levels[i] < img->zoom * 100.0)
  455. return img_zoom(img, zoom_levels[i] / 100.0);
  456. }
  457. return false;
  458. }
  459. bool img_move(img_t *img, int dx, int dy) {
  460. int ox, oy;
  461. if (!img || !img->im)
  462. return false;
  463. ox = img->x;
  464. oy = img->y;
  465. img->x += dx;
  466. img->y += dy;
  467. img_check_pan(img, true);
  468. if (ox != img->x || oy != img->y) {
  469. img->dirty = true;
  470. return true;
  471. } else {
  472. return false;
  473. }
  474. }
  475. bool img_pan(img_t *img, direction_t dir, bool screen) {
  476. if (!img || !img->im || !img->win)
  477. return false;
  478. switch (dir) {
  479. case DIR_LEFT:
  480. return img_move(img, img->win->w / (screen ? 1 : 5), 0);
  481. case DIR_RIGHT:
  482. return img_move(img, img->win->w / (screen ? 1 : 5) * -1, 0);
  483. case DIR_UP:
  484. return img_move(img, 0, img->win->h / (screen ? 1 : 5));
  485. case DIR_DOWN:
  486. return img_move(img, 0, img->win->h / (screen ? 1 : 5) * -1);
  487. }
  488. return false;
  489. }
  490. bool img_pan_edge(img_t *img, direction_t dir) {
  491. int ox, oy;
  492. if (!img || !img->im || !img->win)
  493. return false;
  494. ox = img->x;
  495. oy = img->y;
  496. switch (dir) {
  497. case DIR_LEFT:
  498. img->x = 0;
  499. break;
  500. case DIR_RIGHT:
  501. img->x = img->win->w - img->w * img->zoom;
  502. break;
  503. case DIR_UP:
  504. img->y = 0;
  505. break;
  506. case DIR_DOWN:
  507. img->y = img->win->h - img->h * img->zoom;
  508. break;
  509. }
  510. img_check_pan(img, true);
  511. if (ox != img->x || oy != img->y) {
  512. img->dirty = true;
  513. return true;
  514. } else {
  515. return false;
  516. }
  517. }
  518. void img_rotate(img_t *img, int d) {
  519. win_t *win;
  520. int ox, oy, tmp;
  521. if (!img || !img->im || !img->win)
  522. return;
  523. win = img->win;
  524. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  525. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  526. imlib_context_set_image(img->im);
  527. imlib_image_orientate(d);
  528. img->x = oy + (win->w - win->h) / 2;
  529. img->y = ox + (win->h - win->w) / 2;
  530. tmp = img->w;
  531. img->w = img->h;
  532. img->h = tmp;
  533. img->checkpan = true;
  534. img->dirty = true;
  535. }
  536. void img_rotate_left(img_t *img) {
  537. img_rotate(img, 3);
  538. }
  539. void img_rotate_right(img_t *img) {
  540. img_rotate(img, 1);
  541. }
  542. void img_toggle_antialias(img_t *img) {
  543. if (!img || !img->im)
  544. return;
  545. img->aa = !img->aa;
  546. imlib_context_set_image(img->im);
  547. imlib_context_set_anti_alias(img->aa);
  548. img->dirty = true;
  549. }
  550. bool img_frame_goto(img_t *img, int n) {
  551. if (!img || !img->im)
  552. return false;
  553. if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
  554. return false;
  555. img->multi.sel = n;
  556. img->im = img->multi.frames[n].im;
  557. imlib_context_set_image(img->im);
  558. img->w = imlib_image_get_width();
  559. img->h = imlib_image_get_height();
  560. img->checkpan = true;
  561. img->dirty = true;
  562. return true;
  563. }
  564. bool img_frame_navigate(img_t *img, int d) {
  565. if (!img || !img->im || !img->multi.cnt || !d)
  566. return false;
  567. d += img->multi.sel;
  568. if (d < 0)
  569. d = 0;
  570. else if (d >= img->multi.cnt)
  571. d = img->multi.cnt - 1;
  572. return img_frame_goto(img, d);
  573. }
  574. bool img_frame_animate(img_t *img, bool restart) {
  575. if (!img || !img->im || !img->multi.cnt)
  576. return false;
  577. if (img->multi.sel + 1 >= img->multi.cnt) {
  578. if (restart || (GIF_LOOP && !img->slideshow)) {
  579. img_frame_goto(img, 0);
  580. } else {
  581. img->multi.animate = false;
  582. return false;
  583. }
  584. } else if (!restart) {
  585. img_frame_goto(img, img->multi.sel + 1);
  586. }
  587. img->multi.animate = true;
  588. img->dirty = true;
  589. return true;
  590. }