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.
 
 
 
 
 
 

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