git » fp-git.git » commit 6e301cb

bugfixes and better image hooking

author ecalot
2006-02-17 20:45:19 UTC
committer ecalot
2006-02-17 20:45:19 UTC
parent 93eced84e21d11711833b8c09bd313aba27266c3

bugfixes and better image hooking

PR/src/include/bitmap.h +2 -0
PR/src/include/bmp.h +2 -0
PR/src/include/image.h +1 -1
PR/src/include/object.h +5 -0
PR/src/include/png.h +2 -0
PR/src/lib/formats/bmp.c +13 -16
PR/src/lib/object/image/image16.c +19 -2
PR/src/lib/object/image/image2.c +19 -2
PR/src/lib/object/image/image256.c +19 -2
PR/src/lib/object/image/image_common.c +19 -2
PR/src/lib/object/object.c +3 -0
PR/src/lib/object/palette/pop1_16c.c +3 -3
PR/src/lib/object/palette/pop2_256c.c +3 -3
PR/src/lib/object/palette/pop2_4bit.c +3 -3

diff --git a/PR/src/include/bitmap.h b/PR/src/include/bitmap.h
index 3a26232..811cd60 100644
--- a/PR/src/include/bitmap.h
+++ b/PR/src/include/bitmap.h
@@ -45,4 +45,6 @@ int mFormatImportBmp(tResource *res);
 int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupExtension);
 int mReadBitMap(tImage* img,unsigned char* data, int size); /* Note: this will ignore the palette */
 
+int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits, int colors, tColor* colorArray, int lineWidth, int optionflag, const char* backupExtension);
+
 #endif
diff --git a/PR/src/include/bmp.h b/PR/src/include/bmp.h
index 3a26232..811cd60 100644
--- a/PR/src/include/bmp.h
+++ b/PR/src/include/bmp.h
@@ -45,4 +45,6 @@ int mFormatImportBmp(tResource *res);
 int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupExtension);
 int mReadBitMap(tImage* img,unsigned char* data, int size); /* Note: this will ignore the palette */
 
+int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits, int colors, tColor* colorArray, int lineWidth, int optionflag, const char* backupExtension);
+
 #endif
diff --git a/PR/src/include/image.h b/PR/src/include/image.h
index c3cdfe6..e3cfc84 100644
--- a/PR/src/include/image.h
+++ b/PR/src/include/image.h
@@ -55,7 +55,7 @@ typedef struct {
 	int widthInBytes;
 	int height;
 	unsigned char* pix;
-	tPalette pal;
+	tObject pal;
 	unsigned char type; /* XY where X=B if 4 bits or X=0 if 1 bit; Y=algorithm */
 }tImage;
 
diff --git a/PR/src/include/object.h b/PR/src/include/object.h
index f75c138..89fc5ae 100644
--- a/PR/src/include/object.h
+++ b/PR/src/include/object.h
@@ -35,8 +35,13 @@ object.h: Princed Resources : Main item class types and prototypes
 
 #include "reslist.h"
 #include "common.h"
+#include "image.h"
 
 tObject getObject(tResource* r, int* error);
 int writeObject(tObject o, const char* file, int optionflag, const char* backupExtension);
 
+int paletteGetBits(tObject pal);
+int paletteGetColors(tObject pal);
+tColor* paletteGetColorArray(tObject pal);
+
 #endif
diff --git a/PR/src/include/png.h b/PR/src/include/png.h
index 3a26232..811cd60 100644
--- a/PR/src/include/png.h
+++ b/PR/src/include/png.h
@@ -45,4 +45,6 @@ int mFormatImportBmp(tResource *res);
 int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupExtension);
 int mReadBitMap(tImage* img,unsigned char* data, int size); /* Note: this will ignore the palette */
 
+int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits, int colors, tColor* colorArray, int lineWidth, int optionflag, const char* backupExtension);
+
 #endif
diff --git a/PR/src/lib/formats/bmp.c b/PR/src/lib/formats/bmp.c
index 2169db6..e0adfb8 100644
--- a/PR/src/lib/formats/bmp.c
+++ b/PR/src/lib/formats/bmp.c
@@ -43,11 +43,10 @@ bmp.c: Princed Resources : BMP file support
 #include <stdlib.h>
 #include <string.h>
 
