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 = insetrect(screen->clipr, Edge+Border);
41 r.max.x = r.min.x+Dx(image->r);
42 r.max.y = r.min.y+Dy(image->r);
43 border(screen, r, -Border, nil, ZP);
44 drawop(screen, r, image, nil, image->r.min, S);
45 flushimage(display, 1);
49 main(int argc, char *argv[])
57 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
64 case 'd': /* suppress display of image */
67 case 'e': /* disable floyd-steinberg error diffusion */
71 Fflag++; /* make a movie */
72 fflag++; /* merge two fields per image */
75 fflag++; /* merge two fields per image */
77 case 'J': /* decode jpeg only; no display or remap (for debugging, etc.) */
80 case 'k': /* force black and white */
87 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
90 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
97 case 'v': /* force RGBV */
101 case 'y': /* leave it in CYCbCr; for debugging only */
105 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
113 fprint(2, "usage: jpg -39cdefFkJrtv [file.jpg ...]\n");
117 if(yflag==0 && dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
118 fd = open("/dev/screen", OREAD);
121 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
129 err = show(0, "<stdin>", outchan);
131 for(i=0; i<argc; i++){
132 fd = open(argv[i], OREAD);
134 fprint(2, "jpg: can't open %s: %r\n", argv[i]);
137 err = show(fd, argv[i], outchan);
140 if((nineflag || cflag) && argc>1 && err==nil){
141 fprint(2, "jpg: exiting after one file\n");
150 vidmerge(Rawimage **aa1, Rawimage **aa2)
152 Rawimage **aao, *ao, *a1, *a2;
156 for (i = 0; aa1[i]; i++) {
161 fprint(2, "jpg: vidmerge: unequal lengths\n");
164 aao = realloc(aao, (i+2)*sizeof(Rawimage *));
166 fprint(2, "jpg: vidmerge: realloc\n");
170 ao = aao[i] = malloc(sizeof(Rawimage));
172 fprint(2, "jpg: vidmerge: malloc\n");
175 memcpy(ao, a1, sizeof(Rawimage));
176 if (!eqrect(a1->r , a2->r)){
177 fprint(2, "jpg: vidmerge: rects different in img %d\n", i);
180 if (a1->cmaplen != a2->cmaplen){
181 fprint(2, "jpg: vidmerge: cmaplen different in img %d\n", i);
184 if (a1->nchans != a2->nchans){
185 fprint(2, "jpg: vidmerge: nchans different in img %d\n", i);
188 if (a1->fields != a2->fields){
189 fprint(2, "jpg: vidmerge: fields different in img %d\n", i);
192 ao->r.max.y += Dy(ao->r);
193 ao->chanlen += ao->chanlen;
194 if (ao->chanlen != Dx(ao->r)*Dy(ao->r)){
195 fprint(2, "jpg: vidmerge: chanlen wrong %d != %d*%d\n",
196 ao->chanlen, Dx(ao->r), Dy(ao->r));
200 for (c = 0; c < ao->nchans; c++) {
203 ao->chans[c] = malloc(ao->chanlen);
204 if (ao->chans[c] == nil){
205 fprint(2, "jpg: vidmerge: malloc chan\n");
211 for (col = 0; col < Dy(a1->r); col++) {
213 po += row, p1 += row;
215 po += row, p2 += row;
226 fprint(2, "jpg: vidmerge: unequal lengths\n");
233 show(int fd, char *name, int outc)
235 Rawimage **array, *r, *c;
242 if(Binit(&b, fd, OREAD) < 0)
245 rpt: array = Breadjpg(&b, colorspace);
246 if(array == nil || array[0]==nil){
247 fprint(2, "jpg: decode %s failed: %r\n", name);
253 a = Breadjpg(&b, colorspace);
254 if(a == nil || a[0]==nil){
255 fprint(2, "jpg: decode %s-2 failed: %r\n", name);
258 array = vidmerge(a, array);
268 if(initdraw(0, 0, 0) < 0){
269 fprint(2, "jpg: initdraw failed: %r\n");
273 einit(Ekeyboard|Emouse);
276 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
280 c = torgbv(r, !eflag);
282 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0)){
283 c = totruecolor(r, CY);
286 c = totruecolor(r, CRGB24);
289 fprint(2, "jpg: conversion of %s failed: %r\n", name);
293 if(c->chandesc == CY)
294 i = allocimage(display, c->r, GREY8, 0, 0);
296 i = allocimage(display, c->r, outchan, 0, 0);
298 fprint(2, "jpg: allocimage %s failed: %r\n", name);
301 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
302 fprint(2, "jpg: loadimage %s failed: %r\n", name);
309 for(j=0; j<r->nchans; j++)
316 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
318 draw(screen, screen->clipr, display->white, nil, ZP);
323 chantostr(buf, outchan);
324 print("%11s %11d %11d %11d %11d ", buf,
325 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
326 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
327 fprint(2, "jpg: %s: write error %r\n", name);
331 if(writerawimage(1, c) < 0){
332 fprint(2, "jpg: %s: write error: %r\n", name);
337 for(j=0; j<r->nchans; j++)