-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetsun.c
546 lines (473 loc) · 13 KB
/
getsun.c
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* getsun - display an rle image on a sun workstation running the
* sun window environment.
*
* Bug reports, or better yet fixes/enhancements, may be sent to the author.
* Author:
* Philip J. Klimbal
* RIACS
* MS 230-5
* NASA Ames Research Center
* Moffett Field, CA 94035
* ..!ames!riacs!klimbal
* klimbal@riacs.edu
*
* Copyright (c) 1987 Research Institute for Advanced Computer Science
* All rights reserved. No explicit or implicit warrenty provided.
*
* Based on getX - Put RLE images on X display,
* by Spencer W. Thomas, University of Utah.
* Copyright (c) 1986, University of Utah
*
*
* Utah Copyright Header (getX):
*
* This software is copyrighted as noted below. It may be freely copied,
* modified, and redistributed, provided that the copyright notice is
* preserved on all copies.
*
* There is no warranty or other guarantee of fitness for this software,
* it is provided solely "as is". Bug reports or fixes may be sent
* to the author, who may or may not act on them as he desires.
*
* You may not include this software in a program or other software product
* without supplying the source, or without informing the end-user that the
* source is available for no extra charge.
*
*
* cc .... -lrle -lsuntool -lsunwindow -lpixrect -lm
*/
static char rcsid[] = "$Header: /l/spencer/src/urt/get/RCS/getsun.c,v 3.0.1.2 1992/04/30 14:05:10 spencer Exp $";
/*
getsun() Tag the file.
*/
#include <stdio.h>
#include <math.h>
#include <sys/file.h>
#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include "rle.h"
void get_pic(), create_window(), init_color(), map_rgb_to_bw();
void map_scanline(), put_scanline();
/*
* Basic magic square for dithering
*/
int dm16[16][16];
/*
* Color map, gamma correction map, and lookup tables
*/
rle_pixel ** in_cmap;
int modN[256], divN[256];
int bwflag = 0; /* if non zero, dither in B&W
* Value of 2 means 1-bit system
*/
/*
* Number of color map levels, (square and cube), and number of gradations
* per level.
*/
int levels = 0, levelsq, levelsc;
/*
* Global variables
*/
double disp_gam = 2.5; /* default gammas for display and image */
double img_gam = 1.0;
int iflag = 0; /* flag to tell if gamma was on command line */
int nscan, nrow; /* size of window */
int nbyte; /* size of bitrow for 1-bit displays */
int dbg = 0; /* set if debug mode */
unsigned char *buffer; /* data storage pointers */
Frame frame; /* sun window display frame */
Canvas canvas; /* display frame canvas */
Pixwin *pw; /* pixwin canvas */
/*****************************************************************
* TAG( main )
*
* Usage:
* getsun [-D] [-{wW}] [-{iI} gamma] [-g gamma] [-l levels] [file]
* Inputs:
* -D: Debug mode: print input file as read.
* -f: Don't fork after putting image on screen.
* -w: Black & white: reduce color images to B&W before
* display. Advantage is that smoother shading can
* be achieved.
* -i gamma: Specify gamma of image. (default 1.0)
* -I gamma: Specify gamma of display image was computed for.
* getsun will also read picture comments from the input file to determine
* the image gamma. These are
* image_gamma= gamma of image (equivalent to -i)
* display_gamma= gamma of display image was computed for.
* Command line arguments override values in the file.
* -g gamma: Specify gamma of display. (default 2.5)
* -l levels: Number of color levels.
* file: Input Run Length Encoded file. Uses stdin if not
* specified.
* Outputs:
* Puts image on screen.
* Assumptions:
* Input file is in RLE format.
* Algorithm:
* [None]
*/
void
main(argc, argv)
int argc;
char **argv;
{
CONST_DECL char *infname = NULL;
FILE * infile;
int gflag = 0;
int forkflg = 0;
if ( scanargs( argc, argv,
"% Ww%- D%- f%- l%-levels!d Ii%-gamma!F g%-gamma!F file%s",
&bwflag, &dbg, &forkflg,
&levels, &levels,
&iflag, &img_gam,
&gflag, &disp_gam,
&infname ) == 0 )
exit( 1 );
if ( levels != 0 && (levels > 6 || levels < 2)) {
fprintf(stderr,"Level must be in the range [2,6]; %d is invalid!\n", levels);
exit( 1 );
}
if ( iflag == 1 ) /* -i */
img_gam = 1.0 / img_gam;
infile = rle_open_f("getsun", infname, "r");
if ( infile == stdin )
infname = "Standard Input";
if ( !forkflg && fork() != 0 )
exit(0);
get_pic( infile, infname);
if ( infname != NULL) fclose( infile);
if ( !forkflg )
{
fclose(stdin); /* Close stdio to release rshd. */
fclose(stdout);
fclose(stderr);
}
window_main_loop(frame);
exit(0);
}
/*****************************************************************
* TAG( get_pic )
*
* Read in an RLE image, compute color map, and display it.
*
* Inputs:
* infile: File pointer for input.
* infname: Name of input file.
*
*/
void
get_pic( infile, infname, cmdname )
FILE * infile;
char * infname, * cmdname;
{
register int i,
y;
int ncolors;
unsigned char *scan[3];
rle_hdr hdr;
/*
* Read setup info from file.
*/
hdr = *rle_hdr_init( (rle_hdr *)NULL );
rle_names( &hdr, cmdname, infname, 0 );
hdr.rle_file = infile;
rle_get_setup_ok(&hdr, NULL, NULL);
if ( dbg )
rle_debug( 1 );
/* We're only interested in R, G, & B */
RLE_CLR_BIT(hdr, RLE_ALPHA);
for (i = 3; i < hdr.ncolors; i++)
RLE_CLR_BIT(hdr, i);
ncolors = hdr.ncolors > 3 ? 3 : hdr.ncolors;
/*
* Compute image size and allocate storage.
*/
nrow = (hdr.xmax - hdr.xmin + 1);
nscan = (hdr.ymax - hdr.ymin + 1);
/*
* Get a window of the right size.
*/
create_window(nrow, nscan, infname);
if ( bwflag == 2 )
nbyte = ((nrow + 15) / 16) * 2; /* 1 bit display */
else
nbyte = ((nrow + 1) / 2) * 2;
buffer = (unsigned char *) malloc(nbyte * nscan);
RLE_CHECK_ALLOC( hdr.cmd, buffer, "image memory" );
/*
* Set up for rle_getrow. Pretend image x origin is 0.
*/
for (i = 0; i < 3; i++)
scan[i] = (unsigned char *) malloc(nrow);
RLE_CHECK_ALLOC( hdr.cmd, scan[0]&&scan[1]&&scan[2], "input scan memory" );
hdr.xmax -= hdr.xmin;
hdr.xmin = 0;
/* If no image gamma on command line, check comments in file */
if ( ! iflag )
{
char * v;
if ( (v = rle_getcom( "image_gamma", &hdr )) != NULL )
{
img_gam = atof( v );
/* Protect against bogus information */
if ( img_gam == 0.0 )
img_gam = 1.0;
else
img_gam = 1.0 / img_gam;
}
else if ( (v = rle_getcom( "display_gamma", &hdr )) != NULL )
{
img_gam = atof( v );
/* Protect */
if ( img_gam == 0.0 )
img_gam = 1.0;
}
}
/*
* Set up the color map.
*/
/* Input map, at least 3 channels */
in_cmap = buildmap( &hdr, 3, img_gam, 1.0 );
init_color();
/*
* For each scan line, dither it and display.
*/
while ((y = rle_getrow(&hdr, scan)) <= hdr.ymax)
{
if ( bwflag && ncolors > 1 )
{
map_rgb_to_bw( scan[0], scan[1], scan[ncolors - 1], scan[0],
in_cmap, nrow );
/* Note: map_scanline only uses channel 0 for B&W */
}
else if ( bwflag )
for ( i = 0; i < nrow; i++ )
scan[0][i] = in_cmap[0][scan[0][i]];
else
for (i = 2; i >= ncolors; i--)
bcopy(scan[0], scan[i], nrow);
map_scanline(scan, nrow, 1, y,
&buffer[(hdr.ymax - y) * nbyte]);
put_scanline(&buffer[(hdr.ymax - y) * nbyte], nrow, 0,
hdr.ymax - y );
}
/*
* Free temp storage
*/
for (i = 0; i < 3; i++)
free(scan[i]);
}
/*****************************************************************
* TAG( create_window )
*
* Create a sun window and place a pixwin canvas in it.
*
* Inputs:
* width: width of window.
* height: height of window.
* name: name of image file.
*
*/
void
create_window( width, height, name)
int width, height;
char *name;
{
char buf[BUFSIZ];
sprintf(buf,"\"%s\": %d X %d",
name, height, width);
frame = window_create(0, FRAME,
FRAME_SHOW_LABEL, TRUE,
FRAME_LABEL, buf,
FRAME_NO_CONFIRM, TRUE,
0);
canvas = window_create(frame, CANVAS,
WIN_HEIGHT, height,
WIN_WIDTH, width,
0);
window_fit(frame);
if ((pw = canvas_pixwin(canvas)) == NULL) {
perror("Cannot create pixwin");
exit( 1 );
}
if (pw->pw_pixrect->pr_depth == 1) bwflag=2;
}
/*****************************************************************
* TAG( map_scanline )
*
* Map a scanline to 8 bits through the dither matrix.
*
* Inputs:
* rgb: Pointers to buffers containing the red, green,
* and blue color rows.
* n: Length of row.
* s: Skip between pixels in original image.
* y: Y position of row (necessary for dither)
* line: Pointer to output buffer for dithered color data.
*/
#define DMAP(v,x,y) (modN[v]>dm16[x][y] ? divN[v] + 1 : divN[v])
void
map_scanline( rgb, n, s, y, line )
unsigned char *rgb[3], *line;
int n, s, y;
{
register int i, col, row;
if ( !bwflag )
{
register unsigned char *r, *g, *b;
for ( row = y % 16, col = 0, i = 0, r = rgb[0], g = rgb[1], b = rgb[2];
i < n; i++, r+=s, g+=s, b+=s, col = ((col + 1) & 15) )
line[i] = DMAP(in_cmap[0][*r], col, row) +
DMAP(in_cmap[1][*g], col, row) * levels +
DMAP(in_cmap[2][*b], col, row) * levelsq;
}
else if ( bwflag == 1 ) /* gray scale display */
{
register unsigned char *r;
for ( row = y % 16, col = 0, i = 0, r = rgb[0];
i < n; i++, r+=s, col = ((col + 1) & 15) )
line[i] = DMAP(*r, col, row);
}
else /* bitmap display */
{
register unsigned short *l = (unsigned short *)line;
register unsigned char * r;
for ( row = y % 16, col = 0, i = 0, r = rgb[0], *l = 0;
i < n; r+=s, col = ((col + 1) & 15) )
{
*l |= (*r > dm16[col][row] ? 0 : 1) << (15-(i % 16));
if ( (++i % 16) == 0 )
*++l = 0;
}
}
}
/*****************************************************************
* TAG( put_scanline )
*
* Output scanline into pixwin.
*
* Inputs:
* scan: pointer to scanline.
* width: width of scanline.
* x,y: coordinates of this scanline.
*
*/
void
put_scanline( scan, width, x, y )
unsigned char *scan;
int width, x, y;
{
Pixrect *pix;
if ((pix = mem_point(width,1,pw->pw_pixrect->pr_depth,scan)) == NULL) {
perror("Unable to allocate pixrect");
exit( 1 );
}
pw_write(pw, x, y, width, 1, PIX_SRC, pix, 0, 0);
free(pix);
}
/*****************************************************************
* TAG( map_rgb_to_bw )
*
* Convert RGB to black and white through NTSC transform, but map
* RGB through a color map first.
* Inputs:
* red_row, green_row, blue_row: Given RGB pixel data.
* map: Array[3] of pointers to pixel arrays,
* representing color map.
* rowlen: Number of pixels in the rows.
* Outputs:
* bw_row: Output B&W data. May coincide with one of the
* inputs.
* Algorithm:
* BW = .35*map[0][R] + .55*map[1][G] + .10*map[2][B]
*/
void
map_rgb_to_bw( red_row, green_row, blue_row, bw_row, map, rowlen )
rle_pixel *red_row;
rle_pixel *green_row;
rle_pixel *blue_row;
rle_pixel *bw_row;
rle_pixel **map;
int rowlen;
{
register int x, bw;
for (x=0; x<rowlen; x++)
{
/* 68000 won't store float > 127 into byte? */
/* HP compiler blows it */
bw = .35*map[0][red_row[x]] + .55*map[1][green_row[x]] +
.10*map[2][blue_row[x]];
bw_row[x] = bw;
}
}
/*****************************************************************
* TAG( init_color )
*
* Build color map and load it into the pixwin.
*
*/
void
init_color()
{
register int i;
int rgbmap[256][3], bwmap[256];
unsigned char colormap[3][256];
char buf[12];
if ( !bwflag )
{
/*
* Figure out how many color map entries we can get
*/
if ( levels == 0 )
levels = 5; /* default starting point */
levelsq = levels * levels;
levelsc = levelsq * levels;
dithermap( levels, disp_gam, rgbmap, divN, modN, dm16 );
/*
* Copy the color map entries into something that sunwindows
* can work with.
*/
for(i = 0; i < levelsc; i++) {
colormap[0][i] = (u_char)rgbmap[i][0]&0xff;
colormap[1][i] = (u_char)rgbmap[i][1]&0xff;
colormap[2][i] = (u_char)rgbmap[i][2]&0xff;
}
sprintf(buf,"getsun.c%1d",levels);
pw_setcmsname(pw, buf);
pw_putcolormap(pw, 0, 4<<levels,colormap[0],colormap[1],colormap[2]);
return;
}
/* Get a B&W color map (gray scale) */
if ( bwflag == 1 )
{
if ( levels == 0 )
levels = 5;
levelsq = levels * levels;
levelsc = levelsq * levels;
bwdithermap( levels, disp_gam, bwmap, divN, modN, dm16 );
/*
* Copy the color map entries into something that sunwindows
* can work with.
*/
for ( i = 0; i < levelsc; i++ )
{
colormap[0][i] = (u_char)bwmap[i]&0xff;
colormap[1][i] = (u_char)bwmap[i]&0xff;
colormap[2][i] = (u_char)bwmap[i]&0xff;
}
sprintf(buf,"getsun.g%1d",levels);
pw_setcmsname(pw, buf);
pw_putcolormap(pw, 0,4<<levels, colormap[0],colormap[1],colormap[2]);
return;
}
/* If b&w 1-bit display, just use two colors */
if ( bwflag == 2 )
{
/* All we care about, really, is the magic square */
make_square( 255.0, divN, modN, dm16 );
levels = levelsc = 2;
return;
}
}