home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / pbmtochar / part01 / pbmtochar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-25  |  2.5 KB  |  100 lines

  1. /*
  2.  * pbmtochar.c - read a portable bitmap and produce character graphics
  3.  * 
  4.  * Copyright (C) 1989 Diomidis D. Spinellis.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation.  This software is provided "as is" without express or
  11.  * implied warranty.
  12.  *
  13.  * Comments and additions should be sent to the author:
  14.  *
  15.  *                     Diomidis D. Spinellis
  16.  *                     1 Myrsinis Str.
  17.  *                     GR-145 62 Kifissia
  18.  *                     GREECE
  19.  *               (dds@cc.ic.ac.uk)
  20.  *
  21.  * $Header: pbmtochar.c,v 1.4 89/10/06 22:41:44 dds Rel $
  22.  *
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include "pbm.h"
  27. #include "8x13.h"
  28.  
  29. #ifndef lint
  30. static char RCSid[] = "$Header: pbmtochar.c,v 1.4 89/10/06 22:41:44 dds Rel $";
  31. #endif
  32.  
  33. main(argc, argv)
  34.     int             argc;
  35.     char           *argv[];
  36. {
  37.     FILE           *ifd;
  38.     bit        **bits;
  39.     int             rows, cols, row, col;
  40.     int        bestmatch = -1;
  41.     char        *bestmatchchar;
  42.     register    b2;
  43.     register    bit *p;
  44.     register    match;
  45.     register    bit **pp;
  46.     register    b1;
  47.     register    char *fp;
  48.     bit        **ppstart, **ppend;
  49.  
  50.     pm_progname = argv[0];
  51.  
  52.     if (argc > 2)
  53.         pm_usage("[pbmfile]");
  54.  
  55.     if (argc == 2)
  56.         ifd = pm_openr(argv[1]);
  57.     else
  58.         ifd = stdin;
  59.  
  60.     bits = pbm_readpbm(ifd, &cols, &rows);
  61.  
  62.     pm_close(ifd);
  63.  
  64.     /*
  65.      * The following code is UGLY, but fast.
  66.      * It was hand optimised by inlining three functions into each other,
  67.      * converting all array references to pointers and doing some loop
  68.      * inductions.  The result is 3 times faster than the original.
  69.      * The original was compiled with gcc -O and the inline keyword was
  70.      * used.
  71.      */
  72.     for (    row = 0, ppstart = bits, ppend = bits + FROW;
  73.         row + FROW - 1 < rows; row +=  FROW, ppstart = ppend, ppend += FROW) {
  74.         for (col = 0; col + FCOL - 1 < cols; col += FCOL) {
  75.             bestmatch = -1;
  76.             for (fp = &font[0][0]; fp < &font[NUMCHARS][FROW];) {
  77.                 match = 0;
  78.                 for (pp = ppstart; pp < ppend; pp++) {
  79.                     b1 = *fp++;
  80.                     for (p = *pp + col, b2 = 0x80; b2 != (0x80 >> FCOL); b2 >>= 1, p++)
  81.                         if (b1 & b2) {
  82.                             if (*p == PBM_BLACK)
  83.                                 match++;
  84.                         } else {
  85.                             if (*p == PBM_WHITE)
  86.                                 match++;
  87.                         }
  88.                 }
  89.                 if (match > bestmatch) {
  90.                     bestmatch = match;
  91.                     bestmatchchar = fp;
  92.                 }
  93.             }
  94.             putchar(patternchars[(bestmatchchar - &font[0][0]) / FROW - 1]);
  95.         }
  96.         putchar('\n');
  97.     }
  98.     exit(0);
  99. }
  100.