Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – planix.SVN – Blame – /os/branches/planix-v0/sys/src/cmd/jpg/png.c – Rev 2

Subversion Repositories planix.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include <u.h>
2
#include <libc.h>
3
#include <bio.h>
4
#include <draw.h>
5
#include <event.h>
6
#include "imagefile.h"
7
 
8
extern int	debug;
9
int	cflag = 0;
10
int	dflag = 0;
11
int	eflag = 0;
12
int	nineflag = 0;
13
int	threeflag = 0;
14
int	colorspace = CRGB;
15
int	output = 0;
16
ulong	outchan = CMAP8;
17
Image	*image;
18
int	defaultcolor = 1;
19
 
20
enum{
21
	Border	= 2,
22
	Edge	= 5
23
};
24
 
25
char	*show(int, char*, int);
26
 
27
void
28
eresized(int new)
29
{
30
	Rectangle r;
31
 
32
	if(new && getwindow(display, Refnone) < 0){
33
		fprint(2, "png: can't reattach to window\n");
34
		exits("resize");
35
	}
36
	if(image == nil)
37
		return;
38
	r = insetrect(screen->clipr, Edge+Border);
39
	r.max.x = r.min.x+Dx(image->r);
40
	r.max.y = r.min.y+Dy(image->r);
41
	border(screen, r, -Border, nil, ZP);
42
	draw(screen, r, image, nil, image->r.min);
43
	flushimage(display, 1);
44
}
45
 
46
void
47
main(int argc, char *argv[])
48
{
49
	int fd, i;
50
	char *err;
51
	char buf[12+1];
52
 
53
	ARGBEGIN{
54
	case 'c':		/* produce encoded, compressed, bitmap file; no display by default */
55
		cflag++;
56
		dflag++;
57
		output++;
58
		if(defaultcolor)
59
			outchan = CMAP8;
60
		break;
61
	case 'D':
62
		debug++;
63
		break;
64
	case 'd':		/* suppress display of image */
65
		dflag++;
66
		break;
67
	case 'e':		/* disable floyd-steinberg error diffusion */
68
		eflag++;
69
		break;
70
	case 'k':		/* force black and white */
71
		defaultcolor = 0;
72
		outchan = GREY8;
73
		break;
74
	case 'r':
75
		colorspace = CRGB;
76
		break;
77
	case '3':		/* produce encoded, compressed, three-color bitmap file; no display by default */
78
		threeflag++;
79
		/* fall through */
80
	case 't':		/* produce encoded, compressed, true-color bitmap file; no display by default */
81
		cflag++;
82
		dflag++;
83
		output++;
84
		defaultcolor = 0;
85
		outchan = RGB24;
86
		break;
87
	case 'v':		/* force RGBV */
88
		defaultcolor = 0;
89
		outchan = CMAP8;
90
		break;
91
	case '9':		/* produce plan 9, uncompressed, bitmap file; no display by default */
92
		nineflag++;
93
		dflag++;
94
		output++;
95
		if(defaultcolor)
96
			outchan = CMAP8;
97
		break;
98
	default:
99
		fprint(2, "usage: png [-39cdekrtv] [file.png ...]\n");
100
		exits("usage");
101
	}ARGEND;
102
 
103
	if(dflag==0 && colorspace==CYCbCr){	/* see if we should convert right to RGB */
104
		fd = open("/dev/screen", OREAD);
105
		if(fd > 0){
106
			buf[12] = '\0';
107
			if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
108
				colorspace = CRGB;
109
			close(fd);
110
		}
111
	}
112
 
113
	err = nil;
114
	if(argc == 0)
115
		err = show(0, "<stdin>", outchan);
116
	else{
117
		for(i=0; i<argc; i++){
118
			fd = open(argv[i], OREAD);
119
			if(fd < 0){
120
				fprint(2, "png: can't open %s: %r\n", argv[i]);
121
				err = "open";
122
			}else{
123
				err = show(fd, argv[i], outchan);
124
				close(fd);
125
			}
126
			if((nineflag || cflag) && argc>1 && err==nil){
127
				fprint(2, "png: exiting after one file\n");
128
				break;
129
			}
130
		}
131
	}
132
	exits(err);
133
}
134
 
