Recent

Author Topic: brightness and contrast of the image  (Read 16864 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: brightness and contrast of the image
« Reply #15 on: July 29, 2016, 09:57:14 pm »
I do not understand. You before you told 128
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #16 on: July 30, 2016, 04:31:28 am »
Sorry, I was wrong. I read the page again and now I found it mentioned both 128 and 255. 128 is for example it provided and 255 is for the acceptable range:
Quote
The value of contrast will be in the range of -255 to +255. Negative values will decrease the amount of contrast and, conversely, positive values will increase the amount of contrast.

Here we have the ‘Lena’ and ‘Mandrill’ images which have had the contrast adjusted by -128 (decreased) and +128 (increased):

So the formula for valore should be:
Code: Pascal  [Select][+][-]
  1. valore := (Value_Perc / 100 * 255)

The allowed range of percentage for users to input is -100% to 100%. 0% means do nothing.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: brightness and contrast of the image
« Reply #17 on: July 30, 2016, 05:56:08 pm »
According to what you tell me, then the code should be so. But it does not work

Code: Pascal  [Select][+][-]
  1. procedure TImageSmart.Contrast(value_perc: single);
  2. var
  3.    valore: single;
  4.    x, y: integer;
  5.    p: PBGRAPixel;
  6.    app: TBGRABitmap;
  7.    factor: single;
  8. begin
  9.      app:=TBGRABitmap.create;
  10.      app.Assign(Self.Picture.Bitmap);
  11.      valore := (Value_Perc / 100 * 255);
  12.      factor := (259 * (valore + 255)) / (255 * (259 - valore));
  13.      for y := 0 to app.Height - 1 do
  14.        begin
  15.          p := app.Scanline[y];
  16.          for x := 0 to app.Width - 1 do
  17.          begin
  18.            p^.red := Trunc( factor * (p^.red -255) + 255 );
  19.            p^.green := Trunc( factor * (p^.green -255) + 255 );
  20.            p^.blue := Trunc( factor * (p^.blue -255) + 255 );
  21.            Inc(p);
  22.          end;
  23.        end;
  24.        app.InvalidateBitmap;
  25.        Self.Picture.Bitmap.Assign(app);
  26.        app.Free;
  27. end;  
  28.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #18 on: July 30, 2016, 05:58:17 pm »
Can you please post the sample images before and after processed with contrast filter?

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #19 on: July 30, 2016, 06:29:26 pm »
I checked your code.
Are you sure, you have followed the tutorial correctly?

As I don't have BGRABitmap installed on my computer, what I can do is eye-checking only on your code. Your code is using 255, while the tutorial told you to use 128 on the p^.values assignments.

I know I told you to change 128 to 255, but I only said it on formula for valore.
« Last Edit: July 30, 2016, 06:32:49 pm by Handoko »

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: brightness and contrast of the image
« Reply #20 on: July 30, 2016, 06:36:17 pm »
Yes, but also with the 128 it does not work.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #21 on: July 30, 2016, 06:37:56 pm »
What was the error message? Or can you please provide the image before and after process with the contrast filter?

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: brightness and contrast of the image
« Reply #22 on: July 30, 2016, 06:38:53 pm »
I provide the image. How do you want? to 128?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #23 on: July 30, 2016, 06:39:20 pm »
All, including the original unprocessed image.

Wait, I guess I found the problem. The tutorial tell you to write a Trunc function. But actually FPC already has Trunc function. So, you should try to write the Trunc function as suggested in the tutorial, but name it differently (for example MyTrunc). And use the new Trunc (MyTrunc) on your contrast filter.
« Last Edit: July 30, 2016, 06:48:47 pm by Handoko »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: brightness and contrast of the image
« Reply #24 on: July 30, 2016, 08:07:13 pm »
You need to use 128 instead of 255. Also to add min/max to make sure the value is between 0 and 255:
Code: Pascal  [Select][+][-]
  1.        p^.red := max(0,min(255,Trunc( factor * (p^.red -128) + 128 )));
  2.        p^.green := max(0,min(255,Trunc( factor * (p^.green -128) + 128 )));
  3.        p^.blue := max(0,min(255,Trunc( factor * (p^.blue -128) + 128 )));  

You may need to add "Uses Math"
Conscience is the debugger of the mind

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: brightness and contrast of the image
« Reply #25 on: July 30, 2016, 08:28:35 pm »
You need to use 128 instead of 255. Also to add min/max to make sure the value is between 0 and 255:
Code: Pascal  [Select][+][-]
  1.        p^.red := max(0,min(255,Trunc( factor * (p^.red -128) + 128 )));
  2.        p^.green := max(0,min(255,Trunc( factor * (p^.green -128) + 128 )));
  3.        p^.blue := max(0,min(255,Trunc( factor * (p^.blue -128) + 128 )));  

You may need to add "Uses Math"

Uhhhh, these are bytes.
On i386 you can use {$SATURATION ON} to use mmx and clamp instead of overflow.and do all calculations in integers with scaling. (shl, calculate, shl) and 255) Much faster.
« Last Edit: July 30, 2016, 08:49:19 pm by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #26 on: July 30, 2016, 08:29:33 pm »
The Trunc function in the code (xinyiman) is used to keep the value in range of 0 to 255. In the tutorial, it told us to write a Trunc function. But now I realized, FPC already has Trunc function which works different with the one in the tutorial. So, I think this is the problem that makes xinyiman's code fails to work correctly.

