aboutsummaryrefslogtreecommitdiff
path: root/release/picobsd/tinyware/view/view.c
blob: 9285126616afbd947dc083876f54d626e296e5d6 (plain) (blame)
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*-
 * Copyright (c) 1998 Andrzej Bialecki
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: view.c,v 1.1 1998/08/19 06:13:35 abial Exp $
 */

/*
 * Small PNG viewer with scripting abilities
 */

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <sys/types.h>
#include <machine/console.h>
#include <machine/mouse.h>
#include <vgl.h>
#include <png.h>

#define NUMBER	8

extern char *optarg;
extern int optind;

/* Prototypes */
int kbd_action __P((int x, int y, char hotkey));

struct action {
	int zoom;
	int rotate;
	int Xshift,Yshift;
};

struct menu_item {
	char *descr;
	char hotkey;
	int (*func)(int x, int y, char hotkey);
};

struct menu_item std_menu[]= {
	{"q  Quit",'q',kbd_action},
	{"n  Next",'n',kbd_action},
	{"p  Previous",'p',kbd_action},
	{"Z  Zoom in",'Z',kbd_action},
	{"z  Zoom out",'z',kbd_action},
	{"r  Rotate",'r',kbd_action},
	{"R  Refresh",'R',kbd_action},
	{"l  Left",'l',kbd_action},
	{"h  Right",'h',kbd_action},
	{"j  Up",'j',kbd_action},
	{"k  Down",'k',kbd_action},
	{NULL,0,NULL}
};

char *progname;
VGLBitmap pic,bkg;
struct action a;
byte pal_red[256];
byte pal_green[256];
byte pal_blue[256];
byte pal_colors;
double screen_gamma;
int max_screen_colors=15;
int quit,changed;
char **pres;
int nimg=0;
int cur_img=0;
char act;
FILE *log;

void
usage()
{
	fprintf(stderr,"\nVGL graphics viewer, 1.0 (c) Andrzej Bialecki.\n");
	fprintf(stderr,"\nUsage:\n");
	fprintf(stderr,"\t%s [-r n] [-g n.n] filename\n",progname);
	fprintf(stderr,"\nwhere:\n");
	fprintf(stderr,"\t-r n\tchoose resolution:\n");
	fprintf(stderr,"\t\t0 - 640x480x16 (default)\n");
	fprintf(stderr,"\t\t1 - 640x200x256\n");
	fprintf(stderr,"\t\t2 - 320x240x256\n");
	fprintf(stderr,"\t-g n.n\tset screen gamma (1.3 by default)\n");
	fprintf(stderr,"\n");
}

