author | ecalot
<ecalot> 2006-06-15 06:43:22 UTC |
committer | ecalot
<ecalot> 2006-06-15 06:43:22 UTC |
parent | 15d1233f56e6c1e4e32cb9dd0926862993e294b1 |
PR/src/include/image.h | +6 | -2 |
PR/src/include/palette.h | +2 | -1 |
PR/src/include/pallist.h | +5 | -4 |
PR/src/lib/actions/export.c | +20 | -5 |
PR/src/lib/layers/autodetect.c | +5 | -5 |
PR/src/lib/layers/pallist.c | +30 | -18 |
PR/src/lib/object/image/image16.c | +6 | -4 |
PR/src/lib/object/image/image2.c | +1 | -0 |
PR/src/lib/object/image/image256.c | +22 | -3 |
PR/src/lib/object/image/image_common.c | +6 | -4 |
PR/src/lib/object/object.c | +14 | -10 |
PR/src/lib/object/palette/pop2_256c.c | +4 | -3 |
diff --git a/PR/src/include/image.h b/PR/src/include/image.h index 6b07e56..2e2f7cd 100644 --- a/PR/src/include/image.h +++ b/PR/src/include/image.h @@ -56,6 +56,7 @@ typedef struct { int widthInBytes; int height; int bits; + int colorCount; unsigned char* pix; tObject pal; unsigned char type; /* XY where X=B if 4 bits or X=0 if 1 bit; Y=algorithm */ @@ -71,9 +72,9 @@ int objImage256Set(void* o,tResource* res); int objImage256Write(void* img,const char* file,int optionflag,const char* backupExtension); int objImage2Set(void* o,tResource* res); int objImage2Write(void* img,const char* file,int optionflag,const char* backupExtension); -void* objImage16Create(tBinary c, tObject palette, int *error); +void* objImage16Create(tBinary c, int *error); void* objImage16Read(const char* file,tObject palette, int *result); -void* objImage256Create(tBinary c, tObject palette, int *error); +void* objImage256Create(tBinary c, int *error); void* objImage256Read(const char* file,tObject palette, int *result); void* objImage2Create(tBinary c, int *error); void* objImage2Read(const char* file,tObject palette, int *result); @@ -84,4 +85,7 @@ void* objImage2Read(const char* file,tObject palette, int *result); int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes); int mCompressGraphic(tBinary* input, tBinary* output, int ignoreFirstBytes, int w, int h); +int objImageGetColorCount(void* img); +void applyPalette(tObject image, tObject palette); + #endif diff --git a/PR/src/include/palette.h b/PR/src/include/palette.h index cb94f6f..ad5c870 100644 --- a/PR/src/include/palette.h +++ b/PR/src/include/palette.h @@ -87,10 +87,11 @@ tColor* objPalette_pop2_ncolorsGetColors(void* o); /* Hooks */ -#define PAL_COLORS_eResTypePop2PaletteNColors /*256*/ getColorsByPop2PaletteObject(pal.obj) +#define PAL_COLORS_eResTypePop2PaletteNColors /*256*/ getColorsByPop2PaletteObject(object.obj) #define PAL_COLORS_eResTypePop1Palette4bits 16 #define PAL_COLORS_eResTypePop1PaletteMono 2 int getColorsByPop2PaletteObject(void* o); #endif + diff --git a/PR/src/include/pallist.h b/PR/src/include/pallist.h index 591314b..e8a3187 100644 --- a/PR/src/include/pallist.h +++ b/PR/src/include/pallist.h @@ -52,14 +52,15 @@ tPaletteList paletteListCreate(); typedef enum {highPriority, lowPriority}tPriority; typedef struct pln{ tResourceId resid; - tObject* object; + tObject object; struct pln* next; } tPL_Node; typedef struct { struct { + int enabled; tResourceId idres; - tObject* object; + tObject object; } priority_field; tPL_Node* list_first; tPL_Node* list_deleted_first; @@ -67,9 +68,9 @@ typedef struct { void pl_free (tPL* pl); int pl_tryAdd(tPL* pl, tResourceId resid, tPriority p); -void pl_add (tPL* pl, tObject* o, tResourceId resid, tPriority p); +void pl_add (tPL* pl, tObject o, tResourceId resid, tPriority p); int pl_hasPriority(tPL* pl, tResourceId resid); tPL pl_create(); -tObject* pl_get(tPL* pl, int* priorityRight, int colors); +tObject pl_get(tPL* pl, int* priorityRight, int colors); #endif diff --git a/PR/src/lib/actions/export.c b/PR/src/lib/actions/export.c index c62f7e6..34d6af4 100644 --- a/PR/src/lib/actions/export.c +++ b/PR/src/lib/actions/export.c @@ -63,6 +63,7 @@ extern FILE* outputStream; Extracts a DAT file For parameter documentation, see pr.c */ +void showobj(tObject o); /* TODO: only for debug purposes, delete this line */ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int optionflag, const char* vDatFileName, const char* vDatAuthor,const char* backupExtension,const char* format) { char file[MAX_FILENAME_SIZE]; @@ -83,7 +84,7 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti /* initialize palette buffer */ /*paletteBuffer=paletteListCreate();*/ /* initialize the default palette */ - pl_add(&palettes,¤tPalette,bufferedPalette,lowPriority); /* The null object will be used until a palette is set */ + pl_add(&palettes,currentPalette,bufferedPalette,lowPriority); /* The null object will be used until a palette is set */ ok=1; /* main loop */ @@ -114,9 +115,10 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti case eResTypePop1Palette4bits: { /* save and remember palette file */ /*tPaletteListItem e; * deprecated */ o=getObject(&res,&ok); +printf("new palette object: o=%p type=%d\n",o.obj,o.type); if (!ok) { /* if SUCCESS remember the palette, otherwise keep using the default one */ - pl_add(&palettes,&o,res.id,lowPriority); + pl_add(&palettes,o,res.id,lowPriority); /*e.pal=currentPalette=o; e.id=res.id; list_insert(&paletteBuffer,(void*)&e);*/ @@ -128,11 +130,14 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti /* if (resourceListCompareId(res.paletteId,bufferedPalette) * add &&!paletteCheckCompatibility(currentPalette,image) *) { * The palette isn't in the buffer */ tResource otherPalette; int priorityRight; + tObject pal; otherPalette.id=res.paletteId; /* TODO: use the try system */ /* Read the palette and load it into memory */ if (mReadFileInDatFileId(&otherPalette)==PR_RESULT_SUCCESS) { o=getObject(&otherPalette,&ok); - pl_add(&palettes,&o,res.paletteId,highPriority); +printf("adding "); +showobj(o); + pl_add(&palettes,o,res.paletteId,highPriority); /*tPaletteListItem e; resourceListAddInfo(r,&otherPalette);*/ @@ -140,13 +145,22 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti /*e.pal=currentPalette=getObject(&otherPalette,&ok); e.id=res.id;*/ /*list_insert(&paletteBuffer,(void*)&e);*/ - } /* else, that's bad, I'll have to use the previous palette, even if it is the default */ + } else { /*, that's bad, I'll have to use the previous palette, even if it is the default */ + printf("Warning: the selected palette doesn't exist in the file, the extracted image could result in a wrong palette\n"); + } /* else, good, the palette is buffered */ /*res.palette=currentPalette;*/ - res.palette=pl_get(&palettes,&priorityRight,getColorsByType(res.type)); + o=getObject(&res,&ok); +printf("getting the palette for the %d colours object ",paletteGetColors(o)); +showobj(o); + pal=pl_get(&palettes,&priorityRight,paletteGetColors(o)); +printf("palette "); +showobj(pal); + applyPalette(o,pal); if (!priorityRight) { printf("Warning: the selected palette can't be applied to the image\n"); } + break; } default: o=getObject(&res,&ok); @@ -169,6 +183,7 @@ int extract(const char* vFiledat,const char* vDirExt, tResourceList* r, int opti } TODO: add warning counter */ ok=1; /* efit the for and add !fatal(ok)*/ if (ok) count++; + printf("done with %s\n",file); } else { /* If the DAT file is unknown, add it in the XML */ getFileName(file,vDirExt,&res,vFiledat,vDatFileName,optionflag,backupExtension,format); diff --git a/PR/src/lib/layers/autodetect.c b/PR/src/lib/layers/autodetect.c index b271d3d..288fe08 100644 --- a/PR/src/lib/layers/autodetect.c +++ b/PR/src/lib/layers/autodetect.c @@ -77,9 +77,9 @@ int verifyPaletteHeaderPop1(tBinary c) { int verifyPaletteHeaderPop2(tBinary c) { return ( (c.size>=(3*16)) && - (c.data[0]==0x00) && +/* (c.data[0]==0x00) && (c.data[1]==0x00) && - (c.data[2]==0x00) && + (c.data[2]==0x00) &&*/ (c.size % 3 == 0 ) && isA64kPalette(c) ); @@ -89,7 +89,7 @@ int verifyPaletteHeaderPop2_256(tBinary c) { /* tBinary c2; c2.size=c.size-1; c2.data=c.data+1;*/ - return ( + return 0&&( ((c.size==(256*3+1)||c.size==(320*3))&&isA64kPalette(c)) /* TODO: the +1 is for debug purposes, it must be eliminated */ ); } @@ -126,8 +126,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_256 (c)) return eResTypePop2PaletteNColors; - if (verifyPaletteHeaderPop2 (c)) return eResTypePop2Palette4bits; + /*if (verifyPaletteHeaderPop2 (c)) return eResTypePop2Palette4bits;*/ + if (verifyPaletteHeaderPop2 (c)) return eResTypePop2PaletteNColors; if (verifyWaveHeader (c)) return eResTypeWave; if (verifySpeakerHeader (c)) return eResTypePcspeaker; return eResTypeBinary; diff --git a/PR/src/lib/layers/pallist.c b/PR/src/lib/layers/pallist.c index ab65f13..f33d1e2 100644 --- a/PR/src/lib/layers/pallist.c +++ b/PR/src/lib/layers/pallist.c @@ -49,7 +49,7 @@ tPaletteList paletteListCreate() { #include <stdlib.h> #include "object.h" -#ifdef DEBUG_TEST_PALLST +#ifndef DEBUG_TEST_PALLST void showobj(tObject o) { printf("object type=%d colors=%d\n",o.type,paletteGetColors(o)); } @@ -75,7 +75,7 @@ pl_dellist_add(tPL* pl, tResourceId idres, tObject* obj) { tPL pl_create() { tPL r; - r.priority_field.object=NULL; + r.priority_field.enabled=0; r.list_first=NULL; r.list_deleted_first=NULL; @@ -83,7 +83,7 @@ tPL pl_create() { } int pl_hasPriority(tPL* pl, tResourceId resid) { - if (!pl->priority_field.object) return 0; /* false */ + if (!pl->priority_field.enabled) return 0; /* false */ return resourceListCompareId(resid,pl->priority_field.idres)==0; } @@ -91,43 +91,53 @@ int pl_tryAdd(tPL* pl, tResourceId resid, tPriority p) { return 0; /* always false, optimization disabled */ } -void pl_add(tPL* pl, tObject* o, tResourceId resid, tPriority p) { +void pl_add(tPL* pl, tObject o, tResourceId resid, tPriority p) { if (p==highPriority) { +printf("adding with high priority:\n"); +showobj(o); /* high priority insertion */ - if (pl->priority_field.object) { /* if there was another object proprized, move it to the list */ - tObject* obj_old_priority=pl->priority_field.object; + if (pl->priority_field.enabled) { /* if there was another object proprized, move it to the list */ + tObject obj_old_priority=pl->priority_field.object; + pl->priority_field.enabled=0; if (resourceListCompareId(resid,pl->priority_field.idres)==0) return; /* same object, take no action */ /* drop the object from the priority field and reinsert it with low priority */ - pl->priority_field.object=NULL; pl_add(pl,obj_old_priority,pl->priority_field.idres,lowPriority); } /* now, we know there is no object in the priority field, so we insert it */ pl->priority_field.object=o; pl->priority_field.idres=resid; + pl->priority_field.enabled=1; } else { /* low priority insertion */ tPL_Node* insertNode=malloc(sizeof(tPL_Node)); - int colors=paletteGetColors(*o); + int colors=paletteGetColors(o); +printf("adding with low priority:\n"); +showobj(o); - while (pl->list_first && colors>=paletteGetColors(*(pl->list_first->object))) { - /*printf("deleting: "); - showobj(*pl->list_first->object);*/ + while (pl->list_first && colors>=paletteGetColors(pl->list_first->object)) { + printf("deleting: "); + showobj(pl->list_first->object); pl->list_first=pl->list_first->next; /* Delete */ } insertNode->next=pl->list_first; insertNode->object=o; insertNode->resid=resid; pl->list_first=insertNode; +printf("inserting "); +showobj(pl->list_first->object); } return; } -tObject* pl_get(tPL* pl, int* priorityRight, int colors) { +tObject pl_get(tPL* pl, int* priorityRight, int colors) { tPL_Node* node; - + int junk; +printf("getting PL\n"); + *priorityRight=1; - if (pl->priority_field.object) { - if (colors<=paletteGetColors(*pl->priority_field.object)) { + if (pl->priority_field.enabled) { + if (colors<=paletteGetColors(pl->priority_field.object)) { +showobj(pl->priority_field.object); return pl->priority_field.object; } else { *priorityRight=0; @@ -135,11 +145,13 @@ tObject* pl_get(tPL* pl, int* priorityRight, int colors) { } node=pl->list_first; +printf("first=%p with ",(void*)pl->list_first); +showobj(pl->list_first->object); - while (node && colors>paletteGetColors(*node->object)) - node=node->next; + while (node && colors>paletteGetColors(node->object)) + { node=node->next; printf("next %p\n",node); } - return node?node->object:NULL; + return node?node->object:getObject(NULL,&junk); } #ifdef DEBUG_TEST_PALLST diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c index 539d608..1308fd0 100644 --- a/PR/src/lib/object/image/image16.c +++ b/PR/src/lib/object/image/image16.c @@ -351,7 +351,7 @@ tColor* objPalette_16() { return c; } -void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ +void* objImage16Create(tBinary cont, int *error) { /* use get like main.c */ /* * This function will expand the data into an image structure, @@ -362,7 +362,7 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l */ tImage* image; - int bits; + /*int bits;*/ image=(tImage*)malloc(sizeof(tImage)); /* Expand graphic and check results */ @@ -373,11 +373,13 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l free(image); 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); + image->bits=getCarry(image->type);*/ + + image->colorCount=2; return (void*)image; } diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c index 1cd7cc6..7f6cad0 100644 --- a/PR/src/lib/object/image/image2.c +++ b/PR/src/lib/object/image/image2.c @@ -160,6 +160,7 @@ void* objImage2Read(const char* file,tObject palette, int *result) { free(image); return NULL; } + image->colorCount=2; return (void*)image; } diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c index 9bcf118..2cc3ec5 100644 --- a/PR/src/lib/object/image/image256.c +++ b/PR/src/lib/object/image/image256.c @@ -293,7 +293,7 @@ int pop2decompress(const unsigned char* input, int inputSize, int verify, unsign extern FILE* outputStream; -void* objImage256Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ +void* objImage256Create(tBinary cont, int *error) { /* use get like main.c */ /* * This function will expand the data into an image structure, @@ -304,7 +304,7 @@ void* objImage256Create(tBinary cont, tObject palette, int *error) { /* use get */ tImage* image; - int bits; + /*int bits;*/ image=(tImage*)malloc(sizeof(tImage)); /* Expand graphic and check results */ @@ -316,9 +316,18 @@ void* objImage256Create(tBinary cont, tObject palette, int *error) { /* use get return NULL; } + { int i; + int max=0; + for (i=0;i<image->height*image->widthInBytes;i++) { + if (image->pix[i]>max) max=image->pix[i]; + } + printf("max pixel in this image is %d\n",max); + image->colorCount=max; + } + /* 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)); + if (bits && bits!=getCarry(image->type)) printf("error, palette mismatch (pal=%d bits=%d)\n",bits,getCarry(image->type));*/ image->bits=getCarry(image->type); return (void*)image; @@ -424,3 +433,13 @@ int objImage256Set(void* o,tResource* res) { return PR_RESULT_SUCCESS; } +/* common function TODO: move */ +int objImageGetColorCount(void* img) { + tImage* i=img; + return i->colorCount; +} + +void applyPalette(tObject image, tObject palette) { + tImage* i=image.obj; + i->pal=palette; +} diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c index 539d608..1308fd0 100644 --- a/PR/src/lib/object/image/image_common.c +++ b/PR/src/lib/object/image/image_common.c @@ -351,7 +351,7 @@ tColor* objPalette_16() { return c; } -void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get like main.c */ +void* objImage16Create(tBinary cont, int *error) { /* use get like main.c */ /* * This function will expand the data into an image structure, @@ -362,7 +362,7 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l */ tImage* image; - int bits; + /*int bits;*/ image=(tImage*)malloc(sizeof(tImage)); /* Expand graphic and check results */ @@ -373,11 +373,13 @@ void* objImage16Create(tBinary cont, tObject palette, int *error) { /* use get l free(image); 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); + image->bits=getCarry(image->type);*/ + + image->colorCount=2; return (void*)image; } diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c index 9169061..afec329 100644 --- a/PR/src/lib/object/object.c +++ b/PR/src/lib/object/object.c @@ -39,7 +39,7 @@ main.c: Princed Resources : Main item class implementation #include "palette.h" #include "sound.h" -/* Object polimorphism support layer */ +/* Object polimorphism hook layer */ tObject getObject(tResource* r, int* error) { tObject o; @@ -82,10 +82,10 @@ tObject getObject(tResource* r, int* error) { o.obj=objImage2Create(r->content,error); break; case eResTypeImage16: /* save image */ - o.obj=objImage16Create(r->content,*r->palette,error); + o.obj=objImage16Create(r->content,error); break; case eResTypeImage256: /* save image */ - o.obj=objImage256Create(r->content,*r->palette,error); + o.obj=objImage256Create(r->content,error); break; default: printf("Exception: Unhooked type %d\n",o.type); @@ -111,8 +111,8 @@ int writeObject(tObject o, const char* file, int optionflag, const char* backupE case eResTypePop1Palette4bits: /* save and remember palette file */ error=objPalette_pop1_4bitsWrite(o.obj,file,optionflag,backupExtension); break; - case eResTypePop2Palette4bits: /* save and remember palette file */ - error=objPalette_pop2_4bitsWrite(o.obj,file,optionflag,backupExtension); + case eResTypePop2PaletteNColors: /* save and remember palette file */ + error=objPop2PaletteNColorsWrite(o.obj,file,optionflag,backupExtension); break; case eResTypePcspeaker: /* save pcs file */ error=objPcspeakerWrite(o.obj,file,optionflag,backupExtension); @@ -156,8 +156,8 @@ int paletteGetBits(tObject pal) { } } -int paletteGetColors(tObject pal) { - switch (pal.type) { +int paletteGetColors(tObject object) { /* TODO: rename to objectGetColors */ + switch (object.type) { case eResTypePop2PaletteNColors: return PAL_COLORS_eResTypePop2PaletteNColors; /*256;*/ case eResTypePop1Palette4bits: @@ -166,12 +166,16 @@ int paletteGetColors(tObject pal) { case eResTypePop1PaletteMono: return PAL_COLORS_eResTypePop1PaletteMono; /*2;*/ case eResTypeNone: - return 256; /*256; TODO: use the pal none object */ + return 256; /*256; TODO: use the object none object */ + case eResTypeImage16: + case eResTypeImage2: + case eResTypeImage256: + return objImageGetColorCount(object.obj); default: return 0; } } - +/* int getColorsByType(tResourceType t) { switch (t) { case eResTypeImage16: @@ -184,7 +188,7 @@ int getColorsByType(tResourceType t) { return 0; } } - +*/ tColor* paletteGetColorArray(tObject pal) { switch (pal.type) { diff --git a/PR/src/lib/object/palette/pop2_256c.c b/PR/src/lib/object/palette/pop2_256c.c index 7df4571..639a69f 100644 --- a/PR/src/lib/object/palette/pop2_256c.c +++ b/PR/src/lib/object/palette/pop2_256c.c @@ -57,7 +57,7 @@ void* objPop2PaletteNColorsCreate(tBinary cont, int *error) { *error=PR_RESULT_SUCCESS; - if (!isA64kPalette(cont)) { + if (!isA64kPalette(cont)) { /* TODO: use autodetect function to verify */ *error=-14; /* TODO FIX or assign error code */ return NULL; } @@ -65,12 +65,13 @@ void* objPop2PaletteNColorsCreate(tBinary cont, int *error) { 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].r=to8bits_B(cont.data[i+0]); /* TODO: use increase per line i++ */ r->colorArray[j].g=to8bits_B(cont.data[i+1]); r->colorArray[j].b=to8bits_B(cont.data[i+2]); } - if (j!=256 && j!=320) return NULL; /*TODO: add free */ + /*if (j!=256 && j!=320) return NULL; *TODO: add free */ r->size=j; +printf("created a palette with %d colors. cs=%d\n",j,cont.size); return (void*)r; }