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