int
pop_up(char *title,int x, int y)
{
	VGLBitmap sav,clr;
	int x1,y1,width,height,i,j;
	int last_pos,cur_pos,max_item;
	char buttons;
	char *t;

	sav.Type=VGLDisplay->Type;
	clr.Type=VGLDisplay->Type;
	width=0;
	height=0;
	max_item=0;
	i=0;
	while(std_menu[i].descr!=NULL) {
		height++;
		max_item++;
		if(strlen(std_menu[i].descr)>width) width=strlen(std_menu[i].descr);
		i++;
	}
	width=width*8+2;
	height=height*9+4+8;
	sav.Xsize=width;
	sav.Ysize=height;
	clr.Xsize=width;
	clr.Ysize=height;
	sav.Bitmap=(byte *)calloc(width*height,1);
	clr.Bitmap=(byte *)calloc(width*height,1);
	if(x>(VGLDisplay->Xsize-width)) x1=VGLDisplay->Xsize-width;
	else x1=x;
	if(y>(VGLDisplay->Ysize-height)) y1=VGLDisplay->Ysize-height;
	else y1=y;
	VGLMouseMode(VGL_MOUSEHIDE);
	VGLBitmapCopy(VGLDisplay,x1,y1,&sav,0,0,width,height);
	VGLFilledBox(VGLDisplay,x1,y1,x1+width-1,y1+height-1,pal_colors-1);
	VGLBitmapString(VGLDisplay,x1+1,y1+1,title,0,pal_colors-1,0,0);
	VGLLine(VGLDisplay,x1,y1+9,x1+width,y1+9,0);
	i=0;
	while(std_menu[i].descr!=NULL) {
		VGLBitmapString(VGLDisplay,x1+1,y1+11+i*9,std_menu[i].descr,0,pal_colors-1,0,0);
		i++;
	}
	last_pos=-1;
	VGLMouseMode(VGL_MOUSESHOW);
	do {
		pause();
		VGLMouseStatus(&x,&y,&buttons);
		cur_pos=(y-y1-11)/9;
		if((cur_pos<0)||(cur_pos>max_item-1)) {
			if(last_pos==-1) last_pos=0;
			VGLBitmapString(VGLDisplay,x1+1,y1+11+last_pos*9,std_menu[last_pos].descr,0,pal_colors-1,0,0);
			last_pos=-1;
		} else if(last_pos!=cur_pos) {
			if(last_pos==-1) last_pos=0;
			VGLBitmapString(VGLDisplay,x1+1,y1+11+last_pos*9,std_menu[last_pos].descr,0,pal_colors-1,0,0);
			VGLBitmapString(VGLDisplay,x1+1,y1+11+cur_pos*9,std_menu[cur_pos].descr,pal_colors/2+1,pal_colors-1,0,0);
			last_pos=cur_pos;
		}
	} while (buttons & MOUSE_BUTTON3DOWN);
	VGLMouseMode(VGL_MOUSEHIDE);
	/* XXX Screws up totally when r==3. Libvgl bug! */
	VGLBitmapCopy(&clr,0,0,VGLDisplay,x1,y1,width,height);
	VGLBitmapCopy(&sav,0,0,VGLDisplay,x1,y1,width,height);
	VGLMouseMode(VGL_MOUSESHOW);
	free(sav.Bitmap);
	free(clr.Bitmap);
	changed++;
	if((cur_pos>=0) && (cur_pos<max_item)) {
		std_menu[cur_pos].func(x,y,std_menu[cur_pos].hotkey);
	}
	changed++;
	return(0);
}

void
display(	VGLBitmap *pic,
		byte *red,
		byte *green,
		byte *blue,
		struct action *e)
{
	VGLBitmap target;
	int x,y,i=0,j=0;

	VGLMouseMode(VGL_MOUSEHIDE);
	VGLRestorePalette();
	/* XXX Broken in r!=2. Libvgl bug. */
	//VGLClear(VGLDisplay,0);
	VGLBitmapCopy(&bkg,0,0,VGLDisplay,0,0,bkg.Xsize,bkg.Ysize);

	if(e!=NULL) {
		if(e->zoom!=1 || e->rotate) {
			target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize*e->zoom*e->zoom,1);
			if(e->rotate) {
				target.Xsize=pic->Ysize*e->zoom;
				target.Ysize=pic->Xsize*e->zoom;
			} else {
				target.Xsize=pic->Xsize*e->zoom;
				target.Ysize=pic->Ysize*e->zoom;
			}
			target.Type=pic->Type;
			for(x=0;x<pic->Xsize;x++) {
				for(y=0;y<pic->Ysize;y++) {
					for(i=0;i<e->zoom;i++) {
						for(j=0;j<e->zoom;j++) {
							if(e->rotate) {
								VGLSetXY(&target,target.Xsize-(e->zoom*y+i),e->zoom*x+j,VGLGetXY(pic,x,y));
							} else {
								VGLSetXY(&target,e->zoom*x+i,e->zoom*y+j,VGLGetXY(pic,x,y));
							}
						}
					}
				}
			}
		} else {
			target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize,sizeof(byte));
			target.Xsize=pic->Xsize;
			target.Ysize=pic->Ysize;
			target.Type=pic->Type;
			VGLBitmapCopy(pic,0,0,&target,0,0,pic->Xsize,pic->Ysize);
		}
	} else {
		target.Bitmap=(byte *)calloc(pic->Xsize*pic->Ysize,sizeof(byte));
		target.Xsize=pic->Xsize;
		target.Ysize=pic->Ysize;
		target.Type=pic->Type;
		VGLBitmapCopy(pic,0,0,&target,0,0,pic->Xsize,pic->Ysize);
	}
	VGLSetPalette(red, green, blue);
	if(e!=NULL) {
		VGLBitmapCopy(&target,0,0,VGLDisplay,e->Xshift,e->Yshift,target.Xsize,target.Ysize);
	} else {
		VGLBitmapCopy(&target,0,0,VGLDisplay,0,0,target.Xsize,target.Ysize);
	}
	VGLMouseMode(VGL_MOUSESHOW);
	free(target.Bitmap);
}

