- char tagid[4];
- int len;
- U8*data;
- U8*imagedata;
- U8*zimagedata=0;
- unsigned long int imagedatalen;
- unsigned long int zimagedatalen=0;
- U8*palette = 0;
- int palettelen = 0;
- U8*alphapalette = 0;
- int alphapalettelen = 0;
- struct png_header header;
- int bypp;
-
- FILE *fi;
- U8 *scanline;
-
- if ((fi = fopen(sname, "rb")) == NULL) {
- if (VERBOSE(1))
- fprintf(stderr, "Read access failed: %s\n", sname);
- return t;
- }
-
- if(!png_read_header(fi, &header))
- return 0;
-
- if(header.mode == 3 || header.mode == 0) bypp = 1;
- else
- if(header.mode == 2) bypp = 3;
- else
- if(header.mode == 6) bypp = 4;
- else
- return 0;
- imagedatalen = bypp * header.width * header.height + 65536;
- imagedata = malloc(imagedatalen);
-
- fseek(fi,8,SEEK_SET);
- while(!feof(fi))
- {
- if(!png_read_chunk(&tagid, &len, &data, fi))
- break;
- if(!strncmp(tagid, "IEND", 4)) {
- break;
- }
- if(!strncmp(tagid, "PLTE", 4)) {
- palette = data;
- palettelen = len/3;
- data = 0; //don't free data
- if(VERBOSE(2))
- printf("%d colors in palette\n", palettelen);
- }
- if(!strncmp(tagid, "tRNS", 4)) {
- if(header.mode == 3) {
- alphapalette = data;
- alphapalettelen = len;
- data = 0; //don't free data
- if(VERBOSE(2))
- printf("found %d alpha colors\n", alphapalettelen);
- }
- }
- if(!strncmp(tagid, "IDAT", 4)) {
- if(!zimagedata) {
- zimagedatalen = len;
- zimagedata = malloc(len);
- memcpy(zimagedata,data,len);
- } else {
- zimagedata = realloc(zimagedata, zimagedatalen+len);
- memcpy(&zimagedata[zimagedatalen], data, len);
- zimagedatalen += len;
- }
- }
- if(data)
- free(data);
- }
-
- if(!zimagedata || uncompress(imagedata, &imagedatalen, zimagedata, zimagedatalen) != Z_OK) {
- fprintf(stderr, "Couldn't uncompress %s!\n", sname);
- if(zimagedata)
- free(zimagedata);
- return 0;
- }
- free(zimagedata);
-
- if(alphapalette)
- t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS2);
- else
- t = swf_InsertTag(t, ST_DEFINEBITSLOSSLESS);