| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
|
|
static char SCCSid[] = "$SunId$ AU"; |
| 3 |
|
|
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
rad2pict - Convert an Radiance image to APPLE pict format. |
| 6 |
|
|
|
| 7 |
|
|
School of Architecture, Auckland University, Private Bag |
| 8 |
|
|
Auckland, New Zealand |
| 9 |
|
|
*/ |
| 10 |
|
|
#include <stdio.h> |
| 11 |
|
|
#include <stdlib.h> |
| 12 |
|
|
#include "pict.h" |
| 13 |
|
|
#include "color.h" |
| 14 |
|
|
|
| 15 |
|
|
char cbuf[8192*5]; |
| 16 |
|
|
char pbuf[8192]; |
| 17 |
|
|
int outbytes; |
| 18 |
|
|
FILE *outf, *inf; |
| 19 |
|
|
char **gargv; |
| 20 |
|
|
|
| 21 |
|
|
putpic(int, int); |
| 22 |
|
|
|
| 23 |
|
|
putrect(xorg,yorg,xsize,ysize) |
| 24 |
|
|
int xorg, yorg, xsize, ysize; |
| 25 |
|
|
{ |
| 26 |
|
|
putashort(yorg); |
| 27 |
|
|
putashort(xorg); |
| 28 |
|
|
putashort(ysize); |
| 29 |
|
|
putashort(xsize); |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
|
putfprect(xorg,yorg,xsize,ysize) |
| 33 |
|
|
int xorg, yorg, xsize, ysize; |
| 34 |
|
|
{ |
| 35 |
|
|
putalong(yorg<<16); |
| 36 |
|
|
putalong(xorg<<16); |
| 37 |
|
|
putalong(ysize<<16); |
| 38 |
|
|
putalong(xsize<<16); |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
putalong(l) |
| 42 |
|
|
long l; |
| 43 |
|
|
{ |
| 44 |
|
|
putbyte((l>>24)&0xff); |
| 45 |
|
|
putbyte((l>>16)&0xff); |
| 46 |
|
|
putbyte((l>>8)&0xff); |
| 47 |
|
|
putbyte((l>>0)&0xff); |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
|
|
putashort(s) |
| 51 |
|
|
short s; |
| 52 |
|
|
{ |
| 53 |
|
|
putbyte((s>>8)&0xff); |
| 54 |
|
|
putbyte((s>>0)&0xff); |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
putbyte(b) |
| 58 |
|
|
unsigned char b; |
| 59 |
|
|
{ |
| 60 |
|
|
char c[1]; |
| 61 |
|
|
|
| 62 |
|
|
c[0] = b; |
| 63 |
|
|
if(!fwrite(c,1,1,outf)) { |
| 64 |
|
|
fprintf(stderr,"%s: error on write\n", gargv[0]); |
| 65 |
|
|
exit(1); |
| 66 |
|
|
} |
| 67 |
|
|
outbytes++; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
putbytes(buf,n) |
| 71 |
|
|
unsigned char *buf; |
| 72 |
|
|
int n; |
| 73 |
|
|
{ |
| 74 |
|
|
if(!fwrite(buf,n,1,outf)) { |
| 75 |
|
|
fprintf(stderr,"topict: error on write\n"); |
| 76 |
|
|
exit(1); |
| 77 |
|
|
} |
| 78 |
|
|
outbytes+=n; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
main(argc,argv) |
| 82 |
|
|
int argc; |
| 83 |
|
|
char **argv; |
| 84 |
|
|
{ |
| 85 |
|
|
int xsize, ysize; |
| 86 |
|
|
int i, picsize; |
| 87 |
|
|
int ssizepos, lsizepos; |
| 88 |
|
|
|
| 89 |
|
|
gargv = argv; |
| 90 |
|
|
|
| 91 |
|
|
if( argc<3 ) { |
| 92 |
|
|
fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]); |
| 93 |
|
|
exit(1); |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
if( (inf=fopen(gargv[1],"rb")) == NULL ) { |
| 97 |
|
|
fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]); |
| 98 |
|
|
exit(1); |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
|
|
if( (outf=fopen(gargv[2],"wb")) == NULL ) { |
| 102 |
|
|
fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]); |
| 103 |
|
|
exit(1); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
if (checkheader(inf, COLRFMT, NULL) < 0 || |
| 107 |
|
|
fgetresolu(&xsize, &ysize, inf) != (YMAJOR|YDECR)) { |
| 108 |
|
|
fprintf(stderr, "%s: not a radiance picture\n", argv[1]); |
| 109 |
|
|
exit(1); |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
|
setcolrgam(2.0); |
| 113 |
|
|
|
| 114 |
|
|
for(i=0; i<HEADER_SIZE; i++) |
| 115 |
|
|
putbyte(0); |
| 116 |
|
|
ssizepos = outbytes; |
| 117 |
|
|
putashort(0); /* low 16 bits of file size less HEADER_SIZE */ |
| 118 |
|
|
putrect(0,0,xsize,ysize); /* bounding box of picture */ |
| 119 |
|
|
putashort(PICT_picVersion); |
| 120 |
|
|
putashort(0x02ff); /* version 2 pict */ |
| 121 |
|
|
putashort(PICT_reservedHeader); /* reserved header opcode */ |
| 122 |
|
|
|
| 123 |
|
|
lsizepos = outbytes; |
| 124 |
|
|
putalong(0); /* full size of the file */ |
| 125 |
|
|
putfprect(0,0,xsize,ysize); /* fixed point bounding box of picture */ |
| 126 |
|
|
putalong(0); /* reserved */ |
| 127 |
|
|
|
| 128 |
|
|
putashort(PICT_clipRgn); /* the clip region */ |
| 129 |
|
|
putashort(10); |
| 130 |
|
|
putrect(0,0,xsize,ysize); |
| 131 |
|
|
|
| 132 |
|
|
putpict(xsize, ysize); |
| 133 |
|
|
|
| 134 |
|
|
putashort(PICT_EndOfPicture); /* end of pict */ |
| 135 |
|
|
|
| 136 |
|
|
picsize = outbytes-HEADER_SIZE; |
| 137 |
|
|
fseek(outf,ssizepos,0); |
| 138 |
|
|
putashort(picsize&0xffff); |
| 139 |
|
|
fseek(outf,lsizepos,0); |
| 140 |
|
|
putalong(picsize); |
| 141 |
|
|
|
| 142 |
|
|
fclose(outf); |
| 143 |
|
|
exit(0); |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
|
putpict(xsize, ysize) |
| 147 |
|
|
int xsize; |
| 148 |
|
|
int ysize; |
| 149 |
|
|
{ |
| 150 |
|
|
int y; |
| 151 |
|
|
int nbytes, rowbytes; |
| 152 |
|
|
|
| 153 |
|
|
putashort(PICT_Pack32BitsRect); /* 32 bit rgb */ |
| 154 |
|
|
rowbytes = 4*xsize; |
| 155 |
|
|
putalong(0x000000ff); /* base address */ |
| 156 |
|
|
|
| 157 |
|
|
if(rowbytes&1) |
| 158 |
|
|
rowbytes++; |
| 159 |
|
|
putashort(rowbytes|0x8000); /* rowbytes */ |
| 160 |
|
|
putrect(0,0,xsize,ysize); /* bounds */ |
| 161 |
|
|
putashort(0); /* version */ |
| 162 |
|
|
|
| 163 |
|
|
putashort(4); /* packtype */ |
| 164 |
|
|
putalong(0); /* packsize */ |
| 165 |
|
|
putalong(72<<16); /* hres */ |
| 166 |
|
|
putalong(72<<16); /* vres */ |
| 167 |
|
|
|
| 168 |
|
|
putashort(16); /* pixeltype */ |
| 169 |
|
|
putashort(32); /* pixelsize */ |
| 170 |
|
|
putashort(3); /* cmpcount */ |
| 171 |
|
|
|
| 172 |
|
|
putashort(8); /* cmpsize */ |
| 173 |
|
|
putalong(0); /* planebytes */ |
| 174 |
|
|
putalong(0); /* pmtable */ |
| 175 |
|
|
putalong(0); /* pmreserved */ |
| 176 |
|
|
|
| 177 |
|
|
putrect(0,0,xsize,ysize); /* scr rect */ |
| 178 |
|
|
putrect(0,0,xsize,ysize); /* dest rect */ |
| 179 |
|
|
|
| 180 |
|
|
putashort(0x40); /* transfer mode */ |
| 181 |
|
|
for(y=0; y<ysize; y++) { |
| 182 |
|
|
getrow(inf, cbuf, xsize); |
| 183 |
|
|
|
| 184 |
|
|
nbytes = packbits(cbuf,pbuf,24*xsize); |
| 185 |
|
|
if(rowbytes>250) |
| 186 |
|
|
putashort(nbytes); |
| 187 |
|
|
else |
| 188 |
|
|
putbyte(nbytes); |
| 189 |
|
|
putbytes(pbuf,nbytes); |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
|
|
if(outbytes&1) |
| 193 |
|
|
putbyte(0); |
| 194 |
|
|
} |
| 195 |
|
|
|
| 196 |
|
|
int getrow(FILE *in, char *mybuff, int xsize) |
| 197 |
|
|
{ |
| 198 |
|
|
COLOR color; |
| 199 |
|
|
COLR *scanin = (COLR*) malloc(xsize * sizeof(COLR)); |
| 200 |
|
|
int x; |
| 201 |
|
|
|
| 202 |
|
|
if (scanin == NULL) { |
| 203 |
|
|
printf("scanin null"); |
| 204 |
|
|
} |
| 205 |
|
|
|
| 206 |
|
|
|
| 207 |
|
|
if (freadcolrs(scanin, xsize, in) < 0) { |
| 208 |
|
|
fprintf(stderr, " read error\n"); |
| 209 |
|
|
exit(1); |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
|
|
colrs_gambs(scanin, xsize); |
| 213 |
|
|
|
| 214 |
|
|
for (x = 0; x < xsize; x++) { |
| 215 |
|
|
colr_color(color, scanin[x]); |
| 216 |
|
|
cbuf[xsize * 0 + x] = color[RED] * 255; |
| 217 |
|
|
cbuf[xsize * 1 + x] = color[GRN] * 255; |
| 218 |
|
|
cbuf[xsize * 2 + x] = color[BLU] * 255; |
| 219 |
|
|
} |
| 220 |
|
|
free(scanin); |
| 221 |
|
|
} |
| 222 |
|
|
|
| 223 |
|
|
packbits(ibits,pbits,nbits) |
| 224 |
|
|
unsigned char *ibits, *pbits; |
| 225 |
|
|
int nbits; |
| 226 |
|
|
{ |
| 227 |
|
|
int bytes; |
| 228 |
|
|
unsigned char *sptr; |
| 229 |
|
|
unsigned char *ibitsend; |
| 230 |
|
|
unsigned char *optr = pbits; |
| 231 |
|
|
int nbytes, todo, cc, count; |
| 232 |
|
|
|
| 233 |
|
|
nbytes = ((nbits-1)/8)+1; |
| 234 |
|
|
ibitsend = ibits+nbytes; |
| 235 |
|
|
while (ibits<ibitsend) { |
| 236 |
|
|
sptr = ibits; |
| 237 |
|
|
ibits += 2; |
| 238 |
|
|
while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0]))) |
| 239 |
|
|
ibits++; |
| 240 |
|
|
if(ibits != ibitsend) { |
| 241 |
|
|
ibits -= 2; |
| 242 |
|
|
} |
| 243 |
|
|
count = ibits-sptr; |
| 244 |
|
|
while(count) { |
| 245 |
|
|
todo = count>127 ? 127:count; |
| 246 |
|
|
count -= todo; |
| 247 |
|
|
*optr++ = todo-1; |
| 248 |
|
|
while(todo--) |
| 249 |
|
|
*optr++ = *sptr++; |
| 250 |
|
|
} |
| 251 |
|
|
if(ibits == ibitsend) |
| 252 |
|
|
break; |
| 253 |
|
|
sptr = ibits; |
| 254 |
|
|
cc = *ibits++; |
| 255 |
|
|
while( (ibits<ibitsend) && (*ibits == cc) ) |
| 256 |
|
|
ibits++; |
| 257 |
|
|
count = ibits-sptr; |
| 258 |
|
|
while(count) { |
| 259 |
|
|
todo = count>128 ? 128:count; |
| 260 |
|
|
count -= todo; |
| 261 |
|
|
*optr++ = 257-todo; |
| 262 |
|
|
*optr++ = cc; |
| 263 |
|
|
} |
| 264 |
|
|
} |
| 265 |
|
|
return optr-pbits; |
| 266 |
|
|
} |