Changeset 741 for libcaca/trunk/src/cacaview.c
- Timestamp:
- Apr 11, 2006, 12:36:31 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/src/cacaview.c
r734 r741 18 18 #include <stdlib.h> 19 19 20 #if defined(HAVE_IMLIB2_H)21 # include <Imlib2.h>22 #else23 # include <stdio.h>24 #endif25 26 20 #if defined(HAVE_SLEEP) 27 21 # include <windows.h> … … 30 24 #include "cucul.h" 31 25 #include "caca.h" 26 #include "common-image.h" 32 27 33 28 /* Local macros */ … … 54 49 static void set_zoom(int); 55 50 static void set_gamma(int); 56 static void load_image(char const *);57 static void unload_image(void);58 51 static void draw_checkers(int, int, int, int); 59 #if !defined(HAVE_IMLIB2_H)60 static int freadint(FILE *);61 static int freadshort(FILE *);62 static int freadchar(FILE *);63 #endif64 52 65 53 /* Local variables */ 66 #if defined(HAVE_IMLIB2_H) 67 Imlib_Image image = NULL; 68 #endif 69 char *pixels = NULL; 70 struct cucul_dither *dither = NULL; 71 unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask; 72 #if !defined(HAVE_IMLIB2_H) 73 unsigned int red[256], green[256], blue[256], alpha[256]; 74 #endif 54 struct image *im = NULL; 75 55 76 56 float zoomtab[ZOOM_MAX + 1]; … … 189 169 set_zoom(zoom); 190 170 break; 171 #if 0 /* FIXME */ 191 172 case 'b': 192 173 i = 1 + cucul_get_feature(qq, CUCUL_BACKGROUND); … … 231 212 update = 1; 232 213 break; 214 #endif 233 215 case '+': 234 216 update = 1; … … 326 308 wh = cucul_get_height(qq); 327 309 328 unload_image(); 329 load_image(list[current]); 310 if(im) 311 unload_image(im); 312 im = load_image(list[current]); 330 313 reload = 0; 331 314 … … 346 329 cucul_printf(qq, ww / 2 - 5, wh / 2, " No image. "); 347 330 } 348 else if(! pixels)331 else if(!im) 349 332 { 350 333 #if defined(HAVE_IMLIB2_H) … … 387 370 ww * (xdelta + (1.0 - xdelta) * xfactor), 388 371 y + height * (ydelta + (1.0 - ydelta) * yfactor), 389 dither,pixels);372 im->dither, im->pixels); 390 373 } 391 374 … … 394 377 print_status(); 395 378 379 #if 0 /* FIXME */ 396 380 cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); 397 381 switch(status) … … 410 394 break; 411 395 } 396 #endif 412 397 } 413 398 … … 422 407 423 408 /* Clean up */ 424 unload_image(); 409 if(im) 410 unload_image(im); 425 411 caca_detach(kk); 426 412 cucul_free(qq); … … 479 465 int height; 480 466 467 if(!im) 468 return; 469 481 470 zoom = new_zoom; 482 471 … … 488 477 489 478 xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom]; 490 yfactor = xfactor * ww / height * h /w479 yfactor = xfactor * ww / height * im->h / im->w 491 480 * cucul_get_height(qq) / cucul_get_width(qq) 492 481 * caca_get_window_width(kk) / caca_get_window_height(kk); … … 502 491 static void set_gamma(int new_gamma) 503 492 { 493 if(!im) 494 return; 495 504 496 g = new_gamma; 505 497 … … 507 499 if(g < -GAMMA_MAX) g = -GAMMA_MAX; 508 500 509 cucul_set_dither_gamma(dither, (g < 0) ? 1.0 / gammatab[-g] : gammatab[g]); 510 } 511 512 static void unload_image(void) 513 { 514 #if defined(HAVE_IMLIB2_H) 515 if(image) 516 imlib_free_image(); 517 image = NULL; 518 pixels = NULL; 519 #else 520 if(pixels) 521 free(pixels); 522 pixels = NULL; 523 #endif 524 if(dither) 525 cucul_free_dither(dither); 526 dither = NULL; 527 } 528 529 static void load_image(char const *name) 530 { 531 #if defined(HAVE_IMLIB2_H) 532 /* Load the new image */ 533 image = imlib_load_image(name); 534 535 if(!image) 536 return; 537 538 imlib_context_set_image(image); 539 pixels = (char *)imlib_image_get_data_for_reading_only(); 540 w = imlib_image_get_width(); 541 h = imlib_image_get_height(); 542 rmask = 0x00ff0000; 543 gmask = 0x0000ff00; 544 bmask = 0x000000ff; 545 amask = 0xff000000; 546 bpp = 32; 547 depth = 4; 548 549 /* Create the libcucul dither */ 550 dither = cucul_create_dither(bpp, w, h, depth * w, 551 rmask, gmask, bmask, amask); 552 if(!dither) 553 { 554 imlib_free_image(); 555 image = NULL; 556 } 557 558 #else 559 /* Try to load a BMP file */ 560 FILE *fp; 561 unsigned int i, colors, offset, tmp, planes; 562 563 fp = fopen(name, "rb"); 564 if(!fp) 565 return; 566 567 if(freadshort(fp) != 0x4d42) 568 { 569 fclose(fp); 570 return; 571 } 572 573 freadint(fp); /* size */ 574 freadshort(fp); /* reserved 1 */ 575 freadshort(fp); /* reserved 2 */ 576 577 offset = freadint(fp); 578 579 tmp = freadint(fp); /* header size */ 580 if(tmp == 40) 581 { 582 w = freadint(fp); 583 h = freadint(fp); 584 planes = freadshort(fp); 585 bpp = freadshort(fp); 586 587 tmp = freadint(fp); /* compression */ 588 if(tmp != 0) 589 { 590 fclose(fp); 591 return; 592 } 593 594 freadint(fp); /* sizeimage */ 595 freadint(fp); /* xpelspermeter */ 596 freadint(fp); /* ypelspermeter */ 597 freadint(fp); /* biclrused */ 598 freadint(fp); /* biclrimportantn */ 599 600 colors = (offset - 54) / 4; 601 for(i = 0; i < colors && i < 256; i++) 602 { 603 blue[i] = freadchar(fp) * 16; 604 green[i] = freadchar(fp) * 16; 605 red[i] = freadchar(fp) * 16; 606 alpha[i] = 0; 607 freadchar(fp); 608 } 609 } 610 else if(tmp == 12) 611 { 612 w = freadint(fp); 613 h = freadint(fp); 614 planes = freadshort(fp); 615 bpp = freadshort(fp); 616 617 colors = (offset - 26) / 3; 618 for(i = 0; i < colors && i < 256; i++) 619 { 620 blue[i] = freadchar(fp); 621 green[i] = freadchar(fp); 622 red[i] = freadchar(fp); 623 alpha[i] = 0; 624 } 625 } 626 else 627 { 628 fclose(fp); 629 return; 630 } 631 632 /* Fill the rest of the palette */ 633 for(i = colors; i < 256; i++) 634 blue[i] = green[i] = red[i] = alpha[i] = 0; 635 636 depth = (bpp + 7) / 8; 637 638 /* Sanity check */ 639 if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/) 640 { 641 fclose(fp); 642 return; 643 } 644 645 /* Allocate the pixel buffer */ 646 pixels = malloc(w * h * depth); 647 if(!pixels) 648 { 649 fclose(fp); 650 return; 651 } 652 653 memset(pixels, 0, w * h * depth); 654 655 /* Read the dither data */ 656 for(i = h; i--; ) 657 { 658 unsigned int j, k, bits = 0; 659 660 switch(bpp) 661 { 662 case 1: 663 for(j = 0; j < w; j++) 664 { 665 k = j % 32; 666 if(k == 0) 667 bits = freadint(fp); 668 pixels[w * i * depth + j] = 669 (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1; 670 } 671 break; 672 case 4: 673 for(j = 0; j < w; j++) 674 { 675 k = j % 8; 676 if(k == 0) 677 bits = freadint(fp); 678 pixels[w * i * depth + j] = 679 (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf; 680 } 681 break; 682 default: 683 /* Works for 8bpp, but also for 16, 24 etc. */ 684 fread(pixels + w * i * depth, w * depth, 1, fp); 685 /* Pad reads to 4 bytes */ 686 tmp = (w * depth) % 4; 687 tmp = (4 - tmp) % 4; 688 while(tmp--) 689 freadchar(fp); 690 break; 691 } 692 } 693 694 switch(depth) 695 { 696 case 3: 697 rmask = 0xff0000; 698 gmask = 0x00ff00; 699 bmask = 0x0000ff; 700 amask = 0x000000; 701 break; 702 case 2: /* XXX: those are the 16 bits values */ 703 rmask = 0x7c00; 704 gmask = 0x03e0; 705 bmask = 0x001f; 706 amask = 0x0000; 707 break; 708 case 1: 709 default: 710 bpp = 8; 711 rmask = gmask = bmask = amask = 0; 712 break; 713 } 714 715 fclose(fp); 716 717 /* Create the libcucul dither */ 718 dither = cucul_create_dither(bpp, w, h, depth * w, 719 rmask, gmask, bmask, amask); 720 if(!dither) 721 { 722 free(pixels); 723 pixels = NULL; 724 return; 725 } 726 727 if(bpp == 8) 728 cucul_set_dither_palette(dither, red, green, blue, alpha); 729 #endif 501 cucul_set_dither_gamma(im->dither, 502 (g < 0) ? 1.0 / gammatab[-g] : gammatab[g]); 730 503 } 731 504 … … 748 521 } 749 522 750 #if !defined(HAVE_IMLIB2_H)751 static int freadint(FILE *fp)752 {753 unsigned char buffer[4];754 fread(buffer, 4, 1, fp);755 return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)756 | ((int)buffer[1] << 8) | ((int)buffer[0]);757 }758 759 static int freadshort(FILE *fp)760 {761 unsigned char buffer[2];762 fread(buffer, 2, 1, fp);763 return ((int)buffer[1] << 8) | ((int)buffer[0]);764 }765 766 static int freadchar(FILE *fp)767 {768 unsigned char buffer;769 fread(&buffer, 1, 1, fp);770 return (int)buffer;771 }772 #endif773
Note: See TracChangeset
for help on using the changeset viewer.