From 2c7b8a690246ed4f1a4ff31c8701ce90f81aaad4 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 28 May 2011 13:49:07 +0100 Subject: [PATCH] Fix renderer, add image decode/code methods --- includes/graphics.h | 4 ++ src/graphics.c | 105 ++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 43 ++++++++++++------ 3 files changed, 138 insertions(+), 14 deletions(-) diff --git a/includes/graphics.h b/includes/graphics.h index 90d5d1b4d..17aaa263c 100644 --- a/includes/graphics.h +++ b/includes/graphics.h @@ -48,6 +48,10 @@ extern unsigned int fire_alpha[CELL*3][CELL*3]; extern pixel *fire_bg; extern pixel *pers_bg; +void *ptif_pack(pixel *src, int w, int h, int *result_size); + +pixel *ptif_unpack(void *datain, int size, int *w, int *h); + pixel *rescale_img(pixel *src, int sw, int sh, int *qw, int *qh, int f); void sdl_blit_1(int x, int y, int w, int h, pixel *src, int pitch); diff --git a/src/graphics.c b/src/graphics.c index 39764b1c8..6cc9310dd 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>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