| 1 |
/* brushtopbm.c - read a doodle brush file and write a PBM image |
| 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 |
#define HEADERSIZE 16 /* 16 is just a guess at the header size */ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
static void |
| 20 |
getinit(FILE * const ifP, |
| 21 |
unsigned int * const colsP, |
| 22 |
unsigned int * const rowsP) { |
| 23 |
|
| 24 |
unsigned char header[HEADERSIZE]; |
| 25 |
size_t bytesRead; |
| 26 |
|
| 27 |
bytesRead = fread(header, sizeof(header), 1, ifP); |
| 28 |
if (bytesRead !=1) |
| 29 |
pm_error("Error reading header"); |
| 30 |
|
| 31 |
if (header[0] != 1) |
| 32 |
pm_error("bad magic number 1"); |
| 33 |
if (header[1] != 0) |
| 34 |
pm_error("bad magic number 2"); |
| 35 |
|
| 36 |
*colsP = (header[2] << 8) + header[3]; /* Max 65535 */ |
| 37 |
*rowsP = (header[4] << 8) + header[5]; /* Max 65535 */ |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static void |
| 43 |
validateEof(FILE * const ifP) { |
| 44 |
|
| 45 |
int rc; |
| 46 |
rc = getc(ifP); |
| 47 |
if (rc != EOF) |
| 48 |
pm_message("Extraneous data at end of file"); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
/* |
| 54 |
The routine for converting the raster closely resembles the pbm |
| 55 |
case of pnminvert. Input is padded up to 16 bit border. |
| 56 |
afu December 2013 |
| 57 |
*/ |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
int |
| 62 |
main(int argc, const char ** argv) { |
| 63 |
|
| 64 |
FILE * ifP; |
| 65 |
bit * bitrow; |
| 66 |
unsigned int rows, cols, row; |
| 67 |
|
| 68 |
pm_proginit(&argc, argv); |
| 69 |
|
| 70 |
if (argc-1 > 0) { |
| 71 |
ifP = pm_openr(argv[1]); |
| 72 |
if (argc-1 > 1) |
| 73 |
pm_error("Too many arguments (%u). " |
| 74 |
"The only argument is the brush file name.", argc-1); |
| 75 |
} else |
| 76 |
ifP = stdin; |
| 77 |
|
| 78 |
getinit(ifP, &cols, &rows); |
| 79 |
|
| 80 |
pbm_writepbminit(stdout, cols, rows, 0); |
| 81 |
|
| 82 |
bitrow = pbm_allocrow_packed(cols + 16); |
| 83 |
|
| 84 |
for (row = 0; row < rows; ++row) { |
| 85 |
unsigned int const inRowBytes = ((cols + 15) / 16) * 2; |
| 86 |
unsigned int i; |
| 87 |
size_t bytesRead; |
| 88 |
|
| 89 |
bytesRead = fread (bitrow, 1, inRowBytes, ifP); |
| 90 |
if (bytesRead != inRowBytes) |
| 91 |
pm_error("Error reading a row of data from brushfile"); |
| 92 |
|
| 93 |
for (i = 0; i < inRowBytes; ++i) |
| 94 |
bitrow[i] = ~bitrow[i]; |
| 95 |
|
| 96 |
/* Clean off remainder of fractional last character */ |
| 97 |
pbm_cleanrowend_packed(bitrow, cols); |
| 98 |
|
| 99 |
pbm_writepbmrow_packed(stdout, bitrow, cols, 0); |
| 100 |
} |
| 101 |
|
| 102 |
validateEof(ifP); |
| 103 |
|
| 104 |
pm_close(ifP); |
| 105 |
pm_close(stdout); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|