compositor for 2d glitch effects
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

effects.pde 148KB

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