Recent

Author Topic: BGRABitmap tutorial  (Read 351041 times)

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #390 on: May 04, 2013, 06:12:45 pm »
Which version of BGRABitmap are you using?
Conscience is the debugger of the mind

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: BGRABitmap tutorial
« Reply #391 on: May 03, 2015, 02:26:47 pm »
Hi,

is there a simple solution how to change BGRABitmap contrast and brightness like in LazPaint  ( menu section: Colors - brightness / contrast) ?

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #392 on: May 03, 2015, 06:01:59 pm »
You can do the following:
Code: [Select]
procedure ChangeLightness(ABitmap: TBGRABitmap; AFactor, AShift: single);
var n: integer;
  p: PBGRAPixel;
  ec: TExpandedPixel;
  curLightness, newLightness: integer;
begin
  p := ABitmap.Data;
  for n := ABitmap.NbPixels-1 downto 0 do
  begin
    ec := GammaExpansion(p^);
    curLightness:= GetLightness(ec);
    newLightness:= round(curLightness*AFactor+AShift*65535);
    if newLightness > 65535 then newLightness:= 65535;
    ec := SetLightness(ec, newLightness, curLightness);
    p^ := GammaCompression(ec);
    inc(p);
  end;
end;
Conscience is the debugger of the mind

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: BGRABitmap tutorial
« Reply #393 on: May 04, 2015, 12:45:21 am »
Thank you, it looks good, only I can not go into black / dark.

What should be the range of AFactor and  AShift values ?



circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #394 on: May 04, 2015, 01:47:14 am »
With AFactor between 0 and 1, it becomes darker.

Note that if you would like to use a negative value for AShift, then the bounds would be ensured by adding a line:
Code: [Select]
if newLightness < 0 then newLightness := 0;
Conscience is the debugger of the mind

Zittergie

  • Full Member
  • ***
  • Posts: 114
    • XiX Music Player
Re: BGRABitmap tutorial
« Reply #395 on: August 17, 2015, 10:19:16 pm »
Hi,
I am replacing all my images (standard Timage) with bgrabitmaps. Standard Timages give problems on Ubuntu 14.x ARM , wrong or not rendering (giving message: Attempt to draw a drawable with depth 24 to a drawable with depth 32)

I would like to draw on the bitmap to show the anolog VU-meter:

VU-meter:
Code: [Select]
Form1.VuImage.Canvas.Pen.Color:=needle;
Form1.VuImage.Canvas.Pen.Width:=2;
Form1.VuImage.Canvas.Line(94, 94, y, z);

Spectrum Analyzer:
Code: [Select]
if VU_Settings.Active=3 then    // Spectrum Analyser
 begin
   If VU_Settings.Placement=2 then YAxes:= Form1.Panel_VU.Height div 2;
   If VU_Settings.Placement=3 then YAxes:= Form1.Panel_VU.Height-2;
   If VU_Settings.Placement=1 then YAxes:= 2;

   Form1.VuImage.Canvas.Pen.Width:=2;   Form1.VuImage.Canvas.Pen.Color:=clHighlight;
   Form1.VuImage.Canvas.Brush.Style:=bssolid; Form1.VuImage.Canvas.Brush.Color:=clBlack;
   Form1.VuImage.Canvas.Rectangle(0,0,Form1.Panel_VU.Width,Form1.Panel_VU.Height);
   For Counter:=0 to 117 do
   begin
    YValue:=round(MyThread.FFT[counter]*128); YValue2:=YValue;

    Form1.VuImage.Canvas.Pen.Color:=clLime;
    if VU_Settings.Placement=2 then
    begin
      if YValue>38 then YValue:=38;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes+YValue,round(2+counter*2),YAxes-YValue);
      if YValue2>60 then YValue2:=60;
      Form1.VuImage.Canvas.Pen.Color:=clGreen;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes+trunc(YValue2/1.8),2+counter*2,yAxes-trunc(YValue2/1.8));
      If VU_Settings.ShowPeaks then
      begin
        Form1.VuImage.Canvas.Pixels[Counter*2+2,YAxes-YValue]:=clRed;
        Form1.VuImage.Canvas.Pixels[Counter*2+3,YAxes-YValue]:=clRed;
      end;
    end;
   if VU_Settings.Placement=3 then
    begin
      if YValue>41 then YValue:=41;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes,2+counter*2,YAxes-round(YValue*1.8));
      if YValue2>60 then YValue2:=60;
      Form1.VuImage.Canvas.Pen.Color:=clGreen;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes,2+counter*2,yAxes-trunc(YValue2));
      If VU_Settings.ShowPeaks then
      begin
        Form1.VuImage.Canvas.Pixels[Counter*2+2,YAxes-round(YValue*1.8)]:=clRed;
        Form1.VuImage.Canvas.Pixels[Counter*2+3,YAxes-round(YValue*1.8)]:=clRed;
      end;
    end;
    if VU_Settings.Placement=1 then
    begin
      if YValue>40 then YValue:=40;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes,2+counter*2,YAxes+round(YValue*1.8));
      if YValue2>70 then YValue2:=70;
      Form1.VuImage.Canvas.Pen.Color:=clGreen;
      Form1.VuImage.Canvas.Line(2+counter*2,YAxes,2+counter*2,yAxes+trunc(YValue2));
      If VU_Settings.ShowPeaks then
      begin
        Form1.VuImage.Canvas.Pixels[Counter*2+2,YAxes+round(YValue*1.8)]:=clRed;
        Form1.VuImage.Canvas.Pixels[Counter*2+3,YAxes+round(YValue*1.8)]:=clRed;
      end;
    end;
   end;
 end;

