home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part1 / macptopbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.9 KB  |  125 lines

  1. /* macptopbm.c - read a MacPaint file and produce a portable bitmap
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  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 <stdio.h>
  14. #include <sys/types.h>
  15. #include "pbm.h"
  16. #include "macp.h"
  17.  
  18. main( argc, argv )
  19. int argc;
  20. char *argv[];
  21.     {
  22.     FILE *ifd;
  23.     unsigned char Pic[MAX_LINES][BYTES_WIDE];
  24.     bit **bits;
  25.     int scanLine, rows, cols, row, bcol, i;
  26.  
  27.     if ( argc > 2 )
  28.     {
  29.     fprintf( stderr, "usage: %s [macpfile]\n", argv[0] );
  30.     exit( 1 );
  31.     }
  32.  
  33.     if ( argc == 2 )
  34.     {
  35.     ifd = fopen( argv[1], "r" );
  36.     if ( ifd == NULL )
  37.         {
  38.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  39.         exit( 1 );
  40.         }
  41.     }
  42.     else
  43.     ifd = stdin;
  44.  
  45.     if ( ReadMacPaintFile( ifd, &scanLine, Pic ) < 0 )
  46.     {
  47.     fprintf( stderr, "%s: can't load.\n", argv[1] );
  48.     exit( 1 );
  49.     }
  50.  
  51.     if ( ifd != stdin )
  52.     fclose( ifd );
  53.  
  54.     cols = BYTES_WIDE * 8;
  55.     rows = scanLine;
  56.     bits = pbm_allocarray( cols, rows );
  57.  
  58.     for ( row = 0; row < rows; row++ )
  59.     for ( bcol = 0; bcol < BYTES_WIDE; bcol++ )
  60.         for ( i = 0; i < 8; i++ )
  61.         bits[row][bcol * 8 + i] = ( (Pic[row][bcol] >> (7 - i)) & 1);
  62.  
  63.     pbm_writepbm( stdout, bits, cols, rows );
  64.  
  65.     exit( 0 );
  66.     }
  67.  
  68. /*
  69. ** Some of the following routine is:
  70. **
  71. **                Copyright 1987 by Patrick J. Naughton
  72. **                         All Rights Reserved
  73. ** Permission to use, copy, modify, and distribute this software and its
  74. ** documentation for any purpose and without fee is hereby granted,
  75. ** provided that the above copyright notice appear in all copies and that
  76. ** both that copyright notice and this permission notice appear in
  77. ** supporting documentation.
  78. */
  79.  
  80. int
  81. ReadMacPaintFile( file, scanLineP, Pic )
  82. FILE *file;
  83. int *scanLineP;
  84. unsigned char Pic[MAX_LINES][BYTES_WIDE];
  85.     {
  86.     unsigned int i, j, k;
  87.     unsigned char ch;
  88.  
  89.     /* Skip over the header. */
  90.     for ( i = 0; i < HEADER_LENGTH; i++ )
  91.     getc( file );
  92.  
  93.     *scanLineP = 0;
  94.     k = 0;
  95.  
  96.     while ( *scanLineP < MAX_LINES )
  97.     {
  98.     ch = (unsigned char) getc( file );    /* Count byte */
  99.     i = (unsigned int) ch;
  100.     if ( ch < 0x80 )
  101.         {    /* Unpack next (I+1) chars as is */
  102.         for ( j = 0; j <= i; j++ )
  103.         if ( *scanLineP < MAX_LINES )
  104.             {
  105.             Pic[*scanLineP][k++] = (unsigned char) getc( file );
  106.             if ( ! (k %= BYTES_WIDE) )
  107.             *scanLineP += 1;
  108.             }
  109.         }
  110.     else
  111.         {    /* Repeat next char (2's comp I) times */
  112.         ch = getc( file );
  113.         for ( j = 0; j <= 256 - i; j++ )
  114.         if ( *scanLineP < MAX_LINES )
  115.             {
  116.             Pic[*scanLineP][k++] = (unsigned char) ch;
  117.             if ( ! (k %= BYTES_WIDE) )
  118.             *scanLineP += 1;
  119.             }
  120.         }
  121.     }
  122.  
  123.     return(0);
  124.     }
  125.