Debian Codegraph @

converter/other/sunicontopnm.c

raw
1 /* icontopbm.c - read a Sun icon file and produce a Netbpbm 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 /*
14 Most icon images are monochrome: Depth=1
15
16 Depth=8 images are extremely rare. At least some of these are color
17 images but we haven't found information on the palette color order.
18 Output will be in pgm. Convert to ppm with pgmtoppm or pamlookup
19 if necessary. It is up to the user to provide the color palette in
20 a form acceptable by the above conversion utilities.
21 */
22
23 #include <assert.h>
24 #include <string.h>
25
26 #include "nstring.h"
27 #include "pbm.h"
28 #include "pgm.h"
29
30
31
32 static void
33 ReadIconFileHeader(FILE * const file,
34 int * const widthP,
35 int * const heightP,
36 int * const depthP,
37 int * const bitsPerItemP) {
38
39 unsigned int fieldCt;
40
41 fieldCt = 0;
42 *widthP = *heightP = -1;
43
44 for ( ; ; ) {
45 char variable[80+1];
46 int ch;
47 unsigned int i;
48 int value;
49
50 while ((ch = getc(file)) == ',' || ch == '\n' || ch == '\t' ||
51 ch == ' ')
52 ;
53 for (i = 0;
54 ch != '=' && ch != ',' && ch != '\n' && ch != '\t' &&
55 ch != ' ' && (i < (sizeof(variable) - 1));
56 ++i) {
57 variable[i] = ch;
58 if ((ch = getc( file )) == EOF)
59 pm_error( "invalid input file -- premature EOF" );
60 }
61 variable[i] = '\0';
62
63 if (streq(variable, "*/") && fieldCt > 0)
64 break;
65
66 if (fscanf( file, "%d", &value ) != 1)
67 continue;
68
69 if (streq( variable, "Width")) {
70 *widthP = value;
71 ++fieldCt;
72 } else if (streq( variable, "Height")) {
73 *heightP = value;
74 ++fieldCt;
75 } else if (streq( variable, "Depth")) {
76 if (value != 1 && value != 8)
77 pm_error("invalid depth");
78 *depthP = value;
79 ++fieldCt;
80 } else if (streq(variable, "Format_version")) {
81 if (value != 1)
82 pm_error("invalid Format_version");
83 ++fieldCt;
84 } else if (streq(variable, "Valid_bits_per_item")) {
85 if (value != 16 && value !=32)
86 pm_error("invalid Valid_bits_per_item");
87 *bitsPerItemP = value;
88 ++fieldCt;
89 }
90 }
91
92 if (fieldCt < 5)
93 pm_error("invalid sun icon file header: "
94 "only %u out of required 5 fields present", fieldCt);
95
96 if (*widthP <= 0)
97 pm_error("invalid width (must be positive): %d", *widthP);
98 else if (*widthP % 8 > 0)
99 pm_message("warning: width not a multiple of 8: %d", *widthP);
100 /* We don't know whether widths which are not a multiple of 8
101 are allowed. The program must gracefully handle this case
102 because sun icon files are easy to edit by hand.
103 */
104 if (*heightP <= 0)
105 pm_error("invalid height (must be positive): %d", *heightP);
106
107 }
108
109
110
111 int
112 main(int argc, const char ** argv) {
113
114 FILE * ifP;
115 bit * bitrow;
116 gray * grayrow;
117 int rows, cols, depth, row, format, maxval, colChars, bitsPerItem;
118
119 pm_proginit(&argc, argv);
120
121 if (argc-1 > 1)
122 pm_error("Too many arguments (%u). Program takes at most one: "
123 "name of input file", argc-1);
124
125 if (argc-1 == 1)
126 ifP = pm_openr(argv[1]);
127 else
128 ifP = stdin;
129
130 ReadIconFileHeader(ifP, &cols, &rows, &depth, &bitsPerItem);
131
132 if (depth == 1) {
133 format = PBM_TYPE;
134 maxval = 1;
135 pbm_writepbminit(stdout, cols, rows, 0);
136 bitrow = pbm_allocrow_packed(cols);
137 colChars = pbm_packed_bytes(cols);
138 } else {
139 assert(depth == 8);
140 format = PGM_TYPE;
141 maxval = 255;
142 pgm_writepgminit(stdout, cols, rows, maxval, 0);
143 grayrow = pgm_allocrow(cols);
144 colChars = cols;
145 }
146
147 for (row = 0; row < rows; ++row) {
148 unsigned int colChar;
149 for (colChar = 0; colChar < colChars; ++colChar) {
150 unsigned int data;
151 int status;
152
153 /* read 8 bits */
154 if (row==0 && colChar == 0)
155 status = fscanf(ifP, " 0x%2x", &data);
156 else if (colChar % (bitsPerItem/8) == 0)
157 status = fscanf(ifP, ", 0x%2x", &data);
158 else
159 status = fscanf(ifP, "%2x", &data);
160
161 /* write 8 bits */
162 if (status == 1) {
163 if (format == PBM_TYPE)
164 bitrow[colChar] = data;
165 else
166 grayrow[colChar] = data;
167 } else
168 pm_error("error scanning bits item %u", colChar);
169 }
170
171 /* output row */
172 if (format == PBM_TYPE)
173 pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
174 else
175 pgm_writepgmrow(stdout, grayrow, cols, maxval, 0);
176 }
177
178 if (format == PBM_TYPE)
179 pbm_freerow_packed(bitrow);
180 else
181 pgm_freerow(grayrow);
182
183 pm_close(ifP);
184 pm_close(stdout);
185 return 0;
186 }
187
188
189