| 1 |
/*************************************************************************** |
| 2 |
|
| 3 |
PBMTOMDA: Convert PBM to Microdesign area |
| 4 |
Copyright (C) 1999,2004 John Elliott <[email protected]> |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
|
| 20 |
******************************************************************************/ |
| 21 |
|
| 22 |
#include <stdbool.h> |
| 23 |
#include <assert.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "pbm.h" |
| 28 |
#include "mallocvar.h" |
| 29 |
#include "shhopt.h" |
| 30 |
|
| 31 |
struct CmdlineInfo { |
| 32 |
/* All the information the user supplied in the command line, |
| 33 |
in a form easy for the program to use. |
| 34 |
*/ |
| 35 |
const char * inputFileNm; |
| 36 |
unsigned int dscale; |
| 37 |
unsigned int invert; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
static void |
| 42 |
parseCommandLine(int argc, const char ** argv, |
| 43 |
struct CmdlineInfo * const cmdlineP) { |
| 44 |
/*---------------------------------------------------------------------------- |
| 45 |
Note that the file spec array we return is stored in the storage that |
| 46 |
was passed to as as the argv array. |
| 47 |
-----------------------------------------------------------------------------*/ |
| 48 |
optEntry * option_def; |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
|
| 53 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 54 |
|
| 55 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 56 |
OPTENT3(0, "dscale", OPT_FLAG, NULL, &cmdlineP->dscale, 0); |
| 57 |
OPTENT3(0, "invert", OPT_FLAG, NULL, &cmdlineP->invert, 0); |
| 58 |
|
| 59 |
opt.opt_table = option_def; |
| 60 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 61 |
opt.allowNegNum = false; /* We have no parms that are negative numbers */ |
| 62 |
|
| 63 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 64 |
/* Uses and sets argc, argv, and some of *cmdlineP and others */ |
| 65 |
|
| 66 |
if (argc-1 < 1) |
| 67 |
cmdlineP->inputFileNm = "-"; |
| 68 |
else if (argc-1 == 1) |
| 69 |
cmdlineP->inputFileNm = argv[1]; |
| 70 |
else |
| 71 |
pm_error("Program takes at most one argument: input file name"); |
| 72 |
|
| 73 |
free(option_def); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
/* I'm being somewhat conservative in the PBM -> MDA translation. I output |
| 79 |
* only the MD2 format and don't allow RLE over the ends of lines. |
| 80 |
*/ |
| 81 |
|
| 82 |
typedef unsigned char Mdbyte; |
| 83 |
|
| 84 |
/* Encode 8 pixels as a byte */ |
| 85 |
|
| 86 |
static Mdbyte |
| 87 |
encode(bit ** const bits, |
| 88 |
int const row, |
| 89 |
int const col) { |
| 90 |
|
| 91 |
int n; |
| 92 |
int mask; |
| 93 |
Mdbyte b; |
| 94 |
|
| 95 |
mask = 0x80; /* initial value */ |
| 96 |
b = 0; /* initial value */ |
| 97 |
|
| 98 |
for (n = 0; n < 8; n++) { |
| 99 |
if (bits[row][col+n] == PBM_BLACK) b |= mask; |
| 100 |
mask = mask >> 1; |
| 101 |
} |
| 102 |
return b; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void |
| 108 |
doTranslation(bit ** const bits, |
| 109 |
unsigned int const nOutCols, |
| 110 |
unsigned int const nOutRows, |
| 111 |
unsigned int const nInRows, |
| 112 |
bool const mustInvert, |
| 113 |
bool const mustScale) { |
| 114 |
/*---------------------------------------------------------------------------- |
| 115 |
Translate a pbm to MD2 format, one row at a time |
| 116 |
-----------------------------------------------------------------------------*/ |
| 117 |
unsigned int const step = mustScale ? 2 : 1; |
| 118 |
|
| 119 |
unsigned int row; |
| 120 |
Mdbyte * mdrow; /* malloc'ed */ |
| 121 |
|
| 122 |
MALLOCARRAY(mdrow, nOutCols); |
| 123 |
|
| 124 |
if (mdrow == NULL) |
| 125 |
pm_error("Unable to allocate memory for %u columns", nOutCols); |
| 126 |
|
| 127 |
for (row = 0; row < nOutRows; row += step) { |
| 128 |
unsigned int col; |
| 129 |
|
| 130 |
/* Encode image into non-compressed bitmap */ |
| 131 |
for (col = 0; col < nOutCols; ++col) { |
| 132 |
Mdbyte b; |
| 133 |
|
| 134 |
if (row < nInRows) |
| 135 |
b = encode(bits, row, col * 8); |
| 136 |
else |
| 137 |
b = 0xff; /* All black */ |
| 138 |
|
| 139 |
mdrow[col] = mustInvert ? b : ~b; |
| 140 |
} |
| 141 |
|
| 142 |
/* Encoded. Now RLE it */ |
| 143 |
for (col = 0; col < nOutCols; ) { |
| 144 |
Mdbyte const b = mdrow[col]; |
| 145 |
|
| 146 |
if (b != 0xFF && b != 0) { |
| 147 |
/* Normal byte */ |
| 148 |
putchar(b); |
| 149 |
++col; |
| 150 |
} else { |
| 151 |
/* RLE a run of 0s or 0xFFs */ |
| 152 |
|
| 153 |
unsigned int x1; |
| 154 |
|
| 155 |
for (x1 = col; x1 < nOutCols; ++x1) { |
| 156 |
if (mdrow[x1] != b) break; |
| 157 |
assert(x1 >= col); |
| 158 |
if (x1 - col > 256) break; |
| 159 |
} |
| 160 |
assert(x1 >= col); |
| 161 |
x1 -= col; /* x1 = no. of repeats */ |
| 162 |
if (x1 == 256) x1 = 0; |
| 163 |
putchar(b); |
| 164 |
putchar(x1); |
| 165 |
col += x1; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
free(mdrow); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int |
| 175 |
main(int argc, const char ** argv) { |
| 176 |
|
| 177 |
const char * const headerValue = ".MDAMicroDesignPCWv1.00\r\npbm2mda\r\n"; |
| 178 |
|
| 179 |
struct CmdlineInfo cmdline; |
| 180 |
FILE * ifP; |
| 181 |
unsigned int nOutRowsUnrounded; /* Before rounding up to multiple of 4 */ |
| 182 |
unsigned int nOutCols, nOutRows; |
| 183 |
int nInCols, nInRows; |
| 184 |
bit ** bits; |
| 185 |
Mdbyte header[128]; |
| 186 |
int rc; |
| 187 |
|
| 188 |
pm_proginit(&argc, argv); |
| 189 |
|
| 190 |
parseCommandLine(argc, argv, &cmdline); |
| 191 |
|
| 192 |
/* Output v2-format MDA images. Simulate MDA header... |
| 193 |
* 2004-01-11: Hmm. Apparently some (but not all) MDA-reading |
| 194 |
* programs insist on the program identifier being exactly |
| 195 |
* 'MicroDesignPCW'. The spec does not make this clear. */ |
| 196 |
memcpy(header + 0, headerValue, strlen(headerValue)); |
| 197 |
memset(header + strlen(headerValue), |
| 198 |
0x00, |
| 199 |
sizeof(header)-strlen(headerValue)); |
| 200 |
|
| 201 |
ifP = pm_openr(cmdline.inputFileNm); |
| 202 |
|
| 203 |
bits = pbm_readpbm(ifP, &nInCols, &nInRows); |
| 204 |
|
| 205 |
nOutRowsUnrounded = cmdline.dscale ? nInRows/2 : nInRows; |
| 206 |
|
| 207 |
nOutRows = ((nOutRowsUnrounded + 3) / 4) * 4; |
| 208 |
/* MDA wants rows a multiple of 4 */ |
| 209 |
nOutCols = nInCols / 8; |
| 210 |
|
| 211 |
rc = fwrite(header, 1, 128, stdout); |
| 212 |
if (rc < 128) |
| 213 |
pm_error("Unable to write header to output file. errno=%d (%s)", |
| 214 |
errno, strerror(errno)); |
| 215 |
|
| 216 |
pm_writelittleshort(stdout, nOutRows); |
| 217 |
pm_writelittleshort(stdout, nOutCols); |
| 218 |
|
| 219 |
doTranslation(bits, nOutCols, nOutRows, nInRows, |
| 220 |
!!cmdline.invert, !!cmdline.dscale); |
| 221 |
|
| 222 |
pm_close(ifP); |
| 223 |
fflush(stdout); |
| 224 |
pbm_freearray(bits, nInRows); |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|