Recent

Author Topic: Pascal Script and BGRABitmap  (Read 10254 times)

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Pascal Script and BGRABitmap
« on: October 12, 2014, 12:04:00 am »
According to this https://github.com/remobjects/pascalscript/wiki/Using-Classes-with-RemObjects-PascalScript seems easy to add custom classes to Pascal Script. In theory... in the practice using the import tool http://forum.lazarus.freepascal.org/index.php/topic,26104.msg159749.html#msg159749 I get a lot of error messages.

For example

Code: [Select]
  Int32or64 = {$IFDEF CPU64}Int64{$ELSE}LongInt{$ENDIF};
  UInt32or64 = {$IFDEF CPU64}UInt64{$ELSE}LongWord{$ENDIF};

I need to change to

Code: [Select]
  Int32or64 = NativeInt;
  UInt32or64 = NativeUInt;

in order to continue. Easy. Then I get error on this:

Code: [Select]
  //pixel structure
  TBGRAPixel = packed record
    blue, green, red, alpha: byte;
  end;

And I need to change to this (that is not the same, I need to remove 'packed'):

(PD: I searched 'packed record' in the remobjects and nothing is shown... http://talk.remobjects.com/category/pascal-script)

Code: [Select]
  //pixel structure
  TBGRAPixel = record
    blue: byte;
    green: byte;
    red: byte;
    alpha: byte;
  end;

The pointer is not supported, I comment this:

Code: [Select]
  //PBGRAPixel = ^TBGRAPixel;
And so on...

I think is possible to create the plugin but not completely. There are problems also with constants, variables, and maybe more because I stoped  :o

Well, that was my experience. Pascal Script is really powerfull, I'm using it and I love it. But maybe takes a lot of time to add BGRABitmap to Pascal Script.
« Last Edit: October 13, 2014, 01:28:21 am by 007 »

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Scripd and BGRABitmap
« Reply #1 on: October 12, 2014, 06:35:04 pm »
I see. That's interesting. Maybe it would possible to add a wrapper. This could also add some protection, because a script might write anywhere in memory. Well if pointer is not supported, in principle, it narrows such possibilities. But there might be some low level functions that could be improperly used.

TBGRAPixel without packed does not make much sense. The bytes need to be one after another. Using an array of byte might help, because apparently, those are not padded.
« Last Edit: October 12, 2014, 06:39:04 pm by circular »
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Scripd and BGRABitmap
« Reply #2 on: October 13, 2014, 12:13:31 am »
Something like TBGRAScriptBitmap with just the tested functions..

The available functions are those you declare to be available.

Or do the thing I've seen on uos, a lot of functions that manage the memory.

Instead of
Code: [Select]
Bitmap := TBGRABitmap.Create; // or TBGRAScriptBitmap.Create;
Bitmap.Draw(...);
Bitmap.Free;

do something like
Code: [Select]
bgra_Create(0);
bgra_Draw(0,...);
bgra_Free(0);

all the bitmaps stored in an internal array:
Code: [Select]
BitmapsArray[0] := TBGRABitmap.Create;
so bgra_Free will really do something like

Code: [Select]
procedure bgra_Free(id: integer);
begin
  BitmapsArray[id].Free;
end;

and in procedures that need more than a bitmap

Code: [Select]
procedure something(id1, id2: integer);
BitmapArray[id1]..
BitmapArray[id2]..
...

and has the advantage that it can be used inside any compiled program and in the script. Registering procedures and functions in pascal script is easier than registering classes.

Is something like a pointer and a copy of the declaration in a string:
Code: [Select]
procedure TForm1.PSScript1Compile(Sender: TPSScript);
begin
  Sender.AddMethod(Self, @ShowMessage, 'procedure ShowMessage(const aMsg: string);');
  Sender.Comp.AddTypeS('MyPixel','Record blue, green, red: byte; end;');
end;

BTW I've able to to register simple classes using the import tool.
« Last Edit: October 13, 2014, 01:40:26 am by 007 »

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Script and BGRABitmap
« Reply #3 on: October 13, 2014, 05:27:52 am »
Hi circular,

I've added BGRAPascalScript and uPSI_BGRAPascalScript in BGRA-Controls (it installs an IDE plugin, so rebuild Lazarus).

It just has the most basic Create, CreateFromFile and Destroy, it comes with a test application.

Notice that identifiers must be unique.

You have SVN acces to BGRA-Controls so try adding something more!

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Script and BGRABitmap
« Reply #4 on: October 13, 2014, 02:37:47 pm »
Alright. So I have added basic support for colors.

Colors can be defined rgb() and rgba() functions, are as an ARGB constant like $ff00ffff (opaque cyan).

So the following script:
Code: [Select]
Program Test_BGRAPascalScript;

var c: TBGRAColor;
 i: integer;
begin
  bgra_CreateWithSize(0, 256,1);
  c := rgb(255,255,0); //yellow
  for i := 0 to 255 do
  begin
    bgra_SetPixel(0, i,0, c);
    c := setGreen(c, getGreen(c)-1);
  end;
end.

Would render as:
« Last Edit: October 13, 2014, 02:39:25 pm by circular »
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Script and BGRABitmap
« Reply #5 on: October 13, 2014, 03:20:45 pm »
Ok.  :)

