| 1 |
/* pbmtoptx.c - read a portable bitmap and produce a Printronix printer file |
| 2 |
** |
| 3 |
** Copyright (C) 1988 by Jef Poskanzer. |
| 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 |
#include "pbm.h" |
| 14 |
|
| 15 |
/* Following is obtained by reversing bit order (MFS-LFS) and adding 64. */ |
| 16 |
/* Note the two escape sequences: \\ and \x7f . */ |
| 17 |
|
| 18 |
static unsigned char const ptxchar[64] = |
| 19 |
"@`PpHhXxDdTtLl\\|BbRrJjZzFfVvNn^~AaQqIiYyEeUuMm]}CcSsKk[{GgWwOo_\x7f"; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static void |
| 24 |
putBitrow(const bit * const bitrow, |
| 25 |
unsigned int const cols) { |
| 26 |
/*---------------------------------------------------------------------------- |
| 27 |
Pick up items in 6 bit units from bitrow and convert each to ptx format. |
| 28 |
----------------------------------------------------------------------------*/ |
| 29 |
unsigned int itemCnt; |
| 30 |
|
| 31 |
for (itemCnt = 0; itemCnt * 6 < cols; ++itemCnt) { |
| 32 |
unsigned int const byteCnt = (itemCnt * 6) / 8; |
| 33 |
bit const byteCur = bitrow[byteCnt]; |
| 34 |
bit const byteNext = bitrow[byteCnt + 1]; |
| 35 |
|
| 36 |
unsigned int item; |
| 37 |
|
| 38 |
switch (itemCnt % 4) { |
| 39 |
case 0: item = byteCur >> 2; break; |
| 40 |
case 1: item = byteCur << 4 | byteNext >> 4; break; |
| 41 |
case 2: item = byteCur << 2 | byteNext >> 6; break; |
| 42 |
case 3: item = byteCur; break; |
| 43 |
} |
| 44 |
putchar(ptxchar[item & 0x3f]); |
| 45 |
} |
| 46 |
putchar(5); putchar('\n'); /* end of row mark */ |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
int |
| 52 |
main(int argc, const char ** argv) { |
| 53 |
|
| 54 |
FILE * ifP; |
| 55 |
bit * bitrow; |
| 56 |
int rows, cols, format; |
| 57 |
unsigned int row; |
| 58 |
|
| 59 |
pm_proginit(&argc, argv); |
| 60 |
|
| 61 |
if (argc-1 < 1) |
| 62 |
ifP = stdin; |
| 63 |
else { |
| 64 |
ifP = pm_openr(argv[1]); |
| 65 |
|
| 66 |
if (argc-1 > 1) |
| 67 |
pm_error("Too many arguments. The only possible argument is " |
| 68 |
"the input fil name"); |
| 69 |
} |
| 70 |
|
| 71 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 72 |
|
| 73 |
bitrow = pbm_allocrow_packed(cols + 8); |
| 74 |
|
| 75 |
bitrow[pbm_packed_bytes(cols)] = 0x00; |
| 76 |
|
| 77 |
for (row = 0; row < rows; ++row) { |
| 78 |
pbm_readpbmrow_packed(ifP, bitrow, cols, format); |
| 79 |
pbm_cleanrowend_packed(bitrow, cols); |
| 80 |
putBitrow(bitrow, cols); |
| 81 |
} |
| 82 |
|
| 83 |
pbm_freerow_packed(bitrow); |
| 84 |
pm_close(ifP); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|