Debian Codegraph @

examples/castlescript/image_make_by_script/image_make_by_script.dpr

raw
1 { Simple program that uses CastleScript to create an image
2 [https://castle-engine.io/caste_script.php].
3 CastleScript allows you to load an existing image, or start from scratch,
4 and perform basic image processing. After the script executed,
5 we will save image to file.
6
7 Run with two parameters: script URL (usually just a filename),
8 and output image URL (usualy just a filename).
9 For example
10 image_make_by_script data/mkimage_gradient.castlescript new_image.png
11
12 Within the script, you have variables:
13 - result, helper_img (type image),
14 - i, j, k, l are ints,
15 - x, y, z, are floats,
16 - v2, v3, v3 are single-precision vectors of 2,3,4 elements.
17
18 You should make a function named "main" in the script, this will be executed.
19
20 See example scripts mkimage_*.castlescript in this directory.
21 }
22
23 uses SysUtils, CastleUtils, CastleFilesUtils, CastleStringUtils, CastleImages,
24 CastleClassUtils, CastleParameters, CastleUriUtils,
25 CastleScript, CastleScriptParser, CastleScriptVectors, CastleScriptImages,
26 CastleLog, CastleApplicationProperties;
27
28 var
29 Vars: TCasScriptValueList;
30 Prog: TCasScriptProgram;
31 begin
32 Parameters.CheckHigh(2);
33
34 ApplicationProperties.OnWarning.Add(@ApplicationProperties.WriteWarningOnConsole);
35
36 Vars := TCasScriptValueList.Create(true);
37 try
38 Vars.Add(TCasScriptImage.Create(true));
39 Vars.Last.Name := 'result';
40 Vars.Add(TCasScriptImage.Create(true));
41 Vars.Last.Name := 'helper_img';
42
43 Vars.Add(TCasScriptInteger.Create(true));
44 Vars.Last.Name := 'i';
45 Vars.Add(TCasScriptInteger.Create(true));
46 Vars.Last.Name := 'j';
47 Vars.Add(TCasScriptInteger.Create(true));
48 Vars.Last.Name := 'k';
49 Vars.Add(TCasScriptInteger.Create(true));
50 Vars.Last.Name := 'l';
51
52 Vars.Add(TCasScriptFloat.Create(true));
53 Vars.Last.Name := 'x';
54 Vars.Add(TCasScriptFloat.Create(true));
55 Vars.Last.Name := 'y';
56 Vars.Add(TCasScriptFloat.Create(true));
57 Vars.Last.Name := 'z';
58
59 Vars.Add(TCasScriptVec2f.Create(true));
60 Vars.Last.Name := 'v2';
61 Vars.Add(TCasScriptVec3f.Create(true));
62 Vars.Last.Name := 'v3';
63 Vars.Add(TCasScriptVec4f.Create(true));
64 Vars.Last.Name := 'v4';
65
66 Prog := ParseProgram(FileToString(Parameters[1]), Vars);
67 try
68 Prog.Environment.BaseUrl := URICurrentPath;
69 Prog.ExecuteFunction('main', []);
70 SaveImage(TCasScriptImage(Vars[0]).Value, Parameters[2]);
71 finally FreeAndNil(Prog) end;
72 finally FreeAndNil(Vars) end;
73 end.