compositor for 2d shader
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. class WZIP extends Shader {
  2. final float sqrt05 = sqrt(0.5);
  3. float[] raw, raw1, raw2, raw3;
  4. float[] in, w, out;
  5. float[] in1, in2, in3, out1, out2, out3;
  6. int n, n2, s;
  7. float scalingfactorin, scalingfactorout;
  8. PImage img;
  9. String sessionid;
  10. WZIP() {
  11. name = "fxWZIP";/*
  12. params.add(new Param ("scale", 0, 1000));
  13. params.add(new Param ("factor1", 0, 1));
  14. params.add(new Param ("factor2", 0, 1));
  15. params.add(new Param ("hsb", 2));
  16. params.add(new Param ("mode", 2));
  17. */
  18. sessionid = hex((int)random(0xffff), 4);
  19. img = createImage(renderer.width, renderer.height, ARGB);
  20. img = renderer.get(0, 0, renderer.width, renderer.height);
  21. // printOption();
  22. // printScale();
  23. }
  24. /*
  25. float inc1, inc2;
  26. void animate() {
  27. inc1+=.03;
  28. inc2+=.04;
  29. //gui.brickP5.getController("knobZero-"+str(brick)).setValue( map(sin(inc1), -1, 1, 0, 255));
  30. //gui.brickP5.getController("knobOne-"+str(brick)).setValue(map(cos(inc2), -1, 1, 0, 255));
  31. if (random(1) > .99) {
  32. for (int i = 0; i < params.size(); i++)
  33. params.get(i).randomize();
  34. }
  35. }
  36. */
  37. void apply() {
  38. // img = createImage(renderer.width, renderer.height, ARGB);
  39. img.resize(renderer.width, renderer.height);
  40. img = renderer.get(0, 0, renderer.width, renderer.height);
  41. s = img.width*img.height;
  42. raw = new float[s*3];
  43. raw1 = new float[s];
  44. raw2 = new float[s];
  45. raw3 = new float[s];
  46. renderer.beginDraw();
  47. renderer.background(0);
  48. renderer.noStroke();
  49. if (boolean((int)params.get(3).value)) {
  50. renderer.colorMode(HSB, 255);
  51. colorMode(HSB, 255);
  52. } else {
  53. renderer.colorMode(RGB, 255);
  54. colorMode(RGB, 255);
  55. }
  56. scalingfactorin = map(params.get(1).value, 0, 1, 0, params.get(0).value);
  57. scalingfactorout = map(params.get(2).value, 0, 1, 0, params.get(0).value);
  58. int iter=0;
  59. int iter2 = 0;
  60. for (int y=0; y<img.height; y++) {
  61. for (int x=0; x<img.width; x++) {
  62. color c = img.get(x, y);
  63. float r, g, b;
  64. if (boolean((int)params.get(3).value)) {
  65. r = hue(c)>127?hue(c)-256:hue(c);
  66. g = saturation(c)>127?saturation(c)-256:saturation(c);
  67. b = brightness(c)>127?brightness(c)-256:brightness(c);
  68. } else {
  69. r = red(c)>127?red(c)-256:red(c);
  70. g = green(c)>127?green(c)-256:green(c);
  71. b = blue(c)>127?blue(c)-256:blue(c);
  72. }
  73. raw[iter++] = r;
  74. raw[iter++] = g;
  75. raw[iter++] = b;
  76. raw1[iter2] = r;
  77. raw2[iter2] = g;
  78. raw3[iter2] = b;
  79. iter2++;
  80. }
  81. }
  82. n = (int)pow(2, ceil(log(s*3)/log(2)));
  83. n2 = (int)pow(2, ceil(log(s)/log(2)));
  84. in = new float[n];
  85. w = new float[n];
  86. out = new float[n];
  87. out1 = new float[n2];
  88. out2 = new float[n2];
  89. out3 = new float[n2];
  90. in1 = new float[n2];
  91. in2 = new float[n2];
  92. in3 = new float[n2];
  93. arrayCopy(raw, 0, in, 0, raw.length);
  94. for (int i=raw.length; i<n; i++) in[i] = raw[raw.length-1];
  95. arrayCopy(raw1, 0, in1, 0, s);
  96. arrayCopy(raw2, 0, in2, 0, s);
  97. arrayCopy(raw3, 0, in3, 0, s);
  98. for (int i=s; i<n2; i++) {
  99. in1[i] = raw1[s-1];
  100. in2[i] = raw2[s-1];
  101. in3[i] = raw3[s-1];
  102. }
  103. if (boolean((int)params.get(4).value)) option1();
  104. else option2();
  105. renderer.colorMode(RGB);
  106. colorMode(RGB);
  107. renderer.endDraw();
  108. }
  109. // void printOption() {
  110. // String str1, str2;
  111. // if (do_hsb) {
  112. // str1 = "HSBHSBHSB...";
  113. // str2 = "HHH...SSS...BBB...";
  114. // } else {
  115. // str1 = "RGBRGBRGB...";
  116. // str2 = "RRR...GGG...BBB...";
  117. // }
  118. // if (option1) println("channels combined: " + str1);
  119. // else println("channels separated: " + str2);
  120. // }
  121. //
  122. // void printScale() {
  123. // println("Scale: 0.."+sc);
  124. // }
  125. float clamp(float c) {
  126. return(abs(c<0?256+c:c)%255.0);
  127. }
  128. void option2() {
  129. wtrafo(in1, n2);
  130. wbtrafo(out1, n2);
  131. wtrafo(in2, n2);
  132. wbtrafo(out2, n2);
  133. wtrafo(in3, n2);
  134. wbtrafo(out3, n2);
  135. for (int i=0; i<s; i++) {
  136. float r = clamp(out1[i]);
  137. float g = clamp(out2[i]);
  138. float b = clamp(out3[i]);
  139. renderer.fill(r, g, b);
  140. renderer.rect(i%img.width, i/img.width, 1, 1);
  141. }
  142. }
  143. void option1() {
  144. wtrafo(in, n);
  145. wbtrafo(out, n);
  146. float r=0, g=0, b=0;
  147. int state = 0;
  148. for (int i=0; i<raw.length; i++) {
  149. float c = clamp(out[i]);
  150. switch(state) {
  151. case 0:
  152. r = c;
  153. break;
  154. case 1:
  155. g = c;
  156. break;
  157. case 2:
  158. b = c;
  159. break;
  160. default:
  161. {
  162. r = c;
  163. renderer.fill(r, g, b);
  164. renderer.rect(floor(i/3.0)%img.width, floor(i/3.0)/img.width, 1, 1);
  165. state = 0;
  166. }
  167. }
  168. state++;
  169. }
  170. }
  171. void wbtrafo(float[] y, int n) {
  172. float[] d = new float[n];
  173. d[n-2] = w[n-1];
  174. int b1 = n-4;
  175. int b2 = n-2;
  176. int a=1;
  177. while (a<n/2) {
  178. for (int i=0; i<a; i++) {
  179. d[2*i+b1]=(d[i+b2]+w[i+b2])*sqrt05;
  180. d[2*i+1+b1]=(d[i+b2]-w[i+b2])*sqrt05;
  181. }
  182. b2=b1;
  183. b1=b1-4*a;
  184. a*=2;
  185. }
  186. for (int i=0; i<a; i++) {
  187. y[2*i]=(d[i]+w[i])*sqrt05;
  188. y[2*i+1]=(d[i]-w[i])*sqrt05;
  189. }
  190. for (int i=0; i<n; i++) y[i] *= scalingfactorout;
  191. }
  192. void wtrafo(float[] y, int n) {
  193. float[] d = new float[n];
  194. int a = n/2;
  195. for (int i=0; i<a; i++) {
  196. w[i] = (y[2*i]-y[2*i+1])*sqrt05;
  197. d[i] = (y[2*i]+y[2*i+1])*sqrt05;
  198. }
  199. int b1 = 0;
  200. int b2 = a;
  201. a/=2;
  202. while (a>0) {
  203. for (int i=0; i<a; i++) {
  204. w[i+b2]=(d[2*i+b1]-d[2*i+1+b1])*sqrt05;
  205. d[i+b2]=(d[2*i+b1]+d[2*i+1+b1])*sqrt05;
  206. }
  207. b1=b2;
  208. b2=b2+a;
  209. a/=2;
  210. }
  211. w[b2] = d[b1];
  212. for (int i=0; i<n-1; i++) w[i] = (int)(w[i]/scalingfactorin);
  213. if (w[n-1]>0) w[n-1] = (int)(w[n-1]/scalingfactorin+0.5);
  214. else w[n-1] = (int)(w[n-1]/scalingfactorin-0.5);
  215. }
  216. }