int
png_load(char *filename)
{
	int i,j,k;
	FILE *fd;
	u_char header[NUMBER];
	png_structp png_ptr;
	png_infop info_ptr,end_info;
	png_uint_32 width,height;
	int bit_depth,color_type,interlace_type;
	int compression_type,filter_type;
	int channels,rowbytes;
	double gamma;
	png_colorp palette;
	int num_palette;
	png_bytep *row_pointers;
	char c;
	int res=0;

	fd=fopen(filename,"rb");
	
	if(fd==NULL) {
		VGLEnd();
		perror("fopen");
		exit(1);
	}
	fread(header,1,NUMBER,fd);
	if(!png_check_sig(header,NUMBER)) {
		fprintf(stderr,"Not a PNG file.\n");
		return(-1);
	}
	png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,(void *)NULL,
		NULL,NULL);
	info_ptr=png_create_info_struct(png_ptr);
	end_info=png_create_info_struct(png_ptr);
	if(!png_ptr || !info_ptr || !end_info) {
		VGLEnd();
		fprintf(stderr,"failed to allocate needed structs!\n");
		png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
		return(-1);
	}
	png_set_sig_bytes(png_ptr,NUMBER);
	png_init_io(png_ptr,fd);
	png_read_info(png_ptr,info_ptr);
	png_get_IHDR(png_ptr,info_ptr,&width,&height,&bit_depth,
		&color_type,&interlace_type,&compression_type,&filter_type);
	png_get_PLTE(png_ptr,info_ptr,&palette,&num_palette);
	channels=png_get_channels(png_ptr,info_ptr);
	rowbytes=png_get_rowbytes(png_ptr,info_ptr);
	if(bit_depth==16)
		png_set_strip_16(png_ptr);
	if(color_type & PNG_COLOR_MASK_ALPHA) 
		png_set_strip_alpha(png_ptr);
	if(png_get_gAMA(png_ptr,info_ptr,&gamma))
		png_set_gamma(png_ptr,screen_gamma,gamma);
	else
	png_set_gamma(png_ptr,screen_gamma,0.45);
	if(res==0) {
		/* Dither */
		if(color_type & PNG_COLOR_MASK_COLOR) {
			if(png_get_valid(png_ptr,info_ptr,PNG_INFO_PLTE)) {
				png_uint_16p histogram;
				png_get_hIST(png_ptr,info_ptr,&histogram);
				png_set_dither(png_ptr,palette,num_palette,max_screen_colors,histogram,0);
			} else {
				png_color std_color_cube[16]={
					{0x00,0x00,0x00},
					{0x02,0x02,0x02},
					{0x04,0x04,0x04},
					{0x06,0x06,0x06},
					{0x08,0x08,0x08},
					{0x0a,0x0a,0x0a},
					{0x0c,0x0c,0x0c},
					{0x0e,0x0e,0x0e},
					{0x10,0x10,0x10},
					{0x12,0x12,0x12},
					{0x14,0x14,0x14},
					{0x16,0x16,0x16},
					{0x18,0x18,0x18},
					{0x1a,0x1a,0x1a},
					{0x1d,0x1d,0x1d},
					{0xff,0xff,0xff},
				};
				png_set_dither(png_ptr,std_color_cube,max_screen_colors,max_screen_colors,NULL,0);
			}
		}
	}
	png_set_packing(png_ptr);
	if(png_get_valid(png_ptr,info_ptr,PNG_INFO_sBIT)) {
		png_color_8p sig_bit;

		png_get_sBIT(png_ptr,info_ptr,&sig_bit);
		png_set_shift(png_ptr,sig_bit);
	}
	png_read_update_info(png_ptr,info_ptr);
	png_get_IHDR(png_ptr,info_ptr,&width,&height,&bit_depth,
		&color_type,&interlace_type,&compression_type,&filter_type);
	png_get_PLTE(png_ptr,info_ptr,&palette,&num_palette);
	channels=png_get_channels(png_ptr,info_ptr);
	rowbytes=png_get_rowbytes(png_ptr,info_ptr);
	row_pointers=malloc(height*sizeof(png_bytep));
	for(i=0;i<height;i++) {
		row_pointers[i]=malloc(rowbytes);
	}
	png_read_image(png_ptr,row_pointers);
	png_read_end(png_ptr,end_info);
	png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
	fclose(fd);
	/* Set palette */
	if(res) k=2;
	else k=2;
	for(i=0;i<256;i++) {
	 	pal_red[i]=255;
	 	pal_green[i]=255;
	 	pal_blue[i]=255;
	}
	for(i=0;i<num_palette;i++) {
	 	pal_red[i]=(palette+i)->red>>k;
	 	pal_green[i]=(palette+i)->green>>k;
	 	pal_blue[i]=(palette+i)->blue>>k;
	}
	pal_colors=num_palette;
	if(pic.Bitmap!=NULL) free(pic.Bitmap);
	pic.Bitmap=(byte *)calloc(rowbytes*height,sizeof(byte));
	pic.Type=MEMBUF;
	pic.Xsize=rowbytes;
	pic.Ysize=height;
	for(i=0;i<rowbytes;i++) {
		for(j=0;j<height;j++) {
			VGLSetXY(&pic,
			i,j,row_pointers[j][i]);
		}
	}
	a.zoom=1;
	a.Xshift=(VGLDisplay->Xsize-pic.Xsize)/2;
	a.Yshift=(VGLDisplay->Ysize-pic.Ysize)/2;
	a.rotate=0;
	return(0);
}

