A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

image.c 15 KiB

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