-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSL_Generator.ijm
107 lines (99 loc) · 4.19 KB
/
OSL_Generator.ijm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Copyright 2022 Lorenzo Lunelli
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
// this script converts gwyddion color gradients to Open Shading Language (OSL) scripts
// requires ImageJ 1.52s
// version 0.1 03-08-2020
// version 0.2 12-05-2021
// version 0.3 27-04-2022 - added a check on the input file lines
// version 0.4 31-10-2022 - GUI improved
ver="0.4";
requires("1.52s")
osl_start="//OSL script generated by OSL_Generator version="+ver+"\nshader AFM (\n\tfloat Zshift = 0.0,\n\tfloat Z_factor = 3.0,\n";
osl_end =" output color Color = 0\n )\n {\tint arlength = arraylength(rr);\n\tfloat delta = 0;\n\tfloat dz = 0;\n\tfloat r = 0;\n\tfloat g = 0;\n\tfloat b = 0;\n\tfloat Z_nm = (P[2] / Z_factor) * 100 - Zshift; /* from internal blender coords to nm. Inversion of the formula used in the data importer */\n\tif (Z_nm<=hh[1] ) {\n\t\tr=rr[1];\n\t\tg=gg[1];\n\t\tb=bb[1];\n\t}\n\tfor (int i = 2;i <= arlength-1; ++i) {\n\t\tif (Z_nm>hh[i-1] && Z_nm<=hh[i])\n\t\t { delta = (hh[i]-hh[i-1]);\n\t\t dz = Z_nm-hh[i-1];\n\t\t r = dz * ( rr[i] -rr[i-1] ) / delta + rr[i-1];\n\t\t g = dz * ( gg[i] -gg[i-1] ) / delta + gg[i-1];\n\t\t b = dz * ( bb[i] -bb[i-1] ) / delta + bb[i-1];\n\t\t break;\n\t\t}\n\t}\n\tif (Z_nm>=hh[arlength])\n\t { r = rr[arlength];\n\t g = gg[arlength];\n\t b = bb[arlength];\n\t}\n\ncolor C = color (\"rgb\", r,g,b);\nColor= C;\n}";
userdir=false;
Dialog.create("OSL script generation");
Dialog.addMessage("- Select a color gradient file -",14,"blue");
Dialog.addCheckbox("Use active image directory?", false);
Dialog.addNumber("Set the OSL Z range", 15.00, 1, 4, "nm");
Dialog.show();
userdir=Dialog.getCheckbox();
OSL_range=Dialog.getNumber();
if (getInfo("os.name")!="Windows")
{ gwdir=getDirectory("home")+".gwyddion"+File.separator+"gradients";
File.setDefaultDir(gwdir);
}
if (userdir) {
gwdir=getDir("image");
File.setDefaultDir(gwdir);
}
gradient=File.openAsString("");
oslfile=File.nameWithoutExtension+".converted.osl";
lines=split(gradient,"\n");
if (lines[0]=="Gwyddion resource GwyGradient") {
showMessage("Next choose a directory where to save the converted file");
osldir=getDirectory("Choose a save directory");
z = newArray(lines.length-1);
r = newArray(lines.length-1);
g = newArray(lines.length-1);
b = newArray(lines.length-1);
count=0;
for (i=1; i<lines.length; i++) {
if (startsWith(lines[i], "#")) {
//print ("comment",lines[i]);
}
else {
c = split(lines[i]," +|\t+"); // regexp with two possibilities: space or tab
if (c.length==5) { // if a valid Gwyddion gradient line
for (j=0;j<c.length;j++) {
//print(i,j,c[j]);
z[count]=c[0];
r[count]=c[1];
g[count]=c[2];
b[count]=c[3];
}
count++;
}
}
}
// ---- red -----
red="\tfloat rr["+toString(count)+"] = {";
for (i=0;i<= count-2;i++){
red=red+toString(r[i])+", ";
}
red=red+toString(r[count-1])+"},\n";
// ---- green -----
green="\tfloat gg["+toString(count)+"] = {";
for (i=0;i<= count-2;i++){
green=green+toString(g[i])+", ";
}
green=green+toString(g[count-1])+"},\n";
// ---- blue -----
blue="\tfloat bb["+toString(count)+"] = {";
for (i=0;i<= count-2;i++){
blue=blue+toString(b[i])+", ";
}
blue=blue+toString(b[count-1])+"},\n";
// ---- height -----
hh="\tfloat hh["+toString(count)+"] = {";
for (i=0;i<= count-2;i++){
zz=parseFloat(z[i])*OSL_range;
hh=hh+toString(zz)+", ";
}
zz=parseFloat(z[count-1])*OSL_range;
hh=hh+toString(zz)+"},\n";
osl=osl_start+red+green+blue+hh+osl_end;
//print("=====================");
//print (osl);
File.saveString(osl, osldir+oslfile);
showMessage("", "<html> <font color=blue>OSL script file created.");
} else {
showMessage("ERROR", "<html> <font color=red>This input file is not a color gradient file.");
}