Scripting is working!!

PD: Also I thinked this unit can be usefull for creating a library (.dll) for BGRABitmap that can be used in any language!

Edit: I've added the library project that creates bgrabitmap.dll and a test project for this, seems that is working.
« Last Edit: October 13, 2014, 08:31:07 pm by 007 »

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Script and BGRABitmap
« Reply #6 on: October 14, 2014, 12:34:19 am »
Oh ok so it could be used with another language, right?
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Script and BGRABitmap
« Reply #7 on: October 14, 2014, 01:24:24 am »
Yes with any language that support dll. Just is neccesary to make a header (define the functions and types) in that language. (http://www.freepascal.org/docs-html/prog/progse55.html)

And ensure this:

Quote
If you want your library to be called from programs compiled with other compilers, it is important to specify the correct calling convention for the exported functions. Since the generated programs by other compilers do not know about the Free Pascal calling conventions, your functions would be called incorrectly, resulting in a corrupted stack.

On Windows, most libraries use the stdcall convention, so it may be better to use that one if your library is to be used on Windows systems. On most unix systems, the C calling convention is used, therefore the cdecl modifier should be used in that case.

For that I've added stdcall, since I'm using Windows.

Well it works with Lazarus, I've tested it. It's statically loaded in the test I uploaded.
(http://wiki.lazarus.freepascal.org/Lazarus/FPC_Libraries#External_-_statically_loading_a_dynamic_library)

But I've tested with another language and it doesn't works, because:

That language loads the dll, call the function, sucess, and then free the dll. So the memory is freed and there's no chance to keep calling functions since in this library the functions are dependant to the memory.

It must be loaded, call all the functions you want and then free the library. In Lazarus this is possible with LoadLibrary and UnloadLibrary.
(http://wiki.lazarus.freepascal.org/Lazarus/FPC_Libraries#Loadlibrary_-_dynamically_loading_a_dynamic_library)
« Last Edit: October 14, 2014, 03:49:54 am by 007 »

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Script and BGRABitmap
« Reply #8 on: October 14, 2014, 11:30:51 pm »
I see.  %)
Conscience is the debugger of the mind

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Pascal Script and BGRABitmap
« Reply #9 on: October 15, 2014, 12:31:37 am »
Quote
Also I thinked this unit can be usefull for creating a library (.dll) for BGRABitmap that can be used in any language!

Yep and you may create Java-compatible libraries also (Java cannot deal with "classical" native libraries but fpc can create Java-native libraries !)  ;)

http://wiki.lazarus.freepascal.org/Using_Pascal_Libraries_with_Java

Fred
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Script and BGRABitmap
« Reply #10 on: October 15, 2014, 03:54:27 am »
Quote
Also I thinked this unit can be usefull for creating a library (.dll) for BGRABitmap that can be used in any language!

Yep and you may create Java-compatible libraries also (Java cannot deal with "classical" native libraries but fpc can create Java-native libraries !)  ;)

http://wiki.lazarus.freepascal.org/Using_Pascal_Libraries_with_Java

Fred

Nice. I'm learning hava, I'll take a look.

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Script and BGRABitmap
« Reply #11 on: November 28, 2014, 09:35:43 pm »
Hi folks

I have started to use La-pe scripting language, which is like Pascal Script. However it allows packed records and overloading of functions, so I was able to supply functions that are similar that those in Free Pascal.

Test project is here: https://sourceforge.net/p/lazpaint/code/HEAD/tree/bgralape/
It needs to have a path to lape files.

Lape can be downloaded with git:
git clone https://code.google.com/p/la-pe/

Here is a source code example:
Code: [Select]
var w,h: integer;

  procedure PixelProc(x,y: Int32; var pix: TBGRAPixel);
  var hsla: THSLAPixel;
  begin
    hsla.hue := (x shl 16) div w;
    hsla.saturation := ((y shl 17) div h) and 65535;
    hsla.alpha := 65535;
    if y > h shr 1 then
    begin
      hsla.lightness := 48000;
      pix := GSBAToBGRA(hsla);
    end
    else
    begin
      hsla.lightness := 32768;
      pix := HSLAToBGRA(hsla);
    end;
  end;

begin
  w := BitmapWidth;
  h := BitmapHeight; 
  ForEachPixel(@PixelProc);
end;
Conscience is the debugger of the mind

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Pascal Script and BGRABitmap
« Reply #12 on: November 29, 2014, 06:33:49 am »
interesting,what about speed,ease of use, and real time use. are you able to recover from a bad script or doe's it kill the program.

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Pascal Script and BGRABitmap
« Reply #13 on: November 29, 2014, 11:11:25 am »
It is said to be faster than Pascal Script, however I have not checked that.

It is easy enough to use. You just need to get the grip of the syntax for imported functions. Basically the parameters are transmitted as an array of pointers to the values of the parameters.

The script is first compiled, so if it does not compile, it won't run at all. Then there are exception handling. I have not tested that yet either.
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Pascal Script and BGRABitmap
« Reply #14 on: December 13, 2014, 05:37:59 am »
Nice.

 

TinyPortal © 2005-2018