home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / pbm / Part1 / macptopbm.c < prev    next >
C/C++ Source or Header  |  1991-08-07  |  3KB  |  130 lines

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