Fix renderer, add image decode/code methods

This commit is contained in:
Simon Robertshaw 2011-05-28 13:49:07 +01:00
parent ab7cd2e5f6
commit 2c7b8a6902
3 changed files with 138 additions and 14 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
@ -1346,20 +1347,34 @@ int main(int argc, char *argv[])
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);
}
vid_buf+=XRES+BARSIZE;
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;
}
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