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