-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathOutput.cpp
450 lines (404 loc) · 12.9 KB
/
Output.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//BL_COPYRIGHT_NOTICE
//
// $Id: Output.cpp,v 1.15 1999-05-10 18:54:18 car Exp $
//
// ---------------------------------------------------------------
// Output.cpp
// ---------------------------------------------------------------
#include "Output.H"
#include <fcntl.h>
#include <unistd.h>
#ifdef BL_USE_NEW_HFILES
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstdio>
using std::hex;
using std::dec;
using std::ofstream;
#else
#include <fstream.h>
#include <iomanip.h>
#include <math.h>
#include <stdio.h>
#endif
IMAGE *iopen(char *file, unsigned int type, unsigned int dim,
unsigned int xsize, unsigned int ysize, unsigned int zsize);
int putrow(IMAGE *image, unsigned short *buffer, unsigned int y, unsigned int z);
void cvtshorts( unsigned short buffer[], long n);
int img_write(IMAGE *image, const void *buffer, long count);
int img_seek(IMAGE *image, unsigned int y, unsigned int z);
int img_optseek(IMAGE *image, unsigned long offset);
int iclose(IMAGE *image);
void cvtlongs(long buffer[], long n);
void cvtimage(long *buffer);
// -------------------------------------------------------------------
void WritePSFile(char *filename, XImage *image,
int imagesizehoriz, int imagesizevert,
const Array<XColor> &colorcells)
{
int i, j, index;
XColor color;
ofstream fout(filename);
fout << "%!PS-Adobe-2.0" << '\n';
fout << "%%BoundingBox: 0 0 " << imagesizehoriz-1 << " "
<< imagesizevert-1 << '\n';
fout << "gsave" << '\n';
fout << "/picstr " << (imagesizehoriz * 3) << " string def" << '\n';
fout << imagesizehoriz << " " << imagesizevert << " scale" << '\n';
fout << imagesizehoriz << " " << imagesizevert << " 8" << '\n';
fout << "[" << imagesizehoriz << " 0 0 -" << imagesizevert << " 0 "
<< imagesizevert << "]" << '\n';
fout << "{ currentfile picstr readhexstring pop }" << '\n';
fout << "false 3" << '\n';
fout << "colorimage"; // no << '\n';
fout << hex;
char *buf = new char[8*imagesizehoriz+1];
for(j = 0; j < imagesizevert; j++) {
int charindex = 0;
for(i = 0; i < imagesizehoriz; i++) {
//BL_ASSERT(charindex>8*imagesizehoriz+1);
index = (int) XGetPixel(image, i, j);
color = colorcells[index];
if(i % 10 == 0) {
sprintf(buf+charindex, "\n");
charindex++;
}
//fout << setw(2) << setfill('0') << (color.red >> 8);
//fout << setw(2) << setfill('0') << (color.green >> 8);
//fout << setw(2) << setfill('0') << (color.blue >> 8) << ' ';
sprintf(buf+charindex, "%02x%02x%02x ",
(color.red >> 8),
(color.green >> 8),
(color.blue >> 8));
charindex += 7;
}
fout << buf;
}
delete [] buf;
fout << "grestore" << '\n';
fout << "showpage" << '\n';
fout.close();
}
// -------------------------------------------------------------------
void WritePSPaletteFile(char *filename, XImage *image,
int imagesizehoriz, int imagesizevert,
const Array<XColor> &colorcells,
const Array<Real> &palValueList,
const aString &palNumFormat)
{
int i, j, index;
XColor color;
ofstream fout(filename);
fout << "%!PS-Adobe-2.0" << '\n';
fout << "%%BoundingBox: 0 0 " << imagesizehoriz-1 << " "
<< imagesizevert-1 << '\n';
fout << "gsave" << '\n';
fout << "/picstr " << (imagesizehoriz * 3) << " string def" << '\n';
fout << imagesizehoriz << " " << imagesizevert << " scale" << '\n';
fout << imagesizehoriz << " " << imagesizevert << " 8" << '\n';
fout << "[" << imagesizehoriz << " 0 0 -" << imagesizevert << " 0 "
<< imagesizevert << "]" << '\n';
fout << "{ currentfile picstr readhexstring pop }" << '\n';
fout << "false 3" << '\n';
fout << "colorimage"; // no << '\n';
fout << hex;
char *buf = new char[8*imagesizehoriz+1];
for(j = 0; j < imagesizevert; ++j) {
int charindex = 0;
for(i = 0; i < imagesizehoriz; ++i) {
index = (int) XGetPixel(image, i, j);
color = colorcells[index];
if(i % 10 == 0) {
sprintf(buf+charindex, "\n");
charindex++;
}
sprintf(buf+charindex, "%02x%02x%02x ",
(color.red >> 8),
(color.green >> 8),
(color.blue >> 8));
charindex += 7;
}
fout << buf;
}
fout << dec;
fout << "grestore" << '\n';
fout << "0 setgray" << '\n';
fout << "24 0" << '\n';
fout << "120 " << imagesizevert-1 << '\n';
fout << "rectfill" << '\n';
int paletteHeight(216);
int topOfPalette(256);
double pSpacing((double) paletteHeight / (double) (palValueList.length() - 1));
int palSpacing = int(ceil(pSpacing)) + 1;
fout << "/Palatino-Roman findfont" << '\n' << "20 scalefont"
<< '\n' << "setfont\n1 setgray" << '\n';
for(int j = 0; j < palValueList.length(); ++j) {
fout << "40 " << topOfPalette - ( j * palSpacing) << " moveto" << '\n';
fout << "(";
char dummyString[50];//should be big enough
sprintf(dummyString, palNumFormat.c_str(), palValueList[j]);
fout << dummyString << ") show" << '\n';
}
fout << "showpage" << '\n';
fout.close();
}
// -------------------------------------------------------------------
void WriteRGBFile(char *filename, XImage *ximage,
int imagesizehoriz, int imagesizevert,
const Array<XColor> &colorcells)
{
unsigned short rbuf[8192];
unsigned short gbuf[8192];
unsigned short bbuf[8192];
XColor color;
int x, y, xsize, ysize, index;
IMAGE *image;
xsize = imagesizehoriz;
ysize = imagesizevert;
//image = iopen(filename, RLE(1), 3, xsize, ysize, 3);
// no support for RLE.
image = iopen(filename, VERBATIM(1), 3, xsize, ysize, 3);
for(y=0; y<ysize; y++) {
/* fill rbuf, gbuf, and bbuf with pixel values */
for(x=0; x<xsize; x++) {
index = (int) XGetPixel(ximage,x,y);
color = colorcells[index];
rbuf[x] = color.red >> 8;
gbuf[x] = color.green >> 8;
bbuf[x] = color.blue >> 8;
}
putrow(image,rbuf,ysize-1-y,0); /* red row */
putrow(image,gbuf,ysize-1-y,1); /* green row */
putrow(image,bbuf,ysize-1-y,2); /* blue row */
}
iclose(image);
}
#ifdef BL_Linux
#define _IOWRT 0002
#define _IOERR 0040
#define _IORW 0400
#endif
// -------------------------------------------------------------
IMAGE *iopen(char *file, unsigned int type, unsigned int dim,
unsigned int xsize, unsigned int ysize, unsigned int zsize)
{
IMAGE *image;
int fdesc(0);
image = new IMAGE;
fdesc = creat(file, 0666);
if(fdesc < 0) {
cerr << "iopen: can't open output file " << file << endl;
return NULL;
}
image->imagic = IMAGIC;
image->type = type;
image->xsize = xsize;
image->ysize = 1;
image->zsize = 1;
if(dim > 1) {
image->ysize = ysize;
}
if(dim > 2) {
image->zsize = zsize;
}
if(image->zsize == 1) {
image->dim = 2;
if(image->ysize == 1) {
image->dim = 1;
}
} else {
image->dim = 3;
}
image->min = 10000000;
image->max = 0;
strncpy(image->name,"no name",80);
image->wastebytes = 0;
image->dorev = 0;
if(write(fdesc,image,sizeof(IMAGE)) != sizeof(IMAGE)) {
cerr << "iopen: error on write of image header" << endl;
return NULL;
}
image->flags = _IOWRT;
image->cnt = 0;
image->ptr = 0;
image->base = 0;
if( (image->tmpbuf = new unsigned short[IBUFSIZE(image->xsize)]) == 0 ) {
cerr << "iopen: error on tmpbuf alloc " << image->xsize << endl;
return NULL;
}
image->x = image->y = image->z = 0;
image->file = fdesc;
image->offset = 512L; // set up for img_optseek
lseek(image->file, 512L, 0);
return(image);
} // end iopen
//----------------------------------------------------------------
int putrow(IMAGE *image, unsigned short *buffer, unsigned int y, unsigned int z) {
unsigned short *sptr;
unsigned char *cptr;
unsigned int x;
unsigned long min, max;
long cnt;
if( ! (image->flags & (_IORW|_IOWRT)) ) {
cerr << "Error 1 in putrow." << endl;
return -1;
}
if(image->dim < 3) {
z = 0;
}
if(image->dim < 2) {
y = 0;
}
if(ISVERBATIM(image->type)) {
switch(BPP(image->type)) {
case 1:
min = image->min;
max = image->max;
cptr = (unsigned char *)image->tmpbuf;
sptr = buffer;
for(x=image->xsize; x--;) {
*cptr = *sptr++;
if(*cptr > max) max = *cptr;
if(*cptr < min) min = *cptr;
cptr++;
}
image->min = min;
image->max = max;
img_seek(image,y,z);
cnt = image->xsize;
if(img_write(image,(const void *)image->tmpbuf,cnt) != cnt) {
cerr << "Error 2 in putrow." << endl;
return -1;
} else {
return cnt;
}
// NOTREACHED
case 2:
cerr << "2 bytes per pixel not supported" << endl;
return -1;
default:
cerr << "putrow: weird bpp" << endl;
}
} else if(ISRLE(image->type)) {
cerr << "RLE not supported" << endl;
} else {
cerr << "putrow: weird image type" << endl;
}
return 0;
}
// -------------------------------------------------------------
int img_optseek(IMAGE *image, unsigned long offset) {
if(image->offset != offset) {
image->offset = offset;
return lseek(image->file,offset,0);
}
return offset;
}
// -------------------------------------------------------------
int img_seek(IMAGE *image, unsigned int y, unsigned int z) {
if(y >= image->ysize || z >= image->zsize) {
cerr << "img_seek: row number out of range" << endl;
return EOF;
}
image->x = 0;
image->y = y;
image->z = z;
if(ISVERBATIM(image->type)) {
switch(image->dim) {
case 1:
return img_optseek(image, 512L);
case 2:
return img_optseek(image,512L+(y*image->xsize)*BPP(image->type));
case 3:
return img_optseek(image,
512L+(y*image->xsize+z*image->xsize*image->ysize)*
BPP(image->type));
default:
cerr << "img_seek: weird dim" << endl;
break;
}
} else if(ISRLE(image->type)) {
cerr << "RLE not supported" << endl;
} else {
cerr << "img_seek: weird image type" << endl;
}
return 0;
}
// -------------------------------------------------------------
int img_write(IMAGE *image, const void *buffer, long count) {
long retval;
retval = write(image->file,buffer,count);
if(retval == count) {
image->offset += count;
} else {
image->offset = 0;
}
return retval;
}
// -------------------------------------------------------------------
int iclose(IMAGE *image) {
int ret;
unsigned short *base;
if((image->flags&_IOWRT) && (base=image->base)!=NULL && (image->ptr-base)>0) {
if(putrow(image, base, image->y,image->z)!=image->xsize) {
image->flags |= _IOERR;
return(EOF);
}
}
img_optseek(image, 0);
if(image->flags & _IOWRT) {
if(image->dorev) {
cvtimage((long *) image);
}
if(img_write(image,(const void *)image,sizeof(IMAGE)) != sizeof(IMAGE)) {
cerr << "iclose: error on write of image header" << endl;
return EOF;
}
if(image->dorev) {
cvtimage((long *) image);
}
}
if(image->base) {
delete image->base;
image->base = 0;
}
if(image->tmpbuf) {
delete [] image->tmpbuf;
image->tmpbuf = 0;
}
ret = close(image->file);
delete image;
return ret;
}
// -------------------------------------------------------------------
void cvtlongs(long buffer[], long n) {
short i;
long nlongs = n>>2;
unsigned long lwrd;
for(i = 0; i < nlongs; ++i) {
lwrd = buffer[i];
buffer[i] = ((lwrd>>24) |
(lwrd>>8 & 0xff00) |
(lwrd<<8 & 0xff0000) |
(lwrd<<24) );
}
}
//----------------------------------------------------------------
void cvtshorts(unsigned short buffer[], long n) {
short i;
long nshorts = n>>1;
unsigned short swrd;
for(i = 0; i < nshorts; ++i) {
swrd = *buffer;
*buffer++ = (swrd>>8) | (swrd<<8);
}
}
// -------------------------------------------------------------------
void cvtimage(long *buffer) {
cvtshorts((unsigned short *) buffer, 12);
cvtlongs(buffer+3, 12);
cvtlongs(buffer+26, 4);
}
// -------------------------------------------------------------------
// -------------------------------------------------------------------