compositor for 2d glitch effects
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.

effects.pde 130KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355
  1. /*
  2. ASDFPIXELSORT
  3. by Kim Asendorf
  4. */
  5. class ASDFPIXELSORT extends Shader {
  6. ASDFPIXELSORT() {
  7. name = "fxASDFPixelSort";
  8. params.add(new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  9. params.add(new Param("target", INTVAL, 0, 2, new int[]{RANDOM}));
  10. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  11. directionParamIndex = 2;
  12. }
  13. int previousMode;
  14. void apply() {
  15. if (previousMode != int(params.get(1).value)) {
  16. if (params.get(1).value == 0) changeParam(0, new Param("black", INTVAL, -17000000, -2000000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  17. if (params.get(1).value == 1) changeParam(0, new Param("brightness", INTVAL, 0, 200, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  18. if (params.get(1).value == 2) changeParam(0, new Param("white", INTVAL, -15000000, -700000, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  19. }
  20. previousMode = (int)params.get(1).value;
  21. row = 0;
  22. column = 0;
  23. colorMode(RGB);
  24. canvas.beginDraw();
  25. while (column < canvas.width-1) {
  26. canvas.loadPixels();
  27. sortColumn();
  28. column++;
  29. canvas.updatePixels();
  30. }
  31. while (row < canvas.height-1) {
  32. canvas.loadPixels();
  33. sortRow();
  34. row++;
  35. canvas.updatePixels();
  36. }
  37. canvas.endDraw();
  38. }
  39. int row = 0;
  40. int column = 0;
  41. void sortRow() {
  42. int x = 0;
  43. int y = row;
  44. int xend = 0;
  45. while (xend < canvas.width-1) {
  46. switch((int)params.get(1).value) {
  47. case 0:
  48. x = getFirstNotBlackX(x, y);
  49. xend = getNextBlackX(x, y);
  50. break;
  51. case 1:
  52. x = getFirstBrightX(x, y);
  53. xend = getNextDarkX(x, y);
  54. break;
  55. case 2:
  56. x = getFirstNotWhiteX(x, y);
  57. xend = getNextWhiteX(x, y);
  58. break;
  59. default:
  60. break;
  61. }
  62. if (x < 0) break;
  63. int sortLength = xend-x;
  64. color[] unsorted = new color[sortLength];
  65. color[] sorted = new color[sortLength];
  66. for (int i=0; i<sortLength; i++) {
  67. unsorted[i] = canvas.pixels[x + i + y * canvas.width];
  68. }
  69. sorted = sort(unsorted);
  70. for (int i=0; i<sortLength; i++) {
  71. canvas.pixels[x + i + y * canvas.width] = sorted[i];
  72. }
  73. x = xend+1;
  74. }
  75. }
  76. void sortColumn() {
  77. int x = column;
  78. int y = 0;
  79. int yend = 0;
  80. while (yend < canvas.height-1) {
  81. switch((int)params.get(1).value) {
  82. case 0:
  83. y = getFirstNotBlackY(x, y);
  84. yend = getNextBlackY(x, y);
  85. break;
  86. case 1:
  87. y = getFirstBrightY(x, y);
  88. yend = getNextDarkY(x, y);
  89. break;
  90. case 2:
  91. y = getFirstNotWhiteY(x, y);
  92. yend = getNextWhiteY(x, y);
  93. break;
  94. default:
  95. break;
  96. }
  97. if (y < 0) break;
  98. int sortLength = yend-y;
  99. color[] unsorted = new color[sortLength];
  100. color[] sorted = new color[sortLength];
  101. for (int i=0; i<sortLength; i++) {
  102. unsorted[i] = canvas.pixels[x + (y+i) * canvas.width];
  103. }
  104. sorted = sort(unsorted);
  105. for (int i=0; i<sortLength; i++) {
  106. canvas.pixels[x + (y+i) * canvas.width] = sorted[i];
  107. }
  108. y = yend+1;
  109. }
  110. }
  111. //BLACK
  112. int getFirstNotBlackX(int _x, int _y) {
  113. int x = _x;
  114. int y = _y;
  115. color c;
  116. while ( (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  117. x++;
  118. if (x >= canvas.width) return -1;
  119. }
  120. return x;
  121. }
  122. int getNextBlackX(int _x, int _y) {
  123. int x = _x+1;
  124. int y = _y;
  125. color c;
  126. while ( (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  127. x++;
  128. if (x >= canvas.width) return canvas.width-1;
  129. }
  130. return x-1;
  131. }
  132. //BRIGHTNESS
  133. int getFirstBrightX(int _x, int _y) {
  134. int x = _x;
  135. int y = _y;
  136. color c;
  137. while (brightness (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  138. x++;
  139. if (x >= canvas.width) return -1;
  140. }
  141. return x;
  142. }
  143. int getNextDarkX(int _x, int _y) {
  144. int x = _x+1;
  145. int y = _y;
  146. color c;
  147. while (brightness (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  148. x++;
  149. if (x >= canvas.width) return canvas.width-1;
  150. }
  151. return x-1;
  152. }
  153. //WHITE
  154. int getFirstNotWhiteX(int _x, int _y) {
  155. int x = _x;
  156. int y = _y;
  157. color c;
  158. while ( (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  159. x++;
  160. if (x >= canvas.width) return -1;
  161. }
  162. return x;
  163. }
  164. int getNextWhiteX(int _x, int _y) {
  165. int x = _x+1;
  166. int y = _y;
  167. color c;
  168. while ( (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  169. x++;
  170. if (x >= canvas.width) return canvas.width-1;
  171. }
  172. return x-1;
  173. }
  174. //BLACK
  175. int getFirstNotBlackY(int _x, int _y) {
  176. int x = _x;
  177. int y = _y;
  178. color c;
  179. if (y < canvas.height) {
  180. while ( (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  181. y++;
  182. if (y >= canvas.height) return -1;
  183. }
  184. }
  185. return y;
  186. }
  187. int getNextBlackY(int _x, int _y) {
  188. int x = _x;
  189. int y = _y+1;
  190. color c;
  191. if (y < canvas.height) {
  192. while ( (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  193. y++;
  194. if (y >= canvas.height) return canvas.height-1;
  195. }
  196. }
  197. return y-1;
  198. }
  199. //BRIGHTNESS
  200. int getFirstBrightY(int _x, int _y) {
  201. int x = _x;
  202. int y = _y;
  203. color c;
  204. if (y < canvas.height) {
  205. while (brightness (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  206. y++;
  207. if (y >= canvas.height) return -1;
  208. }
  209. }
  210. return y;
  211. }
  212. int getNextDarkY(int _x, int _y) {
  213. int x = _x;
  214. int y = _y+1;
  215. color c;
  216. if (y < canvas.height) {
  217. while (brightness (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  218. y++;
  219. if (y >= canvas.height) return canvas.height-1;
  220. }
  221. }
  222. return y-1;
  223. }
  224. //WHITE
  225. int getFirstNotWhiteY(int _x, int _y) {
  226. int x = _x;
  227. int y = _y;
  228. color c;
  229. if (y < canvas.height) {
  230. while ( (c = canvas.pixels[x + y * canvas.width]) > params.get(0).value) {
  231. y++;
  232. if (y >= canvas.height) return -1;
  233. }
  234. }
  235. return y;
  236. }
  237. int getNextWhiteY(int _x, int _y) {
  238. int x = _x;
  239. int y = _y+1;
  240. color c;
  241. if (y < canvas.height) {
  242. while ( (c = canvas.pixels[x + y * canvas.width]) < params.get(0).value) {
  243. y++;
  244. if (y >= canvas.height) return canvas.height-1;
  245. }
  246. }
  247. return y-1;
  248. }
  249. }
  250. /*
  251. DISTORTER
  252. by Tomasz Sulej
  253. */
  254. class DISTORTER extends Shader {
  255. boolean do_blend = false; // blend image after process
  256. int blend_mode = OVERLAY; // blend type
  257. int channel = BRIGHTNESS; // channel used in processing (R,G,B) or (H,S,B)
  258. float scalex = 0.05; // from 0.01 to 1
  259. float scaley = 0.1; // from 0.01 to 1
  260. boolean shift_hue = true;
  261. float shift_amt = 0.1; // from 0 to 1
  262. PImage buffer;
  263. final static int distortionMatrixSize = 512; //doesnt really make a difference, it's more or less "noise variety".. only kinda different if real low, like 2 or so
  264. int[][] distort = new int[2][distortionMatrixSize];
  265. final static float tick = 1.0/distortionMatrixSize;
  266. int mode = 0;
  267. int initBufferW, initBufferH;
  268. DISTORTER() {
  269. buffer = createImage(canvas.width, canvas.height, ARGB);
  270. initBufferW = buffer.width;
  271. initBufferH = buffer.height;
  272. name = "fxDistorter";
  273. params.add(new Param("width", FLOATVAL, 2, buffer.width/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  274. params.add(new Param("height", FLOATVAL, 2, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  275. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  276. params.add(new Param("shift hue amount", FLOATVAL, 0, 1, new int[]{SAWTOOTH, SAWTOOTHINVERSE, TAN, TANINVERSE, RAMP, RAMPINVERSE}));
  277. params.add(new Param("scale x", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  278. params.add(new Param("scale y", FLOATVAL, 0.01, 1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  279. params.add(new Param("blend mode", INTVAL, 0, blends.length-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  280. params.add(new Param("channel", INTVAL, 0, 12, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  281. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  282. directionParamIndex = 8;
  283. //params.add(new Param(DIRECTION));
  284. // channel, blend_mode
  285. //params.add(new Param("height", FLOATVAL, 1, buffer.height/4-1, new int[]{SINE, SAWTOOTH, RAMPUPDOWN, TAN, TANINVERSE, TRIANG}));
  286. // prepare distortion pattern
  287. for (int i=0; i<distortionMatrixSize; i++) {
  288. distort[0][i] = (int)random(-128, 128);
  289. distort[1][i] = (int)random(-128, 128);
  290. }
  291. }
  292. // ALL Channels, Nxxx stand for negative (255-value)
  293. // channels to work with
  294. final static int RED = 0;
  295. final static int GREEN = 1;
  296. final static int BLUE = 2;
  297. final static int HUE = 3;
  298. final static int SATURATION = 4;
  299. final static int BRIGHTNESS = 5;
  300. final static int NRED = 6;
  301. final static int NGREEN = 7;
  302. final static int NBLUE = 8;
  303. final static int NHUE = 9;
  304. final static int NSATURATION = 10;
  305. final static int NBRIGHTNESS = 11;
  306. void apply() {
  307. buffer = canvas.get();
  308. buffer.resize(canvas.width, canvas.height);
  309. float neww = map(params.get(0).value, 2, initBufferW-2, 2, buffer.width/4-2);
  310. float newh = map(params.get(1).value, 2, initBufferH-2, 2, buffer.height/4-2);
  311. do_blend = boolean(int(params.get(2).value));
  312. shift_amt = params.get(3).value;
  313. scalex = params.get(4).value;
  314. scaley = params.get(5).value;
  315. blend_mode = blends[(int)params.get(6).value];
  316. channel = (int)params.get(7).value;
  317. float totalnum = neww+newh;
  318. float times = (totalnum/floor(totalnum/neww));
  319. float offx = (totalnum%neww)/times;
  320. float ratiox = neww/buffer.width;
  321. //println(ratiox);
  322. canvas.beginDraw();
  323. canvas.noStroke();
  324. for (int y=0; y<buffer.height; y++) {
  325. float yy = y/(float)buffer.height;
  326. for (int x=0; x<buffer.width; x++) {
  327. float xx = x/(float)buffer.width;
  328. float offy = floor(newh*yy);
  329. float fx = xx*ratiox+offx*offy;
  330. float shift = fx%1.0;
  331. float st = shift/tick;
  332. int no1 = floor(st)%distortionMatrixSize;
  333. int no2 = ceil(st)%distortionMatrixSize ;
  334. float l = st-(float)no1;
  335. float cx = lerp(distort[0][no1], distort[0][no2], l);
  336. float cy = lerp(distort[1][no1], distort[1][no2], l);
  337. float rx =getChannel(buffer.get(x, y), channel);
  338. int sx = (int)((buffer.width+x+cx*rx*scalex*0.1)%buffer.width);
  339. int sy = (int)((buffer.height+y+cy*scaley)%buffer.height);
  340. color c=buffer.get(sx, sy);
  341. if (shift_hue) {
  342. colorMode(HSB, 255);
  343. c = color((hue(c)+shift_amt*255*noise(newh+y))%255.0, constrain(saturation(c)*1.2, 0, 255), constrain(brightness(c), 0, 255));
  344. colorMode(RGB, 255);
  345. }
  346. // buffer.fill(lerpColor(c,img.get(x,y),0.2));
  347. canvas.fill(c); //wärs nich effizienter die pixelmatrix zu ändern ?
  348. canvas.rect(x, y, 1, 1);
  349. }
  350. }
  351. if (do_blend)
  352. canvas.blend(buffer, 0, 0, buffer.width, buffer.height, 0, 0, canvas.width, canvas.height, blend_mode);
  353. canvas.endDraw();
  354. }
  355. float getChannel(color c, int channel) {
  356. int ch = channel>5?channel-6:channel;
  357. float cc;
  358. switch(ch) {
  359. case RED:
  360. cc = red(c);
  361. break;
  362. case GREEN:
  363. cc = green(c);
  364. break;
  365. case BLUE:
  366. cc = blue(c);
  367. break;
  368. case HUE:
  369. cc = hue(c);
  370. break;
  371. case SATURATION:
  372. cc = saturation(c);
  373. break;
  374. default:
  375. cc= brightness(c);
  376. break;
  377. }
  378. return channel>5?255-cc:cc;
  379. }
  380. }
  381. /*
  382. FM
  383. */
  384. class FM extends Shader {
  385. // configuration
  386. int colorspace = RGB;
  387. int quantval = 30; // 0 - off, less - more glitch, more - more precision
  388. boolean do_blend = true; // blend image after process
  389. int blend_mode = OVERLAY; // blend type
  390. //unused parameters (giers):
  391. final static boolean first_channel_only = false; // for L.. or Y.. colorspaces set true to modulate only luma;
  392. final static boolean lowpass1_on = true; // on/off of first low pass filter
  393. final static boolean lowpass2_on = true; // on/off of second low pass filter
  394. final static boolean lowpass3_on = true; // on/off of third low pass filter
  395. // better don't touch it, lowpass filters are run in cascade
  396. float lowpass1_cutoff = 0.25; // percentage of rate
  397. float lowpass2_cutoff = 0.1;
  398. float lowpass3_cutoff = 0.05;
  399. // working buffer
  400. PGraphics buffer;
  401. // local variables
  402. float min_omega, max_omega;
  403. float min_phase_mult=0.05;
  404. float max_phase_mult=50.0;
  405. LowpassFilter lpf1, lpf2, lpf3;
  406. int[][] pxls;
  407. boolean negate = false;
  408. FM() {
  409. name = "fxFM";
  410. params.add(new Param ("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  411. params.add(new Param ("blend_mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  412. params.add(new Param ("omega", FLOATVAL, 0, 1, new int[]{SINE, SAWTOOTH, TRIANG}));
  413. params.add(new Param ("phase", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  414. params.add(new Param ("colorspace", INTVAL, 0, 16, new int[]{RANDOM}));
  415. params.add(new Param ("quant", INTVAL, 0, 40, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  416. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  417. directionParamIndex = 6;
  418. buffer = createGraphics(canvas.width, canvas.height);
  419. buffer.beginDraw();
  420. buffer.noStroke();
  421. //buffer.smooth(8);
  422. //buffer.background(0);
  423. // buffer.image(canvas, 0, 0);
  424. buffer.endDraw();
  425. float rate = 100000.0;
  426. lpf1 = new LowpassFilter(rate, lowpass1_cutoff*rate);
  427. lpf2 = new LowpassFilter(rate, lowpass2_cutoff*rate);
  428. lpf3 = new LowpassFilter(rate, lowpass3_cutoff*rate);
  429. //img.loadPixels();
  430. }
  431. void prepareData() {
  432. pxls = new int[3][canvas.pixels.length];
  433. for (int i=0; i<canvas.pixels.length; i++) {
  434. int cl = toColorspace(canvas.pixels[i], colorspace);
  435. pxls[0][i] = (cl >> 16) & 0xff;
  436. pxls[1][i] = (cl >> 8) & 0xff;
  437. pxls[2][i] = (cl) & 0xff;
  438. }
  439. }
  440. float omega, min_phase, max_phase;
  441. int rw, rh;
  442. void apply() {
  443. buffer.setSize(canvas.width, canvas.height);
  444. min_omega = TWO_PI/(0.05*canvas.width);
  445. max_omega = TWO_PI/(300.0*canvas.width);
  446. rw = canvas.width;
  447. do_blend = boolean(int(params.get(0).value));
  448. blend_mode = blends[(int)params.get(1).value];
  449. omega = map(sqrt(params.get(2).value), 0, 1, min_omega, max_omega);
  450. float phase = map(sq(params.get(3).value), 0, 1, min_phase_mult, max_phase_mult);
  451. colorspace = (int)params.get(4).value;
  452. quantval = (int) params.get(5).value;
  453. if (rw != canvas.width || rh != canvas.height) {
  454. rw = canvas.width;
  455. rh = canvas.height;
  456. min_omega = TWO_PI/(0.05*canvas.width);
  457. max_omega = TWO_PI/(300.0*canvas.width);
  458. }
  459. prepareData();
  460. //buffer = canvas.get(0, 0, canvas.width, canvas.height);
  461. max_phase = phase * omega;
  462. min_phase = -max_phase;
  463. processImage();
  464. }
  465. void processImage() {
  466. buffer.beginDraw();
  467. buffer.loadPixels();
  468. int [][] dest_pxls = new int[3][canvas.pixels.length];
  469. if (first_channel_only) {
  470. arrayCopy(pxls[1], dest_pxls[1]);
  471. arrayCopy(pxls[2], dest_pxls[2]);
  472. }
  473. for (int i=0; i< (first_channel_only?1:3); i++) {
  474. for (int y=0; y<canvas.height; y++) {
  475. int off = y * canvas.width;
  476. //reset filters each line
  477. lpf1.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  478. lpf2.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  479. lpf3.resetFilter(map(pxls[i][off], 0, 255, min_phase, max_phase));
  480. float sig_int = 0; // integral of the signal
  481. float pre_m = 0; // previous value of modulated signal
  482. for (int x=0; x<canvas.width; x++) {
  483. /////////////////////////
  484. // FM part starts here
  485. /////////////////////////
  486. float sig = map(pxls[i][x+off], 0, 255, min_phase, max_phase); // current signal value
  487. sig_int += sig; // current value of signal integral
  488. float m = cos(omega * x + sig_int); // modulate signal
  489. if ( quantval > 0) {
  490. m = map((int)map(m, -1, 1, 0, quantval), 0, quantval, -1, 1); // quantize
  491. }
  492. float dem = abs(m-pre_m); // demodulate signal, derivative
  493. pre_m = m; // remember current value
  494. // lowpass filter chain
  495. if (lowpass1_on) dem = lpf1.lowpass(dem);
  496. if (lowpass2_on) dem = lpf2.lowpass(dem);
  497. if (lowpass3_on) dem = lpf3.lowpass(dem);
  498. // remap signal back to channel value
  499. int v = constrain( (int)map(2*(dem-omega), min_phase, max_phase, 0, 255), 0, 255);
  500. //////////////////////
  501. // FM part ends here
  502. //////////////////////
  503. dest_pxls[i][x+off] = negate?255-v:v;
  504. }
  505. }
  506. }
  507. for (int i=0; i<buffer.pixels.length; i++) {
  508. buffer.pixels[i] = fromColorspace(0xff000000 | (dest_pxls[0][i] << 16) | (dest_pxls[1][i] << 8) | (dest_pxls[2][i]), colorspace);
  509. }
  510. buffer.updatePixels();
  511. if (do_blend)
  512. buffer.blend(canvas, 0, 0, canvas.width, canvas.height, 0, 0, buffer.width, buffer.height, blend_mode);
  513. buffer.endDraw();
  514. canvas.beginDraw();
  515. canvas.image(buffer, canvas.width/2, canvas.height/2, canvas.width, canvas.height);
  516. canvas.endDraw();
  517. }
  518. class LowpassFilter {
  519. float alpha;
  520. float prev;
  521. public LowpassFilter(float rate, float hz) {
  522. alpha = 0.0;
  523. prev = 0.0;
  524. setFilter(rate, hz);
  525. }
  526. void setFilter(float rate, float hz) {
  527. float timeInterval = 1.0/rate;
  528. float tau = 1.0 / (hz * TWO_PI);
  529. alpha = timeInterval / (tau + timeInterval);
  530. }
  531. void resetFilter(float val) {
  532. prev = val;
  533. }
  534. void resetFilter() {
  535. resetFilter(0);
  536. }
  537. float lowpass(float sample) {
  538. float stage1 = sample * alpha;
  539. float stage2 = prev - (prev * alpha);
  540. prev = (stage1 + stage2);
  541. return prev;
  542. }
  543. float highpass(float sample) {
  544. return sample - lowpass(sample);
  545. }
  546. }
  547. }
  548. /*
  549. WZIP
  550. */
  551. class WZIP extends Shader {
  552. final float sqrt05 = sqrt(0.5);
  553. float[] raw, raw1, raw2, raw3;
  554. float[] in, w, out;
  555. float[] in1, in2, in3, out1, out2, out3;
  556. int n, n2, s;
  557. float scalingfactorin, scalingfactorout;
  558. PImage img;
  559. String sessionid;
  560. WZIP() {
  561. name = "fxWZIP";
  562. params.add(new Param ("scale", FLOATVAL, 0.1, 1000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  563. params.add(new Param ("factor in", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  564. params.add(new Param ("factor out", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  565. params.add(new Param("hsb/rgb", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  566. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  567. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  568. directionParamIndex = 5;
  569. sessionid = hex((int)random(0xffff), 4);
  570. img = createImage(canvas.width, canvas.height, ARGB);
  571. img = canvas.get(0, 0, canvas.width, canvas.height);
  572. }
  573. void apply() {
  574. // img = createImage(canvas.width, canvas.height, ARGB);
  575. img.resize(canvas.width, canvas.height);
  576. img = canvas.get(0, 0, canvas.width, canvas.height);
  577. s = img.width*img.height;
  578. raw = new float[s*3];
  579. raw1 = new float[s];
  580. raw2 = new float[s];
  581. raw3 = new float[s];
  582. canvas.beginDraw();
  583. canvas.background(0);
  584. canvas.noStroke();
  585. if (boolean((int)params.get(3).value)) { /////////////////////
  586. canvas.colorMode(HSB, 255);
  587. colorMode(HSB, 255);
  588. } else {
  589. canvas.colorMode(RGB, 255);
  590. colorMode(RGB, 255);
  591. }
  592. scalingfactorin = map(params.get(1).value, 0, 1, 0, params.get(0).value); /////////////////////
  593. scalingfactorout = map(params.get(2).value, 0, 1, 0, params.get(0).value); /////////////////////
  594. int iter=0;
  595. int iter2 = 0;
  596. for (int y=0; y<img.height; y++) {
  597. for (int x=0; x<img.width; x++) {
  598. color c = img.get(x, y);
  599. float r, g, b;
  600. if (boolean((int)params.get(3).value)) { /////////////////////
  601. r = hue(c)>127?hue(c)-256:hue(c);
  602. g = saturation(c)>127?saturation(c)-256:saturation(c);
  603. b = brightness(c)>127?brightness(c)-256:brightness(c);
  604. } else {
  605. r = red(c)>127?red(c)-256:red(c);
  606. g = green(c)>127?green(c)-256:green(c);
  607. b = blue(c)>127?blue(c)-256:blue(c);
  608. }
  609. raw[iter++] = r;
  610. raw[iter++] = g;
  611. raw[iter++] = b;
  612. raw1[iter2] = r;
  613. raw2[iter2] = g;
  614. raw3[iter2] = b;
  615. iter2++;
  616. }
  617. }
  618. n = (int)pow(2, ceil(log(s*3)/log(2)));
  619. n2 = (int)pow(2, ceil(log(s)/log(2)));
  620. in = new float[n];
  621. w = new float[n];
  622. out = new float[n];
  623. out1 = new float[n2];
  624. out2 = new float[n2];
  625. out3 = new float[n2];
  626. in1 = new float[n2];
  627. in2 = new float[n2];
  628. in3 = new float[n2];
  629. arrayCopy(raw, 0, in, 0, raw.length);
  630. for (int i=raw.length; i<n; i++) in[i] = raw[raw.length-1];
  631. arrayCopy(raw1, 0, in1, 0, s);
  632. arrayCopy(raw2, 0, in2, 0, s);
  633. arrayCopy(raw3, 0, in3, 0, s);
  634. for (int i=s; i<n2; i++) {
  635. in1[i] = raw1[s-1];
  636. in2[i] = raw2[s-1];
  637. in3[i] = raw3[s-1];
  638. }
  639. if (boolean((int)params.get(4).value)) option1(); /////////////////////
  640. else option2();
  641. canvas.colorMode(RGB);
  642. colorMode(RGB);
  643. canvas.endDraw();
  644. }
  645. float clamp(float c) {
  646. return(abs(c<0?256+c:c)%255.0);
  647. }
  648. void option2() {
  649. wtrafo(in1, n2);
  650. wbtrafo(out1, n2);
  651. wtrafo(in2, n2);
  652. wbtrafo(out2, n2);
  653. wtrafo(in3, n2);
  654. wbtrafo(out3, n2);
  655. for (int i=0; i<s; i++) {
  656. float r = clamp(out1[i]);
  657. float g = clamp(out2[i]);
  658. float b = clamp(out3[i]);
  659. canvas.fill(r, g, b);
  660. canvas.rect(i%canvas.width, i/canvas.width, 1, 1);
  661. }
  662. }
  663. void option1() {
  664. wtrafo(in, n);
  665. wbtrafo(out, n);
  666. float r=0, g=0, b=0;
  667. int state = 0;
  668. for (int i=0; i<raw.length; i++) {
  669. float c = clamp(out[i]);
  670. switch(state) {
  671. case 0:
  672. r = c;
  673. break;
  674. case 1:
  675. g = c;
  676. break;
  677. case 2:
  678. b = c;
  679. break;
  680. default:
  681. {
  682. r = c;
  683. canvas.fill(r, g, b);
  684. canvas.rect(floor(i/3.0)%canvas.width, floor(i/3.0)/canvas.width, 1, 1);
  685. state = 0;
  686. }
  687. }
  688. state++;
  689. }
  690. }
  691. void wbtrafo(float[] y, int n) {
  692. float[] d = new float[n];
  693. d[n-2] = w[n-1];
  694. int b1 = n-4;
  695. int b2 = n-2;
  696. int a=1;
  697. while (a<n/2) {
  698. for (int i=0; i<a; i++) {
  699. d[2*i+b1]=(d[i+b2]+w[i+b2])*sqrt05;
  700. d[2*i+1+b1]=(d[i+b2]-w[i+b2])*sqrt05;
  701. }
  702. b2=b1;
  703. b1=b1-4*a;
  704. a*=2;
  705. }
  706. for (int i=0; i<a; i++) {
  707. y[2*i]=(d[i]+w[i])*sqrt05;
  708. y[2*i+1]=(d[i]-w[i])*sqrt05;
  709. }
  710. for (int i=0; i<n; i++) y[i] *= scalingfactorout;
  711. }
  712. void wtrafo(float[] y, int n) {
  713. float[] d = new float[n];
  714. int a = n/2;
  715. for (int i=0; i<a; i++) {
  716. w[i] = (y[2*i]-y[2*i+1])*sqrt05;
  717. d[i] = (y[2*i]+y[2*i+1])*sqrt05;
  718. }
  719. int b1 = 0;
  720. int b2 = a;
  721. a/=2;
  722. while (a>0) {
  723. for (int i=0; i<a; i++) {
  724. w[i+b2]=(d[2*i+b1]-d[2*i+1+b1])*sqrt05;
  725. d[i+b2]=(d[2*i+b1]+d[2*i+1+b1])*sqrt05;
  726. }
  727. b1=b2;
  728. b2=b2+a;
  729. a/=2;
  730. }
  731. w[b2] = d[b1];
  732. for (int i=0; i<n-1; i++) w[i] = (int)(w[i]/scalingfactorin);
  733. if (w[n-1]>0) w[n-1] = (int)(w[n-1]/scalingfactorin+0.5);
  734. else w[n-1] = (int)(w[n-1]/scalingfactorin-0.5);
  735. }
  736. }
  737. /*
  738. AUECHO
  739. */
  740. class AUECHO extends Shader {
  741. final int[] blends = {BLEND, ADD, SUBTRACT, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION, MULTIPLY, SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN};
  742. AUECHO() {
  743. name = "fxAUecho";
  744. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  745. params.add(new Param ("echo", FLOATVAL, 0.001, 1, new int[]{TRIANG, SINE, RAMPUPDOWN, }));
  746. params.add(new Param ("decay", FLOATVAL, 0.001, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  747. params.add(new Param ("blend mode", INTVAL, 0, this.blends.length-1, new int[]{RANDOM }));
  748. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  749. directionParamIndex = 4;
  750. }
  751. void apply() {
  752. canvas.beginDraw();
  753. if (boolean((int)params.get(0).value)) {
  754. canvas.colorMode(HSB);
  755. colorMode(HSB);
  756. } else {
  757. canvas.colorMode(RGB);
  758. colorMode(RGB);
  759. }
  760. canvas.loadPixels();
  761. float _delay = params.get(1).value;
  762. float decay = params.get(2).value;
  763. int delay = (int)(canvas.pixels.length * _delay);
  764. color[] history = new color[canvas.pixels.length];
  765. int blendMode =this.blends[(int)params.get(3).value];
  766. for ( int i = 0, l = canvas.pixels.length; i<l; i++) {
  767. history[i] = canvas.pixels[i];
  768. }
  769. for ( int i = 0, l = canvas.pixels.length; i<l; i++) {
  770. int fromPos = i-delay < 0 ? l-abs(i-delay) : i-delay;
  771. color fromColor = history[fromPos];
  772. float r = red(fromColor) * decay;
  773. float g = green(fromColor) * decay;
  774. float b = blue(fromColor) * decay;
  775. color origColor = history[i];
  776. color toColor = color(
  777. r = r + red(origColor) > 255 ? r + red(origColor) - 255 : r + red(origColor), // simulate overflow ;)
  778. g = g + green(origColor) > 255 ? g + green(origColor) - 255 : g + green(origColor),
  779. b = b + blue(origColor) > 255 ? b + blue(origColor) - 255 : b + blue(origColor) );
  780. //canvas.pixels[i] = history[i] = toColor;
  781. canvas.pixels[i] = history[i] = blendColor(origColor, toColor, blendMode);
  782. }
  783. canvas.updatePixels();
  784. if (boolean((int)params.get(0).value)) {
  785. canvas.colorMode(RGB);
  786. colorMode(RGB);
  787. }
  788. canvas.endDraw();
  789. }
  790. }
  791. /*
  792. SLITSCAN
  793. */
  794. class SLITSCAN extends Shader {
  795. int[] fx;
  796. int[] fy;
  797. float[] phx;
  798. float[] phy;
  799. int[] sx, sy;
  800. boolean[] skipfx;
  801. boolean[] skipfy;
  802. boolean dox, doy;
  803. PImage buffer;
  804. float[][] ft = new float[2][32];
  805. //int depth; // number of octaves
  806. int fxnum;
  807. int fynum;
  808. SLITSCAN() {
  809. name = "fxSlitSscan";
  810. buffer = createImage(canvas.width, canvas.height, ARGB);
  811. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  812. directionParamIndex = 0;
  813. int s = (int)(log(min(buffer.width, buffer.height))/log(2));
  814. olds = s;
  815. params.add(new Param("x num", INTVAL, 1, s, new int[]{RANDOM}));
  816. params.add(new Param("y num", INTVAL, 1, s, new int[]{RANDOM}));
  817. for (int i=0; i<32; i++) {
  818. ft[0][i] = pow(2.0, i);
  819. ft[1][i] = 0.5*1.0/ft[0][i];
  820. }
  821. }
  822. int olds, oldfxnum, oldfynum;
  823. void apply() {
  824. canvas.beginDraw();
  825. canvas.colorMode(RGB);
  826. canvas.noStroke();
  827. colorMode(RGB);
  828. canvas.fill(255);
  829. buffer.resize(canvas.width, canvas.height);
  830. buffer = canvas.get(0, 0, canvas.width, canvas.height);
  831. //int s = buffer.width>buffer.height?buffer.height:buffer.width;
  832. //int s = min(buffer.width, buffer.height);
  833. //depth = (int)(log(s)/log(2));
  834. int s = (int)(log(min(buffer.width, buffer.height))/log(2));
  835. if (olds != s) {
  836. olds = s;
  837. changeParam(1, new Param("x num", INTVAL, 1, s, new int[]{RANDOM}));
  838. changeParam(2, new Param("y num", INTVAL, 1, s, new int[]{RANDOM}));
  839. }
  840. //depth = (int)params.get(1).value;
  841. //println(depth);
  842. fxnum = (int)random(params.get(1).value);
  843. fynum = (int)random(params.get(2).value);
  844. fx = new int[fxnum+1];
  845. fy = new int[fynum+1];
  846. sx = new int[fxnum+1];
  847. sy = new int[fynum+1];
  848. phx = new float[fxnum+1];
  849. phy = new float[fynum+1];
  850. skipfx = new boolean[fxnum+1];
  851. skipfy = new boolean[fynum+1];
  852. for (int i=0; i<fxnum; i++) {
  853. fx[i]=(int)random(6);
  854. phx[i] = random(1);
  855. skipfx[i] = random(1)<0.2;
  856. sx[i] = random(1)<0.2?-1:1;
  857. }
  858. for (int i=0; i<fynum; i++) {
  859. fy[i]=(int)random(6);
  860. phy[i] = random(1);
  861. skipfy[i] = random(1)<0.2;
  862. sy[i] = random(1)<0.2?-1:1;
  863. }
  864. //dox = random(1)<0.8;
  865. //doy = dox?random(1)<0.8:true;
  866. dox = true;
  867. doy = true;
  868. float v=0;
  869. for (int y=0; y<buffer.height; y++)
  870. for (int x=0; x<buffer.width; x++) {
  871. float iy = map(y, 0, buffer.height, 0, 1);
  872. v=0;
  873. if (doy) for (int i=0; i<fy.length; i++)
  874. if (!skipfy[i]) v+=sy[i]*getValue(fy[i], iy, i, phy[i]);
  875. float ry = 2*iy+v;
  876. float y2 = (3*buffer.height+ry * buffer.height/2)%buffer.height;
  877. float ix = map(x, 0, buffer.width, 0, 1);
  878. v=0;
  879. if (dox) for (int i=0; i<fx.length; i++)
  880. if (!skipfx[i]) v+=sx[i]*getValue(fx[i], ix, i, phx[i]);
  881. float rx = 2*ix+v;
  882. float x2 = (3*buffer.width+rx * buffer.width/2)%buffer.width;
  883. canvas.fill(buffer.get((int)x2, (int)y2));
  884. canvas.rect(x, y, 1, 1);
  885. }
  886. canvas.endDraw();
  887. }
  888. float getValue(int fun, float idx, int freq, float phase) {
  889. switch(fun) {
  890. case 0:
  891. return getSin(idx, freq, phase);
  892. case 1:
  893. return getSaw(idx, freq, phase);
  894. case 2:
  895. return getTriangle(idx, freq, phase);
  896. case 3:
  897. return getCutTriangle(idx, freq, phase);
  898. case 4:
  899. return getSquare(idx, freq, phase);
  900. case 5:
  901. return getNoise(idx, freq, phase);
  902. default:
  903. return getSin(idx, freq, phase);
  904. }
  905. }
  906. float getNoise(float idx, int freq, float phase) {
  907. return 2*ft[1][freq]*(noise((idx+phase)*ft[0][freq])-0.5);
  908. }
  909. float getSin(float idx, int freq, float phase) {
  910. float p = ft[0][freq];
  911. return ft[1][freq] * sin(idx*TWO_PI*p+phase*TWO_PI);
  912. }
  913. float getSaw(float idx, int freq, float phase) {
  914. float p = ft[0][freq];
  915. float rp = 2.0*ft[1][freq];
  916. float p2 = p*((idx+phase+ft[1][freq])%1.0);
  917. return rp*(p2-floor(p2)-0.5);
  918. }
  919. float getSquare(float idx, int freq, float phase) {
  920. float p = ft[0][freq];
  921. float rp = ft[1][freq];
  922. return (((idx*p)+phase)%1.0)<0.5?rp:-rp;
  923. }
  924. float getTriangle(float idx, int freq, float phase) {
  925. return 2*abs(getSaw(idx, freq, phase+0.5*ft[1][freq]))-ft[1][freq];
  926. }
  927. float getCutTriangle(float idx, int freq, float phase) {
  928. return constrain(getTriangle(idx, freq, phase), -ft[1][freq+1], ft[1][freq+1]);
  929. }
  930. }
  931. /*
  932. WAHWAH
  933. */
  934. class WAHWAH extends Shader {
  935. float sequence, lfoskip, xn1, xn2, yn1, yn2, b0, b1, b2, a0, a1, a2, freqofs, freq, freqoff, startsequence, res, depth;
  936. float mCurRate = 0.4, skipcount = 0;
  937. int lfoskipsamples = 0;
  938. float frequency, omega, sn, cs, alpha;
  939. float in, out;
  940. float val;
  941. WAHWAH() {
  942. name = "fxWahWah";
  943. //params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  944. params.add(new Param ("resolution", FLOATVAL, 1, 100, new int[]{TRIANG, SINE, RAMPUPDOWN, }));
  945. params.add(new Param ("depth", FLOATVAL, 0.0001, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  946. params.add(new Param ("frequency offset", FLOATVAL, 0, 0.9, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  947. params.add(new Param ("mCurRate", FLOATVAL, 0, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  948. params.add(new Param ("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  949. directionParamIndex = 4;
  950. // params.add(new Param ("blend mode", INTVAL, 0, this.blends.length-1, new int[]{RANDOM }));
  951. }
  952. void apply() {
  953. res = params.get(0).value;
  954. depth = params.get(1).value;
  955. freqofs = params.get(2).value;
  956. //res = 12.5
  957. //depth = 0.8;
  958. //freqofs = 0.9;
  959. freq = 1.5;
  960. startsequence = 0.2;
  961. lfoskip = freq * 2 * PI / mCurRate;
  962. skipcount = xn1 = xn2 = yn1 = yn2 = b0 = b1 = b2 = a0 = a1 = a2 = 0;
  963. sequence = startsequence;
  964. canvas.beginDraw();
  965. canvas.colorMode(RGB);
  966. canvas.loadPixels();
  967. float[] rgb = new float[3];
  968. for ( int i = 0, len = canvas.pixels.length; i < len; i++) {
  969. rgb[0] = red(canvas.pixels[i]);
  970. rgb[1] = green(canvas.pixels[i]);
  971. rgb[2] = blue(canvas.pixels[i]);
  972. for ( int ri = 0; ri < 3; ri++ ) {
  973. in = map(rgb[ri], 0, 255, 0, 1);
  974. frequency = (1+cos(skipcount * lfoskip + sequence ))/2;
  975. frequency = frequency * depth * (1-freqofs) + freqofs;
  976. frequency = exp((frequency - 1) * 6 );
  977. omega = PI * frequency;
  978. sn = sin(omega);
  979. cs = cos(omega);
  980. alpha = sn/(2*res);
  981. b0 = (1-cs) /2;
  982. b1 = 1 - cs;
  983. b2 = (1-cs)/2;
  984. a0 = 1 + alpha;
  985. a1 = -2 * cs;
  986. a2 = 1 - alpha;
  987. out = ( b0 * in + b1 * xn1 + b2 * xn2 - a1 * yn1 - a2 * yn2 ) / a0;
  988. xn2 = xn1;
  989. xn1 = in;
  990. yn2 = yn1;
  991. yn1 = out;
  992. rgb[ri] = map(out, 0, 1, 0, 255);
  993. }
  994. canvas.pixels[i] = color(rgb[0], rgb[1], rgb[2]);
  995. }
  996. canvas.updatePixels();
  997. canvas.endDraw();
  998. }
  999. }
  1000. /*
  1001. PHASER
  1002. */
  1003. class PHASER extends Shader {
  1004. //float samplerate = 92230.0; // try setting this to 44100.0 or 2048.5 for kicks
  1005. float samplerate = 44100; // try setting this to 44100.0 or 2048.5 for kicks
  1006. int mode;
  1007. PHASER() {
  1008. name ="fxPhaser";
  1009. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  1010. params.add(new Param ("frequency", FLOATVAL, 0.1, 40.0, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1011. params.add(new Param ("depth", INTVAL, 1, 255, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1012. params.add(new Param ("feedback", INTVAL, -100, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1013. params.add(new Param ("phase", FLOATVAL, 0, TWO_PI, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1014. params.add(new Param ("stages", INTVAL, 1, 24, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1015. params.add(new Param ("sample rate", FLOATVAL, 512, 92230, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1016. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1017. directionParamIndex = 7;
  1018. //params.add(new Param ("frequency offset", FLOATVAL, 0, 1, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1019. }
  1020. /*
  1021. mDepth = (int) map(knobZero, 0, 255, 255, 0);
  1022. mFeedback = (int) map(knobOne, 0, 255, -100, 100);
  1023. // enable these for some more fun :)
  1024. if (mode == 1) {
  1025. mSampleRate = map(knobTwo, 0, 255, 1, 512);
  1026. mStages = (int) ( 2*map(knobThree, 0, 255, 1, 12));
  1027. }
  1028. */
  1029. void apply() {
  1030. mode = (int)params.get(0).value;
  1031. float mFreq = params.get(1).value;
  1032. int mDryWet = 255;
  1033. int mDepth = (int)params.get(2).value;
  1034. int mFeedback = (int)params.get(3).value;
  1035. float mPhase = params.get(4).value;
  1036. //these two are only changed if mode = 1
  1037. int mStages = 2;
  1038. float mSampleRate = samplerate;
  1039. canvas.beginDraw();
  1040. canvas.loadPixels();
  1041. //constants
  1042. float phaserlfoshape = 4.0;
  1043. int lfoskipsamples = 20; //how many samples are processed before recomputing lfo
  1044. int numStages = 24;
  1045. //getParams
  1046. /*
  1047. Phaser Parameters
  1048. mFreq - Phaser's LFO frequency
  1049. mPhase - Phaser's LFO startsequence (radians), needed for stereo Phasers
  1050. mDepth - Phaser depth (0 - no depth, 255 - max depth)
  1051. mStages - Phaser stages (recomanded from 2 to 16-24, and EVEN NUMBER)
  1052. mDryWet - Dry/wet mix, (0 - dry, 128 - dry=wet, 255 - wet)
  1053. mFeedback - Phaser FeedBack (0 - no feedback, 100 = 100% Feedback,
  1054. -100 = -100% FeedBack)
  1055. */
  1056. // enable these for some more fun :)
  1057. if (mode == 1) {
  1058. mStages = (int)params.get(5).value;
  1059. mSampleRate = params.get(6).value;
  1060. }
  1061. //init
  1062. float gain = 0, fbout = 0;
  1063. float lfoskip = mFreq * 2 * PI / mSampleRate;
  1064. float sequence = mPhase * PI / 180;
  1065. float[] old = new float[mStages];
  1066. for ( int j = 0; j < mStages; j++) {
  1067. old[j] = 0.0;
  1068. }
  1069. /* EffectPhaser::ProcessBlock */
  1070. int skipcount = 0;
  1071. float[] rgb = new float[3];
  1072. for ( int i = 0, l = canvas.pixels.length; i<l; i++ ) {
  1073. color c = canvas.pixels[i];
  1074. rgb[0] = map(red(c), 0, 255, 0, 1);
  1075. rgb[1] = map(green(c), 0, 255, 0, 1);
  1076. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1077. for ( int ci = 0; ci < 3; ci++) {
  1078. float in = rgb[ci];
  1079. float m = in + fbout * mFeedback / 100;
  1080. if ( (( skipcount++) % lfoskipsamples ) == 0 ) { //recomopute lfo
  1081. gain = (1.0 + cos(skipcount * lfoskip + sequence)) / 2.0; //compute sine between 0 and 1
  1082. gain = exp(gain * phaserlfoshape) / exp(phaserlfoshape); // change lfo shape
  1083. gain = 1.0 - gain / 255.0 * mDepth; // attenuate the lfo
  1084. }
  1085. //phasing routine
  1086. for ( int j = 0; j<mStages; j++) {
  1087. float tmp = old[j];
  1088. old[j] = gain * tmp + m;
  1089. m = tmp - gain * old[j];
  1090. }
  1091. fbout = m;
  1092. rgb[ci] = (float) (( m * mDryWet + in * (255-mDryWet)) / 255);
  1093. }
  1094. color rc = color(
  1095. map(rgb[0], 0, 1, 0, 255),
  1096. map(rgb[1], 0, 1, 0, 255),
  1097. map(rgb[2], 0, 1, 0, 255));
  1098. canvas.pixels[i] = rc;
  1099. }
  1100. canvas.updatePixels();
  1101. canvas.endDraw();
  1102. }
  1103. }
  1104. /*
  1105. ECHO
  1106. */
  1107. class ECHO extends Shader {
  1108. int mode = 0;
  1109. PImage result;
  1110. ECHO() {
  1111. name = "fxEcho";
  1112. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  1113. params.add(new Param ("xp", INTVAL, 0, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1114. params.add(new Param ("yp", INTVAL, 0, 100, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1115. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1116. directionParamIndex = 3;
  1117. result = createImage(canvas.width, canvas.height, RGB);
  1118. }
  1119. void apply() {
  1120. mode = (int)params.get(0).value;
  1121. int xp = (int)params.get(1).value;
  1122. int yp = (int)params.get(2).value;
  1123. canvas.beginDraw();
  1124. canvas.colorMode(RGB);
  1125. colorMode(RGB);
  1126. canvas.imageMode(CORNER);
  1127. if (mode == 0) {
  1128. canvas.image(auEcho(canvas, xp, yp), 0, 0);
  1129. } else if (mode == 1) {
  1130. canvas.image(auEchoWTF(canvas, xp, yp), 0, 0);
  1131. }
  1132. canvas.endDraw();
  1133. }
  1134. PImage auEcho(PImage img, int xp, int yp) {
  1135. result.resize(img.width, img.height);
  1136. float _delay = map(xp, 0, 100, 0.001, 1.0);
  1137. float decay = map(yp, 0, 100, 0.0, 1.0);
  1138. int delay = (int)(img.pixels.length * _delay);
  1139. color[] history = new color[img.pixels.length];
  1140. int blendMode = BLEND;
  1141. img.loadPixels();
  1142. result.loadPixels();
  1143. for ( int i = 0, l = img.pixels.length; i<l; i++) {
  1144. history[i] = img.pixels[i];
  1145. }
  1146. for ( int i = 0, l = img.pixels.length; i<l; i++) {
  1147. int fromPos = i-delay < 0 ? l-abs(i-delay) : i-delay;
  1148. color fromColor = history[fromPos];
  1149. float r = red(fromColor) * decay;
  1150. float g = green(fromColor) * decay;
  1151. float b = blue(fromColor) * decay;
  1152. color origColor = history[i];
  1153. color toColor = color(
  1154. r + red(origColor),
  1155. g + green(origColor),
  1156. b + blue(origColor) );
  1157. result.pixels[i] = history[i] = blendColor(origColor, toColor, blendMode);
  1158. }
  1159. return result;
  1160. }
  1161. PImage auEchoWTF(PImage img, int xp, int yp) {
  1162. result.resize(img.width, img.height);
  1163. float delay = map(xp, 0, 100, 0.001, 5);
  1164. float decay = map(yp, 0, 100, 0.0, 5.0);
  1165. int histPos = 0;
  1166. int histLen = img.pixels.length*3;
  1167. float[] history = new float[histLen*3];
  1168. img.loadPixels();
  1169. result.loadPixels();
  1170. float ibuf = 0.0, obuf = 0.0;
  1171. float[] rgb = new float[3];
  1172. for ( int i = 0, l = img.pixels.length; i<l; i++, histPos++) {
  1173. color c = img.pixels[i];
  1174. rgb[0] = map(red(c), 0, 255, 0, 1);
  1175. rgb[1] = map(green(c), 0, 255, 0, 1);
  1176. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1177. history[i] = rgb[0];
  1178. history[i+1] = rgb[1];
  1179. history[i+2] = rgb[2];
  1180. }
  1181. for ( int i = 0, l = img.pixels.length; i<l; i++, histPos++) {
  1182. color c = img.pixels[i];
  1183. rgb[0] = map(red(c), 0, 255, 0, 1);
  1184. rgb[1] = map(green(c), 0, 255, 0, 1);
  1185. rgb[2] = map(blue(c), 0, 255, 0, 1);
  1186. if ( histPos == histLen ) histPos = 0;
  1187. for ( int ri = 0; ri < 3; ri++ ) {
  1188. history[histPos+ri] = rgb[ri] = rgb[ri] + history[histPos+ri] * decay;
  1189. }
  1190. color out = color(
  1191. (int)map(rgb[0], 0, 1, 0, 255),
  1192. (int)map(rgb[1], 0, 1, 0, 255),
  1193. (int)map(rgb[2], 0, 1, 0, 255));
  1194. result.pixels[i] = out;
  1195. }
  1196. return result;
  1197. }
  1198. }
  1199. /*
  1200. DARKER
  1201. */
  1202. class DARKER extends Shader {
  1203. float thresh = 127;
  1204. float darken = 150;
  1205. int mode = 1;
  1206. int bangCount = 0;
  1207. DARKER() {
  1208. name = "fxDarker";
  1209. params.add(new Param("count", INTVAL, 1, 4, new int[]{TRIANG, SINE, RAMPUPDOWN, TAN, TANINVERSE}));
  1210. params.add(new Param("threshold", INTVAL, 60, 180, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1211. params.add(new Param("darken", INTVAL, 140, 220, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1212. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1213. //thresh = int(random(60, 180));
  1214. //darken = int(random(140, 220));
  1215. }
  1216. void apply() {
  1217. // bright = knobZero;
  1218. int count = int(params.get(0).value);
  1219. darken = int(params.get(1).value);
  1220. thresh = int(params.get(2).value);
  1221. mode = int(params.get(3).value);
  1222. canvas.beginDraw();
  1223. canvas.colorMode(HSB);
  1224. colorMode(HSB);
  1225. canvas.loadPixels();
  1226. if (mode == 0) {
  1227. for (int h = 1; h < count+1; h++) {
  1228. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1229. float hue = hue(canvas.pixels[i]);
  1230. float sat = saturation(canvas.pixels[i]);
  1231. float bright = brightness(canvas.pixels[i]);
  1232. if (bright > thresh/h) {
  1233. bright -= darken/h;
  1234. constrain(bright, 0, 255);
  1235. }
  1236. color c = color(hue, sat, bright);
  1237. canvas.pixels[i] = c;
  1238. }
  1239. }
  1240. } else if (mode == 1) {
  1241. for (int h = 1; h < count+1; h++) {
  1242. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1243. float hue = hue(canvas.pixels[i]);
  1244. float sat = saturation(canvas.pixels[i]);
  1245. float bright = brightness(canvas.pixels[i]);
  1246. if (bright < thresh/h) {
  1247. bright -= darken/h;
  1248. constrain(bright, 0, 255);
  1249. }
  1250. color c = color(hue, sat, bright);
  1251. canvas.pixels[i] = c;
  1252. }
  1253. }
  1254. }
  1255. canvas.updatePixels();
  1256. canvas.endDraw();
  1257. colorMode(RGB);
  1258. }
  1259. }
  1260. /*
  1261. BRIGHTER
  1262. */
  1263. class BRIGHTER extends Shader {
  1264. float thresh = 120;
  1265. float thresh2 = 150;
  1266. float brighten = 180;
  1267. float speed;
  1268. BRIGHTER() {
  1269. name = "fxBrighter";
  1270. params.add(new Param("threshold", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1271. params.add(new Param("threshold 2", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1272. params.add(new Param("brighten", INTVAL, 0, 255, new int[]{TRIANG, SINE, RAMPUPDOWN }));
  1273. }
  1274. void apply() {
  1275. brighten = int(params.get(2).value);
  1276. thresh2 = int(params.get(1).value);
  1277. thresh = int(params.get(0).value);
  1278. canvas.beginDraw();
  1279. canvas.colorMode(HSB);
  1280. colorMode(HSB);
  1281. canvas.loadPixels();
  1282. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1283. float hue = hue(canvas.pixels[i]);
  1284. float sat = saturation(canvas.pixels[i]);
  1285. float bright = brightness(canvas.pixels[i]);
  1286. if (bright < thresh && bright > thresh2) {
  1287. bright += brighten;
  1288. constrain(bright, 0, 255);
  1289. }
  1290. color c = color(hue, sat, bright);
  1291. canvas.pixels[i] = c;
  1292. }
  1293. canvas.colorMode(RGB);
  1294. canvas.updatePixels();
  1295. canvas.endDraw();
  1296. colorMode(RGB);
  1297. }
  1298. }
  1299. /*
  1300. AMPLIFY
  1301. */
  1302. //13
  1303. class AMPLIFY extends Shader {
  1304. int spawnT;
  1305. float h, s, b;
  1306. AMPLIFY() {
  1307. name = "fxAmplify";
  1308. params.add(new Param("hue / red", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1309. params.add(new Param("saturation / green", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1310. params.add(new Param("brightness / blue", FLOATVAL, -255, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1311. params.add(new Param("RGB / HSB", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1312. }
  1313. void apply() {
  1314. canvas.beginDraw();
  1315. if (params.get(3).value > 0) colorMode(HSB);
  1316. else {
  1317. colorMode(RGB);
  1318. }
  1319. canvas.loadPixels();
  1320. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1321. float h = hue(canvas.pixels[i]);
  1322. float s = saturation(canvas.pixels[i]);
  1323. float b = brightness(canvas.pixels[i]);
  1324. canvas.pixels[i] = color(h+params.get(0).value, s+params.get(1).value, b+params.get(2).value);
  1325. }
  1326. canvas.updatePixels();
  1327. canvas.endDraw();
  1328. colorMode(RGB);
  1329. }
  1330. }
  1331. /*
  1332. BROKENCOLORROT
  1333. */
  1334. class BROKENCOLORROT extends Shader {
  1335. int spawnT;
  1336. float h, s, b;
  1337. BROKENCOLORROT() {
  1338. name = "fxBrokenColorRot";
  1339. params.add(new Param("hue", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1340. params.add(new Param("saturation", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1341. params.add(new Param("brightness", INTVAL, 0, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1342. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1343. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1344. directionParamIndex = 4;
  1345. }
  1346. void apply() {
  1347. canvas.beginDraw();
  1348. if (params.get(3).value > 0) colorMode(HSB);
  1349. else {
  1350. colorMode(RGB);
  1351. }
  1352. canvas.loadPixels();
  1353. float h = params.get(0).value;
  1354. float s = params.get(1).value;
  1355. float b = params.get(2).value;
  1356. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1357. h = hue(canvas.pixels[i])+h;
  1358. if (h > 255) h -= 255;
  1359. s = saturation(canvas.pixels[i])+s;
  1360. if (s > 255) s -= 255;
  1361. b = brightness(canvas.pixels[i])+b;
  1362. if (b > 255) b -= 255;
  1363. canvas.pixels[i] = color(h, s, b);
  1364. }
  1365. canvas.updatePixels();
  1366. canvas.endDraw();
  1367. colorMode(RGB);
  1368. }
  1369. }
  1370. /*
  1371. POSTERIZE
  1372. */
  1373. class POSTER extends Shader {
  1374. POSTER() {
  1375. name = "fxPosterize";
  1376. params.add(new Param("levels", INTVAL, 2, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1377. }
  1378. void apply() {
  1379. canvas.beginDraw();
  1380. canvas.filter(POSTERIZE, (int)params.get(0).value);
  1381. canvas.endDraw();
  1382. }
  1383. }
  1384. /*
  1385. DUAL
  1386. */
  1387. class DUAL extends Shader {
  1388. PImage buffer;
  1389. int dualColor;
  1390. int dirx = 1;
  1391. int diry = 1;
  1392. DUAL() {
  1393. name = "fxDual";
  1394. params.add(new Param("dual color", INTVAL, 2000000, 15000000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1395. params.add(new Param("flip direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1396. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  1397. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1398. directionParamIndex = 3;
  1399. buffer = createImage(canvas.width, canvas.height, ARGB);
  1400. }
  1401. void apply() {
  1402. switch((int)params.get(1).value) {
  1403. case(0):
  1404. dirx = 1;
  1405. diry = 1;
  1406. break;
  1407. case(1):
  1408. dirx = -1;
  1409. diry = 1;
  1410. break;
  1411. case(2):
  1412. dirx = 1;
  1413. diry = -1;
  1414. break;
  1415. case(3):
  1416. dirx = -1;
  1417. diry = -1;
  1418. break;
  1419. }
  1420. dualColor = (int)params.get(0).value;
  1421. buffer.resize(canvas.width, canvas.height);
  1422. canvas.beginDraw();
  1423. canvas.imageMode(CORNER);
  1424. canvas.loadPixels();
  1425. buffer.loadPixels();
  1426. if ((int)params.get(2).value > 0) {
  1427. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1428. buffer.pixels[i] = canvas.pixels[i]+dualColor;
  1429. }
  1430. } else {
  1431. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1432. buffer.pixels[i] = canvas.pixels[i]+dualColor;
  1433. buffer.pixels[i] = buffer.pixels[i]+i/10;
  1434. }
  1435. }
  1436. buffer.updatePixels();
  1437. canvas.updatePixels();
  1438. canvas.pushMatrix();
  1439. canvas.scale(dirx, diry);
  1440. canvas.image(buffer, 0, 0, dirx * canvas.width, diry * canvas.height);
  1441. canvas.popMatrix();
  1442. canvas.endDraw();
  1443. }
  1444. }
  1445. /*
  1446. GRAUZONE
  1447. */
  1448. class GRAUZONE extends Shader {
  1449. int nFrames = 20;
  1450. int iWrite = 0, iRead = 1;
  1451. PImage[] buffer;
  1452. PGraphics grauz;
  1453. GRAUZONE() {
  1454. name = "fxGrauzone";
  1455. params.add(new Param("delay (in frames)", INTVAL, 3, 100, new int[]{RANDOM}));
  1456. nFrames = (int)params.get(0).value;
  1457. buffer = new PImage[nFrames];
  1458. }
  1459. int nFramesPrev;
  1460. void apply() {
  1461. nFramesPrev = nFrames;
  1462. if (nFramesPrev != (int)params.get(0).value) {
  1463. iWrite = 0;
  1464. iRead = 1;
  1465. int nFramesOld = nFrames;
  1466. nFrames = (int)params.get(0).value;
  1467. if (nFrames > nFramesOld) {
  1468. buffer = new PImage[nFrames];
  1469. }
  1470. }
  1471. buffer[iWrite] = canvas.get();
  1472. grauz = createGraphics(canvas.width, canvas.height);
  1473. grauz.beginDraw();
  1474. // grauz.resize(canvas.width, canvas.height);
  1475. buffer[iWrite] = canvas.get();
  1476. if (buffer[iRead] != null) {
  1477. grauz.tint(255, 127);
  1478. buffer[iRead].filter(INVERT);
  1479. grauz.image(buffer[iRead], 0, 0, canvas.width, canvas.height);
  1480. grauz.tint(255, 255);
  1481. }
  1482. grauz.endDraw();
  1483. canvas.beginDraw();
  1484. canvas.imageMode(CORNER);
  1485. canvas.image(grauz, 0, 0, canvas.width, canvas.height);
  1486. canvas.endDraw();
  1487. iWrite++;
  1488. iRead++;
  1489. if (iRead >= nFrames-1) {
  1490. iRead = 0;
  1491. }
  1492. if (iWrite >= nFrames-1) {
  1493. iWrite = 0;
  1494. }
  1495. }
  1496. }
  1497. /*
  1498. COPYZOOM
  1499. */
  1500. class COPYZOOM extends Shader {
  1501. int coprx, copry, coprw, coprh;
  1502. PImage buffer;
  1503. COPYZOOM() {
  1504. name = "fxCopyZoom";
  1505. params.add(new Param("w", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1506. params.add(new Param("h", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1507. params.add(new Param("x", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1508. params.add(new Param("y", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1509. buffer = createImage(canvas.width, canvas.height, ARGB);
  1510. }
  1511. void apply() {
  1512. canvas.imageMode(CORNER);
  1513. if (buffer.width != canvas.width || buffer.height != canvas.height) buffer.resize(canvas.width, canvas.height);
  1514. buffer = canvas.get();
  1515. coprw = int(map(params.get(0).value, 0, 1, 1, canvas.width));
  1516. coprh = int(map(params.get(1).value, 0, 1, 1, canvas.height));
  1517. coprx = int(map(params.get(2).value, 0, 1, 0, canvas.width-coprw));
  1518. copry = int(map(params.get(3).value, 0, 1, 0, canvas.height-coprh));
  1519. canvas.beginDraw();
  1520. buffer.copy(coprx, copry, coprw, coprh, 0, 0, canvas.width, canvas.height);
  1521. canvas.image(buffer, 0, 0, canvas.width, canvas.height);
  1522. canvas.endDraw();
  1523. }
  1524. }
  1525. /*
  1526. SUBTLESORT
  1527. */
  1528. class SUBTLESORT extends Shader {
  1529. int direction = 0;
  1530. int mode = 0;
  1531. int c;
  1532. //int count = int(random(777));
  1533. color col;
  1534. final static int RED = 0;
  1535. final static int GREEN = 1;
  1536. final static int BLUE = 2;
  1537. final static int HUE = 3;
  1538. final static int SATURATION = 4;
  1539. final static int BRIGHTNESS = 5;
  1540. final static int NRED = 6;
  1541. final static int NGREEN = 7;
  1542. final static int NBLUE = 8;
  1543. final static int NHUE = 9;
  1544. final static int NSATURATION = 10;
  1545. final static int NBRIGHTNESS = 11;
  1546. // channels for depth: RED, GREEN, BLUE, HUE, SATURATION, BRIGHTNESS, NRED, NGREEN, NBLUE, NHUE, NSATURATION, NBRIGHTNESS.
  1547. int channel = BRIGHTNESS;
  1548. // channel weight.
  1549. float channel_weight = .2;
  1550. //
  1551. SUBTLESORT() {
  1552. name ="fxSubtleSort";
  1553. params.add(new Param ("channel weight", FLOATVAL, 0.001, 20, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1554. params.add(new Param ("channel", INTVAL, 0, 6, new int[]{RANDOM}));
  1555. //params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1556. params.add(new Param("mode", INTVAL, 0, 1, new int[]{SQUARE, RANDOM}));
  1557. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1558. directionParamIndex = 3;
  1559. }
  1560. void apply() {
  1561. channel_weight = map(renderSize, 4, 15000, 0, 1.5)*params.get(0).value;
  1562. channel = (int)params.get(1).value;
  1563. direction = (int)params.get(2).value;
  1564. mode = (int)params.get(3).value;
  1565. canvas.beginDraw();
  1566. canvas.noStroke();
  1567. //if (direction == 0) {
  1568. for (int i = 0; i < canvas.width; i++) {
  1569. for (int j = 0; j < canvas.height; j++) {
  1570. c = i+(j*canvas.width);
  1571. col = canvas.pixels[c];
  1572. canvas.fill(col);
  1573. canvas.rect(i, j+(getChannel(col)), 1, getChannel(col));
  1574. }
  1575. }
  1576. //}
  1577. /*else if (direction == 1) {
  1578. for (int i = 0; i < canvas.width; i++) {
  1579. for (int j = 0; j < canvas.height; j++) {
  1580. c = i+(j*canvas.width);
  1581. col = canvas.pixels[c];
  1582. canvas.fill(col);
  1583. canvas.rect(i, j-(getChannel(col)*mode), 1, -getChannel(col));
  1584. }
  1585. }
  1586. } else if (direction == 2) {
  1587. for (int i = 0; i < canvas.width; i++) {
  1588. for (int j = 0; j < canvas.height; j++) {
  1589. c = i+(j*canvas.width);
  1590. col = canvas.pixels[c];
  1591. canvas.fill(col);
  1592. canvas.rect(i-(getChannel(col)*mode), j, -getChannel(col), 1);
  1593. }
  1594. }
  1595. } else if (direction == 3) {
  1596. for (int i = 0; i < canvas.width; i++) {
  1597. for (int j = 0; j < canvas.height; j++) {
  1598. c = i+(j*canvas.width);
  1599. col = canvas.pixels[c];
  1600. canvas.fill(col);
  1601. canvas.rect(i+(getChannel(col)), j, getChannel(col), 1);
  1602. }
  1603. }
  1604. }*/
  1605. canvas.endDraw();
  1606. }
  1607. float getChannel(color c) {
  1608. int ch = channel;
  1609. float cc;
  1610. switch(ch) {
  1611. case RED:
  1612. cc = red(c);
  1613. break;
  1614. case GREEN:
  1615. cc = green(c);
  1616. break;
  1617. case BLUE:
  1618. cc = blue(c);
  1619. break;
  1620. case HUE:
  1621. cc = hue(c);
  1622. break;
  1623. case SATURATION:
  1624. cc = saturation(c);
  1625. break;
  1626. default:
  1627. cc= brightness(c);
  1628. break;
  1629. }
  1630. return channel_weight * (channel>5?255-cc:cc);
  1631. }
  1632. }
  1633. /*
  1634. SCANKER
  1635. */
  1636. class SCANKER extends Shader {
  1637. int mode;
  1638. SCANKER() {
  1639. name = "fxScanker";
  1640. params.add(new Param ("detail level 1", FLOATVAL, 0.001, 1000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1641. params.add(new Param ("detail level 2", FLOATVAL, -50, 50, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1642. params.add(new Param ("detail level 3", FLOATVAL, -5, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1643. params.add(new Param ("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  1644. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  1645. directionParamIndex = 4;
  1646. }
  1647. void apply() {
  1648. mode = (int)params.get(3).value;
  1649. canvas.beginDraw();
  1650. canvas.loadPixels();
  1651. float factor = params.get(0).value + params.get(1).value + params.get(2).value;
  1652. if (mode == 0) {
  1653. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1654. // canvas.pixels[i] = canvas.pixels[i]+((i/1000)*scankMulti);
  1655. canvas.pixels[i] = canvas.pixels[i]-int(map(i, 0, canvas.width*canvas.height, 0, source.width*source.height)/10*factor);
  1656. }
  1657. } else if (mode == 1) {
  1658. for (int i = 0; i < canvas.width*canvas.height; i++) {
  1659. // canvas.pixels[i] = canvas.pixels[i]+((i/1000)*scankMulti);
  1660. canvas.pixels[i] = canvas.pixels[i]+int(map(i, 0, canvas.width*canvas.height, 0, source.width*source.height)/10*factor);
  1661. }
  1662. }
  1663. canvas.updatePixels();
  1664. canvas.endDraw();
  1665. }
  1666. }
  1667. /*
  1668. MASK
  1669. */
  1670. class MASK extends Shader {
  1671. int backgroundLayer, maskLayer, bandwidth, val, chan, shaderListLength, invert, blend_mode;
  1672. boolean do_blend;
  1673. PImage mask;
  1674. PImage background;
  1675. PImage foreground;
  1676. MASK() {
  1677. name = "mask";
  1678. shaderListLength = gui.shaderList.size();
  1679. params.add(new Param("target layer", INTVAL, 0, shaderListLength-2, new int[]{RANDOM}));
  1680. params.add(new Param("mask layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  1681. params.add(new Param("invert", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  1682. params.add(new Param("bandwidth", INTVAL, 0, 255, new int[]{TRIANG, SINE}));
  1683. params.add(new Param("value", INTVAL, 1, 255, new int[]{TRIANG, SINE}));
  1684. params.add(new Param("channel (R/G/B/H/S/V", INTVAL, 0, 5, new int[]{RANDOM}));
  1685. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM}));
  1686. params.add(new Param("blend mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  1687. background = createImage(canvas.width, canvas.height, ARGB);
  1688. mask = createImage(canvas.width, canvas.height, ARGB);
  1689. foreground = createImage(canvas.width, canvas.height, ARGB);
  1690. }
  1691. void apply() {
  1692. if (shaderListLength != gui.shaderList.size()) {
  1693. shaderListLength = gui.shaderList.size();
  1694. changeParam(0, new Param("target layer", INTVAL, 0, shaderListLength-2, new int[]{RANDOM}));
  1695. changeParam(1, new Param("mask layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  1696. }
  1697. if (mask.width != canvas.width || mask.height != canvas.height) mask.resize(canvas.width, canvas.height);
  1698. if (background.width != canvas.width || background.height != canvas.height) background.resize(canvas.width, canvas.height);
  1699. if (foreground.width != canvas.width || foreground.height != canvas.height) foreground.resize(canvas.width, canvas.height);
  1700. backgroundLayer = (int)params.get(0).value;
  1701. maskLayer = (int)params.get(1).value;
  1702. invert = (int)params.get(2).value;
  1703. bandwidth = (int)params.get(3).value;
  1704. val = (int)params.get(4).value;
  1705. chan = (int)params.get(5).value;
  1706. do_blend = boolean((int)params.get(6).value);
  1707. blend_mode = blends[(int)params.get(7).value];
  1708. if (backgroundLayer <= 0) {
  1709. background.resize(source.width, source.height);
  1710. background = source.get();
  1711. background.resize(canvas.width, canvas.height);
  1712. } else background = gui.shaderList.get(backgroundLayer-1).result.get();
  1713. if (maskLayer <= 0) {
  1714. mask.resize(source.width, source.height);
  1715. mask = source.get();
  1716. mask.resize(canvas.width, canvas.height);
  1717. } else mask = gui.shaderList.get(maskLayer-1).result.get();
  1718. canvas.beginDraw();
  1719. canvas.imageMode(CORNER);
  1720. mask.loadPixels();
  1721. foreground.loadPixels();
  1722. canvas.loadPixels();
  1723. if (boolean(invert)) {
  1724. for (int i = 0; i < canvas.width * canvas.height; i++) {
  1725. if (!thresh(color(mask.pixels[i])))
  1726. foreground.pixels[i] = canvas.pixels[i];
  1727. else
  1728. foreground.pixels[i] = background.pixels[i];
  1729. }
  1730. } else {
  1731. for (int i = 0; i < canvas.width * canvas.height; i++) {
  1732. if (thresh(color(mask.pixels[i])))
  1733. foreground.pixels[i] = canvas.pixels[i];
  1734. else
  1735. foreground.pixels[i] = background.pixels[i];
  1736. }
  1737. }
  1738. canvas.updatePixels();
  1739. foreground.updatePixels();
  1740. mask.updatePixels();
  1741. if (do_blend)
  1742. canvas.blend(foreground, 0, 0, foreground.width, foreground.height, 0, 0, canvas.width, canvas.height, blend_mode);
  1743. else
  1744. canvas.image(foreground, 0, 0, canvas.width, canvas.height);
  1745. canvas.endDraw();
  1746. }
  1747. boolean thresh(color c) {
  1748. switch(chan) {
  1749. case(0):
  1750. if (red(c) > val - bandwidth && red(c) < val + bandwidth)
  1751. return(true);
  1752. else
  1753. return(false);
  1754. case(1):
  1755. if (green(c) > val - bandwidth && green(c) < val + bandwidth)
  1756. return(true);
  1757. else
  1758. return(false);
  1759. case(2):
  1760. if (blue(c) > val - bandwidth && blue(c) < val + bandwidth)
  1761. return(true);
  1762. else
  1763. return(false);
  1764. case(3):
  1765. if (hue(c) > val - bandwidth && hue(c) < val + bandwidth)
  1766. return(true);
  1767. else
  1768. return(false);
  1769. case(4):
  1770. if (saturation(c) > val - bandwidth && saturation(c) < val + bandwidth)
  1771. return(true);
  1772. else
  1773. return(false);
  1774. case(5):
  1775. if (brightness(c) > val - bandwidth && brightness(c) < val + bandwidth)
  1776. return(true);
  1777. else
  1778. return(false);
  1779. }
  1780. return(false);
  1781. }
  1782. }
  1783. class DRAWSTROKES extends Shader {
  1784. int stat_type = ABSDIST2; // type of diff calculation: fast: ABSDIST, DIST, slow: HUE, SATURATION, BRIGHTNESS
  1785. int stroke_len = 3; // length of the stroke, values: 1 and above
  1786. int angles_no = 30; // number of directions stroke can be drew, 2 and above
  1787. int segments = 500; // number of segments of single thread
  1788. float stroke_width = 1; // width of the stroke, 0.5 - 3
  1789. int stroke_alpha = 100; // alpha channel of the stroke: 30 - 200
  1790. color background_color = color(255, 255, 255); // RGB
  1791. boolean interactive = false;
  1792. int max_display_size = 800; // viewing window size (regardless image size)
  1793. int len;
  1794. // working buffer
  1795. PGraphics buffer;
  1796. int currx, curry;
  1797. int[] sintab, costab;
  1798. int sqwidth;
  1799. int iterations;
  1800. int calcDiff(PImage img1, PImage img2) {
  1801. int err = 0;
  1802. for (int i=0; i<img1.pixels.length; i++)
  1803. err += getStat(img1.pixels[i], img2.pixels[i]);
  1804. return err;
  1805. }
  1806. DRAWSTROKES() {
  1807. name = "fxDrawStrokes";
  1808. params.add(new Param ("stat type", INTVAL, 0, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE})); // 4 und 5 sind mit abstand die schnellsten
  1809. params.add(new Param ("stroke length", INTVAL, 1, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1810. params.add(new Param ("amount of angles", INTVAL, 2, 30, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1811. params.add(new Param ("amount of segments", INTVAL, 50, 1500, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1812. params.add(new Param ("stroke width", FLOATVAL, 0.5, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1813. params.add(new Param ("stroke transparency", INTVAL, 30, 220, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1814. params.add(new Param ("iterations", INTVAL, 1, 500, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  1815. len = (renderer.width<renderer.height?renderer.width:renderer.height)/3;
  1816. buffer = createGraphics(renderer.width, renderer.height);
  1817. buffer.smooth(2);
  1818. buffer.beginDraw();
  1819. buffer.strokeWeight(stroke_width);
  1820. buffer.noFill();
  1821. buffer.endDraw();
  1822. rw = renderer.width;
  1823. rh = renderer.height;
  1824. }
  1825. int rw, rh;
  1826. void apply() {
  1827. stat_type = (int)params.get(0).value;
  1828. stroke_len = (int)params.get(1).value;
  1829. angles_no = (int)params.get(2).value;
  1830. segments = (int)params.get(3).value;
  1831. stroke_width = params.get(4).value;
  1832. stroke_alpha = (int)params.get(5).value;
  1833. iterations = (int)params.get(6).value;
  1834. if (rw != canvas.width || rh != canvas.height) { //doesnt account image swap
  1835. len = (canvas.width<canvas.height?canvas.width:canvas.height)/3;
  1836. rw = canvas.width;
  1837. rh = canvas.height;
  1838. PGraphics save = createGraphics(canvas.width, canvas.height);
  1839. save.beginDraw();
  1840. save.image(buffer, 0, 0, save.width, save.height);
  1841. save.endDraw();
  1842. buffer.setSize(canvas.width, canvas.height);
  1843. buffer.beginDraw();
  1844. buffer.image(save, 0, 0, buffer.width, buffer.height); // ????
  1845. buffer.endDraw();
  1846. }
  1847. for (int j = 0; j < iterations; j++) {
  1848. currx = (int)random(canvas.width);
  1849. curry = (int)random(canvas.height);
  1850. sintab = new int[angles_no];
  1851. costab = new int[angles_no];
  1852. for (int i=0; i<angles_no; i++) {
  1853. sintab[i] = (int)(stroke_len * sin(TWO_PI*i/(float)angles_no));
  1854. costab[i] = (int)(stroke_len * cos(TWO_PI*i/(float)angles_no));
  1855. }
  1856. sqwidth = stroke_len * 2 + 4;
  1857. buffer.beginDraw();
  1858. buffer.strokeWeight(stroke_width);
  1859. //draw whole segment using current color
  1860. buffer.stroke(canvas.get(currx, curry), stroke_alpha);
  1861. for (int iter=0; iter<segments; iter++) {
  1862. // corners of square containing new strokes
  1863. int corx = currx-stroke_len-2;
  1864. int cory = curry-stroke_len-2;
  1865. // take square from image and current screen
  1866. PImage imgpart = canvas.get(corx, cory, sqwidth, sqwidth);
  1867. PImage mypart = buffer.get(corx, cory, sqwidth, sqwidth);
  1868. imgpart.loadPixels();
  1869. mypart.loadPixels();
  1870. // calc current diff
  1871. float localerr = calcDiff(imgpart, mypart);
  1872. // chosen stroke will be here
  1873. PImage destpart = null;
  1874. int _nx=currx, _ny=curry;
  1875. // start with random angle
  1876. int i = (int)random(angles_no);
  1877. int iterangles = angles_no;
  1878. while (iterangles-- > 0) {
  1879. // take end points
  1880. int nx = currx + costab[i];
  1881. int ny = curry + sintab[i];
  1882. // if not out of the screen
  1883. if (nx>=0 && nx<canvas.width-1 && ny>=0 && ny<canvas.height-1) {
  1884. // clean region and draw line
  1885. buffer.image(mypart, corx, cory);
  1886. buffer.line(currx, curry, nx, ny);
  1887. // take region with line and calc diff
  1888. PImage curr = buffer.get(corx, cory, sqwidth, sqwidth);
  1889. curr.loadPixels();
  1890. int currerr = calcDiff(imgpart, curr);
  1891. // if better, remember this region and line endpoint
  1892. if (currerr < localerr) {
  1893. destpart = curr;
  1894. _nx = nx;
  1895. _ny = ny;
  1896. localerr = currerr;
  1897. }
  1898. }
  1899. // next angle
  1900. i = (i+1)%angles_no;
  1901. }
  1902. // if we have new stroke, draw it
  1903. if (destpart != null) {
  1904. buffer.image(destpart, corx, cory);
  1905. currx = _nx;
  1906. curry = _ny;
  1907. } else {
  1908. break; // skip
  1909. }
  1910. }
  1911. buffer.endDraw();
  1912. }
  1913. canvas.beginDraw();
  1914. canvas.image(buffer, canvas.width/2, canvas.height/2);
  1915. canvas.endDraw();
  1916. }
  1917. final static int DIST = 0;
  1918. final static int HUE = 1;
  1919. final static int BRIGHTNESS = 2;
  1920. final static int SATURATION = 3;
  1921. final static int ABSDIST = 4;
  1922. final static int ABSDIST2 = 5;
  1923. final float getStat(color c1, color c2) {
  1924. switch(stat_type) {
  1925. case HUE:
  1926. abs(hue(c1)-hue(c2));
  1927. case BRIGHTNESS:
  1928. abs(brightness(c1)-brightness(c2));
  1929. case SATURATION:
  1930. abs(saturation(c1)-saturation(c2));
  1931. case ABSDIST:
  1932. return abs(red(c1)-red(c2))+abs(green(c1)-green(c2))+abs(blue(c1)-blue(c2));
  1933. case ABSDIST2:
  1934. return abs( (red(c1)+blue(c1)+green(c1)) - (red(c2)+blue(c2)+green(c2)) );
  1935. default:
  1936. return sq(red(c1)-red(c2)) + sq(green(c1)-green(c2)) + sq(blue(c1)-blue(c2));
  1937. }
  1938. }
  1939. }
  1940. /*
  1941. DRAWGENERATIVE
  1942. */
  1943. class DRAWGENERATIVE extends Shader {
  1944. // choose channel
  1945. int channel = HUE;
  1946. // run, after 30 iterations result will be saved automatically
  1947. // or press SPACE
  1948. // channels to work with
  1949. final static int RED = 0;
  1950. final static int GREEN = 1;
  1951. final static int BLUE = 2;
  1952. final static int HUE = 3;
  1953. final static int SATURATION = 4;
  1954. final static int BRIGHTNESS = 5;
  1955. final static int NRED = 6;
  1956. final static int NGREEN = 7;
  1957. final static int NBLUE = 8;
  1958. final static int NHUE = 9;
  1959. final static int NSATURATION = 10;
  1960. final static int NBRIGHTNESS = 11;
  1961. int n=2000;
  1962. float [] cx=new float[n];
  1963. float [] cy=new float[n];
  1964. int len;
  1965. // working buffer
  1966. PGraphics buffer;
  1967. int tick = 0;
  1968. DRAWGENERATIVE() {
  1969. name = "fxDrawGenerative";
  1970. params.add(new Param ("stroke width", FLOATVAL, 0.3, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE})); // 4 und 5 sind mit abstand die schnellsten
  1971. buffer = createGraphics(renderer.width, renderer.height);
  1972. buffer.noFill();
  1973. buffer.beginDraw();
  1974. //buffer.background(0); //ENABLE THIS TO DRAW FROM BLANK
  1975. buffer.endDraw();
  1976. rw = canvas.width;
  1977. rh = canvas.height;
  1978. len = (canvas.width<canvas.height?canvas.width:canvas.height)/6;
  1979. for (int i=0; i<n; i++) {
  1980. cx[i]=random(canvas.width);
  1981. cy[i]=random(canvas.height);
  1982. }
  1983. }
  1984. int rw, rh;
  1985. void apply() {
  1986. if (rw != canvas.width || rh != canvas.height) {
  1987. rw = canvas.width;
  1988. rh = canvas.height;
  1989. PGraphics save = createGraphics(canvas.width, canvas.height);
  1990. save.beginDraw();
  1991. save.image(buffer, 0, 0, save.width, save.height);
  1992. save.endDraw();
  1993. buffer.setSize(canvas.width, canvas.height);
  1994. buffer.beginDraw();
  1995. buffer.image(save, 0, 0, buffer.width, buffer.height);
  1996. buffer.endDraw();
  1997. }
  1998. buffer.beginDraw();
  1999. buffer.strokeWeight(params.get(0).value);
  2000. for (int i=1; i<n; i++) {
  2001. color c = canvas.get((int)cx[i], (int)cy[i]);
  2002. buffer.stroke(c);
  2003. buffer.point(cx[i], cy[i]);
  2004. // you can choose channels: red(c), blue(c), green(c), hue(c), saturation(c) or brightness(c)
  2005. cy[i]+=sin(map(getChannel(c), 0, 255, 0, TWO_PI));
  2006. cx[i]+=cos(map(getChannel(c), 0, 255, 0, TWO_PI));
  2007. }
  2008. if (frameCount>len) {
  2009. frameCount=0;
  2010. //println("iteration: " + tick++);
  2011. for (int i=0; i<n; i++) {
  2012. cx[i]=random(canvas.width); //same problem as in slitscan here
  2013. cy[i]=random(canvas.height);
  2014. }
  2015. }
  2016. buffer.endDraw();
  2017. canvas.beginDraw();
  2018. canvas.image(buffer, canvas.width/2, canvas.height/2);
  2019. canvas.endDraw();
  2020. }
  2021. float getChannel(color c) {
  2022. int ch = channel>5?channel-6:channel;
  2023. float cc;
  2024. switch(ch) {
  2025. case RED:
  2026. cc = red(c);
  2027. break;
  2028. case GREEN:
  2029. cc = green(c);
  2030. break;
  2031. case BLUE:
  2032. cc = blue(c);
  2033. break;
  2034. case HUE:
  2035. cc = hue(c);
  2036. break;
  2037. case SATURATION:
  2038. cc = saturation(c);
  2039. break;
  2040. default:
  2041. cc= brightness(c);
  2042. break;
  2043. }
  2044. return channel>5?255-cc:cc;
  2045. }
  2046. }
  2047. /*
  2048. PIXELDRIFTER
  2049. */
  2050. class PIXELDRIFTER extends Shader {
  2051. int channel = HUE; // channel data used for shift
  2052. float channel_off = 60; // channel value offset
  2053. int iteration_no = 50; // number of iterations 1-100
  2054. int max_iter = iteration_no;
  2055. int direction = UP; // UP, DOWN, LEFT, RIGHT
  2056. boolean automate_channel_offset = false; // if true, change channel_offset every iteration
  2057. float scale = 0.03; // 0.01 - 0.1, step size (0.01: 2px, 0.1:25px)
  2058. float feedback = 0.9; // 0.9 - 0.999 ; blend ratio with original image
  2059. boolean do_blend = false; // blend image after process
  2060. int blend_mode = OVERLAY; // blend type
  2061. // working buffer
  2062. PGraphics buffer;
  2063. // image
  2064. PImage imgb;
  2065. PImage img;
  2066. String sessionid;
  2067. float acho_step;
  2068. int rw, rh;
  2069. PIXELDRIFTER() {
  2070. name = "fxPixelDrifter";
  2071. params.add(new Param ("channel offset", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2072. params.add(new Param ("iterations", INTVAL, 1, 100, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2073. params.add(new Param ("step size", FLOATVAL, 0.01, 0.1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2074. params.add(new Param ("feedback", FLOATVAL, 0.1, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2075. params.add(new Param ("direction", INTVAL, 37, 40, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2076. params.add(new Param ("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2077. params.add(new Param ("blend mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  2078. params.add(new Param ("channel", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  2079. params.add(new Param ("automate channel offset", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  2080. rw = canvas.width;
  2081. rh = canvas.height;
  2082. img = createImage(rw, rh, ARGB);
  2083. buffer = createGraphics(rw, rh);
  2084. buffer.beginDraw();
  2085. buffer.noStroke();
  2086. buffer.smooth(8);
  2087. buffer.background(0);
  2088. buffer.image(img, 0, 0);
  2089. buffer.endDraw();
  2090. scale = abs(scale);
  2091. }
  2092. void apply() {
  2093. if (rw != canvas.width || rh != canvas.height) {
  2094. rw = canvas.width;
  2095. rh = canvas.height;
  2096. img.resize(rw, rh);
  2097. buffer = createGraphics(rw, rh);
  2098. }
  2099. channel_off = map(params.get(0).value, 0, 1, 10, max(rw, rh));
  2100. iteration_no = (int)params.get(1).value;
  2101. scale = params.get(2).value;
  2102. feedback = params.get(3).value;
  2103. direction = (int)params.get(4).value;
  2104. do_blend = boolean((int)params.get(5).value);
  2105. blend_mode = blends[(int)params.get(6).value];
  2106. channel = (int)params.get(7).value == 1 ? HUE : RGB;
  2107. automate_channel_offset = boolean((int)params.get(8).value);
  2108. acho_step = 256.0 / iteration_no;
  2109. img = canvas.get();
  2110. // if (iteration_no>0) {
  2111. for (int i = 0; i < iteration_no; i++) {
  2112. processImage();
  2113. }
  2114. //iteration_no--;
  2115. //iteration_no=(iteration_no==0)?max_iter:iteration_no;
  2116. // if (iteration_no == 0)
  2117. // iteration_no = 50;
  2118. // }
  2119. // } else {
  2120. canvas.beginDraw();
  2121. if (do_blend) {
  2122. canvas.blend(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height, blend_mode);
  2123. } else {
  2124. canvas.image(buffer, canvas.width/2, canvas.height/2, canvas.width, canvas.height);
  2125. }
  2126. canvas.endDraw();
  2127. // canvas.image(img, 0, 0, renderer.width, renderer.height);
  2128. // noLoop();
  2129. // }
  2130. }
  2131. void processImage() {
  2132. buffer.beginDraw();
  2133. for (int x=0; x<img.width; x++) {
  2134. for (int y=0; y<img.height; y++) {
  2135. color c = img.get(x, y);
  2136. color c2;
  2137. if (direction == UP || direction == DOWN) {
  2138. c2 = img.get(x, ((int)(y+img.height+( (channel_off+getChannel(c, channel))%255 )*(direction==DOWN?-1.0:1.0)*scale))%img.height);
  2139. } else {
  2140. c2 = img.get(((int)(x+img.width+( (channel_off+getChannel(c, channel))%255)*(direction==RIGHT?-1.0:1.0)*scale))%img.width, y);
  2141. }
  2142. buffer.set(x, y, lerpColor(c, c2, feedback) );
  2143. }
  2144. }
  2145. buffer.endDraw();
  2146. //canvas.image(buffer, 0, 0, width, height);
  2147. img = buffer.get();
  2148. if (automate_channel_offset) channel_off += acho_step;
  2149. }
  2150. // ALL Channels, Nxxx stand for negative (255-value)
  2151. // channels to work with
  2152. final static int RED = 0;
  2153. final static int GREEN = 1;
  2154. final static int BLUE = 2;
  2155. final static int HUE = 3;
  2156. final static int SATURATION = 4;
  2157. final static int BRIGHTNESS = 5;
  2158. final static int NRED = 6;
  2159. final static int NGREEN = 7;
  2160. final static int NBLUE = 8;
  2161. final static int NHUE = 9;
  2162. final static int NSATURATION = 10;
  2163. final static int NBRIGHTNESS = 11;
  2164. float getChannel(color c, int channel) {
  2165. int ch = channel>5?channel-6:channel;
  2166. float cc;
  2167. switch(ch) {
  2168. case RED:
  2169. cc = red(c);
  2170. break;
  2171. case GREEN:
  2172. cc = green(c);
  2173. break;
  2174. case BLUE:
  2175. cc = blue(c);
  2176. break;
  2177. case HUE:
  2178. cc = hue(c);
  2179. break;
  2180. case SATURATION:
  2181. cc = saturation(c);
  2182. break;
  2183. default:
  2184. cc= brightness(c);
  2185. break;
  2186. }
  2187. return channel>5?255-cc:cc;
  2188. }
  2189. }
  2190. /*
  2191. DRIPDRIP
  2192. */
  2193. class DRIPDRIP extends Shader {
  2194. int maxsteps = 50; //maximum fade length in px
  2195. int minsteps = 2; //minimum fade length in px
  2196. Boolean brightmode = false; //if enabled will fade light over dark
  2197. Boolean autotoggle = true; //switch brightmode at pivot point
  2198. float autoPivot = 0.58; //where on the y axis (0-1) to switch
  2199. int steps[];
  2200. int rw, rh;
  2201. DRIPDRIP() {
  2202. name = "fxDripDrip";
  2203. params.add(new Param ("max steps", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2204. params.add(new Param ("min steps", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2205. params.add(new Param ("bright breakpoint", FLOATVAL, 0, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2206. //params.add(new Param ("auto toggle", INTVAL, 0, 1, new int[]{SQUAR, RANDOM}));
  2207. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2208. directionParamIndex = 3;
  2209. rw = canvas.width;
  2210. rh = canvas.height;
  2211. steps = new int[canvas.width*canvas.height];
  2212. for (int i = 0; i < canvas.width*canvas.height; i++) {
  2213. steps[i] = (int)map(random(1), 0, 1, minsteps, maxsteps);
  2214. }
  2215. }
  2216. void apply() {
  2217. minsteps = (int)map(params.get(1).value, 0, 1, 1, max(canvas.width, canvas.height)/10);
  2218. maxsteps = (int)map(params.get(0).value, 0, 1, minsteps, max(canvas.width, canvas.height)/10);
  2219. autoPivot = params.get(2).value;
  2220. //brightmode = boolean((int)params.get(3).value);
  2221. //autotoggle = boolean((int)params.get(4).value);
  2222. if (rw != canvas.width || rh != canvas.height) {
  2223. rw = canvas.width;
  2224. rh = canvas.height;
  2225. steps = new int[canvas.width*canvas.height];
  2226. }
  2227. for (int i = 0; i < steps.length; i++) {
  2228. steps[i] = (int)map(random(1), 0, 1, minsteps, maxsteps);
  2229. }
  2230. canvas.beginDraw();
  2231. canvas.loadPixels();
  2232. for ( int x = 0, w = canvas.width; x<w; x++) {
  2233. for ( int h = canvas.height, y = h-1; y>-1; y--) {
  2234. /*
  2235. if ( alternatetoggle ) {
  2236. brightmode = !brightmode;
  2237. } else {
  2238. if ( autotoggle ) {
  2239. brightmode = y > (h*autoPivot);
  2240. }
  2241. }
  2242. */
  2243. //if (autotoggle) {
  2244. brightmode = y > (h*autoPivot);
  2245. //}
  2246. float rat = 1.0;
  2247. int pos = x + y * w;
  2248. color c = canvas.pixels[pos];
  2249. int ty = y;
  2250. while ( rat > 1.0/steps[x*y]*2 ) {
  2251. ty++;
  2252. if ( ty >= h ) break;
  2253. int tpos = x + ty * w;
  2254. color tc = canvas.pixels[tpos];
  2255. if (
  2256. ( !brightmode && brightness(tc) < brightness(c) )
  2257. || ( brightmode && brightness(tc) > brightness(c) )
  2258. ) break;
  2259. canvas.pixels[tpos] = blendC(tc, c, rat);
  2260. rat-= rat/steps[x*y];
  2261. }
  2262. }
  2263. }
  2264. canvas.updatePixels();
  2265. canvas.endDraw();
  2266. }
  2267. color blendC(color tc, color sc, float rat) {
  2268. return color(
  2269. (red(tc)*(1.0-rat))+(red(sc)*rat),
  2270. (green(tc)*(1.0-rat))+(green(sc)*rat),
  2271. (blue(tc)*(1.0-rat))+(blue(sc)*rat)
  2272. );
  2273. }
  2274. }
  2275. /*
  2276. WRONGQSORT
  2277. */
  2278. class WRONGQSORT extends Shader {
  2279. boolean mode = L; // L or R, which sort part is broken
  2280. boolean do_blend = false; // blend image after process
  2281. int blend_mode = OVERLAY; // blend type
  2282. static final boolean L = true;
  2283. static final boolean R = false;
  2284. // working buffer
  2285. PGraphics buffer;
  2286. // image
  2287. PImage img;
  2288. float random_point = 0.5;
  2289. int len;
  2290. int rw, rh;
  2291. WRONGQSORT() {
  2292. name = "fxWrongQSort";
  2293. params.add(new Param ("v", FLOATVAL, 0.1, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2294. params.add(new Param ("randomize", FLOATVAL, 0.1, 0.9, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2295. params.add(new Param ("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2296. params.add(new Param ("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2297. params.add(new Param ("blend mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  2298. params.add(new Param ("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2299. directionParamIndex = 5;
  2300. img = createImage(canvas.width, canvas.height, ARGB);
  2301. img = canvas.get();
  2302. buffer = createGraphics(canvas.width, canvas.height);
  2303. buffer.beginDraw();
  2304. buffer.noStroke();
  2305. buffer.smooth(8);
  2306. buffer.background(0);
  2307. buffer.image(img, 0, 0);
  2308. buffer.endDraw();
  2309. }
  2310. void apply() {
  2311. random_point = params.get(1).value;
  2312. mode = boolean((int)params.get(2).value);
  2313. do_blend = boolean((int)params.get(3).value);
  2314. blend_mode = blends[(int)params.get(4).value];
  2315. if (rw != canvas.width || rh != canvas.height) {
  2316. rw = canvas.width;
  2317. rh = canvas.height;
  2318. img.resize(rw, rh);
  2319. buffer = createGraphics(rw, rh);
  2320. }
  2321. img = canvas.get();
  2322. len = rw * rh;
  2323. int v = (int)map(params.get(0).value, 0, 1, 1, len-1);
  2324. buffer.beginDraw();
  2325. buffer.image(img, 0, 0);
  2326. buffer.endDraw();
  2327. buffer.loadPixels();
  2328. int x = 0;
  2329. while (x<len) {
  2330. if (x+v<len) quicksort(buffer.pixels, x, x+v);
  2331. else quicksort(buffer.pixels, x, len-1);
  2332. x+=v;
  2333. }
  2334. buffer.updatePixels();
  2335. buffer.beginDraw();
  2336. if (do_blend) {
  2337. buffer.blend(img, 0, 0, img.width, img.height, 0, 0, buffer.width, buffer.height, blend_mode);
  2338. }
  2339. buffer.endDraw();
  2340. canvas.beginDraw();
  2341. canvas.image(buffer, canvas.width/2, canvas.height/2);
  2342. canvas.endDraw();
  2343. }
  2344. int partition(int x[], int left, int right) {
  2345. int i = left;
  2346. int j = right;
  2347. int temp;
  2348. int pivot = x [(int)map(random_point, 0, 1, left, right)];
  2349. while (i<= j) {
  2350. while (x[i] < pivot) {
  2351. i++;
  2352. }
  2353. while (x[j] > pivot) {
  2354. j--;
  2355. }
  2356. if (i <= j) {
  2357. temp = x[i];
  2358. x[i] = x [j];
  2359. x[j] = temp;
  2360. i++;
  2361. j--;
  2362. }
  2363. }
  2364. return i;
  2365. }
  2366. void quicksort(int x[], int left, int right) {
  2367. if (left<right) {
  2368. int index = partition(x, left, right);
  2369. if (mode) {
  2370. if (left < index-1) quicksort(x, left, index-1);
  2371. if (right < index) quicksort(x, index, right);
  2372. } else {
  2373. if (left > index-1) quicksort(x, left, index-1);
  2374. if (right > index) quicksort(x, index, right);
  2375. }
  2376. }
  2377. }
  2378. }
  2379. /*
  2380. VHS
  2381. */
  2382. class VHS extends Shader {
  2383. PImage timg;
  2384. int bass, treble, sxl, syl;
  2385. VHS () {
  2386. name = "fxVHS";
  2387. params.add(new Param ("sine x length", INTVAL, 1, 100, new int[]{TRIANG, SINE}));
  2388. params.add(new Param ("sine y length", INTVAL, 1, 100, new int[]{TRIANG, SINE}));
  2389. params.add(new Param ("bass", INTVAL, -15, 15, new int[]{TRIANG, SINE}));
  2390. params.add(new Param ("treble", INTVAL, -15, 15, new int[]{TRIANG, SINE}));
  2391. params.add(new Param ("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2392. directionParamIndex = 4;
  2393. }
  2394. void apply() {
  2395. sxl = (int)params.get(0).value;
  2396. syl = (int)params.get(1).value;
  2397. bass = (int)params.get(2).value;
  2398. treble = (int)params.get(3).value;
  2399. int virtw = canvas.width;
  2400. int virth = canvas.height;
  2401. PImage timg = createImage(canvas.width, canvas.height, RGB);
  2402. timg = canvas.get();
  2403. PImage hay = sinwav(timg, sxl, syl);
  2404. PImage result = createImage(timg.width, timg.height, RGB);
  2405. result.copy(hay, 0, 0, virtw, virth, 0, 0, result.width, result.height);
  2406. canvas.beginDraw();
  2407. canvas.image(basstreble(result, bass, treble), canvas.width/2, canvas.height/2, canvas.width, canvas.height); //0 to 100
  2408. canvas.endDraw();
  2409. }
  2410. PImage sinwav(PImage img, int xp, int yp) {
  2411. int ctr = 0;
  2412. img.loadPixels();
  2413. float ylength = xp;
  2414. float xlength = yp; // was map (yp, 0,100, 1, 100);
  2415. PImage result = createImage(img.width, img.height, RGB);
  2416. for ( int y =0, h = img.height; y<h; y++) {
  2417. for ( int x = 0, w = img.width; x<w; x++, ctr++) {
  2418. int pos = x + y * w;
  2419. color c = img.pixels[pos];
  2420. int epos = (int)(( x + sin(y/ylength)*xlength)+ y * w);
  2421. if ( epos < result.pixels.length )
  2422. result.pixels[epos] = c;
  2423. else
  2424. result.pixels[pos] = c;
  2425. }
  2426. }
  2427. result.updatePixels();
  2428. return result;
  2429. }
  2430. PImage basstreble(PImage img, int xp, int yp) {
  2431. float dB_bass = xp;
  2432. float dB_treble = yp;
  2433. PImage result = createImage(img.width, img.height, RGB);
  2434. float slope = 0.4;
  2435. double hzBass = 250.0;
  2436. double hzTreble = 4000.0;
  2437. float b0, b1, b2, a0, a1, a2, xn2Bass, xn1Bass, yn2Bass, yn1Bass, b0Bass, b1Bass, b2Bass, xn1Treble, xn2Treble, yn1Treble, yn2Treble, a0Bass, a1Bass, a2Bass, a0Treble, a1Treble, a2Treble, b0Treble, b1Treble, b2Treble;
  2438. double mMax = 0.0;
  2439. double mSampleRate = 44000;
  2440. xn1Bass = xn2Bass = yn1Bass = yn2Bass = 0.0;
  2441. xn1Treble = xn2Treble = yn1Treble = yn2Treble = 0.0;
  2442. float w = (float)(2 * PI * hzBass / mSampleRate);
  2443. float a = exp((float)(log(10.0) * dB_bass / 40));
  2444. float b = sqrt((float)((a * a + 1) / slope - (pow((float)(a - 1), 2))));
  2445. b0Bass = a * ((a + 1) - (a - 1) * cos(w) + b * sin(w));
  2446. b1Bass = 2 * a * ((a - 1) - (a + 1) * cos(w));
  2447. b2Bass = a * ((a + 1) - (a - 1) * cos(w) - b * sin(w));
  2448. a0Bass = ((a + 1) + (a - 1) * cos(w) + b * sin(w));
  2449. a1Bass = -2 * ((a - 1) + (a + 1) * cos(w));
  2450. a2Bass = (a + 1) + (a - 1) * cos(w) - b * sin(w);
  2451. w = (float)(2 * PI * hzTreble / mSampleRate);
  2452. a = exp((float)(log(10.0) * dB_treble / 40));
  2453. b = sqrt((float)((a * a + 1) / slope - (pow((float)(a - 1), 2))));
  2454. b0Treble = a * ((a + 1) + (a - 1) * cos(w) + b * sin(w));
  2455. b1Treble = -2 * a * ((a - 1) + (a + 1) * cos(w));
  2456. b2Treble = a * ((a + 1) + (a - 1) * cos(w) - b * sin(w));
  2457. a0Treble = ((a + 1) - (a - 1) * cos(w) + b * sin(w));
  2458. a1Treble = 2 * ((a - 1) - (a + 1) * cos(w));
  2459. a2Treble = (a + 1) - (a - 1) * cos(w) - b * sin(w);
  2460. img.loadPixels();
  2461. result.loadPixels();
  2462. for ( int i = 0, l = img.pixels.length; i<l; i++) {
  2463. int[] rgb = new int[3];
  2464. rgb[0] = (int)red(img.pixels[i]);
  2465. rgb[1] = (int)green(img.pixels[i]);
  2466. rgb[2] = (int)blue(img.pixels[i]);
  2467. for ( int ri = 0; ri<3; ri++ ) {
  2468. float in = map(rgb[ri], 0, 255, 0, 1);
  2469. float out = (b0Bass * in + b1Bass * xn1Bass + b2Bass * xn2Bass - a1Bass * yn1Bass - a2Bass * yn2Bass ) / a0Bass;
  2470. //println(a0Bass);
  2471. xn2Bass = xn1Bass;
  2472. xn1Bass = in;
  2473. yn2Bass = yn1Bass;
  2474. yn1Bass = out;
  2475. //treble filter
  2476. in = out;
  2477. out = (b0Treble * in + b1Treble * xn1Treble + b2Treble * xn2Treble - a1Treble * yn1Treble - a2Treble * yn2Treble) / a0Treble;
  2478. xn2Treble = xn1Treble;
  2479. xn1Treble = in;
  2480. yn2Treble = yn1Treble;
  2481. yn1Treble = out;
  2482. //retain max value for use in normalization
  2483. if ( mMax < abs(out))
  2484. mMax = abs(out);
  2485. rgb[ri] = (int)map(out, 0, 1, 0, 255);
  2486. }
  2487. result.pixels[i] = color(rgb[0], rgb[1], rgb[2]);
  2488. }
  2489. result.updatePixels();
  2490. return result;
  2491. }
  2492. }
  2493. /*
  2494. LZ7
  2495. */
  2496. class LZ7 extends Shader {
  2497. // choose colorspace
  2498. int colorspace = HSB; // HSB or RGB
  2499. // set compressor attributes for each channel in chosen colorspace
  2500. // first number is length of dictionary in LZ77 - values 100 - 4000
  2501. // second number is length of word (ahead buffer) in LZ77 - about 5% - 50% of dictionary size
  2502. int[][] compressor_attributes = { {2000, 250}, // channel 1 (H or R)
  2503. {50, 10}, // channel 2 (S or G)
  2504. {100, 100} }; // channel 3 (B or B)
  2505. // set number of glitches made for each channel
  2506. // first number is actual number of random change in compressed channel
  2507. // second number is amount of change (values from 0.01 to 4)
  2508. float[][] number_of_glitches = { {5000, 2}, // channel 1
  2509. {500, 1}, // channel 2
  2510. {50, 0.1} }; // channel 3
  2511. // working buffer
  2512. PGraphics buffer;
  2513. // image
  2514. PImage img;
  2515. boolean do_blend = false; // blend image after process
  2516. int blend_mode = OVERLAY; // blend type
  2517. LZ77 comp1, comp2, comp3;
  2518. byte[] cr, cb, cg;
  2519. //red channel dictionary size
  2520. //red channel word size
  2521. //red channel randomness
  2522. //red channel amount of change
  2523. LZ7() {
  2524. name = "fxLZ77";
  2525. params.add(new Param("dictionary size (red/hue)", INTVAL, 100, 3000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2526. params.add(new Param("word length (red/hue)", FLOATVAL, 0.05, 0.5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2527. params.add(new Param("randomness (red/hue)", INTVAL, 20, 5000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2528. params.add(new Param("amount of change (red/hue)", FLOATVAL, 0.01, 4, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2529. params.add(new Param("dictionary size (green/saturation)", INTVAL, 100, 3000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2530. params.add(new Param("word length (green/saturation)", FLOATVAL, 0.05, 0.5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2531. params.add(new Param("randomness (green/saturation)", INTVAL, 20, 5000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2532. params.add(new Param("amount of change (green/saturation)", FLOATVAL, 0.01, 4, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2533. params.add(new Param("dictionary size (blue/brightness)", INTVAL, 100, 3000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2534. params.add(new Param("word length (blue/brightness)", FLOATVAL, 0.05, 0.5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2535. params.add(new Param("randomness (blue/brightness)", INTVAL, 20, 5000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2536. params.add(new Param("amount of change (blue/brightness)", FLOATVAL, 0.01, 4, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2537. params.add(new Param("colorspace (rgb / hsb)", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2538. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2539. params.add(new Param("blend mode", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  2540. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2541. directionParamIndex = 15;
  2542. rw = canvas.width;
  2543. rh = canvas.height;
  2544. buffer = createGraphics(rw, rh);
  2545. buffer.beginDraw();
  2546. buffer.noStroke();
  2547. //buffer.smooth(8);
  2548. buffer.background(0);
  2549. buffer.endDraw();
  2550. img = createImage(canvas.width, canvas.height, RGB);
  2551. cr = new byte[rw*rh];
  2552. cb = new byte[rw*rh];
  2553. cg = new byte[rw*rh];
  2554. }
  2555. int rw, rh;
  2556. void apply() {
  2557. if (rw != canvas.width || rh != canvas.height) {
  2558. rw = canvas.width;
  2559. rh = canvas.height;
  2560. cr = new byte[rw*rh];
  2561. cb = new byte[rw*rh];
  2562. cg = new byte[rw*rh];
  2563. buffer = createGraphics(rw, rh);
  2564. img.resize(rw, rh);
  2565. }
  2566. img = canvas.get();
  2567. colorspace = (int)params.get(12).value == 0 ?RGB:HSB;
  2568. do_blend = boolean((int)params.get(13).value);
  2569. blend_mode = blends[(int)params.get(14).value];
  2570. //red
  2571. compressor_attributes[0][0] = (int)params.get(0).value;
  2572. compressor_attributes[0][1] = (int)(params.get(1).value*compressor_attributes[0][0]);
  2573. number_of_glitches[0][0] = (int)params.get(2).value;
  2574. number_of_glitches[0][1] = params.get(3).value;
  2575. //green
  2576. compressor_attributes[1][0] = (int)params.get(4).value;
  2577. compressor_attributes[1][1] = (int)(params.get(5).value*compressor_attributes[1][0]);
  2578. number_of_glitches[1][0] = (int)params.get(6).value;
  2579. number_of_glitches[1][1] = params.get(7).value;
  2580. //blue
  2581. compressor_attributes[2][0] = (int)params.get(8).value;
  2582. compressor_attributes[2][1] = (int)(params.get(9).value*compressor_attributes[2][0]);
  2583. number_of_glitches[2][0] = (int)params.get(10).value;
  2584. number_of_glitches[2][1] = params.get(11).value;
  2585. comp1 = new LZ77( compressor_attributes[0][0], compressor_attributes[0][1] );
  2586. comp2 = new LZ77( compressor_attributes[1][0], compressor_attributes[1][1] );
  2587. comp3 = new LZ77( compressor_attributes[2][0], compressor_attributes[2][1] );
  2588. buffer.beginDraw();
  2589. //print("Preparing... ");
  2590. img.loadPixels();
  2591. int iter=0;
  2592. for (int i=0; i<img.pixels.length; i++) {
  2593. color c = img.pixels[i];
  2594. if (colorspace == HSB) {
  2595. cr[iter]= (byte)hue(c);
  2596. cg[iter]= (byte)saturation(c);
  2597. cb[iter]= (byte)brightness(c);
  2598. } else {
  2599. cr[iter]= (byte)red(c);
  2600. cg[iter]= (byte)green(c);
  2601. cb[iter]= (byte)blue(c);
  2602. }
  2603. iter++;
  2604. }
  2605. //println("done");
  2606. //print("Glitching channel 1... ");
  2607. comp1.doCompress(cr);
  2608. comp1.glitch( (int)number_of_glitches[0][0], number_of_glitches[0][1] );
  2609. comp1.doDecompress(cr);
  2610. //println("done");
  2611. //print("Glitching channel 2... ");
  2612. comp2.doCompress(cg);
  2613. comp2.glitch( (int)number_of_glitches[1][0], number_of_glitches[1][1] );
  2614. comp2.doDecompress(cg);
  2615. //println("done");
  2616. //print("Glitching channel 3... ");
  2617. comp3.doCompress(cb);
  2618. comp3.glitch( (int)number_of_glitches[2][0], number_of_glitches[2][1] );
  2619. comp3.doDecompress(cb);
  2620. //println("done");
  2621. for (int i = 0; i < 3; i++) {
  2622. //println("Channel "+(i+1)+" = ("+compressor_attributes[i][0]+","+compressor_attributes[i][1]+")" + ", glitches = ("+number_of_glitches[i][0]+","+number_of_glitches[i][1]+")");
  2623. }
  2624. buffer.loadPixels();
  2625. if (colorspace == HSB) colorMode(HSB);
  2626. else colorMode(RGB);
  2627. iter=0;
  2628. for (int i=0; i<buffer.pixels.length; i++) {
  2629. float r = cr[iter];
  2630. r = r>=0?r:r+256;
  2631. float g = cg[iter];
  2632. g = g>=0?g:g+256;
  2633. float b = cb[iter];
  2634. b = b>=0?b:b+256;
  2635. buffer.pixels[i] = color(r, g, b);
  2636. iter++;
  2637. }
  2638. buffer.updatePixels();
  2639. if (do_blend)
  2640. buffer.blend(img, 0, 0, img.width, img.height, 0, 0, buffer.width, buffer.height, blend_mode);
  2641. buffer.endDraw();
  2642. canvas.beginDraw();
  2643. canvas.image(buffer, canvas.width/2, canvas.height/2);
  2644. canvas.endDraw();
  2645. }
  2646. }
  2647. class Tuple {
  2648. public int offset, len;
  2649. byte chr;
  2650. public Tuple(int o, int l, byte c) {
  2651. offset = o;
  2652. len = l;
  2653. chr = c;
  2654. }
  2655. }
  2656. class LZ77 {
  2657. int windowWidth;
  2658. int lookAheadWidht;
  2659. public LZ77(int ww, int law) {
  2660. windowWidth = ww;
  2661. lookAheadWidht = law;
  2662. }
  2663. ArrayList<Tuple> clist = new ArrayList<Tuple>();
  2664. public void glitch(int no, float fac) {
  2665. for (int i=0; i<no; i++) {
  2666. Tuple r = clist.get( (int)random(clist.size()));
  2667. int what = (int)random(3);
  2668. switch(what) {
  2669. case 0:
  2670. r.chr = (byte)random(256);
  2671. break;
  2672. case 1:
  2673. r.offset = (int)random(2*windowWidth*fac);
  2674. break;
  2675. default:
  2676. r.len = (int)random(2*lookAheadWidht*fac);
  2677. }
  2678. }
  2679. }
  2680. public void doCompress(byte[] buff) {
  2681. int currByte = 1;
  2682. // first is always byte
  2683. clist.add( new Tuple(0, 0, buff[0]) );
  2684. while (currByte < buff.length) {
  2685. int bend = constrain(currByte-windowWidth, 0, buff.length);
  2686. int boff = 0;
  2687. int blen = 0;
  2688. if (currByte<buff.length-1)
  2689. for (int i = currByte-1; i>=bend; i--) {
  2690. if (buff[currByte] == buff[i]) {
  2691. int tboff = abs(i-currByte);
  2692. int tblen = 1;
  2693. int laEnd = constrain(currByte+lookAheadWidht, 0, buff.length-1);
  2694. int mi = currByte+1;
  2695. while (mi<laEnd && (i+mi-currByte)<currByte) {
  2696. if (buff[mi] == buff[i+mi-currByte]) {
  2697. mi++;
  2698. tblen++;
  2699. } else {
  2700. break;
  2701. }
  2702. }
  2703. if (tblen>blen) {
  2704. blen = tblen;
  2705. boff = tboff;
  2706. }
  2707. }
  2708. }
  2709. currByte +=blen+1;
  2710. // println("currchar = " + (currByte-1)+",blen = " + blen);
  2711. clist.add( new Tuple(boff, blen, buff[currByte-1]) );
  2712. // println(boff + ", " + blen + ", " + buff[currByte-1]);
  2713. }
  2714. //println("clist " + clist.size()*2);
  2715. }
  2716. void doDecompress(byte[] buff) {
  2717. int i = 0;
  2718. for (Tuple t : clist) {
  2719. if (i>=buff.length) break;
  2720. if (t.offset == 0) {
  2721. buff[i++] = t.chr;
  2722. } else {
  2723. int start = i-t.offset;
  2724. int end = start + t.len;
  2725. for (int c = start; c<end; c++) {
  2726. int pos = constrain(c, 0, buff.length-1);
  2727. buff[constrain(i++, 0, buff.length-1)] = buff[pos];
  2728. if (i>=buff.length) break;
  2729. }
  2730. if (i>=buff.length) break;
  2731. buff[i++] = t.chr;
  2732. }
  2733. }
  2734. }
  2735. }
  2736. /*
  2737. LENS
  2738. */
  2739. class LENS extends Shader {
  2740. // parameters
  2741. float bendx = 0.1; // from 0 to 1
  2742. float bendy = 0.1; // from 0 to 1
  2743. float[] power_vals = { 2, 0.5 }; // two values of power from 0.1 to 10, one for x and second for y
  2744. int[] types = { LINEAR, POWER }; // always to types one for x second for y
  2745. int[] channels = { BRIGHTNESS, SATURATION }; // as above
  2746. float[] facts = new float[2];
  2747. //////////////
  2748. PImage img;
  2749. PImage limg;
  2750. int shaderListLength;
  2751. // working buffer
  2752. PGraphics buffer;
  2753. LENS() {
  2754. name = "fxLens";
  2755. shaderListLength = gui.shaderList.size();
  2756. params.add(new Param("lens image layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  2757. params.add(new Param("power x", FLOATVAL, 0.1, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2758. params.add(new Param("power y", FLOATVAL, 0.1, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2759. params.add(new Param("lens type x", INTVAL, 0, 3, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2760. params.add(new Param("lens type y", INTVAL, 0, 3, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2761. params.add(new Param("channel x", INTVAL, 0, 11, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2762. params.add(new Param("channel y", INTVAL, 0, 11, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2763. params.add(new Param("curvature factor x", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2764. params.add(new Param("curvature factor y", FLOATVAL, 0.01, 1, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2765. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2766. directionParamIndex = 9;
  2767. rw = canvas.width;
  2768. rh = canvas.height;
  2769. img = createImage(rw, rh, ARGB);
  2770. limg = createImage(rw, rh, ARGB);
  2771. buffer = createGraphics(rw, rh);
  2772. buffer.beginDraw();
  2773. buffer.noStroke();
  2774. buffer.endDraw();
  2775. }
  2776. int rw, rh;
  2777. void apply() {
  2778. if (rw != canvas.width || rh != canvas.height) {
  2779. rw = canvas.width;
  2780. rh = canvas.height;
  2781. buffer = createGraphics(rw, rh);
  2782. buffer.beginDraw();
  2783. buffer.noStroke();
  2784. buffer.endDraw();
  2785. img.resize(rw, rh);
  2786. limg.resize(rw, rh);
  2787. }
  2788. if (shaderListLength != gui.shaderList.size()) {
  2789. shaderListLength = gui.shaderList.size();
  2790. changeParam(0, new Param("lens image layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  2791. }
  2792. img = canvas.get();
  2793. if ((int)params.get(0).value == pos) {
  2794. limg = canvas.get();
  2795. } else {
  2796. limg = gui.shaderList.get((int)params.get(0).value).result.get();
  2797. }
  2798. power_vals[0] = params.get(1).value;
  2799. power_vals[1] = params.get(2).value;
  2800. types[0] = (int)params.get(3).value;
  2801. types[1] = (int)params.get(4).value;
  2802. channels[0] = (int)params.get(5).value;
  2803. channels[1] = (int)params.get(6).value;
  2804. bendx = params.get(7).value;
  2805. bendy = params.get(8).value;
  2806. facts[0] = bendx * img.width;
  2807. facts[1] = bendy * img.height;
  2808. img.loadPixels();
  2809. limg.loadPixels();
  2810. buffer.beginDraw();
  2811. buffer.background(0);
  2812. for (int x=0; x<img.width; x++) {
  2813. int lx = (int)map(x, 0, img.width-1, 0, limg.width-1);
  2814. for (int y=0; y<img.height; y++) {
  2815. int ly = (int)map(y, 0, img.height-1, 0, limg.height-1);
  2816. color c = limg.pixels[lx+ly*limg.width];
  2817. int posx = (x+getShift(c, 0)+2*img.width)%img.width;
  2818. int posy = (y+getShift(c, 1)+2*img.height)%img.height;
  2819. color n = img.pixels[posx+posy*img.width];
  2820. buffer.fill(n);
  2821. // fill(red(c),green(c),blue(n)); // work only on blue channel
  2822. // fill(red(n), abs(green(c)-green(n)), blue(n)); // green channel is blended using difference method
  2823. buffer.rect(x, y, 1, 1);
  2824. }
  2825. }
  2826. buffer.endDraw();
  2827. img.updatePixels();
  2828. limg.updatePixels();
  2829. canvas.beginDraw();
  2830. canvas.image(buffer, canvas.width/2, canvas.height/2);
  2831. canvas.endDraw();
  2832. }
  2833. float getChannel(color c, int channel) {
  2834. int ch = channel>5?channel-6:channel;
  2835. float cc;
  2836. switch(ch) {
  2837. case RED:
  2838. cc = red(c);
  2839. break;
  2840. case GREEN:
  2841. cc = green(c);
  2842. break;
  2843. case BLUE:
  2844. cc = blue(c);
  2845. break;
  2846. case HUE:
  2847. cc = hue(c);
  2848. break;
  2849. case SATURATION:
  2850. cc = saturation(c);
  2851. break;
  2852. default:
  2853. cc= brightness(c);
  2854. break;
  2855. }
  2856. return channel>5?255-cc:cc;
  2857. }
  2858. int getShift(color c, int idx) {
  2859. float cc = getChannel(c, channels[idx]);
  2860. switch(types[idx]) {
  2861. case LINEAR:
  2862. return (int)(facts[idx] * cc/255.0);
  2863. case POWER:
  2864. return (int)(facts[idx]*map(pow(cc/255.0, power_vals[idx]), 0, 1, -1, 1));
  2865. case SINUSOIDAL:
  2866. return (int)(facts[idx]*sin(map(cc, 0, 255, -PI, PI)));
  2867. default:
  2868. { // POLAR
  2869. float c1 = idx==0?cc:getChannel(c, channels[1]);
  2870. float c2 = idx==1?cc:getChannel(c, channels[0]);
  2871. float ang = map(c1, 0, 255, 0, TWO_PI);
  2872. float r = map(c2, 0, 255, 0, facts[0]);
  2873. return (int)(idx==0?r*cos(ang):r*sin(ang));
  2874. }
  2875. }
  2876. }
  2877. }
  2878. /*
  2879. SLICER
  2880. */
  2881. class SLICER extends Shader {
  2882. ArrayList<Edge> edges = new ArrayList<Edge>();
  2883. SegmentNode[] elements;
  2884. Map<Integer, S> m = new HashMap<Integer, S>();
  2885. int max_patterns = 20; // CAN I KILL THIS?
  2886. float max_rotation = TWO_PI; // random or fixed rotation up to specified angle, 0 - TWO_PI, 0 - no rotation
  2887. boolean fixed_rotation = true; // fixed or random rotation
  2888. int thr_min = -1; // you can change color of segment for specified brightness threshold
  2889. int thr_max = -1; // set to -1 if you don't want to use it, values from 0 to 255
  2890. color[] palette = { #000000, #E7CBB3, #CEC6AF, #A0C3BC, #7C7F84, #ffffff }; // choose colors for palette you want to use with threshold, colors will be choosen randomly
  2891. // SEPARATE config
  2892. float max_shift = 2; // max expected shift, using gaussian random, so bigger number, more distortion
  2893. int type = ROTATE; // choose type of distortion: PATTERNS, SEPARATE, ROTATE, SHIFTCOPY, SORT
  2894. boolean do_blend = false; // blend result with original?
  2895. int blend_type = SUBTRACT; // list here: https://processing.org/reference/blend_.html
  2896. // segmentation config START
  2897. float threshold = 500.0; // higher number - bigger segments
  2898. int min_comp_size = 200; // minimal segment size (in pixels), minimum 10
  2899. int blur = 0; // sometimes it's good to blur image to have less sharp segment edges, 0 = off, blur > 0 - blur kernel size
  2900. int stat_type = DIST; // edge calculation method
  2901. final static int DIST = 0;
  2902. final static int HUE = 1;
  2903. final static int BRIGHTNESS = 2;
  2904. final static int SATURATION = 3;
  2905. final static int ABSDIST = 4;
  2906. // do not touch it
  2907. PImage img, mimg; // load image to this variable
  2908. int num;
  2909. // segmentation config END
  2910. // do not touch, list of types
  2911. final static int PATTERNS = 0; // fill segments with patterns
  2912. final static int SEPARATE = 1; // separate segments (black background visible)
  2913. final static int ROTATE = 2; // rotate content of the segments
  2914. final static int SHIFTCOPY = 3; // copy content from shifted image
  2915. final static int SORT = 4; // sort segments by color value
  2916. // working buffer
  2917. PGraphics buffer;
  2918. SLICER() {
  2919. name = "fxSlicer";
  2920. params.add(new Param("mask layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  2921. params.add(new Param("max rotation", FLOATVAL, 0, TWO_PI, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2922. params.add(new Param("min threshold", INTVAL, -1, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2923. params.add(new Param("max threshold", INTVAL, -1, 255, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2924. params.add(new Param("max shift", FLOATVAL, 0, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2925. params.add(new Param("type", INTVAL, 0, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2926. params.add(new Param("do blend", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2927. params.add(new Param("blend type", INTVAL, 0, blends.length-1, new int[]{RANDOM}));
  2928. params.add(new Param("threshold", FLOATVAL, 100, 2000, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2929. params.add(new Param("min comp size", INTVAL, 10, 500, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2930. params.add(new Param("blur", INTVAL, 0, 6, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2931. params.add(new Param("stat type", INTVAL, 0, 6, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  2932. params.add(new Param("fixed rotation", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  2933. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  2934. directionParamIndex = 13;
  2935. shaderListLength = gui.shaderList.size();
  2936. img = createImage(canvas.width, canvas.height, ARGB);
  2937. mimg = createImage(canvas.width, canvas.height, ARGB);
  2938. buffer = createGraphics(img.width, img.height);
  2939. buffer.beginDraw();
  2940. buffer.noStroke();
  2941. buffer.smooth(8);
  2942. buffer.background(0);
  2943. buffer.endDraw();
  2944. }
  2945. int rw, rh, shaderListLength;
  2946. void apply() {
  2947. if (canvas.width != rw || canvas.height != rh) {
  2948. rw = canvas.width;
  2949. rh = canvas.height;
  2950. buffer = createGraphics(rw, rh);
  2951. buffer.beginDraw();
  2952. buffer.noStroke();
  2953. buffer.endDraw();
  2954. img.resize(rw, rh);
  2955. mimg.resize(rw, rh);
  2956. }
  2957. if (shaderListLength != gui.shaderList.size()) {
  2958. shaderListLength = gui.shaderList.size();
  2959. changeParam(0, new Param("mask layer", INTVAL, 0, shaderListLength-1, new int[]{RANDOM}));
  2960. }
  2961. img = canvas.get();
  2962. if ((int)params.get(0).value == pos) {
  2963. mimg = canvas.get();
  2964. } else {
  2965. mimg = gui.shaderList.get((int)params.get(0).value).result.get();
  2966. }
  2967. max_rotation = params.get(1).value;
  2968. thr_min = (int)params.get(2).value;
  2969. thr_max = (int)params.get(3).value;
  2970. max_shift = params.get(4).value;
  2971. type = (int)params.get(5).value;
  2972. do_blend = boolean((int)params.get(6).value);
  2973. blend_type = blends[(int)params.get(7).value];
  2974. threshold = params.get(8).value;
  2975. min_comp_size = (int)params.get(9).value;
  2976. blur = (int)params.get(10).value;
  2977. stat_type = (int)params.get(11).value;
  2978. fixed_rotation = boolean((int)params.get(12).value);
  2979. //println("");
  2980. //println("Processing...");
  2981. edges.clear();
  2982. makeEdges();
  2983. calculateSegmentation();
  2984. buffer.beginDraw();
  2985. m.clear();
  2986. if (type == PATTERNS) preparePatterns();
  2987. for (int x=0; x<img.width; x++)
  2988. for (int y=0; y<img.height; y++) {
  2989. Integer segment = findEnd(y*img.width+x);
  2990. S segm;
  2991. if (m.containsKey(segment)) {
  2992. segm = m.get(segment);
  2993. if (type == SORT) sortHelper(segm, x, y);
  2994. } else {
  2995. segm = new S();
  2996. segm.rots = sin(fixed_rotation?max_rotation:random(max_rotation));
  2997. segm.rotc = cos(fixed_rotation?max_rotation:random(max_rotation));
  2998. segm.c = img.get(x, y);
  2999. segm.dx = (int)(max_shift*randomGaussian());
  3000. segm.dy = (int)(max_shift*randomGaussian());
  3001. segm.x = x;
  3002. segm.y = y;
  3003. if (brightness(segm.c)>=thr_min && brightness(segm.c)<=thr_max) segm.c = palette[(int)random(palette.length)];
  3004. if (type == PATTERNS) {
  3005. segm.pat = patterns[(int)random(max_patterns)];
  3006. segm.pats = random(0.5, 10);
  3007. }
  3008. if (type == SORT) {
  3009. segm.xy = random(1)<0.5;
  3010. int size = elements[segment].size;
  3011. segm.clrs = new color[size];
  3012. segm.positions = new int[size];
  3013. segm.iters = 0;
  3014. sortHelper(segm, x, y);
  3015. }
  3016. m.put(segment, segm);
  3017. }
  3018. if (type == PATTERNS) {
  3019. int vx = segm.x-x;
  3020. int vy = segm.y-y;
  3021. float sinr = segm.rots;
  3022. float cosr = segm.rotc;
  3023. int imgx = int(segm.pat.width+x+(cosr*vx-sinr*vy))%segm.pat.width;
  3024. int imgy = int(segm.pat.height+x+(sinr*vx+cosr*vy))%segm.pat.height;
  3025. buffer.fill(segm.pat.get(imgx, imgy));
  3026. buffer.rect(x, y, 1, 1);
  3027. } else if (type == SEPARATE) {
  3028. buffer.fill(segm.c);
  3029. buffer.rect(x+segm.dx, y+segm.dy, 1, 1);
  3030. } else if (type == SHIFTCOPY) {
  3031. int imgx = (2*x-segm.x)%img.width;
  3032. int imgy = (2*y-segm.y)%img.height;
  3033. buffer.fill(img.get(imgx, imgy));
  3034. buffer.rect(x, y, 1, 1);
  3035. } else if (type == ROTATE) {
  3036. int vx = segm.x-x;
  3037. int vy = segm.y-y;
  3038. float sinr = segm.rots;
  3039. float cosr = segm.rotc;
  3040. int imgx = int(img.width+x+(cosr*vx-sinr*vy))%img.width;
  3041. int imgy = int(img.height+y+(sinr*vx+cosr*vy))%img.height;
  3042. buffer.fill(img.get(imgx, imgy));
  3043. buffer.rect(x, y, 1, 1);
  3044. }
  3045. }
  3046. if (type == SORT) {
  3047. for (Integer key : m.keySet()) {
  3048. S segm = m.get(key);
  3049. segm.clrs = sort(segm.clrs);
  3050. segm.positions = sort(segm.positions);
  3051. }
  3052. for (Integer key : m.keySet()) {
  3053. S segm = m.get(key);
  3054. for (int i=0; i<segm.positions.length; i++) {
  3055. int x, y;
  3056. if (segm.xy) {
  3057. x = (segm.positions[i] >> 16) & 0xffff;
  3058. y = segm.positions[i] & 0xffff;
  3059. } else {
  3060. y = (segm.positions[i] >> 16) & 0xffff;
  3061. x = segm.positions[i] & 0xffff;
  3062. }
  3063. buffer.fill(segm.clrs[i]);
  3064. buffer.rect(x, y, 1, 1);
  3065. }
  3066. }
  3067. }
  3068. if (do_blend)
  3069. buffer.blend(img, 0, 0, img.width, img.height, 0, 0, buffer.width, buffer.height, blend_type);
  3070. buffer.endDraw();
  3071. canvas.beginDraw();
  3072. canvas.image(buffer, canvas.width/2, canvas.height/2);
  3073. canvas.endDraw();
  3074. }
  3075. void sortHelper(S segm, int x, int y) {
  3076. int xyval;
  3077. if (segm.xy)
  3078. xyval = (x << 16) | y;
  3079. else
  3080. xyval = (y << 16) | x;
  3081. segm.clrs[segm.iters] = img.get(x, y);
  3082. segm.positions[segm.iters++] = xyval;
  3083. }
  3084. final float getStat(color c1, color c2) {
  3085. switch(stat_type) {
  3086. case HUE:
  3087. abs(hue(c1)-hue(c2));
  3088. case BRIGHTNESS:
  3089. abs(brightness(c1)-brightness(c2));
  3090. case SATURATION:
  3091. abs(saturation(c1)-saturation(c2));
  3092. case ABSDIST:
  3093. return abs(red(c1)-red(c2)) + abs(green(c1)-green(c2)) + abs(blue(c1)-blue(c2));
  3094. default:
  3095. return sq(red(c1)-red(c2)) + sq(green(c1)-green(c2)) + sq(blue(c1)-blue(c2));
  3096. }
  3097. }
  3098. void makeEdges() {
  3099. PImage img2 = mimg.get(0, 0, img.width, img.height);
  3100. if (blur > 0) img2.filter(BLUR, blur);
  3101. // make grid each point connected to neighbours
  3102. for (int x=0; x<img2.width; x++)
  3103. for (int y=0; y<img2.height; y++) {
  3104. color c = img2.get(x, y);
  3105. if (x<img2.width-1) {
  3106. Edge e = new Edge();
  3107. e.a = y*img2.width+x;
  3108. e.b = y*img2.width+x+1;
  3109. e.weight = getStat(c, img2.get(x+1, y));
  3110. edges.add(e);
  3111. }
  3112. if (y<img2.height-1) {
  3113. Edge e = new Edge();
  3114. e.a = y*img2.width+x;
  3115. e.b = (y+1)*img2.width+x;
  3116. e.weight = getStat(c, img2.get(x, y+1));
  3117. edges.add(e);
  3118. }
  3119. if ( (x<img2.width-1) && (y<img2.height-1)) {
  3120. Edge e = new Edge();
  3121. e.a = y*img2.width+x;
  3122. e.b = (y+1)*img2.width+x+1;
  3123. e.weight = getStat(c, img2.get(x+1, y+1));
  3124. edges.add(e);
  3125. }
  3126. if ( (x<img2.width-1) && (y>0)) {
  3127. Edge e = new Edge();
  3128. e.a = y*img2.width+x;
  3129. e.b = (y-1)*img2.width+x+1;
  3130. e.weight = getStat(c, img2.get(x+1, y-1));
  3131. edges.add(e);
  3132. }
  3133. }
  3134. // sort edges
  3135. Collections.sort(edges);
  3136. }
  3137. int findEnd(int x) {
  3138. int y = x;
  3139. while (y != elements[y].parent) y = elements[y].parent;
  3140. elements[x].parent = y;
  3141. return y;
  3142. }
  3143. void joinSegments(int x, int y) {
  3144. if (elements[x].rank > elements[y].rank) {
  3145. elements[y].parent = x;
  3146. elements[x].size += elements[y].size;
  3147. } else {
  3148. elements[x].parent = y;
  3149. elements[y].size += elements[x].size;
  3150. if (elements[x].rank == elements[y].rank) elements[y].rank++;
  3151. }
  3152. num--;
  3153. }
  3154. void calculateSegmentation() {
  3155. int no_vertices = img.width*img.height;
  3156. num = no_vertices;
  3157. elements = new SegmentNode[no_vertices];
  3158. // init nodes
  3159. for (int i=0; i<no_vertices; i++) {
  3160. SegmentNode s = new SegmentNode();
  3161. s.rank = 0;
  3162. s.size = 1;
  3163. s.parent = i;
  3164. elements[i]=s;
  3165. }
  3166. float[] thresholds = new float[no_vertices];
  3167. Arrays.fill(thresholds, threshold);
  3168. for (Edge edge : edges) {
  3169. int a = findEnd(edge.a);
  3170. int b = findEnd(edge.b);
  3171. if (a!=b) {
  3172. if (edge.weight <= thresholds[a] && edge.weight <= thresholds[b]) {
  3173. joinSegments(a, b);
  3174. a = findEnd(a);
  3175. thresholds[a] = edge.weight + threshold/elements[a].size;
  3176. }
  3177. }
  3178. }
  3179. for (Edge edge : edges) {
  3180. int a = findEnd(edge.a);
  3181. int b = findEnd(edge.b);
  3182. if ( (a != b) && ((elements[a].size < min_comp_size) || (elements[b].size < min_comp_size))) {
  3183. joinSegments(a, b);
  3184. }
  3185. }
  3186. //println("Segments: " +num);
  3187. }
  3188. PImage[] patterns;
  3189. void preparePatterns() {
  3190. patterns = new PImage[max_patterns];
  3191. for (int i=0; i<max_patterns; i++) {
  3192. patterns[i] = getSelfPattern();
  3193. }
  3194. }
  3195. PImage getSelfPattern() {
  3196. int x = (int)random(img.width-50);
  3197. int y = (int)random(img.height-50);
  3198. int sx = (int)random(50, img.width-x-1);
  3199. int sy = (int)random(50, img.height-y-1);
  3200. return img.get(x, y, sx, sy);
  3201. }
  3202. PImage getLayerPattern(int layer) { //use me
  3203. int x = (int)random(gui.shaderList.get(layer).canvas.width-50);
  3204. int y = (int)random(gui.shaderList.get(layer).canvas.height-50);
  3205. int sx = (int)random(50, gui.shaderList.get(layer).canvas.width-x-1);
  3206. int sy = (int)random(50, gui.shaderList.get(layer).canvas.height-y-1);
  3207. return gui.shaderList.get(layer).canvas.get(x, y, sx, sy);
  3208. }
  3209. }
  3210. // general class to save distortions for particular segment
  3211. class S {
  3212. color c; // color of the segment root point
  3213. int x, y; // position of segment root point
  3214. int dx, dy; // segment shift
  3215. float rots, rotc, pats;
  3216. PImage pat;
  3217. // sort
  3218. color[] clrs;
  3219. int[] positions;
  3220. boolean xy;
  3221. int iters;
  3222. }
  3223. class Edge implements Comparable {
  3224. int a, b;
  3225. float weight;
  3226. int compareTo(Object o) {
  3227. Edge e = (Edge)o;
  3228. return this.weight<e.weight?-1:this.weight>e.weight?1:0;
  3229. }
  3230. }
  3231. class SegmentNode {
  3232. int rank, parent, size;
  3233. }
  3234. /*
  3235. STREAKER
  3236. */
  3237. class STREAKER extends Shader {
  3238. final int EXCLUSIVE = 0;
  3239. final int LESSER = 1;
  3240. final int GREATER = 2;
  3241. final int CLASSIC = 0;
  3242. final int RGBSUM = 1;
  3243. final int BRIGHT = 2;
  3244. PImage img;
  3245. float tol = 100.0; // tolerance
  3246. int mode = LESSER; // EXCLUSIVE, LESSER, GREATER
  3247. int sum = BRIGHT; // CLASSIC, RGBSUM, BRIGHT
  3248. int lim = 1; // move divisor
  3249. int iterations = 1;
  3250. float rand = 5;
  3251. boolean h = false;
  3252. boolean v = true;
  3253. boolean Hrev = false;
  3254. boolean Vrev = false;
  3255. int W, H;
  3256. STREAKER() {
  3257. name = "fxStreaker";
  3258. img = createImage(canvas.width, canvas.height, ARGB);
  3259. params.add(new Param("vertical/horizontal/both", INTVAL, 0, 2, new int[]{RANDOM}));
  3260. params.add(new Param("verticals up / down", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  3261. params.add(new Param("horicontals up / down", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  3262. params.add(new Param("tolerance", FLOATVAL, 0, 100, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  3263. params.add(new Param("tolerance randomness", FLOATVAL, 0, 50, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  3264. params.add(new Param("detection mode (EXCLUSIVE, LESSER, GREATER)", INTVAL, 0, 2, new int[]{RANDOM}));
  3265. params.add(new Param("sum (RBG sum, brightness, R>G>B)", INTVAL, 0, 2, new int[]{RANDOM}));
  3266. params.add(new Param("move divisor", INTVAL, 1, 5, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  3267. params.add(new Param("iterations", INTVAL, 1, 10, new int[]{SAWTOOTH, TRIANG, SINE, TAN, TANINVERSE, RAMPUPDOWN, RAMP, RAMPINVERSE}));
  3268. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  3269. directionParamIndex = 9;
  3270. W = canvas.width;
  3271. H = canvas.height;
  3272. }
  3273. void apply() {
  3274. if (W != canvas.width || H != canvas.height) {
  3275. W = canvas.width;
  3276. H = canvas.height;
  3277. img.resize(W, H);
  3278. }
  3279. img = canvas.get();
  3280. int p0 = (int)params.get(0).value;
  3281. v = (p0 == 0 || p0 == 2) ? true : false;
  3282. h = (p0 == 1 || p0 == 2) ? true : false;
  3283. Vrev = boolean((int)params.get(1).value);
  3284. Hrev = boolean((int)params.get(2).value);
  3285. tol = params.get(3).value;
  3286. rand = params.get(4).value;
  3287. mode = (int)params.get(5).value;
  3288. sum = (int)params.get(6).value;
  3289. lim = (int)params.get(7).value;
  3290. iterations = (int)params.get(8).value;
  3291. PGraphics temp = createGraphics(img.width, img.height);
  3292. int off = 0;
  3293. color c;
  3294. boolean a = false;
  3295. for (int i = 0; i < iterations; i++) {
  3296. img.loadPixels();
  3297. temp.beginDraw();
  3298. temp.image(img, 0, 0);
  3299. if (h) {
  3300. if (!Hrev) {
  3301. for (int y = 0; y < img.height-1; y++) {
  3302. for (int x = 0; x < img.width-1; x++) {
  3303. c = img.pixels[x+(y*img.width)];
  3304. while (check (x, y, x+off+1, y) == true && x+off+1 < img.width) {
  3305. off++;
  3306. a = true;
  3307. }
  3308. if (a) {
  3309. temp.stroke(c);
  3310. temp.line(x, y, x+(off/lim), y);
  3311. x = x+(off/lim);
  3312. a = false;
  3313. off = 0;
  3314. }
  3315. }
  3316. }
  3317. } else { // hrev
  3318. for (int y = 0; y < img.height-1; y++) {
  3319. for (int x = img.width-1; x > 1; x--) {
  3320. c = img.pixels[x+(y*img.width)];
  3321. while (check (x, y, x-off-1, y) == true && x-off-1 > 0) {
  3322. off++;
  3323. a = true;
  3324. }
  3325. if (a) {
  3326. temp.stroke(c);
  3327. temp.line(x, y, x-(off/lim), y);
  3328. x = x-(off/lim);
  3329. a = false;
  3330. off = 0;
  3331. }
  3332. }
  3333. }
  3334. }
  3335. }
  3336. if (v) {
  3337. if (!Vrev) {
  3338. for (int x = 0; x < img.width-1; x++) {
  3339. for (int y = 0; y < img.height-1; y++) {
  3340. c = img.pixels[x+(y*img.width)];
  3341. while (check (x, y, x, y+off+1) == true && y+off+1 < img.height-1 ) {
  3342. off++;
  3343. a = true;
  3344. }
  3345. if (a) {
  3346. temp.stroke(c);
  3347. temp.line(x, y, x, y+(off/lim));
  3348. y = y+(off/lim);
  3349. a = false;
  3350. off = 0;
  3351. }
  3352. }
  3353. }
  3354. } else { //Vrev
  3355. for (int x = 0; x < img.width-1; x++) {
  3356. for (int y = img.height-1; y > 1; y--) {
  3357. c = img.pixels[x+(y*img.width)];
  3358. while (check (x, y, x, y-off-1) == true && y-off-1 > 0) {
  3359. off++;
  3360. a = true;
  3361. }
  3362. if (a) {
  3363. temp.stroke(c);
  3364. temp.line(x, y, x, y-(off/lim));
  3365. y = y-(off/lim);
  3366. a = false;
  3367. off = 0;
  3368. }
  3369. }
  3370. }
  3371. }
  3372. }
  3373. temp.endDraw();
  3374. img = temp.get();
  3375. img.updatePixels();
  3376. }
  3377. canvas.beginDraw();
  3378. canvas.image(img, canvas.width/2, canvas.height/2);
  3379. canvas.endDraw();
  3380. }
  3381. boolean check(int x, int y, int xc, int yc) {
  3382. float src, dst, r;
  3383. r = (rand != 0) ? random(0, rand) : 0;
  3384. switch(sum) {
  3385. case 1: //rgbsum
  3386. src = rgbsum(x, y);
  3387. dst = rgbsum(xc, yc);
  3388. break;
  3389. case 2: // brightness
  3390. src = brightness(img.pixels[x+(y*img.width)]);
  3391. dst = brightness(img.pixels[xc+(yc*img.width)]);
  3392. break;
  3393. default: // 'classic' or asdfish
  3394. src = map(img.pixels[x+(y*img.width)], 0, 0xFFFFFF, 0.0, 255.0);
  3395. dst = map(img.pixels[xc+(yc*img.width)], 0, 0xFFFFFF, 0.0, 255.0);
  3396. break;
  3397. }
  3398. if (mode == 0 && (src < dst-tol-r || src > dst+tol+r)) return true;
  3399. else if (mode == 1 && src > dst+tol-r) return true;
  3400. else if (mode == 2 && src < dst-tol+r) return true;
  3401. else return false;
  3402. }
  3403. float rgbsum(int x, int y) {
  3404. int r = img.pixels[x+(y*img.width)] >> 24 & 0xff;
  3405. int g = img.pixels[x+(y*img.width)] >> 16 & 0xff;
  3406. int b = img.pixels[x+(y*img.width)] & 0xff;
  3407. float result = (r + g + b) / 3;
  3408. return result;
  3409. }
  3410. }
  3411. /*
  3412. SEGMENTER
  3413. */
  3414. class SEGMENTER extends Shader {
  3415. int thres = 64; // threshold
  3416. int mode = 0; // lighter/darker, essentially 'reverses' the sort
  3417. boolean diag = true; // diagonal/straight
  3418. boolean v = true; // use to select initial direction.
  3419. boolean h = true; // if cycling, use V=true H=false.
  3420. boolean choice = false;
  3421. int iterations;
  3422. PImage img;
  3423. int[] bounds = {0, 0, 0, 0};
  3424. boolean running = true;
  3425. SEGMENTER() {
  3426. name = "fxSegmenter";
  3427. params.add(new Param("threshold", INTVAL, 8, 252, new int[]{SINE, TRIANG}));
  3428. params.add(new Param("mode", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  3429. params.add(new Param("diagonal", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  3430. params.add(new Param("horizontal", INTVAL, 0, 1, new int[]{RANDOM, SQUAR}));
  3431. params.add(new Param("vertical", INTVAL, 0, 1, new int[] {RANDOM, SQUAR}));
  3432. params.add(new Param("weirdo pattern mode", INTVAL, 0, 1, new int[] {RANDOM, SQUAR}));
  3433. params.add(new Param("iterations", INTVAL, 1, 100, new int[] {SINE, TRIANG}));
  3434. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  3435. directionParamIndex = 7;
  3436. img = createImage(canvas.width, canvas.height, ARGB);
  3437. canvas.beginDraw();
  3438. canvas.colorMode(HSB);
  3439. canvas.endDraw();
  3440. }
  3441. int rw, rh;
  3442. void apply() {
  3443. if (rw != canvas.width || rh != canvas.height) {
  3444. rw = canvas.width;
  3445. rh = canvas.height;
  3446. img.resize(rw, rh);
  3447. bounds[0]= 0;
  3448. bounds[1]= 0;
  3449. bounds[2]= rw;
  3450. bounds[3]= rh;
  3451. }
  3452. img = canvas.get();
  3453. thres = (int)params.get(0).value;
  3454. mode = (int)params.get(1).value;
  3455. diag = boolean((int)params.get(2).value);
  3456. v = boolean((int)params.get(3).value);
  3457. h = boolean((int)params.get(4).value);
  3458. choice = boolean((int)params.get(5).value);
  3459. iterations = (int)params.get(6).value;
  3460. canvas.beginDraw();
  3461. canvas.image(img, canvas.width/2, canvas.height/2);
  3462. canvas.endDraw();
  3463. canvas.beginDraw();
  3464. for (int i = 0; i < iterations; i++) {
  3465. canvas.loadPixels();
  3466. for (int j = bounds[1]; j < bounds[3]-1; j++) {
  3467. for (int k = bounds[0]; k < bounds[2]-1; k++) {
  3468. float bright = canvas.brightness(canvas.get(k, j));
  3469. color c;
  3470. if (!choice) {
  3471. if (diag) {
  3472. if (mode == 0) {
  3473. if (h && bright < abs(thres)&& k > bounds[0]+1 && j > bounds[1]+1 && (bright < canvas.brightness(canvas.pixels[k-1+((j-1)*canvas.width)]))) {
  3474. swap(k, j, k-1, j-1);
  3475. } else if (v && bright < abs(thres*2) && j > bounds[1]+1 && k < bounds[2]-1 && (bright < canvas.brightness(canvas.pixels[k+1+((j-1)*canvas.width)]))) {
  3476. swap(k, j, k+1, j-1);
  3477. } else if (h && bright < abs(thres*3) && k < bounds[2]-1 && j < bounds[3]-1 && (bright < canvas.brightness(canvas.pixels[k+1+((j+1)*canvas.width)]))) {
  3478. swap(k, j, k+1, j+1);
  3479. } else if (v && k > bounds[0] && j < bounds[3]-1 && (bright < canvas.brightness(canvas.pixels[k-1+((j+1)*canvas.width)]))) {
  3480. swap(k, j, k-1, j+1);
  3481. }
  3482. } else { // mode
  3483. if (h && bright < abs(thres)&& k > bounds[0] && j > bounds[1] && (bright > canvas.brightness(canvas.pixels[k-1+((j-1)*canvas.width)]))) {
  3484. swap(k, j, k-1, j-1);
  3485. } else if (v && bright < abs(thres*2) && j > bounds[1] && k < bounds[2]-1 && (bright > canvas.brightness(canvas.pixels[k+1+((j-1)*canvas.width)]))) {
  3486. swap(k, j, k+1, j-1);
  3487. } else if (h && bright < abs(thres*3) && k < bounds[2]-1 && j < bounds[3]-1 && (bright > canvas.brightness(canvas.pixels[k+1+((j+1)*canvas.width)]))) {
  3488. swap(k, j, k+1, j+1);
  3489. } else if (v && k > bounds[0] && j < bounds[3]-1 && (bright > canvas.brightness(canvas.pixels[k-1+((j+1)*canvas.width)]))) {
  3490. swap(k, j, k-1, j+1);
  3491. }
  3492. } // mode
  3493. }//diag
  3494. else {
  3495. if (mode == 0) {
  3496. if (h && bright < abs(thres)&& k > bounds[0] && (bright < canvas.brightness(canvas.get(k-1, j)))) {
  3497. swap(k, j, k-1, j);
  3498. } else if (v && bright < abs(thres*2) && j > bounds[1] && (bright < canvas.brightness(canvas.get(k, j-1)))) {
  3499. swap(k, j, k, j-1);
  3500. } else if (h && bright < abs(thres*3) && k < bounds[2]-1 && (bright < canvas.brightness(canvas.get(k+1, j)))) {
  3501. swap(k, j, k+1, j);
  3502. } else if (v && k > bounds[0] && j < bounds[3]-1 && (bright < canvas.brightness(canvas.get(k, j+1)))) {
  3503. swap(k, j, k, j+1);
  3504. }
  3505. } else { // mode
  3506. if (h && bright < abs(thres)&& k > bounds[0] && (bright > canvas.brightness(canvas.get(k-1, j)))) {
  3507. swap(k, j, k-1, j);
  3508. } else if (v && bright < abs(thres*2) && j > bounds[1] && (bright > canvas.brightness(canvas.get(k, j-1)))) {
  3509. swap(k, j, k, j-1);
  3510. } else if (h && bright < abs(thres*3) && k < bounds[2]-1 && (bright > canvas.brightness(canvas.get(k+1, j)))) {
  3511. swap(k, j, k+1, j);
  3512. } else if (v && k > bounds[0] && j < bounds[3]-1 && (bright > canvas.brightness(canvas.get(k, j+1)))) {
  3513. swap(k, j, k, j+1);
  3514. }
  3515. } // mode
  3516. } // diag
  3517. } else { //choice
  3518. //weirdo pattern mode
  3519. if (mode == 0) {
  3520. if (bright < thres && k-1+((j-1)*canvas.width) > 0) {
  3521. swap(k, j, k-1, j-1);
  3522. } else if (bright < abs(thres*2) && k+1+((j-1)*canvas.width) > 0) {
  3523. swap(k, j, k+1, j-1);
  3524. } else if (bright < abs(thres*3) && k+1+((j+1)*canvas.width) < canvas.width*canvas.height) {
  3525. swap(k, j, k+1, j+1);
  3526. } else if (k-1+((j+1)*canvas.width) < canvas.width*canvas.height) {
  3527. swap(k, j, k-1, j+1);
  3528. }
  3529. } else { // mode
  3530. if (bright > abs(thres*3) && k-1+((j-1)*canvas.width) > 0) {
  3531. if (k-1+((j-1)*canvas.width) > 0) {
  3532. swap(k, j, k-1, j-1);
  3533. }
  3534. } else
  3535. if (bright > abs(thres*2) && k+1+((j-1)*canvas.width) > 0) {
  3536. if (k+1+((j-1)*canvas.width) > 0) {
  3537. swap(k, j, k+1, j-1);
  3538. }
  3539. } else
  3540. if (bright < thres && k+1+((j+1)*canvas.width) < canvas.width*canvas.height) {
  3541. swap(k, j, k+1, j+1);
  3542. } else if (k-1+((j+1)*canvas.width) < canvas.width*canvas.height) {
  3543. swap(k, j, k-1, j+1);
  3544. }
  3545. } // mode
  3546. }//choice
  3547. }//hori loop
  3548. }//vert loop
  3549. canvas.updatePixels();
  3550. }//iterations
  3551. canvas.endDraw();
  3552. }
  3553. void swap(int x, int y, int xc, int yc) {
  3554. // if (x+(y*width) < width*height && xc+(yc*width) < width*height && xc+(yc*width) > -1) {
  3555. color c = canvas.pixels[x+(y*canvas.width)];
  3556. canvas.pixels[x+(y*canvas.width)] = canvas.pixels[xc+(yc*canvas.width)];
  3557. canvas.pixels[xc+((yc)*canvas.width)] = c;
  3558. // }
  3559. }
  3560. }
  3561. /*
  3562. COLORCRUSHER
  3563. */
  3564. class COLORCRUSHER extends Shader {
  3565. float c;
  3566. int m = 5;
  3567. COLORCRUSHER() {
  3568. name = "fxColorCrusher";
  3569. params.add(new Param("multiplier", INTVAL, 2, 10, new int[]{SINE, TAN, SAWTOOTH, TRIANG}));
  3570. }
  3571. void apply() {
  3572. m = (int)params.get(0).value;
  3573. canvas.beginDraw();
  3574. canvas.loadPixels();
  3575. for (int i = 0; i < canvas.pixels.length; i++) {
  3576. c = color(canvas.pixels[i]);
  3577. canvas.pixels[i] = int(c*m);
  3578. }
  3579. canvas.updatePixels();
  3580. canvas.endDraw();
  3581. }
  3582. }
  3583. /*
  3584. RAINBOWHUE
  3585. */
  3586. //...
  3587. /*
  3588. JPGHexGlitch
  3589. by Victor Giers
  3590. */
  3591. class JPGHEXGLITCH extends Shader {
  3592. PImage img;
  3593. byte[] brokenfile;
  3594. JPGHEXGLITCH() {
  3595. name = "fxJPGHexGlitch";
  3596. params.add(new Param("byte amount to change probability", INTVAL, 2, 20, new int[]{RANDOM}));
  3597. params.add(new Param("iterations", INTVAL, 1, 10, new int[]{RANDOM}));
  3598. params.add(new Param("direction", INTVAL, 0, 3, new int[]{RANDOM}));
  3599. directionParamIndex = 2;
  3600. rw = canvas.width;
  3601. rh = canvas.height;
  3602. img = createImage(rw, rh, ARGB);
  3603. }
  3604. int rw, rh;
  3605. void apply() {
  3606. if (rw != canvas.width || rh != canvas.height) {
  3607. rw = canvas.width;
  3608. rh = canvas.height;
  3609. img.resize(rw, rh);
  3610. }
  3611. int probparam = (int)params.get(0).value;
  3612. int iterations = (int)params.get(1).value;
  3613. img = canvas.get();
  3614. canvas.beginDraw();
  3615. for (int k = 0; k < iterations; k++) {
  3616. canvas.image(img, canvas.width/2, canvas.height/2);
  3617. canvas.save(dataPath("")+"/JPGHex.jpg"); //save as jpg
  3618. brokenfile = loadBytes(dataPath("")+"/JPGHex.jpg"); //and reload. just in case it wasnt a jpg.
  3619. double probability = float(probparam) / float(brokenfile.length);
  3620. byte[] savebytes2 = new byte[brokenfile.length];
  3621. boolean headerEnd = false;
  3622. boolean FFfound = false;
  3623. int glitchCount = 0;
  3624. for (int i = 0; i < brokenfile.length; i++) {
  3625. String hexStr = hex(brokenfile[i]);
  3626. if (FFfound && hexStr.equals("DA")) headerEnd = true;
  3627. FFfound = false;
  3628. if (hexStr.equals("FF")) FFfound = true;
  3629. if (headerEnd && random(1)<probability) {
  3630. String randomHex = (str(Character.forDigit((int)random(16), 16)) + str(Character.forDigit((int)random(16), 16))).toUpperCase();
  3631. hexStr = randomHex;
  3632. glitchCount++;
  3633. }
  3634. byte hexByte[] = fromHexString(hexStr);
  3635. for (int j = 0; j < hexByte.length; j++) {
  3636. savebytes2[i] += hexByte[j];
  3637. }
  3638. }
  3639. //println("Glitched " + glitchCount + " bytes");
  3640. saveBytes(dataPath("") + "/result.jpg", savebytes2);
  3641. img = loadImage(dataPath("") + "/result.jpg");
  3642. }
  3643. canvas.endDraw();
  3644. }
  3645. byte[] fromHexString(final String encoded) {
  3646. if ((encoded.length() % 2) != 0)
  3647. throw new IllegalArgumentException("Input string must contain an even number of characters");
  3648. final byte result[] = new byte[encoded.length()/2];
  3649. final char enc[] = encoded.toCharArray();
  3650. for (int i = 0; i < enc.length; i += 2) {
  3651. StringBuilder curr = new StringBuilder(2);
  3652. curr.append(enc[i]).append(enc[i + 1]);
  3653. result[i/2] = (byte) Integer.parseInt(curr.toString(), 16);
  3654. }
  3655. return result;
  3656. }
  3657. }