git » fp-git.git » commit 18f2b5f

hooked image importing

author ecalot
2006-02-25 03:20:47 UTC
committer ecalot
2006-02-25 03:20:47 UTC
parent eb7b4a883226b74a3c3c56a9c0a81b2fd2df69ee

hooked image importing

PR/src/include/disk.h +2 -0
PR/src/include/image.h +1 -0
PR/src/lib/formats/bmp.c +81 -12
PR/src/lib/object/image/image16.c +6 -5
PR/src/lib/object/image/image2.c +6 -5
PR/src/lib/object/image/image256.c +6 -5
PR/src/lib/object/image/image_common.c +6 -5
PR/src/lib/object/object.c +1 -1
PR/src/lib/object/other/binary.c +1 -0

diff --git a/PR/src/include/disk.h b/PR/src/include/disk.h
index fad1792..984651e 100644
--- a/PR/src/include/disk.h
+++ b/PR/src/include/disk.h
@@ -86,6 +86,7 @@ int recurseDirectory(const char* path,int recursive, void* pass, void (*function
 #define fwriteshort(var,file) macfwrites((var),file)
 #define fwritelong(var,file)  macfwritel((var),file)
 #define fwritechar(var,file)  fwrite((var),1,1,file)
+#define freadchar(var,file)   fread ((var),1,1,file)
 int macfreads (void* bigEndian,FILE* file);
 int macfreadl (void* var,FILE* file);
 int macfwrites(const void* var,FILE* file);
@@ -96,6 +97,7 @@ int macfwritel(const void* var,FILE* file);
 #define fwriteshort(var,file) fwrite((var),2,1,file)
 #define fwritelong(var,file)  fwrite((var),4,1,file)
 #define fwritechar(var,file)  fwrite((var),1,1,file)
+#define freadchar(var,file)   fread ((var),1,1,file)
 #endif
 
 #endif
diff --git a/PR/src/include/image.h b/PR/src/include/image.h
index 37c3108..b1f0458 100644
--- a/PR/src/include/image.h
+++ b/PR/src/include/image.h
@@ -66,6 +66,7 @@ int mCompressGraphic(unsigned char** a,tImage* i, int* size); /* make it private
 
 void* objImageCreate(tBinary c, tObject palette, int *error);
 int objImageWrite(void* img,const char* file,int optionflag,const char* backupExtension);
+void* objImageRead(const char* file,tObject palette, int *result);
 
 #define getCarry(a) ((((a)>>4)&7)+1)
 #define getAlgor(a) a&0x4F
diff --git a/PR/src/lib/formats/bmp.c b/PR/src/lib/formats/bmp.c
index 959e27a..7dcfee9 100644
--- a/PR/src/lib/formats/bmp.c
+++ b/PR/src/lib/formats/bmp.c
@@ -84,7 +84,7 @@ int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits
 	fwriteshort(&planes    ,bitmap);    /* Number of colour planes     */
 	fwriteshort(&bits      ,bitmap);    /* Bits per pixel              */
 	fwritelong (&zero      ,bitmap);    /* Compression type (0=none)   */
-	fwritelong (&zero      ,bitmap);    /* Image size in bytes         */
+	fwritelong (&zero      ,bitmap);    /* Image size in bytes (may be 0 if no compression) */
 	fwritelong (&extra     ,bitmap);    /* Pixels per meter x          */
 	fwritelong (&extra     ,bitmap);    /* Pixels per meter y          */
 	fwritelong (&colours   ,bitmap);    /* Number of colours           */
@@ -111,19 +111,78 @@ int mWriteBmp(const char* file,const unsigned char* data, int w, int h, int bits
 	return PR_RESULT_SUCCESS; 
 }
 
-int mReadBitMap(tImage* image,unsigned char* data, int size) {
+int readBmp(const char* file, unsigned char** data, int *ph, int *pw,  int *pbits, int *pcolors, tColor** colorArray, int *plineWidth) {
+/*int mReadBitMap(tImage* image,unsigned char* data, int size) {*/
+	FILE* bitmap;
 	char lineSerialization;
 	int ok;
-	int serializedWidth;
-	int x=0;
-	unsigned char carry;
-	unsigned long int colours;
-	unsigned long int filesize;
-	unsigned long int offset;
-	unsigned short int bits;
-	unsigned short int height;
-	unsigned short int width;
+	char magic[2];
+	int a;
+	unsigned long int colours=0;
+	unsigned long int filesize=0;
+	unsigned long int offset=0;
+	unsigned long int aux=0;
+	unsigned short int bits=0;
+	unsigned short int height=0;
+	unsigned short int width=0;
+
+	bitmap=fopen(file,"rb");
+	if (!bitmap) return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; 
+
+	/* Read headers */
+		/* file header */
+	ok=fread(magic,2,1         ,bitmap);
+	ok=ok&&!strncmp(magic,"BM",2);
+	ok=ok&&freadlong(&filesize ,bitmap);
+	ok=ok&&freadlong(&aux      ,bitmap);
+	ok=ok&&(!aux);
+	ok=ok&&freadlong(&offset   ,bitmap);
+	
+		/* info header */
+	ok=ok&&freadlong(&aux      ,bitmap);
+	ok=ok&&(aux==40);
+	ok=ok&&freadlong(&width    ,bitmap);
+	ok=ok&&freadlong(&height   ,bitmap);
+	ok=ok&&freadshort(&aux     ,bitmap);
+	ok=ok&&(aux==1);
+	ok=ok&&freadshort(&bits    ,bitmap);
+	ok=ok&&freadlong(&aux      ,bitmap);    /* Compression type (0=none)   */
+	if (ok&&aux!=0) { fclose(bitmap); return -1; /* PR_NO_COMPRESS_SUPPORT */ }
+	ok=ok&&freadlong(&aux      ,bitmap);    /* Image size in bytes (junk)  */
+	ok=ok&&freadlong(&aux      ,bitmap);    /* Pixels per meter x (junk)   */
+	ok=ok&&freadlong(&aux      ,bitmap);    /* Pixels per meter y (junk)   */
+	ok=ok&&freadlong(&colours  ,bitmap);    /* Number of colours (junk)    */
+	ok=ok&&(colours<1000);
+	ok=ok&&freadlong(&aux      ,bitmap);    /* Important colours (junk)    */
+	
+	/* Verify */
+	*plineWidth=width*(8/bits); /* Note: only works in bits=1,2,4,8 */
+	lineSerialization=(-*plineWidth)&3;
+/*	offset=54+(colors<<2);
+	lineSerialization=(-lineWidth)&3;
+	filesize=offset+(lineWidth+lineSerialization)*height;*/
+
+	/* Read ColorTable */
+	aux=0;
+	*colorArray=malloc(sizeof(tColor)*colours);
+	for (a=0;a<colours;a++) {
+		ok=ok&&freadchar(&((*colorArray)[a].b),bitmap); /* Blue  */
+		ok=ok&&freadchar(&((*colorArray)[a].g),bitmap); /* Green */
+		ok=ok&&freadchar(&((*colorArray)[a].r),bitmap); /* Red   */
+		ok=ok&&freadchar(&aux,bitmap); /* alpha */
+	}
 
+	/* Write data */
+	*ph=height;
+	*pw=width;
+	*data=malloc((*plineWidth+lineSerialization)*height);
+	while (height--) {
+		ok=ok&&fread(*data+height**plineWidth,*plineWidth,1,bitmap);
+		ok=ok&&fread(&aux,lineSerialization,1,bitmap);
+	}
+
+	
+#if 0
 	/* Validate if there is header and if it starts in BM */
 	ok    = size>50;
 	ok=ok&& data[0]=='B' && data[1]=='M';
@@ -163,6 +222,16 @@ int mReadBitMap(tImage* image,unsigned char* data, int size) {
 			data+offset+height*serializedWidth,
 			image->widthInBytes
 		);
+#endif
 
-	return 1; /* true */
+	if (!ok) {
+		free(*colorArray);
+		free(*data);
+		return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; /* TODO: use a bad format code */
+	}
+	
+	*pbits        = bits;
+	*pcolors      = colours;
+	
+	return PR_RESULT_SUCCESS;			
 }
diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c
index 0b7d26e..1b22f65 100644
--- a/PR/src/lib/object/image/image16.c
+++ b/PR/src/lib/object/image/image16.c
@@ -448,15 +448,16 @@ void objImageFree(void* img) {
 	free(img);
 }
 
-int mFormatImportBmp(tResource *res) {
-	tImage img;
+void* objImageRead(const char* file,tObject palette, int *result) {
+/*int mFormatImportBmp(tResource *res) {*/
+	/*tImage img;*/
 
-	if (!mReadBitMap(&img,res->content.data,res->content.size)) return 0; /* false */
+/*	if (!mReadBitMap(&img,)) return 0; * false *
 	free(res->content.data);
 	mCompressGraphic(&(res->content.data),&img,(int*)&(res->content.size));
 	mWriteFileInDatFile(res);
 	free(img.pix);
-
-	return 1; /* true */
+*/
+	return NULL;
 }
 
diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c
index 0b7d26e..1b22f65 100644
--- a/PR/src/lib/object/image/image2.c
+++ b/PR/src/lib/object/image/image2.c
@@ -448,15 +448,16 @@ void objImageFree(void* img) {
 	free(img);
 }
 
-int mFormatImportBmp(tResource *res) {
-	tImage img;
+void* objImageRead(const char* file,tObject palette, int *result) {
+/*int mFormatImportBmp(tResource *res) {*/
+	/*tImage img;*/
 
-	if (!mReadBitMap(&img,res->content.data,res->content.size)) return 0; /* false */
+/*	if (!mReadBitMap(&img,)) return 0; * false *
 	free(res->content.data);
 	mCompressGraphic(&(res->content.data),&img,(int*)&(res->content.size));
 	mWriteFileInDatFile(res);
 	free(img.pix);
-
-	return 1; /* true */
+*/
+	return NULL;
 }
 
diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c
index 0b7d26e..1b22f65 100644
--- a/PR/src/lib/object/image/image256.c
+++ b/PR/src/lib/object/image/image256.c
@@ -448,15 +448,16 @@ void objImageFree(void* img) {
 	free(img);
 }
 
-int mFormatImportBmp(tResource *res) {
-	tImage img;
+void* objImageRead(const char* file,tObject palette, int *result) {
+/*int mFormatImportBmp(tResource *res) {*/
+	/*tImage img;*/
 
-	if (!mReadBitMap(&img,res->content.data,res->content.size)) return 0; /* false */
+/*	if (!mReadBitMap(&img,)) return 0; * false *
 	free(res->content.data);
 	mCompressGraphic(&(res->content.data),&img,(int*)&(res->content.size));
 	mWriteFileInDatFile(res);
 	free(img.pix);
-
-	return 1; /* true */
+*/
+	return NULL;
 }
 
diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c
index 0b7d26e..1b22f65 100644
--- a/PR/src/lib/object/image/image_common.c
+++ b/PR/src/lib/object/image/image_common.c
@@ -448,15 +448,16 @@ void objImageFree(void* img) {
 	free(img);
 }
 
-int mFormatImportBmp(tResource *res) {
-	tImage img;
+void* objImageRead(const char* file,tObject palette, int *result) {
+/*int mFormatImportBmp(tResource *res) {*/
+	/*tImage img;*/
 
-	if (!mReadBitMap(&img,res->content.data,res->content.size)) return 0; /* false */
+/*	if (!mReadBitMap(&img,)) return 0; * false *
 	free(res->content.data);
 	mCompressGraphic(&(res->content.data),&img,(int*)&(res->content.size));
 	mWriteFileInDatFile(res);
 	free(img.pix);
-
-	return 1; /* true */
+*/
+	return NULL;
 }
 
diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c
index 8a8a0ea..c3edb8b 100644
--- a/PR/src/lib/object/object.c
+++ b/PR/src/lib/object/object.c
@@ -168,7 +168,7 @@ tObject readObject(const char* file,tResource* res,int *result) {
 			/*o.obj=objLevelRead(file,res.content,result);*/
 			break;
 		case eResTypeImage:
-			/*o.obj=objImageRead(file,res.content,res.palette,result);*/
+			o.obj=objImageRead(file,res->palette,result);
 			break;
 		case eResTypeWave:
 			o.obj=objWaveRead(file,result);
diff --git a/PR/src/lib/object/other/binary.c b/PR/src/lib/object/other/binary.c
index cb217e5..d20f8af 100644
--- a/PR/src/lib/object/other/binary.c
+++ b/PR/src/lib/object/other/binary.c
@@ -75,3 +75,4 @@ int objBinarySet(void* o,tResource* res) {
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }
+