Skip to content

Commit

Permalink
palette fixes, handling short bit depth in palette mode...
Browse files Browse the repository at this point in the history
  • Loading branch information
EdouardBERGE committed Jan 6, 2025
1 parent 4b18e3f commit fa2e298
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
63 changes: 43 additions & 20 deletions convgeneric.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ void Build(struct s_parameter *parameter)
int idata=0; /* current output */
int pixrequested; /* how many pix do we need for a byte? */
int limitcolors=16; /* max number of colors regarding the options */
int hasblack; /* index of black color if any, else -1 */
int hasblack,is_hsp; /* index of black color if any, else -1 */
int hsptr=-1,hsptv=-1,hsptb=-1; /* transparency color */
struct s_sprite_info *spinfo=NULL; /* info about extracted sprites */
struct s_sprite_info curspi={0};
int ispi=0,mspi=0; /* fast counters for sprite info struct */
Expand Down Expand Up @@ -788,10 +789,11 @@ void Build(struct s_parameter *parameter)
}

if (parameter->search_transparency) {
// on va prendre la couleur à la con qu'on retrouve partout, en particulier dans les coin...
r=photo->data[photo->width*4+0]&0xF0;
v=photo->data[photo->width*4+1]&0xF0;
b=photo->data[photo->width*4+2]&0xF0;
// on va prendre la couleur à la con qu'on retrouve partout, en particulier dans les coins...
// à faire évoluer avec un forcage RGB...
hsptr=photo->data[photo->width*4+0]&0xF0;
hsptv=photo->data[photo->width*4+1]&0xF0;
hsptb=photo->data[photo->width*4+2]&0xF0;
}

//*****************************************************
Expand All @@ -809,7 +811,7 @@ void Build(struct s_parameter *parameter)
if (parameter->importpalettefilename) {
/* load palette from a text file */
char separator,*curchar,*txtpalette;
int palsize,curcoul,is_hsp;
int palsize,curcoul;
int has_reduce=0;

printf(KIO"Image %dx%d\n",photo->width,photo->height);
Expand All @@ -831,16 +833,18 @@ void Build(struct s_parameter *parameter)
}
if (strstr(txtpalette," HSP")) is_hsp=1; else is_hsp=0;
if (parameter->hsp!=is_hsp) {
printf(KWARNING"Warning, palette mode is different from output mode!\n");
printf(KWARNING"Warning, palette [%s] mode is different from output mode!\n",is_hsp?"HSP":"legacy");
}

/* parse text */
if (is_hsp) {
palette[0]=1;
palette[1]=2;
palette[2]=3;
// la couleur de la transparence
palette[0]=hsptr;
palette[1]=hsptv;
palette[2]=hsptb;
}
maxcoul=is_hsp;
printf("palette=[%s]\n",txtpalette);
while ((curchar=strchr(txtpalette,separator))!=NULL) {
if (maxcoul==16) {
printf(KERROR"\nERROR: invalid palette! too much colorz!\n"KNORMAL);
Expand All @@ -849,10 +853,15 @@ void Build(struct s_parameter *parameter)
*curchar=' ';
curchar++;
curcoul=strtol(curchar,NULL,16);
palette[maxcoul*3+0]=curcoul&0xF0;
palette[maxcoul*3+1]=(curcoul&0xF00)>>4;
palette[maxcoul*3+2]=(curcoul&0xF)<<4;
maxcoul++;
r=curcoul&0xF0;
v=(curcoul&0xF00)>>4;
b=(curcoul&0xF)<<4;
if (r!=hsptr || v!=hsptv || b!=hsptb) {
palette[maxcoul*3+0]=curcoul&0xF0;
palette[maxcoul*3+1]=(curcoul&0xF00)>>4;
palette[maxcoul*3+2]=(curcoul&0xF)<<4;
maxcoul++;
}
}
printf("%d couleur%s importée%s\n",maxcoul-is_hsp,maxcoul-is_hsp>1?"s":"",maxcoul-is_hsp>1?"s":"");

Expand Down Expand Up @@ -925,6 +934,7 @@ void Build(struct s_parameter *parameter)

MemFree(txtpalette);
} else {
is_hsp=parameter->hsp;
/* as the bitmap is RGBA we must scan to find all colors */
maxcoul=0;
png_has_transparency=0;
Expand Down Expand Up @@ -1218,8 +1228,14 @@ printf(KBLUE"expand image to %d\n"KNORMAL,photo->height);


printf("paletteplus: defw ");
for (i=parameter->hsp;i<maxcoul+parameter->hsp;i++) {
printf("%s#%03X",i-parameter->hsp?",":"",(palette[i*3+0]&0xF0)|((palette[i*3+1]>>4)<<8)|(palette[i*3+2]>>4));
if (parameter->importpalettefilename) {
for (i=parameter->hsp;i<maxcoul+parameter->hsp-(1-is_hsp);i++) {
printf("%s#%03X",i-parameter->hsp?",":"",(palette[i*3+0]&0xF0)|((palette[i*3+1]>>4)<<8)|(palette[i*3+2]>>4));
}
} else {
for (i=parameter->hsp;i<maxcoul+parameter->hsp;i++) {
printf("%s#%03X",i-parameter->hsp?",":"",(palette[i*3+0]&0xF0)|((palette[i*3+1]>>4)<<8)|(palette[i*3+2]>>4));
}
}
printf("\n");

Expand Down Expand Up @@ -1283,11 +1299,18 @@ printf(KBLUE"expand image to %d\n"KNORMAL,photo->height);
ticpack=0; /* reset pixel counter */
for (xs=metax;xs<16+metax;xs++) {
/* transparency */
if (photo->data[(i+xs+(j+ys)*photo->width)*4+3]>0) {
zepix=GetIDXFromPixel(palette,&photo->data[(i+xs+(j+ys)*photo->width)*4+0]);
// RIEN A FAIRE!!! if (!parameter->black) zepix++;
} else {
if (parameter->search_transparency
&& photo->data[(i+xs+(j+ys)*photo->width)*4+0]==hsptr
&& photo->data[(i+xs+(j+ys)*photo->width)*4+1]==hsptv
&& photo->data[(i+xs+(j+ys)*photo->width)*4+2]==hsptb ) {
zepix=0;
} else {
if (photo->data[(i+xs+(j+ys)*photo->width)*4+3]>0) {
zepix=GetIDXFromPixel(palette,&photo->data[(i+xs+(j+ys)*photo->width)*4+0]);
// RIEN A FAIRE!!! if (!parameter->black) zepix++;
} else {
zepix=0;
}
}
/* output format */
switch (parameter->packed) {
Expand Down
3 changes: 2 additions & 1 deletion minilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ printf("PNG opening [%s]\n",filename);
pixel_size=3;
}
png_set_palette_to_rgb(png_ptr);
png_info->bit_depth=8;
break;
case PNG_COLOR_TYPE_GRAY:
pixel_size=3;
Expand Down Expand Up @@ -454,7 +455,7 @@ printf("PNG read OK\n");
png_info->color_type=PNG_COLOR_TYPE_RGBA;
png_info->data=ImageFreeRRGGBBToRRGGBBAA(png_info->data,png_info->width,png_info->height);
} else {
logerr("INTERNAL ERROR - cannot process PNG BD:%d CT:%d",png_info->bit_depth,png_info->color_type);
logerr("INTERNAL ERROR - cannot process PNG BitDepth:%d ColType:%d",png_info->bit_depth,png_info->color_type);
exit(INTERNAL_ERROR);
}
}
Expand Down

0 comments on commit fa2e298

Please sign in to comment.