16 int colorspace = CYCbCr; /* default for 8-bit displays: combine color rotation with dither */
18 ulong outchan = CMAP8;
27 char *show(int, char*, int);
34 if(new && getwindow(display, Refnone) < 0){
35 fprint(2, "jpg: can't reattach to window\n");
40 r = rectaddpt(image->clipr, subpt(screen->r.min, image->clipr.min));
43 draw(screen, r, image, nil, image->r.min);
44 flushimage(display, 1);
50 fprint(2, "usage: jpg -39cdefFkJrtv -W winsize [file.jpg ...]\n");
55 main(int argc, char *argv[])
64 winsize = EARGF(usage());
66 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
73 case 'd': /* suppress display of image */
76 case 'e': /* disable floyd-steinberg error diffusion */
80 Fflag++; /* make a movie */
81 fflag++; /* merge two fields per image */
84 fflag++; /* merge two fields per image */
86 case 'J': /* decode jpeg only; no display or remap (for debugging, etc.) */
89 case 'k': /* force black and white */
96 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
99 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
106 case 'v': /* force RGBV */
110 case 'y': /* leave it in CYCbCr; for debugging only */
114 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
125 if(yflag==0 && dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
126 fd = open("/dev/screen", OREAD);
129 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
137 err = show(0, "<stdin>", outchan);
139 for(i=0; i<argc; i++){
140 fd = open(argv[i], OREAD);
142 fprint(2, "jpg: can't open %s: %r\n", argv[i]);
145 err = show(fd, argv[i], outchan);
148 if((nineflag || cflag) && argc>1 && err==nil){
149 fprint(2, "jpg: exiting after one file\n");
158 vidmerge(Rawimage **aa1, Rawimage **aa2)
160 Rawimage **aao, *ao, *a1, *a2;
164 for (i = 0; aa1[i]; i++) {
169 fprint(2, "jpg: vidmerge: unequal lengths\n");
172 aao = realloc(aao, (i+2)*sizeof(Rawimage *));
174 fprint(2, "jpg: vidmerge: realloc\n");
178 ao = aao[i] = malloc(sizeof(Rawimage));
180 fprint(2, "jpg: vidmerge: realloc\n");
183 memcpy(ao, a1, sizeof(Rawimage));
184 if (!eqrect(a1->r , a2->r)){
185 fprint(2, "jpg: vidmerge: rects different in img %d\n", i);
188 if (a1->cmaplen != a2->cmaplen){
189 fprint(2, "jpg: vidmerge: cmaplen different in img %d\n", i);
192 if (a1->nchans != a2->nchans){
193 fprint(2, "jpg: vidmerge: nchans different in img %d\n", i);
196 if (a1->fields != a2->fields){
197 fprint(2, "jpg: vidmerge: fields different in img %d\n", i);
200 ao->r.max.y += Dy(ao->r);
201 ao->chanlen += ao->chanlen;
202 if (ao->chanlen != Dx(ao->r)*Dy(ao->r)){
203 fprint(2, "jpg: vidmerge: chanlen wrong %d != %d*%d\n",
204 ao->chanlen, Dx(ao->r), Dy(ao->r));
208 for (c = 0; c < ao->nchans; c++) {
211 ao->chans[c] = malloc(ao->chanlen);
215 for (col = 0; col < Dy(a1->r); col++) {
217 po += row, p1 += row;
219 po += row, p2 += row;
230 fprint(2, "jpg: vidmerge: unequal lengths\n");
237 show(int fd, char *name, int outc)
239 Rawimage **array, *r, *c;
246 if(Binit(&b, fd, OREAD) < 0)
249 rpt: array = Breadjpg(&b, colorspace);
250 if(array == nil || array[0]==nil){
251 fprint(2, "jpg: decode %s failed: %r\n", name);
257 a = Breadjpg(&b, colorspace);
258 if(a == nil || a[0]==nil){
259 fprint(2, "jpg: decode %s-2 failed: %r\n", name);
262 array = vidmerge(a, array);
272 if(initdraw(0, 0, 0) < 0){
273 fprint(2, "jpg: initdraw failed: %r\n");
277 einit(Ekeyboard|Emouse);
280 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
284 c = torgbv(r, !eflag);
286 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0)){
287 c = totruecolor(r, CY);
290 c = totruecolor(r, CRGB24);
293 fprint(2, "jpg: conversion of %s failed: %r\n", name);
297 if(c->chandesc == CY)
298 i = allocimage(display, c->r, GREY8, 0, 0);
300 i = allocimage(display, c->r, outchan, 0, 0);
302 fprint(2, "jpg: allocimage %s failed: %r\n", name);
305 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
306 fprint(2, "jpg: loadimage %s failed: %r\n", name);
313 for(j=0; j<r->nchans; j++)
320 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
322 draw(screen, screen->clipr, display->white, nil, ZP);
327 chantostr(buf, outchan);
328 print("%11s %11d %11d %11d %11d ", buf,
329 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
330 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
331 fprint(2, "jpg: %s: write error %r\n", name);
335 if(writerawimage(1, c) < 0){
336 fprint(2, "jpg: %s: write error: %r\n", name);
341 for(j=0; j<r->nchans; j++)