| 1 |
/* pbmtomrf - convert pbm to mrf |
| 2 |
* public domain by RJM |
| 3 |
* |
| 4 |
* Adapted to Netpbm by Bryan Henderson 2003.08.09. Bryan got his copy from |
| 5 |
* ftp://ibiblio.org/pub/linux/apps/convert, dated 1998.03.03. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <limits.h> |
| 12 |
|
| 13 |
#include "pm_c_util.h" |
| 14 |
#include "pbm.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
typedef struct bitOut { |
| 19 |
int bitbox; |
| 20 |
int bitsleft; |
| 21 |
FILE * fileP; |
| 22 |
} bitOut; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
static void |
| 27 |
bit_init(struct bitOut * const bitOutP, |
| 28 |
FILE * const ofP) { |
| 29 |
|
| 30 |
bitOutP->bitbox = 0; |
| 31 |
bitOutP->bitsleft = 8; |
| 32 |
bitOutP->fileP = ofP; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
static void |
| 38 |
bit_output(struct bitOut * const bitOutP, |
| 39 |
int const bit) { |
| 40 |
|
| 41 |
--bitOutP->bitsleft; |
| 42 |
bitOutP->bitbox |= (bit << bitOutP->bitsleft); |
| 43 |
if (bitOutP->bitsleft == 0) { |
| 44 |
fputc(bitOutP->bitbox, bitOutP->fileP); |
| 45 |
bitOutP->bitbox = 0; |
| 46 |
bitOutP->bitsleft = 8; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static void |
| 53 |
bit_flush(struct bitOut * const bitOutP) { |
| 54 |
/* there are never 0 bits left outside of bit_output, but |
| 55 |
* if 8 bits are left here there's nothing to flush, so |
| 56 |
* only do it if bitsleft!=8. |
| 57 |
*/ |
| 58 |
if (bitOutP->bitsleft != 8) { |
| 59 |
bitOutP->bitsleft = 1; |
| 60 |
bit_output(bitOutP, 0); /* yes, really. This will always work. */ |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static void |
| 67 |
determineBlackWhiteOrMix(const unsigned char * const image, |
| 68 |
unsigned int const ulCol, |
| 69 |
unsigned int const ulRow, |
| 70 |
unsigned int const imageWidth, |
| 71 |
unsigned int const size, |
| 72 |
bool * const oneColorP, |
| 73 |
int * const colorP) { |
| 74 |
/*---------------------------------------------------------------------------- |
| 75 |
Determine whether a square within 'image' is all white, all black, |
| 76 |
or a mix. |
| 77 |
-----------------------------------------------------------------------------*/ |
| 78 |
unsigned int rowOfSquare; |
| 79 |
unsigned int t; |
| 80 |
|
| 81 |
for (rowOfSquare = 0, t = 0; rowOfSquare < size; ++rowOfSquare) { |
| 82 |
unsigned int colOfSquare; |
| 83 |
for (colOfSquare = 0; colOfSquare < size; ++colOfSquare) { |
| 84 |
unsigned int rowOfImage = ulRow + rowOfSquare; |
| 85 |
unsigned int colOfImage = ulCol + colOfSquare; |
| 86 |
|
| 87 |
t += image[rowOfImage * imageWidth + colOfImage]; |
| 88 |
} |
| 89 |
} |
| 90 |
/* if the total's 0, it's black. if it's size*size, it's white. */ |
| 91 |
if (t == 0) { |
| 92 |
*oneColorP = TRUE; |
| 93 |
*colorP = 0; |
| 94 |
} else if (t == SQR(size)) { |
| 95 |
*oneColorP = TRUE; |
| 96 |
*colorP = 1; |
| 97 |
} else |
| 98 |
*oneColorP = FALSE; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static void |
| 104 |
doSquare(bitOut * const bitOutP, |
| 105 |
const unsigned char * const image, |
| 106 |
unsigned int const ulCol, |
| 107 |
unsigned int const ulRow, |
| 108 |
unsigned int const imageWidth, |
| 109 |
unsigned int const size) { |
| 110 |
/*---------------------------------------------------------------------------- |
| 111 |
Do a square of side 'size', whose upper left corner is at (ulCol, ulRow). |
| 112 |
This is a square within 'image', which is a concatenation of rows |
| 113 |
'imageWidth' pixels wide, one byte per pixel. |
| 114 |
|
| 115 |
Write the pixel values out to the bit stream *bitOutP, in MRF format. |
| 116 |
-----------------------------------------------------------------------------*/ |
| 117 |
if (size == 1) { |
| 118 |
/* The fact that it is all one color is implied because the square is |
| 119 |
just one pixel; no bit goes in MRF output to state that. |
| 120 |
*/ |
| 121 |
bit_output(bitOutP, image[ulRow * imageWidth + ulCol] ? 1 : 0); |
| 122 |
} else { |
| 123 |
bool oneColor; |
| 124 |
int color; |
| 125 |
|
| 126 |
determineBlackWhiteOrMix(image, ulCol, ulRow, imageWidth, size, |
| 127 |
&oneColor, &color); |
| 128 |
|
| 129 |
if (oneColor) { |
| 130 |
bit_output(bitOutP, 1); /* all same color */ |
| 131 |
bit_output(bitOutP, color); |
| 132 |
} else { |
| 133 |
/* Square is not all the same color, so recurse. Do each |
| 134 |
of the four quadrants of this square individually. |
| 135 |
*/ |
| 136 |
unsigned int const quadSize = size/2; |
| 137 |
|
| 138 |
bit_output(bitOutP, 0); /* not all same color */ |
| 139 |
|
| 140 |
doSquare(bitOutP, image, ulCol, ulRow, |
| 141 |
imageWidth, quadSize); |
| 142 |
doSquare(bitOutP, image, ulCol + quadSize, ulRow, |
| 143 |
imageWidth, quadSize); |
| 144 |
doSquare(bitOutP, image, ulCol, ulRow + quadSize, |
| 145 |
imageWidth, quadSize); |
| 146 |
doSquare(bitOutP, image, ulCol + quadSize, ulRow + quadSize, |
| 147 |
imageWidth, quadSize); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
static void |
| 155 |
fiddleRightEdge(unsigned char * const image, |
| 156 |
unsigned int const w, |
| 157 |
unsigned int const h, |
| 158 |
unsigned int const pw, |
| 159 |
bool * const flippedP) { |
| 160 |
|
| 161 |
unsigned int row; |
| 162 |
unsigned int t; |
| 163 |
|
| 164 |
for (row = t = 0; row < h; ++row) |
| 165 |
t += image[row*pw + w - 1]; |
| 166 |
|
| 167 |
if (t*2 > h) { |
| 168 |
unsigned int row; |
| 169 |
|
| 170 |
*flippedP = TRUE; |
| 171 |
for (row = 0; row < h; ++row) { |
| 172 |
unsigned int col; |
| 173 |
for (col = w; col < pw; ++col) |
| 174 |
image[row*pw + col] = 1; |
| 175 |
} |
| 176 |
} else |
| 177 |
*flippedP = FALSE; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static void |
| 183 |
fiddleBottomEdge(unsigned char * const image, |
| 184 |
unsigned int const w, |
| 185 |
unsigned int const h, |
| 186 |
unsigned int const pw, |
| 187 |
unsigned int const ph, |
| 188 |
bool * const flippedP) { |
| 189 |
|
| 190 |
unsigned int col; |
| 191 |
unsigned int t; |
| 192 |
|
| 193 |
for (col = t = 0; col < w; ++col) |
| 194 |
t += image[(h-1)*pw + col]; |
| 195 |
|
| 196 |
if (t*2 > w) { |
| 197 |
unsigned int row; |
| 198 |
*flippedP = TRUE; |
| 199 |
for (row = h; row < ph; ++row) { |
| 200 |
unsigned int col; |
| 201 |
for (col = 0; col < w; ++col) |
| 202 |
image[row*pw + col] = 1; |
| 203 |
} |
| 204 |
} else |
| 205 |
*flippedP = FALSE; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
static void |
| 211 |
fiddleBottomRightCorner(unsigned char * const image, |
| 212 |
unsigned int const w, |
| 213 |
unsigned int const h, |
| 214 |
unsigned int const pw, |
| 215 |
unsigned int const ph) { |
| 216 |
unsigned int row; |
| 217 |
|
| 218 |
for (row = h; row < ph; ++row) { |
| 219 |
unsigned int col; |
| 220 |
|
| 221 |
for (col = w; col < pw; ++col) |
| 222 |
image[row*pw + col] = 1; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static void |
| 229 |
fiddleEdges(unsigned char * const image, |
| 230 |
int const cols, |
| 231 |
int const rows) { |
| 232 |
/* the aim of this routine is play around with the edges which |
| 233 |
* are compressed into the mrf but thrown away when it's decompressed, |
| 234 |
* such that we get the best compression possible. |
| 235 |
* If you don't see why this is a good idea, consider the simple case |
| 236 |
* of a 1x1 white pixel. Placed on a black 64x64 this takes several bytes |
| 237 |
* to compress. On a white 64x64, it takes two bits. |
| 238 |
* (Clearly most cases will be more complicated, but you should get the |
| 239 |
* basic idea from that.) |
| 240 |
*/ |
| 241 |
|
| 242 |
/* there are many possible approaches to this problem, and this one's |
| 243 |
* certainly not the best, but at least it's quick and easy, and it's |
| 244 |
* better than nothing. :-) |
| 245 |
* |
| 246 |
* So, all we do is flip the runoff area of an edge to white |
| 247 |
* if more than half of the pixels on that edge are |
| 248 |
* white. Then for the bottom-right runoff square (if there is |
| 249 |
* one), we flip it if we flipped both edges. |
| 250 |
*/ |
| 251 |
|
| 252 |
/* w64 is units-of-64-bits width, h64 same for height */ |
| 253 |
unsigned int const w64 = (cols + 63) / 64; |
| 254 |
unsigned int const h64 = (rows + 63) / 64; |
| 255 |
|
| 256 |
int const pw=w64*64; |
| 257 |
int const ph=h64*64; |
| 258 |
|
| 259 |
bool flippedRight, flippedBottom; |
| 260 |
|
| 261 |
if (cols % 64 != 0) |
| 262 |
fiddleRightEdge(image, cols, rows, pw, &flippedRight); |
| 263 |
else |
| 264 |
flippedRight = FALSE; |
| 265 |
|
| 266 |
if (rows % 64 != 0) |
| 267 |
fiddleBottomEdge(image, cols, rows, pw, ph, &flippedBottom); |
| 268 |
else |
| 269 |
flippedBottom = FALSE; |
| 270 |
|
| 271 |
if (flippedRight && flippedBottom) |
| 272 |
fiddleBottomRightCorner(image, cols, rows, pw, ph); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
static void |
| 278 |
readPbmImage(FILE * const ifP, |
| 279 |
unsigned char ** const imageP, |
| 280 |
int * const colsP, |
| 281 |
int * const rowsP) { |
| 282 |
|
| 283 |
|
| 284 |
/* w64 is units-of-64-bits width, h64 same for height */ |
| 285 |
unsigned int w64, h64; |
| 286 |
|
| 287 |
unsigned char * image; |
| 288 |
int cols, rows, format; |
| 289 |
unsigned int row; |
| 290 |
bit * bitrow; |
| 291 |
|
| 292 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 293 |
|
| 294 |
w64 = (cols + 63) / 64; |
| 295 |
h64 = (rows + 63) / 64; |
| 296 |
|
| 297 |
if (UINT_MAX/w64/64/h64/64 == 0) |
| 298 |
pm_error("Ridiculously large, unprocessable image: %u cols x %u rows", |
| 299 |
cols, rows); |
| 300 |
|
| 301 |
image = calloc(w64*h64*64*64, 1); |
| 302 |
if (image == NULL) |
| 303 |
pm_error("Unable to get memory for raster"); |
| 304 |
|
| 305 |
/* get bytemap image rounded up into mod 64x64 squares */ |
| 306 |
|
| 307 |
bitrow = pbm_allocrow(cols); |
| 308 |
|
| 309 |
for (row = 0; row < rows; ++row) { |
| 310 |
unsigned int col; |
| 311 |
|
| 312 |
pbm_readpbmrow(ifP, bitrow, cols, format); |
| 313 |
|
| 314 |
for (col =0; col < cols; ++col) |
| 315 |
image[row*(w64*64) + col] = (bitrow[col] == PBM_WHITE ? 1 : 0); |
| 316 |
} |
| 317 |
pbm_freerow(bitrow); |
| 318 |
*imageP = image; |
| 319 |
*colsP = cols; |
| 320 |
*rowsP = rows; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static void |
| 326 |
outputMrf(FILE * const ofP, |
| 327 |
unsigned char * const image, |
| 328 |
unsigned int const cols, |
| 329 |
unsigned int const rows) { |
| 330 |
|
| 331 |
unsigned int const w64 = (cols + 63) / 64; |
| 332 |
unsigned int const h64 = (rows + 63) / 64; |
| 333 |
|
| 334 |
bitOut bitOut; |
| 335 |
|
| 336 |
unsigned int row; |
| 337 |
|
| 338 |
fprintf(ofP, "MRF1"); |
| 339 |
fprintf(ofP, "%c%c%c%c", cols >> 24, cols >> 16, cols >> 8, cols >> 0); |
| 340 |
fprintf(ofP, "%c%c%c%c", rows >> 24, rows >> 16, rows >> 8, rows >> 0); |
| 341 |
fputc(0, ofP); /* option byte, unused for now */ |
| 342 |
|
| 343 |
/* now recursively check squares. */ |
| 344 |
|
| 345 |
bit_init(&bitOut, ofP); |
| 346 |
|
| 347 |
for (row = 0; row < h64; ++row) { |
| 348 |
unsigned int col; |
| 349 |
for (col = 0; col < w64; ++col) |
| 350 |
doSquare(&bitOut, image, col*64, row*64, w64*64, 64); |
| 351 |
} |
| 352 |
bit_flush(&bitOut); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
int |
| 358 |
main(int argc,char *argv[]) { |
| 359 |
|
| 360 |
FILE * ifP; |
| 361 |
FILE * ofP; |
| 362 |
unsigned char *image; |
| 363 |
int rows, cols; |
| 364 |
|
| 365 |
pbm_init(&argc, argv); |
| 366 |
|
| 367 |
if (argc-1 > 1) |
| 368 |
pm_error("Too many arguments: %d. Only argument is input file", |
| 369 |
argc-1); |
| 370 |
|
| 371 |
if (argc-1 == 1) |
| 372 |
ifP = pm_openr(argv[1]); |
| 373 |
else |
| 374 |
ifP = stdin; |
| 375 |
|
| 376 |
ofP = stdout; |
| 377 |
|
| 378 |
readPbmImage(ifP, &image, &cols, &rows); |
| 379 |
|
| 380 |
pm_close(ifP); |
| 381 |
|
| 382 |
/* if necessary, alter the unused outside area to aid compression of |
| 383 |
* edges of image. |
| 384 |
*/ |
| 385 |
|
| 386 |
fiddleEdges(image, cols, rows); |
| 387 |
|
| 388 |
outputMrf(ofP, image, cols, rows); |
| 389 |
|
| 390 |
free(image); |
| 391 |
|
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|