| 1 |
/*============================================================================= |
| 2 |
pgmtosbig |
| 3 |
=============================================================================== |
| 4 |
|
| 5 |
This program converts from PGM to a simple subset of SBIG. |
| 6 |
|
| 7 |
By Bryan Henderson January 19, 2015. |
| 8 |
|
| 9 |
Contributed to the public domain by its author. |
| 10 |
=============================================================================*/ |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "pm.h" |
| 14 |
#include "nstring.h" |
| 15 |
#include "pgm.h" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define SBIG_HEADER_LENGTH 2048 /* File header length */ |
| 20 |
|
| 21 |
#define CTLZ "\x1A" |
| 22 |
|
| 23 |
|
| 24 |
struct SbigHeader { |
| 25 |
/*---------------------------------------------------------------------------- |
| 26 |
The information in an SBIG file header. |
| 27 |
|
| 28 |
This is only the information this program cares about; the header |
| 29 |
may have much more information in it. |
| 30 |
-----------------------------------------------------------------------------*/ |
| 31 |
unsigned int height; |
| 32 |
unsigned int width; |
| 33 |
unsigned int saturationLevel; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static void |
| 39 |
addUintParm(char * const buffer, |
| 40 |
const char * const name, |
| 41 |
unsigned int const value) { |
| 42 |
|
| 43 |
const char * line; |
| 44 |
|
| 45 |
pm_asprintf(&line, "%s=%u\n\r", name, value); |
| 46 |
|
| 47 |
strcat(buffer, line); |
| 48 |
|
| 49 |
pm_strfree(line); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
writeSbigHeader(FILE * const ofP, |
| 56 |
struct SbigHeader const hdr) { |
| 57 |
|
| 58 |
char buffer[SBIG_HEADER_LENGTH]; |
| 59 |
|
| 60 |
memset(&buffer[0], 0x00, sizeof(buffer)); |
| 61 |
|
| 62 |
buffer[0] = '\0'; |
| 63 |
|
| 64 |
/* N.B. LF-CR instead of CRLF. That's what the spec says. */ |
| 65 |
|
| 66 |
strcat(buffer, "ST-6 Image\n\r" ); |
| 67 |
|
| 68 |
addUintParm(buffer, "Height", hdr.height); |
| 69 |
|
| 70 |
addUintParm(buffer, "Width", hdr.width); |
| 71 |
|
| 72 |
addUintParm(buffer, "Sat_level", hdr.saturationLevel); |
| 73 |
|
| 74 |
strcat(buffer, "End\n\r" CTLZ); |
| 75 |
|
| 76 |
fwrite(buffer, 1, sizeof(buffer), ofP); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
int |
| 82 |
main(int argc, const char * argv[]) { |
| 83 |
|
| 84 |
FILE * ifP; |
| 85 |
gray * grayrow; |
| 86 |
int rows; |
| 87 |
int cols; |
| 88 |
int format; |
| 89 |
struct SbigHeader hdr; |
| 90 |
unsigned int row; |
| 91 |
gray maxval; |
| 92 |
const char * inputFile; |
| 93 |
|
| 94 |
pm_proginit(&argc, argv); |
| 95 |
|
| 96 |
if (argc-1 < 1) |
| 97 |
inputFile = "-"; |
| 98 |
else { |
| 99 |
inputFile = argv[1]; |
| 100 |
|
| 101 |
if (argc-1 > 2) |
| 102 |
pm_error("Too many arguments. The only argument is the optional " |
| 103 |
"input file name"); |
| 104 |
} |
| 105 |
|
| 106 |
ifP = pm_openr(inputFile); |
| 107 |
|
| 108 |
pgm_readpgminit(ifP, &cols, &rows, &maxval, &format); |
| 109 |
|
| 110 |
grayrow = pgm_allocrow(cols); |
| 111 |
|
| 112 |
hdr.height = rows; |
| 113 |
hdr.width = cols; |
| 114 |
hdr.saturationLevel = maxval; |
| 115 |
|
| 116 |
writeSbigHeader(stdout, hdr); |
| 117 |
|
| 118 |
for (row = 0; row < rows; ++row) { |
| 119 |
unsigned int col; |
| 120 |
|
| 121 |
pgm_readpgmrow(ifP, grayrow, cols, maxval, format); |
| 122 |
|
| 123 |
for (col = 0; col < cols; ++col) |
| 124 |
pm_writelittleshort(stdout, grayrow[col]); |
| 125 |
} |
| 126 |
|
| 127 |
pm_close(ifP); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|