Forum > Graphics

[Solved] fast code to lower an image brightness

(1/1)

the_magik_mushroom:
hey guys,
I'm trying to port this code from delphi to lazarus but it's not working :


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function IntToByte(i:Integer):Byte;begin  if      i>255 then Result:=255  else if i<0   then Result:=0  else               Result:=i;end; procedure Darkness(var src: TBitmap; Amount: integer);var  p0: PByte;  r, g, b, x, y: Integer;begin  src.PixelFormat := pf24bit;  for y := 0 to src.Height - 1 do  begin    p0 := src.ScanLine[y];    for x := 0 to src.Width - 1 do    begin      r := p0[x * 3];      g := p0[x * 3 + 1];      b := p0[x * 3 + 2];      p0[x * 3] := IntToByte(r - ((r * Amount) div 255));      p0[x * 3 + 1] := IntToByte(g - ((g * Amount) div 255));      p0[x * 3 + 2] := IntToByte(b - ((b * Amount) div 255));    end;  end;end;
what I want is to reduce the brightness of of bitmap, the code must be fast.
nothing happen when I try to use it on a TBitmap: Darkness(bmp,75);
any idea why?

thanks

Blaazen:
Hi,

I tested your code and it writes me: Warning: Symbol "Scanline" is not portable
So I jumped to source (Graphics.pp) via Alt+ArrowUp and there is a comment by the Scanline property: // Use only when wrpped by a begin/endupdate

So I added it to your code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  src.BeginUpdate(False);  //all the code of the Darkness  src.EndUpdate(True);and it works.

When it comes to speed, this kind of graphics will always be rather slow.
Anyway div is a slow operation, you can try bitshift instead (but you will have not fine control).
You can also ommit conversion IntToByte, because it will never go over 255 and beyond 0.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---p0[x * 3] := (r - (r shr 3));

balazsszekely:
If speed is an issue, you can always switch to BGRABitmap:
1. Install BGRABitamp from OPM
2. Download/build/run attached project

Jorg3000:
Not testet!
Avoid all the x*3
Replace div 255 by shr 8 (= div 256)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---r := p0^;  // p0[0]g := p0[1];b := p0[2];p0^ := r - ((r * Amount) shr 8);p0[1] := g - ((g * Amount) shr 8);p0[2] := b - ((b * Amount) shr 8);inc(p0,3);  // next 3 bytes 

the_magik_mushroom:
thanks for all the answer  :-* :-* :-*

Navigation

[0] Message Index

Go to full version