ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_pict.c
Revision: 2.8
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.7: +4 -8 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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