Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / GUI / Cocoa-GUI / SCImage / SCImageKernel.html
blob3c82e2b88107bf580a431e5e7a18ec3ebea3b306
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="949.33">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #a32213}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #a32213; min-height: 14.0px}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #8e1f11}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #0032b4}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #606060; min-height: 14.0px}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #606060}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #9e210a}
24 p.p15 {margin: 0.0px 0.0px 0.0px 36.0px; text-align: justify; text-indent: -36.0px; font: 10.0px Monaco; color: #9e210a; background-color: #f4f4f4}
25 p.p16 {margin: 0.0px 0.0px 0.0px 36.0px; text-align: justify; text-indent: -36.0px; font: 10.0px Monaco; color: #9e210a}
26 span.s1 {color: #0043f0}
27 span.s2 {color: #0042e5}
28 span.s3 {color: #0040e6}
29 span.s4 {text-decoration: underline}
30 span.s5 {font: 14.0px Helvetica}
31 span.s6 {color: #002da2}
32 span.s7 {color: #0032b4}
33 span.s8 {color: #606060}
34 span.s9 {color: #9e210a}
35 span.s10 {color: #000000}
36 span.Apple-tab-span {white-space:pre}
37 </style>
38 </head>
39 <body>
40 <p class="p1"><b>SCImageKernel</b></p>
41 <p class="p2">// very experimental :)</p>
42 <p class="p3"><br></p>
43 <p class="p4">kernel class to use with <a href="SCImage.html"><span class="s1">SCImage</span></a><span class="s2">.</span> Currently this class represents the CoreImage <b>CIKernel</b> you can apply to a <a href="SCImage.html"><span class="s3">SCImage</span></a>. The Kernel language is a subset of the <i>OpenGL Shading Language</i>.</p>
44 <p class="p5"><br></p>
45 <p class="p4">more information about the Kernel Language can be found here :</p>
46 <p class="p5"><br></p>
47 <p class="p4"><span class="s4">http://developer.apple.com/documentation/GraphicsImaging/Reference/CIKernelLangRef/Introduction/chapter_1_section_1.html</span></p>
48 <p class="p5"><br></p>
49 <p class="p4">and here:</p>
50 <p class="p5"><br></p>
51 <p class="p4"><span class="s4">http://developer.apple.com/documentation/GraphicsImaging/Reference/CIKernelLangRef/chapter_2_section_1.html#//apple_ref/doc/uid/TP40004397-CH206-TPXREF101</span></p>
52 <p class="p5"><br></p>
53 <p class="p5"><br></p>
54 <p class="p4">here is the translation table between Kernel language Objects and SuperCollider objects</p>
55 <p class="p4">--------------------------------------------------------------------------------------------------------------------------------------------------</p>
56 <p class="p4"><b> </span>Kernel Language Object<span class="Apple-tab-span"> </span></b><span class="Apple-tab-span"> </span>|<span class="Apple-tab-span"> </span><b>SuperCollider Object</b></p>
57 <p class="p4">--------------------------------------------------------------------------------------------------------------------------------------------------</p>
58 <p class="p4"><span class="Apple-tab-span"> </span>sampler<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>|<span class="Apple-tab-span"> </span><a href="SCImage.html"><span class="s2">SCImage</span></a></p>
59 <p class="p4"><span class="Apple-tab-span"> </span>__color<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>|<span class="Apple-tab-span"> </span><a href="../../GUI-Tools/Color.html"><span class="s2">Color</span></a></p>
60 <p class="p4"><span class="Apple-tab-span"> </span>float<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>|<span class="Apple-tab-span"> </span><a href="../../../Math/Number.html"><span class="s2">Number</span></a></p>
61 <p class="p4"><span class="Apple-tab-span"> </span>vec2, vec3, vec4<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>|<span class="Apple-tab-span"> </span><a href="../../../Collections/Array.html"><span class="s1">Array</span></a></p>
62 <p class="p4"><span class="Apple-tab-span"> </span>__table<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>| <span class="Apple-tab-span"> </span><a href="SCImage.html"><span class="s2">SCImage</span></a> (basically the __table modifier just use Images as a data providers - LUT)</p>
63 <p class="p5"><br></p>
64 <p class="p5"><br></p>
65 <p class="p6"><b>Class Methods</b></p>
66 <p class="p7"><b><span class="Apple-tab-span"> </span></b></p>
67 <p class="p4"><span class="s5"><b><span class="Apple-tab-span"> </span></b></span><b>*new( shader, values, bounds )</b></p>
68 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>creates a new <span class="s6">SCImageKernel</span></p>
69 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>shader</b> - optional. the shader code string. <span class="s6">nil</span> by default</p>
70 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>values</b> - optional. the values that match the kernel proc function defined in the shader string. <span class="s6">nil</span> by default</p>
71 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><b>bounds</b> - optional. not used for now. <span class="s6">nil</span> by default</p>
72 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
73 <p class="p6"><b>Instance Methods</b></p>
74 <p class="p7"><b><span class="Apple-tab-span"> </span></b></p>
75 <p class="p4"><span class="s5"><b><span class="Apple-tab-span"> </span></b></span><b>shader</b></p>
76 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>get the shader string.</p>
77 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
78 <p class="p4"><span class="Apple-tab-span"> </span><b>shader_</b></p>
79 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>set the shader string.<span class="Apple-tab-span"> </span></p>
80 <p class="p7"><br></p>
81 <p class="p4"><span class="s5"><b><span class="Apple-tab-span"> </span></b></span><b>values</b></p>
82 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>get the values array.</p>
83 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
84 <p class="p4"><span class="Apple-tab-span"> </span><b>values_</b></p>
85 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>set the values array.<span class="Apple-tab-span"> </span>the object indexes in the values Array must match the argument declaration order as defined in the main</p>
86 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><i>kernel vec4 routine</i>. See examples for more info.</p>
87 <p class="p7"><b><span class="Apple-tab-span"> </span></b></p>
88 <p class="p4"><span class="s5"><b><span class="Apple-tab-span"> </span></b></span><b>isValid</b></p>
89 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>very basic verification to tell if all arguments of the shader are set.</p>
90 <p class="p7"><span class="Apple-tab-span"> </span></p>
91 <p class="p4"><span class="s5"><span class="Apple-tab-span"> </span></span><b>compile</b></p>
92 <p class="p4"><b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></b>compile the SCImageKernel object (and cache it). </span></p>
93 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>Note: when rendered the first time, the kernel object is always compiled first.</p>
94 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>If you plan to change the shader string after, you must explicitely compile it to make it effective.</p>
95 <p class="p7"><span class="Apple-tab-span"> </span></p>
96 <p class="p8">/**** Kernels ****/</p>
97 <p class="p8">// very experimental</p>
98 <p class="p8">// COLOR INVERSION SHADER EXAMPLE</p>
99 <p class="p9">(</p>
100 <p class="p9">a = <span class="s7">SCImage</span>.new(<span class="s7">Document</span>.current.dir +/+ <span class="s8">"vduck2.jpg"</span>); <span class="s9">// get the image</span></p>
101 <p class="p10"><span class="s10">k = </span>SCImageKernel<span class="s10">.new;</span></p>
102 <p class="p9">k.shader_(<span class="s8">"</span></p>
103 <p class="p11"><br></p>
104 <p class="p12">vec4<span class="Apple-tab-span"> </span>invertPixel(vec4 pix) {</p>
105 <p class="p12"><span class="Apple-tab-span"> </span>return vec4(1.0 - pix.r, 1.0 - pix.g, 1.0 - pix.b, pix.a);</p>
106 <p class="p12">}</p>
107 <p class="p11"><br></p>
108 <p class="p12">kernel vec4 _invertColor(sampler source)</p>
109 <p class="p12">{</p>
110 <p class="p12"><span class="Apple-tab-span"> </span>vec4 pixel;</p>
111 <p class="p12"><span class="Apple-tab-span"> </span>pixel = sample(source, samplerCoord(source));</p>
112 <p class="p12"><span class="Apple-tab-span"> </span>unpremultiply(pixel);</p>
113 <p class="p12"><span class="Apple-tab-span"> </span>return unpremultiply(invertPixel(pixel));</p>
114 <p class="p12">}</p>
115 <p class="p9"><span class="s8">"</span>);</p>
116 <p class="p13"><br></p>
117 <p class="p14">// the argument order should be kept in the array</p>
118 <p class="p14">// here we need only the "sampler" argument wich should be as the translation table informs you a SCImage</p>
119 <p class="p14">// the signature of the Kernel function is normaly 'kernel vec4'</p>
120 <p class="p14">// you can of course add other functions in the shader</p>
121 <p class="p9">k.values_([a]);</p>
122 <p class="p13"><br></p>
123 <p class="p9">k.isValid.postln; <span class="s9">// is it ok</span></p>
124 <p class="p9">a.applyKernel(k);</p>
125 <p class="p13"><br></p>
126 <p class="p9">w = a.plot(freeOnClose:<span class="s7">true</span>);</p>
127 <p class="p9">)</p>
128 <p class="p13"><br></p>
129 <p class="p13"><br></p>
130 <p class="p9">(</p>
131 <p class="p14">// ANOTHER APPLE KERNEL EXAMPLE - See CoreImage programming guide for original example</p>
132 <p class="p9">a = <span class="s7">SCImage</span>.new(<span class="s7">Document</span>.current.dir +/+ <span class="s8">"vduck2.jpg"</span>); <span class="s9">// get the image</span></p>
133 <p class="p10"><span class="s10">k = </span>SCImageKernel<span class="s10">.new;</span></p>
134 <p class="p9">k.shader_(<span class="s8">"</span></p>
135 <p class="p12">vec2 testVec(float x, float y)</p>
136 <p class="p12">{</p>
137 <p class="p12"><span class="Apple-tab-span"> </span>return vec2(x, y);</p>
138 <p class="p12">}</p>
139 <p class="p11"><br></p>
140 <p class="p12">kernel vec4 testKernelFromApple( sampler src, __color color, float distance, float slope )</p>
141 <p class="p12">{</p>
142 <p class="p12"><span class="Apple-tab-span"> </span>vec4 <span class="Apple-tab-span"> </span>t;</p>
143 <p class="p12"><span class="Apple-tab-span"> </span>float<span class="Apple-tab-span"> </span>d;</p>
144 <p class="p12"><span class="Apple-tab-span"> </span>d = destCoord().y * slope + distance;</p>
145 <p class="p12"><span class="Apple-tab-span"> </span>t = unpremultiply(sample(src, samplerCoord(src)));</p>
146 <p class="p12"><span class="Apple-tab-span"> </span>t = (t - d*color) / (1.0-d);</p>
147 <p class="p12"><span class="Apple-tab-span"> </span>return premultiply(t);</p>
148 <p class="p12">}</p>
149 <p class="p9"><span class="s8">"</span>);</p>
150 <p class="p13"><br></p>
151 <p class="p14">// as stated in the Apple Example</p>
152 <p class="p14">// distance - min: 0.0 max: 1.0</p>
153 <p class="p14">// slope - min: -0.01 max: 0.01</p>
154 <p class="p9">k.values_(</p>
155 <p class="p9"><span class="Apple-tab-span"> </span>[</p>
156 <p class="p14"><span class="s10"><span class="Apple-tab-span"> </span>a,<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// arg 0: the SCImage</p>
157 <p class="p14"><span class="s10"><span class="Apple-tab-span"> </span></span><span class="s7">Color</span><span class="s10">.white,  </span></span>// arg 1: color</p>
158 <p class="p14"><span class="s10"><span class="Apple-tab-span"> </span>0.5, <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// arg 2: distance</p>
159 <p class="p14"><span class="s10"><span class="Apple-tab-span"> </span>-0.002<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// arg 3: slope</p>
160 <p class="p9"><span class="Apple-tab-span"> </span>]</p>
161 <p class="p9">);</p>
162 <p class="p13"><br></p>
163 <p class="p9">a.applyKernel(k);</p>
164 <p class="p13"><br></p>
165 <p class="p9">w = a.plot(freeOnClose:<span class="s7">true</span>);</p>
166 <p class="p9">)</p>
167 <p class="p13"><br></p>
168 <p class="p13"><br></p>
169 <p class="p13"><br></p>
170 <p class="p13"><br></p>
171 <p class="p9">(</p>
172 <p class="p14">// OK a Better Colorfull Kernel</p>
173 <p class="p9">a = <span class="s7">SCImage</span>.new(600@600); <span class="s9">// get the image</span></p>
174 <p class="p10"><span class="s10">k = </span>SCImageKernel<span class="s10">.new;</span></p>
175 <p class="p13"><br></p>
176 <p class="p9">k.shader_(</p>
177 <p class="p14">// shader/kernel from toneburst.com</p>
178 <p class="p15">// Generates spherical and planar displacement maps for VBO-based 3D heightfield. </p>
179 <p class="p16">// http://machinesdontcare.wordpress.com </p>
180 <p class="p12">"</p>
181 <p class="p12">const float PI = 3.14159265359;</p>
182 <p class="p12">const float TWOPI = 6.28318530718;</p>
183 <p class="p11"><br></p>
184 <p class="p12">kernel vec4 _heightMap(sampler image, vec3 scale)</p>
185 <p class="p12">{</p>
186 <p class="p12"><span class="Apple-tab-span"> </span>vec2 xyNorm = samplerCoord(image) / samplerSize(image);</p>
187 <p class="p12"><span class="Apple-tab-span"> </span>float u = xyNorm.x * PI;</p>
188 <p class="p12"><span class="Apple-tab-span"> </span>float v = xyNorm.y * TWOPI;</p>
189 <p class="p12"><span class="Apple-tab-span"> </span>vec3 spherical;</p>
190 <p class="p12"><span class="Apple-tab-span"> </span>spherical.r = cos(v) * sin(u);</p>
191 <p class="p12"><span class="Apple-tab-span"> </span>spherical.g = sin(v) * sin(u);</p>
192 <p class="p12"><span class="Apple-tab-span"> </span>spherical.b = cos(u);</p>
193 <p class="p11"><span class="Apple-tab-span"> </span></p>
194 <p class="p12"><span class="Apple-tab-span"> </span>spherical.r = (spherical.r * 0.5 + 0.5) * scale.x;</p>
195 <p class="p12"><span class="Apple-tab-span"> </span>spherical.g = (spherical.g * 0.5 + 0.5) * scale.y;</p>
196 <p class="p12"><span class="Apple-tab-span"> </span>spherical.b = (spherical.b * 0.5 + 0.5) * scale.z;</p>
197 <p class="p11"><span class="Apple-tab-span"> </span></p>
198 <p class="p12"><span class="Apple-tab-span"> </span>return vec4(spherical,1.0);</p>
199 <p class="p12">}</p>
200 <p class="p9"><span class="s8">"</span>);</p>
201 <p class="p13"><br></p>
202 <p class="p9">k.values_([a, [1.0, 1.0, 1.0]]);</p>
203 <p class="p13"><br></p>
204 <p class="p14">// k.isValid; // is it ok</p>
205 <p class="p9">a.applyKernel(k);</p>
206 <p class="p14"><span class="s10">//.flatten; </span>// ensure a bitmap rep so the kernel is not applied at each rendering call - uncomment that and rescale the plot window to see the difference.</p>
207 <p class="p13"><br></p>
208 <p class="p9">w = a.plot(freeOnClose:<span class="s7">true</span>);</p>
209 <p class="p9">)</p>
210 </body>
211 </html>