A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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