author | ecalot
<ecalot> 2006-03-07 10:04:30 UTC |
committer | ecalot
<ecalot> 2006-03-07 10:04:30 UTC |
parent | 34176f944a47de033fa05b44c1d378fb6b5efbc9 |
PR/src/Makefile | +5 | -1 |
PR/src/include/image.h | +3 | -0 |
PR/src/include/palette.h | +0 | -3 |
PR/src/lib/layers/autodetect.c | +12 | -12 |
PR/src/lib/object/image/image16.c | +7 | -2 |
PR/src/lib/object/image/image2.c | +14 | -287 |
PR/src/lib/object/image/image256.c | +13 | -1 |
PR/src/lib/object/image/image_common.c | +7 | -2 |
PR/src/lib/object/object.c | +7 | -2 |
diff --git a/PR/src/Makefile b/PR/src/Makefile index afa0f68..4316bdd 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 .image16.o .image256.o .binary.o .sounds.o .level.o +OBJECT = .object.o .pop1_4bit.o .image2.o .image16.o .image256.o .binary.o .sounds.o .level.o CONSOLE = .main.o .filedir.o MAIN = .pr.o @@ -170,6 +170,10 @@ $(XMLFILE): xml/resources.xml $(INFO) Compiling main item class... $(CC) $(OPTIONS) -c lib/object/main.c -o $@ +.image2.o: lib/object/image/image2.c include/image.h include/compress.h include/disk.h include/memory.h + $(INFO) Compiling 2 colors image type class... + $(CC) $(OPTIONS) -c lib/object/image/image2.c -o $@ + .image16.o: lib/object/image/image16.c include/image.h include/compress.h include/disk.h include/memory.h $(INFO) Compiling 16 colors image type class... $(CC) $(OPTIONS) -c lib/object/image/image16.c -o $@ diff --git a/PR/src/include/image.h b/PR/src/include/image.h index 9954cde..4bd17e1 100644 --- a/PR/src/include/image.h +++ b/PR/src/include/image.h @@ -81,4 +81,7 @@ void* objImage2Read(const char* file,tObject palette, int *result); #define getCarry(a) ((((a)>>4)&7)+1) #define getAlgor(a) a&0x4F +int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes); +int mCompressGraphic(tBinary* input, tBinary* output, int ignoreFirstBytes, int w, int h); + #endif diff --git a/PR/src/include/palette.h b/PR/src/include/palette.h index a72fa64..6c2aa92 100644 --- a/PR/src/include/palette.h +++ b/PR/src/include/palette.h @@ -68,8 +68,5 @@ void* objPop1Palette4bitsRead(const char* file,int *result); int objPop1Palette4bitsSet(void* o,tResource* res); tColor* objPalette_pop1_4bitsGetColors(void* o); -#define objPalette_pop1_monoGetColors(o) paletteGetColorArrayForColors(2) - -tColor* paletteGetColorArrayForColors(int colors); #endif diff --git a/PR/src/lib/layers/autodetect.c b/PR/src/lib/layers/autodetect.c index c475f55..ba3dd00 100644 --- a/PR/src/lib/layers/autodetect.c +++ b/PR/src/lib/layers/autodetect.c @@ -47,14 +47,14 @@ int isA64kPalette(tBinary c) { \***************************************************************/ int verifyLevelHeader(tBinary c) { - return (c.size==12025) || (((c.size==2306)||(c.size==2305))&&!(c.data[1698]&0x0F)&&!(c.data[1700]&0x0F)&&!(c.data[1702]&0x0F)); + return (c.size==12025) || (((c.size==2306)||(c.size==2305))&&!(c.data[1697]&0x0F)&&!(c.data[1699]&0x0F)&&!(c.data[1701]&0x0F)); } int verifyImage16Header(tBinary c) { unsigned char imageBitRate; if (c.size<=7) return 0; /* false */ - imageBitRate=(( (c.data[6])>>4 ) & 7)+1; - return (c.size>7) && ((c.data[5])==0) && (imageBitRate==4); + imageBitRate=(( (c.data[5])>>4 ) & 7)+1; + return (c.size>7) && ((c.data[4])==0) && (imageBitRate==4); /* NOTE: * imageBitRate==1 * works for monochrome images (but is very common and matches more than that) @@ -64,8 +64,8 @@ int verifyImage16Header(tBinary c) { int verifyImage256Header(tBinary c) { unsigned char imageBitRate; if (c.size<=7) return 0; /* false */ - imageBitRate=(( (c.data[6])>>4 ) & 7)+1; - return (c.size>7) && ((c.data[5])==1) && (imageBitRate==8); + imageBitRate=(( (c.data[5])>>4 ) & 7)+1; + return (c.size>7) && ((c.data[4])==1) && (imageBitRate==8); /* NOTE: * imageBitRate==1 * works for monochrome images (but is very common and matches more than that) @@ -86,14 +86,14 @@ int verifyPaletteHeader(tBinary c) { int verifySpeakerHeader(tBinary c) { /* format: (checksum)+(0x00)+(even number)+3 bytes per note */ return - (c.size>4)&&(c.data[1]==0x00)&&(!(c.data[2]%2))&&(!(c.size%3)) + (c.size>4)&&(c.data[0]==0x00)&&(!(c.data[1]%2))&&(!(c.size%3)) ; } int verifyWaveHeader(tBinary c) { /* format: (checksum)+(0x01)+raw wave */ return - (c.size>1)&&(c.data[1]==0x01)&&((c.size%3)==2) /* TODO: use WAVE_MAGIC */ + (c.size>1)&&(c.data[0]==0x01)&&((c.size%3)==2) /* TODO: use WAVE_MAGIC */ ; } @@ -101,11 +101,11 @@ int verifyMidiHeader(tBinary c) { /* format: (checksum)+(0x02)+"MThd"... */ return (c.size>6) && - (c.data[1]==0x02) && - (c.data[2]=='M') && - (c.data[3]=='T') && - (c.data[4]=='h') && - (c.data[5]=='d') + (c.data[0]==0x02) && + (c.data[1]=='M') && + (c.data[2]=='T') && + (c.data[3]=='h') && + (c.data[4]=='d') ; } diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c index c4e38e3..8924cc1 100644 --- a/PR/src/lib/object/image/image16.c +++ b/PR/src/lib/object/image/image16.c @@ -346,6 +346,11 @@ int mCompressGraphic(tBinary* input, tBinary* output, int ignoreFirstBytes, int extern FILE* outputStream; +tColor* objPalette_16() { + static tColor c[16]=SAMPLE_PAL16; + return c; +} + void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ /* @@ -390,7 +395,7 @@ int objImage16Write(void* img,const char* file,int optionflag,const char* backup } else { bits=getCarry(i->type); colors=1<<bits; - colorArray=paletteGetColorArrayForColors(colors); + colorArray=objPalette_16(); } /* Write bitmap */ @@ -459,7 +464,7 @@ int objImage16Set(void* o,tResource* res) { compressed.data[3]=img->width>>8; /* (8 bits)00000000+(4 bits)palette type+(4 bits)algorithm */ compressed.data[4]=0; - compressed.data[5]=(img->bits==4?0xb0:0x00)|algorithm; + compressed.data[5]=0xb0|algorithm; res->content=compressed; mWriteFileInDatFile(res); diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c index c4e38e3..3d9c8ca 100644 --- a/PR/src/lib/object/image/image2.c +++ b/PR/src/lib/object/image/image2.c @@ -94,259 +94,14 @@ image.c: Princed Resources : Image Compression Library | I M P L E M E N T A T I O N | \***************************************************************/ -int pop2decompress(const unsigned char* input, int inputSize, int verify, unsigned char** output,int* outputSize); - -/***************************************************************\ -| Compression Level Manager | -\***************************************************************/ - -int compressionLevel=3; -int compressionHigher; - -void setCompressionLevel(int cl) { - compressionLevel=cl; -} - -/***************************************************************\ -| Image transpose | -\***************************************************************/ - -/* Determines where the transposed byte must be saved */ -int cmp_transpose(int x,int w,int h) { - return ((x%h)*(w))+(int)(x/h); -} - -void cmp_transposeImage(tImage* image,int size) { - unsigned char* outputaux=getMemory(size); - int cursor; - - for (cursor=0;cursor<size;cursor++) - outputaux[cmp_transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor]; - free(image->pix); - image->pix=outputaux; -} - -void cmp_antiTransposeImage(tBinary* b, int widthInBytes, int height) { - unsigned char* outputaux=getMemory(b->size); - int cursor; - - for (cursor=0;cursor<b->size;cursor++) - outputaux[cursor]=b->data[cmp_transpose(cursor,widthInBytes,height)]; - free(b->data); - b->data=outputaux; -} - -/***************************************************************\ -| Main compress and expand graphics | -\***************************************************************/ - -/* - * Header info: - * 1 byte - checksum char checksum - * 2 bytes - height short int height - * 2 bytes - width short int width - * 1 byte - 00 (char)0 - * 1 byte - compression type unsigned char compressionType - */ - -/* Expands an array into an image */ -int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes) { - /* - * Reads data and extracts tImage - * returns the next image address or -1 in case of error - */ - - int imageSizeInBytes=0; - int result; - - image->height=array2short(data); - data+=2; - image->width =array2short(data); - data+=2; - - if (*(data++)>1) return COMPRESS_RESULT_FATAL; /* Verify format */ - image->type=(unsigned char)(*(data++)); - dataSizeInBytes-=6; - switch (((image->type>>4)&7)+1) { - case 8: - image->widthInBytes=(image->width); - break; - case 4: - image->widthInBytes=(image->width+1)/2; - break; - case 1: - image->widthInBytes=(image->width+7)/8; - break; - default: - return COMPRESS_RESULT_FATAL; - } - -#define checkSize if (imageSizeInBytes!=(image->widthInBytes*image->height))\ - return COMPRESS_RESULT_FATAL -#define checkResult if (result==COMPRESS_RESULT_FATAL)\ - return COMPRESS_RESULT_FATAL - - switch (getAlgor(image->type)) { - case COMPRESS_RAW: /* No Compression Algorithm */ - if ((image->pix=getMemory(dataSizeInBytes))==NULL) return COMPRESS_RESULT_FATAL; - memcpy(image->pix,data,dataSizeInBytes); - imageSizeInBytes=image->widthInBytes*image->height; - result=COMPRESS_RESULT_SUCCESS; - break; - case COMPRESS_RLE_LR: /* RLE Left to Right Compression Algorithm */ - result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes); - checkSize; - break; - case COMPRESS_RLE_UD: /* RLE Up to Down Compression Algorithm */ - result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes); - checkResult; - checkSize; - cmp_transposeImage(image,imageSizeInBytes); - break; - case COMPRESS_LZG_LR: /* LZ Groody Left to Right Compression Algorithm */ - result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes); - checkSize; - break; - case COMPRESS_LZG_UD: /* LZ Groody Up to Down Compression Algorithm */ - result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes); - checkResult; - checkSize; - cmp_transposeImage(image,imageSizeInBytes); - break; - default: - result=COMPRESS_RESULT_FATAL; /* unknown algorithm */ - break; - } - return result; /* Ok */ -} - -/* Compress an image into binary data */ -int mCompressGraphic(tBinary* input, tBinary* output, int ignoreFirstBytes, int w, int h) { -/* unsigned char* *data,tImage* image, int* dataSizeInBytes, int ignoreFirstBytes) {*/ - /* Declare variables */ - unsigned char* compressed [COMPRESS_WORKING_ALGORITHMS]; - int compressedSize [COMPRESS_WORKING_ALGORITHMS]; - int algorithm; - int i; - int imageSizeInBytes; - int max_alg=1; - - /* Initialize variables */ - imageSizeInBytes=input->size; /*=image->widthInBytes*image->height;*/ - - /* - * Perform all compressions - */ - - /* Forward compression */ - - /* COMPRESS_RAW - * The allocation size is the image size. - * The algorithm is hard-coded. - * There is no need to code a transposed version because - * we have no compression to improve. - */ - compressed[COMPRESS_RAW]=getMemory(imageSizeInBytes); - compressedSize[COMPRESS_RAW]=imageSizeInBytes; - memcpy(compressed[COMPRESS_RAW],input->data,imageSizeInBytes); - - /* COMPRESS_RLE_LR - * If all the uncompressed data has a big entropy, there - * will be a control byte for a block of 127 bytes. - * The allocation size has a maximum value of the image - * size plus a byte each 127 bytes. - * This is accoted by 2*n+50 - */ - cLevel(2) { - compressed[COMPRESS_RLE_LR]=getMemory((2*imageSizeInBytes+50)); - compressRle( - input->data,imageSizeInBytes, - compressed[COMPRESS_RLE_LR],&(compressedSize[COMPRESS_RLE_LR]) - ); - max_alg++; - } - /* COMPRESS_LZG_LR - * If all the uncompressed data has a big entropy, there - * will be a maskbyte for a block of 8 bytes. - * The allocation size has a maximum value of the image - * size plus a byte in 8. - * Additionally, this compressor needs 1024 bytes extra - * allocated. - * This is accoted by 2*n+1050 - */ - cLevel(4) { - cLevel(6) - setHigh; - else - unsetHigh; - compressed[COMPRESS_LZG_LR]=getMemory((2*imageSizeInBytes+1050)); - compressLzg( - input->data,imageSizeInBytes, - compressed[COMPRESS_LZG_LR],&(compressedSize[COMPRESS_LZG_LR]) - ); - max_alg++; - } - - /* Transposed compression - * Transposition is used to test the same compression - * algorithms with other input in order to get a better - * compression. - * The following algorithms will be the same as above, but - * using the image matrix transposed. - */ - cLevel(3) - cmp_antiTransposeImage(input,w,h); - - /* COMPRESS_RLE_UD */ - cLevel(3) { - compressed[COMPRESS_RLE_UD]=getMemory(2*imageSizeInBytes+50); - compressRle( - input->data,imageSizeInBytes, - compressed[COMPRESS_RLE_UD],&(compressedSize[COMPRESS_RLE_UD]) - ); - max_alg++; - } - - /* COMPRESS_LZG_UD */ - cLevel(5) { - cLevel(7) - setHigh; - else - unsetHigh; - compressed[COMPRESS_LZG_UD]=getMemory(2*imageSizeInBytes+1050); - compressLzg( - input->data,imageSizeInBytes, - compressed[COMPRESS_LZG_UD],&(compressedSize[COMPRESS_LZG_UD]) - ); - max_alg++; - } - /* - * Process results - */ - - /* Select the best compression (find minimum) */ - output->size=compressedSize[COMPRESS_RAW]; - algorithm=COMPRESS_RAW; - for (i=COMPRESS_RLE_LR;i<max_alg;i++) { - if (output->size>compressedSize[i]) { - output->size=compressedSize[i]; - algorithm=i; - } - } - - /* Copy the best algorithm in the compressed data */ - output->data=getMemory(output->size+ignoreFirstBytes); - memcpy(output->data+ignoreFirstBytes,compressed[algorithm],output->size); - output->size+=ignoreFirstBytes; - - /* Free all compression attempts */ - for (i=COMPRESS_RAW;i<max_alg;i++) free(compressed[i]); - return algorithm; +tColor* objPalette_pop1_monoGetColors() { + static tColor c[2]={{0,0,0},{255,255,255}}; + return c; } extern FILE* outputStream; -void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ +void* objImage2Create(tBinary cont, int *error) { /* use get like main.c */ /* * This function will expand the data into an image structure, @@ -357,7 +112,6 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l */ tImage* image; - int bits; image=(tImage*)malloc(sizeof(tImage)); /* Expand graphic and check results */ @@ -369,42 +123,20 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l return NULL; } - image->pal=palette; - bits=paletteGetBits(image->pal); - if (bits && bits!=getCarry(image->type)) printf("error, palette mismatch (pal=%d bits=%d)\n",bits,getCarry(image->type)); - image->bits=getCarry(image->type); + if (getCarry(image->type) != 1 ) printf("error, monochrome palette fromat wrong\n"); return (void*)image; } -int objImage16Write(void* img,const char* file,int optionflag,const char* backupExtension) { +int objImage2Write(void* img,const char* file,int optionflag,const char* backupExtension) { tImage* i=img; - int bits; - int colors; - tColor* colorArray; + tColor* colorArray=objPalette_pop1_monoGetColors(); - if (i->pal.type!=eResTypeNone) { - bits=paletteGetBits(i->pal); - colors=paletteGetColors(i->pal); - colorArray=paletteGetColorArray(i->pal); - } else { - bits=getCarry(i->type); - colors=1<<bits; - colorArray=paletteGetColorArrayForColors(colors); - } - /* Write bitmap */ - return mWriteBmp(file,i->pix,i->width,i->height,bits,colors,colorArray,i->widthInBytes,optionflag,backupExtension); -} - -void objImageFree(void* img) { - if (!img) return; - /* free bitmap */ - free(((tImage*)img)->pix); - free(img); + return mWriteBmp(file,i->pix,i->width,i->height,1,2,colorArray,i->widthInBytes,optionflag,backupExtension); } -void* objImage16Read(const char* file,tObject palette, int *result) { +void* objImage2Read(const char* file,tObject palette, int *result) { int bits; tImage* image=(tImage*)malloc(sizeof(tImage)); tColor* colorArray; @@ -416,28 +148,23 @@ void* objImage16Read(const char* file,tObject palette, int *result) { free(image); return NULL; } + + free(colorArray); /* check the palette information */ - image->pal=palette; bits=paletteGetBits(image->pal); - if (bits && bits!=image->bits) { /* bits=0 means all palettes allowed or ignore palette check */ + if (image->bits!=1) { *result=PR_RESULT_ERR_BMP_BITRATE_DIFFERS; free(image->pix); - free(colorArray); free(image); return NULL; } - /* TODO: palette content checks */ - - free(colorArray); return (void*)image; } - /* TODO: generate image->type in objImageSet */ -/*int mFormatImportBmp(tResource *res) { --> objImageSet */ -int objImage16Set(void* o,tResource* res) { +int objImage2Set(void* o,tResource* res) { tImage* img=o; tBinary decompressed,compressed; int algorithm; @@ -459,7 +186,7 @@ int objImage16Set(void* o,tResource* res) { compressed.data[3]=img->width>>8; /* (8 bits)00000000+(4 bits)palette type+(4 bits)algorithm */ compressed.data[4]=0; - compressed.data[5]=(img->bits==4?0xb0:0x00)|algorithm; + compressed.data[5]=algorithm; res->content=compressed; mWriteFileInDatFile(res); diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c index 3e4b267..b83f12c 100644 --- a/PR/src/lib/object/image/image256.c +++ b/PR/src/lib/object/image/image256.c @@ -258,6 +258,18 @@ void* objImage256Create(tBinary cont, tObject palette, int *error) { /* use get return (void*)image; } +tColor* objPalette_256() { + static tColor c[256]={{0,0,0},{0,0,0}}; + int i; + if (!c[2].r) + for (i=0;i<256;i++) { /* The greyscale */ + c[i].r=i; + c[i].g=i; + c[i].b=i; + } + return c; +} + int objImage256Write(void* img,const char* file,int optionflag,const char* backupExtension) { tImage* i=img; int bits; @@ -271,7 +283,7 @@ int objImage256Write(void* img,const char* file,int optionflag,const char* backu } else { bits=getCarry(i->type); colors=1<<bits; - colorArray=paletteGetColorArrayForColors(colors); + colorArray=objPalette_256(); } /* Write bitmap */ diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c index c4e38e3..8924cc1 100644 --- a/PR/src/lib/object/image/image_common.c +++ b/PR/src/lib/object/image/image_common.c @@ -346,6 +346,11 @@ int mCompressGraphic(tBinary* input, tBinary* output, int ignoreFirstBytes, int extern FILE* outputStream; +tColor* objPalette_16() { + static tColor c[16]=SAMPLE_PAL16; + return c; +} + void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ /* @@ -390,7 +395,7 @@ int objImage16Write(void* img,const char* file,int optionflag,const char* backup } else { bits=getCarry(i->type); colors=1<<bits; - colorArray=paletteGetColorArrayForColors(colors); + colorArray=objPalette_16(); } /* Write bitmap */ @@ -459,7 +464,7 @@ int objImage16Set(void* o,tResource* res) { compressed.data[3]=img->width>>8; /* (8 bits)00000000+(4 bits)palette type+(4 bits)algorithm */ compressed.data[4]=0; - compressed.data[5]=(img->bits==4?0xb0:0x00)|algorithm; + compressed.data[5]=0xb0|algorithm; res->content=compressed; mWriteFileInDatFile(res); diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c index 99096c9..5ac6b95 100644 --- a/PR/src/lib/object/object.c +++ b/PR/src/lib/object/object.c @@ -112,9 +112,16 @@ int writeObject(tObject o, const char* file, int optionflag, const char* backupE case eResTypeWave: /* save wav file */ error=objWaveWrite(o.obj,file,optionflag,backupExtension); break; + case eResTypeImage2: /* save image */ + error=objImage2Write(o.obj,file,optionflag,backupExtension); + break; case eResTypeImage16: /* save image */ error=objImage16Write(o.obj,file,optionflag,backupExtension); break; + case eResTypeImage256: /* save image */ +printf("hello\n"); + error=objImage256Write(o.obj,file,optionflag,backupExtension); + break; default: break; } @@ -150,8 +157,6 @@ tColor* paletteGetColorArray(tObject pal) { switch (pal.type) { case eResTypePop1Palette4bits: /* save and remember palette file */ return objPalette_pop1_4bitsGetColors(pal.obj); - case eResTypePop1PaletteMono: /* save and remember palette file */ - return objPalette_pop1_monoGetColors(pal.obj); default: return NULL; }