| 1 |
/* pbmto10x.c - read a portable bitmap and produce a Gemini 10X printer file |
| 2 |
** |
| 3 |
** Copyright (C) 1990, 1994 by Ken Yap |
| 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 |
** Modified to shorten stripes and eliminate blank stripes. Dec 1994. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdbool.h> |
| 16 |
|
| 17 |
#include "pbm.h" |
| 18 |
#include "mallocvar.h" |
| 19 |
|
| 20 |
#define LOW_RES_ROWS 8 /* printed per pass */ |
| 21 |
#define HIGH_RES_ROWS 16 /* printed per pass */ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
static void |
| 26 |
outstripe(char * const stripe, |
| 27 |
char * const sP, |
| 28 |
int const reschar) { |
| 29 |
|
| 30 |
char * p; |
| 31 |
|
| 32 |
p = sP; /* initial value */ |
| 33 |
|
| 34 |
/* scan backwards, removing empty columns */ |
| 35 |
while (p != stripe) |
| 36 |
if (*--p != 0) { |
| 37 |
++p; |
| 38 |
break; |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
unsigned int const ncols = p - stripe; |
| 43 |
|
| 44 |
if (ncols > 0) { |
| 45 |
printf("\033%c%c%c", reschar, ncols % 256, ncols / 256); |
| 46 |
fwrite(stripe, sizeof(char), ncols, stdout); |
| 47 |
} |
| 48 |
} |
| 49 |
putchar('\n'); /* flush buffer */ |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
res_60x72(FILE * const ifP, |
| 56 |
int const rows, |
| 57 |
int const cols, |
| 58 |
int const format) { |
| 59 |
|
| 60 |
int row; |
| 61 |
unsigned int i; |
| 62 |
bit * bitrows[LOW_RES_ROWS]; |
| 63 |
char *stripe; |
| 64 |
char *sP; |
| 65 |
|
| 66 |
MALLOCARRAY(stripe, cols); |
| 67 |
if (stripe == NULL) |
| 68 |
pm_error("Unable to allocate %u bytes for a stripe buffer.", |
| 69 |
(unsigned)(cols * sizeof(stripe[0]))); |
| 70 |
|
| 71 |
for (i = 0; i < LOW_RES_ROWS; ++i) |
| 72 |
bitrows[i] = pbm_allocrow(cols); |
| 73 |
|
| 74 |
printf("\033A\010"); /* '\n' = 8/72 */ |
| 75 |
|
| 76 |
for (row = 0, sP = stripe; row < rows; row += LOW_RES_ROWS, sP = stripe) { |
| 77 |
unsigned int col; |
| 78 |
unsigned int i; |
| 79 |
unsigned int npins; |
| 80 |
bit * bP[LOW_RES_ROWS]; |
| 81 |
|
| 82 |
if (row + LOW_RES_ROWS <= rows) |
| 83 |
npins = LOW_RES_ROWS; |
| 84 |
else |
| 85 |
npins = rows - row; |
| 86 |
|
| 87 |
for (i = 0; i < npins; ++i) |
| 88 |
pbm_readpbmrow(ifP, bP[i] = bitrows[i], cols, format); |
| 89 |
|
| 90 |
for (col = 0; col < cols; ++col) { |
| 91 |
unsigned int item; |
| 92 |
|
| 93 |
item = 0; |
| 94 |
for (i = 0; i < npins; ++i) |
| 95 |
if (*(bP[i]++) == PBM_BLACK) |
| 96 |
item |= 1 << (7 - i); |
| 97 |
*sP++ = item; |
| 98 |
} |
| 99 |
outstripe(stripe, sP, 'K'); |
| 100 |
} |
| 101 |
printf("\033@"); |
| 102 |
|
| 103 |
for (i = 0; i < LOW_RES_ROWS; ++i) |
| 104 |
pbm_freerow(bitrows[i]); |
| 105 |
|
| 106 |
free(stripe); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static void |
| 112 |
res_120x144(FILE * const ifP, |
| 113 |
int const rows, |
| 114 |
int const cols, |
| 115 |
int const format) { |
| 116 |
|
| 117 |
unsigned int i; |
| 118 |
int row; |
| 119 |
char *stripe; |
| 120 |
char * sP; |
| 121 |
bit * bitrows[HIGH_RES_ROWS]; |
| 122 |
|
| 123 |
MALLOCARRAY(stripe, cols); |
| 124 |
if (stripe == NULL) |
| 125 |
pm_error("Unable to allocate %u bytes for a stripe buffer.", |
| 126 |
(unsigned)(cols * sizeof(stripe[0]))); |
| 127 |
|
| 128 |
for (i = 0; i < HIGH_RES_ROWS; ++i) |
| 129 |
bitrows[i] = pbm_allocrow(cols); |
| 130 |
|
| 131 |
printf("\0333\001"); /* \n = 1/144" */ |
| 132 |
|
| 133 |
for (row = 0, sP = stripe; row < rows; row += HIGH_RES_ROWS, sP = stripe) { |
| 134 |
unsigned int i; |
| 135 |
unsigned int col; |
| 136 |
bit * bP[HIGH_RES_ROWS]; |
| 137 |
unsigned int npins; |
| 138 |
|
| 139 |
if (row + HIGH_RES_ROWS <= rows) |
| 140 |
npins = HIGH_RES_ROWS; |
| 141 |
else |
| 142 |
npins = rows - row; |
| 143 |
for (i = 0; i < npins; ++i) |
| 144 |
pbm_readpbmrow(ifP, bP[i] = bitrows[i], cols, format); |
| 145 |
for (col = 0; col < cols; ++col) { |
| 146 |
unsigned int pin; |
| 147 |
unsigned int item; |
| 148 |
item = 0; |
| 149 |
/* even rows */ |
| 150 |
for (pin = i = 0; i < npins; i += 2, ++pin) |
| 151 |
if (*(bP[i]++) == PBM_BLACK) |
| 152 |
item |= 1 << (7 - pin); |
| 153 |
*sP++ = item; |
| 154 |
} |
| 155 |
outstripe(stripe, sP, 'L'); |
| 156 |
for (col = 0, sP = stripe; col < cols; ++col) { |
| 157 |
unsigned int pin; |
| 158 |
unsigned int item; |
| 159 |
item = 0; |
| 160 |
/* odd rows */ |
| 161 |
for (i = 1, pin = 0; i < npins; i += 2, ++pin) |
| 162 |
if (*(bP[i]++) == PBM_BLACK) |
| 163 |
item |= 1 << (7 - pin); |
| 164 |
*sP++ = item; |
| 165 |
} |
| 166 |
outstripe(stripe, sP, 'L'); |
| 167 |
printf("\033J\016"); /* 14/144 down, \n did 1/144 */ |
| 168 |
} |
| 169 |
printf("\033@"); |
| 170 |
|
| 171 |
for (i = 0; i < LOW_RES_ROWS; ++i) |
| 172 |
pbm_freerow(bitrows[i]); |
| 173 |
|
| 174 |
free(stripe); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int |
| 180 |
main(int argc, const char ** argv) { |
| 181 |
|
| 182 |
const char * fname; |
| 183 |
static FILE * ifP; |
| 184 |
int rows, cols, format; |
| 185 |
|
| 186 |
bool isHighRes; |
| 187 |
|
| 188 |
pm_proginit(&argc, argv); |
| 189 |
|
| 190 |
isHighRes = false; /* initial assumption */ |
| 191 |
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h') { |
| 192 |
isHighRes = true; |
| 193 |
--argc; |
| 194 |
++argv; |
| 195 |
} |
| 196 |
if (argc-1 > 1) |
| 197 |
pm_error("Too many arguments. Only argument is file name"); |
| 198 |
else if (argc-1 == 1) |
| 199 |
fname = argv[1]; |
| 200 |
else |
| 201 |
fname = "-"; |
| 202 |
|
| 203 |
ifP = pm_openr(fname); |
| 204 |
|
| 205 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 206 |
|
| 207 |
if (isHighRes) |
| 208 |
res_120x144(ifP, rows, cols, format); |
| 209 |
else |
| 210 |
res_60x72(ifP, rows, cols, format); |
| 211 |
|
| 212 |
pm_close(ifP); |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|