ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pict.c
Revision: 2.9
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.8: +5 -10 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

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