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 / pbmto4425.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  3KB  |  182 lines

  1. #include "pbm.h"
  2.  
  3. /*extern char *sys_errlist[];
  4. char *malloc();*/
  5.  
  6. /* prototypes */
  7. static void init_map ARGS((void));
  8. static void fill_map ARGS((FILE *pbmfp));
  9. static void set_vmap ARGS((int x, int y));
  10. static void print_map ARGS((void));
  11. #define max(a,b)    ((a) > (b) ? (a) : (b))
  12.  
  13. static char bit_table[2][3] = {
  14. {1, 4, 0x10},
  15. {2, 8, 0x40}
  16. };
  17.  
  18. static int vmap_width;
  19. static int vmap_height;
  20.  
  21. static int xres;
  22. static int yres;
  23.  
  24. static char *vmap;
  25.  
  26.  
  27. int
  28. main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.   int argn;
  33.   char *pbmfile;
  34.   FILE *pbmfp;
  35.   int oerrno;
  36.   char *usage="[pbmfile]";
  37.  
  38.   pbm_init( &argc, argv );
  39.   for(argn = 1;
  40.       argn < argc && argv[argn][0] == '-' && strlen(argv[argn]) > 1;
  41.       ++argn)
  42.   {
  43.     pm_usage(usage);
  44.     exit(1);
  45.   }
  46.  
  47.   if(argn >= argc)
  48.   {
  49.     pbmfile = "-";
  50.   }
  51.   else if(argc - argn != 1)
  52.   {
  53.     pm_usage(usage);
  54.     exit(1);
  55.   }
  56.   else
  57.   {
  58.     pbmfile = argv[argn];
  59.   }
  60.  
  61.   if(strcmp(pbmfile, "-") == 0)
  62.   {
  63.     pbmfp = stdin;
  64.   }
  65.   else
  66.   {
  67.     pbmfp = pm_openr( argv[argn] );
  68.   }
  69.  
  70.   vmap_width = 132;
  71.   vmap_height = 23;
  72.  
  73.   xres = vmap_width * 2;
  74.   yres = vmap_height * 3;
  75.  
  76.   vmap = malloc(vmap_width * vmap_height * sizeof(char));
  77.   if(vmap == NULL)
  78.     {
  79.       pm_error( "Cannot allocate memory" );
  80.     }
  81.  
  82.   init_map();
  83.   fill_map(pbmfp);
  84.   print_map();
  85. }
  86.  
  87. static void
  88. init_map()
  89. {
  90.   int x, y;
  91.  
  92.  
  93.   for(x = 0; x < vmap_width; ++x)
  94.   {
  95.     for(y = 0; y < vmap_height; ++y)
  96.     {
  97.       vmap[y*(vmap_width) + x] = 0x20;
  98.     }
  99.   }
  100. }
  101.  
  102.  
  103. static void
  104. fill_map(pbmfp)
  105. FILE *pbmfp;
  106. {
  107.   bit **pbm_image;
  108.   int cols;
  109.   int rows;
  110.   int x;
  111.   int y;
  112.   int oerrno;
  113.  
  114.   pbm_image = pbm_readpbm(pbmfp, &cols, &rows);
  115.   for(y = 0; y < rows && y < yres; ++y)
  116.   {
  117.     for(x = 0; x < cols && x < xres; ++x)
  118.     {
  119.       if(pbm_image[y][x] == PBM_WHITE)
  120.       {
  121.     set_vmap(x, y);
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127.  
  128. static void
  129. set_vmap(x, y)
  130.   int x, y;
  131. {
  132.   int ix, iy;
  133.  
  134.   ix = x/2;
  135.   iy = y/3;
  136.  
  137.   vmap[iy*(vmap_width) + ix] |= bit_table[x % 2][y % 3];
  138. }
  139.  
  140.  
  141. static void
  142. print_map()
  143. {
  144.   int x, y;
  145.   int last_byte;
  146.   char *iobuf;
  147.  
  148. #ifdef BUFFERED
  149.   iobuf = (char *)malloc(BUFSIZ);
  150.   if(iobuf == NULL)
  151.   {
  152.     pm_message( "Can't allocate space for I/O buffer.  Using unbuffered I/O...\n" );
  153.     setbuf(stdout, NULL);
  154.   }
  155.   else
  156.   {
  157.     setbuf(stdout, iobuf);
  158.   }
  159. #endif
  160.  
  161.   fputs("\033[H\033[J", stdout);    /* clear screen */
  162.   fputs("\033[?3h", stdout);    /* 132 column mode */
  163.   fputs("\033)}\016", stdout);    /* mosaic mode */
  164.  
  165.   for(y = 0; y < vmap_height; ++y)
  166.   {
  167.     for(last_byte = vmap_width - 1;
  168.     last_byte >= 0
  169.     && vmap[y * vmap_width + last_byte] == 0x20;
  170.     --last_byte)
  171.       ;
  172.  
  173.     for(x = 0; x <= last_byte; ++x)
  174.     {
  175.       fputc(vmap[y*(vmap_width) + x], stdout);
  176.     }
  177.     fputc('\n', stdout);
  178.   }
  179.  
  180.   fputs("\033(B\017", stdout);
  181. }
  182.