Debian Codegraph @

converter/pgm/pgmtost4.c

raw
1 /*=============================================================================
2 pgmtost4
3 ===============================================================================
4
5 This program converts from PGM to a simple subset of SBIG ST-4.
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 "pam.h"
16
17
18
19 static unsigned int const st4Height = 165;
20 static unsigned int const st4Width = 192;
21 static unsigned int const st4Maxval = 255;
22
23
24
25 static void
26 writeSt4Footer(FILE * const ofP) {
27
28 const char * const comment = "This was created by Pgmtost4";
29 char buffer[192];
30
31 memset(buffer, ' ', sizeof(buffer)); /* initial value */
32
33 buffer[0] = 'v';
34
35 memcpy(&buffer[ 0], "v", 1);
36 memcpy(&buffer[ 1], comment, strlen(comment));
37 memcpy(&buffer[ 79], " 7", 10);
38 memcpy(&buffer[ 89], " 8", 10);
39 memcpy(&buffer[ 99], " 9", 10);
40 memcpy(&buffer[109], " 10", 10);
41
42 fwrite(buffer, 1, sizeof(buffer), ofP);
43 }
44
45
46
47 int
48 main(int argc, const char * argv[]) {
49
50 FILE * ifP;
51 tuple * tuplerow;
52 struct pam inpam;
53 unsigned int row;
54 const char * inputFile;
55
56 pm_proginit(&argc, argv);
57
58 if (argc-1 < 1)
59 inputFile = "-";
60 else {
61 inputFile = argv[1];
62
63 if (argc-1 > 2)
64 pm_error("Too many arguments. The only argument is the optional "
65 "input file name");
66 }
67
68 ifP = pm_openr(inputFile);
69
70 pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
71
72 if (inpam.height != st4Height)
73 pm_error("Image is wrong height for ST-4 SBIG: %u pixels. "
74 "Must be %u", inpam.height, st4Height);
75
76 if (inpam.width != st4Width)
77 pm_error("Image is wrong width for ST-4 SBIG: %u pixels. "
78 "Must be %u", inpam.width, st4Width);
79
80 /* Really, we should just scale to maxval 255. There are library routines
81 for that, but we're too lazy even for that, since nobody is really
82 going to use this program.
83 */
84 if (inpam.maxval != st4Maxval)
85 pm_error("Image is wrong maxval for ST-4 SBIG: %u. "
86 "Must be %u", (unsigned)inpam.maxval, st4Maxval);
87
88 tuplerow = pnm_allocpamrow(&inpam);
89
90 for (row = 0; row < inpam.height; ++row) {
91 unsigned int col;
92
93 pnm_readpamrow(&inpam, tuplerow);
94
95 for (col = 0; col < inpam.width; ++col)
96 pm_writechar(stdout, (char)tuplerow[col][0]);
97 }
98
99 writeSt4Footer(stdout);
100
101 pm_close(ifP);
102
103 return 0;
104 }
105
106
107