Red Squirrel
No Lifer
I'm writing a png library wrapper to make png image generation easier and adding various features like color addition, adding opacity etc... Just wondering if I'm on the right track.
Basically there's different modifiers that can be added before a pixel write operation is done. There's opacity, which will make it so that whatever I write is semi transparant and takes the colors of the existing pixels into account. this is not to be confused with png alpha, that's not what this is. Basically equivalent to adding transparancy to a layer in photoshop.
Then there's addition and substraction, I think I got those down path, if it's really as simple as I think it is.
The results I'm getting SEEM right, here is the code with calculations:
I'm also open to other fun things I can maybe add as modifiers. Basically these are just bool variables that I can apply before a function is called such as plotting a rectangle. By default it will just fully overwrite the pixels.
Basically there's different modifiers that can be added before a pixel write operation is done. There's opacity, which will make it so that whatever I write is semi transparant and takes the colors of the existing pixels into account. this is not to be confused with png alpha, that's not what this is. Basically equivalent to adding transparancy to a layer in photoshop.
Then there's addition and substraction, I think I got those down path, if it's really as simple as I think it is.
The results I'm getting SEEM right, here is the code with calculations:
Code:
//writes a single pixel to memory
void rspng::WritePixel(int x,int y, int red, int green, int blue, int alpha=255)
{
if(m_width==0 || m_height==0 || m_imagedata==NULL)return; //size not determined, we just bail
if (x>m_width || y>m_height) return; //out of range, just bail
//fix within proper range:
if(red>255)red=255;
if(red<0)red=0;
if(green>255)green=255;
if(green<0)green=0;
if(blue>255)blue=255;
if(blue<0)blue=0;
if(alpha>255)alpha=255;
if(alpha<0)alpha=0;
//debug msg
if(DebugLevel(5))
{
stringstream ss;
ss<<"Write ("<<red<<","<<green<<","<<blue<<","<<alpha<<") to ("<<x<<","<<y<<")...";
Debug(5,ss.str());
}
//get the existing pixel:
pngpixel * pixel = GetPixel(x,y);
if(pixel==NULL) return; //this should not happen because of previous checks...
//temp variables to store final color
int redtw=red;
int greentw=green;
int bluetw=blue;
int alphatw=alpha;
//existing colors:
int redc=pixel->GetRed();
int greenc=pixel->GetGreen();
int bluec=pixel->GetBlue();
int alphac=pixel->GetAlpha();
float opacity=(float)mod_opacity_perc/100; //convert to decimal to make rest of calculations easier
//check modifiers and make any color calculations:
if(mod_addition)
{
redtw=(red*opacity)+redc;
greentw=(green*opacity)+greenc;
bluetw=(blue*opacity)+bluec;
}
if(mod_substraction)
{
redtw=redc-(red*opacity);
greentw=greenc-(green*opacity);
bluetw=bluec-(blue*opacity);
}
//if no mods were applied, do a regular write taking opacity into account unless opacity is 100 then we just skip this part (tw vars already have function sent values)
if(mod_opacity_perc<100 && !mod_addition && !mod_substraction)
{
float opacityrev = 1-opacity;
redtw=(redc*opacityrev)+(red*opacity);
greentw=(greenc*opacityrev)+(green*opacity);
bluetw=(bluec*opacityrev)+(blue*opacity);
}
//fix within proper range: (again in case any transformations happened)
if(redtw>255)redtw=255;
if(redtw<0)redtw=0;
if(greentw>255)greentw=255;
if(greentw<0)greentw=0;
if(bluetw>255)bluetw=255;
if(bluetw<0)bluetw=0;
if(alphatw>255)alphatw=255;
if(alphatw<0)alphatw=0;
pixel->SetColor((unsigned char)redtw,(unsigned char)greentw,(unsigned char)bluetw,(unsigned char)alphatw);
}
I'm also open to other fun things I can maybe add as modifiers. Basically these are just bool variables that I can apply before a function is called such as plotting a rectangle. By default it will just fully overwrite the pixels.