git » fp-git.git » commit f571093

added better pop1 palette support and bugfixed

author ecalot
2006-03-05 05:43:07 UTC
committer ecalot
2006-03-05 05:43:07 UTC
parent be091c802cd3c9349b14117bdcae36f12a019975

added better pop1 palette support and bugfixed

PR/src/lib/formats/bmp.c +3 -4
PR/src/lib/formats/pal.c +9 -11
PR/src/lib/object/image/image16.c +0 -1
PR/src/lib/object/image/image2.c +0 -1
PR/src/lib/object/image/image256.c +0 -1
PR/src/lib/object/image/image_common.c +0 -1
PR/src/lib/object/palette/pop1_16c.c +29 -10
PR/src/lib/object/palette/pop2_256c.c +29 -10
PR/src/lib/object/palette/pop2_4bit.c +29 -10

diff --git a/PR/src/lib/formats/bmp.c b/PR/src/lib/formats/bmp.c
index f6f388b..72ccb8c 100644
--- a/PR/src/lib/formats/bmp.c
+++ b/PR/src/lib/formats/bmp.c
@@ -156,7 +156,7 @@ int readBmp(const char* file, unsigned char** data, int *ph, int *pw,  int *pbit
 	ok=ok&&freadlong(&aux      ,bitmap);    /* Important colours (junk)    */
 	
 	/* Verify */
-	*plineWidth=width*bits/8; /* Note: only works in bits=1,2,4,8 */
+	*plineWidth=(width*bits+7)/8; /* Note: only works in bits=1,2,4,8 */
 	lineSerialization=(-*plineWidth)&3;
 /*	offset=54+(colors<<2);
 	lineSerialization=(-lineWidth)&3;
@@ -178,9 +178,8 @@ int readBmp(const char* file, unsigned char** data, int *ph, int *pw,  int *pbit
 	*data=malloc((*plineWidth+lineSerialization)*height);
 	while (height--) {
 		ok=ok&&fread(*data+height**plineWidth,*plineWidth,1,bitmap);
-		ok=ok&&fread(&aux,lineSerialization,1,bitmap);
+		if (lineSerialization) ok=ok&&fread(&aux,lineSerialization,1,bitmap);
 	}
-
 	
 #if 0
 	/* Validate if there is header and if it starts in BM */
@@ -232,6 +231,6 @@ int readBmp(const char* file, unsigned char** data, int *ph, int *pw,  int *pbit
 	
 	*pbits        = bits;
 	*pcolors      = colours;
-	
+
 	return PR_RESULT_SUCCESS;			
 }
diff --git a/PR/src/lib/formats/pal.c b/PR/src/lib/formats/pal.c
index 997fd8f..d5974f2 100644
--- a/PR/src/lib/formats/pal.c
+++ b/PR/src/lib/formats/pal.c
@@ -70,7 +70,7 @@ int writePal(const char* file, int colors, const tColor* colorArray, int optionf
 
 int readPal(const char* file,tColor* *colorArray,int *colors) {
 	FILE* fd;
-	int ok,i;
+	int ok=0,i;
 	int r;
 	int g;
 	int b;
@@ -79,18 +79,16 @@ int readPal(const char* file,tColor* *colorArray,int *colors) {
 	if (!fd) return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; 
 	/* TODO: do the reading */
 
-	ok=fscanf(fd,"JASC_PAL\r\n");
-	fscanf(fd,"0100\r\n");
-	fscanf(fd,"%d\r\n",colors);
-	
-	if (*colors<1000) return -40;
+	if (fscanf(fd,"JASC-PAL\n0100\n%d\n",colors)!=1)
+		return -40;
+
+	if (*colors>1000) return -40;
 	*colorArray=malloc(sizeof(tColor)**colors);
-	for (i=0;i<*colors;i++) {
-		fscanf(fd,"%d %d %d\r\n",&r,&g,&b);
+	for (i=0; (i<*colors) && (ok=(fscanf(fd,"%d %d %d\n",&r,&g,&b)==3)) ;i++) {
 		/* Those lines mean a loss of data (palette colours are saved in the nearest multiple of 4) */
-		(*colorArray)[i].r=(unsigned char)((r+2)>>2); /* TODO: use the conversion macro */
-		(*colorArray)[i].g=(unsigned char)((g+2)>>2);
-		(*colorArray)[i].b=(unsigned char)((b+2)>>2);
+		(*colorArray)[i].r=r; /* TODO: use an aux tColor instead of r,g,b */
+		(*colorArray)[i].g=g;
+		(*colorArray)[i].b=b;
 	}
 	fclose(fd);
 	if (!ok) {
diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c
index d0ec297..15a1e60 100644
--- a/PR/src/lib/object/image/image16.c
+++ b/PR/src/lib/object/image/image16.c
@@ -444,7 +444,6 @@ void* objImageRead(const char* file,tObject palette, int *result) {
 
 	*result=readBmp(file,&(image->pix),&(image->height),&(image->width),&(image->bits),&colors,&colorArray,&(image->widthInBytes));
 	/* check if image was succesfully read loaded */
-printf("result=%d\n",*result);
 	if (*result!=PR_RESULT_SUCCESS) {
 		free(image);
 		return NULL;
diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c
index d0ec297..15a1e60 100644
--- a/PR/src/lib/object/image/image2.c
+++ b/PR/src/lib/object/image/image2.c
@@ -444,7 +444,6 @@ void* objImageRead(const char* file,tObject palette, int *result) {
 
 	*result=readBmp(file,&(image->pix),&(image->height),&(image->width),&(image->bits),&colors,&colorArray,&(image->widthInBytes));
 	/* check if image was succesfully read loaded */
-printf("result=%d\n",*result);
 	if (*result!=PR_RESULT_SUCCESS) {
 		free(image);
 		return NULL;
diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c
index d0ec297..15a1e60 100644
--- a/PR/src/lib/object/image/image256.c
+++ b/PR/src/lib/object/image/image256.c
@@ -444,7 +444,6 @@ void* objImageRead(const char* file,tObject palette, int *result) {
 
 	*result=readBmp(file,&(image->pix),&(image->height),&(image->width),&(image->bits),&colors,&colorArray,&(image->widthInBytes));
 	/* check if image was succesfully read loaded */
-printf("result=%d\n",*result);
 	if (*result!=PR_RESULT_SUCCESS) {
 		free(image);
 		return NULL;
diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c
index d0ec297..15a1e60 100644
--- a/PR/src/lib/object/image/image_common.c
+++ b/PR/src/lib/object/image/image_common.c
@@ -444,7 +444,6 @@ void* objImageRead(const char* file,tObject palette, int *result) {
 
 	*result=readBmp(file,&(image->pix),&(image->height),&(image->width),&(image->bits),&colors,&colorArray,&(image->widthInBytes));
 	/* check if image was succesfully read loaded */
-printf("result=%d\n",*result);
 	if (*result!=PR_RESULT_SUCCESS) {
 		free(image);
 		return NULL;
diff --git a/PR/src/lib/object/palette/pop1_16c.c b/PR/src/lib/object/palette/pop1_16c.c
index 4e9dfcc..b41669c 100644
--- a/PR/src/lib/object/palette/pop1_16c.c
+++ b/PR/src/lib/object/palette/pop1_16c.c
@@ -41,6 +41,7 @@ palette.c: Princed Resources : The palette object implementation
 #include "palette.h"
 #include "memory.h"
 #include "dat.h"
+#include "disk.h" /* writeData */
 
 #include "pal.h"
 
@@ -196,11 +197,11 @@ void* objPalette_pop1_4bitsCreate(tBinary cont, int *error) {
 
 int objPalette_pop1_4bitsWrite(void* o, const char* file, int optionflag, const char* backupExtension) {
 	tPop1_4bitsPalette* pal=o;
-	/*unsigned char* aux=malloc(MAX_FILENAME_SIZE);*/
-	/* TODO: add the write of the extra information */
+	char aux[260];
+
 	/* Export extra palette information */
-	/*sprintf((char*)aux,"%s.more",file);
-	writeData(data,0,(char*)aux,size,optionflag,backupExtension); TODO fix that */
+	sprintf(aux,"%s.more",file);
+	writeData(pal->raw,0,aux,100,optionflag,backupExtension);
 
 	return writePal(file,16,pal->c,optionflag,backupExtension);
 }
@@ -247,7 +248,16 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	tPop1_4bitsPalette* pal=(tPop1_4bitsPalette*)malloc(sizeof(tPop1_4bitsPalette));
 	tColor* colorArray;
 	int colors;
-	
+	char aux[260];
+	tBinary raw;
+
+	/* Import extra palette information */
+	sprintf(aux,"%s.more",file);
+	raw=mLoadFileArray(aux);
+	if (raw.size!=100) return NULL; /* TODO; free memory */
+	memcpy(pal->raw,raw.data,100);
+	free(raw.data);
+
 	*result=readPal(file,&colorArray,&colors);
 
 	if (*result==PR_RESULT_SUCCESS && colors!=16) {
@@ -261,12 +271,21 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	return (void*)pal;
 }
 
+#define convert24to18(x) (unsigned char)((x+2)>>2);
+
 int objPop1Palette4bitsSet(void* o,tResource* res) {
-	tBinary* wave=o;
-	res->content.size=wave->size+1;
-	res->content.data=malloc(wave->size+1);
-	res->content.data[0]=0x01; /* TODO: use WAVE_MAGIC */
-	memcpy(res->content.data+1,wave->data,wave->size);
+	tPop1_4bitsPalette* pal=o;
+	int i;
+
+	res->content.size=100;
+	res->content.data=pal->raw;
+	for (i=0;i<16;i++) {
+		res->content.data[(i*3)+4]=convert24to18(pal->c[i].r);
+		res->content.data[(i*3)+5]=convert24to18(pal->c[i].g);
+		res->content.data[(i*3)+6]=convert24to18(pal->c[i].b);
+	}
+	res->content.size=100;
+	res->content.data=pal->raw;
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }
diff --git a/PR/src/lib/object/palette/pop2_256c.c b/PR/src/lib/object/palette/pop2_256c.c
index 4e9dfcc..b41669c 100644
--- a/PR/src/lib/object/palette/pop2_256c.c
+++ b/PR/src/lib/object/palette/pop2_256c.c
@@ -41,6 +41,7 @@ palette.c: Princed Resources : The palette object implementation
 #include "palette.h"
 #include "memory.h"
 #include "dat.h"
+#include "disk.h" /* writeData */
 
 #include "pal.h"
 
@@ -196,11 +197,11 @@ void* objPalette_pop1_4bitsCreate(tBinary cont, int *error) {
 
 int objPalette_pop1_4bitsWrite(void* o, const char* file, int optionflag, const char* backupExtension) {
 	tPop1_4bitsPalette* pal=o;
-	/*unsigned char* aux=malloc(MAX_FILENAME_SIZE);*/
-	/* TODO: add the write of the extra information */
+	char aux[260];
+
 	/* Export extra palette information */
-	/*sprintf((char*)aux,"%s.more",file);
-	writeData(data,0,(char*)aux,size,optionflag,backupExtension); TODO fix that */
+	sprintf(aux,"%s.more",file);
+	writeData(pal->raw,0,aux,100,optionflag,backupExtension);
 
 	return writePal(file,16,pal->c,optionflag,backupExtension);
 }
@@ -247,7 +248,16 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	tPop1_4bitsPalette* pal=(tPop1_4bitsPalette*)malloc(sizeof(tPop1_4bitsPalette));
 	tColor* colorArray;
 	int colors;
-	
+	char aux[260];
+	tBinary raw;
+
+	/* Import extra palette information */
+	sprintf(aux,"%s.more",file);
+	raw=mLoadFileArray(aux);
+	if (raw.size!=100) return NULL; /* TODO; free memory */
+	memcpy(pal->raw,raw.data,100);
+	free(raw.data);
+
 	*result=readPal(file,&colorArray,&colors);
 
 	if (*result==PR_RESULT_SUCCESS && colors!=16) {
@@ -261,12 +271,21 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	return (void*)pal;
 }
 
+#define convert24to18(x) (unsigned char)((x+2)>>2);
+
 int objPop1Palette4bitsSet(void* o,tResource* res) {
-	tBinary* wave=o;
-	res->content.size=wave->size+1;
-	res->content.data=malloc(wave->size+1);
-	res->content.data[0]=0x01; /* TODO: use WAVE_MAGIC */
-	memcpy(res->content.data+1,wave->data,wave->size);
+	tPop1_4bitsPalette* pal=o;
+	int i;
+
+	res->content.size=100;
+	res->content.data=pal->raw;
+	for (i=0;i<16;i++) {
+		res->content.data[(i*3)+4]=convert24to18(pal->c[i].r);
+		res->content.data[(i*3)+5]=convert24to18(pal->c[i].g);
+		res->content.data[(i*3)+6]=convert24to18(pal->c[i].b);
+	}
+	res->content.size=100;
+	res->content.data=pal->raw;
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }
diff --git a/PR/src/lib/object/palette/pop2_4bit.c b/PR/src/lib/object/palette/pop2_4bit.c
index 4e9dfcc..b41669c 100644
--- a/PR/src/lib/object/palette/pop2_4bit.c
+++ b/PR/src/lib/object/palette/pop2_4bit.c
@@ -41,6 +41,7 @@ palette.c: Princed Resources : The palette object implementation
 #include "palette.h"
 #include "memory.h"
 #include "dat.h"
+#include "disk.h" /* writeData */
 
 #include "pal.h"
 
@@ -196,11 +197,11 @@ void* objPalette_pop1_4bitsCreate(tBinary cont, int *error) {
 
 int objPalette_pop1_4bitsWrite(void* o, const char* file, int optionflag, const char* backupExtension) {
 	tPop1_4bitsPalette* pal=o;
-	/*unsigned char* aux=malloc(MAX_FILENAME_SIZE);*/
-	/* TODO: add the write of the extra information */
+	char aux[260];
+
 	/* Export extra palette information */
-	/*sprintf((char*)aux,"%s.more",file);
-	writeData(data,0,(char*)aux,size,optionflag,backupExtension); TODO fix that */
+	sprintf(aux,"%s.more",file);
+	writeData(pal->raw,0,aux,100,optionflag,backupExtension);
 
 	return writePal(file,16,pal->c,optionflag,backupExtension);
 }
@@ -247,7 +248,16 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	tPop1_4bitsPalette* pal=(tPop1_4bitsPalette*)malloc(sizeof(tPop1_4bitsPalette));
 	tColor* colorArray;
 	int colors;
-	
+	char aux[260];
+	tBinary raw;
+
+	/* Import extra palette information */
+	sprintf(aux,"%s.more",file);
+	raw=mLoadFileArray(aux);
+	if (raw.size!=100) return NULL; /* TODO; free memory */
+	memcpy(pal->raw,raw.data,100);
+	free(raw.data);
+
 	*result=readPal(file,&colorArray,&colors);
 
 	if (*result==PR_RESULT_SUCCESS && colors!=16) {
@@ -261,12 +271,21 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	return (void*)pal;
 }
 
+#define convert24to18(x) (unsigned char)((x+2)>>2);
+
 int objPop1Palette4bitsSet(void* o,tResource* res) {
-	tBinary* wave=o;
-	res->content.size=wave->size+1;
-	res->content.data=malloc(wave->size+1);
-	res->content.data[0]=0x01; /* TODO: use WAVE_MAGIC */
-	memcpy(res->content.data+1,wave->data,wave->size);
+	tPop1_4bitsPalette* pal=o;
+	int i;
+
+	res->content.size=100;
+	res->content.data=pal->raw;
+	for (i=0;i<16;i++) {
+		res->content.data[(i*3)+4]=convert24to18(pal->c[i].r);
+		res->content.data[(i*3)+5]=convert24to18(pal->c[i].g);
+		res->content.data[(i*3)+6]=convert24to18(pal->c[i].b);
+	}
+	res->content.size=100;
+	res->content.data=pal->raw;
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }