Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <draw.h>
5 #include <event.h>
6 #include "imagefile.h"
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;
20 enum{
21 Border = 2,
22 Edge = 5
23 };
25 char *show(int, char*, int);
27 void
28 eresized(int new)
29 {
30 Rectangle r;
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 }
46 void
47 usage(void)
48 {
49 fprint(2, "usage: png -39cdekrtv -W winsize [file.png ...]\n");
50 exits("usage");
51 }
53 void
54 main(int argc, char *argv[])
55 {
56 int fd, i;
57 char *err;
58 char buf[12+1];
60 ARGBEGIN{
61 case 'W':
62 winsize = EARGF(usage());
63 break;
64 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
65 cflag++;
66 dflag++;
67 output++;
68 if(defaultcolor)
69 outchan = CMAP8;
70 break;
71 case 'D':
72 debug++;
73 break;
74 case 'd': /* suppress display of image */
75 dflag++;
76 break;
77 case 'e': /* disable floyd-steinberg error diffusion */
78 eflag++;
79 break;
80 case 'k': /* force black and white */
81 defaultcolor = 0;
82 outchan = GREY8;
83 break;
84 case 'r':
85 colorspace = CRGB;
86 break;
87 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
88 threeflag++;
89 /* fall through */
90 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
91 cflag++;
92 dflag++;
93 output++;
94 defaultcolor = 0;
95 outchan = RGB24;
96 break;
97 case 'v': /* force RGBV */
98 defaultcolor = 0;
99 outchan = CMAP8;
100 break;
101 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
102 nineflag++;
103 dflag++;
104 output++;
105 if(defaultcolor)
106 outchan = CMAP8;
107 break;
108 default:
109 usage();
110 }ARGEND;
112 if(dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
113 fd = open("/dev/screen", OREAD);
114 if(fd > 0){
115 buf[12] = '\0';
116 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
117 colorspace = CRGB;
118 close(fd);
122 err = nil;
123 if(argc == 0)
124 err = show(0, "<stdin>", outchan);
125 else{
126 for(i=0; i<argc; i++){
127 fd = open(argv[i], OREAD);
128 if(fd < 0){
129 fprint(2, "png: can't open %s: %r\n", argv[i]);
130 err = "open";
131 }else{
132 err = show(fd, argv[i], outchan);
133 close(fd);
135 if((nineflag || cflag) && argc>1 && err==nil){
136 fprint(2, "png: exiting after one file\n");
137 break;
141 exits(err);
144 char*
145 show(int fd, char *name, int outc)
147 Rawimage **array, *r, *c;
148 static int inited;
149 Image *i;
150 int j, ch, outchan;
151 Biobuf b;
152 char buf[32];
154 if(Binit(&b, fd, OREAD) < 0)
155 return nil;
156 outchan = outc;
157 array = Breadpng(&b, colorspace);
158 if(array == nil || array[0]==nil){
159 fprint(2, "png: decode %s failed: %r\n", name);
160 return "decode";
162 Bterm(&b);
164 r = array[0];
165 if(!dflag && !inited){
166 if(initdraw(0, 0, 0) < 0){
167 fprint(2, "png: initdraw failed: %r\n");
168 return "initdraw";
170 einit(Ekeyboard|Emouse);
171 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
172 outchan = RGB24;
173 inited++;
175 if(outchan == CMAP8)
176 c = torgbv(r, !eflag);
177 else{
178 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0)){
179 c = totruecolor(r, CY);
180 outchan = GREY8;
181 }else
182 c = totruecolor(r, CRGB24);
184 if(c == nil){
185 fprint(2, "png: conversion of %s failed: %r\n", name);
186 return "torgbv";
188 if(!dflag){
189 if(c->chandesc == CY)
190 i = allocimage(display, c->r, GREY8, 0, 0);
191 else
192 i = allocimage(display, c->r, outchan, 0, 0);
193 if(i == nil){
194 fprint(2, "png: allocimage %s failed: %r\n", name);
195 return "allocimage";
197 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
198 fprint(2, "png: loadimage %s failed: %r\n", name);
199 return "loadimage";
201 image = i;
202 eresized(0);
203 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
204 exits(nil);
205 draw(screen, screen->clipr, display->white, nil, ZP);
206 image = nil;
207 freeimage(i);
209 if(nineflag){
210 chantostr(buf, outchan);
211 print("%11s %11d %11d %11d %11d ", buf,
212 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
213 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
214 fprint(2, "png: %s: write error %r\n", name);
215 return "write";
217 }else if(cflag){
218 if(writerawimage(1, c) < 0){
219 fprint(2, "png: %s: write error: %r\n", name);
220 return "write";
223 for(j=0; j<r->nchans; j++)
224 free(r->chans[j]);
225 free(r->cmap);
226 free(r);
227 free(array);
228 if(c){
229 free(c->chans[0]);
230 free(c);
232 return nil;