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.
 
 
 
 
 
 

182 lines
5.3 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. #define X_DISPLAY_MISSING 1
  26. #include <Imlib2.h>
  27. #include "caca.h"
  28. Imlib_Image image = NULL;
  29. char *pixels = NULL;
  30. struct caca_bitmap *bitmap = NULL;
  31. int dithering = 1;
  32. const enum caca_dithering dithering_list[] =
  33. { CACA_DITHER_NONE, CACA_DITHER_ORDERED, CACA_DITHER_RANDOM };
  34. int main(int argc, char **argv)
  35. {
  36. int quit = 0, update = 1;
  37. int x, y, w, h, zoom = 0;
  38. if(argc != 2)
  39. {
  40. fprintf(stderr, "usage: %s <filename>\n", argv[0]);
  41. return 1;
  42. }
  43. image = imlib_load_image(argv[1]);
  44. if(!image)
  45. {
  46. fprintf(stderr, "%s: unable to open `%s'\n", argv[0], argv[1]);
  47. return 1;
  48. }
  49. imlib_context_set_image(image);
  50. pixels = (char *)imlib_image_get_data_for_reading_only();
  51. w = imlib_image_get_width();
  52. h = imlib_image_get_height();
  53. x = w / 2;
  54. y = h / 2;
  55. if(caca_init())
  56. {
  57. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  58. return 1;
  59. }
  60. /* Create the libcaca bitmap */
  61. bitmap = caca_create_bitmap(32, w, h, 4 * w,
  62. 0x00ff0000, 0x0000ff00, 0x000000ff);
  63. if(!image)
  64. {
  65. fprintf(stderr, "%s: unable to create libcaca bitmap\n", argv[0]);
  66. caca_end();
  67. return 1;
  68. }
  69. /* Go ! */
  70. while(!quit)
  71. {
  72. int event;
  73. while((event = caca_get_event()))
  74. {
  75. switch(event)
  76. {
  77. case CACA_EVENT_KEY_PRESS | 'd':
  78. case CACA_EVENT_KEY_PRESS | 'D':
  79. dithering = (dithering + 1) % 3;
  80. update = 1;
  81. break;
  82. case CACA_EVENT_KEY_PRESS | '+':
  83. zoom++;
  84. update = 1;
  85. break;
  86. case CACA_EVENT_KEY_PRESS | '-':
  87. zoom--;
  88. update = 1;
  89. break;
  90. case CACA_EVENT_KEY_PRESS | 'x':
  91. case CACA_EVENT_KEY_PRESS | 'X':
  92. zoom = 0;
  93. update = 1;
  94. break;
  95. case CACA_EVENT_KEY_PRESS | CACA_KEY_UP:
  96. if(zoom > 0) y -= 1 + h / (2 + zoom) / 8;
  97. update = 1;
  98. break;
  99. case CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN:
  100. if(zoom > 0) y += 1 + h / (2 + zoom) / 8;
  101. update = 1;
  102. break;
  103. case CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT:
  104. if(zoom > 0) x -= 1 + w / (2 + zoom) / 8;
  105. update = 1;
  106. break;
  107. case CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT:
  108. if(zoom > 0) x += 1 + w / (2 + zoom) / 8;
  109. update = 1;
  110. break;
  111. case CACA_EVENT_KEY_PRESS | 'q':
  112. case CACA_EVENT_KEY_PRESS | 'Q':
  113. quit = 1;
  114. break;
  115. }
  116. }
  117. if(update)
  118. {
  119. caca_clear();
  120. caca_set_dithering(dithering_list[dithering]);
  121. if(zoom < 0)
  122. {
  123. int xo = (caca_get_width() - 1) / 2;
  124. int yo = (caca_get_height() - 1) / 2;
  125. int xn = (caca_get_width() - 1) / (2 - zoom);
  126. int yn = (caca_get_height() - 1) / (2 - zoom);
  127. caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn,
  128. bitmap, pixels);
  129. }
  130. else if(zoom > 0)
  131. {
  132. struct caca_bitmap *newbitmap;
  133. int xn = w / (2 + zoom);
  134. int yn = h / (2 + zoom);
  135. if(x < xn) x = xn;
  136. if(y < yn) y = yn;
  137. if(xn + x > w) x = w - xn;
  138. if(yn + y > h) y = h - yn;
  139. newbitmap = caca_create_bitmap(32, 2 * xn, 2 * yn, 4 * w,
  140. 0x00ff0000, 0x0000ff00, 0x000000ff);
  141. caca_draw_bitmap(0, 0, caca_get_width() - 1,
  142. caca_get_height() - 1, newbitmap,
  143. pixels + 4 * (x - xn) + 4 * w * (y - yn));
  144. caca_free_bitmap(newbitmap);
  145. }
  146. else
  147. {
  148. caca_draw_bitmap(0, 0, caca_get_width() - 1,
  149. caca_get_height() - 1, bitmap, pixels);
  150. }
  151. caca_refresh();
  152. update = 0;
  153. }
  154. }
  155. caca_free_bitmap(bitmap);
  156. imlib_free_image();
  157. /* Clean up */
  158. caca_end();
  159. return 0;
  160. }