I tried to translate it to use bgrabitmaps, but without success
Full source:  https://sourceforge.net/p/xixmusicplayer/svn/HEAD/tree/trunk/vuviewer.pas?format=raw
Be the difference that makes a difference

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: BGRABitmap tutorial
« Reply #396 on: August 18, 2015, 05:43:44 am »
And how far you reached?

var
  Bitmap: TBGRABitmap;

Bitmap.Canvas --> here you have Canvas.
According to Circular is slower than using Bitmap.(DrawingMethodName) directly.

You need to do it line by line IMO. If you want to do it automatically use Bitmap.Canvas.

For example:
- Pen.Color, Pen.Width are not used in this way in BGRABitmap.

You must use DrawLineAntiAlias and specify parameters like Width and Color in the method directly.

To access pixels:
- There is a faster way that's used for example to make filters.

Code: [Select]
{
// all pixels //
var
  i: integer;
  p: PBGRAPixel;
begin
  p := Bitmap.Data;

  for i := Bitmap.NBPixels-1 downto 0 do
  begin
    p^.red := ;
    p^.green := ;
    p^.blue := ;
    p^.alpha := ;
    Inc(p);
  end;

// scan line //
var
  x, y: integer;
  p: PBGRAPixel;
begin
  for y := 0 to Bitmap.Height - 1 do
  begin
    p := Bitmap.Scanline[y];
    for x := 0 to Bitmap.Width - 1 do
    begin
      p^.red := ;
      p^.green := ;
      p^.blue := ;
      p^.alpha := ;
      Inc(p);
    end;
  end;
  Bitmap.InvalidateBitmap;
}

But you can also use Bitmap.Pixels[x,y]:= TBGRAPixel (like TColor with Alpha).

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: BGRABitmap tutorial
« Reply #397 on: August 18, 2015, 07:21:27 am »
Zittergie I think you should make it a component from TBGRAVirtualScreen so its easier to control and for play and pause give it a active or pause property,For painting override RedrawBitmapContent like as I done it WallofCovers and paint what you want from the data that it can get from an Event when needed, so you have beautiful and easy to use component .
For painting as 008 said you should probably use TBGRABitmap functions for lines or gradient.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #398 on: August 18, 2015, 08:54:03 pm »
There is an equivalent for Canvas, it is called CanvasBGRA and provides the same features as Canvas and supports antialiasing.

See : https://www.youtube.com/watch?v=HGYSLgtYx-U
« Last Edit: August 18, 2015, 08:58:10 pm by circular »
Conscience is the debugger of the mind

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: BGRABitmap tutorial
« Reply #399 on: August 19, 2015, 01:37:02 am »
why do you have two canvas ?

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: BGRABitmap tutorial
« Reply #400 on: August 19, 2015, 08:12:53 pm »
Because Canvas property gives access to the canvas of the system. It is the same as TBitmap.Canvas. There is not much control over how it renders things, it often does not support antialiasing and does not provide transparency. Also it is slower to use it because switching from direct pixel access to the system canvas need some intermediate processing.

CanvasBGRA property uses BGRABitmap functions, and thus provides antialiasing and transparency. It can be used along with direct pixel access without any restriction. So it generally better to use CanvasBGRA.

However, sometimes, it is necessary to use system functions, hence Canvas property is still here. Also it was here before and for backwards compatibility, it is still here.
Conscience is the debugger of the mind

scons

  • Full Member
  • ***
  • Posts: 141
Re: BGRABitmap tutorial
« Reply #401 on: August 24, 2016, 12:18:50 pm »
Is there a way to start drawing from the middlepoint of a TpaintBox ? To center a drawing ?

Edit: I've found it.
« Last Edit: August 24, 2016, 08:35:35 pm by scons »
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

 

TinyPortal © 2005-2018