Color bleeding (fading to greyscale)- redux

I had written a bit ago about bleeding the colors from an image, leaving
it greyscape (got no response… taking that as meaning no one on this
list has really tried ;). I think I’ve figured out a way to do it using
a variation to the palette fade to black example in the docs.

Essentially, we have two SDL_Color arrays, say color[] which
has the color palette from the image, and new_color[]. Then, we run
through it, checking each r,g,b value, and setting the lower two to the
higher one (i.e., if r is 200, g is 50 and b is 30, then setting g=200 and
b=200). Small portion of the routine would be:

for(i=0; i<ncolors; i++){
if(color[i].r > color[i].g && color[i].r > color[i].b) {
new_color[i].g = color[i].r;
new_color[i].b = color[i].r;
} else
…etc., checking for each color as above…
}

in theory, this ought to work (correct me if I’m wrong).

However, the problems I’m having now is that my surfaces aren’t
palettized! I’m using GIFs (which I thought were automatically palettized,
but I guess not). I’m also using the SDL_HWPALETTE flag (which I guess I
don’t understand as well as I thought I did).

How do I force a palette on a surface? (I’ve thought SDL_ConverSurface or
some such thing ought to do it… but couldn’t get it to work). Is
this even possible? Is there a better image format I should use?

Ultimately, I’m moving on in my project… but would like to impliment
this in the future.–
Sam Hart http://www.physics.arizona.edu/~hart/
Web Page Highlights: Video Game History, Black Hole Simulation, & more.
OTHER WEB SITES MAINTAINED BY SAM HART
http://www.geekcomix.com/ - Geekcomix, the Daily Geek Comic Strip Site
http://www.physics.arizona.edu/~hart/gw/ - Ghostworks (Alt./Linux Computing)

Sam Hart wrote:

I had written a bit ago about bleeding the colors from an image, leaving
it greyscape (got no response… taking that as meaning no one on this
list has really tried ;). I think I’ve figured out a way to do it using
a variation to the palette fade to black example in the docs.
Woops, I think I have one - sorry to be lazy in responding… :slight_smile:

for(i=0; i<ncolors; i++){
if(color[i].r > color[i].g && color[i].r > color[i].b) {
new_color[i].g = color[i].r;
new_color[i].b = color[i].r;
} else
…etc., checking for each color as above…
}

in theory, this ought to work (correct me if I’m wrong).

Welp, you might be right, but it seems rather complicated to me. If it
doesn’t work, try something like:

for (i=1; i<NUMBER_OF_STEPS+1; ++i){
for (j=0;j<256;++j){
new_color[j].r =(color[j].r*(NUMBER_OF_STEPS-i)+
i*(color[j].r+color[j].g+color[j].b)/3)/NUMBER_OF_STEPS;
new_color[j].g =(color[j].g*(NUMBER_OF_STEPS-i)+
i*(color[j].r+color[j].g+color[j].b)/3)/NUMBER_OF_STEPS;
new_color[j].b =(color[j].b*(NUMBER_OF_STEPS-i)+
i*(color[j].r+color[j].g+color[j].b)/3)/NUMBER_OF_STEPS;
}
}

You can easily fade back if you wish to… For funny effects create a 3*3
matrix with random numbers between e.g. 1 and 3.
Multiply each entrance of the matrix to the corresponding color
component and divide by the total sum of each row. That will fade to
near grey with a slight variation on which colors will be lighter and
darker and adding a random hue to all of it… (I do that for a
pause-effect)… Something like:

for (i=0; i<3; i++)
for (j=0; j<3; j++)
m[i][j]=1+Random(2);

and then replace each of the lines in the fader with something like this
(for 10 steps):

new_color[j].r =(color[j].r*(10-i)+ i*(m[0][0]*color[j].r+
m[0][1]*color[j].b+

m[0][2]*color[j].g)/(m[0][0]+m[0][1]+m[0][2]))/10;
etc.

At least I think it looks kinda neat… :slight_smile:

However, the problems I’m having now is that my surfaces aren’t
palettized! I’m using GIFs (which I thought were automatically palettized,
but I guess not). I’m also using the SDL_HWPALETTE flag (which I guess I
don’t understand as well as I thought I did).
8bpp is the inly palletized mode, so you can’t use more than 256
colors… But as you are using gifs I guess that is no problem…

How do I force a palette on a surface? (I’ve thought SDL_ConverSurface or
some such thing ought to do it… but couldn’t get it to work). Is
this even possible? Is there a better image format I should use?
You could use SDL_ConverSurface to convert images to 8bpp if you were
using more colors, but with the gifs you shouldn’t need to…

Hope this is of any use - and the very best of luck with the project.
Yours sincerely,
/morten bek