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.
 
 
 
 
 
 

572 lines
12 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
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _IMAGE_CONFIG
  19. #include <unistd.h>
  20. #ifdef HAVE_GIFLIB
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <gif_lib.h>
  25. #endif
  26. #include "image.h"
  27. #include "options.h"
  28. #include "util.h"
  29. #include "config.h"
  30. int zl_cnt;
  31. float zoom_min;
  32. float zoom_max;
  33. void img_init(img_t *img, win_t *win) {
  34. zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  35. zoom_min = zoom_levels[0] / 100.0;
  36. zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
  37. if (img) {
  38. img->im = NULL;
  39. img->multi.cap = img->multi.cnt = 0;
  40. img->zoom = options->zoom;
  41. img->zoom = MAX(img->zoom, zoom_min);
  42. img->zoom = MIN(img->zoom, zoom_max);
  43. img->aa = options->aa;
  44. img->alpha = 1;
  45. }
  46. if (win) {
  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. }
  51. }
  52. #ifdef HAVE_GIFLIB
  53. int img_load_gif(img_t *img, const fileinfo_t *file) {
  54. GifFileType *gif;
  55. GifRowType *rows = NULL;
  56. GifRecordType rec;
  57. ColorMapObject *cmap;
  58. DATA32 bgpixel, *data, *ptr;
  59. DATA32 *prev_frame = NULL;
  60. Imlib_Image *im;
  61. int i, j, bg, r, g, b;
  62. int x, y, w, h, sw, sh;
  63. int intoffset[] = { 0, 4, 2, 1 };
  64. int intjump[] = { 8, 8, 4, 2 };
  65. int err = 0, transp = -1;
  66. if (img->multi.cap == 0) {
  67. img->multi.cap = 8;
  68. img->multi.frames = (Imlib_Image**)
  69. s_malloc(sizeof(Imlib_Image*) * img->multi.cap);
  70. }
  71. img->multi.cnt = 0;
  72. img->multi.sel = 0;
  73. gif = DGifOpenFileName(file->path);
  74. if (!gif) {
  75. warn("could not open gif file: %s", file->name);
  76. return 0;
  77. }
  78. bg = gif->SBackGroundColor;
  79. sw = gif->SWidth;
  80. sh = gif->SHeight;
  81. do {
  82. if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
  83. warn("could not open gif file: %s", file->name);
  84. err = 1;
  85. break;
  86. }
  87. if (rec == EXTENSION_RECORD_TYPE) {
  88. int ext_code;
  89. GifByteType *ext = NULL;
  90. DGifGetExtension(gif, &ext_code, &ext);
  91. while (ext) {
  92. if (ext_code == 0xf9) {
  93. if (ext[1] & 1)
  94. transp = (int) ext[4];
  95. else
  96. transp = -1;
  97. }
  98. ext = NULL;
  99. DGifGetExtensionNext(gif, &ext);
  100. }
  101. } else if (rec == IMAGE_DESC_RECORD_TYPE) {
  102. if (DGifGetImageDesc(gif) == GIF_ERROR) {
  103. warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
  104. err = 1;
  105. break;
  106. }
  107. x = gif->Image.Left;
  108. y = gif->Image.Top;
  109. w = gif->Image.Width;
  110. h = gif->Image.Height;
  111. rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
  112. for (i = 0; i < h; i++)
  113. rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
  114. if (gif->Image.Interlace) {
  115. for (i = 0; i < 4; i++) {
  116. for (j = intoffset[i]; j < h; j += intjump[i])
  117. DGifGetLine(gif, rows[j], w);
  118. }
  119. } else {
  120. for (i = 0; i < h; i++)
  121. DGifGetLine(gif, rows[i], w);
  122. }
  123. ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
  124. cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
  125. r = cmap->Colors[bg].Red;
  126. g = cmap->Colors[bg].Green;
  127. b = cmap->Colors[bg].Blue;
  128. bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
  129. for (i = 0; i < sh; i++) {
  130. for (j = 0; j < sw; j++) {
  131. if (i < y || i >= y + h || j < x || j >= x + w) {
  132. if (transp >= 0 && prev_frame)
  133. *ptr = prev_frame[i * sw + j];
  134. else
  135. *ptr = bgpixel;
  136. } else if (rows[i-y][j-x] == transp) {
  137. if (prev_frame)
  138. *ptr = prev_frame[i * sw + j];
  139. else
  140. *ptr = bgpixel;
  141. } else {
  142. r = cmap->Colors[rows[i-y][j-x]].Red;
  143. g = cmap->Colors[rows[i-y][j-x]].Green;
  144. b = cmap->Colors[rows[i-y][j-x]].Blue;
  145. *ptr = 0xff << 24 | r << 16 | g << 8 | b;
  146. }
  147. ptr++;
  148. }
  149. }
  150. im = imlib_create_image_using_copied_data(sw, sh, data);
  151. for (i = 0; i < h; i++)
  152. free(rows[i]);
  153. free(rows);
  154. free(data);
  155. if (!im) {
  156. warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
  157. err = 1;
  158. break;
  159. }
  160. imlib_context_set_image(im);
  161. prev_frame = imlib_image_get_data_for_reading_only();
  162. imlib_image_set_format("gif");
  163. if (transp >= 0)
  164. imlib_image_set_has_alpha(1);
  165. if (img->multi.cnt == img->multi.cap) {
  166. img->multi.cap *= 2;
  167. img->multi.frames = (Imlib_Image**)
  168. s_realloc(img->multi.frames,
  169. img->multi.cap * sizeof(Imlib_Image*));
  170. }
  171. img->multi.frames[img->multi.cnt++] = im;
  172. }
  173. } while (rec != TERMINATE_RECORD_TYPE);
  174. DGifCloseFile(gif);
  175. if (!err && img->multi.cnt > 1) {
  176. imlib_context_set_image(img->im);
  177. imlib_free_image();
  178. img->im = img->multi.frames[0];
  179. } else {
  180. for (i = 0; i < img->multi.cnt; i++) {
  181. imlib_context_set_image(img->multi.frames[i]);
  182. imlib_free_image();
  183. }
  184. img->multi.cnt = 0;
  185. }
  186. imlib_context_set_image(img->im);
  187. return !err;
  188. }
  189. #endif /* HAVE_GIFLIB */
  190. int img_load(img_t *img, const fileinfo_t *file) {
  191. const char *fmt;
  192. if (!img || !file || !file->name || !file->path)
  193. return 0;
  194. if (access(file->path, R_OK) || !(img->im = imlib_load_image(file->path))) {
  195. warn("could not open image: %s", file->name);
  196. return 0;
  197. }
  198. imlib_context_set_image(img->im);
  199. imlib_image_set_changes_on_disk();
  200. imlib_context_set_anti_alias(img->aa);
  201. fmt = imlib_image_format();
  202. #ifdef HAVE_GIFLIB
  203. if (!strcmp(fmt, "gif"))
  204. img_load_gif(img, file);
  205. #else
  206. /* avoid unused-but-set-variable warning */
  207. (void) fmt;
  208. #endif
  209. img->scalemode = options->scalemode;
  210. img->re = 0;
  211. img->checkpan = 0;
  212. img->w = imlib_image_get_width();
  213. img->h = imlib_image_get_height();
  214. return 1;
  215. }
  216. void img_close(img_t *img, int decache) {
  217. int i;
  218. if (!img)
  219. return;
  220. if (img->multi.cnt) {
  221. for (i = 0; i < img->multi.cnt; i++) {
  222. imlib_context_set_image(img->multi.frames[i]);
  223. imlib_free_image();
  224. }
  225. img->multi.cnt = 0;
  226. img->im = NULL;
  227. } else if (img->im) {
  228. imlib_context_set_image(img->im);
  229. if (decache)
  230. imlib_free_image_and_decache();
  231. else
  232. imlib_free_image();
  233. img->im = NULL;
  234. }
  235. }
  236. void img_check_pan(img_t *img, win_t *win) {
  237. if (!img || !win)
  238. return;
  239. if (img->w * img->zoom > win->w) {
  240. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  241. img->x = 0;
  242. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  243. img->x = win->w - img->w * img->zoom;
  244. } else {
  245. img->x = (win->w - img->w * img->zoom) / 2;
  246. }
  247. if (img->h * img->zoom > win->h) {
  248. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  249. img->y = 0;
  250. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  251. img->y = win->h - img->h * img->zoom;
  252. } else {
  253. img->y = (win->h - img->h * img->zoom) / 2;
  254. }
  255. }
  256. int img_fit(img_t *img, win_t *win) {
  257. float oz, zw, zh;
  258. if (!img || !win)
  259. return 0;
  260. oz = img->zoom;
  261. zw = (float) win->w / (float) img->w;
  262. zh = (float) win->h / (float) img->h;
  263. img->zoom = MIN(zw, zh);
  264. img->zoom = MAX(img->zoom, zoom_min);
  265. img->zoom = MIN(img->zoom, zoom_max);
  266. return oz != img->zoom;
  267. }
  268. void img_render(img_t *img, win_t *win) {
  269. int sx, sy, sw, sh;
  270. int dx, dy, dw, dh;
  271. if (!img || !img->im || !win)
  272. return;
  273. if (img->scalemode != SCALE_ZOOM) {
  274. img_fit(img, win);
  275. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  276. img->zoom = 1.0;
  277. }
  278. if (!img->re) {
  279. /* rendered for the first time */
  280. img->re = 1;
  281. if (img->zoom * img->w <= win->w)
  282. img->x = (win->w - img->w * img->zoom) / 2;
  283. else
  284. img->x = 0;
  285. if (img->zoom * img->h <= win->h)
  286. img->y = (win->h - img->h * img->zoom) / 2;
  287. else
  288. img->y = 0;
  289. }
  290. if (img->checkpan) {
  291. img_check_pan(img, win);
  292. img->checkpan = 0;
  293. }
  294. /* calculate source and destination offsets */
  295. if (img->x < 0) {
  296. sx = -img->x / img->zoom;
  297. sw = win->w / img->zoom;
  298. dx = 0;
  299. dw = win->w;
  300. } else {
  301. sx = 0;
  302. sw = img->w;
  303. dx = img->x;
  304. dw = img->w * img->zoom;
  305. }
  306. if (img->y < 0) {
  307. sy = -img->y / img->zoom;
  308. sh = win->h / img->zoom;
  309. dy = 0;
  310. dh = win->h;
  311. } else {
  312. sy = 0;
  313. sh = img->h;
  314. dy = img->y;
  315. dh = img->h * img->zoom;
  316. }
  317. win_clear(win);
  318. imlib_context_set_image(img->im);
  319. if (imlib_image_has_alpha() && !img->alpha)
  320. win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
  321. imlib_context_set_drawable(win->pm);
  322. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  323. win_draw(win);
  324. }
  325. int img_change_frame(img_t *img, int d) {
  326. if (!img || !img->multi.cnt || !d)
  327. return 0;
  328. d += img->multi.sel;
  329. if (d < 0)
  330. d = 0;
  331. else if (d >= img->multi.cnt)
  332. d = img->multi.cnt - 1;
  333. img->multi.sel = d;
  334. img->im = img->multi.frames[d];
  335. imlib_context_set_image(img->im);
  336. img->w = imlib_image_get_width();
  337. img->h = imlib_image_get_height();
  338. img->checkpan = 1;
  339. return 1;
  340. }
  341. int img_fit_win(img_t *img, win_t *win) {
  342. if (!img || !img->im || !win)
  343. return 0;
  344. img->scalemode = SCALE_FIT;
  345. return img_fit(img, win);
  346. }
  347. int img_center(img_t *img, win_t *win) {
  348. int ox, oy;
  349. if (!img || !win)
  350. return 0;
  351. ox = img->x;
  352. oy = img->y;
  353. img->x = (win->w - img->w * img->zoom) / 2;
  354. img->y = (win->h - img->h * img->zoom) / 2;
  355. return ox != img->x || oy != img->y;
  356. }
  357. int img_zoom(img_t *img, win_t *win, float z) {
  358. if (!img || !img->im || !win)
  359. return 0;
  360. z = MAX(z, zoom_min);
  361. z = MIN(z, zoom_max);
  362. img->scalemode = SCALE_ZOOM;
  363. if (z != img->zoom) {
  364. img->x = win->w / 2 - (win->w / 2 - img->x) * z / img->zoom;
  365. img->y = win->h / 2 - (win->h / 2 - img->y) * z / img->zoom;
  366. img->zoom = z;
  367. img->checkpan = 1;
  368. return 1;
  369. } else {
  370. return 0;
  371. }
  372. }
  373. int img_zoom_in(img_t *img, win_t *win) {
  374. int i;
  375. if (!img || !img->im || !win)
  376. return 0;
  377. for (i = 1; i < zl_cnt; i++) {
  378. if (zoom_levels[i] > img->zoom * 100.0)
  379. return img_zoom(img, win, zoom_levels[i] / 100.0);
  380. }
  381. return 0;
  382. }
  383. int img_zoom_out(img_t *img, win_t *win) {
  384. int i;
  385. if (!img || !img->im || !win)
  386. return 0;
  387. for (i = zl_cnt - 2; i >= 0; i--) {
  388. if (zoom_levels[i] < img->zoom * 100.0)
  389. return img_zoom(img, win, zoom_levels[i] / 100.0);
  390. }
  391. return 0;
  392. }
  393. int img_move(img_t *img, win_t *win, int dx, int dy) {
  394. int ox, oy;
  395. if (!img || !img->im || !win)
  396. return 0;
  397. ox = img->x;
  398. oy = img->y;
  399. img->x += dx;
  400. img->y += dy;
  401. img_check_pan(img, win);
  402. return ox != img->x || oy != img->y;
  403. }
  404. int img_pan(img_t *img, win_t *win, direction_t dir, int screen) {
  405. if (!img || !img->im || !win)
  406. return 0;
  407. switch (dir) {
  408. case DIR_LEFT:
  409. return img_move(img, win, win->w / (screen ? 1 : 5), 0);
  410. case DIR_RIGHT:
  411. return img_move(img, win, win->w / (screen ? 1 : 5) * -1, 0);
  412. case DIR_UP:
  413. return img_move(img, win, 0, win->h / (screen ? 1 : 5));
  414. case DIR_DOWN:
  415. return img_move(img, win, 0, win->h / (screen ? 1 : 5) * -1);
  416. }
  417. return 0;
  418. }
  419. int img_pan_edge(img_t *img, win_t *win, direction_t dir) {
  420. int ox, oy;
  421. if (!img || !img->im || !win)
  422. return 0;
  423. ox = img->x;
  424. oy = img->y;
  425. switch (dir) {
  426. case DIR_LEFT:
  427. img->x = 0;
  428. break;
  429. case DIR_RIGHT:
  430. img->x = win->w - img->w * img->zoom;
  431. break;
  432. case DIR_UP:
  433. img->y = 0;
  434. break;
  435. case DIR_DOWN:
  436. img->y = win->h - img->h * img->zoom;
  437. break;
  438. }
  439. img_check_pan(img, win);
  440. return ox != img->x || oy != img->y;
  441. }
  442. void img_rotate(img_t *img, win_t *win, int d) {
  443. int ox, oy, tmp;
  444. if (!img || !img->im || !win)
  445. return;
  446. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  447. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  448. imlib_context_set_image(img->im);
  449. imlib_image_orientate(d);
  450. img->x = oy + (win->w - win->h) / 2;
  451. img->y = ox + (win->h - win->w) / 2;
  452. tmp = img->w;
  453. img->w = img->h;
  454. img->h = tmp;
  455. img->checkpan = 1;
  456. }
  457. void img_rotate_left(img_t *img, win_t *win) {
  458. img_rotate(img, win, 3);
  459. }
  460. void img_rotate_right(img_t *img, win_t *win) {
  461. img_rotate(img, win, 1);
  462. }
  463. void img_toggle_antialias(img_t *img) {
  464. if (img && img->im) {
  465. img->aa ^= 1;
  466. imlib_context_set_image(img->im);
  467. imlib_context_set_anti_alias(img->aa);
  468. }
  469. }