diff options
Diffstat (limited to 'src/graphics.c')
| -rw-r--r-- | src/graphics.c | 105 |
1 files changed, 105 insertions, 0 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; |
