Code archives/Graphics/Convert pixmap to grey
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This function take as input a pixmap and convert it to a new one in grey colour. It preserves the alpha information. Useful to create 'enabled' or 'disabled' icons in GUI. Some routines are taken from the forum section and from other archives, I 'glued them' togheter in this source code. Update with the xlsior suggestion about grey shades. | |||||
Rem From a SOURCE pixmap returns a grey converted one Usage: Local grey_pix:tpixmap=MakeGrey(LoadPixmap("colored_pixmap.png")) End Rem Function MakeGrey:TPixmap(source:TPixmap=Null) If source=Null Return Null Local r:Int,g:Int,b:Int,argb:Int,a:Int,c:Int Local x:Int,y:Int Local tmp_grey:TImage tmp_grey=CreateImage(source.WIDTH,source.HEIGHT) Local pm2:TPixmap=LockImage(tmp_grey) For y=0 Until source.HEIGHT For x=0 Until source.WIDTH argb=ReadPixel(source,x,y) a=argb Shr 24 & $ff r = (argb Shr 16) & $ff g = (argb Shr 8) & $ff b = argb & $ff 'c=(r+g+b)/3 c=(r*0.30)+(g*0.59)+(b*0.11) Local pixcol:Int=(a Shl 24)|(c Shl 16)|(c Shl 8)|(c) WritePixel(pm2,x,y,pixcol) Next Next Return pm2 End Function |
Comments
| ||
Suggestion: Change the line c=(r+g+b)/3 to: c=(r*0.30)+(g*0.59)+(b*0.11) The reason for this is that the human eye has different sensitivities to red, green and blue light wavelengths. your brain gets almost six times as much light and detail from the green light wavelength than it does from the blue wavelength. If you simply average them out equally, then the luminosity of the resulting greyscale image will look all wrong. You'll also likely lose image detail in the process. (This will be especially obvious when you use certain photographic images (like a face) on your icons) For example, a solid green area will appear dark, while in proper greyscale it should be light. for more info, see: http://en.wikipedia.org/wiki/Grayscale |
| ||
Just noticed it looks like the "for x", "for y" loops go out of bounds..shouldn't it loop either from 1-to-width or from 0-to-width minus 1 etc... at least it does in blitz3d/blitzplus....this does not cause an issue on blitzmax perhaps? |
| ||
If you take a second look you will notice that he uses a 'for .. x until y' loop instead of the standard 'for .. x to y' loops. This loop is an extra in Blitzmax and is doing exactly what you have stated, it just loops from 0 to width-1. |
Code Archives Forum