home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PBM / pbmtobbnbg.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  3KB  |  153 lines

  1. /* pbmtobg.c - read a portable bitmap and produce BitGraph graphics
  2. **
  3. ** Copyright 1989 by Mike Parker.
  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. /*
  14. ** Changed to take advantage of negative Packed Pixed Data values and
  15. ** supply ANSI-standard string terminator.  Paul Milazzo, 28 May 1990.
  16. */
  17.  
  18. #include "pbm.h"
  19.  
  20. static void write16 ARGS(( unsigned int    ));
  21.  
  22. static int nco;
  23.  
  24. int
  25. main(argc,argv)
  26. int argc;
  27. char **argv;
  28. {
  29.  int rows;
  30.  int cols;
  31.  int format;
  32.  bit *bitrow;
  33.  int row;
  34.  unsigned int sixteen;
  35.  int i;
  36.  unsigned int mask;
  37.  int op;
  38.  int x;
  39.  int y;
  40.  
  41.  
  42.  pbm_init( &argc, argv );
  43.  
  44.  op = 3;
  45.  switch (argc)
  46.   { case 1:
  47.        break;
  48.     case 2:
  49.        op = atoi(argv[1]);
  50.        break;
  51.     case 3:
  52.        x = atoi(argv[1]);
  53.        y = atoi(argv[2]);
  54.        printf("\33:%d;%dm",x,y);
  55.        break;
  56.     case 4:
  57.        op = atoi(argv[1]);
  58.        x = atoi(argv[2]);
  59.        y = atoi(argv[3]);
  60.        printf("\33:%d;%dm",x,y);
  61.        break;
  62.   }
  63.  nco = 0;
  64.  pbm_readpbminit(stdin,&cols,&rows,&format);
  65.  printf("\33P:%d;%d;%ds\n",op,cols,rows);
  66.  bitrow = pbm_allocrow(cols);
  67.  for (row=0;row<rows;row++)
  68.   { pbm_readpbmrow(stdin,bitrow,cols,format);
  69.     sixteen = 0;
  70.     mask = 0x8000;
  71.     for (i=0;i<cols;i++)
  72.      { if (bitrow[i]==PBM_BLACK) sixteen |= mask;
  73.        mask >>= 1;
  74.        if (mask == 0)
  75.     { mask = 0x8000;
  76.       write16(sixteen);
  77.       sixteen = 0;
  78.     }
  79.      }
  80.     if (mask != 0x8000)
  81.      { write16(sixteen);
  82.      }
  83.   }
  84.  puts("\033\\");
  85.  exit(0);
  86. }
  87.  
  88. #ifdef POSITIVE_VALUES_ONLY
  89. static void
  90. write16(sixteen)
  91. unsigned int sixteen;
  92. {
  93.  if (nco > 75)
  94.   { putchar('\n');
  95.     nco = 0;
  96.   }
  97.  if (sixteen & 0xfc00)
  98.   { putchar(0100+(sixteen>>10));
  99.     nco ++;
  100.   }
  101.  if (sixteen & 0xfff0)
  102.   { putchar(0100+((sixteen>>4)&0x3f));
  103.     nco ++;
  104.   }
  105.  putchar(060+(sixteen&0xf));
  106.  nco ++;
  107. }
  108. #else
  109. /*
  110.  *  This version of "write16" uses negative Packed Pixel Data values to
  111.  *  represent numbers in the range 0x7fff--0xffff; negative values will
  112.  *  require fewer characters as they approach the upper end of that range.
  113.  */
  114. static void
  115. write16 (word)
  116. unsigned int    word;
  117. {
  118.     int        high;
  119.     int        mid;
  120.     int        low;
  121.     int        signChar;
  122.  
  123.     if (nco > 75) {
  124.     putchar ('\n');
  125.     nco = 0;
  126.     }
  127.  
  128.     if (word > 0x7fff) {
  129.     word = (unsigned int) (0x10000L - (long) word);
  130.     signChar = ' ';
  131.     }
  132.     else
  133.     signChar = '0';
  134.  
  135.     high = (word >> 10) + '@';
  136.     mid    = ((word & 0x3f0) >> 4) + '@';
  137.     low    = (word & 0xf) + signChar;
  138.  
  139.     if (high != '@') {
  140.     printf ("%c%c%c", high, mid, low);
  141.     nco += 3;
  142.     }
  143.     else if (mid != '@') {
  144.     printf ("%c%c", mid, low);
  145.     nco += 2;
  146.     }
  147.     else {
  148.     putchar (low);
  149.     nco++;
  150.     }
  151. }
  152. #endif
  153.