March 01, 2021, 11:01:20 PM

Author Topic: [bmx] Convert pixmap to grey by degac [ 1+ years ago ]  (Read 529 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Convert pixmap to grey by degac [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Convert pixmap to grey
Author : degac
Posted : 1+ years ago

Description : 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.

Code :
Code: BlitzMax
  1. Rem
  2. From a SOURCE pixmap returns a grey converted one
  4. Usage:
  6. Local grey_pix:tpixmap=MakeGrey(LoadPixmap("colored_pixmap.png"))
  8. End Rem
  10. Function MakeGrey:TPixmap(source:TPixmap=Null)
  11.                 If source=Null Return Null
  12.                 Local r:Int,g:Int,b:Int,argb:Int,a:Int,c:Int
  13.                 Local x:Int,y:Int
  14.                 Local tmp_grey:TImage
  15.                 tmp_grey=CreateImage(source.WIDTH,source.HEIGHT)
  16.                 Local pm2:TPixmap=LockImage(tmp_grey)
  17.                 For y=0 Until source.HEIGHT
  18.                 For x=0 Until source.WIDTH
  19.                         argb=ReadPixel(source,x,y)
  20.                         a=argb Shr 24 & $ff
  21.                         r = (argb Shr 16) & $ff
  22.                         g = (argb Shr 8) & $ff
  23.                         b = argb & $ff
  24.                         'c=(r+g+b)/3
  25.                         c=(r*0.30)+(g*0.59)+(b*0.11)
  26.                         Local pixcol:Int=(a Shl 24)|(c Shl 16)|(c Shl 8)|(c)
  27.                         WritePixel(pm2,x,y,pixcol)
  28.                 Next
  29.                 Next
  30.                 Return pm2     
  31. End Function

Comments :

xlsior(Posted 1+ years ago)

 Suggestion:Change the line
Code: [Select]
Code: [Select]
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: <a href="" target="_blank">[/url]

Matty(Posted 1+ years ago)

 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 least it does in blitz3d/blitzplus....this does not cause an issue on blitzmax perhaps?

klepto2(Posted 1+ years ago)

 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. [/i]


SimplePortal 2.3.6 © 2008-2014, SimplePortal