Edited:
sorry, the tutorial told us to write a function named Truncate (not Trunc).
« Last Edit: July 30, 2016, 09:19:22 pm by Handoko »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: brightness and contrast of the image
« Reply #27 on: July 30, 2016, 08:39:57 pm »
@Thaddy: Apart from {$SATURATION ON}, not clamping the values will either lead to an "out of bounds" runtime error or that the value will cycle, and that's not what we want here. Writing "and 255" will cycle.
Conscience is the debugger of the mind

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: brightness and contrast of the image
« Reply #28 on: July 30, 2016, 09:23:14 pm »
I think it is clear now, if we inspect the Truncate function suggested by the tutorial:
http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-4-brightness-adjustment/

And now have a look on the xinyiman's code, which he/she mistyped it to Trunc (which is a function provided by FPC):
Code: Pascal  [Select][+][-]
  1.          begin
  2.            p^.red := Trunc( factor * (p^.red -255) + 255 );
  3.            p^.green := Trunc( factor * (p^.green -255) + 255 );
  4.            p^.blue := Trunc( factor * (p^.blue -255) + 255 );
  5.            Inc(p);
  6.          end;

FPC Trunc function is used to convert Real to Integer:
http://www.freepascal.org/docs-html/rtl/system/trunc.html

Case closed.
« Last Edit: July 30, 2016, 09:26:36 pm by Handoko »

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: brightness and contrast of the image
« Reply #29 on: July 31, 2016, 10:12:22 am »
Is in fact the problem was the trunc which I confused with the system function.
The code that works I:

Code: Pascal  [Select][+][-]
  1. function TImageSmart.MyTruncate(value: single): byte;
  2. var
  3.   ret: byte;
  4. begin
  5.      ret := Round(value);
  6.  
  7.      If (value < 0) Then
  8.         ret := 0;
  9.  
  10.      If (value > 255) Then
  11.         ret := 255;
  12.  
  13.      result:= ret;
  14. end;  
  15.  
  16. procedure TImageSmart.Contrast(value_perc: single);
  17. var
  18.    valore: single;
  19.    x, y: integer;
  20.    p: PBGRAPixel;
  21.    app: TBGRABitmap;
  22.    factor: single;
  23. begin
  24.      app:=TBGRABitmap.create;
  25.      app.Assign(Self.Picture.Bitmap);
  26.      valore := (Value_Perc / 100 * 255);
  27.      factor := (259 * (valore + 255)) / (255 * (259 - valore));
  28.      for y := 0 to app.Height - 1 do
  29.        begin
  30.          p := app.Scanline[y];
  31.          for x := 0 to app.Width - 1 do
  32.          begin
  33.            p^.red := MyTruncate( factor * (p^.red -255) + 255 );
  34.            p^.green := MyTruncate( factor * (p^.green -255) + 255 );
  35.            p^.blue := MyTruncate( factor * (p^.blue -255) + 255 );
  36.            Inc(p);
  37.          end;
  38.        end;
  39.        app.InvalidateBitmap;
  40.        Self.Picture.Bitmap.Assign(app);
  41.        app.Free;
  42. end;  
  43.  

But if you know how to make it better accept tips.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018