void
kbd_handler(int sig)
{
	u_char buf[10];
	int res;

	res=read(0,&buf,10);
	changed++;
	act=buf[res-1];
}

int
kbd_action(int x, int y, char key)
{
	changed=0;
	switch(key) {
	case 'q':
		quit=1;
		break;
	case 'Z':
		a.zoom++;
		changed++;
		break;
	case 'z':
		a.zoom--;
		if(a.zoom<1) a.zoom=1;
		changed++;
		break;
	case 'l':
		a.Xshift+=VGLDisplay->Xsize/5;
		changed++;
		break;
	case 'h':
		a.Xshift-=VGLDisplay->Xsize/5;
		changed++;
		break;
	case 'k':
		a.Yshift+=VGLDisplay->Ysize/5;
		changed++;
		break;
	case 'j':
		a.Yshift-=VGLDisplay->Ysize/5;
		changed++;
		break;
	case 'R':
		changed++;
		break;
	case 'r':
		if(a.rotate) a.rotate=0;
		else a.rotate=1;
		changed++;
		break;
	case '\n':
	case 'n':
		if((nimg>0) && (cur_img<nimg-1)) {
			cur_img++;
			png_load(pres[cur_img]);
			changed++;
		}
		break;
	case 'p':
		if((nimg>0) && (cur_img>0)) {
			cur_img--;
			png_load(pres[cur_img]);
			changed++;
		}
		break;
	}
	act=0;
}

