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 314 2004-01-05 18:08:50Z sam $ |
---|
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 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <unistd.h> |
---|
30 | |
---|
31 | #if defined(HAVE_IMLIB2_H) |
---|
32 | # include <Imlib2.h> |
---|
33 | #else |
---|
34 | # include <stdio.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | #include "caca.h" |
---|
38 | |
---|
39 | /* Local macros */ |
---|
40 | #define STATUS_DITHERING 1 |
---|
41 | #define STATUS_ANTIALIASING 2 |
---|
42 | #define STATUS_BACKGROUND 3 |
---|
43 | |
---|
44 | /* Local functions */ |
---|
45 | static void load_image(char const *); |
---|
46 | static void unload_image(void); |
---|
47 | static void draw_checkers(unsigned int, unsigned int, |
---|
48 | unsigned int, unsigned int); |
---|
49 | #if !defined(HAVE_IMLIB2_H) |
---|
50 | static int freadint(FILE *); |
---|
51 | static int freadshort(FILE *); |
---|
52 | static int freadchar(FILE *); |
---|
53 | #endif |
---|
54 | |
---|
55 | /* Local variables */ |
---|
56 | #if defined(HAVE_IMLIB2_H) |
---|
57 | Imlib_Image image = NULL; |
---|
58 | #endif |
---|
59 | char *pixels = NULL; |
---|
60 | struct caca_bitmap *bitmap = NULL; |
---|
61 | int x, y; |
---|
62 | unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask; |
---|
63 | #if !defined(HAVE_IMLIB2_H) |
---|
64 | unsigned int red[256], green[256], blue[256], alpha[256]; |
---|
65 | #endif |
---|
66 | |
---|
67 | int main(int argc, char **argv) |
---|
68 | { |
---|
69 | int quit = 0, update = 1, help = 0, fullscreen = 0, status = 0; |
---|
70 | int reload = 0, zoom = 0; |
---|
71 | |
---|
72 | char **list = NULL; |
---|
73 | int current = 0, items = 0, opts = 1; |
---|
74 | int i; |
---|
75 | |
---|
76 | /* Initialise libcaca */ |
---|
77 | if(caca_init()) |
---|
78 | { |
---|
79 | fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); |
---|
80 | return 1; |
---|
81 | } |
---|
82 | |
---|
83 | /* Load items into playlist */ |
---|
84 | for(i = 1; i < argc; i++) |
---|
85 | { |
---|
86 | /* Skip options except after `--' */ |
---|
87 | if(opts && argv[i][0] == '-') |
---|
88 | { |
---|
89 | if(argv[i][1] == '-' && argv[i][2] == '\0') |
---|
90 | opts = 0; |
---|
91 | continue; |
---|
92 | } |
---|
93 | |
---|
94 | /* Add argv[i] to the list */ |
---|
95 | if(items) |
---|
96 | list = realloc(list, (items + 1) * sizeof(char *)); |
---|
97 | else |
---|
98 | list = malloc(sizeof(char *)); |
---|
99 | list[items] = argv[i]; |
---|
100 | items++; |
---|
101 | |
---|
102 | reload = 1; |
---|
103 | } |
---|
104 | |
---|
105 | /* Go ! */ |
---|
106 | while(!quit) |
---|
107 | { |
---|
108 | int ww = caca_get_width(); |
---|
109 | int wh = caca_get_height(); |
---|
110 | |
---|
111 | int event, new_status = 0, new_help = 0; |
---|
112 | |
---|
113 | while((event = caca_get_event())) |
---|
114 | { |
---|
115 | switch(event) |
---|
116 | { |
---|
117 | case CACA_EVENT_KEY_PRESS | 'n': |
---|
118 | case CACA_EVENT_KEY_PRESS | 'N': |
---|
119 | if(items) current = (current + 1) % items; |
---|
120 | reload = 1; |
---|
121 | break; |
---|
122 | case CACA_EVENT_KEY_PRESS | 'p': |
---|
123 | case CACA_EVENT_KEY_PRESS | 'P': |
---|
124 | if(items) current = (items + current - 1) % items; |
---|
125 | reload = 1; |
---|
126 | break; |
---|
127 | case CACA_EVENT_KEY_PRESS | 'f': |
---|
128 | case CACA_EVENT_KEY_PRESS | 'F': |
---|
129 | fullscreen = ~fullscreen; |
---|
130 | update = 1; |
---|
131 | break; |
---|
132 | case CACA_EVENT_KEY_PRESS | 'b': |
---|
133 | i = 1 + caca_get_feature(CACA_BACKGROUND); |
---|
134 | if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN; |
---|
135 | caca_set_feature(i); |
---|
136 | new_status = STATUS_BACKGROUND; |
---|
137 | update = 1; |
---|
138 | break; |
---|
139 | case CACA_EVENT_KEY_PRESS | 'B': |
---|
140 | i = -1 + caca_get_feature(CACA_BACKGROUND); |
---|
141 | if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX; |
---|
142 | caca_set_feature(i); |
---|
143 | new_status = STATUS_BACKGROUND; |
---|
144 | update = 1; |
---|
145 | break; |
---|
146 | case CACA_EVENT_KEY_PRESS | 'a': |
---|
147 | i = 1 + caca_get_feature(CACA_ANTIALIASING); |
---|
148 | if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN; |
---|
149 | caca_set_feature(i); |
---|
150 | new_status = STATUS_ANTIALIASING; |
---|
151 | update = 1; |
---|
152 | break; |
---|
153 | case CACA_EVENT_KEY_PRESS | 'A': |
---|
154 | i = -1 + caca_get_feature(CACA_ANTIALIASING); |
---|
155 | if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX; |
---|
156 | caca_set_feature(i); |
---|
157 | new_status = STATUS_ANTIALIASING; |
---|
158 | update = 1; |
---|
159 | break; |
---|
160 | case CACA_EVENT_KEY_PRESS | 'd': |
---|
161 | i = 1 + caca_get_feature(CACA_DITHERING); |
---|
162 | if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN; |
---|
163 | caca_set_feature(i); |
---|
164 | new_status = STATUS_DITHERING; |
---|
165 | update = 1; |
---|
166 | break; |
---|
167 | case CACA_EVENT_KEY_PRESS | 'D': |
---|
168 | i = -1 + caca_get_feature(CACA_DITHERING); |
---|
169 | if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX; |
---|
170 | caca_set_feature(i); |
---|
171 | new_status = STATUS_DITHERING; |
---|
172 | update = 1; |
---|
173 | break; |
---|
174 | case CACA_EVENT_KEY_PRESS | '+': |
---|
175 | zoom++; |
---|
176 | if(zoom > 48) zoom = 48; else update = 1; |
---|
177 | break; |
---|
178 | case CACA_EVENT_KEY_PRESS | '-': |
---|
179 | zoom--; |
---|
180 | if(zoom < -48) zoom = -48; else update = 1; |
---|
181 | break; |
---|
182 | case CACA_EVENT_KEY_PRESS | 'x': |
---|
183 | case CACA_EVENT_KEY_PRESS | 'X': |
---|
184 | zoom = 0; |
---|
185 | update = 1; |
---|
186 | break; |
---|
187 | case CACA_EVENT_KEY_PRESS | 'k': |
---|
188 | case CACA_EVENT_KEY_PRESS | 'K': |
---|
189 | case CACA_EVENT_KEY_PRESS | CACA_KEY_UP: |
---|
190 | if(zoom > 0) y -= 1 + h / (2 + zoom) / 8; |
---|
191 | update = 1; |
---|
192 | break; |
---|
193 | case CACA_EVENT_KEY_PRESS | 'j': |
---|
194 | case CACA_EVENT_KEY_PRESS | 'J': |
---|
195 | case CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN: |
---|
196 | if(zoom > 0) y += 1 + h / (2 + zoom) / 8; |
---|
197 | update = 1; |
---|
198 | break; |
---|
199 | case CACA_EVENT_KEY_PRESS | 'h': |
---|
200 | case CACA_EVENT_KEY_PRESS | 'H': |
---|
201 | case CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT: |
---|
202 | if(zoom > 0) x -= 1 + w / (2 + zoom) / 8; |
---|
203 | update = 1; |
---|
204 | break; |
---|
205 | case CACA_EVENT_KEY_PRESS | 'l': |
---|
206 | case CACA_EVENT_KEY_PRESS | 'L': |
---|
207 | case CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT: |
---|
208 | if(zoom > 0) x += 1 + w / (2 + zoom) / 8; |
---|
209 | update = 1; |
---|
210 | break; |
---|
211 | case CACA_EVENT_KEY_PRESS | '?': |
---|
212 | new_help = !help; |
---|
213 | update = 1; |
---|
214 | break; |
---|
215 | case CACA_EVENT_KEY_PRESS | 'q': |
---|
216 | case CACA_EVENT_KEY_PRESS | 'Q': |
---|
217 | quit = 1; |
---|
218 | break; |
---|
219 | } |
---|
220 | |
---|
221 | if(status || new_status) |
---|
222 | status = new_status; |
---|
223 | |
---|
224 | if(help || new_help) |
---|
225 | help = new_help; |
---|
226 | } |
---|
227 | |
---|
228 | if(items && reload) |
---|
229 | { |
---|
230 | char *buffer; |
---|
231 | int len = strlen(" Loading `%s'... ") + strlen(list[current]); |
---|
232 | |
---|
233 | if(len < ww + 1) |
---|
234 | len = ww + 1; |
---|
235 | |
---|
236 | buffer = malloc(len); |
---|
237 | |
---|
238 | /* Reset image-specific runtime variables */ |
---|
239 | zoom = 0; |
---|
240 | |
---|
241 | sprintf(buffer, " Loading `%s'... ", list[current]); |
---|
242 | buffer[ww] = '\0'; |
---|
243 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
244 | caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
245 | caca_refresh(); |
---|
246 | |
---|
247 | unload_image(); |
---|
248 | load_image(list[current]); |
---|
249 | reload = 0; |
---|
250 | update = 1; |
---|
251 | |
---|
252 | free(buffer); |
---|
253 | } |
---|
254 | |
---|
255 | if(!update) |
---|
256 | { |
---|
257 | usleep(10000); |
---|
258 | continue; |
---|
259 | } |
---|
260 | |
---|
261 | caca_clear(); |
---|
262 | |
---|
263 | if(!items) |
---|
264 | { |
---|
265 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
266 | caca_printf(ww / 2 - 5, wh / 2, " No image. "); |
---|
267 | } |
---|
268 | else if(!pixels) |
---|
269 | { |
---|
270 | #if defined(HAVE_IMLIB2_H) |
---|
271 | # define ERROR_STRING " Error loading `%s'. " |
---|
272 | #else |
---|
273 | # define ERROR_STRING " Error loading `%s'. Only BMP is supported. " |
---|
274 | #endif |
---|
275 | char *buffer; |
---|
276 | int len = strlen(ERROR_STRING) + strlen(list[current]); |
---|
277 | |
---|
278 | if(len < ww + 1) |
---|
279 | len = ww + 1; |
---|
280 | |
---|
281 | buffer = malloc(len); |
---|
282 | |
---|
283 | sprintf(buffer, ERROR_STRING, list[current]); |
---|
284 | buffer[ww] = '\0'; |
---|
285 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
286 | caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer); |
---|
287 | free(buffer); |
---|
288 | } |
---|
289 | else if(zoom < 0) |
---|
290 | { |
---|
291 | int xo = (ww - 1) / 2; |
---|
292 | int yo = (wh - 1) / 2; |
---|
293 | int xn = (ww - 1) / (2 - zoom); |
---|
294 | int yn = (wh - 1) / (2 - zoom); |
---|
295 | draw_checkers(xo - xn, yo - yn, xo + xn, yo + yn); |
---|
296 | caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn, |
---|
297 | bitmap, pixels); |
---|
298 | } |
---|
299 | else if(zoom > 0) |
---|
300 | { |
---|
301 | struct caca_bitmap *newbitmap; |
---|
302 | int xn = w / (2 + zoom); |
---|
303 | int yn = h / (2 + zoom); |
---|
304 | if(x < xn) x = xn; |
---|
305 | if(y < yn) y = yn; |
---|
306 | if(xn + x > (int)w) x = w - xn; |
---|
307 | if(yn + y > (int)h) y = h - yn; |
---|
308 | newbitmap = caca_create_bitmap(bpp, 2 * xn, 2 * yn, depth * w, |
---|
309 | rmask, gmask, bmask, amask); |
---|
310 | #if !defined(HAVE_IMLIB2_H) |
---|
311 | if(bpp == 8) |
---|
312 | caca_set_bitmap_palette(newbitmap, red, green, blue, alpha); |
---|
313 | #endif |
---|
314 | draw_checkers(0, fullscreen ? 0 : 1, |
---|
315 | ww - 1, fullscreen ? wh - 1 : wh - 3); |
---|
316 | caca_draw_bitmap(0, fullscreen ? 0 : 1, |
---|
317 | ww - 1, fullscreen ? wh - 1 : wh - 3, |
---|
318 | newbitmap, |
---|
319 | pixels + depth * (x - xn) + depth * w * (y - yn)); |
---|
320 | caca_free_bitmap(newbitmap); |
---|
321 | } |
---|
322 | else |
---|
323 | { |
---|
324 | draw_checkers(0, fullscreen ? 0 : 1, |
---|
325 | ww - 1, fullscreen ? wh - 1 : wh - 3); |
---|
326 | caca_draw_bitmap(0, fullscreen ? 0 : 1, |
---|
327 | ww - 1, fullscreen ? wh - 1 : wh - 3, |
---|
328 | bitmap, pixels); |
---|
329 | } |
---|
330 | |
---|
331 | if(!fullscreen) |
---|
332 | { |
---|
333 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
334 | caca_draw_line(0, 0, ww - 1, 0, ' '); |
---|
335 | caca_draw_line(0, wh - 2, ww - 1, wh - 2, '-'); |
---|
336 | caca_putstr(0, 0, "q:Quit np:Next/Prev +-x:Zoom " |
---|
337 | "hjkl:Move d:Dithering a:Antialias"); |
---|
338 | caca_putstr(ww - strlen("?:Help"), 0, "?:Help"); |
---|
339 | caca_printf(3, wh - 2, "cacaview %s", VERSION); |
---|
340 | caca_printf(ww - 14, wh - 2, |
---|
341 | "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom); |
---|
342 | |
---|
343 | caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK); |
---|
344 | caca_draw_line(0, wh - 1, ww - 1, wh - 1, ' '); |
---|
345 | switch(status) |
---|
346 | { |
---|
347 | case STATUS_ANTIALIASING: |
---|
348 | caca_printf(0, wh - 1, "Antialiasing: %s", |
---|
349 | caca_get_feature_name(caca_get_feature(CACA_ANTIALIASING))); |
---|
350 | break; |
---|
351 | case STATUS_DITHERING: |
---|
352 | caca_printf(0, wh - 1, "Dithering: %s", |
---|
353 | caca_get_feature_name(caca_get_feature(CACA_DITHERING))); |
---|
354 | break; |
---|
355 | case STATUS_BACKGROUND: |
---|
356 | caca_printf(0, wh - 1, "Background: %s", |
---|
357 | caca_get_feature_name(caca_get_feature(CACA_BACKGROUND))); |
---|
358 | break; |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | if(help) |
---|
363 | { |
---|
364 | caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE); |
---|
365 | caca_putstr(ww - 25, 2, " +: zoom in "); |
---|
366 | caca_putstr(ww - 25, 3, " -: zoom out "); |
---|
367 | caca_putstr(ww - 25, 4, " x: reset zoom "); |
---|
368 | caca_putstr(ww - 25, 5, " ---------------------- "); |
---|
369 | caca_putstr(ww - 25, 6, " hjkl: move view "); |
---|
370 | caca_putstr(ww - 25, 7, " arrows: move view "); |
---|
371 | caca_putstr(ww - 25, 8, " ---------------------- "); |
---|
372 | caca_putstr(ww - 25, 9, " a: antialiasing method "); |
---|
373 | caca_putstr(ww - 25, 10, " d: dithering method "); |
---|
374 | caca_putstr(ww - 25, 11, " b: background mode "); |
---|
375 | caca_putstr(ww - 25, 12, " ---------------------- "); |
---|
376 | caca_putstr(ww - 25, 13, " ?: help "); |
---|
377 | caca_putstr(ww - 25, 14, " q: quit "); |
---|
378 | } |
---|
379 | |
---|
380 | caca_refresh(); |
---|
381 | update = 0; |
---|
382 | } |
---|
383 | |
---|
384 | /* Clean up */ |
---|
385 | unload_image(); |
---|
386 | caca_end(); |
---|
387 | |
---|
388 | return 0; |
---|
389 | } |
---|
390 | |
---|
391 | static void unload_image(void) |
---|
392 | { |
---|
393 | #if defined(HAVE_IMLIB2_H) |
---|
394 | if(image) |
---|
395 | imlib_free_image(); |
---|
396 | image = NULL; |
---|
397 | pixels = NULL; |
---|
398 | #else |
---|
399 | if(pixels) |
---|
400 | free(pixels); |
---|
401 | pixels = NULL; |
---|
402 | #endif |
---|
403 | if(bitmap) |
---|
404 | caca_free_bitmap(bitmap); |
---|
405 | bitmap = NULL; |
---|
406 | } |
---|
407 | |
---|
408 | static void load_image(char const *name) |
---|
409 | { |
---|
410 | #if defined(HAVE_IMLIB2_H) |
---|
411 | /* Load the new image */ |
---|
412 | image = imlib_load_image(name); |
---|
413 | |
---|
414 | if(!image) |
---|
415 | return; |
---|
416 | |
---|
417 | imlib_context_set_image(image); |
---|
418 | pixels = (char *)imlib_image_get_data_for_reading_only(); |
---|
419 | w = imlib_image_get_width(); |
---|
420 | h = imlib_image_get_height(); |
---|
421 | rmask = 0x00ff0000; |
---|
422 | gmask = 0x0000ff00; |
---|
423 | bmask = 0x000000ff; |
---|
424 | amask = 0xff000000; |
---|
425 | bpp = 32; |
---|
426 | depth = 4; |
---|
427 | |
---|
428 | /* Create the libcaca bitmap */ |
---|
429 | bitmap = caca_create_bitmap(bpp, w, h, depth * w, |
---|
430 | rmask, gmask, bmask, amask); |
---|
431 | if(!bitmap) |
---|
432 | { |
---|
433 | imlib_free_image(); |
---|
434 | image = NULL; |
---|
435 | } |
---|
436 | |
---|
437 | #else |
---|
438 | /* Try to load a BMP file */ |
---|
439 | FILE *fp; |
---|
440 | unsigned int i, colors, offset, tmp, planes; |
---|
441 | |
---|
442 | fp = fopen(name, "rb"); |
---|
443 | if(!fp) |
---|
444 | return; |
---|
445 | |
---|
446 | if(freadshort(fp) != 0x4d42) |
---|
447 | { |
---|
448 | fclose(fp); |
---|
449 | return; |
---|
450 | } |
---|
451 | |
---|
452 | freadint(fp); /* size */ |
---|
453 | freadshort(fp); /* reserved 1 */ |
---|
454 | freadshort(fp); /* reserved 2 */ |
---|
455 | |
---|
456 | offset = freadint(fp); |
---|
457 | |
---|
458 | tmp = freadint(fp); /* header size */ |
---|
459 | if(tmp == 40) |
---|
460 | { |
---|
461 | w = freadint(fp); |
---|
462 | h = freadint(fp); |
---|
463 | planes = freadshort(fp); |
---|
464 | bpp = freadshort(fp); |
---|
465 | |
---|
466 | tmp = freadint(fp); /* compression */ |
---|
467 | if(tmp != 0) |
---|
468 | { |
---|
469 | fclose(fp); |
---|
470 | return; |
---|
471 | } |
---|
472 | |
---|
473 | freadint(fp); /* sizeimage */ |
---|
474 | freadint(fp); /* xpelspermeter */ |
---|
475 | freadint(fp); /* ypelspermeter */ |
---|
476 | freadint(fp); /* biclrused */ |
---|
477 | freadint(fp); /* biclrimportantn */ |
---|
478 | |
---|
479 | colors = (offset - 54) / 4; |
---|
480 | for(i = 0; i < colors && i < 256; i++) |
---|
481 | { |
---|
482 | blue[i] = freadchar(fp) * 16; |
---|
483 | green[i] = freadchar(fp) * 16; |
---|
484 | red[i] = freadchar(fp) * 16; |
---|
485 | alpha[i] = 0; |
---|
486 | freadchar(fp); |
---|
487 | } |
---|
488 | } |
---|
489 | else if(tmp == 12) |
---|
490 | { |
---|
491 | w = freadint(fp); |
---|
492 | h = freadint(fp); |
---|
493 | planes = freadshort(fp); |
---|
494 | bpp = freadshort(fp); |
---|
495 | |
---|
496 | colors = (offset - 26) / 3; |
---|
497 | for(i = 0; i < colors && i < 256; i++) |
---|
498 | { |
---|
499 | blue[i] = freadchar(fp); |
---|
500 | green[i] = freadchar(fp); |
---|
501 | red[i] = freadchar(fp); |
---|
502 | alpha[i] = 0; |
---|
503 | } |
---|
504 | } |
---|
505 | else |
---|
506 | { |
---|
507 | fclose(fp); |
---|
508 | return; |
---|
509 | } |
---|
510 | |
---|
511 | /* Fill the rest of the palette */ |
---|
512 | for(i = colors; i < 256; i++) |
---|
513 | blue[i] = green[i] = red[i] = alpha[i] = 0; |
---|
514 | |
---|
515 | depth = (bpp + 7) / 8; |
---|
516 | |
---|
517 | /* Sanity check */ |
---|
518 | if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/) |
---|
519 | { |
---|
520 | fclose(fp); |
---|
521 | return; |
---|
522 | } |
---|
523 | |
---|
524 | /* Allocate the pixel buffer */ |
---|
525 | pixels = malloc(w * h * depth); |
---|
526 | if(!pixels) |
---|
527 | { |
---|
528 | fclose(fp); |
---|
529 | return; |
---|
530 | } |
---|
531 | |
---|
532 | memset(pixels, 0, w * h * depth); |
---|
533 | |
---|
534 | /* Read the bitmap data */ |
---|
535 | for(i = h; i--; ) |
---|
536 | { |
---|
537 | unsigned int j, k, bits = 0; |
---|
538 | |
---|
539 | switch(bpp) |
---|
540 | { |
---|
541 | case 1: |
---|
542 | for(j = 0; j < w; j++) |
---|
543 | { |
---|
544 | k = j % 32; |
---|
545 | if(k == 0) |
---|
546 | bits = freadint(fp); |
---|
547 | pixels[w * i * depth + j] = |
---|
548 | (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1; |
---|
549 | } |
---|
550 | break; |
---|
551 | case 4: |
---|
552 | for(j = 0; j < w; j++) |
---|
553 | { |
---|
554 | k = j % 8; |
---|
555 | if(k == 0) |
---|
556 | bits = freadint(fp); |
---|
557 | pixels[w * i * depth + j] = |
---|
558 | (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf; |
---|
559 | } |
---|
560 | break; |
---|
561 | default: |
---|
562 | /* Works for 8bpp, but also for 16, 24 etc. */ |
---|
563 | fread(pixels + w * i * depth, w * depth, 1, fp); |
---|
564 | /* Pad reads to 4 bytes */ |
---|
565 | tmp = (w * depth) % 4; |
---|
566 | tmp = (4 - tmp) % 4; |
---|
567 | while(tmp--) |
---|
568 | freadchar(fp); |
---|
569 | break; |
---|
570 | } |
---|
571 | } |
---|
572 | |
---|
573 | switch(depth) |
---|
574 | { |
---|
575 | case 3: |
---|
576 | rmask = 0xff0000; |
---|
577 | gmask = 0x00ff00; |
---|
578 | bmask = 0x0000ff; |
---|
579 | amask = 0x000000; |
---|
580 | break; |
---|
581 | case 2: /* XXX: those are the 16 bits values */ |
---|
582 | rmask = 0x7c00; |
---|
583 | gmask = 0x03e0; |
---|
584 | bmask = 0x001f; |
---|
585 | amask = 0x0000; |
---|
586 | break; |
---|
587 | case 1: |
---|
588 | default: |
---|
589 | bpp = 8; |
---|
590 | rmask = gmask = bmask = amask = 0; |
---|
591 | break; |
---|
592 | } |
---|
593 | |
---|
594 | fclose(fp); |
---|
595 | |
---|
596 | /* Create the libcaca bitmap */ |
---|
597 | bitmap = caca_create_bitmap(bpp, w, h, depth * w, |
---|
598 | rmask, gmask, bmask, amask); |
---|
599 | if(!bitmap) |
---|
600 | { |
---|
601 | free(pixels); |
---|
602 | pixels = NULL; |
---|
603 | return; |
---|
604 | } |
---|
605 | |
---|
606 | if(bpp == 8) |
---|
607 | caca_set_bitmap_palette(bitmap, red, green, blue, alpha); |
---|
608 | #endif |
---|
609 | |
---|
610 | x = w / 2; |
---|
611 | y = h / 2; |
---|
612 | } |
---|
613 | |
---|
614 | static void draw_checkers(unsigned int x1, unsigned int y1, |
---|
615 | unsigned int x2, unsigned int y2) |
---|
616 | { |
---|
617 | unsigned int xn, yn; |
---|
618 | |
---|
619 | for(yn = y1; yn <= y2; yn++) |
---|
620 | for(xn = x1; xn <= x2; xn++) |
---|
621 | { |
---|
622 | if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1) |
---|
623 | caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_DARKGRAY); |
---|
624 | else |
---|
625 | caca_set_color(CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY); |
---|
626 | caca_putchar(xn, yn, ' '); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | #if !defined(HAVE_IMLIB2_H) |
---|
631 | static int freadint(FILE *fp) |
---|
632 | { |
---|
633 | unsigned char buffer[4]; |
---|
634 | fread(buffer, 4, 1, fp); |
---|
635 | return ((int)buffer[3] << 24) | ((int)buffer[2] << 16) |
---|
636 | | ((int)buffer[1] << 8) | ((int)buffer[0]); |
---|
637 | } |
---|
638 | |
---|
639 | static int freadshort(FILE *fp) |
---|
640 | { |
---|
641 | unsigned char buffer[2]; |
---|
642 | fread(buffer, 2, 1, fp); |
---|
643 | return ((int)buffer[1] << 8) | ((int)buffer[0]); |
---|
644 | } |
---|
645 | |
---|
646 | static int freadchar(FILE *fp) |
---|
647 | { |
---|
648 | unsigned char buffer; |
---|
649 | fread(&buffer, 1, 1, fp); |
---|
650 | return (int)buffer; |
---|
651 | } |
---|
652 | #endif |
---|
653 | |
---|