Different code between GMIC-QT and GMIC CLI

Windows tests oblige me to create 2 variants of scripts for the same result.
The problem is about creating an SVG file.
For example, I have to use CRLF="\\r\\n" for GMIC-QT and CRLF="\r\n" for GMIC CLI.
Is there a variable saying if the script runs under GMIC-QT or GMIC CLI ?

I join the 2 variations of the same script (QT and CLI).



fx_gmicky 0
CRLF="\\r\\n"
Liste="<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"$CRLF
Liste=$Liste"<svg xmlns=\\"http://www.w3.org/2000/svg\\" version=\\"1.1\\" width=\\"1024\\" height=\\"1024\\">"$CRLF
Liste=$Liste"<title>Test GMIC SVG</title>"$CRLF
Liste=$Liste"<desc>Points et Segments</desc>"$CRLF
if {w}>{h}
resize 1000,{{h}/{w}*1000}
else
resize {{w}/{h}*1000},1000
fi
samj_Masques_Noir_Et_Blanc_Preview 1.25,42
to_gray
negate
dilate 10
pointcloud. 3
channels. 0,1
resize. 1%,1
add. 12
tsp. 200
+crop 0,0,1,0
Xend={i0}
Yend={i1}
rm[-1]
repeat {{w}-1}
Index=$>
 +crop $Index,0,{$Index+1},0
 Xa={i0}
 Ya={i1}
 rm[-1]
 +crop {$Index+1},0,{$Index+2},0
 Xb={i0}
 Yb={i1}
 rm[-1]
 Liste=$Liste"<line x1=\\""$Xa
 Liste=$Liste"\\" y1=\\""$Ya
 Liste=$Liste"\\" x2=\\""$Xb
 Liste=$Liste"\\" y2=\\""$Yb
 Liste=$Liste"\\" stroke=\\"black\\" />"$CRLF
done
Liste=$Liste"<line x1=\\""$Xb
Liste=$Liste"\\" y1=\\""$Yb
Liste=$Liste"\\" x2=\\""$Xend
Liste=$Liste"\\" y2=\\""$Yend
Liste=$Liste"\\" stroke=\\"black\\" />"$CRLF
Liste=$Liste"</svg>"$CRLF
('$Liste')
output_text.  $_path_rc/Liste_Points_TSP.svg
# SVG validé sur https://validator.w3.org/
rm.
1024,1024,1,1
repeat {0,w}
 line. {0,boundary=2;[I[$>],I[$>+1]]},1,255
done
rm[-2]


fx_gmicky 0
CRLF="\r\n"
Liste="<?xml version=\"1.0\" encoding=\"utf-8\"?>"$CRLF
Liste=$Liste"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1024\" height=\"1024\">"$CRLF
Liste=$Liste"<title>Test GMIC SVG</title>"$CRLF
Liste=$Liste"<desc>Points et Segments</desc>"$CRLF
if {w}>{h}
resize 1000,{{h}/{w}*1000}
else
resize {{w}/{h}*1000},1000
fi
samj_Masques_Noir_Et_Blanc_Preview 1.25,42
to_gray
negate
dilate 10
pointcloud. 3
channels. 0,1
resize. 1%,1
add. 12
tsp. 200
+crop 0,0,1,0
Xend={i0}
Yend={i1}
rm[-1]
repeat {{w}-1}
Index=$>
 +crop $Index,0,{$Index+1},0
 Xa={i0}
 Ya={i1}
 rm[-1]
 +crop {$Index+1},0,{$Index+2},0
 Xb={i0}
 Yb={i1}
 rm[-1]
 Liste=$Liste"<line x1=\""$Xa
 Liste=$Liste"\" y1=\""$Ya
 Liste=$Liste"\" x2=\""$Xb
 Liste=$Liste"\" y2=\""$Yb
 Liste=$Liste"\" stroke=\"black\" />"$CRLF
done
Liste=$Liste"<line x1=\""$Xb
Liste=$Liste"\" y1=\""$Yb
Liste=$Liste"\" x2=\""$Xend
Liste=$Liste"\" y2=\""$Yend
Liste=$Liste"\" stroke=\"black\" />"$CRLF
Liste=$Liste"</svg>"$CRLF
('$Liste')
output_text.  $_path_rc/Liste_Points_TSP.svg
# SVG validé sur https://validator.w3.org/
rm.
1024,1024,1,1
repeat {0,w}
 line. {0,boundary=2;[I[$>],I[$>+1]]},1,255
