home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / unix / ps2eps11.sha / pbmtoepsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  2.5 KB  |  106 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. /* modified by george jefferson george@mech.seas.upenn.edu
  19. ** when compiled with -Djustbb this program quits after reporting
  20. ** the bounding box information 
  21. ** i have added exactly three lines, all marked /*jbb*/
  22. */
  23.  
  24. #include "pbm.h"
  25.  
  26. #if !defined(MAXINT)
  27. #define MAXINT (0x7fffffff)
  28. #endif
  29.  
  30. main( argc, argv )
  31.     int argc;
  32.     char *argv[];
  33. {
  34.     FILE *ifd;
  35.     register bit **bits;
  36.     int rows, cols, row, col, tot, count;
  37.     int top = MAXINT, bottom = -MAXINT, left = MAXINT, right = -MAXINT;
  38.     
  39.     pbm_init( &argc, argv );
  40.     
  41.     if ( argc > 2 )
  42.         pm_usage( "[pbmfile]" );
  43.     
  44.     if ( argc == 2 )
  45.         ifd = pm_openr( argv[1] );
  46.     else
  47.         ifd = stdin;
  48.     
  49.     bits = pbm_readpbm( ifd, &cols, &rows );
  50.     
  51.     pm_close( ifd );
  52.     
  53.     for (row = 0; row < rows; row++) {
  54.         for (col = 0; col < cols; col++) {
  55.             if (bits[row][col] == PBM_BLACK) {
  56.                 if (row < top) {
  57.                     top = row;
  58.                 }
  59.                 if (row > bottom) {
  60.                     bottom = row;
  61.                 }
  62.                 if (col < left) {
  63.                     left = col;
  64.                 }
  65.                 if (col > right) {
  66.                     right = col;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
  73.      printf("%%%%BoundingBox: %d %d %d %d\n", left, rows - bottom, right, rows - top);
  74. #ifndef justbb
  75.     printf("%%%%BeginPreview: %d %d 1 %d\n", right - left + 1, bottom - top + 1, bottom - top + 1);
  76.  
  77.     for (row = top; row <= bottom; row++) {
  78.         printf("%% ");
  79.         count = 0;
  80.         for (col = left; col <= right; col += 4) {
  81.             tot = 0;
  82.             if (bits[row][col] == PBM_BLACK) {
  83.                 tot += 8;
  84.             }
  85.             if (bits[row][col+1] == PBM_BLACK) {
  86.                 tot += 4;
  87.             }
  88.             if (bits[row][col+2] == PBM_BLACK) {
  89.                 tot += 2;
  90.             }
  91.             if (bits[row][col+3] == PBM_BLACK) {
  92.                 tot++;
  93.             }
  94.             printf("%x", tot);
  95.             count++;
  96.         }
  97.         printf((count % 2) == 0 ? "\n" : "0\n");
  98.     }
  99.     printf("%%%%EndImage\n");
  100.     printf("%%%%EndPreview\n");
  101. #else  justbb
  102.     printf("%%%%End modified pbmtoepsi header\n"); /*justbb*/
  103. #endif justbb
  104.     exit( 0 );
  105. }
  106.