-int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupExtension) {
+int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits, int colors, tColor* colorArray, int lineWidth, int optionflag, const char* backupExtension) {
 
 	/* declare variables */
 	int a;
-	unsigned short int bits;
 	unsigned short int planes=1;
 	unsigned long int colours;
 	unsigned long int extra=1000;
@@ -59,21 +58,20 @@ int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupE
 	unsigned long int width;
 	const unsigned long int zero=0;
 	char lineSerialization;
-	const tColor* palette;
 	FILE* bitmap;
 
 	/* open file */
-	if (!writeOpen(vFile,&bitmap,optionflag)) return 0; /* false */
+	if (!writeOpen(file,&bitmap,optionflag)) return 0; /* false */
 
 	/* initialize variables */
-	width=img.width;
+/*	width=img.width;
 	height=img.height;
 	bits=getCarry(img.type);
-	colours=1<<bits;
+	colours=1<<bits;*/
 	headerSize=40;
-	offset=54+(colours<<2);
-	lineSerialization=(-img.widthInBytes)&3;
-	filesize=offset+(img.widthInBytes+lineSerialization)*height;
+	offset=54+(colors<<2);
+	lineSerialization=(-lineWidth)&3;
+	filesize=offset+(lineWidth+lineSerialization)*height;
 
 	/* Write header */
 	fwrite     ("BM",2,1   ,bitmap);    /* Magic identifier            */
@@ -93,20 +91,19 @@ int mWriteBitMap(tImage img,const char* vFile,int optionflag,const char* backupE
 	fwritelong (&zero      ,bitmap);    /* Important colours           */
 
 	/* Write ColorTable */
-	getPalette(&img.pal,bits,&palette);
-	for (a=0;a<colours;a++) {
-		color=palette[a].b;
+	for (a=0;a<colors;a++) {
+		color=colorArray[a].b;
 		fwritechar(&color,bitmap); /* Blue  */
-		color=palette[a].g;
+		color=colorArray[a].g;
 		fwritechar(&color,bitmap); /* Green */
-		color=palette[a].r;
+		color=colorArray[a].r;
 		fwritechar(&color,bitmap); /* Red   */
 		fwritechar(&zero ,bitmap); /* alpha */
 	}
 
 	/* Write data */
-	while (img.height--) {
-		fwrite(img.pix+img.height*img.widthInBytes,img.widthInBytes,1,bitmap);
+	while (h--) {
+		fwrite(data+h*lineWidth,lineWidth,1,bitmap);
 		fwrite(&zero,lineSerialization,1,bitmap);
 	}
 
diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c
index f23fd4d..8ce4578 100644
--- a/PR/src/lib/object/image/image16.c
+++ b/PR/src/lib/object/image/image16.c
@@ -40,6 +40,7 @@ image.c: Princed Resources : Image Compression Library
 #include "memory.h"
 #include "disk.h" /* array2short */
 #include "dat.h"
+#include "object.h" /* paletteGet* */
 
 #include "bmp.h"
 
@@ -401,20 +402,36 @@ void* objImageCreate(unsigned char* data, int size, tObject palette, int *error)
 	 */
 
 	tImage* image;
+	int bits;
 	image=(tImage*)malloc(sizeof(tImage));
 
 	/* Expand graphic and check results */
 	*error=mExpandGraphic(data,image,size);
 /*	if ((result==COMPRESS_RESULT_WARNING)&&hasFlag(verbose_flag))
 		fprintf(outputStream,PR_TEXT_EXPORT_BMP_WARN);*/
-	if (*error==COMPRESS_RESULT_FATAL) return NULL;
+	if (*error==COMPRESS_RESULT_FATAL) {
+		free(image);
+		return NULL;
+	}
 
+	bits=paletteGetBits(image->pal);
+	if (bits!=getCarry(image->type)) printf("error, palette mismatch\n");
+	
 	return (void*)image;
 }
 
 int objImageWrite(void* img,const char* file,int optionflag,const char* backupExtension) {
+	tImage* i=img;
+	int bits;
+	int colors;
+	tColor* colorArray;
+
+	bits=paletteGetBits(i->pal);
+	colors=paletteGetColors(i->pal);
+	colorArray=paletteGetColorArray(i->pal);
+	
 	/* Write bitmap */
-	return mWriteBitMap(*((tImage*)img),file,optionflag,backupExtension);
+	return mWriteBmp(file,i->pix,i->width,i->height,bits,colors,colorArray,i->widthInBytes,optionflag,backupExtension);
 }
 
 void objImageFree(void* img) {
diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c
index f23fd4d..8ce4578 100644
--- a/PR/src/lib/object/image/image2.c
+++ b/PR/src/lib/object/image/image2.c
@@ -40,6 +40,7 @@ image.c: Princed Resources : Image Compression Library
 #include "memory.h"
 #include "disk.h" /* array2short */
 #include "dat.h"
+#include "object.h" /* paletteGet* */
 
 #include "bmp.h"
 
@@ -401,20 +402,36 @@ void* objImageCreate(unsigned char* data, int size, tObject palette, int *error)
 	 */
 
 	tImage* image;
+	int bits;
 	image=(tImage*)malloc(sizeof(tImage));
 
 	/* Expand graphic and check results */
 	*error=mExpandGraphic(data,image,size);
 /*	if ((result==COMPRESS_RESULT_WARNING)&&hasFlag(verbose_flag))
 		fprintf(outputStream,PR_TEXT_EXPORT_BMP_WARN);*/
-	if (*error==COMPRESS_RESULT_FATAL) return NULL;
+	if (*error==COMPRESS_RESULT_FATAL) {
+		free(image);
+		return NULL;
+	}
 
+	bits=paletteGetBits(image->pal);
+	if (bits!=getCarry(image->type)) printf("error, palette mismatch\n");
+	
 	return (void*)image;
 }
 
 int objImageWrite(void* img,const char* file,int optionflag,const char* backupExtension) {
+	tImage* i=img;
+	int bits;
+	int colors;
+	tColor* colorArray;
+
+	bits=paletteGetBits(i->pal);
+	colors=paletteGetColors(i->pal);
+	colorArray=paletteGetColorArray(i->pal);
+	
 	/* Write bitmap */
-	return mWriteBitMap(*((tImage*)img),file,optionflag,backupExtension);
+	return mWriteBmp(file,i->pix,i->width,i->height,bits,colors,colorArray,i->widthInBytes,optionflag,backupExtension);
 }
 
 void objImageFree(void* img) {
diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c
index f23fd4d..8ce4578 100644
--- a/PR/src/lib/object/image/image256.c
+++ b/PR/src/lib/object/image/image256.c
@@ -40,6 +40,7 @@ image.c: Princed Resources : Image Compression Library
 #include "memory.h"
 #include "disk.h" /* array2short */
 #include "dat.h"
+#include "object.h" /* paletteGet* */
 
 #include "bmp.h"
 
@@ -401,20 +402,36 @@ void* objImageCreate(unsigned char* data, int size, tObject palette, int *error)
 	 */
 
 	tImage* image;
+	int bits;
 	image=(tImage*)malloc(sizeof(tImage));
 
 	/* Expand graphic and check results */
 	*error=mExpandGraphic(data,image,size);
 /*	if ((result==COMPRESS_RESULT_WARNING)&&hasFlag(verbose_flag))
 		fprintf(outputStream,PR_TEXT_EXPORT_BMP_WARN);*/
-	if (*error==COMPRESS_RESULT_FATAL) return NULL;
+	if (*error==COMPRESS_RESULT_FATAL) {
+		free(image);
+		return NULL;
+	}
 
+	bits=paletteGetBits(image->pal);
+	if (bits!=getCarry(image->type)) printf("error, palette mismatch\n");
+	
 	return (void*)image;
 }
 
 int objImageWrite(void* img,const char* file,int optionflag,const char* backupExtension) {
+	tImage* i=img;
+	int bits;
+	int colors;
+	tColor* colorArray;
+
+	bits=paletteGetBits(i->pal);
+	colors=paletteGetColors(i->pal);
+	colorArray=paletteGetColorArray(i->pal);
+	
 	/* Write bitmap */
-	return mWriteBitMap(*((tImage*)img),file,optionflag,backupExtension);
+	return mWriteBmp(file,i->pix,i->width,i->height,bits,colors,colorArray,i->widthInBytes,optionflag,backupExtension);
 }
 
 void objImageFree(void* img) {
diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c
index f23fd4d..8ce4578 100644
--- a/PR/src/lib/object/image/image_common.c
+++ b/PR/src/lib/object/image/image_common.c
@@ -40,6 +40,7 @@ image.c: Princed Resources : Image Compression Library
 #include "memory.h"
 #include "disk.h" /* array2short */
 #include "dat.h"
+#include "object.h" /* paletteGet* */
 
 #include "bmp.h"
 
@@ -401,20 +402,36 @@ void* objImageCreate(unsigned char* data, int size, tObject palette, int *error)
 	 */
 
 	tImage* image;
+	int bits;
 	image=(tImage*)malloc(sizeof(tImage));
 
 	/* Expand graphic and check results */
 	*error=mExpandGraphic(data,image,size);
 /*	if ((result==COMPRESS_RESULT_WARNING)&&hasFlag(verbose_flag))
 		fprintf(outputStream,PR_TEXT_EXPORT_BMP_WARN);*/
-	if (*error==COMPRESS_RESULT_FATAL) return NULL;
+	if (*error==COMPRESS_RESULT_FATAL) {
+		free(image);
+		return NULL;
+	}
 
+	bits=paletteGetBits(image->pal);
+	if (bits!=getCarry(image->type)) printf("error, palette mismatch\n");
+	
 	return (void*)image;
 }
 
 int objImageWrite(void* img,const char* file,int optionflag,const char* backupExtension) {
+	tImage* i=img;
+	int bits;
+	int colors;
+	tColor* colorArray;
+
+	bits=paletteGetBits(i->pal);
+	colors=paletteGetColors(i->pal);
+	colorArray=paletteGetColorArray(i->pal);
+	
 	/* Write bitmap */
-	return mWriteBitMap(*((tImage*)img),file,optionflag,backupExtension);
+	return mWriteBmp(file,i->pix,i->width,i->height,bits,colors,colorArray,i->widthInBytes,optionflag,backupExtension);
 }
 
 void objImageFree(void* img) {
diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c
index cfd01b7..e7c46bc 100644
--- a/PR/src/lib/object/object.c
+++ b/PR/src/lib/object/object.c
@@ -107,3 +107,6 @@ int writeObject(tObject o, const char* file, int optionflag, const char* backupE
 }	
 
 
+int paletteGetBits(tObject pal) { return 4; }
+int paletteGetColors(tObject pal) { return 16; }
+tColor* paletteGetColorArray(tObject pal) { return NULL; }
diff --git a/PR/src/lib/object/palette/pop1_16c.c b/PR/src/lib/object/palette/pop1_16c.c
index 3672695..133baf9 100644
--- a/PR/src/lib/object/palette/pop1_16c.c
+++ b/PR/src/lib/object/palette/pop1_16c.c
@@ -135,7 +135,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 		}
 		bits=4;
 		/* this palette needs to be remembered as binary */
-		addPop1Raw(p,data+1,dataSize-1);
+		/*addPop1Raw(p,data+1,dataSize-1);*/
 		break;
 	case 3*256+1:
 	case 3*320+1:
@@ -151,7 +151,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 	if (bits) setPalette(p,bits,c);
 	return bits;
 }
-
+/*
 int applyPalette(tPalette* p,tImage *i) {
 	i->pal=*p;
 	return 0;
@@ -162,7 +162,7 @@ void addPop1Raw(tPalette* p,unsigned char* data, int dataSize) {
 	p->pop1raw=binaryallocandcopy(data,dataSize);
 	p->pop1rawSize=dataSize;
 }
-
+*/
 
 typedef struct { 
 	tColor c[16];
diff --git a/PR/src/lib/object/palette/pop2_256c.c b/PR/src/lib/object/palette/pop2_256c.c
index 3672695..133baf9 100644
--- a/PR/src/lib/object/palette/pop2_256c.c
+++ b/PR/src/lib/object/palette/pop2_256c.c
@@ -135,7 +135,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 		}
 		bits=4;
 		/* this palette needs to be remembered as binary */
-		addPop1Raw(p,data+1,dataSize-1);
+		/*addPop1Raw(p,data+1,dataSize-1);*/
 		break;
 	case 3*256+1:
 	case 3*320+1:
@@ -151,7 +151,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 	if (bits) setPalette(p,bits,c);
 	return bits;
 }
-
+/*
 int applyPalette(tPalette* p,tImage *i) {
 	i->pal=*p;
 	return 0;
@@ -162,7 +162,7 @@ void addPop1Raw(tPalette* p,unsigned char* data, int dataSize) {
 	p->pop1raw=binaryallocandcopy(data,dataSize);
 	p->pop1rawSize=dataSize;
 }
-
+*/
 
 typedef struct { 
 	tColor c[16];
diff --git a/PR/src/lib/object/palette/pop2_4bit.c b/PR/src/lib/object/palette/pop2_4bit.c
index 3672695..133baf9 100644
--- a/PR/src/lib/object/palette/pop2_4bit.c
+++ b/PR/src/lib/object/palette/pop2_4bit.c
@@ -135,7 +135,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 		}
 		bits=4;
 		/* this palette needs to be remembered as binary */
-		addPop1Raw(p,data+1,dataSize-1);
+		/*addPop1Raw(p,data+1,dataSize-1);*/
 		break;
 	case 3*256+1:
 	case 3*320+1:
@@ -151,7 +151,7 @@ int readPalette(tPalette* p, unsigned char* data, int dataSize) {
 	if (bits) setPalette(p,bits,c);
 	return bits;
 }
-
+/*
 int applyPalette(tPalette* p,tImage *i) {
 	i->pal=*p;
 	return 0;
@@ -162,7 +162,7 @@ void addPop1Raw(tPalette* p,unsigned char* data, int dataSize) {
 	p->pop1raw=binaryallocandcopy(data,dataSize);
 	p->pop1rawSize=dataSize;
 }
-
+*/
 
 typedef struct { 
 	tColor c[16];