done
rm[-2]
````Preformatted text`

Salut, @samj!

I am not sure that it is applicable
in your cases — but could you use
the old trick 0x0D0A?

Have fun!
Claes in Lund, La Suède

@Claes

Thank you for the answer.
Unfortunately 0x0D0A does not work.

I tried CRLF=\013\010 that works only with GMIC-QT.

:o)

Look what I just found:

$_host: A string telling about the host running the G’MIC interpreter (e.g. cli or gimp).

$_os: A string describing the running operating system.

also

is_windows

1 Like

@Claes

Thank you for this search (I do not know $_host) and in all naivety I thought GMIC scripts worked on QT and CLI.

I’m going to use $_host because the difference is the same with Linux between GMIC-QT and GMIC CLI.

I will be able to change the script and if it works with replace_str I will give the script.

:o)


Here are some results with these command lines

text[-1] $_host,20,20,20,1,255,0,0
text[-1] $_os,20,60,20,1,255,0,0

Gimp Windows :
gimp
windows

GMIC-QT Windows :
standalone
windows

CLI windows :
cli
windows

Krita Windows :
krita-plugin
windows

2 Likes

@samj It has been a fairly recent addition on my and others’ prompting. Even though I try to keep track, G’MIC (David) has a speeding train in terms of development.

Yes. I have not been able to get around AI things yet.

They are too slow on my handcar of a laptop.

image

Great! The solution proposed by @Claes works on QT and CLI :grinning:

Thanks to @Reptorian for his advice.

:o)

Script:



fx_gmicky 0
if '$_host'=='cli'
 CRLF="\r\n"
 Liste="<?xml version=\"1.0\" encoding=\"utf-8\"?>"$CRLF
 Liste.="<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1024\" height=\"1024\">"$CRLF
else
 CRLF="\\r\\n"
 Liste="<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"$CRLF
 Liste.="<svg xmlns=\\"http://www.w3.org/2000/svg\\" version=\\"1.1\\" width=\\"1024\\" height=\\"1024\\">"$CRLF
fi
Liste.="<title>Test GMIC SVG</title>"$CRLF
Liste.="<desc>Points et Segments</desc>"$CRLF
if {w}>{h}
resize 1000,{{h}/{w}*1000}
else
resize {{w}/{h}*1000},1000
fi
samj_Masques_Noir_Et_Blanc_Preview 1.25,42
to_gray
negate
dilate 10
pointcloud. 3
channels. 0,1
resize. 1%,1
add. 12
tsp. 200
+crop 0,0,1,0
Xend={i0}
Yend={i1}
rm[-1]
repeat {{w}-1}
Index=$>
 +crop $Index,0,{$Index+1},0
 Xa={i0}
 Ya={i1}
 rm[-1]
 +crop {$Index+1},0,{$Index+2},0
 Xb={i0}
 Yb={i1}
 rm[-1]
 if '$_host'=='cli'
  Liste.="<line x1=\""$Xa
  Liste.="\" y1=\""$Ya
  Liste.="\" x2=\""$Xb
  Liste.="\" y2=\""$Yb
  Liste.="\" stroke=\"black\" />"$CRLF
 else
  Liste.="<line x1=\\""$Xa
  Liste.="\\" y1=\\""$Ya
  Liste.="\\" x2=\\""$Xb
  Liste.="\\" y2=\\""$Yb
  Liste.="\\" stroke=\\"black\\" />"$CRLF
 fi
done
if '$_host'=='cli'
 Liste.="<line x1=\""$Xb
 Liste.="\" y1=\""$Yb
 Liste.="\" x2=\""$Xend
 Liste.="\" y2=\""$Yend
 Liste.="\" stroke=\"black\" />"$CRLF
else
 Liste.="<line x1=\\""$Xb
 Liste.="\\" y1=\\""$Yb
 Liste.="\\" x2=\\""$Xend
 Liste.="\\" y2=\\""$Yend
 Liste.="\\" stroke=\\"black\\" />"$CRLF
fi
Liste.="</svg>"$CRLF
('$Liste')
output_text.  $_path_rc/Liste_Points_TSP.svg
# SVG validé sur https://validator.w3.org/
rm.
1024,1024,1,1
repeat {0,w}
 line. {0,boundary=2;[I[$>],I[$>+1]]},1,255
done
rm[-2]
2 Likes

Here’s a slight more cleaner code.

fx_gmicky 0
if '$_host'=='cli'
 CRLF="\r\n"
 Liste="<?xml version=\"1.0\" encoding=\"utf-8\"?>"$CRLF
 Liste.="<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1024\" height=\"1024\">"$CRLF
else
 CRLF="\\r\\n"
 Liste="<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"$CRLF
 Liste.="<svg xmlns=\\"http://www.w3.org/2000/svg\\" version=\\"1.1\\" width=\\"1024\\" height=\\"1024\\">"$CRLF
fi
Liste.="<title>Test GMIC SVG</title>"$CRLF
Liste.="<desc>Points et Segments</desc>"$CRLF
if w>h
resize 1000,{h/w*1000}
else
resize {w/h*1000},1000
fi
samj_Masques_Noir_Et_Blanc_Preview 1.25,42
to_gray
negate
dilate 10
pointcloud. 3
channels. 0,1
resize. 1%,1
add. 12
tsp. 200
+crop 0,0,1,0
Xend={i0}
Yend={i1}
rm[-1]
repeat w-1
Index=$>
 +crop $Index,0,{$Index+1},0
 Xa={i0}
 Ya={i1}
 rm[-1]
 +crop {$Index+1},0,{$Index+2},0
 Xb={i0}
 Yb={i1}
 rm[-1]
 if '$_host'=='cli'
  Liste.="<line x1=\""$Xa
  Liste.="\" y1=\""$Ya
  Liste.="\" x2=\""$Xb
  Liste.="\" y2=\""$Yb
  Liste.="\" stroke=\"black\" />"$CRLF
 else
  Liste.="<line x1=\\""$Xa
  Liste.="\\" y1=\\""$Ya
  Liste.="\\" x2=\\""$Xb
  Liste.="\\" y2=\\""$Yb
  Liste.="\\" stroke=\\"black\\" />"$CRLF
 fi
done
if '$_host'=='cli'
 Liste.="<line x1=\""$Xb
 Liste.="\" y1=\""$Yb
 Liste.="\" x2=\""$Xend
 Liste.="\" y2=\""$Yend
 Liste.="\" stroke=\"black\" />"$CRLF
else
 Liste.="<line x1=\\""$Xb
 Liste.="\\" y1=\\""$Yb
 Liste.="\\" x2=\\""$Xend
 Liste.="\\" y2=\\""$Yend
 Liste.="\\" stroke=\\"black\\" />"$CRLF
fi
Liste.="</svg>"$CRLF
('$Liste')
output_text.  $_path_rc/Liste_Points_TSP.svg
# SVG validé sur https://validator.w3.org/
rm.
1024,1024,1,1
repeat {0,w}
 line. {0,boundary=2;[I[$>],I[$>+1]]},1,255
done
rm[-2]
1 Like

@Reptorian

Wouldn’t it be a good idea also to do something like this…?

if '$_host'=='cli'
 ESC="\"
else
 ESC="\\"
fi

to be used as

 Liste.=$ESC y1=$ESC $Ya

et cetera?

Have fun!
Claes in Lund, Sweden

For what it is worth, recall that SVG is a flavor of XML. So long as it is well formed, an SVG document can be entirely on one line. Carriage returns and line feeds are a readability nicety, not a parsing necessity.

Have an ampersand:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><svg id="svg2" version="1.1" width="256" height="256" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"><defs id="defs6" /><path id="goudyampersand" style="fill:#fd7c00;fill-opacity:1;stroke:none" d="m 41.359769,107.36499 c -2.7e-5,-0.529 -0.970044,-1.93992 -2.910056,-4.2328 -1.940053,-2.46902 -3.880086,-5.908168 -5.820105,-10.317462 -1.763684,-4.409037 -2.645515,-9.259119 -2.645502,-14.550259 -1.3e-5,-12.169154 4.761884,-22.927518 14.285712,-32.275126 9.700136,-9.523625 20.811239,-14.285522 33.333327,-14.285712 3.880005,1.9e-4 7.848255,0.441092 11.904764,1.322748 4.05636,0.882021 7.84824,2.645691 11.375651,5.291004 3.70362,2.645685 5.55547,5.99665 5.55556,10.052908 -9e-5,3.52751 -1.23466,6.437559 -3.7037,8.730161 -2.46922,2.292926 -5.291086,3.439312 -8.465607,3.439149 -3.703778,1.63e-4 -7.583843,-2.204423 -11.64021,-6.613752 -3.880133,-4.585365 -7.848383,-6.878136 -11.904763,-6.878305 -6.525622,1.69e-4 -12.962998,2.998402 -19.312163,8.994707 -6.349239,5.820261 -9.523835,13.22766 -9.523808,22.222218 -2.7e-5,5.29114 1.234536,10.405776 3.703703,15.343913 2.645469,4.762026 4.938233,7.142978 6.878306,7.142848 0.352711,1.3e-4 1.939992,-0.52899 4.761904,-1.587297 2.821824,-1.234441 6.52552,-2.469011 11.111109,-3.703703 4.585481,-1.234441 9.259193,-1.851719 14.021158,-1.851848 6.701869,1.29e-4 12.257414,1.411061 16.666668,4.232804 4.585443,2.645624 6.878213,6.261134 6.878303,10.846554 -9e-5,4.58564 -2.38104,8.28935 -7.142857,11.11111 -4.761979,2.82197 -10.670261,4.23291 -17.724861,4.2328 l -0.264534,0 c -4.409226,1.1e-4 -8.64203,-0.529 -12.69841,-1.5873 -3.88012,-1.0581 -6.966531,-2.1163 -9.259254,-3.1746 -2.292812,-1.0581 -3.61556,-1.58719 -3.968257,-1.5873 -0.529134,1.1e-4 -1.058235,0.17649 -1.587301,0.52913 -0.352779,0.35285 -0.705504,0.70557 -1.058201,1.0582 -0.176424,0.35285 -0.440957,0.70557 -0.793648,1.0582 l -0.264533,0.52914 c -4.761931,8.64206 -7.142879,18.07768 -7.142852,28.30688 -2.7e-5,6.34926 1.146351,12.61028 3.439149,18.78306 2.292737,5.99652 5.820071,11.99299 10.582009,17.98941 4.938225,5.82014 11.992893,10.67022 21.164017,14.55027 9.347371,3.88008 20.19392,6.08466 32.539683,6.61375 l 4.7619,0 c 10.40553,1e-5 19.48841,-1.4991 27.24867,-4.49735 7.76001,-2.99821 13.66829,-6.96646 17.72487,-11.90476 4.05628,-4.93824 6.96633,-9.96469 8.73015,-15.07936 1.93989,-5.11459 2.9099,-10.22923 2.91006,-15.34392 -1.6e-4,-9.34737 -2.55748,-17.1075 -7.67196,-23.28041 -5.11478,-6.17275 -11.9049,-9.25917 -20.37036,-9.25927 -7.76026,10e-5 -14.63856,2.6456 -20.63492,7.93651 -5.82021,5.29109 -8.73026,11.28755 -8.73016,17.98942 -1e-4,4.5856 1.41083,8.55385 4.2328,11.90476 2.99813,3.17465 6.79001,4.76195 11.37566,4.7619 1.93992,5e-5 4.05632,-0.44089 6.34921,-1.32275 2.29264,-0.88178 3.43903,-1.93998 3.43915,-3.1746 -1.2e-4,-0.70541 -0.35285,-1.23451 -1.0582,-1.58731 -0.70559,-0.35271 -1.58743,-0.61724 -2.6455,-0.79364 -0.88196,-0.17629 -1.94016,-0.61725 -3.17461,-1.32276 -1.23468,-0.70541 -2.29288,-1.67542 -3.1746,-2.91005 -1.23468,-1.76361 -1.85196,-3.70364 -1.85185,-5.8201 -1.1e-4,-2.64544 0.9699,-4.9382 2.91005,-6.87831 2.11629,-1.93995 4.67361,-2.90997 7.67196,-2.91005 4.05631,8e-5 7.67183,1.4992 10.84656,4.49735 3.35083,2.99831 5.02631,6.70201 5.02645,11.11111 -1.4e-4,5.46743 -2.2929,10.31751 -6.8783,14.55026 -4.58567,4.05648 -10.14122,6.0847 -16.66667,6.08466 -7.23114,4e-5 -13.40398,-2.64546 -18.51851,-7.93651 -5.11473,-5.29095 -7.67205,-11.72833 -7.67196,-19.31216 -9e-5,-7.0546 2.29268,-13.75653 6.87831,-20.10582 4.7618,-6.52547 11.19918,-11.81648 19.31216,-15.87301 8.28912,-4.05633 20.10568,-7.31911 35.44973,-9.78836 15.52011,-2.46902 26.01393,-4.93815 31.48148,-7.407405 6.52538,-2.82175 11.99274,-6.966362 16.40211,-12.433864 4.58534,-5.643594 6.8781,-12.257346 6.87831,-19.841263 -2.1e-4,-8.289082 -2.64571,-15.167381 -7.93651,-20.634917 -5.11483,-5.467191 -11.02311,-8.200877 -17.72487,-8.20106 -5.46754,1.83e-4 -8.20122,0.793837 -8.20105,2.380955 l 2.1164,2.910049 c 1.58713,1.940209 2.38078,4.144795 2.38095,6.613759 -1.7e-4,3.174772 -1.05838,5.820267 -3.1746,7.936506 -2.11657,2.116558 -4.76207,3.174759 -7.93651,3.174603 -3.52749,1.56e-4 -6.43754,-1.234407 -8.73015,-3.703703 -2.29293,-2.64534 -3.43931,-5.819935 -3.43916,-9.523808 -1.5e-4,-5.643559 2.3808,-10.405463 7.14286,-14.285712 4.76174,-3.879882 10.31729,-5.819915 16.66666,-5.820104 10.22909,1.89e-4 19.40016,4.32117 27.51322,12.962957 8.28903,8.642145 12.43365,19.40051 12.43387,32.275127 -2.2e-4,14.462216 -5.64395,26.89606 -16.93122,37.30158 -11.11129,10.22938 -24.33879,15.52037 -39.68253,15.87302 -2.64566,9e-5 -3.96841,0.70555 -3.96826,2.1164 -1.5e-4,0.17642 0.70532,1.4992 2.11641,3.96825 1.58714,2.46922 3.17444,6.08474 4.7619,10.84656 1.58714,4.76198 2.38079,9.87661 2.38096,15.34391 l 0,0.52914 c -1.7e-4,18.34219 -7.93667,34.39155 -23.80952,48.14814 -15.87315,13.75661 -34.83256,20.63491 -56.8783,20.63491 -22.222284,0 -40.917148,-7.14285 -56.084651,-21.42857 -15.167557,-14.46204 -22.751325,-31.56961 -22.751318,-51.32274 -7e-6,-5.29093 0.529066,-10.40556 1.587301,-15.34391 1.234563,-4.93819 2.733673,-9.17098 4.497357,-12.69841 1.76365,-3.52724 3.439136,-6.52547 5.026451,-8.99471 1.76365,-2.46903 3.439136,-4.49724 5.026457,-6.08465 l 2.116361,-2.6456 c 0.352711,-0.70536 0.529067,-1.32264 0.529135,-1.85186 l 0,-0.26453 m 43.650782,8.20105 c 7.583701,1.1e-4 11.375582,-2.11629 11.375663,-6.3492 -8.1e-5,-4.58542 -3.880146,-6.87819 -11.640209,-6.87831 -4.232872,1.2e-4 -8.201122,0.35285 -11.904764,1.0582 -3.527388,0.70558 -6.261068,1.58742 -8.201053,2.6455 -1.763718,0.88195 -2.64555,1.76379 -2.645502,2.6455 -4.8e-5,0.52921 0.264533,1.05832 0.793647,1.58731 4.938219,3.52744 12.345618,5.29111 22.222218,5.291" /></svg>

Yes, I do think it’s a good idea to use separate ESC. I don’t know if it is needed to separate host code though.

By the way, G’MIC-QT will run CLI code as if it was CLI code, and code[global/local] might have some difference there.

@grosgood

It’s a good idea to trace one line (polyline) and thus optimize the SVG.

Here is the code tried in GMIC GIMP (but not for CLI) and the result.


rm
fx_gmicky 0
CRLF="\\r\\n"
Liste="<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"$CRLF
Liste.="<svg xmlns=\\"http://www.w3.org/2000/svg\\" version=\\"1.1\\" width=\\"1024\\" height=\\"1024\\">"$CRLF
Liste.="<title>Test GMIC SVG</title>"$CRLF
Liste.="<desc>Points et Segments</desc>"$CRLF
Liste.="<polyline points=\\""
if {w}>{h}
resize 1000,{{h}/{w}*1000}
else
resize {{w}/{h}*1000},1000
fi
samj_Masques_Noir_Et_Blanc_Preview 1.25,42
to_gray
negate
dilate 10
pointcloud. 3
channels. 0,1
resize. 1%,1
add. 12
tsp. 200
+crop 0,0,1,0
Xend={i0}
Yend={i1}
rm[-1]
repeat {w}
Index=$>
 +crop $Index,0,{$Index+1},0
 Xa={i0}
 Ya={i1}
 rm.
 Liste.=$Xa" "
 Liste.=$Ya" "
done
Liste.=$Xend" "
Liste.=$Yend" "
Liste.="\\" stroke=\\"blue\\" fill=\\"transparent\\" stroke-width=\\"4\\"/>"$CRLF
Liste.="</svg>"$CRLF
('$Liste')
output_text.  $_path_rc/Liste_Points_TSP.svg
# SVG validé sur https://validator.w3.org/
rm.
1024,1024,1,1
repeat {0,w}
 line. {0,boundary=2;[I[$>],I[$>+1]]},1,255
done
rm[-2]

Liste_Points_TSP

3 Likes