Debian Codegraph @

converter/pbm/mgrtopbm.c

raw
1 /* mgrtopbm.c - read a MGR bitmap and produce a PBM image.
2
3 Copyright information is at end of file.
4
5 You can find MGR and some MGR format test images at
6 ftp://sunsite.unc.edu/pub/Linux/apps/MGR/!INDEX.html
7
8 */
9
10 #include <string.h>
11 #include <errno.h>
12
13 #include "pbm.h"
14 #include "mgr.h"
15
16
17
18 static void
19 interpHdrWidth(struct b_header const head,
20 unsigned int * const colsP) {
21
22 if (head.h_wide < ' ' || head.l_wide < ' ')
23 pm_error("Invalid width field in MGR header");
24 else {
25 unsigned int const maxDimension = 4095;
26
27 unsigned int const cols =
28 (((int)head.h_wide - ' ') << 6) + ((int)head.l_wide - ' ');
29
30 if (cols == 0 || cols > maxDimension)
31 pm_error("Invalid width value (%u) in MGR header", cols);
32 else
33 *colsP = cols;
34 }
35 }
36
37
38
39 static void
40 interpHdrHeight(struct b_header const head,
41 unsigned int * const rowsP) {
42
43 if (head.h_high < ' ' || head.l_high < ' ')
44 pm_error("Invalid height field in MGR header");
45 else {
46 unsigned int const maxDimension = 4095;
47
48 unsigned int const rows =
49 (((int)head.h_high - ' ') << 6) + ((int)head.l_high - ' ');
50
51 if (rows == 0 || rows > maxDimension)
52 pm_error("Invalid height value (%u) in MGR header", rows);
53 else
54 *rowsP = rows;
55 }
56 }
57
58
59
60 static void
61 readMgrHeader(FILE * const ifP,
62 unsigned int * const colsP,
63 unsigned int * const rowsP,
64 unsigned int * const depthP,
65 unsigned int * const padrightP ) {
66
67 struct b_header head;
68 unsigned int pad;
69 size_t bytesRead;
70
71 bytesRead = fread(&head, sizeof(struct old_b_header), 1, ifP);
72 if (bytesRead != 1)
73 pm_error("Unable to read 1st byte of file. "
74 "fread() returns errno %d (%s)",
75 errno, strerror(errno));
76 if (head.magic[0] == 'y' && head.magic[1] == 'z') {
77 /* new style bitmap */
78 size_t bytesRead;
79 bytesRead = fread(&head.depth,
80 sizeof(head) - sizeof(struct old_b_header), 1, ifP);
81 if (bytesRead != 1 )
82 pm_error("Unable to read header after 1st byte. "
83 "fread() returns errno %d (%s)",
84 errno, strerror(errno));
85 *depthP = (int) head.depth - ' ';
86 pad = 8;
87 } else if (head.magic[0] == 'x' && head.magic[1] == 'z') {
88 /* old style bitmap with 32-bit padding */
89 *depthP = 1;
90 pad = 32;
91 } else if (head.magic[0] == 'z' && head.magic[1] == 'z') {
92 /* old style bitmap with 16-bit padding */
93 *depthP = 1;
94 pad = 16;
95 } else if (head.magic[0] == 'z' && head.magic[1] == 'y') {
96 /* old style 8-bit pixmap with 16-bit padding */
97 *depthP = 8;
98 pad = 16;
99 } else {
100 pm_error("bad magic chars in MGR file: '%c%c'",
101 head.magic[0], head.magic[1] );
102 pad = 0; /* should never reach here */
103 }
104
105 interpHdrWidth (head, colsP);
106 interpHdrHeight(head, rowsP);
107
108 *padrightP = ((*colsP + pad - 1) / pad) * pad - *colsP;
109 }
110
111
112
113 int
114 main(int argc,
115 char * argv[]) {
116
117 FILE * ifP;
118 unsigned char * bitrow;
119 unsigned int rows, cols, depth;
120 unsigned int padright;
121 unsigned int row;
122 unsigned int itemCount;
123 const char * inputFileName;
124
125 pbm_init(&argc, argv);
126
127 if (argc-1 > 1)
128 pm_error("Too many arguments (%u). "
129 "Only argument is optional input file", argc-1);
130 if (argc-1 == 1)
131 inputFileName = argv[1];
132 else
133 inputFileName = "-";
134
135 ifP = pm_openr(inputFileName);
136
137 readMgrHeader(ifP, &cols, &rows, &depth, &padright);
138 if (depth != 1)
139 pm_error("MGR file has depth of %u, must be 1", depth);
140
141 pbm_writepbminit(stdout, cols, rows, 0);
142
143 bitrow = pbm_allocrow_packed(cols + padright);
144
145 itemCount = (cols + padright ) / 8;
146
147 for (row = 0; row < rows; ++row) {
148 /* The raster formats are nearly identical.
149 MGR may have rows padded to 16 or 32 bit boundaries.
150 */
151 size_t bytesRead;
152 bytesRead = fread(bitrow, 1, itemCount, ifP);
153 if (bytesRead < itemCount)
154 pm_error("fread() failed to read mgr bitmap data");
155
156 pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
157 }
158 pm_close(ifP);
159 pm_close(stdout);
160 return 0;
161 }
162
163
164
165 /* 2006.10 (afu)
166 Changed bitrow from plain to raw, read function from getc() to fread(),
167 write function from pbm_writepbmrow() to pbm_writepbmrow_packed().
168 Retired bitwise transformation functions.
169
170 NOT tested for old-style format files. Only one zz file in mgrsrc-0.69 .
171
172 */
173
174
175 /*
176 ** Copyright (C) 1989 by Jef Poskanzer.
177 **
178 ** Permission to use, copy, modify, and distribute this software and its
179 ** documentation for any purpose and without fee is hereby granted, provided
180 ** that the above copyright notice appear in all copies and that both that
181 ** copyright notice and this permission notice appear in supporting
182 ** documentation. This software is provided "as is" without express or
183 ** implied warranty.
184 */