Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

220 lignes
6.7 KiB

  1. /*
  2. * view image viewer for libcaca
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #define X_DISPLAY_MISSING 1
  27. #include <Imlib2.h>
  28. #include "caca.h"
  29. Imlib_Image image = NULL;
  30. char *pixels = NULL;
  31. struct caca_bitmap *bitmap = NULL;
  32. int dithering = 1;
  33. const enum caca_dithering dithering_list[] =
  34. { CACA_DITHER_NONE, CACA_DITHER_ORDERED, CACA_DITHER_RANDOM };
  35. int main(int argc, char **argv)
  36. {
  37. int quit = 0, update = 1, help = 0;
  38. int x, y, w, h, zoom = 0;
  39. if(argc != 2)
  40. {
  41. fprintf(stderr, "usage: %s <filename>\n", argv[0]);
  42. return 1;
  43. }
  44. image = imlib_load_image(argv[1]);
  45. if(!image)
  46. {
  47. fprintf(stderr, "%s: unable to open `%s'\n", argv[0], argv[1]);
  48. return 1;
  49. }
  50. imlib_context_set_image(image);
  51. pixels = (char *)imlib_image_get_data_for_reading_only();
  52. w = imlib_image_get_width();
  53. h = imlib_image_get_height();
  54. x = w / 2;
  55. y = h / 2;
  56. if(caca_init())
  57. {
  58. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  59. return 1;
  60. }
  61. /* Create the libcaca bitmap */
  62. bitmap = caca_create_bitmap(32, w, h, 4 * w,
  63. 0x00ff0000, 0x0000ff00, 0x000000ff);
  64. if(!image)
  65. {
  66. fprintf(stderr, "%s: unable to create libcaca bitmap\n", argv[0]);
  67. caca_end();
  68. return 1;
  69. }
  70. /* Go ! */
  71. while(!quit)
  72. {
  73. int event;
  74. while((event = caca_get_event()))
  75. {
  76. switch(event)
  77. {
  78. case CACA_EVENT_KEY_PRESS | 'd':
  79. case CACA_EVENT_KEY_PRESS | 'D':
  80. dithering = (dithering + 1) % 3;
  81. update = 1;
  82. break;
  83. case CACA_EVENT_KEY_PRESS | '+':
  84. zoom++;
  85. update = 1;
  86. break;
  87. case CACA_EVENT_KEY_PRESS | '-':
  88. zoom--;
  89. update = 1;
  90. break;
  91. case CACA_EVENT_KEY_PRESS | 'x':
  92. case CACA_EVENT_KEY_PRESS | 'X':
  93. zoom = 0;
  94. update = 1;
  95. break;
  96. case CACA_EVENT_KEY_PRESS | 'k':
  97. case CACA_EVENT_KEY_PRESS | 'K':
  98. case CACA_EVENT_KEY_PRESS | CACA_KEY_UP:
  99. if(zoom > 0) y -= 1 + h / (2 + zoom) / 8;
  100. update = 1;
  101. break;
  102. case CACA_EVENT_KEY_PRESS | 'j':
  103. case CACA_EVENT_KEY_PRESS | 'J':
  104. case CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN:
  105. if(zoom > 0) y += 1 + h / (2 + zoom) / 8;
  106. update = 1;
  107. break;
  108. case CACA_EVENT_KEY_PRESS | 'h':
  109. case CACA_EVENT_KEY_PRESS | 'H':
  110. case CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT:
  111. if(zoom > 0) x -= 1 + w / (2 + zoom) / 8;
  112. update = 1;
  113. break;
  114. case CACA_EVENT_KEY_PRESS | 'l':
  115. case CACA_EVENT_KEY_PRESS | 'L':
  116. case CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT:
  117. if(zoom > 0) x += 1 + w / (2 + zoom) / 8;
  118. update = 1;
  119. break;
  120. case CACA_EVENT_KEY_PRESS | '?':
  121. help = !help;
  122. update = 1;
  123. break;
  124. case CACA_EVENT_KEY_PRESS | 'q':
  125. case CACA_EVENT_KEY_PRESS | 'Q':
  126. quit = 1;
  127. break;
  128. }
  129. }
  130. if(update)
  131. {
  132. int ww = caca_get_width();
  133. int wh = caca_get_height();
  134. caca_clear();
  135. caca_set_dithering(dithering_list[dithering]);
  136. if(zoom < 0)
  137. {
  138. int xo = (ww - 1) / 2;
  139. int yo = (wh - 1) / 2;
  140. int xn = (ww - 1) / (2 - zoom);
  141. int yn = (wh - 1) / (2 - zoom);
  142. caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn,
  143. bitmap, pixels);
  144. }
  145. else if(zoom > 0)
  146. {
  147. struct caca_bitmap *newbitmap;
  148. int xn = w / (2 + zoom);
  149. int yn = h / (2 + zoom);
  150. if(x < xn) x = xn;
  151. if(y < yn) y = yn;
  152. if(xn + x > w) x = w - xn;
  153. if(yn + y > h) y = h - yn;
  154. newbitmap = caca_create_bitmap(32, 2 * xn, 2 * yn, 4 * w,
  155. 0x00ff0000, 0x0000ff00, 0x000000ff);
  156. caca_draw_bitmap(0, 0, ww - 1, wh - 1, newbitmap,
  157. pixels + 4 * (x - xn) + 4 * w * (y - yn));
  158. caca_free_bitmap(newbitmap);
  159. }
  160. else
  161. {
  162. caca_draw_bitmap(0, 0, ww - 1, wh - 1, bitmap, pixels);
  163. }
  164. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  165. caca_draw_line(0, 0, ww - 1, 0, ' ');
  166. caca_printf(1, 0, "cacaview %s", VERSION);
  167. caca_putstr(ww - strlen("'?' for help") - 1, 0,
  168. "'?' for help");
  169. if(help)
  170. {
  171. caca_putstr(2, 2, " +: zoom in ");
  172. caca_putstr(2, 3, " -: zoom out ");
  173. caca_putstr(2, 4, " x: reset zoom ");
  174. caca_putstr(2, 5, " ------------------- ");
  175. caca_putstr(2, 6, " hjkl: move view ");
  176. caca_putstr(2, 7, " arrows: move view ");
  177. caca_putstr(2, 8, " ------------------- ");
  178. caca_putstr(2, 9, " d: dithering method ");
  179. caca_putstr(2, 10, " ------------------- ");
  180. caca_putstr(2, 11, " ?: help ");
  181. caca_putstr(2, 12, " q: quit ");
  182. help = 0;
  183. }
  184. caca_refresh();
  185. update = 0;
  186. }
  187. }
  188. caca_free_bitmap(bitmap);
  189. imlib_free_image();
  190. /* Clean up */
  191. caca_end();
  192. return 0;
  193. }