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.
 
 
 
 
 
 

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