509 {
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526 int x, y;
527
528 char buffer[256];
529
530 if(!dib || !handle) return FALSE;
531
532 FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
533
534 int bpp = FreeImage_GetBPP(dib);
535 int width = FreeImage_GetWidth(dib);
536 int height = FreeImage_GetHeight(dib);
537
538
539
540 int magic = 0;
542
543 switch(image_type) {
544 case FIT_BITMAP:
545 switch (bpp) {
546 case 1 :
547 magic = 1;
548 break;
549 case 8 :
550 magic = 2;
551 break;
552
553 case 24 :
554 magic = 3;
555 break;
556
557 default:
558 return FALSE;
559 }
560 break;
561
562 case FIT_UINT16:
563 magic = 2;
564 break;
565
566 case FIT_RGB16:
567 magic = 3;
568 break;
569
570 default:
571 return FALSE;
572 }
573
574
575 if (flags == PNM_SAVE_RAW)
576 magic += 3;
577
578
579
580 sprintf(buffer, "P%d\n%d %d\n", magic, width, height);
581 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
582
583 if (bpp != 1) {
585 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
586 }
587
588
590
591 if(image_type == FIT_BITMAP) {
592 switch(bpp) {
593 case 24 :
594 {
595 if (flags == PNM_SAVE_RAW) {
596 for (y = 0; y < height; y++) {
597
598 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
599
600 for (x = 0; x < width; x++) {
601 io->write_proc(&bits[FI_RGBA_RED], 1, 1, handle);
602 io->write_proc(&bits[FI_RGBA_GREEN], 1, 1, handle);
603 io->write_proc(&bits[FI_RGBA_BLUE], 1, 1, handle);
604
605 bits += 3;
606 }
607 }
608 } else {
609 int length = 0;
610
611 for (y = 0; y < height; y++) {
612
613 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
614
615 for (x = 0; x < width; x++) {
616 sprintf(buffer, "%3d %3d %3d ", bits[FI_RGBA_RED], bits[FI_RGBA_GREEN], bits[FI_RGBA_BLUE]);
617
618 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
619
620 length += 12;
621
622 if(length > 58) {
623
624 sprintf(buffer, "\n");
625 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
626 length = 0;
627 }
628
629 bits += 3;
630 }
631 }
632
633 }
634 }
635 break;
636
637 case 8:
638 {
639 if (flags == PNM_SAVE_RAW) {
640 for (y = 0; y < height; y++) {
641
642 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
643
644 for (x = 0; x < width; x++) {
645 io->write_proc(&bits[x], 1, 1, handle);
646 }
647 }
648 } else {
649 int length = 0;
650
651 for (y = 0; y < height; y++) {
652
653 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
654
655 for (x = 0; x < width; x++) {
656 sprintf(buffer, "%3d ", bits[x]);
657
658 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
659
660 length += 4;
661
662 if (length > 66) {
663
664 sprintf(buffer, "\n");
665 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
666 length = 0;
667 }
668 }
669 }
670 }
671 }
672 break;
673
674 case 1:
675 {
676 int color;
677
678 if (flags == PNM_SAVE_RAW) {
679 for(y = 0; y < height; y++) {
680
681 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
682
683 for(x = 0; x < (int)FreeImage_GetLine(dib); x++)
684 io->write_proc(&bits[x], 1, 1, handle);
685 }
686 } else {
687 int length = 0;
688
689 for (y = 0; y < height; y++) {
690
691 BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
692
693 for (x = 0; x < (int)FreeImage_GetLine(dib) * 8; x++) {
694 color = (bits[x>>3] & (0x80 >> (x & 0x07))) != 0;
695
696 sprintf(buffer, "%c ", color ? '1':'0');
697
698 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
699
700 length += 2;
701
702 if (length > 68) {
703
704 sprintf(buffer, "\n");
705 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
706 length = 0;
707 }
708 }
709 }
710 }
711 }
712
713 break;
714 }
715 }
716
717 else if(image_type == FIT_UINT16) {
718 if (flags == PNM_SAVE_RAW) {
719 for (y = 0; y < height; y++) {
720
721 WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
722
723 for (x = 0; x < width; x++) {
725 }
726 }
727 } else {
728 int length = 0;
729
730 for (y = 0; y < height; y++) {
731
732 WORD *bits = (WORD*)FreeImage_GetScanLine(dib, height - 1 - y);
733
734 for (x = 0; x < width; x++) {
735 sprintf(buffer, "%5d ", bits[x]);
736
737 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
738
739 length += 6;
740
741 if (length > 64) {
742
743 sprintf(buffer, "\n");
744 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
745 length = 0;
746 }
747 }
748 }
749 }
750 }
751
752 else if(image_type == FIT_RGB16) {
753 if (flags == PNM_SAVE_RAW) {
754 for (y = 0; y < height; y++) {
755
756 FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
757
758 for (x = 0; x < width; x++) {
762 }
763 }
764 } else {
765 int length = 0;
766
767 for (y = 0; y < height; y++) {
768
769 FIRGB16 *bits = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
770
771 for (x = 0; x < width; x++) {
772 sprintf(buffer, "%5d %5d %5d ", bits[x].red, bits[x].green, bits[x].blue);
773
774 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
775
776 length += 18;
777
778 if(length > 52) {
779
780 sprintf(buffer, "\n");
781 io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
782 length = 0;
783 }
784 }
785 }
786
787 }
788 }
789
790 return TRUE;
791}
static void WriteWord(FreeImageIO *io, fi_handle handle, const WORD value)