diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2011-05-28 12:49:07 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2011-05-28 12:49:07 (GMT) |
| commit | 2c7b8a690246ed4f1a4ff31c8701ce90f81aaad4 (patch) | |
| tree | b11382adfafdcbd1fe2cd013b8240f1970b9eedc /src | |
| parent | ab7cd2e5f6f63467d806779c6bcea85ecb97c78d (diff) | |
| download | powder-2c7b8a690246ed4f1a4ff31c8701ce90f81aaad4.zip powder-2c7b8a690246ed4f1a4ff31c8701ce90f81aaad4.tar.gz | |
Fix renderer, add image decode/code methods
Diffstat (limited to 'src')
| -rw-r--r-- | src/graphics.c | 105 | ||||
| -rw-r--r-- | src/main.c | 43 |
2 files changed, 134 insertions, 14 deletions
diff --git a/src/graphics.c b/src/graphics.c index 39764b1..6cc9310 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -33,6 +33,111 @@ unsigned int fire_alpha[CELL*3][CELL*3]; pixel *fire_bg; pixel *pers_bg; +void *ptif_pack(pixel *src, int w, int h, int *result_size){ + int i = 0, datalen = (w*h)*3, cx = 0, cy = 0; + unsigned char *red_chan = calloc(1, w*h); + unsigned char *green_chan = calloc(1, w*h); + unsigned char *blue_chan = calloc(1, w*h); + unsigned char *data = malloc(((w*h)*3)+8); + unsigned char *result = malloc(((w*h)*3)+8); + + for(cx = 0; cx<w; cx++){ + for(cy = 0; cy<h; cy++){ + red_chan[w*(cy)+(cx)] = PIXR(src[w*(cy)+(cx)]); + green_chan[w*(cy)+(cx)] = PIXG(src[w*(cy)+(cx)]); + blue_chan[w*(cy)+(cx)] = PIXB(src[w*(cy)+(cx)]); + } + } + + memcpy(data, red_chan, w*h); + memcpy(data+(w*h), green_chan, w*h); + memcpy(data+((w*h)*2), blue_chan, w*h); + free(red_chan); + free(green_chan); + free(blue_chan); + + result[0] = 'P'; + result[1] = 'T'; + result[2] = 'i'; + result[3] = 1; + result[4] = w; + result[5] = w>>8; + result[6] = h; + result[7] = h>>8; + + i -= 8; + + if(BZ2_bzBuffToBuffCompress((char *)(result+8), (unsigned *)&i, (char *)data, datalen, 9, 0, 0) != BZ_OK){ + free(data); + free(result); + return NULL; + } + + *result_size = i+8; + free(data); + return result; +} + +pixel *ptif_unpack(void *datain, int size, int *w, int *h){ + int width, height, i, cx, cy; + unsigned char *red_chan; + unsigned char *green_chan; + unsigned char *blue_chan; + unsigned char *data = datain; + unsigned char *undata; + pixel *result; + if(size<16){ + printf("Image empty\n"); + return NULL; + } + if(!(data[0]=='P' && data[1]=='T' && data[2]=='i')){ + printf("Image header invalid\n"); + return NULL; + } + width = data[4]|(data[5]<<8); + height = data[6]|(data[7]<<8); + + undata = calloc(1, (width*height)*3); + red_chan = calloc(1, width*height); + green_chan = calloc(1, width*height); + blue_chan = calloc(1, width*height); + result = calloc(width*height, PIXELSIZE); + + if (BZ2_bzBuffToBuffDecompress((char *)undata, (unsigned *)&i, (char *)(data+8), size-8, 0, 0)){ + printf("Decompression failure\n"); + free(red_chan); + free(green_chan); + free(blue_chan); + free(undata); + return NULL; + } + if(i != (width*height)*3){ + printf("Result buffer size mismatch\n"); + free(red_chan); + free(green_chan); + free(blue_chan); + free(undata); + return NULL; + } + memcpy(red_chan, undata, width*height); + memcpy(green_chan, undata+(width*height), width*height); + memcpy(blue_chan, undata+((width*height)*2), width*height); + + for(cx = 0; cx<width; cx++){ + for(cy = 0; cy<height; cy++){ + result[width*(cy)+(cx)] = PIXRGB(red_chan[width*(cy)+(cx)], green_chan[width*(cy)+(cx)], blue_chan[width*(cy)+(cx)]); + } + } + + *w = width; + *h = height; + free(red_chan); + free(green_chan); + free(blue_chan); + free(undata); + return result; +} + pixel *rescale_img(pixel *src, int sw, int sh, int *qw, int *qh, int f) { int i,j,x,y,w,h,r,g,b,c; @@ -1322,6 +1322,7 @@ int main(int argc, char *argv[]) parts[NPART-1].life = -1; pfree = 0; + decorations = calloc((XRES+BARSIZE)*YRES, PIXELSIZE); pers_bg = calloc((XRES+BARSIZE)*YRES, PIXELSIZE); fire_bg = calloc(XRES*YRES, PIXELSIZE); @@ -1345,21 +1346,35 @@ int main(int argc, char *argv[]) //return 0; info_box(vid_buf, "Save file invalid or from newer version"); } - - f=fopen(argv[2],"wb"); - fprintf(f,"P6\n%d %d\n255\n",XRES,YRES); - for (j=0; j<YRES; j++) - { - for (i=0; i<XRES; i++) - { - c[0] = PIXR(vid_buf[i]); - c[1] = PIXG(vid_buf[i]); - c[2] = PIXB(vid_buf[i]); - fwrite(c,3,1,f); + + if(!strncmp(argv[3], "pti", 3)){ + char * datares = NULL, *scaled_buf; + int res = 0, sw, sh; + scaled_buf = rescale_img(vid_buf, XRES, YRES, &sw, &sh, 4); + datares = ptif_pack(scaled_buf, sw, sh, &res); + if(datares!=NULL){ + f=fopen(argv[2], "wb"); + fwrite(datares, res, 1, f); + fclose(f); + free(datares); + } + free(scaled_buf); + } else { + f=fopen(argv[2],"wb"); + fprintf(f,"P6\n%d %d\n255\n",XRES,YRES); + for (j=0; j<YRES; j++) + { + for (i=0; i<XRES; i++) + { + c[0] = PIXR(vid_buf[i]); + c[1] = PIXG(vid_buf[i]); + c[2] = PIXB(vid_buf[i]); + fwrite(c,3,1,f); + } + vid_buf+=XRES+BARSIZE; } - vid_buf+=XRES+BARSIZE; + fclose(f); } - fclose(f); return 1; } @@ -1481,7 +1496,7 @@ int main(int argc, char *argv[]) pygood = 0; } #else - printf("python console disabled at compile time."); + printf("python console disabled at compile time.\n"); #endif #ifdef MT |
