ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pict.c
Revision: 2.3
Committed: Wed Nov 27 09:14:03 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +3 -1 lines
Log Message:
removed reference to stdlib.h

File Contents

# Content
1 #ifndef lint
2 static char SCCSid[] = "$SunId$ Auckuni";
3 #endif
4
5 /*
6 * rad2pict -
7 * Convert an Radiance image to APPLE pict format.
8 *
9 * Orginally Iris to PICT by Paul Haeberli - 1990
10 * Hacked into Rad to PICT by Russell Street 1990
11 *
12 * History:
13 * V 1 -- Does basic conversion
14 * V 1.1 (2/11/91) -- Added options for Gamma
15 * -- verbose option
16 * -- man page
17 * -- better about allocating buffers
18 * V 1.2 (19/11/91) -- Revised to handle opening "The Radiance Way"
19 * -- Added exposure level adjustment
20 */
21
22 #include <stdio.h>
23
24 #include "pict.h"
25 #include "color.h"
26 #include "resolu.h"
27
28 extern double atof();
29 extern char *malloc();
30
31 int outbytes; /* This had better be 32 bits! */
32 char *progname;
33 int verbose = 0;
34 float gamma = 2.0;
35 int bradj = 0;
36
37 /* First some utility routines */
38
39 putrect(xorg,yorg,xsize,ysize)
40 int xorg, yorg, xsize, ysize;
41 {
42 putashort(yorg);
43 putashort(xorg);
44 putashort(ysize);
45 putashort(xsize);
46 }
47
48 putfprect(xorg,yorg,xsize,ysize)
49 int xorg, yorg, xsize, ysize;
50 {
51 putalong(yorg<<16);
52 putalong(xorg<<16);
53 putalong(ysize<<16);
54 putalong(xsize<<16);
55 }
56
57 putalong(l)
58 long l;
59 {
60 putbyte((l>>24)&0xff);
61 putbyte((l>>16)&0xff);
62 putbyte((l>>8)&0xff);
63 putbyte((l>>0)&0xff);
64 }
65
66 putashort(s)
67 short s;
68 {
69 putbyte((s>>8)&0xff);
70 putbyte((s>>0)&0xff);
71 }
72
73 putbyte(b)
74 int b;
75 {
76 if (putc(b,stdout) == EOF && ferror(stdout)) {
77 fprintf(stderr,"%s: error on write\n", progname);
78 exit(1);
79 }
80 outbytes++;
81 }
82
83 putbytes(buf,n)
84 unsigned char *buf;
85 int n;
86 {
87 if(!fwrite(buf,n,1,stdout)) {
88 fprintf(stderr,"%s: error on write\n", progname);
89 exit(1);
90 }
91 outbytes+=n;
92 }
93
94 main(argc,argv)
95 int argc;
96 char **argv;
97 {
98 int xsize, ysize;
99 int i, picsize;
100 int ssizepos, lsizepos;
101
102 progname = argv[0];
103
104 for (i = 1; i < argc ; i++)
105 if (argv[i][0] == '-')
106 switch (argv[i][1]) {
107 case 'g': gamma = atof(argv[++i]);
108 break;
109
110 case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
111 usage();
112 else
113 bradj = atoi(argv[++i]);
114 break;
115
116 case 'v': ;
117 verbose = 1;
118 break;
119
120 case 'r': fprintf(stderr, "Sorry. Get a Macintosh :-)\n");
121 exit(1);
122
123 case '-': i++;
124 goto outofparse;
125 break; /* NOTREACHED */
126
127 otherwise: usage();
128 break;
129 }
130 else
131 break;
132
133 outofparse:
134
135 if (i < argc - 2)
136 usage();
137
138 if (i <= argc - 1 && freopen(argv[i], "r", stdin) == NULL) {
139 fprintf(stderr, "%s: can not open input \"%s\"\n",
140 progname, argv[i]);
141 exit(1);
142 }
143
144 if (i <= argc - 2 && freopen(argv[i+1], "w", stdout) == NULL) {
145 fprintf(stderr, "%s: can not open input \"%s\"\n",
146 progname, argv[i+1]);
147 exit(1);
148 }
149
150 #ifdef DEBUG
151 fprintf(stderr, "Input file: %s\n", i <= argc - 1 ? argv[i] : "stdin");
152 fprintf(stderr, "Outut file: %s\n", i <= argc - 2 ? argv[i+1] : "stdout" );
153 fprintf(stderr, "Gamma: %f\n", gamma);
154 fprintf(stderr, "Brightness adjust: %d\n", bradj);
155 fprintf(stderr, "Verbose: %s\n", verbose ? "on" : "off");
156 #endif
157
158
159 /* OK. Now we read the size of the Radiance picture */
160 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
161 fgetresolu(&xsize, &ysize, stdin) < 0 /* != (YMAJOR|YDECR) */ ) {
162 fprintf(stderr, "%s: not a radiance picture\n", progname);
163 exit(1);
164 }
165
166 /* Set the gamma correction */
167
168 setcolrgam(gamma);
169
170 for(i=0; i<HEADER_SIZE; i++)
171 putbyte(0);
172
173 ssizepos = outbytes;
174 putashort(0); /* low 16 bits of file size less HEADER_SIZE */
175 putrect(0,0,xsize,ysize); /* bounding box of picture */
176 putashort(PICT_picVersion);
177 putashort(0x02ff); /* version 2 pict */
178 putashort(PICT_reservedHeader); /* reserved header opcode */
179
180 lsizepos = outbytes;
181 putalong(0); /* full size of the file */
182 putfprect(0,0,xsize,ysize); /* fixed point bounding box of picture */
183 putalong(0); /* reserved */
184
185 putashort(PICT_clipRgn); /* the clip region */
186 putashort(10);
187 putrect(0,0,xsize,ysize);
188
189 if (verbose)
190 fprintf(stderr, "%s: The picture is %d by %d, with a gamma of %f\n",
191 progname, xsize, ysize, gamma);
192
193
194 putpict(xsize, ysize); /* Here is where all the work is done */
195
196 putashort(PICT_EndOfPicture); /* end of pict */
197
198 picsize = outbytes-HEADER_SIZE;
199 fseek(stdout,ssizepos,0);
200 putashort(picsize&0xffff);
201 fseek(stdout,lsizepos,0);
202 putalong(picsize);
203
204 fclose(stdout);
205 fclose(stdin);
206
207 exit(0);
208 return 0; /* lint fodder */
209 }
210
211 putpict(xsize, ysize)
212 int xsize;
213 int ysize;
214 {
215 int y;
216 int nbytes, rowbytes;
217 char *cbuf, *pbuf;
218
219 cbuf = malloc(4 * xsize);
220
221 if (cbuf == NULL) {
222 fprintf(stderr, "%s: not enough memory\n", progname);
223 exit(1);
224 }
225
226 pbuf = malloc(4 * xsize);
227
228 if (pbuf == NULL) {
229 fprintf(stderr, "%s: not enough memory\n", progname);
230 exit(1);
231 }
232
233 putashort(PICT_Pack32BitsRect); /* 32 bit rgb */
234 rowbytes = 4*xsize;
235 putalong(0x000000ff); /* base address */
236
237
238 if(rowbytes&1)
239 rowbytes++;
240 putashort(rowbytes|0x8000); /* rowbytes */
241 putrect(0,0,xsize,ysize); /* bounds */
242 putashort(0); /* version */
243
244 putashort(4); /* packtype */
245 putalong(0); /* packsize */
246 putalong(72<<16); /* hres */
247 putalong(72<<16); /* vres */
248
249 putashort(16); /* pixeltype */
250 putashort(32); /* pixelsize */
251 putashort(3); /* cmpcount */
252
253
254 putashort(8); /* cmpsize */
255 putalong(0); /* planebytes */
256 putalong(0); /* pmtable */
257 putalong(0); /* pmreserved */
258
259
260 putrect(0,0,xsize,ysize); /* scr rect */
261 putrect(0,0,xsize,ysize); /* dest rect */
262
263
264 putashort(0x40); /* transfer mode */
265
266 for(y=0; y<ysize; y++) {
267 getrow(stdin, cbuf, xsize);
268
269 nbytes = packbits(cbuf,pbuf,24*xsize);
270 if(rowbytes>250)
271 putashort(nbytes);
272 else
273 putbyte(nbytes);
274 putbytes(pbuf,nbytes);
275 }
276
277 if(outbytes&1)
278 putbyte(0);
279
280 free(cbuf);
281 free(pbuf);
282 }
283
284 int getrow(in, cbuf, xsize)
285 FILE *in;
286 char *cbuf;
287 int xsize;
288 {
289 extern char *tempbuffer(); /* defined in color.c */
290 COLR *scanin = NULL;
291 int x;
292
293 if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) {
294 fprintf(stderr, "%s: not enough memory\n", progname);
295 exit(1);
296 }
297
298 if (freadcolrs(scanin, xsize, in) < 0) {
299 fprintf(stderr, "%s: read error\n", progname);
300 exit(1);
301 }
302
303 if (bradj) /* Adjust exposure level */
304 shiftcolrs(scanin, xsize, bradj);
305
306
307 colrs_gambs(scanin, xsize); /* Gamma correct it */
308
309 for (x = 0; x < xsize; x++) {
310 cbuf[x] = scanin[x][RED];
311 cbuf[xsize + x] = scanin[x][GRN];
312 cbuf[2*xsize + x] = scanin[x][BLU];
313 }
314
315 }
316
317
318 packbits(ibits,pbits,nbits)
319 unsigned char *ibits, *pbits;
320 int nbits;
321 {
322 int bytes; /* UNUSED */
323 unsigned char *sptr;
324 unsigned char *ibitsend;
325 unsigned char *optr = pbits;
326 int nbytes, todo, cc, count;
327
328 nbytes = ((nbits-1)/8)+1;
329 ibitsend = ibits+nbytes;
330 while(ibits<ibitsend) {
331 sptr = ibits;
332 ibits += 2;
333 while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0])))
334 ibits++;
335 if(ibits != ibitsend) {
336 ibits -= 2;
337 }
338 count = ibits-sptr;
339 while(count) {
340 todo = count>127 ? 127:count;
341 count -= todo;
342 *optr++ = todo-1;
343 while(todo--)
344 *optr++ = *sptr++;
345 }
346 if(ibits == ibitsend)
347 break;
348 sptr = ibits;
349 cc = *ibits++;
350 while( (ibits<ibitsend) && (*ibits == cc) )
351 ibits++;
352 count = ibits-sptr;
353 while(count) {
354 todo = count>128 ? 128:count;
355 count -= todo;
356 *optr++ = 257-todo;
357 *optr++ = cc;
358 }
359 }
360 return optr-pbits;
361 }
362
363 usage()
364 {
365 fprintf(stderr, "Usage: %s [-v] [-g gamma] [infile [outfile]]\n",
366 progname);
367 exit(2);
368 }