git » fp-git.git » commit c2b9b6a

coded pop2 n colors palettes but not already hooked or tested

author ecalot
2006-03-07 12:07:00 UTC
committer ecalot
2006-03-07 12:07:00 UTC
parent d9db3fe4b6b4e2b082fe6e823bede724dbccf72c

coded pop2 n colors palettes but not already hooked or tested

PR/src/Makefile +5 -1
PR/src/include/autodetect.h +1 -0
PR/src/include/memory.h +3 -0
PR/src/include/palette.h +7 -0
PR/src/lib/layers/autodetect.c +6 -5
PR/src/lib/layers/memory.c +13 -1
PR/src/lib/object/palette/pop1_16c.c +0 -7
PR/src/lib/object/palette/pop2_256c.c +36 -212

diff --git a/PR/src/Makefile b/PR/src/Makefile
index 4316bdd..02e2cba 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -56,7 +56,7 @@ XML      = .parse.o .search.o .unknown.o .translate.o .tree.o
 COMPRESS = .lzg_compress.o .lzg_uncompress.o .rle_compress.o .rle_uncompress.o .rlec_uncompress.o 
 LAYERS   = .dat.o .memory.o .list.o .reslist.o .pallist.o .disk.o .idlist.o .autodetect.o .stringformat.o .resourcematch.o
 FORMAT   = .bmp.o .mid.o .pal.o .wav.o .plv.o
-OBJECT   = .object.o .pop1_4bit.o .image2.o .image16.o .image256.o .binary.o .sounds.o .level.o
+OBJECT   = .object.o .pop2_256c.o .pop1_4bit.o .image2.o .image16.o .image256.o .binary.o .sounds.o .level.o
 CONSOLE  = .main.o .filedir.o
 MAIN     = .pr.o
 
@@ -238,6 +238,10 @@ $(XMLFILE): xml/resources.xml
 	$(INFO) Compiling unknown string format parsing feature...
 	$(CC) $(OPTIONS) -c lib/layers/stringformat.c -o $@
 
+.pop2_256c.o: lib/object/palette/pop2_256c.c include/common.h
+	$(INFO) Compiling the POP2 256 colors palette class support...
+	$(CC) $(OPTIONS) -c  lib/object/palette/pop2_256c.c -o $@
+
 .pop1_4bit.o: lib/object/palette/pop1_4bit.c include/common.h
 	$(INFO) Compiling the POP1 16 colors palette class support...
 	$(CC) $(OPTIONS) -c  lib/object/palette/pop1_4bit.c -o $@
diff --git a/PR/src/include/autodetect.h b/PR/src/include/autodetect.h
index 29c04ff..24fd4f5 100644
--- a/PR/src/include/autodetect.h
+++ b/PR/src/include/autodetect.h
@@ -40,5 +40,6 @@ autodetect.h: Princed Resources : Resource automatic detection headers
 
 /* Verify  header */
 tResourceType verifyHeader(tBinary c);
+int isA64kPalette(tBinary c);
 
 #endif
diff --git a/PR/src/include/memory.h b/PR/src/include/memory.h
index 996aefd..8876494 100644
--- a/PR/src/include/memory.h
+++ b/PR/src/include/memory.h
@@ -58,4 +58,7 @@ void str5lowercpy (char* dst,const char* src);
 int matchesIn(const char *s, const char *p);
 int getOrder(const char* order);
 
+#include "binary.h"
+tBinary tbinaryallocandcopy(tBinary c);
+
 #endif
diff --git a/PR/src/include/palette.h b/PR/src/include/palette.h
index 6c2aa92..707913a 100644
--- a/PR/src/include/palette.h
+++ b/PR/src/include/palette.h
@@ -69,4 +69,11 @@ int objPop1Palette4bitsSet(void* o,tResource* res);
 
 tColor* objPalette_pop1_4bitsGetColors(void* o);
 
+/* middle layer */
+#define to8bits_A(a) (((a)<<2)|((a)>>4))
+#define to8bits_B(a) (((a)<<2)         )
+#define to8bits_C(a) (((a)<<2)+2       )
+
+#define convert24to18(x) (unsigned char)((x+2)>>2);
+
 #endif
diff --git a/PR/src/lib/layers/autodetect.c b/PR/src/lib/layers/autodetect.c
index eec642e..f17aca7 100644
--- a/PR/src/lib/layers/autodetect.c
+++ b/PR/src/lib/layers/autodetect.c
@@ -78,12 +78,12 @@ int verifyPaletteHeaderPop1(tBinary c) {
 	);
 }
 