135
char*
136
show(int fd, char *name, int outc)
137
{
138
	Rawimage **array, *r, *c;
139
	Image *i, *i2;
140
	int j, ch, outchan;
141
	long len;
142
	Biobuf b;
143
	char buf[32];
144
	static int inited;
145
 
146
	if(Binit(&b, fd, OREAD) < 0)
147
		return nil;
148
	outchan = outc;
149
	array = Breadpng(&b, colorspace);
150
	if(array == nil || array[0]==nil){
151
		fprint(2, "png: decode %s failed: %r\n", name);
152
		return "decode";
153
	}
154
	Bterm(&b);
155
 
156
	r = array[0];
157
	if(!dflag){
158
		if (!inited) {
159
			if(initdraw(0, 0, 0) < 0){
160
				fprint(2, "png: initdraw failed: %r\n");
161
				return "initdraw";
162
			}
163
			einit(Ekeyboard|Emouse);
164
			inited++;
165
		}
166
		if(defaultcolor && screen->depth>8 && outchan==CMAP8)
167
			outchan = RGB24;
168
	}
169
	if(outchan == CMAP8)
170
		c = torgbv(r, !eflag);
171
	else{
172
		switch(r->chandesc){
173
		case CY:
174
			outchan = GREY8;
175
			break;
176
		case CYA16:
177
			outchan = CHAN2(CGrey, 8, CAlpha, 8);
178
			break;
179
		case CRGB24:
180
			outchan = RGB24;
181
			break;
182
		case CRGBA32:
183
			outchan = RGBA32;
184
			break;
185
		}
186
		c = r;
187
	}
188
	if(c == nil){
189
		fprint(2, "png: conversion of %s failed: %r\n", name);
190
		return "torgbv";
191
	}
192
	if(!dflag){
193
		i = allocimage(display, c->r, outchan, 0, 0);
194
		if(i == nil){
195
			fprint(2, "png: allocimage %s failed: %r\n", name);
196
			return "allocimage";
197
		}
198
		if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
199
			fprint(2, "png: loadimage %s of %d bytes failed: %r\n",
200
				name, c->chanlen);
201
			return "loadimage";
202
		}
203
		i2 = allocimage(display, c->r, outchan, 0, 0);
204
		draw(i2, i2->r, display->black, nil, ZP);
205
		draw(i2, i2->r, i, nil, i->r.min);
206
		image = i2;
207
		eresized(0);
208
		if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
209
			exits(nil);
210
		draw(screen, screen->clipr, display->white, nil, ZP);
211
		image = nil;
212
		freeimage(i);
213
	}
214
	if(nineflag){
215
		chantostr(buf, outchan);
216
		len = (c->r.max.x - c->r.min.x) * (c->r.max.y - c->r.min.y);
217
		switch(c->chandesc){
218
		case CY:
219
			// len *= 1;
220
			break;
221
		case CYA16:
222
			len *= 2;
223
			break;
224
		case CRGB24:
225
			len *= 3;
226
			break;
227
		case CRGBA32:
228
			len *= 4;
229
			break;
230
		}
231
		if(c->chanlen != len)
232
			fprint(2, "%s: writing %d bytes for len %ld chan %s\n",
233
				argv0, c->chanlen, len, buf);
234
		print("%11s %11d %11d %11d %11d ", buf,
235
			c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
236
		if(write(1, c->chans[0], c->chanlen) != c->chanlen){
237
			fprint(2, "png: %s: write error %r\n", name);
238
			return "write";
239
		}
240
	}else if(cflag){
241
		if(writerawimage(1, c) < 0){
242
			fprint(2, "png: %s: write error: %r\n", name);
243
			return "write";
244
		}
245
	}
246
	for(j=0; j<r->nchans; j++)
247
		free(r->chans[j]);
248
	free(r->cmap);
249
	free(r);
250
	free(array);
251
	if(c && c != r){
252
		free(c->chans[0]);
253
		free(c);
254
	}
255
	return nil;
256
}