home *** CD-ROM | disk | FTP | other *** search
- /*
- * pbmtochar.c - read a portable bitmap and produce character graphics
- *
- * Copyright (C) 1989 Diomidis D. Spinellis.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation. This software is provided "as is" without express or
- * implied warranty.
- *
- * Comments and additions should be sent to the author:
- *
- * Diomidis D. Spinellis
- * 1 Myrsinis Str.
- * GR-145 62 Kifissia
- * GREECE
- * (dds@cc.ic.ac.uk)
- *
- * $Header: pbmtochar.c,v 1.4 89/10/06 22:41:44 dds Rel $
- *
- */
-
- #include <stdio.h>
- #include "pbm.h"
- #include "8x13.h"
-
- #ifndef lint
- static char RCSid[] = "$Header: pbmtochar.c,v 1.4 89/10/06 22:41:44 dds Rel $";
- #endif
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- FILE *ifd;
- bit **bits;
- int rows, cols, row, col;
- int bestmatch = -1;
- char *bestmatchchar;
- register b2;
- register bit *p;
- register match;
- register bit **pp;
- register b1;
- register char *fp;
- bit **ppstart, **ppend;
-
- pm_progname = argv[0];
-
- if (argc > 2)
- pm_usage("[pbmfile]");
-
- if (argc == 2)
- ifd = pm_openr(argv[1]);
- else
- ifd = stdin;
-
- bits = pbm_readpbm(ifd, &cols, &rows);
-
- pm_close(ifd);
-
- /*
- * The following code is UGLY, but fast.
- * It was hand optimised by inlining three functions into each other,
- * converting all array references to pointers and doing some loop
- * inductions. The result is 3 times faster than the original.
- * The original was compiled with gcc -O and the inline keyword was
- * used.
- */
- for ( row = 0, ppstart = bits, ppend = bits + FROW;
- row + FROW - 1 < rows; row += FROW, ppstart = ppend, ppend += FROW) {
- for (col = 0; col + FCOL - 1 < cols; col += FCOL) {
- bestmatch = -1;
- for (fp = &font[0][0]; fp < &font[NUMCHARS][FROW];) {
- match = 0;
- for (pp = ppstart; pp < ppend; pp++) {
- b1 = *fp++;
- for (p = *pp + col, b2 = 0x80; b2 != (0x80 >> FCOL); b2 >>= 1, p++)
- if (b1 & b2) {
- if (*p == PBM_BLACK)
- match++;
- } else {
- if (*p == PBM_WHITE)
- match++;
- }
- }
- if (match > bestmatch) {
- bestmatch = match;
- bestmatchchar = fp;
- }
- }
- putchar(patternchars[(bestmatchchar - &font[0][0]) / FROW - 1]);
- }
- putchar('\n');
- }
- exit(0);
- }
-