View Full Version : RGB Maths
clueless
05-12-2008, 09:03 PM
Ive been trying to work out how to merge to bitmaps with added fade. If i want an even fade between the two pictures the process is quite simple, I can go though the pictures pixel by pixel ,Split the matching pixels into RGB values, add them together
Red =(RedA + RedB) / 2
Green = (GreenA + GreenB) / 2
Blue = (BlueA + BlueB) / 2
This is the average color of the 2 combined.
Now i want to be able to set the level of fade on one PicB as its merged onto pic A (which has no fade).
I tried the same calculation as before but halved (50%) of the color channels for picB
Red = (RedA + (50% of RedB)) / 2
Green = (GreenA + (50% of GreenB)) / 2
Blue = (BlueA + (50% of BlueB)) / 2
but instead of getting a weaker image of picB on top of PicA i just get a darker one.
Does anyone know how i can merge the two pics correctly?
thanks.
clueless
05-12-2008, 10:51 PM
I tried a weighted average
weighted average = the sum of the values + the weights assigned to [each value] divided by the sum of the weights
or
x=Exw/Ew
so If picA Red channel was 120 and PicB channel was (50 % of 80) the calculation would be;
((120*1)+(80*0.5))/(1+0.5)
This is slightly better but still to dark.
ShadowUK
05-13-2008, 12:26 AM
function Create(Red,Green,Blue)
return ((Red) + (Green*256) + (Blue*256*256));
end
rgb.lua, Gallery\Scripts\
Dunno if that's what your looking for or not. Don't really understand what you're looking for.
clueless
05-13-2008, 02:36 AM
Thanks, Ive been advised to try this method;-
* Find the average R/G/B values for image B.
* Create a "virtual image" with differential RGB-values, i.e. the values for each pixel will be the differences of the original values for that pixel from the average values for the image. This will produce negative as well as positive values, so this is not an image you can display, of course.
* Now apply this virtual image to image A, multiplying by a fade factor, i.e.
Red = RedA + f*RedV
This will produce a result that has the same brightness as the original A, since you're adding an image with brightness 0, but exhibiting the brightness and colour variations of B superimposed on those of A. This would be called a "Hue/Saturation" blend in Photoshop, I think.
looks promising..
clueless
05-13-2008, 05:43 AM
Ive had an idea,Sside has made 2 dll's that can Read/Write pixels and convert color values between win32 and .net. The .net color values have an extra color channel for opacity so It would just be a case of scanning though picB pixel by pixel converting all the colors first to RGB then adding a value for opacity and converting it back to .Net. Then just write the .net value to picA. I think the mixing of the final colors should be automatic.
Its a shame i haven't got the Declarations and function calls for the dll's in VB, then i could just make a commandline prog to do the job and benefit from the extra speed. I'm going to have a go at guessing them but i haven't had much luck at doing this in the past.lol
Lorne
05-13-2008, 10:55 AM
Look up "alpha blending" on google. The traditional formula is:
result = ( ALPHA * ( srcPixel - destPixel ) ) / 256 + destPixel
...where ALPHA is a value from 0 to 255.
For RGB color modes, you need to do that for each channel (red, green, and blue).
There are various ways to optimize it further, but that basic formula should get you on the right track.
I wouldn't expect your approach to be fast in any case, since your overhead will be way too high doing it one pixel at a time. But it would be a good learning exercise! :)
sside
05-13-2008, 07:19 PM
clueless
Can you post here an manipulated image to show me what you're trying to accomplish?
Its a shame i haven't got the Declarations and function calls for the dll's in VB, then i could just make a commandline prog to do the job and benefit from the extra speed. I'm going to have a go at guessing them but i haven't had much luck at doing this in the past.lol
I thought forum member Rizla did post you the vb declarations? Have you tried it and it didn't work out?
With Kind Regards
sside
clueless
05-14-2008, 03:47 AM
Lorne: thanks ill give that a try.
Sside: Sure, this is the effect im trying to achieve:
I used paintshop pro with blending on normal.Picture A is at full opacity. Pic B is placed into an empty layer which then has its opacity altered in percentage increments of 10.
I had a problem with one or two of the declarations.
clueless
05-21-2008, 07:55 PM
I dont think its possible to use the dll's as they are now with Visual basic. Every time ive attempted to use them i get
Error 49: Bad Dll calling convention
I asked around at few other forums and have been told
A calling convention error most likely means the DLL uses the cdecl convention. You'll would need to add _stdcall to the function signatures and recompile. Alternatively, write a TLB with the _cdecl convention.
RizlaUK
05-22-2008, 08:46 AM
A calling convention error most likely means the DLL uses the cdecl convention. You'll would need to add _stdcall to the function signatures and recompile. Alternatively, write a TLB with the _cdecl convention.
or call the dll with the "DLL_CALL_CDECL" flag.
Imagine Programming
05-22-2008, 11:17 AM
haha xD nice...
clueless
05-22-2008, 12:09 PM
That sounds very promising :) , could you show me how thats done?
clueless
05-22-2008, 02:05 PM
Im realy not sure what you mean , i put "visual basic dll DLL_CALL_CDECL" into google and got nothing.
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.