libcaca  0.99.beta19
About: libcaca is a graphics library that outputs text instead of pixels, so that it can work on older video cards or text terminals (something like an advanced AAlib library).
  Fossies Dox: libcaca-0.99.beta19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

cacaview.c
Go to the documentation of this file.
1 /*
2  * cacaview image viewer for libcaca
3  * Copyright (c) 2003-2012 Sam Hocevar <sam@hocevar.net>
4  * All Rights Reserved
5  *
6  * This program is free software. It comes without any warranty, to
7  * the extent permitted by applicable law. You can redistribute it
8  * and/or modify it under the terms of the Do What the Fuck You Want
9  * to Public License, Version 2, as published by Sam Hocevar. See
10  * http://www.wtfpl.net/ for more details.
11  */
12 
13 #include "config.h"
14 
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 # include <string.h>
18 # include <stdlib.h>
19 #endif
20 
21 #if defined(HAVE_SLEEP)
22 # include <windows.h>
23 #endif
24 
25 #include "caca.h"
26 
27 #include "common-image.h"
28 
29 /* Local macros */
30 #define MODE_IMAGE 1
31 #define MODE_FILES 2
32 
33 #define STATUS_DITHERING 1
34 #define STATUS_ANTIALIASING 2
35 #define STATUS_BACKGROUND 3
36 
37 #define ZOOM_FACTOR 1.08f
38 #define ZOOM_MAX 50
39 #define GAMMA_FACTOR 1.04f
40 #define GAMMA_MAX 100
41 #define GAMMA(g) (((g) < 0) ? 1.0 / gammatab[-(g)] : gammatab[(g)])
42 #define PAD_STEP 0.15
43 
44 /* libcaca/libcaca contexts */
46 
47 /* Local functions */
48 static void print_status(void);
49 static void print_help(int, int);
50 static void set_zoom(int);
51 static void set_gamma(int);
52 static void draw_checkers(int, int, int, int);
53 
54 /* Local variables */
55 struct image *im = NULL;
56 
57 float zoomtab[ZOOM_MAX + 1];
58 float gammatab[GAMMA_MAX + 1];
59 float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5;
60 int zoom = 0, g = 0, fullscreen = 0, mode, ww, wh;
61 
62 int main(int argc, char **argv)
63 {
64  char const * const * algos = caca_get_dither_algorithm_list(NULL);
65  int dither_algorithm = 0;
66 
67  int quit = 0, update = 1, help = 0, status = 0;
68  int reload = 0;
69 
70  char **list = NULL;
71  int current = 0, items = 0, opts = 1;
72  int i;
73 
74  /* Initialise libcaca */
75  cv = caca_create_canvas(0, 0);
76  if(!cv)
77  {
78  fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
79  return 1;
80  }
81 
83  if(!dp)
84  {
85  fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
86  return 1;
87  }
88 
89  /* Set the window title */
90  caca_set_display_title(dp, "cacaview");
91 
94 
95  /* Fill the zoom table */
96  zoomtab[0] = 1.0;
97  for(i = 0; i < ZOOM_MAX; i++)
98  zoomtab[i + 1] = zoomtab[i] * ZOOM_FACTOR;
99 
100  /* Fill the gamma table */
101  gammatab[0] = 1.0;
102  for(i = 0; i < GAMMA_MAX; i++)
103  gammatab[i + 1] = gammatab[i] * GAMMA_FACTOR;
104 
105  /* Load items into playlist */
106  for(i = 1; i < argc; i++)
107  {
108  /* Skip options except after `--' */
109  if(opts && argv[i][0] == '-')
110  {
111  if(argv[i][1] == '-' && argv[i][2] == '\0')
112  opts = 0;
113  continue;
114  }
115 
116  /* Add argv[i] to the list */
117  if(items)
118  list = realloc(list, (items + 1) * sizeof(char *));
119  else
120  list = malloc(sizeof(char *));
121  list[items] = argv[i];
122  items++;
123 
124  reload = 1;
125  }
126 
127  /* Go ! */
128  while(!quit)
129  {
130  caca_event_t ev;
131  unsigned int const event_mask = CACA_EVENT_KEY_PRESS
134  | CACA_EVENT_QUIT;
135  unsigned int new_status = 0, new_help = 0;
136  int event;
137 
138  if(update)
139  event = caca_get_event(dp, event_mask, &ev, 0);
140  else
141  event = caca_get_event(dp, event_mask, &ev, -1);
142 
143  while(event)
144  {
146  {
147  if(caca_get_event_mouse_button(&ev) == 1)
148  {
149  if(items) current = (current + 1) % items;
150  reload = 1;
151  }
152  if(caca_get_event_mouse_button(&ev) == 2)
153  {
154  if(items) current = (items + current - 1) % items;
155  reload = 1;
156  }
157  }
159  switch(caca_get_event_key_ch(&ev))
160  {
161  case 'n':
162  case 'N':
163  if(items) current = (current + 1) % items;
164  reload = 1;
165  break;
166  case 'p':
167  case 'P':
168  if(items) current = (items + current - 1) % items;
169  reload = 1;
170  break;
171  case 'f':
172  case 'F':
173  case CACA_KEY_F11:
175  update = 1;
176  set_zoom(zoom);
177  break;
178 #if 0 /* FIXME */
179  case 'b':
182  caca_set_feature(cv, i);
183  new_status = STATUS_BACKGROUND;
184  update = 1;
185  break;
186  case 'B':
189  caca_set_feature(cv, i);
190  new_status = STATUS_BACKGROUND;
191  update = 1;
192  break;
193  case 'a':
196  caca_set_feature(cv, i);
197  new_status = STATUS_ANTIALIASING;
198  update = 1;
199  break;
200  case 'A':
203  caca_set_feature(cv, i);
204  new_status = STATUS_ANTIALIASING;
205  update = 1;
206  break;
207 #endif
208  case 'd':
209  dither_algorithm++;
210  if(algos[dither_algorithm * 2] == NULL)
211  dither_algorithm = 0;
213  algos[dither_algorithm * 2]);
214  new_status = STATUS_DITHERING;
215  update = 1;
216  break;
217  case 'D':
218  dither_algorithm--;
219  if(dither_algorithm < 0)
220  while(algos[dither_algorithm * 2 + 2] != NULL)
221  dither_algorithm++;
223  algos[dither_algorithm * 2]);
224  new_status = STATUS_DITHERING;
225  update = 1;
226  break;
227  case '+':
228  update = 1;
229  set_zoom(zoom + 1);
230  break;
231  case '-':
232  update = 1;
233  set_zoom(zoom - 1);
234  break;
235  case 'G':
236  update = 1;
237  set_gamma(g + 1);
238  break;
239  case 'g':
240  update = 1;
241  set_gamma(g - 1);
242  break;
243  case 'x':
244  case 'X':
245  update = 1;
246  set_zoom(0);
247  set_gamma(0);
248  break;
249  case 'k':
250  case 'K':
251  case CACA_KEY_UP:
252  if(yfactor > 1.0) dy -= PAD_STEP / yfactor;
253  if(dy < 0.0) dy = 0.0;
254  update = 1;
255  break;
256  case 'j':
257  case 'J':
258  case CACA_KEY_DOWN:
259  if(yfactor > 1.0) dy += PAD_STEP / yfactor;
260  if(dy > 1.0) dy = 1.0;
261  update = 1;
262  break;
263  case 'h':
264  case 'H':
265  case CACA_KEY_LEFT:
266  if(xfactor > 1.0) dx -= PAD_STEP / xfactor;
267  if(dx < 0.0) dx = 0.0;
268  update = 1;
269  break;
270  case 'l':
271  case 'L':
272  case CACA_KEY_RIGHT:
273  if(xfactor > 1.0) dx += PAD_STEP / xfactor;
274  if(dx > 1.0) dx = 1.0;
275  update = 1;
276  break;
277  case '?':
278  new_help = !help;
279  update = 1;
280  break;
281  case 'q':
282  case 'Q':
283  case CACA_KEY_ESCAPE:
284  quit = 1;
285  break;
286  }
287  else if(caca_get_event_type(&ev) == CACA_EVENT_RESIZE)
288  {
292  update = 1;
293  set_zoom(zoom);
294  }
295  else if(caca_get_event_type(&ev) & CACA_EVENT_QUIT)
296  quit = 1;
297 
298  if(status || new_status)
299  status = new_status;
300 
301  if(help || new_help)
302  help = new_help;
303 
304  event = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
305  }
306 
307  if(items && reload)
308  {
309  char *buffer;
310  int len = strlen(" Loading `%s'... ") + strlen(list[current]);
311 
312  if(len < ww + 1)
313  len = ww + 1;
314 
315  buffer = malloc(len);
316 
317  sprintf(buffer, " Loading `%s'... ", list[current]);
318  buffer[ww] = '\0';
320  caca_put_str(cv, (ww - strlen(buffer)) / 2, wh / 2, buffer);
324 
325  if(im)
326  unload_image(im);
327  im = load_image(list[current]);
328  reload = 0;
329 
330  /* Reset image-specific runtime variables */
331  dx = dy = 0.5;
332  update = 1;
333  set_zoom(0);
334  set_gamma(0);
335 
336  free(buffer);
337  }
338 
341 
342  if(!items)
343  {
345  caca_printf(cv, ww / 2 - 5, wh / 2, " No image. ");
346  }
347  else if(!im)
348  {
349 #if defined(USE_IMLIB2)
350 # define ERROR_STRING " Error loading `%s'. "
351 #else
352 # define ERROR_STRING " Error loading `%s'. Only BMP is supported. "
353 #endif
354  char *buffer;
355  int len = strlen(ERROR_STRING) + strlen(list[current]);
356 
357  if(len < ww + 1)
358  len = ww + 1;
359 
360  buffer = malloc(len);
361 
362  sprintf(buffer, ERROR_STRING, list[current]);
363  buffer[ww] = '\0';
365  caca_put_str(cv, (ww - strlen(buffer)) / 2, wh / 2, buffer);
366  free(buffer);
367  }
368  else
369  {
370  float xdelta, ydelta;
371  int y, height;
372 
373  y = fullscreen ? 0 : 1;
374  height = fullscreen ? wh : wh - 3;
375 
376  xdelta = (xfactor > 1.0) ? dx : 0.5;
377  ydelta = (yfactor > 1.0) ? dy : 0.5;
378 
379  draw_checkers(ww * (1.0 - xfactor) / 2,
380  y + height * (1.0 - yfactor) / 2,
381  ww * xfactor, height * yfactor);
382 
383  caca_dither_bitmap(cv, ww * (1.0 - xfactor) * xdelta,
384  y + height * (1.0 - yfactor) * ydelta,
385  ww * xfactor + 1, height * yfactor + 1,
386  im->dither, im->pixels);
387  }
388 
389  if(!fullscreen)
390  {
391  print_status();
392 
394  switch(status)
395  {
396  case STATUS_DITHERING:
397  caca_printf(cv, 0, wh - 1, "Dithering: %s",
399  break;
400 #if 0 /* FIXME */
401  case STATUS_ANTIALIASING:
402  caca_printf(cv, 0, wh - 1, "Antialiasing: %s",
404  break;
405  case STATUS_BACKGROUND:
406  caca_printf(cv, 0, wh - 1, "Background: %s",
408  break;
409 #endif
410  }
411  }
412 
413  if(help)
414  {
415  print_help(ww - 26, 2);
416  }
417 
419  update = 0;
420  }
421 
422  /* Clean up */
423  if(im)
424  unload_image(im);
427 
428  return 0;
429 }
430 
431 static void print_status(void)
432 {
434  caca_draw_line(cv, 0, 0, ww - 1, 0, ' ');
435  caca_draw_line(cv, 0, wh - 2, ww - 1, wh - 2, '-');
436  caca_put_str(cv, 0, 0, "q:Quit np:Next/Prev +-x:Zoom gG:Gamma "
437  "hjkl:Move d:Dither a:Antialias");
438  caca_put_str(cv, ww - strlen("?:Help"), 0, "?:Help");
439  caca_printf(cv, 3, wh - 2, "cacaview %s", PACKAGE_VERSION);
440  caca_printf(cv, ww - 30, wh - 2, "(gamma: %#.3g)", GAMMA(g));
441  caca_printf(cv, ww - 14, wh - 2, "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
442 
444  caca_draw_line(cv, 0, wh - 1, ww - 1, wh - 1, ' ');
445 }
446 
447 static void print_help(int x, int y)
448 {
449  static char const *help[] =
450  {
451  " +: zoom in ",
452  " -: zoom out ",
453  " g: decrease gamma ",
454  " G: increase gamma ",
455  " x: reset zoom and gamma ",
456  " ----------------------- ",
457  " hjkl: move view ",
458  " arrows: move view ",
459  " ----------------------- ",
460  " a: antialiasing method ",
461  " d: dithering method ",
462  " b: background mode ",
463  " ----------------------- ",
464  " ?: help ",
465  " q: quit ",
466  NULL
467  };
468 
469  int i;
470 
472 
473  for(i = 0; help[i]; i++)
474  caca_put_str(cv, x, y + i, help[i]);
475 }
476 
477 static void set_zoom(int new_zoom)
478 {
479  int height;
480 
481  if(!im)
482  return;
483 
484  zoom = new_zoom;
485 
486  if(zoom > ZOOM_MAX) zoom = ZOOM_MAX;
487  if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX;
488 
490  height = fullscreen ? wh : wh - 3;
491 
492  xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom];
493  yfactor = xfactor * ww / height * im->h / im->w
496 
497  if(yfactor > xfactor)
498  {
499  float tmp = xfactor;
500  xfactor = tmp * tmp / yfactor;
501  yfactor = tmp;
502  }
503 }
504 
505 static void set_gamma(int new_gamma)
506 {
507  if(!im)
508  return;
509 
510  g = new_gamma;
511 
512  if(g > GAMMA_MAX) g = GAMMA_MAX;
513  if(g < -GAMMA_MAX) g = -GAMMA_MAX;
514 
516  (g < 0) ? 1.0 / gammatab[-g] : gammatab[g]);
517 }
518 
519 static void draw_checkers(int x, int y, int w, int h)
520 {
521  int xn, yn;
522 
523  if(x + w > (int)caca_get_canvas_width(cv))
525  if(y + h > (int)caca_get_canvas_height(cv))
527 
528  for(yn = y > 0 ? y : 0; yn < y + h; yn++)
529  for(xn = x > 0 ? x : 0; xn < x + w; xn++)
530  {
531  if((((xn - x) / 5) ^ ((yn - y) / 3)) & 1)
533  else
535  caca_put_char(cv, xn, yn, ' ');
536  }
537 }
538 
ww
int ww
Definition: cacaview.c:60
CACA_KEY_DOWN
Definition: caca.h:198
draw_checkers
static void draw_checkers(int, int, int, int)
Definition: cacaview.c:519
im
struct image * im
Definition: cacaview.c:55
STATUS_ANTIALIASING
#define STATUS_ANTIALIASING
Definition: cacaview.c:34
caca_get_event_resize_height
int caca_get_event_resize_height(caca_event_t const *)
Return a resize event's display height value.
Definition: event.c:340
caca_create_display
caca_display_t * caca_create_display(caca_canvas_t *cv)
Attach a caca graphical context to a caca canvas.
Definition: caca.c:68
main
int main(int argc, char **argv)
Definition: cacaview.c:62
CACA_BACKGROUND_MAX
#define CACA_BACKGROUND_MAX
Definition: caca0.h:84
y
static int y
Definition: cacadraw.c:27
print_help
static void print_help(int, int)
Definition: cacaview.c:447
caca_set_dither_algorithm
int caca_set_dither_algorithm(caca_dither_t *, char const *)
Set dithering algorithm.
Definition: dither.c:828
g
int g
Definition: cacaview.c:60
print_status
static void print_status(void)
Definition: cacaview.c:431
CACA_KEY_LEFT
Definition: caca.h:199
PACKAGE_VERSION
#define PACKAGE_VERSION
Definition: config.h:75
image::w
unsigned int w
Definition: common-image.h:16
CACA_ANTIALIASING_MIN
#define CACA_ANTIALIASING_MIN
Definition: caca0.h:88
caca_event
Handling of user events.
Definition: caca.h:129
caca_create_canvas
caca_canvas_t * caca_create_canvas(int, int)
Initialise a libcaca canvas.
Definition: canvas.c:54
caca_display
Definition: caca_internals.h:167
caca_free_canvas
int caca_free_canvas(caca_canvas_t *)
Free a libcaca canvas.
Definition: canvas.c:308
xfactor
float xfactor
Definition: cacaview.c:59
caca_get_event_type
enum caca_event_type caca_get_event_type(caca_event_t const *)
Return an event's type.
Definition: event.c:202
caca_get_canvas_height
int caca_get_canvas_height(caca_canvas_t const *)
Get the canvas height.
Definition: canvas.c:253
CACA_EVENT_QUIT
Definition: caca.h:117
image::pixels
char * pixels
Definition: common-image.h:15
zoom
int zoom
Definition: cacaview.c:60
ZOOM_MAX
#define ZOOM_MAX
Definition: cacaview.c:38
CACA_ANTIALIASING_MAX
#define CACA_ANTIALIASING_MAX
Definition: caca0.h:89
caca_get_canvas_width
int caca_get_canvas_width(caca_canvas_t const *)
Get the canvas width.
Definition: canvas.c:239
set_zoom
static void set_zoom(int)
Definition: cacaview.c:477
caca_get_dither_algorithm
const char * caca_get_dither_algorithm(caca_dither_t const *)
Get current dithering algorithm.
Definition: dither.c:919
caca_get_event_mouse_button
int caca_get_event_mouse_button(caca_event_t const *)
Return a mouse press or mouse release event's button.
Definition: event.c:276
caca_get_feature_name
#define caca_get_feature_name
Definition: caca0.h:126
CACA_LIGHTGRAY
Definition: caca.h:78
dy
float dy
Definition: cacaview.c:59
image
Definition: common-image.h:13
CACA_EVENT_MOUSE_PRESS
Definition: caca.h:113
caca_set_dither_gamma
int caca_set_dither_gamma(caca_dither_t *, float)
Set the gamma of a dither object.
Definition: dither.c:455
CACA_WHITE
Definition: caca.h:86
caca_put_str
int caca_put_str(caca_canvas_t *, int, int, char const *)
Print a string.
Definition: string.c:254
caca_set_feature
#define caca_set_feature
Definition: caca0.h:125
caca_get_event_resize_width
int caca_get_event_resize_width(caca_event_t const *)
Return a resize event's display width value.
Definition: event.c:324
GAMMA_FACTOR
#define GAMMA_FACTOR
Definition: cacaview.c:39
CACA_EVENT_RESIZE
Definition: caca.h:116
STATUS_DITHERING
#define STATUS_DITHERING
Definition: cacaview.c:33
mode
int mode
Definition: cacaview.c:60
unload_image
void unload_image(struct image *im)
Definition: common-image.c:260
caca_draw_line
#define caca_draw_line(x, y, z, t, c)
Definition: caca0.h:151
gammatab
float gammatab[100+1]
Definition: cacaview.c:58
GAMMA_MAX
#define GAMMA_MAX
Definition: cacaview.c:40
CACA_KEY_ESCAPE
Definition: caca.h:192
cv
caca_canvas_t * cv
Definition: cacaview.c:45
caca_refresh_display
int caca_refresh_display(caca_display_t *)
Flush pending changes and redraw the screen.
Definition: graphics.c:146
CACA_KEY_F11
Definition: caca.h:218
set_gamma
static void set_gamma(int)
Definition: cacaview.c:505
GAMMA
#define GAMMA(g)
Definition: cacaview.c:41
caca_put_char
int caca_put_char(caca_canvas_t *, int, int, uint32_t)
Print an ASCII or Unicode character.
Definition: string.c:120
caca_free_display
int caca_free_display(caca_display_t *dp)
Detach a caca graphical context from a caca backend context.
Definition: caca.c:238
caca_printf
#define caca_printf(x, y, f, z...)
Definition: caca0.h:148
caca_get_dither_algorithm_list
const char *const * caca_get_dither_algorithm_list(caca_dither_t const *)
Get dithering algorithms.
Definition: dither.c:894
image::h
unsigned int h
Definition: common-image.h:16
CACA_BLUE
Definition: caca.h:72
caca_get_feature
#define caca_get_feature
Definition: caca0.h:124
caca_get_display_height
int caca_get_display_height(caca_display_t const *)
Get the display height.
Definition: graphics.c:79
CACA_BLACK
Definition: caca.h:71
yfactor
float yfactor
Definition: cacaview.c:59
CACA_KEY_RIGHT
Definition: caca.h:200
CACA_DARKGRAY
Definition: caca.h:79
PAD_STEP
#define PAD_STEP
Definition: cacaview.c:42
STATUS_BACKGROUND
#define STATUS_BACKGROUND
Definition: cacaview.c:35
image::dither
struct caca_dither * dither
Definition: common-image.h:17
ZOOM_FACTOR
#define ZOOM_FACTOR
Definition: cacaview.c:37
caca_set_display_title
int caca_set_display_title(caca_display_t *, char const *)
Set the display title.
Definition: graphics.c:40
caca_dither_bitmap
int caca_dither_bitmap(caca_canvas_t *, int, int, int, int, caca_dither_t const *, void const *)
Dither a bitmap on the canvas.
Definition: dither.c:940
caca_set_color_ansi
int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
Set the default colour pair for text (ANSI version).
Definition: attr.c:234
caca.h
The libcaca public header.
CACA_KEY_UP
Definition: caca.h:197
load_image
struct image * load_image(char const *name)
Definition: common-image.c:34
config.h
wh
int wh
Definition: cacaview.c:60
caca_get_event
#define caca_get_event(x)
Definition: caca0.h:136
common-image.h
CACA_BACKGROUND
Definition: caca0.h:80
fullscreen
int fullscreen
Definition: cacaview.c:60
caca_clear_canvas
int caca_clear_canvas(caca_canvas_t *)
Clear the canvas.
Definition: string.c:374
CACA_EVENT_KEY_PRESS
Definition: caca.h:111
CACA_ANTIALIASING
Definition: caca0.h:85
dx
float dx
Definition: cacaview.c:59
dp
caca_display_t * dp
Definition: cacaview.c:45
zoomtab
float zoomtab[50+1]
Definition: cacaview.c:57
CACA_BACKGROUND_MIN
#define CACA_BACKGROUND_MIN
Definition: caca0.h:83
ERROR_STRING
#define ERROR_STRING
caca_get_display_width
int caca_get_display_width(caca_display_t const *)
Get the display width.
Definition: graphics.c:62
caca_get_event_key_ch
int caca_get_event_key_ch(caca_event_t const *)
Return a key press or key release event's value.
Definition: event.c:219
caca_canvas
Definition: caca_internals.h:47
x
static int x
Definition: cacadraw.c:27