123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- #include <u.h>
- #include <lib9.h>
- #include <bio.h>
- #include <draw.h>
- #include <event.h>
- #include <keyboard.h>
- #include "imagefile.h"
- int cflag = 0;
- int dflag = 0;
- int eflag = 0;
- int nineflag = 0;
- int threeflag = 0;
- int output = 0;
- uint32_t outchan = CMAP8;
- Image **allims;
- int which;
- int defaultcolor = 1;
- enum{
- Border = 2,
- Edge = 5
- };
- char *show(int, char*);
- Rectangle
- imager(void)
- {
- Point p1, p2;
- if(allims==nil || allims[0]==nil)
- return screen->r;
- p1 = addpt(divpt(subpt(allims[0]->r.max, allims[0]->r.min), 2), allims[0]->r.min);
- p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
- return rectaddpt(allims[0]->r, subpt(p2, p1));
- }
- void
- eresized(int new)
- {
- Rectangle r;
- if(new && getwindow(display, Refnone) < 0){
- fprint(2, "gif: can't reattach to window\n");
- exits("resize");
- }
- if(allims==nil || allims[which]==nil)
- return;
- r = imager();
- border(screen, r, -Border, nil, ZP);
- draw(screen, r, allims[which], nil, allims[which]->r.min);
- flushimage(display, 1);
- }
- void
- main(int argc, char *argv[])
- {
- int fd, i;
- char *err;
- ARGBEGIN{
- case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
- threeflag++;
- /* fall through */
- case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
- cflag++;
- dflag++;
- output++;
- defaultcolor = 0;
- outchan = RGB24;
- break;
- case 'c': /* produce encoded, compressed, bitmap file; no display by default */
- cflag++;
- dflag++;
- output++;
- if(defaultcolor)
- outchan = CMAP8;
- break;
- case 'd': /* suppress display of image */
- dflag++;
- break;
- case 'e': /* disable floyd-steinberg error diffusion */
- eflag++;
- break;
- case 'k': /* force black and white */
- defaultcolor = 0;
- outchan = GREY8;
- break;
- case 'v': /* force RGBV */
- defaultcolor = 0;
- outchan = CMAP8;
- break;
- case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
- nineflag++;
- dflag++;
- output++;
- if(defaultcolor)
- outchan = CMAP8;
- break;
- default:
- fprint(2, "usage: gif -39cdektv [file.gif ...]\n");
- exits("usage");
- }ARGEND;
- err = nil;
- if(argc == 0)
- err = show(0, "<stdin>");
- else{
- for(i=0; i<argc; i++){
- fd = sys_open(argv[i], OREAD);
- if(fd < 0){
- fprint(2, "gif: can't open %s: %r\n", argv[i]);
- err = "open";
- }else{
- err = show(fd, argv[i]);
- sys_close(fd);
- }
- if(output && argc>1 && err==nil){
- fprint(2, "gif: exiting after one file\n");
- break;
- }
- }
- }
- exits(err);
- }
- Image*
- transparency(Rawimage *r, char *name)
- {
- Image *i;
- int j, index;
- unsigned char *pic, *mpic, *mask;
- if((r->gifflags&TRANSP) == 0)
- return nil;
- i = allocimage(display, r->r, GREY8, 0, 0);
- if(i == nil){
- fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
- return nil;
- }
- pic = r->chans[0];
- mask = malloc(r->chanlen);
- if(mask == nil){
- fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
- freeimage(i);
- return nil;
- }
- index = r->giftrindex;
- mpic = mask;
- for(j=0; j<r->chanlen; j++)
- if(*pic++ == index)
- *mpic++ = 0;
- else
- *mpic++ = 0xFF;
- if(loadimage(i, i->r, mask, r->chanlen) < 0){
- fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
- free(mask);
- freeimage(i);
- return nil;
- }
- free(mask);
- return i;
- }
- /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
- unsigned char*
- expand(unsigned char *u, int chanlen, int nchan)
- {
- int j, k;
- unsigned char *v, *up, *vp;
- v = malloc(chanlen*(nchan+1));
- if(v == nil){
- fprint(2, "gif: malloc fails: %r\n");
- exits("malloc");
- }
- up = u;
- vp = v;
- for(j=0; j<chanlen; j++){
- *vp++ = 0xFF;
- for(k=0; k<nchan; k++)
- *vp++ = *up++;
- }
- return v;
- }
- void
- addalpha(Rawimage *i)
- {
- char buf[32];
- switch(outchan){
- case CMAP8:
- i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
- i->chanlen = 2*(i->chanlen/1);
- i->chandesc = CRGBVA16;
- outchan = CHAN2(CMap, 8, CAlpha, 8);
- break;
- case GREY8:
- i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
- i->chanlen = 2*(i->chanlen/1);
- i->chandesc = CYA16;
- outchan = CHAN2(CGrey, 8, CAlpha, 8);
- break;
- case RGB24:
- i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
- i->chanlen = 4*(i->chanlen/3);
- i->chandesc = CRGBA32;
- outchan = RGBA32;
- break;
- default:
- chantostr(buf, outchan);
- fprint(2, "gif: can't add alpha to type %s\n", buf);
- exits("err");
- }
- }
- /*
- * Called only when writing output. If the output is RGBA32,
- * we must write four bytes per pixel instead of two.
- * There's always at least two: data plus alpha.
- * r is used only for reference; the image is already in c.
- */
- void
- blackout(Rawimage *r, Rawimage *c)
- {
- int i, trindex;
- unsigned char *rp, *cp;
- rp = r->chans[0];
- cp = c->chans[0];
- trindex = r->giftrindex;
- if(outchan == RGBA32)
- for(i=0; i<r->chanlen; i++){
- if(*rp == trindex){
- *cp++ = 0x00;
- *cp++ = 0x00;
- *cp++ = 0x00;
- *cp++ = 0x00;
- }else{
- *cp++ = 0xFF;
- cp += 3;
- }
- rp++;
- }
- else
- for(i=0; i<r->chanlen; i++){
- if(*rp == trindex){
- *cp++ = 0x00;
- *cp++ = 0x00;
- }else{
- *cp++ = 0xFF;
- cp++;
- }
- rp++;
- }
- }
- int
- init(void)
- {
- static int inited;
- if(inited == 0){
- if(initdraw(0, 0, 0) < 0){
- fprint(2, "gif: initdraw failed: %r\n");
- return -1;
- }
- einit(Ekeyboard|Emouse);
- inited++;
- }
- return 1;
- }
- char*
- show(int fd, char *name)
- {
- Rawimage **images, **rgbv;
- Image *tmp, *msk, *img, *dst, **ims;
- Rectangle r;
- int j, k, n, ch, nloop, loopcount, dt;
- char *err;
- char buf[32];
- err = nil;
- images = readgif(fd, CRGB, dflag);
- if(images == nil){
- fprint(2, "gif: decode %s failed: %r\n", name);
- return "decode";
- }
- for(n=0; images[n]; n++)
- if(n == 0)
- r = images[n]->r;
- else
- combinerect(&r, images[n]->r);
- tmp = nil;
- ims = malloc((n+1)*sizeof(Image*));
- rgbv = malloc((n+1)*sizeof(Rawimage*));
- if(rgbv==nil || ims==nil){
- fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
- err = "malloc";
- goto Return;
- }
- memset(ims, 0, (n+1)*sizeof(Image*));
- memset(rgbv, 0, (n+1)*sizeof(Rawimage*));
- if(!dflag){
- if(init() < 0){
- err = "initdraw";
- goto Return;
- }
- if(defaultcolor && screen->depth>8)
- outchan = RGB24;
- }
- for(k=0; k<n; k++){
- if(outchan == CMAP8)
- rgbv[k] = torgbv(images[k], !eflag);
- else{
- if(outchan==GREY8 || (images[k]->chandesc==CY && threeflag==0))
- rgbv[k] = totruecolor(images[k], CY);
- else
- rgbv[k] = totruecolor(images[k], CRGB24);
- }
- if(rgbv[k] == nil){
- fprint(2, "gif: converting %s to local format failed: %r\n", name);
- err = "torgbv";
- goto Return;
- }
- if(!dflag){
- msk = transparency(images[k], name);
- if(rgbv[k]->chandesc == CY)
- img = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
- else
- img = allocimage(display, rgbv[k]->r, outchan, 0, 0);
- if(tmp == nil)
- tmp = allocimage(display, r, img->chan, 0, DWhite);
- ims[k]= dst = allocimage(display, r, tmp->chan, 0, DWhite);
- if(tmp == nil || img == nil || dst == nil){
- fprint(2, "gif: allocimage %s failed: %r\n", name);
- err = "allocimage";
- goto Return;
- }
- if(loadimage(img, img->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
- fprint(2, "gif: loadimage %s failed: %r\n", name);
- err = "loadimage";
- goto Return;
- }
- switch((images[k]->gifflags>>2)&7){
- default:
- draw(tmp, img->r, img, msk, img->r.min);
- draw(dst, tmp->r, tmp, nil, tmp->r.min);
- break;
- case 2:
- draw(tmp, img->r, display->white, msk, img->r.min);
- /* no break */
- case 3:
- draw(dst, tmp->r, tmp, nil, tmp->r.min);
- draw(dst, img->r, img, msk, img->r.min);
- break;
- }
- freeimage(msk);
- freeimage(img);
- }
- }
- if(tmp)
- freeimage(tmp);
- allims = ims;
- loopcount = images[0]->gifloopcount;
- if(!dflag){
- nloop = 0;
- do{
- for(k=0; k<n; k++){
- which = k;
- eresized(0);
- dt = images[k]->gifdelay*10;
- if(dt < 50)
- dt = 50;
- while(n==1 || ecankbd()){
- /* an odd, democratic list */
- if((ch=ekbd())=='q' || ch==Kdel || ch==Keof)
- exits(nil);
- if(ch == '\n')
- goto Out;
- }sleep(dt);
- }
- /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
- }while(loopcount==0 || ++nloop<loopcount);
- /* loop count has run out */
- ekbd();
- Out:
- draw(screen, screen->clipr, display->white, nil, ZP);
- }
- if(nineflag){
- if(images[0]->gifflags&TRANSP){
- addalpha(rgbv[0]);
- blackout(images[0], rgbv[0]);
- }
- chantostr(buf, outchan);
- print("%11s %11d %11d %11d %11d ", buf,
- rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
- if(jehanne_write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
- fprint(2, "gif: %s: write error %r\n", name);
- return "write";
- }
- }else if(cflag){
- if(images[0]->gifflags&TRANSP){
- addalpha(rgbv[0]);
- blackout(images[0], rgbv[0]);
- }
- if(writerawimage(1, rgbv[0]) < 0){
- fprint(2, "gif: %s: write error: %r\n", name);
- return "write";
- }
- }
- Return:
- allims = nil;
- for(k=0; images[k]; k++){
- for(j=0; j<images[k]->nchans; j++)
- free(images[k]->chans[j]);
- free(images[k]->cmap);
- if(rgbv[k])
- free(rgbv[k]->chans[0]);
- freeimage(ims[k]);
- free(images[k]);
- free(rgbv[k]);
- }
- free(images);
- free(ims);
- return err;
- }
|