home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / ps2eps / pbmtoepsi.c next >
C/C++ Source or Header  |  1991-04-10  |  2KB  |  98 lines

  1. /* pbmtoepsi.c
  2. **
  3. **    by Doug Crabill, based heavily on pbmtoascii
  4. **
  5. **    Converts a pbm file to an encapsulated PostScript style bitmap.
  6. **    Note that it does NOT covert the pbm file to PostScript, only to
  7. **    a bitmap to be added to a piece of PostScript generated elsewhere.
  8. **
  9. ** Copyright (C) 1988 by Jef Poskanzer.
  10. **
  11. ** Permission to use, copy, modify, and distribute this software and its
  12. ** documentation for any purpose and without fee is hereby granted, provided
  13. ** that the above copyright notice appear in all copies and that both that
  14. ** copyright notice and this permission notice appear in supporting
  15. ** documentation.  This software is provided "as is" without express or
  16. ** implied warranty.
  17. */
  18.  
  19. #include "pbm.h"
  20.  
  21. #if !defined(MAXINT)
  22. #define MAXINT (0x7fffffff)
  23. #endif
  24.  
  25. main( argc, argv )
  26.     int argc;
  27.     char *argv[];
  28. {
  29.     FILE *ifd;
  30.     register bit **bits;
  31.     int rows, cols, row, col, tot, count;
  32.     int top = MAXINT, bottom = -MAXINT, left = MAXINT, right = -MAXINT;
  33.     
  34.     pbm_init( &argc, argv );
  35.     
  36.     if ( argc > 2 )
  37.         pm_usage( "[pbmfile]" );
  38.     
  39.     if ( argc == 2 )
  40.         ifd = pm_openr( argv[1] );
  41.     else
  42.         ifd = stdin;
  43.     
  44.     bits = pbm_readpbm( ifd, &cols, &rows );
  45.     
  46.     pm_close( ifd );
  47.     
  48.     for (row = 0; row < rows; row++) {
  49.         for (col = 0; col < cols; col++) {
  50.             if (bits[row][col] == PBM_BLACK) {
  51.                 if (row < top) {
  52.                     top = row;
  53.                 }
  54.                 if (row > bottom) {
  55.                     bottom = row;
  56.                 }
  57.                 if (col < left) {
  58.                     left = col;
  59.                 }
  60.                 if (col > right) {
  61.                     right = col;
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.     printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
  68.      printf("%%%%BoundingBox: %d %d %d %d\n", left, rows - bottom, right, rows - top);
  69.     printf("%%%%BeginPreview: %d %d 1 %d\n", right - left + 1, bottom - top + 1, bottom - top + 1);
  70.  
  71.     for (row = top; row <= bottom; row++) {
  72.         printf("%% ");
  73.         count = 0;
  74.         for (col = left; col <= right; col += 4) {
  75.             tot = 0;
  76.             if (bits[row][col] == PBM_BLACK) {
  77.                 tot += 8;
  78.             }
  79.             if (bits[row][col+1] == PBM_BLACK) {
  80.                 tot += 4;
  81.             }
  82.             if (bits[row][col+2] == PBM_BLACK) {
  83.                 tot += 2;
  84.             }
  85.             if (bits[row][col+3] == PBM_BLACK) {
  86.                 tot++;
  87.             }
  88.             printf("%x", tot);
  89.             count++;
  90.         }
  91.         printf((count % 2) == 0 ? "\n" : "0\n");
  92.     }
  93.     printf("%%%%EndImage\n");
  94.     printf("%%%%EndPreview\n");
  95.  
  96.     exit( 0 );
  97. }
  98.