home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmtomacp.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  6KB  |  300 lines

  1. /* pbmtomacp.c - read a portable bitmap and produce a MacPaint bitmap file
  2. **
  3. ** Copyright (C) 1988 by Douwe vand der Schaaf.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "macp.h"
  15.  
  16. #define TRUE        1
  17. #define FALSE        0
  18. #define EQUAL        1
  19. #define UNEQUAL        0
  20.  
  21. static void fillbits ARGS(( bit **bits, bit **bitsr, int top, int left, int bottom, int right ));
  22. static void writemacp ARGS(( bit **bits ));
  23. static int packit ARGS(( bit *pb, bit *bits ));
  24. static void filltemp ARGS(( bit *dest, bit *src ));
  25. static void sendbytes ARGS(( bit *pb, register int npb ));
  26. static void header ARGS(( void ));
  27.  
  28. static FILE *fdout;
  29.  
  30. int
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. { FILE *ifp;
  35.   register bit **bits, **bitsr;
  36.   int argn, rows, cols;
  37.   int left,bottom,right,top;
  38.   int lflg, rflg, tflg, bflg;
  39.   char name[100];
  40.   char *usage = "[-l left] [-r right] [-b bottom] [-t top] [pbmfile]";
  41.  
  42.  
  43.   pbm_init( &argc, argv );
  44.  
  45.   argn = 1;
  46.   fdout = stdout;
  47.   lflg = rflg = tflg = bflg = 0;
  48.  
  49.   while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  50.   { switch ( argv[argn][1] )
  51.     { case 'l':
  52.       lflg++;
  53.       argn++;
  54.       left = atoi( argv[argn] );
  55.       break;
  56.  
  57.       case 'r':
  58.       rflg++;
  59.       argn++;
  60.       right = atoi( argv[argn] );
  61.       break;
  62.  
  63.       case 't':
  64.       tflg++;
  65.       argn++;
  66.       top = atoi( argv[argn] );
  67.       break;
  68.  
  69.       case 'b':
  70.       bflg++;
  71.       argn++;
  72.       bottom = atoi( argv[argn] );
  73.       break;
  74.  
  75.       case '?':
  76.       default:
  77.       pm_usage( usage );
  78.     }
  79.     ++argn;
  80.   }
  81.  
  82.   if ( argn == argc )
  83.   { ifp = stdin;
  84.     strcpy( name, "noname" );
  85.   }
  86.   else
  87.   { ifp = pm_openr( argv[argn] );
  88.     strcpy( name, argv[argn] );
  89.     ++argn;
  90.   }
  91.  
  92.   if ( argn != argc )
  93.     pm_usage( usage );
  94.  
  95.   bitsr = pbm_readpbm( ifp, &cols, &rows );
  96.  
  97.   pm_close( ifp );
  98.  
  99.   bits = pbm_allocarray( MAX_COLS, MAX_LINES );
  100.  
  101.   if( !lflg )
  102.     left = 0;
  103.  
  104.   if( rflg )
  105.   { if( right - left >= MAX_COLS )
  106.       right = left + MAX_COLS - 1;
  107.   }
  108.   else
  109.     right = ( left + MAX_COLS > cols ) ? ( cols - 1 ) : ( left + MAX_COLS - 1 );
  110.  
  111.   if( !tflg )
  112.     top = 0;
  113.  
  114.   if( bflg )
  115.   { if( bottom - top >= MAX_LINES )
  116.       bottom = top + MAX_LINES - 1;
  117.   }
  118.   else
  119.     bottom = ( top + MAX_LINES > rows ) ?
  120.            ( rows - 1 ) : ( top + MAX_LINES - 1 );
  121.   
  122.     if( right <= left || left < 0 || right - left + 1 > MAX_COLS )
  123.       pm_error("error in right (= %d) and/or left (=%d)",right,left );
  124.     if( bottom <= top || top < 0 || bottom - top + 1 > MAX_LINES )
  125.       pm_error("error in bottom (= %d) and/or top (=%d)",bottom,top );
  126.  
  127.   fillbits( bits, bitsr, top, left, bottom, right );
  128.  
  129.   writemacp( bits );
  130.  
  131.   exit( 0 );
  132.  
  133. }
  134.  
  135. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  136.  
  137. /* centreer het over te zenden plaatje in het MacPaint document
  138.  *
  139.  * Het plaatje wordt vanaf al of niet opgegeven (left, bottom)
  140.  * in een pbm bitmap van de juist macpaint afmetingen gezet,
  141.  * en eventueel afgekapt.
  142.  */
  143. static void
  144. fillbits( bits, bitsr, top, left, bottom, right )
  145. bit **bits, **bitsr;
  146. int top, left, bottom, right;
  147. { register bit *bi, *bir;
  148.   register int i, j;
  149.   register int bottomr, leftr, topr, rightr;
  150.   int width, height;
  151.  
  152.   width = right - left + 1;
  153.   leftr = (MAX_COLS - width) / 2;
  154.   rightr = leftr + width - 1;
  155.  
  156.   height = bottom - top + 1;
  157.   topr = ( MAX_LINES - height ) / 2;
  158.   bottomr = topr + height - 1;
  159.  
  160.   for( i = 0; i < topr; i++ )
  161.   { bi = bits[i];
  162.     for( j = 0; j < MAX_COLS; j++ )
  163.       *bi++ = 0;
  164.   }
  165.  
  166.   for( i = topr; i <= bottomr; i++ )
  167.   { bi = bits[i];
  168.     { for( j = 0; j < leftr; j++ )
  169.     *bi++ = 0;
  170.       bir = bitsr[ i - topr + top ];
  171.       for( j = leftr; j <= rightr; j++ )
  172.     *bi++ = bir[j - leftr + left];
  173.       for( j = rightr + 1; j < MAX_COLS; j++ )
  174.     *bi++ = 0;
  175.   } }
  176.  
  177.   for( i = bottomr + 1; i < MAX_LINES; i++ )
  178.   { bi = bits[i];
  179.     for( j = 0; j < MAX_COLS; j++ )
  180.       *bi++ = 0;
  181.   }
  182. } /* fillbits */
  183.       
  184. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  185.  
  186. static void
  187. writemacp( bits )
  188. bit **bits;
  189. { register int i;
  190.   bit pb[MAX_COLS * 2];
  191.   int npb;
  192.  
  193.   header();
  194.   for( i=0; i < MAX_LINES; i++ )
  195.   { npb = packit( pb, bits[i] );
  196.     sendbytes( pb, npb );
  197.   }
  198. } /* writemacp */
  199.  
  200. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  201.  
  202. /* pack regel van MacPaint doc in Apple's format
  203.  * return value = # of bytes in pb 
  204.  */
  205. static int
  206. packit( pb, bits )
  207. bit *pb, *bits;
  208. { register int charcount, npb, newcount, flg;
  209.   bit temp[72];
  210.   bit *count, *srcb, *destb, save;
  211.  
  212.   srcb = bits; destb = temp;
  213.   filltemp( destb, srcb );
  214.   srcb = temp;
  215.   destb = pb;
  216.   npb = 0;
  217.   charcount = BYTES_WIDE;
  218.   flg = EQUAL;
  219.   while( charcount )
  220.   { save = *srcb++;
  221.     charcount--;
  222.     newcount = 1;
  223.     while( (*srcb == save) && charcount )
  224.     { srcb++;
  225.       newcount++;
  226.       charcount--;
  227.     }
  228.     if( newcount > 2 )
  229.     { count = destb++;
  230.       *count = 257 - newcount;
  231.       *destb++ = save;
  232.       npb += 2;
  233.       flg = EQUAL;
  234.     }
  235.     else
  236.     { if( flg == EQUAL )
  237.       { count = destb++;
  238.     *count = newcount - 1;
  239.     npb++;
  240.       }
  241.       else
  242.     *count += newcount;
  243.       while( newcount-- )
  244.       { *destb++ = save;
  245.         npb++;
  246.       }
  247.       flg = UNEQUAL;
  248.   } }
  249.   return npb;
  250. } /* packit */
  251.  
  252. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  253.  
  254. static void
  255. filltemp( dest, src )
  256. bit *dest, *src;
  257. { register unsigned char ch, zero, acht;
  258.   register int i, j;
  259.  
  260.   zero = '\0';
  261.   acht = 8;
  262.   i = BYTES_WIDE;
  263.   while( i-- )
  264.   { ch = zero; 
  265.     j = acht;
  266.     while( j-- )
  267.     { ch <<= 1;
  268.       if( *src++ )
  269.     ch++;
  270.     }
  271.     *dest++ = ch;
  272.   }
  273. } /* filltemp */
  274.  
  275. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  276.  
  277. static void
  278. sendbytes( pb, npb )
  279. bit *pb;
  280. register int npb;
  281. { register bit *b;
  282.  
  283.   b = pb;
  284.   while( npb-- )
  285.     (void) putc( *b++, fdout );
  286. } /* sendbytes */
  287.  
  288. /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
  289.  
  290. static void
  291. header()
  292. { register int i;
  293.   register char ch;
  294.  
  295.   /* header contains nothing ... */
  296.   ch = '\0';
  297.   for(i = 0; i < HEADER_LENGTH; i++ )
  298.     (void) putc( ch, fdout );
  299. } /* header */
  300.