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

effects.pde 148KB

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