-int verifyPaletteHeaderPop2(tBinary c) {
-	tBinary c2;
+int verifyPaletteHeaderPop2_256(tBinary c) {
+/*	tBinary c2;
 	c2.size=c.size-1;
-	c2.data=c.data+1;
+	c2.data=c.data+1;*/
 	return (
-		((c.size==(256*3+1)||c.size==(320*3+1))&&isA64kPalette(c2))
+		((c.size==(256*3)||c.size==(320*3))&&isA64kPalette(c))
 	);
 }
 
@@ -119,7 +119,8 @@ tResourceType verifyHeader(tBinary c) { /* TODO: add the pop version as another
 	if (verifyImage16Header  (c)) return eResTypeImage16;
 	if (verifyImage256Header (c)) return eResTypeImage256;
 	if (verifyPaletteHeaderPop1  (c)) return eResTypePop1Palette4bits;
-	if (verifyPaletteHeaderPop2  (c)) return eResTypePop2Palette4bits;
+/*	if (verifyPaletteHeaderPop2  (c)) return eResTypePop2Palette4bits;*/
+	if (verifyPaletteHeaderPop2_256  (c)) return eResTypePop2Palette320colors;
 	if (verifyWaveHeader     (c)) return eResTypeWave;
 	if (verifySpeakerHeader  (c)) return eResTypePcspeaker;
 	return eResTypeBinary;
diff --git a/PR/src/lib/layers/memory.c b/PR/src/lib/layers/memory.c
index e88d843..2e0cc9d 100644
--- a/PR/src/lib/layers/memory.c
+++ b/PR/src/lib/layers/memory.c
@@ -32,6 +32,7 @@ memory.c: Princed Resources : Memory handling and comparing routines
 */
 
 #include "memory.h"
+#include "binary.h"
 #include <string.h>
 
 char* strallocandcopy(const char* text) {
@@ -44,7 +45,7 @@ char* strallocandcopy(const char* text) {
 	return aux;
 }
 
-unsigned char* binaryallocandcopy(const unsigned char* bin,int size) {
+unsigned char* binaryallocandcopy(const unsigned char* bin,int size) { /* TODO: replace by the next function */
 	unsigned char* aux;
 	if (bin==NULL) return NULL;
 	aux=(unsigned char*)malloc(size);
@@ -52,6 +53,17 @@ unsigned char* binaryallocandcopy(const unsigned char* bin,int size) {
 	return aux;
 }
 
+tBinary tbinaryallocandcopy(tBinary c) {
+	tBinary aux;
+	if (c.data==NULL) return c;
+
+	aux.data=(unsigned char*)malloc(c.size);
+	aux.size=c.size;
+	
+	if (aux.data) memcpy(aux.data,c.data,c.size);
+	return aux;
+}
+
 void str5lowercpy (char* dst,const char* src) {
 	int i;
 	for (i=0;(i<4)&&(*src);i++) {
diff --git a/PR/src/lib/object/palette/pop1_16c.c b/PR/src/lib/object/palette/pop1_16c.c
index b41669c..f42e715 100644
--- a/PR/src/lib/object/palette/pop1_16c.c
+++ b/PR/src/lib/object/palette/pop1_16c.c
@@ -115,11 +115,6 @@ tPalette createPalette() {
 }
 
 
-/* middle layer */
-#define to8bits_A(a) (((a)<<2)|((a)>>4))
-#define to8bits_B(a) (((a)<<2)         )
-#define to8bits_C(a) (((a)<<2)+2       )
-
 /* reads the information in data and tries to remember it in the palette */
 int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 	tColor c[256];
@@ -271,8 +266,6 @@ void* objPop1Palette4bitsRead(const char* file,int *result) {
 	return (void*)pal;
 }
 
-#define convert24to18(x) (unsigned char)((x+2)>>2);
-
 int objPop1Palette4bitsSet(void* o,tResource* res) {
 	tPop1_4bitsPalette* pal=o;
 	int i;
diff --git a/PR/src/lib/object/palette/pop2_256c.c b/PR/src/lib/object/palette/pop2_256c.c
index b41669c..beba842 100644
--- a/PR/src/lib/object/palette/pop2_256c.c
+++ b/PR/src/lib/object/palette/pop2_256c.c
@@ -42,239 +42,63 @@ palette.c: Princed Resources : The palette object implementation
 #include "memory.h"
 #include "dat.h"
 #include "disk.h" /* writeData */
+#include "autodetect.h" /* isA64kPalette */
 
 #include "pal.h"
 
-void addPop1Raw(tPalette* p,unsigned char* data, int dataSize);
-
-/***************************************************************\
-|                         Palette Object                        |
-\***************************************************************/
-
-static tColor sample_pal4[]={{0,0,0},{4,4,4}}; /*SAMPLE_PAL16;*/
-
-int setPalette(tPalette* p, int bits, tColor* palette) {
-	switch(bits) {
-	case 1:
-		memcpy(p->p1,palette,2*sizeof(tColor));
-		break;
-	case 4:
-		memcpy(p->p4,palette,16*sizeof(tColor));
-		break;
-	case 8:
-		memcpy(p->p8,palette,256*sizeof(tColor));
-		break;
-	default:
-		return -1; /* unsupported bit rate */
-	}
-	return 0;
-}
-
-int getPalette(const tPalette* p, int bits, const tColor** apalette) {
-	switch(bits) {
-	case 1:
-		*apalette=p->p1;
-		break;
-	case 4:
-		*apalette=p->p4;
-		break;
-	case 8:
-		*apalette=p->p8;
-		break;
-	default:
-		return -1; /* unsupported bit rate */
-	}
-	return 0;
-}
-
-tPalette createPalette() {
-	tPalette r;
-	int i;
-
-	/* Palette 1 bit */
-	r.p1[0].r=0;
-	r.p1[0].g=0;
-	r.p1[0].b=0;
-	r.p1[1].r=255;
-	r.p1[1].g=255;
-	r.p1[1].b=255;
-
-	/* Palette 4 bits */
-	memcpy(r.p4, sample_pal4, 16);
-
-	/* Palette 8 bits */
-	for (i=0;i<256;i++) {
-		r.p8[i].r=i;
-		r.p8[i].g=i;
-		r.p8[i].b=i;
-	}
-
-	/* initialize the rest */
-	r.pop1raw=NULL;
-	return r;
-}
-
-
-/* middle layer */
-#define to8bits_A(a) (((a)<<2)|((a)>>4))
-#define to8bits_B(a) (((a)<<2)         )
-#define to8bits_C(a) (((a)<<2)+2       )
-
-/* reads the information in data and tries to remember it in the palette */
-int readPalette(tPalette* p, unsigned char* data, int dataSize) {
-	tColor c[256];
-	int i,bits=0;
-	*p=createPalette();
-	printf("reading a palette from data (%d)\n",dataSize);
-	/* TODO: validate checksum */
-
-	switch (dataSize) {
-	case 101:
-		for (i=0;i<16;i++) {
-			c[i].r=data[(i*3)+5]<<2;
-			c[i].g=data[(i*3)+6]<<2;
-			c[i].b=data[(i*3)+7]<<2;
-		}
-		bits=4;
-		/* this palette needs to be remembered as binary */
-		/*addPop1Raw(p,data+1,dataSize-1);*/
-		break;
-	case 3*256+1:
-	case 3*320+1:
-		for (i=0;i<256;i++) {
-			c[i].r=data[(i*3)+1]<<2;
-			c[i].g=data[(i*3)+2]<<2;
-			c[i].b=data[(i*3)+3]<<2;
-		}
-		bits=8;
-		break;
-	}
-		
-	if (bits) setPalette(p,bits,c);
-	return bits;
-}
-/*
-int applyPalette(tPalette* p,tImage *i) {
-	i->pal=*p;
-	return 0;
-}
+typedef struct { 
+	tColor* colorArray;
+	int size;
+}tGenericPalette;
 
-void addPop1Raw(tPalette* p,unsigned char* data, int dataSize) {
-	freeAllocation(p->pop1raw);
-	p->pop1raw=binaryallocandcopy(data,dataSize);
-	p->pop1rawSize=dataSize;
-}
-*/
+void* objPop2PaletteNColorsCreate(tBinary cont, int *error) {
+	tGenericPalette* r;
+	int i,j;
 
-typedef struct { 
-	tColor c[16];
-	unsigned char raw[100];
-}tPop1_4bitsPalette;
+	*error=PR_RESULT_SUCCESS;
 
-void* objPalette_pop1_4bitsCreate(tBinary cont, int *error) {
-	int i;
-	tPop1_4bitsPalette* pal;
-	
-	if (cont.size!=100) {
-		*error=PR_RESULT_XML_AND_DAT_FORMAT_DO_NOT_MATCH;
+	if (!isA64kPalette(cont)) {
+		*error=-14; /* TODO FIX or assign error code */
 		return NULL;
 	}
-
-	pal=(tPop1_4bitsPalette*)malloc(sizeof(tPop1_4bitsPalette));
 	
-	for (i=0;i<16;i++) {
-		pal->c[i].r=cont.data[(i*3)+4]<<2;
-		pal->c[i].g=cont.data[(i*3)+5]<<2;
-		pal->c[i].b=cont.data[(i*3)+6]<<2;
+	r=(tGenericPalette*)malloc(sizeof(tGenericPalette));
+	r->colorArray=(tColor*)malloc(sizeof(tColor)*((cont.size+2)/3));
+	for (i=0,j=0;i<cont.size;i+=3,j++) {
+		r->colorArray[j].r=to8bits_B(cont.data[i+0]);
+		r->colorArray[j].g=to8bits_B(cont.data[i+1]);
+		r->colorArray[j].b=to8bits_B(cont.data[i+2]);
 	}
-
-	memcpy(pal->raw,cont.data,100);
-
-	*error=PR_RESULT_SUCCESS;
+	if (j!=256 || j!=320) return NULL; /*TODO: add free */
+	r->size=j;
 	
-	return (void*)pal;
-}
-
-int objPalette_pop1_4bitsWrite(void* o, const char* file, int optionflag, const char* backupExtension) {
-	tPop1_4bitsPalette* pal=o;
-	char aux[260];
-
-	/* Export extra palette information */
-	sprintf(aux,"%s.more",file);
-	writeData(pal->raw,0,aux,100,optionflag,backupExtension);
-
-	return writePal(file,16,pal->c,optionflag,backupExtension);
+	return (void*)r;
 }
 
-tColor* objPalette_pop1_4bitsGetColors(void* o) {
-	tPop1_4bitsPalette* pal=o;
-	return pal->c;
+int objPop2PaletteNColorsWrite(void* o, const char* file, int optionflag, const char* backupExtension) {
+	tGenericPalette* p=o;
+	return writePal(file,p->size,p->colorArray,optionflag,backupExtension);
 }
 
-tColor* paletteGetColorArrayForColors(int colors) {
-	static tColor p1[2];
-	static tColor p4[2];
-	static tColor p8[2];
-	int i;
-
-	switch(colors) {
-	case 2:
-		/* Palette 1 bit */
-		p1[0].r=0;
-		p1[0].g=0;
-		p1[0].b=0;
-		p1[1].r=255;
-		p1[1].g=255;
-		p1[1].b=255;
-		return p1;
-	case 16:
-		/* Palette 4 bits */
-		memcpy(p4, sample_pal4, 16);
-		return p4;
-	case 256:
-		/* Palette 8 bits */
-		for (i=0;i<256;i++) {
-			p8[i].r=i;
-			p8[i].g=i;
-			p8[i].b=i;
-		}
-		return p8;
-	default:
-		return NULL; /* unsupported bit rate */
-	}
-}
-
-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);
+void* objPop2PaletteNColorsRead(const char* file,int *result) {
+	tGenericPalette *r;
+	
+	r=(tGenericPalette*)malloc(sizeof(tGenericPalette));
+				
+	*result=readPal(file,&r->colorArray,&r->size);
 
-	if (*result==PR_RESULT_SUCCESS && colors!=16) {
+	if (*result==PR_RESULT_SUCCESS && !(r->size==256 || r->size==320)) {
 		*result=PR_WRONG_PALETTE_COLOR_COUNT;
-		free(colorArray);
+		free(r->colorArray);
+		free(r);
 		return NULL;
 	}
-	memcpy(pal->c,colorArray,sizeof(tColor)*16);
-	free(colorArray);
 	
-	return (void*)pal;
+	return (void*)r;
 }
 
-#define convert24to18(x) (unsigned char)((x+2)>>2);
-
-int objPop1Palette4bitsSet(void* o,tResource* res) {
-	tPop1_4bitsPalette* pal=o;
+int objPop2PaletteNColorsSet(void* o,tResource* res) {
+/*	tPop2_ncolorPalette* pal=o;
 	int i;
 
 	res->content.size=100;
@@ -286,7 +110,7 @@ int objPop1Palette4bitsSet(void* o,tResource* res) {
 	}
 	res->content.size=100;
 	res->content.data=pal->raw;
-	mWriteFileInDatFile(res);
+	mWriteFileInDatFile(res);*/
 	return PR_RESULT_SUCCESS;
 }