home *** CD-ROM | disk | FTP | other *** search
/ Hand Held Organizer Toolkit / walnutcreekcdrom-handheldorganizertoolkit-march1998.iso / PalmPilot / development / ppmtotbmp_tar.gz / ppmtotbmp_tar / ppmtoTbmp-1.1 / Tbmptopnm.c < prev   
C/C++ Source or Header  |  1997-06-25  |  2KB  |  105 lines

  1. /* Tbmptopnm.c - read a pilot Tbmp file and write a Portable aNyMap
  2.  *
  3.  * Program by Ian Goldberg <iang@cs.berkeley.edu>
  4.  */
  5.  
  6. #include "pnm.h"
  7.  
  8. int
  9. main( int argc, char **argv )
  10. {
  11.     FILE* ifp;
  12.     unsigned short rows, cols, color, cpr, flags;
  13.     unsigned long pad;
  14.     int i,j;
  15.     unsigned char *inbyte;
  16.     unsigned char inbit;
  17.     int twobit = 0;
  18.     int format;
  19.     xel *xelrow;
  20.     unsigned char *Tbmprow;
  21.  
  22.     /* Parse default params */
  23.     pnm_init( &argc, argv );
  24.  
  25.     if (argc > 1 && !strcmp(argv[1], "-2bit")) {
  26.     twobit = 1;
  27.     --argc; ++argv;
  28.     }
  29.     if ( argc > 2 || (argc == 2 && argv[1][0] == '-')) {
  30.     pm_usage( "[-2bit] [Tbmpfile]" );
  31.     }
  32.  
  33.     if ( argc == 2 )
  34.     ifp = pm_openr( argv[1] );
  35.     else
  36.     ifp = stdin;
  37.  
  38.     /* Read the bitmap header */
  39.     pm_readbigshort( ifp, &cols );
  40.     pm_readbigshort( ifp, &rows );
  41.     pm_readbigshort( ifp, &cpr );
  42.     pm_readbigshort( ifp, &flags );
  43.     pm_readbiglong( ifp, &pad );
  44.     pm_readbiglong( ifp, &pad );
  45.  
  46.     /* Sanity check */
  47.     if (twobit && (cols & 1)) {
  48.     pm_error("Odd number of columns in 2-bit mode");
  49.     }
  50.     if (twobit) cols >>= 1;
  51.     if ((twobit && cpr != ((cols+7)/8)*2) ||
  52.     (!twobit && cpr != ((cols+15)/16)*2)) {
  53.     pm_error("Wrong number of chars per row");
  54.     }
  55.     if (flags) {
  56.     pm_error("Unknown flags field in bitmap");
  57.     }
  58.  
  59.     /* Write the pnm header */
  60.     format = twobit ? PGM_TYPE : PBM_TYPE;
  61.     pnm_writepnminit(stdout, cols, rows, 255, format, 0);
  62.     xelrow = pnm_allocrow(cols);
  63.  
  64.     /* Read the picture data, one row at a time */
  65.     Tbmprow = (unsigned char *)malloc(cpr);
  66.     if (!Tbmprow) {
  67.     pm_error("Unable to allocate %d bytes", cpr);
  68.     }
  69.     for (i=0;i<rows;++i) {
  70.     if (fread( Tbmprow, 1, cpr, ifp ) != cpr) {
  71.         pm_error("Error reading Tbmp file");
  72.     }
  73.     inbit = twobit ? 6 : 7;
  74.     inbyte = Tbmprow;
  75.     for (j=0; j<cols; ++j) {
  76.         if (twobit) {
  77.         color = ((*inbyte) & (3 << inbit)) >> inbit;
  78.         switch (color) {
  79.             case 0: PNM_ASSIGN1(xelrow[j], 255); break;
  80.             case 1: PNM_ASSIGN1(xelrow[j], 170); break;
  81.             case 2: PNM_ASSIGN1(xelrow[j], 85); break;
  82.             default: PNM_ASSIGN1(xelrow[j], 0); break;
  83.         }
  84.         } else {
  85.         if ((*inbyte) & (1 << inbit)) {
  86.             PNM_ASSIGN1(xelrow[j], 0);
  87.         } else {
  88.             PNM_ASSIGN1(xelrow[j], 255);
  89.         }
  90.         }
  91.         if (!inbit) {
  92.         ++inbyte;
  93.         inbit = twobit ? 6 : 7;
  94.         } else {
  95.         inbit -= (twobit ? 2 : 1);
  96.         }
  97.     }
  98.     pnm_writepnmrow(stdout, xelrow, cols, 255, format, 0);
  99.     }
  100.  
  101.     pm_close( ifp );
  102.  
  103.     exit( 0 );
  104. }
  105.