1 | /* |
---|
2 | * cacaview image viewer for libcaca |
---|
3 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: cacaview.c 540 2006-03-07 09:17:35Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <stdlib.h> |
---|
19 | |
---|
20 | #if defined(HAVE_IMLIB2_H) |
---|
21 | # include <Imlib2.h> |
---|
22 | #else |
---|
23 | # include <stdio.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #if defined(HAVE_SLEEP) |
---|
27 | # include <windows.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #include "cucul.h" |
---|
31 | #include "caca.h" |
---|
32 | |
---|
33 | /* Local macros */ |
---|
34 | #define MODE_IMAGE 1 |
---|
35 | #define MODE_FILES 2 |
---|
36 | |
---|
37 | #define STATUS_DITHERING 1 |
---|
38 | #define STATUS_ANTIALIASING 2 |
---|
39 | #define STATUS_BACKGROUND 3 |
---|
40 | |
---|
41 | #define ZOOM_FACTOR 1.08f |
---|
42 | #define ZOOM_MAX 50 |
---|
43 | #define PAD_STEP 0.15 |
---|
44 | |
---|
45 | /* libcucul/libcaca contexts */ |
---|
46 | cucul_t *qq; caca_t *kk; |
---|
47 | |
---|
48 | /* Local functions */ |
---|
49 | static void print_status(void); |
---|
50 | static void print_help(int, int); |
---|
51 | static void set_zoom(int); |
---|
52 | static void load_image(char const *); |
---|
53 | static void unload_image(void); |
---|
54 | static void draw_checkers(int, int, int, int); |
---|
55 | #if !defined(HAVE_IMLIB2_H) |
---|
56 | static int freadint(FILE *); |
---|
57 | static int freadshort(FILE *); |
---|
58 | static int freadchar(FILE *); |
---|
59 | #endif |
---|
60 | |
---|
61 | /* Local variables */ |
---|
62 | #if defined(HAVE_IMLIB2_H) |
---|
63 | Imlib_Image image = NULL; |
---|
64 | #endif |
---|
65 | char *pixels = NULL; |
---|
66 | struct cucul_bitmap *bitmap = NULL; |
---|
67 | unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask; |
---|
68 | #if !defined(HAVE_IMLIB2_H) |
---|
69 | unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
70 | #endif |
---|
71 | |
---|
72 | float zoomtab[ZOOM_MAX + 1]; |
---|
73 | float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5; |
---|
74 | int zoom = 0, fullscreen = 0, mode, ww, wh; |
---|
75 | |
---|
76 | int main(int argc, char **argv) |
---|
77 | { |
---|
78 | int quit = 0, update = 1, help = 0, status = 0; |
---|
79 | int reload = 0; |
---|
80 | |
---|
81 | char **list = NULL; |
---|
82 | int current = 0, items = 0, opts = 1; |
---|
83 | int i; |
---|
84 | |
---|
85 | /* Initialise libcucul */ |
---|
86 | qq = cucul_init(); |
---|
87 | if(!qq) |
---|
88 | { |
---|
89 | fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]); |
---|
90 | return 1; |
---|
91 | } |
---|
92 | |
---|
93 | kk = caca_attach(qq); |
---|
94 | if(!kk) |
---|
95 | { |
---|
96 | fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); |
---|
97 | return 1; |
---|
98 | } |
---|
99 | |
---|
100 | /* Set the window title */ |
---|
101 | caca_set_window_title(kk, "cacaview"); |
---|
102 | |
---|
103 | ww = cucul_get_width(qq); |
---|
104 | wh = cucul_get_height(qq); |
---|
105 | |
---|
106 | /* Fill the zoom table */ |
---|
107 | zoomtab[0] = 1.0; |
---|
108 | for(i = 0; i < ZOOM_MAX; i++) |
---|
109 | zoomtab[i + 1] = zoomtab[i] * 1.08; |
---|
110 | |
---|
111 | /* Load items into playlist */ |
---|
112 | for(i = 1; i < argc; i++) |
---|
113 | { |
---|
114 | /* Skip options except after `--' */ |
---|
115 | if(opts && argv[i][0] == '-') |
---|
116 | { |
---|
117 | if(argv[i][1] == '-' && argv[i][2] == '\0') |
---|
118 | opts = 0; |
---|
119 | continue; |
---|
120 | } |
---|
121 | |
---|
122 | /* Add argv[i] to the list */ |
---|
123 | if(items) |
---|
124 | list = realloc(list, (items + 1) * sizeof(char *)); |
---|
125 | else |
---|
126 | list = malloc(sizeof(char *)); |
---|
127 | list[items] = argv[i]; |
---|
128 | items++; |
---|
129 | |
---|
130 | reload = 1; |
---|
131 | } |
---|
132 | |
---|
133 | /* Go ! */ |
---|
134 | while(!quit) |
---|
135 | { |
---|
136 | unsigned int const event_mask = CACA_EVENT_KEY_PRESS |
---|
137 | | CACA_EVENT_RESIZE |
---|
138 | | CACA_EVENT_MOUSE_PRESS; |
---|
139 | unsigned int event, new_status = 0, new_help = 0; |
---|
140 | |
---|
141 | if(update) |
---|
142 | event = caca_get_event(kk, event_mask); |
---|
143 | else |
---|
144 | event = caca_wait_event(kk, event_mask); |
---|
145 | |
---|
146 | while(event) |
---|
147 | { |
---|
148 | if(event & CACA_EVENT_MOUSE_PRESS) |
---|
149 | { |
---|
150 | if((event & 0x00ffffff) == 1) |
---|
151 | { |
---|
152 | if(items) current = (current + 1) % items; |
---|
153 | reload = 1; |
---|
154 | } |
---|
155 | else if((event & 0x00ffffff) == 2) |
---|
156 | { |
---|
157 | if(items) current = (items + current - 1) % items; |
---|
158 | reload = 1; |
---|
159 | } |
---|
160 | } |
---|
161 | else if(event & CACA_EVENT_KEY_PRESS) switch(event & 0x00ffffff) |
---|
162 | { |
---|
163 | case 'n': |
---|
164 | case 'N': |
---|
165 | if(items) current = (current + 1) % items; |
---|
166 | reload = 1; |
---|
167 | break; |
---|
168 | case 'p': |
---|
169 | case 'P': |
---|
170 | if(items) current = (items + current - 1) % items; |
---|
171 | reload = 1; |
---|
172 | break; |
---|
173 | case 'f': |
---|
174 | case 'F': |
---|
175 | fullscreen = ~fullscreen; |
---|
176 | update = 1; |
---|
177 | set_zoom(zoom); |
---|
178 | break; |
---|
179 | case 'b': |
---|
180 | i = 1 + cucul_get_feature(qq, CUCUL_BACKGROUND); |
---|
181 | if(i > CUCUL_BACKGROUND_MAX) i = CUCUL_BACKGROUND_MIN; |
---|
182 | cucul_set_feature(qq, i); |
---|
183 | new_status = STATUS_BACKGROUND; |
---|
184 | update = 1; |
---|
185 | break; |
---|
186 | case 'B': |
---|
187 | i = -1 + cucul_get_feature(qq, CUCUL_BACKGROUND); |
---|
188 | if(i < CUCUL_BACKGROUND_MIN) i = CUCUL_BACKGROUND_MAX; |
---|
189 | cucul_set_feature(qq, i); |
---|
190 | new_status = STATUS_BACKGROUND; |
---|
191 | update = 1; |
---|
192 | break; |
---|
193 | case 'a': |
---|
194 | i = 1 + cucul_get_feature(qq, CUCUL_ANTIALIASING); |
---|
195 | if(i > CUCUL_ANTIALIASING_MAX) i = CUCUL_ANTIALIASING_MIN; |
---|
196 | cucul_set_feature(qq, i); |
---|
197 | new_status = STATUS_ANTIALIASING; |
---|
198 | update = 1; |
---|
199 | break; |
---|
200 | case 'A': |
---|
201 | i = -1 + cucul_get_feature(qq, CUCUL_ANTIALIASING); |
---|
202 | if(i < CUCUL_ANTIALIASING_MIN) i = CUCUL_ANTIALIASING_MAX; |
---|
203 | cucul_set_feature(qq, i); |
---|
204 | new_status = STATUS_ANTIALIASING; |
---|
205 | update = 1; |
---|
206 | break; |
---|
207 | case 'd': |
---|
208 | i = 1 + cucul_get_feature(qq, CUCUL_DITHERING); |
---|
209 | if(i > CUCUL_DITHERING_MAX) i = CUCUL_DITHERING_MIN; |
---|
210 | cucul_set_feature(qq, i); |
---|
211 | new_status = STATUS_DITHERING; |
---|
212 | update = 1; |
---|
213 | break; |
---|
214 | case 'D': |
---|
215 | i = -1 + cucul_get_feature(qq, CUCUL_DITHERING); |
---|
216 | if(i < CUCUL_DITHERING_MIN) i = CUCUL_DITHERING_MAX; |
---|
217 | cucul_set_feature(qq, i); |
---|
218 | new_status = STATUS_DITHERING; |
---|
219 | update = 1; |
---|
220 | break; |
---|
221 | case '+': |
---|
222 | update = 1; |
---|
223 | set_zoom(zoom + 1); |
---|
224 | break; |
---|
225 | case '-': |
---|
226 | update = 1; |
---|
227 | set_zoom(zoom - 1); |
---|
228 | break; |
---|
229 | case 'x': |
---|
230 | case 'X': |
---|
231 | update = 1; |
---|
232 | set_zoom(0); |
---|
233 | break; |
---|
234 | case 'k': |
---|
235 | case 'K': |
---|
236 | case CACA_KEY_UP: |
---|
237 | if(yfactor > 1.0) dy -= PAD_STEP / yfactor; |
---|
238 | if(dy < 0.0) dy = 0.0; |
---|
239 | update = 1; |
---|
240 | break; |
---|
241 | case 'j': |
---|
242 | case 'J': |
---|
243 | case CACA_KEY_DOWN: |
---|
244 | if(yfactor > 1.0) dy += PAD_STEP / yfactor; |
---|
245 | if(dy > 1.0) dy = 1.0; |
---|
246 | update = 1; |
---|
247 | break; |
---|
248 | case 'h': |
---|
249 | case 'H': |
---|
250 | case CACA_KEY_LEFT: |
---|
251 | if(xfactor > 1.0) dx -= PAD_STEP / xfactor; |
---|
252 | if(dx < 0.0) dx = 0.0; |
---|
253 | update = 1; |
---|
254 | break; |
---|
255 | case 'l': |
---|
256 | case 'L': |
---|
257 | case CACA_KEY_RIGHT: |
---|
258 | if(xfactor > 1.0) dx += PAD_STEP / xfactor; |
---|
259 | if(dx > 1.0) dx = 1.0; |
---|
260 | update = 1; |
---|
261 | break; |
---|
262 | case '?': |
---|
263 | new_help = !help; |
---|
264 | update = 1; |
---|
265 | break; |
---|
266 | case 'q': |
---|
267 | case 'Q': |
---|
268 | quit = 1; |
---|
269 | break; |
---|
270 | } |
---|
271 | else if(event == CACA_EVENT_RESIZE) |
---|
272 | { |
---|
273 | caca_display(kk); |
---|
274 | ww = cucul_get_width(qq); |
---|
275 | wh = cucul_get_height(qq); |
---|
276 | update = 1; |
---|
277 | set_zoom(zoom); |
---|
278 | } |
---|
279 | |
---|
280 | if(status || new_status) |
---|
281 | status = new_status; |
---|
282 | |
---|
283 | if(help || new_help) |
---|
284 | help = new_help; |
---|
285 | |
---|
286 | event = caca_get_event(kk, CACA_EVENT_KEY_PRESS); |
---|
287 | } |
---|
288 | |
---|
289 | if(items && reload) |
---|
290 | { |
---|
291 | char *buffer; |
---|
292 | int len = strlen(" Loading `%s'... ") + strlen(list[current]); |
---|
293 | |
---|
294 | if(len < ww + 1) |
---|
295 | len = ww + 1; |
---|
296 | |
---|
297 | buffer = malloc(len); |
---|
298 | |
---|
299 | sprintf(buffer, " Loading `%s'... ", list[current]); |
---|
300 | buffer[ww] = '\0'; |
---|
301 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
302 | cucul_putstr(qq, (ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
303 | caca_display(kk); |
---|
304 | ww = cucul_get_width(qq); |
---|
305 | wh = cucul_get_height(qq); |
---|
306 | |
---|
307 | unload_image(); |
---|
308 | load_image(list[current]); |
---|
309 | reload = 0; |
---|
310 | |
---|
311 | /* Reset image-specific runtime variables */ |
---|
312 | dx = dy = 0.5; |
---|
313 | update = 1; |
---|
314 | set_zoom(0); |
---|
315 | |
---|
316 | free(buffer); |
---|
317 | } |
---|
318 | |
---|
319 | cucul_clear(qq); |
---|
320 | |
---|
321 | if(!items) |
---|
322 | { |
---|
323 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
324 | cucul_printf(qq, ww / 2 - 5, wh / 2, " No image. "); |
---|
325 | } |
---|
326 | else if(!pixels) |
---|
327 | { |
---|
328 | #if defined(HAVE_IMLIB2_H) |
---|
329 | # define ERROR_STRING " Error loading `%s'. " |
---|
330 | #else |
---|
331 | # define ERROR_STRING " Error loading `%s'. Only BMP is supported. " |
---|
332 | #endif |
---|
333 | char *buffer; |
---|
334 | int len = strlen(ERROR_STRING) + strlen(list[current]); |
---|
335 | |
---|
336 | if(len < ww + 1) |
---|
337 | len = ww + 1; |
---|
338 | |
---|
339 | buffer = malloc(len); |
---|
340 | |
---|
341 | sprintf(buffer, ERROR_STRING, list[current]); |
---|
342 | buffer[ww] = '\0'; |
---|
343 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
344 | cucul_putstr(qq, (ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
345 | free(buffer); |
---|
346 | } |
---|
347 | else |
---|
348 | { |
---|
349 | float xdelta, ydelta; |
---|
350 | int y, height; |
---|
351 | |
---|
352 | y = fullscreen ? 0 : 1; |
---|
353 | height = fullscreen ? wh : wh - 3; |
---|
354 | |
---|
355 | xdelta = (xfactor > 1.0) ? dx : 0.5; |
---|
356 | ydelta = (yfactor > 1.0) ? dy : 0.5; |
---|
357 | |
---|
358 | draw_checkers(ww * (1.0 - xfactor) / 2, |
---|
359 | y + height * (1.0 - yfactor) / 2, |
---|
360 | ww * (1.0 + xfactor) / 2, |
---|
361 | y + height * (1.0 + yfactor) / 2); |
---|
362 | |
---|
363 | cucul_draw_bitmap(qq, ww * (1.0 - xfactor) * xdelta, |
---|
364 | y + height * (1.0 - yfactor) * ydelta, |
---|
365 | ww * (xdelta + (1.0 - xdelta) * xfactor), |
---|
366 | y + height * (ydelta + (1.0 - ydelta) * yfactor), |
---|
367 | bitmap, pixels); |
---|
368 | } |
---|
369 | |
---|
370 | if(!fullscreen) |
---|
371 | { |
---|
372 | print_status(); |
---|
373 | |
---|
374 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
375 | switch(status) |
---|
376 | { |
---|
377 | case STATUS_ANTIALIASING: |
---|
378 | cucul_printf(qq, 0, wh - 1, "Antialiasing: %s", |
---|
379 | cucul_get_feature_name(cucul_get_feature(qq, CUCUL_ANTIALIASING))); |
---|
380 | break; |
---|
381 | case STATUS_DITHERING: |
---|
382 | cucul_printf(qq, 0, wh - 1, "Dithering: %s", |
---|
383 | cucul_get_feature_name(cucul_get_feature(qq, CUCUL_DITHERING))); |
---|
384 | break; |
---|
385 | case STATUS_BACKGROUND: |
---|
386 | cucul_printf(qq, 0, wh - 1, "Background: %s", |
---|
387 | cucul_get_feature_name(cucul_get_feature(qq, CUCUL_BACKGROUND))); |
---|
388 | break; |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | if(help) |
---|
393 | { |
---|
394 | print_help(ww - 25, 2); |
---|
395 | } |
---|
396 | |
---|
397 | caca_display(kk); |
---|
398 | update = 0; |
---|
399 | } |
---|
400 | |
---|
401 | /* Clean up */ |
---|
402 | unload_image(); |
---|
403 | caca_detach(kk); |
---|
404 | cucul_end(qq); |
---|
405 | |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | static void print_status(void) |
---|
410 | { |
---|
411 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
412 | cucul_draw_line(qq, 0, 0, ww - 1, 0, ' '); |
---|
413 | cucul_draw_line(qq, 0, wh - 2, ww - 1, wh - 2, '-'); |
---|
414 | cucul_putstr(qq, 0, 0, "q:Quit np:Next/Prev +-x:Zoom " |
---|
415 | "hjkl:Move d:Dithering a:Antialias"); |
---|
416 | cucul_putstr(qq, ww - strlen("?:Help"), 0, "?:Help"); |
---|
417 | cucul_printf(qq, 3, wh - 2, "cacaview %s", VERSION); |
---|
418 | cucul_printf(qq, ww - 14, wh - 2, "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom); |
---|
419 | |
---|
420 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
421 | cucul_draw_line(qq, 0, wh - 1, ww - 1, wh - 1, ' '); |
---|
422 | } |
---|
423 | |
---|
424 | static void print_help(int x, int y) |
---|
425 | { |
---|
426 | static char const *help[] = |
---|
427 | { |
---|
428 | " +: zoom in ", |
---|
429 | " -: zoom out ", |
---|
430 | " x: reset zoom ", |
---|
431 | " ---------------------- ", |
---|
432 | " hjkl: move view ", |
---|
433 | " arrows: move view ", |
---|
434 | " ---------------------- ", |
---|
435 | " a: antialiasing method ", |
---|
436 | " d: dithering method ", |
---|
437 | " b: background mode ", |
---|
438 | " ---------------------- ", |
---|
439 | " ?: help ", |
---|
440 | " q: quit ", |
---|
441 | NULL |
---|
442 | }; |
---|
443 | |
---|
444 | int i; |
---|
445 | |
---|
446 | cucul_set_color(qq, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
447 | |
---|
448 | for(i = 0; help[i]; i++) |
---|
449 | cucul_putstr(qq, x, y + i, help[i]); |
---|
450 | } |
---|
451 | |
---|
452 | static void set_zoom(int new_zoom) |
---|
453 | { |
---|
454 | int height; |
---|
455 | |
---|
456 | zoom = new_zoom; |
---|
457 | |
---|
458 | if(zoom > ZOOM_MAX) zoom = ZOOM_MAX; |
---|
459 | if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX; |
---|
460 | |
---|
461 | ww = cucul_get_width(qq); |
---|
462 | height = fullscreen ? wh : wh - 3; |
---|
463 | |
---|
464 | xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom]; |
---|
465 | yfactor = xfactor * ww / height * h / w |
---|
466 | * cucul_get_height(qq) / cucul_get_width(qq) |
---|
467 | * caca_get_window_width(kk) / caca_get_window_height(kk); |
---|
468 | |
---|
469 | if(yfactor > xfactor) |
---|
470 | { |
---|
471 | float tmp = xfactor; |
---|
472 | xfactor = tmp * tmp / yfactor; |
---|
473 | yfactor = tmp; |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | static void unload_image(void) |
---|
478 | { |
---|
479 | #if defined(HAVE_IMLIB2_H) |
---|
480 | if(image) |
---|
481 | imlib_free_image(); |
---|
482 | image = NULL; |
---|
483 | pixels = NULL; |
---|
484 | #else |
---|
485 | if(pixels) |
---|
486 | free(pixels); |
---|
487 | pixels = NULL; |
---|
488 | #endif |
---|
489 | if(bitmap) |
---|
490 | cucul_free_bitmap(qq, bitmap); |
---|
491 | bitmap = NULL; |
---|
492 | } |
---|
493 | |
---|
494 | static void load_image(char const *name) |
---|
495 | { |
---|
496 | #if defined(HAVE_IMLIB2_H) |
---|
497 | /* Load the new image */ |
---|
498 | image = imlib_load_image(name); |
---|
499 | |
---|
500 | if(!image) |
---|
501 | return; |
---|
502 | |
---|
503 | imlib_context_set_image(image); |
---|
504 | pixels = (char *)imlib_image_get_data_for_reading_only(); |
---|
505 | w = imlib_image_get_width(); |
---|
506 | h = imlib_image_get_height(); |
---|
507 | rmask = 0x00ff0000; |
---|
508 | gmask = 0x0000ff00; |
---|
509 | bmask = 0x000000ff; |
---|
510 | amask = 0xff000000; |
---|
511 | bpp = 32; |
---|
512 | depth = 4; |
---|
513 | |
---|
514 | /* Create the libcucul bitmap */ |
---|
515 | bitmap = cucul_create_bitmap(qq, bpp, w, h, depth * w, |
---|
516 | rmask, gmask, bmask, amask); |
---|
517 | if(!bitmap) |
---|
518 | { |
---|
519 | imlib_free_image(); |
---|
520 | image = NULL; |
---|
521 | } |
---|
522 | |
---|
523 | #else |
---|
524 | /* Try to load a BMP file */ |
---|
525 | FILE *fp; |
---|
526 | unsigned int i, colors, offset, tmp, planes; |
---|
527 | |
---|
528 | fp = fopen(name, "rb"); |
---|
529 | if(!fp) |
---|
530 | return; |
---|
531 | |
---|
532 | if(freadshort(fp) != 0x4d42) |
---|
533 | { |
---|
534 | fclose(fp); |
---|
535 | return; |
---|
536 | } |
---|
537 | |
---|
538 | freadint(fp); /* size */ |
---|
539 | freadshort(fp); /* reserved 1 */ |
---|
540 | freadshort(fp); /* reserved 2 */ |
---|
541 | |
---|
542 | offset = freadint(fp); |
---|
543 | |
---|
544 | tmp = freadint(fp); /* header size */ |
---|
545 | if(tmp == 40) |
---|
546 | { |
---|
547 | w = freadint(fp); |
---|
548 | h = freadint(fp); |
---|
549 | planes = freadshort(fp); |
---|
550 | bpp = freadshort(fp); |
---|
551 | |
---|
552 | tmp = freadint(fp); /* compression */ |
---|
553 | if(tmp != 0) |
---|
554 | { |
---|
555 | fclose(fp); |
---|
556 | return; |
---|
557 | } |
---|
558 | |
---|
559 | freadint(fp); /* sizeimage */ |
---|
560 | freadint(fp); /* xpelspermeter */ |
---|
561 | freadint(fp); /* ypelspermeter */ |
---|
562 | freadint(fp); /* biclrused */ |
---|
563 | freadint(fp); /* biclrimportantn */ |
---|
564 | |
---|
565 | colors = (offset - 54) / 4; |
---|
566 | for(i = 0; i < colors && i < 256; i++) |
---|
567 | { |
---|
568 | blue[i] = freadchar(fp) * 16; |
---|
569 | green[i] = freadchar(fp) * 16; |
---|
570 | red[i] = freadchar(fp) * 16; |
---|
571 | alpha[i] = 0; |
---|
572 | freadchar(fp); |
---|
573 | } |
---|
574 | } |
---|
575 | else if(tmp == 12) |
---|
576 | { |
---|
577 | w = freadint(fp); |
---|
578 | h = freadint(fp); |
---|
579 | planes = freadshort(fp); |
---|
580 | bpp = freadshort(fp); |
---|
581 | |
---|
582 | colors = (offset - 26) / 3; |
---|
583 | for(i = 0; i < colors && i < 256; i++) |
---|
584 | { |
---|
585 | blue[i] = freadchar(fp); |
---|
586 | green[i] = freadchar(fp); |
---|
587 | red[i] = freadchar(fp); |
---|
588 | alpha[i] = 0; |
---|
589 | } |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | fclose(fp); |
---|
594 | return; |
---|
595 | } |
---|
596 | |
---|
597 | /* Fill the rest of the palette */ |
---|
598 | for(i = colors; i < 256; i++) |
---|
599 | blue[i] = green[i] = red[i] = alpha[i] = 0; |
---|
600 | |
---|
601 | depth = (bpp + 7) / 8; |
---|
602 | |
---|
603 | /* Sanity check */ |
---|
604 | if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/) |
---|
605 | { |
---|
606 | fclose(fp); |
---|
607 | return; |
---|
608 | } |
---|
609 | |
---|
610 | /* Allocate the pixel buffer */ |
---|
611 | pixels = malloc(w * h * depth); |
---|
612 | if(!pixels) |
---|
613 | { |
---|
614 | fclose(fp); |
---|
615 | return; |
---|
616 | } |
---|
617 | |
---|
618 | memset(pixels, 0, w * h * depth); |
---|
619 | |
---|
620 | /* Read the bitmap data */ |
---|
621 | for(i = h; i--; ) |
---|
622 | { |
---|
623 | unsigned int j, k, bits = 0; |
---|
624 | |
---|
625 | switch(bpp) |
---|
626 | { |
---|
627 | case 1: |
---|
628 | for(j = 0; j < w; j++) |
---|
629 | { |
---|
630 | k = j % 32; |
---|
631 | if(k == 0) |
---|
632 | bits = freadint(fp); |
---|
633 | pixels[w * i * depth + j] = |
---|
634 | (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1; |
---|
635 | } |
---|
636 | break; |
---|
637 | case 4: |
---|
638 | for(j = 0; j < w; j++) |
---|
639 | { |
---|
640 | k = j % 8; |
---|
641 | if(k == 0) |
---|
642 | bits = freadint(fp); |
---|
643 | pixels[w * i * depth + j] = |
---|
644 | (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf; |
---|
645 | } |
---|
646 | break; |
---|
647 | default: |
---|
648 | /* Works for 8bpp, but also for 16, 24 etc. */ |
---|
649 | fread(pixels + w * i * depth, w * depth, 1, fp); |
---|
650 | /* Pad reads to 4 bytes */ |
---|
651 | tmp = (w * depth) % 4; |
---|
652 | tmp = (4 - tmp) % 4; |
---|
653 | while(tmp--) |
---|
654 | freadchar(fp); |
---|
655 | break; |
---|
656 | } |
---|
657 | } |
---|
658 | |
---|
659 | switch(depth) |
---|
660 | { |
---|
661 | case 3: |
---|
662 | rmask = 0xff0000; |
---|
663 | gmask = 0x00ff00; |
---|
664 | bmask = 0x0000ff; |
---|
665 | amask = 0x000000; |
---|
666 | break; |
---|
667 | case 2: /* XXX: those are the 16 bits values */ |
---|
668 | rmask = 0x7c00; |
---|
669 | gmask = 0x03e0; |
---|
670 | bmask = 0x001f; |
---|
671 | amask = 0x0000; |
---|
672 | break; |
---|
673 | case 1: |
---|
674 | default: |
---|
675 | bpp = 8; |
---|
676 | rmask = gmask = bmask = amask = 0; |
---|
677 | break; |
---|
678 | } |
---|
679 | |
---|
680 | fclose(fp); |
---|
681 | |
---|
682 | /* Create the libcaca bitmap */ |
---|
683 | bitmap = cucul_create_bitmap(qq, bpp, w, h, depth * w, |
---|
684 | rmask, gmask, bmask, amask); |
---|
685 | if(!bitmap) |
---|
686 | { |
---|
687 | free(pixels); |
---|
688 | pixels = NULL; |
---|
689 | return; |
---|
690 | } |
---|
691 | |
---|
692 | if(bpp == 8) |
---|
693 | cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha); |
---|
694 | #endif |
---|
695 | } |
---|
696 | |
---|
697 | static void draw_checkers(int x1, int y1, int x2, int y2) |
---|
698 | { |
---|
699 | int xn, yn; |
---|
700 | |
---|
701 | if(x2 + 1 > (int)cucul_get_width(qq)) x2 = cucul_get_width(qq) - 1; |
---|
702 | if(y2 + 1 > (int)cucul_get_height(qq)) y2 = cucul_get_height(qq) - 1; |
---|
703 | |
---|
704 | for(yn = y1 > 0 ? y1 : 0; yn <= y2; yn++) |
---|
705 | for(xn = x1 > 0 ? x1 : 0; xn <= x2; xn++) |
---|
706 | { |
---|
707 | if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1) |
---|
708 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_DARKGRAY); |
---|
709 | else |
---|
710 | cucul_set_color(qq, CUCUL_COLOR_DARKGRAY, CUCUL_COLOR_LIGHTGRAY); |
---|
711 | cucul_putchar(qq, xn, yn, ' '); |
---|
712 | } |
---|
713 | } |
---|
714 | |
---|
715 | #if !defined(HAVE_IMLIB2_H) |
---|
716 | static int freadint(FILE *fp) |
---|
717 | { |
---|
718 | unsigned char buffer[4]; |
---|
719 | fread(buffer, 4, 1, fp); |
---|
720 | return ((int)buffer[3] << 24) | ((int)buffer[2] << 16) |
---|
721 | | ((int)buffer[1] << 8) | ((int)buffer[0]); |
---|
722 | } |
---|
723 | |
---|
724 | static int freadshort(FILE *fp) |
---|
725 | { |
---|
726 | unsigned char buffer[2]; |
---|
727 | fread(buffer, 2, 1, fp); |
---|
728 | return ((int)buffer[1] << 8) | ((int)buffer[0]); |
---|
729 | } |
---|
730 | |
---|
731 | static int freadchar(FILE *fp) |
---|
732 | { |
---|
733 | unsigned char buffer; |
---|
734 | fread(&buffer, 1, 1, fp); |
---|
735 | return (int)buffer; |
---|
736 | } |
---|
737 | #endif |
---|
738 | |
---|