Debian Codegraph @

converter/pbm/pbmtosunicon.c

raw
1 /* pbmtosunicon.c - read a PBM image and produce a Sun icon file
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 /* 2006.10 (afu)
14 Changed bitrow from plain to raw, read function from pbm_readpbmrow() to
15 pbm_readpbmrow_packed. Applied wordint to scoop up 16 bit output items.
16 putitem changed to better express the output format.
17 Retired bitwise transformation functions.
18 */
19
20 #include "pm_config.h"
21 #include "pbm.h"
22
23 static struct ItemPutter {
24 unsigned short int itemBuff[8];
25 unsigned int itemCnt; /* takes values 0 to 8 */
26 FILE * putFp;
27 } ip;
28
29
30
31 static void
32 putinit(FILE * const ofP) {
33 ip.putFp = ofP;
34 ip.itemCnt = 0;
35 }
36
37
38
39 static void
40 putitem(uint16_t const item) {
41
42 if (ip.itemCnt == 8 ) {
43 /* Buffer is full. Write out one line. */
44 int rc;
45
46 rc = fprintf(ip.putFp,
47 "\t0x%04x,0x%04x,0x%04x,0x%04x,"
48 "0x%04x,0x%04x,0x%04x,0x%04x,\n",
49 ip.itemBuff[0], ip.itemBuff[1],
50 ip.itemBuff[2], ip.itemBuff[3],
51 ip.itemBuff[4], ip.itemBuff[5],
52 ip.itemBuff[6], ip.itemBuff[7]);
53 if (rc < 0)
54 pm_error("fprintf() failed to write Icon bitmap");
55
56 ip.itemCnt = 0;
57 }
58 ip.itemBuff[ip.itemCnt++] = item & 0xffff;
59 /* Only lower 16 bits are used */
60 }
61
62
63
64 static void
65 putterm(void) {
66
67 unsigned int i;
68
69 for (i = 0; i < ip.itemCnt; ++i) {
70 int rc;
71 rc = fprintf(ip.putFp, "%s0x%04x%c", i == 0 ? "\t" : "",
72 ip.itemBuff[i],
73 i == ip.itemCnt - 1 ? '\n' : ',');
74 if (rc < 0)
75 pm_error("fprintf() failed to write Icon bitmap");
76 }
77 }
78
79
80
81 static void
82 writeIconHeader(FILE * const ofP,
83 unsigned int const width,
84 unsigned int const height) {
85
86 int rc;
87
88 rc = fprintf(ofP,
89 "/* Format_version=1, Width=%u, Height=%u", width, height);
90 if (rc < 0)
91 pm_error("fprintf() failed to write Icon header");
92
93 rc = fprintf(ofP, ", Depth=1, Valid_bits_per_item=16\n */\n");
94 if (rc < 0)
95 pm_error("fprintf() failed to write Icon header");
96 }
97
98
99
100 static void
101 writeIcon(FILE * const ifP,
102 unsigned int const cols,
103 unsigned int const rows,
104 int const format,
105 FILE * const ofP) {
106
107 unsigned int const items = (cols + 15) / 16;
108 unsigned int const pad = items * 16 - cols;
109
110 unsigned char * const bitrow = pbm_allocrow_packed(items * 16);
111 unsigned int row;
112
113 bitrow[0] = bitrow[items * 2 - 1] = 0;
114
115 writeIconHeader(ofP, cols + pad, rows);
116
117 putinit(ofP);
118
119 for (row = 0; row < rows; ++row) {
120 unsigned int itemSeq;
121
122 pbm_readpbmrow_bitoffset(ifP, bitrow, cols, format, pad/2);
123
124 for (itemSeq = 0; itemSeq < items; ++itemSeq) {
125 /* Read bits from bitrow, send to format & print function. */
126
127 putitem((bitrow[itemSeq*2]<<8) + bitrow[itemSeq*2+1]);
128 }
129 }
130 putterm();
131 pbm_freerow_packed(bitrow);
132 }
133
134
135
136 int
137 main(int argc,
138 char * argv[]) {
139
140 FILE * ifP;
141 int rows, cols;
142 int format;
143 const char * inputFileName;
144
145 pbm_init(&argc, argv);
146
147 if (argc-1 > 1)
148 pm_error("Too many arguments (%u). "
149 "Only argument is optional input file", argc-1);
150 if (argc-1 == 1)
151 inputFileName = argv[1];
152 else
153 inputFileName = "-";
154
155 ifP = pm_openr(inputFileName);
156
157 pbm_readpbminit(ifP, &cols, &rows, &format);
158
159 writeIcon(ifP, cols, rows, format, stdout);
160
161 pm_close(ifP);
162
163 return 0;
164 }
165
166
167