int
main(int argc, char *argv[])
{
	int i,j,k;
	char c;
	int res=0;
	int x,y;
	char buttons;
	struct termios t_new,t_old;
	FILE *fsc;

	char buf[100];

	progname=argv[0];
	screen_gamma=1.5;
	log=fopen("/png/view.log","w");
	while((c=getopt(argc,argv,"r:g:"))!=-1) {
		switch(c) {
		case 'r':
			res=atoi(optarg);
			if(res>0) max_screen_colors=256;
			break;
		case 'g':
			screen_gamma=atof(optarg);
			break;
		case '?':
		default:
			usage();
			exit(0);
		}
	}
	switch(res) {
	case 0:
		VGLInit(SW_CG640x480);
		break;
	case 1:
		VGLInit(SW_VGA_CG320);
		break;
	case 2:
		VGLInit(SW_VGA_MODEX);
		break;
	default:
		fprintf(stderr,"No such resolution!\n");
		usage();
		exit(-1);
	}
	fprintf(log,"VGL initialised\n");
	VGLSavePalette();
	VGLMouseInit(VGL_MOUSEHIDE);
	if(argc>optind) {
		res=png_load(argv[optind]);
	} else {
		VGLEnd();
		usage();
		exit(0);
	}
	if(res) {
		/* Hmm... Script? */
		fsc=fopen(argv[optind],"r");
		fprintf(log,"Trying script %s\n",argv[optind]);
		fgets(buf,99,fsc);
		if(strcmp("VIEW SCRIPT\n",buf)!=NULL) {
			VGLEnd();
			usage();
		}
		fgets(buf,99,fsc);
		buf[strlen(buf)-1]='\0';
		nimg=atoi(buf);
		if(nimg==0) {
			VGLEnd();
			usage();
		}
		pres=(char **)calloc(nimg,sizeof(char *));
		for(i=0;i<nimg;i++) {
			fgets(buf,99,fsc);
			buf[strlen(buf)-1]='\0';
			pres[i]=strdup(buf);
		}
		fclose(fsc);
		cur_img=0;
		fprintf(log,"Script with %d entries\n",nimg);
		png_load(pres[cur_img]);
	}
	/* Prepare the keyboard */
	tcgetattr(0,&t_old);
	memcpy(&t_new,&t_old,sizeof(struct termios));
	cfmakeraw(&t_new);
	tcsetattr(0,TCSAFLUSH,&t_new);
	fcntl(0,F_SETFL,O_ASYNC);
	/* XXX VGLClear doesn't work.. :-(( Prepare a blank background */
	bkg.Bitmap=(byte *)calloc(VGLDisplay->Xsize*VGLDisplay->Ysize,1);
	bkg.Xsize=VGLDisplay->Xsize;
	bkg.Ysize=VGLDisplay->Ysize;
	bkg.Type=VGLDisplay->Type;
	signal(SIGIO,kbd_handler);
	a.zoom=1;
	a.Xshift=(VGLDisplay->Xsize-pic.Xsize)/2;
	a.Yshift=(VGLDisplay->Ysize-pic.Ysize)/2;
	a.rotate=0;
	quit=0;
	changed=0;
	display(&pic,pal_red,pal_green,pal_blue,&a);
	while(!quit) {
		if(act) {
			fprintf(log,"kbd_action(%c)\n",act);
			kbd_action(x,y,act);
		}
		if(quit) break;
		if(changed) {
			fprintf(log,"changed, redisplaying\n");
			display(&pic,pal_red,pal_green,pal_blue,&a);
			changed=0;
		}
		pause();
		VGLMouseStatus(&x,&y,&buttons);
		if(buttons & MOUSE_BUTTON3DOWN) {
			fprintf(log,"pop_up called\n");
			pop_up("View",x,y);
		}
	}
	VGLEnd();
	fclose(log);
	exit(0);
}