Recent

Author Topic: SOLVED: Special effects  (Read 2078 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
SOLVED: Special effects
« on: April 22, 2020, 01:41:10 pm »
Good morning coders

Does anybody have any functions of how to apply special effects on images?
Effects like grayscale, sepia, invert colors, brightness, horizontal/vertical flip

Thanks in advance, guys.
« Last Edit: April 25, 2020, 12:02:15 am by AsleyCruz »
Graphic & web designer

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Special effects
« Reply #1 on: April 22, 2020, 02:11:19 pm »
hI1

Everything you asked for is built in the BGRAbitmap:

grayscale, grayscaleInPlace, negative, horizontalFlip, verticalFlip.

Brightness and Sepia I am not shure and now I have not Lazarus at hand.

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Special effects
« Reply #2 on: April 22, 2020, 02:20:04 pm »

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Special effects
« Reply #3 on: April 22, 2020, 03:08:29 pm »
About Brightness you can use the Normalize function to use all possible colors from black to white.

Otherwise you can simply multiply the values of each RGB channels or add/substract a constant.
Conscience is the debugger of the mind

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Special effects
« Reply #4 on: April 22, 2020, 03:53:12 pm »
A good source for those (and lots more) kind of things was Earl F. Glynn's Reference Library, most of which can be found at the Internet Archive's Wayback Machine.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Special effects
« Reply #5 on: April 22, 2020, 06:29:09 pm »
Hi!

Just implemented the missing Sepia.
The function Sepia () create  a copy of the TBGRAbitmap but in Sepia color.


Code: Pascal  [Select][+][-]
  1. function range2byte (float: single) : byte; inline;
  2. begin
  3. if float > 255 then float := 255 else
  4.    if float < 0 then float := 0;
  5.    result := round (float);
  6. end;
  7.  
  8. function toSepia (col : TBGRAPixel) : TBGRAPixel; inline;
  9. begin
  10. result.red :=   range2Byte (0.393*col.red + 0.769*col.green + 0.189*col.blue);
  11. result.green:= range2Byte (0.349*col.red + 0.686*col.green + 0.168*col.blue);
  12. result.blue :=  range2Byte (0.272*col.red + 0.534*col.green + 0.131*col.blue);
  13. result.alpha := col.alpha;
  14. end;
  15.  
  16. function Sepia (Orig: TBGRABitmap) : TBGRABitmap;
  17. var dest : TBGRABitmap;
  18.     i : Integer;
  19.     p,q : PBGRAPixel;
  20.  
  21. begin
  22. dest := TBGRAbitmap.create (Orig.width, Orig.height);
  23. p := Orig.Data;;
  24. q := dest.Data;
  25. for i := 0 to Orig.NbPixels-1 do
  26.    begin
  27.    q^ := toSepia (p^);
  28.    inc(p);
  29.    inc(q);
  30.    end; // i
  31.  dest.InvalidateBitmap;
  32.  result := dest;
  33. end;                      
  34.  
  35.  

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Special effects
« Reply #6 on: April 22, 2020, 07:33:20 pm »
Hi!

As test:

"Marianne" in Sepia.
Long llive the french revolution!

Winni

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
Re: Special effects
« Reply #7 on: April 22, 2020, 07:59:41 pm »
hI1

Everything you asked for is built in the BGRAbitmap:
grayscale, grayscaleInPlace, negative, horizontalFlip, verticalFlip.

Many thanks Winni,
I will check the functions out. I haven't used BGRAbitmap too much.

Great sample code with sepia... Thanks for your time.
Graphic & web designer

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Special effects
« Reply #8 on: April 23, 2020, 12:33:59 am »
 :) yeah revolution! Well these days, not really able to go out though.

Channels are in sRGB colorspace, which has a gamma. If you want to do transformations in linear RGB, first transform the value with GammaExpansionTab and in the end back with GammaCompressionTab.

Using linear RGB will give you less dark areas.

Code: Pascal  [Select][+][-]
  1. function CompressF (float: single) : byte; inline;
  2. begin
  3.   if float > 65535 then float := 65535 else
  4.    if float < 0 then float := 0;
  5.    result := GammaCompressionTab[round (float)];
  6. end;
  7.  
  8. function toSepia (col : TBGRAPixel) : TBGRAPixel; inline;
  9. begin
  10.   with GammaExpansion(col) do
  11.   begin
  12.     result.red :=   CompressF (0.393*red + 0.769*green + 0.189*blue);
  13.     result.green:= CompressF (0.349*red + 0.686*green + 0.168*blue);
  14.     result.blue :=  CompressF (0.272*red + 0.534*green + 0.131*blue);
  15.   end;
  16.   result.alpha := col.alpha;
  17. end;
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Special effects
« Reply #9 on: April 23, 2020, 01:43:12 am »
A good source for those (and lots more) kind of things was Earl F. Glynn's Reference Library, most of which can be found at the Internet Archive's Wayback Machine.

Thanx! I did not know that efg is reorganizing his pages.
A good source since decades!

Winni

 

TinyPortal © 2005-2018