CImg.h 3.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061
  1. /*
  2. #
  3. # File : CImg.h
  4. # ( C++ header file )
  5. #
  6. # Description : C++ Template Image Processing Toolkit.
  7. # This file is the main component of the CImg Library project.
  8. # ( http://cimg.eu )
  9. #
  10. # Project manager : David Tschumperlé
  11. # ( http://tschumperle.users.greyc.fr/ )
  12. #
  13. # A complete list of contributors is available in file 'README.txt'
  14. # distributed within the CImg package.
  15. #
  16. # Licenses : This file is 'dual-licensed', you have to choose one
  17. # of the two licenses below to apply.
  18. #
  19. # CeCILL-C
  20. # The CeCILL-C license is close to the GNU LGPL.
  21. # ( http://cecill.info/licences/Licence_CeCILL-C_V1-en.html )
  22. #
  23. # or CeCILL v2.1
  24. # The CeCILL license is compatible with the GNU GPL.
  25. # ( http://cecill.info/licences/Licence_CeCILL_V2.1-en.html )
  26. #
  27. # This software is governed either by the CeCILL or the CeCILL-C license
  28. # under French law and abiding by the rules of distribution of free software.
  29. # You can use, modify and or redistribute the software under the terms of
  30. # the CeCILL or CeCILL-C licenses as circulated by CEA, CNRS and INRIA
  31. # at the following URL: "http://cecill.info".
  32. #
  33. # As a counterpart to the access to the source code and rights to copy,
  34. # modify and redistribute granted by the license, users are provided only
  35. # with a limited warranty and the software's author, the holder of the
  36. # economic rights, and the successive licensors have only limited
  37. # liability.
  38. #
  39. # In this respect, the user's attention is drawn to the risks associated
  40. # with loading, using, modifying and/or developing or reproducing the
  41. # software by the user in light of its specific status of free software,
  42. # that may mean that it is complicated to manipulate, and that also
  43. # therefore means that it is reserved for developers and experienced
  44. # professionals having in-depth computer knowledge. Users are therefore
  45. # encouraged to load and test the software's suitability as regards their
  46. # requirements in conditions enabling the security of their systems and/or
  47. # data to be ensured and, more generally, to use and operate it in the
  48. # same conditions as regards security.
  49. #
  50. # The fact that you are presently reading this means that you have had
  51. # knowledge of the CeCILL and CeCILL-C licenses and that you accept its terms.
  52. #
  53. */
  54. // Set version number of the library.
  55. #ifndef cimg_version
  56. #define cimg_version 303
  57. /*-----------------------------------------------------------
  58. #
  59. # Test and possibly auto-set CImg configuration variables
  60. # and include required headers.
  61. #
  62. # If you find that the default configuration variables are
  63. # not adapted to your system, you can override their values
  64. # before including the header file "CImg.h"
  65. # (use the #define directive).
  66. #
  67. ------------------------------------------------------------*/
  68. // Include standard C++ headers.
  69. // This is the minimal set of required headers to make CImg-based codes compile.
  70. #include <cstdio>
  71. #include <cstdlib>
  72. #include <cstdarg>
  73. #include <cstring>
  74. #include <cmath>
  75. #include <cfloat>
  76. #include <climits>
  77. #include <ctime>
  78. #include <exception>
  79. #include <algorithm>
  80. #define cimg_str(x) #x
  81. #define cimg_str2(x) cimg_str(x)
  82. // Detect/configure OS variables.
  83. //
  84. // Define 'cimg_OS' to: '0' for an unknown OS (will try to minize library dependencies).
  85. // '1' for a Unix-like OS (Linux, Solaris, BSD, MacOSX, Irix, ...).
  86. // '2' for Microsoft Windows.
  87. // (auto-detection is performed if 'cimg_OS' is not set by the user).
  88. #ifndef cimg_OS
  89. #if defined(unix) || defined(__unix) || defined(__unix__) \
  90. || defined(linux) || defined(__linux) || defined(__linux__) \
  91. || defined(sun) || defined(__sun) \
  92. || defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) \
  93. || defined(__FreeBSD__) || defined (__DragonFly__) \
  94. || defined(sgi) || defined(__sgi) \
  95. || defined(__OSX__) || defined(__MACOSX__) || defined(__APPLE__) \
  96. || defined(__CYGWIN__)
  97. #define cimg_OS 1
  98. #elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \
  99. || defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
  100. #define cimg_OS 2
  101. #else
  102. #define cimg_OS 0
  103. #endif
  104. #elif !(cimg_OS==0 || cimg_OS==1 || cimg_OS==2)
  105. #error CImg Library: Invalid configuration variable 'cimg_OS'.
  106. #error (correct values are '0 = unknown OS', '1 = Unix-like OS', '2 = Microsoft Windows').
  107. #endif
  108. #ifndef cimg_date
  109. #define cimg_date __DATE__
  110. #endif
  111. #ifndef cimg_time
  112. #define cimg_time __TIME__
  113. #endif
  114. // Disable silly warnings on some Microsoft VC++ compilers.
  115. #ifdef _MSC_VER
  116. #pragma warning(push)
  117. #pragma warning(disable:4127)
  118. #pragma warning(disable:4244)
  119. #pragma warning(disable:4311)
  120. #pragma warning(disable:4312)
  121. #pragma warning(disable:4319)
  122. #pragma warning(disable:4512)
  123. #pragma warning(disable:4571)
  124. #pragma warning(disable:4640)
  125. #pragma warning(disable:4706)
  126. #pragma warning(disable:4710)
  127. #pragma warning(disable:4800)
  128. #pragma warning(disable:4804)
  129. #pragma warning(disable:4820)
  130. #pragma warning(disable:4996)
  131. #ifndef _CRT_SECURE_NO_DEPRECATE
  132. #define _CRT_SECURE_NO_DEPRECATE 1
  133. #endif
  134. #ifndef _CRT_SECURE_NO_WARNINGS
  135. #define _CRT_SECURE_NO_WARNINGS 1
  136. #endif
  137. #ifndef _CRT_NONSTDC_NO_DEPRECATE
  138. #define _CRT_NONSTDC_NO_DEPRECATE 1
  139. #endif
  140. #endif
  141. // Define correct string functions for each compiler and OS.
  142. #if cimg_OS==2 && defined(_MSC_VER)
  143. #define cimg_sscanf std::sscanf
  144. #define cimg_sprintf std::sprintf
  145. #define cimg_snprintf cimg::_snprintf
  146. #define cimg_vsnprintf cimg::_vsnprintf
  147. #else
  148. #include <stdio.h>
  149. #if defined(__MACOSX__) || defined(__APPLE__)
  150. #define cimg_sscanf cimg::_sscanf
  151. #define cimg_sprintf cimg::_sprintf
  152. #define cimg_snprintf cimg::_snprintf
  153. #define cimg_vsnprintf cimg::_vsnprintf
  154. #else
  155. #define cimg_sscanf std::sscanf
  156. #define cimg_sprintf std::sprintf
  157. #define cimg_snprintf snprintf
  158. #define cimg_vsnprintf vsnprintf
  159. #endif
  160. #endif
  161. // Include OS-specific headers.
  162. #if cimg_OS==1
  163. #include <sys/types.h>
  164. #include <sys/time.h>
  165. #include <sys/stat.h>
  166. #include <unistd.h>
  167. #include <dirent.h>
  168. #include <fnmatch.h>
  169. #elif cimg_OS==2
  170. #ifndef NOMINMAX
  171. #define NOMINMAX
  172. #endif
  173. #ifndef WIN32_LEAN_AND_MEAN
  174. #define WIN32_LEAN_AND_MEAN
  175. #endif
  176. #include <windows.h>
  177. #ifndef _WIN32_IE
  178. #define _WIN32_IE 0x0400
  179. #endif
  180. #include <shlobj.h>
  181. #include <process.h>
  182. #include <io.h>
  183. enum {FALSE_WIN = 0};
  184. #endif
  185. // Look for C++11 features.
  186. #ifndef cimg_use_cpp11
  187. #if __cplusplus>201100
  188. #define cimg_use_cpp11 1
  189. #else
  190. #define cimg_use_cpp11 0
  191. #endif
  192. #endif
  193. #if cimg_use_cpp11==1
  194. #include <initializer_list>
  195. #include <utility>
  196. #endif
  197. // Convenient macro to define pragma
  198. #ifdef _MSC_VER
  199. #define cimg_pragma(x) __pragma(x)
  200. #else
  201. #define cimg_pragma(x) _Pragma(#x)
  202. #endif
  203. // Define own types 'cimg_long/ulong' and 'cimg_int64/uint64' to ensure portability.
  204. // ( constrained to 'sizeof(cimg_ulong/cimg_long) = sizeof(void*)' and 'sizeof(cimg_int64/cimg_uint64)=8' ).
  205. #if cimg_OS==2
  206. #define cimg_uint64 unsigned __int64
  207. #define cimg_int64 __int64
  208. #define cimg_ulong UINT_PTR
  209. #define cimg_long INT_PTR
  210. #ifdef _MSC_VER
  211. #define cimg_fuint64 "%I64u"
  212. #define cimg_fint64 "%I64d"
  213. #else
  214. #define cimg_fuint64 "%llu"
  215. #define cimg_fint64 "%lld"
  216. #endif
  217. #else
  218. #if UINTPTR_MAX==0xffffffff || defined(__arm__) || defined(_M_ARM) || ((ULONG_MAX)==(UINT_MAX))
  219. #define cimg_uint64 unsigned long long
  220. #define cimg_int64 long long
  221. #define cimg_fuint64 "%llu"
  222. #define cimg_fint64 "%lld"
  223. #else
  224. #define cimg_uint64 unsigned long
  225. #define cimg_int64 long
  226. #define cimg_fuint64 "%lu"
  227. #define cimg_fint64 "%ld"
  228. #endif
  229. #if defined(__arm__) || defined(_M_ARM)
  230. #define cimg_ulong unsigned long long
  231. #define cimg_long long long
  232. #else
  233. #define cimg_ulong unsigned long
  234. #define cimg_long long
  235. #endif
  236. #endif
  237. // Configure filename separator.
  238. //
  239. // Filename separator is set by default to '/', except for Windows where it is '\'.
  240. #ifndef cimg_file_separator
  241. #if cimg_OS==2
  242. #define cimg_file_separator '\\'
  243. #else
  244. #define cimg_file_separator '/'
  245. #endif
  246. #endif
  247. // Configure verbosity of output messages.
  248. //
  249. // Define 'cimg_verbosity' to: '0' to hide library messages (quiet mode).
  250. // '1' to output library messages on the console.
  251. // '2' to output library messages on a basic dialog window (default behavior).
  252. // '3' to do as '1' + add extra warnings (may slow down the code!).
  253. // '4' to do as '2' + add extra warnings (may slow down the code!).
  254. //
  255. // Define 'cimg_strict_warnings' to replace warning messages by exception throwns.
  256. //
  257. // Define 'cimg_use_vt100' to allow output of color messages on VT100-compatible terminals.
  258. #ifndef cimg_verbosity
  259. #if cimg_OS==2
  260. #define cimg_verbosity 2
  261. #else
  262. #define cimg_verbosity 1
  263. #endif
  264. #elif !(cimg_verbosity==0 || cimg_verbosity==1 || cimg_verbosity==2 || cimg_verbosity==3 || cimg_verbosity==4)
  265. #error CImg Library: Configuration variable 'cimg_verbosity' is badly defined.
  266. #error (should be { 0=quiet | 1=console | 2=dialog | 3=console+warnings | 4=dialog+warnings }).
  267. #endif
  268. // Configure OpenMP support.
  269. // (http://www.openmp.org)
  270. //
  271. // Define 'cimg_use_openmp' to enable OpenMP support (requires OpenMP 3.0+).
  272. //
  273. // OpenMP directives are used in many CImg functions to get
  274. // advantages of multi-core CPUs.
  275. #if !defined(cimg_use_openmp)
  276. #ifdef _OPENMP
  277. #define cimg_use_openmp 1
  278. #else
  279. #define cimg_use_openmp 0
  280. #endif
  281. #endif
  282. #if cimg_use_openmp!=0
  283. #include <omp.h>
  284. #define cimg_pragma_openmp(p) cimg_pragma(omp p)
  285. #else
  286. #define cimg_pragma_openmp(p)
  287. #endif
  288. // Configure the 'abort' signal handler (does nothing by default).
  289. // A typical signal handler can be defined in your own source like this:
  290. // #define cimg_abort_test if (is_abort) throw CImgAbortException("")
  291. //
  292. // where 'is_abort' is a boolean variable defined somewhere in your code and reachable in the method.
  293. // 'cimg_abort_test2' does the same but is called more often (in inner loops).
  294. #if defined(cimg_abort_test) && cimg_use_openmp!=0
  295. // Define abort macros to be used with OpenMP.
  296. #ifndef _cimg_abort_init_openmp
  297. #define _cimg_abort_init_openmp bool _cimg_abort_go_openmp = true; cimg::unused(_cimg_abort_go_openmp)
  298. #endif
  299. #ifndef _cimg_abort_try_openmp
  300. #define _cimg_abort_try_openmp if (_cimg_abort_go_openmp) try
  301. #endif
  302. #ifndef _cimg_abort_catch_openmp
  303. #define _cimg_abort_catch_openmp catch (CImgAbortException&) { cimg_pragma(omp atomic) _cimg_abort_go_openmp&=false; }
  304. #endif
  305. #ifndef _cimg_abort_catch_fill_openmp
  306. #define _cimg_abort_catch_fill_openmp \
  307. catch (CImgException& e) { cimg_pragma(omp critical(abort)) CImg<charT>::string(e._message).move_to(is_error); \
  308. cimg_pragma(omp atomic) _cimg_abort_go_openmp&=false; }
  309. #endif
  310. #ifdef cimg_abort_test2
  311. #ifndef _cimg_abort_try_openmp2
  312. #define _cimg_abort_try_openmp2 _cimg_abort_try_openmp
  313. #endif
  314. #ifndef _cimg_abort_catch_openmp2
  315. #define _cimg_abort_catch_openmp2 _cimg_abort_catch_openmp
  316. #endif
  317. #endif
  318. #endif
  319. #ifndef _cimg_abort_init_openmp
  320. #define _cimg_abort_init_openmp
  321. #endif
  322. #ifndef _cimg_abort_try_openmp
  323. #define _cimg_abort_try_openmp
  324. #endif
  325. #ifndef _cimg_abort_catch_openmp
  326. #define _cimg_abort_catch_openmp
  327. #endif
  328. #ifndef _cimg_abort_try_openmp2
  329. #define _cimg_abort_try_openmp2
  330. #endif
  331. #ifndef _cimg_abort_catch_openmp2
  332. #define _cimg_abort_catch_openmp2
  333. #endif
  334. #ifndef _cimg_abort_catch_fill_openmp
  335. #define _cimg_abort_catch_fill_openmp
  336. #endif
  337. #ifndef cimg_abort_init
  338. #define cimg_abort_init
  339. #endif
  340. #ifndef cimg_abort_test
  341. #define cimg_abort_test
  342. #endif
  343. #ifndef cimg_abort_test2
  344. #define cimg_abort_test2
  345. #endif
  346. // Configure display framework.
  347. //
  348. // Define 'cimg_display' to: '0' to disable display capabilities.
  349. // '1' to use the X-Window framework (X11).
  350. // '2' to use the Microsoft GDI32 framework.
  351. #ifndef cimg_display
  352. #if cimg_OS==0
  353. #define cimg_display 0
  354. #elif cimg_OS==1
  355. #define cimg_display 1
  356. #elif cimg_OS==2
  357. #define cimg_display 2
  358. #endif
  359. #elif !(cimg_display==0 || cimg_display==1 || cimg_display==2)
  360. #error CImg Library: Configuration variable 'cimg_display' is badly defined.
  361. #error (should be { 0=none | 1=X-Window (X11) | 2=Microsoft GDI32 }).
  362. #endif
  363. // Include display-specific headers.
  364. #if cimg_display==1
  365. #include <X11/Xlib.h>
  366. #include <X11/Xutil.h>
  367. #include <X11/keysym.h>
  368. #include <pthread.h>
  369. #ifdef cimg_use_xshm
  370. #include <sys/ipc.h>
  371. #include <sys/shm.h>
  372. #include <X11/extensions/XShm.h>
  373. #endif
  374. #ifdef cimg_use_xrandr
  375. #include <X11/extensions/Xrandr.h>
  376. #endif
  377. #endif
  378. #ifndef cimg_appname
  379. #define cimg_appname "CImg"
  380. #endif
  381. // Configure OpenCV support.
  382. // (http://opencv.willowgarage.com/wiki/)
  383. //
  384. // Define 'cimg_use_opencv' to enable OpenCV support.
  385. //
  386. // OpenCV library may be used to access images from cameras
  387. // (see method 'CImg<T>::load_camera()').
  388. #ifdef cimg_use_opencv
  389. #ifdef True
  390. #undef True
  391. #define _cimg_redefine_True
  392. #endif
  393. #ifdef False
  394. #undef False
  395. #define _cimg_redefine_False
  396. #endif
  397. #ifdef Status
  398. #undef Status
  399. #define _cimg_redefine_Status
  400. #endif
  401. #include <cstddef>
  402. #include <opencv2/opencv.hpp>
  403. #if CV_MAJOR_VERSION>=3
  404. #define _cimg_fourcc cv::VideoWriter::fourcc
  405. #define _cimg_cap_prop_frame_width cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH
  406. #define _cimg_cap_prop_frame_height cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT
  407. #define _cimg_cap_prop_frame_count cv::VideoCaptureProperties::CAP_PROP_FRAME_COUNT
  408. #else
  409. #define _cimg_fourcc CV_FOURCC
  410. #define _cimg_cap_prop_frame_width CV_CAP_PROP_FRAME_WIDTH
  411. #define _cimg_cap_prop_frame_height CV_CAP_PROP_FRAME_HEIGHT
  412. #define _cimg_cap_prop_frame_count CV_CAP_PROP_FRAME_COUNT
  413. #endif
  414. #endif
  415. // Configure LibPNG support.
  416. // (http://www.libpng.org)
  417. //
  418. // Define 'cimg_use_png' to enable LibPNG support.
  419. //
  420. // PNG library may be used to get a native support of '.png' files.
  421. // (see methods 'CImg<T>::{load,save}_png()'.
  422. #ifdef cimg_use_png
  423. extern "C" {
  424. #include "png.h"
  425. }
  426. #endif
  427. // Configure LibJPEG support.
  428. // (http://en.wikipedia.org/wiki/Libjpeg)
  429. //
  430. // Define 'cimg_use_jpeg' to enable LibJPEG support.
  431. //
  432. // JPEG library may be used to get a native support of '.jpg' files.
  433. // (see methods 'CImg<T>::{load,save}_jpeg()').
  434. #ifdef cimg_use_jpeg
  435. extern "C" {
  436. #include "jpeglib.h"
  437. #include "setjmp.h"
  438. }
  439. #endif
  440. // Configure LibTIFF support.
  441. // (http://www.libtiff.org)
  442. //
  443. // Define 'cimg_use_tiff' to enable LibTIFF support.
  444. //
  445. // TIFF library may be used to get a native support of '.tif' files.
  446. // (see methods 'CImg[List]<T>::{load,save}_tiff()').
  447. #ifdef cimg_use_tiff
  448. extern "C" {
  449. #define uint64 uint64_hack_
  450. #define int64 int64_hack_
  451. #include "tiffio.h"
  452. #undef uint64
  453. #undef int64
  454. }
  455. #endif
  456. // Configure HEIF support
  457. // (https://github.com/strukturag/libheif)
  458. //
  459. // Define 'cimg_use_heif' to enable HEIF support.
  460. //
  461. // HEIF library may be used to get a native support of '.heic' and '.avif' files.
  462. // (see method 'CImg<T>::load_heif()').
  463. #ifdef cimg_use_heif
  464. #include <libheif/heif_cxx.h>
  465. #endif
  466. // Configure LibMINC2 support.
  467. // (http://en.wikibooks.org/wiki/MINC/Reference/MINC2.0_File_Format_Reference)
  468. //
  469. // Define 'cimg_use_minc2' to enable LibMINC2 support.
  470. //
  471. // MINC2 library may be used to get a native support of '.mnc' files.
  472. // (see methods 'CImg<T>::{load,save}_minc2()').
  473. #ifdef cimg_use_minc2
  474. #include "minc_io_simple_volume.h"
  475. #include "minc_1_simple.h"
  476. #include "minc_1_simple_rw.h"
  477. #endif
  478. // Configure Zlib support.
  479. // (http://www.zlib.net)
  480. //
  481. // Define 'cimg_use_zlib' to enable Zlib support.
  482. //
  483. // Zlib library may be used to allow compressed data in '.cimgz' files
  484. // (see methods 'CImg[List]<T>::{load,save}_cimg()').
  485. #ifdef cimg_use_zlib
  486. extern "C" {
  487. #include "zlib.h"
  488. }
  489. #endif
  490. // Configure libcurl support.
  491. // (http://curl.haxx.se/libcurl/)
  492. //
  493. // Define 'cimg_use_curl' to enable libcurl support.
  494. //
  495. // Libcurl may be used to get a native support of file downloading from the network.
  496. // (see method 'cimg::load_network()'.)
  497. #ifdef cimg_use_curl
  498. #include "curl/curl.h"
  499. #endif
  500. // Configure Magick++ support.
  501. // (http://www.imagemagick.org/Magick++)
  502. //
  503. // Define 'cimg_use_magick' to enable Magick++ support.
  504. //
  505. // Magick++ library may be used to get a native support of various image file formats.
  506. // (see methods 'CImg<T>::{load,save}()').
  507. #ifdef cimg_use_magick
  508. #include "Magick++.h"
  509. #endif
  510. // Configure FFTW3 support.
  511. // (http://www.fftw.org)
  512. //
  513. // Define 'cimg_use_fftw3' to enable libFFTW3 support.
  514. //
  515. // FFTW3 library may be used to efficiently compute the Fast Fourier Transform
  516. // of image data, without restriction on the image size.
  517. // (see method 'CImg[List]<T>::FFT()').
  518. #ifdef cimg_use_fftw3
  519. extern "C" {
  520. #include "fftw3.h"
  521. }
  522. #endif
  523. // Configure LibBoard support.
  524. // (http://libboard.sourceforge.net/)
  525. //
  526. // Define 'cimg_use_board' to enable Board support.
  527. //
  528. // Board library may be used to draw 3D objects in vector-graphics canvas
  529. // that can be saved as '.ps' or '.svg' files afterwards.
  530. // (see method 'CImg<T>::draw_object3d()').
  531. #ifdef cimg_use_board
  532. #include "Board.h"
  533. #endif
  534. // Configure OpenEXR support.
  535. // (http://www.openexr.com/)
  536. //
  537. // Define 'cimg_use_openexr' to enable OpenEXR support.
  538. //
  539. // OpenEXR library may be used to get a native support of '.exr' files.
  540. // (see methods 'CImg<T>::{load,save}_exr()').
  541. #ifdef cimg_use_openexr
  542. #if __GNUC__>=5
  543. #pragma GCC diagnostic push
  544. #pragma GCC diagnostic ignored "-Wdeprecated"
  545. #pragma GCC diagnostic ignored "-Wdeprecated-copy"
  546. #pragma GCC diagnostic ignored "-Wshadow"
  547. #endif
  548. #include "ImfRgbaFile.h"
  549. #include "ImfInputFile.h"
  550. #include "ImfChannelList.h"
  551. #include "ImfMatrixAttribute.h"
  552. #include "ImfArray.h"
  553. #if __GNUC__>=5
  554. #pragma GCC diagnostic pop
  555. #endif
  556. #endif
  557. // Configure TinyEXR support.
  558. // (https://github.com/syoyo/tinyexr)
  559. //
  560. // Define 'cimg_use_tinyexr' to enable TinyEXR support.
  561. //
  562. // TinyEXR is a small, single header-only library to load and save OpenEXR(.exr) images.
  563. #ifdef cimg_use_tinyexr
  564. #ifndef TINYEXR_IMPLEMENTATION
  565. #define TINYEXR_IMPLEMENTATION
  566. #endif
  567. #include "tinyexr.h"
  568. #endif
  569. // Lapack configuration.
  570. // (http://www.netlib.org/lapack)
  571. //
  572. // Define 'cimg_use_lapack' to enable LAPACK support.
  573. //
  574. // Lapack library may be used in several CImg methods to speed up
  575. // matrix computations (eigenvalues, inverse, ...).
  576. #ifdef cimg_use_lapack
  577. extern "C" {
  578. extern void sgetrf_(int*, int*, float*, int*, int*, int*);
  579. extern void sgetri_(int*, float*, int*, int*, float*, int*, int*);
  580. extern void sgetrs_(char*, int*, int*, float*, int*, int*, float*, int*, int*);
  581. extern void sgesvd_(char*, char*, int*, int*, float*, int*, float*, float*, int*, float*, int*, float*, int*, int*);
  582. extern void ssyev_(char*, char*, int*, float*, int*, float*, float*, int*, int*);
  583. extern void dgetrf_(int*, int*, double*, int*, int*, int*);
  584. extern void dgetri_(int*, double*, int*, int*, double*, int*, int*);
  585. extern void dgetrs_(char*, int*, int*, double*, int*, int*, double*, int*, int*);
  586. extern void dgesvd_(char*, char*, int*, int*, double*, int*, double*, double*,
  587. int*, double*, int*, double*, int*, int*);
  588. extern void dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*);
  589. extern void dgels_(char*, int*,int*,int*,double*,int*,double*,int*,double*,int*,int*);
  590. extern void sgels_(char*, int*,int*,int*,float*,int*,float*,int*,float*,int*,int*);
  591. }
  592. #endif
  593. // Check if min/max/PI macros are defined.
  594. //
  595. // CImg does not compile if macros 'min', 'max' or 'PI' are defined,
  596. // because it redefines functions min(), max() and const variable PI in the cimg:: namespace.
  597. // so it '#undef' these macros if necessary, and restore them to reasonable
  598. // values at the end of this file.
  599. #ifdef min
  600. #undef min
  601. #define _cimg_redefine_min
  602. #endif
  603. #ifdef max
  604. #undef max
  605. #define _cimg_redefine_max
  606. #endif
  607. #ifdef PI
  608. #undef PI
  609. #define _cimg_redefine_PI
  610. #endif
  611. // Define 'cimg_library' namespace suffix.
  612. //
  613. // You may want to add a suffix to the 'cimg_library' namespace, for instance if you need to work
  614. // with several versions of the library at the same time.
  615. #ifdef cimg_namespace_suffix
  616. #define __cimg_library_suffixed(s) cimg_library_##s
  617. #define _cimg_library_suffixed(s) __cimg_library_suffixed(s)
  618. #define cimg_library_suffixed _cimg_library_suffixed(cimg_namespace_suffix)
  619. #else
  620. #define cimg_library_suffixed cimg_library
  621. #endif
  622. /*------------------------------------------------------------------------------
  623. #
  624. # Define user-friendly macros.
  625. #
  626. # These CImg macros are prefixed by 'cimg_' and can be used safely in your own
  627. # code. They are useful to parse command line options, or to write image loops.
  628. #
  629. ------------------------------------------------------------------------------*/
  630. // Macros to define program usage, and retrieve command line arguments.
  631. #define cimg_usage(usage) cimg_library_suffixed::cimg::option((char*)0,argc,argv,(char*)0,usage,false)
  632. #define cimg_help(str) cimg_library_suffixed::cimg::option((char*)0,argc,argv,str,(char*)0)
  633. #define cimg_option(name,_default,usage) cimg_library_suffixed::cimg::option(name,argc,argv,_default,usage)
  634. // Macros to define and manipulate local neighborhoods.
  635. #define CImg_2x2(I,T) T I[4]; \
  636. T& I##cc = I[0]; T& I##nc = I[1]; \
  637. T& I##cn = I[2]; T& I##nn = I[3]; \
  638. I##cc = I##nc = \
  639. I##cn = I##nn = 0
  640. #define CImg_3x3(I,T) T I[9]; \
  641. T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; \
  642. T& I##pc = I[3]; T& I##cc = I[4]; T& I##nc = I[5]; \
  643. T& I##pn = I[6]; T& I##cn = I[7]; T& I##nn = I[8]; \
  644. I##pp = I##cp = I##np = \
  645. I##pc = I##cc = I##nc = \
  646. I##pn = I##cn = I##nn = 0
  647. #define CImg_4x4(I,T) T I[16]; \
  648. T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; T& I##ap = I[3]; \
  649. T& I##pc = I[4]; T& I##cc = I[5]; T& I##nc = I[6]; T& I##ac = I[7]; \
  650. T& I##pn = I[8]; T& I##cn = I[9]; T& I##nn = I[10]; T& I##an = I[11]; \
  651. T& I##pa = I[12]; T& I##ca = I[13]; T& I##na = I[14]; T& I##aa = I[15]; \
  652. I##pp = I##cp = I##np = I##ap = \
  653. I##pc = I##cc = I##nc = I##ac = \
  654. I##pn = I##cn = I##nn = I##an = \
  655. I##pa = I##ca = I##na = I##aa = 0
  656. #define CImg_5x5(I,T) T I[25]; \
  657. T& I##bb = I[0]; T& I##pb = I[1]; T& I##cb = I[2]; T& I##nb = I[3]; T& I##ab = I[4]; \
  658. T& I##bp = I[5]; T& I##pp = I[6]; T& I##cp = I[7]; T& I##np = I[8]; T& I##ap = I[9]; \
  659. T& I##bc = I[10]; T& I##pc = I[11]; T& I##cc = I[12]; T& I##nc = I[13]; T& I##ac = I[14]; \
  660. T& I##bn = I[15]; T& I##pn = I[16]; T& I##cn = I[17]; T& I##nn = I[18]; T& I##an = I[19]; \
  661. T& I##ba = I[20]; T& I##pa = I[21]; T& I##ca = I[22]; T& I##na = I[23]; T& I##aa = I[24]; \
  662. I##bb = I##pb = I##cb = I##nb = I##ab = \
  663. I##bp = I##pp = I##cp = I##np = I##ap = \
  664. I##bc = I##pc = I##cc = I##nc = I##ac = \
  665. I##bn = I##pn = I##cn = I##nn = I##an = \
  666. I##ba = I##pa = I##ca = I##na = I##aa = 0
  667. #define CImg_2x2x2(I,T) T I[8]; \
  668. T& I##ccc = I[0]; T& I##ncc = I[1]; \
  669. T& I##cnc = I[2]; T& I##nnc = I[3]; \
  670. T& I##ccn = I[4]; T& I##ncn = I[5]; \
  671. T& I##cnn = I[6]; T& I##nnn = I[7]; \
  672. I##ccc = I##ncc = \
  673. I##cnc = I##nnc = \
  674. I##ccn = I##ncn = \
  675. I##cnn = I##nnn = 0
  676. #define CImg_3x3x3(I,T) T I[27]; \
  677. T& I##ppp = I[0]; T& I##cpp = I[1]; T& I##npp = I[2]; \
  678. T& I##pcp = I[3]; T& I##ccp = I[4]; T& I##ncp = I[5]; \
  679. T& I##pnp = I[6]; T& I##cnp = I[7]; T& I##nnp = I[8]; \
  680. T& I##ppc = I[9]; T& I##cpc = I[10]; T& I##npc = I[11]; \
  681. T& I##pcc = I[12]; T& I##ccc = I[13]; T& I##ncc = I[14]; \
  682. T& I##pnc = I[15]; T& I##cnc = I[16]; T& I##nnc = I[17]; \
  683. T& I##ppn = I[18]; T& I##cpn = I[19]; T& I##npn = I[20]; \
  684. T& I##pcn = I[21]; T& I##ccn = I[22]; T& I##ncn = I[23]; \
  685. T& I##pnn = I[24]; T& I##cnn = I[25]; T& I##nnn = I[26]; \
  686. I##ppp = I##cpp = I##npp = \
  687. I##pcp = I##ccp = I##ncp = \
  688. I##pnp = I##cnp = I##nnp = \
  689. I##ppc = I##cpc = I##npc = \
  690. I##pcc = I##ccc = I##ncc = \
  691. I##pnc = I##cnc = I##nnc = \
  692. I##ppn = I##cpn = I##npn = \
  693. I##pcn = I##ccn = I##ncn = \
  694. I##pnn = I##cnn = I##nnn = 0
  695. #define cimg_def2x2(img,x,y) \
  696. int _n1##x = x<(img).width() - 1?x + 1:(img).width() - 1, \
  697. _n1##y = y<(img).height() - 1?y + 1:(img).height() - 1
  698. #define cimg_def3x3(img,x,y) \
  699. cimg_def2x2(img,x,y); \
  700. int _p1##x = x>1?x - 1:0, \
  701. _p1##y = y>1?y - 1:0
  702. #define cimg_def4x4(img,x,y) \
  703. cimg_def3x3(img,x,y); \
  704. int _n2##x = x<(img).width() - 2?x + 2:(img).width() - 1, \
  705. _n2##y = y<(img).height() - 2?y + 2:(img).height() - 1
  706. #define cimg_def5x5(img,x,y) \
  707. cimg_def4x4(img,x,y); \
  708. int _p2##x = x>2?x - 2:0, \
  709. _p2##y = y>2?y - 2:0
  710. #define cimg_def6x6(img,x,y) \
  711. cimg_def5x5(img,x,y); \
  712. int _n3##x = x<(img).width() - 3?x + 3:(img).width() - 1, \
  713. _n3##y = y<(img).height() - 3?y + 3:(img).height() - 1
  714. #define cimg_def7x7(img,x,y) \
  715. cimg_def6x6(img,x,y); \
  716. int _p3##x = x>3?x - 3:0, \
  717. _p3##y = y>3?y - 3:0
  718. #define cimg_def8x8(img,x,y) \
  719. cimg_def7x7(img,x,y); \
  720. int _n4##x = x<(img).width() - 4?x + 4:(img).width() - 1, \
  721. _n4##y = y<(img).height() - 4?y + 4:(img).height() - 1
  722. #define cimg_def9x9(img,x,y) \
  723. cimg_def8x8(img,x,y); \
  724. int _p4##x = x>4?x - 4:0, \
  725. _p4##y = y>4?y - 4:0
  726. #define cimg_def2x2x2(img,x,y,z) \
  727. cimg_def2x2(img,x,y); \
  728. int _n1##z = z<(img).depth() - 1?z + 1:(img).depth() - 1
  729. #define cimg_def3x3x3(img,x,y,z) \
  730. cimg_def2x2x2(img,x,y,z); \
  731. int _p1##x = x>1?x - 1:0, \
  732. _p1##y = y>1?y - 1:0, \
  733. _p1##z = z>1?z - 1:0
  734. #define cimg_get2x2(img,x,y,z,c,I,T) \
  735. I[0] = (T)(img)(x,y,z,c), I[1] = (T)(img)(_n1##x,y,z,c), I[2] = (T)(img)(x,_n1##y,z,c), \
  736. I[3] = (T)(img)(_n1##x,_n1##y,z,c)
  737. #define cimg_get3x3(img,x,y,z,c,I,T) \
  738. I[0] = (T)(img)(_p1##x,_p1##y,z,c), I[1] = (T)(img)(x,_p1##y,z,c), I[2] = (T)(img)(_n1##x,_p1##y,z,c), \
  739. I[3] = (T)(img)(_p1##x,y,z,c), I[4] = (T)(img)(x,y,z,c), I[5] = (T)(img)(_n1##x,y,z,c), \
  740. I[6] = (T)(img)(_p1##x,_n1##y,z,c), I[7] = (T)(img)(x,_n1##y,z,c), I[8] = (T)(img)(_n1##x,_n1##y,z,c)
  741. #define cimg_get4x4(img,x,y,z,c,I,T) \
  742. I[0] = (T)(img)(_p1##x,_p1##y,z,c), I[1] = (T)(img)(x,_p1##y,z,c), I[2] = (T)(img)(_n1##x,_p1##y,z,c), \
  743. I[3] = (T)(img)(_n2##x,_p1##y,z,c), I[4] = (T)(img)(_p1##x,y,z,c), I[5] = (T)(img)(x,y,z,c), \
  744. I[6] = (T)(img)(_n1##x,y,z,c), I[7] = (T)(img)(_n2##x,y,z,c), I[8] = (T)(img)(_p1##x,_n1##y,z,c), \
  745. I[9] = (T)(img)(x,_n1##y,z,c), I[10] = (T)(img)(_n1##x,_n1##y,z,c), I[11] = (T)(img)(_n2##x,_n1##y,z,c), \
  746. I[12] = (T)(img)(_p1##x,_n2##y,z,c), I[13] = (T)(img)(x,_n2##y,z,c), I[14] = (T)(img)(_n1##x,_n2##y,z,c), \
  747. I[15] = (T)(img)(_n2##x,_n2##y,z,c)
  748. #define cimg_get5x5(img,x,y,z,c,I,T) \
  749. I[0] = (T)(img)(_p2##x,_p2##y,z,c), I[1] = (T)(img)(_p1##x,_p2##y,z,c), I[2] = (T)(img)(x,_p2##y,z,c), \
  750. I[3] = (T)(img)(_n1##x,_p2##y,z,c), I[4] = (T)(img)(_n2##x,_p2##y,z,c), I[5] = (T)(img)(_p2##x,_p1##y,z,c), \
  751. I[6] = (T)(img)(_p1##x,_p1##y,z,c), I[7] = (T)(img)(x,_p1##y,z,c), I[8] = (T)(img)(_n1##x,_p1##y,z,c), \
  752. I[9] = (T)(img)(_n2##x,_p1##y,z,c), I[10] = (T)(img)(_p2##x,y,z,c), I[11] = (T)(img)(_p1##x,y,z,c), \
  753. I[12] = (T)(img)(x,y,z,c), I[13] = (T)(img)(_n1##x,y,z,c), I[14] = (T)(img)(_n2##x,y,z,c), \
  754. I[15] = (T)(img)(_p2##x,_n1##y,z,c), I[16] = (T)(img)(_p1##x,_n1##y,z,c), I[17] = (T)(img)(x,_n1##y,z,c), \
  755. I[18] = (T)(img)(_n1##x,_n1##y,z,c), I[19] = (T)(img)(_n2##x,_n1##y,z,c), I[20] = (T)(img)(_p2##x,_n2##y,z,c), \
  756. I[21] = (T)(img)(_p1##x,_n2##y,z,c), I[22] = (T)(img)(x,_n2##y,z,c), I[23] = (T)(img)(_n1##x,_n2##y,z,c), \
  757. I[24] = (T)(img)(_n2##x,_n2##y,z,c)
  758. #define cimg_get6x6(img,x,y,z,c,I,T) \
  759. I[0] = (T)(img)(_p2##x,_p2##y,z,c), I[1] = (T)(img)(_p1##x,_p2##y,z,c), I[2] = (T)(img)(x,_p2##y,z,c), \
  760. I[3] = (T)(img)(_n1##x,_p2##y,z,c), I[4] = (T)(img)(_n2##x,_p2##y,z,c), I[5] = (T)(img)(_n3##x,_p2##y,z,c), \
  761. I[6] = (T)(img)(_p2##x,_p1##y,z,c), I[7] = (T)(img)(_p1##x,_p1##y,z,c), I[8] = (T)(img)(x,_p1##y,z,c), \
  762. I[9] = (T)(img)(_n1##x,_p1##y,z,c), I[10] = (T)(img)(_n2##x,_p1##y,z,c), I[11] = (T)(img)(_n3##x,_p1##y,z,c), \
  763. I[12] = (T)(img)(_p2##x,y,z,c), I[13] = (T)(img)(_p1##x,y,z,c), I[14] = (T)(img)(x,y,z,c), \
  764. I[15] = (T)(img)(_n1##x,y,z,c), I[16] = (T)(img)(_n2##x,y,z,c), I[17] = (T)(img)(_n3##x,y,z,c), \
  765. I[18] = (T)(img)(_p2##x,_n1##y,z,c), I[19] = (T)(img)(_p1##x,_n1##y,z,c), I[20] = (T)(img)(x,_n1##y,z,c), \
  766. I[21] = (T)(img)(_n1##x,_n1##y,z,c), I[22] = (T)(img)(_n2##x,_n1##y,z,c), I[23] = (T)(img)(_n3##x,_n1##y,z,c), \
  767. I[24] = (T)(img)(_p2##x,_n2##y,z,c), I[25] = (T)(img)(_p1##x,_n2##y,z,c), I[26] = (T)(img)(x,_n2##y,z,c), \
  768. I[27] = (T)(img)(_n1##x,_n2##y,z,c), I[28] = (T)(img)(_n2##x,_n2##y,z,c), I[29] = (T)(img)(_n3##x,_n2##y,z,c), \
  769. I[30] = (T)(img)(_p2##x,_n3##y,z,c), I[31] = (T)(img)(_p1##x,_n3##y,z,c), I[32] = (T)(img)(x,_n3##y,z,c), \
  770. I[33] = (T)(img)(_n1##x,_n3##y,z,c), I[34] = (T)(img)(_n2##x,_n3##y,z,c), I[35] = (T)(img)(_n3##x,_n3##y,z,c)
  771. #define cimg_get7x7(img,x,y,z,c,I,T) \
  772. I[0] = (T)(img)(_p3##x,_p3##y,z,c), I[1] = (T)(img)(_p2##x,_p3##y,z,c), I[2] = (T)(img)(_p1##x,_p3##y,z,c), \
  773. I[3] = (T)(img)(x,_p3##y,z,c), I[4] = (T)(img)(_n1##x,_p3##y,z,c), I[5] = (T)(img)(_n2##x,_p3##y,z,c), \
  774. I[6] = (T)(img)(_n3##x,_p3##y,z,c), I[7] = (T)(img)(_p3##x,_p2##y,z,c), I[8] = (T)(img)(_p2##x,_p2##y,z,c), \
  775. I[9] = (T)(img)(_p1##x,_p2##y,z,c), I[10] = (T)(img)(x,_p2##y,z,c), I[11] = (T)(img)(_n1##x,_p2##y,z,c), \
  776. I[12] = (T)(img)(_n2##x,_p2##y,z,c), I[13] = (T)(img)(_n3##x,_p2##y,z,c), I[14] = (T)(img)(_p3##x,_p1##y,z,c), \
  777. I[15] = (T)(img)(_p2##x,_p1##y,z,c), I[16] = (T)(img)(_p1##x,_p1##y,z,c), I[17] = (T)(img)(x,_p1##y,z,c), \
  778. I[18] = (T)(img)(_n1##x,_p1##y,z,c), I[19] = (T)(img)(_n2##x,_p1##y,z,c), I[20] = (T)(img)(_n3##x,_p1##y,z,c), \
  779. I[21] = (T)(img)(_p3##x,y,z,c), I[22] = (T)(img)(_p2##x,y,z,c), I[23] = (T)(img)(_p1##x,y,z,c), \
  780. I[24] = (T)(img)(x,y,z,c), I[25] = (T)(img)(_n1##x,y,z,c), I[26] = (T)(img)(_n2##x,y,z,c), \
  781. I[27] = (T)(img)(_n3##x,y,z,c), I[28] = (T)(img)(_p3##x,_n1##y,z,c), I[29] = (T)(img)(_p2##x,_n1##y,z,c), \
  782. I[30] = (T)(img)(_p1##x,_n1##y,z,c), I[31] = (T)(img)(x,_n1##y,z,c), I[32] = (T)(img)(_n1##x,_n1##y,z,c), \
  783. I[33] = (T)(img)(_n2##x,_n1##y,z,c), I[34] = (T)(img)(_n3##x,_n1##y,z,c), I[35] = (T)(img)(_p3##x,_n2##y,z,c), \
  784. I[36] = (T)(img)(_p2##x,_n2##y,z,c), I[37] = (T)(img)(_p1##x,_n2##y,z,c), I[38] = (T)(img)(x,_n2##y,z,c), \
  785. I[39] = (T)(img)(_n1##x,_n2##y,z,c), I[40] = (T)(img)(_n2##x,_n2##y,z,c), I[41] = (T)(img)(_n3##x,_n2##y,z,c), \
  786. I[42] = (T)(img)(_p3##x,_n3##y,z,c), I[43] = (T)(img)(_p2##x,_n3##y,z,c), I[44] = (T)(img)(_p1##x,_n3##y,z,c), \
  787. I[45] = (T)(img)(x,_n3##y,z,c), I[46] = (T)(img)(_n1##x,_n3##y,z,c), I[47] = (T)(img)(_n2##x,_n3##y,z,c), \
  788. I[48] = (T)(img)(_n3##x,_n3##y,z,c)
  789. #define cimg_get8x8(img,x,y,z,c,I,T) \
  790. I[0] = (T)(img)(_p3##x,_p3##y,z,c), I[1] = (T)(img)(_p2##x,_p3##y,z,c), I[2] = (T)(img)(_p1##x,_p3##y,z,c), \
  791. I[3] = (T)(img)(x,_p3##y,z,c), I[4] = (T)(img)(_n1##x,_p3##y,z,c), I[5] = (T)(img)(_n2##x,_p3##y,z,c), \
  792. I[6] = (T)(img)(_n3##x,_p3##y,z,c), I[7] = (T)(img)(_n4##x,_p3##y,z,c), I[8] = (T)(img)(_p3##x,_p2##y,z,c), \
  793. I[9] = (T)(img)(_p2##x,_p2##y,z,c), I[10] = (T)(img)(_p1##x,_p2##y,z,c), I[11] = (T)(img)(x,_p2##y,z,c), \
  794. I[12] = (T)(img)(_n1##x,_p2##y,z,c), I[13] = (T)(img)(_n2##x,_p2##y,z,c), I[14] = (T)(img)(_n3##x,_p2##y,z,c), \
  795. I[15] = (T)(img)(_n4##x,_p2##y,z,c), I[16] = (T)(img)(_p3##x,_p1##y,z,c), I[17] = (T)(img)(_p2##x,_p1##y,z,c), \
  796. I[18] = (T)(img)(_p1##x,_p1##y,z,c), I[19] = (T)(img)(x,_p1##y,z,c), I[20] = (T)(img)(_n1##x,_p1##y,z,c), \
  797. I[21] = (T)(img)(_n2##x,_p1##y,z,c), I[22] = (T)(img)(_n3##x,_p1##y,z,c), I[23] = (T)(img)(_n4##x,_p1##y,z,c), \
  798. I[24] = (T)(img)(_p3##x,y,z,c), I[25] = (T)(img)(_p2##x,y,z,c), I[26] = (T)(img)(_p1##x,y,z,c), \
  799. I[27] = (T)(img)(x,y,z,c), I[28] = (T)(img)(_n1##x,y,z,c), I[29] = (T)(img)(_n2##x,y,z,c), \
  800. I[30] = (T)(img)(_n3##x,y,z,c), I[31] = (T)(img)(_n4##x,y,z,c), I[32] = (T)(img)(_p3##x,_n1##y,z,c), \
  801. I[33] = (T)(img)(_p2##x,_n1##y,z,c), I[34] = (T)(img)(_p1##x,_n1##y,z,c), I[35] = (T)(img)(x,_n1##y,z,c), \
  802. I[36] = (T)(img)(_n1##x,_n1##y,z,c), I[37] = (T)(img)(_n2##x,_n1##y,z,c), I[38] = (T)(img)(_n3##x,_n1##y,z,c), \
  803. I[39] = (T)(img)(_n4##x,_n1##y,z,c), I[40] = (T)(img)(_p3##x,_n2##y,z,c), I[41] = (T)(img)(_p2##x,_n2##y,z,c), \
  804. I[42] = (T)(img)(_p1##x,_n2##y,z,c), I[43] = (T)(img)(x,_n2##y,z,c), I[44] = (T)(img)(_n1##x,_n2##y,z,c), \
  805. I[45] = (T)(img)(_n2##x,_n2##y,z,c), I[46] = (T)(img)(_n3##x,_n2##y,z,c), I[47] = (T)(img)(_n4##x,_n2##y,z,c), \
  806. I[48] = (T)(img)(_p3##x,_n3##y,z,c), I[49] = (T)(img)(_p2##x,_n3##y,z,c), I[50] = (T)(img)(_p1##x,_n3##y,z,c), \
  807. I[51] = (T)(img)(x,_n3##y,z,c), I[52] = (T)(img)(_n1##x,_n3##y,z,c), I[53] = (T)(img)(_n2##x,_n3##y,z,c), \
  808. I[54] = (T)(img)(_n3##x,_n3##y,z,c), I[55] = (T)(img)(_n4##x,_n3##y,z,c), I[56] = (T)(img)(_p3##x,_n4##y,z,c), \
  809. I[57] = (T)(img)(_p2##x,_n4##y,z,c), I[58] = (T)(img)(_p1##x,_n4##y,z,c), I[59] = (T)(img)(x,_n4##y,z,c), \
  810. I[60] = (T)(img)(_n1##x,_n4##y,z,c), I[61] = (T)(img)(_n2##x,_n4##y,z,c), I[62] = (T)(img)(_n3##x,_n4##y,z,c), \
  811. I[63] = (T)(img)(_n4##x,_n4##y,z,c);
  812. #define cimg_get9x9(img,x,y,z,c,I,T) \
  813. I[0] = (T)(img)(_p4##x,_p4##y,z,c), I[1] = (T)(img)(_p3##x,_p4##y,z,c), I[2] = (T)(img)(_p2##x,_p4##y,z,c), \
  814. I[3] = (T)(img)(_p1##x,_p4##y,z,c), I[4] = (T)(img)(x,_p4##y,z,c), I[5] = (T)(img)(_n1##x,_p4##y,z,c), \
  815. I[6] = (T)(img)(_n2##x,_p4##y,z,c), I[7] = (T)(img)(_n3##x,_p4##y,z,c), I[8] = (T)(img)(_n4##x,_p4##y,z,c), \
  816. I[9] = (T)(img)(_p4##x,_p3##y,z,c), I[10] = (T)(img)(_p3##x,_p3##y,z,c), I[11] = (T)(img)(_p2##x,_p3##y,z,c), \
  817. I[12] = (T)(img)(_p1##x,_p3##y,z,c), I[13] = (T)(img)(x,_p3##y,z,c), I[14] = (T)(img)(_n1##x,_p3##y,z,c), \
  818. I[15] = (T)(img)(_n2##x,_p3##y,z,c), I[16] = (T)(img)(_n3##x,_p3##y,z,c), I[17] = (T)(img)(_n4##x,_p3##y,z,c), \
  819. I[18] = (T)(img)(_p4##x,_p2##y,z,c), I[19] = (T)(img)(_p3##x,_p2##y,z,c), I[20] = (T)(img)(_p2##x,_p2##y,z,c), \
  820. I[21] = (T)(img)(_p1##x,_p2##y,z,c), I[22] = (T)(img)(x,_p2##y,z,c), I[23] = (T)(img)(_n1##x,_p2##y,z,c), \
  821. I[24] = (T)(img)(_n2##x,_p2##y,z,c), I[25] = (T)(img)(_n3##x,_p2##y,z,c), I[26] = (T)(img)(_n4##x,_p2##y,z,c), \
  822. I[27] = (T)(img)(_p4##x,_p1##y,z,c), I[28] = (T)(img)(_p3##x,_p1##y,z,c), I[29] = (T)(img)(_p2##x,_p1##y,z,c), \
  823. I[30] = (T)(img)(_p1##x,_p1##y,z,c), I[31] = (T)(img)(x,_p1##y,z,c), I[32] = (T)(img)(_n1##x,_p1##y,z,c), \
  824. I[33] = (T)(img)(_n2##x,_p1##y,z,c), I[34] = (T)(img)(_n3##x,_p1##y,z,c), I[35] = (T)(img)(_n4##x,_p1##y,z,c), \
  825. I[36] = (T)(img)(_p4##x,y,z,c), I[37] = (T)(img)(_p3##x,y,z,c), I[38] = (T)(img)(_p2##x,y,z,c), \
  826. I[39] = (T)(img)(_p1##x,y,z,c), I[40] = (T)(img)(x,y,z,c), I[41] = (T)(img)(_n1##x,y,z,c), \
  827. I[42] = (T)(img)(_n2##x,y,z,c), I[43] = (T)(img)(_n3##x,y,z,c), I[44] = (T)(img)(_n4##x,y,z,c), \
  828. I[45] = (T)(img)(_p4##x,_n1##y,z,c), I[46] = (T)(img)(_p3##x,_n1##y,z,c), I[47] = (T)(img)(_p2##x,_n1##y,z,c), \
  829. I[48] = (T)(img)(_p1##x,_n1##y,z,c), I[49] = (T)(img)(x,_n1##y,z,c), I[50] = (T)(img)(_n1##x,_n1##y,z,c), \
  830. I[51] = (T)(img)(_n2##x,_n1##y,z,c), I[52] = (T)(img)(_n3##x,_n1##y,z,c), I[53] = (T)(img)(_n4##x,_n1##y,z,c), \
  831. I[54] = (T)(img)(_p4##x,_n2##y,z,c), I[55] = (T)(img)(_p3##x,_n2##y,z,c), I[56] = (T)(img)(_p2##x,_n2##y,z,c), \
  832. I[57] = (T)(img)(_p1##x,_n2##y,z,c), I[58] = (T)(img)(x,_n2##y,z,c), I[59] = (T)(img)(_n1##x,_n2##y,z,c), \
  833. I[60] = (T)(img)(_n2##x,_n2##y,z,c), I[61] = (T)(img)(_n3##x,_n2##y,z,c), I[62] = (T)(img)(_n4##x,_n2##y,z,c), \
  834. I[63] = (T)(img)(_p4##x,_n3##y,z,c), I[64] = (T)(img)(_p3##x,_n3##y,z,c), I[65] = (T)(img)(_p2##x,_n3##y,z,c), \
  835. I[66] = (T)(img)(_p1##x,_n3##y,z,c), I[67] = (T)(img)(x,_n3##y,z,c), I[68] = (T)(img)(_n1##x,_n3##y,z,c), \
  836. I[69] = (T)(img)(_n2##x,_n3##y,z,c), I[70] = (T)(img)(_n3##x,_n3##y,z,c), I[71] = (T)(img)(_n4##x,_n3##y,z,c), \
  837. I[72] = (T)(img)(_p4##x,_n4##y,z,c), I[73] = (T)(img)(_p3##x,_n4##y,z,c), I[74] = (T)(img)(_p2##x,_n4##y,z,c), \
  838. I[75] = (T)(img)(_p1##x,_n4##y,z,c), I[76] = (T)(img)(x,_n4##y,z,c), I[77] = (T)(img)(_n1##x,_n4##y,z,c), \
  839. I[78] = (T)(img)(_n2##x,_n4##y,z,c), I[79] = (T)(img)(_n3##x,_n4##y,z,c), I[80] = (T)(img)(_n4##x,_n4##y,z,c)
  840. #define cimg_get2x2x2(img,x,y,z,c,I,T) \
  841. I[0] = (T)(img)(x,y,z,c), I[1] = (T)(img)(_n1##x,y,z,c), I[2] = (T)(img)(x,_n1##y,z,c), \
  842. I[3] = (T)(img)(_n1##x,_n1##y,z,c), I[4] = (T)(img)(x,y,_n1##z,c), I[5] = (T)(img)(_n1##x,y,_n1##z,c), \
  843. I[6] = (T)(img)(x,_n1##y,_n1##z,c), I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)
  844. #define cimg_get3x3x3(img,x,y,z,c,I,T) \
  845. I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c), I[1] = (T)(img)(x,_p1##y,_p1##z,c), \
  846. I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c), I[3] = (T)(img)(_p1##x,y,_p1##z,c), I[4] = (T)(img)(x,y,_p1##z,c), \
  847. I[5] = (T)(img)(_n1##x,y,_p1##z,c), I[6] = (T)(img)(_p1##x,_n1##y,_p1##z,c), I[7] = (T)(img)(x,_n1##y,_p1##z,c), \
  848. I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c), I[9] = (T)(img)(_p1##x,_p1##y,z,c), I[10] = (T)(img)(x,_p1##y,z,c), \
  849. I[11] = (T)(img)(_n1##x,_p1##y,z,c), I[12] = (T)(img)(_p1##x,y,z,c), I[13] = (T)(img)(x,y,z,c), \
  850. I[14] = (T)(img)(_n1##x,y,z,c), I[15] = (T)(img)(_p1##x,_n1##y,z,c), I[16] = (T)(img)(x,_n1##y,z,c), \
  851. I[17] = (T)(img)(_n1##x,_n1##y,z,c), I[18] = (T)(img)(_p1##x,_p1##y,_n1##z,c), I[19] = (T)(img)(x,_p1##y,_n1##z,c), \
  852. I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c), I[21] = (T)(img)(_p1##x,y,_n1##z,c), I[22] = (T)(img)(x,y,_n1##z,c), \
  853. I[23] = (T)(img)(_n1##x,y,_n1##z,c), I[24] = (T)(img)(_p1##x,_n1##y,_n1##z,c), I[25] = (T)(img)(x,_n1##y,_n1##z,c), \
  854. I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)
  855. // Macros to perform various image loops.
  856. //
  857. // These macros are simpler to use than loops with C++ iterators.
  858. #define cimg_for(img,ptrs,T_ptrs) \
  859. for (T_ptrs *ptrs = (img)._data, *_max##ptrs = (img)._data + (img).size(); ptrs<_max##ptrs; ++ptrs)
  860. #define cimg_rof(img,ptrs,T_ptrs) for (T_ptrs *ptrs = (img)._data + (img).size() - 1; ptrs>=(img)._data; --ptrs)
  861. #define cimg_foroff(img,off) for (cimg_ulong off = 0, _max##off = (img).size(); off<_max##off; ++off)
  862. #define cimg_rofoff(img,off) for (cimg_long off = (cimg_long)((img).size() - 1); off>=0; --off)
  863. #define cimg_for1(bound,i) for (int i = 0; i<(int)(bound); ++i)
  864. #define cimg_forX(img,x) cimg_for1((img)._width,x)
  865. #define cimg_forY(img,y) cimg_for1((img)._height,y)
  866. #define cimg_forZ(img,z) cimg_for1((img)._depth,z)
  867. #define cimg_forC(img,c) cimg_for1((img)._spectrum,c)
  868. #define cimg_forXY(img,x,y) cimg_forY(img,y) cimg_forX(img,x)
  869. #define cimg_forXZ(img,x,z) cimg_forZ(img,z) cimg_forX(img,x)
  870. #define cimg_forYZ(img,y,z) cimg_forZ(img,z) cimg_forY(img,y)
  871. #define cimg_forXC(img,x,c) cimg_forC(img,c) cimg_forX(img,x)
  872. #define cimg_forYC(img,y,c) cimg_forC(img,c) cimg_forY(img,y)
  873. #define cimg_forZC(img,z,c) cimg_forC(img,c) cimg_forZ(img,z)
  874. #define cimg_forXYZ(img,x,y,z) cimg_forZ(img,z) cimg_forXY(img,x,y)
  875. #define cimg_forXYC(img,x,y,c) cimg_forC(img,c) cimg_forXY(img,x,y)
  876. #define cimg_forXZC(img,x,z,c) cimg_forC(img,c) cimg_forXZ(img,x,z)
  877. #define cimg_forYZC(img,y,z,c) cimg_forC(img,c) cimg_forYZ(img,y,z)
  878. #define cimg_forXYZC(img,x,y,z,c) cimg_forC(img,c) cimg_forXYZ(img,x,y,z)
  879. #define cimg_rof1(bound,i) for (int i = (int)(bound) - 1; i>=0; --i)
  880. #define cimg_rofX(img,x) cimg_rof1((img)._width,x)
  881. #define cimg_rofY(img,y) cimg_rof1((img)._height,y)
  882. #define cimg_rofZ(img,z) cimg_rof1((img)._depth,z)
  883. #define cimg_rofC(img,c) cimg_rof1((img)._spectrum,c)
  884. #define cimg_rofXY(img,x,y) cimg_rofY(img,y) cimg_rofX(img,x)
  885. #define cimg_rofXZ(img,x,z) cimg_rofZ(img,z) cimg_rofX(img,x)
  886. #define cimg_rofYZ(img,y,z) cimg_rofZ(img,z) cimg_rofY(img,y)
  887. #define cimg_rofXC(img,x,c) cimg_rofC(img,c) cimg_rofX(img,x)
  888. #define cimg_rofYC(img,y,c) cimg_rofC(img,c) cimg_rofY(img,y)
  889. #define cimg_rofZC(img,z,c) cimg_rofC(img,c) cimg_rofZ(img,z)
  890. #define cimg_rofXYZ(img,x,y,z) cimg_rofZ(img,z) cimg_rofXY(img,x,y)
  891. #define cimg_rofXYC(img,x,y,c) cimg_rofC(img,c) cimg_rofXY(img,x,y)
  892. #define cimg_rofXZC(img,x,z,c) cimg_rofC(img,c) cimg_rofXZ(img,x,z)
  893. #define cimg_rofYZC(img,y,z,c) cimg_rofC(img,c) cimg_rofYZ(img,y,z)
  894. #define cimg_rofXYZC(img,x,y,z,c) cimg_rofC(img,c) cimg_rofXYZ(img,x,y,z)
  895. #define cimg_for_in1(bound,i0,i1,i) \
  896. for (int i = (int)(i0)<0?0:(int)(i0), _max##i = (int)(i1)<(int)(bound)?(int)(i1):(int)(bound) - 1; i<=_max##i; ++i)
  897. #define cimg_for_inX(img,x0,x1,x) cimg_for_in1((img)._width,x0,x1,x)
  898. #define cimg_for_inY(img,y0,y1,y) cimg_for_in1((img)._height,y0,y1,y)
  899. #define cimg_for_inZ(img,z0,z1,z) cimg_for_in1((img)._depth,z0,z1,z)
  900. #define cimg_for_inC(img,c0,c1,c) cimg_for_in1((img)._spectrum,c0,c1,c)
  901. #define cimg_for_inXY(img,x0,y0,x1,y1,x,y) cimg_for_inY(img,y0,y1,y) cimg_for_inX(img,x0,x1,x)
  902. #define cimg_for_inXZ(img,x0,z0,x1,z1,x,z) cimg_for_inZ(img,z0,z1,z) cimg_for_inX(img,x0,x1,x)
  903. #define cimg_for_inXC(img,x0,c0,x1,c1,x,c) cimg_for_inC(img,c0,c1,c) cimg_for_inX(img,x0,x1,x)
  904. #define cimg_for_inYZ(img,y0,z0,y1,z1,y,z) cimg_for_inZ(img,x0,z1,z) cimg_for_inY(img,y0,y1,y)
  905. #define cimg_for_inYC(img,y0,c0,y1,c1,y,c) cimg_for_inC(img,c0,c1,c) cimg_for_inY(img,y0,y1,y)
  906. #define cimg_for_inZC(img,z0,c0,z1,c1,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inZ(img,z0,z1,z)
  907. #define cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_inZ(img,z0,z1,z) cimg_for_inXY(img,x0,y0,x1,y1,x,y)
  908. #define cimg_for_inXYC(img,x0,y0,c0,x1,y1,c1,x,y,c) cimg_for_inC(img,c0,c1,c) cimg_for_inXY(img,x0,y0,x1,y1,x,y)
  909. #define cimg_for_inXZC(img,x0,z0,c0,x1,z1,c1,x,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inXZ(img,x0,z0,x1,z1,x,z)
  910. #define cimg_for_inYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_inC(img,c0,c1,c) cimg_for_inYZ(img,y0,z0,y1,z1,y,z)
  911. #define cimg_for_inXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  912. cimg_for_inC(img,c0,c1,c) cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  913. #define cimg_for_insideX(img,x,n) cimg_for_inX(img,n,(img)._width - 1 - (n),x)
  914. #define cimg_for_insideY(img,y,n) cimg_for_inY(img,n,(img)._height - 1 - (n),y)
  915. #define cimg_for_insideZ(img,z,n) cimg_for_inZ(img,n,(img)._depth - 1 - (n),z)
  916. #define cimg_for_insideC(img,c,n) cimg_for_inC(img,n,(img)._spectrum - 1 - (n),c)
  917. #define cimg_for_insideXY(img,x,y,n) cimg_for_inXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
  918. #define cimg_for_insideXYZ(img,x,y,z,n) \
  919. cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
  920. #define cimg_for_insideXYZC(img,x,y,z,c,n) \
  921. cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
  922. #define cimg_for_out1(boundi,i0,i1,i) \
  923. for (int i = (int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); ++i, i = i==(int)(i0)?(int)(i1) + 1:i)
  924. #define cimg_for_out2(boundi,boundj,i0,j0,i1,j1,i,j) \
  925. for (int j = 0; j<(int)(boundj); ++j) \
  926. for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
  927. ++i, i = _n1j?i:(i==(int)(i0)?(int)(i1) + 1:i))
  928. #define cimg_for_out3(boundi,boundj,boundk,i0,j0,k0,i1,j1,k1,i,j,k) \
  929. for (int k = 0; k<(int)(boundk); ++k) \
  930. for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
  931. for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
  932. ++i, i = _n1j || _n1k?i:(i==(int)(i0)?(int)(i1) + 1:i))
  933. #define cimg_for_out4(boundi,boundj,boundk,boundl,i0,j0,k0,l0,i1,j1,k1,l1,i,j,k,l) \
  934. for (int l = 0; l<(int)(boundl); ++l) \
  935. for (int _n1l = (int)(l<(int)(l0) || l>(int)(l1)), k = 0; k<(int)(boundk); ++k) \
  936. for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
  937. for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k || _n1l?0:(int)(i0)>0?0:(int)(i1) + 1; \
  938. i<(int)(boundi); ++i, i = _n1j || _n1k || _n1l?i:(i==(int)(i0)?(int)(i1) + 1:i))
  939. #define cimg_for_outX(img,x0,x1,x) cimg_for_out1((img)._width,x0,x1,x)
  940. #define cimg_for_outY(img,y0,y1,y) cimg_for_out1((img)._height,y0,y1,y)
  941. #define cimg_for_outZ(img,z0,z1,z) cimg_for_out1((img)._depth,z0,z1,z)
  942. #define cimg_for_outC(img,c0,c1,c) cimg_for_out1((img)._spectrum,c0,c1,c)
  943. #define cimg_for_outXY(img,x0,y0,x1,y1,x,y) cimg_for_out2((img)._width,(img)._height,x0,y0,x1,y1,x,y)
  944. #define cimg_for_outXZ(img,x0,z0,x1,z1,x,z) cimg_for_out2((img)._width,(img)._depth,x0,z0,x1,z1,x,z)
  945. #define cimg_for_outXC(img,x0,c0,x1,c1,x,c) cimg_for_out2((img)._width,(img)._spectrum,x0,c0,x1,c1,x,c)
  946. #define cimg_for_outYZ(img,y0,z0,y1,z1,y,z) cimg_for_out2((img)._height,(img)._depth,y0,z0,y1,z1,y,z)
  947. #define cimg_for_outYC(img,y0,c0,y1,c1,y,c) cimg_for_out2((img)._height,(img)._spectrum,y0,c0,y1,c1,y,c)
  948. #define cimg_for_outZC(img,z0,c0,z1,c1,z,c) cimg_for_out2((img)._depth,(img)._spectrum,z0,c0,z1,c1,z,c)
  949. #define cimg_for_outXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) \
  950. cimg_for_out3((img)._width,(img)._height,(img)._depth,x0,y0,z0,x1,y1,z1,x,y,z)
  951. #define cimg_for_outXYC(img,x0,y0,c0,x1,y1,c1,x,y,c) \
  952. cimg_for_out3((img)._width,(img)._height,(img)._spectrum,x0,y0,c0,x1,y1,c1,x,y,c)
  953. #define cimg_for_outXZC(img,x0,z0,c0,x1,z1,c1,x,z,c) \
  954. cimg_for_out3((img)._width,(img)._depth,(img)._spectrum,x0,z0,c0,x1,z1,c1,x,z,c)
  955. #define cimg_for_outYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) \
  956. cimg_for_out3((img)._height,(img)._depth,(img)._spectrum,y0,z0,c0,y1,z1,c1,y,z,c)
  957. #define cimg_for_outXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  958. cimg_for_out4((img)._width,(img)._height,(img)._depth,(img)._spectrum,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c)
  959. #define cimg_for_borderX(img,x,n) cimg_for_outX(img,n,(img)._width - 1 - (n),x)
  960. #define cimg_for_borderY(img,y,n) cimg_for_outY(img,n,(img)._height - 1 - (n),y)
  961. #define cimg_for_borderZ(img,z,n) cimg_for_outZ(img,n,(img)._depth - 1 - (n),z)
  962. #define cimg_for_borderC(img,c,n) cimg_for_outC(img,n,(img)._spectrum - 1 - (n),c)
  963. #define cimg_for_borderXY(img,x,y,n) cimg_for_outXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
  964. #define cimg_for_borderXYZ(img,x,y,z,n) \
  965. cimg_for_outXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
  966. #define cimg_for_borderXYZC(img,x,y,z,c,n) \
  967. cimg_for_outXYZC(img,n,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n), \
  968. (img)._depth - 1 - (n),(img)._spectrum - 1 - (n),x,y,z,c)
  969. #define cimg_for_spiralXY(img,x,y) \
  970. for (int x = 0, y = 0, _n1##x = 1, _n1##y = (img).width()*(img).height(); _n1##y; \
  971. --_n1##y, _n1##x+=(_n1##x>>2) - ((!(_n1##x&3)?--y:((_n1##x&3)==1?(img)._width - 1 - ++x:\
  972. ((_n1##x&3)==2?(img)._height - 1 - ++y:--x))))?0:1)
  973. #define cimg_for_lineXY(x,y,x0,y0,x1,y1) \
  974. for (int x = (int)(x0), y = (int)(y0), _sx = 1, _sy = 1, _steep = 0, \
  975. _dx=(x1)>(x0)?(int)(x1) - (int)(x0):(_sx=-1,(int)(x0) - (int)(x1)), \
  976. _dy=(y1)>(y0)?(int)(y1) - (int)(y0):(_sy=-1,(int)(y0) - (int)(y1)), \
  977. _counter = _dx, \
  978. _err = _dx>_dy?(_dy>>1):((_steep=1),(_counter=_dy),(_dx>>1)); \
  979. _counter>=0; \
  980. --_counter, x+=_steep? \
  981. (y+=_sy,(_err-=_dx)<0?_err+=_dy,_sx:0): \
  982. (y+=(_err-=_dy)<0?_err+=_dx,_sy:0,_sx))
  983. #define cimg_for2(bound,i) \
  984. for (int i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
  985. _n1##i<(int)(bound) || i==--_n1##i; \
  986. ++i, ++_n1##i)
  987. #define cimg_for2X(img,x) cimg_for2((img)._width,x)
  988. #define cimg_for2Y(img,y) cimg_for2((img)._height,y)
  989. #define cimg_for2Z(img,z) cimg_for2((img)._depth,z)
  990. #define cimg_for2C(img,c) cimg_for2((img)._spectrum,c)
  991. #define cimg_for2XY(img,x,y) cimg_for2Y(img,y) cimg_for2X(img,x)
  992. #define cimg_for2XZ(img,x,z) cimg_for2Z(img,z) cimg_for2X(img,x)
  993. #define cimg_for2XC(img,x,c) cimg_for2C(img,c) cimg_for2X(img,x)
  994. #define cimg_for2YZ(img,y,z) cimg_for2Z(img,z) cimg_for2Y(img,y)
  995. #define cimg_for2YC(img,y,c) cimg_for2C(img,c) cimg_for2Y(img,y)
  996. #define cimg_for2ZC(img,z,c) cimg_for2C(img,c) cimg_for2Z(img,z)
  997. #define cimg_for2XYZ(img,x,y,z) cimg_for2Z(img,z) cimg_for2XY(img,x,y)
  998. #define cimg_for2XZC(img,x,z,c) cimg_for2C(img,c) cimg_for2XZ(img,x,z)
  999. #define cimg_for2YZC(img,y,z,c) cimg_for2C(img,c) cimg_for2YZ(img,y,z)
  1000. #define cimg_for2XYZC(img,x,y,z,c) cimg_for2C(img,c) cimg_for2XYZ(img,x,y,z)
  1001. #define cimg_for_in2(bound,i0,i1,i) \
  1002. for (int i = (int)(i0)<0?0:(int)(i0), \
  1003. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
  1004. i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
  1005. ++i, ++_n1##i)
  1006. #define cimg_for_in2X(img,x0,x1,x) cimg_for_in2((img)._width,x0,x1,x)
  1007. #define cimg_for_in2Y(img,y0,y1,y) cimg_for_in2((img)._height,y0,y1,y)
  1008. #define cimg_for_in2Z(img,z0,z1,z) cimg_for_in2((img)._depth,z0,z1,z)
  1009. #define cimg_for_in2C(img,c0,c1,c) cimg_for_in2((img)._spectrum,c0,c1,c)
  1010. #define cimg_for_in2XY(img,x0,y0,x1,y1,x,y) cimg_for_in2Y(img,y0,y1,y) cimg_for_in2X(img,x0,x1,x)
  1011. #define cimg_for_in2XZ(img,x0,z0,x1,z1,x,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2X(img,x0,x1,x)
  1012. #define cimg_for_in2XC(img,x0,c0,x1,c1,x,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2X(img,x0,x1,x)
  1013. #define cimg_for_in2YZ(img,y0,z0,y1,z1,y,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2Y(img,y0,y1,y)
  1014. #define cimg_for_in2YC(img,y0,c0,y1,c1,y,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2Y(img,y0,y1,y)
  1015. #define cimg_for_in2ZC(img,z0,c0,z1,c1,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2Z(img,z0,z1,z)
  1016. #define cimg_for_in2XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in2Z(img,z0,z1,z) cimg_for_in2XY(img,x0,y0,x1,y1,x,y)
  1017. #define cimg_for_in2XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2XZ(img,x0,y0,x1,y1,x,z)
  1018. #define cimg_for_in2YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in2C(img,c0,c1,c) cimg_for_in2YZ(img,y0,z0,y1,z1,y,z)
  1019. #define cimg_for_in2XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1020. cimg_for_in2C(img,c0,c1,c) cimg_for_in2XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1021. #define cimg_for3(bound,i) \
  1022. for (int i = 0, _p1##i = 0, \
  1023. _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
  1024. _n1##i<(int)(bound) || i==--_n1##i; \
  1025. _p1##i = i++, ++_n1##i)
  1026. #define cimg_for3X(img,x) cimg_for3((img)._width,x)
  1027. #define cimg_for3Y(img,y) cimg_for3((img)._height,y)
  1028. #define cimg_for3Z(img,z) cimg_for3((img)._depth,z)
  1029. #define cimg_for3C(img,c) cimg_for3((img)._spectrum,c)
  1030. #define cimg_for3XY(img,x,y) cimg_for3Y(img,y) cimg_for3X(img,x)
  1031. #define cimg_for3XZ(img,x,z) cimg_for3Z(img,z) cimg_for3X(img,x)
  1032. #define cimg_for3XC(img,x,c) cimg_for3C(img,c) cimg_for3X(img,x)
  1033. #define cimg_for3YZ(img,y,z) cimg_for3Z(img,z) cimg_for3Y(img,y)
  1034. #define cimg_for3YC(img,y,c) cimg_for3C(img,c) cimg_for3Y(img,y)
  1035. #define cimg_for3ZC(img,z,c) cimg_for3C(img,c) cimg_for3Z(img,z)
  1036. #define cimg_for3XYZ(img,x,y,z) cimg_for3Z(img,z) cimg_for3XY(img,x,y)
  1037. #define cimg_for3XZC(img,x,z,c) cimg_for3C(img,c) cimg_for3XZ(img,x,z)
  1038. #define cimg_for3YZC(img,y,z,c) cimg_for3C(img,c) cimg_for3YZ(img,y,z)
  1039. #define cimg_for3XYZC(img,x,y,z,c) cimg_for3C(img,c) cimg_for3XYZ(img,x,y,z)
  1040. #define cimg_for_in3(bound,i0,i1,i) \
  1041. for (int i = (int)(i0)<0?0:(int)(i0), \
  1042. _p1##i = i - 1<0?0:i - 1, \
  1043. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
  1044. i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
  1045. _p1##i = i++, ++_n1##i)
  1046. #define cimg_for_in3X(img,x0,x1,x) cimg_for_in3((img)._width,x0,x1,x)
  1047. #define cimg_for_in3Y(img,y0,y1,y) cimg_for_in3((img)._height,y0,y1,y)
  1048. #define cimg_for_in3Z(img,z0,z1,z) cimg_for_in3((img)._depth,z0,z1,z)
  1049. #define cimg_for_in3C(img,c0,c1,c) cimg_for_in3((img)._spectrum,c0,c1,c)
  1050. #define cimg_for_in3XY(img,x0,y0,x1,y1,x,y) cimg_for_in3Y(img,y0,y1,y) cimg_for_in3X(img,x0,x1,x)
  1051. #define cimg_for_in3XZ(img,x0,z0,x1,z1,x,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3X(img,x0,x1,x)
  1052. #define cimg_for_in3XC(img,x0,c0,x1,c1,x,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3X(img,x0,x1,x)
  1053. #define cimg_for_in3YZ(img,y0,z0,y1,z1,y,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3Y(img,y0,y1,y)
  1054. #define cimg_for_in3YC(img,y0,c0,y1,c1,y,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3Y(img,y0,y1,y)
  1055. #define cimg_for_in3ZC(img,z0,c0,z1,c1,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3Z(img,z0,z1,z)
  1056. #define cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in3Z(img,z0,z1,z) cimg_for_in3XY(img,x0,y0,x1,y1,x,y)
  1057. #define cimg_for_in3XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3XZ(img,x0,y0,x1,y1,x,z)
  1058. #define cimg_for_in3YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in3C(img,c0,c1,c) cimg_for_in3YZ(img,y0,z0,y1,z1,y,z)
  1059. #define cimg_for_in3XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1060. cimg_for_in3C(img,c0,c1,c) cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1061. #define cimg_for4(bound,i) \
  1062. for (int i = 0, _p1##i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1063. _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
  1064. _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
  1065. _p1##i = i++, ++_n1##i, ++_n2##i)
  1066. #define cimg_for4X(img,x) cimg_for4((img)._width,x)
  1067. #define cimg_for4Y(img,y) cimg_for4((img)._height,y)
  1068. #define cimg_for4Z(img,z) cimg_for4((img)._depth,z)
  1069. #define cimg_for4C(img,c) cimg_for4((img)._spectrum,c)
  1070. #define cimg_for4XY(img,x,y) cimg_for4Y(img,y) cimg_for4X(img,x)
  1071. #define cimg_for4XZ(img,x,z) cimg_for4Z(img,z) cimg_for4X(img,x)
  1072. #define cimg_for4XC(img,x,c) cimg_for4C(img,c) cimg_for4X(img,x)
  1073. #define cimg_for4YZ(img,y,z) cimg_for4Z(img,z) cimg_for4Y(img,y)
  1074. #define cimg_for4YC(img,y,c) cimg_for4C(img,c) cimg_for4Y(img,y)
  1075. #define cimg_for4ZC(img,z,c) cimg_for4C(img,c) cimg_for4Z(img,z)
  1076. #define cimg_for4XYZ(img,x,y,z) cimg_for4Z(img,z) cimg_for4XY(img,x,y)
  1077. #define cimg_for4XZC(img,x,z,c) cimg_for4C(img,c) cimg_for4XZ(img,x,z)
  1078. #define cimg_for4YZC(img,y,z,c) cimg_for4C(img,c) cimg_for4YZ(img,y,z)
  1079. #define cimg_for4XYZC(img,x,y,z,c) cimg_for4C(img,c) cimg_for4XYZ(img,x,y,z)
  1080. #define cimg_for_in4(bound,i0,i1,i) \
  1081. for (int i = (int)(i0)<0?0:(int)(i0), \
  1082. _p1##i = i - 1<0?0:i - 1, \
  1083. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1084. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
  1085. i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
  1086. _p1##i = i++, ++_n1##i, ++_n2##i)
  1087. #define cimg_for_in4X(img,x0,x1,x) cimg_for_in4((img)._width,x0,x1,x)
  1088. #define cimg_for_in4Y(img,y0,y1,y) cimg_for_in4((img)._height,y0,y1,y)
  1089. #define cimg_for_in4Z(img,z0,z1,z) cimg_for_in4((img)._depth,z0,z1,z)
  1090. #define cimg_for_in4C(img,c0,c1,c) cimg_for_in4((img)._spectrum,c0,c1,c)
  1091. #define cimg_for_in4XY(img,x0,y0,x1,y1,x,y) cimg_for_in4Y(img,y0,y1,y) cimg_for_in4X(img,x0,x1,x)
  1092. #define cimg_for_in4XZ(img,x0,z0,x1,z1,x,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4X(img,x0,x1,x)
  1093. #define cimg_for_in4XC(img,x0,c0,x1,c1,x,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4X(img,x0,x1,x)
  1094. #define cimg_for_in4YZ(img,y0,z0,y1,z1,y,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4Y(img,y0,y1,y)
  1095. #define cimg_for_in4YC(img,y0,c0,y1,c1,y,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4Y(img,y0,y1,y)
  1096. #define cimg_for_in4ZC(img,z0,c0,z1,c1,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4Z(img,z0,z1,z)
  1097. #define cimg_for_in4XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in4Z(img,z0,z1,z) cimg_for_in4XY(img,x0,y0,x1,y1,x,y)
  1098. #define cimg_for_in4XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4XZ(img,x0,y0,x1,y1,x,z)
  1099. #define cimg_for_in4YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in4C(img,c0,c1,c) cimg_for_in4YZ(img,y0,z0,y1,z1,y,z)
  1100. #define cimg_for_in4XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1101. cimg_for_in4C(img,c0,c1,c) cimg_for_in4XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1102. #define cimg_for5(bound,i) \
  1103. for (int i = 0, _p2##i = 0, _p1##i = 0, \
  1104. _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1105. _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
  1106. _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
  1107. _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
  1108. #define cimg_for5X(img,x) cimg_for5((img)._width,x)
  1109. #define cimg_for5Y(img,y) cimg_for5((img)._height,y)
  1110. #define cimg_for5Z(img,z) cimg_for5((img)._depth,z)
  1111. #define cimg_for5C(img,c) cimg_for5((img)._spectrum,c)
  1112. #define cimg_for5XY(img,x,y) cimg_for5Y(img,y) cimg_for5X(img,x)
  1113. #define cimg_for5XZ(img,x,z) cimg_for5Z(img,z) cimg_for5X(img,x)
  1114. #define cimg_for5XC(img,x,c) cimg_for5C(img,c) cimg_for5X(img,x)
  1115. #define cimg_for5YZ(img,y,z) cimg_for5Z(img,z) cimg_for5Y(img,y)
  1116. #define cimg_for5YC(img,y,c) cimg_for5C(img,c) cimg_for5Y(img,y)
  1117. #define cimg_for5ZC(img,z,c) cimg_for5C(img,c) cimg_for5Z(img,z)
  1118. #define cimg_for5XYZ(img,x,y,z) cimg_for5Z(img,z) cimg_for5XY(img,x,y)
  1119. #define cimg_for5XZC(img,x,z,c) cimg_for5C(img,c) cimg_for5XZ(img,x,z)
  1120. #define cimg_for5YZC(img,y,z,c) cimg_for5C(img,c) cimg_for5YZ(img,y,z)
  1121. #define cimg_for5XYZC(img,x,y,z,c) cimg_for5C(img,c) cimg_for5XYZ(img,x,y,z)
  1122. #define cimg_for_in5(bound,i0,i1,i) \
  1123. for (int i = (int)(i0)<0?0:(int)(i0), \
  1124. _p2##i = i - 2<0?0:i - 2, \
  1125. _p1##i = i - 1<0?0:i - 1, \
  1126. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1127. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
  1128. i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
  1129. _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
  1130. #define cimg_for_in5X(img,x0,x1,x) cimg_for_in5((img)._width,x0,x1,x)
  1131. #define cimg_for_in5Y(img,y0,y1,y) cimg_for_in5((img)._height,y0,y1,y)
  1132. #define cimg_for_in5Z(img,z0,z1,z) cimg_for_in5((img)._depth,z0,z1,z)
  1133. #define cimg_for_in5C(img,c0,c1,c) cimg_for_in5((img)._spectrum,c0,c1,c)
  1134. #define cimg_for_in5XY(img,x0,y0,x1,y1,x,y) cimg_for_in5Y(img,y0,y1,y) cimg_for_in5X(img,x0,x1,x)
  1135. #define cimg_for_in5XZ(img,x0,z0,x1,z1,x,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5X(img,x0,x1,x)
  1136. #define cimg_for_in5XC(img,x0,c0,x1,c1,x,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5X(img,x0,x1,x)
  1137. #define cimg_for_in5YZ(img,y0,z0,y1,z1,y,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5Y(img,y0,y1,y)
  1138. #define cimg_for_in5YC(img,y0,c0,y1,c1,y,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5Y(img,y0,y1,y)
  1139. #define cimg_for_in5ZC(img,z0,c0,z1,c1,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5Z(img,z0,z1,z)
  1140. #define cimg_for_in5XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in5Z(img,z0,z1,z) cimg_for_in5XY(img,x0,y0,x1,y1,x,y)
  1141. #define cimg_for_in5XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5XZ(img,x0,y0,x1,y1,x,z)
  1142. #define cimg_for_in5YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in5C(img,c0,c1,c) cimg_for_in5YZ(img,y0,z0,y1,z1,y,z)
  1143. #define cimg_for_in5XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1144. cimg_for_in5C(img,c0,c1,c) cimg_for_in5XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1145. #define cimg_for6(bound,i) \
  1146. for (int i = 0, _p2##i = 0, _p1##i = 0, \
  1147. _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1148. _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1149. _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
  1150. _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
  1151. _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
  1152. #define cimg_for6X(img,x) cimg_for6((img)._width,x)
  1153. #define cimg_for6Y(img,y) cimg_for6((img)._height,y)
  1154. #define cimg_for6Z(img,z) cimg_for6((img)._depth,z)
  1155. #define cimg_for6C(img,c) cimg_for6((img)._spectrum,c)
  1156. #define cimg_for6XY(img,x,y) cimg_for6Y(img,y) cimg_for6X(img,x)
  1157. #define cimg_for6XZ(img,x,z) cimg_for6Z(img,z) cimg_for6X(img,x)
  1158. #define cimg_for6XC(img,x,c) cimg_for6C(img,c) cimg_for6X(img,x)
  1159. #define cimg_for6YZ(img,y,z) cimg_for6Z(img,z) cimg_for6Y(img,y)
  1160. #define cimg_for6YC(img,y,c) cimg_for6C(img,c) cimg_for6Y(img,y)
  1161. #define cimg_for6ZC(img,z,c) cimg_for6C(img,c) cimg_for6Z(img,z)
  1162. #define cimg_for6XYZ(img,x,y,z) cimg_for6Z(img,z) cimg_for6XY(img,x,y)
  1163. #define cimg_for6XZC(img,x,z,c) cimg_for6C(img,c) cimg_for6XZ(img,x,z)
  1164. #define cimg_for6YZC(img,y,z,c) cimg_for6C(img,c) cimg_for6YZ(img,y,z)
  1165. #define cimg_for6XYZC(img,x,y,z,c) cimg_for6C(img,c) cimg_for6XYZ(img,x,y,z)
  1166. #define cimg_for_in6(bound,i0,i1,i) \
  1167. for (int i = (int)(i0)<0?0:(int)(i0), \
  1168. _p2##i = i - 2<0?0:i - 2, \
  1169. _p1##i = i - 1<0?0:i - 1, \
  1170. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1171. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1172. _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
  1173. i<=(int)(i1) && \
  1174. (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
  1175. _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
  1176. #define cimg_for_in6X(img,x0,x1,x) cimg_for_in6((img)._width,x0,x1,x)
  1177. #define cimg_for_in6Y(img,y0,y1,y) cimg_for_in6((img)._height,y0,y1,y)
  1178. #define cimg_for_in6Z(img,z0,z1,z) cimg_for_in6((img)._depth,z0,z1,z)
  1179. #define cimg_for_in6C(img,c0,c1,c) cimg_for_in6((img)._spectrum,c0,c1,c)
  1180. #define cimg_for_in6XY(img,x0,y0,x1,y1,x,y) cimg_for_in6Y(img,y0,y1,y) cimg_for_in6X(img,x0,x1,x)
  1181. #define cimg_for_in6XZ(img,x0,z0,x1,z1,x,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6X(img,x0,x1,x)
  1182. #define cimg_for_in6XC(img,x0,c0,x1,c1,x,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6X(img,x0,x1,x)
  1183. #define cimg_for_in6YZ(img,y0,z0,y1,z1,y,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6Y(img,y0,y1,y)
  1184. #define cimg_for_in6YC(img,y0,c0,y1,c1,y,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6Y(img,y0,y1,y)
  1185. #define cimg_for_in6ZC(img,z0,c0,z1,c1,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6Z(img,z0,z1,z)
  1186. #define cimg_for_in6XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in6Z(img,z0,z1,z) cimg_for_in6XY(img,x0,y0,x1,y1,x,y)
  1187. #define cimg_for_in6XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6XZ(img,x0,y0,x1,y1,x,z)
  1188. #define cimg_for_in6YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in6C(img,c0,c1,c) cimg_for_in6YZ(img,y0,z0,y1,z1,y,z)
  1189. #define cimg_for_in6XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1190. cimg_for_in6C(img,c0,c1,c) cimg_for_in6XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1191. #define cimg_for7(bound,i) \
  1192. for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
  1193. _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1194. _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1195. _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
  1196. _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
  1197. _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
  1198. #define cimg_for7X(img,x) cimg_for7((img)._width,x)
  1199. #define cimg_for7Y(img,y) cimg_for7((img)._height,y)
  1200. #define cimg_for7Z(img,z) cimg_for7((img)._depth,z)
  1201. #define cimg_for7C(img,c) cimg_for7((img)._spectrum,c)
  1202. #define cimg_for7XY(img,x,y) cimg_for7Y(img,y) cimg_for7X(img,x)
  1203. #define cimg_for7XZ(img,x,z) cimg_for7Z(img,z) cimg_for7X(img,x)
  1204. #define cimg_for7XC(img,x,c) cimg_for7C(img,c) cimg_for7X(img,x)
  1205. #define cimg_for7YZ(img,y,z) cimg_for7Z(img,z) cimg_for7Y(img,y)
  1206. #define cimg_for7YC(img,y,c) cimg_for7C(img,c) cimg_for7Y(img,y)
  1207. #define cimg_for7ZC(img,z,c) cimg_for7C(img,c) cimg_for7Z(img,z)
  1208. #define cimg_for7XYZ(img,x,y,z) cimg_for7Z(img,z) cimg_for7XY(img,x,y)
  1209. #define cimg_for7XZC(img,x,z,c) cimg_for7C(img,c) cimg_for7XZ(img,x,z)
  1210. #define cimg_for7YZC(img,y,z,c) cimg_for7C(img,c) cimg_for7YZ(img,y,z)
  1211. #define cimg_for7XYZC(img,x,y,z,c) cimg_for7C(img,c) cimg_for7XYZ(img,x,y,z)
  1212. #define cimg_for_in7(bound,i0,i1,i) \
  1213. for (int i = (int)(i0)<0?0:(int)(i0), \
  1214. _p3##i = i - 3<0?0:i - 3, \
  1215. _p2##i = i - 2<0?0:i - 2, \
  1216. _p1##i = i - 1<0?0:i - 1, \
  1217. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1218. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1219. _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
  1220. i<=(int)(i1) && \
  1221. (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
  1222. _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
  1223. #define cimg_for_in7X(img,x0,x1,x) cimg_for_in7((img)._width,x0,x1,x)
  1224. #define cimg_for_in7Y(img,y0,y1,y) cimg_for_in7((img)._height,y0,y1,y)
  1225. #define cimg_for_in7Z(img,z0,z1,z) cimg_for_in7((img)._depth,z0,z1,z)
  1226. #define cimg_for_in7C(img,c0,c1,c) cimg_for_in7((img)._spectrum,c0,c1,c)
  1227. #define cimg_for_in7XY(img,x0,y0,x1,y1,x,y) cimg_for_in7Y(img,y0,y1,y) cimg_for_in7X(img,x0,x1,x)
  1228. #define cimg_for_in7XZ(img,x0,z0,x1,z1,x,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7X(img,x0,x1,x)
  1229. #define cimg_for_in7XC(img,x0,c0,x1,c1,x,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7X(img,x0,x1,x)
  1230. #define cimg_for_in7YZ(img,y0,z0,y1,z1,y,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7Y(img,y0,y1,y)
  1231. #define cimg_for_in7YC(img,y0,c0,y1,c1,y,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7Y(img,y0,y1,y)
  1232. #define cimg_for_in7ZC(img,z0,c0,z1,c1,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7Z(img,z0,z1,z)
  1233. #define cimg_for_in7XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in7Z(img,z0,z1,z) cimg_for_in7XY(img,x0,y0,x1,y1,x,y)
  1234. #define cimg_for_in7XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7XZ(img,x0,y0,x1,y1,x,z)
  1235. #define cimg_for_in7YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in7C(img,c0,c1,c) cimg_for_in7YZ(img,y0,z0,y1,z1,y,z)
  1236. #define cimg_for_in7XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1237. cimg_for_in7C(img,c0,c1,c) cimg_for_in7XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1238. #define cimg_for8(bound,i) \
  1239. for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
  1240. _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
  1241. _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
  1242. _n3##i = 3>=(bound)?(int)(bound) - 1:3, \
  1243. _n4##i = 4>=(bound)?(int)(bound) - 1:4; \
  1244. _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
  1245. i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
  1246. _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
  1247. #define cimg_for8X(img,x) cimg_for8((img)._width,x)
  1248. #define cimg_for8Y(img,y) cimg_for8((img)._height,y)
  1249. #define cimg_for8Z(img,z) cimg_for8((img)._depth,z)
  1250. #define cimg_for8C(img,c) cimg_for8((img)._spectrum,c)
  1251. #define cimg_for8XY(img,x,y) cimg_for8Y(img,y) cimg_for8X(img,x)
  1252. #define cimg_for8XZ(img,x,z) cimg_for8Z(img,z) cimg_for8X(img,x)
  1253. #define cimg_for8XC(img,x,c) cimg_for8C(img,c) cimg_for8X(img,x)
  1254. #define cimg_for8YZ(img,y,z) cimg_for8Z(img,z) cimg_for8Y(img,y)
  1255. #define cimg_for8YC(img,y,c) cimg_for8C(img,c) cimg_for8Y(img,y)
  1256. #define cimg_for8ZC(img,z,c) cimg_for8C(img,c) cimg_for8Z(img,z)
  1257. #define cimg_for8XYZ(img,x,y,z) cimg_for8Z(img,z) cimg_for8XY(img,x,y)
  1258. #define cimg_for8XZC(img,x,z,c) cimg_for8C(img,c) cimg_for8XZ(img,x,z)
  1259. #define cimg_for8YZC(img,y,z,c) cimg_for8C(img,c) cimg_for8YZ(img,y,z)
  1260. #define cimg_for8XYZC(img,x,y,z,c) cimg_for8C(img,c) cimg_for8XYZ(img,x,y,z)
  1261. #define cimg_for_in8(bound,i0,i1,i) \
  1262. for (int i = (int)(i0)<0?0:(int)(i0), \
  1263. _p3##i = i - 3<0?0:i - 3, \
  1264. _p2##i = i - 2<0?0:i - 2, \
  1265. _p1##i = i - 1<0?0:i - 1, \
  1266. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1267. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1268. _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
  1269. _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
  1270. i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
  1271. i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
  1272. _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
  1273. #define cimg_for_in8X(img,x0,x1,x) cimg_for_in8((img)._width,x0,x1,x)
  1274. #define cimg_for_in8Y(img,y0,y1,y) cimg_for_in8((img)._height,y0,y1,y)
  1275. #define cimg_for_in8Z(img,z0,z1,z) cimg_for_in8((img)._depth,z0,z1,z)
  1276. #define cimg_for_in8C(img,c0,c1,c) cimg_for_in8((img)._spectrum,c0,c1,c)
  1277. #define cimg_for_in8XY(img,x0,y0,x1,y1,x,y) cimg_for_in8Y(img,y0,y1,y) cimg_for_in8X(img,x0,x1,x)
  1278. #define cimg_for_in8XZ(img,x0,z0,x1,z1,x,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8X(img,x0,x1,x)
  1279. #define cimg_for_in8XC(img,x0,c0,x1,c1,x,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8X(img,x0,x1,x)
  1280. #define cimg_for_in8YZ(img,y0,z0,y1,z1,y,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8Y(img,y0,y1,y)
  1281. #define cimg_for_in8YC(img,y0,c0,y1,c1,y,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8Y(img,y0,y1,y)
  1282. #define cimg_for_in8ZC(img,z0,c0,z1,c1,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8Z(img,z0,z1,z)
  1283. #define cimg_for_in8XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in8Z(img,z0,z1,z) cimg_for_in8XY(img,x0,y0,x1,y1,x,y)
  1284. #define cimg_for_in8XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8XZ(img,x0,y0,x1,y1,x,z)
  1285. #define cimg_for_in8YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in8C(img,c0,c1,c) cimg_for_in8YZ(img,y0,z0,y1,z1,y,z)
  1286. #define cimg_for_in8XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1287. cimg_for_in8C(img,c0,c1,c) cimg_for_in8XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1288. #define cimg_for9(bound,i) \
  1289. for (int i = 0, _p4##i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
  1290. _n1##i = 1>=(int)(bound)?(int)(bound) - 1:1, \
  1291. _n2##i = 2>=(int)(bound)?(int)(bound) - 1:2, \
  1292. _n3##i = 3>=(int)(bound)?(int)(bound) - 1:3, \
  1293. _n4##i = 4>=(int)(bound)?(int)(bound) - 1:4; \
  1294. _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
  1295. i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
  1296. _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
  1297. #define cimg_for9X(img,x) cimg_for9((img)._width,x)
  1298. #define cimg_for9Y(img,y) cimg_for9((img)._height,y)
  1299. #define cimg_for9Z(img,z) cimg_for9((img)._depth,z)
  1300. #define cimg_for9C(img,c) cimg_for9((img)._spectrum,c)
  1301. #define cimg_for9XY(img,x,y) cimg_for9Y(img,y) cimg_for9X(img,x)
  1302. #define cimg_for9XZ(img,x,z) cimg_for9Z(img,z) cimg_for9X(img,x)
  1303. #define cimg_for9XC(img,x,c) cimg_for9C(img,c) cimg_for9X(img,x)
  1304. #define cimg_for9YZ(img,y,z) cimg_for9Z(img,z) cimg_for9Y(img,y)
  1305. #define cimg_for9YC(img,y,c) cimg_for9C(img,c) cimg_for9Y(img,y)
  1306. #define cimg_for9ZC(img,z,c) cimg_for9C(img,c) cimg_for9Z(img,z)
  1307. #define cimg_for9XYZ(img,x,y,z) cimg_for9Z(img,z) cimg_for9XY(img,x,y)
  1308. #define cimg_for9XZC(img,x,z,c) cimg_for9C(img,c) cimg_for9XZ(img,x,z)
  1309. #define cimg_for9YZC(img,y,z,c) cimg_for9C(img,c) cimg_for9YZ(img,y,z)
  1310. #define cimg_for9XYZC(img,x,y,z,c) cimg_for9C(img,c) cimg_for9XYZ(img,x,y,z)
  1311. #define cimg_for_in9(bound,i0,i1,i) \
  1312. for (int i = (int)(i0)<0?0:(int)(i0), \
  1313. _p4##i = i - 4<0?0:i - 4, \
  1314. _p3##i = i - 3<0?0:i - 3, \
  1315. _p2##i = i - 2<0?0:i - 2, \
  1316. _p1##i = i - 1<0?0:i - 1, \
  1317. _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
  1318. _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
  1319. _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
  1320. _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
  1321. i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
  1322. i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
  1323. _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
  1324. #define cimg_for_in9X(img,x0,x1,x) cimg_for_in9((img)._width,x0,x1,x)
  1325. #define cimg_for_in9Y(img,y0,y1,y) cimg_for_in9((img)._height,y0,y1,y)
  1326. #define cimg_for_in9Z(img,z0,z1,z) cimg_for_in9((img)._depth,z0,z1,z)
  1327. #define cimg_for_in9C(img,c0,c1,c) cimg_for_in9((img)._spectrum,c0,c1,c)
  1328. #define cimg_for_in9XY(img,x0,y0,x1,y1,x,y) cimg_for_in9Y(img,y0,y1,y) cimg_for_in9X(img,x0,x1,x)
  1329. #define cimg_for_in9XZ(img,x0,z0,x1,z1,x,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9X(img,x0,x1,x)
  1330. #define cimg_for_in9XC(img,x0,c0,x1,c1,x,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9X(img,x0,x1,x)
  1331. #define cimg_for_in9YZ(img,y0,z0,y1,z1,y,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9Y(img,y0,y1,y)
  1332. #define cimg_for_in9YC(img,y0,c0,y1,c1,y,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9Y(img,y0,y1,y)
  1333. #define cimg_for_in9ZC(img,z0,c0,z1,c1,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9Z(img,z0,z1,z)
  1334. #define cimg_for_in9XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) cimg_for_in9Z(img,z0,z1,z) cimg_for_in9XY(img,x0,y0,x1,y1,x,y)
  1335. #define cimg_for_in9XZC(img,x0,z0,c0,x1,y1,c1,x,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9XZ(img,x0,y0,x1,y1,x,z)
  1336. #define cimg_for_in9YZC(img,y0,z0,c0,y1,z1,c1,y,z,c) cimg_for_in9C(img,c0,c1,c) cimg_for_in9YZ(img,y0,z0,y1,z1,y,z)
  1337. #define cimg_for_in9XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
  1338. cimg_for_in9C(img,c0,c1,c) cimg_for_in9XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
  1339. #define cimg_for2x2(img,x,y,z,c,I,T) \
  1340. cimg_for2((img)._height,y) for (int x = 0, \
  1341. _n1##x = (int)( \
  1342. (I[0] = (T)(img)(0,y,z,c)), \
  1343. (I[2] = (T)(img)(0,_n1##y,z,c)), \
  1344. 1>=(img)._width?(img).width() - 1:1); \
  1345. (_n1##x<(img).width() && ( \
  1346. (I[1] = (T)(img)(_n1##x,y,z,c)), \
  1347. (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  1348. x==--_n1##x; \
  1349. I[0] = I[1], \
  1350. I[2] = I[3], \
  1351. ++x, ++_n1##x)
  1352. #define cimg_for_in2x2(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1353. cimg_for_in2((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1354. _n1##x = (int)( \
  1355. (I[0] = (T)(img)(x,y,z,c)), \
  1356. (I[2] = (T)(img)(x,_n1##y,z,c)), \
  1357. x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
  1358. x<=(int)(x1) && ((_n1##x<(img).width() && ( \
  1359. (I[1] = (T)(img)(_n1##x,y,z,c)), \
  1360. (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  1361. x==--_n1##x); \
  1362. I[0] = I[1], \
  1363. I[2] = I[3], \
  1364. ++x, ++_n1##x)
  1365. #define cimg_for3x3(img,x,y,z,c,I,T) \
  1366. cimg_for3((img)._height,y) for (int x = 0, \
  1367. _p1##x = 0, \
  1368. _n1##x = (int)( \
  1369. (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1370. (I[3] = I[4] = (T)(img)(0,y,z,c)), \
  1371. (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
  1372. 1>=(img)._width?(img).width() - 1:1); \
  1373. (_n1##x<(img).width() && ( \
  1374. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1375. (I[5] = (T)(img)(_n1##x,y,z,c)), \
  1376. (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  1377. x==--_n1##x; \
  1378. I[0] = I[1], I[1] = I[2], \
  1379. I[3] = I[4], I[4] = I[5], \
  1380. I[6] = I[7], I[7] = I[8], \
  1381. _p1##x = x++, ++_n1##x)
  1382. #define cimg_for_in3x3(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1383. cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1384. _p1##x = x - 1<0?0:x - 1, \
  1385. _n1##x = (int)( \
  1386. (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1387. (I[3] = (T)(img)(_p1##x,y,z,c)), \
  1388. (I[6] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1389. (I[1] = (T)(img)(x,_p1##y,z,c)), \
  1390. (I[4] = (T)(img)(x,y,z,c)), \
  1391. (I[7] = (T)(img)(x,_n1##y,z,c)), \
  1392. x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
  1393. x<=(int)(x1) && ((_n1##x<(img).width() && ( \
  1394. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1395. (I[5] = (T)(img)(_n1##x,y,z,c)), \
  1396. (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  1397. x==--_n1##x); \
  1398. I[0] = I[1], I[1] = I[2], \
  1399. I[3] = I[4], I[4] = I[5], \
  1400. I[6] = I[7], I[7] = I[8], \
  1401. _p1##x = x++, ++_n1##x)
  1402. #define cimg_for4x4(img,x,y,z,c,I,T) \
  1403. cimg_for4((img)._height,y) for (int x = 0, \
  1404. _p1##x = 0, \
  1405. _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1406. _n2##x = (int)( \
  1407. (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1408. (I[4] = I[5] = (T)(img)(0,y,z,c)), \
  1409. (I[8] = I[9] = (T)(img)(0,_n1##y,z,c)), \
  1410. (I[12] = I[13] = (T)(img)(0,_n2##y,z,c)), \
  1411. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1412. (I[6] = (T)(img)(_n1##x,y,z,c)), \
  1413. (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1414. (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1415. 2>=(img)._width?(img).width() - 1:2); \
  1416. (_n2##x<(img).width() && ( \
  1417. (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1418. (I[7] = (T)(img)(_n2##x,y,z,c)), \
  1419. (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1420. (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
  1421. _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \
  1422. I[0] = I[1], I[1] = I[2], I[2] = I[3], \
  1423. I[4] = I[5], I[5] = I[6], I[6] = I[7], \
  1424. I[8] = I[9], I[9] = I[10], I[10] = I[11], \
  1425. I[12] = I[13], I[13] = I[14], I[14] = I[15], \
  1426. _p1##x = x++, ++_n1##x, ++_n2##x)
  1427. #define cimg_for_in4x4(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1428. cimg_for_in4((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1429. _p1##x = x - 1<0?0:x - 1, \
  1430. _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1431. _n2##x = (int)( \
  1432. (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1433. (I[4] = (T)(img)(_p1##x,y,z,c)), \
  1434. (I[8] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1435. (I[12] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1436. (I[1] = (T)(img)(x,_p1##y,z,c)), \
  1437. (I[5] = (T)(img)(x,y,z,c)), \
  1438. (I[9] = (T)(img)(x,_n1##y,z,c)), \
  1439. (I[13] = (T)(img)(x,_n2##y,z,c)), \
  1440. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1441. (I[6] = (T)(img)(_n1##x,y,z,c)), \
  1442. (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1443. (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1444. x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
  1445. x<=(int)(x1) && ((_n2##x<(img).width() && ( \
  1446. (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1447. (I[7] = (T)(img)(_n2##x,y,z,c)), \
  1448. (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1449. (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
  1450. _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \
  1451. I[0] = I[1], I[1] = I[2], I[2] = I[3], \
  1452. I[4] = I[5], I[5] = I[6], I[6] = I[7], \
  1453. I[8] = I[9], I[9] = I[10], I[10] = I[11], \
  1454. I[12] = I[13], I[13] = I[14], I[14] = I[15], \
  1455. _p1##x = x++, ++_n1##x, ++_n2##x)
  1456. #define cimg_for5x5(img,x,y,z,c,I,T) \
  1457. cimg_for5((img)._height,y) for (int x = 0, \
  1458. _p2##x = 0, _p1##x = 0, \
  1459. _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1460. _n2##x = (int)( \
  1461. (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1462. (I[5] = I[6] = I[7] = (T)(img)(0,_p1##y,z,c)), \
  1463. (I[10] = I[11] = I[12] = (T)(img)(0,y,z,c)), \
  1464. (I[15] = I[16] = I[17] = (T)(img)(0,_n1##y,z,c)), \
  1465. (I[20] = I[21] = I[22] = (T)(img)(0,_n2##y,z,c)), \
  1466. (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1467. (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1468. (I[13] = (T)(img)(_n1##x,y,z,c)), \
  1469. (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1470. (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1471. 2>=(img)._width?(img).width() - 1:2); \
  1472. (_n2##x<(img).width() && ( \
  1473. (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1474. (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1475. (I[14] = (T)(img)(_n2##x,y,z,c)), \
  1476. (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1477. (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
  1478. _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \
  1479. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \
  1480. I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \
  1481. I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \
  1482. I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \
  1483. I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
  1484. _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x)
  1485. #define cimg_for_in5x5(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1486. cimg_for_in5((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1487. _p2##x = x - 2<0?0:x - 2, \
  1488. _p1##x = x - 1<0?0:x - 1, \
  1489. _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1490. _n2##x = (int)( \
  1491. (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1492. (I[5] = (T)(img)(_p2##x,_p1##y,z,c)), \
  1493. (I[10] = (T)(img)(_p2##x,y,z,c)), \
  1494. (I[15] = (T)(img)(_p2##x,_n1##y,z,c)), \
  1495. (I[20] = (T)(img)(_p2##x,_n2##y,z,c)), \
  1496. (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \
  1497. (I[6] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1498. (I[11] = (T)(img)(_p1##x,y,z,c)), \
  1499. (I[16] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1500. (I[21] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1501. (I[2] = (T)(img)(x,_p2##y,z,c)), \
  1502. (I[7] = (T)(img)(x,_p1##y,z,c)), \
  1503. (I[12] = (T)(img)(x,y,z,c)), \
  1504. (I[17] = (T)(img)(x,_n1##y,z,c)), \
  1505. (I[22] = (T)(img)(x,_n2##y,z,c)), \
  1506. (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1507. (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1508. (I[13] = (T)(img)(_n1##x,y,z,c)), \
  1509. (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1510. (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1511. x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
  1512. x<=(int)(x1) && ((_n2##x<(img).width() && ( \
  1513. (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1514. (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1515. (I[14] = (T)(img)(_n2##x,y,z,c)), \
  1516. (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1517. (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
  1518. _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \
  1519. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \
  1520. I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \
  1521. I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \
  1522. I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \
  1523. I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
  1524. _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x)
  1525. #define cimg_for6x6(img,x,y,z,c,I,T) \
  1526. cimg_for6((img)._height,y) for (int x = 0, \
  1527. _p2##x = 0, _p1##x = 0, \
  1528. _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1529. _n2##x = 2>=(img)._width?(img).width() - 1:2, \
  1530. _n3##x = (int)( \
  1531. (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1532. (I[6] = I[7] = I[8] = (T)(img)(0,_p1##y,z,c)), \
  1533. (I[12] = I[13] = I[14] = (T)(img)(0,y,z,c)), \
  1534. (I[18] = I[19] = I[20] = (T)(img)(0,_n1##y,z,c)), \
  1535. (I[24] = I[25] = I[26] = (T)(img)(0,_n2##y,z,c)), \
  1536. (I[30] = I[31] = I[32] = (T)(img)(0,_n3##y,z,c)), \
  1537. (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1538. (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1539. (I[15] = (T)(img)(_n1##x,y,z,c)), \
  1540. (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1541. (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1542. (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1543. (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1544. (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1545. (I[16] = (T)(img)(_n2##x,y,z,c)), \
  1546. (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1547. (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1548. (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1549. 3>=(img)._width?(img).width() - 1:3); \
  1550. (_n3##x<(img).width() && ( \
  1551. (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1552. (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1553. (I[17] = (T)(img)(_n3##x,y,z,c)), \
  1554. (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1555. (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1556. (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
  1557. _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x); \
  1558. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \
  1559. I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \
  1560. I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], I[16] = I[17], \
  1561. I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \
  1562. I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \
  1563. I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \
  1564. _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
  1565. #define cimg_for_in6x6(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1566. cimg_for_in6((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)x0, \
  1567. _p2##x = x - 2<0?0:x - 2, \
  1568. _p1##x = x - 1<0?0:x - 1, \
  1569. _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1570. _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
  1571. _n3##x = (int)( \
  1572. (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1573. (I[6] = (T)(img)(_p2##x,_p1##y,z,c)), \
  1574. (I[12] = (T)(img)(_p2##x,y,z,c)), \
  1575. (I[18] = (T)(img)(_p2##x,_n1##y,z,c)), \
  1576. (I[24] = (T)(img)(_p2##x,_n2##y,z,c)), \
  1577. (I[30] = (T)(img)(_p2##x,_n3##y,z,c)), \
  1578. (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \
  1579. (I[7] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1580. (I[13] = (T)(img)(_p1##x,y,z,c)), \
  1581. (I[19] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1582. (I[25] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1583. (I[31] = (T)(img)(_p1##x,_n3##y,z,c)), \
  1584. (I[2] = (T)(img)(x,_p2##y,z,c)), \
  1585. (I[8] = (T)(img)(x,_p1##y,z,c)), \
  1586. (I[14] = (T)(img)(x,y,z,c)), \
  1587. (I[20] = (T)(img)(x,_n1##y,z,c)), \
  1588. (I[26] = (T)(img)(x,_n2##y,z,c)), \
  1589. (I[32] = (T)(img)(x,_n3##y,z,c)), \
  1590. (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1591. (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1592. (I[15] = (T)(img)(_n1##x,y,z,c)), \
  1593. (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1594. (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1595. (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1596. (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1597. (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1598. (I[16] = (T)(img)(_n2##x,y,z,c)), \
  1599. (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1600. (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1601. (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1602. x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
  1603. x<=(int)(x1) && ((_n3##x<(img).width() && ( \
  1604. (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1605. (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1606. (I[17] = (T)(img)(_n3##x,y,z,c)), \
  1607. (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1608. (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1609. (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
  1610. _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x)); \
  1611. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \
  1612. I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \
  1613. I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], I[16] = I[17], \
  1614. I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \
  1615. I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \
  1616. I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \
  1617. _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
  1618. #define cimg_for7x7(img,x,y,z,c,I,T) \
  1619. cimg_for7((img)._height,y) for (int x = 0, \
  1620. _p3##x = 0, _p2##x = 0, _p1##x = 0, \
  1621. _n1##x = 1>=(img)._width?(img).width() - 1:1, \
  1622. _n2##x = 2>=(img)._width?(img).width() - 1:2, \
  1623. _n3##x = (int)( \
  1624. (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
  1625. (I[7] = I[8] = I[9] = I[10] = (T)(img)(0,_p2##y,z,c)), \
  1626. (I[14] = I[15] = I[16] = I[17] = (T)(img)(0,_p1##y,z,c)), \
  1627. (I[21] = I[22] = I[23] = I[24] = (T)(img)(0,y,z,c)), \
  1628. (I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_n1##y,z,c)), \
  1629. (I[35] = I[36] = I[37] = I[38] = (T)(img)(0,_n2##y,z,c)), \
  1630. (I[42] = I[43] = I[44] = I[45] = (T)(img)(0,_n3##y,z,c)), \
  1631. (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1632. (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1633. (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1634. (I[25] = (T)(img)(_n1##x,y,z,c)), \
  1635. (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1636. (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1637. (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1638. (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
  1639. (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1640. (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1641. (I[26] = (T)(img)(_n2##x,y,z,c)), \
  1642. (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1643. (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1644. (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1645. 3>=(img)._width?(img).width() - 1:3); \
  1646. (_n3##x<(img).width() && ( \
  1647. (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
  1648. (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1649. (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1650. (I[27] = (T)(img)(_n3##x,y,z,c)), \
  1651. (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1652. (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1653. (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
  1654. _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x); \
  1655. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], \
  1656. I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \
  1657. I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \
  1658. I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \
  1659. I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \
  1660. I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \
  1661. I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
  1662. _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
  1663. #define cimg_for_in7x7(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1664. cimg_for_in7((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1665. _p3##x = x - 3<0?0:x - 3, \
  1666. _p2##x = x - 2<0?0:x - 2, \
  1667. _p1##x = x - 1<0?0:x - 1, \
  1668. _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
  1669. _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
  1670. _n3##x = (int)( \
  1671. (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
  1672. (I[7] = (T)(img)(_p3##x,_p2##y,z,c)), \
  1673. (I[14] = (T)(img)(_p3##x,_p1##y,z,c)), \
  1674. (I[21] = (T)(img)(_p3##x,y,z,c)), \
  1675. (I[28] = (T)(img)(_p3##x,_n1##y,z,c)), \
  1676. (I[35] = (T)(img)(_p3##x,_n2##y,z,c)), \
  1677. (I[42] = (T)(img)(_p3##x,_n3##y,z,c)), \
  1678. (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \
  1679. (I[8] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1680. (I[15] = (T)(img)(_p2##x,_p1##y,z,c)), \
  1681. (I[22] = (T)(img)(_p2##x,y,z,c)), \
  1682. (I[29] = (T)(img)(_p2##x,_n1##y,z,c)), \
  1683. (I[36] = (T)(img)(_p2##x,_n2##y,z,c)), \
  1684. (I[43] = (T)(img)(_p2##x,_n3##y,z,c)), \
  1685. (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \
  1686. (I[9] = (T)(img)(_p1##x,_p2##y,z,c)), \
  1687. (I[16] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1688. (I[23] = (T)(img)(_p1##x,y,z,c)), \
  1689. (I[30] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1690. (I[37] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1691. (I[44] = (T)(img)(_p1##x,_n3##y,z,c)), \
  1692. (I[3] = (T)(img)(x,_p3##y,z,c)), \
  1693. (I[10] = (T)(img)(x,_p2##y,z,c)), \
  1694. (I[17] = (T)(img)(x,_p1##y,z,c)), \
  1695. (I[24] = (T)(img)(x,y,z,c)), \
  1696. (I[31] = (T)(img)(x,_n1##y,z,c)), \
  1697. (I[38] = (T)(img)(x,_n2##y,z,c)), \
  1698. (I[45] = (T)(img)(x,_n3##y,z,c)), \
  1699. (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1700. (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1701. (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1702. (I[25] = (T)(img)(_n1##x,y,z,c)), \
  1703. (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1704. (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1705. (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1706. (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
  1707. (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1708. (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1709. (I[26] = (T)(img)(_n2##x,y,z,c)), \
  1710. (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1711. (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1712. (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1713. x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
  1714. x<=(int)(x1) && ((_n3##x<(img).width() && ( \
  1715. (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
  1716. (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1717. (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1718. (I[27] = (T)(img)(_n3##x,y,z,c)), \
  1719. (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1720. (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1721. (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
  1722. _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x)); \
  1723. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], \
  1724. I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \
  1725. I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \
  1726. I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \
  1727. I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \
  1728. I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \
  1729. I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
  1730. _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
  1731. #define cimg_for8x8(img,x,y,z,c,I,T) \
  1732. cimg_for8((img)._height,y) for (int x = 0, \
  1733. _p3##x = 0, _p2##x = 0, _p1##x = 0, \
  1734. _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
  1735. _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
  1736. _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
  1737. _n4##x = (int)( \
  1738. (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
  1739. (I[8] = I[9] = I[10] = I[11] = (T)(img)(0,_p2##y,z,c)), \
  1740. (I[16] = I[17] = I[18] = I[19] = (T)(img)(0,_p1##y,z,c)), \
  1741. (I[24] = I[25] = I[26] = I[27] = (T)(img)(0,y,z,c)), \
  1742. (I[32] = I[33] = I[34] = I[35] = (T)(img)(0,_n1##y,z,c)), \
  1743. (I[40] = I[41] = I[42] = I[43] = (T)(img)(0,_n2##y,z,c)), \
  1744. (I[48] = I[49] = I[50] = I[51] = (T)(img)(0,_n3##y,z,c)), \
  1745. (I[56] = I[57] = I[58] = I[59] = (T)(img)(0,_n4##y,z,c)), \
  1746. (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1747. (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1748. (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1749. (I[28] = (T)(img)(_n1##x,y,z,c)), \
  1750. (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1751. (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1752. (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1753. (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \
  1754. (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
  1755. (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1756. (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1757. (I[29] = (T)(img)(_n2##x,y,z,c)), \
  1758. (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1759. (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1760. (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1761. (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \
  1762. (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
  1763. (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1764. (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1765. (I[30] = (T)(img)(_n3##x,y,z,c)), \
  1766. (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1767. (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1768. (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
  1769. (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
  1770. 4>=((img)._width)?(img).width() - 1:4); \
  1771. (_n4##x<(img).width() && ( \
  1772. (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
  1773. (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
  1774. (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \
  1775. (I[31] = (T)(img)(_n4##x,y,z,c)), \
  1776. (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \
  1777. (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \
  1778. (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \
  1779. (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
  1780. _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \
  1781. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], \
  1782. I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], \
  1783. I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \
  1784. I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], \
  1785. I[32] = I[33], I[33] = I[34], I[34] = I[35], I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], \
  1786. I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], \
  1787. I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[53] = I[54], I[54] = I[55], \
  1788. I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[62] = I[63], \
  1789. _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
  1790. #define cimg_for_in8x8(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1791. cimg_for_in8((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1792. _p3##x = x - 3<0?0:x - 3, \
  1793. _p2##x = x - 2<0?0:x - 2, \
  1794. _p1##x = x - 1<0?0:x - 1, \
  1795. _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
  1796. _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
  1797. _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
  1798. _n4##x = (int)( \
  1799. (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
  1800. (I[8] = (T)(img)(_p3##x,_p2##y,z,c)), \
  1801. (I[16] = (T)(img)(_p3##x,_p1##y,z,c)), \
  1802. (I[24] = (T)(img)(_p3##x,y,z,c)), \
  1803. (I[32] = (T)(img)(_p3##x,_n1##y,z,c)), \
  1804. (I[40] = (T)(img)(_p3##x,_n2##y,z,c)), \
  1805. (I[48] = (T)(img)(_p3##x,_n3##y,z,c)), \
  1806. (I[56] = (T)(img)(_p3##x,_n4##y,z,c)), \
  1807. (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \
  1808. (I[9] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1809. (I[17] = (T)(img)(_p2##x,_p1##y,z,c)), \
  1810. (I[25] = (T)(img)(_p2##x,y,z,c)), \
  1811. (I[33] = (T)(img)(_p2##x,_n1##y,z,c)), \
  1812. (I[41] = (T)(img)(_p2##x,_n2##y,z,c)), \
  1813. (I[49] = (T)(img)(_p2##x,_n3##y,z,c)), \
  1814. (I[57] = (T)(img)(_p2##x,_n4##y,z,c)), \
  1815. (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \
  1816. (I[10] = (T)(img)(_p1##x,_p2##y,z,c)), \
  1817. (I[18] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1818. (I[26] = (T)(img)(_p1##x,y,z,c)), \
  1819. (I[34] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1820. (I[42] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1821. (I[50] = (T)(img)(_p1##x,_n3##y,z,c)), \
  1822. (I[58] = (T)(img)(_p1##x,_n4##y,z,c)), \
  1823. (I[3] = (T)(img)(x,_p3##y,z,c)), \
  1824. (I[11] = (T)(img)(x,_p2##y,z,c)), \
  1825. (I[19] = (T)(img)(x,_p1##y,z,c)), \
  1826. (I[27] = (T)(img)(x,y,z,c)), \
  1827. (I[35] = (T)(img)(x,_n1##y,z,c)), \
  1828. (I[43] = (T)(img)(x,_n2##y,z,c)), \
  1829. (I[51] = (T)(img)(x,_n3##y,z,c)), \
  1830. (I[59] = (T)(img)(x,_n4##y,z,c)), \
  1831. (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1832. (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1833. (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1834. (I[28] = (T)(img)(_n1##x,y,z,c)), \
  1835. (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1836. (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1837. (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1838. (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \
  1839. (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
  1840. (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1841. (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1842. (I[29] = (T)(img)(_n2##x,y,z,c)), \
  1843. (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1844. (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1845. (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1846. (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \
  1847. (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
  1848. (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1849. (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1850. (I[30] = (T)(img)(_n3##x,y,z,c)), \
  1851. (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1852. (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1853. (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
  1854. (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
  1855. x + 4>=(img).width()?(img).width() - 1:x + 4); \
  1856. x<=(int)(x1) && ((_n4##x<(img).width() && ( \
  1857. (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
  1858. (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
  1859. (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \
  1860. (I[31] = (T)(img)(_n4##x,y,z,c)), \
  1861. (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \
  1862. (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \
  1863. (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \
  1864. (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
  1865. _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \
  1866. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], \
  1867. I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], \
  1868. I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], \
  1869. I[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], \
  1870. I[32] = I[33], I[33] = I[34], I[34] = I[35], I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], \
  1871. I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], \
  1872. I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[53] = I[54], I[54] = I[55], \
  1873. I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[62] = I[63], \
  1874. _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
  1875. #define cimg_for9x9(img,x,y,z,c,I,T) \
  1876. cimg_for9((img)._height,y) for (int x = 0, \
  1877. _p4##x = 0, _p3##x = 0, _p2##x = 0, _p1##x = 0, \
  1878. _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
  1879. _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
  1880. _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
  1881. _n4##x = (int)( \
  1882. (I[0] = I[1] = I[2] = I[3] = I[4] = (T)(img)(_p4##x,_p4##y,z,c)), \
  1883. (I[9] = I[10] = I[11] = I[12] = I[13] = (T)(img)(0,_p3##y,z,c)), \
  1884. (I[18] = I[19] = I[20] = I[21] = I[22] = (T)(img)(0,_p2##y,z,c)), \
  1885. (I[27] = I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_p1##y,z,c)), \
  1886. (I[36] = I[37] = I[38] = I[39] = I[40] = (T)(img)(0,y,z,c)), \
  1887. (I[45] = I[46] = I[47] = I[48] = I[49] = (T)(img)(0,_n1##y,z,c)), \
  1888. (I[54] = I[55] = I[56] = I[57] = I[58] = (T)(img)(0,_n2##y,z,c)), \
  1889. (I[63] = I[64] = I[65] = I[66] = I[67] = (T)(img)(0,_n3##y,z,c)), \
  1890. (I[72] = I[73] = I[74] = I[75] = I[76] = (T)(img)(0,_n4##y,z,c)), \
  1891. (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \
  1892. (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1893. (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \
  1894. (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \
  1895. (I[41] = (T)(img)(_n1##x,y,z,c)), \
  1896. (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \
  1897. (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \
  1898. (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \
  1899. (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \
  1900. (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \
  1901. (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \
  1902. (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \
  1903. (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \
  1904. (I[42] = (T)(img)(_n2##x,y,z,c)), \
  1905. (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \
  1906. (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \
  1907. (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \
  1908. (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \
  1909. (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \
  1910. (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \
  1911. (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \
  1912. (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \
  1913. (I[43] = (T)(img)(_n3##x,y,z,c)), \
  1914. (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \
  1915. (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
  1916. (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
  1917. (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
  1918. 4>=((img)._width)?(img).width() - 1:4); \
  1919. (_n4##x<(img).width() && ( \
  1920. (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
  1921. (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
  1922. (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \
  1923. (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \
  1924. (I[44] = (T)(img)(_n4##x,y,z,c)), \
  1925. (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \
  1926. (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \
  1927. (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \
  1928. (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
  1929. _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \
  1930. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], I[7] = I[8], \
  1931. I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], \
  1932. I[16] = I[17], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
  1933. I[24] = I[25], I[25] = I[26], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], \
  1934. I[32] = I[33], I[33] = I[34], I[34] = I[35], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], \
  1935. I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
  1936. I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[54] = I[55], I[55] = I[56], \
  1937. I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[63] = I[64], \
  1938. I[64] = I[65], I[65] = I[66], I[66] = I[67], I[67] = I[68], I[68] = I[69], I[69] = I[70], I[70] = I[71], \
  1939. I[72] = I[73], I[73] = I[74], I[74] = I[75], I[75] = I[76], I[76] = I[77], I[77] = I[78], I[78] = I[79], \
  1940. I[79] = I[80], \
  1941. _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
  1942. #define cimg_for_in9x9(img,x0,y0,x1,y1,x,y,z,c,I,T) \
  1943. cimg_for_in9((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  1944. _p4##x = x - 4<0?0:x - 4, \
  1945. _p3##x = x - 3<0?0:x - 3, \
  1946. _p2##x = x - 2<0?0:x - 2, \
  1947. _p1##x = x - 1<0?0:x - 1, \
  1948. _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
  1949. _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
  1950. _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
  1951. _n4##x = (int)( \
  1952. (I[0] = (T)(img)(_p4##x,_p4##y,z,c)), \
  1953. (I[9] = (T)(img)(_p4##x,_p3##y,z,c)), \
  1954. (I[18] = (T)(img)(_p4##x,_p2##y,z,c)), \
  1955. (I[27] = (T)(img)(_p4##x,_p1##y,z,c)), \
  1956. (I[36] = (T)(img)(_p4##x,y,z,c)), \
  1957. (I[45] = (T)(img)(_p4##x,_n1##y,z,c)), \
  1958. (I[54] = (T)(img)(_p4##x,_n2##y,z,c)), \
  1959. (I[63] = (T)(img)(_p4##x,_n3##y,z,c)), \
  1960. (I[72] = (T)(img)(_p4##x,_n4##y,z,c)), \
  1961. (I[1] = (T)(img)(_p3##x,_p4##y,z,c)), \
  1962. (I[10] = (T)(img)(_p3##x,_p3##y,z,c)), \
  1963. (I[19] = (T)(img)(_p3##x,_p2##y,z,c)), \
  1964. (I[28] = (T)(img)(_p3##x,_p1##y,z,c)), \
  1965. (I[37] = (T)(img)(_p3##x,y,z,c)), \
  1966. (I[46] = (T)(img)(_p3##x,_n1##y,z,c)), \
  1967. (I[55] = (T)(img)(_p3##x,_n2##y,z,c)), \
  1968. (I[64] = (T)(img)(_p3##x,_n3##y,z,c)), \
  1969. (I[73] = (T)(img)(_p3##x,_n4##y,z,c)), \
  1970. (I[2] = (T)(img)(_p2##x,_p4##y,z,c)), \
  1971. (I[11] = (T)(img)(_p2##x,_p3##y,z,c)), \
  1972. (I[20] = (T)(img)(_p2##x,_p2##y,z,c)), \
  1973. (I[29] = (T)(img)(_p2##x,_p1##y,z,c)), \
  1974. (I[38] = (T)(img)(_p2##x,y,z,c)), \
  1975. (I[47] = (T)(img)(_p2##x,_n1##y,z,c)), \
  1976. (I[56] = (T)(img)(_p2##x,_n2##y,z,c)), \
  1977. (I[65] = (T)(img)(_p2##x,_n3##y,z,c)), \
  1978. (I[74] = (T)(img)(_p2##x,_n4##y,z,c)), \
  1979. (I[3] = (T)(img)(_p1##x,_p4##y,z,c)), \
  1980. (I[12] = (T)(img)(_p1##x,_p3##y,z,c)), \
  1981. (I[21] = (T)(img)(_p1##x,_p2##y,z,c)), \
  1982. (I[30] = (T)(img)(_p1##x,_p1##y,z,c)), \
  1983. (I[39] = (T)(img)(_p1##x,y,z,c)), \
  1984. (I[48] = (T)(img)(_p1##x,_n1##y,z,c)), \
  1985. (I[57] = (T)(img)(_p1##x,_n2##y,z,c)), \
  1986. (I[66] = (T)(img)(_p1##x,_n3##y,z,c)), \
  1987. (I[75] = (T)(img)(_p1##x,_n4##y,z,c)), \
  1988. (I[4] = (T)(img)(x,_p4##y,z,c)), \
  1989. (I[13] = (T)(img)(x,_p3##y,z,c)), \
  1990. (I[22] = (T)(img)(x,_p2##y,z,c)), \
  1991. (I[31] = (T)(img)(x,_p1##y,z,c)), \
  1992. (I[40] = (T)(img)(x,y,z,c)), \
  1993. (I[49] = (T)(img)(x,_n1##y,z,c)), \
  1994. (I[58] = (T)(img)(x,_n2##y,z,c)), \
  1995. (I[67] = (T)(img)(x,_n3##y,z,c)), \
  1996. (I[76] = (T)(img)(x,_n4##y,z,c)), \
  1997. (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \
  1998. (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \
  1999. (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \
  2000. (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \
  2001. (I[41] = (T)(img)(_n1##x,y,z,c)), \
  2002. (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \
  2003. (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \
  2004. (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \
  2005. (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \
  2006. (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \
  2007. (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \
  2008. (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \
  2009. (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \
  2010. (I[42] = (T)(img)(_n2##x,y,z,c)), \
  2011. (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \
  2012. (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \
  2013. (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \
  2014. (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \
  2015. (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \
  2016. (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \
  2017. (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \
  2018. (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \
  2019. (I[43] = (T)(img)(_n3##x,y,z,c)), \
  2020. (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \
  2021. (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
  2022. (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
  2023. (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
  2024. x + 4>=(img).width()?(img).width() - 1:x + 4); \
  2025. x<=(int)(x1) && ((_n4##x<(img).width() && ( \
  2026. (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
  2027. (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
  2028. (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \
  2029. (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \
  2030. (I[44] = (T)(img)(_n4##x,y,z,c)), \
  2031. (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \
  2032. (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \
  2033. (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \
  2034. (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
  2035. _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \
  2036. I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], I[5] = I[6], I[6] = I[7], I[7] = I[8], \
  2037. I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], \
  2038. I[16] = I[17], I[18] = I[19], I[19] = I[20], I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
  2039. I[24] = I[25], I[25] = I[26], I[27] = I[28], I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], \
  2040. I[32] = I[33], I[33] = I[34], I[34] = I[35], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], \
  2041. I[40] = I[41], I[41] = I[42], I[42] = I[43], I[43] = I[44], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
  2042. I[48] = I[49], I[49] = I[50], I[50] = I[51], I[51] = I[52], I[52] = I[53], I[54] = I[55], I[55] = I[56], \
  2043. I[56] = I[57], I[57] = I[58], I[58] = I[59], I[59] = I[60], I[60] = I[61], I[61] = I[62], I[63] = I[64], \
  2044. I[64] = I[65], I[65] = I[66], I[66] = I[67], I[67] = I[68], I[68] = I[69], I[69] = I[70], I[70] = I[71], \
  2045. I[72] = I[73], I[73] = I[74], I[74] = I[75], I[75] = I[76], I[76] = I[77], I[77] = I[78], I[78] = I[79], \
  2046. I[79] = I[80], \
  2047. _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
  2048. #define cimg_for2x2x2(img,x,y,z,c,I,T) \
  2049. cimg_for2((img)._depth,z) cimg_for2((img)._height,y) for (int x = 0, \
  2050. _n1##x = (int)( \
  2051. (I[0] = (T)(img)(0,y,z,c)), \
  2052. (I[2] = (T)(img)(0,_n1##y,z,c)), \
  2053. (I[4] = (T)(img)(0,y,_n1##z,c)), \
  2054. (I[6] = (T)(img)(0,_n1##y,_n1##z,c)), \
  2055. 1>=(img)._width?(img).width() - 1:1); \
  2056. (_n1##x<(img).width() && ( \
  2057. (I[1] = (T)(img)(_n1##x,y,z,c)), \
  2058. (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
  2059. (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \
  2060. (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
  2061. x==--_n1##x; \
  2062. I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \
  2063. ++x, ++_n1##x)
  2064. #define cimg_for_in2x2x2(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \
  2065. cimg_for_in2((img)._depth,z0,z1,z) cimg_for_in2((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  2066. _n1##x = (int)( \
  2067. (I[0] = (T)(img)(x,y,z,c)), \
  2068. (I[2] = (T)(img)(x,_n1##y,z,c)), \
  2069. (I[4] = (T)(img)(x,y,_n1##z,c)), \
  2070. (I[6] = (T)(img)(x,_n1##y,_n1##z,c)), \
  2071. x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
  2072. x<=(int)(x1) && ((_n1##x<(img).width() && ( \
  2073. (I[1] = (T)(img)(_n1##x,y,z,c)), \
  2074. (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
  2075. (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \
  2076. (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
  2077. x==--_n1##x); \
  2078. I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \
  2079. ++x, ++_n1##x)
  2080. #define cimg_for3x3x3(img,x,y,z,c,I,T) \
  2081. cimg_for3((img)._depth,z) cimg_for3((img)._height,y) for (int x = 0, \
  2082. _p1##x = 0, \
  2083. _n1##x = (int)( \
  2084. (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \
  2085. (I[3] = I[4] = (T)(img)(0,y,_p1##z,c)), \
  2086. (I[6] = I[7] = (T)(img)(0,_n1##y,_p1##z,c)), \
  2087. (I[9] = I[10] = (T)(img)(0,_p1##y,z,c)), \
  2088. (I[12] = I[13] = (T)(img)(0,y,z,c)), \
  2089. (I[15] = I[16] = (T)(img)(0,_n1##y,z,c)), \
  2090. (I[18] = I[19] = (T)(img)(0,_p1##y,_n1##z,c)), \
  2091. (I[21] = I[22] = (T)(img)(0,y,_n1##z,c)), \
  2092. (I[24] = I[25] = (T)(img)(0,_n1##y,_n1##z,c)), \
  2093. 1>=(img)._width?(img).width() - 1:1); \
  2094. (_n1##x<(img).width() && ( \
  2095. (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
  2096. (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
  2097. (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \
  2098. (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \
  2099. (I[14] = (T)(img)(_n1##x,y,z,c)), \
  2100. (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \
  2101. (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \
  2102. (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \
  2103. (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
  2104. x==--_n1##x; \
  2105. I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \
  2106. I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \
  2107. I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \
  2108. _p1##x = x++, ++_n1##x)
  2109. #define cimg_for_in3x3x3(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \
  2110. cimg_for_in3((img)._depth,z0,z1,z) cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
  2111. _p1##x = x - 1<0?0:x - 1, \
  2112. _n1##x = (int)( \
  2113. (I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \
  2114. (I[3] = (T)(img)(_p1##x,y,_p1##z,c)), \
  2115. (I[6] = (T)(img)(_p1##x,_n1##y,_p1##z,c)), \
  2116. (I[9] = (T)(img)(_p1##x,_p1##y,z,c)), \
  2117. (I[12] = (T)(img)(_p1##x,y,z,c)), \
  2118. (I[15] = (T)(img)(_p1##x,_n1##y,z,c)), \
  2119. (I[18] = (T)(img)(_p1##x,_p1##y,_n1##z,c)), \
  2120. (I[21] = (T)(img)(_p1##x,y,_n1##z,c)), \
  2121. (I[24] = (T)(img)(_p1##x,_n1##y,_n1##z,c)), \
  2122. (I[1] = (T)(img)(x,_p1##y,_p1##z,c)), \
  2123. (I[4] = (T)(img)(x,y,_p1##z,c)), \
  2124. (I[7] = (T)(img)(x,_n1##y,_p1##z,c)), \
  2125. (I[10] = (T)(img)(x,_p1##y,z,c)), \
  2126. (I[13] = (T)(img)(x,y,z,c)), \
  2127. (I[16] = (T)(img)(x,_n1##y,z,c)), \
  2128. (I[19] = (T)(img)(x,_p1##y,_n1##z,c)), \
  2129. (I[22] = (T)(img)(x,y,_n1##z,c)), \
  2130. (I[25] = (T)(img)(x,_n1##y,_n1##z,c)), \
  2131. x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
  2132. x<=(int)(x1) && ((_n1##x<(img).width() && ( \
  2133. (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
  2134. (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
  2135. (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \
  2136. (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \
  2137. (I[14] = (T)(img)(_n1##x,y,z,c)), \
  2138. (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \
  2139. (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \
  2140. (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \
  2141. (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
  2142. x==--_n1##x); \
  2143. I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \
  2144. I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \
  2145. I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \
  2146. _p1##x = x++, ++_n1##x)
  2147. #define cimglist_for(list,l) for (int l = 0; l<(int)(list)._width; ++l)
  2148. #define cimglist_rof(list,l) for (int l = (int)(list)._width - 1; l>=0; --l)
  2149. #define cimglist_for_in(list,l0,l1,l) \
  2150. for (int l = (int)(l0)<0?0:(int)(l0), _max##l = (unsigned int)l1<(list)._width?(int)(l1):(int)(list)._width - 1; \
  2151. l<=_max##l; ++l)
  2152. #define cimglist_apply(list,fn) cimglist_for(list,__##fn) (list)[__##fn].fn
  2153. // Macros used to display error messages when exceptions are thrown.
  2154. // You should not use these macros is your own code.
  2155. #define _cimgdisplay_instance "[instance(%u,%u,%u,%c%s%c)] CImgDisplay::"
  2156. #define cimgdisplay_instance _width,_height,_normalization,_title?'\"':'[',_title?_title:"untitled",_title?'\"':']'
  2157. #define _cimg_instance "[instance(%u,%u,%u,%u,%p,%sshared)] CImg<%s>::"
  2158. #define cimg_instance _width,_height,_depth,_spectrum,_data,_is_shared?"":"non-",pixel_type()
  2159. #define _cimglist_instance "[instance(%u,%u,%p)] CImgList<%s>::"
  2160. #define cimglist_instance _width,_allocated_width,_data,pixel_type()
  2161. /*------------------------------------------------
  2162. #
  2163. #
  2164. # Define cimg_library:: namespace
  2165. #
  2166. #
  2167. -------------------------------------------------*/
  2168. //! Contains <i>all classes and functions</i> of the \CImg library.
  2169. /**
  2170. This namespace is defined to avoid functions and class names collisions
  2171. that could happen with the inclusion of other C++ header files.
  2172. Anyway, it should not happen often and you should reasonably start most of your
  2173. \CImg-based programs with
  2174. \code
  2175. #include "CImg.h"
  2176. using namespace cimg_library;
  2177. \endcode
  2178. to simplify the declaration of \CImg Library objects afterwards.
  2179. **/
  2180. namespace cimg_library_suffixed {
  2181. // Declare the four classes of the CImg Library.
  2182. template<typename T=float> struct CImg;
  2183. template<typename T=float> struct CImgList;
  2184. struct CImgDisplay;
  2185. struct CImgException;
  2186. // Declare cimg:: namespace.
  2187. // This is an incomplete namespace definition here. It only contains some
  2188. // necessary stuff to ensure a correct declaration order of the classes and functions
  2189. // defined afterwards.
  2190. namespace cimg {
  2191. // Define character sequences for colored terminal output.
  2192. #ifdef cimg_use_vt100
  2193. static const char t_normal[] = { 0x1b, '[', '0', ';', '0', ';', '0', 'm', 0 };
  2194. static const char t_black[] = { 0x1b, '[', '0', ';', '3', '0', ';', '5', '9', 'm', 0 };
  2195. static const char t_red[] = { 0x1b, '[', '0', ';', '3', '1', ';', '5', '9', 'm', 0 };
  2196. static const char t_green[] = { 0x1b, '[', '0', ';', '3', '2', ';', '5', '9', 'm', 0 };
  2197. static const char t_yellow[] = { 0x1b, '[', '0', ';', '3', '3', ';', '5', '9', 'm', 0 };
  2198. static const char t_blue[] = { 0x1b, '[', '0', ';', '3', '4', ';', '5', '9', 'm', 0 };
  2199. static const char t_magenta[] = { 0x1b, '[', '0', ';', '3', '5', ';', '5', '9', 'm', 0 };
  2200. static const char t_cyan[] = { 0x1b, '[', '0', ';', '3', '6', ';', '5', '9', 'm', 0 };
  2201. static const char t_white[] = { 0x1b, '[', '0', ';', '3', '7', ';', '5', '9', 'm', 0 };
  2202. static const char t_bold[] = { 0x1b, '[', '1', 'm', 0 };
  2203. static const char t_underscore[] = { 0x1b, '[', '4', 'm', 0 };
  2204. #else
  2205. static const char t_normal[] = { 0 };
  2206. static const char *const t_black = cimg::t_normal,
  2207. *const t_red = cimg::t_normal,
  2208. *const t_green = cimg::t_normal,
  2209. *const t_yellow = cimg::t_normal,
  2210. *const t_blue = cimg::t_normal,
  2211. *const t_magenta = cimg::t_normal,
  2212. *const t_cyan = cimg::t_normal,
  2213. *const t_white = cimg::t_normal,
  2214. *const t_bold = cimg::t_normal,
  2215. *const t_underscore = cimg::t_normal;
  2216. #endif
  2217. inline std::FILE* output(std::FILE *file=0);
  2218. inline void info();
  2219. //! Avoid warning messages due to unused parameters. Do nothing actually.
  2220. template<typename T>
  2221. inline void unused(const T&, ...) {}
  2222. // [internal] Lock/unlock a mutex for managing concurrent threads.
  2223. // 'lock_mode' can be { 0=unlock | 1=lock | 2=trylock }.
  2224. // 'n' can be in [0,31] but mutex range [0,15] is reserved by CImg.
  2225. inline int mutex(const unsigned int n, const int lock_mode=1);
  2226. inline unsigned int& exception_mode(const unsigned int value, const bool is_set) {
  2227. static unsigned int mode = cimg_verbosity;
  2228. if (is_set) { cimg::mutex(0); mode = value<4?value:4; cimg::mutex(0,0); }
  2229. return mode;
  2230. }
  2231. // Functions to return standard streams 'stdin', 'stdout' and 'stderr'.
  2232. inline FILE* _stdin(const bool throw_exception=true);
  2233. inline FILE* _stdout(const bool throw_exception=true);
  2234. inline FILE* _stderr(const bool throw_exception=true);
  2235. // Mandatory because Microsoft's _snprintf() and _vsnprintf() do not add the '\0' character
  2236. // at the end of the string.
  2237. #if cimg_OS==2 && defined(_MSC_VER)
  2238. inline int _snprintf(char *const s, const size_t size, const char *const format, ...) {
  2239. va_list ap;
  2240. va_start(ap,format);
  2241. const int result = _vsnprintf(s,size,format,ap);
  2242. va_end(ap);
  2243. return result;
  2244. }
  2245. inline int _vsnprintf(char *const s, const size_t size, const char *const format, va_list ap) {
  2246. int result = -1;
  2247. cimg::mutex(6);
  2248. if (size) result = _vsnprintf_s(s,size,_TRUNCATE,format,ap);
  2249. if (result==-1) result = _vscprintf(format,ap);
  2250. cimg::mutex(6,0);
  2251. return result;
  2252. }
  2253. // Mutex-protected version of sscanf, sprintf and snprintf.
  2254. // Used only MacOSX, as it seems those functions are not re-entrant on MacOSX.
  2255. #elif defined(__MACOSX__) || defined(__APPLE__)
  2256. inline int _sscanf(const char *const s, const char *const format, ...) {
  2257. cimg::mutex(6);
  2258. va_list args;
  2259. va_start(args,format);
  2260. const int result = std::vsscanf(s,format,args);
  2261. va_end(args);
  2262. cimg::mutex(6,0);
  2263. return result;
  2264. }
  2265. inline int _sprintf(char *const s, const char *const format, ...) {
  2266. cimg::mutex(6);
  2267. va_list args;
  2268. va_start(args,format);
  2269. const int result = std::vsprintf(s,format,args);
  2270. va_end(args);
  2271. cimg::mutex(6,0);
  2272. return result;
  2273. }
  2274. inline int _snprintf(char *const s, const size_t n, const char *const format, ...) {
  2275. cimg::mutex(6);
  2276. va_list args;
  2277. va_start(args,format);
  2278. const int result = std::vsnprintf(s,n,format,args);
  2279. va_end(args);
  2280. cimg::mutex(6,0);
  2281. return result;
  2282. }
  2283. inline int _vsnprintf(char *const s, const size_t size, const char* format, va_list ap) {
  2284. cimg::mutex(6);
  2285. const int result = std::vsnprintf(s,size,format,ap);
  2286. cimg::mutex(6,0);
  2287. return result;
  2288. }
  2289. #endif
  2290. //! Set current \CImg exception mode.
  2291. /**
  2292. The way error messages are handled by \CImg can be changed dynamically, using this function.
  2293. \param mode Desired exception mode. Possible values are:
  2294. - \c 0: Hide library messages (quiet mode).
  2295. - \c 1: Print library messages on the console.
  2296. - \c 2: Display library messages on a dialog window.
  2297. - \c 3: Do as \c 1 + add extra debug warnings (slow down the code!).
  2298. - \c 4: Do as \c 2 + add extra debug warnings (slow down the code!).
  2299. **/
  2300. inline unsigned int& exception_mode(const unsigned int mode) {
  2301. return exception_mode(mode,true);
  2302. }
  2303. //! Return current \CImg exception mode.
  2304. /**
  2305. \note By default, return the value of configuration macro \c cimg_verbosity
  2306. **/
  2307. inline unsigned int& exception_mode() {
  2308. return exception_mode(0,false);
  2309. }
  2310. inline unsigned int openmp_mode(const unsigned int value, const bool is_set) {
  2311. static unsigned int mode = 2;
  2312. if (is_set) { cimg::mutex(0); mode = value<2?value:2; cimg::mutex(0,0); }
  2313. return mode;
  2314. }
  2315. //! Set current \CImg openmp mode.
  2316. /**
  2317. The way openmp-based methods are handled by \CImg can be changed dynamically, using this function.
  2318. \param mode Desired openmp mode. Possible values are:
  2319. - \c 0: Never parallelize.
  2320. - \c 1: Always parallelize.
  2321. - \c 2: Adaptive parallelization mode (default behavior).
  2322. **/
  2323. inline unsigned int openmp_mode(const unsigned int mode) {
  2324. return openmp_mode(mode,true);
  2325. }
  2326. //! Return current \CImg openmp mode.
  2327. inline unsigned int openmp_mode() {
  2328. return openmp_mode(0,false);
  2329. }
  2330. #ifndef cimg_openmp_sizefactor
  2331. #define cimg_openmp_sizefactor 1
  2332. #endif
  2333. #define cimg_openmp_if(cond) if ((cimg::openmp_mode()==1 || (cimg::openmp_mode()>1 && (cond))))
  2334. #define cimg_openmp_if_size(size,min_size) cimg_openmp_if((size)>=(cimg_openmp_sizefactor)*(min_size))
  2335. #ifdef _MSC_VER
  2336. // Disable 'collapse()' directive for MSVC (supports only OpenMP 2.0).
  2337. #define cimg_openmp_collapse(k)
  2338. #else
  2339. #define cimg_openmp_collapse(k) collapse(k)
  2340. #endif
  2341. #if cimg_OS==2
  2342. // Disable parallelization of simple loops on Windows, due to noticed performance drop.
  2343. #define cimg_openmp_for(instance,expr,min_size) cimg_rof((instance),ptr,T) *ptr = (T)(expr);
  2344. #else
  2345. #define cimg_openmp_for(instance,expr,min_size) \
  2346. cimg_pragma_openmp(parallel for cimg_openmp_if_size((instance).size(),min_size)) \
  2347. cimg_rof((instance),ptr,T) *ptr = (T)(expr);
  2348. #endif
  2349. // Display a simple dialog box, and wait for the user's response.
  2350. inline int dialog(const char *const title, const char *const msg,
  2351. const char *const button1_label="OK", const char *const button2_label=0,
  2352. const char *const button3_label=0, const char *const button4_label=0,
  2353. const char *const button5_label=0, const char *const button6_label=0,
  2354. const bool centering=false);
  2355. // Evaluate math expression.
  2356. inline double eval(const char *const expression,
  2357. const double x=0, const double y=0, const double z=0, const double c=0);
  2358. } // namespace cimg { ...
  2359. /*---------------------------------------
  2360. #
  2361. # Define the CImgException structures
  2362. #
  2363. --------------------------------------*/
  2364. //! Instances of \c CImgException are thrown when errors are encountered in a \CImg function call.
  2365. /**
  2366. \par Overview
  2367. CImgException is the base class of all exceptions thrown by \CImg (except \b CImgAbortException).
  2368. CImgException is never thrown itself. Derived classes that specify the type of errord are thrown instead.
  2369. These classes can be:
  2370. - \b CImgAbortException: Thrown when a computationally-intensive function is aborted by an external signal.
  2371. This is the only \c non-derived exception class.
  2372. - \b CImgArgumentException: Thrown when one argument of a called \CImg function is invalid.
  2373. This is probably one of the most thrown exception by \CImg.
  2374. For instance, the following example throws a \c CImgArgumentException:
  2375. \code
  2376. CImg<float> img(100,100,1,3); // Define a 100x100 color image with float-valued pixels
  2377. img.mirror('e'); // Try to mirror image along the (non-existing) 'e'-axis
  2378. \endcode
  2379. - \b CImgDisplayException: Thrown when something went wrong during the display of images in CImgDisplay instances.
  2380. - \b CImgInstanceException: Thrown when an instance associated to a called \CImg method does not fit
  2381. the function requirements. For instance, the following example throws a \c CImgInstanceException:
  2382. \code
  2383. const CImg<float> img; // Define an empty image
  2384. const float value = img.at(0); // Try to read first pixel value (does not exist)
  2385. \endcode
  2386. - \b CImgIOException: Thrown when an error occurred when trying to load or save image files.
  2387. This happens when trying to read files that do not exist or with invalid formats.
  2388. For instance, the following example throws a \c CImgIOException:
  2389. \code
  2390. const CImg<float> img("missing_file.jpg"); // Try to load a file that does not exist
  2391. \endcode
  2392. - \b CImgWarningException: Thrown only if configuration macro \c cimg_strict_warnings is set, and
  2393. when a \CImg function has to display a warning message (see cimg::warn()).
  2394. It is not recommended to throw CImgException instances by yourself,
  2395. since they are expected to be thrown only by \CImg.
  2396. When an error occurs in a library function call, \CImg may display error messages on the screen or on the
  2397. standard output, depending on the current \CImg exception mode.
  2398. The \CImg exception mode can be get and set by functions cimg::exception_mode() and
  2399. cimg::exception_mode(unsigned int).
  2400. \par Exceptions handling
  2401. In all cases, when an error occurs in \CImg, an instance of the corresponding exception class is thrown.
  2402. This may lead the program to break (this is the default behavior), but you can bypass this behavior by
  2403. handling the exceptions by yourself,
  2404. using a usual <tt>try { ... } catch () { ... }</tt> bloc, as in the following example:
  2405. \code
  2406. #define "CImg.h"
  2407. using namespace cimg_library;
  2408. int main() {
  2409. cimg::exception_mode(0); // Enable quiet exception mode
  2410. try {
  2411. ... // Here, do what you want to stress CImg
  2412. } catch (CImgException& e) { // You succeeded: something went wrong!
  2413. std::fprintf(stderr,"CImg Library Error: %s",e.what()); // Display your custom error message
  2414. ... // Do what you want now to save the ship!
  2415. }
  2416. }
  2417. \endcode
  2418. **/
  2419. struct CImgException : public std::exception {
  2420. #define _cimg_exception_err(etype,disp_flag) \
  2421. std::va_list ap, ap2; \
  2422. va_start(ap,format); va_start(ap2,format); \
  2423. int size = cimg_vsnprintf(0,0,format,ap2); \
  2424. if (size++>=0) { \
  2425. delete[] _message; \
  2426. _message = new char[(size_t)size]; \
  2427. cimg_vsnprintf(_message,(size_t)size,format,ap); \
  2428. if (cimg::exception_mode()) { \
  2429. std::fprintf(cimg::output(),"\n%s[CImg] *** %s ***%s %s\n",cimg::t_red,etype,cimg::t_normal,_message); \
  2430. if (cimg_display && disp_flag && !(cimg::exception_mode()%2)) try { cimg::dialog(etype,_message,"Abort"); } \
  2431. catch (CImgException&) {} \
  2432. if (cimg::exception_mode()>=3) cimg_library_suffixed::cimg::info(); \
  2433. } \
  2434. } \
  2435. va_end(ap); va_end(ap2);
  2436. char *_message;
  2437. CImgException() { _message = new char[1]; *_message = 0; }
  2438. CImgException(const char *const format, ...):_message(0) { _cimg_exception_err("CImgException",true); }
  2439. CImgException(const CImgException& e):std::exception(e) {
  2440. const size_t size = std::strlen(e._message);
  2441. _message = new char[size + 1];
  2442. std::strncpy(_message,e._message,size);
  2443. _message[size] = 0;
  2444. }
  2445. ~CImgException() throw() { delete[] _message; }
  2446. CImgException& operator=(const CImgException& e) {
  2447. const size_t size = std::strlen(e._message);
  2448. _message = new char[size + 1];
  2449. std::strncpy(_message,e._message,size);
  2450. _message[size] = 0;
  2451. return *this;
  2452. }
  2453. //! Return a C-string containing the error message associated to the thrown exception.
  2454. const char *what() const throw() { return _message; }
  2455. }; // struct CImgException { ...
  2456. // The CImgAbortException class is used to throw an exception when
  2457. // a computationally-intensive function has been aborted by an external signal.
  2458. struct CImgAbortException : public std::exception {
  2459. char *_message;
  2460. CImgAbortException() { _message = new char[1]; *_message = 0; }
  2461. CImgAbortException(const char *const format, ...):_message(0) { _cimg_exception_err("CImgAbortException",true); }
  2462. CImgAbortException(const CImgAbortException& e):std::exception(e) {
  2463. const size_t size = std::strlen(e._message);
  2464. _message = new char[size + 1];
  2465. std::strncpy(_message,e._message,size);
  2466. _message[size] = 0;
  2467. }
  2468. ~CImgAbortException() throw() { delete[] _message; }
  2469. CImgAbortException& operator=(const CImgAbortException& e) {
  2470. const size_t size = std::strlen(e._message);
  2471. _message = new char[size + 1];
  2472. std::strncpy(_message,e._message,size);
  2473. _message[size] = 0;
  2474. return *this;
  2475. }
  2476. //! Return a C-string containing the error message associated to the thrown exception.
  2477. const char *what() const throw() { return _message; }
  2478. }; // struct CImgAbortException { ...
  2479. // The CImgArgumentException class is used to throw an exception related
  2480. // to invalid arguments encountered in a library function call.
  2481. struct CImgArgumentException : public CImgException {
  2482. CImgArgumentException(const char *const format, ...) { _cimg_exception_err("CImgArgumentException",true); }
  2483. }; // struct CImgArgumentException { ...
  2484. // The CImgDisplayException class is used to throw an exception related
  2485. // to display problems encountered in a library function call.
  2486. struct CImgDisplayException : public CImgException {
  2487. CImgDisplayException(const char *const format, ...) { _cimg_exception_err("CImgDisplayException",false); }
  2488. }; // struct CImgDisplayException { ...
  2489. // The CImgInstanceException class is used to throw an exception related
  2490. // to an invalid instance encountered in a library function call.
  2491. struct CImgInstanceException : public CImgException {
  2492. CImgInstanceException(const char *const format, ...) { _cimg_exception_err("CImgInstanceException",true); }
  2493. }; // struct CImgInstanceException { ...
  2494. // The CImgIOException class is used to throw an exception related
  2495. // to input/output file problems encountered in a library function call.
  2496. struct CImgIOException : public CImgException {
  2497. CImgIOException(const char *const format, ...) { _cimg_exception_err("CImgIOException",true); }
  2498. }; // struct CImgIOException { ...
  2499. // The CImgWarningException class is used to throw an exception for warnings
  2500. // encountered in a library function call.
  2501. struct CImgWarningException : public CImgException {
  2502. CImgWarningException(const char *const format, ...) { _cimg_exception_err("CImgWarningException",false); }
  2503. }; // struct CImgWarningException { ...
  2504. /*-------------------------------------
  2505. #
  2506. # Define cimg:: namespace
  2507. #
  2508. -----------------------------------*/
  2509. //! Contains \a low-level functions and variables of the \CImg Library.
  2510. /**
  2511. Most of the functions and variables within this namespace are used by the \CImg library for low-level operations.
  2512. You may use them to access specific const values or environment variables internally used by \CImg.
  2513. \warning Never write <tt>using namespace cimg_library::cimg;</tt> in your source code. Lot of functions in the
  2514. <tt>cimg:: namespace</tt> have the same names as standard C functions that may be defined in the global
  2515. namespace <tt>::</tt>.
  2516. **/
  2517. namespace cimg {
  2518. // Define traits that will be used to determine the best data type to work in CImg functions.
  2519. //
  2520. template<typename T> struct type {
  2521. static const char* string() {
  2522. static const char* s[] = { "unknown", "unknown8", "unknown16", "unknown24",
  2523. "unknown32", "unknown40", "unknown48", "unknown56",
  2524. "unknown64", "unknown72", "unknown80", "unknown88",
  2525. "unknown96", "unknown104", "unknown112", "unknown120",
  2526. "unknown128" };
  2527. return s[(sizeof(T)<17)?sizeof(T):0];
  2528. }
  2529. static bool is_float() { return false; }
  2530. static bool is_inf(const T) { return false; }
  2531. static bool is_nan(const T) { return false; }
  2532. static bool is_finite(const T) { return true; }
  2533. static T min() { return ~max(); }
  2534. static T max() { return (T)1<<(8*sizeof(T) - 1); }
  2535. static T inf() { return max(); }
  2536. static T cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(T)val; }
  2537. static const char* format() { return "%s"; }
  2538. static const char* format_s() { return "%s"; }
  2539. static const char* format(const T& val) { static const char *const s = "unknown"; cimg::unused(val); return s; }
  2540. };
  2541. template<> struct type<bool> {
  2542. static const char* string() { static const char *const s = "bool"; return s; }
  2543. static bool is_float() { return false; }
  2544. static bool is_inf(const bool) { return false; }
  2545. static bool is_nan(const bool) { return false; }
  2546. static bool is_finite(const bool) { return true; }
  2547. static bool min() { return false; }
  2548. static bool max() { return true; }
  2549. static bool inf() { return max(); }
  2550. static bool is_inf() { return false; }
  2551. static bool cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(bool)val; }
  2552. static const char* format() { return "%s"; }
  2553. static const char* format_s() { return "%s"; }
  2554. static const char* format(const bool val) { static const char* s[] = { "false", "true" }; return s[val?1:0]; }
  2555. };
  2556. template<> struct type<unsigned char> {
  2557. static const char* string() { static const char *const s = "unsigned char"; return s; }
  2558. static bool is_float() { return false; }
  2559. static bool is_inf(const unsigned char) { return false; }
  2560. static bool is_nan(const unsigned char) { return false; }
  2561. static bool is_finite(const unsigned char) { return true; }
  2562. static unsigned char min() { return 0; }
  2563. static unsigned char max() { return (unsigned char)-1; }
  2564. static unsigned char inf() { return max(); }
  2565. static unsigned char cut(const double val) {
  2566. return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
  2567. static const char* format() { return "%u"; }
  2568. static const char* format_s() { return "%u"; }
  2569. static unsigned int format(const unsigned char val) { return (unsigned int)val; }
  2570. };
  2571. #if defined(CHAR_MAX) && CHAR_MAX==255
  2572. template<> struct type<char> {
  2573. static const char* string() { static const char *const s = "char"; return s; }
  2574. static bool is_float() { return false; }
  2575. static bool is_inf(const char) { return false; }
  2576. static bool is_nan(const char) { return false; }
  2577. static bool is_finite(const char) { return true; }
  2578. static char min() { return 0; }
  2579. static char max() { return (char)-1; }
  2580. static char inf() { return max(); }
  2581. static char cut(const double val) {
  2582. return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
  2583. static const char* format() { return "%u"; }
  2584. static const char* format_s() { return "%u"; }
  2585. static unsigned int format(const char val) { return (unsigned int)val; }
  2586. };
  2587. #else
  2588. template<> struct type<char> {
  2589. static const char* string() { static const char *const s = "char"; return s; }
  2590. static bool is_float() { return false; }
  2591. static bool is_inf(const char) { return false; }
  2592. static bool is_nan(const char) { return false; }
  2593. static bool is_finite(const char) { return true; }
  2594. static char min() { return ~max(); }
  2595. static char max() { return (char)((unsigned char)-1>>1); }
  2596. static char inf() { return max(); }
  2597. static char cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(char)val; }
  2598. static const char* format() { return "%d"; }
  2599. static const char* format_s() { return "%d"; }
  2600. static int format(const char val) { return (int)val; }
  2601. };
  2602. #endif
  2603. template<> struct type<signed char> {
  2604. static const char* string() { static const char *const s = "signed char"; return s; }
  2605. static bool is_float() { return false; }
  2606. static bool is_inf(const signed char) { return false; }
  2607. static bool is_nan(const signed char) { return false; }
  2608. static bool is_finite(const signed char) { return true; }
  2609. static signed char min() { return ~max(); }
  2610. static signed char max() { return (signed char)((unsigned char)-1>>1); }
  2611. static signed char inf() { return max(); }
  2612. static signed char cut(const double val) {
  2613. return val<(double)min()?min():val>(double)max()?max():(signed char)val; }
  2614. static const char* format() { return "%d"; }
  2615. static const char* format_s() { return "%d"; }
  2616. static int format(const signed char val) { return (int)val; }
  2617. };
  2618. template<> struct type<unsigned short> {
  2619. static const char* string() { static const char *const s = "unsigned short"; return s; }
  2620. static bool is_float() { return false; }
  2621. static bool is_inf(const unsigned short) { return false; }
  2622. static bool is_nan(const unsigned short) { return false; }
  2623. static bool is_finite(const unsigned short) { return true; }
  2624. static unsigned short min() { return 0; }
  2625. static unsigned short max() { return (unsigned short)-1; }
  2626. static unsigned short inf() { return max(); }
  2627. static unsigned short cut(const double val) {
  2628. return val<(double)min()?min():val>(double)max()?max():(unsigned short)val; }
  2629. static const char* format() { return "%u"; }
  2630. static const char* format_s() { return "%u"; }
  2631. static unsigned int format(const unsigned short val) { return (unsigned int)val; }
  2632. };
  2633. template<> struct type<short> {
  2634. static const char* string() { static const char *const s = "short"; return s; }
  2635. static bool is_float() { return false; }
  2636. static bool is_inf(const short) { return false; }
  2637. static bool is_nan(const short) { return false; }
  2638. static bool is_finite(const short) { return true; }
  2639. static short min() { return ~max(); }
  2640. static short max() { return (short)((unsigned short)-1>>1); }
  2641. static short inf() { return max(); }
  2642. static short cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(short)val; }
  2643. static const char* format() { return "%d"; }
  2644. static const char* format_s() { return "%d"; }
  2645. static int format(const short val) { return (int)val; }
  2646. };
  2647. template<> struct type<unsigned int> {
  2648. static const char* string() { static const char *const s = "unsigned int"; return s; }
  2649. static bool is_float() { return false; }
  2650. static bool is_inf(const unsigned int) { return false; }
  2651. static bool is_nan(const unsigned int) { return false; }
  2652. static bool is_finite(const unsigned int) { return true; }
  2653. static unsigned int min() { return 0; }
  2654. static unsigned int max() { return (unsigned int)-1; }
  2655. static unsigned int inf() { return max(); }
  2656. static unsigned int cut(const double val) {
  2657. return val<(double)min()?min():val>(double)max()?max():(unsigned int)val; }
  2658. static const char* format() { return "%u"; }
  2659. static const char* format_s() { return "%u"; }
  2660. static unsigned int format(const unsigned int val) { return val; }
  2661. };
  2662. template<> struct type<int> {
  2663. static const char* string() { static const char *const s = "int"; return s; }
  2664. static bool is_float() { return false; }
  2665. static bool is_inf(const int) { return false; }
  2666. static bool is_nan(const int) { return false; }
  2667. static bool is_finite(const int) { return true; }
  2668. static int min() { return ~max(); }
  2669. static int max() { return (int)(~0U>>1); }
  2670. static int inf() { return max(); }
  2671. static int cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(int)val; }
  2672. static const char* format() { return "%d"; }
  2673. static const char* format_s() { return "%d"; }
  2674. static int format(const int val) { return val; }
  2675. };
  2676. template<> struct type<cimg_uint64> {
  2677. static const char* string() { static const char *const s = "unsigned int64"; return s; }
  2678. static bool is_float() { return false; }
  2679. static bool is_inf(const cimg_uint64) { return false; }
  2680. static bool is_nan(const cimg_uint64) { return false; }
  2681. static bool is_finite(const cimg_uint64) { return true; }
  2682. static cimg_uint64 min() { return 0; }
  2683. static cimg_uint64 max() { return (cimg_uint64)-1; }
  2684. static cimg_uint64 inf() { return max(); }
  2685. static cimg_uint64 cut(const double val) {
  2686. return val<(double)min()?min():val>(double)max()?max():(cimg_uint64)val; }
  2687. static const char* format() { return cimg_fuint64; }
  2688. static const char* format_s() { return cimg_fuint64; }
  2689. static cimg_uint64 format(const cimg_uint64 val) { return val; }
  2690. };
  2691. template<> struct type<cimg_int64> {
  2692. static const char* string() { static const char *const s = "int64"; return s; }
  2693. static bool is_float() { return false; }
  2694. static bool is_inf(const cimg_int64) { return false; }
  2695. static bool is_nan(const cimg_int64) { return false; }
  2696. static bool is_finite(const cimg_int64) { return true; }
  2697. static cimg_int64 min() { return ~max(); }
  2698. static cimg_int64 max() { return (cimg_int64)((cimg_uint64)-1>>1); }
  2699. static cimg_int64 inf() { return max(); }
  2700. static cimg_int64 cut(const double val) {
  2701. return val<(double)min()?min():val>(double)max()?max():(cimg_int64)val;
  2702. }
  2703. static const char* format() { return cimg_fint64; }
  2704. static const char* format_s() { return cimg_fint64; }
  2705. static long format(const long val) { return (long)val; }
  2706. };
  2707. template<> struct type<double> {
  2708. static const char* string() { static const char *const s = "double"; return s; }
  2709. static bool is_float() { return true; }
  2710. static bool is_inf(const double val) {
  2711. #ifdef isinf
  2712. return (bool)isinf(val);
  2713. #else
  2714. return !is_nan(val) && (val<cimg::type<double>::min() || val>cimg::type<double>::max());
  2715. #endif
  2716. }
  2717. static bool is_nan(const double val) { // Custom version that works with '-ffast-math'
  2718. if (sizeof(double)==8) {
  2719. cimg_uint64 u;
  2720. std::memcpy(&u,&val,sizeof(double));
  2721. return ((unsigned int)(u>>32)&0x7fffffff) + ((unsigned int)u!=0)>0x7ff00000;
  2722. }
  2723. #ifdef isnan
  2724. return (bool)isnan(val);
  2725. #else
  2726. return !(val==val);
  2727. #endif
  2728. }
  2729. static bool is_finite(const double val) {
  2730. #ifdef isfinite
  2731. return (bool)isfinite(val);
  2732. #else
  2733. return !is_nan(val) && !is_inf(val);
  2734. #endif
  2735. }
  2736. static double min() { return -DBL_MAX; }
  2737. static double max() { return DBL_MAX; }
  2738. static double inf() {
  2739. #ifdef INFINITY
  2740. return (double)INFINITY;
  2741. #else
  2742. return max()*max();
  2743. #endif
  2744. }
  2745. static double nan() {
  2746. #ifdef NAN
  2747. return (double)NAN;
  2748. #else
  2749. const double val_nan = -std::sqrt(-1.); return val_nan;
  2750. #endif
  2751. }
  2752. static double cut(const double val) { return val; }
  2753. static const char* format() { return "%.17g"; }
  2754. static const char* format_s() { return "%g"; }
  2755. static double format(const double val) { return val; }
  2756. };
  2757. template<> struct type<float> {
  2758. static const char* string() { static const char *const s = "float"; return s; }
  2759. static bool is_float() { return true; }
  2760. static bool is_inf(const float val) {
  2761. #ifdef isinf
  2762. return (bool)isinf(val);
  2763. #else
  2764. return !is_nan(val) && (val<cimg::type<float>::min() || val>cimg::type<float>::max());
  2765. #endif
  2766. }
  2767. static bool is_nan(const float val) { // Custom version that works with '-ffast-math'
  2768. if (sizeof(float)==4) {
  2769. unsigned int u;
  2770. std::memcpy(&u,&val,sizeof(float));
  2771. return (u&0x7fffffff)>0x7f800000;
  2772. }
  2773. #ifdef isnan
  2774. return (bool)isnan(val);
  2775. #else
  2776. return !(val==val);
  2777. #endif
  2778. }
  2779. static bool is_finite(const float val) {
  2780. #ifdef isfinite
  2781. return (bool)isfinite(val);
  2782. #else
  2783. return !is_nan(val) && !is_inf(val);
  2784. #endif
  2785. }
  2786. static float min() { return -FLT_MAX; }
  2787. static float max() { return FLT_MAX; }
  2788. static float inf() { return (float)cimg::type<double>::inf(); }
  2789. static float nan() { return (float)cimg::type<double>::nan(); }
  2790. static float cut(const double val) { return (float)val; }
  2791. static float cut(const float val) { return (float)val; }
  2792. static const char* format() { return "%.9g"; }
  2793. static const char* format_s() { return "%g"; }
  2794. static double format(const float val) { return (double)val; }
  2795. };
  2796. template<> struct type<long double> {
  2797. static const char* string() { static const char *const s = "long double"; return s; }
  2798. static bool is_float() { return true; }
  2799. static bool is_inf(const long double val) {
  2800. #ifdef isinf
  2801. return (bool)isinf(val);
  2802. #else
  2803. return !is_nan(val) && (val<cimg::type<long double>::min() || val>cimg::type<long double>::max());
  2804. #endif
  2805. }
  2806. static bool is_nan(const long double val) {
  2807. #ifdef isnan
  2808. return (bool)isnan(val);
  2809. #else
  2810. return !(val==val);
  2811. #endif
  2812. }
  2813. static bool is_finite(const long double val) {
  2814. #ifdef isfinite
  2815. return (bool)isfinite(val);
  2816. #else
  2817. return !is_nan(val) && !is_inf(val);
  2818. #endif
  2819. }
  2820. static long double min() { return -LDBL_MAX; }
  2821. static long double max() { return LDBL_MAX; }
  2822. static long double inf() { return max()*max(); }
  2823. static long double nan() { const long double val_nan = -std::sqrt(-1.L); return val_nan; }
  2824. static long double cut(const long double val) { return val; }
  2825. static const char* format() { return "%.17g"; }
  2826. static const char* format_s() { return "%g"; }
  2827. static double format(const long double val) { return (double)val; }
  2828. };
  2829. #ifdef cimg_use_half
  2830. template<> struct type<half> {
  2831. static const char* string() { static const char *const s = "half"; return s; }
  2832. static bool is_float() { return true; }
  2833. static bool is_inf(const long double val) {
  2834. #ifdef isinf
  2835. return (bool)isinf(val);
  2836. #else
  2837. return !is_nan(val) && (val<cimg::type<half>::min() || val>cimg::type<half>::max());
  2838. #endif
  2839. }
  2840. static bool is_nan(const half val) { // Custom version that works with '-ffast-math'
  2841. if (sizeof(half)==2) {
  2842. short u;
  2843. std::memcpy(&u,&val,sizeof(short));
  2844. return (bool)((u&0x7fff)>0x7c00);
  2845. }
  2846. return cimg::type<float>::is_nan((float)val);
  2847. }
  2848. static bool is_finite(const half val) {
  2849. #ifdef isfinite
  2850. return (bool)isfinite(val);
  2851. #else
  2852. return !is_nan(val) && !is_inf(val);
  2853. #endif
  2854. }
  2855. static half min() { return (half)-65504; }
  2856. static half max() { return (half)65504; }
  2857. static half inf() { return max()*max(); }
  2858. static half nan() { const half val_nan = (half)-std::sqrt(-1.); return val_nan; }
  2859. static half cut(const double val) { return (half)val; }
  2860. static const char* format() { return "%.9g"; }
  2861. static const char* format_s() { return "%g"; }
  2862. static double format(const half val) { return (double)val; }
  2863. };
  2864. #endif
  2865. template<typename T, typename t> struct superset { typedef T type; };
  2866. template<> struct superset<bool,unsigned char> { typedef unsigned char type; };
  2867. template<> struct superset<bool,char> { typedef char type; };
  2868. template<> struct superset<bool,signed char> { typedef signed char type; };
  2869. template<> struct superset<bool,unsigned short> { typedef unsigned short type; };
  2870. template<> struct superset<bool,short> { typedef short type; };
  2871. template<> struct superset<bool,unsigned int> { typedef unsigned int type; };
  2872. template<> struct superset<bool,int> { typedef int type; };
  2873. template<> struct superset<bool,cimg_uint64> { typedef cimg_uint64 type; };
  2874. template<> struct superset<bool,cimg_int64> { typedef cimg_int64 type; };
  2875. template<> struct superset<bool,float> { typedef float type; };
  2876. template<> struct superset<bool,double> { typedef double type; };
  2877. template<> struct superset<unsigned char,char> { typedef short type; };
  2878. template<> struct superset<unsigned char,signed char> { typedef short type; };
  2879. template<> struct superset<unsigned char,unsigned short> { typedef unsigned short type; };
  2880. template<> struct superset<unsigned char,short> { typedef short type; };
  2881. template<> struct superset<unsigned char,unsigned int> { typedef unsigned int type; };
  2882. template<> struct superset<unsigned char,int> { typedef int type; };
  2883. template<> struct superset<unsigned char,cimg_uint64> { typedef cimg_uint64 type; };
  2884. template<> struct superset<unsigned char,cimg_int64> { typedef cimg_int64 type; };
  2885. template<> struct superset<unsigned char,float> { typedef float type; };
  2886. template<> struct superset<unsigned char,double> { typedef double type; };
  2887. template<> struct superset<signed char,unsigned char> { typedef short type; };
  2888. template<> struct superset<signed char,char> { typedef short type; };
  2889. template<> struct superset<signed char,unsigned short> { typedef int type; };
  2890. template<> struct superset<signed char,short> { typedef short type; };
  2891. template<> struct superset<signed char,unsigned int> { typedef cimg_int64 type; };
  2892. template<> struct superset<signed char,int> { typedef int type; };
  2893. template<> struct superset<signed char,cimg_uint64> { typedef cimg_int64 type; };
  2894. template<> struct superset<signed char,cimg_int64> { typedef cimg_int64 type; };
  2895. template<> struct superset<signed char,float> { typedef float type; };
  2896. template<> struct superset<signed char,double> { typedef double type; };
  2897. template<> struct superset<char,unsigned char> { typedef short type; };
  2898. template<> struct superset<char,signed char> { typedef short type; };
  2899. template<> struct superset<char,unsigned short> { typedef int type; };
  2900. template<> struct superset<char,short> { typedef short type; };
  2901. template<> struct superset<char,unsigned int> { typedef cimg_int64 type; };
  2902. template<> struct superset<char,int> { typedef int type; };
  2903. template<> struct superset<char,cimg_uint64> { typedef cimg_int64 type; };
  2904. template<> struct superset<char,cimg_int64> { typedef cimg_int64 type; };
  2905. template<> struct superset<char,float> { typedef float type; };
  2906. template<> struct superset<char,double> { typedef double type; };
  2907. template<> struct superset<unsigned short,char> { typedef int type; };
  2908. template<> struct superset<unsigned short,signed char> { typedef int type; };
  2909. template<> struct superset<unsigned short,short> { typedef int type; };
  2910. template<> struct superset<unsigned short,unsigned int> { typedef unsigned int type; };
  2911. template<> struct superset<unsigned short,int> { typedef int type; };
  2912. template<> struct superset<unsigned short,cimg_uint64> { typedef cimg_uint64 type; };
  2913. template<> struct superset<unsigned short,cimg_int64> { typedef cimg_int64 type; };
  2914. template<> struct superset<unsigned short,float> { typedef float type; };
  2915. template<> struct superset<unsigned short,double> { typedef double type; };
  2916. template<> struct superset<short,unsigned short> { typedef int type; };
  2917. template<> struct superset<short,unsigned int> { typedef cimg_int64 type; };
  2918. template<> struct superset<short,int> { typedef int type; };
  2919. template<> struct superset<short,cimg_uint64> { typedef cimg_int64 type; };
  2920. template<> struct superset<short,cimg_int64> { typedef cimg_int64 type; };
  2921. template<> struct superset<short,float> { typedef float type; };
  2922. template<> struct superset<short,double> { typedef double type; };
  2923. template<> struct superset<unsigned int,char> { typedef cimg_int64 type; };
  2924. template<> struct superset<unsigned int,signed char> { typedef cimg_int64 type; };
  2925. template<> struct superset<unsigned int,short> { typedef cimg_int64 type; };
  2926. template<> struct superset<unsigned int,int> { typedef cimg_int64 type; };
  2927. template<> struct superset<unsigned int,cimg_uint64> { typedef cimg_uint64 type; };
  2928. template<> struct superset<unsigned int,cimg_int64> { typedef cimg_int64 type; };
  2929. template<> struct superset<unsigned int,float> { typedef float type; };
  2930. template<> struct superset<unsigned int,double> { typedef double type; };
  2931. template<> struct superset<int,unsigned int> { typedef cimg_int64 type; };
  2932. template<> struct superset<int,cimg_uint64> { typedef cimg_int64 type; };
  2933. template<> struct superset<int,cimg_int64> { typedef cimg_int64 type; };
  2934. template<> struct superset<int,float> { typedef float type; };
  2935. template<> struct superset<int,double> { typedef double type; };
  2936. template<> struct superset<cimg_uint64,char> { typedef cimg_int64 type; };
  2937. template<> struct superset<cimg_uint64,signed char> { typedef cimg_int64 type; };
  2938. template<> struct superset<cimg_uint64,short> { typedef cimg_int64 type; };
  2939. template<> struct superset<cimg_uint64,int> { typedef cimg_int64 type; };
  2940. template<> struct superset<cimg_uint64,cimg_int64> { typedef cimg_int64 type; };
  2941. template<> struct superset<cimg_uint64,float> { typedef double type; };
  2942. template<> struct superset<cimg_uint64,double> { typedef double type; };
  2943. template<> struct superset<cimg_int64,float> { typedef double type; };
  2944. template<> struct superset<cimg_int64,double> { typedef double type; };
  2945. template<> struct superset<float,cimg_uint64> { typedef double type; };
  2946. template<> struct superset<float,cimg_int64> { typedef double type; };
  2947. template<> struct superset<float,double> { typedef double type; };
  2948. #ifdef cimg_use_half
  2949. template<> struct superset<half,unsigned short> { typedef float type; };
  2950. template<> struct superset<half,short> { typedef float type; };
  2951. template<> struct superset<half,unsigned int> { typedef float type; };
  2952. template<> struct superset<half,int> { typedef float type; };
  2953. template<> struct superset<half,cimg_uint64> { typedef float type; };
  2954. template<> struct superset<half,cimg_int64> { typedef float type; };
  2955. template<> struct superset<half,float> { typedef float type; };
  2956. template<> struct superset<half,double> { typedef double type; };
  2957. #endif
  2958. template<typename t1, typename t2, typename t3> struct superset2 {
  2959. typedef typename superset<t1, typename superset<t2,t3>::type>::type type;
  2960. };
  2961. template<typename t1, typename t2, typename t3, typename t4> struct superset3 {
  2962. typedef typename superset<t1, typename superset2<t2,t3,t4>::type>::type type;
  2963. };
  2964. template<typename t1, typename t2> struct last { typedef t2 type; };
  2965. #define _cimg_Tt typename cimg::superset<T,t>::type
  2966. #define _cimg_Tfloat typename cimg::superset<T,float>::type
  2967. #define _cimg_tfloat typename cimg::superset<t,float>::type
  2968. #define _cimg_Ttfloat typename cimg::superset2<T,t,float>::type
  2969. #define _cimg_Ttdouble typename cimg::superset2<T,t,double>::type
  2970. // Define variables used internally by CImg.
  2971. #if cimg_display==1
  2972. struct X11_static {
  2973. unsigned int nb_wins;
  2974. pthread_t *events_thread;
  2975. pthread_cond_t wait_event;
  2976. pthread_mutex_t wait_event_mutex;
  2977. CImgDisplay **wins;
  2978. Display *display;
  2979. unsigned int nb_bits;
  2980. bool is_blue_first;
  2981. bool is_shm_enabled;
  2982. bool byte_order;
  2983. #ifdef cimg_use_xrandr
  2984. XRRScreenSize *resolutions;
  2985. Rotation curr_rotation;
  2986. unsigned int curr_resolution;
  2987. unsigned int nb_resolutions;
  2988. #endif
  2989. X11_static():nb_wins(0),events_thread(0),display(0),
  2990. nb_bits(0),is_blue_first(false),is_shm_enabled(false),byte_order(false) {
  2991. #ifdef __FreeBSD__
  2992. XInitThreads();
  2993. #endif
  2994. wins = new CImgDisplay*[1024];
  2995. pthread_mutex_init(&wait_event_mutex,0);
  2996. pthread_cond_init(&wait_event,0);
  2997. #ifdef cimg_use_xrandr
  2998. resolutions = 0;
  2999. curr_rotation = 0;
  3000. curr_resolution = nb_resolutions = 0;
  3001. #endif
  3002. }
  3003. ~X11_static() {
  3004. delete[] wins;
  3005. /*
  3006. if (events_thread) {
  3007. pthread_cancel(*events_thread);
  3008. delete events_thread;
  3009. }
  3010. if (display) { } // XCloseDisplay(display); }
  3011. pthread_cond_destroy(&wait_event);
  3012. pthread_mutex_unlock(&wait_event_mutex);
  3013. pthread_mutex_destroy(&wait_event_mutex);
  3014. */
  3015. }
  3016. }; // struct X11_static { ...
  3017. #if defined(cimg_module)
  3018. X11_static& X11_attr();
  3019. #elif defined(cimg_main)
  3020. X11_static& X11_attr() { static X11_static val; return val; }
  3021. #else
  3022. inline X11_static& X11_attr() { static X11_static val; return val; }
  3023. #endif
  3024. #elif cimg_display==2
  3025. struct Win32_static {
  3026. HANDLE wait_event;
  3027. Win32_static() { wait_event = CreateEvent(0,FALSE_WIN,FALSE_WIN,0); }
  3028. }; // struct Win32_static { ...
  3029. #if defined(cimg_module)
  3030. Win32_static& Win32_attr();
  3031. #elif defined(cimg_main)
  3032. Win32_static& Win32_attr() { static Win32_static val; return val; }
  3033. #else
  3034. inline Win32_static& Win32_attr() { static Win32_static val; return val; }
  3035. #endif
  3036. #endif
  3037. #define cimg_lock_display() cimg::mutex(15)
  3038. #define cimg_unlock_display() cimg::mutex(15,0)
  3039. struct Mutex_static {
  3040. #if cimg_OS==1 && (defined(cimg_use_pthread) || cimg_display==1)
  3041. pthread_mutex_t mutex[32];
  3042. Mutex_static() { for (unsigned int i = 0; i<32; ++i) pthread_mutex_init(&mutex[i],0); }
  3043. void lock(const unsigned int n) { pthread_mutex_lock(&mutex[n]); }
  3044. void unlock(const unsigned int n) { pthread_mutex_unlock(&mutex[n]); }
  3045. int trylock(const unsigned int n) { return pthread_mutex_trylock(&mutex[n]); }
  3046. #elif cimg_OS==2
  3047. HANDLE mutex[32];
  3048. Mutex_static() { for (unsigned int i = 0; i<32; ++i) mutex[i] = CreateMutex(0,FALSE_WIN,0); }
  3049. void lock(const unsigned int n) { WaitForSingleObject(mutex[n],INFINITE); }
  3050. void unlock(const unsigned int n) { ReleaseMutex(mutex[n]); }
  3051. int trylock(const unsigned int) { return 0; }
  3052. #else
  3053. Mutex_static() {}
  3054. void lock(const unsigned int) {}
  3055. void unlock(const unsigned int) {}
  3056. int trylock(const unsigned int) { return 0; }
  3057. #endif
  3058. }; // struct Mutex_static { ...
  3059. #if defined(cimg_module)
  3060. Mutex_static& Mutex_attr();
  3061. #elif defined(cimg_main)
  3062. Mutex_static& Mutex_attr() { static Mutex_static val; return val; }
  3063. #else
  3064. inline Mutex_static& Mutex_attr() { static Mutex_static val; return val; }
  3065. #endif
  3066. #if defined(cimg_use_magick)
  3067. struct Magick_static {
  3068. Magick_static() {
  3069. Magick::InitializeMagick("");
  3070. }
  3071. }; // struct Magick_static { ...
  3072. static Magick_static _Magick_static;
  3073. #endif
  3074. #if defined(cimg_use_fftw3) && !defined(cimg_use_fftw3_singlethread)
  3075. struct FFTW3_static {
  3076. FFTW3_static() {
  3077. fftw_init_threads();
  3078. }
  3079. }; // struct FFTW3_static { ...
  3080. static FFTW3_static _FFTW3_static;
  3081. #endif
  3082. #if cimg_display==1
  3083. // Define keycodes for X11-based graphical systems.
  3084. const unsigned int keyESC = XK_Escape;
  3085. const unsigned int keyF1 = XK_F1;
  3086. const unsigned int keyF2 = XK_F2;
  3087. const unsigned int keyF3 = XK_F3;
  3088. const unsigned int keyF4 = XK_F4;
  3089. const unsigned int keyF5 = XK_F5;
  3090. const unsigned int keyF6 = XK_F6;
  3091. const unsigned int keyF7 = XK_F7;
  3092. const unsigned int keyF8 = XK_F8;
  3093. const unsigned int keyF9 = XK_F9;
  3094. const unsigned int keyF10 = XK_F10;
  3095. const unsigned int keyF11 = XK_F11;
  3096. const unsigned int keyF12 = XK_F12;
  3097. const unsigned int keyPAUSE = XK_Pause;
  3098. const unsigned int key1 = XK_1;
  3099. const unsigned int key2 = XK_2;
  3100. const unsigned int key3 = XK_3;
  3101. const unsigned int key4 = XK_4;
  3102. const unsigned int key5 = XK_5;
  3103. const unsigned int key6 = XK_6;
  3104. const unsigned int key7 = XK_7;
  3105. const unsigned int key8 = XK_8;
  3106. const unsigned int key9 = XK_9;
  3107. const unsigned int key0 = XK_0;
  3108. const unsigned int keyBACKSPACE = XK_BackSpace;
  3109. const unsigned int keyINSERT = XK_Insert;
  3110. const unsigned int keyHOME = XK_Home;
  3111. const unsigned int keyPAGEUP = XK_Page_Up;
  3112. const unsigned int keyTAB = XK_Tab;
  3113. const unsigned int keyQ = XK_q;
  3114. const unsigned int keyW = XK_w;
  3115. const unsigned int keyE = XK_e;
  3116. const unsigned int keyR = XK_r;
  3117. const unsigned int keyT = XK_t;
  3118. const unsigned int keyY = XK_y;
  3119. const unsigned int keyU = XK_u;
  3120. const unsigned int keyI = XK_i;
  3121. const unsigned int keyO = XK_o;
  3122. const unsigned int keyP = XK_p;
  3123. const unsigned int keyDELETE = XK_Delete;
  3124. const unsigned int keyEND = XK_End;
  3125. const unsigned int keyPAGEDOWN = XK_Page_Down;
  3126. const unsigned int keyCAPSLOCK = XK_Caps_Lock;
  3127. const unsigned int keyA = XK_a;
  3128. const unsigned int keyS = XK_s;
  3129. const unsigned int keyD = XK_d;
  3130. const unsigned int keyF = XK_f;
  3131. const unsigned int keyG = XK_g;
  3132. const unsigned int keyH = XK_h;
  3133. const unsigned int keyJ = XK_j;
  3134. const unsigned int keyK = XK_k;
  3135. const unsigned int keyL = XK_l;
  3136. const unsigned int keyENTER = XK_Return;
  3137. const unsigned int keySHIFTLEFT = XK_Shift_L;
  3138. const unsigned int keyZ = XK_z;
  3139. const unsigned int keyX = XK_x;
  3140. const unsigned int keyC = XK_c;
  3141. const unsigned int keyV = XK_v;
  3142. const unsigned int keyB = XK_b;
  3143. const unsigned int keyN = XK_n;
  3144. const unsigned int keyM = XK_m;
  3145. const unsigned int keySHIFTRIGHT = XK_Shift_R;
  3146. const unsigned int keyARROWUP = XK_Up;
  3147. const unsigned int keyCTRLLEFT = XK_Control_L;
  3148. const unsigned int keyAPPLEFT = XK_Super_L;
  3149. const unsigned int keyALT = XK_Alt_L;
  3150. const unsigned int keySPACE = XK_space;
  3151. const unsigned int keyALTGR = XK_Alt_R;
  3152. const unsigned int keyAPPRIGHT = XK_Super_R;
  3153. const unsigned int keyMENU = XK_Menu;
  3154. const unsigned int keyCTRLRIGHT = XK_Control_R;
  3155. const unsigned int keyARROWLEFT = XK_Left;
  3156. const unsigned int keyARROWDOWN = XK_Down;
  3157. const unsigned int keyARROWRIGHT = XK_Right;
  3158. const unsigned int keyPAD0 = XK_KP_0;
  3159. const unsigned int keyPAD1 = XK_KP_1;
  3160. const unsigned int keyPAD2 = XK_KP_2;
  3161. const unsigned int keyPAD3 = XK_KP_3;
  3162. const unsigned int keyPAD4 = XK_KP_4;
  3163. const unsigned int keyPAD5 = XK_KP_5;
  3164. const unsigned int keyPAD6 = XK_KP_6;
  3165. const unsigned int keyPAD7 = XK_KP_7;
  3166. const unsigned int keyPAD8 = XK_KP_8;
  3167. const unsigned int keyPAD9 = XK_KP_9;
  3168. const unsigned int keyPADADD = XK_KP_Add;
  3169. const unsigned int keyPADSUB = XK_KP_Subtract;
  3170. const unsigned int keyPADMUL = XK_KP_Multiply;
  3171. const unsigned int keyPADDIV = XK_KP_Divide;
  3172. #elif cimg_display==2
  3173. // Define keycodes for Windows.
  3174. const unsigned int keyESC = VK_ESCAPE;
  3175. const unsigned int keyF1 = VK_F1;
  3176. const unsigned int keyF2 = VK_F2;
  3177. const unsigned int keyF3 = VK_F3;
  3178. const unsigned int keyF4 = VK_F4;
  3179. const unsigned int keyF5 = VK_F5;
  3180. const unsigned int keyF6 = VK_F6;
  3181. const unsigned int keyF7 = VK_F7;
  3182. const unsigned int keyF8 = VK_F8;
  3183. const unsigned int keyF9 = VK_F9;
  3184. const unsigned int keyF10 = VK_F10;
  3185. const unsigned int keyF11 = VK_F11;
  3186. const unsigned int keyF12 = VK_F12;
  3187. const unsigned int keyPAUSE = VK_PAUSE;
  3188. const unsigned int key1 = '1';
  3189. const unsigned int key2 = '2';
  3190. const unsigned int key3 = '3';
  3191. const unsigned int key4 = '4';
  3192. const unsigned int key5 = '5';
  3193. const unsigned int key6 = '6';
  3194. const unsigned int key7 = '7';
  3195. const unsigned int key8 = '8';
  3196. const unsigned int key9 = '9';
  3197. const unsigned int key0 = '0';
  3198. const unsigned int keyBACKSPACE = VK_BACK;
  3199. const unsigned int keyINSERT = VK_INSERT;
  3200. const unsigned int keyHOME = VK_HOME;
  3201. const unsigned int keyPAGEUP = VK_PRIOR;
  3202. const unsigned int keyTAB = VK_TAB;
  3203. const unsigned int keyQ = 'Q';
  3204. const unsigned int keyW = 'W';
  3205. const unsigned int keyE = 'E';
  3206. const unsigned int keyR = 'R';
  3207. const unsigned int keyT = 'T';
  3208. const unsigned int keyY = 'Y';
  3209. const unsigned int keyU = 'U';
  3210. const unsigned int keyI = 'I';
  3211. const unsigned int keyO = 'O';
  3212. const unsigned int keyP = 'P';
  3213. const unsigned int keyDELETE = VK_DELETE;
  3214. const unsigned int keyEND = VK_END;
  3215. const unsigned int keyPAGEDOWN = VK_NEXT;
  3216. const unsigned int keyCAPSLOCK = VK_CAPITAL;
  3217. const unsigned int keyA = 'A';
  3218. const unsigned int keyS = 'S';
  3219. const unsigned int keyD = 'D';
  3220. const unsigned int keyF = 'F';
  3221. const unsigned int keyG = 'G';
  3222. const unsigned int keyH = 'H';
  3223. const unsigned int keyJ = 'J';
  3224. const unsigned int keyK = 'K';
  3225. const unsigned int keyL = 'L';
  3226. const unsigned int keyENTER = VK_RETURN;
  3227. const unsigned int keySHIFTLEFT = VK_SHIFT;
  3228. const unsigned int keyZ = 'Z';
  3229. const unsigned int keyX = 'X';
  3230. const unsigned int keyC = 'C';
  3231. const unsigned int keyV = 'V';
  3232. const unsigned int keyB = 'B';
  3233. const unsigned int keyN = 'N';
  3234. const unsigned int keyM = 'M';
  3235. const unsigned int keySHIFTRIGHT = VK_SHIFT;
  3236. const unsigned int keyARROWUP = VK_UP;
  3237. const unsigned int keyCTRLLEFT = VK_CONTROL;
  3238. const unsigned int keyAPPLEFT = VK_LWIN;
  3239. const unsigned int keyALT = VK_LMENU;
  3240. const unsigned int keySPACE = VK_SPACE;
  3241. const unsigned int keyALTGR = VK_CONTROL;
  3242. const unsigned int keyAPPRIGHT = VK_RWIN;
  3243. const unsigned int keyMENU = VK_APPS;
  3244. const unsigned int keyCTRLRIGHT = VK_CONTROL;
  3245. const unsigned int keyARROWLEFT = VK_LEFT;
  3246. const unsigned int keyARROWDOWN = VK_DOWN;
  3247. const unsigned int keyARROWRIGHT = VK_RIGHT;
  3248. const unsigned int keyPAD0 = 0x60;
  3249. const unsigned int keyPAD1 = 0x61;
  3250. const unsigned int keyPAD2 = 0x62;
  3251. const unsigned int keyPAD3 = 0x63;
  3252. const unsigned int keyPAD4 = 0x64;
  3253. const unsigned int keyPAD5 = 0x65;
  3254. const unsigned int keyPAD6 = 0x66;
  3255. const unsigned int keyPAD7 = 0x67;
  3256. const unsigned int keyPAD8 = 0x68;
  3257. const unsigned int keyPAD9 = 0x69;
  3258. const unsigned int keyPADADD = VK_ADD;
  3259. const unsigned int keyPADSUB = VK_SUBTRACT;
  3260. const unsigned int keyPADMUL = VK_MULTIPLY;
  3261. const unsigned int keyPADDIV = VK_DIVIDE;
  3262. #else
  3263. // Define random keycodes when no display is available.
  3264. // (should rarely be used then!).
  3265. const unsigned int keyESC = 1U; //!< Keycode for the \c ESC key (architecture-dependent)
  3266. const unsigned int keyF1 = 2U; //!< Keycode for the \c F1 key (architecture-dependent)
  3267. const unsigned int keyF2 = 3U; //!< Keycode for the \c F2 key (architecture-dependent)
  3268. const unsigned int keyF3 = 4U; //!< Keycode for the \c F3 key (architecture-dependent)
  3269. const unsigned int keyF4 = 5U; //!< Keycode for the \c F4 key (architecture-dependent)
  3270. const unsigned int keyF5 = 6U; //!< Keycode for the \c F5 key (architecture-dependent)
  3271. const unsigned int keyF6 = 7U; //!< Keycode for the \c F6 key (architecture-dependent)
  3272. const unsigned int keyF7 = 8U; //!< Keycode for the \c F7 key (architecture-dependent)
  3273. const unsigned int keyF8 = 9U; //!< Keycode for the \c F8 key (architecture-dependent)
  3274. const unsigned int keyF9 = 10U; //!< Keycode for the \c F9 key (architecture-dependent)
  3275. const unsigned int keyF10 = 11U; //!< Keycode for the \c F10 key (architecture-dependent)
  3276. const unsigned int keyF11 = 12U; //!< Keycode for the \c F11 key (architecture-dependent)
  3277. const unsigned int keyF12 = 13U; //!< Keycode for the \c F12 key (architecture-dependent)
  3278. const unsigned int keyPAUSE = 14U; //!< Keycode for the \c PAUSE key (architecture-dependent)
  3279. const unsigned int key1 = 15U; //!< Keycode for the \c 1 key (architecture-dependent)
  3280. const unsigned int key2 = 16U; //!< Keycode for the \c 2 key (architecture-dependent)
  3281. const unsigned int key3 = 17U; //!< Keycode for the \c 3 key (architecture-dependent)
  3282. const unsigned int key4 = 18U; //!< Keycode for the \c 4 key (architecture-dependent)
  3283. const unsigned int key5 = 19U; //!< Keycode for the \c 5 key (architecture-dependent)
  3284. const unsigned int key6 = 20U; //!< Keycode for the \c 6 key (architecture-dependent)
  3285. const unsigned int key7 = 21U; //!< Keycode for the \c 7 key (architecture-dependent)
  3286. const unsigned int key8 = 22U; //!< Keycode for the \c 8 key (architecture-dependent)
  3287. const unsigned int key9 = 23U; //!< Keycode for the \c 9 key (architecture-dependent)
  3288. const unsigned int key0 = 24U; //!< Keycode for the \c 0 key (architecture-dependent)
  3289. const unsigned int keyBACKSPACE = 25U; //!< Keycode for the \c BACKSPACE key (architecture-dependent)
  3290. const unsigned int keyINSERT = 26U; //!< Keycode for the \c INSERT key (architecture-dependent)
  3291. const unsigned int keyHOME = 27U; //!< Keycode for the \c HOME key (architecture-dependent)
  3292. const unsigned int keyPAGEUP = 28U; //!< Keycode for the \c PAGEUP key (architecture-dependent)
  3293. const unsigned int keyTAB = 29U; //!< Keycode for the \c TAB key (architecture-dependent)
  3294. const unsigned int keyQ = 30U; //!< Keycode for the \c Q key (architecture-dependent)
  3295. const unsigned int keyW = 31U; //!< Keycode for the \c W key (architecture-dependent)
  3296. const unsigned int keyE = 32U; //!< Keycode for the \c E key (architecture-dependent)
  3297. const unsigned int keyR = 33U; //!< Keycode for the \c R key (architecture-dependent)
  3298. const unsigned int keyT = 34U; //!< Keycode for the \c T key (architecture-dependent)
  3299. const unsigned int keyY = 35U; //!< Keycode for the \c Y key (architecture-dependent)
  3300. const unsigned int keyU = 36U; //!< Keycode for the \c U key (architecture-dependent)
  3301. const unsigned int keyI = 37U; //!< Keycode for the \c I key (architecture-dependent)
  3302. const unsigned int keyO = 38U; //!< Keycode for the \c O key (architecture-dependent)
  3303. const unsigned int keyP = 39U; //!< Keycode for the \c P key (architecture-dependent)
  3304. const unsigned int keyDELETE = 40U; //!< Keycode for the \c DELETE key (architecture-dependent)
  3305. const unsigned int keyEND = 41U; //!< Keycode for the \c END key (architecture-dependent)
  3306. const unsigned int keyPAGEDOWN = 42U; //!< Keycode for the \c PAGEDOWN key (architecture-dependent)
  3307. const unsigned int keyCAPSLOCK = 43U; //!< Keycode for the \c CAPSLOCK key (architecture-dependent)
  3308. const unsigned int keyA = 44U; //!< Keycode for the \c A key (architecture-dependent)
  3309. const unsigned int keyS = 45U; //!< Keycode for the \c S key (architecture-dependent)
  3310. const unsigned int keyD = 46U; //!< Keycode for the \c D key (architecture-dependent)
  3311. const unsigned int keyF = 47U; //!< Keycode for the \c F key (architecture-dependent)
  3312. const unsigned int keyG = 48U; //!< Keycode for the \c G key (architecture-dependent)
  3313. const unsigned int keyH = 49U; //!< Keycode for the \c H key (architecture-dependent)
  3314. const unsigned int keyJ = 50U; //!< Keycode for the \c J key (architecture-dependent)
  3315. const unsigned int keyK = 51U; //!< Keycode for the \c K key (architecture-dependent)
  3316. const unsigned int keyL = 52U; //!< Keycode for the \c L key (architecture-dependent)
  3317. const unsigned int keyENTER = 53U; //!< Keycode for the \c ENTER key (architecture-dependent)
  3318. const unsigned int keySHIFTLEFT = 54U; //!< Keycode for the \c SHIFTLEFT key (architecture-dependent)
  3319. const unsigned int keyZ = 55U; //!< Keycode for the \c Z key (architecture-dependent)
  3320. const unsigned int keyX = 56U; //!< Keycode for the \c X key (architecture-dependent)
  3321. const unsigned int keyC = 57U; //!< Keycode for the \c C key (architecture-dependent)
  3322. const unsigned int keyV = 58U; //!< Keycode for the \c V key (architecture-dependent)
  3323. const unsigned int keyB = 59U; //!< Keycode for the \c B key (architecture-dependent)
  3324. const unsigned int keyN = 60U; //!< Keycode for the \c N key (architecture-dependent)
  3325. const unsigned int keyM = 61U; //!< Keycode for the \c M key (architecture-dependent)
  3326. const unsigned int keySHIFTRIGHT = 62U; //!< Keycode for the \c SHIFTRIGHT key (architecture-dependent)
  3327. const unsigned int keyARROWUP = 63U; //!< Keycode for the \c ARROWUP key (architecture-dependent)
  3328. const unsigned int keyCTRLLEFT = 64U; //!< Keycode for the \c CTRLLEFT key (architecture-dependent)
  3329. const unsigned int keyAPPLEFT = 65U; //!< Keycode for the \c APPLEFT key (architecture-dependent)
  3330. const unsigned int keyALT = 66U; //!< Keycode for the \c ALT key (architecture-dependent)
  3331. const unsigned int keySPACE = 67U; //!< Keycode for the \c SPACE key (architecture-dependent)
  3332. const unsigned int keyALTGR = 68U; //!< Keycode for the \c ALTGR key (architecture-dependent)
  3333. const unsigned int keyAPPRIGHT = 69U; //!< Keycode for the \c APPRIGHT key (architecture-dependent)
  3334. const unsigned int keyMENU = 70U; //!< Keycode for the \c MENU key (architecture-dependent)
  3335. const unsigned int keyCTRLRIGHT = 71U; //!< Keycode for the \c CTRLRIGHT key (architecture-dependent)
  3336. const unsigned int keyARROWLEFT = 72U; //!< Keycode for the \c ARROWLEFT key (architecture-dependent)
  3337. const unsigned int keyARROWDOWN = 73U; //!< Keycode for the \c ARROWDOWN key (architecture-dependent)
  3338. const unsigned int keyARROWRIGHT = 74U; //!< Keycode for the \c ARROWRIGHT key (architecture-dependent)
  3339. const unsigned int keyPAD0 = 75U; //!< Keycode for the \c PAD0 key (architecture-dependent)
  3340. const unsigned int keyPAD1 = 76U; //!< Keycode for the \c PAD1 key (architecture-dependent)
  3341. const unsigned int keyPAD2 = 77U; //!< Keycode for the \c PAD2 key (architecture-dependent)
  3342. const unsigned int keyPAD3 = 78U; //!< Keycode for the \c PAD3 key (architecture-dependent)
  3343. const unsigned int keyPAD4 = 79U; //!< Keycode for the \c PAD4 key (architecture-dependent)
  3344. const unsigned int keyPAD5 = 80U; //!< Keycode for the \c PAD5 key (architecture-dependent)
  3345. const unsigned int keyPAD6 = 81U; //!< Keycode for the \c PAD6 key (architecture-dependent)
  3346. const unsigned int keyPAD7 = 82U; //!< Keycode for the \c PAD7 key (architecture-dependent)
  3347. const unsigned int keyPAD8 = 83U; //!< Keycode for the \c PAD8 key (architecture-dependent)
  3348. const unsigned int keyPAD9 = 84U; //!< Keycode for the \c PAD9 key (architecture-dependent)
  3349. const unsigned int keyPADADD = 85U; //!< Keycode for the \c PADADD key (architecture-dependent)
  3350. const unsigned int keyPADSUB = 86U; //!< Keycode for the \c PADSUB key (architecture-dependent)
  3351. const unsigned int keyPADMUL = 87U; //!< Keycode for the \c PADMUL key (architecture-dependent)
  3352. const unsigned int keyPADDIV = 88U; //!< Keycode for the \c PADDDIV key (architecture-dependent)
  3353. #endif
  3354. const double PI = 3.14159265358979323846; //!< Value of the mathematical constant PI
  3355. // Define a 10x13 binary font (small sans).
  3356. static const char *const data_font_small[] = {
  3357. " UwlwnwoyuwHwlwmwcwlwnw[xuwowlwmwoyuwRwlwnxcw Mw (wnwnwuwpwuypwuwoy"
  3358. "ZwnwmwuwowuwmwnwnwuwowuwfwuxnwnwmwuwpwuypwuwZwnwnwtwpwtwow'y Hw cwnw >{ jw %xdxZwdw_wexfwYwkw 7yowoyFx=w "
  3359. "ry qw %wuw !xnwkwnwoyuwfwuw[wkwnwcwowrwpwdwuwoxuwpwkwnwoyuwRwkwnwbwpwNyoyoyoyoy;wdwnxpxtxowG|!ydwnwuwowtwow"
  3360. "pxswqxlwnxnxmwDwoyoxnyoymwp{oyq{pyoy>ypwqwpwp{oyqzo{q{pzrwrwowlwqwswpwnwqwsxswpypzoyqzozq}swrwrwqwtwswswtxsxswq"
  3361. "ws}qwnwkwnydwew_wfwdwkwmwowkw(w0wmwmwGwtwdxQw swuwnwo{q{pynwp|rwtwtwqydwcwcwcwmwmxgwqwpwnzpwuwpzoyRzoyoyexnynwd"
  3362. "z\\xnxgxrwsxrwsyswowmwmwmwmwmwmwo}ryp{q{q{q{nwmwnwmwozqxswpyoyoyoyoyeyuwswrwrwrwrwrwrwrwrwqwrwmwtwnwmwnwuwpwuyp"
  3363. "wuwoyZwmwnwuwowuwmwqwkwuwowuwoxnwuxowmwnwuwpwuypwuwZwmwnwuwowuwnwowmwtw\\wuwuwqwswqwswqwswqwswEwqwtweypzr~qyIw "
  3364. "rwswewnwuwowuwozswtwuwqwtwmwnwlwowuwuwowOxpxuxqwuwowswqwswoxpwlwjwqwswqwsw<wrwowrwuwqwrwqwswrwswpwmwmwrwswrwowl"
  3365. "wqwtwownxsxsxswqwswqwswqwswrwswqwrwowpwrwrwqwtwswswswswqwswmwpwmwlwoxuxSw_wfwdwYwkw(w0wmwmwGwtwoxnwNw uwswpwuwp"
  3366. "wmwmwswq{rwrwrwtwtwrwswfydwdyZwnwtwrwqwrwswowowdwrwqxuwSwrwfwuwnwlwnw[yuw[wowtwgwswqwswqwswewuwowuwowuwowuwowuw"
  3367. "nwowuwowswqwmwmwmwjwmwnwmwowswrxswqwswqwswqwswqwswqwswrwrwqwswrwrwrwrwrwrwrwrwqwswqzpwtw #w DwPwtwtwswqwswuwuwu"
  3368. "wswswuwswqwGwqxtwf{qzr~r{qzqwrwpxowtwrw rzcwnwuwq}rwuwqwtwuwqwtwmwnwlwnynwOwowswowkwmwpwuwpwmwjwpwswqwswowmwjwi"
  3369. "wjxswsytwrwuwqwrwrwmwrwqwmwnwmwrwowlwqwuwnwnxsxswuwtwrwqwrwswrwqwswswqwjwpwrwqwswrwtwtwqwuwowuwmwowmwlwpxsx]ypz"
  3370. "oyozpypzozqznwmwowtwnwqzuyrzoypzozqwuxoypzpwswrwrwrwtwtwswrwrwrwq{owmwmwQyuwqwtwmwoxnypzqxswowowswqwswqwtxr|rwt"
  3371. "wtwqyp{q{qwswpwuwownwnwqwsxuwuxswrwrwtwtwswqwrwmwuwuwnwnwowtwpwuwuwewnzpwn{pwuwnwnxgwtxtwrwtwowtw_wuytwgynwmwlw"
  3372. "gwswpyuw[wowtwqwtwpwtwpwtwowuwmwnwuwowuwowuwowuwowuwowuwqxuwpwlwmwmwmwjwmwnwmwowrwswuwtwrwqwswqwswqwswqwswqwrwt"
  3373. "wqwswuwswrwrwrwrwrwrwrwpwuwpwswqwuwnyoyoyoyoyoyqyuyqyoyoyoyoymwqwjwmwnypzoyoyoyoyoynwnzqwswqwswqwswqwswrwrwqzqw"
  3374. "rw^}s}swtwtwswtwtwswtwtwK}rwuwe{s~t~s}rwtwqwrwpxowtwrw qwawewtwpwuwpxuwpycwlwnynwOwowswowkwpypwtwpzpzmwoypwsw[y"
  3375. "r}rymwrwtwtwtwrwuwq{qwmwrwq{q{rwm|owlwqxmwnwuwuwuwswuwtwrwqwrwswrwqwswswqylwpwrwqwswrwuwuwuwpwmwmwnwmwlwMwqwswq"
  3376. "wmwswqwswpwnwswqwswowmwowuwmwqwswswswswqwswqwswqwswqxnwswpwnwswrwrwrwtwtwrwtwqwrwmwqxlwlx]xuxrwtyqwuwlwpwtwpwmw"
  3377. "swqwtxpxowswrwqwswtwuxrwtwqwtwtwrwswrwswnwo{pwuwnxpwnwqwswtwtwswrwrwtwtwswuyuwswjwkwowpwrwowcwowuwnwnwswqxuxowo"
  3378. "wtwhwuwrwrzpwtwq}jwuwtwuw_}qyoxfwswpyuwowdyoxowtwryuwqyuwqyuwmwnwuwowuwowuwowuwowuwowuwqwt{twl{q{q{q{nwmwnwmwpz"
  3379. "twswuwtwrwqwswqwswqwswqwswqwqxpwtwtwswrwrwrwrwrwrwrwowowswqwuwkwmwmwmwmwmwowswswmwswqwswqwswqwswnwqwjwmwowswqws"
  3380. "wqwswqwswqwswqwswqwswgwtxqwswqwswqwswqwswrwrwqwswrwrw^wtwtwswqwswuwuwuwswuwswswqwHwowuwf}t~s|r}swrwrwrwqwtwpwtw"
  3381. "r~#zcwewtwoynwuxtwtwswgwlwowuwuwr}gyexowswowlwlwrwswlwqwswowowswpz^yayqwqwtwtwuwrwswrwrwrwmwrwqwmwnwsyswrwowlwq"
  3382. "wuwnwnwuwuwuwswtwuwrwqwrzqwqwszmyowpwrwpwuwqwuwuwuwpwmwmwnwlwmwPzqwswqwmwswq{pwnwswqwswowmwoxlwqwswswswswqwswqw"
  3383. "swqwswqwlxnwnwswqwtwqwuwuwuwqxowtwmwnwmwmwoytwiwtwtwswswpwtxqzpwswpxowswpwuwowuwpwswrwtwtwswtwtwrwtwqwtwtwrwswr"
  3384. "wswnwowswqwswowownwqwswtwtwswrwqwuwuwrwuyuwt~pwq~pwq~pwcwowuwozpwswowewswiwuwrwiwtwjwjwuytw\\wRwswoxuwHwtwpwswq"
  3385. "wtxqwswqxowswqwswqwswqwswqwswqwswrwtwpwlwmwmwmwjwmwnwmwowrwswtwuwrwqwswqwswqwswqwswqwqxpwtwtwswrwrwrwrwrwrwrwow"
  3386. "owswqwtwozpzpzpzpzpzr~swm{q{q{q{nwqwjwmwowswqwswqwswqwswqwswqwswqwswr}rwuwuwqwswqwswqwswqwswqwtwpwswqwtw\\wuwuw"
  3387. "qwswqwswqwswqwswJ}qxf}t~rzp{rwrwrwrwqwtwpwtwrw qwawg}owuwpwuwtwuwswuwfwlwmwmwPwnwswowmwkwr|mwqwswowowswmw^yo}oy"
  3388. "qwqwszq{rwrwrwmwrwqwmwnwqwswrwowlwqwtwownwtwtwswtwuwrwqwrwnwqwswtwkwowpwrwpwuwqwuwuwuwqwuwnwnwmwlwmwQwswqwswqwm"
  3389. "wswqwlwnwswqwswowmwowuwmwqwswswswswqwswqwswqwswqwjwownwswqwtwqwuwuwuwqxowtwnwmwmwmwpwtyhwtwtwswswpwswqwtwpwswqw"
  3390. "mwswpwuwpwtwpwswrwtwtwswtwtwrwtwqwtwtwrwswrwswnwowswqwswpwnwnwqwsxuwuxswrwpyqwqwswjwkwqwuwuwrwrwqwuwuwewowuwnwn"
  3391. "wswq{ownxuwiwtxtwrzpwtwkwjwuwtwuw\\wRwswnwuwSzpwtwowtxqwrwrwtxrxn{q{q{q{q{q{s{pwlwmwmwmwjwmwnwmwowrwswtwuwrwqws"
  3392. "wqwswqwswqwswqwrwtwqwuwswswrwrwrwrwrwrwrwowozpwswqwswqwswqwswqwswqwswqwswswswowmwmwmwmwjwqwjwmwowswqwswqwswqwsw"
  3393. "qwswqwswqwswgwuwuwqwswqwswqwswqwswqwtwpwswqwtw[yoyoyoyoyGwmwdwuwuwpxnxnyqwrwqwtwpwtwoxpw rwswSwuwmwuwpwuwtwuxsw"
  3394. "ewlwcwPwnxuxownwnwswnwlwqwswowowswnwZygygwkwswrwrwqwswrwswpwmwmwrwswrwowlwqwswpwnwqwswsxqwswqwmwswrwswqwrwowpxt"
  3395. "xowowswqwswowowlwlwmwQwswqwswqwmwswqwswpwnwswqwswowmwowtwnwqwswswswswqwswqwswqwswqwmwswpwnwswpxowswqwtwoxnwlwmw"
  3396. "mw[xuxrwtxpwswqwtwpwswqwmwswpypwtwpwswrwtwtwsxuwuxrwtwqwtwtwrwswrwswnwnwuwpwswqwmwmwswq{rwrwowowswqwkwlwoypwtwo"
  3397. "ydwowuwnwn{owmwlwgwrwfwtw^wrw6wswnwuwJwtwowtzswrwrwtzswmwswqwswqwswqwswqwswqwswswswowswqwmwmwmwjwmwnwmwowswrwsx"
  3398. "qwswqwswqwswqwswqwswrwrwqwswrxtxrxtxrxtxrxtxowowmwswqwswqwswqwswqwswqwswqwswswtxowmwswqwswqwswqwswnwqwjwmwowswq"
  3399. "wswqwswqwswqwswqwswqwswowoxtwqwswqwswqwswqwswpxowswpx Wwlwbwnxcwpwrwqzpwtwoxo|!ydwfwtwozpwsxszuxgxnxcwmwcwoxmyp"
  3400. "{q{pymwpzoyowmypymwmwjwiwkwowrwrwqws{oyqzo{qwlzrwrwowlwqwrwq{rwqwswsxpypwlyqwrwqznwoznwowswrxsxpwp}qwkwnwPzqzoy"
  3401. "ozpyowmzqwswowmwowswowqwswswswswpypzozqwlynxozpxowswrwrwpwn{owmwmwQxuxqzoxnyoypwswowpwrwqzpxuxq{qwtxq{qzpylwoyq"
  3402. "}r{qwnyuypwpwrwownydwcwcwcwnzq{rwqwpwmwkwgzHz]}U|owuw@wqwswrytwqwqyqwqwswqwswqwswqwswqwswqwuwr{ryp{q{q{q{nwmwnw"
  3403. "mwozqwsxpyoyoyoyoygwuypzpzpzpznwowmwuypzpzpzpzpzpzryuzryoyoyoyoymwqwjwmwnypwswpyoyoyoyoyfzozpzpzpzpwnzow \\w"
  3404. "OwnwXw[w SwGz kx0x lxdx gw[w=wiw*wbyowoyGwKwowewawcwow YwOwoz Ewjwuwdw 7w 9w Iwnwlw \\w 0|*y[x=wiw,"
  3405. "xWw=wKwowewawcwow Yw hwVx 8w 9w Jxmwnxp" };
  3406. // Define a 26x32 font (normal sans).
  3407. static const char *const data_font_normal[] = {
  3408. " #{}~}a{|y~f{|y~}f{|}|x{}|j{|y}y{|y}g{}y~}|2y~|a{}~}f{}y~|"
  3409. "gy}|yy}|i{}~}a{}~}f{}y~}gy}|yx}N{|}|x{}|hy~|ay~|fx~|g{}y~|y{}~j{|y~|yy~}5{}~}a{}~}f{}y~}gy~}yy~}e{|y~ "
  3410. " 2{}~}c{|y~f{|y~}~}h{}w~y}~|j{}y~y{}y~h{}~y}y~|2y~|c{}~}f{"
  3411. "}~y}~|hy~}yy~}hy~|c{|~}f{|~y}~|hy~}y{}y~O{}w~y}~|gy~|cy~|fy~|}~|i{|~y}y~}~}j{|y~|yy~}4{}~|c{}~}f{}~|}~}hy~}yy~}"
  3412. "ey~| g{|}y~} J{}~|dy~|fy~y{}~|i{~}{|}y~}i{}y~y{}y"
  3413. "~i{|~}x{~}2{|y~d{|~}f{|~}yy~hy~}yy~}gy~cy~f{|~}y{}~|iy~}y{}y~P{|~}{|}y~|ey~d{}~|fy~x{}~|j{}~y{|}~}i{|y}|yy}|3{}"
  3414. "~|e{}~}f{}~|y{|~|iy}|yx}f{}~| fy~y}~} k{|y~| /{|y~| y{}"
  3415. "~} Xy|e{|}|f{|}wy|5{|~|x{}~1{|}|ey|ey|wy|M{|}|e{|}|fy|wy| g{|}|3{|y~|_{}~}g{|y~2{}~|y{}~|5{|y~^y~}g{}y~N{|"
  3416. "}|^{|}|g{|}| s{}~}_{|y~|gy~} Z{}~}_{|y~|gy~} )y}| -{|y~ Jy}|yy}| "
  3417. "X{}y~ 4{|~}y{|~} P{| n{|y~`{|y~fx~}3{}~x{|~|4{}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| '{|n~} m{|y}w~}|6{|y~} ?{}y~Z{}~} nx~}|-{}~} B{|y~ g{}~}j{|y~|O{}~}F{|y~J{|y~"
  3734. "h{}y~ q{}~} -{|}x~}| f{}y~}| t{|x~}| kx~}|f{}y~dx~}| "
  3735. " :{}~} I{" };
  3736. // Define a 52x64 font (large sans).
  3737. static const char *const data_font_large[] = {
  3738. " "
  3739. " -{| "
  3740. " [{|x}|Dw}|Pw}| @{}v~} C{|w}|Ew}|Pv}| xv|Ev|Pu| kv|Dw|P{|v} 6{|w}|E{|x}|P{"
  3741. "|w}| pw}| "
  3742. " G{|w~}F{}w~P{}v~}Q{}w~}|w{|x~X{|v~vv~|U{|r~| D{}w~F{}w~P{}u~S{|v~|wv~}V{}w~|G{|w~|Q{"
  3743. "|u~|Sv~}w{}w~}\"{|}x~}|v{|x~W{}w~|F{}w~Q{|u~}Q{}x~}|v{|x~X{}w~}w{|v~ G{}w~F{}w~|Q{}u~Rv~|w{}w~}O{}w~ "
  3744. " "
  3745. " E{|w~|H{}w~P{}t~}Ss~}|y{}x~X{|v~vv~|V{|p~ Cw~}H{|w~|Q{|t~}T{|v~|wv~}U{}w~Gw~}Q{|s~|Tv~}w{}w~}#{|s~}|{|x~"
  3746. "}V{}w~|H{}w~Ps~}St~}w{}y~}X{}w~}w{|v~ Fw~}H{|w~|Q{|t~}Sv~|w{}w~}P{|w~| "
  3747. " D{|w~|J{|w~|Q{|x~}{w~|U"
  3748. "{}l~}X{|v~vv~|Vw~}x{}x~} D{|w~|J{|w~|Q{|w~{}x~}U{|v~|wv~}T{}x~}Iw~}Pw~y|w~Tv~}w{}w~}#k~|U{}w~I{|w~|Q{}x~|{}x~|U"
  3749. "{}r~|{|x~}X{}w~}w{|v~ Ew~|Iw~}Pw~|}x~}Tv~|w{}w~}Q{|w~| M{| "
  3750. " q{}w~Jw~|Q{|x~}xw~Ux~}y{|}t~}W{|v~vv"
  3751. "~|W{|x~|v{|x~| D{|w~|Kw~}Pw~x{}x~|V{|v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W{|w~|#{|w~| j{}w~ s{}w~Uw~} )v~}Iy~} gw~| W{|w~| :{|}w|}w~| /t~y}x|y}v~} U{|}|x{|w~| "
  4417. " f{}x~| W{|}v~}Sw~}H{|}v~} Qq~| J{|y} *{|}l~}| O{}q"
  4418. "~ tt| `{|i~} Lr~| aw~}9{|w~| `{}q~ l{}s~}Tw~|U{|s~}| 9v~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~"
  4419. "} )v~}Iy~} gw~| W{|w~| :{|q~ .{|i~} U{|q~ ly}w|}w~| [{}q~Rw~}"
  4420. "L{}q~ P{}r~ M{|y}u~y}y| L{}r~| R{|j~} Ks~} `w~}9{|w~| "
  4421. " `{}r~| jy|v}|Tw~|U{|u}| 6v~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~} )v~}Iy}| gw~| W{|w~| :{|r~| "
  4422. " -{|k~}| U{|r~} l{}r~} Z{}r~|Rw~}L{}r~| O{}t~ "
  4423. " k{}t~} -{|`}| `{|}m~}| Jt~} _w~}9{|w~| `{}s~| :w~| cv~ S{|w~}W{|w~|#{|w~| j{}w~ s{}"
  4424. "w~Uw~} )v~} d{|w~| 9y}w~y} ){}o~}| S{|}u~}| k{}r~ Y{}s~|Qw~"
  4425. "}L{}s~| M{}w~} j{}w~}| +{}`~} ]{|x}v~y}| Gw~y} ]w~}9{|w~"
  4426. "| `{}v~}| 8w~| cv~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~} g{|w~| 8{|}v~y}| Ly| "
  4427. " g{|y}w~}| X{}v~}|Ow~}L{}v~}| Iy| "
  4428. "l{}`~} Ww~| "
  4429. " L{}`~} Ww}| "
  4430. " r{" };
  4431. // Define a 104x128 binary font (huge sans).
  4432. static const char *const data_font_huge[] = {
  4433. " "
  4434. " "
  4435. " "
  4436. " "
  4437. " "
  4438. " "
  4439. " "
  4440. " "
  4441. " FY AY "
  4442. "'Z ;W @Y @Y 'Z Y @Y (Z :Y ?Y (Z 0Y ?Y (Z >X "
  4443. " "
  4444. " "
  4445. " "
  4446. " "
  4447. " )X AX '\\ )XAV 7YDY -] BY BY '[ +YEY 2X AY (\\ -YDY 'XAU 3Y AY (\\ )XAV 8YD"
  4448. "Y LY AY (\\ ,YEY #Y "
  4449. " "
  4450. " "
  4451. " "
  4452. " (X CX '^ +[CU 6ZEY .` C"
  4453. "X CY '] -ZEZ 2X CY (^ .ZEZ )[CU 2Y CY (] *[CU 7ZEZ LY CY (] -ZEZ %Y "
  4454. " "
  4455. " "
  4456. " "
  4457. " "
  4458. " 'Y EY '^ ,^FV 6ZEY /b CX DX '_ .ZEZ 2Y DX '_ /ZEZ +_FV 1X CX (_ ,^FV 7ZEZ "
  4459. " KX CX (_ .ZEZ &Y "
  4460. " "
  4461. " "
  4462. " "
  4463. " %Y GY '` .aHV 6ZEY 1e DY FX"
  4464. " 'a /ZEZ 1Y FX '` /ZEZ +aHV 0X EX '` .aHV 7ZEZ JX EX (a /ZEZ &X "
  4465. " "
  4466. " "
  4467. " "
  4468. " "
  4469. " #X GX 'XNX 0dKW 6ZEY 1f DY HX &WMX 0ZEZ 0X GX 'XMW 0ZEZ ,dLX /X GX 'WMX 0dLX 7ZEZ"
  4470. " IX GX 'WMX 0ZEZ 'X :T "
  4471. " "
  4472. " "
  4473. " "
  4474. " ;X IX 'XLX 1o 5ZEY 2ZLY "
  4475. " CX IX &WKW 0ZEZ /X HX (XLX 1ZEZ ,o .Y HX (WKX 1o 6ZEZ IY IY (WKW 0ZEZ (X <Z "
  4476. " "
  4477. " "
  4478. " "
  4479. " "
  4480. " =X KX 'XJX 3WKd 5ZEY 3XGX CX JX 'WIW 1ZEZ .X JX (XJX 2ZEZ -WKd -X "
  4481. "IX (WIW 2WKd 6ZEZ HX IX (WIW 1ZEZ )X =^ "
  4482. " "
  4483. " "
  4484. " "
  4485. " >X MX &WH"
  4486. "W 3VHa 4ZEY 3WDW CX LX 'WGW 2ZEZ -X LX 'WHW 2ZEZ -VHa +X KX (XHW 3VHa 5ZEZ GX KX (WGW 2ZEZ )X "
  4487. " ?b "
  4488. " "
  4489. " "
  4490. " "
  4491. " ?W MW &WFW 4VF^ 3ZEY 4WBV BW MX 'WEW 3ZEZ ,W M"
  4492. "X 'WFW 3ZEZ -VF^ )X MX 'WFW 4VF^ 4ZEZ FX MX 'WFW 3ZEZ *X ?d "
  4493. " "
  4494. " "
  4495. " "
  4496. " "
  4497. " ?W X 'WDW 5UC[ 2ZEY 4VAV AW X &WDW 4ZEZ +W NW 'WDW 4ZEZ -UC[ 'W MW 'WDW 5UC[ 3ZEZ "
  4498. "EW MW 'WDW 4ZEZ +X ?f "
  4499. " "
  4500. " "
  4501. " "
  4502. " @X \"X 'WBW 6UAW 0ZEY 4V@V B"
  4503. "X !W &WBV 4ZEZ +X !W 'WBW 5ZEZ .VAW $W W 'WBW 6UAW 1ZEZ DW W 'WBV 4ZEZ +W >f "
  4504. " "
  4505. " "
  4506. " "
  4507. " "
  4508. " ?X #W 'W@W U?V AX #W &W@V NX #W &V@W 9W \"W 'W@V .W "
  4509. "\"W 'W@V !W >XHX "
  4510. " 3Y "
  4511. " "
  4512. " "
  4513. " 6W $W &V>V U?V @W $W &W>V "
  4514. " NW $X 'V>V 8W $X (W>V /X $W 'W>V #W >XFX "
  4515. " 5Z "
  4516. " "
  4517. " ,Z "
  4518. " GZ "
  4519. " #U?V NY 7Z ,X CVCW MY "
  4520. " 7Z ,X $Z 7Z ,X >Z 6Y ,X 4Z 7Y +W 7Y @Z "
  4521. " "
  4522. " +Z "
  4523. " "
  4524. " HY \"U?V "
  4525. " MY 8Y ,Y CVBV LY 9Z ,Y #Z 9Z ,Z >Z 8Y ,Y 3Y 8Z ,Y 9Y "
  4526. " ?Z "
  4527. " *Y "
  4528. " "
  4529. " IY !U?V "
  4530. " LY :Y ,[ $R>U ,V@V MZ :Y +Z #Y 9Y +Z ?R"
  4531. ">U 8Y 9Y +Z %S?U HY :Z ,[ ;Y ?[ "
  4532. " "
  4533. " )Y "
  4534. " 8U "
  4535. " 9Y V@U JY <Y"
  4536. " +[ 'XAU ,V@V LY ;Y +\\ #Y ;Y +\\ CXAU 7Y ;Z ,\\ )XAV HY ;Y +[ <Z "
  4537. " ?U ;T $W /W "
  4538. " 8e !f LY Y LX "
  4539. " L] :Y <Y NX 0X >Y @Y /X 0Y K` .X "
  4540. " ^ =ZEY @Y "
  4541. " NVAV <P -X +Y =Y +] )[CU 7YDY 4V@V KY ="
  4542. "Y +] ,YDY 5Y =Y *] .YDY 5[ M[CU 6Y <Y ,] *[CV 7YDY Y =Y +] ,YEZ !Y =Y FYDY 8X "
  4543. " EU :T %W .X "
  4544. " 9e !f KY !Y LY \"a :Y "
  4545. "<Y NX 0X >Y E^ /X 0_ %f 1] 'c "
  4546. " @ZEZ AY MV"
  4547. "CW <R 4a .Y >X *^ +]DU 7ZEZ 5U>U JY ?Y *^ -YEZ 4Y "
  4548. " ?Y *^ .ZEZ 5[ ]DU 5Y >Y +^ ,]DU 6ZEZ Y ?Y +_ .ZEZ \"Y <Y FYEZ :[ FU "
  4549. " 7Y -T 7W#W <Y 9X -W DU KY HZ \"\\ 4Z M[ \""
  4550. "Y LZ +\\ 8] >Z G[ G\\ @e !f JX !Y "
  4551. "LY %d :Y <Y NX 0X >Y Ha /X 0b *j L] D_ "
  4552. " +g A[ LY 8Z -ZEZ \"Y 1o )V FX NZ FY "
  4553. "%Y ,X NX*Z NW 3WEW H\\ #[ !Z \"[ \"[ \"[ G[7T 8g 0Y "
  4554. "@Y +_ ,_FV 7ZEZ 5U>U IY @Y +` .YEZ 3X ?X *` /ZEZ 4[:P 8_FV 4X ?Y +` ._EU 6ZEZ NX @Y *_ .ZEZ #Y ;Y"
  4555. " FYEZ ;] GU <b 1T :]'X @b >W ,X "
  4556. " FV a \"d -g >d (d +b %b 4f Bg Ie \"e \"h "
  4557. " Ge !f IX \"Y LY &e :Y <Y NX 0X >Y Jc /X 0c "
  4558. " -n $g I` .j >a ;e HU .U +b Ac 2ZEZ 'b "
  4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a )a 5~d)dCb N~S I~S H~R H~R 6Z !Z !Z \"Z :~V Kb0Y MbCb HbCb HbCb HbCb HbCb >bCh%Z(Z"
  4620. "#Z(Z$Z(Z$Y'Y![.Z HZ Z;Z 1X NX )WBV 5VBZ $e >W MX )WBW !X MX )WBW #` /UBZ (W MX )WBW 6UBZ "
  4621. " 9X MW (WCW MX 3Y GXHW M~d 8U *[ +m HnFn A] 9~\\ +^=Y"
  4622. " -Z +XCX -X@X 2U DXAX5W E\\=V (X LX #X .R@V?Q ,X :\\A\\ @m 7\\>_ CY<_ -c ?y :^=V F~Q E]>^ D]@] "
  4623. " <Z@^ @~P 9b ;Z=d Aa;^'Z>j E~R E| Ha8^$Z(Z :Z \"Z 4Z5] CZ 2b-b(b1Z `<_ FZ@d I`=` K[@d C_:Z ~b&Z(Z'Z#Z8Z2`"
  4624. "2Z0[4[ LZ/[\"~^ @X #X <Y 0\\N] NX )Y LY ,Y (Y ;X NX 0X >Y >Y ;Z "
  4625. "!X !Y 8Z9Y 6d 4[5R CZ ;Y:Z 5b 8~R D~Q MbAb 8` =~]+[;Z;Z&`=` 1f KX Lg "
  4626. " ;U *\\=T =Y0Y 4X ,Z;R 5Z3Y &W !Y3Y 3W@W EW LX *W %jEW KV -X=X @W'X W'X EX1W ,b "
  4627. "*b (b )b )b )b 7ZH~R)a:] N~R H~R G~R H~R 6Z !Z !Z \"Z :Z>j Lb0Y N_<` J`<_ J`=` J`=` J`=` @`=e%Z(Z#Z(Z$Z(Z$Y'Y"
  4628. " Z/[ HZ !Z9Y 0W X )WAW 6VAW \"d <W X (VAW X X (V@V &a .VAW &X NW (V@V 6UAW 6X X )WAW "
  4629. " NW 2Y N\\ #[ \"\\ #\\ #[ MXHW L~b 7U +\\ ,n IoGp C_ ;~] ,]:X -Z "
  4630. "+XCX -X@X 8c LX@X7X E[:T (X MX \"X /TAVAT .X :\\?\\ Am 7Y9] CT4] .c ?Y J]8S Z E\\;\\ E]=[ "
  4631. " <W;\\ B~T ;b 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FY >dFY >cEY#d5j Ch 1j 1j 1j 1j -Y Y NX Y"
  5269. " &e 9X0X :e ,f -f -e ,f 4XFe 0cEX <bEX <bEX <bEY 4Y 4YFb )Z ?` (a '` '` (a %X 'T "
  5270. " L{ K_ 0T 4X&[ Ga AX \"Y :Y EX G_ Ie #e !_ c /a EY "
  5271. " EY Hc ?e FZ +b Ni )d Nc (X =Y #Y A^ J^ %a /] N"
  5272. "c ;Y NX *` 7YD^ ,]CX 1c ^ /Y DY X Y 8] 1YF] *\\ N` %c DY 4Y *YG\\A"
  5273. "X J\\;] 9e A^ =` 7YC] *_ G[ >a NU CZ N` 9X -T<[ "
  5274. " LYG]BX 5WEW %U HW NX MX GZ (d +b (b )b )a )b 9V;a "
  5275. ")c *c *c *c =a 4_ &^ %^ $^ &_ &_ :_/c <b +c *c *c *c 3_ K_ &` '` '_ &` 1WB_ *] $] $^ %^ NZ "
  5276. "4YD^ 'Y 6Q HQ GQ FQ HQ LX &S DQ )T 4W Q :Q"
  5277. " 9Y #X :Y EX ?Q 8R ?R @Q @R MQ =Y DY @R -Q <Z "
  5278. " #R >RM] !R <R X <X #Y ;Q <Q GR !Q @Q 2Y MX #R 0Y=Q Q=X 'Q "
  5279. " @Q *Z DY X Y ;Y <P AQ CQ ;Y 4Y *YAQ8P @Q0Q -Y 8X 7Y 3Y=Q LQ "
  5280. " JQ 4Z IU 3X -W@[ KYAQ8P 1WEW $U IV MW LW FZ "
  5281. " V KQ GR HQ GQ GQ 0T2Q HR GR HR HQ ,Q %Q GP GQ FQ GQ "
  5282. "GP *P NQ ,V MQ GR HR HR #Q =Q FQ HR HQ FQ *V:Q LQ GQ GQ GQ GY 3Y=Q !Y "
  5283. " 9X MT +X #X :Y EX "
  5284. " 5X BZ 7Y 7] 8X <X #Y "
  5285. " HY MX 0Y 'X MY CY X Y ;Y 8Y 4Y *Y 1Y E"
  5286. "X 3Y CZ IU 3X -\\I_ KY 8WEW $V"
  5287. " %Z NU 0R #V "
  5288. " )T <Z 3Y =Y 8X "
  5289. " MT +X $X 9X DX 6Y AZ NR "
  5290. " =Z 6\\ 8X <X #Y HY MX 0Y '"
  5291. "X NZ CY X X :Y 8Y 4Y *Y 1Y EX 3Y "
  5292. " CZ IU 3X -q JY 8WEW #V &Z NV "
  5293. " 0V (R "
  5294. " <Y 2Y =Y 8X MT *X "
  5295. "%X 9X EY 6X @[!T >Z 5\\ "
  5296. " 9X ;X $Y HY NY 0Y 'X NY BY X !Y "
  5297. ":Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3X -p "
  5298. " IY 8WEW #V &Z MV "
  5299. " 0U 'P ;Y 2Y >Z 8X "
  5300. " MT *X &X 9X DX "
  5301. " 5X ?\\%W ?Z 4\\ :X ;X $Y "
  5302. " IZ NY 0Y 'X NY BZ !X !Y :Y 8Y 4Y *Y 1Y EX 3Y "
  5303. " CZ IU 3X -o HY 8WEW \"V "
  5304. " 'Z LU 0V "
  5305. " CZ 2Y >Y 7X "
  5306. " MT )X 'X 9X DX 5W <\\(X ?"
  5307. "Z 3\\ ;Y <X $Y IY MY 0Y 'X "
  5308. " Z AY !X !Y :Y 8Y 4Y *Y 1Y EX 3Y CZ I"
  5309. "U 3X -n GY 8WEW \"V '[3Q <V "
  5310. " 0V DY 1Y "
  5311. "?Z 7X MT )X (X 8W "
  5312. " CX 6X ;],[ AZ 1\\ <e"
  5313. " GX 2f JZ MY 0Y 'X Y @Z \"X \"Z :Y 8"
  5314. "Y 4Y *Y 1Y EX 3Y CZ IU 3X ,k "
  5315. " EY 8WEW !V 'Z4R <V "
  5316. " 0V EZ 1Y ?Y ARGX "
  5317. " MT (X )X 8W DX 5W "
  5318. " 9^1^ AZ 0\\ =e GX 2f KZ LY"
  5319. " 0Y 'X !Z @[ #X #Z 9Y 8Y 4Y *Y 1Y EX 3Y "
  5320. " CZ IU 3X )f CY 8WEW !V '[7T "
  5321. " ;V 1V "
  5322. " EY 0Y ?Y FWGW "
  5323. " LT 'W *X 8W CX 5W 8`7` A[ "
  5324. " /\\ >e GX 2f KZ LY 0Y 'X !Y >"
  5325. "\\ %X &] 9Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3"
  5326. "X $^ @Y 8WEW !V '\\:V ;V "
  5327. " 1W GZ 0Y @Z "
  5328. " FWHX LT 'X +W 7W "
  5329. " V 5b?c A[ -\\ ?e !f "
  5330. " <P2\\ MY /Y 'X \"Z >f /X 0g 9Y 8Y 4Y *Y "
  5331. " 1Y EX 3Y CZ IU 3X 5Y "
  5332. " NV &\\=X ;V "
  5333. "1W GY /Y AZ EWHX "
  5334. " LT &W ,X 7V V 3~T "
  5335. " A] ,\\ @e !f <R5\\ LY /Y "
  5336. "'X #Z =f /X 0f 8Y 8Y 4Y *Y 1Y EX 3Y "
  5337. " CZ IU 3X 5Y NW '^B[ <W "
  5338. " 1W "
  5339. " HZ /Y AZ DWIX LT &X -"
  5340. "W 6U NV 1~P B_ *\\ "
  5341. " Ae !f <U:] LZ /Y 'X #Z <e /X 0e 7Y "
  5342. " 8Y 4Y *Y 1Y EX 3Y CZ IU 3X "
  5343. " 5Y X &aJ_ <W "
  5344. " 2X IZ .Y BZ CWJY "
  5345. " LT %X /X "
  5346. " 7| Hf )\\ Be !f <X?_ N[ "
  5347. " .Y 'X %[ :d /X 0e 7Y 8Y 4Y *Y 1Y EX 3Y "
  5348. " CZ IU 3X 5Y -PDX %v "
  5349. " JQDX ?QEY "
  5350. " J[ .Y D\\ CXLY "
  5351. " KT 7x Fe "
  5352. " -_Me %b .Y 'X /e 9c /X 0c "
  5353. "5Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3X "
  5354. " 5Y -d $u Je "
  5355. " ?d $d -Y Ne Ad "
  5356. " KT "
  5357. " 5s Cd ,v %b -"
  5358. "Y 'X 0e 6a /X 0b 4Y 8Y 4Y *Y 1Y EX 3Y "
  5359. " CZ IU 3X 5Y -d #t Jd "
  5360. " >d "
  5361. " %e -Y Nd @c "
  5362. " (m @c "
  5363. " +u $b -Y 'X 0d 2^ /X 0_ 1Y 8Y 4Y *Y "
  5364. " 1Y EX 3Y CZ IT 2X 5Y "
  5365. "-c !q Hd >c "
  5366. " $d ,Y Nd ?b "
  5367. " %g ="
  5368. "b *t #a ,Y 'X 0d "
  5369. " ,X /X 0Y +Y 8Y 4Y *Y 1Y EX 3Y CZ '"
  5370. "X 5Y -c Nm Fc "
  5371. " =c $c +Y Nc "
  5372. " >a "
  5373. " M\\ 8a \"~Y 1"
  5374. "r !` +Y 'X 0c 1X 1Y 8Y 4Y *Y 1Y EX 3Y "
  5375. " CZ &W 5Y -b Lj "
  5376. " Db <b "
  5377. " #b *Y Nb <_ "
  5378. " (_ "
  5379. " ~Y 1q _ *Y 'X 0b 0X 1Y "
  5380. " 8Y 4Y *Y 1Y EX 3Y CZ "
  5381. " 3Y -` He A` "
  5382. " :` !a )Y Na :] "
  5383. " "
  5384. " '] M~Y .l M] (Y 'X "
  5385. " 0` .X 1Y 8Y 4Y *Y 1Y EX 3Y "
  5386. " KY *Z B^ 9Z "
  5387. " 5Z M` (Y N` "
  5388. " 8Z "
  5389. " %X H~Y "
  5390. " *d I[ &Y 'X 0^ ,X 1Y 8Y 4Y *Y 1Y EX 3Y"
  5391. " KY "
  5392. " "
  5393. " H^ &Y N] 3V "
  5394. " "
  5395. " B~Y #X CU !X &X /Y (X 1"
  5396. "Y 7X 4X )X 0Y EX 2X "
  5397. " KY "
  5398. " HZ \"X MY "
  5399. " "
  5400. " J~Y "
  5401. " 9X "
  5402. " "
  5403. " "
  5404. " "
  5405. " 3~Y "
  5406. " 9X "
  5407. " "
  5408. " "
  5409. " "
  5410. " 3~Y "
  5411. " 9X "
  5412. " "
  5413. " "
  5414. " '" };
  5415. // Define a 40x38 'danger' color logo (used by cimg::dialog()).
  5416. static const unsigned char logo40x38[4576] = {
  5417. 177,200,200,200,3,123,123,0,36,200,200,200,1,123,123,0,2,255,255,0,1,189,189,189,1,0,0,0,34,200,200,200,
  5418. 1,123,123,0,4,255,255,0,1,189,189,189,1,0,0,0,1,123,123,123,32,200,200,200,1,123,123,0,5,255,255,0,1,0,0,
  5419. 0,2,123,123,123,30,200,200,200,1,123,123,0,6,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,29,200,200,200,
  5420. 1,123,123,0,7,255,255,0,1,0,0,0,2,123,123,123,28,200,200,200,1,123,123,0,8,255,255,0,1,189,189,189,1,0,0,0,
  5421. 2,123,123,123,27,200,200,200,1,123,123,0,9,255,255,0,1,0,0,0,2,123,123,123,26,200,200,200,1,123,123,0,10,255,
  5422. 255,0,1,189,189,189,1,0,0,0,2,123,123,123,25,200,200,200,1,123,123,0,3,255,255,0,1,189,189,189,3,0,0,0,1,189,
  5423. 189,189,3,255,255,0,1,0,0,0,2,123,123,123,24,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,3,255,255,0,1,189,
  5424. 189,189,1,0,0,0,2,123,123,123,23,200,200,200,1,123,123,0,4,255,255,0,5,0,0,0,4,255,255,0,1,0,0,0,2,123,123,123,
  5425. 22,200,200,200,1,123,123,0,5,255,255,0,5,0,0,0,4,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,21,200,200,200,
  5426. 1,123,123,0,5,255,255,0,5,0,0,0,5,255,255,0,1,0,0,0,2,123,123,123,20,200,200,200,1,123,123,0,6,255,255,0,5,0,0,
  5427. 0,5,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,19,200,200,200,1,123,123,0,6,255,255,0,1,123,123,0,3,0,0,0,1,
  5428. 123,123,0,6,255,255,0,1,0,0,0,2,123,123,123,18,200,200,200,1,123,123,0,7,255,255,0,1,189,189,189,3,0,0,0,1,189,
  5429. 189,189,6,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,17,200,200,200,1,123,123,0,8,255,255,0,3,0,0,0,8,255,255,
  5430. 0,1,0,0,0,2,123,123,123,16,200,200,200,1,123,123,0,9,255,255,0,1,123,123,0,1,0,0,0,1,123,123,0,8,255,255,0,1,189,
  5431. 189,189,1,0,0,0,2,123,123,123,15,200,200,200,1,123,123,0,9,255,255,0,1,189,189,189,1,0,0,0,1,189,189,189,9,255,
  5432. 255,0,1,0,0,0,2,123,123,123,14,200,200,200,1,123,123,0,11,255,255,0,1,0,0,0,10,255,255,0,1,189,189,189,1,0,0,0,2,
  5433. 123,123,123,13,200,200,200,1,123,123,0,23,255,255,0,1,0,0,0,2,123,123,123,12,200,200,200,1,123,123,0,11,255,255,0,
  5434. 1,189,189,189,2,0,0,0,1,189,189,189,9,255,255,0,1,189,189,189,1,0,0,0,2,123,123,123,11,200,200,200,1,123,123,0,11,
  5435. 255,255,0,4,0,0,0,10,255,255,0,1,0,0,0,2,123,123,123,10,200,200,200,1,123,123,0,12,255,255,0,4,0,0,0,10,255,255,0,
  5436. 1,189,189,189,1,0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,12,255,255,0,1,189,189,189,2,0,0,0,1,189,189,189,11,
  5437. 255,255,0,1,0,0,0,2,123,123,123,9,200,200,200,1,123,123,0,27,255,255,0,1,0,0,0,3,123,123,123,8,200,200,200,1,123,
  5438. 123,0,26,255,255,0,1,189,189,189,1,0,0,0,3,123,123,123,9,200,200,200,1,123,123,0,24,255,255,0,1,189,189,189,1,0,0,
  5439. 0,4,123,123,123,10,200,200,200,1,123,123,0,24,0,0,0,5,123,123,123,12,200,200,200,27,123,123,123,14,200,200,200,25,
  5440. 123,123,123,86,200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0 };
  5441. //! Get/set default output stream for the \CImg library messages.
  5442. /**
  5443. \param file Desired output stream. Set to \c 0 to get the currently used output stream only.
  5444. \return Currently used output stream.
  5445. **/
  5446. inline std::FILE* output(std::FILE *file) {
  5447. cimg::mutex(1);
  5448. static std::FILE *res = cimg::_stderr();
  5449. if (file) res = file;
  5450. cimg::mutex(1,0);
  5451. return res;
  5452. }
  5453. // Return number of available CPU cores.
  5454. inline unsigned int nb_cpus() {
  5455. unsigned int res = 1;
  5456. #if cimg_OS==2
  5457. SYSTEM_INFO sysinfo;
  5458. GetSystemInfo(&sysinfo);
  5459. res = (unsigned int)sysinfo.dwNumberOfProcessors;
  5460. #elif cimg_OS == 1
  5461. res = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
  5462. #endif
  5463. return res?res:1U;
  5464. }
  5465. // Lock/unlock mutex for CImg multi-thread programming.
  5466. inline int mutex(const unsigned int n, const int lock_mode) {
  5467. switch (lock_mode) {
  5468. case 0 : cimg::Mutex_attr().unlock(n); return 0;
  5469. case 1 : cimg::Mutex_attr().lock(n); return 0;
  5470. default : return cimg::Mutex_attr().trylock(n);
  5471. }
  5472. }
  5473. //! Display a warning message on the default output stream.
  5474. /**
  5475. \param format C-string containing the format of the message, as with <tt>std::printf()</tt>.
  5476. \note If configuration macro \c cimg_strict_warnings is set, this function throws a
  5477. \c CImgWarningException instead.
  5478. \warning As the first argument is a format string, it is highly recommended to write
  5479. \code
  5480. cimg::warn("%s",warning_message);
  5481. \endcode
  5482. instead of
  5483. \code
  5484. cimg::warn(warning_message);
  5485. \endcode
  5486. if \c warning_message can be arbitrary, to prevent nasty memory access.
  5487. **/
  5488. inline void warn(const char *const format, ...) {
  5489. if (cimg::exception_mode()>=1) {
  5490. char *const message = new char[16384];
  5491. std::va_list ap;
  5492. va_start(ap,format);
  5493. cimg_vsnprintf(message,16384,format,ap);
  5494. va_end(ap);
  5495. #ifdef cimg_strict_warnings
  5496. throw CImgWarningException(message);
  5497. #else
  5498. std::fprintf(cimg::output(),"\n%s[CImg] *** Warning ***%s%s\n",cimg::t_red,cimg::t_normal,message);
  5499. #endif
  5500. delete[] message;
  5501. }
  5502. }
  5503. // Execute an external system command.
  5504. /**
  5505. \param command C-string containing the command line to execute.
  5506. \param module_name Module name.
  5507. \return Status value of the executed command, whose meaning is OS-dependent.
  5508. \note This function is similar to <tt>std::system()</tt>
  5509. but it does not open an extra console windows
  5510. on Windows-based systems.
  5511. **/
  5512. inline int system(const char *const command, const char *const module_name=0, const bool is_verbose=false) {
  5513. cimg::unused(module_name);
  5514. #ifdef cimg_no_system_calls
  5515. return -1;
  5516. #else
  5517. if (is_verbose) return std::system(command);
  5518. #if cimg_OS==1
  5519. const unsigned int l = (unsigned int)std::strlen(command);
  5520. if (l) {
  5521. char *const ncommand = new char[l + 24];
  5522. std::memcpy(ncommand,command,l);
  5523. std::strcpy(ncommand + l," >/dev/null 2>&1"); // Make command silent
  5524. const int out_val = std::system(ncommand);
  5525. delete[] ncommand;
  5526. return out_val;
  5527. } else return -1;
  5528. #elif cimg_OS==2
  5529. PROCESS_INFORMATION pi;
  5530. STARTUPINFOA si;
  5531. std::memset(&pi,0,sizeof(PROCESS_INFORMATION));
  5532. std::memset(&si,0,sizeof(STARTUPINFO));
  5533. GetStartupInfoA(&si);
  5534. si.cb = sizeof(si);
  5535. si.wShowWindow = SW_HIDE;
  5536. si.dwFlags |= SW_HIDE | STARTF_USESHOWWINDOW;
  5537. const BOOL res = CreateProcessA((LPCSTR)module_name,(LPSTR)command,0,0,FALSE,0,0,0,&si,&pi);
  5538. if (res) {
  5539. WaitForSingleObject(pi.hProcess,INFINITE);
  5540. CloseHandle(pi.hThread);
  5541. CloseHandle(pi.hProcess);
  5542. return 0;
  5543. } else {
  5544. char* lpMsgBuf;
  5545. // Get the error message.
  5546. DWORD errorCode = GetLastError();
  5547. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  5548. 0,errorCode,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsgBuf,0,0);
  5549. cimg::warn("cimg::system() : Command '%s' (module name '%s) failed with error %lu: %s",
  5550. module_name==0?"(null)":module_name,
  5551. command==0?"(null)":command,
  5552. errorCode,lpMsgBuf);
  5553. return -1;
  5554. }
  5555. #else
  5556. return std::system(command);
  5557. #endif
  5558. #endif
  5559. }
  5560. //! Return a reference to a temporary variable of type T.
  5561. template<typename T>
  5562. inline T& temporary(const T&) {
  5563. static T temp;
  5564. return temp;
  5565. }
  5566. //! Exchange values of variables \c a and \c b.
  5567. template<typename T>
  5568. inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
  5569. //! Exchange values of variables (\c a1,\c a2) and (\c b1,\c b2).
  5570. template<typename T1, typename T2>
  5571. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2) {
  5572. cimg::swap(a1,b1); cimg::swap(a2,b2);
  5573. }
  5574. //! Exchange values of variables (\c a1,\c a2,\c a3) and (\c b1,\c b2,\c b3).
  5575. template<typename T1, typename T2, typename T3>
  5576. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3) {
  5577. cimg::swap(a1,b1,a2,b2); cimg::swap(a3,b3);
  5578. }
  5579. //! Exchange values of variables (\c a1,\c a2,...,\c a4) and (\c b1,\c b2,...,\c b4).
  5580. template<typename T1, typename T2, typename T3, typename T4>
  5581. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4) {
  5582. cimg::swap(a1,b1,a2,b2,a3,b3); cimg::swap(a4,b4);
  5583. }
  5584. //! Exchange values of variables (\c a1,\c a2,...,\c a5) and (\c b1,\c b2,...,\c b5).
  5585. template<typename T1, typename T2, typename T3, typename T4, typename T5>
  5586. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5) {
  5587. cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4); cimg::swap(a5,b5);
  5588. }
  5589. //! Exchange values of variables (\c a1,\c a2,...,\c a6) and (\c b1,\c b2,...,\c b6).
  5590. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
  5591. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6) {
  5592. cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5); cimg::swap(a6,b6);
  5593. }
  5594. //! Exchange values of variables (\c a1,\c a2,...,\c a7) and (\c b1,\c b2,...,\c b7).
  5595. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
  5596. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6,
  5597. T7& a7, T7& b7) {
  5598. cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6); cimg::swap(a7,b7);
  5599. }
  5600. //! Exchange values of variables (\c a1,\c a2,...,\c a8) and (\c b1,\c b2,...,\c b8).
  5601. template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
  5602. inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5, T6& a6, T6& b6,
  5603. T7& a7, T7& b7, T8& a8, T8& b8) {
  5604. cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7); cimg::swap(a8,b8);
  5605. }
  5606. //! Return the endianness of the current architecture.
  5607. /**
  5608. \return \c false for <i>Little Endian</i> or \c true for <i>Big Endian</i>.
  5609. **/
  5610. inline bool endianness() {
  5611. const int x = 1;
  5612. return ((unsigned char*)&x)[0]?false:true;
  5613. }
  5614. //! Reverse endianness of all elements in a memory buffer.
  5615. /**
  5616. \param[in,out] buffer Memory buffer whose endianness must be reversed.
  5617. \param size Number of buffer elements to reverse.
  5618. **/
  5619. template<typename T>
  5620. inline void invert_endianness(T* const buffer, const cimg_ulong size) {
  5621. if (size) switch (sizeof(T)) {
  5622. case 1 : break;
  5623. case 2 : {
  5624. for (unsigned short *ptr = (unsigned short*)buffer + size; ptr>(unsigned short*)buffer; ) {
  5625. const unsigned short val = *(--ptr);
  5626. *ptr = (unsigned short)((val>>8) | ((val<<8)));
  5627. }
  5628. } break;
  5629. case 4 : {
  5630. for (unsigned int *ptr = (unsigned int*)buffer + size; ptr>(unsigned int*)buffer; ) {
  5631. const unsigned int val = *(--ptr);
  5632. *ptr = (val>>24) | ((val>>8)&0xff00) | ((val<<8)&0xff0000) | (val<<24);
  5633. }
  5634. } break;
  5635. case 8 : {
  5636. const cimg_uint64
  5637. m0 = (cimg_uint64)0xff, m1 = m0<<8, m2 = m0<<16, m3 = m0<<24,
  5638. m4 = m0<<32, m5 = m0<<40, m6 = m0<<48, m7 = m0<<56;
  5639. for (cimg_uint64 *ptr = (cimg_uint64*)buffer + size; ptr>(cimg_uint64*)buffer; ) {
  5640. const cimg_uint64 val = *(--ptr);
  5641. *ptr = (((val&m7)>>56) | ((val&m6)>>40) | ((val&m5)>>24) | ((val&m4)>>8) |
  5642. ((val&m3)<<8) |((val&m2)<<24) | ((val&m1)<<40) | ((val&m0)<<56));
  5643. }
  5644. } break;
  5645. default : {
  5646. for (T* ptr = buffer + size; ptr>buffer; ) {
  5647. unsigned char *pb = (unsigned char*)(--ptr), *pe = pb + sizeof(T);
  5648. for (int i = 0; i<(int)sizeof(T)/2; ++i) swap(*(pb++),*(--pe));
  5649. }
  5650. }
  5651. }
  5652. }
  5653. inline void invert_endianness(bool* const, const cimg_ulong) {}
  5654. inline void invert_endianness(unsigned char* const, const cimg_ulong) {}
  5655. inline void invert_endianness(char* const, const cimg_ulong) {}
  5656. //! Reverse endianness of a single variable.
  5657. /**
  5658. \param[in,out] a Variable to reverse.
  5659. \return Reference to reversed variable.
  5660. **/
  5661. template<typename T>
  5662. inline T& invert_endianness(T& a) {
  5663. invert_endianness(&a,1);
  5664. return a;
  5665. }
  5666. // Conversion functions to get more precision when trying to store unsigned ints values as floats.
  5667. inline unsigned int float2uint(const float f) {
  5668. int tmp = 0;
  5669. std::memcpy(&tmp,&f,sizeof(float));
  5670. if (tmp>=0) return (unsigned int)f;
  5671. unsigned int u;
  5672. // use memcpy instead of assignment to avoid undesired optimizations by C++-compiler.
  5673. std::memcpy(&u,&f,sizeof(float));
  5674. return ((u)<<2)>>2; // set sign & exponent bit to 0
  5675. }
  5676. inline float uint2float(const unsigned int u) {
  5677. if (u<(1U<<19)) return (float)u; // Consider safe storage of unsigned int as floats until 19bits (i.e 524287)
  5678. float f;
  5679. const unsigned int v = u|(3U<<(8*sizeof(unsigned int)-2)); // set sign & exponent bit to 1
  5680. // use memcpy instead of simple assignment to avoid undesired optimizations by C++-compiler.
  5681. std::memcpy(&f,&v,sizeof(float));
  5682. return f;
  5683. }
  5684. //! Return the value of a system timer, with a millisecond precision.
  5685. /**
  5686. \note The timer does not necessarily starts from \c 0.
  5687. **/
  5688. inline cimg_uint64 time() {
  5689. #if cimg_OS==1
  5690. struct timeval st_time;
  5691. gettimeofday(&st_time,0);
  5692. return (cimg_uint64)st_time.tv_sec*1000 + (cimg_uint64)st_time.tv_usec/1000;
  5693. #elif cimg_OS==2
  5694. ULARGE_INTEGER ul;
  5695. FILETIME ft;
  5696. GetSystemTimeAsFileTime(&ft);
  5697. ul.LowPart = ft.dwLowDateTime;
  5698. ul.HighPart = ft.dwHighDateTime;
  5699. return (cimg_uint64)ul.QuadPart/10000;
  5700. #else
  5701. return 0;
  5702. #endif
  5703. }
  5704. // Implement a tic/toc mechanism to display elapsed time of algorithms.
  5705. inline cimg_uint64 tictoc(const bool is_tic);
  5706. //! Start tic/toc timer for time measurement between code instructions.
  5707. /**
  5708. \return Current value of the timer (same value as time()).
  5709. **/
  5710. inline cimg_uint64 tic() {
  5711. return cimg::tictoc(true);
  5712. }
  5713. //! End tic/toc timer and displays elapsed time from last call to tic().
  5714. /**
  5715. \return Time elapsed (in ms) since last call to tic().
  5716. **/
  5717. inline cimg_uint64 toc() {
  5718. return cimg::tictoc(false);
  5719. }
  5720. //! Sleep for a given numbers of milliseconds.
  5721. /**
  5722. \param milliseconds Number of milliseconds to wait for.
  5723. \note This function frees the CPU resources during the sleeping time.
  5724. It can be used to temporize your program properly, without wasting CPU time.
  5725. **/
  5726. inline void sleep(const unsigned int milliseconds) {
  5727. #if cimg_OS==1
  5728. struct timespec tv;
  5729. tv.tv_sec = milliseconds/1000;
  5730. tv.tv_nsec = (milliseconds%1000)*1000000;
  5731. nanosleep(&tv,0);
  5732. #elif cimg_OS==2
  5733. Sleep(milliseconds);
  5734. #else
  5735. cimg::unused(milliseconds);
  5736. #endif
  5737. }
  5738. inline unsigned int wait(const unsigned int milliseconds, cimg_uint64 *const p_timer) {
  5739. if (!*p_timer) *p_timer = cimg::time();
  5740. const cimg_uint64 current_time = cimg::time();
  5741. if (current_time<*p_timer || current_time>=*p_timer + milliseconds) { *p_timer = current_time; return 0; }
  5742. const unsigned int time_diff = (unsigned int)(*p_timer + milliseconds - current_time);
  5743. *p_timer = current_time + time_diff;
  5744. cimg::sleep(time_diff);
  5745. return time_diff;
  5746. }
  5747. //! Wait for a given number of milliseconds since the last call to wait().
  5748. /**
  5749. \param milliseconds Number of milliseconds to wait for.
  5750. \return Number of milliseconds elapsed since the last call to wait().
  5751. \note Same as sleep() with a waiting time computed with regard to the last call
  5752. of wait(). It may be used to temporize your program properly, without wasting CPU time.
  5753. **/
  5754. inline unsigned int wait(const unsigned int milliseconds) {
  5755. cimg::mutex(3);
  5756. static cimg_uint64 timer = cimg::time();
  5757. cimg::mutex(3,0);
  5758. return cimg::wait(milliseconds,&timer);
  5759. }
  5760. // Custom random number generator (allow re-entrance).
  5761. inline cimg_uint64& rng() { // Used as a shared global number for rng
  5762. static cimg_uint64 rng = 0xB16B00B5U;
  5763. return rng;
  5764. }
  5765. inline unsigned int _rand(cimg_uint64 *const p_rng) {
  5766. *p_rng = *p_rng*1103515245 + 12345U;
  5767. return (unsigned int)*p_rng;
  5768. }
  5769. inline unsigned int _rand() {
  5770. cimg::mutex(4);
  5771. const unsigned int res = cimg::_rand(&cimg::rng());
  5772. cimg::mutex(4,0);
  5773. return res;
  5774. }
  5775. inline void srand(cimg_uint64 *const p_rng) {
  5776. #if cimg_OS==1
  5777. *p_rng = cimg::time() + (cimg_uint64)getpid();
  5778. #elif cimg_OS==2
  5779. *p_rng = cimg::time() + (cimg_uint64)_getpid();
  5780. #endif
  5781. }
  5782. inline void srand() {
  5783. cimg::mutex(4);
  5784. cimg::srand(&cimg::rng());
  5785. cimg::mutex(4,0);
  5786. }
  5787. inline void srand(const cimg_uint64 seed) {
  5788. cimg::mutex(4);
  5789. cimg::rng() = seed;
  5790. cimg::mutex(4,0);
  5791. }
  5792. inline double rand(const double val_min, const double val_max, cimg_uint64 *const p_rng) {
  5793. const double val = cimg::_rand(p_rng)/(double)~0U;
  5794. return val_min + (val_max - val_min)*val;
  5795. }
  5796. inline double rand(const double val_min, const double val_max) {
  5797. cimg::mutex(4);
  5798. const double res = cimg::rand(val_min,val_max,&cimg::rng());
  5799. cimg::mutex(4,0);
  5800. return res;
  5801. }
  5802. inline double rand(const double val_max, cimg_uint64 *const p_rng) {
  5803. const double val = cimg::_rand(p_rng)/(double)~0U;
  5804. return val_max*val;
  5805. }
  5806. inline double rand(const double val_max=1) {
  5807. cimg::mutex(4);
  5808. const double res = cimg::rand(val_max,&cimg::rng());
  5809. cimg::mutex(4,0);
  5810. return res;
  5811. }
  5812. inline double grand(cimg_uint64 *const p_rng) {
  5813. double x1, w;
  5814. do {
  5815. const double x2 = cimg::rand(-1,1,p_rng);
  5816. x1 = cimg::rand(-1,1,p_rng);
  5817. w = x1*x1 + x2*x2;
  5818. } while (w<=0 || w>=1.);
  5819. return x1*std::sqrt((-2*std::log(w))/w);
  5820. }
  5821. inline double grand() {
  5822. cimg::mutex(4);
  5823. const double res = cimg::grand(&cimg::rng());
  5824. cimg::mutex(4,0);
  5825. return res;
  5826. }
  5827. inline unsigned int prand(const double z, cimg_uint64 *const p_rng) {
  5828. if (z<=1.e-10) return 0;
  5829. if (z>100) return (unsigned int)((std::sqrt(z) * cimg::grand(p_rng)) + z);
  5830. unsigned int k = 0;
  5831. const double y = std::exp(-z);
  5832. for (double s = 1.; s>=y; ++k) s*=cimg::rand(1,p_rng);
  5833. return k - 1;
  5834. }
  5835. inline unsigned int prand(const double z) {
  5836. cimg::mutex(4);
  5837. const unsigned int res = cimg::prand(z,&cimg::rng());
  5838. cimg::mutex(4,0);
  5839. return res;
  5840. }
  5841. //! Cut (i.e. clamp) value in specified interval.
  5842. template<typename T, typename t>
  5843. inline T cut(const T& val, const t& val_min, const t& val_max) {
  5844. return val<=val_min?(T)val_min:val>=val_max?(T)val_max:val;
  5845. }
  5846. //! Bitwise-rotate value on the left.
  5847. template<typename T>
  5848. inline T rol(const T& a, const unsigned int n=1) {
  5849. return n?(T)((a<<n)|(a>>((sizeof(T)<<3) - n))):a;
  5850. }
  5851. inline float rol(const float a, const unsigned int n=1) {
  5852. return (float)rol((int)a,n);
  5853. }
  5854. inline double rol(const double a, const unsigned int n=1) {
  5855. return (double)rol((cimg_long)a,n);
  5856. }
  5857. inline double rol(const long double a, const unsigned int n=1) {
  5858. return (double)rol((cimg_long)a,n);
  5859. }
  5860. #ifdef cimg_use_half
  5861. inline half rol(const half a, const unsigned int n=1) {
  5862. return (half)rol((int)a,n);
  5863. }
  5864. #endif
  5865. //! Bitwise-rotate value on the right.
  5866. template<typename T>
  5867. inline T ror(const T& a, const unsigned int n=1) {
  5868. return n?(T)((a>>n)|(a<<((sizeof(T)<<3) - n))):a;
  5869. }
  5870. inline float ror(const float a, const unsigned int n=1) {
  5871. return (float)ror((int)a,n);
  5872. }
  5873. inline double ror(const double a, const unsigned int n=1) {
  5874. return (double)ror((cimg_long)a,n);
  5875. }
  5876. inline double ror(const long double a, const unsigned int n=1) {
  5877. return (double)ror((cimg_long)a,n);
  5878. }
  5879. #ifdef cimg_use_half
  5880. inline half ror(const half a, const unsigned int n=1) {
  5881. return (half)ror((int)a,n);
  5882. }
  5883. #endif
  5884. //! Return absolute value of a value.
  5885. template<typename T>
  5886. inline T abs(const T& a) {
  5887. return a>=0?a:-a;
  5888. }
  5889. inline bool abs(const bool a) {
  5890. return a;
  5891. }
  5892. inline int abs(const unsigned char a) {
  5893. return (int)a;
  5894. }
  5895. inline int abs(const unsigned short a) {
  5896. return (int)a;
  5897. }
  5898. inline int abs(const unsigned int a) {
  5899. return (int)a;
  5900. }
  5901. inline int abs(const int a) {
  5902. return std::abs(a);
  5903. }
  5904. inline cimg_int64 abs(const cimg_uint64 a) {
  5905. return (cimg_int64)a;
  5906. }
  5907. inline double abs(const double a) {
  5908. return std::fabs(a);
  5909. }
  5910. inline float abs(const float a) {
  5911. return (float)std::fabs((double)a);
  5912. }
  5913. //! Return hyperbolic arcosine of a value.
  5914. inline double acosh(const double x) {
  5915. #if cimg_use_cpp11==1 && !defined(_MSC_VER)
  5916. return std::acosh(x);
  5917. #else
  5918. return std::log(x + std::sqrt(x*x - 1));
  5919. #endif
  5920. }
  5921. //! Return hyperbolic arcsine of a value.
  5922. inline double asinh(const double x) {
  5923. #if cimg_use_cpp11==1 && !defined(_MSC_VER)
  5924. return std::asinh(x);
  5925. #else
  5926. return std::log(x + std::sqrt(x*x + 1));
  5927. #endif
  5928. }
  5929. //! Return hyperbolic arctangent of a value.
  5930. inline double atanh(const double x) {
  5931. #if cimg_use_cpp11==1 && !defined(_MSC_VER)
  5932. return std::atanh(x);
  5933. #else
  5934. return 0.5*std::log((1. + x)/(1. - x));
  5935. #endif
  5936. }
  5937. //! Return the sinc of a given value.
  5938. inline double sinc(const double x) {
  5939. return x?std::sin(x)/x:1;
  5940. }
  5941. //! Return base-2 logarithm of a value.
  5942. inline double log2(const double x) {
  5943. #if cimg_use_cpp11==1 && !defined(_MSC_VER)
  5944. return std::log2(x);
  5945. #else
  5946. const double base2 = std::log(2.);
  5947. return std::log(x)/base2;
  5948. #endif
  5949. }
  5950. //! Return square of a value.
  5951. template<typename T>
  5952. inline T sqr(const T& val) {
  5953. return val*val;
  5954. }
  5955. // Return inverse of error function.
  5956. template<typename T>
  5957. inline T erfinv(const T& val) {
  5958. const T
  5959. sgn = val<0?-1:1,
  5960. x = (1 - val)*(1 + val),
  5961. lnx = std::log(x),
  5962. tt1 = (T)(2/(cimg::PI*0.147) + 0.5*lnx),
  5963. tt2 = lnx/(T)0.147;
  5964. return sgn*std::sqrt(-tt1 + std::sqrt(tt1*tt1 - tt2));
  5965. }
  5966. //! Return cubic root of a value.
  5967. template<typename T>
  5968. inline double cbrt(const T& x) {
  5969. #if cimg_use_cpp11==1
  5970. return std::cbrt(x);
  5971. #else
  5972. return x>=0?std::pow((double)x,1./3):-std::pow(-(double)x,1./3);
  5973. #endif
  5974. }
  5975. template<typename T>
  5976. inline T pow3(const T& val) {
  5977. return val*val*val;
  5978. }
  5979. template<typename T>
  5980. inline T pow4(const T& val) {
  5981. return val*val*val*val;
  5982. }
  5983. //! Return the minimum between three values.
  5984. template<typename t>
  5985. inline t min(const t& a, const t& b, const t& c) {
  5986. return std::min(std::min(a,b),c);
  5987. }
  5988. //! Return the minimum between four values.
  5989. template<typename t>
  5990. inline t min(const t& a, const t& b, const t& c, const t& d) {
  5991. return std::min(std::min(a,b),std::min(c,d));
  5992. }
  5993. //! Return the minabs between two values.
  5994. template<typename t>
  5995. inline t minabs(const t& a, const t& b) {
  5996. return cimg::abs(b)<cimg::abs(a)?b:a;
  5997. }
  5998. template<typename t>
  5999. inline t minabs(const t& a, const t& b, const t& abs_b) {
  6000. return abs_b<cimg::abs(a)?b:a;
  6001. }
  6002. //! Return the maximum between three values.
  6003. template<typename t>
  6004. inline t max(const t& a, const t& b, const t& c) {
  6005. return std::max(std::max(a,b),c);
  6006. }
  6007. //! Return the maximum between four values.
  6008. template<typename t>
  6009. inline t max(const t& a, const t& b, const t& c, const t& d) {
  6010. return std::max(std::max(a,b),std::max(c,d));
  6011. }
  6012. //! Return the maxabs between two values.
  6013. template<typename t>
  6014. inline t maxabs(const t& a, const t& b) {
  6015. return cimg::abs(b)>cimg::abs(a)?b:a;
  6016. }
  6017. template<typename t>
  6018. inline t maxabs(const t& a, const t& b, const t& abs_b) {
  6019. return abs_b>cimg::abs(a)?b:a;
  6020. }
  6021. //! Return the sign of a value.
  6022. template<typename T>
  6023. inline T sign(const T& x) {
  6024. return (T)(cimg::type<T>::is_nan(x)?0:x<0?-1:x>0);
  6025. }
  6026. //! Return the nearest power of 2 higher than given value.
  6027. template<typename T>
  6028. inline cimg_uint64 nearest_pow2(const T& x) {
  6029. cimg_uint64 i = 1;
  6030. while (x>i) i<<=1;
  6031. return i;
  6032. }
  6033. //! Return the modulo of a value.
  6034. /**
  6035. \param x Input value.
  6036. \param m Modulo value.
  6037. \note This modulo function accepts negative and floating-points modulo numbers, as well as variables of any type.
  6038. **/
  6039. template<typename T>
  6040. inline T mod(const T& x, const T& m) {
  6041. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6042. const double dx = (double)x, dm = (double)m;
  6043. if (!cimg::type<double>::is_finite(dm)) return x;
  6044. if (cimg::type<double>::is_finite(dx)) return (T)(dx - dm * std::floor(dx / dm));
  6045. return (T)0;
  6046. }
  6047. inline int mod(const bool x, const bool m) {
  6048. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6049. return x?1:0;
  6050. }
  6051. inline int mod(const unsigned char x, const unsigned char m) {
  6052. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6053. return x%m;
  6054. }
  6055. inline int mod(const char x, const char m) {
  6056. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6057. #if defined(CHAR_MAX) && CHAR_MAX==255
  6058. return x%m;
  6059. #else
  6060. return x>=0?x%m:(x%m?m + x%m:0);
  6061. #endif
  6062. }
  6063. inline int mod(const unsigned short x, const unsigned short m) {
  6064. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6065. return (int)(x%m);
  6066. }
  6067. inline int mod(const short x, const short m) {
  6068. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6069. return (int)(x>=0?x%m:(x%m?m + x%m:0));
  6070. }
  6071. inline int mod(const unsigned int x, const unsigned int m) {
  6072. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6073. return (int)(x%m);
  6074. }
  6075. inline int mod(const int x, const int m) {
  6076. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6077. return (int)(x>=0?x%m:(x%m?m + x%m:0));
  6078. }
  6079. inline cimg_int64 mod(const cimg_uint64 x, const cimg_uint64 m) {
  6080. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6081. return (cimg_int64)(x%m);
  6082. }
  6083. inline cimg_int64 mod(const cimg_int64 x, const cimg_int64 m) {
  6084. if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
  6085. return (cimg_int64)(x>=0?x%m:(x%m?m + x%m:0));
  6086. }
  6087. //! Return the min-mod of two values.
  6088. /**
  6089. \note <i>minmod(\p a,\p b)</i> is defined to be:
  6090. - <i>minmod(\p a,\p b) = min(\p a,\p b)</i>, if \p a and \p b have the same sign.
  6091. - <i>minmod(\p a,\p b) = 0</i>, if \p a and \p b have different signs.
  6092. **/
  6093. template<typename T>
  6094. inline T minmod(const T& a, const T& b) {
  6095. return a*b<=0?0:(a>0?(a<b?a:b):(a<b?b:a));
  6096. }
  6097. template<typename T>
  6098. inline T round(const T& x) {
  6099. return (T)std::floor((_cimg_Tfloat)x + 0.5f);
  6100. }
  6101. template<typename T>
  6102. inline int uiround(const T x) {
  6103. return cimg::type<T>::is_float()?(int)(x + 0.5f):(int)x;
  6104. }
  6105. //! Return rounded value.
  6106. /**
  6107. \param x Value to be rounded.
  6108. \param y Rounding precision.
  6109. \param rounding_type Type of rounding operation (\c 0 = nearest, \c -1 = backward, \c 1 = forward).
  6110. \return Rounded value, having the same type as input value \c x.
  6111. **/
  6112. template<typename T>
  6113. inline T round(const T& x, const double y, const int rounding_type=0) {
  6114. if (y<=0) return x;
  6115. if (y==1) switch (rounding_type) {
  6116. case 0 : return cimg::round(x);
  6117. case 1 : return (T)std::ceil((_cimg_Tfloat)x);
  6118. default : return (T)std::floor((_cimg_Tfloat)x);
  6119. }
  6120. const double sx = (double)x/y, floor = std::floor(sx), delta = sx - floor;
  6121. return (T)(y*(rounding_type<0?floor:rounding_type>0?std::ceil(sx):delta<0.5?floor:std::ceil(sx)));
  6122. }
  6123. // Code to compute fast median from 2,3,5,7,9,13,25 and 49 values.
  6124. // (contribution by RawTherapee: http://rawtherapee.com/).
  6125. template<typename T>
  6126. inline T median(T val0, T val1) {
  6127. return (val0 + val1)/2;
  6128. }
  6129. template<typename T>
  6130. inline T median(T val0, T val1, T val2) {
  6131. return std::max(std::min(val0,val1),std::min(val2,std::max(val0,val1)));
  6132. }
  6133. template<typename T>
  6134. inline T median(T val0, T val1, T val2, T val3, T val4) {
  6135. T tmp = std::min(val0,val1);
  6136. val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4);
  6137. val3 = std::max(val0,tmp); val1 = std::min(val1,val4); tmp = std::min(val1,val2); val2 = std::max(val1,val2);
  6138. val1 = tmp; tmp = std::min(val2,val3);
  6139. return std::max(val1,tmp);
  6140. }
  6141. template<typename T>
  6142. inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6) {
  6143. T tmp = std::min(val0,val5);
  6144. val5 = std::max(val0,val5); val0 = tmp; tmp = std::min(val0,val3); val3 = std::max(val0,val3); val0 = tmp;
  6145. tmp = std::min(val1,val6); val6 = std::max(val1,val6); val1 = tmp; tmp = std::min(val2,val4);
  6146. val4 = std::max(val2,val4); val2 = tmp; val1 = std::max(val0,val1); tmp = std::min(val3,val5);
  6147. val5 = std::max(val3,val5); val3 = tmp; tmp = std::min(val2,val6); val6 = std::max(val2,val6);
  6148. val3 = std::max(tmp,val3); val3 = std::min(val3,val6); tmp = std::min(val4,val5); val4 = std::max(val1,tmp);
  6149. tmp = std::min(val1,tmp); val3 = std::max(tmp,val3);
  6150. return std::min(val3,val4);
  6151. }
  6152. template<typename T>
  6153. inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6, T val7, T val8) {
  6154. T tmp = std::min(val1,val2);
  6155. val2 = std::max(val1,val2); val1 = tmp; tmp = std::min(val4,val5);
  6156. val5 = std::max(val4,val5); val4 = tmp; tmp = std::min(val7,val8);
  6157. val8 = std::max(val7,val8); val7 = tmp; tmp = std::min(val0,val1);
  6158. val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4);
  6159. val4 = std::max(val3,val4); val3 = tmp; tmp = std::min(val6,val7);
  6160. val7 = std::max(val6,val7); val6 = tmp; tmp = std::min(val1,val2);
  6161. val2 = std::max(val1,val2); val1 = tmp; tmp = std::min(val4,val5);
  6162. val5 = std::max(val4,val5); val4 = tmp; tmp = std::min(val7,val8);
  6163. val8 = std::max(val7,val8); val3 = std::max(val0,val3); val5 = std::min(val5,val8);
  6164. val7 = std::max(val4,tmp); tmp = std::min(val4,tmp); val6 = std::max(val3,val6);
  6165. val4 = std::max(val1,tmp); val2 = std::min(val2,val5); val4 = std::min(val4,val7);
  6166. tmp = std::min(val4,val2); val2 = std::max(val4,val2); val4 = std::max(val6,tmp);
  6167. return std::min(val4,val2);
  6168. }
  6169. template<typename T>
  6170. inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6, T val7, T val8, T val9, T val10, T val11,
  6171. T val12) {
  6172. T tmp = std::min(val1,val7);
  6173. val7 = std::max(val1,val7); val1 = tmp; tmp = std::min(val9,val11); val11 = std::max(val9,val11); val9 = tmp;
  6174. tmp = std::min(val3,val4); val4 = std::max(val3,val4); val3 = tmp; tmp = std::min(val5,val8);
  6175. val8 = std::max(val5,val8); val5 = tmp; tmp = std::min(val0,val12); val12 = std::max(val0,val12);
  6176. val0 = tmp; tmp = std::min(val2,val6); val6 = std::max(val2,val6); val2 = tmp; tmp = std::min(val0,val1);
  6177. val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val2,val3); val3 = std::max(val2,val3); val2 = tmp;
  6178. tmp = std::min(val4,val6); val6 = std::max(val4,val6); val4 = tmp; tmp = std::min(val8,val11);
  6179. val11 = std::max(val8,val11); val8 = tmp; tmp = std::min(val7,val12); val12 = std::max(val7,val12); val7 = tmp;
  6180. tmp = std::min(val5,val9); val9 = std::max(val5,val9); val5 = tmp; tmp = std::min(val0,val2);
  6181. val2 = std::max(val0,val2); val0 = tmp; tmp = std::min(val3,val7); val7 = std::max(val3,val7); val3 = tmp;
  6182. tmp = std::min(val10,val11); val11 = std::max(val10,val11); val10 = tmp; tmp = std::min(val1,val4);
  6183. val4 = std::max(val1,val4); val1 = tmp; tmp = std::min(val6,val12); val12 = std::max(val6,val12); val6 = tmp;
  6184. tmp = std::min(val7,val8); val8 = std::max(val7,val8); val7 = tmp; val11 = std::min(val11,val12);
  6185. tmp = std::min(val4,val9); val9 = std::max(val4,val9); val4 = tmp; tmp = std::min(val6,val10);
  6186. val10 = std::max(val6,val10); val6 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4); val3 = tmp;
  6187. tmp = std::min(val5,val6); val6 = std::max(val5,val6); val5 = tmp; val8 = std::min(val8,val9);
  6188. val10 = std::min(val10,val11); tmp = std::min(val1,val7); val7 = std::max(val1,val7); val1 = tmp;
  6189. tmp = std::min(val2,val6); val6 = std::max(val2,val6); val2 = tmp; val3 = std::max(val1,val3);
  6190. tmp = std::min(val4,val7); val7 = std::max(val4,val7); val4 = tmp; val8 = std::min(val8,val10);
  6191. val5 = std::max(val0,val5); val5 = std::max(val2,val5); tmp = std::min(val6,val8); val8 = std::max(val6,val8);
  6192. val5 = std::max(val3,val5); val7 = std::min(val7,val8); val6 = std::max(val4,tmp); tmp = std::min(val4,tmp);
  6193. val5 = std::max(tmp,val5); val6 = std::min(val6,val7);
  6194. return std::max(val5,val6);
  6195. }
  6196. template<typename T>
  6197. inline T median(T val0, T val1, T val2, T val3, T val4,
  6198. T val5, T val6, T val7, T val8, T val9,
  6199. T val10, T val11, T val12, T val13, T val14,
  6200. T val15, T val16, T val17, T val18, T val19,
  6201. T val20, T val21, T val22, T val23, T val24) {
  6202. T tmp = std::min(val0,val1);
  6203. val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4);
  6204. val3 = tmp; tmp = std::min(val2,val4); val4 = std::max(val2,val4); val2 = std::min(tmp,val3);
  6205. val3 = std::max(tmp,val3); tmp = std::min(val6,val7); val7 = std::max(val6,val7); val6 = tmp;
  6206. tmp = std::min(val5,val7); val7 = std::max(val5,val7); val5 = std::min(tmp,val6); val6 = std::max(tmp,val6);
  6207. tmp = std::min(val9,val10); val10 = std::max(val9,val10); val9 = tmp; tmp = std::min(val8,val10);
  6208. val10 = std::max(val8,val10); val8 = std::min(tmp,val9); val9 = std::max(tmp,val9);
  6209. tmp = std::min(val12,val13); val13 = std::max(val12,val13); val12 = tmp; tmp = std::min(val11,val13);
  6210. val13 = std::max(val11,val13); val11 = std::min(tmp,val12); val12 = std::max(tmp,val12);
  6211. tmp = std::min(val15,val16); val16 = std::max(val15,val16); val15 = tmp; tmp = std::min(val14,val16);
  6212. val16 = std::max(val14,val16); val14 = std::min(tmp,val15); val15 = std::max(tmp,val15);
  6213. tmp = std::min(val18,val19); val19 = std::max(val18,val19); val18 = tmp; tmp = std::min(val17,val19);
  6214. val19 = std::max(val17,val19); val17 = std::min(tmp,val18); val18 = std::max(tmp,val18);
  6215. tmp = std::min(val21,val22); val22 = std::max(val21,val22); val21 = tmp; tmp = std::min(val20,val22);
  6216. val22 = std::max(val20,val22); val20 = std::min(tmp,val21); val21 = std::max(tmp,val21);
  6217. tmp = std::min(val23,val24); val24 = std::max(val23,val24); val23 = tmp; tmp = std::min(val2,val5);
  6218. val5 = std::max(val2,val5); val2 = tmp; tmp = std::min(val3,val6); val6 = std::max(val3,val6); val3 = tmp;
  6219. tmp = std::min(val0,val6); val6 = std::max(val0,val6); val0 = std::min(tmp,val3); val3 = std::max(tmp,val3);
  6220. tmp = std::min(val4,val7); val7 = std::max(val4,val7); val4 = tmp; tmp = std::min(val1,val7);
  6221. val7 = std::max(val1,val7); val1 = std::min(tmp,val4); val4 = std::max(tmp,val4); tmp = std::min(val11,val14);
  6222. val14 = std::max(val11,val14); val11 = tmp; tmp = std::min(val8,val14); val14 = std::max(val8,val14);
  6223. val8 = std::min(tmp,val11); val11 = std::max(tmp,val11); tmp = std::min(val12,val15);
  6224. val15 = std::max(val12,val15); val12 = tmp; tmp = std::min(val9,val15); val15 = std::max(val9,val15);
  6225. val9 = std::min(tmp,val12); val12 = std::max(tmp,val12); tmp = std::min(val13,val16);
  6226. val16 = std::max(val13,val16); val13 = tmp; tmp = std::min(val10,val16); val16 = std::max(val10,val16);
  6227. val10 = std::min(tmp,val13); val13 = std::max(tmp,val13); tmp = std::min(val20,val23);
  6228. val23 = std::max(val20,val23); val20 = tmp; tmp = std::min(val17,val23); val23 = std::max(val17,val23);
  6229. val17 = std::min(tmp,val20); val20 = std::max(tmp,val20); tmp = std::min(val21,val24);
  6230. val24 = std::max(val21,val24); val21 = tmp; tmp = std::min(val18,val24); val24 = std::max(val18,val24);
  6231. val18 = std::min(tmp,val21); val21 = std::max(tmp,val21); tmp = std::min(val19,val22);
  6232. val22 = std::max(val19,val22); val19 = tmp; val17 = std::max(val8,val17); tmp = std::min(val9,val18);
  6233. val18 = std::max(val9,val18); val9 = tmp; tmp = std::min(val0,val18); val18 = std::max(val0,val18);
  6234. val9 = std::max(tmp,val9); tmp = std::min(val10,val19); val19 = std::max(val10,val19); val10 = tmp;
  6235. tmp = std::min(val1,val19); val19 = std::max(val1,val19); val1 = std::min(tmp,val10);
  6236. val10 = std::max(tmp,val10); tmp = std::min(val11,val20); val20 = std::max(val11,val20); val11 = tmp;
  6237. tmp = std::min(val2,val20); val20 = std::max(val2,val20); val11 = std::max(tmp,val11);
  6238. tmp = std::min(val12,val21); val21 = std::max(val12,val21); val12 = tmp; tmp = std::min(val3,val21);
  6239. val21 = std::max(val3,val21); val3 = std::min(tmp,val12); val12 = std::max(tmp,val12);
  6240. tmp = std::min(val13,val22); val22 = std::max(val13,val22); val4 = std::min(val4,val22);
  6241. val13 = std::max(val4,tmp); tmp = std::min(val4,tmp); val4 = tmp; tmp = std::min(val14,val23);
  6242. val23 = std::max(val14,val23); val14 = tmp; tmp = std::min(val5,val23); val23 = std::max(val5,val23);
  6243. val5 = std::min(tmp,val14); val14 = std::max(tmp,val14); tmp = std::min(val15,val24);
  6244. val24 = std::max(val15,val24); val15 = tmp; val6 = std::min(val6,val24); tmp = std::min(val6,val15);
  6245. val15 = std::max(val6,val15); val6 = tmp; tmp = std::min(val7,val16); val7 = std::min(tmp,val19);
  6246. tmp = std::min(val13,val21); val15 = std::min(val15,val23); tmp = std::min(val7,tmp);
  6247. val7 = std::min(tmp,val15); val9 = std::max(val1,val9); val11 = std::max(val3,val11);
  6248. val17 = std::max(val5,val17); val17 = std::max(val11,val17); val17 = std::max(val9,val17);
  6249. tmp = std::min(val4,val10); val10 = std::max(val4,val10); val4 = tmp; tmp = std::min(val6,val12);
  6250. val12 = std::max(val6,val12); val6 = tmp; tmp = std::min(val7,val14); val14 = std::max(val7,val14);
  6251. val7 = tmp; tmp = std::min(val4,val6); val6 = std::max(val4,val6); val7 = std::max(tmp,val7);
  6252. tmp = std::min(val12,val14); val14 = std::max(val12,val14); val12 = tmp; val10 = std::min(val10,val14);
  6253. tmp = std::min(val6,val7); val7 = std::max(val6,val7); val6 = tmp; tmp = std::min(val10,val12);
  6254. val12 = std::max(val10,val12); val10 = std::max(val6,tmp); tmp = std::min(val6,tmp);
  6255. val17 = std::max(tmp,val17); tmp = std::min(val12,val17); val17 = std::max(val12,val17); val12 = tmp;
  6256. val7 = std::min(val7,val17); tmp = std::min(val7,val10); val10 = std::max(val7,val10); val7 = tmp;
  6257. tmp = std::min(val12,val18); val18 = std::max(val12,val18); val12 = std::max(val7,tmp);
  6258. val10 = std::min(val10,val18); tmp = std::min(val12,val20); val20 = std::max(val12,val20); val12 = tmp;
  6259. tmp = std::min(val10,val20);
  6260. return std::max(tmp,val12);
  6261. }
  6262. template<typename T>
  6263. inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6,
  6264. T val7, T val8, T val9, T val10, T val11, T val12, T val13,
  6265. T val14, T val15, T val16, T val17, T val18, T val19, T val20,
  6266. T val21, T val22, T val23, T val24, T val25, T val26, T val27,
  6267. T val28, T val29, T val30, T val31, T val32, T val33, T val34,
  6268. T val35, T val36, T val37, T val38, T val39, T val40, T val41,
  6269. T val42, T val43, T val44, T val45, T val46, T val47, T val48) {
  6270. T tmp = std::min(val0,val32);
  6271. val32 = std::max(val0,val32); val0 = tmp; tmp = std::min(val1,val33); val33 = std::max(val1,val33); val1 = tmp;
  6272. tmp = std::min(val2,val34); val34 = std::max(val2,val34); val2 = tmp; tmp = std::min(val3,val35);
  6273. val35 = std::max(val3,val35); val3 = tmp; tmp = std::min(val4,val36); val36 = std::max(val4,val36); val4 = tmp;
  6274. tmp = std::min(val5,val37); val37 = std::max(val5,val37); val5 = tmp; tmp = std::min(val6,val38);
  6275. val38 = std::max(val6,val38); val6 = tmp; tmp = std::min(val7,val39); val39 = std::max(val7,val39); val7 = tmp;
  6276. tmp = std::min(val8,val40); val40 = std::max(val8,val40); val8 = tmp; tmp = std::min(val9,val41);
  6277. val41 = std::max(val9,val41); val9 = tmp; tmp = std::min(val10,val42); val42 = std::max(val10,val42);
  6278. val10 = tmp; tmp = std::min(val11,val43); val43 = std::max(val11,val43); val11 = tmp;
  6279. tmp = std::min(val12,val44); val44 = std::max(val12,val44); val12 = tmp; tmp = std::min(val13,val45);
  6280. val45 = std::max(val13,val45); val13 = tmp; tmp = std::min(val14,val46); val46 = std::max(val14,val46);
  6281. val14 = tmp; tmp = std::min(val15,val47); val47 = std::max(val15,val47); val15 = tmp;
  6282. tmp = std::min(val16,val48); val48 = std::max(val16,val48); val16 = tmp; tmp = std::min(val0,val16);
  6283. val16 = std::max(val0,val16); val0 = tmp; tmp = std::min(val1,val17); val17 = std::max(val1,val17);
  6284. val1 = tmp; tmp = std::min(val2,val18); val18 = std::max(val2,val18); val2 = tmp; tmp = std::min(val3,val19);
  6285. val19 = std::max(val3,val19); val3 = tmp; tmp = std::min(val4,val20); val20 = std::max(val4,val20); val4 = tmp;
  6286. tmp = std::min(val5,val21); val21 = std::max(val5,val21); val5 = tmp; tmp = std::min(val6,val22);
  6287. val22 = std::max(val6,val22); val6 = tmp; tmp = std::min(val7,val23); val23 = std::max(val7,val23); val7 = tmp;
  6288. tmp = std::min(val8,val24); val24 = std::max(val8,val24); val8 = tmp; tmp = std::min(val9,val25);
  6289. val25 = std::max(val9,val25); val9 = tmp; tmp = std::min(val10,val26); val26 = std::max(val10,val26);
  6290. val10 = tmp; tmp = std::min(val11,val27); val27 = std::max(val11,val27); val11 = tmp;
  6291. tmp = std::min(val12,val28); val28 = std::max(val12,val28); val12 = tmp; tmp = std::min(val13,val29);
  6292. val29 = std::max(val13,val29); val13 = tmp; tmp = std::min(val14,val30); val30 = std::max(val14,val30);
  6293. val14 = tmp; tmp = std::min(val15,val31); val31 = std::max(val15,val31); val15 = tmp;
  6294. tmp = std::min(val32,val48); val48 = std::max(val32,val48); val32 = tmp; tmp = std::min(val16,val32);
  6295. val32 = std::max(val16,val32); val16 = tmp; tmp = std::min(val17,val33); val33 = std::max(val17,val33);
  6296. val17 = tmp; tmp = std::min(val18,val34); val34 = std::max(val18,val34); val18 = tmp;
  6297. tmp = std::min(val19,val35); val35 = std::max(val19,val35); val19 = tmp; tmp = std::min(val20,val36);
  6298. val36 = std::max(val20,val36); val20 = tmp; tmp = std::min(val21,val37); val37 = std::max(val21,val37);
  6299. val21 = tmp; tmp = std::min(val22,val38); val38 = std::max(val22,val38); val22 = tmp;
  6300. tmp = std::min(val23,val39); val39 = std::max(val23,val39); val23 = tmp; tmp = std::min(val24,val40);
  6301. val40 = std::max(val24,val40); val24 = tmp; tmp = std::min(val25,val41); val41 = std::max(val25,val41);
  6302. val25 = tmp; tmp = std::min(val26,val42); val42 = std::max(val26,val42); val26 = tmp;
  6303. tmp = std::min(val27,val43); val43 = std::max(val27,val43); val27 = tmp; tmp = std::min(val28,val44);
  6304. val44 = std::max(val28,val44); val28 = tmp; tmp = std::min(val29,val45); val45 = std::max(val29,val45);
  6305. val29 = tmp; tmp = std::min(val30,val46); val46 = std::max(val30,val46); val30 = tmp;
  6306. tmp = std::min(val31,val47); val47 = std::max(val31,val47); val31 = tmp; tmp = std::min(val0,val8);
  6307. val8 = std::max(val0,val8); val0 = tmp; tmp = std::min(val1,val9); val9 = std::max(val1,val9); val1 = tmp;
  6308. tmp = std::min(val2,val10); val10 = std::max(val2,val10); val2 = tmp; tmp = std::min(val3,val11);
  6309. val11 = std::max(val3,val11); val3 = tmp; tmp = std::min(val4,val12); val12 = std::max(val4,val12); val4 = tmp;
  6310. tmp = std::min(val5,val13); val13 = std::max(val5,val13); val5 = tmp; tmp = std::min(val6,val14);
  6311. val14 = std::max(val6,val14); val6 = tmp; tmp = std::min(val7,val15); val15 = std::max(val7,val15); val7 = tmp;
  6312. tmp = std::min(val16,val24); val24 = std::max(val16,val24); val16 = tmp; tmp = std::min(val17,val25);
  6313. val25 = std::max(val17,val25); val17 = tmp; tmp = std::min(val18,val26); val26 = std::max(val18,val26);
  6314. val18 = tmp; tmp = std::min(val19,val27); val27 = std::max(val19,val27); val19 = tmp;
  6315. tmp = std::min(val20,val28); val28 = std::max(val20,val28); val20 = tmp; tmp = std::min(val21,val29);
  6316. val29 = std::max(val21,val29); val21 = tmp; tmp = std::min(val22,val30); val30 = std::max(val22,val30);
  6317. val22 = tmp; tmp = std::min(val23,val31); val31 = std::max(val23,val31); val23 = tmp;
  6318. tmp = std::min(val32,val40); val40 = std::max(val32,val40); val32 = tmp; tmp = std::min(val33,val41);
  6319. val41 = std::max(val33,val41); val33 = tmp; tmp = std::min(val34,val42); val42 = std::max(val34,val42);
  6320. val34 = tmp; tmp = std::min(val35,val43); val43 = std::max(val35,val43); val35 = tmp;
  6321. tmp = std::min(val36,val44); val44 = std::max(val36,val44); val36 = tmp; tmp = std::min(val37,val45);
  6322. val45 = std::max(val37,val45); val37 = tmp; tmp = std::min(val38,val46); val46 = std::max(val38,val46);
  6323. val38 = tmp; tmp = std::min(val39,val47); val47 = std::max(val39,val47); val39 = tmp;
  6324. tmp = std::min(val8,val32); val32 = std::max(val8,val32); val8 = tmp; tmp = std::min(val9,val33);
  6325. val33 = std::max(val9,val33); val9 = tmp; tmp = std::min(val10,val34); val34 = std::max(val10,val34);
  6326. val10 = tmp; tmp = std::min(val11,val35); val35 = std::max(val11,val35); val11 = tmp;
  6327. tmp = std::min(val12,val36); val36 = std::max(val12,val36); val12 = tmp; tmp = std::min(val13,val37);
  6328. val37 = std::max(val13,val37); val13 = tmp; tmp = std::min(val14,val38); val38 = std::max(val14,val38);
  6329. val14 = tmp; tmp = std::min(val15,val39); val39 = std::max(val15,val39); val15 = tmp;
  6330. tmp = std::min(val24,val48); val48 = std::max(val24,val48); val24 = tmp; tmp = std::min(val8,val16);
  6331. val16 = std::max(val8,val16); val8 = tmp; tmp = std::min(val9,val17); val17 = std::max(val9,val17);
  6332. val9 = tmp; tmp = std::min(val10,val18); val18 = std::max(val10,val18); val10 = tmp;
  6333. tmp = std::min(val11,val19); val19 = std::max(val11,val19); val11 = tmp; tmp = std::min(val12,val20);
  6334. val20 = std::max(val12,val20); val12 = tmp; tmp = std::min(val13,val21); val21 = std::max(val13,val21);
  6335. val13 = tmp; tmp = std::min(val14,val22); val22 = std::max(val14,val22); val14 = tmp;
  6336. tmp = std::min(val15,val23); val23 = std::max(val15,val23); val15 = tmp; tmp = std::min(val24,val32);
  6337. val32 = std::max(val24,val32); val24 = tmp; tmp = std::min(val25,val33); val33 = std::max(val25,val33);
  6338. val25 = tmp; tmp = std::min(val26,val34); val34 = std::max(val26,val34); val26 = tmp;
  6339. tmp = std::min(val27,val35); val35 = std::max(val27,val35); val27 = tmp; tmp = std::min(val28,val36);
  6340. val36 = std::max(val28,val36); val28 = tmp; tmp = std::min(val29,val37); val37 = std::max(val29,val37);
  6341. val29 = tmp; tmp = std::min(val30,val38); val38 = std::max(val30,val38); val30 = tmp;
  6342. tmp = std::min(val31,val39); val39 = std::max(val31,val39); val31 = tmp; tmp = std::min(val40,val48);
  6343. val48 = std::max(val40,val48); val40 = tmp; tmp = std::min(val0,val4); val4 = std::max(val0,val4);
  6344. val0 = tmp; tmp = std::min(val1,val5); val5 = std::max(val1,val5); val1 = tmp; tmp = std::min(val2,val6);
  6345. val6 = std::max(val2,val6); val2 = tmp; tmp = std::min(val3,val7); val7 = std::max(val3,val7); val3 = tmp;
  6346. tmp = std::min(val8,val12); val12 = std::max(val8,val12); val8 = tmp; tmp = std::min(val9,val13);
  6347. val13 = std::max(val9,val13); val9 = tmp; tmp = std::min(val10,val14); val14 = std::max(val10,val14);
  6348. val10 = tmp; tmp = std::min(val11,val15); val15 = std::max(val11,val15); val11 = tmp;
  6349. tmp = std::min(val16,val20); val20 = std::max(val16,val20); val16 = tmp; tmp = std::min(val17,val21);
  6350. val21 = std::max(val17,val21); val17 = tmp; tmp = std::min(val18,val22); val22 = std::max(val18,val22);
  6351. val18 = tmp; tmp = std::min(val19,val23); val23 = std::max(val19,val23); val19 = tmp;
  6352. tmp = std::min(val24,val28); val28 = std::max(val24,val28); val24 = tmp; tmp = std::min(val25,val29);
  6353. val29 = std::max(val25,val29); val25 = tmp; tmp = std::min(val26,val30); val30 = std::max(val26,val30);
  6354. val26 = tmp; tmp = std::min(val27,val31); val31 = std::max(val27,val31); val27 = tmp;
  6355. tmp = std::min(val32,val36); val36 = std::max(val32,val36); val32 = tmp; tmp = std::min(val33,val37);
  6356. val37 = std::max(val33,val37); val33 = tmp; tmp = std::min(val34,val38); val38 = std::max(val34,val38);
  6357. val34 = tmp; tmp = std::min(val35,val39); val39 = std::max(val35,val39); val35 = tmp;
  6358. tmp = std::min(val40,val44); val44 = std::max(val40,val44); val40 = tmp; tmp = std::min(val41,val45);
  6359. val45 = std::max(val41,val45); val41 = tmp; tmp = std::min(val42,val46); val46 = std::max(val42,val46);
  6360. val42 = tmp; tmp = std::min(val43,val47); val47 = std::max(val43,val47); val43 = tmp;
  6361. tmp = std::min(val4,val32); val32 = std::max(val4,val32); val4 = tmp; tmp = std::min(val5,val33);
  6362. val33 = std::max(val5,val33); val5 = tmp; tmp = std::min(val6,val34); val34 = std::max(val6,val34);
  6363. val6 = tmp; tmp = std::min(val7,val35); val35 = std::max(val7,val35); val7 = tmp;
  6364. tmp = std::min(val12,val40); val40 = std::max(val12,val40); val12 = tmp; tmp = std::min(val13,val41);
  6365. val41 = std::max(val13,val41); val13 = tmp; tmp = std::min(val14,val42); val42 = std::max(val14,val42);
  6366. val14 = tmp; tmp = std::min(val15,val43); val43 = std::max(val15,val43); val15 = tmp;
  6367. tmp = std::min(val20,val48); val48 = std::max(val20,val48); val20 = tmp; tmp = std::min(val4,val16);
  6368. val16 = std::max(val4,val16); val4 = tmp; tmp = std::min(val5,val17); val17 = std::max(val5,val17);
  6369. val5 = tmp; tmp = std::min(val6,val18); val18 = std::max(val6,val18); val6 = tmp;
  6370. tmp = std::min(val7,val19); val19 = std::max(val7,val19); val7 = tmp; tmp = std::min(val12,val24);
  6371. val24 = std::max(val12,val24); val12 = tmp; tmp = std::min(val13,val25); val25 = std::max(val13,val25);
  6372. val13 = tmp; tmp = std::min(val14,val26); val26 = std::max(val14,val26); val14 = tmp;
  6373. tmp = std::min(val15,val27); val27 = std::max(val15,val27); val15 = tmp; tmp = std::min(val20,val32);
  6374. val32 = std::max(val20,val32); val20 = tmp; tmp = std::min(val21,val33); val33 = std::max(val21,val33);
  6375. val21 = tmp; tmp = std::min(val22,val34); val34 = std::max(val22,val34); val22 = tmp;
  6376. tmp = std::min(val23,val35); val35 = std::max(val23,val35); val23 = tmp; tmp = std::min(val28,val40);
  6377. val40 = std::max(val28,val40); val28 = tmp; tmp = std::min(val29,val41); val41 = std::max(val29,val41);
  6378. val29 = tmp; tmp = std::min(val30,val42); val42 = std::max(val30,val42); val30 = tmp;
  6379. tmp = std::min(val31,val43); val43 = std::max(val31,val43); val31 = tmp; tmp = std::min(val36,val48);
  6380. val48 = std::max(val36,val48); val36 = tmp; tmp = std::min(val4,val8); val8 = std::max(val4,val8);
  6381. val4 = tmp; tmp = std::min(val5,val9); val9 = std::max(val5,val9); val5 = tmp; tmp = std::min(val6,val10);
  6382. val10 = std::max(val6,val10); val6 = tmp; tmp = std::min(val7,val11); val11 = std::max(val7,val11); val7 = tmp;
  6383. tmp = std::min(val12,val16); val16 = std::max(val12,val16); val12 = tmp; tmp = std::min(val13,val17);
  6384. val17 = std::max(val13,val17); val13 = tmp; tmp = std::min(val14,val18); val18 = std::max(val14,val18);
  6385. val14 = tmp; tmp = std::min(val15,val19); val19 = std::max(val15,val19); val15 = tmp;
  6386. tmp = std::min(val20,val24); val24 = std::max(val20,val24); val20 = tmp; tmp = std::min(val21,val25);
  6387. val25 = std::max(val21,val25); val21 = tmp; tmp = std::min(val22,val26); val26 = std::max(val22,val26);
  6388. val22 = tmp; tmp = std::min(val23,val27); val27 = std::max(val23,val27); val23 = tmp;
  6389. tmp = std::min(val28,val32); val32 = std::max(val28,val32); val28 = tmp; tmp = std::min(val29,val33);
  6390. val33 = std::max(val29,val33); val29 = tmp; tmp = std::min(val30,val34); val34 = std::max(val30,val34);
  6391. val30 = tmp; tmp = std::min(val31,val35); val35 = std::max(val31,val35); val31 = tmp;
  6392. tmp = std::min(val36,val40); val40 = std::max(val36,val40); val36 = tmp; tmp = std::min(val37,val41);
  6393. val41 = std::max(val37,val41); val37 = tmp; tmp = std::min(val38,val42); val42 = std::max(val38,val42);
  6394. val38 = tmp; tmp = std::min(val39,val43); val43 = std::max(val39,val43); val39 = tmp;
  6395. tmp = std::min(val44,val48); val48 = std::max(val44,val48); val44 = tmp; tmp = std::min(val0,val2);
  6396. val2 = std::max(val0,val2); val0 = tmp; tmp = std::min(val1,val3); val3 = std::max(val1,val3); val1 = tmp;
  6397. tmp = std::min(val4,val6); val6 = std::max(val4,val6); val4 = tmp; tmp = std::min(val5,val7);
  6398. val7 = std::max(val5,val7); val5 = tmp; tmp = std::min(val8,val10); val10 = std::max(val8,val10); val8 = tmp;
  6399. tmp = std::min(val9,val11); val11 = std::max(val9,val11); val9 = tmp; tmp = std::min(val12,val14);
  6400. val14 = std::max(val12,val14); val12 = tmp; tmp = std::min(val13,val15); val15 = std::max(val13,val15);
  6401. val13 = tmp; tmp = std::min(val16,val18); val18 = std::max(val16,val18); val16 = tmp;
  6402. tmp = std::min(val17,val19); val19 = std::max(val17,val19); val17 = tmp; tmp = std::min(val20,val22);
  6403. val22 = std::max(val20,val22); val20 = tmp; tmp = std::min(val21,val23); val23 = std::max(val21,val23);
  6404. val21 = tmp; tmp = std::min(val24,val26); val26 = std::max(val24,val26); val24 = tmp;
  6405. tmp = std::min(val25,val27); val27 = std::max(val25,val27); val25 = tmp; tmp = std::min(val28,val30);
  6406. val30 = std::max(val28,val30); val28 = tmp; tmp = std::min(val29,val31); val31 = std::max(val29,val31);
  6407. val29 = tmp; tmp = std::min(val32,val34); val34 = std::max(val32,val34); val32 = tmp;
  6408. tmp = std::min(val33,val35); val35 = std::max(val33,val35); val33 = tmp; tmp = std::min(val36,val38);
  6409. val38 = std::max(val36,val38); val36 = tmp; tmp = std::min(val37,val39); val39 = std::max(val37,val39);
  6410. val37 = tmp; tmp = std::min(val40,val42); val42 = std::max(val40,val42); val40 = tmp;
  6411. tmp = std::min(val41,val43); val43 = std::max(val41,val43); val41 = tmp; tmp = std::min(val44,val46);
  6412. val46 = std::max(val44,val46); val44 = tmp; tmp = std::min(val45,val47); val47 = std::max(val45,val47);
  6413. val45 = tmp; tmp = std::min(val2,val32); val32 = std::max(val2,val32); val2 = tmp; tmp = std::min(val3,val33);
  6414. val33 = std::max(val3,val33); val3 = tmp; tmp = std::min(val6,val36); val36 = std::max(val6,val36); val6 = tmp;
  6415. tmp = std::min(val7,val37); val37 = std::max(val7,val37); val7 = tmp; tmp = std::min(val10,val40);
  6416. val40 = std::max(val10,val40); val10 = tmp; tmp = std::min(val11,val41); val41 = std::max(val11,val41);
  6417. val11 = tmp; tmp = std::min(val14,val44); val44 = std::max(val14,val44); val14 = tmp;
  6418. tmp = std::min(val15,val45); val45 = std::max(val15,val45); val15 = tmp; tmp = std::min(val18,val48);
  6419. val48 = std::max(val18,val48); val18 = tmp; tmp = std::min(val2,val16); val16 = std::max(val2,val16);
  6420. val2 = tmp; tmp = std::min(val3,val17); val17 = std::max(val3,val17); val3 = tmp;
  6421. tmp = std::min(val6,val20); val20 = std::max(val6,val20); val6 = tmp; tmp = std::min(val7,val21);
  6422. val21 = std::max(val7,val21); val7 = tmp; tmp = std::min(val10,val24); val24 = std::max(val10,val24);
  6423. val10 = tmp; tmp = std::min(val11,val25); val25 = std::max(val11,val25); val11 = tmp;
  6424. tmp = std::min(val14,val28); val28 = std::max(val14,val28); val14 = tmp; tmp = std::min(val15,val29);
  6425. val29 = std::max(val15,val29); val15 = tmp; tmp = std::min(val18,val32); val32 = std::max(val18,val32);
  6426. val18 = tmp; tmp = std::min(val19,val33); val33 = std::max(val19,val33); val19 = tmp;
  6427. tmp = std::min(val22,val36); val36 = std::max(val22,val36); val22 = tmp; tmp = std::min(val23,val37);
  6428. val37 = std::max(val23,val37); val23 = tmp; tmp = std::min(val26,val40); val40 = std::max(val26,val40);
  6429. val26 = tmp; tmp = std::min(val27,val41); val41 = std::max(val27,val41); val27 = tmp;
  6430. tmp = std::min(val30,val44); val44 = std::max(val30,val44); val30 = tmp; tmp = std::min(val31,val45);
  6431. val45 = std::max(val31,val45); val31 = tmp; tmp = std::min(val34,val48); val48 = std::max(val34,val48);
  6432. val34 = tmp; tmp = std::min(val2,val8); val8 = std::max(val2,val8); val2 = tmp; tmp = std::min(val3,val9);
  6433. val9 = std::max(val3,val9); val3 = tmp; tmp = std::min(val6,val12); val12 = std::max(val6,val12); val6 = tmp;
  6434. tmp = std::min(val7,val13); val13 = std::max(val7,val13); val7 = tmp; tmp = std::min(val10,val16);
  6435. val16 = std::max(val10,val16); val10 = tmp; tmp = std::min(val11,val17); val17 = std::max(val11,val17);
  6436. val11 = tmp; tmp = std::min(val14,val20); val20 = std::max(val14,val20); val14 = tmp;
  6437. tmp = std::min(val15,val21); val21 = std::max(val15,val21); val15 = tmp; tmp = std::min(val18,val24);
  6438. val24 = std::max(val18,val24); val18 = tmp; tmp = std::min(val19,val25); val25 = std::max(val19,val25);
  6439. val19 = tmp; tmp = std::min(val22,val28); val28 = std::max(val22,val28); val22 = tmp;
  6440. tmp = std::min(val23,val29); val29 = std::max(val23,val29); val23 = tmp; tmp = std::min(val26,val32);
  6441. val32 = std::max(val26,val32); val26 = tmp; tmp = std::min(val27,val33); val33 = std::max(val27,val33);
  6442. val27 = tmp; tmp = std::min(val30,val36); val36 = std::max(val30,val36); val30 = tmp;
  6443. tmp = std::min(val31,val37); val37 = std::max(val31,val37); val31 = tmp; tmp = std::min(val34,val40);
  6444. val40 = std::max(val34,val40); val34 = tmp; tmp = std::min(val35,val41); val41 = std::max(val35,val41);
  6445. val35 = tmp; tmp = std::min(val38,val44); val44 = std::max(val38,val44); val38 = tmp;
  6446. tmp = std::min(val39,val45); val45 = std::max(val39,val45); val39 = tmp; tmp = std::min(val42,val48);
  6447. val48 = std::max(val42,val48); val42 = tmp; tmp = std::min(val2,val4); val4 = std::max(val2,val4);
  6448. val2 = tmp; tmp = std::min(val3,val5); val5 = std::max(val3,val5); val3 = tmp; tmp = std::min(val6,val8);
  6449. val8 = std::max(val6,val8); val6 = tmp; tmp = std::min(val7,val9); val9 = std::max(val7,val9); val7 = tmp;
  6450. tmp = std::min(val10,val12); val12 = std::max(val10,val12); val10 = tmp; tmp = std::min(val11,val13);
  6451. val13 = std::max(val11,val13); val11 = tmp; tmp = std::min(val14,val16); val16 = std::max(val14,val16);
  6452. val14 = tmp; tmp = std::min(val15,val17); val17 = std::max(val15,val17); val15 = tmp;
  6453. tmp = std::min(val18,val20); val20 = std::max(val18,val20); val18 = tmp; tmp = std::min(val19,val21);
  6454. val21 = std::max(val19,val21); val19 = tmp; tmp = std::min(val22,val24); val24 = std::max(val22,val24);
  6455. val22 = tmp; tmp = std::min(val23,val25); val25 = std::max(val23,val25); val23 = tmp;
  6456. tmp = std::min(val26,val28); val28 = std::max(val26,val28); val26 = tmp; tmp = std::min(val27,val29);
  6457. val29 = std::max(val27,val29); val27 = tmp; tmp = std::min(val30,val32); val32 = std::max(val30,val32);
  6458. val30 = tmp; tmp = std::min(val31,val33); val33 = std::max(val31,val33); val31 = tmp;
  6459. tmp = std::min(val34,val36); val36 = std::max(val34,val36); val34 = tmp; tmp = std::min(val35,val37);
  6460. val37 = std::max(val35,val37); val35 = tmp; tmp = std::min(val38,val40); val40 = std::max(val38,val40);
  6461. val38 = tmp; tmp = std::min(val39,val41); val41 = std::max(val39,val41); val39 = tmp;
  6462. tmp = std::min(val42,val44); val44 = std::max(val42,val44); val42 = tmp; tmp = std::min(val43,val45);
  6463. val45 = std::max(val43,val45); val43 = tmp; tmp = std::min(val46,val48); val48 = std::max(val46,val48);
  6464. val46 = tmp; val1 = std::max(val0,val1); val3 = std::max(val2,val3); val5 = std::max(val4,val5);
  6465. val7 = std::max(val6,val7); val9 = std::max(val8,val9); val11 = std::max(val10,val11);
  6466. val13 = std::max(val12,val13); val15 = std::max(val14,val15); val17 = std::max(val16,val17);
  6467. val19 = std::max(val18,val19); val21 = std::max(val20,val21); val23 = std::max(val22,val23);
  6468. val24 = std::min(val24,val25); val26 = std::min(val26,val27); val28 = std::min(val28,val29);
  6469. val30 = std::min(val30,val31); val32 = std::min(val32,val33); val34 = std::min(val34,val35);
  6470. val36 = std::min(val36,val37); val38 = std::min(val38,val39); val40 = std::min(val40,val41);
  6471. val42 = std::min(val42,val43); val44 = std::min(val44,val45); val46 = std::min(val46,val47);
  6472. val32 = std::max(val1,val32); val34 = std::max(val3,val34); val36 = std::max(val5,val36);
  6473. val38 = std::max(val7,val38); val9 = std::min(val9,val40); val11 = std::min(val11,val42);
  6474. val13 = std::min(val13,val44); val15 = std::min(val15,val46); val17 = std::min(val17,val48);
  6475. val24 = std::max(val9,val24); val26 = std::max(val11,val26); val28 = std::max(val13,val28);
  6476. val30 = std::max(val15,val30); val17 = std::min(val17,val32); val19 = std::min(val19,val34);
  6477. val21 = std::min(val21,val36); val23 = std::min(val23,val38); val24 = std::max(val17,val24);
  6478. val26 = std::max(val19,val26); val21 = std::min(val21,val28); val23 = std::min(val23,val30);
  6479. val24 = std::max(val21,val24); val23 = std::min(val23,val26);
  6480. return std::max(val23,val24);
  6481. }
  6482. //! Return sqrt(x^2 + y^2).
  6483. template<typename T>
  6484. inline T hypot(const T x, const T y) {
  6485. return std::sqrt(x*x + y*y);
  6486. }
  6487. template<typename T>
  6488. inline T hypot(const T x, const T y, const T z) {
  6489. return std::sqrt(x*x + y*y + z*z);
  6490. }
  6491. template<typename T>
  6492. inline T _hypot(const T x, const T y) { // Slower but more precise version
  6493. T nx = cimg::abs(x), ny = cimg::abs(y), t;
  6494. if (nx<ny) { t = nx; nx = ny; } else t = ny;
  6495. if (nx>0) { t/=nx; return nx*std::sqrt(1 + t*t); }
  6496. return 0;
  6497. }
  6498. //! Return the factorial of n
  6499. inline double factorial(const int n) {
  6500. if (n<0) return cimg::type<double>::nan();
  6501. if (n<2) return 1;
  6502. double res = 2;
  6503. for (int i = 3; i<=n; ++i) res*=i;
  6504. return res;
  6505. }
  6506. //! Return the number of permutations of k objects in a set of n objects.
  6507. inline double permutations(const int k, const int n, const bool with_order) {
  6508. if (n<0 || k<0) return cimg::type<double>::nan();
  6509. if (k>n) return 0;
  6510. double res = 1;
  6511. for (int i = n; i>=n - k + 1; --i) res*=i;
  6512. return with_order?res:res/cimg::factorial(k);
  6513. }
  6514. inline double _fibonacci(int exp) {
  6515. double
  6516. base = (1 + std::sqrt(5.))/2,
  6517. result = 1/std::sqrt(5.);
  6518. while (exp) {
  6519. if (exp&1) result*=base;
  6520. exp>>=1;
  6521. base*=base;
  6522. }
  6523. return result;
  6524. }
  6525. //! Calculate fibonacci number.
  6526. // (Precise up to n = 78, less precise for n>78).
  6527. inline double fibonacci(const int n) {
  6528. if (n<0) return cimg::type<double>::nan();
  6529. if (n<3) return 1;
  6530. if (n<11) {
  6531. cimg_uint64 fn1 = 1, fn2 = 1, fn = 0;
  6532. for (int i = 3; i<=n; ++i) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; }
  6533. return (double)fn;
  6534. }
  6535. if (n<75) // precise up to n = 74, faster than the integer calculation above for n>10
  6536. return (double)((cimg_uint64)(_fibonacci(n) + 0.5));
  6537. if (n<94) { // precise up to n = 78, less precise for n>78 up to n = 93, overflows for n>93
  6538. cimg_uint64
  6539. fn1 = ((cimg_uint64)303836)<<32 | 3861581201UL, // 1304969544928657ULL (avoid C++98 warning with ULL)
  6540. fn2 = ((cimg_uint64)187781)<<32 | 2279239217UL, // 806515533049393ULL
  6541. fn = 0;
  6542. for (int i = 75; i<=n; ++i) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; }
  6543. return (double)fn;
  6544. }
  6545. return _fibonacci(n); // Not precise, but better than the wrong overflowing calculation
  6546. }
  6547. //! Calculate greatest common divisor.
  6548. inline long gcd(long a, long b) {
  6549. while (a) { const long c = a; a = b%a; b = c; }
  6550. return b;
  6551. }
  6552. //! Convert character to lower case.
  6553. inline char lowercase(const char x) {
  6554. return (char)((x<'A'||x>'Z')?x:x - 'A' + 'a');
  6555. }
  6556. inline double lowercase(const double x) {
  6557. return (double)((x<'A'||x>'Z')?x:x - 'A' + 'a');
  6558. }
  6559. //! Convert C-string to lower case.
  6560. inline void lowercase(char *const str) {
  6561. if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = lowercase(*ptr);
  6562. }
  6563. //! Convert character to upper case.
  6564. inline char uppercase(const char x) {
  6565. return (char)((x<'a'||x>'z')?x:x - 'a' + 'A');
  6566. }
  6567. inline double uppercase(const double x) {
  6568. return (double)((x<'a'||x>'z')?x:x - 'a' + 'A');
  6569. }
  6570. //! Convert C-string to upper case.
  6571. inline void uppercase(char *const str) {
  6572. if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = uppercase(*ptr);
  6573. }
  6574. //! Return \c true if input character is blank (space, tab, or non-printable character).
  6575. inline bool is_blank(const char c) {
  6576. return c>=0 && (unsigned char)c<=' ';
  6577. }
  6578. //! Read value in a C-string.
  6579. /**
  6580. \param str C-string containing the float value to read.
  6581. \return Read value.
  6582. \note Same as <tt>std::atof()</tt> extended to manage the retrieval of fractions from C-strings,
  6583. as in <em>"1/2"</em>.
  6584. **/
  6585. inline double atof(const char *const str) {
  6586. double x = 0, y = 1;
  6587. return str && cimg_sscanf(str,"%lf/%lf",&x,&y)>0?x/y:0;
  6588. }
  6589. //! Compare the first \p l characters of two C-strings, ignoring the case.
  6590. /**
  6591. \param str1 C-string.
  6592. \param str2 C-string.
  6593. \param l Number of characters to compare.
  6594. \return \c 0 if the two strings are equal, something else otherwise.
  6595. \note This function has to be defined since it is not provided by all C++-compilers (not ANSI).
  6596. **/
  6597. inline int strncasecmp(const char *const str1, const char *const str2, const int l) {
  6598. if (!l) return 0;
  6599. if (!str1) return str2?-1:0;
  6600. const char *nstr1 = str1, *nstr2 = str2;
  6601. int k, diff = 0; for (k = 0; k<l && !(diff = lowercase(*nstr1) - lowercase(*nstr2)); ++k) { ++nstr1; ++nstr2; }
  6602. return k!=l?diff:0;
  6603. }
  6604. //! Compare two C-strings, ignoring the case.
  6605. /**
  6606. \param str1 C-string.
  6607. \param str2 C-string.
  6608. \return \c 0 if the two strings are equal, something else otherwise.
  6609. \note This function has to be defined since it is not provided by all C++-compilers (not ANSI).
  6610. **/
  6611. inline int strcasecmp(const char *const str1, const char *const str2) {
  6612. if (!str1) return str2?-1:0;
  6613. const int
  6614. l1 = (int)std::strlen(str1),
  6615. l2 = (int)std::strlen(str2);
  6616. return cimg::strncasecmp(str1,str2,1 + (l1<l2?l1:l2));
  6617. }
  6618. //! Ellipsize a string.
  6619. /**
  6620. \param str C-string.
  6621. \param l Max number of characters.
  6622. \param is_ending Tell if the dots are placed at the end or at the center of the ellipsized string.
  6623. **/
  6624. inline char *strellipsize(char *const str, const unsigned int l=64,
  6625. const bool is_ending=true) {
  6626. if (!str) return str;
  6627. const unsigned int nl = l<5?5:l, ls = (unsigned int)std::strlen(str);
  6628. if (ls<=nl) return str;
  6629. if (is_ending) std::strcpy(str + nl - 5,"(...)");
  6630. else {
  6631. const unsigned int ll = (nl - 5)/2 + 1 - (nl%2), lr = nl - ll - 5;
  6632. std::strcpy(str + ll,"(...)");
  6633. std::memmove(str + ll + 5,str + ls - lr,lr);
  6634. }
  6635. str[nl] = 0;
  6636. return str;
  6637. }
  6638. //! Ellipsize a string.
  6639. /**
  6640. \param str C-string.
  6641. \param res output C-string.
  6642. \param l Max number of characters.
  6643. \param is_ending Tell if the dots are placed at the end or at the center of the ellipsized string.
  6644. **/
  6645. inline char *strellipsize(const char *const str, char *const res, const unsigned int l=64,
  6646. const bool is_ending=true) {
  6647. const unsigned int nl = l<5?5:l, ls = (unsigned int)std::strlen(str);
  6648. if (ls<=nl) { std::strcpy(res,str); return res; }
  6649. if (is_ending) {
  6650. std::strncpy(res,str,nl - 5);
  6651. std::strcpy(res + nl -5,"(...)");
  6652. } else {
  6653. const unsigned int ll = (nl - 5)/2 + 1 - (nl%2), lr = nl - ll - 5;
  6654. std::strncpy(res,str,ll);
  6655. std::strcpy(res + ll,"(...)");
  6656. std::strncpy(res + ll + 5,str + ls - lr,lr);
  6657. }
  6658. res[nl] = 0;
  6659. return res;
  6660. }
  6661. //! Remove delimiters on the start and/or end of a C-string.
  6662. /**
  6663. \param[in,out] str C-string to work with (modified at output).
  6664. \param delimiter Delimiter character code to remove.
  6665. \param is_symmetric Tells if the removal is done only if delimiters are symmetric
  6666. (both at the beginning and the end of \c s).
  6667. \param is_iterative Tells if the removal is done if several iterations are possible.
  6668. \return \c true if delimiters have been removed, \c false otherwise.
  6669. **/
  6670. inline bool strpare(char *const str, const char delimiter,
  6671. const bool is_symmetric, const bool is_iterative) {
  6672. if (!str) return false;
  6673. const int l = (int)std::strlen(str);
  6674. int p, q;
  6675. if (is_symmetric) for (p = 0, q = l - 1; p<q && str[p]==delimiter && str[q]==delimiter; ) {
  6676. --q; ++p; if (!is_iterative) break;
  6677. } else {
  6678. for (p = 0; p<l && str[p]==delimiter; ) { ++p; if (!is_iterative) break; }
  6679. for (q = l - 1; q>p && str[q]==delimiter; ) { --q; if (!is_iterative) break; }
  6680. }
  6681. const int n = q - p + 1;
  6682. if (n!=l) { std::memmove(str,str + p,(unsigned int)n); str[n] = 0; return true; }
  6683. return false;
  6684. }
  6685. //! Remove white spaces on the start and/or end of a C-string.
  6686. inline bool strpare(char *const str, const bool is_symmetric, const bool is_iterative) {
  6687. if (!str) return false;
  6688. const int l = (int)std::strlen(str);
  6689. int p, q;
  6690. if (is_symmetric) for (p = 0, q = l - 1; p<q && is_blank(str[p]) && is_blank(str[q]); ) {
  6691. --q; ++p; if (!is_iterative) break;
  6692. } else {
  6693. for (p = 0; p<l && is_blank(str[p]); ) { ++p; if (!is_iterative) break; }
  6694. for (q = l - 1; q>p && is_blank(str[q]); ) { --q; if (!is_iterative) break; }
  6695. }
  6696. const int n = q - p + 1;
  6697. if (n!=l) { std::memmove(str,str + p,(unsigned int)n); str[n] = 0; return true; }
  6698. return false;
  6699. }
  6700. //! Replace reserved characters (for Windows filename) by another character.
  6701. /**
  6702. \param[in,out] str C-string to work with (modified at output).
  6703. \param[in] c Replacement character.
  6704. **/
  6705. inline void strwindows_reserved(char *const str, const char c='_') {
  6706. for (char *s = str; *s; ++s) {
  6707. const char i = *s;
  6708. if (i=='<' || i=='>' || i==':' || i=='\"' || i=='/' || i=='\\' || i=='|' || i=='?' || i=='*') *s = c;
  6709. }
  6710. }
  6711. //! Replace escape sequences in C-strings by character values.
  6712. /**
  6713. \param[in,out] str C-string to work with (modified at output).
  6714. **/
  6715. inline void strunescape(char *const str) {
  6716. #define cimg_strunescape(ci,co) case ci : *nd = co; ++ns; break;
  6717. unsigned char val = 0;
  6718. for (char *ns = str, *nd = str; *ns || (bool)(*nd = 0); ++nd) if (*ns=='\\') switch (*(++ns)) {
  6719. cimg_strunescape('a','\a');
  6720. cimg_strunescape('b','\b');
  6721. cimg_strunescape('e',0x1B);
  6722. cimg_strunescape('f','\f');
  6723. cimg_strunescape('n','\n');
  6724. cimg_strunescape('r','\r');
  6725. cimg_strunescape('t','\t');
  6726. cimg_strunescape('v','\v');
  6727. cimg_strunescape('\\','\\');
  6728. cimg_strunescape('\'','\'');
  6729. cimg_strunescape('\"','\"');
  6730. cimg_strunescape('\?','\?');
  6731. case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' :
  6732. val = (unsigned char)(*(ns++) - '0');
  6733. if (*ns>='0' && *ns<='7') (val<<=3)|=*(ns++) - '0';
  6734. if (*ns>='0' && *ns<='7') (val<<=3)|=*(ns++) - '0';
  6735. *nd = (char)val;
  6736. break;
  6737. case 'x' : {
  6738. char c = lowercase(*(++ns));
  6739. if ((c>='0' && c<='9') || (c>='a' && c<='f')) {
  6740. val = (unsigned char)(c<='9'?c - '0':c - 'a' + 10);
  6741. c = lowercase(*(++ns));
  6742. if ((c>='0' && c<='9') || (c>='a' && c<='f')) {
  6743. (val<<=4)|=(c<='9'?c - '0':c - 'a' + 10);
  6744. ++ns;
  6745. }
  6746. *nd = (char)val;
  6747. } else *nd = c;
  6748. } break;
  6749. case 'u' : { // UTF-8 BMP
  6750. char c1, c2, c3, c4;
  6751. if ((((c1 = lowercase(ns[1]))>='0' && c1<='9') || (c1>='a' && c1<='f')) &&
  6752. (((c2 = lowercase(ns[2]))>='0' && c2<='9') || (c2>='a' && c2<='f')) &&
  6753. (((c3 = lowercase(ns[3]))>='0' && c3<='9') || (c3>='a' && c3<='f')) &&
  6754. (((c4 = lowercase(ns[4]))>='0' && c4<='9') || (c4>='a' && c4<='f'))) {
  6755. c1 = (c1<='9'?c1 - '0':c1 - 'a' + 10);
  6756. c2 = (c2<='9'?c2 - '0':c2 - 'a' + 10);
  6757. c3 = (c3<='9'?c3 - '0':c3 - 'a' + 10);
  6758. c4 = (c4<='9'?c4 - '0':c4 - 'a' + 10);
  6759. const unsigned int ival =
  6760. ((unsigned int)c1<<12) | ((unsigned int)c2<<8) | ((unsigned int)c3<<4) | c4;
  6761. if (ival<=0x007f) *nd = (char)ival;
  6762. else if (ival<=0x07ff) {
  6763. *(nd++) = (char)((ival>>6)|0xc0);
  6764. *nd = (char)((ival&0x3f)|0x80);
  6765. } else {
  6766. *(nd++) = (char)((ival>>12)|0xe0);
  6767. *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
  6768. *nd = (char)((ival&0x3f)|0x80);
  6769. }
  6770. ns+=5;
  6771. } else *nd = *(ns++);
  6772. } break;
  6773. case 'U' : { // UTF-8 astral planes
  6774. char c1, c2, c3, c4, c5, c6, c7, c8;
  6775. if ((((c1 = lowercase(ns[1]))>='0' && c1<='9') || (c1>='a' && c1<='f')) &&
  6776. (((c2 = lowercase(ns[2]))>='0' && c2<='9') || (c2>='a' && c2<='f')) &&
  6777. (((c3 = lowercase(ns[3]))>='0' && c3<='9') || (c3>='a' && c3<='f')) &&
  6778. (((c4 = lowercase(ns[4]))>='0' && c4<='9') || (c4>='a' && c4<='f')) &&
  6779. (((c5 = lowercase(ns[5]))>='0' && c5<='9') || (c5>='a' && c5<='f')) &&
  6780. (((c6 = lowercase(ns[6]))>='0' && c6<='9') || (c6>='a' && c6<='f')) &&
  6781. (((c7 = lowercase(ns[7]))>='0' && c7<='9') || (c7>='a' && c7<='f')) &&
  6782. (((c8 = lowercase(ns[8]))>='0' && c8<='9') || (c8>='a' && c8<='f'))) {
  6783. c1 = (c1<='9'?c1 - '0':c1 - 'a' + 10);
  6784. c2 = (c2<='9'?c2 - '0':c2 - 'a' + 10);
  6785. c3 = (c3<='9'?c3 - '0':c3 - 'a' + 10);
  6786. c4 = (c4<='9'?c4 - '0':c4 - 'a' + 10);
  6787. c5 = (c5<='9'?c5 - '0':c5 - 'a' + 10);
  6788. c6 = (c6<='9'?c6 - '0':c6 - 'a' + 10);
  6789. c7 = (c7<='9'?c7 - '0':c7 - 'a' + 10);
  6790. c8 = (c8<='9'?c8 - '0':c8 - 'a' + 10);
  6791. const unsigned int ival =
  6792. ((unsigned int)c1<<28) | ((unsigned int)c2<<24) | ((unsigned int)c3<<20) | ((unsigned int)c4<<16) |
  6793. ((unsigned int)c5<<12) | ((unsigned int)c6<<8) | ((unsigned int)c7<<4) | (unsigned int)c8;
  6794. if (ival<=0x007f) *nd = (char)ival;
  6795. else if (ival<=0x07ff) {
  6796. *(nd++) = (char)((ival>>6)|0xc0);
  6797. *nd = (char)((ival&0x3f)|0x80);
  6798. } else if (ival<=0xffff) {
  6799. *(nd++) = (char)((ival>>12)|0xe0);
  6800. *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
  6801. *nd = (char)((ival&0x3f)|0x80);
  6802. } else {
  6803. *(nd++) = (char)((ival>>18)|0xf0);
  6804. *(nd++) = (char)(((ival>>12)&0x3f)|0x80);
  6805. *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
  6806. *nd = (char)((ival&0x3f)|0x80);
  6807. }
  6808. ns+=9;
  6809. } else *nd = *(ns++);
  6810. } break;
  6811. default : if (*ns) *nd = *(ns++);
  6812. }
  6813. else *nd = *(ns++);
  6814. }
  6815. // Return a temporary string describing the size of a memory buffer.
  6816. inline const char *strbuffersize(const cimg_ulong size);
  6817. // Return string that identifies the running OS.
  6818. inline const char *stros() {
  6819. #if defined(linux) || defined(__linux) || defined(__linux__)
  6820. static const char *const str = "Linux";
  6821. #elif defined(sun) || defined(__sun)
  6822. static const char *const str = "Sun OS";
  6823. #elif defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined (__DragonFly__)
  6824. static const char *const str = "BSD";
  6825. #elif defined(sgi) || defined(__sgi)
  6826. static const char *const str = "Irix";
  6827. #elif defined(__MACOSX__) || defined(__APPLE__)
  6828. static const char *const str = "Mac OS";
  6829. #elif defined(unix) || defined(__unix) || defined(__unix__)
  6830. static const char *const str = "Generic Unix";
  6831. #elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
  6832. defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
  6833. static const char *const str = "Windows";
  6834. #else
  6835. const char
  6836. *const _str1 = std::getenv("OSTYPE"),
  6837. *const _str2 = _str1?_str1:std::getenv("OS"),
  6838. *const str = _str2?_str2:"Unknown OS";
  6839. #endif
  6840. return str;
  6841. }
  6842. //! Return the basename of a filename.
  6843. inline const char* basename(const char *const s, const char separator=cimg_file_separator) {
  6844. const char *p = 0, *np = s;
  6845. while (np>=s && (p=np)) np = std::strchr(np,separator) + 1;
  6846. return p;
  6847. }
  6848. // Return a random filename.
  6849. inline const char* filenamerand() {
  6850. cimg::mutex(6);
  6851. static char randomid[9];
  6852. for (unsigned int k = 0; k<8; ++k) {
  6853. const int v = (int)cimg::rand(65535)%3;
  6854. randomid[k] = (char)(v==0?('0' + ((int)cimg::rand(65535)%10)):
  6855. (v==1?('a' + ((int)cimg::rand(65535)%26)):
  6856. ('A' + ((int)cimg::rand(65535)%26))));
  6857. }
  6858. cimg::mutex(6,0);
  6859. return randomid;
  6860. }
  6861. // Convert filename as a Windows-style filename (short path name).
  6862. inline void winformat_string(char *const str) {
  6863. if (str && *str) {
  6864. #if cimg_OS==2
  6865. char *const nstr = new char[MAX_PATH];
  6866. if (GetShortPathNameA(str,nstr,MAX_PATH)) std::strcpy(str,nstr);
  6867. delete[] nstr;
  6868. #endif
  6869. }
  6870. }
  6871. // Open a file (similar to std:: fopen(), but with wide character support on Windows).
  6872. inline std::FILE *std_fopen(const char *const path, const char *const mode);
  6873. //! Open a file.
  6874. /**
  6875. \param path Path of the filename to open.
  6876. \param mode C-string describing the opening mode.
  6877. \return Opened file.
  6878. \note Same as <tt>std::fopen()</tt> but throw a \c CImgIOException when
  6879. the specified file cannot be opened, instead of returning \c 0.
  6880. **/
  6881. inline std::FILE *fopen(const char *const path, const char *const mode) {
  6882. if (!path)
  6883. throw CImgArgumentException("cimg::fopen(): Specified file path is (null).");
  6884. if (!mode)
  6885. throw CImgArgumentException("cimg::fopen(): File '%s', specified mode is (null).",
  6886. path);
  6887. std::FILE *res = 0;
  6888. if (*path=='-' && (!path[1] || path[1]=='.')) {
  6889. res = (*mode=='r')?cimg::_stdin():cimg::_stdout();
  6890. #if cimg_OS==2
  6891. if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode
  6892. #ifdef __BORLANDC__
  6893. if (setmode(_fileno(res),0x8000)==-1) res = 0;
  6894. #else
  6895. if (_setmode(_fileno(res),0x8000)==-1) res = 0;
  6896. #endif
  6897. }
  6898. #endif
  6899. } else res = cimg::std_fopen(path,mode);
  6900. if (!res) throw CImgIOException("cimg::fopen(): Failed to open file '%s' with mode '%s'.",
  6901. path,mode);
  6902. return res;
  6903. }
  6904. //! Close a file.
  6905. /**
  6906. \param file File to close.
  6907. \return \c 0 if file has been closed properly, something else otherwise.
  6908. \note Same as <tt>std::fclose()</tt> but display a warning message if
  6909. the file has not been closed properly.
  6910. **/
  6911. inline int fclose(std::FILE *file) {
  6912. if (!file) { warn("cimg::fclose(): Specified file is (null)."); return 0; }
  6913. if (file==cimg::_stdin(false) || file==cimg::_stdout(false)) return 0;
  6914. const int errn = std::fclose(file);
  6915. if (errn!=0) warn("cimg::fclose(): Error code %d returned during file closing.",
  6916. errn);
  6917. return errn;
  6918. }
  6919. //! Version of 'fseek()' that supports >=64bits offsets everywhere (for Windows).
  6920. inline int fseek(FILE *stream, cimg_long offset, int origin) {
  6921. #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
  6922. return _fseeki64(stream,(__int64)offset,origin);
  6923. #else
  6924. return std::fseek(stream,offset,origin);
  6925. #endif
  6926. }
  6927. //! Version of 'ftell()' that supports >=64bits offsets everywhere (for Windows).
  6928. inline cimg_long ftell(FILE *stream) {
  6929. #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
  6930. return (cimg_long)_ftelli64(stream);
  6931. #else
  6932. return (cimg_long)std::ftell(stream);
  6933. #endif
  6934. }
  6935. // Get the file or directory attributes with support for UTF-8 paths (Windows only).
  6936. #if cimg_OS==2
  6937. inline DWORD win_getfileattributes(const char *const path);
  6938. #endif
  6939. //! Check if a path is a directory.
  6940. /**
  6941. \param path Specified path to test.
  6942. **/
  6943. inline bool is_directory(const char *const path) {
  6944. if (!path || !*path) return false;
  6945. #if cimg_OS==1
  6946. struct stat st_buf;
  6947. return (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode));
  6948. #elif cimg_OS==2
  6949. const DWORD res = win_getfileattributes(path);
  6950. return res!=INVALID_FILE_ATTRIBUTES && (res&FILE_ATTRIBUTE_DIRECTORY);
  6951. #else
  6952. return false;
  6953. #endif
  6954. }
  6955. //! Check if a path is a file.
  6956. /**
  6957. \param path Specified path to test.
  6958. **/
  6959. inline bool is_file(const char *const path) {
  6960. if (!path || !*path) return false;
  6961. #if cimg_OS==2
  6962. const DWORD res = cimg::win_getfileattributes(path);
  6963. return res!=INVALID_FILE_ATTRIBUTES && !(res&FILE_ATTRIBUTE_DIRECTORY);
  6964. #else
  6965. std::FILE *const file = cimg::std_fopen(path,"rb");
  6966. if (!file) return false;
  6967. cimg::fclose(file);
  6968. return !is_directory(path);
  6969. #endif
  6970. }
  6971. //! Get file size.
  6972. /**
  6973. \param filename Specified filename to get size from.
  6974. \return File size or '-1' if file does not exist.
  6975. **/
  6976. inline cimg_int64 fsize(const char *const filename) {
  6977. std::FILE *const file = cimg::std_fopen(filename,"rb");
  6978. if (!file) return (cimg_int64)-1;
  6979. std::fseek(file,0,SEEK_END);
  6980. const cimg_int64 siz = (cimg_int64)std::ftell(file);
  6981. cimg::fclose(file);
  6982. return siz;
  6983. }
  6984. //! Get last write time of a given file or directory (multiple-attributes version).
  6985. /**
  6986. \param path Specified path to get attributes from.
  6987. \param[in,out] attr Type of requested time attributes.
  6988. Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second }
  6989. Replaced by read attributes after return (or -1 if an error occurred).
  6990. \param nb_attr Number of attributes to read/write.
  6991. \return Latest read attribute.
  6992. **/
  6993. template<typename T>
  6994. inline int fdate(const char *const path, T *attr, const unsigned int nb_attr) {
  6995. #define _cimg_fdate_err() for (unsigned int i = 0; i<nb_attr; ++i) attr[i] = (T)-1
  6996. int res = -1;
  6997. if (!path || !*path) { _cimg_fdate_err(); return -1; }
  6998. cimg::mutex(6);
  6999. #if cimg_OS==2
  7000. HANDLE file = CreateFileA(path,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  7001. if (file!=INVALID_HANDLE_VALUE) {
  7002. FILETIME _ft;
  7003. SYSTEMTIME ft;
  7004. if (GetFileTime(file,0,0,&_ft) && FileTimeToSystemTime(&_ft,&ft)) {
  7005. for (unsigned int i = 0; i<nb_attr; ++i) {
  7006. res = (int)(attr[i]==0?ft.wYear:attr[i]==1?ft.wMonth:attr[i]==2?ft.wDay:
  7007. attr[i]==3?ft.wDayOfWeek:attr[i]==4?ft.wHour:attr[i]==5?ft.wMinute:
  7008. attr[i]==6?ft.wSecond:-1);
  7009. attr[i] = (T)res;
  7010. }
  7011. } else _cimg_fdate_err();
  7012. CloseHandle(file);
  7013. } else _cimg_fdate_err();
  7014. #elif cimg_OS==1
  7015. struct stat st_buf;
  7016. if (!stat(path,&st_buf)) {
  7017. const time_t _ft = st_buf.st_mtime;
  7018. const struct tm& ft = *std::localtime(&_ft);
  7019. for (unsigned int i = 0; i<nb_attr; ++i) {
  7020. res = (int)(attr[i]==0?ft.tm_year + 1900:attr[i]==1?ft.tm_mon + 1:attr[i]==2?ft.tm_mday:
  7021. attr[i]==3?ft.tm_wday:attr[i]==4?ft.tm_hour:attr[i]==5?ft.tm_min:
  7022. attr[i]==6?ft.tm_sec:-1);
  7023. attr[i] = (T)res;
  7024. }
  7025. } else _cimg_fdate_err();
  7026. #endif
  7027. cimg::mutex(6,0);
  7028. return res;
  7029. }
  7030. //! Get last write time of a given file or directory (single-attribute version).
  7031. /**
  7032. \param path Specified path to get attributes from.
  7033. \param attr Type of requested time attributes.
  7034. Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second }
  7035. \return Specified attribute or -1 if an error occurred.
  7036. **/
  7037. inline int fdate(const char *const path, unsigned int attr) {
  7038. int out = (int)attr;
  7039. return fdate(path,&out,1);
  7040. }
  7041. //! Get current local time (multiple-attributes version).
  7042. /**
  7043. \param[in,out] attr Type of requested time attributes.
  7044. Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second |
  7045. 7=millisecond }
  7046. Replaced by read attributes after return (or -1 if an error occurred).
  7047. \param nb_attr Number of attributes to read/write.
  7048. \return Latest read attribute.
  7049. **/
  7050. template<typename T>
  7051. inline int date(T *attr, const unsigned int nb_attr) {
  7052. int res = -1;
  7053. cimg::mutex(6);
  7054. #if cimg_OS==2
  7055. SYSTEMTIME st;
  7056. GetLocalTime(&st);
  7057. for (unsigned int i = 0; i<nb_attr; ++i) {
  7058. res = (int)(attr[i]==0?st.wYear:
  7059. attr[i]==1?st.wMonth:
  7060. attr[i]==2?st.wDay:
  7061. attr[i]==3?st.wDayOfWeek:
  7062. attr[i]==4?st.wHour:
  7063. attr[i]==5?st.wMinute:
  7064. attr[i]==6?st.wSecond:
  7065. attr[i]==7?st.wMilliseconds:-1);
  7066. attr[i] = (T)res;
  7067. }
  7068. #else
  7069. struct timeval _st;
  7070. gettimeofday(&_st,0);
  7071. struct tm *st = std::localtime(&_st.tv_sec);
  7072. for (unsigned int i = 0; i<nb_attr; ++i) {
  7073. res = (int)(attr[i]==0?st->tm_year + 1900:
  7074. attr[i]==1?st->tm_mon + 1:
  7075. attr[i]==2?st->tm_mday:
  7076. attr[i]==3?st->tm_wday:
  7077. attr[i]==4?st->tm_hour:
  7078. attr[i]==5?st->tm_min:
  7079. attr[i]==6?st->tm_sec:
  7080. attr[i]==7?_st.tv_usec/1000:-1);
  7081. attr[i] = (T)res;
  7082. }
  7083. #endif
  7084. cimg::mutex(6,0);
  7085. return res;
  7086. }
  7087. //! Get current local time (single-attribute version).
  7088. /**
  7089. \param attr Type of requested time attribute.
  7090. Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second |
  7091. 7=millisecond }
  7092. \return Specified attribute or -1 if an error occurred.
  7093. **/
  7094. inline int date(unsigned int attr) {
  7095. int out = (int)attr;
  7096. return date(&out,1);
  7097. }
  7098. // Get/set path to the \c curl binary.
  7099. inline const char *curl_path(const char *const user_path=0, const bool reinit_path=false);
  7100. // Get/set path to the \c dcraw binary.
  7101. inline const char *dcraw_path(const char *const user_path=0, const bool reinit_path=false);
  7102. // Get/set path to the FFMPEG's \c ffmpeg binary.
  7103. inline const char *ffmpeg_path(const char *const user_path=0, const bool reinit_path=false);
  7104. // Get/set path to the GraphicsMagick's \c gm binary.
  7105. inline const char* graphicsmagick_path(const char *const user_path=0, const bool reinit_path=false);
  7106. // Get/set path to the \c gunzip binary.
  7107. inline const char *gunzip_path(const char *const user_path=0, const bool reinit_path=false);
  7108. // Get/set path to the \c gzip binary.
  7109. inline const char *gzip_path(const char *const user_path=0, const bool reinit_path=false);
  7110. // Get/set path to the ImageMagick's \c convert binary.
  7111. inline const char* imagemagick_path(const char *const user_path=0, const bool reinit_path=false);
  7112. // Get/set path to the Medcon's \c medcon binary.
  7113. inline const char* medcon_path(const char *const user_path=0, const bool reinit_path=false);
  7114. // Get/set path to store temporary files.
  7115. inline const char* temporary_path(const char *const user_path=0, const bool reinit_path=false);
  7116. // Get/set path to the \c wget binary.
  7117. inline const char *wget_path(const char *const user_path=0, const bool reinit_path=false);
  7118. //! Split filename into two C-strings \c body and \c extension.
  7119. /**
  7120. filename and body must not overlap!
  7121. **/
  7122. inline const char *split_filename(const char *const filename, char *const body=0) {
  7123. if (!filename) { if (body) *body = 0; return ""; }
  7124. const char * p = std::strrchr(filename,'.');
  7125. if (!p || std::strchr(p,'/') || std::strchr(p,'\\')) { // No extension.
  7126. if (body) std::strcpy(body,filename);
  7127. return filename + std::strlen(filename);
  7128. }
  7129. const unsigned int l = (unsigned int)(p - filename);
  7130. if (body) { if (l) std::memcpy(body,filename,l); body[l] = 0; }
  7131. return p + 1;
  7132. }
  7133. // Generate a numbered version of a filename.
  7134. inline char* number_filename(const char *const filename, const int number,
  7135. const unsigned int digits, char *const str);
  7136. //! Read data from file.
  7137. /**
  7138. \param[out] ptr Pointer to memory buffer that will contain the binary data read from file.
  7139. \param nmemb Number of elements to read.
  7140. \param stream File to read data from.
  7141. \return Number of read elements.
  7142. \note Same as <tt>std::fread()</tt> but may display warning message if all elements could not be read.
  7143. **/
  7144. template<typename T>
  7145. inline size_t fread(T *const ptr, const size_t nmemb, std::FILE *stream) {
  7146. if (!ptr || !stream)
  7147. throw CImgArgumentException("cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.",
  7148. nmemb,cimg::type<T>::string(),nmemb>1?"s":"",stream,ptr);
  7149. if (!nmemb) return 0;
  7150. const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);
  7151. size_t to_read = nmemb, al_read = 0, l_to_read = 0, l_al_read = 0;
  7152. do {
  7153. l_to_read = (to_read*sizeof(T))<wlimitT?to_read:wlimit;
  7154. l_al_read = std::fread((void*)(ptr + al_read),sizeof(T),l_to_read,stream);
  7155. al_read+=l_al_read;
  7156. to_read-=l_al_read;
  7157. } while (l_to_read==l_al_read && to_read>0);
  7158. if (to_read>0)
  7159. warn("cimg::fread(): Only %lu/%lu elements could be read from file.",
  7160. (unsigned long)al_read,(unsigned long)nmemb);
  7161. return al_read;
  7162. }
  7163. //! Write data to file.
  7164. /**
  7165. \param ptr Pointer to memory buffer containing the binary data to write on file.
  7166. \param nmemb Number of elements to write.
  7167. \param[out] stream File to write data on.
  7168. \return Number of written elements.
  7169. \note Similar to <tt>std::fwrite</tt> but may display warning messages if all elements could not be written.
  7170. **/
  7171. template<typename T>
  7172. inline size_t fwrite(const T *ptr, const size_t nmemb, std::FILE *stream) {
  7173. if (!ptr || !stream)
  7174. throw CImgArgumentException("cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.",
  7175. nmemb,cimg::type<T>::string(),nmemb>1?"s":"",ptr,stream);
  7176. if (!nmemb) return 0;
  7177. const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);
  7178. size_t to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0;
  7179. do {
  7180. l_to_write = (to_write*sizeof(T))<wlimitT?to_write:wlimit;
  7181. l_al_write = std::fwrite((void*)(ptr + al_write),sizeof(T),l_to_write,stream);
  7182. al_write+=l_al_write;
  7183. to_write-=l_al_write;
  7184. } while (l_to_write==l_al_write && to_write>0);
  7185. if (to_write>0)
  7186. warn("cimg::fwrite(): Only %lu/%lu elements could be written in file.",
  7187. (unsigned long)al_write,(unsigned long)nmemb);
  7188. return al_write;
  7189. }
  7190. //! Create an empty file.
  7191. /**
  7192. \param file Input file (can be \c 0 if \c filename is set).
  7193. \param filename Filename, as a C-string (can be \c 0 if \c file is set).
  7194. **/
  7195. inline void fempty(std::FILE *const file, const char *const filename) {
  7196. if (!file && !filename)
  7197. throw CImgArgumentException("cimg::fempty(): Specified filename is (null).");
  7198. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  7199. if (!file) cimg::fclose(nfile);
  7200. }
  7201. // Try to guess format from an image file.
  7202. inline const char *ftype(std::FILE *const file, const char *const filename);
  7203. // Get or set load from network mode (can be { 0=disabled | 1=enabled }).
  7204. inline bool& network_mode(const bool value, const bool is_set) {
  7205. static bool mode = true;
  7206. if (is_set) { cimg::mutex(0); mode = value; cimg::mutex(0,0); }
  7207. return mode;
  7208. }
  7209. inline bool& network_mode() {
  7210. return network_mode(false,false);
  7211. }
  7212. // Load file from network as a local temporary file.
  7213. inline char *load_network(const char *const url, char *const filename_local,
  7214. const unsigned int timeout=0, const bool try_fallback=false,
  7215. const char *const referer=0);
  7216. //! Return options specified on the command line.
  7217. inline const char* option(const char *const name, const int argc, const char *const *const argv,
  7218. const char *const _default, const char *const usage, const bool reset_static) {
  7219. static bool first = true, visu = false;
  7220. if (reset_static) { first = true; return 0; }
  7221. const char *res = 0;
  7222. if (first) {
  7223. first = false;
  7224. visu = cimg::option("-h",argc,argv,(char*)0,(char*)0,false)!=0;
  7225. visu |= cimg::option("-help",argc,argv,(char*)0,(char*)0,false)!=0;
  7226. visu |= cimg::option("--help",argc,argv,(char*)0,(char*)0,false)!=0;
  7227. }
  7228. if (!name && visu) {
  7229. if (usage) {
  7230. std::fprintf(cimg::output(),"\n %s%s%s",cimg::t_red,cimg::basename(argv[0]),cimg::t_normal);
  7231. std::fprintf(cimg::output(),": %s",usage);
  7232. std::fprintf(cimg::output()," (%s, %s)\n\n",cimg_date,cimg_time);
  7233. }
  7234. if (_default) std::fprintf(cimg::output(),"%s\n",_default);
  7235. }
  7236. if (name) {
  7237. if (argc>0) {
  7238. int k = 0;
  7239. while (k<argc && std::strcmp(argv[k],name)) ++k;
  7240. res = (k++==argc?_default:(k==argc?argv[--k]:argv[k]));
  7241. } else res = _default;
  7242. if (visu && usage) std::fprintf(cimg::output()," %s%-16s%s %-24s %s%s%s\n",
  7243. cimg::t_bold,name,cimg::t_normal,res?res:"0",
  7244. cimg::t_green,usage,cimg::t_normal);
  7245. }
  7246. return res;
  7247. }
  7248. inline const char* option(const char *const name, const int argc, const char *const *const argv,
  7249. const char *const _default, const char *const usage=0) {
  7250. return option(name,argc,argv,_default,usage,false);
  7251. }
  7252. inline bool option(const char *const name, const int argc, const char *const *const argv,
  7253. const bool _default, const char *const usage=0) {
  7254. const char *const s = cimg::option(name,argc,argv,(char*)0);
  7255. const bool res = s?(cimg::strcasecmp(s,"false") && cimg::strcasecmp(s,"off") && cimg::strcasecmp(s,"0")):_default;
  7256. cimg::option(name,0,0,res?"true":"false",usage);
  7257. return res;
  7258. }
  7259. inline int option(const char *const name, const int argc, const char *const *const argv,
  7260. const int _default, const char *const usage=0) {
  7261. const char *const s = cimg::option(name,argc,argv,(char*)0);
  7262. const int res = s?std::atoi(s):_default;
  7263. char *const tmp = new char[256];
  7264. cimg_snprintf(tmp,256,"%d",res);
  7265. cimg::option(name,0,0,tmp,usage);
  7266. delete[] tmp;
  7267. return res;
  7268. }
  7269. inline char option(const char *const name, const int argc, const char *const *const argv,
  7270. const char _default, const char *const usage=0) {
  7271. const char *const s = cimg::option(name,argc,argv,(char*)0);
  7272. const char res = s?*s:_default;
  7273. char tmp[8];
  7274. *tmp = res; tmp[1] = 0;
  7275. cimg::option(name,0,0,tmp,usage);
  7276. return res;
  7277. }
  7278. inline float option(const char *const name, const int argc, const char *const *const argv,
  7279. const float _default, const char *const usage=0) {
  7280. const char *const s = cimg::option(name,argc,argv,(char*)0);
  7281. const float res = s?(float)cimg::atof(s):_default;
  7282. char *const tmp = new char[256];
  7283. cimg_snprintf(tmp,256,"%g",res);
  7284. cimg::option(name,0,0,tmp,usage);
  7285. delete[] tmp;
  7286. return res;
  7287. }
  7288. inline double option(const char *const name, const int argc, const char *const *const argv,
  7289. const double _default, const char *const usage=0) {
  7290. const char *const s = cimg::option(name,argc,argv,(char*)0);
  7291. const double res = s?cimg::atof(s):_default;
  7292. char *const tmp = new char[256];
  7293. cimg_snprintf(tmp,256,"%g",res);
  7294. cimg::option(name,0,0,tmp,usage);
  7295. delete[] tmp;
  7296. return res;
  7297. }
  7298. //! Print information about \CImg environment variables.
  7299. /**
  7300. \note Output is done on the default output stream.
  7301. **/
  7302. inline void info() {
  7303. std::fprintf(cimg::output(),"\n %s%sCImg Library %u.%u.%u%s, compiled %s ( %s ) with the following flags:\n\n",
  7304. cimg::t_red,cimg::t_bold,cimg_version/100,(cimg_version/10)%10,cimg_version%10,
  7305. cimg::t_normal,cimg_date,cimg_time);
  7306. std::fprintf(cimg::output()," > Operating System: %s%-13s%s %s('cimg_OS'=%d)%s\n",
  7307. cimg::t_bold,
  7308. cimg_OS==1?"Unix":(cimg_OS==2?"Windows":"Unknown"),
  7309. cimg::t_normal,cimg::t_green,
  7310. cimg_OS,
  7311. cimg::t_normal);
  7312. std::fprintf(cimg::output()," > CPU endianness: %s%s Endian%s\n",
  7313. cimg::t_bold,
  7314. cimg::endianness()?"Big":"Little",
  7315. cimg::t_normal);
  7316. std::fprintf(cimg::output()," > Verbosity mode: %s%-13s%s %s('cimg_verbosity'=%d)%s\n",
  7317. cimg::t_bold,
  7318. cimg_verbosity==0?"Quiet":
  7319. cimg_verbosity==1?"Console":
  7320. cimg_verbosity==2?"Dialog":
  7321. cimg_verbosity==3?"Console+Warnings":"Dialog+Warnings",
  7322. cimg::t_normal,cimg::t_green,
  7323. cimg_verbosity,
  7324. cimg::t_normal);
  7325. std::fprintf(cimg::output()," > Stricts warnings: %s%-13s%s %s('cimg_strict_warnings' %s)%s\n",
  7326. cimg::t_bold,
  7327. #ifdef cimg_strict_warnings
  7328. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7329. #else
  7330. "No",cimg::t_normal,cimg::t_green,"undefined",
  7331. #endif
  7332. cimg::t_normal);
  7333. std::fprintf(cimg::output()," > Support for C++11: %s%-13s%s %s('cimg_use_cpp11'=%d)%s\n",
  7334. cimg::t_bold,
  7335. cimg_use_cpp11?"Yes":"No",
  7336. cimg::t_normal,cimg::t_green,
  7337. (int)cimg_use_cpp11,
  7338. cimg::t_normal);
  7339. std::fprintf(cimg::output()," > Using VT100 messages: %s%-13s%s %s('cimg_use_vt100' %s)%s\n",
  7340. cimg::t_bold,
  7341. #ifdef cimg_use_vt100
  7342. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7343. #else
  7344. "No",cimg::t_normal,cimg::t_green,"undefined",
  7345. #endif
  7346. cimg::t_normal);
  7347. std::fprintf(cimg::output()," > Display type: %s%-13s%s %s('cimg_display'=%d)%s\n",
  7348. cimg::t_bold,
  7349. cimg_display==0?"No display":cimg_display==1?"X11":cimg_display==2?"Windows GDI":"Unknown",
  7350. cimg::t_normal,cimg::t_green,
  7351. (int)cimg_display,
  7352. cimg::t_normal);
  7353. #if cimg_display==1
  7354. std::fprintf(cimg::output()," > Using XShm for X11: %s%-13s%s %s('cimg_use_xshm' %s)%s\n",
  7355. cimg::t_bold,
  7356. #ifdef cimg_use_xshm
  7357. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7358. #else
  7359. "No",cimg::t_normal,cimg::t_green,"undefined",
  7360. #endif
  7361. cimg::t_normal);
  7362. std::fprintf(cimg::output()," > Using XRand for X11: %s%-13s%s %s('cimg_use_xrandr' %s)%s\n",
  7363. cimg::t_bold,
  7364. #ifdef cimg_use_xrandr
  7365. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7366. #else
  7367. "No",cimg::t_normal,cimg::t_green,"undefined",
  7368. #endif
  7369. cimg::t_normal);
  7370. #endif
  7371. std::fprintf(cimg::output()," > Using OpenMP: %s%-13s%s %s('cimg_use_openmp' %s)%s\n",
  7372. cimg::t_bold,
  7373. #if cimg_use_openmp!=0
  7374. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7375. #else
  7376. "No",cimg::t_normal,cimg::t_green,"undefined",
  7377. #endif
  7378. cimg::t_normal);
  7379. std::fprintf(cimg::output()," > Using PNG library: %s%-13s%s %s('cimg_use_png' %s)%s\n",
  7380. cimg::t_bold,
  7381. #ifdef cimg_use_png
  7382. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7383. #else
  7384. "No",cimg::t_normal,cimg::t_green,"undefined",
  7385. #endif
  7386. cimg::t_normal);
  7387. std::fprintf(cimg::output()," > Using JPEG library: %s%-13s%s %s('cimg_use_jpeg' %s)%s\n",
  7388. cimg::t_bold,
  7389. #ifdef cimg_use_jpeg
  7390. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7391. #else
  7392. "No",cimg::t_normal,cimg::t_green,"undefined",
  7393. #endif
  7394. cimg::t_normal);
  7395. std::fprintf(cimg::output()," > Using TIFF library: %s%-13s%s %s('cimg_use_tiff' %s)%s\n",
  7396. cimg::t_bold,
  7397. #ifdef cimg_use_tiff
  7398. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7399. #else
  7400. "No",cimg::t_normal,cimg::t_green,"undefined",
  7401. #endif
  7402. cimg::t_normal);
  7403. std::fprintf(cimg::output()," > Using Magick++ library: %s%-13s%s %s('cimg_use_magick' %s)%s\n",
  7404. cimg::t_bold,
  7405. #ifdef cimg_use_magick
  7406. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7407. #else
  7408. "No",cimg::t_normal,cimg::t_green,"undefined",
  7409. #endif
  7410. cimg::t_normal);
  7411. std::fprintf(cimg::output()," > Using FFTW3 library: %s%-13s%s %s('cimg_use_fftw3' %s)%s\n",
  7412. cimg::t_bold,
  7413. #ifdef cimg_use_fftw3
  7414. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7415. #else
  7416. "No",cimg::t_normal,cimg::t_green,"undefined",
  7417. #endif
  7418. cimg::t_normal);
  7419. std::fprintf(cimg::output()," > Using LAPACK library: %s%-13s%s %s('cimg_use_lapack' %s)%s\n",
  7420. cimg::t_bold,
  7421. #ifdef cimg_use_lapack
  7422. "Yes",cimg::t_normal,cimg::t_green,"defined",
  7423. #else
  7424. "No",cimg::t_normal,cimg::t_green,"undefined",
  7425. #endif
  7426. cimg::t_normal);
  7427. char *const tmp = new char[1024];
  7428. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::curl_path());
  7429. std::fprintf(cimg::output()," > Path of 'curl': %s%-13s%s\n",
  7430. cimg::t_bold,
  7431. tmp,
  7432. cimg::t_normal);
  7433. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::dcraw_path());
  7434. std::fprintf(cimg::output()," > Path of 'dcraw': %s%-13s%s\n",
  7435. cimg::t_bold,
  7436. tmp,
  7437. cimg::t_normal);
  7438. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::ffmpeg_path());
  7439. std::fprintf(cimg::output()," > Path of 'ffmpeg': %s%-13s%s\n",
  7440. cimg::t_bold,
  7441. tmp,
  7442. cimg::t_normal);
  7443. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::graphicsmagick_path());
  7444. std::fprintf(cimg::output()," > Path of 'graphicsmagick': %s%-13s%s\n",
  7445. cimg::t_bold,
  7446. tmp,
  7447. cimg::t_normal);
  7448. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::gunzip_path());
  7449. std::fprintf(cimg::output()," > Path of 'gunzip': %s%-13s%s\n",
  7450. cimg::t_bold,
  7451. tmp,
  7452. cimg::t_normal);
  7453. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::gzip_path());
  7454. std::fprintf(cimg::output()," > Path of 'gzip': %s%-13s%s\n",
  7455. cimg::t_bold,
  7456. tmp,
  7457. cimg::t_normal);
  7458. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::imagemagick_path());
  7459. std::fprintf(cimg::output()," > Path of 'imagemagick': %s%-13s%s\n",
  7460. cimg::t_bold,
  7461. tmp,
  7462. cimg::t_normal);
  7463. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::medcon_path());
  7464. std::fprintf(cimg::output()," > Path of 'medcon': %s%-13s%s\n",
  7465. cimg::t_bold,
  7466. tmp,
  7467. cimg::t_normal);
  7468. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::temporary_path());
  7469. std::fprintf(cimg::output()," > Temporary path: %s%-13s%s\n",
  7470. cimg::t_bold,
  7471. tmp,
  7472. cimg::t_normal);
  7473. cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::wget_path());
  7474. std::fprintf(cimg::output()," > Path of 'wget': %s%-13s%s\n",
  7475. cimg::t_bold,
  7476. tmp,
  7477. cimg::t_normal);
  7478. std::fprintf(cimg::output(),"\n");
  7479. delete[] tmp;
  7480. }
  7481. // Declare LAPACK function signatures if LAPACK support is enabled.
  7482. #ifdef cimg_use_lapack
  7483. template<typename T>
  7484. inline void getrf(int &N, T *lapA, int *IPIV, int &INFO) {
  7485. dgetrf_(&N,&N,lapA,&N,IPIV,&INFO);
  7486. }
  7487. inline void getrf(int &N, float *lapA, int *IPIV, int &INFO) {
  7488. sgetrf_(&N,&N,lapA,&N,IPIV,&INFO);
  7489. }
  7490. template<typename T>
  7491. inline void getri(int &N, T *lapA, int *IPIV, T* WORK, int &LWORK, int &INFO) {
  7492. dgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO);
  7493. }
  7494. inline void getri(int &N, float *lapA, int *IPIV, float* WORK, int &LWORK, int &INFO) {
  7495. sgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO);
  7496. }
  7497. template<typename T>
  7498. inline void gesvd(char &JOB, int &M, int &N, T *lapA, int &MN,
  7499. T *lapS, T *lapU, T *lapV, T *WORK, int &LWORK, int &INFO) {
  7500. dgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO);
  7501. }
  7502. inline void gesvd(char &JOB, int &M, int &N, float *lapA, int &MN,
  7503. float *lapS, float *lapU, float *lapV, float *WORK, int &LWORK, int &INFO) {
  7504. sgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO);
  7505. }
  7506. template<typename T>
  7507. inline void getrs(char &TRANS, int &N, T *lapA, int *IPIV, T *lapB, int &INFO) {
  7508. int one = 1;
  7509. dgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO);
  7510. }
  7511. inline void getrs(char &TRANS, int &N, float *lapA, int *IPIV, float *lapB, int &INFO) {
  7512. int one = 1;
  7513. sgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO);
  7514. }
  7515. template<typename T>
  7516. inline void syev(char &JOB, char &UPLO, int &N, T *lapA, T *lapW, T *WORK, int &LWORK, int &INFO) {
  7517. dsyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO);
  7518. }
  7519. inline void syev(char &JOB, char &UPLO, int &N, float *lapA, float *lapW, float *WORK, int &LWORK, int &INFO) {
  7520. ssyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO);
  7521. }
  7522. template<typename T>
  7523. inline void sgels(char & TRANS, int &M, int &N, int &NRHS, T* lapA, int &LDA,
  7524. T* lapB, int &LDB, T* WORK, int &LWORK, int &INFO) {
  7525. dgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO);
  7526. }
  7527. inline void sgels(char & TRANS, int &M, int &N, int &NRHS, float* lapA, int &LDA,
  7528. float* lapB, int &LDB, float* WORK, int &LWORK, int &INFO) {
  7529. sgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO);
  7530. }
  7531. #endif
  7532. } // namespace cimg { ...
  7533. /*------------------------------------------------
  7534. #
  7535. #
  7536. # Definition of mathematical operators and
  7537. # external functions.
  7538. #
  7539. #
  7540. -------------------------------------------------*/
  7541. #define _cimg_create_operator(typ) \
  7542. template<typename T> \
  7543. inline CImg<typename cimg::superset<T,typ>::type> operator+(const typ val, const CImg<T>& img) { \
  7544. return img + val; \
  7545. } \
  7546. template<typename T> \
  7547. inline CImg<typename cimg::superset<T,typ>::type> operator-(const typ val, const CImg<T>& img) { \
  7548. typedef typename cimg::superset<T,typ>::type Tt; \
  7549. return CImg<Tt>(img._width,img._height,img._depth,img._spectrum,val)-=img; \
  7550. } \
  7551. template<typename T> \
  7552. inline CImg<typename cimg::superset<T,typ>::type> operator*(const typ val, const CImg<T>& img) { \
  7553. return img*val; \
  7554. } \
  7555. template<typename T> \
  7556. inline CImg<typename cimg::superset<T,typ>::type> operator/(const typ val, const CImg<T>& img) { \
  7557. return val*img.get_invert(); \
  7558. } \
  7559. template<typename T> \
  7560. inline CImg<typename cimg::superset<T,typ>::type> operator&(const typ val, const CImg<T>& img) { \
  7561. return img & val; \
  7562. } \
  7563. template<typename T> \
  7564. inline CImg<typename cimg::superset<T,typ>::type> operator|(const typ val, const CImg<T>& img) { \
  7565. return img | val; \
  7566. } \
  7567. template<typename T> \
  7568. inline CImg<typename cimg::superset<T,typ>::type> operator^(const typ val, const CImg<T>& img) { \
  7569. return img ^ val; \
  7570. } \
  7571. template<typename T> \
  7572. inline bool operator==(const typ val, const CImg<T>& img) { \
  7573. return img == val; \
  7574. } \
  7575. template<typename T> \
  7576. inline bool operator!=(const typ val, const CImg<T>& img) { \
  7577. return img != val; \
  7578. }
  7579. _cimg_create_operator(bool)
  7580. _cimg_create_operator(unsigned char)
  7581. _cimg_create_operator(char)
  7582. _cimg_create_operator(signed char)
  7583. _cimg_create_operator(unsigned short)
  7584. _cimg_create_operator(short)
  7585. _cimg_create_operator(unsigned int)
  7586. _cimg_create_operator(int)
  7587. _cimg_create_operator(cimg_uint64)
  7588. _cimg_create_operator(cimg_int64)
  7589. _cimg_create_operator(float)
  7590. _cimg_create_operator(double)
  7591. _cimg_create_operator(long double)
  7592. template<typename T>
  7593. inline CImg<_cimg_Tfloat> operator+(const char *const expression, const CImg<T>& img) {
  7594. return img + expression;
  7595. }
  7596. template<typename T>
  7597. inline CImg<_cimg_Tfloat> operator-(const char *const expression, const CImg<T>& img) {
  7598. return CImg<_cimg_Tfloat>(img,false).fill(expression,true)-=img;
  7599. }
  7600. template<typename T>
  7601. inline CImg<_cimg_Tfloat> operator*(const char *const expression, const CImg<T>& img) {
  7602. return img*expression;
  7603. }
  7604. template<typename T>
  7605. inline CImg<_cimg_Tfloat> operator/(const char *const expression, const CImg<T>& img) {
  7606. return expression*img.get_invert();
  7607. }
  7608. template<typename T>
  7609. inline CImg<T> operator&(const char *const expression, const CImg<T>& img) {
  7610. return img & expression;
  7611. }
  7612. template<typename T>
  7613. inline CImg<T> operator|(const char *const expression, const CImg<T>& img) {
  7614. return img | expression;
  7615. }
  7616. template<typename T>
  7617. inline CImg<T> operator^(const char *const expression, const CImg<T>& img) {
  7618. return img ^ expression;
  7619. }
  7620. template<typename T>
  7621. inline bool operator==(const char *const expression, const CImg<T>& img) {
  7622. return img==expression;
  7623. }
  7624. template<typename T>
  7625. inline bool operator!=(const char *const expression, const CImg<T>& img) {
  7626. return img!=expression;
  7627. }
  7628. template<typename T>
  7629. inline CImg<T> transpose(const CImg<T>& instance) {
  7630. return instance.get_transpose();
  7631. }
  7632. template<typename T>
  7633. inline CImg<_cimg_Tfloat> invert(const CImg<T>& instance, const bool use_LU=true) {
  7634. return instance.get_invert(use_LU);
  7635. }
  7636. template<typename T>
  7637. inline CImg<_cimg_Tfloat> pseudoinvert(const CImg<T>& instance, const bool use_LU=false) {
  7638. return instance.get_pseudoinvert(use_LU);
  7639. }
  7640. #define _cimg_create_pointwise_function(name) \
  7641. template<typename T> \
  7642. inline CImg<_cimg_Tfloat> name(const CImg<T>& instance) { \
  7643. return instance.get_##name(); \
  7644. }
  7645. _cimg_create_pointwise_function(sqr)
  7646. _cimg_create_pointwise_function(sqrt)
  7647. _cimg_create_pointwise_function(erf)
  7648. _cimg_create_pointwise_function(exp)
  7649. _cimg_create_pointwise_function(log)
  7650. _cimg_create_pointwise_function(log2)
  7651. _cimg_create_pointwise_function(log10)
  7652. _cimg_create_pointwise_function(abs)
  7653. _cimg_create_pointwise_function(sign)
  7654. _cimg_create_pointwise_function(cos)
  7655. _cimg_create_pointwise_function(sin)
  7656. _cimg_create_pointwise_function(sinc)
  7657. _cimg_create_pointwise_function(tan)
  7658. _cimg_create_pointwise_function(acos)
  7659. _cimg_create_pointwise_function(asin)
  7660. _cimg_create_pointwise_function(atan)
  7661. _cimg_create_pointwise_function(cosh)
  7662. _cimg_create_pointwise_function(sinh)
  7663. _cimg_create_pointwise_function(tanh)
  7664. _cimg_create_pointwise_function(acosh)
  7665. _cimg_create_pointwise_function(asinh)
  7666. _cimg_create_pointwise_function(atanh)
  7667. /*-----------------------------------
  7668. #
  7669. # Define the CImgDisplay structure
  7670. #
  7671. ----------------------------------*/
  7672. //! Allow the creation of windows, display images on them and manage user events (keyboard, mouse and windows events).
  7673. /**
  7674. CImgDisplay methods rely on a low-level graphic library to perform: it can be either \b X-Window
  7675. (X11, for Unix-based systems) or \b GDI32 (for Windows-based systems).
  7676. If both libraries are missing, CImgDisplay will not be able to display images on screen, and will enter
  7677. a minimal mode where warning messages will be outputted each time the program is trying to call one of the
  7678. CImgDisplay method.
  7679. The configuration variable \c cimg_display tells about the graphic library used.
  7680. It is set automatically by \CImg when one of these graphic libraries has been detected.
  7681. But, you can override its value if necessary. Valid choices are:
  7682. - 0: Disable display capabilities.
  7683. - 1: Use \b X-Window (X11) library.
  7684. - 2: Use \b GDI32 library.
  7685. Remember to link your program against \b X11 or \b GDI32 libraries if you use CImgDisplay.
  7686. **/
  7687. struct CImgDisplay {
  7688. cimg_uint64 _timer, _fps_frames, _fps_timer;
  7689. unsigned int _width, _height, _normalization;
  7690. float _fps_fps, _min, _max;
  7691. bool _is_fullscreen;
  7692. char *_title;
  7693. unsigned int _window_width, _window_height, _button, *_keys, *_released_keys;
  7694. int _window_x, _window_y, _mouse_x, _mouse_y, _wheel;
  7695. bool _is_closed, _is_resized, _is_moved, _is_event,
  7696. _is_keyESC, _is_keyF1, _is_keyF2, _is_keyF3, _is_keyF4, _is_keyF5, _is_keyF6, _is_keyF7,
  7697. _is_keyF8, _is_keyF9, _is_keyF10, _is_keyF11, _is_keyF12, _is_keyPAUSE, _is_key1, _is_key2,
  7698. _is_key3, _is_key4, _is_key5, _is_key6, _is_key7, _is_key8, _is_key9, _is_key0,
  7699. _is_keyBACKSPACE, _is_keyINSERT, _is_keyHOME, _is_keyPAGEUP, _is_keyTAB, _is_keyQ, _is_keyW, _is_keyE,
  7700. _is_keyR, _is_keyT, _is_keyY, _is_keyU, _is_keyI, _is_keyO, _is_keyP, _is_keyDELETE,
  7701. _is_keyEND, _is_keyPAGEDOWN, _is_keyCAPSLOCK, _is_keyA, _is_keyS, _is_keyD, _is_keyF, _is_keyG,
  7702. _is_keyH, _is_keyJ, _is_keyK, _is_keyL, _is_keyENTER, _is_keySHIFTLEFT, _is_keyZ, _is_keyX,
  7703. _is_keyC, _is_keyV, _is_keyB, _is_keyN, _is_keyM, _is_keySHIFTRIGHT, _is_keyARROWUP, _is_keyCTRLLEFT,
  7704. _is_keyAPPLEFT, _is_keyALT, _is_keySPACE, _is_keyALTGR, _is_keyAPPRIGHT, _is_keyMENU, _is_keyCTRLRIGHT,
  7705. _is_keyARROWLEFT, _is_keyARROWDOWN, _is_keyARROWRIGHT, _is_keyPAD0, _is_keyPAD1, _is_keyPAD2, _is_keyPAD3,
  7706. _is_keyPAD4, _is_keyPAD5, _is_keyPAD6, _is_keyPAD7, _is_keyPAD8, _is_keyPAD9, _is_keyPADADD, _is_keyPADSUB,
  7707. _is_keyPADMUL, _is_keyPADDIV;
  7708. //@}
  7709. //---------------------------
  7710. //
  7711. //! \name Plugins
  7712. //@{
  7713. //---------------------------
  7714. #ifdef cimgdisplay_plugin
  7715. #include cimgdisplay_plugin
  7716. #endif
  7717. #ifdef cimgdisplay_plugin1
  7718. #include cimgdisplay_plugin1
  7719. #endif
  7720. #ifdef cimgdisplay_plugin2
  7721. #include cimgdisplay_plugin2
  7722. #endif
  7723. #ifdef cimgdisplay_plugin3
  7724. #include cimgdisplay_plugin3
  7725. #endif
  7726. #ifdef cimgdisplay_plugin4
  7727. #include cimgdisplay_plugin4
  7728. #endif
  7729. #ifdef cimgdisplay_plugin5
  7730. #include cimgdisplay_plugin5
  7731. #endif
  7732. #ifdef cimgdisplay_plugin6
  7733. #include cimgdisplay_plugin6
  7734. #endif
  7735. #ifdef cimgdisplay_plugin7
  7736. #include cimgdisplay_plugin7
  7737. #endif
  7738. #ifdef cimgdisplay_plugin8
  7739. #include cimgdisplay_plugin8
  7740. #endif
  7741. //@}
  7742. //--------------------------------------------------------
  7743. //
  7744. //! \name Constructors / Destructor / Instance Management
  7745. //@{
  7746. //--------------------------------------------------------
  7747. //! Destructor.
  7748. /**
  7749. \note If the associated window is visible on the screen, it is closed by the call to the destructor.
  7750. **/
  7751. ~CImgDisplay() {
  7752. assign();
  7753. delete[] _keys;
  7754. delete[] _released_keys;
  7755. }
  7756. //! Construct an empty display.
  7757. /**
  7758. \note Constructing an empty CImgDisplay instance does not make a window appearing on the screen, until
  7759. display of valid data is performed.
  7760. \par Example
  7761. \code
  7762. CImgDisplay disp; // Does actually nothing
  7763. ...
  7764. disp.display(img); // Construct new window and display image in it
  7765. \endcode
  7766. **/
  7767. CImgDisplay():
  7768. _width(0),_height(0),_normalization(0),
  7769. _min(0),_max(0),
  7770. _is_fullscreen(false),
  7771. _title(0),
  7772. _window_width(0),_window_height(0),_button(0),
  7773. _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
  7774. _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
  7775. _mouse_x(-1),_mouse_y(-1),_wheel(0),
  7776. _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
  7777. assign();
  7778. }
  7779. //! Construct a display with specified dimensions.
  7780. /** \param width Window width.
  7781. \param height Window height.
  7782. \param title Window title.
  7783. \param normalization Normalization type
  7784. (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
  7785. \param is_fullscreen Tells if fullscreen mode is enabled.
  7786. \param is_closed Tells if associated window is initially visible or not.
  7787. \note A black background is initially displayed on the associated window.
  7788. **/
  7789. CImgDisplay(const unsigned int width, const unsigned int height,
  7790. const char *const title=0, const unsigned int normalization=3,
  7791. const bool is_fullscreen=false, const bool is_closed=false):
  7792. _width(0),_height(0),_normalization(0),
  7793. _min(0),_max(0),
  7794. _is_fullscreen(false),
  7795. _title(0),
  7796. _window_width(0),_window_height(0),_button(0),
  7797. _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
  7798. _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
  7799. _mouse_x(-1),_mouse_y(-1),_wheel(0),
  7800. _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
  7801. assign(width,height,title,normalization,is_fullscreen,is_closed);
  7802. }
  7803. //! Construct a display from an image.
  7804. /** \param img Image used as a model to create the window.
  7805. \param title Window title.
  7806. \param normalization Normalization type
  7807. (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
  7808. \param is_fullscreen Tells if fullscreen mode is enabled.
  7809. \param is_closed Tells if associated window is initially visible or not.
  7810. \note The pixels of the input image are initially displayed on the associated window.
  7811. **/
  7812. template<typename T>
  7813. explicit CImgDisplay(const CImg<T>& img,
  7814. const char *const title=0, const unsigned int normalization=3,
  7815. const bool is_fullscreen=false, const bool is_closed=false):
  7816. _width(0),_height(0),_normalization(0),
  7817. _min(0),_max(0),
  7818. _is_fullscreen(false),
  7819. _title(0),
  7820. _window_width(0),_window_height(0),_button(0),
  7821. _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
  7822. _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
  7823. _mouse_x(-1),_mouse_y(-1),_wheel(0),
  7824. _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
  7825. assign(img,title,normalization,is_fullscreen,is_closed);
  7826. }
  7827. //! Construct a display from an image list.
  7828. /** \param list The images list to display.
  7829. \param title Window title.
  7830. \param normalization Normalization type
  7831. (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
  7832. \param is_fullscreen Tells if fullscreen mode is enabled.
  7833. \param is_closed Tells if associated window is initially visible or not.
  7834. \note All images of the list, appended along the X-axis, are initially displayed on the associated window.
  7835. **/
  7836. template<typename T>
  7837. explicit CImgDisplay(const CImgList<T>& list,
  7838. const char *const title=0, const unsigned int normalization=3,
  7839. const bool is_fullscreen=false, const bool is_closed=false):
  7840. _width(0),_height(0),_normalization(0),
  7841. _min(0),_max(0),
  7842. _is_fullscreen(false),
  7843. _title(0),
  7844. _window_width(0),_window_height(0),_button(0),
  7845. _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
  7846. _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
  7847. _mouse_x(-1),_mouse_y(-1),_wheel(0),
  7848. _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
  7849. assign(list,title,normalization,is_fullscreen,is_closed);
  7850. }
  7851. //! Construct a display as a copy of an existing one.
  7852. /**
  7853. \param disp Display instance to copy.
  7854. \note The pixel buffer of the input window is initially displayed on the associated window.
  7855. **/
  7856. CImgDisplay(const CImgDisplay& disp):
  7857. _width(0),_height(0),_normalization(0),
  7858. _min(0),_max(0),
  7859. _is_fullscreen(false),
  7860. _title(0),
  7861. _window_width(0),_window_height(0),_button(0),
  7862. _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
  7863. _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
  7864. _mouse_x(-1),_mouse_y(-1),_wheel(0),
  7865. _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
  7866. assign(disp);
  7867. }
  7868. //! Take a screenshot.
  7869. /**
  7870. \param[out] img Output screenshot. Can be empty on input
  7871. **/
  7872. template<typename T>
  7873. static void screenshot(CImg<T>& img) {
  7874. return screenshot(0,0,cimg::type<int>::max(),cimg::type<int>::max(),img);
  7875. }
  7876. #if cimg_display==0
  7877. static void _no_display_exception() {
  7878. throw CImgDisplayException("CImgDisplay(): No display available.");
  7879. }
  7880. //! Destructor - Empty constructor \inplace.
  7881. /**
  7882. \note Replace the current instance by an empty display.
  7883. **/
  7884. CImgDisplay& assign() {
  7885. return flush();
  7886. }
  7887. //! Construct a display with specified dimensions \inplace.
  7888. /**
  7889. **/
  7890. CImgDisplay& assign(const unsigned int width, const unsigned int height,
  7891. const char *const title=0, const unsigned int normalization=3,
  7892. const bool is_fullscreen=false, const bool is_closed=false) {
  7893. cimg::unused(width,height,title,normalization,is_fullscreen,is_closed);
  7894. _no_display_exception();
  7895. return assign();
  7896. }
  7897. //! Construct a display from an image \inplace.
  7898. /**
  7899. **/
  7900. template<typename T>
  7901. CImgDisplay& assign(const CImg<T>& img,
  7902. const char *const title=0, const unsigned int normalization=3,
  7903. const bool is_fullscreen=false, const bool is_closed=false) {
  7904. _no_display_exception();
  7905. return assign(img._width,img._height,title,normalization,is_fullscreen,is_closed);
  7906. }
  7907. //! Construct a display from an image list \inplace.
  7908. /**
  7909. **/
  7910. template<typename T>
  7911. CImgDisplay& assign(const CImgList<T>& list,
  7912. const char *const title=0, const unsigned int normalization=3,
  7913. const bool is_fullscreen=false, const bool is_closed=false) {
  7914. _no_display_exception();
  7915. return assign(list._width,list._width,title,normalization,is_fullscreen,is_closed);
  7916. }
  7917. //! Construct a display as a copy of another one \inplace.
  7918. /**
  7919. **/
  7920. CImgDisplay& assign(const CImgDisplay &disp) {
  7921. _no_display_exception();
  7922. return assign(disp._width,disp._height);
  7923. }
  7924. #endif
  7925. //! Return a reference to an empty display.
  7926. /**
  7927. \note Can be useful for writing function prototypes where one of the argument (of type CImgDisplay&)
  7928. must have a default value.
  7929. \par Example
  7930. \code
  7931. void foo(CImgDisplay& disp=CImgDisplay::empty());
  7932. \endcode
  7933. **/
  7934. static CImgDisplay& empty() {
  7935. static CImgDisplay _empty;
  7936. return _empty.assign();
  7937. }
  7938. //! Return a reference to an empty display \const.
  7939. static const CImgDisplay& const_empty() {
  7940. static const CImgDisplay _empty;
  7941. return _empty;
  7942. }
  7943. #define cimg_fitscreen(dx,dy,dz) CImgDisplay::_fitscreen(dx,dy,dz,-25,-85,false), \
  7944. CImgDisplay::_fitscreen(dx,dy,dz,-25,-85,true)
  7945. static unsigned int _fitscreen(const unsigned int dx, const unsigned int dy, const unsigned int dz,
  7946. const int dmin, const int dmax, const bool return_y) {
  7947. const int
  7948. u = CImgDisplay::screen_width(),
  7949. v = CImgDisplay::screen_height();
  7950. const float
  7951. mw = dmin<0?cimg::round(u*-dmin/100.f):(float)dmin,
  7952. mh = dmin<0?cimg::round(v*-dmin/100.f):(float)dmin,
  7953. Mw = dmax<0?cimg::round(u*-dmax/100.f):(float)dmax,
  7954. Mh = dmax<0?cimg::round(v*-dmax/100.f):(float)dmax;
  7955. float
  7956. w = (float)std::max(1U,dx),
  7957. h = (float)std::max(1U,dy);
  7958. if (dz>1) { w+=dz; h+=dz; }
  7959. if (w<mw) { h = h*mw/w; w = mw; }
  7960. if (h<mh) { w = w*mh/h; h = mh; }
  7961. if (w>Mw) { h = h*Mw/w; w = Mw; }
  7962. if (h>Mh) { w = w*Mh/h; h = Mh; }
  7963. if (w<mw) w = mw;
  7964. if (h<mh) h = mh;
  7965. return std::max(1U,(unsigned int)cimg::round(return_y?h:w));
  7966. }
  7967. //@}
  7968. //------------------------------------------
  7969. //
  7970. //! \name Overloaded Operators
  7971. //@{
  7972. //------------------------------------------
  7973. //! Display image on associated window.
  7974. /**
  7975. \note <tt>disp = img</tt> is equivalent to <tt>disp.display(img)</tt>.
  7976. **/
  7977. template<typename t>
  7978. CImgDisplay& operator=(const CImg<t>& img) {
  7979. return display(img);
  7980. }
  7981. //! Display list of images on associated window.
  7982. /**
  7983. \note <tt>disp = list</tt> is equivalent to <tt>disp.display(list)</tt>.
  7984. **/
  7985. template<typename t>
  7986. CImgDisplay& operator=(const CImgList<t>& list) {
  7987. return display(list);
  7988. }
  7989. //! Construct a display as a copy of another one \inplace.
  7990. /**
  7991. \note Equivalent to assign(const CImgDisplay&).
  7992. **/
  7993. CImgDisplay& operator=(const CImgDisplay& disp) {
  7994. return assign(disp);
  7995. }
  7996. //! Return \c false if display is empty, \c true otherwise.
  7997. /**
  7998. \note <tt>if (disp) { ... }</tt> is equivalent to <tt>if (!disp.is_empty()) { ... }</tt>.
  7999. **/
  8000. operator bool() const {
  8001. return !is_empty();
  8002. }
  8003. //@}
  8004. //------------------------------------------
  8005. //
  8006. //! \name Instance Checking
  8007. //@{
  8008. //------------------------------------------
  8009. //! Return \c true if display is empty, \c false otherwise.
  8010. /**
  8011. **/
  8012. bool is_empty() const {
  8013. return !(_width && _height);
  8014. }
  8015. //! Return \c true if display is closed (i.e. not visible on the screen), \c false otherwise.
  8016. /**
  8017. \note
  8018. - When a user physically closes the associated window, the display is set to closed.
  8019. - A closed display is not destroyed. Its associated window can be show again on the screen using show().
  8020. **/
  8021. bool is_closed() const {
  8022. return _is_closed;
  8023. }
  8024. //! Return \c true if display is visible (i.e. not closed by the user), \c false otherwise.
  8025. bool is_visible() const {
  8026. return !is_closed();
  8027. }
  8028. //! Return \c true if associated window has been resized on the screen, \c false otherwise.
  8029. /**
  8030. **/
  8031. bool is_resized() const {
  8032. return _is_resized;
  8033. }
  8034. //! Return \c true if associated window has been moved on the screen, \c false otherwise.
  8035. /**
  8036. **/
  8037. bool is_moved() const {
  8038. return _is_moved;
  8039. }
  8040. //! Return \c true if any event has occurred on the associated window, \c false otherwise.
  8041. /**
  8042. **/
  8043. bool is_event() const {
  8044. return _is_event;
  8045. }
  8046. //! Return \c true if current display is in fullscreen mode, \c false otherwise.
  8047. /**
  8048. **/
  8049. bool is_fullscreen() const {
  8050. return _is_fullscreen;
  8051. }
  8052. //! Return \c true if any key is being pressed on the associated window, \c false otherwise.
  8053. /**
  8054. \note The methods below do the same only for specific keys.
  8055. **/
  8056. bool is_key() const {
  8057. return _is_keyESC || _is_keyF1 || _is_keyF2 || _is_keyF3 ||
  8058. _is_keyF4 || _is_keyF5 || _is_keyF6 || _is_keyF7 ||
  8059. _is_keyF8 || _is_keyF9 || _is_keyF10 || _is_keyF11 ||
  8060. _is_keyF12 || _is_keyPAUSE || _is_key1 || _is_key2 ||
  8061. _is_key3 || _is_key4 || _is_key5 || _is_key6 ||
  8062. _is_key7 || _is_key8 || _is_key9 || _is_key0 ||
  8063. _is_keyBACKSPACE || _is_keyINSERT || _is_keyHOME ||
  8064. _is_keyPAGEUP || _is_keyTAB || _is_keyQ || _is_keyW ||
  8065. _is_keyE || _is_keyR || _is_keyT || _is_keyY ||
  8066. _is_keyU || _is_keyI || _is_keyO || _is_keyP ||
  8067. _is_keyDELETE || _is_keyEND || _is_keyPAGEDOWN ||
  8068. _is_keyCAPSLOCK || _is_keyA || _is_keyS || _is_keyD ||
  8069. _is_keyF || _is_keyG || _is_keyH || _is_keyJ ||
  8070. _is_keyK || _is_keyL || _is_keyENTER ||
  8071. _is_keySHIFTLEFT || _is_keyZ || _is_keyX || _is_keyC ||
  8072. _is_keyV || _is_keyB || _is_keyN || _is_keyM ||
  8073. _is_keySHIFTRIGHT || _is_keyARROWUP || _is_keyCTRLLEFT ||
  8074. _is_keyAPPLEFT || _is_keyALT || _is_keySPACE || _is_keyALTGR ||
  8075. _is_keyAPPRIGHT || _is_keyMENU || _is_keyCTRLRIGHT ||
  8076. _is_keyARROWLEFT || _is_keyARROWDOWN || _is_keyARROWRIGHT ||
  8077. _is_keyPAD0 || _is_keyPAD1 || _is_keyPAD2 ||
  8078. _is_keyPAD3 || _is_keyPAD4 || _is_keyPAD5 ||
  8079. _is_keyPAD6 || _is_keyPAD7 || _is_keyPAD8 ||
  8080. _is_keyPAD9 || _is_keyPADADD || _is_keyPADSUB ||
  8081. _is_keyPADMUL || _is_keyPADDIV;
  8082. }
  8083. //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise.
  8084. /**
  8085. \param keycode Keycode to test.
  8086. \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8087. your code stay portable (see cimg::keyESC).
  8088. \par Example
  8089. \code
  8090. CImgDisplay disp(400,400);
  8091. while (!disp.is_closed()) {
  8092. if (disp.key(cimg::keyTAB)) { ... } // Equivalent to 'if (disp.is_keyTAB())'
  8093. disp.wait();
  8094. }
  8095. \endcode
  8096. **/
  8097. bool is_key(const unsigned int keycode) const {
  8098. #define _cimg_iskey_test(k) if (keycode==cimg::key##k) return _is_key##k;
  8099. _cimg_iskey_test(ESC); _cimg_iskey_test(F1); _cimg_iskey_test(F2); _cimg_iskey_test(F3);
  8100. _cimg_iskey_test(F4); _cimg_iskey_test(F5); _cimg_iskey_test(F6); _cimg_iskey_test(F7);
  8101. _cimg_iskey_test(F8); _cimg_iskey_test(F9); _cimg_iskey_test(F10); _cimg_iskey_test(F11);
  8102. _cimg_iskey_test(F12); _cimg_iskey_test(PAUSE); _cimg_iskey_test(1); _cimg_iskey_test(2);
  8103. _cimg_iskey_test(3); _cimg_iskey_test(4); _cimg_iskey_test(5); _cimg_iskey_test(6);
  8104. _cimg_iskey_test(7); _cimg_iskey_test(8); _cimg_iskey_test(9); _cimg_iskey_test(0);
  8105. _cimg_iskey_test(BACKSPACE); _cimg_iskey_test(INSERT); _cimg_iskey_test(HOME);
  8106. _cimg_iskey_test(PAGEUP); _cimg_iskey_test(TAB); _cimg_iskey_test(Q); _cimg_iskey_test(W);
  8107. _cimg_iskey_test(E); _cimg_iskey_test(R); _cimg_iskey_test(T); _cimg_iskey_test(Y);
  8108. _cimg_iskey_test(U); _cimg_iskey_test(I); _cimg_iskey_test(O); _cimg_iskey_test(P);
  8109. _cimg_iskey_test(DELETE); _cimg_iskey_test(END); _cimg_iskey_test(PAGEDOWN);
  8110. _cimg_iskey_test(CAPSLOCK); _cimg_iskey_test(A); _cimg_iskey_test(S); _cimg_iskey_test(D);
  8111. _cimg_iskey_test(F); _cimg_iskey_test(G); _cimg_iskey_test(H); _cimg_iskey_test(J);
  8112. _cimg_iskey_test(K); _cimg_iskey_test(L); _cimg_iskey_test(ENTER);
  8113. _cimg_iskey_test(SHIFTLEFT); _cimg_iskey_test(Z); _cimg_iskey_test(X); _cimg_iskey_test(C);
  8114. _cimg_iskey_test(V); _cimg_iskey_test(B); _cimg_iskey_test(N); _cimg_iskey_test(M);
  8115. _cimg_iskey_test(SHIFTRIGHT); _cimg_iskey_test(ARROWUP); _cimg_iskey_test(CTRLLEFT);
  8116. _cimg_iskey_test(APPLEFT); _cimg_iskey_test(ALT); _cimg_iskey_test(SPACE); _cimg_iskey_test(ALTGR);
  8117. _cimg_iskey_test(APPRIGHT); _cimg_iskey_test(MENU); _cimg_iskey_test(CTRLRIGHT);
  8118. _cimg_iskey_test(ARROWLEFT); _cimg_iskey_test(ARROWDOWN); _cimg_iskey_test(ARROWRIGHT);
  8119. _cimg_iskey_test(PAD0); _cimg_iskey_test(PAD1); _cimg_iskey_test(PAD2);
  8120. _cimg_iskey_test(PAD3); _cimg_iskey_test(PAD4); _cimg_iskey_test(PAD5);
  8121. _cimg_iskey_test(PAD6); _cimg_iskey_test(PAD7); _cimg_iskey_test(PAD8);
  8122. _cimg_iskey_test(PAD9); _cimg_iskey_test(PADADD); _cimg_iskey_test(PADSUB);
  8123. _cimg_iskey_test(PADMUL); _cimg_iskey_test(PADDIV);
  8124. return false;
  8125. }
  8126. //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise.
  8127. /**
  8128. \param keycode C-string containing the keycode label of the key to test.
  8129. \note Use it when the key you want to test can be dynamically set by the user.
  8130. \par Example
  8131. \code
  8132. CImgDisplay disp(400,400);
  8133. const char *const keycode = "TAB";
  8134. while (!disp.is_closed()) {
  8135. if (disp.is_key(keycode)) { ... } // Equivalent to 'if (disp.is_keyTAB())'
  8136. disp.wait();
  8137. }
  8138. \endcode
  8139. **/
  8140. bool& is_key(const char *const keycode) {
  8141. static bool f = false;
  8142. f = false;
  8143. #define _cimg_iskey_test2(k) if (!cimg::strcasecmp(keycode,#k)) return _is_key##k;
  8144. _cimg_iskey_test2(ESC); _cimg_iskey_test2(F1); _cimg_iskey_test2(F2); _cimg_iskey_test2(F3);
  8145. _cimg_iskey_test2(F4); _cimg_iskey_test2(F5); _cimg_iskey_test2(F6); _cimg_iskey_test2(F7);
  8146. _cimg_iskey_test2(F8); _cimg_iskey_test2(F9); _cimg_iskey_test2(F10); _cimg_iskey_test2(F11);
  8147. _cimg_iskey_test2(F12); _cimg_iskey_test2(PAUSE); _cimg_iskey_test2(1); _cimg_iskey_test2(2);
  8148. _cimg_iskey_test2(3); _cimg_iskey_test2(4); _cimg_iskey_test2(5); _cimg_iskey_test2(6);
  8149. _cimg_iskey_test2(7); _cimg_iskey_test2(8); _cimg_iskey_test2(9); _cimg_iskey_test2(0);
  8150. _cimg_iskey_test2(BACKSPACE); _cimg_iskey_test2(INSERT); _cimg_iskey_test2(HOME);
  8151. _cimg_iskey_test2(PAGEUP); _cimg_iskey_test2(TAB); _cimg_iskey_test2(Q); _cimg_iskey_test2(W);
  8152. _cimg_iskey_test2(E); _cimg_iskey_test2(R); _cimg_iskey_test2(T); _cimg_iskey_test2(Y);
  8153. _cimg_iskey_test2(U); _cimg_iskey_test2(I); _cimg_iskey_test2(O); _cimg_iskey_test2(P);
  8154. _cimg_iskey_test2(DELETE); _cimg_iskey_test2(END); _cimg_iskey_test2(PAGEDOWN);
  8155. _cimg_iskey_test2(CAPSLOCK); _cimg_iskey_test2(A); _cimg_iskey_test2(S); _cimg_iskey_test2(D);
  8156. _cimg_iskey_test2(F); _cimg_iskey_test2(G); _cimg_iskey_test2(H); _cimg_iskey_test2(J);
  8157. _cimg_iskey_test2(K); _cimg_iskey_test2(L); _cimg_iskey_test2(ENTER);
  8158. _cimg_iskey_test2(SHIFTLEFT); _cimg_iskey_test2(Z); _cimg_iskey_test2(X); _cimg_iskey_test2(C);
  8159. _cimg_iskey_test2(V); _cimg_iskey_test2(B); _cimg_iskey_test2(N); _cimg_iskey_test2(M);
  8160. _cimg_iskey_test2(SHIFTRIGHT); _cimg_iskey_test2(ARROWUP); _cimg_iskey_test2(CTRLLEFT);
  8161. _cimg_iskey_test2(APPLEFT); _cimg_iskey_test2(ALT); _cimg_iskey_test2(SPACE); _cimg_iskey_test2(ALTGR);
  8162. _cimg_iskey_test2(APPRIGHT); _cimg_iskey_test2(MENU); _cimg_iskey_test2(CTRLRIGHT);
  8163. _cimg_iskey_test2(ARROWLEFT); _cimg_iskey_test2(ARROWDOWN); _cimg_iskey_test2(ARROWRIGHT);
  8164. _cimg_iskey_test2(PAD0); _cimg_iskey_test2(PAD1); _cimg_iskey_test2(PAD2);
  8165. _cimg_iskey_test2(PAD3); _cimg_iskey_test2(PAD4); _cimg_iskey_test2(PAD5);
  8166. _cimg_iskey_test2(PAD6); _cimg_iskey_test2(PAD7); _cimg_iskey_test2(PAD8);
  8167. _cimg_iskey_test2(PAD9); _cimg_iskey_test2(PADADD); _cimg_iskey_test2(PADSUB);
  8168. _cimg_iskey_test2(PADMUL); _cimg_iskey_test2(PADDIV);
  8169. return f;
  8170. }
  8171. //! Return \c true if specified key sequence has been typed on the associated window, \c false otherwise.
  8172. /**
  8173. \param keycodes_sequence Buffer of keycodes to test.
  8174. \param length Number of keys in the \c keycodes_sequence buffer.
  8175. \param remove_sequence Tells if the key sequence must be removed from the key history, if found.
  8176. \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8177. your code stay portable (see cimg::keyESC).
  8178. \par Example
  8179. \code
  8180. CImgDisplay disp(400,400);
  8181. const unsigned int key_seq[] = { cimg::keyCTRLLEFT, cimg::keyD };
  8182. while (!disp.is_closed()) {
  8183. if (disp.is_key_sequence(key_seq,2)) { ... } // Test for the 'CTRL+D' keyboard event
  8184. disp.wait();
  8185. }
  8186. \endcode
  8187. **/
  8188. bool is_key_sequence(const unsigned int *const keycodes_sequence, const unsigned int length,
  8189. const bool remove_sequence=false) {
  8190. if (keycodes_sequence && length) {
  8191. const unsigned int
  8192. *const ps_end = keycodes_sequence + length - 1,
  8193. *const pk_end = (unsigned int*)_keys + 1 + 128 - length,
  8194. k = *ps_end;
  8195. for (unsigned int *pk = (unsigned int*)_keys; pk<pk_end; ) {
  8196. if (*(pk++)==k) {
  8197. bool res = true;
  8198. const unsigned int *ps = ps_end, *pk2 = pk;
  8199. for (unsigned int i = 1; i<length; ++i) res = (*(--ps)==*(pk2++));
  8200. if (res) {
  8201. if (remove_sequence) std::memset((void*)(pk - 1),0,sizeof(unsigned int)*length);
  8202. return true;
  8203. }
  8204. }
  8205. }
  8206. }
  8207. return false;
  8208. }
  8209. #define _cimg_iskey_def(k) \
  8210. bool is_key##k() const { \
  8211. return _is_key##k; \
  8212. }
  8213. //! Return \c true if the \c ESC key is being pressed on the associated window, \c false otherwise.
  8214. /**
  8215. \note Similar methods exist for all keys managed by \CImg (see cimg::keyESC).
  8216. **/
  8217. _cimg_iskey_def(ESC); _cimg_iskey_def(F1); _cimg_iskey_def(F2); _cimg_iskey_def(F3);
  8218. _cimg_iskey_def(F4); _cimg_iskey_def(F5); _cimg_iskey_def(F6); _cimg_iskey_def(F7);
  8219. _cimg_iskey_def(F8); _cimg_iskey_def(F9); _cimg_iskey_def(F10); _cimg_iskey_def(F11);
  8220. _cimg_iskey_def(F12); _cimg_iskey_def(PAUSE); _cimg_iskey_def(1); _cimg_iskey_def(2);
  8221. _cimg_iskey_def(3); _cimg_iskey_def(4); _cimg_iskey_def(5); _cimg_iskey_def(6);
  8222. _cimg_iskey_def(7); _cimg_iskey_def(8); _cimg_iskey_def(9); _cimg_iskey_def(0);
  8223. _cimg_iskey_def(BACKSPACE); _cimg_iskey_def(INSERT); _cimg_iskey_def(HOME);
  8224. _cimg_iskey_def(PAGEUP); _cimg_iskey_def(TAB); _cimg_iskey_def(Q); _cimg_iskey_def(W);
  8225. _cimg_iskey_def(E); _cimg_iskey_def(R); _cimg_iskey_def(T); _cimg_iskey_def(Y);
  8226. _cimg_iskey_def(U); _cimg_iskey_def(I); _cimg_iskey_def(O); _cimg_iskey_def(P);
  8227. _cimg_iskey_def(DELETE); _cimg_iskey_def(END); _cimg_iskey_def(PAGEDOWN);
  8228. _cimg_iskey_def(CAPSLOCK); _cimg_iskey_def(A); _cimg_iskey_def(S); _cimg_iskey_def(D);
  8229. _cimg_iskey_def(F); _cimg_iskey_def(G); _cimg_iskey_def(H); _cimg_iskey_def(J);
  8230. _cimg_iskey_def(K); _cimg_iskey_def(L); _cimg_iskey_def(ENTER);
  8231. _cimg_iskey_def(SHIFTLEFT); _cimg_iskey_def(Z); _cimg_iskey_def(X); _cimg_iskey_def(C);
  8232. _cimg_iskey_def(V); _cimg_iskey_def(B); _cimg_iskey_def(N); _cimg_iskey_def(M);
  8233. _cimg_iskey_def(SHIFTRIGHT); _cimg_iskey_def(ARROWUP); _cimg_iskey_def(CTRLLEFT);
  8234. _cimg_iskey_def(APPLEFT); _cimg_iskey_def(ALT); _cimg_iskey_def(SPACE); _cimg_iskey_def(ALTGR);
  8235. _cimg_iskey_def(APPRIGHT); _cimg_iskey_def(MENU); _cimg_iskey_def(CTRLRIGHT);
  8236. _cimg_iskey_def(ARROWLEFT); _cimg_iskey_def(ARROWDOWN); _cimg_iskey_def(ARROWRIGHT);
  8237. _cimg_iskey_def(PAD0); _cimg_iskey_def(PAD1); _cimg_iskey_def(PAD2);
  8238. _cimg_iskey_def(PAD3); _cimg_iskey_def(PAD4); _cimg_iskey_def(PAD5);
  8239. _cimg_iskey_def(PAD6); _cimg_iskey_def(PAD7); _cimg_iskey_def(PAD8);
  8240. _cimg_iskey_def(PAD9); _cimg_iskey_def(PADADD); _cimg_iskey_def(PADSUB);
  8241. _cimg_iskey_def(PADMUL); _cimg_iskey_def(PADDIV);
  8242. //@}
  8243. //------------------------------------------
  8244. //
  8245. //! \name Instance Characteristics
  8246. //@{
  8247. //------------------------------------------
  8248. #if cimg_display==0
  8249. //! Return width of the screen (current resolution along the X-axis).
  8250. /**
  8251. **/
  8252. static int screen_width() {
  8253. _no_display_exception();
  8254. return 0;
  8255. }
  8256. //! Return height of the screen (current resolution along the Y-axis).
  8257. /**
  8258. **/
  8259. static int screen_height() {
  8260. _no_display_exception();
  8261. return 0;
  8262. }
  8263. #endif
  8264. //! Return display width.
  8265. /**
  8266. \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance)
  8267. may be different from the actual width of the associated window.
  8268. **/
  8269. int width() const {
  8270. return (int)_width;
  8271. }
  8272. //! Return display height.
  8273. /**
  8274. \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance)
  8275. may be different from the actual height of the associated window.
  8276. **/
  8277. int height() const {
  8278. return (int)_height;
  8279. }
  8280. //! Return normalization type of the display.
  8281. /**
  8282. The normalization type tells about how the values of an input image are normalized by the CImgDisplay to be
  8283. correctly displayed. The range of values for pixels displayed on screen is <tt>[0,255]</tt>.
  8284. If the range of values of the data to display is different, a normalization may be required for displaying
  8285. the data in a correct way. The normalization type can be one of:
  8286. - \c 0: Value normalization is disabled. It is then assumed that all input data to be displayed by the
  8287. CImgDisplay instance have values in range <tt>[0,255]</tt>.
  8288. - \c 1: Value normalization is always performed (this is the default behavior).
  8289. Before displaying an input image, its values will be (virtually) stretched
  8290. in range <tt>[0,255]</tt>, so that the contrast of the displayed pixels will be maximum.
  8291. Use this mode for images whose minimum and maximum values are not prescribed to known values
  8292. (e.g. float-valued images).
  8293. Note that when normalized versions of images are computed for display purposes, the actual values of these
  8294. images are not modified.
  8295. - \c 2: Value normalization is performed once (on the first image display), then the same normalization
  8296. coefficients are kept for next displayed frames.
  8297. - \c 3: Value normalization depends on the pixel type of the data to display. For integer pixel types,
  8298. the normalization is done regarding the minimum/maximum values of the type (no normalization occurs then
  8299. for <tt>unsigned char</tt>).
  8300. For float-valued pixel types, the normalization is done regarding the minimum/maximum value of the image
  8301. data instead.
  8302. **/
  8303. unsigned int normalization() const {
  8304. return _normalization;
  8305. }
  8306. //! Return title of the associated window as a C-string.
  8307. /**
  8308. \note Window title may be not visible, depending on the used window manager or if the current display is
  8309. in fullscreen mode.
  8310. **/
  8311. const char *title() const {
  8312. return _title?_title:"";
  8313. }
  8314. //! Return width of the associated window.
  8315. /**
  8316. \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance)
  8317. may be different from the actual width of the associated window.
  8318. **/
  8319. int window_width() const {
  8320. return (int)_window_width;
  8321. }
  8322. //! Return height of the associated window.
  8323. /**
  8324. \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance)
  8325. may be different from the actual height of the associated window.
  8326. **/
  8327. int window_height() const {
  8328. return (int)_window_height;
  8329. }
  8330. //! Return X-coordinate of the associated window.
  8331. /**
  8332. \note The returned coordinate corresponds to the location of the upper-left corner of the associated window.
  8333. **/
  8334. int window_x() const {
  8335. return _window_x;
  8336. }
  8337. //! Return Y-coordinate of the associated window.
  8338. /**
  8339. \note The returned coordinate corresponds to the location of the upper-left corner of the associated window.
  8340. **/
  8341. int window_y() const {
  8342. return _window_y;
  8343. }
  8344. //! Return X-coordinate of the mouse pointer.
  8345. /**
  8346. \note
  8347. - If the mouse pointer is outside window area, \c -1 is returned.
  8348. - Otherwise, the returned value is in the range [0,width()-1].
  8349. **/
  8350. int mouse_x() const {
  8351. return _mouse_x;
  8352. }
  8353. //! Return Y-coordinate of the mouse pointer.
  8354. /**
  8355. \note
  8356. - If the mouse pointer is outside window area, \c -1 is returned.
  8357. - Otherwise, the returned value is in the range [0,height()-1].
  8358. **/
  8359. int mouse_y() const {
  8360. return _mouse_y;
  8361. }
  8362. //! Return current state of the mouse buttons.
  8363. /**
  8364. \note Three mouse buttons can be managed. If one button is pressed, its corresponding bit in the returned
  8365. value is set:
  8366. - bit \c 0 (value \c 0x1): State of the left mouse button.
  8367. - bit \c 1 (value \c 0x2): State of the right mouse button.
  8368. - bit \c 2 (value \c 0x4): State of the middle mouse button.
  8369. Several bits can be activated if more than one button are pressed at the same time.
  8370. \par Example
  8371. \code
  8372. CImgDisplay disp(400,400);
  8373. while (!disp.is_closed()) {
  8374. if (disp.button()&1) { // Left button clicked
  8375. ...
  8376. }
  8377. if (disp.button()&2) { // Right button clicked
  8378. ...
  8379. }
  8380. if (disp.button()&4) { // Middle button clicked
  8381. ...
  8382. }
  8383. disp.wait();
  8384. }
  8385. \endcode
  8386. **/
  8387. unsigned int button() const {
  8388. return _button;
  8389. }
  8390. //! Return current state of the mouse wheel.
  8391. /**
  8392. \note
  8393. - The returned value can be positive or negative depending on whether the mouse wheel has been scrolled
  8394. forward or backward.
  8395. - Scrolling the wheel forward add \c 1 to the wheel value.
  8396. - Scrolling the wheel backward subtract \c 1 to the wheel value.
  8397. - The returned value cumulates the number of forward of backward scrolls since the creation of the display,
  8398. or since the last reset of the wheel value (using set_wheel()). It is strongly recommended to quickly reset
  8399. the wheel counter when an action has been performed regarding the current wheel value.
  8400. Otherwise, the returned wheel value may be for instance \c 0 despite the fact that many scrolls have been done
  8401. (as many in forward as in backward directions).
  8402. \par Example
  8403. \code
  8404. CImgDisplay disp(400,400);
  8405. while (!disp.is_closed()) {
  8406. if (disp.wheel()) {
  8407. int counter = disp.wheel(); // Read the state of the mouse wheel
  8408. ... // Do what you want with 'counter'
  8409. disp.set_wheel(); // Reset the wheel value to 0
  8410. }
  8411. disp.wait();
  8412. }
  8413. \endcode
  8414. **/
  8415. int wheel() const {
  8416. return _wheel;
  8417. }
  8418. //! Return one entry from the pressed keys history.
  8419. /**
  8420. \param pos Index to read from the pressed keys history (index \c 0 corresponds to latest entry).
  8421. \return Keycode of a pressed key or \c 0 for a released key.
  8422. \note
  8423. - Each CImgDisplay stores a history of the pressed keys in a buffer of size \c 128. When a new key is pressed,
  8424. its keycode is stored in the pressed keys history. When a key is released, \c 0 is put instead.
  8425. This means that up to the 64 last pressed keys may be read from the pressed keys history.
  8426. When a new value is stored, the pressed keys history is shifted so that the latest entry is always
  8427. stored at position \c 0.
  8428. - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8429. your code stay portable (see cimg::keyESC).
  8430. **/
  8431. unsigned int& key(const unsigned int pos=0) const {
  8432. static unsigned int key0;
  8433. return pos<128?_keys[pos]:(key0 = 0);
  8434. }
  8435. //! Return one entry from the released keys history.
  8436. /**
  8437. \param pos Index to read from the released keys history (index \c 0 corresponds to latest entry).
  8438. \return Keycode of a released key or \c 0 for a pressed key.
  8439. \note
  8440. - Each CImgDisplay stores a history of the released keys in a buffer of size \c 128. When a new key is released,
  8441. its keycode is stored in the pressed keys history. When a key is pressed, \c 0 is put instead.
  8442. This means that up to the 64 last released keys may be read from the released keys history.
  8443. When a new value is stored, the released keys history is shifted so that the latest entry is always
  8444. stored at position \c 0.
  8445. - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8446. your code stay portable (see cimg::keyESC).
  8447. **/
  8448. unsigned int& released_key(const unsigned int pos=0) const {
  8449. static unsigned int key0;
  8450. return pos<128?_released_keys[pos]:(key0 = 0);
  8451. }
  8452. //! Return keycode corresponding to the specified string.
  8453. /**
  8454. \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8455. your code stay portable (see cimg::keyESC).
  8456. \par Example
  8457. \code
  8458. const unsigned int keyTAB = CImgDisplay::keycode("TAB"); // Return cimg::keyTAB
  8459. \endcode
  8460. **/
  8461. static unsigned int keycode(const char *const keycode) {
  8462. #define _cimg_keycode(k) if (!cimg::strcasecmp(keycode,#k)) return cimg::key##k;
  8463. _cimg_keycode(ESC); _cimg_keycode(F1); _cimg_keycode(F2); _cimg_keycode(F3);
  8464. _cimg_keycode(F4); _cimg_keycode(F5); _cimg_keycode(F6); _cimg_keycode(F7);
  8465. _cimg_keycode(F8); _cimg_keycode(F9); _cimg_keycode(F10); _cimg_keycode(F11);
  8466. _cimg_keycode(F12); _cimg_keycode(PAUSE); _cimg_keycode(1); _cimg_keycode(2);
  8467. _cimg_keycode(3); _cimg_keycode(4); _cimg_keycode(5); _cimg_keycode(6);
  8468. _cimg_keycode(7); _cimg_keycode(8); _cimg_keycode(9); _cimg_keycode(0);
  8469. _cimg_keycode(BACKSPACE); _cimg_keycode(INSERT); _cimg_keycode(HOME);
  8470. _cimg_keycode(PAGEUP); _cimg_keycode(TAB); _cimg_keycode(Q); _cimg_keycode(W);
  8471. _cimg_keycode(E); _cimg_keycode(R); _cimg_keycode(T); _cimg_keycode(Y);
  8472. _cimg_keycode(U); _cimg_keycode(I); _cimg_keycode(O); _cimg_keycode(P);
  8473. _cimg_keycode(DELETE); _cimg_keycode(END); _cimg_keycode(PAGEDOWN);
  8474. _cimg_keycode(CAPSLOCK); _cimg_keycode(A); _cimg_keycode(S); _cimg_keycode(D);
  8475. _cimg_keycode(F); _cimg_keycode(G); _cimg_keycode(H); _cimg_keycode(J);
  8476. _cimg_keycode(K); _cimg_keycode(L); _cimg_keycode(ENTER);
  8477. _cimg_keycode(SHIFTLEFT); _cimg_keycode(Z); _cimg_keycode(X); _cimg_keycode(C);
  8478. _cimg_keycode(V); _cimg_keycode(B); _cimg_keycode(N); _cimg_keycode(M);
  8479. _cimg_keycode(SHIFTRIGHT); _cimg_keycode(ARROWUP); _cimg_keycode(CTRLLEFT);
  8480. _cimg_keycode(APPLEFT); _cimg_keycode(ALT); _cimg_keycode(SPACE); _cimg_keycode(ALTGR);
  8481. _cimg_keycode(APPRIGHT); _cimg_keycode(MENU); _cimg_keycode(CTRLRIGHT);
  8482. _cimg_keycode(ARROWLEFT); _cimg_keycode(ARROWDOWN); _cimg_keycode(ARROWRIGHT);
  8483. _cimg_keycode(PAD0); _cimg_keycode(PAD1); _cimg_keycode(PAD2);
  8484. _cimg_keycode(PAD3); _cimg_keycode(PAD4); _cimg_keycode(PAD5);
  8485. _cimg_keycode(PAD6); _cimg_keycode(PAD7); _cimg_keycode(PAD8);
  8486. _cimg_keycode(PAD9); _cimg_keycode(PADADD); _cimg_keycode(PADSUB);
  8487. _cimg_keycode(PADMUL); _cimg_keycode(PADDIV);
  8488. return 0;
  8489. }
  8490. //! Return the current refresh rate, in frames per second.
  8491. /**
  8492. \note Returns a significant value when the current instance is used to display successive frames.
  8493. It measures the delay between successive calls to frames_per_second().
  8494. **/
  8495. float frames_per_second() {
  8496. if (!_fps_timer) _fps_timer = cimg::time();
  8497. const float delta = (float)((cimg::time() - _fps_timer)/1000.f);
  8498. ++_fps_frames;
  8499. if (delta>=1) {
  8500. _fps_fps = _fps_frames/delta;
  8501. _fps_frames = 0;
  8502. _fps_timer = cimg::time();
  8503. }
  8504. return _fps_fps;
  8505. }
  8506. // Move current display window so that its content stays inside the current screen.
  8507. CImgDisplay& move_inside_screen() {
  8508. if (is_empty()) return *this;
  8509. const int
  8510. x0 = window_x(),
  8511. y0 = window_y(),
  8512. x1 = x0 + window_width() - 1,
  8513. y1 = y0 + window_height() - 1,
  8514. sw = CImgDisplay::screen_width(),
  8515. sh = CImgDisplay::screen_height();
  8516. if (x0<0 || y0<0 || x1>=sw || y1>=sh)
  8517. move(std::max(0,std::min(x0,sw - x1 + x0)),
  8518. std::max(0,std::min(y0,sh - y1 + y0)));
  8519. return *this;
  8520. }
  8521. //@}
  8522. //---------------------------------------
  8523. //
  8524. //! \name Window Manipulation
  8525. //@{
  8526. //---------------------------------------
  8527. #if cimg_display==0
  8528. //! Display image on associated window.
  8529. /**
  8530. \param img Input image to display.
  8531. \note This method returns immediately.
  8532. **/
  8533. template<typename T>
  8534. CImgDisplay& display(const CImg<T>& img) {
  8535. return assign(img);
  8536. }
  8537. #endif
  8538. //! Display list of images on associated window.
  8539. /**
  8540. \param list List of images to display.
  8541. \param axis Axis used to append the images along, for the visualization (can be \c x, \c y, \c z or \c c).
  8542. \param align Relative position of aligned images when displaying lists with images of different sizes
  8543. (\c 0 for upper-left, \c 0.5 for centering and \c 1 for lower-right).
  8544. \note This method returns immediately.
  8545. **/
  8546. template<typename T>
  8547. CImgDisplay& display(const CImgList<T>& list, const char axis='x', const float align=0) {
  8548. if (list._width==1) {
  8549. const CImg<T>& img = list[0];
  8550. if (img._depth==1 && (img._spectrum==1 || img._spectrum>=3) && _normalization!=1) return display(img);
  8551. }
  8552. CImgList<typename CImg<T>::ucharT> visu(list._width);
  8553. unsigned int dims = 0;
  8554. cimglist_for(list,l) {
  8555. const CImg<T>& img = list._data[l];
  8556. img._get_select(*this,_normalization,(img._width - 1)/2,(img._height - 1)/2,
  8557. (img._depth - 1)/2).move_to(visu[l]);
  8558. dims = std::max(dims,visu[l]._spectrum);
  8559. }
  8560. cimglist_for(list,l) if (visu[l]._spectrum<dims) visu[l].resize(-100,-100,-100,dims,1);
  8561. visu.get_append(axis,align).display(*this);
  8562. return *this;
  8563. }
  8564. #if cimg_display==0
  8565. //! Show (closed) associated window on the screen.
  8566. /**
  8567. \note
  8568. - Force the associated window of a display to be visible on the screen, even if it has been closed before.
  8569. - Using show() on a visible display does nothing.
  8570. **/
  8571. CImgDisplay& show() {
  8572. return assign();
  8573. }
  8574. //! Close (visible) associated window and make it disappear from the screen.
  8575. /**
  8576. \note
  8577. - A closed display only means the associated window is not visible anymore. This does not mean the display has
  8578. been destroyed.
  8579. Use show() to make the associated window reappear.
  8580. - Using close() on a closed display does nothing.
  8581. **/
  8582. CImgDisplay& close() {
  8583. return assign();
  8584. }
  8585. //! Move associated window to a new location.
  8586. /**
  8587. \param pos_x X-coordinate of the new window location.
  8588. \param pos_y Y-coordinate of the new window location.
  8589. \note Depending on the window manager behavior, this method may not succeed (no exceptions are thrown
  8590. nevertheless).
  8591. **/
  8592. CImgDisplay& move(const int pos_x, const int pos_y) {
  8593. return assign(pos_x,pos_y);
  8594. }
  8595. #endif
  8596. //! Resize display to the size of the associated window.
  8597. /**
  8598. \param force_redraw Tells if the previous window content must be updated and refreshed as well.
  8599. \note
  8600. - Calling this method ensures that width() and window_width() become equal, as well as height() and
  8601. window_height().
  8602. - The associated window is also resized to specified dimensions.
  8603. **/
  8604. CImgDisplay& resize(const bool force_redraw=true) {
  8605. resize(window_width(),window_height(),force_redraw);
  8606. return *this;
  8607. }
  8608. #if cimg_display==0
  8609. //! Resize display to the specified size.
  8610. /**
  8611. \param width Requested display width.
  8612. \param height Requested display height.
  8613. \param force_redraw Tells if the previous window content must be updated and refreshed as well.
  8614. \note The associated window is also resized to specified dimensions.
  8615. **/
  8616. CImgDisplay& resize(const int width, const int height, const bool force_redraw=true) {
  8617. return assign(width,height,0,3,force_redraw);
  8618. }
  8619. #endif
  8620. //! Resize display to the size of an input image.
  8621. /**
  8622. \param img Input image to take size from.
  8623. \param force_redraw Tells if the previous window content must be resized and updated as well.
  8624. \note
  8625. - Calling this method ensures that width() and <tt>img.width()</tt> become equal, as well as height() and
  8626. <tt>img.height()</tt>.
  8627. - The associated window is also resized to specified dimensions.
  8628. **/
  8629. template<typename T>
  8630. CImgDisplay& resize(const CImg<T>& img, const bool force_redraw=true) {
  8631. return resize(img._width,img._height,force_redraw);
  8632. }
  8633. //! Resize display to the size of another CImgDisplay instance.
  8634. /**
  8635. \param disp Input display to take size from.
  8636. \param force_redraw Tells if the previous window content must be resized and updated as well.
  8637. \note
  8638. - Calling this method ensures that width() and <tt>disp.width()</tt> become equal, as well as height() and
  8639. <tt>disp.height()</tt>.
  8640. - The associated window is also resized to specified dimensions.
  8641. **/
  8642. CImgDisplay& resize(const CImgDisplay& disp, const bool force_redraw=true) {
  8643. return resize(disp.width(),disp.height(),force_redraw);
  8644. }
  8645. // [internal] Render pixel buffer with size (wd,hd) from source buffer of size (ws,hs).
  8646. template<typename t, typename T>
  8647. static void _render_resize(const T *ptrs, const unsigned int ws, const unsigned int hs,
  8648. t *ptrd, const unsigned int wd, const unsigned int hd) {
  8649. typedef typename cimg::last<T,cimg_ulong>::type ulongT;
  8650. const ulongT one = (ulongT)1;
  8651. CImg<ulongT> off_x(wd), off_y(hd + 1);
  8652. if (wd==ws) off_x.fill(1);
  8653. else {
  8654. ulongT *poff_x = off_x._data, curr = 0;
  8655. for (unsigned int x = 0; x<wd; ++x) {
  8656. const ulongT old = curr;
  8657. curr = (x + one)*ws/wd;
  8658. *(poff_x++) = curr - old;
  8659. }
  8660. }
  8661. if (hd==hs) off_y.fill(ws);
  8662. else {
  8663. ulongT *poff_y = off_y._data, curr = 0;
  8664. for (unsigned int y = 0; y<hd; ++y) {
  8665. const ulongT old = curr;
  8666. curr = (y + one)*hs/hd;
  8667. *(poff_y++) = ws*(curr - old);
  8668. }
  8669. *poff_y = 0;
  8670. }
  8671. ulongT *poff_y = off_y._data;
  8672. for (unsigned int y = 0; y<hd; ) {
  8673. const T *ptr = ptrs;
  8674. ulongT *poff_x = off_x._data;
  8675. for (unsigned int x = 0; x<wd; ++x) { *(ptrd++) = *ptr; ptr+=*(poff_x++); }
  8676. ++y;
  8677. ulongT dy = *(poff_y++);
  8678. for ( ; !dy && y<hd; std::memcpy(ptrd,ptrd - wd,sizeof(t)*wd), ++y, ptrd+=wd, dy = *(poff_y++)) {}
  8679. ptrs+=dy;
  8680. }
  8681. }
  8682. //! Set normalization type.
  8683. /**
  8684. \param normalization New normalization mode.
  8685. **/
  8686. CImgDisplay& set_normalization(const unsigned int normalization) {
  8687. _normalization = normalization;
  8688. _min = _max = 0;
  8689. return *this;
  8690. }
  8691. #if cimg_display==0
  8692. //! Set title of the associated window.
  8693. /**
  8694. \param format C-string containing the format of the title, as with <tt>std::printf()</tt>.
  8695. \warning As the first argument is a format string, it is highly recommended to write
  8696. \code
  8697. disp.set_title("%s",window_title);
  8698. \endcode
  8699. instead of
  8700. \code
  8701. disp.set_title(window_title);
  8702. \endcode
  8703. if \c window_title can be arbitrary, to prevent nasty memory access.
  8704. **/
  8705. CImgDisplay& set_title(const char *const format, ...) {
  8706. return assign(0,0,format);
  8707. }
  8708. #endif
  8709. //! Enable or disable fullscreen mode.
  8710. /**
  8711. \param is_fullscreen Tells is the fullscreen mode must be activated or not.
  8712. \param force_redraw Tells if the previous window content must be displayed as well.
  8713. \note
  8714. - When the fullscreen mode is enabled, the associated window fills the entire screen but the size of the
  8715. current display is not modified.
  8716. - The screen resolution may be switched to fit the associated window size and ensure it appears the largest
  8717. as possible.
  8718. For X-Window (X11) users, the configuration flag \c cimg_use_xrandr has to be set to allow the screen
  8719. resolution change (requires the X11 extensions to be enabled).
  8720. **/
  8721. CImgDisplay& set_fullscreen(const bool is_fullscreen, const bool force_redraw=true) {
  8722. if (is_empty() || _is_fullscreen==is_fullscreen) return *this;
  8723. return toggle_fullscreen(force_redraw);
  8724. }
  8725. #if cimg_display==0
  8726. //! Toggle fullscreen mode.
  8727. /**
  8728. \param force_redraw Tells if the previous window content must be displayed as well.
  8729. \note Enable fullscreen mode if it was not enabled, and disable it otherwise.
  8730. **/
  8731. CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
  8732. return assign(_width,_height,0,3,force_redraw);
  8733. }
  8734. //! Show mouse pointer.
  8735. /**
  8736. \note Depending on the window manager behavior, this method may not succeed
  8737. (no exceptions are thrown nevertheless).
  8738. **/
  8739. CImgDisplay& show_mouse() {
  8740. return assign();
  8741. }
  8742. //! Hide mouse pointer.
  8743. /**
  8744. \note Depending on the window manager behavior, this method may not succeed
  8745. (no exceptions are thrown nevertheless).
  8746. **/
  8747. CImgDisplay& hide_mouse() {
  8748. return assign();
  8749. }
  8750. //! Move mouse pointer to a specified location.
  8751. /**
  8752. \note Depending on the window manager behavior, this method may not succeed
  8753. (no exceptions are thrown nevertheless).
  8754. **/
  8755. CImgDisplay& set_mouse(const int pos_x, const int pos_y) {
  8756. return assign(pos_x,pos_y);
  8757. }
  8758. #endif
  8759. //! Simulate a mouse button release event.
  8760. /**
  8761. \note All mouse buttons are considered released at the same time.
  8762. **/
  8763. CImgDisplay& set_button() {
  8764. _button = 0;
  8765. _is_event = true;
  8766. #if cimg_display==1
  8767. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8768. #elif cimg_display==2
  8769. SetEvent(cimg::Win32_attr().wait_event);
  8770. #endif
  8771. return *this;
  8772. }
  8773. //! Simulate a mouse button press or release event.
  8774. /**
  8775. \param button Buttons event code, where each button is associated to a single bit.
  8776. \param is_pressed Tells if the mouse button is considered as pressed or released.
  8777. **/
  8778. CImgDisplay& set_button(const unsigned int button, const bool is_pressed=true) {
  8779. const unsigned int buttoncode = button==1U?1U:button==2U?2U:button==3U?4U:0U;
  8780. if (is_pressed) _button |= buttoncode; else _button &= ~buttoncode;
  8781. _is_event = buttoncode?true:false;
  8782. if (buttoncode) {
  8783. #if cimg_display==1
  8784. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8785. #elif cimg_display==2
  8786. SetEvent(cimg::Win32_attr().wait_event);
  8787. #endif
  8788. }
  8789. return *this;
  8790. }
  8791. //! Flush all mouse wheel events.
  8792. /**
  8793. \note Make wheel() to return \c 0, if called afterwards.
  8794. **/
  8795. CImgDisplay& set_wheel() {
  8796. _wheel = 0;
  8797. _is_event = true;
  8798. #if cimg_display==1
  8799. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8800. #elif cimg_display==2
  8801. SetEvent(cimg::Win32_attr().wait_event);
  8802. #endif
  8803. return *this;
  8804. }
  8805. //! Simulate a wheel event.
  8806. /**
  8807. \param amplitude Amplitude of the wheel scrolling to simulate.
  8808. \note Make wheel() to return \c amplitude, if called afterwards.
  8809. **/
  8810. CImgDisplay& set_wheel(const int amplitude) {
  8811. _wheel+=amplitude;
  8812. _is_event = amplitude?true:false;
  8813. if (amplitude) {
  8814. #if cimg_display==1
  8815. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8816. #elif cimg_display==2
  8817. SetEvent(cimg::Win32_attr().wait_event);
  8818. #endif
  8819. }
  8820. return *this;
  8821. }
  8822. //! Flush all key events.
  8823. /**
  8824. \note Make key() to return \c 0, if called afterwards.
  8825. **/
  8826. CImgDisplay& set_key() {
  8827. std::memset((void*)_keys,0,128*sizeof(unsigned int));
  8828. std::memset((void*)_released_keys,0,128*sizeof(unsigned int));
  8829. _is_keyESC = _is_keyF1 = _is_keyF2 = _is_keyF3 = _is_keyF4 = _is_keyF5 = _is_keyF6 = _is_keyF7 = _is_keyF8 =
  8830. _is_keyF9 = _is_keyF10 = _is_keyF11 = _is_keyF12 = _is_keyPAUSE = _is_key1 = _is_key2 = _is_key3 = _is_key4 =
  8831. _is_key5 = _is_key6 = _is_key7 = _is_key8 = _is_key9 = _is_key0 = _is_keyBACKSPACE = _is_keyINSERT =
  8832. _is_keyHOME = _is_keyPAGEUP = _is_keyTAB = _is_keyQ = _is_keyW = _is_keyE = _is_keyR = _is_keyT = _is_keyY =
  8833. _is_keyU = _is_keyI = _is_keyO = _is_keyP = _is_keyDELETE = _is_keyEND = _is_keyPAGEDOWN = _is_keyCAPSLOCK =
  8834. _is_keyA = _is_keyS = _is_keyD = _is_keyF = _is_keyG = _is_keyH = _is_keyJ = _is_keyK = _is_keyL =
  8835. _is_keyENTER = _is_keySHIFTLEFT = _is_keyZ = _is_keyX = _is_keyC = _is_keyV = _is_keyB = _is_keyN =
  8836. _is_keyM = _is_keySHIFTRIGHT = _is_keyARROWUP = _is_keyCTRLLEFT = _is_keyAPPLEFT = _is_keyALT = _is_keySPACE =
  8837. _is_keyALTGR = _is_keyAPPRIGHT = _is_keyMENU = _is_keyCTRLRIGHT = _is_keyARROWLEFT = _is_keyARROWDOWN =
  8838. _is_keyARROWRIGHT = _is_keyPAD0 = _is_keyPAD1 = _is_keyPAD2 = _is_keyPAD3 = _is_keyPAD4 = _is_keyPAD5 =
  8839. _is_keyPAD6 = _is_keyPAD7 = _is_keyPAD8 = _is_keyPAD9 = _is_keyPADADD = _is_keyPADSUB = _is_keyPADMUL =
  8840. _is_keyPADDIV = false;
  8841. _is_event = true;
  8842. #if cimg_display==1
  8843. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8844. #elif cimg_display==2
  8845. SetEvent(cimg::Win32_attr().wait_event);
  8846. #endif
  8847. return *this;
  8848. }
  8849. //! Simulate a keyboard press/release event.
  8850. /**
  8851. \param keycode Keycode of the associated key.
  8852. \param is_pressed Tells if the key is considered as pressed or released.
  8853. \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
  8854. your code stay portable (see cimg::keyESC).
  8855. **/
  8856. CImgDisplay& set_key(const unsigned int keycode, const bool is_pressed=true) {
  8857. #define _cimg_set_key(k) if (keycode==cimg::key##k) _is_key##k = is_pressed;
  8858. _cimg_set_key(ESC); _cimg_set_key(F1); _cimg_set_key(F2); _cimg_set_key(F3);
  8859. _cimg_set_key(F4); _cimg_set_key(F5); _cimg_set_key(F6); _cimg_set_key(F7);
  8860. _cimg_set_key(F8); _cimg_set_key(F9); _cimg_set_key(F10); _cimg_set_key(F11);
  8861. _cimg_set_key(F12); _cimg_set_key(PAUSE); _cimg_set_key(1); _cimg_set_key(2);
  8862. _cimg_set_key(3); _cimg_set_key(4); _cimg_set_key(5); _cimg_set_key(6);
  8863. _cimg_set_key(7); _cimg_set_key(8); _cimg_set_key(9); _cimg_set_key(0);
  8864. _cimg_set_key(BACKSPACE); _cimg_set_key(INSERT); _cimg_set_key(HOME);
  8865. _cimg_set_key(PAGEUP); _cimg_set_key(TAB); _cimg_set_key(Q); _cimg_set_key(W);
  8866. _cimg_set_key(E); _cimg_set_key(R); _cimg_set_key(T); _cimg_set_key(Y);
  8867. _cimg_set_key(U); _cimg_set_key(I); _cimg_set_key(O); _cimg_set_key(P);
  8868. _cimg_set_key(DELETE); _cimg_set_key(END); _cimg_set_key(PAGEDOWN);
  8869. _cimg_set_key(CAPSLOCK); _cimg_set_key(A); _cimg_set_key(S); _cimg_set_key(D);
  8870. _cimg_set_key(F); _cimg_set_key(G); _cimg_set_key(H); _cimg_set_key(J);
  8871. _cimg_set_key(K); _cimg_set_key(L); _cimg_set_key(ENTER);
  8872. _cimg_set_key(SHIFTLEFT); _cimg_set_key(Z); _cimg_set_key(X); _cimg_set_key(C);
  8873. _cimg_set_key(V); _cimg_set_key(B); _cimg_set_key(N); _cimg_set_key(M);
  8874. _cimg_set_key(SHIFTRIGHT); _cimg_set_key(ARROWUP); _cimg_set_key(CTRLLEFT);
  8875. _cimg_set_key(APPLEFT); _cimg_set_key(ALT); _cimg_set_key(SPACE); _cimg_set_key(ALTGR);
  8876. _cimg_set_key(APPRIGHT); _cimg_set_key(MENU); _cimg_set_key(CTRLRIGHT);
  8877. _cimg_set_key(ARROWLEFT); _cimg_set_key(ARROWDOWN); _cimg_set_key(ARROWRIGHT);
  8878. _cimg_set_key(PAD0); _cimg_set_key(PAD1); _cimg_set_key(PAD2);
  8879. _cimg_set_key(PAD3); _cimg_set_key(PAD4); _cimg_set_key(PAD5);
  8880. _cimg_set_key(PAD6); _cimg_set_key(PAD7); _cimg_set_key(PAD8);
  8881. _cimg_set_key(PAD9); _cimg_set_key(PADADD); _cimg_set_key(PADSUB);
  8882. _cimg_set_key(PADMUL); _cimg_set_key(PADDIV);
  8883. if (is_pressed) {
  8884. if (*_keys)
  8885. std::memmove((void*)(_keys + 1),(void*)_keys,127*sizeof(unsigned int));
  8886. *_keys = keycode;
  8887. if (*_released_keys) {
  8888. std::memmove((void*)(_released_keys + 1),(void*)_released_keys,127*sizeof(unsigned int));
  8889. *_released_keys = 0;
  8890. }
  8891. } else {
  8892. if (*_keys) {
  8893. std::memmove((void*)(_keys + 1),(void*)_keys,127*sizeof(unsigned int));
  8894. *_keys = 0;
  8895. }
  8896. if (*_released_keys)
  8897. std::memmove((void*)(_released_keys + 1),(void*)_released_keys,127*sizeof(unsigned int));
  8898. *_released_keys = keycode;
  8899. }
  8900. _is_event = keycode?true:false;
  8901. if (keycode) {
  8902. #if cimg_display==1
  8903. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  8904. #elif cimg_display==2
  8905. SetEvent(cimg::Win32_attr().wait_event);
  8906. #endif
  8907. }
  8908. return *this;
  8909. }
  8910. //! Flush all display events.
  8911. /**
  8912. \note Remove all passed events from the current display.
  8913. **/
  8914. CImgDisplay& flush() {
  8915. set_key().set_button().set_wheel();
  8916. _is_resized = _is_moved = _is_event = false;
  8917. _fps_timer = _fps_frames = _timer = 0;
  8918. _fps_fps = 0;
  8919. return *this;
  8920. }
  8921. //! Wait for any user event occurring on the current display.
  8922. CImgDisplay& wait() {
  8923. wait(*this);
  8924. return *this;
  8925. }
  8926. //! Wait for a given number of milliseconds since the last call to wait().
  8927. /**
  8928. \param milliseconds Number of milliseconds to wait for.
  8929. \note Similar to cimg::wait().
  8930. **/
  8931. CImgDisplay& wait(const unsigned int milliseconds) {
  8932. cimg::wait(milliseconds,&_timer);
  8933. return *this;
  8934. }
  8935. //! Wait for any event occurring on the display \c disp1.
  8936. static void wait(CImgDisplay& disp1) {
  8937. disp1._is_event = false;
  8938. while (!disp1._is_closed && !disp1._is_event) wait_all();
  8939. }
  8940. //! Wait for any event occurring either on the display \c disp1 or \c disp2.
  8941. static void wait(CImgDisplay& disp1, CImgDisplay& disp2) {
  8942. disp1._is_event = disp2._is_event = false;
  8943. while ((!disp1._is_closed || !disp2._is_closed) &&
  8944. !disp1._is_event && !disp2._is_event) wait_all();
  8945. }
  8946. //! Wait for any event occurring either on the display \c disp1, \c disp2 or \c disp3.
  8947. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3) {
  8948. disp1._is_event = disp2._is_event = disp3._is_event = false;
  8949. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed) &&
  8950. !disp1._is_event && !disp2._is_event && !disp3._is_event) wait_all();
  8951. }
  8952. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3 or \c disp4.
  8953. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4) {
  8954. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = false;
  8955. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed) &&
  8956. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event) wait_all();
  8957. }
  8958. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4 or \c disp5.
  8959. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4,
  8960. CImgDisplay& disp5) {
  8961. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = false;
  8962. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed) &&
  8963. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event)
  8964. wait_all();
  8965. }
  8966. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp6.
  8967. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
  8968. CImgDisplay& disp6) {
  8969. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
  8970. disp6._is_event = false;
  8971. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
  8972. !disp6._is_closed) &&
  8973. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
  8974. !disp6._is_event) wait_all();
  8975. }
  8976. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp7.
  8977. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
  8978. CImgDisplay& disp6, CImgDisplay& disp7) {
  8979. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
  8980. disp6._is_event = disp7._is_event = false;
  8981. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
  8982. !disp6._is_closed || !disp7._is_closed) &&
  8983. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
  8984. !disp6._is_event && !disp7._is_event) wait_all();
  8985. }
  8986. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp8.
  8987. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
  8988. CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8) {
  8989. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
  8990. disp6._is_event = disp7._is_event = disp8._is_event = false;
  8991. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
  8992. !disp6._is_closed || !disp7._is_closed || !disp8._is_closed) &&
  8993. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
  8994. !disp6._is_event && !disp7._is_event && !disp8._is_event) wait_all();
  8995. }
  8996. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp9.
  8997. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
  8998. CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9) {
  8999. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
  9000. disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = false;
  9001. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
  9002. !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed) &&
  9003. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
  9004. !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event) wait_all();
  9005. }
  9006. //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp10.
  9007. static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
  9008. CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9,
  9009. CImgDisplay& disp10) {
  9010. disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
  9011. disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = disp10._is_event = false;
  9012. while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
  9013. !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed || !disp10._is_closed) &&
  9014. !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
  9015. !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event && !disp10._is_event)
  9016. wait_all();
  9017. }
  9018. #if cimg_display==0
  9019. //! Wait for any window event occurring in any opened CImgDisplay.
  9020. static void wait_all() {
  9021. return _no_display_exception();
  9022. }
  9023. //! Render image into internal display buffer.
  9024. /**
  9025. \param img Input image data to render.
  9026. \note
  9027. - Convert image data representation into the internal display buffer (architecture-dependent structure).
  9028. - The content of the associated window is not modified, until paint() is called.
  9029. - Should not be used for common CImgDisplay uses, since display() is more useful.
  9030. **/
  9031. template<typename T>
  9032. CImgDisplay& render(const CImg<T>& img) {
  9033. return assign(img);
  9034. }
  9035. //! Paint internal display buffer on associated window.
  9036. /**
  9037. \note
  9038. - Update the content of the associated window with the internal display buffer, e.g. after a render() call.
  9039. - Should not be used for common CImgDisplay uses, since display() is more useful.
  9040. **/
  9041. CImgDisplay& paint() {
  9042. return assign();
  9043. }
  9044. //! Take a snapshot of the current screen content.
  9045. /**
  9046. \param x0 X-coordinate of the upper left corner.
  9047. \param y0 Y-coordinate of the upper left corner.
  9048. \param x1 X-coordinate of the lower right corner.
  9049. \param y1 Y-coordinate of the lower right corner.
  9050. \param[out] img Output screenshot. Can be empty on input
  9051. **/
  9052. template<typename T>
  9053. static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
  9054. cimg::unused(x0,y0,x1,y1,&img);
  9055. _no_display_exception();
  9056. }
  9057. //! Take a snapshot of the associated window content.
  9058. /**
  9059. \param[out] img Output snapshot. Can be empty on input.
  9060. **/
  9061. template<typename T>
  9062. const CImgDisplay& snapshot(CImg<T>& img) const {
  9063. cimg::unused(img);
  9064. _no_display_exception();
  9065. return *this;
  9066. }
  9067. #endif
  9068. // X11-based implementation
  9069. //--------------------------
  9070. #if cimg_display==1
  9071. Atom _wm_window_atom, _wm_protocol_atom;
  9072. Window _window, _background_window;
  9073. Colormap _colormap;
  9074. XImage *_image;
  9075. void *_data;
  9076. #ifdef cimg_use_xshm
  9077. XShmSegmentInfo *_shminfo;
  9078. #endif
  9079. static int screen_width() {
  9080. Display *const dpy = cimg::X11_attr().display;
  9081. int res = 0;
  9082. if (!dpy) {
  9083. Display *const _dpy = XOpenDisplay(0);
  9084. if (!_dpy)
  9085. throw CImgDisplayException("CImgDisplay::screen_width(): Failed to open X11 display.");
  9086. res = DisplayWidth(_dpy,DefaultScreen(_dpy));
  9087. XCloseDisplay(_dpy);
  9088. } else {
  9089. #ifdef cimg_use_xrandr
  9090. if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution)
  9091. res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width;
  9092. else res = DisplayWidth(dpy,DefaultScreen(dpy));
  9093. #else
  9094. res = DisplayWidth(dpy,DefaultScreen(dpy));
  9095. #endif
  9096. }
  9097. return res;
  9098. }
  9099. static int screen_height() {
  9100. Display *const dpy = cimg::X11_attr().display;
  9101. int res = 0;
  9102. if (!dpy) {
  9103. Display *const _dpy = XOpenDisplay(0);
  9104. if (!_dpy)
  9105. throw CImgDisplayException("CImgDisplay::screen_height(): Failed to open X11 display.");
  9106. res = DisplayHeight(_dpy,DefaultScreen(_dpy));
  9107. XCloseDisplay(_dpy);
  9108. } else {
  9109. #ifdef cimg_use_xrandr
  9110. if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution)
  9111. res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height;
  9112. else res = DisplayHeight(dpy,DefaultScreen(dpy));
  9113. #else
  9114. res = DisplayHeight(dpy,DefaultScreen(dpy));
  9115. #endif
  9116. }
  9117. return res;
  9118. }
  9119. static void wait_all() {
  9120. if (!cimg::X11_attr().display) return;
  9121. pthread_mutex_lock(&cimg::X11_attr().wait_event_mutex);
  9122. pthread_cond_wait(&cimg::X11_attr().wait_event,&cimg::X11_attr().wait_event_mutex);
  9123. pthread_mutex_unlock(&cimg::X11_attr().wait_event_mutex);
  9124. }
  9125. void _handle_events(const XEvent *const pevent) {
  9126. Display *const dpy = cimg::X11_attr().display;
  9127. XEvent event = *pevent;
  9128. switch (event.type) {
  9129. case ClientMessage : {
  9130. if ((int)event.xclient.message_type==(int)_wm_protocol_atom &&
  9131. (int)event.xclient.data.l[0]==(int)_wm_window_atom) {
  9132. XUnmapWindow(cimg::X11_attr().display,_window);
  9133. _is_closed = _is_event = true;
  9134. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  9135. }
  9136. } break;
  9137. case ConfigureNotify : {
  9138. while (XCheckWindowEvent(dpy,_window,StructureNotifyMask,&event)) {}
  9139. const unsigned int nw = event.xconfigure.width, nh = event.xconfigure.height;
  9140. const int nx = event.xconfigure.x, ny = event.xconfigure.y;
  9141. if (nw && nh && (nw!=_window_width || nh!=_window_height)) {
  9142. _window_width = nw; _window_height = nh; _mouse_x = _mouse_y = -1;
  9143. XResizeWindow(dpy,_window,_window_width,_window_height);
  9144. _is_resized = _is_event = true;
  9145. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  9146. }
  9147. if (nx!=_window_x || ny!=_window_y) {
  9148. _window_x = nx;
  9149. _window_y = ny;
  9150. _is_moved = _is_event = true;
  9151. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  9152. }
  9153. } break;
  9154. case Expose : {
  9155. while (XCheckWindowEvent(dpy,_window,ExposureMask,&event)) {}
  9156. _paint(false);
  9157. if (_is_fullscreen) {
  9158. XWindowAttributes attr;
  9159. do {
  9160. XGetWindowAttributes(dpy,_window,&attr);
  9161. if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
  9162. } while (attr.map_state!=IsViewable);
  9163. XSetInputFocus(dpy,_window,RevertToParent,CurrentTime);
  9164. }
  9165. } break;
  9166. case ButtonPress : {
  9167. do {
  9168. _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y;
  9169. if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
  9170. switch (event.xbutton.button) {
  9171. case 1 : set_button(1); break;
  9172. case 3 : set_button(2); break;
  9173. case 2 : set_button(3); break;
  9174. }
  9175. } while (XCheckWindowEvent(dpy,_window,ButtonPressMask,&event));
  9176. } break;
  9177. case ButtonRelease : {
  9178. do {
  9179. _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y;
  9180. if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
  9181. switch (event.xbutton.button) {
  9182. case 1 : set_button(1,false); break;
  9183. case 3 : set_button(2,false); break;
  9184. case 2 : set_button(3,false); break;
  9185. case 4 : set_wheel(1); break;
  9186. case 5 : set_wheel(-1); break;
  9187. }
  9188. } while (XCheckWindowEvent(dpy,_window,ButtonReleaseMask,&event));
  9189. } break;
  9190. case KeyPress : {
  9191. char tmp = 0; KeySym ksym;
  9192. XLookupString(&event.xkey,&tmp,1,&ksym,0);
  9193. set_key((unsigned int)ksym,true);
  9194. } break;
  9195. case KeyRelease : {
  9196. char keys_return[32]; // Check that the key has been physically unpressed
  9197. XQueryKeymap(dpy,keys_return);
  9198. const unsigned int kc = event.xkey.keycode, kc1 = kc/8, kc2 = kc%8;
  9199. const bool is_key_pressed = kc1>=32?false:(keys_return[kc1]>>kc2)&1;
  9200. if (!is_key_pressed) {
  9201. char tmp = 0; KeySym ksym;
  9202. XLookupString(&event.xkey,&tmp,1,&ksym,0);
  9203. set_key((unsigned int)ksym,false);
  9204. }
  9205. } break;
  9206. case EnterNotify: {
  9207. while (XCheckWindowEvent(dpy,_window,EnterWindowMask,&event)) {}
  9208. _mouse_x = event.xmotion.x;
  9209. _mouse_y = event.xmotion.y;
  9210. if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
  9211. } break;
  9212. case LeaveNotify : {
  9213. while (XCheckWindowEvent(dpy,_window,LeaveWindowMask,&event)) {}
  9214. _mouse_x = _mouse_y = -1; _is_event = true;
  9215. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  9216. } break;
  9217. case MotionNotify : {
  9218. while (XCheckWindowEvent(dpy,_window,PointerMotionMask,&event)) {}
  9219. _mouse_x = event.xmotion.x;
  9220. _mouse_y = event.xmotion.y;
  9221. if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
  9222. _is_event = true;
  9223. pthread_cond_broadcast(&cimg::X11_attr().wait_event);
  9224. } break;
  9225. }
  9226. }
  9227. static void* _events_thread(void *arg) { // Thread to manage events for all opened display windows
  9228. Display *const dpy = cimg::X11_attr().display;
  9229. XEvent event;
  9230. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,0);
  9231. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0);
  9232. if (!arg) for ( ; ; ) {
  9233. cimg_lock_display();
  9234. bool event_flag = XCheckTypedEvent(dpy,ClientMessage,&event);
  9235. if (!event_flag) event_flag = XCheckMaskEvent(dpy,
  9236. ExposureMask | StructureNotifyMask | ButtonPressMask |
  9237. KeyPressMask | PointerMotionMask | EnterWindowMask |
  9238. LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask,&event);
  9239. if (event_flag)
  9240. for (unsigned int i = 0; i<cimg::X11_attr().nb_wins; ++i)
  9241. if (!cimg::X11_attr().wins[i]->_is_closed && event.xany.window==cimg::X11_attr().wins[i]->_window)
  9242. cimg::X11_attr().wins[i]->_handle_events(&event);
  9243. cimg_unlock_display();
  9244. pthread_testcancel();
  9245. cimg::sleep(8);
  9246. }
  9247. return 0;
  9248. }
  9249. void _set_colormap(Colormap& cmap, const unsigned int dim) {
  9250. XColor *const colormap = new XColor[256];
  9251. switch (dim) {
  9252. case 1 : { // colormap for greyscale images
  9253. for (unsigned int index = 0; index<256; ++index) {
  9254. colormap[index].pixel = index;
  9255. colormap[index].red = colormap[index].green = colormap[index].blue = (unsigned short)(index<<8);
  9256. colormap[index].flags = DoRed | DoGreen | DoBlue;
  9257. }
  9258. } break;
  9259. case 2 : { // colormap for RG images
  9260. for (unsigned int index = 0, r = 8; r<256; r+=16)
  9261. for (unsigned int g = 8; g<256; g+=16) {
  9262. colormap[index].pixel = index;
  9263. colormap[index].red = colormap[index].blue = (unsigned short)(r<<8);
  9264. colormap[index].green = (unsigned short)(g<<8);
  9265. colormap[index++].flags = DoRed | DoGreen | DoBlue;
  9266. }
  9267. } break;
  9268. default : { // colormap for RGB images
  9269. for (unsigned int index = 0, r = 16; r<256; r+=32)
  9270. for (unsigned int g = 16; g<256; g+=32)
  9271. for (unsigned int b = 32; b<256; b+=64) {
  9272. colormap[index].pixel = index;
  9273. colormap[index].red = (unsigned short)(r<<8);
  9274. colormap[index].green = (unsigned short)(g<<8);
  9275. colormap[index].blue = (unsigned short)(b<<8);
  9276. colormap[index++].flags = DoRed | DoGreen | DoBlue;
  9277. }
  9278. }
  9279. }
  9280. XStoreColors(cimg::X11_attr().display,cmap,colormap,256);
  9281. delete[] colormap;
  9282. }
  9283. void _map_window() {
  9284. Display *const dpy = cimg::X11_attr().display;
  9285. bool is_exposed = false, is_mapped = false;
  9286. XWindowAttributes attr;
  9287. XEvent event;
  9288. XMapRaised(dpy,_window);
  9289. do { // Wait for the window to be mapped
  9290. XWindowEvent(dpy,_window,StructureNotifyMask | ExposureMask,&event);
  9291. switch (event.type) {
  9292. case MapNotify : is_mapped = true; break;
  9293. case Expose : is_exposed = true; break;
  9294. }
  9295. } while (!is_exposed || !is_mapped);
  9296. do { // Wait for the window to be visible
  9297. XGetWindowAttributes(dpy,_window,&attr);
  9298. if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
  9299. } while (attr.map_state!=IsViewable);
  9300. _window_x = attr.x;
  9301. _window_y = attr.y;
  9302. }
  9303. void _paint(const bool wait_expose=true) {
  9304. if (_is_closed || !_image) return;
  9305. Display *const dpy = cimg::X11_attr().display;
  9306. if (wait_expose) { // Send an expose event sticked to display window to force repaint
  9307. XEvent event;
  9308. event.xexpose.type = Expose;
  9309. event.xexpose.serial = 0;
  9310. event.xexpose.send_event = 1;
  9311. event.xexpose.display = dpy;
  9312. event.xexpose.window = _window;
  9313. event.xexpose.x = 0;
  9314. event.xexpose.y = 0;
  9315. event.xexpose.width = width();
  9316. event.xexpose.height = height();
  9317. event.xexpose.count = 0;
  9318. XSendEvent(dpy,_window,0,0,&event);
  9319. } else { // Repaint directly (may be called from the expose event)
  9320. GC gc = DefaultGC(dpy,DefaultScreen(dpy));
  9321. #ifdef cimg_use_xshm
  9322. if (_shminfo) XShmPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height,1);
  9323. else XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height);
  9324. #else
  9325. XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height);
  9326. #endif
  9327. }
  9328. }
  9329. template<typename T>
  9330. void _resize(T pixel_type, const unsigned int ndimx, const unsigned int ndimy, const bool force_redraw) {
  9331. Display *const dpy = cimg::X11_attr().display;
  9332. cimg::unused(pixel_type);
  9333. #ifdef cimg_use_xshm
  9334. if (_shminfo) {
  9335. XShmSegmentInfo *const nshminfo = new XShmSegmentInfo;
  9336. XImage *const nimage = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),
  9337. cimg::X11_attr().nb_bits,ZPixmap,0,nshminfo,ndimx,ndimy);
  9338. if (!nimage) { delete nshminfo; return; }
  9339. else {
  9340. nshminfo->shmid = shmget(IPC_PRIVATE,ndimx*ndimy*sizeof(T),IPC_CREAT | 0777);
  9341. if (nshminfo->shmid==-1) { XDestroyImage(nimage); delete nshminfo; return; }
  9342. else {
  9343. nshminfo->shmaddr = nimage->data = (char*)shmat(nshminfo->shmid,0,0);
  9344. if (nshminfo->shmaddr==(char*)-1) {
  9345. shmctl(nshminfo->shmid,IPC_RMID,0); XDestroyImage(nimage); delete nshminfo; return;
  9346. } else {
  9347. nshminfo->readOnly = 0;
  9348. cimg::X11_attr().is_shm_enabled = true;
  9349. XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm);
  9350. XShmAttach(dpy,nshminfo);
  9351. XFlush(dpy);
  9352. XSetErrorHandler(oldXErrorHandler);
  9353. if (!cimg::X11_attr().is_shm_enabled) {
  9354. shmdt(nshminfo->shmaddr);
  9355. shmctl(nshminfo->shmid,IPC_RMID,0);
  9356. XDestroyImage(nimage);
  9357. delete nshminfo;
  9358. return;
  9359. } else {
  9360. T *const ndata = (T*)nimage->data;
  9361. if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy);
  9362. else std::memset(ndata,0,sizeof(T)*ndimx*ndimy);
  9363. XShmDetach(dpy,_shminfo);
  9364. XDestroyImage(_image);
  9365. shmdt(_shminfo->shmaddr);
  9366. shmctl(_shminfo->shmid,IPC_RMID,0);
  9367. delete _shminfo;
  9368. _shminfo = nshminfo;
  9369. _image = nimage;
  9370. _data = (void*)ndata;
  9371. }
  9372. }
  9373. }
  9374. }
  9375. } else
  9376. #endif
  9377. {
  9378. T *ndata = (T*)std::malloc(ndimx*ndimy*sizeof(T));
  9379. if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy);
  9380. else std::memset(ndata,0,sizeof(T)*ndimx*ndimy);
  9381. _data = (void*)ndata;
  9382. XDestroyImage(_image);
  9383. _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),
  9384. cimg::X11_attr().nb_bits,ZPixmap,0,(char*)_data,ndimx,ndimy,8,0);
  9385. }
  9386. }
  9387. void _init_fullscreen() {
  9388. if (!_is_fullscreen || _is_closed) return;
  9389. Display *const dpy = cimg::X11_attr().display;
  9390. _background_window = 0;
  9391. #ifdef cimg_use_xrandr
  9392. int foo;
  9393. if (XRRQueryExtension(dpy,&foo,&foo)) {
  9394. XRRRotations(dpy,DefaultScreen(dpy),&cimg::X11_attr().curr_rotation);
  9395. if (!cimg::X11_attr().resolutions) {
  9396. cimg::X11_attr().resolutions = XRRSizes(dpy,DefaultScreen(dpy),&foo);
  9397. cimg::X11_attr().nb_resolutions = (unsigned int)foo;
  9398. }
  9399. if (cimg::X11_attr().resolutions) {
  9400. cimg::X11_attr().curr_resolution = 0;
  9401. for (unsigned int i = 0; i<cimg::X11_attr().nb_resolutions; ++i) {
  9402. const unsigned int
  9403. nw = (unsigned int)(cimg::X11_attr().resolutions[i].width),
  9404. nh = (unsigned int)(cimg::X11_attr().resolutions[i].height);
  9405. if (nw>=_width && nh>=_height &&
  9406. nw<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width) &&
  9407. nh<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height))
  9408. cimg::X11_attr().curr_resolution = i;
  9409. }
  9410. if (cimg::X11_attr().curr_resolution>0) {
  9411. XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy));
  9412. XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),
  9413. cimg::X11_attr().curr_resolution,cimg::X11_attr().curr_rotation,CurrentTime);
  9414. XRRFreeScreenConfigInfo(config);
  9415. XSync(dpy,0);
  9416. }
  9417. }
  9418. }
  9419. if (!cimg::X11_attr().resolutions)
  9420. cimg::warn(_cimgdisplay_instance
  9421. "init_fullscreen(): Xrandr extension not supported by the X server.",
  9422. cimgdisplay_instance);
  9423. #endif
  9424. const unsigned int sx = screen_width(), sy = screen_height();
  9425. if (sx==_width && sy==_height) return;
  9426. XSetWindowAttributes attr_set;
  9427. attr_set.background_pixel = XBlackPixel(dpy,XDefaultScreen(dpy));
  9428. attr_set.override_redirect = 1;
  9429. _background_window = XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,sx,sy,0,0,
  9430. InputOutput,CopyFromParent,CWBackPixel | CWOverrideRedirect,&attr_set);
  9431. XEvent event;
  9432. XSelectInput(dpy,_background_window,StructureNotifyMask);
  9433. XMapRaised(dpy,_background_window);
  9434. do XWindowEvent(dpy,_background_window,StructureNotifyMask,&event);
  9435. while (event.type!=MapNotify);
  9436. XWindowAttributes attr;
  9437. do {
  9438. XGetWindowAttributes(dpy,_background_window,&attr);
  9439. if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
  9440. } while (attr.map_state!=IsViewable);
  9441. }
  9442. void _desinit_fullscreen() {
  9443. if (!_is_fullscreen) return;
  9444. Display *const dpy = cimg::X11_attr().display;
  9445. XUngrabKeyboard(dpy,CurrentTime);
  9446. #ifdef cimg_use_xrandr
  9447. if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) {
  9448. XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy));
  9449. XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),0,cimg::X11_attr().curr_rotation,CurrentTime);
  9450. XRRFreeScreenConfigInfo(config);
  9451. XSync(dpy,0);
  9452. cimg::X11_attr().curr_resolution = 0;
  9453. }
  9454. #endif
  9455. if (_background_window) XDestroyWindow(dpy,_background_window);
  9456. _background_window = 0;
  9457. _is_fullscreen = false;
  9458. }
  9459. static int _assign_xshm(Display *dpy, XErrorEvent *error) {
  9460. cimg::unused(dpy,error);
  9461. cimg::X11_attr().is_shm_enabled = false;
  9462. return 0;
  9463. }
  9464. void _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0,
  9465. const unsigned int normalization_type=3,
  9466. const bool fullscreen_flag=false, const bool closed_flag=false) {
  9467. cimg::mutex(14);
  9468. // Allocate space for window title
  9469. const char *const nptitle = ptitle?ptitle:"";
  9470. const unsigned int s = (unsigned int)std::strlen(nptitle) + 1;
  9471. char *const tmp_title = s?new char[s]:0;
  9472. if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char));
  9473. // Destroy previous display window if existing
  9474. if (!is_empty()) assign();
  9475. // Open X11 display and retrieve graphical properties.
  9476. Display* &dpy = cimg::X11_attr().display;
  9477. if (!dpy) {
  9478. dpy = XOpenDisplay(0);
  9479. if (!dpy)
  9480. throw CImgDisplayException(_cimgdisplay_instance
  9481. "assign(): Failed to open X11 display.",
  9482. cimgdisplay_instance);
  9483. cimg::X11_attr().nb_bits = DefaultDepth(dpy,DefaultScreen(dpy));
  9484. if (cimg::X11_attr().nb_bits!=8 && cimg::X11_attr().nb_bits!=16 &&
  9485. cimg::X11_attr().nb_bits!=24 && cimg::X11_attr().nb_bits!=32)
  9486. throw CImgDisplayException(_cimgdisplay_instance
  9487. "assign(): Invalid %u bits screen mode detected "
  9488. "(only 8, 16, 24 and 32 bits modes are managed).",
  9489. cimgdisplay_instance,
  9490. cimg::X11_attr().nb_bits);
  9491. XVisualInfo vtemplate;
  9492. vtemplate.visualid = XVisualIDFromVisual(DefaultVisual(dpy,DefaultScreen(dpy)));
  9493. int nb_visuals;
  9494. XVisualInfo *vinfo = XGetVisualInfo(dpy,VisualIDMask,&vtemplate,&nb_visuals);
  9495. if (vinfo && vinfo->red_mask<vinfo->blue_mask) cimg::X11_attr().is_blue_first = true;
  9496. cimg::X11_attr().byte_order = ImageByteOrder(dpy);
  9497. XFree(vinfo);
  9498. cimg_lock_display();
  9499. cimg::X11_attr().events_thread = new pthread_t;
  9500. pthread_create(cimg::X11_attr().events_thread,0,_events_thread,0);
  9501. } else cimg_lock_display();
  9502. // Set display variables.
  9503. _width = std::min(dimw,(unsigned int)screen_width());
  9504. _height = std::min(dimh,(unsigned int)screen_height());
  9505. _normalization = normalization_type<4?normalization_type:3;
  9506. _is_fullscreen = fullscreen_flag;
  9507. _window_x = _window_y = cimg::type<int>::min();
  9508. _is_closed = closed_flag;
  9509. _title = tmp_title;
  9510. flush();
  9511. // Create X11 window (and LUT, if 8bits display)
  9512. if (_is_fullscreen) {
  9513. if (!_is_closed) _init_fullscreen();
  9514. const unsigned int sx = screen_width(), sy = screen_height();
  9515. XSetWindowAttributes attr_set;
  9516. attr_set.override_redirect = 1;
  9517. _window = XCreateWindow(dpy,DefaultRootWindow(dpy),(sx - _width)/2,(sy - _height)/2,_width,_height,0,0,
  9518. InputOutput,CopyFromParent,CWOverrideRedirect,&attr_set);
  9519. } else
  9520. _window = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,_width,_height,0,0L,0L);
  9521. XSelectInput(dpy,_window,
  9522. ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask | PointerMotionMask |
  9523. EnterWindowMask | LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask);
  9524. XStoreName(dpy,_window,_title?_title:" ");
  9525. if (cimg::X11_attr().nb_bits==8) {
  9526. _colormap = XCreateColormap(dpy,_window,DefaultVisual(dpy,DefaultScreen(dpy)),AllocAll);
  9527. _set_colormap(_colormap,3);
  9528. XSetWindowColormap(dpy,_window,_colormap);
  9529. }
  9530. static const char *const _window_class = cimg_appname;
  9531. XClassHint *const window_class = XAllocClassHint();
  9532. window_class->res_name = (char*)_window_class;
  9533. window_class->res_class = (char*)_window_class;
  9534. XSetClassHint(dpy,_window,window_class);
  9535. XFree(window_class);
  9536. _window_width = _width;
  9537. _window_height = _height;
  9538. // Create XImage
  9539. #ifdef cimg_use_xshm
  9540. _shminfo = 0;
  9541. if (XShmQueryExtension(dpy)) {
  9542. _shminfo = new XShmSegmentInfo;
  9543. _image = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits,
  9544. ZPixmap,0,_shminfo,_width,_height);
  9545. if (!_image) { delete _shminfo; _shminfo = 0; }
  9546. else {
  9547. _shminfo->shmid = shmget(IPC_PRIVATE,_image->bytes_per_line*_image->height,IPC_CREAT|0777);
  9548. if (_shminfo->shmid==-1) { XDestroyImage(_image); delete _shminfo; _shminfo = 0; }
  9549. else {
  9550. _shminfo->shmaddr = _image->data = (char*)(_data = shmat(_shminfo->shmid,0,0));
  9551. if (_shminfo->shmaddr==(char*)-1) {
  9552. shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); delete _shminfo; _shminfo = 0;
  9553. } else {
  9554. _shminfo->readOnly = 0;
  9555. cimg::X11_attr().is_shm_enabled = true;
  9556. XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm);
  9557. XShmAttach(dpy,_shminfo);
  9558. XSync(dpy,0);
  9559. XSetErrorHandler(oldXErrorHandler);
  9560. if (!cimg::X11_attr().is_shm_enabled) {
  9561. shmdt(_shminfo->shmaddr); shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image);
  9562. delete _shminfo; _shminfo = 0;
  9563. }
  9564. }
  9565. }
  9566. }
  9567. }
  9568. if (!_shminfo)
  9569. #endif
  9570. {
  9571. const cimg_ulong buf_size = (cimg_ulong)_width*_height*(cimg::X11_attr().nb_bits==8?1:
  9572. (cimg::X11_attr().nb_bits==16?2:4));
  9573. _data = std::malloc(buf_size);
  9574. _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits,
  9575. ZPixmap,0,(char*)_data,_width,_height,8,0);
  9576. }
  9577. _wm_window_atom = XInternAtom(dpy,"WM_DELETE_WINDOW",0);
  9578. _wm_protocol_atom = XInternAtom(dpy,"WM_PROTOCOLS",0);
  9579. XSetWMProtocols(dpy,_window,&_wm_window_atom,1);
  9580. if (_is_fullscreen) XGrabKeyboard(dpy,_window,1,GrabModeAsync,GrabModeAsync,CurrentTime);
  9581. cimg::X11_attr().wins[cimg::X11_attr().nb_wins++]=this;
  9582. if (!_is_closed) _map_window(); else _window_x = _window_y = cimg::type<int>::min();
  9583. cimg_unlock_display();
  9584. cimg::mutex(14,0);
  9585. }
  9586. CImgDisplay& assign() {
  9587. if (is_empty()) return flush();
  9588. Display *const dpy = cimg::X11_attr().display;
  9589. cimg_lock_display();
  9590. // Remove display window from event thread list.
  9591. unsigned int i;
  9592. for (i = 0; i<cimg::X11_attr().nb_wins && cimg::X11_attr().wins[i]!=this; ++i) {}
  9593. for ( ; i<cimg::X11_attr().nb_wins - 1; ++i) cimg::X11_attr().wins[i] = cimg::X11_attr().wins[i + 1];
  9594. --cimg::X11_attr().nb_wins;
  9595. // Destroy window, image, colormap and title.
  9596. if (_is_fullscreen && !_is_closed) _desinit_fullscreen();
  9597. #ifdef cimg_use_xshm
  9598. if (_shminfo) {
  9599. XShmDetach(dpy,_shminfo);
  9600. shmdt(_shminfo->shmaddr);
  9601. shmctl(_shminfo->shmid,IPC_RMID,0);
  9602. delete _shminfo;
  9603. _shminfo = 0;
  9604. }
  9605. #endif
  9606. XDestroyImage(_image);
  9607. if (cimg::X11_attr().nb_bits==8) XFreeColormap(dpy,_colormap);
  9608. XDestroyWindow(dpy,_window);
  9609. XSync(dpy,0);
  9610. _window = 0; _colormap = 0; _data = 0; _image = 0;
  9611. // Reset display variables.
  9612. delete[] _title;
  9613. _width = _height = _normalization = _window_width = _window_height = 0;
  9614. _window_x = _window_y = cimg::type<int>::min();
  9615. _is_fullscreen = false;
  9616. _is_closed = true;
  9617. _min = _max = 0;
  9618. _title = 0;
  9619. flush();
  9620. cimg_unlock_display();
  9621. return *this;
  9622. }
  9623. CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0,
  9624. const unsigned int normalization_type=3,
  9625. const bool fullscreen_flag=false, const bool closed_flag=false) {
  9626. if (!dimw || !dimh) return assign();
  9627. _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag);
  9628. _min = _max = 0;
  9629. std::memset(_data,0,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char):
  9630. (cimg::X11_attr().nb_bits==16?sizeof(unsigned short):sizeof(unsigned int)))*
  9631. (size_t)_width*_height);
  9632. return paint();
  9633. }
  9634. template<typename T>
  9635. CImgDisplay& assign(const CImg<T>& img, const char *const title=0,
  9636. const unsigned int normalization_type=3,
  9637. const bool fullscreen_flag=false, const bool closed_flag=false) {
  9638. if (!img) return assign();
  9639. CImg<T> tmp;
  9640. const CImg<T>& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
  9641. (img._height - 1)/2,
  9642. (img._depth - 1)/2));
  9643. _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
  9644. if (_normalization==2) _min = (float)nimg.min_max(_max);
  9645. return render(nimg).paint();
  9646. }
  9647. template<typename T>
  9648. CImgDisplay& assign(const CImgList<T>& list, const char *const title=0,
  9649. const unsigned int normalization_type=3,
  9650. const bool fullscreen_flag=false, const bool closed_flag=false) {
  9651. if (!list) return assign();
  9652. CImg<T> tmp;
  9653. const CImg<T> img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
  9654. (img._height - 1)/2,
  9655. (img._depth - 1)/2));
  9656. _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
  9657. if (_normalization==2) _min = (float)nimg.min_max(_max);
  9658. return render(nimg).paint();
  9659. }
  9660. CImgDisplay& assign(const CImgDisplay& disp) {
  9661. if (!disp) return assign();
  9662. _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed);
  9663. std::memcpy(_data,disp._data,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char):
  9664. cimg::X11_attr().nb_bits==16?sizeof(unsigned short):
  9665. sizeof(unsigned int))*(size_t)_width*_height);
  9666. return paint();
  9667. }
  9668. CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) {
  9669. if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign();
  9670. if (is_empty()) return assign(nwidth,nheight);
  9671. Display *const dpy = cimg::X11_attr().display;
  9672. const unsigned int
  9673. tmpdimx = (nwidth>0)?nwidth:(-nwidth*width()/100),
  9674. tmpdimy = (nheight>0)?nheight:(-nheight*height()/100),
  9675. dimx = tmpdimx?tmpdimx:1,
  9676. dimy = tmpdimy?tmpdimy:1;
  9677. if (_width!=dimx || _height!=dimy || _window_width!=dimx || _window_height!=dimy) {
  9678. show();
  9679. cimg_lock_display();
  9680. if (_window_width!=dimx || _window_height!=dimy) {
  9681. XWindowAttributes attr;
  9682. for (unsigned int i = 0; i<10; ++i) {
  9683. XResizeWindow(dpy,_window,dimx,dimy);
  9684. XGetWindowAttributes(dpy,_window,&attr);
  9685. if (attr.width==(int)dimx && attr.height==(int)dimy) break;
  9686. cimg::wait(5,&_timer);
  9687. }
  9688. }
  9689. if (_width!=dimx || _height!=dimy) switch (cimg::X11_attr().nb_bits) {
  9690. case 8 : { unsigned char pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break;
  9691. case 16 : { unsigned short pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break;
  9692. default : { unsigned int pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); }
  9693. }
  9694. _window_width = _width = dimx; _window_height = _height = dimy;
  9695. cimg_unlock_display();
  9696. }
  9697. _is_resized = false;
  9698. if (_is_fullscreen) move((screen_width() - _width)/2,(screen_height() - _height)/2);
  9699. if (force_redraw) return paint();
  9700. return *this;
  9701. }
  9702. CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
  9703. if (is_empty()) return *this;
  9704. if (force_redraw) {
  9705. const cimg_ulong buf_size = (cimg_ulong)_width*_height*
  9706. (cimg::X11_attr().nb_bits==8?1:(cimg::X11_attr().nb_bits==16?2:4));
  9707. void *image_data = std::malloc(buf_size);
  9708. std::memcpy(image_data,_data,buf_size);
  9709. assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
  9710. std::memcpy(_data,image_data,buf_size);
  9711. std::free(image_data);
  9712. return paint();
  9713. }
  9714. return assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
  9715. }
  9716. CImgDisplay& show() {
  9717. if (is_empty() || !_is_closed) return *this;
  9718. cimg_lock_display();
  9719. _is_closed = false;
  9720. if (_is_fullscreen) _init_fullscreen();
  9721. _map_window();
  9722. cimg_unlock_display();
  9723. return paint();
  9724. }
  9725. CImgDisplay& close() {
  9726. if (is_empty() || _is_closed) return *this;
  9727. Display *const dpy = cimg::X11_attr().display;
  9728. cimg_lock_display();
  9729. if (_is_fullscreen) _desinit_fullscreen();
  9730. XUnmapWindow(dpy,_window);
  9731. _window_x = _window_y = cimg::type<int>::min();
  9732. _is_closed = true;
  9733. cimg_unlock_display();
  9734. return *this;
  9735. }
  9736. CImgDisplay& move(const int posx, const int posy) {
  9737. if (is_empty()) return *this;
  9738. show();
  9739. if (_window_x!=posx || _window_y!=posy) {
  9740. Display *const dpy = cimg::X11_attr().display;
  9741. cimg_lock_display();
  9742. XMoveWindow(dpy,_window,posx,posy);
  9743. _window_x = posx;
  9744. _window_y = posy;
  9745. cimg_unlock_display();
  9746. }
  9747. _is_moved = false;
  9748. return paint();
  9749. }
  9750. CImgDisplay& show_mouse() {
  9751. if (is_empty()) return *this;
  9752. Display *const dpy = cimg::X11_attr().display;
  9753. cimg_lock_display();
  9754. XUndefineCursor(dpy,_window);
  9755. cimg_unlock_display();
  9756. return *this;
  9757. }
  9758. CImgDisplay& hide_mouse() {
  9759. if (is_empty()) return *this;
  9760. Display *const dpy = cimg::X11_attr().display;
  9761. cimg_lock_display();
  9762. static const char pix_data[8] = { 0 };
  9763. XColor col;
  9764. col.red = col.green = col.blue = 0;
  9765. Pixmap pix = XCreateBitmapFromData(dpy,_window,pix_data,8,8);
  9766. Cursor cur = XCreatePixmapCursor(dpy,pix,pix,&col,&col,0,0);
  9767. XFreePixmap(dpy,pix);
  9768. XDefineCursor(dpy,_window,cur);
  9769. cimg_unlock_display();
  9770. return *this;
  9771. }
  9772. CImgDisplay& set_mouse(const int posx, const int posy) {
  9773. if (is_empty() || _is_closed) return *this;
  9774. Display *const dpy = cimg::X11_attr().display;
  9775. cimg_lock_display();
  9776. XWarpPointer(dpy,0L,_window,0,0,0,0,posx,posy);
  9777. _mouse_x = posx; _mouse_y = posy;
  9778. _is_moved = false;
  9779. XSync(dpy,0);
  9780. cimg_unlock_display();
  9781. return *this;
  9782. }
  9783. CImgDisplay& set_title(const char *const format, ...) {
  9784. if (is_empty()) return *this;
  9785. char *const tmp = new char[1024];
  9786. va_list ap;
  9787. va_start(ap, format);
  9788. cimg_vsnprintf(tmp,1024,format,ap);
  9789. va_end(ap);
  9790. if (!std::strcmp(_title,tmp)) { delete[] tmp; return *this; }
  9791. delete[] _title;
  9792. const unsigned int s = (unsigned int)std::strlen(tmp) + 1;
  9793. _title = new char[s];
  9794. std::memcpy(_title,tmp,s*sizeof(char));
  9795. Display *const dpy = cimg::X11_attr().display;
  9796. cimg_lock_display();
  9797. XStoreName(dpy,_window,tmp);
  9798. cimg_unlock_display();
  9799. delete[] tmp;
  9800. return *this;
  9801. }
  9802. template<typename T>
  9803. CImgDisplay& display(const CImg<T>& img) {
  9804. if (!img)
  9805. throw CImgArgumentException(_cimgdisplay_instance
  9806. "display(): Empty specified image.",
  9807. cimgdisplay_instance);
  9808. if (is_empty()) return assign(img);
  9809. return render(img).paint(false);
  9810. }
  9811. CImgDisplay& paint(const bool wait_expose=true) {
  9812. if (is_empty()) return *this;
  9813. cimg_lock_display();
  9814. _paint(wait_expose);
  9815. cimg_unlock_display();
  9816. return *this;
  9817. }
  9818. template<typename T>
  9819. CImgDisplay& render(const CImg<T>& img, const bool flag8=false) {
  9820. if (!img)
  9821. throw CImgArgumentException(_cimgdisplay_instance
  9822. "render(): Empty specified image.",
  9823. cimgdisplay_instance);
  9824. if (is_empty()) return *this;
  9825. if (img._depth!=1) return render(img.get_projections2d((img._width - 1)/2,(img._height - 1)/2,
  9826. (img._depth - 1)/2));
  9827. if (cimg::X11_attr().nb_bits==8 && (img._width!=_width || img._height!=_height))
  9828. return render(img.get_resize(_width,_height,1,-100,1));
  9829. if (cimg::X11_attr().nb_bits==8 && !flag8 && img._spectrum==3) {
  9830. static const CImg<typename CImg<T>::ucharT> default_colormap = CImg<typename CImg<T>::ucharT>::default_LUT256();
  9831. return render(img.get_index(default_colormap,1,false));
  9832. }
  9833. const T
  9834. *data1 = img._data,
  9835. *data2 = (img._spectrum>1)?img.data(0,0,0,1):data1,
  9836. *data3 = (img._spectrum>2)?img.data(0,0,0,2):data1;
  9837. if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3);
  9838. cimg_lock_display();
  9839. if (!_normalization || (_normalization==3 && cimg::type<T>::string()==cimg::type<unsigned char>::string())) {
  9840. _min = _max = 0;
  9841. switch (cimg::X11_attr().nb_bits) {
  9842. case 8 : { // 256 colormap, no normalization
  9843. _set_colormap(_colormap,img._spectrum);
  9844. unsigned char
  9845. *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:
  9846. new unsigned char[(size_t)img._width*img._height],
  9847. *ptrd = (unsigned char*)ndata;
  9848. switch (img._spectrum) {
  9849. case 1 :
  9850. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  9851. (*ptrd++) = (unsigned char)*(data1++);
  9852. break;
  9853. case 2 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9854. const unsigned char
  9855. R = (unsigned char)*(data1++),
  9856. G = (unsigned char)*(data2++);
  9857. (*ptrd++) = (R&0xf0) | (G>>4);
  9858. } break;
  9859. default : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9860. const unsigned char
  9861. R = (unsigned char)*(data1++),
  9862. G = (unsigned char)*(data2++),
  9863. B = (unsigned char)*(data3++);
  9864. (*ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6);
  9865. }
  9866. }
  9867. if (ndata!=_data) {
  9868. _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height);
  9869. delete[] ndata;
  9870. }
  9871. } break;
  9872. case 16 : { // 16 bits colors, no normalization
  9873. unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:
  9874. new unsigned short[(size_t)img._width*img._height];
  9875. unsigned char *ptrd = (unsigned char*)ndata;
  9876. const unsigned int M = 248;
  9877. switch (img._spectrum) {
  9878. case 1 :
  9879. if (cimg::X11_attr().byte_order)
  9880. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9881. const unsigned char val = (unsigned char)*(data1++), G = val>>2;
  9882. ptrd[0] = (val&M) | (G>>3);
  9883. ptrd[1] = (G<<5) | (G>>1);
  9884. ptrd+=2;
  9885. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9886. const unsigned char val = (unsigned char)*(data1++), G = val>>2;
  9887. ptrd[0] = (G<<5) | (G>>1);
  9888. ptrd[1] = (val&M) | (G>>3);
  9889. ptrd+=2;
  9890. }
  9891. break;
  9892. case 2 :
  9893. if (cimg::X11_attr().byte_order)
  9894. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9895. const unsigned char G = (unsigned char)*(data2++)>>2;
  9896. ptrd[0] = ((unsigned char)*(data1++)&M) | (G>>3);
  9897. ptrd[1] = (G<<5);
  9898. ptrd+=2;
  9899. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9900. const unsigned char G = (unsigned char)*(data2++)>>2;
  9901. ptrd[0] = (G<<5);
  9902. ptrd[1] = ((unsigned char)*(data1++)&M) | (G>>3);
  9903. ptrd+=2;
  9904. }
  9905. break;
  9906. default :
  9907. if (cimg::X11_attr().byte_order)
  9908. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9909. const unsigned char G = (unsigned char)*(data2++)>>2;
  9910. ptrd[0] = ((unsigned char)*(data1++)&M) | (G>>3);
  9911. ptrd[1] = (G<<5) | ((unsigned char)*(data3++)>>3);
  9912. ptrd+=2;
  9913. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9914. const unsigned char G = (unsigned char)*(data2++)>>2;
  9915. ptrd[0] = (G<<5) | ((unsigned char)*(data3++)>>3);
  9916. ptrd[1] = ((unsigned char)*(data1++)&M) | (G>>3);
  9917. ptrd+=2;
  9918. }
  9919. }
  9920. if (ndata!=_data) {
  9921. _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height);
  9922. delete[] ndata;
  9923. }
  9924. } break;
  9925. default : { // 24 bits colors, no normalization
  9926. unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:
  9927. new unsigned int[(size_t)img._width*img._height];
  9928. if (sizeof(int)==4) { // 32 bits int uses optimized version
  9929. unsigned int *ptrd = ndata;
  9930. switch (img._spectrum) {
  9931. case 1 :
  9932. if (cimg::X11_attr().byte_order==cimg::endianness())
  9933. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9934. const unsigned char val = (unsigned char)*(data1++);
  9935. *(ptrd++) = (val<<16) | (val<<8) | val;
  9936. }
  9937. else
  9938. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9939. const unsigned char val = (unsigned char)*(data1++);
  9940. *(ptrd++) = (val<<16) | (val<<8) | val;
  9941. }
  9942. break;
  9943. case 2 :
  9944. if (cimg::X11_attr().byte_order==cimg::endianness())
  9945. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  9946. *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8);
  9947. else
  9948. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  9949. *(ptrd++) = ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8);
  9950. break;
  9951. default :
  9952. if (cimg::X11_attr().byte_order==cimg::endianness())
  9953. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  9954. *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) |
  9955. (unsigned char)*(data3++);
  9956. else
  9957. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  9958. *(ptrd++) = ((unsigned char)*(data3++)<<24) | ((unsigned char)*(data2++)<<16) |
  9959. ((unsigned char)*(data1++)<<8);
  9960. }
  9961. } else {
  9962. unsigned char *ptrd = (unsigned char*)ndata;
  9963. switch (img._spectrum) {
  9964. case 1 :
  9965. if (cimg::X11_attr().byte_order)
  9966. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9967. ptrd[0] = 0;
  9968. ptrd[1] = (unsigned char)*(data1++);
  9969. ptrd[2] = 0;
  9970. ptrd[3] = 0;
  9971. ptrd+=4;
  9972. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9973. ptrd[0] = 0;
  9974. ptrd[1] = 0;
  9975. ptrd[2] = (unsigned char)*(data1++);
  9976. ptrd[3] = 0;
  9977. ptrd+=4;
  9978. }
  9979. break;
  9980. case 2 :
  9981. if (cimg::X11_attr().byte_order) cimg::swap(data1,data2);
  9982. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9983. ptrd[0] = 0;
  9984. ptrd[1] = (unsigned char)*(data2++);
  9985. ptrd[2] = (unsigned char)*(data1++);
  9986. ptrd[3] = 0;
  9987. ptrd+=4;
  9988. }
  9989. break;
  9990. default :
  9991. if (cimg::X11_attr().byte_order)
  9992. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9993. ptrd[0] = 0;
  9994. ptrd[1] = (unsigned char)*(data1++);
  9995. ptrd[2] = (unsigned char)*(data2++);
  9996. ptrd[3] = (unsigned char)*(data3++);
  9997. ptrd+=4;
  9998. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  9999. ptrd[0] = (unsigned char)*(data3++);
  10000. ptrd[1] = (unsigned char)*(data2++);
  10001. ptrd[2] = (unsigned char)*(data1++);
  10002. ptrd[3] = 0;
  10003. ptrd+=4;
  10004. }
  10005. }
  10006. }
  10007. if (ndata!=_data) {
  10008. _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height);
  10009. delete[] ndata;
  10010. }
  10011. }
  10012. }
  10013. } else {
  10014. if (_normalization==3) {
  10015. if (sizeof(T)>1 && cimg::type<T>::string()!=cimg::type<bool>::string()) _min = (float)img.min_max(_max);
  10016. else { _min = (float)cimg::type<T>::min(); _max = (float)cimg::type<T>::max(); }
  10017. } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max);
  10018. const float delta = _max - _min, mm = 255/(delta?delta:1.f);
  10019. switch (cimg::X11_attr().nb_bits) {
  10020. case 8 : { // 256 colormap, with normalization
  10021. _set_colormap(_colormap,img._spectrum);
  10022. unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:
  10023. new unsigned char[(size_t)img._width*img._height];
  10024. unsigned char *ptrd = (unsigned char*)ndata;
  10025. switch (img._spectrum) {
  10026. case 1 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10027. const unsigned char R = (unsigned char)((*(data1++) - _min)*mm);
  10028. *(ptrd++) = R;
  10029. } break;
  10030. case 2 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10031. const unsigned char
  10032. R = (unsigned char)((*(data1++) - _min)*mm),
  10033. G = (unsigned char)((*(data2++) - _min)*mm);
  10034. (*ptrd++) = (R&0xf0) | (G>>4);
  10035. } break;
  10036. default :
  10037. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10038. const unsigned char
  10039. R = (unsigned char)((*(data1++) - _min)*mm),
  10040. G = (unsigned char)((*(data2++) - _min)*mm),
  10041. B = (unsigned char)((*(data3++) - _min)*mm);
  10042. *(ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6);
  10043. }
  10044. }
  10045. if (ndata!=_data) {
  10046. _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height);
  10047. delete[] ndata;
  10048. }
  10049. } break;
  10050. case 16 : { // 16 bits colors, with normalization
  10051. unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:
  10052. new unsigned short[(size_t)img._width*img._height];
  10053. unsigned char *ptrd = (unsigned char*)ndata;
  10054. const unsigned int M = 248;
  10055. switch (img._spectrum) {
  10056. case 1 :
  10057. if (cimg::X11_attr().byte_order)
  10058. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10059. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm), G = val>>2;
  10060. ptrd[0] = (val&M) | (G>>3);
  10061. ptrd[1] = (G<<5) | (val>>3);
  10062. ptrd+=2;
  10063. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10064. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm), G = val>>2;
  10065. ptrd[0] = (G<<5) | (val>>3);
  10066. ptrd[1] = (val&M) | (G>>3);
  10067. ptrd+=2;
  10068. }
  10069. break;
  10070. case 2 :
  10071. if (cimg::X11_attr().byte_order)
  10072. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10073. const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
  10074. ptrd[0] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
  10075. ptrd[1] = (G<<5);
  10076. ptrd+=2;
  10077. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10078. const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
  10079. ptrd[0] = (G<<5);
  10080. ptrd[1] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
  10081. ptrd+=2;
  10082. }
  10083. break;
  10084. default :
  10085. if (cimg::X11_attr().byte_order)
  10086. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10087. const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
  10088. ptrd[0] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
  10089. ptrd[1] = (G<<5) | ((unsigned char)((*(data3++) - _min)*mm)>>3);
  10090. ptrd+=2;
  10091. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10092. const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
  10093. ptrd[0] = (G<<5) | ((unsigned char)((*(data3++) - _min)*mm)>>3);
  10094. ptrd[1] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
  10095. ptrd+=2;
  10096. }
  10097. }
  10098. if (ndata!=_data) {
  10099. _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height);
  10100. delete[] ndata;
  10101. }
  10102. } break;
  10103. default : { // 24 bits colors, with normalization
  10104. unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:
  10105. new unsigned int[(size_t)img._width*img._height];
  10106. if (sizeof(int)==4) { // 32 bits int uses optimized version
  10107. unsigned int *ptrd = ndata;
  10108. switch (img._spectrum) {
  10109. case 1 :
  10110. if (cimg::X11_attr().byte_order==cimg::endianness())
  10111. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10112. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
  10113. *(ptrd++) = (val<<16) | (val<<8) | val;
  10114. }
  10115. else
  10116. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10117. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
  10118. *(ptrd++) = (val<<24) | (val<<16) | (val<<8);
  10119. }
  10120. break;
  10121. case 2 :
  10122. if (cimg::X11_attr().byte_order==cimg::endianness())
  10123. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  10124. *(ptrd++) =
  10125. ((unsigned char)((*(data1++) - _min)*mm)<<16) |
  10126. ((unsigned char)((*(data2++) - _min)*mm)<<8);
  10127. else
  10128. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  10129. *(ptrd++) =
  10130. ((unsigned char)((*(data2++) - _min)*mm)<<16) |
  10131. ((unsigned char)((*(data1++) - _min)*mm)<<8);
  10132. break;
  10133. default :
  10134. if (cimg::X11_attr().byte_order==cimg::endianness())
  10135. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  10136. *(ptrd++) =
  10137. ((unsigned char)((*(data1++) - _min)*mm)<<16) |
  10138. ((unsigned char)((*(data2++) - _min)*mm)<<8) |
  10139. (unsigned char)((*(data3++) - _min)*mm);
  10140. else
  10141. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
  10142. *(ptrd++) =
  10143. ((unsigned char)((*(data3++) - _min)*mm)<<24) |
  10144. ((unsigned char)((*(data2++) - _min)*mm)<<16) |
  10145. ((unsigned char)((*(data1++) - _min)*mm)<<8);
  10146. }
  10147. } else {
  10148. unsigned char *ptrd = (unsigned char*)ndata;
  10149. switch (img._spectrum) {
  10150. case 1 :
  10151. if (cimg::X11_attr().byte_order)
  10152. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10153. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
  10154. ptrd[0] = 0;
  10155. ptrd[1] = val;
  10156. ptrd[2] = val;
  10157. ptrd[3] = val;
  10158. ptrd+=4;
  10159. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10160. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
  10161. ptrd[0] = val;
  10162. ptrd[1] = val;
  10163. ptrd[2] = val;
  10164. ptrd[3] = 0;
  10165. ptrd+=4;
  10166. }
  10167. break;
  10168. case 2 :
  10169. if (cimg::X11_attr().byte_order) cimg::swap(data1,data2);
  10170. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10171. ptrd[0] = 0;
  10172. ptrd[1] = (unsigned char)((*(data2++) - _min)*mm);
  10173. ptrd[2] = (unsigned char)((*(data1++) - _min)*mm);
  10174. ptrd[3] = 0;
  10175. ptrd+=4;
  10176. }
  10177. break;
  10178. default :
  10179. if (cimg::X11_attr().byte_order)
  10180. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10181. ptrd[0] = 0;
  10182. ptrd[1] = (unsigned char)((*(data1++) - _min)*mm);
  10183. ptrd[2] = (unsigned char)((*(data2++) - _min)*mm);
  10184. ptrd[3] = (unsigned char)((*(data3++) - _min)*mm);
  10185. ptrd+=4;
  10186. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10187. ptrd[0] = (unsigned char)((*(data3++) - _min)*mm);
  10188. ptrd[1] = (unsigned char)((*(data2++) - _min)*mm);
  10189. ptrd[2] = (unsigned char)((*(data1++) - _min)*mm);
  10190. ptrd[3] = 0;
  10191. ptrd+=4;
  10192. }
  10193. }
  10194. }
  10195. if (ndata!=_data) {
  10196. _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height);
  10197. delete[] ndata;
  10198. }
  10199. }
  10200. }
  10201. }
  10202. cimg_unlock_display();
  10203. return *this;
  10204. }
  10205. template<typename T>
  10206. static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
  10207. img.assign();
  10208. Display *dpy = cimg::X11_attr().display;
  10209. cimg_lock_display();
  10210. if (!dpy) {
  10211. dpy = XOpenDisplay(0);
  10212. if (!dpy)
  10213. throw CImgDisplayException("CImgDisplay::screenshot(): Failed to open X11 display.");
  10214. }
  10215. Window root = DefaultRootWindow(dpy);
  10216. XWindowAttributes gwa;
  10217. XGetWindowAttributes(dpy,root,&gwa);
  10218. const int width = gwa.width, height = gwa.height;
  10219. int _x0 = x0, _y0 = y0, _x1 = x1, _y1 = y1;
  10220. if (_x0>_x1) cimg::swap(_x0,_x1);
  10221. if (_y0>_y1) cimg::swap(_y0,_y1);
  10222. XImage *image = 0;
  10223. if (_x1>=0 && _x0<width && _y1>=0 && _y0<height) {
  10224. _x0 = std::max(_x0,0);
  10225. _y0 = std::max(_y0,0);
  10226. _x1 = std::min(_x1,width - 1);
  10227. _y1 = std::min(_y1,height - 1);
  10228. image = XGetImage(dpy,root,_x0,_y0,_x1 - _x0 + 1,_y1 - _y0 + 1,AllPlanes,ZPixmap);
  10229. if (image) {
  10230. const unsigned long
  10231. red_mask = image->red_mask,
  10232. green_mask = image->green_mask,
  10233. blue_mask = image->blue_mask;
  10234. img.assign(image->width,image->height,1,3);
  10235. T *pR = img.data(0,0,0,0), *pG = img.data(0,0,0,1), *pB = img.data(0,0,0,2);
  10236. cimg_forXY(img,x,y) {
  10237. const unsigned long pixel = XGetPixel(image,x,y);
  10238. *(pR++) = (T)((pixel & red_mask)>>16);
  10239. *(pG++) = (T)((pixel & green_mask)>>8);
  10240. *(pB++) = (T)(pixel & blue_mask);
  10241. }
  10242. XDestroyImage(image);
  10243. }
  10244. }
  10245. if (!cimg::X11_attr().display) XCloseDisplay(dpy);
  10246. cimg_unlock_display();
  10247. if (img.is_empty())
  10248. throw CImgDisplayException("CImgDisplay::screenshot(): Failed to take screenshot "
  10249. "with coordinates (%d,%d)-(%d,%d).",
  10250. x0,y0,x1,y1);
  10251. }
  10252. template<typename T>
  10253. const CImgDisplay& snapshot(CImg<T>& img) const {
  10254. if (is_empty()) { img.assign(); return *this; }
  10255. const unsigned char *ptrs = (unsigned char*)_data;
  10256. img.assign(_width,_height,1,3);
  10257. T
  10258. *data1 = img.data(0,0,0,0),
  10259. *data2 = img.data(0,0,0,1),
  10260. *data3 = img.data(0,0,0,2);
  10261. if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3);
  10262. switch (cimg::X11_attr().nb_bits) {
  10263. case 8 : {
  10264. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10265. const unsigned char val = *(ptrs++);
  10266. *(data1++) = (T)(val&0xe0);
  10267. *(data2++) = (T)((val&0x1c)<<3);
  10268. *(data3++) = (T)(val<<6);
  10269. }
  10270. } break;
  10271. case 16 : {
  10272. if (cimg::X11_attr().byte_order) for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10273. const unsigned char
  10274. val0 = ptrs[0],
  10275. val1 = ptrs[1];
  10276. ptrs+=2;
  10277. *(data1++) = (T)(val0&0xf8);
  10278. *(data2++) = (T)((val0<<5) | ((val1&0xe0)>>5));
  10279. *(data3++) = (T)(val1<<3);
  10280. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10281. const unsigned short
  10282. val0 = ptrs[0],
  10283. val1 = ptrs[1];
  10284. ptrs+=2;
  10285. *(data1++) = (T)(val1&0xf8);
  10286. *(data2++) = (T)((val1<<5) | ((val0&0xe0)>>5));
  10287. *(data3++) = (T)(val0<<3);
  10288. }
  10289. } break;
  10290. default : {
  10291. if (cimg::X11_attr().byte_order) for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10292. ++ptrs;
  10293. *(data1++) = (T)ptrs[0];
  10294. *(data2++) = (T)ptrs[1];
  10295. *(data3++) = (T)ptrs[2];
  10296. ptrs+=3;
  10297. } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10298. *(data3++) = (T)ptrs[0];
  10299. *(data2++) = (T)ptrs[1];
  10300. *(data1++) = (T)ptrs[2];
  10301. ptrs+=3;
  10302. ++ptrs;
  10303. }
  10304. }
  10305. }
  10306. return *this;
  10307. }
  10308. // Windows-based implementation.
  10309. //-------------------------------
  10310. #elif cimg_display==2
  10311. bool _is_mouse_tracked, _is_cursor_visible;
  10312. HANDLE _thread, _is_created, _mutex;
  10313. HWND _window, _background_window;
  10314. CLIENTCREATESTRUCT _ccs;
  10315. unsigned int *_data;
  10316. DEVMODE _curr_mode;
  10317. BITMAPINFO _bmi;
  10318. HDC _hdc;
  10319. static int screen_width() {
  10320. DEVMODE mode;
  10321. mode.dmSize = sizeof(DEVMODE);
  10322. mode.dmDriverExtra = 0;
  10323. EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode);
  10324. return (int)mode.dmPelsWidth;
  10325. }
  10326. static int screen_height() {
  10327. DEVMODE mode;
  10328. mode.dmSize = sizeof(DEVMODE);
  10329. mode.dmDriverExtra = 0;
  10330. EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode);
  10331. return (int)mode.dmPelsHeight;
  10332. }
  10333. static void wait_all() {
  10334. WaitForSingleObject(cimg::Win32_attr().wait_event,INFINITE);
  10335. }
  10336. static LRESULT APIENTRY _handle_events(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) {
  10337. #ifdef _WIN64
  10338. CImgDisplay *const disp = (CImgDisplay*)GetWindowLongPtr(window,GWLP_USERDATA);
  10339. #else
  10340. CImgDisplay *const disp = (CImgDisplay*)GetWindowLong(window,GWL_USERDATA);
  10341. #endif
  10342. MSG st_msg;
  10343. switch (msg) {
  10344. case WM_CLOSE :
  10345. disp->_mouse_x = disp->_mouse_y = -1;
  10346. disp->_window_x = disp->_window_y = cimg::type<int>::min();
  10347. disp->set_button().set_key(0).set_key(0,false)._is_closed = true;
  10348. ReleaseMutex(disp->_mutex);
  10349. ShowWindow(disp->_window,SW_HIDE);
  10350. disp->_is_event = true;
  10351. SetEvent(cimg::Win32_attr().wait_event);
  10352. return 0;
  10353. case WM_SIZE : {
  10354. while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {}
  10355. WaitForSingleObject(disp->_mutex,INFINITE);
  10356. const unsigned int nw = LOWORD(lParam),nh = HIWORD(lParam);
  10357. if (nw && nh && (nw!=disp->_width || nh!=disp->_height)) {
  10358. disp->_window_width = nw;
  10359. disp->_window_height = nh;
  10360. disp->_mouse_x = disp->_mouse_y = -1;
  10361. disp->_is_resized = disp->_is_event = true;
  10362. SetEvent(cimg::Win32_attr().wait_event);
  10363. }
  10364. ReleaseMutex(disp->_mutex);
  10365. } break;
  10366. case WM_MOVE : {
  10367. while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {}
  10368. WaitForSingleObject(disp->_mutex,INFINITE);
  10369. const int nx = (int)(short)(LOWORD(lParam)), ny = (int)(short)(HIWORD(lParam));
  10370. if (nx!=disp->_window_x || ny!=disp->_window_y) {
  10371. disp->_window_x = nx;
  10372. disp->_window_y = ny;
  10373. disp->_is_moved = disp->_is_event = true;
  10374. SetEvent(cimg::Win32_attr().wait_event);
  10375. }
  10376. ReleaseMutex(disp->_mutex);
  10377. } break;
  10378. case WM_PAINT :
  10379. disp->paint();
  10380. cimg_lock_display();
  10381. if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE_WIN)>=0);
  10382. cimg_unlock_display();
  10383. break;
  10384. case WM_ERASEBKGND :
  10385. // return 0;
  10386. break;
  10387. case WM_KEYDOWN :
  10388. disp->set_key((unsigned int)wParam);
  10389. SetEvent(cimg::Win32_attr().wait_event);
  10390. break;
  10391. case WM_KEYUP :
  10392. disp->set_key((unsigned int)wParam,false);
  10393. SetEvent(cimg::Win32_attr().wait_event);
  10394. break;
  10395. case WM_MOUSEMOVE : {
  10396. while (PeekMessage(&st_msg,window,WM_MOUSEMOVE,WM_MOUSEMOVE,PM_REMOVE)) {}
  10397. disp->_mouse_x = LOWORD(lParam);
  10398. disp->_mouse_y = HIWORD(lParam);
  10399. #if (_WIN32_WINNT>=0x0400) && !defined(NOTRACKMOUSEEVENT)
  10400. if (!disp->_is_mouse_tracked) {
  10401. TRACKMOUSEEVENT tme;
  10402. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  10403. tme.dwFlags = TME_LEAVE;
  10404. tme.hwndTrack = disp->_window;
  10405. if (TrackMouseEvent(&tme)) disp->_is_mouse_tracked = true;
  10406. }
  10407. #endif
  10408. if (disp->_mouse_x<0 || disp->_mouse_y<0 || disp->_mouse_x>=disp->width() || disp->_mouse_y>=disp->height())
  10409. disp->_mouse_x = disp->_mouse_y = -1;
  10410. disp->_is_event = true;
  10411. SetEvent(cimg::Win32_attr().wait_event);
  10412. cimg_lock_display();
  10413. if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE_WIN)>=0);
  10414. cimg_unlock_display();
  10415. } break;
  10416. case WM_MOUSELEAVE : {
  10417. disp->_mouse_x = disp->_mouse_y = -1;
  10418. disp->_is_mouse_tracked = false;
  10419. cimg_lock_display();
  10420. while (ShowCursor(TRUE)<0) {}
  10421. cimg_unlock_display();
  10422. } break;
  10423. case WM_LBUTTONDOWN :
  10424. disp->set_button(1);
  10425. SetEvent(cimg::Win32_attr().wait_event);
  10426. break;
  10427. case WM_RBUTTONDOWN :
  10428. disp->set_button(2);
  10429. SetEvent(cimg::Win32_attr().wait_event);
  10430. break;
  10431. case WM_MBUTTONDOWN :
  10432. disp->set_button(3);
  10433. SetEvent(cimg::Win32_attr().wait_event);
  10434. break;
  10435. case WM_LBUTTONUP :
  10436. disp->set_button(1,false);
  10437. SetEvent(cimg::Win32_attr().wait_event);
  10438. break;
  10439. case WM_RBUTTONUP :
  10440. disp->set_button(2,false);
  10441. SetEvent(cimg::Win32_attr().wait_event);
  10442. break;
  10443. case WM_MBUTTONUP :
  10444. disp->set_button(3,false);
  10445. SetEvent(cimg::Win32_attr().wait_event);
  10446. break;
  10447. case 0x020A : // WM_MOUSEWHEEL:
  10448. disp->set_wheel((int)((short)HIWORD(wParam))/120);
  10449. SetEvent(cimg::Win32_attr().wait_event);
  10450. }
  10451. return DefWindowProc(window,msg,wParam,lParam);
  10452. }
  10453. static DWORD WINAPI _events_thread(void* arg) {
  10454. CImgDisplay *const disp = (CImgDisplay*)(((void**)arg)[0]);
  10455. const char *const title = (const char*)(((void**)arg)[1]);
  10456. MSG msg;
  10457. delete[] (void**)arg;
  10458. disp->_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  10459. disp->_bmi.bmiHeader.biWidth = disp->width();
  10460. disp->_bmi.bmiHeader.biHeight = -disp->height();
  10461. disp->_bmi.bmiHeader.biPlanes = 1;
  10462. disp->_bmi.bmiHeader.biBitCount = 32;
  10463. disp->_bmi.bmiHeader.biCompression = BI_RGB;
  10464. disp->_bmi.bmiHeader.biSizeImage = 0;
  10465. disp->_bmi.bmiHeader.biXPelsPerMeter = 1;
  10466. disp->_bmi.bmiHeader.biYPelsPerMeter = 1;
  10467. disp->_bmi.bmiHeader.biClrUsed = 0;
  10468. disp->_bmi.bmiHeader.biClrImportant = 0;
  10469. disp->_data = new unsigned int[(size_t)disp->_width*disp->_height];
  10470. if (!disp->_is_fullscreen) { // Normal window
  10471. RECT rect;
  10472. rect.left = rect.top = 0; rect.right = (LONG)disp->_width - 1; rect.bottom = (LONG)disp->_height - 1;
  10473. AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
  10474. const int
  10475. border1 = (int)((rect.right - rect.left + 1 - disp->_width)/2),
  10476. border2 = (int)(rect.bottom - rect.top + 1 - disp->_height - border1),
  10477. ww = disp->width() + 2*border1,
  10478. wh = disp->height() + border1 + border2,
  10479. sw = CImgDisplay::screen_width(),
  10480. sh = CImgDisplay::screen_height();
  10481. int
  10482. wx = (int)cimg::round(cimg::rand(0,sw - ww -1)),
  10483. wy = (int)cimg::round(cimg::rand(64,sh - wh - 65));
  10484. if (wx + ww>=sw) wx = sw - ww;
  10485. if (wy + wh>=sh) wy = sh - wh;
  10486. if (wx<0) wx = 0;
  10487. if (wy<0) wy = 0;
  10488. disp->_window = CreateWindowA("MDICLIENT",title?title:" ",
  10489. (DWORD)(WS_OVERLAPPEDWINDOW | (disp->_is_closed?0:WS_VISIBLE)),
  10490. wx,wy,ww,wh,0,0,0,&(disp->_ccs));
  10491. if (!disp->_is_closed) {
  10492. GetWindowRect(disp->_window,&rect);
  10493. disp->_window_x = rect.left;
  10494. disp->_window_y = rect.top;
  10495. } else disp->_window_x = disp->_window_y = cimg::type<int>::min();
  10496. } else { // Fullscreen window
  10497. const unsigned int
  10498. sx = (unsigned int)screen_width(),
  10499. sy = (unsigned int)screen_height();
  10500. disp->_window = CreateWindowA("MDICLIENT",title?title:" ",
  10501. (DWORD)(WS_POPUP | (disp->_is_closed?0:WS_VISIBLE)),
  10502. (int)(sx - disp->_width)/2,
  10503. (int)(sy - disp->_height)/2,
  10504. disp->width(),disp->height(),0,0,0,&(disp->_ccs));
  10505. disp->_window_x = disp->_window_y = 0;
  10506. }
  10507. SetForegroundWindow(disp->_window);
  10508. disp->_hdc = GetDC(disp->_window);
  10509. disp->_window_width = disp->_width;
  10510. disp->_window_height = disp->_height;
  10511. disp->flush();
  10512. #ifdef _WIN64
  10513. SetWindowLongPtr(disp->_window,GWLP_USERDATA,(LONG_PTR)disp);
  10514. SetWindowLongPtr(disp->_window,GWLP_WNDPROC,(LONG_PTR)_handle_events);
  10515. #else
  10516. SetWindowLong(disp->_window,GWL_USERDATA,(LONG)disp);
  10517. SetWindowLong(disp->_window,GWL_WNDPROC,(LONG)_handle_events);
  10518. #endif
  10519. SetEvent(disp->_is_created);
  10520. while (GetMessage(&msg,0,0,0)) DispatchMessage(&msg);
  10521. return 0;
  10522. }
  10523. CImgDisplay& _update_window_pos() {
  10524. if (_is_closed) _window_x = _window_y = cimg::type<int>::min();
  10525. else {
  10526. RECT rect;
  10527. rect.left = rect.top = 0; rect.right = (LONG)_width - 1; rect.bottom = (LONG)_height - 1;
  10528. AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
  10529. GetWindowRect(_window,&rect);
  10530. _window_x = rect.left;
  10531. _window_y = rect.top;
  10532. }
  10533. return *this;
  10534. }
  10535. void _init_fullscreen() {
  10536. _background_window = 0;
  10537. if (!_is_fullscreen || _is_closed) _curr_mode.dmSize = 0;
  10538. else {
  10539. /* DEVMODE mode;
  10540. unsigned int imode = 0, ibest = 0, bestbpp = 0, bw = ~0U, bh = ~0U;
  10541. for (mode.dmSize = sizeof(DEVMODE), mode.dmDriverExtra = 0; EnumDisplaySettings(0,imode,&mode); ++imode) {
  10542. const unsigned int nw = mode.dmPelsWidth, nh = mode.dmPelsHeight;
  10543. if (nw>=_width && nh>=_height && mode.dmBitsPerPel>=bestbpp && nw<=bw && nh<=bh) {
  10544. bestbpp = mode.dmBitsPerPel;
  10545. ibest = imode;
  10546. bw = nw; bh = nh;
  10547. }
  10548. }
  10549. if (bestbpp) {
  10550. _curr_mode.dmSize = sizeof(DEVMODE); _curr_mode.dmDriverExtra = 0;
  10551. EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&_curr_mode);
  10552. EnumDisplaySettings(0,ibest,&mode);
  10553. ChangeDisplaySettings(&mode,0);
  10554. } else _curr_mode.dmSize = 0;
  10555. */
  10556. _curr_mode.dmSize = 0;
  10557. const unsigned int
  10558. sx = (unsigned int)screen_width(),
  10559. sy = (unsigned int)screen_height();
  10560. if (sx!=_width || sy!=_height) {
  10561. CLIENTCREATESTRUCT background_ccs = { 0,0 };
  10562. _background_window = CreateWindowA("MDICLIENT","",WS_POPUP | WS_VISIBLE,
  10563. 0,0,(int)sx,(int)sy,0,0,0,&background_ccs);
  10564. SetForegroundWindow(_background_window);
  10565. }
  10566. }
  10567. }
  10568. void _desinit_fullscreen() {
  10569. if (!_is_fullscreen) return;
  10570. if (_background_window) DestroyWindow(_background_window);
  10571. _background_window = 0;
  10572. if (_curr_mode.dmSize) ChangeDisplaySettings(&_curr_mode,0);
  10573. _is_fullscreen = false;
  10574. }
  10575. CImgDisplay& _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0,
  10576. const unsigned int normalization_type=3,
  10577. const bool fullscreen_flag=false, const bool closed_flag=false) {
  10578. // Allocate space for window title
  10579. const char *const nptitle = ptitle?ptitle:"";
  10580. const unsigned int s = (unsigned int)std::strlen(nptitle) + 1;
  10581. char *const tmp_title = s?new char[s]:0;
  10582. if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char));
  10583. // Destroy previous window if existing
  10584. if (!is_empty()) assign();
  10585. // Set display variables
  10586. _width = std::min(dimw,(unsigned int)screen_width());
  10587. _height = std::min(dimh,(unsigned int)screen_height());
  10588. _normalization = normalization_type<4?normalization_type:3;
  10589. _is_fullscreen = fullscreen_flag;
  10590. _window_x = _window_y = cimg::type<int>::min();
  10591. _is_closed = closed_flag;
  10592. _is_cursor_visible = true;
  10593. _is_mouse_tracked = false;
  10594. _title = tmp_title;
  10595. flush();
  10596. if (_is_fullscreen) _init_fullscreen();
  10597. // Create event thread
  10598. void *const arg = (void*)(new void*[2]);
  10599. ((void**)arg)[0] = (void*)this;
  10600. ((void**)arg)[1] = (void*)_title;
  10601. _mutex = CreateMutex(0,FALSE_WIN,0);
  10602. _is_created = CreateEvent(0,FALSE_WIN,FALSE_WIN,0);
  10603. _thread = CreateThread(0,0,_events_thread,arg,0,0);
  10604. WaitForSingleObject(_is_created,INFINITE);
  10605. return *this;
  10606. }
  10607. CImgDisplay& assign() {
  10608. if (is_empty()) return flush();
  10609. DestroyWindow(_window);
  10610. TerminateThread(_thread,0);
  10611. delete[] _data;
  10612. delete[] _title;
  10613. _data = 0;
  10614. _title = 0;
  10615. if (_is_fullscreen) _desinit_fullscreen();
  10616. _width = _height = _normalization = _window_width = _window_height = 0;
  10617. _window_x = _window_y = cimg::type<int>::min();
  10618. _is_fullscreen = false;
  10619. _is_closed = true;
  10620. _min = _max = 0;
  10621. _title = 0;
  10622. flush();
  10623. return *this;
  10624. }
  10625. CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0,
  10626. const unsigned int normalization_type=3,
  10627. const bool fullscreen_flag=false, const bool closed_flag=false) {
  10628. if (!dimw || !dimh) return assign();
  10629. _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag);
  10630. _min = _max = 0;
  10631. std::memset(_data,0,sizeof(unsigned int)*_width*_height);
  10632. return paint();
  10633. }
  10634. template<typename T>
  10635. CImgDisplay& assign(const CImg<T>& img, const char *const title=0,
  10636. const unsigned int normalization_type=3,
  10637. const bool fullscreen_flag=false, const bool closed_flag=false) {
  10638. if (!img) return assign();
  10639. CImg<T> tmp;
  10640. const CImg<T>& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
  10641. (img._height - 1)/2,
  10642. (img._depth - 1)/2));
  10643. _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
  10644. if (_normalization==2) _min = (float)nimg.min_max(_max);
  10645. return display(nimg);
  10646. }
  10647. template<typename T>
  10648. CImgDisplay& assign(const CImgList<T>& list, const char *const title=0,
  10649. const unsigned int normalization_type=3,
  10650. const bool fullscreen_flag=false, const bool closed_flag=false) {
  10651. if (!list) return assign();
  10652. CImg<T> tmp;
  10653. const CImg<T> img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
  10654. (img._height - 1)/2,
  10655. (img._depth - 1)/2));
  10656. _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
  10657. if (_normalization==2) _min = (float)nimg.min_max(_max);
  10658. return display(nimg);
  10659. }
  10660. CImgDisplay& assign(const CImgDisplay& disp) {
  10661. if (!disp) return assign();
  10662. _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed);
  10663. std::memcpy(_data,disp._data,sizeof(unsigned int)*_width*_height);
  10664. return paint();
  10665. }
  10666. CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) {
  10667. if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign();
  10668. if (is_empty()) return assign((unsigned int)nwidth,(unsigned int)nheight);
  10669. const unsigned int
  10670. tmpdimx = (nwidth>0)?nwidth:(-nwidth*_width/100),
  10671. tmpdimy = (nheight>0)?nheight:(-nheight*_height/100),
  10672. dimx = tmpdimx?tmpdimx:1,
  10673. dimy = tmpdimy?tmpdimy:1;
  10674. if (_width!=dimx || _height!=dimy || _window_width!=dimx || _window_height!=dimy) {
  10675. if (_window_width!=dimx || _window_height!=dimy) {
  10676. RECT rect; rect.left = rect.top = 0; rect.right = (LONG)dimx - 1; rect.bottom = (LONG)dimy - 1;
  10677. AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
  10678. const int cwidth = rect.right - rect.left + 1, cheight = rect.bottom - rect.top + 1;
  10679. SetWindowPos(_window,0,0,0,cwidth,cheight,SWP_NOMOVE | SWP_NOZORDER | SWP_NOCOPYBITS);
  10680. }
  10681. if (_width!=dimx || _height!=dimy) {
  10682. unsigned int *const ndata = new unsigned int[dimx*dimy];
  10683. if (force_redraw) _render_resize(_data,_width,_height,ndata,dimx,dimy);
  10684. else std::memset(ndata,0x80,sizeof(unsigned int)*dimx*dimy);
  10685. delete[] _data;
  10686. _data = ndata;
  10687. _bmi.bmiHeader.biWidth = (LONG)dimx;
  10688. _bmi.bmiHeader.biHeight = -(int)dimy;
  10689. _width = dimx;
  10690. _height = dimy;
  10691. }
  10692. _window_width = dimx; _window_height = dimy;
  10693. show();
  10694. }
  10695. _is_resized = false;
  10696. if (_is_fullscreen) move((screen_width() - width())/2,(screen_height() - height())/2);
  10697. if (force_redraw) return paint();
  10698. return *this;
  10699. }
  10700. CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
  10701. if (is_empty()) return *this;
  10702. if (force_redraw) {
  10703. const cimg_ulong buf_size = (cimg_ulong)_width*_height*4;
  10704. void *odata = std::malloc(buf_size);
  10705. if (odata) {
  10706. std::memcpy(odata,_data,buf_size);
  10707. assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
  10708. std::memcpy(_data,odata,buf_size);
  10709. std::free(odata);
  10710. }
  10711. return paint();
  10712. }
  10713. return assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
  10714. }
  10715. CImgDisplay& show() {
  10716. if (is_empty() || !_is_closed) return *this;
  10717. _is_closed = false;
  10718. if (_is_fullscreen) _init_fullscreen();
  10719. ShowWindow(_window,SW_SHOW);
  10720. _update_window_pos();
  10721. return paint();
  10722. }
  10723. CImgDisplay& close() {
  10724. if (is_empty() || _is_closed) return *this;
  10725. _is_closed = true;
  10726. if (_is_fullscreen) _desinit_fullscreen();
  10727. ShowWindow(_window,SW_HIDE);
  10728. _window_x = _window_y = cimg::type<int>::min();
  10729. return *this;
  10730. }
  10731. CImgDisplay& move(const int posx, const int posy) {
  10732. if (is_empty()) return *this;
  10733. if (_window_x!=posx || _window_y!=posy) {
  10734. SetWindowPos(_window,0,posx,posy,0,0,SWP_NOSIZE | SWP_NOZORDER);
  10735. _window_x = posx;
  10736. _window_y = posy;
  10737. }
  10738. show();
  10739. _is_moved = false;
  10740. return *this;
  10741. }
  10742. CImgDisplay& show_mouse() {
  10743. if (is_empty()) return *this;
  10744. _is_cursor_visible = true;
  10745. return *this;
  10746. }
  10747. CImgDisplay& hide_mouse() {
  10748. if (is_empty()) return *this;
  10749. _is_cursor_visible = false;
  10750. return *this;
  10751. }
  10752. CImgDisplay& set_mouse(const int posx, const int posy) {
  10753. if (is_empty() || _is_closed || posx<0 || posy<0) return *this;
  10754. if (!_is_closed) {
  10755. _update_window_pos();
  10756. const int res = (int)SetCursorPos(_window_x + posx,_window_y + posy);
  10757. if (res) { _mouse_x = posx; _mouse_y = posy; }
  10758. }
  10759. return *this;
  10760. }
  10761. CImgDisplay& set_title(const char *const format, ...) {
  10762. if (is_empty()) return *this;
  10763. char *const tmp = new char[1024];
  10764. va_list ap;
  10765. va_start(ap, format);
  10766. cimg_vsnprintf(tmp,1024,format,ap);
  10767. va_end(ap);
  10768. if (!std::strcmp(_title,tmp)) { delete[] tmp; return *this; }
  10769. delete[] _title;
  10770. const unsigned int s = (unsigned int)std::strlen(tmp) + 1;
  10771. _title = new char[s];
  10772. std::memcpy(_title,tmp,s*sizeof(char));
  10773. SetWindowTextA(_window, tmp);
  10774. delete[] tmp;
  10775. return *this;
  10776. }
  10777. template<typename T>
  10778. CImgDisplay& display(const CImg<T>& img) {
  10779. if (!img)
  10780. throw CImgArgumentException(_cimgdisplay_instance
  10781. "display(): Empty specified image.",
  10782. cimgdisplay_instance);
  10783. if (is_empty()) return assign(img);
  10784. return render(img).paint();
  10785. }
  10786. CImgDisplay& paint() {
  10787. if (_is_closed) return *this;
  10788. WaitForSingleObject(_mutex,INFINITE);
  10789. SetDIBitsToDevice(_hdc,0,0,_width,_height,0,0,0,_height,_data,&_bmi,DIB_RGB_COLORS);
  10790. ReleaseMutex(_mutex);
  10791. return *this;
  10792. }
  10793. template<typename T>
  10794. CImgDisplay& render(const CImg<T>& img) {
  10795. if (!img)
  10796. throw CImgArgumentException(_cimgdisplay_instance
  10797. "render(): Empty specified image.",
  10798. cimgdisplay_instance);
  10799. if (is_empty()) return *this;
  10800. if (img._depth!=1) return render(img.get_projections2d((img._width - 1)/2,(img._height - 1)/2,
  10801. (img._depth - 1)/2));
  10802. const T
  10803. *data1 = img._data,
  10804. *data2 = (img._spectrum>=2)?img.data(0,0,0,1):data1,
  10805. *data3 = (img._spectrum>=3)?img.data(0,0,0,2):data1;
  10806. WaitForSingleObject(_mutex,INFINITE);
  10807. unsigned int
  10808. *const ndata = (img._width==_width && img._height==_height)?_data:
  10809. new unsigned int[(size_t)img._width*img._height],
  10810. *ptrd = ndata;
  10811. if (!_normalization || (_normalization==3 && cimg::type<T>::string()==cimg::type<unsigned char>::string())) {
  10812. _min = _max = 0;
  10813. switch (img._spectrum) {
  10814. case 1 : {
  10815. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10816. const unsigned char val = (unsigned char)*(data1++);
  10817. *(ptrd++) = (unsigned int)((val<<16) | (val<<8) | val);
  10818. }
  10819. } break;
  10820. case 2 : {
  10821. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10822. const unsigned char
  10823. R = (unsigned char)*(data1++),
  10824. G = (unsigned char)*(data2++);
  10825. *(ptrd++) = (unsigned int)((R<<16) | (G<<8));
  10826. }
  10827. } break;
  10828. default : {
  10829. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10830. const unsigned char
  10831. R = (unsigned char)*(data1++),
  10832. G = (unsigned char)*(data2++),
  10833. B = (unsigned char)*(data3++);
  10834. *(ptrd++) = (unsigned int)((R<<16) | (G<<8) | B);
  10835. }
  10836. }
  10837. }
  10838. } else {
  10839. if (_normalization==3) {
  10840. if (cimg::type<T>::is_float()) _min = (float)img.min_max(_max);
  10841. else {
  10842. _min = (float)cimg::type<T>::min();
  10843. _max = (float)cimg::type<T>::max();
  10844. }
  10845. } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max);
  10846. const float delta = _max - _min, mm = 255/(delta?delta:1.f);
  10847. switch (img._spectrum) {
  10848. case 1 : {
  10849. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10850. const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
  10851. *(ptrd++) = (unsigned int)((val<<16) | (val<<8) | val);
  10852. }
  10853. } break;
  10854. case 2 : {
  10855. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10856. const unsigned char
  10857. R = (unsigned char)((*(data1++) - _min)*mm),
  10858. G = (unsigned char)((*(data2++) - _min)*mm);
  10859. *(ptrd++) = (unsigned int)((R<<16) | (G<<8));
  10860. }
  10861. } break;
  10862. default : {
  10863. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10864. const unsigned char
  10865. R = (unsigned char)((*(data1++) - _min)*mm),
  10866. G = (unsigned char)((*(data2++) - _min)*mm),
  10867. B = (unsigned char)((*(data3++) - _min)*mm);
  10868. *(ptrd++) = (unsigned int)((R<<16) | (G<<8) | B);
  10869. }
  10870. }
  10871. }
  10872. }
  10873. if (ndata!=_data) { _render_resize(ndata,img._width,img._height,_data,_width,_height); delete[] ndata; }
  10874. ReleaseMutex(_mutex);
  10875. return *this;
  10876. }
  10877. template<typename T>
  10878. static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
  10879. img.assign();
  10880. HDC hScreen = GetDC(GetDesktopWindow());
  10881. if (hScreen) {
  10882. const int
  10883. width = GetDeviceCaps(hScreen,HORZRES),
  10884. height = GetDeviceCaps(hScreen,VERTRES);
  10885. int _x0 = x0, _y0 = y0, _x1 = x1, _y1 = y1;
  10886. if (_x0>_x1) cimg::swap(_x0,_x1);
  10887. if (_y0>_y1) cimg::swap(_y0,_y1);
  10888. if (_x1>=0 && _x0<width && _y1>=0 && _y0<height) {
  10889. _x0 = std::max(_x0,0);
  10890. _y0 = std::max(_y0,0);
  10891. _x1 = std::min(_x1,width - 1);
  10892. _y1 = std::min(_y1,height - 1);
  10893. const int bw = _x1 - _x0 + 1, bh = _y1 - _y0 + 1;
  10894. HDC hdcMem = CreateCompatibleDC(hScreen);
  10895. if (hdcMem) {
  10896. HBITMAP hBitmap = CreateCompatibleBitmap(hScreen,bw,bh);
  10897. if (hBitmap) {
  10898. HGDIOBJ hOld = SelectObject(hdcMem,hBitmap);
  10899. if (hOld && BitBlt(hdcMem,0,0,bw,bh,hScreen,_x0,_y0,SRCCOPY) && SelectObject(hdcMem,hOld)) {
  10900. BITMAPINFOHEADER bmi;
  10901. bmi.biSize = sizeof(BITMAPINFOHEADER);
  10902. bmi.biWidth = bw;
  10903. bmi.biHeight = -bh;
  10904. bmi.biPlanes = 1;
  10905. bmi.biBitCount = 32;
  10906. bmi.biCompression = BI_RGB;
  10907. bmi.biSizeImage = 0;
  10908. bmi.biXPelsPerMeter = bmi.biYPelsPerMeter = 0;
  10909. bmi.biClrUsed = bmi.biClrImportant = 0;
  10910. unsigned char *buf = new unsigned char[4*bw*bh];
  10911. if (GetDIBits(hdcMem,hBitmap,0,bh,buf,(BITMAPINFO*)&bmi,DIB_RGB_COLORS)) {
  10912. img.assign(bw,bh,1,3);
  10913. const unsigned char *ptrs = buf;
  10914. T *pR = img.data(0,0,0,0), *pG = img.data(0,0,0,1), *pB = img.data(0,0,0,2);
  10915. cimg_forXY(img,x,y) {
  10916. *(pR++) = (T)ptrs[2];
  10917. *(pG++) = (T)ptrs[1];
  10918. *(pB++) = (T)ptrs[0];
  10919. ptrs+=4;
  10920. }
  10921. }
  10922. delete[] buf;
  10923. }
  10924. DeleteObject(hBitmap);
  10925. }
  10926. DeleteDC(hdcMem);
  10927. }
  10928. }
  10929. ReleaseDC(GetDesktopWindow(),hScreen);
  10930. }
  10931. if (img.is_empty())
  10932. throw CImgDisplayException("CImgDisplay::screenshot(): Failed to take screenshot "
  10933. "with coordinates (%d,%d)-(%d,%d).",
  10934. x0,y0,x1,y1);
  10935. }
  10936. template<typename T>
  10937. const CImgDisplay& snapshot(CImg<T>& img) const {
  10938. if (is_empty()) { img.assign(); return *this; }
  10939. const unsigned int *ptrs = _data;
  10940. img.assign(_width,_height,1,3);
  10941. T
  10942. *data1 = img.data(0,0,0,0),
  10943. *data2 = img.data(0,0,0,1),
  10944. *data3 = img.data(0,0,0,2);
  10945. for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
  10946. const unsigned int val = *(ptrs++);
  10947. *(data1++) = (T)(unsigned char)(val>>16);
  10948. *(data2++) = (T)(unsigned char)((val>>8)&0xFF);
  10949. *(data3++) = (T)(unsigned char)(val&0xFF);
  10950. }
  10951. return *this;
  10952. }
  10953. #endif
  10954. //@}
  10955. }; // struct CImgDisplay { ...
  10956. /*
  10957. #--------------------------------------
  10958. #
  10959. #
  10960. #
  10961. # Definition of the CImg<T> structure
  10962. #
  10963. #
  10964. #
  10965. #--------------------------------------
  10966. */
  10967. //! Class representing an image (up to 4 dimensions wide), each pixel being of type \c T.
  10968. /**
  10969. This is the main class of the %CImg Library. It declares and constructs
  10970. an image, allows access to its pixel values, and is able to perform various image operations.
  10971. \par Image representation
  10972. A %CImg image is defined as an instance of the container \c CImg<T>, which contains a regular grid of pixels,
  10973. each pixel value being of type \c T. The image grid can have up to 4 dimensions: width, height, depth
  10974. and number of channels.
  10975. Usually, the three first dimensions are used to describe spatial coordinates <tt>(x,y,z)</tt>,
  10976. while the number of channels is rather used as a vector-valued dimension
  10977. (it may describe the R,G,B color channels for instance).
  10978. If you need a fifth dimension, you can use image lists \c CImgList<T> rather than simple images \c CImg<T>.
  10979. Thus, the \c CImg<T> class is able to represent volumetric images of vector-valued pixels,
  10980. as well as images with less dimensions (1D scalar signal, 2D color images, ...).
  10981. Most member functions of the class CImg<\c T> are designed to handle this maximum case of (3+1) dimensions.
  10982. Concerning the pixel value type \c T:
  10983. fully supported template types are the basic C++ types: <tt>unsigned char, char, short, unsigned int, int,
  10984. unsigned long, long, float, double, ... </tt>.
  10985. Typically, fast image display can be done using <tt>CImg<unsigned char></tt> images,
  10986. while complex image processing algorithms may be rather coded using <tt>CImg<float></tt> or <tt>CImg<double></tt>
  10987. images that have floating-point pixel values. The default value for the template T is \c float.
  10988. Using your own template types may be possible. However, you will certainly have to define the complete set
  10989. of arithmetic and logical operators for your class.
  10990. \par Image structure
  10991. The \c CImg<T> structure contains \e six fields:
  10992. - \c _width defines the number of \a columns of the image (size along the X-axis).
  10993. - \c _height defines the number of \a rows of the image (size along the Y-axis).
  10994. - \c _depth defines the number of \a slices of the image (size along the Z-axis).
  10995. - \c _spectrum defines the number of \a channels of the image (size along the C-axis).
  10996. - \c _data defines a \a pointer to the \a pixel \a data (of type \c T).
  10997. - \c _is_shared is a boolean that tells if the memory buffer \c data is shared with
  10998. another image.
  10999. You can access these fields publicly although it is recommended to use the dedicated functions
  11000. width(), height(), depth(), spectrum() and ptr() to do so.
  11001. Image dimensions are not limited to a specific range (as long as you got enough available memory).
  11002. A value of \e 1 usually means that the corresponding dimension is \a flat.
  11003. If one of the dimensions is \e 0, or if the data pointer is null, the image is considered as \e empty.
  11004. Empty images should not contain any pixel data and thus, will not be processed by CImg member functions
  11005. (a CImgInstanceException will be thrown instead).
  11006. Pixel data are stored in memory, in a non interlaced mode (See \ref cimg_storage).
  11007. \par Image declaration and construction
  11008. Declaring an image can be done by using one of the several available constructors.
  11009. Here is a list of the most used:
  11010. - Construct images from arbitrary dimensions:
  11011. - <tt>CImg<char> img;</tt> declares an empty image.
  11012. - <tt>CImg<unsigned char> img(128,128);</tt> declares a 128x128 greyscale image with
  11013. \c unsigned \c char pixel values.
  11014. - <tt>CImg<double> img(3,3);</tt> declares a 3x3 matrix with \c double coefficients.
  11015. - <tt>CImg<unsigned char> img(256,256,1,3);</tt> declares a 256x256x1x3 (color) image
  11016. (colors are stored as an image with three channels).
  11017. - <tt>CImg<double> img(128,128,128);</tt> declares a 128x128x128 volumetric and greyscale image
  11018. (with \c double pixel values).
  11019. - <tt>CImg<> img(128,128,128,3);</tt> declares a 128x128x128 volumetric color image
  11020. (with \c float pixels, which is the default value of the template parameter \c T).
  11021. - \b Note: images pixels are <b>not automatically initialized to 0</b>. You may use the function \c fill() to
  11022. do it, or use the specific constructor taking 5 parameters like this:
  11023. <tt>CImg<> img(128,128,128,3,0);</tt> declares a 128x128x128 volumetric color image with all pixel values to 0.
  11024. - Construct images from filenames:
  11025. - <tt>CImg<unsigned char> img("image.jpg");</tt> reads a JPEG color image from the file "image.jpg".
  11026. - <tt>CImg<float> img("analyze.hdr");</tt> reads a volumetric image (ANALYZE7.5 format) from the
  11027. file "analyze.hdr".
  11028. - \b Note: You need to install <a href="http://www.imagemagick.org">ImageMagick</a>
  11029. to be able to read common compressed image formats (JPG,PNG, ...) (See \ref cimg_files_io).
  11030. - Construct images from C-style arrays:
  11031. - <tt>CImg<int> img(data_buffer,256,256);</tt> constructs a 256x256 greyscale image from a \c int* buffer
  11032. \c data_buffer (of size 256x256=65536).
  11033. - <tt>CImg<unsigned char> img(data_buffer,256,256,1,3);</tt> constructs a 256x256 color image
  11034. from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels follow each others).
  11035. The complete list of constructors can be found <a href="#constructors">here</a>.
  11036. \par Most useful functions
  11037. The \c CImg<T> class contains a lot of functions that operates on images.
  11038. Some of the most useful are:
  11039. - operator()(): Read or write pixel values.
  11040. - display(): displays the image in a new window.
  11041. **/
  11042. template<typename T>
  11043. struct CImg {
  11044. unsigned int _width, _height, _depth, _spectrum;
  11045. bool _is_shared;
  11046. T *_data;
  11047. //! Simple iterator type, to loop through each pixel value of an image instance.
  11048. /**
  11049. \note
  11050. - The \c CImg<T>::iterator type is defined to be a <tt>T*</tt>.
  11051. - You will seldom have to use iterators in %CImg, most classical operations
  11052. being achieved (often in a faster way) using methods of \c CImg<T>.
  11053. \par Example
  11054. \code
  11055. CImg<float> img("reference.jpg"); // Load image from file
  11056. // Set all pixels to '0', with a CImg iterator.
  11057. for (CImg<float>::iterator it = img.begin(), it<img.end(); ++it) *it = 0;
  11058. img.fill(0); // Do the same with a built-in method
  11059. \endcode
  11060. **/
  11061. typedef T* iterator;
  11062. //! Simple const iterator type, to loop through each pixel value of a \c const image instance.
  11063. /**
  11064. \note
  11065. - The \c CImg<T>::const_iterator type is defined to be a \c const \c T*.
  11066. - You will seldom have to use iterators in %CImg, most classical operations
  11067. being achieved (often in a faster way) using methods of \c CImg<T>.
  11068. \par Example
  11069. \code
  11070. const CImg<float> img("reference.jpg"); // Load image from file
  11071. float sum = 0;
  11072. // Compute sum of all pixel values, with a CImg iterator.
  11073. for (CImg<float>::iterator it = img.begin(), it<img.end(); ++it) sum+=*it;
  11074. const float sum2 = img.sum(); // Do the same with a built-in method
  11075. \endcode
  11076. **/
  11077. typedef const T* const_iterator;
  11078. //! Pixel value type.
  11079. /**
  11080. Refer to the type of the pixel values of an image instance.
  11081. \note
  11082. - The \c CImg<T>::value_type type of a \c CImg<T> is defined to be a \c T.
  11083. - \c CImg<T>::value_type is actually not used in %CImg methods. It has been mainly defined for
  11084. compatibility with STL naming conventions.
  11085. **/
  11086. typedef T value_type;
  11087. // Define common types related to template type T.
  11088. typedef typename cimg::superset<T,bool>::type Tbool;
  11089. typedef typename cimg::superset<T,unsigned char>::type Tuchar;
  11090. typedef typename cimg::superset<T,char>::type Tchar;
  11091. typedef typename cimg::superset<T,unsigned short>::type Tushort;
  11092. typedef typename cimg::superset<T,short>::type Tshort;
  11093. typedef typename cimg::superset<T,unsigned int>::type Tuint;
  11094. typedef typename cimg::superset<T,int>::type Tint;
  11095. typedef typename cimg::superset<T,cimg_ulong>::type Tulong;
  11096. typedef typename cimg::superset<T,cimg_long>::type Tlong;
  11097. typedef typename cimg::superset<T,float>::type Tfloat;
  11098. typedef typename cimg::superset<T,double>::type Tdouble;
  11099. typedef typename cimg::last<T,bool>::type boolT;
  11100. typedef typename cimg::last<T,unsigned char>::type ucharT;
  11101. typedef typename cimg::last<T,char>::type charT;
  11102. typedef typename cimg::last<T,unsigned short>::type ushortT;
  11103. typedef typename cimg::last<T,short>::type shortT;
  11104. typedef typename cimg::last<T,unsigned int>::type uintT;
  11105. typedef typename cimg::last<T,int>::type intT;
  11106. typedef typename cimg::last<T,cimg_ulong>::type ulongT;
  11107. typedef typename cimg::last<T,cimg_long>::type longT;
  11108. typedef typename cimg::last<T,cimg_uint64>::type uint64T;
  11109. typedef typename cimg::last<T,cimg_int64>::type int64T;
  11110. typedef typename cimg::last<T,float>::type floatT;
  11111. typedef typename cimg::last<T,double>::type doubleT;
  11112. // Return 'dx*dy*dz*dc' as a 'size_t' and check no overflow occurs.
  11113. static size_t safe_size(const unsigned int dx, const unsigned int dy,
  11114. const unsigned int dz, const unsigned int dc) {
  11115. if (!(dx && dy && dz && dc)) return 0;
  11116. size_t siz = (size_t)dx, osiz = siz;
  11117. if ((dy==1 || (siz*=dy)>osiz) &&
  11118. ((osiz = siz), dz==1 || (siz*=dz)>osiz) &&
  11119. ((osiz = siz), dc==1 || (siz*=dc)>osiz) &&
  11120. ((osiz = siz), sizeof(T)==1 || (siz*sizeof(T))>osiz)) return siz;
  11121. throw CImgArgumentException("CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) overflows 'size_t'.",
  11122. pixel_type(),dx,dy,dz,dc);
  11123. }
  11124. //@}
  11125. //---------------------------
  11126. //
  11127. //! \name Plugins
  11128. //@{
  11129. //---------------------------
  11130. #ifdef cimg_plugin
  11131. #include cimg_plugin
  11132. #endif
  11133. #ifdef cimg_plugin1
  11134. #include cimg_plugin1
  11135. #endif
  11136. #ifdef cimg_plugin2
  11137. #include cimg_plugin2
  11138. #endif
  11139. #ifdef cimg_plugin3
  11140. #include cimg_plugin3
  11141. #endif
  11142. #ifdef cimg_plugin4
  11143. #include cimg_plugin4
  11144. #endif
  11145. #ifdef cimg_plugin5
  11146. #include cimg_plugin5
  11147. #endif
  11148. #ifdef cimg_plugin6
  11149. #include cimg_plugin6
  11150. #endif
  11151. #ifdef cimg_plugin7
  11152. #include cimg_plugin7
  11153. #endif
  11154. #ifdef cimg_plugin8
  11155. #include cimg_plugin8
  11156. #endif
  11157. //@}
  11158. //---------------------------------------------------------
  11159. //
  11160. //! \name Constructors / Destructor / Instance Management
  11161. //@{
  11162. //---------------------------------------------------------
  11163. //! Destroy image.
  11164. /**
  11165. \note
  11166. - The pixel buffer data() is deallocated if necessary, e.g. for non-empty and non-shared image instances.
  11167. - Destroying an empty or shared image does nothing actually.
  11168. \warning
  11169. - When destroying a non-shared image, make sure that you will \e not operate on a remaining shared image
  11170. that shares its buffer with the destroyed instance, in order to avoid further invalid memory access
  11171. (to a deallocated buffer).
  11172. **/
  11173. ~CImg() {
  11174. if (!_is_shared) delete[] _data;
  11175. }
  11176. //! Construct empty image.
  11177. /**
  11178. \note
  11179. - An empty image has no pixel data and all of its dimensions width(), height(), depth(), spectrum()
  11180. are set to \c 0, as well as its pixel buffer pointer data().
  11181. - An empty image may be re-assigned afterwards, e.g. with the family of
  11182. assign(unsigned int,unsigned int,unsigned int,unsigned int) methods,
  11183. or by operator=(const CImg<t>&). In all cases, the type of pixels stays \c T.
  11184. - An empty image is never shared.
  11185. \par Example
  11186. \code
  11187. CImg<float> img1, img2; // Construct two empty images
  11188. img1.assign(256,256,1,3); // Re-assign 'img1' to be a 256x256x1x3 (color) image
  11189. img2 = img1.get_rand(0,255); // Re-assign 'img2' to be a random-valued version of 'img1'
  11190. img2.assign(); // Re-assign 'img2' to be an empty image again
  11191. \endcode
  11192. **/
  11193. CImg():_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {}
  11194. //! Construct image with specified size.
  11195. /**
  11196. \param size_x Image width().
  11197. \param size_y Image height().
  11198. \param size_z Image depth().
  11199. \param size_c Image spectrum() (number of channels).
  11200. \note
  11201. - It is able to create only \e non-shared images, and allocates thus a pixel buffer data()
  11202. for each constructed image instance.
  11203. - Setting one dimension \c size_x,\c size_y,\c size_z or \c size_c to \c 0 leads to the construction of
  11204. an \e empty image.
  11205. - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
  11206. (e.g. when requested size is too big for available memory).
  11207. \warning
  11208. - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values.
  11209. In order to initialize pixel values during construction (e.g. with \c 0), use constructor
  11210. CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) instead.
  11211. \par Example
  11212. \code
  11213. CImg<float> img1(256,256,1,3); // Construct a 256x256x1x3 (color) image, filled with garbage values
  11214. CImg<float> img2(256,256,1,3,0); // Construct a 256x256x1x3 (color) image, filled with value '0'
  11215. \endcode
  11216. **/
  11217. explicit CImg(const unsigned int size_x, const unsigned int size_y=1,
  11218. const unsigned int size_z=1, const unsigned int size_c=1):
  11219. _is_shared(false) {
  11220. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11221. if (siz) {
  11222. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11223. try { _data = new T[siz]; } catch (...) {
  11224. _width = _height = _depth = _spectrum = 0; _data = 0;
  11225. throw CImgInstanceException(_cimg_instance
  11226. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11227. cimg_instance,
  11228. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11229. size_x,size_y,size_z,size_c);
  11230. }
  11231. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11232. }
  11233. //! Construct image with specified size and initialize pixel values.
  11234. /**
  11235. \param size_x Image width().
  11236. \param size_y Image height().
  11237. \param size_z Image depth().
  11238. \param size_c Image spectrum() (number of channels).
  11239. \param value Initialization value.
  11240. \note
  11241. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int),
  11242. but it also fills the pixel buffer with the specified \c value.
  11243. \warning
  11244. - It cannot be used to construct a vector-valued image and initialize it with \e vector-valued pixels
  11245. (e.g. RGB vector, for color images).
  11246. For this task, you may use fillC() after construction.
  11247. **/
  11248. CImg(const unsigned int size_x, const unsigned int size_y,
  11249. const unsigned int size_z, const unsigned int size_c, const T& value):
  11250. _is_shared(false) {
  11251. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11252. if (siz) {
  11253. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11254. try { _data = new T[siz]; } catch (...) {
  11255. _width = _height = _depth = _spectrum = 0; _data = 0;
  11256. throw CImgInstanceException(_cimg_instance
  11257. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11258. cimg_instance,
  11259. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11260. size_x,size_y,size_z,size_c);
  11261. }
  11262. fill(value);
  11263. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11264. }
  11265. //! Construct image with specified size and initialize pixel values from a sequence of integers.
  11266. /**
  11267. Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c,
  11268. with pixels of type \c T, and initialize pixel
  11269. values from the specified sequence of integers \c value0,\c value1,\c ...
  11270. \param size_x Image width().
  11271. \param size_y Image height().
  11272. \param size_z Image depth().
  11273. \param size_c Image spectrum() (number of channels).
  11274. \param value0 First value of the initialization sequence (must be an \e integer).
  11275. \param value1 Second value of the initialization sequence (must be an \e integer).
  11276. \param ...
  11277. \note
  11278. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
  11279. the pixel buffer with a sequence of specified integer values.
  11280. \warning
  11281. - You must specify \e exactly \c size_x*\c size_y*\c size_z*\c size_c integers in the initialization sequence.
  11282. Otherwise, the constructor may crash or fill your image pixels with garbage.
  11283. \par Example
  11284. \code
  11285. const CImg<float> img(2,2,1,3, // Construct a 2x2 color (RGB) image
  11286. 0,255,0,255, // Set the 4 values for the red component
  11287. 0,0,255,255, // Set the 4 values for the green component
  11288. 64,64,64,64); // Set the 4 values for the blue component
  11289. img.resize(150,150).display();
  11290. \endcode
  11291. \image html ref_constructor1.jpg
  11292. **/
  11293. CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
  11294. const int value0, const int value1, ...):
  11295. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11296. #define _CImg_stdarg(img,a0,a1,N,t) { \
  11297. size_t _siz = (size_t)N; \
  11298. if (_siz--) { \
  11299. va_list ap; \
  11300. va_start(ap,a1); \
  11301. T *ptrd = (img)._data; \
  11302. *(ptrd++) = (T)a0; \
  11303. if (_siz--) { \
  11304. *(ptrd++) = (T)a1; \
  11305. for ( ; _siz; --_siz) *(ptrd++) = (T)va_arg(ap,t); \
  11306. } \
  11307. va_end(ap); \
  11308. } \
  11309. }
  11310. assign(size_x,size_y,size_z,size_c);
  11311. _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),int);
  11312. }
  11313. #if cimg_use_cpp11==1
  11314. //! Construct image with specified size and initialize pixel values from an initializer list of integers.
  11315. /**
  11316. Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c,
  11317. with pixels of type \c T, and initialize pixel
  11318. values from the specified initializer list of integers { \c value0,\c value1,\c ... }
  11319. \param size_x Image width().
  11320. \param size_y Image height().
  11321. \param size_z Image depth().
  11322. \param size_c Image spectrum() (number of channels).
  11323. \param { value0, value1, ... } Initialization list
  11324. \param repeat_values Tells if the value filling process is repeated over the image.
  11325. \note
  11326. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
  11327. the pixel buffer with a sequence of specified integer values.
  11328. \par Example
  11329. \code
  11330. const CImg<float> img(2,2,1,3, // Construct a 2x2 color (RGB) image
  11331. { 0,255,0,255, // Set the 4 values for the red component
  11332. 0,0,255,255, // Set the 4 values for the green component
  11333. 64,64,64,64 }); // Set the 4 values for the blue component
  11334. img.resize(150,150).display();
  11335. \endcode
  11336. \image html ref_constructor1.jpg
  11337. **/
  11338. template<typename t>
  11339. CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
  11340. const std::initializer_list<t> values,
  11341. const bool repeat_values=true):
  11342. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11343. #define _cimg_constructor_cpp11(repeat_values) \
  11344. auto it = values.begin(); \
  11345. size_t siz = size(); \
  11346. if (repeat_values) for (T *ptrd = _data; siz--; ) { \
  11347. *(ptrd++) = (T)(*(it++)); if (it==values.end()) it = values.begin(); } \
  11348. else { siz = std::min(siz,values.size()); for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); }
  11349. assign(size_x,size_y,size_z,size_c);
  11350. _cimg_constructor_cpp11(repeat_values);
  11351. }
  11352. template<typename t>
  11353. CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z,
  11354. std::initializer_list<t> values,
  11355. const bool repeat_values=true):
  11356. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11357. assign(size_x,size_y,size_z);
  11358. _cimg_constructor_cpp11(repeat_values);
  11359. }
  11360. template<typename t>
  11361. CImg(const unsigned int size_x, const unsigned int size_y,
  11362. std::initializer_list<t> values,
  11363. const bool repeat_values=true):
  11364. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11365. assign(size_x,size_y);
  11366. _cimg_constructor_cpp11(repeat_values);
  11367. }
  11368. template<typename t>
  11369. CImg(const unsigned int size_x,
  11370. std::initializer_list<t> values,
  11371. const bool repeat_values=true):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11372. assign(size_x);
  11373. _cimg_constructor_cpp11(repeat_values);
  11374. }
  11375. //! Construct single channel 1D image with pixel values and width obtained from an initializer list of integers.
  11376. /**
  11377. Construct a new image instance of size \c width x \c 1 x \c 1 x \c 1,
  11378. with pixels of type \c T, and initialize pixel
  11379. values from the specified initializer list of integers { \c value0,\c value1,\c ... }. Image width is
  11380. given by the size of the initializer list.
  11381. \param { value0, value1, ... } Initialization list
  11382. \note
  11383. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int) with height=1, depth=1, and spectrum=1,
  11384. but it also fills the pixel buffer with a sequence of specified integer values.
  11385. \par Example
  11386. \code
  11387. const CImg<float> img = {10,20,30,20,10 }; // Construct a 5x1 image with one channel, and set its pixel values
  11388. img.resize(150,150).display();
  11389. \endcode
  11390. \image html ref_constructor1.jpg
  11391. **/
  11392. template<typename t>
  11393. CImg(const std::initializer_list<t> values):
  11394. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11395. assign(values.size(),1,1,1);
  11396. auto it = values.begin();
  11397. unsigned int siz = _width;
  11398. for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++));
  11399. }
  11400. template<typename t>
  11401. CImg<T>& operator=(std::initializer_list<t> values) {
  11402. _cimg_constructor_cpp11(siz>values.size());
  11403. return *this;
  11404. }
  11405. #endif
  11406. //! Construct image with specified size and initialize pixel values from a sequence of doubles.
  11407. /**
  11408. Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T,
  11409. and initialize pixel values from the specified sequence of doubles \c value0,\c value1,\c ...
  11410. \param size_x Image width().
  11411. \param size_y Image height().
  11412. \param size_z Image depth().
  11413. \param size_c Image spectrum() (number of channels).
  11414. \param value0 First value of the initialization sequence (must be a \e double).
  11415. \param value1 Second value of the initialization sequence (must be a \e double).
  11416. \param ...
  11417. \note
  11418. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...), but
  11419. takes a sequence of double values instead of integers.
  11420. \warning
  11421. - You must specify \e exactly \c dx*\c dy*\c dz*\c dc doubles in the initialization sequence.
  11422. Otherwise, the constructor may crash or fill your image with garbage.
  11423. For instance, the code below will probably crash on most platforms:
  11424. \code
  11425. const CImg<float> img(2,2,1,1, 0.5,0.5,255,255); // FAIL: The two last arguments are 'int', not 'double'!
  11426. \endcode
  11427. **/
  11428. CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
  11429. const double value0, const double value1, ...):
  11430. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11431. assign(size_x,size_y,size_z,size_c);
  11432. _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),double);
  11433. }
  11434. //! Construct image with specified size and initialize pixel values from a value string.
  11435. /**
  11436. Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T,
  11437. and initializes pixel values from the specified string \c values.
  11438. \param size_x Image width().
  11439. \param size_y Image height().
  11440. \param size_z Image depth().
  11441. \param size_c Image spectrum() (number of channels).
  11442. \param values Value string describing the way pixel values are set.
  11443. \param repeat_values Tells if the value filling process is repeated over the image.
  11444. \note
  11445. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
  11446. the pixel buffer with values described in the value string \c values.
  11447. - Value string \c values may describe two different filling processes:
  11448. - Either \c values is a sequences of values assigned to the image pixels, as in <tt>"1,2,3,7,8,2"</tt>.
  11449. In this case, set \c repeat_values to \c true to periodically fill the image with the value sequence.
  11450. - Either, \c values is a formula, as in <tt>"cos(x/10)*sin(y/20)"</tt>.
  11451. In this case, parameter \c repeat_values is pointless.
  11452. - For both cases, specifying \c repeat_values is mandatory.
  11453. It disambiguates the possible overloading of constructor
  11454. CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) with \c T being a <tt>const char*</tt>.
  11455. - A \c CImgArgumentException is thrown when an invalid value string \c values is specified.
  11456. \par Example
  11457. \code
  11458. const CImg<float> img1(129,129,1,3,"0,64,128,192,255",true), // Construct image from a value sequence
  11459. img2(129,129,1,3,"if(c==0,255*abs(cos(x/10)),1.8*y)",false); // Construct image from a formula
  11460. (img1,img2).display();
  11461. \endcode
  11462. \image html ref_constructor2.jpg
  11463. **/
  11464. CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
  11465. const char *const values, const bool repeat_values):_is_shared(false) {
  11466. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11467. if (siz) {
  11468. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11469. try { _data = new T[siz]; } catch (...) {
  11470. _width = _height = _depth = _spectrum = 0; _data = 0;
  11471. throw CImgInstanceException(_cimg_instance
  11472. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11473. cimg_instance,
  11474. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11475. size_x,size_y,size_z,size_c);
  11476. }
  11477. fill(values,repeat_values);
  11478. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11479. }
  11480. //! Construct image with specified size and initialize pixel values from a memory buffer.
  11481. /**
  11482. Construct a new image instance of size \c size_x x \c size_y x \c size_z x \c size_c, with pixels of type \c T,
  11483. and initializes pixel values from the specified \c t* memory buffer.
  11484. \param values Pointer to the input memory buffer.
  11485. \param size_x Image width().
  11486. \param size_y Image height().
  11487. \param size_z Image depth().
  11488. \param size_c Image spectrum() (number of channels).
  11489. \param is_shared Tells if input memory buffer must be shared by the current instance.
  11490. \note
  11491. - If \c is_shared is \c false, the image instance allocates its own pixel buffer,
  11492. and values from the specified input buffer are copied to the instance buffer.
  11493. If buffer types \c T and \c t are different, a regular static cast is performed during buffer copy.
  11494. - Otherwise, the image instance does \e not allocate a new buffer, and uses the input memory buffer as its
  11495. own pixel buffer. This case requires that types \c T and \c t are the same. Later, destroying such a shared
  11496. image will not deallocate the pixel buffer, this task being obviously charged to the initial buffer allocator.
  11497. - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
  11498. (e.g. when requested size is too big for available memory).
  11499. \warning
  11500. - You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data()
  11501. (e.g. already deallocated).
  11502. \par Example
  11503. \code
  11504. unsigned char tab[256*256] = { 0 };
  11505. CImg<unsigned char> img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'
  11506. img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'
  11507. tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'
  11508. \endcode
  11509. **/
  11510. template<typename t>
  11511. CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1,
  11512. const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) {
  11513. if (is_shared) {
  11514. _width = _height = _depth = _spectrum = 0; _data = 0;
  11515. throw CImgArgumentException(_cimg_instance
  11516. "CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance "
  11517. "from a (%s*) buffer (pixel types are different).",
  11518. cimg_instance,
  11519. size_x,size_y,size_z,size_c,CImg<t>::pixel_type());
  11520. }
  11521. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11522. if (values && siz) {
  11523. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11524. try { _data = new T[siz]; } catch (...) {
  11525. _width = _height = _depth = _spectrum = 0; _data = 0;
  11526. throw CImgInstanceException(_cimg_instance
  11527. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11528. cimg_instance,
  11529. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11530. size_x,size_y,size_z,size_c);
  11531. }
  11532. const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
  11533. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11534. }
  11535. //! Construct image with specified size and initialize pixel values from a memory buffer \specialization.
  11536. CImg(const T *const values, const unsigned int size_x, const unsigned int size_y=1,
  11537. const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false) {
  11538. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11539. if (values && siz) {
  11540. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = is_shared;
  11541. if (_is_shared) _data = const_cast<T*>(values);
  11542. else {
  11543. try { _data = new T[siz]; } catch (...) {
  11544. _width = _height = _depth = _spectrum = 0; _data = 0;
  11545. throw CImgInstanceException(_cimg_instance
  11546. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11547. cimg_instance,
  11548. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11549. size_x,size_y,size_z,size_c);
  11550. }
  11551. std::memcpy(_data,values,siz*sizeof(T));
  11552. }
  11553. } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
  11554. }
  11555. //! Construct image from memory buffer with specified size and pixel ordering scheme.
  11556. template<typename t>
  11557. CImg(const t *const values, const unsigned int size_x, const unsigned int size_y,
  11558. const unsigned int size_z, const unsigned int size_c,
  11559. const char *const axes_order):_data(0),_is_shared(false) {
  11560. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11561. if (values && siz) {
  11562. unsigned char s_code[4] = { 0,1,2,3 }, n_code[4] = { 0 };
  11563. for (unsigned int l = 0; axes_order[l]; ++l) {
  11564. int c = cimg::lowercase(axes_order[l]);
  11565. if (l>=4 || (c!='x' && c!='y' && c!='z' && c!='c')) { *s_code = 4; break; }
  11566. else { ++n_code[c%=4]; s_code[l] = c; }
  11567. }
  11568. if (*axes_order && *s_code<4 && *n_code<=1 && n_code[1]<=1 && n_code[2]<=1 && n_code[3]<=1) {
  11569. const unsigned int code = (s_code[0]<<12) | (s_code[1]<<8) | (s_code[2]<<4) | (s_code[3]);
  11570. int s0 = 0, s1 = 0, s2 = 0, s3 = 0;
  11571. const char *inv_order = 0;
  11572. switch (code) {
  11573. case 0x0123 : inv_order = "xyzc"; s0 = size_x; s1 = size_y; s2 = size_z; s3 = size_c; break; // xyzc
  11574. case 0x0132 : inv_order = "xyzc"; s0 = size_x; s1 = size_y; s2 = size_c; s3 = size_z; break; // xycz
  11575. case 0x0213 : inv_order = "xzyc"; s0 = size_x; s1 = size_z; s2 = size_y; s3 = size_c; break; // xzyc
  11576. case 0x0231 : inv_order = "xcyz"; s0 = size_x; s1 = size_z; s2 = size_c; s3 = size_y; break; // xzcy
  11577. case 0x0312 : inv_order = "xzcy"; s0 = size_x; s1 = size_c; s2 = size_y; s3 = size_z; break; // xcyz
  11578. case 0x0321 : inv_order = "xczy"; s0 = size_x; s1 = size_c; s2 = size_z; s3 = size_y; break; // xczy
  11579. case 0x1023 : inv_order = "yxzc"; s0 = size_y; s1 = size_x; s2 = size_z; s3 = size_c; break; // yxzc
  11580. case 0x1032 : inv_order = "yxcz"; s0 = size_y; s1 = size_x; s2 = size_c; s3 = size_z; break; // yxcz
  11581. case 0x1203 : inv_order = "zxyc"; s0 = size_y; s1 = size_z; s2 = size_x; s3 = size_c; break; // yzxc
  11582. case 0x1230 : inv_order = "cxyz"; s0 = size_y; s1 = size_z; s2 = size_c; s3 = size_x; break; // yzcx
  11583. case 0x1302 : inv_order = "zxcy"; s0 = size_y; s1 = size_c; s2 = size_x; s3 = size_z; break; // ycxz
  11584. case 0x1320 : inv_order = "cxzy"; s0 = size_y; s1 = size_c; s2 = size_z; s3 = size_x; break; // yczx
  11585. case 0x2013 : inv_order = "yzxc"; s0 = size_z; s1 = size_x; s2 = size_y; s3 = size_c; break; // zxyc
  11586. case 0x2031 : inv_order = "ycxz"; s0 = size_z; s1 = size_x; s2 = size_c; s3 = size_y; break; // zxcy
  11587. case 0x2103 : inv_order = "zyxc"; s0 = size_z; s1 = size_y; s2 = size_x; s3 = size_c; break; // zyxc
  11588. case 0x2130 : inv_order = "cyxz"; s0 = size_z; s1 = size_y; s2 = size_c; s3 = size_x; break; // zycx
  11589. case 0x2301 : inv_order = "zcxy"; s0 = size_z; s1 = size_c; s2 = size_x; s3 = size_y; break; // zcxy
  11590. case 0x2310 : inv_order = "czxy"; s0 = size_z; s1 = size_c; s2 = size_y; s3 = size_x; break; // zcyx
  11591. case 0x3012 : inv_order = "yzcx"; s0 = size_c; s1 = size_x; s2 = size_y; s3 = size_z; break; // cxyz
  11592. case 0x3021 : inv_order = "yczx"; s0 = size_c; s1 = size_x; s2 = size_z; s3 = size_y; break; // cxzy
  11593. case 0x3102 : inv_order = "zycx"; s0 = size_c; s1 = size_y; s2 = size_x; s3 = size_z; break; // cyxz
  11594. case 0x3120 : inv_order = "cyzx"; s0 = size_c; s1 = size_y; s2 = size_z; s3 = size_x; break; // cyzx
  11595. case 0x3201 : inv_order = "zcyx"; s0 = size_c; s1 = size_z; s2 = size_x; s3 = size_y; break; // czxy
  11596. case 0x3210 : inv_order = "czyx"; s0 = size_c; s1 = size_z; s2 = size_y; s3 = size_x; break; // czyx
  11597. }
  11598. CImg<t>(values,s0,s1,s2,s3,true).get_permute_axes(inv_order).move_to(*this);
  11599. } else {
  11600. _width = _height = _depth = _spectrum = 0; _data = 0;
  11601. throw CImgArgumentException(_cimg_instance
  11602. "CImg(): Invalid specified axes order '%s'.",
  11603. cimg_instance,
  11604. axes_order);
  11605. }
  11606. } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
  11607. }
  11608. //! Construct image from reading an image file.
  11609. /**
  11610. Construct a new image instance with pixels of type \c T, and initialize pixel values with the data read from
  11611. an image file.
  11612. \param filename Filename, as a C-string.
  11613. \note
  11614. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it reads the image
  11615. dimensions and pixel values from the specified image file.
  11616. - The recognition of the image file format by %CImg higlhy depends on the tools installed on your system
  11617. and on the external libraries you used to link your code against.
  11618. - Considered pixel type \c T should better fit the file format specification, or data loss may occur during
  11619. file load (e.g. constructing a \c CImg<unsigned char> from a float-valued image file).
  11620. - A \c CImgIOException is thrown when the specified \c filename cannot be read, or if the file format is not
  11621. recognized.
  11622. \par Example
  11623. \code
  11624. const CImg<float> img("reference.jpg");
  11625. img.display();
  11626. \endcode
  11627. \image html ref_image.jpg
  11628. **/
  11629. explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11630. assign(filename);
  11631. }
  11632. //! Construct image copy.
  11633. /**
  11634. Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg<t> instance.
  11635. \param img Input image to copy.
  11636. \note
  11637. - Constructed copy has the same size width() x height() x depth() x spectrum() and pixel values as the
  11638. input image \c img.
  11639. - If input image \c img is \e shared and if types \c T and \c t are the same, the constructed copy is also
  11640. \e shared, and shares its pixel buffer with \c img.
  11641. Modifying a pixel value in the constructed copy will thus also modifies it in the input image \c img.
  11642. This behavior is needful to allow functions to return shared images.
  11643. - Otherwise, the constructed copy allocates its own pixel buffer, and copies pixel values from the input
  11644. image \c img into its buffer. The copied pixel values may be eventually statically casted if types \c T and
  11645. \c t are different.
  11646. - Constructing a copy from an image \c img when types \c t and \c T are the same is significantly faster than
  11647. with different types.
  11648. - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
  11649. (e.g. not enough available memory).
  11650. **/
  11651. template<typename t>
  11652. CImg(const CImg<t>& img):_is_shared(false) {
  11653. const size_t siz = (size_t)img.size();
  11654. if (img._data && siz) {
  11655. _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
  11656. try { _data = new T[siz]; } catch (...) {
  11657. _width = _height = _depth = _spectrum = 0; _data = 0;
  11658. throw CImgInstanceException(_cimg_instance
  11659. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11660. cimg_instance,
  11661. cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
  11662. img._width,img._height,img._depth,img._spectrum);
  11663. }
  11664. const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
  11665. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11666. }
  11667. //! Construct image copy \specialization.
  11668. CImg(const CImg<T>& img) {
  11669. const size_t siz = (size_t)img.size();
  11670. if (img._data && siz) {
  11671. _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
  11672. _is_shared = img._is_shared;
  11673. if (_is_shared) _data = const_cast<T*>(img._data);
  11674. else {
  11675. try { _data = new T[siz]; } catch (...) {
  11676. _width = _height = _depth = _spectrum = 0; _data = 0;
  11677. throw CImgInstanceException(_cimg_instance
  11678. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11679. cimg_instance,
  11680. cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
  11681. img._width,img._height,img._depth,img._spectrum);
  11682. }
  11683. std::memcpy(_data,img._data,siz*sizeof(T));
  11684. }
  11685. } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
  11686. }
  11687. //! Advanced copy constructor.
  11688. /**
  11689. Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg<t> instance,
  11690. while forcing the shared state of the constructed copy.
  11691. \param img Input image to copy.
  11692. \param is_shared Tells about the shared state of the constructed copy.
  11693. \note
  11694. - Similar to CImg(const CImg<t>&), except that it allows to decide the shared state of
  11695. the constructed image, which does not depend anymore on the shared state of the input image \c img:
  11696. - If \c is_shared is \c true, the constructed copy will share its pixel buffer with the input image \c img.
  11697. For that case, the pixel types \c T and \c t \e must be the same.
  11698. - If \c is_shared is \c false, the constructed copy will allocate its own pixel buffer, whether the input
  11699. image \c img is shared or not.
  11700. - A \c CImgArgumentException is thrown when a shared copy is requested with different pixel types \c T and \c t.
  11701. **/
  11702. template<typename t>
  11703. CImg(const CImg<t>& img, const bool is_shared):_is_shared(false) {
  11704. if (is_shared) {
  11705. _width = _height = _depth = _spectrum = 0; _data = 0;
  11706. throw CImgArgumentException(_cimg_instance
  11707. "CImg(): Invalid construction request of a shared instance from a "
  11708. "CImg<%s> image (%u,%u,%u,%u,%p) (pixel types are different).",
  11709. cimg_instance,
  11710. CImg<t>::pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data);
  11711. }
  11712. const size_t siz = (size_t)img.size();
  11713. if (img._data && siz) {
  11714. _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
  11715. try { _data = new T[siz]; } catch (...) {
  11716. _width = _height = _depth = _spectrum = 0; _data = 0;
  11717. throw CImgInstanceException(_cimg_instance
  11718. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11719. cimg_instance,
  11720. cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
  11721. img._width,img._height,img._depth,img._spectrum);
  11722. }
  11723. const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
  11724. } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
  11725. }
  11726. //! Advanced copy constructor \specialization.
  11727. CImg(const CImg<T>& img, const bool is_shared) {
  11728. const size_t siz = (size_t)img.size();
  11729. if (img._data && siz) {
  11730. _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
  11731. _is_shared = is_shared;
  11732. if (_is_shared) _data = const_cast<T*>(img._data);
  11733. else {
  11734. try { _data = new T[siz]; } catch (...) {
  11735. _width = _height = _depth = _spectrum = 0; _data = 0;
  11736. throw CImgInstanceException(_cimg_instance
  11737. "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11738. cimg_instance,
  11739. cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
  11740. img._width,img._height,img._depth,img._spectrum);
  11741. }
  11742. std::memcpy(_data,img._data,siz*sizeof(T));
  11743. }
  11744. } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
  11745. }
  11746. //! Construct image with dimensions borrowed from another image.
  11747. /**
  11748. Construct a new image instance with pixels of type \c T, and size get from some dimensions of an existing
  11749. \c CImg<t> instance.
  11750. \param img Input image from which dimensions are borrowed.
  11751. \param dimensions C-string describing the image size along the X,Y,Z and C-dimensions.
  11752. \note
  11753. - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it takes the image dimensions
  11754. (\e not its pixel values) from an existing \c CImg<t> instance.
  11755. - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values.
  11756. In order to initialize pixel values (e.g. with \c 0), use constructor CImg(const CImg<t>&,const char*,T)
  11757. instead.
  11758. \par Example
  11759. \code
  11760. const CImg<float> img1(256,128,1,3), // 'img1' is a 256x128x1x3 image
  11761. img2(img1,"xyzc"), // 'img2' is a 256x128x1x3 image
  11762. img3(img1,"y,x,z,c"), // 'img3' is a 128x256x1x3 image
  11763. img4(img1,"c,x,y,3",0), // 'img4' is a 3x128x256x3 image (with pixels initialized to '0')
  11764. \endcode
  11765. **/
  11766. template<typename t>
  11767. CImg(const CImg<t>& img, const char *const dimensions):
  11768. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11769. assign(img,dimensions);
  11770. }
  11771. //! Construct image with dimensions borrowed from another image and initialize pixel values.
  11772. /**
  11773. Construct a new image instance with pixels of type \c T, and size get from the dimensions of an existing
  11774. \c CImg<t> instance, and set all pixel values to specified \c value.
  11775. \param img Input image from which dimensions are borrowed.
  11776. \param dimensions String describing the image size along the X,Y,Z and V-dimensions.
  11777. \param value Value used for initialization.
  11778. \note
  11779. - Similar to CImg(const CImg<t>&,const char*), but it also fills the pixel buffer with the specified \c value.
  11780. **/
  11781. template<typename t>
  11782. CImg(const CImg<t>& img, const char *const dimensions, const T& value):
  11783. _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11784. assign(img,dimensions).fill(value);
  11785. }
  11786. //! Construct image from a display window.
  11787. /**
  11788. Construct a new image instance with pixels of type \c T, as a snapshot of an existing \c CImgDisplay instance.
  11789. \param disp Input display window.
  11790. \note
  11791. - The width() and height() of the constructed image instance are the same as the specified \c CImgDisplay.
  11792. - The depth() and spectrum() of the constructed image instance are respectively set to \c 1 and \c 3
  11793. (i.e. a 2D color image).
  11794. - The image pixels are read as 8-bits RGB values.
  11795. **/
  11796. explicit CImg(const CImgDisplay &disp):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11797. disp.snapshot(*this);
  11798. }
  11799. // Constructor and assignment operator for rvalue references (c++11).
  11800. // This avoids an additional image copy for methods returning new images. Can save RAM for big images !
  11801. #if cimg_use_cpp11==1
  11802. CImg(CImg<T>&& img):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  11803. swap(img);
  11804. }
  11805. CImg<T>& operator=(CImg<T>&& img) {
  11806. if (_is_shared) return assign(img);
  11807. return img.swap(*this);
  11808. }
  11809. #endif
  11810. //! Construct empty image \inplace.
  11811. /**
  11812. In-place version of the default constructor CImg(). It simply resets the instance to an empty image.
  11813. **/
  11814. CImg<T>& assign() {
  11815. if (!_is_shared) delete[] _data;
  11816. _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0;
  11817. return *this;
  11818. }
  11819. //! Construct image with specified size \inplace.
  11820. /**
  11821. In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int).
  11822. **/
  11823. CImg<T>& assign(const unsigned int size_x, const unsigned int size_y=1,
  11824. const unsigned int size_z=1, const unsigned int size_c=1) {
  11825. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11826. if (!siz) return assign();
  11827. const size_t curr_siz = (size_t)size();
  11828. if (siz!=curr_siz) {
  11829. if (_is_shared)
  11830. throw CImgArgumentException(_cimg_instance
  11831. "assign(): Invalid assignment request of shared instance from specified "
  11832. "image (%u,%u,%u,%u).",
  11833. cimg_instance,
  11834. size_x,size_y,size_z,size_c);
  11835. else {
  11836. delete[] _data;
  11837. try { _data = new T[siz]; } catch (...) {
  11838. _width = _height = _depth = _spectrum = 0; _data = 0;
  11839. throw CImgInstanceException(_cimg_instance
  11840. "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11841. cimg_instance,
  11842. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11843. size_x,size_y,size_z,size_c);
  11844. }
  11845. }
  11846. }
  11847. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11848. return *this;
  11849. }
  11850. //! Construct image with specified size and initialize pixel values \inplace.
  11851. /**
  11852. In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,T).
  11853. **/
  11854. CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
  11855. const unsigned int size_z, const unsigned int size_c, const T& value) {
  11856. return assign(size_x,size_y,size_z,size_c).fill(value);
  11857. }
  11858. //! Construct image with specified size and initialize pixel values from a sequence of integers \inplace.
  11859. /**
  11860. In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...).
  11861. **/
  11862. CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
  11863. const unsigned int size_z, const unsigned int size_c,
  11864. const int value0, const int value1, ...) {
  11865. assign(size_x,size_y,size_z,size_c);
  11866. _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),int);
  11867. return *this;
  11868. }
  11869. //! Construct image with specified size and initialize pixel values from a sequence of doubles \inplace.
  11870. /**
  11871. In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,double,double,...).
  11872. **/
  11873. CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
  11874. const unsigned int size_z, const unsigned int size_c,
  11875. const double value0, const double value1, ...) {
  11876. assign(size_x,size_y,size_z,size_c);
  11877. _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),double);
  11878. return *this;
  11879. }
  11880. //! Construct image with specified size and initialize pixel values from a value string \inplace.
  11881. /**
  11882. In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,const char*,bool).
  11883. **/
  11884. CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
  11885. const unsigned int size_z, const unsigned int size_c,
  11886. const char *const values, const bool repeat_values) {
  11887. return assign(size_x,size_y,size_z,size_c).fill(values,repeat_values);
  11888. }
  11889. //! Construct image with specified size and initialize pixel values from a memory buffer \inplace.
  11890. /**
  11891. In-place version of the constructor CImg(const t*,unsigned int,unsigned int,unsigned int,unsigned int).
  11892. **/
  11893. template<typename t>
  11894. CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y=1,
  11895. const unsigned int size_z=1, const unsigned int size_c=1) {
  11896. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11897. if (!values || !siz) return assign();
  11898. assign(size_x,size_y,size_z,size_c);
  11899. const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
  11900. return *this;
  11901. }
  11902. //! Construct image with specified size and initialize pixel values from a memory buffer \specialization.
  11903. CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y=1,
  11904. const unsigned int size_z=1, const unsigned int size_c=1) {
  11905. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11906. if (!values || !siz) return assign();
  11907. const size_t curr_siz = (size_t)size();
  11908. if (values==_data && siz==curr_siz) return assign(size_x,size_y,size_z,size_c);
  11909. if (_is_shared || values + siz<_data || values>=_data + size()) {
  11910. assign(size_x,size_y,size_z,size_c);
  11911. if (_is_shared) std::memmove((void*)_data,(void*)values,siz*sizeof(T));
  11912. else std::memcpy((void*)_data,(void*)values,siz*sizeof(T));
  11913. } else {
  11914. T *new_data = 0;
  11915. try { new_data = new T[siz]; } catch (...) {
  11916. _width = _height = _depth = _spectrum = 0; _data = 0;
  11917. throw CImgInstanceException(_cimg_instance
  11918. "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
  11919. cimg_instance,
  11920. cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
  11921. size_x,size_y,size_z,size_c);
  11922. }
  11923. std::memcpy((void*)new_data,(void*)values,siz*sizeof(T));
  11924. delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
  11925. }
  11926. return *this;
  11927. }
  11928. //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
  11929. template<typename t>
  11930. CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,
  11931. const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
  11932. if (is_shared)
  11933. throw CImgArgumentException(_cimg_instance
  11934. "assign(): Invalid assignment request of shared instance from (%s*) buffer"
  11935. "(pixel types are different).",
  11936. cimg_instance,
  11937. CImg<t>::pixel_type());
  11938. return assign(values,size_x,size_y,size_z,size_c);
  11939. }
  11940. //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
  11941. CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y,
  11942. const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
  11943. const size_t siz = safe_size(size_x,size_y,size_z,size_c);
  11944. if (!values || !siz) return assign();
  11945. if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); }
  11946. else {
  11947. if (!_is_shared) {
  11948. if (values + siz<_data || values>=_data + size()) assign();
  11949. else cimg::warn(_cimg_instance
  11950. "assign(): Shared image instance has overlapping memory.",
  11951. cimg_instance);
  11952. }
  11953. _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = true;
  11954. _data = const_cast<T*>(values);
  11955. }
  11956. return *this;
  11957. }
  11958. //! Construct image from memory buffer with specified size and pixel ordering scheme.
  11959. template<typename t>
  11960. CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,
  11961. const unsigned int size_z, const unsigned int size_c,
  11962. const char *const axes_order) {
  11963. CImg<T>(values,size_x,size_y,size_z,size_c,axes_order).move_to(*this);
  11964. }
  11965. //! Construct image from reading an image file \inplace.
  11966. /**
  11967. In-place version of the constructor CImg(const char*).
  11968. **/
  11969. CImg<T>& assign(const char *const filename) {
  11970. return load(filename);
  11971. }
  11972. //! Construct image copy \inplace.
  11973. /**
  11974. In-place version of the constructor CImg(const CImg<t>&).
  11975. **/
  11976. template<typename t>
  11977. CImg<T>& assign(const CImg<t>& img) {
  11978. return assign(img._data,img._width,img._height,img._depth,img._spectrum);
  11979. }
  11980. //! In-place version of the advanced copy constructor.
  11981. /**
  11982. In-place version of the constructor CImg(const CImg<t>&,bool).
  11983. **/
  11984. template<typename t>
  11985. CImg<T>& assign(const CImg<t>& img, const bool is_shared) {
  11986. return assign(img._data,img._width,img._height,img._depth,img._spectrum,is_shared);
  11987. }
  11988. //! Construct image with dimensions borrowed from another image \inplace.
  11989. /**
  11990. In-place version of the constructor CImg(const CImg<t>&,const char*).
  11991. **/
  11992. template<typename t>
  11993. CImg<T>& assign(const CImg<t>& img, const char *const dimensions) {
  11994. if (!dimensions || !*dimensions) return assign(img._width,img._height,img._depth,img._spectrum);
  11995. unsigned int siz[4] = { 0,1,1,1 }, k = 0;
  11996. CImg<charT> item(256);
  11997. for (const char *s = dimensions; *s && k<4; ++k) {
  11998. if (cimg_sscanf(s,"%255[^0-9%xyzvwhdcXYZVWHDC]",item._data)>0) s+=std::strlen(item);
  11999. if (*s) {
  12000. unsigned int val = 0; char sep = 0;
  12001. if (cimg_sscanf(s,"%u%c",&val,&sep)>0) {
  12002. if (sep=='%') siz[k] = val*(k==0?_width:k==1?_height:k==2?_depth:_spectrum)/100;
  12003. else siz[k] = val;
  12004. while (*s>='0' && *s<='9') ++s;
  12005. if (sep=='%') ++s;
  12006. } else switch (cimg::lowercase(*s)) {
  12007. case 'x' : case 'w' : siz[k] = img._width; ++s; break;
  12008. case 'y' : case 'h' : siz[k] = img._height; ++s; break;
  12009. case 'z' : case 'd' : siz[k] = img._depth; ++s; break;
  12010. case 'c' : case 's' : siz[k] = img._spectrum; ++s; break;
  12011. default :
  12012. throw CImgArgumentException(_cimg_instance
  12013. "assign(): Invalid character '%c' detected in specified dimension string '%s'.",
  12014. cimg_instance,
  12015. *s,dimensions);
  12016. }
  12017. }
  12018. }
  12019. return assign(siz[0],siz[1],siz[2],siz[3]);
  12020. }
  12021. //! Construct image with dimensions borrowed from another image and initialize pixel values \inplace.
  12022. /**
  12023. In-place version of the constructor CImg(const CImg<t>&,const char*,T).
  12024. **/
  12025. template<typename t>
  12026. CImg<T>& assign(const CImg<t>& img, const char *const dimensions, const T& value) {
  12027. return assign(img,dimensions).fill(value);
  12028. }
  12029. //! Construct image from a display window \inplace.
  12030. /**
  12031. In-place version of the constructor CImg(const CImgDisplay&).
  12032. **/
  12033. CImg<T>& assign(const CImgDisplay &disp) {
  12034. disp.snapshot(*this);
  12035. return *this;
  12036. }
  12037. //! Construct empty image \inplace.
  12038. /**
  12039. Equivalent to assign().
  12040. \note
  12041. - It has been defined for compatibility with STL naming conventions.
  12042. **/
  12043. CImg<T>& clear() {
  12044. return assign();
  12045. }
  12046. //! Transfer content of an image instance into another one.
  12047. /**
  12048. Transfer the dimensions and the pixel buffer content of an image instance into another one,
  12049. and replace instance by an empty image. It avoids the copy of the pixel buffer
  12050. when possible.
  12051. \param img Destination image.
  12052. \note
  12053. - Pixel types \c T and \c t of source and destination images can be different, though the process is
  12054. designed to be instantaneous when \c T and \c t are the same.
  12055. \par Example
  12056. \code
  12057. CImg<float> src(256,256,1,3,0), // Construct a 256x256x1x3 (color) image filled with value '0'
  12058. dest(16,16); // Construct a 16x16x1x1 (scalar) image
  12059. src.move_to(dest); // Now, 'src' is empty and 'dest' is the 256x256x1x3 image
  12060. \endcode
  12061. **/
  12062. template<typename t>
  12063. CImg<t>& move_to(CImg<t>& img) {
  12064. img.assign(*this);
  12065. assign();
  12066. return img;
  12067. }
  12068. //! Transfer content of an image instance into another one \specialization.
  12069. CImg<T>& move_to(CImg<T>& img) {
  12070. if (_is_shared || img._is_shared) img.assign(*this);
  12071. else swap(img);
  12072. assign();
  12073. return img;
  12074. }
  12075. //! Transfer content of an image instance into a new image in an image list.
  12076. /**
  12077. Transfer the dimensions and the pixel buffer content of an image instance
  12078. into a newly inserted image at position \c pos in specified \c CImgList<t> instance.
  12079. \param list Destination list.
  12080. \param pos Position of the newly inserted image in the list.
  12081. \note
  12082. - When optional parameter \c pos is omitted, the image instance is transferred as a new
  12083. image at the end of the specified \c list.
  12084. - It is convenient to sequentially insert new images into image lists, with no
  12085. additional copies of memory buffer.
  12086. \par Example
  12087. \code
  12088. CImgList<float> list; // Construct an empty image list
  12089. CImg<float> img("reference.jpg"); // Read image from filename
  12090. img.move_to(list); // Transfer image content as a new item in the list (no buffer copy)
  12091. \endcode
  12092. **/
  12093. template<typename t>
  12094. CImgList<t>& move_to(CImgList<t>& list, const unsigned int pos=~0U) {
  12095. const unsigned int npos = pos>list._width?list._width:pos;
  12096. move_to(list.insert(1,npos)[npos]);
  12097. return list;
  12098. }
  12099. //! Swap fields of two image instances.
  12100. /**
  12101. \param img Image to swap fields with.
  12102. \note
  12103. - It can be used to interchange the content of two images in a very fast way. Can be convenient when dealing
  12104. with algorithms requiring two swapping buffers.
  12105. \par Example
  12106. \code
  12107. CImg<float> img1("lena.jpg"),
  12108. img2("milla.jpg");
  12109. img1.swap(img2); // Now, 'img1' is 'milla' and 'img2' is 'lena'
  12110. \endcode
  12111. **/
  12112. CImg<T>& swap(CImg<T>& img) {
  12113. cimg::swap(_width,img._width,_height,img._height,_depth,img._depth,_spectrum,img._spectrum);
  12114. cimg::swap(_data,img._data);
  12115. cimg::swap(_is_shared,img._is_shared);
  12116. return img;
  12117. }
  12118. //! Return a reference to an empty image.
  12119. /**
  12120. \note
  12121. This function is useful mainly to declare optional parameters having type \c CImg<T> in functions prototypes,
  12122. e.g.
  12123. \code
  12124. void f(const int x=0, const int y=0, const CImg<float>& img=CImg<float>::empty());
  12125. \endcode
  12126. **/
  12127. static CImg<T>& empty() {
  12128. static CImg<T> _empty;
  12129. return _empty.assign();
  12130. }
  12131. //! Return a reference to an empty image \const.
  12132. static const CImg<T>& const_empty() {
  12133. static const CImg<T> _empty;
  12134. return _empty;
  12135. }
  12136. //@}
  12137. //------------------------------------------
  12138. //
  12139. //! \name Overloaded Operators
  12140. //@{
  12141. //------------------------------------------
  12142. //! Access to a pixel value.
  12143. /**
  12144. Return a reference to a located pixel value of the image instance,
  12145. being possibly \e const, whether the image instance is \e const or not.
  12146. This is the standard method to get/set pixel values in \c CImg<T> images.
  12147. \param x X-coordinate of the pixel value.
  12148. \param y Y-coordinate of the pixel value.
  12149. \param z Z-coordinate of the pixel value.
  12150. \param c C-coordinate of the pixel value.
  12151. \note
  12152. - Range of pixel coordinates start from <tt>(0,0,0,0)</tt> to
  12153. <tt>(width() - 1,height() - 1,depth() - 1,spectrum() - 1)</tt>.
  12154. - Due to the particular arrangement of the pixel buffers defined in %CImg, you can omit one coordinate if the
  12155. corresponding dimension is equal to \c 1.
  12156. For instance, pixels of a 2D image (depth() equal to \c 1) can be accessed by <tt>img(x,y,c)</tt> instead of
  12157. <tt>img(x,y,0,c)</tt>.
  12158. \warning
  12159. - There is \e no boundary checking done in this operator, to make it as fast as possible.
  12160. You \e must take care of out-of-bounds access by yourself, if necessary.
  12161. For debugging purposes, you may want to define macro \c 'cimg_verbosity'>=3 to enable additional boundary
  12162. checking operations in this operator. In that case, warning messages will be printed on the error output
  12163. when accessing out-of-bounds pixels.
  12164. \par Example
  12165. \code
  12166. CImg<float> img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'
  12167. const float
  12168. valR = img(10,10,0,0), // Read red value at coordinates (10,10)
  12169. valG = img(10,10,0,1), // Read green value at coordinates (10,10)
  12170. valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted)
  12171. avg = (valR + valG + valB)/3; // Compute average pixel value
  12172. img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value
  12173. \endcode
  12174. **/
  12175. #if cimg_verbosity>=3
  12176. T& operator()(const unsigned int x, const unsigned int y=0,
  12177. const unsigned int z=0, const unsigned int c=0) {
  12178. const ulongT off = (ulongT)offset(x,y,z,c);
  12179. if (!_data || off>=size()) {
  12180. cimg::warn(_cimg_instance
  12181. "operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].",
  12182. cimg_instance,
  12183. (int)x,(int)y,(int)z,(int)c,off);
  12184. return *_data;
  12185. }
  12186. else return _data[off];
  12187. }
  12188. //! Access to a pixel value \const.
  12189. const T& operator()(const unsigned int x, const unsigned int y=0,
  12190. const unsigned int z=0, const unsigned int c=0) const {
  12191. return const_cast<CImg<T>*>(this)->operator()(x,y,z,c);
  12192. }
  12193. //! Access to a pixel value.
  12194. /**
  12195. \param x X-coordinate of the pixel value.
  12196. \param y Y-coordinate of the pixel value.
  12197. \param z Z-coordinate of the pixel value.
  12198. \param c C-coordinate of the pixel value.
  12199. \param wh Precomputed offset, must be equal to <tt>width()*\ref height()</tt>.
  12200. \param whd Precomputed offset, must be equal to <tt>width()*\ref height()*\ref depth()</tt>.
  12201. \note
  12202. - Similar to (but faster than) operator()().
  12203. It uses precomputed offsets to optimize memory access. You may use it to optimize
  12204. the reading/writing of several pixel values in the same image (e.g. in a loop).
  12205. **/
  12206. T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
  12207. const ulongT wh, const ulongT whd=0) {
  12208. cimg::unused(wh,whd);
  12209. return (*this)(x,y,z,c);
  12210. }
  12211. //! Access to a pixel value \const.
  12212. const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
  12213. const ulongT wh, const ulongT whd=0) const {
  12214. cimg::unused(wh,whd);
  12215. return (*this)(x,y,z,c);
  12216. }
  12217. #else
  12218. T& operator()(const unsigned int x) {
  12219. return _data[x];
  12220. }
  12221. const T& operator()(const unsigned int x) const {
  12222. return _data[x];
  12223. }
  12224. T& operator()(const unsigned int x, const unsigned int y) {
  12225. return _data[x + y*_width];
  12226. }
  12227. const T& operator()(const unsigned int x, const unsigned int y) const {
  12228. return _data[x + y*_width];
  12229. }
  12230. T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) {
  12231. return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height];
  12232. }
  12233. const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) const {
  12234. return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height];
  12235. }
  12236. T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) {
  12237. return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height + c*(ulongT)_width*_height*_depth];
  12238. }
  12239. const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) const {
  12240. return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height + c*(ulongT)_width*_height*_depth];
  12241. }
  12242. T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int,
  12243. const ulongT wh) {
  12244. return _data[x + y*_width + z*wh];
  12245. }
  12246. const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int,
  12247. const ulongT wh) const {
  12248. return _data[x + y*_width + z*wh];
  12249. }
  12250. T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
  12251. const ulongT wh, const ulongT whd) {
  12252. return _data[x + y*_width + z*wh + c*whd];
  12253. }
  12254. const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
  12255. const ulongT wh, const ulongT whd) const {
  12256. return _data[x + y*_width + z*wh + c*whd];
  12257. }
  12258. #endif
  12259. //! Implicitly cast an image into a \c T*.
  12260. /**
  12261. Implicitly cast a \c CImg<T> instance into a \c T* or \c const \c T* pointer, whether the image instance
  12262. is \e const or not. The returned pointer points on the first value of the image pixel buffer.
  12263. \note
  12264. - It simply returns the pointer data() to the pixel buffer.
  12265. - This implicit conversion is convenient to test the empty state of images (data() being \c 0 in this case), e.g.
  12266. \code
  12267. CImg<float> img1(100,100), img2; // 'img1' is a 100x100 image, 'img2' is an empty image
  12268. if (img1) { // Test succeeds, 'img1' is not an empty image
  12269. if (!img2) { // Test succeeds, 'img2' is an empty image
  12270. std::printf("'img1' is not empty, 'img2' is empty.");
  12271. }
  12272. }
  12273. \endcode
  12274. - It also allows to use brackets to access pixel values, without need for a \c CImg<T>::operator[](), e.g.
  12275. \code
  12276. CImg<float> img(100,100);
  12277. const float value = img[99]; // Access to value of the last pixel on the first row
  12278. img[510] = 255; // Set pixel value at (10,5)
  12279. \endcode
  12280. **/
  12281. operator T*() {
  12282. return _data;
  12283. }
  12284. //! Implicitly cast an image into a \c T* \const.
  12285. operator const T*() const {
  12286. return _data;
  12287. }
  12288. //! Assign a value to all image pixels.
  12289. /**
  12290. Assign specified \c value to each pixel value of the image instance.
  12291. \param value Value that will be assigned to image pixels.
  12292. \note
  12293. - The image size is never modified.
  12294. - The \c value may be casted to pixel type \c T if necessary.
  12295. \par Example
  12296. \code
  12297. CImg<char> img(100,100); // Declare image (with garbage values)
  12298. img = 0; // Set all pixel values to '0'
  12299. img = 1.2; // Set all pixel values to '1' (cast of '1.2' as a 'char')
  12300. \endcode
  12301. **/
  12302. CImg<T>& operator=(const T& value) {
  12303. return fill(value);
  12304. }
  12305. //! Assign pixels values from a specified expression.
  12306. /**
  12307. Initialize all pixel values from the specified string \c expression.
  12308. \param expression Value string describing the way pixel values are set.
  12309. \note
  12310. - String parameter \c expression may describe different things:
  12311. - If \c expression is a list of values (as in \c "1,2,3,8,3,2"), or a formula (as in \c "(x*y)%255"),
  12312. the pixel values are set from specified \c expression and the image size is not modified.
  12313. - If \c expression is a filename (as in \c "reference.jpg"), the corresponding image file is loaded and
  12314. replace the image instance. The image size is modified if necessary.
  12315. \par Example
  12316. \code
  12317. CImg<float> img1(100,100), img2(img1), img3(img1); // Declare 3 scalar images 100x100 with uninitialized values
  12318. img1 = "0,50,100,150,200,250,200,150,100,50"; // Set pixel values of 'img1' from a value sequence
  12319. img2 = "10*((x*y)%25)"; // Set pixel values of 'img2' from a formula
  12320. img3 = "reference.jpg"; // Set pixel values of 'img3' from a file (image size is modified)
  12321. (img1,img2,img3).display();
  12322. \endcode
  12323. \image html ref_operator_eq.jpg
  12324. **/
  12325. CImg<T>& operator=(const char *const expression) {
  12326. const unsigned int omode = cimg::exception_mode();
  12327. cimg::exception_mode(0);
  12328. try {
  12329. _fill(expression,true,1,0,0,"operator=",0);
  12330. } catch (CImgException&) {
  12331. cimg::exception_mode(omode);
  12332. load(expression);
  12333. }
  12334. cimg::exception_mode(omode);
  12335. return *this;
  12336. }
  12337. //! Copy an image into the current image instance.
  12338. /**
  12339. Similar to the in-place copy constructor assign(const CImg<t>&).
  12340. **/
  12341. template<typename t>
  12342. CImg<T>& operator=(const CImg<t>& img) {
  12343. return assign(img);
  12344. }
  12345. //! Copy an image into the current image instance \specialization.
  12346. CImg<T>& operator=(const CImg<T>& img) {
  12347. return assign(img);
  12348. }
  12349. //! Copy the content of a display window to the current image instance.
  12350. /**
  12351. Similar to assign(const CImgDisplay&).
  12352. **/
  12353. CImg<T>& operator=(const CImgDisplay& disp) {
  12354. disp.snapshot(*this);
  12355. return *this;
  12356. }
  12357. //! In-place addition operator.
  12358. /**
  12359. Add specified \c value to all pixels of an image instance.
  12360. \param value Value to add.
  12361. \note
  12362. - Resulting pixel values are casted to fit the pixel type \c T.
  12363. For instance, adding \c 0.2 to a \c CImg<char> is possible but does nothing indeed.
  12364. - Overflow values are treated as with standard C++ numeric types. For instance,
  12365. \code
  12366. CImg<unsigned char> img(100,100,1,1,255); // Construct a 100x100 image with pixel values '255'
  12367. img+=1; // Add '1' to each pixels -> Overflow
  12368. // here all pixels of image 'img' are equal to '0'.
  12369. \endcode
  12370. - To prevent value overflow, you may want to consider pixel type \c T as \c float or \c double,
  12371. and use cut() after addition.
  12372. \par Example
  12373. \code
  12374. CImg<unsigned char> img1("reference.jpg"); // Load a 8-bits RGB image (values in [0,255])
  12375. CImg<float> img2(img1); // Construct a float-valued copy of 'img1'
  12376. img2+=100; // Add '100' to pixel values -> goes out of [0,255] but no problems with floats
  12377. img2.cut(0,255); // Cut values in [0,255] to fit the 'unsigned char' constraint
  12378. img1 = img2; // Rewrite safe result in 'unsigned char' version 'img1'
  12379. const CImg<unsigned char> img3 = (img1 + 100).cut(0,255); // Do the same in a more simple and elegant way
  12380. (img1,img2,img3).display();
  12381. \endcode
  12382. \image html ref_operator_plus.jpg
  12383. **/
  12384. template<typename t>
  12385. CImg<T>& operator+=(const t value) {
  12386. if (is_empty()) return *this;
  12387. cimg_openmp_for(*this,*ptr + value,524288);
  12388. return *this;
  12389. }
  12390. //! In-place addition operator.
  12391. /**
  12392. Add values to image pixels, according to the specified string \c expression.
  12393. \param expression Value string describing the way pixel values are added.
  12394. \note
  12395. - Similar to operator=(const char*), except that it adds values to the pixels of the current image instance,
  12396. instead of assigning them.
  12397. **/
  12398. CImg<T>& operator+=(const char *const expression) {
  12399. return *this+=(+*this)._fill(expression,true,1,0,0,"operator+=",this);
  12400. }
  12401. //! In-place addition operator.
  12402. /**
  12403. Add values to image pixels, according to the values of the input image \c img.
  12404. \param img Input image to add.
  12405. \note
  12406. - The size of the image instance is never modified.
  12407. - It is not mandatory that input image \c img has the same size as the image instance.
  12408. If less values are available in \c img, then the values are added periodically. For instance, adding one
  12409. WxH scalar image (spectrum() equal to \c 1) to one WxH color image (spectrum() equal to \c 3)
  12410. means each color channel will be incremented with the same values at the same locations.
  12411. \par Example
  12412. \code
  12413. CImg<float> img1("reference.jpg"); // Load a RGB color image (img1.spectrum()==3)
  12414. // Construct a scalar shading (img2.spectrum()==1).
  12415. const CImg<float> img2(img1.width(),img.height(),1,1,"255*(x/w)^2");
  12416. img1+=img2; // Add shading to each channel of 'img1'
  12417. img1.cut(0,255); // Prevent [0,255] overflow
  12418. (img2,img1).display();
  12419. \endcode
  12420. \image html ref_operator_plus1.jpg
  12421. **/
  12422. template<typename t>
  12423. CImg<T>& operator+=(const CImg<t>& img) {
  12424. const ulongT siz = size(), isiz = img.size();
  12425. if (siz && isiz) {
  12426. if (is_overlapped(img)) return *this+=+img;
  12427. T *ptrd = _data, *const ptre = _data + siz;
  12428. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  12429. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  12430. *ptrd = (T)(*ptrd + *(ptrs++));
  12431. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd + *(ptrs++));
  12432. }
  12433. return *this;
  12434. }
  12435. //! In-place increment operator (prefix).
  12436. /**
  12437. Add \c 1 to all image pixels, and return a reference to the current incremented image instance.
  12438. \note
  12439. - Writing \c ++img is equivalent to \c img+=1.
  12440. **/
  12441. CImg<T>& operator++() {
  12442. if (is_empty()) return *this;
  12443. cimg_openmp_for(*this,*ptr + 1,524288);
  12444. return *this;
  12445. }
  12446. //! In-place increment operator (postfix).
  12447. /**
  12448. Add \c 1 to all image pixels, and return a new copy of the initial (pre-incremented) image instance.
  12449. \note
  12450. - Use the prefixed version operator++() if you don't need a copy of the initial
  12451. (pre-incremented) image instance, since a useless image copy may be expensive in terms of memory usage.
  12452. **/
  12453. CImg<T> operator++(int) {
  12454. const CImg<T> copy(*this,false);
  12455. ++*this;
  12456. return copy;
  12457. }
  12458. //! Return a non-shared copy of the image instance.
  12459. /**
  12460. \note
  12461. - Use this operator to ensure you get a non-shared copy of an image instance with same pixel type \c T.
  12462. Indeed, the usual copy constructor CImg<T>(const CImg<T>&) returns a shared copy of a shared input image,
  12463. and it may be not desirable to work on a regular copy (e.g. for a resize operation) if you have no
  12464. information about the shared state of the input image.
  12465. - Writing \c (+img) is equivalent to \c CImg<T>(img,false).
  12466. **/
  12467. CImg<T> operator+() const {
  12468. return CImg<T>(*this,false);
  12469. }
  12470. //! Addition operator.
  12471. /**
  12472. Similar to operator+=(const t), except that it returns a new image instance instead of operating in-place.
  12473. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12474. **/
  12475. template<typename t>
  12476. CImg<_cimg_Tt> operator+(const t value) const {
  12477. return CImg<_cimg_Tt>(*this,false)+=value;
  12478. }
  12479. //! Addition operator.
  12480. /**
  12481. Similar to operator+=(const char*), except that it returns a new image instance instead of operating in-place.
  12482. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12483. **/
  12484. CImg<Tfloat> operator+(const char *const expression) const {
  12485. return CImg<Tfloat>(*this,false)+=expression;
  12486. }
  12487. //! Addition operator.
  12488. /**
  12489. Similar to operator+=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  12490. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12491. **/
  12492. template<typename t>
  12493. CImg<_cimg_Tt> operator+(const CImg<t>& img) const {
  12494. return CImg<_cimg_Tt>(*this,false)+=img;
  12495. }
  12496. //! In-place subtraction operator.
  12497. /**
  12498. Similar to operator+=(const t), except that it performs a subtraction instead of an addition.
  12499. **/
  12500. template<typename t>
  12501. CImg<T>& operator-=(const t value) {
  12502. if (is_empty()) return *this;
  12503. cimg_openmp_for(*this,*ptr - value,524288);
  12504. return *this;
  12505. }
  12506. //! In-place subtraction operator.
  12507. /**
  12508. Similar to operator+=(const char*), except that it performs a subtraction instead of an addition.
  12509. **/
  12510. CImg<T>& operator-=(const char *const expression) {
  12511. return *this-=(+*this)._fill(expression,true,1,0,0,"operator-=",this);
  12512. }
  12513. //! In-place subtraction operator.
  12514. /**
  12515. Similar to operator+=(const CImg<t>&), except that it performs a subtraction instead of an addition.
  12516. **/
  12517. template<typename t>
  12518. CImg<T>& operator-=(const CImg<t>& img) {
  12519. const ulongT siz = size(), isiz = img.size();
  12520. if (siz && isiz) {
  12521. if (is_overlapped(img)) return *this-=+img;
  12522. T *ptrd = _data, *const ptre = _data + siz;
  12523. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  12524. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  12525. *ptrd = (T)(*ptrd - *(ptrs++));
  12526. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd - *(ptrs++));
  12527. }
  12528. return *this;
  12529. }
  12530. //! In-place decrement operator (prefix).
  12531. /**
  12532. Similar to operator++(), except that it performs a decrement instead of an increment.
  12533. **/
  12534. CImg<T>& operator--() {
  12535. if (is_empty()) return *this;
  12536. cimg_openmp_for(*this,*ptr - 1,524288);
  12537. return *this;
  12538. }
  12539. //! In-place decrement operator (postfix).
  12540. /**
  12541. Similar to operator++(int), except that it performs a decrement instead of an increment.
  12542. **/
  12543. CImg<T> operator--(int) {
  12544. const CImg<T> copy(*this,false);
  12545. --*this;
  12546. return copy;
  12547. }
  12548. //! Replace each pixel by its opposite value.
  12549. /**
  12550. \note
  12551. - If the computed opposite values are out-of-range, they are treated as with standard C++ numeric types.
  12552. For instance, the \c unsigned \c char opposite of \c 1 is \c 255.
  12553. \par Example
  12554. \code
  12555. const CImg<unsigned char>
  12556. img1("reference.jpg"), // Load a RGB color image
  12557. img2 = -img1; // Compute its opposite (in 'unsigned char')
  12558. (img1,img2).display();
  12559. \endcode
  12560. \image html ref_operator_minus.jpg
  12561. **/
  12562. CImg<T> operator-() const {
  12563. return CImg<T>(_width,_height,_depth,_spectrum,(T)0)-=*this;
  12564. }
  12565. //! Subtraction operator.
  12566. /**
  12567. Similar to operator-=(const t), except that it returns a new image instance instead of operating in-place.
  12568. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12569. **/
  12570. template<typename t>
  12571. CImg<_cimg_Tt> operator-(const t value) const {
  12572. return CImg<_cimg_Tt>(*this,false)-=value;
  12573. }
  12574. //! Subtraction operator.
  12575. /**
  12576. Similar to operator-=(const char*), except that it returns a new image instance instead of operating in-place.
  12577. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12578. **/
  12579. CImg<Tfloat> operator-(const char *const expression) const {
  12580. return CImg<Tfloat>(*this,false)-=expression;
  12581. }
  12582. //! Subtraction operator.
  12583. /**
  12584. Similar to operator-=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  12585. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12586. **/
  12587. template<typename t>
  12588. CImg<_cimg_Tt> operator-(const CImg<t>& img) const {
  12589. return CImg<_cimg_Tt>(*this,false)-=img;
  12590. }
  12591. //! In-place multiplication operator.
  12592. /**
  12593. Similar to operator+=(const t), except that it performs a multiplication instead of an addition.
  12594. **/
  12595. template<typename t>
  12596. CImg<T>& operator*=(const t value) {
  12597. if (is_empty()) return *this;
  12598. cimg_openmp_for(*this,*ptr * value,262144);
  12599. return *this;
  12600. }
  12601. //! In-place multiplication operator.
  12602. /**
  12603. Similar to operator+=(const char*), except that it performs a multiplication instead of an addition.
  12604. **/
  12605. CImg<T>& operator*=(const char *const expression) {
  12606. return mul((+*this)._fill(expression,true,1,0,0,"operator*=",this));
  12607. }
  12608. //! In-place multiplication operator.
  12609. /**
  12610. Replace the image instance by the matrix multiplication between the image instance and the specified matrix
  12611. \c img.
  12612. \param img Second operand of the matrix multiplication.
  12613. \note
  12614. - It does \e not compute a pointwise multiplication between two images. For this purpose, use
  12615. mul(const CImg<t>&) instead.
  12616. - The size of the image instance can be modified by this operator.
  12617. \par Example
  12618. \code
  12619. CImg<float> A(2,2,1,1, 1,2,3,4); // Construct 2x2 matrix A = [1,2;3,4]
  12620. const CImg<float> X(1,2,1,1, 1,2); // Construct 1x2 vector X = [1;2]
  12621. A*=X; // Assign matrix multiplication A*X to 'A'
  12622. // 'A' is now a 1x2 vector whose values are [5;11].
  12623. \endcode
  12624. **/
  12625. template<typename t>
  12626. CImg<T>& operator*=(const CImg<t>& img) {
  12627. return ((*this)*img).move_to(*this);
  12628. }
  12629. //! Multiplication operator.
  12630. /**
  12631. Similar to operator*=(const t), except that it returns a new image instance instead of operating in-place.
  12632. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12633. **/
  12634. template<typename t>
  12635. CImg<_cimg_Tt> operator*(const t value) const {
  12636. return CImg<_cimg_Tt>(*this,false)*=value;
  12637. }
  12638. //! Multiplication operator.
  12639. /**
  12640. Similar to operator*=(const char*), except that it returns a new image instance instead of operating in-place.
  12641. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12642. **/
  12643. CImg<Tfloat> operator*(const char *const expression) const {
  12644. return CImg<Tfloat>(*this,false)*=expression;
  12645. }
  12646. //! Multiplication operator.
  12647. /**
  12648. Similar to operator*=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  12649. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12650. **/
  12651. template<typename t>
  12652. CImg<_cimg_Tt> operator*(const CImg<t>& img) const {
  12653. typedef _cimg_Ttdouble Ttdouble;
  12654. typedef _cimg_Tt Tt;
  12655. if (_width!=img._height || _depth!=1 || _spectrum!=1)
  12656. throw CImgArgumentException(_cimg_instance
  12657. "operator*(): Invalid multiplication of instance by specified "
  12658. "matrix (%u,%u,%u,%u,%p).",
  12659. cimg_instance,
  12660. img._width,img._height,img._depth,img._spectrum,img._data);
  12661. CImg<Tt> res(img._width,_height);
  12662. // Check for common cases to optimize.
  12663. if (img._width==1) { // Matrix * Vector
  12664. if (_height==1) switch (_width) { // Vector^T * Vector
  12665. case 1 :
  12666. res[0] = (Tt)((Ttdouble)_data[0]*img[0]);
  12667. return res;
  12668. case 2 :
  12669. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1]);
  12670. return res;
  12671. case 3 :
  12672. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
  12673. (Ttdouble)_data[2]*img[2]);
  12674. return res;
  12675. case 4 :
  12676. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
  12677. (Ttdouble)_data[2]*img[2] + (Ttdouble)_data[3]*img[3]);
  12678. return res;
  12679. default : {
  12680. Ttdouble val = 0;
  12681. cimg_pragma_openmp(parallel for reduction(+:val) cimg_openmp_if_size(size(),4096))
  12682. cimg_forX(*this,i) val+=(Ttdouble)_data[i]*img[i];
  12683. res[0] = val;
  12684. return res;
  12685. }
  12686. } else if (_height==_width) switch (_width) { // Square_matrix * Vector
  12687. case 2 : // 2x2_matrix * Vector
  12688. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1]);
  12689. res[1] = (Tt)((Ttdouble)_data[2]*img[0] + (Ttdouble)_data[3]*img[1]);
  12690. return res;
  12691. case 3 : // 3x3_matrix * Vector
  12692. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
  12693. (Ttdouble)_data[2]*img[2]);
  12694. res[1] = (Tt)((Ttdouble)_data[3]*img[0] + (Ttdouble)_data[4]*img[1] +
  12695. (Ttdouble)_data[5]*img[2]);
  12696. res[2] = (Tt)((Ttdouble)_data[6]*img[0] + (Ttdouble)_data[7]*img[1] +
  12697. (Ttdouble)_data[8]*img[2]);
  12698. return res;
  12699. case 4 : // 4x4_matrix * Vector
  12700. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
  12701. (Ttdouble)_data[2]*img[2] + (Ttdouble)_data[3]*img[3]);
  12702. res[1] = (Tt)((Ttdouble)_data[4]*img[0] + (Ttdouble)_data[5]*img[1] +
  12703. (Ttdouble)_data[6]*img[2] + (Ttdouble)_data[7]*img[3]);
  12704. res[2] = (Tt)((Ttdouble)_data[8]*img[0] + (Ttdouble)_data[9]*img[1] +
  12705. (Ttdouble)_data[10]*img[2] + (Ttdouble)_data[11]*img[3]);
  12706. res[3] = (Tt)((Ttdouble)_data[12]*img[0] + (Ttdouble)_data[13]*img[1] +
  12707. (Ttdouble)_data[14]*img[2] + (Ttdouble)_data[15]*img[3]);
  12708. return res;
  12709. }
  12710. } else if (_height==_width) {
  12711. if (img._height==img._width) switch (_width) { // Square_matrix * Square_matrix
  12712. case 2 : // 2x2_matrix * 2x2_matrix
  12713. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[2]);
  12714. res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[3]);
  12715. res[2] = (Tt)((Ttdouble)_data[2]*img[0] + (Ttdouble)_data[3]*img[2]);
  12716. res[3] = (Tt)((Ttdouble)_data[2]*img[1] + (Ttdouble)_data[3]*img[3]);
  12717. return res;
  12718. case 3 : // 3x3_matrix * 3x3_matrix
  12719. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[3] +
  12720. (Ttdouble)_data[2]*img[6]);
  12721. res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[4] +
  12722. (Ttdouble)_data[2]*img[7]);
  12723. res[2] = (Tt)((Ttdouble)_data[0]*img[2] + (Ttdouble)_data[1]*img[5] +
  12724. (Ttdouble)_data[2]*img[8]);
  12725. res[3] = (Tt)((Ttdouble)_data[3]*img[0] + (Ttdouble)_data[4]*img[3] +
  12726. (Ttdouble)_data[5]*img[6]);
  12727. res[4] = (Tt)((Ttdouble)_data[3]*img[1] + (Ttdouble)_data[4]*img[4] +
  12728. (Ttdouble)_data[5]*img[7]);
  12729. res[5] = (Tt)((Ttdouble)_data[3]*img[2] + (Ttdouble)_data[4]*img[5] +
  12730. (Ttdouble)_data[5]*img[8]);
  12731. res[6] = (Tt)((Ttdouble)_data[6]*img[0] + (Ttdouble)_data[7]*img[3] +
  12732. (Ttdouble)_data[8]*img[6]);
  12733. res[7] = (Tt)((Ttdouble)_data[6]*img[1] + (Ttdouble)_data[7]*img[4] +
  12734. (Ttdouble)_data[8]*img[7]);
  12735. res[8] = (Tt)((Ttdouble)_data[6]*img[2] + (Ttdouble)_data[7]*img[5] +
  12736. (Ttdouble)_data[8]*img[8]);
  12737. return res;
  12738. case 4 : // 4x4_matrix * 4x4_matrix
  12739. res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[4] +
  12740. (Ttdouble)_data[2]*img[8] + (Ttdouble)_data[3]*img[12]);
  12741. res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[5] +
  12742. (Ttdouble)_data[2]*img[9] + (Ttdouble)_data[3]*img[13]);
  12743. res[2] = (Tt)((Ttdouble)_data[0]*img[2] + (Ttdouble)_data[1]*img[6] +
  12744. (Ttdouble)_data[2]*img[10] + (Ttdouble)_data[3]*img[14]);
  12745. res[3] = (Tt)((Ttdouble)_data[0]*img[3] + (Ttdouble)_data[1]*img[7] +
  12746. (Ttdouble)_data[2]*img[11] + (Ttdouble)_data[3]*img[15]);
  12747. res[4] = (Tt)((Ttdouble)_data[4]*img[0] + (Ttdouble)_data[5]*img[4] +
  12748. (Ttdouble)_data[6]*img[8] + (Ttdouble)_data[7]*img[12]);
  12749. res[5] = (Tt)((Ttdouble)_data[4]*img[1] + (Ttdouble)_data[5]*img[5] +
  12750. (Ttdouble)_data[6]*img[9] + (Ttdouble)_data[7]*img[13]);
  12751. res[6] = (Tt)((Ttdouble)_data[4]*img[2] + (Ttdouble)_data[5]*img[6] +
  12752. (Ttdouble)_data[6]*img[10] + (Ttdouble)_data[7]*img[14]);
  12753. res[7] = (Tt)((Ttdouble)_data[4]*img[3] + (Ttdouble)_data[5]*img[7] +
  12754. (Ttdouble)_data[6]*img[11] + (Ttdouble)_data[7]*img[15]);
  12755. res[8] = (Tt)((Ttdouble)_data[8]*img[0] + (Ttdouble)_data[9]*img[4] +
  12756. (Ttdouble)_data[10]*img[8] + (Ttdouble)_data[11]*img[12]);
  12757. res[9] = (Tt)((Ttdouble)_data[8]*img[1] + (Ttdouble)_data[9]*img[5] +
  12758. (Ttdouble)_data[10]*img[9] + (Ttdouble)_data[11]*img[13]);
  12759. res[10] = (Tt)((Ttdouble)_data[8]*img[2] + (Ttdouble)_data[9]*img[6] +
  12760. (Ttdouble)_data[10]*img[10] + (Ttdouble)_data[11]*img[14]);
  12761. res[11] = (Tt)((Ttdouble)_data[8]*img[3] + (Ttdouble)_data[9]*img[7] +
  12762. (Ttdouble)_data[10]*img[11] + (Ttdouble)_data[11]*img[15]);
  12763. res[12] = (Tt)((Ttdouble)_data[12]*img[0] + (Ttdouble)_data[13]*img[4] +
  12764. (Ttdouble)_data[14]*img[8] + (Ttdouble)_data[15]*img[12]);
  12765. res[13] = (Tt)((Ttdouble)_data[12]*img[1] + (Ttdouble)_data[13]*img[5] +
  12766. (Ttdouble)_data[14]*img[9] + (Ttdouble)_data[15]*img[13]);
  12767. res[14] = (Tt)((Ttdouble)_data[12]*img[2] + (Ttdouble)_data[13]*img[6] +
  12768. (Ttdouble)_data[14]*img[10] + (Ttdouble)_data[15]*img[14]);
  12769. res[15] = (Tt)((Ttdouble)_data[12]*img[3] + (Ttdouble)_data[13]*img[7] +
  12770. (Ttdouble)_data[14]*img[11] + (Ttdouble)_data[15]*img[15]);
  12771. return res;
  12772. } else switch (_width) { // Square_matrix * Matrix
  12773. case 2 : { // 2x2_matrix * Matrix
  12774. const t *const ps0 = img.data(), *const ps1 = img.data(0,1);
  12775. Tt *const pd0 = res.data(), *const pd1 = res.data(0,1);
  12776. const Ttdouble
  12777. a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1],
  12778. a2 = (Ttdouble)_data[2], a3 = (Ttdouble)_data[3];
  12779. cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),4096))
  12780. cimg_forX(img,i) {
  12781. const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i];
  12782. pd0[i] = (Tt)(a0*x + a1*y);
  12783. pd1[i] = (Tt)(a2*x + a3*y);
  12784. }
  12785. return res;
  12786. }
  12787. case 3 : { // 3x3_matrix * Matrix
  12788. const t *const ps0 = img.data(), *const ps1 = img.data(0,1), *const ps2 = img.data(0,2);
  12789. Tt *const pd0 = res.data(), *const pd1 = res.data(0,1), *const pd2 = res.data(0,2);
  12790. const Ttdouble
  12791. a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1], a2 = (Ttdouble)_data[2],
  12792. a3 = (Ttdouble)_data[3], a4 = (Ttdouble)_data[4], a5 = (Ttdouble)_data[5],
  12793. a6 = (Ttdouble)_data[6], a7 = (Ttdouble)_data[7], a8 = (Ttdouble)_data[8];
  12794. cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),1024))
  12795. cimg_forX(img,i) {
  12796. const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i], z = (Ttdouble)ps2[i];
  12797. pd0[i] = (Tt)(a0*x + a1*y + a2*z);
  12798. pd1[i] = (Tt)(a3*x + a4*y + a5*z);
  12799. pd2[i] = (Tt)(a6*x + a7*y + a8*z);
  12800. }
  12801. return res;
  12802. }
  12803. case 4 : { // 4x4_matrix * Matrix
  12804. const t
  12805. *const ps0 = img.data(), *const ps1 = img.data(0,1),
  12806. *const ps2 = img.data(0,2), *const ps3 = img.data(0,3);
  12807. Tt
  12808. *const pd0 = res.data(), *const pd1 = res.data(0,1),
  12809. *const pd2 = res.data(0,2), *const pd3 = res.data(0,3);
  12810. const Ttdouble
  12811. a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1], a2 = (Ttdouble)_data[2], a3 = (Ttdouble)_data[3],
  12812. a4 = (Ttdouble)_data[4], a5 = (Ttdouble)_data[5], a6 = (Ttdouble)_data[6], a7 = (Ttdouble)_data[7],
  12813. a8 = (Ttdouble)_data[8], a9 = (Ttdouble)_data[9], a10 = (Ttdouble)_data[10], a11 = (Ttdouble)_data[11],
  12814. a12 = (Ttdouble)_data[12], a13 = (Ttdouble)_data[13], a14 = (Ttdouble)_data[14],
  12815. a15 = (Ttdouble)_data[15];
  12816. cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),512))
  12817. cimg_forX(img,i) {
  12818. const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i], z = (Ttdouble)ps2[i], c = (Ttdouble)ps3[i];
  12819. pd0[i] = (Tt)(a0*x + a1*y + a2*z + a3*c);
  12820. pd1[i] = (Tt)(a4*x + a5*y + a6*z + a7*c);
  12821. pd2[i] = (Tt)(a8*x + a9*y + a10*z + a11*c);
  12822. pd3[i] = (Tt)(a12*x + a13*y + a14*z + a15*c);
  12823. }
  12824. return res;
  12825. }
  12826. }
  12827. }
  12828. // Fallback to generic version.
  12829. #if cimg_use_openmp!=0
  12830. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  12831. cimg_openmp_if(size()>(cimg_openmp_sizefactor)*1024 &&
  12832. img.size()>(cimg_openmp_sizefactor)*1024))
  12833. cimg_forXY(res,i,j) {
  12834. Ttdouble value = 0;
  12835. cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k);
  12836. res(i,j) = (Tt)value;
  12837. }
  12838. #else
  12839. Tt *ptrd = res._data;
  12840. cimg_forXY(res,i,j) {
  12841. Ttdouble value = 0;
  12842. cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k);
  12843. *(ptrd++) = (Tt)value;
  12844. }
  12845. #endif
  12846. return res;
  12847. }
  12848. //! In-place division operator.
  12849. /**
  12850. Similar to operator+=(const t), except that it performs a division instead of an addition.
  12851. **/
  12852. template<typename t>
  12853. CImg<T>& operator/=(const t value) {
  12854. if (is_empty()) return *this;
  12855. cimg_openmp_for(*this,*ptr / value,32768);
  12856. return *this;
  12857. }
  12858. //! In-place division operator.
  12859. /**
  12860. Similar to operator+=(const char*), except that it performs a division instead of an addition.
  12861. **/
  12862. CImg<T>& operator/=(const char *const expression) {
  12863. return div((+*this)._fill(expression,true,1,0,0,"operator/=",this));
  12864. }
  12865. //! In-place division operator.
  12866. /**
  12867. Replace the image instance by the (right) matrix division between the image instance and the specified
  12868. matrix \c img.
  12869. \param img Second operand of the matrix division.
  12870. \note
  12871. - It does \e not compute a pointwise division between two images. For this purpose, use
  12872. div(const CImg<t>&) instead.
  12873. - It returns the matrix operation \c A*inverse(img).
  12874. - The size of the image instance can be modified by this operator.
  12875. **/
  12876. template<typename t>
  12877. CImg<T>& operator/=(const CImg<t>& img) {
  12878. return (*this*img.get_invert()).move_to(*this);
  12879. }
  12880. //! Division operator.
  12881. /**
  12882. Similar to operator/=(const t), except that it returns a new image instance instead of operating in-place.
  12883. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12884. **/
  12885. template<typename t>
  12886. CImg<_cimg_Tt> operator/(const t value) const {
  12887. return CImg<_cimg_Tt>(*this,false)/=value;
  12888. }
  12889. //! Division operator.
  12890. /**
  12891. Similar to operator/=(const char*), except that it returns a new image instance instead of operating in-place.
  12892. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12893. **/
  12894. CImg<Tfloat> operator/(const char *const expression) const {
  12895. return CImg<Tfloat>(*this,false)/=expression;
  12896. }
  12897. //! Division operator.
  12898. /**
  12899. Similar to operator/=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  12900. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12901. **/
  12902. template<typename t>
  12903. CImg<_cimg_Tt> operator/(const CImg<t>& img) const {
  12904. return (*this)*img.get_invert();
  12905. }
  12906. //! In-place modulo operator.
  12907. /**
  12908. Similar to operator+=(const t), except that it performs a modulo operation instead of an addition.
  12909. **/
  12910. template<typename t>
  12911. CImg<T>& operator%=(const t value) {
  12912. if (is_empty()) return *this;
  12913. cimg_openmp_for(*this,cimg::mod(*ptr,(T)value),16384);
  12914. return *this;
  12915. }
  12916. //! In-place modulo operator.
  12917. /**
  12918. Similar to operator+=(const char*), except that it performs a modulo operation instead of an addition.
  12919. **/
  12920. CImg<T>& operator%=(const char *const expression) {
  12921. return *this%=(+*this)._fill(expression,true,1,0,0,"operator%=",this);
  12922. }
  12923. //! In-place modulo operator.
  12924. /**
  12925. Similar to operator+=(const CImg<t>&), except that it performs a modulo operation instead of an addition.
  12926. **/
  12927. template<typename t>
  12928. CImg<T>& operator%=(const CImg<t>& img) {
  12929. const ulongT siz = size(), isiz = img.size();
  12930. if (siz && isiz) {
  12931. if (is_overlapped(img)) return *this%=+img;
  12932. T *ptrd = _data, *const ptre = _data + siz;
  12933. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  12934. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  12935. *ptrd = cimg::mod(*ptrd,(T)*(ptrs++));
  12936. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::mod(*ptrd,(T)*(ptrs++));
  12937. }
  12938. return *this;
  12939. }
  12940. //! Modulo operator.
  12941. /**
  12942. Similar to operator%=(const t), except that it returns a new image instance instead of operating in-place.
  12943. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12944. **/
  12945. template<typename t>
  12946. CImg<_cimg_Tt> operator%(const t value) const {
  12947. return CImg<_cimg_Tt>(*this,false)%=value;
  12948. }
  12949. //! Modulo operator.
  12950. /**
  12951. Similar to operator%=(const char*), except that it returns a new image instance instead of operating in-place.
  12952. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12953. **/
  12954. CImg<Tfloat> operator%(const char *const expression) const {
  12955. return CImg<Tfloat>(*this,false)%=expression;
  12956. }
  12957. //! Modulo operator.
  12958. /**
  12959. Similar to operator%=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  12960. The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
  12961. **/
  12962. template<typename t>
  12963. CImg<_cimg_Tt> operator%(const CImg<t>& img) const {
  12964. return CImg<_cimg_Tt>(*this,false)%=img;
  12965. }
  12966. //! In-place bitwise AND operator.
  12967. /**
  12968. Similar to operator+=(const t), except that it performs a bitwise AND operation instead of an addition.
  12969. **/
  12970. template<typename t>
  12971. CImg<T>& operator&=(const t value) {
  12972. if (is_empty()) return *this;
  12973. cimg_openmp_for(*this,(longT)*ptr & (longT)value,32768);
  12974. return *this;
  12975. }
  12976. //! In-place bitwise AND operator.
  12977. /**
  12978. Similar to operator+=(const char*), except that it performs a bitwise AND operation instead of an addition.
  12979. **/
  12980. CImg<T>& operator&=(const char *const expression) {
  12981. return *this&=(+*this)._fill(expression,true,1,0,0,"operator&=",this);
  12982. }
  12983. //! In-place bitwise AND operator.
  12984. /**
  12985. Similar to operator+=(const CImg<t>&), except that it performs a bitwise AND operation instead of an addition.
  12986. **/
  12987. template<typename t>
  12988. CImg<T>& operator&=(const CImg<t>& img) {
  12989. const ulongT siz = size(), isiz = img.size();
  12990. if (siz && isiz) {
  12991. if (is_overlapped(img)) return *this&=+img;
  12992. T *ptrd = _data, *const ptre = _data + siz;
  12993. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  12994. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  12995. *ptrd = (T)((longT)*ptrd & (longT)*(ptrs++));
  12996. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd & (longT)*(ptrs++));
  12997. }
  12998. return *this;
  12999. }
  13000. //! Bitwise AND operator.
  13001. /**
  13002. Similar to operator&=(const t), except that it returns a new image instance instead of operating in-place.
  13003. The pixel type of the returned image is \c T.
  13004. **/
  13005. template<typename t>
  13006. CImg<T> operator&(const t value) const {
  13007. return (+*this)&=value;
  13008. }
  13009. //! Bitwise AND operator.
  13010. /**
  13011. Similar to operator&=(const char*), except that it returns a new image instance instead of operating in-place.
  13012. The pixel type of the returned image is \c T.
  13013. **/
  13014. CImg<T> operator&(const char *const expression) const {
  13015. return (+*this)&=expression;
  13016. }
  13017. //! Bitwise AND operator.
  13018. /**
  13019. Similar to operator&=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  13020. The pixel type of the returned image is \c T.
  13021. **/
  13022. template<typename t>
  13023. CImg<T> operator&(const CImg<t>& img) const {
  13024. return (+*this)&=img;
  13025. }
  13026. //! In-place bitwise OR operator.
  13027. /**
  13028. Similar to operator+=(const t), except that it performs a bitwise OR operation instead of an addition.
  13029. **/
  13030. template<typename t>
  13031. CImg<T>& operator|=(const t value) {
  13032. if (is_empty()) return *this;
  13033. cimg_openmp_for(*this,(longT)*ptr | (longT)value,32768);
  13034. return *this;
  13035. }
  13036. //! In-place bitwise OR operator.
  13037. /**
  13038. Similar to operator+=(const char*), except that it performs a bitwise OR operation instead of an addition.
  13039. **/
  13040. CImg<T>& operator|=(const char *const expression) {
  13041. return *this|=(+*this)._fill(expression,true,1,0,0,"operator|=",this);
  13042. }
  13043. //! In-place bitwise OR operator.
  13044. /**
  13045. Similar to operator+=(const CImg<t>&), except that it performs a bitwise OR operation instead of an addition.
  13046. **/
  13047. template<typename t>
  13048. CImg<T>& operator|=(const CImg<t>& img) {
  13049. const ulongT siz = size(), isiz = img.size();
  13050. if (siz && isiz) {
  13051. if (is_overlapped(img)) return *this|=+img;
  13052. T *ptrd = _data, *const ptre = _data + siz;
  13053. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  13054. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  13055. *ptrd = (T)((longT)*ptrd | (longT)*(ptrs++));
  13056. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd | (longT)*(ptrs++));
  13057. }
  13058. return *this;
  13059. }
  13060. //! Bitwise OR operator.
  13061. /**
  13062. Similar to operator|=(const t), except that it returns a new image instance instead of operating in-place.
  13063. The pixel type of the returned image is \c T.
  13064. **/
  13065. template<typename t>
  13066. CImg<T> operator|(const t value) const {
  13067. return (+*this)|=value;
  13068. }
  13069. //! Bitwise OR operator.
  13070. /**
  13071. Similar to operator|=(const char*), except that it returns a new image instance instead of operating in-place.
  13072. The pixel type of the returned image is \c T.
  13073. **/
  13074. CImg<T> operator|(const char *const expression) const {
  13075. return (+*this)|=expression;
  13076. }
  13077. //! Bitwise OR operator.
  13078. /**
  13079. Similar to operator|=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  13080. The pixel type of the returned image is \c T.
  13081. **/
  13082. template<typename t>
  13083. CImg<T> operator|(const CImg<t>& img) const {
  13084. return (+*this)|=img;
  13085. }
  13086. //! In-place bitwise XOR operator.
  13087. /**
  13088. Similar to operator+=(const t), except that it performs a bitwise XOR operation instead of an addition.
  13089. \warning
  13090. - It does \e not compute the \e power of pixel values. For this purpose, use pow(const t) instead.
  13091. **/
  13092. template<typename t>
  13093. CImg<T>& operator^=(const t value) {
  13094. if (is_empty()) return *this;
  13095. cimg_openmp_for(*this,(longT)*ptr ^ (longT)value,32768);
  13096. return *this;
  13097. }
  13098. //! In-place bitwise XOR operator.
  13099. /**
  13100. Similar to operator+=(const char*), except that it performs a bitwise XOR operation instead of an addition.
  13101. \warning
  13102. - It does \e not compute the \e power of pixel values. For this purpose, use pow(const char*) instead.
  13103. **/
  13104. CImg<T>& operator^=(const char *const expression) {
  13105. return *this^=(+*this)._fill(expression,true,1,0,0,"operator^=",this);
  13106. }
  13107. //! In-place bitwise XOR operator.
  13108. /**
  13109. Similar to operator+=(const CImg<t>&), except that it performs a bitwise XOR operation instead of an addition.
  13110. \warning
  13111. - It does \e not compute the \e power of pixel values. For this purpose, use pow(const CImg<t>&) instead.
  13112. **/
  13113. template<typename t>
  13114. CImg<T>& operator^=(const CImg<t>& img) {
  13115. const ulongT siz = size(), isiz = img.size();
  13116. if (siz && isiz) {
  13117. if (is_overlapped(img)) return *this^=+img;
  13118. T *ptrd = _data, *const ptre = _data + siz;
  13119. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  13120. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  13121. *ptrd = (T)((longT)*ptrd ^ (longT)*(ptrs++));
  13122. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd ^ (longT)*(ptrs++));
  13123. }
  13124. return *this;
  13125. }
  13126. //! Bitwise XOR operator.
  13127. /**
  13128. Similar to operator^=(const t), except that it returns a new image instance instead of operating in-place.
  13129. The pixel type of the returned image is \c T.
  13130. **/
  13131. template<typename t>
  13132. CImg<T> operator^(const t value) const {
  13133. return (+*this)^=value;
  13134. }
  13135. //! Bitwise XOR operator.
  13136. /**
  13137. Similar to operator^=(const char*), except that it returns a new image instance instead of operating in-place.
  13138. The pixel type of the returned image is \c T.
  13139. **/
  13140. CImg<T> operator^(const char *const expression) const {
  13141. return (+*this)^=expression;
  13142. }
  13143. //! Bitwise XOR operator.
  13144. /**
  13145. Similar to operator^=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
  13146. The pixel type of the returned image is \c T.
  13147. **/
  13148. template<typename t>
  13149. CImg<T> operator^(const CImg<t>& img) const {
  13150. return (+*this)^=img;
  13151. }
  13152. //! In-place bitwise left shift operator.
  13153. /**
  13154. Similar to operator+=(const t), except that it performs a bitwise left shift instead of an addition.
  13155. **/
  13156. template<typename t>
  13157. CImg<T>& operator<<=(const t value) {
  13158. if (is_empty()) return *this;
  13159. cimg_openmp_for(*this,((longT)*ptr) << (int)value,65536);
  13160. return *this;
  13161. }
  13162. //! In-place bitwise left shift operator.
  13163. /**
  13164. Similar to operator+=(const char*), except that it performs a bitwise left shift instead of an addition.
  13165. **/
  13166. CImg<T>& operator<<=(const char *const expression) {
  13167. return *this<<=(+*this)._fill(expression,true,1,0,0,"operator<<=",this);
  13168. }
  13169. //! In-place bitwise left shift operator.
  13170. /**
  13171. Similar to operator+=(const CImg<t>&), except that it performs a bitwise left shift instead of an addition.
  13172. **/
  13173. template<typename t>
  13174. CImg<T>& operator<<=(const CImg<t>& img) {
  13175. const ulongT siz = size(), isiz = img.size();
  13176. if (siz && isiz) {
  13177. if (is_overlapped(img)) return *this^=+img;
  13178. T *ptrd = _data, *const ptre = _data + siz;
  13179. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  13180. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  13181. *ptrd = (T)((longT)*ptrd << (int)*(ptrs++));
  13182. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd << (int)*(ptrs++));
  13183. }
  13184. return *this;
  13185. }
  13186. //! Bitwise left shift operator.
  13187. /**
  13188. Similar to operator<<=(const t), except that it returns a new image instance instead of operating in-place.
  13189. The pixel type of the returned image is \c T.
  13190. **/
  13191. template<typename t>
  13192. CImg<T> operator<<(const t value) const {
  13193. return (+*this)<<=value;
  13194. }
  13195. //! Bitwise left shift operator.
  13196. /**
  13197. Similar to operator<<=(const char*), except that it returns a new image instance instead of operating in-place.
  13198. The pixel type of the returned image is \c T.
  13199. **/
  13200. CImg<T> operator<<(const char *const expression) const {
  13201. return (+*this)<<=expression;
  13202. }
  13203. //! Bitwise left shift operator.
  13204. /**
  13205. Similar to operator<<=(const CImg<t>&), except that it returns a new image instance instead of
  13206. operating in-place.
  13207. The pixel type of the returned image is \c T.
  13208. **/
  13209. template<typename t>
  13210. CImg<T> operator<<(const CImg<t>& img) const {
  13211. return (+*this)<<=img;
  13212. }
  13213. //! In-place bitwise right shift operator.
  13214. /**
  13215. Similar to operator+=(const t), except that it performs a bitwise right shift instead of an addition.
  13216. **/
  13217. template<typename t>
  13218. CImg<T>& operator>>=(const t value) {
  13219. if (is_empty()) return *this;
  13220. cimg_openmp_for(*this,((longT)*ptr) >> (int)value,65536);
  13221. return *this;
  13222. }
  13223. //! In-place bitwise right shift operator.
  13224. /**
  13225. Similar to operator+=(const char*), except that it performs a bitwise right shift instead of an addition.
  13226. **/
  13227. CImg<T>& operator>>=(const char *const expression) {
  13228. return *this>>=(+*this)._fill(expression,true,1,0,0,"operator>>=",this);
  13229. }
  13230. //! In-place bitwise right shift operator.
  13231. /**
  13232. Similar to operator+=(const CImg<t>&), except that it performs a bitwise right shift instead of an addition.
  13233. **/
  13234. template<typename t>
  13235. CImg<T>& operator>>=(const CImg<t>& img) {
  13236. const ulongT siz = size(), isiz = img.size();
  13237. if (siz && isiz) {
  13238. if (is_overlapped(img)) return *this^=+img;
  13239. T *ptrd = _data, *const ptre = _data + siz;
  13240. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  13241. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  13242. *ptrd = (T)((longT)*ptrd >> (int)*(ptrs++));
  13243. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd >> (int)*(ptrs++));
  13244. }
  13245. return *this;
  13246. }
  13247. //! Bitwise right shift operator.
  13248. /**
  13249. Similar to operator>>=(const t), except that it returns a new image instance instead of operating in-place.
  13250. The pixel type of the returned image is \c T.
  13251. **/
  13252. template<typename t>
  13253. CImg<T> operator>>(const t value) const {
  13254. return (+*this)>>=value;
  13255. }
  13256. //! Bitwise right shift operator.
  13257. /**
  13258. Similar to operator>>=(const char*), except that it returns a new image instance instead of operating in-place.
  13259. The pixel type of the returned image is \c T.
  13260. **/
  13261. CImg<T> operator>>(const char *const expression) const {
  13262. return (+*this)>>=expression;
  13263. }
  13264. //! Bitwise right shift operator.
  13265. /**
  13266. Similar to operator>>=(const CImg<t>&), except that it returns a new image instance instead of
  13267. operating in-place.
  13268. The pixel type of the returned image is \c T.
  13269. **/
  13270. template<typename t>
  13271. CImg<T> operator>>(const CImg<t>& img) const {
  13272. return (+*this)>>=img;
  13273. }
  13274. //! Bitwise inversion operator.
  13275. /**
  13276. Similar to operator-(), except that it compute the bitwise inverse instead of the opposite value.
  13277. **/
  13278. CImg<T> operator~() const {
  13279. CImg<T> res(_width,_height,_depth,_spectrum);
  13280. const T *ptrs = _data;
  13281. cimg_for(res,ptrd,T) { const ulongT value = (ulongT)*(ptrs++); *ptrd = (T)~value; }
  13282. return res;
  13283. }
  13284. //! Test if all pixels of an image have the same value.
  13285. /**
  13286. Return \c true is all pixels of the image instance are equal to the specified \c value.
  13287. \param value Reference value to compare with.
  13288. **/
  13289. template<typename t>
  13290. bool operator==(const t value) const {
  13291. if (is_empty()) return false;
  13292. typedef _cimg_Tt Tt;
  13293. bool is_equal = true;
  13294. for (T *ptrd = _data + size(); is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)value)) {}
  13295. return is_equal;
  13296. }
  13297. //! Test if all pixel values of an image follow a specified expression.
  13298. /**
  13299. Return \c true is all pixels of the image instance are equal to the specified \c expression.
  13300. \param expression Value string describing the way pixel values are compared.
  13301. **/
  13302. bool operator==(const char *const expression) const {
  13303. return *this==(+*this)._fill(expression,true,1,0,0,"operator==",this);
  13304. }
  13305. //! Test if two images have the same size and values.
  13306. /**
  13307. Return \c true if the image instance and the input image \c img have the same pixel values,
  13308. even if the dimensions of the two images do not match. It returns \c false otherwise.
  13309. \param img Input image to compare with.
  13310. \note
  13311. - The pixel buffer pointers data() of the two compared images do not have to be the same for operator==()
  13312. to return \c true.
  13313. Only the dimensions and the pixel values matter. Thus, the comparison can be \c true even for different
  13314. pixel types \c T and \c t.
  13315. \par Example
  13316. \code
  13317. const CImg<float> img1(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'float' pixel values)
  13318. const CImg<char> img2(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'char' pixel values)
  13319. if (img1==img2) { // Test succeeds, image dimensions and values are the same
  13320. std::printf("'img1' and 'img2' have same dimensions and values.");
  13321. }
  13322. \endcode
  13323. **/
  13324. template<typename t>
  13325. bool operator==(const CImg<t>& img) const {
  13326. typedef _cimg_Tt Tt;
  13327. const ulongT siz = size();
  13328. bool is_equal = true;
  13329. if (siz!=img.size()) return false;
  13330. t *ptrs = img._data + siz;
  13331. for (T *ptrd = _data + siz; is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)*(--ptrs))) {}
  13332. return is_equal;
  13333. }
  13334. //! Test if pixels of an image are all different from a value.
  13335. /**
  13336. Return \c true is all pixels of the image instance are different than the specified \c value.
  13337. \param value Reference value to compare with.
  13338. **/
  13339. template<typename t>
  13340. bool operator!=(const t value) const {
  13341. return !((*this)==value);
  13342. }
  13343. //! Test if all pixel values of an image are different from a specified expression.
  13344. /**
  13345. Return \c true is all pixels of the image instance are different to the specified \c expression.
  13346. \param expression Value string describing the way pixel values are compared.
  13347. **/
  13348. bool operator!=(const char *const expression) const {
  13349. return !((*this)==expression);
  13350. }
  13351. //! Test if two images have different sizes or values.
  13352. /**
  13353. Return \c true if the image instance and the input image \c img have different dimensions or pixel values,
  13354. and \c false otherwise.
  13355. \param img Input image to compare with.
  13356. \note
  13357. - Writing \c img1!=img2 is equivalent to \c !(img1==img2).
  13358. **/
  13359. template<typename t>
  13360. bool operator!=(const CImg<t>& img) const {
  13361. return !((*this)==img);
  13362. }
  13363. //! Construct an image list from two images.
  13364. /**
  13365. Return a new list of image (\c CImgList instance) containing exactly two elements:
  13366. - A copy of the image instance, at position [\c 0].
  13367. - A copy of the specified image \c img, at position [\c 1].
  13368. \param img Input image that will be the second image of the resulting list.
  13369. \note
  13370. - The family of operator,() is convenient to easily create list of images, but it is also \e quite \e slow
  13371. in practice (see warning below).
  13372. - Constructed lists contain no shared images. If image instance or input image \c img are shared, they are
  13373. inserted as new non-shared copies in the resulting list.
  13374. - The pixel type of the returned list may be a superset of the initial pixel type \c T, if necessary.
  13375. \warning
  13376. - Pipelining operator,() \c N times will perform \c N copies of the entire content of a (growing) image list.
  13377. This may become very expensive in terms of speed and used memory. You should avoid using this technique to
  13378. build a new CImgList instance from several images, if you are seeking for performance.
  13379. Fast insertions of images in an image list are possible with
  13380. CImgList<T>::insert(const CImg<t>&,unsigned int,bool) or move_to(CImgList<t>&,unsigned int).
  13381. \par Example
  13382. \code
  13383. const CImg<float>
  13384. img1("reference.jpg"),
  13385. img2 = img1.get_mirror('x'),
  13386. img3 = img2.get_blur(5);
  13387. const CImgList<float> list = (img1,img2); // Create list of two elements from 'img1' and 'img2'
  13388. (list,img3).display(); // Display image list containing copies of 'img1','img2' and 'img3'
  13389. \endcode
  13390. \image html ref_operator_comma.jpg
  13391. **/
  13392. template<typename t>
  13393. CImgList<_cimg_Tt> operator,(const CImg<t>& img) const {
  13394. return CImgList<_cimg_Tt>(*this,img);
  13395. }
  13396. //! Construct an image list from image instance and an input image list.
  13397. /**
  13398. Return a new list of images (\c CImgList instance) containing exactly \c list.size() \c + \c 1 elements:
  13399. - A copy of the image instance, at position [\c 0].
  13400. - A copy of the specified image list \c list, from positions [\c 1] to [\c list.size()].
  13401. \param list Input image list that will be appended to the image instance.
  13402. \note
  13403. - Similar to operator,(const CImg<t>&) const, except that it takes an image list as an argument.
  13404. **/
  13405. template<typename t>
  13406. CImgList<_cimg_Tt> operator,(const CImgList<t>& list) const {
  13407. return CImgList<_cimg_Tt>(list,false).insert(*this,0);
  13408. }
  13409. //! Split image along specified axis.
  13410. /**
  13411. Return a new list of images (\c CImgList instance) containing the split components
  13412. of the instance image along the specified axis.
  13413. \param axis Splitting axis (can be '\c x','\c y','\c z' or '\c c')
  13414. \note
  13415. - Similar to get_split(char,int) const, with default second argument.
  13416. \par Example
  13417. \code
  13418. const CImg<unsigned char> img("reference.jpg"); // Load a RGB color image
  13419. const CImgList<unsigned char> list = (img<'c'); // Get a list of its three R,G,B channels
  13420. (img,list).display();
  13421. \endcode
  13422. \image html ref_operator_less.jpg
  13423. **/
  13424. CImgList<T> operator<(const char axis) const {
  13425. return get_split(axis);
  13426. }
  13427. //@}
  13428. //-------------------------------------
  13429. //
  13430. //! \name Instance Characteristics
  13431. //@{
  13432. //-------------------------------------
  13433. //! Return the type of image pixel values as a C string.
  13434. /**
  13435. Return a \c char* string containing the usual type name of the image pixel values
  13436. (i.e. a stringified version of the template parameter \c T).
  13437. \note
  13438. - The returned string may contain spaces (as in \c "unsigned char").
  13439. - If the pixel type \c T does not correspond to a registered type, the string <tt>"unknown"</tt> is returned.
  13440. **/
  13441. static const char* pixel_type() {
  13442. return cimg::type<T>::string();
  13443. }
  13444. //! Return the number of image columns.
  13445. /**
  13446. Return the image width, i.e. the image dimension along the X-axis.
  13447. \note
  13448. - The width() of an empty image is equal to \c 0.
  13449. - width() is typically equal to \c 1 when considering images as \e vectors for matrix calculations.
  13450. - width() returns an \c int, although the image width is internally stored as an \c unsigned \c int.
  13451. Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
  13452. \c unsigned \c int variables.
  13453. Access to the initial \c unsigned \c int variable is possible (though not recommended) by
  13454. <tt>(*this)._width</tt>.
  13455. **/
  13456. int width() const {
  13457. return (int)_width;
  13458. }
  13459. //! Return the number of image rows.
  13460. /**
  13461. Return the image height, i.e. the image dimension along the Y-axis.
  13462. \note
  13463. - The height() of an empty image is equal to \c 0.
  13464. - height() returns an \c int, although the image height is internally stored as an \c unsigned \c int.
  13465. Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
  13466. \c unsigned \c int variables.
  13467. Access to the initial \c unsigned \c int variable is possible (though not recommended) by
  13468. <tt>(*this)._height</tt>.
  13469. **/
  13470. int height() const {
  13471. return (int)_height;
  13472. }
  13473. //! Return the number of image slices.
  13474. /**
  13475. Return the image depth, i.e. the image dimension along the Z-axis.
  13476. \note
  13477. - The depth() of an empty image is equal to \c 0.
  13478. - depth() is typically equal to \c 1 when considering usual 2D images. When depth()\c > \c 1, the image
  13479. is said to be \e volumetric.
  13480. - depth() returns an \c int, although the image depth is internally stored as an \c unsigned \c int.
  13481. Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
  13482. \c unsigned \c int variables.
  13483. Access to the initial \c unsigned \c int variable is possible (though not recommended) by
  13484. <tt>(*this)._depth</tt>.
  13485. **/
  13486. int depth() const {
  13487. return (int)_depth;
  13488. }
  13489. //! Return the number of image channels.
  13490. /**
  13491. Return the number of image channels, i.e. the image dimension along the C-axis.
  13492. \note
  13493. - The spectrum() of an empty image is equal to \c 0.
  13494. - spectrum() is typically equal to \c 1 when considering scalar-valued images, to \c 3
  13495. for RGB-coded color images, and to \c 4 for RGBA-coded color images (with alpha-channel).
  13496. The number of channels of an image instance is not limited. The meaning of the pixel values is not linked
  13497. up to the number of channels (e.g. a 4-channel image may indifferently stands for a RGBA or CMYK color image).
  13498. - spectrum() returns an \c int, although the image spectrum is internally stored as an \c unsigned \c int.
  13499. Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
  13500. \c unsigned \c int variables.
  13501. Access to the initial \c unsigned \c int variable is possible (though not recommended) by
  13502. <tt>(*this)._spectrum</tt>.
  13503. **/
  13504. int spectrum() const {
  13505. return (int)_spectrum;
  13506. }
  13507. //! Return the total number of pixel values.
  13508. /**
  13509. Return <tt>width()*\ref height()*\ref depth()*\ref spectrum()</tt>,
  13510. i.e. the total number of values of type \c T in the pixel buffer of the image instance.
  13511. \note
  13512. - The size() of an empty image is equal to \c 0.
  13513. - The allocated memory size for a pixel buffer of a non-shared \c CImg<T> instance is equal to
  13514. <tt>size()*sizeof(T)</tt>.
  13515. \par Example
  13516. \code
  13517. const CImg<float> img(100,100,1,3); // Construct new 100x100 color image
  13518. if (img.size()==30000) // Test succeeds
  13519. std::printf("Pixel buffer uses %lu bytes",
  13520. img.size()*sizeof(float));
  13521. \endcode
  13522. **/
  13523. ulongT size() const {
  13524. return (ulongT)_width*_height*_depth*_spectrum;
  13525. }
  13526. //! Return a pointer to the first pixel value.
  13527. /**
  13528. Return a \c T*, or a \c const \c T* pointer to the first value in the pixel buffer of the image instance,
  13529. whether the instance is \c const or not.
  13530. \note
  13531. - The data() of an empty image is equal to \c 0 (null pointer).
  13532. - The allocated pixel buffer for the image instance starts from \c data()
  13533. and goes to <tt>data()+\ref size() - 1</tt> (included).
  13534. - To get the pointer to one particular location of the pixel buffer, use
  13535. data(unsigned int,unsigned int,unsigned int,unsigned int) instead.
  13536. **/
  13537. T* data() {
  13538. return _data;
  13539. }
  13540. //! Return a pointer to the first pixel value \const.
  13541. const T* data() const {
  13542. return _data;
  13543. }
  13544. //! Return a pointer to a located pixel value.
  13545. /**
  13546. Return a \c T*, or a \c const \c T* pointer to the value located at (\c x,\c y,\c z,\c c) in the pixel buffer
  13547. of the image instance,
  13548. whether the instance is \c const or not.
  13549. \param x X-coordinate of the pixel value.
  13550. \param y Y-coordinate of the pixel value.
  13551. \param z Z-coordinate of the pixel value.
  13552. \param c C-coordinate of the pixel value.
  13553. \note
  13554. - Writing \c img.data(x,y,z,c) is equivalent to <tt>&(img(x,y,z,c))</tt>. Thus, this method has the same
  13555. properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int).
  13556. **/
  13557. #if cimg_verbosity>=3
  13558. T *data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) {
  13559. const ulongT off = (ulongT)offset(x,y,z,c);
  13560. if (off>=size())
  13561. cimg::warn(_cimg_instance
  13562. "data(): Invalid pointer request, at coordinates (%u,%u,%u,%u) [offset=%u].",
  13563. cimg_instance,
  13564. x,y,z,c,off);
  13565. return _data + off;
  13566. }
  13567. //! Return a pointer to a located pixel value \const.
  13568. const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const {
  13569. return const_cast<CImg<T>*>(this)->data(x,y,z,c);
  13570. }
  13571. #else
  13572. T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) {
  13573. return _data + x + (ulongT)y*_width + (ulongT)z*_width*_height + (ulongT)c*_width*_height*_depth;
  13574. }
  13575. const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const {
  13576. return _data + x + (ulongT)y*_width + (ulongT)z*_width*_height + (ulongT)c*_width*_height*_depth;
  13577. }
  13578. #endif
  13579. //! Return the offset to a located pixel value, with respect to the beginning of the pixel buffer.
  13580. /**
  13581. \param x X-coordinate of the pixel value.
  13582. \param y Y-coordinate of the pixel value.
  13583. \param z Z-coordinate of the pixel value.
  13584. \param c C-coordinate of the pixel value.
  13585. \note
  13586. - Writing \c img.data(x,y,z,c) is equivalent to <tt>&(img(x,y,z,c)) - img.data()</tt>.
  13587. Thus, this method has the same properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int).
  13588. \par Example
  13589. \code
  13590. const CImg<float> img(100,100,1,3); // Define a 100x100 RGB-color image
  13591. const long off = img.offset(10,10,0,2); // Get the offset of the blue value of the pixel located at (10,10)
  13592. const float val = img[off]; // Get the blue value of this pixel
  13593. \endcode
  13594. **/
  13595. longT offset(const int x, const int y=0, const int z=0, const int c=0) const {
  13596. return x + (longT)y*_width + (longT)z*_width*_height + (longT)c*_width*_height*_depth;
  13597. }
  13598. //! Return a CImg<T>::iterator pointing to the first pixel value.
  13599. /**
  13600. \note
  13601. - Equivalent to data().
  13602. - It has been mainly defined for compatibility with STL naming conventions.
  13603. **/
  13604. iterator begin() {
  13605. return _data;
  13606. }
  13607. //! Return a CImg<T>::iterator pointing to the first value of the pixel buffer \const.
  13608. const_iterator begin() const {
  13609. return _data;
  13610. }
  13611. //! Return a CImg<T>::iterator pointing next to the last pixel value.
  13612. /**
  13613. \note
  13614. - Writing \c img.end() is equivalent to <tt>img.data() + img.size()</tt>.
  13615. - It has been mainly defined for compatibility with STL naming conventions.
  13616. \warning
  13617. - The returned iterator actually points to a value located \e outside the acceptable bounds of the pixel buffer.
  13618. Trying to read or write the content of the returned iterator will probably result in a crash.
  13619. Use it mainly as a strict upper bound for a CImg<T>::iterator.
  13620. \par Example
  13621. \code
  13622. CImg<float> img(100,100,1,3); // Define a 100x100 RGB color image
  13623. // 'img.end()' used below as an upper bound for the iterator.
  13624. for (CImg<float>::iterator it = img.begin(); it<img.end(); ++it)
  13625. *it = 0;
  13626. \endcode
  13627. **/
  13628. iterator end() {
  13629. return _data + size();
  13630. }
  13631. //! Return a CImg<T>::iterator pointing next to the last pixel value \const.
  13632. const_iterator end() const {
  13633. return _data + size();
  13634. }
  13635. //! Return a reference to the first pixel value.
  13636. /**
  13637. \note
  13638. - Writing \c img.front() is equivalent to <tt>img[0]</tt>, or <tt>img(0,0,0,0)</tt>.
  13639. - It has been mainly defined for compatibility with STL naming conventions.
  13640. **/
  13641. T& front() {
  13642. return *_data;
  13643. }
  13644. //! Return a reference to the first pixel value \const.
  13645. const T& front() const {
  13646. return *_data;
  13647. }
  13648. //! Return a reference to the last pixel value.
  13649. /**
  13650. \note
  13651. - Writing \c img.back() is equivalent to <tt>img[img.size() - 1]</tt>, or
  13652. <tt>img(img.width() - 1,img.height() - 1,img.depth() - 1,img.spectrum() - 1)</tt>.
  13653. - It has been mainly defined for compatibility with STL naming conventions.
  13654. **/
  13655. T& back() {
  13656. return *(_data + size() - 1);
  13657. }
  13658. //! Return a reference to the last pixel value \const.
  13659. const T& back() const {
  13660. return *(_data + size() - 1);
  13661. }
  13662. //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions.
  13663. /**
  13664. Return a reference to the pixel value of the image instance located at a specified \c offset,
  13665. or to a specified default value in case of out-of-bounds access.
  13666. \param offset Offset to the desired pixel value.
  13667. \param out_value Default value returned if \c offset is outside image bounds.
  13668. \note
  13669. - Writing \c img.at(offset,out_value) is similar to <tt>img[offset]</tt>, except that if \c offset
  13670. is outside bounds (e.g. \c offset<0 or \c offset>=img.size()), a reference to a value \c out_value
  13671. is safely returned instead.
  13672. - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
  13673. you are \e not sure about the validity of the specified pixel offset.
  13674. **/
  13675. T& at(const int offset, const T& out_value) {
  13676. return (offset<0 || offset>=(int)size())?(cimg::temporary(out_value)=out_value):(*this)[offset];
  13677. }
  13678. //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions \const.
  13679. T at(const int offset, const T& out_value) const {
  13680. return (offset<0 || offset>=(int)size())?out_value:(*this)[offset];
  13681. }
  13682. //! Access to a pixel value at a specified offset, using Neumann boundary conditions.
  13683. /**
  13684. Return a reference to the pixel value of the image instance located at a specified \c offset,
  13685. or to the nearest pixel location in the image instance in case of out-of-bounds access.
  13686. \param offset Offset to the desired pixel value.
  13687. \note
  13688. - Similar to at(int,const T), except that an out-of-bounds access returns the value of the
  13689. nearest pixel in the image instance, regarding the specified offset, i.e.
  13690. - If \c offset<0, then \c img[0] is returned.
  13691. - If \c offset>=img.size(), then \c img[img.size() - 1] is returned.
  13692. - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
  13693. you are \e not sure about the validity of the specified pixel offset.
  13694. - If you know your image instance is \e not empty, you may rather use the slightly faster method \c _at(int).
  13695. **/
  13696. T& at(const int offset) {
  13697. if (is_empty())
  13698. throw CImgInstanceException(_cimg_instance
  13699. "at(): Empty instance.",
  13700. cimg_instance);
  13701. return _at(offset);
  13702. }
  13703. T& _at(const int offset) {
  13704. const unsigned int siz = (unsigned int)size();
  13705. return (*this)[offset<0?0:(unsigned int)offset>=siz?siz - 1:offset];
  13706. }
  13707. //! Access to a pixel value at a specified offset, using Neumann boundary conditions \const.
  13708. const T& at(const int offset) const {
  13709. if (is_empty())
  13710. throw CImgInstanceException(_cimg_instance
  13711. "at(): Empty instance.",
  13712. cimg_instance);
  13713. return _at(offset);
  13714. }
  13715. const T& _at(const int offset) const {
  13716. const unsigned int siz = (unsigned int)size();
  13717. return (*this)[offset<0?0:(unsigned int)offset>=siz?siz - 1:offset];
  13718. }
  13719. //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate.
  13720. /**
  13721. Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c),
  13722. or to a specified default value in case of out-of-bounds access along the X-axis.
  13723. \param x X-coordinate of the pixel value.
  13724. \param y Y-coordinate of the pixel value.
  13725. \param z Z-coordinate of the pixel value.
  13726. \param c C-coordinate of the pixel value.
  13727. \param out_value Default value returned if \c (\c x,\c y,\c z,\c c) is outside image bounds.
  13728. \note
  13729. - Similar to operator()(), except that an out-of-bounds access along the X-axis returns the specified value
  13730. \c out_value.
  13731. - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
  13732. you are \e not sure about the validity of the specified pixel coordinates.
  13733. \warning
  13734. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  13735. **/
  13736. T& atX(const int x, const int y, const int z, const int c, const T& out_value) {
  13737. return (x<0 || x>=width())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
  13738. }
  13739. //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate \const.
  13740. T atX(const int x, const int y, const int z, const int c, const T& out_value) const {
  13741. return (x<0 || x>=width())?out_value:(*this)(x,y,z,c);
  13742. }
  13743. //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate.
  13744. /**
  13745. Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c),
  13746. or to the nearest pixel location in the image instance in case of out-of-bounds access along the X-axis.
  13747. \param x X-coordinate of the pixel value.
  13748. \param y Y-coordinate of the pixel value.
  13749. \param z Z-coordinate of the pixel value.
  13750. \param c C-coordinate of the pixel value.
  13751. \note
  13752. - Similar to at(int,int,int,int,const T), except that an out-of-bounds access returns the value of the
  13753. nearest pixel in the image instance, regarding the specified X-coordinate.
  13754. - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
  13755. you are \e not sure about the validity of the specified pixel coordinates.
  13756. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  13757. \c _at(int,int,int,int).
  13758. \warning
  13759. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  13760. **/
  13761. T& atX(const int x, const int y=0, const int z=0, const int c=0) {
  13762. if (is_empty())
  13763. throw CImgInstanceException(_cimg_instance
  13764. "atX(): Empty instance.",
  13765. cimg_instance);
  13766. return _atX(x,y,z,c);
  13767. }
  13768. T& _atX(const int x, const int y=0, const int z=0, const int c=0) {
  13769. return (*this)(x<0?0:(x>=width()?width() - 1:x),y,z,c);
  13770. }
  13771. //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate \const.
  13772. const T& atX(const int x, const int y=0, const int z=0, const int c=0) const {
  13773. if (is_empty())
  13774. throw CImgInstanceException(_cimg_instance
  13775. "atX(): Empty instance.",
  13776. cimg_instance);
  13777. return _atX(x,y,z,c);
  13778. }
  13779. const T& _atX(const int x, const int y=0, const int z=0, const int c=0) const {
  13780. return (*this)(x<0?0:(x>=width()?width() - 1:x),y,z,c);
  13781. }
  13782. //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y-coordinates.
  13783. /**
  13784. Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on X and Y-coordinates.
  13785. **/
  13786. T& atXY(const int x, const int y, const int z, const int c, const T& out_value) {
  13787. return (x<0 || y<0 || x>=width() || y>=height())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
  13788. }
  13789. //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y coordinates \const.
  13790. T atXY(const int x, const int y, const int z, const int c, const T& out_value) const {
  13791. return (x<0 || y<0 || x>=width() || y>=height())?out_value:(*this)(x,y,z,c);
  13792. }
  13793. //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates.
  13794. /**
  13795. Similar to atX(int,int,int,int), except that boundary checking is performed both on X and Y-coordinates.
  13796. \note
  13797. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  13798. \c _atXY(int,int,int,int).
  13799. **/
  13800. T& atXY(const int x, const int y, const int z=0, const int c=0) {
  13801. if (is_empty())
  13802. throw CImgInstanceException(_cimg_instance
  13803. "atXY(): Empty instance.",
  13804. cimg_instance);
  13805. return _atXY(x,y,z,c);
  13806. }
  13807. T& _atXY(const int x, const int y, const int z=0, const int c=0) {
  13808. return (*this)(cimg::cut(x,0,width() - 1),
  13809. cimg::cut(y,0,height() - 1),z,c);
  13810. }
  13811. //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates \const.
  13812. const T& atXY(const int x, const int y, const int z=0, const int c=0) const {
  13813. if (is_empty())
  13814. throw CImgInstanceException(_cimg_instance
  13815. "atXY(): Empty instance.",
  13816. cimg_instance);
  13817. return _atXY(x,y,z,c);
  13818. }
  13819. const T& _atXY(const int x, const int y, const int z=0, const int c=0) const {
  13820. return (*this)(cimg::cut(x,0,width() - 1),
  13821. cimg::cut(y,0,height() - 1),z,c);
  13822. }
  13823. //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates.
  13824. /**
  13825. Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on
  13826. X,Y and Z-coordinates.
  13827. **/
  13828. T& atXYZ(const int x, const int y, const int z, const int c, const T& out_value) {
  13829. return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?
  13830. (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
  13831. }
  13832. //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates \const.
  13833. T atXYZ(const int x, const int y, const int z, const int c, const T& out_value) const {
  13834. return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?out_value:(*this)(x,y,z,c);
  13835. }
  13836. //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates.
  13837. /**
  13838. Similar to atX(int,int,int,int), except that boundary checking is performed both on X,Y and Z-coordinates.
  13839. \note
  13840. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  13841. \c _atXYZ(int,int,int,int).
  13842. **/
  13843. T& atXYZ(const int x, const int y, const int z, const int c=0) {
  13844. if (is_empty())
  13845. throw CImgInstanceException(_cimg_instance
  13846. "atXYZ(): Empty instance.",
  13847. cimg_instance);
  13848. return _atXYZ(x,y,z,c);
  13849. }
  13850. T& _atXYZ(const int x, const int y, const int z, const int c=0) {
  13851. return (*this)(cimg::cut(x,0,width() - 1),
  13852. cimg::cut(y,0,height() - 1),
  13853. cimg::cut(z,0,depth() - 1),c);
  13854. }
  13855. //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates \const.
  13856. const T& atXYZ(const int x, const int y, const int z, const int c=0) const {
  13857. if (is_empty())
  13858. throw CImgInstanceException(_cimg_instance
  13859. "atXYZ(): Empty instance.",
  13860. cimg_instance);
  13861. return _atXYZ(x,y,z,c);
  13862. }
  13863. const T& _atXYZ(const int x, const int y, const int z, const int c=0) const {
  13864. return (*this)(cimg::cut(x,0,width() - 1),
  13865. cimg::cut(y,0,height() - 1),
  13866. cimg::cut(z,0,depth() - 1),c);
  13867. }
  13868. //! Access to a pixel value, using Dirichlet boundary conditions.
  13869. /**
  13870. Similar to atX(int,int,int,int,const T), except that boundary checking is performed on all
  13871. X,Y,Z and C-coordinates.
  13872. **/
  13873. T& atXYZC(const int x, const int y, const int z, const int c, const T& out_value) {
  13874. return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?
  13875. (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
  13876. }
  13877. //! Access to a pixel value, using Dirichlet boundary conditions \const.
  13878. T atXYZC(const int x, const int y, const int z, const int c, const T& out_value) const {
  13879. return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?out_value:
  13880. (*this)(x,y,z,c);
  13881. }
  13882. //! Access to a pixel value, using Neumann boundary conditions.
  13883. /**
  13884. Similar to atX(int,int,int,int), except that boundary checking is performed on all X,Y,Z and C-coordinates.
  13885. \note
  13886. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  13887. \c _atXYZC(int,int,int,int).
  13888. **/
  13889. T& atXYZC(const int x, const int y, const int z, const int c) {
  13890. if (is_empty())
  13891. throw CImgInstanceException(_cimg_instance
  13892. "atXYZC(): Empty instance.",
  13893. cimg_instance);
  13894. return _atXYZC(x,y,z,c);
  13895. }
  13896. T& _atXYZC(const int x, const int y, const int z, const int c) {
  13897. return (*this)(cimg::cut(x,0,width() - 1),
  13898. cimg::cut(y,0,height() - 1),
  13899. cimg::cut(z,0,depth() - 1),
  13900. cimg::cut(c,0,spectrum() - 1));
  13901. }
  13902. //! Access to a pixel value, using Neumann boundary conditions \const.
  13903. const T& atXYZC(const int x, const int y, const int z, const int c) const {
  13904. if (is_empty())
  13905. throw CImgInstanceException(_cimg_instance
  13906. "atXYZC(): Empty instance.",
  13907. cimg_instance);
  13908. return _atXYZC(x,y,z,c);
  13909. }
  13910. const T& _atXYZC(const int x, const int y, const int z, const int c) const {
  13911. return (*this)(cimg::cut(x,0,width() - 1),
  13912. cimg::cut(y,0,height() - 1),
  13913. cimg::cut(z,0,depth() - 1),
  13914. cimg::cut(c,0,spectrum() - 1));
  13915. }
  13916. //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X-coordinate.
  13917. /**
  13918. Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
  13919. or a specified default value in case of out-of-bounds access along the X-axis.
  13920. \param fx X-coordinate of the pixel value (float-valued).
  13921. \param y Y-coordinate of the pixel value.
  13922. \param z Z-coordinate of the pixel value.
  13923. \param c C-coordinate of the pixel value.
  13924. \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds.
  13925. \note
  13926. - Similar to atX(int,int,int,int,const T), except that the returned pixel value is approximated by
  13927. a linear interpolation along the X-axis, if corresponding coordinates are not integers.
  13928. - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued.
  13929. \warning
  13930. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  13931. **/
  13932. Tfloat linear_atX(const float fx, const int y, const int z, const int c, const T& out_value) const {
  13933. const int
  13934. x = (int)fx - (fx>=0?0:1), nx = x + 1;
  13935. const float
  13936. dx = fx - x;
  13937. const Tfloat
  13938. Ic = (Tfloat)atX(x,y,z,c,out_value), In = (Tfloat)atXY(nx,y,z,c,out_value);
  13939. return Ic + dx*(In - Ic);
  13940. }
  13941. //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X-coordinate.
  13942. /**
  13943. Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
  13944. or the value of the nearest pixel location in the image instance in case of out-of-bounds access along
  13945. the X-axis.
  13946. \param fx X-coordinate of the pixel value (float-valued).
  13947. \param y Y-coordinate of the pixel value.
  13948. \param z Z-coordinate of the pixel value.
  13949. \param c C-coordinate of the pixel value.
  13950. \note
  13951. - Similar to linear_atX(float,int,int,int,const T) const, except that an out-of-bounds access returns
  13952. the value of the nearest pixel in the image instance, regarding the specified X-coordinate.
  13953. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  13954. \c _linear_atX(float,int,int,int).
  13955. \warning
  13956. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  13957. **/
  13958. Tfloat linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
  13959. if (is_empty())
  13960. throw CImgInstanceException(_cimg_instance
  13961. "linear_atX(): Empty instance.",
  13962. cimg_instance);
  13963. return _linear_atX(fx,y,z,c);
  13964. }
  13965. Tfloat _linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
  13966. const float
  13967. nfx = cimg::cut(fx,0,width() - 1);
  13968. const unsigned int
  13969. x = (unsigned int)nfx;
  13970. const float
  13971. dx = nfx - x;
  13972. const unsigned int
  13973. nx = dx>0?x + 1:x;
  13974. const Tfloat
  13975. Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c);
  13976. return Ic + dx*(In - Ic);
  13977. }
  13978. //! Return pixel value, using linear interpolation and periodic boundary conditions for the X-coordinate.
  13979. Tfloat linear_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
  13980. if (is_empty())
  13981. throw CImgInstanceException(_cimg_instance
  13982. "linear_atX_p(): Empty instance.",
  13983. cimg_instance);
  13984. return _linear_atX_p(fx,y,z,c);
  13985. }
  13986. Tfloat _linear_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
  13987. const float
  13988. nfx = cimg::mod(fx,_width - 0.5f);
  13989. const unsigned int
  13990. x = (unsigned int)nfx;
  13991. const float
  13992. dx = nfx - x;
  13993. const unsigned int
  13994. nx = cimg::mod(x + 1,_width);
  13995. const Tfloat
  13996. Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c);
  13997. return Ic + dx*(In - Ic);
  13998. }
  13999. //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X and Y-coordinates.
  14000. /**
  14001. Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
  14002. boundary checking are achieved both for X and Y-coordinates.
  14003. **/
  14004. Tfloat linear_atXY(const float fx, const float fy, const int z, const int c, const T& out_value) const {
  14005. const int
  14006. x = (int)fx - (fx>=0?0:1), nx = x + 1,
  14007. y = (int)fy - (fy>=0?0:1), ny = y + 1;
  14008. const float
  14009. dx = fx - x,
  14010. dy = fy - y;
  14011. const Tfloat
  14012. Icc = (Tfloat)atXY(x,y,z,c,out_value), Inc = (Tfloat)atXY(nx,y,z,c,out_value),
  14013. Icn = (Tfloat)atXY(x,ny,z,c,out_value), Inn = (Tfloat)atXY(nx,ny,z,c,out_value);
  14014. return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
  14015. }
  14016. //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X and Y-coordinates.
  14017. /**
  14018. Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
  14019. are achieved both for X and Y-coordinates.
  14020. \note
  14021. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14022. \c _linear_atXY(float,float,int,int).
  14023. **/
  14024. Tfloat linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
  14025. if (is_empty())
  14026. throw CImgInstanceException(_cimg_instance
  14027. "linear_atXY(): Empty instance.",
  14028. cimg_instance);
  14029. return _linear_atXY(fx,fy,z,c);
  14030. }
  14031. Tfloat _linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
  14032. const float
  14033. nfx = cimg::cut(fx,0,width() - 1),
  14034. nfy = cimg::cut(fy,0,height() - 1);
  14035. const unsigned int
  14036. x = (unsigned int)nfx,
  14037. y = (unsigned int)nfy;
  14038. const float
  14039. dx = nfx - x,
  14040. dy = nfy - y;
  14041. const unsigned int
  14042. nx = dx>0?x + 1:x,
  14043. ny = dy>0?y + 1:y;
  14044. const Tfloat
  14045. Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
  14046. Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c);
  14047. return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
  14048. }
  14049. //! Return pixel value, using linear interpolation and periodic boundary conditions for the X and Y-coordinates.
  14050. Tfloat linear_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
  14051. if (is_empty())
  14052. throw CImgInstanceException(_cimg_instance
  14053. "linear_atXY_p(): Empty instance.",
  14054. cimg_instance);
  14055. return _linear_atXY_p(fx,fy,z,c);
  14056. }
  14057. Tfloat _linear_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
  14058. const float
  14059. nfx = cimg::mod(fx,_width - 0.5f),
  14060. nfy = cimg::mod(fy,_height - 0.5f);
  14061. const unsigned int
  14062. x = (unsigned int)nfx,
  14063. y = (unsigned int)nfy;
  14064. const float
  14065. dx = nfx - x,
  14066. dy = nfy - y;
  14067. const unsigned int
  14068. nx = cimg::mod(x + 1,_width),
  14069. ny = cimg::mod(y + 1,_height);
  14070. const Tfloat
  14071. Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
  14072. Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c);
  14073. return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
  14074. }
  14075. //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates.
  14076. /**
  14077. Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
  14078. boundary checking are achieved both for X,Y and Z-coordinates.
  14079. **/
  14080. Tfloat linear_atXYZ(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
  14081. const int
  14082. x = (int)fx - (fx>=0?0:1), nx = x + 1,
  14083. y = (int)fy - (fy>=0?0:1), ny = y + 1,
  14084. z = (int)fz - (fz>=0?0:1), nz = z + 1;
  14085. const float
  14086. dx = fx - x,
  14087. dy = fy - y,
  14088. dz = fz - z;
  14089. const Tfloat
  14090. Iccc = (Tfloat)atXYZ(x,y,z,c,out_value), Incc = (Tfloat)atXYZ(nx,y,z,c,out_value),
  14091. Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value),
  14092. Iccn = (Tfloat)atXYZ(x,y,nz,c,out_value), Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value),
  14093. Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value);
  14094. return Iccc +
  14095. (Incc - Iccc +
  14096. (Iccc + Innc - Icnc - Incc +
  14097. (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
  14098. (Iccc + Incn - Iccn - Incc)*dz)*dx +
  14099. (Icnc - Iccc +
  14100. (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
  14101. (Iccn - Iccc)*dz;
  14102. }
  14103. //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
  14104. /**
  14105. Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
  14106. are achieved both for X,Y and Z-coordinates.
  14107. \note
  14108. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14109. \c _linear_atXYZ(float,float,float,int).
  14110. **/
  14111. Tfloat linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const {
  14112. if (is_empty())
  14113. throw CImgInstanceException(_cimg_instance
  14114. "linear_atXYZ(): Empty instance.",
  14115. cimg_instance);
  14116. return _linear_atXYZ(fx,fy,fz,c);
  14117. }
  14118. Tfloat _linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const {
  14119. const float
  14120. nfx = cimg::cut(fx,0,width() - 1),
  14121. nfy = cimg::cut(fy,0,height() - 1),
  14122. nfz = cimg::cut(fz,0,depth() - 1);
  14123. const unsigned int
  14124. x = (unsigned int)nfx,
  14125. y = (unsigned int)nfy,
  14126. z = (unsigned int)nfz;
  14127. const float
  14128. dx = nfx - x,
  14129. dy = nfy - y,
  14130. dz = nfz - z;
  14131. const unsigned int
  14132. nx = dx>0?x + 1:x,
  14133. ny = dy>0?y + 1:y,
  14134. nz = dz>0?z + 1:z;
  14135. const Tfloat
  14136. Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c),
  14137. Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c),
  14138. Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c),
  14139. Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c);
  14140. return Iccc +
  14141. (Incc - Iccc +
  14142. (Iccc + Innc - Icnc - Incc +
  14143. (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
  14144. (Iccc + Incn - Iccn - Incc)*dz)*dx +
  14145. (Icnc - Iccc +
  14146. (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
  14147. (Iccn - Iccc)*dz;
  14148. }
  14149. //! Return pixel value, using linear interpolation and periodic boundary conditions for the X,Y and Z-coordinates.
  14150. Tfloat linear_atXYZ_p(const float fx, const float fy=0, const float fz=0, const int c=0) const {
  14151. if (is_empty())
  14152. throw CImgInstanceException(_cimg_instance
  14153. "linear_atXYZ_p(): Empty instance.",
  14154. cimg_instance);
  14155. return _linear_atXYZ_p(fx,fy,fz,c);
  14156. }
  14157. Tfloat _linear_atXYZ_p(const float fx, const float fy=0, const float fz=0, const int c=0) const {
  14158. const float
  14159. nfx = cimg::mod(fx,_width - 0.5f),
  14160. nfy = cimg::mod(fy,_height - 0.5f),
  14161. nfz = cimg::mod(fz,_depth - 0.5f);
  14162. const unsigned int
  14163. x = (unsigned int)nfx,
  14164. y = (unsigned int)nfy,
  14165. z = (unsigned int)nfz;
  14166. const float
  14167. dx = nfx - x,
  14168. dy = nfy - y,
  14169. dz = nfz - z;
  14170. const unsigned int
  14171. nx = cimg::mod(x + 1,_width),
  14172. ny = cimg::mod(y + 1,_height),
  14173. nz = cimg::mod(z + 1,_depth);
  14174. const Tfloat
  14175. Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c),
  14176. Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c),
  14177. Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c),
  14178. Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c);
  14179. return Iccc +
  14180. (Incc - Iccc +
  14181. (Iccc + Innc - Icnc - Incc +
  14182. (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
  14183. (Iccc + Incn - Iccn - Incc)*dz)*dx +
  14184. (Icnc - Iccc +
  14185. (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
  14186. (Iccn - Iccc)*dz;
  14187. }
  14188. //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for all X,Y,Z,C-coordinates.
  14189. /**
  14190. Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
  14191. boundary checking are achieved for all X,Y,Z and C-coordinates.
  14192. **/
  14193. Tfloat linear_atXYZC(const float fx, const float fy, const float fz, const float fc, const T& out_value) const {
  14194. const int
  14195. x = (int)fx - (fx>=0?0:1), nx = x + 1,
  14196. y = (int)fy - (fy>=0?0:1), ny = y + 1,
  14197. z = (int)fz - (fz>=0?0:1), nz = z + 1,
  14198. c = (int)fc - (fc>=0?0:1), nc = c + 1;
  14199. const float
  14200. dx = fx - x,
  14201. dy = fy - y,
  14202. dz = fz - z,
  14203. dc = fc - c;
  14204. const Tfloat
  14205. Icccc = (Tfloat)atXYZC(x,y,z,c,out_value), Inccc = (Tfloat)atXYZC(nx,y,z,c,out_value),
  14206. Icncc = (Tfloat)atXYZC(x,ny,z,c,out_value), Inncc = (Tfloat)atXYZC(nx,ny,z,c,out_value),
  14207. Iccnc = (Tfloat)atXYZC(x,y,nz,c,out_value), Incnc = (Tfloat)atXYZC(nx,y,nz,c,out_value),
  14208. Icnnc = (Tfloat)atXYZC(x,ny,nz,c,out_value), Innnc = (Tfloat)atXYZC(nx,ny,nz,c,out_value),
  14209. Icccn = (Tfloat)atXYZC(x,y,z,nc,out_value), Inccn = (Tfloat)atXYZC(nx,y,z,nc,out_value),
  14210. Icncn = (Tfloat)atXYZC(x,ny,z,nc,out_value), Inncn = (Tfloat)atXYZC(nx,ny,z,nc,out_value),
  14211. Iccnn = (Tfloat)atXYZC(x,y,nz,nc,out_value), Incnn = (Tfloat)atXYZC(nx,y,nz,nc,out_value),
  14212. Icnnn = (Tfloat)atXYZC(x,ny,nz,nc,out_value), Innnn = (Tfloat)atXYZC(nx,ny,nz,nc,out_value);
  14213. return Icccc +
  14214. dx*(Inccc - Icccc +
  14215. dy*(Icccc + Inncc - Icncc - Inccc +
  14216. dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
  14217. dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
  14218. Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
  14219. dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
  14220. dz*(Icccc + Incnc - Iccnc - Inccc +
  14221. dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
  14222. dc*(Icccc + Inccn - Inccc - Icccn)) +
  14223. dy*(Icncc - Icccc +
  14224. dz*(Icccc + Icnnc - Iccnc - Icncc +
  14225. dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
  14226. dc*(Icccc + Icncn - Icncc - Icccn)) +
  14227. dz*(Iccnc - Icccc +
  14228. dc*(Icccc + Iccnn - Iccnc - Icccn)) +
  14229. dc*(Icccn -Icccc);
  14230. }
  14231. //! Return pixel value, using linear interpolation and Neumann boundary conditions for all X,Y,Z and C-coordinates.
  14232. /**
  14233. Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
  14234. are achieved for all X,Y,Z and C-coordinates.
  14235. \note
  14236. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14237. \c _linear_atXYZC(float,float,float,float).
  14238. **/
  14239. Tfloat linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
  14240. if (is_empty())
  14241. throw CImgInstanceException(_cimg_instance
  14242. "linear_atXYZC(): Empty instance.",
  14243. cimg_instance);
  14244. return _linear_atXYZC(fx,fy,fz,fc);
  14245. }
  14246. Tfloat _linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
  14247. const float
  14248. nfx = cimg::cut(fx,0,width() - 1),
  14249. nfy = cimg::cut(fy,0,height() - 1),
  14250. nfz = cimg::cut(fz,0,depth() - 1),
  14251. nfc = cimg::cut(fc,0,spectrum() - 1);
  14252. const unsigned int
  14253. x = (unsigned int)nfx,
  14254. y = (unsigned int)nfy,
  14255. z = (unsigned int)nfz,
  14256. c = (unsigned int)nfc;
  14257. const float
  14258. dx = nfx - x,
  14259. dy = nfy - y,
  14260. dz = nfz - z,
  14261. dc = nfc - c;
  14262. const unsigned int
  14263. nx = dx>0?x + 1:x,
  14264. ny = dy>0?y + 1:y,
  14265. nz = dz>0?z + 1:z,
  14266. nc = dc>0?c + 1:c;
  14267. const Tfloat
  14268. Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c),
  14269. Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c),
  14270. Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c),
  14271. Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c),
  14272. Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc),
  14273. Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc),
  14274. Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc),
  14275. Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc);
  14276. return Icccc +
  14277. dx*(Inccc - Icccc +
  14278. dy*(Icccc + Inncc - Icncc - Inccc +
  14279. dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
  14280. dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
  14281. Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
  14282. dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
  14283. dz*(Icccc + Incnc - Iccnc - Inccc +
  14284. dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
  14285. dc*(Icccc + Inccn - Inccc - Icccn)) +
  14286. dy*(Icncc - Icccc +
  14287. dz*(Icccc + Icnnc - Iccnc - Icncc +
  14288. dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
  14289. dc*(Icccc + Icncn - Icncc - Icccn)) +
  14290. dz*(Iccnc - Icccc +
  14291. dc*(Icccc + Iccnn - Iccnc - Icccn)) +
  14292. dc*(Icccn - Icccc);
  14293. }
  14294. //! Return pixel value, using linear interpolation and periodic boundary conditions for all X,Y,Z and C-coordinates.
  14295. Tfloat linear_atXYZC_p(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
  14296. if (is_empty())
  14297. throw CImgInstanceException(_cimg_instance
  14298. "linear_atXYZC_p(): Empty instance.",
  14299. cimg_instance);
  14300. return _linear_atXYZC_p(fx,fy,fz,fc);
  14301. }
  14302. Tfloat _linear_atXYZC_p(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
  14303. const float
  14304. nfx = cimg::mod(fx,_width - 0.5f),
  14305. nfy = cimg::mod(fy,_height - 0.5f),
  14306. nfz = cimg::mod(fz,_depth - 0.5f),
  14307. nfc = cimg::mod(fc,_spectrum - 0.5f);
  14308. const unsigned int
  14309. x = (unsigned int)nfx,
  14310. y = (unsigned int)nfy,
  14311. z = (unsigned int)nfz,
  14312. c = (unsigned int)nfc;
  14313. const float
  14314. dx = nfx - x,
  14315. dy = nfy - y,
  14316. dz = nfz - z,
  14317. dc = nfc - c;
  14318. const unsigned int
  14319. nx = cimg::mod(x + 1,_width),
  14320. ny = cimg::mod(y + 1,_height),
  14321. nz = cimg::mod(z + 1,_depth),
  14322. nc = cimg::mod(c + 1,_spectrum);
  14323. const Tfloat
  14324. Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c),
  14325. Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c),
  14326. Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c),
  14327. Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c),
  14328. Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc),
  14329. Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc),
  14330. Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc),
  14331. Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc);
  14332. return Icccc +
  14333. dx*(Inccc - Icccc +
  14334. dy*(Icccc + Inncc - Icncc - Inccc +
  14335. dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
  14336. dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
  14337. Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
  14338. dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
  14339. dz*(Icccc + Incnc - Iccnc - Inccc +
  14340. dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
  14341. dc*(Icccc + Inccn - Inccc - Icccn)) +
  14342. dy*(Icncc - Icccc +
  14343. dz*(Icccc + Icnnc - Iccnc - Icncc +
  14344. dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
  14345. dc*(Icccc + Icncn - Icncc - Icccn)) +
  14346. dz*(Iccnc - Icccc +
  14347. dc*(Icccc + Iccnn - Iccnc - Icccn)) +
  14348. dc*(Icccn - Icccc);
  14349. }
  14350. //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate.
  14351. /**
  14352. Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
  14353. or a specified default value in case of out-of-bounds access along the X-axis.
  14354. The cubic interpolation uses Hermite splines.
  14355. \param fx d X-coordinate of the pixel value (float-valued).
  14356. \param y Y-coordinate of the pixel value.
  14357. \param z Z-coordinate of the pixel value.
  14358. \param c C-coordinate of the pixel value.
  14359. \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds.
  14360. \note
  14361. - Similar to linear_atX(float,int,int,int,const T) const, except that the returned pixel value is
  14362. approximated by a \e cubic interpolation along the X-axis.
  14363. - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued.
  14364. \warning
  14365. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  14366. **/
  14367. Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T& out_value) const {
  14368. const int
  14369. x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2;
  14370. const float
  14371. dx = fx - x;
  14372. const Tfloat
  14373. Ip = (Tfloat)atX(px,y,z,c,out_value), Ic = (Tfloat)atX(x,y,z,c,out_value),
  14374. In = (Tfloat)atX(nx,y,z,c,out_value), Ia = (Tfloat)atX(ax,y,z,c,out_value);
  14375. return Ic + 0.5f*(dx*(-Ip + In) + dx*dx*(2*Ip - 5*Ic + 4*In - Ia) + dx*dx*dx*(-Ip + 3*Ic - 3*In + Ia));
  14376. }
  14377. //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate.
  14378. /**
  14379. Similar to cubic_atX(float,int,int,int,const T) const, except that the return value is clamped to stay in the
  14380. min/max range of the datatype \c T.
  14381. **/
  14382. T cubic_atX_c(const float fx, const int y, const int z, const int c, const T& out_value) const {
  14383. return cimg::type<T>::cut(cubic_atX(fx,y,z,c,out_value));
  14384. }
  14385. //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate.
  14386. /**
  14387. Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
  14388. or the value of the nearest pixel location in the image instance in case of out-of-bounds access
  14389. along the X-axis. The cubic interpolation uses Hermite splines.
  14390. \param fx X-coordinate of the pixel value (float-valued).
  14391. \param y Y-coordinate of the pixel value.
  14392. \param z Z-coordinate of the pixel value.
  14393. \param c C-coordinate of the pixel value.
  14394. \note
  14395. - Similar to cubic_atX(float,int,int,int,const T) const, except that the returned pixel value is
  14396. approximated by a cubic interpolation along the X-axis.
  14397. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14398. \c _cubic_atX(float,int,int,int).
  14399. \warning
  14400. - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
  14401. **/
  14402. Tfloat cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
  14403. if (is_empty())
  14404. throw CImgInstanceException(_cimg_instance
  14405. "cubic_atX(): Empty instance.",
  14406. cimg_instance);
  14407. return _cubic_atX(fx,y,z,c);
  14408. }
  14409. Tfloat _cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
  14410. const float
  14411. nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1);
  14412. const int
  14413. x = (int)nfx;
  14414. const float
  14415. dx = nfx - x;
  14416. const int
  14417. px = x - 1<0?0:x - 1, nx = dx>0?x + 1:x, ax = x + 2>=width()?width() - 1:x + 2;
  14418. const Tfloat
  14419. Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c),
  14420. In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c);
  14421. return Ic + 0.5f*(dx*(-Ip + In) + dx*dx*(2*Ip - 5*Ic + 4*In - Ia) + dx*dx*dx*(-Ip + 3*Ic - 3*In + Ia));
  14422. }
  14423. //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate.
  14424. /**
  14425. Similar to cubic_atX(float,int,int,int) const, except that the return value is clamped to stay in the
  14426. min/max range of the datatype \c T.
  14427. **/
  14428. T cubic_atX_c(const float fx, const int y, const int z, const int c) const {
  14429. return cimg::type<T>::cut(cubic_atX(fx,y,z,c));
  14430. }
  14431. T _cubic_atX_c(const float fx, const int y, const int z, const int c) const {
  14432. return cimg::type<T>::cut(_cubic_atX(fx,y,z,c));
  14433. }
  14434. //! Return pixel value, using cubic interpolation and periodic boundary conditions for the X-coordinate.
  14435. Tfloat cubic_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
  14436. if (is_empty())
  14437. throw CImgInstanceException(_cimg_instance
  14438. "cubic_atX_p(): Empty instance.",
  14439. cimg_instance);
  14440. return _cubic_atX_p(fx,y,z,c);
  14441. }
  14442. Tfloat _cubic_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
  14443. const float
  14444. nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f);
  14445. const int
  14446. x = (int)nfx;
  14447. const float
  14448. dx = nfx - x;
  14449. const int
  14450. px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width());
  14451. const Tfloat
  14452. Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c),
  14453. In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c);
  14454. return Ic + 0.5f*(dx*(-Ip + In) + dx*dx*(2*Ip - 5*Ic + 4*In - Ia) + dx*dx*dx*(-Ip + 3*Ic - 3*In + Ia));
  14455. }
  14456. T cubic_atX_pc(const float fx, const int y, const int z, const int c) const {
  14457. return cimg::type<T>::cut(cubic_atX_p(fx,y,z,c));
  14458. }
  14459. T _cubic_atX_pc(const float fx, const int y, const int z, const int c) const {
  14460. return cimg::type<T>::cut(_cubic_atX_p(fx,y,z,c));
  14461. }
  14462. //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X and Y-coordinates.
  14463. /**
  14464. Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking
  14465. are achieved both for X and Y-coordinates.
  14466. **/
  14467. Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T& out_value) const {
  14468. const int
  14469. x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2,
  14470. y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2;
  14471. const float dx = fx - x, dy = fy - y;
  14472. const Tfloat
  14473. Ipp = (Tfloat)atXY(px,py,z,c,out_value), Icp = (Tfloat)atXY(x,py,z,c,out_value),
  14474. Inp = (Tfloat)atXY(nx,py,z,c,out_value), Iap = (Tfloat)atXY(ax,py,z,c,out_value),
  14475. Ip = Icp + 0.5f*(dx*(-Ipp + Inp) + dx*dx*(2*Ipp - 5*Icp + 4*Inp - Iap) + dx*dx*dx*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14476. Ipc = (Tfloat)atXY(px,y,z,c,out_value), Icc = (Tfloat)atXY(x, y,z,c,out_value),
  14477. Inc = (Tfloat)atXY(nx,y,z,c,out_value), Iac = (Tfloat)atXY(ax,y,z,c,out_value),
  14478. Ic = Icc + 0.5f*(dx*(-Ipc + Inc) + dx*dx*(2*Ipc - 5*Icc + 4*Inc - Iac) + dx*dx*dx*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14479. Ipn = (Tfloat)atXY(px,ny,z,c,out_value), Icn = (Tfloat)atXY(x,ny,z,c,out_value),
  14480. Inn = (Tfloat)atXY(nx,ny,z,c,out_value), Ian = (Tfloat)atXY(ax,ny,z,c,out_value),
  14481. In = Icn + 0.5f*(dx*(-Ipn + Inn) + dx*dx*(2*Ipn - 5*Icn + 4*Inn - Ian) + dx*dx*dx*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14482. Ipa = (Tfloat)atXY(px,ay,z,c,out_value), Ica = (Tfloat)atXY(x,ay,z,c,out_value),
  14483. Ina = (Tfloat)atXY(nx,ay,z,c,out_value), Iaa = (Tfloat)atXY(ax,ay,z,c,out_value),
  14484. Ia = Ica + 0.5f*(dx*(-Ipa + Ina) + dx*dx*(2*Ipa - 5*Ica + 4*Ina - Iaa) + dx*dx*dx*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14485. return Ic + 0.5f*(dy*(-Ip + In) + dy*dy*(2*Ip - 5*Ic + 4*In - Ia) + dy*dy*dy*(-Ip + 3*Ic - 3*In + Ia));
  14486. }
  14487. //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y-coordinates.
  14488. /**
  14489. Similar to cubic_atXY(float,float,int,int,const T) const, except that the return value is clamped to stay in the
  14490. min/max range of the datatype \c T.
  14491. **/
  14492. T cubic_atXY_c(const float fx, const float fy, const int z, const int c, const T& out_value) const {
  14493. return cimg::type<T>::cut(cubic_atXY(fx,fy,z,c,out_value));
  14494. }
  14495. //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X and Y-coordinates.
  14496. /**
  14497. Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
  14498. are achieved for both X and Y-coordinates.
  14499. \note
  14500. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14501. \c _cubic_atXY(float,float,int,int).
  14502. **/
  14503. Tfloat cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
  14504. if (is_empty())
  14505. throw CImgInstanceException(_cimg_instance
  14506. "cubic_atXY(): Empty instance.",
  14507. cimg_instance);
  14508. return _cubic_atXY(fx,fy,z,c);
  14509. }
  14510. Tfloat _cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
  14511. const float
  14512. nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1),
  14513. nfy = cimg::type<float>::is_nan(fy)?0:cimg::cut(fy,0,height() - 1);
  14514. const int x = (int)nfx, y = (int)nfy;
  14515. const float dx = nfx - x, dy = nfy - y;
  14516. const int
  14517. px = x - 1<0?0:x - 1, nx = dx<=0?x:x + 1, ax = x + 2>=width()?width() - 1:x + 2,
  14518. py = y - 1<0?0:y - 1, ny = dy<=0?y:y + 1, ay = y + 2>=height()?height() - 1:y + 2;
  14519. const Tfloat
  14520. Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c),
  14521. Iap = (Tfloat)(*this)(ax,py,z,c),
  14522. Ip = Icp + 0.5f*(dx*(-Ipp + Inp) + dx*dx*(2*Ipp - 5*Icp + 4*Inp - Iap) + dx*dx*dx*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14523. Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
  14524. Iac = (Tfloat)(*this)(ax,y,z,c),
  14525. Ic = Icc + 0.5f*(dx*(-Ipc + Inc) + dx*dx*(2*Ipc - 5*Icc + 4*Inc - Iac) + dx*dx*dx*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14526. Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c),
  14527. Ian = (Tfloat)(*this)(ax,ny,z,c),
  14528. In = Icn + 0.5f*(dx*(-Ipn + Inn) + dx*dx*(2*Ipn - 5*Icn + 4*Inn - Ian) + dx*dx*dx*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14529. Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c),
  14530. Iaa = (Tfloat)(*this)(ax,ay,z,c),
  14531. Ia = Ica + 0.5f*(dx*(-Ipa + Ina) + dx*dx*(2*Ipa - 5*Ica + 4*Ina - Iaa) + dx*dx*dx*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14532. return Ic + 0.5f*(dy*(-Ip + In) + dy*dy*(2*Ip - 5*Ic + 4*In - Ia) + dy*dy*dy*(-Ip + 3*Ic - 3*In + Ia));
  14533. }
  14534. //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y-coordinates.
  14535. /**
  14536. Similar to cubic_atXY(float,float,int,int) const, except that the return value is clamped to stay in the
  14537. min/max range of the datatype \c T.
  14538. **/
  14539. T cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
  14540. return cimg::type<T>::cut(cubic_atXY(fx,fy,z,c));
  14541. }
  14542. T _cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
  14543. return cimg::type<T>::cut(_cubic_atXY(fx,fy,z,c));
  14544. }
  14545. //! Return pixel value, using cubic interpolation and periodic boundary conditions for the X and Y-coordinates.
  14546. Tfloat cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
  14547. if (is_empty())
  14548. throw CImgInstanceException(_cimg_instance
  14549. "cubic_atXY_p(): Empty instance.",
  14550. cimg_instance);
  14551. return _cubic_atXY_p(fx,fy,z,c);
  14552. }
  14553. Tfloat _cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
  14554. const float
  14555. nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f),
  14556. nfy = cimg::type<float>::is_nan(fy)?0:cimg::mod(fy,_height - 0.5f);
  14557. const int x = (int)nfx, y = (int)nfy;
  14558. const float dx = nfx - x, dy = nfy - y;
  14559. const int
  14560. px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width()),
  14561. py = cimg::mod(y - 1,height()), ny = cimg::mod(y + 1,height()), ay = cimg::mod(y + 2,height());
  14562. const Tfloat
  14563. Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c),
  14564. Iap = (Tfloat)(*this)(ax,py,z,c),
  14565. Ip = Icp + 0.5f*(dx*(-Ipp + Inp) + dx*dx*(2*Ipp - 5*Icp + 4*Inp - Iap) + dx*dx*dx*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14566. Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
  14567. Iac = (Tfloat)(*this)(ax,y,z,c),
  14568. Ic = Icc + 0.5f*(dx*(-Ipc + Inc) + dx*dx*(2*Ipc - 5*Icc + 4*Inc - Iac) + dx*dx*dx*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14569. Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c),
  14570. Ian = (Tfloat)(*this)(ax,ny,z,c),
  14571. In = Icn + 0.5f*(dx*(-Ipn + Inn) + dx*dx*(2*Ipn - 5*Icn + 4*Inn - Ian) + dx*dx*dx*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14572. Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c),
  14573. Iaa = (Tfloat)(*this)(ax,ay,z,c),
  14574. Ia = Ica + 0.5f*(dx*(-Ipa + Ina) + dx*dx*(2*Ipa - 5*Ica + 4*Ina - Iaa) + dx*dx*dx*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14575. return Ic + 0.5f*(dy*(-Ip + In) + dy*dy*(2*Ip - 5*Ic + 4*In - Ia) + dy*dy*dy*(-Ip + 3*Ic - 3*In + Ia));
  14576. }
  14577. T cubic_atXY_pc(const float fx, const float fy, const int z, const int c) const {
  14578. return cimg::type<T>::cut(cubic_atXY_p(fx,fy,z,c));
  14579. }
  14580. T _cubic_atXY_pc(const float fx, const float fy, const int z, const int c) const {
  14581. return cimg::type<T>::cut(_cubic_atXY_p(fx,fy,z,c));
  14582. }
  14583. //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates.
  14584. /**
  14585. Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking
  14586. are achieved both for X,Y and Z-coordinates.
  14587. **/
  14588. Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
  14589. const int
  14590. x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2,
  14591. y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2,
  14592. z = (int)fz - (fz>=0?0:1), pz = z - 1, nz = z + 1, az = z + 2;
  14593. const float dx = fx - x, dy = fy - y, dz = fz - z;
  14594. const Tfloat
  14595. Ippp = (Tfloat)atXYZ(px,py,pz,c,out_value), Icpp = (Tfloat)atXYZ(x,py,pz,c,out_value),
  14596. Inpp = (Tfloat)atXYZ(nx,py,pz,c,out_value), Iapp = (Tfloat)atXYZ(ax,py,pz,c,out_value),
  14597. Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
  14598. dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
  14599. Ipcp = (Tfloat)atXYZ(px,y,pz,c,out_value), Iccp = (Tfloat)atXYZ(x, y,pz,c,out_value),
  14600. Incp = (Tfloat)atXYZ(nx,y,pz,c,out_value), Iacp = (Tfloat)atXYZ(ax,y,pz,c,out_value),
  14601. Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
  14602. dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
  14603. Ipnp = (Tfloat)atXYZ(px,ny,pz,c,out_value), Icnp = (Tfloat)atXYZ(x,ny,pz,c,out_value),
  14604. Innp = (Tfloat)atXYZ(nx,ny,pz,c,out_value), Ianp = (Tfloat)atXYZ(ax,ny,pz,c,out_value),
  14605. Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
  14606. dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
  14607. Ipap = (Tfloat)atXYZ(px,ay,pz,c,out_value), Icap = (Tfloat)atXYZ(x,ay,pz,c,out_value),
  14608. Inap = (Tfloat)atXYZ(nx,ay,pz,c,out_value), Iaap = (Tfloat)atXYZ(ax,ay,pz,c,out_value),
  14609. Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
  14610. dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
  14611. Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
  14612. dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14613. Ippc = (Tfloat)atXYZ(px,py,z,c,out_value), Icpc = (Tfloat)atXYZ(x,py,z,c,out_value),
  14614. Inpc = (Tfloat)atXYZ(nx,py,z,c,out_value), Iapc = (Tfloat)atXYZ(ax,py,z,c,out_value),
  14615. Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
  14616. dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
  14617. Ipcc = (Tfloat)atXYZ(px,y,z,c,out_value), Iccc = (Tfloat)atXYZ(x, y,z,c,out_value),
  14618. Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), Iacc = (Tfloat)atXYZ(ax,y,z,c,out_value),
  14619. Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
  14620. dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
  14621. Ipnc = (Tfloat)atXYZ(px,ny,z,c,out_value), Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value),
  14622. Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), Ianc = (Tfloat)atXYZ(ax,ny,z,c,out_value),
  14623. Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
  14624. dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
  14625. Ipac = (Tfloat)atXYZ(px,ay,z,c,out_value), Icac = (Tfloat)atXYZ(x,ay,z,c,out_value),
  14626. Inac = (Tfloat)atXYZ(nx,ay,z,c,out_value), Iaac = (Tfloat)atXYZ(ax,ay,z,c,out_value),
  14627. Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
  14628. dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
  14629. Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
  14630. dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14631. Ippn = (Tfloat)atXYZ(px,py,nz,c,out_value), Icpn = (Tfloat)atXYZ(x,py,nz,c,out_value),
  14632. Inpn = (Tfloat)atXYZ(nx,py,nz,c,out_value), Iapn = (Tfloat)atXYZ(ax,py,nz,c,out_value),
  14633. Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
  14634. dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
  14635. Ipcn = (Tfloat)atXYZ(px,y,nz,c,out_value), Iccn = (Tfloat)atXYZ(x, y,nz,c,out_value),
  14636. Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), Iacn = (Tfloat)atXYZ(ax,y,nz,c,out_value),
  14637. Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
  14638. dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
  14639. Ipnn = (Tfloat)atXYZ(px,ny,nz,c,out_value), Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value),
  14640. Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value), Iann = (Tfloat)atXYZ(ax,ny,nz,c,out_value),
  14641. Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
  14642. dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
  14643. Ipan = (Tfloat)atXYZ(px,ay,nz,c,out_value), Ican = (Tfloat)atXYZ(x,ay,nz,c,out_value),
  14644. Inan = (Tfloat)atXYZ(nx,ay,nz,c,out_value), Iaan = (Tfloat)atXYZ(ax,ay,nz,c,out_value),
  14645. Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
  14646. dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
  14647. In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
  14648. dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14649. Ippa = (Tfloat)atXYZ(px,py,az,c,out_value), Icpa = (Tfloat)atXYZ(x,py,az,c,out_value),
  14650. Inpa = (Tfloat)atXYZ(nx,py,az,c,out_value), Iapa = (Tfloat)atXYZ(ax,py,az,c,out_value),
  14651. Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
  14652. dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
  14653. Ipca = (Tfloat)atXYZ(px,y,az,c,out_value), Icca = (Tfloat)atXYZ(x, y,az,c,out_value),
  14654. Inca = (Tfloat)atXYZ(nx,y,az,c,out_value), Iaca = (Tfloat)atXYZ(ax,y,az,c,out_value),
  14655. Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
  14656. dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
  14657. Ipna = (Tfloat)atXYZ(px,ny,az,c,out_value), Icna = (Tfloat)atXYZ(x,ny,az,c,out_value),
  14658. Inna = (Tfloat)atXYZ(nx,ny,az,c,out_value), Iana = (Tfloat)atXYZ(ax,ny,az,c,out_value),
  14659. Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
  14660. dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
  14661. Ipaa = (Tfloat)atXYZ(px,ay,az,c,out_value), Icaa = (Tfloat)atXYZ(x,ay,az,c,out_value),
  14662. Inaa = (Tfloat)atXYZ(nx,ay,az,c,out_value), Iaaa = (Tfloat)atXYZ(ax,ay,az,c,out_value),
  14663. Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
  14664. dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
  14665. Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
  14666. dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14667. return Ic + 0.5f*(dz*(-Ip + In) + dz*dz*(2*Ip - 5*Ic + 4*In - Ia) + dz*dz*dz*(-Ip + 3*Ic - 3*In + Ia));
  14668. }
  14669. //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the XYZ-coordinates.
  14670. /**
  14671. Similar to cubic_atXYZ(float,float,float,int,const T) const, except that the return value is clamped to stay
  14672. in the min/max range of the datatype \c T.
  14673. **/
  14674. T cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
  14675. return cimg::type<T>::cut(cubic_atXYZ(fx,fy,fz,c,out_value));
  14676. }
  14677. //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
  14678. /**
  14679. Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
  14680. are achieved both for X,Y and Z-coordinates.
  14681. \note
  14682. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14683. \c _cubic_atXYZ(float,float,float,int).
  14684. **/
  14685. Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const {
  14686. if (is_empty())
  14687. throw CImgInstanceException(_cimg_instance
  14688. "cubic_atXYZ(): Empty instance.",
  14689. cimg_instance);
  14690. return _cubic_atXYZ(fx,fy,fz,c);
  14691. }
  14692. Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const {
  14693. const float
  14694. nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1),
  14695. nfy = cimg::type<float>::is_nan(fy)?0:cimg::cut(fy,0,height() - 1),
  14696. nfz = cimg::type<float>::is_nan(fz)?0:cimg::cut(fz,0,depth() - 1);
  14697. const int x = (int)nfx, y = (int)nfy, z = (int)nfz;
  14698. const float dx = nfx - x, dy = nfy - y, dz = nfz - z;
  14699. const int
  14700. px = x - 1<0?0:x - 1, nx = dx>0?x + 1:x, ax = x + 2>=width()?width() - 1:x + 2,
  14701. py = y - 1<0?0:y - 1, ny = dy>0?y + 1:y, ay = y + 2>=height()?height() - 1:y + 2,
  14702. pz = z - 1<0?0:z - 1, nz = dz>0?z + 1:z, az = z + 2>=depth()?depth() - 1:z + 2;
  14703. const Tfloat
  14704. Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c),
  14705. Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c),
  14706. Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
  14707. dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
  14708. Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c),
  14709. Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c),
  14710. Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
  14711. dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
  14712. Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c),
  14713. Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c),
  14714. Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
  14715. dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
  14716. Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c),
  14717. Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c),
  14718. Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
  14719. dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
  14720. Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
  14721. dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14722. Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c),
  14723. Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c),
  14724. Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
  14725. dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
  14726. Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c),
  14727. Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c),
  14728. Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
  14729. dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
  14730. Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c),
  14731. Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c),
  14732. Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
  14733. dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
  14734. Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c),
  14735. Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c),
  14736. Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
  14737. dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
  14738. Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
  14739. dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14740. Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c),
  14741. Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c),
  14742. Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
  14743. dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
  14744. Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c),
  14745. Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c),
  14746. Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
  14747. dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
  14748. Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c),
  14749. Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c),
  14750. Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
  14751. dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
  14752. Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c),
  14753. Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c),
  14754. Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
  14755. dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
  14756. In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
  14757. dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14758. Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c),
  14759. Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c),
  14760. Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
  14761. dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
  14762. Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c),
  14763. Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c),
  14764. Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
  14765. dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
  14766. Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c),
  14767. Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c),
  14768. Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
  14769. dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
  14770. Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c),
  14771. Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c),
  14772. Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
  14773. dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
  14774. Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
  14775. dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14776. return Ic + 0.5f*(dz*(-Ip + In) + dz*dz*(2*Ip - 5*Ic + 4*In - Ia) + dz*dz*dz*(-Ip + 3*Ic - 3*In + Ia));
  14777. }
  14778. //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the XYZ-coordinates.
  14779. /**
  14780. Similar to cubic_atXYZ(float,float,float,int) const, except that the return value is clamped to stay in the
  14781. min/max range of the datatype \c T.
  14782. **/
  14783. T cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c) const {
  14784. return cimg::type<T>::cut(cubic_atXYZ(fx,fy,fz,c));
  14785. }
  14786. T _cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c) const {
  14787. return cimg::type<T>::cut(_cubic_atXYZ(fx,fy,fz,c));
  14788. }
  14789. //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
  14790. /**
  14791. Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
  14792. are achieved both for X,Y and Z-coordinates.
  14793. \note
  14794. - If you know your image instance is \e not empty, you may rather use the slightly faster method
  14795. \c _cubic_atXYZ(float,float,float,int).
  14796. **/
  14797. Tfloat cubic_atXYZ_p(const float fx, const float fy, const float fz, const int c=0) const {
  14798. if (is_empty())
  14799. throw CImgInstanceException(_cimg_instance
  14800. "cubic_atXYZ_p(): Empty instance.",
  14801. cimg_instance);
  14802. return _cubic_atXYZ_p(fx,fy,fz,c);
  14803. }
  14804. Tfloat _cubic_atXYZ_p(const float fx, const float fy, const float fz, const int c=0) const {
  14805. const float
  14806. nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f),
  14807. nfy = cimg::type<float>::is_nan(fy)?0:cimg::mod(fy,_height - 0.5f),
  14808. nfz = cimg::type<float>::is_nan(fz)?0:cimg::mod(fz,_depth - 0.5f);
  14809. const int x = (int)nfx, y = (int)nfy, z = (int)nfz;
  14810. const float dx = nfx - x, dy = nfy - y, dz = nfz - z;
  14811. const int
  14812. px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width()),
  14813. py = cimg::mod(y - 1,height()), ny = cimg::mod(y + 1,height()), ay = cimg::mod(y + 2,height()),
  14814. pz = cimg::mod(z - 1,depth()), nz = cimg::mod(z + 1,depth()), az = cimg::mod(z + 2,depth());
  14815. const Tfloat
  14816. Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c),
  14817. Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c),
  14818. Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
  14819. dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
  14820. Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c),
  14821. Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c),
  14822. Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
  14823. dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
  14824. Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c),
  14825. Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c),
  14826. Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
  14827. dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
  14828. Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c),
  14829. Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c),
  14830. Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
  14831. dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
  14832. Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
  14833. dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
  14834. Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c),
  14835. Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c),
  14836. Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
  14837. dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
  14838. Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c),
  14839. Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c),
  14840. Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
  14841. dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
  14842. Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c),
  14843. Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c),
  14844. Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
  14845. dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
  14846. Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c),
  14847. Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c),
  14848. Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
  14849. dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
  14850. Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
  14851. dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
  14852. Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c),
  14853. Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c),
  14854. Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
  14855. dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
  14856. Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c),
  14857. Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c),
  14858. Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
  14859. dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
  14860. Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c),
  14861. Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c),
  14862. Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
  14863. dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
  14864. Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c),
  14865. Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c),
  14866. Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
  14867. dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
  14868. In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
  14869. dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
  14870. Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c),
  14871. Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c),
  14872. Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
  14873. dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
  14874. Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c),
  14875. Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c),
  14876. Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
  14877. dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
  14878. Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c),
  14879. Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c),
  14880. Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
  14881. dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
  14882. Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c),
  14883. Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c),
  14884. Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
  14885. dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
  14886. Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
  14887. dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
  14888. return Ic + 0.5f*(dz*(-Ip + In) + dz*dz*(2*Ip - 5*Ic + 4*In - Ia) + dz*dz*dz*(-Ip + 3*Ic - 3*In + Ia));
  14889. }
  14890. T cubic_atXYZ_pc(const float fx, const float fy, const float fz, const int c) const {
  14891. return cimg::type<T>::cut(cubic_atXYZ_p(fx,fy,fz,c));
  14892. }
  14893. T _cubic_atXYZ_pc(const float fx, const float fy, const float fz, const int c) const {
  14894. return cimg::type<T>::cut(_cubic_atXYZ_p(fx,fy,fz,c));
  14895. }
  14896. //! Set pixel value, using linear interpolation for the X-coordinates.
  14897. /**
  14898. Set pixel value at specified coordinates (\c fx,\c y,\c z,\c c) in the image instance, in a way that
  14899. the value is spread amongst several neighbors if the pixel coordinates are float-valued.
  14900. \param value Pixel value to set.
  14901. \param fx X-coordinate of the pixel value (float-valued).
  14902. \param y Y-coordinate of the pixel value.
  14903. \param z Z-coordinate of the pixel value.
  14904. \param c C-coordinate of the pixel value.
  14905. \param is_added Tells if the pixel value is added to (\c true), or simply replace (\c false) the current image
  14906. pixel(s).
  14907. \return A reference to the current image instance.
  14908. \note
  14909. - Calling this method with out-of-bounds coordinates does nothing.
  14910. **/
  14911. CImg<T>& set_linear_atX(const T& value, const float fx, const int y=0, const int z=0, const int c=0,
  14912. const bool is_added=false) {
  14913. const int
  14914. x = (int)fx - (fx>=0?0:1), nx = x + 1;
  14915. const float
  14916. dx = fx - x;
  14917. if (y>=0 && y<height() && z>=0 && z<depth() && c>=0 && c<spectrum()) {
  14918. if (x>=0 && x<width()) {
  14919. const float w1 = 1 - dx, w2 = is_added?1:(1 - w1);
  14920. (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
  14921. }
  14922. if (nx>=0 && nx<width()) {
  14923. const float w1 = dx, w2 = is_added?1:(1 - w1);
  14924. (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
  14925. }
  14926. }
  14927. return *this;
  14928. }
  14929. //! Set pixel value, using linear interpolation for the X and Y-coordinates.
  14930. /**
  14931. Similar to set_linear_atX(const T&,float,int,int,int,bool), except that the linear interpolation
  14932. is achieved both for X and Y-coordinates.
  14933. **/
  14934. CImg<T>& set_linear_atXY(const T& value, const float fx, const float fy=0, const int z=0, const int c=0,
  14935. const bool is_added=false) {
  14936. const int
  14937. x = (int)fx - (fx>=0?0:1), nx = x + 1,
  14938. y = (int)fy - (fy>=0?0:1), ny = y + 1;
  14939. const float
  14940. dx = fx - x,
  14941. dy = fy - y;
  14942. if (z>=0 && z<depth() && c>=0 && c<spectrum()) {
  14943. if (y>=0 && y<height()) {
  14944. if (x>=0 && x<width()) {
  14945. const float w1 = (1 - dx)*(1 - dy), w2 = is_added?1:(1 - w1);
  14946. (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
  14947. }
  14948. if (nx>=0 && nx<width()) {
  14949. const float w1 = dx*(1 - dy), w2 = is_added?1:(1 - w1);
  14950. (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
  14951. }
  14952. }
  14953. if (ny>=0 && ny<height()) {
  14954. if (x>=0 && x<width()) {
  14955. const float w1 = (1 - dx)*dy, w2 = is_added?1:(1 - w1);
  14956. (*this)(x,ny,z,c) = (T)(w1*value + w2*(*this)(x,ny,z,c));
  14957. }
  14958. if (nx>=0 && nx<width()) {
  14959. const float w1 = dx*dy, w2 = is_added?1:(1 - w1);
  14960. (*this)(nx,ny,z,c) = (T)(w1*value + w2*(*this)(nx,ny,z,c));
  14961. }
  14962. }
  14963. }
  14964. return *this;
  14965. }
  14966. //! Set pixel value, using linear interpolation for the X,Y and Z-coordinates.
  14967. /**
  14968. Similar to set_linear_atXY(const T&,float,float,int,int,bool), except that the linear interpolation
  14969. is achieved both for X,Y and Z-coordinates.
  14970. **/
  14971. CImg<T>& set_linear_atXYZ(const T& value, const float fx, const float fy=0, const float fz=0, const int c=0,
  14972. const bool is_added=false) {
  14973. const int
  14974. x = (int)fx - (fx>=0?0:1), nx = x + 1,
  14975. y = (int)fy - (fy>=0?0:1), ny = y + 1,
  14976. z = (int)fz - (fz>=0?0:1), nz = z + 1;
  14977. const float
  14978. dx = fx - x,
  14979. dy = fy - y,
  14980. dz = fz - z;
  14981. if (c>=0 && c<spectrum()) {
  14982. if (z>=0 && z<depth()) {
  14983. if (y>=0 && y<height()) {
  14984. if (x>=0 && x<width()) {
  14985. const float w1 = (1 - dx)*(1 - dy)*(1 - dz), w2 = is_added?1:(1 - w1);
  14986. (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
  14987. }
  14988. if (nx>=0 && nx<width()) {
  14989. const float w1 = dx*(1 - dy)*(1 - dz), w2 = is_added?1:(1 - w1);
  14990. (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
  14991. }
  14992. }
  14993. if (ny>=0 && ny<height()) {
  14994. if (x>=0 && x<width()) {
  14995. const float w1 = (1 - dx)*dy*(1 - dz), w2 = is_added?1:(1 - w1);
  14996. (*this)(x,ny,z,c) = (T)(w1*value + w2*(*this)(x,ny,z,c));
  14997. }
  14998. if (nx>=0 && nx<width()) {
  14999. const float w1 = dx*dy*(1 - dz), w2 = is_added?1:(1 - w1);
  15000. (*this)(nx,ny,z,c) = (T)(w1*value + w2*(*this)(nx,ny,z,c));
  15001. }
  15002. }
  15003. }
  15004. if (nz>=0 && nz<depth()) {
  15005. if (y>=0 && y<height()) {
  15006. if (x>=0 && x<width()) {
  15007. const float w1 = (1 - dx)*(1 - dy)*dz, w2 = is_added?1:(1 - w1);
  15008. (*this)(x,y,nz,c) = (T)(w1*value + w2*(*this)(x,y,nz,c));
  15009. }
  15010. if (nx>=0 && nx<width()) {
  15011. const float w1 = dx*(1 - dy)*dz, w2 = is_added?1:(1 - w1);
  15012. (*this)(nx,y,nz,c) = (T)(w1*value + w2*(*this)(nx,y,nz,c));
  15013. }
  15014. }
  15015. if (ny>=0 && ny<height()) {
  15016. if (x>=0 && x<width()) {
  15017. const float w1 = (1 - dx)*dy*dz, w2 = is_added?1:(1 - w1);
  15018. (*this)(x,ny,nz,c) = (T)(w1*value + w2*(*this)(x,ny,nz,c));
  15019. }
  15020. if (nx>=0 && nx<width()) {
  15021. const float w1 = dx*dy*dz, w2 = is_added?1:(1 - w1);
  15022. (*this)(nx,ny,nz,c) = (T)(w1*value + w2*(*this)(nx,ny,nz,c));
  15023. }
  15024. }
  15025. }
  15026. }
  15027. return *this;
  15028. }
  15029. //! Return a C-string containing a list of all values of the image instance.
  15030. /**
  15031. Return a new \c CImg<char> image whose buffer data() is a \c char* string describing the list of all pixel values
  15032. of the image instance (written in base 10), separated by specified \c separator character.
  15033. \param separator A \c char character which specifies the separator between values in the returned C-string.
  15034. \param max_size Maximum size of the returned image (or \c 0 if no limits are set).
  15035. \param format For float/double-values, tell the printf format used to generate the text representation
  15036. of the numbers (or \c 0 for default representation).
  15037. \note
  15038. - The returned image is never empty.
  15039. - For an empty image instance, the returned string is <tt>""</tt>.
  15040. - If \c max_size is equal to \c 0, there are no limits on the size of the returned string.
  15041. - Otherwise, if the maximum number of string characters is exceeded, the value string is cut off
  15042. and terminated by character \c '\0'. In that case, the returned image size is <tt>max_size + 1</tt>.
  15043. **/
  15044. CImg<charT> value_string(const char separator=',', const unsigned int max_size=0,
  15045. const char *const format=0) const {
  15046. if (is_empty() || max_size==1) return CImg<charT>(1,1,1,1,0);
  15047. CImgList<charT> items;
  15048. CImg<charT> s_item(256); *s_item = 0;
  15049. const T *ptrs = _data;
  15050. unsigned int string_size = 0;
  15051. const char *const _format = format?format:cimg::type<T>::format();
  15052. for (ulongT off = 0, siz = size(); off<siz && (!max_size || string_size<max_size); ++off) {
  15053. const unsigned int printed_size = 1U + cimg_snprintf(s_item,s_item._width,_format,
  15054. cimg::type<T>::format(*(ptrs++)));
  15055. CImg<charT> item(s_item._data,printed_size);
  15056. item[printed_size - 1] = separator;
  15057. item.move_to(items);
  15058. if (max_size) string_size+=printed_size;
  15059. }
  15060. CImg<charT> res;
  15061. (items>'x').move_to(res);
  15062. if (max_size && res._width>=max_size) res.crop(0,max_size - 1);
  15063. res.back() = 0;
  15064. return res;
  15065. }
  15066. //@}
  15067. //-------------------------------------
  15068. //
  15069. //! \name Instance Checking
  15070. //@{
  15071. //-------------------------------------
  15072. //! Test shared state of the pixel buffer.
  15073. /**
  15074. Return \c true if image instance has a shared memory buffer, and \c false otherwise.
  15075. \note
  15076. - A shared image do not own his pixel buffer data() and will not deallocate it on destruction.
  15077. - Most of the time, a \c CImg<T> image instance will \e not be shared.
  15078. - A shared image can only be obtained by a limited set of constructors and methods (see list below).
  15079. **/
  15080. bool is_shared() const {
  15081. return _is_shared;
  15082. }
  15083. //! Test if image instance is empty.
  15084. /**
  15085. Return \c true, if image instance is empty, i.e. does \e not contain any pixel values, has dimensions
  15086. \c 0 x \c 0 x \c 0 x \c 0 and a pixel buffer pointer set to \c 0 (null pointer), and \c false otherwise.
  15087. **/
  15088. bool is_empty() const {
  15089. return !(_data && _width && _height && _depth && _spectrum);
  15090. }
  15091. //! Test if image instance contains a 'inf' value.
  15092. /**
  15093. Return \c true, if image instance contains a 'inf' value, and \c false otherwise.
  15094. **/
  15095. bool is_inf() const {
  15096. if (cimg::type<T>::is_float()) cimg_for(*this,p,T) if (cimg::type<T>::is_inf((float)*p)) return true;
  15097. return false;
  15098. }
  15099. //! Test if image instance contains a NaN value.
  15100. /**
  15101. Return \c true, if image instance contains a NaN value, and \c false otherwise.
  15102. **/
  15103. bool is_nan() const {
  15104. if (cimg::type<T>::is_float()) cimg_for(*this,p,T) if (cimg::type<T>::is_nan((float)*p)) return true;
  15105. return false;
  15106. }
  15107. //! Test if image width is equal to specified value.
  15108. bool is_sameX(const unsigned int size_x) const {
  15109. return _width==size_x;
  15110. }
  15111. //! Test if image width is equal to specified value.
  15112. template<typename t>
  15113. bool is_sameX(const CImg<t>& img) const {
  15114. return is_sameX(img._width);
  15115. }
  15116. //! Test if image width is equal to specified value.
  15117. bool is_sameX(const CImgDisplay& disp) const {
  15118. return is_sameX(disp._width);
  15119. }
  15120. //! Test if image height is equal to specified value.
  15121. bool is_sameY(const unsigned int size_y) const {
  15122. return _height==size_y;
  15123. }
  15124. //! Test if image height is equal to specified value.
  15125. template<typename t>
  15126. bool is_sameY(const CImg<t>& img) const {
  15127. return is_sameY(img._height);
  15128. }
  15129. //! Test if image height is equal to specified value.
  15130. bool is_sameY(const CImgDisplay& disp) const {
  15131. return is_sameY(disp._height);
  15132. }
  15133. //! Test if image depth is equal to specified value.
  15134. bool is_sameZ(const unsigned int size_z) const {
  15135. return _depth==size_z;
  15136. }
  15137. //! Test if image depth is equal to specified value.
  15138. template<typename t>
  15139. bool is_sameZ(const CImg<t>& img) const {
  15140. return is_sameZ(img._depth);
  15141. }
  15142. //! Test if image spectrum is equal to specified value.
  15143. bool is_sameC(const unsigned int size_c) const {
  15144. return _spectrum==size_c;
  15145. }
  15146. //! Test if image spectrum is equal to specified value.
  15147. template<typename t>
  15148. bool is_sameC(const CImg<t>& img) const {
  15149. return is_sameC(img._spectrum);
  15150. }
  15151. //! Test if image width and height are equal to specified values.
  15152. /**
  15153. Test if is_sameX(unsigned int) const and is_sameY(unsigned int) const are both verified.
  15154. **/
  15155. bool is_sameXY(const unsigned int size_x, const unsigned int size_y) const {
  15156. return _width==size_x && _height==size_y;
  15157. }
  15158. //! Test if image width and height are the same as that of another image.
  15159. /**
  15160. Test if is_sameX(const CImg<t>&) const and is_sameY(const CImg<t>&) const are both verified.
  15161. **/
  15162. template<typename t>
  15163. bool is_sameXY(const CImg<t>& img) const {
  15164. return is_sameXY(img._width,img._height);
  15165. }
  15166. //! Test if image width and height are the same as that of an existing display window.
  15167. /**
  15168. Test if is_sameX(const CImgDisplay&) const and is_sameY(const CImgDisplay&) const are both verified.
  15169. **/
  15170. bool is_sameXY(const CImgDisplay& disp) const {
  15171. return is_sameXY(disp._width,disp._height);
  15172. }
  15173. //! Test if image width and depth are equal to specified values.
  15174. /**
  15175. Test if is_sameX(unsigned int) const and is_sameZ(unsigned int) const are both verified.
  15176. **/
  15177. bool is_sameXZ(const unsigned int size_x, const unsigned int size_z) const {
  15178. return _width==size_x && _depth==size_z;
  15179. }
  15180. //! Test if image width and depth are the same as that of another image.
  15181. /**
  15182. Test if is_sameX(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
  15183. **/
  15184. template<typename t>
  15185. bool is_sameXZ(const CImg<t>& img) const {
  15186. return is_sameXZ(img._width,img._depth);
  15187. }
  15188. //! Test if image width and spectrum are equal to specified values.
  15189. /**
  15190. Test if is_sameX(unsigned int) const and is_sameC(unsigned int) const are both verified.
  15191. **/
  15192. bool is_sameXC(const unsigned int size_x, const unsigned int size_c) const {
  15193. return _width==size_x && _spectrum==size_c;
  15194. }
  15195. //! Test if image width and spectrum are the same as that of another image.
  15196. /**
  15197. Test if is_sameX(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15198. **/
  15199. template<typename t>
  15200. bool is_sameXC(const CImg<t>& img) const {
  15201. return is_sameXC(img._width,img._spectrum);
  15202. }
  15203. //! Test if image height and depth are equal to specified values.
  15204. /**
  15205. Test if is_sameY(unsigned int) const and is_sameZ(unsigned int) const are both verified.
  15206. **/
  15207. bool is_sameYZ(const unsigned int size_y, const unsigned int size_z) const {
  15208. return _height==size_y && _depth==size_z;
  15209. }
  15210. //! Test if image height and depth are the same as that of another image.
  15211. /**
  15212. Test if is_sameY(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
  15213. **/
  15214. template<typename t>
  15215. bool is_sameYZ(const CImg<t>& img) const {
  15216. return is_sameYZ(img._height,img._depth);
  15217. }
  15218. //! Test if image height and spectrum are equal to specified values.
  15219. /**
  15220. Test if is_sameY(unsigned int) const and is_sameC(unsigned int) const are both verified.
  15221. **/
  15222. bool is_sameYC(const unsigned int size_y, const unsigned int size_c) const {
  15223. return _height==size_y && _spectrum==size_c;
  15224. }
  15225. //! Test if image height and spectrum are the same as that of another image.
  15226. /**
  15227. Test if is_sameY(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15228. **/
  15229. template<typename t>
  15230. bool is_sameYC(const CImg<t>& img) const {
  15231. return is_sameYC(img._height,img._spectrum);
  15232. }
  15233. //! Test if image depth and spectrum are equal to specified values.
  15234. /**
  15235. Test if is_sameZ(unsigned int) const and is_sameC(unsigned int) const are both verified.
  15236. **/
  15237. bool is_sameZC(const unsigned int size_z, const unsigned int size_c) const {
  15238. return _depth==size_z && _spectrum==size_c;
  15239. }
  15240. //! Test if image depth and spectrum are the same as that of another image.
  15241. /**
  15242. Test if is_sameZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15243. **/
  15244. template<typename t>
  15245. bool is_sameZC(const CImg<t>& img) const {
  15246. return is_sameZC(img._depth,img._spectrum);
  15247. }
  15248. //! Test if image width, height and depth are equal to specified values.
  15249. /**
  15250. Test if is_sameXY(unsigned int,unsigned int) const and is_sameZ(unsigned int) const are both verified.
  15251. **/
  15252. bool is_sameXYZ(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z) const {
  15253. return is_sameXY(size_x,size_y) && _depth==size_z;
  15254. }
  15255. //! Test if image width, height and depth are the same as that of another image.
  15256. /**
  15257. Test if is_sameXY(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
  15258. **/
  15259. template<typename t>
  15260. bool is_sameXYZ(const CImg<t>& img) const {
  15261. return is_sameXYZ(img._width,img._height,img._depth);
  15262. }
  15263. //! Test if image width, height and spectrum are equal to specified values.
  15264. /**
  15265. Test if is_sameXY(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
  15266. **/
  15267. bool is_sameXYC(const unsigned int size_x, const unsigned int size_y, const unsigned int size_c) const {
  15268. return is_sameXY(size_x,size_y) && _spectrum==size_c;
  15269. }
  15270. //! Test if image width, height and spectrum are the same as that of another image.
  15271. /**
  15272. Test if is_sameXY(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15273. **/
  15274. template<typename t>
  15275. bool is_sameXYC(const CImg<t>& img) const {
  15276. return is_sameXYC(img._width,img._height,img._spectrum);
  15277. }
  15278. //! Test if image width, depth and spectrum are equal to specified values.
  15279. /**
  15280. Test if is_sameXZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
  15281. **/
  15282. bool is_sameXZC(const unsigned int size_x, const unsigned int size_z, const unsigned int size_c) const {
  15283. return is_sameXZ(size_x,size_z) && _spectrum==size_c;
  15284. }
  15285. //! Test if image width, depth and spectrum are the same as that of another image.
  15286. /**
  15287. Test if is_sameXZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15288. **/
  15289. template<typename t>
  15290. bool is_sameXZC(const CImg<t>& img) const {
  15291. return is_sameXZC(img._width,img._depth,img._spectrum);
  15292. }
  15293. //! Test if image height, depth and spectrum are equal to specified values.
  15294. /**
  15295. Test if is_sameYZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
  15296. **/
  15297. bool is_sameYZC(const unsigned int size_y, const unsigned int size_z, const unsigned int size_c) const {
  15298. return is_sameYZ(size_y,size_z) && _spectrum==size_c;
  15299. }
  15300. //! Test if image height, depth and spectrum are the same as that of another image.
  15301. /**
  15302. Test if is_sameYZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15303. **/
  15304. template<typename t>
  15305. bool is_sameYZC(const CImg<t>& img) const {
  15306. return is_sameYZC(img._height,img._depth,img._spectrum);
  15307. }
  15308. //! Test if image width, height, depth and spectrum are equal to specified values.
  15309. /**
  15310. Test if is_sameXYZ(unsigned int,unsigned int,unsigned int) const and is_sameC(unsigned int) const are both
  15311. verified.
  15312. **/
  15313. bool is_sameXYZC(const unsigned int size_x, const unsigned int size_y,
  15314. const unsigned int size_z, const unsigned int size_c) const {
  15315. return is_sameXYZ(size_x,size_y,size_z) && _spectrum==size_c;
  15316. }
  15317. //! Test if image width, height, depth and spectrum are the same as that of another image.
  15318. /**
  15319. Test if is_sameXYZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
  15320. **/
  15321. template<typename t>
  15322. bool is_sameXYZC(const CImg<t>& img) const {
  15323. return is_sameXYZC(img._width,img._height,img._depth,img._spectrum);
  15324. }
  15325. //! Test if specified coordinates are inside image bounds.
  15326. /**
  15327. Return \c true if pixel located at (\c x,\c y,\c z,\c c) is inside bounds of the image instance,
  15328. and \c false otherwise.
  15329. \param x X-coordinate of the pixel value.
  15330. \param y Y-coordinate of the pixel value.
  15331. \param z Z-coordinate of the pixel value.
  15332. \param c C-coordinate of the pixel value.
  15333. \note
  15334. - Return \c true only if all these conditions are verified:
  15335. - The image instance is \e not empty.
  15336. - <tt>0<=x<=\ref width() - 1</tt>.
  15337. - <tt>0<=y<=\ref height() - 1</tt>.
  15338. - <tt>0<=z<=\ref depth() - 1</tt>.
  15339. - <tt>0<=c<=\ref spectrum() - 1</tt>.
  15340. **/
  15341. bool containsXYZC(const int x, const int y=0, const int z=0, const int c=0) const {
  15342. return !is_empty() && x>=0 && x<width() && y>=0 && y<height() && z>=0 && z<depth() && c>=0 && c<spectrum();
  15343. }
  15344. //! Test if pixel value is inside image bounds and get its X,Y,Z and C-coordinates.
  15345. /**
  15346. Return \c true, if specified reference refers to a pixel value inside bounds of the image instance,
  15347. and \c false otherwise.
  15348. \param pixel Reference to pixel value to test.
  15349. \param[out] x X-coordinate of the pixel value, if test succeeds.
  15350. \param[out] y Y-coordinate of the pixel value, if test succeeds.
  15351. \param[out] z Z-coordinate of the pixel value, if test succeeds.
  15352. \param[out] c C-coordinate of the pixel value, if test succeeds.
  15353. \note
  15354. - Useful to convert an offset to a buffer value into pixel value coordinates:
  15355. \code
  15356. const CImg<float> img(100,100,1,3); // Construct a 100x100 RGB color image
  15357. const unsigned long offset = 1249; // Offset to the pixel (49,12,0,0)
  15358. unsigned int x,y,z,c;
  15359. if (img.contains(img[offset],x,y,z,c)) { // Convert offset to (x,y,z,c) coordinates
  15360. std::printf("Offset %u refers to pixel located at (%u,%u,%u,%u).\n",
  15361. offset,x,y,z,c);
  15362. }
  15363. \endcode
  15364. **/
  15365. template<typename t>
  15366. bool contains(const T& pixel, t& x, t& y, t& z, t& c) const {
  15367. const ulongT wh = (ulongT)_width*_height, whd = wh*_depth, siz = whd*_spectrum;
  15368. const T *const ppixel = &pixel;
  15369. if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
  15370. ulongT off = (ulongT)(ppixel - _data);
  15371. const ulongT nc = off/whd;
  15372. off%=whd;
  15373. const ulongT nz = off/wh;
  15374. off%=wh;
  15375. const ulongT ny = off/_width, nx = off%_width;
  15376. x = (t)nx; y = (t)ny; z = (t)nz; c = (t)nc;
  15377. return true;
  15378. }
  15379. //! Test if pixel value is inside image bounds and get its X,Y and Z-coordinates.
  15380. /**
  15381. Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X,Y and Z-coordinates are set.
  15382. **/
  15383. template<typename t>
  15384. bool contains(const T& pixel, t& x, t& y, t& z) const {
  15385. const ulongT wh = (ulongT)_width*_height, whd = wh*_depth, siz = whd*_spectrum;
  15386. const T *const ppixel = &pixel;
  15387. if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
  15388. ulongT off = ((ulongT)(ppixel - _data))%whd;
  15389. const ulongT nz = off/wh;
  15390. off%=wh;
  15391. const ulongT ny = off/_width, nx = off%_width;
  15392. x = (t)nx; y = (t)ny; z = (t)nz;
  15393. return true;
  15394. }
  15395. //! Test if pixel value is inside image bounds and get its X and Y-coordinates.
  15396. /**
  15397. Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X and Y-coordinates are set.
  15398. **/
  15399. template<typename t>
  15400. bool contains(const T& pixel, t& x, t& y) const {
  15401. const ulongT wh = (ulongT)_width*_height, siz = wh*_depth*_spectrum;
  15402. const T *const ppixel = &pixel;
  15403. if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
  15404. ulongT off = ((unsigned int)(ppixel - _data))%wh;
  15405. const ulongT ny = off/_width, nx = off%_width;
  15406. x = (t)nx; y = (t)ny;
  15407. return true;
  15408. }
  15409. //! Test if pixel value is inside image bounds and get its X-coordinate.
  15410. /**
  15411. Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X-coordinate is set.
  15412. **/
  15413. template<typename t>
  15414. bool contains(const T& pixel, t& x) const {
  15415. const T *const ppixel = &pixel;
  15416. if (is_empty() || ppixel<_data || ppixel>=_data + size()) return false;
  15417. x = (t)(((ulongT)(ppixel - _data))%_width);
  15418. return true;
  15419. }
  15420. //! Test if pixel value is inside image bounds.
  15421. /**
  15422. Similar to contains(const T&,t&,t&,t&,t&) const, except that no pixel coordinates are set.
  15423. **/
  15424. bool contains(const T& pixel) const {
  15425. const T *const ppixel = &pixel;
  15426. return !is_empty() && ppixel>=_data && ppixel<_data + size();
  15427. }
  15428. //! Test if pixel buffers of instance and input images overlap.
  15429. /**
  15430. Return \c true, if pixel buffers attached to image instance and input image \c img overlap,
  15431. and \c false otherwise.
  15432. \param img Input image to compare with.
  15433. \note
  15434. - Buffer overlapping may happen when manipulating \e shared images.
  15435. - If two image buffers overlap, operating on one of the image will probably modify the other one.
  15436. - Most of the time, \c CImg<T> instances are \e non-shared and do not overlap between each others.
  15437. \par Example
  15438. \code
  15439. const CImg<float>
  15440. img1("reference.jpg"), // Load RGB-color image
  15441. img2 = img1.get_shared_channel(1); // Get shared version of the green channel
  15442. if (img1.is_overlapped(img2)) { // Test succeeds, 'img1' and 'img2' overlaps
  15443. std::printf("Buffers overlap!\n");
  15444. }
  15445. \endcode
  15446. **/
  15447. template<typename t>
  15448. bool is_overlapped(const CImg<t>& img) const {
  15449. const ulongT csiz = size(), isiz = img.size();
  15450. return !((void*)(_data + csiz)<=(void*)img._data || (void*)_data>=(void*)(img._data + isiz));
  15451. }
  15452. //! Test if the set {\c *this,\c primitives,\c colors,\c opacities} defines a valid 3D object.
  15453. /**
  15454. Return \c true is the 3D object represented by the set {\c *this,\c primitives,\c colors,\c opacities} defines a
  15455. valid 3D object, and \c false otherwise. The vertex coordinates are defined by the instance image.
  15456. \param primitives List of primitives of the 3D object.
  15457. \param colors List of colors of the 3D object.
  15458. \param opacities List (or image) of opacities of the 3D object.
  15459. \param full_check Tells if full checking of the 3D object must be performed.
  15460. \param[out] error_message C-string to contain the error message, if the test does not succeed.
  15461. \note
  15462. - Set \c full_checking to \c false to speed-up the 3D object checking. In this case, only the size of
  15463. each 3D object component is checked.
  15464. - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message.
  15465. **/
  15466. template<typename tp, typename tc, typename to>
  15467. bool is_object3d(const CImgList<tp>& primitives,
  15468. const CImgList<tc>& colors,
  15469. const to& opacities,
  15470. const bool full_check=true,
  15471. char *const error_message=0) const {
  15472. if (error_message) *error_message = 0;
  15473. // Check consistency for the particular case of an empty 3D object.
  15474. if (is_empty()) {
  15475. if (primitives || colors || opacities) {
  15476. if (error_message) cimg_sprintf(error_message,
  15477. "3D object (%u,%u) defines no vertices but %u primitives, "
  15478. "%u colors and %lu opacities",
  15479. _width,primitives._width,primitives._width,
  15480. colors._width,(unsigned long)opacities.size());
  15481. return false;
  15482. }
  15483. return true;
  15484. }
  15485. // Check consistency of vertices.
  15486. if (_height!=3 || _depth>1 || _spectrum>1) { // Check vertices dimensions
  15487. if (error_message) cimg_sprintf(error_message,
  15488. "3D object (%u,%u) has invalid vertex dimensions (%u,%u,%u,%u)",
  15489. _width,primitives._width,_width,_height,_depth,_spectrum);
  15490. return false;
  15491. }
  15492. if (colors._width>primitives._width + 1) {
  15493. if (error_message) cimg_sprintf(error_message,
  15494. "3D object (%u,%u) defines %u colors",
  15495. _width,primitives._width,colors._width);
  15496. return false;
  15497. }
  15498. if (opacities.size()>primitives._width) {
  15499. if (error_message) cimg_sprintf(error_message,
  15500. "3D object (%u,%u) defines %lu opacities",
  15501. _width,primitives._width,(unsigned long)opacities.size());
  15502. return false;
  15503. }
  15504. if (!full_check) return true;
  15505. // Check consistency of primitives.
  15506. cimglist_for(primitives,l) {
  15507. const CImg<tp>& primitive = primitives[l];
  15508. const unsigned int psiz = (unsigned int)primitive.size();
  15509. switch (psiz) {
  15510. case 1 : { // Point
  15511. const unsigned int i0 = (unsigned int)primitive(0);
  15512. if (i0>=_width) {
  15513. if (error_message) cimg_sprintf(error_message,
  15514. "3D object (%u,%u) refers to invalid vertex index %u in "
  15515. "point primitive [%u]",
  15516. _width,primitives._width,i0,l);
  15517. return false;
  15518. }
  15519. } break;
  15520. case 5 : { // Sphere
  15521. const unsigned int
  15522. i0 = (unsigned int)primitive(0),
  15523. i1 = (unsigned int)primitive(1);
  15524. if (i0>=_width || i1>=_width) {
  15525. if (error_message) cimg_sprintf(error_message,
  15526. "3D object (%u,%u) refers to invalid vertex indices (%u,%u) in "
  15527. "sphere primitive [%u]",
  15528. _width,primitives._width,i0,i1,l);
  15529. return false;
  15530. }
  15531. } break;
  15532. case 2 : case 6 : { // Segment
  15533. const unsigned int
  15534. i0 = (unsigned int)primitive(0),
  15535. i1 = (unsigned int)primitive(1);
  15536. if (i0>=_width || i1>=_width) {
  15537. if (error_message) cimg_sprintf(error_message,
  15538. "3D object (%u,%u) refers to invalid vertex indices (%u,%u) in "
  15539. "segment primitive [%u]",
  15540. _width,primitives._width,i0,i1,l);
  15541. return false;
  15542. }
  15543. } break;
  15544. case 3 : case 9 : { // Triangle
  15545. const unsigned int
  15546. i0 = (unsigned int)primitive(0),
  15547. i1 = (unsigned int)primitive(1),
  15548. i2 = (unsigned int)primitive(2);
  15549. if (i0>=_width || i1>=_width || i2>=_width) {
  15550. if (error_message) cimg_sprintf(error_message,
  15551. "3D object (%u,%u) refers to invalid vertex indices (%u,%u,%u) in "
  15552. "triangle primitive [%u]",
  15553. _width,primitives._width,i0,i1,i2,l);
  15554. return false;
  15555. }
  15556. } break;
  15557. case 4 : case 12 : { // Quadrangle
  15558. const unsigned int
  15559. i0 = (unsigned int)primitive(0),
  15560. i1 = (unsigned int)primitive(1),
  15561. i2 = (unsigned int)primitive(2),
  15562. i3 = (unsigned int)primitive(3);
  15563. if (i0>=_width || i1>=_width || i2>=_width || i3>=_width) {
  15564. if (error_message) cimg_sprintf(error_message,
  15565. "3D object (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in "
  15566. "quadrangle primitive [%u]",
  15567. _width,primitives._width,i0,i1,i2,i3,l);
  15568. return false;
  15569. }
  15570. } break;
  15571. default :
  15572. if (error_message) cimg_sprintf(error_message,
  15573. "3D object (%u,%u) defines an invalid primitive [%u] of size %u",
  15574. _width,primitives._width,l,(unsigned int)psiz);
  15575. return false;
  15576. }
  15577. }
  15578. // Check consistency of colors.
  15579. cimglist_for(colors,c) {
  15580. const CImg<tc>& color = colors[c];
  15581. if (!color) {
  15582. if (error_message) cimg_sprintf(error_message,
  15583. "3D object (%u,%u) defines no color for primitive [%u]",
  15584. _width,primitives._width,c);
  15585. return false;
  15586. }
  15587. }
  15588. // Check consistency of light texture.
  15589. if (colors._width>primitives._width) {
  15590. const CImg<tc> &light = colors.back();
  15591. if (!light || light._depth>1) {
  15592. if (error_message) cimg_sprintf(error_message,
  15593. "3D object (%u,%u) defines an invalid light texture (%u,%u,%u,%u)",
  15594. _width,primitives._width,light._width,
  15595. light._height,light._depth,light._spectrum);
  15596. return false;
  15597. }
  15598. }
  15599. return true;
  15600. }
  15601. //! Test if image instance represents a valid serialization of a 3D object.
  15602. /**
  15603. Return \c true if the image instance represents a valid serialization of a 3D object, and \c false otherwise.
  15604. \param full_check Tells if full checking of the instance must be performed.
  15605. \param[out] error_message C-string to contain the error message, if the test does not succeed.
  15606. \note
  15607. - Set \c full_check to \c false to speed-up the 3D object checking. In this case, only the size of
  15608. each 3D object component is checked.
  15609. - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message.
  15610. **/
  15611. bool is_CImg3d(const bool full_check=true, char *const error_message=0) const {
  15612. if (error_message) *error_message = 0;
  15613. // Check instance dimension and header.
  15614. if (_width!=1 || _height<8 || _depth!=1 || _spectrum!=1) {
  15615. if (error_message) cimg_sprintf(error_message,
  15616. "CImg3d has invalid dimensions (%u,%u,%u,%u)",
  15617. _width,_height,_depth,_spectrum);
  15618. return false;
  15619. }
  15620. const T *ptrs = _data, *const ptre = end();
  15621. if (!_is_CImg3d(*(ptrs++),'C') || !_is_CImg3d(*(ptrs++),'I') || !_is_CImg3d(*(ptrs++),'m') ||
  15622. !_is_CImg3d(*(ptrs++),'g') || !_is_CImg3d(*(ptrs++),'3') || !_is_CImg3d(*(ptrs++),'d')) {
  15623. if (error_message) cimg_sprintf(error_message,
  15624. "CImg3d header not found");
  15625. return false;
  15626. }
  15627. const unsigned int
  15628. nb_points = cimg::float2uint((float)*(ptrs++)),
  15629. nb_primitives = cimg::float2uint((float)*(ptrs++));
  15630. // Check consistency of number of vertices / primitives.
  15631. if (!full_check) {
  15632. const ulongT minimal_size = 8UL + 3*nb_points + 6*nb_primitives;
  15633. if (_data + minimal_size>ptre) {
  15634. if (error_message) cimg_sprintf(error_message,
  15635. "CImg3d (%u,%u) has only %lu values, while at least %lu values were expected",
  15636. nb_points,nb_primitives,(unsigned long)size(),(unsigned long)minimal_size);
  15637. return false;
  15638. }
  15639. }
  15640. // Check consistency of vertex data.
  15641. if (!nb_points) {
  15642. if (nb_primitives) {
  15643. if (error_message) cimg_sprintf(error_message,
  15644. "CImg3d (%u,%u) defines no vertices but %u primitives",
  15645. nb_points,nb_primitives,nb_primitives);
  15646. return false;
  15647. }
  15648. if (ptrs!=ptre) {
  15649. if (error_message) cimg_sprintf(error_message,
  15650. "CImg3d (%u,%u) is an empty object but contains %u value%s "
  15651. "more than expected",
  15652. nb_points,nb_primitives,(unsigned int)(ptre - ptrs),(ptre - ptrs)>1?"s":"");
  15653. return false;
  15654. }
  15655. return true;
  15656. }
  15657. if (ptrs + 3*nb_points>ptre) {
  15658. if (error_message) cimg_sprintf(error_message,
  15659. "CImg3d (%u,%u) defines only %u vertices data",
  15660. nb_points,nb_primitives,(unsigned int)(ptre - ptrs)/3);
  15661. return false;
  15662. }
  15663. ptrs+=3*nb_points;
  15664. // Check consistency of primitive data.
  15665. if (ptrs==ptre) {
  15666. if (error_message) cimg_sprintf(error_message,
  15667. "CImg3d (%u,%u) defines %u vertices but no primitive",
  15668. nb_points,nb_primitives,nb_points);
  15669. return false;
  15670. }
  15671. if (!full_check) return true;
  15672. for (unsigned int p = 0; p<nb_primitives; ++p) {
  15673. const unsigned int nb_inds = (unsigned int)*(ptrs++);
  15674. switch (nb_inds) {
  15675. case 1 : { // Point
  15676. const unsigned int i0 = cimg::float2uint((float)*(ptrs++));
  15677. if (i0>=nb_points) {
  15678. if (error_message) cimg_sprintf(error_message,
  15679. "CImg3d (%u,%u) refers to invalid vertex index %u in point primitive [%u]",
  15680. nb_points,nb_primitives,i0,p);
  15681. return false;
  15682. }
  15683. } break;
  15684. case 5 : { // Sphere
  15685. const unsigned int
  15686. i0 = cimg::float2uint((float)*(ptrs++)),
  15687. i1 = cimg::float2uint((float)*(ptrs++));
  15688. ptrs+=3;
  15689. if (i0>=nb_points || i1>=nb_points) {
  15690. if (error_message) cimg_sprintf(error_message,
  15691. "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in "
  15692. "sphere primitive [%u]",
  15693. nb_points,nb_primitives,i0,i1,p);
  15694. return false;
  15695. }
  15696. } break;
  15697. case 2 : case 6 : { // Segment
  15698. const unsigned int
  15699. i0 = cimg::float2uint((float)*(ptrs++)),
  15700. i1 = cimg::float2uint((float)*(ptrs++));
  15701. if (nb_inds==6) ptrs+=4;
  15702. if (i0>=nb_points || i1>=nb_points) {
  15703. if (error_message) cimg_sprintf(error_message,
  15704. "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in "
  15705. "segment primitive [%u]",
  15706. nb_points,nb_primitives,i0,i1,p);
  15707. return false;
  15708. }
  15709. } break;
  15710. case 3 : case 9 : { // Triangle
  15711. const unsigned int
  15712. i0 = cimg::float2uint((float)*(ptrs++)),
  15713. i1 = cimg::float2uint((float)*(ptrs++)),
  15714. i2 = cimg::float2uint((float)*(ptrs++));
  15715. if (nb_inds==9) ptrs+=6;
  15716. if (i0>=nb_points || i1>=nb_points || i2>=nb_points) {
  15717. if (error_message) cimg_sprintf(error_message,
  15718. "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u) in "
  15719. "triangle primitive [%u]",
  15720. nb_points,nb_primitives,i0,i1,i2,p);
  15721. return false;
  15722. }
  15723. } break;
  15724. case 4 : case 12 : { // Quadrangle
  15725. const unsigned int
  15726. i0 = cimg::float2uint((float)*(ptrs++)),
  15727. i1 = cimg::float2uint((float)*(ptrs++)),
  15728. i2 = cimg::float2uint((float)*(ptrs++)),
  15729. i3 = cimg::float2uint((float)*(ptrs++));
  15730. if (nb_inds==12) ptrs+=8;
  15731. if (i0>=nb_points || i1>=nb_points || i2>=nb_points || i3>=nb_points) {
  15732. if (error_message) cimg_sprintf(error_message,
  15733. "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in "
  15734. "quadrangle primitive [%u]",
  15735. nb_points,nb_primitives,i0,i1,i2,i3,p);
  15736. return false;
  15737. }
  15738. } break;
  15739. default :
  15740. if (error_message) cimg_sprintf(error_message,
  15741. "CImg3d (%u,%u) defines an invalid primitive [%u] of size %u",
  15742. nb_points,nb_primitives,p,nb_inds);
  15743. return false;
  15744. }
  15745. if (ptrs>ptre) {
  15746. if (error_message) cimg_sprintf(error_message,
  15747. "CImg3d (%u,%u) has incomplete primitive data for primitive [%u], "
  15748. "%u values missing",
  15749. nb_points,nb_primitives,p,(unsigned int)(ptrs - ptre));
  15750. return false;
  15751. }
  15752. }
  15753. // Check consistency of color data.
  15754. if (ptrs==ptre) {
  15755. if (error_message) cimg_sprintf(error_message,
  15756. "CImg3d (%u,%u) defines no color/texture data",
  15757. nb_points,nb_primitives);
  15758. return false;
  15759. }
  15760. for (unsigned int c = 0; c<nb_primitives; ++c) {
  15761. if (*(ptrs++)!=(T)-128) ptrs+=2;
  15762. else if ((ptrs+=3)<ptre) {
  15763. const unsigned int
  15764. w = (unsigned int)*(ptrs - 3),
  15765. h = (unsigned int)*(ptrs - 2),
  15766. s = (unsigned int)*(ptrs - 1);
  15767. if (!h && !s) {
  15768. if (w>=c) {
  15769. if (error_message) cimg_sprintf(error_message,
  15770. "CImg3d (%u,%u) refers to invalid shared sprite/texture index %u "
  15771. "for primitive [%u]",
  15772. nb_points,nb_primitives,w,c);
  15773. return false;
  15774. }
  15775. } else ptrs+=w*h*s;
  15776. }
  15777. if (ptrs>ptre) {
  15778. if (error_message) cimg_sprintf(error_message,
  15779. "CImg3d (%u,%u) has incomplete color/texture data for primitive [%u], "
  15780. "%u values missing",
  15781. nb_points,nb_primitives,c,(unsigned int)(ptrs - ptre));
  15782. return false;
  15783. }
  15784. }
  15785. // Check consistency of opacity data.
  15786. if (ptrs==ptre) {
  15787. if (error_message) cimg_sprintf(error_message,
  15788. "CImg3d (%u,%u) defines no opacity data",
  15789. nb_points,nb_primitives);
  15790. return false;
  15791. }
  15792. for (unsigned int o = 0; o<nb_primitives; ++o) {
  15793. if (*(ptrs++)==(T)-128 && (ptrs+=3)<ptre) {
  15794. const unsigned int
  15795. w = (unsigned int)*(ptrs - 3),
  15796. h = (unsigned int)*(ptrs - 2),
  15797. s = (unsigned int)*(ptrs - 1);
  15798. if (!h && !s) {
  15799. if (w>=o) {
  15800. if (error_message) cimg_sprintf(error_message,
  15801. "CImg3d (%u,%u) refers to invalid shared opacity index %u "
  15802. "for primitive [%u]",
  15803. nb_points,nb_primitives,w,o);
  15804. return false;
  15805. }
  15806. } else ptrs+=w*h*s;
  15807. }
  15808. if (ptrs>ptre) {
  15809. if (error_message) cimg_sprintf(error_message,
  15810. "CImg3d (%u,%u) has incomplete opacity data for primitive [%u]",
  15811. nb_points,nb_primitives,o);
  15812. return false;
  15813. }
  15814. }
  15815. // Check end of data.
  15816. if (ptrs<ptre) {
  15817. if (error_message) cimg_sprintf(error_message,
  15818. "CImg3d (%u,%u) contains %u value%s more than expected",
  15819. nb_points,nb_primitives,(unsigned int)(ptre - ptrs),(ptre - ptrs)>1?"s":"");
  15820. return false;
  15821. }
  15822. return true;
  15823. }
  15824. static bool _is_CImg3d(const T val, const char c) {
  15825. return val>=(T)c && val<(T)(c + 1);
  15826. }
  15827. //@}
  15828. //-------------------------------------
  15829. //
  15830. //! \name Mathematical Functions
  15831. //@{
  15832. //-------------------------------------
  15833. // Define the math formula parser/compiler and expression evaluator.
  15834. struct _cimg_math_parser {
  15835. CImg<doubleT> mem;
  15836. CImg<intT> memtype, memmerge;
  15837. CImgList<ulongT> _code, &code, code_begin, code_end,
  15838. _code_begin_t, &code_begin_t, _code_end_t, &code_end_t;
  15839. CImg<ulongT> opcode;
  15840. const CImg<ulongT> *p_code_end, *p_code;
  15841. const CImg<ulongT> *const p_break;
  15842. CImg<charT> expr, pexpr;
  15843. const CImg<T>& imgin;
  15844. CImg<T> &imgout;
  15845. CImgList<T>& imglist;
  15846. CImg<doubleT> _img_stats, &img_stats, constcache_vals;
  15847. CImgList<doubleT> _list_stats, &list_stats, _list_median, &list_median, _list_norm, &list_norm;
  15848. CImg<uintT> mem_img_stats, constcache_inds;
  15849. CImg<uintT> level, variable_pos, reserved_label;
  15850. CImgList<charT> variable_def, macro_def, macro_body;
  15851. CImgList<boolT> macro_body_is_string;
  15852. char *user_macro;
  15853. unsigned int mempos, mem_img_median, mem_img_norm, mem_img_index, debug_indent, result_dim, break_type,
  15854. constcache_size;
  15855. bool is_parallelizable, is_end_code, is_fill, return_new_comp, need_input_copy;
  15856. double *result;
  15857. cimg_uint64 rng;
  15858. const char *const calling_function, *s_op, *ss_op;
  15859. typedef double (*mp_func)(_cimg_math_parser&);
  15860. #define _cimg_mp_is_scalar(arg) (memtype[arg]<2) // Is scalar value?
  15861. #define _cimg_mp_is_const_scalar(arg) (memtype[arg]==1) // Is const scalar?
  15862. #define _cimg_mp_is_vector(arg) (memtype[arg]>1) // Is vector?
  15863. #define _cimg_mp_is_comp(arg) (!memtype[arg]) // Is computation value?
  15864. #define _cimg_mp_is_reserved(arg) (memtype[arg]==-1) // Is scalar and reserved (e.g. variable)?
  15865. #define _cimg_mp_size(arg) (_cimg_mp_is_scalar(arg)?0U:(unsigned int)memtype[arg] - 1) // Size (0=scalar, N>0=vectorN)
  15866. #define _cimg_mp_calling_function s_calling_function()._data
  15867. #define _cimg_mp_op(s) s_op = s; ss_op = ss
  15868. #define _cimg_mp_check_type(arg,n_arg,mode,N) check_type(arg,n_arg,mode,N,ss,se,saved_char)
  15869. #define _cimg_mp_check_const_scalar(arg,n_arg,mode) check_const_scalar(arg,n_arg,mode,ss,se,saved_char)
  15870. #define _cimg_mp_check_const_index(arg) check_const_index(arg,ss,se,saved_char)
  15871. #define _cimg_mp_check_matrix_square(arg,n_arg) check_matrix_square(arg,n_arg,ss,se,saved_char)
  15872. #define _cimg_mp_check_list() check_list(ss,se,saved_char)
  15873. #define _cimg_mp_defunc(mp) (*(mp_func)(*(mp).opcode))(mp)
  15874. #define _cimg_mp_return(x) { *se = saved_char; s_op = previous_s_op; ss_op = previous_ss_op; return x; }
  15875. #define _cimg_mp_return_nan() _cimg_mp_return(_cimg_mp_slot_nan)
  15876. #define _cimg_mp_const_scalar(val) _cimg_mp_return(const_scalar((double)(val)))
  15877. #define _cimg_mp_scalar0(op) _cimg_mp_return(scalar0(op))
  15878. #define _cimg_mp_scalar1(op,i1) _cimg_mp_return(scalar1(op,i1))
  15879. #define _cimg_mp_scalar2(op,i1,i2) _cimg_mp_return(scalar2(op,i1,i2))
  15880. #define _cimg_mp_scalar3(op,i1,i2,i3) _cimg_mp_return(scalar3(op,i1,i2,i3))
  15881. #define _cimg_mp_scalar4(op,i1,i2,i3,i4) _cimg_mp_return(scalar4(op,i1,i2,i3,i4))
  15882. #define _cimg_mp_scalar5(op,i1,i2,i3,i4,i5) _cimg_mp_return(scalar5(op,i1,i2,i3,i4,i5))
  15883. #define _cimg_mp_scalar6(op,i1,i2,i3,i4,i5,i6) _cimg_mp_return(scalar6(op,i1,i2,i3,i4,i5,i6))
  15884. #define _cimg_mp_scalar7(op,i1,i2,i3,i4,i5,i6,i7) _cimg_mp_return(scalar7(op,i1,i2,i3,i4,i5,i6,i7))
  15885. #define _cimg_mp_vector1_v(op,i1) _cimg_mp_return(vector1_v(op,i1))
  15886. #define _cimg_mp_vector2_sv(op,i1,i2) _cimg_mp_return(vector2_sv(op,i1,i2))
  15887. #define _cimg_mp_vector2_vs(op,i1,i2) _cimg_mp_return(vector2_vs(op,i1,i2))
  15888. #define _cimg_mp_vector2_vv(op,i1,i2) _cimg_mp_return(vector2_vv(op,i1,i2))
  15889. #define _cimg_mp_vector3_vss(op,i1,i2,i3) _cimg_mp_return(vector3_vss(op,i1,i2,i3))
  15890. #define _cimg_mp_strerr \
  15891. *se = saved_char; \
  15892. for (s0 = ss; s0>expr._data && *s0!=';'; --s0) {} \
  15893. if (*s0==';') ++s0; \
  15894. while (cimg::is_blank(*s0)) ++s0; \
  15895. cimg::strellipsize(s0,64)
  15896. // Constructors / Destructors.
  15897. ~_cimg_math_parser() {
  15898. cimg::srand(rng);
  15899. }
  15900. _cimg_math_parser(const char *const expression, const char *const funcname=0,
  15901. const CImg<T>& img_input=CImg<T>::const_empty(), CImg<T> *const img_output=0,
  15902. CImgList<T> *const list_images=0, const bool _is_fill=false):
  15903. code(_code),code_begin_t(_code_begin_t),code_end_t(_code_end_t),
  15904. p_break((CImg<ulongT>*)(cimg_ulong)-2),imgin(img_input),
  15905. imgout(img_output?*img_output:CImg<T>::empty()),imglist(list_images?*list_images:CImgList<T>::empty()),
  15906. img_stats(_img_stats),list_stats(_list_stats),list_median(_list_median),list_norm(_list_norm),user_macro(0),
  15907. mem_img_median(~0U),mem_img_norm(~0U),mem_img_index(~0U),debug_indent(0),result_dim(0),break_type(0),
  15908. constcache_size(0),is_parallelizable(true),is_fill(_is_fill),need_input_copy(false),
  15909. rng((cimg::_rand(),cimg::rng())),calling_function(funcname?funcname:"cimg_math_parser") {
  15910. #if cimg_use_openmp!=0
  15911. rng+=omp_get_thread_num();
  15912. #endif
  15913. if (!expression || !*expression)
  15914. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  15915. "CImg<%s>::%s: Empty expression.",
  15916. pixel_type(),_cimg_mp_calling_function);
  15917. const char *_expression = expression;
  15918. while (*_expression && (cimg::is_blank(*_expression) || *_expression==';')) ++_expression;
  15919. CImg<charT>::string(_expression).move_to(expr);
  15920. char *ps = &expr.back() - 1;
  15921. while (ps>expr._data && (cimg::is_blank(*ps) || *ps==';')) --ps;
  15922. *(++ps) = 0; expr._width = (unsigned int)(ps - expr._data + 1);
  15923. // Ease the retrieval of previous non-space characters afterwards.
  15924. pexpr.assign(expr._width);
  15925. char c, *pe = pexpr._data;
  15926. for (ps = expr._data, c = ' '; *ps; ++ps) {
  15927. if (!cimg::is_blank(*ps)) c = *ps; else *ps = ' ';
  15928. *(pe++) = c;
  15929. }
  15930. *pe = 0;
  15931. level = get_level(expr);
  15932. // Init constant values.
  15933. #define _cimg_mp_interpolation (reserved_label[30]!=~0U?reserved_label[30]:0)
  15934. #define _cimg_mp_boundary (reserved_label[31]!=~0U?reserved_label[31]:0)
  15935. #define _cimg_mp_slot_t 17
  15936. #define _cimg_mp_slot_nan 29
  15937. #define _cimg_mp_slot_x 30
  15938. #define _cimg_mp_slot_y 31
  15939. #define _cimg_mp_slot_z 32
  15940. #define _cimg_mp_slot_c 33
  15941. mem.assign(96);
  15942. for (unsigned int i = 0; i<=10; ++i) mem[i] = (double)i; // mem[0-10] = 0...10
  15943. for (unsigned int i = 1; i<=5; ++i) mem[i + 10] = -(double)i; // mem[11-15] = -1...-5
  15944. mem[16] = 0.5;
  15945. mem[_cimg_mp_slot_t] = 0; // thread_id
  15946. mem[18] = (double)imgin._width; // w
  15947. mem[19] = (double)imgin._height; // h
  15948. mem[20] = (double)imgin._depth; // d
  15949. mem[21] = (double)imgin._spectrum; // s
  15950. mem[22] = (double)imgin._is_shared; // r
  15951. mem[23] = (double)imgin._width*imgin._height; // wh
  15952. mem[24] = (double)imgin._width*imgin._height*imgin._depth; // whd
  15953. mem[25] = (double)imgin._width*imgin._height*imgin._depth*imgin._spectrum; // whds
  15954. mem[26] = (double)imglist._width; // l
  15955. mem[27] = std::exp(1.); // e
  15956. mem[28] = cimg::PI; // pi
  15957. mem[_cimg_mp_slot_nan] = cimg::type<double>::nan(); // nan
  15958. // Set value property :
  15959. // { -1 = reserved (e.g. variable) | 0 = computation value |
  15960. // 1 = compile-time constant | N>1 = constant ptr to vector[N-1] }.
  15961. memtype.assign(mem._width,1,1,1,0);
  15962. for (unsigned int i = 0; i<_cimg_mp_slot_x; ++i) memtype[i] = 1;
  15963. memtype[_cimg_mp_slot_t] = memtype[_cimg_mp_slot_x] = memtype[_cimg_mp_slot_y] =
  15964. memtype[_cimg_mp_slot_z] = memtype[_cimg_mp_slot_c] = -1;
  15965. mempos = _cimg_mp_slot_c + 1;
  15966. variable_pos.assign(8);
  15967. reserved_label.assign(128,1,1,1,~0U);
  15968. // reserved_label[0-31] are used to store the memory index of these variables:
  15969. // [0] = wh, [1] = whd, [2] = whds, [3] = pi, [4] = im, [5] = iM, [6] = ia, [7] = iv,
  15970. // [8] = is, [9] = ip, [10] = ic, [11] = in, [12] = xm, [13] = ym, [14] = zm, [15] = cm, [16] = xM,
  15971. // [17] = yM, [18] = zM, [19] = cM, [20] = i0...[29] = i9, [30] = interpolation, [31] = boundary
  15972. // Compile expression into a sequence of opcodes.
  15973. s_op = ""; ss_op = expr._data;
  15974. const unsigned int ind_result = compile(expr._data,expr._data + expr._width - 1,0,0,0);
  15975. if (!_cimg_mp_is_const_scalar(ind_result)) {
  15976. if (_cimg_mp_is_vector(ind_result))
  15977. CImg<doubleT>(&mem[ind_result] + 1,_cimg_mp_size(ind_result),1,1,1,true).
  15978. fill(cimg::type<double>::nan());
  15979. else if (ind_result!=_cimg_mp_slot_t) mem[ind_result] = cimg::type<double>::nan();
  15980. }
  15981. // Free resources used for compiling expression and prepare evaluation.
  15982. result_dim = _cimg_mp_size(ind_result);
  15983. if (mem._width>=256 && mem._width - mempos>=mem._width/2) mem.resize(mempos,1,1,1,-1);
  15984. result = mem._data + ind_result;
  15985. memtype.assign();
  15986. constcache_vals.assign();
  15987. constcache_inds.assign();
  15988. level.assign();
  15989. variable_pos.assign();
  15990. reserved_label.assign();
  15991. expr.assign();
  15992. pexpr.assign();
  15993. opcode.assign();
  15994. opcode._is_shared = true;
  15995. // Execute begin() bloc if any specified.
  15996. if (code_begin) {
  15997. mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
  15998. p_code_end = code_begin.end();
  15999. for (p_code = code_begin; p_code<p_code_end; ++p_code) {
  16000. opcode._data = p_code->_data;
  16001. const ulongT target = opcode[1];
  16002. mem[target] = _cimg_mp_defunc(*this);
  16003. }
  16004. }
  16005. p_code_end = code.end();
  16006. }
  16007. _cimg_math_parser():
  16008. code(_code),code_begin_t(_code_begin_t),code_end_t(_code_end_t),
  16009. p_code_end(0),p_break((CImg<ulongT>*)(cimg_ulong)-2),
  16010. imgin(CImg<T>::const_empty()),imgout(CImg<T>::empty()),imglist(CImgList<T>::empty()),
  16011. img_stats(_img_stats),list_stats(_list_stats),list_median(_list_median),list_norm(_list_norm),debug_indent(0),
  16012. result_dim(0),break_type(0),constcache_size(0),is_parallelizable(true),is_fill(false),
  16013. need_input_copy(false),rng(0),calling_function(0) {
  16014. mem.assign(1 + _cimg_mp_slot_c,1,1,1,0); // Allow to skip 'is_empty?' test in operator()()
  16015. result = mem._data;
  16016. }
  16017. _cimg_math_parser(const _cimg_math_parser& mp):
  16018. mem(mp.mem),code(mp.code),code_begin_t(mp.code_begin_t),code_end_t(mp.code_end_t),
  16019. p_code_end(mp.p_code_end),p_break(mp.p_break),
  16020. imgin(mp.imgin),imgout(mp.imgout),imglist(mp.imglist),
  16021. img_stats(mp.img_stats),list_stats(mp.list_stats),list_median(mp.list_median),list_norm(mp.list_norm),
  16022. debug_indent(0),result_dim(mp.result_dim),break_type(0),constcache_size(0),
  16023. is_parallelizable(mp.is_parallelizable),is_fill(mp.is_fill),
  16024. need_input_copy(mp.need_input_copy),result(mem._data + (mp.result - mp.mem._data)),
  16025. rng((cimg::_rand(),cimg::rng())),calling_function(0) {
  16026. #if cimg_use_openmp!=0
  16027. mem[_cimg_mp_slot_t] = (double)omp_get_thread_num();
  16028. rng+=omp_get_thread_num();
  16029. #endif
  16030. opcode.assign();
  16031. opcode._is_shared = true;
  16032. }
  16033. // Compilation procedure.
  16034. unsigned int compile(char *ss, char *se, const unsigned int depth, unsigned int *const p_ref,
  16035. const unsigned char bloc_flags) {
  16036. if (depth>256) {
  16037. cimg::strellipsize(expr,64);
  16038. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16039. "CImg<%s>::%s: Call stack overflow (infinite recursion?), "
  16040. "in expression '%s'.",
  16041. pixel_type(),_cimg_mp_calling_function,
  16042. (ss - 4)>expr._data?ss - 4:expr._data);
  16043. }
  16044. char c1, c2;
  16045. // Simplify expression when possible.
  16046. do {
  16047. c2 = 0;
  16048. if (ss<se) {
  16049. while (*ss && (cimg::is_blank(*ss) || *ss==';')) ++ss; // Remove leading blanks and ';'
  16050. while (se>ss && (cimg::is_blank(c1 = *(se - 1)) || c1==';')) --se; // Remove trailing blanks and ';'
  16051. }
  16052. while (*ss=='(' && *(se - 1)==')' && std::strchr(ss,')')==se - 1) { // Remove useless start/end parentheses
  16053. ++ss; --se; c2 = 1;
  16054. }
  16055. if (*ss=='_' && ss + 1<se && ss[1]=='(') { // Remove leading '_(something)' comment.
  16056. const unsigned int clevel = level[ss - expr._data];
  16057. ss+=2;
  16058. while (ss<se && (*ss!=')' || level[ss - expr._data]!=clevel)) ++ss;
  16059. if (ss<se) ++ss;
  16060. if (ss>=se) return _cimg_mp_slot_nan;
  16061. c2 = 1;
  16062. }
  16063. } while (c2 && ss<se);
  16064. if (se<=ss || !*ss) {
  16065. cimg::strellipsize(expr,64);
  16066. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16067. "CImg<%s>::%s: %s%s Missing %s, in expression '%s'.",
  16068. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
  16069. *s_op=='F'?"argument":"item",
  16070. ss_op);
  16071. }
  16072. static const size_t siz_ref = 7*sizeof(unsigned int);
  16073. const char *const previous_s_op = s_op, *const previous_ss_op = ss_op;
  16074. const unsigned int depth1 = depth + 1;
  16075. unsigned int pos, p1, p2, p3, arg1, arg2, arg3, arg4, arg5, arg6;
  16076. char
  16077. *const se1 = se - 1, *const se2 = se - 2, *const se3 = se - 3,
  16078. *const ss1 = ss + 1, *const ss2 = ss + 2, *const ss3 = ss + 3, *const ss4 = ss + 4,
  16079. *const ss5 = ss + 5, *const ss6 = ss + 6, *const ss7 = ss + 7, *const ss8 = ss + 8,
  16080. *s, *ps, *ns, *s0, *s1, *s2, *s3, sep = 0, end = 0;
  16081. double val = 0, val1, val2;
  16082. mp_func op;
  16083. return_new_comp = false;
  16084. // Bits of 'bloc_flags' tell about in which code bloc we currently are:
  16085. // 0: critical(), 1: begin(), 2: begin_t(), 3: end(), 4: end_t().
  16086. const bool is_inside_critical = (bool)(bloc_flags&1);
  16087. // 'p_ref' is a 'unsigned int[7]' used to return a reference to an image or vector value
  16088. // linked to the returned memory slot (reference that cannot be determined at compile time).
  16089. // p_ref[0] can be { 0 = scalar (unlinked) | 1 = vector value | 2 = image value (offset) |
  16090. // 3 = image value (coordinates) | 4 = image value as a vector (offsets) |
  16091. // 5 = image value as a vector (coordinates) }.
  16092. // Depending on p_ref[0], the remaining p_ref[k] have the following meaning:
  16093. // When p_ref[0]==0, p_ref is actually unlinked.
  16094. // When p_ref[0]==1, p_ref = [ 1, vector_ind, offset ].
  16095. // When p_ref[0]==2, p_ref = [ 2, image_ind (or ~0U), is_relative, offset ].
  16096. // When p_ref[0]==3, p_ref = [ 3, image_ind (or ~0U), is_relative, x, y, z, c ].
  16097. // When p_ref[0]==4, p_ref = [ 4, image_ind (or ~0U), is_relative, offset ].
  16098. // When p_ref[0]==5, p_ref = [ 5, image_ind (or ~0U), is_relative, x, y, z ].
  16099. if (p_ref) { *p_ref = 0; p_ref[1] = p_ref[2] = p_ref[3] = p_ref[4] = p_ref[5] = p_ref[6] = ~0U; }
  16100. const char saved_char = *se; *se = 0;
  16101. const unsigned int clevel = level[ss - expr._data], clevel1 = clevel + 1;
  16102. bool is_sth, is_relative;
  16103. CImg<uintT> ref;
  16104. CImg<charT> variable_name;
  16105. CImgList<ulongT> l_opcode;
  16106. // Look for a single value or a pre-defined variable.
  16107. int nb = 0;
  16108. s = ss + (*ss=='+' || *ss=='-'?1:0);
  16109. if (*s=='i' || *s=='I' || *s=='n' || *s=='N') { // Particular cases : +/-NaN and +/-Inf
  16110. is_sth = *ss=='-';
  16111. if (!cimg::strcasecmp(s,"inf")) { val = cimg::type<double>::inf(); nb = 1; }
  16112. else if (!cimg::strcasecmp(s,"nan")) { val = cimg::type<double>::nan(); nb = 1; }
  16113. if (nb==1 && is_sth) val = -val;
  16114. } else if (*s=='0' && (s[1]=='x' || s[1]=='X')) { // Hexadecimal number
  16115. is_sth = *ss=='-';
  16116. if (cimg_sscanf(s + 2,"%x%c",&arg1,&sep)==1) {
  16117. nb = 1;
  16118. val = (double)arg1;
  16119. if (is_sth) val = -val;
  16120. }
  16121. }
  16122. if (!nb) nb = cimg_sscanf(ss,"%lf%c%c",&val,&(sep=0),&(end=0));
  16123. if (nb==1) _cimg_mp_const_scalar(val);
  16124. if (nb==2 && sep=='%') _cimg_mp_const_scalar(val/100);
  16125. if (ss1==se) switch (*ss) { // One-char reserved variable
  16126. case 'c' : _cimg_mp_return(reserved_label[(int)'c']!=~0U?reserved_label[(int)'c']:_cimg_mp_slot_c);
  16127. case 'd' : _cimg_mp_return(reserved_label[(int)'d']!=~0U?reserved_label[(int)'d']:20);
  16128. case 'e' : _cimg_mp_return(reserved_label[(int)'e']!=~0U?reserved_label[(int)'e']:27);
  16129. case 'h' : _cimg_mp_return(reserved_label[(int)'h']!=~0U?reserved_label[(int)'h']:19);
  16130. case 'k' :
  16131. if (reserved_label[(int)'k']!=~0U) _cimg_mp_return(reserved_label[(int)'k']);
  16132. pos = get_mem_img_index();
  16133. if (pos!=~0U) _cimg_mp_return(pos);
  16134. _cimg_mp_return_nan();
  16135. case 'l' : _cimg_mp_return(reserved_label[(int)'l']!=~0U?reserved_label[(int)'l']:26);
  16136. case 'r' : _cimg_mp_return(reserved_label[(int)'r']!=~0U?reserved_label[(int)'r']:22);
  16137. case 's' : _cimg_mp_return(reserved_label[(int)'s']!=~0U?reserved_label[(int)'s']:21);
  16138. case 't' : _cimg_mp_return(reserved_label[(int)'t']!=~0U?reserved_label[(int)'t']:_cimg_mp_slot_t);
  16139. case 'n' :
  16140. if (reserved_label[(int)'n']!=~0U) _cimg_mp_return(reserved_label[(int)'n']);
  16141. #if cimg_use_openmp!=0
  16142. _cimg_mp_const_scalar((double)omp_get_max_threads());
  16143. #else
  16144. _cimg_mp_return(1);
  16145. #endif
  16146. case 'w' : _cimg_mp_return(reserved_label[(int)'w']!=~0U?reserved_label[(int)'w']:18);
  16147. case 'x' : _cimg_mp_return(reserved_label[(int)'x']!=~0U?reserved_label[(int)'x']:_cimg_mp_slot_x);
  16148. case 'y' : _cimg_mp_return(reserved_label[(int)'y']!=~0U?reserved_label[(int)'y']:_cimg_mp_slot_y);
  16149. case 'z' : _cimg_mp_return(reserved_label[(int)'z']!=~0U?reserved_label[(int)'z']:_cimg_mp_slot_z);
  16150. case 'u' :
  16151. if (reserved_label[(int)'u']!=~0U) _cimg_mp_return(reserved_label[(int)'u']);
  16152. _cimg_mp_scalar2(mp_u,0,1);
  16153. case 'g' :
  16154. if (reserved_label[(int)'g']!=~0U) _cimg_mp_return(reserved_label[(int)'g']);
  16155. _cimg_mp_scalar0(mp_g);
  16156. case 'i' :
  16157. if (reserved_label[(int)'i']!=~0U) _cimg_mp_return(reserved_label[(int)'i']);
  16158. _cimg_mp_scalar0(mp_i);
  16159. case 'I' :
  16160. _cimg_mp_op("Variable 'I'");
  16161. if (reserved_label[(int)'I']!=~0U) _cimg_mp_return(reserved_label[(int)'I']);
  16162. if (!imgin._spectrum) _cimg_mp_return(0);
  16163. need_input_copy = true;
  16164. pos = vector(imgin._spectrum);
  16165. CImg<ulongT>::vector((ulongT)mp_Joff,pos,0,0,imgin._spectrum).move_to(code);
  16166. return_new_comp = true;
  16167. _cimg_mp_return(pos);
  16168. case 'R' :
  16169. if (reserved_label[(int)'R']!=~0U) _cimg_mp_return(reserved_label[(int)'R']);
  16170. need_input_copy = true;
  16171. _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,0,0,0);
  16172. case 'G' :
  16173. if (reserved_label[(int)'G']!=~0U) _cimg_mp_return(reserved_label[(int)'G']);
  16174. need_input_copy = true;
  16175. _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,1,0,0);
  16176. case 'B' :
  16177. if (reserved_label[(int)'B']!=~0U) _cimg_mp_return(reserved_label[(int)'B']);
  16178. need_input_copy = true;
  16179. _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,2,0,0);
  16180. case 'A' :
  16181. if (reserved_label[(int)'A']!=~0U) _cimg_mp_return(reserved_label[(int)'A']);
  16182. need_input_copy = true;
  16183. _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,3,0,0);
  16184. }
  16185. else if (ss2==se) { // Two-chars reserved variable
  16186. arg1 = arg2 = ~0U;
  16187. if (*ss=='w' && *ss1=='h') // wh
  16188. _cimg_mp_return(reserved_label[0]!=~0U?reserved_label[0]:23);
  16189. if (*ss=='p' && *ss1=='i') // pi
  16190. _cimg_mp_return(reserved_label[3]!=~0U?reserved_label[3]:28);
  16191. if (*ss=='i') {
  16192. if (*ss1>='0' && *ss1<='9') { // i0...i9
  16193. pos = 20 + *ss1 - '0';
  16194. if (reserved_label[pos]!=~0U) _cimg_mp_return(reserved_label[pos]);
  16195. need_input_copy = true;
  16196. _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,pos - 20,0,0);
  16197. }
  16198. switch (*ss1) {
  16199. case 'm' : arg1 = 4; arg2 = 0; break; // im
  16200. case 'M' : arg1 = 5; arg2 = 1; break; // iM
  16201. case 'a' : arg1 = 6; arg2 = 2; break; // ia
  16202. case 'v' : arg1 = 7; arg2 = 3; break; // iv
  16203. case 's' : arg1 = 8; arg2 = 12; break; // is
  16204. case 'p' : arg1 = 9; arg2 = 13; break; // ip
  16205. case 'c' : // ic
  16206. if (reserved_label[10]!=~0U) _cimg_mp_return(reserved_label[10]);
  16207. if (mem_img_median==~0U) mem_img_median = imgin?const_scalar(imgin.median()):0;
  16208. _cimg_mp_return(mem_img_median);
  16209. break;
  16210. case 'n' : // in
  16211. if (reserved_label[11]!=~0U) _cimg_mp_return(reserved_label[11]);
  16212. if (mem_img_norm==~0U) mem_img_norm = imgin?const_scalar(imgin.magnitude()):0;
  16213. _cimg_mp_return(mem_img_norm);
  16214. }
  16215. }
  16216. else if (*ss1=='m') switch (*ss) {
  16217. case 'x' : arg1 = 12; arg2 = 4; break; // xm
  16218. case 'y' : arg1 = 13; arg2 = 5; break; // ym
  16219. case 'z' : arg1 = 14; arg2 = 6; break; // zm
  16220. case 'c' : arg1 = 15; arg2 = 7; break; // cm
  16221. }
  16222. else if (*ss1=='M') switch (*ss) {
  16223. case 'x' : arg1 = 16; arg2 = 8; break; // xM
  16224. case 'y' : arg1 = 17; arg2 = 9; break; // yM
  16225. case 'z' : arg1 = 18; arg2 = 10; break; // zM
  16226. case 'c' : arg1 = 19; arg2 = 11; break; // cM
  16227. }
  16228. if (arg1!=~0U) {
  16229. if (reserved_label[arg1]!=~0U) _cimg_mp_return(reserved_label[arg1]);
  16230. if (!img_stats) {
  16231. img_stats.assign(1,14,1,1,0).fill(imgin.get_stats(),false);
  16232. mem_img_stats.assign(1,14,1,1,~0U);
  16233. }
  16234. if (mem_img_stats[arg2]==~0U) mem_img_stats[arg2] = const_scalar(img_stats[arg2]);
  16235. _cimg_mp_return(mem_img_stats[arg2]);
  16236. }
  16237. } else if (ss3==se) { // Three-chars reserved variable
  16238. if (*ss=='w' && *ss1=='h' && *ss2=='d') // whd
  16239. _cimg_mp_return(reserved_label[1]!=~0U?reserved_label[1]:24);
  16240. } else if (ss4==se) { // Four-chars reserved variable
  16241. if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='s') // whds
  16242. _cimg_mp_return(reserved_label[2]!=~0U?reserved_label[2]:25);
  16243. }
  16244. pos = ~0U;
  16245. is_sth = false;
  16246. for (s0 = ss, s = ss1; s<se1; ++s)
  16247. if (*s==';' && level[s - expr._data]==clevel) { // Separator ';'
  16248. is_end_code = false;
  16249. arg1 = compile(s0,s++,depth,0,bloc_flags);
  16250. if (!is_end_code) pos = arg1; // 'end()' and 'end_t()' return void
  16251. is_sth = true;
  16252. while (*s && (cimg::is_blank(*s) || *s==';')) ++s;
  16253. s0 = s;
  16254. }
  16255. if (is_sth) {
  16256. is_end_code = false;
  16257. arg1 = compile(s0,se,depth,p_ref,bloc_flags);
  16258. if (!is_end_code) pos = arg1; // 'end()' and 'end_t()' return void
  16259. _cimg_mp_return(pos!=~0U?pos:_cimg_mp_slot_nan);
  16260. }
  16261. // Declare / assign variable, vector value or image value.
  16262. for (s = ss1, ps = ss, ns = ss2; s<se1; ++s, ++ps, ++ns)
  16263. if (*s=='=' && *ns!='=' && *ps!='=' && *ps!='>' && *ps!='<' && *ps!='!' &&
  16264. *ps!='+' && *ps!='-' && *ps!='*' && *ps!='/' && *ps!='%' &&
  16265. *ps!='>' && *ps!='<' && *ps!='&' && *ps!='|' && *ps!='^' &&
  16266. level[s - expr._data]==clevel) {
  16267. variable_name.assign(ss,(unsigned int)(s + 1 - ss)).back() = 0;
  16268. cimg::strpare(variable_name,false,true);
  16269. const unsigned int l_variable_name = (unsigned int)std::strlen(variable_name);
  16270. char *const ve1 = ss + l_variable_name - 1;
  16271. _cimg_mp_op("Operator '='");
  16272. // Assign image value (direct).
  16273. if (l_variable_name>2 && (*ss=='i' || *ss=='j' || *ss=='I' || *ss=='J') && (*ss1=='(' || *ss1=='[') &&
  16274. (reserved_label[(int)*ss]==~0U || *ss1=='(' || !_cimg_mp_is_vector(reserved_label[(int)*ss]))) {
  16275. is_relative = *ss=='j' || *ss=='J';
  16276. if (*ss1=='[' && *ve1==']') { // i/j/I/J[_#ind,offset] = value
  16277. if (!is_inside_critical) is_parallelizable = false;
  16278. if (*ss2=='#') { // Index specified
  16279. s0 = ss3; while (s0<ve1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  16280. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  16281. _cimg_mp_check_list();
  16282. } else { p1 = ~0U; s0 = ss2; }
  16283. arg1 = compile(s0,ve1,depth1,0,bloc_flags); // Offset
  16284. _cimg_mp_check_type(arg1,0,1,0);
  16285. arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
  16286. _cimg_mp_check_type(arg2,2,*ss>='i'?1:3,0);
  16287. if (_cimg_mp_is_vector(arg2)) {
  16288. if (p1!=~0U) {
  16289. _cimg_mp_check_const_index(p1);
  16290. p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  16291. p2 = imglist[p3]._spectrum;
  16292. } else p2 = imgin._spectrum;
  16293. if (!p2) _cimg_mp_return(0);
  16294. _cimg_mp_check_type(arg2,2,2,p2);
  16295. } else p2 = 0;
  16296. if (p_ref) {
  16297. *p_ref = _cimg_mp_is_vector(arg2)?4:2;
  16298. p_ref[1] = p1;
  16299. p_ref[2] = (unsigned int)is_relative;
  16300. p_ref[3] = arg1;
  16301. if (_cimg_mp_is_vector(arg2))
  16302. set_reserved_vector(arg2); // Prevent from being used in further optimization
  16303. else if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
  16304. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  16305. }
  16306. if (p1!=~0U) {
  16307. if (!imglist) _cimg_mp_return(arg2);
  16308. if (*ss>='i')
  16309. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
  16310. arg2,p1,arg1).move_to(code);
  16311. else if (_cimg_mp_is_scalar(arg2))
  16312. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
  16313. arg2,p1,arg1).move_to(code);
  16314. else
  16315. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  16316. arg2,p1,arg1,_cimg_mp_size(arg2)).move_to(code);
  16317. } else {
  16318. if (!imgout) _cimg_mp_return(arg2);
  16319. if (*ss>='i')
  16320. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
  16321. arg2,arg1).move_to(code);
  16322. else if (_cimg_mp_is_scalar(arg2))
  16323. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
  16324. arg2,arg1).move_to(code);
  16325. else
  16326. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  16327. arg2,arg1,_cimg_mp_size(arg2)).move_to(code);
  16328. }
  16329. _cimg_mp_return(arg2);
  16330. }
  16331. if (*ss1=='(' && *ve1==')') { // i/j/I/J(_#ind,_x,_y,_z,_c) = value
  16332. if (!is_inside_critical) is_parallelizable = false;
  16333. if (*ss2=='#') { // Index specified
  16334. s0 = ss3; while (s0<ve1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  16335. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  16336. _cimg_mp_check_list();
  16337. } else { p1 = ~0U; s0 = ss2; }
  16338. arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
  16339. arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
  16340. arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
  16341. arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
  16342. arg5 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
  16343. _cimg_mp_check_type(arg5,2,*ss>='i'?1:3,0);
  16344. if (s0<ve1) { // X or [ X,_Y,_Z,_C ]
  16345. s1 = s0; while (s1<ve1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  16346. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  16347. if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
  16348. p2 = _cimg_mp_size(arg1); // Vector size
  16349. ++arg1;
  16350. if (p2>1) {
  16351. arg2 = arg1 + 1;
  16352. if (p2>2) {
  16353. arg3 = arg2 + 1;
  16354. if (p2>3) arg4 = arg3 + 1;
  16355. }
  16356. }
  16357. } else if (s1<ve1) { // Y
  16358. s2 = ++s1; while (s2<ve1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  16359. arg2 = compile(s1,s2,depth1,0,bloc_flags);
  16360. if (s2<ve1) { // Z
  16361. s3 = ++s2; while (s3<ve1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  16362. arg3 = compile(s2,s3,depth1,0,bloc_flags);
  16363. if (s3<ve1) arg4 = compile(++s3,ve1,depth1,0,bloc_flags); // C
  16364. }
  16365. }
  16366. }
  16367. if (_cimg_mp_is_vector(arg5)) {
  16368. if (p1!=~0U) {
  16369. _cimg_mp_check_const_index(p1);
  16370. p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  16371. p2 = imglist[p3]._spectrum;
  16372. } else p2 = imgin._spectrum;
  16373. if (!p2) _cimg_mp_return(0);
  16374. _cimg_mp_check_type(arg5,2,2,p2);
  16375. } else p2 = 0;
  16376. if (p_ref) {
  16377. *p_ref = _cimg_mp_is_vector(arg5)?5:3;
  16378. p_ref[1] = p1;
  16379. p_ref[2] = (unsigned int)is_relative;
  16380. p_ref[3] = arg1;
  16381. p_ref[4] = arg2;
  16382. p_ref[5] = arg3;
  16383. p_ref[6] = arg4;
  16384. if (_cimg_mp_is_vector(arg5))
  16385. set_reserved_vector(arg5); // Prevent from being used in further optimization
  16386. else if (_cimg_mp_is_comp(arg5)) memtype[arg5] = -1;
  16387. if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1;
  16388. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  16389. if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
  16390. if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
  16391. if (_cimg_mp_is_comp(arg4)) memtype[arg4] = -1;
  16392. }
  16393. if (p1!=~0U) {
  16394. if (!imglist) _cimg_mp_return(arg5);
  16395. if (*ss>='i')
  16396. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
  16397. arg5,p1,arg1,arg2,arg3,arg4).move_to(code);
  16398. else if (_cimg_mp_is_scalar(arg5))
  16399. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
  16400. arg5,p1,arg1,arg2,arg3).move_to(code);
  16401. else
  16402. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  16403. arg5,p1,arg1,arg2,arg3,_cimg_mp_size(arg5)).move_to(code);
  16404. } else {
  16405. if (!imgout) _cimg_mp_return(arg5);
  16406. if (*ss>='i')
  16407. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
  16408. arg5,arg1,arg2,arg3,arg4).move_to(code);
  16409. else if (_cimg_mp_is_scalar(arg5))
  16410. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
  16411. arg5,arg1,arg2,arg3).move_to(code);
  16412. else
  16413. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  16414. arg5,arg1,arg2,arg3,_cimg_mp_size(arg5)).move_to(code);
  16415. }
  16416. _cimg_mp_return(arg5);
  16417. }
  16418. }
  16419. // Assign vector value (direct).
  16420. if (l_variable_name>3 && *ve1==']' && *ss!='[') {
  16421. s0 = ve1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
  16422. if (s0>ss && is_varname(ss,s0 - ss)) {
  16423. variable_name[s0 - ss] = 0; // Remove brackets in variable name
  16424. get_variable_pos(variable_name,arg1,arg2);
  16425. arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U; // Vector slot
  16426. if (arg1==~0U || _cimg_mp_is_scalar(arg1))
  16427. compile(ss,s0,depth1,0,bloc_flags); // Variable does not exist or is not a vector -> error
  16428. arg2 = compile(++s0,ve1,depth1,0,bloc_flags); // Index
  16429. arg3 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
  16430. _cimg_mp_check_type(arg3,2,1,0);
  16431. if (_cimg_mp_is_const_scalar(arg2)) { // Constant index -> return corresponding variable slot directly
  16432. nb = (int)mem[arg2];
  16433. if (nb>=0 && nb<(int)_cimg_mp_size(arg1)) {
  16434. arg1+=nb + 1;
  16435. CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg3).move_to(code);
  16436. _cimg_mp_return(arg1);
  16437. }
  16438. compile(ss,s,depth1,0,bloc_flags); // Out-of-bounds reference -> error
  16439. }
  16440. // Case of non-constant index -> return assigned value + linked reference
  16441. if (p_ref) {
  16442. *p_ref = 1;
  16443. p_ref[1] = arg1;
  16444. p_ref[2] = arg2;
  16445. if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1; // Prevent from being used in further optimization
  16446. if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
  16447. }
  16448. CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg3,arg1,(ulongT)_cimg_mp_size(arg1),arg2).
  16449. move_to(code);
  16450. _cimg_mp_return(arg3);
  16451. }
  16452. }
  16453. // Assign user-defined macro.
  16454. if (l_variable_name>2 && *ve1==')' && *ss!='(') {
  16455. s0 = ve1; while (s0>ss && *s0!='(') --s0;
  16456. if (is_varname(ss,s0 - ss) && std::strncmp(variable_name,"debug(",6) &&
  16457. std::strncmp(variable_name,"print(",6)) { // Valid macro name
  16458. s0 = variable_name._data + (s0 - ss);
  16459. *s0 = 0;
  16460. s1 = variable_name._data + l_variable_name - 1; // Pointer to closing parenthesis
  16461. CImg<charT>(variable_name._data,(unsigned int)(s0 - variable_name._data + 1)).move_to(macro_def,0);
  16462. ++s; while (*s && cimg::is_blank(*s)) ++s;
  16463. CImg<charT>(s,(unsigned int)(se - s + 1)).move_to(macro_body,0);
  16464. p1 = 1; // Index of current parsed argument
  16465. for (s = s0 + 1; s<=s1; ++p1, s = ns + 1) { // Parse function arguments
  16466. if (p1>24) {
  16467. _cimg_mp_strerr;
  16468. cimg::strellipsize(variable_name,64);
  16469. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16470. "CImg<%s>::%s: %s: Too much specified arguments (>24) in macro "
  16471. "definition '%s()', in expression '%s'.",
  16472. pixel_type(),_cimg_mp_calling_function,s_op,
  16473. variable_name._data,s0);
  16474. }
  16475. while (*s && cimg::is_blank(*s)) ++s;
  16476. if (*s==')' && p1==1) break; // Function has no arguments
  16477. s2 = s; // Start of the argument name
  16478. is_sth = true; // is_valid_argument_name?
  16479. if (*s>='0' && *s<='9') is_sth = false;
  16480. else for (ns = s; ns<s1 && *ns!=',' && !cimg::is_blank(*ns); ++ns)
  16481. if (!is_varchar(*ns)) { is_sth = false; break; }
  16482. s3 = ns; // End of the argument name
  16483. while (*ns && cimg::is_blank(*ns)) ++ns;
  16484. if (!is_sth || s2==s3 || (*ns!=',' && ns!=s1)) {
  16485. _cimg_mp_strerr;
  16486. cimg::strellipsize(variable_name,64);
  16487. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16488. "CImg<%s>::%s: %s: %s name specified for argument %u when defining "
  16489. "macro '%s()', in expression '%s'.",
  16490. pixel_type(),_cimg_mp_calling_function,s_op,
  16491. is_sth?"Empty":"Invalid",p1,
  16492. variable_name._data,s0);
  16493. }
  16494. if (ns==s1 || *ns==',') { // New argument found
  16495. *s3 = 0;
  16496. p2 = (unsigned int)(s3 - s2); // Argument length
  16497. for (ps = std::strstr(macro_body[0],s2); ps; ps = std::strstr(ps,s2)) { // Replace by arg number
  16498. if (!((ps>macro_body[0]._data && is_varchar(*(ps - 1))) ||
  16499. (ps + p2<macro_body[0].end() && is_varchar(*(ps + p2))))) {
  16500. if (ps>macro_body[0]._data && *(ps - 1)=='#') { // Remove pre-number sign
  16501. *(ps - 1) = (char)p1;
  16502. if (ps + p2<macro_body[0].end() && *(ps + p2)=='#') { // Has pre & post number signs
  16503. std::memmove(ps,ps + p2 + 1,macro_body[0].end() - ps - p2 - 1);
  16504. macro_body[0]._width-=p2 + 1;
  16505. } else { // Has pre number sign only
  16506. std::memmove(ps,ps + p2,macro_body[0].end() - ps - p2);
  16507. macro_body[0]._width-=p2;
  16508. }
  16509. } else if (ps + p2<macro_body[0].end() && *(ps + p2)=='#') { // Remove post-number sign
  16510. *(ps++) = (char)p1;
  16511. std::memmove(ps,ps + p2,macro_body[0].end() - ps - p2);
  16512. macro_body[0]._width-=p2;
  16513. } else { // Not near a number sign
  16514. if (p2<3) {
  16515. ps-=(ulongT)macro_body[0]._data;
  16516. macro_body[0].resize(macro_body[0]._width - p2 + 3,1,1,1,0);
  16517. ps+=(ulongT)macro_body[0]._data;
  16518. } else macro_body[0]._width-=p2 - 3;
  16519. std::memmove(ps + 3,ps + p2,macro_body[0].end() - ps - 3);
  16520. *(ps++) = '(';
  16521. *(ps++) = (char)p1;
  16522. *(ps++) = ')';
  16523. }
  16524. } else ++ps;
  16525. }
  16526. }
  16527. }
  16528. // Store number of arguments.
  16529. macro_def[0].resize(macro_def[0]._width + 1,1,1,1,0).back() = (char)(p1 - 1);
  16530. // Detect parts of function body inside a string.
  16531. is_inside_string(macro_body[0]).move_to(macro_body_is_string,0);
  16532. _cimg_mp_return_nan();
  16533. }
  16534. }
  16535. // Check if the variable name could be valid. If not, this is probably an lvalue assignment.
  16536. const bool is_const = l_variable_name>6 && !std::strncmp(variable_name,"const ",6);
  16537. s0 = variable_name._data;
  16538. if (is_const) {
  16539. s0+=6; while (cimg::is_blank(*s0)) ++s0;
  16540. variable_name.resize(variable_name.end() - s0,1,1,1,0,0,1);
  16541. }
  16542. if (is_varname(variable_name)) { // Valid variable name
  16543. // Assign variable (direct).
  16544. get_variable_pos(variable_name,arg1,arg2);
  16545. arg3 = compile(s + 1,se,depth1,0,bloc_flags);
  16546. is_sth = return_new_comp; // is arg3 a new blank object?
  16547. if (is_const) _cimg_mp_check_const_scalar(arg3,2,0);
  16548. arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U;
  16549. if (arg1==~0U) { // Create new variable
  16550. if (_cimg_mp_is_vector(arg3)) { // Vector variable
  16551. arg1 = is_sth || is_comp_vector(arg3)?arg3:vector_copy(arg3);
  16552. set_reserved_vector(arg1); // Prevent from being used in further optimization
  16553. } else { // Scalar variable
  16554. if (is_const) arg1 = arg3;
  16555. else {
  16556. arg1 = is_sth || _cimg_mp_is_comp(arg3)?arg3:scalar1(mp_copy,arg3);
  16557. memtype[arg1] = -1;
  16558. }
  16559. }
  16560. if (arg2!=~0U) reserved_label[arg2] = arg1;
  16561. else {
  16562. if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
  16563. variable_pos[variable_def._width] = arg1;
  16564. variable_name.move_to(variable_def);
  16565. }
  16566. } else { // Variable already exists -> assign a new value
  16567. if (is_const || _cimg_mp_is_const_scalar(arg1)) {
  16568. _cimg_mp_strerr;
  16569. cimg::strellipsize(variable_name,64);
  16570. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16571. "CImg<%s>::%s: %s: Invalid assignment of %sconst variable '%s'%s, "
  16572. "in expression '%s'.",
  16573. pixel_type(),_cimg_mp_calling_function,s_op,
  16574. _cimg_mp_is_const_scalar(arg1)?"":"non-",
  16575. variable_name._data,
  16576. !_cimg_mp_is_const_scalar(arg1) && is_const?" as a const variable":"",
  16577. s0);
  16578. }
  16579. _cimg_mp_check_type(arg3,2,_cimg_mp_is_vector(arg1)?3:1,_cimg_mp_size(arg1));
  16580. if (_cimg_mp_is_vector(arg1)) { // Vector
  16581. if (_cimg_mp_is_vector(arg3)) // From vector
  16582. CImg<ulongT>::vector((ulongT)mp_vector_copy,arg1,arg3,(ulongT)_cimg_mp_size(arg1)).
  16583. move_to(code);
  16584. else // From scalar
  16585. CImg<ulongT>::vector((ulongT)mp_vector_init,arg1,1,(ulongT)_cimg_mp_size(arg1),arg3).
  16586. move_to(code);
  16587. } else // Scalar
  16588. CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg3).move_to(code);
  16589. }
  16590. return_new_comp = false;
  16591. _cimg_mp_return(arg1);
  16592. }
  16593. // Assign lvalue (variable name was not valid for a direct assignment).
  16594. arg1 = ~0U;
  16595. is_sth = (bool)std::strchr(variable_name,'?'); // Contains_ternary_operator?
  16596. if (is_sth) break; // Do nothing and make ternary operator priority over assignment
  16597. if (l_variable_name>2 && (std::strchr(variable_name,'(') || std::strchr(variable_name,'['))) {
  16598. ref.assign(7);
  16599. arg1 = compile(ss,s,depth1,ref,bloc_flags); // Lvalue slot
  16600. arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
  16601. if (*ref==1) { // Vector value (scalar): V[k] = scalar
  16602. _cimg_mp_check_type(arg2,2,1,0);
  16603. arg3 = ref[1]; // Vector slot
  16604. arg4 = ref[2]; // Index
  16605. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16606. CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg2,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
  16607. move_to(code);
  16608. _cimg_mp_return(arg2);
  16609. }
  16610. if (*ref==2) { // Image value (scalar): i/j[_#ind,off] = scalar
  16611. if (!is_inside_critical) is_parallelizable = false;
  16612. _cimg_mp_check_type(arg2,2,1,0);
  16613. p1 = ref[1]; // Index
  16614. is_relative = (bool)ref[2];
  16615. arg3 = ref[3]; // Offset
  16616. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16617. if (p1!=~0U) {
  16618. if (!imglist) _cimg_mp_return(arg2);
  16619. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
  16620. arg2,p1,arg3).move_to(code);
  16621. } else {
  16622. if (!imgout) _cimg_mp_return(arg2);
  16623. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
  16624. arg2,arg3).move_to(code);
  16625. }
  16626. _cimg_mp_return(arg2);
  16627. }
  16628. if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c) = scalar
  16629. if (!is_inside_critical) is_parallelizable = false;
  16630. _cimg_mp_check_type(arg2,2,1,0);
  16631. p1 = ref[1]; // Index
  16632. is_relative = (bool)ref[2];
  16633. arg3 = ref[3]; // X
  16634. arg4 = ref[4]; // Y
  16635. arg5 = ref[5]; // Z
  16636. arg6 = ref[6]; // C
  16637. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16638. if (p1!=~0U) {
  16639. if (!imglist) _cimg_mp_return(arg2);
  16640. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
  16641. arg2,p1,arg3,arg4,arg5,arg6).move_to(code);
  16642. } else {
  16643. if (!imgout) _cimg_mp_return(arg2);
  16644. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
  16645. arg2,arg3,arg4,arg5,arg6).move_to(code);
  16646. }
  16647. _cimg_mp_return(arg2);
  16648. }
  16649. if (*ref==4) { // Image value (vector): I/J[_#ind,off] = value
  16650. if (!is_inside_critical) is_parallelizable = false;
  16651. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16652. p1 = ref[1]; // Index
  16653. is_relative = (bool)ref[2];
  16654. arg3 = ref[3]; // Offset
  16655. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16656. if (p1!=~0U) {
  16657. if (!imglist) _cimg_mp_return(arg2);
  16658. if (_cimg_mp_is_scalar(arg2))
  16659. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
  16660. arg2,p1,arg3).move_to(code);
  16661. else {
  16662. _cimg_mp_check_const_index(p1);
  16663. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  16664. arg2,p1,arg3,_cimg_mp_size(arg2)).move_to(code);
  16665. }
  16666. } else {
  16667. if (!imgout) _cimg_mp_return(arg2);
  16668. if (_cimg_mp_is_scalar(arg2))
  16669. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
  16670. arg2,arg3).move_to(code);
  16671. else
  16672. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  16673. arg2,arg3,_cimg_mp_size(arg2)).move_to(code);
  16674. }
  16675. _cimg_mp_return(arg2);
  16676. }
  16677. if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) = value
  16678. if (!is_inside_critical) is_parallelizable = false;
  16679. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16680. p1 = ref[1]; // Index
  16681. is_relative = (bool)ref[2];
  16682. arg3 = ref[3]; // X
  16683. arg4 = ref[4]; // Y
  16684. arg5 = ref[5]; // Z
  16685. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16686. if (p1!=~0U) {
  16687. if (!imglist) _cimg_mp_return(arg2);
  16688. if (_cimg_mp_is_scalar(arg2))
  16689. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
  16690. arg2,p1,arg3,arg4,arg5).move_to(code);
  16691. else {
  16692. _cimg_mp_check_const_index(p1);
  16693. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  16694. arg2,p1,arg3,arg4,arg5,_cimg_mp_size(arg2)).move_to(code);
  16695. }
  16696. } else {
  16697. if (!imgout) _cimg_mp_return(arg2);
  16698. if (_cimg_mp_is_scalar(arg2))
  16699. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
  16700. arg2,arg3,arg4,arg5).move_to(code);
  16701. else
  16702. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  16703. arg2,arg3,arg4,arg5,_cimg_mp_size(arg2)).move_to(code);
  16704. }
  16705. _cimg_mp_return(arg2);
  16706. }
  16707. if (_cimg_mp_is_vector(arg1)) { // Vector variable: V = value
  16708. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16709. if (_cimg_mp_is_vector(arg2)) // From vector
  16710. CImg<ulongT>::vector((ulongT)mp_vector_copy,arg1,arg2,(ulongT)_cimg_mp_size(arg1)).
  16711. move_to(code);
  16712. else // From scalar
  16713. CImg<ulongT>::vector((ulongT)mp_vector_init,arg1,1,(ulongT)_cimg_mp_size(arg1),arg2).
  16714. move_to(code);
  16715. _cimg_mp_return(arg1);
  16716. }
  16717. if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s = scalar
  16718. _cimg_mp_check_type(arg2,2,1,0);
  16719. CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg2).move_to(code);
  16720. _cimg_mp_return(arg1);
  16721. }
  16722. }
  16723. // No assignment expressions match -> error
  16724. _cimg_mp_strerr;
  16725. cimg::strellipsize(variable_name,64);
  16726. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16727. "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
  16728. "in expression '%s'.",
  16729. pixel_type(),_cimg_mp_calling_function,s_op,
  16730. arg1!=~0U && _cimg_mp_is_const_scalar(arg1)?"const ":"",
  16731. variable_name._data,s0);
  16732. }
  16733. // Apply unary/binary/ternary operators. The operator precedences should be the same as in C++.
  16734. for (s = se2, ps = se3, ns = ps - 1; s>ss1; --s, --ps, --ns) // Here, ns = ps - 1
  16735. if (*s=='=' && (*ps=='*' || *ps=='/' || *ps=='^') && *ns==*ps &&
  16736. level[s - expr._data]==clevel) { // Self-operators for complex numbers only (**=,//=,^^=)
  16737. _cimg_mp_op(*ps=='*'?"Operator '**='":*ps=='/'?"Operator '//='":"Operator '^^='");
  16738. ref.assign(7);
  16739. arg1 = compile(ss,ns,depth1,ref,bloc_flags); // Vector slot
  16740. arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Right operand
  16741. _cimg_mp_check_type(arg1,1,2,2);
  16742. _cimg_mp_check_type(arg2,2,3,2);
  16743. if (_cimg_mp_is_vector(arg2)) { // Complex **= complex
  16744. if (*ps=='*')
  16745. CImg<ulongT>::vector((ulongT)mp_complex_mul,arg1,arg1,arg2).move_to(code);
  16746. else if (*ps=='/')
  16747. CImg<ulongT>::vector((ulongT)mp_complex_div_vv,arg1,arg1,arg2).move_to(code);
  16748. else
  16749. CImg<ulongT>::vector((ulongT)mp_complex_pow_vv,arg1,arg1,arg2).move_to(code);
  16750. } else { // Complex **= scalar
  16751. if (*ps=='*') {
  16752. if (arg2==1) _cimg_mp_return(arg1);
  16753. self_vector_s(arg1,mp_self_mul,arg2);
  16754. } else if (*ps=='/') {
  16755. if (arg2==1) _cimg_mp_return(arg1);
  16756. self_vector_s(arg1,mp_self_div,arg2);
  16757. } else {
  16758. if (arg2==1) _cimg_mp_return(arg1);
  16759. CImg<ulongT>::vector((ulongT)mp_complex_pow_vs,arg1,arg1,arg2).move_to(code);
  16760. }
  16761. }
  16762. if (*ref==4) { // Image value (vector): I/J[_#ind,off] **= value
  16763. if (!is_inside_critical) is_parallelizable = false;
  16764. p1 = ref[1]; // Index
  16765. is_relative = (bool)ref[2];
  16766. arg3 = ref[3]; // Offset
  16767. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16768. if (p1!=~0U) {
  16769. if (!imglist) _cimg_mp_return(arg1);
  16770. _cimg_mp_check_const_index(p1);
  16771. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  16772. arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
  16773. } else {
  16774. if (!imgout) _cimg_mp_return(arg1);
  16775. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  16776. arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
  16777. }
  16778. } else if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) **= value
  16779. if (!is_inside_critical) is_parallelizable = false;
  16780. p1 = ref[1]; // Index
  16781. is_relative = (bool)ref[2];
  16782. arg3 = ref[3]; // X
  16783. arg4 = ref[4]; // Y
  16784. arg5 = ref[5]; // Z
  16785. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16786. if (p1!=~0U) {
  16787. if (!imglist) _cimg_mp_return(arg1);
  16788. _cimg_mp_check_const_index(p1);
  16789. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  16790. arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  16791. } else {
  16792. if (!imgout) _cimg_mp_return(arg1);
  16793. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  16794. arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  16795. }
  16796. }
  16797. _cimg_mp_return(arg1);
  16798. }
  16799. for (s = se2, ps = se3, ns = ps - 1; s>ss1; --s, --ps, --ns) // Here, ns = ps - 1
  16800. if (*s=='=' && (*ps=='+' || *ps=='-' || *ps=='*' || *ps=='/' || (*ps=='%' && s[1]!='=') ||
  16801. *ps=='&' || *ps=='^' || *ps=='|' ||
  16802. (*ps=='>' && *ns=='>') || (*ps=='<' && *ns=='<')) &&
  16803. level[s - expr._data]==clevel) { // Self-operators (+=,-=,*=,/=,%=,>>=,<<=,&=,^=,|=)
  16804. switch (*ps) {
  16805. case '+' : op = mp_self_add; _cimg_mp_op("Operator '+='"); break;
  16806. case '-' : op = mp_self_sub; _cimg_mp_op("Operator '-='"); break;
  16807. case '*' : op = mp_self_mul; _cimg_mp_op("Operator '*='"); break;
  16808. case '/' : op = mp_self_div; _cimg_mp_op("Operator '/='"); break;
  16809. case '%' : op = mp_self_modulo; _cimg_mp_op("Operator '%='"); break;
  16810. case '<' : op = mp_self_bitwise_left_shift; _cimg_mp_op("Operator '<<='"); break;
  16811. case '>' : op = mp_self_bitwise_right_shift; _cimg_mp_op("Operator '>>='"); break;
  16812. case '&' : op = mp_self_bitwise_and; _cimg_mp_op("Operator '&='"); break;
  16813. case '|' : op = mp_self_bitwise_or; _cimg_mp_op("Operator '|='"); break;
  16814. default : op = mp_self_pow; _cimg_mp_op("Operator '^='"); break;
  16815. }
  16816. s1 = *ps=='>' || *ps=='<'?ns:ps;
  16817. ref.assign(7);
  16818. arg1 = compile(ss,s1,depth1,ref,bloc_flags); // Variable slot
  16819. arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to apply
  16820. // Check for particular case to be simplified.
  16821. if ((op==mp_self_add || op==mp_self_sub) && !arg2) _cimg_mp_return(arg1);
  16822. if ((op==mp_self_mul || op==mp_self_div) && arg2==1) _cimg_mp_return(arg1);
  16823. // Apply operator on a copy to prevent modifying a constant or a variable.
  16824. if (*ref && (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_vector(arg1) || _cimg_mp_is_reserved(arg1))) {
  16825. if (_cimg_mp_is_vector(arg1)) arg1 = vector_copy(arg1);
  16826. else arg1 = scalar1(mp_copy,arg1);
  16827. }
  16828. if (*ref==1) { // Vector value (scalar): V[k] += scalar
  16829. _cimg_mp_check_type(arg2,2,1,0);
  16830. arg3 = ref[1]; // Vector slot
  16831. arg4 = ref[2]; // Index
  16832. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16833. CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
  16834. CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
  16835. move_to(code);
  16836. _cimg_mp_return(arg1);
  16837. }
  16838. if (*ref==2) { // Image value (scalar): i/j[_#ind,off] += scalar
  16839. if (!is_inside_critical) is_parallelizable = false;
  16840. _cimg_mp_check_type(arg2,2,1,0);
  16841. p1 = ref[1]; // Index
  16842. is_relative = (bool)ref[2];
  16843. arg3 = ref[3]; // Offset
  16844. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16845. CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
  16846. if (p1!=~0U) {
  16847. if (!imglist) _cimg_mp_return(arg1);
  16848. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
  16849. arg1,p1,arg3).move_to(code);
  16850. } else {
  16851. if (!imgout) _cimg_mp_return(arg1);
  16852. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
  16853. arg1,arg3).move_to(code);
  16854. }
  16855. _cimg_mp_return(arg1);
  16856. }
  16857. if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c) += scalar
  16858. if (!is_inside_critical) is_parallelizable = false;
  16859. _cimg_mp_check_type(arg2,2,1,0);
  16860. p1 = ref[1]; // Index
  16861. is_relative = (bool)ref[2];
  16862. arg3 = ref[3]; // X
  16863. arg4 = ref[4]; // Y
  16864. arg5 = ref[5]; // Z
  16865. arg6 = ref[6]; // C
  16866. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16867. CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
  16868. if (p1!=~0U) {
  16869. if (!imglist) _cimg_mp_return(arg1);
  16870. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
  16871. arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
  16872. } else {
  16873. if (!imgout) _cimg_mp_return(arg1);
  16874. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
  16875. arg1,arg3,arg4,arg5,arg6).move_to(code);
  16876. }
  16877. _cimg_mp_return(arg1);
  16878. }
  16879. if (*ref==4) { // Image value (vector): I/J[_#ind,off] += value
  16880. if (!is_inside_critical) is_parallelizable = false;
  16881. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16882. p1 = ref[1]; // Index
  16883. is_relative = (bool)ref[2];
  16884. arg3 = ref[3]; // Offset
  16885. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16886. if (_cimg_mp_is_scalar(arg2)) self_vector_s(arg1,op,arg2); else self_vector_v(arg1,op,arg2);
  16887. if (p1!=~0U) {
  16888. if (!imglist) _cimg_mp_return(arg1);
  16889. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  16890. arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
  16891. } else {
  16892. if (!imgout) _cimg_mp_return(arg1);
  16893. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  16894. arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
  16895. }
  16896. _cimg_mp_return(arg1);
  16897. }
  16898. if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) += value
  16899. if (!is_inside_critical) is_parallelizable = false;
  16900. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16901. p1 = ref[1]; // Index
  16902. is_relative = (bool)ref[2];
  16903. arg3 = ref[3]; // X
  16904. arg4 = ref[4]; // Y
  16905. arg5 = ref[5]; // Z
  16906. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  16907. if (_cimg_mp_is_scalar(arg2)) self_vector_s(arg1,op,arg2); else self_vector_v(arg1,op,arg2);
  16908. if (p1!=~0U) {
  16909. if (!imglist) _cimg_mp_return(arg1);
  16910. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  16911. arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  16912. } else {
  16913. if (!imgout) _cimg_mp_return(arg1);
  16914. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  16915. arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  16916. }
  16917. _cimg_mp_return(arg1);
  16918. }
  16919. if (_cimg_mp_is_vector(arg1)) { // Vector variable: V += value
  16920. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  16921. if (_cimg_mp_is_vector(arg2)) self_vector_v(arg1,op,arg2); // Vector += vector
  16922. else self_vector_s(arg1,op,arg2); // Vector += scalar
  16923. _cimg_mp_return(arg1);
  16924. }
  16925. if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s += scalar
  16926. _cimg_mp_check_type(arg2,2,1,0);
  16927. CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
  16928. _cimg_mp_return(arg1);
  16929. }
  16930. variable_name.assign(ss,(unsigned int)(s - ss)).back() = 0;
  16931. cimg::strpare(variable_name,false,true);
  16932. _cimg_mp_strerr;
  16933. cimg::strellipsize(variable_name,64);
  16934. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  16935. "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
  16936. "in expression '%s'.",
  16937. pixel_type(),_cimg_mp_calling_function,s_op,
  16938. _cimg_mp_is_const_scalar(arg1)?"const ":"",
  16939. variable_name._data,s0);
  16940. }
  16941. for (s = ss1; s<se1; ++s)
  16942. if (*s=='?' && level[s - expr._data]==clevel) { // Ternary operator 'cond?expr1:expr2'
  16943. _cimg_mp_op("Operator '?:'");
  16944. s1 = s + 1; while (s1<se1 && (*s1!=':' || level[s1 - expr._data]!=clevel)) ++s1;
  16945. arg1 = compile(ss,s,depth1,0,bloc_flags);
  16946. _cimg_mp_check_type(arg1,1,1,0);
  16947. if (_cimg_mp_is_const_scalar(arg1)) {
  16948. if ((bool)mem[arg1]) return compile(s + 1,*s1!=':'?se:s1,depth1,0,bloc_flags);
  16949. else return *s1!=':'?0:compile(++s1,se,depth1,0,bloc_flags);
  16950. }
  16951. p2 = code._width;
  16952. arg2 = compile(s + 1,*s1!=':'?se:s1,depth1,0,bloc_flags);
  16953. p3 = code._width;
  16954. arg3 = *s1==':'?compile(++s1,se,depth1,0,bloc_flags):
  16955. _cimg_mp_is_vector(arg2)?vector(_cimg_mp_size(arg2),0):0;
  16956. _cimg_mp_check_type(arg3,3,_cimg_mp_is_vector(arg2)?2:1,_cimg_mp_size(arg2));
  16957. arg4 = _cimg_mp_size(arg2);
  16958. if (arg4) pos = vector(arg4); else pos = scalar();
  16959. CImg<ulongT>::vector((ulongT)mp_if,pos,arg1,arg2,arg3,
  16960. p3 - p2,code._width - p3,arg4).move_to(code,p2);
  16961. return_new_comp = true;
  16962. _cimg_mp_return(pos);
  16963. }
  16964. for (s = se3, ns = se2; s>ss; --s, --ns)
  16965. if (*s=='|' && *ns=='|' && level[s - expr._data]==clevel) { // Logical or ('||')
  16966. _cimg_mp_op("Operator '||'");
  16967. arg1 = compile(ss,s,depth1,0,bloc_flags);
  16968. _cimg_mp_check_type(arg1,1,1,0);
  16969. if (arg1>0 && arg1<=16) _cimg_mp_return(1);
  16970. p2 = code._width;
  16971. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  16972. _cimg_mp_check_type(arg2,2,1,0);
  16973. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  16974. _cimg_mp_const_scalar(mem[arg1] || mem[arg2]);
  16975. if (!arg1) _cimg_mp_return(arg2);
  16976. pos = scalar();
  16977. CImg<ulongT>::vector((ulongT)mp_logical_or,pos,arg1,arg2,code._width - p2).
  16978. move_to(code,p2);
  16979. return_new_comp = true;
  16980. _cimg_mp_return(pos);
  16981. }
  16982. for (s = se3, ns = se2; s>ss; --s, --ns)
  16983. if (*s=='&' && *ns=='&' && level[s - expr._data]==clevel) { // Logical and ('&&')
  16984. _cimg_mp_op("Operator '&&'");
  16985. arg1 = compile(ss,s,depth1,0,bloc_flags);
  16986. _cimg_mp_check_type(arg1,1,1,0);
  16987. if (!arg1) _cimg_mp_return(0);
  16988. p2 = code._width;
  16989. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  16990. _cimg_mp_check_type(arg2,2,1,0);
  16991. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  16992. _cimg_mp_const_scalar(mem[arg1] && mem[arg2]);
  16993. if (arg1>0 && arg1<=16) _cimg_mp_return(arg2);
  16994. pos = scalar();
  16995. CImg<ulongT>::vector((ulongT)mp_logical_and,pos,arg1,arg2,code._width - p2).
  16996. move_to(code,p2);
  16997. return_new_comp = true;
  16998. _cimg_mp_return(pos);
  16999. }
  17000. for (s = se2; s>ss; --s)
  17001. if (*s=='|' && level[s - expr._data]==clevel) { // Bitwise or ('|')
  17002. _cimg_mp_op("Operator '|'");
  17003. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17004. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17005. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17006. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_or,arg1,arg2);
  17007. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
  17008. if (!arg2) _cimg_mp_return(arg1);
  17009. _cimg_mp_vector2_vs(mp_bitwise_or,arg1,arg2);
  17010. }
  17011. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
  17012. if (!arg1) _cimg_mp_return(arg2);
  17013. _cimg_mp_vector2_sv(mp_bitwise_or,arg1,arg2);
  17014. }
  17015. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17016. _cimg_mp_const_scalar((longT)mem[arg1] | (longT)mem[arg2]);
  17017. if (!arg2) _cimg_mp_return(arg1);
  17018. if (!arg1) _cimg_mp_return(arg2);
  17019. _cimg_mp_scalar2(mp_bitwise_or,arg1,arg2);
  17020. }
  17021. for (s = se2; s>ss; --s)
  17022. if (*s=='&' && level[s - expr._data]==clevel) { // Bitwise and ('&')
  17023. _cimg_mp_op("Operator '&'");
  17024. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17025. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17026. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17027. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_and,arg1,arg2);
  17028. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_bitwise_and,arg1,arg2);
  17029. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_bitwise_and,arg1,arg2);
  17030. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17031. _cimg_mp_const_scalar((longT)mem[arg1] & (longT)mem[arg2]);
  17032. if (!arg1 || !arg2) _cimg_mp_return(0);
  17033. _cimg_mp_scalar2(mp_bitwise_and,arg1,arg2);
  17034. }
  17035. for (s = se3, ns = se2; s>ss; --s, --ns)
  17036. if (*s=='!' && *ns=='=' && level[s - expr._data]==clevel) { // Not equal to ('!=')
  17037. _cimg_mp_op("Operator '!='");
  17038. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17039. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17040. if (arg1==arg2) _cimg_mp_return(0);
  17041. p1 = _cimg_mp_size(arg1);
  17042. p2 = _cimg_mp_size(arg2);
  17043. if (p1 || p2) {
  17044. if (p1 && p2 && p1!=p2) _cimg_mp_return(1);
  17045. pos = scalar();
  17046. CImg<ulongT>::vector((ulongT)mp_vector_neq,pos,arg1,p1,arg2,p2,11,1).move_to(code);
  17047. return_new_comp = true;
  17048. _cimg_mp_return(pos);
  17049. }
  17050. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17051. _cimg_mp_const_scalar(mem[arg1]!=mem[arg2]);
  17052. _cimg_mp_scalar2(mp_neq,arg1,arg2);
  17053. }
  17054. for (s = se3, ns = se2; s>ss; --s, --ns)
  17055. if (*s=='=' && *ns=='=' && level[s - expr._data]==clevel) { // Equal to ('==')
  17056. _cimg_mp_op("Operator '=='");
  17057. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17058. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17059. if (arg1==arg2) _cimg_mp_return(1);
  17060. p1 = _cimg_mp_size(arg1);
  17061. p2 = _cimg_mp_size(arg2);
  17062. if (p1 || p2) {
  17063. if (p1 && p2 && p1!=p2) _cimg_mp_return(0);
  17064. pos = scalar();
  17065. CImg<ulongT>::vector((ulongT)mp_vector_eq,pos,arg1,p1,arg2,p2,11,1).move_to(code);
  17066. return_new_comp = true;
  17067. _cimg_mp_return(pos);
  17068. }
  17069. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17070. _cimg_mp_const_scalar(mem[arg1]==mem[arg2]);
  17071. _cimg_mp_scalar2(mp_eq,arg1,arg2);
  17072. }
  17073. for (s = se3, ns = se2; s>ss; --s, --ns)
  17074. if (*s=='<' && *ns=='=' && level[s - expr._data]==clevel) { // Less or equal than ('<=')
  17075. _cimg_mp_op("Operator '<='");
  17076. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17077. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17078. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17079. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_lte,arg1,arg2);
  17080. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_lte,arg1,arg2);
  17081. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_lte,arg1,arg2);
  17082. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17083. _cimg_mp_const_scalar(mem[arg1]<=mem[arg2]);
  17084. if (arg1==arg2) _cimg_mp_return(1);
  17085. _cimg_mp_scalar2(mp_lte,arg1,arg2);
  17086. }
  17087. for (s = se3, ns = se2; s>ss; --s, --ns)
  17088. if (*s=='>' && *ns=='=' && level[s - expr._data]==clevel) { // Greater or equal than ('>=')
  17089. _cimg_mp_op("Operator '>='");
  17090. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17091. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17092. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17093. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_gte,arg1,arg2);
  17094. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_gte,arg1,arg2);
  17095. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_gte,arg1,arg2);
  17096. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17097. _cimg_mp_const_scalar(mem[arg1]>=mem[arg2]);
  17098. if (arg1==arg2) _cimg_mp_return(1);
  17099. _cimg_mp_scalar2(mp_gte,arg1,arg2);
  17100. }
  17101. for (s = se2, ns = se1, ps = se3; s>ss; --s, --ns, --ps)
  17102. if (*s=='<' && *ns!='<' && *ps!='<' && level[s - expr._data]==clevel) { // Less than ('<')
  17103. _cimg_mp_op("Operator '<'");
  17104. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17105. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17106. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17107. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_lt,arg1,arg2);
  17108. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_lt,arg1,arg2);
  17109. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_lt,arg1,arg2);
  17110. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17111. _cimg_mp_const_scalar(mem[arg1]<mem[arg2]);
  17112. if (arg1==arg2) _cimg_mp_return(0);
  17113. _cimg_mp_scalar2(mp_lt,arg1,arg2);
  17114. }
  17115. for (s = se2, ns = se1, ps = se3; s>ss; --s, --ns, --ps)
  17116. if (*s=='>' && *ns!='>' && *ps!='>' && level[s - expr._data]==clevel) { // Greater than ('>')
  17117. _cimg_mp_op("Operator '>'");
  17118. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17119. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17120. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17121. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_gt,arg1,arg2);
  17122. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_gt,arg1,arg2);
  17123. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_gt,arg1,arg2);
  17124. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17125. _cimg_mp_const_scalar(mem[arg1]>mem[arg2]);
  17126. if (arg1==arg2) _cimg_mp_return(0);
  17127. _cimg_mp_scalar2(mp_gt,arg1,arg2);
  17128. }
  17129. for (s = se3, ns = se2; s>ss; --s, --ns)
  17130. if (*s=='<' && *ns=='<' && level[s - expr._data]==clevel) { // Left bit shift ('<<')
  17131. _cimg_mp_op("Operator '<<'");
  17132. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17133. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17134. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17135. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
  17136. _cimg_mp_vector2_vv(mp_bitwise_left_shift,arg1,arg2);
  17137. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
  17138. if (!arg2) _cimg_mp_return(arg1);
  17139. _cimg_mp_vector2_vs(mp_bitwise_left_shift,arg1,arg2);
  17140. }
  17141. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
  17142. _cimg_mp_vector2_sv(mp_bitwise_left_shift,arg1,arg2);
  17143. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17144. _cimg_mp_const_scalar((longT)mem[arg1]<<(unsigned int)mem[arg2]);
  17145. if (!arg1) _cimg_mp_return(0);
  17146. if (!arg2) _cimg_mp_return(arg1);
  17147. _cimg_mp_scalar2(mp_bitwise_left_shift,arg1,arg2);
  17148. }
  17149. for (s = se3, ns = se2; s>ss; --s, --ns)
  17150. if (*s=='>' && *ns=='>' && level[s - expr._data]==clevel) { // Right bit shift ('>>')
  17151. _cimg_mp_op("Operator '>>'");
  17152. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17153. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17154. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17155. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
  17156. _cimg_mp_vector2_vv(mp_bitwise_right_shift,arg1,arg2);
  17157. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
  17158. if (!arg2) _cimg_mp_return(arg1);
  17159. _cimg_mp_vector2_vs(mp_bitwise_right_shift,arg1,arg2);
  17160. }
  17161. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
  17162. _cimg_mp_vector2_sv(mp_bitwise_right_shift,arg1,arg2);
  17163. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17164. _cimg_mp_const_scalar((longT)mem[arg1]>>(unsigned int)mem[arg2]);
  17165. if (!arg1) _cimg_mp_return(0);
  17166. if (!arg2) _cimg_mp_return(arg1);
  17167. _cimg_mp_scalar2(mp_bitwise_right_shift,arg1,arg2);
  17168. }
  17169. for (ns = se1, s = se2, ps = pexpr._data + (se3 - expr._data); s>ss; --ns, --s, --ps)
  17170. if (*s=='+' && (*ns!='+' || ns!=se1) && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' &&
  17171. *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && *ps!='#' &&
  17172. (*ps!='e' || !(ps - pexpr._data>ss - expr._data && (*(ps - 1)=='.' || (*(ps - 1)>='0' &&
  17173. *(ps - 1)<='9')))) &&
  17174. level[s - expr._data]==clevel) { // Addition ('+')
  17175. _cimg_mp_op("Operator '+'");
  17176. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17177. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17178. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17179. if (!arg2) _cimg_mp_return(arg1);
  17180. if (!arg1) _cimg_mp_return(arg2);
  17181. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_add,arg1,arg2);
  17182. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_add,arg1,arg2);
  17183. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_add,arg1,arg2);
  17184. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17185. _cimg_mp_const_scalar(mem[arg1] + mem[arg2]);
  17186. if (code) { // Try to spot linear case 'a*b + c'
  17187. CImg<ulongT> &pop = code.back();
  17188. if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
  17189. arg3 = (unsigned int)pop[1];
  17190. arg4 = (unsigned int)pop[2];
  17191. arg5 = (unsigned int)pop[3];
  17192. code.remove();
  17193. CImg<ulongT>::vector((ulongT)mp_linear_add,arg3,arg4,arg5,arg3==arg2?arg1:arg2).move_to(code);
  17194. _cimg_mp_return(arg3);
  17195. }
  17196. }
  17197. if (arg2==1) _cimg_mp_scalar1(mp_increment,arg1);
  17198. if (arg1==1) _cimg_mp_scalar1(mp_increment,arg2);
  17199. _cimg_mp_scalar2(mp_add,arg1,arg2);
  17200. }
  17201. for (ns = se1, s = se2, ps = pexpr._data + (se3 - expr._data); s>ss; --ns, --s, --ps)
  17202. if (*s=='-' && (*ns!='-' || ns!=se1) && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' &&
  17203. *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && *ps!='#' &&
  17204. (*ps!='e' || !(ps - pexpr._data>ss - expr._data && (*(ps - 1)=='.' || (*(ps - 1)>='0' &&
  17205. *(ps - 1)<='9')))) &&
  17206. level[s - expr._data]==clevel) { // Subtraction ('-')
  17207. _cimg_mp_op("Operator '-'");
  17208. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17209. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17210. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17211. if (!arg2) _cimg_mp_return(arg1);
  17212. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_sub,arg1,arg2);
  17213. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_sub,arg1,arg2);
  17214. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
  17215. if (!arg1) _cimg_mp_vector1_v(mp_minus,arg2);
  17216. _cimg_mp_vector2_sv(mp_sub,arg1,arg2);
  17217. }
  17218. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17219. _cimg_mp_const_scalar(mem[arg1] - mem[arg2]);
  17220. if (!arg1) _cimg_mp_scalar1(mp_minus,arg2);
  17221. if (code) { // Try to spot linear cases 'a*b - c' and 'c - a*b'
  17222. CImg<ulongT> &pop = code.back();
  17223. if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
  17224. arg3 = (unsigned int)pop[1];
  17225. arg4 = (unsigned int)pop[2];
  17226. arg5 = (unsigned int)pop[3];
  17227. code.remove();
  17228. CImg<ulongT>::vector((ulongT)(arg3==arg1?mp_linear_sub_left:mp_linear_sub_right),
  17229. arg3,arg4,arg5,arg3==arg1?arg2:arg1).move_to(code);
  17230. _cimg_mp_return(arg3);
  17231. }
  17232. }
  17233. if (arg2==1) _cimg_mp_scalar1(mp_decrement,arg1);
  17234. _cimg_mp_scalar2(mp_sub,arg1,arg2);
  17235. }
  17236. for (s = se3, ns = se2; s>ss; --s, --ns)
  17237. if (*s=='*' && *ns=='*' && level[s - expr._data]==clevel) { // Complex multiplication ('**')
  17238. _cimg_mp_op("Operator '**'");
  17239. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17240. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17241. _cimg_mp_check_type(arg1,1,3,2);
  17242. _cimg_mp_check_type(arg2,2,3,2);
  17243. if (arg2==1) _cimg_mp_return(arg1);
  17244. if (arg1==1) _cimg_mp_return(arg2);
  17245. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) {
  17246. pos = vector(2);
  17247. CImg<ulongT>::vector((ulongT)mp_complex_mul,pos,arg1,arg2).move_to(code);
  17248. return_new_comp = true;
  17249. _cimg_mp_return(pos);
  17250. }
  17251. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_mul,arg1,arg2);
  17252. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_mul,arg1,arg2);
  17253. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17254. _cimg_mp_const_scalar(mem[arg1]*mem[arg2]);
  17255. if (!arg1 || !arg2) _cimg_mp_return(0);
  17256. _cimg_mp_scalar2(mp_mul,arg1,arg2);
  17257. }
  17258. for (s = se3, ns = se2; s>ss; --s, --ns)
  17259. if (*s=='/' && *ns=='/' && level[s - expr._data]==clevel) { // Complex division ('//')
  17260. _cimg_mp_op("Operator '//'");
  17261. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17262. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17263. _cimg_mp_check_type(arg1,1,3,2);
  17264. _cimg_mp_check_type(arg2,2,3,2);
  17265. if (arg2==1) _cimg_mp_return(arg1);
  17266. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) {
  17267. pos = vector(2);
  17268. CImg<ulongT>::vector((ulongT)mp_complex_div_vv,pos,arg1,arg2).move_to(code);
  17269. return_new_comp = true;
  17270. _cimg_mp_return(pos);
  17271. }
  17272. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
  17273. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
  17274. pos = vector(2);
  17275. CImg<ulongT>::vector((ulongT)mp_complex_div_sv,pos,arg1,arg2).move_to(code);
  17276. return_new_comp = true;
  17277. _cimg_mp_return(pos);
  17278. }
  17279. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17280. _cimg_mp_const_scalar(mem[arg1]/mem[arg2]);
  17281. if (!arg1) _cimg_mp_return(0);
  17282. _cimg_mp_scalar2(mp_div,arg1,arg2);
  17283. }
  17284. for (s = se2; s>ss; --s) if (*s=='*' && level[s - expr._data]==clevel) { // Multiplication ('*')
  17285. _cimg_mp_op("Operator '*'");
  17286. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17287. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17288. p2 = _cimg_mp_size(arg2);
  17289. if (p2>0 && (ulongT)_cimg_mp_size(arg1)==(ulongT)p2*p2) { // Particular case of matrix multiplication
  17290. pos = vector(p2);
  17291. CImg<ulongT>::vector((ulongT)mp_matrix_mul,pos,arg1,arg2,p2,p2,1).move_to(code);
  17292. return_new_comp = true;
  17293. _cimg_mp_return(pos);
  17294. }
  17295. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17296. if (arg2==1) _cimg_mp_return(arg1);
  17297. if (arg1==1) _cimg_mp_return(arg2);
  17298. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_mul,arg1,arg2);
  17299. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_mul,arg1,arg2);
  17300. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_mul,arg1,arg2);
  17301. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17302. _cimg_mp_const_scalar(mem[arg1]*mem[arg2]);
  17303. if (code) { // Try to spot double multiplication 'a*b*c'
  17304. CImg<ulongT> &pop = code.back();
  17305. if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
  17306. arg3 = (unsigned int)pop[1];
  17307. arg4 = (unsigned int)pop[2];
  17308. arg5 = (unsigned int)pop[3];
  17309. code.remove();
  17310. CImg<ulongT>::vector((ulongT)mp_mul2,arg3,arg4,arg5,arg3==arg2?arg1:arg2).move_to(code);
  17311. _cimg_mp_return(arg3);
  17312. }
  17313. }
  17314. if (!arg1 || !arg2) _cimg_mp_return(0);
  17315. _cimg_mp_scalar2(mp_mul,arg1,arg2);
  17316. }
  17317. for (s = se2; s>ss; --s) if (*s=='/' && level[s - expr._data]==clevel) { // Division ('/')
  17318. _cimg_mp_op("Operator '/'");
  17319. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17320. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17321. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17322. if (arg2==1) _cimg_mp_return(arg1);
  17323. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_div,arg1,arg2);
  17324. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
  17325. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_div,arg1,arg2);
  17326. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17327. _cimg_mp_const_scalar(mem[arg1]/mem[arg2]);
  17328. if (!arg1) _cimg_mp_return(0);
  17329. _cimg_mp_scalar2(mp_div,arg1,arg2);
  17330. }
  17331. for (s = se2, ns = se1; s>ss; --s, --ns)
  17332. if (*s=='%' && *ns!='^' && level[s - expr._data]==clevel) { // Modulo ('%')
  17333. _cimg_mp_op("Operator '%'");
  17334. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17335. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17336. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17337. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_modulo,arg1,arg2);
  17338. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_modulo,arg1,arg2);
  17339. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_modulo,arg1,arg2);
  17340. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17341. _cimg_mp_const_scalar(cimg::mod(mem[arg1],mem[arg2]));
  17342. _cimg_mp_scalar2(mp_modulo,arg1,arg2);
  17343. }
  17344. if (se1>ss) {
  17345. if (*ss=='+' && (*ss1!='+' || (ss2<se && *ss2>='0' && *ss2<='9'))) { // Unary plus ('+')
  17346. _cimg_mp_op("Operator '+'");
  17347. _cimg_mp_return(compile(ss1,se,depth1,0,bloc_flags));
  17348. }
  17349. if (*ss=='-' && (*ss1!='-' || (ss2<se && *ss2>='0' && *ss2<='9'))) { // Unary minus ('-')
  17350. _cimg_mp_op("Operator '-'");
  17351. arg1 = compile(ss1,se,depth1,0,bloc_flags);
  17352. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_minus,arg1);
  17353. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(-mem[arg1]);
  17354. _cimg_mp_scalar1(mp_minus,arg1);
  17355. }
  17356. if (*ss=='!') { // Logical not ('!')
  17357. _cimg_mp_op("Operator '!'");
  17358. if (*ss1=='!') { // '!!expr' optimized as 'bool(expr)'
  17359. arg1 = compile(ss2,se,depth1,0,bloc_flags);
  17360. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bool,arg1);
  17361. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((bool)mem[arg1]);
  17362. _cimg_mp_scalar1(mp_bool,arg1);
  17363. }
  17364. arg1 = compile(ss1,se,depth1,0,bloc_flags);
  17365. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_logical_not,arg1);
  17366. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(!mem[arg1]);
  17367. _cimg_mp_scalar1(mp_logical_not,arg1);
  17368. }
  17369. if (*ss=='~') { // Bitwise not ('~')
  17370. _cimg_mp_op("Operator '~'");
  17371. arg1 = compile(ss1,se,depth1,0,bloc_flags);
  17372. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bitwise_not,arg1);
  17373. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(~(unsigned int)mem[arg1]);
  17374. _cimg_mp_scalar1(mp_bitwise_not,arg1);
  17375. }
  17376. }
  17377. for (s = se3, ns = se2; s>ss; --s, --ns)
  17378. if (*s=='^' && *ns=='^' && level[s - expr._data]==clevel) { // Complex power ('^^')
  17379. _cimg_mp_op("Operator '^^'");
  17380. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17381. arg2 = compile(s + 2,se,depth1,0,bloc_flags);
  17382. _cimg_mp_check_type(arg1,1,3,2);
  17383. _cimg_mp_check_type(arg2,2,3,2);
  17384. if (arg2==1) _cimg_mp_return(arg1);
  17385. pos = vector(2);
  17386. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
  17387. CImg<ulongT>::vector((ulongT)mp_complex_pow_vv,pos,arg1,arg2).move_to(code);
  17388. else if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2))
  17389. CImg<ulongT>::vector((ulongT)mp_complex_pow_vs,pos,arg1,arg2).move_to(code);
  17390. else if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
  17391. CImg<ulongT>::vector((ulongT)mp_complex_pow_sv,pos,arg1,arg2).move_to(code);
  17392. else
  17393. CImg<ulongT>::vector((ulongT)mp_complex_pow_ss,pos,arg1,arg2).move_to(code);
  17394. return_new_comp = true;
  17395. _cimg_mp_return(pos);
  17396. }
  17397. for (s = se2; s>ss; --s)
  17398. if (*s=='^' && level[s - expr._data]==clevel) { // Power ('^')
  17399. _cimg_mp_op("Operator '^'");
  17400. arg1 = compile(ss,s,depth1,0,bloc_flags);
  17401. arg2 = compile(s + 1,se,depth1,0,bloc_flags);
  17402. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17403. if (arg2==1) _cimg_mp_return(arg1);
  17404. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_pow,arg1,arg2);
  17405. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_pow,arg1,arg2);
  17406. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_pow,arg1,arg2);
  17407. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17408. _cimg_mp_const_scalar(std::pow(mem[arg1],mem[arg2]));
  17409. switch (arg2) {
  17410. case 0 : _cimg_mp_return(1);
  17411. case 2 : _cimg_mp_scalar1(mp_sqr,arg1);
  17412. case 3 : _cimg_mp_scalar1(mp_pow3,arg1);
  17413. case 4 : _cimg_mp_scalar1(mp_pow4,arg1);
  17414. default :
  17415. if (_cimg_mp_is_const_scalar(arg2)) {
  17416. if (mem[arg2]==0.5) { _cimg_mp_scalar1(mp_sqrt,arg1); }
  17417. else if (mem[arg2]==0.25) { _cimg_mp_scalar1(mp_pow0_25,arg1); }
  17418. }
  17419. _cimg_mp_scalar2(mp_pow,arg1,arg2);
  17420. }
  17421. }
  17422. // Percentage computation.
  17423. if (*se1=='%') {
  17424. arg1 = compile(ss,se1,depth1,0,bloc_flags);
  17425. arg2 = _cimg_mp_is_const_scalar(arg1)?0:const_scalar(100);
  17426. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
  17427. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]/100);
  17428. _cimg_mp_scalar2(mp_div,arg1,arg2);
  17429. }
  17430. // Degree to radian postfix operator ('°' in UTF-8).
  17431. if (se2>ss && (unsigned char)*se2==0xC2 && (unsigned char)*se1==0xB0) {
  17432. arg1 = compile(ss,se2,depth1,0,bloc_flags);
  17433. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_deg2rad,arg1);
  17434. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*cimg::PI/180);
  17435. _cimg_mp_scalar1(mp_deg2rad,arg1);
  17436. }
  17437. // Pre/post-decrement and increment.
  17438. is_sth = ss1<se1 && (*ss=='+' || *ss=='-') && *ss1==*ss; // is pre-?
  17439. if (is_sth || (se2>ss && (*se1=='+' || *se1=='-') && *se2==*se1)) {
  17440. if ((is_sth && *ss=='+') || (!is_sth && *se1=='+')) {
  17441. _cimg_mp_op("Operator '++'");
  17442. op = mp_self_increment;
  17443. } else {
  17444. _cimg_mp_op("Operator '--'");
  17445. op = mp_self_decrement;
  17446. }
  17447. ref.assign(7);
  17448. arg1 = is_sth?compile(ss2,se,depth1,ref,bloc_flags):
  17449. compile(ss,se2,depth1,ref,bloc_flags); // Variable slot
  17450. // Apply operator on a copy to prevent modifying a constant or a variable.
  17451. if (*ref && (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_vector(arg1) || _cimg_mp_is_reserved(arg1))) {
  17452. if (_cimg_mp_is_vector(arg1)) arg1 = vector_copy(arg1);
  17453. else arg1 = scalar1(mp_copy,arg1);
  17454. }
  17455. if (is_sth) pos = arg1; // Determine return index, depending on pre/post action
  17456. else {
  17457. if (_cimg_mp_is_vector(arg1)) pos = vector_copy(arg1);
  17458. else pos = scalar1(mp_copy,arg1);
  17459. }
  17460. if (*ref==1) { // Vector value (scalar): V[k]++
  17461. arg3 = ref[1]; // Vector slot
  17462. arg4 = ref[2]; // Index
  17463. if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
  17464. CImg<ulongT>::vector((ulongT)op,arg1,1).move_to(code);
  17465. CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
  17466. move_to(code);
  17467. _cimg_mp_return(pos);
  17468. }
  17469. if (*ref==2) { // Image value (scalar): i/j[_#ind,off]++
  17470. if (!is_inside_critical) is_parallelizable = false;
  17471. p1 = ref[1]; // Index
  17472. is_relative = (bool)ref[2];
  17473. arg3 = ref[3]; // Offset
  17474. if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
  17475. CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
  17476. if (p1!=~0U) {
  17477. if (!imglist) _cimg_mp_return(pos);
  17478. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
  17479. arg1,p1,arg3).move_to(code);
  17480. } else {
  17481. if (!imgout) _cimg_mp_return(pos);
  17482. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
  17483. arg1,arg3).move_to(code);
  17484. }
  17485. _cimg_mp_return(pos);
  17486. }
  17487. if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c)++
  17488. if (!is_inside_critical) is_parallelizable = false;
  17489. p1 = ref[1]; // Index
  17490. is_relative = (bool)ref[2];
  17491. arg3 = ref[3]; // X
  17492. arg4 = ref[4]; // Y
  17493. arg5 = ref[5]; // Z
  17494. arg6 = ref[6]; // C
  17495. if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
  17496. CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
  17497. if (p1!=~0U) {
  17498. if (!imglist) _cimg_mp_return(pos);
  17499. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
  17500. arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
  17501. } else {
  17502. if (!imgout) _cimg_mp_return(pos);
  17503. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
  17504. arg1,arg3,arg4,arg5,arg6).move_to(code);
  17505. }
  17506. _cimg_mp_return(pos);
  17507. }
  17508. if (*ref==4) { // Image value (vector): I/J[_#ind,off]++
  17509. if (!is_inside_critical) is_parallelizable = false;
  17510. p1 = ref[1]; // Index
  17511. is_relative = (bool)ref[2];
  17512. arg3 = ref[3]; // Offset
  17513. if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
  17514. self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
  17515. if (p1!=~0U) {
  17516. if (!imglist) _cimg_mp_return(pos);
  17517. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  17518. arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
  17519. } else {
  17520. if (!imgout) _cimg_mp_return(pos);
  17521. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  17522. arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
  17523. }
  17524. _cimg_mp_return(pos);
  17525. }
  17526. if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c)++
  17527. if (!is_inside_critical) is_parallelizable = false;
  17528. p1 = ref[1]; // Index
  17529. is_relative = (bool)ref[2];
  17530. arg3 = ref[3]; // X
  17531. arg4 = ref[4]; // Y
  17532. arg5 = ref[5]; // Z
  17533. if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
  17534. self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
  17535. if (p1!=~0U) {
  17536. if (!imglist) _cimg_mp_return(pos);
  17537. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  17538. arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  17539. } else {
  17540. if (!imgout) _cimg_mp_return(pos);
  17541. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  17542. arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  17543. }
  17544. _cimg_mp_return(pos);
  17545. }
  17546. if (_cimg_mp_is_vector(arg1)) { // Vector variable: V++
  17547. self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
  17548. _cimg_mp_return(pos);
  17549. }
  17550. if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s++
  17551. CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
  17552. _cimg_mp_return(pos);
  17553. }
  17554. if (is_sth) variable_name.assign(ss2,(unsigned int)(se - ss1));
  17555. else variable_name.assign(ss,(unsigned int)(se1 - ss));
  17556. variable_name.back() = 0;
  17557. cimg::strpare(variable_name,false,true);
  17558. _cimg_mp_strerr;
  17559. cimg::strellipsize(variable_name,64);
  17560. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  17561. "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
  17562. "in expression '%s'.",
  17563. pixel_type(),_cimg_mp_calling_function,s_op,
  17564. _cimg_mp_is_const_scalar(arg1)?"const ":"",
  17565. variable_name._data,s0);
  17566. }
  17567. // Array-like access to vectors and image values 'i/j/I/J[_#ind,offset,_boundary]' and 'vector[offset]'.
  17568. if (*se1==']') {
  17569. _cimg_mp_op("Value accessor '[]'");
  17570. // Find opening bracket for the offset.
  17571. s0 = se1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
  17572. if (s0>ss) { s1 = s0; do { --s1; } while (cimg::is_blank(*s1)); cimg::swap(*s0,*++s1); }
  17573. is_sth=s0>ss && *(s0-1)==']'; // Particular case s.a. '..[..][..]' ?
  17574. is_relative = *ss=='j' || *ss=='J';
  17575. if (!is_sth && (*ss=='I' || *ss=='J') && *ss1=='[' &&
  17576. (reserved_label[(int)*ss]==~0U ||
  17577. !_cimg_mp_is_vector(reserved_label[(int)*ss]))) { // Image value as a vector
  17578. if (*ss2=='#') { // Index specified
  17579. s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  17580. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  17581. _cimg_mp_check_const_index(p1);
  17582. _cimg_mp_check_list();
  17583. } else { p1 = ~0U; s0 = ss2; }
  17584. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17585. p2 = 1 + (p1!=~0U);
  17586. arg1 = compile(s0,s1,depth1,0,bloc_flags); // Offset
  17587. _cimg_mp_check_type(arg1,p2,1,0);
  17588. arg2 = ~0U;
  17589. if (s1<se1) {
  17590. arg2 = compile(++s1,se1,depth1,0,bloc_flags); // Boundary
  17591. _cimg_mp_check_type(arg2,p2 + 1,1,0);
  17592. }
  17593. if (p_ref && arg2==~0U) {
  17594. *p_ref = 4;
  17595. p_ref[1] = p1;
  17596. p_ref[2] = (unsigned int)is_relative;
  17597. p_ref[3] = arg1;
  17598. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  17599. }
  17600. p2 = ~0U; // 'p2' must be the dimension of the vector-valued operand if any
  17601. if (p1==~0U) p2 = imgin._spectrum;
  17602. else {
  17603. p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  17604. p2 = imglist[p3]._spectrum;
  17605. }
  17606. if (!p2) _cimg_mp_return(0);
  17607. pos = vector(p2);
  17608. if (p1!=~0U) {
  17609. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_Joff:mp_list_Ioff),
  17610. pos,p1,arg1,arg2==~0U?_cimg_mp_boundary:arg2,p2).move_to(code);
  17611. } else {
  17612. need_input_copy = true;
  17613. CImg<ulongT>::vector((ulongT)(is_relative?mp_Joff:mp_Ioff),
  17614. pos,arg1,arg2==~0U?_cimg_mp_boundary:arg2,p2).move_to(code);
  17615. }
  17616. return_new_comp = true;
  17617. _cimg_mp_return(pos);
  17618. }
  17619. if (!is_sth && (*ss=='i' || *ss=='j') && *ss1=='[' &&
  17620. (reserved_label[(int)*ss]==~0U ||
  17621. !_cimg_mp_is_vector(reserved_label[(int)*ss]))) { // Image value as a scalar
  17622. if (*ss2=='#') { // Index specified
  17623. s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  17624. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  17625. } else { p1 = ~0U; s0 = ss2; }
  17626. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17627. arg1 = compile(s0,s1,depth1,0,bloc_flags); // Offset
  17628. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U; // Boundary
  17629. if (p_ref && arg2==~0U) {
  17630. *p_ref = 2;
  17631. p_ref[1] = p1;
  17632. p_ref[2] = (unsigned int)is_relative;
  17633. p_ref[3] = arg1;
  17634. if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
  17635. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  17636. }
  17637. if (p1!=~0U) {
  17638. if (!imglist) _cimg_mp_return(0);
  17639. pos = scalar3(is_relative?mp_list_joff:mp_list_ioff,p1,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
  17640. } else {
  17641. if (!imgin) _cimg_mp_return(0);
  17642. need_input_copy = true;
  17643. pos = scalar2(is_relative?mp_joff:mp_ioff,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
  17644. }
  17645. memtype[pos] = -1; // Prevent from being used in further optimization
  17646. _cimg_mp_return(pos);
  17647. }
  17648. s0 = se1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
  17649. if (s0>ss) { // Vector element
  17650. arg1 = compile(ss,s0,depth1,0,bloc_flags);
  17651. if (_cimg_mp_is_scalar(arg1)) {
  17652. variable_name.assign(ss,(unsigned int)(s0 - ss + 1)).back() = 0;
  17653. _cimg_mp_strerr;
  17654. cimg::strellipsize(variable_name,64);
  17655. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  17656. "CImg<%s>::%s: %s: Array brackets used on non-vector variable '%s', "
  17657. "in expression '%s'.",
  17658. pixel_type(),_cimg_mp_calling_function,s_op,
  17659. variable_name._data,s0);
  17660. }
  17661. s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17662. if (s1<se1) { // Two or three arguments -> sub-vector extraction
  17663. p1 = _cimg_mp_size(arg1);
  17664. arg2 = compile(++s0,s1,depth1,0,bloc_flags); // Starting index
  17665. s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  17666. arg3 = compile(s1,s0,depth1,0,bloc_flags); // Length
  17667. arg4 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):1; // Step
  17668. _cimg_mp_check_const_scalar(arg3,2,3);
  17669. arg3 = (unsigned int)mem[arg3];
  17670. pos = vector(arg3);
  17671. CImg<ulongT>::vector((ulongT)mp_vector_crop,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
  17672. return_new_comp = true;
  17673. _cimg_mp_return(pos);
  17674. }
  17675. // One argument -> vector value reference
  17676. arg2 = compile(++s0,se1,depth1,0,bloc_flags);
  17677. if (_cimg_mp_is_const_scalar(arg2)) { // Constant index
  17678. nb = (int)mem[arg2];
  17679. if (nb>=0 && nb<(int)_cimg_mp_size(arg1)) _cimg_mp_return(arg1 + 1 + nb);
  17680. variable_name.assign(ss,(unsigned int)(s0 - ss)).back() = 0;
  17681. _cimg_mp_strerr;
  17682. cimg::strellipsize(variable_name,64);
  17683. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  17684. "CImg<%s>::%s: Out-of-bounds reference '%s[%d]' "
  17685. "(vector '%s' has dimension %u), "
  17686. "in expression '%s'.",
  17687. pixel_type(),_cimg_mp_calling_function,
  17688. variable_name._data,nb,
  17689. variable_name._data,_cimg_mp_size(arg1),s0);
  17690. }
  17691. if (p_ref) {
  17692. *p_ref = 1;
  17693. p_ref[1] = arg1;
  17694. p_ref[2] = arg2;
  17695. if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1; // Prevent from being used in further optimization
  17696. }
  17697. pos = scalar3(mp_vector_off,arg1,_cimg_mp_size(arg1),arg2);
  17698. memtype[pos] = -1; // Prevent from being used in further optimization
  17699. _cimg_mp_return(pos);
  17700. }
  17701. }
  17702. // Look for a function call, an access to image value, or a parenthesis.
  17703. if (*se1==')') {
  17704. if (*ss=='(') _cimg_mp_return(compile(ss1,se1,depth1,p_ref,bloc_flags)); // Simple parentheses
  17705. _cimg_mp_op("Value accessor '()'");
  17706. is_relative = *ss=='j' || *ss=='J';
  17707. s0 = s1 = std::strchr(ss,'('); if (s0) { do { --s1; } while (cimg::is_blank(*s1)); cimg::swap(*s0,*++s1); }
  17708. // I/J(_#ind,_x,_y,_z,_interpolation,_boundary_conditions)
  17709. if ((*ss=='I' || *ss=='J') && *ss1=='(') { // Image value as scalar
  17710. if (*ss2=='#') { // Index specified
  17711. s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  17712. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  17713. _cimg_mp_check_const_index(p1);
  17714. _cimg_mp_check_list();
  17715. } else { p1 = ~0U; s0 = ss2; }
  17716. arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
  17717. arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
  17718. arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
  17719. arg4 = arg5 = ~0U;
  17720. if (s0<se1) {
  17721. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17722. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  17723. if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
  17724. p2 = _cimg_mp_size(arg1);
  17725. ++arg1;
  17726. if (p2>1) {
  17727. arg2 = arg1 + 1;
  17728. if (p2>2) arg3 = arg2 + 1;
  17729. }
  17730. if (s1<se1) {
  17731. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17732. arg4 = compile(s1,s2,depth1,0,bloc_flags);
  17733. arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
  17734. }
  17735. } else if (s1<se1) {
  17736. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17737. arg2 = compile(s1,s2,depth1,0,bloc_flags);
  17738. if (s2<se1) {
  17739. s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  17740. arg3 = compile(s2,s3,depth1,0,bloc_flags);
  17741. if (s3<se1) {
  17742. s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17743. arg4 = compile(s3,s2,depth1,0,bloc_flags);
  17744. arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
  17745. }
  17746. }
  17747. }
  17748. }
  17749. if (p_ref && arg4==~0U && arg5==~0U) {
  17750. *p_ref = 5;
  17751. p_ref[1] = p1;
  17752. p_ref[2] = (unsigned int)is_relative;
  17753. p_ref[3] = arg1;
  17754. p_ref[4] = arg2;
  17755. p_ref[5] = arg3;
  17756. if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
  17757. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  17758. if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
  17759. if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
  17760. }
  17761. p2 = ~0U; // 'p2' must be the dimension of the vector-valued operand if any
  17762. if (p1==~0U) p2 = imgin._spectrum;
  17763. else if (_cimg_mp_is_const_scalar(p1)) {
  17764. p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  17765. p2 = imglist[p3]._spectrum;
  17766. }
  17767. if (!p2) _cimg_mp_return(0);
  17768. pos = vector(p2);
  17769. if (p1!=~0U)
  17770. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_Jxyz:mp_list_Ixyz),
  17771. pos,p1,arg1,arg2,arg3,
  17772. arg4==~0U?_cimg_mp_interpolation:arg4,
  17773. arg5==~0U?_cimg_mp_boundary:arg5,p2).move_to(code);
  17774. else {
  17775. need_input_copy = true;
  17776. CImg<ulongT>::vector((ulongT)(is_relative?mp_Jxyz:mp_Ixyz),
  17777. pos,arg1,arg2,arg3,
  17778. arg4==~0U?_cimg_mp_interpolation:arg4,
  17779. arg5==~0U?_cimg_mp_boundary:arg5,p2).move_to(code);
  17780. }
  17781. return_new_comp = true;
  17782. _cimg_mp_return(pos);
  17783. }
  17784. // i/j(_#ind,_x,_y,_z,_c,_interpolation,_boundary_conditions)
  17785. if ((*ss=='i' || *ss=='j') && *ss1=='(') { // Image value as scalar
  17786. if (*ss2=='#') { // Index specified
  17787. s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  17788. p1 = compile(ss3,s0++,depth1,0,bloc_flags);
  17789. } else { p1 = ~0U; s0 = ss2; }
  17790. arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
  17791. arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
  17792. arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
  17793. arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
  17794. arg5 = arg6 = ~0U;
  17795. if (s0<se1) {
  17796. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17797. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  17798. if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
  17799. p2 = _cimg_mp_size(arg1);
  17800. ++arg1;
  17801. if (p2>1) {
  17802. arg2 = arg1 + 1;
  17803. if (p2>2) {
  17804. arg3 = arg2 + 1;
  17805. if (p2>3) arg4 = arg3 + 1;
  17806. }
  17807. }
  17808. if (s1<se1) {
  17809. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17810. arg5 = compile(s1,s2,depth1,0,bloc_flags);
  17811. arg6 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
  17812. }
  17813. } else if (s1<se1) {
  17814. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17815. arg2 = compile(s1,s2,depth1,0,bloc_flags);
  17816. if (s2<se1) {
  17817. s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  17818. arg3 = compile(s2,s3,depth1,0,bloc_flags);
  17819. if (s3<se1) {
  17820. s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17821. arg4 = compile(s3,s2,depth1,0,bloc_flags);
  17822. if (s2<se1) {
  17823. s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  17824. arg5 = compile(s2,s3,depth1,0,bloc_flags);
  17825. arg6 = s3<se1?compile(++s3,se1,depth1,0,bloc_flags):~0U;
  17826. }
  17827. }
  17828. }
  17829. }
  17830. }
  17831. if (p_ref && arg5==~0U && arg6==~0U) {
  17832. *p_ref = 3;
  17833. p_ref[1] = p1;
  17834. p_ref[2] = (unsigned int)is_relative;
  17835. p_ref[3] = arg1;
  17836. p_ref[4] = arg2;
  17837. p_ref[5] = arg3;
  17838. p_ref[6] = arg4;
  17839. if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
  17840. if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
  17841. if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
  17842. if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
  17843. if (_cimg_mp_is_comp(arg4)) memtype[arg4] = -1;
  17844. }
  17845. if (p1!=~0U) {
  17846. if (!imglist) _cimg_mp_return(0);
  17847. pos = scalar7(is_relative?mp_list_jxyzc:mp_list_ixyzc,
  17848. p1,arg1,arg2,arg3,arg4,
  17849. arg5==~0U?_cimg_mp_interpolation:arg5,
  17850. arg6==~0U?_cimg_mp_boundary:arg6);
  17851. } else {
  17852. if (!imgin) _cimg_mp_return(0);
  17853. need_input_copy = true;
  17854. pos = scalar6(is_relative?mp_jxyzc:mp_ixyzc,
  17855. arg1,arg2,arg3,arg4,
  17856. arg5==~0U?_cimg_mp_interpolation:arg5,
  17857. arg6==~0U?_cimg_mp_boundary:arg6);
  17858. }
  17859. memtype[pos] = -1; // Prevent from being used in further optimization
  17860. _cimg_mp_return(pos);
  17861. }
  17862. // Mathematical functions.
  17863. switch (*ss) {
  17864. case 'a' :
  17865. if (!std::strncmp(ss,"abs(",4)) { // Absolute value
  17866. _cimg_mp_op("Function 'abs()'");
  17867. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  17868. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_abs,arg1);
  17869. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::abs(mem[arg1]));
  17870. _cimg_mp_scalar1(mp_abs,arg1);
  17871. }
  17872. if (!std::strncmp(ss,"addr(",5)) { // Pointer address
  17873. _cimg_mp_op("Function 'addr()'");
  17874. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  17875. _cimg_mp_const_scalar((double)arg1);
  17876. }
  17877. if (!std::strncmp(ss,"acos(",5)) { // Arccos
  17878. _cimg_mp_op("Function 'acos()'");
  17879. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  17880. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_acos,arg1);
  17881. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::acos(mem[arg1]));
  17882. _cimg_mp_scalar1(mp_acos,arg1);
  17883. }
  17884. if (!std::strncmp(ss,"acosh(",6)) { // Hyperbolic arccosine
  17885. _cimg_mp_op("Function 'acosh()'");
  17886. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  17887. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_acosh,arg1);
  17888. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::acosh(mem[arg1]));
  17889. _cimg_mp_scalar1(mp_acosh,arg1);
  17890. }
  17891. if (!std::strncmp(ss,"asinh(",6)) { // Hyperbolic arcsine
  17892. _cimg_mp_op("Function 'asinh()'");
  17893. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  17894. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_asinh,arg1);
  17895. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::asinh(mem[arg1]));
  17896. _cimg_mp_scalar1(mp_asinh,arg1);
  17897. }
  17898. if (!std::strncmp(ss,"atanh(",6)) { // Hyperbolic arctangent
  17899. _cimg_mp_op("Function 'atanh()'");
  17900. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  17901. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_atanh,arg1);
  17902. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::atanh(mem[arg1]));
  17903. _cimg_mp_scalar1(mp_atanh,arg1);
  17904. }
  17905. if (!std::strncmp(ss,"arg(",4) ||
  17906. !std::strncmp(ss,"arg0(",5) ||
  17907. !std::strncmp(ss,"arg1(",5)) { // Nth argument
  17908. _cimg_mp_op(*ss3=='('?"Function 'arg()'":*ss3=='0'?"Function 'arg0()'":"Function 'arg1()'");
  17909. s0 = ss4 + (*ss3!='('?1:0);
  17910. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17911. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  17912. _cimg_mp_check_type(arg1,1,1,0);
  17913. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  17914. arg2 = compile(s1,s2,depth1,0,bloc_flags);
  17915. p2 = _cimg_mp_size(arg2);
  17916. p3 = 3;
  17917. CImg<ulongT>::vector((ulongT)(*ss3=='0'?mp_arg0:mp_arg),0,0,p2,arg1,arg2).move_to(l_opcode);
  17918. for (s = ++s2; s<se; ++s) {
  17919. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  17920. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  17921. arg3 = compile(s,ns,depth1,0,bloc_flags);
  17922. _cimg_mp_check_type(arg3,p3,p2?2:1,p2);
  17923. CImg<ulongT>::vector(arg3).move_to(l_opcode);
  17924. ++p3;
  17925. s = ns;
  17926. }
  17927. (l_opcode>'y').move_to(opcode);
  17928. opcode[2] = opcode._height;
  17929. if (_cimg_mp_is_const_scalar(arg1)) {
  17930. p3-=1; // Number of args
  17931. if (*ss3=='0') arg1 = (unsigned int)(mem[arg1]<0?mem[arg1] + p3:mem[arg1] + 1);
  17932. else arg1 = (unsigned int)(mem[arg1]<0?mem[arg1] + p3:mem[arg1]);
  17933. if (arg1<p3) _cimg_mp_return(opcode[4 + arg1]);
  17934. if (p2) {
  17935. pos = vector(p2);
  17936. std::memset(&mem[pos] + 1,0,p2*sizeof(double));
  17937. return_new_comp = true;
  17938. _cimg_mp_return(pos);
  17939. } else _cimg_mp_return(0);
  17940. }
  17941. pos = opcode[1] = p2?vector(p2):scalar();
  17942. opcode.move_to(code);
  17943. return_new_comp = true;
  17944. _cimg_mp_return(pos);
  17945. }
  17946. if (!std::strncmp(ss,"asin(",5)) { // Arcsin
  17947. _cimg_mp_op("Function 'asin()'");
  17948. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  17949. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_asin,arg1);
  17950. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::asin(mem[arg1]));
  17951. _cimg_mp_scalar1(mp_asin,arg1);
  17952. }
  17953. if (!std::strncmp(ss,"atan(",5)) { // Arctan
  17954. _cimg_mp_op("Function 'atan()'");
  17955. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  17956. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_atan,arg1);
  17957. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::atan(mem[arg1]));
  17958. _cimg_mp_scalar1(mp_atan,arg1);
  17959. }
  17960. if (!std::strncmp(ss,"atan2(",6)) { // Arctan2
  17961. _cimg_mp_op("Function 'atan2()'");
  17962. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  17963. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  17964. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  17965. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  17966. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_atan2,arg1,arg2);
  17967. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_atan2,arg1,arg2);
  17968. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_atan2,arg1,arg2);
  17969. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  17970. _cimg_mp_const_scalar(std::atan2(mem[arg1],mem[arg2]));
  17971. _cimg_mp_scalar2(mp_atan2,arg1,arg2);
  17972. }
  17973. break;
  17974. case 'b' :
  17975. if (!std::strncmp(ss,"break(",6)) { // Break current loop
  17976. if (pexpr[se2 - expr._data]=='(') { // no arguments?
  17977. CImg<ulongT>::vector((ulongT)mp_break,_cimg_mp_slot_nan).move_to(code);
  17978. _cimg_mp_return_nan();
  17979. }
  17980. }
  17981. if (!std::strncmp(ss,"breakpoint(",11)) { // Break point (for abort test)
  17982. _cimg_mp_op("Function 'breakpoint()'");
  17983. if (pexpr[se2 - expr._data]=='(') { // no arguments?
  17984. CImg<ulongT>::vector((ulongT)mp_breakpoint,_cimg_mp_slot_nan).move_to(code);
  17985. _cimg_mp_return_nan();
  17986. }
  17987. }
  17988. if (!std::strncmp(ss,"bool(",5)) { // Boolean cast
  17989. _cimg_mp_op("Function 'bool()'");
  17990. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  17991. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bool,arg1);
  17992. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((bool)mem[arg1]);
  17993. _cimg_mp_scalar1(mp_bool,arg1);
  17994. }
  17995. if (!std::strncmp(ss,"begin(",6)) { // Begin
  17996. _cimg_mp_op("Function 'begin()'");
  17997. s1 = ss6; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
  17998. if (s1!=se1) {
  17999. const bool is_inside_begin = (bool)(bloc_flags&2);
  18000. if (!is_inside_begin) code.swap(code_begin);
  18001. arg1 = compile(s1,se1,depth1,p_ref,2);
  18002. if (!is_inside_begin) code.swap(code_begin);
  18003. _cimg_mp_return(arg1);
  18004. } else _cimg_mp_return_nan();
  18005. }
  18006. if (!std::strncmp(ss,"begin_t(",8)) { // Begin thread
  18007. _cimg_mp_op("Function 'begin_t()'");
  18008. s1 = ss8; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
  18009. if (s1!=se1) {
  18010. const bool is_inside_begin_t = (bool)(bloc_flags&4);
  18011. if (!is_inside_begin_t) code.swap(code_begin_t);
  18012. arg1 = compile(s1,se1,depth1,p_ref,4);
  18013. if (!is_inside_begin_t) code.swap(code_begin_t);
  18014. _cimg_mp_return(arg1);
  18015. } else _cimg_mp_return_nan();
  18016. }
  18017. break;
  18018. case 'c' :
  18019. if (!std::strncmp(ss,"cabs(",5)) { // Complex absolute value
  18020. _cimg_mp_op("Function 'cabs()'");
  18021. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18022. _cimg_mp_check_type(arg1,0,3,2);
  18023. if (_cimg_mp_is_scalar(arg1)) _cimg_mp_scalar2(mp_complex_abs,arg1,0);
  18024. _cimg_mp_scalar2(mp_complex_abs,arg1 + 1,arg1 + 2);
  18025. }
  18026. if (!std::strncmp(ss,"carg(",5)) { // Complex argument
  18027. _cimg_mp_op("Function 'carg()'");
  18028. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18029. _cimg_mp_check_type(arg1,0,3,2);
  18030. if (_cimg_mp_is_scalar(arg1)) _cimg_mp_scalar2(mp_atan2,0,arg1);
  18031. _cimg_mp_scalar2(mp_atan2,arg1 + 2,arg1 + 1);
  18032. }
  18033. if (!std::strncmp(ss,"cbrt(",5)) { // Cubic root
  18034. _cimg_mp_op("Function 'cbrt()'");
  18035. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18036. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cbrt,arg1);
  18037. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::cbrt(mem[arg1]));
  18038. _cimg_mp_scalar1(mp_cbrt,arg1);
  18039. }
  18040. if (!std::strncmp(ss,"cconj(",6)) { // Complex conjugate
  18041. _cimg_mp_op("Function 'cconj()'");
  18042. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  18043. _cimg_mp_check_type(arg1,0,3,2);
  18044. pos = vector(2);
  18045. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_conj,pos,arg1,0).move_to(code);
  18046. else CImg<ulongT>::vector((ulongT)mp_complex_conj,pos,arg1 + 1,arg1 + 2).move_to(code);
  18047. return_new_comp = true;
  18048. _cimg_mp_return(pos);
  18049. }
  18050. if (!std::strncmp(ss,"ceil(",5)) { // Ceil
  18051. _cimg_mp_op("Function 'ceil()'");
  18052. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18053. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_ceil,arg1);
  18054. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::ceil(mem[arg1]));
  18055. _cimg_mp_scalar1(mp_ceil,arg1);
  18056. }
  18057. if (!std::strncmp(ss,"cexp(",5)) { // Complex exponential
  18058. _cimg_mp_op("Function 'cexp()'");
  18059. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18060. _cimg_mp_check_type(arg1,0,3,2);
  18061. pos = vector(2);
  18062. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_exp,pos,arg1,0).move_to(code);
  18063. else CImg<ulongT>::vector((ulongT)mp_complex_exp,pos,arg1 + 1,arg1 + 2).move_to(code);
  18064. return_new_comp = true;
  18065. _cimg_mp_return(pos);
  18066. }
  18067. if (!std::strncmp(ss,"clog(",5)) { // Complex logarithm
  18068. _cimg_mp_op("Function 'clog()'");
  18069. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18070. _cimg_mp_check_type(arg1,0,3,2);
  18071. pos = vector(2);
  18072. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_log,pos,arg1,0).move_to(code);
  18073. else CImg<ulongT>::vector((ulongT)mp_complex_log,pos,arg1 + 1,arg1 + 2).move_to(code);
  18074. return_new_comp = true;
  18075. _cimg_mp_return(pos);
  18076. }
  18077. if (!std::strncmp(ss,"ccos(",5)) { // Complex cosine
  18078. _cimg_mp_op("Function 'ccos()'");
  18079. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18080. _cimg_mp_check_type(arg1,0,3,2);
  18081. pos = vector(2);
  18082. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_cos,pos,arg1,0).move_to(code);
  18083. else CImg<ulongT>::vector((ulongT)mp_complex_cos,pos,arg1 + 1,arg1 + 2).move_to(code);
  18084. return_new_comp = true;
  18085. _cimg_mp_return(pos);
  18086. }
  18087. if (!std::strncmp(ss,"csin(",5)) { // Complex sine
  18088. _cimg_mp_op("Function 'csin()'");
  18089. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18090. _cimg_mp_check_type(arg1,0,3,2);
  18091. pos = vector(2);
  18092. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_sin,pos,arg1,0).move_to(code);
  18093. else CImg<ulongT>::vector((ulongT)mp_complex_sin,pos,arg1 + 1,arg1 + 2).move_to(code);
  18094. return_new_comp = true;
  18095. _cimg_mp_return(pos);
  18096. }
  18097. if (!std::strncmp(ss,"ctan(",5)) { // Complex tangent
  18098. _cimg_mp_op("Function 'ctan()'");
  18099. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18100. _cimg_mp_check_type(arg1,0,3,2);
  18101. pos = vector(2);
  18102. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_tan,pos,arg1,0).move_to(code);
  18103. else CImg<ulongT>::vector((ulongT)mp_complex_tan,pos,arg1 + 1,arg1 + 2).move_to(code);
  18104. return_new_comp = true;
  18105. _cimg_mp_return(pos);
  18106. }
  18107. if (!std::strncmp(ss,"ccosh(",6)) { // Complex hyperbolic cosine
  18108. _cimg_mp_op("Function 'ccosh()'");
  18109. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  18110. _cimg_mp_check_type(arg1,0,3,2);
  18111. pos = vector(2);
  18112. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_cosh,pos,arg1,0).move_to(code);
  18113. else CImg<ulongT>::vector((ulongT)mp_complex_cosh,pos,arg1 + 1,arg1 + 2).move_to(code);
  18114. return_new_comp = true;
  18115. _cimg_mp_return(pos);
  18116. }
  18117. if (!std::strncmp(ss,"csinh(",6)) { // Complex hyperbolic sine
  18118. _cimg_mp_op("Function 'csinh()'");
  18119. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  18120. _cimg_mp_check_type(arg1,0,3,2);
  18121. pos = vector(2);
  18122. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_sinh,pos,arg1,0).move_to(code);
  18123. else CImg<ulongT>::vector((ulongT)mp_complex_sinh,pos,arg1 + 1,arg1 + 2).move_to(code);
  18124. return_new_comp = true;
  18125. _cimg_mp_return(pos);
  18126. }
  18127. if (!std::strncmp(ss,"ctanh(",6)) { // Complex hyperbolic tangent
  18128. _cimg_mp_op("Function 'ctanh()'");
  18129. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  18130. _cimg_mp_check_type(arg1,0,3,2);
  18131. pos = vector(2);
  18132. if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_tanh,pos,arg1,0).move_to(code);
  18133. else CImg<ulongT>::vector((ulongT)mp_complex_tanh,pos,arg1 + 1,arg1 + 2).move_to(code);
  18134. return_new_comp = true;
  18135. _cimg_mp_return(pos);
  18136. }
  18137. if (!std::strncmp(ss,"continue(",9)) { // Continue loop
  18138. if (pexpr[se2 - expr._data]=='(') { // no arguments?
  18139. CImg<ulongT>::vector((ulongT)mp_continue,_cimg_mp_slot_nan).move_to(code);
  18140. _cimg_mp_return_nan();
  18141. }
  18142. }
  18143. if (!std::strncmp(ss,"copy(",5)) { // Memory copy
  18144. _cimg_mp_op("Function 'copy()'");
  18145. ref.assign(14);
  18146. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18147. arg1 = p1 = compile(ss5,s1,depth1,ref,bloc_flags);
  18148. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18149. arg2 = compile(s1,s2,depth1,ref._data + 7,bloc_flags);
  18150. arg3 = arg4 = arg5 = ~0U; arg6 = 1;
  18151. if (s2<se1) {
  18152. s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  18153. arg3 = compile(s2,s3,depth1,0,bloc_flags);
  18154. if (s3<se1) {
  18155. s1 = ++s3; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18156. arg4 = compile(s3,s1,depth1,0,bloc_flags);
  18157. if (s1<se1) {
  18158. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18159. arg5 = compile(s1,s2,depth1,0,bloc_flags);
  18160. arg6 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
  18161. }
  18162. }
  18163. }
  18164. if (_cimg_mp_is_vector(arg1)) {
  18165. if (!ref[0]) ++arg1;
  18166. else if (ref[0]>=4 && arg4==~0U) arg4 = scalar1(mp_image_whd,ref[1]);
  18167. }
  18168. if (_cimg_mp_is_vector(arg2)) {
  18169. if (arg3==~0U) arg3 = const_scalar(_cimg_mp_size(arg2));
  18170. if (!ref[7]) ++arg2;
  18171. if (ref[7]>=4 && arg5==~0U) arg5 = scalar1(mp_image_whd,ref[8]);
  18172. }
  18173. if (arg3==~0U) arg3 = 1;
  18174. if (arg4==~0U) arg4 = 1;
  18175. if (arg5==~0U) arg5 = 1;
  18176. _cimg_mp_check_type(arg3,3,1,0);
  18177. _cimg_mp_check_type(arg4,4,1,0);
  18178. _cimg_mp_check_type(arg5,5,1,0);
  18179. _cimg_mp_check_type(arg6,5,1,0);
  18180. CImg<ulongT>(1,22).move_to(code);
  18181. code.back().get_shared_rows(0,7).fill((ulongT)mp_memcopy,p1,arg1,arg2,arg3,arg4,arg5,arg6);
  18182. code.back().get_shared_rows(8,21).fill(ref);
  18183. _cimg_mp_return(p1);
  18184. }
  18185. if (!std::strncmp(ss,"cos(",4)) { // Cosine
  18186. _cimg_mp_op("Function 'cos()'");
  18187. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18188. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cos,arg1);
  18189. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::cos(mem[arg1]));
  18190. _cimg_mp_scalar1(mp_cos,arg1);
  18191. }
  18192. if (!std::strncmp(ss,"cosh(",5)) { // Hyperbolic cosine
  18193. _cimg_mp_op("Function 'cosh()'");
  18194. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18195. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cosh,arg1);
  18196. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::cosh(mem[arg1]));
  18197. _cimg_mp_scalar1(mp_cosh,arg1);
  18198. }
  18199. if (!std::strncmp(ss,"critical(",9)) { // Critical section (single thread at a time)
  18200. _cimg_mp_op("Function 'critical()'");
  18201. p1 = code._width;
  18202. arg1 = compile(ss + 9,se1,depth1,p_ref,bloc_flags | 1);
  18203. CImg<ulongT>::vector((ulongT)mp_critical,arg1,code._width - p1).move_to(code,p1);
  18204. _cimg_mp_return(arg1);
  18205. }
  18206. if (!std::strncmp(ss,"crop(",5)) { // Image crop
  18207. _cimg_mp_op("Function 'crop()'");
  18208. if (*ss5=='#') { // Index specified
  18209. s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18210. p1 = compile(ss6,s0++,depth1,0,bloc_flags);
  18211. _cimg_mp_check_list();
  18212. } else { p1 = ~0U; s0 = ss5; need_input_copy = true; }
  18213. pos = 0;
  18214. is_sth = false; // Coordinates specified as a vector?
  18215. if (s0<se1) for (s = s0; s<se; ++s, ++pos) {
  18216. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18217. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18218. arg1 = compile(s,ns,depth1,0,bloc_flags);
  18219. if (!pos && _cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
  18220. opcode = CImg<ulongT>::sequence(_cimg_mp_size(arg1),arg1 + 1,
  18221. arg1 + (ulongT)_cimg_mp_size(arg1));
  18222. opcode.resize(1,std::min(opcode._height,4U),1,1,0).move_to(l_opcode);
  18223. is_sth = true;
  18224. } else {
  18225. _cimg_mp_check_type(arg1,pos + 1,1,0);
  18226. CImg<ulongT>::vector(arg1).move_to(l_opcode);
  18227. }
  18228. s = ns;
  18229. }
  18230. (l_opcode>'y').move_to(opcode);
  18231. arg1 = 0; arg2 = (p1!=~0U);
  18232. switch (opcode._height) {
  18233. case 0 : case 1 :
  18234. CImg<ulongT>::vector(0,0,0,0,~0U,~0U,~0U,~0U,0).move_to(opcode);
  18235. break;
  18236. case 2 :
  18237. CImg<ulongT>::vector(*opcode,0,0,0,opcode[1],~0U,~0U,~0U,_cimg_mp_boundary).move_to(opcode);
  18238. arg1 = arg2 + 2;
  18239. break;
  18240. case 3 :
  18241. CImg<ulongT>::vector(*opcode,0,0,0,opcode[1],~0U,~0U,~0U,opcode[2]).move_to(opcode);
  18242. arg1 = arg2 + 2;
  18243. break;
  18244. case 4 :
  18245. CImg<ulongT>::vector(*opcode,opcode[1],0,0,opcode[2],opcode[3],~0U,~0U,_cimg_mp_boundary).
  18246. move_to(opcode);
  18247. arg1 = arg2 + (is_sth?2:3);
  18248. break;
  18249. case 5 :
  18250. CImg<ulongT>::vector(*opcode,opcode[1],0,0,opcode[2],opcode[3],~0U,~0U,opcode[4]).
  18251. move_to(opcode);
  18252. arg1 = arg2 + (is_sth?2:3);
  18253. break;
  18254. case 6 :
  18255. CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],0,opcode[3],opcode[4],opcode[5],~0U,
  18256. _cimg_mp_boundary).move_to(opcode);
  18257. arg1 = arg2 + (is_sth?2:4);
  18258. break;
  18259. case 7 :
  18260. CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],0,opcode[3],opcode[4],opcode[5],~0U,
  18261. opcode[6]).move_to(opcode);
  18262. arg1 = arg2 + (is_sth?2:4);
  18263. break;
  18264. case 8 :
  18265. CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],opcode[3],opcode[4],opcode[5],opcode[6],
  18266. opcode[7],_cimg_mp_boundary).move_to(opcode);
  18267. arg1 = arg2 + (is_sth?2:5);
  18268. break;
  18269. case 9 :
  18270. arg1 = arg2 + (is_sth?2:5);
  18271. break;
  18272. default : // Error -> too much arguments
  18273. _cimg_mp_strerr;
  18274. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18275. "CImg<%s>::%s: %s: Too much arguments specified, "
  18276. "in expression '%s'.",
  18277. pixel_type(),_cimg_mp_calling_function,s_op,s0);
  18278. }
  18279. _cimg_mp_check_type((unsigned int)*opcode,arg2 + 1,1,0);
  18280. _cimg_mp_check_type((unsigned int)opcode[1],arg2 + 1 + (is_sth?0:1),1,0);
  18281. _cimg_mp_check_type((unsigned int)opcode[2],arg2 + 1 + (is_sth?0:2),1,0);
  18282. _cimg_mp_check_type((unsigned int)opcode[3],arg2 + 1 + (is_sth?0:3),1,0);
  18283. if (opcode[4]!=(ulongT)~0U) {
  18284. _cimg_mp_check_const_scalar((unsigned int)opcode[4],arg1,3);
  18285. opcode[4] = (ulongT)mem[opcode[4]];
  18286. }
  18287. if (opcode[5]!=(ulongT)~0U) {
  18288. _cimg_mp_check_const_scalar((unsigned int)opcode[5],arg1 + 1,3);
  18289. opcode[5] = (ulongT)mem[opcode[5]];
  18290. }
  18291. if (opcode[6]!=(ulongT)~0U) {
  18292. _cimg_mp_check_const_scalar((unsigned int)opcode[6],arg1 + 2,3);
  18293. opcode[6] = (ulongT)mem[opcode[6]];
  18294. }
  18295. if (opcode[7]!=(ulongT)~0U) {
  18296. _cimg_mp_check_const_scalar((unsigned int)opcode[7],arg1 + 3,3);
  18297. opcode[7] = (ulongT)mem[opcode[7]];
  18298. }
  18299. _cimg_mp_check_type((unsigned int)opcode[8],arg1 + 4,1,0);
  18300. if (opcode[4]==(ulongT)~0U || opcode[5]==(ulongT)~0U ||
  18301. opcode[6]==(ulongT)~0U || opcode[7]==(ulongT)~0U) {
  18302. p2 = 0;
  18303. if (p1!=~0U) {
  18304. _cimg_mp_check_const_scalar(p1,1,1);
  18305. p2 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  18306. }
  18307. const CImg<T> &img = p1!=~0U?imglist[p2]:imgin;
  18308. if (!img) {
  18309. _cimg_mp_strerr;
  18310. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18311. "CImg<%s>::%s: %s: Cannot crop empty image when "
  18312. "some xyzc-coordinates are unspecified, in expression '%s'.",
  18313. pixel_type(),_cimg_mp_calling_function,s_op,s0);
  18314. }
  18315. if (opcode[4]==(ulongT)~0U) opcode[4] = (ulongT)img._width;
  18316. if (opcode[5]==(ulongT)~0U) opcode[5] = (ulongT)img._height;
  18317. if (opcode[6]==(ulongT)~0U) opcode[6] = (ulongT)img._depth;
  18318. if (opcode[7]==(ulongT)~0U) opcode[7] = (ulongT)img._spectrum;
  18319. }
  18320. pos = vector((unsigned int)(opcode[4]*opcode[5]*opcode[6]*opcode[7]));
  18321. CImg<ulongT>::vector((ulongT)mp_crop,
  18322. pos,p1,
  18323. *opcode,opcode[1],opcode[2],opcode[3],
  18324. opcode[4],opcode[5],opcode[6],opcode[7],
  18325. opcode[8]).move_to(code);
  18326. return_new_comp = true;
  18327. _cimg_mp_return(pos);
  18328. }
  18329. if (!std::strncmp(ss,"cross(",6)) { // Cross product
  18330. _cimg_mp_op("Function 'cross()'");
  18331. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18332. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  18333. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  18334. _cimg_mp_check_type(arg1,1,2,3);
  18335. _cimg_mp_check_type(arg2,2,2,3);
  18336. pos = vector(3);
  18337. CImg<ulongT>::vector((ulongT)mp_cross,pos,arg1,arg2).move_to(code);
  18338. return_new_comp = true;
  18339. _cimg_mp_return(pos);
  18340. }
  18341. if (!std::strncmp(ss,"cut(",4)) { // Cut
  18342. _cimg_mp_op("Function 'cut()'");
  18343. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18344. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  18345. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18346. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  18347. arg3 = compile(++s2,se1,depth1,0,bloc_flags);
  18348. _cimg_mp_check_type(arg2,2,1,0);
  18349. _cimg_mp_check_type(arg3,3,1,0);
  18350. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_cut,arg1,arg2,arg3);
  18351. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3)) {
  18352. val = mem[arg1];
  18353. val1 = mem[arg2];
  18354. val2 = mem[arg3];
  18355. _cimg_mp_const_scalar(val<val1?val1:val>val2?val2:val);
  18356. }
  18357. _cimg_mp_scalar3(mp_cut,arg1,arg2,arg3);
  18358. }
  18359. if (!std::strncmp(ss,"convolve(",9) || !std::strncmp(ss,"correlate(",10)) { // Convolve & Correlate
  18360. is_sth = *ss2=='n'; // is_convolve?
  18361. _cimg_mp_op(is_sth?"Function 'convolve()'":"Function 'correlate()'");
  18362. op = is_sth?mp_convolve:mp_correlate;
  18363. const ulongT default_params[] = { (ulongT)op,0, // [0]=function, [1]=result vector
  18364. 0,0,0,0,0, // [2]=A, [3]=wA, [4]=hA, [5]=dA, [6]=sA
  18365. 0,0,0,0,0, // [7]=M, [8]=wM, [9]=hM, [10]=dM, [11]=sM
  18366. 1,0,1, // [12]=boundary_conditions, [13]=is_normalized, [14]=chan._mode
  18367. ~0U,~0U,~0U, // [15]=xcenter, [16]=ycenter, [17]=zcenter
  18368. 0,0,0, // [18]=xstart, [19]=ystart, [20]=zstart
  18369. ~0U,~0U,~0U, // [21]=xend, [22]=yend, [23]=zend
  18370. 1,1,1, // [24]=xstride, [25]=ystride, [26]=zstride
  18371. 1,1,1, // [27]=xdilation, [28]=ydilation, [29]=zdilation,
  18372. 0 }; // [30]=interpolation_type
  18373. l_opcode.assign(); // Don't use 'opcode': it could be modified by further calls to 'compile()'!
  18374. CImg<ulongT>(default_params,1,sizeof(default_params)/sizeof(ulongT)).move_to(l_opcode);
  18375. arg1 = 2;
  18376. for (s = std::strchr(ss,'(') + 1; s<se && arg1<l_opcode[0]._height; ++s) {
  18377. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18378. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18379. l_opcode(0,arg1++) = compile(s,ns,depth1,0,bloc_flags);
  18380. s = ns;
  18381. }
  18382. l_opcode[0].move_to(opcode);
  18383. if (arg1<12 || arg1>opcode._height) {
  18384. _cimg_mp_strerr;
  18385. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18386. "CImg<%s>::%s: %s: %s arguments provided, in expression '%s'.",
  18387. pixel_type(),_cimg_mp_calling_function,s_op,
  18388. arg1<12?"Not enough":"Too much",s0);
  18389. }
  18390. _cimg_mp_check_type(opcode[2],1,2,0); // A
  18391. _cimg_mp_check_const_scalar(opcode[3],2,3); // wA
  18392. _cimg_mp_check_const_scalar(opcode[4],3,3); // hA
  18393. _cimg_mp_check_const_scalar(opcode[5],4,3); // dA
  18394. _cimg_mp_check_const_scalar(opcode[6],5,3); // sA
  18395. _cimg_mp_check_type(opcode[7],6,2,0); // M
  18396. _cimg_mp_check_const_scalar(opcode[8],7,3); // wM
  18397. _cimg_mp_check_const_scalar(opcode[9],8,3); // hM
  18398. _cimg_mp_check_const_scalar(opcode[10],9,3); // dM
  18399. _cimg_mp_check_const_scalar(opcode[11],10,3); // sM
  18400. _cimg_mp_check_type(opcode[12],11,1,0); // boundary_conditions
  18401. _cimg_mp_check_type(opcode[13],12,1,0); // is_normalized
  18402. _cimg_mp_check_const_scalar(opcode[14],13,1); // channel_mode
  18403. if (opcode[15]!=~0U) _cimg_mp_check_type(opcode[15],14,1,0); // xcenter
  18404. if (opcode[16]!=~0U) _cimg_mp_check_type(opcode[16],15,1,0); // ycenter
  18405. if (opcode[17]!=~0U) _cimg_mp_check_type(opcode[17],16,1,0); // zcenter
  18406. _cimg_mp_check_const_scalar(opcode[18],17,1); // xstart
  18407. _cimg_mp_check_const_scalar(opcode[19],18,1); // ystart
  18408. _cimg_mp_check_const_scalar(opcode[20],19,1); // zstart
  18409. if (opcode[21]!=~0U) _cimg_mp_check_const_scalar(opcode[21],20,1); // xend
  18410. if (opcode[22]!=~0U) _cimg_mp_check_const_scalar(opcode[22],21,1); // yend
  18411. if (opcode[23]!=~0U) _cimg_mp_check_const_scalar(opcode[23],22,1); // zend
  18412. _cimg_mp_check_const_scalar(opcode[24],23,0); // xstride
  18413. _cimg_mp_check_const_scalar(opcode[25],24,0); // ystride
  18414. _cimg_mp_check_const_scalar(opcode[26],25,0); // zstride
  18415. _cimg_mp_check_type(opcode[27],26,1,0); // xdilation
  18416. _cimg_mp_check_type(opcode[28],27,1,0); // ydilation
  18417. _cimg_mp_check_type(opcode[29],28,1,0); // zdilation
  18418. _cimg_mp_check_type(opcode[30],29,1,0); // interpolation_type
  18419. const unsigned int
  18420. wA = (unsigned int)mem[opcode[3]],
  18421. hA = (unsigned int)mem[opcode[4]],
  18422. dA = (unsigned int)mem[opcode[5]],
  18423. sA = (unsigned int)mem[opcode[6]],
  18424. wM = (unsigned int)mem[opcode[8]],
  18425. hM = (unsigned int)mem[opcode[9]],
  18426. dM = (unsigned int)mem[opcode[10]],
  18427. sM = (unsigned int)mem[opcode[11]],
  18428. channel_mode = (unsigned int)mem[opcode[14]];
  18429. const int
  18430. xstart = (int)mem[opcode[18]],
  18431. ystart = (int)mem[opcode[19]],
  18432. zstart = (int)mem[opcode[20]],
  18433. xend = opcode[21]!=~0U?(int)mem[opcode[21]]:wA - 1,
  18434. yend = opcode[22]!=~0U?(int)mem[opcode[22]]:hA - 1,
  18435. zend = opcode[23]!=~0U?(int)mem[opcode[23]]:dA - 1;
  18436. if (xstart>xend || ystart>yend || zstart>zend) {
  18437. _cimg_mp_strerr;
  18438. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18439. "CImg<%s>::%s: %s: Invalid xyz-start/end arguments "
  18440. "(start = (%d,%d,%d), end = (%d,%d,%d)), in expression '%s'.",
  18441. pixel_type(),_cimg_mp_calling_function,s_op,
  18442. xstart,ystart,zstart,xend,yend,zend,s0);
  18443. }
  18444. const float
  18445. xstride = (float)mem[opcode[24]],
  18446. ystride = (float)mem[opcode[25]],
  18447. zstride = (float)mem[opcode[26]];
  18448. if (xstride<=0 || ystride<=0 || zstride<=0) {
  18449. _cimg_mp_strerr;
  18450. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18451. "CImg<%s>::%s: %s: Invalid stride arguments (%g,%g,%g), "
  18452. "in expression '%s'.",
  18453. pixel_type(),_cimg_mp_calling_function,s_op,
  18454. xstride,ystride,zstride,s0);
  18455. }
  18456. arg2 = xend - xstart + 1;
  18457. arg3 = yend - ystart + 1;
  18458. arg4 = zend + zstart + 1;
  18459. arg5 = !channel_mode?sA*sM:channel_mode==1?std::max(sA,sM):
  18460. channel_mode==2?std::max(sA,sM)/std::min(sA,sM):1U;
  18461. opcode[1] = pos = vector(arg2*arg3*arg4*arg5);
  18462. opcode[3] = (ulongT)wA;
  18463. opcode[4] = (ulongT)hA;
  18464. opcode[5] = (ulongT)dA;
  18465. opcode[6] = (ulongT)sA;
  18466. opcode[8] = (ulongT)wM;
  18467. opcode[9] = (ulongT)hM;
  18468. opcode[10] = (ulongT)dM;
  18469. opcode[11] = (ulongT)sM;
  18470. opcode[14] = (ulongT)channel_mode;
  18471. opcode[18] = (ulongT)xstart;
  18472. opcode[19] = (ulongT)ystart;
  18473. opcode[20] = (ulongT)zstart;
  18474. opcode[21] = (ulongT)xend;
  18475. opcode[22] = (ulongT)yend;
  18476. opcode[23] = (ulongT)zend;
  18477. opcode.move_to(code);
  18478. return_new_comp = true;
  18479. _cimg_mp_return(pos);
  18480. }
  18481. break;
  18482. case 'd' :
  18483. if (*ss1=='(') { // Image depth
  18484. _cimg_mp_op("Function 'd()'");
  18485. if (*ss2=='#') { // Index specified
  18486. p1 = compile(ss3,se1,depth1,0,bloc_flags);
  18487. _cimg_mp_check_list();
  18488. } else { if (ss2!=se1) break; p1 = ~0U; }
  18489. _cimg_mp_scalar1(mp_image_d,p1);
  18490. }
  18491. if (!std::strncmp(ss,"da_back(",8) ||
  18492. !std::strncmp(ss,"da_pop(",7)) { // Get latest element in a dynamic array
  18493. if (!is_inside_critical) is_parallelizable = false;
  18494. const bool is_pop = *ss3=='p';
  18495. _cimg_mp_op(is_pop?"Function 'da_pop()'":"Function 'da_back()'");
  18496. s0 = ss + (is_pop?7:8);
  18497. if (*s0=='#') { // Index specified
  18498. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18499. p1 = compile(s0,s1++,depth1,0,bloc_flags);
  18500. } else { p1 = 11; s1 = s0; }
  18501. _cimg_mp_check_list();
  18502. _cimg_mp_check_const_scalar(p1,1,1);
  18503. p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
  18504. p2 = imglist[p3]._spectrum;
  18505. if (p2>1) pos = vector(p2); else pos = scalar(); // Return vector or scalar result
  18506. CImg<ulongT>::vector((ulongT)mp_da_back_or_pop,pos,p2,p1,is_pop).move_to(code);
  18507. return_new_comp = true;
  18508. _cimg_mp_return(pos);
  18509. }
  18510. if (!std::strncmp(ss,"da_insert(",10) ||
  18511. !std::strncmp(ss,"da_push(",8)) { // Insert element(s) in a dynamic array
  18512. if (!is_inside_critical) is_parallelizable = false;
  18513. const bool is_push = *ss3=='p';
  18514. _cimg_mp_op(is_push?"Function 'da_push()'":"Function 'da_insert()'");
  18515. s0 = ss + (is_push?8:10);
  18516. if (*s0=='#') { // Index specified
  18517. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18518. p1 = compile(s0,s1++,depth1,0,bloc_flags);
  18519. } else { p1 = 11; s1 = s0; }
  18520. _cimg_mp_check_list();
  18521. if (!is_push) {
  18522. s0 = s1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18523. arg1 = compile(s0,s1++,depth1,0,bloc_flags); // Position
  18524. } else arg1 = ~0U;
  18525. CImg<ulongT>::vector((ulongT)mp_da_insert_or_push,_cimg_mp_slot_nan,p1,arg1,0,0).move_to(l_opcode);
  18526. p3 = p1==~0U?2:3;
  18527. p1 = ~0U;
  18528. for (s = s1; s<se; ++s) {
  18529. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18530. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18531. arg2 = compile(s,ns,depth1,0,bloc_flags); // Element
  18532. p2 = _cimg_mp_size(arg2);
  18533. if (p1==~0U) p1 = p2;
  18534. else {
  18535. if (!p1) _cimg_mp_check_type(arg2,p3,1,0);
  18536. else _cimg_mp_check_type(arg2,p3,2,p1);
  18537. }
  18538. CImg<ulongT>::vector(arg2).move_to(l_opcode);
  18539. s = ns;
  18540. ++p3;
  18541. }
  18542. if (p1==~0U) compile(s1 + 1,se1,depth1,0,bloc_flags); // Missing element -> error
  18543. (l_opcode>'y').move_to(opcode);
  18544. opcode[4] = p1;
  18545. opcode[5] = opcode._height;
  18546. opcode.move_to(code);
  18547. _cimg_mp_return_nan();
  18548. }
  18549. if (!std::strncmp(ss,"da_remove(",10)) { // Remove element(s) in a dynamic array
  18550. if (!is_inside_critical) is_parallelizable = false;
  18551. _cimg_mp_op("Function 'da_remove()'");
  18552. if (ss[10]=='#') { // Index specified
  18553. s0 = ss + 11; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18554. p1 = compile(ss + 11,s0++,depth1,0,bloc_flags);
  18555. } else { p1 = 11; s0 = ss + 10; }
  18556. _cimg_mp_check_list();
  18557. arg1 = arg2 = ~0U;
  18558. if (s0<se1) {
  18559. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18560. arg1 = compile(s0,s1,depth1,0,bloc_flags); // Starting position
  18561. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U; // Ending position
  18562. }
  18563. CImg<ulongT>::vector((ulongT)mp_da_remove,_cimg_mp_slot_nan,p1,arg1,arg2).move_to(code);
  18564. _cimg_mp_return_nan();
  18565. }
  18566. if (!std::strncmp(ss,"da_size(",8)) { // Size of a dynamic array
  18567. if (!is_inside_critical) is_parallelizable = false;
  18568. _cimg_mp_op("Function 'da_size()'");
  18569. if (ss[8]=='#') { // Index specified
  18570. s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18571. p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
  18572. } else { p1 = 11; s0 = ss + 8; }
  18573. _cimg_mp_check_list();
  18574. _cimg_mp_scalar1(mp_da_size,p1);
  18575. }
  18576. if (!std::strncmp(ss,"date(",5)) { // Current date or file date
  18577. _cimg_mp_op("Function 'date()'");
  18578. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18579. arg1 = ss5!=se1?compile(ss5,s1,depth1,0,bloc_flags):~0U;
  18580. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U;
  18581. if (arg2!=~0U) _cimg_mp_check_type(arg2,1,2,0);
  18582. pos = arg1==~0U || _cimg_mp_is_vector(arg1)?vector(arg1==~0U?7:_cimg_mp_size(arg1)):scalar();
  18583. CImg<ulongT>::vector((ulongT)mp_date,pos,_cimg_mp_size(pos),
  18584. arg1,arg1==~0U?~0U:_cimg_mp_size(arg1),
  18585. arg2,arg2==~0U?~0U:_cimg_mp_size(arg2)).move_to(code);
  18586. return_new_comp = true;
  18587. _cimg_mp_return(pos);
  18588. }
  18589. if (!std::strncmp(ss,"debug(",6)) { // Print debug info
  18590. _cimg_mp_op("Function 'debug()'");
  18591. p1 = code._width;
  18592. arg1 = compile(ss6,se1,depth1,p_ref,bloc_flags);
  18593. *se1 = 0;
  18594. variable_name.assign(CImg<charT>::string(ss6,true,true).unroll('y'),true);
  18595. cimg::strpare(variable_name,false,true);
  18596. ((CImg<ulongT>::vector((ulongT)mp_debug,arg1,0,code._width - p1),
  18597. variable_name)>'y').move_to(opcode);
  18598. opcode[2] = opcode._height;
  18599. opcode.move_to(code,p1);
  18600. *se1 = ')';
  18601. _cimg_mp_return(arg1);
  18602. }
  18603. if (!std::strncmp(ss,"deg2rad(",8)) { // Degrees to radians
  18604. _cimg_mp_op("Function 'deg2rad()'");
  18605. arg1 = compile(ss8,se1,depth1,0,bloc_flags);
  18606. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_deg2rad,arg1);
  18607. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*cimg::PI/180);
  18608. _cimg_mp_scalar1(mp_deg2rad,arg1);
  18609. }
  18610. if (!std::strncmp(ss,"display(",8)) { // Display memory, vector or image
  18611. _cimg_mp_op("Function 'display()'");
  18612. if (pexpr[se2 - expr._data]=='(') { // no arguments?
  18613. CImg<ulongT>::vector((ulongT)mp_display_memory,_cimg_mp_slot_nan).move_to(code);
  18614. _cimg_mp_return_nan();
  18615. }
  18616. if (*ss8!='#') { // Vector
  18617. s1 = ss8; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18618. arg1 = compile(ss8,s1,depth1,0,bloc_flags);
  18619. arg2 = 0; arg3 = arg4 = arg5 = 1;
  18620. if (s1<se1) {
  18621. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18622. arg2 = compile(s1 + 1,s2,depth1,0,bloc_flags);
  18623. if (s2<se1) {
  18624. s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  18625. arg3 = compile(s2,s3,depth1,0,bloc_flags);
  18626. if (s3<se1) {
  18627. s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18628. arg4 = compile(s3,s2,depth1,0,bloc_flags);
  18629. arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  18630. }
  18631. }
  18632. }
  18633. _cimg_mp_check_type(arg2,2,1,0);
  18634. _cimg_mp_check_type(arg3,3,1,0);
  18635. _cimg_mp_check_type(arg4,4,1,0);
  18636. _cimg_mp_check_type(arg5,5,1,0);
  18637. c1 = *s1; *s1 = 0;
  18638. variable_name.assign(CImg<charT>::string(ss8,true,true).unroll('y'),true);
  18639. cimg::strpare(variable_name,false,true);
  18640. if (_cimg_mp_is_vector(arg1))
  18641. ((CImg<ulongT>::vector((ulongT)mp_vector_print,arg1,0,(ulongT)_cimg_mp_size(arg1),0),
  18642. variable_name)>'y').move_to(opcode);
  18643. else
  18644. ((CImg<ulongT>::vector((ulongT)mp_print,arg1,0,0),
  18645. variable_name)>'y').move_to(opcode);
  18646. opcode[2] = opcode._height;
  18647. opcode.move_to(code);
  18648. ((CImg<ulongT>::vector((ulongT)mp_display,arg1,0,(ulongT)_cimg_mp_size(arg1),
  18649. arg2,arg3,arg4,arg5),
  18650. variable_name)>'y').move_to(opcode);
  18651. opcode[2] = opcode._height;
  18652. opcode.move_to(code);
  18653. *s1 = c1;
  18654. _cimg_mp_return(arg1);
  18655. } else { // Image
  18656. p1 = compile(ss8 + 1,se1,depth1,0,bloc_flags);
  18657. _cimg_mp_check_list();
  18658. CImg<ulongT>::vector((ulongT)mp_image_display,_cimg_mp_slot_nan,p1).move_to(code);
  18659. _cimg_mp_return_nan();
  18660. }
  18661. }
  18662. if (!std::strncmp(ss,"det(",4)) { // Matrix determinant
  18663. _cimg_mp_op("Function 'det()'");
  18664. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18665. _cimg_mp_check_matrix_square(arg1,1);
  18666. p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
  18667. _cimg_mp_scalar2(mp_det,arg1,p1);
  18668. }
  18669. if (!std::strncmp(ss,"diag(",5)) { // Diagonal matrix
  18670. _cimg_mp_op("Function 'diag()'");
  18671. CImg<ulongT>::vector((ulongT)mp_diag,0,0).move_to(l_opcode);
  18672. for (s = ss5; s<se; ++s) {
  18673. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18674. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18675. arg2 = compile(s,ns,depth1,0,bloc_flags);
  18676. if (_cimg_mp_is_vector(arg2))
  18677. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  18678. arg2 + (ulongT)_cimg_mp_size(arg2)).
  18679. move_to(l_opcode);
  18680. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  18681. s = ns;
  18682. }
  18683. (l_opcode>'y').move_to(opcode);
  18684. arg1 = opcode._height - 3;
  18685. pos = vector(arg1*arg1);
  18686. opcode[1] = pos;
  18687. opcode[2] = opcode._height;
  18688. opcode.move_to(code);
  18689. return_new_comp = true;
  18690. _cimg_mp_return(pos);
  18691. }
  18692. if (!std::strncmp(ss,"dot(",4)) { // Dot product
  18693. _cimg_mp_op("Function 'dot()'");
  18694. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18695. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  18696. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  18697. if (_cimg_mp_is_vector(arg1)) {
  18698. _cimg_mp_check_type(arg2,2,2,_cimg_mp_size(arg1));
  18699. _cimg_mp_scalar3(mp_dot,arg1,arg2,_cimg_mp_size(arg1));
  18700. }
  18701. _cimg_mp_check_type(arg2,2,1,0);
  18702. _cimg_mp_scalar2(mp_mul,arg1,arg2);
  18703. }
  18704. if (!std::strncmp(ss,"do(",3)) { // Do..while
  18705. _cimg_mp_op("Function 'do()'");
  18706. s0 = *ss2=='('?ss3:ss8;
  18707. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18708. arg1 = code._width;
  18709. arg6 = mempos;
  18710. p1 = compile(s0,s1,depth1,0,bloc_flags); // Body
  18711. arg2 = code._width;
  18712. p2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):p1; // Condition
  18713. _cimg_mp_check_type(p2,2,1,0);
  18714. CImg<ulongT>::vector((ulongT)mp_do,p1,p2,arg2 - arg1,code._width - arg2,_cimg_mp_size(p1),
  18715. p1>=arg6 && !_cimg_mp_is_const_scalar(p1),
  18716. p2>=arg6 && !_cimg_mp_is_const_scalar(p2)).move_to(code,arg1);
  18717. _cimg_mp_return(p1);
  18718. }
  18719. if (!std::strncmp(ss,"draw(",5)) { // Draw image
  18720. if (!is_inside_critical) is_parallelizable = false;
  18721. _cimg_mp_op("Function 'draw()'");
  18722. if (*ss5=='#') { // Index specified
  18723. s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18724. p1 = compile(ss6,s0++,depth1,0,bloc_flags);
  18725. _cimg_mp_check_list();
  18726. } else { p1 = ~0U; s0 = ss5; }
  18727. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18728. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  18729. arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
  18730. arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
  18731. arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
  18732. arg5 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
  18733. s0 = se1;
  18734. if (s1<se1) {
  18735. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18736. arg2 = compile(++s1,s0,depth1,0,bloc_flags);
  18737. if (_cimg_mp_is_vector(arg2)) { // Coordinates specified as a vector
  18738. p2 = _cimg_mp_size(arg2);
  18739. ++arg2;
  18740. if (p2>1) {
  18741. arg3 = arg2 + 1;
  18742. if (p2>2) {
  18743. arg4 = arg3 + 1;
  18744. if (p2>3) arg5 = arg4 + 1;
  18745. }
  18746. }
  18747. ++s0;
  18748. is_sth = true;
  18749. } else {
  18750. if (s0<se1) {
  18751. is_sth = p1!=~0U;
  18752. s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18753. arg3 = compile(++s0,s1,depth1,0,bloc_flags);
  18754. _cimg_mp_check_type(arg3,is_sth?4:3,1,0);
  18755. if (s1<se1) {
  18756. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18757. arg4 = compile(++s1,s0,depth1,0,bloc_flags);
  18758. _cimg_mp_check_type(arg4,is_sth?5:4,1,0);
  18759. if (s0<se1) {
  18760. s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18761. arg5 = compile(++s0,s1,depth1,0,bloc_flags);
  18762. _cimg_mp_check_type(arg5,is_sth?6:5,1,0);
  18763. s0 = ++s1;
  18764. }
  18765. }
  18766. }
  18767. is_sth = false;
  18768. }
  18769. }
  18770. l_opcode.assign(); // Don't use 'opcode': it could be modified by further calls to 'compile()'!
  18771. CImg<ulongT>::vector((ulongT)mp_draw,arg1,(ulongT)_cimg_mp_size(arg1),p1,arg2,arg3,arg4,arg5,
  18772. 0,0,0,0,1,(ulongT)~0U,0,1).move_to(l_opcode);
  18773. arg2 = arg3 = arg4 = arg5 = ~0U;
  18774. p2 = p1!=~0U?0:1;
  18775. if (s0<se1) {
  18776. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18777. arg2 = compile(s0,s1,depth1,0,bloc_flags);
  18778. _cimg_mp_check_type(arg2,p2 + (is_sth?3:6),1,0);
  18779. if (s1<se1) {
  18780. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18781. arg3 = compile(++s1,s0,depth1,0,bloc_flags);
  18782. _cimg_mp_check_type(arg3,p2 + (is_sth?4:7),1,0);
  18783. if (s0<se1) {
  18784. s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18785. arg4 = compile(++s0,s1,depth1,0,bloc_flags);
  18786. _cimg_mp_check_type(arg4,p2 + (is_sth?5:8),1,0);
  18787. if (s1<se1) {
  18788. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18789. arg5 = compile(++s1,s0,depth1,0,bloc_flags);
  18790. _cimg_mp_check_type(arg5,p2 + (is_sth?6:9),1,0);
  18791. }
  18792. }
  18793. }
  18794. }
  18795. if (s0<s1) s0 = s1;
  18796. l_opcode(0,8) = (ulongT)arg2;
  18797. l_opcode(0,9) = (ulongT)arg3;
  18798. l_opcode(0,10) = (ulongT)arg4;
  18799. l_opcode(0,11) = (ulongT)arg5;
  18800. if (s0<se1) {
  18801. s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18802. arg6 = compile(++s0,s1,depth1,0,bloc_flags);
  18803. _cimg_mp_check_type(arg6,0,1,0);
  18804. l_opcode(0,12) = arg6;
  18805. if (s1<se1) {
  18806. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18807. p2 = compile(++s1,s0,depth1,0,bloc_flags);
  18808. _cimg_mp_check_type(p2,0,2,0);
  18809. l_opcode(0,13) = p2;
  18810. l_opcode(0,14) = _cimg_mp_size(p2);
  18811. p3 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):1;
  18812. _cimg_mp_check_type(p3,0,1,0);
  18813. l_opcode(0,15) = p3;
  18814. }
  18815. }
  18816. l_opcode[0].move_to(code);
  18817. _cimg_mp_return(arg1);
  18818. }
  18819. break;
  18820. case 'e' :
  18821. if (!std::strncmp(ss,"echo(",5)) { // Echo
  18822. _cimg_mp_op("Function 'echo()'");
  18823. CImg<ulongT>::vector((ulongT)mp_echo,_cimg_mp_slot_nan,0).move_to(l_opcode);
  18824. for (s = ss5; s<se1; ++s) {
  18825. ns = s; while (ns<se1 && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18826. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18827. arg1 = compile(s,ns,depth1,0,bloc_flags);
  18828. CImg<ulongT>::vector(arg1,_cimg_mp_size(arg1)).move_to(l_opcode);
  18829. s = ns;
  18830. }
  18831. (l_opcode>'y').move_to(opcode);
  18832. opcode[2] = opcode._height;
  18833. opcode.move_to(code);
  18834. _cimg_mp_return_nan();
  18835. }
  18836. if (!std::strncmp(ss,"eig(",4)) { // Matrix eigenvalues/eigenvector
  18837. _cimg_mp_op("Function 'eig()'");
  18838. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18839. _cimg_mp_check_matrix_square(arg1,1);
  18840. p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
  18841. pos = vector((p1 + 1)*p1);
  18842. CImg<ulongT>::vector((ulongT)mp_matrix_eig,pos,arg1,p1).move_to(code);
  18843. return_new_comp = true;
  18844. _cimg_mp_return(pos);
  18845. }
  18846. if (!std::strncmp(ss,"ellipse(",8)) { // Ellipse/circle drawing
  18847. if (!is_inside_critical) is_parallelizable = false;
  18848. _cimg_mp_op("Function 'ellipse()'");
  18849. if (*ss8=='#') { // Index specified
  18850. s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18851. p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
  18852. _cimg_mp_check_list();
  18853. } else { p1 = ~0U; s0 = ss8; }
  18854. if (s0==se1) compile(s0,se1,depth1,0,bloc_flags); // 'missing' argument error
  18855. CImg<ulongT>::vector((ulongT)mp_ellipse,_cimg_mp_slot_nan,0,p1).move_to(l_opcode);
  18856. for (s = s0; s<se; ++s) {
  18857. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  18858. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  18859. arg2 = compile(s,ns,depth1,0,bloc_flags);
  18860. if (_cimg_mp_is_vector(arg2))
  18861. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  18862. arg2 + (ulongT)_cimg_mp_size(arg2)).
  18863. move_to(l_opcode);
  18864. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  18865. s = ns;
  18866. }
  18867. (l_opcode>'y').move_to(opcode);
  18868. opcode[2] = opcode._height;
  18869. opcode.move_to(code);
  18870. _cimg_mp_return_nan();
  18871. }
  18872. if (!std::strncmp(ss,"erf(",4)) { // Error function
  18873. _cimg_mp_op("Function 'erf()'");
  18874. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18875. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_erf,arg1);
  18876. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::erf(mem[arg1]));
  18877. _cimg_mp_scalar1(mp_erf,arg1);
  18878. }
  18879. if (!std::strncmp(ss,"erfinv(",7)) { // Inverse of error function
  18880. _cimg_mp_op("Function 'erfinv()'");
  18881. arg1 = compile(ss7,se1,depth1,0,bloc_flags);
  18882. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_erfinv,arg1);
  18883. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::erfinv(mem[arg1]));
  18884. _cimg_mp_scalar1(mp_erfinv,arg1);
  18885. }
  18886. if (!std::strncmp(ss,"exp(",4)) { // Exponential
  18887. _cimg_mp_op("Function 'exp()'");
  18888. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18889. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_exp,arg1);
  18890. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::exp(mem[arg1]));
  18891. _cimg_mp_scalar1(mp_exp,arg1);
  18892. }
  18893. if (!std::strncmp(ss,"expr(",5)) { // Vector from expression
  18894. _cimg_mp_op("Function 'expr()'");
  18895. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18896. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  18897. _cimg_mp_check_type(arg1,1,2,0);
  18898. p1 = _cimg_mp_size(arg1);
  18899. arg2 = arg3 = arg4 = arg5 = 0;
  18900. if (s1<se1) {
  18901. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18902. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  18903. _cimg_mp_check_const_scalar(arg2,2,2);
  18904. arg2 = (unsigned int)mem[arg2];
  18905. if (arg2) arg3 = arg4 = arg5 = 1;
  18906. if (s2<se1) {
  18907. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18908. arg3 = compile(++s2,s1,depth1,0,bloc_flags);
  18909. _cimg_mp_check_const_scalar(arg3,3,3);
  18910. arg3 = (unsigned int)mem[arg3];
  18911. if (s1<se1) {
  18912. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  18913. arg4 = compile(++s1,s2,depth1,0,bloc_flags);
  18914. _cimg_mp_check_const_scalar(arg4,4,3);
  18915. arg4 = (unsigned int)mem[arg4];
  18916. arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  18917. _cimg_mp_check_const_scalar(arg5,5,3);
  18918. arg5 = (unsigned int)mem[arg5];
  18919. }
  18920. }
  18921. }
  18922. p2 = arg2*arg3*arg4*arg5;
  18923. if (p2) pos = vector(p2); else pos = scalar();
  18924. CImg<ulongT>::vector((ulongT)mp_expr,pos,arg1,p1,arg2,arg3,arg4,arg5).move_to(code);
  18925. return_new_comp = true;
  18926. _cimg_mp_return(pos);
  18927. }
  18928. if (!std::strncmp(ss,"eye(",4)) { // Identity matrix
  18929. _cimg_mp_op("Function 'eye()'");
  18930. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  18931. _cimg_mp_check_const_scalar(arg1,1,3);
  18932. p1 = (unsigned int)mem[arg1];
  18933. pos = vector(p1*p1);
  18934. CImg<ulongT>::vector((ulongT)mp_eye,pos,p1).move_to(code);
  18935. return_new_comp = true;
  18936. _cimg_mp_return(pos);
  18937. }
  18938. if (!std::strncmp(ss,"end(",4)) { // End
  18939. _cimg_mp_op("Function 'end()'");
  18940. s1 = ss4; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
  18941. if (s1!=se1) {
  18942. const bool is_inside_end = (bool)(bloc_flags&8);
  18943. if (!is_inside_end) code.swap(code_end);
  18944. compile(s1,se1,depth1,p_ref,8);
  18945. if (!is_inside_end) code.swap(code_end);
  18946. is_end_code = true;
  18947. }
  18948. _cimg_mp_return_nan();
  18949. }
  18950. if (!std::strncmp(ss,"end_t(",6)) { // End thread
  18951. _cimg_mp_op("Function 'end_t()'");
  18952. s1 = ss6; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
  18953. if (s1!=se1) {
  18954. const bool is_inside_end = (bool)(bloc_flags&16);
  18955. if (!is_inside_end) code.swap(code_end_t);
  18956. compile(s1,se1,depth1,p_ref,16);
  18957. if (!is_inside_end) code.swap(code_end_t);
  18958. is_end_code = true;
  18959. }
  18960. _cimg_mp_return_nan();
  18961. }
  18962. break;
  18963. case 'f' :
  18964. if (!std::strncmp(ss,"f2ui(",5)) { // Special float->uint conversion
  18965. _cimg_mp_op("Function 'f2ui()'");
  18966. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18967. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_f2ui,arg1);
  18968. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((double)cimg::float2uint((float)mem[arg1]));
  18969. _cimg_mp_scalar1(mp_f2ui,arg1);
  18970. }
  18971. if (!std::strncmp(ss,"fact(",5)) { // Factorial
  18972. _cimg_mp_op("Function 'fact()'");
  18973. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18974. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_factorial,arg1);
  18975. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::factorial((int)mem[arg1]));
  18976. _cimg_mp_scalar1(mp_factorial,arg1);
  18977. }
  18978. if (!std::strncmp(ss,"fibo(",5)) { // Fibonacci
  18979. _cimg_mp_op("Function 'fibo()'");
  18980. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  18981. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_fibonacci,arg1);
  18982. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::fibonacci((int)mem[arg1]));
  18983. _cimg_mp_scalar1(mp_fibonacci,arg1);
  18984. }
  18985. if (!std::strncmp(ss,"fill(",5)) { // Fill
  18986. _cimg_mp_op("Function 'fill()'");
  18987. s0 = ss5; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  18988. arg1 = compile(ss5,s0,depth1,0,bloc_flags); // Object to fill
  18989. if (_cimg_mp_is_const_scalar(arg1))
  18990. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  18991. "CImg<%s>::%s: %s: Target scalar is constant, "
  18992. "in expression '%s'.",
  18993. pixel_type(),_cimg_mp_calling_function,s_op,ss);
  18994. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  18995. p1 = code._width;
  18996. if (s1<se1) { // Version with 3 arguments
  18997. variable_name.assign(s0,(unsigned int)(s1 + 1 - s0)).back() = 0;
  18998. cimg::strpare(variable_name,false,true);
  18999. if (!is_varname(variable_name)) { // Invalid variable name
  19000. cimg::strellipsize(variable_name,64);
  19001. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19002. "CImg<%s>::%s: %s: Invalid loop variable name '%s', "
  19003. "in expression '%s'.",
  19004. pixel_type(),_cimg_mp_calling_function,s_op,
  19005. variable_name._data,s0);
  19006. }
  19007. get_variable_pos(variable_name,arg2,arg3);
  19008. arg2 = arg3!=~0U?reserved_label[arg3]:arg2!=~0U?variable_pos[arg2]:~0U; // Variable slot
  19009. if (arg2!=~0U && (!_cimg_mp_is_scalar(arg2) ||
  19010. _cimg_mp_is_const_scalar(arg2))) { // Variable is not a vector or is a constant->error
  19011. cimg::strellipsize(variable_name,64);
  19012. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19013. "CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' "
  19014. "(expected 'scalar'), in expression '%s'.",
  19015. pixel_type(),_cimg_mp_calling_function,s_op,
  19016. s_type(arg2)._data,variable_name._data,s0);
  19017. } else if (arg2==~0U) { // Variable does not exist -> create it
  19018. arg2 = scalar();
  19019. if (arg3!=~0U) reserved_label[arg3] = arg2;
  19020. else {
  19021. if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
  19022. variable_pos[variable_def._width] = arg2;
  19023. variable_name.move_to(variable_def);
  19024. }
  19025. memtype[arg2] = -1;
  19026. }
  19027. arg3 = compile(++s1,se1,depth1,0,bloc_flags);
  19028. _cimg_mp_check_type(arg3,3,1,0);
  19029. } else { // Version with 2 arguments
  19030. arg2 = ~0U;
  19031. arg3 = compile(s0,se1,depth1,0,bloc_flags);
  19032. }
  19033. // arg2 = variable slot, arg3 = fill expression.
  19034. _cimg_mp_check_type(arg3,3,1,0);
  19035. CImg<ulongT>::vector((ulongT)mp_fill,arg1,_cimg_mp_size(arg1),arg2,arg3,code._width - p1).
  19036. move_to(code,p1);
  19037. _cimg_mp_return(arg1);
  19038. }
  19039. if (!std::strncmp(ss,"find(",5)) { // Find
  19040. _cimg_mp_op("Function 'find()'");
  19041. // First argument: data to look at.
  19042. s0 = ss5; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19043. if (*ss5=='#') { // Index specified
  19044. p1 = compile(ss6,s0,depth1,0,bloc_flags);
  19045. _cimg_mp_check_list();
  19046. arg1 = ~0U;
  19047. } else { // Vector specified
  19048. arg1 = compile(ss5,s0,depth1,0,bloc_flags);
  19049. _cimg_mp_check_type(arg1,1,2,0);
  19050. p1 = ~0U;
  19051. }
  19052. // Second argument: data to find.
  19053. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19054. arg2 = compile(s0,s1,depth1,0,bloc_flags);
  19055. // Third and fourth arguments: starting index and search direction.
  19056. arg3 = _cimg_mp_slot_nan; arg4 = 1;
  19057. if (s1<se1) {
  19058. s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19059. arg3 = compile(++s1,s0,depth1,0,bloc_flags);
  19060. _cimg_mp_check_type(arg3,3,1,0);
  19061. if (s0<se1) {
  19062. arg4 = compile(++s0,se1,depth1,0,bloc_flags);
  19063. _cimg_mp_check_type(arg4,4,1,0);
  19064. }
  19065. }
  19066. if (p1!=~0U) {
  19067. if (_cimg_mp_size(arg2)>1)
  19068. _cimg_mp_scalar5(mp_list_find_seq,p1,arg2,_cimg_mp_size(arg2),arg3,arg4);
  19069. _cimg_mp_scalar4(mp_list_find,p1,arg2 + (_cimg_mp_size(arg2)?1:0),arg3,arg4);
  19070. }
  19071. if (_cimg_mp_size(arg2)>1)
  19072. _cimg_mp_scalar6(mp_find_seq,arg1,_cimg_mp_size(arg1),arg2,_cimg_mp_size(arg2),arg3,arg4);
  19073. _cimg_mp_scalar5(mp_find,arg1,_cimg_mp_size(arg1),arg2 + (_cimg_mp_size(arg2)?1:0),arg3,arg4);
  19074. }
  19075. if (*ss1=='o' && *ss2=='r' && *ss3=='(') { // For loop
  19076. _cimg_mp_op("Function 'for()'");
  19077. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19078. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19079. s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  19080. arg1 = code._width;
  19081. p1 = compile(ss4,s1,depth1,0,bloc_flags); // Init
  19082. arg2 = code._width;
  19083. p2 = compile(++s1,s2,depth1,0,bloc_flags); // Cond
  19084. arg3 = code._width;
  19085. arg6 = mempos;
  19086. if (s3<se1) { // Body + post
  19087. p3 = compile(s3 + 1,se1,depth1,0,bloc_flags); // Body
  19088. arg4 = code._width;
  19089. pos = compile(++s2,s3,depth1,0,bloc_flags); // Post
  19090. } else {
  19091. p3 = compile(++s2,se1,depth1,0,bloc_flags); // Body only
  19092. arg4 = pos = code._width;
  19093. }
  19094. _cimg_mp_check_type(p2,2,1,0);
  19095. arg5 = _cimg_mp_size(pos);
  19096. CImg<ulongT>::vector((ulongT)mp_for,p3,(ulongT)_cimg_mp_size(p3),p2,arg2 - arg1,arg3 - arg2,
  19097. arg4 - arg3,code._width - arg4,
  19098. p3>=arg6 && !_cimg_mp_is_const_scalar(p3),
  19099. p2>=arg6 && !_cimg_mp_is_const_scalar(p2)).move_to(code,arg1);
  19100. _cimg_mp_return(p3);
  19101. }
  19102. if (!std::strncmp(ss,"floor(",6)) { // Floor
  19103. _cimg_mp_op("Function 'floor()'");
  19104. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19105. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_floor,arg1);
  19106. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::floor(mem[arg1]));
  19107. _cimg_mp_scalar1(mp_floor,arg1);
  19108. }
  19109. if (!std::strncmp(ss,"fsize(",6)) { // File size
  19110. _cimg_mp_op("Function 'fsize()'");
  19111. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19112. _cimg_mp_check_type(arg1,1,2,0);
  19113. pos = scalar();
  19114. CImg<ulongT>::vector((ulongT)mp_fsize,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
  19115. return_new_comp = true;
  19116. _cimg_mp_return(pos);
  19117. }
  19118. break;
  19119. case 'g' :
  19120. if (!std::strncmp(ss,"gauss(",6)) { // Gaussian function
  19121. _cimg_mp_op("Function 'gauss()'");
  19122. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19123. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  19124. arg2 = arg3 = 1;
  19125. if (s1<se1) {
  19126. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19127. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19128. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
  19129. }
  19130. _cimg_mp_check_type(arg2,2,1,0);
  19131. _cimg_mp_check_type(arg3,3,1,0);
  19132. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_gauss,arg1,arg2,arg3);
  19133. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3)) {
  19134. val1 = mem[arg1];
  19135. val2 = mem[arg2];
  19136. _cimg_mp_const_scalar(std::exp(-val1*val1/(2*val2*val2))/(mem[arg3]?std::sqrt(2*val2*val2*cimg::PI):1));
  19137. }
  19138. _cimg_mp_scalar3(mp_gauss,arg1,arg2,arg3);
  19139. }
  19140. if (!std::strncmp(ss,"gcd(",4)) { // Gcd
  19141. _cimg_mp_op("Function 'gcd()'");
  19142. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19143. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  19144. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  19145. _cimg_mp_check_type(arg1,1,1,0);
  19146. _cimg_mp_check_type(arg2,2,1,0);
  19147. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  19148. _cimg_mp_const_scalar(cimg::gcd((long)mem[arg1],(long)mem[arg2]));
  19149. _cimg_mp_scalar2(mp_gcd,arg1,arg2);
  19150. }
  19151. #ifdef cimg_mp_func_get
  19152. if (!std::strncmp(ss,"get(",4)) { // Get value/vector from external variable
  19153. _cimg_mp_op("Function 'get()'");
  19154. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19155. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  19156. arg2 = arg3 = 0;
  19157. if (s1<se1) {
  19158. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19159. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19160. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  19161. }
  19162. _cimg_mp_check_type(arg1,1,2,0);
  19163. _cimg_mp_check_const_scalar(arg2,2,2);
  19164. _cimg_mp_check_type(arg3,3,1,0);
  19165. p1 = _cimg_mp_size(arg1);
  19166. arg2 = (unsigned int)mem[arg2];
  19167. if (arg2) pos = vector(arg2); else pos = scalar();
  19168. CImg<ulongT>::vector((ulongT)mp_get,pos,arg1,p1,arg2,arg3).move_to(code);
  19169. return_new_comp = true;
  19170. _cimg_mp_return(pos);
  19171. }
  19172. #endif
  19173. break;
  19174. case 'h' :
  19175. if (*ss1=='(') { // Image height
  19176. _cimg_mp_op("Function 'h()'");
  19177. if (*ss2=='#') { // Index specified
  19178. p1 = compile(ss3,se1,depth1,0,bloc_flags);
  19179. _cimg_mp_check_list();
  19180. } else { if (ss2!=se1) break; p1 = ~0U; }
  19181. _cimg_mp_scalar1(mp_image_h,p1);
  19182. }
  19183. break;
  19184. case 'i' :
  19185. if (*ss1=='c' && *ss2=='(') { // Image median
  19186. _cimg_mp_op("Function 'ic()'");
  19187. if (*ss3=='#') { // Index specified
  19188. p1 = compile(ss4,se1,depth1,0,bloc_flags);
  19189. _cimg_mp_check_list();
  19190. } else { if (ss3!=se1) break; p1 = ~0U; }
  19191. pos = scalar();
  19192. CImg<ulongT>::vector((ulongT)mp_image_median,pos,p1).move_to(code);
  19193. return_new_comp = true;
  19194. _cimg_mp_return(pos);
  19195. }
  19196. if (*ss1=='n' && *ss2=='(') { // Image norm
  19197. _cimg_mp_op("Function 'in()'");
  19198. if (*ss3=='#') { // Index specified
  19199. p1 = compile(ss4,se1,depth1,0,bloc_flags);
  19200. _cimg_mp_check_list();
  19201. } else { if (ss3!=se1) break; p1 = ~0U; }
  19202. pos = scalar();
  19203. CImg<ulongT>::vector((ulongT)mp_image_norm,pos,p1).move_to(code);
  19204. return_new_comp = true;
  19205. _cimg_mp_return(pos);
  19206. }
  19207. if (*ss1=='f' && *ss2=='(') { // If..then[..else.]
  19208. _cimg_mp_op("Function 'if()'");
  19209. s1 = ss3; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19210. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19211. arg1 = compile(ss3,s1,depth1,0,bloc_flags);
  19212. _cimg_mp_check_type(arg1,1,1,0);
  19213. if (_cimg_mp_is_const_scalar(arg1)) {
  19214. if ((bool)mem[arg1]) return compile(++s1,s2,depth1,0,bloc_flags);
  19215. else return s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  19216. }
  19217. p2 = code._width;
  19218. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19219. p3 = code._width;
  19220. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):
  19221. _cimg_mp_is_vector(arg2)?vector(_cimg_mp_size(arg2),0):0;
  19222. _cimg_mp_check_type(arg3,3,_cimg_mp_is_vector(arg2)?2:1,_cimg_mp_size(arg2));
  19223. arg4 = _cimg_mp_size(arg2);
  19224. if (arg4) pos = vector(arg4); else pos = scalar();
  19225. CImg<ulongT>::vector((ulongT)mp_if,pos,arg1,arg2,arg3,
  19226. p3 - p2,code._width - p3,arg4).move_to(code,p2);
  19227. return_new_comp = true;
  19228. _cimg_mp_return(pos);
  19229. }
  19230. if (!std::strncmp(ss,"inrange(",8)) { // Check value range
  19231. _cimg_mp_op("Function 'inrange()'");
  19232. s1 = ss8; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19233. arg1 = compile(ss8,s1,depth1,0,bloc_flags);
  19234. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19235. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19236. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19237. arg3 = compile(++s2,s1,depth1,0,bloc_flags);
  19238. arg4 = arg5 = 1;
  19239. if (s1<se1) {
  19240. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19241. arg4 = compile(++s1,s2,depth1,0,bloc_flags);
  19242. arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):arg4;
  19243. _cimg_mp_check_type(arg4,4,1,0);
  19244. _cimg_mp_check_type(arg5,5,1,0);
  19245. }
  19246. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) &&
  19247. _cimg_mp_is_const_scalar(arg3) && _cimg_mp_is_const_scalar(arg4) &&
  19248. _cimg_mp_is_const_scalar(arg5)) { // Optimize constant case
  19249. val = mem[arg1]; val1 = mem[arg2]; val2 = mem[arg3];
  19250. if (val2>=val1)
  19251. is_sth = (mem[arg4]?(val>=val1):(val>val1)) && (mem[arg5]?(val<=val2):(val<val2));
  19252. else
  19253. is_sth = (mem[arg5]?(val>=val2):(val>val2)) && (mem[arg4]?(val<=val1):(val<val1));
  19254. _cimg_mp_return(is_sth?1:0);
  19255. }
  19256. p1 = _cimg_mp_size(arg1);
  19257. p2 = _cimg_mp_size(arg2);
  19258. p3 = _cimg_mp_size(arg3);
  19259. arg6 = ~0U; // Size of return value
  19260. if (!p1) {
  19261. arg6 = p2?p2:p3;
  19262. if (arg6) _cimg_mp_check_type(arg3,3,3,arg6);
  19263. } else {
  19264. arg6 = p1;
  19265. _cimg_mp_check_type(arg2,2,3,arg6);
  19266. _cimg_mp_check_type(arg3,3,3,arg6);
  19267. }
  19268. pos = arg6?vector(arg6):scalar();
  19269. CImg<ulongT>::vector((ulongT)mp_inrange,pos,arg6,arg1,p1,arg2,p2,arg3,p3,arg4,arg5).move_to(code);
  19270. return_new_comp = true;
  19271. _cimg_mp_return(pos);
  19272. }
  19273. if (!std::strncmp(ss,"int(",4)) { // Integer cast
  19274. _cimg_mp_op("Function 'int()'");
  19275. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  19276. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_int,arg1);
  19277. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((longT)mem[arg1]);
  19278. _cimg_mp_scalar1(mp_int,arg1);
  19279. }
  19280. if (!std::strncmp(ss,"invert(",7)) { // Matrix/scalar inversion
  19281. _cimg_mp_op("Function 'invert()'");
  19282. s1 = ss7; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19283. arg1 = compile(ss7,s1,depth1,0,bloc_flags);
  19284. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
  19285. _cimg_mp_check_type(arg2,2,1,0);
  19286. if (_cimg_mp_is_vector(arg1)) {
  19287. _cimg_mp_check_matrix_square(arg1,1);
  19288. p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
  19289. pos = vector(p1*p1);
  19290. CImg<ulongT>::vector((ulongT)mp_matrix_invert,pos,arg1,p1,arg2).move_to(code);
  19291. return_new_comp = true;
  19292. _cimg_mp_return(pos);
  19293. }
  19294. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(1/mem[arg1]);
  19295. _cimg_mp_scalar2(mp_div,1,arg1);
  19296. }
  19297. if (*ss1=='s') { // Family of 'is_?()' functions
  19298. if (!std::strncmp(ss,"isbool(",7)) { // Is boolean?
  19299. _cimg_mp_op("Function 'isbool()'");
  19300. if (ss7==se1) _cimg_mp_return(0);
  19301. try { arg1 = compile(ss7,se1,depth1,0,bloc_flags); }
  19302. catch(CImgException&) { _cimg_mp_return(0); }
  19303. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isbool,arg1);
  19304. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_return(mem[arg1]==0. || mem[arg1]==1.);
  19305. _cimg_mp_scalar1(mp_isbool,arg1);
  19306. }
  19307. if (!std::strncmp(ss,"isdir(",6)) { // Is directory?
  19308. _cimg_mp_op("Function 'isdir()'");
  19309. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19310. pos = scalar();
  19311. CImg<ulongT>::vector((ulongT)mp_isdir,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
  19312. return_new_comp = true;
  19313. _cimg_mp_return(pos);
  19314. }
  19315. if (!std::strncmp(ss,"isfile(",7)) { // Is file?
  19316. _cimg_mp_op("Function 'isfile()'");
  19317. arg1 = compile(ss7,se1,depth1,0,bloc_flags);
  19318. pos = scalar();
  19319. CImg<ulongT>::vector((ulongT)mp_isfile,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
  19320. return_new_comp = true;
  19321. _cimg_mp_return(pos);
  19322. }
  19323. if (!std::strncmp(ss,"isin(",5)) { // Is in sequence/vector?
  19324. if (ss5>=se1) _cimg_mp_return(0);
  19325. _cimg_mp_op("Function 'isin()'");
  19326. pos = scalar();
  19327. CImg<ulongT>::vector((ulongT)mp_isin,pos,0).move_to(l_opcode);
  19328. for (s = ss5; s<se; ++s) {
  19329. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19330. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19331. arg1 = compile(s,ns,depth1,0,bloc_flags);
  19332. if (_cimg_mp_is_vector(arg1))
  19333. CImg<ulongT>::sequence(_cimg_mp_size(arg1),arg1 + 1,
  19334. arg1 + (ulongT)_cimg_mp_size(arg1)).
  19335. move_to(l_opcode);
  19336. else CImg<ulongT>::vector(arg1).move_to(l_opcode);
  19337. s = ns;
  19338. }
  19339. (l_opcode>'y').move_to(opcode);
  19340. opcode[2] = opcode._height;
  19341. opcode.move_to(code);
  19342. return_new_comp = true;
  19343. _cimg_mp_return(pos);
  19344. }
  19345. if (!std::strncmp(ss,"isinf(",6)) { // Is infinite?
  19346. _cimg_mp_op("Function 'isinf()'");
  19347. if (ss6==se1) _cimg_mp_return(0);
  19348. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19349. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isinf,arg1);
  19350. if (_cimg_mp_is_const_scalar(arg1))
  19351. _cimg_mp_return((unsigned int)cimg::type<double>::is_inf(mem[arg1]));
  19352. _cimg_mp_scalar1(mp_isinf,arg1);
  19353. }
  19354. if (!std::strncmp(ss,"isint(",6)) { // Is integer?
  19355. _cimg_mp_op("Function 'isint()'");
  19356. if (ss6==se1) _cimg_mp_return(0);
  19357. try { arg1 = compile(ss6,se1,depth1,0,bloc_flags); }
  19358. catch(CImgException&) { _cimg_mp_return(0); }
  19359. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isint,arg1);
  19360. if (_cimg_mp_is_const_scalar(arg1))
  19361. _cimg_mp_return((unsigned int)((double)(longT)mem[arg1]==mem[arg1]));
  19362. _cimg_mp_scalar1(mp_isint,arg1);
  19363. }
  19364. if (!std::strncmp(ss,"isnan(",6)) { // Is NaN?
  19365. _cimg_mp_op("Function 'isnan()'");
  19366. if (ss6==se1) _cimg_mp_return(0);
  19367. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19368. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isnan,arg1);
  19369. if (_cimg_mp_is_const_scalar(arg1))
  19370. _cimg_mp_return((unsigned int)cimg::type<double>::is_nan(mem[arg1]));
  19371. _cimg_mp_scalar1(mp_isnan,arg1);
  19372. }
  19373. if (!std::strncmp(ss,"isnum(",6)) { // Is number?
  19374. _cimg_mp_op("Function 'isnum()'");
  19375. val = 0;
  19376. if (cimg_sscanf(ss6,"%lf%c%c",&val,&sep,&end)==2 && sep==')') _cimg_mp_return(1);
  19377. _cimg_mp_return(0);
  19378. }
  19379. if (!std::strncmp(ss,"isexpr(",7)) { // Is valid expression?
  19380. _cimg_mp_op("Function 'isexpr()'");
  19381. if (ss7==se1) _cimg_mp_return(0);
  19382. try { arg1 = compile(ss7,se1,depth1,0,bloc_flags); }
  19383. catch (CImgException&) { _cimg_mp_return(0); }
  19384. _cimg_mp_return(1);
  19385. }
  19386. if (!std::strncmp(ss,"isvarname(",10)) { // Is variable name?
  19387. _cimg_mp_op("Function 'isvarname()'");
  19388. arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
  19389. pos = scalar();
  19390. CImg<ulongT>::vector((ulongT)mp_isvarname,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
  19391. return_new_comp = true;
  19392. _cimg_mp_return(pos);
  19393. }
  19394. }
  19395. break;
  19396. case 'l' :
  19397. if (*ss1=='(') { // Size of image list
  19398. _cimg_mp_op("Function 'l()'");
  19399. if (ss2!=se1) break;
  19400. _cimg_mp_scalar0(mp_list_l);
  19401. }
  19402. if (!std::strncmp(ss,"lerp(",5)) { // Linear interpolation
  19403. _cimg_mp_op("Function 'lerp()'");
  19404. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19405. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  19406. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19407. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19408. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):16; // Default value is 0.5
  19409. _cimg_mp_check_type(arg3,3,1,0);
  19410. if (_cimg_mp_is_const_scalar(arg3)) { // Optimize constant cases
  19411. if (!arg3) _cimg_mp_return(arg1);
  19412. if (arg3==1) _cimg_mp_return(arg2);
  19413. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2)) {
  19414. const double t = mem[arg3];
  19415. _cimg_mp_const_scalar(mem[arg1]*(1-t) + mem[arg2]*t);
  19416. }
  19417. }
  19418. if (_cimg_mp_is_scalar(arg1)) {
  19419. _cimg_mp_check_type(arg2,2,1,0);
  19420. _cimg_mp_scalar3(mp_lerp,arg1,arg2,arg3);
  19421. }
  19422. p1 = _cimg_mp_size(arg1);
  19423. _cimg_mp_check_type(arg2,2,2,p1);
  19424. pos = vector(p1);
  19425. CImg<ulongT>::vector((ulongT)mp_vector_lerp,pos,p1,arg1,arg2,arg3).move_to(code);
  19426. return_new_comp = true;
  19427. _cimg_mp_return(pos);
  19428. }
  19429. if (!std::strncmp(ss,"log(",4)) { // Natural logarithm
  19430. _cimg_mp_op("Function 'log()'");
  19431. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  19432. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log,arg1);
  19433. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::log(mem[arg1]));
  19434. _cimg_mp_scalar1(mp_log,arg1);
  19435. }
  19436. if (!std::strncmp(ss,"log2(",5)) { // Base-2 logarithm
  19437. _cimg_mp_op("Function 'log2()'");
  19438. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  19439. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log2,arg1);
  19440. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::log2(mem[arg1]));
  19441. _cimg_mp_scalar1(mp_log2,arg1);
  19442. }
  19443. if (!std::strncmp(ss,"log10(",6)) { // Base-10 logarithm
  19444. _cimg_mp_op("Function 'log10()'");
  19445. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  19446. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log10,arg1);
  19447. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::log10(mem[arg1]));
  19448. _cimg_mp_scalar1(mp_log10,arg1);
  19449. }
  19450. if (!std::strncmp(ss,"lowercase(",10)) { // Lower case
  19451. _cimg_mp_op("Function 'lowercase()'");
  19452. arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
  19453. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_lowercase,arg1);
  19454. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::lowercase(mem[arg1]));
  19455. _cimg_mp_scalar1(mp_lowercase,arg1);
  19456. }
  19457. break;
  19458. case 'm' :
  19459. if (!std::strncmp(ss,"mul(",4)) { // Matrix multiplication
  19460. _cimg_mp_op("Function 'mul()'");
  19461. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19462. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  19463. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19464. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19465. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
  19466. _cimg_mp_check_type(arg1,1,2,0);
  19467. _cimg_mp_check_type(arg2,2,2,0);
  19468. _cimg_mp_check_const_scalar(arg3,3,3);
  19469. p1 = _cimg_mp_size(arg1);
  19470. p2 = _cimg_mp_size(arg2);
  19471. p3 = (unsigned int)mem[arg3];
  19472. arg5 = p2/p3;
  19473. arg4 = p1/arg5;
  19474. if (arg4*arg5!=p1 || arg5*p3!=p2) {
  19475. _cimg_mp_strerr;
  19476. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19477. "CImg<%s>::%s: %s: Types of first and second arguments ('%s' and '%s') "
  19478. "do not match with third argument 'nb_colsB=%u', "
  19479. "in expression '%s'.",
  19480. pixel_type(),_cimg_mp_calling_function,s_op,
  19481. s_type(arg1)._data,s_type(arg2)._data,p3,s0);
  19482. }
  19483. pos = vector(arg4*p3);
  19484. CImg<ulongT>::vector((ulongT)mp_matrix_mul,pos,arg1,arg2,arg4,arg5,p3).move_to(code);
  19485. return_new_comp = true;
  19486. _cimg_mp_return(pos);
  19487. }
  19488. if (!std::strncmp(ss,"mproj(",6)) { // Project matrix onto dictionary
  19489. _cimg_mp_op("Function 'mproj()'");
  19490. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19491. arg1 = compile(ss6,s1,depth1,0,bloc_flags); // S
  19492. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19493. arg2 = compile(++s1,s2,depth1,0,bloc_flags); // ncolS
  19494. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19495. arg3 = compile(++s2,s1,depth1,0,bloc_flags); // D
  19496. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19497. arg4 = compile(++s1,s2,depth1,0,bloc_flags); // ncolD
  19498. arg5 = arg6 = p3 = 0;
  19499. if (s2<se1) {
  19500. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19501. arg5 = compile(++s2,s1,depth1,0,bloc_flags); // method
  19502. if (s1<se1) {
  19503. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19504. arg6 = compile(++s1,s2,depth1,0,bloc_flags); // max_iter
  19505. p3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0; // method
  19506. }
  19507. }
  19508. _cimg_mp_check_type(arg1,1,2,0);
  19509. _cimg_mp_check_const_scalar(arg2,2,3);
  19510. _cimg_mp_check_type(arg3,3,2,0);
  19511. _cimg_mp_check_const_scalar(arg4,4,3);
  19512. _cimg_mp_check_type(arg5,5,1,0);
  19513. _cimg_mp_check_type(arg6,6,1,0);
  19514. _cimg_mp_check_type(p3,7,1,0);
  19515. p1 = _cimg_mp_size(arg1);
  19516. p2 = _cimg_mp_size(arg3);
  19517. const unsigned int
  19518. wS = (unsigned int)mem[arg2],
  19519. wD = (unsigned int)mem[arg4],
  19520. hS = p1/wS,
  19521. hD = p2/wD;
  19522. if (wS*hS!=p1) {
  19523. _cimg_mp_strerr;
  19524. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19525. "CImg<%s>::%s: %s: Type of first argument ('%s') "
  19526. "do not match with second argument 'nb_colsS=%u', "
  19527. "in expression '%s'.",
  19528. pixel_type(),_cimg_mp_calling_function,s_op,
  19529. s_type(arg1)._data,wS,s0);
  19530. }
  19531. if (wD*hD!=p2) {
  19532. _cimg_mp_strerr;
  19533. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19534. "CImg<%s>::%s: %s: Type of third argument ('%s') "
  19535. "do not match with fourth argument 'nb_colsD=%u', "
  19536. "in expression '%s'.",
  19537. pixel_type(),_cimg_mp_calling_function,s_op,
  19538. s_type(arg3)._data,wD,s0);
  19539. }
  19540. if (hS!=hD) {
  19541. _cimg_mp_strerr;
  19542. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19543. "CImg<%s>::%s: %s: Type of first argument ('%s') "
  19544. "do not match with third argument ('%s'), "
  19545. "in expression '%s'.",
  19546. pixel_type(),_cimg_mp_calling_function,s_op,
  19547. s_type(arg1)._data,s_type(arg3)._data,s0);
  19548. }
  19549. pos = vector(wS*wD);
  19550. CImg<ulongT>::vector((ulongT)mp_mproj,pos,arg1,wS,hS,arg3,wD,arg5,arg6,p3).move_to(code);
  19551. return_new_comp = true;
  19552. _cimg_mp_return(pos);
  19553. }
  19554. if (!std::strncmp(ss,"merge(",6)) { // Merge inter-thread variables
  19555. _cimg_mp_op("Function 'merge()'");
  19556. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19557. ref.assign(7);
  19558. pos = compile(ss6,s1,depth1,ref,bloc_flags);
  19559. if (*ref) {
  19560. _cimg_mp_strerr;
  19561. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19562. "CImg<%s>::%s: %s: First argument cannot be a linked reference, "
  19563. "in expression '%s'.",
  19564. pixel_type(),_cimg_mp_calling_function,s_op,
  19565. s0);
  19566. }
  19567. arg1 = ~0U; // Merge operator
  19568. // (0='=',1='+',2='-',3='*',4='/',5='&',6='|',7='xor',8='&&',9=='||',10='min',11='max')
  19569. if (s1<se1) {
  19570. ++s1;
  19571. char st_op[4] = { 0 };
  19572. is_sth = false; // blank after operator?
  19573. if (cimg_sscanf(s1," %3[=+-*/&|minaxor]%c",st_op,&sep)==2 && (sep==')' ||
  19574. (is_sth=cimg::is_blank(sep)))) {
  19575. if (!is_sth || (is_sth && cimg_sscanf(s1," %*[=+-*/&|minaxor ]%c",&sep)==1 && sep==')')) {
  19576. cimg::strpare(st_op,' ',false,true);
  19577. if (!st_op[1])
  19578. arg1 = *st_op=='='?0:*st_op=='+'?1:*st_op=='-'?2:*st_op=='*'?3:*st_op=='/'?4:
  19579. *st_op=='&'?5:*st_op=='|'?6:~0U;
  19580. else if (*st_op=='x' && st_op[1]=='o' && st_op[2]=='r' && !st_op[3]) arg1 = 7;
  19581. else if (*st_op=='&' && st_op[1]=='&' && !st_op[2]) arg1 = 8;
  19582. else if (*st_op=='|' && st_op[1]=='|' && !st_op[2]) arg1 = 9;
  19583. else if (*st_op=='m' && st_op[1]=='i' && st_op[2]=='n' && !st_op[3]) arg1 = 10;
  19584. else if (*st_op=='m' && st_op[1]=='a' && st_op[2]=='x' && !st_op[3]) arg1 = 11;
  19585. }
  19586. }
  19587. }
  19588. cimg_rofY(memmerge,k) if (memmerge(0,k)==(int)pos) {
  19589. _cimg_mp_strerr;
  19590. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19591. "CImg<%s>::%s: %s: Merge has already been requested before "
  19592. "for specified variable "
  19593. "in expression '%s'.",
  19594. pixel_type(),_cimg_mp_calling_function,s_op,s0);
  19595. }
  19596. if (arg1==~0U) {
  19597. _cimg_mp_strerr;
  19598. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19599. "CImg<%s>::%s: %s: Invalid specified operator "
  19600. "(should be one of '=,+,-,*,/,&,|,xor,&&,||,min,max'), "
  19601. "in expression '%s'.",
  19602. pixel_type(),_cimg_mp_calling_function,s_op,s0);
  19603. }
  19604. memmerge.resize(3,memmerge._height + 1,1,1,0,0);
  19605. memmerge(0,memmerge._height - 1) = (int)pos;
  19606. memmerge(1,memmerge._height - 1) = (int)_cimg_mp_size(pos);
  19607. memmerge(2,memmerge._height - 1) = (int)arg1;
  19608. _cimg_mp_return(pos);
  19609. }
  19610. break;
  19611. case 'n' :
  19612. #ifdef cimg_mp_func_name
  19613. if (!std::strncmp(ss,"name(",5)) { // Get image name as a string vector
  19614. _cimg_mp_op("Function 'name()'");
  19615. if (*ss5=='#') { // Index specified
  19616. s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19617. p1 = compile(ss6,s0++,depth1,0,bloc_flags);
  19618. is_sth = true; // is_index_specified?
  19619. _cimg_mp_check_list();
  19620. } else { s0 = ss5; p1 = get_mem_img_index(); is_sth = false; }
  19621. arg1 = s0<se1?compile(s0,se1,depth1,0,bloc_flags):~0U;
  19622. if (arg1!=~0U) {
  19623. _cimg_mp_check_const_scalar(arg1,is_sth?2:1,3);
  19624. arg1 = (unsigned int)mem[arg1];
  19625. } else arg1 = 1024;
  19626. pos = vector(arg1);
  19627. CImg<ulongT>::vector((ulongT)mp_name,pos,p1,arg1).move_to(code);
  19628. return_new_comp = true;
  19629. _cimg_mp_return(pos);
  19630. }
  19631. #endif
  19632. if (!std::strncmp(ss,"narg(",5)) { // Number of arguments
  19633. _cimg_mp_op("Function 'narg()'");
  19634. if (ss5>=se1) _cimg_mp_return(0);
  19635. arg1 = 0;
  19636. for (s = ss5; s<se; ++s) {
  19637. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19638. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19639. ++arg1; s = ns;
  19640. }
  19641. _cimg_mp_const_scalar((double)arg1);
  19642. }
  19643. if ((cimg_sscanf(ss,"norm%u%c",&(arg1=~0U),&sep)==2 && sep=='(') ||
  19644. !std::strncmp(ss,"norminf(",8) || !std::strncmp(ss,"norm(",5) ||
  19645. (!std::strncmp(ss,"norm",4) && ss5<se1 && (s=std::strchr(ss5,'('))!=0)) { // Lp norm
  19646. _cimg_mp_op("Function 'normP()'");
  19647. if (*ss4=='(') { arg1 = 2; s = ss5; }
  19648. else if (*ss4=='i' && *ss5=='n' && *ss6=='f' && *ss7=='(') { arg1 = ~0U; s = ss8; }
  19649. else if (arg1==~0U) {
  19650. arg1 = compile(ss4,s++,depth1,0,bloc_flags);
  19651. _cimg_mp_check_const_scalar(arg1,0,2);
  19652. arg1 = (unsigned int)mem[arg1];
  19653. } else s = std::strchr(ss4,'(') + 1;
  19654. pos = scalar();
  19655. switch (arg1) {
  19656. case 0 :
  19657. CImg<ulongT>::vector((ulongT)mp_norm0,pos,0).move_to(l_opcode); break;
  19658. case 1 :
  19659. CImg<ulongT>::vector((ulongT)mp_norm1,pos,0).move_to(l_opcode); break;
  19660. case 2 :
  19661. CImg<ulongT>::vector((ulongT)mp_norm2,pos,0).move_to(l_opcode); break;
  19662. case ~0U :
  19663. CImg<ulongT>::vector((ulongT)mp_norminf,pos,0).move_to(l_opcode); break;
  19664. default :
  19665. CImg<ulongT>::vector((ulongT)mp_normp,pos,0,(ulongT)(arg1==~0U?-1:(int)arg1)).
  19666. move_to(l_opcode);
  19667. }
  19668. for ( ; s<se; ++s) {
  19669. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19670. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19671. arg2 = compile(s,ns,depth1,0,bloc_flags);
  19672. if (_cimg_mp_is_vector(arg2))
  19673. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  19674. arg2 + (ulongT)_cimg_mp_size(arg2)).
  19675. move_to(l_opcode);
  19676. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  19677. s = ns;
  19678. }
  19679. (l_opcode>'y').move_to(opcode);
  19680. if (arg1>0 && opcode._height==4) // Special case with one argument and p>=1
  19681. _cimg_mp_scalar1(mp_abs,opcode[3]);
  19682. opcode[2] = opcode._height;
  19683. opcode.move_to(code);
  19684. return_new_comp = true;
  19685. _cimg_mp_return(pos);
  19686. }
  19687. break;
  19688. case 'p' :
  19689. if (!std::strncmp(ss,"permut(",7)) { // Number of permutations
  19690. _cimg_mp_op("Function 'permut()'");
  19691. s1 = ss7; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19692. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19693. arg1 = compile(ss7,s1,depth1,0,bloc_flags);
  19694. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  19695. arg3 = compile(++s2,se1,depth1,0,bloc_flags);
  19696. _cimg_mp_check_type(arg1,1,1,0);
  19697. _cimg_mp_check_type(arg2,2,1,0);
  19698. _cimg_mp_check_type(arg3,3,1,0);
  19699. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3))
  19700. _cimg_mp_const_scalar(cimg::permutations((int)mem[arg1],(int)mem[arg2],(bool)mem[arg3]));
  19701. _cimg_mp_scalar3(mp_permutations,arg1,arg2,arg3);
  19702. }
  19703. if (!std::strncmp(ss,"polygon(",8)) { // Polygon/line drawing
  19704. if (!is_inside_critical) is_parallelizable = false;
  19705. _cimg_mp_op("Function 'polygon()'");
  19706. if (*ss8=='#') { // Index specified
  19707. s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19708. p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
  19709. _cimg_mp_check_list();
  19710. } else { p1 = ~0U; s0 = ss8; }
  19711. if (s0==se1) compile(s0,se1,depth1,0,bloc_flags); // 'missing' argument error
  19712. CImg<ulongT>::vector((ulongT)mp_polygon,_cimg_mp_slot_nan,0,p1).move_to(l_opcode);
  19713. for (s = s0; s<se; ++s) {
  19714. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19715. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19716. arg2 = compile(s,ns,depth1,0,bloc_flags);
  19717. if (_cimg_mp_is_vector(arg2))
  19718. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  19719. arg2 + (ulongT)_cimg_mp_size(arg2)).
  19720. move_to(l_opcode);
  19721. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  19722. s = ns;
  19723. }
  19724. (l_opcode>'y').move_to(opcode);
  19725. opcode[2] = opcode._height;
  19726. opcode.move_to(code);
  19727. _cimg_mp_return_nan();
  19728. }
  19729. if (!std::strncmp(ss,"print(",6) ||
  19730. !std::strncmp(ss,"prints(",7)) { // Print expressions
  19731. s0 = ss6 + (*ss5=='('?0:1);
  19732. is_sth = *ss5=='s'; // string must be printed?
  19733. _cimg_mp_op(is_sth?"Function 'prints()'":"Function 'print()'");
  19734. if (!is_sth && *s0=='#') { // Image
  19735. p1 = compile(ss7,se1,depth1,0,bloc_flags);
  19736. _cimg_mp_check_list();
  19737. CImg<ulongT>::vector((ulongT)mp_image_print,_cimg_mp_slot_nan,p1).move_to(code);
  19738. _cimg_mp_return_nan();
  19739. }
  19740. // Regular expression
  19741. for (s = s0; s<se; ++s) {
  19742. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19743. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19744. pos = compile(s,ns,depth1,p_ref,bloc_flags);
  19745. c1 = *ns; *ns = 0;
  19746. variable_name.assign(CImg<charT>::string(s,true,true).unroll('y'),true);
  19747. cimg::strpare(variable_name,false,true);
  19748. if (_cimg_mp_is_const_scalar(pos)) // Const scalar
  19749. std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %g "
  19750. "(compiled as '%s', memslot = %u)",
  19751. variable_name._data,mem[pos],s_type(pos)._data,pos);
  19752. else // Vector or non-const scalar
  19753. std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = (uninitialized) "
  19754. "(compiled as '%s', memslot = %u)",
  19755. variable_name._data,s_type(pos)._data,pos);
  19756. if (_cimg_mp_is_vector(pos)) // Vector
  19757. ((CImg<ulongT>::vector((ulongT)mp_vector_print,pos,0,(ulongT)_cimg_mp_size(pos),is_sth?1:0),
  19758. variable_name)>'y').move_to(opcode);
  19759. else // Scalar
  19760. ((CImg<ulongT>::vector((ulongT)mp_print,pos,0,is_sth?1:0),
  19761. variable_name)>'y').move_to(opcode);
  19762. opcode[2] = opcode._height;
  19763. opcode.move_to(code);
  19764. *ns = c1; s = ns;
  19765. }
  19766. _cimg_mp_return(pos);
  19767. }
  19768. if (!std::strncmp(ss,"pseudoinvert(",13)) { // Matrix/scalar pseudo-inversion
  19769. _cimg_mp_op("Function 'pseudoinvert()'");
  19770. s1 = ss + 13; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19771. arg1 = compile(ss + 13,s1,depth1,0,bloc_flags);
  19772. arg2 = 1;
  19773. arg3 = 0;
  19774. if (s1<se1) {
  19775. s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  19776. arg2 = compile(s1,s2,depth1,0,bloc_flags);
  19777. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  19778. }
  19779. _cimg_mp_check_type(arg1,1,2,0);
  19780. _cimg_mp_check_const_scalar(arg2,2,3);
  19781. _cimg_mp_check_type(arg3,3,1,0);
  19782. p1 = _cimg_mp_size(arg1);
  19783. p2 = (unsigned int)mem[arg2];
  19784. p3 = p1/p2;
  19785. if (p3*p2!=p1) {
  19786. _cimg_mp_strerr;
  19787. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19788. "CImg<%s>::%s: %s: Type of first argument ('%s') "
  19789. "does not match with second argument 'nb_colsA=%u', "
  19790. "in expression '%s'.",
  19791. pixel_type(),_cimg_mp_calling_function,s_op,
  19792. s_type(arg1)._data,p2,s0);
  19793. }
  19794. pos = vector(p1);
  19795. CImg<ulongT>::vector((ulongT)mp_matrix_pseudoinvert,pos,arg1,p2,p3,arg3).move_to(code);
  19796. return_new_comp = true;
  19797. _cimg_mp_return(pos);
  19798. }
  19799. break;
  19800. case 'r' :
  19801. if (!std::strncmp(ss,"rad2deg(",8)) { // Degrees to radians
  19802. _cimg_mp_op("Function 'rad2deg()'");
  19803. arg1 = compile(ss8,se1,depth1,0,bloc_flags);
  19804. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_rad2deg,arg1);
  19805. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*180/cimg::PI);
  19806. _cimg_mp_scalar1(mp_rad2deg,arg1);
  19807. }
  19808. if (!std::strncmp(ss,"ref(",4)) { // Variable declaration
  19809. _cimg_mp_op("Function 'ref()'");
  19810. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19811. if (s1>=se1 || !*s1) compile(s1,s1,depth1,0,bloc_flags); // Will throw missing argument error
  19812. arg3 = compile(ss4,s1++,depth1,p_ref,bloc_flags);
  19813. *se1 = 0;
  19814. if (!is_varname(s1)) { // Invalid variable name
  19815. variable_name.assign(s1,(unsigned int)(se1 + 1 - s1)).back() = 0;
  19816. cimg::strellipsize(variable_name,64);
  19817. *se1 = ')';
  19818. _cimg_mp_strerr;
  19819. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19820. "CImg<%s>::%s: %s: Invalid specified variable name '%s', "
  19821. "in expression '%s'.",
  19822. pixel_type(),_cimg_mp_calling_function,s_op,
  19823. variable_name._data,s0);
  19824. }
  19825. get_variable_pos(s1,arg1,arg2);
  19826. if (arg2!=~0U) reserved_label[arg2] = arg3;
  19827. else if (arg1!=~0U) variable_pos[arg1] = arg3;
  19828. else { // New variable
  19829. if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
  19830. variable_pos[variable_def._width] = arg3;
  19831. CImg<char>::string(s1).move_to(variable_def);
  19832. }
  19833. if (_cimg_mp_is_vector(arg3))
  19834. set_reserved_vector(arg3); // Prevent from being used in further optimization
  19835. else if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
  19836. *se1 = ')';
  19837. _cimg_mp_return(arg3);
  19838. }
  19839. if (!std::strncmp(ss,"repeat(",7)) { // Repeat
  19840. _cimg_mp_op("Function 'repeat()'");
  19841. s0 = ss7; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19842. arg1 = compile(ss7,s0,depth1,0,bloc_flags); // Number of iterations
  19843. _cimg_mp_check_type(arg1,1,1,0);
  19844. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  19845. p1 = code._width;
  19846. if (s1<se1) { // Version with 3 arguments
  19847. variable_name.assign(s0,(unsigned int)(s1 + 1 - s0)).back() = 0;
  19848. cimg::strpare(variable_name,false,true);
  19849. if (!is_varname(variable_name)) { // Invalid variable name
  19850. cimg::strellipsize(variable_name,64);
  19851. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19852. "CImg<%s>::%s: %s: Invalid loop variable name '%s', "
  19853. "in expression '%s'.",
  19854. pixel_type(),_cimg_mp_calling_function,s_op,
  19855. variable_name._data,s0);
  19856. }
  19857. get_variable_pos(variable_name,arg2,arg3);
  19858. arg2 = arg3!=~0U?reserved_label[arg3]:arg2!=~0U?variable_pos[arg2]:~0U; // Variable slot
  19859. if (arg2!=~0U && (!_cimg_mp_is_scalar(arg2) ||
  19860. _cimg_mp_is_const_scalar(arg2))) { // Variable is not a vector or is a constant->error
  19861. cimg::strellipsize(variable_name,64);
  19862. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19863. "CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' "
  19864. "(expected 'scalar'), in expression '%s'.",
  19865. pixel_type(),_cimg_mp_calling_function,s_op,
  19866. s_type(arg2)._data,variable_name._data,s0);
  19867. } else if (arg2==~0U) { // Variable does not exist -> create it
  19868. arg2 = scalar();
  19869. if (arg3!=~0U) reserved_label[arg3] = arg2;
  19870. else {
  19871. if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
  19872. variable_pos[variable_def._width] = arg2;
  19873. variable_name.move_to(variable_def);
  19874. }
  19875. memtype[arg2] = -1;
  19876. }
  19877. arg3 = compile(++s1,se1,depth1,0,bloc_flags);
  19878. } else { // Version with 2 arguments
  19879. arg2 = ~0U;
  19880. arg3 = compile(s0,se1,depth1,0,bloc_flags);
  19881. }
  19882. // arg2 = variable slot, arg3 = fill expression.
  19883. CImg<ulongT>::vector((ulongT)mp_repeat,arg3,arg1,arg2,code._width - p1).move_to(code,p1);
  19884. _cimg_mp_return(arg3);
  19885. }
  19886. if (!std::strncmp(ss,"resize(",7)) { // Vector or image resize
  19887. _cimg_mp_op("Function 'resize()'");
  19888. if (*ss7!='#') { // Vector
  19889. for (s = ss7; s<se; ++s) {
  19890. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19891. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19892. arg2 = compile(s,ns,depth1,0,bloc_flags);
  19893. if (s!=ss7 && _cimg_mp_is_vector(arg2))
  19894. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  19895. arg2 + (ulongT)_cimg_mp_size(arg2)).
  19896. move_to(l_opcode);
  19897. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  19898. s = ns;
  19899. }
  19900. (l_opcode>'y').move_to(opcode);
  19901. if (opcode.height()<2) compile(s,se1,depth1,0,bloc_flags); // Not enough arguments -> throw exception
  19902. arg1 = (unsigned int)opcode[0]; // Vector to resize
  19903. p1 = _cimg_mp_size(arg1);
  19904. if (opcode.height()<=4) { // Simple vector resize
  19905. arg2 = (unsigned int)opcode[1];
  19906. _cimg_mp_check_const_scalar(arg2,2,3);
  19907. arg2 = (unsigned int)mem[arg2];
  19908. arg3 = opcode.height()<3?1U:(unsigned int)opcode[2];
  19909. _cimg_mp_check_type(arg3,3,1,0);
  19910. arg4 = opcode.height()<4?0U:(unsigned int)opcode[3];
  19911. _cimg_mp_check_type(arg4,4,1,0);
  19912. pos = vector(arg2);
  19913. CImg<ulongT>::vector((ulongT)mp_vector_resize,pos,arg2,arg1,p1,arg3,arg4).move_to(code);
  19914. } else { // Advanced vector resize (vector viewed as an image)
  19915. // opcode = [ A, ow,oh,od,os, nw,nh,nd,ns, interp, boundary_cond, ax,ay,az,ac ]
  19916. // [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ]
  19917. if (opcode.height()<6) compile(s,se1,depth1,0,bloc_flags); // Not enough arguments -> throw exception
  19918. p2 = opcode.height();
  19919. opcode.resize(1,15,1,1,0);
  19920. if (p2<7) opcode[6] = opcode[2];
  19921. if (p2<8) opcode[7] = opcode[3];
  19922. if (p2<9) opcode[8] = opcode[4];
  19923. if (p2<10) opcode[9] = 1;
  19924. _cimg_mp_check_const_scalar(opcode[1],2,3);
  19925. _cimg_mp_check_const_scalar(opcode[2],3,3);
  19926. _cimg_mp_check_const_scalar(opcode[3],4,3);
  19927. _cimg_mp_check_const_scalar(opcode[4],5,3);
  19928. _cimg_mp_check_const_scalar(opcode[5],6,3);
  19929. _cimg_mp_check_const_scalar(opcode[6],7,3);
  19930. _cimg_mp_check_const_scalar(opcode[7],8,3);
  19931. _cimg_mp_check_const_scalar(opcode[8],9,3);
  19932. arg2 = (unsigned int)mem[opcode[1]]; opcode[1] = arg2;
  19933. arg3 = (unsigned int)mem[opcode[2]]; opcode[2] = arg3;
  19934. arg4 = (unsigned int)mem[opcode[3]]; opcode[3] = arg4;
  19935. arg5 = (unsigned int)mem[opcode[4]]; opcode[4] = arg5;
  19936. if (arg2*arg3*arg4*arg5!=std::max(1U,p1))
  19937. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19938. "CImg<%s>::%s: %s: Input size (%lu values) and specified input vector "
  19939. "geometry (%u,%u,%u,%u) (%lu values) do not match.",
  19940. pixel_type(),_cimg_mp_calling_function,s_op,
  19941. std::max(p1,1U),arg2,arg3,arg4,arg5,(ulongT)arg2*arg3*arg4*arg5);
  19942. arg2 = (unsigned int)mem[opcode[5]]; opcode[5] = arg2;
  19943. arg3 = (unsigned int)mem[opcode[6]]; opcode[6] = arg3;
  19944. arg4 = (unsigned int)mem[opcode[7]]; opcode[7] = arg4;
  19945. arg5 = (unsigned int)mem[opcode[8]]; opcode[8] = arg5;
  19946. pos = vector(arg2*arg3*arg4*arg5);
  19947. opcode.resize(1,18,1,1,0,0,0,1);
  19948. opcode[0] = (ulongT)mp_vector_resize_ext;
  19949. opcode[1] = (ulongT)pos;
  19950. opcode[2] = (ulongT)p1;
  19951. opcode.move_to(code);
  19952. }
  19953. return_new_comp = true;
  19954. _cimg_mp_return(pos);
  19955. } else { // Image
  19956. if (!is_inside_critical) is_parallelizable = false;
  19957. s0 = ss8; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  19958. p1 = compile(ss8,s0++,depth1,0,bloc_flags);
  19959. _cimg_mp_check_list();
  19960. l_opcode.assign(); // Don't use 'opcode': it can be modified by further calls to 'compile()'!
  19961. CImg<ulongT>::vector((ulongT)mp_image_resize,_cimg_mp_slot_nan,p1,~0U,~0U,~0U,~0U,1,0,0,0,0,0).
  19962. move_to(l_opcode);
  19963. pos = 0;
  19964. for (s = s0; s<se && pos<10; ++s) {
  19965. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  19966. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  19967. arg1 = compile(s,ns,depth1,0,bloc_flags);
  19968. _cimg_mp_check_type(arg1,pos + 2,1,0);
  19969. l_opcode(0,pos + 3) = arg1;
  19970. s = ns;
  19971. ++pos;
  19972. }
  19973. if (pos<1 || pos>10) {
  19974. _cimg_mp_strerr;
  19975. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  19976. "CImg<%s>::%s: %s: %s arguments, in expression '%s'.",
  19977. pixel_type(),_cimg_mp_calling_function,s_op,
  19978. pos<1?"Missing":"Too much",s0);
  19979. }
  19980. l_opcode[0].move_to(code);
  19981. _cimg_mp_return_nan();
  19982. }
  19983. }
  19984. if (!std::strncmp(ss,"reverse(",8)) { // Vector reverse
  19985. _cimg_mp_op("Function 'reverse()'");
  19986. arg1 = compile(ss8,se1,depth1,0,bloc_flags);
  19987. if (!_cimg_mp_is_vector(arg1)) _cimg_mp_return(arg1);
  19988. p1 = _cimg_mp_size(arg1);
  19989. pos = vector(p1);
  19990. CImg<ulongT>::vector((ulongT)mp_vector_reverse,pos,arg1,p1).move_to(code);
  19991. return_new_comp = true;
  19992. _cimg_mp_return(pos);
  19993. }
  19994. if (!std::strncmp(ss,"rol(",4) || !std::strncmp(ss,"ror(",4)) { // Bitwise rotation
  19995. _cimg_mp_op(ss[2]=='l'?"Function 'rol()'":"Function 'ror()'");
  19996. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1-expr._data]!=clevel1)) ++s1;
  19997. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  19998. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
  19999. _cimg_mp_check_type(arg2,2,1,0);
  20000. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector2_vs(*ss2=='l'?mp_rol:mp_ror,arg1,arg2);
  20001. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  20002. _cimg_mp_const_scalar(*ss2=='l'?cimg::rol(mem[arg1],(unsigned int)mem[arg2]):
  20003. cimg::ror(mem[arg1],(unsigned int)mem[arg2]));
  20004. _cimg_mp_scalar2(*ss2=='l'?mp_rol:mp_ror,arg1,arg2);
  20005. }
  20006. if (!std::strncmp(ss,"rot(",4)) { // 2D/3D rotation matrix
  20007. _cimg_mp_op("Function 'rot()'");
  20008. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20009. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  20010. if (s1<se1) { // 3D rotation
  20011. _cimg_mp_check_type(arg1,1,3,3);
  20012. is_sth = false; // Is coordinates as vector?
  20013. if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
  20014. is_sth = true;
  20015. p2 = _cimg_mp_size(arg1);
  20016. ++arg1;
  20017. arg2 = arg3 = 0;
  20018. if (p2>1) {
  20019. arg2 = arg1 + 1;
  20020. if (p2>2) arg3 = arg2 + 1;
  20021. }
  20022. arg4 = compile(++s1,se1,depth1,0,bloc_flags);
  20023. } else {
  20024. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20025. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20026. s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  20027. arg3 = compile(++s2,s3,depth1,0,bloc_flags);
  20028. arg4 = compile(++s3,se1,depth1,0,bloc_flags);
  20029. _cimg_mp_check_type(arg2,2,1,0);
  20030. _cimg_mp_check_type(arg3,3,1,0);
  20031. }
  20032. _cimg_mp_check_type(arg4,is_sth?2:4,1,0);
  20033. pos = vector(9);
  20034. CImg<ulongT>::vector((ulongT)mp_rot3d,pos,arg1,arg2,arg3,arg4).move_to(code);
  20035. } else { // 2D rotation
  20036. _cimg_mp_check_type(arg1,1,1,0);
  20037. pos = vector(4);
  20038. CImg<ulongT>::vector((ulongT)mp_rot2d,pos,arg1).move_to(code);
  20039. }
  20040. return_new_comp = true;
  20041. _cimg_mp_return(pos);
  20042. }
  20043. if (!std::strncmp(ss,"round(",6)) { // Value rounding
  20044. _cimg_mp_op("Function 'round()'");
  20045. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20046. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  20047. arg2 = 1;
  20048. arg3 = 0;
  20049. if (s1<se1) {
  20050. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20051. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20052. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  20053. }
  20054. _cimg_mp_check_type(arg2,2,1,0);
  20055. _cimg_mp_check_type(arg3,3,1,0);
  20056. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_round,arg1,arg2,arg3);
  20057. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3))
  20058. _cimg_mp_const_scalar(cimg::round(mem[arg1],mem[arg2],(int)mem[arg3]));
  20059. _cimg_mp_scalar3(mp_round,arg1,arg2,arg3);
  20060. }
  20061. #ifdef cimg_mp_func_run
  20062. if (!std::strncmp(ss,"run(",4)) { // Run external command
  20063. _cimg_mp_op("Function 'run()'");
  20064. if (!is_inside_critical) is_parallelizable = false;
  20065. CImg<ulongT>::vector((ulongT)mp_run,0,0).move_to(l_opcode);
  20066. pos = 1;
  20067. for (s = ss4; s<se; ++s) {
  20068. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20069. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20070. arg1 = compile(s,ns,depth1,0,bloc_flags);
  20071. CImg<ulongT>::vector(arg1,_cimg_mp_size(arg1)).move_to(l_opcode);
  20072. s = ns;
  20073. }
  20074. (l_opcode>'y').move_to(opcode);
  20075. pos = scalar();
  20076. opcode[1] = pos;
  20077. opcode[2] = opcode._height;
  20078. opcode.move_to(code);
  20079. return_new_comp = true;
  20080. _cimg_mp_return(pos);
  20081. }
  20082. #endif
  20083. break;
  20084. case 's' :
  20085. if (*ss1=='(') { // Image spectrum
  20086. _cimg_mp_op("Function 's()'");
  20087. if (*ss2=='#') { // Index specified
  20088. p1 = compile(ss3,se1,depth1,0,bloc_flags);
  20089. _cimg_mp_check_list();
  20090. } else { if (ss2!=se1) break; p1 = ~0U; }
  20091. _cimg_mp_scalar1(mp_image_s,p1);
  20092. }
  20093. if (!std::strncmp(ss,"same(",5)) { // Test if operands have the same values
  20094. _cimg_mp_op("Function 'same()'");
  20095. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20096. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  20097. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20098. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20099. arg3 = 11;
  20100. arg4 = 1;
  20101. if (s2<se1) {
  20102. s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
  20103. arg3 = compile(++s2,s3,depth1,0,bloc_flags);
  20104. _cimg_mp_check_type(arg3,3,1,0);
  20105. arg4 = s3<se1?compile(++s3,se1,depth1,0,bloc_flags):1;
  20106. }
  20107. p1 = _cimg_mp_size(arg1);
  20108. p2 = _cimg_mp_size(arg2);
  20109. _cimg_mp_scalar6(mp_vector_eq,arg1,p1,arg2,p2,arg3,arg4);
  20110. }
  20111. #ifdef cimg_mp_func_set
  20112. if (!std::strncmp(ss,"set(",4)) { // Set value/vector to external variable
  20113. _cimg_mp_op("Function 'set()'");
  20114. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20115. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  20116. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  20117. _cimg_mp_check_type(arg2,2,2,0);
  20118. p1 = _cimg_mp_size(arg1);
  20119. p2 = _cimg_mp_size(arg2);
  20120. CImg<ulongT>::vector((ulongT)mp_set,arg1,p1,arg2,p2).move_to(code);
  20121. _cimg_mp_return(arg1);
  20122. }
  20123. #endif
  20124. if (!std::strncmp(ss,"shift(",6)) { // Shift vector
  20125. _cimg_mp_op("Function 'shift()'");
  20126. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20127. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  20128. arg2 = 1; arg3 = 0;
  20129. if (s1<se1) {
  20130. s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  20131. arg2 = compile(s1,s0,depth1,0,bloc_flags);
  20132. arg3 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):0;
  20133. }
  20134. _cimg_mp_check_type(arg1,1,2,0);
  20135. _cimg_mp_check_type(arg2,2,1,0);
  20136. _cimg_mp_check_type(arg3,3,1,0);
  20137. p1 = _cimg_mp_size(arg1);
  20138. pos = vector(p1);
  20139. CImg<ulongT>::vector((ulongT)mp_shift,pos,arg1,p1,arg2,arg3).move_to(code);
  20140. return_new_comp = true;
  20141. _cimg_mp_return(pos);
  20142. }
  20143. if (!std::strncmp(ss,"sign(",5)) { // Sign
  20144. _cimg_mp_op("Function 'sign()'");
  20145. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20146. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sign,arg1);
  20147. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sign(mem[arg1]));
  20148. _cimg_mp_scalar1(mp_sign,arg1);
  20149. }
  20150. if (!std::strncmp(ss,"sin(",4)) { // Sine
  20151. _cimg_mp_op("Function 'sin()'");
  20152. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  20153. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sin,arg1);
  20154. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sin(mem[arg1]));
  20155. _cimg_mp_scalar1(mp_sin,arg1);
  20156. }
  20157. if (!std::strncmp(ss,"sinc(",5)) { // Sine cardinal
  20158. _cimg_mp_op("Function 'sinc()'");
  20159. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20160. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sinc,arg1);
  20161. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sinc(mem[arg1]));
  20162. _cimg_mp_scalar1(mp_sinc,arg1);
  20163. }
  20164. if (!std::strncmp(ss,"sinh(",5)) { // Hyperbolic sine
  20165. _cimg_mp_op("Function 'sinh()'");
  20166. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20167. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sinh,arg1);
  20168. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sinh(mem[arg1]));
  20169. _cimg_mp_scalar1(mp_sinh,arg1);
  20170. }
  20171. if (!std::strncmp(ss,"size(",5)) { // Vector size
  20172. _cimg_mp_op("Function 'size()'");
  20173. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20174. _cimg_mp_const_scalar(_cimg_mp_is_scalar(arg1)?0:_cimg_mp_size(arg1));
  20175. }
  20176. if (!std::strncmp(ss,"solve(",6)) { // Solve square linear system
  20177. _cimg_mp_op("Function 'solve()'");
  20178. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20179. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  20180. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20181. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20182. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
  20183. _cimg_mp_check_type(arg1,1,2,0);
  20184. _cimg_mp_check_type(arg2,2,2,0);
  20185. _cimg_mp_check_const_scalar(arg3,3,3);
  20186. p1 = _cimg_mp_size(arg1);
  20187. p2 = _cimg_mp_size(arg2);
  20188. p3 = (unsigned int)mem[arg3];
  20189. arg5 = p2/p3;
  20190. arg4 = p1/arg5;
  20191. if (arg4*arg5!=p1 || arg5*p3!=p2) {
  20192. _cimg_mp_strerr;
  20193. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20194. "CImg<%s>::%s: %s: Types of first and second arguments ('%s' and '%s') "
  20195. "do not match with third argument 'nb_colsB=%u', "
  20196. "in expression '%s'.",
  20197. pixel_type(),_cimg_mp_calling_function,s_op,
  20198. s_type(arg1)._data,s_type(arg2)._data,p3,s0);
  20199. }
  20200. pos = vector(arg4*p3);
  20201. CImg<ulongT>::vector((ulongT)mp_solve,pos,arg1,arg2,arg4,arg5,p3).move_to(code);
  20202. return_new_comp = true;
  20203. _cimg_mp_return(pos);
  20204. }
  20205. if (!std::strncmp(ss,"sort(",5)) { // Sort vector
  20206. _cimg_mp_op("Function 'sort()'");
  20207. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20208. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  20209. arg2 = arg4 = 1; arg3 = ~0U;
  20210. if (s1<se1) {
  20211. s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  20212. arg2 = compile(s1,s0,depth1,0,bloc_flags);
  20213. if (s0<se1) {
  20214. s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20215. arg3 = compile(s0,s1,depth1,0,bloc_flags);
  20216. arg4 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
  20217. }
  20218. }
  20219. _cimg_mp_check_type(arg1,1,2,0);
  20220. _cimg_mp_check_type(arg2,2,1,0);
  20221. if (arg3!=~0U) _cimg_mp_check_type(arg3,3,1,0);
  20222. _cimg_mp_check_type(arg4,4,1,0);
  20223. p1 = _cimg_mp_size(arg1);
  20224. pos = vector(p1);
  20225. CImg<ulongT>::vector((ulongT)mp_sort,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
  20226. return_new_comp = true;
  20227. _cimg_mp_return(pos);
  20228. }
  20229. if (!std::strncmp(ss,"sqr(",4)) { // Square
  20230. _cimg_mp_op("Function 'sqr()'");
  20231. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  20232. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sqr,arg1);
  20233. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sqr(mem[arg1]));
  20234. _cimg_mp_scalar1(mp_sqr,arg1);
  20235. }
  20236. if (!std::strncmp(ss,"sqrt(",5)) { // Square root
  20237. _cimg_mp_op("Function 'sqrt()'");
  20238. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20239. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sqrt,arg1);
  20240. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sqrt(mem[arg1]));
  20241. _cimg_mp_scalar1(mp_sqrt,arg1);
  20242. }
  20243. if (!std::strncmp(ss,"srand(",6)) { // Set RNG seed
  20244. _cimg_mp_op("Function 'srand()'");
  20245. arg1 = ss6<se1?compile(ss6,se1,depth1,0,bloc_flags):~0U;
  20246. if (arg1!=~0U) { _cimg_mp_check_type(arg1,1,1,0); _cimg_mp_scalar1(mp_srand,arg1); }
  20247. _cimg_mp_scalar0(mp_srand0);
  20248. }
  20249. if (!std::strncmp(ss,"stats(",6)) { // Image statistics
  20250. _cimg_mp_op("Function 'stats()'");
  20251. if (*ss6=='#') { // Index specified
  20252. p1 = compile(ss7,se1,depth1,0,bloc_flags);
  20253. _cimg_mp_check_list();
  20254. } else { if (ss6!=se1) break; p1 = ~0U; }
  20255. pos = vector(14);
  20256. CImg<ulongT>::vector((ulongT)mp_image_stats,pos,p1).move_to(code);
  20257. return_new_comp = true;
  20258. _cimg_mp_return(pos);
  20259. }
  20260. #ifdef cimg_mp_func_store
  20261. if (!std::strncmp(ss,"store(",6)) { // Store vector to variable
  20262. _cimg_mp_op("Function 'store()'");
  20263. s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20264. arg1 = compile(ss6,s1,depth1,0,bloc_flags);
  20265. p1 = _cimg_mp_size(arg1);
  20266. p3 = std::max(1U,p1);
  20267. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20268. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20269. _cimg_mp_check_type(arg2,2,2,0);
  20270. p2 = _cimg_mp_size(arg2);
  20271. arg3 = ~0U; arg4 = arg5 = arg6 = 1U; pos = 0;
  20272. if (s2<se1) {
  20273. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20274. arg3 = compile(++s2,s1,depth1,0,bloc_flags);
  20275. _cimg_mp_check_type(arg3,3,1,0);
  20276. arg4 = arg5 = arg6 = 1U;
  20277. if (s1<se1) {
  20278. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20279. arg4 = compile(++s1,s2,depth1,0,bloc_flags);
  20280. _cimg_mp_check_type(arg4,4,1,0);
  20281. if (s2<se1) {
  20282. s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20283. arg5 = compile(++s2,s1,depth1,0,bloc_flags);
  20284. _cimg_mp_check_type(arg5,5,1,0);
  20285. if (s1<se1) {
  20286. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20287. arg6 = compile(++s1,s2,depth1,0,bloc_flags);
  20288. pos = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  20289. _cimg_mp_check_type(arg6,6,1,0);
  20290. _cimg_mp_check_type(pos,7,1,0);
  20291. }
  20292. }
  20293. }
  20294. }
  20295. if (arg3==~0U) arg3 = const_scalar(p3);
  20296. CImg<ulongT>::vector((ulongT)mp_store,_cimg_mp_slot_nan,arg1,p1,arg2,p2,
  20297. arg3,arg4,arg5,arg6,pos).move_to(code);
  20298. _cimg_mp_return_nan();
  20299. }
  20300. #endif
  20301. if (!std::strncmp(ss,"stov(",5)) { // String to double
  20302. _cimg_mp_op("Function 'stov()'");
  20303. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20304. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  20305. arg2 = arg3 = 0;
  20306. if (s1<se1) {
  20307. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20308. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20309. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
  20310. }
  20311. _cimg_mp_check_type(arg2,2,1,0);
  20312. _cimg_mp_check_type(arg3,3,1,0);
  20313. p1 = _cimg_mp_size(arg1);
  20314. pos = scalar();
  20315. CImg<ulongT>::vector((ulongT)mp_stov,pos,arg1,p1,arg2,arg3).move_to(code);
  20316. return_new_comp = true;
  20317. _cimg_mp_return(pos);
  20318. }
  20319. if (!std::strncmp(ss,"string(",7)) { // Construct string from list of arguments
  20320. _cimg_mp_op("Function 'string()'");
  20321. CImg<ulongT>::vector((ulongT)mp_string,0,0,0).move_to(l_opcode);
  20322. if (*ss7=='#') { // Output vector size specified, with '#'
  20323. s0 = ss8; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  20324. arg1 = compile(ss8,s0++,depth1,0,bloc_flags);
  20325. _cimg_mp_check_const_scalar(arg1,1,3);
  20326. arg1 = (unsigned int)mem[arg1];
  20327. s = s0;
  20328. } else { arg1=~0U; s = ss7; }
  20329. p1 = 0;
  20330. for (; s<se; ++s) {
  20331. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20332. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20333. arg2 = compile(s,ns,depth1,0,bloc_flags);
  20334. p2 = _cimg_mp_size(arg2);
  20335. if (p2) p1+=p2;
  20336. else {
  20337. if (_cimg_mp_is_const_scalar(arg2)) p1+=cimg_snprintf(variable_name.assign(24),24,"%.17g",mem[arg2]);
  20338. else p1+=23;
  20339. }
  20340. CImg<ulongT>::vector(arg2,p2).move_to(l_opcode);
  20341. s = ns;
  20342. }
  20343. if (arg1==~0U) arg1 = p1;
  20344. pos = vector(arg1,0);
  20345. (l_opcode>'y').move_to(opcode);
  20346. opcode[1] = pos;
  20347. opcode[2] = arg1;
  20348. opcode[3] = opcode._height;
  20349. opcode.move_to(code);
  20350. return_new_comp = true;
  20351. _cimg_mp_return(pos);
  20352. }
  20353. if (!std::strncmp(ss,"svd(",4)) { // Matrix SVD
  20354. _cimg_mp_op("Function 'svd()'");
  20355. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20356. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  20357. arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
  20358. _cimg_mp_check_type(arg1,1,2,0);
  20359. _cimg_mp_check_const_scalar(arg2,2,3);
  20360. p1 = _cimg_mp_size(arg1);
  20361. p2 = (unsigned int)mem[arg2];
  20362. p3 = p1/p2;
  20363. if (p3*p2!=p1) {
  20364. _cimg_mp_strerr;
  20365. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20366. "CImg<%s>::%s: %s: Type of first argument ('%s') "
  20367. "does not match with second argument 'nb_colsA=%u', "
  20368. "in expression '%s'.",
  20369. pixel_type(),_cimg_mp_calling_function,s_op,
  20370. s_type(arg1)._data,p2,s0);
  20371. }
  20372. pos = vector(p1 + p2 + p2*p2);
  20373. CImg<ulongT>::vector((ulongT)mp_matrix_svd,pos,arg1,p2,p3).move_to(code);
  20374. return_new_comp = true;
  20375. _cimg_mp_return(pos);
  20376. }
  20377. if (!std::strncmp(ss,"swap(",5)) { // Swap values
  20378. _cimg_mp_op("Function 'swap()'");
  20379. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20380. ref.assign(14);
  20381. arg1 = compile(ss5,s1,depth1,ref,bloc_flags);
  20382. arg2 = compile(++s1,se1,depth1,ref._data + 7,bloc_flags);
  20383. p1 = _cimg_mp_size(arg1);
  20384. _cimg_mp_check_type(arg2,2,p1?2:1,p1);
  20385. if (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_const_scalar(arg2)) {
  20386. _cimg_mp_strerr;
  20387. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20388. "CImg<%s>::%s: %s: %s argument cannot be a constant, "
  20389. "in expression '%s'.",
  20390. pixel_type(),_cimg_mp_calling_function,s_op,
  20391. _cimg_mp_is_const_scalar(arg1)?"First":"Second",s0);
  20392. }
  20393. CImg<ulongT>::vector((ulongT)mp_swap,arg1,arg2,p1).move_to(code);
  20394. // Write back values of linked arg1 and arg2.
  20395. const unsigned int *_ref = ref;
  20396. is_sth = true; // Is first argument?
  20397. do {
  20398. switch (*_ref) {
  20399. case 1 : // arg1: V[k]
  20400. arg3 = _ref[1]; // Vector slot
  20401. arg4 = _ref[2]; // Index
  20402. CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
  20403. move_to(code);
  20404. break;
  20405. case 2 : // arg1: i/j[_#ind,off]
  20406. if (!is_inside_critical) is_parallelizable = false;
  20407. p1 = _ref[1]; // Index
  20408. is_relative = (bool)_ref[2];
  20409. arg3 = _ref[3]; // Offset
  20410. if (p1!=~0U) {
  20411. if (imglist)
  20412. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
  20413. arg1,p1,arg3).move_to(code);
  20414. } else {
  20415. if (imgout)
  20416. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
  20417. arg1,arg3).move_to(code);
  20418. }
  20419. break;
  20420. case 3 : // arg1: i/j(_#ind,_x,_y,_z,_c)
  20421. if (!is_inside_critical) is_parallelizable = false;
  20422. p1 = _ref[1]; // Index
  20423. is_relative = (bool)_ref[2];
  20424. arg3 = _ref[3]; // X
  20425. arg4 = _ref[4]; // Y
  20426. arg5 = _ref[5]; // Z
  20427. arg6 = _ref[6]; // C
  20428. if (p1!=~0U) {
  20429. if (imglist)
  20430. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
  20431. arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
  20432. } else {
  20433. if (imgout)
  20434. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
  20435. arg1,arg3,arg4,arg5,arg6).move_to(code);
  20436. }
  20437. break;
  20438. case 4: // arg1: I/J[_#ind,off]
  20439. if (!is_inside_critical) is_parallelizable = false;
  20440. p1 = _ref[1]; // Index
  20441. is_relative = (bool)_ref[2];
  20442. arg3 = _ref[3]; // Offset
  20443. if (p1!=~0U) {
  20444. if (imglist) {
  20445. if (_cimg_mp_is_scalar(arg1))
  20446. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
  20447. arg1,p1,arg3).move_to(code);
  20448. else {
  20449. _cimg_mp_check_const_index(p1);
  20450. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
  20451. arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
  20452. }
  20453. }
  20454. } else {
  20455. if (imgout) {
  20456. if (_cimg_mp_is_scalar(arg1))
  20457. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
  20458. arg1,arg3).move_to(code);
  20459. else
  20460. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
  20461. arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
  20462. }
  20463. }
  20464. break;
  20465. case 5 : // arg1: I/J(_#ind,_x,_y,_z,_c)
  20466. if (!is_inside_critical) is_parallelizable = false;
  20467. p1 = _ref[1]; // Index
  20468. is_relative = (bool)_ref[2];
  20469. arg3 = _ref[3]; // X
  20470. arg4 = _ref[4]; // Y
  20471. arg5 = _ref[5]; // Z
  20472. if (p1!=~0U) {
  20473. if (imglist) {
  20474. if (_cimg_mp_is_scalar(arg1))
  20475. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
  20476. arg1,p1,arg3,arg4,arg5).move_to(code);
  20477. else {
  20478. _cimg_mp_check_const_index(p1);
  20479. CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
  20480. arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  20481. }
  20482. }
  20483. } else {
  20484. if (imgout) {
  20485. if (_cimg_mp_is_scalar(arg1))
  20486. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
  20487. arg1,arg3,arg4,arg5).move_to(code);
  20488. else
  20489. CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
  20490. arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
  20491. }
  20492. }
  20493. break;
  20494. }
  20495. _ref+=7;
  20496. arg1 = arg2;
  20497. is_sth = !is_sth;
  20498. } while (!is_sth);
  20499. if (p_ref) std::memcpy(p_ref,ref,siz_ref);
  20500. _cimg_mp_return(arg1);
  20501. }
  20502. break;
  20503. case 't' :
  20504. if (!std::strncmp(ss,"tan(",4)) { // Tangent
  20505. _cimg_mp_op("Function 'tan()'");
  20506. arg1 = compile(ss4,se1,depth1,0,bloc_flags);
  20507. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_tan,arg1);
  20508. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::tan(mem[arg1]));
  20509. _cimg_mp_scalar1(mp_tan,arg1);
  20510. }
  20511. if (!std::strncmp(ss,"tanh(",5)) { // Hyperbolic tangent
  20512. _cimg_mp_op("Function 'tanh()'");
  20513. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20514. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_tanh,arg1);
  20515. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::tanh(mem[arg1]));
  20516. _cimg_mp_scalar1(mp_tanh,arg1);
  20517. }
  20518. if (!std::strncmp(ss,"trace(",6)) { // Matrix trace
  20519. _cimg_mp_op("Function 'trace()'");
  20520. arg1 = compile(ss6,se1,depth1,0,bloc_flags);
  20521. _cimg_mp_check_matrix_square(arg1,1);
  20522. p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
  20523. _cimg_mp_scalar2(mp_trace,arg1,p1);
  20524. }
  20525. if (!std::strncmp(ss,"transpose(",10)) { // Matrix transpose
  20526. _cimg_mp_op("Function 'transpose()'");
  20527. s1 = ss + 10; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20528. arg1 = compile(ss + 10,s1,depth1,0,bloc_flags);
  20529. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  20530. _cimg_mp_check_type(arg1,1,2,0);
  20531. _cimg_mp_check_const_scalar(arg2,2,3);
  20532. p1 = _cimg_mp_size(arg1);
  20533. p2 = (unsigned int)mem[arg2];
  20534. p3 = p1/p2;
  20535. if (p2*p3!=p1) {
  20536. _cimg_mp_strerr;
  20537. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20538. "CImg<%s>::%s: %s: Size of first argument ('%s') does not match "
  20539. "second argument 'nb_cols=%u', in expression '%s'.",
  20540. pixel_type(),_cimg_mp_calling_function,s_op,
  20541. s_type(arg1)._data,p2,s0);
  20542. }
  20543. pos = vector(p3*p2);
  20544. CImg<ulongT>::vector((ulongT)mp_transpose,pos,arg1,p2,p3).move_to(code);
  20545. return_new_comp = true;
  20546. _cimg_mp_return(pos);
  20547. }
  20548. break;
  20549. case 'u' :
  20550. if (*ss1=='(') { // Random value with uniform distribution
  20551. _cimg_mp_op("Function 'u()'");
  20552. if (*ss2==')') _cimg_mp_scalar2(mp_u,0,1);
  20553. s1 = ss2; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20554. arg1 = compile(ss2,s1,depth1,0,bloc_flags);
  20555. if (s1<se1) arg2 = compile(++s1,se1,depth1,0,bloc_flags); else { arg2 = arg1; arg1 = 0; }
  20556. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  20557. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_u,arg1,arg2);
  20558. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_u,arg1,arg2);
  20559. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_u,arg1,arg2);
  20560. _cimg_mp_scalar2(mp_u,arg1,arg2);
  20561. }
  20562. if (!std::strncmp(ss,"ui2f(",5)) { // Special uint->float conversion
  20563. _cimg_mp_op("Function 'ui2f()'");
  20564. arg1 = compile(ss5,se1,depth1,0,bloc_flags);
  20565. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_ui2f,arg1);
  20566. if (_cimg_mp_is_const_scalar(arg1))
  20567. _cimg_mp_const_scalar((double)cimg::uint2float((unsigned int)mem[arg1]));
  20568. _cimg_mp_scalar1(mp_ui2f,arg1);
  20569. }
  20570. if (!std::strncmp(ss,"unref(",6)) { // Un-reference variable
  20571. _cimg_mp_op("Function 'unref()'");
  20572. arg1=~0U;
  20573. for (s0 = ss6; s0<se1; s0 = s1) {
  20574. if (s0>ss6 && *s0==',') ++s0;
  20575. s1 = s0; while (s1<se1 && *s1!=',') ++s1;
  20576. c1 = *s1;
  20577. if (s1>s0) {
  20578. *s1 = 0;
  20579. get_variable_pos(s0,arg1,arg2);
  20580. if (arg2!=~0U) reserved_label[arg2] = ~0U;
  20581. else if (arg1!=~0U) {
  20582. variable_def.remove(arg1);
  20583. if (arg1<variable_pos._width - 1)
  20584. std::memmove(variable_pos._data + arg1,variable_pos._data + arg1 + 1,
  20585. sizeof(uintT)*(variable_pos._width - arg1 - 1));
  20586. --variable_pos._width;
  20587. }
  20588. *s1 = c1;
  20589. } else compile(s0,s1,depth1,0,bloc_flags); // Will throw a 'missing argument' exception
  20590. }
  20591. _cimg_mp_return(arg1!=~0U?arg1:_cimg_mp_slot_nan); // Return value of last specified variable
  20592. }
  20593. if (!std::strncmp(ss,"uppercase(",10)) { // Upper case
  20594. _cimg_mp_op("Function 'uppercase()'");
  20595. arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
  20596. if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_uppercase,arg1);
  20597. if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::uppercase(mem[arg1]));
  20598. _cimg_mp_scalar1(mp_uppercase,arg1);
  20599. }
  20600. break;
  20601. case 'v' :
  20602. if ((cimg_sscanf(ss,"vector%u%c",&(arg1=~0U),&sep)==2 && sep=='(' && arg1>0) ||
  20603. !std::strncmp(ss,"vector(",7) ||
  20604. (!std::strncmp(ss,"vector",6) && ss7<se1 && (s=std::strchr(ss7,'('))!=0)) { // Vector
  20605. _cimg_mp_op("Function 'vector()'");
  20606. arg2 = 0; // Number of specified values
  20607. if (arg1==~0U && *ss6!='(') {
  20608. arg1 = compile(ss6,s++,depth1,0,bloc_flags);
  20609. _cimg_mp_check_const_scalar(arg1,0,3);
  20610. arg1 = (unsigned int)mem[arg1];
  20611. } else s = std::strchr(ss6,'(') + 1;
  20612. if (arg1==~0U && *s=='#') { // Number of elements specified as first argument with '#'
  20613. s0 = ++s; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
  20614. arg1 = compile(s,s0++,depth1,0,bloc_flags);
  20615. _cimg_mp_check_const_scalar(arg1,1,3);
  20616. arg1 = (unsigned int)mem[arg1];
  20617. s = s0;
  20618. }
  20619. if (s<se1 || arg1==~0U) for ( ; s<se; ++s) {
  20620. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20621. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20622. arg3 = compile(s,ns,depth1,0,bloc_flags);
  20623. if (_cimg_mp_is_vector(arg3)) {
  20624. arg4 = _cimg_mp_size(arg3);
  20625. CImg<ulongT>::sequence(arg4,arg3 + 1,arg3 + arg4).move_to(l_opcode);
  20626. arg2+=arg4;
  20627. } else { CImg<ulongT>::vector(arg3).move_to(l_opcode); ++arg2; }
  20628. s = ns;
  20629. }
  20630. if (arg1==~0U) arg1 = arg2;
  20631. if (!arg1) _cimg_mp_return(0);
  20632. pos = vector(arg1);
  20633. l_opcode.insert(CImg<ulongT>::vector((ulongT)mp_vector_init,pos,0,arg1),0);
  20634. (l_opcode>'y').move_to(opcode);
  20635. opcode[2] = opcode._height;
  20636. opcode.move_to(code);
  20637. return_new_comp = true;
  20638. _cimg_mp_return(pos);
  20639. }
  20640. if (!std::strncmp(ss,"vmax(",5) || !std::strncmp(ss,"vmin(",5) ||
  20641. !std::strncmp(ss,"vmaxabs(",8) || !std::strncmp(ss,"vminabs(",8) ||
  20642. !std::strncmp(ss,"vmed(",5) || !std::strncmp(ss,"vkth(",5) ||
  20643. !std::strncmp(ss,"vsum(",5) || !std::strncmp(ss,"vavg(",5) ||
  20644. !std::strncmp(ss,"vstd(",5) || !std::strncmp(ss,"vvar(",5) ||
  20645. !std::strncmp(ss,"vprod(",6) ||
  20646. !std::strncmp(ss,"vargmin(",8) || !std::strncmp(ss,"vargmax(",8) ||
  20647. !std::strncmp(ss,"vargminabs(",11) || !std::strncmp(ss,"vargmaxabs(",11) ||
  20648. !std::strncmp(ss,"vargkth(",8)) { // Multi-argument vector functions
  20649. _cimg_mp_op(ss[1]=='a'?(ss[2]=='v'?"Function 'vavg()'":
  20650. ss[4]=='k'?"Function 'vargkth()'":
  20651. ss[5]=='i' && ss[7]=='('?"Function 'vargmin()'":
  20652. ss[5]=='i'?"Function vargminabs()'":
  20653. ss[7]=='('?"Function 'vargmax()'":
  20654. "Function 'vargmaxabs()'"):
  20655. ss[1]=='s'?(ss[2]=='u'?"Function 'vsum()'":"Function 'vstd()'"):
  20656. ss[1]=='k'?"Function 'vkth()'":
  20657. ss[1]=='p'?"Function 'vprod()'":
  20658. ss[1]=='v'?"Function 'vvar()'":
  20659. ss[2]=='i'?(ss[4]=='('?"Function 'vmin()'":
  20660. "Function 'vminabs()'"):
  20661. ss[2]=='a'?(ss[4]=='('?"Function 'vmax()'":
  20662. "Function 'vmaxabs()'"):
  20663. "Function 'vmed()'");
  20664. op = ss[1]=='a'?(ss[2]=='v'?mp_vavg:
  20665. ss[4]=='k'?mp_vargkth:
  20666. ss[5]=='i' && ss[7]=='('?mp_vargmin:
  20667. ss[5]=='i'?mp_vargminabs:
  20668. ss[7]=='('?mp_vargmax:mp_vargmaxabs):
  20669. ss[1]=='s'?(ss[2]=='u'?mp_vsum:mp_vstd):
  20670. ss[1]=='k'?mp_vkth:
  20671. ss[1]=='p'?mp_vprod:
  20672. ss[1]=='v'?mp_vvar:
  20673. ss[2]=='i'?(ss[4]=='('?mp_vmin:mp_vminabs):
  20674. ss[2]=='a'?(ss[4]=='('?mp_vmax:mp_vmaxabs):
  20675. mp_vmedian;
  20676. CImg<ulongT>::vector((ulongT)op,0,0,0).move_to(l_opcode);
  20677. p1 = ~0U;
  20678. p3 = 1;
  20679. for (s = std::strchr(ss,'(') + 1; s<se; ++s) {
  20680. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20681. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20682. arg2 = compile(s,ns,depth1,0,bloc_flags);
  20683. p2 = _cimg_mp_size(arg2);
  20684. if (p1==~0U) { if (_cimg_mp_is_vector(arg2)) p1 = p2; }
  20685. else _cimg_mp_check_type(arg2,p3,3,p1);
  20686. CImg<ulongT>::vector(arg2,p2).move_to(l_opcode);
  20687. s = ns;
  20688. ++p3;
  20689. }
  20690. (l_opcode>'y').move_to(opcode);
  20691. if (p1==~0U) { pos = scalar(); p1 = 0; } else pos = vector(p1);
  20692. opcode[1] = pos;
  20693. opcode[2] = p1;
  20694. opcode[3] = opcode._height;
  20695. opcode.move_to(code);
  20696. return_new_comp = true;
  20697. _cimg_mp_return(pos);
  20698. }
  20699. if (!std::strncmp(ss,"vtos(",5)) { // Double(s) to string
  20700. _cimg_mp_op("Function 'vtos()'");
  20701. s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20702. arg1 = compile(ss5,s1,depth1,0,bloc_flags);
  20703. arg2 = 0; arg3 = ~0U;
  20704. if (s1<se1) {
  20705. s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
  20706. arg2 = compile(++s1,s2,depth1,0,bloc_flags);
  20707. arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
  20708. }
  20709. _cimg_mp_check_type(arg2,2,1,0);
  20710. if (arg3==~0U) { // Auto-guess best output vector size
  20711. p1 = _cimg_mp_size(arg1);
  20712. p1 = p1?25*p1 - 1:24;
  20713. } else {
  20714. _cimg_mp_check_const_scalar(arg3,3,3);
  20715. p1 = (unsigned int)mem[arg3];
  20716. }
  20717. pos = vector(p1);
  20718. CImg<ulongT>::vector((ulongT)mp_vtos,pos,p1,arg1,_cimg_mp_size(arg1),arg2).move_to(code);
  20719. return_new_comp = true;
  20720. _cimg_mp_return(pos);
  20721. }
  20722. break;
  20723. case 'w' :
  20724. if (*ss1=='(') { // Image width
  20725. _cimg_mp_op("Function 'w()'");
  20726. if (*ss2=='#') { // Index specified
  20727. p1 = compile(ss3,se1,depth1,0,bloc_flags);
  20728. _cimg_mp_check_list();
  20729. } else { if (ss2!=se1) break; p1 = ~0U; }
  20730. _cimg_mp_scalar1(mp_image_w,p1);
  20731. }
  20732. if (*ss1=='h' && *ss2=='(') { // Image width*height
  20733. _cimg_mp_op("Function 'wh()'");
  20734. if (*ss3=='#') { // Index specified
  20735. p1 = compile(ss4,se1,depth1,0,bloc_flags);
  20736. _cimg_mp_check_list();
  20737. } else { if (ss3!=se1) break; p1 = ~0U; }
  20738. _cimg_mp_scalar1(mp_image_wh,p1);
  20739. }
  20740. if (*ss1=='h' && *ss2=='d' && *ss3=='(') { // Image width*height*depth
  20741. _cimg_mp_op("Function 'whd()'");
  20742. if (*ss4=='#') { // Index specified
  20743. p1 = compile(ss5,se1,depth1,0,bloc_flags);
  20744. _cimg_mp_check_list();
  20745. } else { if (ss4!=se1) break; p1 = ~0U; }
  20746. _cimg_mp_scalar1(mp_image_whd,p1);
  20747. }
  20748. if (*ss1=='h' && *ss2=='d' && *ss3=='s' && *ss4=='(') { // Image width*height*depth*spectrum
  20749. _cimg_mp_op("Function 'whds()'");
  20750. if (*ss5=='#') { // Index specified
  20751. p1 = compile(ss6,se1,depth1,0,bloc_flags);
  20752. _cimg_mp_check_list();
  20753. } else { if (ss5!=se1) break; p1 = ~0U; }
  20754. _cimg_mp_scalar1(mp_image_whds,p1);
  20755. }
  20756. if (!std::strncmp(ss,"while(",6)) { // While...do
  20757. _cimg_mp_op("Function 'while()'");
  20758. s0 = *ss5=='('?ss6:ss8;
  20759. s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20760. p1 = code._width;
  20761. arg1 = compile(s0,s1,depth1,0,bloc_flags);
  20762. p2 = code._width;
  20763. arg6 = mempos;
  20764. pos = compile(++s1,se1,depth1,0,bloc_flags);
  20765. _cimg_mp_check_type(arg1,1,1,0);
  20766. arg2 = _cimg_mp_size(pos);
  20767. CImg<ulongT>::vector((ulongT)mp_while,pos,arg1,p2 - p1,code._width - p2,arg2,
  20768. pos>=arg6 && !_cimg_mp_is_const_scalar(pos),
  20769. arg1>=arg6 && !_cimg_mp_is_const_scalar(arg1)).move_to(code,p1);
  20770. _cimg_mp_return(pos);
  20771. }
  20772. break;
  20773. case 'x' :
  20774. if (!std::strncmp(ss,"xor(",4)) { // Xor
  20775. _cimg_mp_op("Function 'xor()'");
  20776. s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
  20777. arg1 = compile(ss4,s1,depth1,0,bloc_flags);
  20778. arg2 = compile(++s1,se1,depth1,0,bloc_flags);
  20779. _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
  20780. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_xor,arg1,arg2);
  20781. if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_bitwise_xor,arg1,arg2);
  20782. if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_bitwise_xor,arg1,arg2);
  20783. if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
  20784. _cimg_mp_const_scalar((longT)mem[arg1] ^ (longT)mem[arg2]);
  20785. _cimg_mp_scalar2(mp_bitwise_xor,arg1,arg2);
  20786. }
  20787. break;
  20788. }
  20789. if (!std::strncmp(ss,"max(",4) || !std::strncmp(ss,"min(",4) ||
  20790. !std::strncmp(ss,"maxabs(",7) || !std::strncmp(ss,"minabs(",7) ||
  20791. !std::strncmp(ss,"med(",4) || !std::strncmp(ss,"kth(",4) ||
  20792. !std::strncmp(ss,"sum(",4) || !std::strncmp(ss,"avg(",4) ||
  20793. !std::strncmp(ss,"std(",4) || !std::strncmp(ss,"var(",4) ||
  20794. !std::strncmp(ss,"prod(",5) ||
  20795. !std::strncmp(ss,"argmin(",7) || !std::strncmp(ss,"argmax(",7) ||
  20796. !std::strncmp(ss,"argminabs(",10) || !std::strncmp(ss,"argmaxabs(",10) ||
  20797. !std::strncmp(ss,"argkth(",7)) { // Multi-argument functions
  20798. _cimg_mp_op(*ss=='a'?(ss[1]=='v'?"Function 'avg()'":
  20799. ss[3]=='k'?"Function 'argkth()'":
  20800. ss[4]=='i' && ss[6]=='('?"Function 'argmin()'":
  20801. ss[4]=='i'?"Function argminabs()'":
  20802. ss[6]=='('?"Function 'argmax()'":
  20803. "Function 'argmaxabs()'"):
  20804. *ss=='s'?(ss[1]=='u'?"Function 'sum()'":"Function 'std()'"):
  20805. *ss=='k'?"Function 'kth()'":
  20806. *ss=='p'?"Function 'prod()'":
  20807. *ss=='v'?"Function 'var()'":
  20808. ss[1]=='i'?(ss[3]=='('?"Function 'min()'":
  20809. "Function 'minabs()'"):
  20810. ss[1]=='a'?(ss[3]=='('?"Function 'max()'":
  20811. "Function 'maxabs()'"):
  20812. "Function 'med()'");
  20813. op = *ss=='a'?(ss[1]=='v'?mp_avg:
  20814. ss[3]=='k'?mp_argkth:
  20815. ss[4]=='i' && ss[6]=='('?mp_argmin:
  20816. ss[4]=='i'?mp_argminabs:
  20817. ss[6]=='('?mp_argmax:mp_argmaxabs):
  20818. *ss=='s'?(ss[1]=='u'?mp_sum:mp_std):
  20819. *ss=='k'?mp_kth:
  20820. *ss=='p'?mp_prod:
  20821. *ss=='v'?mp_var:
  20822. ss[1]=='i'?(ss[3]=='('?mp_min:mp_minabs):
  20823. ss[1]=='a'?(ss[3]=='('?mp_max:mp_maxabs):
  20824. mp_median;
  20825. is_sth = true; // Tell if all arguments are constant
  20826. pos = scalar();
  20827. CImg<ulongT>::vector((ulongT)op,pos,0).move_to(l_opcode);
  20828. for (s = std::strchr(ss,'(') + 1; s<se; ++s) {
  20829. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20830. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20831. arg2 = compile(s,ns,depth1,0,bloc_flags);
  20832. if (_cimg_mp_is_vector(arg2))
  20833. CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
  20834. arg2 + (ulongT)_cimg_mp_size(arg2)).
  20835. move_to(l_opcode);
  20836. else CImg<ulongT>::vector(arg2).move_to(l_opcode);
  20837. is_sth&=_cimg_mp_is_const_scalar(arg2);
  20838. s = ns;
  20839. }
  20840. (l_opcode>'y').move_to(opcode);
  20841. opcode[2] = opcode._height;
  20842. if (is_sth) _cimg_mp_const_scalar(op(*this));
  20843. opcode.move_to(code);
  20844. return_new_comp = true;
  20845. _cimg_mp_return(pos);
  20846. }
  20847. // No corresponding built-in function -> Look for a user-defined macro call.
  20848. s0 = strchr(ss,'(');
  20849. if (s0) {
  20850. variable_name.assign(ss,(unsigned int)(s0 - ss + 1)).back() = 0;
  20851. // Count number of specified arguments.
  20852. p1 = 0;
  20853. for (s = s0 + 1; s<=se1; ++p1, s = ns + 1) {
  20854. while (*s && cimg::is_blank(*s)) ++s;
  20855. if (*s==')' && !p1) break;
  20856. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20857. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20858. }
  20859. arg3 = 0; // Number of possible name matches
  20860. cimglist_for(macro_def,l) if (!std::strcmp(macro_def[l],variable_name) && ++arg3 &&
  20861. macro_def[l].back()==(char)p1) {
  20862. p2 = (unsigned int)macro_def[l].back(); // Number of required arguments
  20863. CImg<charT> _expr = macro_body[l]; // Expression to be substituted
  20864. p1 = 1; // Index of current parsed argument
  20865. for (s = s0 + 1; s<=se1; ++p1, s = ns + 1) { // Parse function arguments
  20866. while (*s && cimg::is_blank(*s)) ++s;
  20867. if (*s==')' && p1==1) break; // Function has no arguments
  20868. if (p1>p2) { ++p1; break; }
  20869. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20870. (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
  20871. variable_name.assign(s,(unsigned int)(ns - s + 1)).back() = 0; // Argument to write
  20872. arg2 = 0;
  20873. cimg_forX(_expr,k) {
  20874. if (_expr[k]==(char)p1) { // Perform argument substitution
  20875. arg1 = _expr._width;
  20876. _expr.resize(arg1 + variable_name._width - 2,1,1,1,0);
  20877. std::memmove(_expr._data + k + variable_name._width - 1,_expr._data + k + 1,arg1 - k - 1);
  20878. std::memcpy(_expr._data + k,variable_name,variable_name._width - 1);
  20879. k+=variable_name._width - 2;
  20880. }
  20881. ++arg2;
  20882. }
  20883. }
  20884. // Recompute 'pexpr' and 'level' for evaluating substituted expression.
  20885. CImg<charT> _pexpr(_expr._width);
  20886. ns = _pexpr._data;
  20887. for (ps = _expr._data, c1 = ' '; *ps; ++ps) {
  20888. if (!cimg::is_blank(*ps)) c1 = *ps;
  20889. *(ns++) = c1;
  20890. }
  20891. *ns = 0;
  20892. CImg<uintT> _level = get_level(_expr);
  20893. expr.swap(_expr);
  20894. pexpr.swap(_pexpr);
  20895. level.swap(_level);
  20896. s0 = user_macro;
  20897. user_macro = macro_def[l];
  20898. pos = compile(expr._data,expr._data + expr._width - 1,depth1,p_ref,bloc_flags);
  20899. user_macro = s0;
  20900. level.swap(_level);
  20901. pexpr.swap(_pexpr);
  20902. expr.swap(_expr);
  20903. _cimg_mp_return(pos);
  20904. }
  20905. if (arg3) { // Macro name matched but number of arguments does not
  20906. CImg<uintT> sig_nargs(arg3);
  20907. arg1 = 0;
  20908. cimglist_for(macro_def,l) if (!std::strcmp(macro_def[l],variable_name))
  20909. sig_nargs[arg1++] = (unsigned int)macro_def[l].back();
  20910. _cimg_mp_strerr;
  20911. cimg::strellipsize(variable_name,64);
  20912. if (sig_nargs._width>1) {
  20913. sig_nargs.sort();
  20914. arg1 = sig_nargs.back();
  20915. --sig_nargs._width;
  20916. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20917. "CImg<%s>::%s: Function '%s()': Number of specified arguments (%u) "
  20918. "does not match macro declaration (defined for %s or %u arguments), "
  20919. "in expression '%s'.",
  20920. pixel_type(),_cimg_mp_calling_function,variable_name._data,
  20921. p1,sig_nargs.value_string()._data,arg1,s0);
  20922. } else
  20923. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20924. "CImg<%s>::%s: Function '%s()': Number of specified arguments (%u) "
  20925. "does not match macro declaration (defined for %u argument%s), "
  20926. "in expression '%s'.",
  20927. pixel_type(),_cimg_mp_calling_function,variable_name._data,
  20928. p1,*sig_nargs,*sig_nargs!=1?"s":"",s0);
  20929. }
  20930. }
  20931. } // if (se1==')')
  20932. // Char / string initializer.
  20933. if (*se1=='\'' &&
  20934. ((se1>ss && *ss=='\'') ||
  20935. (se1>ss1 && *ss=='_' && *ss1=='\''))) {
  20936. if (*ss=='_') { _cimg_mp_op("Char initializer"); s1 = ss2; }
  20937. else { _cimg_mp_op("String initializer"); s1 = ss1; }
  20938. arg1 = (unsigned int)(se1 - s1); // Original string length
  20939. if (arg1) {
  20940. CImg<charT>(s1,arg1 + 1).move_to(variable_name).back() = 0;
  20941. cimg::strunescape(variable_name);
  20942. arg1 = (unsigned int)std::strlen(variable_name);
  20943. }
  20944. if (!arg1) _cimg_mp_return(0); // Empty string -> 0
  20945. if (*ss=='_') {
  20946. if (arg1==1) _cimg_mp_const_scalar((unsigned char)*variable_name);
  20947. _cimg_mp_strerr;
  20948. cimg::strellipsize(variable_name,64);
  20949. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  20950. "CImg<%s>::%s: %s: Literal %s contains more than one byte, "
  20951. "in expression '%s'.",
  20952. pixel_type(),_cimg_mp_calling_function,s_op,
  20953. ss1,s0);
  20954. }
  20955. pos = vector(arg1);
  20956. CImg<ulongT>::vector((ulongT)mp_string_init,pos,arg1).move_to(l_opcode);
  20957. CImg<ulongT>(1,arg1/sizeof(ulongT) + (arg1%sizeof(ulongT)?1:0)).move_to(l_opcode);
  20958. std::memcpy((char*)l_opcode[1]._data,variable_name,arg1);
  20959. (l_opcode>'y').move_to(code);
  20960. return_new_comp = true;
  20961. _cimg_mp_return(pos);
  20962. }
  20963. // Vector initializer [ ... ].
  20964. if (*ss=='[' && *se1==']') {
  20965. _cimg_mp_op("Vector initializer");
  20966. s1 = ss1; while (s1<se2 && cimg::is_blank(*s1)) ++s1;
  20967. s2 = se2; while (s2>s1 && cimg::is_blank(*s2)) --s2;
  20968. if (s2>s1 && *s1=='\'' && *s2=='\'') { // Vector values provided as a string
  20969. arg1 = (unsigned int)(s2 - s1 - 1); // Original string length
  20970. if (arg1) {
  20971. CImg<charT>(s1 + 1,arg1 + 1).move_to(variable_name).back() = 0;
  20972. cimg::strunescape(variable_name);
  20973. arg1 = (unsigned int)std::strlen(variable_name);
  20974. }
  20975. if (!arg1) _cimg_mp_return(0); // Empty string -> 0
  20976. pos = vector(arg1);
  20977. CImg<ulongT>::vector((ulongT)mp_string_init,pos,arg1).move_to(l_opcode);
  20978. CImg<ulongT>(1,arg1/sizeof(ulongT) + (arg1%sizeof(ulongT)?1:0)).move_to(l_opcode);
  20979. std::memcpy((char*)l_opcode[1]._data,variable_name,arg1);
  20980. (l_opcode>'y').move_to(code);
  20981. } else { // Vector values provided as list of items
  20982. arg1 = 0; // Number of specified values
  20983. if (*ss1!=']') for (s = ss1; s<se; ++s) {
  20984. ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
  20985. (*ns!=']' || level[ns - expr._data]!=clevel)) ++ns;
  20986. arg2 = compile(s,ns,depth1,0,bloc_flags);
  20987. if (_cimg_mp_is_vector(arg2)) {
  20988. arg3 = _cimg_mp_size(arg2);
  20989. CImg<ulongT>::sequence(arg3,arg2 + 1,arg2 + arg3).move_to(l_opcode);
  20990. arg1+=arg3;
  20991. } else { CImg<ulongT>::vector(arg2).move_to(l_opcode); ++arg1; }
  20992. s = ns;
  20993. }
  20994. if (!arg1) _cimg_mp_return(0);
  20995. pos = vector(arg1);
  20996. l_opcode.insert(CImg<ulongT>::vector((ulongT)mp_vector_init,pos,0,arg1),0);
  20997. (l_opcode>'y').move_to(opcode);
  20998. opcode[2] = opcode._height;
  20999. opcode.move_to(code);
  21000. }
  21001. return_new_comp = true;
  21002. _cimg_mp_return(pos);
  21003. }
  21004. // Variables related to the input list of images.
  21005. if (*ss1=='#' && ss2<se) {
  21006. arg1 = compile(ss2,se,depth1,0,bloc_flags);
  21007. p1 = (unsigned int)(imglist._width && _cimg_mp_is_const_scalar(arg1)?
  21008. cimg::mod((int)mem[arg1],imglist.width()):~0U);
  21009. switch (*ss) {
  21010. case 'w' : // w#ind
  21011. if (!imglist) _cimg_mp_return(0);
  21012. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width);
  21013. _cimg_mp_scalar1(mp_list_width,arg1);
  21014. case 'h' : // h#ind
  21015. if (!imglist) _cimg_mp_return(0);
  21016. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._height);
  21017. _cimg_mp_scalar1(mp_list_height,arg1);
  21018. case 'd' : // d#ind
  21019. if (!imglist) _cimg_mp_return(0);
  21020. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._depth);
  21021. _cimg_mp_scalar1(mp_list_depth,arg1);
  21022. case 'r' : // r#ind
  21023. if (!imglist) _cimg_mp_return(0);
  21024. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._is_shared);
  21025. _cimg_mp_scalar1(mp_list_is_shared,arg1);
  21026. case 's' : // s#ind
  21027. if (!imglist) _cimg_mp_return(0);
  21028. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._spectrum);
  21029. _cimg_mp_scalar1(mp_list_spectrum,arg1);
  21030. case 'i' : // i#ind
  21031. if (!imglist) _cimg_mp_return(0);
  21032. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,_cimg_mp_slot_c,
  21033. 0,_cimg_mp_boundary);
  21034. case 'I' : // I#ind
  21035. p2 = p1!=~0U?imglist[p1]._spectrum:imglist._width?~0U:0;
  21036. if (!p2) _cimg_mp_return(0);
  21037. pos = vector(p2);
  21038. CImg<ulongT>::vector((ulongT)mp_list_Joff,pos,p1,0,0,p2).move_to(code);
  21039. return_new_comp = true;
  21040. _cimg_mp_return(pos);
  21041. case 'R' : // R#ind
  21042. if (!imglist) _cimg_mp_return(0);
  21043. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,0,
  21044. 0,_cimg_mp_boundary);
  21045. case 'G' : // G#ind
  21046. if (!imglist) _cimg_mp_return(0);
  21047. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,1,
  21048. 0,_cimg_mp_boundary);
  21049. case 'B' : // B#ind
  21050. if (!imglist) _cimg_mp_return(0);
  21051. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,2,
  21052. 0,_cimg_mp_boundary);
  21053. case 'A' : // A#ind
  21054. if (!imglist) _cimg_mp_return(0);
  21055. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,3,
  21056. 0,_cimg_mp_boundary);
  21057. }
  21058. }
  21059. if (*ss1 && *ss2=='#' && ss3<se) {
  21060. arg1 = compile(ss3,se,depth1,0,bloc_flags);
  21061. p1 = (unsigned int)(imglist._width && _cimg_mp_is_const_scalar(arg1)?
  21062. cimg::mod((int)mem[arg1],imglist.width()):~0U);
  21063. if (*ss=='w' && *ss1=='h') { // wh#ind
  21064. if (!imglist) _cimg_mp_return(0);
  21065. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height);
  21066. _cimg_mp_scalar1(mp_list_wh,arg1);
  21067. }
  21068. arg2 = ~0U;
  21069. if (*ss=='i') {
  21070. if (*ss1>='0' && *ss1<='9') { // i0#ind...i9#ind
  21071. if (!imglist) _cimg_mp_return(0);
  21072. _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,*ss1 - '0',
  21073. 0,_cimg_mp_boundary);
  21074. }
  21075. if (*ss1=='c') { // ic#ind
  21076. if (!imglist) _cimg_mp_return(0);
  21077. if (_cimg_mp_is_const_scalar(arg1)) {
  21078. if (!list_median) list_median.assign(imglist._width);
  21079. if (!list_median[p1]) CImg<doubleT>::vector(imglist[p1].median()).move_to(list_median[p1]);
  21080. _cimg_mp_const_scalar(*list_median[p1]);
  21081. }
  21082. _cimg_mp_scalar1(mp_list_median,arg1);
  21083. }
  21084. if (*ss1=='n') { // in#ind
  21085. if (!imglist) _cimg_mp_return(0);
  21086. if (_cimg_mp_is_const_scalar(arg1)) {
  21087. if (!list_norm) list_norm.assign(imglist._width);
  21088. if (!list_norm[p1]) CImg<doubleT>::vector(imglist[p1].magnitude()).move_to(list_norm[p1]);
  21089. _cimg_mp_const_scalar(*list_norm[p1]);
  21090. }
  21091. _cimg_mp_scalar1(mp_list_norm,arg1);
  21092. }
  21093. switch (*ss1) {
  21094. case 'm' : arg2 = 0; break; // im#ind
  21095. case 'M' : arg2 = 1; break; // iM#ind
  21096. case 'a' : arg2 = 2; break; // ia#ind
  21097. case 'v' : arg2 = 3; break; // iv#ind
  21098. case 's' : arg2 = 12; break; // is#ind
  21099. case 'p' : arg2 = 13; break; // ip#ind
  21100. }
  21101. } else if (*ss1=='m') switch (*ss) {
  21102. case 'x' : arg2 = 4; break; // xm#ind
  21103. case 'y' : arg2 = 5; break; // ym#ind
  21104. case 'z' : arg2 = 6; break; // zm#ind
  21105. case 'c' : arg2 = 7; break; // cm#ind
  21106. } else if (*ss1=='M') switch (*ss) {
  21107. case 'x' : arg2 = 8; break; // xM#ind
  21108. case 'y' : arg2 = 9; break; // yM#ind
  21109. case 'z' : arg2 = 10; break; // zM#ind
  21110. case 'c' : arg2 = 11; break; // cM#ind
  21111. }
  21112. if (arg2!=~0U) {
  21113. if (!imglist) _cimg_mp_return(0);
  21114. if (_cimg_mp_is_const_scalar(arg1)) {
  21115. if (!list_stats) list_stats.assign(imglist._width);
  21116. if (!list_stats[p1]) list_stats[p1].assign(1,14,1,1,0).fill(imglist[p1].get_stats(),false);
  21117. _cimg_mp_const_scalar(list_stats(p1,arg2));
  21118. }
  21119. _cimg_mp_scalar2(mp_list_stats,arg1,arg2);
  21120. }
  21121. }
  21122. if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='#' && ss4<se) { // whd#ind
  21123. arg1 = compile(ss4,se,depth1,0,bloc_flags);
  21124. if (!imglist) _cimg_mp_return(0);
  21125. p1 = (unsigned int)(_cimg_mp_is_const_scalar(arg1)?cimg::mod((int)mem[arg1],imglist.width()):~0U);
  21126. if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height*imglist[p1]._depth);
  21127. _cimg_mp_scalar1(mp_list_whd,arg1);
  21128. }
  21129. if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='s' && *ss4=='#' && ss5<se) { // whds#ind
  21130. arg1 = compile(ss5,se,depth1,0,bloc_flags);
  21131. if (!imglist) _cimg_mp_return(0);
  21132. p1 = (unsigned int)(_cimg_mp_is_const_scalar(arg1)?cimg::mod((int)mem[arg1],imglist.width()):~0U);
  21133. if (p1!=~0U)
  21134. _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height*imglist[p1]._depth*imglist[p1]._spectrum);
  21135. _cimg_mp_scalar1(mp_list_whds,arg1);
  21136. }
  21137. if (!std::strcmp(ss,"interpolation")) _cimg_mp_return(_cimg_mp_interpolation); // interpolation
  21138. if (!std::strcmp(ss,"boundary")) _cimg_mp_return(_cimg_mp_boundary); // boundary
  21139. #ifdef cimg_mp_operator_dollar
  21140. // External variable '$varname'.
  21141. variable_name.assign(ss,(unsigned int)(se + 1 - ss)).back() = 0;
  21142. if (*ss=='$' && is_varname(variable_name._data + 1))
  21143. _cimg_mp_const_scalar(cimg_mp_operator_dollar(variable_name._data + 1));
  21144. #endif
  21145. // No known item found, assuming this is an already initialized variable.
  21146. if (is_varname(variable_name)) { // Valid variable name
  21147. get_variable_pos(variable_name,arg1,arg2);
  21148. arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U;
  21149. if (arg1!=~0U) _cimg_mp_return(arg1);
  21150. }
  21151. // Reached an unknown item -> error.
  21152. c1 = *se1;
  21153. _cimg_mp_strerr;
  21154. cimg::strellipsize(variable_name,64);
  21155. if (is_sth)
  21156. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21157. "CImg<%s>::%s: Undefined variable '%s' in expression '%s'.",
  21158. pixel_type(),_cimg_mp_calling_function,
  21159. variable_name._data,s0);
  21160. s1 = std::strchr(ss,'(');
  21161. s_op = s1 && c1==')'?"function call":"item";
  21162. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21163. "CImg<%s>::%s: Unrecognized %s '%s' in expression '%s'.",
  21164. pixel_type(),_cimg_mp_calling_function,
  21165. s_op,variable_name._data,s0);
  21166. }
  21167. // Evaluation procedure.
  21168. double operator()(const double x, const double y, const double z, const double c) {
  21169. mem[_cimg_mp_slot_x] = x; mem[_cimg_mp_slot_y] = y; mem[_cimg_mp_slot_z] = z; mem[_cimg_mp_slot_c] = c;
  21170. for (p_code = code; p_code<p_code_end; ++p_code) {
  21171. opcode._data = p_code->_data;
  21172. const ulongT target = opcode[1];
  21173. mem[target] = _cimg_mp_defunc(*this);
  21174. }
  21175. return *result;
  21176. }
  21177. // Evaluation procedure (return output values in vector 'output').
  21178. template<typename t>
  21179. void operator()(const double x, const double y, const double z, const double c, t *const output) {
  21180. mem[_cimg_mp_slot_x] = x; mem[_cimg_mp_slot_y] = y; mem[_cimg_mp_slot_z] = z; mem[_cimg_mp_slot_c] = c;
  21181. for (p_code = code; p_code<p_code_end; ++p_code) {
  21182. opcode._data = p_code->_data;
  21183. const ulongT target = opcode[1];
  21184. mem[target] = _cimg_mp_defunc(*this);
  21185. }
  21186. if (result_dim) {
  21187. const double *ptrs = result + 1;
  21188. t *ptrd = output;
  21189. for (unsigned int k = 0; k<result_dim; ++k) *(ptrd++) = (t)*(ptrs++);
  21190. } else *output = (t)*result;
  21191. }
  21192. // Evaluation procedure for begin_t() bloc.
  21193. void begin_t() {
  21194. if (!code_begin_t) return;
  21195. mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
  21196. p_code_end = code_begin_t.end();
  21197. for (p_code = code_begin_t; p_code<p_code_end; ++p_code) {
  21198. opcode._data = p_code->_data;
  21199. const ulongT target = opcode[1];
  21200. mem[target] = _cimg_mp_defunc(*this);
  21201. }
  21202. p_code_end = code.end();
  21203. }
  21204. // Evaluation procedure for end_t() bloc.
  21205. void end_t() {
  21206. if (!code_end_t) return;
  21207. if (imgin) {
  21208. mem[_cimg_mp_slot_x] = imgin._width - 1.;
  21209. mem[_cimg_mp_slot_y] = imgin._height - 1.;
  21210. mem[_cimg_mp_slot_z] = imgin._depth - 1.;
  21211. mem[_cimg_mp_slot_c] = imgin._spectrum - 1.;
  21212. } else mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
  21213. p_code_end = code_end_t.end();
  21214. for (p_code = code_end_t; p_code<p_code_end; ++p_code) {
  21215. opcode._data = p_code->_data;
  21216. const ulongT target = opcode[1];
  21217. mem[target] = _cimg_mp_defunc(*this);
  21218. }
  21219. }
  21220. // Evaluation procedure the end() bloc.
  21221. void end() {
  21222. if (!code_end) return;
  21223. if (imgin) {
  21224. mem[_cimg_mp_slot_x] = imgin._width - 1.;
  21225. mem[_cimg_mp_slot_y] = imgin._height - 1.;
  21226. mem[_cimg_mp_slot_z] = imgin._depth - 1.;
  21227. mem[_cimg_mp_slot_c] = imgin._spectrum - 1.;
  21228. } else mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
  21229. p_code_end = code_end.end();
  21230. for (p_code = code_end; p_code<p_code_end; ++p_code) {
  21231. opcode._data = p_code->_data;
  21232. const ulongT target = opcode[1];
  21233. mem[target] = _cimg_mp_defunc(*this);
  21234. }
  21235. }
  21236. // Merge inter-thread variables.
  21237. // (argument 'mp' is the master instance).
  21238. void merge(_cimg_math_parser& mp) {
  21239. if (&mp==this) return;
  21240. cimg_rofY(mp.memmerge,k) {
  21241. const unsigned int
  21242. pos = (unsigned int)mp.memmerge(0,k),
  21243. siz = (unsigned int)mp.memmerge(1,k),
  21244. iop = (unsigned int)mp.memmerge(2,k);
  21245. if (!siz) switch (iop) { // Scalar value
  21246. case 0 : mp.mem[pos] = mem[pos]; break; // Assignment
  21247. case 1 : mp.mem[pos]+=mem[pos]; break; // Operator+
  21248. case 2 : mp.mem[pos]-=mem[pos]; break; // Operator-
  21249. case 3 : mp.mem[pos]*=mem[pos]; break; // Operator*
  21250. case 4 : mp.mem[pos]/=mem[pos]; break; // Operator/
  21251. case 5 : mp.mem[pos] = (double)((longT)mp.mem[pos] & (longT)mem[pos]); break; // Operator&
  21252. case 6 : mp.mem[pos] = (double)((longT)mp.mem[pos] | (longT)mem[pos]); break; // Operator|
  21253. case 7 : mp.mem[pos] = (double)((longT)mp.mem[pos] ^ (longT)mem[pos]); break; // Operator 'xor'
  21254. case 8 : mp.mem[pos] = mp.mem[pos] && mem[pos]; break; // Operator&&
  21255. case 9 : mp.mem[pos] = mp.mem[pos] || mem[pos]; break; // Operator||
  21256. case 10 : mp.mem[pos] = std::min(mp.mem[pos],mem[pos]); break; // Operator 'min'
  21257. case 11 : mp.mem[pos] = std::max(mp.mem[pos],mem[pos]); break; // Operator 'max'
  21258. } else switch (iop) { // Vector value
  21259. case 0 : // Assignment
  21260. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true) = CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21261. break;
  21262. case 1 : // Operator+
  21263. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)+=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21264. break;
  21265. case 2 : // Operator-
  21266. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)-=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21267. break;
  21268. case 3 : // Operator*
  21269. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)*=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21270. break;
  21271. case 4 : // Operator/
  21272. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)/=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21273. break;
  21274. case 5 : // Operator&
  21275. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)&=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21276. break;
  21277. case 6 : // Operator|
  21278. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)|=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21279. break;
  21280. case 7 : // Operator 'xor'
  21281. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)^=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
  21282. break;
  21283. case 8 : { // Operator&&
  21284. CImg<doubleT>
  21285. arg1(&mp.mem[pos + 1],siz,1,1,1,true),
  21286. arg2(&mem[pos + 1],siz,1,1,1,true);
  21287. cimg_foroff(arg1,off) arg1[off] = arg1[off] && arg2[off];
  21288. } break;
  21289. case 9 : { // Operator||
  21290. CImg<doubleT>
  21291. arg1(&mp.mem[pos + 1],siz,1,1,1,true),
  21292. arg2(&mem[pos + 1],siz,1,1,1,true);
  21293. cimg_foroff(arg1,off) arg1[off] = arg1[off] || arg2[off];
  21294. } break;
  21295. case 10 : // Operator 'min'
  21296. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true).min(CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true));
  21297. break;
  21298. case 11 : // Operator 'max'
  21299. CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true).max(CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true));
  21300. break;
  21301. }
  21302. }
  21303. }
  21304. // Return specified argument number as a string.
  21305. static const char *s_argth(const unsigned int n_arg) {
  21306. const char
  21307. *_s_arg[] = { "", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth","Ninth",
  21308. "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
  21309. "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "One of the" };
  21310. return _s_arg[n_arg<sizeof(_s_arg)/sizeof(char*)?n_arg:sizeof(_s_arg)/sizeof(char*)-1];
  21311. }
  21312. // Return a string that defines the calling function + the user-defined function scope.
  21313. CImg<charT> s_calling_function() const {
  21314. CImg<charT> res;
  21315. const unsigned int
  21316. l1 = calling_function?(unsigned int)std::strlen(calling_function):0U,
  21317. l2 = user_macro?(unsigned int)std::strlen(user_macro):0U;
  21318. if (l2) {
  21319. res.assign(l1 + l2 + 48);
  21320. cimg_snprintf(res,res._width,"%s(): When substituting function '%s()'",calling_function,user_macro);
  21321. } else {
  21322. res.assign(l1 + l2 + 4);
  21323. cimg_snprintf(res,res._width,"%s()",calling_function);
  21324. }
  21325. return res;
  21326. }
  21327. // Return type of a memory element as a string.
  21328. CImg<charT> s_type(const unsigned int arg) const {
  21329. CImg<charT> res;
  21330. if (_cimg_mp_is_vector(arg)) { // Vector
  21331. CImg<charT>::string("vectorXXXXXXXXXXXXXXXX").move_to(res);
  21332. cimg_sprintf(res._data + 6,"%u",_cimg_mp_size(arg));
  21333. } else if (_cimg_mp_is_const_scalar(arg)) CImg<charT>::string("const scalar").move_to(res); // Const scalar
  21334. else CImg<charT>::string("scalar").move_to(res); // Scalar
  21335. return res;
  21336. }
  21337. // Count parentheses/brackets level of each character of the expression.
  21338. CImg<uintT> get_level(CImg<charT>& _expr) const {
  21339. bool is_escaped = false, next_is_escaped = false;
  21340. unsigned int mode = 0, next_mode = 0; // { 0=normal | 1=char-string | 2=vector-string
  21341. CImg<uintT> res(_expr._width - 1);
  21342. unsigned int *pd = res._data;
  21343. int _level = 0;
  21344. for (const char *ps = _expr._data; *ps && _level>=0; ++ps) {
  21345. if (!is_escaped && !next_is_escaped && *ps=='\\') next_is_escaped = true;
  21346. if (!is_escaped && *ps=='\'') { // Non-escaped character
  21347. if (!mode && ps>_expr._data && *(ps - 1)=='[') next_mode = mode = 2; // Start vector-string
  21348. else if (mode==2 && *(ps + 1)==']') next_mode = !mode; // End vector-string
  21349. else if (mode<2) next_mode = mode?(mode = 0):1; // Start/end char-string
  21350. }
  21351. *(pd++) = (unsigned int)(mode>=1 || is_escaped?_level + (mode==1):
  21352. *ps=='(' || *ps=='['?_level++:
  21353. *ps==')' || *ps==']'?--_level:
  21354. _level);
  21355. mode = next_mode;
  21356. is_escaped = next_is_escaped;
  21357. next_is_escaped = false;
  21358. }
  21359. if (mode) {
  21360. cimg::strellipsize(_expr,64);
  21361. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21362. "CImg<%s>::%s: Unterminated string literal, in expression '%s'.",
  21363. pixel_type(),_cimg_mp_calling_function,
  21364. _expr._data);
  21365. }
  21366. if (_level) {
  21367. cimg::strellipsize(_expr,64);
  21368. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21369. "CImg<%s>::%s: Unbalanced parentheses/brackets, in expression '%s'.",
  21370. pixel_type(),_cimg_mp_calling_function,
  21371. _expr._data);
  21372. }
  21373. return res;
  21374. }
  21375. // Find and return index of current image 'imgin' within image list 'imglist'.
  21376. unsigned int get_mem_img_index() {
  21377. if (mem_img_index==~0U) {
  21378. if (&imgout>imglist.data() && &imgout<imglist.end())
  21379. mem_img_index = const_scalar((double)(&imgout - imglist.data()));
  21380. else {
  21381. unsigned int pos = ~0U;
  21382. cimglist_for(imglist,l)
  21383. if (imgout._data==imglist[l]._data && imgout.is_sameXYZC(imglist[l])) { pos = l; break; }
  21384. if (pos!=~0U) mem_img_index = const_scalar((double)pos);
  21385. }
  21386. }
  21387. return mem_img_index;
  21388. }
  21389. // Return indices for accessing math parser variables.
  21390. void get_variable_pos(const char *variable_name, unsigned int &pos, unsigned int &rpos) {
  21391. char c1, c2, c3, c4;
  21392. pos = rpos = ~0U;
  21393. if (!variable_name || !*variable_name) return;
  21394. unsigned int rp = variable_name[1]?~0U:*variable_name; // One-char variable
  21395. if (variable_name[1] && !variable_name[2]) { // Two-chars variable
  21396. c1 = variable_name[0];
  21397. c2 = variable_name[1];
  21398. if (c1=='w' && c2=='h') rp = 0; // wh
  21399. else if (c1=='p' && c2=='i') rp = 3; // pi
  21400. else if (c1=='i') {
  21401. if (c2>='0' && c2<='9') rp = 20 + c2 - '0'; // i0...i9
  21402. else if (c2=='m') rp = 4; // im
  21403. else if (c2=='M') rp = 5; // iM
  21404. else if (c2=='a') rp = 6; // ia
  21405. else if (c2=='v') rp = 7; // iv
  21406. else if (c2=='s') rp = 8; // is
  21407. else if (c2=='p') rp = 9; // ip
  21408. else if (c2=='c') rp = 10; // ic
  21409. else if (c2=='n') rp = 11; // in
  21410. } else if (c2=='m') {
  21411. if (c1=='x') rp = 12; // xm
  21412. else if (c1=='y') rp = 13; // ym
  21413. else if (c1=='z') rp = 14; // zm
  21414. else if (c1=='c') rp = 15; // cm
  21415. } else if (c2=='M') {
  21416. if (c1=='x') rp = 16; // xM
  21417. else if (c1=='y') rp = 17; // yM
  21418. else if (c1=='z') rp = 18; // zM
  21419. else if (c1=='c') rp = 19; // cM
  21420. }
  21421. } else if (variable_name[1] && variable_name[2] && !variable_name[3]) { // Three-chars variable
  21422. c1 = variable_name[0];
  21423. c2 = variable_name[1];
  21424. c3 = variable_name[2];
  21425. if (c1=='w' && c2=='h' && c3=='d') rp = 1; // whd
  21426. } else if (variable_name[1] && variable_name[2] && variable_name[3] &&
  21427. !variable_name[4]) { // Four-chars variable
  21428. c1 = variable_name[0];
  21429. c2 = variable_name[1];
  21430. c3 = variable_name[2];
  21431. c4 = variable_name[3];
  21432. if (c1=='w' && c2=='h' && c3=='d' && c4=='s') rp = 2; // whds
  21433. } else if (!std::strcmp(variable_name,"interpolation")) rp = 30; // interpolation
  21434. else if (!std::strcmp(variable_name,"boundary")) rp = 31; // boundary
  21435. if (rp!=~0U) { rpos = rp; return; } // One of the reserved labels
  21436. // Multi-char variable name : check for existing variable with same name
  21437. cimglist_for(variable_def,i)
  21438. if (!std::strcmp(variable_name,variable_def[i])) { pos = i; break; }
  21439. }
  21440. // Tell for each character of an expression if it is inside a string or not.
  21441. CImg<boolT> is_inside_string(CImg<charT>& _expr) const {
  21442. bool is_escaped = false, next_is_escaped = false;
  21443. unsigned int mode = 0, next_mode = 0; // { 0=normal | 1=char-string | 2=vector-string
  21444. CImg<boolT> res = CImg<charT>::string(_expr);
  21445. bool *pd = res._data;
  21446. for (const char *ps = _expr._data; *ps; ++ps) {
  21447. if (!next_is_escaped && *ps=='\\') next_is_escaped = true;
  21448. if (!is_escaped && *ps=='\'') { // Non-escaped character
  21449. if (!mode && ps>_expr._data && *(ps - 1)=='[') next_mode = mode = 2; // Start vector-string
  21450. else if (mode==2 && *(ps + 1)==']') next_mode = !mode; // End vector-string
  21451. else if (mode<2) next_mode = mode?(mode = 0):1; // Start/end char-string
  21452. }
  21453. *(pd++) = mode>=1 || is_escaped;
  21454. mode = next_mode;
  21455. is_escaped = next_is_escaped;
  21456. next_is_escaped = false;
  21457. }
  21458. return res;
  21459. }
  21460. // Return true if specified argument can be a part of an allowed variable name.
  21461. static bool is_varchar(const char c) {
  21462. return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
  21463. }
  21464. // Return true if specified argument can be considered as a variable name.
  21465. static bool is_varname(const char *const str, const unsigned int length=~0U) {
  21466. if (*str>='0' && *str<='9') return false;
  21467. for (unsigned int l = 0; l<length && str[l]; ++l)
  21468. if (!is_varchar(str[l])) return false;
  21469. return true;
  21470. }
  21471. // Return true if all values of a vector are computation values.
  21472. bool is_comp_vector(const unsigned int arg) const {
  21473. unsigned int siz = _cimg_mp_size(arg);
  21474. if (siz>128) return false;
  21475. const int *ptr = memtype.data(arg + 1);
  21476. bool is_tmp = true;
  21477. while (siz-->0) if (*(ptr++)) { is_tmp = false; break; }
  21478. return is_tmp;
  21479. }
  21480. // Check if a memory slot is a positive integer constant scalar value.
  21481. // 'mode' can be:
  21482. // { 0=constant | 1=integer constant | 2=positive integer constant | 3=strictly-positive integer constant }
  21483. void check_const_scalar(const unsigned int arg, const unsigned int n_arg,
  21484. const unsigned int mode,
  21485. char *const ss, char *const se, const char saved_char) {
  21486. _cimg_mp_check_type(arg,n_arg,1,0);
  21487. if (!_cimg_mp_is_const_scalar(arg)) {
  21488. const char *const s_arg = s_argth(n_arg);
  21489. char *s0; _cimg_mp_strerr;
  21490. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21491. "CImg<%s>::%s: %s%s %s%s (of type '%s') is not a constant, "
  21492. "in expression '%s'.",
  21493. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
  21494. s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,s0);
  21495. }
  21496. const double val = mem[arg];
  21497. if (!((!mode || (double)(int)mem[arg]==mem[arg]) &&
  21498. (mode<2 || mem[arg]>=(mode==3)))) {
  21499. const char *const s_arg = s_argth(n_arg);
  21500. char *s0; _cimg_mp_strerr;
  21501. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21502. "CImg<%s>::%s: %s%s %s%s (of type '%s' and value %g) is not a%s constant, "
  21503. "in expression '%s'.",
  21504. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
  21505. s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,val,
  21506. !mode?"":mode==1?"n integer":
  21507. mode==2?" positive integer":" strictly positive integer",s0);
  21508. }
  21509. }
  21510. // Check if an image index is a constant value.
  21511. void check_const_index(const unsigned int arg,
  21512. char *const ss, char *const se, const char saved_char) {
  21513. if (arg!=~0U && !_cimg_mp_is_const_scalar(arg)) {
  21514. char *s0; _cimg_mp_strerr;
  21515. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21516. "CImg<%s>::%s: %s%s Specified image index is not a constant, "
  21517. "in expression '%s'.",
  21518. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s0);
  21519. }
  21520. }
  21521. // Check a matrix is square.
  21522. void check_matrix_square(const unsigned int arg, const unsigned int n_arg,
  21523. char *const ss, char *const se, const char saved_char) {
  21524. _cimg_mp_check_type(arg,n_arg,2,0);
  21525. const unsigned int
  21526. siz = _cimg_mp_size(arg),
  21527. n = (unsigned int)cimg::round(std::sqrt((float)siz));
  21528. if (n*n!=siz) {
  21529. const char *s_arg;
  21530. if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
  21531. else s_arg = !n_arg?"":n_arg==1?"First":n_arg==2?"Second":n_arg==3?"Third":"One";
  21532. char *s0; _cimg_mp_strerr;
  21533. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21534. "CImg<%s>::%s: %s%s %s%s (of type '%s') "
  21535. "cannot be considered as a square matrix, in expression '%s'.",
  21536. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
  21537. s_arg,*s_op=='F'?(*s_arg?" argument":" Argument"):(*s_arg?" operand":" Operand"),
  21538. s_type(arg)._data,s0);
  21539. }
  21540. }
  21541. // Check type compatibility for one argument.
  21542. // Bits of 'mode' tells what types are allowed:
  21543. // { 1 = scalar | 2 = vectorN }.
  21544. // If 'N' is not zero, it also restricts the vectors to be of size N only.
  21545. void check_type(const unsigned int arg, const unsigned int n_arg,
  21546. const unsigned int mode, const unsigned int N,
  21547. char *const ss, char *const se, const char saved_char) {
  21548. const bool
  21549. is_scalar = _cimg_mp_is_scalar(arg),
  21550. is_vector = _cimg_mp_is_vector(arg) && (!N || _cimg_mp_size(arg)==N);
  21551. bool cond = false;
  21552. if (mode&1) cond|=is_scalar;
  21553. if (mode&2) cond|=is_vector;
  21554. if (!cond) {
  21555. const char *s_arg;
  21556. if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
  21557. else s_arg = s_argth(n_arg);
  21558. CImg<charT> sb_type(32);
  21559. if (mode==1) cimg_snprintf(sb_type,sb_type._width,"'scalar'");
  21560. else if (mode==2) {
  21561. if (N) cimg_snprintf(sb_type,sb_type._width,"'vector%u'",N);
  21562. else cimg_snprintf(sb_type,sb_type._width,"'vector'");
  21563. } else {
  21564. if (N) cimg_snprintf(sb_type,sb_type._width,"'scalar' or 'vector%u'",N);
  21565. else cimg_snprintf(sb_type,sb_type._width,"'scalar' or 'vector'");
  21566. }
  21567. char *s0; _cimg_mp_strerr;
  21568. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21569. "CImg<%s>::%s: %s%s %s%s has invalid type '%s' (should be %s), "
  21570. "in expression '%s'.",
  21571. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
  21572. s_arg,*s_op=='F'?(*s_arg?" argument":" Argument"):(*s_arg?" operand":" Operand"),
  21573. s_type(arg)._data,sb_type._data,s0);
  21574. }
  21575. }
  21576. // Check that imglist are not empty.
  21577. void check_list(char *const ss, char *const se, const char saved_char) {
  21578. if (!imglist) {
  21579. char *s0; _cimg_mp_strerr;
  21580. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21581. "CImg<%s>::%s: %s%s Invalid call with an empty image list, "
  21582. "in expression '%s'.",
  21583. pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s0);
  21584. }
  21585. }
  21586. static void mp_check_list(_cimg_math_parser& mp, const char *const funcname) {
  21587. if (!mp.imglist)
  21588. throw CImgArgumentException("[" cimg_appname "_math_parser] "
  21589. "CImg<%s>: Function '%s()': Invalid call with an empty image list.",
  21590. pixel_type(),funcname);
  21591. }
  21592. // Insert constant value in memory.
  21593. unsigned int const_scalar(const double val) {
  21594. // Search for built-in constant.
  21595. if (cimg::type<double>::is_nan(val)) return _cimg_mp_slot_nan;
  21596. if (val==(double)(int)val) {
  21597. if (val>=0 && val<=10) return (unsigned int)val;
  21598. if (val<0 && val>=-5) return (unsigned int)(10 - val);
  21599. }
  21600. if (val==0.5) return 16;
  21601. // Search for constant already requested before (in const cache).
  21602. unsigned int ind = ~0U;
  21603. if (constcache_size<1024) {
  21604. if (!constcache_size) {
  21605. constcache_vals.assign(16,1,1,1,0);
  21606. constcache_inds.assign(16,1,1,1,0);
  21607. *constcache_vals = val;
  21608. constcache_size = 1;
  21609. ind = 0;
  21610. } else { // Dichotomic search
  21611. const double val_beg = *constcache_vals, val_end = constcache_vals[constcache_size - 1];
  21612. if (val_beg>=val) ind = 0;
  21613. else if (val_end==val) ind = constcache_size - 1;
  21614. else if (val_end<val) ind = constcache_size;
  21615. else {
  21616. unsigned int i0 = 1, i1 = constcache_size - 2;
  21617. while (i0<=i1) {
  21618. const unsigned int mid = (i0 + i1)/2;
  21619. if (constcache_vals[mid]==val) { i0 = mid; break; }
  21620. else if (constcache_vals[mid]<val) i0 = mid + 1;
  21621. else i1 = mid - 1;
  21622. }
  21623. ind = i0;
  21624. }
  21625. if (ind>=constcache_size || constcache_vals[ind]!=val) {
  21626. ++constcache_size;
  21627. if (constcache_size>constcache_vals._width) {
  21628. constcache_vals.resize(-200,1,1,1,0);
  21629. constcache_inds.resize(-200,1,1,1,0);
  21630. }
  21631. const int l = constcache_size - (int)ind - 1;
  21632. if (l>0) {
  21633. std::memmove(&constcache_vals[ind + 1],&constcache_vals[ind],l*sizeof(double));
  21634. std::memmove(&constcache_inds[ind + 1],&constcache_inds[ind],l*sizeof(unsigned int));
  21635. }
  21636. constcache_vals[ind] = val;
  21637. constcache_inds[ind] = 0;
  21638. }
  21639. }
  21640. if (constcache_inds[ind]) return constcache_inds[ind];
  21641. }
  21642. // Insert new constant in memory if necessary.
  21643. if (mempos>=mem._width) { mem.resize(-200,1,1,1,0); memtype.resize(-200,1,1,1,0); }
  21644. const unsigned int pos = mempos++;
  21645. mem[pos] = val;
  21646. memtype[pos] = 1; // Set constant property
  21647. if (ind!=~0U) constcache_inds[ind] = pos;
  21648. return pos;
  21649. }
  21650. // Insert new scalar in memory.
  21651. unsigned int scalar() {
  21652. if (mempos>=mem._width) { mem.resize(-200,1,1,1,0); memtype.resize(mem._width,1,1,1,0); }
  21653. return mempos++;
  21654. }
  21655. // Insert new vector of specified size in memory.
  21656. unsigned int vector(const unsigned int siz) {
  21657. if (mempos + siz>=mem._width) {
  21658. mem.resize(2*mem._width + siz,1,1,1,0);
  21659. memtype.resize(mem._width,1,1,1,0);
  21660. }
  21661. const unsigned int pos = mempos++;
  21662. mem[pos] = cimg::type<double>::nan();
  21663. memtype[pos] = siz + 1;
  21664. mempos+=siz;
  21665. return pos;
  21666. }
  21667. // Insert new initialized vector.
  21668. unsigned int vector(const unsigned int siz, const double value) {
  21669. const unsigned int pos = vector(siz);
  21670. double *ptr = &mem[pos] + 1;
  21671. for (unsigned int i = 0; i<siz; ++i) *(ptr++) = value;
  21672. return pos;
  21673. }
  21674. // Insert new copy of specified vector in memory.
  21675. unsigned int vector_copy(const unsigned int arg) {
  21676. const unsigned int
  21677. siz = _cimg_mp_size(arg),
  21678. pos = vector(siz);
  21679. CImg<ulongT>::vector((ulongT)mp_vector_copy,pos,arg,siz).move_to(code);
  21680. return pos;
  21681. }
  21682. // Set reserved status to all values of a vector.
  21683. void set_reserved_vector(const unsigned int arg) {
  21684. unsigned int siz = _cimg_mp_size(arg);
  21685. int *ptr = memtype.data(arg + 1);
  21686. while (siz-->0) *(ptr++) = -1;
  21687. }
  21688. unsigned int scalar0(const mp_func op) {
  21689. const unsigned int pos = scalar();
  21690. CImg<ulongT>::vector((ulongT)op,pos).move_to(code);
  21691. return_new_comp = true;
  21692. return pos;
  21693. }
  21694. unsigned int scalar1(const mp_func op, const unsigned int arg1) {
  21695. const unsigned int pos =
  21696. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1) && op!=mp_copy?arg1:
  21697. ((return_new_comp = true), scalar());
  21698. CImg<ulongT>::vector((ulongT)op,pos,arg1).move_to(code);
  21699. return pos;
  21700. }
  21701. unsigned int scalar2(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
  21702. const unsigned int pos =
  21703. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21704. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21705. ((return_new_comp = true), scalar());
  21706. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2).move_to(code);
  21707. return pos;
  21708. }
  21709. unsigned int scalar3(const mp_func op,
  21710. const unsigned int arg1, const unsigned int arg2, const unsigned int arg3) {
  21711. const unsigned int pos =
  21712. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21713. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21714. arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
  21715. ((return_new_comp = true), scalar());
  21716. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3).move_to(code);
  21717. return pos;
  21718. }
  21719. unsigned int scalar4(const mp_func op,
  21720. const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
  21721. const unsigned int arg4) {
  21722. const unsigned int pos =
  21723. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21724. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21725. arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
  21726. arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
  21727. ((return_new_comp = true), scalar());
  21728. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4).move_to(code);
  21729. return pos;
  21730. }
  21731. unsigned int scalar5(const mp_func op,
  21732. const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
  21733. const unsigned int arg4, const unsigned int arg5) {
  21734. const unsigned int pos =
  21735. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21736. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21737. arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
  21738. arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
  21739. arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
  21740. ((return_new_comp = true), scalar());
  21741. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5).move_to(code);
  21742. return pos;
  21743. }
  21744. unsigned int scalar6(const mp_func op,
  21745. const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
  21746. const unsigned int arg4, const unsigned int arg5, const unsigned int arg6) {
  21747. const unsigned int pos =
  21748. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21749. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21750. arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
  21751. arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
  21752. arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
  21753. arg6!=~0U && arg6>_cimg_mp_slot_c && _cimg_mp_is_comp(arg6)?arg6:
  21754. ((return_new_comp = true), scalar());
  21755. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5,arg6).move_to(code);
  21756. return pos;
  21757. }
  21758. unsigned int scalar7(const mp_func op,
  21759. const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
  21760. const unsigned int arg4, const unsigned int arg5, const unsigned int arg6,
  21761. const unsigned int arg7) {
  21762. const unsigned int pos =
  21763. arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
  21764. arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
  21765. arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
  21766. arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
  21767. arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
  21768. arg6!=~0U && arg6>_cimg_mp_slot_c && _cimg_mp_is_comp(arg6)?arg6:
  21769. arg7!=~0U && arg7>_cimg_mp_slot_c && _cimg_mp_is_comp(arg7)?arg7:
  21770. ((return_new_comp = true), scalar());
  21771. CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5,arg6,arg7).move_to(code);
  21772. return pos;
  21773. }
  21774. void self_vector_s(const unsigned int pos, const mp_func op, const unsigned int arg1) {
  21775. const unsigned int siz = _cimg_mp_size(pos);
  21776. if (siz>24) CImg<ulongT>::vector((ulongT)mp_self_map_vector_s,pos,siz,(ulongT)op,arg1).move_to(code);
  21777. else {
  21778. code.insert(siz);
  21779. for (unsigned int k = 1; k<=siz; ++k)
  21780. CImg<ulongT>::vector((ulongT)op,pos + k,arg1).move_to(code[code._width - 1 - siz + k]);
  21781. }
  21782. }
  21783. void self_vector_v(const unsigned int pos, const mp_func op, const unsigned int arg1) {
  21784. const unsigned int siz = _cimg_mp_size(pos);
  21785. if (siz>24) CImg<ulongT>::vector((ulongT)mp_self_map_vector_v,pos,siz,(ulongT)op,arg1).move_to(code);
  21786. else {
  21787. code.insert(siz);
  21788. for (unsigned int k = 1; k<=siz; ++k)
  21789. CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k).move_to(code[code._width - 1 - siz + k]);
  21790. }
  21791. }
  21792. unsigned int vector1_v(const mp_func op, const unsigned int arg1) {
  21793. const unsigned int
  21794. siz = _cimg_mp_size(arg1),
  21795. pos = is_comp_vector(arg1)?arg1:
  21796. ((return_new_comp = true), vector(siz));
  21797. if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_v,pos,siz,(ulongT)op,arg1).move_to(code);
  21798. else {
  21799. code.insert(siz);
  21800. for (unsigned int k = 1; k<=siz; ++k)
  21801. CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k).move_to(code[code._width - 1 - siz + k]);
  21802. }
  21803. return pos;
  21804. }
  21805. unsigned int vector2_vv(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
  21806. const unsigned int
  21807. siz = _cimg_mp_size(arg1),
  21808. pos = is_comp_vector(arg1)?arg1:is_comp_vector(arg2)?arg2:
  21809. ((return_new_comp = true), vector(siz));
  21810. if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vv,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
  21811. else {
  21812. code.insert(siz);
  21813. for (unsigned int k = 1; k<=siz; ++k)
  21814. CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2 + k).move_to(code[code._width - 1 - siz + k]);
  21815. }
  21816. return pos;
  21817. }
  21818. unsigned int vector2_vs(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
  21819. const unsigned int
  21820. siz = _cimg_mp_size(arg1),
  21821. pos = is_comp_vector(arg1)?arg1:
  21822. ((return_new_comp = true), vector(siz));
  21823. if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vs,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
  21824. else {
  21825. code.insert(siz);
  21826. for (unsigned int k = 1; k<=siz; ++k)
  21827. CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2).move_to(code[code._width - 1 - siz + k]);
  21828. }
  21829. return pos;
  21830. }
  21831. unsigned int vector2_sv(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
  21832. const unsigned int
  21833. siz = _cimg_mp_size(arg2),
  21834. pos = is_comp_vector(arg2)?arg2:
  21835. ((return_new_comp = true), vector(siz));
  21836. if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_sv,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
  21837. else {
  21838. code.insert(siz);
  21839. for (unsigned int k = 1; k<=siz; ++k)
  21840. CImg<ulongT>::vector((ulongT)op,pos + k,arg1,arg2 + k).move_to(code[code._width - 1 - siz + k]);
  21841. }
  21842. return pos;
  21843. }
  21844. unsigned int vector3_vss(const mp_func op, const unsigned int arg1, const unsigned int arg2,
  21845. const unsigned int arg3) {
  21846. const unsigned int
  21847. siz = _cimg_mp_size(arg1),
  21848. pos = is_comp_vector(arg1)?arg1:
  21849. ((return_new_comp = true), vector(siz));
  21850. if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vss,pos,siz,(ulongT)op,arg1,arg2,arg3).move_to(code);
  21851. else {
  21852. code.insert(siz);
  21853. for (unsigned int k = 1; k<=siz; ++k)
  21854. CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2,arg3).move_to(code[code._width - 1 - siz + k]);
  21855. }
  21856. return pos;
  21857. }
  21858. // Evaluation functions, known by the parser.
  21859. // Defining these functions 'static' ensures that sizeof(mp_func)==sizeof(ulongT),
  21860. // so we can store pointers to them directly in the opcode vectors.
  21861. #ifdef _mp_arg
  21862. #undef _mp_arg
  21863. #endif
  21864. #define _mp_arg(x) mp.mem[mp.opcode[x]]
  21865. static double mp_abs(_cimg_math_parser& mp) {
  21866. return cimg::abs(_mp_arg(2));
  21867. }
  21868. static double mp_add(_cimg_math_parser& mp) {
  21869. return _mp_arg(2) + _mp_arg(3);
  21870. }
  21871. static double mp_acos(_cimg_math_parser& mp) {
  21872. return std::acos(_mp_arg(2));
  21873. }
  21874. static double mp_acosh(_cimg_math_parser& mp) {
  21875. return cimg::acosh(_mp_arg(2));
  21876. }
  21877. static double mp_asinh(_cimg_math_parser& mp) {
  21878. return cimg::asinh(_mp_arg(2));
  21879. }
  21880. static double mp_atanh(_cimg_math_parser& mp) {
  21881. return cimg::atanh(_mp_arg(2));
  21882. }
  21883. static double mp_arg(_cimg_math_parser& mp) {
  21884. const int _ind = (int)_mp_arg(4);
  21885. const unsigned int
  21886. nb_args = (unsigned int)mp.opcode[2] - 4,
  21887. ind = _ind<0?_ind + nb_args:(unsigned int)_ind,
  21888. siz = (unsigned int)mp.opcode[3];
  21889. if (siz>0) {
  21890. if (ind>=nb_args) std::memset(&_mp_arg(1) + 1,0,siz*sizeof(double));
  21891. else std::memcpy(&_mp_arg(1) + 1,&_mp_arg(ind + 4) + 1,siz*sizeof(double));
  21892. return cimg::type<double>::nan();
  21893. }
  21894. if (ind>=nb_args) return 0;
  21895. return _mp_arg(ind + 4);
  21896. }
  21897. static double mp_arg0(_cimg_math_parser& mp) {
  21898. const int _ind = (int)_mp_arg(4);
  21899. const unsigned int
  21900. nb_args = (unsigned int)mp.opcode[2] - 4,
  21901. ind = _ind<0?_ind + nb_args:_ind + 1U,
  21902. siz = (unsigned int)mp.opcode[3];
  21903. if (siz>0) {
  21904. if (ind>=nb_args) std::memset(&_mp_arg(1) + 1,0,siz*sizeof(double));
  21905. else std::memcpy(&_mp_arg(1) + 1,&_mp_arg(ind + 4) + 1,siz*sizeof(double));
  21906. return cimg::type<double>::nan();
  21907. }
  21908. if (ind>=nb_args) return 0;
  21909. return _mp_arg(ind + 4);
  21910. }
  21911. static double mp_argkth(_cimg_math_parser& mp) {
  21912. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21913. const double val = mp_kth(mp);
  21914. for (unsigned int i = 4; i<i_end; ++i) if (val==_mp_arg(i)) return i - 3.;
  21915. return 1.;
  21916. }
  21917. static double mp_argmin(_cimg_math_parser& mp) {
  21918. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21919. double val = _mp_arg(3);
  21920. unsigned int argval = 0;
  21921. for (unsigned int i = 4; i<i_end; ++i) {
  21922. const double _val = _mp_arg(i);
  21923. if (_val<val) { val = _val; argval = i - 3; }
  21924. }
  21925. return (double)argval;
  21926. }
  21927. static double mp_argminabs(_cimg_math_parser& mp) {
  21928. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21929. double val = _mp_arg(3), absval = cimg::abs(val);
  21930. unsigned int argval = 0;
  21931. for (unsigned int i = 4; i<i_end; ++i) {
  21932. const double _val = _mp_arg(i), _absval = cimg::abs(_val);
  21933. if (_absval<absval) { val = _val; absval = _absval; argval = i - 3; }
  21934. }
  21935. return (double)argval;
  21936. }
  21937. static double mp_argmax(_cimg_math_parser& mp) {
  21938. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21939. double val = _mp_arg(3);
  21940. unsigned int argval = 0;
  21941. for (unsigned int i = 4; i<i_end; ++i) {
  21942. const double _val = _mp_arg(i);
  21943. if (_val>val) { val = _val; argval = i - 3; }
  21944. }
  21945. return (double)argval;
  21946. }
  21947. static double mp_argmaxabs(_cimg_math_parser& mp) {
  21948. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21949. double val = _mp_arg(3), absval = cimg::abs(val);
  21950. unsigned int argval = 0;
  21951. for (unsigned int i = 4; i<i_end; ++i) {
  21952. const double _val = _mp_arg(i), _absval = cimg::abs(_val);
  21953. if (_absval>absval) { val = _val; absval = _absval; argval = i - 3; }
  21954. }
  21955. return (double)argval;
  21956. }
  21957. static double mp_asin(_cimg_math_parser& mp) {
  21958. return std::asin(_mp_arg(2));
  21959. }
  21960. static double mp_atan(_cimg_math_parser& mp) {
  21961. return std::atan(_mp_arg(2));
  21962. }
  21963. static double mp_atan2(_cimg_math_parser& mp) {
  21964. return std::atan2(_mp_arg(2),_mp_arg(3));
  21965. }
  21966. static double mp_avg(_cimg_math_parser& mp) {
  21967. const unsigned int i_end = (unsigned int)mp.opcode[2];
  21968. double val = _mp_arg(3);
  21969. for (unsigned int i = 4; i<i_end; ++i) val+=_mp_arg(i);
  21970. return val/(i_end - 3);
  21971. }
  21972. static double mp_bitwise_and(_cimg_math_parser& mp) {
  21973. return (double)((longT)_mp_arg(2) & (longT)_mp_arg(3));
  21974. }
  21975. static double mp_bitwise_left_shift(_cimg_math_parser& mp) {
  21976. return (double)((longT)_mp_arg(2)<<(unsigned int)_mp_arg(3));
  21977. }
  21978. static double mp_bitwise_not(_cimg_math_parser& mp) {
  21979. // Limit result to 32bits such that it can be entirely represented as a 'double'.
  21980. return (double)~(unsigned int)_mp_arg(2);
  21981. }
  21982. static double mp_bitwise_or(_cimg_math_parser& mp) {
  21983. return (double)((longT)_mp_arg(2) | (longT)_mp_arg(3));
  21984. }
  21985. static double mp_bitwise_right_shift(_cimg_math_parser& mp) {
  21986. return (double)((longT)_mp_arg(2)>>(unsigned int)_mp_arg(3));
  21987. }
  21988. static double mp_bitwise_xor(_cimg_math_parser& mp) {
  21989. return (double)((longT)_mp_arg(2) ^ (longT)_mp_arg(3));
  21990. }
  21991. static double mp_bool(_cimg_math_parser& mp) {
  21992. return (double)(bool)_mp_arg(2);
  21993. }
  21994. static double mp_break(_cimg_math_parser& mp) {
  21995. mp.break_type = 1;
  21996. mp.p_code = mp.p_break - 1;
  21997. return cimg::type<double>::nan();
  21998. }
  21999. static double mp_breakpoint(_cimg_math_parser& mp) {
  22000. cimg_abort_init;
  22001. cimg_abort_test;
  22002. cimg::unused(mp);
  22003. return cimg::type<double>::nan();
  22004. }
  22005. #ifdef cimg_mp_func_run
  22006. static double mp_run(_cimg_math_parser& mp) {
  22007. const unsigned int nb_args = (unsigned int)(mp.opcode[2] - 3)/2;
  22008. CImgList<charT> _str;
  22009. CImg<charT> it;
  22010. for (unsigned int n = 0; n<nb_args; ++n) {
  22011. const unsigned int siz = (unsigned int)mp.opcode[4 + 2*n];
  22012. if (siz) { // Vector argument -> string
  22013. const double *ptr = &_mp_arg(3 + 2*n) + 1;
  22014. unsigned int l = 0;
  22015. while (l<siz && ptr[l]) ++l;
  22016. CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
  22017. } else { // Scalar argument -> number
  22018. it.assign(24);
  22019. cimg_snprintf(it,it._width,"%.17g",_mp_arg(3 + 2*n));
  22020. CImg<charT>::string(it,false,true).move_to(_str);
  22021. }
  22022. }
  22023. CImg(1,1,1,1,0).move_to(_str);
  22024. CImg<charT> str = _str>'x';
  22025. cimg_mp_func_run(str._data);
  22026. return cimg::type<double>::nan();
  22027. }
  22028. #endif
  22029. static double mp_cbrt(_cimg_math_parser& mp) {
  22030. return cimg::cbrt(_mp_arg(2));
  22031. }
  22032. static double mp_ceil(_cimg_math_parser& mp) {
  22033. return std::ceil(_mp_arg(2));
  22034. }
  22035. static double mp_complex_abs(_cimg_math_parser& mp) {
  22036. return cimg::_hypot(_mp_arg(2),_mp_arg(3));
  22037. }
  22038. static double mp_complex_conj(_cimg_math_parser& mp) {
  22039. const double real = _mp_arg(2), imag = _mp_arg(3);
  22040. double *ptrd = &_mp_arg(1) + 1;
  22041. ptrd[0] = real;
  22042. ptrd[1] = -imag;
  22043. return cimg::type<double>::nan();
  22044. }
  22045. static double mp_complex_div_sv(_cimg_math_parser& mp) {
  22046. const double
  22047. *ptr2 = &_mp_arg(3) + 1,
  22048. r1 = _mp_arg(2),
  22049. r2 = *(ptr2++), i2 = *ptr2;
  22050. double *ptrd = &_mp_arg(1) + 1;
  22051. const double denom = r2*r2 + i2*i2;
  22052. *(ptrd++) = r1*r2/denom;
  22053. *ptrd = -r1*i2/denom;
  22054. return cimg::type<double>::nan();
  22055. }
  22056. static double mp_complex_div_vv(_cimg_math_parser& mp) {
  22057. const double
  22058. *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1,
  22059. r1 = *(ptr1++), i1 = *ptr1,
  22060. r2 = *(ptr2++), i2 = *ptr2;
  22061. double *ptrd = &_mp_arg(1) + 1;
  22062. const double denom = r2*r2 + i2*i2;
  22063. *(ptrd++) = (r1*r2 + i1*i2)/denom;
  22064. *ptrd = (r2*i1 - r1*i2)/denom;
  22065. return cimg::type<double>::nan();
  22066. }
  22067. static double mp_complex_exp(_cimg_math_parser& mp) {
  22068. const double real = _mp_arg(2), imag = _mp_arg(3), exp_real = std::exp(real);
  22069. double *ptrd = &_mp_arg(1) + 1;
  22070. ptrd[0] = exp_real*std::cos(imag);
  22071. ptrd[1] = exp_real*std::sin(imag);
  22072. return cimg::type<double>::nan();
  22073. }
  22074. static double mp_complex_log(_cimg_math_parser& mp) {
  22075. const double real = _mp_arg(2), imag = _mp_arg(3);
  22076. double *ptrd = &_mp_arg(1) + 1;
  22077. ptrd[0] = 0.5*std::log(real*real + imag*imag);
  22078. ptrd[1] = std::atan2(imag,real);
  22079. return cimg::type<double>::nan();
  22080. }
  22081. static double mp_complex_mul(_cimg_math_parser& mp) {
  22082. const double
  22083. *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1,
  22084. r1 = *(ptr1++), i1 = *ptr1,
  22085. r2 = *(ptr2++), i2 = *ptr2;
  22086. double *ptrd = &_mp_arg(1) + 1;
  22087. *(ptrd++) = r1*r2 - i1*i2;
  22088. *(ptrd++) = r1*i2 + r2*i1;
  22089. return cimg::type<double>::nan();
  22090. }
  22091. static void _mp_complex_pow(const double r1, const double i1,
  22092. const double r2, const double i2,
  22093. double *ptrd) {
  22094. double ro, io;
  22095. if (cimg::abs(i2)<1e-15) { // Exponent is real
  22096. if (cimg::abs(r1)<1e-15 && cimg::abs(i1)<1e-15) {
  22097. if (cimg::abs(r2)<1e-15) { ro = 1; io = 0; }
  22098. else ro = io = 0;
  22099. } else {
  22100. const double
  22101. mod1_2 = r1*r1 + i1*i1,
  22102. phi1 = std::atan2(i1,r1),
  22103. modo = std::pow(mod1_2,0.5*r2),
  22104. phio = r2*phi1;
  22105. ro = modo*std::cos(phio);
  22106. io = modo*std::sin(phio);
  22107. }
  22108. } else { // Exponent is complex
  22109. if (cimg::abs(r1)<1e-15 && cimg::abs(i1)<1e-15) ro = io = 0;
  22110. const double
  22111. mod1_2 = r1*r1 + i1*i1,
  22112. phi1 = std::atan2(i1,r1),
  22113. modo = std::pow(mod1_2,0.5*r2)*std::exp(-i2*phi1),
  22114. phio = r2*phi1 + 0.5*i2*std::log(mod1_2);
  22115. ro = modo*std::cos(phio);
  22116. io = modo*std::sin(phio);
  22117. }
  22118. *(ptrd++) = ro;
  22119. *ptrd = io;
  22120. }
  22121. static double mp_complex_pow_ss(_cimg_math_parser& mp) {
  22122. const double val1 = _mp_arg(2), val2 = _mp_arg(3);
  22123. double *ptrd = &_mp_arg(1) + 1;
  22124. _mp_complex_pow(val1,0,val2,0,ptrd);
  22125. return cimg::type<double>::nan();
  22126. }
  22127. static double mp_complex_pow_sv(_cimg_math_parser& mp) {
  22128. const double val1 = _mp_arg(2), *ptr2 = &_mp_arg(3) + 1;
  22129. double *ptrd = &_mp_arg(1) + 1;
  22130. _mp_complex_pow(val1,0,ptr2[0],ptr2[1],ptrd);
  22131. return cimg::type<double>::nan();
  22132. }
  22133. static double mp_complex_pow_vs(_cimg_math_parser& mp) {
  22134. const double *ptr1 = &_mp_arg(2) + 1, val2 = _mp_arg(3);
  22135. double *ptrd = &_mp_arg(1) + 1;
  22136. _mp_complex_pow(ptr1[0],ptr1[1],val2,0,ptrd);
  22137. return cimg::type<double>::nan();
  22138. }
  22139. static double mp_complex_pow_vv(_cimg_math_parser& mp) {
  22140. const double *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1;
  22141. double *ptrd = &_mp_arg(1) + 1;
  22142. _mp_complex_pow(ptr1[0],ptr1[1],ptr2[0],ptr2[1],ptrd);
  22143. return cimg::type<double>::nan();
  22144. }
  22145. static double mp_complex_cos(_cimg_math_parser& mp) {
  22146. const double real = _mp_arg(2), imag = _mp_arg(3);
  22147. double *ptrd = &_mp_arg(1) + 1;
  22148. ptrd[0] = std::cos(real)*std::cosh(imag);
  22149. ptrd[1] = -std::sin(real)*std::sinh(imag);
  22150. return cimg::type<double>::nan();
  22151. }
  22152. static double mp_complex_sin(_cimg_math_parser& mp) {
  22153. const double real = _mp_arg(2), imag = _mp_arg(3);
  22154. double *ptrd = &_mp_arg(1) + 1;
  22155. ptrd[0] = std::sin(real)*std::cosh(imag);
  22156. ptrd[1] = std::cos(real)*std::sinh(imag);
  22157. return cimg::type<double>::nan();
  22158. }
  22159. static double mp_complex_tan(_cimg_math_parser& mp) {
  22160. const double real = _mp_arg(2), imag = _mp_arg(3), denom = std::cos(2*real) + std::cosh(2*imag);
  22161. double *ptrd = &_mp_arg(1) + 1;
  22162. ptrd[0] = std::sin(2*real)/denom;
  22163. ptrd[1] = std::sinh(2*imag)/denom;
  22164. return cimg::type<double>::nan();
  22165. }
  22166. static double mp_complex_cosh(_cimg_math_parser& mp) {
  22167. const double real = _mp_arg(2), imag = _mp_arg(3);
  22168. double *ptrd = &_mp_arg(1) + 1;
  22169. ptrd[0] = std::cosh(real)*std::cos(imag);
  22170. ptrd[1] = std::sinh(real)*std::sin(imag);
  22171. return cimg::type<double>::nan();
  22172. }
  22173. static double mp_complex_sinh(_cimg_math_parser& mp) {
  22174. const double real = _mp_arg(2), imag = _mp_arg(3);
  22175. double *ptrd = &_mp_arg(1) + 1;
  22176. ptrd[0] = std::sinh(real)*std::cos(imag);
  22177. ptrd[1] = std::cosh(real)*std::sin(imag);
  22178. return cimg::type<double>::nan();
  22179. }
  22180. static double mp_complex_tanh(_cimg_math_parser& mp) {
  22181. const double real = _mp_arg(2), imag = _mp_arg(3), denom = std::cosh(2*real) + std::cos(2*imag);
  22182. double *ptrd = &_mp_arg(1) + 1;
  22183. ptrd[0] = std::sinh(2*real)/denom;
  22184. ptrd[1] = std::sin(2*imag)/denom;
  22185. return cimg::type<double>::nan();
  22186. }
  22187. static double mp_continue(_cimg_math_parser& mp) {
  22188. mp.break_type = 2;
  22189. mp.p_code = mp.p_break - 1;
  22190. return cimg::type<double>::nan();
  22191. }
  22192. static double mp_convolve(_cimg_math_parser &mp) {
  22193. return _mp_correlate(mp,true);
  22194. }
  22195. static double mp_copy(_cimg_math_parser& mp) {
  22196. return _mp_arg(2);
  22197. }
  22198. static double mp_correlate(_cimg_math_parser &mp) {
  22199. return _mp_correlate(mp,false);
  22200. }
  22201. static double _mp_correlate(_cimg_math_parser &mp, bool is_convolve) {
  22202. double *ptrd = &_mp_arg(1) + 1;
  22203. const double *const ptrA = &_mp_arg(2) + 1, *const ptrM = &_mp_arg(7) + 1;
  22204. const unsigned int
  22205. wA = (unsigned int)mp.opcode[3],
  22206. hA = (unsigned int)mp.opcode[4],
  22207. dA = (unsigned int)mp.opcode[5],
  22208. sA = (unsigned int)mp.opcode[6],
  22209. wM = (unsigned int)mp.opcode[8],
  22210. hM = (unsigned int)mp.opcode[9],
  22211. dM = (unsigned int)mp.opcode[10],
  22212. sM = (unsigned int)mp.opcode[11],
  22213. boundary_conditions = (unsigned int)_mp_arg(12),
  22214. channel_mode = (unsigned int)mp.opcode[14];
  22215. const bool
  22216. is_normalized = (bool)_mp_arg(13),
  22217. interpolation_type = (bool)_mp_arg(30);
  22218. const int
  22219. xcenter = mp.opcode[15]!=~0U?(int)_mp_arg(15):(int)(~0U>>1),
  22220. ycenter = mp.opcode[16]!=~0U?(int)_mp_arg(16):(int)(~0U>>1),
  22221. zcenter = mp.opcode[17]!=~0U?(int)_mp_arg(17):(int)(~0U>>1),
  22222. xstart = (int)mp.opcode[18],
  22223. ystart = (int)mp.opcode[19],
  22224. zstart = (int)mp.opcode[20],
  22225. xend = (int)mp.opcode[21],
  22226. yend = (int)mp.opcode[22],
  22227. zend = (int)mp.opcode[23];
  22228. const float
  22229. xstride = (float)_mp_arg(24),
  22230. ystride = (float)_mp_arg(25),
  22231. zstride = (float)_mp_arg(26),
  22232. xdilation = (float)_mp_arg(27),
  22233. ydilation = (float)_mp_arg(28),
  22234. zdilation = (float)_mp_arg(29);
  22235. CImg<doubleT> res;
  22236. if (is_convolve) res = CImg<doubleT>(ptrA,wA,hA,dA,sA,true).
  22237. get_convolve(CImg<doubleT>(ptrM,wM,hM,dM,sM,true),
  22238. boundary_conditions,is_normalized,channel_mode,
  22239. xcenter,ycenter,zcenter,
  22240. xstart,ystart,zstart,
  22241. xend,yend,zend,
  22242. xstride,ystride,zstride,
  22243. xdilation,ydilation,zdilation,
  22244. interpolation_type);
  22245. else res = CImg<doubleT>(ptrA,wA,hA,dA,sA,true).
  22246. get_correlate(CImg<doubleT>(ptrM,wM,hM,dM,sM,true),
  22247. boundary_conditions,is_normalized,channel_mode,
  22248. xcenter,ycenter,zcenter,
  22249. xstart,ystart,zstart,
  22250. xend,yend,zend,
  22251. xstride,ystride,zstride,
  22252. xdilation,ydilation,zdilation,
  22253. interpolation_type);
  22254. CImg<doubleT>(ptrd,res._width,res._height,res._depth,res._spectrum,true) = res;
  22255. return cimg::type<double>::nan();
  22256. }
  22257. static double mp_cos(_cimg_math_parser& mp) {
  22258. return std::cos(_mp_arg(2));
  22259. }
  22260. static double mp_cosh(_cimg_math_parser& mp) {
  22261. return std::cosh(_mp_arg(2));
  22262. }
  22263. static double mp_critical(_cimg_math_parser& mp) {
  22264. const ulongT g_target = mp.opcode[1];
  22265. cimg_pragma_openmp(critical(mp_critical))
  22266. {
  22267. for (const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[2];
  22268. mp.p_code<p_end; ++mp.p_code) { // Evaluate body
  22269. mp.opcode._data = mp.p_code->_data;
  22270. const ulongT target = mp.opcode[1];
  22271. mp.mem[target] = _cimg_mp_defunc(mp);
  22272. }
  22273. }
  22274. --mp.p_code;
  22275. return mp.mem[g_target];
  22276. }
  22277. static double mp_crop(_cimg_math_parser& mp) {
  22278. double *ptrd = &_mp_arg(1) + 1;
  22279. const int x = (int)_mp_arg(3), y = (int)_mp_arg(4), z = (int)_mp_arg(5), c = (int)_mp_arg(6);
  22280. const unsigned int
  22281. dx = (unsigned int)mp.opcode[7],
  22282. dy = (unsigned int)mp.opcode[8],
  22283. dz = (unsigned int)mp.opcode[9],
  22284. dc = (unsigned int)mp.opcode[10];
  22285. const unsigned int boundary_conditions = (unsigned int)_mp_arg(11);
  22286. unsigned int ind = (unsigned int)mp.opcode[2];
  22287. if (ind!=~0U) ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  22288. const CImg<T> &img = ind==~0U?mp.imgin:mp.imglist[ind];
  22289. if (!img) std::memset(ptrd,0,dx*dy*dz*dc*sizeof(double));
  22290. else CImg<doubleT>(ptrd,dx,dy,dz,dc,true) = img.get_crop(x,y,z,c,
  22291. x + dx - 1,y + dy - 1,
  22292. z + dz - 1,c + dc - 1,
  22293. boundary_conditions);
  22294. return cimg::type<double>::nan();
  22295. }
  22296. static double mp_cross(_cimg_math_parser& mp) {
  22297. CImg<doubleT>
  22298. vout(&_mp_arg(1) + 1,1,3,1,1,true),
  22299. v1(&_mp_arg(2) + 1,1,3,1,1,true),
  22300. v2(&_mp_arg(3) + 1,1,3,1,1,true);
  22301. (vout = v1).cross(v2);
  22302. return cimg::type<double>::nan();
  22303. }
  22304. static double mp_cut(_cimg_math_parser& mp) {
  22305. double val = _mp_arg(2), cmin = _mp_arg(3), cmax = _mp_arg(4);
  22306. return val<cmin?cmin:val>cmax?cmax:val;
  22307. }
  22308. static double mp_da_back_or_pop(_cimg_math_parser& mp) {
  22309. const bool is_pop = (bool)mp.opcode[4];
  22310. const char *const s_op = is_pop?"da_pop":"da_back";
  22311. mp_check_list(mp,s_op);
  22312. const unsigned int
  22313. dim = (unsigned int)mp.opcode[2],
  22314. ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
  22315. double *const ptrd = &_mp_arg(1) + (dim>1?1:0);
  22316. CImg<T> &img = mp.imglist[ind];
  22317. int siz = img?(int)img[img._height - 1]:0;
  22318. if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
  22319. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
  22320. "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
  22321. mp.imgout.pixel_type(),s_op,img.width(),img.height(),img.depth(),img.spectrum(),
  22322. img._width==1 && img._depth==1?"":" (contains invalid element counter)");
  22323. if (!siz)
  22324. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
  22325. "Specified dynamic array #%d contains no elements.",
  22326. mp.imgout.pixel_type(),s_op,(int)_mp_arg(3));
  22327. double ret = cimg::type<double>::nan();
  22328. if (dim<1) ret = img[siz - 1]; // Scalar element
  22329. else cimg_forC(img,c) ptrd[c] = img(0,siz - 1,0,c); // Vector element
  22330. if (is_pop) { // Remove element from array
  22331. --siz;
  22332. if (img.height()>32 && siz<2*img.height()/3) // Reduce size of dynamic array
  22333. img.resize(1,std::max(2*siz + 1,32),1,-100,0);
  22334. img[img._height - 1] = (T)siz;
  22335. }
  22336. return ret;
  22337. }
  22338. static double mp_da_insert_or_push(_cimg_math_parser& mp) {
  22339. const char *const s_op = mp.opcode[3]==~0U?"da_push":"da_insert";
  22340. mp_check_list(mp,s_op);
  22341. const unsigned int
  22342. dim = (unsigned int)mp.opcode[4],
  22343. _dim = std::max(1U,dim),
  22344. nb_elts = (unsigned int)mp.opcode[5] - 6,
  22345. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  22346. CImg<T> &img = mp.imglist[ind];
  22347. const int
  22348. siz = img?(int)img[img._height - 1]:0,
  22349. pos0 = mp.opcode[3]==~0U?siz:(int)_mp_arg(3),
  22350. pos = pos0<0?pos0 + siz:pos0;
  22351. if (img && _dim!=img._spectrum)
  22352. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
  22353. "Element to insert has invalid size %u (should be %u).",
  22354. mp.imgout.pixel_type(),s_op,_dim,img._spectrum);
  22355. if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
  22356. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
  22357. "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
  22358. mp.imgout.pixel_type(),s_op,img.width(),img.height(),img.depth(),img.spectrum(),
  22359. img._width==1 && img._depth==1?"":" (contains invalid element counter)");
  22360. if (pos<0 || pos>siz)
  22361. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
  22362. "Invalid position %d (not in range -%d...%d).",
  22363. mp.imgout.pixel_type(),s_op,pos0,siz,siz);
  22364. if (siz + nb_elts + 1>=img._height) // Increase size of dynamic array, if necessary
  22365. img.resize(1,2*siz + nb_elts + 1,1,_dim,0);
  22366. if (pos!=siz) // Move existing data in dynamic array
  22367. cimg_forC(img,c) std::memmove(img.data(0,pos + nb_elts,0,c),img.data(0,pos,0,c),(siz - pos)*sizeof(T));
  22368. if (!dim) // Scalar or vector1() elements
  22369. for (unsigned int k = 0; k<nb_elts; ++k) img[pos + k] = (T)_mp_arg(6 + k);
  22370. else // vectorN() elements, with N>1
  22371. for (unsigned int k = 0; k<nb_elts; ++k) {
  22372. double *ptrs = &_mp_arg(6 + k) + 1;
  22373. cimg_forC(img,c) img(0,pos + k,0,c) = ptrs[c];
  22374. }
  22375. img[img._height - 1] = (T)(siz + nb_elts);
  22376. return cimg::type<double>::nan();
  22377. }
  22378. static double mp_da_remove(_cimg_math_parser& mp) {
  22379. mp_check_list(mp,"da_remove");
  22380. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  22381. CImg<T> &img = mp.imglist[ind];
  22382. int siz = img?(int)img[img._height - 1]:0;
  22383. if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
  22384. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
  22385. "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
  22386. mp.imgout.pixel_type(),img.width(),img.height(),img.depth(),img.spectrum(),
  22387. img._width==1 && img._depth==1?"":" (contains invalid element counter)");
  22388. if (img._height<2)
  22389. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
  22390. "Dynamic array is empty.",
  22391. mp.imgout.pixel_type());
  22392. int
  22393. start0 = mp.opcode[3]==~0U?siz - 1:_mp_arg(3),
  22394. end0 = mp.opcode[4]==~0U?start0:_mp_arg(4),
  22395. start = start0<0?start0 + siz:start0,
  22396. end = end0<0?end0 + siz:end0;
  22397. if (start<0 || start>=siz || end<0 || end>=siz || start>end)
  22398. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
  22399. "Invalid starting (%d) and ending (%d) positions "
  22400. "(not ordered, in range -%d...%d).",
  22401. mp.imgout.pixel_type(),start0,end0,siz,siz - 1);
  22402. if (end<siz - 1) // Move remaining data in dynamic array
  22403. cimg_forC(img,c) std::memmove(img.data(0,start,0,c),img.data(0,end + 1,0,c),(siz - 1 - end)*sizeof(T));
  22404. siz-=end - start + 1;
  22405. if (img.height()>32 && siz<2*img.height()/3) // Reduce size of dynamic array
  22406. img.resize(1,std::max(2*siz + 1,32),1,-100,0);
  22407. img[img._height - 1] = (T)siz;
  22408. return cimg::type<double>::nan();
  22409. }
  22410. static double mp_da_size(_cimg_math_parser& mp) {
  22411. mp_check_list(mp,"da_size");
  22412. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  22413. CImg<T> &img = mp.imglist[ind];
  22414. const int siz = img?(int)img[img._height - 1]:0;
  22415. if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
  22416. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_size()': "
  22417. "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
  22418. mp.imgout.pixel_type(),img.width(),img.height(),img.depth(),img.spectrum(),
  22419. img._width==1 && img._depth==1?"":" (contains invalid element counter)");
  22420. return siz;
  22421. }
  22422. static double mp_date(_cimg_math_parser& mp) {
  22423. const unsigned int
  22424. siz_out = (unsigned int)mp.opcode[2],
  22425. siz_arg1 = (unsigned int)mp.opcode[4],
  22426. siz_arg2 = (unsigned int)mp.opcode[6];
  22427. double *ptr_out = &_mp_arg(1) + (siz_out?1:0);
  22428. const double
  22429. *ptr_arg1 = siz_arg1==~0U?0:&_mp_arg(3) + (siz_arg1?1:0),
  22430. *ptr_arg2 = siz_arg2==~0U?0:&_mp_arg(5) + 1;
  22431. if (!ptr_arg2) { // No filename specified
  22432. if (!siz_arg1) return cimg::date((unsigned int)*ptr_arg1);
  22433. if (siz_arg1==~0U) for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = k;
  22434. else for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = ptr_arg1[k];
  22435. cimg::date(ptr_out,siz_out);
  22436. return cimg::type<double>::nan();
  22437. }
  22438. // Filename specified.
  22439. CImg<charT> ss(siz_arg2 + 1);
  22440. cimg_forX(ss,i) ss[i] = (char)ptr_arg2[i];
  22441. ss.back() = 0;
  22442. if (!siz_arg1) return cimg::fdate(ss,(unsigned int)*ptr_arg1);
  22443. for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = ptr_arg1[k];
  22444. cimg::fdate(ss,ptr_out,siz_out);
  22445. return cimg::type<double>::nan();
  22446. }
  22447. static double mp_debug(_cimg_math_parser& mp) {
  22448. CImg<charT> expr(mp.opcode[2] - 4);
  22449. {
  22450. const ulongT *ptrs = mp.opcode._data + 4;
  22451. cimg_for(expr,ptrd,char) *ptrd = (char)*(ptrs++);
  22452. }
  22453. cimg::strellipsize(expr);
  22454. const ulongT g_target = mp.opcode[1];
  22455. #if cimg_use_openmp==0
  22456. const unsigned int n_thread = 0;
  22457. #else
  22458. const unsigned int n_thread = omp_get_thread_num();
  22459. #endif
  22460. cimg_pragma_openmp(critical(mp_debug))
  22461. {
  22462. std::fprintf(cimg::output(),
  22463. "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
  22464. "Start debugging expression '%s', code length %u -> mem[%u] (memsize: %u)",
  22465. (void*)&mp,n_thread,mp.debug_indent,' ',
  22466. expr._data,(unsigned int)mp.opcode[3],(unsigned int)g_target,mp.mem._width);
  22467. std::fflush(cimg::output());
  22468. mp.debug_indent+=3;
  22469. }
  22470. const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[3];
  22471. CImg<ulongT> _op;
  22472. for ( ; mp.p_code<p_end; ++mp.p_code) {
  22473. const CImg<ulongT> &op = *mp.p_code;
  22474. mp.opcode._data = op._data;
  22475. _op.assign(1,op._height - 1);
  22476. const ulongT *ptrs = op._data + 1;
  22477. for (ulongT *ptrd = _op._data, *const ptrde = _op._data + _op._height; ptrd<ptrde; ++ptrd)
  22478. *ptrd = *(ptrs++);
  22479. const ulongT target = mp.opcode[1];
  22480. mp.mem[target] = _cimg_mp_defunc(mp);
  22481. cimg_pragma_openmp(critical(mp_debug))
  22482. {
  22483. std::fprintf(cimg::output(),
  22484. "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
  22485. "Opcode %p = [ %p,%s ] -> mem[%u] = %.17g",
  22486. (void*)&mp,n_thread,mp.debug_indent,' ',
  22487. (void*)mp.opcode._data,(void*)*mp.opcode,_op.value_string().data(),
  22488. (unsigned int)target,mp.mem[target]);
  22489. std::fflush(cimg::output());
  22490. }
  22491. }
  22492. cimg_pragma_openmp(critical(mp_debug))
  22493. {
  22494. mp.debug_indent-=3;
  22495. std::fprintf(cimg::output(),
  22496. "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
  22497. "End debugging expression '%s' -> mem[%u] = %.17g (memsize: %u)",
  22498. (void*)&mp,n_thread,mp.debug_indent,' ',
  22499. expr._data,(unsigned int)g_target,mp.mem[g_target],mp.mem._width);
  22500. std::fflush(cimg::output());
  22501. }
  22502. --mp.p_code;
  22503. return mp.mem[g_target];
  22504. }
  22505. static double mp_decrement(_cimg_math_parser& mp) {
  22506. return _mp_arg(2) - 1;
  22507. }
  22508. static double mp_deg2rad(_cimg_math_parser& mp) {
  22509. return _mp_arg(2)*cimg::PI/180;
  22510. }
  22511. static double mp_det(_cimg_math_parser& mp) {
  22512. const double *ptrs = &_mp_arg(2) + 1;
  22513. const unsigned int k = (unsigned int)mp.opcode[3];
  22514. return CImg<doubleT>(ptrs,k,k,1,1,true).det();
  22515. }
  22516. static double mp_diag(_cimg_math_parser& mp) {
  22517. const unsigned int i_end = (unsigned int)mp.opcode[2], siz = mp.opcode[2] - 3;
  22518. double *ptrd = &_mp_arg(1) + 1;
  22519. std::memset(ptrd,0,siz*siz*sizeof(double));
  22520. for (unsigned int i = 3; i<i_end; ++i) { *(ptrd++) = _mp_arg(i); ptrd+=siz; }
  22521. return cimg::type<double>::nan();
  22522. }
  22523. static double mp_display_memory(_cimg_math_parser& mp) {
  22524. cimg::unused(mp);
  22525. std::fputc('\n',cimg::output());
  22526. CImg<charT> title(128);
  22527. cimg_snprintf(title,title._width,"%s (%u)","[" cimg_appname "_math_parser] Memory snapshot",mp.mem._width);
  22528. mp.mem.display(title);
  22529. return cimg::type<double>::nan();
  22530. }
  22531. static double mp_display(_cimg_math_parser& mp) {
  22532. const unsigned int
  22533. _siz = (unsigned int)mp.opcode[3],
  22534. siz = _siz?_siz:1;
  22535. const double *const ptr = &_mp_arg(1) + (_siz?1:0);
  22536. const int
  22537. w = (int)_mp_arg(4),
  22538. h = (int)_mp_arg(5),
  22539. d = (int)_mp_arg(6),
  22540. s = (int)_mp_arg(7);
  22541. CImg<doubleT> img;
  22542. if (w>0 && h>0 && d>0 && s>0) {
  22543. if ((unsigned int)w*h*d*s<=siz) img.assign(ptr,w,h,d,s,true);
  22544. else img.assign(ptr,siz).resize(w,h,d,s,-1);
  22545. } else img.assign(ptr,1,siz,1,1,true);
  22546. CImg<charT> expr(mp.opcode[2] - 8);
  22547. const ulongT *ptrs = mp.opcode._data + 8;
  22548. cimg_for(expr,ptrd,char) *ptrd = (char)*(ptrs++);
  22549. ((CImg<charT>::string("[" cimg_appname "_math_parser] ",false,true),expr)>'x').move_to(expr);
  22550. cimg::strellipsize(expr);
  22551. std::fputc('\n',cimg::output());
  22552. img.display(expr._data);
  22553. return cimg::type<double>::nan();
  22554. }
  22555. static double mp_div(_cimg_math_parser& mp) {
  22556. return _mp_arg(2)/_mp_arg(3);
  22557. }
  22558. static double mp_dot(_cimg_math_parser& mp) {
  22559. const unsigned int siz = (unsigned int)mp.opcode[4];
  22560. return CImg<doubleT>(&_mp_arg(2) + 1,1,siz,1,1,true).
  22561. dot(CImg<doubleT>(&_mp_arg(3) + 1,1,siz,1,1,true));
  22562. }
  22563. static double mp_do(_cimg_math_parser& mp) {
  22564. const ulongT
  22565. mem_body = mp.opcode[1],
  22566. mem_cond = mp.opcode[2];
  22567. const CImg<ulongT>
  22568. *const p_body = ++mp.p_code,
  22569. *const p_cond = p_body + mp.opcode[3],
  22570. *const p_end = p_cond + mp.opcode[4];
  22571. const unsigned int vsiz = (unsigned int)mp.opcode[5];
  22572. if (mp.opcode[6]) { // Set default value for result and condition if necessary
  22573. if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
  22574. else mp.mem[mem_body] = cimg::type<double>::nan();
  22575. }
  22576. if (mp.opcode[7]) mp.mem[mem_cond] = 0;
  22577. const unsigned int _break_type = mp.break_type;
  22578. mp.break_type = 0;
  22579. do {
  22580. for (mp.p_code = p_body; mp.p_code<p_cond; ++mp.p_code) { // Evaluate body
  22581. mp.opcode._data = mp.p_code->_data;
  22582. const ulongT target = mp.opcode[1];
  22583. mp.mem[target] = _cimg_mp_defunc(mp);
  22584. }
  22585. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22586. for (mp.p_code = p_cond; mp.p_code<p_end; ++mp.p_code) { // Evaluate condition
  22587. mp.opcode._data = mp.p_code->_data;
  22588. const ulongT target = mp.opcode[1];
  22589. mp.mem[target] = _cimg_mp_defunc(mp);
  22590. }
  22591. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22592. } while (mp.mem[mem_cond]);
  22593. mp.break_type = _break_type;
  22594. mp.p_code = p_end - 1;
  22595. return mp.mem[mem_body];
  22596. }
  22597. static double mp_draw(_cimg_math_parser& mp) {
  22598. const int x = (int)_mp_arg(4), y = (int)_mp_arg(5), z = (int)_mp_arg(6), c = (int)_mp_arg(7);
  22599. unsigned int ind = (unsigned int)mp.opcode[3];
  22600. if (ind!=~0U) {
  22601. if (!mp.imglist.width()) return cimg::type<double>::nan();
  22602. ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
  22603. }
  22604. CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  22605. unsigned int
  22606. dx = (unsigned int)mp.opcode[8],
  22607. dy = (unsigned int)mp.opcode[9],
  22608. dz = (unsigned int)mp.opcode[10],
  22609. dc = (unsigned int)mp.opcode[11];
  22610. dx = dx==~0U?img._width:(unsigned int)_mp_arg(8);
  22611. dy = dy==~0U?img._height:(unsigned int)_mp_arg(9);
  22612. dz = dz==~0U?img._depth:(unsigned int)_mp_arg(10);
  22613. dc = dc==~0U?img._spectrum:(unsigned int)_mp_arg(11);
  22614. const ulongT sizS = mp.opcode[2];
  22615. if (sizS<(ulongT)dx*dy*dz*dc)
  22616. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'draw()': "
  22617. "Sprite dimension (%lu values) and specified sprite geometry (%u,%u,%u,%u) "
  22618. "(%lu values) do not match.",
  22619. mp.imgin.pixel_type(),sizS,dx,dy,dz,dc,(ulongT)dx*dy*dz*dc);
  22620. CImg<doubleT> S(&_mp_arg(1) + 1,dx,dy,dz,dc,true);
  22621. const float opacity = (float)_mp_arg(12);
  22622. if (img._data) {
  22623. if (mp.opcode[13]!=~0U) { // Opacity mask specified
  22624. const ulongT sizM = mp.opcode[14];
  22625. if (sizM<(ulongT)dx*dy*dz)
  22626. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'draw()': "
  22627. "Mask dimension (%lu values) and specified sprite geometry (%u,%u,%u,%u) "
  22628. "(%lu values) do not match.",
  22629. mp.imgin.pixel_type(),sizS,dx,dy,dz,dc,(ulongT)dx*dy*dz*dc);
  22630. const CImg<doubleT> M(&_mp_arg(13) + 1,dx,dy,dz,(unsigned int)(sizM/(dx*dy*dz)),true);
  22631. img.draw_image(x,y,z,c,S,M,opacity,(float)_mp_arg(15));
  22632. } else img.draw_image(x,y,z,c,S,opacity);
  22633. }
  22634. return cimg::type<double>::nan();
  22635. }
  22636. static double mp_echo(_cimg_math_parser& mp) {
  22637. const unsigned int nb_args = (unsigned int)(mp.opcode[2] - 3)/2;
  22638. if (!nb_args) { std::fputc('\n',cimg::output()); return cimg::type<double>::nan(); } // No arguments
  22639. CImgList<charT> _str;
  22640. CImg<charT> it;
  22641. for (unsigned int n = 0; n<nb_args; ++n) {
  22642. const unsigned int siz = (unsigned int)mp.opcode[4 + 2*n];
  22643. if (siz) { // Vector argument -> string
  22644. const double *ptr = &_mp_arg(3 + 2*n) + 1;
  22645. unsigned int l = 0;
  22646. while (l<siz && ptr[l]) ++l;
  22647. CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
  22648. } else { // Scalar argument -> number
  22649. it.assign(24);
  22650. cimg_snprintf(it,it._width,"%.17g",_mp_arg(3 + 2*n));
  22651. CImg<charT>::string(it,false,true).move_to(_str);
  22652. }
  22653. }
  22654. CImg(1,1,1,1,0).move_to(_str);
  22655. const CImg<charT> str = _str>'x';
  22656. std::fprintf(cimg::output(),"\n%s",str._data);
  22657. return cimg::type<double>::nan();
  22658. }
  22659. static double mp_ellipse(_cimg_math_parser& mp) {
  22660. mp_check_list(mp,"ellipse");
  22661. const unsigned int i_end = (unsigned int)mp.opcode[2];
  22662. unsigned int ind = (unsigned int)mp.opcode[3];
  22663. if (ind!=~0U) {
  22664. if (!mp.imglist.width()) return cimg::type<double>::nan();
  22665. ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
  22666. }
  22667. CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  22668. CImg<T> color(img._spectrum,1,1,1,0);
  22669. bool is_invalid_arguments = false, is_outlined = false;
  22670. float r1 = 0, r2 = 0, angle = 0, opacity = 1;
  22671. unsigned int i = 4, pattern = ~0U;
  22672. int x0 = 0, y0 = 0;
  22673. if (i>=i_end) is_invalid_arguments = true;
  22674. else {
  22675. x0 = (int)cimg::round(_mp_arg(i++));
  22676. if (i>=i_end) is_invalid_arguments = true;
  22677. else {
  22678. y0 = (int)cimg::round(_mp_arg(i++));
  22679. if (i>=i_end) is_invalid_arguments = true;
  22680. else {
  22681. r1 = (float)_mp_arg(i++);
  22682. if (i>=i_end) r2 = r1;
  22683. else {
  22684. r2 = (float)_mp_arg(i++);
  22685. if (i<i_end) {
  22686. angle = (float)(_mp_arg(i++)*180/cimg::PI);
  22687. if (i<i_end) {
  22688. opacity = (float)_mp_arg(i++);
  22689. if (r1<0 && r2<0) {
  22690. pattern = (unsigned int)_mp_arg(i++);
  22691. is_outlined = true;
  22692. r1 = -r1; r2 = -r2;
  22693. }
  22694. if (i<i_end) {
  22695. cimg_forX(color,k) if (i<i_end) color[k] = (T)_mp_arg(i++);
  22696. else { color.resize(k,1,1,1,-1); break; }
  22697. color.resize(img._spectrum,1,1,1,0,2);
  22698. }
  22699. }
  22700. }
  22701. }
  22702. }
  22703. }
  22704. }
  22705. if (!is_invalid_arguments) {
  22706. if (is_outlined) img.draw_ellipse(x0,y0,r1,r2,angle,color._data,opacity,pattern);
  22707. else img.draw_ellipse(x0,y0,r1,r2,angle,color._data,opacity);
  22708. } else {
  22709. CImg<doubleT> args(i_end - 4);
  22710. cimg_forX(args,k) args[k] = _mp_arg(4 + k);
  22711. if (ind==~0U)
  22712. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'ellipse()': "
  22713. "Invalid arguments '%s'. ",
  22714. mp.imgin.pixel_type(),args.value_string()._data);
  22715. else
  22716. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'ellipse()': "
  22717. "Invalid arguments '#%u%s%s'. ",
  22718. mp.imgin.pixel_type(),ind,args._width?",":"",args.value_string()._data);
  22719. }
  22720. return cimg::type<double>::nan();
  22721. }
  22722. static double mp_eq(_cimg_math_parser& mp) {
  22723. return (double)(_mp_arg(2)==_mp_arg(3));
  22724. }
  22725. static double mp_erf(_cimg_math_parser& mp) {
  22726. return std::erf(_mp_arg(2));
  22727. }
  22728. static double mp_erfinv(_cimg_math_parser& mp) {
  22729. return cimg::erfinv(_mp_arg(2));
  22730. }
  22731. static double mp_exp(_cimg_math_parser& mp) {
  22732. return std::exp(_mp_arg(2));
  22733. }
  22734. static double mp_expr(_cimg_math_parser& mp) {
  22735. const unsigned int
  22736. sizs = (unsigned int)mp.opcode[3],
  22737. w = (unsigned int)mp.opcode[4],
  22738. h = (unsigned int)mp.opcode[5],
  22739. d = (unsigned int)mp.opcode[6],
  22740. s = (unsigned int)mp.opcode[7],
  22741. sizd = w*h*d*s;
  22742. const double *ptrs = &_mp_arg(2) + 1;
  22743. double *ptrd = &_mp_arg(1);
  22744. CImg<charT> ss(sizs + 1);
  22745. cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptrs[i];
  22746. ss.back() = 0;
  22747. if (!sizd) return CImg<T>(w,h,d,s,0).eval(ss,0,0,0,0,&mp.imglist); // Scalar result
  22748. CImg<doubleT>(++ptrd,w,h,d,s,true) = CImg<T>(w,h,d,s,0).fill(ss,true,true,&mp.imglist);
  22749. return cimg::type<double>::nan();
  22750. }
  22751. static double mp_eye(_cimg_math_parser& mp) {
  22752. double *ptrd = &_mp_arg(1) + 1;
  22753. const unsigned int k = (unsigned int)mp.opcode[2];
  22754. CImg<doubleT>(ptrd,k,k,1,1,true).identity_matrix();
  22755. return cimg::type<double>::nan();
  22756. }
  22757. static double mp_f2ui(_cimg_math_parser& mp) {
  22758. return (double)cimg::float2uint((float)_mp_arg(2));
  22759. }
  22760. static double mp_factorial(_cimg_math_parser& mp) {
  22761. return cimg::factorial((int)_mp_arg(2));
  22762. }
  22763. static double mp_fibonacci(_cimg_math_parser& mp) {
  22764. return cimg::fibonacci((int)_mp_arg(2));
  22765. }
  22766. static double mp_fill(_cimg_math_parser& mp) {
  22767. unsigned int siz = (unsigned int)mp.opcode[2];
  22768. double
  22769. *ptrd = &_mp_arg(1),
  22770. *const ptrc = mp.opcode[3]!=~0U?&_mp_arg(3):0,
  22771. *const ptrs = &_mp_arg(4);
  22772. if (siz) ++ptrd; else ++siz; // Fill vector-valued slot
  22773. const CImg<ulongT>
  22774. *const p_body = ++mp.p_code,
  22775. *const p_end = p_body + mp.opcode[5];
  22776. const unsigned int _break_type = mp.break_type;
  22777. mp.break_type = 0;
  22778. unsigned int it = 0;
  22779. if (ptrc) { // Version with loop variable (3 arguments)
  22780. while (it<siz) {
  22781. *ptrc = (double)it;
  22782. for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
  22783. mp.opcode._data = mp.p_code->_data;
  22784. const ulongT target = mp.opcode[1];
  22785. mp.mem[target] = _cimg_mp_defunc(mp);
  22786. }
  22787. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22788. else ptrd[it] = *ptrs;
  22789. ++it;
  22790. }
  22791. *ptrc = (double)it;
  22792. } else // Version without loop variable (2 arguments)
  22793. while (it<siz) {
  22794. for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
  22795. mp.opcode._data = mp.p_code->_data;
  22796. const ulongT target = mp.opcode[1];
  22797. mp.mem[target] = _cimg_mp_defunc(mp);
  22798. }
  22799. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22800. else ptrd[it] = *ptrs;
  22801. ++it;
  22802. }
  22803. mp.break_type = _break_type;
  22804. mp.p_code = p_end - 1;
  22805. return *ptrd;
  22806. }
  22807. static double mp_find(_cimg_math_parser& mp) {
  22808. const int _step = (int)_mp_arg(6), step = _step?_step:-1;
  22809. const ulongT siz = (ulongT)mp.opcode[3];
  22810. longT ind = (longT)(mp.opcode[5]!=_cimg_mp_slot_nan?_mp_arg(5):step>0?0:siz - 1);
  22811. if (ind<0 || ind>=(longT)siz) return -1.;
  22812. const double
  22813. *const ptrb = &_mp_arg(2) + 1,
  22814. *const ptre = ptrb + siz,
  22815. val = _mp_arg(4),
  22816. *ptr = ptrb + ind;
  22817. // Forward search
  22818. if (step>0) {
  22819. while (ptr<ptre && *ptr!=val) ptr+=step;
  22820. return ptr>=ptre?-1.:(double)(ptr - ptrb);
  22821. }
  22822. // Backward search.
  22823. while (ptr>=ptrb && *ptr!=val) ptr+=step;
  22824. return ptr<ptrb?-1.:(double)(ptr - ptrb);
  22825. }
  22826. static double mp_find_seq(_cimg_math_parser& mp) {
  22827. const int _step = (int)_mp_arg(7), step = _step?_step:-1;
  22828. const ulongT
  22829. siz1 = (ulongT)mp.opcode[3],
  22830. siz2 = (ulongT)mp.opcode[5];
  22831. longT ind = (longT)(mp.opcode[6]!=_cimg_mp_slot_nan?_mp_arg(6):step>0?0:siz1 - 1);
  22832. if (ind<0 || ind>=(longT)siz1) return -1.;
  22833. const double
  22834. *const ptr1b = &_mp_arg(2) + 1,
  22835. *const ptr1e = ptr1b + siz1,
  22836. *const ptr2b = &_mp_arg(4) + 1,
  22837. *const ptr2e = ptr2b + siz2,
  22838. *ptr1 = ptr1b + ind,
  22839. *p1 = 0,
  22840. *p2 = 0;
  22841. // Forward search.
  22842. if (step>0) {
  22843. do {
  22844. while (ptr1<ptr1e && *ptr1!=*ptr2b) ptr1+=step;
  22845. if (ptr1>=ptr1e) return -1.;
  22846. p1 = ptr1 + 1;
  22847. p2 = ptr2b + 1;
  22848. while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
  22849. } while (p2<ptr2e && (ptr1+=step)<ptr1e);
  22850. return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
  22851. }
  22852. // Backward search.
  22853. do {
  22854. while (ptr1>=ptr1b && *ptr1!=*ptr2b) ptr1+=step;
  22855. if (ptr1<ptr1b) return -1.;
  22856. p1 = ptr1 + 1;
  22857. p2 = ptr2b + 1;
  22858. while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
  22859. } while (p2<ptr2e && (ptr1+=step)>=ptr1b);
  22860. return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
  22861. }
  22862. static double mp_floor(_cimg_math_parser& mp) {
  22863. return std::floor(_mp_arg(2));
  22864. }
  22865. static double mp_for(_cimg_math_parser& mp) {
  22866. const ulongT
  22867. mem_body = mp.opcode[1],
  22868. mem_cond = mp.opcode[3];
  22869. const CImg<ulongT>
  22870. *const p_init = ++mp.p_code,
  22871. *const p_cond = p_init + mp.opcode[4],
  22872. *const p_body = p_cond + mp.opcode[5],
  22873. *const p_post = p_body + mp.opcode[6],
  22874. *const p_end = p_post + mp.opcode[7];
  22875. const unsigned int vsiz = (unsigned int)mp.opcode[2];
  22876. bool is_cond = false;
  22877. if (mp.opcode[8]) { // Set default value for result and condition if necessary
  22878. if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
  22879. else mp.mem[mem_body] = cimg::type<double>::nan();
  22880. }
  22881. if (mp.opcode[9]) mp.mem[mem_cond] = 0;
  22882. const unsigned int _break_type = mp.break_type;
  22883. mp.break_type = 0;
  22884. for (mp.p_code = p_init; mp.p_code<p_cond; ++mp.p_code) { // Evaluate init
  22885. mp.opcode._data = mp.p_code->_data;
  22886. const ulongT target = mp.opcode[1];
  22887. mp.mem[target] = _cimg_mp_defunc(mp);
  22888. }
  22889. if (!mp.break_type) do {
  22890. for (mp.p_code = p_cond; mp.p_code<p_body; ++mp.p_code) { // Evaluate condition
  22891. mp.opcode._data = mp.p_code->_data;
  22892. const ulongT target = mp.opcode[1];
  22893. mp.mem[target] = _cimg_mp_defunc(mp);
  22894. }
  22895. if (mp.break_type==1) break;
  22896. is_cond = (bool)mp.mem[mem_cond];
  22897. if (is_cond && !mp.break_type) {
  22898. for (mp.p_code = p_body; mp.p_code<p_post; ++mp.p_code) { // Evaluate body
  22899. mp.opcode._data = mp.p_code->_data;
  22900. const ulongT target = mp.opcode[1];
  22901. mp.mem[target] = _cimg_mp_defunc(mp);
  22902. }
  22903. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22904. for (mp.p_code = p_post; mp.p_code<p_end; ++mp.p_code) { // Evaluate post-code
  22905. mp.opcode._data = mp.p_code->_data;
  22906. const ulongT target = mp.opcode[1];
  22907. mp.mem[target] = _cimg_mp_defunc(mp);
  22908. }
  22909. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  22910. }
  22911. } while (is_cond);
  22912. mp.break_type = _break_type;
  22913. mp.p_code = p_end - 1;
  22914. return mp.mem[mem_body];
  22915. }
  22916. static double mp_fsize(_cimg_math_parser& mp) {
  22917. const double *ptrs = &_mp_arg(2) + 1;
  22918. const ulongT siz = (ulongT)mp.opcode[3];
  22919. CImg<charT> ss(siz + 1);
  22920. cimg_forX(ss,i) ss[i] = (char)ptrs[i];
  22921. ss.back() = 0;
  22922. return (double)cimg::fsize(ss);
  22923. }
  22924. static double mp_g(_cimg_math_parser& mp) {
  22925. cimg::unused(mp);
  22926. return cimg::grand(&mp.rng);
  22927. }
  22928. static double mp_gauss(_cimg_math_parser& mp) {
  22929. const double x = _mp_arg(2), s = _mp_arg(3);
  22930. return std::exp(-x*x/(2*s*s))/(_mp_arg(4)?std::sqrt(2*s*s*cimg::PI):1);
  22931. }
  22932. #ifdef cimg_mp_func_get
  22933. static double mp_get(_cimg_math_parser& mp) {
  22934. const double *ptrs = &_mp_arg(2) + 1;
  22935. double *ptrd = &_mp_arg(1);
  22936. const unsigned int
  22937. sizs = (unsigned int)mp.opcode[3],
  22938. sizd = (unsigned int)mp.opcode[4];
  22939. const bool to_string = (bool)mp.opcode[5];
  22940. CImg<charT> ss(sizs + 1);
  22941. cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptrs[i];
  22942. ss.back() = 0;
  22943. if (sizd) cimg_mp_func_get(ptrd + 1,sizd,to_string,ss._data);
  22944. else cimg_mp_func_get(ptrd,0,to_string,ss._data);
  22945. return cimg::type<double>::nan();
  22946. }
  22947. #endif
  22948. static double mp_gcd(_cimg_math_parser& mp) {
  22949. return cimg::gcd((long)_mp_arg(2),(long)_mp_arg(3));
  22950. }
  22951. #ifdef cimg_mp_func_name
  22952. static double mp_name(_cimg_math_parser& mp) {
  22953. double *const ptr = &_mp_arg(1) + 1;
  22954. const unsigned int siz = (unsigned int)mp.opcode[3];
  22955. unsigned int ind = (unsigned int)mp.opcode[2];
  22956. if (ind==~0U) std::memset(ptr,0,siz*sizeof(double));
  22957. else {
  22958. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  22959. cimg_mp_func_name(ind,ptr,siz);
  22960. }
  22961. return cimg::type<double>::nan();
  22962. }
  22963. #endif
  22964. static double mp_gt(_cimg_math_parser& mp) {
  22965. return (double)(_mp_arg(2)>_mp_arg(3));
  22966. }
  22967. static double mp_gte(_cimg_math_parser& mp) {
  22968. return (double)(_mp_arg(2)>=_mp_arg(3));
  22969. }
  22970. static double mp_i(_cimg_math_parser& mp) {
  22971. return (double)mp.imgin.atXYZC((int)mp.mem[_cimg_mp_slot_x],(int)mp.mem[_cimg_mp_slot_y],
  22972. (int)mp.mem[_cimg_mp_slot_z],(int)mp.mem[_cimg_mp_slot_c],(T)0);
  22973. }
  22974. static double mp_if(_cimg_math_parser& mp) {
  22975. const bool is_cond = (bool)_mp_arg(2);
  22976. const ulongT
  22977. mem_left = mp.opcode[3],
  22978. mem_right = mp.opcode[4];
  22979. const CImg<ulongT>
  22980. *const p_right = ++mp.p_code + mp.opcode[5],
  22981. *const p_end = p_right + mp.opcode[6];
  22982. const unsigned int vtarget = (unsigned int)mp.opcode[1], vsiz = (unsigned int)mp.opcode[7];
  22983. if (is_cond) for ( ; mp.p_code<p_right; ++mp.p_code) {
  22984. mp.opcode._data = mp.p_code->_data;
  22985. const ulongT target = mp.opcode[1];
  22986. mp.mem[target] = _cimg_mp_defunc(mp);
  22987. }
  22988. else for (mp.p_code = p_right; mp.p_code<p_end; ++mp.p_code) {
  22989. mp.opcode._data = mp.p_code->_data;
  22990. const ulongT target = mp.opcode[1];
  22991. mp.mem[target] = _cimg_mp_defunc(mp);
  22992. }
  22993. if (mp.p_code==mp.p_break) --mp.p_code;
  22994. else mp.p_code = p_end - 1;
  22995. if (vsiz) std::memcpy(&mp.mem[vtarget] + 1,&mp.mem[is_cond?mem_left:mem_right] + 1,sizeof(double)*vsiz);
  22996. return mp.mem[is_cond?mem_left:mem_right];
  22997. }
  22998. static double mp_image_d(_cimg_math_parser& mp) {
  22999. unsigned int ind = (unsigned int)mp.opcode[2];
  23000. if (ind!=~0U) {
  23001. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23002. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23003. }
  23004. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23005. return (double)img.depth();
  23006. }
  23007. static double mp_image_display(_cimg_math_parser& mp) {
  23008. mp_check_list(mp,"display");
  23009. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23010. cimg::mutex(6);
  23011. CImg<T> &img = mp.imglist[ind];
  23012. CImg<charT> title(256);
  23013. std::fputc('\n',cimg::output());
  23014. cimg_snprintf(title,title._width,"[ Image #%u ]",ind);
  23015. img.display(title);
  23016. cimg::mutex(6,0);
  23017. return cimg::type<double>::nan();
  23018. }
  23019. static double mp_image_h(_cimg_math_parser& mp) {
  23020. unsigned int ind = (unsigned int)mp.opcode[2];
  23021. if (ind!=~0U) {
  23022. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23023. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23024. }
  23025. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23026. return (double)img.height();
  23027. }
  23028. static double mp_image_median(_cimg_math_parser& mp) {
  23029. unsigned int ind = (unsigned int)mp.opcode[2];
  23030. if (ind!=~0U) {
  23031. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23032. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23033. }
  23034. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23035. return (double)img.median();
  23036. }
  23037. static double mp_image_norm(_cimg_math_parser& mp) {
  23038. unsigned int ind = (unsigned int)mp.opcode[2];
  23039. if (ind!=~0U) {
  23040. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23041. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23042. }
  23043. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23044. return (double)img.magnitude();
  23045. }
  23046. static double mp_image_print(_cimg_math_parser& mp) {
  23047. mp_check_list(mp,"print");
  23048. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23049. cimg::mutex(6);
  23050. CImg<T> &img = mp.imglist[ind];
  23051. CImg<charT> title(256);
  23052. std::fputc('\n',cimg::output());
  23053. cimg_snprintf(title,title._width,"[ Image #%u ]",ind);
  23054. img.print(title);
  23055. cimg::mutex(6,0);
  23056. return cimg::type<double>::nan();
  23057. }
  23058. static double mp_image_resize(_cimg_math_parser& mp) {
  23059. mp_check_list(mp,"resize");
  23060. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23061. cimg::mutex(6);
  23062. CImg<T> &img = mp.imglist[ind];
  23063. const double
  23064. _w = mp.opcode[3]==~0U?-100:_mp_arg(3),
  23065. _h = mp.opcode[4]==~0U?-100:_mp_arg(4),
  23066. _d = mp.opcode[5]==~0U?-100:_mp_arg(5),
  23067. _s = mp.opcode[6]==~0U?-100:_mp_arg(6);
  23068. const unsigned int
  23069. w = (unsigned int)(_w>=0?_w:-_w*img.width()/100),
  23070. h = (unsigned int)(_h>=0?_h:-_h*img.height()/100),
  23071. d = (unsigned int)(_d>=0?_d:-_d*img.depth()/100),
  23072. s = (unsigned int)(_s>=0?_s:-_s*img.spectrum()/100),
  23073. interp = (int)_mp_arg(7);
  23074. if (mp.is_fill && img._data==mp.imgout._data) {
  23075. cimg::mutex(6,0);
  23076. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'resize()': "
  23077. "Cannot both fill and resize image (%u,%u,%u,%u) "
  23078. "to new dimensions (%u,%u,%u,%u).",
  23079. img.pixel_type(),img._width,img._height,img._depth,img._spectrum,w,h,d,s);
  23080. }
  23081. const unsigned int
  23082. boundary = (int)_mp_arg(8);
  23083. const float
  23084. cx = (float)_mp_arg(9),
  23085. cy = (float)_mp_arg(10),
  23086. cz = (float)_mp_arg(11),
  23087. cc = (float)_mp_arg(12);
  23088. img.resize(w,h,d,s,interp,boundary,cx,cy,cz,cc);
  23089. cimg::mutex(6,0);
  23090. return cimg::type<double>::nan();
  23091. }
  23092. static double mp_image_s(_cimg_math_parser& mp) {
  23093. unsigned int ind = (unsigned int)mp.opcode[2];
  23094. if (ind!=~0U) {
  23095. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23096. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23097. }
  23098. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23099. return (double)img.spectrum();
  23100. }
  23101. static double mp_image_sort(_cimg_math_parser& mp) {
  23102. mp_check_list(mp,"sort");
  23103. const bool is_increasing = (bool)_mp_arg(3);
  23104. const unsigned int
  23105. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23106. axis = (unsigned int)_mp_arg(4);
  23107. cimg::mutex(6);
  23108. CImg<T> &img = mp.imglist[ind];
  23109. img.sort(is_increasing,
  23110. axis==0 || axis=='x'?'x':
  23111. axis==1 || axis=='y'?'y':
  23112. axis==2 || axis=='z'?'z':
  23113. axis==3 || axis=='c'?'c':0);
  23114. cimg::mutex(6,0);
  23115. return cimg::type<double>::nan();
  23116. }
  23117. static double mp_image_stats(_cimg_math_parser& mp) {
  23118. double *ptrd = &_mp_arg(1) + 1;
  23119. unsigned int ind = (unsigned int)mp.opcode[2];
  23120. if (ind==~0U)
  23121. CImg<doubleT>(ptrd,14,1,1,1,true) = mp.imgout.get_stats();
  23122. else {
  23123. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23124. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23125. CImg<doubleT>(ptrd,14,1,1,1,true) = mp.imglist[ind].get_stats();
  23126. }
  23127. return cimg::type<double>::nan();
  23128. }
  23129. static double mp_image_w(_cimg_math_parser& mp) {
  23130. unsigned int ind = (unsigned int)mp.opcode[2];
  23131. if (ind!=~0U) {
  23132. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23133. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23134. }
  23135. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23136. return (double)img.width();
  23137. }
  23138. static double mp_image_wh(_cimg_math_parser& mp) {
  23139. unsigned int ind = (unsigned int)mp.opcode[2];
  23140. if (ind!=~0U) {
  23141. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23142. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23143. }
  23144. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23145. return (double)img.width()*img.height();
  23146. }
  23147. static double mp_image_whd(_cimg_math_parser& mp) {
  23148. unsigned int ind = (unsigned int)mp.opcode[2];
  23149. if (ind!=~0U) {
  23150. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23151. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23152. }
  23153. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23154. return (double)img.width()*img.height()*img.depth();
  23155. }
  23156. static double mp_image_whds(_cimg_math_parser& mp) {
  23157. unsigned int ind = (unsigned int)mp.opcode[2];
  23158. if (ind!=~0U) {
  23159. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23160. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23161. }
  23162. const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  23163. return (double)img.width()*img.height()*img.depth()*img.spectrum();
  23164. }
  23165. static double mp_increment(_cimg_math_parser& mp) {
  23166. return _mp_arg(2) + 1;
  23167. }
  23168. static double mp_inrange(_cimg_math_parser& mp) {
  23169. const unsigned int sizd = (unsigned int)mp.opcode[2];
  23170. const bool
  23171. include_m = (bool)_mp_arg(9),
  23172. include_M = (bool)_mp_arg(10);
  23173. if (!sizd) { // Scalar result
  23174. const double val = _mp_arg(3);
  23175. const double m = _mp_arg(5), M = _mp_arg(7);
  23176. if (M>=m) return (double)((include_m?(val>=m):(val>m)) && (include_M?(val<=M):(val<M)));
  23177. else return (double)((include_M?(val>=M):(val>M)) && (include_m?(val<=m):(val<m)));
  23178. }
  23179. // Vector result
  23180. const unsigned int
  23181. siz1 = (unsigned int)mp.opcode[4],
  23182. siz2 = (unsigned int)mp.opcode[6],
  23183. siz3 = (unsigned int)mp.opcode[8],
  23184. off1 = siz1?1:0,
  23185. off2 = siz2?1:0,
  23186. off3 = siz3?1:0;
  23187. double *ptrd = &_mp_arg(1) + 1;
  23188. const double
  23189. *ptr1 = &_mp_arg(3) + off1,
  23190. *ptr2 = &_mp_arg(5) + off2,
  23191. *ptr3 = &_mp_arg(7) + off3;
  23192. for (unsigned int k = 0; k<sizd; ++k) {
  23193. const double val = *ptr1;
  23194. const double m = *ptr2, M = *ptr3;
  23195. if (M>=m)
  23196. ptrd[k] = (double)((include_m?(val>=m):(val>m)) && (include_M?(val<=M):(val<M)));
  23197. else
  23198. ptrd[k] = (double)((include_M?(val>=M):(val>M)) && (include_m?(val<=m):(val<m)));
  23199. ptr1+=off1;
  23200. ptr2+=off2;
  23201. ptr3+=off3;
  23202. }
  23203. return cimg::type<double>::nan();
  23204. }
  23205. static double mp_int(_cimg_math_parser& mp) {
  23206. return (double)(longT)_mp_arg(2);
  23207. }
  23208. static double mp_ioff(_cimg_math_parser& mp) {
  23209. const unsigned int
  23210. boundary_conditions = (unsigned int)_mp_arg(3);
  23211. const CImg<T> &img = mp.imgin;
  23212. const longT
  23213. off = (longT)_mp_arg(2),
  23214. whds = (longT)img.size();
  23215. if (off>=0 && off<whds) return (double)img[off];
  23216. if (img._data) switch (boundary_conditions) {
  23217. case 3 : { // Mirror
  23218. const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
  23219. return (double)img[moff<whds?moff:whds2 - moff - 1];
  23220. }
  23221. case 2 : // Periodic
  23222. return (double)img[cimg::mod(off,whds)];
  23223. case 1 : // Neumann
  23224. return (double)img[off<0?0:whds - 1];
  23225. default : // Dirichlet
  23226. return 0;
  23227. }
  23228. return 0;
  23229. }
  23230. static double mp_isbool(_cimg_math_parser& mp) {
  23231. const double val = _mp_arg(2);
  23232. return (double)(val==0. || val==1.);
  23233. }
  23234. static double mp_isdir(_cimg_math_parser& mp) {
  23235. const unsigned int siz = (unsigned int)mp.opcode[3];
  23236. const double *ptrs = &_mp_arg(2) + (siz?1:0);
  23237. if (!siz) { char str[2] = { 0 }; *str = *ptrs; return (double)cimg::is_directory(str); }
  23238. CImg<charT> ss(siz + 1);
  23239. cimg_forX(ss,i) ss[i] = (char)ptrs[i];
  23240. ss.back() = 0;
  23241. return (double)cimg::is_directory(ss);
  23242. }
  23243. static double mp_isin(_cimg_math_parser& mp) {
  23244. const unsigned int i_end = (unsigned int)mp.opcode[2];
  23245. const double val = _mp_arg(3);
  23246. for (unsigned int i = 4; i<i_end; ++i)
  23247. if (val==_mp_arg(i)) return 1.;
  23248. return 0.;
  23249. }
  23250. static double mp_isinf(_cimg_math_parser& mp) {
  23251. return (double)cimg::type<double>::is_inf(_mp_arg(2));
  23252. }
  23253. static double mp_isint(_cimg_math_parser& mp) {
  23254. return (double)((double)(longT)_mp_arg(2)==_mp_arg(2));
  23255. }
  23256. static double mp_isfile(_cimg_math_parser& mp) {
  23257. const unsigned int siz = (unsigned int)mp.opcode[3];
  23258. const double *ptrs = &_mp_arg(2) + (siz?1:0);
  23259. if (!siz) { char str[2] = { 0 }; *str = *ptrs; return (double)cimg::is_file(str); }
  23260. CImg<charT> ss(siz + 1);
  23261. cimg_forX(ss,i) ss[i] = (char)ptrs[i];
  23262. ss.back() = 0;
  23263. return (double)cimg::is_file(ss);
  23264. }
  23265. static double mp_isnan(_cimg_math_parser& mp) {
  23266. return (double)cimg::type<double>::is_nan(_mp_arg(2));
  23267. }
  23268. static double mp_isvarname(_cimg_math_parser& mp) {
  23269. const unsigned int siz = (unsigned int)mp.opcode[3];
  23270. const double *ptrs = &_mp_arg(2) + (siz?1:0);
  23271. if (!siz) {
  23272. const char c = (char)*ptrs;
  23273. return (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_';
  23274. }
  23275. if (*ptrs>='0' && *ptrs<='9') return 0;
  23276. for (unsigned int k = 0; k<siz; ++k) if (!is_varchar((char)ptrs[k])) return 0;
  23277. return 1;
  23278. }
  23279. static double mp_ixyzc(_cimg_math_parser& mp) {
  23280. const unsigned int
  23281. interpolation = (unsigned int)_mp_arg(6),
  23282. boundary_conditions = (unsigned int)_mp_arg(7);
  23283. const CImg<T> &img = mp.imgin;
  23284. const double
  23285. x = _mp_arg(2), y = _mp_arg(3),
  23286. z = _mp_arg(4), c = _mp_arg(5);
  23287. switch (interpolation) {
  23288. case 2 : // Cubic interpolation
  23289. switch (boundary_conditions) {
  23290. case 3 : { // Mirror
  23291. const float
  23292. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23293. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23294. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23295. return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23296. my<img.height()?my:h2 - my - 1,
  23297. mz<img.depth()?mz:d2 - mz - 1,
  23298. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23299. }
  23300. case 2 : // Periodic
  23301. return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
  23302. (int)cimg::mod(c,(double)img._spectrum));
  23303. case 1 : // Neumann
  23304. return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
  23305. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23306. default : // Dirichlet
  23307. if (c<0 || c>=img._spectrum) return (T)0;
  23308. return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23309. }
  23310. case 1 : // Linear interpolation
  23311. switch (boundary_conditions) {
  23312. case 3 : { // Mirror
  23313. const float
  23314. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23315. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23316. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23317. return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23318. my<img.height()?my:h2 - my - 1,
  23319. mz<img.depth()?mz:d2 - mz - 1,
  23320. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23321. }
  23322. case 2 : // Periodic
  23323. return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
  23324. (int)cimg::mod(c,(double)img._spectrum));
  23325. case 1 : // Neumann
  23326. return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
  23327. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23328. default : // Dirichlet
  23329. if (c<0 || c>=img._spectrum) return (T)0;
  23330. return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23331. }
  23332. default : // Nearest neighbor interpolation
  23333. switch (boundary_conditions) {
  23334. case 3 : { // Mirror
  23335. const int
  23336. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
  23337. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
  23338. mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
  23339. return (double)img(mx<img.width()?mx:w2 - mx - 1,
  23340. my<img.height()?my:h2 - my - 1,
  23341. mz<img.depth()?mz:d2 - mz - 1,
  23342. mc<img.spectrum()?mc:s2 - mc - 1);
  23343. }
  23344. case 2 : // Periodic
  23345. return (double)img((int)cimg::mod(x,(double)img._width),
  23346. (int)cimg::mod(y,(double)img._height),
  23347. (int)cimg::mod(z,(double)img._depth),
  23348. (int)cimg::mod(c,(double)img._spectrum));
  23349. case 1 : // Neumann
  23350. return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
  23351. default : // Dirichlet
  23352. return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
  23353. }
  23354. }
  23355. }
  23356. static double mp_joff(_cimg_math_parser& mp) {
  23357. const unsigned int
  23358. boundary_conditions = (unsigned int)_mp_arg(3);
  23359. const int
  23360. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  23361. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  23362. const CImg<T> &img = mp.imgin;
  23363. const longT
  23364. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
  23365. whds = (longT)img.size();
  23366. if (off>=0 && off<whds) return (double)img[off];
  23367. if (img._data) switch (boundary_conditions) {
  23368. case 3 : { // Mirror
  23369. const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
  23370. return (double)img[moff<whds?moff:whds2 - moff - 1];
  23371. }
  23372. case 2 : // Periodic
  23373. return (double)img[cimg::mod(off,whds)];
  23374. case 1 : // Neumann
  23375. return (double)img[off<0?0:whds - 1];
  23376. default : // Dirichlet
  23377. return 0;
  23378. }
  23379. return 0;
  23380. }
  23381. static double mp_jxyzc(_cimg_math_parser& mp) {
  23382. const unsigned int
  23383. interpolation = (unsigned int)_mp_arg(6),
  23384. boundary_conditions = (unsigned int)_mp_arg(7);
  23385. const CImg<T> &img = mp.imgin;
  23386. const double
  23387. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
  23388. oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c],
  23389. x = ox + _mp_arg(2), y = oy + _mp_arg(3),
  23390. z = oz + _mp_arg(4), c = oc + _mp_arg(5);
  23391. switch (interpolation) {
  23392. case 2 : // Cubic interpolation
  23393. switch (boundary_conditions) {
  23394. case 3 : { // Mirror
  23395. const float
  23396. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23397. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23398. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23399. return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23400. my<img.height()?my:h2 - my - 1,
  23401. mz<img.depth()?mz:d2 - mz - 1,
  23402. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23403. }
  23404. case 2 : // Periodic
  23405. return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
  23406. (int)cimg::mod(c,(double)img._spectrum));
  23407. case 1 : // Neumann
  23408. return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
  23409. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23410. default : // Dirichlet
  23411. if (c<0 || c>=img._spectrum) return (T)0;
  23412. return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23413. }
  23414. case 1 : // Linear interpolation
  23415. switch (boundary_conditions) {
  23416. case 3 : { // Mirror
  23417. const float
  23418. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23419. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23420. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23421. return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23422. my<img.height()?my:h2 - my - 1,
  23423. mz<img.depth()?mz:d2 - mz - 1,
  23424. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23425. }
  23426. case 2 : // Periodic
  23427. return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
  23428. (int)cimg::mod(c,(double)img._spectrum));
  23429. case 1 : // Neumann
  23430. return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
  23431. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23432. default : // Dirichlet
  23433. if (c<0 || c>=img._spectrum) return (T)0;
  23434. return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23435. }
  23436. default : // Nearest neighbor interpolation
  23437. switch (boundary_conditions) {
  23438. case 3 : { // Mirror
  23439. const int
  23440. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
  23441. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
  23442. mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
  23443. return (double)img(mx<img.width()?mx:w2 - mx - 1,
  23444. my<img.height()?my:h2 - my - 1,
  23445. mz<img.depth()?mz:d2 - mz - 1,
  23446. mc<img.spectrum()?mc:s2 - mc - 1);
  23447. }
  23448. case 2 : // Periodic
  23449. return (double)img((int)cimg::mod(x,(double)img._width),
  23450. (int)cimg::mod(y,(double)img._height),
  23451. (int)cimg::mod(z,(double)img._depth),
  23452. (int)cimg::mod(c,(double)img._spectrum));
  23453. case 1 : // Neumann
  23454. return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
  23455. default : // Dirichlet
  23456. return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
  23457. }
  23458. }
  23459. }
  23460. static double mp_kth(_cimg_math_parser& mp) {
  23461. const unsigned int i_end = (unsigned int)mp.opcode[2];
  23462. CImg<doubleT> vals(i_end - 4);
  23463. double *p = vals.data();
  23464. for (unsigned int i = 4; i<i_end; ++i) *(p++) = _mp_arg(i);
  23465. longT ind = (longT)cimg::round(_mp_arg(3));
  23466. if (ind<0) ind+=vals.width() + 1;
  23467. ind = cimg::cut(ind,(longT)1,(longT)vals.width());
  23468. return vals.kth_smallest((ulongT)(ind - 1));
  23469. }
  23470. static double mp_lerp(_cimg_math_parser& mp) {
  23471. const double t = _mp_arg(4);
  23472. return _mp_arg(2)*(1-t) + _mp_arg(3)*t;
  23473. }
  23474. static double mp_linear_add(_cimg_math_parser& mp) {
  23475. return _mp_arg(2)*_mp_arg(3) + _mp_arg(4);
  23476. }
  23477. static double mp_linear_sub_left(_cimg_math_parser& mp) {
  23478. return _mp_arg(2)*_mp_arg(3) - _mp_arg(4);
  23479. }
  23480. static double mp_linear_sub_right(_cimg_math_parser& mp) {
  23481. return _mp_arg(4) - _mp_arg(2)*_mp_arg(3);
  23482. }
  23483. static double mp_list_depth(_cimg_math_parser& mp) {
  23484. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23485. return (double)mp.imglist[ind]._depth;
  23486. }
  23487. static double mp_list_find(_cimg_math_parser& mp) {
  23488. const unsigned int
  23489. indi = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23490. const CImg<T> &img = mp.imglist[indi];
  23491. const int _step = (int)_mp_arg(5), step = _step?_step:-1;
  23492. const ulongT siz = (ulongT)img.size();
  23493. longT ind = (longT)(mp.opcode[4]!=_cimg_mp_slot_nan?_mp_arg(4):step>0?0:siz - 1);
  23494. if (ind<0 || ind>=(longT)siz) return -1.;
  23495. const T
  23496. *const ptrb = img.data(),
  23497. *const ptre = img.end(),
  23498. *ptr = ptrb + ind;
  23499. const double val = _mp_arg(3);
  23500. // Forward search
  23501. if (step>0) {
  23502. while (ptr<ptre && (double)*ptr!=val) ptr+=step;
  23503. return ptr>=ptre?-1.:(double)(ptr - ptrb);
  23504. }
  23505. // Backward search.
  23506. while (ptr>=ptrb && (double)*ptr!=val) ptr+=step;
  23507. return ptr<ptrb?-1.:(double)(ptr - ptrb);
  23508. }
  23509. static double mp_list_find_seq(_cimg_math_parser& mp) {
  23510. const unsigned int
  23511. indi = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23512. const CImg<T> &img = mp.imglist[indi];
  23513. const int _step = (bool)_mp_arg(6), step = _step?_step:-1;
  23514. const ulongT
  23515. siz1 = (ulongT)img.size(),
  23516. siz2 = (ulongT)mp.opcode[4];
  23517. longT ind = (longT)(mp.opcode[5]!=_cimg_mp_slot_nan?_mp_arg(5):step>0?0:siz1 - 1);
  23518. if (ind<0 || ind>=(longT)siz1) return -1.;
  23519. const T
  23520. *const ptr1b = img.data(),
  23521. *const ptr1e = ptr1b + siz1,
  23522. *ptr1 = ptr1b + ind,
  23523. *p1 = 0;
  23524. const double
  23525. *const ptr2b = &_mp_arg(3) + 1,
  23526. *const ptr2e = ptr2b + siz2,
  23527. *p2 = 0;
  23528. // Forward search.
  23529. if (step>0) {
  23530. do {
  23531. while (ptr1<ptr1e && *ptr1!=*ptr2b) ptr1+=step;
  23532. if (ptr1>=ptr1e) return -1.;
  23533. p1 = ptr1 + 1;
  23534. p2 = ptr2b + 1;
  23535. while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
  23536. } while (p2<ptr2e && (ptr1+=step)<ptr1e);
  23537. return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
  23538. }
  23539. // Backward search.
  23540. do {
  23541. while (ptr1>=ptr1b && *ptr1!=*ptr2b) ptr1+=step;
  23542. if (ptr1<ptr1b) return -1.;
  23543. p1 = ptr1 + 1;
  23544. p2 = ptr2b + 1;
  23545. while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
  23546. } while (p2<ptr2e && (ptr1+=step)>=ptr1b);
  23547. return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
  23548. }
  23549. static double mp_list_height(_cimg_math_parser& mp) {
  23550. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23551. return (double)mp.imglist[ind]._height;
  23552. }
  23553. static double mp_list_ioff(_cimg_math_parser& mp) {
  23554. const unsigned int
  23555. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23556. boundary_conditions = (unsigned int)_mp_arg(4);
  23557. const CImg<T> &img = mp.imglist[ind];
  23558. const longT
  23559. off = (longT)_mp_arg(3),
  23560. whds = (longT)img.size();
  23561. if (off>=0 && off<whds) return (double)img[off];
  23562. if (img._data) switch (boundary_conditions) {
  23563. case 3 : { // Mirror
  23564. const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
  23565. return (double)img[moff<whds?moff:whds2 - moff - 1];
  23566. }
  23567. case 2 : // Periodic
  23568. return (double)img[cimg::mod(off,whds)];
  23569. case 1 : // Neumann
  23570. return (double)img[off<0?0:whds - 1];
  23571. default : // Dirichlet
  23572. return 0;
  23573. }
  23574. return 0;
  23575. }
  23576. static double mp_list_is_shared(_cimg_math_parser& mp) {
  23577. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23578. return (double)mp.imglist[ind]._is_shared;
  23579. }
  23580. static double mp_list_ixyzc(_cimg_math_parser& mp) {
  23581. const unsigned int
  23582. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23583. interpolation = (unsigned int)_mp_arg(7),
  23584. boundary_conditions = (unsigned int)_mp_arg(8);
  23585. const CImg<T> &img = mp.imglist[ind];
  23586. const double
  23587. x = _mp_arg(3), y = _mp_arg(4),
  23588. z = _mp_arg(5), c = _mp_arg(6);
  23589. switch (interpolation) {
  23590. case 2 : // Cubic interpolation
  23591. switch (boundary_conditions) {
  23592. case 3 : { // Mirror
  23593. const float
  23594. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23595. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23596. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23597. return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23598. my<img.height()?my:h2 - my - 1,
  23599. mz<img.depth()?mz:d2 - mz - 1,
  23600. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23601. }
  23602. case 2 : // Periodic
  23603. return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
  23604. (int)cimg::mod(c,(double)img._spectrum));
  23605. case 1 : // Neumann
  23606. return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
  23607. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23608. default : // Dirichlet
  23609. if (c<0 || c>=img._spectrum) return (T)0;
  23610. return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23611. }
  23612. case 1 : // Linear interpolation
  23613. switch (boundary_conditions) {
  23614. case 3 : { // Mirror
  23615. const float
  23616. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23617. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23618. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23619. return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23620. my<img.height()?my:h2 - my - 1,
  23621. mz<img.depth()?mz:d2 - mz - 1,
  23622. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23623. }
  23624. case 2 : // Periodic
  23625. return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
  23626. (int)cimg::mod(c,(double)img._spectrum));
  23627. case 1 : // Neumann
  23628. return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
  23629. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23630. default : // Dirichlet
  23631. if (c<0 || c>=img._spectrum) return (T)0;
  23632. return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23633. }
  23634. default : // Nearest neighbor interpolation
  23635. switch (boundary_conditions) {
  23636. case 3 : { // Mirror
  23637. const int
  23638. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
  23639. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
  23640. mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
  23641. return (double)img(mx<img.width()?mx:w2 - mx - 1,
  23642. my<img.height()?my:h2 - my - 1,
  23643. mz<img.depth()?mz:d2 - mz - 1,
  23644. mc<img.spectrum()?mc:s2 - mc - 1);
  23645. }
  23646. case 2 : // Periodic
  23647. return (double)img((int)cimg::mod(x,(double)img._width),
  23648. (int)cimg::mod(y,(double)img._height),
  23649. (int)cimg::mod(z,(double)img._depth),
  23650. (int)cimg::mod(c,(double)img._spectrum));
  23651. case 1 : // Neumann
  23652. return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
  23653. default : // Dirichlet
  23654. return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
  23655. }
  23656. }
  23657. }
  23658. static double mp_list_joff(_cimg_math_parser& mp) {
  23659. const unsigned int
  23660. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23661. boundary_conditions = (unsigned int)_mp_arg(4);
  23662. const int
  23663. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  23664. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  23665. const CImg<T> &img = mp.imglist[ind];
  23666. const longT
  23667. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
  23668. whds = (longT)img.size();
  23669. if (off>=0 && off<whds) return (double)img[off];
  23670. if (img._data) switch (boundary_conditions) {
  23671. case 3 : { // Mirror
  23672. const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
  23673. return (double)img[moff<whds?moff:whds2 - moff - 1];
  23674. }
  23675. case 2 : // Periodic
  23676. return (double)img[cimg::mod(off,whds)];
  23677. case 1 : // Neumann
  23678. return (double)img[off<0?0:whds - 1];
  23679. default : // Dirichlet
  23680. return 0;
  23681. }
  23682. return 0;
  23683. }
  23684. static double mp_list_jxyzc(_cimg_math_parser& mp) {
  23685. const unsigned int
  23686. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23687. interpolation = (unsigned int)_mp_arg(7),
  23688. boundary_conditions = (unsigned int)_mp_arg(8);
  23689. const CImg<T> &img = mp.imglist[ind];
  23690. const double
  23691. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
  23692. oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c],
  23693. x = ox + _mp_arg(3), y = oy + _mp_arg(4),
  23694. z = oz + _mp_arg(5), c = oc + _mp_arg(6);
  23695. switch (interpolation) {
  23696. case 2 : // Cubic interpolation
  23697. switch (boundary_conditions) {
  23698. case 3 : { // Mirror
  23699. const float
  23700. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23701. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23702. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23703. return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23704. my<img.height()?my:h2 - my - 1,
  23705. mz<img.depth()?mz:d2 - mz - 1,
  23706. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23707. }
  23708. case 2 : // Periodic
  23709. return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
  23710. (int)cimg::mod(c,(double)img._spectrum));
  23711. case 1 : // Neumann
  23712. return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
  23713. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23714. default : // Dirichlet
  23715. if (c<0 || c>=img._spectrum) return (T)0;
  23716. return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23717. }
  23718. case 1 : // Linear interpolation
  23719. switch (boundary_conditions) {
  23720. case 3 : { // Mirror
  23721. const float
  23722. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
  23723. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
  23724. mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
  23725. return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
  23726. my<img.height()?my:h2 - my - 1,
  23727. mz<img.depth()?mz:d2 - mz - 1,
  23728. (int)(mc<img.spectrum()?mc:s2 - mc - 1));
  23729. }
  23730. case 2 : // Periodic
  23731. return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
  23732. (int)cimg::mod(c,(double)img._spectrum));
  23733. case 1 : // Neumann
  23734. return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
  23735. (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
  23736. default : // Dirichlet
  23737. if (c<0 || c>=img._spectrum) return (T)0;
  23738. return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
  23739. }
  23740. default : // Nearest neighbor interpolation
  23741. switch (boundary_conditions) {
  23742. case 3 : { // Mirror
  23743. const int
  23744. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
  23745. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
  23746. mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
  23747. return (double)img(mx<img.width()?mx:w2 - mx - 1,
  23748. my<img.height()?my:h2 - my - 1,
  23749. mz<img.depth()?mz:d2 - mz - 1,
  23750. mc<img.spectrum()?mc:s2 - mc - 1);
  23751. }
  23752. case 2 : // Periodic
  23753. return (double)img((int)cimg::mod(x,(double)img._width),
  23754. (int)cimg::mod(y,(double)img._height),
  23755. (int)cimg::mod(z,(double)img._depth),
  23756. (int)cimg::mod(c,(double)img._spectrum));
  23757. case 1 : // Neumann
  23758. return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
  23759. default : // Dirichlet
  23760. return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
  23761. }
  23762. }
  23763. }
  23764. static double mp_list_l(_cimg_math_parser& mp) {
  23765. return (double)mp.imglist.width();
  23766. }
  23767. static double mp_list_median(_cimg_math_parser& mp) {
  23768. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23769. if (!mp.list_median) mp.list_median.assign(mp.imglist._width);
  23770. if (!mp.list_median[ind]) CImg<doubleT>::vector(mp.imglist[ind].median()).move_to(mp.list_median[ind]);
  23771. return *mp.list_median[ind];
  23772. }
  23773. static double mp_list_norm(_cimg_math_parser& mp) {
  23774. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23775. if (!mp.list_norm) mp.list_norm.assign(mp.imglist._width);
  23776. if (!mp.list_norm[ind]) CImg<doubleT>::vector(mp.imglist[ind].magnitude()).move_to(mp.list_norm[ind]);
  23777. return *mp.list_norm[ind];
  23778. }
  23779. static double mp_list_set_ioff(_cimg_math_parser& mp) {
  23780. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23781. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23782. CImg<T> &img = mp.imglist[ind];
  23783. const longT
  23784. off = (longT)_mp_arg(3),
  23785. whds = (longT)img.size();
  23786. const double val = _mp_arg(1);
  23787. if (off>=0 && off<whds) img[off] = (T)val;
  23788. return val;
  23789. }
  23790. static double mp_list_set_ixyzc(_cimg_math_parser& mp) {
  23791. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23792. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23793. CImg<T> &img = mp.imglist[ind];
  23794. const int
  23795. x = (int)_mp_arg(3), y = (int)_mp_arg(4),
  23796. z = (int)_mp_arg(5), c = (int)_mp_arg(6);
  23797. const double val = _mp_arg(1);
  23798. if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
  23799. z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
  23800. img(x,y,z,c) = (T)val;
  23801. return val;
  23802. }
  23803. static double mp_list_set_joff(_cimg_math_parser& mp) {
  23804. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23805. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23806. CImg<T> &img = mp.imglist[ind];
  23807. const int
  23808. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  23809. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  23810. const longT
  23811. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
  23812. whds = (longT)img.size();
  23813. const double val = _mp_arg(1);
  23814. if (off>=0 && off<whds) img[off] = (T)val;
  23815. return val;
  23816. }
  23817. static double mp_list_set_jxyzc(_cimg_math_parser& mp) {
  23818. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23819. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23820. CImg<T> &img = mp.imglist[ind];
  23821. const double
  23822. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
  23823. oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c];
  23824. const int
  23825. x = (int)(ox + _mp_arg(3)), y = (int)(oy + _mp_arg(4)),
  23826. z = (int)(oz + _mp_arg(5)), c = (int)(oc + _mp_arg(6));
  23827. const double val = _mp_arg(1);
  23828. if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
  23829. z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
  23830. img(x,y,z,c) = (T)val;
  23831. return val;
  23832. }
  23833. static double mp_list_set_Ioff_s(_cimg_math_parser& mp) {
  23834. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23835. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23836. CImg<T> &img = mp.imglist[ind];
  23837. const longT
  23838. off = (longT)_mp_arg(3),
  23839. whd = (longT)img.width()*img.height()*img.depth();
  23840. const T val = (T)_mp_arg(1);
  23841. if (off>=0 && off<whd) {
  23842. T *ptrd = &img[off];
  23843. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  23844. }
  23845. return _mp_arg(1);
  23846. }
  23847. static double mp_list_set_Ioff_v(_cimg_math_parser& mp) {
  23848. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23849. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23850. CImg<T> &img = mp.imglist[ind];
  23851. const longT
  23852. off = (longT)_mp_arg(3),
  23853. whd = (longT)img.width()*img.height()*img.depth();
  23854. const double *ptrs = &_mp_arg(1) + 1;
  23855. if (off>=0 && off<whd) {
  23856. const unsigned int vsiz = (unsigned int)mp.opcode[4];
  23857. T *ptrd = &img[off];
  23858. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  23859. }
  23860. return cimg::type<double>::nan();
  23861. }
  23862. static double mp_list_set_Ixyz_s(_cimg_math_parser& mp) {
  23863. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23864. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23865. CImg<T> &img = mp.imglist[ind];
  23866. const int
  23867. x = (int)_mp_arg(3),
  23868. y = (int)_mp_arg(4),
  23869. z = (int)_mp_arg(5);
  23870. const T val = (T)_mp_arg(1);
  23871. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  23872. T *ptrd = &img(x,y,z);
  23873. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  23874. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  23875. }
  23876. return _mp_arg(1);
  23877. }
  23878. static double mp_list_set_Ixyz_v(_cimg_math_parser& mp) {
  23879. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23880. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23881. CImg<T> &img = mp.imglist[ind];
  23882. const int
  23883. x = (int)_mp_arg(3),
  23884. y = (int)_mp_arg(4),
  23885. z = (int)_mp_arg(5);
  23886. const double *ptrs = &_mp_arg(1) + 1;
  23887. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  23888. const unsigned int vsiz = (unsigned int)mp.opcode[6];
  23889. T *ptrd = &img(x,y,z);
  23890. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  23891. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  23892. }
  23893. return cimg::type<double>::nan();
  23894. }
  23895. static double mp_list_set_Joff_s(_cimg_math_parser& mp) {
  23896. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23897. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23898. CImg<T> &img = mp.imglist[ind];
  23899. const int
  23900. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  23901. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  23902. const longT
  23903. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
  23904. whd = (longT)img.width()*img.height()*img.depth();
  23905. const T val = (T)_mp_arg(1);
  23906. if (off>=0 && off<whd) {
  23907. T *ptrd = &img[off];
  23908. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  23909. }
  23910. return _mp_arg(1);
  23911. }
  23912. static double mp_list_set_Joff_v(_cimg_math_parser& mp) {
  23913. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23914. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23915. CImg<T> &img = mp.imglist[ind];
  23916. const int
  23917. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  23918. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  23919. const longT
  23920. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
  23921. whd = (longT)img.width()*img.height()*img.depth();
  23922. const double *ptrs = &_mp_arg(1) + 1;
  23923. if (off>=0 && off<whd) {
  23924. const unsigned int vsiz = (unsigned int)mp.opcode[4];
  23925. T *ptrd = &img[off];
  23926. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  23927. }
  23928. return cimg::type<double>::nan();
  23929. }
  23930. static double mp_list_set_Jxyz_s(_cimg_math_parser& mp) {
  23931. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23932. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23933. CImg<T> &img = mp.imglist[ind];
  23934. const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
  23935. const int
  23936. x = (int)(ox + _mp_arg(3)),
  23937. y = (int)(oy + _mp_arg(4)),
  23938. z = (int)(oz + _mp_arg(5));
  23939. const T val = (T)_mp_arg(1);
  23940. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  23941. T *ptrd = &img(x,y,z);
  23942. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  23943. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  23944. }
  23945. return _mp_arg(1);
  23946. }
  23947. static double mp_list_set_Jxyz_v(_cimg_math_parser& mp) {
  23948. if (!mp.imglist.width()) return cimg::type<double>::nan();
  23949. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23950. CImg<T> &img = mp.imglist[ind];
  23951. const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
  23952. const int
  23953. x = (int)(ox + _mp_arg(3)),
  23954. y = (int)(oy + _mp_arg(4)),
  23955. z = (int)(oz + _mp_arg(5));
  23956. const double *ptrs = &_mp_arg(1) + 1;
  23957. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  23958. const unsigned int vsiz = (unsigned int)mp.opcode[6];
  23959. T *ptrd = &img(x,y,z);
  23960. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  23961. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  23962. }
  23963. return cimg::type<double>::nan();
  23964. }
  23965. static double mp_list_spectrum(_cimg_math_parser& mp) {
  23966. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23967. return (double)mp.imglist[ind]._spectrum;
  23968. }
  23969. static double mp_list_stats(_cimg_math_parser& mp) {
  23970. const unsigned int
  23971. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23972. k = (unsigned int)mp.opcode[3];
  23973. if (!mp.list_stats) mp.list_stats.assign(mp.imglist._width);
  23974. if (!mp.list_stats[ind]) mp.list_stats[ind].assign(1,14,1,1,0).fill(mp.imglist[ind].get_stats(),false);
  23975. return mp.list_stats(ind,k);
  23976. }
  23977. static double mp_list_wh(_cimg_math_parser& mp) {
  23978. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23979. return (double)mp.imglist[ind]._width*mp.imglist[ind]._height;
  23980. }
  23981. static double mp_list_whd(_cimg_math_parser& mp) {
  23982. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23983. return (double)mp.imglist[ind]._width*mp.imglist[ind]._height*mp.imglist[ind]._depth;
  23984. }
  23985. static double mp_list_whds(_cimg_math_parser& mp) {
  23986. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23987. return (double)mp.imglist[ind]._width*mp.imglist[ind]._height*mp.imglist[ind]._depth*mp.imglist[ind]._spectrum;
  23988. }
  23989. static double mp_list_width(_cimg_math_parser& mp) {
  23990. const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
  23991. return (double)mp.imglist[ind]._width;
  23992. }
  23993. static double mp_list_Ioff(_cimg_math_parser& mp) {
  23994. double *ptrd = &_mp_arg(1) + 1;
  23995. const unsigned int
  23996. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  23997. boundary_conditions = (unsigned int)_mp_arg(4),
  23998. vsiz = (unsigned int)mp.opcode[5];
  23999. const CImg<T> &img = mp.imglist[ind];
  24000. const longT
  24001. off = (longT)_mp_arg(3),
  24002. whd = (longT)img.width()*img.height()*img.depth();
  24003. const T *ptrs;
  24004. if (off>=0 && off<whd) {
  24005. ptrs = &img[off];
  24006. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24007. return cimg::type<double>::nan();
  24008. }
  24009. if (img._data) switch (boundary_conditions) {
  24010. case 3 : { // Mirror
  24011. const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
  24012. ptrs = &img[moff<whd?moff:whd2 - moff - 1];
  24013. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24014. return cimg::type<double>::nan();
  24015. }
  24016. case 2 : // Periodic
  24017. ptrs = &img[cimg::mod(off,whd)];
  24018. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24019. return cimg::type<double>::nan();
  24020. case 1 : // Neumann
  24021. ptrs = off<0?&img[0]:&img[whd - 1];
  24022. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24023. return cimg::type<double>::nan();
  24024. default : // Dirichlet
  24025. std::memset(ptrd,0,vsiz*sizeof(double));
  24026. return cimg::type<double>::nan();
  24027. }
  24028. std::memset(ptrd,0,vsiz*sizeof(double));
  24029. return cimg::type<double>::nan();
  24030. }
  24031. static double mp_list_Ixyz(_cimg_math_parser& mp) {
  24032. double *ptrd = &_mp_arg(1) + 1;
  24033. const unsigned int
  24034. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  24035. interpolation = (unsigned int)_mp_arg(6),
  24036. boundary_conditions = (unsigned int)_mp_arg(7),
  24037. vsiz = (unsigned int)mp.opcode[8];
  24038. const CImg<T> &img = mp.imglist[ind];
  24039. const double x = _mp_arg(3), y = _mp_arg(4), z = _mp_arg(5);
  24040. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24041. const T *ptrs;
  24042. switch (interpolation) {
  24043. case 2 : // Cubic interpolation
  24044. switch (boundary_conditions) {
  24045. case 3 : { // Mirror
  24046. const float
  24047. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  24048. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  24049. cx = mx<img.width()?mx:w2 - mx - 1,
  24050. cy = my<img.height()?my:h2 - my - 1,
  24051. cz = mz<img.depth()?mz:d2 - mz - 1;
  24052. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
  24053. } break;
  24054. case 2 : // Periodic
  24055. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
  24056. break;
  24057. case 1 : // Neumann
  24058. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
  24059. break;
  24060. default : // Dirichlet
  24061. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  24062. } break;
  24063. case 1 : // Linear interpolation
  24064. switch (boundary_conditions) {
  24065. case 3 : { // Mirror
  24066. const float
  24067. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  24068. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  24069. cx = mx<img.width()?mx:w2 - mx - 1,
  24070. cy = my<img.height()?my:h2 - my - 1,
  24071. cz = mz<img.depth()?mz:d2 - mz - 1;
  24072. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
  24073. } break;
  24074. case 2 : // Periodic
  24075. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
  24076. break;
  24077. case 1 : // Neumann
  24078. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
  24079. break;
  24080. default : // Dirichlet
  24081. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  24082. } break;
  24083. default : // Nearest neighbor interpolation
  24084. switch (boundary_conditions) {
  24085. case 3 : { // Mirror
  24086. const int
  24087. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
  24088. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
  24089. cx = mx<img.width()?mx:w2 - mx - 1,
  24090. cy = my<img.height()?my:h2 - my - 1,
  24091. cz = mz<img.depth()?mz:d2 - mz - 1;
  24092. ptrs = &img(cx,cy,cz);
  24093. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24094. } break;
  24095. case 2 : { // Periodic
  24096. const int
  24097. cx = (int)cimg::mod(x,(double)img._width),
  24098. cy = (int)cimg::mod(y,(double)img._height),
  24099. cz = (int)cimg::mod(z,(double)img._depth);
  24100. ptrs = &img(cx,cy,cz);
  24101. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24102. } break;
  24103. case 1 : { // Neumann
  24104. ptrs = &img._atXYZ((int)x,(int)y,(int)z);
  24105. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24106. } break;
  24107. default : // Dirichlet
  24108. if (img.containsXYZC((int)x,(int)y,(int)z)) {
  24109. ptrs = &img((int)x,(int)y,(int)z);
  24110. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24111. } else std::memset(ptrd,0,vsiz*sizeof(double));
  24112. }
  24113. }
  24114. return cimg::type<double>::nan();
  24115. }
  24116. static double mp_list_Joff(_cimg_math_parser& mp) {
  24117. double *ptrd = &_mp_arg(1) + 1;
  24118. const unsigned int
  24119. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  24120. boundary_conditions = (unsigned int)_mp_arg(4),
  24121. vsiz = (unsigned int)mp.opcode[5];
  24122. const int
  24123. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y], oz = (int)mp.mem[_cimg_mp_slot_z];
  24124. const CImg<T> &img = mp.imglist[ind];
  24125. const longT
  24126. off = img.offset(ox,oy,oz) + (longT)_mp_arg(3),
  24127. whd = (longT)img.width()*img.height()*img.depth();
  24128. const T *ptrs;
  24129. if (off>=0 && off<whd) {
  24130. ptrs = &img[off];
  24131. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24132. return cimg::type<double>::nan();
  24133. }
  24134. if (img._data) switch (boundary_conditions) {
  24135. case 3 : { // Mirror
  24136. const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
  24137. ptrs = &img[moff<whd?moff:whd2 - moff - 1];
  24138. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24139. return cimg::type<double>::nan();
  24140. }
  24141. case 2 : // Periodic
  24142. ptrs = &img[cimg::mod(off,whd)];
  24143. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24144. return cimg::type<double>::nan();
  24145. case 1 : // Neumann
  24146. ptrs = off<0?&img[0]:&img[whd - 1];
  24147. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  24148. return cimg::type<double>::nan();
  24149. default : // Dirichlet
  24150. std::memset(ptrd,0,vsiz*sizeof(double));
  24151. return cimg::type<double>::nan();
  24152. }
  24153. std::memset(ptrd,0,vsiz*sizeof(double));
  24154. return cimg::type<double>::nan();
  24155. }
  24156. static double mp_list_Jxyz(_cimg_math_parser& mp) {
  24157. double *ptrd = &_mp_arg(1) + 1;
  24158. const unsigned int
  24159. ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
  24160. interpolation = (unsigned int)_mp_arg(6),
  24161. boundary_conditions = (unsigned int)_mp_arg(7),
  24162. vsiz = (unsigned int)mp.opcode[8];
  24163. const CImg<T> &img = mp.imglist[ind];
  24164. const double
  24165. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z],
  24166. x = ox + _mp_arg(3), y = oy + _mp_arg(4), z = oz + _mp_arg(5);
  24167. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24168. const T *ptrs;
  24169. switch (interpolation) {
  24170. case 2 : // Cubic interpolation
  24171. switch (boundary_conditions) {
  24172. case 3 : { // Mirror
  24173. const float
  24174. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  24175. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  24176. cx = mx<img.width()?mx:w2 - mx - 1,
  24177. cy = my<img.height()?my:h2 - my - 1,
  24178. cz = mz<img.depth()?mz:d2 - mz - 1;
  24179. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
  24180. } break;
  24181. case 2 : // Periodic
  24182. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
  24183. break;
  24184. case 1 : // Neumann
  24185. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
  24186. break;
  24187. default : // Dirichlet
  24188. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  24189. } break;
  24190. case 1 : // Linear interpolation
  24191. switch (boundary_conditions) {
  24192. case 3 : { // Mirror
  24193. const float
  24194. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  24195. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  24196. cx = mx<img.width()?mx:w2 - mx - 1,
  24197. cy = my<img.height()?my:h2 - my - 1,
  24198. cz = mz<img.depth()?mz:d2 - mz - 1;
  24199. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
  24200. } break;
  24201. case 2 : // Periodic
  24202. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
  24203. break;
  24204. case 1 : // Neumann
  24205. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
  24206. break;
  24207. default : // Dirichlet
  24208. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  24209. } break;
  24210. case 0 : // Nearest neighbor interpolation
  24211. switch (boundary_conditions) {
  24212. case 3 : { // Mirror
  24213. const int
  24214. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
  24215. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
  24216. cx = mx<img.width()?mx:w2 - mx - 1,
  24217. cy = my<img.height()?my:h2 - my - 1,
  24218. cz = mz<img.depth()?mz:d2 - mz - 1;
  24219. ptrs = &img(cx,cy,cz);
  24220. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24221. } break;
  24222. case 2 : { // Periodic
  24223. const int
  24224. cx = (int)cimg::mod(x,(double)img._width),
  24225. cy = (int)cimg::mod(y,(double)img._height),
  24226. cz = (int)cimg::mod(z,(double)img._depth);
  24227. ptrs = &img(cx,cy,cz);
  24228. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24229. } break;
  24230. case 1 : { // Neumann
  24231. ptrs = &img._atXYZ((int)x,(int)y,(int)z);
  24232. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24233. } break;
  24234. default : // Dirichlet
  24235. if (img.containsXYZC((int)x,(int)y,(int)z)) {
  24236. ptrs = &img((int)x,(int)y,(int)z);
  24237. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  24238. } else std::memset(ptrd,0,vsiz*sizeof(double));
  24239. }
  24240. }
  24241. return cimg::type<double>::nan();
  24242. }
  24243. static double mp_log(_cimg_math_parser& mp) {
  24244. return std::log(_mp_arg(2));
  24245. }
  24246. static double mp_log10(_cimg_math_parser& mp) {
  24247. return std::log10(_mp_arg(2));
  24248. }
  24249. static double mp_log2(_cimg_math_parser& mp) {
  24250. return cimg::log2(_mp_arg(2));
  24251. }
  24252. static double mp_logical_and(_cimg_math_parser& mp) {
  24253. const bool val_left = (bool)_mp_arg(2);
  24254. const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[4];
  24255. if (!val_left) { mp.p_code = p_end - 1; return 0; }
  24256. const ulongT mem_right = mp.opcode[3];
  24257. for ( ; mp.p_code<p_end; ++mp.p_code) {
  24258. mp.opcode._data = mp.p_code->_data;
  24259. const ulongT target = mp.opcode[1];
  24260. mp.mem[target] = _cimg_mp_defunc(mp);
  24261. }
  24262. --mp.p_code;
  24263. return (double)(bool)mp.mem[mem_right];
  24264. }
  24265. static double mp_logical_not(_cimg_math_parser& mp) {
  24266. return (double)!_mp_arg(2);
  24267. }
  24268. static double mp_logical_or(_cimg_math_parser& mp) {
  24269. const bool val_left = (bool)_mp_arg(2);
  24270. const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[4];
  24271. if (val_left) { mp.p_code = p_end - 1; return 1; }
  24272. const ulongT mem_right = mp.opcode[3];
  24273. for ( ; mp.p_code<p_end; ++mp.p_code) {
  24274. mp.opcode._data = mp.p_code->_data;
  24275. const ulongT target = mp.opcode[1];
  24276. mp.mem[target] = _cimg_mp_defunc(mp);
  24277. }
  24278. --mp.p_code;
  24279. return (double)(bool)mp.mem[mem_right];
  24280. }
  24281. static double mp_lowercase(_cimg_math_parser& mp) {
  24282. return cimg::lowercase(_mp_arg(2));
  24283. }
  24284. static double mp_lt(_cimg_math_parser& mp) {
  24285. return (double)(_mp_arg(2)<_mp_arg(3));
  24286. }
  24287. static double mp_lte(_cimg_math_parser& mp) {
  24288. return (double)(_mp_arg(2)<=_mp_arg(3));
  24289. }
  24290. static double mp_matrix_eig(_cimg_math_parser& mp) {
  24291. double *ptrd = &_mp_arg(1) + 1;
  24292. const double *ptr1 = &_mp_arg(2) + 1;
  24293. const unsigned int k = (unsigned int)mp.opcode[3];
  24294. CImg<doubleT> val, vec;
  24295. CImg<doubleT>(ptr1,k,k,1,1,true).symmetric_eigen(val,vec);
  24296. CImg<doubleT>(ptrd,1,k,1,1,true) = val;
  24297. CImg<doubleT>(ptrd + k,k,k,1,1,true) = vec.get_transpose();
  24298. return cimg::type<double>::nan();
  24299. }
  24300. static double mp_matrix_invert(_cimg_math_parser& mp) {
  24301. double *const ptrd = &_mp_arg(1) + 1;
  24302. const double *const ptr1 = &_mp_arg(2) + 1;
  24303. const unsigned int k = (unsigned int)mp.opcode[3];
  24304. const bool use_LU = (bool)_mp_arg(4);
  24305. CImg<doubleT>(ptrd,k,k,1,1,true) = CImg<doubleT>(ptr1,k,k,1,1,true).get_invert(use_LU);
  24306. return cimg::type<double>::nan();
  24307. }
  24308. static double mp_matrix_mul(_cimg_math_parser& mp) {
  24309. double *ptrd = &_mp_arg(1) + 1;
  24310. const double
  24311. *ptr1 = &_mp_arg(2) + 1,
  24312. *ptr2 = &_mp_arg(3) + 1;
  24313. const unsigned int
  24314. k = (unsigned int)mp.opcode[4],
  24315. l = (unsigned int)mp.opcode[5],
  24316. m = (unsigned int)mp.opcode[6];
  24317. CImg<doubleT>(ptrd,m,k,1,1,true) = CImg<doubleT>(ptr1,l,k,1,1,true)*CImg<doubleT>(ptr2,m,l,1,1,true);
  24318. return cimg::type<double>::nan();
  24319. }
  24320. static double mp_matrix_pseudoinvert(_cimg_math_parser& mp) {
  24321. double *ptrd = &_mp_arg(1) + 1;
  24322. const double *ptr1 = &_mp_arg(2) + 1;
  24323. const unsigned int
  24324. k = (unsigned int)mp.opcode[3],
  24325. l = (unsigned int)mp.opcode[4];
  24326. const bool use_LU = (bool)_mp_arg(5);
  24327. CImg<doubleT>(ptrd,l,k,1,1,true) = CImg<doubleT>(ptr1,k,l,1,1,true).get_pseudoinvert(use_LU);
  24328. return cimg::type<double>::nan();
  24329. }
  24330. static double mp_matrix_svd(_cimg_math_parser& mp) {
  24331. double *ptrd = &_mp_arg(1) + 1;
  24332. const double *ptr1 = &_mp_arg(2) + 1;
  24333. const unsigned int
  24334. k = (unsigned int)mp.opcode[3],
  24335. l = (unsigned int)mp.opcode[4];
  24336. CImg<doubleT> U, S, V;
  24337. CImg<doubleT>(ptr1,k,l,1,1,true).SVD(U,S,V);
  24338. CImg<doubleT>(ptrd,k,l,1,1,true) = U;
  24339. CImg<doubleT>(ptrd + k*l,1,k,1,1,true) = S;
  24340. CImg<doubleT>(ptrd + k*l + k,k,k,1,1,true) = V;
  24341. return cimg::type<double>::nan();
  24342. }
  24343. static double mp_max(_cimg_math_parser& mp) {
  24344. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24345. double val = _mp_arg(3);
  24346. for (unsigned int i = 4; i<i_end; ++i) val = std::max(val,_mp_arg(i));
  24347. return val;
  24348. }
  24349. static double mp_maxabs(_cimg_math_parser& mp) {
  24350. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24351. double val = _mp_arg(3), absval = cimg::abs(val);
  24352. for (unsigned int i = 4; i<i_end; ++i) {
  24353. const double _val = _mp_arg(i), _absval = cimg::abs(_val);
  24354. if (_absval>absval) { val = _val; absval = _absval; }
  24355. }
  24356. return val;
  24357. }
  24358. static double* _mp_memcopy_double(_cimg_math_parser& mp, const unsigned int ind, const ulongT *const p_ref,
  24359. const longT siz, const long inc) {
  24360. const longT
  24361. off = *p_ref?p_ref[1] + (longT)mp.mem[(longT)p_ref[2]] + 1:ind,
  24362. eoff = off + (siz - 1)*inc;
  24363. if (off<0 || eoff>=mp.mem.width())
  24364. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': "
  24365. "Out-of-bounds variable pointer "
  24366. "(length: %ld, increment: %ld, offset start: %ld, "
  24367. "offset end: %ld, offset max: %u).",
  24368. mp.imgin.pixel_type(),siz,inc,off,eoff,mp.mem._width - 1);
  24369. return &mp.mem[off];
  24370. }
  24371. static float* _mp_memcopy_float(_cimg_math_parser& mp, const ulongT *const p_ref,
  24372. const longT siz, const long inc, const bool is_out) {
  24373. const unsigned ind = (unsigned int)p_ref[1];
  24374. const CImg<T> &img = is_out?
  24375. (ind==~0U?mp.imgout:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]):
  24376. (ind==~0U?mp.imgin:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]);
  24377. const bool is_relative = (bool)p_ref[2];
  24378. int ox, oy, oz, oc;
  24379. longT off = 0;
  24380. if (is_relative) {
  24381. ox = (int)mp.mem[_cimg_mp_slot_x];
  24382. oy = (int)mp.mem[_cimg_mp_slot_y];
  24383. oz = (int)mp.mem[_cimg_mp_slot_z];
  24384. oc = (int)mp.mem[_cimg_mp_slot_c];
  24385. off = img.offset(ox,oy,oz,oc);
  24386. }
  24387. if ((*p_ref)%2) {
  24388. const int
  24389. x = (int)mp.mem[p_ref[3]],
  24390. y = (int)mp.mem[p_ref[4]],
  24391. z = (int)mp.mem[p_ref[5]],
  24392. c = *p_ref==5?0:(int)mp.mem[p_ref[6]];
  24393. off+=img.offset(x,y,z,c);
  24394. } else off+=(longT)mp.mem[p_ref[3]];
  24395. const longT eoff = off + (siz - 1)*inc;
  24396. if (off<0 || eoff>=(longT)img.size())
  24397. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': "
  24398. "Out-of-bounds image pointer "
  24399. "(length: %ld, increment: %ld, offset start: %ld, "
  24400. "offset end: %ld, offset max: %lu).",
  24401. mp.imgin.pixel_type(),siz,inc,off,eoff,img.size() - 1);
  24402. return (float*)&img[off];
  24403. }
  24404. static double mp_memcopy(_cimg_math_parser& mp) {
  24405. longT siz = (longT)_mp_arg(4);
  24406. const longT inc_d = (longT)_mp_arg(5), inc_s = (longT)_mp_arg(6);
  24407. const float
  24408. _opacity = (float)_mp_arg(7),
  24409. opacity = (float)cimg::abs(_opacity),
  24410. omopacity = 1 - std::max(_opacity,0.f);
  24411. if (siz>0) {
  24412. const bool
  24413. is_doubled = mp.opcode[8]<=1,
  24414. is_doubles = mp.opcode[15]<=1;
  24415. if (is_doubled && is_doubles) { // (double*) <- (double*)
  24416. double *ptrd = _mp_memcopy_double(mp,(unsigned int)mp.opcode[2],&mp.opcode[8],siz,inc_d);
  24417. const double *ptrs = _mp_memcopy_double(mp,(unsigned int)mp.opcode[3],&mp.opcode[15],siz,inc_s);
  24418. if (inc_d==1 && inc_s==1 && _opacity>=1) {
  24419. if (ptrs + siz - 1<ptrd || ptrs>ptrd + siz - 1) std::memcpy(ptrd,ptrs,siz*sizeof(double));
  24420. else std::memmove(ptrd,ptrs,siz*sizeof(double));
  24421. } else {
  24422. if (ptrs + (siz - 1)*inc_s<ptrd || ptrs>ptrd + (siz - 1)*inc_d) {
  24423. if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24424. else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24425. } else { // Overlapping buffers
  24426. CImg<doubleT> buf((unsigned int)siz);
  24427. cimg_for(buf,ptr,double) { *ptr = *ptrs; ptrs+=inc_s; }
  24428. ptrs = buf;
  24429. if (_opacity>=1) while (siz-->0) { *ptrd = *(ptrs++); ptrd+=inc_d; }
  24430. else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**(ptrs++); ptrd+=inc_d; }
  24431. }
  24432. }
  24433. } else if (is_doubled && !is_doubles) { // (double*) <- (float*)
  24434. double *ptrd = _mp_memcopy_double(mp,(unsigned int)mp.opcode[2],&mp.opcode[8],siz,inc_d);
  24435. const float *ptrs = _mp_memcopy_float(mp,&mp.opcode[15],siz,inc_s,false);
  24436. if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24437. else while (siz-->0) { *ptrd = omopacity**ptrd + _opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24438. } else if (!is_doubled && is_doubles) { // (float*) <- (double*)
  24439. float *ptrd = _mp_memcopy_float(mp,&mp.opcode[8],siz,inc_d,true);
  24440. const double *ptrs = _mp_memcopy_double(mp,(unsigned int)mp.opcode[3],&mp.opcode[15],siz,inc_s);
  24441. if (_opacity>=1) while (siz-->0) { *ptrd = (float)*ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24442. else while (siz-->0) { *ptrd = (float)(omopacity**ptrd + opacity**ptrs); ptrd+=inc_d; ptrs+=inc_s; }
  24443. } else { // (float*) <- (float*)
  24444. float *ptrd = _mp_memcopy_float(mp,&mp.opcode[8],siz,inc_d,true);
  24445. const float *ptrs = _mp_memcopy_float(mp,&mp.opcode[15],siz,inc_s,false);
  24446. if (inc_d==1 && inc_s==1 && _opacity>=1) {
  24447. if (ptrs + siz - 1<ptrd || ptrs>ptrd + siz - 1) std::memcpy(ptrd,ptrs,siz*sizeof(float));
  24448. else std::memmove(ptrd,ptrs,siz*sizeof(float));
  24449. } else {
  24450. if (ptrs + (siz - 1)*inc_s<ptrd || ptrs>ptrd + (siz - 1)*inc_d) {
  24451. if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24452. else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
  24453. } else { // Overlapping buffers
  24454. CImg<floatT> buf((unsigned int)siz);
  24455. cimg_for(buf,ptr,float) { *ptr = *ptrs; ptrs+=inc_s; }
  24456. ptrs = buf;
  24457. if (_opacity>=1) while (siz-->0) { *ptrd = *(ptrs++); ptrd+=inc_d; }
  24458. else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**(ptrs++); ptrd+=inc_d; }
  24459. }
  24460. }
  24461. }
  24462. }
  24463. return _mp_arg(1);
  24464. }
  24465. static double mp_min(_cimg_math_parser& mp) {
  24466. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24467. double val = _mp_arg(3);
  24468. for (unsigned int i = 4; i<i_end; ++i) val = std::min(val,_mp_arg(i));
  24469. return val;
  24470. }
  24471. static double mp_minabs(_cimg_math_parser& mp) {
  24472. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24473. double val = _mp_arg(3), absval = cimg::abs(val);
  24474. for (unsigned int i = 4; i<i_end; ++i) {
  24475. const double _val = _mp_arg(i), _absval = cimg::abs(_val);
  24476. if (_absval<absval) { val = _val; absval = _absval; }
  24477. }
  24478. return val;
  24479. }
  24480. static double mp_minus(_cimg_math_parser& mp) {
  24481. return -_mp_arg(2);
  24482. }
  24483. static double mp_median(_cimg_math_parser& mp) {
  24484. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24485. switch (i_end - 3) {
  24486. case 1 : return _mp_arg(3);
  24487. case 2 : return cimg::median(_mp_arg(3),_mp_arg(4));
  24488. case 3 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5));
  24489. case 5 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5),_mp_arg(6),_mp_arg(7));
  24490. case 7 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5),_mp_arg(6),_mp_arg(7),_mp_arg(8),_mp_arg(9));
  24491. case 9 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5),_mp_arg(6),_mp_arg(7),_mp_arg(8),_mp_arg(9),
  24492. _mp_arg(10),_mp_arg(11));
  24493. case 13 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5),_mp_arg(6),_mp_arg(7),_mp_arg(8),_mp_arg(9),
  24494. _mp_arg(10),_mp_arg(11),_mp_arg(12),_mp_arg(13),_mp_arg(14),_mp_arg(15));
  24495. }
  24496. CImg<doubleT> vals(i_end - 3);
  24497. double *p = vals.data();
  24498. for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
  24499. return vals.median();
  24500. }
  24501. static double mp_modulo(_cimg_math_parser& mp) {
  24502. return cimg::mod(_mp_arg(2),_mp_arg(3));
  24503. }
  24504. static double mp_mproj(_cimg_math_parser& mp) {
  24505. double *ptrd = &_mp_arg(1) + 1;
  24506. const double
  24507. *ptrS = &_mp_arg(2) + 1,
  24508. *ptrD = &_mp_arg(5) + 1;
  24509. const unsigned int
  24510. wS = (unsigned int)mp.opcode[3],
  24511. hS = (unsigned int)mp.opcode[4],
  24512. wD = (unsigned int)mp.opcode[6];
  24513. const int
  24514. method = std::max(0,(int)_mp_arg(7)),
  24515. max_iter = std::max(0,(int)_mp_arg(8));
  24516. const double
  24517. max_residual = std::max(0.,_mp_arg(9));
  24518. CImg<doubleT>(ptrd,wS,wD,1,1,true) = CImg<doubleT>(ptrS,wS,hS,1,1,false).
  24519. project_matrix(CImg<doubleT>(ptrD,wD,hS,1,1,true),method,max_iter,max_residual);
  24520. return cimg::type<double>::nan();
  24521. }
  24522. static double mp_mul(_cimg_math_parser& mp) {
  24523. return _mp_arg(2)*_mp_arg(3);
  24524. }
  24525. static double mp_mul2(_cimg_math_parser& mp) {
  24526. return _mp_arg(2)*_mp_arg(3)*_mp_arg(4);
  24527. }
  24528. static double mp_neq(_cimg_math_parser& mp) {
  24529. return (double)(_mp_arg(2)!=_mp_arg(3));
  24530. }
  24531. static double mp_norm0(_cimg_math_parser& mp) {
  24532. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24533. switch (i_end - 3) {
  24534. case 1 : return _mp_arg(3)!=0;
  24535. case 2 : return (_mp_arg(3)!=0) + (_mp_arg(4)!=0);
  24536. }
  24537. double res = 0;
  24538. for (unsigned int i = 3; i<i_end; ++i)
  24539. res+=_mp_arg(i)==0?0:1;
  24540. return res;
  24541. }
  24542. static double mp_norm1(_cimg_math_parser& mp) {
  24543. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24544. switch (i_end - 3) {
  24545. case 1 : return cimg::abs(_mp_arg(3));
  24546. case 2 : return cimg::abs(_mp_arg(3)) + cimg::abs(_mp_arg(4));
  24547. }
  24548. double res = 0;
  24549. for (unsigned int i = 3; i<i_end; ++i)
  24550. res+=cimg::abs(_mp_arg(i));
  24551. return res;
  24552. }
  24553. static double mp_norm2(_cimg_math_parser& mp) {
  24554. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24555. switch (i_end - 3) {
  24556. case 1 : return cimg::abs(_mp_arg(3));
  24557. case 2 : return cimg::_hypot(_mp_arg(3),_mp_arg(4));
  24558. }
  24559. double res = 0;
  24560. for (unsigned int i = 3; i<i_end; ++i)
  24561. res+=cimg::sqr(_mp_arg(i));
  24562. return std::sqrt(res);
  24563. }
  24564. static double mp_norminf(_cimg_math_parser& mp) {
  24565. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24566. switch (i_end - 3) {
  24567. case 1 : return cimg::abs(_mp_arg(3));
  24568. case 2 : return std::max(cimg::abs(_mp_arg(3)),cimg::abs(_mp_arg(4)));
  24569. }
  24570. double res = 0;
  24571. for (unsigned int i = 3; i<i_end; ++i) {
  24572. const double val = cimg::abs(_mp_arg(i));
  24573. if (val>res) res = val;
  24574. }
  24575. return res;
  24576. }
  24577. static double mp_normp(_cimg_math_parser& mp) {
  24578. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24579. if (i_end==4) return cimg::abs(_mp_arg(3));
  24580. const double p = (double)mp.opcode[3];
  24581. double res = 0;
  24582. for (unsigned int i = 4; i<i_end; ++i)
  24583. res+=std::pow(cimg::abs(_mp_arg(i)),p);
  24584. res = std::pow(res,1/p);
  24585. return res>0?res:0.;
  24586. }
  24587. static double mp_permutations(_cimg_math_parser& mp) {
  24588. return cimg::permutations((int)_mp_arg(2),(int)_mp_arg(3),(bool)_mp_arg(4));
  24589. }
  24590. static double mp_polygon(_cimg_math_parser& mp) {
  24591. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24592. unsigned int ind = (unsigned int)mp.opcode[3];
  24593. if (ind!=~0U) {
  24594. if (!mp.imglist.width()) return cimg::type<double>::nan();
  24595. ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
  24596. }
  24597. CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
  24598. bool is_invalid_arguments = i_end<=4, is_outlined = false;
  24599. if (!is_invalid_arguments) {
  24600. int nbv = (int)_mp_arg(4);
  24601. if (!nbv) is_invalid_arguments = true;
  24602. else {
  24603. if (nbv<0) { nbv = -nbv; is_outlined = true; }
  24604. CImg<intT> points(nbv,2,1,1,0);
  24605. CImg<T> color(img._spectrum,1,1,1,0);
  24606. float opacity = 1;
  24607. unsigned int i = 5, pattern=~0U;
  24608. cimg_foroff(points,k) if (i<i_end) points(k/2,k%2) = (int)cimg::round(_mp_arg(i++));
  24609. else { is_invalid_arguments = true; break; }
  24610. if (!is_invalid_arguments) {
  24611. if (i<i_end) opacity = (float)_mp_arg(i++);
  24612. if (is_outlined && i<i_end) pattern = (unsigned int)_mp_arg(i++);
  24613. cimg_forX(color,k) if (i<i_end) color[k] = (T)_mp_arg(i++);
  24614. else { color.resize(k,1,1,1,-1); break; }
  24615. color.resize(img._spectrum,1,1,1,0,2);
  24616. if (is_outlined) img.draw_polygon(points,color._data,opacity,pattern);
  24617. else img.draw_polygon(points,color._data,opacity);
  24618. }
  24619. }
  24620. }
  24621. if (is_invalid_arguments) {
  24622. CImg<doubleT> args(i_end - 4);
  24623. cimg_forX(args,k) args[k] = _mp_arg(4 + k);
  24624. if (ind==~0U)
  24625. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
  24626. "Invalid arguments '%s'. ",
  24627. mp.imgin.pixel_type(),args.value_string()._data);
  24628. else
  24629. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
  24630. "Invalid arguments '#%u%s%s'. ",
  24631. mp.imgin.pixel_type(),ind,args._width?",":"",args.value_string()._data);
  24632. }
  24633. return cimg::type<double>::nan();
  24634. }
  24635. static double mp_pow(_cimg_math_parser& mp) {
  24636. const double v = _mp_arg(2), p = _mp_arg(3);
  24637. return std::pow(v,p);
  24638. }
  24639. static double mp_pow0_25(_cimg_math_parser& mp) {
  24640. const double val = _mp_arg(2);
  24641. return std::sqrt(std::sqrt(val));
  24642. }
  24643. static double mp_pow3(_cimg_math_parser& mp) {
  24644. const double val = _mp_arg(2);
  24645. return val*val*val;
  24646. }
  24647. static double mp_pow4(_cimg_math_parser& mp) {
  24648. const double val = _mp_arg(2);
  24649. return val*val*val*val;
  24650. }
  24651. static double mp_print(_cimg_math_parser& mp) {
  24652. const double val = _mp_arg(1);
  24653. const bool print_char = (bool)mp.opcode[3];
  24654. cimg_pragma_openmp(critical(mp_print))
  24655. {
  24656. CImg<charT> _expr(mp.opcode[2] - 4);
  24657. const ulongT *ptrs = mp.opcode._data + 4;
  24658. cimg_for(_expr,ptrd,char) *ptrd = (char)*(ptrs++);
  24659. cimg::strellipsize(_expr);
  24660. cimg::mutex(6);
  24661. if (print_char)
  24662. std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %.17g = '%c'",
  24663. _expr._data,val,(int)val);
  24664. else
  24665. std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %.17g",
  24666. _expr._data,val);
  24667. std::fflush(cimg::output());
  24668. cimg::mutex(6,0);
  24669. }
  24670. return val;
  24671. }
  24672. static double mp_prod(_cimg_math_parser& mp) {
  24673. const unsigned int i_end = (unsigned int)mp.opcode[2];
  24674. double val = _mp_arg(3);
  24675. for (unsigned int i = 4; i<i_end; ++i) val*=_mp_arg(i);
  24676. return val;
  24677. }
  24678. static double mp_rad2deg(_cimg_math_parser& mp) {
  24679. return _mp_arg(2)*180/cimg::PI;
  24680. }
  24681. static double mp_repeat(_cimg_math_parser& mp) {
  24682. const double nb_it = _mp_arg(2);
  24683. double
  24684. *const ptrc = mp.opcode[3]!=~0U?&_mp_arg(3):0,
  24685. *const ptrs = &_mp_arg(1);
  24686. const CImg<ulongT>
  24687. *const p_body = ++mp.p_code,
  24688. *const p_end = p_body + mp.opcode[4];
  24689. if (nb_it>0) {
  24690. const unsigned int _break_type = mp.break_type;
  24691. mp.break_type = 0;
  24692. double it = 0;
  24693. if (ptrc) { // Version with loop variable (3 arguments)
  24694. while (it<nb_it) {
  24695. *ptrc = it;
  24696. for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
  24697. mp.opcode._data = mp.p_code->_data;
  24698. const ulongT target = mp.opcode[1];
  24699. mp.mem[target] = _cimg_mp_defunc(mp);
  24700. }
  24701. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  24702. ++it;
  24703. }
  24704. *ptrc = it;
  24705. } else // Version without loop variable (2 arguments)
  24706. while (it<nb_it) {
  24707. for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
  24708. mp.opcode._data = mp.p_code->_data;
  24709. const ulongT target = mp.opcode[1];
  24710. mp.mem[target] = _cimg_mp_defunc(mp);
  24711. }
  24712. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  24713. ++it;
  24714. }
  24715. mp.break_type = _break_type;
  24716. }
  24717. mp.p_code = p_end - 1;
  24718. return *ptrs;
  24719. }
  24720. static double mp_rol(_cimg_math_parser& mp) {
  24721. return cimg::rol(_mp_arg(2),(unsigned int)_mp_arg(3));
  24722. }
  24723. static double mp_ror(_cimg_math_parser& mp) {
  24724. return cimg::ror(_mp_arg(2),(unsigned int)_mp_arg(3));
  24725. }
  24726. static double mp_rot2d(_cimg_math_parser& mp) {
  24727. double *ptrd = &_mp_arg(1) + 1;
  24728. const float
  24729. theta = (float)_mp_arg(2),
  24730. ca = std::cos(theta),
  24731. sa = std::sin(theta);
  24732. *(ptrd++) = ca;
  24733. *(ptrd++) = -sa;
  24734. *(ptrd++) = sa;
  24735. *ptrd = ca;
  24736. return cimg::type<double>::nan();
  24737. }
  24738. static double mp_rot3d(_cimg_math_parser& mp) {
  24739. double *ptrd = &_mp_arg(1) + 1;
  24740. const float
  24741. x = (float)_mp_arg(2),
  24742. y = (float)_mp_arg(3),
  24743. z = (float)_mp_arg(4),
  24744. theta = (float)_mp_arg(5);
  24745. CImg<doubleT>(ptrd,3,3,1,1,true) = CImg<doubleT>::rotation_matrix(x,y,z,theta*180/cimg::PI);
  24746. return cimg::type<double>::nan();
  24747. }
  24748. static double mp_round(_cimg_math_parser& mp) {
  24749. return cimg::round(_mp_arg(2),_mp_arg(3),(int)_mp_arg(4));
  24750. }
  24751. static double mp_self_add(_cimg_math_parser& mp) {
  24752. return _mp_arg(1)+=_mp_arg(2);
  24753. }
  24754. static double mp_self_bitwise_and(_cimg_math_parser& mp) {
  24755. double &val = _mp_arg(1);
  24756. return val = (double)((longT)val & (longT)_mp_arg(2));
  24757. }
  24758. static double mp_self_bitwise_left_shift(_cimg_math_parser& mp) {
  24759. double &val = _mp_arg(1);
  24760. return val = (double)((longT)val<<(unsigned int)_mp_arg(2));
  24761. }
  24762. static double mp_self_bitwise_or(_cimg_math_parser& mp) {
  24763. double &val = _mp_arg(1);
  24764. return val = (double)((longT)val | (longT)_mp_arg(2));
  24765. }
  24766. static double mp_self_bitwise_right_shift(_cimg_math_parser& mp) {
  24767. double &val = _mp_arg(1);
  24768. return val = (double)((longT)val>>(unsigned int)_mp_arg(2));
  24769. }
  24770. static double mp_self_decrement(_cimg_math_parser& mp) {
  24771. return --_mp_arg(1);
  24772. }
  24773. static double mp_self_increment(_cimg_math_parser& mp) {
  24774. return ++_mp_arg(1);
  24775. }
  24776. static double mp_self_map_vector_s(_cimg_math_parser& mp) { // Vector += scalar
  24777. unsigned int
  24778. ptrd = (unsigned int)mp.opcode[1] + 1,
  24779. siz = (unsigned int)mp.opcode[2];
  24780. mp_func op = (mp_func)mp.opcode[3];
  24781. CImg<ulongT> l_opcode(1,3);
  24782. l_opcode[2] = mp.opcode[4]; // Scalar argument
  24783. l_opcode.swap(mp.opcode);
  24784. ulongT &target = mp.opcode[1];
  24785. while (siz-->0) { target = ptrd++; (*op)(mp); }
  24786. l_opcode.swap(mp.opcode);
  24787. return cimg::type<double>::nan();
  24788. }
  24789. static double mp_self_map_vector_v(_cimg_math_parser& mp) { // Vector += vector
  24790. unsigned int
  24791. ptrd = (unsigned int)mp.opcode[1] + 1,
  24792. siz = (unsigned int)mp.opcode[2],
  24793. ptrs = (unsigned int)mp.opcode[4] + 1;
  24794. mp_func op = (mp_func)mp.opcode[3];
  24795. CImg<ulongT> l_opcode(1,4);
  24796. l_opcode.swap(mp.opcode);
  24797. ulongT &target = mp.opcode[1], &argument = mp.opcode[2];
  24798. while (siz-->0) { target = ptrd++; argument = ptrs++; (*op)(mp); }
  24799. l_opcode.swap(mp.opcode);
  24800. return cimg::type<double>::nan();
  24801. }
  24802. static double mp_self_mul(_cimg_math_parser& mp) {
  24803. return _mp_arg(1)*=_mp_arg(2);
  24804. }
  24805. static double mp_self_div(_cimg_math_parser& mp) {
  24806. return _mp_arg(1)/=_mp_arg(2);
  24807. }
  24808. static double mp_self_modulo(_cimg_math_parser& mp) {
  24809. double &val = _mp_arg(1);
  24810. return val = cimg::mod(val,_mp_arg(2));
  24811. }
  24812. static double mp_self_pow(_cimg_math_parser& mp) {
  24813. double &val = _mp_arg(1);
  24814. return val = std::pow(val,_mp_arg(2));
  24815. }
  24816. static double mp_self_sub(_cimg_math_parser& mp) {
  24817. return _mp_arg(1)-=_mp_arg(2);
  24818. }
  24819. #ifdef cimg_mp_func_set
  24820. static double mp_set(_cimg_math_parser& mp) {
  24821. const double *ptrs = &_mp_arg(1);
  24822. double *ptrd = &_mp_arg(3) + 1;
  24823. const unsigned int
  24824. sizs = (unsigned int)mp.opcode[2],
  24825. sizd = (unsigned int)mp.opcode[4];
  24826. CImg<charT> sd(sizd + 1);
  24827. cimg_for_inX(sd,0,sd.width() - 1,i) sd[i] = (char)ptrd[i];
  24828. sd.back() = 0;
  24829. if (sizs) cimg_mp_func_set(ptrs + 1,sizs,sd._data);
  24830. else cimg_mp_func_set(ptrs,0,sd._data);
  24831. return *ptrs;
  24832. }
  24833. #endif
  24834. static double mp_set_ioff(_cimg_math_parser& mp) {
  24835. CImg<T> &img = mp.imgout;
  24836. const longT
  24837. off = (longT)_mp_arg(2),
  24838. whds = (longT)img.size();
  24839. const double val = _mp_arg(1);
  24840. if (off>=0 && off<whds) img[off] = (T)val;
  24841. return val;
  24842. }
  24843. static double mp_set_ixyzc(_cimg_math_parser& mp) {
  24844. CImg<T> &img = mp.imgout;
  24845. const int
  24846. x = (int)_mp_arg(2), y = (int)_mp_arg(3),
  24847. z = (int)_mp_arg(4), c = (int)_mp_arg(5);
  24848. const double val = _mp_arg(1);
  24849. if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
  24850. z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
  24851. img(x,y,z,c) = (T)val;
  24852. return val;
  24853. }
  24854. static double mp_set_joff(_cimg_math_parser& mp) {
  24855. CImg<T> &img = mp.imgout;
  24856. const int
  24857. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  24858. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  24859. const longT
  24860. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
  24861. whds = (longT)img.size();
  24862. const double val = _mp_arg(1);
  24863. if (off>=0 && off<whds) img[off] = (T)val;
  24864. return val;
  24865. }
  24866. static double mp_set_jxyzc(_cimg_math_parser& mp) {
  24867. CImg<T> &img = mp.imgout;
  24868. const double
  24869. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
  24870. oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c];
  24871. const int
  24872. x = (int)(ox + _mp_arg(2)), y = (int)(oy + _mp_arg(3)),
  24873. z = (int)(oz + _mp_arg(4)), c = (int)(oc + _mp_arg(5));
  24874. const double val = _mp_arg(1);
  24875. if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
  24876. z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
  24877. img(x,y,z,c) = (T)val;
  24878. return val;
  24879. }
  24880. static double mp_set_Ioff_s(_cimg_math_parser& mp) {
  24881. CImg<T> &img = mp.imgout;
  24882. const longT
  24883. off = (longT)_mp_arg(2),
  24884. whd = (longT)img.width()*img.height()*img.depth();
  24885. const T val = (T)_mp_arg(1);
  24886. if (off>=0 && off<whd) {
  24887. T *ptrd = &img[off];
  24888. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  24889. }
  24890. return _mp_arg(1);
  24891. }
  24892. static double mp_set_Ioff_v(_cimg_math_parser& mp) {
  24893. CImg<T> &img = mp.imgout;
  24894. const longT
  24895. off = (longT)_mp_arg(2),
  24896. whd = (longT)img.width()*img.height()*img.depth();
  24897. const double *ptrs = &_mp_arg(1) + 1;
  24898. if (off>=0 && off<whd) {
  24899. const unsigned int vsiz = (unsigned int)mp.opcode[3];
  24900. T *ptrd = &img[off];
  24901. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  24902. }
  24903. return cimg::type<double>::nan();
  24904. }
  24905. static double mp_set_Ixyz_s(_cimg_math_parser& mp) {
  24906. CImg<T> &img = mp.imgout;
  24907. const int
  24908. x = (int)_mp_arg(2),
  24909. y = (int)_mp_arg(3),
  24910. z = (int)_mp_arg(4);
  24911. const T val = (T)_mp_arg(1);
  24912. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  24913. T *ptrd = &img(x,y,z);
  24914. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24915. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  24916. }
  24917. return _mp_arg(1);
  24918. }
  24919. static double mp_set_Ixyz_v(_cimg_math_parser& mp) {
  24920. CImg<T> &img = mp.imgout;
  24921. const int
  24922. x = (int)_mp_arg(2),
  24923. y = (int)_mp_arg(3),
  24924. z = (int)_mp_arg(4);
  24925. const double *ptrs = &_mp_arg(1) + 1;
  24926. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  24927. const unsigned int vsiz = (unsigned int)mp.opcode[5];
  24928. T *ptrd = &img(x,y,z);
  24929. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24930. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  24931. }
  24932. return cimg::type<double>::nan();
  24933. }
  24934. static double mp_set_Joff_s(_cimg_math_parser& mp) {
  24935. CImg<T> &img = mp.imgout;
  24936. const int
  24937. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  24938. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  24939. const longT
  24940. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
  24941. whd = (longT)img.width()*img.height()*img.depth();
  24942. const T val = (T)_mp_arg(1);
  24943. if (off>=0 && off<whd) {
  24944. T *ptrd = &img[off];
  24945. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  24946. }
  24947. return _mp_arg(1);
  24948. }
  24949. static double mp_set_Joff_v(_cimg_math_parser& mp) {
  24950. CImg<T> &img = mp.imgout;
  24951. const int
  24952. ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
  24953. oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
  24954. const longT
  24955. off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
  24956. whd = (longT)img.width()*img.height()*img.depth();
  24957. const double *ptrs = &_mp_arg(1) + 1;
  24958. if (off>=0 && off<whd) {
  24959. const unsigned int vsiz = (unsigned int)mp.opcode[3];
  24960. T *ptrd = &img[off];
  24961. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  24962. }
  24963. return cimg::type<double>::nan();
  24964. }
  24965. static double mp_set_Jxyz_s(_cimg_math_parser& mp) {
  24966. CImg<T> &img = mp.imgout;
  24967. const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
  24968. const int
  24969. x = (int)(ox + _mp_arg(2)),
  24970. y = (int)(oy + _mp_arg(3)),
  24971. z = (int)(oz + _mp_arg(4));
  24972. const T val = (T)_mp_arg(1);
  24973. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  24974. T *ptrd = &img(x,y,z);
  24975. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24976. cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
  24977. }
  24978. return _mp_arg(1);
  24979. }
  24980. static double mp_set_Jxyz_v(_cimg_math_parser& mp) {
  24981. CImg<T> &img = mp.imgout;
  24982. const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
  24983. const int
  24984. x = (int)(ox + _mp_arg(2)),
  24985. y = (int)(oy + _mp_arg(3)),
  24986. z = (int)(oz + _mp_arg(4));
  24987. const double *ptrs = &_mp_arg(1) + 1;
  24988. if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
  24989. const unsigned int vsiz = (unsigned int)mp.opcode[5];
  24990. T *ptrd = &img(x,y,z);
  24991. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  24992. cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
  24993. }
  24994. return cimg::type<double>::nan();
  24995. }
  24996. static double mp_shift(_cimg_math_parser& mp) {
  24997. double *const ptrd = &_mp_arg(1) + 1;
  24998. const double *const ptrs = &_mp_arg(2) + 1;
  24999. const unsigned int siz = (unsigned int)mp.opcode[3];
  25000. const int
  25001. shift = (int)_mp_arg(4),
  25002. boundary_conditions = (int)_mp_arg(5);
  25003. CImg<doubleT>(ptrd,siz,1,1,1,true) = CImg<doubleT>(ptrs,siz,1,1,1,true).shift(shift,0,0,0,boundary_conditions);
  25004. return cimg::type<double>::nan();
  25005. }
  25006. static double mp_sign(_cimg_math_parser& mp) {
  25007. return cimg::sign(_mp_arg(2));
  25008. }
  25009. static double mp_sin(_cimg_math_parser& mp) {
  25010. return std::sin(_mp_arg(2));
  25011. }
  25012. static double mp_sinc(_cimg_math_parser& mp) {
  25013. return cimg::sinc(_mp_arg(2));
  25014. }
  25015. static double mp_sinh(_cimg_math_parser& mp) {
  25016. return std::sinh(_mp_arg(2));
  25017. }
  25018. static double mp_solve(_cimg_math_parser& mp) {
  25019. double *ptrd = &_mp_arg(1) + 1;
  25020. const double
  25021. *ptr1 = &_mp_arg(2) + 1,
  25022. *ptr2 = &_mp_arg(3) + 1;
  25023. const unsigned int
  25024. k = (unsigned int)mp.opcode[4],
  25025. l = (unsigned int)mp.opcode[5],
  25026. m = (unsigned int)mp.opcode[6];
  25027. CImg<doubleT>(ptrd,m,k,1,1,true) = CImg<doubleT>(ptr2,m,l,1,1,false).solve(CImg<doubleT>(ptr1,k,l,1,1,true));
  25028. return cimg::type<double>::nan();
  25029. }
  25030. static double mp_sort(_cimg_math_parser& mp) {
  25031. double *const ptrd = &_mp_arg(1) + 1;
  25032. const double *const ptrs = &_mp_arg(2) + 1;
  25033. const bool is_increasing = (bool)_mp_arg(4);
  25034. const unsigned int
  25035. siz = (unsigned int)mp.opcode[3],
  25036. nb_elts = mp.opcode[5]==~0U?siz:(unsigned int)_mp_arg(5),
  25037. siz_elt = (unsigned int)_mp_arg(6);
  25038. const ulongT sn = siz_elt*nb_elts;
  25039. if (sn>siz || siz_elt<1)
  25040. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'sort()': "
  25041. "Arguments 'nb_elts=%g' and 'siz_elt=%g' are invalid "
  25042. "for sorting a vector of size %u.",
  25043. mp.imgin.pixel_type(),_mp_arg(5),_mp_arg(6),siz);
  25044. CImg<doubleT>(ptrd,siz_elt,nb_elts,1,1,true) = CImg<doubleT>(ptrs,siz_elt,nb_elts,1,1,true).
  25045. get_sort(is_increasing,siz_elt>1?'y':0);
  25046. if (sn<siz) CImg<doubleT>(ptrd + sn,siz - sn,1,1,1,true) = CImg<doubleT>(ptrs + sn,siz - sn,1,1,1,true);
  25047. return cimg::type<double>::nan();
  25048. }
  25049. static double mp_sqr(_cimg_math_parser& mp) {
  25050. return cimg::sqr(_mp_arg(2));
  25051. }
  25052. static double mp_sqrt(_cimg_math_parser& mp) {
  25053. return std::sqrt(_mp_arg(2));
  25054. }
  25055. static double mp_srand(_cimg_math_parser& mp) {
  25056. mp.rng = (cimg_uint64)_mp_arg(2);
  25057. return cimg::type<double>::nan();
  25058. }
  25059. static double mp_srand0(_cimg_math_parser& mp) {
  25060. cimg::srand(&mp.rng);
  25061. #if cimg_use_openmp!=0
  25062. mp.rng+=omp_get_thread_num();
  25063. #endif
  25064. return cimg::type<double>::nan();
  25065. }
  25066. static double mp_std(_cimg_math_parser& mp) {
  25067. const unsigned int i_end = (unsigned int)mp.opcode[2];
  25068. CImg<doubleT> vals(i_end - 3);
  25069. double *p = vals.data();
  25070. for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
  25071. return std::sqrt(vals.variance());
  25072. }
  25073. static double mp_string_init(_cimg_math_parser& mp) {
  25074. const unsigned char *ptrs = (unsigned char*)&mp.opcode[3];
  25075. unsigned int
  25076. ptrd = (unsigned int)mp.opcode[1] + 1,
  25077. siz = (unsigned int)mp.opcode[2];
  25078. while (siz-->0) mp.mem[ptrd++] = (double)*(ptrs++);
  25079. return cimg::type<double>::nan();
  25080. }
  25081. #ifdef cimg_mp_func_store
  25082. static double mp_store(_cimg_math_parser& mp) {
  25083. const double
  25084. *ptr1 = &_mp_arg(2),
  25085. *ptr2 = &_mp_arg(4) + 1;
  25086. const unsigned int
  25087. siz1 = (unsigned int)mp.opcode[3],
  25088. siz2 = (unsigned int)mp.opcode[5];
  25089. const int
  25090. w = (int)_mp_arg(6),
  25091. h = (int)_mp_arg(7),
  25092. d = (int)_mp_arg(8),
  25093. s = (int)_mp_arg(9);
  25094. const bool is_compressed = (bool)_mp_arg(10);
  25095. if (w<0 || h<0 || d<0 || s<0)
  25096. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'store()': "
  25097. "Specified image dimensions (%d,%d,%d,%d) are invalid.",
  25098. pixel_type(),w,h,d,s);
  25099. CImg<charT> ss(siz2 + 1);
  25100. cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptr2[i];
  25101. ss.back() = 0;
  25102. if (siz1) cimg_mp_func_store(ptr1 + 1,siz1,
  25103. (unsigned int)w,(unsigned int)h,(unsigned int)d,(unsigned int)s,
  25104. is_compressed,ss._data);
  25105. else cimg_mp_func_store(ptr1,1,(unsigned int)w,(unsigned int)h,(unsigned int)d,(unsigned int)s,
  25106. is_compressed,ss._data);
  25107. return cimg::type<double>::nan();
  25108. }
  25109. #endif
  25110. static double mp_stov(_cimg_math_parser& mp) {
  25111. const double *ptrs = &_mp_arg(2);
  25112. const ulongT siz = (ulongT)mp.opcode[3];
  25113. longT ind = (longT)_mp_arg(4);
  25114. const bool is_strict = (bool)_mp_arg(5);
  25115. double val = cimg::type<double>::nan();
  25116. if (ind<0 || ind>=(longT)siz) return val;
  25117. if (!siz) return *ptrs>='0' && *ptrs<='9'?*ptrs - '0':val;
  25118. CImg<charT> ss(siz + 1 - ind);
  25119. ptrs+=1 + ind;
  25120. cimg_forX(ss,i) ss[i] = (char)ptrs[i];
  25121. ss.back() = 0;
  25122. const char *s = ss._data;
  25123. while (*s && *s<=32) ++s;
  25124. const bool is_negative = *s=='-';
  25125. if (is_negative || *s=='+') ++s;
  25126. int err = 0;
  25127. char sep;
  25128. if (*s=='0' && (s[1]=='x' || s[1]=='X') && s[2]>32) { // Hexadecimal number
  25129. unsigned int ival;
  25130. err = cimg_sscanf(s + 2,"%x%c",&ival,&sep);
  25131. if (err>0) val = (double)ival;
  25132. } else if (*s>32) { // Decimal number
  25133. err = cimg_sscanf(s,"%lf%c",&val,&sep);
  25134. #if cimg_OS==2
  25135. // Check for +/-NaN and +/-inf as Microsoft's sscanf() version is not able
  25136. // to read those particular values.
  25137. if (!err && (*s=='i' || *s=='I' || *s=='n' || *s=='N')) {
  25138. if (!cimg::strncasecmp(s,"inf",3)) { val = cimg::type<double>::inf(); err = 1 + (s[3]!=0); }
  25139. else if (!cimg::strncasecmp(s,"nan",3)) { val = cimg::type<double>::nan(); err = 1 + (s[3]!=0); }
  25140. }
  25141. #endif
  25142. }
  25143. if (err<=0 || (is_strict && err!=1)) return cimg::type<double>::nan();
  25144. if (is_negative) val = -val;
  25145. return val;
  25146. }
  25147. static double mp_string(_cimg_math_parser& mp) {
  25148. double *const ptrd = &_mp_arg(1) + 1;
  25149. const unsigned int nb_args = (unsigned int)(mp.opcode[3] - 3)/2;
  25150. CImgList<charT> _str;
  25151. CImg<charT> it;
  25152. for (unsigned int n = 0; n<nb_args; ++n) {
  25153. const unsigned int siz = (unsigned int)mp.opcode[5 + 2*n];
  25154. if (siz) { // Vector argument -> string
  25155. const double *ptr = &_mp_arg(4 + 2*n) + 1;
  25156. unsigned int l = 0;
  25157. while (l<siz && ptr[l]) ++l;
  25158. CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
  25159. } else { // Scalar argument -> number
  25160. it.assign(24);
  25161. cimg_snprintf(it,it._width,"%.17g",_mp_arg(4 + 2*n));
  25162. CImg<charT>::string(it,false,true).move_to(_str);
  25163. }
  25164. }
  25165. const CImg<charT> str = _str>'x';
  25166. const unsigned int sizd = std::min(str._width,(unsigned int)mp.opcode[2]);
  25167. std::memset(ptrd,0,mp.opcode[2]*sizeof(double));
  25168. for (unsigned int k = 0; k<sizd; ++k) ptrd[k] = (double)str[k];
  25169. return cimg::type<double>::nan();
  25170. }
  25171. static double mp_sub(_cimg_math_parser& mp) {
  25172. return _mp_arg(2) - _mp_arg(3);
  25173. }
  25174. static double mp_sum(_cimg_math_parser& mp) {
  25175. const unsigned int i_end = (unsigned int)mp.opcode[2];
  25176. double val = _mp_arg(3);
  25177. for (unsigned int i = 4; i<i_end; ++i) val+=_mp_arg(i);
  25178. return val;
  25179. }
  25180. static double mp_swap(_cimg_math_parser& mp) {
  25181. const unsigned int siz = (unsigned int)mp.opcode[3];
  25182. if (!siz) { // Scalar
  25183. double &arg1 = _mp_arg(1), &arg2 = _mp_arg(2);
  25184. cimg::swap(arg1,arg2);
  25185. } else { // Vector
  25186. double *const ptr1 = &_mp_arg(1) + 1, *const ptr2 = &_mp_arg(2) + 1;
  25187. for (unsigned int k = 0; k<siz; ++k) cimg::swap(ptr1[k],ptr2[k]);
  25188. }
  25189. return _mp_arg(1);
  25190. }
  25191. static double mp_tan(_cimg_math_parser& mp) {
  25192. return std::tan(_mp_arg(2));
  25193. }
  25194. static double mp_tanh(_cimg_math_parser& mp) {
  25195. return std::tanh(_mp_arg(2));
  25196. }
  25197. static double mp_trace(_cimg_math_parser& mp) {
  25198. const double *ptrs = &_mp_arg(2) + 1;
  25199. const unsigned int k = (unsigned int)mp.opcode[3];
  25200. return CImg<doubleT>(ptrs,k,k,1,1,true).trace();
  25201. }
  25202. static double mp_transpose(_cimg_math_parser& mp) {
  25203. double *ptrd = &_mp_arg(1) + 1;
  25204. const double *ptrs = &_mp_arg(2) + 1;
  25205. const unsigned int
  25206. k = (unsigned int)mp.opcode[3],
  25207. l = (unsigned int)mp.opcode[4];
  25208. CImg<doubleT>(ptrd,l,k,1,1,true) = CImg<doubleT>(ptrs,k,l,1,1,true).get_transpose();
  25209. return cimg::type<double>::nan();
  25210. }
  25211. static double mp_u(_cimg_math_parser& mp) {
  25212. return cimg::rand(_mp_arg(2),_mp_arg(3),&mp.rng);
  25213. }
  25214. static double mp_ui2f(_cimg_math_parser& mp) {
  25215. return (double)cimg::uint2float((unsigned int)_mp_arg(2));
  25216. }
  25217. static double mp_uppercase(_cimg_math_parser& mp) {
  25218. return cimg::uppercase(_mp_arg(2));
  25219. }
  25220. static double mp_var(_cimg_math_parser& mp) {
  25221. const unsigned int i_end = (unsigned int)mp.opcode[2];
  25222. CImg<doubleT> vals(i_end - 3);
  25223. double *p = vals.data();
  25224. for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
  25225. return vals.variance();
  25226. }
  25227. static double mp_vector_copy(_cimg_math_parser& mp) {
  25228. std::memcpy(&_mp_arg(1) + 1,&_mp_arg(2) + 1,sizeof(double)*mp.opcode[3]);
  25229. return cimg::type<double>::nan();
  25230. }
  25231. static double mp_vector_crop(_cimg_math_parser& mp) {
  25232. double *ptrd = &_mp_arg(1) + 1;
  25233. const double *ptrs = &_mp_arg(2) + 1;
  25234. const longT
  25235. length = (longT)mp.opcode[3],
  25236. start = (longT)_mp_arg(4),
  25237. sublength = (longT)mp.opcode[5],
  25238. step = (longT)_mp_arg(6);
  25239. if (start<0 || start + step*(sublength-1)>=length)
  25240. throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Value accessor '[]': "
  25241. "Out-of-bounds sub-vector request "
  25242. "(length: %ld, start: %ld, sub-length: %ld, step: %ld).",
  25243. mp.imgin.pixel_type(),length,start,sublength,step);
  25244. ptrs+=start;
  25245. if (step==1) std::memcpy(ptrd,ptrs,sublength*sizeof(double));
  25246. else for (longT k = 0; k<sublength; ++k) { *(ptrd++) = *ptrs; ptrs+=step; }
  25247. return cimg::type<double>::nan();
  25248. }
  25249. static double mp_vector_init(_cimg_math_parser& mp) {
  25250. unsigned int
  25251. ptrs = 4U,
  25252. ptrd = (unsigned int)mp.opcode[1] + 1,
  25253. siz = (unsigned int)mp.opcode[3];
  25254. switch (mp.opcode[2] - 4) {
  25255. case 0 : std::memset(mp.mem._data + ptrd,0,siz*sizeof(double)); break; // 0 values given
  25256. case 1 : { const double val = _mp_arg(ptrs); while (siz-->0) mp.mem[ptrd++] = val; } break;
  25257. default : while (siz-->0) { mp.mem[ptrd++] = _mp_arg(ptrs++); if (ptrs>=mp.opcode[2]) ptrs = 4U; }
  25258. }
  25259. return cimg::type<double>::nan();
  25260. }
  25261. static double mp_vector_eq(_cimg_math_parser& mp) {
  25262. const double
  25263. *ptr1 = &_mp_arg(2) + 1,
  25264. *ptr2 = &_mp_arg(4) + 1;
  25265. unsigned int p1 = (unsigned int)mp.opcode[3], p2 = (unsigned int)mp.opcode[5], n;
  25266. const int N = (int)_mp_arg(6);
  25267. const bool case_sensitive = (bool)_mp_arg(7);
  25268. bool still_equal = true;
  25269. double value;
  25270. if (!N) return true;
  25271. // Compare all values.
  25272. if (N<0) {
  25273. if (p1>0 && p2>0) { // Vector == vector
  25274. if (p1!=p2) return false;
  25275. if (case_sensitive)
  25276. while (still_equal && p1--) still_equal = *(ptr1++)==*(ptr2++);
  25277. else
  25278. while (still_equal && p1--)
  25279. still_equal = cimg::lowercase(*(ptr1++))==cimg::lowercase(*(ptr2++));
  25280. return still_equal;
  25281. } else if (p1>0 && !p2) { // Vector == scalar
  25282. value = _mp_arg(4);
  25283. if (!case_sensitive) value = cimg::lowercase(value);
  25284. while (still_equal && p1--) still_equal = *(ptr1++)==value;
  25285. return still_equal;
  25286. } else if (!p1 && p2>0) { // Scalar == vector
  25287. value = _mp_arg(2);
  25288. if (!case_sensitive) value = cimg::lowercase(value);
  25289. while (still_equal && p2--) still_equal = *(ptr2++)==value;
  25290. return still_equal;
  25291. } else { // Scalar == scalar
  25292. if (case_sensitive) return _mp_arg(2)==_mp_arg(4);
  25293. else return cimg::lowercase(_mp_arg(2))==cimg::lowercase(_mp_arg(4));
  25294. }
  25295. }
  25296. // Compare only first N values.
  25297. if (p1>0 && p2>0) { // Vector == vector
  25298. n = cimg::min((unsigned int)N,p1,p2);
  25299. if (case_sensitive)
  25300. while (still_equal && n--) still_equal = *(ptr1++)==(*ptr2++);
  25301. else
  25302. while (still_equal && n--) still_equal = cimg::lowercase(*(ptr1++))==cimg::lowercase(*(ptr2++));
  25303. return still_equal;
  25304. } else if (p1>0 && !p2) { // Vector == scalar
  25305. n = std::min((unsigned int)N,p1);
  25306. value = _mp_arg(4);
  25307. if (!case_sensitive) value = cimg::lowercase(value);
  25308. while (still_equal && n--) still_equal = *(ptr1++)==value;
  25309. return still_equal;
  25310. } else if (!p1 && p2>0) { // Scalar == vector
  25311. n = std::min((unsigned int)N,p2);
  25312. value = _mp_arg(2);
  25313. if (!case_sensitive) value = cimg::lowercase(value);
  25314. while (still_equal && n--) still_equal = *(ptr2++)==value;
  25315. return still_equal;
  25316. } // Scalar == scalar
  25317. if (case_sensitive) return _mp_arg(2)==_mp_arg(4);
  25318. return cimg::lowercase(_mp_arg(2))==cimg::lowercase(_mp_arg(4));
  25319. }
  25320. static double mp_vector_lerp(_cimg_math_parser& mp) {
  25321. unsigned int siz = (unsigned int)mp.opcode[2];
  25322. double *ptrd = &_mp_arg(1) + 1;
  25323. const double
  25324. *ptrs1 = &_mp_arg(3) + 1,
  25325. *ptrs2 = &_mp_arg(4) + 1,
  25326. t = _mp_arg(5);
  25327. for (unsigned int k = 0; k<siz; ++k) ptrd[k] = ptrs1[k]*(1-t) + ptrs2[k]*t;
  25328. return cimg::type<double>::nan();
  25329. }
  25330. static double mp_vector_off(_cimg_math_parser& mp) {
  25331. const unsigned int
  25332. ptr = (unsigned int)mp.opcode[2] + 1,
  25333. siz = (unsigned int)mp.opcode[3];
  25334. const int off = (int)_mp_arg(4);
  25335. return off>=0 && off<(int)siz?mp.mem[ptr + off]:cimg::type<double>::nan();
  25336. }
  25337. static double mp_vector_map_sv(_cimg_math_parser& mp) { // Operator(scalar,vector)
  25338. unsigned int
  25339. siz = (unsigned int)mp.opcode[2],
  25340. ptrs = (unsigned int)mp.opcode[5] + 1;
  25341. double *ptrd = &_mp_arg(1) + 1;
  25342. mp_func op = (mp_func)mp.opcode[3];
  25343. CImg<ulongT> l_opcode(4);
  25344. l_opcode[2] = mp.opcode[4]; // Scalar argument1
  25345. l_opcode.swap(mp.opcode);
  25346. ulongT &argument2 = mp.opcode[3];
  25347. while (siz-->0) { argument2 = ptrs++; *(ptrd++) = (*op)(mp); }
  25348. l_opcode.swap(mp.opcode);
  25349. return cimg::type<double>::nan();
  25350. }
  25351. static double mp_vector_map_v(_cimg_math_parser& mp) { // Operator(vector)
  25352. unsigned int
  25353. siz = (unsigned int)mp.opcode[2],
  25354. ptrs = (unsigned int)mp.opcode[4] + 1;
  25355. double *ptrd = &_mp_arg(1) + 1;
  25356. mp_func op = (mp_func)mp.opcode[3];
  25357. CImg<ulongT> l_opcode(1,3);
  25358. l_opcode.swap(mp.opcode);
  25359. ulongT &argument = mp.opcode[2];
  25360. while (siz-->0) { argument = ptrs++; *(ptrd++) = (*op)(mp); }
  25361. l_opcode.swap(mp.opcode);
  25362. return cimg::type<double>::nan();
  25363. }
  25364. static double mp_vector_map_vs(_cimg_math_parser& mp) { // Operator(vector,scalar)
  25365. unsigned int
  25366. siz = (unsigned int)mp.opcode[2],
  25367. ptrs = (unsigned int)mp.opcode[4] + 1;
  25368. double *ptrd = &_mp_arg(1) + 1;
  25369. mp_func op = (mp_func)mp.opcode[3];
  25370. CImg<ulongT> l_opcode(1,4);
  25371. l_opcode[3] = mp.opcode[5]; // Scalar argument2
  25372. l_opcode.swap(mp.opcode);
  25373. ulongT &argument1 = mp.opcode[2];
  25374. while (siz-->0) { argument1 = ptrs++; *(ptrd++) = (*op)(mp); }
  25375. l_opcode.swap(mp.opcode);
  25376. return cimg::type<double>::nan();
  25377. }
  25378. static double mp_vector_map_vss(_cimg_math_parser& mp) { // Operator(vector,scalar,scalar)
  25379. unsigned int
  25380. siz = (unsigned int)mp.opcode[2],
  25381. ptrs = (unsigned int)mp.opcode[4] + 1;
  25382. double *ptrd = &_mp_arg(1) + 1;
  25383. mp_func op = (mp_func)mp.opcode[3];
  25384. CImg<ulongT> l_opcode(1,5);
  25385. l_opcode[3] = mp.opcode[5]; // Scalar argument2
  25386. l_opcode[4] = mp.opcode[6]; // Scalar argument3
  25387. l_opcode.swap(mp.opcode);
  25388. ulongT &argument1 = mp.opcode[2];
  25389. while (siz-->0) { argument1 = ptrs++; *(ptrd++) = (*op)(mp); }
  25390. l_opcode.swap(mp.opcode);
  25391. return cimg::type<double>::nan();
  25392. }
  25393. static double mp_vector_map_vv(_cimg_math_parser& mp) { // Operator(vector,vector)
  25394. unsigned int
  25395. siz = (unsigned int)mp.opcode[2],
  25396. ptrs1 = (unsigned int)mp.opcode[4] + 1,
  25397. ptrs2 = (unsigned int)mp.opcode[5] + 1;
  25398. double *ptrd = &_mp_arg(1) + 1;
  25399. mp_func op = (mp_func)mp.opcode[3];
  25400. CImg<ulongT> l_opcode(1,4);
  25401. l_opcode.swap(mp.opcode);
  25402. ulongT &argument1 = mp.opcode[2], &argument2 = mp.opcode[3];
  25403. while (siz-->0) { argument1 = ptrs1++; argument2 = ptrs2++; *(ptrd++) = (*op)(mp); }
  25404. l_opcode.swap(mp.opcode);
  25405. return cimg::type<double>::nan();
  25406. }
  25407. static double mp_vector_neq(_cimg_math_parser& mp) {
  25408. return !mp_vector_eq(mp);
  25409. }
  25410. static double mp_vector_print(_cimg_math_parser& mp) {
  25411. const bool print_string = (bool)mp.opcode[4];
  25412. cimg_pragma_openmp(critical(mp_vector_print))
  25413. {
  25414. CImg<charT> _expr(mp.opcode[2] - 5);
  25415. const ulongT *ptrs = mp.opcode._data + 5;
  25416. cimg_for(_expr,ptrd,char) *ptrd = (char)*(ptrs++);
  25417. cimg::strellipsize(_expr);
  25418. unsigned int
  25419. ptr = (unsigned int)mp.opcode[1] + 1,
  25420. siz0 = (unsigned int)mp.opcode[3],
  25421. siz = siz0;
  25422. cimg::mutex(6);
  25423. std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = [ ",_expr._data);
  25424. unsigned int count = 0;
  25425. while (siz-->0) {
  25426. if (count>=64 && siz>=64) {
  25427. std::fprintf(cimg::output(),"...,");
  25428. ptr = (unsigned int)mp.opcode[1] + 1 + siz0 - 64;
  25429. siz = 64;
  25430. } else std::fprintf(cimg::output(),"%.17g%s",mp.mem[ptr++],siz?",":"");
  25431. ++count;
  25432. }
  25433. if (print_string) {
  25434. CImg<charT> str(siz0 + 1);
  25435. ptr = (unsigned int)mp.opcode[1] + 1;
  25436. for (unsigned int k = 0; k<siz0; ++k) str[k] = (char)mp.mem[ptr++];
  25437. str[siz0] = 0;
  25438. cimg::strellipsize(str,1024,false);
  25439. std::fprintf(cimg::output()," ] = '%s' (size: %u)",str._data,siz0);
  25440. } else std::fprintf(cimg::output()," ] (size: %u)",siz0);
  25441. std::fflush(cimg::output());
  25442. cimg::mutex(6,0);
  25443. }
  25444. return cimg::type<double>::nan();
  25445. }
  25446. static double mp_vector_resize(_cimg_math_parser& mp) {
  25447. double *const ptrd = &_mp_arg(1) + 1;
  25448. const unsigned int p1 = (unsigned int)mp.opcode[2], p2 = (unsigned int)mp.opcode[4];
  25449. const int
  25450. interpolation = (int)_mp_arg(5),
  25451. boundary_conditions = (int)_mp_arg(6);
  25452. if (p2) { // Resize vector
  25453. const double *const ptrs = &_mp_arg(3) + 1;
  25454. CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(ptrs,p2,1,1,1,true).
  25455. get_resize(p1,1,1,1,interpolation,boundary_conditions);
  25456. } else { // Resize scalar
  25457. const double value = _mp_arg(3);
  25458. CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(1,1,1,1,value).resize(p1,1,1,1,interpolation,
  25459. boundary_conditions);
  25460. }
  25461. return cimg::type<double>::nan();
  25462. }
  25463. static double mp_vector_resize_ext(_cimg_math_parser& mp) {
  25464. double *const ptrd = &_mp_arg(1) + 1;
  25465. const unsigned int
  25466. siz = (unsigned int)mp.opcode[2],
  25467. ow = (unsigned int)mp.opcode[4],
  25468. oh = (unsigned int)mp.opcode[5],
  25469. od = (unsigned int)mp.opcode[6],
  25470. os = (unsigned int)mp.opcode[7],
  25471. nw = (unsigned int)mp.opcode[8],
  25472. nh = (unsigned int)mp.opcode[9],
  25473. nd = (unsigned int)mp.opcode[10],
  25474. ns = (unsigned int)mp.opcode[11];
  25475. const int
  25476. interpolation = (int)_mp_arg(12),
  25477. boundary_conditions = (int)_mp_arg(13);
  25478. const float
  25479. ax = (float)_mp_arg(14),
  25480. ay = (float)_mp_arg(15),
  25481. az = (float)_mp_arg(16),
  25482. ac = (float)_mp_arg(17);
  25483. if (siz) { // Resize vector
  25484. const double *const ptrs = &_mp_arg(3) + 1;
  25485. CImg<doubleT>(ptrd,nw,nh,nd,ns,true) = CImg<doubleT>(ptrs,ow,oh,od,os,true).
  25486. get_resize(nw,nh,nd,ns,interpolation,boundary_conditions,ax,ay,az,ac);
  25487. } else { // Resize scalar
  25488. const double value = _mp_arg(3);
  25489. CImg<doubleT>(ptrd,nw,nh,nd,ns,true) = CImg<doubleT>(1,1,1,1,value).
  25490. resize(nw,nh,nd,ns,interpolation,boundary_conditions,ax,ay,az,ac);
  25491. }
  25492. return cimg::type<double>::nan();
  25493. }
  25494. static double mp_vector_reverse(_cimg_math_parser& mp) {
  25495. double *const ptrd = &_mp_arg(1) + 1;
  25496. const double *const ptrs = &_mp_arg(2) + 1;
  25497. const unsigned int p1 = (unsigned int)mp.opcode[3];
  25498. CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(ptrs,p1,1,1,1,true).get_mirror('x');
  25499. return cimg::type<double>::nan();
  25500. }
  25501. static double mp_vector_set_off(_cimg_math_parser& mp) {
  25502. const unsigned int
  25503. ptr = (unsigned int)mp.opcode[2] + 1,
  25504. siz = (unsigned int)mp.opcode[3];
  25505. const int off = (int)_mp_arg(4);
  25506. if (off>=0 && off<(int)siz) mp.mem[ptr + off] = _mp_arg(1);
  25507. return _mp_arg(1);
  25508. }
  25509. #define _cimg_mp_vfunc(func) \
  25510. const longT sizd = (longT)mp.opcode[2];\
  25511. const unsigned int nbargs = (unsigned int)(mp.opcode[3] - 4)/2; \
  25512. double *const ptrd = &_mp_arg(1) + (sizd?1:0); \
  25513. cimg_pragma_openmp(parallel cimg_openmp_if_size(sizd,256)) \
  25514. { CImg<doubleT> vec(nbargs); double res; \
  25515. cimg_pragma_openmp(for) for (longT k = sizd?sizd - 1:0; k>=0; --k) { \
  25516. cimg_forX(vec,n) vec[n] = *(&_mp_arg(4 + 2*n) + (k+1)*(mp.opcode[4 + 2*n + 1]?1:0)); \
  25517. func; ptrd[k] = res; \
  25518. }} \
  25519. return sizd?cimg::type<double>::nan():*ptrd;
  25520. static double _mp_vargkth(CImg<doubleT>& vec) {
  25521. const double val = (+vec).get_shared_points(1,vec.width() - 1).
  25522. kth_smallest((ulongT)cimg::cut((longT)*vec - 1,(longT)0,(longT)vec.width() - 2));
  25523. cimg_for_inX(vec,1,vec.width()-1,ind) if (vec[ind]==val) return ind - 1.;
  25524. return 1.;
  25525. }
  25526. static double mp_vargkth(_cimg_math_parser& mp) {
  25527. _cimg_mp_vfunc(res = _mp_vargkth(vec));
  25528. }
  25529. static double mp_vargmax(_cimg_math_parser& mp) {
  25530. _cimg_mp_vfunc(res = (double)(&vec.max() - vec.data()));
  25531. }
  25532. static double mp_vargmaxabs(_cimg_math_parser& mp) {
  25533. _cimg_mp_vfunc(res = (double)(&vec.maxabs() - vec.data()));
  25534. }
  25535. static double mp_vargmin(_cimg_math_parser& mp) {
  25536. _cimg_mp_vfunc(res = (double)(&vec.min() - vec.data()));
  25537. }
  25538. static double mp_vargminabs(_cimg_math_parser& mp) {
  25539. _cimg_mp_vfunc(res = (double)(&vec.minabs() - vec.data()));
  25540. }
  25541. static double mp_vavg(_cimg_math_parser& mp) {
  25542. _cimg_mp_vfunc(res = vec.mean());
  25543. }
  25544. static double mp_vkth(_cimg_math_parser& mp) {
  25545. _cimg_mp_vfunc(res = vec.get_shared_points(1,vec.width() - 1).
  25546. kth_smallest((ulongT)cimg::cut((longT)*vec - 1,(longT)0,(longT)vec.width() - 2)));
  25547. }
  25548. static double mp_vmax(_cimg_math_parser& mp) {
  25549. _cimg_mp_vfunc(res = vec.max());
  25550. }
  25551. static double mp_vmaxabs(_cimg_math_parser& mp) {
  25552. _cimg_mp_vfunc(res = vec.maxabs());
  25553. }
  25554. static double mp_vmedian(_cimg_math_parser& mp) {
  25555. _cimg_mp_vfunc(res = vec.median());
  25556. }
  25557. static double mp_vmin(_cimg_math_parser& mp) {
  25558. _cimg_mp_vfunc(res = vec.min());
  25559. }
  25560. static double mp_vminabs(_cimg_math_parser& mp) {
  25561. _cimg_mp_vfunc(res = vec.minabs());
  25562. }
  25563. static double mp_vprod(_cimg_math_parser& mp) {
  25564. _cimg_mp_vfunc(res = vec.product());
  25565. }
  25566. static double mp_vstd(_cimg_math_parser& mp) {
  25567. _cimg_mp_vfunc(res = std::sqrt(vec.get_stats()[3]));
  25568. }
  25569. static double mp_vsum(_cimg_math_parser& mp) {
  25570. _cimg_mp_vfunc(res = vec.sum());
  25571. }
  25572. static double mp_vvar(_cimg_math_parser& mp) {
  25573. _cimg_mp_vfunc(res = vec.get_stats()[3]);
  25574. }
  25575. static double mp_vtos(_cimg_math_parser& mp) {
  25576. double *ptrd = &_mp_arg(1) + 1;
  25577. const unsigned int
  25578. sizd = (unsigned int)mp.opcode[2],
  25579. sizs = (unsigned int)mp.opcode[4];
  25580. std::memset(ptrd,0,sizd*sizeof(double));
  25581. const int nb_digits = (int)_mp_arg(5);
  25582. CImg<charT> format(8);
  25583. switch (nb_digits) {
  25584. case -1 : std::strcpy(format,"%g"); break;
  25585. case 0 : std::strcpy(format,"%.17g"); break;
  25586. default : cimg_snprintf(format,format._width,"%%.%dg",nb_digits);
  25587. }
  25588. CImg<charT> str;
  25589. if (sizs) { // Vector expression
  25590. const double *ptrs = &_mp_arg(3) + 1;
  25591. CImg<doubleT>(ptrs,sizs,1,1,1,true).value_string(',',sizd + 1,format).move_to(str);
  25592. } else { // Scalar expression
  25593. str.assign(sizd + 1);
  25594. cimg_snprintf(str,sizd + 1,format,_mp_arg(3));
  25595. }
  25596. const unsigned int l = std::min(sizd,(unsigned int)std::strlen(str) + 1);
  25597. CImg<doubleT>(ptrd,l,1,1,1,true) = str.get_shared_points(0,l - 1);
  25598. return cimg::type<double>::nan();
  25599. }
  25600. static double mp_while(_cimg_math_parser& mp) {
  25601. const ulongT
  25602. mem_body = mp.opcode[1],
  25603. mem_cond = mp.opcode[2];
  25604. const CImg<ulongT>
  25605. *const p_cond = ++mp.p_code,
  25606. *const p_body = p_cond + mp.opcode[3],
  25607. *const p_end = p_body + mp.opcode[4];
  25608. const unsigned int vsiz = (unsigned int)mp.opcode[5];
  25609. bool is_cond = false;
  25610. if (mp.opcode[6]) { // Set default value for result and condition if necessary
  25611. if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
  25612. else mp.mem[mem_body] = cimg::type<double>::nan();
  25613. }
  25614. if (mp.opcode[7]) mp.mem[mem_cond] = 0;
  25615. const unsigned int _break_type = mp.break_type;
  25616. mp.break_type = 0;
  25617. do {
  25618. for (mp.p_code = p_cond; mp.p_code<p_body; ++mp.p_code) { // Evaluate condition
  25619. mp.opcode._data = mp.p_code->_data;
  25620. const ulongT target = mp.opcode[1];
  25621. mp.mem[target] = _cimg_mp_defunc(mp);
  25622. }
  25623. if (mp.break_type==1) break;
  25624. is_cond = (bool)mp.mem[mem_cond];
  25625. if (is_cond && !mp.break_type) // Evaluate body
  25626. for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
  25627. mp.opcode._data = mp.p_code->_data;
  25628. const ulongT target = mp.opcode[1];
  25629. mp.mem[target] = _cimg_mp_defunc(mp);
  25630. }
  25631. if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
  25632. } while (is_cond);
  25633. mp.break_type = _break_type;
  25634. mp.p_code = p_end - 1;
  25635. return mp.mem[mem_body];
  25636. }
  25637. static double mp_Ioff(_cimg_math_parser& mp) {
  25638. double *ptrd = &_mp_arg(1) + 1;
  25639. const unsigned int
  25640. boundary_conditions = (unsigned int)_mp_arg(3),
  25641. vsiz = (unsigned int)mp.opcode[4];
  25642. const CImg<T> &img = mp.imgin;
  25643. const longT
  25644. off = (longT)_mp_arg(2),
  25645. whd = (longT)img.width()*img.height()*img.depth();
  25646. const T *ptrs;
  25647. if (off>=0 && off<whd) {
  25648. ptrs = &img[off];
  25649. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25650. return cimg::type<double>::nan();
  25651. }
  25652. if (img._data) switch (boundary_conditions) {
  25653. case 3 : { // Mirror
  25654. const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
  25655. ptrs = &img[moff<whd?moff:whd2 - moff - 1];
  25656. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25657. return cimg::type<double>::nan();
  25658. }
  25659. case 2 : // Periodic
  25660. ptrs = &img[cimg::mod(off,whd)];
  25661. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25662. return cimg::type<double>::nan();
  25663. case 1 : // Neumann
  25664. ptrs = off<0?&img[0]:&img[whd - 1];
  25665. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25666. return cimg::type<double>::nan();
  25667. default : // Dirichlet
  25668. std::memset(ptrd,0,vsiz*sizeof(double));
  25669. return cimg::type<double>::nan();
  25670. }
  25671. std::memset(ptrd,0,vsiz*sizeof(double));
  25672. return cimg::type<double>::nan();
  25673. }
  25674. static double mp_Ixyz(_cimg_math_parser& mp) {
  25675. double *ptrd = &_mp_arg(1) + 1;
  25676. const unsigned int
  25677. interpolation = (unsigned int)_mp_arg(5),
  25678. boundary_conditions = (unsigned int)_mp_arg(6),
  25679. vsiz = (unsigned int)mp.opcode[7];
  25680. const CImg<T> &img = mp.imgin;
  25681. const double x = _mp_arg(2), y = _mp_arg(3), z = _mp_arg(4);
  25682. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  25683. const T *ptrs;
  25684. switch (interpolation) {
  25685. case 2 : // Cubic interpolation
  25686. switch (boundary_conditions) {
  25687. case 3 : { // Mirror
  25688. const float
  25689. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  25690. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  25691. cx = mx<img.width()?mx:w2 - mx - 1,
  25692. cy = my<img.height()?my:h2 - my - 1,
  25693. cz = mz<img.depth()?mz:d2 - mz - 1;
  25694. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
  25695. } break;
  25696. case 2 : // Periodic
  25697. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
  25698. break;
  25699. case 1 : // Neumann
  25700. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
  25701. break;
  25702. default : // Dirichlet
  25703. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  25704. } break;
  25705. case 1 : // Linear interpolation
  25706. switch (boundary_conditions) {
  25707. case 3 : { // Mirror
  25708. const float
  25709. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  25710. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  25711. cx = mx<img.width()?mx:w2 - mx - 1,
  25712. cy = my<img.height()?my:h2 - my - 1,
  25713. cz = mz<img.depth()?mz:d2 - mz - 1;
  25714. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
  25715. } break;
  25716. case 2 : // Periodic
  25717. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
  25718. break;
  25719. case 1 : // Neumann
  25720. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
  25721. break;
  25722. default : // Dirichlet
  25723. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  25724. } break;
  25725. default : // Nearest neighbor interpolation
  25726. switch (boundary_conditions) {
  25727. case 3 : { // Mirror
  25728. const int
  25729. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
  25730. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
  25731. cx = mx<img.width()?mx:w2 - mx - 1,
  25732. cy = my<img.height()?my:h2 - my - 1,
  25733. cz = mz<img.depth()?mz:d2 - mz - 1;
  25734. ptrs = &img(cx,cy,cz);
  25735. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25736. } break;
  25737. case 2 : { // Periodic
  25738. const int
  25739. cx = (int)cimg::mod(x,(double)img._width),
  25740. cy = (int)cimg::mod(y,(double)img._height),
  25741. cz = (int)cimg::mod(z,(double)img._depth);
  25742. ptrs = &img(cx,cy,cz);
  25743. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25744. } break;
  25745. case 1 : { // Neumann
  25746. ptrs = &img._atXYZ((int)x,(int)y,(int)z);
  25747. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25748. } break;
  25749. default : // Dirichlet
  25750. if (img.containsXYZC((int)x,(int)y,(int)z)) {
  25751. ptrs = &img((int)x,(int)y,(int)z);
  25752. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25753. } else std::memset(ptrd,0,vsiz*sizeof(double));
  25754. }
  25755. }
  25756. return cimg::type<double>::nan();
  25757. }
  25758. static double mp_Joff(_cimg_math_parser& mp) {
  25759. double *ptrd = &_mp_arg(1) + 1;
  25760. const unsigned int
  25761. boundary_conditions = (unsigned int)_mp_arg(3),
  25762. vsiz = (unsigned int)mp.opcode[4];
  25763. const CImg<T> &img = mp.imgin;
  25764. const int
  25765. ox = (int)mp.mem[_cimg_mp_slot_x],
  25766. oy = (int)mp.mem[_cimg_mp_slot_y],
  25767. oz = (int)mp.mem[_cimg_mp_slot_z];
  25768. const longT
  25769. off = img.offset(ox,oy,oz) + (longT)_mp_arg(2),
  25770. whd = (longT)img.width()*img.height()*img.depth();
  25771. const T *ptrs;
  25772. if (off>=0 && off<whd) {
  25773. ptrs = &img[off];
  25774. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25775. return cimg::type<double>::nan();
  25776. }
  25777. if (img._data) switch (boundary_conditions) {
  25778. case 3 : { // Mirror
  25779. const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
  25780. ptrs = &img[moff<whd?moff:whd2 - moff - 1];
  25781. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25782. return cimg::type<double>::nan();
  25783. }
  25784. case 2 : // Periodic
  25785. ptrs = &img[cimg::mod(off,whd)];
  25786. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25787. return cimg::type<double>::nan();
  25788. case 1 : // Neumann
  25789. ptrs = off<0?&img[0]:&img[whd - 1];
  25790. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  25791. return cimg::type<double>::nan();
  25792. default : // Dirichlet
  25793. std::memset(ptrd,0,vsiz*sizeof(double));
  25794. return cimg::type<double>::nan();
  25795. }
  25796. std::memset(ptrd,0,vsiz*sizeof(double));
  25797. return cimg::type<double>::nan();
  25798. }
  25799. static double mp_Jxyz(_cimg_math_parser& mp) {
  25800. double *ptrd = &_mp_arg(1) + 1;
  25801. const unsigned int
  25802. interpolation = (unsigned int)_mp_arg(5),
  25803. boundary_conditions = (unsigned int)_mp_arg(6),
  25804. vsiz = (unsigned int)mp.opcode[7];
  25805. const CImg<T> &img = mp.imgin;
  25806. const double
  25807. ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z],
  25808. x = ox + _mp_arg(2), y = oy + _mp_arg(3), z = oz + _mp_arg(4);
  25809. const ulongT whd = (ulongT)img._width*img._height*img._depth;
  25810. const T *ptrs;
  25811. switch (interpolation) {
  25812. case 2 : // Cubic interpolation
  25813. switch (boundary_conditions) {
  25814. case 3 : { // Mirror
  25815. const float
  25816. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  25817. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  25818. cx = mx<img.width()?mx:w2 - mx - 1,
  25819. cy = my<img.height()?my:h2 - my - 1,
  25820. cz = mz<img.depth()?mz:d2 - mz - 1;
  25821. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
  25822. } break;
  25823. case 2 : // Periodic
  25824. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
  25825. break;
  25826. case 1 : // Neumann
  25827. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
  25828. break;
  25829. default : // Dirichlet
  25830. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  25831. } break;
  25832. case 1 : // Linear interpolation
  25833. switch (boundary_conditions) {
  25834. case 3 : { // Mirror
  25835. const float
  25836. w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
  25837. mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
  25838. cx = mx<img.width()?mx:w2 - mx - 1,
  25839. cy = my<img.height()?my:h2 - my - 1,
  25840. cz = mz<img.depth()?mz:d2 - mz - 1;
  25841. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
  25842. } break;
  25843. case 2 : // Periodic
  25844. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
  25845. break;
  25846. case 1 : // Neumann
  25847. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
  25848. break;
  25849. default : // Dirichlet
  25850. cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
  25851. } break;
  25852. default : // Nearest neighbor interpolation
  25853. switch (boundary_conditions) {
  25854. case 3 : { // Mirror
  25855. const int
  25856. w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
  25857. mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
  25858. cx = mx<img.width()?mx:w2 - mx - 1,
  25859. cy = my<img.height()?my:h2 - my - 1,
  25860. cz = mz<img.depth()?mz:d2 - mz - 1;
  25861. ptrs = &img(cx,cy,cz);
  25862. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25863. } break;
  25864. case 2 : { // Periodic
  25865. const int
  25866. cx = (int)cimg::mod(x,(double)img._width),
  25867. cy = (int)cimg::mod(y,(double)img._height),
  25868. cz = (int)cimg::mod(z,(double)img._depth);
  25869. ptrs = &img(cx,cy,cz);
  25870. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25871. } break;
  25872. case 1 : { // Neumann
  25873. ptrs = &img._atXYZ((int)x,(int)y,(int)z);
  25874. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25875. } break;
  25876. default : // Dirichlet
  25877. if (img.containsXYZC((int)x,(int)y,(int)z)) {
  25878. ptrs = &img((int)x,(int)y,(int)z);
  25879. cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
  25880. } else std::memset(ptrd,0,vsiz*sizeof(double));
  25881. }
  25882. }
  25883. return cimg::type<double>::nan();
  25884. }
  25885. #undef _mp_arg
  25886. }; // struct _cimg_math_parser {}
  25887. #define _cimg_create_pointwise_functions(name,func,min_size) \
  25888. CImg<T>& name() { \
  25889. if (is_empty()) return *this; \
  25890. cimg_openmp_for(*this,func((typename cimg::superset<T,float>::type)*ptr),min_size); \
  25891. return *this; \
  25892. } \
  25893. CImg<Tfloat> get_##name() const { \
  25894. return CImg<Tfloat>(*this,false).name(); \
  25895. }
  25896. //! Compute the square value of each pixel value.
  25897. /**
  25898. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square value \f$I_{(x,y,z,c)}^2\f$.
  25899. \note
  25900. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25901. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25902. \par Example
  25903. \code
  25904. const CImg<float> img("reference.jpg");
  25905. (img,img.get_sqr().normalize(0,255)).display();
  25906. \endcode
  25907. \image html ref_sqr.jpg
  25908. **/
  25909. _cimg_create_pointwise_functions(sqr,cimg::sqr,524288)
  25910. //! Compute the square root of each pixel value.
  25911. /**
  25912. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its square root \f$\sqrt{I_{(x,y,z,c)}}\f$.
  25913. \note
  25914. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25915. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25916. \par Example
  25917. \code
  25918. const CImg<float> img("reference.jpg");
  25919. (img,img.get_sqrt().normalize(0,255)).display();
  25920. \endcode
  25921. \image html ref_sqrt.jpg
  25922. **/
  25923. _cimg_create_pointwise_functions(sqrt,std::sqrt,8192)
  25924. //! Compute the exponential of each pixel value.
  25925. /**
  25926. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its exponential \f$e^{I_{(x,y,z,c)}}\f$.
  25927. \note
  25928. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25929. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25930. **/
  25931. _cimg_create_pointwise_functions(exp,std::exp,4096)
  25932. //! Compute the error function of each pixel value.
  25933. /**
  25934. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its error function.
  25935. \note
  25936. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25937. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25938. **/
  25939. _cimg_create_pointwise_functions(erf,std::erf,4096)
  25940. //! Compute the logarithm of each pixel value.
  25941. /**
  25942. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its logarithm
  25943. \f$\mathrm{log}_{e}(I_{(x,y,z,c)})\f$.
  25944. \note
  25945. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25946. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25947. **/
  25948. _cimg_create_pointwise_functions(log,std::log,262144)
  25949. //! Compute the base-2 logarithm of each pixel value.
  25950. /**
  25951. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-2 logarithm
  25952. \f$\mathrm{log}_{2}(I_{(x,y,z,c)})\f$.
  25953. \note
  25954. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25955. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25956. **/
  25957. _cimg_create_pointwise_functions(log2,cimg::log2,4096)
  25958. //! Compute the base-10 logarithm of each pixel value.
  25959. /**
  25960. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-10 logarithm
  25961. \f$\mathrm{log}_{10}(I_{(x,y,z,c)})\f$.
  25962. \note
  25963. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25964. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25965. **/
  25966. _cimg_create_pointwise_functions(log10,std::log10,4096)
  25967. //! Compute the absolute value of each pixel value.
  25968. /**
  25969. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its absolute value \f$|I_{(x,y,z,c)}|\f$.
  25970. \note
  25971. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25972. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25973. **/
  25974. _cimg_create_pointwise_functions(abs,cimg::abs,524288)
  25975. //! Compute the sign of each pixel value.
  25976. /**
  25977. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sign
  25978. \f$\mathrm{sign}(I_{(x,y,z,c)})\f$.
  25979. \note
  25980. - The sign is set to:
  25981. - \c 1 if pixel value is strictly positive.
  25982. - \c -1 if pixel value is strictly negative.
  25983. - \c 0 if pixel value is equal to \c 0.
  25984. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25985. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25986. **/
  25987. _cimg_create_pointwise_functions(sign,cimg::sign,32768)
  25988. //! Compute the cosine of each pixel value.
  25989. /**
  25990. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its cosine \f$\cos(I_{(x,y,z,c)})\f$.
  25991. \note
  25992. - Pixel values are regarded as being in \e radian.
  25993. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  25994. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  25995. **/
  25996. _cimg_create_pointwise_functions(cos,std::cos,8192)
  25997. //! Compute the sine of each pixel value.
  25998. /**
  25999. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sine \f$\sin(I_{(x,y,z,c)})\f$.
  26000. \note
  26001. - Pixel values are regarded as being in \e radian.
  26002. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26003. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26004. **/
  26005. _cimg_create_pointwise_functions(sin,std::sin,8192)
  26006. //! Compute the sinc of each pixel value.
  26007. /**
  26008. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sinc
  26009. \f$\mathrm{sinc}(I_{(x,y,z,c)})\f$.
  26010. \note
  26011. - Pixel values are regarded as being exin \e radian.
  26012. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26013. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26014. **/
  26015. _cimg_create_pointwise_functions(sinc,cimg::sinc,2048)
  26016. //! Compute the tangent of each pixel value.
  26017. /**
  26018. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its tangent \f$\tan(I_{(x,y,z,c)})\f$.
  26019. \note
  26020. - Pixel values are regarded as being exin \e radian.
  26021. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26022. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26023. **/
  26024. _cimg_create_pointwise_functions(tan,std::tan,2048)
  26025. //! Compute the hyperbolic cosine of each pixel value.
  26026. /**
  26027. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic cosine
  26028. \f$\mathrm{cosh}(I_{(x,y,z,c)})\f$.
  26029. \note
  26030. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26031. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26032. **/
  26033. _cimg_create_pointwise_functions(cosh,std::cosh,2048)
  26034. //! Compute the hyperbolic sine of each pixel value.
  26035. /**
  26036. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic sine
  26037. \f$\mathrm{sinh}(I_{(x,y,z,c)})\f$.
  26038. \note
  26039. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26040. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26041. **/
  26042. _cimg_create_pointwise_functions(sinh,std::sinh,2048)
  26043. //! Compute the hyperbolic tangent of each pixel value.
  26044. /**
  26045. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic tangent
  26046. \f$\mathrm{tanh}(I_{(x,y,z,c)})\f$.
  26047. \note
  26048. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26049. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26050. **/
  26051. _cimg_create_pointwise_functions(tanh,std::tanh,2048)
  26052. //! Compute the arccosine of each pixel value.
  26053. /**
  26054. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosine
  26055. \f$\mathrm{acos}(I_{(x,y,z,c)})\f$.
  26056. \note
  26057. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26058. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26059. **/
  26060. _cimg_create_pointwise_functions(acos,std::acos,8192)
  26061. //! Compute the arcsine of each pixel value.
  26062. /**
  26063. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arcsine
  26064. \f$\mathrm{asin}(I_{(x,y,z,c)})\f$.
  26065. \note
  26066. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26067. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26068. **/
  26069. _cimg_create_pointwise_functions(asin,std::asin,8192)
  26070. //! Compute the arctangent of each pixel value.
  26071. /**
  26072. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent
  26073. \f$\mathrm{atan}(I_{(x,y,z,c)})\f$.
  26074. \note
  26075. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26076. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26077. **/
  26078. _cimg_create_pointwise_functions(atan,std::atan,8192)
  26079. //! Compute the arctangent2 of each pixel value.
  26080. /**
  26081. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent2
  26082. \f$\mathrm{atan2}(I_{(x,y,z,c)})\f$.
  26083. \param img Image whose pixel values specify the second argument of the \c atan2() function.
  26084. \note
  26085. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26086. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26087. \par Example
  26088. \code
  26089. const CImg<float>
  26090. img_x(100,100,1,1,"x-w/2",false), // Define an horizontal centered gradient, from '-width/2' to 'width/2'
  26091. img_y(100,100,1,1,"y-h/2",false), // Define a vertical centered gradient, from '-height/2' to 'height/2'
  26092. img_atan2 = img_y.get_atan2(img_x); // Compute atan2(y,x) for each pixel value
  26093. (img_x,img_y,img_atan2).display();
  26094. \endcode
  26095. **/
  26096. template<typename t>
  26097. CImg<T>& atan2(const CImg<t>& img) {
  26098. const ulongT siz = size(), isiz = img.size();
  26099. if (siz && isiz) {
  26100. if (is_overlapped(img)) return atan2(+img);
  26101. T *ptrd = _data, *const ptre = _data + siz;
  26102. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26103. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26104. *ptrd = (T)std::atan2((double)*ptrd,(double)*(ptrs++));
  26105. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)std::atan2((double)*ptrd,(double)*(ptrs++));
  26106. }
  26107. return *this;
  26108. }
  26109. //! Compute the arctangent2 of each pixel value \newinstance.
  26110. template<typename t>
  26111. CImg<Tfloat> get_atan2(const CImg<t>& img) const {
  26112. return CImg<Tfloat>(*this,false).atan2(img);
  26113. }
  26114. //! Compute the hyperbolic arccosine of each pixel value.
  26115. /**
  26116. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosineh
  26117. \f$\mathrm{acosh}(I_{(x,y,z,c)})\f$.
  26118. \note
  26119. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26120. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26121. **/
  26122. _cimg_create_pointwise_functions(acosh,cimg::acosh,8192)
  26123. //! Compute the hyperbolic arcsine of each pixel value.
  26124. /**
  26125. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic arcsine
  26126. \f$\mathrm{asinh}(I_{(x,y,z,c)})\f$.
  26127. \note
  26128. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26129. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26130. **/
  26131. _cimg_create_pointwise_functions(asinh,cimg::asinh,8192)
  26132. //! Compute the hyperbolic arctangent of each pixel value.
  26133. /**
  26134. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic arctangent
  26135. \f$\mathrm{atanh}(I_{(x,y,z,c)})\f$.
  26136. \note
  26137. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26138. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26139. **/
  26140. _cimg_create_pointwise_functions(atanh,cimg::atanh,8192)
  26141. //! In-place pointwise multiplication.
  26142. /**
  26143. Compute the pointwise multiplication between the image instance and the specified input image \c img.
  26144. \param img Input image, as the second operand of the multiplication.
  26145. \note
  26146. - Similar to operator+=(const CImg<t>&), except that it performs a pointwise multiplication
  26147. instead of an addition.
  26148. - It does \e not perform a \e matrix multiplication. For this purpose, use operator*=(const CImg<t>&) instead.
  26149. \par Example
  26150. \code
  26151. CImg<float>
  26152. img("reference.jpg"),
  26153. shade(img.width,img.height(),1,1,"-(x-w/2)^2-(y-h/2)^2",false);
  26154. shade.normalize(0,1);
  26155. (img,shade,img.get_mul(shade)).display();
  26156. \endcode
  26157. **/
  26158. template<typename t>
  26159. CImg<T>& mul(const CImg<t>& img) {
  26160. const ulongT siz = size(), isiz = img.size();
  26161. if (siz && isiz) {
  26162. if (is_overlapped(img)) return mul(+img);
  26163. T *ptrd = _data, *const ptre = _data + siz;
  26164. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26165. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26166. *ptrd = (T)(*ptrd * *(ptrs++));
  26167. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd * *(ptrs++));
  26168. }
  26169. return *this;
  26170. }
  26171. //! In-place pointwise multiplication \newinstance.
  26172. template<typename t>
  26173. CImg<_cimg_Tt> get_mul(const CImg<t>& img) const {
  26174. return CImg<_cimg_Tt>(*this,false).mul(img);
  26175. }
  26176. //! In-place pointwise division.
  26177. /**
  26178. Similar to mul(const CImg<t>&), except that it performs a pointwise division instead of a multiplication.
  26179. **/
  26180. template<typename t>
  26181. CImg<T>& div(const CImg<t>& img) {
  26182. const ulongT siz = size(), isiz = img.size();
  26183. if (siz && isiz) {
  26184. if (is_overlapped(img)) return div(+img);
  26185. T *ptrd = _data, *const ptre = _data + siz;
  26186. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26187. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26188. *ptrd = (T)(*ptrd / *(ptrs++));
  26189. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd / *(ptrs++));
  26190. }
  26191. return *this;
  26192. }
  26193. //! In-place pointwise division \newinstance.
  26194. template<typename t>
  26195. CImg<_cimg_Tt> get_div(const CImg<t>& img) const {
  26196. return CImg<_cimg_Tt>(*this,false).div(img);
  26197. }
  26198. //! Raise each pixel value to a specified power.
  26199. /**
  26200. Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its power \f$I_{(x,y,z,c)}^p\f$.
  26201. \param p Exponent value.
  26202. \note
  26203. - The \inplace of this method statically casts the computed values to the pixel type \c T.
  26204. - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
  26205. \par Example
  26206. \code
  26207. const CImg<float>
  26208. img0("reference.jpg"), // Load reference color image
  26209. img1 = (img0/255).pow(1.8)*=255, // Compute gamma correction, with gamma = 1.8
  26210. img2 = (img0/255).pow(0.5)*=255; // Compute gamma correction, with gamma = 0.5
  26211. (img0,img1,img2).display();
  26212. \endcode
  26213. **/
  26214. CImg<T>& pow(const double p) {
  26215. if (is_empty()) return *this;
  26216. if (p==-4) { cimg_openmp_for(*this,1/(Tfloat)cimg::pow4(*ptr),32768); return *this; }
  26217. if (p==-3) { cimg_openmp_for(*this,1/(Tfloat)cimg::pow3(*ptr),32768); return *this; }
  26218. if (p==-2) { cimg_openmp_for(*this,1/(Tfloat)cimg::sqr(*ptr),32768); return *this; }
  26219. if (p==-1) { cimg_openmp_for(*this,1/(Tfloat)*ptr,32768); return *this; }
  26220. if (p==-0.5) { cimg_openmp_for(*this,1/std::sqrt((Tfloat)*ptr),8192); return *this; }
  26221. if (p==0) return fill((T)1);
  26222. if (p==0.5) return sqrt();
  26223. if (p==1) return *this;
  26224. if (p==2) return sqr();
  26225. if (p==3) { cimg_openmp_for(*this,cimg::pow3(*ptr),262144); return *this; }
  26226. if (p==4) { cimg_openmp_for(*this,cimg::pow4(*ptr),131072); return *this; }
  26227. cimg_openmp_for(*this,std::pow((Tfloat)*ptr,(Tfloat)p),1024);
  26228. return *this;
  26229. }
  26230. //! Raise each pixel value to a specified power \newinstance.
  26231. CImg<Tfloat> get_pow(const double p) const {
  26232. return CImg<Tfloat>(*this,false).pow(p);
  26233. }
  26234. //! Raise each pixel value to a power, specified from an expression.
  26235. /**
  26236. Similar to operator+=(const char*), except it performs a pointwise exponentiation instead of an addition.
  26237. **/
  26238. CImg<T>& pow(const char *const expression) {
  26239. return pow((+*this)._fill(expression,true,1,0,0,"pow",this));
  26240. }
  26241. //! Raise each pixel value to a power, specified from an expression \newinstance.
  26242. CImg<Tfloat> get_pow(const char *const expression) const {
  26243. return CImg<Tfloat>(*this,false).pow(expression);
  26244. }
  26245. //! Raise each pixel value to a power, pointwisely specified from another image.
  26246. /**
  26247. Similar to operator+=(const CImg<t>& img), except that it performs an exponentiation instead of an addition.
  26248. **/
  26249. template<typename t>
  26250. CImg<T>& pow(const CImg<t>& img) {
  26251. const ulongT siz = size(), isiz = img.size();
  26252. if (siz && isiz) {
  26253. if (is_overlapped(img)) return pow(+img);
  26254. T *ptrd = _data, *const ptre = _data + siz;
  26255. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26256. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26257. *ptrd = (T)std::pow((double)*ptrd,(double)(*(ptrs++)));
  26258. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)std::pow((double)*ptrd,(double)(*(ptrs++)));
  26259. }
  26260. return *this;
  26261. }
  26262. //! Raise each pixel value to a power, pointwisely specified from another image \newinstance.
  26263. template<typename t>
  26264. CImg<Tfloat> get_pow(const CImg<t>& img) const {
  26265. return CImg<Tfloat>(*this,false).pow(img);
  26266. }
  26267. //! Compute the bitwise left rotation of each pixel value.
  26268. /**
  26269. Similar to operator<<=(unsigned int), except that it performs a left rotation instead of a left shift.
  26270. **/
  26271. CImg<T>& rol(const unsigned int n=1) {
  26272. if (is_empty()) return *this;
  26273. cimg_openmp_for(*this,cimg::rol(*ptr,n),32768);
  26274. return *this;
  26275. }
  26276. //! Compute the bitwise left rotation of each pixel value \newinstance.
  26277. CImg<T> get_rol(const unsigned int n=1) const {
  26278. return (+*this).rol(n);
  26279. }
  26280. //! Compute the bitwise left rotation of each pixel value.
  26281. /**
  26282. Similar to operator<<=(const char*), except that it performs a left rotation instead of a left shift.
  26283. **/
  26284. CImg<T>& rol(const char *const expression) {
  26285. return rol((+*this)._fill(expression,true,1,0,0,"rol",this));
  26286. }
  26287. //! Compute the bitwise left rotation of each pixel value \newinstance.
  26288. CImg<T> get_rol(const char *const expression) const {
  26289. return (+*this).rol(expression);
  26290. }
  26291. //! Compute the bitwise left rotation of each pixel value.
  26292. /**
  26293. Similar to operator<<=(const CImg<t>&), except that it performs a left rotation instead of a left shift.
  26294. **/
  26295. template<typename t>
  26296. CImg<T>& rol(const CImg<t>& img) {
  26297. const ulongT siz = size(), isiz = img.size();
  26298. if (siz && isiz) {
  26299. if (is_overlapped(img)) return rol(+img);
  26300. T *ptrd = _data, *const ptre = _data + siz;
  26301. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26302. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26303. *ptrd = (T)cimg::rol(*ptrd,(unsigned int)(*(ptrs++)));
  26304. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)cimg::rol(*ptrd,(unsigned int)(*(ptrs++)));
  26305. }
  26306. return *this;
  26307. }
  26308. //! Compute the bitwise left rotation of each pixel value \newinstance.
  26309. template<typename t>
  26310. CImg<T> get_rol(const CImg<t>& img) const {
  26311. return (+*this).rol(img);
  26312. }
  26313. //! Compute the bitwise right rotation of each pixel value.
  26314. /**
  26315. Similar to operator>>=(unsigned int), except that it performs a right rotation instead of a right shift.
  26316. **/
  26317. CImg<T>& ror(const unsigned int n=1) {
  26318. if (is_empty()) return *this;
  26319. cimg_openmp_for(*this,cimg::ror(*ptr,n),32768);
  26320. return *this;
  26321. }
  26322. //! Compute the bitwise right rotation of each pixel value \newinstance.
  26323. CImg<T> get_ror(const unsigned int n=1) const {
  26324. return (+*this).ror(n);
  26325. }
  26326. //! Compute the bitwise right rotation of each pixel value.
  26327. /**
  26328. Similar to operator>>=(const char*), except that it performs a right rotation instead of a right shift.
  26329. **/
  26330. CImg<T>& ror(const char *const expression) {
  26331. return ror((+*this)._fill(expression,true,1,0,0,"ror",this));
  26332. }
  26333. //! Compute the bitwise right rotation of each pixel value \newinstance.
  26334. CImg<T> get_ror(const char *const expression) const {
  26335. return (+*this).ror(expression);
  26336. }
  26337. //! Compute the bitwise right rotation of each pixel value.
  26338. /**
  26339. Similar to operator>>=(const CImg<t>&), except that it performs a right rotation instead of a right shift.
  26340. **/
  26341. template<typename t>
  26342. CImg<T>& ror(const CImg<t>& img) {
  26343. const ulongT siz = size(), isiz = img.size();
  26344. if (siz && isiz) {
  26345. if (is_overlapped(img)) return ror(+img);
  26346. T *ptrd = _data, *const ptre = _data + siz;
  26347. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26348. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26349. *ptrd = (T)cimg::ror(*ptrd,(unsigned int)(*(ptrs++)));
  26350. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)cimg::ror(*ptrd,(unsigned int)(*(ptrs++)));
  26351. }
  26352. return *this;
  26353. }
  26354. //! Compute the bitwise right rotation of each pixel value \newinstance.
  26355. template<typename t>
  26356. CImg<T> get_ror(const CImg<t>& img) const {
  26357. return (+*this).ror(img);
  26358. }
  26359. //! Pointwise min operator between instance image and a value.
  26360. /**
  26361. \param val Value used as the reference argument of the min operator.
  26362. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26363. \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{val})\f$.
  26364. **/
  26365. CImg<T>& min(const T& value) {
  26366. if (is_empty()) return *this;
  26367. cimg_openmp_for(*this,std::min(*ptr,value),65536);
  26368. return *this;
  26369. }
  26370. //! Pointwise min operator between instance image and a value \newinstance.
  26371. CImg<T> get_min(const T& value) const {
  26372. return (+*this).min(value);
  26373. }
  26374. //! Pointwise min operator between two images.
  26375. /**
  26376. \param img Image used as the reference argument of the min operator.
  26377. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26378. \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
  26379. **/
  26380. template<typename t>
  26381. CImg<T>& min(const CImg<t>& img) {
  26382. const ulongT siz = size(), isiz = img.size();
  26383. if (siz && isiz) {
  26384. if (is_overlapped(img)) return min(+img);
  26385. T *ptrd = _data, *const ptre = _data + siz;
  26386. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26387. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26388. *ptrd = std::min((T)*(ptrs++),*ptrd);
  26389. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = std::min((T)*(ptrs++),*ptrd);
  26390. }
  26391. return *this;
  26392. }
  26393. //! Pointwise min operator between two images \newinstance.
  26394. template<typename t>
  26395. CImg<_cimg_Tt> get_min(const CImg<t>& img) const {
  26396. return CImg<_cimg_Tt>(*this,false).min(img);
  26397. }
  26398. //! Pointwise min operator between an image and an expression.
  26399. /**
  26400. \param expression Math formula as a C-string.
  26401. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26402. \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
  26403. **/
  26404. CImg<T>& min(const char *const expression) {
  26405. return min((+*this)._fill(expression,true,1,0,0,"min",this));
  26406. }
  26407. //! Pointwise min operator between an image and an expression \newinstance.
  26408. CImg<Tfloat> get_min(const char *const expression) const {
  26409. return CImg<Tfloat>(*this,false).min(expression);
  26410. }
  26411. //! Pointwise max operator between instance image and a value.
  26412. /**
  26413. \param val Value used as the reference argument of the max operator.
  26414. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26415. \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{val})\f$.
  26416. **/
  26417. CImg<T>& max(const T& value) {
  26418. if (is_empty()) return *this;
  26419. cimg_openmp_for(*this,std::max(*ptr,value),65536);
  26420. return *this;
  26421. }
  26422. //! Pointwise max operator between instance image and a value \newinstance.
  26423. CImg<T> get_max(const T& value) const {
  26424. return (+*this).max(value);
  26425. }
  26426. //! Pointwise max operator between two images.
  26427. /**
  26428. \param img Image used as the reference argument of the max operator.
  26429. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26430. \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
  26431. **/
  26432. template<typename t>
  26433. CImg<T>& max(const CImg<t>& img) {
  26434. const ulongT siz = size(), isiz = img.size();
  26435. if (siz && isiz) {
  26436. if (is_overlapped(img)) return max(+img);
  26437. T *ptrd = _data, *const ptre = _data + siz;
  26438. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26439. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26440. *ptrd = std::max((T)*(ptrs++),*ptrd);
  26441. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = std::max((T)*(ptrs++),*ptrd);
  26442. }
  26443. return *this;
  26444. }
  26445. //! Pointwise max operator between two images \newinstance.
  26446. template<typename t>
  26447. CImg<_cimg_Tt> get_max(const CImg<t>& img) const {
  26448. return CImg<_cimg_Tt>(*this,false).max(img);
  26449. }
  26450. //! Pointwise max operator between an image and an expression.
  26451. /**
  26452. \param expression Math formula as a C-string.
  26453. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26454. \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
  26455. **/
  26456. CImg<T>& max(const char *const expression) {
  26457. return max((+*this)._fill(expression,true,1,0,0,"max",this));
  26458. }
  26459. //! Pointwise max operator between an image and an expression \newinstance.
  26460. CImg<Tfloat> get_max(const char *const expression) const {
  26461. return CImg<Tfloat>(*this,false).max(expression);
  26462. }
  26463. //! Pointwise minabs operator between instance image and a value.
  26464. /**
  26465. \param val Value used as the reference argument of the minabs operator.
  26466. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26467. \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{val})\f$.
  26468. **/
  26469. CImg<T>& minabs(const T& value) {
  26470. if (is_empty()) return *this;
  26471. const T absvalue = cimg::abs(value);
  26472. cimg_openmp_for(*this,cimg::minabs(*ptr,value,absvalue),65536);
  26473. return *this;
  26474. }
  26475. //! Pointwise minabs operator between instance image and a value \newinstance.
  26476. CImg<T> get_minabs(const T& value) const {
  26477. return (+*this).minabs(value);
  26478. }
  26479. //! Pointwise minabs operator between two images.
  26480. /**
  26481. \param img Image used as the reference argument of the minabs operator.
  26482. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26483. \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
  26484. **/
  26485. template<typename t>
  26486. CImg<T>& minabs(const CImg<t>& img) {
  26487. const ulongT siz = size(), isiz = img.size();
  26488. if (siz && isiz) {
  26489. if (is_overlapped(img)) return minabs(+img);
  26490. T *ptrd = _data, *const ptre = _data + siz;
  26491. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26492. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26493. *ptrd = cimg::minabs((T)*(ptrs++),*ptrd);
  26494. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::minabs((T)*(ptrs++),*ptrd);
  26495. }
  26496. return *this;
  26497. }
  26498. //! Pointwise minabs operator between two images \newinstance.
  26499. template<typename t>
  26500. CImg<_cimg_Tt> get_minabs(const CImg<t>& img) const {
  26501. return CImg<_cimg_Tt>(*this,false).minabs(img);
  26502. }
  26503. //! Pointwise minabs operator between an image and an expression.
  26504. /**
  26505. \param expression Math formula as a C-string.
  26506. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26507. \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
  26508. **/
  26509. CImg<T>& minabs(const char *const expression) {
  26510. return minabs((+*this)._fill(expression,true,1,0,0,"minabs",this));
  26511. }
  26512. //! Pointwise minabs operator between an image and an expression \newinstance.
  26513. CImg<Tfloat> get_minabs(const char *const expression) const {
  26514. return CImg<Tfloat>(*this,false).minabs(expression);
  26515. }
  26516. //! Pointwise maxabs operator between instance image and a value.
  26517. /**
  26518. \param val Value used as the reference argument of the maxabs operator.
  26519. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26520. \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{val})\f$.
  26521. **/
  26522. CImg<T>& maxabs(const T& value) {
  26523. if (is_empty()) return *this;
  26524. const T absvalue = cimg::abs(value);
  26525. cimg_openmp_for(*this,cimg::maxabs(*ptr,value,absvalue),65536);
  26526. return *this;
  26527. }
  26528. //! Pointwise maxabs operator between instance image and a value \newinstance.
  26529. CImg<T> get_maxabs(const T& value) const {
  26530. return (+*this).maxabs(value);
  26531. }
  26532. //! Pointwise maxabs operator between two images.
  26533. /**
  26534. \param img Image used as the reference argument of the maxabs operator.
  26535. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26536. \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
  26537. **/
  26538. template<typename t>
  26539. CImg<T>& maxabs(const CImg<t>& img) {
  26540. const ulongT siz = size(), isiz = img.size();
  26541. if (siz && isiz) {
  26542. if (is_overlapped(img)) return maxabs(+img);
  26543. T *ptrd = _data, *const ptre = _data + siz;
  26544. if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
  26545. for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
  26546. *ptrd = cimg::maxabs((T)*(ptrs++),*ptrd);
  26547. for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::maxabs((T)*(ptrs++),*ptrd);
  26548. }
  26549. return *this;
  26550. }
  26551. //! Pointwise maxabs operator between two images \newinstance.
  26552. template<typename t>
  26553. CImg<_cimg_Tt> get_maxabs(const CImg<t>& img) const {
  26554. return CImg<_cimg_Tt>(*this,false).maxabs(img);
  26555. }
  26556. //! Pointwise maxabs operator between an image and an expression.
  26557. /**
  26558. \param expression Math formula as a C-string.
  26559. \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
  26560. \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
  26561. **/
  26562. CImg<T>& maxabs(const char *const expression) {
  26563. return maxabs((+*this)._fill(expression,true,1,0,0,"maxabs",this));
  26564. }
  26565. //! Pointwise maxabs operator between an image and an expression \newinstance.
  26566. CImg<Tfloat> get_maxabs(const char *const expression) const {
  26567. return CImg<Tfloat>(*this,false).maxabs(expression);
  26568. }
  26569. //! Return a reference to the minimum pixel value.
  26570. /**
  26571. **/
  26572. T& min() {
  26573. if (is_empty())
  26574. throw CImgInstanceException(_cimg_instance
  26575. "min(): Empty instance.",
  26576. cimg_instance);
  26577. T *ptr_min = _data;
  26578. T min_value = *ptr_min;
  26579. cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
  26580. return *ptr_min;
  26581. }
  26582. //! Return a reference to the minimum pixel value \const.
  26583. const T& min() const {
  26584. if (is_empty())
  26585. throw CImgInstanceException(_cimg_instance
  26586. "min(): Empty instance.",
  26587. cimg_instance);
  26588. const T *ptr_min = _data;
  26589. T min_value = *ptr_min;
  26590. cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
  26591. return *ptr_min;
  26592. }
  26593. //! Return a reference to the minimum pixel value in absolute value.
  26594. /**
  26595. **/
  26596. T& minabs() {
  26597. if (is_empty())
  26598. throw CImgInstanceException(_cimg_instance
  26599. "minabs(): Empty instance.",
  26600. cimg_instance);
  26601. T *ptr_minabs = _data;
  26602. T minabs_value = *ptr_minabs;
  26603. cimg_for(*this,ptrs,T) {
  26604. const T ma = cimg::abs(*ptrs);
  26605. if (ma<minabs_value) { minabs_value = ma; ptr_minabs = ptrs; }
  26606. }
  26607. return *ptr_minabs;
  26608. }
  26609. //! Return a reference to the minimum pixel value in absolute value \const.
  26610. const T& minabs() const {
  26611. if (is_empty())
  26612. throw CImgInstanceException(_cimg_instance
  26613. "minabs(): Empty instance.",
  26614. cimg_instance);
  26615. const T *ptr_minabs = _data;
  26616. T minabs_value = *ptr_minabs;
  26617. cimg_for(*this,ptrs,T) {
  26618. const T ma = cimg::abs(*ptrs);
  26619. if (ma<minabs_value) { minabs_value = ma; ptr_minabs = ptrs; }
  26620. }
  26621. return *ptr_minabs;
  26622. }
  26623. //! Return a reference to the maximum pixel value.
  26624. /**
  26625. **/
  26626. T& max() {
  26627. if (is_empty())
  26628. throw CImgInstanceException(_cimg_instance
  26629. "max(): Empty instance.",
  26630. cimg_instance);
  26631. T *ptr_max = _data;
  26632. T max_value = *ptr_max;
  26633. cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
  26634. return *ptr_max;
  26635. }
  26636. //! Return a reference to the maximum pixel value \const.
  26637. const T& max() const {
  26638. if (is_empty())
  26639. throw CImgInstanceException(_cimg_instance
  26640. "max(): Empty instance.",
  26641. cimg_instance);
  26642. const T *ptr_max = _data;
  26643. T max_value = *ptr_max;
  26644. cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
  26645. return *ptr_max;
  26646. }
  26647. //! Return a reference to the maximum pixel value in absolute value.
  26648. /**
  26649. **/
  26650. T& maxabs() {
  26651. if (is_empty())
  26652. throw CImgInstanceException(_cimg_instance
  26653. "maxabs(): Empty instance.",
  26654. cimg_instance);
  26655. T *ptr_maxabs = _data;
  26656. T maxabs_value = *ptr_maxabs;
  26657. cimg_for(*this,ptrs,T) {
  26658. const T ma = cimg::abs(*ptrs);
  26659. if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }
  26660. }
  26661. return *ptr_maxabs;
  26662. }
  26663. //! Return a reference to the maximum pixel value in absolute value \const.
  26664. const T& maxabs() const {
  26665. if (is_empty())
  26666. throw CImgInstanceException(_cimg_instance
  26667. "maxabs(): Empty instance.",
  26668. cimg_instance);
  26669. const T *ptr_maxabs = _data;
  26670. T maxabs_value = *ptr_maxabs;
  26671. cimg_for(*this,ptrs,T) {
  26672. const T ma = cimg::abs(*ptrs);
  26673. if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }
  26674. }
  26675. return *ptr_maxabs;
  26676. }
  26677. //! Return a reference to the minimum pixel value as well as the maximum pixel value.
  26678. /**
  26679. \param[out] max_val Maximum pixel value.
  26680. **/
  26681. template<typename t>
  26682. T& min_max(t& max_val) {
  26683. if (is_empty())
  26684. throw CImgInstanceException(_cimg_instance
  26685. "min_max(): Empty instance.",
  26686. cimg_instance);
  26687. T *ptr_min = _data;
  26688. T min_value = *ptr_min, max_value = min_value;
  26689. cimg_for(*this,ptrs,T) {
  26690. const T val = *ptrs;
  26691. if (val<min_value) { min_value = val; ptr_min = ptrs; }
  26692. if (val>max_value) max_value = val;
  26693. }
  26694. max_val = (t)max_value;
  26695. return *ptr_min;
  26696. }
  26697. //! Return a reference to the minimum pixel value as well as the maximum pixel value \const.
  26698. template<typename t>
  26699. const T& min_max(t& max_val) const {
  26700. if (is_empty())
  26701. throw CImgInstanceException(_cimg_instance
  26702. "min_max(): Empty instance.",
  26703. cimg_instance);
  26704. const T *ptr_min = _data;
  26705. T min_value = *ptr_min, max_value = min_value;
  26706. cimg_for(*this,ptrs,T) {
  26707. const T val = *ptrs;
  26708. if (val<min_value) { min_value = val; ptr_min = ptrs; }
  26709. if (val>max_value) max_value = val;
  26710. }
  26711. max_val = (t)max_value;
  26712. return *ptr_min;
  26713. }
  26714. //! Return a reference to the maximum pixel value as well as the minimum pixel value.
  26715. /**
  26716. \param[out] min_val Minimum pixel value.
  26717. **/
  26718. template<typename t>
  26719. T& max_min(t& min_val) {
  26720. if (is_empty())
  26721. throw CImgInstanceException(_cimg_instance
  26722. "max_min(): Empty instance.",
  26723. cimg_instance);
  26724. T *ptr_max = _data;
  26725. T max_value = *ptr_max, min_value = max_value;
  26726. cimg_for(*this,ptrs,T) {
  26727. const T val = *ptrs;
  26728. if (val>max_value) { max_value = val; ptr_max = ptrs; }
  26729. if (val<min_value) min_value = val;
  26730. }
  26731. min_val = (t)min_value;
  26732. return *ptr_max;
  26733. }
  26734. //! Return a reference to the maximum pixel value as well as the minimum pixel value \const.
  26735. template<typename t>
  26736. const T& max_min(t& min_val) const {
  26737. if (is_empty())
  26738. throw CImgInstanceException(_cimg_instance
  26739. "max_min(): Empty instance.",
  26740. cimg_instance);
  26741. const T *ptr_max = _data;
  26742. T max_value = *ptr_max, min_value = max_value;
  26743. cimg_for(*this,ptrs,T) {
  26744. const T val = *ptrs;
  26745. if (val>max_value) { max_value = val; ptr_max = ptrs; }
  26746. if (val<min_value) min_value = val;
  26747. }
  26748. min_val = (t)min_value;
  26749. return *ptr_max;
  26750. }
  26751. //! Return the kth smallest pixel value.
  26752. /**
  26753. \param k Rank of the smallest element searched.
  26754. **/
  26755. T kth_smallest(const ulongT k) const {
  26756. if (is_empty())
  26757. throw CImgInstanceException(_cimg_instance
  26758. "kth_smallest(): Empty instance.",
  26759. cimg_instance);
  26760. if (k>=size()) return max();
  26761. CImg<T> arr(*this,false);
  26762. ulongT l = 0, ir = size() - 1;
  26763. for ( ; ; ) {
  26764. if (ir<=l + 1) {
  26765. if (ir==l + 1 && arr[ir]<arr[l]) cimg::swap(arr[l],arr[ir]);
  26766. return arr[k];
  26767. } else {
  26768. const ulongT mid = (l + ir)>>1;
  26769. cimg::swap(arr[mid],arr[l + 1]);
  26770. if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]);
  26771. if (arr[l + 1]>arr[ir]) cimg::swap(arr[l + 1],arr[ir]);
  26772. if (arr[l]>arr[l + 1]) cimg::swap(arr[l],arr[l + 1]);
  26773. ulongT i = l + 1, j = ir;
  26774. const T pivot = arr[l + 1];
  26775. for ( ; ; ) {
  26776. do ++i; while (arr[i]<pivot);
  26777. do --j; while (arr[j]>pivot);
  26778. if (j<i) break;
  26779. cimg::swap(arr[i],arr[j]);
  26780. }
  26781. arr[l + 1] = arr[j];
  26782. arr[j] = pivot;
  26783. if (j>=k) ir = j - 1;
  26784. if (j<=k) l = i;
  26785. }
  26786. }
  26787. }
  26788. //! Return the median pixel value.
  26789. /**
  26790. **/
  26791. T median() const {
  26792. if (is_empty())
  26793. throw CImgInstanceException(_cimg_instance
  26794. "median(): Empty instance.",
  26795. cimg_instance);
  26796. const ulongT s = size();
  26797. switch (s) {
  26798. case 1 : return _data[0];
  26799. case 2 : return cimg::median(_data[0],_data[1]);
  26800. case 3 : return cimg::median(_data[0],_data[1],_data[2]);
  26801. case 5 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4]);
  26802. case 7 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6]);
  26803. case 9 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8]);
  26804. case 13 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8],
  26805. _data[9],_data[10],_data[11],_data[12]);
  26806. }
  26807. const T res = kth_smallest(s>>1);
  26808. return (s%2)?res:(T)((res + kth_smallest((s>>1) - 1))/2);
  26809. }
  26810. //! Return the product of all the pixel values.
  26811. /**
  26812. **/
  26813. double product() const {
  26814. if (is_empty()) return 0;
  26815. double res = 1;
  26816. cimg_for(*this,ptrs,T) res*=(double)*ptrs;
  26817. return res;
  26818. }
  26819. //! Return the sum of all the pixel values.
  26820. /**
  26821. **/
  26822. double sum() const {
  26823. double res = 0;
  26824. cimg_for(*this,ptrs,T) res+=(double)*ptrs;
  26825. return res;
  26826. }
  26827. //! Return the average pixel value.
  26828. /**
  26829. **/
  26830. double mean() const {
  26831. double res = 0;
  26832. cimg_for(*this,ptrs,T) res+=(double)*ptrs;
  26833. return res/size();
  26834. }
  26835. //! Return the variance of the pixel values.
  26836. /**
  26837. \param variance_method Method used to estimate the variance. Can be:
  26838. - \c 0: Second moment, computed as
  26839. \f$1/N \sum\limits_{k=1}^{N} (x_k - \bar x)^2 =
  26840. 1/N \left( \sum\limits_{k=1}^N x_k^2 - \left( \sum\limits_{k=1}^N x_k \right)^2 / N \right)\f$
  26841. with \f$ \bar x = 1/N \sum\limits_{k=1}^N x_k \f$.
  26842. - \c 1: Best unbiased estimator, computed as \f$\frac{1}{N - 1} \sum\limits_{k=1}^{N} (x_k - \bar x)^2 \f$.
  26843. - \c 2: Least median of squares.
  26844. - \c 3: Least trimmed of squares.
  26845. **/
  26846. double variance(const unsigned int variance_method=1) const {
  26847. double foo;
  26848. return variance_mean(variance_method,foo);
  26849. }
  26850. //! Return the variance as well as the average of the pixel values.
  26851. /**
  26852. \param variance_method Method used to estimate the variance (see variance(const unsigned int) const).
  26853. \param[out] mean Average pixel value.
  26854. **/
  26855. template<typename t>
  26856. double variance_mean(const unsigned int variance_method, t& mean) const {
  26857. if (is_empty())
  26858. throw CImgInstanceException(_cimg_instance
  26859. "variance_mean(): Empty instance.",
  26860. cimg_instance);
  26861. double variance = 0, average = 0;
  26862. const ulongT siz = size();
  26863. switch (variance_method) {
  26864. case 0 : { // Least mean square (standard definition)
  26865. double S = 0, S2 = 0;
  26866. cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
  26867. variance = (S2 - S*S/siz)/siz;
  26868. average = S;
  26869. } break;
  26870. case 1 : { // Least mean square (robust definition)
  26871. double S = 0, S2 = 0;
  26872. cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
  26873. variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
  26874. average = S;
  26875. } break;
  26876. case 2 : { // Least Median of Squares (MAD)
  26877. CImg<Tfloat> buf(*this,false);
  26878. buf.sort();
  26879. const ulongT siz2 = siz>>1;
  26880. const double med_i = (double)buf[siz2];
  26881. cimg_for(buf,ptrs,Tfloat) {
  26882. const double val = (double)*ptrs; *ptrs = (Tfloat)cimg::abs(val - med_i); average+=val;
  26883. }
  26884. buf.sort();
  26885. const double sig = (double)(1.4828*buf[siz2]);
  26886. variance = sig*sig;
  26887. } break;
  26888. default : { // Least trimmed of Squares
  26889. CImg<Tfloat> buf(*this,false);
  26890. const ulongT siz2 = siz>>1;
  26891. cimg_for(buf,ptrs,Tfloat) {
  26892. const double val = (double)*ptrs; (*ptrs)=(Tfloat)((*ptrs)*val); average+=val;
  26893. }
  26894. buf.sort();
  26895. double a = 0;
  26896. const Tfloat *ptrs = buf._data;
  26897. for (ulongT j = 0; j<siz2; ++j) a+=(double)*(ptrs++);
  26898. const double sig = (double)(2.6477*std::sqrt(a/siz2));
  26899. variance = sig*sig;
  26900. }
  26901. }
  26902. mean = (t)(average/siz);
  26903. return variance>0?variance:0;
  26904. }
  26905. //! Return estimated variance of the noise.
  26906. /**
  26907. \param variance_method Method used to compute the variance (see variance(const unsigned int) const).
  26908. \note Because of structures such as edges in images it is
  26909. recommended to use a robust variance estimation. The variance of the
  26910. noise is estimated by computing the variance of the Laplacian \f$(\Delta
  26911. I)^2 \f$ scaled by a factor \f$c\f$ insuring \f$ c E[(\Delta I)^2]=
  26912. \sigma^2\f$ where \f$\sigma\f$ is the noise variance.
  26913. **/
  26914. double variance_noise(const unsigned int variance_method=2) const {
  26915. if (is_empty())
  26916. throw CImgInstanceException(_cimg_instance
  26917. "variance_noise(): Empty instance.",
  26918. cimg_instance);
  26919. const ulongT siz = size();
  26920. if (!siz || !_data) return 0;
  26921. if (variance_method>1) { // Compute a scaled version of the Laplacian
  26922. CImg<Tdouble> tmp(*this,false);
  26923. if (_depth==1) {
  26924. const double cste = 1./std::sqrt(20.); // Depends on how the Laplacian is computed
  26925. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*262144 &&
  26926. _spectrum>=2))
  26927. cimg_forC(*this,c) {
  26928. CImg_3x3(I,T);
  26929. cimg_for3x3(*this,x,y,0,c,I,T) {
  26930. tmp(x,y,c) = cste*((double)Inc + (double)Ipc + (double)Icn +
  26931. (double)Icp - 4*(double)Icc);
  26932. }
  26933. }
  26934. } else {
  26935. const double cste = 1./std::sqrt(42.); // Depends on how the Laplacian is computed
  26936. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*262144 &&
  26937. _spectrum>=2))
  26938. cimg_forC(*this,c) {
  26939. CImg_3x3x3(I,T);
  26940. cimg_for3x3x3(*this,x,y,z,c,I,T) {
  26941. tmp(x,y,z,c) = cste*(
  26942. (double)Incc + (double)Ipcc + (double)Icnc + (double)Icpc +
  26943. (double)Iccn + (double)Iccp - 6*(double)Iccc);
  26944. }
  26945. }
  26946. }
  26947. return tmp.variance(variance_method);
  26948. }
  26949. // Version that doesn't need intermediate images.
  26950. double variance = 0, S = 0, S2 = 0;
  26951. if (_depth==1) {
  26952. const double cste = 1./std::sqrt(20.);
  26953. CImg_3x3(I,T);
  26954. cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,T) {
  26955. const double val = cste*((double)Inc + (double)Ipc +
  26956. (double)Icn + (double)Icp - 4*(double)Icc);
  26957. S+=val; S2+=val*val;
  26958. }
  26959. } else {
  26960. const double cste = 1./std::sqrt(42.);
  26961. CImg_3x3x3(I,T);
  26962. cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,T) {
  26963. const double val = cste *
  26964. ((double)Incc + (double)Ipcc + (double)Icnc +
  26965. (double)Icpc +
  26966. (double)Iccn + (double)Iccp - 6*(double)Iccc);
  26967. S+=val; S2+=val*val;
  26968. }
  26969. }
  26970. if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
  26971. else variance = (S2 - S*S/siz)/siz;
  26972. return variance>0?variance:0;
  26973. }
  26974. //! Compute the MSE (Mean-Squared Error) between two images.
  26975. /**
  26976. \param img Image used as the second argument of the MSE operator.
  26977. **/
  26978. template<typename t>
  26979. double MSE(const CImg<t>& img) const {
  26980. if (img.size()!=size())
  26981. throw CImgArgumentException(_cimg_instance
  26982. "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.",
  26983. cimg_instance,
  26984. img._width,img._height,img._depth,img._spectrum,img._data);
  26985. double vMSE = 0;
  26986. const t* ptr2 = img._data;
  26987. cimg_for(*this,ptr1,T) {
  26988. const double diff = (double)*ptr1 - (double)*(ptr2++);
  26989. vMSE+=diff*diff;
  26990. }
  26991. const ulongT siz = img.size();
  26992. if (siz) vMSE/=siz;
  26993. return vMSE;
  26994. }
  26995. //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images.
  26996. /**
  26997. \param img Image used as the second argument of the PSNR operator.
  26998. \param max_value Maximum theoretical value of the signal.
  26999. **/
  27000. template<typename t>
  27001. double PSNR(const CImg<t>& img, const double max_value=255) const {
  27002. const double vMSE = (double)std::sqrt(MSE(img));
  27003. return (vMSE!=0)?(double)(20*std::log10(max_value/vMSE)):(double)(cimg::type<double>::max());
  27004. }
  27005. //! Evaluate math formula.
  27006. /**
  27007. \param expression Math formula, as a C-string.
  27008. \param x Value of the pre-defined variable \c x.
  27009. \param y Value of the pre-defined variable \c y.
  27010. \param z Value of the pre-defined variable \c z.
  27011. \param c Value of the pre-defined variable \c c.
  27012. \param list_images A list of images attached to the specified math formula.
  27013. **/
  27014. double eval(const char *const expression,
  27015. const double x=0, const double y=0, const double z=0, const double c=0,
  27016. CImgList<T> *const list_images=0) {
  27017. return _eval(this,expression,x,y,z,c,list_images);
  27018. }
  27019. //! Evaluate math formula \const.
  27020. double eval(const char *const expression,
  27021. const double x=0, const double y=0, const double z=0, const double c=0,
  27022. CImgList<T> *const list_images=0) const {
  27023. return _eval(0,expression,x,y,z,c,list_images);
  27024. }
  27025. // Fast function to pre-evaluate common expressions.
  27026. // (return 'true' in case of success, and set value of 'res').
  27027. template<typename t>
  27028. bool __eval(const char *const expression, t &res) const {
  27029. if (!expression || !*expression) { res = (t)0; return true; }
  27030. const char c = *expression;
  27031. bool is_success = false;
  27032. char sep, end;
  27033. double val,val2;
  27034. int err;
  27035. if ((c>='0' && c<='9') || c=='.') { // Possible value
  27036. if (!expression[1]) { // Single digit
  27037. res = (t)(c - '0');
  27038. is_success = true;
  27039. } else if ((err = std::sscanf(expression,"%lf %c%lf %c",&val,&sep,&val2,&end))==1) { // Single value
  27040. res = (t)val;
  27041. is_success = true;
  27042. } else if (err==3) { // Value1 Operator Value2
  27043. switch (sep) {
  27044. case '+' : res = (t)(val + val2); is_success = true; break;
  27045. case '-' : res = (t)(val - val2); is_success = true; break;
  27046. case '*' : res = (t)(val*val2); is_success = true; break;
  27047. case '/' : res = (t)(val/val2); is_success = true; break;
  27048. case '%' : res = (t)cimg::mod(val,val2); is_success = true; break;
  27049. case '&' : res = (t)((long)val & (long)val2); is_success = true; break;
  27050. case '|' : res = (t)((long)val | (long)val2); is_success = true; break;
  27051. case '>' : res = (t)(val>val2); is_success = true; break;
  27052. case '<' : res = (t)(val<val2); is_success = true; break;
  27053. case ';' : res = (t)val2; is_success = true; break;
  27054. case '^' : res = (t)std::pow(val,val2); is_success = true; break;
  27055. }
  27056. }
  27057. } else if ((c=='+' || c=='-' || c=='!') && // +Value, -Value or !Value
  27058. (((sep = expression[1])>='0' && sep<='9') || sep=='.')) {
  27059. if (!expression[2]) { // [+-!] + Single digit
  27060. const int ival = sep - '0';
  27061. res = (t)(c=='+'?ival:c=='-'?-ival:!ival);
  27062. is_success = true;
  27063. } else if ((err = std::sscanf(expression + 1,"%lf %c%lf %c",&val,&sep,&val2,&end))==1) { // [+-!] Single value
  27064. res = (t)(c=='+'?val:c=='-'?-val:(double)!val);
  27065. is_success = true;
  27066. } else if (err==3) { // [+-!] Value1 Operator Value2
  27067. const double val1 = c=='+'?val:c=='-'?-val:(double)!val;
  27068. switch (sep) {
  27069. case '+' : res = (t)(val1 + val2); is_success = true; break;
  27070. case '-' : res = (t)(val1 - val2); is_success = true; break;
  27071. case '*' : res = (t)(val1*val2); is_success = true; break;
  27072. case '/' : res = (t)(val1/val2); is_success = true; break;
  27073. case '%' : res = (t)cimg::mod(val1,val2); is_success = true; break;
  27074. case '&' : res = (t)((long)val1 & (long)val2); is_success = true; break;
  27075. case '|' : res = (t)((long)val1 | (long)val2); is_success = true; break;
  27076. case '>' : res = (t)(val1>val2); is_success = true; break;
  27077. case '<' : res = (t)(val1<val2); is_success = true; break;
  27078. case ';' : res = (t)val2; is_success = true; break;
  27079. case '^' : val = std::pow(val,val2); res = (t)(c=='+'?val:c=='-'?-val:!val); is_success = true; break;
  27080. }
  27081. }
  27082. } else if (!expression[1]) switch (*expression) { // Other common single-char expressions
  27083. case 'w' : res = (t)_width; is_success = true; break;
  27084. case 'h' : res = (t)_height; is_success = true; break;
  27085. case 'd' : res = (t)_depth; is_success = true; break;
  27086. case 's' : res = (t)_spectrum; is_success = true; break;
  27087. case 'r' : res = (t)_is_shared; is_success = true; break;
  27088. }
  27089. return is_success;
  27090. }
  27091. double _eval(CImg<T> *const img_output, const char *const expression,
  27092. const double x, const double y, const double z, const double c,
  27093. CImgList<T> *const list_images) const {
  27094. if (!expression || !*expression) return 0;
  27095. double _val = 0;
  27096. if (__eval(expression,_val)) return _val;
  27097. _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
  27098. *expression=='*' || *expression==':'),"eval",
  27099. *this,img_output,list_images,false);
  27100. mp.begin_t();
  27101. const double val = mp(x,y,z,c);
  27102. mp.end_t();
  27103. mp.end();
  27104. return val;
  27105. }
  27106. //! Evaluate math formula.
  27107. /**
  27108. \param[out] output Contains values of output vector returned by the evaluated expression
  27109. (or is empty if the returned type is scalar).
  27110. \param expression Math formula, as a C-string.
  27111. \param x Value of the pre-defined variable \c x.
  27112. \param y Value of the pre-defined variable \c y.
  27113. \param z Value of the pre-defined variable \c z.
  27114. \param c Value of the pre-defined variable \c c.
  27115. \param list_images A list of input images attached to the specified math formula.
  27116. **/
  27117. template<typename t>
  27118. void eval(CImg<t> &output, const char *const expression,
  27119. const double x=0, const double y=0, const double z=0, const double c=0,
  27120. CImgList<T> *const list_images=0) {
  27121. _eval(output,this,expression,x,y,z,c,list_images);
  27122. }
  27123. //! Evaluate math formula \const.
  27124. template<typename t>
  27125. void eval(CImg<t>& output, const char *const expression,
  27126. const double x=0, const double y=0, const double z=0, const double c=0,
  27127. CImgList<T> *const list_images=0) const {
  27128. _eval(output,0,expression,x,y,z,c,list_images);
  27129. }
  27130. template<typename t>
  27131. void _eval(CImg<t>& output, CImg<T> *const img_output, const char *const expression,
  27132. const double x, const double y, const double z, const double c,
  27133. CImgList<T> *const list_images) const {
  27134. if (!expression || !*expression) { output.assign(1); *output = 0; return; }
  27135. double _val = 0;
  27136. if (__eval(expression,_val)) { output.assign(1); *output = _val; return; }
  27137. _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
  27138. *expression=='*' || *expression==':'),"eval",
  27139. *this,img_output,list_images,false);
  27140. output.assign(1,std::max(1U,mp.result_dim));
  27141. mp.begin_t();
  27142. mp(x,y,z,c,output._data);
  27143. mp.end_t();
  27144. mp.end();
  27145. }
  27146. //! Evaluate math formula on a set of variables.
  27147. /**
  27148. \param expression Math formula, as a C-string.
  27149. \param xyzc Set of values (x,y,z,c) used for the evaluation.
  27150. \param list_images A list of input images attached to the specified math formula.
  27151. **/
  27152. template<typename t>
  27153. CImg<doubleT> eval(const char *const expression, const CImg<t>& xyzc,
  27154. CImgList<T> *const list_images=0) {
  27155. return _eval(this,expression,xyzc,list_images);
  27156. }
  27157. //! Evaluate math formula on a set of variables \const.
  27158. template<typename t>
  27159. CImg<doubleT> eval(const char *const expression, const CImg<t>& xyzc,
  27160. CImgList<T> *const list_images=0) const {
  27161. return _eval(0,expression,xyzc,list_images);
  27162. }
  27163. template<typename t>
  27164. CImg<doubleT> _eval(CImg<T> *const output, const char *const expression, const CImg<t>& xyzc,
  27165. CImgList<T> *const list_images=0) const {
  27166. CImg<doubleT> res(1,xyzc.size()/4);
  27167. if (!expression || !*expression) return res.fill(0);
  27168. _cimg_math_parser mp(expression,"eval",*this,output,list_images,false);
  27169. #if cimg_use_openmp!=0
  27170. cimg_pragma_openmp(parallel if (res._height>=512))
  27171. {
  27172. _cimg_math_parser
  27173. *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
  27174. &lmp = *_mp;
  27175. cimg_pragma_openmp(barrier)
  27176. lmp.begin_t();
  27177. cimg_pragma_openmp(for)
  27178. for (int i = 0; i<res.height(); ++i) {
  27179. const unsigned int i4 = 4*i;
  27180. const double
  27181. x = (double)xyzc[i4], y = (double)xyzc[i4 + 1],
  27182. z = (double)xyzc[i4 + 2], c = (double)xyzc[i4 + 3];
  27183. res[i] = lmp(x,y,z,c);
  27184. }
  27185. lmp.end_t();
  27186. cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
  27187. if (&lmp!=&mp) delete &lmp;
  27188. }
  27189. #else
  27190. mp.begin_t();
  27191. const t *ps = xyzc._data;
  27192. cimg_for(res,pd,double) {
  27193. const double x = (double)*(ps++), y = (double)*(ps++), z = (double)*(ps++), c = (double)*(ps++);
  27194. *pd = mp(x,y,z,c);
  27195. }
  27196. mp.end_t();
  27197. #endif
  27198. mp.end();
  27199. return res;
  27200. }
  27201. //! Compute statistics vector from the pixel values.
  27202. /**
  27203. \param variance_method Method used to compute the variance (see variance(const unsigned int) const).
  27204. \return Statistics vector as
  27205. <tt>[min, max, mean, variance, xmin, ymin, zmin, cmin, xmax, ymax, zmax, cmax, sum, product]</tt>.
  27206. **/
  27207. CImg<Tdouble> get_stats(const unsigned int variance_method=1) const {
  27208. if (is_empty()) return CImg<doubleT>();
  27209. const ulongT siz = size();
  27210. const longT off_end = (longT)siz;
  27211. double S = 0, S2 = 0, P = 1;
  27212. longT offm = 0, offM = 0;
  27213. T m = *_data, M = m;
  27214. cimg_pragma_openmp(parallel reduction(+:S,S2) reduction(*:P) cimg_openmp_if_size(siz,131072)) {
  27215. longT loffm = 0, loffM = 0;
  27216. T lm = *_data, lM = lm;
  27217. cimg_pragma_openmp(for)
  27218. for (longT off = 0; off<off_end; ++off) {
  27219. const T val = _data[off];
  27220. const double _val = (double)val;
  27221. if (val<lm) { lm = val; loffm = off; }
  27222. if (val>lM) { lM = val; loffM = off; }
  27223. S+=_val;
  27224. S2+=_val*_val;
  27225. P*=_val;
  27226. }
  27227. cimg_pragma_openmp(critical(get_stats)) {
  27228. if (lm<m || (lm==m && loffm<offm)) { m = lm; offm = loffm; }
  27229. if (lM>M || (lM==M && loffM<offM)) { M = lM; offM = loffM; }
  27230. }
  27231. }
  27232. const double
  27233. mean_value = S/siz,
  27234. _variance_value = variance_method==0?(S2 - S*S/siz)/siz:
  27235. (variance_method==1?(siz>1?(S2 - S*S/siz)/(siz - 1):0):
  27236. variance(variance_method)),
  27237. variance_value = _variance_value>0?_variance_value:0;
  27238. int
  27239. xm = 0, ym = 0, zm = 0, cm = 0,
  27240. xM = 0, yM = 0, zM = 0, cM = 0;
  27241. contains(_data[offm],xm,ym,zm,cm);
  27242. contains(_data[offM],xM,yM,zM,cM);
  27243. return CImg<Tdouble>(1,14).fill((double)m,(double)M,mean_value,variance_value,
  27244. (double)xm,(double)ym,(double)zm,(double)cm,
  27245. (double)xM,(double)yM,(double)zM,(double)cM,
  27246. S,P);
  27247. }
  27248. //! Compute statistics vector from the pixel values \inplace.
  27249. CImg<T>& stats(const unsigned int variance_method=1) {
  27250. return get_stats(variance_method).move_to(*this);
  27251. }
  27252. //@}
  27253. //-------------------------------------
  27254. //
  27255. //! \name Vector / Matrix Operations
  27256. //@{
  27257. //-------------------------------------
  27258. //! Compute norm of the image, viewed as a matrix.
  27259. /**
  27260. \param magnitude_type Norm type. Can be:
  27261. - \c -1: Linf-norm
  27262. - \c 0: L0-norm
  27263. - \c 1: L1-norm
  27264. - \c 2: L2-norm
  27265. **/
  27266. double magnitude(const int magnitude_type=2) const {
  27267. if (is_empty())
  27268. throw CImgInstanceException(_cimg_instance
  27269. "magnitude(): Empty instance.",
  27270. cimg_instance);
  27271. const ulongT siz = size();
  27272. double res = 0;
  27273. switch (magnitude_type) {
  27274. case -1 : {
  27275. cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }
  27276. } break;
  27277. case 1 : {
  27278. cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
  27279. for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::abs(_data[off]);
  27280. } break;
  27281. default : {
  27282. cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
  27283. for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::sqr(_data[off]);
  27284. res = (double)std::sqrt(res);
  27285. }
  27286. }
  27287. return res;
  27288. }
  27289. //! Compute the trace of the image, viewed as a matrix.
  27290. /**
  27291. **/
  27292. double trace() const {
  27293. if (is_empty())
  27294. throw CImgInstanceException(_cimg_instance
  27295. "trace(): Empty instance.",
  27296. cimg_instance);
  27297. double res = 0;
  27298. cimg_forX(*this,k) res+=(double)(*this)(k,k);
  27299. return res;
  27300. }
  27301. //! Compute the determinant of the image, viewed as a matrix.
  27302. /**
  27303. **/
  27304. double det() const {
  27305. if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1)
  27306. throw CImgInstanceException(_cimg_instance
  27307. "det(): Instance is not a square matrix.",
  27308. cimg_instance);
  27309. switch (_width) {
  27310. case 1 : return (double)((*this)(0,0));
  27311. case 2 : return (double)((*this)(0,0))*(double)((*this)(1,1)) - (double)((*this)(0,1))*(double)((*this)(1,0));
  27312. case 3 : {
  27313. const double
  27314. a = (double)_data[0], d = (double)_data[1], g = (double)_data[2],
  27315. b = (double)_data[3], e = (double)_data[4], h = (double)_data[5],
  27316. c = (double)_data[6], f = (double)_data[7], i = (double)_data[8];
  27317. return i*a*e - a*h*f - i*b*d + b*g*f + c*d*h - c*g*e;
  27318. }
  27319. default : {
  27320. CImg<Tfloat> lu(*this,false);
  27321. CImg<uintT> indx;
  27322. bool d;
  27323. lu._LU(indx,d);
  27324. double res = d?(double)1:(double)-1;
  27325. cimg_forX(lu,i) res*=lu(i,i);
  27326. return res;
  27327. }
  27328. }
  27329. }
  27330. //! Compute the dot product between instance and argument, viewed as matrices.
  27331. /**
  27332. \param img Image used as a second argument of the dot product.
  27333. **/
  27334. template<typename t>
  27335. double dot(const CImg<t>& img) const {
  27336. const ulongT nb = std::min(size(),img.size());
  27337. double res = 0;
  27338. cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(nb,8192))
  27339. for (longT off = 0; off<(longT)nb; ++off) res+=(double)_data[off]*(double)img[off];
  27340. return res;
  27341. }
  27342. //! Get vector-valued pixel located at specified position.
  27343. /**
  27344. \param x X-coordinate of the pixel value.
  27345. \param y Y-coordinate of the pixel value.
  27346. \param z Z-coordinate of the pixel value.
  27347. **/
  27348. CImg<T> get_vector_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const {
  27349. CImg<T> res;
  27350. if (res._height!=_spectrum) res.assign(1,_spectrum);
  27351. const ulongT whd = (ulongT)_width*_height*_depth;
  27352. const T *ptrs = data(x,y,z);
  27353. T *ptrd = res._data;
  27354. cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  27355. return res;
  27356. }
  27357. //! Get (square) matrix-valued pixel located at specified position.
  27358. /**
  27359. \param x X-coordinate of the pixel value.
  27360. \param y Y-coordinate of the pixel value.
  27361. \param z Z-coordinate of the pixel value.
  27362. \note - The spectrum() of the image must be a square.
  27363. **/
  27364. CImg<T> get_matrix_at(const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) const {
  27365. const int n = (int)cimg::round(std::sqrt((double)_spectrum));
  27366. const T *ptrs = data(x,y,z,0);
  27367. const ulongT whd = (ulongT)_width*_height*_depth;
  27368. CImg<T> res(n,n);
  27369. T *ptrd = res._data;
  27370. cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
  27371. return res;
  27372. }
  27373. //! Get tensor-valued pixel located at specified position.
  27374. /**
  27375. \param x X-coordinate of the pixel value.
  27376. \param y Y-coordinate of the pixel value.
  27377. \param z Z-coordinate of the pixel value.
  27378. **/
  27379. CImg<T> get_tensor_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const {
  27380. const T *ptrs = data(x,y,z,0);
  27381. const ulongT whd = (ulongT)_width*_height*_depth;
  27382. if (_spectrum==6)
  27383. return tensor(*ptrs,*(ptrs + whd),*(ptrs + 2*whd),*(ptrs + 3*whd),*(ptrs + 4*whd),*(ptrs + 5*whd));
  27384. if (_spectrum==3)
  27385. return tensor(*ptrs,*(ptrs + whd),*(ptrs + 2*whd));
  27386. return tensor(*ptrs);
  27387. }
  27388. //! Set vector-valued pixel at specified position.
  27389. /**
  27390. \param vec Vector to put on the instance image.
  27391. \param x X-coordinate of the pixel value.
  27392. \param y Y-coordinate of the pixel value.
  27393. \param z Z-coordinate of the pixel value.
  27394. **/
  27395. template<typename t>
  27396. CImg<T>& set_vector_at(const CImg<t>& vec, const unsigned int x, const unsigned int y=0, const unsigned int z=0) {
  27397. if (x<_width && y<_height && z<_depth) {
  27398. const t *ptrs = vec._data;
  27399. const ulongT whd = (ulongT)_width*_height*_depth;
  27400. T *ptrd = data(x,y,z);
  27401. for (unsigned int k = std::min((unsigned int)vec.size(),_spectrum); k; --k) {
  27402. *ptrd = (T)*(ptrs++); ptrd+=whd;
  27403. }
  27404. }
  27405. return *this;
  27406. }
  27407. //! Set (square) matrix-valued pixel at specified position.
  27408. /**
  27409. \param mat Matrix to put on the instance image.
  27410. \param x X-coordinate of the pixel value.
  27411. \param y Y-coordinate of the pixel value.
  27412. \param z Z-coordinate of the pixel value.
  27413. **/
  27414. template<typename t>
  27415. CImg<T>& set_matrix_at(const CImg<t>& mat, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) {
  27416. return set_vector_at(mat,x,y,z);
  27417. }
  27418. //! Set tensor-valued pixel at specified position.
  27419. /**
  27420. \param ten Tensor to put on the instance image.
  27421. \param x X-coordinate of the pixel value.
  27422. \param y Y-coordinate of the pixel value.
  27423. \param z Z-coordinate of the pixel value.
  27424. **/
  27425. template<typename t>
  27426. CImg<T>& set_tensor_at(const CImg<t>& ten, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) {
  27427. T *ptrd = data(x,y,z,0);
  27428. const ulongT siz = (ulongT)_width*_height*_depth;
  27429. if (ten._height==2) {
  27430. *ptrd = (T)ten[0]; ptrd+=siz;
  27431. *ptrd = (T)ten[1]; ptrd+=siz;
  27432. *ptrd = (T)ten[3];
  27433. }
  27434. else {
  27435. *ptrd = (T)ten[0]; ptrd+=siz;
  27436. *ptrd = (T)ten[1]; ptrd+=siz;
  27437. *ptrd = (T)ten[2]; ptrd+=siz;
  27438. *ptrd = (T)ten[4]; ptrd+=siz;
  27439. *ptrd = (T)ten[5]; ptrd+=siz;
  27440. *ptrd = (T)ten[8];
  27441. }
  27442. return *this;
  27443. }
  27444. //! Resize image to become a diagonal matrix.
  27445. /**
  27446. \note Transform the image as a diagonal matrix so that each of its initial value becomes a diagonal coefficient.
  27447. **/
  27448. CImg<T>& diagonal() {
  27449. return get_diagonal().move_to(*this);
  27450. }
  27451. //! Resize image to become a diagonal matrix \newinstance.
  27452. CImg<T> get_diagonal() const {
  27453. if (is_empty()) return *this;
  27454. const unsigned int siz = (unsigned int)size();
  27455. CImg<T> res(siz,siz,1,1,0);
  27456. cimg_foroff(*this,off) res((unsigned int)off,(unsigned int)off) = (*this)[off];
  27457. return res;
  27458. }
  27459. //! Replace the image by an identity matrix.
  27460. /**
  27461. \note If the instance image is not square, it is resized to a square matrix using its maximum
  27462. dimension as a reference.
  27463. **/
  27464. CImg<T>& identity_matrix() {
  27465. return identity_matrix(std::max(_width,_height)).move_to(*this);
  27466. }
  27467. //! Replace the image by an identity matrix \newinstance.
  27468. CImg<T> get_identity_matrix() const {
  27469. return identity_matrix(std::max(_width,_height));
  27470. }
  27471. //! Fill image with a linear sequence of values.
  27472. /**
  27473. \param a0 Starting value of the sequence.
  27474. \param a1 Ending value of the sequence.
  27475. **/
  27476. CImg<T>& sequence(const T& a0, const T& a1) {
  27477. if (is_empty()) return *this;
  27478. const ulongT siz = size() - 1;
  27479. T* ptr = _data;
  27480. if (siz) {
  27481. const double delta = (double)a1 - (double)a0;
  27482. cimg_foroff(*this,l) *(ptr++) = (T)(a0 + delta*l/siz);
  27483. } else *ptr = a0;
  27484. return *this;
  27485. }
  27486. //! Fill image with a linear sequence of values \newinstance.
  27487. CImg<T> get_sequence(const T& a0, const T& a1) const {
  27488. return (+*this).sequence(a0,a1);
  27489. }
  27490. //! Transpose the image, viewed as a matrix.
  27491. /**
  27492. \note Equivalent to \code permute_axes("yxzc"); \endcode.
  27493. **/
  27494. CImg<T>& transpose() {
  27495. if (_width==1) { _width = _height; _height = 1; return *this; }
  27496. if (_height==1) { _height = _width; _width = 1; return *this; }
  27497. if (_width==_height) {
  27498. cimg_forYZC(*this,y,z,c) for (int x = y; x<width(); ++x) cimg::swap((*this)(x,y,z,c),(*this)(y,x,z,c));
  27499. return *this;
  27500. }
  27501. return get_transpose().move_to(*this);
  27502. }
  27503. //! Transpose the image, viewed as a matrix \newinstance.
  27504. CImg<T> get_transpose() const {
  27505. return get_permute_axes("yxzc");
  27506. }
  27507. //! Compute the cross product between two \c 1x3 images, viewed as 3D vectors.
  27508. /**
  27509. \param img Image used as the second argument of the cross product.
  27510. \note The first argument of the cross product is \c *this.
  27511. **/
  27512. template<typename t>
  27513. CImg<T>& cross(const CImg<t>& img) {
  27514. if (_width!=1 || _height<3 || img._width!=1 || img._height<3)
  27515. throw CImgInstanceException(_cimg_instance
  27516. "cross(): Instance and/or specified image (%u,%u,%u,%u,%p) are not 3D vectors.",
  27517. cimg_instance,
  27518. img._width,img._height,img._depth,img._spectrum,img._data);
  27519. const T x = (*this)[0], y = (*this)[1], z = (*this)[2];
  27520. (*this)[0] = (T)(y*img[2] - z*img[1]);
  27521. (*this)[1] = (T)(z*img[0] - x*img[2]);
  27522. (*this)[2] = (T)(x*img[1] - y*img[0]);
  27523. return *this;
  27524. }
  27525. //! Compute the cross product between two \c 1x3 images, viewed as 3D vectors \newinstance.
  27526. template<typename t>
  27527. CImg<_cimg_Tt> get_cross(const CImg<t>& img) const {
  27528. return CImg<_cimg_Tt>(*this).cross(img);
  27529. }
  27530. //! Invert the instance image, viewed as a matrix.
  27531. /**
  27532. \param use_LU Choose the inverting algorithm. Can be:
  27533. - \c true: LU-based matrix inversion.
  27534. - \c false: SVD-based matrix inversion.
  27535. **/
  27536. CImg<T>& invert(const bool use_LU=true) {
  27537. if (_width!=_height || _depth!=1 || _spectrum!=1)
  27538. throw CImgInstanceException(_cimg_instance
  27539. "invert(): Instance is not a square matrix.",
  27540. cimg_instance);
  27541. const double dete = _width>3?-1.:det();
  27542. if (dete!=0. && _width==2) {
  27543. const double
  27544. a = _data[0], c = _data[1],
  27545. b = _data[2], d = _data[3];
  27546. _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete);
  27547. _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete);
  27548. } else if (dete!=0. && _width==3) {
  27549. const double
  27550. a = _data[0], d = _data[1], g = _data[2],
  27551. b = _data[3], e = _data[4], h = _data[5],
  27552. c = _data[6], f = _data[7], i = _data[8];
  27553. _data[0] = (T)((i*e - f*h)/dete), _data[1] = (T)((g*f - i*d)/dete), _data[2] = (T)((d*h - g*e)/dete);
  27554. _data[3] = (T)((h*c - i*b)/dete), _data[4] = (T)((i*a - c*g)/dete), _data[5] = (T)((g*b - a*h)/dete);
  27555. _data[6] = (T)((b*f - e*c)/dete), _data[7] = (T)((d*c - a*f)/dete), _data[8] = (T)((a*e - d*b)/dete);
  27556. } else {
  27557. #ifdef cimg_use_lapack
  27558. int INFO = (int)use_LU, N = _width, LWORK = 4*N, *const IPIV = new int[N];
  27559. Tfloat
  27560. *const lapA = new Tfloat[N*N],
  27561. *const WORK = new Tfloat[LWORK];
  27562. cimg_forXY(*this,k,l) lapA[k*N + l] = (Tfloat)((*this)(k,l));
  27563. cimg::getrf(N,lapA,IPIV,INFO);
  27564. if (INFO)
  27565. cimg::warn(_cimg_instance
  27566. "invert(): LAPACK function dgetrf_() returned error code %d.",
  27567. cimg_instance,
  27568. INFO);
  27569. else {
  27570. cimg::getri(N,lapA,IPIV,WORK,LWORK,INFO);
  27571. if (INFO)
  27572. cimg::warn(_cimg_instance
  27573. "invert(): LAPACK function dgetri_() returned error code %d.",
  27574. cimg_instance,
  27575. INFO);
  27576. }
  27577. if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)(lapA[k*N + l]); else fill(0);
  27578. delete[] IPIV; delete[] lapA; delete[] WORK;
  27579. #else
  27580. if (use_LU) { // LU-based
  27581. CImg<Tfloat> A(*this,false), indx;
  27582. bool d;
  27583. A._LU(indx,d);
  27584. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,16*16))
  27585. cimg_forX(*this,j) {
  27586. CImg<Tfloat> col(1,_width,1,1,0);
  27587. col(j) = 1;
  27588. col._solve(A,indx);
  27589. cimg_forX(*this,i) (*this)(j,i) = (T)col(i);
  27590. }
  27591. } else pseudoinvert(false); // SVD-based
  27592. #endif
  27593. }
  27594. return *this;
  27595. }
  27596. //! Invert the instance image, viewed as a matrix \newinstance.
  27597. CImg<Tfloat> get_invert(const bool use_LU=true) const {
  27598. return CImg<Tfloat>(*this,false).invert(use_LU);
  27599. }
  27600. //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix.
  27601. /**
  27602. **/
  27603. CImg<T>& pseudoinvert(const bool use_LU=false) {
  27604. return get_pseudoinvert(use_LU).move_to(*this);
  27605. }
  27606. //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix \newinstance.
  27607. CImg<Tfloat> get_pseudoinvert(const bool use_LU=false) const {
  27608. // LU-based method.
  27609. if (use_LU) {
  27610. CImg<Tfloat> AtA(width(),width());
  27611. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,128*128))
  27612. cimg_forY(AtA,i)
  27613. for (int j = 0; j<=i; ++j) {
  27614. double res = 0;
  27615. cimg_forY(*this,k) res+=(*this)(i,k)*(*this)(j,k);
  27616. AtA(j,i) = AtA(i,j) = (Tfloat)res;
  27617. }
  27618. AtA.invert(true);
  27619. return AtA*get_transpose();
  27620. }
  27621. // SVD-based method.
  27622. CImg<Tfloat> U, S, V;
  27623. SVD(U,S,V,false);
  27624. const Tfloat epsilon = (sizeof(Tfloat)<=4?5.96e-8f:1.11e-16f)*std::max(_width,_height)*S.max();
  27625. cimg_forX(V,x) {
  27626. const Tfloat s = S(x), invs = s>epsilon?1/s:0;
  27627. cimg_forY(V,y) V(x,y)*=invs;
  27628. }
  27629. return V*U.transpose();
  27630. }
  27631. //! Solve a system of linear equations.
  27632. /**
  27633. \param A Matrix of the linear system.
  27634. \param use_LU In case of non square system (least-square solution),
  27635. choose between SVD-based (\c false) or LU-based (\c true) method.
  27636. LU method is faster for large matrices, but numerically less stable.
  27637. \note Solve \c AX = B where \c B=*this.
  27638. **/
  27639. template<typename t>
  27640. CImg<T>& solve(const CImg<t>& A, const bool use_LU=false) {
  27641. if (_depth!=1 || _spectrum!=1 || _height!=A._height || A._depth!=1 || A._spectrum!=1)
  27642. throw CImgArgumentException(_cimg_instance
  27643. "solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have "
  27644. "incompatible dimensions.",
  27645. cimg_instance,
  27646. A._width,A._height,A._depth,A._spectrum,A._data);
  27647. typedef _cimg_Ttfloat Ttfloat;
  27648. if (A.size()==1) return (*this)/=A[0];
  27649. if (A._width==2 && A._height==2 && _height==2) { // 2x2 linear system
  27650. const double a = (double)A[0], b = (double)A[1], c = (double)A[2], d = (double)A[3],
  27651. fa = std::fabs(a), fb = std::fabs(b), fc = std::fabs(c), fd = std::fabs(d),
  27652. det = a*d - b*c, fM = cimg::max(fa,fb,fc,fd);
  27653. if (fM==fa)
  27654. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
  27655. cimg_forX(*this,k) {
  27656. const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), y = (a*v - c*u)/det;
  27657. (*this)(k,0) = (T)((u - b*y)/a); (*this)(k,1) = (T)y;
  27658. } else if (fM==fc)
  27659. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
  27660. cimg_forX(*this,k) {
  27661. const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), y = (a*v - c*u)/det;
  27662. (*this)(k,0) = (T)((v - d*y)/c); (*this)(k,1) = (T)y;
  27663. } else if (fM==fb)
  27664. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
  27665. cimg_forX(*this,k) {
  27666. const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), x = (d*u - b*v)/det;
  27667. (*this)(k,0) = (T)x; (*this)(k,1) = (T)((u - a*x)/b);
  27668. } else
  27669. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
  27670. cimg_forX(*this,k) {
  27671. const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), x = (d*u - b*v)/det;
  27672. (*this)(k,0) = (T)x; (*this)(k,1) = (T)((v - c*x)/d);
  27673. }
  27674. return *this;
  27675. }
  27676. if (A._width==A._height) { // Square linear system
  27677. #ifdef cimg_use_lapack
  27678. char TRANS = 'N';
  27679. int INFO, N = _height, LWORK = 4*N, *const IPIV = new int[N];
  27680. Ttfloat
  27681. *const lapA = new Ttfloat[N*N],
  27682. *const lapB = new Ttfloat[N],
  27683. *const WORK = new Ttfloat[LWORK];
  27684. cimg_forXY(A,k,l) lapA[k*N + l] = (Ttfloat)(A(k,l));
  27685. cimg_forX(*this,i) {
  27686. cimg_forY(*this,j) lapB[j] = (Ttfloat)((*this)(i,j));
  27687. cimg::getrf(N,lapA,IPIV,INFO);
  27688. if (INFO)
  27689. cimg::warn(_cimg_instance
  27690. "solve(): LAPACK library function dgetrf_() returned error code %d.",
  27691. cimg_instance,
  27692. INFO);
  27693. else {
  27694. cimg::getrs(TRANS,N,lapA,IPIV,lapB,INFO);
  27695. if (INFO)
  27696. cimg::warn(_cimg_instance
  27697. "solve(): LAPACK library function dgetrs_() returned error code %d.",
  27698. cimg_instance,
  27699. INFO);
  27700. }
  27701. if (!INFO) cimg_forY(*this,j) (*this)(i,j) = (T)(lapB[j]); else cimg_forY(*this,j) (*this)(i,j) = (T)0;
  27702. }
  27703. delete[] IPIV; delete[] lapA; delete[] lapB; delete[] WORK;
  27704. #else
  27705. CImg<Ttfloat> lu(A,false);
  27706. CImg<Ttfloat> indx;
  27707. bool d;
  27708. lu._LU(indx,d);
  27709. CImg<T> res(_width,A._width);
  27710. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,16))
  27711. cimg_forX(*this,i) res.draw_image(i,get_column(i)._solve(lu,indx));
  27712. res.move_to(*this);
  27713. #endif
  27714. } else { // Least-square solution for non-square systems
  27715. #ifdef cimg_use_lapack
  27716. char TRANS = 'N';
  27717. int INFO, N = A._width, M = A._height, LWORK = -1, LDA = M, LDB = M, NRHS = _width;
  27718. Ttfloat WORK_QUERY;
  27719. Ttfloat
  27720. * const lapA = new Ttfloat[M*N],
  27721. * const lapB = new Ttfloat[M*NRHS];
  27722. cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, &WORK_QUERY, LWORK, INFO);
  27723. LWORK = (int) WORK_QUERY;
  27724. Ttfloat *const WORK = new Ttfloat[LWORK];
  27725. cimg_forXY(A,k,l) lapA[k*M + l] = (Ttfloat)(A(k,l));
  27726. cimg_forXY(*this,k,l) lapB[k*M + l] = (Ttfloat)((*this)(k,l));
  27727. cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, WORK, LWORK, INFO);
  27728. if (INFO != 0)
  27729. cimg::warn(_cimg_instance
  27730. "solve(): LAPACK library function sgels() returned error code %d.",
  27731. cimg_instance,
  27732. INFO);
  27733. assign(NRHS, N);
  27734. if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)lapB[k*M + l];
  27735. else (A.get_pseudoinvert(use_LU)*(*this)).move_to(*this);
  27736. delete[] lapA; delete[] lapB; delete[] WORK;
  27737. #else
  27738. (A.get_pseudoinvert(use_LU)*(*this)).move_to(*this);
  27739. #endif
  27740. }
  27741. return *this;
  27742. }
  27743. //! Solve a system of linear equations \newinstance.
  27744. template<typename t>
  27745. CImg<_cimg_Ttfloat> get_solve(const CImg<t>& A, const bool use_LU=false) const {
  27746. typedef _cimg_Ttfloat Ttfloat;
  27747. return CImg<Ttfloat>(*this,false).solve(A,use_LU);
  27748. }
  27749. template<typename t, typename ti>
  27750. CImg<T>& _solve(const CImg<t>& A, const CImg<ti>& indx) {
  27751. typedef _cimg_Ttfloat Ttfloat;
  27752. const int N = height();
  27753. int ii = -1;
  27754. Ttfloat sum;
  27755. for (int i = 0; i<N; ++i) {
  27756. const int ip = (int)indx[i];
  27757. sum = (*this)(ip);
  27758. (*this)(ip) = (*this)(i);
  27759. if (ii>=0) for (int j = ii; j<=i - 1; ++j) sum-=A(j,i)*(*this)(j);
  27760. else if (sum!=0) ii = i;
  27761. (*this)(i) = (T)sum;
  27762. }
  27763. for (int i = N - 1; i>=0; --i) {
  27764. sum = (*this)(i);
  27765. for (int j = i + 1; j<N; ++j) sum-=A(j,i)*(*this)(j);
  27766. (*this)(i) = (T)(sum/A(i,i));
  27767. }
  27768. return *this;
  27769. }
  27770. //! Solve a tridiagonal system of linear equations.
  27771. /**
  27772. \param A Coefficients of the tridiagonal system.
  27773. A is a tridiagonal matrix A = [ b0,c0,0,...; a1,b1,c1,0,... ; ... ; ...,0,aN,bN ],
  27774. stored as a 3 columns matrix
  27775. \note Solve AX=B where \c B=*this, using the Thomas algorithm.
  27776. **/
  27777. template<typename t>
  27778. CImg<T>& solve_tridiagonal(const CImg<t>& A) {
  27779. const unsigned int siz = (unsigned int)size();
  27780. if (A._width!=3 || A._height!=siz)
  27781. throw CImgArgumentException(_cimg_instance
  27782. "solve_tridiagonal(): Instance and tridiagonal matrix "
  27783. "(%u,%u,%u,%u,%p) have incompatible dimensions.",
  27784. cimg_instance,
  27785. A._width,A._height,A._depth,A._spectrum,A._data);
  27786. typedef _cimg_Ttfloat Ttfloat;
  27787. const Ttfloat epsilon = 1e-4f;
  27788. CImg<Ttfloat> B = A.get_column(1), V(*this,false);
  27789. for (int i = 1; i<(int)siz; ++i) {
  27790. const Ttfloat m = A(0,i)/(B[i - 1]?B[i - 1]:epsilon);
  27791. B[i] -= m*A(2,i - 1);
  27792. V[i] -= m*V[i - 1];
  27793. }
  27794. (*this)[siz - 1] = (T)(V[siz - 1]/(B[siz - 1]?B[siz - 1]:epsilon));
  27795. for (int i = (int)siz - 2; i>=0; --i) (*this)[i] = (T)((V[i] - A(2,i)*(*this)[i + 1])/(B[i]?B[i]:epsilon));
  27796. return *this;
  27797. }
  27798. //! Solve a tridiagonal system of linear equations \newinstance.
  27799. template<typename t>
  27800. CImg<_cimg_Ttfloat> get_solve_tridiagonal(const CImg<t>& A) const {
  27801. return CImg<_cimg_Ttfloat>(*this,false).solve_tridiagonal(A);
  27802. }
  27803. //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.
  27804. /**
  27805. \param[out] val Vector of the estimated eigenvalues, in decreasing order.
  27806. \param[out] vec Matrix of the estimated eigenvectors, sorted by columns.
  27807. **/
  27808. template<typename t>
  27809. const CImg<T>& eigen(CImg<t>& val, CImg<t> &vec) const {
  27810. if (is_empty()) { val.assign(); vec.assign(); }
  27811. else {
  27812. if (_width!=_height || _depth>1 || _spectrum>1)
  27813. throw CImgInstanceException(_cimg_instance
  27814. "eigen(): Instance is not a square matrix.",
  27815. cimg_instance);
  27816. if (val.size()<(ulongT)_width) val.assign(1,_width);
  27817. if (vec.size()<(ulongT)_width*_width) vec.assign(_width,_width);
  27818. switch (_width) {
  27819. case 1 : { val[0] = (t)(*this)[0]; vec[0] = (t)1; } break;
  27820. case 2 : {
  27821. const double a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3], e = a + d;
  27822. double f = e*e - 4*(a*d - b*c);
  27823. if (f<0) cimg::warn(_cimg_instance
  27824. "eigen(): Complex eigenvalues found.",
  27825. cimg_instance);
  27826. f = std::sqrt(f);
  27827. const double
  27828. l1 = 0.5*(e - f),
  27829. l2 = 0.5*(e + f),
  27830. b2 = b*b,
  27831. norm1 = std::sqrt(cimg::sqr(l2 - a) + b2),
  27832. norm2 = std::sqrt(cimg::sqr(l1 - a) + b2);
  27833. val[0] = (t)l2;
  27834. val[1] = (t)l1;
  27835. if (norm1>0) { vec(0,0) = (t)(b/norm1); vec(0,1) = (t)((l2 - a)/norm1); } else { vec(0,0) = 1; vec(0,1) = 0; }
  27836. if (norm2>0) { vec(1,0) = (t)(b/norm2); vec(1,1) = (t)((l1 - a)/norm2); } else { vec(1,0) = 1; vec(1,1) = 0; }
  27837. } break;
  27838. default :
  27839. throw CImgInstanceException(_cimg_instance
  27840. "eigen(): Eigenvalues computation of general matrices is limited "
  27841. "to 2x2 matrices.",
  27842. cimg_instance);
  27843. }
  27844. }
  27845. return *this;
  27846. }
  27847. //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.
  27848. /**
  27849. \return A list of two images <tt>[val; vec]</tt>, whose meaning is similar as in eigen(CImg<t>&,CImg<t>&) const.
  27850. **/
  27851. CImgList<Tfloat> get_eigen() const {
  27852. CImgList<Tfloat> res(2);
  27853. eigen(res[0],res[1]);
  27854. return res;
  27855. }
  27856. //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix.
  27857. /**
  27858. \param[out] val Vector of the estimated eigenvalues, in decreasing order.
  27859. \param[out] vec Matrix of the estimated eigenvectors, sorted by columns.
  27860. **/
  27861. template<typename t>
  27862. const CImg<T>& symmetric_eigen(CImg<t>& val, CImg<t>& vec) const {
  27863. if (is_empty()) { val.assign(); vec.assign(); return *this; }
  27864. if (_width!=_height || _depth>1 || _spectrum>1)
  27865. throw CImgInstanceException(_cimg_instance
  27866. "eigen(): Instance is not a square matrix.",
  27867. cimg_instance);
  27868. val.assign(1,_width);
  27869. vec.assign(_width,_width);
  27870. if (_width==1) { val[0] = cimg::abs((*this)[0]); vec[0] = 1; return *this; }
  27871. if (_width==2) {
  27872. const double
  27873. a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3],
  27874. e = a + d, f = std::sqrt(std::max(e*e - 4*(a*d - b*c),0.0)),
  27875. l1 = 0.5*(e - f), l2 = 0.5*(e + f),
  27876. n = std::sqrt(cimg::sqr(l2 - a) + b*b);
  27877. val[0] = (t)l2;
  27878. val[1] = (t)l1;
  27879. if (n>0) { vec[0] = (t)(b/n); vec[2] = (t)((l2 - a)/n); } else { vec[0] = 1; vec[2] = 0; }
  27880. vec[1] = -vec[2];
  27881. vec[3] = vec[0];
  27882. return *this;
  27883. }
  27884. #ifdef cimg_use_lapack
  27885. char JOB = 'V', UPLO = 'U';
  27886. int N = _width, LWORK = 4*N, INFO;
  27887. Tfloat
  27888. *const lapA = new Tfloat[N*N],
  27889. *const lapW = new Tfloat[N],
  27890. *const WORK = new Tfloat[LWORK];
  27891. cimg_forXY(*this,k,l) lapA[k*N + l] = (Tfloat)((*this)(k,l));
  27892. cimg::syev(JOB,UPLO,N,lapA,lapW,WORK,LWORK,INFO);
  27893. if (INFO)
  27894. cimg::warn(_cimg_instance
  27895. "symmetric_eigen(): LAPACK library function dsyev_() returned error code %d.",
  27896. cimg_instance,
  27897. INFO);
  27898. if (!INFO) {
  27899. cimg_forY(val,i) val(i) = (T)lapW[N - 1 -i];
  27900. cimg_forXY(vec,k,l) vec(k,l) = (T)(lapA[(N - 1 - k)*N + l]);
  27901. } else { val.fill(0); vec.fill(0); }
  27902. delete[] lapA; delete[] lapW; delete[] WORK;
  27903. #else
  27904. CImg<t> V(_width,_width);
  27905. Tfloat M = 0, m = (Tfloat)min_max(M), maxabs = cimg::max((Tfloat)1,cimg::abs(m),cimg::abs(M));
  27906. (CImg<Tfloat>(*this,false)/=maxabs).SVD(vec,val,V,false);
  27907. if (maxabs!=1) val*=maxabs;
  27908. bool is_ambiguous = false;
  27909. float eig = 0;
  27910. cimg_forY(val,p) { // Check for ambiguous cases
  27911. if (val[p]>eig) eig = (float)val[p];
  27912. t scal = 0;
  27913. cimg_forY(vec,y) scal+=vec(p,y)*V(p,y);
  27914. if (cimg::abs(scal)<0.9f) is_ambiguous = true;
  27915. if (scal<0) val[p] = -val[p];
  27916. }
  27917. if (is_ambiguous) {
  27918. ++(eig*=2);
  27919. SVD(vec,val,V,false,40,eig);
  27920. val-=eig;
  27921. }
  27922. CImg<intT> permutations; // Sort eigenvalues in decreasing order
  27923. CImg<t> tmp(_width);
  27924. val.sort(permutations,false);
  27925. cimg_forY(vec,k) {
  27926. cimg_forY(permutations,y) tmp(y) = vec(permutations(y),k);
  27927. std::memcpy(vec.data(0,k),tmp._data,sizeof(t)*_width);
  27928. }
  27929. #endif
  27930. return *this;
  27931. }
  27932. //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix.
  27933. /**
  27934. \return A list of two images <tt>[val; vec]</tt>, whose meaning are similar as in
  27935. symmetric_eigen(CImg<t>&,CImg<t>&) const.
  27936. **/
  27937. CImgList<Tfloat> get_symmetric_eigen() const {
  27938. CImgList<Tfloat> res(2);
  27939. symmetric_eigen(res[0],res[1]);
  27940. return res;
  27941. }
  27942. //! Sort pixel values and get sorting permutations.
  27943. /**
  27944. \param[out] permutations Permutation map used for the sorting.
  27945. \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way.
  27946. **/
  27947. template<typename t>
  27948. CImg<T>& sort(CImg<t>& permutations, const bool is_increasing=true) {
  27949. permutations.assign(_width,_height,_depth,_spectrum);
  27950. if (is_empty()) return *this;
  27951. cimg_foroff(permutations,off) permutations[off] = (t)off;
  27952. return _quicksort(0,size() - 1,permutations,is_increasing,true);
  27953. }
  27954. //! Sort pixel values and get sorting permutations \newinstance.
  27955. template<typename t>
  27956. CImg<T> get_sort(CImg<t>& permutations, const bool is_increasing=true) const {
  27957. return (+*this).sort(permutations,is_increasing);
  27958. }
  27959. //! Sort pixel values.
  27960. /**
  27961. \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way.
  27962. \param axis Tells if the value sorting must be done along a specific axis. Can be:
  27963. - \c 0: All pixel values are sorted, independently on their initial position.
  27964. - \c 'x': Image columns are sorted, according to the first value in each column.
  27965. - \c 'y': Image rows are sorted, according to the first value in each row.
  27966. - \c 'z': Image slices are sorted, according to the first value in each slice.
  27967. - \c 'c': Image channels are sorted, according to the first value in each channel.
  27968. **/
  27969. CImg<T>& sort(const bool is_increasing=true, const char axis=0) {
  27970. if (is_empty()) return *this;
  27971. CImg<uintT> perm;
  27972. switch (cimg::lowercase(axis)) {
  27973. case 0 :
  27974. _quicksort(0,size() - 1,perm,is_increasing,false);
  27975. break;
  27976. case 'x' : {
  27977. perm.assign(_width);
  27978. get_crop(0,0,0,0,_width - 1,0,0,0).sort(perm,is_increasing);
  27979. CImg<T> img(*this,false);
  27980. cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(perm[x],y,z,c);
  27981. } break;
  27982. case 'y' : {
  27983. perm.assign(_height);
  27984. get_crop(0,0,0,0,0,_height - 1,0,0).sort(perm,is_increasing);
  27985. CImg<T> img(*this,false);
  27986. cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,perm[y],z,c);
  27987. } break;
  27988. case 'z' : {
  27989. perm.assign(_depth);
  27990. get_crop(0,0,0,0,0,0,_depth - 1,0).sort(perm,is_increasing);
  27991. CImg<T> img(*this,false);
  27992. cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,perm[z],c);
  27993. } break;
  27994. case 'c' : {
  27995. perm.assign(_spectrum);
  27996. get_crop(0,0,0,0,0,0,0,_spectrum - 1).sort(perm,is_increasing);
  27997. CImg<T> img(*this,false);
  27998. cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,z,perm[c]);
  27999. } break;
  28000. default :
  28001. throw CImgArgumentException(_cimg_instance
  28002. "sort(): Invalid specified axis '%c' "
  28003. "(should be { x | y | z | c }).",
  28004. cimg_instance,axis);
  28005. }
  28006. return *this;
  28007. }
  28008. //! Sort pixel values \newinstance.
  28009. CImg<T> get_sort(const bool is_increasing=true, const char axis=0) const {
  28010. return (+*this).sort(is_increasing,axis);
  28011. }
  28012. template<typename t>
  28013. CImg<T>& _quicksort(const long indm, const long indM, CImg<t>& permutations,
  28014. const bool is_increasing, const bool is_permutations) {
  28015. if (indm<indM) {
  28016. const long mid = (indm + indM)/2;
  28017. if (is_increasing) {
  28018. if ((*this)[indm]>(*this)[mid]) {
  28019. cimg::swap((*this)[indm],(*this)[mid]);
  28020. if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
  28021. }
  28022. if ((*this)[mid]>(*this)[indM]) {
  28023. cimg::swap((*this)[indM],(*this)[mid]);
  28024. if (is_permutations) cimg::swap(permutations[indM],permutations[mid]);
  28025. }
  28026. if ((*this)[indm]>(*this)[mid]) {
  28027. cimg::swap((*this)[indm],(*this)[mid]);
  28028. if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
  28029. }
  28030. } else {
  28031. if ((*this)[indm]<(*this)[mid]) {
  28032. cimg::swap((*this)[indm],(*this)[mid]);
  28033. if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
  28034. }
  28035. if ((*this)[mid]<(*this)[indM]) {
  28036. cimg::swap((*this)[indM],(*this)[mid]);
  28037. if (is_permutations) cimg::swap(permutations[indM],permutations[mid]);
  28038. }
  28039. if ((*this)[indm]<(*this)[mid]) {
  28040. cimg::swap((*this)[indm],(*this)[mid]);
  28041. if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
  28042. }
  28043. }
  28044. if (indM - indm>=3) {
  28045. const T pivot = (*this)[mid];
  28046. long i = indm, j = indM;
  28047. if (is_increasing) {
  28048. do {
  28049. while ((*this)[i]<pivot) ++i;
  28050. while ((*this)[j]>pivot) --j;
  28051. if (i<=j) {
  28052. if (is_permutations) cimg::swap(permutations[i],permutations[j]);
  28053. cimg::swap((*this)[i++],(*this)[j--]);
  28054. }
  28055. } while (i<=j);
  28056. } else {
  28057. do {
  28058. while ((*this)[i]>pivot) ++i;
  28059. while ((*this)[j]<pivot) --j;
  28060. if (i<=j) {
  28061. if (is_permutations) cimg::swap(permutations[i],permutations[j]);
  28062. cimg::swap((*this)[i++],(*this)[j--]);
  28063. }
  28064. } while (i<=j);
  28065. }
  28066. if (indm<j) _quicksort(indm,j,permutations,is_increasing,is_permutations);
  28067. if (i<indM) _quicksort(i,indM,permutations,is_increasing,is_permutations);
  28068. }
  28069. }
  28070. return *this;
  28071. }
  28072. //! Compute the SVD of the instance image, viewed as a general matrix.
  28073. /**
  28074. Compute the SVD decomposition \c *this=U*S*V' where \c U and \c V are orthogonal matrices
  28075. and \c S is a diagonal matrix. \c V' denotes the matrix transpose of \c V.
  28076. \param[out] U First matrix of the SVD product.
  28077. \param[out] S Coefficients of the second (diagonal) matrix of the SVD product.
  28078. These coefficients are stored as a vector.
  28079. \param[out] V Third matrix of the SVD product.
  28080. \param sorting Tells if the diagonal coefficients are sorted (in decreasing order).
  28081. \param max_iteration Maximum number of iterations considered for the algorithm convergence.
  28082. \param lambda Epsilon used for the algorithm convergence.
  28083. \note The instance matrix can be computed from \c U,\c S and \c V by
  28084. \code
  28085. const CImg<> A; // Input matrix (assumed to contain some values)
  28086. CImg<> U,S,V;
  28087. A.SVD(U,S,V)
  28088. \endcode
  28089. **/
  28090. template<typename t>
  28091. const CImg<T>& SVD(CImg<t>& U, CImg<t>& S, CImg<t>& V, const bool sorting=true,
  28092. const unsigned int max_iteration=40, const float lambda=0) const {
  28093. typedef _cimg_Ttfloat Ttfloat;
  28094. const Ttfloat epsilon = (Ttfloat)1e-25;
  28095. if (is_empty()) { U.assign(); S.assign(); V.assign(); }
  28096. else if (_depth!=1 || _spectrum!=1)
  28097. throw CImgInstanceException(_cimg_instance
  28098. "SVD(): Instance has invalid dimensions (depth or channels different from 1).",
  28099. cimg_instance);
  28100. else {
  28101. U = *this;
  28102. if (lambda!=0) {
  28103. const unsigned int delta = std::min(U._width,U._height);
  28104. for (unsigned int i = 0; i<delta; ++i) U(i,i) = (t)(U(i,i) + lambda);
  28105. }
  28106. if (S.size()<_width) S.assign(1,_width);
  28107. if (V._width<_width || V._height<_height) V.assign(_width,_width);
  28108. CImg<t> rv1(_width);
  28109. Ttfloat anorm = 0, c, f, g = 0, h, s, scale = 0;
  28110. int l = 0;
  28111. cimg_forX(U,i) {
  28112. l = i + 1;
  28113. rv1[i] = scale*g;
  28114. g = s = scale = 0;
  28115. if (i<height()) {
  28116. for (int k = i; k<height(); ++k) scale+=cimg::abs(U(i,k));
  28117. if (scale) {
  28118. for (int k = i; k<height(); ++k) {
  28119. U(i,k)/=scale;
  28120. s+=U(i,k)*U(i,k);
  28121. }
  28122. f = U(i,i);
  28123. g = (Ttfloat)((f>=0?-1:1)*std::sqrt(s));
  28124. h = f*g - s;
  28125. U(i,i) = f - g;
  28126. for (int j = l; j<width(); ++j) {
  28127. s = 0;
  28128. for (int k=i; k<height(); ++k) s+=U(i,k)*U(j,k);
  28129. f = s/h;
  28130. for (int k = i; k<height(); ++k) U(j,k)+=f*U(i,k);
  28131. }
  28132. for (int k = i; k<height(); ++k) U(i,k)*=scale;
  28133. }
  28134. }
  28135. S[i] = scale*g;
  28136. g = s = scale = 0;
  28137. if (i<height() && i!=width() - 1) {
  28138. for (int k = l; k<width(); ++k) scale+=cimg::abs(U(k,i));
  28139. if (scale) {
  28140. for (int k = l; k<width(); ++k) {
  28141. U(k,i)/=scale;
  28142. s+=U(k,i)*U(k,i);
  28143. }
  28144. f = U(l,i);
  28145. g = (Ttfloat)((f>=0?-1:1)*std::sqrt(s));
  28146. h = f*g - s;
  28147. U(l,i) = f - g;
  28148. for (int k = l; k<width(); ++k) rv1[k] = U(k,i)/h;
  28149. for (int j = l; j<height(); ++j) {
  28150. s = 0;
  28151. for (int k = l; k<width(); ++k) s+=U(k,j)*U(k,i);
  28152. for (int k = l; k<width(); ++k) U(k,j)+=s*rv1[k];
  28153. }
  28154. for (int k = l; k<width(); ++k) U(k,i)*=scale;
  28155. }
  28156. }
  28157. anorm = (Ttfloat)std::max((float)anorm,(float)(cimg::abs(S[i]) + cimg::abs(rv1[i])));
  28158. }
  28159. for (int i = width() - 1; i>=0; --i) {
  28160. if (i<width() - 1) {
  28161. if (g) {
  28162. for (int j = l; j<width(); ++j) V(i,j) =(U(j,i)/U(l,i))/g;
  28163. for (int j = l; j<width(); ++j) {
  28164. s = 0;
  28165. for (int k = l; k<width(); ++k) s+=U(k,i)*V(j,k);
  28166. for (int k = l; k<width(); ++k) V(j,k)+=s*V(i,k);
  28167. }
  28168. }
  28169. for (int j = l; j<width(); ++j) V(j,i) = V(i,j) = (t)0.;
  28170. }
  28171. V(i,i) = (t)1;
  28172. g = rv1[i];
  28173. l = i;
  28174. }
  28175. for (int i = std::min(width(),height()) - 1; i>=0; --i) {
  28176. l = i + 1;
  28177. g = S[i];
  28178. for (int j = l; j<width(); ++j) U(j,i) = 0;
  28179. if (g) {
  28180. g = 1/g;
  28181. for (int j = l; j<width(); ++j) {
  28182. s = 0;
  28183. for (int k = l; k<height(); ++k) s+=U(i,k)*U(j,k);
  28184. f = (s/U(i,i))*g;
  28185. for (int k = i; k<height(); ++k) U(j,k)+=f*U(i,k);
  28186. }
  28187. for (int j = i; j<height(); ++j) U(i,j)*= g;
  28188. } else for (int j = i; j<height(); ++j) U(i,j) = 0;
  28189. ++U(i,i);
  28190. }
  28191. for (int k = width() - 1; k>=0; --k) {
  28192. int nm = 0;
  28193. for (unsigned int its = 0; its<max_iteration; ++its) {
  28194. bool flag = true;
  28195. for (l = k; l>=1; --l) {
  28196. nm = l - 1;
  28197. if ((cimg::abs(rv1[l]) + anorm)==anorm) { flag = false; break; }
  28198. if ((cimg::abs(S[nm]) + anorm)==anorm) break;
  28199. }
  28200. if (flag) {
  28201. c = 0;
  28202. s = 1;
  28203. for (int i = l; i<=k; ++i) {
  28204. f = s*rv1[i];
  28205. rv1[i] = c*rv1[i];
  28206. if ((cimg::abs(f) + anorm)==anorm) break;
  28207. g = S[i];
  28208. h = cimg::_hypot(f,g);
  28209. S[i] = h;
  28210. h = 1/h;
  28211. c = g*h;
  28212. s = -f*h;
  28213. cimg_forY(U,j) {
  28214. const t y = U(nm,j), z = U(i,j);
  28215. U(nm,j) = y*c + z*s;
  28216. U(i,j) = z*c - y*s;
  28217. }
  28218. }
  28219. }
  28220. const t z = S[k];
  28221. if (l==k) { if (z<0) { S[k] = -z; cimg_forX(U,j) V(k,j) = -V(k,j); } break; }
  28222. nm = k - 1;
  28223. t x = S[l], y = S[nm];
  28224. g = rv1[nm];
  28225. h = rv1[k];
  28226. f = ((y - z)*(y + z) + (g - h)*(g + h))/std::max(epsilon,(Ttfloat)2*h*y);
  28227. g = cimg::_hypot(f,(Ttfloat)1);
  28228. f = ((x - z)*(x + z) + h*((y/(f + (f>=0?g:-g))) - h))/std::max(epsilon,(Ttfloat)x);
  28229. c = s = 1;
  28230. for (int j = l; j<=nm; ++j) {
  28231. const int i = j + 1;
  28232. g = rv1[i];
  28233. h = s*g;
  28234. g = c*g;
  28235. t y1 = S[i], z1 = cimg::_hypot(f,h);
  28236. rv1[j] = z1;
  28237. c = f/std::max(epsilon,(Ttfloat)z1);
  28238. s = h/std::max(epsilon,(Ttfloat)z1);
  28239. f = x*c + g*s;
  28240. g = g*c - x*s;
  28241. h = y1*s;
  28242. y1*=c;
  28243. cimg_forX(U,jj) {
  28244. const t x2 = V(j,jj), z2 = V(i,jj);
  28245. V(j,jj) = x2*c + z2*s;
  28246. V(i,jj) = z2*c - x2*s;
  28247. }
  28248. z1 = cimg::_hypot(f,h);
  28249. S[j] = z1;
  28250. if (z1) {
  28251. z1 = 1/std::max(epsilon,(Ttfloat)z1);
  28252. c = f*z1;
  28253. s = h*z1;
  28254. }
  28255. f = c*g + s*y1;
  28256. x = c*y1 - s*g;
  28257. cimg_forY(U,jj) {
  28258. const t y2 = U(j,jj), z2 = U(i,jj);
  28259. U(j,jj) = y2*c + z2*s;
  28260. U(i,jj) = z2*c - y2*s;
  28261. }
  28262. }
  28263. rv1[l] = 0;
  28264. rv1[k] = f;
  28265. S[k] = x;
  28266. }
  28267. }
  28268. if (sorting) {
  28269. CImg<intT> permutations;
  28270. CImg<t> tmp(_width);
  28271. S.sort(permutations,false);
  28272. cimg_forY(U,k) {
  28273. cimg_forY(permutations,y) tmp(y) = U(permutations(y),k);
  28274. std::memcpy(U.data(0,k),tmp._data,sizeof(t)*_width);
  28275. }
  28276. cimg_forY(V,k) {
  28277. cimg_forY(permutations,y) tmp(y) = V(permutations(y),k);
  28278. std::memcpy(V.data(0,k),tmp._data,sizeof(t)*_width);
  28279. }
  28280. }
  28281. }
  28282. return *this;
  28283. }
  28284. //! Compute the SVD of the instance image, viewed as a general matrix.
  28285. /**
  28286. \return A list of three images <tt>[U; S; V]</tt>, whose meaning is similar as in
  28287. SVD(CImg<t>&,CImg<t>&,CImg<t>&,bool,unsigned int,float) const.
  28288. **/
  28289. CImgList<Tfloat> get_SVD(const bool sorting=true,
  28290. const unsigned int max_iteration=40, const float lambda=0) const {
  28291. CImgList<Tfloat> res(3);
  28292. SVD(res[0],res[1],res[2],sorting,max_iteration,lambda);
  28293. return res;
  28294. }
  28295. // [internal] Compute the LU decomposition of a permuted matrix.
  28296. template<typename t>
  28297. CImg<T>& _LU(CImg<t>& indx, bool& d) {
  28298. const int N = width();
  28299. int imax = 0;
  28300. CImg<Tfloat> vv(N);
  28301. indx.assign(N);
  28302. d = true;
  28303. bool return0 = false;
  28304. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=512))
  28305. cimg_forX(*this,i) {
  28306. Tfloat vmax = 0;
  28307. cimg_forX(*this,j) {
  28308. const Tfloat tmp = cimg::abs((*this)(j,i));
  28309. if (tmp>vmax) vmax = tmp;
  28310. }
  28311. if (vmax==0) return0 = true; else vv[i] = 1/vmax;
  28312. }
  28313. if (return0) { indx.fill(0); return fill(0); }
  28314. cimg_forX(*this,j) {
  28315. for (int i = 0; i<j; ++i) {
  28316. Tfloat sum = (*this)(j,i);
  28317. for (int k = 0; k<i; ++k) sum-=(*this)(k,i)*(*this)(j,k);
  28318. (*this)(j,i) = (T)sum;
  28319. }
  28320. Tfloat vmax = 0;
  28321. for (int i = j; i<width(); ++i) {
  28322. Tfloat sum = (*this)(j,i);
  28323. for (int k = 0; k<j; ++k) sum-=(*this)(k,i)*(*this)(j,k);
  28324. (*this)(j,i) = (T)sum;
  28325. const Tfloat tmp = vv[i]*cimg::abs(sum);
  28326. if (tmp>=vmax) { vmax = tmp; imax = i; }
  28327. }
  28328. if (j!=imax) {
  28329. cimg_forX(*this,k) cimg::swap((*this)(k,imax),(*this)(k,j));
  28330. d = !d;
  28331. vv[imax] = vv[j];
  28332. }
  28333. indx[j] = (t)imax;
  28334. if ((*this)(j,j)==0) (*this)(j,j) = (T)1e-20;
  28335. if (j<N) {
  28336. const Tfloat tmp = 1/(Tfloat)(*this)(j,j);
  28337. for (int i = j + 1; i<N; ++i) (*this)(j,i) = (T)((*this)(j,i)*tmp);
  28338. }
  28339. }
  28340. return *this;
  28341. }
  28342. //! Compute the projection of the instance matrix onto the specified dictionary.
  28343. /**
  28344. Find the best matching projection of selected matrix onto the span of an over-complete dictionary D,
  28345. using the orthogonal projection or (opt. Orthogonal) Matching Pursuit algorithm.
  28346. Instance image must a 2D-matrix in which each column represent a signal to project.
  28347. \param dictionary A matrix in which each column is an element of the dictionary D.
  28348. \param method Tell what projection method is applied. It can be:
  28349. - 0 = orthogonal projection (default).
  28350. - 1 = matching pursuit.
  28351. - 2 = matching pursuit, with a single orthogonal projection step at the end.
  28352. - >=3 = orthogonal matching pursuit where an orthogonal projection step is performed
  28353. every 'method-2' iterations.
  28354. \param max_iter Sets the max number of iterations processed for each signal.
  28355. If set to '0' (default), 'max_iter' is set to the number of dictionary columns.
  28356. (only meaningful for matching pursuit and its variants).
  28357. \param max_residual Gives a stopping criterion on signal reconstruction accuracy.
  28358. (only meaningful for matching pursuit and its variants).
  28359. \return A matrix W whose columns correspond to the sparse weights of associated to each input matrix column.
  28360. Thus, the matrix product D*W is an approximation of the input matrix.
  28361. **/
  28362. template<typename t>
  28363. CImg<T>& project_matrix(const CImg<t>& dictionary, const unsigned int method=0,
  28364. const unsigned int max_iter=0, const double max_residual=1e-6) {
  28365. return get_project_matrix(dictionary,method,max_iter,max_residual).move_to(*this);
  28366. }
  28367. template<typename t>
  28368. CImg<Tfloat> get_project_matrix(const CImg<t>& dictionary, const unsigned int method=0,
  28369. const unsigned int max_iter=0, const double max_residual=1e-6) const {
  28370. if (_depth!=1 || _spectrum!=1)
  28371. throw CImgInstanceException(_cimg_instance
  28372. "project_matrix(): Instance image is not a matrix.",
  28373. cimg_instance);
  28374. if (dictionary._height!=_height || dictionary._depth!=1 || dictionary._spectrum!=1)
  28375. throw CImgArgumentException(_cimg_instance
  28376. "project_matrix(): Specified dictionary (%u,%u,%u,%u) has an invalid size.",
  28377. cimg_instance,
  28378. dictionary._width,dictionary._height,dictionary._depth,dictionary._spectrum);
  28379. if (!method) return get_solve(dictionary,true);
  28380. CImg<Tfloat> W(_width,dictionary._width,1,1,0);
  28381. // Compute dictionary norm and normalize it.
  28382. CImg<Tfloat> D(dictionary,false), Dnorm(D._width);
  28383. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=2 && _width*_height>=32))
  28384. cimg_forX(Dnorm,d) {
  28385. Tfloat norm = 0;
  28386. cimg_forY(D,y) norm+=cimg::sqr(D(d,y));
  28387. Dnorm[d] = std::max((Tfloat)1e-8,std::sqrt(norm));
  28388. }
  28389. cimg_forXY(D,d,y) D(d,y)/=Dnorm[d];
  28390. // Matching pursuit.
  28391. const unsigned int proj_step = method<3?1:method - 2;
  28392. bool is_orthoproj = false;
  28393. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=2 && _width*_height>=32))
  28394. cimg_forX(*this,x) {
  28395. CImg<Tfloat> S = get_column(x);
  28396. const CImg<Tfloat> S0 = method<2?CImg<Tfloat>():S;
  28397. Tfloat residual = S.magnitude()/S._height;
  28398. const unsigned int nmax = max_iter?max_iter:D._width;
  28399. for (unsigned int n = 0; n<nmax && residual>max_residual; ++n) {
  28400. // Find best matching column in D.
  28401. int dmax = 0;
  28402. Tfloat absdotmax = 0, dotmax = 0;
  28403. cimg_pragma_openmp(parallel for cimg_openmp_if(D._width>=2 && D._width*D._height>=32))
  28404. cimg_forX(D,d) {
  28405. Tfloat _dot = 0;
  28406. cimg_forY(D,y) _dot+=S[y]*D(d,y);
  28407. Tfloat absdot = cimg::abs(_dot);
  28408. cimg_pragma_openmp(critical(get_project_matrix)) {
  28409. if (absdot>absdotmax) {
  28410. absdotmax = absdot;
  28411. dotmax = _dot;
  28412. dmax = d;
  28413. }
  28414. }
  28415. }
  28416. if (!n || method<3 || n%proj_step) {
  28417. // Matching Pursuit: Subtract component to signal.
  28418. W(x,dmax)+=dotmax;
  28419. residual = 0;
  28420. cimg_forY(S,y) {
  28421. S[y]-=dotmax*D(dmax,y);
  28422. residual+=cimg::sqr(S[y]);
  28423. }
  28424. residual = std::sqrt(residual)/S._height;
  28425. is_orthoproj = false;
  28426. } else {
  28427. // Orthogonal Matching Pursuit: Orthogonal projection step.
  28428. W(x,dmax) = 1; // Used as a marker only.
  28429. unsigned int nbW = 0;
  28430. cimg_forY(W,d) if (W(x,d)) ++nbW;
  28431. CImg<Tfloat> sD(nbW,D._height);
  28432. CImg<uintT> inds(nbW);
  28433. int sd = 0;
  28434. cimg_forY(W,d) if (W(x,d)) {
  28435. cimg_forY(sD,y) sD(sd,y) = D(d,y);
  28436. inds[sd++] = d;
  28437. }
  28438. S0.get_solve(sD,true).move_to(sD); // sD is now a one-column vector of weights
  28439. // Recompute residual signal.
  28440. S = S0;
  28441. cimg_forY(sD,k) {
  28442. const Tfloat weight = sD[k];
  28443. const unsigned int ind = inds[k];
  28444. W(x,ind) = weight;
  28445. cimg_forY(S,y) S[y]-=weight*D(ind,y);
  28446. }
  28447. residual = S.magnitude()/S._height;
  28448. is_orthoproj = true;
  28449. }
  28450. }
  28451. // Perform last orthoprojection step if needed.
  28452. if (method>=2 && !is_orthoproj) {
  28453. unsigned int nbW = 0;
  28454. cimg_forY(W,d) if (W(x,d)) ++nbW;
  28455. if (nbW) { // Avoid degenerated case where 0 coefs are used
  28456. CImg<Tfloat> sD(nbW,D._height);
  28457. CImg<uintT> inds(nbW);
  28458. int sd = 0;
  28459. cimg_forY(W,d) if (W(x,d)) {
  28460. cimg_forY(sD,y) sD(sd,y) = D(d,y);
  28461. inds[sd++] = d;
  28462. }
  28463. S0.get_solve(sD,true).move_to(sD);
  28464. cimg_forY(sD,k) W(x,inds[k]) = sD[k];
  28465. }
  28466. }
  28467. }
  28468. // Normalize resulting coefficients according to initial (non-normalized) dictionary.
  28469. cimg_forXY(W,x,y) W(x,y)/=Dnorm[y];
  28470. return W;
  28471. }
  28472. //! Compute minimal path in a graph, using the Dijkstra algorithm.
  28473. /**
  28474. \param distance An object having operator()(unsigned int i, unsigned int j) which returns distance
  28475. between two nodes (i,j).
  28476. \param nb_nodes Number of graph nodes.
  28477. \param starting_node Index of the starting node.
  28478. \param ending_node Index of the ending node (set to ~0U to ignore ending node).
  28479. \param previous_node Array that gives the previous node index in the path to the starting node
  28480. (optional parameter).
  28481. \return Array of distances of each node to the starting node.
  28482. **/
  28483. template<typename tf, typename t>
  28484. static CImg<T> dijkstra(const tf& distance, const unsigned int nb_nodes,
  28485. const unsigned int starting_node, const unsigned int ending_node,
  28486. CImg<t>& previous_node) {
  28487. if (starting_node>=nb_nodes)
  28488. throw CImgArgumentException("CImg<%s>::dijkstra(): Specified index of starting node %u is higher "
  28489. "than number of nodes %u.",
  28490. pixel_type(),starting_node,nb_nodes);
  28491. CImg<T> dist(1,nb_nodes,1,1,cimg::type<T>::max());
  28492. dist(starting_node) = 0;
  28493. previous_node.assign(1,nb_nodes,1,1,(t)-1);
  28494. previous_node(starting_node) = (t)starting_node;
  28495. CImg<uintT> Q(nb_nodes);
  28496. cimg_forX(Q,u) Q(u) = (unsigned int)u;
  28497. cimg::swap(Q(starting_node),Q(0));
  28498. unsigned int sizeQ = nb_nodes;
  28499. while (sizeQ) {
  28500. // Update neighbors from minimal vertex
  28501. const unsigned int umin = Q(0);
  28502. if (umin==ending_node) sizeQ = 0;
  28503. else {
  28504. const T dmin = dist(umin);
  28505. const T infty = cimg::type<T>::max();
  28506. for (unsigned int q = 1; q<sizeQ; ++q) {
  28507. const unsigned int v = Q(q);
  28508. const T d = (T)distance(v,umin);
  28509. if (d<infty) {
  28510. const T alt = dmin + d;
  28511. if (alt<dist(v)) {
  28512. dist(v) = alt;
  28513. previous_node(v) = (t)umin;
  28514. const T distpos = dist(Q(q));
  28515. for (unsigned int pos = q, par = 0; pos && distpos<dist(Q(par=(pos + 1)/2 - 1)); pos=par)
  28516. cimg::swap(Q(pos),Q(par));
  28517. }
  28518. }
  28519. }
  28520. // Remove minimal vertex from queue
  28521. Q(0) = Q(--sizeQ);
  28522. const T distpos = dist(Q(0));
  28523. for (unsigned int pos = 0, left = 0, right = 0;
  28524. ((right=2*(pos + 1),(left=right - 1))<sizeQ && distpos>dist(Q(left))) ||
  28525. (right<sizeQ && distpos>dist(Q(right)));) {
  28526. if (right<sizeQ) {
  28527. if (dist(Q(left))<dist(Q(right))) { cimg::swap(Q(pos),Q(left)); pos = left; }
  28528. else { cimg::swap(Q(pos),Q(right)); pos = right; }
  28529. } else { cimg::swap(Q(pos),Q(left)); pos = left; }
  28530. }
  28531. }
  28532. }
  28533. return dist;
  28534. }
  28535. //! Return minimal path in a graph, using the Dijkstra algorithm.
  28536. template<typename tf, typename t>
  28537. static CImg<T> dijkstra(const tf& distance, const unsigned int nb_nodes,
  28538. const unsigned int starting_node, const unsigned int ending_node=~0U) {
  28539. CImg<uintT> foo;
  28540. return dijkstra(distance,nb_nodes,starting_node,ending_node,foo);
  28541. }
  28542. //! Return minimal path in a graph, using the Dijkstra algorithm.
  28543. /**
  28544. \param starting_node Index of the starting node.
  28545. \param ending_node Index of the ending node.
  28546. \param previous_node Array that gives the previous node index in the path to the starting node
  28547. (optional parameter).
  28548. \return Array of distances of each node to the starting node.
  28549. \note image instance corresponds to the adjacency matrix of the graph.
  28550. **/
  28551. template<typename t>
  28552. CImg<T>& dijkstra(const unsigned int starting_node, const unsigned int ending_node,
  28553. CImg<t>& previous_node) {
  28554. return get_dijkstra(starting_node,ending_node,previous_node).move_to(*this);
  28555. }
  28556. //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance.
  28557. template<typename t>
  28558. CImg<T> get_dijkstra(const unsigned int starting_node, const unsigned int ending_node,
  28559. CImg<t>& previous_node) const {
  28560. if (_width!=_height || _depth!=1 || _spectrum!=1)
  28561. throw CImgInstanceException(_cimg_instance
  28562. "dijkstra(): Instance is not a graph adjacency matrix.",
  28563. cimg_instance);
  28564. return dijkstra(*this,_width,starting_node,ending_node,previous_node);
  28565. }
  28566. //! Return minimal path in a graph, using the Dijkstra algorithm.
  28567. CImg<T>& dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) {
  28568. return get_dijkstra(starting_node,ending_node).move_to(*this);
  28569. }
  28570. //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance.
  28571. CImg<Tfloat> get_dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) const {
  28572. CImg<uintT> foo;
  28573. return get_dijkstra(starting_node,ending_node,foo);
  28574. }
  28575. //! Return an image containing the character codes of specified string.
  28576. /**
  28577. \param str input C-string to encode as an image.
  28578. \param is_last_zero Tells if the ending \c '0' character appear in the resulting image.
  28579. \param is_shared Return result that shares its buffer with \p str.
  28580. **/
  28581. static CImg<T> string(const char *const str, const bool is_last_zero=true, const bool is_shared=false) {
  28582. if (!str) return CImg<T>();
  28583. return CImg<T>(str,(unsigned int)std::strlen(str) + (is_last_zero?1:0),1,1,1,is_shared);
  28584. }
  28585. //! Return a \c 1x1 image containing specified value.
  28586. /**
  28587. \param a0 First vector value.
  28588. **/
  28589. static CImg<T> row_vector(const T& a0) {
  28590. return vector(a0);
  28591. }
  28592. //! Return a \c 2x1 image containing specified values.
  28593. /**
  28594. \param a0 First vector value.
  28595. \param a1 Second vector value.
  28596. **/
  28597. static CImg<T> row_vector(const T& a0, const T& a1) {
  28598. CImg<T> r(2,1);
  28599. r[0] = a0; r[1] = a1;
  28600. return r;
  28601. }
  28602. //! Return a \c 3x1 image containing specified values.
  28603. /**
  28604. \param a0 First vector value.
  28605. \param a1 Second vector value.
  28606. \param a2 Third vector value.
  28607. **/
  28608. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2) {
  28609. CImg<T> r(3,1);
  28610. r[0] = a0; r[1] = a1; r[2] = a2;
  28611. return r;
  28612. }
  28613. //! Return a \c 4x1 image containing specified values.
  28614. /**
  28615. \param a0 First vector value.
  28616. \param a1 Second vector value.
  28617. \param a2 Third vector value.
  28618. \param a3 Fourth vector value.
  28619. **/
  28620. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3) {
  28621. CImg<T> r(4,1);
  28622. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3;
  28623. return r;
  28624. }
  28625. //! Return a \c 5x1 image containing specified values.
  28626. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
  28627. CImg<T> r(5,1);
  28628. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4;
  28629. return r;
  28630. }
  28631. //! Return a \c 6x1 image containing specified values.
  28632. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
  28633. CImg<T> r(6,1);
  28634. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5;
  28635. return r;
  28636. }
  28637. //! Return a \c 7x1 image containing specified values.
  28638. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28639. const T& a4, const T& a5, const T& a6) {
  28640. CImg<T> r(7,1);
  28641. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6;
  28642. return r;
  28643. }
  28644. //! Return a \c 8x1 image containing specified values.
  28645. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28646. const T& a4, const T& a5, const T& a6, const T& a7) {
  28647. CImg<T> r(8,1);
  28648. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7;
  28649. return r;
  28650. }
  28651. //! Return a \c 9x1 image containing specified values.
  28652. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28653. const T& a4, const T& a5, const T& a6, const T& a7,
  28654. const T& a8) {
  28655. CImg<T> r(9,1);
  28656. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8;
  28657. return r;
  28658. }
  28659. //! Return a \c 10x1 image containing specified values.
  28660. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28661. const T& a4, const T& a5, const T& a6, const T& a7,
  28662. const T& a8, const T& a9) {
  28663. CImg<T> r(10,1);
  28664. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28665. return r;
  28666. }
  28667. //! Return a \c 11x1 image containing specified values.
  28668. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28669. const T& a4, const T& a5, const T& a6, const T& a7,
  28670. const T& a8, const T& a9, const T& a10) {
  28671. CImg<T> r(11,1);
  28672. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28673. r[10] = a10;
  28674. return r;
  28675. }
  28676. //! Return a \c 12x1 image containing specified values.
  28677. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28678. const T& a4, const T& a5, const T& a6, const T& a7,
  28679. const T& a8, const T& a9, const T& a10, const T& a11) {
  28680. CImg<T> r(12,1);
  28681. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28682. r[10] = a10; r[11] = a11;
  28683. return r;
  28684. }
  28685. //! Return a \c 13x1 image containing specified values.
  28686. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28687. const T& a4, const T& a5, const T& a6, const T& a7,
  28688. const T& a8, const T& a9, const T& a10, const T& a11,
  28689. const T& a12) {
  28690. CImg<T> r(13,1);
  28691. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28692. r[10] = a10; r[11] = a11; r[12] = a12;
  28693. return r;
  28694. }
  28695. //! Return a \c 14x1 image containing specified values.
  28696. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28697. const T& a4, const T& a5, const T& a6, const T& a7,
  28698. const T& a8, const T& a9, const T& a10, const T& a11,
  28699. const T& a12, const T& a13) {
  28700. CImg<T> r(14,1);
  28701. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28702. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13;
  28703. return r;
  28704. }
  28705. //! Return a \c 15x1 image containing specified values.
  28706. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28707. const T& a4, const T& a5, const T& a6, const T& a7,
  28708. const T& a8, const T& a9, const T& a10, const T& a11,
  28709. const T& a12, const T& a13, const T& a14) {
  28710. CImg<T> r(15,1);
  28711. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28712. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14;
  28713. return r;
  28714. }
  28715. //! Return a \c 16x1 image containing specified values.
  28716. static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28717. const T& a4, const T& a5, const T& a6, const T& a7,
  28718. const T& a8, const T& a9, const T& a10, const T& a11,
  28719. const T& a12, const T& a13, const T& a14, const T& a15) {
  28720. CImg<T> r(16,1);
  28721. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28722. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14; r[15] = a15;
  28723. return r;
  28724. }
  28725. //! Return a \c 1x1 image containing specified value.
  28726. /**
  28727. \param a0 First vector value.
  28728. **/
  28729. static CImg<T> vector(const T& a0) {
  28730. CImg<T> r(1,1);
  28731. r[0] = a0;
  28732. return r;
  28733. }
  28734. //! Return a \c 1x2 image containing specified values.
  28735. /**
  28736. \param a0 First vector value.
  28737. \param a1 Second vector value.
  28738. **/
  28739. static CImg<T> vector(const T& a0, const T& a1) {
  28740. CImg<T> r(1,2);
  28741. r[0] = a0; r[1] = a1;
  28742. return r;
  28743. }
  28744. //! Return a \c 1x3 image containing specified values.
  28745. /**
  28746. \param a0 First vector value.
  28747. \param a1 Second vector value.
  28748. \param a2 Third vector value.
  28749. **/
  28750. static CImg<T> vector(const T& a0, const T& a1, const T& a2) {
  28751. CImg<T> r(1,3);
  28752. r[0] = a0; r[1] = a1; r[2] = a2;
  28753. return r;
  28754. }
  28755. //! Return a \c 1x4 image containing specified values.
  28756. /**
  28757. \param a0 First vector value.
  28758. \param a1 Second vector value.
  28759. \param a2 Third vector value.
  28760. \param a3 Fourth vector value.
  28761. **/
  28762. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3) {
  28763. CImg<T> r(1,4);
  28764. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3;
  28765. return r;
  28766. }
  28767. //! Return a \c 1x5 image containing specified values.
  28768. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
  28769. CImg<T> r(1,5);
  28770. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4;
  28771. return r;
  28772. }
  28773. //! Return a \c 1x6 image containing specified values.
  28774. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
  28775. CImg<T> r(1,6);
  28776. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5;
  28777. return r;
  28778. }
  28779. //! Return a \c 1x7 image containing specified values.
  28780. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28781. const T& a4, const T& a5, const T& a6) {
  28782. CImg<T> r(1,7);
  28783. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6;
  28784. return r;
  28785. }
  28786. //! Return a \c 1x8 image containing specified values.
  28787. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28788. const T& a4, const T& a5, const T& a6, const T& a7) {
  28789. CImg<T> r(1,8);
  28790. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7;
  28791. return r;
  28792. }
  28793. //! Return a \c 1x9 image containing specified values.
  28794. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28795. const T& a4, const T& a5, const T& a6, const T& a7,
  28796. const T& a8) {
  28797. CImg<T> r(1,9);
  28798. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8;
  28799. return r;
  28800. }
  28801. //! Return a \c 1x10 image containing specified values.
  28802. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28803. const T& a4, const T& a5, const T& a6, const T& a7,
  28804. const T& a8, const T& a9) {
  28805. CImg<T> r(1,10);
  28806. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28807. return r;
  28808. }
  28809. //! Return a \c 1x11 image containing specified values.
  28810. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28811. const T& a4, const T& a5, const T& a6, const T& a7,
  28812. const T& a8, const T& a9, const T& a10) {
  28813. CImg<T> r(1,11);
  28814. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28815. r[10] = a10;
  28816. return r;
  28817. }
  28818. //! Return a \c 1x12 image containing specified values.
  28819. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28820. const T& a4, const T& a5, const T& a6, const T& a7,
  28821. const T& a8, const T& a9, const T& a10, const T& a11) {
  28822. CImg<T> r(1,12);
  28823. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28824. r[10] = a10; r[11] = a11;
  28825. return r;
  28826. }
  28827. //! Return a \c 1x13 image containing specified values.
  28828. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28829. const T& a4, const T& a5, const T& a6, const T& a7,
  28830. const T& a8, const T& a9, const T& a10, const T& a11,
  28831. const T& a12) {
  28832. CImg<T> r(1,13);
  28833. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28834. r[10] = a10; r[11] = a11; r[12] = a12;
  28835. return r;
  28836. }
  28837. //! Return a \c 1x14 image containing specified values.
  28838. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28839. const T& a4, const T& a5, const T& a6, const T& a7,
  28840. const T& a8, const T& a9, const T& a10, const T& a11,
  28841. const T& a12, const T& a13) {
  28842. CImg<T> r(1,14);
  28843. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28844. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13;
  28845. return r;
  28846. }
  28847. //! Return a \c 1x15 image containing specified values.
  28848. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28849. const T& a4, const T& a5, const T& a6, const T& a7,
  28850. const T& a8, const T& a9, const T& a10, const T& a11,
  28851. const T& a12, const T& a13, const T& a14) {
  28852. CImg<T> r(1,15);
  28853. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28854. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14;
  28855. return r;
  28856. }
  28857. //! Return a \c 1x16 image containing specified values.
  28858. static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
  28859. const T& a4, const T& a5, const T& a6, const T& a7,
  28860. const T& a8, const T& a9, const T& a10, const T& a11,
  28861. const T& a12, const T& a13, const T& a14, const T& a15) {
  28862. CImg<T> r(1,16);
  28863. r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7; r[8] = a8; r[9] = a9;
  28864. r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14; r[15] = a15;
  28865. return r;
  28866. }
  28867. //! Return a 1x1 matrix containing specified coefficients.
  28868. /**
  28869. \param a0 First matrix value.
  28870. \note Equivalent to vector(const T&).
  28871. **/
  28872. static CImg<T> matrix(const T& a0) {
  28873. return vector(a0);
  28874. }
  28875. //! Return a 2x2 matrix containing specified coefficients.
  28876. /**
  28877. \param a0 First matrix value.
  28878. \param a1 Second matrix value.
  28879. \param a2 Third matrix value.
  28880. \param a3 Fourth matrix value.
  28881. **/
  28882. static CImg<T> matrix(const T& a0, const T& a1,
  28883. const T& a2, const T& a3) {
  28884. CImg<T> r(2,2); T *ptr = r._data;
  28885. *(ptr++) = a0; *(ptr++) = a1;
  28886. *(ptr++) = a2; *(ptr++) = a3;
  28887. return r;
  28888. }
  28889. //! Return a 3x3 matrix containing specified coefficients.
  28890. /**
  28891. \param a0 First matrix value.
  28892. \param a1 Second matrix value.
  28893. \param a2 Third matrix value.
  28894. \param a3 Fourth matrix value.
  28895. \param a4 Fifth matrix value.
  28896. \param a5 Sixth matrix value.
  28897. \param a6 Seventh matrix value.
  28898. \param a7 Eighth matrix value.
  28899. \param a8 Ninth matrix value.
  28900. **/
  28901. static CImg<T> matrix(const T& a0, const T& a1, const T& a2,
  28902. const T& a3, const T& a4, const T& a5,
  28903. const T& a6, const T& a7, const T& a8) {
  28904. CImg<T> r(3,3); T *ptr = r._data;
  28905. *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2;
  28906. *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5;
  28907. *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8;
  28908. return r;
  28909. }
  28910. //! Return a 4x4 matrix containing specified coefficients.
  28911. static CImg<T> matrix(const T& a0, const T& a1, const T& a2, const T& a3,
  28912. const T& a4, const T& a5, const T& a6, const T& a7,
  28913. const T& a8, const T& a9, const T& a10, const T& a11,
  28914. const T& a12, const T& a13, const T& a14, const T& a15) {
  28915. CImg<T> r(4,4); T *ptr = r._data;
  28916. *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3;
  28917. *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7;
  28918. *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11;
  28919. *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15;
  28920. return r;
  28921. }
  28922. //! Return a 5x5 matrix containing specified coefficients.
  28923. static CImg<T> matrix(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4,
  28924. const T& a5, const T& a6, const T& a7, const T& a8, const T& a9,
  28925. const T& a10, const T& a11, const T& a12, const T& a13, const T& a14,
  28926. const T& a15, const T& a16, const T& a17, const T& a18, const T& a19,
  28927. const T& a20, const T& a21, const T& a22, const T& a23, const T& a24) {
  28928. CImg<T> r(5,5); T *ptr = r._data;
  28929. *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4;
  28930. *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; *(ptr++) = a9;
  28931. *(ptr++) = a10; *(ptr++) = a11; *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14;
  28932. *(ptr++) = a15; *(ptr++) = a16; *(ptr++) = a17; *(ptr++) = a18; *(ptr++) = a19;
  28933. *(ptr++) = a20; *(ptr++) = a21; *(ptr++) = a22; *(ptr++) = a23; *(ptr++) = a24;
  28934. return r;
  28935. }
  28936. //! Return a 1x1 symmetric matrix containing specified coefficients.
  28937. /**
  28938. \param a0 First matrix value.
  28939. \note Equivalent to vector(const T&).
  28940. **/
  28941. static CImg<T> tensor(const T& a0) {
  28942. return matrix(a0);
  28943. }
  28944. //! Return a 2x2 symmetric matrix tensor containing specified coefficients.
  28945. static CImg<T> tensor(const T& a0, const T& a1, const T& a2) {
  28946. return matrix(a0,a1,a1,a2);
  28947. }
  28948. //! Return a 3x3 symmetric matrix containing specified coefficients.
  28949. static CImg<T> tensor(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
  28950. return matrix(a0,a1,a2,a1,a3,a4,a2,a4,a5);
  28951. }
  28952. //! Return a 1x1 diagonal matrix containing specified coefficients.
  28953. static CImg<T> diagonal(const T& a0) {
  28954. return matrix(a0);
  28955. }
  28956. //! Return a 2x2 diagonal matrix containing specified coefficients.
  28957. static CImg<T> diagonal(const T& a0, const T& a1) {
  28958. return matrix(a0,0,0,a1);
  28959. }
  28960. //! Return a 3x3 diagonal matrix containing specified coefficients.
  28961. static CImg<T> diagonal(const T& a0, const T& a1, const T& a2) {
  28962. return matrix(a0,0,0,0,a1,0,0,0,a2);
  28963. }
  28964. //! Return a 4x4 diagonal matrix containing specified coefficients.
  28965. static CImg<T> diagonal(const T& a0, const T& a1, const T& a2, const T& a3) {
  28966. return matrix(a0,0,0,0,0,a1,0,0,0,0,a2,0,0,0,0,a3);
  28967. }
  28968. //! Return a 5x5 diagonal matrix containing specified coefficients.
  28969. static CImg<T> diagonal(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
  28970. return matrix(a0,0,0,0,0,0,a1,0,0,0,0,0,a2,0,0,0,0,0,a3,0,0,0,0,0,a4);
  28971. }
  28972. //! Return a NxN identity matrix.
  28973. /**
  28974. \param N Dimension of the matrix.
  28975. **/
  28976. static CImg<T> identity_matrix(const unsigned int N) {
  28977. CImg<T> res(N,N,1,1,0);
  28978. cimg_forX(res,x) res(x,x) = 1;
  28979. return res;
  28980. }
  28981. //! Return a N-numbered sequence vector from \p a0 to \p a1.
  28982. /**
  28983. \param N Size of the resulting vector.
  28984. \param a0 Starting value of the sequence.
  28985. \param a1 Ending value of the sequence.
  28986. **/
  28987. static CImg<T> sequence(const unsigned int N, const T& a0, const T& a1) {
  28988. if (N) return CImg<T>(1,N).sequence(a0,a1);
  28989. return CImg<T>();
  28990. }
  28991. //! Return a 3x3 rotation matrix from an { axis + angle } or a quaternion.
  28992. /**
  28993. \param x X-coordinate of the rotation axis, or first quaternion coordinate.
  28994. \param y Y-coordinate of the rotation axis, or second quaternion coordinate.
  28995. \param z Z-coordinate of the rotation axis, or third quaternion coordinate.
  28996. \param w Angle of the rotation axis (in degree), or fourth quaternion coordinate.
  28997. \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion (x,y,z,w).
  28998. **/
  28999. static CImg<T> rotation_matrix(const float x, const float y, const float z, const float w,
  29000. const bool is_quaternion=false) {
  29001. double X, Y, Z, W, N;
  29002. if (is_quaternion) {
  29003. N = std::sqrt((double)x*x + (double)y*y + (double)z*z + (double)w*w);
  29004. if (N>0) { X = x/N; Y = y/N; Z = z/N; W = w/N; }
  29005. else { X = Y = Z = 0; W = 1; }
  29006. return CImg<T>::matrix((T)(X*X + Y*Y - Z*Z - W*W),(T)(2*Y*Z - 2*X*W),(T)(2*X*Z + 2*Y*W),
  29007. (T)(2*X*W + 2*Y*Z),(T)(X*X - Y*Y + Z*Z - W*W),(T)(2*Z*W - 2*X*Y),
  29008. (T)(2*Y*W - 2*X*Z),(T)(2*X*Y + 2*Z*W),(T)(X*X - Y*Y - Z*Z + W*W));
  29009. }
  29010. N = cimg::hypot((double)x,(double)y,(double)z);
  29011. if (N>0) { X = x/N; Y = y/N; Z = z/N; }
  29012. else { X = Y = 0; Z = 1; }
  29013. const double ang = w*cimg::PI/180, c = std::cos(ang), omc = 1 - c, s = std::sin(ang);
  29014. return CImg<T>::matrix((T)(X*X*omc + c),(T)(X*Y*omc - Z*s),(T)(X*Z*omc + Y*s),
  29015. (T)(X*Y*omc + Z*s),(T)(Y*Y*omc + c),(T)(Y*Z*omc - X*s),
  29016. (T)(X*Z*omc - Y*s),(T)(Y*Z*omc + X*s),(T)(Z*Z*omc + c));
  29017. }
  29018. //@}
  29019. //-----------------------------------
  29020. //
  29021. //! \name Value Manipulation
  29022. //@{
  29023. //-----------------------------------
  29024. //! Fill all pixel values with specified value.
  29025. /**
  29026. \param val Fill value.
  29027. **/
  29028. CImg<T>& fill(const T& val) {
  29029. if (is_empty()) return *this;
  29030. if (val && sizeof(T)!=1) cimg_for(*this,ptrd,T) *ptrd = val;
  29031. else std::memset(_data,(int)(ulongT)val,sizeof(T)*size()); // Double cast to allow val to be (void*)
  29032. return *this;
  29033. }
  29034. //! Fill all pixel values with specified value \newinstance.
  29035. CImg<T> get_fill(const T& val) const {
  29036. return CImg<T>(_width,_height,_depth,_spectrum).fill(val);
  29037. }
  29038. //! Fill sequentially all pixel values with specified values.
  29039. /**
  29040. \param val0 First fill value.
  29041. \param val1 Second fill value.
  29042. **/
  29043. CImg<T>& fill(const T& val0, const T& val1) {
  29044. if (is_empty()) return *this;
  29045. T *ptrd, *ptre = end() - 1;
  29046. for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; }
  29047. if (ptrd!=ptre + 1) *(ptrd++) = val0;
  29048. return *this;
  29049. }
  29050. //! Fill sequentially all pixel values with specified values \newinstance.
  29051. CImg<T> get_fill(const T& val0, const T& val1) const {
  29052. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1);
  29053. }
  29054. //! Fill sequentially all pixel values with specified values \overloading.
  29055. CImg<T>& fill(const T& val0, const T& val1, const T& val2) {
  29056. if (is_empty()) return *this;
  29057. T *ptrd, *ptre = end() - 2;
  29058. for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; }
  29059. ptre+=2;
  29060. switch (ptre - ptrd) {
  29061. case 2 : *(--ptre) = val1; // fallthrough
  29062. case 1 : *(--ptre) = val0; // fallthrough
  29063. }
  29064. return *this;
  29065. }
  29066. //! Fill sequentially all pixel values with specified values \newinstance.
  29067. CImg<T> get_fill(const T& val0, const T& val1, const T& val2) const {
  29068. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2);
  29069. }
  29070. //! Fill sequentially all pixel values with specified values \overloading.
  29071. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3) {
  29072. if (is_empty()) return *this;
  29073. T *ptrd, *ptre = end() - 3;
  29074. for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; }
  29075. ptre+=3;
  29076. switch (ptre - ptrd) {
  29077. case 3 : *(--ptre) = val2; // fallthrough
  29078. case 2 : *(--ptre) = val1; // fallthrough
  29079. case 1 : *(--ptre) = val0; // fallthrough
  29080. }
  29081. return *this;
  29082. }
  29083. //! Fill sequentially all pixel values with specified values \newinstance.
  29084. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3) const {
  29085. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3);
  29086. }
  29087. //! Fill sequentially all pixel values with specified values \overloading.
  29088. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4) {
  29089. if (is_empty()) return *this;
  29090. T *ptrd, *ptre = end() - 4;
  29091. for (ptrd = _data; ptrd<ptre; ) {
  29092. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
  29093. }
  29094. ptre+=4;
  29095. switch (ptre - ptrd) {
  29096. case 4 : *(--ptre) = val3; // fallthrough
  29097. case 3 : *(--ptre) = val2; // fallthrough
  29098. case 2 : *(--ptre) = val1; // fallthrough
  29099. case 1 : *(--ptre) = val0; // fallthrough
  29100. }
  29101. return *this;
  29102. }
  29103. //! Fill sequentially all pixel values with specified values \newinstance.
  29104. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4) const {
  29105. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4);
  29106. }
  29107. //! Fill sequentially all pixel values with specified values \overloading.
  29108. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5) {
  29109. if (is_empty()) return *this;
  29110. T *ptrd, *ptre = end() - 5;
  29111. for (ptrd = _data; ptrd<ptre; ) {
  29112. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29113. }
  29114. ptre+=5;
  29115. switch (ptre - ptrd) {
  29116. case 5 : *(--ptre) = val4; // fallthrough
  29117. case 4 : *(--ptre) = val3; // fallthrough
  29118. case 3 : *(--ptre) = val2; // fallthrough
  29119. case 2 : *(--ptre) = val1; // fallthrough
  29120. case 1 : *(--ptre) = val0; // fallthrough
  29121. }
  29122. return *this;
  29123. }
  29124. //! Fill sequentially all pixel values with specified values \newinstance.
  29125. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5) const {
  29126. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5);
  29127. }
  29128. //! Fill sequentially all pixel values with specified values \overloading.
  29129. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29130. const T& val6) {
  29131. if (is_empty()) return *this;
  29132. T *ptrd, *ptre = end() - 6;
  29133. for (ptrd = _data; ptrd<ptre; ) {
  29134. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29135. *(ptrd++) = val6;
  29136. }
  29137. ptre+=6;
  29138. switch (ptre - ptrd) {
  29139. case 6 : *(--ptre) = val5; // fallthrough
  29140. case 5 : *(--ptre) = val4; // fallthrough
  29141. case 4 : *(--ptre) = val3; // fallthrough
  29142. case 3 : *(--ptre) = val2; // fallthrough
  29143. case 2 : *(--ptre) = val1; // fallthrough
  29144. case 1 : *(--ptre) = val0; // fallthrough
  29145. }
  29146. return *this;
  29147. }
  29148. //! Fill sequentially all pixel values with specified values \newinstance.
  29149. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29150. const T& val6) const {
  29151. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6);
  29152. }
  29153. //! Fill sequentially all pixel values with specified values \overloading.
  29154. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29155. const T& val6, const T& val7) {
  29156. if (is_empty()) return *this;
  29157. T *ptrd, *ptre = end() - 7;
  29158. for (ptrd = _data; ptrd<ptre; ) {
  29159. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3;
  29160. *(ptrd++) = val4; *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7;
  29161. }
  29162. ptre+=7;
  29163. switch (ptre - ptrd) {
  29164. case 7 : *(--ptre) = val6; // fallthrough
  29165. case 6 : *(--ptre) = val5; // fallthrough
  29166. case 5 : *(--ptre) = val4; // fallthrough
  29167. case 4 : *(--ptre) = val3; // fallthrough
  29168. case 3 : *(--ptre) = val2; // fallthrough
  29169. case 2 : *(--ptre) = val1; // fallthrough
  29170. case 1 : *(--ptre) = val0; // fallthrough
  29171. }
  29172. return *this;
  29173. }
  29174. //! Fill sequentially all pixel values with specified values \newinstance.
  29175. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29176. const T& val6, const T& val7) const {
  29177. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7);
  29178. }
  29179. //! Fill sequentially all pixel values with specified values \overloading.
  29180. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29181. const T& val6, const T& val7, const T& val8) {
  29182. if (is_empty()) return *this;
  29183. T *ptrd, *ptre = end() - 8;
  29184. for (ptrd = _data; ptrd<ptre; ) {
  29185. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2;
  29186. *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29187. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8;
  29188. }
  29189. ptre+=8;
  29190. switch (ptre - ptrd) {
  29191. case 8 : *(--ptre) = val7; // fallthrough
  29192. case 7 : *(--ptre) = val6; // fallthrough
  29193. case 6 : *(--ptre) = val5; // fallthrough
  29194. case 5 : *(--ptre) = val4; // fallthrough
  29195. case 4 : *(--ptre) = val3; // fallthrough
  29196. case 3 : *(--ptre) = val2; // fallthrough
  29197. case 2 : *(--ptre) = val1; // fallthrough
  29198. case 1 : *(--ptre) = val0; // fallthrough
  29199. }
  29200. return *this;
  29201. }
  29202. //! Fill sequentially all pixel values with specified values \newinstance.
  29203. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29204. const T& val6, const T& val7, const T& val8) const {
  29205. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8);
  29206. }
  29207. //! Fill sequentially all pixel values with specified values \overloading.
  29208. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29209. const T& val6, const T& val7, const T& val8, const T& val9) {
  29210. if (is_empty()) return *this;
  29211. T *ptrd, *ptre = end() - 9;
  29212. for (ptrd = _data; ptrd<ptre; ) {
  29213. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
  29214. *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9;
  29215. }
  29216. ptre+=9;
  29217. switch (ptre - ptrd) {
  29218. case 9 : *(--ptre) = val8; // fallthrough
  29219. case 8 : *(--ptre) = val7; // fallthrough
  29220. case 7 : *(--ptre) = val6; // fallthrough
  29221. case 6 : *(--ptre) = val5; // fallthrough
  29222. case 5 : *(--ptre) = val4; // fallthrough
  29223. case 4 : *(--ptre) = val3; // fallthrough
  29224. case 3 : *(--ptre) = val2; // fallthrough
  29225. case 2 : *(--ptre) = val1; // fallthrough
  29226. case 1 : *(--ptre) = val0; // fallthrough
  29227. }
  29228. return *this;
  29229. }
  29230. //! Fill sequentially all pixel values with specified values \newinstance.
  29231. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29232. const T& val6, const T& val7, const T& val8, const T& val9) const {
  29233. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9);
  29234. }
  29235. //! Fill sequentially all pixel values with specified values \overloading.
  29236. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29237. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10) {
  29238. if (is_empty()) return *this;
  29239. T *ptrd, *ptre = end() - 10;
  29240. for (ptrd = _data; ptrd<ptre; ) {
  29241. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
  29242. *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9;
  29243. *(ptrd++) = val10;
  29244. }
  29245. ptre+=10;
  29246. switch (ptre - ptrd) {
  29247. case 10 : *(--ptre) = val9; // fallthrough
  29248. case 9 : *(--ptre) = val8; // fallthrough
  29249. case 8 : *(--ptre) = val7; // fallthrough
  29250. case 7 : *(--ptre) = val6; // fallthrough
  29251. case 6 : *(--ptre) = val5; // fallthrough
  29252. case 5 : *(--ptre) = val4; // fallthrough
  29253. case 4 : *(--ptre) = val3; // fallthrough
  29254. case 3 : *(--ptre) = val2; // fallthrough
  29255. case 2 : *(--ptre) = val1; // fallthrough
  29256. case 1 : *(--ptre) = val0; // fallthrough
  29257. }
  29258. return *this;
  29259. }
  29260. //! Fill sequentially all pixel values with specified values \newinstance.
  29261. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29262. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10) const {
  29263. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10);
  29264. }
  29265. //! Fill sequentially all pixel values with specified values \overloading.
  29266. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29267. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11) {
  29268. if (is_empty()) return *this;
  29269. T *ptrd, *ptre = end() - 11;
  29270. for (ptrd = _data; ptrd<ptre; ) {
  29271. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29272. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
  29273. }
  29274. ptre+=11;
  29275. switch (ptre - ptrd) {
  29276. case 11 : *(--ptre) = val10; // fallthrough
  29277. case 10 : *(--ptre) = val9; // fallthrough
  29278. case 9 : *(--ptre) = val8; // fallthrough
  29279. case 8 : *(--ptre) = val7; // fallthrough
  29280. case 7 : *(--ptre) = val6; // fallthrough
  29281. case 6 : *(--ptre) = val5; // fallthrough
  29282. case 5 : *(--ptre) = val4; // fallthrough
  29283. case 4 : *(--ptre) = val3; // fallthrough
  29284. case 3 : *(--ptre) = val2; // fallthrough
  29285. case 2 : *(--ptre) = val1; // fallthrough
  29286. case 1 : *(--ptre) = val0; // fallthrough
  29287. }
  29288. return *this;
  29289. }
  29290. //! Fill sequentially all pixel values with specified values \newinstance.
  29291. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29292. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11) const {
  29293. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
  29294. val11);
  29295. }
  29296. //! Fill sequentially all pixel values with specified values \overloading.
  29297. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29298. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29299. const T& val12) {
  29300. if (is_empty()) return *this;
  29301. T *ptrd, *ptre = end() - 12;
  29302. for (ptrd = _data; ptrd<ptre; ) {
  29303. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29304. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
  29305. *(ptrd++) = val12;
  29306. }
  29307. ptre+=12;
  29308. switch (ptre - ptrd) {
  29309. case 12 : *(--ptre) = val11; // fallthrough
  29310. case 11 : *(--ptre) = val10; // fallthrough
  29311. case 10 : *(--ptre) = val9; // fallthrough
  29312. case 9 : *(--ptre) = val8; // fallthrough
  29313. case 8 : *(--ptre) = val7; // fallthrough
  29314. case 7 : *(--ptre) = val6; // fallthrough
  29315. case 6 : *(--ptre) = val5; // fallthrough
  29316. case 5 : *(--ptre) = val4; // fallthrough
  29317. case 4 : *(--ptre) = val3; // fallthrough
  29318. case 3 : *(--ptre) = val2; // fallthrough
  29319. case 2 : *(--ptre) = val1; // fallthrough
  29320. case 1 : *(--ptre) = val0; // fallthrough
  29321. }
  29322. return *this;
  29323. }
  29324. //! Fill sequentially all pixel values with specified values \newinstance.
  29325. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29326. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29327. const T& val12) const {
  29328. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
  29329. val11,val12);
  29330. }
  29331. //! Fill sequentially all pixel values with specified values \overloading.
  29332. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29333. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29334. const T& val12, const T& val13) {
  29335. if (is_empty()) return *this;
  29336. T *ptrd, *ptre = end() - 13;
  29337. for (ptrd = _data; ptrd<ptre; ) {
  29338. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29339. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
  29340. *(ptrd++) = val12; *(ptrd++) = val13;
  29341. }
  29342. ptre+=13;
  29343. switch (ptre - ptrd) {
  29344. case 13 : *(--ptre) = val12; // fallthrough
  29345. case 12 : *(--ptre) = val11; // fallthrough
  29346. case 11 : *(--ptre) = val10; // fallthrough
  29347. case 10 : *(--ptre) = val9; // fallthrough
  29348. case 9 : *(--ptre) = val8; // fallthrough
  29349. case 8 : *(--ptre) = val7; // fallthrough
  29350. case 7 : *(--ptre) = val6; // fallthrough
  29351. case 6 : *(--ptre) = val5; // fallthrough
  29352. case 5 : *(--ptre) = val4; // fallthrough
  29353. case 4 : *(--ptre) = val3; // fallthrough
  29354. case 3 : *(--ptre) = val2; // fallthrough
  29355. case 2 : *(--ptre) = val1; // fallthrough
  29356. case 1 : *(--ptre) = val0; // fallthrough
  29357. }
  29358. return *this;
  29359. }
  29360. //! Fill sequentially all pixel values with specified values \newinstance.
  29361. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29362. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29363. const T& val12, const T& val13) const {
  29364. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
  29365. val11,val12,val13);
  29366. }
  29367. //! Fill sequentially all pixel values with specified values \overloading.
  29368. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29369. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29370. const T& val12, const T& val13, const T& val14) {
  29371. if (is_empty()) return *this;
  29372. T *ptrd, *ptre = end() - 14;
  29373. for (ptrd = _data; ptrd<ptre; ) {
  29374. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29375. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
  29376. *(ptrd++) = val12; *(ptrd++) = val13; *(ptrd++) = val14;
  29377. }
  29378. ptre+=14;
  29379. switch (ptre - ptrd) {
  29380. case 14 : *(--ptre) = val13; // fallthrough
  29381. case 13 : *(--ptre) = val12; // fallthrough
  29382. case 12 : *(--ptre) = val11; // fallthrough
  29383. case 11 : *(--ptre) = val10; // fallthrough
  29384. case 10 : *(--ptre) = val9; // fallthrough
  29385. case 9 : *(--ptre) = val8; // fallthrough
  29386. case 8 : *(--ptre) = val7; // fallthrough
  29387. case 7 : *(--ptre) = val6; // fallthrough
  29388. case 6 : *(--ptre) = val5; // fallthrough
  29389. case 5 : *(--ptre) = val4; // fallthrough
  29390. case 4 : *(--ptre) = val3; // fallthrough
  29391. case 3 : *(--ptre) = val2; // fallthrough
  29392. case 2 : *(--ptre) = val1; // fallthrough
  29393. case 1 : *(--ptre) = val0; // fallthrough
  29394. }
  29395. return *this;
  29396. }
  29397. //! Fill sequentially all pixel values with specified values \newinstance.
  29398. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29399. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29400. const T& val12, const T& val13, const T& val14) const {
  29401. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
  29402. val11,val12,val13,val14);
  29403. }
  29404. //! Fill sequentially all pixel values with specified values \overloading.
  29405. CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29406. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29407. const T& val12, const T& val13, const T& val14, const T& val15) {
  29408. if (is_empty()) return *this;
  29409. T *ptrd, *ptre = end() - 15;
  29410. for (ptrd = _data; ptrd<ptre; ) {
  29411. *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
  29412. *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
  29413. *(ptrd++) = val12; *(ptrd++) = val13; *(ptrd++) = val14; *(ptrd++) = val15;
  29414. }
  29415. ptre+=15;
  29416. switch (ptre - ptrd) {
  29417. case 15 : *(--ptre) = val14; // fallthrough
  29418. case 14 : *(--ptre) = val13; // fallthrough
  29419. case 13 : *(--ptre) = val12; // fallthrough
  29420. case 12 : *(--ptre) = val11; // fallthrough
  29421. case 11 : *(--ptre) = val10; // fallthrough
  29422. case 10 : *(--ptre) = val9; // fallthrough
  29423. case 9 : *(--ptre) = val8; // fallthrough
  29424. case 8 : *(--ptre) = val7; // fallthrough
  29425. case 7 : *(--ptre) = val6; // fallthrough
  29426. case 6 : *(--ptre) = val5; // fallthrough
  29427. case 5 : *(--ptre) = val4; // fallthrough
  29428. case 4 : *(--ptre) = val3; // fallthrough
  29429. case 3 : *(--ptre) = val2; // fallthrough
  29430. case 2 : *(--ptre) = val1; // fallthrough
  29431. case 1 : *(--ptre) = val0; // fallthrough
  29432. }
  29433. return *this;
  29434. }
  29435. //! Fill sequentially all pixel values with specified values \newinstance.
  29436. CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
  29437. const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
  29438. const T& val12, const T& val13, const T& val14, const T& val15) const {
  29439. return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
  29440. val11,val12,val13,val14,val15);
  29441. }
  29442. //! Fill sequentially pixel values according to a given expression.
  29443. /**
  29444. \param expression C-string describing a math formula, or a sequence of values.
  29445. \param repeat_values In case a list of values is provided, tells if this list must be repeated for the filling.
  29446. \param allow_formula Tells that mathematical formulas are authorized for the filling.
  29447. \param list_images In case of a mathematical expression, attach a list of images to the specified expression.
  29448. **/
  29449. CImg<T>& fill(const char *const expression, const bool repeat_values, const bool allow_formula=true,
  29450. CImgList<T> *const list_images=0) {
  29451. return _fill(expression,repeat_values,allow_formula?1:0,list_images,"fill",0);
  29452. }
  29453. // 'formula_mode' = { 0 = does not allow formula | 1 = allow formula |
  29454. // 2 = allow formula and do not fill image values }.
  29455. CImg<T>& _fill(const char *const expression, const bool repeat_values, const unsigned int formula_mode,
  29456. CImgList<T> *const list_images, const char *const calling_function, const CImg<T> *provides_copy) {
  29457. if (is_empty() || !expression || !*expression) return *this;
  29458. const unsigned int omode = cimg::exception_mode();
  29459. cimg::exception_mode(0);
  29460. CImg<charT> is_error;
  29461. bool is_value_sequence = false;
  29462. cimg_abort_init;
  29463. if (formula_mode) {
  29464. // Try to pre-detect regular value sequence to avoid exception thrown by _cimg_math_parser.
  29465. double value;
  29466. char sep;
  29467. const int err = cimg_sscanf(expression,"%lf %c",&value,&sep);
  29468. if (err==1 || (err==2 && sep==',')) {
  29469. if (err==1) { if (formula_mode==2) return *this; return fill((T)value); }
  29470. else is_value_sequence = true;
  29471. }
  29472. // Try to fill values according to a formula.
  29473. _cimg_abort_init_openmp;
  29474. if (!is_value_sequence) try {
  29475. CImg<T> base = provides_copy?provides_copy->get_shared():get_shared();
  29476. _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
  29477. *expression=='*' || *expression==':'),
  29478. calling_function,base,this,list_images,true);
  29479. if (!provides_copy && expression && *expression!='>' && *expression!='<' && *expression!=':' &&
  29480. mp.need_input_copy)
  29481. base.assign().assign(*this,false); // Needs input copy
  29482. // Determine 2nd largest image dimension (used as axis for inner loop in parallelized evaluation).
  29483. unsigned int M;
  29484. if (mp.result_dim) {
  29485. M = cimg::max(_width,_height,_depth);
  29486. M = M==_width?std::max(_height,_depth):M==_height?std::max(_width,_depth):std::max(_width,_height);
  29487. } else {
  29488. M = cimg::max(_width,_height,_depth,_spectrum);
  29489. M = M==_width?cimg::max(_height,_depth,_spectrum):
  29490. M==_height?cimg::max(_width,_depth,_spectrum):
  29491. M==_depth?cimg::max(_width,_height,_spectrum):cimg::max(_width,_height,_depth);
  29492. }
  29493. bool do_in_parallel = false;
  29494. #if cimg_use_openmp!=0
  29495. cimg_openmp_if(*expression=='*' || *expression==':' ||
  29496. (mp.is_parallelizable && M>=(cimg_openmp_sizefactor)*320 && size()/M>=2))
  29497. do_in_parallel = true;
  29498. #endif
  29499. if (mp.result_dim) { // Vector-valued expression
  29500. const unsigned int N = std::min(mp.result_dim,_spectrum);
  29501. const ulongT whd = (ulongT)_width*_height*_depth;
  29502. T *ptrd = *expression=='<'?_data + _width*_height*_depth - 1:_data;
  29503. if (*expression=='<') {
  29504. CImg<doubleT> res(1,mp.result_dim);
  29505. mp.begin_t();
  29506. cimg_rofYZ(*this,y,z) {
  29507. cimg_abort_test;
  29508. if (formula_mode==2) cimg_rofX(*this,x) mp(x,y,z,0);
  29509. else cimg_rofX(*this,x) {
  29510. mp(x,y,z,0,res._data);
  29511. const double *ptrs = res._data;
  29512. T *_ptrd = ptrd--; for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; }
  29513. }
  29514. }
  29515. mp.end_t();
  29516. } else if (*expression=='>' || !do_in_parallel) {
  29517. CImg<doubleT> res(1,mp.result_dim);
  29518. mp.begin_t();
  29519. cimg_forYZ(*this,y,z) {
  29520. cimg_abort_test;
  29521. if (formula_mode==2) cimg_forX(*this,x) mp(x,y,z,0);
  29522. else cimg_forX(*this,x) {
  29523. mp(x,y,z,0,res._data);
  29524. const double *ptrs = res._data;
  29525. T *_ptrd = ptrd++; for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; }
  29526. }
  29527. }
  29528. mp.end_t();
  29529. } else {
  29530. #if cimg_use_openmp!=0
  29531. cimg_pragma_openmp(parallel)
  29532. {
  29533. _cimg_math_parser
  29534. *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
  29535. &lmp = *_mp;
  29536. lmp.is_fill = true;
  29537. cimg_pragma_openmp(barrier)
  29538. lmp.begin_t();
  29539. #define _cimg_fill_openmp_vector(_YZ,_y,_z,_X,_x,_sx,_sy,_sz,_off) \
  29540. cimg_pragma_openmp(for cimg_openmp_collapse(2)) \
  29541. cimg_for##_YZ(*this,_y,_z) _cimg_abort_try_openmp { \
  29542. cimg_abort_test; \
  29543. if (formula_mode==2) cimg_for##_X(*this,_x) lmp(x,y,z,0); \
  29544. else { \
  29545. CImg<doubleT> res(1,lmp.result_dim); \
  29546. T *__ptrd = data(_sx,_sy,_sz,0); \
  29547. const ulongT off = (ulongT)_off; \
  29548. cimg_for##_X(*this,_x) { \
  29549. lmp(x,y,z,0,res._data); \
  29550. const double *ptrs = res._data; \
  29551. T *_ptrd = __ptrd; \
  29552. for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; } \
  29553. __ptrd+=off; \
  29554. } \
  29555. } \
  29556. } _cimg_abort_catch_openmp _cimg_abort_catch_fill_openmp
  29557. if (M==_width) { _cimg_fill_openmp_vector(YZ,y,z,X,x,0,y,z,1) }
  29558. else if (M==_height) { _cimg_fill_openmp_vector(XZ,x,z,Y,y,x,0,z,_width) }
  29559. else { _cimg_fill_openmp_vector(XY,x,y,Z,z,x,y,0,_width*_height) }
  29560. lmp.end_t();
  29561. cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
  29562. if (&lmp!=&mp) delete &lmp;
  29563. }
  29564. #endif
  29565. }
  29566. } else { // Scalar-valued expression
  29567. T *ptrd = *expression=='<'?end() - 1:_data;
  29568. if (*expression=='<') {
  29569. mp.begin_t();
  29570. if (formula_mode==2) cimg_rofYZC(*this,y,z,c) { cimg_abort_test; cimg_rofX(*this,x) mp(x,y,z,c); }
  29571. else cimg_rofYZC(*this,y,z,c) { cimg_abort_test; cimg_rofX(*this,x) *(ptrd--) = (T)mp(x,y,z,c); }
  29572. mp.end_t();
  29573. } else if (*expression=='>' || !do_in_parallel) {
  29574. mp.begin_t();
  29575. if (formula_mode==2) cimg_forYZC(*this,y,z,c) { cimg_abort_test; cimg_forX(*this,x) mp(x,y,z,c); }
  29576. else cimg_forYZC(*this,y,z,c) { cimg_abort_test; cimg_forX(*this,x) *(ptrd++) = (T)mp(x,y,z,c); }
  29577. mp.end_t();
  29578. } else {
  29579. #if cimg_use_openmp!=0
  29580. cimg_pragma_openmp(parallel)
  29581. {
  29582. _cimg_math_parser
  29583. *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
  29584. &lmp = *_mp;
  29585. lmp.is_fill = true;
  29586. cimg_pragma_openmp(barrier)
  29587. lmp.begin_t();
  29588. #define _cimg_fill_openmp_scalar(_YZC,_y,_z,_c,_X,_x,_sx,_sy,_sz,_sc,_off) \
  29589. cimg_pragma_openmp(for cimg_openmp_collapse(3)) \
  29590. cimg_for##_YZC(*this,_y,_z,_c) _cimg_abort_try_openmp { \
  29591. cimg_abort_test; \
  29592. if (formula_mode==2) cimg_for##_X(*this,_x) lmp(x,y,z,c); \
  29593. else { \
  29594. T *_ptrd = data(_sx,_sy,_sz,_sc); \
  29595. const ulongT off = (ulongT)_off; \
  29596. cimg_for##_X(*this,_x) { *_ptrd = (T)lmp(x,y,z,c); _ptrd+=off; } \
  29597. } \
  29598. } _cimg_abort_catch_openmp _cimg_abort_catch_fill_openmp
  29599. if (M==_width) { _cimg_fill_openmp_scalar(YZC,y,z,c,X,x,0,y,z,c,1) }
  29600. else if (M==_height) { _cimg_fill_openmp_scalar(XZC,x,z,c,Y,y,x,0,z,c,_width) }
  29601. else if (M==_depth) { _cimg_fill_openmp_scalar(XYC,x,y,c,Z,z,x,y,0,c,_width*_height) }
  29602. else { _cimg_fill_openmp_scalar(XYZ,x,y,z,C,c,x,y,z,0,_width*_height*_depth) }
  29603. lmp.end_t();
  29604. cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
  29605. if (&lmp!=&mp) delete &lmp;
  29606. }
  29607. #endif
  29608. }
  29609. }
  29610. mp.end();
  29611. } catch (CImgException& e) { CImg<charT>::string(e._message).move_to(is_error); }
  29612. }
  29613. // Try to fill values according to a value sequence.
  29614. if (!formula_mode || is_value_sequence || is_error) {
  29615. CImg<charT> item(256);
  29616. char sep = 0;
  29617. const char *nexpression = expression;
  29618. ulongT nb = 0;
  29619. const ulongT siz = size();
  29620. T *ptrd = _data;
  29621. for (double val = 0; *nexpression && nb<siz; ++nb) {
  29622. sep = 0;
  29623. const int err = cimg_sscanf(nexpression,"%255[ \n\t0-9.eEinfa+-]%c",item._data,&sep);
  29624. if (err>0 && cimg_sscanf(item,"%lf",&val)==1 && (sep==',' || sep==';' || err==1)) {
  29625. nexpression+=std::strlen(item) + (err>1);
  29626. *(ptrd++) = (T)val;
  29627. } else break;
  29628. }
  29629. cimg::exception_mode(omode);
  29630. if (nb<siz && (sep || *nexpression)) {
  29631. if (is_error) throw CImgArgumentException("%s",is_error._data);
  29632. else throw CImgArgumentException(_cimg_instance
  29633. "%s(): Invalid sequence of filling values '%s'.",
  29634. cimg_instance,calling_function,expression);
  29635. }
  29636. if (repeat_values && nb && nb<siz)
  29637. for (T *ptrs = _data, *const ptre = _data + siz; ptrd<ptre; ++ptrs) *(ptrd++) = *ptrs;
  29638. }
  29639. cimg::exception_mode(omode);
  29640. cimg_abort_test;
  29641. return *this;
  29642. }
  29643. //! Fill sequentially pixel values according to a given expression \newinstance.
  29644. CImg<T> get_fill(const char *const expression, const bool repeat_values, const bool allow_formula=true,
  29645. CImgList<T> *const list_images=0) const {
  29646. return (+*this).fill(expression,repeat_values,allow_formula?1:0,list_images);
  29647. }
  29648. //! Fill sequentially pixel values according to the values found in another image.
  29649. /**
  29650. \param values Image containing the values used for the filling.
  29651. \param repeat_values In case there are less values than necessary in \c values, tells if these values must be
  29652. repeated for the filling.
  29653. **/
  29654. template<typename t>
  29655. CImg<T>& fill(const CImg<t>& values, const bool repeat_values=true) {
  29656. if (is_empty() || !values) return *this;
  29657. T *ptrd = _data, *ptre = ptrd + size();
  29658. for (t *ptrs = values._data, *ptrs_end = ptrs + values.size(); ptrs<ptrs_end && ptrd<ptre; ++ptrs)
  29659. *(ptrd++) = (T)*ptrs;
  29660. if (repeat_values && ptrd<ptre) for (T *ptrs = _data; ptrd<ptre; ++ptrs) *(ptrd++) = *ptrs;
  29661. return *this;
  29662. }
  29663. //! Fill sequentially pixel values according to the values found in another image \newinstance.
  29664. template<typename t>
  29665. CImg<T> get_fill(const CImg<t>& values, const bool repeat_values=true) const {
  29666. return repeat_values?CImg<T>(_width,_height,_depth,_spectrum).fill(values,repeat_values):
  29667. (+*this).fill(values,repeat_values);
  29668. }
  29669. //! Fill pixel values along the X-axis at a specified pixel position.
  29670. /**
  29671. \param y Y-coordinate of the filled column.
  29672. \param z Z-coordinate of the filled column.
  29673. \param c C-coordinate of the filled column.
  29674. \param a0 First fill value.
  29675. **/
  29676. CImg<T>& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const int a0, ...) {
  29677. #define _cimg_fill1(x,y,z,c,off,siz,t) { \
  29678. va_list ap; va_start(ap,a0); T *ptrd = data(x,y,z,c); *ptrd = (T)a0; \
  29679. for (unsigned int k = 1; k<siz; ++k) { ptrd+=off; *ptrd = (T)va_arg(ap,t); } \
  29680. va_end(ap); }
  29681. if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,int);
  29682. return *this;
  29683. }
  29684. //! Fill pixel values along the X-axis at a specified pixel position \overloading.
  29685. CImg<T>& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const double a0, ...) {
  29686. if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,double);
  29687. return *this;
  29688. }
  29689. //! Fill pixel values along the Y-axis at a specified pixel position.
  29690. /**
  29691. \param x X-coordinate of the filled row.
  29692. \param z Z-coordinate of the filled row.
  29693. \param c C-coordinate of the filled row.
  29694. \param a0 First fill value.
  29695. **/
  29696. CImg<T>& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const int a0, ...) {
  29697. if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,int);
  29698. return *this;
  29699. }
  29700. //! Fill pixel values along the Y-axis at a specified pixel position \overloading.
  29701. CImg<T>& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const double a0, ...) {
  29702. if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,double);
  29703. return *this;
  29704. }
  29705. //! Fill pixel values along the Z-axis at a specified pixel position.
  29706. /**
  29707. \param x X-coordinate of the filled slice.
  29708. \param y Y-coordinate of the filled slice.
  29709. \param c C-coordinate of the filled slice.
  29710. \param a0 First fill value.
  29711. **/
  29712. CImg<T>& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const int a0, ...) {
  29713. const ulongT wh = (ulongT)_width*_height;
  29714. if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,int);
  29715. return *this;
  29716. }
  29717. //! Fill pixel values along the Z-axis at a specified pixel position \overloading.
  29718. CImg<T>& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const double a0, ...) {
  29719. const ulongT wh = (ulongT)_width*_height;
  29720. if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,double);
  29721. return *this;
  29722. }
  29723. //! Fill pixel values along the C-axis at a specified pixel position.
  29724. /**
  29725. \param x X-coordinate of the filled channel.
  29726. \param y Y-coordinate of the filled channel.
  29727. \param z Z-coordinate of the filled channel.
  29728. \param a0 First filling value.
  29729. **/
  29730. CImg<T>& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const int a0, ...) {
  29731. const ulongT whd = (ulongT)_width*_height*_depth;
  29732. if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,int);
  29733. return *this;
  29734. }
  29735. //! Fill pixel values along the C-axis at a specified pixel position \overloading.
  29736. CImg<T>& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const double a0, ...) {
  29737. const ulongT whd = (ulongT)_width*_height*_depth;
  29738. if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,double);
  29739. return *this;
  29740. }
  29741. //! Discard specified sequence of values in the image buffer, along a specific axis.
  29742. /**
  29743. \param values Sequence of values to discard.
  29744. \param axis Axis along which the values are discarded. If set to \c 0 (default value)
  29745. the method does it for all the buffer values and returns a one-column vector.
  29746. \note Discarded values will change the image geometry, so the resulting image
  29747. is returned as a one-column vector.
  29748. **/
  29749. template<typename t>
  29750. CImg<T>& discard(const CImg<t>& values, const char axis=0) {
  29751. if (is_empty() || !values) return *this;
  29752. return get_discard(values,axis).move_to(*this);
  29753. }
  29754. template<typename t>
  29755. CImg<T> get_discard(const CImg<t>& values, const char axis=0) const {
  29756. if (!values) return +*this;
  29757. CImg<T> res;
  29758. if (is_empty()) return res;
  29759. const ulongT vsiz = values.size();
  29760. const char _axis = cimg::lowercase(axis);
  29761. ulongT j = 0;
  29762. unsigned int k = 0;
  29763. int i0 = 0;
  29764. res.assign(width(),height(),depth(),spectrum());
  29765. switch (_axis) {
  29766. case 'x' : {
  29767. cimg_forX(*this,i) {
  29768. if ((*this)(i)!=(T)values[j]) {
  29769. if (j) --i;
  29770. res.draw_image(k,get_columns(i0,i));
  29771. k+=i - i0 + 1; i0 = i + 1; j = 0;
  29772. } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
  29773. }
  29774. if (i0<width()) { res.draw_image(k,get_columns(i0,width() - 1)); k+=width() - i0; }
  29775. res.resize(k,-100,-100,-100,0);
  29776. } break;
  29777. case 'y' : {
  29778. cimg_forY(*this,i) {
  29779. if ((*this)(0,i)!=(T)values[j]) {
  29780. if (j) --i;
  29781. res.draw_image(0,k,get_rows(i0,i));
  29782. k+=i - i0 + 1; i0 = i + 1; j = 0;
  29783. } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
  29784. }
  29785. if (i0<height()) { res.draw_image(0,k,get_rows(i0,height() - 1)); k+=height() - i0; }
  29786. res.resize(-100,k,-100,-100,0);
  29787. } break;
  29788. case 'z' : {
  29789. cimg_forZ(*this,i) {
  29790. if ((*this)(0,0,i)!=(T)values[j]) {
  29791. if (j) --i;
  29792. res.draw_image(0,0,k,get_slices(i0,i));
  29793. k+=i - i0 + 1; i0 = i + 1; j = 0;
  29794. } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
  29795. }
  29796. if (i0<depth()) { res.draw_image(0,0,k,get_slices(i0,height() - 1)); k+=depth() - i0; }
  29797. res.resize(-100,-100,k,-100,0);
  29798. } break;
  29799. case 'c' : {
  29800. cimg_forC(*this,i) {
  29801. if ((*this)(0,0,0,i)!=(T)values[j]) {
  29802. if (j) --i;
  29803. res.draw_image(0,0,0,k,get_channels(i0,i));
  29804. k+=i - i0 + 1; i0 = i + 1; j = 0;
  29805. } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
  29806. }
  29807. if (i0<spectrum()) { res.draw_image(0,0,k,get_channels(i0,height() - 1)); k+=spectrum() - i0; }
  29808. res.resize(-100,-100,-100,k,0);
  29809. } break;
  29810. default : {
  29811. const ulongT siz = size();
  29812. res.unroll('y');
  29813. if (vsiz==1) { // Optimized version for a single discard value
  29814. const T val = (T)values[0];
  29815. cimg_foroff(*this,i) {
  29816. const T _val = (T)_data[i];
  29817. if (_val!=val) res[k++] = _val;
  29818. }
  29819. } else { // Generic version
  29820. cimg_foroff(*this,i) {
  29821. if ((*this)[i]!=(T)values[j]) {
  29822. if (j) --i;
  29823. std::memcpy(res._data + k,_data + i0,(i - i0 + 1)*sizeof(T));
  29824. k+=i - i0 + 1; i0 = (int)i + 1; j = 0;
  29825. } else { ++j; if (j>=vsiz) { j = 0; i0 = (int)i + 1; }}
  29826. }
  29827. if ((ulongT)i0<siz) { std::memcpy(res._data + k,_data + i0,(siz - i0)*sizeof(T)); k+=siz - i0; }
  29828. }
  29829. res.resize(1,k,1,1,0);
  29830. }
  29831. }
  29832. return res;
  29833. }
  29834. //! Discard neighboring duplicates in the image buffer, along the specified axis.
  29835. CImg<T>& discard(const char axis=0) {
  29836. return get_discard(axis).move_to(*this);
  29837. }
  29838. //! Discard neighboring duplicates in the image buffer, along the specified axis \newinstance.
  29839. CImg<T> get_discard(const char axis=0) const {
  29840. CImg<T> res;
  29841. if (is_empty()) return res;
  29842. const char _axis = cimg::lowercase(axis);
  29843. T current = *_data?(T)0:(T)1;
  29844. int j = 0;
  29845. res.assign(width(),height(),depth(),spectrum());
  29846. switch (_axis) {
  29847. case 'x' : {
  29848. cimg_forX(*this,i)
  29849. if ((*this)(i)!=current) { res.draw_image(j++,get_column(i)); current = (*this)(i); }
  29850. res.resize(j,-100,-100,-100,0);
  29851. } break;
  29852. case 'y' : {
  29853. cimg_forY(*this,i)
  29854. if ((*this)(0,i)!=current) { res.draw_image(0,j++,get_row(i)); current = (*this)(0,i); }
  29855. res.resize(-100,j,-100,-100,0);
  29856. } break;
  29857. case 'z' : {
  29858. cimg_forZ(*this,i)
  29859. if ((*this)(0,0,i)!=current) { res.draw_image(0,0,j++,get_slice(i)); current = (*this)(0,0,i); }
  29860. res.resize(-100,-100,j,-100,0);
  29861. } break;
  29862. case 'c' : {
  29863. cimg_forC(*this,i)
  29864. if ((*this)(0,0,0,i)!=current) { res.draw_image(0,0,0,j++,get_channel(i)); current = (*this)(0,0,0,i); }
  29865. res.resize(-100,-100,-100,j,0);
  29866. } break;
  29867. default : {
  29868. res.unroll('y');
  29869. cimg_foroff(*this,i) {
  29870. const T val = (*this)[i];
  29871. if (val!=current) res[j++] = current = val;
  29872. }
  29873. res.resize(-100,j,-100,-100,0);
  29874. }
  29875. }
  29876. return res;
  29877. }
  29878. //! Invert endianness of all pixel values.
  29879. /**
  29880. **/
  29881. CImg<T>& invert_endianness() {
  29882. cimg::invert_endianness(_data,size());
  29883. return *this;
  29884. }
  29885. //! Invert endianness of all pixel values \newinstance.
  29886. CImg<T> get_invert_endianness() const {
  29887. return (+*this).invert_endianness();
  29888. }
  29889. //! Fill image with random values in specified range.
  29890. /**
  29891. \param val_min Minimal authorized random value.
  29892. \param val_max Maximal authorized random value.
  29893. \note Random variables are uniformly distributed in [val_min,val_max].
  29894. **/
  29895. CImg<T>& rand(const T& val_min, const T& val_max) {
  29896. const float delta = (float)val_max - (float)val_min + (cimg::type<T>::is_float()?0:1);
  29897. if (cimg::type<T>::is_float()) cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),524288)) {
  29898. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  29899. #if cimg_use_openmp!=0
  29900. rng+=omp_get_thread_num();
  29901. #endif
  29902. cimg_pragma_openmp(for)
  29903. cimg_rofoff(*this,off) _data[off] = (T)(val_min + delta*cimg::rand(1,&rng));
  29904. cimg::srand(rng);
  29905. } else cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),524288)) {
  29906. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  29907. #if cimg_use_openmp!=0
  29908. rng+=omp_get_thread_num();
  29909. #endif
  29910. cimg_pragma_openmp(for)
  29911. cimg_rofoff(*this,off) _data[off] = std::min(val_max,(T)(val_min + delta*cimg::rand(1,&rng)));
  29912. cimg::srand(rng);
  29913. }
  29914. return *this;
  29915. }
  29916. //! Fill image with random values in specified range \newinstance.
  29917. CImg<T> get_rand(const T& val_min, const T& val_max) const {
  29918. return (+*this).rand(val_min,val_max);
  29919. }
  29920. //! Round pixel values.
  29921. /**
  29922. \param y Rounding precision.
  29923. \param rounding_type Rounding type. Can be:
  29924. - \c -1: Backward.
  29925. - \c 0: Nearest.
  29926. - \c 1: Forward.
  29927. **/
  29928. CImg<T>& round(const double y=1, const int rounding_type=0) {
  29929. if (y>0) cimg_openmp_for(*this,cimg::round(*ptr,y,rounding_type),8192);
  29930. return *this;
  29931. }
  29932. //! Round pixel values \newinstance.
  29933. CImg<T> get_round(const double y=1, const unsigned int rounding_type=0) const {
  29934. return (+*this).round(y,rounding_type);
  29935. }
  29936. //! Add random noise to pixel values.
  29937. /**
  29938. \param sigma Amplitude of the random additive noise. If \p sigma<0, it stands for a percentage of the
  29939. global value range.
  29940. \param noise_type Type of additive noise (can be \p 0=gaussian, \p 1=uniform, \p 2=Salt and Pepper,
  29941. \p 3=Poisson or \p 4=Rician).
  29942. \return A reference to the modified image instance.
  29943. \note
  29944. - For Poisson noise (\p noise_type=3), parameter \p sigma is ignored, as Poisson noise only depends on
  29945. the image value itself.
  29946. - Function \p CImg<T>::get_noise() is also defined. It returns a non-shared modified copy of the image instance.
  29947. \par Example
  29948. \code
  29949. const CImg<float> img("reference.jpg"), res = img.get_noise(40);
  29950. (img,res.normalize(0,255)).display();
  29951. \endcode
  29952. \image html ref_noise.jpg
  29953. **/
  29954. CImg<T>& noise(const double sigma, const unsigned int noise_type=0) {
  29955. if (is_empty()) return *this;
  29956. const Tfloat vmin = (Tfloat)cimg::type<T>::min(), vmax = (Tfloat)cimg::type<T>::max();
  29957. Tfloat nsigma = (Tfloat)sigma, m = 0, M = 0;
  29958. if (nsigma==0 && noise_type!=3) return *this;
  29959. if (nsigma<0 || noise_type==2) m = (Tfloat)min_max(M);
  29960. if (nsigma<0) nsigma = (Tfloat)(-nsigma*(M-m)/100.);
  29961. switch (noise_type) {
  29962. case 0 : { // Gaussian noise
  29963. cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
  29964. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  29965. #if cimg_use_openmp!=0
  29966. rng+=omp_get_thread_num();
  29967. #endif
  29968. cimg_pragma_openmp(for)
  29969. cimg_rofoff(*this,off) {
  29970. Tfloat val = (Tfloat)(_data[off] + nsigma*cimg::grand(&rng));
  29971. if (val>vmax) val = vmax;
  29972. if (val<vmin) val = vmin;
  29973. _data[off] = (T)val;
  29974. }
  29975. cimg::srand(rng);
  29976. }
  29977. } break;
  29978. case 1 : { // Uniform noise
  29979. cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
  29980. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  29981. #if cimg_use_openmp!=0
  29982. rng+=omp_get_thread_num();
  29983. #endif
  29984. cimg_pragma_openmp(for)
  29985. cimg_rofoff(*this,off) {
  29986. Tfloat val = (Tfloat)(_data[off] + nsigma*cimg::rand(-1,1,&rng));
  29987. if (val>vmax) val = vmax;
  29988. if (val<vmin) val = vmin;
  29989. _data[off] = (T)val;
  29990. }
  29991. cimg::srand(rng);
  29992. }
  29993. } break;
  29994. case 2 : { // Salt & Pepper noise
  29995. if (nsigma<0) nsigma = -nsigma;
  29996. if (M==m) {
  29997. if (cimg::type<T>::is_float()) { --m; ++M; }
  29998. else { m = (Tfloat)cimg::type<T>::min(); M = (Tfloat)cimg::type<T>::max(); }
  29999. }
  30000. cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
  30001. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  30002. #if cimg_use_openmp!=0
  30003. rng+=omp_get_thread_num();
  30004. #endif
  30005. cimg_pragma_openmp(for)
  30006. cimg_rofoff(*this,off) if (cimg::rand(100,&rng)<nsigma) _data[off] = (T)(cimg::rand(1,&rng)<0.5?M:m);
  30007. cimg::srand(rng);
  30008. }
  30009. } break;
  30010. case 3 : { // Poisson Noise
  30011. cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
  30012. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  30013. #if cimg_use_openmp!=0
  30014. rng+=omp_get_thread_num();
  30015. #endif
  30016. cimg_pragma_openmp(for)
  30017. cimg_rofoff(*this,off) _data[off] = (T)cimg::prand(_data[off],&rng);
  30018. cimg::srand(rng);
  30019. }
  30020. } break;
  30021. case 4 : { // Rice noise
  30022. const Tfloat sqrt2 = (Tfloat)std::sqrt(2.);
  30023. cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
  30024. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  30025. #if cimg_use_openmp!=0
  30026. rng+=omp_get_thread_num();
  30027. #endif
  30028. cimg_pragma_openmp(for)
  30029. cimg_rofoff(*this,off) {
  30030. const Tfloat
  30031. val0 = (Tfloat)_data[off]/sqrt2,
  30032. re = (Tfloat)(val0 + nsigma*cimg::grand(&rng)),
  30033. im = (Tfloat)(val0 + nsigma*cimg::grand(&rng));
  30034. Tfloat val = cimg::hypot(re,im);
  30035. if (val>vmax) val = vmax;
  30036. if (val<vmin) val = vmin;
  30037. _data[off] = (T)val;
  30038. }
  30039. cimg::srand(rng);
  30040. }
  30041. } break;
  30042. default :
  30043. throw CImgArgumentException(_cimg_instance
  30044. "noise(): Invalid specified noise type %d "
  30045. "(should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).",
  30046. cimg_instance,
  30047. noise_type);
  30048. }
  30049. return *this;
  30050. }
  30051. //! Add random noise to pixel values \newinstance.
  30052. CImg<T> get_noise(const double sigma, const unsigned int noise_type=0) const {
  30053. return (+*this).noise(sigma,noise_type);
  30054. }
  30055. //! Linearly normalize pixel values.
  30056. /**
  30057. \param min_value Minimum desired value of the resulting image.
  30058. \param max_value Maximum desired value of the resulting image.
  30059. \param constant_case_ratio In case of instance image having a constant value, tell what ratio
  30060. of [min_value,max_value] is used to fill the normalized image
  30061. (=0 for min_value, =1 for max_value, =0.5 for (min_value + max_value)/2).
  30062. \par Example
  30063. \code
  30064. const CImg<float> img("reference.jpg"), res = img.get_normalize(160,220);
  30065. (img,res).display();
  30066. \endcode
  30067. \image html ref_normalize2.jpg
  30068. **/
  30069. CImg<T>& normalize(const T& min_value, const T& max_value,
  30070. const float constant_case_ratio=0) {
  30071. if (is_empty()) return *this;
  30072. const T a = min_value<max_value?min_value:max_value, b = min_value<max_value?max_value:min_value;
  30073. T m, M = max_min(m);
  30074. const Tfloat fm = (Tfloat)m, fM = (Tfloat)M;
  30075. if (m==M)
  30076. return fill(constant_case_ratio==0?a:
  30077. constant_case_ratio==1?b:
  30078. (T)((1 - constant_case_ratio)*a + constant_case_ratio*b));
  30079. if (m!=a || M!=b) cimg_rof(*this,ptrd,T) *ptrd = (T)((*ptrd - fm)/(fM - fm)*(b - a) + a);
  30080. return *this;
  30081. }
  30082. //! Linearly normalize pixel values \newinstance.
  30083. CImg<Tfloat> get_normalize(const T& min_value, const T& max_value,
  30084. const float ratio_if_constant_image=0) const {
  30085. return CImg<Tfloat>(*this,false).normalize((Tfloat)min_value,(Tfloat)max_value,ratio_if_constant_image);
  30086. }
  30087. //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm.
  30088. /**
  30089. \par Example
  30090. \code
  30091. const CImg<float> img("reference.jpg"), res = img.get_normalize();
  30092. (img,res.normalize(0,255)).display();
  30093. \endcode
  30094. \image html ref_normalize.jpg
  30095. **/
  30096. CImg<T>& normalize() {
  30097. const ulongT whd = (ulongT)_width*_height*_depth;
  30098. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30099. _height*_depth>=16))
  30100. cimg_forYZ(*this,y,z) {
  30101. T *ptrd = data(0,y,z,0);
  30102. cimg_forX(*this,x) {
  30103. const T *ptrs = ptrd;
  30104. float n = 0;
  30105. cimg_forC(*this,c) { n+=cimg::sqr((float)*ptrs); ptrs+=whd; }
  30106. n = (float)std::sqrt(n);
  30107. T *_ptrd = ptrd++;
  30108. if (n>0) cimg_forC(*this,c) { *_ptrd = (T)(*_ptrd/n); _ptrd+=whd; }
  30109. else cimg_forC(*this,c) { *_ptrd = (T)0; _ptrd+=whd; }
  30110. }
  30111. }
  30112. return *this;
  30113. }
  30114. //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm \newinstance.
  30115. CImg<Tfloat> get_normalize() const {
  30116. return CImg<Tfloat>(*this,false).normalize();
  30117. }
  30118. //! Compute Lp-norm of each multi-valued pixel of the image instance.
  30119. /**
  30120. \param norm_type Type of computed vector norm (can be \p -1=Linf, or \p greater or equal than 0).
  30121. \par Example
  30122. \code
  30123. const CImg<float> img("reference.jpg"), res = img.get_norm();
  30124. (img,res.normalize(0,255)).display();
  30125. \endcode
  30126. \image html ref_norm.jpg
  30127. **/
  30128. CImg<T>& norm(const int norm_type=2) {
  30129. if (_spectrum==1 && norm_type) return abs();
  30130. return get_norm(norm_type).move_to(*this);
  30131. }
  30132. //! Compute L2-norm of each multi-valued pixel of the image instance \newinstance.
  30133. CImg<Tfloat> get_norm(const int norm_type=2) const {
  30134. if (is_empty()) return *this;
  30135. if (_spectrum==1 && norm_type) return get_abs();
  30136. const ulongT whd = (ulongT)_width*_height*_depth;
  30137. CImg<Tfloat> res(_width,_height,_depth);
  30138. switch (norm_type) {
  30139. case -1 : { // Linf-norm
  30140. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30141. _height*_depth>=16))
  30142. cimg_forYZ(*this,y,z) {
  30143. const ulongT off = (ulongT)offset(0,y,z);
  30144. const T *ptrs = _data + off;
  30145. Tfloat *ptrd = res._data + off;
  30146. cimg_forX(*this,x) {
  30147. Tfloat n = 0;
  30148. const T *_ptrs = ptrs++;
  30149. cimg_forC(*this,c) { const Tfloat val = (Tfloat)cimg::abs(*_ptrs); if (val>n) n = val; _ptrs+=whd; }
  30150. *(ptrd++) = n;
  30151. }
  30152. }
  30153. } break;
  30154. case 0 : { // L0-norm
  30155. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30156. _height*_depth>=16))
  30157. cimg_forYZ(*this,y,z) {
  30158. const ulongT off = (ulongT)offset(0,y,z);
  30159. const T *ptrs = _data + off;
  30160. Tfloat *ptrd = res._data + off;
  30161. cimg_forX(*this,x) {
  30162. unsigned int n = 0;
  30163. const T *_ptrs = ptrs++;
  30164. cimg_forC(*this,c) { n+=*_ptrs==0?0:1; _ptrs+=whd; }
  30165. *(ptrd++) = (Tfloat)n;
  30166. }
  30167. }
  30168. } break;
  30169. case 1 : { // L1-norm
  30170. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30171. _height*_depth>=16))
  30172. cimg_forYZ(*this,y,z) {
  30173. const ulongT off = (ulongT)offset(0,y,z);
  30174. const T *ptrs = _data + off;
  30175. Tfloat *ptrd = res._data + off;
  30176. cimg_forX(*this,x) {
  30177. Tfloat n = 0;
  30178. const T *_ptrs = ptrs++;
  30179. cimg_forC(*this,c) { n+=cimg::abs(*_ptrs); _ptrs+=whd; }
  30180. *(ptrd++) = n;
  30181. }
  30182. }
  30183. } break;
  30184. case 2 : { // L2-norm
  30185. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30186. _height*_depth>=16))
  30187. cimg_forYZ(*this,y,z) {
  30188. const ulongT off = (ulongT)offset(0,y,z);
  30189. const T *ptrs = _data + off;
  30190. Tfloat *ptrd = res._data + off;
  30191. cimg_forX(*this,x) {
  30192. Tfloat n = 0;
  30193. const T *_ptrs = ptrs++;
  30194. cimg_forC(*this,c) { n+=cimg::sqr((Tfloat)*_ptrs); _ptrs+=whd; }
  30195. *(ptrd++) = (Tfloat)std::sqrt((Tfloat)n);
  30196. }
  30197. }
  30198. } break;
  30199. default : { // Linf-norm
  30200. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  30201. _height*_depth>=16))
  30202. cimg_forYZ(*this,y,z) {
  30203. const ulongT off = (ulongT)offset(0,y,z);
  30204. const T *ptrs = _data + off;
  30205. Tfloat *ptrd = res._data + off;
  30206. cimg_forX(*this,x) {
  30207. Tfloat n = 0;
  30208. const T *_ptrs = ptrs++;
  30209. cimg_forC(*this,c) { n+=std::pow(cimg::abs((Tfloat)*_ptrs),(Tfloat)norm_type); _ptrs+=whd; }
  30210. *(ptrd++) = (Tfloat)std::pow((Tfloat)n,1/(Tfloat)norm_type);
  30211. }
  30212. }
  30213. }
  30214. }
  30215. return res;
  30216. }
  30217. //! Cut pixel values in specified range.
  30218. /**
  30219. \param min_value Minimum desired value of the resulting image.
  30220. \param max_value Maximum desired value of the resulting image.
  30221. \par Example
  30222. \code
  30223. const CImg<float> img("reference.jpg"), res = img.get_cut(160,220);
  30224. (img,res).display();
  30225. \endcode
  30226. \image html ref_cut.jpg
  30227. **/
  30228. CImg<T>& cut(const T& min_value, const T& max_value) {
  30229. if (is_empty()) return *this;
  30230. const T a = min_value<max_value?min_value:max_value, b = min_value<max_value?max_value:min_value;
  30231. cimg_openmp_for(*this,cimg::cut(*ptr,a,b),32768);
  30232. return *this;
  30233. }
  30234. //! Cut pixel values in specified range \newinstance.
  30235. CImg<T> get_cut(const T& min_value, const T& max_value) const {
  30236. return (+*this).cut(min_value,max_value);
  30237. }
  30238. //! Uniformly quantize pixel values.
  30239. /**
  30240. \param nb_levels Number of quantization levels.
  30241. \param keep_range Tells if resulting values keep the same range as the original ones.
  30242. \par Example
  30243. \code
  30244. const CImg<float> img("reference.jpg"), res = img.get_quantize(4);
  30245. (img,res).display();
  30246. \endcode
  30247. \image html ref_quantize.jpg
  30248. **/
  30249. CImg<T>& quantize(const unsigned int nb_levels, const bool keep_range=true) {
  30250. if (!nb_levels)
  30251. throw CImgArgumentException(_cimg_instance
  30252. "quantize(): Invalid quantization request with 0 values.",
  30253. cimg_instance);
  30254. if (is_empty()) return *this;
  30255. Tfloat m, M = (Tfloat)max_min(m), range = M - m;
  30256. if (range>0) {
  30257. if (keep_range)
  30258. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
  30259. cimg_rofoff(*this,off) {
  30260. const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);
  30261. _data[off] = (T)(m + std::min(val,nb_levels - 1)*range/nb_levels);
  30262. } else
  30263. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
  30264. cimg_rofoff(*this,off) {
  30265. const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);
  30266. _data[off] = (T)std::min(val,nb_levels - 1);
  30267. }
  30268. }
  30269. return *this;
  30270. }
  30271. //! Uniformly quantize pixel values \newinstance.
  30272. CImg<T> get_quantize(const unsigned int n, const bool keep_range=true) const {
  30273. return (+*this).quantize(n,keep_range);
  30274. }
  30275. //! Threshold pixel values.
  30276. /**
  30277. \param value Threshold value
  30278. \param soft_threshold Tells if soft thresholding must be applied (instead of hard one).
  30279. \param strict_threshold Tells if threshold value is strict.
  30280. \par Example
  30281. \code
  30282. const CImg<float> img("reference.jpg"), res = img.get_threshold(128);
  30283. (img,res.normalize(0,255)).display();
  30284. \endcode
  30285. \image html ref_threshold.jpg
  30286. **/
  30287. CImg<T>& threshold(const T& value, const bool soft_threshold=false, const bool strict_threshold=false) {
  30288. if (is_empty()) return *this;
  30289. if (strict_threshold) {
  30290. if (soft_threshold)
  30291. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
  30292. cimg_rofoff(*this,off) {
  30293. const T v = _data[off];
  30294. _data[off] = v>value?(T)(v-value):v<-(float)value?(T)(v + value):(T)0;
  30295. }
  30296. else
  30297. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),65536))
  30298. cimg_rofoff(*this,off) _data[off] = _data[off]>value?(T)1:(T)0;
  30299. } else {
  30300. if (soft_threshold)
  30301. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
  30302. cimg_rofoff(*this,off) {
  30303. const T v = _data[off];
  30304. _data[off] = v>=value?(T)(v-value):v<=-(float)value?(T)(v + value):(T)0;
  30305. }
  30306. else
  30307. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),65536))
  30308. cimg_rofoff(*this,off) _data[off] = _data[off]>=value?(T)1:(T)0;
  30309. }
  30310. return *this;
  30311. }
  30312. //! Threshold pixel values \newinstance.
  30313. CImg<T> get_threshold(const T& value, const bool soft_threshold=false, const bool strict_threshold=false) const {
  30314. return (+*this).threshold(value,soft_threshold,strict_threshold);
  30315. }
  30316. //! Compute the histogram of pixel values.
  30317. /**
  30318. \param nb_levels Number of desired histogram levels.
  30319. \param min_value Minimum pixel value considered for the histogram computation.
  30320. All pixel values lower than \p min_value will not be counted.
  30321. \param max_value Maximum pixel value considered for the histogram computation.
  30322. All pixel values higher than \p max_value will not be counted.
  30323. \note
  30324. - The histogram H of an image I is the 1D function where H(x) counts the number of occurrences of the value x
  30325. in the image I.
  30326. - The resulting histogram is always defined in 1D. Histograms of multi-valued images are not multi-dimensional.
  30327. \par Example
  30328. \code
  30329. const CImg<float> img = CImg<float>("reference.jpg").histogram(256);
  30330. img.display_graph(0,3);
  30331. \endcode
  30332. \image html ref_histogram.jpg
  30333. **/
  30334. CImg<T>& histogram(const unsigned int nb_levels, const T& min_value, const T& max_value) {
  30335. return get_histogram(nb_levels,min_value,max_value).move_to(*this);
  30336. }
  30337. //! Compute the histogram of pixel values \overloading.
  30338. CImg<T>& histogram(const unsigned int nb_levels) {
  30339. return get_histogram(nb_levels).move_to(*this);
  30340. }
  30341. //! Compute the histogram of pixel values \newinstance.
  30342. CImg<ulongT> get_histogram(const unsigned int nb_levels, const T& min_value, const T& max_value) const {
  30343. if (!nb_levels || is_empty()) return CImg<ulongT>();
  30344. const double
  30345. vmin = (double)(min_value<max_value?min_value:max_value),
  30346. vmax = (double)(min_value<max_value?max_value:min_value);
  30347. CImg<ulongT> res(nb_levels,1,1,1,0);
  30348. cimg_rof(*this,ptrs,T) {
  30349. const T val = *ptrs;
  30350. if (val>=vmin && val<=vmax) ++res[val==vmax?nb_levels - 1:(unsigned int)((val - vmin)*nb_levels/(vmax - vmin))];
  30351. }
  30352. return res;
  30353. }
  30354. //! Compute the histogram of pixel values \newinstance.
  30355. CImg<ulongT> get_histogram(const unsigned int nb_levels) const {
  30356. if (!nb_levels || is_empty()) return CImg<ulongT>();
  30357. T vmax = 0, vmin = min_max(vmax);
  30358. return get_histogram(nb_levels,vmin,vmax);
  30359. }
  30360. //! Equalize histogram of pixel values.
  30361. /**
  30362. \param nb_levels Number of histogram levels used for the equalization.
  30363. \param min_value Minimum pixel value considered for the histogram computation.
  30364. All pixel values lower than \p min_value will not be counted.
  30365. \param max_value Maximum pixel value considered for the histogram computation.
  30366. All pixel values higher than \p max_value will not be counted.
  30367. \par Example
  30368. \code
  30369. const CImg<float> img("reference.jpg"), res = img.get_equalize(256);
  30370. (img,res).display();
  30371. \endcode
  30372. \image html ref_equalize.jpg
  30373. **/
  30374. CImg<T>& equalize(const unsigned int nb_levels, const T& min_value, const T& max_value) {
  30375. if (!nb_levels || is_empty()) return *this;
  30376. const T
  30377. vmin = min_value<max_value?min_value:max_value,
  30378. vmax = min_value<max_value?max_value:min_value;
  30379. CImg<ulongT> hist = get_histogram(nb_levels,vmin,vmax);
  30380. ulongT cumul = 0;
  30381. cimg_forX(hist,pos) { cumul+=hist[pos]; hist[pos] = cumul; }
  30382. if (!cumul) cumul = 1;
  30383. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),1048576))
  30384. cimg_rofoff(*this,off) {
  30385. const int pos = (int)((_data[off] - vmin)*(nb_levels - 1.)/(vmax - vmin));
  30386. if (pos>=0 && pos<(int)nb_levels) _data[off] = (T)(vmin + (vmax - vmin)*hist[pos]/cumul);
  30387. }
  30388. return *this;
  30389. }
  30390. //! Equalize histogram of pixel values \overloading.
  30391. CImg<T>& equalize(const unsigned int nb_levels) {
  30392. if (!nb_levels || is_empty()) return *this;
  30393. T vmax = 0, vmin = min_max(vmax);
  30394. return equalize(nb_levels,vmin,vmax);
  30395. }
  30396. //! Equalize histogram of pixel values \newinstance.
  30397. CImg<T> get_equalize(const unsigned int nblevels, const T& val_min, const T& val_max) const {
  30398. return (+*this).equalize(nblevels,val_min,val_max);
  30399. }
  30400. //! Equalize histogram of pixel values \newinstance.
  30401. CImg<T> get_equalize(const unsigned int nblevels) const {
  30402. return (+*this).equalize(nblevels);
  30403. }
  30404. //! Index multi-valued pixels regarding to a specified colormap.
  30405. /**
  30406. \param colormap Multi-valued colormap used as the basis for multi-valued pixel indexing.
  30407. \param dithering Level of dithering (0=disable, 1=standard level).
  30408. \param map_indexes Tell if the values of the resulting image are the colormap indices or the colormap vectors.
  30409. \note
  30410. - \p img.index(colormap,dithering,1) is equivalent to <tt>img.index(colormap,dithering,0).map(colormap)</tt>.
  30411. \par Example
  30412. \code
  30413. const CImg<float> img("reference.jpg"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255);
  30414. const CImg<float> res = img.get_index(colormap,1,true);
  30415. (img,res).display();
  30416. \endcode
  30417. \image html ref_index.jpg
  30418. **/
  30419. template<typename t>
  30420. CImg<T>& index(const CImg<t>& colormap, const float dithering=1, const bool map_indexes=false) {
  30421. return get_index(colormap,dithering,map_indexes).move_to(*this);
  30422. }
  30423. //! Index multi-valued pixels regarding to a specified colormap \newinstance.
  30424. template<typename t>
  30425. CImg<typename CImg<t>::Tuint>
  30426. get_index(const CImg<t>& colormap, const float dithering=1, const bool map_indexes=true) const {
  30427. if (colormap._spectrum!=_spectrum)
  30428. throw CImgArgumentException(_cimg_instance
  30429. "index(): Instance and specified colormap (%u,%u,%u,%u,%p) "
  30430. "have incompatible dimensions.",
  30431. cimg_instance,
  30432. colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);
  30433. typedef typename CImg<t>::Tuint tuint;
  30434. if (is_empty()) return CImg<tuint>();
  30435. const ulongT
  30436. whd = (ulongT)_width*_height*_depth,
  30437. pwhd = (ulongT)colormap._width*colormap._height*colormap._depth;
  30438. CImg<tuint> res(_width,_height,_depth,map_indexes?_spectrum:1);
  30439. if (dithering>0) { // Dithered versions
  30440. tuint *ptrd = res._data;
  30441. const float ndithering = cimg::cut(dithering,0,1)/16;
  30442. Tfloat valm = 0, valM = (Tfloat)max_min(valm);
  30443. if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; }
  30444. CImg<Tfloat> cache = get_crop(-1,0,0,0,_width,1,0,_spectrum - 1);
  30445. Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0);
  30446. const ulongT cwhd = (ulongT)cache._width*cache._height*cache._depth;
  30447. switch (_spectrum) {
  30448. case 1 : { // Optimized for scalars
  30449. cimg_forYZ(*this,y,z) {
  30450. if (y<height() - 2) {
  30451. Tfloat *ptrc0 = cache_next; const T *ptrs0 = data(0,y + 1,z,0);
  30452. cimg_forX(*this,x) *(ptrc0++) = (Tfloat)*(ptrs0++);
  30453. }
  30454. Tfloat *ptrs0 = cache_current, *ptrsn0 = cache_next;
  30455. cimg_forX(*this,x) {
  30456. const Tfloat _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0;
  30457. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30458. for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0<ptrp_end; ) {
  30459. const Tfloat pval0 = (Tfloat)*(ptrp0++) - val0, dist = pval0*pval0;
  30460. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30461. }
  30462. const Tfloat err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering;
  30463. *ptrs0+=7*err0; *(ptrsn0 - 1)+=3*err0; *(ptrsn0++)+=5*err0; *ptrsn0+=err0;
  30464. if (map_indexes) *(ptrd++) = (tuint)*ptrmin0; else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30465. }
  30466. cimg::swap(cache_current,cache_next);
  30467. }
  30468. } break;
  30469. case 2 : { // Optimized for 2D vectors
  30470. tuint *ptrd1 = ptrd + whd;
  30471. cimg_forYZ(*this,y,z) {
  30472. if (y<height() - 2) {
  30473. Tfloat *ptrc0 = cache_next, *ptrc1 = ptrc0 + cwhd;
  30474. const T *ptrs0 = data(0,y + 1,z,0), *ptrs1 = ptrs0 + whd;
  30475. cimg_forX(*this,x) { *(ptrc0++) = (Tfloat)*(ptrs0++); *(ptrc1++) = (Tfloat)*(ptrs1++); }
  30476. }
  30477. Tfloat
  30478. *ptrs0 = cache_current, *ptrs1 = ptrs0 + cwhd,
  30479. *ptrsn0 = cache_next, *ptrsn1 = ptrsn0 + cwhd;
  30480. cimg_forX(*this,x) {
  30481. const Tfloat
  30482. _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0,
  30483. _val1 = (Tfloat)*ptrs1, val1 = _val1<valm?valm:_val1>valM?valM:_val1;
  30484. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30485. for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
  30486. const Tfloat
  30487. pval0 = (Tfloat)*(ptrp0++) - val0, pval1 = (Tfloat)*(ptrp1++) - val1,
  30488. dist = pval0*pval0 + pval1*pval1;
  30489. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30490. }
  30491. const t *const ptrmin1 = ptrmin0 + pwhd;
  30492. const Tfloat
  30493. err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering,
  30494. err1 = ((*(ptrs1++)=val1) - (Tfloat)*ptrmin1)*ndithering;
  30495. *ptrs0+=7*err0; *ptrs1+=7*err1;
  30496. *(ptrsn0 - 1)+=3*err0; *(ptrsn1 - 1)+=3*err1;
  30497. *(ptrsn0++)+=5*err0; *(ptrsn1++)+=5*err1;
  30498. *ptrsn0+=err0; *ptrsn1+=err1;
  30499. if (map_indexes) { *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*ptrmin1; }
  30500. else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30501. }
  30502. cimg::swap(cache_current,cache_next);
  30503. }
  30504. } break;
  30505. case 3 : { // Optimized for 3D vectors (colors)
  30506. tuint *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd;
  30507. cimg_forYZ(*this,y,z) {
  30508. if (y<height() - 2) {
  30509. Tfloat *ptrc0 = cache_next, *ptrc1 = ptrc0 + cwhd, *ptrc2 = ptrc1 + cwhd;
  30510. const T *ptrs0 = data(0,y + 1,z,0), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd;
  30511. cimg_forX(*this,x) {
  30512. *(ptrc0++) = (Tfloat)*(ptrs0++); *(ptrc1++) = (Tfloat)*(ptrs1++); *(ptrc2++) = (Tfloat)*(ptrs2++);
  30513. }
  30514. }
  30515. Tfloat
  30516. *ptrs0 = cache_current, *ptrs1 = ptrs0 + cwhd, *ptrs2 = ptrs1 + cwhd,
  30517. *ptrsn0 = cache_next, *ptrsn1 = ptrsn0 + cwhd, *ptrsn2 = ptrsn1 + cwhd;
  30518. cimg_forX(*this,x) {
  30519. const Tfloat
  30520. _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0,
  30521. _val1 = (Tfloat)*ptrs1, val1 = _val1<valm?valm:_val1>valM?valM:_val1,
  30522. _val2 = (Tfloat)*ptrs2, val2 = _val2<valm?valm:_val2>valM?valM:_val2;
  30523. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30524. for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd,
  30525. *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
  30526. const Tfloat
  30527. pval0 = (Tfloat)*(ptrp0++) - val0,
  30528. pval1 = (Tfloat)*(ptrp1++) - val1,
  30529. pval2 = (Tfloat)*(ptrp2++) - val2,
  30530. dist = pval0*pval0 + pval1*pval1 + pval2*pval2;
  30531. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30532. }
  30533. const t *const ptrmin1 = ptrmin0 + pwhd, *const ptrmin2 = ptrmin1 + pwhd;
  30534. const Tfloat
  30535. err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering,
  30536. err1 = ((*(ptrs1++)=val1) - (Tfloat)*ptrmin1)*ndithering,
  30537. err2 = ((*(ptrs2++)=val2) - (Tfloat)*ptrmin2)*ndithering;
  30538. *ptrs0+=7*err0; *ptrs1+=7*err1; *ptrs2+=7*err2;
  30539. *(ptrsn0 - 1)+=3*err0; *(ptrsn1 - 1)+=3*err1; *(ptrsn2 - 1)+=3*err2;
  30540. *(ptrsn0++)+=5*err0; *(ptrsn1++)+=5*err1; *(ptrsn2++)+=5*err2;
  30541. *ptrsn0+=err0; *ptrsn1+=err1; *ptrsn2+=err2;
  30542. if (map_indexes) {
  30543. *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*ptrmin1; *(ptrd2++) = (tuint)*ptrmin2;
  30544. } else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30545. }
  30546. cimg::swap(cache_current,cache_next);
  30547. }
  30548. } break;
  30549. default : // Generic version
  30550. cimg_forYZ(*this,y,z) {
  30551. if (y<height() - 2) {
  30552. Tfloat *ptrc = cache_next;
  30553. cimg_forC(*this,c) {
  30554. Tfloat *_ptrc = ptrc; const T *_ptrs = data(0,y + 1,z,c);
  30555. cimg_forX(*this,x) *(_ptrc++) = (Tfloat)*(_ptrs++);
  30556. ptrc+=cwhd;
  30557. }
  30558. }
  30559. Tfloat *ptrs = cache_current, *ptrsn = cache_next;
  30560. cimg_forX(*this,x) {
  30561. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin = colormap._data;
  30562. for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp<ptrp_end; ++ptrp) {
  30563. Tfloat dist = 0; Tfloat *_ptrs = ptrs; const t *_ptrp = ptrp;
  30564. cimg_forC(*this,c) {
  30565. const Tfloat _val = *_ptrs, val = _val<valm?valm:_val>valM?valM:_val;
  30566. dist+=cimg::sqr((*_ptrs=val) - (Tfloat)*_ptrp); _ptrs+=cwhd; _ptrp+=pwhd;
  30567. }
  30568. if (dist<distmin) { ptrmin = ptrp; distmin = dist; }
  30569. }
  30570. const t *_ptrmin = ptrmin; Tfloat *_ptrs = ptrs++, *_ptrsn = (ptrsn++) - 1;
  30571. cimg_forC(*this,c) {
  30572. const Tfloat err = (*(_ptrs++) - (Tfloat)*_ptrmin)*ndithering;
  30573. *_ptrs+=7*err; *(_ptrsn++)+=3*err; *(_ptrsn++)+=5*err; *_ptrsn+=err;
  30574. _ptrmin+=pwhd; _ptrs+=cwhd - 1; _ptrsn+=cwhd - 2;
  30575. }
  30576. if (map_indexes) {
  30577. tuint *_ptrd = ptrd++;
  30578. cimg_forC(*this,c) { *_ptrd = (tuint)*ptrmin; _ptrd+=whd; ptrmin+=pwhd; }
  30579. }
  30580. else *(ptrd++) = (tuint)(ptrmin - colormap._data);
  30581. }
  30582. cimg::swap(cache_current,cache_next);
  30583. }
  30584. }
  30585. } else { // Non-dithered versions
  30586. switch (_spectrum) {
  30587. case 1 : { // Optimized for scalars
  30588. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
  30589. _height*_depth>=16 && pwhd>=16))
  30590. cimg_forYZ(*this,y,z) {
  30591. tuint *ptrd = res.data(0,y,z);
  30592. for (const T *ptrs0 = data(0,y,z), *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
  30593. const Tfloat val0 = (Tfloat)*(ptrs0++);
  30594. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30595. for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0<ptrp_end; ) {
  30596. const Tfloat pval0 = (Tfloat)*(ptrp0++) - val0, dist = pval0*pval0;
  30597. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30598. }
  30599. if (map_indexes) *(ptrd++) = (tuint)*ptrmin0; else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30600. }
  30601. }
  30602. } break;
  30603. case 2 : { // Optimized for 2D vectors
  30604. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
  30605. _height*_depth>=16 && pwhd>=16))
  30606. cimg_forYZ(*this,y,z) {
  30607. tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd;
  30608. for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
  30609. const Tfloat val0 = (Tfloat)*(ptrs0++), val1 = (Tfloat)*(ptrs1++);
  30610. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30611. for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
  30612. const Tfloat
  30613. pval0 = (Tfloat)*(ptrp0++) - val0, pval1 = (Tfloat)*(ptrp1++) - val1,
  30614. dist = pval0*pval0 + pval1*pval1;
  30615. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30616. }
  30617. if (map_indexes) { *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*(ptrmin0 + pwhd); }
  30618. else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30619. }
  30620. }
  30621. } break;
  30622. case 3 : { // Optimized for 3D vectors (colors)
  30623. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
  30624. _height*_depth>=16 && pwhd>=16))
  30625. cimg_forYZ(*this,y,z) {
  30626. tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd;
  30627. for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd,
  30628. *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
  30629. const Tfloat val0 = (Tfloat)*(ptrs0++), val1 = (Tfloat)*(ptrs1++), val2 = (Tfloat)*(ptrs2++);
  30630. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
  30631. for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd,
  30632. *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
  30633. const Tfloat
  30634. pval0 = (Tfloat)*(ptrp0++) - val0,
  30635. pval1 = (Tfloat)*(ptrp1++) - val1,
  30636. pval2 = (Tfloat)*(ptrp2++) - val2,
  30637. dist = pval0*pval0 + pval1*pval1 + pval2*pval2;
  30638. if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
  30639. }
  30640. if (map_indexes) {
  30641. *(ptrd++) = (tuint)*ptrmin0;
  30642. *(ptrd1++) = (tuint)*(ptrmin0 + pwhd);
  30643. *(ptrd2++) = (tuint)*(ptrmin0 + 2*pwhd);
  30644. } else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
  30645. }
  30646. }
  30647. } break;
  30648. default : // Generic version
  30649. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
  30650. _height*_depth>=16 && pwhd>=16))
  30651. cimg_forYZ(*this,y,z) {
  30652. tuint *ptrd = res.data(0,y,z);
  30653. for (const T *ptrs = data(0,y,z), *ptrs_end = ptrs + _width; ptrs<ptrs_end; ++ptrs) {
  30654. Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin = colormap._data;
  30655. for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp<ptrp_end; ++ptrp) {
  30656. Tfloat dist = 0; const T *_ptrs = ptrs; const t *_ptrp = ptrp;
  30657. cimg_forC(*this,c) { dist+=cimg::sqr((Tfloat)*_ptrs - (Tfloat)*_ptrp); _ptrs+=whd; _ptrp+=pwhd; }
  30658. if (dist<distmin) { ptrmin = ptrp; distmin = dist; }
  30659. }
  30660. if (map_indexes) {
  30661. tuint *_ptrd = ptrd++;
  30662. cimg_forC(*this,c) { *_ptrd = (tuint)*ptrmin; _ptrd+=whd; ptrmin+=pwhd; }
  30663. }
  30664. else *(ptrd++) = (tuint)(ptrmin - colormap._data);
  30665. }
  30666. }
  30667. }
  30668. }
  30669. return res;
  30670. }
  30671. //! Map predefined colormap on the scalar (indexed) image instance.
  30672. /**
  30673. \param colormap Multi-valued colormap used for mapping the indexes.
  30674. \param boundary_conditions Boundary conditions.
  30675. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  30676. \par Example
  30677. \code
  30678. const CImg<float> img("reference.jpg"),
  30679. colormap1(3,1,1,3, 0,128,255, 0,128,255, 0,128,255),
  30680. colormap2(3,1,1,3, 255,0,0, 0,255,0, 0,0,255),
  30681. res = img.get_index(colormap1,0).map(colormap2);
  30682. (img,res).display();
  30683. \endcode
  30684. \image html ref_map.jpg
  30685. **/
  30686. template<typename t>
  30687. CImg<T>& map(const CImg<t>& colormap, const unsigned int boundary_conditions=0) {
  30688. return get_map(colormap,boundary_conditions).move_to(*this);
  30689. }
  30690. //! Map predefined colormap on the scalar (indexed) image instance \newinstance.
  30691. template<typename t>
  30692. CImg<t> get_map(const CImg<t>& colormap, const unsigned int boundary_conditions=0) const {
  30693. const ulongT
  30694. whd = (ulongT)_width*_height*_depth, siz = size(),
  30695. cwhd = (ulongT)colormap._width*colormap._height*colormap._depth,
  30696. cwhd2 = 2*cwhd;
  30697. CImg<t> res(_width,_height,_depth,_spectrum*colormap._spectrum);
  30698. switch (colormap._spectrum) {
  30699. case 1 : { // Optimized for scalars
  30700. switch (boundary_conditions) {
  30701. case 3 : // Mirror
  30702. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30703. for (longT off = 0; off<(longT)siz; ++off) {
  30704. const ulongT ind = ((ulongT)_data[off])%cwhd2;
  30705. res[off] = colormap[ind<cwhd?ind:cwhd2 - ind - 1];
  30706. }
  30707. break;
  30708. case 2 : // Periodic
  30709. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30710. for (longT off = 0; off<(longT)siz; ++off) {
  30711. const ulongT ind = (ulongT)_data[off];
  30712. res[off] = colormap[ind%cwhd];
  30713. }
  30714. break;
  30715. case 1 : // Neumann
  30716. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30717. for (longT off = 0; off<(longT)siz; ++off) {
  30718. const longT ind = (longT)_data[off];
  30719. res[off] = colormap[cimg::cut(ind,(longT)0,(longT)cwhd - 1)];
  30720. } break;
  30721. default : // Dirichlet
  30722. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30723. for (longT off = 0; off<(longT)siz; ++off) {
  30724. const ulongT ind = (ulongT)_data[off];
  30725. res[off] = ind<cwhd?colormap[ind]:(t)0;
  30726. }
  30727. }
  30728. } break;
  30729. case 2 : { // Optimized for 2D vectors
  30730. const t *const ptrp0 = colormap._data, *const ptrp1 = ptrp0 + cwhd;
  30731. switch (boundary_conditions) {
  30732. case 3 : // Mirror
  30733. cimg_forC(*this,c) {
  30734. t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
  30735. const T *const ptrs = data(0,0,0,c);
  30736. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30737. for (longT off = 0; off<(longT)whd; ++off) {
  30738. const ulongT
  30739. _ind = ((ulongT)ptrs[off])%cwhd2,
  30740. ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
  30741. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
  30742. }
  30743. } break;
  30744. case 2 : // Periodic
  30745. cimg_forC(*this,c) {
  30746. t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
  30747. const T *const ptrs = data(0,0,0,c);
  30748. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30749. for (longT off = 0; off<(longT)whd; ++off) {
  30750. const ulongT ind = ((ulongT)ptrs[off])%cwhd;
  30751. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
  30752. }
  30753. } break;
  30754. case 1 : // Neumann
  30755. cimg_forC(*this,c) {
  30756. t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
  30757. const T *const ptrs = data(0,0,0,c);
  30758. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30759. for (longT off = 0; off<(longT)whd; ++off) {
  30760. const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
  30761. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
  30762. }
  30763. } break;
  30764. default : // Dirichlet
  30765. cimg_forC(*this,c) {
  30766. t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
  30767. const T *const ptrs = data(0,0,0,c);
  30768. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30769. for (longT off = 0; off<(longT)whd; ++off) {
  30770. const ulongT ind = (ulongT)ptrs[off];
  30771. if (ind<cwhd) { ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; }
  30772. else ptrd0[off] = ptrd1[off] = (t)0;
  30773. }
  30774. }
  30775. }
  30776. } break;
  30777. case 3 : { // Optimized for 3D vectors (colors)
  30778. const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + cwhd, *ptrp2 = ptrp0 + 2*cwhd;
  30779. switch (boundary_conditions) {
  30780. case 3 : // Mirror
  30781. cimg_forC(*this,c) {
  30782. t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
  30783. const T *const ptrs = data(0,0,0,c);
  30784. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30785. for (longT off = 0; off<(longT)whd; ++off) {
  30786. const ulongT
  30787. _ind = ((ulongT)ptrs[off])%cwhd2,
  30788. ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
  30789. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
  30790. }
  30791. } break;
  30792. case 2 : // Periodic
  30793. cimg_forC(*this,c) {
  30794. t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
  30795. const T *const ptrs = data(0,0,0,c);
  30796. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30797. for (longT off = 0; off<(longT)whd; ++off) {
  30798. const ulongT ind = ((ulongT)ptrs[off])%cwhd;
  30799. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
  30800. }
  30801. } break;
  30802. case 1 : // Neumann
  30803. cimg_forC(*this,c) {
  30804. t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
  30805. const T *const ptrs = data(0,0,0,c);
  30806. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30807. for (longT off = 0; off<(longT)whd; ++off) {
  30808. const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
  30809. ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
  30810. }
  30811. } break;
  30812. default : // Dirichlet
  30813. cimg_forC(*this,c) {
  30814. t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
  30815. const T *const ptrs = data(0,0,0,c);
  30816. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30817. for (longT off = 0; off<(longT)whd; ++off) {
  30818. const ulongT ind = (ulongT)ptrs[off];
  30819. if (ind<cwhd) { ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind]; }
  30820. else ptrd0[off] = ptrd1[off] = ptrd2[off] = (t)0;
  30821. }
  30822. }
  30823. }
  30824. } break;
  30825. default : { // Generic version
  30826. switch (boundary_conditions) {
  30827. case 3 : // Mirror
  30828. cimg_forC(*this,c) {
  30829. t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
  30830. const T *const ptrs = data(0,0,0,c);
  30831. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30832. for (longT off = 0; off<(longT)whd; ++off) {
  30833. const ulongT
  30834. _ind = ((ulongT)ptrs[off])%cwhd,
  30835. ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
  30836. t *const _ptrd = ptrd + off;
  30837. const t *const ptrp = &colormap[ind];
  30838. cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
  30839. }
  30840. } break;
  30841. case 2 : // Periodic
  30842. cimg_forC(*this,c) {
  30843. t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
  30844. const T *const ptrs = data(0,0,0,c);
  30845. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30846. for (longT off = 0; off<(longT)whd; ++off) {
  30847. const ulongT ind = ((ulongT)ptrs[off])%cwhd;
  30848. t *const _ptrd = ptrd + off;
  30849. const t *const ptrp = &colormap[ind];
  30850. cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
  30851. }
  30852. } break;
  30853. case 1 : // Neumann
  30854. cimg_forC(*this,c) {
  30855. t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
  30856. const T *const ptrs = data(0,0,0,c);
  30857. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30858. for (longT off = 0; off<(longT)whd; ++off) {
  30859. const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
  30860. t *const _ptrd = ptrd + off;
  30861. const t *const ptrp = &colormap[ind];
  30862. cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
  30863. }
  30864. } break;
  30865. default : // Dirichlet
  30866. cimg_forC(*this,c) {
  30867. t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
  30868. const T *const ptrs = data(0,0,0,c);
  30869. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
  30870. for (longT off = 0; off<(longT)whd; ++off) {
  30871. const ulongT ind = (ulongT)ptrs[off];
  30872. t *const _ptrd = ptrd + off;
  30873. if (ind<cwhd) {
  30874. const t *const ptrp = &colormap[ind];
  30875. cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
  30876. } else cimg_forC(colormap,k) _ptrd[k*whd] = (t)0;
  30877. }
  30878. }
  30879. }
  30880. }
  30881. }
  30882. return res;
  30883. }
  30884. //! Label connected components.
  30885. /**
  30886. \param is_high_connectivity Boolean that choose between 4(false)- or 8(true)-connectivity
  30887. in 2D case, and between 6(false)- or 26(true)-connectivity in 3D case.
  30888. \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region.
  30889. \param is_L2_norm If true, tolerance is compared against L2 difference, otherwise L1 is used.
  30890. \note The algorithm of connected components computation has been primarily done
  30891. by A. Meijster, according to the publication:
  30892. 'W.H. Hesselink, A. Meijster, C. Bron, "Concurrent Determination of Connected Components.",
  30893. In: Science of Computer Programming 41 (2001), pp. 173--194'.
  30894. The submitted code has then been modified to fit CImg coding style and constraints.
  30895. **/
  30896. CImg<T>& label(const bool is_high_connectivity=false, const Tfloat tolerance=0,
  30897. const bool is_L2_norm=true) {
  30898. if (is_empty()) return *this;
  30899. return get_label(is_high_connectivity,tolerance,is_L2_norm).move_to(*this);
  30900. }
  30901. //! Label connected components \newinstance.
  30902. CImg<ulongT> get_label(const bool is_high_connectivity=false, const Tfloat tolerance=0,
  30903. const bool is_L2_norm=true) const {
  30904. if (is_empty()) return CImg<ulongT>();
  30905. // Create neighborhood tables.
  30906. int dx[13], dy[13], dz[13], nb = 0;
  30907. dx[nb] = 1; dy[nb] = 0; dz[nb++] = 0;
  30908. dx[nb] = 0; dy[nb] = 1; dz[nb++] = 0;
  30909. if (is_high_connectivity) {
  30910. dx[nb] = 1; dy[nb] = 1; dz[nb++] = 0;
  30911. dx[nb] = 1; dy[nb] = -1; dz[nb++] = 0;
  30912. }
  30913. if (_depth>1) { // 3D version
  30914. dx[nb] = 0; dy[nb] = 0; dz[nb++]=1;
  30915. if (is_high_connectivity) {
  30916. dx[nb] = 1; dy[nb] = 1; dz[nb++] = -1;
  30917. dx[nb] = 1; dy[nb] = 0; dz[nb++] = -1;
  30918. dx[nb] = 1; dy[nb] = -1; dz[nb++] = -1;
  30919. dx[nb] = 0; dy[nb] = 1; dz[nb++] = -1;
  30920. dx[nb] = 0; dy[nb] = 1; dz[nb++] = 1;
  30921. dx[nb] = 1; dy[nb] = -1; dz[nb++] = 1;
  30922. dx[nb] = 1; dy[nb] = 0; dz[nb++] = 1;
  30923. dx[nb] = 1; dy[nb] = 1; dz[nb++] = 1;
  30924. }
  30925. }
  30926. return _label(nb,dx,dy,dz,tolerance,is_L2_norm);
  30927. }
  30928. //! Label connected components \overloading.
  30929. /**
  30930. \param connectivity_mask Mask of the neighboring pixels.
  30931. \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region.
  30932. \param is_L2_norm If true, tolerance is compared against L2 difference, otherwise L1 is used.
  30933. **/
  30934. template<typename t>
  30935. CImg<T>& label(const CImg<t>& connectivity_mask, const Tfloat tolerance=0,
  30936. const bool is_L2_norm=true) {
  30937. if (is_empty()) return *this;
  30938. return get_label(connectivity_mask,tolerance,is_L2_norm).move_to(*this);
  30939. }
  30940. //! Label connected components \newinstance.
  30941. template<typename t>
  30942. CImg<ulongT> get_label(const CImg<t>& connectivity_mask, const Tfloat tolerance=0,
  30943. const bool is_L2_norm=true) const {
  30944. if (is_empty()) return CImg<ulongT>();
  30945. int nb = 0;
  30946. cimg_for(connectivity_mask,ptr,t) if (*ptr) ++nb;
  30947. CImg<intT> dx(nb,1,1,1,0), dy(nb,1,1,1,0), dz(nb,1,1,1,0);
  30948. nb = 0;
  30949. cimg_forXYZ(connectivity_mask,x,y,z) if ((x || y || z) &&
  30950. connectivity_mask(x,y,z)) {
  30951. dx[nb] = x; dy[nb] = y; dz[nb++] = z;
  30952. }
  30953. return _label(nb,dx,dy,dz,tolerance,is_L2_norm);
  30954. }
  30955. CImg<ulongT> _label(const unsigned int nb, const int *const dx,
  30956. const int *const dy, const int *const dz,
  30957. const Tfloat tolerance, const bool is_L2_norm) const {
  30958. CImg<ulongT> res(_width,_height,_depth);
  30959. const Tfloat _tolerance = _spectrum>1 && is_L2_norm?cimg::sqr(tolerance):tolerance;
  30960. // Init label numbers.
  30961. ulongT *ptr = res.data();
  30962. cimg_foroff(res,p) *(ptr++) = p;
  30963. // For each neighbour-direction, label.
  30964. for (unsigned int n = 0; n<nb; ++n) {
  30965. const int _dx = dx[n], _dy = dy[n], _dz = dz[n];
  30966. if (_dx || _dy || _dz) {
  30967. const int
  30968. x0 = _dx<0?-_dx:0,
  30969. x1 = _dx<0?width():width() - _dx,
  30970. y0 = _dy<0?-_dy:0,
  30971. y1 = _dy<0?height():height() - _dy,
  30972. z0 = _dz<0?-_dz:0,
  30973. z1 = _dz<0?depth():depth() - _dz;
  30974. const longT
  30975. wh = (longT)width()*height(),
  30976. whd = (longT)width()*height()*depth(),
  30977. offset = _dz*wh + _dy*width() + _dx;
  30978. for (longT z = z0, nz = z0 + _dz, pz = z0*wh; z<z1; ++z, ++nz, pz+=wh) {
  30979. for (longT y = y0, ny = y0 + _dy, py = y0*width() + pz; y<y1; ++y, ++ny, py+=width()) {
  30980. for (longT x = x0, nx = x0 + _dx, p = x0 + py; x<x1; ++x, ++nx, ++p) {
  30981. Tfloat diff;
  30982. switch (_spectrum) {
  30983. case 1 :
  30984. diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd));
  30985. break;
  30986. case 2 :
  30987. if (is_L2_norm)
  30988. diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  30989. cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd));
  30990. else
  30991. diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  30992. cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd));
  30993. break;
  30994. case 3 :
  30995. if (is_L2_norm)
  30996. diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  30997. cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
  30998. cimg::sqr((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd));
  30999. else
  31000. diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  31001. cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
  31002. cimg::abs((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd));
  31003. break;
  31004. case 4 :
  31005. if (is_L2_norm)
  31006. diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  31007. cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
  31008. cimg::sqr((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd)) +
  31009. cimg::sqr((Tfloat)(*this)(x,y,z,3,wh,whd) - (Tfloat)(*this)(nx,ny,nz,3,wh,whd));
  31010. else
  31011. diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
  31012. cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
  31013. cimg::abs((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd)) +
  31014. cimg::abs((Tfloat)(*this)(x,y,z,3,wh,whd) - (Tfloat)(*this)(nx,ny,nz,3,wh,whd));
  31015. break;
  31016. default :
  31017. diff = 0;
  31018. if (is_L2_norm)
  31019. cimg_forC(*this,c)
  31020. diff+=cimg::sqr((Tfloat)(*this)(x,y,z,c,wh,whd) - (Tfloat)(*this)(nx,ny,nz,c,wh,whd));
  31021. else
  31022. cimg_forC(*this,c)
  31023. diff+=cimg::abs((Tfloat)(*this)(x,y,z,c,wh,whd) - (Tfloat)(*this)(nx,ny,nz,c,wh,whd));
  31024. }
  31025. if (diff<=_tolerance) {
  31026. const longT q = p + offset;
  31027. ulongT xk, yk;
  31028. for (xk = (ulongT)(p<q?q:p), yk = (ulongT)(p<q?p:q); xk!=yk && res[xk]!=xk; ) {
  31029. xk = res[xk]; if (xk<yk) cimg::swap(xk,yk);
  31030. }
  31031. if (xk!=yk) res[xk] = (ulongT)yk;
  31032. for (ulongT _p = (ulongT)p; _p!=yk; ) {
  31033. const ulongT h = res[_p];
  31034. res[_p] = (ulongT)yk;
  31035. _p = h;
  31036. }
  31037. for (ulongT _q = (ulongT)q; _q!=yk; ) {
  31038. const ulongT h = res[_q];
  31039. res[_q] = (ulongT)yk;
  31040. _q = h;
  31041. }
  31042. }
  31043. }
  31044. }
  31045. }
  31046. }
  31047. }
  31048. // Resolve equivalences.
  31049. ulongT counter = 0;
  31050. ptr = res.data();
  31051. cimg_foroff(res,p) { *ptr = *ptr==p?counter++:res[*ptr]; ++ptr; }
  31052. return res;
  31053. }
  31054. // [internal] Replace possibly malicious characters for commands to be called by system() by their escaped version.
  31055. CImg<T>& _system_strescape() {
  31056. #define cimg_system_strescape(c,s) case c : if (p!=ptrs) CImg<T>(ptrs,(unsigned int)(p-ptrs),1,1,1,false).\
  31057. move_to(list); \
  31058. CImg<T>(s,(unsigned int)std::strlen(s),1,1,1,false).move_to(list); ptrs = p + 1; break
  31059. CImgList<T> list;
  31060. const T *ptrs = _data;
  31061. cimg_for(*this,p,T) switch ((int)*p) {
  31062. cimg_system_strescape('\\',"\\\\");
  31063. cimg_system_strescape('\"',"\\\"");
  31064. cimg_system_strescape('!',"\"\\!\"");
  31065. cimg_system_strescape('`',"\\`");
  31066. cimg_system_strescape('$',"\\$");
  31067. }
  31068. if (ptrs<end()) CImg<T>(ptrs,(unsigned int)(end()-ptrs),1,1,1,false).move_to(list);
  31069. return (list>'x').move_to(*this);
  31070. }
  31071. //@}
  31072. //---------------------------------
  31073. //
  31074. //! \name Color Base Management
  31075. //@{
  31076. //---------------------------------
  31077. //! Return colormap \e "default", containing 256 colors entries in RGB.
  31078. /**
  31079. \return The following \c 256x1x1x3 colormap is returned:
  31080. \image html ref_colormap_default.jpg
  31081. **/
  31082. static const CImg<Tuchar>& default_LUT256() {
  31083. static CImg<Tuchar> colormap;
  31084. cimg::mutex(8);
  31085. if (!colormap) {
  31086. colormap.assign(1,256,1,3);
  31087. for (unsigned int index = 0, r = 16; r<256; r+=32)
  31088. for (unsigned int g = 16; g<256; g+=32)
  31089. for (unsigned int b = 32; b<256; b+=64) {
  31090. colormap(0,index,0) = (Tuchar)r;
  31091. colormap(0,index,1) = (Tuchar)g;
  31092. colormap(0,index++,2) = (Tuchar)b;
  31093. }
  31094. }
  31095. cimg::mutex(8,0);
  31096. return colormap;
  31097. }
  31098. //! Return colormap \e "HSV", containing 256 colors entries in RGB.
  31099. /**
  31100. \return The following \c 256x1x1x3 colormap is returned:
  31101. \image html ref_colormap_hsv.jpg
  31102. **/
  31103. static const CImg<Tuchar>& HSV_LUT256() {
  31104. static CImg<Tuchar> colormap;
  31105. cimg::mutex(8);
  31106. if (!colormap) {
  31107. CImg<Tint> tmp(1,256,1,3,1);
  31108. tmp.get_shared_channel(0).sequence(0,359);
  31109. colormap = tmp.HSVtoRGB();
  31110. }
  31111. cimg::mutex(8,0);
  31112. return colormap;
  31113. }
  31114. //! Return colormap \e "lines", containing 256 colors entries in RGB.
  31115. /**
  31116. \return The following \c 256x1x1x3 colormap is returned:
  31117. \image html ref_colormap_lines.jpg
  31118. **/
  31119. static const CImg<Tuchar>& lines_LUT256() {
  31120. static const unsigned char pal[] = {
  31121. 0,255,255,0,0,28,125,125,235,210,186,182,36,0,125,255,
  31122. 53,32,255,210,89,186,65,45,125,210,210,97,130,194,0,125,
  31123. 206,53,190,89,255,146,20,190,154,73,255,36,130,215,0,138,
  31124. 101,210,61,194,206,0,77,45,255,154,174,0,190,239,89,125,
  31125. 16,36,158,223,117,0,97,69,223,255,40,239,0,0,255,0,
  31126. 97,170,93,255,138,40,117,210,0,170,53,158,186,255,0,121,
  31127. 227,121,186,40,20,190,89,255,77,57,130,142,255,73,186,85,
  31128. 210,8,32,166,243,130,210,40,255,45,61,142,223,49,121,255,
  31129. 20,162,158,73,89,255,53,138,210,190,57,235,36,73,255,49,
  31130. 210,0,210,85,57,97,255,121,85,174,40,255,162,178,0,121,
  31131. 166,125,53,146,166,255,97,121,65,89,235,231,12,170,36,190,
  31132. 85,255,166,97,198,77,20,146,109,166,255,28,40,202,121,81,
  31133. 247,0,210,255,49,0,65,255,36,166,93,77,255,85,251,0,
  31134. 170,178,0,182,255,0,162,16,154,142,162,223,223,0,0,81,
  31135. 215,4,215,162,215,125,77,206,121,36,125,231,101,16,255,121,
  31136. 0,57,190,215,65,125,89,142,255,101,73,53,146,223,125,125,
  31137. 0,255,0,255,0,206,93,138,49,255,0,202,154,85,45,219,
  31138. 251,53,0,255,40,130,219,158,16,117,186,130,202,49,65,239,
  31139. 89,202,49,28,247,134,150,0,255,117,202,4,215,81,186,57,
  31140. 202,89,73,210,40,93,45,251,206,28,223,142,40,134,162,125,
  31141. 32,247,97,170,0,255,57,134,73,247,162,0,251,40,142,142,
  31142. 8,166,206,81,154,194,93,89,125,243,28,109,227,0,190,65,
  31143. 194,186,0,255,53,45,109,186,186,0,255,130,49,170,69,210,
  31144. 154,0,109,227,45,255,125,105,81,81,255,0,219,134,170,85,
  31145. 146,28,170,89,223,97,8,210,255,158,49,40,125,174,174,125,
  31146. 0,227,166,28,219,130,0,93,239,0,85,255,81,178,125,49,
  31147. 89,255,53,206,73,113,146,255,0,150,36,219,162,0,210,125,
  31148. 69,134,255,85,40,89,235,49,215,121,0,206,36,223,174,69,
  31149. 40,182,178,130,69,45,255,210,85,77,215,0,231,146,0,194,
  31150. 125,174,0,255,40,89,121,206,57,0,206,170,231,150,81,0,
  31151. 125,255,4,174,4,190,121,255,4,166,109,130,49,239,170,93,
  31152. 16,174,210,0,255,16,105,158,93,255,0,125,0,255,158,85,
  31153. 0,255,0,0,255,170,166,61,121,28,198,215,45,243,61,97,
  31154. 255,53,81,130,109,255,8,117,235,121,40,178,174,0,182,49,
  31155. 162,121,255,69,206,0,219,125,0,101,255,239,121,32,210,130,
  31156. 36,231,32,125,81,142,215,158,4,178,255,0,40,251,125,125,
  31157. 219,89,130,0,166,255,24,65,194,125,255,125,77,125,93,125,
  31158. 202,24,138,174,178,32,255,85,194,40,85,36,174,174,125,210,
  31159. 85,255,53,16,93,206,40,130,170,202,93,255,0,24,117,255,
  31160. 97,113,105,81,255,186,194,57,69,206,57,53,223,190,4,255,
  31161. 85,97,130,255,85,0,125,223,85,219,0,215,146,77,40,239,
  31162. 89,36,142,154,227,0,255,85,162,0,162,0,235,178,45,166,
  31163. 0,247,255,20,69,210,89,142,53,255,40,146,166,255,69,0,
  31164. 174,154,142,130,162,0,215,255,0,89,40,255,166,61,146,69,
  31165. 162,40,255,32,121,255,117,178,0,186,206,0,57,215,215,81,
  31166. 158,77,166,210,77,89,210,0,24,202,150,186,0,255,20,97,
  31167. 57,170,235,251,16,73,142,251,93,0,202,0,255,121,219,4,
  31168. 73,219,8,162,206,16,219,93,117,0,255,8,130,174,223,45 };
  31169. static const CImg<Tuchar> colormap(pal,1,256,1,3,false);
  31170. return colormap;
  31171. }
  31172. //! Return colormap \e "hot", containing 256 colors entries in RGB.
  31173. /**
  31174. \return The following \c 256x1x1x3 colormap is returned:
  31175. \image html ref_colormap_hot.jpg
  31176. **/
  31177. static const CImg<Tuchar>& hot_LUT256() {
  31178. static CImg<Tuchar> colormap;
  31179. cimg::mutex(8);
  31180. if (!colormap) {
  31181. colormap.assign(1,4,1,3,(T)0);
  31182. colormap[1] = colormap[2] = colormap[3] = colormap[6] = colormap[7] = colormap[11] = 255;
  31183. colormap.resize(1,256,1,3,3);
  31184. }
  31185. cimg::mutex(8,0);
  31186. return colormap;
  31187. }
  31188. //! Return colormap \e "cool", containing 256 colors entries in RGB.
  31189. /**
  31190. \return The following \c 256x1x1x3 colormap is returned:
  31191. \image html ref_colormap_cool.jpg
  31192. **/
  31193. static const CImg<Tuchar>& cool_LUT256() {
  31194. static CImg<Tuchar> colormap;
  31195. cimg::mutex(8);
  31196. if (!colormap) colormap.assign(1,2,1,3).fill((T)0,(T)255,(T)255,(T)0,(T)255,(T)255).resize(1,256,1,3,3);
  31197. cimg::mutex(8,0);
  31198. return colormap;
  31199. }
  31200. //! Return colormap \e "jet", containing 256 colors entries in RGB.
  31201. /**
  31202. \return The following \c 256x1x1x3 colormap is returned:
  31203. \image html ref_colormap_jet.jpg
  31204. **/
  31205. static const CImg<Tuchar>& jet_LUT256() {
  31206. static CImg<Tuchar> colormap;
  31207. cimg::mutex(8);
  31208. if (!colormap) {
  31209. colormap.assign(1,4,1,3,(T)0);
  31210. colormap[2] = colormap[3] = colormap[5] = colormap[6] = colormap[8] = colormap[9] = 255;
  31211. colormap.resize(1,256,1,3,3);
  31212. }
  31213. cimg::mutex(8,0);
  31214. return colormap;
  31215. }
  31216. //! Return colormap \e "flag", containing 256 colors entries in RGB.
  31217. /**
  31218. \return The following \c 256x1x1x3 colormap is returned:
  31219. \image html ref_colormap_flag.jpg
  31220. **/
  31221. static const CImg<Tuchar>& flag_LUT256() {
  31222. static CImg<Tuchar> colormap;
  31223. cimg::mutex(8);
  31224. if (!colormap) {
  31225. colormap.assign(1,4,1,3,(T)0);
  31226. colormap[0] = colormap[1] = colormap[5] = colormap[9] = colormap[10] = 255;
  31227. colormap.resize(1,256,1,3,0,2);
  31228. }
  31229. cimg::mutex(8,0);
  31230. return colormap;
  31231. }
  31232. //! Return colormap \e "cube", containing 256 colors entries in RGB.
  31233. /**
  31234. \return The following \c 256x1x1x3 colormap is returned:
  31235. \image html ref_colormap_cube.jpg
  31236. **/
  31237. static const CImg<Tuchar>& cube_LUT256() {
  31238. static CImg<Tuchar> colormap;
  31239. cimg::mutex(8);
  31240. if (!colormap) {
  31241. colormap.assign(1,8,1,3,(T)0);
  31242. colormap[1] = colormap[3] = colormap[5] = colormap[7] =
  31243. colormap[10] = colormap[11] = colormap[12] = colormap[13] =
  31244. colormap[20] = colormap[21] = colormap[22] = colormap[23] = 255;
  31245. colormap.resize(1,256,1,3,3);
  31246. }
  31247. cimg::mutex(8,0);
  31248. return colormap;
  31249. }
  31250. //! Convert pixel values from sRGB to RGB color spaces.
  31251. CImg<T>& sRGBtoRGB() {
  31252. if (is_empty()) return *this;
  31253. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))
  31254. cimg_rofoff(*this,off) {
  31255. const Tfloat
  31256. sval = (Tfloat)_data[off]/255,
  31257. val = (Tfloat)(sval<=0.04045f?sval/12.92f:std::pow((sval + 0.055f)/(1.055f),2.4f));
  31258. _data[off] = (T)cimg::cut(val*255,0,255);
  31259. }
  31260. return *this;
  31261. }
  31262. //! Convert pixel values from sRGB to RGB color spaces \newinstance.
  31263. CImg<Tfloat> get_sRGBtoRGB() const {
  31264. return CImg<Tfloat>(*this,false).sRGBtoRGB();
  31265. }
  31266. //! Convert pixel values from RGB to sRGB color spaces.
  31267. CImg<T>& RGBtosRGB() {
  31268. if (is_empty()) return *this;
  31269. cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))
  31270. cimg_rofoff(*this,off) {
  31271. const Tfloat
  31272. val = (Tfloat)_data[off]/255,
  31273. sval = (Tfloat)(val<=0.0031308f?val*12.92f:1.055f*std::pow(val,0.416667f) - 0.055f);
  31274. _data[off] = (T)cimg::cut(sval*255,0,255);
  31275. }
  31276. return *this;
  31277. }
  31278. //! Convert pixel values from RGB to sRGB color spaces \newinstance.
  31279. CImg<Tfloat> get_RGBtosRGB() const {
  31280. return CImg<Tfloat>(*this,false).RGBtosRGB();
  31281. }
  31282. //! Convert pixel values from RGB to HSI color spaces.
  31283. CImg<T>& RGBtoHSI() {
  31284. if (_spectrum!=3)
  31285. throw CImgInstanceException(_cimg_instance
  31286. "RGBtoHSI(): Instance is not a RGB image.",
  31287. cimg_instance);
  31288. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31289. const longT whd = (longT)width()*height()*depth();
  31290. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31291. for (longT N = 0; N<whd; ++N) {
  31292. const Tfloat
  31293. R = (Tfloat)p1[N],
  31294. G = (Tfloat)p2[N],
  31295. B = (Tfloat)p3[N],
  31296. m = cimg::min(R,G,B),
  31297. M = cimg::max(R,G,B),
  31298. C = M - m,
  31299. sum = R + G + B,
  31300. H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
  31301. S = sum<=0?0:1 - 3*m/sum,
  31302. I = sum/(3*255);
  31303. p1[N] = (T)H;
  31304. p2[N] = (T)S;
  31305. p3[N] = (T)I;
  31306. }
  31307. return *this;
  31308. }
  31309. //! Convert pixel values from RGB to HSI color spaces \newinstance.
  31310. CImg<Tfloat> get_RGBtoHSI() const {
  31311. return CImg<Tfloat>(*this,false).RGBtoHSI();
  31312. }
  31313. //! Convert pixel values from HSI to RGB color spaces.
  31314. CImg<T>& HSItoRGB() {
  31315. if (_spectrum!=3)
  31316. throw CImgInstanceException(_cimg_instance
  31317. "HSItoRGB(): Instance is not a HSI image.",
  31318. cimg_instance);
  31319. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31320. const longT whd = (longT)width()*height()*depth();
  31321. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31322. for (longT N = 0; N<whd; ++N) {
  31323. const Tfloat
  31324. H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
  31325. S = (Tfloat)p2[N],
  31326. I = (Tfloat)p3[N],
  31327. Z = 1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1),
  31328. C = I*S/(1 + Z),
  31329. X = C*Z,
  31330. m = I*(1 - S)/3;
  31331. Tfloat R, G, B;
  31332. switch ((int)H) {
  31333. case 0 : R = C; G = X; B = 0; break;
  31334. case 1 : R = X; G = C; B = 0; break;
  31335. case 2 : R = 0; G = C; B = X; break;
  31336. case 3 : R = 0; G = X; B = C; break;
  31337. case 4 : R = X; G = 0; B = C; break;
  31338. default : R = C; G = 0; B = X;
  31339. }
  31340. p1[N] = (T)((R + m)*3*255);
  31341. p2[N] = (T)((G + m)*3*255);
  31342. p3[N] = (T)((B + m)*3*255);
  31343. }
  31344. return *this;
  31345. }
  31346. //! Convert pixel values from HSI to RGB color spaces \newinstance.
  31347. CImg<Tfloat> get_HSItoRGB() const {
  31348. return CImg< Tuchar>(*this,false).HSItoRGB();
  31349. }
  31350. //! Convert pixel values from RGB to HSL color spaces.
  31351. CImg<T>& RGBtoHSL() {
  31352. if (_spectrum!=3)
  31353. throw CImgInstanceException(_cimg_instance
  31354. "RGBtoHSL(): Instance is not a RGB image.",
  31355. cimg_instance);
  31356. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31357. const longT whd = (longT)width()*height()*depth();
  31358. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31359. for (longT N = 0; N<whd; ++N) {
  31360. const Tfloat
  31361. R = (Tfloat)p1[N],
  31362. G = (Tfloat)p2[N],
  31363. B = (Tfloat)p3[N],
  31364. m = cimg::min(R,G,B),
  31365. M = cimg::max(R,G,B),
  31366. C = M - m,
  31367. H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
  31368. L = 0.5f*(m + M)/255,
  31369. S = L==1 || L==0?0:C/(1 - cimg::abs(2*L - 1))/255;
  31370. p1[N] = (T)H;
  31371. p2[N] = (T)S;
  31372. p3[N] = (T)L;
  31373. }
  31374. return *this;
  31375. }
  31376. //! Convert pixel values from RGB to HSL color spaces \newinstance.
  31377. CImg<Tfloat> get_RGBtoHSL() const {
  31378. return CImg<Tfloat>(*this,false).RGBtoHSL();
  31379. }
  31380. //! Convert pixel values from HSL to RGB color spaces.
  31381. CImg<T>& HSLtoRGB() {
  31382. if (_spectrum!=3)
  31383. throw CImgInstanceException(_cimg_instance
  31384. "HSLtoRGB(): Instance is not a HSL image.",
  31385. cimg_instance);
  31386. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31387. const longT whd = (longT)width()*height()*depth();
  31388. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31389. for (longT N = 0; N<whd; ++N) {
  31390. const Tfloat
  31391. H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
  31392. S = (Tfloat)p2[N],
  31393. L = (Tfloat)p3[N],
  31394. C = (1 - cimg::abs(2*L - 1))*S,
  31395. X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),
  31396. m = L - C/2;
  31397. Tfloat R, G, B;
  31398. switch ((int)H) {
  31399. case 0 : R = C; G = X; B = 0; break;
  31400. case 1 : R = X; G = C; B = 0; break;
  31401. case 2 : R = 0; G = C; B = X; break;
  31402. case 3 : R = 0; G = X; B = C; break;
  31403. case 4 : R = X; G = 0; B = C; break;
  31404. default : R = C; G = 0; B = X;
  31405. }
  31406. p1[N] = (T)((R + m)*255);
  31407. p2[N] = (T)((G + m)*255);
  31408. p3[N] = (T)((B + m)*255);
  31409. }
  31410. return *this;
  31411. }
  31412. //! Convert pixel values from HSL to RGB color spaces \newinstance.
  31413. CImg<Tuchar> get_HSLtoRGB() const {
  31414. return CImg<Tuchar>(*this,false).HSLtoRGB();
  31415. }
  31416. //! Convert pixel values from RGB to HSV color spaces.
  31417. CImg<T>& RGBtoHSV() {
  31418. if (_spectrum!=3)
  31419. throw CImgInstanceException(_cimg_instance
  31420. "RGBtoHSV(): Instance is not a RGB image.",
  31421. cimg_instance);
  31422. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31423. const longT whd = (longT)width()*height()*depth();
  31424. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31425. for (longT N = 0; N<whd; ++N) {
  31426. const Tfloat
  31427. R = (Tfloat)p1[N],
  31428. G = (Tfloat)p2[N],
  31429. B = (Tfloat)p3[N],
  31430. M = cimg::max(R,G,B),
  31431. C = M - cimg::min(R,G,B),
  31432. H = 60*(C==0?0:M==R?cimg::mod((G-B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
  31433. S = M<=0?0:C/M;
  31434. p1[N] = (T)H;
  31435. p2[N] = (T)S;
  31436. p3[N] = (T)(M/255);
  31437. }
  31438. return *this;
  31439. }
  31440. //! Convert pixel values from RGB to HSV color spaces \newinstance.
  31441. CImg<Tfloat> get_RGBtoHSV() const {
  31442. return CImg<Tfloat>(*this,false).RGBtoHSV();
  31443. }
  31444. //! Convert pixel values from HSV to RGB color spaces.
  31445. CImg<T>& HSVtoRGB() {
  31446. if (_spectrum!=3)
  31447. throw CImgInstanceException(_cimg_instance
  31448. "HSVtoRGB(): Instance is not a HSV image.",
  31449. cimg_instance);
  31450. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31451. const longT whd = (longT)width()*height()*depth();
  31452. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
  31453. for (longT N = 0; N<whd; ++N) {
  31454. Tfloat
  31455. H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
  31456. S = (Tfloat)p2[N],
  31457. V = (Tfloat)p3[N],
  31458. C = V*S,
  31459. X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),
  31460. m = V - C;
  31461. Tfloat R, G, B;
  31462. switch ((int)H) {
  31463. case 0 : R = C; G = X; B = 0; break;
  31464. case 1 : R = X; G = C; B = 0; break;
  31465. case 2 : R = 0; G = C; B = X; break;
  31466. case 3 : R = 0; G = X; B = C; break;
  31467. case 4 : R = X; G = 0; B = C; break;
  31468. default : R = C; G = 0; B = X;
  31469. }
  31470. p1[N] = (T)((R + m)*255);
  31471. p2[N] = (T)((G + m)*255);
  31472. p3[N] = (T)((B + m)*255);
  31473. }
  31474. return *this;
  31475. }
  31476. //! Convert pixel values from HSV to RGB color spaces \newinstance.
  31477. CImg<Tuchar> get_HSVtoRGB() const {
  31478. return CImg<Tuchar>(*this,false).HSVtoRGB();
  31479. }
  31480. //! Convert pixel values from RGB to YCbCr color spaces.
  31481. CImg<T>& RGBtoYCbCr() {
  31482. if (_spectrum!=3)
  31483. throw CImgInstanceException(_cimg_instance
  31484. "RGBtoYCbCr(): Instance is not a RGB image.",
  31485. cimg_instance);
  31486. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31487. const longT whd = (longT)width()*height()*depth();
  31488. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,512))
  31489. for (longT N = 0; N<whd; ++N) {
  31490. const Tfloat
  31491. R = (Tfloat)p1[N],
  31492. G = (Tfloat)p2[N],
  31493. B = (Tfloat)p3[N],
  31494. Y = (66*R + 129*G + 25*B + 128)/256 + 16,
  31495. Cb = (-38*R - 74*G + 112*B + 128)/256 + 128,
  31496. Cr = (112*R - 94*G - 18*B + 128)/256 + 128;
  31497. p1[N] = (T)cimg::cut(Y,0,255),
  31498. p2[N] = (T)cimg::cut(Cb,0,255),
  31499. p3[N] = (T)cimg::cut(Cr,0,255);
  31500. }
  31501. return *this;
  31502. }
  31503. //! Convert pixel values from RGB to YCbCr color spaces \newinstance.
  31504. CImg<Tuchar> get_RGBtoYCbCr() const {
  31505. return CImg<Tuchar>(*this,false).RGBtoYCbCr();
  31506. }
  31507. //! Convert pixel values from RGB to YCbCr color spaces.
  31508. CImg<T>& YCbCrtoRGB() {
  31509. if (_spectrum!=3)
  31510. throw CImgInstanceException(_cimg_instance
  31511. "YCbCrtoRGB(): Instance is not a YCbCr image.",
  31512. cimg_instance);
  31513. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31514. const longT whd = (longT)width()*height()*depth();
  31515. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,512))
  31516. for (longT N = 0; N<whd; ++N) {
  31517. const Tfloat
  31518. Y = (Tfloat)p1[N] - 16,
  31519. Cb = (Tfloat)p2[N] - 128,
  31520. Cr = (Tfloat)p3[N] - 128,
  31521. R = (298*Y + 409*Cr + 128)/256,
  31522. G = (298*Y - 100*Cb - 208*Cr + 128)/256,
  31523. B = (298*Y + 516*Cb + 128)/256;
  31524. p1[N] = (T)cimg::cut(R,0,255),
  31525. p2[N] = (T)cimg::cut(G,0,255),
  31526. p3[N] = (T)cimg::cut(B,0,255);
  31527. }
  31528. return *this;
  31529. }
  31530. //! Convert pixel values from RGB to YCbCr color spaces \newinstance.
  31531. CImg<Tuchar> get_YCbCrtoRGB() const {
  31532. return CImg<Tuchar>(*this,false).YCbCrtoRGB();
  31533. }
  31534. //! Convert pixel values from RGB to YUV color spaces.
  31535. CImg<T>& RGBtoYUV() {
  31536. if (_spectrum!=3)
  31537. throw CImgInstanceException(_cimg_instance
  31538. "RGBtoYUV(): Instance is not a RGB image.",
  31539. cimg_instance);
  31540. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31541. const longT whd = (longT)width()*height()*depth();
  31542. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,16384))
  31543. for (longT N = 0; N<whd; ++N) {
  31544. const Tfloat
  31545. R = (Tfloat)p1[N]/255,
  31546. G = (Tfloat)p2[N]/255,
  31547. B = (Tfloat)p3[N]/255,
  31548. Y = 0.299f*R + 0.587f*G + 0.114f*B;
  31549. p1[N] = (T)Y;
  31550. p2[N] = (T)(0.492f*(B - Y));
  31551. p3[N] = (T)(0.877*(R - Y));
  31552. }
  31553. return *this;
  31554. }
  31555. //! Convert pixel values from RGB to YUV color spaces \newinstance.
  31556. CImg<Tfloat> get_RGBtoYUV() const {
  31557. return CImg<Tfloat>(*this,false).RGBtoYUV();
  31558. }
  31559. //! Convert pixel values from YUV to RGB color spaces.
  31560. CImg<T>& YUVtoRGB() {
  31561. if (_spectrum!=3)
  31562. throw CImgInstanceException(_cimg_instance
  31563. "YUVtoRGB(): Instance is not a YUV image.",
  31564. cimg_instance);
  31565. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31566. const longT whd = (longT)width()*height()*depth();
  31567. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,16384))
  31568. for (longT N = 0; N<whd; ++N) {
  31569. const Tfloat
  31570. Y = (Tfloat)p1[N],
  31571. U = (Tfloat)p2[N],
  31572. V = (Tfloat)p3[N],
  31573. R = (Y + 1.140f*V)*255,
  31574. G = (Y - 0.395f*U - 0.581f*V)*255,
  31575. B = (Y + 2.032f*U)*255;
  31576. p1[N] = (T)cimg::cut(R,0,255),
  31577. p2[N] = (T)cimg::cut(G,0,255),
  31578. p3[N] = (T)cimg::cut(B,0,255);
  31579. }
  31580. return *this;
  31581. }
  31582. //! Convert pixel values from YUV to RGB color spaces \newinstance.
  31583. CImg<Tuchar> get_YUVtoRGB() const {
  31584. return CImg< Tuchar>(*this,false).YUVtoRGB();
  31585. }
  31586. //! Convert pixel values from RGB to CMY color spaces.
  31587. CImg<T>& RGBtoCMY() {
  31588. if (_spectrum!=3)
  31589. throw CImgInstanceException(_cimg_instance
  31590. "RGBtoCMY(): Instance is not a RGB image.",
  31591. cimg_instance);
  31592. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31593. const longT whd = (longT)width()*height()*depth();
  31594. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
  31595. for (longT N = 0; N<whd; ++N) {
  31596. const Tfloat
  31597. R = (Tfloat)p1[N],
  31598. G = (Tfloat)p2[N],
  31599. B = (Tfloat)p3[N],
  31600. C = 255 - R,
  31601. M = 255 - G,
  31602. Y = 255 - B;
  31603. p1[N] = (T)cimg::cut(C,0,255),
  31604. p2[N] = (T)cimg::cut(M,0,255),
  31605. p3[N] = (T)cimg::cut(Y,0,255);
  31606. }
  31607. return *this;
  31608. }
  31609. //! Convert pixel values from RGB to CMY color spaces \newinstance.
  31610. CImg<Tuchar> get_RGBtoCMY() const {
  31611. return CImg<Tfloat>(*this,false).RGBtoCMY();
  31612. }
  31613. //! Convert pixel values from CMY to RGB color spaces.
  31614. CImg<T>& CMYtoRGB() {
  31615. if (_spectrum!=3)
  31616. throw CImgInstanceException(_cimg_instance
  31617. "CMYtoRGB(): Instance is not a CMY image.",
  31618. cimg_instance);
  31619. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31620. const longT whd = (longT)width()*height()*depth();
  31621. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
  31622. for (longT N = 0; N<whd; ++N) {
  31623. const Tfloat
  31624. C = (Tfloat)p1[N],
  31625. M = (Tfloat)p2[N],
  31626. Y = (Tfloat)p3[N],
  31627. R = 255 - C,
  31628. G = 255 - M,
  31629. B = 255 - Y;
  31630. p1[N] = (T)cimg::cut(R,0,255),
  31631. p2[N] = (T)cimg::cut(G,0,255),
  31632. p3[N] = (T)cimg::cut(B,0,255);
  31633. }
  31634. return *this;
  31635. }
  31636. //! Convert pixel values from CMY to RGB color spaces \newinstance.
  31637. CImg<Tuchar> get_CMYtoRGB() const {
  31638. return CImg<Tuchar>(*this,false).CMYtoRGB();
  31639. }
  31640. //! Convert pixel values from CMY to CMYK color spaces.
  31641. CImg<T>& CMYtoCMYK() {
  31642. return get_CMYtoCMYK().move_to(*this);
  31643. }
  31644. //! Convert pixel values from CMY to CMYK color spaces \newinstance.
  31645. CImg<Tuchar> get_CMYtoCMYK() const {
  31646. if (_spectrum!=3)
  31647. throw CImgInstanceException(_cimg_instance
  31648. "CMYtoCMYK(): Instance is not a CMY image.",
  31649. cimg_instance);
  31650. CImg<Tfloat> res(_width,_height,_depth,4);
  31651. const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2);
  31652. Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2), *pd4 = res.data(0,0,0,3);
  31653. const longT whd = (longT)width()*height()*depth();
  31654. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,1024))
  31655. for (longT N = 0; N<whd; ++N) {
  31656. Tfloat
  31657. C = (Tfloat)ps1[N],
  31658. M = (Tfloat)ps2[N],
  31659. Y = (Tfloat)ps3[N],
  31660. K = cimg::min(C,M,Y);
  31661. if (K>=255) C = M = Y = 0;
  31662. else { const Tfloat K1 = 255 - K; C = 255*(C - K)/K1; M = 255*(M - K)/K1; Y = 255*(Y - K)/K1; }
  31663. pd1[N] = (Tfloat)cimg::cut(C,0,255),
  31664. pd2[N] = (Tfloat)cimg::cut(M,0,255),
  31665. pd3[N] = (Tfloat)cimg::cut(Y,0,255),
  31666. pd4[N] = (Tfloat)cimg::cut(K,0,255);
  31667. }
  31668. return res;
  31669. }
  31670. //! Convert pixel values from CMYK to CMY color spaces.
  31671. CImg<T>& CMYKtoCMY() {
  31672. return get_CMYKtoCMY().move_to(*this);
  31673. }
  31674. //! Convert pixel values from CMYK to CMY color spaces \newinstance.
  31675. CImg<Tfloat> get_CMYKtoCMY() const {
  31676. if (_spectrum!=4)
  31677. throw CImgInstanceException(_cimg_instance
  31678. "CMYKtoCMY(): Instance is not a CMYK image.",
  31679. cimg_instance);
  31680. CImg<Tfloat> res(_width,_height,_depth,3);
  31681. const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2), *ps4 = data(0,0,0,3);
  31682. Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2);
  31683. const longT whd = (longT)width()*height()*depth();
  31684. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,1024))
  31685. for (longT N = 0; N<whd; ++N) {
  31686. const Tfloat
  31687. C = (Tfloat)ps1[N],
  31688. M = (Tfloat)ps2[N],
  31689. Y = (Tfloat)ps3[N],
  31690. K = (Tfloat)ps4[N],
  31691. K1 = 1 - K/255,
  31692. nC = C*K1 + K,
  31693. nM = M*K1 + K,
  31694. nY = Y*K1 + K;
  31695. pd1[N] = (Tfloat)cimg::cut(nC,0,255),
  31696. pd2[N] = (Tfloat)cimg::cut(nM,0,255),
  31697. pd3[N] = (Tfloat)cimg::cut(nY,0,255);
  31698. }
  31699. return res;
  31700. }
  31701. //! Convert pixel values from RGB to XYZ color spaces.
  31702. /**
  31703. \param use_D65 Tell to use the D65 illuminant (D50 otherwise).
  31704. **/
  31705. CImg<T>& RGBtoXYZ(const bool use_D65=true) {
  31706. if (_spectrum!=3)
  31707. throw CImgInstanceException(_cimg_instance
  31708. "RGBtoXYZ(): Instance is not a RGB image.",
  31709. cimg_instance);
  31710. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31711. const longT whd = (longT)width()*height()*depth();
  31712. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
  31713. for (longT N = 0; N<whd; ++N) {
  31714. const Tfloat
  31715. R = (Tfloat)p1[N]/255,
  31716. G = (Tfloat)p2[N]/255,
  31717. B = (Tfloat)p3[N]/255;
  31718. if (use_D65) { // D65
  31719. p1[N] = (T)(0.4124564*R + 0.3575761*G + 0.1804375*B);
  31720. p2[N] = (T)(0.2126729*R + 0.7151522*G + 0.0721750*B);
  31721. p3[N] = (T)(0.0193339*R + 0.1191920*G + 0.9503041*B);
  31722. } else { // D50
  31723. p1[N] = (T)(0.43603516*R + 0.38511658*G + 0.14305115*B);
  31724. p2[N] = (T)(0.22248840*R + 0.71690369*G + 0.06060791*B);
  31725. p3[N] = (T)(0.01391602*R + 0.09706116*G + 0.71392822*B);
  31726. }
  31727. }
  31728. return *this;
  31729. }
  31730. //! Convert pixel values from RGB to XYZ color spaces \newinstance.
  31731. CImg<Tfloat> get_RGBtoXYZ(const bool use_D65=true) const {
  31732. return CImg<Tfloat>(*this,false).RGBtoXYZ(use_D65);
  31733. }
  31734. //! Convert pixel values from XYZ to RGB color spaces.
  31735. /**
  31736. \param use_D65 Tell to use the D65 illuminant (D50 otherwise).
  31737. **/
  31738. CImg<T>& XYZtoRGB(const bool use_D65=true) {
  31739. if (_spectrum!=3)
  31740. throw CImgInstanceException(_cimg_instance
  31741. "XYZtoRGB(): Instance is not a XYZ image.",
  31742. cimg_instance);
  31743. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31744. const longT whd = (longT)width()*height()*depth();
  31745. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
  31746. for (longT N = 0; N<whd; ++N) {
  31747. const Tfloat
  31748. X = (Tfloat)p1[N]*255,
  31749. Y = (Tfloat)p2[N]*255,
  31750. Z = (Tfloat)p3[N]*255;
  31751. if (use_D65) {
  31752. p1[N] = (T)cimg::cut(3.2404542*X - 1.5371385*Y - 0.4985314*Z,0,255);
  31753. p2[N] = (T)cimg::cut(-0.9692660*X + 1.8760108*Y + 0.0415560*Z,0,255);
  31754. p3[N] = (T)cimg::cut(0.0556434*X - 0.2040259*Y + 1.0572252*Z,0,255);
  31755. } else {
  31756. p1[N] = (T)cimg::cut(3.134274799724*X - 1.617275708956*Y - 0.490724283042*Z,0,255);
  31757. p2[N] = (T)cimg::cut(-0.978795575994*X + 1.916161689117*Y + 0.033453331711*Z,0,255);
  31758. p3[N] = (T)cimg::cut(0.071976988401*X - 0.228984974402*Y + 1.405718224383*Z,0,255);
  31759. }
  31760. }
  31761. return *this;
  31762. }
  31763. //! Convert pixel values from XYZ to RGB color spaces \newinstance.
  31764. CImg<Tuchar> get_XYZtoRGB(const bool use_D65=true) const {
  31765. return CImg<Tuchar>(*this,false).XYZtoRGB(use_D65);
  31766. }
  31767. //! Convert pixel values from XYZ to Lab color spaces.
  31768. CImg<T>& XYZtoLab(const bool use_D65=true) {
  31769. #define _cimg_Labf(x) (24389*(x)>216?cimg::cbrt(x):(24389*(x)/27 + 16)/116)
  31770. if (_spectrum!=3)
  31771. throw CImgInstanceException(_cimg_instance
  31772. "XYZtoLab(): Instance is not a XYZ image.",
  31773. cimg_instance);
  31774. const CImg<Tfloat> white = CImg<Tfloat>(1,1,1,3,255).RGBtoXYZ(use_D65);
  31775. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31776. const longT whd = (longT)width()*height()*depth();
  31777. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,128))
  31778. for (longT N = 0; N<whd; ++N) {
  31779. const Tfloat
  31780. X = (Tfloat)(p1[N]/white[0]),
  31781. Y = (Tfloat)(p2[N]/white[1]),
  31782. Z = (Tfloat)(p3[N]/white[2]),
  31783. fX = (Tfloat)_cimg_Labf(X),
  31784. fY = (Tfloat)_cimg_Labf(Y),
  31785. fZ = (Tfloat)_cimg_Labf(Z);
  31786. p1[N] = (T)cimg::cut(116*fY - 16,0,100);
  31787. p2[N] = (T)(500*(fX - fY));
  31788. p3[N] = (T)(200*(fY - fZ));
  31789. }
  31790. return *this;
  31791. }
  31792. //! Convert pixel values from XYZ to Lab color spaces \newinstance.
  31793. CImg<Tfloat> get_XYZtoLab(const bool use_D65=true) const {
  31794. return CImg<Tfloat>(*this,false).XYZtoLab(use_D65);
  31795. }
  31796. //! Convert pixel values from Lab to XYZ color spaces.
  31797. CImg<T>& LabtoXYZ(const bool use_D65=true) {
  31798. if (_spectrum!=3)
  31799. throw CImgInstanceException(_cimg_instance
  31800. "LabtoXYZ(): Instance is not a Lab image.",
  31801. cimg_instance);
  31802. const CImg<Tfloat> white = CImg<Tfloat>(1,1,1,3,255).RGBtoXYZ(use_D65);
  31803. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31804. const longT whd = (longT)width()*height()*depth();
  31805. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,128))
  31806. for (longT N = 0; N<whd; ++N) {
  31807. const Tfloat
  31808. L = (Tfloat)p1[N],
  31809. a = (Tfloat)p2[N],
  31810. b = (Tfloat)p3[N],
  31811. cY = (L + 16)/116,
  31812. cZ = cY - b/200,
  31813. cX = a/500 + cY,
  31814. X = (Tfloat)(24389*cX>216?cX*cX*cX:(116*cX - 16)*27/24389),
  31815. Y = (Tfloat)(27*L>216?cY*cY*cY:27*L/24389),
  31816. Z = (Tfloat)(24389*cZ>216?cZ*cZ*cZ:(116*cZ - 16)*27/24389);
  31817. p1[N] = (T)(X*white[0]);
  31818. p2[N] = (T)(Y*white[1]);
  31819. p3[N] = (T)(Z*white[2]);
  31820. }
  31821. return *this;
  31822. }
  31823. //! Convert pixel values from Lab to XYZ color spaces \newinstance.
  31824. CImg<Tfloat> get_LabtoXYZ(const bool use_D65=true) const {
  31825. return CImg<Tfloat>(*this,false).LabtoXYZ(use_D65);
  31826. }
  31827. //! Convert pixel values from XYZ to xyY color spaces.
  31828. CImg<T>& XYZtoxyY() {
  31829. if (_spectrum!=3)
  31830. throw CImgInstanceException(_cimg_instance
  31831. "XYZtoxyY(): Instance is not a XYZ image.",
  31832. cimg_instance);
  31833. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31834. const longT whd = (longT)width()*height()*depth();
  31835. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,4096))
  31836. for (longT N = 0; N<whd; ++N) {
  31837. const Tfloat
  31838. X = (Tfloat)p1[N],
  31839. Y = (Tfloat)p2[N],
  31840. Z = (Tfloat)p3[N],
  31841. sum = X + Y + Z,
  31842. nsum = sum>0?sum:1;
  31843. p1[N] = (T)(X/nsum);
  31844. p2[N] = (T)(Y/nsum);
  31845. p3[N] = (T)Y;
  31846. }
  31847. return *this;
  31848. }
  31849. //! Convert pixel values from XYZ to xyY color spaces \newinstance.
  31850. CImg<Tfloat> get_XYZtoxyY() const {
  31851. return CImg<Tfloat>(*this,false).XYZtoxyY();
  31852. }
  31853. //! Convert pixel values from xyY pixels to XYZ color spaces.
  31854. CImg<T>& xyYtoXYZ() {
  31855. if (_spectrum!=3)
  31856. throw CImgInstanceException(_cimg_instance
  31857. "xyYtoXYZ(): Instance is not a xyY image.",
  31858. cimg_instance);
  31859. T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
  31860. const longT whd = (longT)width()*height()*depth();
  31861. cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,4096))
  31862. for (longT N = 0; N<whd; ++N) {
  31863. const Tfloat
  31864. px = (Tfloat)p1[N],
  31865. py = (Tfloat)p2[N],
  31866. Y = (Tfloat)p3[N],
  31867. ny = py>0?py:1;
  31868. p1[N] = (T)(px*Y/ny);
  31869. p2[N] = (T)Y;
  31870. p3[N] = (T)((1 - px - py)*Y/ny);
  31871. }
  31872. return *this;
  31873. }
  31874. //! Convert pixel values from xyY pixels to XYZ color spaces \newinstance.
  31875. CImg<Tfloat> get_xyYtoXYZ() const {
  31876. return CImg<Tfloat>(*this,false).xyYtoXYZ();
  31877. }
  31878. //! Convert pixel values from RGB to Lab color spaces.
  31879. CImg<T>& RGBtoLab(const bool use_D65=true) {
  31880. return RGBtoXYZ(use_D65).XYZtoLab(use_D65);
  31881. }
  31882. //! Convert pixel values from RGB to Lab color spaces \newinstance.
  31883. CImg<Tfloat> get_RGBtoLab(const bool use_D65=true) const {
  31884. return CImg<Tfloat>(*this,false).RGBtoLab(use_D65);
  31885. }
  31886. //! Convert pixel values from Lab to RGB color spaces.
  31887. CImg<T>& LabtoRGB(const bool use_D65=true) {
  31888. return LabtoXYZ().XYZtoRGB(use_D65);
  31889. }
  31890. //! Convert pixel values from Lab to RGB color spaces \newinstance.
  31891. CImg<Tuchar> get_LabtoRGB(const bool use_D65=true) const {
  31892. return CImg<Tuchar>(*this,false).LabtoRGB(use_D65);
  31893. }
  31894. //! Convert pixel values from RGB to xyY color spaces.
  31895. CImg<T>& RGBtoxyY(const bool use_D65=true) {
  31896. return RGBtoXYZ(use_D65).XYZtoxyY();
  31897. }
  31898. //! Convert pixel values from RGB to xyY color spaces \newinstance.
  31899. CImg<Tfloat> get_RGBtoxyY(const bool use_D65=true) const {
  31900. return CImg<Tfloat>(*this,false).RGBtoxyY(use_D65);
  31901. }
  31902. //! Convert pixel values from xyY to RGB color spaces.
  31903. CImg<T>& xyYtoRGB(const bool use_D65=true) {
  31904. return xyYtoXYZ().XYZtoRGB(use_D65);
  31905. }
  31906. //! Convert pixel values from xyY to RGB color spaces \newinstance.
  31907. CImg<Tuchar> get_xyYtoRGB(const bool use_D65=true) const {
  31908. return CImg<Tuchar>(*this,false).xyYtoRGB(use_D65);
  31909. }
  31910. //! Convert pixel values from RGB to CMYK color spaces.
  31911. CImg<T>& RGBtoCMYK() {
  31912. return RGBtoCMY().CMYtoCMYK();
  31913. }
  31914. //! Convert pixel values from RGB to CMYK color spaces \newinstance.
  31915. CImg<Tfloat> get_RGBtoCMYK() const {
  31916. return CImg<Tfloat>(*this,false).RGBtoCMYK();
  31917. }
  31918. //! Convert pixel values from CMYK to RGB color spaces.
  31919. CImg<T>& CMYKtoRGB() {
  31920. return CMYKtoCMY().CMYtoRGB();
  31921. }
  31922. //! Convert pixel values from CMYK to RGB color spaces \newinstance.
  31923. CImg<Tuchar> get_CMYKtoRGB() const {
  31924. return CImg<Tuchar>(*this,false).CMYKtoRGB();
  31925. }
  31926. //@}
  31927. //------------------------------------------
  31928. //
  31929. //! \name Geometric / Spatial Manipulation
  31930. //@{
  31931. //------------------------------------------
  31932. static float _cimg_lanczos(const float x) {
  31933. if (x<=-2 || x>=2) return 0;
  31934. const float a = (float)cimg::PI*x, b = 0.5f*a;
  31935. return (float)(x?std::sin(a)*std::sin(b)/(a*b):1);
  31936. }
  31937. //! Resize image to new dimensions.
  31938. /**
  31939. \param size_x Number of columns (new size along the X-axis).
  31940. \param size_y Number of rows (new size along the Y-axis).
  31941. \param size_z Number of slices (new size along the Z-axis).
  31942. \param size_c Number of vector-channels (new size along the C-axis).
  31943. \param interpolation_type Method of interpolation:
  31944. - -1 = no interpolation: raw memory resizing.
  31945. - 0 = no interpolation: additional space is filled according to \p boundary_conditions.
  31946. - 1 = nearest-neighbor interpolation.
  31947. - 2 = moving average interpolation.
  31948. - 3 = linear interpolation.
  31949. - 4 = grid interpolation.
  31950. - 5 = cubic interpolation.
  31951. - 6 = lanczos interpolation.
  31952. \param boundary_conditions Type of boundary conditions used if necessary.
  31953. \param centering_x Set centering type (only if \p interpolation_type=0).
  31954. \param centering_y Set centering type (only if \p interpolation_type=0).
  31955. \param centering_z Set centering type (only if \p interpolation_type=0).
  31956. \param centering_c Set centering type (only if \p interpolation_type=0).
  31957. \note If pd[x,y,z,v]<0, it corresponds to a percentage of the original size (the default value is -100).
  31958. **/
  31959. CImg<T>& resize(const int size_x, const int size_y=-100,
  31960. const int size_z=-100, const int size_c=-100,
  31961. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  31962. const float centering_x = 0, const float centering_y = 0,
  31963. const float centering_z = 0, const float centering_c = 0) {
  31964. if (!size_x || !size_y || !size_z || !size_c) return assign();
  31965. const unsigned int
  31966. _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x),
  31967. _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y),
  31968. _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z),
  31969. _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c),
  31970. sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1;
  31971. if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return *this;
  31972. if (is_empty()) return assign(sx,sy,sz,sc,(T)0);
  31973. if (interpolation_type==-1 && sx*sy*sz*sc==size()) {
  31974. _width = sx; _height = sy; _depth = sz; _spectrum = sc;
  31975. return *this;
  31976. }
  31977. return get_resize(sx,sy,sz,sc,interpolation_type,boundary_conditions,
  31978. centering_x,centering_y,centering_z,centering_c).move_to(*this);
  31979. }
  31980. //! Resize image to new dimensions \newinstance.
  31981. CImg<T> get_resize(const int size_x, const int size_y = -100,
  31982. const int size_z = -100, const int size_c = -100,
  31983. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  31984. const float centering_x = 0, const float centering_y = 0,
  31985. const float centering_z = 0, const float centering_c = 0) const {
  31986. if (centering_x<0 || centering_x>1 || centering_y<0 || centering_y>1 ||
  31987. centering_z<0 || centering_z>1 || centering_c<0 || centering_c>1)
  31988. throw CImgArgumentException(_cimg_instance
  31989. "resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].",
  31990. cimg_instance,
  31991. centering_x,centering_y,centering_z,centering_c);
  31992. if (!size_x || !size_y || !size_z || !size_c) return CImg<T>();
  31993. const unsigned int
  31994. sx = std::max(1U,(unsigned int)(size_x>=0?size_x:-size_x*width()/100)),
  31995. sy = std::max(1U,(unsigned int)(size_y>=0?size_y:-size_y*height()/100)),
  31996. sz = std::max(1U,(unsigned int)(size_z>=0?size_z:-size_z*depth()/100)),
  31997. sc = std::max(1U,(unsigned int)(size_c>=0?size_c:-size_c*spectrum()/100));
  31998. if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return +*this;
  31999. if (is_empty()) return CImg<T>(sx,sy,sz,sc,(T)0);
  32000. CImg<T> res;
  32001. switch (interpolation_type) {
  32002. // Raw resizing.
  32003. //
  32004. case -1 :
  32005. std::memcpy(res.assign(sx,sy,sz,sc,(T)0)._data,_data,sizeof(T)*std::min(size(),(ulongT)sx*sy*sz*sc));
  32006. break;
  32007. // No interpolation.
  32008. //
  32009. case 0 : {
  32010. const int
  32011. xc = (int)(centering_x*((int)sx - width())),
  32012. yc = (int)(centering_y*((int)sy - height())),
  32013. zc = (int)(centering_z*((int)sz - depth())),
  32014. cc = (int)(centering_c*((int)sc - spectrum()));
  32015. switch (boundary_conditions) {
  32016. case 3 : { // Mirror
  32017. res.assign(sx,sy,sz,sc);
  32018. const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth(), s2 = 2*spectrum();
  32019. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1024*1024))
  32020. cimg_forXYZC(res,x,y,z,c) {
  32021. const int
  32022. mx = cimg::mod(x - xc,w2), my = cimg::mod(y - yc,h2),
  32023. mz = cimg::mod(z - zc,d2), mc = cimg::mod(c - cc,s2);
  32024. res(x,y,z,c) = (*this)(mx<width()?mx:w2 - mx - 1,
  32025. my<height()?my:h2 - my - 1,
  32026. mz<depth()?mz:d2 - mz - 1,
  32027. mc<spectrum()?mc:s2 - mc - 1);
  32028. }
  32029. } break;
  32030. case 2 : { // Periodic
  32031. res.assign(sx,sy,sz,sc);
  32032. const int
  32033. x0 = ((int)xc%width()) - width(),
  32034. y0 = ((int)yc%height()) - height(),
  32035. z0 = ((int)zc%depth()) - depth(),
  32036. c0 = ((int)cc%spectrum()) - spectrum(),
  32037. dx = width(), dy = height(), dz = depth(), dc = spectrum();
  32038. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1024*1024))
  32039. for (int c = c0; c<(int)sc; c+=dc)
  32040. for (int z = z0; z<(int)sz; z+=dz)
  32041. for (int y = y0; y<(int)sy; y+=dy)
  32042. for (int x = x0; x<(int)sx; x+=dx)
  32043. res.draw_image(x,y,z,c,*this);
  32044. } break;
  32045. case 1 : { // Neumann
  32046. res.assign(sx,sy,sz,sc).draw_image(xc,yc,zc,cc,*this);
  32047. CImg<T> sprite;
  32048. if (xc>0) { // X-backward
  32049. res.get_crop(xc,yc,zc,cc,xc,yc + height() - 1,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
  32050. for (int x = xc - 1; x>=0; --x) res.draw_image(x,yc,zc,cc,sprite);
  32051. }
  32052. if (xc + width()<(int)sx) { // X-forward
  32053. res.get_crop(xc + width() - 1,yc,zc,cc,xc + width() - 1,yc + height() - 1,
  32054. zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
  32055. for (int x = xc + width(); x<(int)sx; ++x) res.draw_image(x,yc,zc,cc,sprite);
  32056. }
  32057. if (yc>0) { // Y-backward
  32058. res.get_crop(0,yc,zc,cc,sx - 1,yc,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
  32059. for (int y = yc - 1; y>=0; --y) res.draw_image(0,y,zc,cc,sprite);
  32060. }
  32061. if (yc + height()<(int)sy) { // Y-forward
  32062. res.get_crop(0,yc + height() - 1,zc,cc,sx - 1,yc + height() - 1,
  32063. zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
  32064. for (int y = yc + height(); y<(int)sy; ++y) res.draw_image(0,y,zc,cc,sprite);
  32065. }
  32066. if (zc>0) { // Z-backward
  32067. res.get_crop(0,0,zc,cc,sx - 1,sy - 1,zc,cc + spectrum() - 1).move_to(sprite);
  32068. for (int z = zc - 1; z>=0; --z) res.draw_image(0,0,z,cc,sprite);
  32069. }
  32070. if (zc + depth()<(int)sz) { // Z-forward
  32071. res.get_crop(0,0,zc +depth() - 1,cc,sx - 1,sy - 1,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
  32072. for (int z = zc + depth(); z<(int)sz; ++z) res.draw_image(0,0,z,cc,sprite);
  32073. }
  32074. if (cc>0) { // C-backward
  32075. res.get_crop(0,0,0,cc,sx - 1,sy - 1,sz - 1,cc).move_to(sprite);
  32076. for (int c = cc - 1; c>=0; --c) res.draw_image(0,0,0,c,sprite);
  32077. }
  32078. if (cc + spectrum()<(int)sc) { // C-forward
  32079. res.get_crop(0,0,0,cc + spectrum() - 1,sx - 1,sy - 1,sz - 1,cc + spectrum() - 1).move_to(sprite);
  32080. for (int c = cc + spectrum(); c<(int)sc; ++c) res.draw_image(0,0,0,c,sprite);
  32081. }
  32082. } break;
  32083. default : // Dirichlet
  32084. res.assign(sx,sy,sz,sc,(T)0).draw_image(xc,yc,zc,cc,*this);
  32085. }
  32086. break;
  32087. } break;
  32088. // Nearest neighbor interpolation.
  32089. //
  32090. case 1 : {
  32091. res.assign(sx,sy,sz,sc);
  32092. CImg<ulongT> off_x(sx), off_y(sy + 1), off_z(sz + 1), off_c(sc + 1);
  32093. const ulongT
  32094. wh = (ulongT)_width*_height,
  32095. whd = (ulongT)_width*_height*_depth,
  32096. sxy = (ulongT)sx*sy,
  32097. sxyz = (ulongT)sx*sy*sz,
  32098. one = (ulongT)1;
  32099. if (sx==_width) off_x.fill(1);
  32100. else {
  32101. ulongT *poff_x = off_x._data, curr = 0;
  32102. cimg_forX(res,x) {
  32103. const ulongT old = curr;
  32104. curr = (x + one)*_width/sx;
  32105. *(poff_x++) = curr - old;
  32106. }
  32107. }
  32108. if (sy==_height) off_y.fill(_width);
  32109. else {
  32110. ulongT *poff_y = off_y._data, curr = 0;
  32111. cimg_forY(res,y) {
  32112. const ulongT old = curr;
  32113. curr = (y + one)*_height/sy;
  32114. *(poff_y++) = _width*(curr - old);
  32115. }
  32116. *poff_y = 0;
  32117. }
  32118. if (sz==_depth) off_z.fill(wh);
  32119. else {
  32120. ulongT *poff_z = off_z._data, curr = 0;
  32121. cimg_forZ(res,z) {
  32122. const ulongT old = curr;
  32123. curr = (z + one)*_depth/sz;
  32124. *(poff_z++) = wh*(curr - old);
  32125. }
  32126. *poff_z = 0;
  32127. }
  32128. if (sc==_spectrum) off_c.fill(whd);
  32129. else {
  32130. ulongT *poff_c = off_c._data, curr = 0;
  32131. cimg_forC(res,c) {
  32132. const ulongT old = curr;
  32133. curr = (c + one)*_spectrum/sc;
  32134. *(poff_c++) = whd*(curr - old);
  32135. }
  32136. *poff_c = 0;
  32137. }
  32138. T *ptrd = res._data;
  32139. const T* ptrc = _data;
  32140. const ulongT *poff_c = off_c._data;
  32141. for (unsigned int c = 0; c<sc; ) {
  32142. const T *ptrz = ptrc;
  32143. const ulongT *poff_z = off_z._data;
  32144. for (unsigned int z = 0; z<sz; ) {
  32145. const T *ptry = ptrz;
  32146. const ulongT *poff_y = off_y._data;
  32147. for (unsigned int y = 0; y<sy; ) {
  32148. const T *ptrx = ptry;
  32149. const ulongT *poff_x = off_x._data;
  32150. cimg_forX(res,x) { *(ptrd++) = *ptrx; ptrx+=*(poff_x++); }
  32151. ++y;
  32152. ulongT dy = *(poff_y++);
  32153. for ( ; !dy && y<dy; std::memcpy(ptrd,ptrd - sx,sizeof(T)*sx), ++y, ptrd+=sx, dy = *(poff_y++)) {}
  32154. ptry+=dy;
  32155. }
  32156. ++z;
  32157. ulongT dz = *(poff_z++);
  32158. for ( ; !dz && z<dz; std::memcpy(ptrd,ptrd - sxy,sizeof(T)*sxy), ++z, ptrd+=sxy, dz = *(poff_z++)) {}
  32159. ptrz+=dz;
  32160. }
  32161. ++c;
  32162. ulongT dc = *(poff_c++);
  32163. for ( ; !dc && c<dc; std::memcpy(ptrd,ptrd - sxyz,sizeof(T)*sxyz), ++c, ptrd+=sxyz, dc = *(poff_c++)) {}
  32164. ptrc+=dc;
  32165. }
  32166. } break;
  32167. // Moving average.
  32168. //
  32169. case 2 : {
  32170. bool instance_first = true;
  32171. if (sx!=_width) {
  32172. if (sx>_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(res);
  32173. else {
  32174. CImg<Tfloat> tmp(sx,_height,_depth,_spectrum,0);
  32175. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32176. cimg_openmp_if(sx>=256 && _height*_depth*_spectrum>=256))
  32177. cimg_forYZC(tmp,y,z,v) {
  32178. for (unsigned int a = _width*sx, b = _width, c = sx, s = 0, t = 0; a; ) {
  32179. const unsigned int d = std::min(b,c);
  32180. a-=d; b-=d; c-=d;
  32181. tmp(t,y,z,v)+=(Tfloat)(*this)(s,y,z,v)*d;
  32182. if (!b) { tmp(t++,y,z,v)/=_width; b = _width; }
  32183. if (!c) { ++s; c = sx; }
  32184. }
  32185. }
  32186. tmp.move_to(res);
  32187. }
  32188. instance_first = false;
  32189. }
  32190. if (sy!=_height) {
  32191. if (sy>_height) get_resize(sx,sy,_depth,_spectrum,1).move_to(res);
  32192. else {
  32193. CImg<Tfloat> tmp(sx,sy,_depth,_spectrum,0);
  32194. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32195. cimg_openmp_if(sy>=256 && _width*_depth*_spectrum>=256))
  32196. cimg_forXZC(tmp,x,z,v) {
  32197. for (unsigned int a = _height*sy, b = _height, c = sy, s = 0, t = 0; a; ) {
  32198. const unsigned int d = std::min(b,c);
  32199. a-=d; b-=d; c-=d;
  32200. if (instance_first) tmp(x,t,z,v)+=(Tfloat)(*this)(x,s,z,v)*d;
  32201. else tmp(x,t,z,v)+=(Tfloat)res(x,s,z,v)*d;
  32202. if (!b) { tmp(x,t++,z,v)/=_height; b = _height; }
  32203. if (!c) { ++s; c = sy; }
  32204. }
  32205. }
  32206. tmp.move_to(res);
  32207. }
  32208. instance_first = false;
  32209. }
  32210. if (sz!=_depth) {
  32211. if (sz>_depth) get_resize(sx,sy,sz,_spectrum,1).move_to(res);
  32212. else {
  32213. CImg<Tfloat> tmp(sx,sy,sz,_spectrum,0);
  32214. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32215. cimg_openmp_if(sz>=256 && _width*_height*_spectrum>=256))
  32216. cimg_forXYC(tmp,x,y,v) {
  32217. for (unsigned int a = _depth*sz, b = _depth, c = sz, s = 0, t = 0; a; ) {
  32218. const unsigned int d = std::min(b,c);
  32219. a-=d; b-=d; c-=d;
  32220. if (instance_first) tmp(x,y,t,v)+=(Tfloat)(*this)(x,y,s,v)*d;
  32221. else tmp(x,y,t,v)+=(Tfloat)res(x,y,s,v)*d;
  32222. if (!b) { tmp(x,y,t++,v)/=_depth; b = _depth; }
  32223. if (!c) { ++s; c = sz; }
  32224. }
  32225. }
  32226. tmp.move_to(res);
  32227. }
  32228. instance_first = false;
  32229. }
  32230. if (sc!=_spectrum) {
  32231. if (sc>_spectrum) get_resize(sx,sy,sz,sc,1).move_to(res);
  32232. else {
  32233. CImg<Tfloat> tmp(sx,sy,sz,sc,0);
  32234. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32235. cimg_openmp_if(sc>=256 && _width*_height*_depth>=256))
  32236. cimg_forXYZ(tmp,x,y,z) {
  32237. for (unsigned int a = _spectrum*sc, b = _spectrum, c = sc, s = 0, t = 0; a; ) {
  32238. const unsigned int d = std::min(b,c);
  32239. a-=d; b-=d; c-=d;
  32240. if (instance_first) tmp(x,y,z,t)+=(Tfloat)(*this)(x,y,z,s)*d;
  32241. else tmp(x,y,z,t)+=(Tfloat)res(x,y,z,s)*d;
  32242. if (!b) { tmp(x,y,z,t++)/=_spectrum; b = _spectrum; }
  32243. if (!c) { ++s; c = sc; }
  32244. }
  32245. }
  32246. tmp.move_to(res);
  32247. }
  32248. instance_first = false;
  32249. }
  32250. } break;
  32251. // Linear interpolation.
  32252. //
  32253. case 3 : {
  32254. CImg<uintT> off(cimg::max(sx,sy,sz,sc));
  32255. CImg<doubleT> foff(off._width);
  32256. CImg<T> resx, resy, resz, resc;
  32257. double curr, old;
  32258. if (sx!=_width) {
  32259. if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
  32260. else if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
  32261. else {
  32262. const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
  32263. (double)_width/sx;
  32264. resx.assign(sx,_height,_depth,_spectrum);
  32265. curr = old = 0;
  32266. {
  32267. unsigned int *poff = off._data;
  32268. double *pfoff = foff._data;
  32269. cimg_forX(resx,x) {
  32270. *(pfoff++) = curr - (unsigned int)curr;
  32271. old = curr;
  32272. curr = std::min(width() - 1.,curr + fx);
  32273. *(poff++) = (unsigned int)curr - (unsigned int)old;
  32274. }
  32275. }
  32276. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32277. cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
  32278. cimg_forYZC(resx,y,z,c) {
  32279. const T *ptrs = data(0,y,z,c), *const ptrsmax = ptrs + _width - 1;
  32280. T *ptrd = resx.data(0,y,z,c);
  32281. const unsigned int *poff = off._data;
  32282. const double *pfoff = foff._data;
  32283. cimg_forX(resx,x) {
  32284. const double alpha = *(pfoff++);
  32285. const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + 1):val1;
  32286. *(ptrd++) = (T)((1 - alpha)*val1 + alpha*val2);
  32287. ptrs+=*(poff++);
  32288. }
  32289. }
  32290. }
  32291. } else resx.assign(*this,true);
  32292. if (sy!=_height) {
  32293. if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
  32294. else {
  32295. if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
  32296. else {
  32297. const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
  32298. (double)_height/sy;
  32299. resy.assign(sx,sy,_depth,_spectrum);
  32300. curr = old = 0;
  32301. {
  32302. unsigned int *poff = off._data;
  32303. double *pfoff = foff._data;
  32304. cimg_forY(resy,y) {
  32305. *(pfoff++) = curr - (unsigned int)curr;
  32306. old = curr;
  32307. curr = std::min(height() - 1.,curr + fy);
  32308. *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
  32309. }
  32310. }
  32311. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32312. cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
  32313. cimg_forXZC(resy,x,z,c) {
  32314. const T *ptrs = resx.data(x,0,z,c), *const ptrsmax = ptrs + (_height - 1)*sx;
  32315. T *ptrd = resy.data(x,0,z,c);
  32316. const unsigned int *poff = off._data;
  32317. const double *pfoff = foff._data;
  32318. cimg_forY(resy,y) {
  32319. const double alpha = *(pfoff++);
  32320. const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sx):val1;
  32321. *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
  32322. ptrd+=sx;
  32323. ptrs+=*(poff++);
  32324. }
  32325. }
  32326. }
  32327. }
  32328. resx.assign();
  32329. } else resy.assign(resx,true);
  32330. if (sz!=_depth) {
  32331. if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
  32332. else {
  32333. if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
  32334. else {
  32335. const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
  32336. (double)_depth/sz;
  32337. const unsigned int sxy = sx*sy;
  32338. resz.assign(sx,sy,sz,_spectrum);
  32339. curr = old = 0;
  32340. {
  32341. unsigned int *poff = off._data;
  32342. double *pfoff = foff._data;
  32343. cimg_forZ(resz,z) {
  32344. *(pfoff++) = curr - (unsigned int)curr;
  32345. old = curr;
  32346. curr = std::min(depth() - 1.,curr + fz);
  32347. *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
  32348. }
  32349. }
  32350. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32351. cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
  32352. cimg_forXYC(resz,x,y,c) {
  32353. const T *ptrs = resy.data(x,y,0,c), *const ptrsmax = ptrs + (_depth - 1)*sxy;
  32354. T *ptrd = resz.data(x,y,0,c);
  32355. const unsigned int *poff = off._data;
  32356. const double *pfoff = foff._data;
  32357. cimg_forZ(resz,z) {
  32358. const double alpha = *(pfoff++);
  32359. const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sxy):val1;
  32360. *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
  32361. ptrd+=sxy;
  32362. ptrs+=*(poff++);
  32363. }
  32364. }
  32365. }
  32366. }
  32367. resy.assign();
  32368. } else resz.assign(resy,true);
  32369. if (sc!=_spectrum) {
  32370. if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
  32371. else {
  32372. if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
  32373. else {
  32374. const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
  32375. (double)_spectrum/sc;
  32376. const unsigned int sxyz = sx*sy*sz;
  32377. resc.assign(sx,sy,sz,sc);
  32378. curr = old = 0;
  32379. {
  32380. unsigned int *poff = off._data;
  32381. double *pfoff = foff._data;
  32382. cimg_forC(resc,c) {
  32383. *(pfoff++) = curr - (unsigned int)curr;
  32384. old = curr;
  32385. curr = std::min(spectrum() - 1.,curr + fc);
  32386. *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
  32387. }
  32388. }
  32389. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32390. cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
  32391. cimg_forXYZ(resc,x,y,z) {
  32392. const T *ptrs = resz.data(x,y,z,0), *const ptrsmax = ptrs + (_spectrum - 1)*sxyz;
  32393. T *ptrd = resc.data(x,y,z,0);
  32394. const unsigned int *poff = off._data;
  32395. const double *pfoff = foff._data;
  32396. cimg_forC(resc,c) {
  32397. const double alpha = *(pfoff++);
  32398. const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sxyz):val1;
  32399. *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
  32400. ptrd+=sxyz;
  32401. ptrs+=*(poff++);
  32402. }
  32403. }
  32404. }
  32405. }
  32406. resz.assign();
  32407. } else resc.assign(resz,true);
  32408. return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
  32409. } break;
  32410. // Grid interpolation.
  32411. //
  32412. case 4 : {
  32413. CImg<T> resx, resy, resz, resc;
  32414. if (sx!=_width) {
  32415. if (sx<_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
  32416. else {
  32417. resx.assign(sx,_height,_depth,_spectrum,(T)0);
  32418. const int dx = (int)(2*sx), dy = 2*width();
  32419. int err = (int)(dy + centering_x*(sx*dy/width() - dy)), xs = 0;
  32420. cimg_forX(resx,x) if ((err-=dy)<=0) {
  32421. cimg_forYZC(resx,y,z,c) resx(x,y,z,c) = (*this)(xs,y,z,c);
  32422. ++xs;
  32423. err+=dx;
  32424. }
  32425. }
  32426. } else resx.assign(*this,true);
  32427. if (sy!=_height) {
  32428. if (sy<_height) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
  32429. else {
  32430. resy.assign(sx,sy,_depth,_spectrum,(T)0);
  32431. const int dx = (int)(2*sy), dy = 2*height();
  32432. int err = (int)(dy + centering_y*(sy*dy/height() - dy)), ys = 0;
  32433. cimg_forY(resy,y) if ((err-=dy)<=0) {
  32434. cimg_forXZC(resy,x,z,c) resy(x,y,z,c) = resx(x,ys,z,c);
  32435. ++ys;
  32436. err+=dx;
  32437. }
  32438. }
  32439. resx.assign();
  32440. } else resy.assign(resx,true);
  32441. if (sz!=_depth) {
  32442. if (sz<_depth) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
  32443. else {
  32444. resz.assign(sx,sy,sz,_spectrum,(T)0);
  32445. const int dx = (int)(2*sz), dy = 2*depth();
  32446. int err = (int)(dy + centering_z*(sz*dy/depth() - dy)), zs = 0;
  32447. cimg_forZ(resz,z) if ((err-=dy)<=0) {
  32448. cimg_forXYC(resz,x,y,c) resz(x,y,z,c) = resy(x,y,zs,c);
  32449. ++zs;
  32450. err+=dx;
  32451. }
  32452. }
  32453. resy.assign();
  32454. } else resz.assign(resy,true);
  32455. if (sc!=_spectrum) {
  32456. if (sc<_spectrum) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
  32457. else {
  32458. resc.assign(sx,sy,sz,sc,(T)0);
  32459. const int dx = (int)(2*sc), dy = 2*spectrum();
  32460. int err = (int)(dy + centering_c*(sc*dy/spectrum() - dy)), cs = 0;
  32461. cimg_forC(resc,c) if ((err-=dy)<=0) {
  32462. cimg_forXYZ(resc,x,y,z) resc(x,y,z,c) = resz(x,y,z,cs);
  32463. ++cs;
  32464. err+=dx;
  32465. }
  32466. }
  32467. resz.assign();
  32468. } else resc.assign(resz,true);
  32469. return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
  32470. } break;
  32471. // Cubic interpolation.
  32472. //
  32473. case 5 : {
  32474. const Tfloat vmin = (Tfloat)cimg::type<T>::min(), vmax = (Tfloat)cimg::type<T>::max();
  32475. CImg<uintT> off(cimg::max(sx,sy,sz,sc));
  32476. CImg<doubleT> foff(off._width);
  32477. CImg<T> resx, resy, resz, resc;
  32478. double curr, old;
  32479. if (sx!=_width) {
  32480. if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
  32481. else {
  32482. if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
  32483. else {
  32484. const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
  32485. (double)_width/sx;
  32486. resx.assign(sx,_height,_depth,_spectrum);
  32487. curr = old = 0;
  32488. {
  32489. unsigned int *poff = off._data;
  32490. double *pfoff = foff._data;
  32491. cimg_forX(resx,x) {
  32492. *(pfoff++) = curr - (unsigned int)curr;
  32493. old = curr;
  32494. curr = std::min(width() - 1.,curr + fx);
  32495. *(poff++) = (unsigned int)curr - (unsigned int)old;
  32496. }
  32497. }
  32498. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32499. cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
  32500. cimg_forYZC(resx,y,z,c) {
  32501. const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_width - 2);
  32502. T *ptrd = resx.data(0,y,z,c);
  32503. const unsigned int *poff = off._data;
  32504. const double *pfoff = foff._data;
  32505. cimg_forX(resx,x) {
  32506. const double
  32507. t = *(pfoff++),
  32508. val1 = (double)*ptrs,
  32509. val0 = ptrs>ptrs0?(double)*(ptrs - 1):val1,
  32510. val2 = ptrs<=ptrsmax?(double)*(ptrs + 1):val1,
  32511. val3 = ptrs<ptrsmax?(double)*(ptrs + 2):val2,
  32512. val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
  32513. t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
  32514. *(ptrd++) = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32515. ptrs+=*(poff++);
  32516. }
  32517. }
  32518. }
  32519. }
  32520. } else resx.assign(*this,true);
  32521. if (sy!=_height) {
  32522. if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
  32523. else {
  32524. if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
  32525. else {
  32526. const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
  32527. (double)_height/sy;
  32528. resy.assign(sx,sy,_depth,_spectrum);
  32529. curr = old = 0;
  32530. {
  32531. unsigned int *poff = off._data;
  32532. double *pfoff = foff._data;
  32533. cimg_forY(resy,y) {
  32534. *(pfoff++) = curr - (unsigned int)curr;
  32535. old = curr;
  32536. curr = std::min(height() - 1.,curr + fy);
  32537. *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
  32538. }
  32539. }
  32540. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32541. cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
  32542. cimg_forXZC(resy,x,z,c) {
  32543. const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_height - 2)*sx;
  32544. T *ptrd = resy.data(x,0,z,c);
  32545. const unsigned int *poff = off._data;
  32546. const double *pfoff = foff._data;
  32547. cimg_forY(resy,y) {
  32548. const double
  32549. t = *(pfoff++),
  32550. val1 = (double)*ptrs,
  32551. val0 = ptrs>ptrs0?(double)*(ptrs - sx):val1,
  32552. val2 = ptrs<=ptrsmax?(double)*(ptrs + sx):val1,
  32553. val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sx):val2,
  32554. val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
  32555. t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
  32556. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32557. ptrd+=sx;
  32558. ptrs+=*(poff++);
  32559. }
  32560. }
  32561. }
  32562. }
  32563. resx.assign();
  32564. } else resy.assign(resx,true);
  32565. if (sz!=_depth) {
  32566. if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
  32567. else {
  32568. if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
  32569. else {
  32570. const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
  32571. (double)_depth/sz;
  32572. const unsigned int sxy = sx*sy;
  32573. resz.assign(sx,sy,sz,_spectrum);
  32574. curr = old = 0;
  32575. {
  32576. unsigned int *poff = off._data;
  32577. double *pfoff = foff._data;
  32578. cimg_forZ(resz,z) {
  32579. *(pfoff++) = curr - (unsigned int)curr;
  32580. old = curr;
  32581. curr = std::min(depth() - 1.,curr + fz);
  32582. *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
  32583. }
  32584. }
  32585. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32586. cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
  32587. cimg_forXYC(resz,x,y,c) {
  32588. const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_depth - 2)*sxy;
  32589. T *ptrd = resz.data(x,y,0,c);
  32590. const unsigned int *poff = off._data;
  32591. const double *pfoff = foff._data;
  32592. cimg_forZ(resz,z) {
  32593. const double
  32594. t = *(pfoff++),
  32595. val1 = (double)*ptrs,
  32596. val0 = ptrs>ptrs0?(double)*(ptrs - sxy):val1,
  32597. val2 = ptrs<=ptrsmax?(double)*(ptrs + sxy):val1,
  32598. val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sxy):val2,
  32599. val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
  32600. t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
  32601. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32602. ptrd+=sxy;
  32603. ptrs+=*(poff++);
  32604. }
  32605. }
  32606. }
  32607. }
  32608. resy.assign();
  32609. } else resz.assign(resy,true);
  32610. if (sc!=_spectrum) {
  32611. if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
  32612. else {
  32613. if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
  32614. else {
  32615. const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
  32616. (double)_spectrum/sc;
  32617. const unsigned int sxyz = sx*sy*sz;
  32618. resc.assign(sx,sy,sz,sc);
  32619. curr = old = 0;
  32620. {
  32621. unsigned int *poff = off._data;
  32622. double *pfoff = foff._data;
  32623. cimg_forC(resc,c) {
  32624. *(pfoff++) = curr - (unsigned int)curr;
  32625. old = curr;
  32626. curr = std::min(spectrum() - 1.,curr + fc);
  32627. *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
  32628. }
  32629. }
  32630. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32631. cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
  32632. cimg_forXYZ(resc,x,y,z) {
  32633. const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmax = ptrs + (_spectrum - 2)*sxyz;
  32634. T *ptrd = resc.data(x,y,z,0);
  32635. const unsigned int *poff = off._data;
  32636. const double *pfoff = foff._data;
  32637. cimg_forC(resc,c) {
  32638. const double
  32639. t = *(pfoff++),
  32640. val1 = (double)*ptrs,
  32641. val0 = ptrs>ptrs0?(double)*(ptrs - sxyz):val1,
  32642. val2 = ptrs<=ptrsmax?(double)*(ptrs + sxyz):val1,
  32643. val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sxyz):val2,
  32644. val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
  32645. t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
  32646. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32647. ptrd+=sxyz;
  32648. ptrs+=*(poff++);
  32649. }
  32650. }
  32651. }
  32652. }
  32653. resz.assign();
  32654. } else resc.assign(resz,true);
  32655. return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
  32656. } break;
  32657. // Lanczos interpolation.
  32658. //
  32659. case 6 : {
  32660. const double vmin = (double)cimg::type<T>::min(), vmax = (double)cimg::type<T>::max();
  32661. CImg<uintT> off(cimg::max(sx,sy,sz,sc));
  32662. CImg<doubleT> foff(off._width);
  32663. CImg<T> resx, resy, resz, resc;
  32664. double curr, old;
  32665. if (sx!=_width) {
  32666. if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
  32667. else {
  32668. if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
  32669. else {
  32670. const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
  32671. (double)_width/sx;
  32672. resx.assign(sx,_height,_depth,_spectrum);
  32673. curr = old = 0;
  32674. {
  32675. unsigned int *poff = off._data;
  32676. double *pfoff = foff._data;
  32677. cimg_forX(resx,x) {
  32678. *(pfoff++) = curr - (unsigned int)curr;
  32679. old = curr;
  32680. curr = std::min(width() - 1.,curr + fx);
  32681. *(poff++) = (unsigned int)curr - (unsigned int)old;
  32682. }
  32683. }
  32684. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32685. cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
  32686. cimg_forYZC(resx,y,z,c) {
  32687. const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + 1,
  32688. *const ptrsmax = ptrs0 + (_width - 2);
  32689. T *ptrd = resx.data(0,y,z,c);
  32690. const unsigned int *poff = off._data;
  32691. const double *pfoff = foff._data;
  32692. cimg_forX(resx,x) {
  32693. const double
  32694. t = *(pfoff++),
  32695. w0 = _cimg_lanczos(t + 2),
  32696. w1 = _cimg_lanczos(t + 1),
  32697. w2 = _cimg_lanczos(t),
  32698. w3 = _cimg_lanczos(t - 1),
  32699. w4 = _cimg_lanczos(t - 2),
  32700. val2 = (double)*ptrs,
  32701. val1 = ptrs>=ptrsmin?(double)*(ptrs - 1):val2,
  32702. val0 = ptrs>ptrsmin?(double)*(ptrs - 2):val1,
  32703. val3 = ptrs<=ptrsmax?(double)*(ptrs + 1):val2,
  32704. val4 = ptrs<ptrsmax?(double)*(ptrs + 2):val3,
  32705. val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
  32706. *(ptrd++) = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32707. ptrs+=*(poff++);
  32708. }
  32709. }
  32710. }
  32711. }
  32712. } else resx.assign(*this,true);
  32713. if (sy!=_height) {
  32714. if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
  32715. else {
  32716. if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
  32717. else {
  32718. const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
  32719. (double)_height/sy;
  32720. resy.assign(sx,sy,_depth,_spectrum);
  32721. curr = old = 0;
  32722. {
  32723. unsigned int *poff = off._data;
  32724. double *pfoff = foff._data;
  32725. cimg_forY(resy,y) {
  32726. *(pfoff++) = curr - (unsigned int)curr;
  32727. old = curr;
  32728. curr = std::min(height() - 1.,curr + fy);
  32729. *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
  32730. }
  32731. }
  32732. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32733. cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
  32734. cimg_forXZC(resy,x,z,c) {
  32735. const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sx,
  32736. *const ptrsmax = ptrs0 + (_height - 2)*sx;
  32737. T *ptrd = resy.data(x,0,z,c);
  32738. const unsigned int *poff = off._data;
  32739. const double *pfoff = foff._data;
  32740. cimg_forY(resy,y) {
  32741. const double
  32742. t = *(pfoff++),
  32743. w0 = _cimg_lanczos(t + 2),
  32744. w1 = _cimg_lanczos(t + 1),
  32745. w2 = _cimg_lanczos(t),
  32746. w3 = _cimg_lanczos(t - 1),
  32747. w4 = _cimg_lanczos(t - 2),
  32748. val2 = (double)*ptrs,
  32749. val1 = ptrs>=ptrsmin?(double)*(ptrs - sx):val2,
  32750. val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sx):val1,
  32751. val3 = ptrs<=ptrsmax?(double)*(ptrs + sx):val2,
  32752. val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sx):val3,
  32753. val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
  32754. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32755. ptrd+=sx;
  32756. ptrs+=*(poff++);
  32757. }
  32758. }
  32759. }
  32760. }
  32761. resx.assign();
  32762. } else resy.assign(resx,true);
  32763. if (sz!=_depth) {
  32764. if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
  32765. else {
  32766. if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
  32767. else {
  32768. const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
  32769. (double)_depth/sz;
  32770. const unsigned int sxy = sx*sy;
  32771. resz.assign(sx,sy,sz,_spectrum);
  32772. curr = old = 0;
  32773. {
  32774. unsigned int *poff = off._data;
  32775. double *pfoff = foff._data;
  32776. cimg_forZ(resz,z) {
  32777. *(pfoff++) = curr - (unsigned int)curr;
  32778. old = curr;
  32779. curr = std::min(depth() - 1.,curr + fz);
  32780. *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
  32781. }
  32782. }
  32783. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32784. cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
  32785. cimg_forXYC(resz,x,y,c) {
  32786. const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxy,
  32787. *const ptrsmax = ptrs0 + (_depth - 2)*sxy;
  32788. T *ptrd = resz.data(x,y,0,c);
  32789. const unsigned int *poff = off._data;
  32790. const double *pfoff = foff._data;
  32791. cimg_forZ(resz,z) {
  32792. const double
  32793. t = *(pfoff++),
  32794. w0 = _cimg_lanczos(t + 2),
  32795. w1 = _cimg_lanczos(t + 1),
  32796. w2 = _cimg_lanczos(t),
  32797. w3 = _cimg_lanczos(t - 1),
  32798. w4 = _cimg_lanczos(t - 2),
  32799. val2 = (double)*ptrs,
  32800. val1 = ptrs>=ptrsmin?(double)*(ptrs - sxy):val2,
  32801. val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sxy):val1,
  32802. val3 = ptrs<=ptrsmax?(double)*(ptrs + sxy):val2,
  32803. val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sxy):val3,
  32804. val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
  32805. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32806. ptrd+=sxy;
  32807. ptrs+=*(poff++);
  32808. }
  32809. }
  32810. }
  32811. }
  32812. resy.assign();
  32813. } else resz.assign(resy,true);
  32814. if (sc!=_spectrum) {
  32815. if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
  32816. else {
  32817. if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
  32818. else {
  32819. const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
  32820. (double)_spectrum/sc;
  32821. const unsigned int sxyz = sx*sy*sz;
  32822. resc.assign(sx,sy,sz,sc);
  32823. curr = old = 0;
  32824. {
  32825. unsigned int *poff = off._data;
  32826. double *pfoff = foff._data;
  32827. cimg_forC(resc,c) {
  32828. *(pfoff++) = curr - (unsigned int)curr;
  32829. old = curr;
  32830. curr = std::min(spectrum() - 1.,curr + fc);
  32831. *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
  32832. }
  32833. }
  32834. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  32835. cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
  32836. cimg_forXYZ(resc,x,y,z) {
  32837. const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxyz,
  32838. *const ptrsmax = ptrs + (_spectrum - 2)*sxyz;
  32839. T *ptrd = resc.data(x,y,z,0);
  32840. const unsigned int *poff = off._data;
  32841. const double *pfoff = foff._data;
  32842. cimg_forC(resc,c) {
  32843. const double
  32844. t = *(pfoff++),
  32845. w0 = _cimg_lanczos(t + 2),
  32846. w1 = _cimg_lanczos(t + 1),
  32847. w2 = _cimg_lanczos(t),
  32848. w3 = _cimg_lanczos(t - 1),
  32849. w4 = _cimg_lanczos(t - 2),
  32850. val2 = (double)*ptrs,
  32851. val1 = ptrs>=ptrsmin?(double)*(ptrs - sxyz):val2,
  32852. val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sxyz):val1,
  32853. val3 = ptrs<=ptrsmax?(double)*(ptrs + sxyz):val2,
  32854. val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sxyz):val3,
  32855. val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
  32856. *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
  32857. ptrd+=sxyz;
  32858. ptrs+=*(poff++);
  32859. }
  32860. }
  32861. }
  32862. }
  32863. resz.assign();
  32864. } else resc.assign(resz,true);
  32865. return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
  32866. } break;
  32867. // Unknown interpolation.
  32868. //
  32869. default :
  32870. throw CImgArgumentException(_cimg_instance
  32871. "resize(): Invalid specified interpolation %d "
  32872. "(should be { -1=raw | 0=none | 1=nearest | 2=average | 3=linear | 4=grid | "
  32873. "5=cubic | 6=lanczos }).",
  32874. cimg_instance,
  32875. interpolation_type);
  32876. }
  32877. return res;
  32878. }
  32879. //! Resize image to dimensions of another image.
  32880. /**
  32881. \param src Reference image used for dimensions.
  32882. \param interpolation_type Interpolation method.
  32883. \param boundary_conditions Boundary conditions.
  32884. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  32885. \param centering_x Set centering type (only if \p interpolation_type=0).
  32886. \param centering_y Set centering type (only if \p interpolation_type=0).
  32887. \param centering_z Set centering type (only if \p interpolation_type=0).
  32888. \param centering_c Set centering type (only if \p interpolation_type=0).
  32889. **/
  32890. template<typename t>
  32891. CImg<T>& resize(const CImg<t>& src,
  32892. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  32893. const float centering_x = 0, const float centering_y = 0,
  32894. const float centering_z = 0, const float centering_c = 0) {
  32895. return resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions,
  32896. centering_x,centering_y,centering_z,centering_c);
  32897. }
  32898. //! Resize image to dimensions of another image \newinstance.
  32899. template<typename t>
  32900. CImg<T> get_resize(const CImg<t>& src,
  32901. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  32902. const float centering_x = 0, const float centering_y = 0,
  32903. const float centering_z = 0, const float centering_c = 0) const {
  32904. return get_resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions,
  32905. centering_x,centering_y,centering_z,centering_c);
  32906. }
  32907. //! Resize image to dimensions of a display window.
  32908. /**
  32909. \param disp Reference display window used for dimensions.
  32910. \param interpolation_type Interpolation method.
  32911. \param boundary_conditions Boundary conditions.
  32912. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  32913. \param centering_x Set centering type (only if \p interpolation_type=0).
  32914. \param centering_y Set centering type (only if \p interpolation_type=0).
  32915. \param centering_z Set centering type (only if \p interpolation_type=0).
  32916. \param centering_c Set centering type (only if \p interpolation_type=0).
  32917. **/
  32918. CImg<T>& resize(const CImgDisplay& disp,
  32919. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  32920. const float centering_x = 0, const float centering_y = 0,
  32921. const float centering_z = 0, const float centering_c = 0) {
  32922. return resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions,
  32923. centering_x,centering_y,centering_z,centering_c);
  32924. }
  32925. //! Resize image to dimensions of a display window \newinstance.
  32926. CImg<T> get_resize(const CImgDisplay& disp,
  32927. const int interpolation_type=1, const unsigned int boundary_conditions=0,
  32928. const float centering_x = 0, const float centering_y = 0,
  32929. const float centering_z = 0, const float centering_c = 0) const {
  32930. return get_resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions,
  32931. centering_x,centering_y,centering_z,centering_c);
  32932. }
  32933. //! Resize image to half-size along XY axes, using an optimized filter.
  32934. CImg<T>& resize_halfXY() {
  32935. return get_resize_halfXY().move_to(*this);
  32936. }
  32937. //! Resize image to half-size along XY axes, using an optimized filter \newinstance.
  32938. CImg<T> get_resize_halfXY() const {
  32939. if (is_empty()) return *this;
  32940. static const Tfloat kernel[9] = { 0.07842776544f, 0.1231940459f, 0.07842776544f,
  32941. 0.1231940459f, 0.1935127547f, 0.1231940459f,
  32942. 0.07842776544f, 0.1231940459f, 0.07842776544f };
  32943. CImg<T> I(9), res(_width/2,_height/2,_depth,_spectrum);
  32944. T *ptrd = res._data;
  32945. cimg_forZC(*this,z,c) cimg_for3x3(*this,x,y,z,c,I,T)
  32946. if (x%2 && y%2) *(ptrd++) = (T)
  32947. (I[0]*kernel[0] + I[1]*kernel[1] + I[2]*kernel[2] +
  32948. I[3]*kernel[3] + I[4]*kernel[4] + I[5]*kernel[5] +
  32949. I[6]*kernel[6] + I[7]*kernel[7] + I[8]*kernel[8]);
  32950. return res;
  32951. }
  32952. //! Resize image to double-size, using the Scale2X algorithm.
  32953. /**
  32954. \note Use anisotropic upscaling algorithm
  32955. <a href="http://scale2x.sourceforge.net/algorithm.html">described here</a>.
  32956. **/
  32957. CImg<T>& resize_doubleXY() {
  32958. return get_resize_doubleXY().move_to(*this);
  32959. }
  32960. //! Resize image to double-size, using the Scale2X algorithm \newinstance.
  32961. CImg<T> get_resize_doubleXY() const {
  32962. #define _cimg_gs2x_for3(bound,i) \
  32963. for (int i = 0, _p1##i = 0, \
  32964. _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
  32965. _n1##i<(int)(bound) || i==--_n1##i; \
  32966. _p1##i = i++, ++_n1##i, ptrd1+=(res)._width, ptrd2+=(res)._width)
  32967. #define _cimg_gs2x_for3x3(img,x,y,z,c,I,T) \
  32968. _cimg_gs2x_for3((img)._height,y) for (int x = 0, \
  32969. _p1##x = 0, \
  32970. _n1##x = (int)( \
  32971. (I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
  32972. (I[3] = I[4] = (T)(img)(0,y,z,c)), \
  32973. (I[7] = (T)(img)(0,_n1##y,z,c)), \
  32974. 1>=(img)._width?(img).width() - 1:1); \
  32975. (_n1##x<(img).width() && ( \
  32976. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  32977. (I[5] = (T)(img)(_n1##x,y,z,c)), \
  32978. (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  32979. x==--_n1##x; \
  32980. I[1] = I[2], \
  32981. I[3] = I[4], I[4] = I[5], \
  32982. I[7] = I[8], \
  32983. _p1##x = x++, ++_n1##x)
  32984. if (is_empty()) return *this;
  32985. CImg<T> res(_width<<1,_height<<1,_depth,_spectrum);
  32986. CImg_3x3(I,T);
  32987. cimg_forZC(*this,z,c) {
  32988. T
  32989. *ptrd1 = res.data(0,0,z,c),
  32990. *ptrd2 = ptrd1 + res._width;
  32991. _cimg_gs2x_for3x3(*this,x,y,z,c,I,T) {
  32992. if (Icp!=Icn && Ipc!=Inc) {
  32993. *(ptrd1++) = Ipc==Icp?Ipc:Icc;
  32994. *(ptrd1++) = Icp==Inc?Inc:Icc;
  32995. *(ptrd2++) = Ipc==Icn?Ipc:Icc;
  32996. *(ptrd2++) = Icn==Inc?Inc:Icc;
  32997. } else { *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; }
  32998. }
  32999. }
  33000. return res;
  33001. }
  33002. //! Resize image to triple-size, using the Scale3X algorithm.
  33003. /**
  33004. \note Use anisotropic upscaling algorithm
  33005. <a href="http://scale2x.sourceforge.net/algorithm.html">described here</a>.
  33006. **/
  33007. CImg<T>& resize_tripleXY() {
  33008. return get_resize_tripleXY().move_to(*this);
  33009. }
  33010. //! Resize image to triple-size, using the Scale3X algorithm \newinstance.
  33011. CImg<T> get_resize_tripleXY() const {
  33012. #define _cimg_gs3x_for3(bound,i) \
  33013. for (int i = 0, _p1##i = 0, \
  33014. _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
  33015. _n1##i<(int)(bound) || i==--_n1##i; \
  33016. _p1##i = i++, ++_n1##i, ptrd1+=2*(res)._width, ptrd2+=2*(res)._width, ptrd3+=2*(res)._width)
  33017. #define _cimg_gs3x_for3x3(img,x,y,z,c,I,T) \
  33018. _cimg_gs3x_for3((img)._height,y) for (int x = 0, \
  33019. _p1##x = 0, \
  33020. _n1##x = (int)( \
  33021. (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
  33022. (I[3] = I[4] = (T)(img)(0,y,z,c)), \
  33023. (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
  33024. 1>=(img)._width?(img).width() - 1:1); \
  33025. (_n1##x<(img).width() && ( \
  33026. (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
  33027. (I[5] = (T)(img)(_n1##x,y,z,c)), \
  33028. (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
  33029. x==--_n1##x; \
  33030. I[0] = I[1], I[1] = I[2], \
  33031. I[3] = I[4], I[4] = I[5], \
  33032. I[6] = I[7], I[7] = I[8], \
  33033. _p1##x = x++, ++_n1##x)
  33034. if (is_empty()) return *this;
  33035. CImg<T> res(3*_width,3*_height,_depth,_spectrum);
  33036. CImg_3x3(I,T);
  33037. cimg_forZC(*this,z,c) {
  33038. T
  33039. *ptrd1 = res.data(0,0,z,c),
  33040. *ptrd2 = ptrd1 + res._width,
  33041. *ptrd3 = ptrd2 + res._width;
  33042. _cimg_gs3x_for3x3(*this,x,y,z,c,I,T) {
  33043. if (Icp != Icn && Ipc != Inc) {
  33044. *(ptrd1++) = Ipc==Icp?Ipc:Icc;
  33045. *(ptrd1++) = (Ipc==Icp && Icc!=Inp) || (Icp==Inc && Icc!=Ipp)?Icp:Icc;
  33046. *(ptrd1++) = Icp==Inc?Inc:Icc;
  33047. *(ptrd2++) = (Ipc==Icp && Icc!=Ipn) || (Ipc==Icn && Icc!=Ipp)?Ipc:Icc;
  33048. *(ptrd2++) = Icc;
  33049. *(ptrd2++) = (Icp==Inc && Icc!=Inn) || (Icn==Inc && Icc!=Inp)?Inc:Icc;
  33050. *(ptrd3++) = Ipc==Icn?Ipc:Icc;
  33051. *(ptrd3++) = (Ipc==Icn && Icc!=Inn) || (Icn==Inc && Icc!=Ipn)?Icn:Icc;
  33052. *(ptrd3++) = Icn==Inc?Inc:Icc;
  33053. } else {
  33054. *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd1++) = Icc;
  33055. *(ptrd2++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc;
  33056. *(ptrd3++) = Icc; *(ptrd3++) = Icc; *(ptrd3++) = Icc;
  33057. }
  33058. }
  33059. }
  33060. return res;
  33061. }
  33062. //! Mirror image content along specified axis.
  33063. /**
  33064. \param axis Mirror axis
  33065. **/
  33066. CImg<T>& mirror(const char axis) {
  33067. if (is_empty()) return *this;
  33068. T *pf, *pb, *buf = 0;
  33069. switch (cimg::lowercase(axis)) {
  33070. case 'x' : {
  33071. pf = _data; pb = data(_width - 1);
  33072. const unsigned int width2 = _width/2;
  33073. for (unsigned int yzv = 0; yzv<_height*_depth*_spectrum; ++yzv) {
  33074. for (unsigned int x = 0; x<width2; ++x) { const T val = *pf; *(pf++) = *pb; *(pb--) = val; }
  33075. pf+=_width - width2;
  33076. pb+=_width + width2;
  33077. }
  33078. } break;
  33079. case 'y' : {
  33080. buf = new T[_width];
  33081. pf = _data; pb = data(0,_height - 1);
  33082. const unsigned int height2 = _height/2;
  33083. for (unsigned int zv = 0; zv<_depth*_spectrum; ++zv) {
  33084. for (unsigned int y = 0; y<height2; ++y) {
  33085. std::memcpy(buf,pf,_width*sizeof(T));
  33086. std::memcpy(pf,pb,_width*sizeof(T));
  33087. std::memcpy(pb,buf,_width*sizeof(T));
  33088. pf+=_width;
  33089. pb-=_width;
  33090. }
  33091. pf+=(ulongT)_width*(_height - height2);
  33092. pb+=(ulongT)_width*(_height + height2);
  33093. }
  33094. } break;
  33095. case 'z' : {
  33096. buf = new T[(ulongT)_width*_height];
  33097. pf = _data; pb = data(0,0,_depth - 1);
  33098. const unsigned int depth2 = _depth/2;
  33099. cimg_forC(*this,c) {
  33100. for (unsigned int z = 0; z<depth2; ++z) {
  33101. std::memcpy(buf,pf,_width*_height*sizeof(T));
  33102. std::memcpy(pf,pb,_width*_height*sizeof(T));
  33103. std::memcpy(pb,buf,_width*_height*sizeof(T));
  33104. pf+=(ulongT)_width*_height;
  33105. pb-=(ulongT)_width*_height;
  33106. }
  33107. pf+=(ulongT)_width*_height*(_depth - depth2);
  33108. pb+=(ulongT)_width*_height*(_depth + depth2);
  33109. }
  33110. } break;
  33111. case 'c' : {
  33112. buf = new T[(ulongT)_width*_height*_depth];
  33113. pf = _data; pb = data(0,0,0,_spectrum - 1);
  33114. const unsigned int _spectrum2 = _spectrum/2;
  33115. for (unsigned int v = 0; v<_spectrum2; ++v) {
  33116. std::memcpy(buf,pf,_width*_height*_depth*sizeof(T));
  33117. std::memcpy(pf,pb,_width*_height*_depth*sizeof(T));
  33118. std::memcpy(pb,buf,_width*_height*_depth*sizeof(T));
  33119. pf+=(ulongT)_width*_height*_depth;
  33120. pb-=(ulongT)_width*_height*_depth;
  33121. }
  33122. } break;
  33123. default :
  33124. throw CImgArgumentException(_cimg_instance
  33125. "mirror(): Invalid specified axis '%c'.",
  33126. cimg_instance,
  33127. axis);
  33128. }
  33129. delete[] buf;
  33130. return *this;
  33131. }
  33132. //! Mirror image content along specified axis \newinstance.
  33133. CImg<T> get_mirror(const char axis) const {
  33134. return (+*this).mirror(axis);
  33135. }
  33136. //! Mirror image content along specified axes.
  33137. /**
  33138. \param axes Mirror axes, as a C-string.
  33139. \note \c axes may contains multiple characters, e.g. \c "xyz"
  33140. **/
  33141. CImg<T>& mirror(const char *const axes) {
  33142. for (const char *s = axes; *s; ++s) mirror(*s);
  33143. return *this;
  33144. }
  33145. //! Mirror image content along specified axes \newinstance.
  33146. CImg<T> get_mirror(const char *const axes) const {
  33147. return (+*this).mirror(axes);
  33148. }
  33149. //! Shift image content.
  33150. /**
  33151. \param delta_x Amount of displacement along the X-axis.
  33152. \param delta_y Amount of displacement along the Y-axis.
  33153. \param delta_z Amount of displacement along the Z-axis.
  33154. \param delta_c Amount of displacement along the C-axis.
  33155. \param boundary_conditions Boundary conditions.
  33156. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  33157. **/
  33158. CImg<T>& shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0,
  33159. const unsigned int boundary_conditions=0) {
  33160. if (is_empty()) return *this;
  33161. if (boundary_conditions==3)
  33162. return get_crop(-delta_x,-delta_y,-delta_z,-delta_c,
  33163. width() - delta_x - 1,
  33164. height() - delta_y - 1,
  33165. depth() - delta_z - 1,
  33166. spectrum() - delta_c - 1,3).move_to(*this);
  33167. if (delta_x) // Shift along X-axis
  33168. switch (boundary_conditions) {
  33169. case 2 : { // Periodic
  33170. const int ml = cimg::mod(-delta_x,width()), ndelta_x = (ml<=width()/2)?ml:(ml-width());
  33171. if (!ndelta_x) return *this;
  33172. CImg<T> buf(cimg::abs(ndelta_x));
  33173. if (ndelta_x>0) cimg_forYZC(*this,y,z,c) {
  33174. std::memcpy(buf,data(0,y,z,c),ndelta_x*sizeof(T));
  33175. std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T));
  33176. std::memcpy(data(_width-ndelta_x,y,z,c),buf,ndelta_x*sizeof(T));
  33177. } else cimg_forYZC(*this,y,z,c) {
  33178. std::memcpy(buf,data(_width + ndelta_x,y,z,c),-ndelta_x*sizeof(T));
  33179. std::memmove(data(-ndelta_x,y,z,c),data(0,y,z,c),(_width + ndelta_x)*sizeof(T));
  33180. std::memcpy(data(0,y,z,c),buf,-ndelta_x*sizeof(T));
  33181. }
  33182. } break;
  33183. case 1 : // Neumann
  33184. if (delta_x<0) {
  33185. const int ndelta_x = (-delta_x>=width())?width() - 1:-delta_x;
  33186. if (!ndelta_x) return *this;
  33187. cimg_forYZC(*this,y,z,c) {
  33188. std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T));
  33189. T *ptrd = data(_width - 1,y,z,c);
  33190. const T val = *ptrd;
  33191. for (int l = 0; l<ndelta_x - 1; ++l) *(--ptrd) = val;
  33192. }
  33193. } else {
  33194. const int ndelta_x = (delta_x>=width())?width() - 1:delta_x;
  33195. if (!ndelta_x) return *this;
  33196. cimg_forYZC(*this,y,z,c) {
  33197. std::memmove(data(ndelta_x,y,z,c),data(0,y,z,c),(_width-ndelta_x)*sizeof(T));
  33198. T *ptrd = data(0,y,z,c);
  33199. const T val = *ptrd;
  33200. for (int l = 0; l<ndelta_x - 1; ++l) *(++ptrd) = val;
  33201. }
  33202. }
  33203. break;
  33204. default : // Dirichlet
  33205. if (delta_x<=-width() || delta_x>=width()) return fill((T)0);
  33206. if (delta_x<0) cimg_forYZC(*this,y,z,c) {
  33207. std::memmove(data(0,y,z,c),data(-delta_x,y,z,c),(_width + delta_x)*sizeof(T));
  33208. std::memset(data(_width + delta_x,y,z,c),0,-delta_x*sizeof(T));
  33209. } else cimg_forYZC(*this,y,z,c) {
  33210. std::memmove(data(delta_x,y,z,c),data(0,y,z,c),(_width-delta_x)*sizeof(T));
  33211. std::memset(data(0,y,z,c),0,delta_x*sizeof(T));
  33212. }
  33213. }
  33214. if (delta_y) // Shift along Y-axis
  33215. switch (boundary_conditions) {
  33216. case 2 : { // Periodic
  33217. const int ml = cimg::mod(-delta_y,height()), ndelta_y = (ml<=height()/2)?ml:(ml-height());
  33218. if (!ndelta_y) return *this;
  33219. CImg<T> buf(width(),cimg::abs(ndelta_y));
  33220. if (ndelta_y>0) cimg_forZC(*this,z,c) {
  33221. std::memcpy(buf,data(0,0,z,c),_width*ndelta_y*sizeof(T));
  33222. std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T));
  33223. std::memcpy(data(0,_height-ndelta_y,z,c),buf,_width*ndelta_y*sizeof(T));
  33224. } else cimg_forZC(*this,z,c) {
  33225. std::memcpy(buf,data(0,_height + ndelta_y,z,c),-ndelta_y*_width*sizeof(T));
  33226. std::memmove(data(0,-ndelta_y,z,c),data(0,0,z,c),_width*(_height + ndelta_y)*sizeof(T));
  33227. std::memcpy(data(0,0,z,c),buf,-ndelta_y*_width*sizeof(T));
  33228. }
  33229. } break;
  33230. case 1 : // Neumann
  33231. if (delta_y<0) {
  33232. const int ndelta_y = (-delta_y>=height())?height() - 1:-delta_y;
  33233. if (!ndelta_y) return *this;
  33234. cimg_forZC(*this,z,c) {
  33235. std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T));
  33236. T *ptrd = data(0,_height-ndelta_y,z,c), *ptrs = data(0,_height - 1,z,c);
  33237. for (int l = 0; l<ndelta_y - 1; ++l) { std::memcpy(ptrd,ptrs,_width*sizeof(T)); ptrd+=_width; }
  33238. }
  33239. } else {
  33240. const int ndelta_y = (delta_y>=height())?height() - 1:delta_y;
  33241. if (!ndelta_y) return *this;
  33242. cimg_forZC(*this,z,c) {
  33243. std::memmove(data(0,ndelta_y,z,c),data(0,0,z,c),_width*(_height-ndelta_y)*sizeof(T));
  33244. T *ptrd = data(0,1,z,c), *ptrs = data(0,0,z,c);
  33245. for (int l = 0; l<ndelta_y - 1; ++l) { std::memcpy(ptrd,ptrs,_width*sizeof(T)); ptrd+=_width; }
  33246. }
  33247. }
  33248. break;
  33249. default : // Dirichlet
  33250. if (delta_y<=-height() || delta_y>=height()) return fill((T)0);
  33251. if (delta_y<0) cimg_forZC(*this,z,c) {
  33252. std::memmove(data(0,0,z,c),data(0,-delta_y,z,c),_width*(_height + delta_y)*sizeof(T));
  33253. std::memset(data(0,_height + delta_y,z,c),0,-delta_y*_width*sizeof(T));
  33254. } else cimg_forZC(*this,z,c) {
  33255. std::memmove(data(0,delta_y,z,c),data(0,0,z,c),_width*(_height-delta_y)*sizeof(T));
  33256. std::memset(data(0,0,z,c),0,delta_y*_width*sizeof(T));
  33257. }
  33258. }
  33259. if (delta_z) // Shift along Z-axis
  33260. switch (boundary_conditions) {
  33261. case 2 : { // Periodic
  33262. const int ml = cimg::mod(-delta_z,depth()), ndelta_z = (ml<=depth()/2)?ml:(ml-depth());
  33263. if (!ndelta_z) return *this;
  33264. CImg<T> buf(width(),height(),cimg::abs(ndelta_z));
  33265. if (ndelta_z>0) cimg_forC(*this,c) {
  33266. std::memcpy(buf,data(0,0,0,c),_width*_height*ndelta_z*sizeof(T));
  33267. std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
  33268. std::memcpy(data(0,0,_depth-ndelta_z,c),buf,_width*_height*ndelta_z*sizeof(T));
  33269. } else cimg_forC(*this,c) {
  33270. std::memcpy(buf,data(0,0,_depth + ndelta_z,c),-ndelta_z*_width*_height*sizeof(T));
  33271. std::memmove(data(0,0,-ndelta_z,c),data(0,0,0,c),_width*_height*(_depth + ndelta_z)*sizeof(T));
  33272. std::memcpy(data(0,0,0,c),buf,-ndelta_z*_width*_height*sizeof(T));
  33273. }
  33274. } break;
  33275. case 1 : // Neumann
  33276. if (delta_z<0) {
  33277. const int ndelta_z = (-delta_z>=depth())?depth() - 1:-delta_z;
  33278. if (!ndelta_z) return *this;
  33279. cimg_forC(*this,c) {
  33280. std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
  33281. T *ptrd = data(0,0,_depth-ndelta_z,c), *ptrs = data(0,0,_depth - 1,c);
  33282. for (int l = 0; l<ndelta_z - 1; ++l) {
  33283. std::memcpy(ptrd,ptrs,_width*_height*sizeof(T)); ptrd+=(ulongT)_width*_height;
  33284. }
  33285. }
  33286. } else {
  33287. const int ndelta_z = (delta_z>=depth())?depth() - 1:delta_z;
  33288. if (!ndelta_z) return *this;
  33289. cimg_forC(*this,c) {
  33290. std::memmove(data(0,0,ndelta_z,c),data(0,0,0,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
  33291. T *ptrd = data(0,0,1,c), *ptrs = data(0,0,0,c);
  33292. for (int l = 0; l<ndelta_z - 1; ++l) {
  33293. std::memcpy(ptrd,ptrs,_width*_height*sizeof(T)); ptrd+=(ulongT)_width*_height;
  33294. }
  33295. }
  33296. }
  33297. break;
  33298. default : // Dirichlet
  33299. if (delta_z<=-depth() || delta_z>=depth()) return fill((T)0);
  33300. if (delta_z<0) cimg_forC(*this,c) {
  33301. std::memmove(data(0,0,0,c),data(0,0,-delta_z,c),_width*_height*(_depth + delta_z)*sizeof(T));
  33302. std::memset(data(0,0,_depth + delta_z,c),0,_width*_height*(-delta_z)*sizeof(T));
  33303. } else cimg_forC(*this,c) {
  33304. std::memmove(data(0,0,delta_z,c),data(0,0,0,c),_width*_height*(_depth-delta_z)*sizeof(T));
  33305. std::memset(data(0,0,0,c),0,delta_z*_width*_height*sizeof(T));
  33306. }
  33307. }
  33308. if (delta_c) // Shift along C-axis
  33309. switch (boundary_conditions) {
  33310. case 2 : { // Periodic
  33311. const int ml = cimg::mod(-delta_c,spectrum()), ndelta_c = (ml<=spectrum()/2)?ml:(ml-spectrum());
  33312. if (!ndelta_c) return *this;
  33313. CImg<T> buf(width(),height(),depth(),cimg::abs(ndelta_c));
  33314. if (ndelta_c>0) {
  33315. std::memcpy(buf,_data,_width*_height*_depth*ndelta_c*sizeof(T));
  33316. std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
  33317. std::memcpy(data(0,0,0,_spectrum-ndelta_c),buf,_width*_height*_depth*ndelta_c*sizeof(T));
  33318. } else {
  33319. std::memcpy(buf,data(0,0,0,_spectrum + ndelta_c),-ndelta_c*_width*_height*_depth*sizeof(T));
  33320. std::memmove(data(0,0,0,-ndelta_c),_data,_width*_height*_depth*(_spectrum + ndelta_c)*sizeof(T));
  33321. std::memcpy(_data,buf,-ndelta_c*_width*_height*_depth*sizeof(T));
  33322. }
  33323. } break;
  33324. case 1 : // Neumann
  33325. if (delta_c<0) {
  33326. const int ndelta_c = (-delta_c>=spectrum())?spectrum() - 1:-delta_c;
  33327. if (!ndelta_c) return *this;
  33328. std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
  33329. T *ptrd = data(0,0,0,_spectrum-ndelta_c), *ptrs = data(0,0,0,_spectrum - 1);
  33330. for (int l = 0; l<ndelta_c - 1; ++l) {
  33331. std::memcpy(ptrd,ptrs,_width*_height*_depth*sizeof(T)); ptrd+=(ulongT)_width*_height*_depth;
  33332. }
  33333. } else {
  33334. const int ndelta_c = (delta_c>=spectrum())?spectrum() - 1:delta_c;
  33335. if (!ndelta_c) return *this;
  33336. std::memmove(data(0,0,0,ndelta_c),_data,_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
  33337. T *ptrd = data(0,0,0,1);
  33338. for (int l = 0; l<ndelta_c - 1; ++l) {
  33339. std::memcpy(ptrd,_data,_width*_height*_depth*sizeof(T)); ptrd+=(ulongT)_width*_height*_depth;
  33340. }
  33341. }
  33342. break;
  33343. default : // Dirichlet
  33344. if (delta_c<=-spectrum() || delta_c>=spectrum()) return fill((T)0);
  33345. if (delta_c<0) {
  33346. std::memmove(_data,data(0,0,0,-delta_c),_width*_height*_depth*(_spectrum + delta_c)*sizeof(T));
  33347. std::memset(data(0,0,0,_spectrum + delta_c),0,_width*_height*_depth*(-delta_c)*sizeof(T));
  33348. } else {
  33349. std::memmove(data(0,0,0,delta_c),_data,_width*_height*_depth*(_spectrum-delta_c)*sizeof(T));
  33350. std::memset(_data,0,delta_c*_width*_height*_depth*sizeof(T));
  33351. }
  33352. }
  33353. return *this;
  33354. }
  33355. //! Shift image content \newinstance.
  33356. CImg<T> get_shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0,
  33357. const unsigned int boundary_conditions=0) const {
  33358. return (+*this).shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions);
  33359. }
  33360. //! Permute axes order.
  33361. /**
  33362. \param axes_order Axes permutations, as a C-string of 4 characters.
  33363. This function permutes image content regarding the specified axes permutation.
  33364. **/
  33365. CImg<T>& permute_axes(const char *const axes_order) {
  33366. return get_permute_axes(axes_order).move_to(*this);
  33367. }
  33368. //! Permute axes order \newinstance.
  33369. CImg<T> get_permute_axes(const char *const axes_order) const {
  33370. const T foo = (T)0;
  33371. return _permute_axes(axes_order,foo);
  33372. }
  33373. template<typename t>
  33374. CImg<t> _permute_axes(const char *const axes_order, const t&) const {
  33375. if (is_empty() || !axes_order) return CImg<t>(*this,false);
  33376. CImg<t> res;
  33377. const T* ptrs = _data;
  33378. unsigned char s_code[4] = { 0,1,2,3 }, n_code[4] = { 0 };
  33379. for (unsigned int l = 0; axes_order[l]; ++l) {
  33380. int c = cimg::lowercase(axes_order[l]);
  33381. if (l>=4 || (c!='x' && c!='y' && c!='z' && c!='c')) { *s_code = 4; break; }
  33382. else { ++n_code[c%=4]; s_code[l] = (unsigned char)c; }
  33383. }
  33384. if (*axes_order && *s_code<4 && *n_code<=1 && n_code[1]<=1 && n_code[2]<=1 && n_code[3]<=1) {
  33385. const unsigned int code = (s_code[0]<<12) | (s_code[1]<<8) | (s_code[2]<<4) | (s_code[3]);
  33386. ulongT wh, whd;
  33387. switch (code) {
  33388. case 0x0123 : // xyzc
  33389. return +*this;
  33390. case 0x0132 : // xycz
  33391. res.assign(_width,_height,_spectrum,_depth);
  33392. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33393. cimg_forXYZC(*this,x,y,z,c) res(x,y,c,z,wh,whd) = (t)*(ptrs++);
  33394. break;
  33395. case 0x0213 : // xzyc
  33396. res.assign(_width,_depth,_height,_spectrum);
  33397. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33398. cimg_forXYZC(*this,x,y,z,c) res(x,z,y,c,wh,whd) = (t)*(ptrs++);
  33399. break;
  33400. case 0x0231 : // xzcy
  33401. res.assign(_width,_depth,_spectrum,_height);
  33402. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33403. cimg_forXYZC(*this,x,y,z,c) res(x,z,c,y,wh,whd) = (t)*(ptrs++);
  33404. break;
  33405. case 0x0312 : // xcyz
  33406. res.assign(_width,_spectrum,_height,_depth);
  33407. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33408. cimg_forXYZC(*this,x,y,z,c) res(x,c,y,z,wh,whd) = (t)*(ptrs++);
  33409. break;
  33410. case 0x0321 : // xczy
  33411. res.assign(_width,_spectrum,_depth,_height);
  33412. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33413. cimg_forXYZC(*this,x,y,z,c) res(x,c,z,y,wh,whd) = (t)*(ptrs++);
  33414. break;
  33415. case 0x1023 : // yxzc
  33416. res.assign(_height,_width,_depth,_spectrum);
  33417. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33418. cimg_forXYZC(*this,x,y,z,c) res(y,x,z,c,wh,whd) = (t)*(ptrs++);
  33419. break;
  33420. case 0x1032 : // yxcz
  33421. res.assign(_height,_width,_spectrum,_depth);
  33422. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33423. cimg_forXYZC(*this,x,y,z,c) res(y,x,c,z,wh,whd) = (t)*(ptrs++);
  33424. break;
  33425. case 0x1203 : // yzxc
  33426. res.assign(_height,_depth,_width,_spectrum);
  33427. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33428. cimg_forXYZC(*this,x,y,z,c) res(y,z,x,c,wh,whd) = (t)*(ptrs++);
  33429. break;
  33430. case 0x1230 : // yzcx
  33431. res.assign(_height,_depth,_spectrum,_width);
  33432. switch (_width) {
  33433. case 1 : {
  33434. t *ptr_r = res.data(0,0,0,0);
  33435. for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
  33436. *(ptr_r++) = (t)*(ptrs++);
  33437. }
  33438. } break;
  33439. case 2 : {
  33440. t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1);
  33441. for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
  33442. *(ptr_r++) = (t)ptrs[0];
  33443. *(ptr_g++) = (t)ptrs[1];
  33444. ptrs+=2;
  33445. }
  33446. } break;
  33447. case 3 : { // Optimization for the classical conversion from interleaved RGB to planar RGB
  33448. t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1), *ptr_b = res.data(0,0,0,2);
  33449. for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
  33450. *(ptr_r++) = (t)ptrs[0];
  33451. *(ptr_g++) = (t)ptrs[1];
  33452. *(ptr_b++) = (t)ptrs[2];
  33453. ptrs+=3;
  33454. }
  33455. } break;
  33456. case 4 : { // Optimization for the classical conversion from interleaved RGBA to planar RGBA
  33457. t
  33458. *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1),
  33459. *ptr_b = res.data(0,0,0,2), *ptr_a = res.data(0,0,0,3);
  33460. for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
  33461. *(ptr_r++) = (t)ptrs[0];
  33462. *(ptr_g++) = (t)ptrs[1];
  33463. *(ptr_b++) = (t)ptrs[2];
  33464. *(ptr_a++) = (t)ptrs[3];
  33465. ptrs+=4;
  33466. }
  33467. } break;
  33468. default : {
  33469. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33470. cimg_forXYZC(*this,x,y,z,c) res(y,z,c,x,wh,whd) = *(ptrs++);
  33471. return res;
  33472. }
  33473. }
  33474. break;
  33475. case 0x1302 : // ycxz
  33476. res.assign(_height,_spectrum,_width,_depth);
  33477. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33478. cimg_forXYZC(*this,x,y,z,c) res(y,c,x,z,wh,whd) = (t)*(ptrs++);
  33479. break;
  33480. case 0x1320 : // yczx
  33481. res.assign(_height,_spectrum,_depth,_width);
  33482. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33483. cimg_forXYZC(*this,x,y,z,c) res(y,c,z,x,wh,whd) = (t)*(ptrs++);
  33484. break;
  33485. case 0x2013 : // zxyc
  33486. res.assign(_depth,_width,_height,_spectrum);
  33487. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33488. cimg_forXYZC(*this,x,y,z,c) res(z,x,y,c,wh,whd) = (t)*(ptrs++);
  33489. break;
  33490. case 0x2031 : // zxcy
  33491. res.assign(_depth,_width,_spectrum,_height);
  33492. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33493. cimg_forXYZC(*this,x,y,z,c) res(z,x,c,y,wh,whd) = (t)*(ptrs++);
  33494. break;
  33495. case 0x2103 : // zyxc
  33496. res.assign(_depth,_height,_width,_spectrum);
  33497. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33498. cimg_forXYZC(*this,x,y,z,c) res(z,y,x,c,wh,whd) = (t)*(ptrs++);
  33499. break;
  33500. case 0x2130 : // zycx
  33501. res.assign(_depth,_height,_spectrum,_width);
  33502. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33503. cimg_forXYZC(*this,x,y,z,c) res(z,y,c,x,wh,whd) = (t)*(ptrs++);
  33504. break;
  33505. case 0x2301 : // zcxy
  33506. res.assign(_depth,_spectrum,_width,_height);
  33507. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33508. cimg_forXYZC(*this,x,y,z,c) res(z,c,x,y,wh,whd) = (t)*(ptrs++);
  33509. break;
  33510. case 0x2310 : // zcyx
  33511. res.assign(_depth,_spectrum,_height,_width);
  33512. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33513. cimg_forXYZC(*this,x,y,z,c) res(z,c,y,x,wh,whd) = (t)*(ptrs++);
  33514. break;
  33515. case 0x3012 : // cxyz
  33516. res.assign(_spectrum,_width,_height,_depth);
  33517. switch (_spectrum) {
  33518. case 1 : {
  33519. const T *ptr_r = data(0,0,0,0);
  33520. t *ptrd = res._data;
  33521. for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) *(ptrd++) = (t)*(ptr_r++);
  33522. } break;
  33523. case 2 : {
  33524. const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1);
  33525. t *ptrd = res._data;
  33526. for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
  33527. ptrd[0] = (t)*(ptr_r++);
  33528. ptrd[1] = (t)*(ptr_g++);
  33529. ptrd+=2;
  33530. }
  33531. } break;
  33532. case 3 : { // Optimization for the classical conversion from planar RGB to interleaved RGB
  33533. const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
  33534. t *ptrd = res._data;
  33535. for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
  33536. ptrd[0] = (t)*(ptr_r++);
  33537. ptrd[1] = (t)*(ptr_g++);
  33538. ptrd[2] = (t)*(ptr_b++);
  33539. ptrd+=3;
  33540. }
  33541. } break;
  33542. case 4 : { // Optimization for the classical conversion from planar RGBA to interleaved RGBA
  33543. const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3);
  33544. t *ptrd = res._data;
  33545. for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
  33546. ptrd[0] = (t)*(ptr_r++);
  33547. ptrd[1] = (t)*(ptr_g++);
  33548. ptrd[2] = (t)*(ptr_b++);
  33549. ptrd[3] = (t)*(ptr_a++);
  33550. ptrd+=4;
  33551. }
  33552. } break;
  33553. default : {
  33554. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33555. cimg_forXYZC(*this,x,y,z,c) res(c,x,y,z,wh,whd) = (t)*(ptrs++);
  33556. }
  33557. }
  33558. break;
  33559. case 0x3021 : // cxzy
  33560. res.assign(_spectrum,_width,_depth,_height);
  33561. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33562. cimg_forXYZC(*this,x,y,z,c) res(c,x,z,y,wh,whd) = (t)*(ptrs++);
  33563. break;
  33564. case 0x3102 : // cyxz
  33565. res.assign(_spectrum,_height,_width,_depth);
  33566. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33567. cimg_forXYZC(*this,x,y,z,c) res(c,y,x,z,wh,whd) = (t)*(ptrs++);
  33568. break;
  33569. case 0x3120 : // cyzx
  33570. res.assign(_spectrum,_height,_depth,_width);
  33571. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33572. cimg_forXYZC(*this,x,y,z,c) res(c,y,z,x,wh,whd) = (t)*(ptrs++);
  33573. break;
  33574. case 0x3201 : // czxy
  33575. res.assign(_spectrum,_depth,_width,_height);
  33576. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33577. cimg_forXYZC(*this,x,y,z,c) res(c,z,x,y,wh,whd) = (t)*(ptrs++);
  33578. break;
  33579. case 0x3210 : // czyx
  33580. res.assign(_spectrum,_depth,_height,_width);
  33581. wh = (ulongT)res._width*res._height; whd = wh*res._depth;
  33582. cimg_forXYZC(*this,x,y,z,c) res(c,z,y,x,wh,whd) = (t)*(ptrs++);
  33583. break;
  33584. }
  33585. }
  33586. if (!res)
  33587. throw CImgArgumentException(_cimg_instance
  33588. "permute_axes(): Invalid specified axes order '%s'.",
  33589. cimg_instance,
  33590. axes_order);
  33591. return res;
  33592. }
  33593. //! Unroll pixel values along specified axis.
  33594. /**
  33595. \param axis Unroll axis (can be \c 'x', \c 'y', \c 'z' or c 'c').
  33596. **/
  33597. CImg<T>& unroll(const char axis) {
  33598. const unsigned int siz = (unsigned int)size();
  33599. if (siz) switch (cimg::lowercase(axis)) {
  33600. case 'x' : _width = siz; _height = _depth = _spectrum = 1; break;
  33601. case 'y' : _height = siz; _width = _depth = _spectrum = 1; break;
  33602. case 'z' : _depth = siz; _width = _height = _spectrum = 1; break;
  33603. case 'c' : _spectrum = siz; _width = _height = _depth = 1; break;
  33604. }
  33605. return *this;
  33606. }
  33607. //! Unroll pixel values along specified axis \newinstance.
  33608. CImg<T> get_unroll(const char axis) const {
  33609. return (+*this).unroll(axis);
  33610. }
  33611. //! Rotate image with arbitrary angle.
  33612. /**
  33613. \param angle Rotation angle, in degrees.
  33614. \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
  33615. \param boundary_conditions Boundary conditions.
  33616. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  33617. \note The size of the image is modified.
  33618. **/
  33619. CImg<T>& rotate(const float angle, const unsigned int interpolation=1,
  33620. const unsigned int boundary_conditions=0) {
  33621. const float nangle = cimg::mod(angle,360.f);
  33622. if (nangle==0.f) return *this;
  33623. return get_rotate(nangle,interpolation,boundary_conditions).move_to(*this);
  33624. }
  33625. //! Rotate image with arbitrary angle \newinstance.
  33626. CImg<T> get_rotate(const float angle, const unsigned int interpolation=1,
  33627. const unsigned int boundary_conditions=0) const {
  33628. if (is_empty()) return *this;
  33629. CImg<T> res;
  33630. const float nangle = cimg::mod(angle,360.f);
  33631. if (boundary_conditions!=1 && cimg::mod(nangle,90.f)==0) { // Optimized version for orthogonal angles
  33632. const int wm1 = width() - 1, hm1 = height() - 1;
  33633. const int iangle = (int)nangle/90;
  33634. switch (iangle) {
  33635. case 1 : { // 90 deg
  33636. res.assign(_height,_width,_depth,_spectrum);
  33637. T *ptrd = res._data;
  33638. cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(y,hm1 - x,z,c);
  33639. } break;
  33640. case 2 : { // 180 deg
  33641. res.assign(_width,_height,_depth,_spectrum);
  33642. T *ptrd = res._data;
  33643. cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1 - x,hm1 - y,z,c);
  33644. } break;
  33645. case 3 : { // 270 deg
  33646. res.assign(_height,_width,_depth,_spectrum);
  33647. T *ptrd = res._data;
  33648. cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1 - y,x,z,c);
  33649. } break;
  33650. default : // 0 deg
  33651. return *this;
  33652. }
  33653. } else { // Generic angle
  33654. const float
  33655. rad = (float)(nangle*cimg::PI/180.),
  33656. ca = (float)std::cos(rad), sa = (float)std::sin(rad),
  33657. ux = cimg::abs((_width - 1)*ca), uy = cimg::abs((_width - 1)*sa),
  33658. vx = cimg::abs((_height - 1)*sa), vy = cimg::abs((_height - 1)*ca),
  33659. w2 = 0.5f*(_width - 1), h2 = 0.5f*(_height - 1);
  33660. res.assign((int)cimg::round(1 + ux + vx),(int)cimg::round(1 + uy + vy),_depth,_spectrum);
  33661. const float rw2 = 0.5f*(res._width - 1), rh2 = 0.5f*(res._height - 1);
  33662. _rotate(res,nangle,interpolation,boundary_conditions,w2,h2,rw2,rh2);
  33663. }
  33664. return res;
  33665. }
  33666. //! Rotate image with arbitrary angle, around a center point.
  33667. /**
  33668. \param angle Rotation angle, in degrees.
  33669. \param cx X-coordinate of the rotation center.
  33670. \param cy Y-coordinate of the rotation center.
  33671. \param interpolation Type of interpolation, <tt>{ 0=nearest | 1=linear | 2=cubic | 3=mirror }</tt>.
  33672. \param boundary_conditions Boundary conditions, <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  33673. **/
  33674. CImg<T>& rotate(const float angle, const float cx, const float cy,
  33675. const unsigned int interpolation, const unsigned int boundary_conditions=0) {
  33676. return get_rotate(angle,cx,cy,interpolation,boundary_conditions).move_to(*this);
  33677. }
  33678. //! Rotate image with arbitrary angle, around a center point \newinstance.
  33679. CImg<T> get_rotate(const float angle, const float cx, const float cy,
  33680. const unsigned int interpolation, const unsigned int boundary_conditions=0) const {
  33681. if (is_empty()) return *this;
  33682. CImg<T> res(_width,_height,_depth,_spectrum);
  33683. _rotate(res,angle,interpolation,boundary_conditions,cx,cy,cx,cy);
  33684. return res;
  33685. }
  33686. // [internal] Perform 2D rotation with arbitrary angle.
  33687. void _rotate(CImg<T>& res, const float angle,
  33688. const unsigned int interpolation, const unsigned int boundary_conditions,
  33689. const float w2, const float h2,
  33690. const float rw2, const float rh2) const {
  33691. const float
  33692. rad = (float)(angle*cimg::PI/180.),
  33693. ca = (float)std::cos(rad), sa = (float)std::sin(rad);
  33694. switch (boundary_conditions) {
  33695. case 3 : { // Mirror
  33696. switch (interpolation) {
  33697. case 2 : { // Cubic interpolation
  33698. const float ww = 2.f*width(), hh = 2.f*height();
  33699. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33700. cimg_forXYZC(res,x,y,z,c) {
  33701. const float xc = x - rw2, yc = y - rh2,
  33702. mx = cimg::mod(w2 + xc*ca + yc*sa,ww),
  33703. my = cimg::mod(h2 - xc*sa + yc*ca,hh);
  33704. res(x,y,z,c) = _cubic_atXY_c(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
  33705. }
  33706. } break;
  33707. case 1 : { // Linear interpolation
  33708. const float ww = 2.f*width(), hh = 2.f*height();
  33709. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33710. cimg_forXYZC(res,x,y,z,c) {
  33711. const float xc = x - rw2, yc = y - rh2,
  33712. mx = cimg::mod(w2 + xc*ca + yc*sa,ww),
  33713. my = cimg::mod(h2 - xc*sa + yc*ca,hh);
  33714. res(x,y,z,c) = (T)_linear_atXY(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
  33715. }
  33716. } break;
  33717. default : { // Nearest-neighbor interpolation
  33718. const int ww = 2*width(), hh = 2*height();
  33719. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33720. cimg_forXYZC(res,x,y,z,c) {
  33721. const float xc = x - rw2, yc = y - rh2,
  33722. mx = cimg::mod((int)cimg::round(w2 + xc*ca + yc*sa),ww),
  33723. my = cimg::mod((int)cimg::round(h2 - xc*sa + yc*ca),hh);
  33724. res(x,y,z,c) = (*this)(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
  33725. }
  33726. }
  33727. }
  33728. } break;
  33729. case 2 : // Periodic
  33730. switch (interpolation) {
  33731. case 2 : { // Cubic interpolation
  33732. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33733. cimg_forXYZC(res,x,y,z,c) {
  33734. const float xc = x - rw2, yc = y - rh2;
  33735. res(x,y,z,c) = _cubic_atXY_pc(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
  33736. }
  33737. } break;
  33738. case 1 : { // Linear interpolation
  33739. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33740. cimg_forXYZC(res,x,y,z,c) {
  33741. const float xc = x - rw2, yc = y - rh2;
  33742. res(x,y,z,c) = (T)_linear_atXY_p(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
  33743. }
  33744. } break;
  33745. default : { // Nearest-neighbor interpolation
  33746. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33747. cimg_forXYZC(res,x,y,z,c) {
  33748. const float xc = x - rw2, yc = y - rh2;
  33749. res(x,y,z,c) = (*this)(cimg::mod((int)cimg::round(w2 + xc*ca + yc*sa),(float)width()),
  33750. cimg::mod((int)cimg::round(h2 - xc*sa + yc*ca),(float)height()),z,c);
  33751. }
  33752. }
  33753. } break;
  33754. case 1 : // Neumann
  33755. switch (interpolation) {
  33756. case 2 : { // Cubic interpolation
  33757. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33758. cimg_forXYZC(res,x,y,z,c) {
  33759. const float xc = x - rw2, yc = y - rh2;
  33760. res(x,y,z,c) = _cubic_atXY_c(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
  33761. }
  33762. } break;
  33763. case 1 : { // Linear interpolation
  33764. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33765. cimg_forXYZC(res,x,y,z,c) {
  33766. const float xc = x - rw2, yc = y - rh2;
  33767. res(x,y,z,c) = (T)_linear_atXY(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
  33768. }
  33769. } break;
  33770. default : { // Nearest-neighbor interpolation
  33771. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33772. cimg_forXYZC(res,x,y,z,c) {
  33773. const float xc = x - rw2, yc = y - rh2;
  33774. res(x,y,z,c) = _atXY((int)cimg::round(w2 + xc*ca + yc*sa),
  33775. (int)cimg::round(h2 - xc*sa + yc*ca),z,c);
  33776. }
  33777. }
  33778. } break;
  33779. default : // Dirichlet
  33780. switch (interpolation) {
  33781. case 2 : { // Cubic interpolation
  33782. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33783. cimg_forXYZC(res,x,y,z,c) {
  33784. const float xc = x - rw2, yc = y - rh2;
  33785. res(x,y,z,c) = cubic_atXY_c(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c,(T)0);
  33786. }
  33787. } break;
  33788. case 1 : { // Linear interpolation
  33789. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33790. cimg_forXYZC(res,x,y,z,c) {
  33791. const float xc = x - rw2, yc = y - rh2;
  33792. res(x,y,z,c) = (T)linear_atXY(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c,(T)0);
  33793. }
  33794. } break;
  33795. default : { // Nearest-neighbor interpolation
  33796. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
  33797. cimg_forXYZC(res,x,y,z,c) {
  33798. const float xc = x - rw2, yc = y - rh2;
  33799. res(x,y,z,c) = atXY((int)cimg::round(w2 + xc*ca + yc*sa),
  33800. (int)cimg::round(h2 - xc*sa + yc*ca),z,c,(T)0);
  33801. }
  33802. }
  33803. }
  33804. }
  33805. }
  33806. //! Rotate volumetric image with arbitrary angle and axis.
  33807. /**
  33808. \param u X-coordinate of the 3D rotation axis.
  33809. \param v Y-coordinate of the 3D rotation axis.
  33810. \param w Z-coordinate of the 3D rotation axis.
  33811. \param angle Rotation angle, in degrees.
  33812. \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
  33813. \param boundary_conditions Boundary conditions.
  33814. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  33815. \note Most of the time, size of the image is modified.
  33816. **/
  33817. CImg<T> rotate(const float u, const float v, const float w, const float angle,
  33818. const unsigned int interpolation, const unsigned int boundary_conditions) {
  33819. const float nangle = cimg::mod(angle,360.f);
  33820. if (nangle==0.f) return *this;
  33821. return get_rotate(u,v,w,nangle,interpolation,boundary_conditions).move_to(*this);
  33822. }
  33823. //! Rotate volumetric image with arbitrary angle and axis \newinstance.
  33824. CImg<T> get_rotate(const float u, const float v, const float w, const float angle,
  33825. const unsigned int interpolation, const unsigned int boundary_conditions) const {
  33826. if (is_empty()) return *this;
  33827. CImg<T> res;
  33828. const float
  33829. w1 = _width - 1, h1 = _height - 1, d1 = _depth -1,
  33830. w2 = 0.5f*w1, h2 = 0.5f*h1, d2 = 0.5f*d1;
  33831. CImg<floatT> R = CImg<floatT>::rotation_matrix(u,v,w,angle);
  33832. const CImg<Tfloat>
  33833. X = R*CImg<Tfloat>(8,3,1,1,
  33834. 0.f,w1,w1,0.f,0.f,w1,w1,0.f,
  33835. 0.f,0.f,h1,h1,0.f,0.f,h1,h1,
  33836. 0.f,0.f,0.f,0.f,d1,d1,d1,d1);
  33837. float
  33838. xm, xM = X.get_shared_row(0).max_min(xm),
  33839. ym, yM = X.get_shared_row(1).max_min(ym),
  33840. zm, zM = X.get_shared_row(2).max_min(zm);
  33841. const int
  33842. dx = (int)cimg::round(xM - xm),
  33843. dy = (int)cimg::round(yM - ym),
  33844. dz = (int)cimg::round(zM - zm);
  33845. R.transpose();
  33846. res.assign(1 + dx,1 + dy,1 + dz,_spectrum);
  33847. const float rw2 = 0.5f*dx, rh2 = 0.5f*dy, rd2 = 0.5f*dz;
  33848. _rotate(res,R,interpolation,boundary_conditions,w2,h2,d2,rw2,rh2,rd2);
  33849. return res;
  33850. }
  33851. //! Rotate volumetric image with arbitrary angle and axis, around a center point.
  33852. /**
  33853. \param u X-coordinate of the 3D rotation axis.
  33854. \param v Y-coordinate of the 3D rotation axis.
  33855. \param w Z-coordinate of the 3D rotation axis.
  33856. \param angle Rotation angle, in degrees.
  33857. \param cx X-coordinate of the rotation center.
  33858. \param cy Y-coordinate of the rotation center.
  33859. \param cz Z-coordinate of the rotation center.
  33860. \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic | 3=mirror }</tt>.
  33861. \param boundary_conditions Boundary conditions.
  33862. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic }</tt>.
  33863. \note Most of the time, size of the image is modified.
  33864. **/
  33865. CImg<T> rotate(const float u, const float v, const float w, const float angle,
  33866. const float cx, const float cy, const float cz,
  33867. const unsigned int interpolation=1, const unsigned int boundary_conditions=0) {
  33868. const float nangle = cimg::mod(angle,360.f);
  33869. if (nangle==0.f) return *this;
  33870. return get_rotate(u,v,w,nangle,cx,cy,cz,interpolation,boundary_conditions).move_to(*this);
  33871. }
  33872. //! Rotate volumetric image with arbitrary angle and axis, around a center point \newinstance.
  33873. CImg<T> get_rotate(const float u, const float v, const float w, const float angle,
  33874. const float cx, const float cy, const float cz,
  33875. const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const {
  33876. if (is_empty()) return *this;
  33877. CImg<T> res(_width,_height,_depth,_spectrum);
  33878. CImg<floatT> R = CImg<floatT>::rotation_matrix(u,v,w,-angle);
  33879. _rotate(res,R,interpolation,boundary_conditions,cx,cy,cz,cx,cy,cz);
  33880. return res;
  33881. }
  33882. // [internal] Perform 3D rotation with arbitrary axis and angle.
  33883. void _rotate(CImg<T>& res, const CImg<Tfloat>& R,
  33884. const unsigned int interpolation, const unsigned int boundary_conditions,
  33885. const float w2, const float h2, const float d2,
  33886. const float rw2, const float rh2, const float rd2) const {
  33887. switch (boundary_conditions) {
  33888. case 3 : // Mirror
  33889. switch (interpolation) {
  33890. case 2 : { // Cubic interpolation
  33891. const float ww = 2.f*width(), hh = 2.f*height(), dd = 2.f*depth();
  33892. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33893. cimg_forXYZ(res,x,y,z) {
  33894. const float
  33895. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33896. X = cimg::mod((float)(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
  33897. Y = cimg::mod((float)(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
  33898. Z = cimg::mod((float)(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
  33899. cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_c(X<width()?X:ww - X - 1,
  33900. Y<height()?Y:hh - Y - 1,
  33901. Z<depth()?Z:dd - Z - z,c);
  33902. }
  33903. } break;
  33904. case 1 : { // Linear interpolation
  33905. const float ww = 2.f*width(), hh = 2.f*height(), dd = 2.f*depth();
  33906. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33907. cimg_forXYZ(res,x,y,z) {
  33908. const float
  33909. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33910. X = cimg::mod((float)(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
  33911. Y = cimg::mod((float)(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
  33912. Z = cimg::mod((float)(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
  33913. cimg_forC(res,c) res(x,y,z,c) = (T)_linear_atXYZ(X<width()?X:ww - X - 1,
  33914. Y<height()?Y:hh - Y - 1,
  33915. Z<depth()?Z:dd - Z - 1,c);
  33916. }
  33917. } break;
  33918. default : { // Nearest-neighbor interpolation
  33919. const int ww = 2*width(), hh = 2*height(), dd = 2*depth();
  33920. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33921. cimg_forXYZ(res,x,y,z) {
  33922. const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
  33923. const int
  33924. X = cimg::mod((int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
  33925. Y = cimg::mod((int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
  33926. Z = cimg::mod((int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
  33927. cimg_forC(res,c) res(x,y,z,c) = (*this)(X<width()?X:ww - X - 1,
  33928. Y<height()?Y:hh - Y - 1,
  33929. Z<depth()?Z:dd - Z - 1,c);
  33930. }
  33931. }
  33932. } break;
  33933. case 2 : // Periodic
  33934. switch (interpolation) {
  33935. case 2 : { // Cubic interpolation
  33936. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33937. cimg_forXYZ(res,x,y,z) {
  33938. const float
  33939. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33940. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  33941. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  33942. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  33943. cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_pc(X,Y,Z,c);
  33944. }
  33945. } break;
  33946. case 1 : { // Linear interpolation
  33947. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33948. cimg_forXYZ(res,x,y,z) {
  33949. const float
  33950. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33951. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  33952. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  33953. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  33954. cimg_forC(res,c) res(x,y,z,c) = (T)_linear_atXYZ_p(X,Y,Z,c);
  33955. }
  33956. } break;
  33957. default : { // Nearest-neighbor interpolation
  33958. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33959. cimg_forXYZ(res,x,y,z) {
  33960. const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
  33961. const int
  33962. X = cimg::mod((int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),width()),
  33963. Y = cimg::mod((int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),height()),
  33964. Z = cimg::mod((int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),depth());
  33965. cimg_forC(res,c) res(x,y,z,c) = (*this)(X,Y,Z,c);
  33966. }
  33967. }
  33968. } break;
  33969. case 1 : // Neumann
  33970. switch (interpolation) {
  33971. case 2 : { // Cubic interpolation
  33972. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33973. cimg_forXYZ(res,x,y,z) {
  33974. const float
  33975. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33976. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  33977. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  33978. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  33979. cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_c(X,Y,Z,c);
  33980. }
  33981. } break;
  33982. case 1 : { // Linear interpolation
  33983. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33984. cimg_forXYZ(res,x,y,z) {
  33985. const float
  33986. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  33987. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  33988. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  33989. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  33990. cimg_forC(res,c) res(x,y,z,c) = _linear_atXYZ(X,Y,Z,c);
  33991. }
  33992. } break;
  33993. default : { // Nearest-neighbor interpolation
  33994. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  33995. cimg_forXYZ(res,x,y,z) {
  33996. const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
  33997. const int
  33998. X = (int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),
  33999. Y = (int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),
  34000. Z = (int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc);
  34001. cimg_forC(res,c) res(x,y,z,c) = _atXYZ(X,Y,Z,c);
  34002. }
  34003. }
  34004. } break;
  34005. default : // Dirichlet
  34006. switch (interpolation) {
  34007. case 2 : { // Cubic interpolation
  34008. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  34009. cimg_forXYZ(res,x,y,z) {
  34010. const float
  34011. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  34012. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  34013. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  34014. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  34015. cimg_forC(res,c) res(x,y,z,c) = cubic_atXYZ_c(X,Y,Z,c,(T)0);
  34016. }
  34017. } break;
  34018. case 1 : { // Linear interpolation
  34019. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  34020. cimg_forXYZ(res,x,y,z) {
  34021. const float
  34022. xc = x - rw2, yc = y - rh2, zc = z - rd2,
  34023. X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
  34024. Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
  34025. Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
  34026. cimg_forC(res,c) res(x,y,z,c) = linear_atXYZ(X,Y,Z,c,(T)0);
  34027. }
  34028. } break;
  34029. default : { // Nearest-neighbor interpolation
  34030. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
  34031. cimg_forXYZ(res,x,y,z) {
  34032. const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
  34033. const int
  34034. X = (int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),
  34035. Y = (int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),
  34036. Z = (int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc);
  34037. cimg_forC(res,c) res(x,y,z,c) = atXYZ(X,Y,Z,c,(T)0);
  34038. }
  34039. }
  34040. } break;
  34041. }
  34042. }
  34043. //! Warp image content by a warping field.
  34044. /**
  34045. \param warp Warping field.
  34046. \param mode Can be { 0=backward-absolute | 1=backward-relative | 2=forward-absolute | 3=foward-relative }
  34047. \param interpolation Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
  34048. \param boundary_conditions Boundary conditions <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  34049. **/
  34050. template<typename t>
  34051. CImg<T>& warp(const CImg<t>& p_warp, const unsigned int mode=0,
  34052. const unsigned int interpolation=1, const unsigned int boundary_conditions=0) {
  34053. return get_warp(p_warp,mode,interpolation,boundary_conditions).move_to(*this);
  34054. }
  34055. //! Warp image content by a warping field \newinstance
  34056. template<typename t>
  34057. CImg<T> get_warp(const CImg<t>& p_warp, const unsigned int mode=0,
  34058. const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const {
  34059. if (is_empty() || !p_warp) return *this;
  34060. if (mode && !is_sameXYZ(p_warp))
  34061. throw CImgArgumentException(_cimg_instance
  34062. "warp(): Instance and specified relative warping field (%u,%u,%u,%u,%p) "
  34063. "have different XYZ dimensions.",
  34064. cimg_instance,
  34065. p_warp._width,p_warp._height,p_warp._depth,p_warp._spectrum,p_warp._data);
  34066. CImg<T> res(p_warp._width,p_warp._height,p_warp._depth,_spectrum);
  34067. if (p_warp._spectrum==1) { // 1D warping
  34068. if (mode>=3) { // Forward-relative warp
  34069. res.fill((T)0);
  34070. if (interpolation>=1) // Linear interpolation
  34071. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34072. cimg_forYZC(res,y,z,c) {
  34073. const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
  34074. cimg_forX(res,x) res.set_linear_atX(*(ptrs++),x + (float)*(ptrs0++),y,z,c);
  34075. }
  34076. else // Nearest-neighbor interpolation
  34077. cimg_forYZC(res,y,z,c) {
  34078. const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
  34079. cimg_forX(res,x) {
  34080. const int X = x + (int)cimg::round(*(ptrs0++));
  34081. if (X>=0 && X<width()) res(X,y,z,c) = *(ptrs++);
  34082. }
  34083. }
  34084. } else if (mode==2) { // Forward-absolute warp
  34085. res.fill((T)0);
  34086. if (interpolation>=1) // Linear interpolation
  34087. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34088. cimg_forYZC(res,y,z,c) {
  34089. const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
  34090. cimg_forX(res,x) res.set_linear_atX(*(ptrs++),(float)*(ptrs0++),y,z,c);
  34091. }
  34092. else // Nearest-neighbor interpolation
  34093. cimg_forYZC(res,y,z,c) {
  34094. const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
  34095. cimg_forX(res,x) {
  34096. const int X = (int)cimg::round(*(ptrs0++));
  34097. if (X>=0 && X<width()) res(X,y,z,c) = *(ptrs++);
  34098. }
  34099. }
  34100. } else if (mode==1) { // Backward-relative warp
  34101. if (interpolation==2) // Cubic interpolation
  34102. switch (boundary_conditions) {
  34103. case 3 : { // Mirror
  34104. const float w2 = 2.f*width();
  34105. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34106. cimg_forYZC(res,y,z,c) {
  34107. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34108. cimg_forX(res,x) {
  34109. const float mx = cimg::mod(x - (float)*(ptrs0++),w2);
  34110. *(ptrd++) = _cubic_atX_c(mx<width()?mx:w2 - mx - 1,y,z,c);
  34111. }
  34112. }
  34113. } break;
  34114. case 2 : // Periodic
  34115. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34116. cimg_forYZC(res,y,z,c) {
  34117. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34118. cimg_forX(res,x) *(ptrd++) = _cubic_atX_pc(x - (float)*(ptrs0++),y,z,c);
  34119. }
  34120. break;
  34121. case 1 : // Neumann
  34122. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34123. cimg_forYZC(res,y,z,c) {
  34124. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34125. cimg_forX(res,x) *(ptrd++) = _cubic_atX_c(x - (float)*(ptrs0++),y,z,c);
  34126. }
  34127. break;
  34128. default : // Dirichlet
  34129. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34130. cimg_forYZC(res,y,z,c) {
  34131. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34132. cimg_forX(res,x) *(ptrd++) = cubic_atX_c(x - (float)*(ptrs0++),y,z,c,(T)0);
  34133. }
  34134. }
  34135. else if (interpolation==1) // Linear interpolation
  34136. switch (boundary_conditions) {
  34137. case 3 : { // Mirror
  34138. const float w2 = 2.f*width();
  34139. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34140. cimg_forYZC(res,y,z,c) {
  34141. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34142. cimg_forX(res,x) {
  34143. const float mx = cimg::mod(x - (float)*(ptrs0++),w2);
  34144. *(ptrd++) = (T)_linear_atX(mx<width()?mx:w2 - mx - 1,y,z,c);
  34145. }
  34146. }
  34147. } break;
  34148. case 2 : // Periodic
  34149. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34150. cimg_forYZC(res,y,z,c) {
  34151. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34152. cimg_forX(res,x) *(ptrd++) = (T)_linear_atX_p(x - (float)*(ptrs0++),y,z,c);
  34153. }
  34154. break;
  34155. case 1 : // Neumann
  34156. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34157. cimg_forYZC(res,y,z,c) {
  34158. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34159. cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(x - (float)*(ptrs0++),y,z,c);
  34160. }
  34161. break;
  34162. default : // Dirichlet
  34163. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34164. cimg_forYZC(res,y,z,c) {
  34165. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34166. cimg_forX(res,x) *(ptrd++) = (T)linear_atX(x - (float)*(ptrs0++),y,z,c,(T)0);
  34167. }
  34168. }
  34169. else // Nearest-neighbor interpolation
  34170. switch (boundary_conditions) {
  34171. case 3 : { // Mirror
  34172. const int w2 = 2*width();
  34173. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34174. cimg_forYZC(res,y,z,c) {
  34175. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34176. cimg_forX(res,x) {
  34177. const int mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2);
  34178. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,y,z,c);
  34179. }
  34180. }
  34181. } break;
  34182. case 2 : // Periodic
  34183. cimg_forYZC(res,y,z,c) {
  34184. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34185. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),y,z,c);
  34186. }
  34187. break;
  34188. case 1 : // Neumann
  34189. cimg_forYZC(res,y,z,c) {
  34190. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34191. cimg_forX(res,x) *(ptrd++) = _atX(x - (int)cimg::round(*(ptrs0++)),y,z,c);
  34192. }
  34193. break;
  34194. default : // Dirichlet
  34195. cimg_forYZC(res,y,z,c) {
  34196. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34197. cimg_forX(res,x) *(ptrd++) = atX(x - (int)cimg::round(*(ptrs0++)),y,z,c,(T)0);
  34198. }
  34199. }
  34200. }
  34201. else { // Backward-absolute warp
  34202. if (interpolation==2) // Cubic interpolation
  34203. switch (boundary_conditions) {
  34204. case 3 : { // Mirror
  34205. const float w2 = 2.f*width();
  34206. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34207. cimg_forYZC(res,y,z,c) {
  34208. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34209. cimg_forX(res,x) {
  34210. const float mx = cimg::mod((float)*(ptrs0++),w2);
  34211. *(ptrd++) = _cubic_atX_c(mx<width()?mx:w2 - mx - 1,0,0,c);
  34212. }
  34213. }
  34214. } break;
  34215. case 2 : // Periodic
  34216. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34217. cimg_forYZC(res,y,z,c) {
  34218. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34219. cimg_forX(res,x) *(ptrd++) = _cubic_atX_pc((float)*(ptrs0++),0,0,c);
  34220. }
  34221. break;
  34222. case 1 : // Neumann
  34223. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34224. cimg_forYZC(res,y,z,c) {
  34225. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34226. cimg_forX(res,x) *(ptrd++) = _cubic_atX_c((float)*(ptrs0++),0,0,c);
  34227. }
  34228. break;
  34229. default : // Dirichlet
  34230. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34231. cimg_forYZC(res,y,z,c) {
  34232. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34233. cimg_forX(res,x) *(ptrd++) = cubic_atX_c((float)*(ptrs0++),0,0,c,(T)0);
  34234. }
  34235. }
  34236. else if (interpolation==1) // Linear interpolation
  34237. switch (boundary_conditions) {
  34238. case 3 : { // Mirror
  34239. const float w2 = 2.f*width();
  34240. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34241. cimg_forYZC(res,y,z,c) {
  34242. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34243. cimg_forX(res,x) {
  34244. const float mx = cimg::mod((float)*(ptrs0++),w2);
  34245. *(ptrd++) = (T)_linear_atX(mx<width()?mx:w2 - mx - 1,0,0,c);
  34246. }
  34247. }
  34248. } break;
  34249. case 2 : // Periodic
  34250. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34251. cimg_forYZC(res,y,z,c) {
  34252. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34253. cimg_forX(res,x) *(ptrd++) = (T)_linear_atX_p((float)*(ptrs0++),0,0,c);
  34254. }
  34255. break;
  34256. case 1 : // Neumann
  34257. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34258. cimg_forYZC(res,y,z,c) {
  34259. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34260. cimg_forX(res,x) *(ptrd++) = (T)_linear_atX((float)*(ptrs0++),0,0,c);
  34261. }
  34262. break;
  34263. default : // Dirichlet
  34264. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34265. cimg_forYZC(res,y,z,c) {
  34266. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34267. cimg_forX(res,x) *(ptrd++) = (T)linear_atX((float)*(ptrs0++),0,0,c,(T)0);
  34268. }
  34269. }
  34270. else // Nearest-neighbor interpolation
  34271. switch (boundary_conditions) {
  34272. case 3 : { // Mirror
  34273. const int w2 = 2*width();
  34274. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34275. cimg_forYZC(res,y,z,c) {
  34276. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34277. cimg_forX(res,x) {
  34278. const int mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2);
  34279. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,0,0,c);
  34280. }
  34281. }
  34282. } break;
  34283. case 2 : // Periodic
  34284. cimg_forYZC(res,y,z,c) {
  34285. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34286. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),0,0,c);
  34287. }
  34288. break;
  34289. case 1 : // Neumann
  34290. cimg_forYZC(res,y,z,c) {
  34291. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34292. cimg_forX(res,x) *(ptrd++) = _atX((int)cimg::round(*(ptrs0++)),0,0,c);
  34293. }
  34294. break;
  34295. default : // Dirichlet
  34296. cimg_forYZC(res,y,z,c) {
  34297. const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
  34298. cimg_forX(res,x) *(ptrd++) = atX((int)cimg::round(*(ptrs0++)),0,0,c,(T)0);
  34299. }
  34300. }
  34301. }
  34302. } else if (p_warp._spectrum==2) { // 2D warping
  34303. if (mode>=3) { // Forward-relative warp
  34304. res.fill((T)0);
  34305. if (interpolation>=1) // Linear interpolation
  34306. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34307. cimg_forYZC(res,y,z,c) {
  34308. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); const T *ptrs = data(0,y,z,c);
  34309. cimg_forX(res,x) res.set_linear_atXY(*(ptrs++),x + (float)*(ptrs0++),y + (float)*(ptrs1++),z,c);
  34310. }
  34311. else // Nearest-neighbor interpolation
  34312. cimg_forYZC(res,y,z,c) {
  34313. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); const T *ptrs = data(0,y,z,c);
  34314. cimg_forX(res,x) {
  34315. const int X = x + (int)cimg::round(*(ptrs0++)), Y = y + (int)cimg::round(*(ptrs1++));
  34316. if (X>=0 && X<width() && Y>=0 && Y<height()) res(X,Y,z,c) = *(ptrs++);
  34317. }
  34318. }
  34319. } else if (mode==2) { // Forward-absolute warp
  34320. res.fill((T)0);
  34321. if (interpolation>=1) // Linear interpolation
  34322. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34323. cimg_forYZC(res,y,z,c) {
  34324. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); const T *ptrs = data(0,y,z,c);
  34325. cimg_forX(res,x) res.set_linear_atXY(*(ptrs++),(float)*(ptrs0++),(float)*(ptrs1++),z,c);
  34326. }
  34327. else // Nearest-neighbor interpolation
  34328. cimg_forYZC(res,y,z,c) {
  34329. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); const T *ptrs = data(0,y,z,c);
  34330. cimg_forX(res,x) {
  34331. const int X = (int)cimg::round(*(ptrs0++)), Y = (int)cimg::round(*(ptrs1++));
  34332. if (X>=0 && X<width() && Y>=0 && Y<height()) res(X,Y,z,c) = *(ptrs++);
  34333. }
  34334. }
  34335. } else if (mode==1) { // Backward-relative warp
  34336. if (interpolation==2) // Cubic interpolation
  34337. switch (boundary_conditions) {
  34338. case 3 : { // Mirror
  34339. const float w2 = 2.f*width(), h2 = 2.f*height();
  34340. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34341. cimg_forYZC(res,y,z,c) {
  34342. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34343. cimg_forX(res,x) {
  34344. const float
  34345. mx = cimg::mod(x - (float)*(ptrs0++),w2),
  34346. my = cimg::mod(y - (float)*(ptrs1++),h2);
  34347. *(ptrd++) = _cubic_atXY_c(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
  34348. }
  34349. }
  34350. } break;
  34351. case 2 : // Periodic
  34352. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34353. cimg_forYZC(res,y,z,c) {
  34354. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34355. cimg_forX(res,x) *(ptrd++) = _cubic_atXY_pc(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
  34356. }
  34357. break;
  34358. case 1 : // Neumann
  34359. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34360. cimg_forYZC(res,y,z,c) {
  34361. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34362. cimg_forX(res,x) *(ptrd++) = _cubic_atXY_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
  34363. }
  34364. break;
  34365. default : // Dirichlet
  34366. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34367. cimg_forYZC(res,y,z,c) {
  34368. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34369. cimg_forX(res,x) *(ptrd++) = cubic_atXY_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,(T)0);
  34370. }
  34371. }
  34372. else if (interpolation==1) // Linear interpolation
  34373. switch (boundary_conditions) {
  34374. case 3 : { // Mirror
  34375. const float w2 = 2.f*width(), h2 = 2.f*height();
  34376. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34377. cimg_forYZC(res,y,z,c) {
  34378. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34379. cimg_forX(res,x) {
  34380. const float
  34381. mx = cimg::mod(x - (float)*(ptrs0++),w2),
  34382. my = cimg::mod(y - (float)*(ptrs1++),h2);
  34383. *(ptrd++) = (T)_linear_atXY(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
  34384. }
  34385. }
  34386. } break;
  34387. case 2 : // Periodic
  34388. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34389. cimg_forYZC(res,y,z,c) {
  34390. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34391. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY_p(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
  34392. }
  34393. break;
  34394. case 1 : // Neumann
  34395. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34396. cimg_forYZC(res,y,z,c) {
  34397. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34398. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
  34399. }
  34400. break;
  34401. default : // Dirichlet
  34402. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34403. cimg_forYZC(res,y,z,c) {
  34404. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34405. cimg_forX(res,x) *(ptrd++) = (T)linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,(T)0);
  34406. }
  34407. }
  34408. else // Nearest-neighbor interpolation
  34409. switch (boundary_conditions) {
  34410. case 3 : { // Mirror
  34411. const int w2 = 2*width(), h2 = 2*height();
  34412. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34413. cimg_forYZC(res,y,z,c) {
  34414. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34415. cimg_forX(res,x) {
  34416. const int
  34417. mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2),
  34418. my = cimg::mod(y - (int)cimg::round(*(ptrs1++)),h2);
  34419. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
  34420. }
  34421. }
  34422. } break;
  34423. case 2 : // Periodic
  34424. cimg_forYZC(res,y,z,c) {
  34425. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34426. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),
  34427. cimg::mod(y - (int)cimg::round(*(ptrs1++)),height()),z,c);
  34428. }
  34429. break;
  34430. case 1 : // Neumann
  34431. cimg_forYZC(res,y,z,c) {
  34432. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34433. cimg_forX(res,x) *(ptrd++) = _atXY(x - (int)cimg::round(*(ptrs0++)),
  34434. y - (int)cimg::round(*(ptrs1++)),z,c);
  34435. }
  34436. break;
  34437. default : // Dirichlet
  34438. cimg_forYZC(res,y,z,c) {
  34439. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34440. cimg_forX(res,x) *(ptrd++) = atXY(x - (int)cimg::round(*(ptrs0++)),
  34441. y - (int)cimg::round(*(ptrs1++)),z,c,(T)0);
  34442. }
  34443. }
  34444. } else { // Backward-absolute warp
  34445. if (interpolation==2) // Cubic interpolation
  34446. switch (boundary_conditions) {
  34447. case 3 : { // Mirror
  34448. const float w2 = 2.f*width(), h2 = 2.f*height();
  34449. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34450. cimg_forYZC(res,y,z,c) {
  34451. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34452. cimg_forX(res,x) {
  34453. const float
  34454. mx = cimg::mod((float)*(ptrs0++),w2),
  34455. my = cimg::mod((float)*(ptrs1++),h2);
  34456. *(ptrd++) = _cubic_atXY_c(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
  34457. }
  34458. }
  34459. } break;
  34460. case 2 : // Periodic
  34461. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34462. cimg_forYZC(res,y,z,c) {
  34463. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34464. cimg_forX(res,x) *(ptrd++) = _cubic_atXY_pc((float)*(ptrs0++),(float)*(ptrs1++),0,c);
  34465. }
  34466. break;
  34467. case 1 : // Neumann
  34468. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34469. cimg_forYZC(res,y,z,c) {
  34470. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34471. cimg_forX(res,x) *(ptrd++) = _cubic_atXY_c((float)*(ptrs0++),(float)*(ptrs1++),0,c);
  34472. }
  34473. break;
  34474. default : // Dirichlet
  34475. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34476. cimg_forYZC(res,y,z,c) {
  34477. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34478. cimg_forX(res,x) *(ptrd++) = cubic_atXY_c((float)*(ptrs0++),(float)*(ptrs1++),0,c,(T)0);
  34479. }
  34480. }
  34481. else if (interpolation==1) // Linear interpolation
  34482. switch (boundary_conditions) {
  34483. case 3 : { // Mirror
  34484. const float w2 = 2.f*width(), h2 = 2.f*height();
  34485. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34486. cimg_forYZC(res,y,z,c) {
  34487. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34488. cimg_forX(res,x) {
  34489. const float
  34490. mx = cimg::mod((float)*(ptrs0++),w2),
  34491. my = cimg::mod((float)*(ptrs1++),h2);
  34492. *(ptrd++) = (T)_linear_atXY(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
  34493. }
  34494. }
  34495. } break;
  34496. case 2 : // Periodic
  34497. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34498. cimg_forYZC(res,y,z,c) {
  34499. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34500. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY_p((float)*(ptrs0++),(float)*(ptrs1++),0,c);
  34501. }
  34502. break;
  34503. case 1 : // Neumann
  34504. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34505. cimg_forYZC(res,y,z,c) {
  34506. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34507. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c);
  34508. }
  34509. break;
  34510. default : // Dirichlet
  34511. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34512. cimg_forYZC(res,y,z,c) {
  34513. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34514. cimg_forX(res,x) *(ptrd++) = (T)linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,(T)0);
  34515. }
  34516. }
  34517. else // Nearest-neighbor interpolation
  34518. switch (boundary_conditions) {
  34519. case 3 : { // Mirror
  34520. const int w2 = 2*width(), h2 = 2*height();
  34521. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34522. cimg_forYZC(res,y,z,c) {
  34523. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34524. cimg_forX(res,x) {
  34525. const int
  34526. mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2),
  34527. my = cimg::mod((int)cimg::round(*(ptrs1++)),h2);
  34528. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
  34529. }
  34530. }
  34531. } break;
  34532. case 2 : // Periodic
  34533. cimg_forYZC(res,y,z,c) {
  34534. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34535. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),
  34536. cimg::mod((int)cimg::round(*(ptrs1++)),height()),0,c);
  34537. }
  34538. break;
  34539. case 1 : // Neumann
  34540. cimg_forYZC(res,y,z,c) {
  34541. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34542. cimg_forX(res,x) *(ptrd++) = _atXY((int)cimg::round(*(ptrs0++)),
  34543. (int)cimg::round(*(ptrs1++)),0,c);
  34544. }
  34545. break;
  34546. default : // Dirichlet
  34547. cimg_forYZC(res,y,z,c) {
  34548. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1); T *ptrd = res.data(0,y,z,c);
  34549. cimg_forX(res,x) *(ptrd++) = atXY((int)cimg::round(*(ptrs0++)),
  34550. (int)cimg::round(*(ptrs1++)),0,c,(T)0);
  34551. }
  34552. }
  34553. }
  34554. } else { // 3D warping
  34555. if (mode>=3) { // Forward-relative warp
  34556. res.fill((T)0);
  34557. if (interpolation>=1) // Linear interpolation
  34558. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34559. cimg_forYZC(res,y,z,c) {
  34560. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34561. const T *ptrs = data(0,y,z,c);
  34562. cimg_forX(res,x) res.set_linear_atXYZ(*(ptrs++),x + (float)*(ptrs0++),y + (float)*(ptrs1++),
  34563. z + (float)*(ptrs2++),c);
  34564. }
  34565. else // Nearest-neighbor interpolation
  34566. cimg_forYZC(res,y,z,c) {
  34567. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34568. const T *ptrs = data(0,y,z,c);
  34569. cimg_forX(res,x) {
  34570. const int
  34571. X = x + (int)cimg::round(*(ptrs0++)),
  34572. Y = y + (int)cimg::round(*(ptrs1++)),
  34573. Z = z + (int)cimg::round(*(ptrs2++));
  34574. if (X>=0 && X<width() && Y>=0 && Y<height() && Z>=0 && Z<depth()) res(X,Y,Z,c) = *(ptrs++);
  34575. }
  34576. }
  34577. } else if (mode==2) { // Forward-absolute warp
  34578. res.fill((T)0);
  34579. if (interpolation>=1) // Linear interpolation
  34580. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34581. cimg_forYZC(res,y,z,c) {
  34582. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34583. const T *ptrs = data(0,y,z,c);
  34584. cimg_forX(res,x) res.set_linear_atXYZ(*(ptrs++),(float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
  34585. }
  34586. else // Nearest-neighbor interpolation
  34587. cimg_forYZC(res,y,z,c) {
  34588. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34589. const T *ptrs = data(0,y,z,c);
  34590. cimg_forX(res,x) {
  34591. const int
  34592. X = (int)cimg::round(*(ptrs0++)),
  34593. Y = (int)cimg::round(*(ptrs1++)),
  34594. Z = (int)cimg::round(*(ptrs2++));
  34595. if (X>=0 && X<width() && Y>=0 && Y<height() && Z>=0 && Z<depth()) res(X,Y,Z,c) = *(ptrs++);
  34596. }
  34597. }
  34598. } else if (mode==1) { // Backward-relative warp
  34599. if (interpolation==2) // Cubic interpolation
  34600. switch (boundary_conditions) {
  34601. case 3 : { // Mirror
  34602. const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
  34603. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34604. cimg_forYZC(res,y,z,c) {
  34605. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34606. T *ptrd = res.data(0,y,z,c);
  34607. cimg_forX(res,x) {
  34608. const float
  34609. mx = cimg::mod(x - (float)*(ptrs0++),w2),
  34610. my = cimg::mod(y - (float)*(ptrs1++),h2),
  34611. mz = cimg::mod(z - (float)*(ptrs2++),d2);
  34612. *(ptrd++) = _cubic_atXYZ_c(mx<width()?mx:w2 - mx - 1,
  34613. my<height()?my:h2 - my - 1,
  34614. mz<depth()?mz:d2 - mz - 1,c);
  34615. }
  34616. }
  34617. } break;
  34618. case 2 : // Periodic
  34619. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34620. cimg_forYZC(res,y,z,c) {
  34621. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34622. T *ptrd = res.data(0,y,z,c);
  34623. cimg_forX(res,x)
  34624. *(ptrd++) = _cubic_atXYZ_pc(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
  34625. }
  34626. break;
  34627. case 1 : // Neumann
  34628. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34629. cimg_forYZC(res,y,z,c) {
  34630. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34631. T *ptrd = res.data(0,y,z,c);
  34632. cimg_forX(res,x)
  34633. *(ptrd++) = _cubic_atXYZ_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
  34634. }
  34635. break;
  34636. default : // Dirichlet
  34637. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34638. cimg_forYZC(res,y,z,c) {
  34639. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34640. T *ptrd = res.data(0,y,z,c);
  34641. cimg_forX(res,x)
  34642. *(ptrd++) = cubic_atXYZ_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,(T)0);
  34643. }
  34644. }
  34645. else if (interpolation==1) // Linear interpolation
  34646. switch (boundary_conditions) {
  34647. case 3 : { // Mirror
  34648. const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
  34649. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34650. cimg_forYZC(res,y,z,c) {
  34651. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34652. T *ptrd = res.data(0,y,z,c);
  34653. cimg_forX(res,x) {
  34654. const float
  34655. mx = cimg::mod(x - (float)*(ptrs0++),w2),
  34656. my = cimg::mod(y - (float)*(ptrs1++),h2),
  34657. mz = cimg::mod(z - (float)*(ptrs2++),d2);
  34658. *(ptrd++) = (T)_linear_atXYZ(mx<width()?mx:w2 - mx - 1,
  34659. my<height()?my:h2 - my - 1,
  34660. mz<depth()?mz:d2 - mz - 1,c);
  34661. }
  34662. }
  34663. } break;
  34664. case 2 : // Periodic
  34665. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34666. cimg_forYZC(res,y,z,c) {
  34667. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34668. T *ptrd = res.data(0,y,z,c);
  34669. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ_p(x - (float)*(ptrs0++),y - (float)*(ptrs1++),
  34670. z - (float)*(ptrs2++),c);
  34671. }
  34672. break;
  34673. case 1 : // Neumann
  34674. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34675. cimg_forYZC(res,y,z,c) {
  34676. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34677. T *ptrd = res.data(0,y,z,c);
  34678. cimg_forX(res,x)
  34679. *(ptrd++) = (T)_linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
  34680. }
  34681. break;
  34682. default : // Dirichlet
  34683. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34684. cimg_forYZC(res,y,z,c) {
  34685. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34686. T *ptrd = res.data(0,y,z,c);
  34687. cimg_forX(res,x)
  34688. *(ptrd++) = (T)linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,(T)0);
  34689. }
  34690. }
  34691. else // Nearest neighbor interpolation
  34692. switch (boundary_conditions) {
  34693. case 3 : { // Mirror
  34694. const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
  34695. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34696. cimg_forYZC(res,y,z,c) {
  34697. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34698. T *ptrd = res.data(0,y,z,c);
  34699. cimg_forX(res,x) {
  34700. const int
  34701. mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2),
  34702. my = cimg::mod(y - (int)cimg::round(*(ptrs1++)),h2),
  34703. mz = cimg::mod(z - (int)cimg::round(*(ptrs2++)),d2);
  34704. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,
  34705. my<height()?my:h2 - my - 1,
  34706. mz<depth()?mz:d2 - mz - 1,c);
  34707. }
  34708. }
  34709. } break;
  34710. case 2 : // Periodic
  34711. cimg_forYZC(res,y,z,c) {
  34712. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34713. T *ptrd = res.data(0,y,z,c);
  34714. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),
  34715. cimg::mod(y - (int)cimg::round(*(ptrs1++)),height()),
  34716. cimg::mod(z - (int)cimg::round(*(ptrs2++)),depth()),c);
  34717. }
  34718. break;
  34719. case 1 : // Neumann
  34720. cimg_forYZC(res,y,z,c) {
  34721. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34722. T *ptrd = res.data(0,y,z,c);
  34723. cimg_forX(res,x) *(ptrd++) = _atXYZ(x - (int)cimg::round(*(ptrs0++)),
  34724. y - (int)cimg::round(*(ptrs1++)),
  34725. z - (int)cimg::round(*(ptrs2++)),c);
  34726. }
  34727. break;
  34728. default : // Dirichlet
  34729. cimg_forYZC(res,y,z,c) {
  34730. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34731. T *ptrd = res.data(0,y,z,c);
  34732. cimg_forX(res,x) *(ptrd++) = atXYZ(x - (int)cimg::round(*(ptrs0++)),
  34733. y - (int)cimg::round(*(ptrs1++)),
  34734. z - (int)cimg::round(*(ptrs2++)),c,(T)0);
  34735. }
  34736. }
  34737. } else { // Backward-absolute warp
  34738. if (interpolation==2) // Cubic interpolation
  34739. switch (boundary_conditions) {
  34740. case 3 : { // Mirror
  34741. const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
  34742. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34743. cimg_forYZC(res,y,z,c) {
  34744. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34745. T *ptrd = res.data(0,y,z,c);
  34746. cimg_forX(res,x) {
  34747. const float
  34748. mx = cimg::mod((float)*(ptrs0++),w2),
  34749. my = cimg::mod((float)*(ptrs1++),h2),
  34750. mz = cimg::mod((float)*(ptrs2++),d2);
  34751. *(ptrd++) = _cubic_atXYZ_c(mx<width()?mx:w2 - mx - 1,
  34752. my<height()?my:h2 - my - 1,
  34753. mz<depth()?mz:d2 - mz - 1,c);
  34754. }
  34755. }
  34756. } break;
  34757. case 2 : // Periodic
  34758. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34759. cimg_forYZC(res,y,z,c) {
  34760. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34761. T *ptrd = res.data(0,y,z,c);
  34762. cimg_forX(res,x) *(ptrd++) = _cubic_atXYZ_pc((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
  34763. }
  34764. break;
  34765. case 1 : // Neumann
  34766. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34767. cimg_forYZC(res,y,z,c) {
  34768. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34769. T *ptrd = res.data(0,y,z,c);
  34770. cimg_forX(res,x) *(ptrd++) = _cubic_atXYZ_c((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
  34771. }
  34772. break;
  34773. default : // Dirichlet
  34774. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34775. cimg_forYZC(res,y,z,c) {
  34776. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34777. T *ptrd = res.data(0,y,z,c);
  34778. cimg_forX(res,x) *(ptrd++) = cubic_atXYZ_c((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),
  34779. c,(T)0);
  34780. }
  34781. }
  34782. else if (interpolation==1) // Linear interpolation
  34783. switch (boundary_conditions) {
  34784. case 3 : { // Mirror
  34785. const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
  34786. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34787. cimg_forYZC(res,y,z,c) {
  34788. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34789. T *ptrd = res.data(0,y,z,c);
  34790. cimg_forX(res,x) {
  34791. const float
  34792. mx = cimg::mod((float)*(ptrs0++),w2),
  34793. my = cimg::mod((float)*(ptrs1++),h2),
  34794. mz = cimg::mod((float)*(ptrs2++),d2);
  34795. *(ptrd++) = (T)_linear_atXYZ(mx<width()?mx:w2 - mx - 1,
  34796. my<height()?my:h2 - my - 1,
  34797. mz<depth()?mz:d2 - mz - 1,c);
  34798. }
  34799. }
  34800. } break;
  34801. case 2 :// Periodic
  34802. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34803. cimg_forYZC(res,y,z,c) {
  34804. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34805. T *ptrd = res.data(0,y,z,c);
  34806. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ_p((float)*(ptrs0++),(float)*(ptrs1++),
  34807. (float)*(ptrs2++),c);
  34808. }
  34809. break;
  34810. case 1 : // Neumann
  34811. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34812. cimg_forYZC(res,y,z,c) {
  34813. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34814. T *ptrd = res.data(0,y,z,c);
  34815. cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
  34816. }
  34817. break;
  34818. default : // Dirichlet
  34819. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
  34820. cimg_forYZC(res,y,z,c) {
  34821. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34822. T *ptrd = res.data(0,y,z,c);
  34823. cimg_forX(res,x) *(ptrd++) = (T)linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),
  34824. c,(T)0);
  34825. }
  34826. }
  34827. else // Nearest-neighbor interpolation
  34828. switch (boundary_conditions) {
  34829. case 3 : { // Mirror
  34830. const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
  34831. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
  34832. cimg_forYZC(res,y,z,c) {
  34833. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34834. T *ptrd = res.data(0,y,z,c);
  34835. cimg_forX(res,x) {
  34836. const int
  34837. mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2),
  34838. my = cimg::mod((int)cimg::round(*(ptrs1++)),h2),
  34839. mz = cimg::mod((int)cimg::round(*(ptrs2++)),d2);
  34840. *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,
  34841. my<height()?my:h2 - my - 1,
  34842. mz<depth()?mz:d2 - mz - 1,c);
  34843. }
  34844. }
  34845. } break;
  34846. case 2 : // Periodic
  34847. cimg_forYZC(res,y,z,c) {
  34848. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34849. T *ptrd = res.data(0,y,z,c);
  34850. cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),
  34851. cimg::mod((int)cimg::round(*(ptrs1++)),height()),
  34852. cimg::mod((int)cimg::round(*(ptrs2++)),depth()),c);
  34853. }
  34854. break;
  34855. case 1 : // Neumann
  34856. cimg_forYZC(res,y,z,c) {
  34857. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34858. T *ptrd = res.data(0,y,z,c);
  34859. cimg_forX(res,x) *(ptrd++) = _atXYZ((int)cimg::round(*(ptrs0++)),
  34860. (int)cimg::round(*(ptrs1++)),
  34861. (int)cimg::round(*(ptrs2++)),c);
  34862. }
  34863. break;
  34864. default : // Dirichlet
  34865. cimg_forYZC(res,y,z,c) {
  34866. const t *ptrs0 = p_warp.data(0,y,z,0), *ptrs1 = p_warp.data(0,y,z,1), *ptrs2 = p_warp.data(0,y,z,2);
  34867. T *ptrd = res.data(0,y,z,c);
  34868. cimg_forX(res,x) *(ptrd++) = atXYZ((int)cimg::round(*(ptrs0++)),
  34869. (int)cimg::round(*(ptrs1++)),
  34870. (int)cimg::round(*(ptrs2++)),c,(T)0);
  34871. }
  34872. }
  34873. }
  34874. }
  34875. return res;
  34876. }
  34877. //! Generate a 2D representation of a 3D image, with XY,XZ and YZ views.
  34878. /**
  34879. \param x0 X-coordinate of the projection point.
  34880. \param y0 Y-coordinate of the projection point.
  34881. \param z0 Z-coordinate of the projection point.
  34882. **/
  34883. CImg<T> get_projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) const {
  34884. if (is_empty() || _depth<2) return +*this;
  34885. const unsigned int
  34886. _x0 = (x0>=_width)?_width - 1:x0,
  34887. _y0 = (y0>=_height)?_height - 1:y0,
  34888. _z0 = (z0>=_depth)?_depth - 1:z0;
  34889. const CImg<T>
  34890. img_xy = get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1),
  34891. img_zy = get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1).permute_axes("xzyc").
  34892. resize(_depth,_height,1,-100,-1),
  34893. img_xz = get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1).resize(_width,_depth,1,-100,-1);
  34894. return CImg<T>(_width + _depth,_height + _depth,1,_spectrum,cimg::min(img_xy.min(),img_zy.min(),img_xz.min())).
  34895. draw_image(0,0,img_xy).draw_image(img_xy._width,0,img_zy).
  34896. draw_image(0,img_xy._height,img_xz);
  34897. }
  34898. //! Construct a 2D representation of a 3D image, with XY,XZ and YZ views \inplace.
  34899. CImg<T>& projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) {
  34900. if (_depth<2) return *this;
  34901. return get_projections2d(x0,y0,z0).move_to(*this);
  34902. }
  34903. //! Crop image region.
  34904. /**
  34905. \param x0 = X-coordinate of the upper-left crop rectangle corner.
  34906. \param y0 = Y-coordinate of the upper-left crop rectangle corner.
  34907. \param z0 = Z-coordinate of the upper-left crop rectangle corner.
  34908. \param c0 = C-coordinate of the upper-left crop rectangle corner.
  34909. \param x1 = X-coordinate of the lower-right crop rectangle corner.
  34910. \param y1 = Y-coordinate of the lower-right crop rectangle corner.
  34911. \param z1 = Z-coordinate of the lower-right crop rectangle corner.
  34912. \param c1 = C-coordinate of the lower-right crop rectangle corner.
  34913. \param boundary_conditions = Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  34914. **/
  34915. CImg<T>& crop(const int x0, const int y0, const int z0, const int c0,
  34916. const int x1, const int y1, const int z1, const int c1,
  34917. const unsigned int boundary_conditions=0) {
  34918. return get_crop(x0,y0,z0,c0,x1,y1,z1,c1,boundary_conditions).move_to(*this);
  34919. }
  34920. //! Crop image region \newinstance.
  34921. CImg<T> get_crop(const int x0, const int y0, const int z0, const int c0,
  34922. const int x1, const int y1, const int z1, const int c1,
  34923. const unsigned int boundary_conditions=0) const {
  34924. if (is_empty())
  34925. throw CImgInstanceException(_cimg_instance
  34926. "crop(): Empty instance.",
  34927. cimg_instance);
  34928. const int
  34929. nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
  34930. ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0,
  34931. nz0 = z0<z1?z0:z1, nz1 = z0^z1^nz0,
  34932. nc0 = c0<c1?c0:c1, nc1 = c0^c1^nc0;
  34933. const unsigned int
  34934. _boundary_conditions = nx0>=0 && nx1<width() &&
  34935. ny0>=0 && ny1<height() &&
  34936. nz0>=0 && nz1<depth() &&
  34937. nc0>=0 && nc1<spectrum()?0:boundary_conditions;
  34938. CImg<T> res(1U + nx1 - nx0,1U + ny1 - ny0,1U + nz1 - nz0,1U + nc1 - nc0);
  34939. if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height() || nz0<0 || nz1>=depth() || nc0<0 || nc1>=spectrum())
  34940. switch (_boundary_conditions) {
  34941. case 3 : { // Mirror
  34942. const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth(), s2 = 2*spectrum();
  34943. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  34944. _height*_depth*_spectrum>=4))
  34945. cimg_forXYZC(res,x,y,z,c) {
  34946. const int
  34947. mx = cimg::mod(nx0 + x,w2),
  34948. my = cimg::mod(ny0 + y,h2),
  34949. mz = cimg::mod(nz0 + z,d2),
  34950. mc = cimg::mod(nc0 + c,s2);
  34951. res(x,y,z,c) = (*this)(mx<width()?mx:w2 - mx - 1,
  34952. my<height()?my:h2 - my - 1,
  34953. mz<depth()?mz:d2 - mz - 1,
  34954. mc<spectrum()?mc:s2 - mc - 1);
  34955. }
  34956. } break;
  34957. case 2 : { // Periodic
  34958. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  34959. _height*_depth*_spectrum>=4))
  34960. cimg_forXYZC(res,x,y,z,c) {
  34961. res(x,y,z,c) = (*this)(cimg::mod(nx0 + x,width()),cimg::mod(ny0 + y,height()),
  34962. cimg::mod(nz0 + z,depth()),cimg::mod(nc0 + c,spectrum()));
  34963. }
  34964. } break;
  34965. case 1 : // Neumann
  34966. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  34967. _height*_depth*_spectrum>=4))
  34968. cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _atXYZC(nx0 + x,ny0 + y,nz0 + z,nc0 + c);
  34969. break;
  34970. default : // Dirichlet
  34971. res.fill((T)0).draw_image(-nx0,-ny0,-nz0,-nc0,*this);
  34972. }
  34973. else res.draw_image(-nx0,-ny0,-nz0,-nc0,*this);
  34974. return res;
  34975. }
  34976. //! Crop image region \overloading.
  34977. CImg<T>& crop(const int x0, const int y0, const int z0,
  34978. const int x1, const int y1, const int z1,
  34979. const unsigned int boundary_conditions=0) {
  34980. return crop(x0,y0,z0,0,x1,y1,z1,_spectrum - 1,boundary_conditions);
  34981. }
  34982. //! Crop image region \newinstance.
  34983. CImg<T> get_crop(const int x0, const int y0, const int z0,
  34984. const int x1, const int y1, const int z1,
  34985. const unsigned int boundary_conditions=0) const {
  34986. return get_crop(x0,y0,z0,0,x1,y1,z1,_spectrum - 1,boundary_conditions);
  34987. }
  34988. //! Crop image region \overloading.
  34989. CImg<T>& crop(const int x0, const int y0,
  34990. const int x1, const int y1,
  34991. const unsigned int boundary_conditions=0) {
  34992. return crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions);
  34993. }
  34994. //! Crop image region \newinstance.
  34995. CImg<T> get_crop(const int x0, const int y0,
  34996. const int x1, const int y1,
  34997. const unsigned int boundary_conditions=0) const {
  34998. return get_crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions);
  34999. }
  35000. //! Crop image region \overloading.
  35001. CImg<T>& crop(const int x0, const int x1, const unsigned int boundary_conditions=0) {
  35002. return crop(x0,0,0,0,x1,_height - 1,_depth - 1,_spectrum - 1,boundary_conditions);
  35003. }
  35004. //! Crop image region \newinstance.
  35005. CImg<T> get_crop(const int x0, const int x1, const unsigned int boundary_conditions=0) const {
  35006. return get_crop(x0,0,0,0,x1,_height - 1,_depth - 1,_spectrum - 1,boundary_conditions);
  35007. }
  35008. //! Autocrop image region, regarding the specified background value.
  35009. CImg<T>& autocrop(const T& value, const char *const axes="czyx") {
  35010. if (is_empty()) return *this;
  35011. for (const char *s = axes; *s; ++s) {
  35012. const char axis = cimg::lowercase(*s);
  35013. const CImg<intT> coords = _autocrop(value,axis);
  35014. if (coords[0]==-1 && coords[1]==-1) return assign(); // Image has only 'value' pixels
  35015. else switch (axis) {
  35016. case 'x' : {
  35017. const int x0 = coords[0], x1 = coords[1];
  35018. if (x0>=0 && x1>=0) crop(x0,x1);
  35019. } break;
  35020. case 'y' : {
  35021. const int y0 = coords[0], y1 = coords[1];
  35022. if (y0>=0 && y1>=0) crop(0,y0,_width - 1,y1);
  35023. } break;
  35024. case 'z' : {
  35025. const int z0 = coords[0], z1 = coords[1];
  35026. if (z0>=0 && z1>=0) crop(0,0,z0,_width - 1,_height - 1,z1);
  35027. } break;
  35028. default : {
  35029. const int c0 = coords[0], c1 = coords[1];
  35030. if (c0>=0 && c1>=0) crop(0,0,0,c0,_width - 1,_height - 1,_depth - 1,c1);
  35031. }
  35032. }
  35033. }
  35034. return *this;
  35035. }
  35036. //! Autocrop image region, regarding the specified background value \newinstance.
  35037. CImg<T> get_autocrop(const T& value, const char *const axes="czyx") const {
  35038. return (+*this).autocrop(value,axes);
  35039. }
  35040. //! Autocrop image region, regarding the specified background color.
  35041. /**
  35042. \param color Color used for the crop. If \c 0, color is guessed.
  35043. \param axes Axes used for the crop.
  35044. **/
  35045. CImg<T>& autocrop(const T *const color=0, const char *const axes="zyx") {
  35046. if (is_empty()) return *this;
  35047. if (!color) { // Guess color
  35048. const CImg<T> col1 = get_vector_at(0,0,0);
  35049. const unsigned int w = _width, h = _height, d = _depth, s = _spectrum;
  35050. autocrop(col1,axes);
  35051. if (_width==w && _height==h && _depth==d && _spectrum==s) {
  35052. const CImg<T> col2 = get_vector_at(w - 1,h - 1,d - 1);
  35053. autocrop(col2,axes);
  35054. }
  35055. return *this;
  35056. }
  35057. for (const char *s = axes; *s; ++s) {
  35058. const char axis = cimg::lowercase(*s);
  35059. switch (axis) {
  35060. case 'x' : {
  35061. int x0 = width(), x1 = -1;
  35062. cimg_forC(*this,c) {
  35063. const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'x');
  35064. const int nx0 = coords[0], nx1 = coords[1];
  35065. if (nx0>=0 && nx1>=0) { x0 = std::min(x0,nx0); x1 = std::max(x1,nx1); }
  35066. }
  35067. if (x0==width() && x1==-1) return assign(); else crop(x0,x1);
  35068. } break;
  35069. case 'y' : {
  35070. int y0 = height(), y1 = -1;
  35071. cimg_forC(*this,c) {
  35072. const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'y');
  35073. const int ny0 = coords[0], ny1 = coords[1];
  35074. if (ny0>=0 && ny1>=0) { y0 = std::min(y0,ny0); y1 = std::max(y1,ny1); }
  35075. }
  35076. if (y0==height() && y1==-1) return assign(); else crop(0,y0,_width - 1,y1);
  35077. } break;
  35078. default : {
  35079. int z0 = depth(), z1 = -1;
  35080. cimg_forC(*this,c) {
  35081. const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'z');
  35082. const int nz0 = coords[0], nz1 = coords[1];
  35083. if (nz0>=0 && nz1>=0) { z0 = std::min(z0,nz0); z1 = std::max(z1,nz1); }
  35084. }
  35085. if (z0==depth() && z1==-1) return assign(); else crop(0,0,z0,_width - 1,_height - 1,z1);
  35086. }
  35087. }
  35088. }
  35089. return *this;
  35090. }
  35091. //! Autocrop image region, regarding the specified background color \newinstance.
  35092. CImg<T> get_autocrop(const T *const color=0, const char *const axes="zyx") const {
  35093. return (+*this).autocrop(color,axes);
  35094. }
  35095. CImg<intT> _autocrop(const T& value, const char axis) const {
  35096. CImg<intT> res;
  35097. switch (cimg::lowercase(axis)) {
  35098. case 'x' : {
  35099. int x0 = -1, x1 = -1;
  35100. cimg_forX(*this,x) cimg_forYZC(*this,y,z,c)
  35101. if ((*this)(x,y,z,c)!=value) { x0 = x; x = width(); y = height(); z = depth(); c = spectrum(); }
  35102. if (x0>=0) {
  35103. for (int x = width() - 1; x>=0; --x) cimg_forYZC(*this,y,z,c)
  35104. if ((*this)(x,y,z,c)!=value) { x1 = x; x = 0; y = height(); z = depth(); c = spectrum(); }
  35105. }
  35106. res = CImg<intT>::vector(x0,x1);
  35107. } break;
  35108. case 'y' : {
  35109. int y0 = -1, y1 = -1;
  35110. cimg_forY(*this,y) cimg_forXZC(*this,x,z,c)
  35111. if ((*this)(x,y,z,c)!=value) { y0 = y; x = width(); y = height(); z = depth(); c = spectrum(); }
  35112. if (y0>=0) {
  35113. for (int y = height() - 1; y>=0; --y) cimg_forXZC(*this,x,z,c)
  35114. if ((*this)(x,y,z,c)!=value) { y1 = y; x = width(); y = 0; z = depth(); c = spectrum(); }
  35115. }
  35116. res = CImg<intT>::vector(y0,y1);
  35117. } break;
  35118. case 'z' : {
  35119. int z0 = -1, z1 = -1;
  35120. cimg_forZ(*this,z) cimg_forXYC(*this,x,y,c)
  35121. if ((*this)(x,y,z,c)!=value) { z0 = z; x = width(); y = height(); z = depth(); c = spectrum(); }
  35122. if (z0>=0) {
  35123. for (int z = depth() - 1; z>=0; --z) cimg_forXYC(*this,x,y,c)
  35124. if ((*this)(x,y,z,c)!=value) { z1 = z; x = width(); y = height(); z = 0; c = spectrum(); }
  35125. }
  35126. res = CImg<intT>::vector(z0,z1);
  35127. } break;
  35128. default : {
  35129. int c0 = -1, c1 = -1;
  35130. cimg_forC(*this,c) cimg_forXYZ(*this,x,y,z)
  35131. if ((*this)(x,y,z,c)!=value) { c0 = c; x = width(); y = height(); z = depth(); c = spectrum(); }
  35132. if (c0>=0) {
  35133. for (int c = spectrum() - 1; c>=0; --c) cimg_forXYZ(*this,x,y,z)
  35134. if ((*this)(x,y,z,c)!=value) { c1 = c; x = width(); y = height(); z = depth(); c = 0; }
  35135. }
  35136. res = CImg<intT>::vector(c0,c1);
  35137. }
  35138. }
  35139. return res;
  35140. }
  35141. //! Return specified image column.
  35142. /**
  35143. \param x0 Image column.
  35144. **/
  35145. CImg<T> get_column(const int x0) const {
  35146. return get_columns(x0,x0);
  35147. }
  35148. //! Return specified image column \inplace.
  35149. CImg<T>& column(const int x0) {
  35150. return columns(x0,x0);
  35151. }
  35152. //! Return specified range of image columns.
  35153. /**
  35154. \param x0 Starting image column.
  35155. \param x1 Ending image column.
  35156. **/
  35157. CImg<T>& columns(const int x0, const int x1) {
  35158. return get_columns(x0,x1).move_to(*this);
  35159. }
  35160. //! Return specified range of image columns \inplace.
  35161. CImg<T> get_columns(const int x0, const int x1) const {
  35162. return get_crop(x0,0,0,0,x1,height() - 1,depth() - 1,spectrum() - 1);
  35163. }
  35164. //! Return specified image row.
  35165. CImg<T> get_row(const int y0) const {
  35166. return get_rows(y0,y0);
  35167. }
  35168. //! Return specified image row \inplace.
  35169. /**
  35170. \param y0 Image row.
  35171. **/
  35172. CImg<T>& row(const int y0) {
  35173. return rows(y0,y0);
  35174. }
  35175. //! Return specified range of image rows.
  35176. /**
  35177. \param y0 Starting image row.
  35178. \param y1 Ending image row.
  35179. **/
  35180. CImg<T> get_rows(const int y0, const int y1) const {
  35181. return get_crop(0,y0,0,0,width() - 1,y1,depth() - 1,spectrum() - 1);
  35182. }
  35183. //! Return specified range of image rows \inplace.
  35184. CImg<T>& rows(const int y0, const int y1) {
  35185. return get_rows(y0,y1).move_to(*this);
  35186. }
  35187. //! Return specified image slice.
  35188. /**
  35189. \param z0 Image slice.
  35190. **/
  35191. CImg<T> get_slice(const int z0) const {
  35192. return get_slices(z0,z0);
  35193. }
  35194. //! Return specified image slice \inplace.
  35195. CImg<T>& slice(const int z0) {
  35196. return slices(z0,z0);
  35197. }
  35198. //! Return specified range of image slices.
  35199. /**
  35200. \param z0 Starting image slice.
  35201. \param z1 Ending image slice.
  35202. **/
  35203. CImg<T> get_slices(const int z0, const int z1) const {
  35204. return get_crop(0,0,z0,0,width() - 1,height() - 1,z1,spectrum() - 1);
  35205. }
  35206. //! Return specified range of image slices \inplace.
  35207. CImg<T>& slices(const int z0, const int z1) {
  35208. return get_slices(z0,z1).move_to(*this);
  35209. }
  35210. //! Return specified image channel.
  35211. /**
  35212. \param c0 Image channel.
  35213. **/
  35214. CImg<T> get_channel(const int c0) const {
  35215. return get_channels(c0,c0);
  35216. }
  35217. //! Return specified image channel \inplace.
  35218. CImg<T>& channel(const int c0) {
  35219. return channels(c0,c0);
  35220. }
  35221. //! Return specified range of image channels.
  35222. /**
  35223. \param c0 Starting image channel.
  35224. \param c1 Ending image channel.
  35225. **/
  35226. CImg<T> get_channels(const int c0, const int c1) const {
  35227. return get_crop(0,0,0,c0,width() - 1,height() - 1,depth() - 1,c1);
  35228. }
  35229. //! Return specified range of image channels \inplace.
  35230. CImg<T>& channels(const int c0, const int c1) {
  35231. return get_channels(c0,c1).move_to(*this);
  35232. }
  35233. //! Return stream line of a 2D or 3D vector field.
  35234. CImg<floatT> get_streamline(const float x, const float y, const float z,
  35235. const float L=256, const float dl=0.1f,
  35236. const unsigned int interpolation_type=2, const bool is_backward_tracking=false,
  35237. const bool is_oriented_only=false) const {
  35238. if (_spectrum!=2 && _spectrum!=3)
  35239. throw CImgInstanceException(_cimg_instance
  35240. "streamline(): Instance is not a 2D or 3D vector field.",
  35241. cimg_instance);
  35242. if (_spectrum==2) {
  35243. if (is_oriented_only) {
  35244. typename CImg<T>::_functor4d_streamline2d_oriented func(*this);
  35245. return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true,
  35246. 0,0,0,_width - 1.f,_height - 1.f,0.f);
  35247. } else {
  35248. typename CImg<T>::_functor4d_streamline2d_directed func(*this);
  35249. return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false,
  35250. 0,0,0,_width - 1.f,_height - 1.f,0.f);
  35251. }
  35252. }
  35253. if (is_oriented_only) {
  35254. typename CImg<T>::_functor4d_streamline3d_oriented func(*this);
  35255. return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true,
  35256. 0,0,0,_width - 1.f,_height - 1.f,_depth - 1.f);
  35257. }
  35258. typename CImg<T>::_functor4d_streamline3d_directed func(*this);
  35259. return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false,
  35260. 0,0,0,_width - 1.f,_height - 1.f,_depth - 1.f);
  35261. }
  35262. //! Return stream line of a 3D vector field.
  35263. /**
  35264. \param func Vector field function.
  35265. \param x X-coordinate of the starting point of the streamline.
  35266. \param y Y-coordinate of the starting point of the streamline.
  35267. \param z Z-coordinate of the starting point of the streamline.
  35268. \param L Streamline length.
  35269. \param dl Streamline length increment.
  35270. \param interpolation_type Type of interpolation.
  35271. Can be <tt>{ 0=nearest int | 1=linear | 2=2nd-order RK | 3=4th-order RK. }</tt>.
  35272. \param is_backward_tracking Tells if the streamline is estimated forward or backward.
  35273. \param is_oriented_only Tells if the direction of the vectors must be ignored.
  35274. \param x0 X-coordinate of the first bounding-box vertex.
  35275. \param y0 Y-coordinate of the first bounding-box vertex.
  35276. \param z0 Z-coordinate of the first bounding-box vertex.
  35277. \param x1 X-coordinate of the second bounding-box vertex.
  35278. \param y1 Y-coordinate of the second bounding-box vertex.
  35279. \param z1 Z-coordinate of the second bounding-box vertex.
  35280. **/
  35281. template<typename tfunc>
  35282. static CImg<floatT> streamline(const tfunc& func,
  35283. const float x, const float y, const float z,
  35284. const float L=256, const float dl=0.1f,
  35285. const unsigned int interpolation_type=2, const bool is_backward_tracking=false,
  35286. const bool is_oriented_only=false,
  35287. const float x0=0, const float y0=0, const float z0=0,
  35288. const float x1=0, const float y1=0, const float z1=0) {
  35289. if (dl<=0)
  35290. throw CImgArgumentException("CImg<%s>::streamline(): Invalid specified integration length %g "
  35291. "(should be >0).",
  35292. pixel_type(),
  35293. dl);
  35294. const bool is_bounded = (x0!=x1 || y0!=y1 || z0!=z1);
  35295. if (L<=0 || (is_bounded && (x<x0 || x>x1 || y<y0 || y>y1 || z<z0 || z>z1))) return CImg<floatT>();
  35296. const unsigned int size_L = (unsigned int)cimg::round(L/dl + 1);
  35297. CImg<floatT> coordinates(size_L,3);
  35298. const float dl2 = dl/2;
  35299. float
  35300. *ptr_x = coordinates.data(0,0),
  35301. *ptr_y = coordinates.data(0,1),
  35302. *ptr_z = coordinates.data(0,2),
  35303. pu = (float)(dl*func(x,y,z,0)),
  35304. pv = (float)(dl*func(x,y,z,1)),
  35305. pw = (float)(dl*func(x,y,z,2)),
  35306. X = x, Y = y, Z = z;
  35307. switch (interpolation_type) {
  35308. case 0 : { // Nearest integer interpolation
  35309. cimg_forX(coordinates,l) {
  35310. *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
  35311. const int
  35312. xi = (int)(X>0?X + 0.5f:X - 0.5f),
  35313. yi = (int)(Y>0?Y + 0.5f:Y - 0.5f),
  35314. zi = (int)(Z>0?Z + 0.5f:Z - 0.5f);
  35315. float
  35316. u = (float)(dl*func((float)xi,(float)yi,(float)zi,0)),
  35317. v = (float)(dl*func((float)xi,(float)yi,(float)zi,1)),
  35318. w = (float)(dl*func((float)xi,(float)yi,(float)zi,2));
  35319. if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
  35320. if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
  35321. if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
  35322. }
  35323. } break;
  35324. case 1 : { // First-order interpolation
  35325. cimg_forX(coordinates,l) {
  35326. *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
  35327. float
  35328. u = (float)(dl*func(X,Y,Z,0)),
  35329. v = (float)(dl*func(X,Y,Z,1)),
  35330. w = (float)(dl*func(X,Y,Z,2));
  35331. if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
  35332. if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
  35333. if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
  35334. }
  35335. } break;
  35336. case 2 : { // Second order interpolation
  35337. cimg_forX(coordinates,l) {
  35338. *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
  35339. float
  35340. u0 = (float)(dl2*func(X,Y,Z,0)),
  35341. v0 = (float)(dl2*func(X,Y,Z,1)),
  35342. w0 = (float)(dl2*func(X,Y,Z,2));
  35343. if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; }
  35344. float
  35345. u = (float)(dl*func(X + u0,Y + v0,Z + w0,0)),
  35346. v = (float)(dl*func(X + u0,Y + v0,Z + w0,1)),
  35347. w = (float)(dl*func(X + u0,Y + v0,Z + w0,2));
  35348. if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
  35349. if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
  35350. if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
  35351. }
  35352. } break;
  35353. default : { // Fourth order interpolation
  35354. cimg_forX(coordinates,k) {
  35355. *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
  35356. float
  35357. u0 = (float)(dl2*func(X,Y,Z,0)),
  35358. v0 = (float)(dl2*func(X,Y,Z,1)),
  35359. w0 = (float)(dl2*func(X,Y,Z,2));
  35360. if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; }
  35361. float
  35362. u1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,0)),
  35363. v1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,1)),
  35364. w1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,2));
  35365. if (is_oriented_only && u1*pu + v1*pv + w1*pw<0) { u1 = -u1; v1 = -v1; w1 = -w1; }
  35366. float
  35367. u2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,0)),
  35368. v2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,1)),
  35369. w2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,2));
  35370. if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u2 = -u2; v2 = -v2; w2 = -w2; }
  35371. float
  35372. u3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,0)),
  35373. v3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,1)),
  35374. w3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,2));
  35375. if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u3 = -u3; v3 = -v3; w3 = -w3; }
  35376. const float
  35377. u = (u0 + u3)/3 + (u1 + u2)/1.5f,
  35378. v = (v0 + v3)/3 + (v1 + v2)/1.5f,
  35379. w = (w0 + w3)/3 + (w1 + w2)/1.5f;
  35380. if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
  35381. if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
  35382. }
  35383. }
  35384. }
  35385. if (ptr_x!=coordinates.data(0,1)) coordinates.resize((int)(ptr_x-coordinates.data()),3,1,1,0);
  35386. return coordinates;
  35387. }
  35388. //! Return stream line of a 3D vector field \overloading.
  35389. static CImg<floatT> streamline(const char *const expression,
  35390. const float x, const float y, const float z,
  35391. const float L=256, const float dl=0.1f,
  35392. const unsigned int interpolation_type=2, const bool is_backward_tracking=true,
  35393. const bool is_oriented_only=false,
  35394. const float x0=0, const float y0=0, const float z0=0,
  35395. const float x1=0, const float y1=0, const float z1=0) {
  35396. _functor4d_streamline_expr func(expression);
  35397. return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,is_oriented_only,x0,y0,z0,x1,y1,z1);
  35398. }
  35399. struct _functor4d_streamline2d_directed {
  35400. const CImg<T>& ref;
  35401. _functor4d_streamline2d_directed(const CImg<T>& pref):ref(pref) {}
  35402. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  35403. return c<2?(float)ref._linear_atXY(x,y,(int)z,c):0;
  35404. }
  35405. };
  35406. struct _functor4d_streamline3d_directed {
  35407. const CImg<T>& ref;
  35408. _functor4d_streamline3d_directed(const CImg<T>& pref):ref(pref) {}
  35409. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  35410. return (float)ref._linear_atXYZ(x,y,z,c);
  35411. }
  35412. };
  35413. struct _functor4d_streamline2d_oriented {
  35414. const CImg<T>& ref;
  35415. CImg<floatT> *pI;
  35416. _functor4d_streamline2d_oriented(const CImg<T>& pref):ref(pref),pI(0) { pI = new CImg<floatT>(2,2,1,2); }
  35417. ~_functor4d_streamline2d_oriented() { delete pI; }
  35418. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  35419. #define _cimg_vecalign2d(i,j) \
  35420. if (I(i,j,0)*I(0,0,0) + I(i,j,1)*I(0,0,1)<0) { I(i,j,0) = -I(i,j,0); I(i,j,1) = -I(i,j,1); }
  35421. int
  35422. xi = (int)x - (x>=0?0:1), nxi = xi + 1,
  35423. yi = (int)y - (y>=0?0:1), nyi = yi + 1,
  35424. zi = (int)z;
  35425. const float
  35426. dx = x - xi,
  35427. dy = y - yi;
  35428. if (c==0) {
  35429. CImg<floatT>& I = *pI;
  35430. if (xi<0) xi = 0;
  35431. if (nxi<0) nxi = 0;
  35432. if (xi>=ref.width()) xi = ref.width() - 1;
  35433. if (nxi>=ref.width()) nxi = ref.width() - 1;
  35434. if (yi<0) yi = 0;
  35435. if (nyi<0) nyi = 0;
  35436. if (yi>=ref.height()) yi = ref.height() - 1;
  35437. if (nyi>=ref.height()) nyi = ref.height() - 1;
  35438. I(0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,1) = (float)ref(xi,yi,zi,1);
  35439. I(1,0,0) = (float)ref(nxi,yi,zi,0); I(1,0,1) = (float)ref(nxi,yi,zi,1);
  35440. I(1,1,0) = (float)ref(nxi,nyi,zi,0); I(1,1,1) = (float)ref(nxi,nyi,zi,1);
  35441. I(0,1,0) = (float)ref(xi,nyi,zi,0); I(0,1,1) = (float)ref(xi,nyi,zi,1);
  35442. _cimg_vecalign2d(1,0); _cimg_vecalign2d(1,1); _cimg_vecalign2d(0,1);
  35443. }
  35444. return c<2?(float)pI->_linear_atXY(dx,dy,0,c):0;
  35445. }
  35446. };
  35447. struct _functor4d_streamline3d_oriented {
  35448. const CImg<T>& ref;
  35449. CImg<floatT> *pI;
  35450. _functor4d_streamline3d_oriented(const CImg<T>& pref):ref(pref),pI(0) { pI = new CImg<floatT>(2,2,2,3); }
  35451. ~_functor4d_streamline3d_oriented() { delete pI; }
  35452. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  35453. #define _cimg_vecalign3d(i,j,k) if (I(i,j,k,0)*I(0,0,0,0) + I(i,j,k,1)*I(0,0,0,1) + I(i,j,k,2)*I(0,0,0,2)<0) { \
  35454. I(i,j,k,0) = -I(i,j,k,0); I(i,j,k,1) = -I(i,j,k,1); I(i,j,k,2) = -I(i,j,k,2); }
  35455. int
  35456. xi = (int)x - (x>=0?0:1), nxi = xi + 1,
  35457. yi = (int)y - (y>=0?0:1), nyi = yi + 1,
  35458. zi = (int)z - (z>=0?0:1), nzi = zi + 1;
  35459. const float
  35460. dx = x - xi,
  35461. dy = y - yi,
  35462. dz = z - zi;
  35463. if (c==0) {
  35464. CImg<floatT>& I = *pI;
  35465. if (xi<0) xi = 0;
  35466. if (nxi<0) nxi = 0;
  35467. if (xi>=ref.width()) xi = ref.width() - 1;
  35468. if (nxi>=ref.width()) nxi = ref.width() - 1;
  35469. if (yi<0) yi = 0;
  35470. if (nyi<0) nyi = 0;
  35471. if (yi>=ref.height()) yi = ref.height() - 1;
  35472. if (nyi>=ref.height()) nyi = ref.height() - 1;
  35473. if (zi<0) zi = 0;
  35474. if (nzi<0) nzi = 0;
  35475. if (zi>=ref.depth()) zi = ref.depth() - 1;
  35476. if (nzi>=ref.depth()) nzi = ref.depth() - 1;
  35477. I(0,0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,0,1) = (float)ref(xi,yi,zi,1);
  35478. I(0,0,0,2) = (float)ref(xi,yi,zi,2); I(1,0,0,0) = (float)ref(nxi,yi,zi,0);
  35479. I(1,0,0,1) = (float)ref(nxi,yi,zi,1); I(1,0,0,2) = (float)ref(nxi,yi,zi,2);
  35480. I(1,1,0,0) = (float)ref(nxi,nyi,zi,0); I(1,1,0,1) = (float)ref(nxi,nyi,zi,1);
  35481. I(1,1,0,2) = (float)ref(nxi,nyi,zi,2); I(0,1,0,0) = (float)ref(xi,nyi,zi,0);
  35482. I(0,1,0,1) = (float)ref(xi,nyi,zi,1); I(0,1,0,2) = (float)ref(xi,nyi,zi,2);
  35483. I(0,0,1,0) = (float)ref(xi,yi,nzi,0); I(0,0,1,1) = (float)ref(xi,yi,nzi,1);
  35484. I(0,0,1,2) = (float)ref(xi,yi,nzi,2); I(1,0,1,0) = (float)ref(nxi,yi,nzi,0);
  35485. I(1,0,1,1) = (float)ref(nxi,yi,nzi,1); I(1,0,1,2) = (float)ref(nxi,yi,nzi,2);
  35486. I(1,1,1,0) = (float)ref(nxi,nyi,nzi,0); I(1,1,1,1) = (float)ref(nxi,nyi,nzi,1);
  35487. I(1,1,1,2) = (float)ref(nxi,nyi,nzi,2); I(0,1,1,0) = (float)ref(xi,nyi,nzi,0);
  35488. I(0,1,1,1) = (float)ref(xi,nyi,nzi,1); I(0,1,1,2) = (float)ref(xi,nyi,nzi,2);
  35489. _cimg_vecalign3d(1,0,0); _cimg_vecalign3d(1,1,0); _cimg_vecalign3d(0,1,0);
  35490. _cimg_vecalign3d(0,0,1); _cimg_vecalign3d(1,0,1); _cimg_vecalign3d(1,1,1); _cimg_vecalign3d(0,1,1);
  35491. }
  35492. return (float)pI->_linear_atXYZ(dx,dy,dz,c);
  35493. }
  35494. };
  35495. struct _functor4d_streamline_expr {
  35496. _cimg_math_parser *mp;
  35497. ~_functor4d_streamline_expr() { mp->end(); delete mp; }
  35498. _functor4d_streamline_expr(const char *const expr):mp(0) {
  35499. mp = new _cimg_math_parser(expr,"streamline",CImg<T>::const_empty(),0);
  35500. }
  35501. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  35502. return (float)(*mp)(x,y,z,c);
  35503. }
  35504. };
  35505. //! Return a shared-memory image referencing a range of pixels of the image instance.
  35506. /**
  35507. \param x0 X-coordinate of the starting pixel.
  35508. \param x1 X-coordinate of the ending pixel.
  35509. \param y0 Y-coordinate.
  35510. \param z0 Z-coordinate.
  35511. \param c0 C-coordinate.
  35512. **/
  35513. CImg<T> get_shared_points(const unsigned int x0, const unsigned int x1,
  35514. const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) {
  35515. const ulongT
  35516. beg = (ulongT)offset(x0,y0,z0,c0),
  35517. end = (ulongT)offset(x1,y0,z0,c0);
  35518. if (beg>end || beg>=size() || end>=size())
  35519. throw CImgArgumentException(_cimg_instance
  35520. "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).",
  35521. cimg_instance,
  35522. x0,x1,y0,z0,c0);
  35523. return CImg<T>(_data + beg,x1 - x0 + 1,1,1,1,true);
  35524. }
  35525. //! Return a shared-memory image referencing a range of pixels of the image instance \const.
  35526. const CImg<T> get_shared_points(const unsigned int x0, const unsigned int x1,
  35527. const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) const {
  35528. const ulongT
  35529. beg = (ulongT)offset(x0,y0,z0,c0),
  35530. end = (ulongT)offset(x1,y0,z0,c0);
  35531. if (beg>end || beg>=size() || end>=size())
  35532. throw CImgArgumentException(_cimg_instance
  35533. "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).",
  35534. cimg_instance,
  35535. x0,x1,y0,z0,c0);
  35536. return CImg<T>(_data + beg,x1 - x0 + 1,1,1,1,true);
  35537. }
  35538. //! Return a shared-memory image referencing a range of rows of the image instance.
  35539. /**
  35540. \param y0 Y-coordinate of the starting row.
  35541. \param y1 Y-coordinate of the ending row.
  35542. \param z0 Z-coordinate.
  35543. \param c0 C-coordinate.
  35544. **/
  35545. CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
  35546. const unsigned int z0=0, const unsigned int c0=0) {
  35547. const ulongT
  35548. beg = (ulongT)offset(0,y0,z0,c0),
  35549. end = (ulongT)offset(0,y1,z0,c0);
  35550. if (beg>end || beg>=size() || end>=size())
  35551. throw CImgArgumentException(_cimg_instance
  35552. "get_shared_rows(): Invalid request of a shared-memory subset "
  35553. "(0->%u,%u->%u,%u,%u).",
  35554. cimg_instance,
  35555. _width - 1,y0,y1,z0,c0);
  35556. return CImg<T>(_data + beg,_width,y1 - y0 + 1,1,1,true);
  35557. }
  35558. //! Return a shared-memory image referencing a range of rows of the image instance \const.
  35559. const CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
  35560. const unsigned int z0=0, const unsigned int c0=0) const {
  35561. const ulongT
  35562. beg = (ulongT)offset(0,y0,z0,c0),
  35563. end = (ulongT)offset(0,y1,z0,c0);
  35564. if (beg>end || beg>=size() || end>=size())
  35565. throw CImgArgumentException(_cimg_instance
  35566. "get_shared_rows(): Invalid request of a shared-memory subset "
  35567. "(0->%u,%u->%u,%u,%u).",
  35568. cimg_instance,
  35569. _width - 1,y0,y1,z0,c0);
  35570. return CImg<T>(_data + beg,_width,y1 - y0 + 1,1,1,true);
  35571. }
  35572. //! Return a shared-memory image referencing one row of the image instance.
  35573. /**
  35574. \param y0 Y-coordinate.
  35575. \param z0 Z-coordinate.
  35576. \param c0 C-coordinate.
  35577. **/
  35578. CImg<T> get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) {
  35579. return get_shared_rows(y0,y0,z0,c0);
  35580. }
  35581. //! Return a shared-memory image referencing one row of the image instance \const.
  35582. const CImg<T> get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) const {
  35583. return get_shared_rows(y0,y0,z0,c0);
  35584. }
  35585. //! Return a shared memory image referencing a range of slices of the image instance.
  35586. /**
  35587. \param z0 Z-coordinate of the starting slice.
  35588. \param z1 Z-coordinate of the ending slice.
  35589. \param c0 C-coordinate.
  35590. **/
  35591. CImg<T> get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) {
  35592. const ulongT
  35593. beg = (ulongT)offset(0,0,z0,c0),
  35594. end = (ulongT)offset(0,0,z1,c0);
  35595. if (beg>end || beg>=size() || end>=size())
  35596. throw CImgArgumentException(_cimg_instance
  35597. "get_shared_slices(): Invalid request of a shared-memory subset "
  35598. "(0->%u,0->%u,%u->%u,%u).",
  35599. cimg_instance,
  35600. _width - 1,_height - 1,z0,z1,c0);
  35601. return CImg<T>(_data + beg,_width,_height,z1 - z0 + 1,1,true);
  35602. }
  35603. //! Return a shared memory image referencing a range of slices of the image instance \const.
  35604. const CImg<T> get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) const {
  35605. const ulongT
  35606. beg = (ulongT)offset(0,0,z0,c0),
  35607. end = (ulongT)offset(0,0,z1,c0);
  35608. if (beg>end || beg>=size() || end>=size())
  35609. throw CImgArgumentException(_cimg_instance
  35610. "get_shared_slices(): Invalid request of a shared-memory subset "
  35611. "(0->%u,0->%u,%u->%u,%u).",
  35612. cimg_instance,
  35613. _width - 1,_height - 1,z0,z1,c0);
  35614. return CImg<T>(_data + beg,_width,_height,z1 - z0 + 1,1,true);
  35615. }
  35616. //! Return a shared-memory image referencing one slice of the image instance.
  35617. /**
  35618. \param z0 Z-coordinate.
  35619. \param c0 C-coordinate.
  35620. **/
  35621. CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) {
  35622. return get_shared_slices(z0,z0,c0);
  35623. }
  35624. //! Return a shared-memory image referencing one slice of the image instance \const.
  35625. const CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) const {
  35626. return get_shared_slices(z0,z0,c0);
  35627. }
  35628. //! Return a shared-memory image referencing a range of channels of the image instance.
  35629. /**
  35630. \param c0 C-coordinate of the starting channel.
  35631. \param c1 C-coordinate of the ending channel.
  35632. **/
  35633. CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) {
  35634. const ulongT
  35635. beg = (ulongT)offset(0,0,0,c0),
  35636. end = (ulongT)offset(0,0,0,c1);
  35637. if (beg>end || beg>=size() || end>=size())
  35638. throw CImgArgumentException(_cimg_instance
  35639. "get_shared_channels(): Invalid request of a shared-memory subset "
  35640. "(0->%u,0->%u,0->%u,%u->%u).",
  35641. cimg_instance,
  35642. _width - 1,_height - 1,_depth - 1,c0,c1);
  35643. return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);
  35644. }
  35645. //! Return a shared-memory image referencing a range of channels of the image instance \const.
  35646. const CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) const {
  35647. const ulongT
  35648. beg = (ulongT)offset(0,0,0,c0),
  35649. end = (ulongT)offset(0,0,0,c1);
  35650. if (beg>end || beg>=size() || end>=size())
  35651. throw CImgArgumentException(_cimg_instance
  35652. "get_shared_channels(): Invalid request of a shared-memory subset "
  35653. "(0->%u,0->%u,0->%u,%u->%u).",
  35654. cimg_instance,
  35655. _width - 1,_height - 1,_depth - 1,c0,c1);
  35656. return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);
  35657. }
  35658. //! Return a shared-memory image referencing one channel of the image instance.
  35659. /**
  35660. \param c0 C-coordinate.
  35661. **/
  35662. CImg<T> get_shared_channel(const unsigned int c0) {
  35663. return get_shared_channels(c0,c0);
  35664. }
  35665. //! Return a shared-memory image referencing one channel of the image instance \const.
  35666. const CImg<T> get_shared_channel(const unsigned int c0) const {
  35667. return get_shared_channels(c0,c0);
  35668. }
  35669. //! Return a shared-memory version of the image instance.
  35670. CImg<T> get_shared() {
  35671. return CImg<T>(_data,_width,_height,_depth,_spectrum,true);
  35672. }
  35673. //! Return a shared-memory version of the image instance \const.
  35674. const CImg<T> get_shared() const {
  35675. return CImg<T>(_data,_width,_height,_depth,_spectrum,true);
  35676. }
  35677. //! Split image into a list along specified axis.
  35678. /**
  35679. \param axis Splitting axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  35680. \param nb Number of split parts.
  35681. \note
  35682. - If \c nb==0, instance image is split into blocs of equal values along the specified axis.
  35683. - If \c nb<=0, instance image is split into blocs of -\c nb pixel wide.
  35684. - If \c nb>0, instance image is split into \c nb blocs.
  35685. **/
  35686. CImgList<T> get_split(const char axis, const int nb=-1) const {
  35687. CImgList<T> res;
  35688. if (is_empty()) return res;
  35689. const char _axis = cimg::lowercase(axis);
  35690. if (nb<0) { // Split by bloc size
  35691. const unsigned int dp = (unsigned int)(nb?-nb:1);
  35692. switch (_axis) {
  35693. case 'x': {
  35694. if (_width>dp) {
  35695. res.assign(_width/dp + (_width%dp?1:0),1,1);
  35696. const unsigned int pe = _width - dp;
  35697. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
  35698. _height*_depth*_spectrum>=128))
  35699. for (int p = 0; p<(int)pe; p+=dp)
  35700. get_crop(p,0,0,0,p + dp - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res[p/dp]);
  35701. get_crop((res._width - 1)*dp,0,0,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
  35702. } else res.assign(*this);
  35703. } break;
  35704. case 'y': {
  35705. if (_height>dp) {
  35706. res.assign(_height/dp + (_height%dp?1:0),1,1);
  35707. const unsigned int pe = _height - dp;
  35708. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
  35709. _width*_depth*_spectrum>=128))
  35710. for (int p = 0; p<(int)pe; p+=dp)
  35711. get_crop(0,p,0,0,_width - 1,p + dp - 1,_depth - 1,_spectrum - 1).move_to(res[p/dp]);
  35712. get_crop(0,(res._width - 1)*dp,0,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
  35713. } else res.assign(*this);
  35714. } break;
  35715. case 'z': {
  35716. if (_depth>dp) {
  35717. res.assign(_depth/dp + (_depth%dp?1:0),1,1);
  35718. const unsigned int pe = _depth - dp;
  35719. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
  35720. _width*_height*_spectrum>=128))
  35721. for (int p = 0; p<(int)pe; p+=dp)
  35722. get_crop(0,0,p,0,_width - 1,_height - 1,p + dp - 1,_spectrum - 1).move_to(res[p/dp]);
  35723. get_crop(0,0,(res._width - 1)*dp,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
  35724. } else res.assign(*this);
  35725. } break;
  35726. case 'c' : {
  35727. if (_spectrum>dp) {
  35728. res.assign(_spectrum/dp + (_spectrum%dp?1:0),1,1);
  35729. const unsigned int pe = _spectrum - dp;
  35730. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
  35731. _width*_height*_depth>=128))
  35732. for (int p = 0; p<(int)pe; p+=dp)
  35733. get_crop(0,0,0,p,_width - 1,_height - 1,_depth - 1,p + dp - 1).move_to(res[p/dp]);
  35734. get_crop(0,0,0,(res._width - 1)*dp,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
  35735. } else res.assign(*this);
  35736. }
  35737. }
  35738. } else if (nb>0) { // Split by number of (non-homogeneous) blocs
  35739. const unsigned int siz = _axis=='x'?_width:_axis=='y'?_height:_axis=='z'?_depth:_axis=='c'?_spectrum:0;
  35740. if ((unsigned int)nb>siz)
  35741. throw CImgArgumentException(_cimg_instance
  35742. "get_split(): Instance cannot be split along %c-axis into %u blocs.",
  35743. cimg_instance,
  35744. axis,nb);
  35745. if (nb==1) res.assign(*this);
  35746. else {
  35747. int err = (int)siz;
  35748. unsigned int _p = 0;
  35749. switch (_axis) {
  35750. case 'x' : {
  35751. cimg_forX(*this,p) if ((err-=nb)<=0) {
  35752. get_crop(_p,0,0,0,p,_height - 1,_depth - 1,_spectrum - 1).move_to(res);
  35753. err+=(int)siz;
  35754. _p = p + 1U;
  35755. }
  35756. } break;
  35757. case 'y' : {
  35758. cimg_forY(*this,p) if ((err-=nb)<=0) {
  35759. get_crop(0,_p,0,0,_width - 1,p,_depth - 1,_spectrum - 1).move_to(res);
  35760. err+=(int)siz;
  35761. _p = p + 1U;
  35762. }
  35763. } break;
  35764. case 'z' : {
  35765. cimg_forZ(*this,p) if ((err-=nb)<=0) {
  35766. get_crop(0,0,_p,0,_width - 1,_height - 1,p,_spectrum - 1).move_to(res);
  35767. err+=(int)siz;
  35768. _p = p + 1U;
  35769. }
  35770. } break;
  35771. case 'c' : {
  35772. cimg_forC(*this,p) if ((err-=nb)<=0) {
  35773. get_crop(0,0,0,_p,_width - 1,_height - 1,_depth - 1,p).move_to(res);
  35774. err+=(int)siz;
  35775. _p = p + 1U;
  35776. }
  35777. }
  35778. }
  35779. }
  35780. } else { // Split by equal values according to specified axis
  35781. T current = *_data;
  35782. switch (_axis) {
  35783. case 'x' : {
  35784. int i0 = 0;
  35785. cimg_forX(*this,i)
  35786. if ((*this)(i)!=current) { get_columns(i0,i - 1).move_to(res); i0 = i; current = (*this)(i); }
  35787. get_columns(i0,width() - 1).move_to(res);
  35788. } break;
  35789. case 'y' : {
  35790. int i0 = 0;
  35791. cimg_forY(*this,i)
  35792. if ((*this)(0,i)!=current) { get_rows(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,i); }
  35793. get_rows(i0,height() - 1).move_to(res);
  35794. } break;
  35795. case 'z' : {
  35796. int i0 = 0;
  35797. cimg_forZ(*this,i)
  35798. if ((*this)(0,0,i)!=current) { get_slices(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,0,i); }
  35799. get_slices(i0,depth() - 1).move_to(res);
  35800. } break;
  35801. case 'c' : {
  35802. int i0 = 0;
  35803. cimg_forC(*this,i)
  35804. if ((*this)(0,0,0,i)!=current) { get_channels(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,0,0,i); }
  35805. get_channels(i0,spectrum() - 1).move_to(res);
  35806. } break;
  35807. default : {
  35808. longT i0 = 0;
  35809. cimg_foroff(*this,i)
  35810. if ((*this)[i]!=current) {
  35811. CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res);
  35812. i0 = (longT)i; current = (*this)[i];
  35813. }
  35814. CImg<T>(_data + i0,1,(unsigned int)(size() - i0)).move_to(res);
  35815. }
  35816. }
  35817. }
  35818. return res;
  35819. }
  35820. //! Split image into a list of sub-images, according to a specified splitting value sequence and optionally axis.
  35821. /**
  35822. \param values Splitting value sequence.
  35823. \param axis Axis along which the splitting is performed. Can be '0' to ignore axis.
  35824. \param keep_values Tells if the splitting sequence must be kept in the split blocs.
  35825. **/
  35826. template<typename t>
  35827. CImgList<T> get_split(const CImg<t>& values, const char axis=0, const bool keep_values=true) const {
  35828. typedef _cimg_Tt Tt;
  35829. CImgList<T> res;
  35830. if (is_empty()) return res;
  35831. const ulongT vsiz = values.size();
  35832. const char _axis = cimg::lowercase(axis);
  35833. if (!vsiz) return CImgList<T>(*this);
  35834. if (vsiz==1) { // Split according to a single value
  35835. const T value = (T)*values;
  35836. switch (_axis) {
  35837. case 'x' : {
  35838. unsigned int i0 = 0, i = 0;
  35839. do {
  35840. while (i<_width && (*this)(i)==value) ++i;
  35841. if (i>i0) { if (keep_values) get_columns(i0,i - 1).move_to(res); i0 = i; }
  35842. while (i<_width && (*this)(i)!=value) ++i;
  35843. if (i>i0) { get_columns(i0,i - 1).move_to(res); i0 = i; }
  35844. } while (i<_width);
  35845. } break;
  35846. case 'y' : {
  35847. unsigned int i0 = 0, i = 0;
  35848. do {
  35849. while (i<_height && (*this)(0,i)==value) ++i;
  35850. if (i>i0) { if (keep_values) get_rows(i0,i - 1).move_to(res); i0 = i; }
  35851. while (i<_height && (*this)(0,i)!=value) ++i;
  35852. if (i>i0) { get_rows(i0,i - 1).move_to(res); i0 = i; }
  35853. } while (i<_height);
  35854. } break;
  35855. case 'z' : {
  35856. unsigned int i0 = 0, i = 0;
  35857. do {
  35858. while (i<_depth && (*this)(0,0,i)==value) ++i;
  35859. if (i>i0) { if (keep_values) get_slices(i0,i - 1).move_to(res); i0 = i; }
  35860. while (i<_depth && (*this)(0,0,i)!=value) ++i;
  35861. if (i>i0) { get_slices(i0,i - 1).move_to(res); i0 = i; }
  35862. } while (i<_depth);
  35863. } break;
  35864. case 'c' : {
  35865. unsigned int i0 = 0, i = 0;
  35866. do {
  35867. while (i<_spectrum && (*this)(0,0,0,i)==value) ++i;
  35868. if (i>i0) { if (keep_values) get_channels(i0,i - 1).move_to(res); i0 = i; }
  35869. while (i<_spectrum && (*this)(0,0,0,i)!=value) ++i;
  35870. if (i>i0) { get_channels(i0,i - 1).move_to(res); i0 = i; }
  35871. } while (i<_spectrum);
  35872. } break;
  35873. default : {
  35874. const ulongT siz = size();
  35875. ulongT i0 = 0, i = 0;
  35876. do {
  35877. while (i<siz && (*this)[i]==value) ++i;
  35878. if (i>i0) {
  35879. if (keep_values) CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res);
  35880. i0 = i;
  35881. }
  35882. while (i<siz && (*this)[i]!=value) ++i;
  35883. if (i>i0) { CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res); i0 = i; }
  35884. } while (i<siz);
  35885. }
  35886. }
  35887. } else { // Split according to multiple values
  35888. ulongT j = 0;
  35889. switch (_axis) {
  35890. case 'x' : {
  35891. unsigned int i0 = 0, i1 = 0, i = 0;
  35892. do {
  35893. if ((Tt)(*this)(i)==(Tt)*values) {
  35894. i1 = i; j = 0;
  35895. while (i<_width && (Tt)(*this)(i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
  35896. i-=j;
  35897. if (i>i1) {
  35898. if (i1>i0) get_columns(i0,i1 - 1).move_to(res);
  35899. if (keep_values) get_columns(i1,i - 1).move_to(res);
  35900. i0 = i;
  35901. } else ++i;
  35902. } else ++i;
  35903. } while (i<_width);
  35904. if (i0<_width) get_columns(i0,width() - 1).move_to(res);
  35905. } break;
  35906. case 'y' : {
  35907. unsigned int i0 = 0, i1 = 0, i = 0;
  35908. do {
  35909. if ((Tt)(*this)(0,i)==(Tt)*values) {
  35910. i1 = i; j = 0;
  35911. while (i<_height && (Tt)(*this)(0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
  35912. i-=j;
  35913. if (i>i1) {
  35914. if (i1>i0) get_rows(i0,i1 - 1).move_to(res);
  35915. if (keep_values) get_rows(i1,i - 1).move_to(res);
  35916. i0 = i;
  35917. } else ++i;
  35918. } else ++i;
  35919. } while (i<_height);
  35920. if (i0<_height) get_rows(i0,height() - 1).move_to(res);
  35921. } break;
  35922. case 'z' : {
  35923. unsigned int i0 = 0, i1 = 0, i = 0;
  35924. do {
  35925. if ((Tt)(*this)(0,0,i)==(Tt)*values) {
  35926. i1 = i; j = 0;
  35927. while (i<_depth && (Tt)(*this)(0,0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
  35928. i-=j;
  35929. if (i>i1) {
  35930. if (i1>i0) get_slices(i0,i1 - 1).move_to(res);
  35931. if (keep_values) get_slices(i1,i - 1).move_to(res);
  35932. i0 = i;
  35933. } else ++i;
  35934. } else ++i;
  35935. } while (i<_depth);
  35936. if (i0<_depth) get_slices(i0,depth() - 1).move_to(res);
  35937. } break;
  35938. case 'c' : {
  35939. unsigned int i0 = 0, i1 = 0, i = 0;
  35940. do {
  35941. if ((Tt)(*this)(0,0,0,i)==(Tt)*values) {
  35942. i1 = i; j = 0;
  35943. while (i<_spectrum && (Tt)(*this)(0,0,0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
  35944. i-=j;
  35945. if (i>i1) {
  35946. if (i1>i0) get_channels(i0,i1 - 1).move_to(res);
  35947. if (keep_values) get_channels(i1,i - 1).move_to(res);
  35948. i0 = i;
  35949. } else ++i;
  35950. } else ++i;
  35951. } while (i<_spectrum);
  35952. if (i0<_spectrum) get_channels(i0,spectrum() - 1).move_to(res);
  35953. } break;
  35954. default : {
  35955. const ulongT siz = size();
  35956. ulongT i0 = 0, i1 = 0, i = 0;
  35957. do {
  35958. if ((Tt)(*this)[i]==(Tt)*values) {
  35959. i1 = i; j = 0;
  35960. while (i<siz && (Tt)(*this)[i]==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
  35961. i-=j;
  35962. if (i>i1) {
  35963. if (i1>i0) CImg<T>(_data + i0,1,(unsigned int)(i1 - i0)).move_to(res);
  35964. if (keep_values) CImg<T>(_data + i1,1,(unsigned int)(i - i1)).move_to(res);
  35965. i0 = i;
  35966. } else ++i;
  35967. } else ++i;
  35968. } while (i<siz);
  35969. if (i0<siz) CImg<T>(_data + i0,1,(unsigned int)(siz - i0)).move_to(res);
  35970. } break;
  35971. }
  35972. }
  35973. return res;
  35974. }
  35975. //! Append two images along specified axis.
  35976. /**
  35977. \param img Image to append with instance image.
  35978. \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  35979. \param align Append alignment in \c [0,1].
  35980. **/
  35981. template<typename t>
  35982. CImg<T>& append(const CImg<t>& img, const char axis='x', const float align=0) {
  35983. if (is_empty()) return assign(img,false);
  35984. if (!img) return *this;
  35985. return CImgList<T>(*this,true).insert(img).get_append(axis,align).move_to(*this);
  35986. }
  35987. //! Append two images along specified axis \specialization.
  35988. CImg<T>& append(const CImg<T>& img, const char axis='x', const float align=0) {
  35989. if (is_empty()) return assign(img,false);
  35990. if (!img) return *this;
  35991. return CImgList<T>(*this,img,true).get_append(axis,align).move_to(*this);
  35992. }
  35993. //! Append two images along specified axis \const.
  35994. template<typename t>
  35995. CImg<_cimg_Tt> get_append(const CImg<T>& img, const char axis='x', const float align=0) const {
  35996. if (is_empty()) return +img;
  35997. if (!img) return +*this;
  35998. return CImgList<_cimg_Tt>(*this,true).insert(img).get_append(axis,align);
  35999. }
  36000. //! Append two images along specified axis \specialization.
  36001. CImg<T> get_append(const CImg<T>& img, const char axis='x', const float align=0) const {
  36002. if (is_empty()) return +img;
  36003. if (!img) return +*this;
  36004. return CImgList<T>(*this,img,true).get_append(axis,align);
  36005. }
  36006. //@}
  36007. //---------------------------------------
  36008. //
  36009. //! \name Filtering / Transforms
  36010. //@{
  36011. //---------------------------------------
  36012. //! Correlate image by a kernel.
  36013. /**
  36014. \param kernel = the correlation kernel.
  36015. \param boundary_conditions Boundary condition. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  36016. \param is_normalized = enable local normalization.
  36017. \param channel_mode Channel processing mode.
  36018. Can be { 0=all | 1=one for one (default) | 2=partial sum | 3=full sum }.
  36019. \param xcenter X-coordinate of the kernel center (~0U>>1 means 'centered').
  36020. \param ycenter Y-coordinate of the kernel center (~0U>>1 means 'centered').
  36021. \param zcenter Z-coordinate of the kernel center (~0U>>1 means 'centered').
  36022. \param xstart Starting X-coordinate of the instance image.
  36023. \param ystart Starting Y-coordinate of the instance image.
  36024. \param zstart Starting Z-coordinate of the instance image.
  36025. \param xend Ending X-coordinate of the instance image.
  36026. \param yend Ending Y-coordinate of the instance image.
  36027. \param zend Ending Z-coordinate of the instance image.
  36028. \param xstride Stride along the X-axis.
  36029. \param ystride Stride along the Y-axis.
  36030. \param zstride Stride along the Z-axis.
  36031. \param xdilation Dilation along the X-axis.
  36032. \param ydilation Dilation along the Y-axis.
  36033. \param zdilation Dilation along the Z-axis.
  36034. \param interpolation_type Can be { false=nearest | true=linear }.
  36035. \note
  36036. - The correlation of the image instance \p *this by the kernel \p kernel is defined to be:
  36037. res(x,y,z) = sum_{i,j,k} (*this)(\alpha_x\;x + \beta_x\;(i - c_x),\alpha_y\;y + \beta_y\;(j -
  36038. c_y),\alpha_z\;z + \beta_z\;(k - c_z))*kernel(i,j,k).
  36039. **/
  36040. template<typename t>
  36041. CImg<T>& correlate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36042. const bool is_normalized=false, const unsigned int channel_mode=1,
  36043. const int xcenter=(int)(~0U>>1),
  36044. const int ycenter=(int)(~0U>>1),
  36045. const int zcenter=(int)(~0U>>1),
  36046. const int xstart=0,
  36047. const int ystart=0,
  36048. const int zstart=0,
  36049. const int xend=(int)(~0U>>1),
  36050. const int yend=(int)(~0U>>1),
  36051. const int zend=(int)(~0U>>1),
  36052. const float xstride=1, const float ystride=1, const float zstride=1,
  36053. const float xdilation=1, const float ydilation=1, const float zdilation=1,
  36054. const bool interpolation_type=false) {
  36055. if (is_empty() || !kernel) return *this;
  36056. return get_correlate(kernel,boundary_conditions,is_normalized,channel_mode,
  36057. xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
  36058. xstride,ystride,zstride,xdilation,ydilation,zdilation,
  36059. interpolation_type).move_to(*this);
  36060. }
  36061. template<typename t>
  36062. CImg<_cimg_Ttfloat> get_correlate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36063. const bool is_normalized=false, const unsigned int channel_mode=1,
  36064. const int xcenter=(int)(~0U>>1),
  36065. const int ycenter=(int)(~0U>>1),
  36066. const int zcenter=(int)(~0U>>1),
  36067. const int xstart=0,
  36068. const int ystart=0,
  36069. const int zstart=0,
  36070. const int xend=(int)(~0U>>1),
  36071. const int yend=(int)(~0U>>1),
  36072. const int zend=(int)(~0U>>1),
  36073. const float xstride=1, const float ystride=1, const float zstride=1,
  36074. const float xdilation=1, const float ydilation=1, const float zdilation=1,
  36075. const bool interpolation_type=false) const {
  36076. return _correlate(kernel,boundary_conditions,is_normalized,channel_mode,
  36077. xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
  36078. xstride,ystride,zstride,xdilation,ydilation,zdilation,
  36079. interpolation_type,false);
  36080. }
  36081. //! Correlate image by a kernel \newinstance.
  36082. template<typename t>
  36083. CImg<_cimg_Ttfloat> _correlate(const CImg<t>& kernel, const unsigned int boundary_conditions,
  36084. const bool is_normalized, const unsigned int channel_mode,
  36085. const int xcenter, const int ycenter, const int zcenter,
  36086. const int xstart, const int ystart, const int zstart,
  36087. const int xend, const int yend, const int zend,
  36088. const float xstride, const float ystride, const float zstride,
  36089. const float xdilation, const float ydilation, const float zdilation,
  36090. const bool interpolation_type, const bool is_convolve) const {
  36091. typedef _cimg_Ttfloat Ttfloat;
  36092. CImg<Ttfloat> res;
  36093. _cimg_abort_init_openmp;
  36094. cimg_abort_init;
  36095. if (xstart>xend || ystart>yend || zstart>zend)
  36096. throw CImgArgumentException(_cimg_instance
  36097. "%s(): Invalid xyz-start/end arguments (start = (%d,%d,%d), end = (%d,%d,%d)).",
  36098. cimg_instance,
  36099. is_convolve?"convolve":"correlate",
  36100. xstart,ystart,zstart,xend,yend,zend);
  36101. if (xstride<=0 || ystride<=0 || zstride<=0)
  36102. throw CImgArgumentException(_cimg_instance
  36103. "%s(): Invalid stride arguments (%g,%g,%g).",
  36104. cimg_instance,
  36105. is_convolve?"convolve":"correlate",
  36106. xstride,ystride,zstride);
  36107. if (is_empty() || !kernel) return *this;
  36108. int
  36109. _xcenter = xcenter==(int)(~0U>>1)?kernel.width()/2 - 1 + (kernel.width()%2):
  36110. std::min(xcenter,kernel.width() - 1),
  36111. _ycenter = ycenter==(int)(~0U>>1)?kernel.height()/2 - 1 + (kernel.height()%2):
  36112. std::min(ycenter,kernel.height() - 1),
  36113. _zcenter = zcenter==(int)(~0U>>1)?kernel.depth()/2 - 1 + (kernel.depth()%2):
  36114. std::min(zcenter,kernel.depth() - 1);
  36115. float _xdilation = xdilation, _ydilation = ydilation, _zdilation = zdilation;
  36116. CImg<t> _kernel;
  36117. if (is_convolve) { // If convolution, go back to correlation
  36118. if (kernel.size()/kernel.spectrum()<=27) {
  36119. _kernel = CImg<t>(kernel._data,kernel.size()/kernel._spectrum,1,1,kernel._spectrum,true).
  36120. get_mirror('x').resize(kernel,-1);
  36121. _xcenter = kernel.width() - 1 - _xcenter;
  36122. _ycenter = kernel.height() - 1 - _ycenter;
  36123. _zcenter = kernel.depth() - _zcenter - 1;
  36124. } else { _kernel = kernel.get_shared(); _xdilation*=-1; _ydilation*=-1; _zdilation*=-1; }
  36125. } else _kernel = kernel.get_shared();
  36126. const int
  36127. _xend = xend==(int)(~0U>>1)?width() - 1:xend,
  36128. _yend = yend==(int)(~0U>>1)?height() - 1:yend,
  36129. _zend = zend==(int)(~0U>>1)?depth() - 1:zend,
  36130. i_xstride = (int)cimg::round(xstride),
  36131. i_ystride = (int)cimg::round(ystride),
  36132. i_zstride = (int)cimg::round(zstride),
  36133. i_xdilation = (int)cimg::round(_xdilation),
  36134. i_ydilation = (int)cimg::round(_ydilation),
  36135. i_zdilation = (int)cimg::round(_zdilation),
  36136. res_width = _xend - xstart + 1,
  36137. res_height = _yend - ystart + 1,
  36138. res_depth = _zend + zstart + 1,
  36139. smin = std::min(spectrum(),_kernel.spectrum()),
  36140. smax = std::max(spectrum(),_kernel.spectrum()),
  36141. cend = !channel_mode?spectrum()*_kernel.spectrum():smax;
  36142. const ulongT
  36143. res_wh = (ulongT)res_width*res_height,
  36144. res_whd = res_wh*res_depth,
  36145. res_siz = res_whd*res._spectrum;
  36146. if (!res_whd) return CImg<Ttfloat>();
  36147. res.assign(res_width,res_height,res_depth,
  36148. !channel_mode?_spectrum*_kernel._spectrum:
  36149. channel_mode==1?smax:
  36150. channel_mode==2?(int)std::ceil((float)smax/smin):1);
  36151. if (channel_mode>=2) res.fill(0);
  36152. const bool
  36153. #if cimg_use_openmp==1
  36154. is_master_thread = !omp_get_thread_num(),
  36155. #else
  36156. is_master_thread = true,
  36157. #endif
  36158. is_outer_parallel = is_master_thread &&
  36159. (res._spectrum>=cimg::nb_cpus() || res_siz<=(cimg_openmp_sizefactor)*32768),
  36160. is_inner_parallel = is_master_thread &&
  36161. (!is_outer_parallel && res_whd>=(cimg_openmp_sizefactor)*32768),
  36162. is_int_stride_dilation = xstride==i_xstride && ystride==i_ystride && zstride==i_zstride &&
  36163. _xdilation==i_xdilation && _ydilation==i_ydilation && _zdilation==i_zdilation;
  36164. cimg::unused(is_inner_parallel,is_outer_parallel);
  36165. const int
  36166. w = width(), h = height(), d = depth(),
  36167. w1 = w - 1, h1 = h - 1, d1 = d - 1,
  36168. w2 = 2*w, h2 = 2*h, d2 = 2*d;
  36169. const ulongT wh = (ulongT)w*h, whd = wh*d;
  36170. // Reshape kernel to enable optimizations for a few cases.
  36171. if (boundary_conditions==1 &&
  36172. _kernel._width>1 && _kernel._height>1 &&
  36173. ((_kernel._depth==1 && _kernel._width<=5 && _kernel._height<=5) ||
  36174. (_kernel._depth<=3 && _kernel._width<=3 && _kernel._height<=3)) &&
  36175. xstart>=0 && ystart>=0 && zstart>=0 &&
  36176. _xend<width() && _yend<height() && _zend<depth() &&
  36177. is_int_stride_dilation &&
  36178. xstride==1 && ystride==1 && zstride==1 &&
  36179. i_xdilation>=0 && i_ydilation>=0 && i_zdilation>=0) {
  36180. const unsigned int M = cimg::max(_kernel._width,_kernel._height,_kernel._depth);
  36181. _kernel.assign(_kernel.get_resize(M + 1 - (M%2),M + 1 - (M%2),_kernel._depth>1?M + 1 - (M%2):1,-100,
  36182. 0,0,
  36183. 1,1,1),false);
  36184. _xcenter = _ycenter = (int)M/2;
  36185. if (_kernel._depth>1) _ycenter = (int)M/2;
  36186. }
  36187. // Optimized version for a few particular cases (3x3, 5x5 and 3x3x3 kernels, with a few other conditions).
  36188. if (boundary_conditions==1 &&
  36189. _kernel._width==_kernel._height &&
  36190. ((_kernel._depth==1 && (_kernel._width==3 || _kernel._width==5)) ||
  36191. (_kernel._depth==_kernel._width && _kernel._width==3)) &&
  36192. _xcenter==_kernel.width()/2 && _ycenter==_kernel.height()/2 && _zcenter==_kernel.depth()/2 &&
  36193. xstart>=0 && ystart>=0 && zstart>=0 &&
  36194. _xend<width() && _yend<height() && _zend<depth() &&
  36195. is_int_stride_dilation &&
  36196. xstride==1 && ystride==1 && zstride==1 &&
  36197. i_xdilation>=0 && i_ydilation>=0 && i_zdilation>=0) {
  36198. switch (_kernel._depth) {
  36199. case 3 : { // 3x3x3 centered kernel
  36200. cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
  36201. for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
  36202. cimg_abort_test2;
  36203. const CImg<T> I = get_shared_channel(c%_spectrum);
  36204. const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
  36205. CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
  36206. CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
  36207. if (is_normalized) {
  36208. const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
  36209. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36210. cimg_forXYZ(res,X,Y,Z) {
  36211. const int
  36212. x = xstart + X, y = ystart + Y, z = zstart + Z,
  36213. px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
  36214. py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1,
  36215. pz = z - i_zdilation>0?z - i_zdilation:0, nz = z + i_zdilation<d1?z + i_zdilation:d1;
  36216. const Ttfloat N = M2*(cimg::sqr(I(px,py,pz)) + cimg::sqr(I(x,py,pz)) + cimg::sqr(I(nx,py,pz)) +
  36217. cimg::sqr(I(px,y,pz)) + cimg::sqr(I(x,y,pz)) + cimg::sqr(I(nx,y,pz)) +
  36218. cimg::sqr(I(px,ny,pz)) + cimg::sqr(I(x,ny,pz)) + cimg::sqr(I(nx,ny,pz)) +
  36219. cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) + cimg::sqr(I(nx,py,z)) +
  36220. cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) + cimg::sqr(I(nx,y,z)) +
  36221. cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) + cimg::sqr(I(nx,ny,z)) +
  36222. cimg::sqr(I(px,py,nz)) + cimg::sqr(I(x,py,nz)) + cimg::sqr(I(nx,py,nz)) +
  36223. cimg::sqr(I(px,y,nz)) + cimg::sqr(I(x,y,nz)) + cimg::sqr(I(nx,y,nz)) +
  36224. cimg::sqr(I(px,ny,nz)) + cimg::sqr(I(x,ny,nz)) + cimg::sqr(I(nx,ny,nz)));
  36225. _res(X,Y,Z) = (Ttfloat)(N?(K[0]*I(px,py,pz) + K[1]*I(x,py,pz) + K[2]*I(nx,py,pz) +
  36226. K[3]*I(px,y,pz) + K[4]*I(x,y,pz) + K[5]*I(nx,y,pz) +
  36227. K[6]*I(px,ny,pz) + K[7]*I(x,ny,pz) + K[8]*I(nx,ny,pz) +
  36228. K[9]*I(px,py,z) + K[10]*I(x,py,z) + K[11]*I(nx,py,z) +
  36229. K[12]*I(px,y,z) + K[13]*I(x,y,z) + K[14]*I(nx,y,z) +
  36230. K[15]*I(px,ny,z) + K[16]*I(x,ny,z) + K[17]*I(nx,ny,z) +
  36231. K[18]*I(px,py,nz) + K[19]*I(x,py,nz) + K[20]*I(nx,py,nz) +
  36232. K[21]*I(px,y,nz) + K[22]*I(x,y,nz) + K[23]*I(nx,y,nz) +
  36233. K[24]*I(px,ny,nz) + K[25]*I(x,ny,nz) + K[26]*I(nx,ny,nz))/std::sqrt(N):0);
  36234. }
  36235. } else {
  36236. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36237. cimg_forXYZ(res,X,Y,Z) {
  36238. const int
  36239. x = xstart + X, y = ystart + Y, z = zstart + Z,
  36240. px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
  36241. py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1,
  36242. pz = z - i_zdilation>0?z - i_zdilation:0, nz = z + i_zdilation<d1?z + i_zdilation:d1;
  36243. _res(X,Y,Z) = (Ttfloat)(K[0]*I(px,py,pz) + K[1]*I(x,py,pz) + K[2]*I(nx,py,pz) +
  36244. K[3]*I(px,y,pz) + K[4]*I(x,y,pz) + K[5]*I(nx,y,pz) +
  36245. K[6]*I(px,ny,pz) + K[7]*I(x,ny,pz) + K[8]*I(nx,ny,pz) +
  36246. K[9]*I(px,py,z) + K[10]*I(x,py,z) + K[11]*I(nx,py,z) +
  36247. K[12]*I(px,y,z) + K[13]*I(x,y,z) + K[14]*I(nx,y,z) +
  36248. K[15]*I(px,ny,z) + K[16]*I(x,ny,z) + K[17]*I(nx,ny,z) +
  36249. K[18]*I(px,py,nz) + K[19]*I(x,py,nz) + K[20]*I(nx,py,nz) +
  36250. K[21]*I(px,y,nz) + K[22]*I(x,y,nz) + K[23]*I(nx,y,nz) +
  36251. K[24]*I(px,ny,nz) + K[25]*I(x,ny,nz) + K[26]*I(nx,ny,nz));
  36252. }
  36253. }
  36254. if (channel_mode==2)
  36255. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
  36256. else if (channel_mode==3)
  36257. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
  36258. } _cimg_abort_catch_openmp2
  36259. } break;
  36260. default :
  36261. case 1 :
  36262. switch (_kernel._width) {
  36263. case 5 : { // 5x5 centered kernel
  36264. cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
  36265. for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
  36266. cimg_abort_test2;
  36267. const CImg<T> I = get_shared_channel(c%_spectrum);
  36268. const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
  36269. CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
  36270. CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
  36271. if (is_normalized) {
  36272. const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
  36273. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36274. cimg_forXYZ(res,X,Y,z) {
  36275. const int
  36276. x = xstart + X, y = ystart + Y,
  36277. px = x - i_xdilation>0?x - i_xdilation:0, bx = px - i_xdilation>0?px - i_xdilation:0,
  36278. nx = x + i_xdilation<w1?x + i_xdilation:w1, ax = nx + i_xdilation<w1?nx + i_xdilation:w1,
  36279. py = y - i_ydilation>0?y - i_ydilation:0, by = py - i_ydilation>0?py - i_ydilation:0,
  36280. ny = y + i_ydilation<h1?y + i_ydilation:h1, ay = ny + i_ydilation<h1?ny + i_ydilation:h1;
  36281. const Ttfloat N = M2*(cimg::sqr(I(bx,by,z)) + cimg::sqr(I(px,by,z)) + cimg::sqr(I(x,by,z)) +
  36282. cimg::sqr(I(nx,by,z)) + cimg::sqr(I(ax,by,z)) +
  36283. cimg::sqr(I(bx,py,z)) + cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) +
  36284. cimg::sqr(I(nx,py,z)) + cimg::sqr(I(ax,py,z)) +
  36285. cimg::sqr(I(bx,y,z)) + cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) +
  36286. cimg::sqr(I(nx,y,z)) + cimg::sqr(I(ax,y,z)) +
  36287. cimg::sqr(I(bx,ny,z)) + cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) +
  36288. cimg::sqr(I(nx,ny,z)) + cimg::sqr(I(ax,ny,z)) +
  36289. cimg::sqr(I(bx,ay,z)) + cimg::sqr(I(px,ay,z)) + cimg::sqr(I(x,ay,z)) +
  36290. cimg::sqr(I(nx,ay,z)) + cimg::sqr(I(ax,ay,z)));
  36291. _res(X,Y,z) = (Ttfloat)(N?(K[0]*I(bx,by,z) + K[1]*I(px,by,z) + K[2]*I(x,by,z) +
  36292. K[3]*I(nx,by,z) + K[4]*I(ax,by,z) +
  36293. K[5]*I(bx,py,z) + K[6]*I(px,py,z) + K[7]*I(x,py,z) +
  36294. K[8]*I(nx,py,z) + K[9]*I(ax,py,z) +
  36295. K[10]*I(bx,y,z) + K[11]*I(px,y,z) + K[12]*I(x,y,z) +
  36296. K[13]*I(nx,y,z) + K[14]*I(ax,y,z) +
  36297. K[15]*I(bx,ny,z) + K[16]*I(px,ny,z) + K[17]*I(x,ny,z) +
  36298. K[18]*I(nx,ny,z) + K[19]*I(ax,ny,z) +
  36299. K[20]*I(bx,ay,z) + K[21]*I(px,ay,z) + K[22]*I(x,ay,z) +
  36300. K[23]*I(nx,ay,z) + K[24]*I(ax,ay,z))/std::sqrt(N):0);
  36301. }
  36302. } else {
  36303. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  36304. cimg_forXYZ(res,X,Y,z) {
  36305. const int
  36306. x = xstart + X, y = ystart + Y,
  36307. px = x - i_xdilation>0?x - i_xdilation:0, bx = px - i_xdilation>0?px - i_xdilation:0,
  36308. nx = x + i_xdilation<w1?x + i_xdilation:w1, ax = nx + i_xdilation<w1?nx + i_xdilation:w1,
  36309. py = y - i_ydilation>0?y - i_ydilation:0, by = py - i_ydilation>0?py - i_ydilation:0,
  36310. ny = y + i_ydilation<h1?y + i_ydilation:h1, ay = ny + i_ydilation<h1?ny + i_ydilation:h1;
  36311. _res(X,Y,z) = (Ttfloat)(K[0]*I(bx,by,z) + K[1]*I(px,by,z) + K[2]*I(x,by,z) +
  36312. K[3]*I(nx,by,z) + K[4]*I(ax,by,z) +
  36313. K[5]*I(bx,py,z) + K[6]*I(px,py,z) + K[7]*I(x,py,z) +
  36314. K[8]*I(nx,py,z) + K[9]*I(ax,py,z) +
  36315. K[10]*I(bx,y,z) + K[11]*I(px,y,z) + K[12]*I(x,y,z) +
  36316. K[13]*I(nx,y,z) + K[14]*I(ax,y,z) +
  36317. K[15]*I(bx,ny,z) + K[16]*I(px,ny,z) + K[17]*I(x,ny,z) +
  36318. K[18]*I(nx,ny,z) + K[19]*I(ax,ny,z) +
  36319. K[20]*I(bx,ay,z) + K[21]*I(px,ay,z) + K[22]*I(x,ay,z) +
  36320. K[23]*I(nx,ay,z) + K[24]*I(ax,ay,z));
  36321. }
  36322. }
  36323. if (channel_mode==2)
  36324. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
  36325. else if (channel_mode==3)
  36326. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
  36327. } _cimg_abort_catch_openmp2
  36328. } break;
  36329. case 3 : { // 3x3 centered kernel
  36330. cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
  36331. for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
  36332. cimg_abort_test2;
  36333. const CImg<T> I = get_shared_channel(c%_spectrum);
  36334. const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
  36335. CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
  36336. CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
  36337. if (is_normalized) {
  36338. const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
  36339. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36340. cimg_forXYZ(res,X,Y,z) {
  36341. const int
  36342. x = xstart + X, y = ystart + Y,
  36343. px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
  36344. py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1;
  36345. const Ttfloat N = M2*(cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) + cimg::sqr(I(nx,py,z)) +
  36346. cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) + cimg::sqr(I(nx,y,z)) +
  36347. cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) + cimg::sqr(I(nx,ny,z)));
  36348. _res(X,Y,z) = (Ttfloat)(N?(K[0]*I(px,py,z) + K[1]*I(x,py,z) + K[2]*I(nx,py,z) +
  36349. K[3]*I(px,y,z) + K[4]*I(x,y,z) + K[5]*I(nx,y,z) +
  36350. K[6]*I(px,ny,z) + K[7]*I(x,ny,z) + K[8]*I(nx,ny,z))/std::sqrt(N):0);
  36351. }
  36352. } else {
  36353. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  36354. cimg_forXYZ(res,X,Y,z) {
  36355. const int
  36356. x = xstart + X, y = ystart + Y,
  36357. px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
  36358. py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1;
  36359. _res(X,Y,z) = (Ttfloat)(K[0]*I(px,py,z) + K[1]*I(x,py,z) + K[2]*I(nx,py,z) +
  36360. K[3]*I(px,y,z) + K[4]*I(x,y,z) + K[5]*I(nx,y,z) +
  36361. K[6]*I(px,ny,z) + K[7]*I(x,ny,z) + K[8]*I(nx,ny,z));
  36362. }
  36363. }
  36364. if (channel_mode==2)
  36365. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
  36366. else if (channel_mode==3)
  36367. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
  36368. } _cimg_abort_catch_openmp2
  36369. } break;
  36370. }
  36371. }
  36372. } else if (_kernel._width==1 && _kernel._height==1 && _kernel._depth==1 &&
  36373. !_xcenter && !_ycenter && !_zcenter &&
  36374. xstart>=0 && ystart>=0 && zstart>=0 &&
  36375. _xend<width() && _yend<height() && _zend<depth() &&
  36376. xstride==1 && ystride==1 && zstride==1) {
  36377. // Special optimization for 1x1 kernel.
  36378. cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
  36379. for (int c = 0; c<cend; ++c) {
  36380. const t valK = _kernel[!channel_mode?c/_spectrum:c%_kernel._spectrum];
  36381. CImg<T> I = get_crop(xstart,ystart,zstart,c%_spectrum,_xend,_yend,_zend,c%_spectrum)*=valK;
  36382. if (is_normalized) I.sign();
  36383. switch (channel_mode) {
  36384. case 0 : // All
  36385. case 1 : // One for one
  36386. res.get_shared_channel(c) = I;
  36387. break;
  36388. case 2 : // Partial sum
  36389. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=I;
  36390. break;
  36391. case 3 : // Full sum
  36392. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=I;
  36393. break;
  36394. }
  36395. }
  36396. } else { // Generic version
  36397. cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
  36398. for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
  36399. cimg_abort_test2;
  36400. const CImg<T> I = get_shared_channel(c%_spectrum);
  36401. const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
  36402. CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
  36403. CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
  36404. Ttfloat M = 0, M2 = 0;
  36405. if (is_normalized) { M = (Ttfloat)K.magnitude(2); M2 = cimg::sqr(M); }
  36406. #define _cimg_correlate_x_int const int ix = xstart + i_xstride*x + i_xdilation*(p - _xcenter)
  36407. #define _cimg_correlate_y_int const int iy = ystart + i_ystride*y + i_ydilation*(q - _ycenter)
  36408. #define _cimg_correlate_z_int const int iz = zstart + i_zstride*z + i_zdilation*(r - _zcenter)
  36409. #define _cimg_correlate_x_float const float ix = xstart + xstride*x + _xdilation*(p - _xcenter)
  36410. #define _cimg_correlate_y_float const float iy = ystart + ystride*y + _ydilation*(q - _ycenter)
  36411. #define _cimg_correlate_z_float const float iz = zstart + zstride*z + _zdilation*(r - _zcenter)
  36412. #define _cimg_correlate_x_int_dirichlet const bool is_in_x = ix>=0 && ix<w
  36413. #define _cimg_correlate_y_int_dirichlet const bool is_in_y = iy>=0 && iy<h
  36414. #define _cimg_correlate_z_int_dirichlet const bool is_in_z = iz>=0 && iz<d
  36415. #define _cimg_correlate_x_float_dirichlet _cimg_correlate_x_int_dirichlet
  36416. #define _cimg_correlate_y_float_dirichlet _cimg_correlate_y_int_dirichlet
  36417. #define _cimg_correlate_z_float_dirichlet _cimg_correlate_z_int_dirichlet
  36418. #define _cimg_correlate_x_int_neumann const int nix = cimg::cut(ix,0,w1)
  36419. #define _cimg_correlate_y_int_neumann const int niy = cimg::cut(iy,0,h1)
  36420. #define _cimg_correlate_z_int_neumann const int niz = cimg::cut(iz,0,d1)
  36421. #define _cimg_correlate_x_float_neumann const float nix = cimg::cut(ix,0,w1)
  36422. #define _cimg_correlate_y_float_neumann const float niy = cimg::cut(iy,0,h1)
  36423. #define _cimg_correlate_z_float_neumann const float niz = cimg::cut(iz,0,d1)
  36424. #define _cimg_correlate_x_int_periodic const int nix = cimg::mod(ix,w)
  36425. #define _cimg_correlate_y_int_periodic const int niy = cimg::mod(iy,h)
  36426. #define _cimg_correlate_z_int_periodic const int niz = cimg::mod(iz,d)
  36427. #define _cimg_correlate_x_float_periodic const float nix = cimg::mod(ix,w)
  36428. #define _cimg_correlate_y_float_periodic const float niy = cimg::mod(iy,h)
  36429. #define _cimg_correlate_z_float_periodic const float niz = cimg::mod(iz,d)
  36430. #define _cimg_correlate_x_int_mirror const int mx = cimg::mod(ix,w2), nix = mx<w?mx:w2 - mx - 1
  36431. #define _cimg_correlate_y_int_mirror const int my = cimg::mod(iy,h2), niy = my<h?my:h2 - my - 1
  36432. #define _cimg_correlate_z_int_mirror const int mz = cimg::mod(iz,d2), niz = mz<d?mz:d2 - mz - 1
  36433. #define _cimg_correlate_x_float_mirror const float mx = cimg::mod(ix,w2), nix = mx<w?mx:w2 - mx - 1
  36434. #define _cimg_correlate_y_float_mirror const float my = cimg::mod(iy,h2), niy = my<h?my:h2 - my - 1
  36435. #define _cimg_correlate_z_float_mirror const float mz = cimg::mod(iz,d2), niz = mz<d?mz:d2 - mz - 1
  36436. #define _cimg_correlate(type,boundary,access) \
  36437. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel)) \
  36438. cimg_forXYZ(res,x,y,z) { \
  36439. Ttfloat val = 0; \
  36440. const t *pK = K._data; \
  36441. cimg_forZ(_kernel,r) { _cimg_correlate_z_##type; _cimg_correlate_z_##type##_##boundary; \
  36442. cimg_forY(_kernel,q) { _cimg_correlate_y_##type; _cimg_correlate_y_##type##_##boundary; \
  36443. cimg_forX(_kernel,p) { _cimg_correlate_x_##type; _cimg_correlate_x_##type##_##boundary; \
  36444. val+=*(pK++)*(access); \
  36445. } \
  36446. } \
  36447. } \
  36448. _res(x,y,z,0,res_wh,res_whd) = val; \
  36449. }
  36450. #define _cimg_correlate_n(type,boundary,access) \
  36451. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel)) \
  36452. cimg_forXYZ(res,x,y,z) { \
  36453. Ttfloat val = 0, N = 0; \
  36454. const t *pK = K._data; \
  36455. cimg_forZ(_kernel,r) { _cimg_correlate_z_##type; _cimg_correlate_z_##type##_##boundary; \
  36456. cimg_forY(_kernel,q) { _cimg_correlate_y_##type; _cimg_correlate_y_##type##_##boundary; \
  36457. cimg_forX(_kernel,p) { _cimg_correlate_x_##type; _cimg_correlate_x_##type##_##boundary; \
  36458. Ttfloat _val = access; \
  36459. val+=*(pK++)*_val; \
  36460. _val*=_val; N+=_val; \
  36461. } \
  36462. } \
  36463. } \
  36464. N*=M2; _res(x,y,z,0,res_wh,res_whd) = N?val/std::sqrt(N):0; \
  36465. }
  36466. if (is_normalized) { // Normalized convolution/correlation
  36467. if (is_int_stride_dilation) // Integer stride and dilation
  36468. switch (boundary_conditions) {
  36469. case 0 : // Dirichlet
  36470. _cimg_correlate_n(int,dirichlet,is_in_x && is_in_y && is_in_z?I(ix,iy,iz,0,wh,whd):(T)0);
  36471. break;
  36472. case 1 : // Neumann
  36473. _cimg_correlate_n(int,neumann,I(nix,niy,niz,0,wh,whd));
  36474. break;
  36475. case 2 : // Periodic
  36476. _cimg_correlate_n(int,periodic,I(nix,niy,niz,0,wh,whd));
  36477. break;
  36478. case 3 : // Mirror
  36479. _cimg_correlate_n(int,mirror,I(nix,niy,niz,0,wh,whd));
  36480. break;
  36481. }
  36482. else if (interpolation_type) // Non-integer stride or dilation, linear interpolation
  36483. switch (boundary_conditions) {
  36484. case 0 : // Dirichlet
  36485. _cimg_correlate_n(float,dirichlet,is_in_x && is_in_y && is_in_z?I.linear_atXYZ(ix,iy,iz,0,0):0);
  36486. break;
  36487. case 1 : // Neumann
  36488. _cimg_correlate_n(float,neumann,I._linear_atXYZ(nix,niy,niz,0));
  36489. break;
  36490. case 2 : // Periodic
  36491. _cimg_correlate_n(float,periodic,I._linear_atXYZ(nix,niy,niz,0));
  36492. break;
  36493. case 3 : // Mirror
  36494. _cimg_correlate_n(float,mirror,I._linear_atXYZ(nix,niy,niz,0));
  36495. break;
  36496. }
  36497. else // Non-integer stride or dilation, nearest-neighbor interpolation
  36498. switch (boundary_conditions) {
  36499. case 0 : // Dirichlet
  36500. _cimg_correlate_n(float,dirichlet,is_in_x && is_in_y && is_in_z?I((int)ix,(int)iy,(int)iz,0,0):(T)0);
  36501. break;
  36502. case 1 : // Neumann
  36503. _cimg_correlate_n(float,neumann,I((int)nix,(int)niy,(int)niz,0));
  36504. break;
  36505. case 2 : // Periodic
  36506. _cimg_correlate_n(float,periodic,I((int)nix,(int)niy,(int)niz,0));
  36507. break;
  36508. case 3 : // Mirror
  36509. _cimg_correlate_n(float,mirror,I((int)nix,(int)niy,(int)niz,0));
  36510. break;
  36511. }
  36512. } else { // Standard convolution/correlation
  36513. if (is_int_stride_dilation) // Integer stride and dilation
  36514. switch (boundary_conditions) {
  36515. case 0 : // Dirichlet
  36516. _cimg_correlate(int,dirichlet,is_in_x && is_in_y && is_in_z?I(ix,iy,iz,0,wh,whd):(T)0);
  36517. break;
  36518. case 1 : // Neumann
  36519. _cimg_correlate(int,neumann,I(nix,niy,niz,0,wh,whd));
  36520. break;
  36521. case 2 : // Periodic
  36522. _cimg_correlate(int,periodic,I(nix,niy,niz,0,wh,whd));
  36523. break;
  36524. case 3 : // Mirror
  36525. _cimg_correlate(int,mirror,I(nix,niy,niz,0,wh,whd));
  36526. break;
  36527. }
  36528. else if (interpolation_type) // Non-integer stride or dilation, linear interpolation
  36529. switch (boundary_conditions) {
  36530. case 0 : // Dirichlet
  36531. _cimg_correlate(float,dirichlet,is_in_x && is_in_y && is_in_z?I.linear_atXYZ(ix,iy,iz,0,0):0);
  36532. break;
  36533. case 1 : // Neumann
  36534. _cimg_correlate(float,neumann,I._linear_atXYZ(nix,niy,niz,0));
  36535. break;
  36536. case 2 : // Periodic
  36537. _cimg_correlate(float,periodic,I._linear_atXYZ(nix,niy,niz,0));
  36538. break;
  36539. case 3 : // Mirror
  36540. _cimg_correlate(float,mirror,I._linear_atXYZ(nix,niy,niz,0));
  36541. break;
  36542. }
  36543. else // Non-integer stride or dilation, nearest-neighbor interpolation
  36544. switch (boundary_conditions) {
  36545. case 0 : // Dirichlet
  36546. _cimg_correlate(float,dirichlet,is_in_x && is_in_y && is_in_z?I((int)ix,(int)iy,(int)iz,0,0):(T)0);
  36547. break;
  36548. case 1 : // Neumann
  36549. _cimg_correlate(float,neumann,I((int)nix,(int)niy,(int)niz,0));
  36550. break;
  36551. case 2 : // Periodic
  36552. _cimg_correlate(float,periodic,I((int)nix,(int)niy,(int)niz,0));
  36553. break;
  36554. case 3 : // Mirror
  36555. _cimg_correlate(float,mirror,I((int)nix,(int)niy,(int)niz,0));
  36556. break;
  36557. }
  36558. }
  36559. if (channel_mode==2)
  36560. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
  36561. else if (channel_mode==3)
  36562. cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
  36563. } _cimg_abort_catch_openmp2
  36564. }
  36565. cimg_abort_test;
  36566. return res;
  36567. }
  36568. //! Convolve image by a kernel.
  36569. /**
  36570. \param kernel = the correlation kernel.
  36571. \param boundary_conditions Boundary condition. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  36572. \param is_normalized = enable local normalization.
  36573. \param channel_mode Channel processing mode.
  36574. Can be { 0=all | 1=one for one (default) | 2=partial sum | 3=full sum }.
  36575. \param xcenter X-coordinate of the kernel center (~0U means 'centered').
  36576. \param ycenter Y-coordinate of the kernel center (~0U means 'centered').
  36577. \param zcenter Z-coordinate of the kernel center (~0U means 'centered').
  36578. \param xstart Starting X-coordinate of the instance image.
  36579. \param ystart Starting Y-coordinate of the instance image.
  36580. \param zstart Starting Z-coordinate of the instance image.
  36581. \param xend Ending X-coordinate of the instance image.
  36582. \param yend Ending Y-coordinate of the instance image.
  36583. \param zend Ending Z-coordinate of the instance image.
  36584. \param xstride Stride along the X-axis.
  36585. \param ystride Stride along the Y-axis.
  36586. \param zstride Stride along the Z-axis.
  36587. \param xdilation Dilation along the X-axis.
  36588. \param ydilation Dilation along the Y-axis.
  36589. \param zdilation Dilation along the Z-axis.
  36590. \param interpolation_type Can be { false=nearest | true=linear }.
  36591. \note
  36592. - The convolution of the image instance \p *this by the kernel \p kernel is defined to be:
  36593. res(x,y,z) = sum_{i,j,k} (*this)(\alpha_x\;x - \beta_x\;(i - c_x),\alpha_y\;y
  36594. - \beta_y\;(j - c_y),\alpha_z\;z - \beta_z\;(k - c_z))*kernel(i,j,k).
  36595. **/
  36596. template<typename t>
  36597. CImg<T>& convolve(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36598. const bool is_normalized=false, const unsigned int channel_mode=1,
  36599. const int xcenter=(int)(~0U>>1),
  36600. const int ycenter=(int)(~0U>>1),
  36601. const int zcenter=(int)(~0U>>1),
  36602. const int xstart=0,
  36603. const int ystart=0,
  36604. const int zstart=0,
  36605. const int xend=(int)(~0U>>1),
  36606. const int yend=(int)(~0U>>1),
  36607. const int zend=(int)(~0U>>1),
  36608. const float xstride=1, const float ystride=1, const float zstride=1,
  36609. const float xdilation=1, const float ydilation=1, const float zdilation=1,
  36610. const bool interpolation_type=false) {
  36611. if (is_empty() || !kernel) return *this;
  36612. return get_convolve(kernel,boundary_conditions,is_normalized,channel_mode,
  36613. xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
  36614. xstride,ystride,zstride,xdilation,ydilation,zdilation,
  36615. interpolation_type).move_to(*this);
  36616. }
  36617. //! Convolve image by a kernel \newinstance.
  36618. template<typename t>
  36619. CImg<_cimg_Ttfloat> get_convolve(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36620. const bool is_normalized=false, const unsigned int channel_mode=1,
  36621. const int xcenter=(int)(~0U>>1),
  36622. const int ycenter=(int)(~0U>>1),
  36623. const int zcenter=(int)(~0U>>1),
  36624. const int xstart=0,
  36625. const int ystart=0,
  36626. const int zstart=0,
  36627. const int xend=(int)(~0U>>1),
  36628. const int yend=(int)(~0U>>1),
  36629. const int zend=(int)(~0U>>1),
  36630. const float xstride=1, const float ystride=1, const float zstride=1,
  36631. const float xdilation=1, const float ydilation=1, const float zdilation=1,
  36632. const bool interpolation_type=false) const {
  36633. return _correlate(kernel,boundary_conditions,is_normalized,channel_mode,
  36634. xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
  36635. xstride,ystride,zstride,xdilation,ydilation,zdilation,
  36636. interpolation_type,true);
  36637. }
  36638. //! Cumulate image values, optionally along specified axis.
  36639. /**
  36640. \param axis Cumulation axis. Set it to 0 to cumulate all values globally without taking axes into account.
  36641. **/
  36642. CImg<T>& cumulate(const char axis=0) {
  36643. switch (cimg::lowercase(axis)) {
  36644. case 'x' :
  36645. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  36646. _height*_depth*_spectrum>=16))
  36647. cimg_forYZC(*this,y,z,c) {
  36648. T *ptrd = data(0,y,z,c);
  36649. Tlong cumul = (Tlong)0;
  36650. cimg_forX(*this,x) { cumul+=(Tlong)*ptrd; *(ptrd++) = (T)cumul; }
  36651. }
  36652. break;
  36653. case 'y' : {
  36654. const ulongT w = (ulongT)_width;
  36655. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*512 &&
  36656. _width*_depth*_spectrum>=16))
  36657. cimg_forXZC(*this,x,z,c) {
  36658. T *ptrd = data(x,0,z,c);
  36659. Tlong cumul = (Tlong)0;
  36660. cimg_forY(*this,y) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=w; }
  36661. }
  36662. } break;
  36663. case 'z' : {
  36664. const ulongT wh = (ulongT)_width*_height;
  36665. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_depth>=(cimg_openmp_sizefactor)*512 &&
  36666. _width*_depth*_spectrum>=16))
  36667. cimg_forXYC(*this,x,y,c) {
  36668. T *ptrd = data(x,y,0,c);
  36669. Tlong cumul = (Tlong)0;
  36670. cimg_forZ(*this,z) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=wh; }
  36671. }
  36672. } break;
  36673. case 'c' : {
  36674. const ulongT whd = (ulongT)_width*_height*_depth;
  36675. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  36676. cimg_openmp_if(_spectrum>=(cimg_openmp_sizefactor)*512 && _width*_height*_depth>=16))
  36677. cimg_forXYZ(*this,x,y,z) {
  36678. T *ptrd = data(x,y,z,0);
  36679. Tlong cumul = (Tlong)0;
  36680. cimg_forC(*this,c) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=whd; }
  36681. }
  36682. } break;
  36683. default : { // Global cumulation
  36684. Tlong cumul = (Tlong)0;
  36685. cimg_for(*this,ptrd,T) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; }
  36686. }
  36687. }
  36688. return *this;
  36689. }
  36690. //! Cumulate image values, optionally along specified axis \newinstance.
  36691. CImg<Tlong> get_cumulate(const char axis=0) const {
  36692. return CImg<Tlong>(*this,false).cumulate(axis);
  36693. }
  36694. //! Cumulate image values, along specified axes.
  36695. /**
  36696. \param axes Cumulation axes, as a C-string.
  36697. \note \c axes may contains multiple characters, e.g. \c "xyz"
  36698. **/
  36699. CImg<T>& cumulate(const char *const axes) {
  36700. for (const char *s = axes; *s; ++s) cumulate(*s);
  36701. return *this;
  36702. }
  36703. //! Cumulate image values, along specified axes \newinstance.
  36704. CImg<Tlong> get_cumulate(const char *const axes) const {
  36705. return CImg<Tlong>(*this,false).cumulate(axes);
  36706. }
  36707. //! Erode image by a structuring element.
  36708. /**
  36709. \param kernel Structuring element.
  36710. \param boundary_conditions Boundary conditions.
  36711. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  36712. \param is_real Do the erosion in real (a.k.a 'non-flat') mode (\c true) rather than binary mode (\c false).
  36713. **/
  36714. template<typename t>
  36715. CImg<T>& erode(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36716. const bool is_real=false) {
  36717. if (is_empty() || !kernel) return *this;
  36718. return get_erode(kernel,boundary_conditions,is_real).move_to(*this);
  36719. }
  36720. //! Erode image by a structuring element \newinstance.
  36721. template<typename t>
  36722. CImg<_cimg_Tt> get_erode(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  36723. const bool is_real=false) const {
  36724. if (is_empty() || !kernel) return *this;
  36725. if (!is_real && kernel==0) return CImg<T>(width(),height(),depth(),spectrum(),0);
  36726. typedef _cimg_Tt Tt;
  36727. CImg<Tt> res(_width,_height,_depth,std::max(_spectrum,kernel._spectrum));
  36728. const int
  36729. mx2 = kernel.width()/2, my2 = kernel.height()/2, mz2 = kernel.depth()/2,
  36730. mx1 = kernel.width() - mx2 - 1, my1 = kernel.height() - my2 - 1, mz1 = kernel.depth() - mz2 - 1,
  36731. mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2,
  36732. w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
  36733. const bool
  36734. is_inner_parallel = _width*_height*_depth>=(cimg_openmp_sizefactor)*32768,
  36735. is_outer_parallel = res.size()>=(cimg_openmp_sizefactor)*32768;
  36736. cimg::unused(is_inner_parallel,is_outer_parallel);
  36737. _cimg_abort_init_openmp;
  36738. cimg_abort_init;
  36739. cimg_pragma_openmp(parallel for cimg_openmp_if(!is_inner_parallel && is_outer_parallel))
  36740. cimg_forC(res,c) _cimg_abort_try_openmp {
  36741. cimg_abort_test;
  36742. const CImg<T> img = get_shared_channel(c%_spectrum);
  36743. const CImg<t> K = kernel.get_shared_channel(c%kernel._spectrum);
  36744. if (is_real) { // Real erosion
  36745. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36746. for (int z = mz1; z<mze; ++z)
  36747. for (int y = my1; y<mye; ++y)
  36748. for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
  36749. cimg_abort_test2;
  36750. Tt min_val = cimg::type<Tt>::max();
  36751. for (int zm = -mz1; zm<=mz2; ++zm)
  36752. for (int ym = -my1; ym<=my2; ++ym)
  36753. for (int xm = -mx1; xm<=mx2; ++xm) {
  36754. const t mval = K(mx1 + xm,my1 + ym,mz1 + zm);
  36755. const Tt cval = (Tt)(img(x + xm,y + ym,z + zm) - mval);
  36756. if (cval<min_val) min_val = cval;
  36757. }
  36758. res(x,y,z,c) = min_val;
  36759. } _cimg_abort_catch_openmp2
  36760. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  36761. cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
  36762. cimg_abort_test2;
  36763. for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
  36764. Tt min_val = cimg::type<Tt>::max();
  36765. for (int zm = -mz1; zm<=mz2; ++zm)
  36766. for (int ym = -my1; ym<=my2; ++ym)
  36767. for (int xm = -mx1; xm<=mx2; ++xm) {
  36768. const t mval = K(mx1 + xm,my1 + ym,mz1 + zm);
  36769. Tt cval;
  36770. switch (boundary_conditions) {
  36771. case 0 : cval = (Tt)(img.atXYZ(x + xm,y + ym,z + zm,0,(T)0) - mval); break;
  36772. case 1 : cval = (Tt)(img._atXYZ(x + xm,y + ym,z + zm) - mval); break;
  36773. case 2 : {
  36774. const int
  36775. nx = cimg::mod(x + xm,width()),
  36776. ny = cimg::mod(y + ym,height()),
  36777. nz = cimg::mod(z + zm,depth());
  36778. cval = img(nx,ny,nz) - mval;
  36779. } break;
  36780. default : {
  36781. const int
  36782. tx = cimg::mod(x + xm,w2),
  36783. ty = cimg::mod(y + ym,h2),
  36784. tz = cimg::mod(z + zm,d2),
  36785. nx = tx<width()?tx:w2 - tx - 1,
  36786. ny = ty<height()?ty:h2 - ty - 1,
  36787. nz = tz<depth()?tz:d2 - tz - 1;
  36788. cval = img(nx,ny,nz) - mval;
  36789. }
  36790. }
  36791. if (cval<min_val) min_val = cval;
  36792. }
  36793. res(x,y,z,c) = min_val;
  36794. }
  36795. } _cimg_abort_catch_openmp2
  36796. } else { // Binary erosion
  36797. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  36798. for (int z = mz1; z<mze; ++z)
  36799. for (int y = my1; y<mye; ++y)
  36800. for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
  36801. cimg_abort_test2;
  36802. Tt min_val = cimg::type<Tt>::max();
  36803. for (int zm = -mz1; zm<=mz2; ++zm)
  36804. for (int ym = -my1; ym<=my2; ++ym)
  36805. for (int xm = -mx1; xm<=mx2; ++xm)
  36806. if (K(mx1 + xm,my1 + ym,mz1 + zm)) {
  36807. const Tt cval = (Tt)img(x + xm,y + ym,z + zm);
  36808. if (cval<min_val) min_val = cval;
  36809. }
  36810. res(x,y,z,c) = min_val;
  36811. } _cimg_abort_catch_openmp2
  36812. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  36813. cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
  36814. cimg_abort_test2;
  36815. for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
  36816. Tt min_val = cimg::type<Tt>::max();
  36817. for (int zm = -mz1; zm<=mz2; ++zm)
  36818. for (int ym = -my1; ym<=my2; ++ym)
  36819. for (int xm = -mx1; xm<=mx2; ++xm) {
  36820. if (K(mx1 + xm,my1 + ym,mz1 + zm)) {
  36821. Tt cval;
  36822. switch (boundary_conditions) {
  36823. case 0 : cval = (Tt)img.atXYZ(x + xm,y + ym,z + zm,0,(T)0); break;
  36824. case 1 : cval = (Tt)img._atXYZ(x + xm,y + ym,z + zm); break;
  36825. case 2 : {
  36826. const int
  36827. nx = cimg::mod(x + xm,width()),
  36828. ny = cimg::mod(y + ym,height()),
  36829. nz = cimg::mod(z + zm,depth());
  36830. cval = img(nx,ny,nz);
  36831. } break;
  36832. default : {
  36833. const int
  36834. tx = cimg::mod(x + xm,w2),
  36835. ty = cimg::mod(y + ym,h2),
  36836. tz = cimg::mod(z + zm,d2),
  36837. nx = tx<width()?tx:w2 - tx - 1,
  36838. ny = ty<height()?ty:h2 - ty - 1,
  36839. nz = tz<depth()?tz:d2 - tz - 1;
  36840. cval = img(nx,ny,nz);
  36841. }
  36842. }
  36843. if (cval<min_val) min_val = cval;
  36844. }
  36845. }
  36846. res(x,y,z,c) = min_val;
  36847. }
  36848. } _cimg_abort_catch_openmp2
  36849. }
  36850. } _cimg_abort_catch_openmp
  36851. cimg_abort_test;
  36852. return res;
  36853. }
  36854. //! Erode image by a rectangular structuring element of specified size.
  36855. /**
  36856. \param sx Width of the structuring element.
  36857. \param sy Height of the structuring element.
  36858. \param sz Depth of the structuring element.
  36859. **/
  36860. CImg<T>& erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) {
  36861. if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this;
  36862. if (sx>1 && _width>1) { // Along X-axis
  36863. const int L = width(), off = 1, s = (int)sx, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2;
  36864. CImg<T> buf(L);
  36865. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  36866. cimg_forYZC(*this,y,z,c) {
  36867. T *const ptrdb = buf._data, *ptrd = buf._data, *const ptrde = buf._data + L - 1;
  36868. const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  36869. T cur = *ptrs; ptrs+=off; bool is_first = true;
  36870. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  36871. const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }}
  36872. *(ptrd++) = cur;
  36873. if (ptrs>=ptrse) {
  36874. T *pd = data(0,y,z,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  36875. } else {
  36876. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  36877. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
  36878. *(ptrd++) = cur;
  36879. }
  36880. for (int p = L - s - 1; p>0; --p) {
  36881. const T val = *ptrs; ptrs+=off;
  36882. if (is_first) {
  36883. const T *nptrs = ptrs - off; cur = val;
  36884. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
  36885. nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
  36886. } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  36887. *(ptrd++) = cur;
  36888. }
  36889. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  36890. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  36891. const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
  36892. }
  36893. *(ptrd--) = cur;
  36894. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  36895. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
  36896. }
  36897. T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  36898. }
  36899. }
  36900. }
  36901. if (sy>1 && _height>1) { // Along Y-axis
  36902. const int L = height(), off = width(), s = (int)sy, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1,
  36903. s2 = _s2>L?L:_s2;
  36904. CImg<T> buf(L);
  36905. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  36906. cimg_forXZC(*this,x,z,c) {
  36907. T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
  36908. const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  36909. T cur = *ptrs; ptrs+=off; bool is_first = true;
  36910. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  36911. const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
  36912. }
  36913. *(ptrd++) = cur;
  36914. if (ptrs>=ptrse) {
  36915. T *pd = data(x,0,z,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  36916. } else {
  36917. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  36918. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
  36919. *(ptrd++) = cur;
  36920. }
  36921. for (int p = L - s - 1; p>0; --p) {
  36922. const T val = *ptrs; ptrs+=off;
  36923. if (is_first) {
  36924. const T *nptrs = ptrs - off; cur = val;
  36925. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
  36926. nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
  36927. } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  36928. *(ptrd++) = cur;
  36929. }
  36930. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  36931. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  36932. const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
  36933. }
  36934. *(ptrd--) = cur;
  36935. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  36936. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
  36937. }
  36938. T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  36939. }
  36940. }
  36941. }
  36942. if (sz>1 && _depth>1) { // Along Z-axis
  36943. const int L = depth(), off = width()*height(), s = (int)sz, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1,
  36944. s2 = _s2>L?L:_s2;
  36945. CImg<T> buf(L);
  36946. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  36947. cimg_forXYC(*this,x,y,c) {
  36948. T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
  36949. const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  36950. T cur = *ptrs; ptrs+=off; bool is_first = true;
  36951. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  36952. const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
  36953. }
  36954. *(ptrd++) = cur;
  36955. if (ptrs>=ptrse) {
  36956. T *pd = data(x,y,0,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  36957. } else {
  36958. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  36959. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
  36960. *(ptrd++) = cur;
  36961. }
  36962. for (int p = L - s - 1; p>0; --p) {
  36963. const T val = *ptrs; ptrs+=off;
  36964. if (is_first) {
  36965. const T *nptrs = ptrs - off; cur = val;
  36966. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
  36967. nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
  36968. } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  36969. *(ptrd++) = cur;
  36970. }
  36971. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  36972. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  36973. const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
  36974. }
  36975. *(ptrd--) = cur;
  36976. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  36977. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
  36978. }
  36979. T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  36980. }
  36981. }
  36982. }
  36983. return *this;
  36984. }
  36985. //! Erode image by a rectangular structuring element of specified size \newinstance.
  36986. CImg<T> get_erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const {
  36987. return (+*this).erode(sx,sy,sz);
  36988. }
  36989. //! Erode the image by a square structuring element of specified size.
  36990. /**
  36991. \param s Size of the structuring element.
  36992. **/
  36993. CImg<T>& erode(const unsigned int s) {
  36994. return erode(s,s,s);
  36995. }
  36996. //! Erode the image by a square structuring element of specified size \newinstance.
  36997. CImg<T> get_erode(const unsigned int s) const {
  36998. return (+*this).erode(s);
  36999. }
  37000. //! Dilate image by a structuring element.
  37001. /**
  37002. \param kernel Structuring element.
  37003. \param boundary_conditions Boundary conditions.
  37004. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
  37005. \param is_real Do the dilation in real (a.k.a 'non-flat') mode (\c true) rather than binary mode (\c false).
  37006. **/
  37007. template<typename t>
  37008. CImg<T>& dilate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  37009. const bool is_real=false) {
  37010. if (is_empty() || !kernel) return *this;
  37011. return get_dilate(kernel,boundary_conditions,is_real).move_to(*this);
  37012. }
  37013. //! Dilate image by a structuring element \newinstance.
  37014. template<typename t>
  37015. CImg<_cimg_Tt> get_dilate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
  37016. const bool is_real=false) const {
  37017. if (is_empty() || !kernel || (!is_real && kernel==0)) return *this;
  37018. typedef _cimg_Tt Tt;
  37019. CImg<Tt> res(_width,_height,_depth,std::max(_spectrum,kernel._spectrum));
  37020. const int
  37021. mx1 = kernel.width()/2, my1 = kernel.height()/2, mz1 = kernel.depth()/2,
  37022. mx2 = kernel.width() - mx1 - 1, my2 = kernel.height() - my1 - 1, mz2 = kernel.depth() - mz1 - 1,
  37023. mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2,
  37024. w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
  37025. const bool
  37026. is_inner_parallel = _width*_height*_depth>=(cimg_openmp_sizefactor)*32768,
  37027. is_outer_parallel = res.size()>=(cimg_openmp_sizefactor)*32768;
  37028. cimg::unused(is_inner_parallel,is_outer_parallel);
  37029. _cimg_abort_init_openmp;
  37030. cimg_abort_init;
  37031. cimg_pragma_openmp(parallel for cimg_openmp_if(!is_inner_parallel && is_outer_parallel))
  37032. cimg_forC(res,c) _cimg_abort_try_openmp {
  37033. cimg_abort_test;
  37034. const CImg<T> img = get_shared_channel(c%_spectrum);
  37035. const CImg<t> K = kernel.get_shared_channel(c%kernel._spectrum);
  37036. if (is_real) { // Real dilation
  37037. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  37038. for (int z = mz1; z<mze; ++z)
  37039. for (int y = my1; y<mye; ++y)
  37040. for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
  37041. cimg_abort_test2;
  37042. Tt max_val = cimg::type<Tt>::min();
  37043. for (int zm = -mz1; zm<=mz2; ++zm)
  37044. for (int ym = -my1; ym<=my2; ++ym)
  37045. for (int xm = -mx1; xm<=mx2; ++xm) {
  37046. const t mval = K(mx2 - xm,my2 - ym,mz2 - zm);
  37047. const Tt cval = (Tt)(img(x + xm,y + ym,z + zm) + mval);
  37048. if (cval>max_val) max_val = cval;
  37049. }
  37050. res(x,y,z,c) = max_val;
  37051. } _cimg_abort_catch_openmp2
  37052. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  37053. cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
  37054. cimg_abort_test2;
  37055. for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
  37056. Tt max_val = cimg::type<Tt>::min();
  37057. for (int zm = -mz1; zm<=mz2; ++zm)
  37058. for (int ym = -my1; ym<=my2; ++ym)
  37059. for (int xm = -mx1; xm<=mx2; ++xm) {
  37060. const t mval = K(mx2 - xm,my2 - ym,mz2 - zm);
  37061. Tt cval;
  37062. switch (boundary_conditions) {
  37063. case 0 : cval = (Tt)(img.atXYZ(x + xm,y + ym,z + zm,0,(T)0) + mval); break;
  37064. case 1 : cval = (Tt)(img._atXYZ(x + xm,y + ym,z + zm) + mval); break;
  37065. case 2 : {
  37066. const int
  37067. nx = cimg::mod(x + xm,width()),
  37068. ny = cimg::mod(y + ym,height()),
  37069. nz = cimg::mod(z + zm,depth());
  37070. cval = img(nx,ny,nz) + mval;
  37071. } break;
  37072. default : {
  37073. const int
  37074. tx = cimg::mod(x + xm,w2),
  37075. ty = cimg::mod(y + ym,h2),
  37076. tz = cimg::mod(z + zm,d2),
  37077. nx = tx<width()?tx:w2 - tx - 1,
  37078. ny = ty<height()?ty:h2 - ty - 1,
  37079. nz = tz<depth()?tz:d2 - tz - 1;
  37080. cval = img(nx,ny,nz) + mval;
  37081. }
  37082. }
  37083. if (cval>max_val) max_val = cval;
  37084. }
  37085. res(x,y,z,c) = max_val;
  37086. }
  37087. } _cimg_abort_catch_openmp2
  37088. } else { // Binary dilation
  37089. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
  37090. for (int z = mz1; z<mze; ++z)
  37091. for (int y = my1; y<mye; ++y)
  37092. for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
  37093. cimg_abort_test2;
  37094. Tt max_val = cimg::type<Tt>::min();
  37095. for (int zm = -mz1; zm<=mz2; ++zm)
  37096. for (int ym = -my1; ym<=my2; ++ym)
  37097. for (int xm = -mx1; xm<=mx2; ++xm)
  37098. if (K(mx2 - xm,my2 - ym,mz2 - zm)) {
  37099. const Tt cval = (Tt)img(x + xm,y + ym,z + zm);
  37100. if (cval>max_val) max_val = cval;
  37101. }
  37102. res(x,y,z,c) = max_val;
  37103. } _cimg_abort_catch_openmp2
  37104. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
  37105. cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
  37106. cimg_abort_test2;
  37107. for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
  37108. Tt max_val = cimg::type<Tt>::min();
  37109. for (int zm = -mz1; zm<=mz2; ++zm)
  37110. for (int ym = -my1; ym<=my2; ++ym)
  37111. for (int xm = -mx1; xm<=mx2; ++xm) {
  37112. if (K(mx2 - xm,my2 - ym,mz2 - zm)) {
  37113. Tt cval;
  37114. switch (boundary_conditions) {
  37115. case 0 : cval = (Tt)img.atXYZ(x + xm,y + ym,z + zm,0,(T)0); break;
  37116. case 1 : cval = (Tt)img._atXYZ(x + xm,y + ym,z + zm); break;
  37117. case 2 : {
  37118. const int
  37119. nx = cimg::mod(x + xm,width()),
  37120. ny = cimg::mod(y + ym,height()),
  37121. nz = cimg::mod(z + zm,depth());
  37122. cval = img(nx,ny,nz);
  37123. } break;
  37124. default : {
  37125. const int
  37126. tx = cimg::mod(x + xm,w2),
  37127. ty = cimg::mod(y + ym,h2),
  37128. tz = cimg::mod(z + zm,d2),
  37129. nx = tx<width()?tx:w2 - tx - 1,
  37130. ny = ty<height()?ty:h2 - ty - 1,
  37131. nz = tz<depth()?tz:d2 - tz - 1;
  37132. cval = img(nx,ny,nz);
  37133. }
  37134. }
  37135. if (cval>max_val) max_val = cval;
  37136. }
  37137. }
  37138. res(x,y,z,c) = max_val;
  37139. }
  37140. } _cimg_abort_catch_openmp2
  37141. }
  37142. } _cimg_abort_catch_openmp
  37143. cimg_abort_test;
  37144. return res;
  37145. }
  37146. //! Dilate image by a rectangular structuring element of specified size.
  37147. /**
  37148. \param sx Width of the structuring element.
  37149. \param sy Height of the structuring element.
  37150. \param sz Depth of the structuring element.
  37151. **/
  37152. CImg<T>& dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) {
  37153. if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this;
  37154. if (sx>1 && _width>1) { // Along X-axis
  37155. const int L = width(), off = 1, s = (int)sx, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1, s2 = _s2>L?L:_s2;
  37156. CImg<T> buf(L);
  37157. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  37158. cimg_forYZC(*this,y,z,c) {
  37159. T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
  37160. const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  37161. T cur = *ptrs; ptrs+=off; bool is_first = true;
  37162. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  37163. const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37164. }
  37165. *(ptrd++) = cur;
  37166. if (ptrs>=ptrse) {
  37167. T *pd = data(0,y,z,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  37168. } else {
  37169. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  37170. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37171. *(ptrd++) = cur;
  37172. }
  37173. for (int p = L - s - 1; p>0; --p) {
  37174. const T val = *ptrs; ptrs+=off;
  37175. if (is_first) {
  37176. const T *nptrs = ptrs - off; cur = val;
  37177. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
  37178. nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
  37179. } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  37180. *(ptrd++) = cur;
  37181. }
  37182. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  37183. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  37184. const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
  37185. }
  37186. *(ptrd--) = cur;
  37187. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  37188. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
  37189. }
  37190. T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  37191. }
  37192. }
  37193. }
  37194. if (sy>1 && _height>1) { // Along Y-axis
  37195. const int L = height(), off = width(), s = (int)sy, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1,
  37196. s2 = _s2>L?L:_s2;
  37197. CImg<T> buf(L);
  37198. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  37199. cimg_forXZC(*this,x,z,c) {
  37200. T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
  37201. const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  37202. T cur = *ptrs; ptrs+=off; bool is_first = true;
  37203. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  37204. const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37205. }
  37206. *(ptrd++) = cur;
  37207. if (ptrs>=ptrse) {
  37208. T *pd = data(x,0,z,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  37209. } else {
  37210. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  37211. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37212. *(ptrd++) = cur;
  37213. }
  37214. for (int p = L - s - 1; p>0; --p) {
  37215. const T val = *ptrs; ptrs+=off;
  37216. if (is_first) {
  37217. const T *nptrs = ptrs - off; cur = val;
  37218. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
  37219. nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
  37220. } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  37221. *(ptrd++) = cur;
  37222. }
  37223. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  37224. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  37225. const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
  37226. }
  37227. *(ptrd--) = cur;
  37228. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  37229. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
  37230. }
  37231. T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  37232. }
  37233. }
  37234. }
  37235. if (sz>1 && _depth>1) { // Along Z-axis
  37236. const int L = depth(), off = width()*height(), s = (int)sz, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1,
  37237. s2 = _s2>L?L:_s2;
  37238. CImg<T> buf(L);
  37239. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
  37240. cimg_forXYC(*this,x,y,c) {
  37241. T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
  37242. const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
  37243. T cur = *ptrs; ptrs+=off; bool is_first = true;
  37244. for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
  37245. const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37246. }
  37247. *(ptrd++) = cur;
  37248. if (ptrs>=ptrse) {
  37249. T *pd = data(x,y,0,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
  37250. } else {
  37251. for (int p = s1; p>0 && ptrd<=ptrde; --p) {
  37252. const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
  37253. *(ptrd++) = cur;
  37254. }
  37255. for (int p = L - s - 1; p>0; --p) {
  37256. const T val = *ptrs; ptrs+=off;
  37257. if (is_first) {
  37258. const T *nptrs = ptrs - off; cur = val;
  37259. for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
  37260. nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
  37261. } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
  37262. *(ptrd++) = cur;
  37263. }
  37264. ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
  37265. for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
  37266. const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
  37267. }
  37268. *(ptrd--) = cur;
  37269. for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
  37270. const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
  37271. }
  37272. T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
  37273. }
  37274. }
  37275. }
  37276. return *this;
  37277. }
  37278. //! Dilate image by a rectangular structuring element of specified size \newinstance.
  37279. CImg<T> get_dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const {
  37280. return (+*this).dilate(sx,sy,sz);
  37281. }
  37282. //! Dilate image by a square structuring element of specified size.
  37283. /**
  37284. \param s Size of the structuring element.
  37285. **/
  37286. CImg<T>& dilate(const unsigned int s) {
  37287. return dilate(s,s,s);
  37288. }
  37289. //! Dilate image by a square structuring element of specified size \newinstance.
  37290. CImg<T> get_dilate(const unsigned int s) const {
  37291. return (+*this).dilate(s);
  37292. }
  37293. //! Compute watershed transform.
  37294. /**
  37295. \param priority Priority map.
  37296. \param is_high_connectivity Boolean that choose between 4(false)- or 8(true)-connectivity
  37297. in 2D case, and between 6(false)- or 26(true)-connectivity in 3D case.
  37298. \note Non-zero values of the instance instance are propagated to zero-valued ones according to
  37299. specified the priority map.
  37300. **/
  37301. template<typename t>
  37302. CImg<T>& watershed(const CImg<t>& priority, const bool is_high_connectivity=false) {
  37303. #define _cimg_watershed_init(cond,X,Y,Z) \
  37304. if (cond && !(*this)(X,Y,Z)) Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,nb_seeds)
  37305. #define _cimg_watershed_propagate(cond,X,Y,Z) \
  37306. if (cond) { \
  37307. if ((*this)(X,Y,Z)) { \
  37308. ns = labels(X,Y,Z) - 1; xs = seeds(ns,0); ys = seeds(ns,1); zs = seeds(ns,2); \
  37309. d = cimg::sqr((float)x - xs) + cimg::sqr((float)y - ys) + cimg::sqr((float)z - zs); \
  37310. if (d<dmin) { dmin = d; nmin = ns; nlabel = (*this)(xs,ys,zs); } \
  37311. } else Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,n); \
  37312. }
  37313. if (is_empty()) return *this;
  37314. if (!is_sameXYZ(priority))
  37315. throw CImgArgumentException(_cimg_instance
  37316. "watershed(): image instance and specified priority (%u,%u,%u,%u,%p) "
  37317. "have different dimensions.",
  37318. cimg_instance,
  37319. priority._width,priority._height,priority._depth,priority._spectrum,priority._data);
  37320. if (_spectrum!=1) {
  37321. cimg_forC(*this,c)
  37322. get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum));
  37323. return *this;
  37324. }
  37325. CImg<uintT> labels(_width,_height,_depth,1,0), seeds(64,3);
  37326. CImg<typename cimg::superset2<T,t,int>::type> Q;
  37327. unsigned int sizeQ = 0;
  37328. int px, nx, py, ny, pz, nz;
  37329. bool is_px, is_nx, is_py, is_ny, is_pz, is_nz;
  37330. const bool is_3d = _depth>1;
  37331. // Find seed points and insert them in priority queue.
  37332. unsigned int nb_seeds = 0;
  37333. const T *ptrs = _data;
  37334. cimg_forXYZ(*this,x,y,z) if (*(ptrs++)) { // 3D version
  37335. if (nb_seeds>=seeds._width) seeds.resize(2*seeds._width,3,1,1,0);
  37336. seeds(nb_seeds,0) = x; seeds(nb_seeds,1) = y; seeds(nb_seeds++,2) = z;
  37337. px = x - 1; nx = x + 1;
  37338. py = y - 1; ny = y + 1;
  37339. pz = z - 1; nz = z + 1;
  37340. is_px = px>=0; is_nx = nx<width();
  37341. is_py = py>=0; is_ny = ny<height();
  37342. is_pz = pz>=0; is_nz = nz<depth();
  37343. _cimg_watershed_init(is_px,px,y,z);
  37344. _cimg_watershed_init(is_nx,nx,y,z);
  37345. _cimg_watershed_init(is_py,x,py,z);
  37346. _cimg_watershed_init(is_ny,x,ny,z);
  37347. if (is_3d) {
  37348. _cimg_watershed_init(is_pz,x,y,pz);
  37349. _cimg_watershed_init(is_nz,x,y,nz);
  37350. }
  37351. if (is_high_connectivity) {
  37352. _cimg_watershed_init(is_px && is_py,px,py,z);
  37353. _cimg_watershed_init(is_nx && is_py,nx,py,z);
  37354. _cimg_watershed_init(is_px && is_ny,px,ny,z);
  37355. _cimg_watershed_init(is_nx && is_ny,nx,ny,z);
  37356. if (is_3d) {
  37357. _cimg_watershed_init(is_px && is_pz,px,y,pz);
  37358. _cimg_watershed_init(is_nx && is_pz,nx,y,pz);
  37359. _cimg_watershed_init(is_px && is_nz,px,y,nz);
  37360. _cimg_watershed_init(is_nx && is_nz,nx,y,nz);
  37361. _cimg_watershed_init(is_py && is_pz,x,py,pz);
  37362. _cimg_watershed_init(is_ny && is_pz,x,ny,pz);
  37363. _cimg_watershed_init(is_py && is_nz,x,py,nz);
  37364. _cimg_watershed_init(is_ny && is_nz,x,ny,nz);
  37365. _cimg_watershed_init(is_px && is_py && is_pz,px,py,pz);
  37366. _cimg_watershed_init(is_nx && is_py && is_pz,nx,py,pz);
  37367. _cimg_watershed_init(is_px && is_ny && is_pz,px,ny,pz);
  37368. _cimg_watershed_init(is_nx && is_ny && is_pz,nx,ny,pz);
  37369. _cimg_watershed_init(is_px && is_py && is_nz,px,py,nz);
  37370. _cimg_watershed_init(is_nx && is_py && is_nz,nx,py,nz);
  37371. _cimg_watershed_init(is_px && is_ny && is_nz,px,ny,nz);
  37372. _cimg_watershed_init(is_nx && is_ny && is_nz,nx,ny,nz);
  37373. }
  37374. }
  37375. labels(x,y,z) = nb_seeds;
  37376. }
  37377. // Start watershed computation.
  37378. while (sizeQ) {
  37379. // Get and remove point with maximal priority from the queue.
  37380. const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3);
  37381. const unsigned int n = labels(x,y,z);
  37382. px = x - 1; nx = x + 1;
  37383. py = y - 1; ny = y + 1;
  37384. pz = z - 1; nz = z + 1;
  37385. is_px = px>=0; is_nx = nx<width();
  37386. is_py = py>=0; is_ny = ny<height();
  37387. is_pz = pz>=0; is_nz = nz<depth();
  37388. // Check labels of the neighbors.
  37389. Q._priority_queue_remove(sizeQ);
  37390. unsigned int xs, ys, zs, ns, nmin = 0;
  37391. float d, dmin = cimg::type<float>::inf();
  37392. T nlabel = (T)0;
  37393. _cimg_watershed_propagate(is_px,px,y,z);
  37394. _cimg_watershed_propagate(is_nx,nx,y,z);
  37395. _cimg_watershed_propagate(is_py,x,py,z);
  37396. _cimg_watershed_propagate(is_ny,x,ny,z);
  37397. if (is_3d) {
  37398. _cimg_watershed_propagate(is_pz,x,y,pz);
  37399. _cimg_watershed_propagate(is_nz,x,y,nz);
  37400. }
  37401. if (is_high_connectivity) {
  37402. _cimg_watershed_propagate(is_px && is_py,px,py,z);
  37403. _cimg_watershed_propagate(is_nx && is_py,nx,py,z);
  37404. _cimg_watershed_propagate(is_px && is_ny,px,ny,z);
  37405. _cimg_watershed_propagate(is_nx && is_ny,nx,ny,z);
  37406. if (is_3d) {
  37407. _cimg_watershed_propagate(is_px && is_pz,px,y,pz);
  37408. _cimg_watershed_propagate(is_nx && is_pz,nx,y,pz);
  37409. _cimg_watershed_propagate(is_px && is_nz,px,y,nz);
  37410. _cimg_watershed_propagate(is_nx && is_nz,nx,y,nz);
  37411. _cimg_watershed_propagate(is_py && is_pz,x,py,pz);
  37412. _cimg_watershed_propagate(is_ny && is_pz,x,ny,pz);
  37413. _cimg_watershed_propagate(is_py && is_nz,x,py,nz);
  37414. _cimg_watershed_propagate(is_ny && is_nz,x,ny,nz);
  37415. _cimg_watershed_propagate(is_px && is_py && is_pz,px,py,pz);
  37416. _cimg_watershed_propagate(is_nx && is_py && is_pz,nx,py,pz);
  37417. _cimg_watershed_propagate(is_px && is_ny && is_pz,px,ny,pz);
  37418. _cimg_watershed_propagate(is_nx && is_ny && is_pz,nx,ny,pz);
  37419. _cimg_watershed_propagate(is_px && is_py && is_nz,px,py,nz);
  37420. _cimg_watershed_propagate(is_nx && is_py && is_nz,nx,py,nz);
  37421. _cimg_watershed_propagate(is_px && is_ny && is_nz,px,ny,nz);
  37422. _cimg_watershed_propagate(is_nx && is_ny && is_nz,nx,ny,nz);
  37423. }
  37424. }
  37425. (*this)(x,y,z) = nlabel;
  37426. labels(x,y,z) = ++nmin;
  37427. }
  37428. return *this;
  37429. }
  37430. //! Compute watershed transform \newinstance.
  37431. template<typename t>
  37432. CImg<T> get_watershed(const CImg<t>& priority, const bool is_high_connectivity=false) const {
  37433. return (+*this).watershed(priority,is_high_connectivity);
  37434. }
  37435. // [internal] Insert/Remove items in priority queue, for watershed/distance transforms.
  37436. template<typename tq, typename tv>
  37437. bool _priority_queue_insert(CImg<tq>& is_queued, unsigned int& siz, const tv value,
  37438. const unsigned int x, const unsigned int y, const unsigned int z,
  37439. const unsigned int n=1) {
  37440. if (is_queued(x,y,z)) return false;
  37441. is_queued(x,y,z) = (tq)n;
  37442. if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); }
  37443. (*this)(siz - 1,0) = (T)value;
  37444. (*this)(siz - 1,1) = (T)x;
  37445. (*this)(siz - 1,2) = (T)y;
  37446. (*this)(siz - 1,3) = (T)z;
  37447. for (unsigned int pos = siz - 1, par = 0; pos && value>(tv)(*this)(par=(pos + 1)/2 - 1,0); pos = par) {
  37448. cimg::swap((*this)(pos,0),(*this)(par,0));
  37449. cimg::swap((*this)(pos,1),(*this)(par,1));
  37450. cimg::swap((*this)(pos,2),(*this)(par,2));
  37451. cimg::swap((*this)(pos,3),(*this)(par,3));
  37452. }
  37453. return true;
  37454. }
  37455. CImg<T>& _priority_queue_remove(unsigned int& siz) {
  37456. (*this)(0,0) = (*this)(--siz,0);
  37457. (*this)(0,1) = (*this)(siz,1);
  37458. (*this)(0,2) = (*this)(siz,2);
  37459. (*this)(0,3) = (*this)(siz,3);
  37460. const float value = (*this)(0,0);
  37461. unsigned int pos = 0, swap = 0;
  37462. do {
  37463. const unsigned int left = 2*pos + 1, right = left + 1;
  37464. if (right<siz && value<(*this)(right,0)) swap = (*this)(left,0)>(*this)(right,0)?left:right;
  37465. else if (left<siz && value<(*this)(left,0)) swap = left;
  37466. else break;
  37467. cimg::swap((*this)(pos,0),(*this)(swap,0));
  37468. cimg::swap((*this)(pos,1),(*this)(swap,1));
  37469. cimg::swap((*this)(pos,2),(*this)(swap,2));
  37470. cimg::swap((*this)(pos,3),(*this)(swap,3));
  37471. pos = swap;
  37472. } while (true);
  37473. return *this;
  37474. }
  37475. //! Apply recursive Deriche filter.
  37476. /**
  37477. \param sigma Standard deviation of the filter.
  37478. \param order Order of the filter. Can be <tt>{ 0=smooth-filter | 1=1st-derivative | 2=2nd-derivative }</tt>.
  37479. \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  37480. \param boundary_conditions Boundary conditions.
  37481. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  37482. **/
  37483. CImg<T>& deriche(const float sigma, const unsigned int order=0, const char axis='x',
  37484. const unsigned int boundary_conditions=1) {
  37485. #define _cimg_deriche_apply \
  37486. CImg<doubleT> Y(N); \
  37487. double *ptrY = Y._data, yb = 0, yp = 0; \
  37488. T xp = (T)0; \
  37489. if (boundary_conditions) { xp = *ptrX; yb = yp = (double)(coefp*xp); } \
  37490. for (int m = 0; m<N; ++m) { \
  37491. const T xc = *ptrX; ptrX+=off; \
  37492. const double yc = *(ptrY++) = (double)(a0*xc + a1*xp - b1*yp - b2*yb); \
  37493. xp = xc; yb = yp; yp = yc; \
  37494. } \
  37495. T xn = (T)0, xa = (T)0; \
  37496. double yn = 0, ya = 0; \
  37497. if (boundary_conditions) { xn = xa = *(ptrX - off); yn = ya = (double)coefn*xn; } \
  37498. for (int n = N - 1; n>=0; --n) { \
  37499. const T xc = *(ptrX-=off); \
  37500. const double yc = (double)(a2*xn + a3*xa - b1*yn - b2*ya); \
  37501. xa = xn; xn = xc; ya = yn; yn = yc; \
  37502. *ptrX = (T)(*(--ptrY)+yc); \
  37503. }
  37504. if (order>2)
  37505. throw CImgArgumentException(_cimg_instance
  37506. "deriche(): Invalid specified order '%d' "
  37507. "('order' can be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).",
  37508. cimg_instance,
  37509. order);
  37510. const char naxis = cimg::lowercase(axis);
  37511. if (naxis!='x' && naxis!='y' && naxis!='z' && naxis!='c')
  37512. throw CImgArgumentException(_cimg_instance
  37513. "deriche(): Invalid specified axis '%c'.",
  37514. cimg_instance,
  37515. axis);
  37516. const double
  37517. nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:
  37518. naxis=='y'?_height:
  37519. naxis=='z'?_depth:_spectrum)/100,
  37520. nnsigma = nsigma<0.1f?0.1f:nsigma;
  37521. if (is_empty() || (nsigma<0.1f && !order)) return *this;
  37522. if (boundary_conditions>1) {
  37523. const int w = width(), h = height(), d = depth(), s = spectrum(), border = (int)cimg::round(1 + 3*nnsigma);
  37524. switch (naxis) {
  37525. case 'x' :
  37526. return draw_image(get_resize(w + 2*border,h,d,s,0,boundary_conditions,0.5).
  37527. deriche(nnsigma,order,naxis,1).columns(border,w - 1 + border));
  37528. case 'y' :
  37529. return draw_image(get_resize(w,h + 2*border,d,s,0,boundary_conditions,0,0.5).
  37530. deriche(nnsigma,order,naxis,1).rows(border,h - 1 + border));
  37531. case 'z' :
  37532. return draw_image(get_resize(w,h,d + 2*border,s,0,boundary_conditions,0,0,0.5).
  37533. deriche(nnsigma,order,naxis,1).slices(border,d - 1 + border));
  37534. default :
  37535. return draw_image(get_resize(w,h,d,s + 2*border,0,boundary_conditions,0,0,0,0.5).
  37536. deriche(nnsigma,order,naxis,1).channels(border,d - 1 + border));
  37537. }
  37538. }
  37539. const double
  37540. alpha = 1.695f/nnsigma,
  37541. ema = std::exp(-alpha),
  37542. ema2 = std::exp(-2*alpha),
  37543. b1 = -2*ema,
  37544. b2 = ema2;
  37545. double a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0;
  37546. switch (order) {
  37547. case 0 : {
  37548. const double k = (1-ema)*(1-ema)/(1 + 2*alpha*ema-ema2);
  37549. a0 = k;
  37550. a1 = k*(alpha - 1)*ema;
  37551. a2 = k*(alpha + 1)*ema;
  37552. a3 = -k*ema2;
  37553. } break;
  37554. case 1 : {
  37555. const double k = -(1-ema)*(1-ema)*(1-ema)/(2*(ema + 1)*ema);
  37556. a0 = a3 = 0;
  37557. a1 = k*ema;
  37558. a2 = -a1;
  37559. } break;
  37560. default : {
  37561. const double
  37562. ea = std::exp(-alpha),
  37563. k = -(ema2 - 1)/(2*alpha*ema),
  37564. kn = (-2*(-1 + 3*ea - 3*ea*ea + ea*ea*ea)/(3*ea + 1 + 3*ea*ea + ea*ea*ea));
  37565. a0 = kn;
  37566. a1 = -kn*(1 + k*alpha)*ema;
  37567. a2 = kn*(1 - k*alpha)*ema;
  37568. a3 = -kn*ema2;
  37569. } break;
  37570. }
  37571. coefp = (a0 + a1)/(1 + b1 + b2);
  37572. coefn = (a2 + a3)/(1 + b1 + b2);
  37573. switch (naxis) {
  37574. case 'x' : {
  37575. const int N = width();
  37576. const ulongT off = 1U;
  37577. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37578. _height*_depth*_spectrum>=16))
  37579. cimg_forYZC(*this,y,z,c) { T *ptrX = data(0,y,z,c); _cimg_deriche_apply; }
  37580. } break;
  37581. case 'y' : {
  37582. const int N = height();
  37583. const ulongT off = (ulongT)_width;
  37584. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37585. _height*_depth*_spectrum>=16))
  37586. cimg_forXZC(*this,x,z,c) { T *ptrX = data(x,0,z,c); _cimg_deriche_apply; }
  37587. } break;
  37588. case 'z' : {
  37589. const int N = depth();
  37590. const ulongT off = (ulongT)_width*_height;
  37591. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37592. _height*_depth*_spectrum>=16))
  37593. cimg_forXYC(*this,x,y,c) { T *ptrX = data(x,y,0,c); _cimg_deriche_apply; }
  37594. } break;
  37595. default : {
  37596. const int N = spectrum();
  37597. const ulongT off = (ulongT)_width*_height*_depth;
  37598. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37599. _height*_depth*_spectrum>=16))
  37600. cimg_forXYZ(*this,x,y,z) { T *ptrX = data(x,y,z,0); _cimg_deriche_apply; }
  37601. }
  37602. }
  37603. return *this;
  37604. }
  37605. //! Apply recursive Deriche filter \newinstance.
  37606. CImg<Tfloat> get_deriche(const float sigma, const unsigned int order=0, const char axis='x',
  37607. const unsigned int boundary_conditions=1) const {
  37608. return CImg<Tfloat>(*this,false).deriche(sigma,order,axis,boundary_conditions);
  37609. }
  37610. // [internal] Apply a recursive filter (used by CImg<T>::vanvliet()).
  37611. /*
  37612. \param ptr the pointer of the data
  37613. \param filter the coefficient of the filter in the following order [n,n - 1,n - 2,n - 3].
  37614. \param N size of the data
  37615. \param off the offset between two data point
  37616. \param order the order of the filter 0 (smoothing), 1st derivative, 2nd derivative, 3rd derivative
  37617. \param boundary_conditions Boundary conditions.
  37618. Can be <tt>{ 0=dirichlet | 1=neumann }</tt>.
  37619. \note Boundary condition using B. Triggs method (IEEE trans on Sig Proc 2005).
  37620. */
  37621. static void _cimg_recursive_apply(T *data, const double filter[], const int N, const ulongT off,
  37622. const unsigned int order, const bool boundary_conditions) {
  37623. double val[4] = { 0 }; // res[n,n - 1,n - 2,n - 3,..] or res[n,n + 1,n + 2,n + 3,..]
  37624. const double
  37625. sumsq = filter[0], sum = sumsq * sumsq,
  37626. a1 = filter[1], a2 = filter[2], a3 = filter[3],
  37627. scaleM = 1. / ( (1. + a1 - a2 + a3) * (1. - a1 - a2 - a3) * (1. + a2 + (a1 - a3) * a3) );
  37628. double M[9]; // Triggs matrix
  37629. M[0] = scaleM * (-a3 * a1 + 1. - a3 * a3 - a2);
  37630. M[1] = scaleM * (a3 + a1) * (a2 + a3 * a1);
  37631. M[2] = scaleM * a3 * (a1 + a3 * a2);
  37632. M[3] = scaleM * (a1 + a3 * a2);
  37633. M[4] = -scaleM * (a2 - 1.) * (a2 + a3 * a1);
  37634. M[5] = -scaleM * a3 * (a3 * a1 + a3 * a3 + a2 - 1.);
  37635. M[6] = scaleM * (a3 * a1 + a2 + a1 * a1 - a2 * a2);
  37636. M[7] = scaleM * (a1 * a2 + a3 * a2 * a2 - a1 * a3 * a3 - a3 * a3 * a3 - a3 * a2 + a3);
  37637. M[8] = scaleM * a3 * (a1 + a3 * a2);
  37638. switch (order) {
  37639. case 0 : {
  37640. const double iplus = (boundary_conditions?data[(N - 1)*off]:(T)0);
  37641. for (int pass = 0; pass<2; ++pass) {
  37642. if (!pass) {
  37643. for (int k = 1; k<4; ++k) val[k] = (boundary_conditions?*data/sumsq:0);
  37644. } else {
  37645. // Apply Triggs boundary conditions
  37646. const double
  37647. uplus = iplus/(1. - a1 - a2 - a3), vplus = uplus/(1. - a1 - a2 - a3),
  37648. unp = val[1] - uplus, unp1 = val[2] - uplus, unp2 = val[3] - uplus;
  37649. val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2 + vplus) * sum;
  37650. val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2 + vplus) * sum;
  37651. val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2 + vplus) * sum;
  37652. *data = (T)val[0];
  37653. data -= off;
  37654. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37655. }
  37656. for (int n = pass; n<N; ++n) {
  37657. val[0] = (*data);
  37658. if (pass) val[0] *= sum;
  37659. for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
  37660. *data = (T)val[0];
  37661. if (!pass) data += off; else data -= off;
  37662. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37663. }
  37664. if (!pass) data -= off;
  37665. }
  37666. } break;
  37667. case 1 : {
  37668. double x[3]; // [front,center,back]
  37669. for (int pass = 0; pass<2; ++pass) {
  37670. if (!pass) {
  37671. for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
  37672. for (int k = 0; k<4; ++k) val[k] = 0;
  37673. } else {
  37674. // Apply Triggs boundary conditions
  37675. const double
  37676. unp = val[1], unp1 = val[2], unp2 = val[3];
  37677. val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
  37678. val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
  37679. val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
  37680. *data = (T)val[0];
  37681. data -= off;
  37682. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37683. }
  37684. for (int n = pass; n<N - 1; ++n) {
  37685. if (!pass) {
  37686. x[0] = *(data + off);
  37687. val[0] = 0.5f * (x[0] - x[2]);
  37688. } else val[0] = (*data) * sum;
  37689. for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
  37690. *data = (T)val[0];
  37691. if (!pass) {
  37692. data += off;
  37693. for (int k = 2; k>0; --k) x[k] = x[k - 1];
  37694. } else { data-=off;}
  37695. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37696. }
  37697. *data = (T)0;
  37698. }
  37699. } break;
  37700. case 2: {
  37701. double x[3]; // [front,center,back]
  37702. for (int pass = 0; pass<2; ++pass) {
  37703. if (!pass) {
  37704. for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
  37705. for (int k = 0; k<4; ++k) val[k] = 0;
  37706. } else {
  37707. // Apply Triggs boundary conditions
  37708. const double
  37709. unp = val[1], unp1 = val[2], unp2 = val[3];
  37710. val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
  37711. val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
  37712. val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
  37713. *data = (T)val[0];
  37714. data -= off;
  37715. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37716. }
  37717. for (int n = pass; n<N - 1; ++n) {
  37718. if (!pass) { x[0] = *(data + off); val[0] = (x[1] - x[2]); }
  37719. else { x[0] = *(data - off); val[0] = (x[2] - x[1]) * sum; }
  37720. for (int k = 1; k<4; ++k) val[0] += val[k]*filter[k];
  37721. *data = (T)val[0];
  37722. if (!pass) data += off; else data -= off;
  37723. for (int k = 2; k>0; --k) x[k] = x[k - 1];
  37724. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37725. }
  37726. *data = (T)0;
  37727. }
  37728. } break;
  37729. case 3: {
  37730. double x[3]; // [front,center,back]
  37731. for (int pass = 0; pass<2; ++pass) {
  37732. if (!pass) {
  37733. for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
  37734. for (int k = 0; k<4; ++k) val[k] = 0;
  37735. } else {
  37736. // Apply Triggs boundary conditions
  37737. const double
  37738. unp = val[1], unp1 = val[2], unp2 = val[3];
  37739. val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
  37740. val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
  37741. val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
  37742. *data = (T)val[0];
  37743. data -= off;
  37744. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37745. }
  37746. for (int n = pass; n<N - 1; ++n) {
  37747. if (!pass) { x[0] = *(data + off); val[0] = (x[0] - 2*x[1] + x[2]); }
  37748. else { x[0] = *(data - off); val[0] = 0.5f * (x[2] - x[0]) * sum; }
  37749. for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
  37750. *data = (T)val[0];
  37751. if (!pass) data += off; else data -= off;
  37752. for (int k = 2; k>0; --k) x[k] = x[k - 1];
  37753. for (int k = 3; k>0; --k) val[k] = val[k - 1];
  37754. }
  37755. *data = (T)0;
  37756. }
  37757. } break;
  37758. }
  37759. }
  37760. //! Van Vliet recursive Gaussian filter.
  37761. /**
  37762. \param sigma standard deviation of the Gaussian filter
  37763. \param order the order of the filter 0,1,2,3
  37764. \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  37765. \param boundary_conditions Boundary conditions.
  37766. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  37767. \note dirichlet boundary condition has a strange behavior
  37768. I.T. Young, L.J. van Vliet, M. van Ginkel, Recursive Gabor filtering.
  37769. IEEE Trans. Sig. Proc., vol. 50, pp. 2799-2805, 2002.
  37770. (this is an improvement over Young-Van Vliet, Sig. Proc. 44, 1995)
  37771. Boundary conditions (only for order 0) using Triggs matrix, from
  37772. B. Triggs and M. Sdika. Boundary conditions for Young-van Vliet
  37773. recursive filtering. IEEE Trans. Signal Processing,
  37774. vol. 54, pp. 2365-2367, 2006.
  37775. **/
  37776. CImg<T>& vanvliet(const float sigma, const unsigned int order, const char axis='x',
  37777. const unsigned int boundary_conditions=1) {
  37778. if (order>2)
  37779. throw CImgArgumentException(_cimg_instance
  37780. "deriche(): Invalid specified order '%d' "
  37781. "('order' can be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).",
  37782. cimg_instance,
  37783. order);
  37784. const char naxis = cimg::lowercase(axis);
  37785. if (naxis!='x' && naxis!='y' && naxis!='z' && naxis!='c')
  37786. throw CImgArgumentException(_cimg_instance
  37787. "deriche(): Invalid specified axis '%c'.",
  37788. cimg_instance,
  37789. axis);
  37790. const double
  37791. nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:
  37792. naxis=='y'?_height:
  37793. naxis=='z'?_depth:_spectrum)/100,
  37794. nnsigma = nsigma<0.5f?0.5f:nsigma;
  37795. if (is_empty() || (nsigma<0.1f && !order)) return *this;
  37796. if (nsigma<0.5f) return deriche(nsigma,order,axis,boundary_conditions);
  37797. if (!cimg::type<T>::is_float())
  37798. return CImg<Tfloat>(*this,false).vanvliet(sigma,order,axis,boundary_conditions).move_to(*this);
  37799. if (boundary_conditions>1) {
  37800. const int w = width(), h = height(), d = depth(), s = spectrum(), border = (int)cimg::round(1 + 3*nnsigma);
  37801. switch (naxis) {
  37802. case 'x' :
  37803. return draw_image(get_resize(w + 2*border,h,d,s,0,boundary_conditions,0.5).
  37804. vanvliet(nnsigma,order,naxis,1).columns(border,w - 1 + border));
  37805. case 'y' :
  37806. return draw_image(get_resize(w,h + 2*border,d,s,0,boundary_conditions,0,0.5).
  37807. vanvliet(nnsigma,order,naxis,1).rows(border,h - 1 + border));
  37808. case 'z' :
  37809. return draw_image(get_resize(w,h,d + 2*border,s,0,boundary_conditions,0,0,0.5).
  37810. vanvliet(nnsigma,order,naxis,1).slices(border,d - 1 + border));
  37811. default :
  37812. return draw_image(get_resize(w,h,d,s + 2*border,0,boundary_conditions,0,0,0,0.5).
  37813. vanvliet(nnsigma,order,naxis,1).channels(border,d - 1 + border));
  37814. }
  37815. }
  37816. const double
  37817. m0 = 1.16680, m1 = 1.10783, m2 = 1.40586,
  37818. m1sq = m1 * m1, m2sq = m2 * m2,
  37819. q = (nnsigma<3.556?-0.2568 + 0.5784*nnsigma + 0.0561*nnsigma*nnsigma:2.5091 + 0.9804*(nnsigma - 3.556)),
  37820. qsq = q * q,
  37821. scale = (m0 + q) * (m1sq + m2sq + 2 * m1 * q + qsq),
  37822. b1 = -q * (2 * m0 * m1 + m1sq + m2sq + (2 * m0 + 4 * m1) * q + 3 * qsq) / scale,
  37823. b2 = qsq * (m0 + 2 * m1 + 3 * q) / scale,
  37824. b3 = -qsq * q / scale,
  37825. B = ( m0 * (m1sq + m2sq) ) / scale;
  37826. double filter[4];
  37827. filter[0] = B; filter[1] = -b1; filter[2] = -b2; filter[3] = -b3;
  37828. switch (naxis) {
  37829. case 'x' : {
  37830. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37831. _height*_depth*_spectrum>=16))
  37832. cimg_forYZC(*this,y,z,c)
  37833. _cimg_recursive_apply(data(0,y,z,c),filter,_width,1U,order,boundary_conditions);
  37834. } break;
  37835. case 'y' : {
  37836. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37837. _height*_depth*_spectrum>=16))
  37838. cimg_forXZC(*this,x,z,c)
  37839. _cimg_recursive_apply(data(x,0,z,c),filter,_height,(ulongT)_width,order,boundary_conditions);
  37840. } break;
  37841. case 'z' : {
  37842. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37843. _height*_depth*_spectrum>=16))
  37844. cimg_forXYC(*this,x,y,c)
  37845. _cimg_recursive_apply(data(x,y,0,c),filter,_depth,(ulongT)_width*_height,
  37846. order,boundary_conditions);
  37847. } break;
  37848. default : {
  37849. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  37850. _height*_depth*_spectrum>=16))
  37851. cimg_forXYZ(*this,x,y,z)
  37852. _cimg_recursive_apply(data(x,y,z,0),filter,_spectrum,(ulongT)_width*_height*_depth,
  37853. order,boundary_conditions);
  37854. }
  37855. }
  37856. return *this;
  37857. }
  37858. //! Blur image using Van Vliet recursive Gaussian filter. \newinstance.
  37859. CImg<Tfloat> get_vanvliet(const float sigma, const unsigned int order, const char axis='x',
  37860. const unsigned int boundary_conditions=1) const {
  37861. return CImg<Tfloat>(*this,false).vanvliet(sigma,order,axis,boundary_conditions);
  37862. }
  37863. //! Blur image.
  37864. /**
  37865. \param sigma_x Standard deviation of the blur, along the X-axis.
  37866. \param sigma_y Standard deviation of the blur, along the Y-axis.
  37867. \param sigma_z Standard deviation of the blur, along the Z-axis.
  37868. \param boundary_conditions Boundary conditions.
  37869. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  37870. \param is_gaussian Tells if the blur uses a gaussian (\c true) or quasi-gaussian (\c false) kernel.
  37871. \note
  37872. - The blur is computed as a 0-order Vanvliet (gaussian) or Deriche filter (quasi-gaussian).
  37873. - This is a recursive algorithm, not depending on the values of the standard deviations.
  37874. \see deriche(), vanvliet().
  37875. **/
  37876. CImg<T>& blur(const float sigma_x, const float sigma_y, const float sigma_z,
  37877. const unsigned int boundary_conditions=1, const bool is_gaussian=true) {
  37878. if (is_empty()) return *this;
  37879. if (is_gaussian) {
  37880. if (_width>1) vanvliet(sigma_x,0,'x',boundary_conditions);
  37881. if (_height>1) vanvliet(sigma_y,0,'y',boundary_conditions);
  37882. if (_depth>1) vanvliet(sigma_z,0,'z',boundary_conditions);
  37883. } else {
  37884. if (_width>1) deriche(sigma_x,0,'x',boundary_conditions);
  37885. if (_height>1) deriche(sigma_y,0,'y',boundary_conditions);
  37886. if (_depth>1) deriche(sigma_z,0,'z',boundary_conditions);
  37887. }
  37888. return *this;
  37889. }
  37890. //! Blur image \newinstance.
  37891. CImg<Tfloat> get_blur(const float sigma_x, const float sigma_y, const float sigma_z,
  37892. const unsigned int boundary_conditions=1, const bool is_gaussian=true) const {
  37893. return CImg<Tfloat>(*this,false).blur(sigma_x,sigma_y,sigma_z,boundary_conditions,is_gaussian);
  37894. }
  37895. //! Blur image isotropically.
  37896. /**
  37897. \param sigma Standard deviation of the blur.
  37898. \param boundary_conditions Boundary conditions.
  37899. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.a
  37900. \param is_gaussian Use a gaussian kernel (VanVliet) is set, a quasi-gaussian (Deriche) otherwise.
  37901. \see deriche(), vanvliet().
  37902. **/
  37903. CImg<T>& blur(const float sigma, const unsigned int boundary_conditions=1, const bool is_gaussian=true) {
  37904. const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100;
  37905. return blur(nsigma,nsigma,nsigma,boundary_conditions,is_gaussian);
  37906. }
  37907. //! Blur image isotropically \newinstance.
  37908. CImg<Tfloat> get_blur(const float sigma, const unsigned int boundary_conditions=1,
  37909. const bool is_gaussian=true) const {
  37910. return CImg<Tfloat>(*this,false).blur(sigma,boundary_conditions,is_gaussian);
  37911. }
  37912. //! Blur image anisotropically, directed by a field of diffusion tensors.
  37913. /**
  37914. \param G Field of square roots of diffusion tensors/vectors used to drive the smoothing.
  37915. \param amplitude Amplitude of the smoothing.
  37916. \param dl Spatial discretization.
  37917. \param da Angular discretization.
  37918. \param gauss_prec Precision of the diffusion process.
  37919. \param interpolation_type Interpolation scheme.
  37920. Can be <tt>{ 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }</tt>.
  37921. \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
  37922. **/
  37923. template<typename t>
  37924. CImg<T>& blur_anisotropic(const CImg<t>& G,
  37925. const float amplitude=60, const float dl=0.8f, const float da=30,
  37926. const float gauss_prec=2, const unsigned int interpolation_type=0,
  37927. const bool is_fast_approx=1) {
  37928. // Check arguments and init variables
  37929. if (!is_sameXYZ(G) || (G._spectrum!=3 && G._spectrum!=6))
  37930. throw CImgArgumentException(_cimg_instance
  37931. "blur_anisotropic(): Invalid specified diffusion tensor field (%u,%u,%u,%u,%p).",
  37932. cimg_instance,
  37933. G._width,G._height,G._depth,G._spectrum,G._data);
  37934. if (is_empty() || dl<0) return *this;
  37935. const float namplitude = amplitude>=0?amplitude:-amplitude*cimg::max(_width,_height,_depth)/100;
  37936. unsigned int iamplitude = cimg::round(namplitude);
  37937. const bool is_3d = (G._spectrum==6);
  37938. T val_min, val_max = max_min(val_min);
  37939. _cimg_abort_init_openmp;
  37940. cimg_abort_init;
  37941. if (da<=0) { // Iterated oriented Laplacians
  37942. CImg<Tfloat> velocity(_width,_height,_depth,_spectrum);
  37943. for (unsigned int iteration = 0; iteration<iamplitude; ++iteration) {
  37944. Tfloat *ptrd = velocity._data, veloc_max = 0;
  37945. if (is_3d) // 3D version
  37946. cimg_forC(*this,c) {
  37947. cimg_abort_test;
  37948. CImg_3x3x3(I,Tfloat);
  37949. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
  37950. const Tfloat
  37951. ixx = Incc + Ipcc - 2*Iccc,
  37952. ixy = (Innc + Ippc - Inpc - Ipnc)/4,
  37953. ixz = (Incn + Ipcp - Incp - Ipcn)/4,
  37954. iyy = Icnc + Icpc - 2*Iccc,
  37955. iyz = (Icnn + Icpp - Icnp - Icpn)/4,
  37956. izz = Iccn + Iccp - 2*Iccc,
  37957. veloc = (Tfloat)(G(x,y,z,0)*ixx + 2*G(x,y,z,1)*ixy + 2*G(x,y,z,2)*ixz +
  37958. G(x,y,z,3)*iyy + 2*G(x,y,z,4)*iyz + G(x,y,z,5)*izz);
  37959. *(ptrd++) = veloc;
  37960. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  37961. }
  37962. }
  37963. else // 2D version
  37964. cimg_forZC(*this,z,c) {
  37965. cimg_abort_test;
  37966. CImg_3x3(I,Tfloat);
  37967. cimg_for3x3(*this,x,y,z,c,I,Tfloat) {
  37968. const Tfloat
  37969. ixx = Inc + Ipc - 2*Icc,
  37970. ixy = (Inn + Ipp - Inp - Ipn)/4,
  37971. iyy = Icn + Icp - 2*Icc,
  37972. veloc = (Tfloat)(G(x,y,0,0)*ixx + 2*G(x,y,0,1)*ixy + G(x,y,0,2)*iyy);
  37973. *(ptrd++) = veloc;
  37974. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  37975. }
  37976. }
  37977. if (veloc_max>0) *this+=(velocity*=dl/veloc_max);
  37978. }
  37979. } else { // LIC-based smoothing
  37980. const ulongT whd = (ulongT)_width*_height*_depth;
  37981. const float sqrt2amplitude = (float)std::sqrt(2*namplitude);
  37982. const int dx1 = width() - 1, dy1 = height() - 1, dz1 = depth() - 1;
  37983. CImg<Tfloat> res(_width,_height,_depth,_spectrum,0), W(_width,_height,_depth,is_3d?4:3), val(_spectrum,1,1,1,0);
  37984. int N = 0;
  37985. if (is_3d) { // 3D version
  37986. for (float phi = cimg::mod(180.f,da)/2.f; phi<=180; phi+=da) {
  37987. const float phir = (float)(phi*cimg::PI/180), datmp = (float)(da/std::cos(phir)),
  37988. da2 = datmp<1?360.f:datmp;
  37989. for (float theta = 0; theta<360; (theta+=da2),++N) {
  37990. const float
  37991. thetar = (float)(theta*cimg::PI/180),
  37992. vx = (float)(std::cos(thetar)*std::cos(phir)),
  37993. vy = (float)(std::sin(thetar)*std::cos(phir)),
  37994. vz = (float)std::sin(phir);
  37995. const t
  37996. *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2),
  37997. *pd = G.data(0,0,0,3), *pe = G.data(0,0,0,4), *pf = G.data(0,0,0,5);
  37998. Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2), *pd3 = W.data(0,0,0,3);
  37999. cimg_forXYZ(G,xg,yg,zg) {
  38000. const t a = *(pa++), b = *(pb++), c = *(pc++), d = *(pd++), e = *(pe++), f = *(pf++);
  38001. const float
  38002. u = (float)(a*vx + b*vy + c*vz),
  38003. v = (float)(b*vx + d*vy + e*vz),
  38004. w = (float)(c*vx + e*vy + f*vz),
  38005. n = 1e-5f + cimg::hypot(u,v,w),
  38006. dln = dl/n;
  38007. *(pd0++) = (Tfloat)(u*dln);
  38008. *(pd1++) = (Tfloat)(v*dln);
  38009. *(pd2++) = (Tfloat)(w*dln);
  38010. *(pd3++) = (Tfloat)n;
  38011. }
  38012. cimg_abort_test;
  38013. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  38014. cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 && _height*_depth>=2)
  38015. firstprivate(val))
  38016. cimg_forYZ(*this,y,z) _cimg_abort_try_openmp2 {
  38017. cimg_abort_test2;
  38018. cimg_forX(*this,x) {
  38019. val.fill(0);
  38020. const float
  38021. n = (float)W(x,y,z,3),
  38022. fsigma = (float)(n*sqrt2amplitude),
  38023. fsigma2 = 2*fsigma*fsigma,
  38024. length = gauss_prec*fsigma;
  38025. float
  38026. S = 0,
  38027. X = (float)x,
  38028. Y = (float)y,
  38029. Z = (float)z;
  38030. switch (interpolation_type) {
  38031. case 0 : { // Nearest neighbor
  38032. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
  38033. const int
  38034. cx = (int)(X + 0.5f),
  38035. cy = (int)(Y + 0.5f),
  38036. cz = (int)(Z + 0.5f);
  38037. const float
  38038. u = (float)W(cx,cy,cz,0),
  38039. v = (float)W(cx,cy,cz,1),
  38040. w = (float)W(cx,cy,cz,2);
  38041. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,cz,c); ++S; }
  38042. else {
  38043. const float coef = (float)std::exp(-l*l/fsigma2);
  38044. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,cz,c));
  38045. S+=coef;
  38046. }
  38047. X+=u; Y+=v; Z+=w;
  38048. }
  38049. } break;
  38050. case 1 : { // Linear interpolation
  38051. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
  38052. const float
  38053. u = (float)(W._linear_atXYZ(X,Y,Z,0)),
  38054. v = (float)(W._linear_atXYZ(X,Y,Z,1)),
  38055. w = (float)(W._linear_atXYZ(X,Y,Z,2));
  38056. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; }
  38057. else {
  38058. const float coef = (float)std::exp(-l*l/fsigma2);
  38059. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c));
  38060. S+=coef;
  38061. }
  38062. X+=u; Y+=v; Z+=w;
  38063. }
  38064. } break;
  38065. default : { // 2nd order Runge Kutta
  38066. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
  38067. const float
  38068. u0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,0)),
  38069. v0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,1)),
  38070. w0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,2)),
  38071. u = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,0)),
  38072. v = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,1)),
  38073. w = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,2));
  38074. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; }
  38075. else {
  38076. const float coef = (float)std::exp(-l*l/fsigma2);
  38077. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c));
  38078. S+=coef;
  38079. }
  38080. X+=u; Y+=v; Z+=w;
  38081. }
  38082. } break;
  38083. }
  38084. Tfloat *ptrd = res.data(x,y,z);
  38085. if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; }
  38086. else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,z,c)); ptrd+=whd; }
  38087. }
  38088. } _cimg_abort_catch_openmp2
  38089. }
  38090. }
  38091. } else { // 2D LIC algorithm
  38092. for (float theta = cimg::mod(360.f,da)/2.f; theta<360; (theta+=da),++N) {
  38093. const float thetar = (float)(theta*cimg::PI/180),
  38094. vx = (float)(std::cos(thetar)), vy = (float)(std::sin(thetar));
  38095. const t *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2);
  38096. Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2);
  38097. cimg_forXY(G,xg,yg) {
  38098. const t a = *(pa++), b = *(pb++), c = *(pc++);
  38099. const float
  38100. u = (float)(a*vx + b*vy),
  38101. v = (float)(b*vx + c*vy),
  38102. n = std::max(1e-5f,cimg::hypot(u,v)),
  38103. dln = dl/n;
  38104. *(pd0++) = (Tfloat)(u*dln);
  38105. *(pd1++) = (Tfloat)(v*dln);
  38106. *(pd2++) = (Tfloat)n;
  38107. }
  38108. cimg_abort_test;
  38109. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 && _height>=2)
  38110. firstprivate(val))
  38111. cimg_forY(*this,y) _cimg_abort_try_openmp2 {
  38112. cimg_abort_test2;
  38113. cimg_forX(*this,x) {
  38114. val.fill(0);
  38115. const float
  38116. n = (float)W(x,y,0,2),
  38117. fsigma = (float)(n*sqrt2amplitude),
  38118. fsigma2 = 2*fsigma*fsigma,
  38119. length = gauss_prec*fsigma;
  38120. float
  38121. S = 0,
  38122. X = (float)x,
  38123. Y = (float)y;
  38124. switch (interpolation_type) {
  38125. case 0 : { // Nearest-neighbor
  38126. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
  38127. const int
  38128. cx = (int)(X + 0.5f),
  38129. cy = (int)(Y + 0.5f);
  38130. const float
  38131. u = (float)W(cx,cy,0,0),
  38132. v = (float)W(cx,cy,0,1);
  38133. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,0,c); ++S; }
  38134. else {
  38135. const float coef = (float)std::exp(-l*l/fsigma2);
  38136. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,0,c));
  38137. S+=coef;
  38138. }
  38139. X+=u; Y+=v;
  38140. }
  38141. } break;
  38142. case 1 : { // Linear interpolation
  38143. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
  38144. const float
  38145. u = (float)(W._linear_atXY(X,Y,0,0)),
  38146. v = (float)(W._linear_atXY(X,Y,0,1));
  38147. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; }
  38148. else {
  38149. const float coef = (float)std::exp(-l*l/fsigma2);
  38150. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c));
  38151. S+=coef;
  38152. }
  38153. X+=u; Y+=v;
  38154. }
  38155. } break;
  38156. default : { // 2nd-order Runge-kutta interpolation
  38157. for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
  38158. const float
  38159. u0 = (float)(0.5f*W._linear_atXY(X,Y,0,0)),
  38160. v0 = (float)(0.5f*W._linear_atXY(X,Y,0,1)),
  38161. u = (float)(W._linear_atXY(X + u0,Y + v0,0,0)),
  38162. v = (float)(W._linear_atXY(X + u0,Y + v0,0,1));
  38163. if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; }
  38164. else {
  38165. const float coef = (float)std::exp(-l*l/fsigma2);
  38166. cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c));
  38167. S+=coef;
  38168. }
  38169. X+=u; Y+=v;
  38170. }
  38171. }
  38172. }
  38173. Tfloat *ptrd = res.data(x,y);
  38174. if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; }
  38175. else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,0,c)); ptrd+=whd; }
  38176. }
  38177. } _cimg_abort_catch_openmp2
  38178. }
  38179. }
  38180. const Tfloat *ptrs = res._data;
  38181. cimg_for(*this,ptrd,T) {
  38182. const Tfloat _val = *(ptrs++)/N;
  38183. *ptrd = _val<val_min?val_min:(_val>val_max?val_max:(T)_val);
  38184. }
  38185. }
  38186. cimg_abort_test;
  38187. return *this;
  38188. }
  38189. //! Blur image anisotropically, directed by a field of diffusion tensors \newinstance.
  38190. template<typename t>
  38191. CImg<Tfloat> get_blur_anisotropic(const CImg<t>& G,
  38192. const float amplitude=60, const float dl=0.8f, const float da=30,
  38193. const float gauss_prec=2, const unsigned int interpolation_type=0,
  38194. const bool is_fast_approx=true) const {
  38195. return CImg<Tfloat>(*this,false).blur_anisotropic(G,amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx);
  38196. }
  38197. //! Blur image anisotropically, in an edge-preserving way.
  38198. /**
  38199. \param amplitude Amplitude of the smoothing.
  38200. \param sharpness Sharpness.
  38201. \param anisotropy Anisotropy.
  38202. \param alpha Standard deviation of the gradient blur.
  38203. \param sigma Standard deviation of the structure tensor blur.
  38204. \param dl Spatial discretization.
  38205. \param da Angular discretization.
  38206. \param gauss_prec Precision of the diffusion process.
  38207. \param interpolation_type Interpolation scheme.
  38208. Can be <tt>{ 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }</tt>.
  38209. \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
  38210. **/
  38211. CImg<T>& blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f,
  38212. const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, const float da=30,
  38213. const float gauss_prec=2, const unsigned int interpolation_type=0,
  38214. const bool is_fast_approx=true) {
  38215. const float nalpha = alpha>=0?alpha:-alpha*cimg::max(_width,_height,_depth)/100;
  38216. const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100;
  38217. return blur_anisotropic(get_diffusion_tensors(sharpness,anisotropy,nalpha,nsigma,interpolation_type!=3),
  38218. amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx);
  38219. }
  38220. //! Blur image anisotropically, in an edge-preserving way \newinstance.
  38221. CImg<Tfloat> get_blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f,
  38222. const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f,
  38223. const float da=30, const float gauss_prec=2,
  38224. const unsigned int interpolation_type=0,
  38225. const bool is_fast_approx=true) const {
  38226. return CImg<Tfloat>(*this,false).blur_anisotropic(amplitude,sharpness,anisotropy,alpha,sigma,dl,da,gauss_prec,
  38227. interpolation_type,is_fast_approx);
  38228. }
  38229. //! Blur image, with the joint bilateral filter.
  38230. /**
  38231. \param guide Image used to model the smoothing weights.
  38232. \param sigma_x Amount of blur along the X-axis.
  38233. \param sigma_y Amount of blur along the Y-axis.
  38234. \param sigma_z Amount of blur along the Z-axis.
  38235. \param sigma_r Amount of blur along the value axis.
  38236. \param sampling_x Amount of downsampling along the X-axis used for the approximation.
  38237. Defaults (0) to sigma_x.
  38238. \param sampling_y Amount of downsampling along the Y-axis used for the approximation.
  38239. Defaults (0) to sigma_y.
  38240. \param sampling_z Amount of downsampling along the Z-axis used for the approximation.
  38241. Defaults (0) to sigma_z.
  38242. \param sampling_r Amount of downsampling along the value axis used for the approximation.
  38243. Defaults (0) to sigma_r.
  38244. \note This algorithm uses the optimisation technique proposed by S. Paris and F. Durand, in ECCV'2006
  38245. (extended for 3D volumetric images).
  38246. It is based on the reference implementation http://people.csail.mit.edu/jiawen/software/bilateralFilter.m
  38247. **/
  38248. template<typename t>
  38249. CImg<T>& blur_bilateral(const CImg<t>& guide,
  38250. const float sigma_x, const float sigma_y,
  38251. const float sigma_z, const float sigma_r,
  38252. const float sampling_x, const float sampling_y,
  38253. const float sampling_z, const float sampling_r) {
  38254. if (!is_sameXYZ(guide))
  38255. throw CImgArgumentException(_cimg_instance
  38256. "blur_bilateral(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
  38257. cimg_instance,
  38258. guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
  38259. if (is_empty() || (!sigma_x && !sigma_y && !sigma_z)) return *this;
  38260. T edge_min, edge_max = guide.max_min(edge_min);
  38261. if (edge_min==edge_max) return blur(sigma_x,sigma_y,sigma_z);
  38262. const float
  38263. edge_delta = (float)(edge_max - edge_min),
  38264. _sigma_x = sigma_x>=0?sigma_x:-sigma_x*_width/100,
  38265. _sigma_y = sigma_y>=0?sigma_y:-sigma_y*_height/100,
  38266. _sigma_z = sigma_z>=0?sigma_z:-sigma_z*_depth/100,
  38267. _sigma_r = sigma_r>=0?sigma_r:-sigma_r*edge_delta/100,
  38268. _sampling_x = sampling_x?sampling_x:std::max(_sigma_x,1.f),
  38269. _sampling_y = sampling_y?sampling_y:std::max(_sigma_y,1.f),
  38270. _sampling_z = sampling_z?sampling_z:std::max(_sigma_z,1.f),
  38271. _sampling_r = sampling_r?sampling_r:std::max(_sigma_r,edge_delta/256),
  38272. derived_sigma_x = _sigma_x / _sampling_x,
  38273. derived_sigma_y = _sigma_y / _sampling_y,
  38274. derived_sigma_z = _sigma_z / _sampling_z,
  38275. derived_sigma_r = _sigma_r / _sampling_r;
  38276. const int
  38277. padding_x = (int)(2*derived_sigma_x) + 1,
  38278. padding_y = (int)(2*derived_sigma_y) + 1,
  38279. padding_z = (int)(2*derived_sigma_z) + 1,
  38280. padding_r = (int)(2*derived_sigma_r) + 1;
  38281. const unsigned int
  38282. bx = (unsigned int)((_width - 1)/_sampling_x + 1 + 2*padding_x),
  38283. by = (unsigned int)((_height - 1)/_sampling_y + 1 + 2*padding_y),
  38284. bz = (unsigned int)((_depth - 1)/_sampling_z + 1 + 2*padding_z),
  38285. br = (unsigned int)(edge_delta/_sampling_r + 1 + 2*padding_r);
  38286. if (bx>0 || by>0 || bz>0 || br>0) {
  38287. const bool is_3d = (_depth>1);
  38288. if (is_3d) { // 3D version of the algorithm
  38289. CImg<floatT> bgrid(bx,by,bz,br), bgridw(bx,by,bz,br);
  38290. cimg_forC(*this,c) {
  38291. const CImg<t> _guide = guide.get_shared_channel(c%guide._spectrum);
  38292. bgrid.fill(0); bgridw.fill(0);
  38293. cimg_forXYZ(*this,x,y,z) {
  38294. const T val = (*this)(x,y,z,c);
  38295. const float edge = (float)_guide(x,y,z);
  38296. const int
  38297. X = (int)cimg::round(x/_sampling_x) + padding_x,
  38298. Y = (int)cimg::round(y/_sampling_y) + padding_y,
  38299. Z = (int)cimg::round(z/_sampling_z) + padding_z,
  38300. R = (int)cimg::round((edge - edge_min)/_sampling_r) + padding_r;
  38301. bgrid(X,Y,Z,R)+=(float)val;
  38302. bgridw(X,Y,Z,R)+=1;
  38303. }
  38304. bgrid.blur(derived_sigma_x,derived_sigma_y,derived_sigma_z,true).deriche(derived_sigma_r,0,'c',false);
  38305. bgridw.blur(derived_sigma_x,derived_sigma_y,derived_sigma_z,true).deriche(derived_sigma_r,0,'c',false);
  38306. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),4096))
  38307. cimg_forXYZ(*this,x,y,z) {
  38308. const float edge = (float)_guide(x,y,z);
  38309. const float
  38310. X = x/_sampling_x + padding_x,
  38311. Y = y/_sampling_y + padding_y,
  38312. Z = z/_sampling_z + padding_z,
  38313. R = (edge - edge_min)/_sampling_r + padding_r;
  38314. const float bval0 = bgrid._linear_atXYZC(X,Y,Z,R), bval1 = bgridw._linear_atXYZC(X,Y,Z,R);
  38315. (*this)(x,y,z,c) = (T)(bval0/bval1);
  38316. }
  38317. }
  38318. } else { // 2D version of the algorithm
  38319. CImg<floatT> bgrid(bx,by,br,2);
  38320. cimg_forC(*this,c) {
  38321. const CImg<t> _guide = guide.get_shared_channel(c%guide._spectrum);
  38322. bgrid.fill(0);
  38323. cimg_forXY(*this,x,y) {
  38324. const T val = (*this)(x,y,c);
  38325. const float edge = (float)_guide(x,y);
  38326. const int
  38327. X = (int)cimg::round(x/_sampling_x) + padding_x,
  38328. Y = (int)cimg::round(y/_sampling_y) + padding_y,
  38329. R = (int)cimg::round((edge - edge_min)/_sampling_r) + padding_r;
  38330. bgrid(X,Y,R,0)+=(float)val;
  38331. bgrid(X,Y,R,1)+=1;
  38332. }
  38333. bgrid.blur(derived_sigma_x,derived_sigma_y,0,true).blur(0,0,derived_sigma_r,false);
  38334. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(size(),4096))
  38335. cimg_forXY(*this,x,y) {
  38336. const float edge = (float)_guide(x,y);
  38337. const float
  38338. X = x/_sampling_x + padding_x,
  38339. Y = y/_sampling_y + padding_y,
  38340. R = (edge - edge_min)/_sampling_r + padding_r;
  38341. const float bval0 = bgrid._linear_atXYZ(X,Y,R,0), bval1 = bgrid._linear_atXYZ(X,Y,R,1);
  38342. (*this)(x,y,c) = (T)(bval0/bval1);
  38343. }
  38344. }
  38345. }
  38346. }
  38347. return *this;
  38348. }
  38349. //! Blur image, with the joint bilateral filter \newinstance.
  38350. template<typename t>
  38351. CImg<Tfloat> get_blur_bilateral(const CImg<t>& guide,
  38352. const float sigma_x, const float sigma_y,
  38353. const float sigma_z, const float sigma_r,
  38354. const float sampling_x, const float sampling_y,
  38355. const float sampling_z, const float sampling_r) const {
  38356. return CImg<Tfloat>(*this,false).blur_bilateral(guide,sigma_x,sigma_y,sigma_z,sigma_r,
  38357. sampling_x,sampling_y,sampling_z,sampling_r);
  38358. }
  38359. //! Blur image using the joint bilateral filter.
  38360. /**
  38361. \param guide Image used to model the smoothing weights.
  38362. \param sigma_s Amount of blur along the XYZ-axes.
  38363. \param sigma_r Amount of blur along the value axis.
  38364. \param sampling_s Amount of downsampling along the XYZ-axes used for the approximation. Defaults to sigma_s.
  38365. \param sampling_r Amount of downsampling along the value axis used for the approximation. Defaults to sigma_r.
  38366. **/
  38367. template<typename t>
  38368. CImg<T>& blur_bilateral(const CImg<t>& guide,
  38369. const float sigma_s, const float sigma_r,
  38370. const float sampling_s=0, const float sampling_r=0) {
  38371. const float _sigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100;
  38372. return blur_bilateral(guide,_sigma_s,_sigma_s,_sigma_s,sigma_r,sampling_s,sampling_s,sampling_s,sampling_r);
  38373. }
  38374. //! Blur image using the bilateral filter \newinstance.
  38375. template<typename t>
  38376. CImg<Tfloat> get_blur_bilateral(const CImg<t>& guide,
  38377. const float sigma_s, const float sigma_r,
  38378. const float sampling_s=0, const float sampling_r=0) const {
  38379. return CImg<Tfloat>(*this,false).blur_bilateral(guide,sigma_s,sigma_r,sampling_s,sampling_r);
  38380. }
  38381. // [internal] Apply a box filter (used by CImg<T>::boxfilter() and CImg<T>::blur_box()).
  38382. /*
  38383. \param ptr the pointer of the data
  38384. \param N size of the data
  38385. \param boxsize Size of the box filter (can be subpixel).
  38386. \param off the offset between two data point
  38387. \param order the order of the filter 0 (smoothing), 1st derivative and 2nd derivative.
  38388. \param boundary_conditions Boundary conditions.
  38389. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  38390. */
  38391. static void _cimg_blur_box_apply(T *ptr, const float boxsize, const int N, const ulongT off,
  38392. const int order, const unsigned int boundary_conditions,
  38393. const unsigned int nb_iter) {
  38394. const int nboundary_conditions = boundary_conditions>1 && boxsize<=3?1:boundary_conditions;
  38395. // Smooth.
  38396. if (boxsize>1 && nb_iter) {
  38397. const int w2 = (int)(boxsize - 1)/2;
  38398. const unsigned int winsize = 2*w2 + 1U;
  38399. const double frac = (boxsize - winsize)/2.;
  38400. CImg<T> win(winsize);
  38401. for (unsigned int iter = 0; iter<nb_iter; ++iter) {
  38402. Tdouble sum = 0; // window sum
  38403. for (int x = -w2; x<=w2; ++x) {
  38404. win[x + w2] = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x);
  38405. sum+=win[x + w2];
  38406. }
  38407. int ifirst = 0, ilast = 2*w2;
  38408. T
  38409. prev = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-w2 - 1),
  38410. next = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,w2 + 1);
  38411. for (int x = 0; x < N - 1; ++x) {
  38412. const double sum2 = sum + frac * (prev + next);
  38413. ptr[x*off] = (T)(sum2/boxsize);
  38414. prev = win[ifirst];
  38415. sum-=prev;
  38416. ifirst = (int)((ifirst + 1)%winsize);
  38417. ilast = (int)((ilast + 1)%winsize);
  38418. win[ilast] = next;
  38419. sum+=next;
  38420. next = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + w2 + 2);
  38421. }
  38422. const double sum2 = sum + frac * (prev + next);
  38423. ptr[(N - 1)*off] = (T)(sum2/boxsize);
  38424. }
  38425. }
  38426. // Derive.
  38427. switch (order) {
  38428. case 0 :
  38429. break;
  38430. case 1 : {
  38431. Tfloat
  38432. p = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-1),
  38433. c = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,0),
  38434. n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,1);
  38435. for (int x = 0; x<N - 1; ++x) {
  38436. ptr[x*off] = (T)((n-p)/2.);
  38437. p = c;
  38438. c = n;
  38439. n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + 2);
  38440. }
  38441. ptr[(N - 1)*off] = (T)((n-p)/2.);
  38442. } break;
  38443. case 2: {
  38444. Tfloat
  38445. p = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-1),
  38446. c = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,0),
  38447. n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,1);
  38448. for (int x = 0; x<N - 1; ++x) {
  38449. ptr[x*off] = (T)(n - 2*c + p);
  38450. p = c;
  38451. c = n;
  38452. n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + 2);
  38453. }
  38454. ptr[(N - 1)*off] = (T)(n - 2*c + p);
  38455. } break;
  38456. }
  38457. }
  38458. static T __cimg_blur_box_apply(T *ptr, const int N, const ulongT off,
  38459. const unsigned int boundary_conditions, const int x) {
  38460. switch (boundary_conditions) {
  38461. case 0 : // Dirichlet
  38462. return x<0 || x>=N?(T)0:ptr[x*off];
  38463. case 1 : { // Neumann
  38464. const int nx = x<0?0:x>=N?N - 1:x;
  38465. return ptr[nx*off];
  38466. }
  38467. case 2 : { // Periodic
  38468. const int nx = cimg::mod(x,N);
  38469. return ptr[nx*off];
  38470. }
  38471. default : { // Mirror
  38472. const int
  38473. N2 = 2*N,
  38474. tx = cimg::mod(x,N2),
  38475. nx = tx<N?tx:N2 - tx - 1;
  38476. return ptr[nx*off];
  38477. }
  38478. }
  38479. return (T)0;
  38480. }
  38481. // Apply box filter of order 0,1,2.
  38482. /**
  38483. \param boxsize Size of the box window (can be subpixel)
  38484. \param order the order of the filter 0,1 or 2.
  38485. \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  38486. \param boundary_conditions Boundary conditions.
  38487. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
  38488. \param nb_iter Number of filter iterations.
  38489. **/
  38490. CImg<T>& boxfilter(const float boxsize, const int order, const char axis='x',
  38491. const unsigned int boundary_conditions=1,
  38492. const unsigned int nb_iter=1) {
  38493. const char naxis = cimg::lowercase(axis);
  38494. const float nboxsize = boxsize>=0?boxsize:-boxsize*
  38495. (naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100;
  38496. if (is_empty() || !nboxsize || (nboxsize<=1 && !order)) return *this;
  38497. switch (naxis) {
  38498. case 'x' : {
  38499. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  38500. _height*_depth*_spectrum>=16))
  38501. cimg_forYZC(*this,y,z,c)
  38502. _cimg_blur_box_apply(data(0,y,z,c),nboxsize,_width,1U,order,boundary_conditions,nb_iter);
  38503. } break;
  38504. case 'y' : {
  38505. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  38506. _height*_depth*_spectrum>=16))
  38507. cimg_forXZC(*this,x,z,c)
  38508. _cimg_blur_box_apply(data(x,0,z,c),nboxsize,_height,(ulongT)_width,order,boundary_conditions,nb_iter);
  38509. } break;
  38510. case 'z' : {
  38511. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  38512. _height*_depth*_spectrum>=16))
  38513. cimg_forXYC(*this,x,y,c)
  38514. _cimg_blur_box_apply(data(x,y,0,c),nboxsize,_depth,(ulongT)_width*_height,order,boundary_conditions,nb_iter);
  38515. } break;
  38516. default : {
  38517. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  38518. _height*_depth*_spectrum>=16))
  38519. cimg_forXYZ(*this,x,y,z)
  38520. _cimg_blur_box_apply(data(x,y,z,0),nboxsize,_spectrum,(ulongT)_width*_height*_depth,
  38521. order,boundary_conditions,nb_iter);
  38522. }
  38523. }
  38524. return *this;
  38525. }
  38526. // Apply box filter of order 0,1 or 2 \newinstance.
  38527. CImg<Tfloat> get_boxfilter(const float boxsize, const int order, const char axis='x',
  38528. const unsigned int boundary_conditions=1,
  38529. const unsigned int nb_iter=1) const {
  38530. return CImg<Tfloat>(*this,false).boxfilter(boxsize,order,axis,boundary_conditions,nb_iter);
  38531. }
  38532. //! Blur image with a box filter.
  38533. /**
  38534. \param boxsize_x Size of the box window, along the X-axis (can be subpixel).
  38535. \param boxsize_y Size of the box window, along the Y-axis (can be subpixel).
  38536. \param boxsize_z Size of the box window, along the Z-axis (can be subpixel).
  38537. \param boundary_conditions Boundary conditions.
  38538. Can be <tt>{ false=dirichlet | true=neumann | 2=periodic | 3=mirror }</tt>.
  38539. \param nb_iter Number of filter iterations.
  38540. \note
  38541. - This is a recursive algorithm, not depending on the values of the box kernel size.
  38542. \see blur().
  38543. **/
  38544. CImg<T>& blur_box(const float boxsize_x, const float boxsize_y, const float boxsize_z,
  38545. const unsigned int boundary_conditions=1,
  38546. const unsigned int nb_iter=1) {
  38547. if (is_empty()) return *this;
  38548. if (_width>1) boxfilter(boxsize_x,0,'x',boundary_conditions,nb_iter);
  38549. if (_height>1) boxfilter(boxsize_y,0,'y',boundary_conditions,nb_iter);
  38550. if (_depth>1) boxfilter(boxsize_z,0,'z',boundary_conditions,nb_iter);
  38551. return *this;
  38552. }
  38553. //! Blur image with a box filter \newinstance.
  38554. CImg<Tfloat> get_blur_box(const float boxsize_x, const float boxsize_y, const float boxsize_z,
  38555. const unsigned int boundary_conditions=1) const {
  38556. return CImg<Tfloat>(*this,false).blur_box(boxsize_x,boxsize_y,boxsize_z,boundary_conditions);
  38557. }
  38558. //! Blur image with a box filter.
  38559. /**
  38560. \param boxsize Size of the box window (can be subpixel).
  38561. \param boundary_conditions Boundary conditions.
  38562. Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.a
  38563. \see deriche(), vanvliet().
  38564. **/
  38565. CImg<T>& blur_box(const float boxsize, const unsigned int boundary_conditions=1) {
  38566. const float nboxsize = boxsize>=0?boxsize:-boxsize*cimg::max(_width,_height,_depth)/100;
  38567. return blur_box(nboxsize,nboxsize,nboxsize,boundary_conditions);
  38568. }
  38569. //! Blur image with a box filter \newinstance.
  38570. CImg<Tfloat> get_blur_box(const float boxsize, const unsigned int boundary_conditions=1) const {
  38571. return CImg<Tfloat>(*this,false).blur_box(boxsize,boundary_conditions);
  38572. }
  38573. //! Blur image, with the image guided filter.
  38574. /**
  38575. \param guide Image used to guide the smoothing process.
  38576. \param radius Spatial radius. If negative, it is expressed as a percentage of the largest image size.
  38577. \param regularization Regularization parameter.
  38578. If negative, it is expressed as a percentage of the guide value range.
  38579. \note This method implements the filtering algorithm described in:
  38580. He, Kaiming; Sun, Jian; Tang, Xiaoou, "Guided Image Filtering," Pattern Analysis and Machine Intelligence,
  38581. IEEE Transactions on , vol.35, no.6, pp.1397,1409, June 2013
  38582. **/
  38583. template<typename t>
  38584. CImg<T>& blur_guided(const CImg<t>& guide, const float radius, const float regularization) {
  38585. return get_blur_guided(guide,radius,regularization).move_to(*this);
  38586. }
  38587. //! Blur image, with the image guided filter \newinstance.
  38588. template<typename t>
  38589. CImg<Tfloat> get_blur_guided(const CImg<t>& guide, const float radius, const float regularization) const {
  38590. if (!is_sameXYZ(guide))
  38591. throw CImgArgumentException(_cimg_instance
  38592. "blur_guided(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
  38593. cimg_instance,
  38594. guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
  38595. if (is_empty() || !radius) return *this;
  38596. const int _radius = radius>=0?(int)radius:(int)(-radius*cimg::max(_width,_height,_depth)/100);
  38597. float _regularization = regularization;
  38598. if (regularization<0) {
  38599. T edge_min, edge_max = guide.max_min(edge_min);
  38600. if (edge_min==edge_max) return *this;
  38601. _regularization = -regularization*(edge_max - edge_min)/100;
  38602. }
  38603. _regularization = std::max(_regularization,0.01f);
  38604. const unsigned int psize = (unsigned int)(1 + 2*_radius);
  38605. CImg<Tfloat>
  38606. mean_p = get_blur_box(psize,true),
  38607. mean_I = guide.get_blur_box(psize,true).resize(mean_p),
  38608. cov_Ip = get_mul(guide).blur_box(psize,true)-=mean_p.get_mul(mean_I),
  38609. var_I = guide.get_sqr().blur_box(psize,true)-=mean_I.get_sqr(),
  38610. &a = cov_Ip.div(var_I+=_regularization),
  38611. &b = mean_p-=a.get_mul(mean_I);
  38612. a.blur_box(psize,true);
  38613. b.blur_box(psize,true);
  38614. return a.mul(guide)+=b;
  38615. }
  38616. //! Blur image using patch-based space.
  38617. /**
  38618. \param guide Image used to model the smoothing weights.
  38619. \param sigma_s Amount of blur along the XYZ-axes.
  38620. \param sigma_r Amount of blur along the value axis.
  38621. \param patch_size Size of the patches.
  38622. \param lookup_size Size of the window to search similar patches.
  38623. \param smoothness Smoothness for the patch comparison.
  38624. \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
  38625. **/
  38626. template<typename t>
  38627. CImg<T>& blur_patch(const CImg<t>& guide,
  38628. const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
  38629. const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) {
  38630. if (is_empty() || !patch_size || !lookup_size) return *this;
  38631. return get_blur_patch(guide,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx).move_to(*this);
  38632. }
  38633. //! Blur image using patch-based space \newinstance.
  38634. template<typename t>
  38635. CImg<Tfloat> get_blur_patch(const CImg<t>& guide,
  38636. const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
  38637. const unsigned int lookup_size=4, const float smoothness=0,
  38638. const bool is_fast_approx=true) const {
  38639. #define _cimg_blur_patch3d_fast(N) { \
  38640. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) \
  38641. cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4) \
  38642. firstprivate(P,Q)) \
  38643. cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { \
  38644. cimg_abort_test2; \
  38645. cimg_def##N##x##N##x##N(res,x,y,z); \
  38646. tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,x,y,z,c,pP,tfloat); pP+=N3; } \
  38647. const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \
  38648. x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \
  38649. tfloat sum_weights = 0; \
  38650. cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) \
  38651. if (cimg::abs(_guide(x,y,z,0) - _guide(p,q,r,0))<sigma_r3) { \
  38652. tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,p,q,r,c,pQ,tfloat); pQ+=N3; } \
  38653. tfloat distance2 = 0; \
  38654. pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
  38655. distance2/=Pnorm; \
  38656. const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, dz = (tfloat)r - z, \
  38657. alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = alldist>3?0:1; \
  38658. sum_weights+=weight; \
  38659. cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c); \
  38660. } \
  38661. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights; \
  38662. else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \
  38663. } _cimg_abort_catch_openmp2 }
  38664. #define _cimg_blur_patch3d(N) { \
  38665. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) \
  38666. cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4) \
  38667. firstprivate(P,Q)) \
  38668. cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { \
  38669. cimg_abort_test2; \
  38670. cimg_def##N##x##N##x##N(res,x,y,z); \
  38671. tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,x,y,z,c,pP,tfloat); pP+=N3; } \
  38672. const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \
  38673. x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \
  38674. tfloat sum_weights = 0, weight_max = 0; \
  38675. cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { \
  38676. tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,p,q,r,c,pQ,tfloat); pQ+=N3; } \
  38677. tfloat distance2 = 0; \
  38678. pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
  38679. distance2/=Pnorm; \
  38680. const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, dz = (tfloat)r - z, \
  38681. alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = std::exp(-alldist); \
  38682. if (weight>weight_max) weight_max = weight; \
  38683. sum_weights+=weight; \
  38684. cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c); \
  38685. } \
  38686. sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight_max*(*this)(x,y,z,c); \
  38687. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights; \
  38688. else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \
  38689. } _cimg_abort_catch_openmp2 }
  38690. #define _cimg_blur_patch2d_fast(N) { \
  38691. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4) \
  38692. firstprivate(P,Q)) \
  38693. cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { \
  38694. cimg_abort_test2; \
  38695. cimg_def##N##x##N(res,x,y); \
  38696. tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,x,y,0,c,pP,tfloat); pP+=N2; } \
  38697. const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \
  38698. tfloat sum_weights = 0; \
  38699. cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) \
  38700. if (cimg::abs(_guide(x,y,0,0) - _guide(p,q,0,0))<sigma_r3) { \
  38701. tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,p,q,0,c,pQ,tfloat); pQ+=N2; } \
  38702. tfloat distance2 = 0; \
  38703. pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
  38704. distance2/=Pnorm; \
  38705. const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, \
  38706. alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = alldist>3?0:1; \
  38707. sum_weights+=weight; \
  38708. cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c); \
  38709. } \
  38710. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights; \
  38711. else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \
  38712. } _cimg_abort_catch_openmp2 }
  38713. #define _cimg_blur_patch2d(N) { \
  38714. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4) \
  38715. firstprivate(P,Q)) \
  38716. cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { \
  38717. cimg_abort_test2; \
  38718. cimg_def##N##x##N(res,x,y); \
  38719. tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,x,y,0,c,pP,tfloat); pP+=N2; } \
  38720. const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \
  38721. tfloat sum_weights = 0, weight_max = 0; \
  38722. cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { \
  38723. tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,p,q,0,c,pQ,tfloat); pQ+=N2; } \
  38724. tfloat distance2 = 0; \
  38725. pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
  38726. distance2/=Pnorm; \
  38727. const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, \
  38728. alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = std::exp(-alldist); \
  38729. if (weight>weight_max) weight_max = weight; \
  38730. sum_weights+=weight; \
  38731. cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c); \
  38732. } \
  38733. sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight_max*(*this)(x,y,c); \
  38734. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights; \
  38735. else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \
  38736. } _cimg_abort_catch_openmp2 }
  38737. typedef _cimg_tfloat tfloat;
  38738. if (!is_sameXYZ(guide))
  38739. throw CImgArgumentException(_cimg_instance
  38740. "blur_patch(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
  38741. cimg_instance,
  38742. guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
  38743. if (is_empty() || !patch_size || !lookup_size) return +*this;
  38744. Tfloat val_min, val_max = (Tfloat)max_min(val_min);
  38745. _cimg_abort_init_openmp;
  38746. cimg_abort_init;
  38747. CImg<Tfloat> res(_width,_height,_depth,_spectrum,0);
  38748. const CImg<tfloat>
  38749. __guide = guide?CImg<tfloat>(guide,guide.pixel_type()==cimg::type<tfloat>::string()):
  38750. CImg<tfloat>(*this,pixel_type()==cimg::type<tfloat>::string()),
  38751. _guide = smoothness>0?__guide.get_blur(smoothness):__guide.get_shared();
  38752. CImg<tfloat> P(_guide._spectrum*patch_size*patch_size*(_depth>1?patch_size:1)), Q(P);
  38753. t guide_min = (t)0, guide_max = (t)0;
  38754. if (sigma_r<0) guide_max = guide.max_min(guide_min);
  38755. const float
  38756. guide_delta = (float)(guide_max - guide_min),
  38757. _sigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100,
  38758. _sigma_r = sigma_r>=0?sigma_r:-sigma_r*guide_delta/100,
  38759. sigma_s2 = _sigma_s*_sigma_s,
  38760. sigma_r2 = _sigma_r*_sigma_r,
  38761. sigma_r3 = 3*_sigma_r,
  38762. Pnorm = P.size()*sigma_r2;
  38763. const int rsize2 = (int)lookup_size/2, rsize1 = (int)lookup_size - rsize2 - 1;
  38764. const unsigned int N2 = patch_size*patch_size, N3 = N2*patch_size;
  38765. cimg::unused(N2,N3);
  38766. if (_depth>1) switch (patch_size) { // 3D
  38767. case 2 : if (is_fast_approx) _cimg_blur_patch3d_fast(2) else _cimg_blur_patch3d(2) break;
  38768. case 3 : if (is_fast_approx) _cimg_blur_patch3d_fast(3) else _cimg_blur_patch3d(3) break;
  38769. default : {
  38770. const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1;
  38771. if (is_fast_approx) {
  38772. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  38773. cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4)
  38774. firstprivate(P,Q))
  38775. cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { // Fast
  38776. cimg_abort_test2;
  38777. P = _guide.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true);
  38778. const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1,
  38779. x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2;
  38780. tfloat sum_weights = 0;
  38781. cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r)
  38782. if (cimg::abs(_guide(x,y,z,0) - _guide(p,q,r,0))<sigma_r3) {
  38783. (Q = _guide.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P;
  38784. const tfloat
  38785. dx = (tfloat)x - p, dy = (tfloat)y - q, dz = (tfloat)z - r,
  38786. distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2),
  38787. weight = distance2>3?0:1;
  38788. sum_weights+=weight;
  38789. cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c);
  38790. }
  38791. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights;
  38792. else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c));
  38793. } _cimg_abort_catch_openmp2
  38794. } else {
  38795. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  38796. cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4)
  38797. firstprivate(P,Q))
  38798. cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { // Exact
  38799. cimg_abort_test2;
  38800. P = _guide.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true);
  38801. const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1,
  38802. x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2;
  38803. tfloat sum_weights = 0, weight_max = 0;
  38804. cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) {
  38805. (Q = _guide.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P;
  38806. const tfloat
  38807. dx = (tfloat)x - p, dy = (tfloat)y - q, dz = (tfloat)z - r,
  38808. distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2),
  38809. weight = std::exp(-distance2);
  38810. if (weight>weight_max) weight_max = weight;
  38811. sum_weights+=weight;
  38812. cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c);
  38813. }
  38814. sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight_max*(*this)(x,y,z,c);
  38815. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights;
  38816. else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c));
  38817. } _cimg_abort_catch_openmp2
  38818. }
  38819. }
  38820. } else switch (patch_size) { // 2D
  38821. case 2 : if (is_fast_approx) _cimg_blur_patch2d_fast(2) else _cimg_blur_patch2d(2) break;
  38822. case 3 : if (is_fast_approx) _cimg_blur_patch2d_fast(3) else _cimg_blur_patch2d(3) break;
  38823. case 4 : if (is_fast_approx) _cimg_blur_patch2d_fast(4) else _cimg_blur_patch2d(4) break;
  38824. case 5 : if (is_fast_approx) _cimg_blur_patch2d_fast(5) else _cimg_blur_patch2d(5) break;
  38825. case 6 : if (is_fast_approx) _cimg_blur_patch2d_fast(6) else _cimg_blur_patch2d(6) break;
  38826. case 7 : if (is_fast_approx) _cimg_blur_patch2d_fast(7) else _cimg_blur_patch2d(7) break;
  38827. case 8 : if (is_fast_approx) _cimg_blur_patch2d_fast(8) else _cimg_blur_patch2d(8) break;
  38828. case 9 : if (is_fast_approx) _cimg_blur_patch2d_fast(9) else _cimg_blur_patch2d(9) break;
  38829. default : { // Fast
  38830. const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1;
  38831. if (is_fast_approx) {
  38832. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4)
  38833. firstprivate(P,Q))
  38834. cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { // Fast
  38835. cimg_abort_test2;
  38836. P = _guide.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true);
  38837. const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2;
  38838. tfloat sum_weights = 0;
  38839. cimg_for_inXY(res,x0,y0,x1,y1,p,q)
  38840. if (cimg::abs(_guide(x,y,0) - _guide(p,q,0))<sigma_r3) {
  38841. (Q = _guide.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P;
  38842. const tfloat
  38843. dx = (tfloat)x - p, dy = (tfloat)y - q,
  38844. distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2),
  38845. weight = distance2>3?0:1;
  38846. sum_weights+=weight;
  38847. cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c);
  38848. }
  38849. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights;
  38850. else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c));
  38851. } _cimg_abort_catch_openmp2
  38852. } else {
  38853. cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4)
  38854. firstprivate(P,Q))
  38855. cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { // Exact
  38856. cimg_abort_test2;
  38857. P = _guide.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true);
  38858. const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2;
  38859. tfloat sum_weights = 0, weight_max = 0;
  38860. cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) {
  38861. (Q = _guide.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P;
  38862. const tfloat
  38863. dx = (tfloat)x - p, dy = (tfloat)y - q,
  38864. distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2),
  38865. weight = std::exp(-distance2);
  38866. if (weight>weight_max) weight_max = weight;
  38867. sum_weights+=weight;
  38868. cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c);
  38869. }
  38870. sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight_max*(*this)(x,y,c);
  38871. if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights;
  38872. else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c));
  38873. } _cimg_abort_catch_openmp2
  38874. }
  38875. }
  38876. }
  38877. cimg_abort_test;
  38878. return res.cut(val_min,val_max);
  38879. }
  38880. //! Blur image using patch-based space \simplification.
  38881. CImg<T>& blur_patch(const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
  38882. const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) {
  38883. return blur_patch(*this,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx);
  38884. }
  38885. //! Blur image using patch-based space \simplification \newinstance.
  38886. CImg<Tfloat> get_blur_patch(const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
  38887. const unsigned int lookup_size=4, const float smoothness=0,
  38888. const bool is_fast_approx=true) const {
  38889. return get_blur_patch(*this,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx);
  38890. }
  38891. //! Blur image with the median filter.
  38892. /**
  38893. \param n Size of the median filter.
  38894. \param threshold Threshold used to discard pixels too far from the current pixel value in the median computation.
  38895. **/
  38896. CImg<T>& blur_median(const unsigned int n, const float threshold=0) {
  38897. if (!n) return *this;
  38898. return get_blur_median(n,threshold).move_to(*this);
  38899. }
  38900. //! Blur image with the median filter \newinstance.
  38901. CImg<T> get_blur_median(const unsigned int n, const float threshold=0) const {
  38902. if (is_empty() || n<=1) return +*this;
  38903. CImg<T> res(_width,_height,_depth,_spectrum);
  38904. T *ptrd = res._data;
  38905. cimg::unused(ptrd);
  38906. const int hr = (int)n/2, hl = n - hr - 1;
  38907. if (res._depth!=1) { // 3D
  38908. if (threshold>0)
  38909. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  38910. _height*_depth*_spectrum>=4))
  38911. cimg_forXYZC(*this,x,y,z,c) { // With threshold
  38912. const int
  38913. x0 = x - hl, y0 = y - hl, z0 = z - hl, x1 = x + hr, y1 = y + hr, z1 = z + hr,
  38914. nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0,
  38915. nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1, nz1 = z1>=depth()?depth() - 1:z1;
  38916. const Tfloat val0 = (Tfloat)(*this)(x,y,z,c);
  38917. CImg<T> values(n*n*n);
  38918. unsigned int nb_values = 0;
  38919. T *_ptrd = values.data();
  38920. cimg_for_inXYZ(*this,nx0,ny0,nz0,nx1,ny1,nz1,p,q,r)
  38921. if (cimg::abs((*this)(p,q,r,c) - val0)<=threshold) { *(_ptrd++) = (*this)(p,q,r,c); ++nb_values; }
  38922. res(x,y,z,c) = nb_values?values.get_shared_points(0,nb_values - 1).median():(*this)(x,y,z,c);
  38923. }
  38924. else
  38925. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  38926. _height*_depth*_spectrum>=4))
  38927. cimg_forXYZC(*this,x,y,z,c) { // Without threshold
  38928. const int
  38929. x0 = x - hl, y0 = y - hl, z0 = z - hl, x1 = x + hr, y1 = y + hr, z1 = z + hr,
  38930. nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0,
  38931. nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1, nz1 = z1>=depth()?depth() - 1:z1;
  38932. res(x,y,z,c) = get_crop(nx0,ny0,nz0,c,nx1,ny1,nz1,c).median();
  38933. }
  38934. } else {
  38935. if (threshold>0)
  38936. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
  38937. _height*_spectrum>=4))
  38938. cimg_forXYC(*this,x,y,c) { // With threshold
  38939. const int
  38940. x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr,
  38941. nx0 = x0<0?0:x0, ny0 = y0<0?0:y0,
  38942. nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1;
  38943. const Tfloat val0 = (Tfloat)(*this)(x,y,c);
  38944. CImg<T> values(n*n);
  38945. unsigned int nb_values = 0;
  38946. T *_ptrd = values.data();
  38947. cimg_for_inXY(*this,nx0,ny0,nx1,ny1,p,q)
  38948. if (cimg::abs((*this)(p,q,c) - val0)<=threshold) { *(_ptrd++) = (*this)(p,q,c); ++nb_values; }
  38949. res(x,y,c) = nb_values?values.get_shared_points(0,nb_values - 1).median():(*this)(x,y,c);
  38950. }
  38951. else {
  38952. const int
  38953. w1 = width() - 1, h1 = height() - 1,
  38954. w2 = width() - 2, h2 = height() - 2,
  38955. w3 = width() - 3, h3 = height() - 3,
  38956. w4 = width() - 4, h4 = height() - 4;
  38957. switch (n) { // Without threshold
  38958. case 3 : {
  38959. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
  38960. cimg_forC(*this,c) {
  38961. CImg<T> I(9);
  38962. cimg_for_in3x3(*this,1,1,w2,h2,x,y,0,c,I,T)
  38963. res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],I[5],I[6],I[7],I[8]);
  38964. cimg_for_borderXY(*this,x,y,1)
  38965. res(x,y,c) = get_crop(std::max(0,x - 1),std::max(0,y - 1),0,c,
  38966. std::min(w1,x + 1),std::min(h1,y + 1),0,c).median();
  38967. }
  38968. } break;
  38969. case 5 : {
  38970. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
  38971. cimg_forC(*this,c) {
  38972. CImg<T> I(25);
  38973. cimg_for_in5x5(*this,2,2,w3,h3,x,y,0,c,I,T)
  38974. res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],
  38975. I[5],I[6],I[7],I[8],I[9],
  38976. I[10],I[11],I[12],I[13],I[14],
  38977. I[15],I[16],I[17],I[18],I[19],
  38978. I[20],I[21],I[22],I[23],I[24]);
  38979. cimg_for_borderXY(*this,x,y,2)
  38980. res(x,y,c) = get_crop(std::max(0,x - 2),std::max(0,y - 2),0,c,
  38981. std::min(w1,x + 2),std::min(h1,y + 2),0,c).median();
  38982. }
  38983. } break;
  38984. case 7 : {
  38985. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
  38986. cimg_forC(*this,c) {
  38987. CImg<T> I(49);
  38988. cimg_for_in7x7(*this,3,3,w4,h4,x,y,0,c,I,T)
  38989. res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],I[5],I[6],
  38990. I[7],I[8],I[9],I[10],I[11],I[12],I[13],
  38991. I[14],I[15],I[16],I[17],I[18],I[19],I[20],
  38992. I[21],I[22],I[23],I[24],I[25],I[26],I[27],
  38993. I[28],I[29],I[30],I[31],I[32],I[33],I[34],
  38994. I[35],I[36],I[37],I[38],I[39],I[40],I[41],
  38995. I[42],I[43],I[44],I[45],I[46],I[47],I[48]);
  38996. cimg_for_borderXY(*this,x,y,3)
  38997. res(x,y,c) = get_crop(std::max(0,x - 3),std::max(0,y - 3),0,c,
  38998. std::min(w1,x + 3),std::min(h1,y + 3),0,c).median();
  38999. }
  39000. } break;
  39001. default : {
  39002. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39003. cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 && _height*_spectrum>=4))
  39004. cimg_forXYC(*this,x,y,c) {
  39005. const int
  39006. x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr,
  39007. nx0 = x0<0?0:x0, ny0 = y0<0?0:y0,
  39008. nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1;
  39009. res(x,y,c) = get_crop(nx0,ny0,0,c,nx1,ny1,0,c).median();
  39010. }
  39011. }
  39012. }
  39013. }
  39014. }
  39015. return res;
  39016. }
  39017. //! Sharpen image.
  39018. /**
  39019. \param amplitude Sharpening amplitude
  39020. \param sharpen_type Select sharpening method. Can be <tt>{ false=inverse diffusion | true=shock filters }</tt>.
  39021. \param edge Edge threshold (shock filters only).
  39022. \param alpha Gradient smoothness (shock filters only).
  39023. \param sigma Tensor smoothness (shock filters only).
  39024. **/
  39025. CImg<T>& sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1,
  39026. const float alpha=0, const float sigma=0) {
  39027. if (is_empty()) return *this;
  39028. T val_min, val_max = max_min(val_min);
  39029. const float nedge = edge/2;
  39030. CImg<Tfloat> velocity(_width,_height,_depth,_spectrum), _veloc_max(_spectrum);
  39031. if (_depth>1) { // 3D
  39032. if (sharpen_type) { // Shock filters
  39033. CImg<Tfloat> G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors());
  39034. if (sigma>0) G.blur(sigma);
  39035. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*32 &&
  39036. _height*_depth>=16))
  39037. cimg_forYZ(G,y,z) {
  39038. Tfloat *ptrG0 = G.data(0,y,z,0), *ptrG1 = G.data(0,y,z,1),
  39039. *ptrG2 = G.data(0,y,z,2), *ptrG3 = G.data(0,y,z,3);
  39040. CImg<Tfloat> val, vec;
  39041. cimg_forX(G,x) {
  39042. G.get_tensor_at(x,y,z).symmetric_eigen(val,vec);
  39043. if (val[0]<0) val[0] = 0;
  39044. if (val[1]<0) val[1] = 0;
  39045. if (val[2]<0) val[2] = 0;
  39046. *(ptrG0++) = vec(0,0);
  39047. *(ptrG1++) = vec(0,1);
  39048. *(ptrG2++) = vec(0,2);
  39049. *(ptrG3++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1] + val[2],-(Tfloat)nedge);
  39050. }
  39051. }
  39052. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*512 &&
  39053. _spectrum>=2))
  39054. cimg_forC(*this,c) {
  39055. Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
  39056. CImg_3x3x3(I,Tfloat);
  39057. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
  39058. const Tfloat
  39059. u = G(x,y,z,0),
  39060. v = G(x,y,z,1),
  39061. w = G(x,y,z,2),
  39062. amp = G(x,y,z,3),
  39063. ixx = Incc + Ipcc - 2*Iccc,
  39064. ixy = (Innc + Ippc - Inpc - Ipnc)/4,
  39065. ixz = (Incn + Ipcp - Incp - Ipcn)/4,
  39066. iyy = Icnc + Icpc - 2*Iccc,
  39067. iyz = (Icnn + Icpp - Icnp - Icpn)/4,
  39068. izz = Iccn + Iccp - 2*Iccc,
  39069. ixf = Incc - Iccc,
  39070. ixb = Iccc - Ipcc,
  39071. iyf = Icnc - Iccc,
  39072. iyb = Iccc - Icpc,
  39073. izf = Iccn - Iccc,
  39074. izb = Iccc - Iccp,
  39075. itt = u*u*ixx + v*v*iyy + w*w*izz + 2*u*v*ixy + 2*u*w*ixz + 2*v*w*iyz,
  39076. it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb) + w*cimg::minmod(izf,izb),
  39077. veloc = -amp*cimg::sign(itt)*cimg::abs(it);
  39078. *(ptrd++) = veloc;
  39079. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  39080. }
  39081. _veloc_max[c] = veloc_max;
  39082. }
  39083. } else // Inverse diffusion
  39084. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*512 &&
  39085. _spectrum>=2))
  39086. cimg_forC(*this,c) {
  39087. Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
  39088. CImg_3x3x3(I,Tfloat);
  39089. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
  39090. const Tfloat veloc = -Ipcc - Incc - Icpc - Icnc - Iccp - Iccn + 6*Iccc;
  39091. *(ptrd++) = veloc;
  39092. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  39093. }
  39094. _veloc_max[c] = veloc_max;
  39095. }
  39096. } else { // 2D
  39097. if (sharpen_type) { // Shock filters
  39098. CImg<Tfloat> G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors());
  39099. if (sigma>0) G.blur(sigma);
  39100. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*32 &&
  39101. _height>=(cimg_openmp_sizefactor)*16))
  39102. cimg_forY(G,y) {
  39103. CImg<Tfloat> val, vec;
  39104. Tfloat *ptrG0 = G.data(0,y,0,0), *ptrG1 = G.data(0,y,0,1), *ptrG2 = G.data(0,y,0,2);
  39105. cimg_forX(G,x) {
  39106. G.get_tensor_at(x,y).symmetric_eigen(val,vec);
  39107. if (val[0]<0) val[0] = 0;
  39108. if (val[1]<0) val[1] = 0;
  39109. *(ptrG0++) = vec(0,0);
  39110. *(ptrG1++) = vec(0,1);
  39111. *(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1],-(Tfloat)nedge);
  39112. }
  39113. }
  39114. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*512 &&
  39115. _spectrum>=2))
  39116. cimg_forC(*this,c) {
  39117. Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
  39118. CImg_3x3(I,Tfloat);
  39119. cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
  39120. const Tfloat
  39121. u = G(x,y,0),
  39122. v = G(x,y,1),
  39123. amp = G(x,y,2),
  39124. ixx = Inc + Ipc - 2*Icc,
  39125. ixy = (Inn + Ipp - Inp - Ipn)/4,
  39126. iyy = Icn + Icp - 2*Icc,
  39127. ixf = Inc - Icc,
  39128. ixb = Icc - Ipc,
  39129. iyf = Icn - Icc,
  39130. iyb = Icc - Icp,
  39131. itt = u*u*ixx + v*v*iyy + 2*u*v*ixy,
  39132. it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb),
  39133. veloc = -amp*cimg::sign(itt)*cimg::abs(it);
  39134. *(ptrd++) = veloc;
  39135. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  39136. }
  39137. _veloc_max[c] = veloc_max;
  39138. }
  39139. } else // Inverse diffusion
  39140. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*512 &&
  39141. _spectrum>=2))
  39142. cimg_forC(*this,c) {
  39143. Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
  39144. CImg_3x3(I,Tfloat);
  39145. cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
  39146. const Tfloat veloc = -Ipc - Inc - Icp - Icn + 4*Icc;
  39147. *(ptrd++) = veloc;
  39148. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  39149. }
  39150. _veloc_max[c] = veloc_max;
  39151. }
  39152. }
  39153. const Tfloat veloc_max = _veloc_max.max();
  39154. if (veloc_max<=0) return *this;
  39155. return ((velocity*=amplitude/veloc_max)+=*this).cut(val_min,val_max).move_to(*this);
  39156. }
  39157. //! Sharpen image \newinstance.
  39158. CImg<T> get_sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1,
  39159. const float alpha=0, const float sigma=0) const {
  39160. return (+*this).sharpen(amplitude,sharpen_type,edge,alpha,sigma);
  39161. }
  39162. //! Return image gradient.
  39163. /**
  39164. \param axes Axes considered for the gradient computation, as a C-string (e.g "xy").
  39165. \param scheme = Numerical scheme used for the gradient computation:
  39166. - -1 = Backward finite differences
  39167. - 0 = Centered finite differences (default)
  39168. - 1 = Forward finite differences
  39169. - 2 = Using Sobel kernels
  39170. - 3 = Using rotation invariant kernels
  39171. - 4 = Using Deriche recursive filter.
  39172. - 5 = Using Van Vliet recursive filter.
  39173. **/
  39174. CImgList<Tfloat> get_gradient(const char *const axes=0, const int scheme=0) const {
  39175. CImgList<Tfloat> res;
  39176. char __axes[4] = { 0 };
  39177. const char *_axes = axes?axes:__axes;
  39178. if (!axes) {
  39179. unsigned int k = 0;
  39180. if (_width>1) __axes[k++] = 'x';
  39181. if (_height>1) __axes[k++] = 'y';
  39182. if (_depth>1) __axes[k++] = 'z';
  39183. }
  39184. CImg<Tfloat> grad;
  39185. while (*_axes) {
  39186. const char axis = cimg::lowercase(*(_axes++));
  39187. if (axis!='x' && axis!='y' && axis!='z')
  39188. throw CImgArgumentException(_cimg_instance
  39189. "get_gradient(): Invalid specified axes '%s'.",
  39190. cimg_instance,
  39191. axes);
  39192. const longT off = axis=='x'?1:axis=='y'?_width:_width*_height;
  39193. if ((axis=='x' && _width==1) || (axis=='y' && _height==1) || (axis=='z' && _depth==1)) {
  39194. grad.assign(_width,_height,_depth,_spectrum,0).move_to(res);
  39195. continue;
  39196. }
  39197. const int _scheme = axis=='z' && (scheme==2 || scheme==3)?0:scheme;
  39198. switch (_scheme) {
  39199. case -1 : { // Backward finite differences
  39200. grad.assign(_width,_height,_depth,_spectrum);
  39201. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
  39202. cimg_forXYZC(*this,x,y,z,c) {
  39203. const ulongT pos = offset(x,y,z,c);
  39204. if ((axis=='x' && !x) || (axis=='y' && !y) || (axis=='z' && !z))
  39205. grad[pos] = 0;
  39206. else
  39207. grad[pos] = (Tfloat)_data[pos] - _data[pos - off];
  39208. }
  39209. grad.move_to(res);
  39210. } break;
  39211. case 1 : { // Forward finite differences
  39212. grad.assign(_width,_height,_depth,_spectrum);
  39213. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
  39214. cimg_forXYZC(*this,x,y,z,c) {
  39215. const ulongT pos = offset(x,y,z,c);
  39216. if ((axis=='x' && x==width() - 1) || (axis=='y' && y==height() - 1) || (axis=='z' && z==depth() - 1))
  39217. grad[pos] = 0;
  39218. else
  39219. grad[pos] = (Tfloat)_data[pos + off] - _data[pos];
  39220. }
  39221. grad.move_to(res);
  39222. } break;
  39223. case 2 : { // Sobel scheme
  39224. grad.assign(_width,_height,_depth,_spectrum);
  39225. if (axis=='x') // X-axis
  39226. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39227. cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
  39228. _depth*_spectrum>=2))
  39229. cimg_forZC(*this,z,c) {
  39230. CImg_3x3(I,Tfloat);
  39231. cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = - Ipp + Inp - 2*Ipc + 2*Inc - Ipn + Inn;
  39232. }
  39233. else // Y-axis
  39234. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39235. cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
  39236. _depth*_spectrum>=2))
  39237. cimg_forZC(*this,z,c) {
  39238. CImg_3x3(I,Tfloat);
  39239. cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = - Ipp - 2*Icp - Inp + Ipn + 2*Icn + Inn;
  39240. }
  39241. grad.move_to(res);
  39242. } break;
  39243. case 3 : { // Rotation invariant scheme
  39244. const Tfloat a = (Tfloat)(0.25f*(2 - std::sqrt(2.f))), b = (Tfloat)(0.5f*(std::sqrt(2.f) - 1));
  39245. grad.assign(_width,_height,_depth,_spectrum);
  39246. if (axis=='x') // X-axis
  39247. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39248. cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
  39249. _depth*_spectrum>=2))
  39250. cimg_forZC(*this,z,c) {
  39251. CImg_3x3(I,Tfloat);
  39252. cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = -a*Ipp - b*Ipc - a*Ipn + a*Inp + b*Inc + a*Inn;
  39253. }
  39254. else // Y-axis
  39255. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39256. cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
  39257. _depth*_spectrum>=2))
  39258. cimg_forZC(*this,z,c) {
  39259. CImg_3x3(I,Tfloat);
  39260. cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = -a*Ipp - b*Icp - a*Inp + a*Ipn + b*Icn + a*Inn;
  39261. }
  39262. grad.move_to(res);
  39263. } break;
  39264. case 4 : // Deriche filter
  39265. get_deriche(0,1,axis).move_to(res);
  39266. break;
  39267. case 5 : // Van Vliet filter
  39268. get_vanvliet(0,1,axis).move_to(res);
  39269. break;
  39270. default : { // Central finite differences
  39271. grad.assign(_width,_height,_depth,_spectrum);
  39272. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
  39273. cimg_forXYZC(*this,x,y,z,c) {
  39274. const ulongT pos = offset(x,y,z,c);
  39275. if ((axis=='x' && !x) || (axis=='y' && !y) || (axis=='z' && !z))
  39276. grad[pos] = ((Tfloat)_data[pos + off] - _data[pos])/2;
  39277. else if ((axis=='x' && x==width() - 1) || (axis=='y' && y==height() - 1) || (axis=='z' && z==depth() - 1))
  39278. grad[pos] = ((Tfloat)_data[pos] - _data[pos - off])/2;
  39279. else
  39280. grad[pos] = ((Tfloat)_data[pos + off] - _data[pos - off])/2;
  39281. }
  39282. grad.move_to(res);
  39283. } break;
  39284. }
  39285. }
  39286. return res;
  39287. }
  39288. //! Return image hessian.
  39289. /**
  39290. \param axes Axes considered for the hessian computation, as a C-string (e.g "xy").
  39291. **/
  39292. CImgList<Tfloat> get_hessian(const char *const axes=0) const {
  39293. CImgList<Tfloat> res;
  39294. char __axes[12] = { 0 };
  39295. const char *_axes = axes?axes:__axes;
  39296. if (!axes) {
  39297. unsigned int k = 0;
  39298. if (_width>1) { __axes[k++] = 'x'; __axes[k++] = 'x'; }
  39299. if (_width>1 && _height>1) { __axes[k++] = 'x'; __axes[k++] = 'y'; }
  39300. if (_width>1 && _depth>1) { __axes[k++] = 'x'; __axes[k++] = 'z'; }
  39301. if (_height>1) { __axes[k++] = 'y'; __axes[k++] = 'y'; }
  39302. if (_height>1 && _depth>1) { __axes[k++] = 'y'; __axes[k++] = 'z'; }
  39303. if (_depth>1) { __axes[k++] = 'z'; __axes[k++] = 'z'; }
  39304. }
  39305. const unsigned int len = (unsigned int)std::strlen(_axes);
  39306. if (len%2)
  39307. throw CImgArgumentException(_cimg_instance
  39308. "get_hessian(): Invalid specified axes '%s'.",
  39309. cimg_instance,
  39310. axes);
  39311. CImg<Tfloat> hess;
  39312. for (unsigned int k = 0; k<len; k+=2) {
  39313. const char
  39314. _axis1 = cimg::lowercase(_axes[k]),
  39315. _axis2 = cimg::lowercase(_axes[k + 1]),
  39316. axis1 = std::min(_axis1,_axis2),
  39317. axis2 = std::max(_axis2,_axis2);
  39318. if (axis1!='x' && axis1!='y' && axis1!='z' &&
  39319. axis2!='x' && axis2!='y' && axis2!='z')
  39320. throw CImgArgumentException(_cimg_instance
  39321. "get_hessian(): Invalid specified axes '%s'.",
  39322. cimg_instance,
  39323. axes);
  39324. const longT off = axis1=='x'?1:axis1=='y'?_width:_width*_height;
  39325. hess.assign(_width,_height,_depth,_spectrum);
  39326. if (((axis1=='x' || axis2=='x') && _width==1) ||
  39327. ((axis1=='y' || axis2=='y') && _height==1) ||
  39328. ((axis1=='z' || axis2=='z') && _depth==1)) {
  39329. hess.fill(0).move_to(res);
  39330. continue;
  39331. }
  39332. if (axis1==axis2) // Ixx, Iyy, Izz
  39333. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
  39334. cimg_forXYZC(*this,x,y,z,c) {
  39335. const ulongT pos = offset(x,y,z,c);
  39336. if ((axis1=='x' && !x) || (axis1=='y' && !y) || (axis1=='z' && !z))
  39337. hess[pos] = (Tfloat)_data[pos + off] - _data[pos];
  39338. else if ((axis1=='x' && x==width() - 1) ||
  39339. (axis1=='y' && y==height() - 1) ||
  39340. (axis1=='z' && z==depth() - 1))
  39341. hess[pos] = (Tfloat)_data[pos - off] - _data[pos];
  39342. else
  39343. hess[pos] = (Tfloat)_data[pos + off] + _data[pos - off] - 2*_data[pos];
  39344. }
  39345. else if (axis1=='x' && axis2=='y') // Ixy
  39346. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39347. cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
  39348. _depth*_spectrum>=2))
  39349. cimg_forZC(*this,z,c) {
  39350. CImg_3x3(I,Tfloat);
  39351. cimg_for3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Inn + Ipp - Inp - Ipn)/4;
  39352. }
  39353. else if (axis1=='x' && axis2=='z') // Ixz
  39354. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*16384 &&
  39355. _spectrum>=2))
  39356. cimg_forC(*this,c) {
  39357. CImg_3x3x3(I,Tfloat);
  39358. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Incn + Ipcp - Incp - Ipcn)/4;
  39359. }
  39360. else // Iyz
  39361. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*16384 &&
  39362. _spectrum>=2))
  39363. cimg_forC(*this,c) {
  39364. CImg_3x3x3(I,Tfloat);
  39365. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Icnn + Icpp - Icnp - Icpn)/4;
  39366. }
  39367. hess.move_to(res);
  39368. }
  39369. return res;
  39370. }
  39371. //! Compute image Laplacian.
  39372. CImg<T>& laplacian() {
  39373. return get_laplacian().move_to(*this);
  39374. }
  39375. //! Compute image Laplacian \newinstance.
  39376. CImg<Tfloat> get_laplacian() const {
  39377. if (is_empty()) return CImg<Tfloat>();
  39378. CImg<Tfloat> res(_width,_height,_depth,_spectrum);
  39379. if (_depth>1) { // 3D
  39380. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
  39381. _spectrum>=2))
  39382. cimg_forC(*this,c) {
  39383. Tfloat *ptrd = res.data(0,0,0,c);
  39384. CImg_3x3x3(I,Tfloat);
  39385. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Incc + Ipcc + Icnc + Icpc + Iccn + Iccp - 6*Iccc;
  39386. }
  39387. } else if (_height>1) { // 2D
  39388. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
  39389. _depth*_spectrum>=2))
  39390. cimg_forC(*this,c) {
  39391. Tfloat *ptrd = res.data(0,0,0,c);
  39392. CImg_3x3(I,Tfloat);
  39393. cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc + Icn + Icp - 4*Icc;
  39394. }
  39395. } else { // 1D
  39396. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*1048576 &&
  39397. _height*_depth*_spectrum>=2))
  39398. cimg_forC(*this,c) {
  39399. Tfloat *ptrd = res.data(0,0,0,c);
  39400. CImg_3x3(I,Tfloat);
  39401. cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc - 2*Icc;
  39402. }
  39403. }
  39404. return res;
  39405. }
  39406. //! Compute the structure tensor field of an image.
  39407. /**
  39408. \param is_fwbw_scheme scheme. Can be <tt>{ false=centered | true=forward-backward }</tt>
  39409. **/
  39410. CImg<T>& structure_tensors(const bool is_fwbw_scheme=false) {
  39411. return get_structure_tensors(is_fwbw_scheme).move_to(*this);
  39412. }
  39413. //! Compute the structure tensor field of an image \newinstance.
  39414. CImg<Tfloat> get_structure_tensors(const bool is_fwbw_scheme=false) const {
  39415. if (is_empty()) return *this;
  39416. CImg<Tfloat> res;
  39417. if (_depth>1) { // 3D
  39418. res.assign(_width,_height,_depth,6,0);
  39419. if (!is_fwbw_scheme) { // Classical central finite differences
  39420. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
  39421. _spectrum>=2))
  39422. cimg_forC(*this,c) {
  39423. Tfloat
  39424. *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2),
  39425. *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5);
  39426. CImg_3x3x3(I,Tfloat);
  39427. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
  39428. const Tfloat
  39429. ix = (Incc - Ipcc)/2,
  39430. iy = (Icnc - Icpc)/2,
  39431. iz = (Iccn - Iccp)/2;
  39432. cimg_pragma_openmp(atomic) *(ptrd0++)+=ix*ix;
  39433. cimg_pragma_openmp(atomic) *(ptrd1++)+=ix*iy;
  39434. cimg_pragma_openmp(atomic) *(ptrd2++)+=ix*iz;
  39435. cimg_pragma_openmp(atomic) *(ptrd3++)+=iy*iy;
  39436. cimg_pragma_openmp(atomic) *(ptrd4++)+=iy*iz;
  39437. cimg_pragma_openmp(atomic) *(ptrd5++)+=iz*iz;
  39438. }
  39439. }
  39440. } else { // Forward/backward finite differences
  39441. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
  39442. _spectrum>=2))
  39443. cimg_forC(*this,c) {
  39444. Tfloat
  39445. *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2),
  39446. *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5);
  39447. CImg_3x3x3(I,Tfloat);
  39448. cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
  39449. const Tfloat
  39450. ixf = Incc - Iccc, ixb = Iccc - Ipcc, ixc = (Incc - Ipcc)/2,
  39451. iyf = Icnc - Iccc, iyb = Iccc - Icpc, iyc = (Icnc - Icpc)/2,
  39452. izf = Iccn - Iccc, izb = Iccc - Iccp, izc = (Iccn - Iccp)/2;
  39453. cimg_pragma_openmp(atomic) *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2;
  39454. cimg_pragma_openmp(atomic) *(ptrd1++)+=ixc*iyc;
  39455. cimg_pragma_openmp(atomic) *(ptrd2++)+=ixc*izc;
  39456. cimg_pragma_openmp(atomic) *(ptrd3++)+=(iyf*iyf + iyb*iyb)/2;
  39457. cimg_pragma_openmp(atomic) *(ptrd4++)+=iyc*izc;
  39458. cimg_pragma_openmp(atomic) *(ptrd5++)+=(izf*izf + izb*izb)/2;
  39459. }
  39460. }
  39461. }
  39462. } else { // 2D
  39463. res.assign(_width,_height,_depth,3,0);
  39464. if (!is_fwbw_scheme) { // Classical central finite differences
  39465. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
  39466. _depth*_spectrum>=2))
  39467. cimg_forC(*this,c) {
  39468. Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2);
  39469. CImg_3x3(I,Tfloat);
  39470. cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
  39471. const Tfloat
  39472. ix = (Inc - Ipc)/2,
  39473. iy = (Icn - Icp)/2;
  39474. cimg_pragma_openmp(atomic) *(ptrd0++)+=ix*ix;
  39475. cimg_pragma_openmp(atomic) *(ptrd1++)+=ix*iy;
  39476. cimg_pragma_openmp(atomic) *(ptrd2++)+=iy*iy;
  39477. }
  39478. }
  39479. } else { // Forward/backward finite differences (version 2)
  39480. cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
  39481. _depth*_spectrum>=2))
  39482. cimg_forC(*this,c) {
  39483. Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2);
  39484. CImg_3x3(I,Tfloat);
  39485. cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
  39486. const Tfloat
  39487. ixf = Inc - Icc, ixb = Icc - Ipc, ixc = (Inc - Ipc)/2,
  39488. iyf = Icn - Icc, iyb = Icc - Icp, iyc = (Icn - Icp)/2;
  39489. cimg_pragma_openmp(atomic) *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2;
  39490. cimg_pragma_openmp(atomic) *(ptrd1++)+=ixc*iyc;
  39491. cimg_pragma_openmp(atomic) *(ptrd2++)+=(iyf*iyf + iyb*iyb)/2;
  39492. }
  39493. }
  39494. }
  39495. }
  39496. return res;
  39497. }
  39498. //! Compute field of diffusion tensors for edge-preserving smoothing.
  39499. /**
  39500. \param sharpness Sharpness
  39501. \param anisotropy Anisotropy
  39502. \param alpha Standard deviation of the gradient blur.
  39503. \param sigma Standard deviation of the structure tensor blur.
  39504. \param is_sqrt Tells if the square root of the tensor field is computed instead.
  39505. **/
  39506. CImg<T>& diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f,
  39507. const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) {
  39508. CImg<Tfloat> res;
  39509. const float
  39510. nsharpness = std::max(sharpness,1e-5f),
  39511. power1 = (is_sqrt?0.5f:1)*nsharpness,
  39512. power2 = power1/(1e-7f + 1 - anisotropy);
  39513. blur(alpha).normalize(0,(T)255);
  39514. if (_depth>1) { // 3D
  39515. get_structure_tensors().move_to(res).blur(sigma);
  39516. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  39517. _height*_depth>=(cimg_openmp_sizefactor)*256))
  39518. cimg_forYZ(*this,y,z) {
  39519. Tfloat
  39520. *ptrd0 = res.data(0,y,z,0), *ptrd1 = res.data(0,y,z,1), *ptrd2 = res.data(0,y,z,2),
  39521. *ptrd3 = res.data(0,y,z,3), *ptrd4 = res.data(0,y,z,4), *ptrd5 = res.data(0,y,z,5);
  39522. CImg<floatT> val(3), vec(3,3);
  39523. cimg_forX(*this,x) {
  39524. res.get_tensor_at(x,y,z).symmetric_eigen(val,vec);
  39525. const float
  39526. _l1 = val[2], _l2 = val[1], _l3 = val[0],
  39527. l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, l3 = _l3>0?_l3:0,
  39528. ux = vec(0,0), uy = vec(0,1), uz = vec(0,2),
  39529. vx = vec(1,0), vy = vec(1,1), vz = vec(1,2),
  39530. wx = vec(2,0), wy = vec(2,1), wz = vec(2,2),
  39531. n1 = (float)std::pow(1 + l1 + l2 + l3,-power1),
  39532. n2 = (float)std::pow(1 + l1 + l2 + l3,-power2);
  39533. *(ptrd0++) = n1*(ux*ux + vx*vx) + n2*wx*wx;
  39534. *(ptrd1++) = n1*(ux*uy + vx*vy) + n2*wx*wy;
  39535. *(ptrd2++) = n1*(ux*uz + vx*vz) + n2*wx*wz;
  39536. *(ptrd3++) = n1*(uy*uy + vy*vy) + n2*wy*wy;
  39537. *(ptrd4++) = n1*(uy*uz + vy*vz) + n2*wy*wz;
  39538. *(ptrd5++) = n1*(uz*uz + vz*vz) + n2*wz*wz;
  39539. }
  39540. }
  39541. } else { // for 2D images
  39542. get_structure_tensors().move_to(res).blur(sigma);
  39543. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
  39544. _height>=(cimg_openmp_sizefactor)*256))
  39545. cimg_forY(*this,y) {
  39546. Tfloat *ptrd0 = res.data(0,y,0,0), *ptrd1 = res.data(0,y,0,1), *ptrd2 = res.data(0,y,0,2);
  39547. CImg<floatT> val(2), vec(2,2);
  39548. cimg_forX(*this,x) {
  39549. res.get_tensor_at(x,y).symmetric_eigen(val,vec);
  39550. const float
  39551. _l1 = val[1], _l2 = val[0],
  39552. l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0,
  39553. ux = vec(1,0), uy = vec(1,1),
  39554. vx = vec(0,0), vy = vec(0,1),
  39555. n1 = (float)std::pow(1 + l1 + l2,-power1),
  39556. n2 = (float)std::pow(1 + l1 + l2,-power2);
  39557. *(ptrd0++) = n1*ux*ux + n2*vx*vx;
  39558. *(ptrd1++) = n1*ux*uy + n2*vx*vy;
  39559. *(ptrd2++) = n1*uy*uy + n2*vy*vy;
  39560. }
  39561. }
  39562. }
  39563. return res.move_to(*this);
  39564. }
  39565. //! Compute field of diffusion tensors for edge-preserving smoothing \newinstance.
  39566. CImg<Tfloat> get_diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f,
  39567. const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) const {
  39568. return CImg<Tfloat>(*this,false).diffusion_tensors(sharpness,anisotropy,alpha,sigma,is_sqrt);
  39569. }
  39570. //! Estimate displacement field between two images.
  39571. /**
  39572. \param source Reference image.
  39573. \param smoothness Smoothness of estimated displacement field.
  39574. \param precision Precision required for algorithm convergence.
  39575. \param nb_scales Number of scales used to estimate the displacement field.
  39576. \param iteration_max Maximum number of iterations allowed for one scale.
  39577. \param is_backward If false, match I2(X + U(X)) = I1(X), else match I2(X) = I1(X - U(X)).
  39578. \param guide Image used as the initial correspondence estimate for the algorithm.
  39579. 'guide' may have a last channel with boolean values (0=false | other=true) that
  39580. tells for each pixel if its correspondence vector is constrained to its initial value (constraint mask).
  39581. **/
  39582. CImg<T>& displacement(const CImg<T>& source, const float smoothness=0.1f, const float precision=5.f,
  39583. const unsigned int nb_scales=0, const unsigned int iteration_max=10000,
  39584. const bool is_backward=false,
  39585. const CImg<floatT>& guide=CImg<floatT>::const_empty()) {
  39586. return get_displacement(source,smoothness,precision,nb_scales,iteration_max,is_backward,guide).
  39587. move_to(*this);
  39588. }
  39589. //! Estimate displacement field between two images \newinstance.
  39590. CImg<floatT> get_displacement(const CImg<T>& source,
  39591. const float smoothness=0.1f, const float precision=5.f,
  39592. const unsigned int nb_scales=0, const unsigned int iteration_max=10000,
  39593. const bool is_backward=false,
  39594. const CImg<floatT>& guide=CImg<floatT>::const_empty()) const {
  39595. if (is_empty() || !source) return +*this;
  39596. if (!is_sameXYZC(source))
  39597. throw CImgArgumentException(_cimg_instance
  39598. "displacement(): Instance and source image (%u,%u,%u,%u,%p) have "
  39599. "different dimensions.",
  39600. cimg_instance,
  39601. source._width,source._height,source._depth,source._spectrum,source._data);
  39602. if (precision<0)
  39603. throw CImgArgumentException(_cimg_instance
  39604. "displacement(): Invalid specified precision %g "
  39605. "(should be >=0)",
  39606. cimg_instance,
  39607. precision);
  39608. const bool is_3d = source._depth>1;
  39609. const unsigned int constraint = is_3d?3:2;
  39610. if (guide &&
  39611. (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<constraint))
  39612. throw CImgArgumentException(_cimg_instance
  39613. "displacement(): Specified guide (%u,%u,%u,%u,%p) "
  39614. "has invalid dimensions.",
  39615. cimg_instance,
  39616. guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
  39617. const unsigned int
  39618. mins = is_3d?cimg::min(_width,_height,_depth):std::min(_width,_height),
  39619. _nb_scales = nb_scales>0?nb_scales:
  39620. (unsigned int)cimg::round(std::log(mins/8.)/std::log(1.5),1,1);
  39621. const float _precision = (float)std::pow(10.,-(double)precision);
  39622. float sm, sM = source.max_min(sm), tm, tM = max_min(tm);
  39623. const float sdelta = sm==sM?1:(sM - sm), tdelta = tm==tM?1:(tM - tm);
  39624. CImg<floatT> U, V;
  39625. floatT bound = 0;
  39626. for (int scale = (int)_nb_scales - 1; scale>=0; --scale) {
  39627. const float factor = (float)std::pow(1.5,(double)scale);
  39628. const unsigned int
  39629. _sw = (unsigned int)(_width/factor), sw = _sw?_sw:1,
  39630. _sh = (unsigned int)(_height/factor), sh = _sh?_sh:1,
  39631. _sd = (unsigned int)(_depth/factor), sd = _sd?_sd:1;
  39632. if (sw<5 && sh<5 && (!is_3d || sd<5)) continue; // Skip too small scales
  39633. const CImg<Tfloat>
  39634. I1 = (source.get_resize(sw,sh,sd,-100,2)-=sm)/=sdelta,
  39635. I2 = (get_resize(I1,2)-=tm)/=tdelta;
  39636. if (guide._spectrum>constraint) guide.get_resize(I2._width,I2._height,I2._depth,-100,1).move_to(V);
  39637. if (U) (U*=1.5f).resize(I2._width,I2._height,I2._depth,-100,3);
  39638. else {
  39639. if (guide)
  39640. guide.get_shared_channels(0,is_3d?2:1).get_resize(I2._width,I2._height,I2._depth,-100,2).move_to(U);
  39641. else U.assign(I2._width,I2._height,I2._depth,is_3d?3:2,0);
  39642. }
  39643. float dt = 2, energy = cimg::type<float>::max();
  39644. const CImgList<Tfloat> dI = is_backward?I1.get_gradient():I2.get_gradient();
  39645. cimg_abort_init;
  39646. for (unsigned int iteration = 0; iteration<iteration_max; ++iteration) {
  39647. cimg_abort_test;
  39648. float _energy = 0;
  39649. if (is_3d) { // 3D version
  39650. if (smoothness>=0) // Isotropic regularization
  39651. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39652. cimg_openmp_if(_height*_depth>=(cimg_openmp_sizefactor)*8 &&
  39653. _width>=(cimg_openmp_sizefactor)*16)
  39654. reduction(+:_energy))
  39655. cimg_forYZ(U,y,z) {
  39656. const int
  39657. _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y,
  39658. _p1z = z?z - 1:0, _n1z = z<U.depth() - 1?z + 1:z;
  39659. cimg_for3X(U,x) {
  39660. const float
  39661. X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0),
  39662. Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1),
  39663. Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2);
  39664. float delta_I = 0, _energy_regul = 0;
  39665. if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c));
  39666. else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2._linear_atXYZ(X,Y,Z,c));
  39667. cimg_forC(U,c) {
  39668. const float
  39669. Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)),
  39670. Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)),
  39671. Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)),
  39672. Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c),
  39673. Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c),
  39674. Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c);
  39675. U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c]._linear_atXYZ(X,Y,Z) +
  39676. smoothness* ( Uxx + Uyy + Uzz)))/(1 + 6*smoothness*dt);
  39677. _energy_regul+=Ux*Ux + Uy*Uy + Uz*Uz;
  39678. }
  39679. if (is_backward) { // Constraint displacement vectors to stay in image
  39680. if (U(x,y,z,0)>x) U(x,y,z,0) = (float)x;
  39681. if (U(x,y,z,1)>y) U(x,y,z,1) = (float)y;
  39682. if (U(x,y,z,2)>z) U(x,y,z,2) = (float)z;
  39683. bound = (float)x - _width; if (U(x,y,z,0)<=bound) U(x,y,z,0) = bound;
  39684. bound = (float)y - _height; if (U(x,y,z,1)<=bound) U(x,y,z,1) = bound;
  39685. bound = (float)z - _depth; if (U(x,y,z,2)<=bound) U(x,y,z,2) = bound;
  39686. } else {
  39687. if (U(x,y,z,0)<-x) U(x,y,z,0) = -(float)x;
  39688. if (U(x,y,z,1)<-y) U(x,y,z,1) = -(float)y;
  39689. if (U(x,y,z,2)<-z) U(x,y,z,2) = -(float)z;
  39690. bound = (float)_width - x; if (U(x,y,z,0)>=bound) U(x,y,z,0) = bound;
  39691. bound = (float)_height - y; if (U(x,y,z,1)>=bound) U(x,y,z,1) = bound;
  39692. bound = (float)_depth - z; if (U(x,y,z,2)>=bound) U(x,y,z,2) = bound;
  39693. }
  39694. _energy+=delta_I*delta_I + smoothness*_energy_regul;
  39695. }
  39696. if (V) cimg_forXYZ(V,_x,_y,_z) if (V(_x,_y,_z,3)) { // Apply constraints
  39697. U(_x,_y,_z,0) = V(_x,_y,_z,0)/factor;
  39698. U(_x,_y,_z,1) = V(_x,_y,_z,1)/factor;
  39699. U(_x,_y,_z,2) = V(_x,_y,_z,2)/factor;
  39700. }
  39701. } else { // Anisotropic regularization
  39702. const float nsmoothness = -smoothness;
  39703. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  39704. cimg_openmp_if(_height*_depth>=(cimg_openmp_sizefactor)*8 &&
  39705. _width>=(cimg_openmp_sizefactor)*16)
  39706. reduction(+:_energy))
  39707. cimg_forYZ(U,y,z) {
  39708. const int
  39709. _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y,
  39710. _p1z = z?z - 1:0, _n1z = z<U.depth() - 1?z + 1:z;
  39711. cimg_for3X(U,x) {
  39712. const float
  39713. X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0),
  39714. Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1),
  39715. Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2);
  39716. float delta_I = 0, _energy_regul = 0;
  39717. if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c));
  39718. else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2._linear_atXYZ(X,Y,Z,c));
  39719. cimg_forC(U,c) {
  39720. const float
  39721. Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)),
  39722. Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)),
  39723. Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)),
  39724. N2 = Ux*Ux + Uy*Uy + Uz*Uz,
  39725. N = std::sqrt(N2),
  39726. N3 = 1e-5f + N2*N,
  39727. coef_a = (1 - Ux*Ux/N2)/N,
  39728. coef_b = -2*Ux*Uy/N3,
  39729. coef_c = -2*Ux*Uz/N3,
  39730. coef_d = (1 - Uy*Uy/N2)/N,
  39731. coef_e = -2*Uy*Uz/N3,
  39732. coef_f = (1 - Uz*Uz/N2)/N,
  39733. Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c),
  39734. Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c),
  39735. Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c),
  39736. Uxy = 0.25f*(U(_n1x,_n1y,z,c) + U(_p1x,_p1y,z,c) - U(_n1x,_p1y,z,c) - U(_n1x,_p1y,z,c)),
  39737. Uxz = 0.25f*(U(_n1x,y,_n1z,c) + U(_p1x,y,_p1z,c) - U(_n1x,y,_p1z,c) - U(_n1x,y,_p1z,c)),
  39738. Uyz = 0.25f*(U(x,_n1y,_n1z,c) + U(x,_p1y,_p1z,c) - U(x,_n1y,_p1z,c) - U(x,_n1y,_p1z,c));
  39739. U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c]._linear_atXYZ(X,Y,Z) +
  39740. nsmoothness* ( coef_a*Uxx + coef_b*Uxy +
  39741. coef_c*Uxz + coef_d*Uyy +
  39742. coef_e*Uyz + coef_f*Uzz ))
  39743. )/(1 + 2*(coef_a + coef_d + coef_f)*nsmoothness*dt);
  39744. _energy_regul+=N;
  39745. }
  39746. if (is_backward) { // Constraint displacement vectors to stay in image
  39747. if (U(x,y,z,0)>x) U(x,y,z,0) = (float)x;
  39748. if (U(x,y,z,1)>y) U(x,y,z,1) = (float)y;
  39749. if (U(x,y,z,2)>z) U(x,y,z,2) = (float)z;
  39750. bound = (float)x - _width; if (U(x,y,z,0)<=bound) U(x,y,z,0) = bound;
  39751. bound = (float)y - _height; if (U(x,y,z,1)<=bound) U(x,y,z,1) = bound;
  39752. bound = (float)z - _depth; if (U(x,y,z,2)<=bound) U(x,y,z,2) = bound;
  39753. } else {
  39754. if (U(x,y,z,0)<-x) U(x,y,z,0) = -(float)x;
  39755. if (U(x,y,z,1)<-y) U(x,y,z,1) = -(float)y;
  39756. if (U(x,y,z,2)<-z) U(x,y,z,2) = -(float)z;
  39757. bound = (float)_width - x; if (U(x,y,z,0)>=bound) U(x,y,z,0) = bound;
  39758. bound = (float)_height - y; if (U(x,y,z,1)>=bound) U(x,y,z,1) = bound;
  39759. bound = (float)_depth - z; if (U(x,y,z,2)>=bound) U(x,y,z,2) = bound;
  39760. }
  39761. _energy+=delta_I*delta_I + nsmoothness*_energy_regul;
  39762. }
  39763. if (V) cimg_forXYZ(V,_x,_y,_z) if (V(_x,_y,_z,3)) { // Apply constraints
  39764. U(_x,_y,_z,0) = V(_x,_y,_z,0)/factor;
  39765. U(_x,_y,_z,1) = V(_x,_y,_z,1)/factor;
  39766. U(_x,_y,_z,2) = V(_x,_y,_z,2)/factor;
  39767. }
  39768. }
  39769. }
  39770. } else { // 2D version
  39771. if (smoothness>=0) // Isotropic regularization
  39772. cimg_pragma_openmp(parallel for cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*8 &&
  39773. _width>=(cimg_openmp_sizefactor)*16) reduction(+:_energy))
  39774. cimg_forY(U,y) {
  39775. const int _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y;
  39776. cimg_for3X(U,x) {
  39777. const float
  39778. X = is_backward?x - U(x,y,0):x + U(x,y,0),
  39779. Y = is_backward?y - U(x,y,1):y + U(x,y,1);
  39780. float delta_I = 0, _energy_regul = 0;
  39781. if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXY(X,Y,c) - I2(x,y,c));
  39782. else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2._linear_atXY(X,Y,c));
  39783. cimg_forC(U,c) {
  39784. const float
  39785. Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)),
  39786. Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)),
  39787. Uxx = U(_n1x,y,c) + U(_p1x,y,c),
  39788. Uyy = U(x,_n1y,c) + U(x,_p1y,c);
  39789. U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c]._linear_atXY(X,Y) +
  39790. smoothness*( Uxx + Uyy )))/(1 + 4*smoothness*dt);
  39791. _energy_regul+=Ux*Ux + Uy*Uy;
  39792. }
  39793. if (is_backward) { // Constraint displacement vectors to stay in image
  39794. if (U(x,y,0)>x) U(x,y,0) = (float)x;
  39795. if (U(x,y,1)>y) U(x,y,1) = (float)y;
  39796. bound = (float)x - _width; if (U(x,y,0)<=bound) U(x,y,0) = bound;
  39797. bound = (float)y - _height; if (U(x,y,1)<=bound) U(x,y,1) = bound;
  39798. } else {
  39799. if (U(x,y,0)<-x) U(x,y,0) = -(float)x;
  39800. if (U(x,y,1)<-y) U(x,y,1) = -(float)y;
  39801. bound = (float)_width - x; if (U(x,y,0)>=bound) U(x,y,0) = bound;
  39802. bound = (float)_height - y; if (U(x,y,1)>=bound) U(x,y,1) = bound;
  39803. }
  39804. _energy+=delta_I*delta_I + smoothness*_energy_regul;
  39805. }
  39806. if (V) cimg_forXY(V,_x,_y) if (V(_x,_y,2)) { // Apply constraints
  39807. U(_x,_y,0) = V(_x,_y,0)/factor;
  39808. U(_x,_y,1) = V(_x,_y,1)/factor;
  39809. }
  39810. } else { // Anisotropic regularization
  39811. const float nsmoothness = -smoothness;
  39812. cimg_pragma_openmp(parallel for cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*8 &&
  39813. _width>=(cimg_openmp_sizefactor)*16) reduction(+:_energy))
  39814. cimg_forY(U,y) {
  39815. const int _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y;
  39816. cimg_for3X(U,x) {
  39817. const float
  39818. X = is_backward?x - U(x,y,0):x + U(x,y,0),
  39819. Y = is_backward?y - U(x,y,1):y + U(x,y,1);
  39820. float delta_I = 0, _energy_regul = 0;
  39821. if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXY(X,Y,c) - I2(x,y,c));
  39822. else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2._linear_atXY(X,Y,c));
  39823. cimg_forC(U,c) {
  39824. const float
  39825. Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)),
  39826. Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)),
  39827. N2 = Ux*Ux + Uy*Uy,
  39828. N = std::sqrt(N2),
  39829. N3 = 1e-5f + N2*N,
  39830. coef_a = Uy*Uy/N3,
  39831. coef_b = -2*Ux*Uy/N3,
  39832. coef_c = Ux*Ux/N3,
  39833. Uxx = U(_n1x,y,c) + U(_p1x,y,c),
  39834. Uyy = U(x,_n1y,c) + U(x,_p1y,c),
  39835. Uxy = 0.25f*(U(_n1x,_n1y,c) + U(_p1x,_p1y,c) - U(_n1x,_p1y,c) - U(_n1x,_p1y,c));
  39836. U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c]._linear_atXY(X,Y) +
  39837. nsmoothness*( coef_a*Uxx + coef_b*Uxy + coef_c*Uyy )))/
  39838. (1 + 2*(coef_a + coef_c)*nsmoothness*dt);
  39839. _energy_regul+=N;
  39840. }
  39841. if (is_backward) { // Constraint displacement vectors to stay in image
  39842. if (U(x,y,0)>x) U(x,y,0) = (float)x;
  39843. if (U(x,y,1)>y) U(x,y,1) = (float)y;
  39844. bound = (float)x - _width; if (U(x,y,0)<=bound) U(x,y,0) = bound;
  39845. bound = (float)y - _height; if (U(x,y,1)<=bound) U(x,y,1) = bound;
  39846. } else {
  39847. if (U(x,y,0)<-x) U(x,y,0) = -(float)x;
  39848. if (U(x,y,1)<-y) U(x,y,1) = -(float)y;
  39849. bound = (float)_width - x; if (U(x,y,0)>=bound) U(x,y,0) = bound;
  39850. bound = (float)_height - y; if (U(x,y,1)>=bound) U(x,y,1) = bound;
  39851. }
  39852. _energy+=delta_I*delta_I + nsmoothness*_energy_regul;
  39853. }
  39854. if (V) cimg_forXY(V,_x,_y) if (V(_x,_y,2)) { // Apply constraints
  39855. U(_x,_y,0) = V(_x,_y,0)/factor;
  39856. U(_x,_y,1) = V(_x,_y,1)/factor;
  39857. }
  39858. }
  39859. }
  39860. }
  39861. const float d_energy = (_energy - energy)/(sw*sh*sd);
  39862. if (d_energy<=0 && -d_energy<_precision) break;
  39863. if (d_energy>0) dt*=0.5f;
  39864. energy = _energy;
  39865. }
  39866. }
  39867. return U;
  39868. }
  39869. //! Compute correspondence map between two images, using a patch-matching algorithm.
  39870. /**
  39871. \param patch_image The image containing the reference patches to match with the instance image.
  39872. \param patch_width Width of the patch used for matching.
  39873. \param patch_height Height of the patch used for matching.
  39874. \param patch_depth Depth of the patch used for matching.
  39875. \param nb_iterations Number of patch-match iterations.
  39876. \param nb_randoms Number of randomization attempts (per pixel).
  39877. \param patch_penalization Penalization factor in score related patch occurrences.
  39878. if negative, also tells that identity result is not avoided.
  39879. \param guide Image used as the initial correspondence estimate for the algorithm.
  39880. 'guide' may have a last channel with boolean values (0=false | other=true) that
  39881. tells for each pixel if its correspondence vector is constrained to its initial value (constraint mask).
  39882. \param[out] matching_score Returned as the image of matching scores.
  39883. **/
  39884. template<typename t1, typename t2>
  39885. CImg<T>& matchpatch(const CImg<T>& patch_image,
  39886. const unsigned int patch_width,
  39887. const unsigned int patch_height,
  39888. const unsigned int patch_depth,
  39889. const unsigned int nb_iterations,
  39890. const unsigned int nb_randoms,
  39891. const float patch_penalization,
  39892. const CImg<t1> &guide,
  39893. CImg<t2> &matching_score) {
  39894. return get_matchpatch(patch_image,patch_width,patch_height,patch_depth,
  39895. nb_iterations,nb_randoms,patch_penalization,guide,matching_score).move_to(*this);
  39896. }
  39897. //! Compute correspondence map between two images, using the patch-match algorithm \newinstance.
  39898. template<typename t1, typename t2>
  39899. CImg<intT> get_matchpatch(const CImg<T>& patch_image,
  39900. const unsigned int patch_width,
  39901. const unsigned int patch_height,
  39902. const unsigned int patch_depth,
  39903. const unsigned int nb_iterations,
  39904. const unsigned int nb_randoms,
  39905. const float patch_penalization,
  39906. const CImg<t1> &guide,
  39907. CImg<t2> &matching_score) const {
  39908. return _matchpatch(patch_image,patch_width,patch_height,patch_depth,
  39909. nb_iterations,nb_randoms,patch_penalization,
  39910. guide,true,matching_score);
  39911. }
  39912. //! Compute correspondence map between two images, using the patch-match algorithm \overloading.
  39913. template<typename t>
  39914. CImg<T>& matchpatch(const CImg<T>& patch_image,
  39915. const unsigned int patch_width,
  39916. const unsigned int patch_height,
  39917. const unsigned int patch_depth,
  39918. const unsigned int nb_iterations=5,
  39919. const unsigned int nb_randoms=5,
  39920. const float patch_penalization=0,
  39921. const CImg<t> &guide=CImg<t>::const_empty()) {
  39922. return get_matchpatch(patch_image,patch_width,patch_height,patch_depth,
  39923. nb_iterations,nb_randoms,patch_penalization,guide).move_to(*this);
  39924. }
  39925. //! Compute correspondence map between two images, using the patch-match algorithm \overloading.
  39926. template<typename t>
  39927. CImg<intT> get_matchpatch(const CImg<T>& patch_image,
  39928. const unsigned int patch_width,
  39929. const unsigned int patch_height,
  39930. const unsigned int patch_depth,
  39931. const unsigned int nb_iterations=5,
  39932. const unsigned int nb_randoms=5,
  39933. const float patch_penalization=0,
  39934. const CImg<t> &guide=CImg<t>::const_empty()) const {
  39935. CImg<T> matching_score;
  39936. return _matchpatch(patch_image,patch_width,patch_height,patch_depth,
  39937. nb_iterations,nb_randoms,patch_penalization,guide,false,matching_score);
  39938. }
  39939. template<typename t1, typename t2>
  39940. CImg<intT> _matchpatch(const CImg<T>& patch_image,
  39941. const unsigned int patch_width,
  39942. const unsigned int patch_height,
  39943. const unsigned int patch_depth,
  39944. const unsigned int nb_iterations,
  39945. const unsigned int nb_randoms,
  39946. const float patch_penalization,
  39947. const CImg<t1> &guide,
  39948. const bool is_matching_score,
  39949. CImg<t2> &matching_score) const {
  39950. if (is_empty()) return CImg<intT>::const_empty();
  39951. if (patch_image._spectrum!=_spectrum)
  39952. throw CImgArgumentException(_cimg_instance
  39953. "matchpatch(): Instance image and specified patch image (%u,%u,%u,%u,%p) "
  39954. "have different spectrums.",
  39955. cimg_instance,
  39956. patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
  39957. patch_image._data);
  39958. if (patch_width>_width || patch_height>_height || patch_depth>_depth)
  39959. throw CImgArgumentException(_cimg_instance
  39960. "matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions "
  39961. "of the instance image.",
  39962. cimg_instance,patch_width,patch_height,patch_depth);
  39963. if (patch_width>patch_image._width || patch_height>patch_image._height || patch_depth>patch_image._depth)
  39964. throw CImgArgumentException(_cimg_instance
  39965. "matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions "
  39966. "of the patch image image (%u,%u,%u,%u,%p).",
  39967. cimg_instance,patch_width,patch_height,patch_depth,
  39968. patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
  39969. patch_image._data);
  39970. const unsigned int
  39971. _constraint = patch_image._depth>1?3:2,
  39972. constraint = guide._spectrum>_constraint?_constraint:0;
  39973. if (guide &&
  39974. (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<_constraint))
  39975. throw CImgArgumentException(_cimg_instance
  39976. "matchpatch(): Specified guide (%u,%u,%u,%u,%p) has invalid dimensions "
  39977. "considering instance and patch image (%u,%u,%u,%u,%p).",
  39978. cimg_instance,
  39979. guide._width,guide._height,guide._depth,guide._spectrum,guide._data,
  39980. patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
  39981. patch_image._data);
  39982. CImg<intT> a_map(_width,_height,_depth,patch_image._depth>1?3:2);
  39983. CImg<ucharT> is_updated(_width,_height,_depth,1,3);
  39984. CImg<floatT> score(_width,_height,_depth), penalty;
  39985. const float _patch_penalization = cimg::abs(patch_penalization);
  39986. const bool allow_identity = patch_penalization>=0;
  39987. if (_patch_penalization!=0)
  39988. penalty.assign(patch_image._width,patch_image._height,patch_image._depth,1,0);
  39989. const int
  39990. psizew = (int)patch_width, psizew1 = psizew/2, psizew2 = psizew - psizew1 - 1,
  39991. psizeh = (int)patch_height, psizeh1 = psizeh/2, psizeh2 = psizeh - psizeh1 - 1,
  39992. psized = (int)patch_depth, psized1 = psized/2, psized2 = psized - psized1 - 1;
  39993. // Interleave image buffers to speed up patch comparison (more cache-friendly).
  39994. CImg<T> in_this = get_permute_axes("cxyz");
  39995. in_this._width = _width*_spectrum;
  39996. in_this._height = _height;
  39997. in_this._depth = _depth;
  39998. in_this._spectrum = 1;
  39999. CImg<T> in_patch = patch_image.get_permute_axes("cxyz");
  40000. in_patch._width = patch_image._width*patch_image._spectrum;
  40001. in_patch._height = patch_image._height;
  40002. in_patch._depth = patch_image._depth;
  40003. in_patch._spectrum = 1;
  40004. if (_depth>1 || patch_image._depth>1) { // 3D version
  40005. // Initialize correspondence map.
  40006. if (guide)
  40007. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(_width,64))
  40008. cimg_forXYZ(*this,x,y,z) { // User-defined initialization
  40009. const int
  40010. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40011. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40012. cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
  40013. u = cimg::cut((int)guide(x,y,z,0),cx1,patch_image.width() - 1 - cx2),
  40014. v = cimg::cut((int)guide(x,y,z,1),cy1,patch_image.height() - 1 - cy2),
  40015. w = cimg::cut((int)guide(x,y,z,2),cz1,patch_image.depth() - 1 - cz2);
  40016. a_map(x,y,z,0) = u;
  40017. a_map(x,y,z,1) = v;
  40018. a_map(x,y,z,2) = w;
  40019. score(x,y,z) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40020. x - cx1,y - cy1,z - cz1,
  40021. u - cx1,v - cy1,w - cz1,
  40022. u,v,w,0,allow_identity,cimg::type<float>::inf());
  40023. } else cimg_pragma_openmp(parallel cimg_openmp_if_size(_width,64)) {
  40024. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  40025. #if cimg_use_openmp!=0
  40026. rng+=omp_get_thread_num();
  40027. #endif
  40028. cimg_pragma_openmp(for cimg_openmp_collapse(2))
  40029. cimg_forXYZ(*this,x,y,z) { // Random initialization
  40030. const int
  40031. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40032. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40033. cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
  40034. u = (int)cimg::round(cimg::rand(cx1,patch_image.width() - 1 - cx2,&rng)),
  40035. v = (int)cimg::round(cimg::rand(cy1,patch_image.height() - 1 - cy2,&rng)),
  40036. w = (int)cimg::round(cimg::rand(cz1,patch_image.depth() - 1 - cz2,&rng));
  40037. a_map(x,y,z,0) = u;
  40038. a_map(x,y,z,1) = v;
  40039. a_map(x,y,z,2) = w;
  40040. score(x,y,z) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40041. x - cx1,y - cy1,z - cz1,
  40042. u - cx1,v - cy1,w - cz1,
  40043. u,v,w,0,allow_identity,cimg::type<float>::inf());
  40044. }
  40045. cimg::srand(rng);
  40046. }
  40047. // Start iteration loop.
  40048. cimg_abort_init;
  40049. for (unsigned int iter = 0; iter<nb_iterations; ++iter) {
  40050. cimg_abort_test;
  40051. const bool is_backward = iter&1, is_forward = !is_backward;
  40052. const unsigned int cmask = is_backward?1:2, nmask = 3 - cmask;
  40053. cimg_pragma_openmp(parallel cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64)) {
  40054. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  40055. #if cimg_use_openmp!=0
  40056. rng+=omp_get_thread_num();
  40057. #endif
  40058. cimg_pragma_openmp(for cimg_openmp_collapse(2))
  40059. cimg_forXYZ(*this,X,Y,Z) {
  40060. const int
  40061. x = is_backward?width() - 1 - X:X,
  40062. y = is_backward?height() - 1 - Y:Y,
  40063. z = is_backward?depth() - 1 - Z:Z;
  40064. if (score(x,y,z)<=1e-5 || (constraint && guide(x,y,z,constraint)!=0)) continue;
  40065. const int
  40066. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40067. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40068. cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
  40069. xp = x - cx1,
  40070. yp = y - cy1,
  40071. zp = z - cz1;
  40072. int best_u = a_map(x,y,z,0), best_v = a_map(x,y,z,1), best_w = a_map(x,y,z,2), u, v, w;
  40073. const float best_score0 = score(x,y,z);
  40074. float best_score = best_score0, s;
  40075. if (is_forward && x>0 && (is_updated(x - 1,y,z)&cmask)) { // Compare with left neighbor
  40076. u = a_map(x - 1,y,z,0);
  40077. v = a_map(x - 1,y,z,1);
  40078. w = a_map(x - 1,y,z,2);
  40079. if (u>=cx1 - 1 && u<patch_image.width() - 1 - cx2 &&
  40080. v>=cy1 && v<patch_image.height() - cy2 &&
  40081. w>=cz1 && w<patch_image.depth() - cz2) {
  40082. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40083. xp,yp,zp,u + 1 - cx1,v - cy1,w - cz1,
  40084. u,v,w,_patch_penalization,allow_identity,best_score);
  40085. if (s<best_score) { best_u = u + 1; best_v = v; best_w = w; best_score = s; }
  40086. }
  40087. }
  40088. if (is_forward && y>0 && (is_updated(x,y - 1,z)&cmask)) { // Compare with up neighbor
  40089. u = a_map(x,y - 1,z,0);
  40090. v = a_map(x,y - 1,z,1);
  40091. w = a_map(x,y - 1,z,2);
  40092. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40093. v>=cy1 - 1 && v<patch_image.height() - 1 - cy2 &&
  40094. w>=cz1 && w<patch_image.depth() - cz2) {
  40095. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40096. xp,yp,zp,u - cx1,v + 1 - cy1,w - cz1,
  40097. u,v,w,_patch_penalization,allow_identity,best_score);
  40098. if (s<best_score) { best_u = u; best_v = v + 1; best_w = w; best_score = s; }
  40099. }
  40100. }
  40101. if (is_forward && z>0 && (is_updated(x,y,z - 1)&cmask)) { // Compare with backward neighbor
  40102. u = a_map(x,y,z - 1,0);
  40103. v = a_map(x,y,z - 1,1);
  40104. w = a_map(x,y,z - 1,2);
  40105. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40106. v>=cy1 && v<patch_image.height() - cy2 &&
  40107. w>=cz1 - 1 && w<patch_image.depth() - 1 - cz2) {
  40108. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40109. xp,yp,zp,u - cx1,v - cy1,w + 1 - cz1,
  40110. u,v,w,_patch_penalization,allow_identity,best_score);
  40111. if (s<best_score) { best_u = u; best_v = v; best_w = w + 1; best_score = s; }
  40112. }
  40113. }
  40114. if (is_backward && x<width() - 1 && (is_updated(x + 1,y,z)&cmask)) { // Compare with right neighbor
  40115. u = a_map(x + 1,y,z,0);
  40116. v = a_map(x + 1,y,z,1);
  40117. w = a_map(x + 1,y,z,2);
  40118. if (u>=cx1 + 1 && u<patch_image.width() + 1 - cx2 &&
  40119. v>=cy1 && v<patch_image.height() - cy2 &&
  40120. w>=cz1 && w<patch_image.depth() - cz2) {
  40121. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40122. xp,yp,zp,u - 1 - cx1,v - cy1,w - cz1,
  40123. u,v,w,_patch_penalization,allow_identity,best_score);
  40124. if (s<best_score) { best_u = u - 1; best_v = v; best_w = w; best_score = s; }
  40125. }
  40126. }
  40127. if (is_backward && y<height() - 1 && (is_updated(x,y + 1,z)&cmask)) { // Compare with bottom neighbor
  40128. u = a_map(x,y + 1,z,0);
  40129. v = a_map(x,y + 1,z,1);
  40130. w = a_map(x,y + 1,z,2);
  40131. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40132. v>=cy1 + 1 && v<patch_image.height() + 1 - cy2 &&
  40133. w>=cz1 && w<patch_image.depth() - cz2) {
  40134. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40135. xp,yp,zp,u - cx1,v - 1 - cy1,w - cz1,
  40136. u,v,w,_patch_penalization,allow_identity,best_score);
  40137. if (s<best_score) { best_u = u; best_v = v - 1; best_w = w; best_score = s; }
  40138. }
  40139. }
  40140. if (is_backward && z<depth() - 1 && (is_updated(x,y,z + 1)&cmask)) { // Compare with forward neighbor
  40141. u = a_map(x,y,z + 1,0);
  40142. v = a_map(x,y,z + 1,1);
  40143. w = a_map(x,y,z + 1,2);
  40144. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40145. v>=cy1 && v<patch_image.height() - cy2 &&
  40146. w>=cz1 + 1 && w<patch_image.depth() + 1 - cz2) {
  40147. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40148. xp,yp,zp,u - cx1,v - cy1,w - 1 - cz1,
  40149. u,v,w,_patch_penalization,allow_identity,best_score);
  40150. if (s<best_score) { best_u = u; best_v = v; best_w = w - 1; best_score = s; }
  40151. }
  40152. }
  40153. float
  40154. dw = (float)patch_image.width(),
  40155. dh = (float)patch_image.height(),
  40156. dd = (float)patch_image.depth();
  40157. for (unsigned int i = 0; i<nb_randoms; ++i) {
  40158. u = (int)cimg::round(cimg::rand(std::max((float)cx1,best_u - dw),
  40159. std::min(patch_image.width() - 1.f - cx2,best_u + dw),&rng));
  40160. v = (int)cimg::round(cimg::rand(std::max((float)cy1,best_v - dh),
  40161. std::min(patch_image.height() - 1.f - cy2,best_v + dh),&rng));
  40162. w = (int)cimg::round(cimg::rand(std::max((float)cz1,best_w - dd),
  40163. std::min(patch_image.depth() - 1.f - cz2,best_w + dd),&rng));
  40164. if (u!=best_u || v!=best_v || w!=best_w) {
  40165. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40166. xp,yp,zp,u - cx1,v - cy1,w - cz1,
  40167. u,v,w,_patch_penalization,allow_identity,best_score);
  40168. if (s<best_score) { best_u = u; best_v = v; best_w = w; best_score = s; }
  40169. dw = std::max(5.f,dw*0.5f); dh = std::max(5.f,dh*0.5f); dd = std::max(5.f,dd*0.5f);
  40170. }
  40171. }
  40172. if (best_score<best_score0) {
  40173. if (_patch_penalization!=0) {
  40174. float &p_penalty = penalty(a_map(x,y,z,0),a_map(x,y,z,1),a_map(x,y,z,2));
  40175. if (p_penalty) cimg_pragma_openmp(atomic) --p_penalty;
  40176. }
  40177. a_map(x,y,z,0) = best_u;
  40178. a_map(x,y,z,1) = best_v;
  40179. a_map(x,y,z,2) = best_w;
  40180. score(x,y,z) = best_score;
  40181. is_updated(x,y,z) = 3;
  40182. } else is_updated(x,y,z)&=~nmask;
  40183. if (_patch_penalization!=0) cimg_pragma_openmp(atomic) ++penalty(best_u,best_v,best_w);
  40184. }
  40185. cimg::srand(rng);
  40186. }
  40187. // Update score according to new penalties.
  40188. if (penalty)
  40189. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64))
  40190. cimg_forXYZ(score,x,y,z) {
  40191. const float p_score = score(x,y,z);
  40192. const int
  40193. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()),
  40194. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()),
  40195. cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()),
  40196. xp = x - cx1,
  40197. yp = y - cy1,
  40198. zp = z - cz1,
  40199. u = a_map(x,y,z,0),
  40200. v = a_map(x,y,z,1),
  40201. w = a_map(x,y,z,2);
  40202. const float n_score = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
  40203. xp,yp,zp,u - cx1,v - cy1,w - cz1,
  40204. u,v,w,_patch_penalization,allow_identity,cimg::type<float>::inf());
  40205. if (n_score!=p_score) { score(x,y,z) = n_score; is_updated(x,y) = 3; }
  40206. }
  40207. }
  40208. } else { // 2D version
  40209. // Initialize correspondence map.
  40210. if (guide)
  40211. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,64))
  40212. cimg_forXY(*this,x,y) { // User-defined initialization
  40213. const int
  40214. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40215. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40216. u = cimg::cut((int)guide(x,y,0),cx1,patch_image.width() - 1 - cx2),
  40217. v = cimg::cut((int)guide(x,y,1),cy1,patch_image.height() - 1 - cy2);
  40218. a_map(x,y,0) = u;
  40219. a_map(x,y,1) = v;
  40220. score(x,y) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40221. x - cx1,y - cy1,u - cx1,v - cy1,
  40222. u,v,0,allow_identity,cimg::type<float>::inf());
  40223. } else cimg_pragma_openmp(parallel cimg_openmp_if_size(_width,64)) {
  40224. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  40225. #if cimg_use_openmp!=0
  40226. rng+=omp_get_thread_num();
  40227. #endif
  40228. cimg_pragma_openmp(for)
  40229. cimg_forXY(*this,x,y) { // Random initialization
  40230. const int
  40231. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40232. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40233. u = (int)cimg::round(cimg::rand(cx1,patch_image.width() - 1 - cx2,&rng)),
  40234. v = (int)cimg::round(cimg::rand(cy1,patch_image.height() - 1 - cy2,&rng));
  40235. a_map(x,y,0) = u;
  40236. a_map(x,y,1) = v;
  40237. score(x,y) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40238. x - cx1,y - cy1,u - cx1,v - cy1,
  40239. u,v,0,allow_identity,cimg::type<float>::inf());
  40240. }
  40241. cimg::srand(rng);
  40242. }
  40243. // Start iteration loop.
  40244. cimg_abort_init;
  40245. for (unsigned int iter = 0; iter<nb_iterations; ++iter) {
  40246. cimg_abort_test;
  40247. const bool is_backward = iter&1, is_forward = !is_backward;
  40248. const unsigned int cmask = is_backward?1:2, nmask = 3 - cmask;
  40249. cimg_pragma_openmp(parallel cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64)) {
  40250. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  40251. #if cimg_use_openmp!=0
  40252. rng+=omp_get_thread_num();
  40253. #endif
  40254. cimg_pragma_openmp(for)
  40255. cimg_forXY(*this,X,Y) {
  40256. const int
  40257. x = is_backward?width() - 1 - X:X,
  40258. y = is_backward?height() - 1 - Y:Y;
  40259. if (score(x,y)<=1e-5 || (constraint && guide(x,y,constraint)!=0)) continue;
  40260. const int
  40261. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
  40262. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
  40263. xp = x - cx1,
  40264. yp = y - cy1;
  40265. int best_u = a_map(x,y,0), best_v = a_map(x,y,1), u, v;
  40266. const float best_score0 = score(x,y);
  40267. float best_score = best_score0, s;
  40268. if (is_forward && x>0 && (is_updated(x - 1,y)&cmask)) { // Compare with left neighbor
  40269. u = a_map(x - 1,y,0);
  40270. v = a_map(x - 1,y,1);
  40271. if (u>=cx1 - 1 && u<patch_image.width() - 1 - cx2 &&
  40272. v>=cy1 && v<patch_image.height() - cy2) {
  40273. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40274. xp,yp,u + 1 - cx1,v - cy1,
  40275. u,v,_patch_penalization,allow_identity,best_score);
  40276. if (s<best_score) { best_u = u + 1; best_v = v; best_score = s; }
  40277. }
  40278. }
  40279. if (is_forward && y>0 && (is_updated(x,y - 1)&cmask)) { // Compare with up neighbor
  40280. u = a_map(x,y - 1,0);
  40281. v = a_map(x,y - 1,1);
  40282. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40283. v>=cy1 - 1 && v<patch_image.height() - 1 - cy2) {
  40284. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40285. xp,yp,u - cx1,v + 1 - cy1,
  40286. u,v,_patch_penalization,allow_identity,best_score);
  40287. if (s<best_score) { best_u = u; best_v = v + 1; best_score = s; }
  40288. }
  40289. }
  40290. if (is_backward && x<width() - 1 && (is_updated(x + 1,y)&cmask)) { // Compare with right neighbor
  40291. u = a_map(x + 1,y,0);
  40292. v = a_map(x + 1,y,1);
  40293. if (u>=cx1 + 1 && u<patch_image.width() + 1 - cx2 &&
  40294. v>=cy1 && v<patch_image.height() - cy2) {
  40295. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40296. xp,yp,u - 1 - cx1,v - cy1,
  40297. u,v,_patch_penalization,allow_identity,best_score);
  40298. if (s<best_score) { best_u = u - 1; best_v = v; best_score = s; }
  40299. }
  40300. }
  40301. if (is_backward && y<height() - 1 && (is_updated(x,y + 1)&cmask)) { // Compare with bottom neighbor
  40302. u = a_map(x,y + 1,0);
  40303. v = a_map(x,y + 1,1);
  40304. if (u>=cx1 && u<patch_image.width() - cx2 &&
  40305. v>=cy1 + 1 && v<patch_image.height() + 1 - cy2) {
  40306. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40307. xp,yp,u - cx1,v - 1 - cy1,
  40308. u,v,_patch_penalization,allow_identity,best_score);
  40309. if (s<best_score) { best_u = u; best_v = v - 1; best_score = s; }
  40310. }
  40311. }
  40312. float
  40313. dw = (float)patch_image.width(),
  40314. dh = (float)patch_image.height();
  40315. for (unsigned int i = 0; i<nb_randoms; ++i) {
  40316. u = (int)cimg::round(cimg::rand(std::max((float)cx1,best_u - dw),
  40317. std::min(patch_image.width() - 1.f - cx2,best_u + dw),&rng));
  40318. v = (int)cimg::round(cimg::rand(std::max((float)cy1,best_v - dh),
  40319. std::min(patch_image.height() - 1.f - cy2,best_v + dh),&rng));
  40320. if (u!=best_u || v!=best_v) {
  40321. s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40322. xp,yp,u - cx1,v - cy1,
  40323. u,v,_patch_penalization,allow_identity,best_score);
  40324. if (s<best_score) { best_u = u; best_v = v; best_score = s; }
  40325. dw = std::max(5.f,dw*0.5f); dh = std::max(5.f,dh*0.5f);
  40326. }
  40327. }
  40328. if (best_score<best_score0) {
  40329. if (_patch_penalization!=0) {
  40330. float &p_penalty = penalty(a_map(x,y,0),a_map(x,y,1));
  40331. if (p_penalty) cimg_pragma_openmp(atomic) --p_penalty;
  40332. }
  40333. a_map(x,y,0) = best_u;
  40334. a_map(x,y,1) = best_v;
  40335. score(x,y) = best_score;
  40336. is_updated(x,y) = 3;
  40337. } else is_updated(x,y)&=~nmask;
  40338. if (_patch_penalization!=0) cimg_pragma_openmp(atomic) ++penalty(best_u,best_v);
  40339. }
  40340. cimg::srand(rng);
  40341. }
  40342. // Update score according to new penalties.
  40343. if (penalty)
  40344. cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64))
  40345. cimg_forXY(score,x,y) {
  40346. const float p_score = score(x,y);
  40347. const int
  40348. cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()),
  40349. cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()),
  40350. xp = x - cx1,
  40351. yp = y - cy1,
  40352. u = a_map(x,y,0),
  40353. v = a_map(x,y,1);
  40354. const float n_score = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
  40355. xp,yp,u - cx1,v - cy1,
  40356. u,v,_patch_penalization,allow_identity,cimg::type<float>::inf());
  40357. if (n_score!=p_score) { score(x,y) = n_score; is_updated(x,y) = 3; }
  40358. }
  40359. }
  40360. }
  40361. if (is_matching_score) score.move_to(matching_score);
  40362. return a_map;
  40363. }
  40364. // Compute SSD between two patches in different images.
  40365. static float _matchpatch(const CImg<T>& img1, const CImg<T>& img2, const CImg<floatT>& penalty,
  40366. const unsigned int psizew, const unsigned int psizeh,
  40367. const unsigned int psized, const unsigned int psizec,
  40368. const int x1, const int y1, const int z1,
  40369. const int x2, const int y2, const int z2,
  40370. const int xc, const int yc, const int zc,
  40371. const float patch_penalization,
  40372. const bool allow_identity,
  40373. const float max_score) { // 3D version
  40374. if (!allow_identity && cimg::hypot((float)x1 - x2,(float)y1 - y2,(float)z1 - z2)<patch_penalization)
  40375. return cimg::type<float>::inf();
  40376. const T *p1 = img1.data(x1*psizec,y1,z1), *p2 = img2.data(x2*psizec,y2,z2);
  40377. const unsigned int psizewc = psizew*psizec;
  40378. const ulongT
  40379. offx1 = (ulongT)img1._width - psizewc,
  40380. offx2 = (ulongT)img2._width - psizewc,
  40381. offy1 = (ulongT)img1._width*img1._height - (ulongT)psizeh*img1._width,
  40382. offy2 = (ulongT)img2._width*img2._height - (ulongT)psizeh*img2._width;
  40383. float ssd = 0;
  40384. for (unsigned int k = 0; k<psized; ++k) {
  40385. for (unsigned int j = 0; j<psizeh; ++j) {
  40386. for (unsigned int i = 0; i<psizewc; ++i)
  40387. ssd += cimg::sqr((Tfloat)*(p1++) - *(p2++));
  40388. if (ssd>max_score) return max_score;
  40389. p1+=offx1; p2+=offx2;
  40390. }
  40391. p1+=offy1; p2+=offy2;
  40392. }
  40393. return patch_penalization==0?ssd:cimg::sqr(std::sqrt(ssd) +
  40394. patch_penalization*psizewc*psizeh*psized*penalty(xc,yc,zc)/100);
  40395. }
  40396. static float _matchpatch(const CImg<T>& img1, const CImg<T>& img2, const CImg<floatT>& penalty,
  40397. const unsigned int psizew, const unsigned int psizeh, const unsigned int psizec,
  40398. const int x1, const int y1,
  40399. const int x2, const int y2,
  40400. const int xc, const int yc,
  40401. const float patch_penalization,
  40402. const bool allow_identity,
  40403. const float max_score) { // 2D version
  40404. if (!allow_identity && cimg::hypot((float)x1-x2,(float)y1-y2)<patch_penalization)
  40405. return cimg::type<float>::inf();
  40406. const T *p1 = img1.data(x1*psizec,y1), *p2 = img2.data(x2*psizec,y2);
  40407. const unsigned int psizewc = psizew*psizec;
  40408. const ulongT
  40409. offx1 = (ulongT)img1._width - psizewc,
  40410. offx2 = (ulongT)img2._width - psizewc;
  40411. float ssd = 0;
  40412. for (unsigned int j = 0; j<psizeh; ++j) {
  40413. for (unsigned int i = 0; i<psizewc; ++i)
  40414. ssd += cimg::sqr((Tfloat)*(p1++) - *(p2++));
  40415. if (ssd>max_score) return max_score;
  40416. p1+=offx1; p2+=offx2;
  40417. }
  40418. return patch_penalization==0?ssd:cimg::sqr(std::sqrt(ssd) +
  40419. patch_penalization*psizewc*psizeh*penalty(xc,yc)/100);
  40420. }
  40421. //! Compute Euclidean distance function to a specified value.
  40422. /**
  40423. \param value Reference value.
  40424. \param metric Type of metric. Can be <tt>{ 0=Chebyshev | 1=Manhattan | 2=Euclidean | 3=Squared-euclidean }</tt>.
  40425. \note
  40426. The distance transform implementation has been submitted by A. Meijster, and implements
  40427. the article 'W.H. Hesselink, A. Meijster, J.B.T.M. Roerdink,
  40428. "A general algorithm for computing distance transforms in linear time.",
  40429. In: Mathematical Morphology and its Applications to Image and Signal Processing,
  40430. J. Goutsias, L. Vincent, and D.S. Bloomberg (eds.), Kluwer, 2000, pp. 331-340.'
  40431. The submitted code has then been modified to fit CImg coding style and constraints.
  40432. **/
  40433. CImg<T>& distance(const T& value, const unsigned int metric=2) {
  40434. if (is_empty()) return *this;
  40435. if (cimg::type<Tint>::string()!=pixel_type()) // For datatype < int
  40436. return CImg<Tint>(*this,false).distance((Tint)value,metric).
  40437. cut((Tint)cimg::type<T>::min(),(Tint)cimg::type<T>::max()).move_to(*this);
  40438. bool is_value = false;
  40439. cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,(T)0:(T)std::max(0,99999999); // (avoid VC++ warning)
  40440. if (!is_value) return fill(cimg::type<T>::max());
  40441. switch (metric) {
  40442. case 0 : return _distance_core(_distance_sep_cdt,_distance_dist_cdt); // Chebyshev
  40443. case 1 : return _distance_core(_distance_sep_mdt,_distance_dist_mdt); // Manhattan
  40444. case 3 : return _distance_core(_distance_sep_edt,_distance_dist_edt); // Squared Euclidean
  40445. default : return _distance_core(_distance_sep_edt,_distance_dist_edt).sqrt(); // Euclidean
  40446. }
  40447. return *this;
  40448. }
  40449. //! Compute distance to a specified value \newinstance.
  40450. CImg<Tfloat> get_distance(const T& value, const unsigned int metric=2) const {
  40451. return CImg<Tfloat>(*this,false).distance((Tfloat)value,metric);
  40452. }
  40453. static longT _distance_sep_edt(const longT i, const longT u, const longT *const g) {
  40454. return (u*u - i*i + g[u] - g[i])/(2*(u - i));
  40455. }
  40456. static longT _distance_dist_edt(const longT x, const longT i, const longT *const g) {
  40457. return (x - i)*(x - i) + g[i];
  40458. }
  40459. static longT _distance_sep_mdt(const longT i, const longT u, const longT *const g) {
  40460. return (u - i<=g[u] - g[i]?999999999:(g[u] - g[i] + u + i)/2);
  40461. }
  40462. static longT _distance_dist_mdt(const longT x, const longT i, const longT *const g) {
  40463. return (x<i?i - x:x - i) + g[i];
  40464. }
  40465. static longT _distance_sep_cdt(const longT i, const longT u, const longT *const g) {
  40466. const longT h = (i + u)/2;
  40467. if (g[i]<=g[u]) { return h<i + g[u]?i + g[u]:h; }
  40468. return h<u - g[i]?h:u - g[i];
  40469. }
  40470. static longT _distance_dist_cdt(const longT x, const longT i, const longT *const g) {
  40471. const longT d = x<i?i - x:x - i;
  40472. return d<g[i]?g[i]:d;
  40473. }
  40474. static void _distance_scan(const unsigned int len,
  40475. const longT *const g,
  40476. longT (*const sep)(const longT, const longT, const longT *const),
  40477. longT (*const f)(const longT, const longT, const longT *const),
  40478. longT *const s,
  40479. longT *const t,
  40480. longT *const dt) {
  40481. longT q = s[0] = t[0] = 0;
  40482. for (int u = 1; u<(int)len; ++u) { // Forward scan
  40483. while ((q>=0) && f(t[q],s[q],g)>f(t[q],u,g)) { --q; }
  40484. if (q<0) { q = 0; s[0] = u; }
  40485. else { const longT w = 1 + sep(s[q], u, g); if (w<(longT)len) { ++q; s[q] = u; t[q] = w; }}
  40486. }
  40487. for (int u = (int)len - 1; u>=0; --u) { dt[u] = f(u,s[q],g); if (u==t[q]) --q; } // Backward scan
  40488. }
  40489. CImg<T>& _distance_core(longT (*const sep)(const longT, const longT, const longT *const),
  40490. longT (*const f)(const longT, const longT, const longT *const)) {
  40491. // Check for g++ 4.9.X, as OpenMP seems to crash for this particular function. I have no clues why.
  40492. #define cimg_is_gcc49x (__GNUC__==4 && __GNUC_MINOR__==9)
  40493. const ulongT wh = (ulongT)_width*_height;
  40494. #if cimg_use_openmp!=0 && !cimg_is_gcc49x
  40495. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
  40496. #endif
  40497. cimg_forC(*this,c) {
  40498. CImg<longT> g(_width), dt(_width), s(_width), t(_width);
  40499. CImg<T> img = get_shared_channel(c);
  40500. #if cimg_use_openmp!=0 && !cimg_is_gcc49x
  40501. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
  40502. _height*_depth>=16)
  40503. firstprivate(g,dt,s,t))
  40504. #endif
  40505. cimg_forYZ(*this,y,z) { // Over X-direction
  40506. cimg_forX(*this,x) g[x] = (longT)img(x,y,z,0,wh);
  40507. _distance_scan(_width,g,sep,f,s,t,dt);
  40508. cimg_forX(*this,x) img(x,y,z,0,wh) = (T)dt[x];
  40509. }
  40510. if (_height>1) {
  40511. g.assign(_height); dt.assign(_height); s.assign(_height); t.assign(_height);
  40512. #if cimg_use_openmp!=0 && !cimg_is_gcc49x
  40513. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  40514. cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*512 && _width*_depth>=16)
  40515. firstprivate(g,dt,s,t))
  40516. #endif
  40517. cimg_forXZ(*this,x,z) { // Over Y-direction
  40518. cimg_forY(*this,y) g[y] = (longT)img(x,y,z,0,wh);
  40519. _distance_scan(_height,g,sep,f,s,t,dt);
  40520. cimg_forY(*this,y) img(x,y,z,0,wh) = (T)dt[y];
  40521. }
  40522. }
  40523. if (_depth>1) {
  40524. g.assign(_depth); dt.assign(_depth); s.assign(_depth); t.assign(_depth);
  40525. #if cimg_use_openmp!=0 && !cimg_is_gcc49x
  40526. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  40527. cimg_openmp_if(_depth>=(cimg_openmp_sizefactor)*512 && _width*_height>=16)
  40528. firstprivate(g,dt,s,t))
  40529. #endif
  40530. cimg_forXY(*this,x,y) { // Over Z-direction
  40531. cimg_forZ(*this,z) g[z] = (longT)img(x,y,z,0,wh);
  40532. _distance_scan(_depth,g,sep,f,s,t,dt);
  40533. cimg_forZ(*this,z) img(x,y,z,0,wh) = (T)dt[z];
  40534. }
  40535. }
  40536. }
  40537. return *this;
  40538. }
  40539. //! Compute chamfer distance to a specified value, with a custom metric.
  40540. /**
  40541. \param value Reference value.
  40542. \param metric_mask Metric mask.
  40543. \note The algorithm code has been initially proposed by A. Meijster, and modified by D. Tschumperlé.
  40544. **/
  40545. template<typename t>
  40546. CImg<T>& distance(const T& value, const CImg<t>& metric_mask) {
  40547. if (is_empty()) return *this;
  40548. bool is_value = false;
  40549. cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999;
  40550. if (!is_value) return fill(cimg::type<T>::max());
  40551. const ulongT wh = (ulongT)_width*_height;
  40552. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
  40553. cimg_forC(*this,c) {
  40554. CImg<T> img = get_shared_channel(c);
  40555. cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
  40556. cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1024))
  40557. cimg_forXYZ(metric_mask,dx,dy,dz) {
  40558. const t weight = metric_mask(dx,dy,dz);
  40559. if (weight) {
  40560. for (int z = dz, nz = 0; z<depth(); ++z,++nz) { // Forward scan
  40561. for (int y = dy , ny = 0; y<height(); ++y,++ny) {
  40562. for (int x = dx, nx = 0; x<width(); ++x,++nx) {
  40563. const T dd = img(nx,ny,nz,0,wh) + weight;
  40564. if (dd<img(x,y,z,0,wh)) img(x,y,z,0,wh) = dd;
  40565. }
  40566. }
  40567. }
  40568. for (int z = depth() - 1 - dz, nz = depth() - 1; z>=0; --z,--nz) { // Backward scan
  40569. for (int y = height() - 1 - dy, ny = height() - 1; y>=0; --y,--ny) {
  40570. for (int x = width() - 1 - dx, nx = width() - 1; x>=0; --x,--nx) {
  40571. const T dd = img(nx,ny,nz,0,wh) + weight;
  40572. if (dd<img(x,y,z,0,wh)) img(x,y,z,0,wh) = dd;
  40573. }
  40574. }
  40575. }
  40576. }
  40577. }
  40578. }
  40579. return *this;
  40580. }
  40581. //! Compute chamfer distance to a specified value, with a custom metric \newinstance.
  40582. template<typename t>
  40583. CImg<Tfloat> get_distance(const T& value, const CImg<t>& metric_mask) const {
  40584. return CImg<Tfloat>(*this,false).distance(value,metric_mask);
  40585. }
  40586. //! Compute distance to a specified value, according to a custom metric (use dijkstra algorithm).
  40587. /**
  40588. \param value Reference value.
  40589. \param metric Field of distance potentials.
  40590. \param is_high_connectivity Tells if the algorithm uses low or high connectivity.
  40591. \param[out] return_path An image containing the nodes of the minimal path.
  40592. **/
  40593. template<typename t, typename to>
  40594. CImg<T>& distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,
  40595. CImg<to>& return_path) {
  40596. return get_distance_dijkstra(value,metric,is_high_connectivity,return_path).move_to(*this);
  40597. }
  40598. //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm) \newinstance.
  40599. template<typename t, typename to>
  40600. CImg<typename cimg::superset<t,long>::type>
  40601. get_distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,
  40602. CImg<to>& return_path) const {
  40603. if (is_empty()) return return_path.assign();
  40604. if (!is_sameXYZ(metric))
  40605. throw CImgArgumentException(_cimg_instance
  40606. "distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) "
  40607. "have incompatible dimensions.",
  40608. cimg_instance,
  40609. metric._width,metric._height,metric._depth,metric._spectrum);
  40610. typedef typename cimg::superset<t,long>::type td; // Type used for computing cumulative distances
  40611. CImg<td> result(_width,_height,_depth,_spectrum), Q;
  40612. CImg<boolT> is_queued(_width,_height,_depth,1);
  40613. if (return_path) return_path.assign(_width,_height,_depth,_spectrum);
  40614. cimg_forC(*this,c) {
  40615. const CImg<T> img = get_shared_channel(c);
  40616. const CImg<t> met = metric.get_shared_channel(c%metric._spectrum);
  40617. CImg<td> res = result.get_shared_channel(c);
  40618. CImg<to> path = return_path?return_path.get_shared_channel(c):CImg<to>();
  40619. unsigned int sizeQ = 0;
  40620. // Detect initial seeds.
  40621. is_queued.fill(0);
  40622. cimg_forXYZ(img,x,y,z) if (img(x,y,z)==value) {
  40623. Q._priority_queue_insert(is_queued,sizeQ,0,x,y,z);
  40624. res(x,y,z) = 0;
  40625. if (path) path(x,y,z) = (to)0;
  40626. }
  40627. // Start distance propagation.
  40628. while (sizeQ) {
  40629. // Get and remove point with minimal potential from the queue.
  40630. const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3);
  40631. const td P = (td)-Q(0,0);
  40632. Q._priority_queue_remove(sizeQ);
  40633. // Update neighbors.
  40634. td npot = 0;
  40635. if (x - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x - 1,y,z) + P),x - 1,y,z)) {
  40636. res(x - 1,y,z) = npot; if (path) path(x - 1,y,z) = (to)2;
  40637. }
  40638. if (x + 1<width() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x + 1,y,z) + P),x + 1,y,z)) {
  40639. res(x + 1,y,z) = npot; if (path) path(x + 1,y,z) = (to)1;
  40640. }
  40641. if (y - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y - 1,z) + P),x,y - 1,z)) {
  40642. res(x,y - 1,z) = npot; if (path) path(x,y - 1,z) = (to)8;
  40643. }
  40644. if (y + 1<height() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y + 1,z) + P),x,y + 1,z)) {
  40645. res(x,y + 1,z) = npot; if (path) path(x,y + 1,z) = (to)4;
  40646. }
  40647. if (z - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z - 1) + P),x,y,z - 1)) {
  40648. res(x,y,z - 1) = npot; if (path) path(x,y,z - 1) = (to)32;
  40649. }
  40650. if (z + 1<depth() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z + 1) + P),x,y,z + 1)) {
  40651. res(x,y,z + 1) = npot; if (path) path(x,y,z + 1) = (to)16;
  40652. }
  40653. if (is_high_connectivity) {
  40654. const float sqrt2 = std::sqrt(2.f), sqrt3 = std::sqrt(3.f);
  40655. // Diagonal neighbors on slice z.
  40656. if (x - 1>=0 && y - 1>=0 &&
  40657. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y - 1,z) + P)),x - 1,y - 1,z)) {
  40658. res(x - 1,y - 1,z) = npot; if (path) path(x - 1,y - 1,z) = (to)10;
  40659. }
  40660. if (x + 1<width() && y - 1>=0 &&
  40661. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y - 1,z) + P)),x + 1,y - 1,z)) {
  40662. res(x + 1,y - 1,z) = npot; if (path) path(x + 1,y - 1,z) = (to)9;
  40663. }
  40664. if (x - 1>=0 && y + 1<height() &&
  40665. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y + 1,z) + P)),x - 1,y + 1,z)) {
  40666. res(x - 1,y + 1,z) = npot; if (path) path(x - 1,y + 1,z) = (to)6;
  40667. }
  40668. if (x + 1<width() && y + 1<height() &&
  40669. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y + 1,z) + P)),x + 1,y + 1,z)) {
  40670. res(x + 1,y + 1,z) = npot; if (path) path(x + 1,y + 1,z) = (to)5;
  40671. }
  40672. if (z - 1>=0) { // Diagonal neighbors on slice z - 1
  40673. if (x - 1>=0 &&
  40674. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y,z - 1) + P)),x - 1,y,z - 1)) {
  40675. res(x - 1,y,z - 1) = npot; if (path) path(x - 1,y,z - 1) = (to)34;
  40676. }
  40677. if (x + 1<width() &&
  40678. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y,z - 1) + P)),x + 1,y,z - 1)) {
  40679. res(x + 1,y,z - 1) = npot; if (path) path(x + 1,y,z - 1) = (to)33;
  40680. }
  40681. if (y - 1>=0 &&
  40682. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y - 1,z - 1) + P)),x,y - 1,z - 1)) {
  40683. res(x,y - 1,z - 1) = npot; if (path) path(x,y - 1,z - 1) = (to)40;
  40684. }
  40685. if (y + 1<height() &&
  40686. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y + 1,z - 1) + P)),x,y + 1,z - 1)) {
  40687. res(x,y + 1,z - 1) = npot; if (path) path(x,y + 1,z - 1) = (to)36;
  40688. }
  40689. if (x - 1>=0 && y - 1>=0 &&
  40690. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y - 1,z - 1) + P)),
  40691. x - 1,y - 1,z - 1)) {
  40692. res(x - 1,y - 1,z - 1) = npot; if (path) path(x - 1,y - 1,z - 1) = (to)42;
  40693. }
  40694. if (x + 1<width() && y - 1>=0 &&
  40695. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y - 1,z - 1) + P)),
  40696. x + 1,y - 1,z - 1)) {
  40697. res(x + 1,y - 1,z - 1) = npot; if (path) path(x + 1,y - 1,z - 1) = (to)41;
  40698. }
  40699. if (x - 1>=0 && y + 1<height() &&
  40700. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y + 1,z - 1) + P)),
  40701. x - 1,y + 1,z - 1)) {
  40702. res(x - 1,y + 1,z - 1) = npot; if (path) path(x - 1,y + 1,z - 1) = (to)38;
  40703. }
  40704. if (x + 1<width() && y + 1<height() &&
  40705. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y + 1,z - 1) + P)),
  40706. x + 1,y + 1,z - 1)) {
  40707. res(x + 1,y + 1,z - 1) = npot; if (path) path(x + 1,y + 1,z - 1) = (to)37;
  40708. }
  40709. }
  40710. if (z + 1<depth()) { // Diagonal neighbors on slice z + 1
  40711. if (x - 1>=0 &&
  40712. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y,z + 1) + P)),x - 1,y,z + 1)) {
  40713. res(x - 1,y,z + 1) = npot; if (path) path(x - 1,y,z + 1) = (to)18;
  40714. }
  40715. if (x + 1<width() &&
  40716. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y,z + 1) + P)),x + 1,y,z + 1)) {
  40717. res(x + 1,y,z + 1) = npot; if (path) path(x + 1,y,z + 1) = (to)17;
  40718. }
  40719. if (y - 1>=0 &&
  40720. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y - 1,z + 1) + P)),x,y - 1,z + 1)) {
  40721. res(x,y - 1,z + 1) = npot; if (path) path(x,y - 1,z + 1) = (to)24;
  40722. }
  40723. if (y + 1<height() &&
  40724. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y + 1,z + 1) + P)),x,y + 1,z + 1)) {
  40725. res(x,y + 1,z + 1) = npot; if (path) path(x,y + 1,z + 1) = (to)20;
  40726. }
  40727. if (x - 1>=0 && y - 1>=0 &&
  40728. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y - 1,z + 1) + P)),
  40729. x - 1,y - 1,z + 1)) {
  40730. res(x - 1,y - 1,z + 1) = npot; if (path) path(x - 1,y - 1,z + 1) = (to)26;
  40731. }
  40732. if (x + 1<width() && y - 1>=0 &&
  40733. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y - 1,z + 1) + P)),
  40734. x + 1,y - 1,z + 1)) {
  40735. res(x + 1,y - 1,z + 1) = npot; if (path) path(x + 1,y - 1,z + 1) = (to)25;
  40736. }
  40737. if (x - 1>=0 && y + 1<height() &&
  40738. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y + 1,z + 1) + P)),
  40739. x - 1,y + 1,z + 1)) {
  40740. res(x - 1,y + 1,z + 1) = npot; if (path) path(x - 1,y + 1,z + 1) = (to)22;
  40741. }
  40742. if (x + 1<width() && y + 1<height() &&
  40743. Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y + 1,z + 1) + P)),
  40744. x + 1,y + 1,z + 1)) {
  40745. res(x + 1,y + 1,z + 1) = npot; if (path) path(x + 1,y + 1,z + 1) = (to)21;
  40746. }
  40747. }
  40748. }
  40749. }
  40750. }
  40751. return result;
  40752. }
  40753. //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \overloading.
  40754. template<typename t>
  40755. CImg<T>& distance_dijkstra(const T& value, const CImg<t>& metric,
  40756. const bool is_high_connectivity=false) {
  40757. return get_distance_dijkstra(value,metric,is_high_connectivity).move_to(*this);
  40758. }
  40759. //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance.
  40760. template<typename t>
  40761. CImg<Tfloat> get_distance_dijkstra(const T& value, const CImg<t>& metric,
  40762. const bool is_high_connectivity=false) const {
  40763. CImg<T> return_path;
  40764. return get_distance_dijkstra(value,metric,is_high_connectivity,return_path);
  40765. }
  40766. //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).
  40767. /**
  40768. \param value Reference value.
  40769. \param metric Field of distance potentials.
  40770. **/
  40771. template<typename t>
  40772. CImg<T>& distance_eikonal(const T& value, const CImg<t>& metric) {
  40773. return get_distance_eikonal(value,metric).move_to(*this);
  40774. }
  40775. //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).
  40776. template<typename t>
  40777. CImg<Tfloat> get_distance_eikonal(const T& value, const CImg<t>& metric) const {
  40778. if (is_empty()) return *this;
  40779. if (!is_sameXYZ(metric))
  40780. throw CImgArgumentException(_cimg_instance
  40781. "distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have "
  40782. "incompatible dimensions.",
  40783. cimg_instance,
  40784. metric._width,metric._height,metric._depth,metric._spectrum);
  40785. CImg<Tfloat> result(_width,_height,_depth,_spectrum,cimg::type<Tfloat>::max()), Q;
  40786. CImg<charT> state(_width,_height,_depth); // -1=far away, 0=narrow, 1=frozen
  40787. cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2) firstprivate(Q,state))
  40788. cimg_forC(*this,c) {
  40789. const CImg<T> img = get_shared_channel(c);
  40790. const CImg<t> met = metric.get_shared_channel(c%metric._spectrum);
  40791. CImg<Tfloat> res = result.get_shared_channel(c);
  40792. unsigned int sizeQ = 0;
  40793. state.fill(-1);
  40794. // Detect initial seeds.
  40795. Tfloat *ptr1 = res._data; char *ptr2 = state._data;
  40796. cimg_for(img,ptr0,T) { if (*ptr0==value) { *ptr1 = 0; *ptr2 = 1; } ++ptr1; ++ptr2; }
  40797. // Initialize seeds neighbors.
  40798. ptr2 = state._data;
  40799. cimg_forXYZ(img,x,y,z) if (*(ptr2++)==1) {
  40800. if (x - 1>=0 && state(x - 1,y,z)==-1) {
  40801. const Tfloat dist = res(x - 1,y,z) = __distance_eikonal(res,met(x - 1,y,z),x - 1,y,z);
  40802. Q._eik_priority_queue_insert(state,sizeQ,-dist,x - 1,y,z);
  40803. }
  40804. if (x + 1<width() && state(x + 1,y,z)==-1) {
  40805. const Tfloat dist = res(x + 1,y,z) = __distance_eikonal(res,met(x + 1,y,z),x + 1,y,z);
  40806. Q._eik_priority_queue_insert(state,sizeQ,-dist,x + 1,y,z);
  40807. }
  40808. if (y - 1>=0 && state(x,y - 1,z)==-1) {
  40809. const Tfloat dist = res(x,y - 1,z) = __distance_eikonal(res,met(x,y - 1,z),x,y - 1,z);
  40810. Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y - 1,z);
  40811. }
  40812. if (y + 1<height() && state(x,y + 1,z)==-1) {
  40813. const Tfloat dist = res(x,y + 1,z) = __distance_eikonal(res,met(x,y + 1,z),x,y + 1,z);
  40814. Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y + 1,z);
  40815. }
  40816. if (z - 1>=0 && state(x,y,z - 1)==-1) {
  40817. const Tfloat dist = res(x,y,z - 1) = __distance_eikonal(res,met(x,y,z - 1),x,y,z - 1);
  40818. Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z - 1);
  40819. }
  40820. if (z + 1<depth() && state(x,y,z + 1)==-1) {
  40821. const Tfloat dist = res(x,y,z + 1) = __distance_eikonal(res,met(x,y,z + 1),x,y,z + 1);
  40822. Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z + 1);
  40823. }
  40824. }
  40825. // Propagate front.
  40826. while (sizeQ) {
  40827. int x = -1, y = -1, z = -1;
  40828. while (sizeQ && x<0) {
  40829. x = (int)Q(0,1); y = (int)Q(0,2); z = (int)Q(0,3);
  40830. Q._priority_queue_remove(sizeQ);
  40831. if (state(x,y,z)==1) x = -1; else state(x,y,z) = 1;
  40832. }
  40833. if (x>=0) {
  40834. if (x - 1>=0 && state(x - 1,y,z)!=1) {
  40835. const Tfloat dist = __distance_eikonal(res,met(x - 1,y,z),x - 1,y,z);
  40836. if (dist<res(x - 1,y,z)) {
  40837. res(x - 1,y,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x - 1,y,z);
  40838. }
  40839. }
  40840. if (x + 1<width() && state(x + 1,y,z)!=1) {
  40841. const Tfloat dist = __distance_eikonal(res,met(x + 1,y,z),x + 1,y,z);
  40842. if (dist<res(x + 1,y,z)) {
  40843. res(x + 1,y,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x + 1,y,z);
  40844. }
  40845. }
  40846. if (y - 1>=0 && state(x,y - 1,z)!=1) {
  40847. const Tfloat dist = __distance_eikonal(res,met(x,y - 1,z),x,y - 1,z);
  40848. if (dist<res(x,y - 1,z)) {
  40849. res(x,y - 1,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y - 1,z);
  40850. }
  40851. }
  40852. if (y + 1<height() && state(x,y + 1,z)!=1) {
  40853. const Tfloat dist = __distance_eikonal(res,met(x,y + 1,z),x,y + 1,z);
  40854. if (dist<res(x,y + 1,z)) {
  40855. res(x,y + 1,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y + 1,z);
  40856. }
  40857. }
  40858. if (z - 1>=0 && state(x,y,z - 1)!=1) {
  40859. const Tfloat dist = __distance_eikonal(res,met(x,y,z - 1),x,y,z - 1);
  40860. if (dist<res(x,y,z - 1)) {
  40861. res(x,y,z - 1) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z - 1);
  40862. }
  40863. }
  40864. if (z + 1<depth() && state(x,y,z + 1)!=1) {
  40865. const Tfloat dist = __distance_eikonal(res,met(x,y,z + 1),x,y,z + 1);
  40866. if (dist<res(x,y,z + 1)) {
  40867. res(x,y,z + 1) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z + 1);
  40868. }
  40869. }
  40870. }
  40871. }
  40872. }
  40873. return result;
  40874. }
  40875. // Locally solve eikonal equation.
  40876. Tfloat __distance_eikonal(const CImg<Tfloat>& res, const Tfloat P,
  40877. const int x=0, const int y=0, const int z=0) const {
  40878. const Tfloat M = (Tfloat)cimg::type<T>::max();
  40879. T T1 = (T)std::min(x - 1>=0?res(x - 1,y,z):M,x + 1<width()?res(x + 1,y,z):M);
  40880. Tfloat root = 0;
  40881. if (_depth>1) { // 3D
  40882. T
  40883. T2 = (T)std::min(y - 1>=0?res(x,y - 1,z):M,y + 1<height()?res(x,y + 1,z):M),
  40884. T3 = (T)std::min(z - 1>=0?res(x,y,z - 1):M,z + 1<depth()?res(x,y,z + 1):M);
  40885. if (T1>T2) cimg::swap(T1,T2);
  40886. if (T2>T3) cimg::swap(T2,T3);
  40887. if (T1>T2) cimg::swap(T1,T2);
  40888. if (P<=0) return (Tfloat)T1;
  40889. if (T3<M && ___distance_eikonal(3,-2*(T1 + T2 + T3),T1*T1 + T2*T2 + T3*T3 - P*P,root))
  40890. return std::max((Tfloat)T3,root);
  40891. if (T2<M && ___distance_eikonal(2,-2*(T1 + T2),T1*T1 + T2*T2 - P*P,root))
  40892. return std::max((Tfloat)T2,root);
  40893. return P + T1;
  40894. } else if (_height>1) { // 2D
  40895. T T2 = (T)std::min(y - 1>=0?res(x,y - 1,z):M,y + 1<height()?res(x,y + 1,z):M);
  40896. if (T1>T2) cimg::swap(T1,T2);
  40897. if (P<=0) return (Tfloat)T1;
  40898. if (T2<M && ___distance_eikonal(2,-2*(T1 + T2),T1*T1 + T2*T2 - P*P,root))
  40899. return std::max((Tfloat)T2,root);
  40900. return P + T1;
  40901. } else { // 1D
  40902. if (P<=0) return (Tfloat)T1;
  40903. return P + T1;
  40904. }
  40905. return 0;
  40906. }
  40907. // Find max root of a 2nd-order polynomial.
  40908. static bool ___distance_eikonal(const Tfloat a, const Tfloat b, const Tfloat c, Tfloat &root) {
  40909. const Tfloat delta = b*b - 4*a*c;
  40910. if (delta<0) return false;
  40911. root = 0.5f*(-b + std::sqrt(delta))/a;
  40912. return true;
  40913. }
  40914. // Insert new point in heap.
  40915. template<typename t>
  40916. void _eik_priority_queue_insert(CImg<charT>& state, unsigned int& siz, const t value,
  40917. const unsigned int x, const unsigned int y, const unsigned int z) {
  40918. if (state(x,y,z)>0) return;
  40919. state(x,y,z) = 0;
  40920. if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); }
  40921. (*this)(siz - 1,0) = (T)value; (*this)(siz - 1,1) = (T)x; (*this)(siz - 1,2) = (T)y; (*this)(siz - 1,3) = (T)z;
  40922. for (unsigned int pos = siz - 1, par = 0; pos && value>(t)(*this)(par=(pos + 1)/2 - 1,0); pos = par) {
  40923. cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1));
  40924. cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3));
  40925. }
  40926. }
  40927. //! Compute distance function to 0-valued isophotes, using the Eikonal PDE.
  40928. /**
  40929. \param nb_iterations Number of PDE iterations.
  40930. \param band_size Size of the narrow band.
  40931. \param time_step Time step of the PDE iterations.
  40932. **/
  40933. CImg<T>& distance_eikonal(const unsigned int nb_iterations, const float band_size=0, const float time_step=0.5f) {
  40934. if (is_empty()) return *this;
  40935. CImg<Tfloat> velocity(*this,false);
  40936. for (unsigned int iteration = 0; iteration<nb_iterations; ++iteration) {
  40937. Tfloat *ptrd = velocity._data, veloc_max = 0;
  40938. if (_depth>1) { // 3D
  40939. CImg_3x3x3(I,Tfloat);
  40940. cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) if (band_size<=0 || cimg::abs(Iccc)<band_size) {
  40941. const Tfloat
  40942. gx = (Incc - Ipcc)/2,
  40943. gy = (Icnc - Icpc)/2,
  40944. gz = (Iccn - Iccp)/2,
  40945. sgn = -cimg::sign(Iccc),
  40946. ix = gx*sgn>0?(Incc - Iccc):(Iccc - Ipcc),
  40947. iy = gy*sgn>0?(Icnc - Iccc):(Iccc - Icpc),
  40948. iz = gz*sgn>0?(Iccn - Iccc):(Iccc - Iccp),
  40949. ng = 1e-5f + cimg::hypot(gx,gy,gz),
  40950. ngx = gx/ng,
  40951. ngy = gy/ng,
  40952. ngz = gz/ng,
  40953. veloc = sgn*(ngx*ix + ngy*iy + ngz*iz - 1);
  40954. *(ptrd++) = veloc;
  40955. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  40956. } else *(ptrd++) = 0;
  40957. } else { // 2D version
  40958. CImg_3x3(I,Tfloat);
  40959. cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,Tfloat) if (band_size<=0 || cimg::abs(Icc)<band_size) {
  40960. const Tfloat
  40961. gx = (Inc - Ipc)/2,
  40962. gy = (Icn - Icp)/2,
  40963. sgn = -cimg::sign(Icc),
  40964. ix = gx*sgn>0?(Inc - Icc):(Icc - Ipc),
  40965. iy = gy*sgn>0?(Icn - Icc):(Icc - Icp),
  40966. ng = std::max((Tfloat)1e-5,cimg::hypot(gx,gy)),
  40967. ngx = gx/ng,
  40968. ngy = gy/ng,
  40969. veloc = sgn*(ngx*ix + ngy*iy - 1);
  40970. *(ptrd++) = veloc;
  40971. if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
  40972. } else *(ptrd++) = 0;
  40973. }
  40974. if (veloc_max>0) *this+=(velocity*=time_step/veloc_max);
  40975. }
  40976. return *this;
  40977. }
  40978. //! Compute distance function to 0-valued isophotes, using the Eikonal PDE \newinstance.
  40979. CImg<Tfloat> get_distance_eikonal(const unsigned int nb_iterations, const float band_size=0,
  40980. const float time_step=0.5f) const {
  40981. return CImg<Tfloat>(*this,false).distance_eikonal(nb_iterations,band_size,time_step);
  40982. }
  40983. //! Compute Haar multiscale wavelet transform.
  40984. /**
  40985. \param axis Axis considered for the transform.
  40986. \param invert Set inverse of direct transform.
  40987. \param nb_scales Number of scales used for the transform.
  40988. **/
  40989. CImg<T>& haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) {
  40990. return get_haar(axis,invert,nb_scales).move_to(*this);
  40991. }
  40992. //! Compute Haar multiscale wavelet transform \newinstance.
  40993. CImg<Tfloat> get_haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) const {
  40994. if (is_empty() || !nb_scales) return +*this;
  40995. CImg<Tfloat> res;
  40996. const Tfloat sqrt2 = std::sqrt(2.f);
  40997. if (nb_scales==1) {
  40998. switch (cimg::lowercase(axis)) { // Single scale transform
  40999. case 'x' : {
  41000. const unsigned int w = _width/2;
  41001. if (w) {
  41002. if ((w%2) && w!=1)
  41003. throw CImgInstanceException(_cimg_instance
  41004. "haar(): Sub-image width %u is not even.",
  41005. cimg_instance,
  41006. w);
  41007. res.assign(_width,_height,_depth,_spectrum);
  41008. if (invert) cimg_forYZC(*this,y,z,c) { // Inverse transform along X
  41009. for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {
  41010. const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(xw,y,z,c);
  41011. res(x2++,y,z,c) = (val0 - val1)/sqrt2;
  41012. res(x2++,y,z,c) = (val0 + val1)/sqrt2;
  41013. }
  41014. } else cimg_forYZC(*this,y,z,c) { // Direct transform along X
  41015. for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {
  41016. const Tfloat val0 = (Tfloat)(*this)(x2++,y,z,c), val1 = (Tfloat)(*this)(x2++,y,z,c);
  41017. res(x,y,z,c) = (val0 + val1)/sqrt2;
  41018. res(xw,y,z,c) = (val1 - val0)/sqrt2;
  41019. }
  41020. }
  41021. } else return *this;
  41022. } break;
  41023. case 'y' : {
  41024. const unsigned int h = _height/2;
  41025. if (h) {
  41026. if ((h%2) && h!=1)
  41027. throw CImgInstanceException(_cimg_instance
  41028. "haar(): Sub-image height %u is not even.",
  41029. cimg_instance,
  41030. h);
  41031. res.assign(_width,_height,_depth,_spectrum);
  41032. if (invert) cimg_forXZC(*this,x,z,c) { // Inverse transform along Y
  41033. for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) {
  41034. const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(x,yh,z,c);
  41035. res(x,y2++,z,c) = (val0 - val1)/sqrt2;
  41036. res(x,y2++,z,c) = (val0 + val1)/sqrt2;
  41037. }
  41038. } else cimg_forXZC(*this,x,z,c) {
  41039. for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) { // Direct transform along Y
  41040. const Tfloat val0 = (Tfloat)(*this)(x,y2++,z,c), val1 = (Tfloat)(*this)(x,y2++,z,c);
  41041. res(x,y,z,c) = (val0 + val1)/sqrt2;
  41042. res(x,yh,z,c) = (val1 - val0)/sqrt2;
  41043. }
  41044. }
  41045. } else return *this;
  41046. } break;
  41047. case 'z' : {
  41048. const unsigned int d = _depth/2;
  41049. if (d) {
  41050. if ((d%2) && d!=1)
  41051. throw CImgInstanceException(_cimg_instance
  41052. "haar(): Sub-image depth %u is not even.",
  41053. cimg_instance,
  41054. d);
  41055. res.assign(_width,_height,_depth,_spectrum);
  41056. if (invert) cimg_forXYC(*this,x,y,c) { // Inverse transform along Z
  41057. for (unsigned int z = 0, zd = d, z2 = 0; z<d; ++z, ++zd) {
  41058. const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(x,y,zd,c);
  41059. res(x,y,z2++,c) = (val0 - val1)/sqrt2;
  41060. res(x,y,z2++,c) = (val0 + val1)/sqrt2;
  41061. }
  41062. } else cimg_forXYC(*this,x,y,c) {
  41063. for (unsigned int z = 0, zd = d, z2 = 0; z<d; ++z, ++zd) { // Direct transform along Z
  41064. const Tfloat val0 = (Tfloat)(*this)(x,y,z2++,c), val1 = (Tfloat)(*this)(x,y,z2++,c);
  41065. res(x,y,z,c) = (val0 + val1)/sqrt2;
  41066. res(x,y,zd,c) = (val1 - val0)/sqrt2;
  41067. }
  41068. }
  41069. } else return *this;
  41070. } break;
  41071. default :
  41072. throw CImgArgumentException(_cimg_instance
  41073. "haar(): Invalid specified axis '%c' "
  41074. "(should be { x | y | z }).",
  41075. cimg_instance,
  41076. axis);
  41077. }
  41078. } else { // Multi-scale version
  41079. if (invert) {
  41080. res.assign(*this,false);
  41081. switch (cimg::lowercase(axis)) {
  41082. case 'x' : {
  41083. unsigned int w = _width;
  41084. for (unsigned int s = 1; w && s<nb_scales; ++s) w/=2;
  41085. for (w = w?w:1; w<=_width; w*=2) res.draw_image(res.get_crop(0,w - 1).get_haar('x',true,1));
  41086. } break;
  41087. case 'y' : {
  41088. unsigned int h = _width;
  41089. for (unsigned int s = 1; h && s<nb_scales; ++s) h/=2;
  41090. for (h = h?h:1; h<=_height; h*=2) res.draw_image(res.get_crop(0,0,_width - 1,h - 1).get_haar('y',true,1));
  41091. } break;
  41092. case 'z' : {
  41093. unsigned int d = _depth;
  41094. for (unsigned int s = 1; d && s<nb_scales; ++s) d/=2;
  41095. for (d = d?d:1; d<=_depth; d*=2)
  41096. res.draw_image(res.get_crop(0,0,0,_width - 1,_height - 1,d - 1).get_haar('z',true,1));
  41097. } break;
  41098. default :
  41099. throw CImgArgumentException(_cimg_instance
  41100. "haar(): Invalid specified axis '%c' "
  41101. "(should be { x | y | z }).",
  41102. cimg_instance,
  41103. axis);
  41104. }
  41105. } else { // Direct transform
  41106. res = get_haar(axis,false,1);
  41107. switch (cimg::lowercase(axis)) {
  41108. case 'x' : {
  41109. for (unsigned int s = 1, w = _width/2; w && s<nb_scales; ++s, w/=2)
  41110. res.draw_image(res.get_crop(0,w - 1).get_haar('x',false,1));
  41111. } break;
  41112. case 'y' : {
  41113. for (unsigned int s = 1, h = _height/2; h && s<nb_scales; ++s, h/=2)
  41114. res.draw_image(res.get_crop(0,0,_width - 1,h - 1).get_haar('y',false,1));
  41115. } break;
  41116. case 'z' : {
  41117. for (unsigned int s = 1, d = _depth/2; d && s<nb_scales; ++s, d/=2)
  41118. res.draw_image(res.get_crop(0,0,0,_width - 1,_height - 1,d - 1).get_haar('z',false,1));
  41119. } break;
  41120. default :
  41121. throw CImgArgumentException(_cimg_instance
  41122. "haar(): Invalid specified axis '%c' "
  41123. "(should be { x | y | z }).",
  41124. cimg_instance,
  41125. axis);
  41126. }
  41127. }
  41128. }
  41129. return res;
  41130. }
  41131. //! Compute Haar multiscale wavelet transform \overloading.
  41132. /**
  41133. \param invert Set inverse of direct transform.
  41134. \param nb_scales Number of scales used for the transform.
  41135. **/
  41136. CImg<T>& haar(const bool invert=false, const unsigned int nb_scales=1) {
  41137. return get_haar(invert,nb_scales).move_to(*this);
  41138. }
  41139. //! Compute Haar multiscale wavelet transform \newinstance.
  41140. CImg<Tfloat> get_haar(const bool invert=false, const unsigned int nb_scales=1) const {
  41141. CImg<Tfloat> res;
  41142. if (nb_scales==1) { // Single scale transform
  41143. if (_width>1) get_haar('x',invert,1).move_to(res);
  41144. if (_height>1) { if (res) res.haar('y',invert,1); else get_haar('y',invert,1).move_to(res); }
  41145. if (_depth>1) { if (res) res.haar('z',invert,1); else get_haar('z',invert,1).move_to(res); }
  41146. if (res) return res;
  41147. } else { // Multi-scale transform
  41148. if (invert) { // Inverse transform
  41149. res.assign(*this,false);
  41150. if (_width>1) {
  41151. if (_height>1) {
  41152. if (_depth>1) {
  41153. unsigned int w = _width, h = _height, d = _depth;
  41154. for (unsigned int s = 1; w && h && d && s<nb_scales; ++s) { w/=2; h/=2; d/=2; }
  41155. for (w = w?w:1, h = h?h:1, d = d?d:1; w<=_width && h<=_height && d<=_depth; w*=2, h*=2, d*=2)
  41156. res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,d - 1).get_haar(true,1));
  41157. } else {
  41158. unsigned int w = _width, h = _height;
  41159. for (unsigned int s = 1; w && h && s<nb_scales; ++s) { w/=2; h/=2; }
  41160. for (w = w?w:1, h = h?h:1; w<=_width && h<=_height; w*=2, h*=2)
  41161. res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,0).get_haar(true,1));
  41162. }
  41163. } else {
  41164. if (_depth>1) {
  41165. unsigned int w = _width, d = _depth;
  41166. for (unsigned int s = 1; w && d && s<nb_scales; ++s) { w/=2; d/=2; }
  41167. for (w = w?w:1, d = d?d:1; w<=_width && d<=_depth; w*=2, d*=2)
  41168. res.draw_image(res.get_crop(0,0,0,w - 1,0,d - 1).get_haar(true,1));
  41169. } else {
  41170. unsigned int w = _width;
  41171. for (unsigned int s = 1; w && s<nb_scales; ++s) w/=2;
  41172. for (w = w?w:1; w<=_width; w*=2)
  41173. res.draw_image(res.get_crop(0,0,0,w - 1,0,0).get_haar(true,1));
  41174. }
  41175. }
  41176. } else {
  41177. if (_height>1) {
  41178. if (_depth>1) {
  41179. unsigned int h = _height, d = _depth;
  41180. for (unsigned int s = 1; h && d && s<nb_scales; ++s) { h/=2; d/=2; }
  41181. for (h = h?h:1, d = d?d:1; h<=_height && d<=_depth; h*=2, d*=2)
  41182. res.draw_image(res.get_crop(0,0,0,0,h - 1,d - 1).get_haar(true,1));
  41183. } else {
  41184. unsigned int h = _height;
  41185. for (unsigned int s = 1; h && s<nb_scales; ++s) h/=2;
  41186. for (h = h?h:1; h<=_height; h*=2)
  41187. res.draw_image(res.get_crop(0,0,0,0,h - 1,0).get_haar(true,1));
  41188. }
  41189. } else {
  41190. if (_depth>1) {
  41191. unsigned int d = _depth;
  41192. for (unsigned int s = 1; d && s<nb_scales; ++s) d/=2;
  41193. for (d = d?d:1; d<=_depth; d*=2)
  41194. res.draw_image(res.get_crop(0,0,0,0,0,d - 1).get_haar(true,1));
  41195. } else return *this;
  41196. }
  41197. }
  41198. } else { // Direct transform
  41199. res = get_haar(false,1);
  41200. if (_width>1) {
  41201. if (_height>1) {
  41202. if (_depth>1)
  41203. for (unsigned int s = 1, w = _width/2, h = _height/2, d = _depth/2; w && h && d && s<nb_scales;
  41204. ++s, w/=2, h/=2, d/=2)
  41205. res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,d - 1).haar(false,1));
  41206. else for (unsigned int s = 1, w = _width/2, h = _height/2; w && h && s<nb_scales; ++s, w/=2, h/=2)
  41207. res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,0).haar(false,1));
  41208. } else {
  41209. if (_depth>1) for (unsigned int s = 1, w = _width/2, d = _depth/2; w && d && s<nb_scales; ++s, w/=2, d/=2)
  41210. res.draw_image(res.get_crop(0,0,0,w - 1,0,d - 1).haar(false,1));
  41211. else for (unsigned int s = 1, w = _width/2; w && s<nb_scales; ++s, w/=2)
  41212. res.draw_image(res.get_crop(0,0,0,w - 1,0,0).haar(false,1));
  41213. }
  41214. } else {
  41215. if (_height>1) {
  41216. if (_depth>1)
  41217. for (unsigned int s = 1, h = _height/2, d = _depth/2; h && d && s<nb_scales; ++s, h/=2, d/=2)
  41218. res.draw_image(res.get_crop(0,0,0,0,h - 1,d - 1).haar(false,1));
  41219. else for (unsigned int s = 1, h = _height/2; h && s<nb_scales; ++s, h/=2)
  41220. res.draw_image(res.get_crop(0,0,0,0,h - 1,0).haar(false,1));
  41221. } else {
  41222. if (_depth>1) for (unsigned int s = 1, d = _depth/2; d && s<nb_scales; ++s, d/=2)
  41223. res.draw_image(res.get_crop(0,0,0,0,0,d - 1).haar(false,1));
  41224. else return *this;
  41225. }
  41226. }
  41227. }
  41228. return res;
  41229. }
  41230. return *this;
  41231. }
  41232. //! Compute 1D Fast Fourier Transform, along a specified axis.
  41233. /**
  41234. \param axis Axis along which the FFT is computed.
  41235. \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
  41236. **/
  41237. CImgList<Tfloat> get_FFT(const char axis, const bool is_inverse=false) const {
  41238. CImgList<Tfloat> res(*this,CImg<Tfloat>());
  41239. CImg<Tfloat>::FFT(res[0],res[1],axis,is_inverse);
  41240. return res;
  41241. }
  41242. //! Compute n-D Fast Fourier Transform.
  41243. /*
  41244. \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
  41245. **/
  41246. CImgList<Tfloat> get_FFT(const bool is_inverse=false) const {
  41247. CImgList<Tfloat> res(*this,CImg<Tfloat>());
  41248. CImg<Tfloat>::FFT(res[0],res[1],is_inverse);
  41249. return res;
  41250. }
  41251. //! Compute 1D Fast Fourier Transform, along a specified axis.
  41252. /**
  41253. \param[in,out] real Real part of the pixel values.
  41254. \param[in,out] imag Imaginary part of the pixel values.
  41255. \param axis Axis along which the FFT is computed.
  41256. \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
  41257. **/
  41258. static void FFT(CImg<T>& real, CImg<T>& imag, const char axis, const bool is_inverse=false,
  41259. const unsigned int nb_threads=0) {
  41260. if (!real)
  41261. throw CImgInstanceException("CImg<%s>::FFT(): Specified real part is empty.",
  41262. pixel_type());
  41263. if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,(T)0);
  41264. if (!real.is_sameXYZC(imag))
  41265. throw CImgInstanceException("CImg<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and "
  41266. "imaginary part (%u,%u,%u,%u,%p) have different dimensions.",
  41267. pixel_type(),
  41268. real._width,real._height,real._depth,real._spectrum,real._data,
  41269. imag._width,imag._height,imag._depth,imag._spectrum,imag._data);
  41270. const char _axis = cimg::lowercase(axis);
  41271. if (_axis!='x' && _axis!='y' && _axis!='z')
  41272. throw CImgArgumentException("CImgList<%s>::FFT(): Invalid specified axis '%c' for real and imaginary parts "
  41273. "(%u,%u,%u,%u) "
  41274. "(should be { x | y | z }).",
  41275. pixel_type(),axis,
  41276. real._width,real._height,real._depth,real._spectrum);
  41277. cimg::unused(nb_threads);
  41278. #ifdef cimg_use_fftw3
  41279. cimg::mutex(12);
  41280. #ifndef cimg_use_fftw3_singlethread
  41281. fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());
  41282. #endif
  41283. fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);
  41284. if (!data_in)
  41285. throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) "
  41286. "for computing FFT of image (%u,%u,%u,%u) along the X-axis.",
  41287. pixel_type(),
  41288. cimg::strbuffersize(sizeof(fftw_complex)*real._width*real._height*real._depth),
  41289. real._width,real._height,real._depth,real._spectrum);
  41290. double *const ptrf = (double*)data_in;
  41291. fftw_plan data_plan =
  41292. _axis=='x'?fftw_plan_many_dft(1,(int*)&real._width,real.height()*real.depth(),
  41293. data_in,0,1,real.width(),
  41294. data_in,0,1,real.width(),
  41295. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
  41296. _axis=='y'?fftw_plan_many_dft(1,(int*)&real._height,real.width()*real.depth(),
  41297. data_in,0,1,real.height(),
  41298. data_in,0,1,real.height(),
  41299. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
  41300. fftw_plan_many_dft(1,(int*)&real._depth,real.width()*real.height(),
  41301. data_in,0,1,real.depth(),
  41302. data_in,0,1,real.depth(),
  41303. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);
  41304. cimg_forC(real,c) {
  41305. CImg<T> realc = real.get_shared_channel(c), imagc = imag.get_shared_channel(c);
  41306. switch (_axis) {
  41307. case 'x' :
  41308. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41309. cimg_forXYZ(realc,x,y,z) {
  41310. const ulongT
  41311. i = realc.offset(x,y,z),
  41312. j = 2*(x + (ulongT)y*realc._width + (ulongT)z*realc._width*realc._height);
  41313. ptrf[j] = (double)realc[i];
  41314. ptrf[j + 1] = (double)imagc[i];
  41315. }
  41316. break;
  41317. case 'y' :
  41318. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41319. cimg_forXYZ(realc,x,y,z) {
  41320. const ulongT
  41321. i = realc.offset(x,y,z),
  41322. j = 2*(y + (ulongT)x*realc._height + (ulongT)z*realc._width*realc._height);
  41323. ptrf[j] = (double)realc[i];
  41324. ptrf[j + 1] = (double)imagc[i];
  41325. }
  41326. break;
  41327. default :
  41328. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41329. cimg_forXYZ(realc,x,y,z) {
  41330. const ulongT
  41331. i = realc.offset(x,y,z),
  41332. j = 2*(z + (ulongT)x*realc._depth + (ulongT)y*realc._width*realc._depth);
  41333. ptrf[j] = (double)realc[i];
  41334. ptrf[j + 1] = (double)imagc[i];
  41335. }
  41336. }
  41337. fftw_execute(data_plan);
  41338. const double a = is_inverse?1.0/(_axis=='x'?real.width():_axis=='y'?real.height():real.depth()):1.0;
  41339. switch (_axis) {
  41340. case 'x' :
  41341. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41342. cimg_forXYZ(realc,x,y,z) {
  41343. const ulongT
  41344. i = 2*(x + (ulongT)y*realc._width + (ulongT)z*realc._width*realc._height),
  41345. j = realc.offset(x,y,z);
  41346. realc[j] = (T)(a*ptrf[i]);
  41347. imagc[j] = (T)(a*ptrf[i + 1]);
  41348. }
  41349. break;
  41350. case 'y' :
  41351. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41352. cimg_forXYZ(realc,x,y,z) {
  41353. const ulongT
  41354. i = 2*(y + (ulongT)x*realc._height + (ulongT)z*realc._width*realc._height),
  41355. j = realc.offset(x,y,z);
  41356. realc[j] = (T)(a*ptrf[i]);
  41357. imagc[j] = (T)(a*ptrf[i + 1]);
  41358. }
  41359. break;
  41360. default :
  41361. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41362. cimg_forXYZ(realc,x,y,z) {
  41363. const ulongT
  41364. i = 2*(z + (ulongT)x*realc._depth + (ulongT)y*realc._width*realc._depth),
  41365. j = realc.offset(x,y,z);
  41366. realc[j] = (T)(a*ptrf[i]);
  41367. imagc[j] = (T)(a*ptrf[i + 1]);
  41368. }
  41369. }
  41370. }
  41371. fftw_destroy_plan(data_plan);
  41372. fftw_free(data_in);
  41373. #ifndef cimg_use_fftw3_singlethread
  41374. fftw_cleanup_threads();
  41375. #endif
  41376. cimg::mutex(12,0);
  41377. #else
  41378. switch (_axis) {
  41379. case 'x' : { // Fourier along X, using built-in functions
  41380. const unsigned int N = real._width, N2 = N>>1;
  41381. if (((N - 1)&N) && N!=1)
  41382. throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
  41383. "have non 2^N dimension along the X-axis.",
  41384. pixel_type(),
  41385. real._width,real._height,real._depth,real._spectrum);
  41386. for (unsigned int i = 0, j = 0; i<N2; ++i) {
  41387. if (j>i) cimg_forYZC(real,y,z,c) {
  41388. cimg::swap(real(i,y,z,c),real(j,y,z,c));
  41389. cimg::swap(imag(i,y,z,c),imag(j,y,z,c));
  41390. if (j<N2) {
  41391. const unsigned int ri = N - 1 - i, rj = N - 1 - j;
  41392. cimg::swap(real(ri,y,z,c),real(rj,y,z,c));
  41393. cimg::swap(imag(ri,y,z,c),imag(rj,y,z,c));
  41394. }
  41395. }
  41396. for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
  41397. }
  41398. for (unsigned int delta = 2; delta<=N; delta<<=1) {
  41399. const unsigned int delta2 = delta>>1;
  41400. for (unsigned int i = 0; i<N; i+=delta) {
  41401. float wr = 1, wi = 0;
  41402. const float
  41403. angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
  41404. ca = (float)std::cos(angle),
  41405. sa = (float)std::sin(angle);
  41406. for (unsigned int k = 0; k<delta2; ++k) {
  41407. const unsigned int j = i + k, nj = j + delta2;
  41408. cimg_forYZC(real,y,z,c) {
  41409. T &ir = real(j,y,z,c), &ii = imag(j,y,z,c), &nir = real(nj,y,z,c), &nii = imag(nj,y,z,c);
  41410. const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
  41411. nir = (T)(ir - tmpr);
  41412. nii = (T)(ii - tmpi);
  41413. ir+=(T)tmpr;
  41414. ii+=(T)tmpi;
  41415. }
  41416. const float nwr = wr*ca-wi*sa;
  41417. wi = wi*ca + wr*sa;
  41418. wr = nwr;
  41419. }
  41420. }
  41421. }
  41422. if (is_inverse) { real/=N; imag/=N; }
  41423. } break;
  41424. case 'y' : { // Fourier along Y, using built-in functions
  41425. const unsigned int N = real._height, N2 = N>>1;
  41426. if (((N - 1)&N) && N!=1)
  41427. throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
  41428. "have non 2^N dimension along the Y-axis.",
  41429. pixel_type(),
  41430. real._width,real._height,real._depth,real._spectrum);
  41431. for (unsigned int i = 0, j = 0; i<N2; ++i) {
  41432. if (j>i) cimg_forXZC(real,x,z,c) {
  41433. cimg::swap(real(x,i,z,c),real(x,j,z,c));
  41434. cimg::swap(imag(x,i,z,c),imag(x,j,z,c));
  41435. if (j<N2) {
  41436. const unsigned int ri = N - 1 - i, rj = N - 1 - j;
  41437. cimg::swap(real(x,ri,z,c),real(x,rj,z,c));
  41438. cimg::swap(imag(x,ri,z,c),imag(x,rj,z,c));
  41439. }
  41440. }
  41441. for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
  41442. }
  41443. for (unsigned int delta = 2; delta<=N; delta<<=1) {
  41444. const unsigned int delta2 = (delta>>1);
  41445. for (unsigned int i = 0; i<N; i+=delta) {
  41446. float wr = 1, wi = 0;
  41447. const float
  41448. angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
  41449. ca = (float)std::cos(angle),
  41450. sa = (float)std::sin(angle);
  41451. for (unsigned int k = 0; k<delta2; ++k) {
  41452. const unsigned int j = i + k, nj = j + delta2;
  41453. cimg_forXZC(real,x,z,c) {
  41454. T &ir = real(x,j,z,c), &ii = imag(x,j,z,c), &nir = real(x,nj,z,c), &nii = imag(x,nj,z,c);
  41455. const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
  41456. nir = (T)(ir - tmpr);
  41457. nii = (T)(ii - tmpi);
  41458. ir+=(T)tmpr;
  41459. ii+=(T)tmpi;
  41460. }
  41461. const float nwr = wr*ca-wi*sa;
  41462. wi = wi*ca + wr*sa;
  41463. wr = nwr;
  41464. }
  41465. }
  41466. }
  41467. if (is_inverse) { real/=N; imag/=N; }
  41468. } break;
  41469. default : { // Fourier along Z, using built-in functions
  41470. const unsigned int N = real._depth, N2 = N>>1;
  41471. if (((N - 1)&N) && N!=1)
  41472. throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
  41473. "have non 2^N dimension along the Z-axis.",
  41474. pixel_type(),
  41475. real._width,real._height,real._depth,real._spectrum);
  41476. for (unsigned int i = 0, j = 0; i<N2; ++i) {
  41477. if (j>i) cimg_forXYC(real,x,y,c) {
  41478. cimg::swap(real(x,y,i,c),real(x,y,j,c));
  41479. cimg::swap(imag(x,y,i,c),imag(x,y,j,c));
  41480. if (j<N2) {
  41481. const unsigned int ri = N - 1 - i, rj = N - 1 - j;
  41482. cimg::swap(real(x,y,ri,c),real(x,y,rj,c));
  41483. cimg::swap(imag(x,y,ri,c),imag(x,y,rj,c));
  41484. }
  41485. }
  41486. for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
  41487. }
  41488. for (unsigned int delta = 2; delta<=N; delta<<=1) {
  41489. const unsigned int delta2 = (delta>>1);
  41490. for (unsigned int i = 0; i<N; i+=delta) {
  41491. float wr = 1, wi = 0;
  41492. const float
  41493. angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
  41494. ca = (float)std::cos(angle),
  41495. sa = (float)std::sin(angle);
  41496. for (unsigned int k = 0; k<delta2; ++k) {
  41497. const unsigned int j = i + k, nj = j + delta2;
  41498. cimg_forXYC(real,x,y,c) {
  41499. T &ir = real(x,y,j,c), &ii = imag(x,y,j,c), &nir = real(x,y,nj,c), &nii = imag(x,y,nj,c);
  41500. const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
  41501. nir = (T)(ir - tmpr);
  41502. nii = (T)(ii - tmpi);
  41503. ir+=(T)tmpr;
  41504. ii+=(T)tmpi;
  41505. }
  41506. const float nwr = wr*ca-wi*sa;
  41507. wi = wi*ca + wr*sa;
  41508. wr = nwr;
  41509. }
  41510. }
  41511. }
  41512. if (is_inverse) { real/=N; imag/=N; }
  41513. } break;
  41514. }
  41515. #endif
  41516. }
  41517. //! Compute n-D Fast Fourier Transform.
  41518. /**
  41519. \param[in,out] real Real part of the pixel values.
  41520. \param[in,out] imag Imaginary part of the pixel values.
  41521. \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
  41522. \param nb_threads Number of parallel threads used for the computation.
  41523. Use \c 0 to set this to the number of available cpus.
  41524. **/
  41525. static void FFT(CImg<T>& real, CImg<T>& imag, const bool is_inverse=false,
  41526. const unsigned int nb_threads=0) {
  41527. if (!real)
  41528. throw CImgInstanceException("CImgList<%s>::FFT(): Empty specified real part.",
  41529. pixel_type());
  41530. if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,(T)0);
  41531. if (!real.is_sameXYZC(imag))
  41532. throw CImgInstanceException("CImgList<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and "
  41533. "imaginary part (%u,%u,%u,%u,%p) have different dimensions.",
  41534. pixel_type(),
  41535. real._width,real._height,real._depth,real._spectrum,real._data,
  41536. imag._width,imag._height,imag._depth,imag._spectrum,imag._data);
  41537. cimg::unused(nb_threads);
  41538. #ifdef cimg_use_fftw3
  41539. cimg::mutex(12);
  41540. #ifndef cimg_use_fftw3_singlethread
  41541. fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());
  41542. #endif
  41543. fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);
  41544. if (!data_in)
  41545. throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) "
  41546. "for computing FFT of image (%u,%u,%u,%u).",
  41547. pixel_type(),
  41548. cimg::strbuffersize(sizeof(fftw_complex)*real._width*
  41549. real._height*real._depth*real._spectrum),
  41550. real._width,real._height,real._depth,real._spectrum);
  41551. double *const ptrf = (double*)data_in;
  41552. fftw_plan data_plan =
  41553. real._depth>1?fftw_plan_dft_3d(real._depth,real._height,real._width,data_in,data_in,
  41554. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
  41555. real._height>1?fftw_plan_dft_2d(real._height,real._width,data_in,data_in,
  41556. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
  41557. fftw_plan_dft_1d(real._width,data_in,data_in,
  41558. is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);
  41559. cimg_forC(real,c) {
  41560. CImg<T> realc = real.get_shared_channel(c), imagc = imag.get_shared_channel(c);
  41561. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41562. cimg_rofoff(realc,i) { const ulongT i2 = 2*i; ptrf[i2] = (double)realc[i]; ptrf[i2 + 1] = (double)imagc[i]; }
  41563. fftw_execute(data_plan);
  41564. if (is_inverse) {
  41565. const double a = 1.0/(real.width()*real.height()*real.depth());
  41566. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41567. cimg_rofoff(realc,i) { const ulongT i2 = 2*i; realc[i] = (T)(a*ptrf[i2]); imagc[i] = (T)(a*ptrf[i2 + 1]); }
  41568. } else
  41569. cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
  41570. cimg_rofoff(realc,i) { const ulongT i2 = 2*i; realc[i] = (T)ptrf[i2]; imagc[i] = (T)ptrf[i2 + 1]; }
  41571. }
  41572. fftw_destroy_plan(data_plan);
  41573. fftw_free(data_in);
  41574. #ifndef cimg_use_fftw3_singlethread
  41575. fftw_cleanup_threads();
  41576. #endif
  41577. cimg::mutex(12,0);
  41578. #else
  41579. if (real._depth>1) FFT(real,imag,'z',is_inverse);
  41580. if (real._height>1) FFT(real,imag,'y',is_inverse);
  41581. if (real._width>1) FFT(real,imag,'x',is_inverse);
  41582. #endif
  41583. }
  41584. //@}
  41585. //-------------------------------------
  41586. //
  41587. //! \name 3D Objects Management
  41588. //@{
  41589. //-------------------------------------
  41590. //! Rotate 3D object's vertices.
  41591. /**
  41592. \param x X-coordinate of the rotation axis, or first quaternion coordinate.
  41593. \param y Y-coordinate of the rotation axis, or second quaternion coordinate.
  41594. \param z Z-coordinate of the rotation axis, or second quaternion coordinate.
  41595. \param w Angle of the rotation axis (in degree), or fourth quaternion coordinate.
  41596. \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion (x,y,z,w).
  41597. **/
  41598. CImg<T>& rotate_object3d(const float x, const float y, const float z, const float w,
  41599. const bool is_quaternion=false) {
  41600. return get_rotate_object3d(x,y,z,w,is_quaternion).move_to(*this);
  41601. }
  41602. CImg<Tfloat> get_rotate_object3d(const float x, const float y, const float z, const float w,
  41603. const bool is_quaternion=false) const {
  41604. if (_height!=3 || _depth>1 || _spectrum>1)
  41605. throw CImgInstanceException(_cimg_instance
  41606. "rotate_object3d(): Instance is not a set of 3D vertices.",
  41607. cimg_instance);
  41608. return CImg<Tfloat>::rotation_matrix(x,y,z,w,is_quaternion)**this;
  41609. }
  41610. //! Shift 3D object's vertices.
  41611. /**
  41612. \param tx X-coordinate of the 3D displacement vector.
  41613. \param ty Y-coordinate of the 3D displacement vector.
  41614. \param tz Z-coordinate of the 3D displacement vector.
  41615. **/
  41616. CImg<T>& shift_object3d(const float tx, const float ty=0, const float tz=0) {
  41617. if (_height!=3 || _depth>1 || _spectrum>1)
  41618. throw CImgInstanceException(_cimg_instance
  41619. "shift_object3d(): Instance is not a set of 3D vertices.",
  41620. cimg_instance);
  41621. get_shared_row(0)+=tx; get_shared_row(1)+=ty; get_shared_row(2)+=tz;
  41622. return *this;
  41623. }
  41624. //! Shift 3D object's vertices \newinstance.
  41625. CImg<Tfloat> get_shift_object3d(const float tx, const float ty=0, const float tz=0) const {
  41626. return CImg<Tfloat>(*this,false).shift_object3d(tx,ty,tz);
  41627. }
  41628. //! Shift 3D object's vertices, so that it becomes centered.
  41629. /**
  41630. \note The object center is computed as its barycenter.
  41631. **/
  41632. CImg<T>& shift_object3d() {
  41633. if (_height!=3 || _depth>1 || _spectrum>1)
  41634. throw CImgInstanceException(_cimg_instance
  41635. "shift_object3d(): Instance is not a set of 3D vertices.",
  41636. cimg_instance);
  41637. CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
  41638. float
  41639. xm, xM = (float)xcoords.max_min(xm),
  41640. ym, yM = (float)ycoords.max_min(ym),
  41641. zm, zM = (float)zcoords.max_min(zm);
  41642. xcoords-=(xm + xM)/2; ycoords-=(ym + yM)/2; zcoords-=(zm + zM)/2;
  41643. return *this;
  41644. }
  41645. //! Shift 3D object's vertices, so that it becomes centered \newinstance.
  41646. CImg<Tfloat> get_shift_object3d() const {
  41647. return CImg<Tfloat>(*this,false).shift_object3d();
  41648. }
  41649. //! Resize 3D object.
  41650. /**
  41651. \param sx Width of the 3D object's bounding box.
  41652. \param sy Height of the 3D object's bounding box.
  41653. \param sz Depth of the 3D object's bounding box.
  41654. **/
  41655. CImg<T>& resize_object3d(const float sx, const float sy=-100, const float sz=-100) {
  41656. if (_height!=3 || _depth>1 || _spectrum>1)
  41657. throw CImgInstanceException(_cimg_instance
  41658. "resize_object3d(): Instance is not a set of 3D vertices.",
  41659. cimg_instance);
  41660. CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
  41661. float
  41662. xm, xM = (float)xcoords.max_min(xm),
  41663. ym, yM = (float)ycoords.max_min(ym),
  41664. zm, zM = (float)zcoords.max_min(zm);
  41665. if (xm<xM) { if (sx>0) xcoords*=sx/(xM-xm); else xcoords*=-sx/100; }
  41666. if (ym<yM) { if (sy>0) ycoords*=sy/(yM-ym); else ycoords*=-sy/100; }
  41667. if (zm<zM) { if (sz>0) zcoords*=sz/(zM-zm); else zcoords*=-sz/100; }
  41668. return *this;
  41669. }
  41670. //! Resize 3D object \newinstance.
  41671. CImg<Tfloat> get_resize_object3d(const float sx, const float sy=-100, const float sz=-100) const {
  41672. return CImg<Tfloat>(*this,false).resize_object3d(sx,sy,sz);
  41673. }
  41674. //! Resize 3D object to unit size.
  41675. CImg<T> resize_object3d() {
  41676. if (_height!=3 || _depth>1 || _spectrum>1)
  41677. throw CImgInstanceException(_cimg_instance
  41678. "resize_object3d(): Instance is not a set of 3D vertices.",
  41679. cimg_instance);
  41680. CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
  41681. float
  41682. xm, xM = (float)xcoords.max_min(xm),
  41683. ym, yM = (float)ycoords.max_min(ym),
  41684. zm, zM = (float)zcoords.max_min(zm);
  41685. const float dx = xM - xm, dy = yM - ym, dz = zM - zm, dmax = cimg::max(dx,dy,dz);
  41686. if (dmax>0) { xcoords/=dmax; ycoords/=dmax; zcoords/=dmax; }
  41687. return *this;
  41688. }
  41689. //! Resize 3D object to unit size \newinstance.
  41690. CImg<Tfloat> get_resize_object3d() const {
  41691. return CImg<Tfloat>(*this,false).resize_object3d();
  41692. }
  41693. //! Merge two 3D objects together.
  41694. /**
  41695. \param[in,out] primitives Primitives data of the current 3D object.
  41696. \param obj_vertices Vertices data of the additional 3D object.
  41697. \param obj_primitives Primitives data of the additional 3D object.
  41698. **/
  41699. template<typename tf, typename tp, typename tff>
  41700. CImg<T>& append_object3d(CImgList<tf>& primitives, const CImg<tp>& obj_vertices,
  41701. const CImgList<tff>& obj_primitives) {
  41702. if (!obj_vertices || !obj_primitives) return *this;
  41703. if (obj_vertices._height!=3 || obj_vertices._depth>1 || obj_vertices._spectrum>1)
  41704. throw CImgInstanceException(_cimg_instance
  41705. "append_object3d(): Specified vertice image (%u,%u,%u,%u,%p) is not a "
  41706. "set of 3D vertices.",
  41707. cimg_instance,
  41708. obj_vertices._width,obj_vertices._height,
  41709. obj_vertices._depth,obj_vertices._spectrum,obj_vertices._data);
  41710. if (is_empty()) { primitives.assign(obj_primitives); return assign(obj_vertices); }
  41711. if (_height!=3 || _depth>1 || _spectrum>1)
  41712. throw CImgInstanceException(_cimg_instance
  41713. "append_object3d(): Instance is not a set of 3D vertices.",
  41714. cimg_instance);
  41715. const unsigned int P = _width;
  41716. append(obj_vertices,'x');
  41717. const unsigned int N = primitives._width;
  41718. primitives.insert(obj_primitives);
  41719. for (unsigned int i = N; i<primitives._width; ++i) {
  41720. CImg<tf> &p = primitives[i];
  41721. switch (p.size()) {
  41722. case 1 : p[0]+=P; break; // Point
  41723. case 5 : p[0]+=P; p[1]+=P; break; // Sphere
  41724. case 2 : case 6 : p[0]+=P; p[1]+=P; break; // Segment
  41725. case 3 : case 9 : p[0]+=P; p[1]+=P; p[2]+=P; break; // Triangle
  41726. case 4 : case 12 : p[0]+=P; p[1]+=P; p[2]+=P; p[3]+=P; break; // Rectangle
  41727. }
  41728. }
  41729. return *this;
  41730. }
  41731. //! Texturize primitives of a 3D object.
  41732. /**
  41733. \param[in,out] primitives Primitives data of the 3D object.
  41734. \param[in,out] colors Colors data of the 3D object.
  41735. \param texture Texture image to map to 3D object.
  41736. \param coords Texture-mapping coordinates.
  41737. **/
  41738. template<typename tp, typename tc, typename tt, typename tx>
  41739. const CImg<T>& texturize_object3d(CImgList<tp>& primitives, CImgList<tc>& colors,
  41740. const CImg<tt>& texture, const CImg<tx>& coords=CImg<tx>::const_empty()) const {
  41741. if (is_empty()) return *this;
  41742. if (_height!=3)
  41743. throw CImgInstanceException(_cimg_instance
  41744. "texturize_object3d(): image instance is not a set of 3D points.",
  41745. cimg_instance);
  41746. if (coords && (coords._width!=_width || coords._height!=2))
  41747. throw CImgArgumentException(_cimg_instance
  41748. "texturize_object3d(): Invalid specified texture coordinates (%u,%u,%u,%u,%p).",
  41749. cimg_instance,
  41750. coords._width,coords._height,coords._depth,coords._spectrum,coords._data);
  41751. CImg<intT> _coords;
  41752. if (!coords) { // If no texture coordinates specified, do a default XY-projection
  41753. _coords.assign(_width,2);
  41754. float
  41755. xmin, xmax = (float)get_shared_row(0).max_min(xmin),
  41756. ymin, ymax = (float)get_shared_row(1).max_min(ymin),
  41757. dx = xmax>xmin?xmax-xmin:1,
  41758. dy = ymax>ymin?ymax-ymin:1;
  41759. cimg_forX(*this,p) {
  41760. _coords(p,0) = (int)(((*this)(p,0) - xmin)*texture._width/dx);
  41761. _coords(p,1) = (int)(((*this)(p,1) - ymin)*texture._height/dy);
  41762. }
  41763. } else _coords = coords;
  41764. int texture_ind = -1;
  41765. cimglist_for(primitives,l) {
  41766. CImg<tp> &p = primitives[l];
  41767. const unsigned int siz = p.size();
  41768. switch (siz) {
  41769. case 1 : { // Point
  41770. const unsigned int i0 = (unsigned int)p[0];
  41771. const int x0 = _coords(i0,0), y0 = _coords(i0,1);
  41772. texture.get_vector_at(x0<=0?0:x0>=texture.width()?texture.width() - 1:x0,
  41773. y0<=0?0:y0>=texture.height()?texture.height() - 1:y0).move_to(colors[l]);
  41774. } break;
  41775. case 2 : case 6 : { // Line
  41776. const unsigned int i0 = (unsigned int)p[0], i1 = (unsigned int)p[1];
  41777. const int
  41778. x0 = _coords(i0,0), y0 = _coords(i0,1),
  41779. x1 = _coords(i1,0), y1 = _coords(i1,1);
  41780. if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
  41781. else colors[l].assign(colors[texture_ind],true);
  41782. CImg<tp>::vector(i0,i1,x0,y0,x1,y1).move_to(p);
  41783. } break;
  41784. case 3 : case 9 : { // Triangle
  41785. const unsigned int i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2];
  41786. const int
  41787. x0 = _coords(i0,0), y0 = _coords(i0,1),
  41788. x1 = _coords(i1,0), y1 = _coords(i1,1),
  41789. x2 = _coords(i2,0), y2 = _coords(i2,1);
  41790. if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
  41791. else colors[l].assign(colors[texture_ind],true);
  41792. CImg<tp>::vector(i0,i1,i2,x0,y0,x1,y1,x2,y2).move_to(p);
  41793. } break;
  41794. case 4 : case 12 : { // Quadrangle
  41795. const unsigned int
  41796. i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], i3 = (unsigned int)p[3];
  41797. const int
  41798. x0 = _coords(i0,0), y0 = _coords(i0,1),
  41799. x1 = _coords(i1,0), y1 = _coords(i1,1),
  41800. x2 = _coords(i2,0), y2 = _coords(i2,1),
  41801. x3 = _coords(i3,0), y3 = _coords(i3,1);
  41802. if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
  41803. else colors[l].assign(colors[texture_ind],true);
  41804. CImg<tp>::vector(i0,i1,i2,i3,x0,y0,x1,y1,x2,y2,x3,y3).move_to(p);
  41805. } break;
  41806. }
  41807. }
  41808. return *this;
  41809. }
  41810. //! Generate a 3D elevation of the image instance.
  41811. /**
  41812. \param[out] primitives The returned list of the 3D object primitives
  41813. (template type \e tf should be at least \e unsigned \e int).
  41814. \param[out] colors The returned list of the 3D object colors.
  41815. \param elevation The input elevation map.
  41816. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  41817. \par Example
  41818. \code
  41819. const CImg<float> img("reference.jpg");
  41820. CImgList<unsigned int> faces3d;
  41821. CImgList<unsigned char> colors3d;
  41822. const CImg<float> points3d = img.get_elevation3d(faces3d,colors3d,img.get_norm()*0.2);
  41823. CImg<unsigned char>().display_object3d("Elevation3d",points3d,faces3d,colors3d);
  41824. \endcode
  41825. \image html ref_elevation3d.jpg
  41826. **/
  41827. template<typename tf, typename tc, typename te>
  41828. CImg<floatT> get_elevation3d(CImgList<tf>& primitives, CImgList<tc>& colors, const CImg<te>& elevation) const {
  41829. if (!is_sameXY(elevation) || elevation._depth>1 || elevation._spectrum>1)
  41830. throw CImgArgumentException(_cimg_instance
  41831. "get_elevation3d(): Instance and specified elevation (%u,%u,%u,%u,%p) "
  41832. "have incompatible dimensions.",
  41833. cimg_instance,
  41834. elevation._width,elevation._height,elevation._depth,
  41835. elevation._spectrum,elevation._data);
  41836. if (is_empty()) return *this;
  41837. float m, M = (float)max_min(m);
  41838. if (M==m) ++M;
  41839. colors.assign();
  41840. const unsigned int size_x1 = _width - 1, size_y1 = _height - 1;
  41841. for (unsigned int y = 0; y<size_y1; ++y)
  41842. for (unsigned int x = 0; x<size_x1; ++x) {
  41843. const unsigned char
  41844. r = (unsigned char)(((*this)(x,y,0) - m)*255/(M-m)),
  41845. g = (unsigned char)(_spectrum>1?((*this)(x,y,1) - m)*255/(M-m):r),
  41846. b = (unsigned char)(_spectrum>2?((*this)(x,y,2) - m)*255/(M-m):_spectrum>1?0:r);
  41847. CImg<tc>::vector((tc)r,(tc)g,(tc)b).move_to(colors);
  41848. }
  41849. const typename CImg<te>::_functor2d_int func(elevation);
  41850. return elevation3d(primitives,func,0,0,_width - 1.f,_height - 1.f,_width,_height);
  41851. }
  41852. //! Generate the 3D projection planes of the image instance.
  41853. /**
  41854. \param[out] primitives Primitives data of the returned 3D object.
  41855. \param[out] colors Colors data of the returned 3D object.
  41856. \param x0 X-coordinate of the projection point.
  41857. \param y0 Y-coordinate of the projection point.
  41858. \param z0 Z-coordinate of the projection point.
  41859. \param normalize_colors Tells if the created textures have normalized colors.
  41860. **/
  41861. template<typename tf, typename tc>
  41862. CImg<floatT> get_projections3d(CImgList<tf>& primitives, CImgList<tc>& colors,
  41863. const unsigned int x0, const unsigned int y0, const unsigned int z0,
  41864. const bool normalize_colors=false) const {
  41865. float m = 0, M = 0, delta = 1;
  41866. if (normalize_colors) { m = (float)min_max(M); delta = 255/(m==M?1:M-m); }
  41867. const unsigned int
  41868. _x0 = (x0>=_width)?_width - 1:x0,
  41869. _y0 = (y0>=_height)?_height - 1:y0,
  41870. _z0 = (z0>=_depth)?_depth - 1:z0;
  41871. CImg<tc> img_xy, img_xz, img_yz;
  41872. if (normalize_colors) {
  41873. ((get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1)-=m)*=delta).move_to(img_xy);
  41874. ((get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1)-=m)*=delta).resize(_width,_depth,1,-100,-1).
  41875. move_to(img_xz);
  41876. ((get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1)-=m)*=delta).resize(_height,_depth,1,-100,-1).
  41877. move_to(img_yz);
  41878. } else {
  41879. get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1).move_to(img_xy);
  41880. get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1).resize(_width,_depth,1,-100,-1).move_to(img_xz);
  41881. get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1).resize(_height,_depth,1,-100,-1).move_to(img_yz);
  41882. }
  41883. CImg<floatT> points(12,3,1,1,
  41884. 0,_width - 1,_width - 1,0, 0,_width - 1,_width - 1,0, _x0,_x0,_x0,_x0,
  41885. 0,0,_height - 1,_height - 1, _y0,_y0,_y0,_y0, 0,_height - 1,_height - 1,0,
  41886. _z0,_z0,_z0,_z0, 0,0,_depth - 1,_depth - 1, 0,0,_depth - 1,_depth - 1);
  41887. primitives.assign();
  41888. CImg<tf>::vector(0,1,2,3,0,0,img_xy._width - 1,0,img_xy._width - 1,img_xy._height - 1,0,img_xy._height - 1).
  41889. move_to(primitives);
  41890. CImg<tf>::vector(4,5,6,7,0,0,img_xz._width - 1,0,img_xz._width - 1,img_xz._height - 1,0,img_xz._height - 1).
  41891. move_to(primitives);
  41892. CImg<tf>::vector(8,9,10,11,0,0,img_yz._width - 1,0,img_yz._width - 1,img_yz._height - 1,0,img_yz._height - 1).
  41893. move_to(primitives);
  41894. colors.assign();
  41895. img_xy.move_to(colors);
  41896. img_xz.move_to(colors);
  41897. img_yz.move_to(colors);
  41898. return points;
  41899. }
  41900. //! Generate a isoline of the image instance as a 3D object.
  41901. /**
  41902. \param[out] primitives The returned list of the 3D object primitives
  41903. (template type \e tf should be at least \e unsigned \e int).
  41904. \param isovalue The returned list of the 3D object colors.
  41905. \param size_x The number of subdivisions along the X-axis.
  41906. \param size_y The number of subdisivions along the Y-axis.
  41907. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  41908. \par Example
  41909. \code
  41910. const CImg<float> img("reference.jpg");
  41911. CImgList<unsigned int> faces3d;
  41912. const CImg<float> points3d = img.get_isoline3d(faces3d,100);
  41913. CImg<unsigned char>().display_object3d("Isoline3d",points3d,faces3d,colors3d);
  41914. \endcode
  41915. \image html ref_isoline3d.jpg
  41916. **/
  41917. template<typename tf>
  41918. CImg<floatT> get_isoline3d(CImgList<tf>& primitives, const float isovalue,
  41919. const int size_x=-100, const int size_y=-100) const {
  41920. if (_spectrum>1)
  41921. throw CImgInstanceException(_cimg_instance
  41922. "get_isoline3d(): Instance is not a scalar image.",
  41923. cimg_instance);
  41924. if (_depth>1)
  41925. throw CImgInstanceException(_cimg_instance
  41926. "get_isoline3d(): Instance is not a 2D image.",
  41927. cimg_instance);
  41928. primitives.assign();
  41929. if (is_empty()) return *this;
  41930. CImg<floatT> vertices;
  41931. if ((size_x==-100 && size_y==-100) || (size_x==width() && size_y==height())) {
  41932. const _functor2d_int func(*this);
  41933. vertices = isoline3d(primitives,func,isovalue,0,0,width() - 1.f,height() - 1.f,width(),height());
  41934. } else {
  41935. const _functor2d_float func(*this);
  41936. vertices = isoline3d(primitives,func,isovalue,0,0,width() - 1.f,height() - 1.f,size_x,size_y);
  41937. }
  41938. return vertices;
  41939. }
  41940. //! Compute isolines of a function, as a 3D object.
  41941. /**
  41942. \param[out] primitives Primitives data of the resulting 3D object.
  41943. \param func Elevation functor. Must have <tt>operator()(x,y)</tt> defined.
  41944. \param isovalue Isovalue to extract from function.
  41945. \param x0 X-coordinate of the starting point.
  41946. \param y0 Y-coordinate of the starting point.
  41947. \param x1 X-coordinate of the ending point.
  41948. \param y1 Y-coordinate of the ending point.
  41949. \param size_x Resolution of the function along the X-axis.
  41950. \param size_y Resolution of the function along the Y-axis.
  41951. \note Use the marching squares algorithm for extracting the isolines.
  41952. **/
  41953. template<typename tf, typename tfunc>
  41954. static CImg<floatT> isoline3d(CImgList<tf>& primitives, const tfunc& func, const float isovalue,
  41955. const float x0, const float y0, const float x1, const float y1,
  41956. const int size_x=256, const int size_y=256) {
  41957. CImgList<floatT> vertices;
  41958. primitives.assign();
  41959. typename CImg<floatT>::_functor_isoline3d add_vertex(vertices);
  41960. typename CImg<tf>::_functor_isoline3d add_segment(primitives);
  41961. isoline3d(add_vertex,add_segment,func,isovalue,x0,y0,x1,y1,size_x,size_y);
  41962. return vertices>'x';
  41963. }
  41964. //! Compute isolines of a function, as a 3D object.
  41965. /**
  41966. \param[out] add_vertex : Functor with operator()(x,y,z) defined for adding new vertex.
  41967. \param[out] add_segment : Functor with operator()(i,j) defined for adding new segment.
  41968. \param func Elevation function. Is of type <tt>float (*func)(const float x,const float y)</tt>.
  41969. \param isovalue Isovalue to extract from function.
  41970. \param x0 X-coordinate of the starting point.
  41971. \param y0 Y-coordinate of the starting point.
  41972. \param x1 X-coordinate of the ending point.
  41973. \param y1 Y-coordinate of the ending point.
  41974. \param size_x Resolution of the function along the X-axis.
  41975. \param size_y Resolution of the function along the Y-axis.
  41976. \note Use the marching squares algorithm for extracting the isolines.
  41977. **/
  41978. template<typename tv, typename tf, typename tfunc>
  41979. static void isoline3d(tv& add_vertex, tf& add_segment, const tfunc& func, const float isovalue,
  41980. const float x0, const float y0, const float x1, const float y1,
  41981. const int size_x, const int size_y) {
  41982. static const unsigned int edges[16] = { 0x0, 0x9, 0x3, 0xa, 0x6, 0xf, 0x5, 0xc, 0xc,
  41983. 0x5, 0xf, 0x6, 0xa, 0x3, 0x9, 0x0 };
  41984. static const int segments[16][4] = { { -1,-1,-1,-1 }, { 0,3,-1,-1 }, { 0,1,-1,-1 }, { 1,3,-1,-1 },
  41985. { 1,2,-1,-1 }, { 0,1,2,3 }, { 0,2,-1,-1 }, { 2,3,-1,-1 },
  41986. { 2,3,-1,-1 }, { 0,2,-1,-1}, { 0,3,1,2 }, { 1,2,-1,-1 },
  41987. { 1,3,-1,-1 }, { 0,1,-1,-1}, { 0,3,-1,-1}, { -1,-1,-1,-1 } };
  41988. const unsigned int
  41989. _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)),
  41990. _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)),
  41991. nx = _nx?_nx:1,
  41992. ny = _ny?_ny:1,
  41993. nxm1 = nx - 1,
  41994. nym1 = ny - 1;
  41995. if (!nxm1 || !nym1) return;
  41996. const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1;
  41997. CImg<intT> indices1(nx,1,1,2,-1), indices2(nx,1,1,2);
  41998. CImg<floatT> values1(nx), values2(nx);
  41999. float X = x0, Y = y0, nX = X + dx, nY = Y + dy;
  42000. int nb_vertices = 0;
  42001. // Fill first line with values
  42002. cimg_forX(values1,x) { values1(x) = (float)func(X,Y); X+=dx; }
  42003. // Run the marching squares algorithm
  42004. for (unsigned int yi = 0, nyi = 1; yi<nym1; ++yi, ++nyi, Y=nY, nY+=dy) {
  42005. X = x0; nX = X + dx;
  42006. indices2.fill(-1);
  42007. values2(0) = (float)func(X,nY);
  42008. for (unsigned int xi = 0, nxi = 1; xi<nxm1; ++xi, ++nxi, X=nX, nX+=dx) {
  42009. // Determine square configuration
  42010. const float
  42011. val0 = values1(xi),
  42012. val1 = values1(nxi),
  42013. val2 = values2(nxi) = (float)func(nX,nY),
  42014. val3 = values2(xi);
  42015. const unsigned int
  42016. configuration = (val0<isovalue?1U:0U) | (val1<isovalue?2U:0U) |
  42017. (val2<isovalue?4U:0U) | (val3<isovalue?8U:0U),
  42018. edge = edges[configuration];
  42019. // Compute intersection vertices
  42020. if (edge) {
  42021. if ((edge&1) && indices1(xi,0)<0) {
  42022. const float Xi = X + (isovalue-val0)*dx/(val1-val0);
  42023. indices1(xi,0) = nb_vertices++;
  42024. add_vertex(Xi,Y,0.0f);
  42025. }
  42026. if ((edge&2) && indices1(nxi,1)<0) {
  42027. const float Yi = Y + (isovalue-val1)*dy/(val2-val1);
  42028. indices1(nxi,1) = nb_vertices++;
  42029. add_vertex(nX,Yi,0.0f);
  42030. }
  42031. if ((edge&4) && indices2(xi,0)<0) {
  42032. const float Xi = X + (isovalue-val3)*dx/(val2-val3);
  42033. indices2(xi,0) = nb_vertices++;
  42034. add_vertex(Xi,nY,0.0f);
  42035. }
  42036. if ((edge&8) && indices1(xi,1)<0) {
  42037. const float Yi = Y + (isovalue-val0)*dy/(val3-val0);
  42038. indices1(xi,1) = nb_vertices++;
  42039. add_vertex(X,Yi,0.0f);
  42040. }
  42041. // Create segments
  42042. for (const int *segment = segments[configuration]; *segment!=-1; ) {
  42043. const unsigned int p0 = (unsigned int)*(segment++), p1 = (unsigned int)*(segment++);
  42044. const int
  42045. i0 = _isoline3d_index(p0,indices1,indices2,xi,nxi),
  42046. i1 = _isoline3d_index(p1,indices1,indices2,xi,nxi);
  42047. add_segment(i0,i1);
  42048. }
  42049. }
  42050. }
  42051. values1.swap(values2);
  42052. indices1.swap(indices2);
  42053. }
  42054. }
  42055. //! Compute isolines of a function, as a 3D object \overloading.
  42056. template<typename tf>
  42057. static CImg<floatT> isoline3d(CImgList<tf>& primitives, const char *const expression, const float isovalue,
  42058. const float x0, const float y0, const float x1, const float y1,
  42059. const int size_x=256, const int size_y=256) {
  42060. const _functor2d_expr func(expression);
  42061. return isoline3d(primitives,func,isovalue,x0,y0,x1,y1,size_x,size_y);
  42062. }
  42063. template<typename t>
  42064. static int _isoline3d_index(const unsigned int edge, const CImg<t>& indices1, const CImg<t>& indices2,
  42065. const unsigned int x, const unsigned int nx) {
  42066. switch (edge) {
  42067. case 0 : return (int)indices1(x,0);
  42068. case 1 : return (int)indices1(nx,1);
  42069. case 2 : return (int)indices2(x,0);
  42070. case 3 : return (int)indices1(x,1);
  42071. }
  42072. return 0;
  42073. }
  42074. //! Generate an isosurface of the image instance as a 3D object.
  42075. /**
  42076. \param[out] primitives The returned list of the 3D object primitives
  42077. (template type \e tf should be at least \e unsigned \e int).
  42078. \param isovalue The returned list of the 3D object colors.
  42079. \param size_x Number of subdivisions along the X-axis.
  42080. \param size_y Number of subdisivions along the Y-axis.
  42081. \param size_z Number of subdisivions along the Z-axis.
  42082. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42083. \par Example
  42084. \code
  42085. const CImg<float> img = CImg<unsigned char>("reference.jpg").resize(-100,-100,20);
  42086. CImgList<unsigned int> faces3d;
  42087. const CImg<float> points3d = img.get_isosurface3d(faces3d,100);
  42088. CImg<unsigned char>().display_object3d("Isosurface3d",points3d,faces3d,colors3d);
  42089. \endcode
  42090. \image html ref_isosurface3d.jpg
  42091. **/
  42092. template<typename tf>
  42093. CImg<floatT> get_isosurface3d(CImgList<tf>& primitives, const float isovalue,
  42094. const int size_x=-100, const int size_y=-100, const int size_z=-100) const {
  42095. if (_spectrum>1)
  42096. throw CImgInstanceException(_cimg_instance
  42097. "get_isosurface3d(): Instance is not a scalar image.",
  42098. cimg_instance);
  42099. primitives.assign();
  42100. if (is_empty()) return *this;
  42101. CImg<floatT> vertices;
  42102. if ((size_x==-100 && size_y==-100 && size_z==-100) || (size_x==width() && size_y==height() && size_z==depth())) {
  42103. const _functor3d_int func(*this);
  42104. vertices = isosurface3d(primitives,func,isovalue,0,0,0,width() - 1.f,height() - 1.f,depth() - 1.f,
  42105. width(),height(),depth());
  42106. } else {
  42107. const _functor3d_float func(*this);
  42108. vertices = isosurface3d(primitives,func,isovalue,0,0,0,width() - 1.f,height() - 1.f,depth() - 1.f,
  42109. size_x,size_y,size_z);
  42110. }
  42111. return vertices;
  42112. }
  42113. //! Compute isosurface of a function, as a 3D object.
  42114. /**
  42115. \param[out] primitives Primitives data of the resulting 3D object.
  42116. \param func Implicit function. Is of type <tt>float (*func)(const float x, const float y, const float z)</tt>.
  42117. \param isovalue Isovalue to extract.
  42118. \param x0 X-coordinate of the starting point.
  42119. \param y0 Y-coordinate of the starting point.
  42120. \param z0 Z-coordinate of the starting point.
  42121. \param x1 X-coordinate of the ending point.
  42122. \param y1 Y-coordinate of the ending point.
  42123. \param z1 Z-coordinate of the ending point.
  42124. \param size_x Resolution of the elevation function along the X-axis.
  42125. \param size_y Resolution of the elevation function along the Y-axis.
  42126. \param size_z Resolution of the elevation function along the Z-axis.
  42127. \note Use the marching cubes algorithm for extracting the isosurface.
  42128. **/
  42129. template<typename tf, typename tfunc>
  42130. static CImg<floatT> isosurface3d(CImgList<tf>& primitives, const tfunc& func, const float isovalue,
  42131. const float x0, const float y0, const float z0,
  42132. const float x1, const float y1, const float z1,
  42133. const int size_x=32, const int size_y=32, const int size_z=32) {
  42134. CImgList<floatT> vertices;
  42135. primitives.assign();
  42136. typename CImg<floatT>::_functor_isosurface3d add_vertex(vertices);
  42137. typename CImg<tf>::_functor_isosurface3d add_triangle(primitives);
  42138. isosurface3d(add_vertex,add_triangle,func,isovalue,x0,y0,z0,x1,y1,z1,size_x,size_y,size_z);
  42139. return vertices>'x';
  42140. }
  42141. //! Compute isosurface of a function, as a 3D object.
  42142. /**
  42143. \param[out] add_vertex : Functor with operator()(x,y,z) defined for adding new vertex.
  42144. \param[out] add_triangle : Functor with operator()(i,j) defined for adding new segment.
  42145. \param func Implicit function. Is of type <tt>float (*func)(const float x, const float y, const float z)</tt>.
  42146. \param isovalue Isovalue to extract.
  42147. \param x0 X-coordinate of the starting point.
  42148. \param y0 Y-coordinate of the starting point.
  42149. \param z0 Z-coordinate of the starting point.
  42150. \param x1 X-coordinate of the ending point.
  42151. \param y1 Y-coordinate of the ending point.
  42152. \param z1 Z-coordinate of the ending point.
  42153. \param size_x Resolution of the elevation function along the X-axis.
  42154. \param size_y Resolution of the elevation function along the Y-axis.
  42155. \param size_z Resolution of the elevation function along the Z-axis.
  42156. \note Use the marching cubes algorithm for extracting the isosurface.
  42157. **/
  42158. template<typename tv, typename tf, typename tfunc>
  42159. static void isosurface3d(tv& add_vertex, tf& add_triangle, const tfunc& func, const float isovalue,
  42160. const float x0, const float y0, const float z0,
  42161. const float x1, const float y1, const float z1,
  42162. const int size_x, const int size_y, const int size_z) {
  42163. static const unsigned int edges[256] = {
  42164. 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
  42165. 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
  42166. 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
  42167. 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
  42168. 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
  42169. 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
  42170. 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
  42171. 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
  42172. 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
  42173. 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
  42174. 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
  42175. 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
  42176. 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
  42177. 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
  42178. 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
  42179. 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000
  42180. };
  42181. static const int triangles[256][16] = {
  42182. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42183. { 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42184. { 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42185. { 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42186. { 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42187. { 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42188. { 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42189. { 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1 },
  42190. { 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42191. { 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42192. { 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42193. { 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1 },
  42194. { 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42195. { 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1 },
  42196. { 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1 },
  42197. { 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42198. { 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42199. { 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42200. { 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42201. { 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1 },
  42202. { 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42203. { 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1 },
  42204. { 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 },
  42205. { 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1 },
  42206. { 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42207. { 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1 },
  42208. { 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 },
  42209. { 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1 },
  42210. { 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1 },
  42211. { 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1 },
  42212. { 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1 },
  42213. { 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1 },
  42214. { 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42215. { 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42216. { 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42217. { 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1 },
  42218. { 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42219. { 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 },
  42220. { 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1 },
  42221. { 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1 },
  42222. { 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42223. { 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 },
  42224. { 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 },
  42225. { 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1 },
  42226. { 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1 },
  42227. { 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1 },
  42228. { 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1 },
  42229. { 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1 },
  42230. { 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42231. { 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1 },
  42232. { 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1 },
  42233. { 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42234. { 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1 },
  42235. { 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1 },
  42236. { 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1 },
  42237. { 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1 },
  42238. { 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1 },
  42239. { 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1 },
  42240. { 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1 },
  42241. { 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1 },
  42242. { 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1 },
  42243. { 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1 },
  42244. { 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1 },
  42245. { 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42246. { 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42247. { 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42248. { 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42249. { 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 },
  42250. { 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42251. { 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1 },
  42252. { 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1 },
  42253. { 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1 },
  42254. { 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42255. { 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 },
  42256. { 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 },
  42257. { 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1 },
  42258. { 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1 },
  42259. { 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1 },
  42260. { 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1 },
  42261. { 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1 },
  42262. { 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42263. { 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1 },
  42264. { 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 },
  42265. { 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1 },
  42266. { 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1 },
  42267. { 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1 },
  42268. { 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1 },
  42269. { 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1 },
  42270. { 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 },
  42271. { 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1 },
  42272. { 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1 },
  42273. { 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1 },
  42274. { 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1 },
  42275. { 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1 },
  42276. { 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1 },
  42277. { 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1 },
  42278. { 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42279. { 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1 },
  42280. { 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1 },
  42281. { 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1 },
  42282. { 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1 },
  42283. { 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1 },
  42284. { 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42285. { 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1 },
  42286. { 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1 },
  42287. { 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1 },
  42288. { 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1 },
  42289. { 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1 },
  42290. { 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1 },
  42291. { 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1 },
  42292. { 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1 },
  42293. { 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42294. { 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1 },
  42295. { 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1 },
  42296. { 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1 },
  42297. { 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1 },
  42298. { 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1 },
  42299. { 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1 },
  42300. { 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1 },
  42301. { 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42302. { 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1 },
  42303. { 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1 },
  42304. { 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1 },
  42305. { 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1 },
  42306. { 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1 },
  42307. { 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42308. { 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1 },
  42309. { 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42310. { 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42311. { 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42312. { 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42313. { 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 },
  42314. { 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42315. { 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 },
  42316. { 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 },
  42317. { 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1 },
  42318. { 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42319. { 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1 },
  42320. { 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1 },
  42321. { 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1 },
  42322. { 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1 },
  42323. { 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1 },
  42324. { 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1 },
  42325. { 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1 },
  42326. { 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42327. { 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1 },
  42328. { 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  42329. { 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1 },
  42330. { 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1 },
  42331. { 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1 },
  42332. { 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1 },
  42333. { 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1 },
  42334. { 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1 },
  42335. { 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42336. { 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1 },
  42337. { 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 },
  42338. { 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1 },
  42339. { 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1 },
  42340. { 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1 },
  42341. { 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42342. { 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42343. { 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 },
  42344. { 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 },
  42345. { 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1 },
  42346. { 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 },
  42347. { 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1 },
  42348. { 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1 },
  42349. { 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1 },
  42350. { 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1 },
  42351. { 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1 },
  42352. { 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1 },
  42353. { 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1 },
  42354. { 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1 },
  42355. { 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1 },
  42356. { 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1 },
  42357. { 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1 },
  42358. { 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1 },
  42359. { 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1 },
  42360. { 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1 },
  42361. { 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1 },
  42362. { 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1 },
  42363. { 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1 },
  42364. { 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1 },
  42365. { 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1 },
  42366. { 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1 },
  42367. { 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1 },
  42368. { 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1 },
  42369. { 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42370. { 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1 },
  42371. { 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1 },
  42372. { 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42373. { 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42374. { 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42375. { 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1 },
  42376. { 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1 },
  42377. { 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1 },
  42378. { 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1 },
  42379. { 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1 },
  42380. { 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1 },
  42381. { 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1 },
  42382. { 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1 },
  42383. { 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1 },
  42384. { 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1 },
  42385. { 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1 },
  42386. { 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42387. { 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1 },
  42388. { 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1 },
  42389. { 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42390. { 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1 },
  42391. { 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1 },
  42392. { 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1 },
  42393. { 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1 },
  42394. { 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1 },
  42395. { 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1 },
  42396. { 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1 },
  42397. { 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42398. { 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1 },
  42399. { 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1 },
  42400. { 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1 },
  42401. { 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1 },
  42402. { 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1 },
  42403. { 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42404. { 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1 },
  42405. { 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42406. { 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1 },
  42407. { 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1 },
  42408. { 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1 },
  42409. { 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1 },
  42410. { 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1 },
  42411. { 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1 },
  42412. { 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1 },
  42413. { 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1 },
  42414. { 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1 },
  42415. { 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1 },
  42416. { 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1 },
  42417. { 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42418. { 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1 },
  42419. { 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1 },
  42420. { 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42421. { 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42422. { 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42423. { 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1 },
  42424. { 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1 },
  42425. { 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42426. { 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1 },
  42427. { 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1 },
  42428. { 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42429. { 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42430. { 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1 },
  42431. { 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42432. { 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1 },
  42433. { 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42434. { 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42435. { 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42436. { 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  42437. { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
  42438. };
  42439. const unsigned int
  42440. _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)),
  42441. _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)),
  42442. _nz = (unsigned int)(size_z>=0?size_z:cimg::round((z1-z0)*-size_z/100 + 1)),
  42443. nx = _nx?_nx:1,
  42444. ny = _ny?_ny:1,
  42445. nz = _nz?_nz:1,
  42446. nxm1 = nx - 1,
  42447. nym1 = ny - 1,
  42448. nzm1 = nz - 1;
  42449. if (!nxm1 || !nym1 || !nzm1) return;
  42450. const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1, dz = (z1 - z0)/nzm1;
  42451. CImg<intT> indices1(nx,ny,1,3,-1), indices2(indices1);
  42452. CImg<floatT> values1(nx,ny), values2(nx,ny);
  42453. float X = 0, Y = 0, Z = 0, nX = 0, nY = 0, nZ = 0;
  42454. int nb_vertices = 0;
  42455. // Fill the first plane with function values
  42456. Y = y0;
  42457. cimg_forY(values1,y) {
  42458. X = x0;
  42459. cimg_forX(values1,x) { values1(x,y) = (float)func(X,Y,z0); X+=dx; }
  42460. Y+=dy;
  42461. }
  42462. // Run Marching Cubes algorithm
  42463. Z = z0; nZ = Z + dz;
  42464. for (unsigned int zi = 0; zi<nzm1; ++zi, Z = nZ, nZ+=dz) {
  42465. Y = y0; nY = Y + dy;
  42466. indices2.fill(-1);
  42467. X = x0; for (unsigned int xi = 0; xi<nx; ++xi) { values2(xi,0) = (float)func(X,Y,nZ); X += dx; }
  42468. for (unsigned int yi = 0, nyi = 1; yi<nym1; ++yi, ++nyi, Y = nY, nY+=dy) {
  42469. X = x0; nX = X + dx;
  42470. values2(0,nyi) = (float)func(X,nY,nZ);
  42471. for (unsigned int xi = 0, nxi = 1; xi<nxm1; ++xi, ++nxi, X = nX, nX+=dx) {
  42472. // Determine cube configuration
  42473. const float
  42474. val0 = values1(xi,yi),
  42475. val1 = values1(nxi,yi),
  42476. val2 = values1(nxi,nyi),
  42477. val3 = values1(xi,nyi),
  42478. val4 = values2(xi,yi),
  42479. val5 = values2(nxi,yi),
  42480. val6 = values2(nxi,nyi) = (float)func(nX,nY,nZ),
  42481. val7 = values2(xi,nyi);
  42482. const unsigned int configuration =
  42483. (val0<isovalue?1U:0U) | (val1<isovalue?2U:0U) | (val2<isovalue?4U:0U) | (val3<isovalue?8U:0U) |
  42484. (val4<isovalue?16U:0U) | (val5<isovalue?32U:0U) | (val6<isovalue?64U:0U) | (val7<isovalue?128U:0U),
  42485. edge = edges[configuration];
  42486. // Compute intersection vertices
  42487. if (edge) {
  42488. if ((edge&1) && indices1(xi,yi,0)<0) {
  42489. const float Xi = X + (isovalue-val0)*dx/(val1-val0);
  42490. indices1(xi,yi,0) = nb_vertices++;
  42491. add_vertex(Xi,Y,Z);
  42492. }
  42493. if ((edge&2) && indices1(nxi,yi,1)<0) {
  42494. const float Yi = Y + (isovalue-val1)*dy/(val2-val1);
  42495. indices1(nxi,yi,1) = nb_vertices++;
  42496. add_vertex(nX,Yi,Z);
  42497. }
  42498. if ((edge&4) && indices1(xi,nyi,0)<0) {
  42499. const float Xi = X + (isovalue-val3)*dx/(val2-val3);
  42500. indices1(xi,nyi,0) = nb_vertices++;
  42501. add_vertex(Xi,nY,Z);
  42502. }
  42503. if ((edge&8) && indices1(xi,yi,1)<0) {
  42504. const float Yi = Y + (isovalue-val0)*dy/(val3-val0);
  42505. indices1(xi,yi,1) = nb_vertices++;
  42506. add_vertex(X,Yi,Z);
  42507. }
  42508. if ((edge&16) && indices2(xi,yi,0)<0) {
  42509. const float Xi = X + (isovalue-val4)*dx/(val5-val4);
  42510. indices2(xi,yi,0) = nb_vertices++;
  42511. add_vertex(Xi,Y,nZ);
  42512. }
  42513. if ((edge&32) && indices2(nxi,yi,1)<0) {
  42514. const float Yi = Y + (isovalue-val5)*dy/(val6-val5);
  42515. indices2(nxi,yi,1) = nb_vertices++;
  42516. add_vertex(nX,Yi,nZ);
  42517. }
  42518. if ((edge&64) && indices2(xi,nyi,0)<0) {
  42519. const float Xi = X + (isovalue-val7)*dx/(val6-val7);
  42520. indices2(xi,nyi,0) = nb_vertices++;
  42521. add_vertex(Xi,nY,nZ);
  42522. }
  42523. if ((edge&128) && indices2(xi,yi,1)<0) {
  42524. const float Yi = Y + (isovalue-val4)*dy/(val7-val4);
  42525. indices2(xi,yi,1) = nb_vertices++;
  42526. add_vertex(X,Yi,nZ);
  42527. }
  42528. if ((edge&256) && indices1(xi,yi,2)<0) {
  42529. const float Zi = Z+ (isovalue-val0)*dz/(val4-val0);
  42530. indices1(xi,yi,2) = nb_vertices++;
  42531. add_vertex(X,Y,Zi);
  42532. }
  42533. if ((edge&512) && indices1(nxi,yi,2)<0) {
  42534. const float Zi = Z + (isovalue-val1)*dz/(val5-val1);
  42535. indices1(nxi,yi,2) = nb_vertices++;
  42536. add_vertex(nX,Y,Zi);
  42537. }
  42538. if ((edge&1024) && indices1(nxi,nyi,2)<0) {
  42539. const float Zi = Z + (isovalue-val2)*dz/(val6-val2);
  42540. indices1(nxi,nyi,2) = nb_vertices++;
  42541. add_vertex(nX,nY,Zi);
  42542. }
  42543. if ((edge&2048) && indices1(xi,nyi,2)<0) {
  42544. const float Zi = Z + (isovalue-val3)*dz/(val7-val3);
  42545. indices1(xi,nyi,2) = nb_vertices++;
  42546. add_vertex(X,nY,Zi);
  42547. }
  42548. // Create triangles
  42549. for (const int *triangle = triangles[configuration]; *triangle!=-1; ) {
  42550. const unsigned int
  42551. p0 = (unsigned int)*(triangle++),
  42552. p1 = (unsigned int)*(triangle++),
  42553. p2 = (unsigned int)*(triangle++);
  42554. const int
  42555. i0 = _isosurface3d_index(p0,indices1,indices2,xi,yi,nxi,nyi),
  42556. i1 = _isosurface3d_index(p1,indices1,indices2,xi,yi,nxi,nyi),
  42557. i2 = _isosurface3d_index(p2,indices1,indices2,xi,yi,nxi,nyi);
  42558. add_triangle(i0,i2,i1);
  42559. }
  42560. }
  42561. }
  42562. }
  42563. cimg::swap(values1,values2);
  42564. cimg::swap(indices1,indices2);
  42565. }
  42566. }
  42567. //! Compute isosurface of a function, as a 3D object \overloading.
  42568. template<typename tf>
  42569. static CImg<floatT> isosurface3d(CImgList<tf>& primitives, const char *const expression, const float isovalue,
  42570. const float x0, const float y0, const float z0,
  42571. const float x1, const float y1, const float z1,
  42572. const int dx=32, const int dy=32, const int dz=32) {
  42573. const _functor3d_expr func(expression);
  42574. return isosurface3d(primitives,func,isovalue,x0,y0,z0,x1,y1,z1,dx,dy,dz);
  42575. }
  42576. template<typename t>
  42577. static int _isosurface3d_index(const unsigned int edge, const CImg<t>& indices1, const CImg<t>& indices2,
  42578. const unsigned int x, const unsigned int y,
  42579. const unsigned int nx, const unsigned int ny) {
  42580. switch (edge) {
  42581. case 0 : return indices1(x,y,0);
  42582. case 1 : return indices1(nx,y,1);
  42583. case 2 : return indices1(x,ny,0);
  42584. case 3 : return indices1(x,y,1);
  42585. case 4 : return indices2(x,y,0);
  42586. case 5 : return indices2(nx,y,1);
  42587. case 6 : return indices2(x,ny,0);
  42588. case 7 : return indices2(x,y,1);
  42589. case 8 : return indices1(x,y,2);
  42590. case 9 : return indices1(nx,y,2);
  42591. case 10 : return indices1(nx,ny,2);
  42592. case 11 : return indices1(x,ny,2);
  42593. }
  42594. return 0;
  42595. }
  42596. // Define functors for accessing image values (used in previous functions).
  42597. struct _functor2d_int {
  42598. const CImg<T>& ref;
  42599. _functor2d_int(const CImg<T>& pref):ref(pref) {}
  42600. float operator()(const float x, const float y) const {
  42601. return (float)ref((int)x,(int)y);
  42602. }
  42603. };
  42604. struct _functor2d_float {
  42605. const CImg<T>& ref;
  42606. _functor2d_float(const CImg<T>& pref):ref(pref) {}
  42607. float operator()(const float x, const float y) const {
  42608. return (float)ref._linear_atXY(x,y);
  42609. }
  42610. };
  42611. struct _functor2d_expr {
  42612. _cimg_math_parser *mp;
  42613. ~_functor2d_expr() { mp->end(); delete mp; }
  42614. _functor2d_expr(const char *const expr):mp(0) {
  42615. mp = new _cimg_math_parser(expr,0,CImg<T>::const_empty(),0);
  42616. }
  42617. float operator()(const float x, const float y) const {
  42618. return (float)(*mp)(x,y,0,0);
  42619. }
  42620. };
  42621. struct _functor3d_int {
  42622. const CImg<T>& ref;
  42623. _functor3d_int(const CImg<T>& pref):ref(pref) {}
  42624. float operator()(const float x, const float y, const float z) const {
  42625. return (float)ref((int)x,(int)y,(int)z);
  42626. }
  42627. };
  42628. struct _functor3d_float {
  42629. const CImg<T>& ref;
  42630. _functor3d_float(const CImg<T>& pref):ref(pref) {}
  42631. float operator()(const float x, const float y, const float z) const {
  42632. return (float)ref._linear_atXYZ(x,y,z);
  42633. }
  42634. };
  42635. struct _functor3d_expr {
  42636. _cimg_math_parser *mp;
  42637. ~_functor3d_expr() { mp->end(); delete mp; }
  42638. _functor3d_expr(const char *const expr):mp(0) {
  42639. mp = new _cimg_math_parser(expr,0,CImg<T>::const_empty(),0);
  42640. }
  42641. float operator()(const float x, const float y, const float z) const {
  42642. return (float)(*mp)(x,y,z,0);
  42643. }
  42644. };
  42645. struct _functor4d_int {
  42646. const CImg<T>& ref;
  42647. _functor4d_int(const CImg<T>& pref):ref(pref) {}
  42648. float operator()(const float x, const float y, const float z, const unsigned int c) const {
  42649. return (float)ref((int)x,(int)y,(int)z,c);
  42650. }
  42651. };
  42652. struct _functor_isoline3d {
  42653. CImgList<T>& list;
  42654. _functor_isoline3d(CImgList<T>& _list):list(_list) {}
  42655. template<typename t>
  42656. void operator()(const t x, const t y, const t z) { CImg<T>::vector((T)x,(T)y,(T)z).move_to(list); }
  42657. template<typename t>
  42658. void operator()(const t i, const t j) { CImg<T>::vector((T)i,(T)j).move_to(list); }
  42659. };
  42660. struct _functor_isosurface3d {
  42661. CImgList<T>& list;
  42662. _functor_isosurface3d(CImgList<T>& _list):list(_list) {}
  42663. template<typename t>
  42664. void operator()(const t x, const t y, const t z) { CImg<T>::vector((T)x,(T)y,(T)z).move_to(list); }
  42665. };
  42666. //! Compute 3D elevation of a function as a 3D object.
  42667. /**
  42668. \param[out] primitives Primitives data of the resulting 3D object.
  42669. \param func Elevation function. Is of type <tt>float (*func)(const float x,const float y)</tt>.
  42670. \param x0 X-coordinate of the starting point.
  42671. \param y0 Y-coordinate of the starting point.
  42672. \param x1 X-coordinate of the ending point.
  42673. \param y1 Y-coordinate of the ending point.
  42674. \param size_x Resolution of the function along the X-axis.
  42675. \param size_y Resolution of the function along the Y-axis.
  42676. **/
  42677. template<typename tf, typename tfunc>
  42678. static CImg<floatT> elevation3d(CImgList<tf>& primitives, const tfunc& func,
  42679. const float x0, const float y0, const float x1, const float y1,
  42680. const int size_x=256, const int size_y=256) {
  42681. const float
  42682. nx0 = x0<x1?x0:x1, ny0 = y0<y1?y0:y1,
  42683. nx1 = x0<x1?x1:x0, ny1 = y0<y1?y1:y0;
  42684. const unsigned int
  42685. _nsize_x = (unsigned int)(size_x>=0?size_x:(nx1-nx0)*-size_x/100),
  42686. nsize_x = _nsize_x?_nsize_x:1, nsize_x1 = nsize_x - 1,
  42687. _nsize_y = (unsigned int)(size_y>=0?size_y:(ny1-ny0)*-size_y/100),
  42688. nsize_y = _nsize_y?_nsize_y:1, nsize_y1 = nsize_y - 1;
  42689. if (nsize_x<2 || nsize_y<2)
  42690. throw CImgArgumentException("CImg<%s>::elevation3d(): Invalid specified size (%d,%d).",
  42691. pixel_type(),
  42692. nsize_x,nsize_y);
  42693. CImg<floatT> vertices(nsize_x*nsize_y,3);
  42694. floatT *ptr_x = vertices.data(0,0), *ptr_y = vertices.data(0,1), *ptr_z = vertices.data(0,2);
  42695. for (unsigned int y = 0; y<nsize_y; ++y) {
  42696. const float Y = ny0 + y*(ny1-ny0)/nsize_y1;
  42697. for (unsigned int x = 0; x<nsize_x; ++x) {
  42698. const float X = nx0 + x*(nx1-nx0)/nsize_x1;
  42699. *(ptr_x++) = (float)x;
  42700. *(ptr_y++) = (float)y;
  42701. *(ptr_z++) = (float)func(X,Y);
  42702. }
  42703. }
  42704. primitives.assign(nsize_x1*nsize_y1,1,4);
  42705. for (unsigned int p = 0, y = 0; y<nsize_y1; ++y) {
  42706. const unsigned int yw = y*nsize_x;
  42707. for (unsigned int x = 0; x<nsize_x1; ++x) {
  42708. const unsigned int xpyw = x + yw, xpyww = xpyw + nsize_x;
  42709. primitives[p++].fill(xpyw,xpyww,xpyww + 1,xpyw + 1);
  42710. }
  42711. }
  42712. return vertices;
  42713. }
  42714. //! Compute 3D elevation of a function, as a 3D object \overloading.
  42715. template<typename tf>
  42716. static CImg<floatT> elevation3d(CImgList<tf>& primitives, const char *const expression,
  42717. const float x0, const float y0, const float x1, const float y1,
  42718. const int size_x=256, const int size_y=256) {
  42719. const _functor2d_expr func(expression);
  42720. return elevation3d(primitives,func,x0,y0,x1,y1,size_x,size_y);
  42721. }
  42722. //! Generate a 3D box object.
  42723. /**
  42724. \param[out] primitives The returned list of the 3D object primitives
  42725. (template type \e tf should be at least \e unsigned \e int).
  42726. \param size_x The width of the box (dimension along the X-axis).
  42727. \param size_y The height of the box (dimension along the Y-axis).
  42728. \param size_z The depth of the box (dimension along the Z-axis).
  42729. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42730. \par Example
  42731. \code
  42732. CImgList<unsigned int> faces3d;
  42733. const CImg<float> points3d = CImg<float>::box3d(faces3d,10,20,30);
  42734. CImg<unsigned char>().display_object3d("Box3d",points3d,faces3d);
  42735. \endcode
  42736. \image html ref_box3d.jpg
  42737. **/
  42738. template<typename tf>
  42739. static CImg<floatT> box3d(CImgList<tf>& primitives,
  42740. const float size_x=200, const float size_y=100, const float size_z=100) {
  42741. primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 0,1,5,4, 3,7,6,2, 0,4,7,3, 1,2,6,5);
  42742. return CImg<floatT>(8,3,1,1,
  42743. 0.,size_x,size_x, 0., 0.,size_x,size_x, 0.,
  42744. 0., 0.,size_y,size_y, 0., 0.,size_y,size_y,
  42745. 0., 0., 0., 0.,size_z,size_z,size_z,size_z);
  42746. }
  42747. //! Generate a 3D cone.
  42748. /**
  42749. \param[out] primitives The returned list of the 3D object primitives
  42750. (template type \e tf should be at least \e unsigned \e int).
  42751. \param radius The radius of the cone basis.
  42752. \param size_z The cone's height.
  42753. \param subdivisions The number of basis angular subdivisions.
  42754. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42755. \par Example
  42756. \code
  42757. CImgList<unsigned int> faces3d;
  42758. const CImg<float> points3d = CImg<float>::cone3d(faces3d,50);
  42759. CImg<unsigned char>().display_object3d("Cone3d",points3d,faces3d);
  42760. \endcode
  42761. \image html ref_cone3d.jpg
  42762. **/
  42763. template<typename tf>
  42764. static CImg<floatT> cone3d(CImgList<tf>& primitives,
  42765. const float radius=50, const float size_z=100, const unsigned int subdivisions=24) {
  42766. primitives.assign();
  42767. if (!subdivisions) return CImg<floatT>();
  42768. CImgList<floatT> vertices(2,1,3,1,1,
  42769. 0.,0.,size_z,
  42770. 0.,0.,0.);
  42771. for (float delta = 360.f/subdivisions, angle = 0; angle<360; angle+=delta) {
  42772. const float a = (float)(angle*cimg::PI/180);
  42773. CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0).move_to(vertices);
  42774. }
  42775. const unsigned int nbr = vertices._width - 2;
  42776. for (unsigned int p = 0; p<nbr; ++p) {
  42777. const unsigned int curr = 2 + p, next = 2 + ((p + 1)%nbr);
  42778. CImg<tf>::vector(1,next,curr).move_to(primitives);
  42779. CImg<tf>::vector(0,curr,next).move_to(primitives);
  42780. }
  42781. return vertices>'x';
  42782. }
  42783. //! Generate a 3D cylinder.
  42784. /**
  42785. \param[out] primitives The returned list of the 3D object primitives
  42786. (template type \e tf should be at least \e unsigned \e int).
  42787. \param radius The radius of the cylinder basis.
  42788. \param size_z The cylinder's height.
  42789. \param subdivisions The number of basis angular subdivisions.
  42790. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42791. \par Example
  42792. \code
  42793. CImgList<unsigned int> faces3d;
  42794. const CImg<float> points3d = CImg<float>::cylinder3d(faces3d,50);
  42795. CImg<unsigned char>().display_object3d("Cylinder3d",points3d,faces3d);
  42796. \endcode
  42797. \image html ref_cylinder3d.jpg
  42798. **/
  42799. template<typename tf>
  42800. static CImg<floatT> cylinder3d(CImgList<tf>& primitives,
  42801. const float radius=50, const float size_z=100, const unsigned int subdivisions=24) {
  42802. primitives.assign();
  42803. if (!subdivisions) return CImg<floatT>();
  42804. CImgList<floatT> vertices(2,1,3,1,1,
  42805. 0.,0.,0.,
  42806. 0.,0.,size_z);
  42807. for (float delta = 360.f/subdivisions, angle = 0; angle<360; angle+=delta) {
  42808. const float a = (float)(angle*cimg::PI/180);
  42809. CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0.f).move_to(vertices);
  42810. CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),size_z).move_to(vertices);
  42811. }
  42812. const unsigned int nbr = (vertices._width - 2)/2;
  42813. for (unsigned int p = 0; p<nbr; ++p) {
  42814. const unsigned int curr = 2 + 2*p, next = 2 + (2*((p + 1)%nbr));
  42815. CImg<tf>::vector(0,next,curr).move_to(primitives);
  42816. CImg<tf>::vector(1,curr + 1,next + 1).move_to(primitives);
  42817. CImg<tf>::vector(curr,next,next + 1,curr + 1).move_to(primitives);
  42818. }
  42819. return vertices>'x';
  42820. }
  42821. //! Generate a 3D torus.
  42822. /**
  42823. \param[out] primitives The returned list of the 3D object primitives
  42824. (template type \e tf should be at least \e unsigned \e int).
  42825. \param radius1 The large radius.
  42826. \param radius2 The small radius.
  42827. \param subdivisions1 The number of angular subdivisions for the large radius.
  42828. \param subdivisions2 The number of angular subdivisions for the small radius.
  42829. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42830. \par Example
  42831. \code
  42832. CImgList<unsigned int> faces3d;
  42833. const CImg<float> points3d = CImg<float>::torus3d(faces3d,20,4);
  42834. CImg<unsigned char>().display_object3d("Torus3d",points3d,faces3d);
  42835. \endcode
  42836. \image html ref_torus3d.jpg
  42837. **/
  42838. template<typename tf>
  42839. static CImg<floatT> torus3d(CImgList<tf>& primitives,
  42840. const float radius1=100, const float radius2=30,
  42841. const unsigned int subdivisions1=24, const unsigned int subdivisions2=12) {
  42842. primitives.assign();
  42843. if (!subdivisions1 || !subdivisions2) return CImg<floatT>();
  42844. CImgList<floatT> vertices;
  42845. for (unsigned int v = 0; v<subdivisions1; ++v) {
  42846. const float
  42847. beta = (float)(v*2*cimg::PI/subdivisions1),
  42848. xc = radius1*(float)std::cos(beta),
  42849. yc = radius1*(float)std::sin(beta);
  42850. for (unsigned int u = 0; u<subdivisions2; ++u) {
  42851. const float
  42852. alpha = (float)(u*2*cimg::PI/subdivisions2),
  42853. x = xc + radius2*(float)(std::cos(alpha)*std::cos(beta)),
  42854. y = yc + radius2*(float)(std::cos(alpha)*std::sin(beta)),
  42855. z = radius2*(float)std::sin(alpha);
  42856. CImg<floatT>::vector(x,y,z).move_to(vertices);
  42857. }
  42858. }
  42859. for (unsigned int vv = 0; vv<subdivisions1; ++vv) {
  42860. const unsigned int nv = (vv + 1)%subdivisions1;
  42861. for (unsigned int uu = 0; uu<subdivisions2; ++uu) {
  42862. const unsigned int nu = (uu + 1)%subdivisions2, svv = subdivisions2*vv, snv = subdivisions2*nv;
  42863. CImg<tf>::vector(svv + nu,svv + uu,snv + uu,snv + nu).move_to(primitives);
  42864. }
  42865. }
  42866. return vertices>'x';
  42867. }
  42868. //! Generate a 3D XY-plane.
  42869. /**
  42870. \param[out] primitives The returned list of the 3D object primitives
  42871. (template type \e tf should be at least \e unsigned \e int).
  42872. \param size_x The width of the plane (dimension along the X-axis).
  42873. \param size_y The height of the plane (dimensions along the Y-axis).
  42874. \param subdivisions_x The number of planar subdivisions along the X-axis.
  42875. \param subdivisions_y The number of planar subdivisions along the Y-axis.
  42876. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42877. \par Example
  42878. \code
  42879. CImgList<unsigned int> faces3d;
  42880. const CImg<float> points3d = CImg<float>::plane3d(faces3d,100,50);
  42881. CImg<unsigned char>().display_object3d("Plane3d",points3d,faces3d);
  42882. \endcode
  42883. \image html ref_plane3d.jpg
  42884. **/
  42885. template<typename tf>
  42886. static CImg<floatT> plane3d(CImgList<tf>& primitives,
  42887. const float size_x=100, const float size_y=100,
  42888. const unsigned int subdivisions_x=10, const unsigned int subdivisions_y=10) {
  42889. primitives.assign();
  42890. if (!subdivisions_x || !subdivisions_y) return CImg<floatT>();
  42891. CImgList<floatT> vertices;
  42892. const unsigned int w = subdivisions_x + 1, h = subdivisions_y + 1;
  42893. const float fx = (float)size_x/w, fy = (float)size_y/h;
  42894. for (unsigned int y = 0; y<h; ++y) for (unsigned int x = 0; x<w; ++x)
  42895. CImg<floatT>::vector(fx*x,fy*y,0).move_to(vertices);
  42896. for (unsigned int y = 0; y<subdivisions_y; ++y) for (unsigned int x = 0; x<subdivisions_x; ++x) {
  42897. const int off1 = x + y*w, off2 = x + 1 + y*w, off3 = x + 1 + (y + 1)*w, off4 = x + (y + 1)*w;
  42898. CImg<tf>::vector(off1,off4,off3,off2).move_to(primitives);
  42899. }
  42900. return vertices>'x';
  42901. }
  42902. //! Generate a 3D sphere.
  42903. /**
  42904. \param[out] primitives The returned list of the 3D object primitives
  42905. (template type \e tf should be at least \e unsigned \e int).
  42906. \param radius The radius of the sphere (dimension along the X-axis).
  42907. \param subdivisions The number of recursive subdivisions from an initial icosahedron.
  42908. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42909. \par Example
  42910. \code
  42911. CImgList<unsigned int> faces3d;
  42912. const CImg<float> points3d = CImg<float>::sphere3d(faces3d,100,4);
  42913. CImg<unsigned char>().display_object3d("Sphere3d",points3d,faces3d);
  42914. \endcode
  42915. \image html ref_sphere3d.jpg
  42916. **/
  42917. template<typename tf>
  42918. static CImg<floatT> sphere3d(CImgList<tf>& primitives,
  42919. const float radius=50, const unsigned int subdivisions=3) {
  42920. // Create initial icosahedron
  42921. primitives.assign();
  42922. const double tmp = (1 + std::sqrt(5.f))/2, a = 1./std::sqrt(1 + tmp*tmp), b = tmp*a;
  42923. CImgList<floatT> vertices(12,1,3,1,1, b,a,0., -b,a,0., -b,-a,0., b,-a,0., a,0.,b, a,0.,-b,
  42924. -a,0.,-b, -a,0.,b, 0.,b,a, 0.,-b,a, 0.,-b,-a, 0.,b,-a);
  42925. primitives.assign(20,1,3,1,1, 4,8,7, 4,7,9, 5,6,11, 5,10,6, 0,4,3, 0,3,5, 2,7,1, 2,1,6,
  42926. 8,0,11, 8,11,1, 9,10,3, 9,2,10, 8,4,0, 11,0,5, 4,9,3,
  42927. 5,3,10, 7,8,1, 6,1,11, 7,2,9, 6,10,2);
  42928. // edge - length/2
  42929. float he = (float)a;
  42930. // Recurse subdivisions
  42931. for (unsigned int i = 0; i<subdivisions; ++i) {
  42932. const unsigned int L = primitives._width;
  42933. he/=2;
  42934. const float he2 = he*he;
  42935. for (unsigned int l = 0; l<L; ++l) {
  42936. const unsigned int
  42937. p0 = (unsigned int)primitives(0,0), p1 = (unsigned int)primitives(0,1), p2 = (unsigned int)primitives(0,2);
  42938. const float
  42939. x0 = vertices(p0,0), y0 = vertices(p0,1), z0 = vertices(p0,2),
  42940. x1 = vertices(p1,0), y1 = vertices(p1,1), z1 = vertices(p1,2),
  42941. x2 = vertices(p2,0), y2 = vertices(p2,1), z2 = vertices(p2,2),
  42942. tnx0 = (x0 + x1)/2, tny0 = (y0 + y1)/2, tnz0 = (z0 + z1)/2,
  42943. nn0 = cimg::hypot(tnx0,tny0,tnz0),
  42944. tnx1 = (x0 + x2)/2, tny1 = (y0 + y2)/2, tnz1 = (z0 + z2)/2,
  42945. nn1 = cimg::hypot(tnx1,tny1,tnz1),
  42946. tnx2 = (x1 + x2)/2, tny2 = (y1 + y2)/2, tnz2 = (z1 + z2)/2,
  42947. nn2 = cimg::hypot(tnx2,tny2,tnz2),
  42948. nx0 = tnx0/nn0, ny0 = tny0/nn0, nz0 = tnz0/nn0,
  42949. nx1 = tnx1/nn1, ny1 = tny1/nn1, nz1 = tnz1/nn1,
  42950. nx2 = tnx2/nn2, ny2 = tny2/nn2, nz2 = tnz2/nn2;
  42951. int i0 = -1, i1 = -1, i2 = -1;
  42952. cimglist_for(vertices,p) {
  42953. const float x = (float)vertices(p,0), y = (float)vertices(p,1), z = (float)vertices(p,2);
  42954. if (cimg::sqr(x-nx0) + cimg::sqr(y-ny0) + cimg::sqr(z-nz0)<he2) i0 = p;
  42955. if (cimg::sqr(x-nx1) + cimg::sqr(y-ny1) + cimg::sqr(z-nz1)<he2) i1 = p;
  42956. if (cimg::sqr(x-nx2) + cimg::sqr(y-ny2) + cimg::sqr(z-nz2)<he2) i2 = p;
  42957. }
  42958. if (i0<0) { CImg<floatT>::vector(nx0,ny0,nz0).move_to(vertices); i0 = vertices.width() - 1; }
  42959. if (i1<0) { CImg<floatT>::vector(nx1,ny1,nz1).move_to(vertices); i1 = vertices.width() - 1; }
  42960. if (i2<0) { CImg<floatT>::vector(nx2,ny2,nz2).move_to(vertices); i2 = vertices.width() - 1; }
  42961. primitives.remove(0);
  42962. CImg<tf>::vector(p0,i0,i1).move_to(primitives);
  42963. CImg<tf>::vector((tf)i0,(tf)p1,(tf)i2).move_to(primitives);
  42964. CImg<tf>::vector((tf)i1,(tf)i2,(tf)p2).move_to(primitives);
  42965. CImg<tf>::vector((tf)i1,(tf)i0,(tf)i2).move_to(primitives);
  42966. }
  42967. }
  42968. return (vertices>'x')*=radius;
  42969. }
  42970. //! Generate a 3D ellipsoid.
  42971. /**
  42972. \param[out] primitives The returned list of the 3D object primitives
  42973. (template type \e tf should be at least \e unsigned \e int).
  42974. \param tensor The tensor which gives the shape and size of the ellipsoid.
  42975. \param subdivisions The number of recursive subdivisions from an initial stretched icosahedron.
  42976. \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
  42977. \par Example
  42978. \code
  42979. CImgList<unsigned int> faces3d;
  42980. const CImg<float> tensor = CImg<float>::diagonal(10,7,3),
  42981. points3d = CImg<float>::ellipsoid3d(faces3d,tensor,4);
  42982. CImg<unsigned char>().display_object3d("Ellipsoid3d",points3d,faces3d);
  42983. \endcode
  42984. \image html ref_ellipsoid3d.jpg
  42985. **/
  42986. template<typename tf, typename t>
  42987. static CImg<floatT> ellipsoid3d(CImgList<tf>& primitives,
  42988. const CImg<t>& tensor, const unsigned int subdivisions=3) {
  42989. primitives.assign();
  42990. if (!subdivisions) return CImg<floatT>();
  42991. CImg<floatT> S, V;
  42992. tensor.symmetric_eigen(S,V);
  42993. const float orient =
  42994. (V(0,1)*V(1,2) - V(0,2)*V(1,1))*V(2,0) +
  42995. (V(0,2)*V(1,0) - V(0,0)*V(1,2))*V(2,1) +
  42996. (V(0,0)*V(1,1) - V(0,1)*V(1,0))*V(2,2);
  42997. if (orient<0) { V(2,0) = -V(2,0); V(2,1) = -V(2,1); V(2,2) = -V(2,2); }
  42998. const float l0 = S[0], l1 = S[1], l2 = S[2];
  42999. CImg<floatT> vertices = sphere3d(primitives,1.,subdivisions);
  43000. vertices.get_shared_row(0)*=l0;
  43001. vertices.get_shared_row(1)*=l1;
  43002. vertices.get_shared_row(2)*=l2;
  43003. return V*vertices;
  43004. }
  43005. //! Convert 3D object into a CImg3d representation.
  43006. /**
  43007. \param primitives Primitives data of the 3D object.
  43008. \param colors Colors data of the 3D object.
  43009. \param opacities Opacities data of the 3D object.
  43010. \param full_check Tells if full checking of the 3D object must be performed.
  43011. **/
  43012. template<typename tp, typename tc, typename to>
  43013. CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
  43014. const CImgList<tc>& colors,
  43015. const to& opacities,
  43016. const bool full_check=true) {
  43017. return get_object3dtoCImg3d(primitives,colors,opacities,full_check).move_to(*this);
  43018. }
  43019. //! Convert 3D object into a CImg3d representation \overloading.
  43020. template<typename tp, typename tc>
  43021. CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
  43022. const CImgList<tc>& colors,
  43023. const bool full_check=true) {
  43024. return get_object3dtoCImg3d(primitives,colors,full_check).move_to(*this);
  43025. }
  43026. //! Convert 3D object into a CImg3d representation \overloading.
  43027. template<typename tp>
  43028. CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
  43029. const bool full_check=true) {
  43030. return get_object3dtoCImg3d(primitives,full_check).move_to(*this);
  43031. }
  43032. //! Convert 3D object into a CImg3d representation \overloading.
  43033. CImg<T>& object3dtoCImg3d(const bool full_check=true) {
  43034. return get_object3dtoCImg3d(full_check).move_to(*this);
  43035. }
  43036. //! Convert 3D object into a CImg3d representation \newinstance.
  43037. template<typename tp, typename tc, typename to>
  43038. CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
  43039. const CImgList<tc>& colors,
  43040. const to& opacities,
  43041. const bool full_check=true) const {
  43042. CImg<charT> error_message(1024);
  43043. if (!is_object3d(primitives,colors,opacities,full_check,error_message))
  43044. throw CImgInstanceException(_cimg_instance
  43045. "object3dtoCImg3d(): Invalid specified 3D object (%u,%u) (%s).",
  43046. cimg_instance,_width,primitives._width,error_message.data());
  43047. CImg<floatT> res(1,_size_object3dtoCImg3d(primitives,colors,opacities));
  43048. float *ptrd = res._data;
  43049. // Put magick number.
  43050. *(ptrd++) = 'C' + 0.5f; *(ptrd++) = 'I' + 0.5f; *(ptrd++) = 'm' + 0.5f;
  43051. *(ptrd++) = 'g' + 0.5f; *(ptrd++) = '3' + 0.5f; *(ptrd++) = 'd' + 0.5f;
  43052. // Put number of vertices and primitives.
  43053. *(ptrd++) = cimg::uint2float(_width);
  43054. *(ptrd++) = cimg::uint2float(primitives._width);
  43055. // Put vertex data.
  43056. if (is_empty() || !primitives) return res;
  43057. const T *ptrx = data(0,0), *ptry = data(0,1), *ptrz = data(0,2);
  43058. cimg_forX(*this,p) {
  43059. *(ptrd++) = (float)*(ptrx++);
  43060. *(ptrd++) = (float)*(ptry++);
  43061. *(ptrd++) = (float)*(ptrz++);
  43062. }
  43063. // Put primitive data.
  43064. cimglist_for(primitives,p) {
  43065. *(ptrd++) = (float)primitives[p].size();
  43066. const tp *ptrp = primitives[p]._data;
  43067. cimg_foroff(primitives[p],i) *(ptrd++) = cimg::uint2float((unsigned int)*(ptrp++));
  43068. }
  43069. // Put color/texture data.
  43070. const unsigned int csiz = std::min(colors._width,primitives._width);
  43071. for (int c = 0; c<(int)csiz; ++c) {
  43072. const CImg<tc>& color = colors[c];
  43073. const tc *ptrc = color._data;
  43074. if (color.size()==3) { *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*ptrc; }
  43075. else {
  43076. *(ptrd++) = -128.f;
  43077. int shared_ind = -1;
  43078. if (color.is_shared()) for (int i = 0; i<c; ++i) if (ptrc==colors[i]._data) { shared_ind = i; break; }
  43079. if (shared_ind<0) {
  43080. *(ptrd++) = (float)color._width;
  43081. *(ptrd++) = (float)color._height;
  43082. *(ptrd++) = (float)color._spectrum;
  43083. cimg_foroff(color,l) *(ptrd++) = (float)*(ptrc++);
  43084. } else {
  43085. *(ptrd++) = (float)shared_ind;
  43086. *(ptrd++) = 0;
  43087. *(ptrd++) = 0;
  43088. }
  43089. }
  43090. }
  43091. const int csiz2 = primitives.width() - colors.width();
  43092. for (int c = 0; c<csiz2; ++c) { *(ptrd++) = 200.f; *(ptrd++) = 200.f; *(ptrd++) = 200.f; }
  43093. // Put opacity data.
  43094. ptrd = _object3dtoCImg3d(opacities,ptrd);
  43095. const float *ptre = res.end();
  43096. while (ptrd<ptre) *(ptrd++) = 1.f;
  43097. return res;
  43098. }
  43099. template<typename to>
  43100. float* _object3dtoCImg3d(const CImgList<to>& opacities, float *ptrd) const {
  43101. cimglist_for(opacities,o) {
  43102. const CImg<to>& opacity = opacities[o];
  43103. const to *ptro = opacity._data;
  43104. if (opacity.size()==1) *(ptrd++) = (float)*ptro;
  43105. else {
  43106. *(ptrd++) = -128.f;
  43107. int shared_ind = -1;
  43108. if (opacity.is_shared()) for (int i = 0; i<o; ++i) if (ptro==opacities[i]._data) { shared_ind = i; break; }
  43109. if (shared_ind<0) {
  43110. *(ptrd++) = (float)opacity._width;
  43111. *(ptrd++) = (float)opacity._height;
  43112. *(ptrd++) = (float)opacity._spectrum;
  43113. cimg_foroff(opacity,l) *(ptrd++) = (float)*(ptro++);
  43114. } else {
  43115. *(ptrd++) = (float)shared_ind;
  43116. *(ptrd++) = 0;
  43117. *(ptrd++) = 0;
  43118. }
  43119. }
  43120. }
  43121. return ptrd;
  43122. }
  43123. template<typename to>
  43124. float* _object3dtoCImg3d(const CImg<to>& opacities, float *ptrd) const {
  43125. const to *ptro = opacities._data;
  43126. cimg_foroff(opacities,o) *(ptrd++) = (float)*(ptro++);
  43127. return ptrd;
  43128. }
  43129. template<typename tp, typename tc, typename to>
  43130. unsigned int _size_object3dtoCImg3d(const CImgList<tp>& primitives,
  43131. const CImgList<tc>& colors,
  43132. const CImgList<to>& opacities) const {
  43133. unsigned int siz = 8U + 3*_width;
  43134. cimglist_for(primitives,p) siz+=primitives[p].size() + 1;
  43135. for (int c = std::min(primitives.width(),colors.width()) - 1; c>=0; --c) {
  43136. if (colors[c].is_shared()) siz+=4;
  43137. else { const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4 + csiz:3; }
  43138. }
  43139. if (colors._width<primitives._width) siz+=3*(primitives._width - colors._width);
  43140. cimglist_for(opacities,o) {
  43141. if (opacities[o].is_shared()) siz+=4;
  43142. else { const unsigned int osiz = opacities[o].size(); siz+=(osiz!=1)?4 + osiz:1; }
  43143. }
  43144. siz+=primitives._width - opacities._width;
  43145. return siz;
  43146. }
  43147. template<typename tp, typename tc, typename to>
  43148. unsigned int _size_object3dtoCImg3d(const CImgList<tp>& primitives,
  43149. const CImgList<tc>& colors,
  43150. const CImg<to>& opacities) const {
  43151. unsigned int siz = 8U + 3*_width;
  43152. cimglist_for(primitives,p) siz+=primitives[p].size() + 1;
  43153. for (int c = std::min(primitives.width(),colors.width()) - 1; c>=0; --c) {
  43154. const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4 + csiz:3;
  43155. }
  43156. if (colors._width<primitives._width) siz+=3*(primitives._width - colors._width);
  43157. siz+=primitives.size();
  43158. cimg::unused(opacities);
  43159. return siz;
  43160. }
  43161. //! Convert 3D object into a CImg3d representation \overloading.
  43162. template<typename tp, typename tc>
  43163. CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
  43164. const CImgList<tc>& colors,
  43165. const bool full_check=true) const {
  43166. CImgList<T> opacities;
  43167. return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
  43168. }
  43169. //! Convert 3D object into a CImg3d representation \overloading.
  43170. template<typename tp>
  43171. CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
  43172. const bool full_check=true) const {
  43173. CImgList<T> colors, opacities;
  43174. return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
  43175. }
  43176. //! Convert 3D object into a CImg3d representation \overloading.
  43177. CImg<floatT> get_object3dtoCImg3d(const bool full_check=true) const {
  43178. CImgList<T> opacities, colors;
  43179. CImgList<uintT> primitives(width(),1,1,1,1);
  43180. cimglist_for(primitives,p) primitives(p,0) = p;
  43181. return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
  43182. }
  43183. //! Convert CImg3d representation into a 3D object.
  43184. /**
  43185. \param[out] primitives Primitives data of the 3D object.
  43186. \param[out] colors Colors data of the 3D object.
  43187. \param[out] opacities Opacities data of the 3D object.
  43188. \param full_check Tells if full checking of the 3D object must be performed.
  43189. **/
  43190. template<typename tp, typename tc, typename to>
  43191. CImg<T>& CImg3dtoobject3d(CImgList<tp>& primitives,
  43192. CImgList<tc>& colors,
  43193. CImgList<to>& opacities,
  43194. const bool full_check=true) {
  43195. return get_CImg3dtoobject3d(primitives,colors,opacities,full_check).move_to(*this);
  43196. }
  43197. //! Convert CImg3d representation into a 3D object \newinstance.
  43198. template<typename tp, typename tc, typename to>
  43199. CImg<T> get_CImg3dtoobject3d(CImgList<tp>& primitives,
  43200. CImgList<tc>& colors,
  43201. CImgList<to>& opacities,
  43202. const bool full_check=true) const {
  43203. CImg<charT> error_message(1024);
  43204. if (!is_CImg3d(full_check,error_message))
  43205. throw CImgInstanceException(_cimg_instance
  43206. "CImg3dtoobject3d(): image instance is not a CImg3d (%s).",
  43207. cimg_instance,error_message.data());
  43208. const T *ptrs = _data + 6;
  43209. const unsigned int
  43210. nb_points = cimg::float2uint((float)*(ptrs++)),
  43211. nb_primitives = cimg::float2uint((float)*(ptrs++));
  43212. const CImg<T> points = CImg<T>(ptrs,3,nb_points,1,1,true).get_transpose();
  43213. ptrs+=3*nb_points;
  43214. primitives.assign(nb_primitives);
  43215. cimglist_for(primitives,p) {
  43216. const unsigned int nb_inds = (unsigned int)*(ptrs++);
  43217. primitives[p].assign(1,nb_inds);
  43218. tp *ptrp = primitives[p]._data;
  43219. for (unsigned int i = 0; i<nb_inds; ++i) *(ptrp++) = (tp)cimg::float2uint((float)*(ptrs++));
  43220. }
  43221. colors.assign(nb_primitives);
  43222. cimglist_for(colors,c) {
  43223. if (*ptrs==(T)-128) {
  43224. ++ptrs;
  43225. const unsigned int w = (unsigned int)*(ptrs++), h = (unsigned int)*(ptrs++), s = (unsigned int)*(ptrs++);
  43226. if (!h && !s) colors[c].assign(colors[w],true);
  43227. else { colors[c].assign(ptrs,w,h,1,s,false); ptrs+=w*h*s; }
  43228. } else { colors[c].assign(ptrs,1,1,1,3,false); ptrs+=3; }
  43229. }
  43230. opacities.assign(nb_primitives);
  43231. cimglist_for(opacities,o) {
  43232. if (*ptrs==(T)-128) {
  43233. ++ptrs;
  43234. const unsigned int w = (unsigned int)*(ptrs++), h = (unsigned int)*(ptrs++), s = (unsigned int)*(ptrs++);
  43235. if (!h && !s) opacities[o].assign(opacities[w],true);
  43236. else { opacities[o].assign(ptrs,w,h,1,s,false); ptrs+=w*h*s; }
  43237. } else opacities[o].assign(1,1,1,1,*(ptrs++));
  43238. }
  43239. return points;
  43240. }
  43241. //@}
  43242. //---------------------------
  43243. //
  43244. //! \name Drawing Functions
  43245. //@{
  43246. //---------------------------
  43247. #define cimg_init_scanline(opacity) \
  43248. static const T _sc_maxval = (T)std::min(cimg::type<T>::max(),(T)cimg::type<tc>::max()); \
  43249. const float _sc_nopacity = cimg::abs((float)opacity), _sc_copacity = 1 - std::max((float)opacity,0.f); \
  43250. const ulongT _sc_whd = (ulongT)_width*_height*_depth; \
  43251. cimg::unused(_sc_maxval);
  43252. #define cimg_draw_scanline(x0,x1,y,color,opacity,brightness) \
  43253. _draw_scanline(x0,x1,y,color,opacity,brightness,_sc_nopacity,_sc_copacity,_sc_whd,_sc_maxval)
  43254. // [internal] The following _draw_scanline() routines are *non user-friendly functions*,
  43255. // used only for internal purpose.
  43256. // Pre-requisites: x0<=x1, y-coordinate is valid, col is valid.
  43257. template<typename tc>
  43258. CImg<T>& _draw_scanline(const int x0, const int x1, const int y,
  43259. const tc *const color, const float opacity,
  43260. const float brightness,
  43261. const float nopacity, const float copacity, const ulongT whd, const T _sc_maxval) {
  43262. const int nx0 = x0>0?x0:0, nx1 = x1<width()?x1:width() - 1, dx = nx1 - nx0;
  43263. if (dx>=0) {
  43264. const tc *col = color;
  43265. const ulongT off = whd - dx - 1;
  43266. T *ptrd = data(nx0,y);
  43267. if (opacity>=1) { // ** Opaque drawing **
  43268. if (brightness==1) { // Brightness==1
  43269. if (sizeof(T)!=1) cimg_forC(*this,c) {
  43270. const T val = (T)*(col++);
  43271. for (int x = dx; x>=0; --x) *(ptrd++) = val;
  43272. ptrd+=off;
  43273. } else cimg_forC(*this,c) {
  43274. const T val = (T)*(col++);
  43275. std::memset(ptrd,(int)val,dx + 1);
  43276. ptrd+=whd;
  43277. }
  43278. } else if (brightness<1) { // Brightness<1
  43279. if (sizeof(T)!=1) cimg_forC(*this,c) {
  43280. const T val = (T)(*(col++)*brightness);
  43281. for (int x = dx; x>=0; --x) *(ptrd++) = val;
  43282. ptrd+=off;
  43283. } else cimg_forC(*this,c) {
  43284. const T val = (T)(*(col++)*brightness);
  43285. std::memset(ptrd,(int)val,dx + 1);
  43286. ptrd+=whd;
  43287. }
  43288. } else { // Brightness>1
  43289. if (sizeof(T)!=1) cimg_forC(*this,c) {
  43290. const T val = (T)((2-brightness)**(col++) + (brightness - 1)*_sc_maxval);
  43291. for (int x = dx; x>=0; --x) *(ptrd++) = val;
  43292. ptrd+=off;
  43293. } else cimg_forC(*this,c) {
  43294. const T val = (T)((2-brightness)**(col++) + (brightness - 1)*_sc_maxval);
  43295. std::memset(ptrd,(int)val,dx + 1);
  43296. ptrd+=whd;
  43297. }
  43298. }
  43299. } else { // ** Transparent drawing **
  43300. if (brightness==1) { // Brightness==1
  43301. cimg_forC(*this,c) {
  43302. const Tfloat val = *(col++)*nopacity;
  43303. for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
  43304. ptrd+=off;
  43305. }
  43306. } else if (brightness<=1) { // Brightness<1
  43307. cimg_forC(*this,c) {
  43308. const Tfloat val = *(col++)*brightness*nopacity;
  43309. for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
  43310. ptrd+=off;
  43311. }
  43312. } else { // Brightness>1
  43313. cimg_forC(*this,c) {
  43314. const Tfloat val = ((2-brightness)**(col++) + (brightness - 1)*_sc_maxval)*nopacity;
  43315. for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
  43316. ptrd+=off;
  43317. }
  43318. }
  43319. }
  43320. }
  43321. return *this;
  43322. }
  43323. //! Draw a 3D point.
  43324. /**
  43325. \param x0 X-coordinate of the point.
  43326. \param y0 Y-coordinate of the point.
  43327. \param z0 Z-coordinate of the point.
  43328. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  43329. \param opacity Drawing opacity.
  43330. \note
  43331. - To set pixel values without clipping needs, you should use the faster CImg::operator()() function.
  43332. \par Example:
  43333. \code
  43334. CImg<unsigned char> img(100,100,1,3,0);
  43335. const unsigned char color[] = { 255,128,64 };
  43336. img.draw_point(50,50,color);
  43337. \endcode
  43338. **/
  43339. template<typename tc>
  43340. CImg<T>& draw_point(const int x0, const int y0, const int z0,
  43341. const tc *const color, const float opacity=1) {
  43342. if (is_empty()) return *this;
  43343. if (!color)
  43344. throw CImgArgumentException(_cimg_instance
  43345. "draw_point(): Specified color is (null).",
  43346. cimg_instance);
  43347. if (x0>=0 && y0>=0 && z0>=0 && x0<width() && y0<height() && z0<depth()) {
  43348. const ulongT whd = (ulongT)_width*_height*_depth;
  43349. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  43350. T *ptrd = data(x0,y0,z0,0);
  43351. const tc *col = color;
  43352. if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; }
  43353. else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; }
  43354. }
  43355. return *this;
  43356. }
  43357. //! Draw a 2D point \simplification.
  43358. template<typename tc>
  43359. CImg<T>& draw_point(const int x0, const int y0,
  43360. const tc *const color, const float opacity=1) {
  43361. return draw_point(x0,y0,0,color,opacity);
  43362. }
  43363. // Draw a points cloud.
  43364. /**
  43365. \param points Image of vertices coordinates.
  43366. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  43367. \param opacity Drawing opacity.
  43368. **/
  43369. template<typename t, typename tc>
  43370. CImg<T>& draw_point(const CImg<t>& points,
  43371. const tc *const color, const float opacity=1) {
  43372. if (is_empty() || !points) return *this;
  43373. switch (points._height) {
  43374. case 0 : case 1 :
  43375. throw CImgArgumentException(_cimg_instance
  43376. "draw_point(): Invalid specified point set (%u,%u,%u,%u,%p).",
  43377. cimg_instance,
  43378. points._width,points._height,points._depth,points._spectrum,points._data);
  43379. case 2 : {
  43380. cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),color,opacity);
  43381. } break;
  43382. default : {
  43383. cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),(int)points(i,2),color,opacity);
  43384. }
  43385. }
  43386. return *this;
  43387. }
  43388. //! Draw a 2D line.
  43389. /**
  43390. \param x0 X-coordinate of the starting line point.
  43391. \param y0 Y-coordinate of the starting line point.
  43392. \param x1 X-coordinate of the ending line point.
  43393. \param y1 Y-coordinate of the ending line point.
  43394. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43395. \param opacity Drawing opacity.
  43396. \param pattern An integer whose bits describe the line pattern.
  43397. \param init_hatch Tells if a reinitialization of the hash state must be done.
  43398. \note
  43399. - Line routine uses Bresenham's algorithm.
  43400. - Set \p init_hatch = false to draw consecutive hatched segments without breaking the line pattern.
  43401. \par Example:
  43402. \code
  43403. CImg<unsigned char> img(100,100,1,3,0);
  43404. const unsigned char color[] = { 255,128,64 };
  43405. img.draw_line(40,40,80,70,color);
  43406. \endcode
  43407. **/
  43408. template<typename tc>
  43409. CImg<T>& draw_line(int x0, int y0,
  43410. int x1, int y1,
  43411. const tc *const color, const float opacity=1,
  43412. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43413. if (is_empty() || !opacity || !pattern ||
  43414. std::min(y0,y1)>=height() || std::max(y0,y1)<0 ||
  43415. std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
  43416. int
  43417. w1 = width() - 1, h1 = height() - 1,
  43418. dx01 = x1 - x0, dy01 = y1 - y0;
  43419. const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
  43420. if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
  43421. if (pattern==~0U && y0>y1) {
  43422. cimg::swap(x0,x1,y0,y1);
  43423. dx01*=-1; dy01*=-1;
  43424. }
  43425. static unsigned int hatch = ~0U - (~0U>>1);
  43426. if (init_hatch) hatch = ~0U - (~0U>>1);
  43427. cimg_init_scanline(opacity);
  43428. const int
  43429. step = y0<=y1?1:-1,hdy01 = dy01*cimg::sign(dx01)/2,
  43430. cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
  43431. dy01+=dy01?0:1;
  43432. for (int y = cy0; y!=cy1; y+=step) {
  43433. const int
  43434. yy0 = y - y0,
  43435. x = x0 + (dx01*yy0 + hdy01)/dy01;
  43436. if (x>=0 && x<=w1 && pattern&hatch) {
  43437. T *const ptrd = is_horizontal?data(y,x):data(x,y);
  43438. cimg_forC(*this,c) {
  43439. const T val = color[c];
  43440. ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  43441. }
  43442. }
  43443. if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
  43444. }
  43445. return *this;
  43446. }
  43447. //! Draw a 2D line, with z-buffering.
  43448. /**
  43449. \param zbuffer Zbuffer image.
  43450. \param x0 X-coordinate of the starting point.
  43451. \param y0 Y-coordinate of the starting point.
  43452. \param z0 Z-coordinate of the starting point
  43453. \param x1 X-coordinate of the ending point.
  43454. \param y1 Y-coordinate of the ending point.
  43455. \param z1 Z-coordinate of the ending point.
  43456. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43457. \param opacity Drawing opacity.
  43458. \param pattern An integer whose bits describe the line pattern.
  43459. \param init_hatch Tells if a reinitialization of the hash state must be done.
  43460. **/
  43461. template<typename tz,typename tc>
  43462. CImg<T>& draw_line(CImg<tz>& zbuffer,
  43463. int x0, int y0, const float z0,
  43464. int x1, int y1, const float z1,
  43465. const tc *const color, const float opacity=1,
  43466. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43467. if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
  43468. if (!color)
  43469. throw CImgArgumentException(_cimg_instance
  43470. "draw_line(): Specified color is (null).",
  43471. cimg_instance);
  43472. if (!is_sameXY(zbuffer))
  43473. throw CImgArgumentException(_cimg_instance
  43474. "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  43475. "different dimensions.",
  43476. cimg_instance,
  43477. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  43478. if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
  43479. float iz0 = 1/z0, iz1 = 1/z1;
  43480. int
  43481. w1 = width() - 1, h1 = height() - 1,
  43482. dx01 = x1 - x0, dy01 = y1 - y0;
  43483. float diz01 = iz1 - iz0;
  43484. const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
  43485. if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
  43486. if (pattern==~0U && y0>y1) {
  43487. cimg::swap(x0,x1,y0,y1,iz0,iz1);
  43488. dx01*=-1; dy01*=-1; diz01*=-1;
  43489. }
  43490. static unsigned int hatch = ~0U - (~0U>>1);
  43491. if (init_hatch) hatch = ~0U - (~0U>>1);
  43492. cimg_init_scanline(opacity);
  43493. const int
  43494. step = y0<=y1?1:-1, hdy01 = dy01*cimg::sign(dx01)/2,
  43495. cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
  43496. dy01+=dy01?0:1;
  43497. for (int y = cy0; y!=cy1; y+=step) {
  43498. const int
  43499. yy0 = y - y0,
  43500. x = x0 + (dx01*yy0 + hdy01)/dy01;
  43501. const float iz = iz0 + diz01*yy0/dy01;
  43502. tz *const ptrz = is_horizontal?zbuffer.data(y,x):zbuffer.data(x,y);
  43503. if (x>=0 && x<=w1 && pattern&hatch && iz>=*ptrz) {
  43504. *ptrz = (tz)iz;
  43505. T *const ptrd = is_horizontal?data(y,x):data(x,y);
  43506. cimg_forC(*this,c) {
  43507. const T val = color[c];
  43508. ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  43509. }
  43510. }
  43511. if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
  43512. }
  43513. return *this;
  43514. }
  43515. //! Draw a textured 2D line.
  43516. /**
  43517. \param x0 X-coordinate of the starting line point.
  43518. \param y0 Y-coordinate of the starting line point.
  43519. \param x1 X-coordinate of the ending line point.
  43520. \param y1 Y-coordinate of the ending line point.
  43521. \param texture Texture image defining the pixel colors.
  43522. \param tx0 X-coordinate of the starting texture point.
  43523. \param ty0 Y-coordinate of the starting texture point.
  43524. \param tx1 X-coordinate of the ending texture point.
  43525. \param ty1 Y-coordinate of the ending texture point.
  43526. \param opacity Drawing opacity.
  43527. \param pattern An integer whose bits describe the line pattern.
  43528. \param init_hatch Tells if the hash variable must be reinitialized.
  43529. \note
  43530. - Line routine uses the well known Bresenham's algorithm.
  43531. \par Example:
  43532. \code
  43533. CImg<unsigned char> img(100,100,1,3,0), texture("texture256x256.ppm");
  43534. const unsigned char color[] = { 255,128,64 };
  43535. img.draw_line(40,40,80,70,texture,0,0,255,255);
  43536. \endcode
  43537. **/
  43538. template<typename tc>
  43539. CImg<T>& draw_line(int x0, int y0,
  43540. int x1, int y1,
  43541. const CImg<tc>& texture,
  43542. int tx0, int ty0,
  43543. int tx1, int ty1,
  43544. const float opacity=1,
  43545. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43546. if (is_empty() || !opacity || !pattern) return *this;
  43547. if (texture._depth>1 || texture._spectrum<_spectrum)
  43548. throw CImgArgumentException(_cimg_instance
  43549. "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
  43550. cimg_instance,
  43551. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  43552. if (is_overlapped(texture)) return draw_line(x0,y0,x1,y1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
  43553. if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
  43554. int w1 = width() - 1, h1 = height() - 1;
  43555. longT
  43556. dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0,
  43557. dtx01 = (longT)tx1 - tx0, dty01 = (longT)ty1 - ty0;
  43558. const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
  43559. if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
  43560. if (pattern==~0U && y0>y1) {
  43561. cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1);
  43562. dx01*=-1; dy01*=-1; dtx01*=-1; dty01*=-1;
  43563. }
  43564. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  43565. static unsigned int hatch = ~0U - (~0U>>1);
  43566. if (init_hatch) hatch = ~0U - (~0U>>1);
  43567. cimg_init_scanline(opacity);
  43568. const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
  43569. const longT
  43570. hdy01 = dy01*cimg::sign(dx01)/2,
  43571. hdy01tx = dy01*cimg::sign(dtx01)/2,
  43572. hdy01ty = dy01*cimg::sign(dty01)/2;
  43573. dy01+=dy01?0:1;
  43574. for (int y = cy0; y!=cy1; y+=step) {
  43575. const longT
  43576. yy0 = (longT)y - y0,
  43577. x = x0 + (dx01*yy0 + hdy01)/dy01,
  43578. tx = tx0 + (dtx01*yy0 + hdy01tx)/dy01,
  43579. ty = ty0 + (dty01*yy0 + hdy01ty)/dy01;
  43580. if (x>=0 && x<=w1 && pattern&hatch) {
  43581. T *const ptrd = is_horizontal?data(y,x):data(x,y);
  43582. const tc *const color = &texture._atXY(tx,ty);
  43583. cimg_forC(*this,c) {
  43584. const T val = color[c*twhd];
  43585. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  43586. }
  43587. }
  43588. if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
  43589. }
  43590. return *this;
  43591. }
  43592. //! Draw a textured 2D line, with perspective correction.
  43593. /**
  43594. \param x0 X-coordinate of the starting point.
  43595. \param y0 Y-coordinate of the starting point.
  43596. \param z0 Z-coordinate of the starting point
  43597. \param x1 X-coordinate of the ending point.
  43598. \param y1 Y-coordinate of the ending point.
  43599. \param z1 Z-coordinate of the ending point.
  43600. \param texture Texture image defining the pixel colors.
  43601. \param tx0 X-coordinate of the starting texture point.
  43602. \param ty0 Y-coordinate of the starting texture point.
  43603. \param tx1 X-coordinate of the ending texture point.
  43604. \param ty1 Y-coordinate of the ending texture point.
  43605. \param opacity Drawing opacity.
  43606. \param pattern An integer whose bits describe the line pattern.
  43607. \param init_hatch Tells if the hash variable must be reinitialized.
  43608. **/
  43609. template<typename tc>
  43610. CImg<T>& draw_line(int x0, int y0, const float z0,
  43611. int x1, int y1, const float z1,
  43612. const CImg<tc>& texture,
  43613. const int tx0, const int ty0,
  43614. const int tx1, const int ty1,
  43615. const float opacity=1,
  43616. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43617. if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
  43618. if (texture._depth>1 || texture._spectrum<_spectrum)
  43619. throw CImgArgumentException(_cimg_instance
  43620. "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
  43621. cimg_instance,
  43622. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  43623. if (is_overlapped(texture))
  43624. return draw_line(x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
  43625. if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
  43626. float iz0 = 1/z0, iz1 = 1/z1;
  43627. int w1 = width() - 1, h1 = height() - 1;
  43628. longT dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0;
  43629. float
  43630. diz01 = iz1 - iz0,
  43631. txz0 = tx0*iz0, txz1 = tx1*iz1,
  43632. tyz0 = ty0*iz0, tyz1 = ty1*iz1,
  43633. dtxz01 = txz1 - txz0, dtyz01 = tyz1 - tyz0;
  43634. const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
  43635. if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
  43636. if (pattern==~0U && y0>y1) {
  43637. cimg::swap(x0,x1,y0,y1,iz0,iz1,txz0,txz1,tyz0,tyz1);
  43638. dx01*=-1; dy01*=-1; diz01*=-1; dtxz01*=-1; dtyz01*=-1;
  43639. }
  43640. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  43641. static unsigned int hatch = ~0U - (~0U>>1);
  43642. if (init_hatch) hatch = ~0U - (~0U>>1);
  43643. cimg_init_scanline(opacity);
  43644. const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
  43645. const longT hdy01 = dy01*cimg::sign(dx01)/2;
  43646. dy01+=dy01?0:1;
  43647. for (int y = cy0; y!=cy1; y+=step) {
  43648. const longT
  43649. yy0 = (longT)y - y0,
  43650. x = x0 + (dx01*yy0 + hdy01)/dy01;
  43651. const float
  43652. iz = iz0 + diz01*yy0/dy01,
  43653. txz = txz0 + dtxz01*yy0/dy01,
  43654. tyz = tyz0 + dtyz01*yy0/dy01;
  43655. if (x>=0 && x<=w1 && pattern&hatch) {
  43656. const int
  43657. tx = (int)cimg::round(txz/iz),
  43658. ty = (int)cimg::round(tyz/iz);
  43659. T *const ptrd = is_horizontal?data(y,x):data(x,y);
  43660. const tc *const color = &texture._atXY(tx,ty);
  43661. cimg_forC(*this,c) {
  43662. const T val = color[c*twhd];
  43663. ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  43664. }
  43665. }
  43666. if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
  43667. }
  43668. return *this;
  43669. }
  43670. //! Draw a textured 2D line, with perspective correction and z-buffering.
  43671. /**
  43672. \param zbuffer Z-buffer image.
  43673. \param x0 X-coordinate of the starting point.
  43674. \param y0 Y-coordinate of the starting point.
  43675. \param z0 Z-coordinate of the starting point
  43676. \param x1 X-coordinate of the ending point.
  43677. \param y1 Y-coordinate of the ending point.
  43678. \param z1 Z-coordinate of the ending point.
  43679. \param texture Texture image defining the pixel colors.
  43680. \param tx0 X-coordinate of the starting texture point.
  43681. \param ty0 Y-coordinate of the starting texture point.
  43682. \param tx1 X-coordinate of the ending texture point.
  43683. \param ty1 Y-coordinate of the ending texture point.
  43684. \param opacity Drawing opacity.
  43685. \param pattern An integer whose bits describe the line pattern.
  43686. \param init_hatch Tells if the hash variable must be reinitialized.
  43687. **/
  43688. template<typename tz, typename tc>
  43689. CImg<T>& draw_line(CImg<tz>& zbuffer,
  43690. int x0, int y0, const float z0,
  43691. int x1, int y1, const float z1,
  43692. const CImg<tc>& texture,
  43693. const int tx0, const int ty0,
  43694. const int tx1, const int ty1,
  43695. const float opacity=1,
  43696. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43697. if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
  43698. if (!is_sameXY(zbuffer))
  43699. throw CImgArgumentException(_cimg_instance
  43700. "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  43701. "different dimensions.",
  43702. cimg_instance,
  43703. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  43704. if (texture._depth>1 || texture._spectrum<_spectrum)
  43705. throw CImgArgumentException(_cimg_instance
  43706. "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
  43707. cimg_instance,
  43708. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  43709. if (is_overlapped(texture))
  43710. return draw_line(zbuffer,x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
  43711. if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
  43712. float iz0 = 1/z0, iz1 = 1/z1;
  43713. int w1 = width() - 1, h1 = height() - 1;
  43714. longT dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0;
  43715. float
  43716. diz01 = iz1 - iz0,
  43717. txz0 = tx0*iz0, txz1 = tx1*iz1,
  43718. tyz0 = ty0*iz0, tyz1 = ty1*iz1,
  43719. dtxz01 = txz1 - txz0, dtyz01 = tyz1 - tyz0;
  43720. const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
  43721. if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
  43722. if (pattern==~0U && y0>y1) {
  43723. cimg::swap(x0,x1,y0,y1,iz0,iz1,txz0,txz1,tyz0,tyz1);
  43724. dx01*=-1; dy01*=-1; diz01*=-1; dtxz01*=-1; dtyz01*=-1;
  43725. }
  43726. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  43727. static unsigned int hatch = ~0U - (~0U>>1);
  43728. if (init_hatch) hatch = ~0U - (~0U>>1);
  43729. cimg_init_scanline(opacity);
  43730. const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
  43731. const longT hdy01 = dy01*cimg::sign(dx01)/2;
  43732. dy01+=dy01?0:1;
  43733. for (int y = cy0; y!=cy1; y+=step) {
  43734. const longT
  43735. yy0 = (longT)y - y0,
  43736. x = x0 + (dx01*yy0 + hdy01)/dy01;
  43737. const float
  43738. iz = iz0 + diz01*yy0/dy01,
  43739. txz = txz0 + dtxz01*yy0/dy01,
  43740. tyz = tyz0 + dtyz01*yy0/dy01;
  43741. tz *const ptrz = is_horizontal?zbuffer.data(y,x):zbuffer.data(x,y);
  43742. if (x>=0 && x<=w1 && pattern&hatch && iz>=*ptrz) {
  43743. *ptrz = (tz)iz;
  43744. const int
  43745. tx = (int)cimg::round(txz/iz),
  43746. ty = (int)cimg::round(tyz/iz);
  43747. T *const ptrd = is_horizontal?data(y,x):data(x,y);
  43748. const tc *const color = &texture._atXY(tx,ty);
  43749. cimg_forC(*this,c) {
  43750. const T val = color[c*twhd];
  43751. ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  43752. }
  43753. }
  43754. if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
  43755. }
  43756. return *this;
  43757. }
  43758. //! Draw a set of consecutive lines.
  43759. /**
  43760. \param points Coordinates of vertices, stored as a list of vectors.
  43761. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43762. \param opacity Drawing opacity.
  43763. \param pattern An integer whose bits describe the line pattern.
  43764. \param init_hatch If set to true, init hatch motif.
  43765. \note
  43766. - This function uses several call to the single CImg::draw_line() procedure,
  43767. depending on the vectors size in \p points.
  43768. **/
  43769. template<typename t, typename tc>
  43770. CImg<T>& draw_line(const CImg<t>& points,
  43771. const tc *const color, const float opacity=1,
  43772. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43773. if (is_empty() || !points || points._width<2) return *this;
  43774. bool ninit_hatch = init_hatch;
  43775. switch (points._height) {
  43776. case 0 : case 1 :
  43777. throw CImgArgumentException(_cimg_instance
  43778. "draw_line(): Invalid specified point set (%u,%u,%u,%u,%p).",
  43779. cimg_instance,
  43780. points._width,points._height,points._depth,points._spectrum,points._data);
  43781. default : {
  43782. const int x0 = (int)points(0,0), y0 = (int)points(0,1);
  43783. int ox = x0, oy = y0;
  43784. for (unsigned int i = 1; i<points._width; ++i) {
  43785. const int x = (int)points(i,0), y = (int)points(i,1);
  43786. draw_line(ox,oy,x,y,color,opacity,pattern,ninit_hatch);
  43787. ninit_hatch = false;
  43788. ox = x; oy = y;
  43789. }
  43790. }
  43791. }
  43792. return *this;
  43793. }
  43794. //! Draw a 2D arrow.
  43795. /**
  43796. \param x0 X-coordinate of the starting arrow point (tail).
  43797. \param y0 Y-coordinate of the starting arrow point (tail).
  43798. \param x1 X-coordinate of the ending arrow point (head).
  43799. \param y1 Y-coordinate of the ending arrow point (head).
  43800. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43801. \param angle Aperture angle of the arrow head.
  43802. \param length Length of the arrow head. If negative, describes a percentage of the arrow length.
  43803. \param opacity Drawing opacity.
  43804. \param pattern An integer whose bits describe the line pattern.
  43805. **/
  43806. template<typename tc>
  43807. CImg<T>& draw_arrow(const int x0, const int y0,
  43808. const int x1, const int y1,
  43809. const tc *const color, const float opacity=1,
  43810. const float angle=30, const float length=-10,
  43811. const unsigned int pattern=~0U) {
  43812. if (is_empty()) return *this;
  43813. const float u = (float)(x0 - x1), v = (float)(y0 - y1), sq = u*u + v*v,
  43814. deg = (float)(angle*cimg::PI/180), ang = (sq>0)?(float)std::atan2(v,u):0.f,
  43815. l = (length>=0)?length:-length*(float)std::sqrt(sq)/100;
  43816. if (sq>0) {
  43817. const float
  43818. cl = (float)std::cos(ang - deg), sl = (float)std::sin(ang - deg),
  43819. cr = (float)std::cos(ang + deg), sr = (float)std::sin(ang + deg);
  43820. const int
  43821. xl = x1 + (int)(l*cl), yl = y1 + (int)(l*sl),
  43822. xr = x1 + (int)(l*cr), yr = y1 + (int)(l*sr),
  43823. xc = x1 + (int)((l + 1)*(cl + cr))/2, yc = y1 + (int)((l + 1)*(sl + sr))/2;
  43824. draw_line(x0,y0,xc,yc,color,opacity,pattern).draw_triangle(x1,y1,xl,yl,xr,yr,color,opacity);
  43825. } else draw_point(x0,y0,color,opacity);
  43826. return *this;
  43827. }
  43828. //! Draw a 2D spline.
  43829. /**
  43830. \param x0 X-coordinate of the starting curve point
  43831. \param y0 Y-coordinate of the starting curve point
  43832. \param u0 X-coordinate of the starting velocity
  43833. \param v0 Y-coordinate of the starting velocity
  43834. \param x1 X-coordinate of the ending curve point
  43835. \param y1 Y-coordinate of the ending curve point
  43836. \param u1 X-coordinate of the ending velocity
  43837. \param v1 Y-coordinate of the ending velocity
  43838. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43839. \param precision Curve drawing precision.
  43840. \param opacity Drawing opacity.
  43841. \param pattern An integer whose bits describe the line pattern.
  43842. \param init_hatch If \c true, init hatch motif.
  43843. \note
  43844. - The curve is a 2D cubic Bezier spline, from the set of specified starting/ending points
  43845. and corresponding velocity vectors.
  43846. - The spline is drawn as a sequence of connected segments. The \p precision parameter sets the
  43847. average number of pixels in each drawn segment.
  43848. - A cubic Bezier curve is sometimes defined by a set of 4 points { (\p x0,\p y0), (\p xa,\p ya),
  43849. (\p xb,\p yb), (\p x1,\p y1) } where (\p x0,\p y0) is the starting point, (\p x1,\p y1) is the ending point
  43850. and (\p xa,\p ya), (\p xb,\p yb) are two
  43851. \e control points.
  43852. The starting and ending velocities (\p u0,\p v0) and (\p u1,\p v1) can be deduced easily from
  43853. the control points as
  43854. \p u0 = (\p xa - \p x0), \p v0 = (\p ya - \p y0), \p u1 = (\p x1 - \p xb) and \p v1 = (\p y1 - \p yb).
  43855. \par Example:
  43856. \code
  43857. CImg<unsigned char> img(100,100,1,3,0);
  43858. const unsigned char color[] = { 255,255,255 };
  43859. img.draw_spline(30,30,0,100,90,40,0,-100,color);
  43860. \endcode
  43861. **/
  43862. template<typename tc>
  43863. CImg<T>& draw_spline(const int x0, const int y0, const float u0, const float v0,
  43864. const int x1, const int y1, const float u1, const float v1,
  43865. const tc *const color, const float opacity=1,
  43866. const float precision=0.25, const unsigned int pattern=~0U,
  43867. const bool init_hatch=true) {
  43868. if (is_empty()) return *this;
  43869. if (!color)
  43870. throw CImgArgumentException(_cimg_instance
  43871. "draw_spline(): Specified color is (null).",
  43872. cimg_instance);
  43873. if (x0==x1 && y0==y1) return draw_point(x0,y0,color,opacity);
  43874. bool ninit_hatch = init_hatch;
  43875. const float
  43876. ax = u0 + u1 + 2*(x0 - x1),
  43877. bx = 3*(x1 - x0) - 2*u0 - u1,
  43878. ay = v0 + v1 + 2*(y0 - y1),
  43879. by = 3*(y1 - y0) - 2*v0 - v1,
  43880. _precision = 1/(cimg::hypot((float)x0 - x1,(float)y0 - y1)*(precision>0?precision:1));
  43881. int ox = x0, oy = y0;
  43882. for (float t = 0; t<1; t+=_precision) {
  43883. const float t2 = t*t, t3 = t2*t;
  43884. const int
  43885. nx = (int)(ax*t3 + bx*t2 + u0*t + x0),
  43886. ny = (int)(ay*t3 + by*t2 + v0*t + y0);
  43887. draw_line(ox,oy,nx,ny,color,opacity,pattern,ninit_hatch);
  43888. ninit_hatch = false;
  43889. ox = nx; oy = ny;
  43890. }
  43891. return draw_line(ox,oy,x1,y1,color,opacity,pattern,false);
  43892. }
  43893. //! Draw a textured 2D spline.
  43894. /**
  43895. \param x0 X-coordinate of the starting curve point
  43896. \param y0 Y-coordinate of the starting curve point
  43897. \param u0 X-coordinate of the starting velocity
  43898. \param v0 Y-coordinate of the starting velocity
  43899. \param x1 X-coordinate of the ending curve point
  43900. \param y1 Y-coordinate of the ending curve point
  43901. \param u1 X-coordinate of the ending velocity
  43902. \param v1 Y-coordinate of the ending velocity
  43903. \param texture Texture image defining line pixel colors.
  43904. \param tx0 X-coordinate of the starting texture point.
  43905. \param ty0 Y-coordinate of the starting texture point.
  43906. \param tx1 X-coordinate of the ending texture point.
  43907. \param ty1 Y-coordinate of the ending texture point.
  43908. \param precision Curve drawing precision.
  43909. \param opacity Drawing opacity.
  43910. \param pattern An integer whose bits describe the line pattern.
  43911. \param init_hatch if \c true, reinit hatch motif.
  43912. **/
  43913. template<typename t>
  43914. CImg<T>& draw_spline(const int x0, const int y0, const float u0, const float v0,
  43915. const int x1, const int y1, const float u1, const float v1,
  43916. const CImg<t>& texture,
  43917. const int tx0, const int ty0, const int tx1, const int ty1,
  43918. const float opacity=1,
  43919. const float precision=4, const unsigned int pattern=~0U,
  43920. const bool init_hatch=true) {
  43921. if (texture._depth>1 || texture._spectrum<_spectrum)
  43922. throw CImgArgumentException(_cimg_instance
  43923. "draw_spline(): Invalid specified texture (%u,%u,%u,%u,%p).",
  43924. cimg_instance,
  43925. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  43926. if (is_empty()) return *this;
  43927. if (is_overlapped(texture))
  43928. return draw_spline(x0,y0,u0,v0,x1,y1,u1,v1,+texture,tx0,ty0,tx1,ty1,precision,opacity,pattern,init_hatch);
  43929. if (x0==x1 && y0==y1)
  43930. return draw_point(x0,y0,texture.get_vector_at(x0<=0?0:x0>=texture.width()?texture.width() - 1:x0,
  43931. y0<=0?0:y0>=texture.height()?texture.height() - 1:y0).data(),
  43932. opacity);
  43933. bool ninit_hatch = init_hatch;
  43934. const float
  43935. ax = u0 + u1 + 2*(x0 - x1),
  43936. bx = 3*(x1 - x0) - 2*u0 - u1,
  43937. ay = v0 + v1 + 2*(y0 - y1),
  43938. by = 3*(y1 - y0) - 2*v0 - v1,
  43939. _precision = 1/(cimg::hypot((float)x0 - x1,(float)y0 - y1)*(precision>0?precision:1));
  43940. int ox = x0, oy = y0, otx = tx0, oty = ty0;
  43941. for (float t1 = 0; t1<1; t1+=_precision) {
  43942. const float t2 = t1*t1, t3 = t2*t1;
  43943. const int
  43944. nx = (int)(ax*t3 + bx*t2 + u0*t1 + x0),
  43945. ny = (int)(ay*t3 + by*t2 + v0*t1 + y0),
  43946. ntx = tx0 + (int)((tx1 - tx0)*t1),
  43947. nty = ty0 + (int)((ty1 - ty0)*t1);
  43948. draw_line(ox,oy,nx,ny,texture,otx,oty,ntx,nty,opacity,pattern,ninit_hatch);
  43949. ninit_hatch = false;
  43950. ox = nx; oy = ny; otx = ntx; oty = nty;
  43951. }
  43952. return draw_line(ox,oy,x1,y1,texture,otx,oty,tx1,ty1,opacity,pattern,false);
  43953. }
  43954. //! Draw a set of consecutive splines.
  43955. /**
  43956. \param points Vertices data.
  43957. \param tangents Tangents data.
  43958. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  43959. \param opacity Drawing opacity.
  43960. \param is_closed_set Tells if the drawn spline set is closed.
  43961. \param precision Precision of the drawing.
  43962. \param pattern An integer whose bits describe the line pattern.
  43963. \param init_hatch If \c true, init hatch motif.
  43964. **/
  43965. template<typename tp, typename tt, typename tc>
  43966. CImg<T>& draw_spline(const CImg<tp>& points, const CImg<tt>& tangents,
  43967. const tc *const color, const float opacity=1,
  43968. const bool is_closed_set=false, const float precision=4,
  43969. const unsigned int pattern=~0U, const bool init_hatch=true) {
  43970. if (is_empty() || !points || !tangents || points._width<2 || tangents._width<2) return *this;
  43971. bool ninit_hatch = init_hatch;
  43972. switch (points._height) {
  43973. case 0 : case 1 :
  43974. throw CImgArgumentException(_cimg_instance
  43975. "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).",
  43976. cimg_instance,
  43977. points._width,points._height,points._depth,points._spectrum,points._data);
  43978. default : {
  43979. const int x0 = (int)points(0,0), y0 = (int)points(0,1);
  43980. const float u0 = (float)tangents(0,0), v0 = (float)tangents(0,1);
  43981. int ox = x0, oy = y0;
  43982. float ou = u0, ov = v0;
  43983. for (unsigned int i = 1; i<points._width; ++i) {
  43984. const int x = (int)points(i,0), y = (int)points(i,1);
  43985. const float u = (float)tangents(i,0), v = (float)tangents(i,1);
  43986. draw_spline(ox,oy,ou,ov,x,y,u,v,color,precision,opacity,pattern,ninit_hatch);
  43987. ninit_hatch = false;
  43988. ox = x; oy = y; ou = u; ov = v;
  43989. }
  43990. if (is_closed_set) draw_spline(ox,oy,ou,ov,x0,y0,u0,v0,color,precision,opacity,pattern,false);
  43991. }
  43992. }
  43993. return *this;
  43994. }
  43995. //! Draw a set of consecutive splines \overloading.
  43996. /**
  43997. Similar to previous function, with the point tangents automatically estimated from the given points set.
  43998. **/
  43999. template<typename tp, typename tc>
  44000. CImg<T>& draw_spline(const CImg<tp>& points,
  44001. const tc *const color, const float opacity=1,
  44002. const bool is_closed_set=false, const float precision=4,
  44003. const unsigned int pattern=~0U, const bool init_hatch=true) {
  44004. if (is_empty() || !points || points._width<2) return *this;
  44005. CImg<Tfloat> tangents;
  44006. switch (points._height) {
  44007. case 0 : case 1 :
  44008. throw CImgArgumentException(_cimg_instance
  44009. "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).",
  44010. cimg_instance,
  44011. points._width,points._height,points._depth,points._spectrum,points._data);
  44012. case 2 : {
  44013. tangents.assign(points._width,points._height);
  44014. cimg_forX(points,p) {
  44015. const unsigned int
  44016. p0 = is_closed_set?(p + points.width() - 1)%points.width():(p?p - 1:0),
  44017. p1 = is_closed_set?(p + 1)%points.width():(p + 1<points.width()?p + 1:p);
  44018. const float
  44019. x = (float)points(p,0),
  44020. y = (float)points(p,1),
  44021. x0 = (float)points(p0,0),
  44022. y0 = (float)points(p0,1),
  44023. x1 = (float)points(p1,0),
  44024. y1 = (float)points(p1,1),
  44025. u0 = x - x0,
  44026. v0 = y - y0,
  44027. n0 = 1e-8f + cimg::hypot(u0,v0),
  44028. u1 = x1 - x,
  44029. v1 = y1 - y,
  44030. n1 = 1e-8f + cimg::hypot(u1,v1),
  44031. u = u0/n0 + u1/n1,
  44032. v = v0/n0 + v1/n1,
  44033. n = 1e-8f + cimg::hypot(u,v),
  44034. fact = 0.5f*(n0 + n1);
  44035. tangents(p,0) = (Tfloat)(fact*u/n);
  44036. tangents(p,1) = (Tfloat)(fact*v/n);
  44037. }
  44038. } break;
  44039. default : {
  44040. tangents.assign(points._width,points._height);
  44041. cimg_forX(points,p) {
  44042. const unsigned int
  44043. p0 = is_closed_set?(p + points.width() - 1)%points.width():(p?p - 1:0),
  44044. p1 = is_closed_set?(p + 1)%points.width():(p + 1<points.width()?p + 1:p);
  44045. const float
  44046. x = (float)points(p,0),
  44047. y = (float)points(p,1),
  44048. z = (float)points(p,2),
  44049. x0 = (float)points(p0,0),
  44050. y0 = (float)points(p0,1),
  44051. z0 = (float)points(p0,2),
  44052. x1 = (float)points(p1,0),
  44053. y1 = (float)points(p1,1),
  44054. z1 = (float)points(p1,2),
  44055. u0 = x - x0,
  44056. v0 = y - y0,
  44057. w0 = z - z0,
  44058. n0 = 1e-8f + cimg::hypot(u0,v0,w0),
  44059. u1 = x1 - x,
  44060. v1 = y1 - y,
  44061. w1 = z1 - z,
  44062. n1 = 1e-8f + cimg::hypot(u1,v1,w1),
  44063. u = u0/n0 + u1/n1,
  44064. v = v0/n0 + v1/n1,
  44065. w = w0/n0 + w1/n1,
  44066. n = 1e-8f + cimg::hypot(u,v,w),
  44067. fact = 0.5f*(n0 + n1);
  44068. tangents(p,0) = (Tfloat)(fact*u/n);
  44069. tangents(p,1) = (Tfloat)(fact*v/n);
  44070. tangents(p,2) = (Tfloat)(fact*w/n);
  44071. }
  44072. }
  44073. }
  44074. return draw_spline(points,tangents,color,opacity,is_closed_set,precision,pattern,init_hatch);
  44075. }
  44076. // [internal] Draw a filled triangle.
  44077. template<typename tc>
  44078. CImg<T>& _draw_triangle(int x0, int y0,
  44079. int x1, int y1,
  44080. int x2, int y2,
  44081. const tc *const color, const float opacity,
  44082. const float brightness) {
  44083. if (y0>y1) cimg::swap(x0,x1,y0,y1);
  44084. if (y0>y2) cimg::swap(x0,x2,y0,y2);
  44085. if (y1>y2) cimg::swap(x1,x2,y1,y2);
  44086. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44087. const int
  44088. h1 = height() - 1,
  44089. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
  44090. const longT
  44091. dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
  44092. dy01 = std::max((longT)1,(longT)y1 - y0),
  44093. dy02 = std::max((longT)1,(longT)y2 - y0),
  44094. dy12 = std::max((longT)1,(longT)y2 - y1),
  44095. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44096. const float cbs = cimg::cut(brightness,0,2);
  44097. cimg_init_scanline(opacity);
  44098. for (int y = cy0; y<=cy2; ++y) {
  44099. const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
  44100. longT
  44101. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44102. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44103. if (xm>xM) cimg::swap(xm,xM);
  44104. cimg_draw_scanline(xm,xM,y,color,opacity,cbs);
  44105. }
  44106. return *this;
  44107. }
  44108. //! Draw a filled 2D triangle.
  44109. /**
  44110. \param x0 X-coordinate of the first vertex.
  44111. \param y0 Y-coordinate of the first vertex.
  44112. \param x1 X-coordinate of the second vertex.
  44113. \param y1 Y-coordinate of the second vertex.
  44114. \param x2 X-coordinate of the third vertex.
  44115. \param y2 Y-coordinate of the third vertex.
  44116. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  44117. \param opacity Drawing opacity.
  44118. **/
  44119. template<typename tc>
  44120. CImg<T>& draw_triangle(const int x0, const int y0,
  44121. const int x1, const int y1,
  44122. const int x2, const int y2,
  44123. const tc *const color, const float opacity=1) {
  44124. if (is_empty()) return *this;
  44125. if (!color)
  44126. throw CImgArgumentException(_cimg_instance
  44127. "draw_triangle(): Specified color is (null).",
  44128. cimg_instance);
  44129. _draw_triangle(x0,y0,x1,y1,x2,y2,color,opacity,1);
  44130. return *this;
  44131. }
  44132. //! Draw a outlined 2D triangle.
  44133. /**
  44134. \param x0 X-coordinate of the first vertex.
  44135. \param y0 Y-coordinate of the first vertex.
  44136. \param x1 X-coordinate of the second vertex.
  44137. \param y1 Y-coordinate of the second vertex.
  44138. \param x2 X-coordinate of the third vertex.
  44139. \param y2 Y-coordinate of the third vertex.
  44140. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  44141. \param opacity Drawing opacity.
  44142. \param pattern An integer whose bits describe the outline pattern.
  44143. **/
  44144. template<typename tc>
  44145. CImg<T>& draw_triangle(const int x0, const int y0,
  44146. const int x1, const int y1,
  44147. const int x2, const int y2,
  44148. const tc *const color, const float opacity,
  44149. const unsigned int pattern) {
  44150. if (is_empty()) return *this;
  44151. if (!color)
  44152. throw CImgArgumentException(_cimg_instance
  44153. "draw_triangle(): Specified color is (null).",
  44154. cimg_instance);
  44155. draw_line(x0,y0,x1,y1,color,opacity,pattern,true).
  44156. draw_line(x1,y1,x2,y2,color,opacity,pattern,false).
  44157. draw_line(x2,y2,x0,y0,color,opacity,pattern,false);
  44158. return *this;
  44159. }
  44160. //! Draw a filled 2D triangle, with z-buffering.
  44161. /**
  44162. \param zbuffer Z-buffer image.
  44163. \param x0 X-coordinate of the first vertex.
  44164. \param y0 Y-coordinate of the first vertex.
  44165. \param z0 Z-coordinate of the first vertex.
  44166. \param x1 X-coordinate of the second vertex.
  44167. \param y1 Y-coordinate of the second vertex.
  44168. \param z1 Z-coordinate of the second vertex.
  44169. \param x2 X-coordinate of the third vertex.
  44170. \param y2 Y-coordinate of the third vertex.
  44171. \param z2 Z-coordinate of the third vertex.
  44172. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  44173. \param opacity Drawing opacity.
  44174. \param brightness Brightness factor.
  44175. **/
  44176. template<typename tz, typename tc>
  44177. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  44178. int x0, int y0, const float z0,
  44179. int x1, int y1, const float z1,
  44180. int x2, int y2, const float z2,
  44181. const tc *const color, const float opacity=1,
  44182. const float brightness=1) {
  44183. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  44184. if (!color)
  44185. throw CImgArgumentException(_cimg_instance
  44186. "draw_triangle(): Specified color is (null).",
  44187. cimg_instance);
  44188. if (!is_sameXY(zbuffer))
  44189. throw CImgArgumentException(_cimg_instance
  44190. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  44191. "different dimensions.",
  44192. cimg_instance,
  44193. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  44194. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  44195. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1);
  44196. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2);
  44197. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2);
  44198. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44199. const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
  44200. const longT
  44201. dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
  44202. dy01 = std::max((longT)1,(longT)y1 - y0),
  44203. dy02 = std::max((longT)1,(longT)y2 - y0),
  44204. dy12 = std::max((longT)1,(longT)y2 - y1),
  44205. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44206. const float diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1;
  44207. const float cbs = cimg::cut(brightness,0,2);
  44208. cimg_init_scanline(opacity);
  44209. for (int y = cy0; y<=cy2; ++y) {
  44210. const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
  44211. longT
  44212. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44213. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44214. float
  44215. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  44216. izM = iz0 + diz02*yy0/dy02;
  44217. if (xm>xM) cimg::swap(xm,xM,izm,izM);
  44218. if (xM>=0 && xm<=w1) {
  44219. const int
  44220. cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
  44221. cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
  44222. T *ptrd = data(cxm,y);
  44223. tz *ptrz = zbuffer.data(cxm,y);
  44224. const longT dxmM = std::max((longT)1,xM - xm);
  44225. const float dizmM = izM - izm;
  44226. for (int x = cxm; x<=cxM; ++x) {
  44227. const longT xxm = x - xm;
  44228. const float iz = izm + dizmM*xxm/dxmM;
  44229. if (iz>=*ptrz) {
  44230. *ptrz = (tz)iz;
  44231. cimg_forC(*this,c) {
  44232. const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
  44233. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44234. }
  44235. }
  44236. ++ptrd; ++ptrz;
  44237. }
  44238. }
  44239. }
  44240. return *this;
  44241. }
  44242. //! Draw a Gouraud-shaded 2D triangle.
  44243. /**
  44244. \param x0 X-coordinate of the first vertex in the image instance.
  44245. \param y0 Y-coordinate of the first vertex in the image instance.
  44246. \param x1 X-coordinate of the second vertex in the image instance.
  44247. \param y1 Y-coordinate of the second vertex in the image instance.
  44248. \param x2 X-coordinate of the third vertex in the image instance.
  44249. \param y2 Y-coordinate of the third vertex in the image instance.
  44250. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  44251. \param bs0 Brightness factor of the first vertex (in [0,2]).
  44252. \param bs1 brightness factor of the second vertex (in [0,2]).
  44253. \param bs2 brightness factor of the third vertex (in [0,2]).
  44254. \param opacity Drawing opacity.
  44255. **/
  44256. template<typename tc>
  44257. CImg<T>& draw_triangle(int x0, int y0,
  44258. int x1, int y1,
  44259. int x2, int y2,
  44260. const tc *const color,
  44261. float bs0,
  44262. float bs1,
  44263. float bs2,
  44264. const float opacity=1) {
  44265. if (is_empty()) return *this;
  44266. if (!color)
  44267. throw CImgArgumentException(_cimg_instance
  44268. "draw_triangle(): Specified color is (null).",
  44269. cimg_instance);
  44270. if (y0>y1) cimg::swap(x0,x1,y0,y1,bs0,bs1);
  44271. if (y0>y2) cimg::swap(x0,x2,y0,y2,bs0,bs2);
  44272. if (y1>y2) cimg::swap(x1,x2,y1,y2,bs1,bs2);
  44273. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44274. const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
  44275. const longT
  44276. dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
  44277. dy01 = std::max((longT)1,(longT)y1 - y0),
  44278. dy02 = std::max((longT)1,(longT)y2 - y0),
  44279. dy12 = std::max((longT)1,(longT)y2 - y1),
  44280. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44281. const float dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
  44282. cimg_init_scanline(opacity);
  44283. for (int y = cy0; y<=cy2; ++y) {
  44284. const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
  44285. longT
  44286. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44287. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44288. float
  44289. bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
  44290. bsM = bs0 + dbs02*yy0/dy02;
  44291. if (xm>xM) cimg::swap(xm,xM,bsm,bsM);
  44292. if (xM>=0 && xm<=w1) {
  44293. const int
  44294. cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
  44295. cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
  44296. T *ptrd = data(cxm,y);
  44297. const longT dxmM = std::max((longT)1,xM - xm);
  44298. const float dbsmM = bsM - bsm;
  44299. for (int x = cxm; x<=cxM; ++x) {
  44300. const longT xxm = (longT)x - xm;
  44301. const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
  44302. cimg_forC(*this,c) {
  44303. const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
  44304. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44305. }
  44306. ++ptrd;
  44307. }
  44308. }
  44309. }
  44310. return *this;
  44311. }
  44312. //! Draw a Gouraud-shaded 2D triangle, with z-buffering \overloading.
  44313. template<typename tz, typename tc>
  44314. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  44315. int x0, int y0, const float z0,
  44316. int x1, int y1, const float z1,
  44317. int x2, int y2, const float z2,
  44318. const tc *const color,
  44319. float bs0,
  44320. float bs1,
  44321. float bs2,
  44322. float opacity=1) {
  44323. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  44324. if (!color)
  44325. throw CImgArgumentException(_cimg_instance
  44326. "draw_triangle(): Specified color is (null).",
  44327. cimg_instance);
  44328. if (!is_sameXY(zbuffer))
  44329. throw CImgArgumentException(_cimg_instance
  44330. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  44331. "different dimensions.",
  44332. cimg_instance,
  44333. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  44334. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  44335. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,bs0,bs1);
  44336. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,bs0,bs2);
  44337. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,bs1,bs2);
  44338. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44339. const int
  44340. w1 = width() - 1, h1 = height() - 1,
  44341. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44342. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44343. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44344. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44345. const float
  44346. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  44347. dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
  44348. cimg_init_scanline(opacity);
  44349. for (int y = cy0; y<=cy2; ++y) {
  44350. const int yy0 = y - y0, yy1 = y - y1;
  44351. int
  44352. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44353. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44354. float
  44355. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  44356. izM = iz0 + diz02*yy0/dy02,
  44357. bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
  44358. bsM = bs0 + dbs02*yy0/dy02;
  44359. if (xm>xM) cimg::swap(xm,xM,izm,izM,bsm,bsM);
  44360. if (xM>=0 && xm<=w1) {
  44361. const int
  44362. cxm = cimg::cut(xm,0,w1),
  44363. cxM = cimg::cut(xM,0,w1);
  44364. T *ptrd = data(cxm,y);
  44365. tz *ptrz = zbuffer.data(cxm,y);
  44366. const int dxmM = std::max(1,xM - xm);
  44367. const float dizmM = izM - izm, dbsmM = bsM - bsm;
  44368. for (int x = cxm; x<=cxM; ++x) {
  44369. const int xxm = x - xm;
  44370. const float iz = izm + dizmM*xxm/dxmM;
  44371. if (iz>=*ptrz) {
  44372. *ptrz = (tz)iz;
  44373. const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
  44374. cimg_forC(*this,c) {
  44375. const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
  44376. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44377. }
  44378. }
  44379. ++ptrd; ++ptrz;
  44380. }
  44381. }
  44382. }
  44383. return *this;
  44384. }
  44385. //! Draw a color-interpolated 2D triangle.
  44386. /**
  44387. \param x0 X-coordinate of the first vertex in the image instance.
  44388. \param y0 Y-coordinate of the first vertex in the image instance.
  44389. \param x1 X-coordinate of the second vertex in the image instance.
  44390. \param y1 Y-coordinate of the second vertex in the image instance.
  44391. \param x2 X-coordinate of the third vertex in the image instance.
  44392. \param y2 Y-coordinate of the third vertex in the image instance.
  44393. \param color1 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the first vertex.
  44394. \param color2 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the second vertex.
  44395. \param color3 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the third vertex.
  44396. \param opacity Drawing opacity.
  44397. **/
  44398. template<typename tc>
  44399. CImg<T>& draw_triangle(int x0, int y0,
  44400. int x1, int y1,
  44401. int x2, int y2,
  44402. const tc *color0,
  44403. const tc *color1,
  44404. const tc *color2,
  44405. const float opacity=1) {
  44406. typedef typename cimg::superset<tc,int>::type stc;
  44407. if (is_empty()) return *this;
  44408. if (!color0 || !color1 || !color2)
  44409. throw CImgArgumentException(_cimg_instance
  44410. "draw_triangle(): One of the specified color is (null).",
  44411. cimg_instance);
  44412. if (y0>y1) cimg::swap(x0,x1,y0,y1,color0,color1);
  44413. if (y0>y2) cimg::swap(x0,x2,y0,y2,color0,color2);
  44414. if (y1>y2) cimg::swap(x1,x2,y1,y2,color1,color2);
  44415. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44416. const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
  44417. const longT
  44418. dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
  44419. dy01 = std::max((longT)1,(longT)y1 - y0),
  44420. dy02 = std::max((longT)1,(longT)y2 - y0),
  44421. dy12 = std::max((longT)1,(longT)y2 - y1),
  44422. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44423. cimg_init_scanline(opacity);
  44424. cimg_forC(*this,c) {
  44425. const stc dcolor01 = color1[c] - color0[c], dcolor02 = color2[c] - color0[c], dcolor12 = color2[c] - color1[c];
  44426. for (int y = cy0; y<=cy2; ++y) {
  44427. const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
  44428. longT
  44429. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44430. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44431. stc
  44432. colorm = y<y1?(color0[c] + dcolor01*yy0/dy01):(color1[c] + dcolor12*yy1/dy12),
  44433. colorM = color0[c] + dcolor02*yy0/dy02;
  44434. if (xm>xM) cimg::swap(xm,xM,colorm,colorM);
  44435. if (xM>=0 && xm<=w1) {
  44436. const int
  44437. cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
  44438. cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
  44439. T *ptrd = data(cxm,y);
  44440. const longT dxmM = std::max((longT)1,xM - xm);
  44441. const stc dcolormM = colorM - colorm;
  44442. for (int x = cxm; x<=cxM; ++x) {
  44443. const longT xxm = (longT)x - xm;
  44444. const stc col = colorm + dcolormM*xxm/dxmM;
  44445. ptrd[c*_sc_whd] = (T)(opacity>=1?col:col*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44446. ++ptrd;
  44447. }
  44448. }
  44449. }
  44450. }
  44451. return *this;
  44452. }
  44453. //! Draw a textured 2D triangle.
  44454. /**
  44455. \param x0 X-coordinate of the first vertex in the image instance.
  44456. \param y0 Y-coordinate of the first vertex in the image instance.
  44457. \param x1 X-coordinate of the second vertex in the image instance.
  44458. \param y1 Y-coordinate of the second vertex in the image instance.
  44459. \param x2 X-coordinate of the third vertex in the image instance.
  44460. \param y2 Y-coordinate of the third vertex in the image instance.
  44461. \param texture Texture image used to fill the triangle.
  44462. \param tx0 X-coordinate of the first vertex in the texture image.
  44463. \param ty0 Y-coordinate of the first vertex in the texture image.
  44464. \param tx1 X-coordinate of the second vertex in the texture image.
  44465. \param ty1 Y-coordinate of the second vertex in the texture image.
  44466. \param tx2 X-coordinate of the third vertex in the texture image.
  44467. \param ty2 Y-coordinate of the third vertex in the texture image.
  44468. \param opacity Drawing opacity.
  44469. \param brightness Brightness factor of the drawing (in [0,2]).
  44470. **/
  44471. template<typename tc>
  44472. CImg<T>& draw_triangle(int x0, int y0,
  44473. int x1, int y1,
  44474. int x2, int y2,
  44475. const CImg<tc>& texture,
  44476. int tx0, int ty0,
  44477. int tx1, int ty1,
  44478. int tx2, int ty2,
  44479. const float opacity=1,
  44480. const float brightness=1) {
  44481. if (is_empty()) return *this;
  44482. if (texture._depth>1 || texture._spectrum<_spectrum)
  44483. throw CImgArgumentException(_cimg_instance
  44484. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  44485. cimg_instance,
  44486. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  44487. if (is_overlapped(texture))
  44488. return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
  44489. if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1);
  44490. if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2);
  44491. if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,ty1,tx2,ty2);
  44492. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44493. const int
  44494. w1 = width() - 1, h1 = height() - 1,
  44495. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44496. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44497. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44498. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
  44499. dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
  44500. dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
  44501. hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
  44502. hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2;
  44503. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  44504. const float cbs = cimg::cut(brightness,0,2);
  44505. cimg_init_scanline(opacity);
  44506. for (int y = cy0; y<=cy2; ++y) {
  44507. const int yy0 = y - y0, yy1 = y - y1;
  44508. int
  44509. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44510. xM = x0 + (dx02*yy0 + hdy02)/dy02,
  44511. txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
  44512. txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
  44513. tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
  44514. tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02;
  44515. if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM);
  44516. if (xM>=0 && xm<=w1) {
  44517. const int
  44518. cxm = cimg::cut(xm,0,w1),
  44519. cxM = cimg::cut(xM,0,w1);
  44520. T *ptrd = data(cxm,y);
  44521. const int
  44522. dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
  44523. dtxmM = txM - txm, dtymM = tyM - tym;
  44524. for (int x = cxm; x<=cxM; ++x) {
  44525. const int
  44526. xxm = x - xm,
  44527. tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
  44528. ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM;
  44529. const tc *const color = &texture._atXY(tx,ty);
  44530. cimg_forC(*this,c) {
  44531. const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
  44532. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44533. }
  44534. ++ptrd;
  44535. }
  44536. }
  44537. }
  44538. return *this;
  44539. }
  44540. //! Draw a 2D textured triangle, with perspective correction.
  44541. template<typename tc>
  44542. CImg<T>& draw_triangle(int x0, int y0, const float z0,
  44543. int x1, int y1, const float z1,
  44544. int x2, int y2, const float z2,
  44545. const CImg<tc>& texture,
  44546. int tx0, int ty0,
  44547. int tx1, int ty1,
  44548. int tx2, int ty2,
  44549. const float opacity=1,
  44550. const float brightness=1) {
  44551. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  44552. if (texture._depth>1 || texture._spectrum<_spectrum)
  44553. throw CImgArgumentException(_cimg_instance
  44554. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  44555. cimg_instance,
  44556. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  44557. if (is_overlapped(texture))
  44558. return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
  44559. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  44560. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1);
  44561. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2);
  44562. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2);
  44563. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44564. const int
  44565. w1 = width() - 1, h1 = height() - 1,
  44566. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44567. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44568. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44569. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44570. const float
  44571. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  44572. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  44573. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  44574. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  44575. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1;
  44576. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  44577. const float cbs = cimg::cut(brightness,0,2);
  44578. cimg_init_scanline(opacity);
  44579. for (int y = cy0; y<=cy2; ++y) {
  44580. const int yy0 = y - y0, yy1 = y - y1;
  44581. int
  44582. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44583. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44584. float
  44585. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  44586. izM = iz0 + diz02*yy0/dy02,
  44587. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  44588. txzM = txz0 + dtxz02*yy0/dy02,
  44589. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  44590. tyzM = tyz0 + dtyz02*yy0/dy02;
  44591. if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM);
  44592. if (xM>=0 && xm<=w1) {
  44593. const int
  44594. cxm = cimg::cut(xm,0,w1),
  44595. cxM = cimg::cut(xM,0,w1);
  44596. T *ptrd = data(cxm,y);
  44597. const int dxmM = std::max(1,xM - xm);
  44598. const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm;
  44599. for (int x = cxm; x<=cxM; ++x) {
  44600. const int xxm = x - xm;
  44601. const float
  44602. iz = izm + dizmM*xxm/dxmM,
  44603. txz = txzm + dtxzmM*xxm/dxmM,
  44604. tyz = tyzm + dtyzmM*xxm/dxmM;
  44605. const int
  44606. tx = (int)cimg::round(txz/iz),
  44607. ty = (int)cimg::round(tyz/iz);
  44608. const tc *const color = &texture._atXY(tx,ty);
  44609. cimg_forC(*this,c) {
  44610. const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
  44611. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44612. }
  44613. ++ptrd;
  44614. }
  44615. }
  44616. }
  44617. return *this;
  44618. }
  44619. //! Draw a textured 2D triangle, with perspective correction and z-buffering.
  44620. template<typename tz, typename tc>
  44621. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  44622. int x0, int y0, const float z0,
  44623. int x1, int y1, const float z1,
  44624. int x2, int y2, const float z2,
  44625. const CImg<tc>& texture,
  44626. int tx0, int ty0,
  44627. int tx1, int ty1,
  44628. int tx2, int ty2,
  44629. const float opacity=1,
  44630. const float brightness=1) {
  44631. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  44632. if (!is_sameXY(zbuffer))
  44633. throw CImgArgumentException(_cimg_instance
  44634. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  44635. "different dimensions.",
  44636. cimg_instance,
  44637. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  44638. if (texture._depth>1 || texture._spectrum<_spectrum)
  44639. throw CImgArgumentException(_cimg_instance
  44640. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  44641. cimg_instance,
  44642. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  44643. if (is_overlapped(texture))
  44644. return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
  44645. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  44646. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1);
  44647. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2);
  44648. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2);
  44649. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44650. const int
  44651. w1 = width() - 1, h1 = height() - 1,
  44652. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44653. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44654. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44655. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  44656. const float
  44657. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  44658. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  44659. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  44660. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  44661. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1;
  44662. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  44663. const float cbs = cimg::cut(brightness,0,2);
  44664. cimg_init_scanline(opacity);
  44665. for (int y = cy0; y<=cy2; ++y) {
  44666. const int yy0 = y - y0, yy1 = y - y1;
  44667. int
  44668. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44669. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  44670. float
  44671. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  44672. izM = iz0 + diz02*yy0/dy02,
  44673. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  44674. txzM = txz0 + dtxz02*yy0/dy02,
  44675. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  44676. tyzM = tyz0 + dtyz02*yy0/dy02;
  44677. if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM);
  44678. if (xM>=0 && xm<=w1) {
  44679. const int
  44680. cxm = cimg::cut(xm,0,w1),
  44681. cxM = cimg::cut(xM,0,w1);
  44682. T *ptrd = data(cxm,y);
  44683. tz *ptrz = zbuffer.data(cxm,y);
  44684. const int dxmM = std::max(1,xM - xm);
  44685. const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm;
  44686. for (int x = cxm; x<=cxM; ++x) {
  44687. const int xxm = x - xm;
  44688. const float iz = izm + dizmM*xxm/dxmM;
  44689. if (iz>=*ptrz) {
  44690. *ptrz = (tz)iz;
  44691. const float
  44692. txz = txzm + dtxzmM*xxm/dxmM,
  44693. tyz = tyzm + dtyzmM*xxm/dxmM;
  44694. const int
  44695. tx = (int)cimg::round(txz/iz),
  44696. ty = (int)cimg::round(tyz/iz);
  44697. const tc *const color = &texture._atXY(tx,ty);
  44698. cimg_forC(*this,c) {
  44699. const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
  44700. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44701. }
  44702. }
  44703. ++ptrd; ++ptrz;
  44704. }
  44705. }
  44706. }
  44707. return *this;
  44708. }
  44709. //! Draw a Phong-shaded 2D triangle.
  44710. /**
  44711. \param x0 X-coordinate of the first vertex in the image instance.
  44712. \param y0 Y-coordinate of the first vertex in the image instance.
  44713. \param x1 X-coordinate of the second vertex in the image instance.
  44714. \param y1 Y-coordinate of the second vertex in the image instance.
  44715. \param x2 X-coordinate of the third vertex in the image instance.
  44716. \param y2 Y-coordinate of the third vertex in the image instance.
  44717. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  44718. \param light Light image.
  44719. \param lx0 X-coordinate of the first vertex in the light image.
  44720. \param ly0 Y-coordinate of the first vertex in the light image.
  44721. \param lx1 X-coordinate of the second vertex in the light image.
  44722. \param ly1 Y-coordinate of the second vertex in the light image.
  44723. \param lx2 X-coordinate of the third vertex in the light image.
  44724. \param ly2 Y-coordinate of the third vertex in the light image.
  44725. \param opacity Drawing opacity.
  44726. **/
  44727. template<typename tc, typename tl>
  44728. CImg<T>& draw_triangle(int x0, int y0,
  44729. int x1, int y1,
  44730. int x2, int y2,
  44731. const tc *const color,
  44732. const CImg<tl>& light,
  44733. int lx0, int ly0,
  44734. int lx1, int ly1,
  44735. int lx2, int ly2,
  44736. const float opacity=1) {
  44737. if (is_empty()) return *this;
  44738. if (!color)
  44739. throw CImgArgumentException(_cimg_instance
  44740. "draw_triangle(): Specified color is (null).",
  44741. cimg_instance);
  44742. if (light._depth>1 || light._spectrum<_spectrum)
  44743. throw CImgArgumentException(_cimg_instance
  44744. "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
  44745. cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
  44746. if (y0>y1) cimg::swap(x0,x1,y0,y1,lx0,lx1,ly0,ly1);
  44747. if (y0>y2) cimg::swap(x0,x2,y0,y2,lx0,lx2,ly0,ly2);
  44748. if (y1>y2) cimg::swap(x1,x2,y1,y2,lx1,lx2,ly1,ly2);
  44749. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44750. const int
  44751. w1 = width() - 1, h1 = height() - 1,
  44752. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44753. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44754. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44755. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
  44756. dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
  44757. dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
  44758. hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
  44759. hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
  44760. const ulongT lwhd = (ulongT)light._width*light._height*light._depth;
  44761. cimg_init_scanline(opacity);
  44762. for (int y = cy0; y<=cy2; ++y) {
  44763. const int yy0 = y - y0, yy1 = y - y1;
  44764. int
  44765. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44766. xM = x0 + (dx02*yy0 + hdy02)/dy02,
  44767. lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
  44768. lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
  44769. lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
  44770. lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
  44771. if (xm>xM) cimg::swap(xm,xM,lxm,lxM,lym,lyM);
  44772. if (xM>=0 && xm<=w1) {
  44773. const int
  44774. cxm = cimg::cut(xm,0,w1),
  44775. cxM = cimg::cut(xM,0,w1);
  44776. T *ptrd = data(cxm,y);
  44777. const int
  44778. dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
  44779. dlxmM = lxM - lxm, dlymM = lyM - lym;
  44780. for (int x = cxm; x<=cxM; ++x) {
  44781. const int
  44782. xxm = x - xm,
  44783. lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
  44784. ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
  44785. const tl *const lig = &light._atXY(lx,ly);
  44786. cimg_forC(*this,c) {
  44787. const tc col = color[c];
  44788. const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
  44789. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  44790. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44791. }
  44792. ++ptrd;
  44793. }
  44794. }
  44795. }
  44796. return *this;
  44797. }
  44798. //! Draw a Phong-shaded 2D triangle, with z-buffering.
  44799. template<typename tz, typename tc, typename tl>
  44800. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  44801. int x0, int y0, const float z0,
  44802. int x1, int y1, const float z1,
  44803. int x2, int y2, const float z2,
  44804. const tc *const color,
  44805. const CImg<tl>& light,
  44806. int lx0, int ly0,
  44807. int lx1, int ly1,
  44808. int lx2, int ly2,
  44809. const float opacity=1) {
  44810. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  44811. if (!color)
  44812. throw CImgArgumentException(_cimg_instance
  44813. "draw_triangle(): Specified color is (null).",
  44814. cimg_instance);
  44815. if (light._depth>1 || light._spectrum<_spectrum)
  44816. throw CImgArgumentException(_cimg_instance
  44817. "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
  44818. cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
  44819. if (!is_sameXY(zbuffer))
  44820. throw CImgArgumentException(_cimg_instance
  44821. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  44822. "different dimensions.",
  44823. cimg_instance,
  44824. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  44825. if (is_overlapped(light)) return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,
  44826. +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  44827. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  44828. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,lx0,lx1,ly0,ly1);
  44829. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,lx0,lx2,ly0,ly2);
  44830. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,lx1,lx2,ly1,ly2);
  44831. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44832. const int
  44833. w1 = width() - 1, h1 = height() - 1,
  44834. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44835. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44836. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44837. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
  44838. dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
  44839. dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
  44840. hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
  44841. hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
  44842. const float diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1;
  44843. const ulongT lwhd = (ulongT)light._width*light._height*light._depth;
  44844. cimg_init_scanline(opacity);
  44845. for (int y = cy0; y<=cy2; ++y) {
  44846. const int yy0 = y - y0, yy1 = y - y1;
  44847. int
  44848. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44849. xM = x0 + (dx02*yy0 + hdy02)/dy02,
  44850. lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
  44851. lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
  44852. lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
  44853. lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
  44854. float
  44855. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  44856. izM = iz0 + diz02*yy0/dy02;
  44857. if (xm>xM) cimg::swap(xm,xM,lxm,lxM,lym,lyM,izm,izM);
  44858. if (xM>=0 && xm<=w1) {
  44859. const int
  44860. cxm = cimg::cut(xm,0,w1),
  44861. cxM = cimg::cut(xM,0,w1);
  44862. T *ptrd = data(cxm,y);
  44863. tz *ptrz = zbuffer.data(cxm,y);
  44864. const int
  44865. dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
  44866. dlxmM = lxM - lxm, dlymM = lyM - lym;
  44867. const float dizmM = izM - izm;
  44868. for (int x = cxm; x<=cxM; ++x) {
  44869. const int xxm = x - xm;
  44870. const float iz = izm + dizmM*xxm/dxmM;
  44871. if (iz>=*ptrz) {
  44872. *ptrz = (tz)iz;
  44873. const int
  44874. lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
  44875. ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
  44876. const tl *const lig = &light._atXY(lx,ly);
  44877. cimg_forC(*this,c) {
  44878. const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
  44879. const tc col = color[c];
  44880. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  44881. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44882. }
  44883. }
  44884. ++ptrd; ++ptrz;
  44885. }
  44886. }
  44887. }
  44888. return *this;
  44889. }
  44890. //! Draw a textured Gouraud-shaded 2D triangle.
  44891. /**
  44892. \param x0 X-coordinate of the first vertex in the image instance.
  44893. \param y0 Y-coordinate of the first vertex in the image instance.
  44894. \param x1 X-coordinate of the second vertex in the image instance.
  44895. \param y1 Y-coordinate of the second vertex in the image instance.
  44896. \param x2 X-coordinate of the third vertex in the image instance.
  44897. \param y2 Y-coordinate of the third vertex in the image instance.
  44898. \param texture Texture image used to fill the triangle.
  44899. \param tx0 X-coordinate of the first vertex in the texture image.
  44900. \param ty0 Y-coordinate of the first vertex in the texture image.
  44901. \param tx1 X-coordinate of the second vertex in the texture image.
  44902. \param ty1 Y-coordinate of the second vertex in the texture image.
  44903. \param tx2 X-coordinate of the third vertex in the texture image.
  44904. \param ty2 Y-coordinate of the third vertex in the texture image.
  44905. \param bs0 Brightness factor of the first vertex.
  44906. \param bs1 Brightness factor of the second vertex.
  44907. \param bs2 Brightness factor of the third vertex.
  44908. \param opacity Drawing opacity.
  44909. **/
  44910. template<typename tc>
  44911. CImg<T>& draw_triangle(int x0, int y0,
  44912. int x1, int y1,
  44913. int x2, int y2,
  44914. const CImg<tc>& texture,
  44915. int tx0, int ty0,
  44916. int tx1, int ty1,
  44917. int tx2, int ty2,
  44918. float bs0,
  44919. float bs1,
  44920. float bs2,
  44921. const float opacity=1) {
  44922. if (is_empty()) return *this;
  44923. if (texture._depth>1 || texture._spectrum<_spectrum)
  44924. throw CImgArgumentException(_cimg_instance
  44925. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  44926. cimg_instance,
  44927. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  44928. if (is_overlapped(texture))
  44929. return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
  44930. bs0,bs1,bs2,opacity);
  44931. if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1,bs0,bs1);
  44932. if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2,bs0,bs2);
  44933. if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,tx2,ty1,ty2,bs1,bs2);
  44934. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  44935. const int
  44936. w1 = width() - 1, h1 = height() - 1,
  44937. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  44938. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  44939. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  44940. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
  44941. dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
  44942. dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
  44943. hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
  44944. hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2;
  44945. const float dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
  44946. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  44947. cimg_init_scanline(opacity);
  44948. for (int y = cy0; y<=cy2; ++y) {
  44949. const int yy0 = y - y0, yy1 = y - y1;
  44950. int
  44951. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  44952. xM = x0 + (dx02*yy0 + hdy02)/dy02,
  44953. txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
  44954. txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
  44955. tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
  44956. tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02;
  44957. float
  44958. bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
  44959. bsM = bs0 + dbs02*yy0/dy02;
  44960. if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM,bsm,bsM);
  44961. if (xM>=0 && xm<=w1) {
  44962. const int
  44963. cxm = cimg::cut(xm,0,w1),
  44964. cxM = cimg::cut(xM,0,w1);
  44965. T *ptrd = data(cxm,y);
  44966. const int
  44967. dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
  44968. dtxmM = txM - txm, dtymM = tyM - tym;
  44969. const float dbsmM = bsM - bsm;
  44970. for (int x = cxm; x<=cxM; ++x) {
  44971. const int
  44972. xxm = x - xm,
  44973. tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
  44974. ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM;
  44975. const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
  44976. const tc *const color = &texture._atXY(tx,ty);
  44977. cimg_forC(*this,c) {
  44978. const tc col = color[c*twhd];
  44979. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  44980. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  44981. }
  44982. ++ptrd;
  44983. }
  44984. }
  44985. }
  44986. return *this;
  44987. }
  44988. //! Draw a textured Gouraud-shaded 2D triangle, with perspective correction \overloading.
  44989. template<typename tc>
  44990. CImg<T>& draw_triangle(int x0, int y0, const float z0,
  44991. int x1, int y1, const float z1,
  44992. int x2, int y2, const float z2,
  44993. const CImg<tc>& texture,
  44994. int tx0, int ty0,
  44995. int tx1, int ty1,
  44996. int tx2, int ty2,
  44997. float bs0,
  44998. float bs1,
  44999. float bs2,
  45000. const float opacity=1) {
  45001. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  45002. if (texture._depth>1 || texture._spectrum<_spectrum)
  45003. throw CImgArgumentException(_cimg_instance
  45004. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  45005. cimg_instance,
  45006. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  45007. if (is_overlapped(texture)) return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
  45008. bs0,bs1,bs2,opacity);
  45009. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  45010. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,bs0,bs1);
  45011. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,bs0,bs2);
  45012. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,bs1,bs2);
  45013. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  45014. const int
  45015. w1 = width() - 1, h1 = height() - 1,
  45016. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  45017. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  45018. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  45019. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  45020. const float
  45021. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  45022. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  45023. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  45024. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  45025. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
  45026. dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
  45027. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  45028. cimg_init_scanline(opacity);
  45029. for (int y = cy0; y<=cy2; ++y) {
  45030. const int yy0 = y - y0, yy1 = y - y1;
  45031. int
  45032. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  45033. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  45034. float
  45035. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  45036. izM = iz0 + diz02*yy0/dy02,
  45037. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  45038. txzM = txz0 + dtxz02*yy0/dy02,
  45039. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  45040. tyzM = tyz0 + dtyz02*yy0/dy02,
  45041. bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
  45042. bsM = bs0 + dbs02*yy0/dy02;
  45043. if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM,bsm,bsM);
  45044. if (xM>=0 && xm<=w1) {
  45045. const int
  45046. cxm = cimg::cut(xm,0,w1),
  45047. cxM = cimg::cut(xM,0,w1);
  45048. T *ptrd = data(cxm,y);
  45049. const int dxmM = std::max(1,xM - xm);
  45050. const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm, dbsmM = bsM - bsm;
  45051. for (int x = cxm; x<=cxM; ++x) {
  45052. const int xxm = x - xm;
  45053. const float
  45054. iz = izm + dizmM*xxm/dxmM,
  45055. txz = txzm + dtxzmM*xxm/dxmM,
  45056. tyz = tyzm + dtyzmM*xxm/dxmM,
  45057. cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
  45058. const int
  45059. tx = (int)cimg::round(txz/iz),
  45060. ty = (int)cimg::round(tyz/iz);
  45061. const tc *const color = &texture._atXY(tx,ty);
  45062. cimg_forC(*this,c) {
  45063. const tc col = color[c*twhd];
  45064. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  45065. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  45066. }
  45067. ++ptrd;
  45068. }
  45069. }
  45070. }
  45071. return *this;
  45072. }
  45073. //! Draw a textured Gouraud-shaded 2D triangle, with perspective correction and z-buffering \overloading.
  45074. template<typename tz, typename tc>
  45075. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  45076. int x0, int y0, const float z0,
  45077. int x1, int y1, const float z1,
  45078. int x2, int y2, const float z2,
  45079. const CImg<tc>& texture,
  45080. int tx0, int ty0,
  45081. int tx1, int ty1,
  45082. int tx2, int ty2,
  45083. float bs0,
  45084. float bs1,
  45085. float bs2,
  45086. const float opacity=1) {
  45087. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  45088. if (!is_sameXY(zbuffer))
  45089. throw CImgArgumentException(_cimg_instance
  45090. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  45091. "different dimensions.",
  45092. cimg_instance,
  45093. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  45094. if (texture._depth>1 || texture._spectrum<_spectrum)
  45095. throw CImgArgumentException(_cimg_instance
  45096. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  45097. cimg_instance,
  45098. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  45099. if (is_overlapped(texture))
  45100. return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,bs0,bs1,bs2,opacity);
  45101. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  45102. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,bs0,bs1);
  45103. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,bs0,bs2);
  45104. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,bs1,bs2);
  45105. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  45106. const int
  45107. w1 = width() - 1, h1 = height() - 1,
  45108. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  45109. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  45110. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  45111. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  45112. const float
  45113. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  45114. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  45115. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  45116. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  45117. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
  45118. dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
  45119. const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
  45120. cimg_init_scanline(opacity);
  45121. for (int y = cy0; y<=cy2; ++y) {
  45122. const int yy0 = y - y0, yy1 = y - y1;
  45123. int
  45124. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  45125. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  45126. float
  45127. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  45128. izM = iz0 + diz02*yy0/dy02,
  45129. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  45130. txzM = txz0 + dtxz02*yy0/dy02,
  45131. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  45132. tyzM = tyz0 + dtyz02*yy0/dy02,
  45133. bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
  45134. bsM = bs0 + dbs02*yy0/dy02;
  45135. if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM,bsm,bsM);
  45136. if (xM>=0 && xm<=w1) {
  45137. const int
  45138. cxm = cimg::cut(xm,0,w1),
  45139. cxM = cimg::cut(xM,0,w1);
  45140. T *ptrd = data(cxm,y);
  45141. tz *ptrz = zbuffer.data(cxm,y);
  45142. const int dxmM = std::max(1,xM - xm);
  45143. const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm, dbsmM = bsM - bsm;
  45144. for (int x = cxm; x<=cxM; ++x) {
  45145. const int xxm = x - xm;
  45146. const float iz = izm + dizmM*xxm/dxmM;
  45147. if (iz>=*ptrz) {
  45148. *ptrz = (tz)iz;
  45149. const float
  45150. txz = txzm + dtxzmM*xxm/dxmM,
  45151. tyz = tyzm + dtyzmM*xxm/dxmM,
  45152. cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
  45153. const int
  45154. tx = (int)cimg::round(txz/iz),
  45155. ty = (int)cimg::round(tyz/iz);
  45156. const tc *const color = &texture._atXY(tx,ty);
  45157. cimg_forC(*this,c) {
  45158. const tc col = color[c*twhd];
  45159. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  45160. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  45161. }
  45162. }
  45163. ++ptrd; ++ptrz;
  45164. }
  45165. }
  45166. }
  45167. return *this;
  45168. }
  45169. //! Draw a textured Phong-shaded 2D triangle.
  45170. /**
  45171. \param x0 X-coordinate of the first vertex in the image instance.
  45172. \param y0 Y-coordinate of the first vertex in the image instance.
  45173. \param x1 X-coordinate of the second vertex in the image instance.
  45174. \param y1 Y-coordinate of the second vertex in the image instance.
  45175. \param x2 X-coordinate of the third vertex in the image instance.
  45176. \param y2 Y-coordinate of the third vertex in the image instance.
  45177. \param texture Texture image used to fill the triangle.
  45178. \param tx0 X-coordinate of the first vertex in the texture image.
  45179. \param ty0 Y-coordinate of the first vertex in the texture image.
  45180. \param tx1 X-coordinate of the second vertex in the texture image.
  45181. \param ty1 Y-coordinate of the second vertex in the texture image.
  45182. \param tx2 X-coordinate of the third vertex in the texture image.
  45183. \param ty2 Y-coordinate of the third vertex in the texture image.
  45184. \param light Light image.
  45185. \param lx0 X-coordinate of the first vertex in the light image.
  45186. \param ly0 Y-coordinate of the first vertex in the light image.
  45187. \param lx1 X-coordinate of the second vertex in the light image.
  45188. \param ly1 Y-coordinate of the second vertex in the light image.
  45189. \param lx2 X-coordinate of the third vertex in the light image.
  45190. \param ly2 Y-coordinate of the third vertex in the light image.
  45191. \param opacity Drawing opacity.
  45192. **/
  45193. template<typename tc, typename tl>
  45194. CImg<T>& draw_triangle(int x0, int y0,
  45195. int x1, int y1,
  45196. int x2, int y2,
  45197. const CImg<tc>& texture,
  45198. int tx0, int ty0,
  45199. int tx1, int ty1,
  45200. int tx2, int ty2,
  45201. const CImg<tl>& light,
  45202. int lx0, int ly0,
  45203. int lx1, int ly1,
  45204. int lx2, int ly2,
  45205. const float opacity=1) {
  45206. if (is_empty()) return *this;
  45207. if (texture._depth>1 || texture._spectrum<_spectrum)
  45208. throw CImgArgumentException(_cimg_instance
  45209. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  45210. cimg_instance,
  45211. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  45212. if (light._depth>1 || light._spectrum<_spectrum)
  45213. throw CImgArgumentException(_cimg_instance
  45214. "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
  45215. cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
  45216. if (is_overlapped(texture))
  45217. return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45218. if (is_overlapped(light))
  45219. return draw_triangle(x0,y0,x1,y1,x2,y2,texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45220. if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
  45221. if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
  45222. if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
  45223. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  45224. const int
  45225. w1 = width() - 1, h1 = height() - 1,
  45226. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  45227. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  45228. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  45229. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
  45230. dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
  45231. dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
  45232. hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
  45233. hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2,
  45234. dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
  45235. dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
  45236. hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
  45237. hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
  45238. const ulongT
  45239. twhd = (ulongT)texture._width*texture._height*texture._depth,
  45240. lwhd = (ulongT)light._width*light._height*light._depth;
  45241. cimg_init_scanline(opacity);
  45242. for (int y = cy0; y<=cy2; ++y) {
  45243. const int yy0 = y - y0, yy1 = y - y1;
  45244. int
  45245. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  45246. xM = x0 + (dx02*yy0 + hdy02)/dy02,
  45247. txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
  45248. txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
  45249. tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
  45250. tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02,
  45251. lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
  45252. lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
  45253. lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
  45254. lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
  45255. if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM,lxm,lxM,lym,lyM);
  45256. if (xM>=0 && xm<=w1) {
  45257. const int
  45258. cxm = cimg::cut(xm,0,w1),
  45259. cxM = cimg::cut(xM,0,w1);
  45260. T *ptrd = data(cxm,y);
  45261. const int
  45262. dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
  45263. dtxmM = txM - txm, dtymM = tyM - tym,
  45264. dlxmM = lxM - lxm, dlymM = lyM - lym;
  45265. for (int x = cxm; x<=cxM; ++x) {
  45266. const int
  45267. xxm = x - xm,
  45268. tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
  45269. ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM,
  45270. lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
  45271. ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
  45272. const tc *const color = &texture._atXY(tx,ty);
  45273. const tl *const lig = &light._atXY(lx,ly);
  45274. cimg_forC(*this,c) {
  45275. const tc col = color[c*twhd];
  45276. const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
  45277. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  45278. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  45279. }
  45280. ++ptrd;
  45281. }
  45282. }
  45283. }
  45284. return *this;
  45285. }
  45286. //! Draw a textured Phong-shaded 2D triangle, with perspective correction.
  45287. template<typename tc, typename tl>
  45288. CImg<T>& draw_triangle(int x0, int y0, const float z0,
  45289. int x1, int y1, const float z1,
  45290. int x2, int y2, const float z2,
  45291. const CImg<tc>& texture,
  45292. int tx0, int ty0,
  45293. int tx1, int ty1,
  45294. int tx2, int ty2,
  45295. const CImg<tl>& light,
  45296. int lx0, int ly0,
  45297. int lx1, int ly1,
  45298. int lx2, int ly2,
  45299. const float opacity=1) {
  45300. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  45301. if (texture._depth>1 || texture._spectrum<_spectrum)
  45302. throw CImgArgumentException(_cimg_instance
  45303. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  45304. cimg_instance,
  45305. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  45306. if (light._depth>1 || light._spectrum<_spectrum)
  45307. throw CImgArgumentException(_cimg_instance
  45308. "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
  45309. cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
  45310. if (is_overlapped(texture))
  45311. return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
  45312. light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45313. if (is_overlapped(light))
  45314. return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,texture,tx0,ty0,tx1,ty1,tx2,ty2,
  45315. +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45316. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  45317. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
  45318. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
  45319. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
  45320. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  45321. const int
  45322. w1 = width() - 1, h1 = height() - 1,
  45323. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  45324. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  45325. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  45326. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  45327. const float
  45328. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  45329. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  45330. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  45331. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  45332. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
  45333. lxz0 = lx0*iz0, lxz1 = lx1*iz1, lxz2 = lx2*iz2,
  45334. lyz0 = ly0*iz0, lyz1 = ly1*iz1, lyz2 = ly2*iz2,
  45335. dlxz01 = lxz1 - lxz0, dlxz02 = lxz2 - lxz0, dlxz12 = lxz2 - lxz1,
  45336. dlyz01 = lyz1 - lyz0, dlyz02 = lyz2 - lyz0, dlyz12 = lyz2 - lyz1;
  45337. const ulongT
  45338. twhd = (ulongT)texture._width*texture._height*texture._depth,
  45339. lwhd = (ulongT)light._width*light._height*light._depth;
  45340. cimg_init_scanline(opacity);
  45341. for (int y = cy0; y<=cy2; ++y) {
  45342. const int yy0 = y - y0, yy1 = y - y1;
  45343. int
  45344. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  45345. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  45346. float
  45347. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  45348. izM = iz0 + diz02*yy0/dy02,
  45349. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  45350. txzM = txz0 + dtxz02*yy0/dy02,
  45351. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  45352. tyzM = tyz0 + dtyz02*yy0/dy02,
  45353. lxzm = y<y1?(lxz0 + dlxz01*yy0/dy01):(lxz1 + dlxz12*yy1/dy12),
  45354. lxzM = lxz0 + dlxz02*yy0/dy02,
  45355. lyzm = y<y1?(lyz0 + dlyz01*yy0/dy01):(lyz1 + dlyz12*yy1/dy12),
  45356. lyzM = lyz0 + dlyz02*yy0/dy02;
  45357. if (xm>xM) cimg::swap(xm,xM,izm,izM,txzm,txzM,tyzm,tyzM,lxzm,lxzM,lyzm,lyzM);
  45358. if (xM>=0 && xm<=w1) {
  45359. const int
  45360. cxm = cimg::cut(xm,0,w1),
  45361. cxM = cimg::cut(xM,0,w1);
  45362. T *ptrd = data(cxm,y);
  45363. const int dxmM = std::max(1,xM - xm);
  45364. const float
  45365. dizmM = izM - izm,
  45366. dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm,
  45367. dlxzmM = lxzM - lxzm, dlyzmM = lyzM - lyzm;
  45368. for (int x = cxm; x<=cxM; ++x) {
  45369. const int xxm = x - xm;
  45370. const float
  45371. iz = izm + dizmM*xxm/dxmM,
  45372. txz = txzm + dtxzmM*xxm/dxmM,
  45373. tyz = tyzm + dtyzmM*xxm/dxmM,
  45374. lxz = lxzm + dlxzmM*xxm/dxmM,
  45375. lyz = lyzm + dlyzmM*xxm/dxmM;
  45376. const int
  45377. tx = (int)cimg::round(txz/iz),
  45378. ty = (int)cimg::round(tyz/iz),
  45379. lx = (int)cimg::round(lxz/iz),
  45380. ly = (int)cimg::round(lyz/iz);
  45381. const tc *const color = &texture._atXY(tx,ty);
  45382. const tl *const lig = &light._atXY(lx,ly);
  45383. cimg_forC(*this,c) {
  45384. const tc col = color[c*twhd];
  45385. const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
  45386. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  45387. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  45388. }
  45389. ++ptrd;
  45390. }
  45391. }
  45392. }
  45393. return *this;
  45394. }
  45395. //! Draw a textured Phong-shaded 2D triangle, with perspective correction and z-buffering.
  45396. template<typename tz, typename tc, typename tl>
  45397. CImg<T>& draw_triangle(CImg<tz>& zbuffer,
  45398. int x0, int y0, const float z0,
  45399. int x1, int y1, const float z1,
  45400. int x2, int y2, const float z2,
  45401. const CImg<tc>& texture,
  45402. int tx0, int ty0,
  45403. int tx1, int ty1,
  45404. int tx2, int ty2,
  45405. const CImg<tl>& light,
  45406. int lx0, int ly0,
  45407. int lx1, int ly1,
  45408. int lx2, int ly2,
  45409. const float opacity=1) {
  45410. if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
  45411. if (!is_sameXY(zbuffer))
  45412. throw CImgArgumentException(_cimg_instance
  45413. "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
  45414. "different dimensions.",
  45415. cimg_instance,
  45416. zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
  45417. if (texture._depth>1 || texture._spectrum<_spectrum)
  45418. throw CImgArgumentException(_cimg_instance
  45419. "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
  45420. cimg_instance,
  45421. texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
  45422. if (light._depth>1 || light._spectrum<_spectrum)
  45423. throw CImgArgumentException(_cimg_instance
  45424. "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
  45425. cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
  45426. if (is_overlapped(texture))
  45427. return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,
  45428. +texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45429. if (is_overlapped(light))
  45430. return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,
  45431. texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  45432. float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
  45433. if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
  45434. if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
  45435. if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
  45436. if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
  45437. const int
  45438. w1 = width() - 1, h1 = height() - 1,
  45439. dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
  45440. dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
  45441. cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
  45442. hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
  45443. const float
  45444. diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
  45445. txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
  45446. tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
  45447. dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
  45448. dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
  45449. lxz0 = lx0*iz0, lxz1 = lx1*iz1, lxz2 = lx2*iz2,
  45450. lyz0 = ly0*iz0, lyz1 = ly1*iz1, lyz2 = ly2*iz2,
  45451. dlxz01 = lxz1 - lxz0, dlxz02 = lxz2 - lxz0, dlxz12 = lxz2 - lxz1,
  45452. dlyz01 = lyz1 - lyz0, dlyz02 = lyz2 - lyz0, dlyz12 = lyz2 - lyz1;
  45453. const ulongT
  45454. twhd = (ulongT)texture._width*texture._height*texture._depth,
  45455. lwhd = (ulongT)light._width*light._height*light._depth;
  45456. cimg_init_scanline(opacity);
  45457. for (int y = cy0; y<=cy2; ++y) {
  45458. const int yy0 = y - y0, yy1 = y - y1;
  45459. int
  45460. xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
  45461. xM = x0 + (dx02*yy0 + hdy02)/dy02;
  45462. float
  45463. izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
  45464. izM = iz0 + diz02*yy0/dy02,
  45465. txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
  45466. txzM = txz0 + dtxz02*yy0/dy02,
  45467. tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
  45468. tyzM = tyz0 + dtyz02*yy0/dy02,
  45469. lxzm = y<y1?(lxz0 + dlxz01*yy0/dy01):(lxz1 + dlxz12*yy1/dy12),
  45470. lxzM = lxz0 + dlxz02*yy0/dy02,
  45471. lyzm = y<y1?(lyz0 + dlyz01*yy0/dy01):(lyz1 + dlyz12*yy1/dy12),
  45472. lyzM = lyz0 + dlyz02*yy0/dy02;
  45473. if (xm>xM) cimg::swap(xm,xM,izm,izM,txzm,txzM,tyzm,tyzM,lxzm,lxzM,lyzm,lyzM);
  45474. if (xM>=0 && xm<=w1) {
  45475. const int
  45476. cxm = cimg::cut(xm,0,w1),
  45477. cxM = cimg::cut(xM,0,w1);
  45478. T *ptrd = data(cxm,y);
  45479. tz *ptrz = zbuffer.data(cxm,y);
  45480. const int dxmM = std::max(1,xM - xm);
  45481. const float
  45482. dizmM = izM - izm,
  45483. dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm,
  45484. dlxzmM = lxzM - lxzm, dlyzmM = lyzM - lyzm;
  45485. for (int x = cxm; x<=cxM; ++x) {
  45486. const int xxm = x - xm;
  45487. const float iz = izm + dizmM*xxm/dxmM;
  45488. if (iz>=*ptrz) {
  45489. *ptrz = (tz)iz;
  45490. const float
  45491. txz = txzm + dtxzmM*xxm/dxmM,
  45492. tyz = tyzm + dtyzmM*xxm/dxmM,
  45493. lxz = lxzm + dlxzmM*xxm/dxmM,
  45494. lyz = lyzm + dlyzmM*xxm/dxmM;
  45495. const int
  45496. tx = (int)cimg::round(txz/iz),
  45497. ty = (int)cimg::round(tyz/iz),
  45498. lx = (int)cimg::round(lxz/iz),
  45499. ly = (int)cimg::round(lyz/iz);
  45500. const tc *const color = &texture._atXY(tx,ty);
  45501. const tl *const lig = &light._atXY(lx,ly);
  45502. cimg_forC(*this,c) {
  45503. const tc col = color[c*twhd];
  45504. const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
  45505. const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
  45506. ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
  45507. }
  45508. }
  45509. ++ptrd; ++ptrz;
  45510. }
  45511. }
  45512. }
  45513. return *this;
  45514. }
  45515. //! Draw a filled 4D rectangle.
  45516. /**
  45517. \param x0 X-coordinate of the upper-left rectangle corner.
  45518. \param y0 Y-coordinate of the upper-left rectangle corner.
  45519. \param z0 Z-coordinate of the upper-left rectangle corner.
  45520. \param c0 C-coordinate of the upper-left rectangle corner.
  45521. \param x1 X-coordinate of the lower-right rectangle corner.
  45522. \param y1 Y-coordinate of the lower-right rectangle corner.
  45523. \param z1 Z-coordinate of the lower-right rectangle corner.
  45524. \param c1 C-coordinate of the lower-right rectangle corner.
  45525. \param val Scalar value used to fill the rectangle area.
  45526. \param opacity Drawing opacity.
  45527. **/
  45528. CImg<T>& draw_rectangle(const int x0, const int y0, const int z0, const int c0,
  45529. const int x1, const int y1, const int z1, const int c1,
  45530. const T val, const float opacity=1) {
  45531. if (is_empty()) return *this;
  45532. const int
  45533. nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
  45534. ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0,
  45535. nz0 = z0<z1?z0:z1, nz1 = z0^z1^nz0,
  45536. nc0 = c0<c1?c0:c1, nc1 = c0^c1^nc0;
  45537. const int
  45538. lx = (1 + nx1 - nx0) + (nx1>=width()?width() - 1 - nx1:0) + (nx0<0?nx0:0),
  45539. ly = (1 + ny1 - ny0) + (ny1>=height()?height() - 1 - ny1:0) + (ny0<0?ny0:0),
  45540. lz = (1 + nz1 - nz0) + (nz1>=depth()?depth() - 1 - nz1:0) + (nz0<0?nz0:0),
  45541. lc = (1 + nc1 - nc0) + (nc1>=spectrum()?spectrum() - 1 - nc1:0) + (nc0<0?nc0:0);
  45542. const ulongT
  45543. offX = (ulongT)_width - lx,
  45544. offY = (ulongT)_width*(_height - ly),
  45545. offZ = (ulongT)_width*_height*(_depth - lz);
  45546. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  45547. T *ptrd = data(nx0<0?0:nx0,ny0<0?0:ny0,nz0<0?0:nz0,nc0<0?0:nc0);
  45548. if (lx>0 && ly>0 && lz>0 && lc>0)
  45549. for (int v = 0; v<lc; ++v) {
  45550. for (int z = 0; z<lz; ++z) {
  45551. for (int y = 0; y<ly; ++y) {
  45552. if (opacity>=1) {
  45553. if (sizeof(T)!=1) { for (int x = 0; x<lx; ++x) *(ptrd++) = val; ptrd+=offX; }
  45554. else { std::memset(ptrd,(int)val,lx); ptrd+=_width; }
  45555. } else { for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*val + *ptrd*copacity); ++ptrd; } ptrd+=offX; }
  45556. }
  45557. ptrd+=offY;
  45558. }
  45559. ptrd+=offZ;
  45560. }
  45561. return *this;
  45562. }
  45563. //! Draw a filled 3D rectangle.
  45564. /**
  45565. \param x0 X-coordinate of the upper-left rectangle corner.
  45566. \param y0 Y-coordinate of the upper-left rectangle corner.
  45567. \param z0 Z-coordinate of the upper-left rectangle corner.
  45568. \param x1 X-coordinate of the lower-right rectangle corner.
  45569. \param y1 Y-coordinate of the lower-right rectangle corner.
  45570. \param z1 Z-coordinate of the lower-right rectangle corner.
  45571. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  45572. \param opacity Drawing opacity.
  45573. **/
  45574. template<typename tc>
  45575. CImg<T>& draw_rectangle(const int x0, const int y0, const int z0,
  45576. const int x1, const int y1, const int z1,
  45577. const tc *const color, const float opacity=1) {
  45578. if (is_empty()) return *this;
  45579. if (!color)
  45580. throw CImgArgumentException(_cimg_instance
  45581. "draw_rectangle(): Specified color is (null).",
  45582. cimg_instance);
  45583. cimg_forC(*this,c) draw_rectangle(x0,y0,z0,c,x1,y1,z1,c,(T)color[c],opacity);
  45584. return *this;
  45585. }
  45586. //! Draw a filled 2D rectangle.
  45587. /**
  45588. \param x0 X-coordinate of the upper-left rectangle corner.
  45589. \param y0 Y-coordinate of the upper-left rectangle corner.
  45590. \param x1 X-coordinate of the lower-right rectangle corner.
  45591. \param y1 Y-coordinate of the lower-right rectangle corner.
  45592. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  45593. \param opacity Drawing opacity.
  45594. **/
  45595. template<typename tc>
  45596. CImg<T>& draw_rectangle(const int x0, const int y0,
  45597. const int x1, const int y1,
  45598. const tc *const color, const float opacity=1) {
  45599. return draw_rectangle(x0,y0,0,x1,y1,_depth - 1,color,opacity);
  45600. }
  45601. //! Draw a outlined 2D rectangle \overloading.
  45602. template<typename tc>
  45603. CImg<T>& draw_rectangle(const int x0, const int y0,
  45604. const int x1, const int y1,
  45605. const tc *const color, const float opacity,
  45606. const unsigned int pattern) {
  45607. if (is_empty()) return *this;
  45608. if (y0==y1) return draw_line(x0,y0,x1,y0,color,opacity,pattern,true);
  45609. if (x0==x1) return draw_line(x0,y0,x0,y1,color,opacity,pattern,true);
  45610. const int
  45611. nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
  45612. ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0;
  45613. if (ny1==ny0 + 1) return draw_line(nx0,ny0,nx1,ny0,color,opacity,pattern,true).
  45614. draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false);
  45615. return draw_line(nx0,ny0,nx1,ny0,color,opacity,pattern,true).
  45616. draw_line(nx1,ny0 + 1,nx1,ny1 - 1,color,opacity,pattern,false).
  45617. draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false).
  45618. draw_line(nx0,ny1 - 1,nx0,ny0 + 1,color,opacity,pattern,false);
  45619. }
  45620. //! Draw a filled 2D polygon.
  45621. /**
  45622. \param points Set of polygon vertices.
  45623. \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
  45624. \param opacity Drawing opacity.
  45625. **/
  45626. template<typename tp, typename tc>
  45627. CImg<T>& draw_polygon(const CImg<tp>& points,
  45628. const tc *const color, const float opacity=1) {
  45629. if (is_empty() || !points) return *this;
  45630. if (!color)
  45631. throw CImgArgumentException(_cimg_instance
  45632. "draw_polygon(): Specified color is (null).",
  45633. cimg_instance);
  45634. if (points.height()!=2)
  45635. throw CImgArgumentException(_cimg_instance
  45636. "draw_polygon(): Invalid specified point set (%u,%u,%u,%u).",
  45637. cimg_instance,
  45638. points._width,points._height,points._depth,points._spectrum);
  45639. if (points._width==1) return draw_point(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),color,opacity);
  45640. if (points._width==2) return draw_line(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),
  45641. cimg::uiround(points(1,0)),cimg::uiround(points(1,1)),color,opacity);
  45642. if (points._width==3) return draw_triangle(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),
  45643. cimg::uiround(points(1,0)),cimg::uiround(points(1,1)),
  45644. cimg::uiround(points(2,0)),cimg::uiround(points(2,1)),color,opacity);
  45645. cimg_init_scanline(opacity);
  45646. int
  45647. xmin = 0, ymin = 0,
  45648. xmax = points.get_shared_row(0).max_min(xmin),
  45649. ymax = points.get_shared_row(1).max_min(ymin);
  45650. if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this;
  45651. if (ymin==ymax) return draw_line(xmin,ymin,xmax,ymax,color,opacity);
  45652. ymin = std::max(0,ymin);
  45653. ymax = std::min(height() - 1,ymax);
  45654. CImg<intT> Xs(points._width,ymax - ymin + 1);
  45655. CImg<uintT> count(Xs._height,1,1,1,0);
  45656. unsigned int n = 0, nn = 1;
  45657. bool go_on = true;
  45658. while (go_on) {
  45659. unsigned int an = (nn + 1)%points._width;
  45660. const int
  45661. x0 = cimg::uiround(points(n,0)),
  45662. y0 = cimg::uiround(points(n,1));
  45663. if (points(nn,1)==y0) while (points(an,1)==y0) { nn = an; (an+=1)%=points._width; }
  45664. const int
  45665. x1 = cimg::uiround(points(nn,0)),
  45666. y1 = cimg::uiround(points(nn,1));
  45667. unsigned int tn = an;
  45668. while (points(tn,1)==y1) (tn+=1)%=points._width;
  45669. if (y0!=y1) {
  45670. const int
  45671. y2 = cimg::uiround(points(tn,1)),
  45672. x01 = x1 - x0, y01 = y1 - y0, y12 = y2 - y1,
  45673. step = cimg::sign(y01),
  45674. tmax = std::max(1,cimg::abs(y01)), htmax = tmax*cimg::sign(x01)/2,
  45675. tend = tmax - (step==cimg::sign(y12));
  45676. unsigned int y = (unsigned int)y0 - ymin;
  45677. for (int t = 0; t<=tend; ++t, y+=step)
  45678. if (y<Xs._height) Xs(count[y]++,y) = x0 + (t*x01 + htmax)/tmax;
  45679. }
  45680. go_on = nn>n;
  45681. n = nn;
  45682. nn = an;
  45683. }
  45684. cimg_pragma_openmp(parallel for cimg_openmp_if(Xs._height>=(cimg_openmp_sizefactor)*512))
  45685. cimg_forY(Xs,y) {
  45686. const CImg<intT> Xsy = Xs.get_shared_points(0,count[y] - 1,y).sort();
  45687. int px = width();
  45688. for (unsigned int k = 0; k<Xsy._width; k+=2) {
  45689. int x0 = Xsy[k];
  45690. const int x1 = Xsy[k + 1];
  45691. x0+=x0==px;
  45692. cimg_draw_scanline(x0,x1,y + ymin,color,opacity,1);
  45693. px = x1;
  45694. }
  45695. }
  45696. return *this;
  45697. }
  45698. //! Draw a outlined 2D or 3D polygon \overloading.
  45699. template<typename t, typename tc>
  45700. CImg<T>& draw_polygon(const CImg<t>& points,
  45701. const tc *const color, const float opacity, const unsigned int pattern) {
  45702. if (is_empty() || !points) return *this;
  45703. if (!color)
  45704. throw CImgArgumentException(_cimg_instance
  45705. "draw_polygon(): Specified color is (null).",
  45706. cimg_instance);
  45707. if (points._width==1) return draw_point((int)points(0,0),(int)points(0,1),color,opacity);
  45708. if (points._width==2) return draw_line((int)points(0,0),(int)points(0,1),
  45709. (int)points(1,0),(int)points(1,1),color,opacity,pattern);
  45710. bool ninit_hatch = true;
  45711. switch (points._height) {
  45712. case 0 : case 1 :
  45713. throw CImgArgumentException(_cimg_instance
  45714. "draw_polygon(): Invalid specified point set (%u,%u,%u,%u).",
  45715. cimg_instance,
  45716. points._width,points._height,points._depth,points._spectrum);
  45717. default : {
  45718. CImg<intT> npoints(points._width,2);
  45719. int x = npoints(0,0) = (int)points(0,0), y = npoints(0,1) = (int)points(0,1);
  45720. unsigned int nb_points = 1;
  45721. for (unsigned int p = 1; p<points._width; ++p) {
  45722. const int nx = (int)points(p,0), ny = (int)points(p,1);
  45723. if (nx!=x || ny!=y) { npoints(nb_points,0) = nx; npoints(nb_points++,1) = ny; x = nx; y = ny; }
  45724. }
  45725. const int x0 = (int)npoints(0,0), y0 = (int)npoints(0,1);
  45726. int ox = x0, oy = y0;
  45727. for (unsigned int i = 1; i<nb_points; ++i) {
  45728. const int _x = (int)npoints(i,0), _y = (int)npoints(i,1);
  45729. draw_line(ox,oy,_x,_y,color,opacity,pattern,ninit_hatch);
  45730. ninit_hatch = false;
  45731. ox = _x; oy = _y;
  45732. }
  45733. draw_line(ox,oy,x0,y0,color,opacity,pattern,false);
  45734. }
  45735. }
  45736. return *this;
  45737. }
  45738. //! Draw a filled 2D ellipse.
  45739. /**
  45740. \param x0 X-coordinate of the ellipse center.
  45741. \param y0 Y-coordinate of the ellipse center.
  45742. \param r1 First radius of the ellipse.
  45743. \param r2 Second radius of the ellipse.
  45744. \param angle Angle of the first radius.
  45745. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45746. \param opacity Drawing opacity.
  45747. **/
  45748. template<typename tc>
  45749. CImg<T>& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle,
  45750. const tc *const color, const float opacity=1) {
  45751. return _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,0U,true);
  45752. }
  45753. //! Draw a filled 2D ellipse \overloading.
  45754. /**
  45755. \param x0 X-coordinate of the ellipse center.
  45756. \param y0 Y-coordinate of the ellipse center.
  45757. \param tensor Diffusion tensor describing the ellipse.
  45758. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45759. \param opacity Drawing opacity.
  45760. **/
  45761. template<typename t, typename tc>
  45762. CImg<T>& draw_ellipse(const int x0, const int y0, const CImg<t> &tensor,
  45763. const tc *const color, const float opacity=1) {
  45764. CImgList<t> eig = tensor.get_symmetric_eigen();
  45765. const CImg<t> &val = eig[0], &vec = eig[1];
  45766. return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)),
  45767. std::atan2(vec(0,1),vec(0,0))*180/cimg::PI,
  45768. color,opacity);
  45769. }
  45770. //! Draw an outlined 2D ellipse.
  45771. /**
  45772. \param x0 X-coordinate of the ellipse center.
  45773. \param y0 Y-coordinate of the ellipse center.
  45774. \param r1 First radius of the ellipse.
  45775. \param r2 Second radius of the ellipse.
  45776. \param angle Angle of the first radius.
  45777. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45778. \param opacity Drawing opacity.
  45779. \param pattern An integer whose bits describe the outline pattern.
  45780. **/
  45781. template<typename tc>
  45782. CImg<T>& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle,
  45783. const tc *const color, const float opacity, const unsigned int pattern) {
  45784. if (pattern) _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,pattern,false);
  45785. return *this;
  45786. }
  45787. //! Draw an outlined 2D ellipse \overloading.
  45788. /**
  45789. \param x0 X-coordinate of the ellipse center.
  45790. \param y0 Y-coordinate of the ellipse center.
  45791. \param tensor Diffusion tensor describing the ellipse.
  45792. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45793. \param opacity Drawing opacity.
  45794. \param pattern An integer whose bits describe the outline pattern.
  45795. **/
  45796. template<typename t, typename tc>
  45797. CImg<T>& draw_ellipse(const int x0, const int y0, const CImg<t> &tensor,
  45798. const tc *const color, const float opacity,
  45799. const unsigned int pattern) {
  45800. CImgList<t> eig = tensor.get_symmetric_eigen();
  45801. const CImg<t> &val = eig[0], &vec = eig[1];
  45802. return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)),
  45803. std::atan2(vec(0,1),vec(0,0))*180/cimg::PI,
  45804. color,opacity,pattern);
  45805. }
  45806. template<typename tc>
  45807. CImg<T>& _draw_ellipse(const int x0, const int y0, const float radius1, const float radius2, const float angle,
  45808. const tc *const color, const float opacity,
  45809. const unsigned int pattern, const bool is_filled) {
  45810. if (is_empty() || (!is_filled && !pattern)) return *this;
  45811. const float radiusM = std::max(radius1,radius2);
  45812. if (radius1<0 || radius2<0 || x0 - radiusM>=width() || y0 + radiusM<0 || y0 - radiusM>=height()) return *this;
  45813. if (!color)
  45814. throw CImgArgumentException(_cimg_instance
  45815. "draw_ellipse(): Specified color is (null).",
  45816. cimg_instance);
  45817. const int iradius1 = (int)cimg::round(radius1), iradius2 = (int)cimg::round(radius2);
  45818. if (!iradius1 && !iradius2) return draw_point(x0,y0,color,opacity);
  45819. if (iradius1==iradius2) {
  45820. if (is_filled) return draw_circle(x0,y0,iradius1,color,opacity);
  45821. else if (pattern==~0U) return draw_circle(x0,y0,iradius1,color,opacity,pattern);
  45822. }
  45823. const float ang = (float)(angle*cimg::PI/180);
  45824. if (!is_filled) { // Outlined
  45825. const float ca = std::cos(ang), sa = std::sin(ang);
  45826. CImg<int> points((unsigned int)cimg::round(6*radiusM),2);
  45827. cimg_forX(points,k) {
  45828. const float
  45829. _ang = (float)(2*cimg::PI*k/points._width),
  45830. X = (float)(radius1*std::cos(_ang)),
  45831. Y = (float)(radius2*std::sin(_ang));
  45832. points(k,0) = (int)cimg::round(x0 + (X*ca - Y*sa));
  45833. points(k,1) = (int)cimg::round(y0 + (X*sa + Y*ca));
  45834. }
  45835. draw_polygon(points,color,opacity,pattern);
  45836. } else { // Filled
  45837. cimg_init_scanline(opacity);
  45838. const float
  45839. ca = std::cos(ang),
  45840. sa = -std::sin(ang),
  45841. ca2 = ca*ca,
  45842. sa2 = sa*sa,
  45843. casa = ca*sa,
  45844. i1 = 1/cimg::sqr(radius1),
  45845. i2 = 1/cimg::sqr(radius2),
  45846. t1 = i1*ca2 + i2*sa2,
  45847. t2 = (i2 - i1)*casa,
  45848. t3 = i2*ca2 + i1*sa2,
  45849. t12 = t1*2;
  45850. const int
  45851. _ymin = (int)std::floor(y0 - radiusM),
  45852. _ymax = (int)std::ceil(y0 + radiusM),
  45853. ymin = _ymin<0?0:_ymin,
  45854. ymax = _ymax>=height()?height() - 1:_ymax;
  45855. for (int y = ymin; y<=ymax; ++y) {
  45856. const float
  45857. Y = y - y0 + 0.5f,
  45858. B = 2*t2*Y,
  45859. C = t3*Y*Y - 1,
  45860. D = B*B - 4*t1*C;
  45861. if (D>=0) {
  45862. const float sD = std::sqrt(D);
  45863. const int
  45864. xmin = (int)(x0 + cimg::round((-B - sD)/t12)),
  45865. xmax = (int)(x0 + cimg::round((-B + sD)/t12));
  45866. cimg_draw_scanline(xmin,xmax,y,color,opacity,1);
  45867. }
  45868. }
  45869. }
  45870. return *this;
  45871. }
  45872. //! Draw a filled 2D circle.
  45873. /**
  45874. \param x0 X-coordinate of the circle center.
  45875. \param y0 Y-coordinate of the circle center.
  45876. \param radius Circle radius.
  45877. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45878. \param opacity Drawing opacity.
  45879. \note
  45880. - Circle version of the Bresenham's algorithm is used.
  45881. **/
  45882. template<typename tc>
  45883. CImg<T>& draw_circle(const int x0, const int y0, int radius,
  45884. const tc *const color, const float opacity=1) {
  45885. if (is_empty()) return *this;
  45886. if (radius<0 || x0 - radius>=width() || y0 + radius<0 || y0 - radius>=height()) return *this;
  45887. if (!color)
  45888. throw CImgArgumentException(_cimg_instance
  45889. "draw_circle(): Specified color is (null).",
  45890. cimg_instance);
  45891. if (!radius) return draw_point(x0,y0,color,opacity);
  45892. cimg_init_scanline(opacity);
  45893. if (y0>=0 && y0<height()) cimg_draw_scanline(x0 - radius,x0 + radius,y0,color,opacity,1);
  45894. for (int f = 1 - radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x<y; ) {
  45895. if (f>=0) {
  45896. const int x1 = x0 - x, x2 = x0 + x, y1 = y0 - y, y2 = y0 + y;
  45897. if (y1>=0 && y1<height()) cimg_draw_scanline(x1,x2,y1,color,opacity,1);
  45898. if (y2>=0 && y2<height()) cimg_draw_scanline(x1,x2,y2,color,opacity,1);
  45899. f+=(ddFy+=2); --y;
  45900. }
  45901. const bool no_diag = y!=(x++);
  45902. ++(f+=(ddFx+=2));
  45903. const int x1 = x0 - y, x2 = x0 + y, y1 = y0 - x, y2 = y0 + x;
  45904. if (no_diag) {
  45905. if (y1>=0 && y1<height()) cimg_draw_scanline(x1,x2,y1,color,opacity,1);
  45906. if (y2>=0 && y2<height()) cimg_draw_scanline(x1,x2,y2,color,opacity,1);
  45907. }
  45908. }
  45909. return *this;
  45910. }
  45911. //! Draw an outlined 2D circle.
  45912. /**
  45913. \param x0 X-coordinate of the circle center.
  45914. \param y0 Y-coordinate of the circle center.
  45915. \param radius Circle radius.
  45916. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  45917. \param opacity Drawing opacity.
  45918. \param pattern An integer whose bits describe the outline pattern.
  45919. **/
  45920. template<typename tc>
  45921. CImg<T>& draw_circle(const int x0, const int y0, int radius,
  45922. const tc *const color, const float opacity,
  45923. const unsigned int pattern) {
  45924. if (pattern!=~0U) return draw_ellipse(x0,y0,radius,radius,0,color,opacity,pattern);
  45925. if (is_empty()) return *this;
  45926. if (!color)
  45927. throw CImgArgumentException(_cimg_instance
  45928. "draw_circle(): Specified color is (null).",
  45929. cimg_instance);
  45930. if (radius<0 || x0 - radius>=width() || y0 + radius<0 || y0 - radius>=height()) return *this;
  45931. if (!radius) return draw_point(x0,y0,color,opacity);
  45932. draw_point(x0 - radius,y0,color,opacity).draw_point(x0 + radius,y0,color,opacity).
  45933. draw_point(x0,y0 - radius,color,opacity).draw_point(x0,y0 + radius,color,opacity);
  45934. if (radius==1) return *this;
  45935. for (int f = 1 - radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x<y; ) {
  45936. if (f>=0) { f+=(ddFy+=2); --y; }
  45937. ++x; ++(f+=(ddFx+=2));
  45938. if (x!=y + 1) {
  45939. const int x1 = x0 - y, x2 = x0 + y, y1 = y0 - x, y2 = y0 + x,
  45940. x3 = x0 - x, x4 = x0 + x, y3 = y0 - y, y4 = y0 + y;
  45941. draw_point(x1,y1,color,opacity).draw_point(x1,y2,color,opacity).
  45942. draw_point(x2,y1,color,opacity).draw_point(x2,y2,color,opacity);
  45943. if (x!=y)
  45944. draw_point(x3,y3,color,opacity).draw_point(x4,y4,color,opacity).
  45945. draw_point(x4,y3,color,opacity).draw_point(x3,y4,color,opacity);
  45946. }
  45947. }
  45948. return *this;
  45949. }
  45950. //! Draw an image.
  45951. /**
  45952. \param sprite Sprite image.
  45953. \param x0 X-coordinate of the sprite position.
  45954. \param y0 Y-coordinate of the sprite position.
  45955. \param z0 Z-coordinate of the sprite position.
  45956. \param c0 C-coordinate of the sprite position.
  45957. \param opacity Drawing opacity.
  45958. **/
  45959. template<typename t>
  45960. CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
  45961. const CImg<t>& sprite, const float opacity=1) {
  45962. if (is_empty() || !sprite) return *this;
  45963. if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity);
  45964. if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared())
  45965. return assign(sprite,false);
  45966. const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
  45967. const int
  45968. dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
  45969. sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
  45970. lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
  45971. ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
  45972. lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
  45973. lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
  45974. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  45975. if (lx>0 && ly>0 && lz>0 && lc>0) {
  45976. for (int c = 0; c<lc; ++c)
  45977. for (int z = 0; z<lz; ++z)
  45978. for (int y = 0; y<ly; ++y) {
  45979. T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
  45980. const t *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
  45981. if (opacity>=1) for (int x = 0; x<lx; ++x) *(ptrd++) = (T)*(ptrs++);
  45982. else for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*(*(ptrs++)) + *ptrd*copacity); ++ptrd; }
  45983. }
  45984. }
  45985. return *this;
  45986. }
  45987. //! Draw an image \specialization.
  45988. CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
  45989. const CImg<T>& sprite, const float opacity=1) {
  45990. if (is_empty() || !sprite) return *this;
  45991. if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity);
  45992. if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared())
  45993. return assign(sprite,false);
  45994. const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
  45995. const int
  45996. dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
  45997. sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
  45998. lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
  45999. ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
  46000. lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
  46001. lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
  46002. const ulongT slx = lx*sizeof(T);
  46003. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  46004. if (lx>0 && ly>0 && lz>0 && lc>0) {
  46005. for (int c = 0; c<lc; ++c)
  46006. for (int z = 0; z<lz; ++z)
  46007. for (int y = 0; y<ly; ++y) {
  46008. T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
  46009. const T *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
  46010. if (opacity>=1) std::memcpy(ptrd,ptrs,slx);
  46011. else for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*(*(ptrs++)) + *ptrd*copacity); ++ptrd; }
  46012. }
  46013. }
  46014. return *this;
  46015. }
  46016. //! Draw an image \overloading.
  46017. template<typename t>
  46018. CImg<T>& draw_image(const int x0, const int y0, const int z0,
  46019. const CImg<t>& sprite, const float opacity=1) {
  46020. return draw_image(x0,y0,z0,0,sprite,opacity);
  46021. }
  46022. //! Draw an image \overloading.
  46023. template<typename t>
  46024. CImg<T>& draw_image(const int x0, const int y0,
  46025. const CImg<t>& sprite, const float opacity=1) {
  46026. return draw_image(x0,y0,0,sprite,opacity);
  46027. }
  46028. //! Draw an image \overloading.
  46029. template<typename t>
  46030. CImg<T>& draw_image(const int x0,
  46031. const CImg<t>& sprite, const float opacity=1) {
  46032. return draw_image(x0,0,sprite,opacity);
  46033. }
  46034. //! Draw an image \overloading.
  46035. template<typename t>
  46036. CImg<T>& draw_image(const CImg<t>& sprite, const float opacity=1) {
  46037. return draw_image(0,sprite,opacity);
  46038. }
  46039. //! Draw a masked image.
  46040. /**
  46041. \param sprite Sprite image.
  46042. \param mask Mask image.
  46043. \param x0 X-coordinate of the sprite position in the image instance.
  46044. \param y0 Y-coordinate of the sprite position in the image instance.
  46045. \param z0 Z-coordinate of the sprite position in the image instance.
  46046. \param c0 C-coordinate of the sprite position in the image instance.
  46047. \param mask_max_value Maximum pixel value of the mask image \c mask.
  46048. \param opacity Drawing opacity.
  46049. \note
  46050. - Pixel values of \c mask set the opacity of the corresponding pixels in \c sprite.
  46051. - Dimensions along x,y and z of \p sprite and \p mask must be the same.
  46052. **/
  46053. template<typename ti, typename tm>
  46054. CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
  46055. const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
  46056. const float mask_max_value=1) {
  46057. if (is_empty() || !sprite || !mask) return *this;
  46058. if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,mask,opacity,mask_max_value);
  46059. if (is_overlapped(mask)) return draw_image(x0,y0,z0,c0,sprite,+mask,opacity,mask_max_value);
  46060. if (mask._width!=sprite._width || mask._height!=sprite._height || mask._depth!=sprite._depth)
  46061. throw CImgArgumentException(_cimg_instance
  46062. "draw_image(): Sprite (%u,%u,%u,%u,%p) and mask (%u,%u,%u,%u,%p) have "
  46063. "incompatible dimensions.",
  46064. cimg_instance,
  46065. sprite._width,sprite._height,sprite._depth,sprite._spectrum,sprite._data,
  46066. mask._width,mask._height,mask._depth,mask._spectrum,mask._data);
  46067. const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
  46068. const int
  46069. dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
  46070. sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
  46071. lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
  46072. ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
  46073. lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
  46074. lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
  46075. const ulongT msize = mask.size();
  46076. if (lx>0 && ly>0 && lz>0 && lc>0) {
  46077. for (int c = 0; c<lc; ++c)
  46078. for (int z = 0; z<lz; ++z)
  46079. for (int y = 0; y<ly; ++y) {
  46080. T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
  46081. const ti *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
  46082. const tm *ptrm = mask._data + (mask.offset(sx0,sy0 + y,sz0 + z,sc0 + c)%msize);
  46083. for (int x = 0; x<lx; ++x) {
  46084. const float mopacity = (float)(*(ptrm++)*opacity),
  46085. nopacity = cimg::abs(mopacity), copacity = mask_max_value - std::max(mopacity,0.f);
  46086. *ptrd = (T)((nopacity*(*(ptrs++)) + *ptrd*copacity)/mask_max_value);
  46087. ++ptrd;
  46088. }
  46089. }
  46090. }
  46091. return *this;
  46092. }
  46093. //! Draw a masked image \overloading.
  46094. template<typename ti, typename tm>
  46095. CImg<T>& draw_image(const int x0, const int y0, const int z0,
  46096. const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
  46097. const float mask_max_value=1) {
  46098. return draw_image(x0,y0,z0,0,sprite,mask,opacity,mask_max_value);
  46099. }
  46100. //! Draw a image \overloading.
  46101. template<typename ti, typename tm>
  46102. CImg<T>& draw_image(const int x0, const int y0,
  46103. const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
  46104. const float mask_max_value=1) {
  46105. return draw_image(x0,y0,0,sprite,mask,opacity,mask_max_value);
  46106. }
  46107. //! Draw a image \overloading.
  46108. template<typename ti, typename tm>
  46109. CImg<T>& draw_image(const int x0,
  46110. const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
  46111. const float mask_max_value=1) {
  46112. return draw_image(x0,0,sprite,mask,opacity,mask_max_value);
  46113. }
  46114. //! Draw an image.
  46115. template<typename ti, typename tm>
  46116. CImg<T>& draw_image(const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
  46117. const float mask_max_value=1) {
  46118. return draw_image(0,sprite,mask,opacity,mask_max_value);
  46119. }
  46120. //! Draw a text string.
  46121. /**
  46122. \param x0 X-coordinate of the text in the image instance.
  46123. \param y0 Y-coordinate of the text in the image instance.
  46124. \param text Format of the text ('printf'-style format string).
  46125. \param foreground_color Pointer to \c spectrum() consecutive values, defining the foreground drawing color.
  46126. \param background_color Pointer to \c spectrum() consecutive values, defining the background drawing color.
  46127. \param opacity Drawing opacity.
  46128. \param font Font used for drawing text.
  46129. **/
  46130. template<typename tc1, typename tc2, typename t>
  46131. CImg<T>& draw_text(const int x0, const int y0,
  46132. const char *const text,
  46133. const tc1 *const foreground_color, const tc2 *const background_color,
  46134. const float opacity, const CImgList<t>& font, ...) {
  46135. if (!font) return *this;
  46136. CImg<charT> tmp(2048);
  46137. std::va_list ap; va_start(ap,font);
  46138. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46139. return _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,false);
  46140. }
  46141. //! Draw a text string \overloading.
  46142. /**
  46143. \note A transparent background is used for the text.
  46144. **/
  46145. template<typename tc, typename t>
  46146. CImg<T>& draw_text(const int x0, const int y0,
  46147. const char *const text,
  46148. const tc *const foreground_color, const int,
  46149. const float opacity, const CImgList<t>& font, ...) {
  46150. if (!font) return *this;
  46151. CImg<charT> tmp(2048);
  46152. std::va_list ap; va_start(ap,font);
  46153. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46154. return _draw_text(x0,y0,tmp,foreground_color,(tc*)0,opacity,font,false);
  46155. }
  46156. //! Draw a text string \overloading.
  46157. /**
  46158. \note A transparent foreground is used for the text.
  46159. **/
  46160. template<typename tc, typename t>
  46161. CImg<T>& draw_text(const int x0, const int y0,
  46162. const char *const text,
  46163. const int, const tc *const background_color,
  46164. const float opacity, const CImgList<t>& font, ...) {
  46165. if (!font) return *this;
  46166. CImg<charT> tmp(2048);
  46167. std::va_list ap; va_start(ap,font);
  46168. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46169. return _draw_text(x0,y0,tmp,(tc*)0,background_color,opacity,font,false);
  46170. }
  46171. //! Draw a text string \overloading.
  46172. /**
  46173. \param x0 X-coordinate of the text in the image instance.
  46174. \param y0 Y-coordinate of the text in the image instance.
  46175. \param text Format of the text ('printf'-style format string).
  46176. \param foreground_color Array of spectrum() values of type \c T,
  46177. defining the foreground color (0 means 'transparent').
  46178. \param background_color Array of spectrum() values of type \c T,
  46179. defining the background color (0 means 'transparent').
  46180. \param opacity Drawing opacity.
  46181. \param font_height Height of the text font (exact match for 13,23,53,103, interpolated otherwise).
  46182. **/
  46183. template<typename tc1, typename tc2>
  46184. CImg<T>& draw_text(const int x0, const int y0,
  46185. const char *const text,
  46186. const tc1 *const foreground_color, const tc2 *const background_color,
  46187. const float opacity=1, const unsigned int font_height=13, ...) {
  46188. if (!font_height) return *this;
  46189. CImg<charT> tmp(2048);
  46190. std::va_list ap; va_start(ap,font_height);
  46191. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46192. const CImgList<ucharT>& font = CImgList<ucharT>::font(font_height,true);
  46193. _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,true);
  46194. return *this;
  46195. }
  46196. //! Draw a text string \overloading.
  46197. template<typename tc>
  46198. CImg<T>& draw_text(const int x0, const int y0,
  46199. const char *const text,
  46200. const tc *const foreground_color, const int background_color=0,
  46201. const float opacity=1, const unsigned int font_height=13, ...) {
  46202. if (!font_height) return *this;
  46203. cimg::unused(background_color);
  46204. CImg<charT> tmp(2048);
  46205. std::va_list ap; va_start(ap,font_height);
  46206. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46207. return draw_text(x0,y0,"%s",foreground_color,(const tc*)0,opacity,font_height,tmp._data);
  46208. }
  46209. //! Draw a text string \overloading.
  46210. template<typename tc>
  46211. CImg<T>& draw_text(const int x0, const int y0,
  46212. const char *const text,
  46213. const int, const tc *const background_color,
  46214. const float opacity=1, const unsigned int font_height=13, ...) {
  46215. if (!font_height) return *this;
  46216. CImg<charT> tmp(2048);
  46217. std::va_list ap; va_start(ap,font_height);
  46218. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46219. return draw_text(x0,y0,"%s",(tc*)0,background_color,opacity,font_height,tmp._data);
  46220. }
  46221. template<typename tc1, typename tc2, typename t>
  46222. CImg<T>& _draw_text(const int x0, const int y0,
  46223. const char *const text,
  46224. const tc1 *const foreground_color, const tc2 *const background_color,
  46225. const float opacity, const CImgList<t>& font,
  46226. const bool is_native_font) {
  46227. if (!text) return *this;
  46228. if (!font)
  46229. throw CImgArgumentException(_cimg_instance
  46230. "draw_text(): Empty specified font.",
  46231. cimg_instance);
  46232. const unsigned int text_length = (unsigned int)std::strlen(text);
  46233. const int padding_x = font[0]._height<48?1:font[0]._height<128?(int)std::ceil(font[0]._height/51.0f + 0.745f):4;
  46234. unsigned char o_ch, ch = 0;
  46235. int x, y, w;
  46236. CImg<intT> left_paddings(text_length,1,1,1,0);
  46237. const CImg<t> empty = CImg<t>::empty();
  46238. if (is_empty() || is_native_font) {
  46239. // Pre-compute necessary size of the image as well as left paddings of each character.
  46240. x = y = w = 0;
  46241. o_ch = 0;
  46242. for (unsigned int i = 0; i<text_length; ++i) {
  46243. ch = (unsigned char)text[i];
  46244. switch (ch) {
  46245. case '\n' : y+=font[0]._height; if (x>w) w = x; x = 0; break;
  46246. case '\t' : x+=4*font[(int)' ']._width; break;
  46247. case ' ' : x+=font[(int)' ']._width; break;
  46248. default : if (ch<font._width) {
  46249. int left_padding = 0;
  46250. if (is_native_font && font[0]._height<128) {
  46251. // Determine left padding from various rules.
  46252. if (ch==':' || ch=='!' || ch=='.' || ch==';')
  46253. left_padding = 2*padding_x;
  46254. else if (o_ch==',' || (o_ch=='.' && (ch<'0' || ch>'9')) || o_ch==';' || o_ch==':' || o_ch=='!')
  46255. left_padding = 4*padding_x;
  46256. else if (((o_ch=='i' || o_ch=='l' || o_ch=='I' || o_ch=='J' || o_ch=='M' || o_ch=='N') &&
  46257. ((ch>='0' && ch<='9') ||
  46258. (ch>='a' && ch<='z' && ch!='v' && ch!='x' && ch!='y') ||
  46259. (ch>='B' && ch<='Z' && ch!='J' && ch!='T' && ch!='V' && ch!='X' && ch!='Y'))) ||
  46260. o_ch=='.' || o_ch=='\'' || ch=='\'')
  46261. left_padding = padding_x;
  46262. else if ((o_ch<'0' || o_ch>'9') && ch!='-') {
  46263. const CImg<t> &mask = ch + 256U<font._width?font[ch + 256]:empty;
  46264. if (o_ch && ch>' ' && o_ch>' ' && mask._height>13) {
  46265. const CImg<t> &o_mask = o_ch + 256U<font._width?font[o_ch + 256]:empty;
  46266. if (o_mask._height>13) {
  46267. const int w1 = mask.width()>0?o_mask.width() - 1:0, w2 = w1>1?w1 - 1:0, w3 = w2>1?w2 - 1:0;
  46268. left_padding = -10;
  46269. cimg_forY(mask,k) {
  46270. const int
  46271. lpad = o_mask(w1,k)>=8?0:
  46272. o_mask._width<=2 || o_mask(w2,k)>=8?-1:
  46273. o_mask._width<=3 || o_mask(w3,k)>=8?-2:-3,
  46274. rpad = mask(0,k)>=8?0:
  46275. mask._width<=2 || mask(1,k)>=8?-1:
  46276. mask._width<=3 || mask(2,k)>=8?-2:-3;
  46277. left_padding = std::max(left_padding,lpad + rpad);
  46278. }
  46279. }
  46280. }
  46281. }
  46282. left_paddings[i] = left_padding;
  46283. }
  46284. x+=left_padding + font[ch]._width + padding_x;
  46285. o_ch = ch;
  46286. }
  46287. }
  46288. }
  46289. if (x!=0 || ch=='\n') { if (x>w) w = x; y+=font[0]._height; }
  46290. if (is_empty()) assign(x0 + w,y0 + y,1,is_native_font?1:font[0]._spectrum,(T)0);
  46291. }
  46292. // Draw font characters on image.
  46293. x = x0; y = y0;
  46294. for (unsigned int i = 0; i<text_length; ++i) {
  46295. ch = (unsigned char)text[i];
  46296. switch (ch) {
  46297. case '\n' : y+=font[0]._height; x = x0; break;
  46298. case '\t' :
  46299. case ' ' : {
  46300. const unsigned int lw = (ch=='\t'?4:1)*font[(int)' ']._width, lh = font[(int)' ']._height;
  46301. if (background_color) draw_rectangle(x,y,x + lw - 1,y + lh - 1,background_color,opacity);
  46302. x+=lw;
  46303. } break;
  46304. default : if (ch<font._width) {
  46305. CImg<T> letter = font[ch];
  46306. if (letter) {
  46307. const CImg<t> &mask = ch + 256U<font._width?font[ch + 256]:empty;
  46308. const int posx = x + left_paddings[i] + padding_x;
  46309. if (is_native_font && _spectrum>letter._spectrum)
  46310. letter.assign(letter.get_resize(-100,-100,1,_spectrum,0,2),false);
  46311. const unsigned int cmin = std::min(_spectrum,letter._spectrum);
  46312. if (foreground_color)
  46313. for (unsigned int c = 0; c<cmin; ++c)
  46314. if (foreground_color[c]!=1) letter.get_shared_channel(c)*=foreground_color[c];
  46315. if (mask) { // Letter has mask
  46316. if (background_color)
  46317. for (unsigned int c = 0; c<cmin; ++c)
  46318. draw_rectangle(x,y,0,c,posx + letter._width - 1,y + letter._height - 1,0,c,
  46319. background_color[c],opacity);
  46320. draw_image(posx,y,letter,font[ch + 256],opacity,255.f);
  46321. } else draw_image(posx,y,letter,opacity); // Letter has no mask
  46322. x = posx + letter._width;
  46323. }
  46324. }
  46325. }
  46326. }
  46327. return *this;
  46328. }
  46329. // [internal] Version used to display text in interactive viewers.
  46330. CImg<T>& __draw_text(const char *const text, unsigned int &font_size, const int is_down, ...) {
  46331. CImg<charT> tmp(2048);
  46332. std::va_list ap;
  46333. va_start(ap,is_down);
  46334. cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
  46335. CImg<ucharT> a_label, a_labelmask;
  46336. const unsigned char a_labelcolor = 255;
  46337. unsigned int ofs = font_size, fs = ofs;
  46338. do { // Determine best font size
  46339. a_label.assign().draw_text(0,0,"%s",&a_labelcolor,0,1,fs,tmp._data);
  46340. if (a_label._width<7*_width/10 && a_label._height>_height/20 && a_label._height<_height/5) {
  46341. font_size = fs; break;
  46342. } else if ((a_label._width>7*_width/10 || a_label._height>_height/5) && fs>13 && ofs>=fs) {
  46343. ofs = fs; fs = std::max(13U,(unsigned int)cimg::round(fs/1.25f));
  46344. } else if (a_label._width<3*_width/10 && a_label._height<_height/20 && fs<64 && ofs<=fs) {
  46345. ofs = fs; fs = std::min(64U,(unsigned int)cimg::round(fs*1.25f));
  46346. } else { font_size = fs; break; }
  46347. } while (true);
  46348. a_label.normalize(0,255);
  46349. a_label+=(255 - a_label.get_dilate(3)).normalize(0,80);
  46350. a_label.resize(-100,-100,1,3,1);
  46351. return draw_image(0,is_down?height() - a_label.height():0,a_label,0.85f);
  46352. }
  46353. //! Draw a 2D vector field.
  46354. /**
  46355. \param flow Image of 2D vectors used as input data.
  46356. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46357. \param opacity Drawing opacity.
  46358. \param sampling Length (in pixels) between each arrow.
  46359. \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length).
  46360. \param is_arrow Tells if arrows must be drawn, instead of oriented segments.
  46361. \param pattern Used pattern to draw lines.
  46362. \note Clipping is supported.
  46363. **/
  46364. template<typename t1, typename t2>
  46365. CImg<T>& draw_quiver(const CImg<t1>& flow,
  46366. const t2 *const color, const float opacity=1,
  46367. const unsigned int sampling=25, const float factor=-20,
  46368. const bool is_arrow=true, const unsigned int pattern=~0U) {
  46369. return draw_quiver(flow,CImg<t2>(color,_spectrum,1,1,1,true),opacity,sampling,factor,is_arrow,pattern);
  46370. }
  46371. //! Draw a 2D vector field, using a field of colors.
  46372. /**
  46373. \param flow Image of 2D vectors used as input data.
  46374. \param color Image of spectrum()-D vectors corresponding to the color of each arrow.
  46375. \param opacity Opacity of the drawing.
  46376. \param sampling Length (in pixels) between each arrow.
  46377. \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length).
  46378. \param is_arrow Tells if arrows must be drawn, instead of oriented segments.
  46379. \param pattern Used pattern to draw lines.
  46380. \note Clipping is supported.
  46381. **/
  46382. template<typename t1, typename t2>
  46383. CImg<T>& draw_quiver(const CImg<t1>& flow,
  46384. const CImg<t2>& color, const float opacity=1,
  46385. const unsigned int sampling=25, const float factor=-20,
  46386. const bool is_arrow=true, const unsigned int pattern=~0U) {
  46387. if (is_empty()) return *this;
  46388. if (!flow || flow._spectrum!=2)
  46389. throw CImgArgumentException(_cimg_instance
  46390. "draw_quiver(): Invalid dimensions of specified flow (%u,%u,%u,%u,%p).",
  46391. cimg_instance,
  46392. flow._width,flow._height,flow._depth,flow._spectrum,flow._data);
  46393. if (sampling<=0)
  46394. throw CImgArgumentException(_cimg_instance
  46395. "draw_quiver(): Invalid sampling value %g "
  46396. "(should be >0)",
  46397. cimg_instance,
  46398. sampling);
  46399. const bool colorfield = (color._width==flow._width && color._height==flow._height &&
  46400. color._depth==1 && color._spectrum==_spectrum);
  46401. if (is_overlapped(flow)) return draw_quiver(+flow,color,opacity,sampling,factor,is_arrow,pattern);
  46402. float vmax,fact;
  46403. if (factor<=0) {
  46404. float m, M = (float)flow.get_norm(2).max_min(m);
  46405. vmax = (float)std::max(cimg::abs(m),cimg::abs(M));
  46406. if (!vmax) vmax = 1;
  46407. fact = -factor;
  46408. } else { fact = factor; vmax = 1; }
  46409. for (unsigned int y = sampling/2; y<_height; y+=sampling)
  46410. for (unsigned int x = sampling/2; x<_width; x+=sampling) {
  46411. const unsigned int X = x*flow._width/_width, Y = y*flow._height/_height;
  46412. float u = (float)flow(X,Y,0,0)*fact/vmax, v = (float)flow(X,Y,0,1)*fact/vmax;
  46413. if (is_arrow) {
  46414. const int xx = (int)(x + u), yy = (int)(y + v);
  46415. if (colorfield) draw_arrow(x,y,xx,yy,color.get_vector_at(X,Y)._data,opacity,45,sampling/5.f,pattern);
  46416. else draw_arrow(x,y,xx,yy,color._data,opacity,45,sampling/5.f,pattern);
  46417. } else {
  46418. if (colorfield)
  46419. draw_line((int)(x - 0.5*u),(int)(y - 0.5*v),(int)(x + 0.5*u),(int)(y + 0.5*v),
  46420. color.get_vector_at(X,Y)._data,opacity,pattern);
  46421. else draw_line((int)(x - 0.5*u),(int)(y - 0.5*v),(int)(x + 0.5*u),(int)(y + 0.5*v),
  46422. color._data,opacity,pattern);
  46423. }
  46424. }
  46425. return *this;
  46426. }
  46427. //! Draw a labeled horizontal axis.
  46428. /**
  46429. \param values_x Values along the horizontal axis.
  46430. \param y Y-coordinate of the horizontal axis in the image instance.
  46431. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46432. \param opacity Drawing opacity.
  46433. \param pattern Drawing pattern.
  46434. \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
  46435. \param allow_zero Enable/disable the drawing of label '0' if found.
  46436. **/
  46437. template<typename t, typename tc>
  46438. CImg<T>& draw_axis(const CImg<t>& values_x, const int y,
  46439. const tc *const color, const float opacity=1,
  46440. const unsigned int pattern=~0U, const unsigned int font_height=13,
  46441. const bool allow_zero=true, const float round_x=0) {
  46442. if (is_empty()) return *this;
  46443. const int yt = (y + 3 + font_height)<_height?y + 3:y - 2 - (int)font_height;
  46444. const int siz = (int)values_x.size() - 1;
  46445. CImg<charT> txt(32);
  46446. CImg<T> a_label;
  46447. if (siz<=0) { // Degenerated case
  46448. draw_line(0,y,_width - 1,y,color,opacity,pattern);
  46449. if (!siz) {
  46450. cimg_snprintf(txt,txt._width,"%g",round_x?cimg::round((double)*values_x,round_x):(double)*values_x);
  46451. a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
  46452. const int
  46453. _xt = (width() - a_label.width())/2,
  46454. xt = _xt<3?3:_xt + a_label.width()>=width() - 2?width() - 3 - a_label.width():_xt;
  46455. draw_point(width()/2,y - 1,color,opacity).draw_point(width()/2,y + 1,color,opacity);
  46456. if (allow_zero || *txt!='0' || txt[1]!=0)
  46457. draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
  46458. }
  46459. } else { // Regular case
  46460. if (values_x[0]<values_x[siz]) draw_arrow(0,y,_width - 1,y,color,opacity,30,5,pattern);
  46461. else draw_arrow(_width - 1,y,0,y,color,opacity,30,5,pattern);
  46462. cimg_foroff(values_x,x) {
  46463. cimg_snprintf(txt,txt._width,"%g",round_x?cimg::round((double)values_x(x),round_x):(double)values_x(x));
  46464. a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
  46465. const int
  46466. xi = (int)(x*(_width - 1)/siz),
  46467. _xt = xi - a_label.width()/2,
  46468. xt = _xt<3?3:_xt + a_label.width()>=width() - 2?width() - 3 - a_label.width():_xt;
  46469. draw_point(xi,y - 1,color,opacity).draw_point(xi,y + 1,color,opacity);
  46470. if (allow_zero || *txt!='0' || txt[1]!=0)
  46471. draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
  46472. }
  46473. }
  46474. return *this;
  46475. }
  46476. //! Draw a labeled vertical axis.
  46477. /**
  46478. \param x X-coordinate of the vertical axis in the image instance.
  46479. \param values_y Values along the Y-axis.
  46480. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46481. \param opacity Drawing opacity.
  46482. \param pattern Drawing pattern.
  46483. \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
  46484. \param allow_zero Enable/disable the drawing of label '0' if found.
  46485. **/
  46486. template<typename t, typename tc>
  46487. CImg<T>& draw_axis(const int x, const CImg<t>& values_y,
  46488. const tc *const color, const float opacity=1,
  46489. const unsigned int pattern=~0U, const unsigned int font_height=13,
  46490. const bool allow_zero=true, const float round_y=0) {
  46491. if (is_empty()) return *this;
  46492. int siz = (int)values_y.size() - 1;
  46493. CImg<charT> txt(32);
  46494. CImg<T> a_label;
  46495. if (siz<=0) { // Degenerated case
  46496. draw_line(x,0,x,_height - 1,color,opacity,pattern);
  46497. if (!siz) {
  46498. cimg_snprintf(txt,txt._width,"%g",round_y?cimg::round((double)*values_y,round_y):(double)*values_y);
  46499. a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
  46500. const int
  46501. _yt = (height() - a_label.height())/2,
  46502. yt = _yt<0?0:_yt + a_label.height()>=height()?height() - 1 - a_label.height():_yt,
  46503. _xt = x - 2 - a_label.width(),
  46504. xt = _xt>=0?_xt:x + 3;
  46505. draw_point(x - 1,height()/2,color,opacity).draw_point(x + 1,height()/2,color,opacity);
  46506. if (allow_zero || *txt!='0' || txt[1]!=0)
  46507. draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
  46508. }
  46509. } else { // Regular case
  46510. if (values_y[0]<values_y[siz]) draw_arrow(x,0,x,_height - 1,color,opacity,30,5,pattern);
  46511. else draw_arrow(x,_height - 1,x,0,color,opacity,30,5,pattern);
  46512. cimg_foroff(values_y,y) {
  46513. cimg_snprintf(txt,txt._width,"%g",round_y?cimg::round((double)values_y(y),round_y):(double)values_y(y));
  46514. a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
  46515. const int
  46516. yi = (int)(y*(_height - 1)/siz),
  46517. _yt = yi - a_label.height()/2,
  46518. yt = _yt<0?0:_yt + a_label.height()>=height()?height() - 1 - a_label.height():_yt,
  46519. _xt = x - 2 - a_label.width(),
  46520. xt = _xt>=0?_xt:x + 3;
  46521. draw_point(x - 1,yi,color,opacity).draw_point(x + 1,yi,color,opacity);
  46522. if (allow_zero || *txt!='0' || txt[1]!=0)
  46523. draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
  46524. }
  46525. }
  46526. return *this;
  46527. }
  46528. //! Draw labeled horizontal and vertical axes.
  46529. /**
  46530. \param values_x Values along the X-axis.
  46531. \param values_y Values along the Y-axis.
  46532. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46533. \param opacity Drawing opacity.
  46534. \param pattern_x Drawing pattern for the X-axis.
  46535. \param pattern_y Drawing pattern for the Y-axis.
  46536. \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
  46537. \param allow_zero Enable/disable the drawing of label '0' if found.
  46538. **/
  46539. template<typename tx, typename ty, typename tc>
  46540. CImg<T>& draw_axes(const CImg<tx>& values_x, const CImg<ty>& values_y,
  46541. const tc *const color, const float opacity=1,
  46542. const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U,
  46543. const unsigned int font_height=13, const bool allow_zero=true,
  46544. const float round_x=0, const float round_y=0) {
  46545. if (is_empty()) return *this;
  46546. const CImg<tx> nvalues_x(values_x._data,values_x.size(),1,1,1,true);
  46547. const int sizx = (int)values_x.size() - 1, wm1 = width() - 1;
  46548. if (sizx>=0) {
  46549. float ox = (float)*nvalues_x;
  46550. for (unsigned int x = sizx?1U:0U; x<_width; ++x) {
  46551. const float nx = (float)nvalues_x._linear_atX((float)x*sizx/wm1);
  46552. if (nx*ox<=0) {
  46553. draw_axis(nx==0?x:x - 1,values_y,color,opacity,pattern_y,font_height,allow_zero,round_y);
  46554. break;
  46555. }
  46556. ox = nx;
  46557. }
  46558. }
  46559. const CImg<ty> nvalues_y(values_y._data,values_y.size(),1,1,1,true);
  46560. const int sizy = (int)values_y.size() - 1, hm1 = height() - 1;
  46561. if (sizy>0) {
  46562. float oy = (float)nvalues_y[0];
  46563. for (unsigned int y = sizy?1U:0U; y<_height; ++y) {
  46564. const float ny = (float)nvalues_y._linear_atX((float)y*sizy/hm1);
  46565. if (ny*oy<=0) {
  46566. draw_axis(values_x,ny==0?y:y - 1,color,opacity,pattern_x,font_height,allow_zero,round_x);
  46567. break;
  46568. }
  46569. oy = ny;
  46570. }
  46571. }
  46572. return *this;
  46573. }
  46574. //! Draw labeled horizontal and vertical axes \overloading.
  46575. template<typename tc>
  46576. CImg<T>& draw_axes(const float x0, const float x1, const float y0, const float y1,
  46577. const tc *const color, const float opacity=1,
  46578. const int subdivisionx=-60, const int subdivisiony=-60,
  46579. const float precisionx=0, const float precisiony=0,
  46580. const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U,
  46581. const unsigned int font_height=13) {
  46582. if (is_empty()) return *this;
  46583. const bool allow_zero = (x0*x1>0) || (y0*y1>0);
  46584. const float
  46585. dx = cimg::abs(x1 - x0), dy = cimg::abs(y1 - y0),
  46586. px = dx<=0?1:precisionx==0?(float)std::pow(10.,(int)std::log10(dx) - 2.):precisionx,
  46587. py = dy<=0?1:precisiony==0?(float)std::pow(10.,(int)std::log10(dy) - 2.):precisiony;
  46588. if (x0!=x1 && y0!=y1)
  46589. draw_axes(CImg<floatT>::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1),
  46590. CImg<floatT>::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1),
  46591. color,opacity,pattern_x,pattern_y,font_height,allow_zero,px,py);
  46592. else if (x0==x1 && y0!=y1)
  46593. draw_axis((int)x0,CImg<floatT>::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1),
  46594. color,opacity,pattern_y,font_height,py);
  46595. else if (x0!=x1 && y0==y1)
  46596. draw_axis(CImg<floatT>::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1),(int)y0,
  46597. color,opacity,pattern_x,font_height,px);
  46598. return *this;
  46599. }
  46600. //! Draw 2D grid.
  46601. /**
  46602. \param values_x X-coordinates of the vertical lines.
  46603. \param values_y Y-coordinates of the horizontal lines.
  46604. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46605. \param opacity Drawing opacity.
  46606. \param pattern_x Drawing pattern for vertical lines.
  46607. \param pattern_y Drawing pattern for horizontal lines.
  46608. **/
  46609. template<typename tx, typename ty, typename tc>
  46610. CImg<T>& draw_grid(const CImg<tx>& values_x, const CImg<ty>& values_y,
  46611. const tc *const color, const float opacity=1,
  46612. const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) {
  46613. if (is_empty()) return *this;
  46614. if (values_x) cimg_foroff(values_x,x) {
  46615. const int xi = (int)values_x[x];
  46616. if (xi>=0 && xi<width()) draw_line(xi,0,xi,_height - 1,color,opacity,pattern_x);
  46617. }
  46618. if (values_y) cimg_foroff(values_y,y) {
  46619. const int yi = (int)values_y[y];
  46620. if (yi>=0 && yi<height()) draw_line(0,yi,_width - 1,yi,color,opacity,pattern_y);
  46621. }
  46622. return *this;
  46623. }
  46624. //! Draw 2D grid \simplification.
  46625. template<typename tc>
  46626. CImg<T>& draw_grid(const float delta_x, const float delta_y,
  46627. const float offsetx, const float offsety,
  46628. const bool invertx, const bool inverty,
  46629. const tc *const color, const float opacity=1,
  46630. const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) {
  46631. if (is_empty()) return *this;
  46632. CImg<uintT> seqx, seqy;
  46633. if (delta_x!=0) {
  46634. const float dx = delta_x>0?delta_x:_width*-delta_x/100;
  46635. const unsigned int nx = (unsigned int)(_width/dx);
  46636. seqx = CImg<uintT>::sequence(1 + nx,0,(unsigned int)(dx*nx));
  46637. if (offsetx) cimg_foroff(seqx,x) seqx(x) = (unsigned int)cimg::mod(seqx(x) + offsetx,(float)_width);
  46638. if (invertx) cimg_foroff(seqx,x) seqx(x) = _width - 1 - seqx(x);
  46639. }
  46640. if (delta_y!=0) {
  46641. const float dy = delta_y>0?delta_y:_height*-delta_y/100;
  46642. const unsigned int ny = (unsigned int)(_height/dy);
  46643. seqy = CImg<uintT>::sequence(1 + ny,0,(unsigned int)(dy*ny));
  46644. if (offsety) cimg_foroff(seqy,y) seqy(y) = (unsigned int)cimg::mod(seqy(y) + offsety,(float)_height);
  46645. if (inverty) cimg_foroff(seqy,y) seqy(y) = _height - 1 - seqy(y);
  46646. }
  46647. return draw_grid(seqx,seqy,color,opacity,pattern_x,pattern_y);
  46648. }
  46649. //! Draw 1D graph.
  46650. /**
  46651. \param data Image containing the graph values I = f(x).
  46652. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46653. \param opacity Drawing opacity.
  46654. \param plot_type Define the type of the plot:
  46655. - 0 = No plot.
  46656. - 1 = Plot using segments.
  46657. - 2 = Plot using cubic splines.
  46658. - 3 = Plot with bars.
  46659. \param vertex_type Define the type of points:
  46660. - 0 = No points.
  46661. - 1 = Point.
  46662. - 2 = Straight cross.
  46663. - 3 = Diagonal cross.
  46664. - 4 = Filled circle.
  46665. - 5 = Outlined circle.
  46666. - 6 = Square.
  46667. - 7 = Diamond.
  46668. \param ymin Lower bound of the y-range.
  46669. \param ymax Upper bound of the y-range.
  46670. \param pattern Drawing pattern.
  46671. \note
  46672. - if \c ymin==ymax==0, the y-range is computed automatically from the input samples.
  46673. **/
  46674. template<typename t, typename tc>
  46675. CImg<T>& draw_graph(const CImg<t>& data,
  46676. const tc *const color, const float opacity=1,
  46677. const unsigned int plot_type=1, const int vertex_type=1,
  46678. const double ymin=0, const double ymax=0, const unsigned int pattern=~0U) {
  46679. if (is_empty() || _height<=1) return *this;
  46680. if (!color)
  46681. throw CImgArgumentException(_cimg_instance
  46682. "draw_graph(): Specified color is (null).",
  46683. cimg_instance);
  46684. // Create shaded colors for displaying bar plots.
  46685. CImg<tc> color1, color2;
  46686. if (plot_type==3) {
  46687. color1.assign(_spectrum); color2.assign(_spectrum);
  46688. cimg_forC(*this,c) {
  46689. color1[c] = (tc)std::min((float)cimg::type<tc>::max(),(float)color[c]*1.2f);
  46690. color2[c] = (tc)(color[c]*0.4f);
  46691. }
  46692. }
  46693. // Compute min/max and normalization factors.
  46694. const ulongT
  46695. siz = data.size(),
  46696. _siz1 = siz - (plot_type!=3),
  46697. siz1 = _siz1?_siz1:1;
  46698. const unsigned int
  46699. _width1 = _width - (plot_type!=3),
  46700. width1 = _width1?_width1:1;
  46701. double m = ymin, M = ymax;
  46702. if (ymin==ymax) m = (double)data.max_min(M);
  46703. if (m==M) { --m; ++M; }
  46704. const float ca = (float)(M-m)/(_height - 1);
  46705. bool init_hatch = true;
  46706. // Draw graph edges
  46707. switch (plot_type%4) {
  46708. case 1 : { // Segments
  46709. int oX = 0, oY = (int)cimg::round((data[0] - m)/ca);
  46710. if (siz==1) {
  46711. const int Y = (int)cimg::round((*data - m)/ca);
  46712. draw_line(0,Y,width() - 1,Y,color,opacity,pattern);
  46713. } else {
  46714. const float fx = (float)_width/siz1;
  46715. for (ulongT off = 1; off<siz; ++off) {
  46716. const int
  46717. X = (int)cimg::round(off*fx) - 1,
  46718. Y = (int)cimg::round((data[off]-m)/ca);
  46719. draw_line(oX,oY,X,Y,color,opacity,pattern,init_hatch);
  46720. oX = X; oY = Y;
  46721. init_hatch = false;
  46722. }
  46723. }
  46724. } break;
  46725. case 2 : { // Spline
  46726. const CImg<t> ndata(data._data,siz,1,1,1,true);
  46727. int oY = (int)cimg::round((data[0] - m)/ca);
  46728. cimg_forX(*this,x) {
  46729. const int Y = (int)cimg::round((ndata._cubic_atX((float)x*siz1/width1)-m)/ca);
  46730. if (x>0) draw_line(x,oY,x + 1,Y,color,opacity,pattern,init_hatch);
  46731. init_hatch = false;
  46732. oY = Y;
  46733. }
  46734. } break;
  46735. case 3 : { // Bars
  46736. const int Y0 = (int)cimg::round(-m/ca);
  46737. const float fx = (float)_width/siz1;
  46738. int oX = 0;
  46739. cimg_foroff(data,off) {
  46740. const int
  46741. X = (int)cimg::round((off + 1)*fx) - 1,
  46742. Y = (int)cimg::round((data[off] - m)/ca);
  46743. draw_rectangle(oX,Y0,X,Y,color,opacity).
  46744. draw_line(oX,Y,oX,Y0,color2.data(),opacity).
  46745. draw_line(oX,Y0,X,Y0,Y<=Y0?color2.data():color1.data(),opacity).
  46746. draw_line(X,Y,X,Y0,color1.data(),opacity).
  46747. draw_line(oX,Y,X,Y,Y<=Y0?color1.data():color2.data(),opacity);
  46748. oX = X + 1;
  46749. }
  46750. } break;
  46751. default : break; // No edges
  46752. }
  46753. // Draw graph points
  46754. const unsigned int wb2 = plot_type==3?_width1/(2*siz):0;
  46755. const float fx = (float)_width1/siz1;
  46756. switch (vertex_type%8) {
  46757. case 1 : { // Point
  46758. cimg_foroff(data,off) {
  46759. const int
  46760. X = (int)cimg::round(off*fx + wb2),
  46761. Y = (int)cimg::round((data[off]-m)/ca);
  46762. draw_point(X,Y,color,opacity);
  46763. }
  46764. } break;
  46765. case 2 : { // Straight Cross
  46766. cimg_foroff(data,off) {
  46767. const int
  46768. X = (int)cimg::round(off*fx + wb2),
  46769. Y = (int)cimg::round((data[off]-m)/ca);
  46770. draw_line(X - 3,Y,X + 3,Y,color,opacity).draw_line(X,Y - 3,X,Y + 3,color,opacity);
  46771. }
  46772. } break;
  46773. case 3 : { // Diagonal Cross
  46774. cimg_foroff(data,off) {
  46775. const int
  46776. X = (int)cimg::round(off*fx + wb2),
  46777. Y = (int)cimg::round((data[off]-m)/ca);
  46778. draw_line(X - 3,Y - 3,X + 3,Y + 3,color,opacity).draw_line(X - 3,Y + 3,X + 3,Y - 3,color,opacity);
  46779. }
  46780. } break;
  46781. case 4 : { // Filled Circle
  46782. cimg_foroff(data,off) {
  46783. const int
  46784. X = (int)cimg::round(off*fx + wb2),
  46785. Y = (int)cimg::round((data[off]-m)/ca);
  46786. draw_circle(X,Y,3,color,opacity);
  46787. }
  46788. } break;
  46789. case 5 : { // Outlined circle
  46790. cimg_foroff(data,off) {
  46791. const int
  46792. X = (int)cimg::round(off*fx + wb2),
  46793. Y = (int)cimg::round((data[off]-m)/ca);
  46794. draw_circle(X,Y,3,color,opacity,~0U);
  46795. }
  46796. } break;
  46797. case 6 : { // Square
  46798. cimg_foroff(data,off) {
  46799. const int
  46800. X = (int)cimg::round(off*fx + wb2),
  46801. Y = (int)cimg::round((data[off]-m)/ca);
  46802. draw_rectangle(X - 3,Y - 3,X + 3,Y + 3,color,opacity,~0U);
  46803. }
  46804. } break;
  46805. case 7 : { // Diamond
  46806. cimg_foroff(data,off) {
  46807. const int
  46808. X = (int)cimg::round(off*fx + wb2),
  46809. Y = (int)cimg::round((data[off]-m)/ca);
  46810. draw_line(X,Y - 4,X + 4,Y,color,opacity).
  46811. draw_line(X + 4,Y,X,Y + 4,color,opacity).
  46812. draw_line(X,Y + 4,X - 4,Y,color,opacity).
  46813. draw_line(X - 4,Y,X,Y - 4,color,opacity);
  46814. }
  46815. } break;
  46816. default : break; // No points
  46817. }
  46818. return *this;
  46819. }
  46820. bool _draw_fill(const int x, const int y, const int z,
  46821. const CImg<T>& ref, const float tolerance2) const {
  46822. const T *ptr1 = data(x,y,z), *ptr2 = ref._data;
  46823. const ulongT off = _width*_height*_depth;
  46824. float diff = 0;
  46825. cimg_forC(*this,c) { diff += cimg::sqr(*ptr1 - *(ptr2++)); ptr1+=off; }
  46826. return diff<=tolerance2;
  46827. }
  46828. //! Draw filled 3D region with the flood fill algorithm.
  46829. /**
  46830. \param x0 X-coordinate of the starting point of the region to fill.
  46831. \param y0 Y-coordinate of the starting point of the region to fill.
  46832. \param z0 Z-coordinate of the starting point of the region to fill.
  46833. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  46834. \param[out] region Image that will contain the mask of the filled region mask, as an output.
  46835. \param tolerance Tolerance concerning neighborhood values.
  46836. \param opacity Opacity of the drawing.
  46837. \param is_high_connectivity Tells if 8-connexity must be used.
  46838. \return \c region is initialized with the binary mask of the filled region.
  46839. **/
  46840. template<typename tc, typename t>
  46841. CImg<T>& draw_fill(const int x0, const int y0, const int z0,
  46842. const tc *const color, const float opacity,
  46843. CImg<t> &region,
  46844. const float tolerance = 0,
  46845. const bool is_high_connectivity = false) {
  46846. #define _draw_fill_push(x,y,z) if (N>=stack._width) stack.resize(2*N + 1,1,1,3,0); \
  46847. stack[N] = x; stack(N,1) = y; stack(N++,2) = z
  46848. #define _draw_fill_pop(x,y,z) x = stack[--N]; y = stack(N,1); z = stack(N,2)
  46849. #define _draw_fill_is_inside(x,y,z) !_region(x,y,z) && _draw_fill(x,y,z,ref,tolerance2)
  46850. if (!containsXYZC(x0,y0,z0,0)) return *this;
  46851. const float nopacity = cimg::abs((float)opacity), copacity = 1 - std::max((float)opacity,0.f);
  46852. const float tolerance2 = cimg::sqr(tolerance);
  46853. const CImg<T> ref = get_vector_at(x0,y0,z0);
  46854. CImg<uintT> stack(256,1,1,3);
  46855. CImg<ucharT> _region(_width,_height,_depth,1,0);
  46856. unsigned int N = 0;
  46857. int x, y, z;
  46858. _draw_fill_push(x0,y0,z0);
  46859. while (N>0) {
  46860. _draw_fill_pop(x,y,z);
  46861. if (!_region(x,y,z)) {
  46862. const int yp = y - 1, yn = y + 1, zp = z - 1, zn = z + 1;
  46863. int xl = x, xr = x;
  46864. // Using these booleans reduces the number of pushes drastically.
  46865. bool is_yp = false, is_yn = false, is_zp = false, is_zn = false;
  46866. for (int step = -1; step<2; step+=2) {
  46867. while (x>=0 && x<width() && _draw_fill_is_inside(x,y,z)) {
  46868. if (yp>=0 && _draw_fill_is_inside(x,yp,z)) {
  46869. if (!is_yp) { _draw_fill_push(x,yp,z); is_yp = true; }
  46870. } else is_yp = false;
  46871. if (yn<height() && _draw_fill_is_inside(x,yn,z)) {
  46872. if (!is_yn) { _draw_fill_push(x,yn,z); is_yn = true; }
  46873. } else is_yn = false;
  46874. if (depth()>1) {
  46875. if (zp>=0 && _draw_fill_is_inside(x,y,zp)) {
  46876. if (!is_zp) { _draw_fill_push(x,y,zp); is_zp = true; }
  46877. } else is_zp = false;
  46878. if (zn<depth() && _draw_fill_is_inside(x,y,zn)) {
  46879. if (!is_zn) { _draw_fill_push(x,y,zn); is_zn = true; }
  46880. } else is_zn = false;
  46881. }
  46882. if (is_high_connectivity) {
  46883. const int xp = x - 1, xn = x + 1;
  46884. if (yp>=0 && !is_yp) {
  46885. if (xp>=0 && _draw_fill_is_inside(xp,yp,z)) {
  46886. _draw_fill_push(xp,yp,z); if (step<0) is_yp = true;
  46887. }
  46888. if (xn<width() && _draw_fill_is_inside(xn,yp,z)) {
  46889. _draw_fill_push(xn,yp,z); if (step>0) is_yp = true;
  46890. }
  46891. }
  46892. if (yn<height() && !is_yn) {
  46893. if (xp>=0 && _draw_fill_is_inside(xp,yn,z)) {
  46894. _draw_fill_push(xp,yn,z); if (step<0) is_yn = true;
  46895. }
  46896. if (xn<width() && _draw_fill_is_inside(xn,yn,z)) {
  46897. _draw_fill_push(xn,yn,z); if (step>0) is_yn = true;
  46898. }
  46899. }
  46900. if (depth()>1) {
  46901. if (zp>=0 && !is_zp) {
  46902. if (xp>=0 && _draw_fill_is_inside(xp,y,zp)) {
  46903. _draw_fill_push(xp,y,zp); if (step<0) is_zp = true;
  46904. }
  46905. if (xn<width() && _draw_fill_is_inside(xn,y,zp)) {
  46906. _draw_fill_push(xn,y,zp); if (step>0) is_zp = true;
  46907. }
  46908. if (yp>=0 && !is_yp) {
  46909. if (_draw_fill_is_inside(x,yp,zp)) { _draw_fill_push(x,yp,zp); }
  46910. if (xp>=0 && _draw_fill_is_inside(xp,yp,zp)) { _draw_fill_push(xp,yp,zp); }
  46911. if (xn<width() && _draw_fill_is_inside(xn,yp,zp)) { _draw_fill_push(xn,yp,zp); }
  46912. }
  46913. if (yn<height() && !is_yn) {
  46914. if (_draw_fill_is_inside(x,yn,zp)) { _draw_fill_push(x,yn,zp); }
  46915. if (xp>=0 && _draw_fill_is_inside(xp,yn,zp)) { _draw_fill_push(xp,yn,zp); }
  46916. if (xn<width() && _draw_fill_is_inside(xn,yn,zp)) { _draw_fill_push(xn,yn,zp); }
  46917. }
  46918. }
  46919. if (zn<depth() && !is_zn) {
  46920. if (xp>=0 && _draw_fill_is_inside(xp,y,zn)) {
  46921. _draw_fill_push(xp,y,zn); if (step<0) is_zn = true;
  46922. }
  46923. if (xn<width() && _draw_fill_is_inside(xn,y,zn)) {
  46924. _draw_fill_push(xn,y,zn); if (step>0) is_zn = true;
  46925. }
  46926. if (yp>=0 && !is_yp) {
  46927. if (_draw_fill_is_inside(x,yp,zn)) { _draw_fill_push(x,yp,zn); }
  46928. if (xp>=0 && _draw_fill_is_inside(xp,yp,zn)) { _draw_fill_push(xp,yp,zn); }
  46929. if (xn<width() && _draw_fill_is_inside(xn,yp,zn)) { _draw_fill_push(xn,yp,zn); }
  46930. }
  46931. if (yn<height() && !is_yn) {
  46932. if (_draw_fill_is_inside(x,yn,zn)) { _draw_fill_push(x,yn,zn); }
  46933. if (xp>=0 && _draw_fill_is_inside(xp,yn,zn)) { _draw_fill_push(xp,yn,zn); }
  46934. if (xn<width() && _draw_fill_is_inside(xn,yn,zn)) { _draw_fill_push(xn,yn,zn); }
  46935. }
  46936. }
  46937. }
  46938. }
  46939. x+=step;
  46940. }
  46941. if (step<0) { xl = ++x; x = xr + 1; is_yp = is_yn = is_zp = is_zn = false; }
  46942. else xr = --x;
  46943. }
  46944. std::memset(_region.data(xl,y,z),1,xr - xl + 1);
  46945. if (opacity==1) {
  46946. if (sizeof(T)==1) {
  46947. const int dx = xr - xl + 1;
  46948. cimg_forC(*this,c) std::memset(data(xl,y,z,c),(int)color[c],dx);
  46949. } else cimg_forC(*this,c) {
  46950. const T val = (T)color[c];
  46951. T *ptri = data(xl,y,z,c); for (int k = xl; k<=xr; ++k) *(ptri++) = val;
  46952. }
  46953. } else cimg_forC(*this,c) {
  46954. const T val = (T)(color[c]*nopacity);
  46955. T *ptri = data(xl,y,z,c); for (int k = xl; k<=xr; ++k) { *ptri = (T)(val + *ptri*copacity); ++ptri; }
  46956. }
  46957. }
  46958. }
  46959. _region.move_to(region);
  46960. return *this;
  46961. }
  46962. //! Draw filled 3D region with the flood fill algorithm \simplification.
  46963. template<typename tc>
  46964. CImg<T>& draw_fill(const int x0, const int y0, const int z0,
  46965. const tc *const color, const float opacity=1,
  46966. const float tolerance=0, const bool is_high_connexity=false) {
  46967. CImg<ucharT> tmp;
  46968. return draw_fill(x0,y0,z0,color,opacity,tmp,tolerance,is_high_connexity);
  46969. }
  46970. //! Draw filled 2D region with the flood fill algorithm \simplification.
  46971. template<typename tc>
  46972. CImg<T>& draw_fill(const int x0, const int y0,
  46973. const tc *const color, const float opacity=1,
  46974. const float tolerance=0, const bool is_high_connexity=false) {
  46975. CImg<ucharT> tmp;
  46976. return draw_fill(x0,y0,0,color,opacity,tmp,tolerance,is_high_connexity);
  46977. }
  46978. //! Draw a random plasma texture.
  46979. /**
  46980. \param alpha Alpha-parameter.
  46981. \param beta Beta-parameter.
  46982. \param scale Scale-parameter.
  46983. \note Use the mid-point algorithm to render.
  46984. **/
  46985. CImg<T>& draw_plasma(const float alpha=1, const float beta=0, const unsigned int scale=8) {
  46986. if (is_empty()) return *this;
  46987. const int w = width(), h = height();
  46988. const Tfloat m = (Tfloat)cimg::type<T>::min(), M = (Tfloat)cimg::type<T>::max();
  46989. cimg_uint64 rng = (cimg::_rand(),cimg::rng());
  46990. cimg_forZC(*this,z,c) {
  46991. CImg<T> ref = get_shared_slice(z,c);
  46992. for (int delta = 1<<std::min(scale,31U); delta>1; delta>>=1) {
  46993. const int delta2 = delta>>1;
  46994. const float r = alpha*delta + beta;
  46995. // Square step.
  46996. for (int y0 = 0; y0<h; y0+=delta)
  46997. for (int x0 = 0; x0<w; x0+=delta) {
  46998. const int x1 = (x0 + delta)%w, y1 = (y0 + delta)%h, xc = (x0 + delta2)%w, yc = (y0 + delta2)%h;
  46999. const Tfloat val = (Tfloat)(0.25f*(ref(x0,y0) + ref(x0,y1) + ref(x0,y1) + ref(x1,y1)) +
  47000. r*cimg::rand(-1,1,&rng));
  47001. ref(xc,yc) = (T)(val<m?m:val>M?M:val);
  47002. }
  47003. // Diamond steps.
  47004. for (int y = -delta2; y<h; y+=delta)
  47005. for (int x0=0; x0<w; x0+=delta) {
  47006. const int y0 = cimg::mod(y,h), x1 = (x0 + delta)%w, y1 = (y + delta)%h,
  47007. xc = (x0 + delta2)%w, yc = (y + delta2)%h;
  47008. const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
  47009. r*cimg::rand(-1,1,&rng));
  47010. ref(xc,yc) = (T)(val<m?m:val>M?M:val);
  47011. }
  47012. for (int y0 = 0; y0<h; y0+=delta)
  47013. for (int x = -delta2; x<w; x+=delta) {
  47014. const int x0 = cimg::mod(x,w), x1 = (x + delta)%w, y1 = (y0 + delta)%h,
  47015. xc = (x + delta2)%w, yc = (y0 + delta2)%h;
  47016. const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
  47017. r*cimg::rand(-1,1,&rng));
  47018. ref(xc,yc) = (T)(val<m?m:val>M?M:val);
  47019. }
  47020. for (int y = -delta2; y<h; y+=delta)
  47021. for (int x = -delta2; x<w; x+=delta) {
  47022. const int x0 = cimg::mod(x,w), y0 = cimg::mod(y,h), x1 = (x + delta)%w, y1 = (y + delta)%h,
  47023. xc = (x + delta2)%w, yc = (y + delta2)%h;
  47024. const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
  47025. r*cimg::rand(-1,1,&rng));
  47026. ref(xc,yc) = (T)(val<m?m:val>M?M:val);
  47027. }
  47028. }
  47029. }
  47030. cimg::srand(rng);
  47031. return *this;
  47032. }
  47033. //! Draw a quadratic Mandelbrot or Julia 2D fractal.
  47034. /**
  47035. \param x0 X-coordinate of the upper-left pixel.
  47036. \param y0 Y-coordinate of the upper-left pixel.
  47037. \param x1 X-coordinate of the lower-right pixel.
  47038. \param y1 Y-coordinate of the lower-right pixel.
  47039. \param colormap Colormap.
  47040. \param opacity Drawing opacity.
  47041. \param z0r Real part of the upper-left fractal vertex.
  47042. \param z0i Imaginary part of the upper-left fractal vertex.
  47043. \param z1r Real part of the lower-right fractal vertex.
  47044. \param z1i Imaginary part of the lower-right fractal vertex.
  47045. \param iteration_max Maximum number of iterations for each estimated point.
  47046. \param is_normalized_iteration Tells if iterations are normalized.
  47047. \param is_julia_set Tells if the Mandelbrot or Julia set is rendered.
  47048. \param param_r Real part of the Julia set parameter.
  47049. \param param_i Imaginary part of the Julia set parameter.
  47050. \note Fractal rendering is done by the Escape Time Algorithm.
  47051. **/
  47052. template<typename tc>
  47053. CImg<T>& draw_mandelbrot(const int x0, const int y0, const int x1, const int y1,
  47054. const CImg<tc>& colormap, const float opacity=1,
  47055. const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2,
  47056. const unsigned int iteration_max=255,
  47057. const bool is_normalized_iteration=false,
  47058. const bool is_julia_set=false,
  47059. const double param_r=0, const double param_i=0) {
  47060. if (is_empty()) return *this;
  47061. CImg<tc> palette;
  47062. if (colormap) palette.assign(colormap._data,colormap.size()/colormap._spectrum,1,1,colormap._spectrum,true);
  47063. if (palette && palette._spectrum!=_spectrum)
  47064. throw CImgArgumentException(_cimg_instance
  47065. "draw_mandelbrot(): Instance and specified colormap (%u,%u,%u,%u,%p) have "
  47066. "incompatible dimensions.",
  47067. cimg_instance,
  47068. colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);
  47069. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f), ln2 = (float)std::log(2.);
  47070. const int
  47071. _x0 = cimg::cut(x0,0,width() - 1),
  47072. _y0 = cimg::cut(y0,0,height() - 1),
  47073. _x1 = cimg::cut(x1,0,width() - 1),
  47074. _y1 = cimg::cut(y1,0,height() - 1);
  47075. cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
  47076. cimg_openmp_if((1 + _x1 - _x0)*(1 + _y1 - _y0)>=(cimg_openmp_sizefactor)*2048))
  47077. for (int q = _y0; q<=_y1; ++q)
  47078. for (int p = _x0; p<=_x1; ++p) {
  47079. unsigned int iteration = 0;
  47080. const double x = z0r + p*(z1r-z0r)/_width, y = z0i + q*(z1i-z0i)/_height;
  47081. double zr, zi, cr, ci;
  47082. if (is_julia_set) { zr = x; zi = y; cr = param_r; ci = param_i; }
  47083. else { zr = param_r; zi = param_i; cr = x; ci = y; }
  47084. for (iteration=1; zr*zr + zi*zi<=4 && iteration<=iteration_max; ++iteration) {
  47085. const double temp = zr*zr - zi*zi + cr;
  47086. zi = 2*zr*zi + ci;
  47087. zr = temp;
  47088. }
  47089. if (iteration>iteration_max) {
  47090. if (palette) {
  47091. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette(0,c);
  47092. else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(0,c)*nopacity + (*this)(p,q,0,c)*copacity);
  47093. } else {
  47094. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)0;
  47095. else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)((*this)(p,q,0,c)*copacity);
  47096. }
  47097. } else if (is_normalized_iteration) {
  47098. const float
  47099. normz = (float)cimg::abs(zr*zr + zi*zi),
  47100. niteration = (float)(iteration + 1 - std::log(std::log(normz))/ln2);
  47101. if (palette) {
  47102. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._linear_atX(niteration,c);
  47103. else cimg_forC(*this,c)
  47104. (*this)(p,q,0,c) = (T)(palette._linear_atX(niteration,c)*nopacity + (*this)(p,q,0,c)*copacity);
  47105. } else {
  47106. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)niteration;
  47107. else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(niteration*nopacity + (*this)(p,q,0,c)*copacity);
  47108. }
  47109. } else {
  47110. if (palette) {
  47111. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._atX(iteration,c);
  47112. else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(iteration,c)*nopacity + (*this)(p,q,0,c)*copacity);
  47113. } else {
  47114. if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)iteration;
  47115. else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(iteration*nopacity + (*this)(p,q,0,c)*copacity);
  47116. }
  47117. }
  47118. }
  47119. return *this;
  47120. }
  47121. //! Draw a quadratic Mandelbrot or Julia 2D fractal \overloading.
  47122. template<typename tc>
  47123. CImg<T>& draw_mandelbrot(const CImg<tc>& colormap, const float opacity=1,
  47124. const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2,
  47125. const unsigned int iteration_max=255,
  47126. const bool is_normalized_iteration=false,
  47127. const bool is_julia_set=false,
  47128. const double param_r=0, const double param_i=0) {
  47129. return draw_mandelbrot(0,0,_width - 1,_height - 1,colormap,opacity,
  47130. z0r,z0i,z1r,z1i,iteration_max,is_normalized_iteration,is_julia_set,param_r,param_i);
  47131. }
  47132. //! Draw a 1D gaussian function.
  47133. /**
  47134. \param xc X-coordinate of the gaussian center.
  47135. \param sigma Standard variation of the gaussian distribution.
  47136. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  47137. \param opacity Drawing opacity.
  47138. **/
  47139. template<typename tc>
  47140. CImg<T>& draw_gaussian(const float xc, const float sigma,
  47141. const tc *const color, const float opacity=1) {
  47142. if (is_empty()) return *this;
  47143. if (!color)
  47144. throw CImgArgumentException(_cimg_instance
  47145. "draw_gaussian(): Specified color is (null).",
  47146. cimg_instance);
  47147. const float sigma2 = 2*sigma*sigma, nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  47148. const ulongT whd = (ulongT)_width*_height*_depth;
  47149. const tc *col = color;
  47150. cimg_forX(*this,x) {
  47151. const float dx = (x - xc), val = (float)std::exp(-dx*dx/sigma2);
  47152. T *ptrd = data(x,0,0,0);
  47153. if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
  47154. else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
  47155. col-=_spectrum;
  47156. }
  47157. return *this;
  47158. }
  47159. //! Draw a 2D gaussian function.
  47160. /**
  47161. \param xc X-coordinate of the gaussian center.
  47162. \param yc Y-coordinate of the gaussian center.
  47163. \param tensor Covariance matrix (must be 2x2).
  47164. \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
  47165. \param opacity Drawing opacity.
  47166. **/
  47167. template<typename t, typename tc>
  47168. CImg<T>& draw_gaussian(const float xc, const float yc, const CImg<t>& tensor,
  47169. const tc *const color, const float opacity=1) {
  47170. if (is_empty()) return *this;
  47171. if (tensor._width!=2 || tensor._height!=2 || tensor._depth!=1 || tensor._spectrum!=1)
  47172. throw CImgArgumentException(_cimg_instance
  47173. "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 2x2 matrix.",
  47174. cimg_instance,
  47175. tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data);
  47176. if (!color)
  47177. throw CImgArgumentException(_cimg_instance
  47178. "draw_gaussian(): Specified color is (null).",
  47179. cimg_instance);
  47180. typedef typename CImg<t>::Tfloat tfloat;
  47181. const CImg<tfloat> invT = tensor.get_invert(), invT2 = (invT*invT)/=-2.;
  47182. const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = invT2(1,1);
  47183. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  47184. const ulongT whd = (ulongT)_width*_height*_depth;
  47185. const tc *col = color;
  47186. float dy = -yc;
  47187. cimg_forY(*this,y) {
  47188. float dx = -xc;
  47189. cimg_forX(*this,x) {
  47190. const float val = (float)std::exp(a*dx*dx + b*dx*dy + c*dy*dy);
  47191. T *ptrd = data(x,y,0,0);
  47192. if (opacity>=1) cimg_forC(*this,k) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
  47193. else cimg_forC(*this,k) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
  47194. col-=_spectrum;
  47195. ++dx;
  47196. }
  47197. ++dy;
  47198. }
  47199. return *this;
  47200. }
  47201. //! Draw a 2D gaussian function \overloading.
  47202. template<typename tc>
  47203. CImg<T>& draw_gaussian(const int xc, const int yc, const float r1, const float r2, const float ru, const float rv,
  47204. const tc *const color, const float opacity=1) {
  47205. const double
  47206. a = r1*ru*ru + r2*rv*rv,
  47207. b = (r1-r2)*ru*rv,
  47208. c = r1*rv*rv + r2*ru*ru;
  47209. const CImg<Tfloat> tensor(2,2,1,1, a,b,b,c);
  47210. return draw_gaussian(xc,yc,tensor,color,opacity);
  47211. }
  47212. //! Draw a 2D gaussian function \overloading.
  47213. template<typename tc>
  47214. CImg<T>& draw_gaussian(const float xc, const float yc, const float sigma,
  47215. const tc *const color, const float opacity=1) {
  47216. return draw_gaussian(xc,yc,CImg<floatT>::diagonal(sigma,sigma),color,opacity);
  47217. }
  47218. //! Draw a 3D gaussian function \overloading.
  47219. template<typename t, typename tc>
  47220. CImg<T>& draw_gaussian(const float xc, const float yc, const float zc, const CImg<t>& tensor,
  47221. const tc *const color, const float opacity=1) {
  47222. if (is_empty()) return *this;
  47223. typedef typename CImg<t>::Tfloat tfloat;
  47224. if (tensor._width!=3 || tensor._height!=3 || tensor._depth!=1 || tensor._spectrum!=1)
  47225. throw CImgArgumentException(_cimg_instance
  47226. "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 3x3 matrix.",
  47227. cimg_instance,
  47228. tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data);
  47229. const CImg<tfloat> invT = tensor.get_invert(), invT2 = (invT*invT)/=-2.;
  47230. const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = 2*invT2(2,0), d = invT2(1,1), e = 2*invT2(2,1), f = invT2(2,2);
  47231. const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
  47232. const ulongT whd = (ulongT)_width*_height*_depth;
  47233. const tc *col = color;
  47234. cimg_forXYZ(*this,x,y,z) {
  47235. const float
  47236. dx = (x - xc), dy = (y - yc), dz = (z - zc),
  47237. val = (float)std::exp(a*dx*dx + b*dx*dy + c*dx*dz + d*dy*dy + e*dy*dz + f*dz*dz);
  47238. T *ptrd = data(x,y,z,0);
  47239. if (opacity>=1) cimg_forC(*this,k) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
  47240. else cimg_forC(*this,k) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
  47241. col-=_spectrum;
  47242. }
  47243. return *this;
  47244. }
  47245. //! Draw a 3D gaussian function \overloading.
  47246. template<typename tc>
  47247. CImg<T>& draw_gaussian(const float xc, const float yc, const float zc, const float sigma,
  47248. const tc *const color, const float opacity=1) {
  47249. return draw_gaussian(xc,yc,zc,CImg<floatT>::diagonal(sigma,sigma,sigma),color,opacity);
  47250. }
  47251. //! Draw a 3D object.
  47252. /**
  47253. \param x0 X-coordinate of the 3D object position
  47254. \param y0 Y-coordinate of the 3D object position
  47255. \param z0 Z-coordinate of the 3D object position
  47256. \param vertices Image Nx3 describing 3D point coordinates
  47257. \param primitives List of P primitives
  47258. \param colors List of P color (or textures)
  47259. \param opacities Image or list of P opacities
  47260. \param render_type d Render type (0=Points, 1=Lines, 2=Faces (no light), 3=Faces (flat), 4=Faces(Gouraud)
  47261. \param is_double_sided Tells if object faces have two sides or are oriented.
  47262. \param focale length of the focale (0 for parallel projection)
  47263. \param lightx X-coordinate of the light
  47264. \param lighty Y-coordinate of the light
  47265. \param lightz Z-coordinate of the light
  47266. \param specular_lightness Amount of specular light.
  47267. \param specular_shininess Shininess of the object
  47268. \param g_opacity Global opacity of the object.
  47269. **/
  47270. template<typename tp, typename tf, typename tc, typename to>
  47271. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47272. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47273. const CImgList<tc>& colors, const CImg<to>& opacities,
  47274. const unsigned int render_type=4,
  47275. const bool is_double_sided=false, const float focale=700,
  47276. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47277. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47278. const float g_opacity=1) {
  47279. return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type,
  47280. is_double_sided,focale,lightx,lighty,lightz,
  47281. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47282. }
  47283. //! Draw a 3D object \simplification.
  47284. template<typename tp, typename tf, typename tc, typename to, typename tz>
  47285. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47286. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47287. const CImgList<tc>& colors, const CImg<to>& opacities,
  47288. const unsigned int render_type,
  47289. const bool is_double_sided, const float focale,
  47290. const float lightx, const float lighty, const float lightz,
  47291. const float specular_lightness, const float specular_shininess,
  47292. const float g_opacity, CImg<tz>& zbuffer) {
  47293. return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
  47294. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47295. specular_lightness,specular_shininess,g_opacity,1);
  47296. }
  47297. #ifdef cimg_use_board
  47298. template<typename tp, typename tf, typename tc, typename to>
  47299. CImg<T>& draw_object3d(LibBoard::Board& board,
  47300. const float x0, const float y0, const float z0,
  47301. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47302. const CImgList<tc>& colors, const CImg<to>& opacities,
  47303. const unsigned int render_type=4,
  47304. const bool is_double_sided=false, const float focale=700,
  47305. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47306. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47307. const float g_opacity=1) {
  47308. return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type,
  47309. is_double_sided,focale,lightx,lighty,lightz,
  47310. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47311. }
  47312. template<typename tp, typename tf, typename tc, typename to, typename tz>
  47313. CImg<T>& draw_object3d(LibBoard::Board& board,
  47314. const float x0, const float y0, const float z0,
  47315. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47316. const CImgList<tc>& colors, const CImg<to>& opacities,
  47317. const unsigned int render_type,
  47318. const bool is_double_sided, const float focale,
  47319. const float lightx, const float lighty, const float lightz,
  47320. const float specular_lightness, const float specular_shininess,
  47321. const float g_opacity, CImg<tz>& zbuffer) {
  47322. return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
  47323. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47324. specular_lightness,specular_shininess,g_opacity,1);
  47325. }
  47326. #endif
  47327. //! Draw a 3D object \simplification.
  47328. template<typename tp, typename tf, typename tc, typename to>
  47329. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47330. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47331. const CImgList<tc>& colors, const CImgList<to>& opacities,
  47332. const unsigned int render_type=4,
  47333. const bool is_double_sided=false, const float focale=700,
  47334. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47335. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47336. const float g_opacity=1) {
  47337. return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type,
  47338. is_double_sided,focale,lightx,lighty,lightz,
  47339. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47340. }
  47341. //! Draw a 3D object \simplification.
  47342. template<typename tp, typename tf, typename tc, typename to, typename tz>
  47343. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47344. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47345. const CImgList<tc>& colors, const CImgList<to>& opacities,
  47346. const unsigned int render_type,
  47347. const bool is_double_sided, const float focale,
  47348. const float lightx, const float lighty, const float lightz,
  47349. const float specular_lightness, const float specular_shininess,
  47350. const float g_opacity, CImg<tz>& zbuffer) {
  47351. return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
  47352. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47353. specular_lightness,specular_shininess,g_opacity,1);
  47354. }
  47355. #ifdef cimg_use_board
  47356. template<typename tp, typename tf, typename tc, typename to>
  47357. CImg<T>& draw_object3d(LibBoard::Board& board,
  47358. const float x0, const float y0, const float z0,
  47359. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47360. const CImgList<tc>& colors, const CImgList<to>& opacities,
  47361. const unsigned int render_type=4,
  47362. const bool is_double_sided=false, const float focale=700,
  47363. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47364. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47365. const float g_opacity=1) {
  47366. return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type,
  47367. is_double_sided,focale,lightx,lighty,lightz,
  47368. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47369. }
  47370. template<typename tp, typename tf, typename tc, typename to, typename tz>
  47371. CImg<T>& draw_object3d(LibBoard::Board& board,
  47372. const float x0, const float y0, const float z0,
  47373. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47374. const CImgList<tc>& colors, const CImgList<to>& opacities,
  47375. const unsigned int render_type,
  47376. const bool is_double_sided, const float focale,
  47377. const float lightx, const float lighty, const float lightz,
  47378. const float specular_lightness, const float specular_shininess,
  47379. const float g_opacity, CImg<tz>& zbuffer) {
  47380. return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
  47381. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47382. specular_lightness,specular_shininess,g_opacity,1);
  47383. }
  47384. #endif
  47385. //! Draw a 3D object \simplification.
  47386. template<typename tp, typename tf, typename tc>
  47387. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47388. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47389. const CImgList<tc>& colors,
  47390. const unsigned int render_type=4,
  47391. const bool is_double_sided=false, const float focale=700,
  47392. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47393. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47394. const float g_opacity=1) {
  47395. return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
  47396. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47397. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47398. }
  47399. //! Draw a 3D object \simplification.
  47400. template<typename tp, typename tf, typename tc, typename tz>
  47401. CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
  47402. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47403. const CImgList<tc>& colors,
  47404. const unsigned int render_type,
  47405. const bool is_double_sided, const float focale,
  47406. const float lightx, const float lighty, const float lightz,
  47407. const float specular_lightness, const float specular_shininess,
  47408. const float g_opacity, CImg<tz>& zbuffer) {
  47409. return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
  47410. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47411. specular_lightness,specular_shininess,g_opacity,zbuffer);
  47412. }
  47413. #ifdef cimg_use_board
  47414. template<typename tp, typename tf, typename tc, typename to>
  47415. CImg<T>& draw_object3d(LibBoard::Board& board,
  47416. const float x0, const float y0, const float z0,
  47417. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47418. const CImgList<tc>& colors,
  47419. const unsigned int render_type=4,
  47420. const bool is_double_sided=false, const float focale=700,
  47421. const float lightx=0, const float lighty=0, const float lightz=-5e8,
  47422. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  47423. const float g_opacity=1) {
  47424. return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
  47425. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47426. specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
  47427. }
  47428. template<typename tp, typename tf, typename tc, typename to, typename tz>
  47429. CImg<T>& draw_object3d(LibBoard::Board& board,
  47430. const float x0, const float y0, const float z0,
  47431. const CImg<tp>& vertices, const CImgList<tf>& primitives,
  47432. const CImgList<tc>& colors,
  47433. const unsigned int render_type,
  47434. const bool is_double_sided, const float focale,
  47435. const float lightx, const float lighty, const float lightz,
  47436. const float specular_lightness, const float specular_shininess,
  47437. const float g_opacity, CImg<tz>& zbuffer) {
  47438. return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
  47439. render_type,is_double_sided,focale,lightx,lighty,lightz,
  47440. specular_lightness,specular_shininess,g_opacity,zbuffer);
  47441. }
  47442. #endif
  47443. template<typename t, typename to>
  47444. static float __draw_object3d(const CImgList<t>& opacities, const unsigned int n_primitive, CImg<to>& opacity) {
  47445. if (n_primitive>=opacities._width || opacities[n_primitive].is_empty()) { opacity.assign(); return 1; }
  47446. if (opacities[n_primitive].size()==1) { opacity.assign(); return opacities(n_primitive,0); }
  47447. opacity.assign(opacities[n_primitive],true);
  47448. return 1.f;
  47449. }
  47450. template<typename t, typename to>
  47451. static float __draw_object3d(const CImg<t>& opacities, const unsigned int n_primitive, CImg<to>& opacity) {
  47452. opacity.assign();
  47453. return n_primitive>=opacities._width?1.f:(float)opacities[n_primitive];
  47454. }
  47455. template<typename t>
  47456. static float ___draw_object3d(const CImgList<t>& opacities, const unsigned int n_primitive) {
  47457. return n_primitive<opacities._width && opacities[n_primitive].size()==1?(float)opacities(n_primitive,0):1.f;
  47458. }
  47459. template<typename t>
  47460. static float ___draw_object3d(const CImg<t>& opacities, const unsigned int n_primitive) {
  47461. return n_primitive<opacities._width?(float)opacities[n_primitive]:1.f;
  47462. }
  47463. template<typename tz, typename tp, typename tf, typename tc, typename to>
  47464. CImg<T>& _draw_object3d(void *const pboard, CImg<tz>& zbuffer,
  47465. const float X, const float Y, const float Z,
  47466. const CImg<tp>& vertices,
  47467. const CImgList<tf>& primitives,
  47468. const CImgList<tc>& colors,
  47469. const to& opacities,
  47470. const unsigned int render_type,
  47471. const bool is_double_sided, const float focale,
  47472. const float lightx, const float lighty, const float lightz,
  47473. const float specular_lightness, const float specular_shininess,
  47474. const float g_opacity, const float sprite_scale) {
  47475. typedef typename cimg::superset2<tp,tz,float>::type tpfloat;
  47476. typedef typename to::value_type _to;
  47477. if (is_empty() || !vertices || !primitives) return *this;
  47478. CImg<char> error_message(1024);
  47479. if (!vertices.is_object3d(primitives,colors,opacities,false,error_message))
  47480. throw CImgArgumentException(_cimg_instance
  47481. "draw_object3d(): Invalid specified 3D object (%u,%u) (%s).",
  47482. cimg_instance,vertices._width,primitives._width,error_message.data());
  47483. #ifndef cimg_use_board
  47484. if (pboard) return *this;
  47485. #endif
  47486. if (render_type==5) cimg::mutex(10); // Static variable used in this case, breaks thread-safety
  47487. const float
  47488. nspec = 1 - (specular_lightness<0.f?0.f:(specular_lightness>1.f?1.f:specular_lightness)),
  47489. nspec2 = 1 + (specular_shininess<0.f?0.f:specular_shininess),
  47490. nsl1 = (nspec2 - 1)/cimg::sqr(nspec - 1),
  47491. nsl2 = 1 - 2*nsl1*nspec,
  47492. nsl3 = nspec2 - nsl1 - nsl2;
  47493. // Create light texture for phong-like rendering.
  47494. CImg<floatT> light_texture;
  47495. if (render_type==5) {
  47496. if (colors._width>primitives._width) {
  47497. static CImg<floatT> default_light_texture;
  47498. static const tc *lptr = 0;
  47499. static tc ref_values[64] = { 0 };
  47500. const CImg<tc>& img = colors.back();
  47501. bool is_same_texture = (lptr==img._data);
  47502. if (is_same_texture)
  47503. for (unsigned int r = 0, j = 0; j<8; ++j)
  47504. for (unsigned int i = 0; i<8; ++i)
  47505. if (ref_values[r++]!=img(i*img._width/9,j*img._height/9,0,(i + j)%img._spectrum)) {
  47506. is_same_texture = false; break;
  47507. }
  47508. if (!is_same_texture || default_light_texture._spectrum<_spectrum) {
  47509. (default_light_texture.assign(img,false)/=255).resize(-100,-100,1,_spectrum);
  47510. lptr = colors.back().data();
  47511. for (unsigned int r = 0, j = 0; j<8; ++j)
  47512. for (unsigned int i = 0; i<8; ++i)
  47513. ref_values[r++] = img(i*img._width/9,j*img._height/9,0,(i + j)%img._spectrum);
  47514. }
  47515. light_texture.assign(default_light_texture,true);
  47516. } else {
  47517. static CImg<floatT> default_light_texture;
  47518. static float olightx = 0, olighty = 0, olightz = 0, ospecular_shininess = 0;
  47519. if (!default_light_texture ||
  47520. lightx!=olightx || lighty!=olighty || lightz!=olightz ||
  47521. specular_shininess!=ospecular_shininess || default_light_texture._spectrum<_spectrum) {
  47522. default_light_texture.assign(512,512);
  47523. const float
  47524. dlx = lightx - X,
  47525. dly = lighty - Y,
  47526. dlz = lightz - Z,
  47527. nl = cimg::hypot(dlx,dly,dlz),
  47528. nlx = (default_light_texture._width - 1)/2*(1 + dlx/nl),
  47529. nly = (default_light_texture._height - 1)/2*(1 + dly/nl),
  47530. white[] = { 1 };
  47531. default_light_texture.draw_gaussian(nlx,nly,default_light_texture._width/3.f,white);
  47532. cimg_forXY(default_light_texture,x,y) {
  47533. const float factor = default_light_texture(x,y);
  47534. if (factor>nspec) default_light_texture(x,y) = std::min(2.f,nsl1*factor*factor + nsl2*factor + nsl3);
  47535. }
  47536. default_light_texture.resize(-100,-100,1,_spectrum);
  47537. olightx = lightx; olighty = lighty; olightz = lightz; ospecular_shininess = specular_shininess;
  47538. }
  47539. light_texture.assign(default_light_texture,true);
  47540. }
  47541. }
  47542. // Compute 3D to 2D projection.
  47543. CImg<tpfloat> projections(vertices._width,2);
  47544. tpfloat parallzmin = cimg::type<tpfloat>::max();
  47545. const float absfocale = focale?cimg::abs(focale):0;
  47546. if (absfocale) {
  47547. cimg_pragma_openmp(parallel for cimg_openmp_if_size(projections.size(),4096))
  47548. cimg_forX(projections,l) { // Perspective projection
  47549. const tpfloat
  47550. x = (tpfloat)vertices(l,0),
  47551. y = (tpfloat)vertices(l,1),
  47552. z = (tpfloat)vertices(l,2);
  47553. const tpfloat projectedz = z + Z + absfocale;
  47554. projections(l,1) = Y + absfocale*y/projectedz;
  47555. projections(l,0) = X + absfocale*x/projectedz;
  47556. }
  47557. } else {
  47558. cimg_pragma_openmp(parallel for cimg_openmp_if_size(projections.size(),4096))
  47559. cimg_forX(projections,l) { // Parallel projection
  47560. const tpfloat
  47561. x = (tpfloat)vertices(l,0),
  47562. y = (tpfloat)vertices(l,1),
  47563. z = (tpfloat)vertices(l,2);
  47564. if (z<parallzmin) parallzmin = z;
  47565. projections(l,1) = Y + y;
  47566. projections(l,0) = X + x;
  47567. }
  47568. }
  47569. const float _focale = absfocale?absfocale:(1e5f-parallzmin);
  47570. float zmax = 0;
  47571. if (zbuffer) zmax = vertices.get_shared_row(2).max();
  47572. // Compute visible primitives.
  47573. CImg<uintT> visibles(primitives._width,1,1,1,~0U);
  47574. CImg<tpfloat> zrange(primitives._width);
  47575. const tpfloat zmin = absfocale?(tpfloat)(1.5f - absfocale):cimg::type<tpfloat>::min();
  47576. bool is_forward = zbuffer?true:false;
  47577. cimg_pragma_openmp(parallel for cimg_openmp_if_size(primitives.size(),4096))
  47578. cimglist_for(primitives,l) {
  47579. const CImg<tf>& primitive = primitives[l];
  47580. switch (primitive.size()) {
  47581. case 1 : { // Point
  47582. CImg<_to> _opacity;
  47583. __draw_object3d(opacities,l,_opacity);
  47584. if (l<=colors.width() && (colors[l].size()!=_spectrum || _opacity)) is_forward = false;
  47585. const unsigned int i0 = (unsigned int)primitive(0);
  47586. const tpfloat z0 = Z + vertices(i0,2);
  47587. if (z0>zmin) {
  47588. visibles(l) = (unsigned int)l;
  47589. zrange(l) = z0;
  47590. }
  47591. } break;
  47592. case 5 : { // Sphere
  47593. const unsigned int
  47594. i0 = (unsigned int)primitive(0),
  47595. i1 = (unsigned int)primitive(1);
  47596. const tpfloat
  47597. Xc = 0.5f*((float)vertices(i0,0) + (float)vertices(i1,0)),
  47598. Yc = 0.5f*((float)vertices(i0,1) + (float)vertices(i1,1)),
  47599. Zc = 0.5f*((float)vertices(i0,2) + (float)vertices(i1,2)),
  47600. _zc = Z + Zc,
  47601. zc = _zc + _focale,
  47602. xc = X + Xc*(absfocale?absfocale/zc:1),
  47603. yc = Y + Yc*(absfocale?absfocale/zc:1),
  47604. radius = 0.5f*cimg::hypot(vertices(i1,0) - vertices(i0,0),
  47605. vertices(i1,1) - vertices(i0,1),
  47606. vertices(i1,2) - vertices(i0,2))*(absfocale?absfocale/zc:1),
  47607. xm = xc - radius,
  47608. ym = yc - radius,
  47609. xM = xc + radius,
  47610. yM = yc + radius;
  47611. if (xM>=0 && xm<_width && yM>=0 && ym<_height && _zc>zmin) {
  47612. visibles(l) = (unsigned int)l;
  47613. zrange(l) = _zc;
  47614. }
  47615. is_forward = false;
  47616. } break;
  47617. case 2 : case 6 : { // Segment
  47618. const unsigned int
  47619. i0 = (unsigned int)primitive(0),
  47620. i1 = (unsigned int)primitive(1);
  47621. const tpfloat
  47622. x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
  47623. x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2);
  47624. tpfloat xm, xM, ym, yM;
  47625. if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
  47626. if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
  47627. if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin) {
  47628. visibles(l) = (unsigned int)l;
  47629. zrange(l) = (z0 + z1)/2;
  47630. }
  47631. } break;
  47632. case 3 : case 9 : { // Triangle
  47633. const unsigned int
  47634. i0 = (unsigned int)primitive(0),
  47635. i1 = (unsigned int)primitive(1),
  47636. i2 = (unsigned int)primitive(2);
  47637. const tpfloat
  47638. x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
  47639. x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2),
  47640. x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2);
  47641. tpfloat xm, xM, ym, yM;
  47642. if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
  47643. if (x2<xm) xm = x2;
  47644. if (x2>xM) xM = x2;
  47645. if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
  47646. if (y2<ym) ym = y2;
  47647. if (y2>yM) yM = y2;
  47648. if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) {
  47649. const tpfloat d = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0);
  47650. if (is_double_sided || d<0) {
  47651. visibles(l) = (unsigned int)l;
  47652. zrange(l) = (z0 + z1 + z2)/3;
  47653. }
  47654. }
  47655. } break;
  47656. case 4 : case 12 : { // Quadrangle
  47657. const unsigned int
  47658. i0 = (unsigned int)primitive(0),
  47659. i1 = (unsigned int)primitive(1),
  47660. i2 = (unsigned int)primitive(2),
  47661. i3 = (unsigned int)primitive(3);
  47662. const tpfloat
  47663. x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
  47664. x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2),
  47665. x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2),
  47666. x3 = projections(i3,0), y3 = projections(i3,1), z3 = Z + vertices(i3,2);
  47667. tpfloat xm, xM, ym, yM;
  47668. if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
  47669. if (x2<xm) xm = x2;
  47670. if (x2>xM) xM = x2;
  47671. if (x3<xm) xm = x3;
  47672. if (x3>xM) xM = x3;
  47673. if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
  47674. if (y2<ym) ym = y2;
  47675. if (y2>yM) yM = y2;
  47676. if (y3<ym) ym = y3;
  47677. if (y3>yM) yM = y3;
  47678. if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin && z3>zmin) {
  47679. const float d = (x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0);
  47680. if (is_double_sided || d<0) {
  47681. visibles(l) = (unsigned int)l;
  47682. zrange(l) = (z0 + z1 + z2 + z3)/4;
  47683. }
  47684. }
  47685. } break;
  47686. default :
  47687. if (render_type==5) cimg::mutex(10,0);
  47688. throw CImgArgumentException(_cimg_instance
  47689. "draw_object3d(): Invalid primitive[%u] with size %u "
  47690. "(should have size 1,2,3,4,5,6,9 or 12).",
  47691. cimg_instance,
  47692. l,primitive.size());
  47693. }
  47694. }
  47695. // Force transparent primitives to be drawn last when zbuffer is activated
  47696. // (and if object contains no spheres or sprites).
  47697. if (is_forward)
  47698. cimglist_for(primitives,l)
  47699. if (___draw_object3d(opacities,l)!=1) zrange(l) = 2*zmax - zrange(l);
  47700. // Sort only visibles primitives.
  47701. unsigned int *p_visibles = visibles._data;
  47702. tpfloat *p_zrange = zrange._data;
  47703. const tpfloat *ptrz = p_zrange;
  47704. cimg_for(visibles,ptr,unsigned int) {
  47705. if (*ptr!=~0U) { *(p_visibles++) = *ptr; *(p_zrange++) = *ptrz; }
  47706. ++ptrz;
  47707. }
  47708. const unsigned int nb_visibles = (unsigned int)(p_zrange - zrange._data);
  47709. if (!nb_visibles) {
  47710. if (render_type==5) cimg::mutex(10,0);
  47711. return *this;
  47712. }
  47713. CImg<uintT> permutations;
  47714. CImg<tpfloat>(zrange._data,nb_visibles,1,1,1,true).sort(permutations,is_forward);
  47715. // Compute light properties
  47716. CImg<floatT> lightprops;
  47717. switch (render_type) {
  47718. case 3 : { // Flat Shading
  47719. lightprops.assign(nb_visibles);
  47720. cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
  47721. cimg_forX(lightprops,l) {
  47722. const CImg<tf>& primitive = primitives(visibles(permutations(l)));
  47723. const unsigned int psize = (unsigned int)primitive.size();
  47724. if (psize==3 || psize==4 || psize==9 || psize==12) {
  47725. const unsigned int
  47726. i0 = (unsigned int)primitive(0),
  47727. i1 = (unsigned int)primitive(1),
  47728. i2 = (unsigned int)primitive(2);
  47729. const tpfloat
  47730. x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2),
  47731. x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2),
  47732. x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2),
  47733. dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0,
  47734. dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0,
  47735. nx = dy1*dz2 - dz1*dy2,
  47736. ny = dz1*dx2 - dx1*dz2,
  47737. nz = dx1*dy2 - dy1*dx2,
  47738. norm = 1e-5f + cimg::hypot(nx,ny,nz),
  47739. lx = X + (x0 + x1 + x2)/3 - lightx,
  47740. ly = Y + (y0 + y1 + y2)/3 - lighty,
  47741. lz = Z + (z0 + z1 + z2)/3 - lightz,
  47742. nl = 1e-5f + cimg::hypot(lx,ly,lz),
  47743. factor = std::max(cimg::abs(-lx*nx - ly*ny - lz*nz)/(norm*nl),(tpfloat)0);
  47744. lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3);
  47745. } else lightprops[l] = 1;
  47746. }
  47747. } break;
  47748. case 4 : // Gouraud Shading
  47749. case 5 : { // Phong-Shading
  47750. CImg<tpfloat> vertices_normals(vertices._width,6,1,1,0);
  47751. cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
  47752. for (int l = 0; l<(int)nb_visibles; ++l) {
  47753. const CImg<tf>& primitive = primitives[visibles(l)];
  47754. const unsigned int psize = (unsigned int)primitive.size();
  47755. const bool
  47756. triangle_flag = (psize==3) || (psize==9),
  47757. quadrangle_flag = (psize==4) || (psize==12);
  47758. if (triangle_flag || quadrangle_flag) {
  47759. const unsigned int
  47760. i0 = (unsigned int)primitive(0),
  47761. i1 = (unsigned int)primitive(1),
  47762. i2 = (unsigned int)primitive(2),
  47763. i3 = quadrangle_flag?(unsigned int)primitive(3):0;
  47764. const tpfloat
  47765. x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2),
  47766. x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2),
  47767. x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2),
  47768. dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0,
  47769. dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0,
  47770. nnx = dy1*dz2 - dz1*dy2,
  47771. nny = dz1*dx2 - dx1*dz2,
  47772. nnz = dx1*dy2 - dy1*dx2,
  47773. norm = 1e-5f + cimg::hypot(nnx,nny,nnz),
  47774. nx = nnx/norm,
  47775. ny = nny/norm,
  47776. nz = nnz/norm;
  47777. unsigned int ix = 0, iy = 1, iz = 2;
  47778. if (is_double_sided && nz>0) { ix = 3; iy = 4; iz = 5; }
  47779. vertices_normals(i0,ix)+=nx; vertices_normals(i0,iy)+=ny; vertices_normals(i0,iz)+=nz;
  47780. vertices_normals(i1,ix)+=nx; vertices_normals(i1,iy)+=ny; vertices_normals(i1,iz)+=nz;
  47781. vertices_normals(i2,ix)+=nx; vertices_normals(i2,iy)+=ny; vertices_normals(i2,iz)+=nz;
  47782. if (quadrangle_flag) {
  47783. vertices_normals(i3,ix)+=nx; vertices_normals(i3,iy)+=ny; vertices_normals(i3,iz)+=nz;
  47784. }
  47785. }
  47786. }
  47787. if (is_double_sided) cimg_forX(vertices_normals,p) {
  47788. const float
  47789. nx0 = vertices_normals(p,0), ny0 = vertices_normals(p,1), nz0 = vertices_normals(p,2),
  47790. nx1 = vertices_normals(p,3), ny1 = vertices_normals(p,4), nz1 = vertices_normals(p,5),
  47791. n0 = nx0*nx0 + ny0*ny0 + nz0*nz0, n1 = nx1*nx1 + ny1*ny1 + nz1*nz1;
  47792. if (n1>n0) {
  47793. vertices_normals(p,0) = -nx1;
  47794. vertices_normals(p,1) = -ny1;
  47795. vertices_normals(p,2) = -nz1;
  47796. }
  47797. }
  47798. if (render_type==4) {
  47799. lightprops.assign(vertices._width);
  47800. cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
  47801. cimg_forX(lightprops,l) {
  47802. const tpfloat
  47803. nx = vertices_normals(l,0),
  47804. ny = vertices_normals(l,1),
  47805. nz = vertices_normals(l,2),
  47806. norm = 1e-5f + cimg::hypot(nx,ny,nz),
  47807. lx = X + vertices(l,0) - lightx,
  47808. ly = Y + vertices(l,1) - lighty,
  47809. lz = Z + vertices(l,2) - lightz,
  47810. nl = 1e-5f + cimg::hypot(lx,ly,lz),
  47811. factor = std::max((-lx*nx - ly*ny - lz*nz)/(norm*nl),(tpfloat)0);
  47812. lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3);
  47813. }
  47814. } else {
  47815. const unsigned int
  47816. lw2 = light_texture._width/2 - 1,
  47817. lh2 = light_texture._height/2 - 1;
  47818. lightprops.assign(vertices._width,2);
  47819. cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
  47820. cimg_forX(lightprops,l) {
  47821. const tpfloat
  47822. nx = vertices_normals(l,0),
  47823. ny = vertices_normals(l,1),
  47824. nz = vertices_normals(l,2),
  47825. norm = 1e-5f + cimg::hypot(nx,ny,nz),
  47826. nnx = nx/norm,
  47827. nny = ny/norm;
  47828. lightprops(l,0) = lw2*(1 + nnx);
  47829. lightprops(l,1) = lh2*(1 + nny);
  47830. }
  47831. }
  47832. } break;
  47833. }
  47834. // Draw visible primitives
  47835. const CImg<tc> default_color(1,_spectrum,1,1,(tc)200);
  47836. CImg<_to> _opacity;
  47837. for (unsigned int l = 0; l<nb_visibles; ++l) {
  47838. const unsigned int n_primitive = visibles(permutations(l));
  47839. const CImg<tf>& primitive = primitives[n_primitive];
  47840. const CImg<tc>
  47841. &__color = n_primitive<colors._width?colors[n_primitive]:CImg<tc>(),
  47842. _color = (__color && __color.size()!=_spectrum && __color._spectrum<_spectrum)?
  47843. __color.get_resize(-100,-100,-100,_spectrum,0):CImg<tc>(),
  47844. &color = _color?_color:(__color?__color:default_color);
  47845. const tc *const pcolor = color._data;
  47846. float opacity = __draw_object3d(opacities,n_primitive,_opacity);
  47847. if (_opacity.is_empty()) opacity*=g_opacity;
  47848. #ifdef cimg_use_board
  47849. LibBoard::Board &board = *(LibBoard::Board*)pboard;
  47850. #endif
  47851. switch (primitive.size()) {
  47852. case 1 : { // Colored point or sprite
  47853. const unsigned int n0 = (unsigned int)primitive[0];
  47854. const int x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1));
  47855. if (_opacity.is_empty()) { // Scalar opacity
  47856. if (color.size()==_spectrum) { // Colored point
  47857. draw_point(x0,y0,pcolor,opacity);
  47858. #ifdef cimg_use_board
  47859. if (pboard) {
  47860. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  47861. board.drawDot((float)x0,height()-(float)y0);
  47862. }
  47863. #endif
  47864. } else { // Sprite
  47865. const tpfloat z = Z + vertices(n0,2);
  47866. const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1);
  47867. const unsigned int
  47868. _sw = (unsigned int)(color._width*factor),
  47869. _sh = (unsigned int)(color._height*factor),
  47870. sw = _sw?_sw:1, sh = _sh?_sh:1;
  47871. const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2;
  47872. if (sw<=3*_width/2 && sh<=3*_height/2 &&
  47873. (nx0 + (int)sw/2>=0 || nx0 - (int)sw/2<width() || ny0 + (int)sh/2>=0 || ny0 - (int)sh/2<height())) {
  47874. const CImg<tc>
  47875. _sprite = (sw!=color._width || sh!=color._height)?
  47876. color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<tc>(),
  47877. &sprite = _sprite?_sprite:color;
  47878. draw_image(nx0,ny0,sprite,opacity);
  47879. #ifdef cimg_use_board
  47880. if (pboard) {
  47881. board.setPenColorRGBi(128,128,128);
  47882. board.setFillColor(LibBoard::Color::Null);
  47883. board.drawRectangle((float)nx0,height() - (float)ny0,sw,sh);
  47884. }
  47885. #endif
  47886. }
  47887. }
  47888. } else { // Opacity mask
  47889. const tpfloat z = Z + vertices(n0,2);
  47890. const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1);
  47891. const unsigned int
  47892. _sw = (unsigned int)(std::max(color._width,_opacity._width)*factor),
  47893. _sh = (unsigned int)(std::max(color._height,_opacity._height)*factor),
  47894. sw = _sw?_sw:1, sh = _sh?_sh:1;
  47895. const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2;
  47896. if (sw<=3*_width/2 && sh<=3*_height/2 &&
  47897. (nx0 + (int)sw/2>=0 || nx0 - (int)sw/2<width() || ny0 + (int)sh/2>=0 || ny0 - (int)sh/2<height())) {
  47898. const CImg<tc>
  47899. _sprite = (sw!=color._width || sh!=color._height)?
  47900. color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<tc>(),
  47901. &sprite = _sprite?_sprite:color;
  47902. const CImg<_to>
  47903. _nopacity = (sw!=_opacity._width || sh!=_opacity._height)?
  47904. _opacity.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<_to>(),
  47905. &nopacity = _nopacity?_nopacity:_opacity;
  47906. draw_image(nx0,ny0,sprite,nopacity,g_opacity);
  47907. #ifdef cimg_use_board
  47908. if (pboard) {
  47909. board.setPenColorRGBi(128,128,128);
  47910. board.setFillColor(LibBoard::Color::Null);
  47911. board.drawRectangle((float)nx0,height() - (float)ny0,sw,sh);
  47912. }
  47913. #endif
  47914. }
  47915. }
  47916. } break;
  47917. case 2 : { // Colored line
  47918. const unsigned int
  47919. n0 = (unsigned int)primitive[0],
  47920. n1 = (unsigned int)primitive[1];
  47921. const int
  47922. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  47923. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1));
  47924. const float
  47925. z0 = vertices(n0,2) + Z + _focale,
  47926. z1 = vertices(n1,2) + Z + _focale;
  47927. if (render_type) {
  47928. if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity);
  47929. else draw_line(x0,y0,x1,y1,pcolor,opacity);
  47930. #ifdef cimg_use_board
  47931. if (pboard) {
  47932. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  47933. board.drawLine((float)x0,height() - (float)y0,x1,height() - (float)y1);
  47934. }
  47935. #endif
  47936. } else {
  47937. draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity);
  47938. #ifdef cimg_use_board
  47939. if (pboard) {
  47940. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  47941. board.drawDot((float)x0,height() - (float)y0);
  47942. board.drawDot((float)x1,height() - (float)y1);
  47943. }
  47944. #endif
  47945. }
  47946. } break;
  47947. case 5 : { // Colored sphere
  47948. const unsigned int
  47949. n0 = (unsigned int)primitive[0],
  47950. n1 = (unsigned int)primitive[1],
  47951. is_wireframe = (unsigned int)primitive[2],
  47952. is_radius = (unsigned int)primitive[3];
  47953. float Xc,Yc,Zc,radius;
  47954. if (is_radius) {
  47955. Xc = (float)vertices(n0,0);
  47956. Yc = (float)vertices(n0,1);
  47957. Zc = (float)vertices(n0,2);
  47958. radius = cimg::hypot(vertices(n1,0) - vertices(n0,0),
  47959. vertices(n1,1) - vertices(n0,1),
  47960. vertices(n1,2) - vertices(n0,2));
  47961. } else {
  47962. Xc = 0.5f*((float)vertices(n0,0) + (float)vertices(n1,0));
  47963. Yc = 0.5f*((float)vertices(n0,1) + (float)vertices(n1,1));
  47964. Zc = 0.5f*((float)vertices(n0,2) + (float)vertices(n1,2));
  47965. radius = 0.5f*cimg::hypot(vertices(n1,0) - vertices(n0,0),
  47966. vertices(n1,1) - vertices(n0,1),
  47967. vertices(n1,2) - vertices(n0,2));
  47968. }
  47969. const float
  47970. zc = Z + Zc + _focale,
  47971. af = absfocale?absfocale/zc:1,
  47972. xc = X + Xc*af,
  47973. yc = Y + Yc*af;
  47974. radius*=af;
  47975. switch (render_type) {
  47976. case 0 :
  47977. draw_point((int)xc,(int)yc,pcolor,opacity);
  47978. #ifdef cimg_use_board
  47979. if (pboard) {
  47980. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  47981. board.drawDot(xc,height() - yc);
  47982. }
  47983. #endif
  47984. break;
  47985. case 1 :
  47986. draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U);
  47987. #ifdef cimg_use_board
  47988. if (pboard) {
  47989. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  47990. board.setFillColor(LibBoard::Color::Null);
  47991. board.drawCircle(xc,height() - yc,radius);
  47992. }
  47993. #endif
  47994. break;
  47995. default :
  47996. if (is_wireframe) draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U);
  47997. else draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity);
  47998. #ifdef cimg_use_board
  47999. if (pboard) {
  48000. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48001. if (!is_wireframe) board.fillCircle(xc,height() - yc,radius);
  48002. else {
  48003. board.setFillColor(LibBoard::Color::Null);
  48004. board.drawCircle(xc,height() - yc,radius);
  48005. }
  48006. }
  48007. #endif
  48008. break;
  48009. }
  48010. } break;
  48011. case 6 : { // Textured line
  48012. if (!__color) {
  48013. if (render_type==5) cimg::mutex(10,0);
  48014. throw CImgArgumentException(_cimg_instance
  48015. "draw_object3d(): Undefined texture for line primitive [%u].",
  48016. cimg_instance,n_primitive);
  48017. }
  48018. const unsigned int
  48019. n0 = (unsigned int)primitive[0],
  48020. n1 = (unsigned int)primitive[1];
  48021. const int
  48022. tx0 = (int)primitive[2], ty0 = (int)primitive[3],
  48023. tx1 = (int)primitive[4], ty1 = (int)primitive[5],
  48024. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  48025. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1));
  48026. const float
  48027. z0 = vertices(n0,2) + Z + _focale,
  48028. z1 = vertices(n1,2) + Z + _focale;
  48029. if (render_type) {
  48030. if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity);
  48031. else draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity);
  48032. #ifdef cimg_use_board
  48033. if (pboard) {
  48034. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48035. board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
  48036. }
  48037. #endif
  48038. } else {
  48039. draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
  48040. ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
  48041. draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
  48042. ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity);
  48043. #ifdef cimg_use_board
  48044. if (pboard) {
  48045. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48046. board.drawDot((float)x0,height() - (float)y0);
  48047. board.drawDot((float)x1,height() - (float)y1);
  48048. }
  48049. #endif
  48050. }
  48051. } break;
  48052. case 3 : { // Colored triangle
  48053. const unsigned int
  48054. n0 = (unsigned int)primitive[0],
  48055. n1 = (unsigned int)primitive[1],
  48056. n2 = (unsigned int)primitive[2];
  48057. const int
  48058. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  48059. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
  48060. x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1));
  48061. const float
  48062. z0 = vertices(n0,2) + Z + _focale,
  48063. z1 = vertices(n1,2) + Z + _focale,
  48064. z2 = vertices(n2,2) + Z + _focale;
  48065. switch (render_type) {
  48066. case 0 :
  48067. draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).draw_point(x2,y2,pcolor,opacity);
  48068. #ifdef cimg_use_board
  48069. if (pboard) {
  48070. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48071. board.drawDot((float)x0,height() - (float)y0);
  48072. board.drawDot((float)x1,height() - (float)y1);
  48073. board.drawDot((float)x2,height() - (float)y2);
  48074. }
  48075. #endif
  48076. break;
  48077. case 1 :
  48078. if (zbuffer)
  48079. draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x0,y0,z0,x2,y2,z2,pcolor,opacity).
  48080. draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity);
  48081. else
  48082. draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x0,y0,x2,y2,pcolor,opacity).
  48083. draw_line(x1,y1,x2,y2,pcolor,opacity);
  48084. #ifdef cimg_use_board
  48085. if (pboard) {
  48086. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48087. board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
  48088. board.drawLine((float)x0,height() - (float)y0,(float)x2,height() - (float)y2);
  48089. board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
  48090. }
  48091. #endif
  48092. break;
  48093. case 2 :
  48094. if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity);
  48095. else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity);
  48096. #ifdef cimg_use_board
  48097. if (pboard) {
  48098. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48099. board.fillTriangle((float)x0,height() - (float)y0,
  48100. (float)x1,height() - (float)y1,
  48101. (float)x2,height() - (float)y2);
  48102. }
  48103. #endif
  48104. break;
  48105. case 3 :
  48106. if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l));
  48107. else _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l));
  48108. #ifdef cimg_use_board
  48109. if (pboard) {
  48110. const float lp = std::min(lightprops(l),1.f);
  48111. board.setPenColorRGBi((unsigned char)(color[0]*lp),
  48112. (unsigned char)(color[1]*lp),
  48113. (unsigned char)(color[2]*lp),
  48114. (unsigned char)(opacity*255));
  48115. board.fillTriangle((float)x0,height() - (float)y0,
  48116. (float)x1,height() - (float)y1,
  48117. (float)x2,height() - (float)y2);
  48118. }
  48119. #endif
  48120. break;
  48121. case 4 :
  48122. if (zbuffer)
  48123. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,
  48124. lightprops(n0),lightprops(n1),lightprops(n2),opacity);
  48125. else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprops(n0),lightprops(n1),lightprops(n2),opacity);
  48126. #ifdef cimg_use_board
  48127. if (pboard) {
  48128. board.setPenColorRGBi((unsigned char)(color[0]),
  48129. (unsigned char)(color[1]),
  48130. (unsigned char)(color[2]),
  48131. (unsigned char)(opacity*255));
  48132. board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprops(n0),
  48133. (float)x1,height() - (float)y1,lightprops(n1),
  48134. (float)x2,height() - (float)y2,lightprops(n2));
  48135. }
  48136. #endif
  48137. break;
  48138. case 5 : {
  48139. const unsigned int
  48140. lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
  48141. lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
  48142. lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1));
  48143. if (zbuffer)
  48144. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  48145. else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
  48146. #ifdef cimg_use_board
  48147. if (pboard) {
  48148. const float
  48149. l0 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n0,0))),
  48150. (int)(light_texture.height()/2*(1 + lightprops(n0,1)))),
  48151. l1 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n1,0))),
  48152. (int)(light_texture.height()/2*(1 + lightprops(n1,1)))),
  48153. l2 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n2,0))),
  48154. (int)(light_texture.height()/2*(1 + lightprops(n2,1))));
  48155. board.setPenColorRGBi((unsigned char)(color[0]),
  48156. (unsigned char)(color[1]),
  48157. (unsigned char)(color[2]),
  48158. (unsigned char)(opacity*255));
  48159. board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
  48160. (float)x1,height() - (float)y1,l1,
  48161. (float)x2,height() - (float)y2,l2);
  48162. }
  48163. #endif
  48164. } break;
  48165. }
  48166. } break;
  48167. case 4 : { // Colored quadrangle
  48168. const unsigned int
  48169. n0 = (unsigned int)primitive[0],
  48170. n1 = (unsigned int)primitive[1],
  48171. n2 = (unsigned int)primitive[2],
  48172. n3 = (unsigned int)primitive[3];
  48173. const int
  48174. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  48175. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
  48176. x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1)),
  48177. x3 = cimg::uiround(projections(n3,0)), y3 = cimg::uiround(projections(n3,1)),
  48178. xc = (x0 + x1 + x2 + x3)/4, yc = (y0 + y1 + y2 + y3)/4;
  48179. const float
  48180. z0 = vertices(n0,2) + Z + _focale,
  48181. z1 = vertices(n1,2) + Z + _focale,
  48182. z2 = vertices(n2,2) + Z + _focale,
  48183. z3 = vertices(n3,2) + Z + _focale,
  48184. zc = (z0 + z1 + z2 + z3)/4;
  48185. switch (render_type) {
  48186. case 0 :
  48187. draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).
  48188. draw_point(x2,y2,pcolor,opacity).draw_point(x3,y3,pcolor,opacity);
  48189. #ifdef cimg_use_board
  48190. if (pboard) {
  48191. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48192. board.drawDot((float)x0,height() - (float)y0);
  48193. board.drawDot((float)x1,height() - (float)y1);
  48194. board.drawDot((float)x2,height() - (float)y2);
  48195. board.drawDot((float)x3,height() - (float)y3);
  48196. }
  48197. #endif
  48198. break;
  48199. case 1 :
  48200. if (zbuffer)
  48201. draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity).
  48202. draw_line(zbuffer,x2,y2,z2,x3,y3,z3,pcolor,opacity).draw_line(zbuffer,x3,y3,z3,x0,y0,z0,pcolor,opacity);
  48203. else
  48204. draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x1,y1,x2,y2,pcolor,opacity).
  48205. draw_line(x2,y2,x3,y3,pcolor,opacity).draw_line(x3,y3,x0,y0,pcolor,opacity);
  48206. #ifdef cimg_use_board
  48207. if (pboard) {
  48208. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48209. board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
  48210. board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
  48211. board.drawLine((float)x2,height() - (float)y2,(float)x3,height() - (float)y3);
  48212. board.drawLine((float)x3,height() - (float)y3,(float)x0,height() - (float)y0);
  48213. }
  48214. #endif
  48215. break;
  48216. case 2 :
  48217. if (zbuffer)
  48218. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity).
  48219. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity);
  48220. else
  48221. draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity).draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity);
  48222. #ifdef cimg_use_board
  48223. if (pboard) {
  48224. board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
  48225. board.fillTriangle((float)x0,height() - (float)y0,
  48226. (float)x1,height() - (float)y1,
  48227. (float)x2,height() - (float)y2);
  48228. board.fillTriangle((float)x0,height() - (float)y0,
  48229. (float)x2,height() - (float)y2,
  48230. (float)x3,height() - (float)y3);
  48231. }
  48232. #endif
  48233. break;
  48234. case 3 :
  48235. if (zbuffer)
  48236. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)).
  48237. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity,lightprops(l));
  48238. else
  48239. _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)).
  48240. _draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity,lightprops(l));
  48241. #ifdef cimg_use_board
  48242. if (pboard) {
  48243. const float lp = std::min(lightprops(l),1.f);
  48244. board.setPenColorRGBi((unsigned char)(color[0]*lp),
  48245. (unsigned char)(color[1]*lp),
  48246. (unsigned char)(color[2]*lp),(unsigned char)(opacity*255));
  48247. board.fillTriangle((float)x0,height() - (float)y0,
  48248. (float)x1,height() - (float)y1,
  48249. (float)x2,height() - (float)y2);
  48250. board.fillTriangle((float)x0,height() - (float)y0,
  48251. (float)x2,height() - (float)y2,
  48252. (float)x3,height() - (float)y3);
  48253. }
  48254. #endif
  48255. break;
  48256. case 4 : {
  48257. const float
  48258. lightprop0 = lightprops(n0), lightprop1 = lightprops(n1),
  48259. lightprop2 = lightprops(n2), lightprop3 = lightprops(n3),
  48260. lightpropc = (lightprop0 + lightprop1 + lightprop2 + lightprop2)/4;
  48261. if (zbuffer)
  48262. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,xc,yc,zc,pcolor,lightprop0,lightprop1,lightpropc,opacity).
  48263. draw_triangle(zbuffer,x1,y1,z1,x2,y2,z2,xc,yc,zc,pcolor,lightprop1,lightprop2,lightpropc,opacity).
  48264. draw_triangle(zbuffer,x2,y2,z2,x3,y3,z3,xc,yc,zc,pcolor,lightprop2,lightprop3,lightpropc,opacity).
  48265. draw_triangle(zbuffer,x3,y3,z3,x0,y0,z0,xc,yc,zc,pcolor,lightprop3,lightprop0,lightpropc,opacity);
  48266. else
  48267. draw_triangle(x0,y0,x1,y1,xc,yc,pcolor,lightprop0,lightprop1,lightpropc,opacity).
  48268. draw_triangle(x1,y1,x2,y2,xc,yc,pcolor,lightprop1,lightprop2,lightpropc,opacity).
  48269. draw_triangle(x2,y2,x3,y3,xc,yc,pcolor,lightprop2,lightprop3,lightpropc,opacity).
  48270. draw_triangle(x3,y3,x0,y0,xc,yc,pcolor,lightprop3,lightprop0,lightpropc,opacity);
  48271. #ifdef cimg_use_board
  48272. if (pboard) {
  48273. board.setPenColorRGBi((unsigned char)(color[0]),
  48274. (unsigned char)(color[1]),
  48275. (unsigned char)(color[2]),
  48276. (unsigned char)(opacity*255));
  48277. board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
  48278. (float)x1,height() - (float)y1,lightprop1,
  48279. (float)x2,height() - (float)y2,lightprop2);
  48280. board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
  48281. (float)x2,height() - (float)y2,lightprop2,
  48282. (float)x3,height() - (float)y3,lightprop3);
  48283. }
  48284. #endif
  48285. } break;
  48286. case 5 : {
  48287. const unsigned int
  48288. lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
  48289. lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
  48290. lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1)),
  48291. lx3 = (unsigned int)cimg::uiround(lightprops(n3,0)), ly3 = (unsigned int)cimg::uiround(lightprops(n3,1)),
  48292. lxc = (lx0 + lx1 + lx2 + lx3)/4, lyc = (ly0 + ly1 + ly2 + ly3)/4;
  48293. if (zbuffer)
  48294. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,xc,yc,zc,pcolor,light_texture,lx0,ly0,lx1,ly1,lxc,lyc,opacity).
  48295. draw_triangle(zbuffer,x1,y1,z1,x2,y2,z2,xc,yc,zc,pcolor,light_texture,lx1,ly1,lx2,ly2,lxc,lyc,opacity).
  48296. draw_triangle(zbuffer,x2,y2,z2,x3,y3,z3,xc,yc,zc,pcolor,light_texture,lx2,ly2,lx3,ly3,lxc,lyc,opacity).
  48297. draw_triangle(zbuffer,x3,y3,z3,x0,y0,z0,xc,yc,zc,pcolor,light_texture,lx3,ly3,lx0,ly0,lxc,lyc,opacity);
  48298. else
  48299. draw_triangle(x0,y0,x1,y1,xc,yc,pcolor,light_texture,lx0,ly0,lx1,ly1,lxc,lyc,opacity).
  48300. draw_triangle(x1,y1,x2,y2,xc,yc,pcolor,light_texture,lx1,ly1,lx2,ly2,lxc,lyc,opacity).
  48301. draw_triangle(x2,y2,x3,y3,xc,yc,pcolor,light_texture,lx2,ly2,lx3,ly3,lxc,lyc,opacity).
  48302. draw_triangle(x3,y3,x0,y0,xc,yc,pcolor,light_texture,lx3,ly3,lx0,ly0,lxc,lyc,opacity);
  48303. #ifdef cimg_use_board
  48304. if (pboard) {
  48305. const float
  48306. l0 = light_texture((int)(light_texture.width()/2*(1 + lx0)), (int)(light_texture.height()/2*(1 + ly0))),
  48307. l1 = light_texture((int)(light_texture.width()/2*(1 + lx1)), (int)(light_texture.height()/2*(1 + ly1))),
  48308. l2 = light_texture((int)(light_texture.width()/2*(1 + lx2)), (int)(light_texture.height()/2*(1 + ly2))),
  48309. l3 = light_texture((int)(light_texture.width()/2*(1 + lx3)), (int)(light_texture.height()/2*(1 + ly3)));
  48310. board.setPenColorRGBi((unsigned char)(color[0]),
  48311. (unsigned char)(color[1]),
  48312. (unsigned char)(color[2]),
  48313. (unsigned char)(opacity*255));
  48314. board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
  48315. (float)x1,height() - (float)y1,l1,
  48316. (float)x2,height() - (float)y2,l2);
  48317. board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
  48318. (float)x2,height() - (float)y2,l2,
  48319. (float)x3,height() - (float)y3,l3);
  48320. }
  48321. #endif
  48322. } break;
  48323. }
  48324. } break;
  48325. case 9 : { // Textured triangle
  48326. if (!__color) {
  48327. if (render_type==5) cimg::mutex(10,0);
  48328. throw CImgArgumentException(_cimg_instance
  48329. "draw_object3d(): Undefined texture for triangle primitive [%u].",
  48330. cimg_instance,n_primitive);
  48331. }
  48332. const unsigned int
  48333. n0 = (unsigned int)primitive[0],
  48334. n1 = (unsigned int)primitive[1],
  48335. n2 = (unsigned int)primitive[2];
  48336. const int
  48337. tx0 = (int)primitive[3], ty0 = (int)primitive[4],
  48338. tx1 = (int)primitive[5], ty1 = (int)primitive[6],
  48339. tx2 = (int)primitive[7], ty2 = (int)primitive[8],
  48340. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  48341. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
  48342. x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1));
  48343. const float
  48344. z0 = vertices(n0,2) + Z + _focale,
  48345. z1 = vertices(n1,2) + Z + _focale,
  48346. z2 = vertices(n2,2) + Z + _focale;
  48347. switch (render_type) {
  48348. case 0 :
  48349. draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
  48350. ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
  48351. draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
  48352. ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity).
  48353. draw_point(x2,y2,color.get_vector_at(tx2<=0?0:tx2>=color.width()?color.width() - 1:tx2,
  48354. ty2<=0?0:ty2>=color.height()?color.height() - 1:ty2)._data,opacity);
  48355. #ifdef cimg_use_board
  48356. if (pboard) {
  48357. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48358. board.drawDot((float)x0,height() - (float)y0);
  48359. board.drawDot((float)x1,height() - (float)y1);
  48360. board.drawDot((float)x2,height() - (float)y2);
  48361. }
  48362. #endif
  48363. break;
  48364. case 1 :
  48365. if (zbuffer)
  48366. draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
  48367. draw_line(zbuffer,x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity).
  48368. draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity);
  48369. else
  48370. draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
  48371. draw_line(x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity).
  48372. draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity);
  48373. #ifdef cimg_use_board
  48374. if (pboard) {
  48375. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48376. board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
  48377. board.drawLine((float)x0,height() - (float)y0,(float)x2,height() - (float)y2);
  48378. board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
  48379. }
  48380. #endif
  48381. break;
  48382. case 2 :
  48383. if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity);
  48384. else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity);
  48385. #ifdef cimg_use_board
  48386. if (pboard) {
  48387. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48388. board.fillTriangle((float)x0,height() - (float)y0,
  48389. (float)x1,height() - (float)y1,
  48390. (float)x2,height() - (float)y2);
  48391. }
  48392. #endif
  48393. break;
  48394. case 3 :
  48395. if (zbuffer)
  48396. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l));
  48397. else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l));
  48398. #ifdef cimg_use_board
  48399. if (pboard) {
  48400. const float lp = std::min(lightprops(l),1.f);
  48401. board.setPenColorRGBi((unsigned char)(128*lp),
  48402. (unsigned char)(128*lp),
  48403. (unsigned char)(128*lp),
  48404. (unsigned char)(opacity*255));
  48405. board.fillTriangle((float)x0,height() - (float)y0,
  48406. (float)x1,height() - (float)y1,
  48407. (float)x2,height() - (float)y2);
  48408. }
  48409. #endif
  48410. break;
  48411. case 4 :
  48412. if (zbuffer)
  48413. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48414. lightprops(n0),lightprops(n1),lightprops(n2),opacity);
  48415. else
  48416. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48417. lightprops(n0),lightprops(n1),lightprops(n2),opacity);
  48418. #ifdef cimg_use_board
  48419. if (pboard) {
  48420. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48421. board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprops(n0),
  48422. (float)x1,height() - (float)y1,lightprops(n1),
  48423. (float)x2,height() - (float)y2,lightprops(n2));
  48424. }
  48425. #endif
  48426. break;
  48427. case 5 :
  48428. if (zbuffer)
  48429. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture,
  48430. (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1),
  48431. (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1),
  48432. (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1),
  48433. opacity);
  48434. else
  48435. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture,
  48436. (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1),
  48437. (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1),
  48438. (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1),
  48439. opacity);
  48440. #ifdef cimg_use_board
  48441. if (pboard) {
  48442. const float
  48443. l0 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n0,0))),
  48444. (int)(light_texture.height()/2*(1 + lightprops(n0,1)))),
  48445. l1 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n1,0))),
  48446. (int)(light_texture.height()/2*(1 + lightprops(n1,1)))),
  48447. l2 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n2,0))),
  48448. (int)(light_texture.height()/2*(1 + lightprops(n2,1))));
  48449. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48450. board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
  48451. (float)x1,height() - (float)y1,l1,
  48452. (float)x2,height() - (float)y2,l2);
  48453. }
  48454. #endif
  48455. break;
  48456. }
  48457. } break;
  48458. case 12 : { // Textured quadrangle
  48459. if (!__color) {
  48460. if (render_type==5) cimg::mutex(10,0);
  48461. throw CImgArgumentException(_cimg_instance
  48462. "draw_object3d(): Undefined texture for quadrangle primitive [%u].",
  48463. cimg_instance,n_primitive);
  48464. }
  48465. const unsigned int
  48466. n0 = (unsigned int)primitive[0],
  48467. n1 = (unsigned int)primitive[1],
  48468. n2 = (unsigned int)primitive[2],
  48469. n3 = (unsigned int)primitive[3];
  48470. const int
  48471. tx0 = (int)primitive[4], ty0 = (int)primitive[5],
  48472. tx1 = (int)primitive[6], ty1 = (int)primitive[7],
  48473. tx2 = (int)primitive[8], ty2 = (int)primitive[9],
  48474. tx3 = (int)primitive[10], ty3 = (int)primitive[11],
  48475. x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
  48476. x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
  48477. x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1)),
  48478. x3 = cimg::uiround(projections(n3,0)), y3 = cimg::uiround(projections(n3,1));
  48479. const float
  48480. z0 = vertices(n0,2) + Z + _focale,
  48481. z1 = vertices(n1,2) + Z + _focale,
  48482. z2 = vertices(n2,2) + Z + _focale,
  48483. z3 = vertices(n3,2) + Z + _focale;
  48484. switch (render_type) {
  48485. case 0 :
  48486. draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
  48487. ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
  48488. draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
  48489. ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity).
  48490. draw_point(x2,y2,color.get_vector_at(tx2<=0?0:tx2>=color.width()?color.width() - 1:tx2,
  48491. ty2<=0?0:ty2>=color.height()?color.height() - 1:ty2)._data,opacity).
  48492. draw_point(x3,y3,color.get_vector_at(tx3<=0?0:tx3>=color.width()?color.width() - 1:tx3,
  48493. ty3<=0?0:ty3>=color.height()?color.height() - 1:ty3)._data,opacity);
  48494. #ifdef cimg_use_board
  48495. if (pboard) {
  48496. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48497. board.drawDot((float)x0,height() - (float)y0);
  48498. board.drawDot((float)x1,height() - (float)y1);
  48499. board.drawDot((float)x2,height() - (float)y2);
  48500. board.drawDot((float)x3,height() - (float)y3);
  48501. }
  48502. #endif
  48503. break;
  48504. case 1 :
  48505. if (zbuffer)
  48506. draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
  48507. draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity).
  48508. draw_line(zbuffer,x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity).
  48509. draw_line(zbuffer,x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity);
  48510. else
  48511. draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
  48512. draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity).
  48513. draw_line(x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity).
  48514. draw_line(x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity);
  48515. #ifdef cimg_use_board
  48516. if (pboard) {
  48517. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48518. board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
  48519. board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
  48520. board.drawLine((float)x2,height() - (float)y2,(float)x3,height() - (float)y3);
  48521. board.drawLine((float)x3,height() - (float)y3,(float)x0,height() - (float)y0);
  48522. }
  48523. #endif
  48524. break;
  48525. case 2 :
  48526. if (zbuffer)
  48527. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity).
  48528. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity);
  48529. else
  48530. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity).
  48531. draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity);
  48532. #ifdef cimg_use_board
  48533. if (pboard) {
  48534. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48535. board.fillTriangle((float)x0,height() - (float)y0,
  48536. (float)x1,height() - (float)y1,
  48537. (float)x2,height() - (float)y2);
  48538. board.fillTriangle((float)x0,height() - (float)y0,
  48539. (float)x2,height() - (float)y2,
  48540. (float)x3,height() - (float)y3);
  48541. }
  48542. #endif
  48543. break;
  48544. case 3 :
  48545. if (zbuffer)
  48546. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)).
  48547. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l));
  48548. else
  48549. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)).
  48550. draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l));
  48551. #ifdef cimg_use_board
  48552. if (pboard) {
  48553. const float lp = std::min(lightprops(l),1.f);
  48554. board.setPenColorRGBi((unsigned char)(128*lp),
  48555. (unsigned char)(128*lp),
  48556. (unsigned char)(128*lp),
  48557. (unsigned char)(opacity*255));
  48558. board.fillTriangle((float)x0,height() - (float)y0,
  48559. (float)x1,height() - (float)y1,
  48560. (float)x2,height() - (float)y2);
  48561. board.fillTriangle((float)x0,height() - (float)y0,
  48562. (float)x2,height() - (float)y2,
  48563. (float)x3,height() - (float)y3);
  48564. }
  48565. #endif
  48566. break;
  48567. case 4 : {
  48568. const float
  48569. lightprop0 = lightprops(n0), lightprop1 = lightprops(n1),
  48570. lightprop2 = lightprops(n2), lightprop3 = lightprops(n3);
  48571. if (zbuffer)
  48572. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48573. lightprop0,lightprop1,lightprop2,opacity).
  48574. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
  48575. lightprop0,lightprop2,lightprop3,opacity);
  48576. else
  48577. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48578. lightprop0,lightprop1,lightprop2,opacity).
  48579. draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
  48580. lightprop0,lightprop2,lightprop3,opacity);
  48581. #ifdef cimg_use_board
  48582. if (pboard) {
  48583. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48584. board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
  48585. (float)x1,height() - (float)y1,lightprop1,
  48586. (float)x2,height() - (float)y2,lightprop2);
  48587. board.fillGouraudTriangle((float)x0,height() -(float)y0,lightprop0,
  48588. (float)x2,height() - (float)y2,lightprop2,
  48589. (float)x3,height() - (float)y3,lightprop3);
  48590. }
  48591. #endif
  48592. } break;
  48593. case 5 : {
  48594. const unsigned int
  48595. lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
  48596. lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
  48597. lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1)),
  48598. lx3 = (unsigned int)cimg::uiround(lightprops(n3,0)), ly3 = (unsigned int)cimg::uiround(lightprops(n3,1));
  48599. if (zbuffer)
  48600. draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48601. light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity).
  48602. draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
  48603. light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity);
  48604. else
  48605. draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
  48606. light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity).
  48607. draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
  48608. light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity);
  48609. #ifdef cimg_use_board
  48610. if (pboard) {
  48611. const float
  48612. l0 = light_texture((int)(light_texture.width()/2*(1 + lx0)), (int)(light_texture.height()/2*(1 + ly0))),
  48613. l1 = light_texture((int)(light_texture.width()/2*(1 + lx1)), (int)(light_texture.height()/2*(1 + ly1))),
  48614. l2 = light_texture((int)(light_texture.width()/2*(1 + lx2)), (int)(light_texture.height()/2*(1 + ly2))),
  48615. l3 = light_texture((int)(light_texture.width()/2*(1 + lx3)), (int)(light_texture.height()/2*(1 + ly3)));
  48616. board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
  48617. board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
  48618. (float)x1,height() - (float)y1,l1,
  48619. (float)x2,height() - (float)y2,l2);
  48620. board.fillGouraudTriangle((float)x0,height() -(float)y0,l0,
  48621. (float)x2,height() - (float)y2,l2,
  48622. (float)x3,height() - (float)y3,l3);
  48623. }
  48624. #endif
  48625. } break;
  48626. }
  48627. } break;
  48628. }
  48629. }
  48630. if (render_type==5) cimg::mutex(10,0);
  48631. return *this;
  48632. }
  48633. //@}
  48634. //---------------------------
  48635. //
  48636. //! \name Data Input
  48637. //@{
  48638. //---------------------------
  48639. //! Launch simple interface to select a shape from an image.
  48640. /**
  48641. \param disp Display window to use.
  48642. \param feature_type Type of feature to select. Can be <tt>{ 0=point | 1=line | 2=rectangle | 3=ellipse }</tt>.
  48643. \param XYZ Pointer to 3 values X,Y,Z which tells about the projection point coordinates, for volumetric images.
  48644. \param exit_on_anykey Exit function when any key is pressed.
  48645. **/
  48646. CImg<T>& select(CImgDisplay &disp,
  48647. const unsigned int feature_type=2, unsigned int *const XYZ=0,
  48648. const bool exit_on_anykey=false,
  48649. const bool is_deep_selection_default=false) {
  48650. return get_select(disp,feature_type,XYZ,exit_on_anykey,is_deep_selection_default).move_to(*this);
  48651. }
  48652. //! Simple interface to select a shape from an image \overloading.
  48653. CImg<T>& select(const char *const title,
  48654. const unsigned int feature_type=2, unsigned int *const XYZ=0,
  48655. const bool exit_on_anykey=false,
  48656. const bool is_deep_selection_default=false) {
  48657. return get_select(title,feature_type,XYZ,exit_on_anykey,is_deep_selection_default).move_to(*this);
  48658. }
  48659. //! Simple interface to select a shape from an image \newinstance.
  48660. CImg<intT> get_select(CImgDisplay &disp,
  48661. const unsigned int feature_type=2, unsigned int *const XYZ=0,
  48662. const bool exit_on_anykey=false,
  48663. const bool is_deep_selection_default=false) const {
  48664. return _select(disp,0,feature_type,XYZ,0,0,0,exit_on_anykey,true,false,is_deep_selection_default);
  48665. }
  48666. //! Simple interface to select a shape from an image \newinstance.
  48667. CImg<intT> get_select(const char *const title,
  48668. const unsigned int feature_type=2, unsigned int *const XYZ=0,
  48669. const bool exit_on_anykey=false,
  48670. const bool is_deep_selection_default=false) const {
  48671. CImgDisplay disp;
  48672. return _select(disp,title,feature_type,XYZ,0,0,0,exit_on_anykey,true,false,is_deep_selection_default);
  48673. }
  48674. CImg<intT> _select(CImgDisplay &disp, const char *const title,
  48675. const unsigned int feature_type, unsigned int *const XYZ,
  48676. const int origX, const int origY, const int origZ,
  48677. const bool exit_on_anykey,
  48678. const bool reset_view3d,
  48679. const bool force_display_z_coord,
  48680. const bool is_deep_selection_default) const {
  48681. if (is_empty()) return CImg<intT>(1,feature_type==0?3:6,1,1,-1);
  48682. if (!disp) {
  48683. disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1);
  48684. if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum);
  48685. } else {
  48686. if (title) disp.set_title("%s",title);
  48687. disp.move_inside_screen();
  48688. }
  48689. CImg<T> thumb;
  48690. if (width()>disp.screen_width() || height()>disp.screen_height())
  48691. get_resize(cimg_fitscreen(width(),height(),depth()),depth(),-100).move_to(thumb);
  48692. const unsigned int old_normalization = disp.normalization();
  48693. bool old_is_resized = disp.is_resized();
  48694. disp._normalization = 0;
  48695. disp.show().set_key(0).set_wheel().show_mouse();
  48696. static const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 };
  48697. int area = 0, area_started = 0, area_clicked = 0, phase = 0,
  48698. X0 = (int)((XYZ?XYZ[0]:_width/2)%_width),
  48699. Y0 = (int)((XYZ?XYZ[1]:_height/2)%_height),
  48700. Z0 = (int)((XYZ?XYZ[2]:_depth/2)%_depth),
  48701. X1 =-1, Y1 = -1, Z1 = -1,
  48702. X3d = -1, Y3d = -1,
  48703. oX3d = X3d, oY3d = -1,
  48704. omx = -1, omy = -1;
  48705. float X = -1, Y = -1, Z = -1;
  48706. unsigned int key = 0, font_size = 32;
  48707. bool is_deep_selection = is_deep_selection_default,
  48708. shape_selected = false, text_down = false, visible_cursor = true;
  48709. static CImg<floatT> pose3d;
  48710. static bool is_view3d = false, is_axes = true;
  48711. if (reset_view3d) { pose3d.assign(); is_view3d = false; }
  48712. CImg<floatT> points3d, opacities3d, sel_opacities3d;
  48713. CImgList<uintT> primitives3d, sel_primitives3d;
  48714. CImgList<ucharT> colors3d, sel_colors3d;
  48715. CImg<ucharT> visu, visu0, view3d;
  48716. CImg<charT> text(1024); *text = 0;
  48717. while (!key && !disp.is_closed() && !shape_selected) {
  48718. // Handle mouse motion and selection
  48719. int
  48720. mx = disp.mouse_x(),
  48721. my = disp.mouse_y();
  48722. const float
  48723. mX = mx<0?-1.f:(float)mx*(width() + (depth()>1?depth():0))/disp.width(),
  48724. mY = my<0?-1.f:(float)my*(height() + (depth()>1?depth():0))/disp.height();
  48725. area = 0;
  48726. if (mX>=0 && mY>=0 && mX<width() && mY<height()) { area = 1; X = mX; Y = mY; Z = (float)(phase?Z1:Z0); }
  48727. if (mX>=0 && mX<width() && mY>=height()) { area = 2; X = mX; Z = mY - _height; Y = (float)(phase?Y1:Y0); }
  48728. if (mY>=0 && mX>=width() && mY<height()) { area = 3; Y = mY; Z = mX - _width; X = (float)(phase?X1:X0); }
  48729. if (mX>=width() && mY>=height()) area = 4;
  48730. if (disp.button()) { if (!area_clicked) area_clicked = area; } else area_clicked = 0;
  48731. CImg<charT> filename(32);
  48732. switch (key = disp.key()) {
  48733. #if cimg_OS!=2
  48734. case cimg::keyCTRLRIGHT :
  48735. #endif
  48736. case 0 : case cimg::keyCTRLLEFT : key = 0; break;
  48737. case cimg::keyPAGEUP :
  48738. if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(1); key = 0; } break;
  48739. case cimg::keyPAGEDOWN :
  48740. if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(-1); key = 0; } break;
  48741. case cimg::keyX : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48742. is_axes = !is_axes; disp.set_key(key,false); key = 0; visu0.assign();
  48743. } break;
  48744. case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48745. disp.set_fullscreen(false).
  48746. resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
  48747. CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
  48748. _is_resized = true;
  48749. disp.set_key(key,false); key = 0; visu0.assign();
  48750. } break;
  48751. case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48752. disp.set_fullscreen(false).
  48753. resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
  48754. disp.set_key(key,false); key = 0; visu0.assign();
  48755. } break;
  48756. case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48757. disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true;
  48758. disp.set_key(key,false); key = 0; visu0.assign();
  48759. } break;
  48760. case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48761. disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
  48762. disp.set_key(key,false); key = 0; visu0.assign();
  48763. } break;
  48764. case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48765. is_view3d = !is_view3d; disp.set_key(key,false); key = 0; visu0.assign();
  48766. } break;
  48767. case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48768. static unsigned int snap_number = 0;
  48769. std::FILE *file;
  48770. do {
  48771. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
  48772. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  48773. } while (file);
  48774. if (visu0) {
  48775. (+visu0).__draw_text(" Saving snapshot...",font_size,(int)text_down).display(disp);
  48776. visu0.save(filename);
  48777. (+visu0).__draw_text(" Snapshot '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
  48778. }
  48779. disp.set_key(key,false); key = 0;
  48780. } break;
  48781. case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  48782. static unsigned int snap_number = 0;
  48783. std::FILE *file;
  48784. do {
  48785. #ifdef cimg_use_zlib
  48786. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
  48787. #else
  48788. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
  48789. #endif
  48790. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  48791. } while (file);
  48792. (+visu0).__draw_text(" Saving instance... ",font_size,(int)text_down).display(disp);
  48793. save(filename);
  48794. (+visu0).__draw_text(" Instance '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
  48795. disp.set_key(key,false); key = 0;
  48796. } break;
  48797. }
  48798. switch (area) {
  48799. case 0 : // When mouse is out of image range
  48800. mx = my = -1; X = Y = Z = -1;
  48801. break;
  48802. case 1 : case 2 : case 3 : { // When mouse is over the XY,XZ or YZ projections
  48803. const unsigned int but = disp.button();
  48804. const bool b1 = (bool)(but&1), b2 = (bool)(but&2), b3 = (bool)(but&4);
  48805. if (b1 && phase==1 && area_clicked==area) { // When selection has been started (1st step)
  48806. if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign();
  48807. X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z;
  48808. }
  48809. if (!b1 && phase==2 && area_clicked!=area) { // When selection is at 2nd step (for volumes)
  48810. switch (area_started) {
  48811. case 1 : if (Z1!=(int)Z) visu0.assign(); Z1 = (int)Z; break;
  48812. case 2 : if (Y1!=(int)Y) visu0.assign(); Y1 = (int)Y; break;
  48813. case 3 : if (X1!=(int)X) visu0.assign(); X1 = (int)X; break;
  48814. }
  48815. }
  48816. if (b2 && area_clicked==area) { // When moving through the image/volume
  48817. if (phase) {
  48818. if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign();
  48819. X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z;
  48820. } else {
  48821. if (_depth>1 && (X0!=(int)X || Y0!=(int)Y || Z0!=(int)Z)) visu0.assign();
  48822. X0 = (int)X; Y0 = (int)Y; Z0 = (int)Z;
  48823. }
  48824. }
  48825. if (b3) { // Reset selection
  48826. X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = area = area_clicked = area_started = 0;
  48827. visu0.assign();
  48828. }
  48829. if (disp.wheel()) { // When moving through the slices of the volume (with mouse wheel)
  48830. if (_depth>1 && !disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT() &&
  48831. !disp.is_keySHIFTLEFT() && !disp.is_keySHIFTRIGHT()) {
  48832. switch (area) {
  48833. case 1 :
  48834. if (phase) Z = (float)(Z1+=disp.wheel()); else Z = (float)(Z0+=disp.wheel());
  48835. visu0.assign(); break;
  48836. case 2 :
  48837. if (phase) Y = (float)(Y1+=disp.wheel()); else Y = (float)(Y0+=disp.wheel());
  48838. visu0.assign(); break;
  48839. case 3 :
  48840. if (phase) X = (float)(X1+=disp.wheel()); else X = (float)(X0+=disp.wheel());
  48841. visu0.assign(); break;
  48842. }
  48843. disp.set_wheel();
  48844. } else key = ~0U;
  48845. }
  48846. if ((phase==0 && b1) ||
  48847. (phase==1 && !b1) ||
  48848. (phase==2 && b1)) switch (phase) { // Detect change of phase
  48849. case 0 :
  48850. if (area==area_clicked) {
  48851. X0 = X1 = (int)X; Y0 = Y1 = (int)Y; Z0 = Z1 = (int)Z; area_started = area; ++phase;
  48852. } break;
  48853. case 1 :
  48854. if (area==area_started) {
  48855. X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; ++phase;
  48856. if (_depth>1) {
  48857. if (disp.is_keyCTRLLEFT()) is_deep_selection = !is_deep_selection_default;
  48858. if (is_deep_selection) ++phase;
  48859. }
  48860. } else if (!b1) { X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = 0; visu0.assign(); }
  48861. break;
  48862. case 2 : ++phase; break;
  48863. }
  48864. } break;
  48865. case 4 : // When mouse is over the 3D view
  48866. if (is_view3d && points3d) {
  48867. X3d = mx - width()*disp.width()/(width() + (depth()>1?depth():0));
  48868. Y3d = my - height()*disp.height()/(height() + (depth()>1?depth():0));
  48869. if (oX3d<0) { oX3d = X3d; oY3d = Y3d; }
  48870. // Left + right buttons: reset.
  48871. if ((disp.button()&3)==3) { pose3d.assign(); view3d.assign(); oX3d = oY3d = X3d = Y3d = -1; }
  48872. else if (disp.button()&1 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Left button: rotate
  48873. const float
  48874. R = 0.45f*std::min(view3d._width,view3d._height),
  48875. R2 = R*R,
  48876. u0 = (float)(oX3d - view3d.width()/2),
  48877. v0 = (float)(oY3d - view3d.height()/2),
  48878. u1 = (float)(X3d - view3d.width()/2),
  48879. v1 = (float)(Y3d - view3d.height()/2),
  48880. n0 = cimg::hypot(u0,v0),
  48881. n1 = cimg::hypot(u1,v1),
  48882. nu0 = n0>R?(u0*R/n0):u0,
  48883. nv0 = n0>R?(v0*R/n0):v0,
  48884. nw0 = (float)std::sqrt(std::max(0.f,R2 - nu0*nu0 - nv0*nv0)),
  48885. nu1 = n1>R?(u1*R/n1):u1,
  48886. nv1 = n1>R?(v1*R/n1):v1,
  48887. nw1 = (float)std::sqrt(std::max(0.f,R2 - nu1*nu1 - nv1*nv1)),
  48888. u = nv0*nw1 - nw0*nv1,
  48889. v = nw0*nu1 - nu0*nw1,
  48890. w = nv0*nu1 - nu0*nv1,
  48891. n = cimg::hypot(u,v,w),
  48892. alpha = (float)std::asin(n/R2)*180/cimg::PI;
  48893. pose3d.draw_image(CImg<floatT>::rotation_matrix(u,v,w,-alpha)*pose3d.get_crop(0,0,2,2));
  48894. view3d.assign();
  48895. } else if (disp.button()&2 && pose3d && oY3d!=Y3d) { // Right button: zoom
  48896. pose3d(3,2)+=(Y3d - oY3d)*1.5f; view3d.assign();
  48897. }
  48898. if (disp.wheel()) { // Wheel: zoom
  48899. pose3d(3,2)-=disp.wheel()*15; view3d.assign(); disp.set_wheel();
  48900. }
  48901. if (disp.button()&4 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Middle button: shift
  48902. pose3d(3,0)-=oX3d - X3d; pose3d(3,1)-=oY3d - Y3d; view3d.assign();
  48903. }
  48904. oX3d = X3d; oY3d = Y3d;
  48905. }
  48906. mx = my = -1; X = Y = Z = -1;
  48907. break;
  48908. }
  48909. if (phase) {
  48910. if (!feature_type) shape_selected = phase?true:false;
  48911. else {
  48912. if (_depth>1) shape_selected = (phase==3)?true:false;
  48913. else shape_selected = (phase==2)?true:false;
  48914. }
  48915. }
  48916. if (X0<0) X0 = 0;
  48917. if (X0>=width()) X0 = width() - 1;
  48918. if (Y0<0) Y0 = 0;
  48919. if (Y0>=height()) Y0 = height() - 1;
  48920. if (Z0<0) Z0 = 0;
  48921. if (Z0>=depth()) Z0 = depth() - 1;
  48922. if (X1<1) X1 = 0;
  48923. if (X1>=width()) X1 = width() - 1;
  48924. if (Y1<0) Y1 = 0;
  48925. if (Y1>=height()) Y1 = height() - 1;
  48926. if (Z1<0) Z1 = 0;
  48927. if (Z1>=depth()) Z1 = depth() - 1;
  48928. // Draw visualization image on the display
  48929. if (mx!=omx || my!=omy || !visu0 || (_depth>1 && !view3d)) {
  48930. if (!visu0) { // Create image of projected planes
  48931. if (thumb) thumb._get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0);
  48932. else _get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0);
  48933. visu0.resize(disp);
  48934. view3d.assign();
  48935. points3d.assign();
  48936. }
  48937. if (is_view3d && _depth>1 && !view3d) { // Create 3D view for volumetric images
  48938. const unsigned int
  48939. _x3d = (unsigned int)cimg::round((float)_width*visu0._width/(_width + _depth),1,1),
  48940. _y3d = (unsigned int)cimg::round((float)_height*visu0._height/(_height + _depth),1,1),
  48941. x3d = _x3d>=visu0._width?visu0._width - 1:_x3d,
  48942. y3d = _y3d>=visu0._height?visu0._height - 1:_y3d;
  48943. CImg<ucharT>(1,2,1,1,64,128).resize(visu0._width - x3d,visu0._height - y3d,1,visu0._spectrum,3).
  48944. move_to(view3d);
  48945. if (!points3d) {
  48946. get_projections3d(primitives3d,colors3d,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0,true).move_to(points3d);
  48947. points3d.append(CImg<floatT>(8,3,1,1,
  48948. 0,_width - 1,_width - 1,0,0,_width - 1,_width - 1,0,
  48949. 0,0,_height - 1,_height - 1,0,0,_height - 1,_height - 1,
  48950. 0,0,0,0,_depth - 1,_depth - 1,_depth - 1,_depth - 1),'x');
  48951. CImg<uintT>::vector(12,13).move_to(primitives3d); CImg<uintT>::vector(13,14).move_to(primitives3d);
  48952. CImg<uintT>::vector(14,15).move_to(primitives3d); CImg<uintT>::vector(15,12).move_to(primitives3d);
  48953. CImg<uintT>::vector(16,17).move_to(primitives3d); CImg<uintT>::vector(17,18).move_to(primitives3d);
  48954. CImg<uintT>::vector(18,19).move_to(primitives3d); CImg<uintT>::vector(19,16).move_to(primitives3d);
  48955. CImg<uintT>::vector(12,16).move_to(primitives3d); CImg<uintT>::vector(13,17).move_to(primitives3d);
  48956. CImg<uintT>::vector(14,18).move_to(primitives3d); CImg<uintT>::vector(15,19).move_to(primitives3d);
  48957. colors3d.insert(12,CImg<ucharT>::vector(255,255,255));
  48958. opacities3d.assign(primitives3d.width(),1,1,1,0.5f);
  48959. if (!phase) {
  48960. opacities3d[0] = opacities3d[1] = opacities3d[2] = 0.8f;
  48961. sel_primitives3d.assign();
  48962. sel_colors3d.assign();
  48963. sel_opacities3d.assign();
  48964. } else {
  48965. if (feature_type==2) {
  48966. points3d.append(CImg<floatT>(8,3,1,1,
  48967. X0,X1,X1,X0,X0,X1,X1,X0,
  48968. Y0,Y0,Y1,Y1,Y0,Y0,Y1,Y1,
  48969. Z0,Z0,Z0,Z0,Z1,Z1,Z1,Z1),'x');
  48970. sel_primitives3d.assign();
  48971. CImg<uintT>::vector(20,21).move_to(sel_primitives3d);
  48972. CImg<uintT>::vector(21,22).move_to(sel_primitives3d);
  48973. CImg<uintT>::vector(22,23).move_to(sel_primitives3d);
  48974. CImg<uintT>::vector(23,20).move_to(sel_primitives3d);
  48975. CImg<uintT>::vector(24,25).move_to(sel_primitives3d);
  48976. CImg<uintT>::vector(25,26).move_to(sel_primitives3d);
  48977. CImg<uintT>::vector(26,27).move_to(sel_primitives3d);
  48978. CImg<uintT>::vector(27,24).move_to(sel_primitives3d);
  48979. CImg<uintT>::vector(20,24).move_to(sel_primitives3d);
  48980. CImg<uintT>::vector(21,25).move_to(sel_primitives3d);
  48981. CImg<uintT>::vector(22,26).move_to(sel_primitives3d);
  48982. CImg<uintT>::vector(23,27).move_to(sel_primitives3d);
  48983. } else {
  48984. points3d.append(CImg<floatT>(2,3,1,1,
  48985. X0,X1,
  48986. Y0,Y1,
  48987. Z0,Z1),'x');
  48988. sel_primitives3d.assign(CImg<uintT>::vector(20,21));
  48989. }
  48990. sel_colors3d.assign(sel_primitives3d._width,CImg<ucharT>::vector(255,255,255));
  48991. sel_opacities3d.assign(sel_primitives3d._width,1,1,1,0.8f);
  48992. }
  48993. points3d.shift_object3d(-0.5f*(_width - 1),-0.5f*(_height - 1),-0.5f*(_depth - 1)).resize_object3d();
  48994. points3d*=0.75f*std::min(view3d._width,view3d._height);
  48995. }
  48996. if (!pose3d) CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose3d);
  48997. CImg<floatT> zbuffer3d(view3d._width,view3d._height,1,1,0);
  48998. const CImg<floatT> rotated_points3d = pose3d.get_crop(0,0,2,2)*points3d;
  48999. if (sel_primitives3d)
  49000. view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width,
  49001. pose3d(3,1) + 0.5f*view3d._height,
  49002. pose3d(3,2),
  49003. rotated_points3d,sel_primitives3d,sel_colors3d,sel_opacities3d,
  49004. 2,true,500,0,0,0,0,0,1,zbuffer3d);
  49005. view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width,
  49006. pose3d(3,1) + 0.5f*view3d._height,
  49007. pose3d(3,2),
  49008. rotated_points3d,primitives3d,colors3d,opacities3d,
  49009. 2,true,500,0,0,0,0,0,1,zbuffer3d);
  49010. visu0.draw_image(x3d,y3d,view3d);
  49011. }
  49012. visu = visu0;
  49013. if (X<0 || Y<0 || Z<0) { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }}
  49014. else {
  49015. if (is_axes) { if (visible_cursor) { disp.hide_mouse(); visible_cursor = false; }}
  49016. else { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }}
  49017. const int d = (depth()>1)?depth():0;
  49018. int _vX = (int)X, _vY = (int)Y, _vZ = (int)Z;
  49019. if (phase>=2) { _vX = X1; _vY = Y1; _vZ = Z1; }
  49020. int
  49021. w = disp.width(), W = width() + d,
  49022. h = disp.height(), H = height() + d,
  49023. _xp = (int)(_vX*(float)w/W), xp = _xp + ((int)(_xp*(float)W/w)!=_vX),
  49024. _yp = (int)(_vY*(float)h/H), yp = _yp + ((int)(_yp*(float)H/h)!=_vY),
  49025. _xn = (int)((_vX + 1.f)*w/W - 1), xn = _xn + ((int)((_xn + 1.f)*W/w)!=_vX + 1),
  49026. _yn = (int)((_vY + 1.f)*h/H - 1), yn = _yn + ((int)((_yn + 1.f)*H/h)!=_vY + 1),
  49027. _zxp = (int)((_vZ + width())*(float)w/W), zxp = _zxp + ((int)(_zxp*(float)W/w)!=_vZ + width()),
  49028. _zyp = (int)((_vZ + height())*(float)h/H), zyp = _zyp + ((int)(_zyp*(float)H/h)!=_vZ + height()),
  49029. _zxn = (int)((_vZ + width() + 1.f)*w/W - 1),
  49030. zxn = _zxn + ((int)((_zxn + 1.f)*W/w)!=_vZ + width() + 1),
  49031. _zyn = (int)((_vZ + height() + 1.f)*h/H - 1),
  49032. zyn = _zyn + ((int)((_zyn + 1.f)*H/h)!=_vZ + height() + 1),
  49033. _xM = (int)(width()*(float)w/W - 1), xM = _xM + ((int)((_xM + 1.f)*W/w)!=width()),
  49034. _yM = (int)(height()*(float)h/H - 1), yM = _yM + ((int)((_yM + 1.f)*H/h)!=height()),
  49035. xc = (xp + xn)/2,
  49036. yc = (yp + yn)/2,
  49037. zxc = (zxp + zxn)/2,
  49038. zyc = (zyp + zyn)/2,
  49039. xf = (int)(X*w/W),
  49040. yf = (int)(Y*h/H),
  49041. zxf = (int)((Z + width())*w/W),
  49042. zyf = (int)((Z + height())*h/H);
  49043. if (is_axes) { // Draw axes
  49044. visu.draw_line(0,yf,visu.width() - 1,yf,foreground_color,0.7f,0xFF00FF00).
  49045. draw_line(0,yf,visu.width() - 1,yf,background_color,0.7f,0x00FF00FF).
  49046. draw_line(xf,0,xf,visu.height() - 1,foreground_color,0.7f,0xFF00FF00).
  49047. draw_line(xf,0,xf,visu.height() - 1,background_color,0.7f,0x00FF00FF);
  49048. if (_depth>1)
  49049. visu.draw_line(zxf,0,zxf,yM,foreground_color,0.7f,0xFF00FF00).
  49050. draw_line(zxf,0,zxf,yM,background_color,0.7f,0x00FF00FF).
  49051. draw_line(0,zyf,xM,zyf,foreground_color,0.7f,0xFF00FF00).
  49052. draw_line(0,zyf,xM,zyf,background_color,0.7f,0x00FF00FF);
  49053. }
  49054. // Draw box cursor.
  49055. if (xn - xp>=4 && yn - yp>=4)
  49056. visu.draw_rectangle(xp,yp,xn,yn,foreground_color,0.2f).
  49057. draw_rectangle(xp,yp,xn,yn,foreground_color,1,0xAAAAAAAA).
  49058. draw_rectangle(xp,yp,xn,yn,background_color,1,0x55555555);
  49059. if (_depth>1) {
  49060. if (yn - yp>=4 && zxn - zxp>=4)
  49061. visu.draw_rectangle(zxp,yp,zxn,yn,background_color,0.2f).
  49062. draw_rectangle(zxp,yp,zxn,yn,foreground_color,1,0xAAAAAAAA).
  49063. draw_rectangle(zxp,yp,zxn,yn,background_color,1,0x55555555);
  49064. if (xn - xp>=4 && zyn - zyp>=4)
  49065. visu.draw_rectangle(xp,zyp,xn,zyn,background_color,0.2f).
  49066. draw_rectangle(xp,zyp,xn,zyn,foreground_color,1,0xAAAAAAAA).
  49067. draw_rectangle(xp,zyp,xn,zyn,background_color,1,0x55555555);
  49068. }
  49069. // Draw selection.
  49070. if (phase && (phase!=1 || area_started==area)) {
  49071. const int
  49072. _xp0 = (int)(X0*(float)w/W), xp0 = _xp0 + ((int)(_xp0*(float)W/w)!=X0),
  49073. _yp0 = (int)(Y0*(float)h/H), yp0 = _yp0 + ((int)(_yp0*(float)H/h)!=Y0),
  49074. _xn0 = (int)((X0 + 1.f)*w/W - 1), xn0 = _xn0 + ((int)((_xn0 + 1.f)*W/w)!=X0 + 1),
  49075. _yn0 = (int)((Y0 + 1.f)*h/H - 1), yn0 = _yn0 + ((int)((_yn0 + 1.f)*H/h)!=Y0 + 1),
  49076. _zxp0 = (int)((Z0 + width())*(float)w/W), zxp0 = _zxp0 + ((int)(_zxp0*(float)W/w)!=Z0 + width()),
  49077. _zyp0 = (int)((Z0 + height())*(float)h/H), zyp0 = _zyp0 + ((int)(_zyp0*(float)H/h)!=Z0 + height()),
  49078. _zxn0 = (int)((Z0 + width() + 1.f)*w/W - 1),
  49079. zxn0 = _zxn0 + ((int)((_zxn0 + 1.f)*W/w)!=Z0 + width() + 1),
  49080. _zyn0 = (int)((Z0 + height() + 1.f)*h/H - 1),
  49081. zyn0 = _zyn0 + ((int)((_zyn0 + 1.f)*H/h)!=Z0 + height() + 1),
  49082. xc0 = (xp0 + xn0)/2,
  49083. yc0 = (yp0 + yn0)/2,
  49084. zxc0 = (zxp0 + zxn0)/2,
  49085. zyc0 = (zyp0 + zyn0)/2;
  49086. switch (feature_type) {
  49087. case 1 : { // Vector
  49088. visu.draw_arrow(xc0,yc0,xc,yc,background_color,0.9f,30,5,0x33333333).
  49089. draw_arrow(xc0,yc0,xc,yc,foreground_color,0.9f,30,5,0xCCCCCCCC);
  49090. if (d) {
  49091. visu.draw_arrow(zxc0,yc0,zxc,yc,background_color,0.9f,30,5,0x33333333).
  49092. draw_arrow(zxc0,yc0,zxc,yc,foreground_color,0.9f,30,5,0xCCCCCCCC).
  49093. draw_arrow(xc0,zyc0,xc,zyc,background_color,0.9f,30,5,0x33333333).
  49094. draw_arrow(xc0,zyc0,xc,zyc,foreground_color,0.9f,30,5,0xCCCCCCCC);
  49095. }
  49096. } break;
  49097. case 2 : { // Box
  49098. visu.draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,background_color,0.2f).
  49099. draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,background_color,0.9f,0x55555555).
  49100. draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,foreground_color,0.9f,0xAAAAAAAA);
  49101. if (xc0!=xc && yc0!=yc)
  49102. visu.draw_line(xc0,yc0,xc,yc,background_color,0.9f,0x33333333).
  49103. draw_line(xc0,yc0,xc,yc,foreground_color,0.9f,0xCCCCCCCC);
  49104. if (d) {
  49105. visu.draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,background_color,0.2f).
  49106. draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,
  49107. background_color,0.9f,0x55555555).
  49108. draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,
  49109. foreground_color,0.9f,0xAAAAAAAA);
  49110. if (zxc0!=zxc && yc0!=yc)
  49111. visu.draw_line(zxc0,yc0,zxc,yc,background_color,0.9f,0x33333333).
  49112. draw_line(zxc0,yc0,zxc,yc,foreground_color,0.9f,0xCCCCCCCC);
  49113. visu.draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
  49114. background_color,0.2f).
  49115. draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
  49116. background_color,0.9f,0x55555555).
  49117. draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
  49118. foreground_color,0.9f,0xAAAAAAAA);
  49119. if (xp0!=xn && zyp0!=zyn)
  49120. visu.draw_line(xp0,zyp0,xn,zyn,background_color,0.9f,0x33333333).
  49121. draw_line(xp0,zyp0,xn,zyn,foreground_color,0.9f,0xCCCCCCCC);
  49122. }
  49123. } break;
  49124. case 3 : { // Ellipse
  49125. visu.draw_ellipse(xc0,yc0,
  49126. (float)cimg::abs(xc - xc0),
  49127. (float)cimg::abs(yc - yc0),0,background_color,0.2f).
  49128. draw_ellipse(xc0,yc0,
  49129. (float)cimg::abs(xc - xc0),
  49130. (float)cimg::abs(yc - yc0),0,foreground_color,0.9f,~0U).
  49131. draw_point(xc0,yc0,foreground_color,0.9f);
  49132. if (d) {
  49133. visu.draw_ellipse(zxc0,yc0,(float)cimg::abs(zxc - zxc0),(float)cimg::abs(yc - yc0),0,
  49134. background_color,0.2f).
  49135. draw_ellipse(zxc0,yc0,(float)cimg::abs(zxc - zxc0),(float)cimg::abs(yc - yc0),0,
  49136. foreground_color,0.9f,~0U).
  49137. draw_point(zxc0,yc0,foreground_color,0.9f).
  49138. draw_ellipse(xc0,zyc0,(float)cimg::abs(xc - xc0),(float)cimg::abs(zyc - zyc0),0,
  49139. background_color,0.2f).
  49140. draw_ellipse(xc0,zyc0,(float)cimg::abs(xc - xc0),(float)cimg::abs(zyc - zyc0),0,
  49141. foreground_color,0.9f,~0U).
  49142. draw_point(xc0,zyc0,foreground_color,0.9f);
  49143. }
  49144. } break;
  49145. }
  49146. }
  49147. // Draw text info.
  49148. if (my>=0 && my<13) text_down = true; else if (my>=visu.height() - 13) text_down = false;
  49149. if (!feature_type || !phase) {
  49150. if (X>=0 && Y>=0 && Z>=0 && X<width() && Y<height() && Z<depth()) {
  49151. if (_depth>1 || force_display_z_coord)
  49152. cimg_snprintf(text,text._width," Point (%d,%d,%d) = [ ",origX + (int)X,origY + (int)Y,origZ + (int)Z);
  49153. else cimg_snprintf(text,text._width," Point (%d,%d) = [ ",origX + (int)X,origY + (int)Y);
  49154. CImg<T> values = get_vector_at((int)X,(int)Y,(int)Z);
  49155. const bool is_large_spectrum = values._height>8;
  49156. if (is_large_spectrum)
  49157. values.draw_image(0,4,values.get_rows(values._height - 4,values._height - 1)).resize(1,8,1,1,0);
  49158. char *ctext = text._data + std::strlen(text), *const ltext = text._data + 512;
  49159. for (unsigned int c = 0; c<values._height && ctext<ltext; ++c) {
  49160. cimg_snprintf(ctext,24,cimg::type<T>::format_s(),
  49161. cimg::type<T>::format(values[c]));
  49162. ctext += std::strlen(ctext);
  49163. if (c==3 && is_large_spectrum) {
  49164. cimg_snprintf(ctext,24," ...");
  49165. ctext += std::strlen(ctext);
  49166. }
  49167. *(ctext++) = ' '; *ctext = 0;
  49168. }
  49169. std::strcpy(text._data + std::strlen(text),"] ");
  49170. }
  49171. } else switch (feature_type) {
  49172. case 1 : {
  49173. const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1),
  49174. length = cimg::round(cimg::hypot(dX,dY,dZ),0.1);
  49175. if (_depth>1 || force_display_z_coord)
  49176. cimg_snprintf(text,text._width," Vect (%d,%d,%d)-(%d,%d,%d), Length = %g ",
  49177. origX + X0,origY + Y0,origZ + Z0,origX + X1,origY + Y1,origZ + Z1,length);
  49178. else if (_width!=1 && _height!=1)
  49179. cimg_snprintf(text,text._width," Vect (%d,%d)-(%d,%d), Length = %g, Angle = %g\260 ",
  49180. origX + X0,origY + Y0,origX + X1,origY + Y1,length,
  49181. cimg::round(cimg::mod(180*std::atan2(-dY,-dX)/cimg::PI,360.),0.1));
  49182. else
  49183. cimg_snprintf(text,text._width," Vect (%d,%d)-(%d,%d), Length = %g ",
  49184. origX + X0,origY + Y0,origX + X1,origY + Y1,length);
  49185. } break;
  49186. case 2 : {
  49187. const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1),
  49188. length = cimg::round(cimg::hypot(dX,dY,dZ),0.1);
  49189. if (_depth>1 || force_display_z_coord)
  49190. cimg_snprintf(text,text._width,
  49191. " Box ( %d,%d,%d ) - ( %d,%d,%d )\n Size = ( %d,%d,%d ), Length = %g ",
  49192. origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),origZ + (Z0<Z1?Z0:Z1),
  49193. origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),origZ + (Z0<Z1?Z1:Z0),
  49194. 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),1 + cimg::abs(Z0 - Z1),length);
  49195. else if (_width!=1 && _height!=1)
  49196. cimg_snprintf(text,text._width,
  49197. " Box ( %d,%d ) - ( %d,%d )\n Size = ( %d,%d ), Length = %g \n Angle = %g\260 ",
  49198. origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),
  49199. origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),
  49200. 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),length,
  49201. cimg::round(cimg::mod(180*std::atan2(-dY,-dX)/cimg::PI,360.),0.1));
  49202. else
  49203. cimg_snprintf(text,text._width,
  49204. " Box ( %d,%d ) - ( %d,%d )\n Size = (%d,%d), Length = %g ",
  49205. origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),
  49206. origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),
  49207. 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),length);
  49208. } break;
  49209. default :
  49210. if (_depth>1 || force_display_z_coord)
  49211. cimg_snprintf(text,text._width," Ellipse ( %d,%d,%d ) - ( %d,%d,%d ), Radii = ( %d,%d,%d ) ",
  49212. origX + X0,origY + Y0,origZ + Z0,origX + X1,origY + Y1,origZ + Z1,
  49213. 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),1 + cimg::abs(Z0 - Z1));
  49214. else cimg_snprintf(text,text._width," Ellipse ( %d,%d ) - ( %d,%d ), Radii = ( %d,%d ) ",
  49215. origX + X0,origY + Y0,origX + X1,origY + Y1,
  49216. 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1));
  49217. }
  49218. if (phase || (mx>=0 && my>=0)) visu.__draw_text("%s",font_size,(int)text_down,text._data);
  49219. }
  49220. disp.display(visu);
  49221. }
  49222. if (!shape_selected) disp.wait();
  49223. if (disp.is_resized()) { disp.resize(false)._is_resized = false; old_is_resized = true; visu0.assign(); }
  49224. omx = mx; omy = my;
  49225. if (!exit_on_anykey && key && key!=cimg::keyESC &&
  49226. (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  49227. key = 0;
  49228. }
  49229. }
  49230. // Return result.
  49231. CImg<intT> res(1,feature_type==0?3:6,1,1,-1);
  49232. if (XYZ) { XYZ[0] = (unsigned int)X0; XYZ[1] = (unsigned int)Y0; XYZ[2] = (unsigned int)Z0; }
  49233. if (shape_selected) {
  49234. if (feature_type==2) {
  49235. if (is_deep_selection) switch (area_started) {
  49236. case 1 : Z0 = 0; Z1 = _depth - 1; break;
  49237. case 2 : Y0 = 0; Y1 = _height - 1; break;
  49238. case 3 : X0 = 0; X1 = _width - 1; break;
  49239. }
  49240. if (X0>X1) cimg::swap(X0,X1);
  49241. if (Y0>Y1) cimg::swap(Y0,Y1);
  49242. if (Z0>Z1) cimg::swap(Z0,Z1);
  49243. }
  49244. if (X1<0 || Y1<0 || Z1<0) X0 = Y0 = Z0 = X1 = Y1 = Z1 = -1;
  49245. switch (feature_type) {
  49246. case 1 : case 2 : res[0] = X0; res[1] = Y0; res[2] = Z0; res[3] = X1; res[4] = Y1; res[5] = Z1; break;
  49247. case 3 :
  49248. res[3] = cimg::abs(X1 - X0); res[4] = cimg::abs(Y1 - Y0); res[5] = cimg::abs(Z1 - Z0);
  49249. res[0] = X0; res[1] = Y0; res[2] = Z0;
  49250. break;
  49251. default : res[0] = X0; res[1] = Y0; res[2] = Z0;
  49252. }
  49253. }
  49254. if (!exit_on_anykey || !(disp.button()&4)) disp.set_button();
  49255. if (!visible_cursor) disp.show_mouse();
  49256. disp._normalization = old_normalization;
  49257. disp._is_resized = old_is_resized;
  49258. if (key!=~0U) disp.set_key(key);
  49259. return res;
  49260. }
  49261. // Return a visualizable uchar8 image for display routines.
  49262. CImg<ucharT> _get_select(const CImgDisplay& disp, const int normalization,
  49263. const int x, const int y, const int z) const {
  49264. if (is_empty()) return CImg<ucharT>(1,1,1,1,0);
  49265. const CImg<T> crop = get_shared_channels(0,std::min(2,spectrum() - 1));
  49266. CImg<Tuchar> img2d;
  49267. if (_depth>1) {
  49268. const int mdisp = std::min(disp.screen_width(),disp.screen_height());
  49269. if (depth()>mdisp) {
  49270. crop.get_resize(-100,-100,mdisp,-100,0).move_to(img2d);
  49271. img2d.projections2d(x,y,z*img2d._depth/_depth);
  49272. } else crop.get_projections2d(x,y,z).move_to(img2d);
  49273. } else CImg<Tuchar>(crop,false).move_to(img2d);
  49274. // Check for inf and NaN values.
  49275. if (cimg::type<T>::is_float() && normalization) {
  49276. bool is_inf = false, is_nan = false;
  49277. cimg_for(img2d,ptr,Tuchar)
  49278. if (cimg::type<T>::is_inf(*ptr)) { is_inf = true; break; }
  49279. else if (cimg::type<T>::is_nan(*ptr)) { is_nan = true; break; }
  49280. if (is_inf || is_nan) {
  49281. Tint m0 = (Tint)cimg::type<T>::max(), M0 = (Tint)cimg::type<T>::min();
  49282. if (!normalization) { m0 = 0; M0 = 255; }
  49283. else if (normalization==2) { m0 = (Tint)disp._min; M0 = (Tint)disp._max; }
  49284. else {
  49285. cimg_for(img2d,ptr,Tuchar)
  49286. if (!cimg::type<T>::is_inf(*ptr) && !cimg::type<T>::is_nan(*ptr)) {
  49287. if (*ptr<(Tuchar)m0) m0 = *ptr;
  49288. if (*ptr>(Tuchar)M0) M0 = *ptr;
  49289. }
  49290. }
  49291. const T
  49292. val_minf = (T)(normalization==1 || normalization==3?m0 - cimg::abs(m0):m0),
  49293. val_pinf = (T)(normalization==1 || normalization==3?M0 + cimg::abs(M0):M0);
  49294. if (is_nan)
  49295. cimg_for(img2d,ptr,Tuchar)
  49296. if (cimg::type<T>::is_nan(*ptr)) *ptr = val_minf; // Replace NaN values
  49297. if (is_inf)
  49298. cimg_for(img2d,ptr,Tuchar)
  49299. if (cimg::type<T>::is_inf(*ptr)) *ptr = (float)*ptr<0?val_minf:val_pinf; // Replace +-inf values
  49300. }
  49301. }
  49302. switch (normalization) {
  49303. case 1 : img2d.normalize((ucharT)0,(ucharT)255); break;
  49304. case 2 : {
  49305. const float m = disp._min, M = disp._max;
  49306. (img2d-=m)*=255.f/(M - m>0?M - m:1);
  49307. } break;
  49308. case 3 :
  49309. if (cimg::type<T>::is_float()) img2d.normalize((ucharT)0,(ucharT)255);
  49310. else {
  49311. const float
  49312. m = (float)cimg::type<T>::min(),
  49313. M = (float)cimg::type<T>::max();
  49314. (img2d-=m)*=255.f/(M - m>0?M - m:1);
  49315. } break;
  49316. }
  49317. if (img2d.spectrum()==2) img2d.channels(0,2);
  49318. return img2d;
  49319. }
  49320. //! Select sub-graph in a graph.
  49321. CImg<intT> get_select_graph(CImgDisplay &disp,
  49322. const unsigned int plot_type=1, const unsigned int vertex_type=1,
  49323. const char *const labelx=0, const double xmin=0, const double xmax=0,
  49324. const char *const labely=0, const double ymin=0, const double ymax=0,
  49325. const bool exit_on_anykey=false) const {
  49326. if (is_empty())
  49327. throw CImgInstanceException(_cimg_instance
  49328. "select_graph(): Empty instance.",
  49329. cimg_instance);
  49330. if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
  49331. set_title("CImg<%s>",pixel_type());
  49332. const ulongT siz = (ulongT)_width*_height*_depth;
  49333. const unsigned int old_normalization = disp.normalization();
  49334. disp.show().set_button().set_wheel()._normalization = 0;
  49335. double nymin = ymin, nymax = ymax, nxmin = xmin, nxmax = xmax;
  49336. if (nymin==nymax) { nymin = (Tfloat)min_max(nymax); const double dy = nymax - nymin; nymin-=dy/20; nymax+=dy/20; }
  49337. if (nymin==nymax) { --nymin; ++nymax; }
  49338. if (nxmin==nxmax && nxmin==0) { nxmin = 0; nxmax = siz - 1.; }
  49339. static const unsigned char black[] = { 0, 0, 0 }, white[] = { 255, 255, 255 }, gray[] = { 220, 220, 220 };
  49340. static const unsigned char gray2[] = { 110, 110, 110 }, ngray[] = { 35, 35, 35 };
  49341. CImg<ucharT> colormap(3,_spectrum);
  49342. if (_spectrum==1) { colormap[0] = colormap[1] = 120; colormap[2] = 200; }
  49343. else {
  49344. colormap(0,0) = 220; colormap(1,0) = 10; colormap(2,0) = 10;
  49345. if (_spectrum>1) { colormap(0,1) = 10; colormap(1,1) = 220; colormap(2,1) = 10; }
  49346. if (_spectrum>2) { colormap(0,2) = 10; colormap(1,2) = 10; colormap(2,2) = 220; }
  49347. if (_spectrum>3) { colormap(0,3) = 220; colormap(1,3) = 220; colormap(2,3) = 10; }
  49348. if (_spectrum>4) { colormap(0,4) = 220; colormap(1,4) = 10; colormap(2,4) = 220; }
  49349. if (_spectrum>5) { colormap(0,5) = 10; colormap(1,5) = 220; colormap(2,5) = 220; }
  49350. if (_spectrum>6) {
  49351. cimg_uint64 rng = 10;
  49352. cimg_for_inY(colormap,6,colormap.height()-1,k) {
  49353. colormap(0,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
  49354. colormap(1,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
  49355. colormap(2,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
  49356. }
  49357. }
  49358. }
  49359. CImg<ucharT> visu0, visu, graph, text, axes;
  49360. int x0 = -1, x1 = -1, y0 = -1, y1 = -1, omouse_x = -2, omouse_y = -2;
  49361. const unsigned int one = plot_type==3?0U:1U;
  49362. unsigned int okey = 0, obutton = 0, font_size = 32;
  49363. CImg<charT> message(1024);
  49364. CImg_3x3(I,unsigned char);
  49365. for (bool selected = false; !selected && !disp.is_closed() && !okey && !disp.wheel(); ) {
  49366. const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y();
  49367. const unsigned int key = disp.key(), button = disp.button();
  49368. // Generate graph representation.
  49369. if (!visu0) {
  49370. visu0.assign(disp.width(),disp.height(),1,3,220);
  49371. const int gdimx = disp.width() - 32, gdimy = disp.height() - 32;
  49372. if (gdimx>0 && gdimy>0) {
  49373. graph.assign(gdimx,gdimy,1,3,255);
  49374. if (siz<32) {
  49375. if (siz>1) graph.draw_grid(gdimx/(float)(siz - one),gdimy/(float)(siz - one),0,0,
  49376. false,true,black,0.2f,0x33333333,0x33333333);
  49377. } else graph.draw_grid(-10,-10,0,0,false,true,black,0.2f,0x33333333,0x33333333);
  49378. cimg_forC(*this,c)
  49379. graph.draw_graph(get_shared_channel(c),&colormap(0,c),(plot_type!=3 || _spectrum==1)?1:0.6f,
  49380. plot_type,vertex_type,nymax,nymin);
  49381. axes.assign(gdimx,gdimy,1,1,0);
  49382. const float
  49383. dx = (float)cimg::abs(nxmax - nxmin), dy = (float)cimg::abs(nymax - nymin),
  49384. px = (float)std::pow(10.,(int)std::log10(dx?dx:1) - 2.),
  49385. py = (float)std::pow(10.,(int)std::log10(dy?dy:1) - 2.);
  49386. const CImg<Tdouble>
  49387. seqx = dx<=0?CImg<Tdouble>::vector(nxmin):
  49388. CImg<Tdouble>::sequence(1 + gdimx/60,nxmin,one?nxmax:nxmin + (nxmax - nxmin)*(siz + 1)/siz),
  49389. seqy = CImg<Tdouble>::sequence(1 + gdimy/60,nymax,nymin);
  49390. const bool allow_zero = (nxmin*nxmax>0) || (nymin*nymax>0);
  49391. axes.draw_axes(seqx,seqy,white,1,~0U,~0U,13,allow_zero,px,py);
  49392. if (nymin>0) axes.draw_axis(seqx,gdimy - 1,gray,1,~0U,13,allow_zero,px);
  49393. if (nymax<0) axes.draw_axis(seqx,0,gray,1,~0U,13,allow_zero,px);
  49394. if (nxmin>0) axes.draw_axis(0,seqy,gray,1,~0U,13,allow_zero,py);
  49395. if (nxmax<0) axes.draw_axis(gdimx - 1,seqy,gray,1,~0U,13,allow_zero,py);
  49396. cimg_for3x3(axes,x,y,0,0,I,unsigned char)
  49397. if (Icc) {
  49398. if (Icc==255) cimg_forC(graph,c) graph(x,y,c) = 0;
  49399. else cimg_forC(graph,c) graph(x,y,c) = (unsigned char)(2*graph(x,y,c)/3);
  49400. }
  49401. else if (Ipc || Inc || Icp || Icn || Ipp || Inn || Ipn || Inp)
  49402. cimg_forC(graph,c) graph(x,y,c) = (unsigned char)((graph(x,y,c) + 511)/3);
  49403. visu0.draw_image(16,16,graph);
  49404. visu0.draw_line(15,15,16 + gdimx,15,gray2).draw_line(16 + gdimx,15,16 + gdimx,16 + gdimy,gray2).
  49405. draw_line(16 + gdimx,16 + gdimy,15,16 + gdimy,white).draw_line(15,16 + gdimy,15,15,white);
  49406. } else graph.assign();
  49407. text.assign().draw_text(0,0,labelx?labelx:"X-axis",white,ngray,1,13).resize(-100,-100,1,3);
  49408. visu0.draw_image((visu0.width() - text.width())/2,visu0.height() - 14,~text);
  49409. text.assign().draw_text(0,0,labely?labely:"Y-axis",white,ngray,1,13).rotate(-90).resize(-100,-100,1,3);
  49410. visu0.draw_image(1,(visu0.height() - text.height())/2,~text);
  49411. visu.assign();
  49412. }
  49413. // Generate and display current view.
  49414. if (!visu) {
  49415. visu.assign(visu0);
  49416. if (graph && x0>=0 && x1>=0) {
  49417. const int
  49418. nx0 = x0<=x1?x0:x1,
  49419. nx1 = x0<=x1?x1:x0,
  49420. ny0 = y0<=y1?y0:y1,
  49421. ny1 = y0<=y1?y1:y0,
  49422. sx0 = (int)(16 + nx0*(visu.width() - 32)/std::max((ulongT)1,siz - one)),
  49423. sx1 = (int)(15 + (nx1 + 1)*(visu.width() - 32)/std::max((ulongT)1,siz - one)),
  49424. sy0 = 16 + ny0,
  49425. sy1 = 16 + ny1;
  49426. if (y0>=0 && y1>=0)
  49427. visu.draw_rectangle(sx0,sy0,sx1,sy1,gray,0.5f).draw_rectangle(sx0,sy0,sx1,sy1,black,0.5f,0xCCCCCCCCU);
  49428. else visu.draw_rectangle(sx0,0,sx1,visu.height() - 17,gray,0.5f).
  49429. draw_line(sx0,16,sx0,visu.height() - 17,black,0.5f,0xCCCCCCCCU).
  49430. draw_line(sx1,16,sx1,visu.height() - 17,black,0.5f,0xCCCCCCCCU);
  49431. }
  49432. if (mouse_x>=16 && mouse_y>=16 && mouse_x<visu.width() - 16 && mouse_y<visu.height() - 16) {
  49433. if (graph) visu.draw_line(mouse_x,16,mouse_x,visu.height() - 17,black,0.5f,0x55555555U);
  49434. const unsigned int
  49435. x = (unsigned int)cimg::round((mouse_x - 16.f)*(siz - one)/(disp.width() - 32),1,one?0:-1);
  49436. const double cx = nxmin + x*(nxmax - nxmin)/std::max((ulongT)1,siz - 1);
  49437. if (_spectrum>=7)
  49438. cimg_snprintf(message,message._width,"Value[%u:%g] = ( %g %g %g ... %g %g %g )",x,cx,
  49439. (double)(*this)(x,0,0,0),(double)(*this)(x,0,0,1),(double)(*this)(x,0,0,2),
  49440. (double)(*this)(x,0,0,_spectrum - 4),(double)(*this)(x,0,0,_spectrum - 3),
  49441. (double)(*this)(x,0,0,_spectrum - 1));
  49442. else {
  49443. cimg_snprintf(message,message._width,"Value[%u:%g] = ( ",x,cx);
  49444. cimg_forC(*this,c) cimg_sprintf(message._data + std::strlen(message),"%g ",(double)(*this)(x,0,0,c));
  49445. cimg_sprintf(message._data + std::strlen(message),")");
  49446. }
  49447. if (x0>=0 && x1>=0) {
  49448. const unsigned int
  49449. nx0 = (unsigned int)(x0<=x1?x0:x1),
  49450. nx1 = (unsigned int)(x0<=x1?x1:x0),
  49451. ny0 = (unsigned int)(y0<=y1?y0:y1),
  49452. ny1 = (unsigned int)(y0<=y1?y1:y0);
  49453. const double
  49454. cx0 = nxmin + nx0*(nxmax - nxmin)/std::max((ulongT)1,siz - 1),
  49455. cx1 = nxmin + (nx1 + one)*(nxmax - nxmin)/std::max((ulongT)1,siz - 1),
  49456. cy0 = nymax - ny0*(nymax - nymin)/(visu._height - 32),
  49457. cy1 = nymax - ny1*(nymax - nymin)/(visu._height - 32);
  49458. if (y0>=0 && y1>=0)
  49459. cimg_sprintf(message._data + std::strlen(message)," - Range ( %u:%g, %g ) - ( %u:%g, %g )",
  49460. x0,cx0,cy0,x1 + one,cx1,cy1);
  49461. else
  49462. cimg_sprintf(message._data + std::strlen(message)," - Range [ %u:%g - %u:%g ]",
  49463. x0,cx0,x1 + one,cx1);
  49464. }
  49465. text.assign().draw_text(0,0,message,white,ngray,1,13).resize(-100,-100,1,3);
  49466. visu.draw_image((visu.width() - text.width())/2,1,~text);
  49467. }
  49468. visu.display(disp);
  49469. }
  49470. // Test keys.
  49471. CImg<charT> filename(32);
  49472. switch (okey = key) {
  49473. #if cimg_OS!=2
  49474. case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT :
  49475. #endif
  49476. case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : okey = 0; break;
  49477. case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49478. disp.set_fullscreen(false).
  49479. resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
  49480. CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
  49481. _is_resized = true;
  49482. disp.set_key(key,false); okey = 0;
  49483. } break;
  49484. case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49485. disp.set_fullscreen(false).
  49486. resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
  49487. disp.set_key(key,false); okey = 0;
  49488. } break;
  49489. case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49490. disp.set_fullscreen(false).
  49491. resize(cimg_fitscreen(CImgDisplay::screen_width()/2,
  49492. CImgDisplay::screen_height()/2,1),false)._is_resized = true;
  49493. disp.set_key(key,false); okey = 0;
  49494. } break;
  49495. case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49496. disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
  49497. disp.set_key(key,false); okey = 0;
  49498. } break;
  49499. case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49500. static unsigned int snap_number = 0;
  49501. if (visu || visu0) {
  49502. CImg<ucharT> &screen = visu?visu:visu0;
  49503. std::FILE *file;
  49504. do {
  49505. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
  49506. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  49507. } while (file);
  49508. (+screen).__draw_text(" Saving snapshot... ",font_size,0).display(disp);
  49509. screen.save(filename);
  49510. (+screen).__draw_text(" Snapshot '%s' saved. ",font_size,0,filename._data).display(disp);
  49511. }
  49512. disp.set_key(key,false); okey = 0;
  49513. } break;
  49514. case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  49515. static unsigned int snap_number = 0;
  49516. if (visu || visu0) {
  49517. CImg<ucharT> &screen = visu?visu:visu0;
  49518. std::FILE *file;
  49519. do {
  49520. #ifdef cimg_use_zlib
  49521. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
  49522. #else
  49523. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
  49524. #endif
  49525. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  49526. } while (file);
  49527. (+screen).__draw_text(" Saving instance... ",font_size,0).display(disp);
  49528. save(filename);
  49529. (+screen).__draw_text(" Instance '%s' saved. ",font_size,0,filename._data).display(disp);
  49530. }
  49531. disp.set_key(key,false); okey = 0;
  49532. } break;
  49533. }
  49534. // Handle mouse motion and mouse buttons.
  49535. if (obutton!=button || omouse_x!=mouse_x || omouse_y!=mouse_y) {
  49536. visu.assign();
  49537. if (disp.mouse_x()>=0 && disp.mouse_y()>=0) {
  49538. const int
  49539. mx = (mouse_x - 16)*(int)(siz - one)/(disp.width() - 32),
  49540. cx = cimg::cut(mx,0,(int)(siz - 1 - one)),
  49541. my = mouse_y - 16,
  49542. cy = cimg::cut(my,0,disp.height() - 32);
  49543. if (button&1) {
  49544. if (!obutton) { x0 = cx; y0 = -1; } else { x1 = cx; y1 = -1; }
  49545. }
  49546. else if (button&2) {
  49547. if (!obutton) { x0 = cx; y0 = cy; } else { x1 = cx; y1 = cy; }
  49548. }
  49549. else if (obutton) { x1 = x1>=0?cx:-1; y1 = y1>=0?cy:-1; selected = true; }
  49550. } else if (!button && obutton) selected = true;
  49551. obutton = button; omouse_x = mouse_x; omouse_y = mouse_y;
  49552. }
  49553. if (disp.is_resized()) { disp.resize(false); visu0.assign(); }
  49554. if (visu && visu0) disp.wait();
  49555. if (!exit_on_anykey && okey && okey!=cimg::keyESC &&
  49556. (okey!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  49557. disp.set_key(key,false);
  49558. okey = 0;
  49559. }
  49560. }
  49561. disp._normalization = old_normalization;
  49562. if (x1>=0 && x1<x0) cimg::swap(x0,x1);
  49563. if (y1<y0) cimg::swap(y0,y1);
  49564. disp.set_key(okey);
  49565. return CImg<intT>(4,1,1,1,x0,y0,x1>=0?x1 + (int)one:-1,y1);
  49566. }
  49567. //! Load image from a file.
  49568. /**
  49569. \param filename Filename, as a C-string.
  49570. \note The extension of \c filename defines the file format. If no filename
  49571. extension is provided, CImg<T>::get_load() will try to load the file as a .cimg or .cimgz file.
  49572. **/
  49573. CImg<T>& load(const char *const filename) {
  49574. if (!filename)
  49575. throw CImgArgumentException(_cimg_instance
  49576. "load(): Specified filename is (null).",
  49577. cimg_instance);
  49578. if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) {
  49579. CImg<charT> filename_local(256);
  49580. load(cimg::load_network(filename,filename_local));
  49581. std::remove(filename_local);
  49582. return *this;
  49583. }
  49584. const char *const ext = cimg::split_filename(filename);
  49585. const unsigned int omode = cimg::exception_mode();
  49586. cimg::exception_mode(0);
  49587. bool is_loaded = true;
  49588. try {
  49589. #ifdef cimg_load_plugin
  49590. cimg_load_plugin(filename);
  49591. #endif
  49592. #ifdef cimg_load_plugin1
  49593. cimg_load_plugin1(filename);
  49594. #endif
  49595. #ifdef cimg_load_plugin2
  49596. cimg_load_plugin2(filename);
  49597. #endif
  49598. #ifdef cimg_load_plugin3
  49599. cimg_load_plugin3(filename);
  49600. #endif
  49601. #ifdef cimg_load_plugin4
  49602. cimg_load_plugin4(filename);
  49603. #endif
  49604. #ifdef cimg_load_plugin5
  49605. cimg_load_plugin5(filename);
  49606. #endif
  49607. #ifdef cimg_load_plugin6
  49608. cimg_load_plugin6(filename);
  49609. #endif
  49610. #ifdef cimg_load_plugin7
  49611. cimg_load_plugin7(filename);
  49612. #endif
  49613. #ifdef cimg_load_plugin8
  49614. cimg_load_plugin8(filename);
  49615. #endif
  49616. // Text formats
  49617. if (!cimg::strcasecmp(ext,"asc")) load_ascii(filename);
  49618. else if (!cimg::strcasecmp(ext,"csv") ||
  49619. !cimg::strcasecmp(ext,"dlm") ||
  49620. !cimg::strcasecmp(ext,"txt")) load_dlm(filename);
  49621. else if (!cimg::strcasecmp(ext,"pdf")) load_pdf_external(filename);
  49622. // 2D binary formats
  49623. else if (!cimg::strcasecmp(ext,"bmp")) load_bmp(filename);
  49624. else if (!cimg::strcasecmp(ext,"jpg") ||
  49625. !cimg::strcasecmp(ext,"jpeg") ||
  49626. !cimg::strcasecmp(ext,"jpe") ||
  49627. !cimg::strcasecmp(ext,"jfif") ||
  49628. !cimg::strcasecmp(ext,"jif")) load_jpeg(filename);
  49629. else if (!cimg::strcasecmp(ext,"png")) load_png(filename);
  49630. else if (!cimg::strcasecmp(ext,"ppm") ||
  49631. !cimg::strcasecmp(ext,"pgm") ||
  49632. !cimg::strcasecmp(ext,"pnm") ||
  49633. !cimg::strcasecmp(ext,"pbm") ||
  49634. !cimg::strcasecmp(ext,"pnk")) load_pnm(filename);
  49635. else if (!cimg::strcasecmp(ext,"pfm")) load_pfm(filename);
  49636. else if (!cimg::strcasecmp(ext,"tif") ||
  49637. !cimg::strcasecmp(ext,"tiff")) load_tiff(filename);
  49638. else if (!cimg::strcasecmp(ext,"exr")) load_exr(filename);
  49639. else if (!cimg::strcasecmp(ext,"arw") ||
  49640. !cimg::strcasecmp(ext,"cr2") ||
  49641. !cimg::strcasecmp(ext,"crw") ||
  49642. !cimg::strcasecmp(ext,"dcr") ||
  49643. !cimg::strcasecmp(ext,"dng") ||
  49644. !cimg::strcasecmp(ext,"mrw") ||
  49645. !cimg::strcasecmp(ext,"nef") ||
  49646. !cimg::strcasecmp(ext,"orf") ||
  49647. !cimg::strcasecmp(ext,"pix") ||
  49648. !cimg::strcasecmp(ext,"ptx") ||
  49649. !cimg::strcasecmp(ext,"raf") ||
  49650. !cimg::strcasecmp(ext,"srf")) load_dcraw_external(filename);
  49651. else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename);
  49652. else if (!cimg::strcasecmp(ext,"heic") ||
  49653. !cimg::strcasecmp(ext,"avif")) load_heif(filename);
  49654. // 3D binary formats
  49655. else if (!cimg::strcasecmp(ext,"dcm") ||
  49656. !cimg::strcasecmp(ext,"dicom")) load_medcon_external(filename);
  49657. else if (!cimg::strcasecmp(ext,"hdr") ||
  49658. !cimg::strcasecmp(ext,"nii")) load_analyze(filename);
  49659. else if (!cimg::strcasecmp(ext,"par") ||
  49660. !cimg::strcasecmp(ext,"rec")) load_parrec(filename);
  49661. else if (!cimg::strcasecmp(ext,"mnc")) load_minc2(filename);
  49662. else if (!cimg::strcasecmp(ext,"inr")) load_inr(filename);
  49663. else if (!cimg::strcasecmp(ext,"pan")) load_pandore(filename);
  49664. else if (!cimg::strcasecmp(ext,"cimg") ||
  49665. !cimg::strcasecmp(ext,"cimgz") ||
  49666. !*ext) return load_cimg(filename);
  49667. // Archive files
  49668. else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename);
  49669. // Image sequences
  49670. else if (!cimg::strcasecmp(ext,"avi") ||
  49671. !cimg::strcasecmp(ext,"mov") ||
  49672. !cimg::strcasecmp(ext,"asf") ||
  49673. !cimg::strcasecmp(ext,"divx") ||
  49674. !cimg::strcasecmp(ext,"flv") ||
  49675. !cimg::strcasecmp(ext,"mpg") ||
  49676. !cimg::strcasecmp(ext,"m1v") ||
  49677. !cimg::strcasecmp(ext,"m2v") ||
  49678. !cimg::strcasecmp(ext,"m4v") ||
  49679. !cimg::strcasecmp(ext,"mjp") ||
  49680. !cimg::strcasecmp(ext,"mp4") ||
  49681. !cimg::strcasecmp(ext,"mkv") ||
  49682. !cimg::strcasecmp(ext,"mpe") ||
  49683. !cimg::strcasecmp(ext,"movie") ||
  49684. !cimg::strcasecmp(ext,"ogm") ||
  49685. !cimg::strcasecmp(ext,"ogg") ||
  49686. !cimg::strcasecmp(ext,"ogv") ||
  49687. !cimg::strcasecmp(ext,"qt") ||
  49688. !cimg::strcasecmp(ext,"rm") ||
  49689. !cimg::strcasecmp(ext,"vob") ||
  49690. !cimg::strcasecmp(ext,"webm") ||
  49691. !cimg::strcasecmp(ext,"wmv") ||
  49692. !cimg::strcasecmp(ext,"xvid") ||
  49693. !cimg::strcasecmp(ext,"mpeg")) load_video(filename);
  49694. else is_loaded = false;
  49695. } catch (CImgIOException&) { is_loaded = false; }
  49696. // If nothing loaded, try to guess file format from magic number in file.
  49697. if (!is_loaded) {
  49698. std::FILE *file = cimg::std_fopen(filename,"rb");
  49699. if (!file) {
  49700. cimg::exception_mode(omode);
  49701. throw CImgIOException(_cimg_instance
  49702. "load(): Failed to open file '%s'.",
  49703. cimg_instance,
  49704. filename);
  49705. }
  49706. const char *const f_type = cimg::ftype(file,filename);
  49707. cimg::fclose(file);
  49708. is_loaded = true;
  49709. try {
  49710. if (!cimg::strcasecmp(f_type,"pnm")) load_pnm(filename);
  49711. else if (!cimg::strcasecmp(f_type,"pfm")) load_pfm(filename);
  49712. else if (!cimg::strcasecmp(f_type,"bmp")) load_bmp(filename);
  49713. else if (!cimg::strcasecmp(f_type,"inr")) load_inr(filename);
  49714. else if (!cimg::strcasecmp(f_type,"jpg")) load_jpeg(filename);
  49715. else if (!cimg::strcasecmp(f_type,"pan")) load_pandore(filename);
  49716. else if (!cimg::strcasecmp(f_type,"png")) load_png(filename);
  49717. else if (!cimg::strcasecmp(f_type,"tif")) load_tiff(filename);
  49718. else if (!cimg::strcasecmp(f_type,"gif")) load_gif_external(filename);
  49719. else if (!cimg::strcasecmp(f_type,"dcm")) load_medcon_external(filename);
  49720. else is_loaded = false;
  49721. } catch (CImgIOException&) { is_loaded = false; }
  49722. }
  49723. // If nothing loaded, try to load file with other means.
  49724. if (!is_loaded) {
  49725. try {
  49726. load_other(filename);
  49727. } catch (CImgIOException&) {
  49728. cimg::exception_mode(omode);
  49729. throw CImgIOException(_cimg_instance
  49730. "load(): Failed to recognize format of file '%s'.",
  49731. cimg_instance,
  49732. filename);
  49733. }
  49734. }
  49735. cimg::exception_mode(omode);
  49736. return *this;
  49737. }
  49738. //! Load image from a file \newinstance.
  49739. static CImg<T> get_load(const char *const filename) {
  49740. return CImg<T>().load(filename);
  49741. }
  49742. //! Load image from an ascii file.
  49743. /**
  49744. \param filename Filename, as a C -string.
  49745. **/
  49746. CImg<T>& load_ascii(const char *const filename) {
  49747. return _load_ascii(0,filename);
  49748. }
  49749. //! Load image from an ascii file \inplace.
  49750. static CImg<T> get_load_ascii(const char *const filename) {
  49751. return CImg<T>().load_ascii(filename);
  49752. }
  49753. //! Load image from an ascii file \overloading.
  49754. CImg<T>& load_ascii(std::FILE *const file) {
  49755. return _load_ascii(file,0);
  49756. }
  49757. //! Loadimage from an ascii file \newinstance.
  49758. static CImg<T> get_load_ascii(std::FILE *const file) {
  49759. return CImg<T>().load_ascii(file);
  49760. }
  49761. CImg<T>& _load_ascii(std::FILE *const file, const char *const filename) {
  49762. if (!file && !filename)
  49763. throw CImgArgumentException(_cimg_instance
  49764. "load_ascii(): Specified filename is (null).",
  49765. cimg_instance);
  49766. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  49767. CImg<charT> line(256); *line = 0;
  49768. int err = std::fscanf(nfile,"%255[^\n]",line._data);
  49769. unsigned int dx = 0, dy = 1, dz = 1, dc = 1;
  49770. cimg_sscanf(line,"%u%*c%u%*c%u%*c%u",&dx,&dy,&dz,&dc);
  49771. err = std::fscanf(nfile,"%*[^0-9.eEinfa+-]");
  49772. if (!dx || !dy || !dz || !dc) {
  49773. if (!file) cimg::fclose(nfile);
  49774. throw CImgIOException(_cimg_instance
  49775. "load_ascii(): Invalid ascii header in file '%s', image dimensions are set "
  49776. "to (%u,%u,%u,%u).",
  49777. cimg_instance,
  49778. filename?filename:"(FILE*)",dx,dy,dz,dc);
  49779. }
  49780. assign(dx,dy,dz,dc);
  49781. const ulongT siz = size();
  49782. ulongT off = 0;
  49783. double val;
  49784. T *ptr = _data;
  49785. for (err = 1, off = 0; off<siz && err==1; ++off) {
  49786. err = std::fscanf(nfile,"%lf%*[^0-9.eEinfa+-]",&val);
  49787. *(ptr++) = (T)val;
  49788. }
  49789. if (err!=1)
  49790. cimg::warn(_cimg_instance
  49791. "load_ascii(): Only %lu/%lu values read from file '%s'.",
  49792. cimg_instance,
  49793. off - 1,siz,filename?filename:"(FILE*)");
  49794. if (!file) cimg::fclose(nfile);
  49795. return *this;
  49796. }
  49797. //! Load image from a DLM file.
  49798. /**
  49799. \param filename Filename, as a C-string.
  49800. **/
  49801. CImg<T>& load_dlm(const char *const filename) {
  49802. return _load_dlm(0,filename);
  49803. }
  49804. //! Load image from a DLM file \newinstance.
  49805. static CImg<T> get_load_dlm(const char *const filename) {
  49806. return CImg<T>().load_dlm(filename);
  49807. }
  49808. //! Load image from a DLM file \overloading.
  49809. CImg<T>& load_dlm(std::FILE *const file) {
  49810. return _load_dlm(file,0);
  49811. }
  49812. //! Load image from a DLM file \newinstance.
  49813. static CImg<T> get_load_dlm(std::FILE *const file) {
  49814. return CImg<T>().load_dlm(file);
  49815. }
  49816. CImg<T>& _load_dlm(std::FILE *const file, const char *const filename) {
  49817. if (!file && !filename)
  49818. throw CImgArgumentException(_cimg_instance
  49819. "load_dlm(): Specified filename is (null).",
  49820. cimg_instance);
  49821. std::FILE *const nfile = file?file:cimg::fopen(filename,"r");
  49822. CImg<charT> delimiter(256), tmp(256); *delimiter = *tmp = 0;
  49823. unsigned int cdx = 0, dx = 0, dy = 0;
  49824. int err = 0;
  49825. double val;
  49826. assign(256,256,1,1,(T)0);
  49827. while ((err = std::fscanf(nfile,"%lf%255[^0-9eEinfa.+-]",&val,delimiter._data))>0) {
  49828. if (err>0) (*this)(cdx++,dy) = (T)val;
  49829. if (cdx>=_width) resize(3*_width/2,_height,1,1,0);
  49830. char c = 0;
  49831. if (!cimg_sscanf(delimiter,"%255[^\n]%c",tmp._data,&c) || c=='\n') {
  49832. dx = std::max(cdx,dx);
  49833. if (++dy>=_height) resize(_width,3*_height/2,1,1,0);
  49834. cdx = 0;
  49835. }
  49836. }
  49837. if (cdx && err==1) { dx = cdx; ++dy; }
  49838. if (!dx || !dy) {
  49839. if (!file) cimg::fclose(nfile);
  49840. throw CImgIOException(_cimg_instance
  49841. "load_dlm(): Invalid DLM file '%s'.",
  49842. cimg_instance,
  49843. filename?filename:"(FILE*)");
  49844. }
  49845. resize(dx,dy,1,1,0);
  49846. if (!file) cimg::fclose(nfile);
  49847. return *this;
  49848. }
  49849. //! Load image from a BMP file.
  49850. /**
  49851. \param filename Filename, as a C-string.
  49852. **/
  49853. CImg<T>& load_bmp(const char *const filename) {
  49854. return _load_bmp(0,filename);
  49855. }
  49856. //! Load image from a BMP file \newinstance.
  49857. static CImg<T> get_load_bmp(const char *const filename) {
  49858. return CImg<T>().load_bmp(filename);
  49859. }
  49860. //! Load image from a BMP file \overloading.
  49861. CImg<T>& load_bmp(std::FILE *const file) {
  49862. return _load_bmp(file,0);
  49863. }
  49864. //! Load image from a BMP file \newinstance.
  49865. static CImg<T> get_load_bmp(std::FILE *const file) {
  49866. return CImg<T>().load_bmp(file);
  49867. }
  49868. CImg<T>& _load_bmp(std::FILE *const file, const char *const filename) {
  49869. if (!file && !filename)
  49870. throw CImgArgumentException(_cimg_instance
  49871. "load_bmp(): Specified filename is (null).",
  49872. cimg_instance);
  49873. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  49874. CImg<ucharT> header(54);
  49875. cimg::fread(header._data,54,nfile);
  49876. if (*header!='B' || header[1]!='M') {
  49877. if (!file) cimg::fclose(nfile);
  49878. throw CImgIOException(_cimg_instance
  49879. "load_bmp(): Invalid BMP file '%s'.",
  49880. cimg_instance,
  49881. filename?filename:"(FILE*)");
  49882. }
  49883. // Read header and pixel buffer
  49884. int
  49885. file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),
  49886. offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24),
  49887. header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24),
  49888. dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24),
  49889. dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24),
  49890. compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24),
  49891. nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),
  49892. bpp = header[0x1C] + (header[0x1D]<<8);
  49893. if (!file_size || file_size==offset) {
  49894. cimg::fseek(nfile,0,SEEK_END);
  49895. file_size = (int)cimg::ftell(nfile);
  49896. cimg::fseek(nfile,54,SEEK_SET);
  49897. }
  49898. if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);
  49899. const int
  49900. dx_bytes = (bpp==1)?(dx/8 + (dx%8?1:0)):((bpp==4)?(dx/2 + (dx%2)):(int)((longT)dx*bpp/8)),
  49901. align_bytes = (4 - dx_bytes%4)%4;
  49902. const ulongT
  49903. cimg_iobuffer = (ulongT)24*1024*1024,
  49904. buf_size = (ulongT)cimg::abs(dy)*(dx_bytes + align_bytes);
  49905. CImg<intT> colormap;
  49906. if (bpp<16) { if (!nb_colors) nb_colors = 1<<bpp; } else nb_colors = 0;
  49907. if (nb_colors) { colormap.assign(nb_colors); cimg::fread(colormap._data,nb_colors,nfile); }
  49908. const int xoffset = offset - 14 - header_size - 4*nb_colors;
  49909. if (xoffset>0) cimg::fseek(nfile,xoffset,SEEK_CUR);
  49910. CImg<ucharT> buffer;
  49911. if (buf_size<cimg_iobuffer) {
  49912. buffer.assign(buf_size,1,1,1,0);
  49913. cimg::fread(buffer._data,buf_size,nfile);
  49914. } else buffer.assign(dx_bytes + align_bytes);
  49915. unsigned char *ptrs = buffer;
  49916. // Decompress buffer (if necessary)
  49917. if (compression==1 || compression==2) {
  49918. if (file)
  49919. throw CImgIOException(_cimg_instance
  49920. "load_bmp(): Unable to load compressed data from '(*FILE)' inputs.",
  49921. cimg_instance);
  49922. else {
  49923. if (!file) cimg::fclose(nfile);
  49924. return load_other(filename);
  49925. }
  49926. }
  49927. // Read pixel data
  49928. assign(dx,cimg::abs(dy),1,3,0);
  49929. switch (bpp) {
  49930. case 1 : { // Monochrome
  49931. if (colormap._width>=2) for (int y = height() - 1; y>=0; --y) {
  49932. if (buf_size>=cimg_iobuffer) {
  49933. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  49934. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  49935. }
  49936. unsigned char mask = 0x80, val = 0;
  49937. cimg_forX(*this,x) {
  49938. if (mask==0x80) val = *(ptrs++);
  49939. const unsigned char *col = (unsigned char*)(colormap._data + (val&mask?1:0));
  49940. (*this)(x,y,2) = (T)*(col++);
  49941. (*this)(x,y,1) = (T)*(col++);
  49942. (*this)(x,y,0) = (T)*(col++);
  49943. mask = cimg::ror(mask);
  49944. }
  49945. ptrs+=align_bytes;
  49946. }
  49947. } break;
  49948. case 4 : { // 16 colors
  49949. if (colormap._width>=16) for (int y = height() - 1; y>=0; --y) {
  49950. if (buf_size>=cimg_iobuffer) {
  49951. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  49952. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  49953. }
  49954. unsigned char mask = 0xF0, val = 0;
  49955. cimg_forX(*this,x) {
  49956. if (mask==0xF0) val = *(ptrs++);
  49957. const unsigned char color = (unsigned char)((mask<16)?(val&mask):((val&mask)>>4));
  49958. const unsigned char *col = (unsigned char*)(colormap._data + color);
  49959. (*this)(x,y,2) = (T)*(col++);
  49960. (*this)(x,y,1) = (T)*(col++);
  49961. (*this)(x,y,0) = (T)*(col++);
  49962. mask = cimg::ror(mask,4);
  49963. }
  49964. ptrs+=align_bytes;
  49965. }
  49966. } break;
  49967. case 8 : { // 256 colors
  49968. if (colormap._width>=256) for (int y = height() - 1; y>=0; --y) {
  49969. if (buf_size>=cimg_iobuffer) {
  49970. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  49971. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  49972. }
  49973. cimg_forX(*this,x) {
  49974. const unsigned char *col = (unsigned char*)(colormap._data + *(ptrs++));
  49975. (*this)(x,y,2) = (T)*(col++);
  49976. (*this)(x,y,1) = (T)*(col++);
  49977. (*this)(x,y,0) = (T)*(col++);
  49978. }
  49979. ptrs+=align_bytes;
  49980. }
  49981. } break;
  49982. case 16 : { // 16 bits colors (RGB565)
  49983. for (int y = height() - 1; y>=0; --y) {
  49984. if (buf_size>=cimg_iobuffer) {
  49985. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  49986. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  49987. }
  49988. cimg_forX(*this,x) {
  49989. const unsigned char c1 = *(ptrs++), c2 = *(ptrs++);
  49990. const unsigned short col = (unsigned short)c2<<8 | c1;
  49991. (*this)(x,y,2) = (T)((col&0x1F)<<3);
  49992. (*this)(x,y,1) = (T)(((col>>5)&0x3F)<<3);
  49993. (*this)(x,y,0) = (T)(((col>>11)&0x1F)<<3);
  49994. }
  49995. ptrs+=align_bytes;
  49996. }
  49997. } break;
  49998. case 24 : { // 24 bits colors
  49999. for (int y = height() - 1; y>=0; --y) {
  50000. if (buf_size>=cimg_iobuffer) {
  50001. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  50002. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  50003. }
  50004. cimg_forX(*this,x) {
  50005. (*this)(x,y,2) = (T)*(ptrs++);
  50006. (*this)(x,y,1) = (T)*(ptrs++);
  50007. (*this)(x,y,0) = (T)*(ptrs++);
  50008. }
  50009. ptrs+=align_bytes;
  50010. }
  50011. } break;
  50012. case 32 : { // 32 bits colors
  50013. for (int y = height() - 1; y>=0; --y) {
  50014. if (buf_size>=cimg_iobuffer) {
  50015. if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
  50016. cimg::fseek(nfile,align_bytes,SEEK_CUR);
  50017. }
  50018. cimg_forX(*this,x) {
  50019. (*this)(x,y,2) = (T)*(ptrs++);
  50020. (*this)(x,y,1) = (T)*(ptrs++);
  50021. (*this)(x,y,0) = (T)*(ptrs++);
  50022. ++ptrs;
  50023. }
  50024. ptrs+=align_bytes;
  50025. }
  50026. } break;
  50027. }
  50028. if (dy<0) mirror('y');
  50029. if (!file) cimg::fclose(nfile);
  50030. return *this;
  50031. }
  50032. //! Load image from a JPEG file.
  50033. /**
  50034. \param filename Filename, as a C-string.
  50035. **/
  50036. CImg<T>& load_jpeg(const char *const filename) {
  50037. return _load_jpeg(0,filename);
  50038. }
  50039. //! Load image from a JPEG file \newinstance.
  50040. static CImg<T> get_load_jpeg(const char *const filename) {
  50041. return CImg<T>().load_jpeg(filename);
  50042. }
  50043. //! Load image from a JPEG file \overloading.
  50044. CImg<T>& load_jpeg(std::FILE *const file) {
  50045. return _load_jpeg(file,0);
  50046. }
  50047. //! Load image from a JPEG file \newinstance.
  50048. static CImg<T> get_load_jpeg(std::FILE *const file) {
  50049. return CImg<T>().load_jpeg(file);
  50050. }
  50051. // Custom error handler for libjpeg.
  50052. #ifdef cimg_use_jpeg
  50053. struct _cimg_error_mgr {
  50054. struct jpeg_error_mgr original;
  50055. jmp_buf setjmp_buffer;
  50056. char message[JMSG_LENGTH_MAX];
  50057. };
  50058. typedef struct _cimg_error_mgr *_cimg_error_ptr;
  50059. METHODDEF(void) _cimg_jpeg_error_exit(j_common_ptr cinfo) {
  50060. _cimg_error_ptr c_err = (_cimg_error_ptr) cinfo->err; // Return control to the setjmp point
  50061. (*cinfo->err->format_message)(cinfo,c_err->message);
  50062. jpeg_destroy(cinfo); // Clean memory and temp files
  50063. longjmp(c_err->setjmp_buffer,1);
  50064. }
  50065. #endif
  50066. CImg<T>& _load_jpeg(std::FILE *const file, const char *const filename) {
  50067. if (!file && !filename)
  50068. throw CImgArgumentException(_cimg_instance
  50069. "load_jpeg(): Specified filename is (null).",
  50070. cimg_instance);
  50071. #ifndef cimg_use_jpeg
  50072. if (file)
  50073. throw CImgIOException(_cimg_instance
  50074. "load_jpeg(): Unable to load data from '(FILE*)' unless libjpeg is enabled.",
  50075. cimg_instance);
  50076. else return load_other(filename);
  50077. #else
  50078. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  50079. struct jpeg_decompress_struct cinfo;
  50080. struct _cimg_error_mgr jerr;
  50081. cinfo.err = jpeg_std_error(&jerr.original);
  50082. jerr.original.error_exit = _cimg_jpeg_error_exit;
  50083. if (setjmp(jerr.setjmp_buffer)) { // JPEG error
  50084. if (!file) cimg::fclose(nfile);
  50085. throw CImgIOException(_cimg_instance
  50086. "load_jpeg(): Error message returned by libjpeg: %s.",
  50087. cimg_instance,jerr.message);
  50088. }
  50089. jpeg_create_decompress(&cinfo);
  50090. jpeg_stdio_src(&cinfo,nfile);
  50091. jpeg_read_header(&cinfo,TRUE);
  50092. jpeg_start_decompress(&cinfo);
  50093. if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) {
  50094. if (!file) {
  50095. cimg::fclose(nfile);
  50096. return load_other(filename);
  50097. } else
  50098. throw CImgIOException(_cimg_instance
  50099. "load_jpeg(): Failed to load JPEG data from file '%s'.",
  50100. cimg_instance,filename?filename:"(FILE*)");
  50101. }
  50102. CImg<ucharT> buffer(cinfo.output_width*cinfo.output_components);
  50103. JSAMPROW row_pointer[1];
  50104. try { assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); }
  50105. catch (...) { if (!file) cimg::fclose(nfile); throw; }
  50106. T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height,
  50107. *ptr_a = _data + 3UL*_width*_height;
  50108. while (cinfo.output_scanline<cinfo.output_height) {
  50109. *row_pointer = buffer._data;
  50110. if (jpeg_read_scanlines(&cinfo,row_pointer,1)!=1) {
  50111. cimg::warn(_cimg_instance
  50112. "load_jpeg(): Incomplete data in file '%s'.",
  50113. cimg_instance,filename?filename:"(FILE*)");
  50114. break;
  50115. }
  50116. const unsigned char *ptrs = buffer._data;
  50117. switch (_spectrum) {
  50118. case 1 : {
  50119. cimg_forX(*this,x) *(ptr_r++) = (T)*(ptrs++);
  50120. } break;
  50121. case 3 : {
  50122. cimg_forX(*this,x) {
  50123. *(ptr_r++) = (T)*(ptrs++);
  50124. *(ptr_g++) = (T)*(ptrs++);
  50125. *(ptr_b++) = (T)*(ptrs++);
  50126. }
  50127. } break;
  50128. case 4 : {
  50129. cimg_forX(*this,x) {
  50130. *(ptr_r++) = (T)*(ptrs++);
  50131. *(ptr_g++) = (T)*(ptrs++);
  50132. *(ptr_b++) = (T)*(ptrs++);
  50133. *(ptr_a++) = (T)*(ptrs++);
  50134. }
  50135. } break;
  50136. }
  50137. }
  50138. jpeg_finish_decompress(&cinfo);
  50139. jpeg_destroy_decompress(&cinfo);
  50140. if (!file) cimg::fclose(nfile);
  50141. return *this;
  50142. #endif
  50143. }
  50144. //! Load image from a file, using Magick++ library.
  50145. /**
  50146. \param filename Filename, as a C-string.
  50147. **/
  50148. // Added April/may 2006 by Christoph Hormann <[email protected]>
  50149. // This is experimental code, not much tested, use with care.
  50150. CImg<T>& load_magick(const char *const filename) {
  50151. if (!filename)
  50152. throw CImgArgumentException(_cimg_instance
  50153. "load_magick(): Specified filename is (null).",
  50154. cimg_instance);
  50155. #ifdef cimg_use_magick
  50156. Magick::Image image(filename);
  50157. const unsigned int W = image.size().width(), H = image.size().height();
  50158. switch (image.type()) {
  50159. case Magick::PaletteMatteType :
  50160. case Magick::TrueColorMatteType :
  50161. case Magick::ColorSeparationType : {
  50162. assign(W,H,1,4);
  50163. T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3);
  50164. Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
  50165. for (ulongT off = (ulongT)W*H; off; --off) {
  50166. *(ptr_r++) = (T)(pixels->red);
  50167. *(ptr_g++) = (T)(pixels->green);
  50168. *(ptr_b++) = (T)(pixels->blue);
  50169. *(ptr_a++) = (T)(pixels->opacity);
  50170. ++pixels;
  50171. }
  50172. } break;
  50173. case Magick::PaletteType :
  50174. case Magick::TrueColorType : {
  50175. assign(W,H,1,3);
  50176. T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
  50177. Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
  50178. for (ulongT off = (ulongT)W*H; off; --off) {
  50179. *(ptr_r++) = (T)(pixels->red);
  50180. *(ptr_g++) = (T)(pixels->green);
  50181. *(ptr_b++) = (T)(pixels->blue);
  50182. ++pixels;
  50183. }
  50184. } break;
  50185. case Magick::GrayscaleMatteType : {
  50186. assign(W,H,1,2);
  50187. T *ptr_r = data(0,0,0,0), *ptr_a = data(0,0,0,1);
  50188. Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
  50189. for (ulongT off = (ulongT)W*H; off; --off) {
  50190. *(ptr_r++) = (T)(pixels->red);
  50191. *(ptr_a++) = (T)(pixels->opacity);
  50192. ++pixels;
  50193. }
  50194. } break;
  50195. default : {
  50196. assign(W,H,1,1);
  50197. T *ptr_r = data(0,0,0,0);
  50198. Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
  50199. for (ulongT off = (ulongT)W*H; off; --off) {
  50200. *(ptr_r++) = (T)(pixels->red);
  50201. ++pixels;
  50202. }
  50203. }
  50204. }
  50205. return *this;
  50206. #else
  50207. throw CImgIOException(_cimg_instance
  50208. "load_magick(): Unable to load file '%s' unless libMagick++ is enabled.",
  50209. cimg_instance,
  50210. filename);
  50211. #endif
  50212. }
  50213. //! Load image from a file, using Magick++ library \newinstance.
  50214. static CImg<T> get_load_magick(const char *const filename) {
  50215. return CImg<T>().load_magick(filename);
  50216. }
  50217. //! Load image from a PNG file.
  50218. /**
  50219. \param filename Filename, as a C-string.
  50220. \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
  50221. **/
  50222. CImg<T>& load_png(const char *const filename, unsigned int *const bits_per_value=0) {
  50223. return _load_png(0,filename,bits_per_value);
  50224. }
  50225. //! Load image from a PNG file \newinstance.
  50226. static CImg<T> get_load_png(const char *const filename, unsigned int *const bits_per_value=0) {
  50227. return CImg<T>().load_png(filename,bits_per_value);
  50228. }
  50229. //! Load image from a PNG file \overloading.
  50230. CImg<T>& load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
  50231. return _load_png(file,0,bits_per_value);
  50232. }
  50233. //! Load image from a PNG file \newinstance.
  50234. static CImg<T> get_load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
  50235. return CImg<T>().load_png(file,bits_per_value);
  50236. }
  50237. // (Note: Most of this function has been written by Eric Fausett)
  50238. CImg<T>& _load_png(std::FILE *const file, const char *const filename, unsigned int *const bits_per_value) {
  50239. if (!file && !filename)
  50240. throw CImgArgumentException(_cimg_instance
  50241. "load_png(): Specified filename is (null).",
  50242. cimg_instance);
  50243. #ifndef cimg_use_png
  50244. cimg::unused(bits_per_value);
  50245. if (file)
  50246. throw CImgIOException(_cimg_instance
  50247. "load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.",
  50248. cimg_instance);
  50249. else return load_other(filename);
  50250. #else
  50251. // Open file and check for PNG validity
  50252. #if defined __GNUC__
  50253. const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning
  50254. std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb");
  50255. #else
  50256. const char *nfilename = filename;
  50257. std::FILE *nfile = file?file:cimg::fopen(nfilename,"rb");
  50258. #endif
  50259. unsigned char pngCheck[8] = { 0 };
  50260. cimg::fread(pngCheck,8,(std::FILE*)nfile);
  50261. if (png_sig_cmp(pngCheck,0,8)) {
  50262. if (!file) cimg::fclose(nfile);
  50263. throw CImgIOException(_cimg_instance
  50264. "load_png(): Invalid PNG file '%s'.",
  50265. cimg_instance,
  50266. nfilename?nfilename:"(FILE*)");
  50267. }
  50268. // Setup PNG structures for read
  50269. png_voidp user_error_ptr = 0;
  50270. png_error_ptr user_error_fn = 0, user_warning_fn = 0;
  50271. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);
  50272. if (!png_ptr) {
  50273. if (!file) cimg::fclose(nfile);
  50274. throw CImgIOException(_cimg_instance
  50275. "load_png(): Failed to initialize 'png_ptr' structure for file '%s'.",
  50276. cimg_instance,
  50277. nfilename?nfilename:"(FILE*)");
  50278. }
  50279. png_infop info_ptr = png_create_info_struct(png_ptr);
  50280. if (!info_ptr) {
  50281. if (!file) cimg::fclose(nfile);
  50282. png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0);
  50283. throw CImgIOException(_cimg_instance
  50284. "load_png(): Failed to initialize 'info_ptr' structure for file '%s'.",
  50285. cimg_instance,
  50286. nfilename?nfilename:"(FILE*)");
  50287. }
  50288. png_infop end_info = png_create_info_struct(png_ptr);
  50289. if (!end_info) {
  50290. if (!file) cimg::fclose(nfile);
  50291. png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0);
  50292. throw CImgIOException(_cimg_instance
  50293. "load_png(): Failed to initialize 'end_info' structure for file '%s'.",
  50294. cimg_instance,
  50295. nfilename?nfilename:"(FILE*)");
  50296. }
  50297. // Error handling callback for png file reading
  50298. if (setjmp(png_jmpbuf(png_ptr))) {
  50299. if (!file) cimg::fclose((std::FILE*)nfile);
  50300. png_destroy_read_struct(&png_ptr, &end_info, (png_infopp)0);
  50301. throw CImgIOException(_cimg_instance
  50302. "load_png(): Encountered unknown fatal error in libpng for file '%s'.",
  50303. cimg_instance,
  50304. nfilename?nfilename:"(FILE*)");
  50305. }
  50306. png_init_io(png_ptr, nfile);
  50307. png_set_sig_bytes(png_ptr, 8);
  50308. // Get PNG Header Info up to data block
  50309. png_read_info(png_ptr,info_ptr);
  50310. png_uint_32 W, H;
  50311. int bit_depth, color_type, interlace_type;
  50312. bool is_gray = false;
  50313. png_get_IHDR(png_ptr,info_ptr,&W,&H,&bit_depth,&color_type,&interlace_type,(int*)0,(int*)0);
  50314. if (bits_per_value) *bits_per_value = (unsigned int)bit_depth;
  50315. // Transforms to unify image data
  50316. if (color_type==PNG_COLOR_TYPE_PALETTE) {
  50317. png_set_palette_to_rgb(png_ptr);
  50318. color_type = PNG_COLOR_TYPE_RGB;
  50319. bit_depth = 8;
  50320. }
  50321. if (color_type==PNG_COLOR_TYPE_GRAY && bit_depth<8) {
  50322. png_set_expand_gray_1_2_4_to_8(png_ptr);
  50323. is_gray = true;
  50324. bit_depth = 8;
  50325. }
  50326. if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) {
  50327. png_set_tRNS_to_alpha(png_ptr);
  50328. color_type |= PNG_COLOR_MASK_ALPHA;
  50329. }
  50330. if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) {
  50331. png_set_gray_to_rgb(png_ptr);
  50332. color_type |= PNG_COLOR_MASK_COLOR;
  50333. is_gray = true;
  50334. }
  50335. if (color_type==PNG_COLOR_TYPE_RGB)
  50336. png_set_filler(png_ptr,0xffffU,PNG_FILLER_AFTER);
  50337. png_read_update_info(png_ptr,info_ptr);
  50338. if (bit_depth!=8 && bit_depth!=16) {
  50339. if (!file) cimg::fclose(nfile);
  50340. png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);
  50341. throw CImgIOException(_cimg_instance
  50342. "load_png(): Invalid bit depth %u in file '%s'.",
  50343. cimg_instance,
  50344. bit_depth,nfilename?nfilename:"(FILE*)");
  50345. }
  50346. const int byte_depth = bit_depth>>3;
  50347. // Allocate memory for image reading
  50348. png_bytep *const imgData = new png_bytep[H];
  50349. for (unsigned int row = 0; row<H; ++row) imgData[row] = new png_byte[(size_t)byte_depth*4*W];
  50350. png_read_image(png_ptr,imgData);
  50351. png_read_end(png_ptr,end_info);
  50352. // Read pixel data
  50353. if (color_type!=PNG_COLOR_TYPE_RGB && color_type!=PNG_COLOR_TYPE_RGB_ALPHA) {
  50354. if (!file) cimg::fclose(nfile);
  50355. png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);
  50356. throw CImgIOException(_cimg_instance
  50357. "load_png(): Invalid color coding type %u in file '%s'.",
  50358. cimg_instance,
  50359. color_type,nfilename?nfilename:"(FILE*)");
  50360. }
  50361. const bool is_alpha = (color_type==PNG_COLOR_TYPE_RGBA);
  50362. try { assign(W,H,1,(is_gray?1:3) + (is_alpha?1:0)); }
  50363. catch (...) { if (!file) cimg::fclose(nfile); throw; }
  50364. T
  50365. *ptr_r = data(0,0,0,0),
  50366. *ptr_g = is_gray?0:data(0,0,0,1),
  50367. *ptr_b = is_gray?0:data(0,0,0,2),
  50368. *ptr_a = !is_alpha?0:data(0,0,0,is_gray?1:3);
  50369. switch (bit_depth) {
  50370. case 8 : {
  50371. cimg_forY(*this,y) {
  50372. const unsigned char *ptrs = (unsigned char*)imgData[y];
  50373. cimg_forX(*this,x) {
  50374. *(ptr_r++) = (T)*(ptrs++);
  50375. if (ptr_g) *(ptr_g++) = (T)*(ptrs++); else ++ptrs;
  50376. if (ptr_b) *(ptr_b++) = (T)*(ptrs++); else ++ptrs;
  50377. if (ptr_a) *(ptr_a++) = (T)*(ptrs++); else ++ptrs;
  50378. }
  50379. }
  50380. } break;
  50381. case 16 : {
  50382. cimg_forY(*this,y) {
  50383. const unsigned short *ptrs = (unsigned short*)(imgData[y]);
  50384. if (!cimg::endianness()) cimg::invert_endianness(ptrs,4*_width);
  50385. cimg_forX(*this,x) {
  50386. *(ptr_r++) = (T)*(ptrs++);
  50387. if (ptr_g) *(ptr_g++) = (T)*(ptrs++); else ++ptrs;
  50388. if (ptr_b) *(ptr_b++) = (T)*(ptrs++); else ++ptrs;
  50389. if (ptr_a) *(ptr_a++) = (T)*(ptrs++); else ++ptrs;
  50390. }
  50391. }
  50392. } break;
  50393. }
  50394. png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  50395. // Deallocate image read memory
  50396. cimg_forY(*this,n) delete[] imgData[n];
  50397. delete[] imgData;
  50398. if (!file) cimg::fclose(nfile);
  50399. return *this;
  50400. #endif
  50401. }
  50402. //! Load image from a PNM file.
  50403. /**
  50404. \param filename Filename, as a C-string.
  50405. **/
  50406. CImg<T>& load_pnm(const char *const filename) {
  50407. return _load_pnm(0,filename);
  50408. }
  50409. //! Load image from a PNM file \newinstance.
  50410. static CImg<T> get_load_pnm(const char *const filename) {
  50411. return CImg<T>().load_pnm(filename);
  50412. }
  50413. //! Load image from a PNM file \overloading.
  50414. CImg<T>& load_pnm(std::FILE *const file) {
  50415. return _load_pnm(file,0);
  50416. }
  50417. //! Load image from a PNM file \newinstance.
  50418. static CImg<T> get_load_pnm(std::FILE *const file) {
  50419. return CImg<T>().load_pnm(file);
  50420. }
  50421. CImg<T>& _load_pnm(std::FILE *const file, const char *const filename) {
  50422. if (!file && !filename)
  50423. throw CImgArgumentException(_cimg_instance
  50424. "load_pnm(): Specified filename is (null).",
  50425. cimg_instance);
  50426. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  50427. unsigned int ppm_type, W, H, D = 1, colormax = 255;
  50428. CImg<charT> item(16384,1,1,1,0);
  50429. int err, rval, gval, bval;
  50430. const longT cimg_iobuffer = (longT)24*1024*1024;
  50431. while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50432. if (cimg_sscanf(item," P%u",&ppm_type)!=1) {
  50433. if (!file) cimg::fclose(nfile);
  50434. throw CImgIOException(_cimg_instance
  50435. "load_pnm(): PNM header not found in file '%s'.",
  50436. cimg_instance,
  50437. filename?filename:"(FILE*)");
  50438. }
  50439. while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50440. if ((err=cimg_sscanf(item," %u %u %u %u",&W,&H,&D,&colormax))<2) {
  50441. if (!file) cimg::fclose(nfile);
  50442. throw CImgIOException(_cimg_instance
  50443. "load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.",
  50444. cimg_instance,
  50445. filename?filename:"(FILE*)");
  50446. }
  50447. if (ppm_type!=1 && ppm_type!=4) {
  50448. if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) {
  50449. while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50450. if (cimg_sscanf(item,"%u",&colormax)!=1)
  50451. cimg::warn(_cimg_instance
  50452. "load_pnm(): COLORMAX field is undefined in file '%s'.",
  50453. cimg_instance,
  50454. filename?filename:"(FILE*)");
  50455. } else { colormax = D; D = 1; }
  50456. }
  50457. std::fgetc(nfile);
  50458. if (filename) { // Check that dimensions specified in file does not exceed the buffer dimension
  50459. const cimg_int64 siz = cimg::fsize(filename);
  50460. if (W*H*D>siz)
  50461. throw CImgIOException(_cimg_instance
  50462. "load_pnm(): Specified image dimensions in file '%s' exceed file size.",
  50463. cimg_instance,
  50464. filename);
  50465. }
  50466. switch (ppm_type) {
  50467. case 1 : { // 2D B&W ascii
  50468. assign(W,H,1,1);
  50469. T* ptrd = _data;
  50470. cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)(rval?0:255); else break; }
  50471. } break;
  50472. case 2 : { // 2D grey ascii
  50473. assign(W,H,1,1);
  50474. T* ptrd = _data;
  50475. cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)rval; else break; }
  50476. } break;
  50477. case 3 : { // 2D color ascii
  50478. assign(W,H,1,3);
  50479. T *ptrd = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
  50480. cimg_forXY(*this,x,y) {
  50481. if (std::fscanf(nfile,"%d %d %d",&rval,&gval,&bval)==3) {
  50482. *(ptrd++) = (T)rval; *(ptr_g++) = (T)gval; *(ptr_b++) = (T)bval;
  50483. } else break;
  50484. }
  50485. } break;
  50486. case 4 : { // 2D b&w binary (support 3D PINK extension)
  50487. CImg<ucharT> raw;
  50488. assign(W,H,D,1);
  50489. T *ptrd = data(0,0,0,0);
  50490. unsigned int w = 0, h = 0, d = 0;
  50491. for (longT to_read = (longT)((W/8 + (W%8?1:0))*H*D); to_read>0; ) {
  50492. raw.assign(std::min(to_read,cimg_iobuffer));
  50493. cimg::fread(raw._data,raw._width,nfile);
  50494. to_read-=raw._width;
  50495. const unsigned char *ptrs = raw._data;
  50496. unsigned char mask = 0, val = 0;
  50497. for (ulongT off = (ulongT)raw._width; off || mask; mask>>=1) {
  50498. if (!mask) { if (off--) val = *(ptrs++); mask = 128; }
  50499. *(ptrd++) = (T)((val&mask)?0:255);
  50500. if (++w==W) { w = 0; mask = 0; if (++h==H) { h = 0; if (++d==D) break; }}
  50501. }
  50502. }
  50503. } break;
  50504. case 5 : case 7 : { // 2D/3D grey binary (support 3D PINK extension)
  50505. if (colormax<256) { // 8 bits
  50506. CImg<ucharT> raw;
  50507. assign(W,H,D,1);
  50508. T *ptrd = data(0,0,0,0);
  50509. for (longT to_read = (longT)size(); to_read>0; ) {
  50510. raw.assign(std::min(to_read,cimg_iobuffer));
  50511. cimg::fread(raw._data,raw._width,nfile);
  50512. to_read-=raw._width;
  50513. const unsigned char *ptrs = raw._data;
  50514. for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
  50515. }
  50516. } else { // 16 bits
  50517. CImg<ushortT> raw;
  50518. assign(W,H,D,1);
  50519. T *ptrd = data(0,0,0,0);
  50520. for (longT to_read = (longT)size(); to_read>0; ) {
  50521. raw.assign(std::min(to_read,cimg_iobuffer/2));
  50522. cimg::fread(raw._data,raw._width,nfile);
  50523. if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width);
  50524. to_read-=raw._width;
  50525. const unsigned short *ptrs = raw._data;
  50526. for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
  50527. }
  50528. }
  50529. } break;
  50530. case 6 : { // 2D color binary
  50531. if (colormax<256) { // 8 bits
  50532. CImg<ucharT> raw;
  50533. assign(W,H,1,3);
  50534. T
  50535. *ptr_r = data(0,0,0,0),
  50536. *ptr_g = data(0,0,0,1),
  50537. *ptr_b = data(0,0,0,2);
  50538. for (longT to_read = (longT)size(); to_read>0; ) {
  50539. raw.assign(std::min(to_read,cimg_iobuffer));
  50540. cimg::fread(raw._data,raw._width,nfile);
  50541. to_read-=raw._width;
  50542. const unsigned char *ptrs = raw._data;
  50543. for (ulongT off = (ulongT)raw._width/3; off; --off) {
  50544. *(ptr_r++) = (T)*(ptrs++);
  50545. *(ptr_g++) = (T)*(ptrs++);
  50546. *(ptr_b++) = (T)*(ptrs++);
  50547. }
  50548. }
  50549. } else { // 16 bits
  50550. CImg<ushortT> raw;
  50551. assign(W,H,1,3);
  50552. T
  50553. *ptr_r = data(0,0,0,0),
  50554. *ptr_g = data(0,0,0,1),
  50555. *ptr_b = data(0,0,0,2);
  50556. for (longT to_read = (longT)size(); to_read>0; ) {
  50557. raw.assign(std::min(to_read,cimg_iobuffer/2));
  50558. cimg::fread(raw._data,raw._width,nfile);
  50559. if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width);
  50560. to_read-=raw._width;
  50561. const unsigned short *ptrs = raw._data;
  50562. for (ulongT off = (ulongT)raw._width/3; off; --off) {
  50563. *(ptr_r++) = (T)*(ptrs++);
  50564. *(ptr_g++) = (T)*(ptrs++);
  50565. *(ptr_b++) = (T)*(ptrs++);
  50566. }
  50567. }
  50568. }
  50569. } break;
  50570. case 8 : { // 2D/3D grey binary with int32 integers (PINK extension)
  50571. CImg<intT> raw;
  50572. assign(W,H,D,1);
  50573. T *ptrd = data(0,0,0,0);
  50574. for (longT to_read = (longT)size(); to_read>0; ) {
  50575. raw.assign(std::min(to_read,cimg_iobuffer));
  50576. cimg::fread(raw._data,raw._width,nfile);
  50577. to_read-=raw._width;
  50578. const int *ptrs = raw._data;
  50579. for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
  50580. }
  50581. } break;
  50582. case 9 : { // 2D/3D grey binary with float values (PINK extension)
  50583. CImg<floatT> raw;
  50584. assign(W,H,D,1);
  50585. T *ptrd = data(0,0,0,0);
  50586. for (longT to_read = (longT)size(); to_read>0; ) {
  50587. raw.assign(std::min(to_read,cimg_iobuffer));
  50588. cimg::fread(raw._data,raw._width,nfile);
  50589. to_read-=raw._width;
  50590. const float *ptrs = raw._data;
  50591. for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
  50592. }
  50593. } break;
  50594. default :
  50595. assign();
  50596. if (!file) cimg::fclose(nfile);
  50597. throw CImgIOException(_cimg_instance
  50598. "load_pnm(): PNM type 'P%d' found, but type is not supported.",
  50599. cimg_instance,
  50600. filename?filename:"(FILE*)",ppm_type);
  50601. }
  50602. if (!file) cimg::fclose(nfile);
  50603. return *this;
  50604. }
  50605. //! Load image from a PFM file.
  50606. /**
  50607. \param filename Filename, as a C-string.
  50608. **/
  50609. CImg<T>& load_pfm(const char *const filename) {
  50610. return _load_pfm(0,filename);
  50611. }
  50612. //! Load image from a PFM file \newinstance.
  50613. static CImg<T> get_load_pfm(const char *const filename) {
  50614. return CImg<T>().load_pfm(filename);
  50615. }
  50616. //! Load image from a PFM file \overloading.
  50617. CImg<T>& load_pfm(std::FILE *const file) {
  50618. return _load_pfm(file,0);
  50619. }
  50620. //! Load image from a PFM file \newinstance.
  50621. static CImg<T> get_load_pfm(std::FILE *const file) {
  50622. return CImg<T>().load_pfm(file);
  50623. }
  50624. CImg<T>& _load_pfm(std::FILE *const file, const char *const filename) {
  50625. if (!file && !filename)
  50626. throw CImgArgumentException(_cimg_instance
  50627. "load_pfm(): Specified filename is (null).",
  50628. cimg_instance);
  50629. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  50630. char pfm_type;
  50631. CImg<charT> item(16384,1,1,1,0);
  50632. int W = 0, H = 0, err = 0;
  50633. double scale = 0;
  50634. while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50635. if (cimg_sscanf(item," P%c",&pfm_type)!=1) {
  50636. if (!file) cimg::fclose(nfile);
  50637. throw CImgIOException(_cimg_instance
  50638. "load_pfm(): PFM header not found in file '%s'.",
  50639. cimg_instance,
  50640. filename?filename:"(FILE*)");
  50641. }
  50642. while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50643. if ((err=cimg_sscanf(item," %d %d",&W,&H))<2) {
  50644. if (!file) cimg::fclose(nfile);
  50645. throw CImgIOException(_cimg_instance
  50646. "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.",
  50647. cimg_instance,
  50648. filename?filename:"(FILE*)");
  50649. } else if (W<=0 || H<=0) {
  50650. if (!file) cimg::fclose(nfile);
  50651. throw CImgIOException(_cimg_instance
  50652. "load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.",
  50653. cimg_instance,W,H,
  50654. filename?filename:"(FILE*)");
  50655. }
  50656. if (err==2) {
  50657. while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
  50658. if (cimg_sscanf(item,"%lf",&scale)!=1)
  50659. cimg::warn(_cimg_instance
  50660. "load_pfm(): SCALE field is undefined in file '%s'.",
  50661. cimg_instance,
  50662. filename?filename:"(FILE*)");
  50663. }
  50664. std::fgetc(nfile);
  50665. const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness();
  50666. if (is_color) {
  50667. assign(W,H,1,3,(T)0);
  50668. CImg<floatT> buf(3*W);
  50669. T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
  50670. cimg_forY(*this,y) {
  50671. cimg::fread(buf._data,3*W,nfile);
  50672. if (is_inverted) cimg::invert_endianness(buf._data,3*W);
  50673. const float *ptrs = buf._data;
  50674. cimg_forX(*this,x) {
  50675. *(ptr_r++) = (T)*(ptrs++);
  50676. *(ptr_g++) = (T)*(ptrs++);
  50677. *(ptr_b++) = (T)*(ptrs++);
  50678. }
  50679. }
  50680. } else {
  50681. assign(W,H,1,1,(T)0);
  50682. CImg<floatT> buf(W);
  50683. T *ptrd = data(0,0,0,0);
  50684. cimg_forY(*this,y) {
  50685. cimg::fread(buf._data,W,nfile);
  50686. if (is_inverted) cimg::invert_endianness(buf._data,W);
  50687. const float *ptrs = buf._data;
  50688. cimg_forX(*this,x) *(ptrd++) = (T)*(ptrs++);
  50689. }
  50690. }
  50691. if (!file) cimg::fclose(nfile);
  50692. return mirror('y'); // Most of the .pfm files are flipped along the y-axis
  50693. }
  50694. //! Load image from a RGB file.
  50695. /**
  50696. \param filename Filename, as a C-string.
  50697. \param dimw Width of the image buffer.
  50698. \param dimh Height of the image buffer.
  50699. **/
  50700. CImg<T>& load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
  50701. return _load_rgb(0,filename,dimw,dimh);
  50702. }
  50703. //! Load image from a RGB file \newinstance.
  50704. static CImg<T> get_load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
  50705. return CImg<T>().load_rgb(filename,dimw,dimh);
  50706. }
  50707. //! Load image from a RGB file \overloading.
  50708. CImg<T>& load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
  50709. return _load_rgb(file,0,dimw,dimh);
  50710. }
  50711. //! Load image from a RGB file \newinstance.
  50712. static CImg<T> get_load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
  50713. return CImg<T>().load_rgb(file,dimw,dimh);
  50714. }
  50715. CImg<T>& _load_rgb(std::FILE *const file, const char *const filename,
  50716. const unsigned int dimw, const unsigned int dimh) {
  50717. if (!file && !filename)
  50718. throw CImgArgumentException(_cimg_instance
  50719. "load_rgb(): Specified filename is (null).",
  50720. cimg_instance);
  50721. if (!dimw || !dimh) return assign();
  50722. const longT cimg_iobuffer = (longT)24*1024*1024;
  50723. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  50724. CImg<ucharT> raw;
  50725. assign(dimw,dimh,1,3);
  50726. T
  50727. *ptr_r = data(0,0,0,0),
  50728. *ptr_g = data(0,0,0,1),
  50729. *ptr_b = data(0,0,0,2);
  50730. for (longT to_read = (longT)size(); to_read>0; ) {
  50731. raw.assign(std::min(to_read,cimg_iobuffer));
  50732. cimg::fread(raw._data,raw._width,nfile);
  50733. to_read-=raw._width;
  50734. const unsigned char *ptrs = raw._data;
  50735. for (ulongT off = raw._width/3UL; off; --off) {
  50736. *(ptr_r++) = (T)*(ptrs++);
  50737. *(ptr_g++) = (T)*(ptrs++);
  50738. *(ptr_b++) = (T)*(ptrs++);
  50739. }
  50740. }
  50741. if (!file) cimg::fclose(nfile);
  50742. return *this;
  50743. }
  50744. //! Load image from a RGBA file.
  50745. /**
  50746. \param filename Filename, as a C-string.
  50747. \param dimw Width of the image buffer.
  50748. \param dimh Height of the image buffer.
  50749. **/
  50750. CImg<T>& load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
  50751. return _load_rgba(0,filename,dimw,dimh);
  50752. }
  50753. //! Load image from a RGBA file \newinstance.
  50754. static CImg<T> get_load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
  50755. return CImg<T>().load_rgba(filename,dimw,dimh);
  50756. }
  50757. //! Load image from a RGBA file \overloading.
  50758. CImg<T>& load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
  50759. return _load_rgba(file,0,dimw,dimh);
  50760. }
  50761. //! Load image from a RGBA file \newinstance.
  50762. static CImg<T> get_load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
  50763. return CImg<T>().load_rgba(file,dimw,dimh);
  50764. }
  50765. CImg<T>& _load_rgba(std::FILE *const file, const char *const filename,
  50766. const unsigned int dimw, const unsigned int dimh) {
  50767. if (!file && !filename)
  50768. throw CImgArgumentException(_cimg_instance
  50769. "load_rgba(): Specified filename is (null).",
  50770. cimg_instance);
  50771. if (!dimw || !dimh) return assign();
  50772. const longT cimg_iobuffer = (longT)24*1024*1024;
  50773. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  50774. CImg<ucharT> raw;
  50775. assign(dimw,dimh,1,4);
  50776. T
  50777. *ptr_r = data(0,0,0,0),
  50778. *ptr_g = data(0,0,0,1),
  50779. *ptr_b = data(0,0,0,2),
  50780. *ptr_a = data(0,0,0,3);
  50781. for (longT to_read = (longT)size(); to_read>0; ) {
  50782. raw.assign(std::min(to_read,cimg_iobuffer));
  50783. cimg::fread(raw._data,raw._width,nfile);
  50784. to_read-=raw._width;
  50785. const unsigned char *ptrs = raw._data;
  50786. for (ulongT off = raw._width/4UL; off; --off) {
  50787. *(ptr_r++) = (T)*(ptrs++);
  50788. *(ptr_g++) = (T)*(ptrs++);
  50789. *(ptr_b++) = (T)*(ptrs++);
  50790. *(ptr_a++) = (T)*(ptrs++);
  50791. }
  50792. }
  50793. if (!file) cimg::fclose(nfile);
  50794. return *this;
  50795. }
  50796. //! Load image from a TIFF file.
  50797. /**
  50798. \param filename Filename, as a C-string.
  50799. \param first_frame First frame to read (for multi-pages tiff).
  50800. \param last_frame Last frame to read (for multi-pages tiff).
  50801. \param step_frame Step value of frame reading.
  50802. \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
  50803. \param[out] voxel_size Voxel size, as stored in the filename.
  50804. \param[out] description Description, as stored in the filename.
  50805. \note
  50806. - libtiff support is enabled by defining the precompilation
  50807. directive \c cimg_use_tif.
  50808. - When libtiff is enabled, 2D and 3D (multipage) several
  50809. channel per pixel are supported for
  50810. <tt>char,uchar,short,ushort,float</tt> and \c double pixel types.
  50811. - If \c cimg_use_tiff is not defined at compile time the
  50812. function uses CImg<T>& load_other(const char*).
  50813. **/
  50814. CImg<T>& load_tiff(const char *const filename,
  50815. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  50816. const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
  50817. float *const voxel_size=0, CImg<charT> *const description=0) {
  50818. if (!filename)
  50819. throw CImgArgumentException(_cimg_instance
  50820. "load_tiff(): Specified filename is (null).",
  50821. cimg_instance);
  50822. const unsigned int
  50823. nfirst_frame = first_frame<last_frame?first_frame:last_frame,
  50824. nstep_frame = step_frame?step_frame:1;
  50825. unsigned int nlast_frame = first_frame<last_frame?last_frame:first_frame;
  50826. #ifndef cimg_use_tiff
  50827. cimg::unused(bits_per_value,voxel_size,description);
  50828. if (nfirst_frame || nlast_frame!=~0U || nstep_frame>1)
  50829. throw CImgArgumentException(_cimg_instance
  50830. "load_tiff(): Unable to read sub-images from file '%s' unless libtiff is enabled.",
  50831. cimg_instance,
  50832. filename);
  50833. return load_other(filename);
  50834. #else
  50835. #if cimg_verbosity<3
  50836. TIFFSetWarningHandler(0);
  50837. TIFFSetErrorHandler(0);
  50838. #endif
  50839. TIFF *tif = TIFFOpen(filename,"r");
  50840. if (tif) {
  50841. unsigned int nb_images = 0;
  50842. do ++nb_images; while (TIFFReadDirectory(tif));
  50843. if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))
  50844. cimg::warn(_cimg_instance
  50845. "load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).",
  50846. cimg_instance,
  50847. filename,nb_images,nfirst_frame,nlast_frame,nstep_frame);
  50848. if (nfirst_frame>=nb_images) return assign();
  50849. if (nlast_frame>=nb_images) nlast_frame = nb_images - 1;
  50850. TIFFSetDirectory(tif,0);
  50851. CImg<T> frame;
  50852. for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) {
  50853. frame._load_tiff(tif,l,bits_per_value,voxel_size,description);
  50854. if (l==nfirst_frame)
  50855. assign(frame._width,frame._height,1 + (nlast_frame - nfirst_frame)/nstep_frame,frame._spectrum);
  50856. if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum)
  50857. resize(std::max(frame._width,_width),
  50858. std::max(frame._height,_height),-100,
  50859. std::max(frame._spectrum,_spectrum),0);
  50860. draw_image(0,0,(l - nfirst_frame)/nstep_frame,frame);
  50861. }
  50862. TIFFClose(tif);
  50863. } else throw CImgIOException(_cimg_instance
  50864. "load_tiff(): Failed to open file '%s'.",
  50865. cimg_instance,
  50866. filename);
  50867. return *this;
  50868. #endif
  50869. }
  50870. //! Load image from a TIFF file \newinstance.
  50871. static CImg<T> get_load_tiff(const char *const filename,
  50872. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  50873. const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
  50874. float *const voxel_size=0, CImg<charT> *const description=0) {
  50875. return CImg<T>().load_tiff(filename,first_frame,last_frame,step_frame,bits_per_value,voxel_size,description);
  50876. }
  50877. // (Original contribution by Jerome Boulanger).
  50878. #ifdef cimg_use_tiff
  50879. template<typename t>
  50880. void _load_tiff_tiled_contig(TIFF *const tif, const uint16_t samplesperpixel,
  50881. const uint32_t nx, const uint32_t ny, const uint32_t tw, const uint32_t th) {
  50882. t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif));
  50883. if (buf) {
  50884. for (unsigned int row = 0; row<ny; row+=th)
  50885. for (unsigned int col = 0; col<nx; col+=tw) {
  50886. if (TIFFReadTile(tif,buf,col,row,0,0)<0) {
  50887. _TIFFfree(buf); TIFFClose(tif);
  50888. throw CImgIOException(_cimg_instance
  50889. "load_tiff(): Invalid tile in file '%s'.",
  50890. cimg_instance,
  50891. TIFFFileName(tif));
  50892. }
  50893. const t *ptr = buf;
  50894. for (unsigned int rr = row; rr<std::min((unsigned int)(row + th),(unsigned int)ny); ++rr)
  50895. for (unsigned int cc = col; cc<std::min((unsigned int)(col + tw),(unsigned int)nx); ++cc)
  50896. for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
  50897. (*this)(cc,rr,vv) = (T)(ptr[(rr - row)*th*samplesperpixel + (cc - col)*samplesperpixel + vv]);
  50898. }
  50899. _TIFFfree(buf);
  50900. }
  50901. }
  50902. template<typename t>
  50903. void _load_tiff_tiled_separate(TIFF *const tif, const uint16_t samplesperpixel,
  50904. const uint32_t nx, const uint32_t ny, const uint32_t tw, const uint32_t th) {
  50905. t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif));
  50906. if (buf) {
  50907. for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
  50908. for (unsigned int row = 0; row<ny; row+=th)
  50909. for (unsigned int col = 0; col<nx; col+=tw) {
  50910. if (TIFFReadTile(tif,buf,col,row,0,vv)<0) {
  50911. _TIFFfree(buf); TIFFClose(tif);
  50912. throw CImgIOException(_cimg_instance
  50913. "load_tiff(): Invalid tile in file '%s'.",
  50914. cimg_instance,
  50915. TIFFFileName(tif));
  50916. }
  50917. const t *ptr = buf;
  50918. for (unsigned int rr = row; rr<std::min((unsigned int)(row + th),(unsigned int)ny); ++rr)
  50919. for (unsigned int cc = col; cc<std::min((unsigned int)(col + tw),(unsigned int)nx); ++cc)
  50920. (*this)(cc,rr,vv) = (T)*(ptr++);
  50921. }
  50922. _TIFFfree(buf);
  50923. }
  50924. }
  50925. template<typename t>
  50926. void _load_tiff_contig(TIFF *const tif, const uint16_t samplesperpixel, const uint32_t nx, const uint32_t ny) {
  50927. t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
  50928. if (buf) {
  50929. uint32_t row, rowsperstrip = (uint32_t)-1;
  50930. TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip);
  50931. for (row = 0; row<ny; row+= rowsperstrip) {
  50932. uint32_t nrow = (row + rowsperstrip>ny?ny - row:rowsperstrip);
  50933. tstrip_t strip = TIFFComputeStrip(tif, row, 0);
  50934. if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) {
  50935. _TIFFfree(buf); TIFFClose(tif);
  50936. throw CImgIOException(_cimg_instance
  50937. "load_tiff(): Invalid strip in file '%s'.",
  50938. cimg_instance,
  50939. TIFFFileName(tif));
  50940. }
  50941. const t *ptr = buf;
  50942. for (unsigned int rr = 0; rr<nrow; ++rr)
  50943. for (unsigned int cc = 0; cc<nx; ++cc)
  50944. for (unsigned int vv = 0; vv<samplesperpixel; ++vv) (*this)(cc,row + rr,vv) = (T)*(ptr++);
  50945. }
  50946. _TIFFfree(buf);
  50947. }
  50948. }
  50949. template<typename t>
  50950. void _load_tiff_separate(TIFF *const tif, const uint16_t samplesperpixel, const uint32_t nx, const uint32_t ny) {
  50951. t *buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
  50952. if (buf) {
  50953. uint32_t row, rowsperstrip = (uint32_t)-1;
  50954. TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip);
  50955. for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
  50956. for (row = 0; row<ny; row+= rowsperstrip) {
  50957. uint32_t nrow = (row + rowsperstrip>ny?ny - row:rowsperstrip);
  50958. tstrip_t strip = TIFFComputeStrip(tif, row, vv);
  50959. if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) {
  50960. _TIFFfree(buf); TIFFClose(tif);
  50961. throw CImgIOException(_cimg_instance
  50962. "load_tiff(): Invalid strip in file '%s'.",
  50963. cimg_instance,
  50964. TIFFFileName(tif));
  50965. }
  50966. const t *ptr = buf;
  50967. for (unsigned int rr = 0;rr<nrow; ++rr)
  50968. for (unsigned int cc = 0; cc<nx; ++cc)
  50969. (*this)(cc,row + rr,vv) = (T)*(ptr++);
  50970. }
  50971. _TIFFfree(buf);
  50972. }
  50973. }
  50974. CImg<T>& _load_tiff(TIFF *const tif, const unsigned int directory, unsigned int *const bits_per_value,
  50975. float *const voxel_size, CImg<charT> *const description) {
  50976. if (!TIFFSetDirectory(tif,directory)) return assign();
  50977. uint16_t samplesperpixel = 1, bitspersample = 8, photo = 0;
  50978. uint16_t sampleformat = 1;
  50979. uint32_t nx = 1, ny = 1;
  50980. const char *const filename = TIFFFileName(tif);
  50981. const bool is_spp = (bool)TIFFGetField(tif,TIFFTAG_SAMPLESPERPIXEL,&samplesperpixel);
  50982. TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&nx);
  50983. TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&ny);
  50984. TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleformat);
  50985. TIFFGetFieldDefaulted(tif,TIFFTAG_BITSPERSAMPLE,&bitspersample);
  50986. TIFFGetField(tif,TIFFTAG_PHOTOMETRIC,&photo);
  50987. if (bits_per_value) *bits_per_value = (unsigned int)bitspersample;
  50988. if (voxel_size) {
  50989. const char *s_description = 0;
  50990. float vx = 0, vy = 0, vz = 0;
  50991. if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description) {
  50992. const char *s_desc = std::strstr(s_description,"VX=");
  50993. if (s_desc && cimg_sscanf(s_desc,"VX=%f VY=%f VZ=%f",&vx,&vy,&vz)==3) { // CImg format
  50994. voxel_size[0] = vx; voxel_size[1] = vy; voxel_size[2] = vz;
  50995. }
  50996. s_desc = std::strstr(s_description,"spacing=");
  50997. if (s_desc && cimg_sscanf(s_desc,"spacing=%f",&vz)==1) { // Fiji format
  50998. voxel_size[2] = vz;
  50999. }
  51000. }
  51001. TIFFGetField(tif,TIFFTAG_XRESOLUTION,voxel_size);
  51002. TIFFGetField(tif,TIFFTAG_YRESOLUTION,voxel_size + 1);
  51003. voxel_size[0] = 1.f/voxel_size[0];
  51004. voxel_size[1] = 1.f/voxel_size[1];
  51005. }
  51006. if (description) {
  51007. const char *s_description = 0;
  51008. if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description)
  51009. CImg<charT>::string(s_description).move_to(*description);
  51010. }
  51011. const unsigned int spectrum = !is_spp || photo>=3?(photo>1?3:1):samplesperpixel;
  51012. assign(nx,ny,1,spectrum);
  51013. if ((photo>=3 && sampleformat==1 &&
  51014. (bitspersample==4 || bitspersample==8) &&
  51015. (samplesperpixel==1 || samplesperpixel==3 || samplesperpixel==4)) ||
  51016. (bitspersample==1 && samplesperpixel==1)) {
  51017. // Special case for unsigned color images.
  51018. uint32_t *const raster = (uint32_t*)_TIFFmalloc(nx*ny*sizeof(uint32_t));
  51019. if (!raster) {
  51020. _TIFFfree(raster); TIFFClose(tif);
  51021. throw CImgException(_cimg_instance
  51022. "load_tiff(): Failed to allocate memory (%s) for file '%s'.",
  51023. cimg_instance,
  51024. cimg::strbuffersize(nx*ny*sizeof(uint32_t)),filename);
  51025. }
  51026. TIFFReadRGBAImage(tif,nx,ny,raster,0);
  51027. switch (spectrum) {
  51028. case 1 :
  51029. cimg_forXY(*this,x,y)
  51030. (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 -y) + x]);
  51031. break;
  51032. case 3 :
  51033. cimg_forXY(*this,x,y) {
  51034. (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 - y) + x]);
  51035. (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny - 1 - y) + x]);
  51036. (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny - 1 - y) + x]);
  51037. }
  51038. break;
  51039. case 4 :
  51040. cimg_forXY(*this,x,y) {
  51041. (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 - y) + x]);
  51042. (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny - 1 - y) + x]);
  51043. (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny - 1 - y) + x]);
  51044. (*this)(x,y,3) = (T)(float)TIFFGetA(raster[nx*(ny - 1 - y) + x]);
  51045. }
  51046. break;
  51047. }
  51048. _TIFFfree(raster);
  51049. } else { // Other cases
  51050. uint16_t config;
  51051. TIFFGetField(tif,TIFFTAG_PLANARCONFIG,&config);
  51052. if (TIFFIsTiled(tif)) {
  51053. uint32_t tw = 1, th = 1;
  51054. TIFFGetField(tif,TIFFTAG_TILEWIDTH,&tw);
  51055. TIFFGetField(tif,TIFFTAG_TILELENGTH,&th);
  51056. if (config==PLANARCONFIG_CONTIG) switch (bitspersample) {
  51057. case 8 :
  51058. if (sampleformat==SAMPLEFORMAT_UINT)
  51059. _load_tiff_tiled_contig<unsigned char>(tif,samplesperpixel,nx,ny,tw,th);
  51060. else _load_tiff_tiled_contig<signed char>(tif,samplesperpixel,nx,ny,tw,th);
  51061. break;
  51062. case 16 :
  51063. if (sampleformat==SAMPLEFORMAT_UINT)
  51064. _load_tiff_tiled_contig<unsigned short>(tif,samplesperpixel,nx,ny,tw,th);
  51065. else _load_tiff_tiled_contig<short>(tif,samplesperpixel,nx,ny,tw,th);
  51066. break;
  51067. case 32 :
  51068. if (sampleformat==SAMPLEFORMAT_UINT)
  51069. _load_tiff_tiled_contig<unsigned int>(tif,samplesperpixel,nx,ny,tw,th);
  51070. else if (sampleformat==SAMPLEFORMAT_INT)
  51071. _load_tiff_tiled_contig<int>(tif,samplesperpixel,nx,ny,tw,th);
  51072. else _load_tiff_tiled_contig<float>(tif,samplesperpixel,nx,ny,tw,th);
  51073. break;
  51074. case 64 :
  51075. if (sampleformat==SAMPLEFORMAT_UINT)
  51076. _load_tiff_tiled_contig<uint64T>(tif,samplesperpixel,nx,ny,tw,th);
  51077. else if (sampleformat==SAMPLEFORMAT_INT)
  51078. _load_tiff_tiled_contig<int64T>(tif,samplesperpixel,nx,ny,tw,th);
  51079. else _load_tiff_tiled_contig<double>(tif,samplesperpixel,nx,ny,tw,th);
  51080. break;
  51081. } else switch (bitspersample) {
  51082. case 8 :
  51083. if (sampleformat==SAMPLEFORMAT_UINT)
  51084. _load_tiff_tiled_separate<unsigned char>(tif,samplesperpixel,nx,ny,tw,th);
  51085. else _load_tiff_tiled_separate<signed char>(tif,samplesperpixel,nx,ny,tw,th);
  51086. break;
  51087. case 16 :
  51088. if (sampleformat==SAMPLEFORMAT_UINT)
  51089. _load_tiff_tiled_separate<unsigned short>(tif,samplesperpixel,nx,ny,tw,th);
  51090. else _load_tiff_tiled_separate<short>(tif,samplesperpixel,nx,ny,tw,th);
  51091. break;
  51092. case 32 :
  51093. if (sampleformat==SAMPLEFORMAT_UINT)
  51094. _load_tiff_tiled_separate<unsigned int>(tif,samplesperpixel,nx,ny,tw,th);
  51095. else if (sampleformat==SAMPLEFORMAT_INT)
  51096. _load_tiff_tiled_separate<int>(tif,samplesperpixel,nx,ny,tw,th);
  51097. else _load_tiff_tiled_separate<float>(tif,samplesperpixel,nx,ny,tw,th);
  51098. break;
  51099. case 64 :
  51100. if (sampleformat==SAMPLEFORMAT_UINT)
  51101. _load_tiff_tiled_separate<uint64T>(tif,samplesperpixel,nx,ny,tw,th);
  51102. else if (sampleformat==SAMPLEFORMAT_INT)
  51103. _load_tiff_tiled_separate<int64T>(tif,samplesperpixel,nx,ny,tw,th);
  51104. else _load_tiff_tiled_separate<double>(tif,samplesperpixel,nx,ny,tw,th);
  51105. break;
  51106. }
  51107. } else {
  51108. if (config==PLANARCONFIG_CONTIG) switch (bitspersample) {
  51109. case 8 :
  51110. if (sampleformat==SAMPLEFORMAT_UINT)
  51111. _load_tiff_contig<unsigned char>(tif,samplesperpixel,nx,ny);
  51112. else _load_tiff_contig<signed char>(tif,samplesperpixel,nx,ny);
  51113. break;
  51114. case 16 :
  51115. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<unsigned short>(tif,samplesperpixel,nx,ny);
  51116. else _load_tiff_contig<short>(tif,samplesperpixel,nx,ny);
  51117. break;
  51118. case 32 :
  51119. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<unsigned int>(tif,samplesperpixel,nx,ny);
  51120. else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig<int>(tif,samplesperpixel,nx,ny);
  51121. else _load_tiff_contig<float>(tif,samplesperpixel,nx,ny);
  51122. break;
  51123. case 64 :
  51124. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<uint64T>(tif,samplesperpixel,nx,ny);
  51125. else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig<int64T>(tif,samplesperpixel,nx,ny);
  51126. else _load_tiff_contig<double>(tif,samplesperpixel,nx,ny);
  51127. break;
  51128. } else switch (bitspersample) {
  51129. case 8 :
  51130. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned char>(tif,samplesperpixel,nx,ny);
  51131. else _load_tiff_separate<signed char>(tif,samplesperpixel,nx,ny);
  51132. break;
  51133. case 16 :
  51134. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned short>(tif,samplesperpixel,nx,ny);
  51135. else _load_tiff_separate<short>(tif,samplesperpixel,nx,ny);
  51136. break;
  51137. case 32 :
  51138. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned int>(tif,samplesperpixel,nx,ny);
  51139. else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate<int>(tif,samplesperpixel,nx,ny);
  51140. else _load_tiff_separate<float>(tif,samplesperpixel,nx,ny);
  51141. break;
  51142. case 64 :
  51143. if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<uint64T>(tif,samplesperpixel,nx,ny);
  51144. else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate<int64T>(tif,samplesperpixel,nx,ny);
  51145. else _load_tiff_separate<double>(tif,samplesperpixel,nx,ny);
  51146. break;
  51147. }
  51148. }
  51149. }
  51150. return *this;
  51151. }
  51152. #endif
  51153. //! Load image from a MINC2 file.
  51154. /**
  51155. \param filename Filename, as a C-string.
  51156. **/
  51157. // (Original code by Haz-Edine Assemlal).
  51158. CImg<T>& load_minc2(const char *const filename) {
  51159. if (!filename)
  51160. throw CImgArgumentException(_cimg_instance
  51161. "load_minc2(): Specified filename is (null).",
  51162. cimg_instance);
  51163. #ifndef cimg_use_minc2
  51164. return load_other(filename);
  51165. #else
  51166. minc::minc_1_reader rdr;
  51167. rdr.open(filename);
  51168. assign(rdr.ndim(1)?rdr.ndim(1):1,
  51169. rdr.ndim(2)?rdr.ndim(2):1,
  51170. rdr.ndim(3)?rdr.ndim(3):1,
  51171. rdr.ndim(4)?rdr.ndim(4):1);
  51172. if (pixel_type()==cimg::type<unsigned char>::string())
  51173. rdr.setup_read_byte();
  51174. else if (pixel_type()==cimg::type<int>::string())
  51175. rdr.setup_read_int();
  51176. else if (pixel_type()==cimg::type<double>::string())
  51177. rdr.setup_read_double();
  51178. else
  51179. rdr.setup_read_float();
  51180. minc::load_standard_volume(rdr,this->_data);
  51181. return *this;
  51182. #endif
  51183. }
  51184. //! Load image from a MINC2 file \newinstance.
  51185. static CImg<T> get_load_minc2(const char *const filename) {
  51186. return CImg<T>().load_analyze(filename);
  51187. }
  51188. //! Load image from an ANALYZE7.5/NIFTI file.
  51189. /**
  51190. \param filename Filename, as a C-string.
  51191. \param[out] voxel_size Pointer to the three voxel sizes read from the file.
  51192. **/
  51193. CImg<T>& load_analyze(const char *const filename, float *const voxel_size=0) {
  51194. return _load_analyze(0,filename,voxel_size);
  51195. }
  51196. //! Load image from an ANALYZE7.5/NIFTI file \newinstance.
  51197. static CImg<T> get_load_analyze(const char *const filename, float *const voxel_size=0) {
  51198. return CImg<T>().load_analyze(filename,voxel_size);
  51199. }
  51200. //! Load image from an ANALYZE7.5/NIFTI file \overloading.
  51201. CImg<T>& load_analyze(std::FILE *const file, float *const voxel_size=0) {
  51202. return _load_analyze(file,0,voxel_size);
  51203. }
  51204. //! Load image from an ANALYZE7.5/NIFTI file \newinstance.
  51205. static CImg<T> get_load_analyze(std::FILE *const file, float *const voxel_size=0) {
  51206. return CImg<T>().load_analyze(file,voxel_size);
  51207. }
  51208. CImg<T>& _load_analyze(std::FILE *const file, const char *const filename, float *const voxel_size=0) {
  51209. if (!file && !filename)
  51210. throw CImgArgumentException(_cimg_instance
  51211. "load_analyze(): Specified filename is (null).",
  51212. cimg_instance);
  51213. std::FILE *nfile_header = 0, *nfile = 0;
  51214. if (!file) {
  51215. CImg<charT> body(1024);
  51216. const char *const ext = cimg::split_filename(filename,body);
  51217. if (!cimg::strcasecmp(ext,"hdr")) { // File is an Analyze header file
  51218. nfile_header = cimg::fopen(filename,"rb");
  51219. cimg_sprintf(body._data + std::strlen(body),".img");
  51220. nfile = cimg::fopen(body,"rb");
  51221. } else if (!cimg::strcasecmp(ext,"img")) { // File is an Analyze data file
  51222. nfile = cimg::fopen(filename,"rb");
  51223. cimg_sprintf(body._data + std::strlen(body),".hdr");
  51224. nfile_header = cimg::fopen(body,"rb");
  51225. } else nfile_header = nfile = cimg::fopen(filename,"rb"); // File is a Niftii file
  51226. } else nfile_header = nfile = file; // File is a Niftii file
  51227. if (!nfile || !nfile_header)
  51228. throw CImgIOException(_cimg_instance
  51229. "load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.",
  51230. cimg_instance,
  51231. filename?filename:"(FILE*)");
  51232. // Read header.
  51233. bool endian = false;
  51234. unsigned int header_size;
  51235. cimg::fread(&header_size,1,nfile_header);
  51236. if (!header_size)
  51237. throw CImgIOException(_cimg_instance
  51238. "load_analyze(): Invalid zero-size header in file '%s'.",
  51239. cimg_instance,
  51240. filename?filename:"(FILE*)");
  51241. if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); }
  51242. unsigned char *const header = new unsigned char[header_size];
  51243. cimg::fread(header + 4,header_size - 4,nfile_header);
  51244. if (!file && nfile_header!=nfile) cimg::fclose(nfile_header);
  51245. if (endian) {
  51246. cimg::invert_endianness((short*)(header + 40),5);
  51247. cimg::invert_endianness((short*)(header + 70),1);
  51248. cimg::invert_endianness((short*)(header + 72),1);
  51249. cimg::invert_endianness((float*)(header + 76),4);
  51250. cimg::invert_endianness((float*)(header + 108),1);
  51251. cimg::invert_endianness((float*)(header + 112),1);
  51252. }
  51253. if (nfile_header==nfile) {
  51254. const unsigned int vox_offset = (unsigned int)*(float*)(header + 108);
  51255. std::fseek(nfile,vox_offset,SEEK_SET);
  51256. }
  51257. unsigned short *dim = (unsigned short*)(header + 40), dimx = 1, dimy = 1, dimz = 1, dimv = 1;
  51258. if (!dim[0])
  51259. cimg::warn(_cimg_instance
  51260. "load_analyze(): File '%s' defines an image with zero dimensions.",
  51261. cimg_instance,
  51262. filename?filename:"(FILE*)");
  51263. if (dim[0]>4)
  51264. cimg::warn(_cimg_instance
  51265. "load_analyze(): File '%s' defines an image with %u dimensions, reading only the 4 first.",
  51266. cimg_instance,
  51267. filename?filename:"(FILE*)",dim[0]);
  51268. if (dim[0]>=1) dimx = dim[1];
  51269. if (dim[0]>=2) dimy = dim[2];
  51270. if (dim[0]>=3) dimz = dim[3];
  51271. if (dim[0]>=4) dimv = dim[4];
  51272. float scalefactor = *(float*)(header + 112); if (scalefactor==0) scalefactor = 1;
  51273. const unsigned short datatype = *(unsigned short*)(header + 70);
  51274. if (voxel_size) {
  51275. const float *vsize = (float*)(header + 76);
  51276. voxel_size[0] = vsize[1]; voxel_size[1] = vsize[2]; voxel_size[2] = vsize[3];
  51277. }
  51278. delete[] header;
  51279. // Read pixel data.
  51280. assign(dimx,dimy,dimz,dimv);
  51281. const size_t pdim = (size_t)dimx*dimy*dimz*dimv;
  51282. switch (datatype) {
  51283. case 2 : {
  51284. unsigned char *const buffer = new unsigned char[pdim];
  51285. cimg::fread(buffer,pdim,nfile);
  51286. cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
  51287. delete[] buffer;
  51288. } break;
  51289. case 4 : {
  51290. short *const buffer = new short[pdim];
  51291. cimg::fread(buffer,pdim,nfile);
  51292. if (endian) cimg::invert_endianness(buffer,pdim);
  51293. cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
  51294. delete[] buffer;
  51295. } break;
  51296. case 8 : {
  51297. int *const buffer = new int[pdim];
  51298. cimg::fread(buffer,pdim,nfile);
  51299. if (endian) cimg::invert_endianness(buffer,pdim);
  51300. cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
  51301. delete[] buffer;
  51302. } break;
  51303. case 16 : {
  51304. float *const buffer = new float[pdim];
  51305. cimg::fread(buffer,pdim,nfile);
  51306. if (endian) cimg::invert_endianness(buffer,pdim);
  51307. cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
  51308. delete[] buffer;
  51309. } break;
  51310. case 64 : {
  51311. double *const buffer = new double[pdim];
  51312. cimg::fread(buffer,pdim,nfile);
  51313. if (endian) cimg::invert_endianness(buffer,pdim);
  51314. cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
  51315. delete[] buffer;
  51316. } break;
  51317. default :
  51318. if (!file) cimg::fclose(nfile);
  51319. throw CImgIOException(_cimg_instance
  51320. "load_analyze(): Unable to load datatype %d in file '%s'",
  51321. cimg_instance,
  51322. datatype,filename?filename:"(FILE*)");
  51323. }
  51324. if (!file) cimg::fclose(nfile);
  51325. return *this;
  51326. }
  51327. //! Load image from a .cimg[z] file.
  51328. /**
  51329. \param filename Filename, as a C-string.
  51330. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  51331. \param align Appending alignment.
  51332. **/
  51333. CImg<T>& load_cimg(const char *const filename, const char axis='z', const float align=0) {
  51334. CImgList<T> list;
  51335. list.load_cimg(filename);
  51336. if (list._width==1) return list[0].move_to(*this);
  51337. return assign(list.get_append(axis,align));
  51338. }
  51339. //! Load image from a .cimg[z] file \newinstance
  51340. static CImg<T> get_load_cimg(const char *const filename, const char axis='z', const float align=0) {
  51341. return CImg<T>().load_cimg(filename,axis,align);
  51342. }
  51343. //! Load image from a .cimg[z] file \overloading.
  51344. CImg<T>& load_cimg(std::FILE *const file, const char axis='z', const float align=0) {
  51345. CImgList<T> list;
  51346. list.load_cimg(file);
  51347. if (list._width==1) return list[0].move_to(*this);
  51348. return assign(list.get_append(axis,align));
  51349. }
  51350. //! Load image from a .cimg[z] file \newinstance
  51351. static CImg<T> get_load_cimg(std::FILE *const file, const char axis='z', const float align=0) {
  51352. return CImg<T>().load_cimg(file,axis,align);
  51353. }
  51354. //! Load sub-images of a .cimg file.
  51355. /**
  51356. \param filename Filename, as a C-string.
  51357. \param n0 Starting frame.
  51358. \param n1 Ending frame (~0U for max).
  51359. \param x0 X-coordinate of the starting sub-image vertex.
  51360. \param y0 Y-coordinate of the starting sub-image vertex.
  51361. \param z0 Z-coordinate of the starting sub-image vertex.
  51362. \param c0 C-coordinate of the starting sub-image vertex.
  51363. \param x1 X-coordinate of the ending sub-image vertex (~0U for max).
  51364. \param y1 Y-coordinate of the ending sub-image vertex (~0U for max).
  51365. \param z1 Z-coordinate of the ending sub-image vertex (~0U for max).
  51366. \param c1 C-coordinate of the ending sub-image vertex (~0U for max).
  51367. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  51368. \param align Appending alignment.
  51369. **/
  51370. CImg<T>& load_cimg(const char *const filename,
  51371. const unsigned int n0, const unsigned int n1,
  51372. const unsigned int x0, const unsigned int y0,
  51373. const unsigned int z0, const unsigned int c0,
  51374. const unsigned int x1, const unsigned int y1,
  51375. const unsigned int z1, const unsigned int c1,
  51376. const char axis='z', const float align=0) {
  51377. CImgList<T> list;
  51378. list.load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  51379. if (list._width==1) return list[0].move_to(*this);
  51380. return assign(list.get_append(axis,align));
  51381. }
  51382. //! Load sub-images of a .cimg file \newinstance.
  51383. static CImg<T> get_load_cimg(const char *const filename,
  51384. const unsigned int n0, const unsigned int n1,
  51385. const unsigned int x0, const unsigned int y0,
  51386. const unsigned int z0, const unsigned int c0,
  51387. const unsigned int x1, const unsigned int y1,
  51388. const unsigned int z1, const unsigned int c1,
  51389. const char axis='z', const float align=0) {
  51390. return CImg<T>().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align);
  51391. }
  51392. //! Load sub-images of a .cimg file \overloading.
  51393. CImg<T>& load_cimg(std::FILE *const file,
  51394. const unsigned int n0, const unsigned int n1,
  51395. const unsigned int x0, const unsigned int y0,
  51396. const unsigned int z0, const unsigned int c0,
  51397. const unsigned int x1, const unsigned int y1,
  51398. const unsigned int z1, const unsigned int c1,
  51399. const char axis='z', const float align=0) {
  51400. CImgList<T> list;
  51401. list.load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  51402. if (list._width==1) return list[0].move_to(*this);
  51403. return assign(list.get_append(axis,align));
  51404. }
  51405. //! Load sub-images of a .cimg file \newinstance.
  51406. static CImg<T> get_load_cimg(std::FILE *const file,
  51407. const unsigned int n0, const unsigned int n1,
  51408. const unsigned int x0, const unsigned int y0,
  51409. const unsigned int z0, const unsigned int c0,
  51410. const unsigned int x1, const unsigned int y1,
  51411. const unsigned int z1, const unsigned int c1,
  51412. const char axis='z', const float align=0) {
  51413. return CImg<T>().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align);
  51414. }
  51415. //! Load image from an INRIMAGE-4 file.
  51416. /**
  51417. \param filename Filename, as a C-string.
  51418. \param[out] voxel_size Pointer to the three voxel sizes read from the file.
  51419. **/
  51420. CImg<T>& load_inr(const char *const filename, float *const voxel_size=0) {
  51421. return _load_inr(0,filename,voxel_size);
  51422. }
  51423. //! Load image from an INRIMAGE-4 file \newinstance.
  51424. static CImg<T> get_load_inr(const char *const filename, float *const voxel_size=0) {
  51425. return CImg<T>().load_inr(filename,voxel_size);
  51426. }
  51427. //! Load image from an INRIMAGE-4 file \overloading.
  51428. CImg<T>& load_inr(std::FILE *const file, float *const voxel_size=0) {
  51429. return _load_inr(file,0,voxel_size);
  51430. }
  51431. //! Load image from an INRIMAGE-4 file \newinstance.
  51432. static CImg<T> get_load_inr(std::FILE *const file, float *voxel_size=0) {
  51433. return CImg<T>().load_inr(file,voxel_size);
  51434. }
  51435. static void _load_inr_header(std::FILE *file, int out[8], float *const voxel_size) {
  51436. CImg<charT> item(1024), tmp1(64), tmp2(64);
  51437. *item = *tmp1 = *tmp2 = 0;
  51438. out[0] = std::fscanf(file,"%63s",item._data);
  51439. out[0] = out[1] = out[2] = out[3] = out[5] = 1; out[4] = out[6] = out[7] = -1;
  51440. if (cimg::strncasecmp(item,"#INRIMAGE-4#{",13)!=0)
  51441. throw CImgIOException("CImg<%s>::load_inr(): INRIMAGE-4 header not found.",
  51442. pixel_type());
  51443. while (std::fscanf(file," %63[^\n]%*c",item._data)!=EOF && std::strncmp(item,"##}",3)) {
  51444. cimg_sscanf(item," XDIM%*[^0-9]%d",out);
  51445. cimg_sscanf(item," YDIM%*[^0-9]%d",out + 1);
  51446. cimg_sscanf(item," ZDIM%*[^0-9]%d",out + 2);
  51447. cimg_sscanf(item," VDIM%*[^0-9]%d",out + 3);
  51448. cimg_sscanf(item," PIXSIZE%*[^0-9]%d",out + 6);
  51449. if (voxel_size) {
  51450. cimg_sscanf(item," VX%*[^0-9.+-]%f",voxel_size);
  51451. cimg_sscanf(item," VY%*[^0-9.+-]%f",voxel_size + 1);
  51452. cimg_sscanf(item," VZ%*[^0-9.+-]%f",voxel_size + 2);
  51453. }
  51454. if (cimg_sscanf(item," CPU%*[ =]%s",tmp1._data)) out[7] = cimg::strncasecmp(tmp1,"sun",3)?0:1;
  51455. switch (cimg_sscanf(item," TYPE%*[ =]%s %s",tmp1._data,tmp2._data)) {
  51456. case 0 : break;
  51457. case 2 :
  51458. out[5] = cimg::strncasecmp(tmp1,"unsigned",8)?1:0;
  51459. std::strncpy(tmp1,tmp2,tmp1._width - 1); // fallthrough
  51460. case 1 :
  51461. if (!cimg::strncasecmp(tmp1,"int",3) || !cimg::strncasecmp(tmp1,"fixed",5)) out[4] = 0;
  51462. if (!cimg::strncasecmp(tmp1,"float",5) || !cimg::strncasecmp(tmp1,"double",6)) out[4] = 1;
  51463. if (!cimg::strncasecmp(tmp1,"packed",6)) out[4] = 2;
  51464. if (out[4]>=0) break; // fallthrough
  51465. default :
  51466. throw CImgIOException("CImg<%s>::load_inr(): Invalid pixel type '%s' defined in header.",
  51467. pixel_type(),
  51468. tmp2._data);
  51469. }
  51470. }
  51471. if (out[0]<0 || out[1]<0 || out[2]<0 || out[3]<0)
  51472. throw CImgIOException("CImg<%s>::load_inr(): Invalid dimensions (%d,%d,%d,%d) defined in header.",
  51473. pixel_type(),
  51474. out[0],out[1],out[2],out[3]);
  51475. if (out[4]<0 || out[5]<0)
  51476. throw CImgIOException("CImg<%s>::load_inr(): Incomplete pixel type defined in header.",
  51477. pixel_type());
  51478. if (out[6]<0)
  51479. throw CImgIOException("CImg<%s>::load_inr(): Incomplete PIXSIZE field defined in header.",
  51480. pixel_type());
  51481. if (out[7]<0)
  51482. throw CImgIOException("CImg<%s>::load_inr(): Big/Little Endian coding type undefined in header.",
  51483. pixel_type());
  51484. }
  51485. CImg<T>& _load_inr(std::FILE *const file, const char *const filename, float *const voxel_size) {
  51486. #define _cimg_load_inr_case(Tf,sign,pixsize,Ts) \
  51487. if (!loaded && fopt[6]==pixsize && fopt[4]==Tf && fopt[5]==sign) { \
  51488. Ts *xval, *const val = new Ts[(size_t)fopt[0]*fopt[3]]; \
  51489. cimg_forYZ(*this,y,z) { \
  51490. cimg::fread(val,fopt[0]*fopt[3],nfile); \
  51491. if (fopt[7]!=endian) cimg::invert_endianness(val,fopt[0]*fopt[3]); \
  51492. xval = val; cimg_forX(*this,x) cimg_forC(*this,c) (*this)(x,y,z,c) = (T)*(xval++); \
  51493. } \
  51494. delete[] val; \
  51495. loaded = true; \
  51496. }
  51497. if (!file && !filename)
  51498. throw CImgArgumentException(_cimg_instance
  51499. "load_inr(): Specified filename is (null).",
  51500. cimg_instance);
  51501. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  51502. int fopt[8], endian = cimg::endianness()?1:0;
  51503. bool loaded = false;
  51504. if (voxel_size) voxel_size[0] = voxel_size[1] = voxel_size[2] = 1;
  51505. _load_inr_header(nfile,fopt,voxel_size);
  51506. assign(fopt[0],fopt[1],fopt[2],fopt[3]);
  51507. _cimg_load_inr_case(0,0,8,unsigned char);
  51508. _cimg_load_inr_case(0,1,8,char);
  51509. _cimg_load_inr_case(0,0,16,unsigned short);
  51510. _cimg_load_inr_case(0,1,16,short);
  51511. _cimg_load_inr_case(0,0,32,unsigned int);
  51512. _cimg_load_inr_case(0,1,32,int);
  51513. _cimg_load_inr_case(1,0,32,float);
  51514. _cimg_load_inr_case(1,1,32,float);
  51515. _cimg_load_inr_case(1,0,64,double);
  51516. _cimg_load_inr_case(1,1,64,double);
  51517. if (!loaded) {
  51518. if (!file) cimg::fclose(nfile);
  51519. throw CImgIOException(_cimg_instance
  51520. "load_inr(): Unknown pixel type defined in file '%s'.",
  51521. cimg_instance,
  51522. filename?filename:"(FILE*)");
  51523. }
  51524. if (!file) cimg::fclose(nfile);
  51525. return *this;
  51526. }
  51527. //! Load image from a EXR file.
  51528. /**
  51529. \param filename Filename, as a C-string.
  51530. **/
  51531. CImg<T>& load_exr(const char *const filename) {
  51532. if (!filename)
  51533. throw CImgArgumentException(_cimg_instance
  51534. "load_exr(): Specified filename is (null).",
  51535. cimg_instance);
  51536. #if defined(cimg_use_openexr)
  51537. Imf::RgbaInputFile file(filename);
  51538. Imath::Box2i dw = file.dataWindow();
  51539. const int
  51540. inwidth = dw.max.x - dw.min.x + 1,
  51541. inheight = dw.max.y - dw.min.y + 1;
  51542. Imf::Array2D<Imf::Rgba> pixels;
  51543. pixels.resizeErase(inheight,inwidth);
  51544. file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y*inwidth, 1, inwidth);
  51545. file.readPixels(dw.min.y, dw.max.y);
  51546. assign(inwidth,inheight,1,4);
  51547. T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3);
  51548. cimg_forXY(*this,x,y) {
  51549. *(ptr_r++) = (T)pixels[y][x].r;
  51550. *(ptr_g++) = (T)pixels[y][x].g;
  51551. *(ptr_b++) = (T)pixels[y][x].b;
  51552. *(ptr_a++) = (T)pixels[y][x].a;
  51553. }
  51554. return *this;
  51555. #elif defined(cimg_use_tinyexr)
  51556. float *res;
  51557. const char *err = 0;
  51558. int width = 0, height = 0;
  51559. const int ret = LoadEXR(&res,&width,&height,filename,&err);
  51560. if (ret) throw CImgIOException(_cimg_instance
  51561. "load_exr(): Unable to load EXR file '%s'.",
  51562. cimg_instance,filename);
  51563. CImg<floatT>(res,4,width,height,1,true).get_permute_axes("yzcx").move_to(*this);
  51564. std::free(res);
  51565. return *this;
  51566. #else
  51567. return load_other(filename);
  51568. #endif
  51569. }
  51570. //! Load image from a EXR file \newinstance.
  51571. static CImg<T> get_load_exr(const char *const filename) {
  51572. return CImg<T>().load_exr(filename);
  51573. }
  51574. //! Load image from a PANDORE-5 file.
  51575. /**
  51576. \param filename Filename, as a C-string.
  51577. **/
  51578. CImg<T>& load_pandore(const char *const filename) {
  51579. return _load_pandore(0,filename);
  51580. }
  51581. //! Load image from a PANDORE-5 file \newinstance.
  51582. static CImg<T> get_load_pandore(const char *const filename) {
  51583. return CImg<T>().load_pandore(filename);
  51584. }
  51585. //! Load image from a PANDORE-5 file \overloading.
  51586. CImg<T>& load_pandore(std::FILE *const file) {
  51587. return _load_pandore(file,0);
  51588. }
  51589. //! Load image from a PANDORE-5 file \newinstance.
  51590. static CImg<T> get_load_pandore(std::FILE *const file) {
  51591. return CImg<T>().load_pandore(file);
  51592. }
  51593. CImg<T>& _load_pandore(std::FILE *const file, const char *const filename) {
  51594. #define __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,ndim,stype) \
  51595. cimg::fread(dims,nbdim,nfile); \
  51596. if (endian) cimg::invert_endianness(dims,nbdim); \
  51597. assign(nwidth,nheight,ndepth,ndim); \
  51598. const size_t siz = size(); \
  51599. stype *buffer = new stype[siz]; \
  51600. cimg::fread(buffer,siz,nfile); \
  51601. if (endian) cimg::invert_endianness(buffer,siz); \
  51602. T *ptrd = _data; \
  51603. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); \
  51604. buffer-=siz; \
  51605. delete[] buffer
  51606. #define _cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1,stype2,stype3,ltype) { \
  51607. if (sizeof(stype1)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1); } \
  51608. else if (sizeof(stype2)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype2); } \
  51609. else if (sizeof(stype3)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype3); } \
  51610. else throw CImgIOException(_cimg_instance \
  51611. "load_pandore(): Unknown pixel datatype in file '%s'.", \
  51612. cimg_instance, \
  51613. filename?filename:"(FILE*)"); }
  51614. if (!file && !filename)
  51615. throw CImgArgumentException(_cimg_instance
  51616. "load_pandore(): Specified filename is (null).",
  51617. cimg_instance);
  51618. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  51619. CImg<charT> header(32);
  51620. cimg::fread(header._data,12,nfile);
  51621. if (cimg::strncasecmp("PANDORE",header,7)) {
  51622. if (!file) cimg::fclose(nfile);
  51623. throw CImgIOException(_cimg_instance
  51624. "load_pandore(): PANDORE header not found in file '%s'.",
  51625. cimg_instance,
  51626. filename?filename:"(FILE*)");
  51627. }
  51628. unsigned int imageid, dims[8] = { 0 };
  51629. int ptbuf[4] = { 0 };
  51630. cimg::fread(&imageid,1,nfile);
  51631. const bool endian = imageid>255;
  51632. if (endian) cimg::invert_endianness(imageid);
  51633. cimg::fread(header._data,20,nfile);
  51634. switch (imageid) {
  51635. case 2 : _cimg_load_pandore_case(2,dims[1],1,1,1,unsigned char,unsigned char,unsigned char,1); break;
  51636. case 3 : _cimg_load_pandore_case(2,dims[1],1,1,1,long,int,short,4); break;
  51637. case 4 : _cimg_load_pandore_case(2,dims[1],1,1,1,double,float,float,4); break;
  51638. case 5 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,unsigned char,unsigned char,unsigned char,1); break;
  51639. case 6 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,long,int,short,4); break;
  51640. case 7 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,double,float,float,4); break;
  51641. case 8 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,unsigned char,unsigned char,unsigned char,1); break;
  51642. case 9 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,long,int,short,4); break;
  51643. case 10 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,double,float,float,4); break;
  51644. case 11 : { // Region 1D
  51645. cimg::fread(dims,3,nfile);
  51646. if (endian) cimg::invert_endianness(dims,3);
  51647. assign(dims[1],1,1,1);
  51648. const unsigned siz = size();
  51649. if (dims[2]<256) {
  51650. unsigned char *buffer = new unsigned char[siz];
  51651. cimg::fread(buffer,siz,nfile);
  51652. T *ptrd = _data;
  51653. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51654. buffer-=siz;
  51655. delete[] buffer;
  51656. } else {
  51657. if (dims[2]<65536) {
  51658. unsigned short *buffer = new unsigned short[siz];
  51659. cimg::fread(buffer,siz,nfile);
  51660. if (endian) cimg::invert_endianness(buffer,siz);
  51661. T *ptrd = _data;
  51662. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51663. buffer-=siz;
  51664. delete[] buffer;
  51665. } else {
  51666. unsigned int *buffer = new unsigned int[siz];
  51667. cimg::fread(buffer,siz,nfile);
  51668. if (endian) cimg::invert_endianness(buffer,siz);
  51669. T *ptrd = _data;
  51670. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51671. buffer-=siz;
  51672. delete[] buffer;
  51673. }
  51674. }
  51675. }
  51676. break;
  51677. case 12 : { // Region 2D
  51678. cimg::fread(dims,4,nfile);
  51679. if (endian) cimg::invert_endianness(dims,4);
  51680. assign(dims[2],dims[1],1,1);
  51681. const size_t siz = size();
  51682. if (dims[3]<256) {
  51683. unsigned char *buffer = new unsigned char[siz];
  51684. cimg::fread(buffer,siz,nfile);
  51685. T *ptrd = _data;
  51686. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51687. buffer-=siz;
  51688. delete[] buffer;
  51689. } else {
  51690. if (dims[3]<65536) {
  51691. unsigned short *buffer = new unsigned short[siz];
  51692. cimg::fread(buffer,siz,nfile);
  51693. if (endian) cimg::invert_endianness(buffer,siz);
  51694. T *ptrd = _data;
  51695. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51696. buffer-=siz;
  51697. delete[] buffer;
  51698. } else {
  51699. unsigned int *buffer = new unsigned int[siz];
  51700. cimg::fread(buffer,siz,nfile);
  51701. if (endian) cimg::invert_endianness(buffer,siz);
  51702. T *ptrd = _data;
  51703. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51704. buffer-=siz;
  51705. delete[] buffer;
  51706. }
  51707. }
  51708. }
  51709. break;
  51710. case 13 : { // Region 3D
  51711. cimg::fread(dims,5,nfile);
  51712. if (endian) cimg::invert_endianness(dims,5);
  51713. assign(dims[3],dims[2],dims[1],1);
  51714. const size_t siz = size();
  51715. if (dims[4]<256) {
  51716. unsigned char *buffer = new unsigned char[siz];
  51717. cimg::fread(buffer,siz,nfile);
  51718. T *ptrd = _data;
  51719. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51720. buffer-=siz;
  51721. delete[] buffer;
  51722. } else {
  51723. if (dims[4]<65536) {
  51724. unsigned short *buffer = new unsigned short[siz];
  51725. cimg::fread(buffer,siz,nfile);
  51726. if (endian) cimg::invert_endianness(buffer,siz);
  51727. T *ptrd = _data;
  51728. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51729. buffer-=siz;
  51730. delete[] buffer;
  51731. } else {
  51732. unsigned int *buffer = new unsigned int[siz];
  51733. cimg::fread(buffer,siz,nfile);
  51734. if (endian) cimg::invert_endianness(buffer,siz);
  51735. T *ptrd = _data;
  51736. cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
  51737. buffer-=siz;
  51738. delete[] buffer;
  51739. }
  51740. }
  51741. }
  51742. break;
  51743. case 16 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,unsigned char,unsigned char,unsigned char,1); break;
  51744. case 17 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,long,int,short,4); break;
  51745. case 18 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,double,float,float,4); break;
  51746. case 19 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,unsigned char,unsigned char,unsigned char,1); break;
  51747. case 20 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,long,int,short,4); break;
  51748. case 21 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,double,float,float,4); break;
  51749. case 22 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned char,unsigned char,unsigned char,1); break;
  51750. case 23 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],long,int,short,4); break;
  51751. case 24 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned long,unsigned int,unsigned short,4); break;
  51752. case 25 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],double,float,float,4); break;
  51753. case 26 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned char,unsigned char,unsigned char,1); break;
  51754. case 27 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],long,int,short,4); break;
  51755. case 28 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned long,unsigned int,unsigned short,4); break;
  51756. case 29 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],double,float,float,4); break;
  51757. case 30 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned char,unsigned char,unsigned char,1);
  51758. break;
  51759. case 31 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],long,int,short,4); break;
  51760. case 32 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned long,unsigned int,unsigned short,4);
  51761. break;
  51762. case 33 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],double,float,float,4); break;
  51763. case 34 : { // Points 1D
  51764. cimg::fread(ptbuf,1,nfile);
  51765. if (endian) cimg::invert_endianness(ptbuf,1);
  51766. assign(1); (*this)(0) = (T)ptbuf[0];
  51767. } break;
  51768. case 35 : { // Points 2D
  51769. cimg::fread(ptbuf,2,nfile);
  51770. if (endian) cimg::invert_endianness(ptbuf,2);
  51771. assign(2); (*this)(0) = (T)ptbuf[1]; (*this)(1) = (T)ptbuf[0];
  51772. } break;
  51773. case 36 : { // Points 3D
  51774. cimg::fread(ptbuf,3,nfile);
  51775. if (endian) cimg::invert_endianness(ptbuf,3);
  51776. assign(3); (*this)(0) = (T)ptbuf[2]; (*this)(1) = (T)ptbuf[1]; (*this)(2) = (T)ptbuf[0];
  51777. } break;
  51778. default :
  51779. if (!file) cimg::fclose(nfile);
  51780. throw CImgIOException(_cimg_instance
  51781. "load_pandore(): Unable to load data with ID_type %u in file '%s'.",
  51782. cimg_instance,
  51783. imageid,filename?filename:"(FILE*)");
  51784. }
  51785. if (!file) cimg::fclose(nfile);
  51786. return *this;
  51787. }
  51788. //! Load image from a PAR-REC (Philips) file.
  51789. /**
  51790. \param filename Filename, as a C-string.
  51791. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  51792. \param align Appending alignment.
  51793. **/
  51794. CImg<T>& load_parrec(const char *const filename, const char axis='c', const float align=0) {
  51795. CImgList<T> list;
  51796. list.load_parrec(filename);
  51797. if (list._width==1) return list[0].move_to(*this);
  51798. return assign(list.get_append(axis,align));
  51799. }
  51800. //! Load image from a PAR-REC (Philips) file \newinstance.
  51801. static CImg<T> get_load_parrec(const char *const filename, const char axis='c', const float align=0) {
  51802. return CImg<T>().load_parrec(filename,axis,align);
  51803. }
  51804. //! Load image from a raw binary file.
  51805. /**
  51806. \param filename Filename, as a C-string.
  51807. \param size_x Width of the image buffer.
  51808. \param size_y Height of the image buffer.
  51809. \param size_z Depth of the image buffer.
  51810. \param size_c Spectrum of the image buffer.
  51811. \param is_multiplexed Tells if the image values are multiplexed along the C-axis.
  51812. \param invert_endianness Tells if the endianness of the image buffer must be inverted.
  51813. \param offset Starting offset of the read in the specified file.
  51814. **/
  51815. CImg<T>& load_raw(const char *const filename,
  51816. const unsigned int size_x=0, const unsigned int size_y=1,
  51817. const unsigned int size_z=1, const unsigned int size_c=1,
  51818. const bool is_multiplexed=false, const bool invert_endianness=false,
  51819. const ulongT offset=0) {
  51820. return _load_raw(0,filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
  51821. }
  51822. //! Load image from a raw binary file \newinstance.
  51823. static CImg<T> get_load_raw(const char *const filename,
  51824. const unsigned int size_x=0, const unsigned int size_y=1,
  51825. const unsigned int size_z=1, const unsigned int size_c=1,
  51826. const bool is_multiplexed=false, const bool invert_endianness=false,
  51827. const ulongT offset=0) {
  51828. return CImg<T>().load_raw(filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
  51829. }
  51830. //! Load image from a raw binary file \overloading.
  51831. CImg<T>& load_raw(std::FILE *const file,
  51832. const unsigned int size_x=0, const unsigned int size_y=1,
  51833. const unsigned int size_z=1, const unsigned int size_c=1,
  51834. const bool is_multiplexed=false, const bool invert_endianness=false,
  51835. const ulongT offset=0) {
  51836. return _load_raw(file,0,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
  51837. }
  51838. //! Load image from a raw binary file \newinstance.
  51839. static CImg<T> get_load_raw(std::FILE *const file,
  51840. const unsigned int size_x=0, const unsigned int size_y=1,
  51841. const unsigned int size_z=1, const unsigned int size_c=1,
  51842. const bool is_multiplexed=false, const bool invert_endianness=false,
  51843. const ulongT offset=0) {
  51844. return CImg<T>().load_raw(file,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
  51845. }
  51846. CImg<T>& _load_raw(std::FILE *const file, const char *const filename,
  51847. const unsigned int size_x, const unsigned int size_y,
  51848. const unsigned int size_z, const unsigned int size_c,
  51849. const bool is_multiplexed, const bool invert_endianness,
  51850. const ulongT offset) {
  51851. if (!file && !filename)
  51852. throw CImgArgumentException(_cimg_instance
  51853. "load_raw(): Specified filename is (null).",
  51854. cimg_instance);
  51855. if (cimg::is_directory(filename))
  51856. throw CImgArgumentException(_cimg_instance
  51857. "load_raw(): Specified filename '%s' is a directory.",
  51858. cimg_instance,filename);
  51859. const bool is_bool = pixel_type()==cimg::type<bool>::string();
  51860. ulongT siz = (ulongT)size_x*size_y*size_z*size_c;
  51861. unsigned int
  51862. _size_x = size_x,
  51863. _size_y = size_y,
  51864. _size_z = size_z,
  51865. _size_c = size_c;
  51866. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  51867. if (!siz) { // Retrieve file size
  51868. const longT fpos = cimg::ftell(nfile);
  51869. if (fpos<0) throw CImgArgumentException(_cimg_instance
  51870. "load_raw(): Cannot determine size of input file '%s'.",
  51871. cimg_instance,filename?filename:"(FILE*)");
  51872. cimg::fseek(nfile,0,SEEK_END);
  51873. siz = (ulongT)cimg::ftell(nfile);
  51874. if (!is_bool) { siz/=sizeof(T); _size_y = (unsigned int)siz; }
  51875. else _size_y = (unsigned int)(siz*8);
  51876. _size_x = _size_z = _size_c = 1;
  51877. cimg::fseek(nfile,fpos,SEEK_SET);
  51878. }
  51879. cimg::fseek(nfile,(longT)offset,SEEK_SET);
  51880. assign(_size_x,_size_y,_size_z,_size_c,0);
  51881. if (is_bool) { // Boolean data (bitwise)
  51882. unsigned char *const buf = new unsigned char[siz];
  51883. cimg::fread(buf,siz,nfile);
  51884. _uchar2bool(buf,siz,is_multiplexed);
  51885. delete[] buf;
  51886. } else { // Non-boolean data
  51887. if (siz && (!is_multiplexed || size_c==1)) { // Non-multiplexed
  51888. cimg::fread(_data,siz,nfile);
  51889. if (invert_endianness) cimg::invert_endianness(_data,siz);
  51890. } else if (siz) { // Multiplexed
  51891. CImg<T> buf(1,1,1,_size_c);
  51892. cimg_forXYZ(*this,x,y,z) {
  51893. cimg::fread(buf._data,_size_c,nfile);
  51894. if (invert_endianness) cimg::invert_endianness(buf._data,_size_c);
  51895. set_vector_at(buf,x,y,z);
  51896. }
  51897. }
  51898. }
  51899. if (!file) cimg::fclose(nfile);
  51900. return *this;
  51901. }
  51902. //! Load image sequence from a YUV file.
  51903. /**
  51904. \param filename Filename, as a C-string.
  51905. \param size_x Width of the frames.
  51906. \param size_y Height of the frames.
  51907. \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
  51908. \param first_frame Index of the first frame to read.
  51909. \param last_frame Index of the last frame to read.
  51910. \param step_frame Step value for frame reading.
  51911. \param yuv2rgb Tells if the YUV to RGB transform must be applied.
  51912. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  51913. **/
  51914. CImg<T>& load_yuv(const char *const filename,
  51915. const unsigned int size_x, const unsigned int size_y=1,
  51916. const unsigned int chroma_subsampling=444,
  51917. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  51918. const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
  51919. return get_load_yuv(filename,size_x,size_y,chroma_subsampling,
  51920. first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this);
  51921. }
  51922. //! Load image sequence from a YUV file \newinstance.
  51923. static CImg<T> get_load_yuv(const char *const filename,
  51924. const unsigned int size_x, const unsigned int size_y=1,
  51925. const unsigned int chroma_subsampling=444,
  51926. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  51927. const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
  51928. return CImgList<T>().load_yuv(filename,size_x,size_y,chroma_subsampling,
  51929. first_frame,last_frame,step_frame,yuv2rgb).get_append(axis);
  51930. }
  51931. //! Load image sequence from a YUV file \overloading.
  51932. CImg<T>& load_yuv(std::FILE *const file,
  51933. const unsigned int size_x, const unsigned int size_y=1,
  51934. const unsigned int chroma_subsampling=444,
  51935. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  51936. const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
  51937. return get_load_yuv(file,size_x,size_y,chroma_subsampling,
  51938. first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this);
  51939. }
  51940. //! Load image sequence from a YUV file \newinstance.
  51941. static CImg<T> get_load_yuv(std::FILE *const file,
  51942. const unsigned int size_x, const unsigned int size_y=1,
  51943. const unsigned int chroma_subsampling=444,
  51944. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  51945. const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
  51946. return CImgList<T>().load_yuv(file,size_x,size_y,chroma_subsampling,
  51947. first_frame,last_frame,step_frame,yuv2rgb).get_append(axis);
  51948. }
  51949. //! Load 3D object from a .OFF file.
  51950. /**
  51951. \param[out] primitives Primitives data of the 3D object.
  51952. \param[out] colors Colors data of the 3D object.
  51953. \param filename Filename, as a C-string.
  51954. **/
  51955. template<typename tf, typename tc>
  51956. CImg<T>& load_off(CImgList<tf>& primitives, CImgList<tc>& colors, const char *const filename) {
  51957. return _load_off(primitives,colors,0,filename);
  51958. }
  51959. //! Load 3D object from a .OFF file \newinstance.
  51960. template<typename tf, typename tc>
  51961. static CImg<T> get_load_off(CImgList<tf>& primitives, CImgList<tc>& colors, const char *const filename) {
  51962. return CImg<T>().load_off(primitives,colors,filename);
  51963. }
  51964. //! Load 3D object from a .OFF file \overloading.
  51965. template<typename tf, typename tc>
  51966. CImg<T>& load_off(CImgList<tf>& primitives, CImgList<tc>& colors, std::FILE *const file) {
  51967. return _load_off(primitives,colors,file,0);
  51968. }
  51969. //! Load 3D object from a .OFF file \newinstance.
  51970. template<typename tf, typename tc>
  51971. static CImg<T> get_load_off(CImgList<tf>& primitives, CImgList<tc>& colors, std::FILE *const file) {
  51972. return CImg<T>().load_off(primitives,colors,file);
  51973. }
  51974. template<typename tf, typename tc>
  51975. CImg<T>& _load_off(CImgList<tf>& primitives, CImgList<tc>& colors,
  51976. std::FILE *const file, const char *const filename) {
  51977. if (!file && !filename)
  51978. throw CImgArgumentException(_cimg_instance
  51979. "load_off(): Specified filename is (null).",
  51980. cimg_instance);
  51981. std::FILE *const nfile = file?file:cimg::fopen(filename,"r");
  51982. unsigned int nb_points = 0, nb_primitives = 0, nb_read = 0;
  51983. CImg<charT> line(256); *line = 0;
  51984. int err;
  51985. // Skip comments, and read magic string OFF
  51986. do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
  51987. if (cimg::strncasecmp(line,"OFF",3) && cimg::strncasecmp(line,"COFF",4)) {
  51988. if (!file) cimg::fclose(nfile);
  51989. throw CImgIOException(_cimg_instance
  51990. "load_off(): OFF header not found in file '%s'.",
  51991. cimg_instance,
  51992. filename?filename:"(FILE*)");
  51993. }
  51994. do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
  51995. if ((err = cimg_sscanf(line,"%u%u%*[^\n] ",&nb_points,&nb_primitives))!=2) {
  51996. if (!file) cimg::fclose(nfile);
  51997. throw CImgIOException(_cimg_instance
  51998. "load_off(): Invalid number of vertices or primitives specified in file '%s'.",
  51999. cimg_instance,
  52000. filename?filename:"(FILE*)");
  52001. }
  52002. // Read points data
  52003. assign(nb_points,3);
  52004. float X = 0, Y = 0, Z = 0;
  52005. cimg_forX(*this,l) {
  52006. do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
  52007. if ((err = cimg_sscanf(line,"%f%f%f%*[^\n] ",&X,&Y,&Z))!=3) {
  52008. if (!file) cimg::fclose(nfile);
  52009. throw CImgIOException(_cimg_instance
  52010. "load_off(): Failed to read vertex %u/%u in file '%s'.",
  52011. cimg_instance,
  52012. l + 1,nb_points,filename?filename:"(FILE*)");
  52013. }
  52014. (*this)(l,0) = (T)X; (*this)(l,1) = (T)Y; (*this)(l,2) = (T)Z;
  52015. }
  52016. // Read primitive data
  52017. primitives.assign();
  52018. colors.assign();
  52019. bool stop_flag = false;
  52020. while (!stop_flag) {
  52021. float c0 = 0.7f, c1 = 0.7f, c2 = 0.7f;
  52022. unsigned int prim = 0, i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0;
  52023. *line = 0;
  52024. if ((err = std::fscanf(nfile,"%u",&prim))!=1) stop_flag = true;
  52025. else {
  52026. ++nb_read;
  52027. switch (prim) {
  52028. case 1 : {
  52029. if ((err = std::fscanf(nfile,"%u%255[^\n] ",&i0,line._data))<2) {
  52030. cimg::warn(_cimg_instance
  52031. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52032. cimg_instance,
  52033. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52034. err = std::fscanf(nfile,"%*[^\n] ");
  52035. } else {
  52036. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52037. CImg<tf>::vector(i0).move_to(primitives);
  52038. CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
  52039. }
  52040. } break;
  52041. case 2 : {
  52042. if ((err = std::fscanf(nfile,"%u%u%255[^\n] ",&i0,&i1,line._data))<2) {
  52043. cimg::warn(_cimg_instance
  52044. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52045. cimg_instance,
  52046. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52047. err = std::fscanf(nfile,"%*[^\n] ");
  52048. } else {
  52049. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52050. CImg<tf>::vector(i0,i1).move_to(primitives);
  52051. CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
  52052. }
  52053. } break;
  52054. case 3 : {
  52055. if ((err = std::fscanf(nfile,"%u%u%u%255[^\n] ",&i0,&i1,&i2,line._data))<3) {
  52056. cimg::warn(_cimg_instance
  52057. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52058. cimg_instance,
  52059. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52060. err = std::fscanf(nfile,"%*[^\n] ");
  52061. } else {
  52062. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52063. CImg<tf>::vector(i0,i2,i1).move_to(primitives);
  52064. CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
  52065. }
  52066. } break;
  52067. case 4 : {
  52068. if ((err = std::fscanf(nfile,"%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,line._data))<4) {
  52069. cimg::warn(_cimg_instance
  52070. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52071. cimg_instance,
  52072. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52073. err = std::fscanf(nfile,"%*[^\n] ");
  52074. } else {
  52075. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52076. CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
  52077. CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
  52078. }
  52079. } break;
  52080. case 5 : {
  52081. if ((err = std::fscanf(nfile,"%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,line._data))<5) {
  52082. cimg::warn(_cimg_instance
  52083. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52084. cimg_instance,
  52085. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52086. err = std::fscanf(nfile,"%*[^\n] ");
  52087. } else {
  52088. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52089. CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
  52090. CImg<tf>::vector(i0,i4,i3).move_to(primitives);
  52091. colors.insert(2,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
  52092. ++nb_primitives;
  52093. }
  52094. } break;
  52095. case 6 : {
  52096. if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,line._data))<6) {
  52097. cimg::warn(_cimg_instance
  52098. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52099. cimg_instance,
  52100. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52101. err = std::fscanf(nfile,"%*[^\n] ");
  52102. } else {
  52103. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52104. CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
  52105. CImg<tf>::vector(i0,i5,i4,i3).move_to(primitives);
  52106. colors.insert(2,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
  52107. ++nb_primitives;
  52108. }
  52109. } break;
  52110. case 7 : {
  52111. if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,line._data))<7) {
  52112. cimg::warn(_cimg_instance
  52113. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52114. cimg_instance,
  52115. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52116. err = std::fscanf(nfile,"%*[^\n] ");
  52117. } else {
  52118. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52119. CImg<tf>::vector(i0,i4,i3,i1).move_to(primitives);
  52120. CImg<tf>::vector(i0,i6,i5,i4).move_to(primitives);
  52121. CImg<tf>::vector(i3,i2,i1).move_to(primitives);
  52122. colors.insert(3,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
  52123. ++(++nb_primitives);
  52124. }
  52125. } break;
  52126. case 8 : {
  52127. if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,&i7,line._data))<7) {
  52128. cimg::warn(_cimg_instance
  52129. "load_off(): Failed to read primitive %u/%u from file '%s'.",
  52130. cimg_instance,
  52131. nb_read,nb_primitives,filename?filename:"(FILE*)");
  52132. err = std::fscanf(nfile,"%*[^\n] ");
  52133. } else {
  52134. err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
  52135. CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
  52136. CImg<tf>::vector(i0,i5,i4,i3).move_to(primitives);
  52137. CImg<tf>::vector(i0,i7,i6,i5).move_to(primitives);
  52138. colors.insert(3,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
  52139. ++(++nb_primitives);
  52140. }
  52141. } break;
  52142. default :
  52143. cimg::warn(_cimg_instance
  52144. "load_off(): Failed to read primitive %u/%u (%u vertices) from file '%s'.",
  52145. cimg_instance,
  52146. nb_read,nb_primitives,prim,filename?filename:"(FILE*)");
  52147. err = std::fscanf(nfile,"%*[^\n] ");
  52148. }
  52149. }
  52150. }
  52151. if (!file) cimg::fclose(nfile);
  52152. if (primitives._width!=nb_primitives)
  52153. cimg::warn(_cimg_instance
  52154. "load_off(): Only %u/%u primitives read from file '%s'.",
  52155. cimg_instance,
  52156. primitives._width,nb_primitives,filename?filename:"(FILE*)");
  52157. return *this;
  52158. }
  52159. //! Load image sequence from a video file, using OpenCV library.
  52160. /**
  52161. \param filename Filename, as a C-string.
  52162. \param first_frame Index of the first frame to read.
  52163. \param last_frame Index of the last frame to read.
  52164. \param step_frame Step value for frame reading.
  52165. \param axis Alignment axis.
  52166. \param align Appending alignment.
  52167. **/
  52168. CImg<T>& load_video(const char *const filename,
  52169. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  52170. const unsigned int step_frame=1,
  52171. const char axis='z', const float align=0) {
  52172. return get_load_video(filename,first_frame,last_frame,step_frame,axis,align).move_to(*this);
  52173. }
  52174. //! Load image sequence from a video file, using OpenCV library \newinstance.
  52175. static CImg<T> get_load_video(const char *const filename,
  52176. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  52177. const unsigned int step_frame=1,
  52178. const char axis='z', const float align=0) {
  52179. return CImgList<T>().load_video(filename,first_frame,last_frame,step_frame).get_append(axis,align);
  52180. }
  52181. //! Load image sequence using FFMPEG's external tool 'ffmpeg'.
  52182. /**
  52183. \param filename Filename, as a C-string.
  52184. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  52185. \param align Appending alignment.
  52186. **/
  52187. CImg<T>& load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) {
  52188. return get_load_ffmpeg_external(filename,axis,align).move_to(*this);
  52189. }
  52190. //! Load image sequence using FFMPEG's external tool 'ffmpeg' \newinstance.
  52191. static CImg<T> get_load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) {
  52192. return CImgList<T>().load_ffmpeg_external(filename).get_append(axis,align);
  52193. }
  52194. //! Load gif file, using Imagemagick or GraphicsMagicks's external tools.
  52195. /**
  52196. \param filename Filename, as a C-string.
  52197. \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  52198. \param align Appending alignment.
  52199. **/
  52200. CImg<T>& load_gif_external(const char *const filename,
  52201. const char axis='z', const float align=0) {
  52202. return get_load_gif_external(filename,axis,align).move_to(*this);
  52203. }
  52204. //! Load gif file, using ImageMagick or GraphicsMagick's external tool 'convert' \newinstance.
  52205. static CImg<T> get_load_gif_external(const char *const filename,
  52206. const char axis='z', const float align=0) {
  52207. return CImgList<T>().load_gif_external(filename).get_append(axis,align);
  52208. }
  52209. //! Load image from a HEIC file.
  52210. /**
  52211. \param filename Filename, as a C-string.
  52212. **/
  52213. CImg<T>& load_heif(const char *const filename) {
  52214. return _load_heif(filename);
  52215. }
  52216. //! Load image from a HEIC file \newinstance.
  52217. static CImg<T> get_load_heif(const char *const filename) {
  52218. return CImg<T>().load_heif(filename);
  52219. }
  52220. CImg<T>& _load_heif(const char *const filename) {
  52221. #ifndef cimg_use_heif
  52222. return load_other(filename);
  52223. #else
  52224. try {
  52225. heif::Context ctx;
  52226. ctx.read_from_file(filename);
  52227. heif::ImageHandle handle = ctx.get_primary_image_handle();
  52228. const heif::Image image =
  52229. handle.decode_image(heif_colorspace_RGB,handle.has_alpha_channel()?heif_chroma_interleaved_RGBA:
  52230. heif_chroma_interleaved_RGB);
  52231. const int
  52232. W = image.get_width(heif_channel_interleaved),
  52233. H = image.get_height(heif_channel_interleaved),
  52234. S = handle.has_alpha_channel()?4:3;
  52235. assign(W,H,1,S);
  52236. int stride;
  52237. const unsigned char *const buffer = image.get_plane(heif_channel_interleaved,&stride);
  52238. T *ptr_r = _data, *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = S>3?data(0,0,0,3):0;
  52239. cimg_forY(*this,y) {
  52240. const unsigned char *ptrs = buffer + y*stride;
  52241. if (ptr_a) cimg_forX(*this,x) { // RGBA
  52242. *(ptr_r++) = (T)*(ptrs++);
  52243. *(ptr_g++) = (T)*(ptrs++);
  52244. *(ptr_b++) = (T)*(ptrs++);
  52245. *(ptr_a++) = (T)*(ptrs++);
  52246. }
  52247. else cimg_forX(*this,x) { // RGB
  52248. *(ptr_r++) = (T)*(ptrs++);
  52249. *(ptr_g++) = (T)*(ptrs++);
  52250. *(ptr_b++) = (T)*(ptrs++);
  52251. }
  52252. }
  52253. } catch (const heif::Error& e) {
  52254. throw CImgInstanceException(_cimg_instance
  52255. "load_heif(): Unable to decode image: %s",
  52256. cimg_instance,
  52257. e.get_message().c_str());
  52258. } catch (...) {
  52259. throw;
  52260. }
  52261. return *this;
  52262. #endif
  52263. }
  52264. //! Load image using GraphicsMagick's external tool 'gm'.
  52265. /**
  52266. \param filename Filename, as a C-string.
  52267. **/
  52268. CImg<T>& load_graphicsmagick_external(const char *const filename) {
  52269. if (!filename)
  52270. throw CImgArgumentException(_cimg_instance
  52271. "load_graphicsmagick_external(): Specified filename is (null).",
  52272. cimg_instance);
  52273. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  52274. CImg<charT> command(1024), filename_tmp(256);
  52275. std::FILE *file = 0;
  52276. const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
  52277. #if cimg_OS==1
  52278. if (!cimg::system("which gm")) {
  52279. cimg_snprintf(command,command._width,"%s convert \"%s\" %s:-",
  52280. cimg::graphicsmagick_path(),
  52281. s_filename.data(),
  52282. #ifdef cimg_use_png
  52283. "png"
  52284. #else
  52285. "pnm"
  52286. #endif
  52287. );
  52288. file = popen(command,"r");
  52289. if (file) {
  52290. const unsigned int omode = cimg::exception_mode();
  52291. cimg::exception_mode(0);
  52292. try {
  52293. #ifdef cimg_use_png
  52294. load_png(file);
  52295. #else
  52296. load_pnm(file);
  52297. #endif
  52298. } catch (...) {
  52299. pclose(file);
  52300. cimg::exception_mode(omode);
  52301. throw CImgIOException(_cimg_instance
  52302. "load_graphicsmagick_external(): Failed to load file '%s' "
  52303. "with external command 'gm'.",
  52304. cimg_instance,
  52305. filename);
  52306. }
  52307. pclose(file);
  52308. return *this;
  52309. }
  52310. }
  52311. #endif
  52312. do {
  52313. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  52314. cimg::temporary_path(),
  52315. cimg_file_separator,
  52316. cimg::filenamerand(),
  52317. #ifdef cimg_use_png
  52318. "png"
  52319. #else
  52320. "pnm"
  52321. #endif
  52322. );
  52323. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52324. } while (file);
  52325. cimg_snprintf(command,command._width,"\"%s\" convert \"%s\" \"%s\"",
  52326. cimg::graphicsmagick_path(),
  52327. s_filename.data(),
  52328. CImg<charT>::string(filename_tmp)._system_strescape().data());
  52329. cimg::system(command,cimg::graphicsmagick_path());
  52330. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  52331. cimg::fclose(cimg::fopen(filename,"r"));
  52332. throw CImgIOException(_cimg_instance
  52333. "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.",
  52334. cimg_instance,
  52335. filename);
  52336. } else cimg::fclose(file);
  52337. #ifdef cimg_use_png
  52338. load_png(filename_tmp);
  52339. #else
  52340. load_pnm(filename_tmp);
  52341. #endif
  52342. std::remove(filename_tmp);
  52343. return *this;
  52344. }
  52345. //! Load image using GraphicsMagick's external tool 'gm' \newinstance.
  52346. static CImg<T> get_load_graphicsmagick_external(const char *const filename) {
  52347. return CImg<T>().load_graphicsmagick_external(filename);
  52348. }
  52349. //! Load gzipped image file, using external tool 'gunzip'.
  52350. /**
  52351. \param filename Filename, as a C-string.
  52352. **/
  52353. CImg<T>& load_gzip_external(const char *const filename) {
  52354. if (!filename)
  52355. throw CImgIOException(_cimg_instance
  52356. "load_gzip_external(): Specified filename is (null).",
  52357. cimg_instance);
  52358. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  52359. CImg<charT> command(1024), filename_tmp(256), body(256);
  52360. const char
  52361. *const ext = cimg::split_filename(filename,body),
  52362. *const ext2 = cimg::split_filename(body,0);
  52363. std::FILE *file = 0;
  52364. do {
  52365. if (!cimg::strcasecmp(ext,"gz")) {
  52366. if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  52367. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
  52368. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  52369. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  52370. } else {
  52371. if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  52372. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
  52373. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  52374. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  52375. }
  52376. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52377. } while (file);
  52378. cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
  52379. cimg::gunzip_path(),
  52380. CImg<charT>::string(filename)._system_strescape().data(),
  52381. CImg<charT>::string(filename_tmp)._system_strescape().data());
  52382. cimg::system(command);
  52383. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  52384. cimg::fclose(cimg::fopen(filename,"r"));
  52385. throw CImgIOException(_cimg_instance
  52386. "load_gzip_external(): Failed to load file '%s' with external command 'gunzip'.",
  52387. cimg_instance,
  52388. filename);
  52389. } else cimg::fclose(file);
  52390. load(filename_tmp);
  52391. std::remove(filename_tmp);
  52392. return *this;
  52393. }
  52394. //! Load gzipped image file, using external tool 'gunzip' \newinstance.
  52395. static CImg<T> get_load_gzip_external(const char *const filename) {
  52396. return CImg<T>().load_gzip_external(filename);
  52397. }
  52398. //! Load image using ImageMagick's external tool 'convert'.
  52399. /**
  52400. \param filename Filename, as a C-string.
  52401. **/
  52402. CImg<T>& load_imagemagick_external(const char *const filename) {
  52403. if (!filename)
  52404. throw CImgArgumentException(_cimg_instance
  52405. "load_imagemagick_external(): Specified filename is (null).",
  52406. cimg_instance);
  52407. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  52408. CImg<charT> command(1024), filename_tmp(256);
  52409. std::FILE *file = 0;
  52410. const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
  52411. #if cimg_OS==1
  52412. if (!cimg::system("which convert")) {
  52413. cimg_snprintf(command,command._width,"%s%s \"%s\" %s:-",
  52414. cimg::imagemagick_path(),
  52415. !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"",
  52416. s_filename.data(),
  52417. #ifdef cimg_use_png
  52418. "png"
  52419. #else
  52420. "pnm"
  52421. #endif
  52422. );
  52423. file = popen(command,"r");
  52424. if (file) {
  52425. const unsigned int omode = cimg::exception_mode();
  52426. cimg::exception_mode(0);
  52427. try {
  52428. #ifdef cimg_use_png
  52429. load_png(file);
  52430. #else
  52431. load_pnm(file);
  52432. #endif
  52433. } catch (...) {
  52434. pclose(file);
  52435. cimg::exception_mode(omode);
  52436. throw CImgIOException(_cimg_instance
  52437. "load_imagemagick_external(): Failed to load file '%s' with "
  52438. "external command 'magick/convert'.",
  52439. cimg_instance,
  52440. filename);
  52441. }
  52442. pclose(file);
  52443. return *this;
  52444. }
  52445. }
  52446. #endif
  52447. do {
  52448. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  52449. cimg::temporary_path(),
  52450. cimg_file_separator,
  52451. cimg::filenamerand(),
  52452. #ifdef cimg_use_png
  52453. "png"
  52454. #else
  52455. "pnm"
  52456. #endif
  52457. );
  52458. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52459. } while (file);
  52460. cimg_snprintf(command,command._width,"\"%s\"%s \"%s\" \"%s\"",
  52461. cimg::imagemagick_path(),
  52462. !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"",
  52463. s_filename.data(),
  52464. CImg<charT>::string(filename_tmp)._system_strescape().data());
  52465. cimg::system(command,cimg::imagemagick_path());
  52466. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  52467. cimg::fclose(cimg::fopen(filename,"r"));
  52468. throw CImgIOException(_cimg_instance
  52469. "load_imagemagick_external(): Failed to load file '%s' with "
  52470. "external command 'magick/convert'.",
  52471. cimg_instance,
  52472. filename);
  52473. } else cimg::fclose(file);
  52474. #ifdef cimg_use_png
  52475. load_png(filename_tmp);
  52476. #else
  52477. load_pnm(filename_tmp);
  52478. #endif
  52479. std::remove(filename_tmp);
  52480. return *this;
  52481. }
  52482. //! Load image using ImageMagick's external tool 'convert' \newinstance.
  52483. static CImg<T> get_load_imagemagick_external(const char *const filename) {
  52484. return CImg<T>().load_imagemagick_external(filename);
  52485. }
  52486. //! Load image from a DICOM file, using Medcon's external tool 'medcon'.
  52487. /**
  52488. \param filename Filename, as a C-string.
  52489. **/
  52490. CImg<T>& load_medcon_external(const char *const filename) {
  52491. if (!filename)
  52492. throw CImgArgumentException(_cimg_instance
  52493. "load_medcon_external(): Specified filename is (null).",
  52494. cimg_instance);
  52495. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  52496. CImg<charT> command(1024), filename_tmp(256), body(256);
  52497. cimg::fclose(cimg::fopen(filename,"r"));
  52498. std::FILE *file = 0;
  52499. do {
  52500. cimg_snprintf(filename_tmp,filename_tmp._width,"%s.hdr",cimg::filenamerand());
  52501. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52502. } while (file);
  52503. cimg_snprintf(command,command._width,"\"%s\" -w -c anlz -o \"%s\" -f \"%s\"",
  52504. cimg::medcon_path(),
  52505. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  52506. CImg<charT>::string(filename)._system_strescape().data());
  52507. cimg::system(command, cimg::medcon_path());
  52508. cimg::split_filename(filename_tmp,body);
  52509. cimg_snprintf(command,command._width,"%s.hdr",body._data);
  52510. file = cimg::std_fopen(command,"rb");
  52511. if (!file) {
  52512. cimg_snprintf(command,command._width,"m000-%s.hdr",body._data);
  52513. file = cimg::std_fopen(command,"rb");
  52514. if (!file) {
  52515. throw CImgIOException(_cimg_instance
  52516. "load_medcon_external(): Failed to load file '%s' with external command 'medcon'.",
  52517. cimg_instance,
  52518. filename);
  52519. }
  52520. }
  52521. cimg::fclose(file);
  52522. load_analyze(command);
  52523. std::remove(command);
  52524. cimg::split_filename(command,body);
  52525. cimg_snprintf(command,command._width,"%s.img",body._data);
  52526. std::remove(command);
  52527. return *this;
  52528. }
  52529. //! Load image from a DICOM file, using Medcon's external tool 'medcon' \newinstance.
  52530. static CImg<T> get_load_medcon_external(const char *const filename) {
  52531. return CImg<T>().load_medcon_external(filename);
  52532. }
  52533. //! Load image from a .pdf file.
  52534. /**
  52535. \param filename Filename, as a C-string.
  52536. \param resolution Image resolution.
  52537. **/
  52538. CImg<T>& load_pdf_external(const char *const filename, const unsigned int resolution=400) {
  52539. if (!filename)
  52540. throw CImgArgumentException(_cimg_instance
  52541. "load_pdf_external(): Specified filename is (null).",
  52542. cimg_instance);
  52543. CImg<charT> command(1024), filename_tmp(256);
  52544. std::FILE *file = 0;
  52545. const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
  52546. #if cimg_OS==1
  52547. cimg_snprintf(command,command._width,"gs -q -dNOPAUSE -sDEVICE=ppmraw -o - -r%u \"%s\"",
  52548. resolution,s_filename.data());
  52549. file = popen(command,"r");
  52550. if (file) {
  52551. const unsigned int omode = cimg::exception_mode();
  52552. cimg::exception_mode(0);
  52553. try { load_pnm(file); } catch (...) {
  52554. pclose(file);
  52555. cimg::exception_mode(omode);
  52556. throw CImgIOException(_cimg_instance
  52557. "load_pdf_external(): Failed to load file '%s' with external command 'gs'.",
  52558. cimg_instance,
  52559. filename);
  52560. }
  52561. pclose(file);
  52562. return *this;
  52563. }
  52564. #endif
  52565. do {
  52566. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.ppm",
  52567. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  52568. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52569. } while (file);
  52570. cimg_snprintf(command,command._width,"gs -q -dNOPAUSE -sDEVICE=ppmraw -o \"%s\" -r%u \"%s\"",
  52571. CImg<charT>::string(filename_tmp)._system_strescape().data(),resolution,s_filename.data());
  52572. cimg::system(command,"gs");
  52573. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  52574. cimg::fclose(cimg::fopen(filename,"r"));
  52575. throw CImgIOException(_cimg_instance
  52576. "load_pdf_external(): Failed to load file '%s' with external command 'gs'.",
  52577. cimg_instance,
  52578. filename);
  52579. } else cimg::fclose(file);
  52580. load_pnm(filename_tmp);
  52581. std::remove(filename_tmp);
  52582. return *this;
  52583. }
  52584. //! Load image from a .pdf file \newinstance.
  52585. static CImg<T> get_load_pdf_external(const char *const filename, const unsigned int resolution=400) {
  52586. return CImg<T>().load_pdf_external(filename,resolution);
  52587. }
  52588. //! Load image from a RAW Color Camera file, using external tool 'dcraw'.
  52589. /**
  52590. \param filename Filename, as a C-string.
  52591. **/
  52592. CImg<T>& load_dcraw_external(const char *const filename) {
  52593. if (!filename)
  52594. throw CImgArgumentException(_cimg_instance
  52595. "load_dcraw_external(): Specified filename is (null).",
  52596. cimg_instance);
  52597. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  52598. CImg<charT> command(1024), filename_tmp(256);
  52599. std::FILE *file = 0;
  52600. const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
  52601. #if cimg_OS==1
  52602. cimg_snprintf(command,command._width,"%s -w -4 -c \"%s\"",
  52603. cimg::dcraw_path(),s_filename.data());
  52604. file = popen(command,"r");
  52605. if (file) {
  52606. const unsigned int omode = cimg::exception_mode();
  52607. cimg::exception_mode(0);
  52608. try { load_pnm(file); } catch (...) {
  52609. pclose(file);
  52610. cimg::exception_mode(omode);
  52611. throw CImgIOException(_cimg_instance
  52612. "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.",
  52613. cimg_instance,
  52614. filename);
  52615. }
  52616. pclose(file);
  52617. return *this;
  52618. }
  52619. #endif
  52620. do {
  52621. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.ppm",
  52622. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  52623. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  52624. } while (file);
  52625. cimg_snprintf(command,command._width,"\"%s\" -w -4 -c \"%s\" > \"%s\"",
  52626. cimg::dcraw_path(),s_filename.data(),CImg<charT>::string(filename_tmp)._system_strescape().data());
  52627. cimg::system(command,cimg::dcraw_path());
  52628. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  52629. cimg::fclose(cimg::fopen(filename,"r"));
  52630. throw CImgIOException(_cimg_instance
  52631. "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.",
  52632. cimg_instance,
  52633. filename);
  52634. } else cimg::fclose(file);
  52635. load_pnm(filename_tmp);
  52636. std::remove(filename_tmp);
  52637. return *this;
  52638. }
  52639. //! Load image from a RAW Color Camera file, using external tool 'dcraw' \newinstance.
  52640. static CImg<T> get_load_dcraw_external(const char *const filename) {
  52641. return CImg<T>().load_dcraw_external(filename);
  52642. }
  52643. #ifdef cimg_use_opencv
  52644. // Convert a continuous cv::Mat<uchar> to a CImg<uchar>.
  52645. static CImg<ucharT> _cvmat2cimg(const cv::Mat &src) {
  52646. if (src.channels()==1) return CImg<ucharT>(src.ptr(),src.cols,src.rows,1,1);
  52647. else if (src.channels()==3) { // BGR
  52648. CImg<ucharT> res(src.cols,src.rows,1,src.channels());
  52649. const unsigned char *ptrs = src.ptr();
  52650. unsigned char *pR = res.data(), *pG = res.data(0,0,0,1), *pB = res.data(0,0,0,2);
  52651. cimg_forXY(res,x,y) { *(pB++) = *(ptrs++); *(pG++) = *(ptrs++); *(pR++) = *(ptrs++); }
  52652. return res;
  52653. }
  52654. return CImg<ucharT>(src.ptr(),src.channels(),src.cols,src.rows,1,true).get_permute_axes("yzcx");
  52655. }
  52656. // Convert a CImg<T> to a cv::Mat.
  52657. cv::Mat _cimg2cvmat() const {
  52658. if (is_empty())
  52659. throw CImgInstanceException(_cimg_instance
  52660. "_cimg2cvmat() : Instance image is empty.",
  52661. cimg_instance);
  52662. if (_spectrum==2)
  52663. throw CImgInstanceException(_cimg_instance
  52664. "_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').",
  52665. cimg_instance);
  52666. if (_depth!=1)
  52667. throw CImgInstanceException(_cimg_instance
  52668. "_cimg2cvmat() : Invalid number of slices (should be '1').",
  52669. cimg_instance);
  52670. int mat_type = -1;
  52671. if (pixel_type()==cimg::type<unsigned char>::string()) mat_type = CV_8UC1;
  52672. if (pixel_type()==cimg::type<char>::string()) mat_type = CV_8SC1;
  52673. if (pixel_type()==cimg::type<unsigned short>::string()) mat_type = CV_16UC1;
  52674. if (pixel_type()==cimg::type<short>::string()) mat_type = CV_16SC1;
  52675. if (pixel_type()==cimg::type<int>::string()) mat_type = CV_32SC1;
  52676. if (pixel_type()==cimg::type<float>::string()) mat_type = CV_32FC1;
  52677. if (pixel_type()==cimg::type<double>::string()) mat_type = CV_64FC1;
  52678. if (mat_type<0)
  52679. throw CImgInstanceException(_cimg_instance
  52680. "_cvmat2cimg() : pixel type '%s' is not supported.",
  52681. cimg_instance,pixel_type());
  52682. cv::Mat res;
  52683. std::vector<cv::Mat> channels(_spectrum);
  52684. if (_spectrum>1) {
  52685. cimg_forC(*this,c)
  52686. channels[c] = cv::Mat(_height,_width,mat_type,_data + _width*_height*(_spectrum - 1 - c));
  52687. cv::merge(channels,res);
  52688. } else res = cv::Mat(_height,_width,mat_type,_data).clone();
  52689. return res;
  52690. }
  52691. #endif
  52692. //! Load image from a camera stream, using OpenCV.
  52693. /**
  52694. \param index Index of the camera to capture images from (from 0 to 63).
  52695. \param capture_width Width of the desired image ('0' stands for default value).
  52696. \param capture_height Height of the desired image ('0' stands for default value).
  52697. \param skip_frames Number of frames to skip before the capture.
  52698. \param release_camera Tells if the camera resource must be released at the end of the method.
  52699. **/
  52700. CImg<T>& load_camera(const unsigned int camera_index=0,
  52701. const unsigned int capture_width=0, const unsigned int capture_height=0,
  52702. const unsigned int skip_frames=0, const bool release_camera=true) {
  52703. #ifdef cimg_use_opencv
  52704. if (camera_index>=64)
  52705. throw CImgArgumentException(_cimg_instance
  52706. "load_camera(): Invalid request for camera #%u "
  52707. "(no more than 100 cameras can be managed simultaneously).",
  52708. cimg_instance,
  52709. camera_index);
  52710. static cv::VideoCapture *captures[64] = { 0 };
  52711. static unsigned int captures_w[64], captures_h[64];
  52712. if (release_camera) {
  52713. cimg::mutex(9);
  52714. if (captures[camera_index]) captures[camera_index]->release();
  52715. delete captures[camera_index];
  52716. captures[camera_index] = 0;
  52717. captures_w[camera_index] = captures_h[camera_index] = 0;
  52718. cimg::mutex(9,0);
  52719. return *this;
  52720. }
  52721. if (!captures[camera_index]) {
  52722. cimg::mutex(9);
  52723. captures[camera_index] = new cv::VideoCapture(camera_index);
  52724. captures_w[camera_index] = captures_h[camera_index] = 0;
  52725. if (!captures[camera_index]->isOpened()) {
  52726. delete captures[camera_index];
  52727. captures[camera_index] = 0;
  52728. cimg::mutex(9,0);
  52729. throw CImgIOException(_cimg_instance
  52730. "load_camera(): Failed to initialize camera #%u.",
  52731. cimg_instance,
  52732. camera_index);
  52733. }
  52734. cimg::mutex(9,0);
  52735. }
  52736. cimg::mutex(9);
  52737. if (capture_width!=captures_w[camera_index]) {
  52738. captures[camera_index]->set(_cimg_cap_prop_frame_width,capture_width);
  52739. captures_w[camera_index] = capture_width;
  52740. }
  52741. if (capture_height!=captures_h[camera_index]) {
  52742. captures[camera_index]->set(_cimg_cap_prop_frame_height,capture_height);
  52743. captures_h[camera_index] = capture_height;
  52744. }
  52745. for (unsigned int i = 0; i<skip_frames; ++i) captures[camera_index]->grab();
  52746. cv::Mat cvimg;
  52747. captures[camera_index]->read(cvimg);
  52748. if (cvimg.empty()) assign(); else _cvmat2cimg(cvimg).move_to(*this);
  52749. cimg::mutex(9,0);
  52750. return *this;
  52751. #else
  52752. cimg::unused(camera_index,skip_frames,release_camera,capture_width,capture_height);
  52753. throw CImgIOException(_cimg_instance
  52754. "load_camera(): This function requires features from the OpenCV library "
  52755. "('-Dcimg_use_opencv' must be defined).",
  52756. cimg_instance);
  52757. #endif
  52758. }
  52759. //! Load image from a camera stream, using OpenCV \newinstance.
  52760. static CImg<T> get_load_camera(const unsigned int camera_index=0,
  52761. const unsigned int capture_width=0, const unsigned int capture_height=0,
  52762. const unsigned int skip_frames=0, const bool release_camera=true) {
  52763. return CImg<T>().load_camera(camera_index,capture_width,capture_height,skip_frames,release_camera);
  52764. }
  52765. //! Load image using various non-native ways.
  52766. /**
  52767. \param filename Filename, as a C-string.
  52768. **/
  52769. CImg<T>& load_other(const char *const filename) {
  52770. if (!filename)
  52771. throw CImgArgumentException(_cimg_instance
  52772. "load_other(): Specified filename is (null).",
  52773. cimg_instance);
  52774. const unsigned int omode = cimg::exception_mode();
  52775. cimg::exception_mode(0);
  52776. try { load_magick(filename); }
  52777. catch (CImgException&) {
  52778. try { load_imagemagick_external(filename); }
  52779. catch (CImgException&) {
  52780. try { load_graphicsmagick_external(filename); }
  52781. catch (CImgException&) {
  52782. try { load_cimg(filename); }
  52783. catch (CImgException&) {
  52784. try {
  52785. cimg::fclose(cimg::fopen(filename,"rb"));
  52786. } catch (CImgException&) {
  52787. cimg::exception_mode(omode);
  52788. throw CImgIOException(_cimg_instance
  52789. "load_other(): Failed to open file '%s'.",
  52790. cimg_instance,
  52791. filename);
  52792. }
  52793. cimg::exception_mode(omode);
  52794. throw CImgIOException(_cimg_instance
  52795. "load_other(): Failed to recognize format of file '%s'.",
  52796. cimg_instance,
  52797. filename);
  52798. }
  52799. }
  52800. }
  52801. }
  52802. cimg::exception_mode(omode);
  52803. return *this;
  52804. }
  52805. //! Load image using various non-native ways \newinstance.
  52806. static CImg<T> get_load_other(const char *const filename) {
  52807. return CImg<T>().load_other(filename);
  52808. }
  52809. //@}
  52810. //---------------------------
  52811. //
  52812. //! \name Data Output
  52813. //@{
  52814. //---------------------------
  52815. //! Display information about the image data.
  52816. /**
  52817. \param title Name for the considered image.
  52818. \param display_stats Tells to compute and display image statistics.
  52819. **/
  52820. const CImg<T>& print(const char *const title=0, const bool display_stats=true) const {
  52821. int xm = 0, ym = 0, zm = 0, vm = 0, xM = 0, yM = 0, zM = 0, vM = 0;
  52822. CImg<doubleT> st;
  52823. if (!is_empty() && display_stats) {
  52824. st = get_stats();
  52825. xm = (int)st[4]; ym = (int)st[5], zm = (int)st[6], vm = (int)st[7];
  52826. xM = (int)st[8]; yM = (int)st[9], zM = (int)st[10], vM = (int)st[11];
  52827. }
  52828. const ulongT siz = size(), msiz = siz*sizeof(T), siz1 = siz - 1,
  52829. mdisp = msiz<8*1024?0U:msiz<8*1024*1024?1U:2U, width1 = _width - 1;
  52830. CImg<charT> _title(64);
  52831. if (!title) cimg_snprintf(_title,_title._width,"CImg<%s>",pixel_type());
  52832. std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = (%u,%u,%u,%u) [%lu %s], %sdata%s = (%s*)%p",
  52833. cimg::t_magenta,cimg::t_bold,title?title:_title._data,cimg::t_normal,
  52834. cimg::t_bold,cimg::t_normal,(void*)this,
  52835. cimg::t_bold,cimg::t_normal,_width,_height,_depth,_spectrum,
  52836. (unsigned long)(mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20))),
  52837. mdisp==0?"b":(mdisp==1?"Kio":"Mio"),
  52838. cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin());
  52839. if (_data)
  52840. std::fprintf(cimg::output(),"..%p (%s) = [ ",(void*)((char*)end() - 1),_is_shared?"shared":"non-shared");
  52841. else std::fprintf(cimg::output()," (%s) = [ ",_is_shared?"shared":"non-shared");
  52842. if (!is_empty()) cimg_foroff(*this,off) {
  52843. std::fprintf(cimg::output(),"%g",(double)_data[off]);
  52844. if (off!=siz1) std::fprintf(cimg::output(),"%s",off%_width==width1?" ; ":" ");
  52845. if (off==7 && siz>16) { off = siz1 - 8; std::fprintf(cimg::output(),"... "); }
  52846. }
  52847. if (!is_empty() && display_stats)
  52848. std::fprintf(cimg::output(),
  52849. " ], %smin%s = %g, %smax%s = %g, %smean%s = %g, %sstd%s = %g, %scoords_min%s = (%u,%u,%u,%u), "
  52850. "%scoords_max%s = (%u,%u,%u,%u).\n",
  52851. cimg::t_bold,cimg::t_normal,st[0],
  52852. cimg::t_bold,cimg::t_normal,st[1],
  52853. cimg::t_bold,cimg::t_normal,st[2],
  52854. cimg::t_bold,cimg::t_normal,std::sqrt(st[3]),
  52855. cimg::t_bold,cimg::t_normal,xm,ym,zm,vm,
  52856. cimg::t_bold,cimg::t_normal,xM,yM,zM,vM);
  52857. else std::fprintf(cimg::output(),"%s].\n",is_empty()?"":" ");
  52858. std::fflush(cimg::output());
  52859. return *this;
  52860. }
  52861. //! Display image into a CImgDisplay window.
  52862. /**
  52863. \param disp Display window.
  52864. **/
  52865. const CImg<T>& display(CImgDisplay& disp) const {
  52866. disp.display(*this);
  52867. return *this;
  52868. }
  52869. //! Display image into a CImgDisplay window, in an interactive way.
  52870. /**
  52871. \param disp Display window.
  52872. \param display_info Tells if image information are displayed on the standard output.
  52873. \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
  52874. \param exit_on_anykey Exit function when any key is pressed.
  52875. **/
  52876. const CImg<T>& display(CImgDisplay &disp, const bool display_info, unsigned int *const XYZ=0,
  52877. const bool exit_on_anykey=false) const {
  52878. return _display(disp,0,display_info,XYZ,exit_on_anykey,false);
  52879. }
  52880. //! Display image into an interactive window.
  52881. /**
  52882. \param title Window title
  52883. \param display_info Tells if image information are displayed on the standard output.
  52884. \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
  52885. \param exit_on_anykey Exit function when any key is pressed.
  52886. **/
  52887. const CImg<T>& display(const char *const title=0, const bool display_info=true, unsigned int *const XYZ=0,
  52888. const bool exit_on_anykey=false) const {
  52889. CImgDisplay disp;
  52890. return _display(disp,title,display_info,XYZ,exit_on_anykey,false);
  52891. }
  52892. const CImg<T>& _display(CImgDisplay &disp, const char *const title, const bool display_info,
  52893. unsigned int *const XYZ, const bool exit_on_anykey,
  52894. const bool exit_on_singleclick) const {
  52895. unsigned int oldw = 0, oldh = 0, _XYZ[3] = { 0 }, key = 0;
  52896. int x0 = 0, y0 = 0, z0 = 0, x1 = width() - 1, y1 = height() - 1, z1 = depth() - 1,
  52897. old_mouse_x = -1, old_mouse_y = -1;
  52898. if (!disp) {
  52899. disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1);
  52900. if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum);
  52901. else disp.set_title("%s",title);
  52902. } else if (title) disp.set_title("%s",title);
  52903. disp.show().flush();
  52904. const CImg<char> dtitle = CImg<char>::string(disp.title());
  52905. if (display_info) print(dtitle);
  52906. CImg<T> zoom;
  52907. for (bool reset_view = true, resize_disp = false, is_first_select = true; !key && !disp.is_closed(); ) {
  52908. if (reset_view) {
  52909. if (XYZ) { _XYZ[0] = XYZ[0]; _XYZ[1] = XYZ[1]; _XYZ[2] = XYZ[2]; }
  52910. else {
  52911. _XYZ[0] = (unsigned int)(x0 + x1 + 1)/2;
  52912. _XYZ[1] = (unsigned int)(y0 + y1 + 1)/2;
  52913. _XYZ[2] = (unsigned int)(z0 + z1 + 1)/2;
  52914. }
  52915. x0 = 0; y0 = 0; z0 = 0; x1 = width() - 1; y1 = height() - 1; z1 = depth() - 1;
  52916. disp.resize(cimg_fitscreen(_width,_height,_depth),false);
  52917. oldw = disp._width; oldh = disp._height;
  52918. resize_disp = true;
  52919. reset_view = false;
  52920. }
  52921. if (!x0 && !y0 && !z0 && x1==width() - 1 && y1==height() - 1 && z1==depth() - 1) {
  52922. if (is_empty()) zoom.assign(1,1,1,1,(T)0); else zoom.assign();
  52923. } else zoom = get_crop(x0,y0,z0,x1,y1,z1);
  52924. const CImg<T>& visu = zoom?zoom:*this;
  52925. const unsigned int
  52926. dx = 1U + x1 - x0, dy = 1U + y1 - y0, dz = 1U + z1 - z0,
  52927. tw = dx + (dz>1?dz:0U), th = dy + (dz>1?dz:0U);
  52928. if (!is_empty() && !disp.is_fullscreen() && resize_disp) {
  52929. const float
  52930. ttw = (float)tw*disp.width()/oldw, tth = (float)th*disp.height()/oldh,
  52931. dM = std::max(ttw,tth), diM = (float)std::max(disp.width(),disp.height());
  52932. const unsigned int
  52933. imgw = (unsigned int)(ttw*diM/dM), imgh = (unsigned int)(tth*diM/dM);
  52934. disp.set_fullscreen(false).resize(cimg_fitscreen(imgw,imgh,1),false);
  52935. resize_disp = false;
  52936. }
  52937. oldw = tw; oldh = th;
  52938. bool
  52939. go_up = false, go_down = false, go_left = false, go_right = false,
  52940. go_inc = false, go_dec = false, go_in = false, go_out = false,
  52941. go_in_center = false;
  52942. disp.set_title("%s",dtitle._data);
  52943. if (_width>1 && visu._width==1) disp.set_title("%s | x=%u",disp._title,x0);
  52944. if (_height>1 && visu._height==1) disp.set_title("%s | y=%u",disp._title,y0);
  52945. if (_depth>1 && visu._depth==1) disp.set_title("%s | z=%u",disp._title,z0);
  52946. disp._mouse_x = old_mouse_x; disp._mouse_y = old_mouse_y;
  52947. CImg<intT> selection = visu._select(disp,0,2,_XYZ,x0,y0,z0,true,is_first_select,_depth>1,true);
  52948. old_mouse_x = disp._mouse_x; old_mouse_y = disp._mouse_y;
  52949. is_first_select = false;
  52950. if (disp.wheel()) {
  52951. if ((disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) &&
  52952. (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT())) {
  52953. go_left = !(go_right = disp.wheel()>0);
  52954. } else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) {
  52955. go_down = !(go_up = disp.wheel()>0);
  52956. } else if (depth()==1 || disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  52957. go_out = !(go_in = disp.wheel()>0); go_in_center = false;
  52958. }
  52959. disp.set_wheel();
  52960. }
  52961. const int
  52962. sx0 = selection(0), sy0 = selection(1), sz0 = selection(2),
  52963. sx1 = selection(3), sy1 = selection(4), sz1 = selection(5);
  52964. if (sx0>=0 && sy0>=0 && sz0>=0 && sx1>=0 && sy1>=0 && sz1>=0) {
  52965. x1 = x0 + sx1; y1 = y0 + sy1; z1 = z0 + sz1;
  52966. x0+=sx0; y0+=sy0; z0+=sz0;
  52967. if ((sx0==sx1 && sy0==sy1) || (_depth>1 && sx0==sx1 && sz0==sz1) || (_depth>1 && sy0==sy1 && sz0==sz1)) {
  52968. if (exit_on_singleclick && (!zoom || is_empty())) break; else reset_view = true;
  52969. }
  52970. resize_disp = true;
  52971. } else switch (key = disp.key()) {
  52972. #if cimg_OS!=2
  52973. case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT :
  52974. #endif
  52975. case 0 : case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : key = 0; break;
  52976. case cimg::keyP : if (visu._depth>1 && (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT())) {
  52977. // Special mode: play stack of frames
  52978. const unsigned int
  52979. w1 = visu._width*disp.width()/(visu._width + (visu._depth>1?visu._depth:0)),
  52980. h1 = visu._height*disp.height()/(visu._height + (visu._depth>1?visu._depth:0));
  52981. float frame_timing = 5;
  52982. bool is_stopped = false;
  52983. disp.set_key(key,false).set_wheel().resize(cimg_fitscreen(w1,h1,1),false); key = 0;
  52984. for (unsigned int timer = 0; !key && !disp.is_closed() && !disp.button(); ) {
  52985. if (disp.is_resized()) disp.resize(false);
  52986. if (!timer) {
  52987. visu.get_slice((int)_XYZ[2]).display(disp.set_title("%s | z=%d",dtitle.data(),_XYZ[2]));
  52988. (++_XYZ[2])%=visu._depth;
  52989. }
  52990. if (!is_stopped) { if (++timer>(unsigned int)frame_timing) timer = 0; } else timer = ~0U;
  52991. if (disp.wheel()) { frame_timing-=disp.wheel()/3.f; disp.set_wheel(); }
  52992. switch (key = disp.key()) {
  52993. #if cimg_OS!=2
  52994. case cimg::keyCTRLRIGHT :
  52995. #endif
  52996. case cimg::keyCTRLLEFT : key = 0; break;
  52997. case cimg::keyPAGEUP : frame_timing-=0.3f; key = 0; break;
  52998. case cimg::keyPAGEDOWN : frame_timing+=0.3f; key = 0; break;
  52999. case cimg::keySPACE : is_stopped = !is_stopped; disp.set_key(key,false); key = 0; break;
  53000. case cimg::keyARROWLEFT : case cimg::keyARROWUP : is_stopped = true; timer = 0; key = 0; break;
  53001. case cimg::keyARROWRIGHT : case cimg::keyARROWDOWN : is_stopped = true;
  53002. (_XYZ[2]+=visu._depth - 2)%=visu._depth; timer = 0; key = 0; break;
  53003. case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53004. disp.set_fullscreen(false).
  53005. resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
  53006. CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false);
  53007. disp.set_key(key,false); key = 0;
  53008. } break;
  53009. case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53010. disp.set_fullscreen(false).
  53011. resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false).set_key(key,false); key = 0;
  53012. } break;
  53013. case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53014. disp.set_fullscreen(false).
  53015. resize(cimg_fitscreen(_width,_height,_depth),false).set_key(key,false); key = 0;
  53016. } break;
  53017. case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53018. disp.resize(disp.screen_width(),disp.screen_height(),false).
  53019. toggle_fullscreen().set_key(key,false); key = 0;
  53020. } break;
  53021. }
  53022. frame_timing = frame_timing<1?1:(frame_timing>39?39:frame_timing);
  53023. disp.wait(20);
  53024. }
  53025. const unsigned int
  53026. w2 = (visu._width + (visu._depth>1?visu._depth:0))*disp.width()/visu._width,
  53027. h2 = (visu._height + (visu._depth>1?visu._depth:0))*disp.height()/visu._height;
  53028. disp.resize(cimg_fitscreen(w2,h2,1),false).set_title(dtitle.data()).set_key().set_button().set_wheel();
  53029. key = 0;
  53030. } break;
  53031. case cimg::keyHOME : reset_view = resize_disp = true; key = 0; break;
  53032. case cimg::keyPADADD : go_in = true; go_in_center = true; key = 0; break;
  53033. case cimg::keyPADSUB : go_out = true; key = 0; break;
  53034. case cimg::keyARROWLEFT : case cimg::keyPAD4: go_left = true; key = 0; break;
  53035. case cimg::keyARROWRIGHT : case cimg::keyPAD6: go_right = true; key = 0; break;
  53036. case cimg::keyARROWUP : case cimg::keyPAD8: go_up = true; key = 0; break;
  53037. case cimg::keyARROWDOWN : case cimg::keyPAD2: go_down = true; key = 0; break;
  53038. case cimg::keyPAD7 : go_up = go_left = true; key = 0; break;
  53039. case cimg::keyPAD9 : go_up = go_right = true; key = 0; break;
  53040. case cimg::keyPAD1 : go_down = go_left = true; key = 0; break;
  53041. case cimg::keyPAD3 : go_down = go_right = true; key = 0; break;
  53042. case cimg::keyPAGEUP : go_inc = true; key = 0; break;
  53043. case cimg::keyPAGEDOWN : go_dec = true; key = 0; break;
  53044. }
  53045. if (go_in) {
  53046. const int
  53047. mx = go_in_center?disp.width()/2:disp.mouse_x(),
  53048. my = go_in_center?disp.height()/2:disp.mouse_y(),
  53049. mX = mx*(width() + (depth()>1?depth():0))/disp.width(),
  53050. mY = my*(height() + (depth()>1?depth():0))/disp.height();
  53051. int X = (int)_XYZ[0], Y = (int)_XYZ[1], Z = (int)_XYZ[2];
  53052. if (mX<width() && mY<height()) {
  53053. X = x0 + mX*(1 + x1 - x0)/width(); Y = y0 + mY*(1 + y1 - y0)/height();
  53054. }
  53055. if (mX<width() && mY>=height()) {
  53056. X = x0 + mX*(1 + x1 - x0)/width(); Z = z0 + (mY - height())*(1 + z1 - z0)/depth();
  53057. }
  53058. if (mX>=width() && mY<height()) {
  53059. Y = y0 + mY*(1 + y1 - y0)/height(); Z = z0 + (mX - width())*(1 + z1 - z0)/depth();
  53060. }
  53061. if (x1 - x0>4) { x0 = X - 3*(X - x0)/4; x1 = X + 3*(x1 - X)/4; }
  53062. if (y1 - y0>4) { y0 = Y - 3*(Y - y0)/4; y1 = Y + 3*(y1 - Y)/4; }
  53063. if (z1 - z0>4) { z0 = Z - 3*(Z - z0)/4; z1 = Z + 3*(z1 - Z)/4; }
  53064. }
  53065. if (go_out) {
  53066. const int
  53067. delta_x = (x1 - x0)/8, delta_y = (y1 - y0)/8, delta_z = (z1 - z0)/8,
  53068. ndelta_x = delta_x?delta_x:(_width>1),
  53069. ndelta_y = delta_y?delta_y:(_height>1),
  53070. ndelta_z = delta_z?delta_z:(_depth>1);
  53071. x0-=ndelta_x; y0-=ndelta_y; z0-=ndelta_z;
  53072. x1+=ndelta_x; y1+=ndelta_y; z1+=ndelta_z;
  53073. if (x0<0) { x1-=x0; x0 = 0; if (x1>=width()) x1 = width() - 1; }
  53074. if (y0<0) { y1-=y0; y0 = 0; if (y1>=height()) y1 = height() - 1; }
  53075. if (z0<0) { z1-=z0; z0 = 0; if (z1>=depth()) z1 = depth() - 1; }
  53076. if (x1>=width()) { x0-=(x1 - width() + 1); x1 = width() - 1; if (x0<0) x0 = 0; }
  53077. if (y1>=height()) { y0-=(y1 - height() + 1); y1 = height() - 1; if (y0<0) y0 = 0; }
  53078. if (z1>=depth()) { z0-=(z1 - depth() + 1); z1 = depth() - 1; if (z0<0) z0 = 0; }
  53079. const float
  53080. ratio = (float)(x1-x0)/(y1-y0),
  53081. ratiow = (float)disp._width/disp._height,
  53082. sub = std::min(cimg::abs(ratio - ratiow),cimg::abs(1/ratio-1/ratiow));
  53083. if (sub>0.01) resize_disp = true;
  53084. }
  53085. if (go_left) {
  53086. const int delta = (x1 - x0)/4, ndelta = delta?delta:(_width>1);
  53087. if (x0 - ndelta>=0) { x0-=ndelta; x1-=ndelta; }
  53088. else { x1-=x0; x0 = 0; }
  53089. }
  53090. if (go_right) {
  53091. const int delta = (x1 - x0)/4, ndelta = delta?delta:(_width>1);
  53092. if (x1+ndelta<width()) { x0+=ndelta; x1+=ndelta; }
  53093. else { x0+=(width() - 1 - x1); x1 = width() - 1; }
  53094. }
  53095. if (go_up) {
  53096. const int delta = (y1 - y0)/4, ndelta = delta?delta:(_height>1);
  53097. if (y0 - ndelta>=0) { y0-=ndelta; y1-=ndelta; }
  53098. else { y1-=y0; y0 = 0; }
  53099. }
  53100. if (go_down) {
  53101. const int delta = (y1 - y0)/4, ndelta = delta?delta:(_height>1);
  53102. if (y1+ndelta<height()) { y0+=ndelta; y1+=ndelta; }
  53103. else { y0+=(height() - 1 - y1); y1 = height() - 1; }
  53104. }
  53105. if (go_inc) {
  53106. const int delta = (z1 - z0)/4, ndelta = delta?delta:(_depth>1);
  53107. if (z0 - ndelta>=0) { z0-=ndelta; z1-=ndelta; }
  53108. else { z1-=z0; z0 = 0; }
  53109. }
  53110. if (go_dec) {
  53111. const int delta = (z1 - z0)/4, ndelta = delta?delta:(_depth>1);
  53112. if (z1+ndelta<depth()) { z0+=ndelta; z1+=ndelta; }
  53113. else { z0+=(depth() - 1 - z1); z1 = depth() - 1; }
  53114. }
  53115. disp.wait(100);
  53116. if (!exit_on_anykey && key && key!=cimg::keyESC &&
  53117. (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  53118. key = 0;
  53119. }
  53120. }
  53121. disp.set_key(key);
  53122. if (XYZ) { XYZ[0] = _XYZ[0]; XYZ[1] = _XYZ[1]; XYZ[2] = _XYZ[2]; }
  53123. return *this;
  53124. }
  53125. //! Display object 3D in an interactive window.
  53126. /**
  53127. \param disp Display window.
  53128. \param vertices Vertices data of the 3D object.
  53129. \param primitives Primitives data of the 3D object.
  53130. \param colors Colors data of the 3D object.
  53131. \param opacities Opacities data of the 3D object.
  53132. \param centering Tells if the 3D object must be centered for the display.
  53133. \param render_static Rendering mode.
  53134. \param render_motion Rendering mode, when the 3D object is moved.
  53135. \param is_double_sided Tells if the object primitives are double-sided.
  53136. \param focale Focale
  53137. \param light_x X-coordinate of the light source.
  53138. \param light_y Y-coordinate of the light source.
  53139. \param light_z Z-coordinate of the light source.
  53140. \param specular_lightness Amount of specular light.
  53141. \param specular_shininess Shininess of the object material.
  53142. \param display_axes Tells if the 3D axes are displayed.
  53143. \param pose_matrix Pointer to 12 values, defining a 3D pose (as a 4x3 matrix).
  53144. \param exit_on_anykey Exit function when any key is pressed.
  53145. **/
  53146. template<typename tp, typename tf, typename tc, typename to>
  53147. const CImg<T>& display_object3d(CImgDisplay& disp,
  53148. const CImg<tp>& vertices,
  53149. const CImgList<tf>& primitives,
  53150. const CImgList<tc>& colors,
  53151. const to& opacities,
  53152. const bool centering=true,
  53153. const int render_static=4, const int render_motion=1,
  53154. const bool is_double_sided=true, const float focale=700,
  53155. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53156. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53157. const bool display_axes=true, float *const pose_matrix=0,
  53158. const bool exit_on_anykey=false) const {
  53159. return _display_object3d(disp,0,vertices,primitives,colors,opacities,centering,render_static,
  53160. render_motion,is_double_sided,focale,
  53161. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53162. display_axes,pose_matrix,exit_on_anykey);
  53163. }
  53164. //! Display object 3D in an interactive window \simplification.
  53165. template<typename tp, typename tf, typename tc, typename to>
  53166. const CImg<T>& display_object3d(const char *const title,
  53167. const CImg<tp>& vertices,
  53168. const CImgList<tf>& primitives,
  53169. const CImgList<tc>& colors,
  53170. const to& opacities,
  53171. const bool centering=true,
  53172. const int render_static=4, const int render_motion=1,
  53173. const bool is_double_sided=true, const float focale=700,
  53174. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53175. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53176. const bool display_axes=true, float *const pose_matrix=0,
  53177. const bool exit_on_anykey=false) const {
  53178. CImgDisplay disp;
  53179. return _display_object3d(disp,title,vertices,primitives,colors,opacities,centering,render_static,
  53180. render_motion,is_double_sided,focale,
  53181. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53182. display_axes,pose_matrix,exit_on_anykey);
  53183. }
  53184. //! Display object 3D in an interactive window \simplification.
  53185. template<typename tp, typename tf, typename tc>
  53186. const CImg<T>& display_object3d(CImgDisplay &disp,
  53187. const CImg<tp>& vertices,
  53188. const CImgList<tf>& primitives,
  53189. const CImgList<tc>& colors,
  53190. const bool centering=true,
  53191. const int render_static=4, const int render_motion=1,
  53192. const bool is_double_sided=true, const float focale=700,
  53193. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53194. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53195. const bool display_axes=true, float *const pose_matrix=0,
  53196. const bool exit_on_anykey=false) const {
  53197. return display_object3d(disp,vertices,primitives,colors,CImgList<floatT>(),centering,
  53198. render_static,render_motion,is_double_sided,focale,
  53199. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53200. display_axes,pose_matrix,exit_on_anykey);
  53201. }
  53202. //! Display object 3D in an interactive window \simplification.
  53203. template<typename tp, typename tf, typename tc>
  53204. const CImg<T>& display_object3d(const char *const title,
  53205. const CImg<tp>& vertices,
  53206. const CImgList<tf>& primitives,
  53207. const CImgList<tc>& colors,
  53208. const bool centering=true,
  53209. const int render_static=4, const int render_motion=1,
  53210. const bool is_double_sided=true, const float focale=700,
  53211. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53212. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53213. const bool display_axes=true, float *const pose_matrix=0,
  53214. const bool exit_on_anykey=false) const {
  53215. return display_object3d(title,vertices,primitives,colors,CImgList<floatT>(),centering,
  53216. render_static,render_motion,is_double_sided,focale,
  53217. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53218. display_axes,pose_matrix,exit_on_anykey);
  53219. }
  53220. //! Display object 3D in an interactive window \simplification.
  53221. template<typename tp, typename tf>
  53222. const CImg<T>& display_object3d(CImgDisplay &disp,
  53223. const CImg<tp>& vertices,
  53224. const CImgList<tf>& primitives,
  53225. const bool centering=true,
  53226. const int render_static=4, const int render_motion=1,
  53227. const bool is_double_sided=true, const float focale=700,
  53228. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53229. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53230. const bool display_axes=true, float *const pose_matrix=0,
  53231. const bool exit_on_anykey=false) const {
  53232. return display_object3d(disp,vertices,primitives,CImgList<T>(),centering,
  53233. render_static,render_motion,is_double_sided,focale,
  53234. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53235. display_axes,pose_matrix,exit_on_anykey);
  53236. }
  53237. //! Display object 3D in an interactive window \simplification.
  53238. template<typename tp, typename tf>
  53239. const CImg<T>& display_object3d(const char *const title,
  53240. const CImg<tp>& vertices,
  53241. const CImgList<tf>& primitives,
  53242. const bool centering=true,
  53243. const int render_static=4, const int render_motion=1,
  53244. const bool is_double_sided=true, const float focale=700,
  53245. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53246. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53247. const bool display_axes=true, float *const pose_matrix=0,
  53248. const bool exit_on_anykey=false) const {
  53249. return display_object3d(title,vertices,primitives,CImgList<T>(),centering,
  53250. render_static,render_motion,is_double_sided,focale,
  53251. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53252. display_axes,pose_matrix,exit_on_anykey);
  53253. }
  53254. //! Display object 3D in an interactive window \simplification.
  53255. template<typename tp>
  53256. const CImg<T>& display_object3d(CImgDisplay &disp,
  53257. const CImg<tp>& vertices,
  53258. const bool centering=true,
  53259. const int render_static=4, const int render_motion=1,
  53260. const bool is_double_sided=true, const float focale=700,
  53261. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53262. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53263. const bool display_axes=true, float *const pose_matrix=0,
  53264. const bool exit_on_anykey=false) const {
  53265. return display_object3d(disp,vertices,CImgList<uintT>(),centering,
  53266. render_static,render_motion,is_double_sided,focale,
  53267. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53268. display_axes,pose_matrix,exit_on_anykey);
  53269. }
  53270. //! Display object 3D in an interactive window \simplification.
  53271. template<typename tp>
  53272. const CImg<T>& display_object3d(const char *const title,
  53273. const CImg<tp>& vertices,
  53274. const bool centering=true,
  53275. const int render_static=4, const int render_motion=1,
  53276. const bool is_double_sided=true, const float focale=700,
  53277. const float light_x=0, const float light_y=0, const float light_z=-5e8f,
  53278. const float specular_lightness=0.2f, const float specular_shininess=0.1f,
  53279. const bool display_axes=true, float *const pose_matrix=0,
  53280. const bool exit_on_anykey=false) const {
  53281. return display_object3d(title,vertices,CImgList<uintT>(),centering,
  53282. render_static,render_motion,is_double_sided,focale,
  53283. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53284. display_axes,pose_matrix,exit_on_anykey);
  53285. }
  53286. template<typename tp, typename tf, typename tc, typename to>
  53287. const CImg<T>& _display_object3d(CImgDisplay& disp, const char *const title,
  53288. const CImg<tp>& vertices,
  53289. const CImgList<tf>& primitives,
  53290. const CImgList<tc>& colors,
  53291. const to& opacities,
  53292. const bool centering,
  53293. const int render_static, const int render_motion,
  53294. const bool is_double_sided, const float focale,
  53295. const float light_x, const float light_y, const float light_z,
  53296. const float specular_lightness, const float specular_shininess,
  53297. const bool display_axes, float *const pose_matrix,
  53298. const bool exit_on_anykey) const {
  53299. typedef typename cimg::superset<tp,float>::type tpfloat;
  53300. // Check input arguments
  53301. if (is_empty()) {
  53302. CImg<T> background;
  53303. if (colors && colors[0].size()==1) background.assign(1,2,1,1,64,128);
  53304. else background.assign(1,2,1,3,32,64,32,116,64,96);
  53305. if (disp) background.resize(disp.width(),disp.height(),1,-100,3);
  53306. else background.resize(cimg_fitscreen(CImgDisplay::screen_width()/2,
  53307. CImgDisplay::screen_height()/2,1),1,-100,3);
  53308. return background._display_object3d(disp,title,vertices,primitives,colors,opacities,centering,
  53309. render_static,render_motion,is_double_sided,focale,
  53310. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53311. display_axes,pose_matrix,exit_on_anykey);
  53312. } else { if (disp) disp.resize(*this,false); }
  53313. CImg<charT> error_message(1024);
  53314. if (!vertices.is_object3d(primitives,colors,opacities,true,error_message))
  53315. throw CImgArgumentException(_cimg_instance
  53316. "display_object3d(): Invalid specified 3D object (%u,%u) (%s).",
  53317. cimg_instance,vertices._width,primitives._width,error_message.data());
  53318. if (vertices._width && !primitives) {
  53319. CImgList<tf> nprimitives(vertices._width,1,1,1,1);
  53320. cimglist_for(nprimitives,l) nprimitives(l,0) = (tf)l;
  53321. return _display_object3d(disp,title,vertices,nprimitives,colors,opacities,centering,
  53322. render_static,render_motion,is_double_sided,focale,
  53323. light_x,light_y,light_z,specular_lightness,specular_shininess,
  53324. display_axes,pose_matrix,exit_on_anykey);
  53325. }
  53326. if (!disp) {
  53327. disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,3);
  53328. if (!title) disp.set_title("CImg<%s> (%u vertices, %u primitives)",
  53329. pixel_type(),vertices._width,primitives._width);
  53330. } else if (title) disp.set_title("%s",title);
  53331. // Init 3D objects and compute object statistics
  53332. CImg<floatT>
  53333. pose,
  53334. rotated_vertices(vertices._width,3),
  53335. bbox_vertices, rotated_bbox_vertices,
  53336. axes_vertices, rotated_axes_vertices,
  53337. bbox_opacities, axes_opacities;
  53338. CImgList<uintT> bbox_primitives, axes_primitives;
  53339. CImgList<tf> reverse_primitives;
  53340. CImgList<T> bbox_colors, bbox_colors2, axes_colors;
  53341. unsigned int ns_width = 0, ns_height = 0;
  53342. int _is_double_sided = (int)is_double_sided;
  53343. bool ndisplay_axes = display_axes;
  53344. const CImg<T>
  53345. background_color(1,1,1,_spectrum,0),
  53346. foreground_color(1,1,1,_spectrum,(T)std::min((int)cimg::type<T>::max(),255));
  53347. float
  53348. Xoff = 0, Yoff = 0, Zoff = 0, sprite_scale = 1,
  53349. xm = 0, xM = vertices?vertices.get_shared_row(0).max_min(xm):0,
  53350. ym = 0, yM = vertices?vertices.get_shared_row(1).max_min(ym):0,
  53351. zm = 0, zM = vertices?vertices.get_shared_row(2).max_min(zm):0;
  53352. const float delta = cimg::max(xM - xm,yM - ym,zM - zm);
  53353. rotated_bbox_vertices = bbox_vertices.assign(8,3,1,1,
  53354. xm,xM,xM,xm,xm,xM,xM,xm,
  53355. ym,ym,yM,yM,ym,ym,yM,yM,
  53356. zm,zm,zm,zm,zM,zM,zM,zM);
  53357. bbox_primitives.assign(6,1,4,1,1, 0,3,2,1, 4,5,6,7, 1,2,6,5, 0,4,7,3, 0,1,5,4, 2,3,7,6);
  53358. bbox_colors.assign(6,_spectrum,1,1,1,background_color[0]);
  53359. bbox_colors2.assign(6,_spectrum,1,1,1,foreground_color[0]);
  53360. bbox_opacities.assign(bbox_colors._width,1,1,1,0.3f);
  53361. rotated_axes_vertices = axes_vertices.assign(7,3,1,1,
  53362. 0,20,0,0,22,-6,-6,
  53363. 0,0,20,0,-6,22,-6,
  53364. 0,0,0,20,0,0,22);
  53365. axes_opacities.assign(3,1,1,1,1);
  53366. axes_colors.assign(3,_spectrum,1,1,1,foreground_color[0]);
  53367. axes_primitives.assign(3,1,2,1,1, 0,1, 0,2, 0,3);
  53368. // Begin user interaction loop
  53369. CImg<T> visu0(*this,false), visu;
  53370. CImg<tpfloat> zbuffer(visu0.width(),visu0.height(),1,1,0);
  53371. bool init_pose = true, clicked = false, redraw = true;
  53372. unsigned int key = 0, font_size = 32;
  53373. int
  53374. x0 = 0, y0 = 0, x1 = 0, y1 = 0,
  53375. nrender_static = render_static,
  53376. nrender_motion = render_motion;
  53377. disp.show().flush();
  53378. while (!disp.is_closed() && !key) {
  53379. // Init object pose
  53380. if (init_pose) {
  53381. const float
  53382. ratio = delta>0?(2.f*std::min(disp.width(),disp.height())/(3.f*delta)):1,
  53383. dx = (xM + xm)/2, dy = (yM + ym)/2, dz = (zM + zm)/2;
  53384. if (centering)
  53385. CImg<floatT>(4,3,1,1, ratio,0.,0.,-ratio*dx, 0.,ratio,0.,-ratio*dy, 0.,0.,ratio,-ratio*dz).move_to(pose);
  53386. else CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose);
  53387. if (pose_matrix) {
  53388. CImg<floatT> pose0(pose_matrix,4,3,1,1,false);
  53389. pose0.resize(4,4,1,1,0); pose.resize(4,4,1,1,0);
  53390. pose0(3,3) = pose(3,3) = 1;
  53391. (pose0*pose).get_crop(0,0,3,2).move_to(pose);
  53392. Xoff = pose_matrix[12]; Yoff = pose_matrix[13]; Zoff = pose_matrix[14]; sprite_scale = pose_matrix[15];
  53393. } else { Xoff = Yoff = Zoff = 0; sprite_scale = 1; }
  53394. init_pose = false;
  53395. redraw = true;
  53396. }
  53397. // Rotate and draw 3D object
  53398. if (redraw) {
  53399. const float
  53400. r00 = pose(0,0), r10 = pose(1,0), r20 = pose(2,0), r30 = pose(3,0),
  53401. r01 = pose(0,1), r11 = pose(1,1), r21 = pose(2,1), r31 = pose(3,1),
  53402. r02 = pose(0,2), r12 = pose(1,2), r22 = pose(2,2), r32 = pose(3,2);
  53403. if ((clicked && nrender_motion>=0) || (!clicked && nrender_static>=0)) {
  53404. const tp *const pv0 = vertices.data(), *const pv1 = vertices.data(0,1), *const pv2 = vertices.data(0,2);
  53405. float
  53406. *const prv0 = rotated_vertices.data(),
  53407. *const prv1 = rotated_vertices.data(0,1),
  53408. *const prv2 = rotated_vertices.data(0,2);
  53409. cimg_pragma_openmp(parallel for cimg_openmp_if(vertices.width()>(cimg_openmp_sizefactor)*1024))
  53410. cimg_forX(vertices,l) {
  53411. const float x = (float)pv0[l], y = (float)pv1[l], z = (float)pv2[l];
  53412. prv0[l] = r00*x + r10*y + r20*z + r30;
  53413. prv1[l] = r01*x + r11*y + r21*z + r31;
  53414. prv2[l] = r02*x + r12*y + r22*z + r32;
  53415. }
  53416. }
  53417. else cimg_forX(bbox_vertices,l) {
  53418. const float x = bbox_vertices(l,0), y = bbox_vertices(l,1), z = bbox_vertices(l,2);
  53419. rotated_bbox_vertices(l,0) = r00*x + r10*y + r20*z + r30;
  53420. rotated_bbox_vertices(l,1) = r01*x + r11*y + r21*z + r31;
  53421. rotated_bbox_vertices(l,2) = r02*x + r12*y + r22*z + r32;
  53422. }
  53423. // Draw objects
  53424. const bool render_with_zbuffer = !clicked && nrender_static>0;
  53425. visu = visu0;
  53426. if ((clicked && nrender_motion<0) || (!clicked && nrender_static<0))
  53427. visu.draw_object3d(Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
  53428. rotated_bbox_vertices,bbox_primitives,bbox_colors,bbox_opacities,2,false,focale).
  53429. draw_object3d(Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
  53430. rotated_bbox_vertices,bbox_primitives,bbox_colors2,1,false,focale);
  53431. else visu._draw_object3d((void*)0,render_with_zbuffer?zbuffer.fill(0):CImg<tpfloat>::empty(),
  53432. Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
  53433. rotated_vertices,reverse_primitives?reverse_primitives:primitives,
  53434. colors,opacities,clicked?nrender_motion:nrender_static,_is_double_sided==1,focale,
  53435. width()/2.f + light_x,height()/2.f + light_y,light_z + Zoff,
  53436. specular_lightness,specular_shininess,1,sprite_scale);
  53437. // Draw axes
  53438. if (ndisplay_axes) {
  53439. const float
  53440. n = 1e-8f + cimg::hypot(r00,r01,r02),
  53441. _r00 = r00/n, _r10 = r10/n, _r20 = r20/n,
  53442. _r01 = r01/n, _r11 = r11/n, _r21 = r21/n,
  53443. _r02 = r01/n, _r12 = r12/n, _r22 = r22/n,
  53444. Xaxes = 25, Yaxes = visu._height - 38.f;
  53445. cimg_forX(axes_vertices,l) {
  53446. const float
  53447. x = axes_vertices(l,0),
  53448. y = axes_vertices(l,1),
  53449. z = axes_vertices(l,2);
  53450. rotated_axes_vertices(l,0) = _r00*x + _r10*y + _r20*z;
  53451. rotated_axes_vertices(l,1) = _r01*x + _r11*y + _r21*z;
  53452. rotated_axes_vertices(l,2) = _r02*x + _r12*y + _r22*z;
  53453. }
  53454. axes_opacities(0,0) = (rotated_axes_vertices(1,2)>0)?0.5f:1.f;
  53455. axes_opacities(1,0) = (rotated_axes_vertices(2,2)>0)?0.5f:1.f;
  53456. axes_opacities(2,0) = (rotated_axes_vertices(3,2)>0)?0.5f:1.f;
  53457. visu.draw_object3d(Xaxes,Yaxes,0,rotated_axes_vertices,axes_primitives,
  53458. axes_colors,axes_opacities,1,false,focale).
  53459. draw_text((int)(Xaxes + rotated_axes_vertices(4,0)),
  53460. (int)(Yaxes + rotated_axes_vertices(4,1)),
  53461. "X",axes_colors[0]._data,0,axes_opacities(0,0),13).
  53462. draw_text((int)(Xaxes + rotated_axes_vertices(5,0)),
  53463. (int)(Yaxes + rotated_axes_vertices(5,1)),
  53464. "Y",axes_colors[1]._data,0,axes_opacities(1,0),13).
  53465. draw_text((int)(Xaxes + rotated_axes_vertices(6,0)),
  53466. (int)(Yaxes + rotated_axes_vertices(6,1)),
  53467. "Z",axes_colors[2]._data,0,axes_opacities(2,0),13);
  53468. }
  53469. visu.display(disp);
  53470. if (!clicked || nrender_motion==nrender_static) redraw = false;
  53471. }
  53472. // Handle user interaction
  53473. if (!redraw) disp.wait();
  53474. if ((disp.button() || disp.wheel()) && disp.mouse_x()>=0 && disp.mouse_y()>=0) {
  53475. redraw = true;
  53476. if (!clicked) { x0 = x1 = disp.mouse_x(); y0 = y1 = disp.mouse_y(); if (!disp.wheel()) clicked = true; }
  53477. else { x1 = disp.mouse_x(); y1 = disp.mouse_y(); }
  53478. const bool is_keyCTRL = disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT();
  53479. if (disp.button()&1 && !is_keyCTRL) {
  53480. const float
  53481. R = 0.45f*std::min(disp.width(),disp.height()),
  53482. R2 = R*R,
  53483. u0 = (float)(x0 - disp.width()/2),
  53484. v0 = (float)(y0 - disp.height()/2),
  53485. u1 = (float)(x1 - disp.width()/2),
  53486. v1 = (float)(y1 - disp.height()/2),
  53487. n0 = cimg::hypot(u0,v0),
  53488. n1 = cimg::hypot(u1,v1),
  53489. nu0 = n0>R?(u0*R/n0):u0,
  53490. nv0 = n0>R?(v0*R/n0):v0,
  53491. nw0 = (float)std::sqrt(std::max(0.f,R2 - nu0*nu0 - nv0*nv0)),
  53492. nu1 = n1>R?(u1*R/n1):u1,
  53493. nv1 = n1>R?(v1*R/n1):v1,
  53494. nw1 = (float)std::sqrt(std::max(0.f,R2 - nu1*nu1 - nv1*nv1)),
  53495. u = nv0*nw1 - nw0*nv1,
  53496. v = nw0*nu1 - nu0*nw1,
  53497. w = nv0*nu1 - nu0*nv1,
  53498. n = cimg::hypot(u,v,w),
  53499. alpha = (float)std::asin(n/R2)*180/cimg::PI;
  53500. (CImg<floatT>::rotation_matrix(u,v,w,-alpha)*pose).move_to(pose);
  53501. x0 = x1; y0 = y1;
  53502. }
  53503. if (disp.button()&2 && !is_keyCTRL) {
  53504. if (focale>0) Zoff-=(y0 - y1)*focale/400;
  53505. else { const float s = std::exp((y0 - y1)/400.f); pose*=s; sprite_scale*=s; }
  53506. x0 = x1; y0 = y1;
  53507. }
  53508. if (disp.wheel()) {
  53509. if (focale>0) Zoff-=disp.wheel()*focale/20;
  53510. else { const float s = std::exp(disp.wheel()/20.f); pose*=s; sprite_scale*=s; }
  53511. disp.set_wheel();
  53512. }
  53513. if (disp.button()&4 || (disp.button()&1 && is_keyCTRL)) {
  53514. Xoff+=(x1 - x0); Yoff+=(y1 - y0); x0 = x1; y0 = y1;
  53515. }
  53516. if ((disp.button()&1) && (disp.button()&2) && !is_keyCTRL) {
  53517. init_pose = true; disp.set_button(); x0 = x1; y0 = y1;
  53518. pose = CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0);
  53519. }
  53520. } else if (clicked) { x0 = x1; y0 = y1; clicked = false; redraw = true; }
  53521. CImg<charT> filename(32);
  53522. switch (key = disp.key()) {
  53523. #if cimg_OS!=2
  53524. case cimg::keyCTRLRIGHT :
  53525. #endif
  53526. case 0 : case cimg::keyCTRLLEFT : key = 0; break;
  53527. case cimg::keyD: if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53528. disp.set_fullscreen(false).
  53529. resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
  53530. CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
  53531. _is_resized = true;
  53532. disp.set_key(key,false); key = 0;
  53533. } break;
  53534. case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53535. disp.set_fullscreen(false).
  53536. resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
  53537. disp.set_key(key,false); key = 0;
  53538. } break;
  53539. case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53540. disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true;
  53541. disp.set_key(key,false); key = 0;
  53542. } break;
  53543. case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53544. if (!ns_width || !ns_height ||
  53545. ns_width>(unsigned int)disp.screen_width() || ns_height>(unsigned int)disp.screen_height()) {
  53546. ns_width = disp.screen_width()*3U/4;
  53547. ns_height = disp.screen_height()*3U/4;
  53548. }
  53549. if (disp.is_fullscreen()) disp.resize(ns_width,ns_height,false);
  53550. else {
  53551. ns_width = disp._width; ns_height = disp._height;
  53552. disp.resize(disp.screen_width(),disp.screen_height(),false);
  53553. }
  53554. disp.toggle_fullscreen()._is_resized = true;
  53555. disp.set_key(key,false); key = 0;
  53556. } break;
  53557. case cimg::keyT : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53558. // Switch single/double-sided primitives.
  53559. if (--_is_double_sided==-2) _is_double_sided = 1;
  53560. if (_is_double_sided>=0) reverse_primitives.assign();
  53561. else primitives.get_reverse_object3d().move_to(reverse_primitives);
  53562. disp.set_key(key,false); key = 0; redraw = true;
  53563. } break;
  53564. case cimg::keyZ : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Enable/disable Z-buffer
  53565. if (zbuffer) zbuffer.assign();
  53566. else zbuffer.assign(visu0.width(),visu0.height(),1,1,0);
  53567. disp.set_key(key,false); key = 0; redraw = true;
  53568. } break;
  53569. case cimg::keyX : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Show/hide 3D axes
  53570. ndisplay_axes = !ndisplay_axes;
  53571. disp.set_key(key,false); key = 0; redraw = true;
  53572. } break;
  53573. case cimg::keyF1 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to points
  53574. nrender_motion = (nrender_static==0 && nrender_motion!=0)?0:-1; nrender_static = 0;
  53575. disp.set_key(key,false); key = 0; redraw = true;
  53576. } break;
  53577. case cimg::keyF2 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to lines
  53578. nrender_motion = (nrender_static==1 && nrender_motion!=1)?1:-1; nrender_static = 1;
  53579. disp.set_key(key,false); key = 0; redraw = true;
  53580. } break;
  53581. case cimg::keyF3 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat
  53582. nrender_motion = (nrender_static==2 && nrender_motion!=2)?2:-1; nrender_static = 2;
  53583. disp.set_key(key,false); key = 0; redraw = true;
  53584. } break;
  53585. case cimg::keyF4 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat-shaded
  53586. nrender_motion = (nrender_static==3 && nrender_motion!=3)?3:-1; nrender_static = 3;
  53587. disp.set_key(key,false); key = 0; redraw = true;
  53588. } break;
  53589. case cimg::keyF5 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53590. // Set rendering mode to gouraud-shaded.
  53591. nrender_motion = (nrender_static==4 && nrender_motion!=4)?4:-1; nrender_static = 4;
  53592. disp.set_key(key,false); key = 0; redraw = true;
  53593. } break;
  53594. case cimg::keyF6 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to phong-shaded
  53595. nrender_motion = (nrender_static==5 && nrender_motion!=5)?5:-1; nrender_static = 5;
  53596. disp.set_key(key,false); key = 0; redraw = true;
  53597. } break;
  53598. case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save snapshot
  53599. static unsigned int snap_number = 0;
  53600. std::FILE *file;
  53601. do {
  53602. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
  53603. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  53604. } while (file);
  53605. (+visu).__draw_text(" Saving snapshot... ",font_size,0).display(disp);
  53606. visu.save(filename);
  53607. (+visu).__draw_text(" Snapshot '%s' saved. ",font_size,0,filename._data).display(disp);
  53608. disp.set_key(key,false); key = 0;
  53609. } break;
  53610. case cimg::keyG : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .off file
  53611. static unsigned int snap_number = 0;
  53612. std::FILE *file;
  53613. do {
  53614. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.off",snap_number++);
  53615. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  53616. } while (file);
  53617. (+visu).__draw_text(" Saving object... ",font_size,0).display(disp);
  53618. vertices.save_off(reverse_primitives?reverse_primitives:primitives,colors,filename);
  53619. (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
  53620. disp.set_key(key,false); key = 0;
  53621. } break;
  53622. case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .cimg file
  53623. static unsigned int snap_number = 0;
  53624. std::FILE *file;
  53625. do {
  53626. #ifdef cimg_use_zlib
  53627. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
  53628. #else
  53629. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
  53630. #endif
  53631. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  53632. } while (file);
  53633. (+visu).__draw_text(" Saving object... ",font_size,0).display(disp);
  53634. vertices.get_object3dtoCImg3d(reverse_primitives?reverse_primitives:primitives,colors,opacities).
  53635. save(filename);
  53636. (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
  53637. disp.set_key(key,false); key = 0;
  53638. } break;
  53639. #ifdef cimg_use_board
  53640. case cimg::keyP : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .EPS file
  53641. static unsigned int snap_number = 0;
  53642. std::FILE *file;
  53643. do {
  53644. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.eps",snap_number++);
  53645. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  53646. } while (file);
  53647. (+visu).__draw_text(" Saving EPS snapshot... ",font_size,0).display(disp);
  53648. LibBoard::Board board;
  53649. (+visu)._draw_object3d(&board,zbuffer.fill(0),
  53650. Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
  53651. rotated_vertices,reverse_primitives?reverse_primitives:primitives,
  53652. colors,opacities,clicked?nrender_motion:nrender_static,
  53653. _is_double_sided==1,focale,
  53654. visu.width()/2.f + light_x,visu.height()/2.f + light_y,light_z + Zoff,
  53655. specular_lightness,specular_shininess,1,
  53656. sprite_scale);
  53657. board.saveEPS(filename);
  53658. (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
  53659. disp.set_key(key,false); key = 0;
  53660. } break;
  53661. case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .SVG file
  53662. static unsigned int snap_number = 0;
  53663. std::FILE *file;
  53664. do {
  53665. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.svg",snap_number++);
  53666. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  53667. } while (file);
  53668. (+visu).__draw_text(" Saving SVG snapshot... ",font_size,0).display(disp);
  53669. LibBoard::Board board;
  53670. (+visu)._draw_object3d(&board,zbuffer.fill(0),
  53671. Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
  53672. rotated_vertices,reverse_primitives?reverse_primitives:primitives,
  53673. colors,opacities,clicked?nrender_motion:nrender_static,
  53674. _is_double_sided==1,focale,
  53675. visu.width()/2.f + light_x,visu.height()/2.f + light_y,light_z + Zoff,
  53676. specular_lightness,specular_shininess,1,
  53677. sprite_scale);
  53678. board.saveSVG(filename);
  53679. (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
  53680. disp.set_key(key,false); key = 0;
  53681. } break;
  53682. #endif
  53683. }
  53684. if (disp.is_resized()) {
  53685. disp.resize(false); visu0 = get_resize(disp,1);
  53686. if (zbuffer) zbuffer.assign(disp.width(),disp.height());
  53687. redraw = true;
  53688. }
  53689. if (!exit_on_anykey && key && key!=cimg::keyESC &&
  53690. (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  53691. key = 0;
  53692. }
  53693. }
  53694. if (pose_matrix) {
  53695. std::memcpy(pose_matrix,pose._data,12*sizeof(float));
  53696. pose_matrix[12] = Xoff; pose_matrix[13] = Yoff; pose_matrix[14] = Zoff; pose_matrix[15] = sprite_scale;
  53697. }
  53698. disp.set_button().set_key(key);
  53699. return *this;
  53700. }
  53701. //! Display 1D graph in an interactive window.
  53702. /**
  53703. \param disp Display window.
  53704. \param plot_type Plot type. Can be <tt>{ 0=points | 1=segments | 2=splines | 3=bars }</tt>.
  53705. \param vertex_type Vertex type.
  53706. \param labelx Title for the horizontal axis, as a C-string.
  53707. \param xmin Minimum value along the X-axis.
  53708. \param xmax Maximum value along the X-axis.
  53709. \param labely Title for the vertical axis, as a C-string.
  53710. \param ymin Minimum value along the X-axis.
  53711. \param ymax Maximum value along the X-axis.
  53712. \param exit_on_anykey Exit function when any key is pressed.
  53713. **/
  53714. const CImg<T>& display_graph(CImgDisplay &disp,
  53715. const unsigned int plot_type=1, const unsigned int vertex_type=1,
  53716. const char *const labelx=0, const double xmin=0, const double xmax=0,
  53717. const char *const labely=0, const double ymin=0, const double ymax=0,
  53718. const bool exit_on_anykey=false) const {
  53719. return _display_graph(disp,0,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax,exit_on_anykey);
  53720. }
  53721. //! Display 1D graph in an interactive window \overloading.
  53722. const CImg<T>& display_graph(const char *const title=0,
  53723. const unsigned int plot_type=1, const unsigned int vertex_type=1,
  53724. const char *const labelx=0, const double xmin=0, const double xmax=0,
  53725. const char *const labely=0, const double ymin=0, const double ymax=0,
  53726. const bool exit_on_anykey=false) const {
  53727. CImgDisplay disp;
  53728. return _display_graph(disp,title,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax,exit_on_anykey);
  53729. }
  53730. const CImg<T>& _display_graph(CImgDisplay &disp, const char *const title=0,
  53731. const unsigned int plot_type=1, const unsigned int vertex_type=1,
  53732. const char *const labelx=0, const double xmin=0, const double xmax=0,
  53733. const char *const labely=0, const double ymin=0, const double ymax=0,
  53734. const bool exit_on_anykey=false) const {
  53735. if (is_empty())
  53736. throw CImgInstanceException(_cimg_instance
  53737. "display_graph(): Empty instance.",
  53738. cimg_instance);
  53739. if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
  53740. set_title(title?"%s":"CImg<%s>",title?title:pixel_type());
  53741. const ulongT siz = (ulongT)_width*_height*_depth, siz1 = std::max((ulongT)1,siz - 1);
  53742. const unsigned int old_normalization = disp.normalization();
  53743. disp.show().flush()._normalization = 0;
  53744. double y0 = ymin, y1 = ymax, nxmin = xmin, nxmax = xmax;
  53745. if (nxmin==nxmax) { nxmin = 0; nxmax = siz1; }
  53746. int x0 = 0, x1 = width()*height()*depth() - 1, key = 0;
  53747. for (bool reset_view = true; !key && !disp.is_closed(); ) {
  53748. if (reset_view) { x0 = 0; x1 = width()*height()*depth() - 1; y0 = ymin; y1 = ymax; reset_view = false; }
  53749. CImg<T> zoom(x1 - x0 + 1,1,1,spectrum());
  53750. cimg_forC(*this,c) zoom.get_shared_channel(c) = CImg<T>(data(x0,0,0,c),x1 - x0 + 1,1,1,1,true);
  53751. if (y0==y1) { y0 = zoom.min_max(y1); const double dy = y1 - y0; y0-=dy/20; y1+=dy/20; }
  53752. if (y0==y1) { --y0; ++y1; }
  53753. const CImg<intT> selection = zoom.get_select_graph(disp,plot_type,vertex_type,
  53754. labelx,
  53755. nxmin + x0*(nxmax - nxmin)/siz1,
  53756. nxmin + x1*(nxmax - nxmin)/siz1,
  53757. labely,y0,y1,true);
  53758. const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y();
  53759. if (selection[0]>=0) {
  53760. if (selection[2]<0) reset_view = true;
  53761. else {
  53762. x1 = x0 + selection[2]; x0+=selection[0];
  53763. if (selection[1]>=0 && selection[3]>=0) {
  53764. y0 = y1 - selection[3]*(y1 - y0)/(disp.height() - 32);
  53765. y1-=selection[1]*(y1 - y0)/(disp.height() - 32);
  53766. }
  53767. }
  53768. } else {
  53769. bool go_in = false, go_out = false, go_left = false, go_right = false, go_up = false, go_down = false;
  53770. switch (key = (int)disp.key()) {
  53771. case cimg::keyHOME : reset_view = true; key = 0; disp.set_key(); break;
  53772. case cimg::keyPADADD : go_in = true; go_out = false; key = 0; disp.set_key(); break;
  53773. case cimg::keyPADSUB : go_out = true; go_in = false; key = 0; disp.set_key(); break;
  53774. case cimg::keyARROWLEFT : case cimg::keyPAD4 : go_left = true; go_right = false; key = 0; disp.set_key();
  53775. break;
  53776. case cimg::keyARROWRIGHT : case cimg::keyPAD6 : go_right = true; go_left = false; key = 0; disp.set_key();
  53777. break;
  53778. case cimg::keyARROWUP : case cimg::keyPAD8 : go_up = true; go_down = false; key = 0; disp.set_key(); break;
  53779. case cimg::keyARROWDOWN : case cimg::keyPAD2 : go_down = true; go_up = false; key = 0; disp.set_key(); break;
  53780. case cimg::keyPAD7 : go_left = true; go_up = true; key = 0; disp.set_key(); break;
  53781. case cimg::keyPAD9 : go_right = true; go_up = true; key = 0; disp.set_key(); break;
  53782. case cimg::keyPAD1 : go_left = true; go_down = true; key = 0; disp.set_key(); break;
  53783. case cimg::keyPAD3 : go_right = true; go_down = true; key = 0; disp.set_key(); break;
  53784. }
  53785. if (disp.wheel()) {
  53786. if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) go_up = !(go_down = disp.wheel()<0);
  53787. else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) go_left = !(go_right = disp.wheel()>0);
  53788. else go_out = !(go_in = disp.wheel()>0);
  53789. key = 0;
  53790. }
  53791. if (go_in) {
  53792. const int
  53793. xsiz = x1 - x0,
  53794. mx = (mouse_x - 16)*xsiz/(disp.width() - 32),
  53795. cx = x0 + cimg::cut(mx,0,xsiz);
  53796. if (x1 - x0>4) {
  53797. x0 = cx - 7*(cx - x0)/8; x1 = cx + 7*(x1 - cx)/8;
  53798. if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  53799. const double
  53800. ysiz = y1 - y0,
  53801. my = (mouse_y - 16)*ysiz/(disp.height() - 32),
  53802. cy = y1 - cimg::cut(my,0.,ysiz);
  53803. y0 = cy - 7*(cy - y0)/8; y1 = cy + 7*(y1 - cy)/8;
  53804. } else y0 = y1 = 0;
  53805. }
  53806. }
  53807. if (go_out) {
  53808. if (x0>0 || x1<(int)siz1) {
  53809. const int delta_x = (x1 - x0)/8, ndelta_x = delta_x?delta_x:(siz>1);
  53810. const double ndelta_y = (y1 - y0)/8;
  53811. x0-=ndelta_x; x1+=ndelta_x;
  53812. y0-=ndelta_y; y1+=ndelta_y;
  53813. if (x0<0) { x1-=x0; x0 = 0; if (x1>=(int)siz) x1 = (int)siz1; }
  53814. if (x1>=(int)siz) { x0-=(x1 - siz1); x1 = (int)siz1; if (x0<0) x0 = 0; }
  53815. }
  53816. }
  53817. if (go_left) {
  53818. const int delta = (x1 - x0)/5, ndelta = delta?delta:1;
  53819. if (x0 - ndelta>=0) { x0-=ndelta; x1-=ndelta; }
  53820. else { x1-=x0; x0 = 0; }
  53821. go_left = false;
  53822. }
  53823. if (go_right) {
  53824. const int delta = (x1 - x0)/5, ndelta = delta?delta:1;
  53825. if (x1 + ndelta<(int)siz) { x0+=ndelta; x1+=ndelta; }
  53826. else { x0+=(siz1 - x1); x1 = (int)siz1; }
  53827. go_right = false;
  53828. }
  53829. if (go_up) {
  53830. const double delta = (y1 - y0)/10, ndelta = delta?delta:1;
  53831. y0+=ndelta; y1+=ndelta;
  53832. go_up = false;
  53833. }
  53834. if (go_down) {
  53835. const double delta = (y1 - y0)/10, ndelta = delta?delta:1;
  53836. y0-=ndelta; y1-=ndelta;
  53837. go_down = false;
  53838. }
  53839. }
  53840. if (!exit_on_anykey && key && key!=(int)cimg::keyESC &&
  53841. (key!=(int)cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  53842. disp.set_key(key,false);
  53843. key = 0;
  53844. }
  53845. }
  53846. disp._normalization = old_normalization;
  53847. return *this;
  53848. }
  53849. //! Save image as a file.
  53850. /**
  53851. \param filename Filename, as a C-string.
  53852. \param number When positive, represents an index added to the filename. Otherwise, no number is added.
  53853. \param digits Number of digits used for adding the number to the filename.
  53854. \note
  53855. - The used file format is defined by the file extension in the filename \p filename.
  53856. - Parameter \p number can be used to add a 6-digit number to the filename before saving.
  53857. **/
  53858. const CImg<T>& save(const char *const filename, const int number=-1, const unsigned int digits=6) const {
  53859. if (!filename)
  53860. throw CImgArgumentException(_cimg_instance
  53861. "save(): Specified filename is (null).",
  53862. cimg_instance);
  53863. // Do not test for empty instances, since .cimg format is able to manage empty instances.
  53864. const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.');
  53865. const char *const ext = cimg::split_filename(filename);
  53866. CImg<charT> nfilename(1024);
  53867. const char *const fn = is_stdout?filename:(number>=0)?cimg::number_filename(filename,number,digits,nfilename):
  53868. filename;
  53869. #ifdef cimg_save_plugin
  53870. cimg_save_plugin(fn);
  53871. #endif
  53872. #ifdef cimg_save_plugin1
  53873. cimg_save_plugin1(fn);
  53874. #endif
  53875. #ifdef cimg_save_plugin2
  53876. cimg_save_plugin2(fn);
  53877. #endif
  53878. #ifdef cimg_save_plugin3
  53879. cimg_save_plugin3(fn);
  53880. #endif
  53881. #ifdef cimg_save_plugin4
  53882. cimg_save_plugin4(fn);
  53883. #endif
  53884. #ifdef cimg_save_plugin5
  53885. cimg_save_plugin5(fn);
  53886. #endif
  53887. #ifdef cimg_save_plugin6
  53888. cimg_save_plugin6(fn);
  53889. #endif
  53890. #ifdef cimg_save_plugin7
  53891. cimg_save_plugin7(fn);
  53892. #endif
  53893. #ifdef cimg_save_plugin8
  53894. cimg_save_plugin8(fn);
  53895. #endif
  53896. // Text formats
  53897. if (!cimg::strcasecmp(ext,"asc")) return save_ascii(fn);
  53898. else if (!cimg::strcasecmp(ext,"csv") ||
  53899. !cimg::strcasecmp(ext,"dlm") ||
  53900. !cimg::strcasecmp(ext,"txt")) return save_dlm(fn);
  53901. else if (!cimg::strcasecmp(ext,"cpp") ||
  53902. !cimg::strcasecmp(ext,"hpp") ||
  53903. !cimg::strcasecmp(ext,"h") ||
  53904. !cimg::strcasecmp(ext,"c")) return save_cpp(fn);
  53905. // 2D binary formats
  53906. else if (!cimg::strcasecmp(ext,"bmp")) return save_bmp(fn);
  53907. else if (!cimg::strcasecmp(ext,"jpg") ||
  53908. !cimg::strcasecmp(ext,"jpeg") ||
  53909. !cimg::strcasecmp(ext,"jpe") ||
  53910. !cimg::strcasecmp(ext,"jfif") ||
  53911. !cimg::strcasecmp(ext,"jif")) return save_jpeg(fn);
  53912. else if (!cimg::strcasecmp(ext,"rgb")) return save_rgb(fn);
  53913. else if (!cimg::strcasecmp(ext,"rgba")) return save_rgba(fn);
  53914. else if (!cimg::strcasecmp(ext,"png")) return save_png(fn);
  53915. else if (!cimg::strcasecmp(ext,"pgm") ||
  53916. !cimg::strcasecmp(ext,"ppm") ||
  53917. !cimg::strcasecmp(ext,"pnm")) return save_pnm(fn);
  53918. else if (!cimg::strcasecmp(ext,"pnk")) return save_pnk(fn);
  53919. else if (!cimg::strcasecmp(ext,"pfm")) return save_pfm(fn);
  53920. else if (!cimg::strcasecmp(ext,"exr")) return save_exr(fn);
  53921. else if (!cimg::strcasecmp(ext,"tif") ||
  53922. !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn);
  53923. // 3D binary formats
  53924. else if (!*ext) {
  53925. #ifdef cimg_use_zlib
  53926. return save_cimg(fn,true);
  53927. #else
  53928. return save_cimg(fn,false);
  53929. #endif
  53930. } else if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true);
  53931. else if (!cimg::strcasecmp(ext,"cimg")) return save_cimg(fn,false);
  53932. else if (!cimg::strcasecmp(ext,"dcm")) return save_medcon_external(fn);
  53933. else if (!cimg::strcasecmp(ext,"hdr") ||
  53934. !cimg::strcasecmp(ext,"nii")) return save_analyze(fn);
  53935. else if (!cimg::strcasecmp(ext,"inr")) return save_inr(fn);
  53936. else if (!cimg::strcasecmp(ext,"mnc")) return save_minc2(fn);
  53937. else if (!cimg::strcasecmp(ext,"pan")) return save_pandore(fn);
  53938. else if (!cimg::strcasecmp(ext,"raw")) return save_raw(fn);
  53939. // Archive files
  53940. else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn);
  53941. // Image sequences
  53942. else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,444,true);
  53943. else if (!cimg::strcasecmp(ext,"avi") ||
  53944. !cimg::strcasecmp(ext,"mov") ||
  53945. !cimg::strcasecmp(ext,"asf") ||
  53946. !cimg::strcasecmp(ext,"divx") ||
  53947. !cimg::strcasecmp(ext,"flv") ||
  53948. !cimg::strcasecmp(ext,"mpg") ||
  53949. !cimg::strcasecmp(ext,"m1v") ||
  53950. !cimg::strcasecmp(ext,"m2v") ||
  53951. !cimg::strcasecmp(ext,"m4v") ||
  53952. !cimg::strcasecmp(ext,"mjp") ||
  53953. !cimg::strcasecmp(ext,"mp4") ||
  53954. !cimg::strcasecmp(ext,"mkv") ||
  53955. !cimg::strcasecmp(ext,"mpe") ||
  53956. !cimg::strcasecmp(ext,"movie") ||
  53957. !cimg::strcasecmp(ext,"ogm") ||
  53958. !cimg::strcasecmp(ext,"ogg") ||
  53959. !cimg::strcasecmp(ext,"ogv") ||
  53960. !cimg::strcasecmp(ext,"qt") ||
  53961. !cimg::strcasecmp(ext,"rm") ||
  53962. !cimg::strcasecmp(ext,"vob") ||
  53963. !cimg::strcasecmp(ext,"webm") ||
  53964. !cimg::strcasecmp(ext,"wmv") ||
  53965. !cimg::strcasecmp(ext,"xvid") ||
  53966. !cimg::strcasecmp(ext,"mpeg")) return save_video(fn);
  53967. return save_other(fn);
  53968. }
  53969. //! Save image as an ascii file.
  53970. /**
  53971. \param filename Filename, as a C-string.
  53972. **/
  53973. const CImg<T>& save_ascii(const char *const filename) const {
  53974. return _save_ascii(0,filename);
  53975. }
  53976. //! Save image as an Ascii file \overloading.
  53977. const CImg<T>& save_ascii(std::FILE *const file) const {
  53978. return _save_ascii(file,0);
  53979. }
  53980. const CImg<T>& _save_ascii(std::FILE *const file, const char *const filename) const {
  53981. if (!file && !filename)
  53982. throw CImgArgumentException(_cimg_instance
  53983. "save_ascii(): Specified filename is (null).",
  53984. cimg_instance);
  53985. std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
  53986. std::fprintf(nfile,"%u %u %u %u\n",_width,_height,_depth,_spectrum);
  53987. const T* ptrs = _data;
  53988. cimg_forYZC(*this,y,z,c) {
  53989. cimg_forX(*this,x) std::fprintf(nfile,"%.17g ",(double)*(ptrs++));
  53990. std::fputc('\n',nfile);
  53991. }
  53992. if (!file) cimg::fclose(nfile);
  53993. return *this;
  53994. }
  53995. //! Save image as a .cpp source file.
  53996. /**
  53997. \param filename Filename, as a C-string.
  53998. **/
  53999. const CImg<T>& save_cpp(const char *const filename) const {
  54000. return _save_cpp(0,filename);
  54001. }
  54002. //! Save image as a .cpp source file \overloading.
  54003. const CImg<T>& save_cpp(std::FILE *const file) const {
  54004. return _save_cpp(file,0);
  54005. }
  54006. const CImg<T>& _save_cpp(std::FILE *const file, const char *const filename) const {
  54007. if (!file && !filename)
  54008. throw CImgArgumentException(_cimg_instance
  54009. "save_cpp(): Specified filename is (null).",
  54010. cimg_instance);
  54011. std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
  54012. CImg<charT> varname(1024); *varname = 0;
  54013. if (filename) cimg_sscanf(cimg::basename(filename),"%1023[a-zA-Z0-9_]",varname._data);
  54014. if (!*varname) cimg_snprintf(varname,varname._width,"unnamed");
  54015. std::fprintf(nfile,
  54016. "/* Define image '%s' of size %ux%ux%ux%u and type '%s' */\n"
  54017. "%s data_%s[] = { %s\n ",
  54018. varname._data,_width,_height,_depth,_spectrum,pixel_type(),pixel_type(),varname._data,
  54019. is_empty()?"};":"");
  54020. if (!is_empty()) for (ulongT off = 0, siz = size() - 1; off<=siz; ++off) {
  54021. std::fprintf(nfile,cimg::type<T>::format(),cimg::type<T>::format((*this)[off]));
  54022. if (off==siz) std::fprintf(nfile," };\n");
  54023. else if (!((off + 1)%16)) std::fprintf(nfile,",\n ");
  54024. else std::fprintf(nfile,", ");
  54025. }
  54026. if (!file) cimg::fclose(nfile);
  54027. return *this;
  54028. }
  54029. //! Save image as a DLM file.
  54030. /**
  54031. \param filename Filename, as a C-string.
  54032. **/
  54033. const CImg<T>& save_dlm(const char *const filename) const {
  54034. return _save_dlm(0,filename);
  54035. }
  54036. //! Save image as a DLM file \overloading.
  54037. const CImg<T>& save_dlm(std::FILE *const file) const {
  54038. return _save_dlm(file,0);
  54039. }
  54040. const CImg<T>& _save_dlm(std::FILE *const file, const char *const filename) const {
  54041. if (!file && !filename)
  54042. throw CImgArgumentException(_cimg_instance
  54043. "save_dlm(): Specified filename is (null).",
  54044. cimg_instance);
  54045. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54046. if (_depth>1)
  54047. cimg::warn(_cimg_instance
  54048. "save_dlm(): Instance is volumetric, values along Z will be unrolled in file '%s'.",
  54049. cimg_instance,
  54050. filename?filename:"(FILE*)");
  54051. if (_spectrum>1)
  54052. cimg::warn(_cimg_instance
  54053. "save_dlm(): Instance is multispectral, values along C will be unrolled in file '%s'.",
  54054. cimg_instance,
  54055. filename?filename:"(FILE*)");
  54056. std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
  54057. const T* ptrs = _data;
  54058. cimg_forYZC(*this,y,z,c) {
  54059. cimg_forX(*this,x) std::fprintf(nfile,"%.17g%s",(double)*(ptrs++),(x==width() - 1)?"":",");
  54060. std::fputc('\n',nfile);
  54061. }
  54062. if (!file) cimg::fclose(nfile);
  54063. return *this;
  54064. }
  54065. //! Save image as a BMP file.
  54066. /**
  54067. \param filename Filename, as a C-string.
  54068. **/
  54069. const CImg<T>& save_bmp(const char *const filename) const {
  54070. return _save_bmp(0,filename);
  54071. }
  54072. //! Save image as a BMP file \overloading.
  54073. const CImg<T>& save_bmp(std::FILE *const file) const {
  54074. return _save_bmp(file,0);
  54075. }
  54076. const CImg<T>& _save_bmp(std::FILE *const file, const char *const filename) const {
  54077. if (!file && !filename)
  54078. throw CImgArgumentException(_cimg_instance
  54079. "save_bmp(): Specified filename is (null).",
  54080. cimg_instance);
  54081. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54082. if (_depth>1)
  54083. cimg::warn(_cimg_instance
  54084. "save_bmp(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54085. cimg_instance,
  54086. filename?filename:"(FILE*)");
  54087. if (_spectrum>3)
  54088. cimg::warn(_cimg_instance
  54089. "save_bmp(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
  54090. cimg_instance,
  54091. filename?filename:"(FILE*)");
  54092. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54093. CImg<ucharT> header(54,1,1,1,0);
  54094. unsigned char align_buf[4] = { 0 };
  54095. const unsigned int
  54096. align = (4 - (3*_width)%4)%4,
  54097. buf_size = (3*_width + align)*height(),
  54098. file_size = 54 + buf_size;
  54099. header[0] = 'B'; header[1] = 'M';
  54100. header[0x02] = file_size&0xFF;
  54101. header[0x03] = (file_size>>8)&0xFF;
  54102. header[0x04] = (file_size>>16)&0xFF;
  54103. header[0x05] = (file_size>>24)&0xFF;
  54104. header[0x0A] = 0x36;
  54105. header[0x0E] = 0x28;
  54106. header[0x12] = _width&0xFF;
  54107. header[0x13] = (_width>>8)&0xFF;
  54108. header[0x14] = (_width>>16)&0xFF;
  54109. header[0x15] = (_width>>24)&0xFF;
  54110. header[0x16] = _height&0xFF;
  54111. header[0x17] = (_height>>8)&0xFF;
  54112. header[0x18] = (_height>>16)&0xFF;
  54113. header[0x19] = (_height>>24)&0xFF;
  54114. header[0x1A] = 1;
  54115. header[0x1B] = 0;
  54116. header[0x1C] = 24;
  54117. header[0x1D] = 0;
  54118. header[0x22] = buf_size&0xFF;
  54119. header[0x23] = (buf_size>>8)&0xFF;
  54120. header[0x24] = (buf_size>>16)&0xFF;
  54121. header[0x25] = (buf_size>>24)&0xFF;
  54122. header[0x27] = 0x1;
  54123. header[0x2B] = 0x1;
  54124. cimg::fwrite(header._data,54,nfile);
  54125. const T
  54126. *ptr_r = data(0,_height - 1,0,0),
  54127. *ptr_g = (_spectrum>=2)?data(0,_height - 1,0,1):0,
  54128. *ptr_b = (_spectrum>=3)?data(0,_height - 1,0,2):0;
  54129. switch (_spectrum) {
  54130. case 1 : {
  54131. cimg_forY(*this,y) {
  54132. cimg_forX(*this,x) {
  54133. const unsigned char val = (unsigned char)*(ptr_r++);
  54134. std::fputc(val,nfile); std::fputc(val,nfile); std::fputc(val,nfile);
  54135. }
  54136. cimg::fwrite(align_buf,align,nfile);
  54137. ptr_r-=2*_width;
  54138. }
  54139. } break;
  54140. case 2 : {
  54141. cimg_forY(*this,y) {
  54142. cimg_forX(*this,x) {
  54143. std::fputc(0,nfile);
  54144. std::fputc((unsigned char)(*(ptr_g++)),nfile);
  54145. std::fputc((unsigned char)(*(ptr_r++)),nfile);
  54146. }
  54147. cimg::fwrite(align_buf,align,nfile);
  54148. ptr_r-=2*_width; ptr_g-=2*_width;
  54149. }
  54150. } break;
  54151. default : {
  54152. cimg_forY(*this,y) {
  54153. cimg_forX(*this,x) {
  54154. std::fputc((unsigned char)(*(ptr_b++)),nfile);
  54155. std::fputc((unsigned char)(*(ptr_g++)),nfile);
  54156. std::fputc((unsigned char)(*(ptr_r++)),nfile);
  54157. }
  54158. cimg::fwrite(align_buf,align,nfile);
  54159. ptr_r-=2*_width; ptr_g-=2*_width; ptr_b-=2*_width;
  54160. }
  54161. }
  54162. }
  54163. if (!file) cimg::fclose(nfile);
  54164. return *this;
  54165. }
  54166. //! Save image as a JPEG file.
  54167. /**
  54168. \param filename Filename, as a C-string.
  54169. \param quality Image quality (in %)
  54170. **/
  54171. const CImg<T>& save_jpeg(const char *const filename, const unsigned int quality=100) const {
  54172. return _save_jpeg(0,filename,quality);
  54173. }
  54174. //! Save image as a JPEG file \overloading.
  54175. const CImg<T>& save_jpeg(std::FILE *const file, const unsigned int quality=100) const {
  54176. return _save_jpeg(file,0,quality);
  54177. }
  54178. const CImg<T>& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const {
  54179. if (!file && !filename)
  54180. throw CImgArgumentException(_cimg_instance
  54181. "save_jpeg(): Specified filename is (null).",
  54182. cimg_instance);
  54183. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54184. if (_depth>1)
  54185. cimg::warn(_cimg_instance
  54186. "save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54187. cimg_instance,
  54188. filename?filename:"(FILE*)");
  54189. #ifndef cimg_use_jpeg
  54190. if (!file) return save_other(filename,quality);
  54191. else throw CImgIOException(_cimg_instance
  54192. "save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.",
  54193. cimg_instance);
  54194. #else
  54195. unsigned int dimbuf = 0;
  54196. J_COLOR_SPACE colortype = JCS_RGB;
  54197. switch (_spectrum) {
  54198. case 1 : dimbuf = 1; colortype = JCS_GRAYSCALE; break;
  54199. case 2 : dimbuf = 3; colortype = JCS_RGB; break;
  54200. case 3 : dimbuf = 3; colortype = JCS_RGB; break;
  54201. default : dimbuf = 4; colortype = JCS_CMYK; break;
  54202. }
  54203. // Call libjpeg functions
  54204. struct jpeg_compress_struct cinfo;
  54205. struct jpeg_error_mgr jerr;
  54206. cinfo.err = jpeg_std_error(&jerr);
  54207. jpeg_create_compress(&cinfo);
  54208. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54209. jpeg_stdio_dest(&cinfo,nfile);
  54210. cinfo.image_width = _width;
  54211. cinfo.image_height = _height;
  54212. cinfo.input_components = dimbuf;
  54213. cinfo.in_color_space = colortype;
  54214. jpeg_set_defaults(&cinfo);
  54215. jpeg_set_quality(&cinfo,quality<100?quality:100,TRUE);
  54216. jpeg_start_compress(&cinfo,TRUE);
  54217. JSAMPROW row_pointer[1];
  54218. CImg<ucharT> buffer(_width*dimbuf);
  54219. while (cinfo.next_scanline<cinfo.image_height) {
  54220. unsigned char *ptrd = buffer._data;
  54221. // Fill pixel buffer
  54222. switch (_spectrum) {
  54223. case 1 : { // Greyscale images
  54224. const T *ptr_g = data(0, cinfo.next_scanline);
  54225. for (unsigned int b = 0; b<cinfo.image_width; b++)
  54226. *(ptrd++) = (unsigned char)*(ptr_g++);
  54227. } break;
  54228. case 2 : { // RG images
  54229. const T *ptr_r = data(0,cinfo.next_scanline,0,0),
  54230. *ptr_g = data(0,cinfo.next_scanline,0,1);
  54231. for (unsigned int b = 0; b<cinfo.image_width; ++b) {
  54232. *(ptrd++) = (unsigned char)*(ptr_r++);
  54233. *(ptrd++) = (unsigned char)*(ptr_g++);
  54234. *(ptrd++) = 0;
  54235. }
  54236. } break;
  54237. case 3 : { // RGB images
  54238. const T *ptr_r = data(0,cinfo.next_scanline,0,0),
  54239. *ptr_g = data(0,cinfo.next_scanline,0,1),
  54240. *ptr_b = data(0,cinfo.next_scanline,0,2);
  54241. for (unsigned int b = 0; b<cinfo.image_width; ++b) {
  54242. *(ptrd++) = (unsigned char)*(ptr_r++);
  54243. *(ptrd++) = (unsigned char)*(ptr_g++);
  54244. *(ptrd++) = (unsigned char)*(ptr_b++);
  54245. }
  54246. } break;
  54247. default : { // CMYK images
  54248. const T *ptr_r = data(0,cinfo.next_scanline,0,0),
  54249. *ptr_g = data(0,cinfo.next_scanline,0,1),
  54250. *ptr_b = data(0,cinfo.next_scanline,0,2),
  54251. *ptr_a = data(0,cinfo.next_scanline,0,3);
  54252. for (unsigned int b = 0; b<cinfo.image_width; ++b) {
  54253. *(ptrd++) = (unsigned char)*(ptr_r++);
  54254. *(ptrd++) = (unsigned char)*(ptr_g++);
  54255. *(ptrd++) = (unsigned char)*(ptr_b++);
  54256. *(ptrd++) = (unsigned char)*(ptr_a++);
  54257. }
  54258. }
  54259. }
  54260. *row_pointer = buffer._data;
  54261. jpeg_write_scanlines(&cinfo,row_pointer,1);
  54262. }
  54263. jpeg_finish_compress(&cinfo);
  54264. if (!file) cimg::fclose(nfile);
  54265. jpeg_destroy_compress(&cinfo);
  54266. return *this;
  54267. #endif
  54268. }
  54269. //! Save image, using built-in ImageMagick++ library.
  54270. /**
  54271. \param filename Filename, as a C-string.
  54272. \param bytes_per_pixel Force the number of bytes per pixel for the saving, when possible.
  54273. **/
  54274. const CImg<T>& save_magick(const char *const filename, const unsigned int bytes_per_pixel=0) const {
  54275. if (!filename)
  54276. throw CImgArgumentException(_cimg_instance
  54277. "save_magick(): Specified filename is (null).",
  54278. cimg_instance);
  54279. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  54280. #ifdef cimg_use_magick
  54281. double stmin, stmax = (double)max_min(stmin);
  54282. if (_depth>1)
  54283. cimg::warn(_cimg_instance
  54284. "save_magick(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54285. cimg_instance,
  54286. filename);
  54287. if (_spectrum>3)
  54288. cimg::warn(_cimg_instance
  54289. "save_magick(): Instance is multispectral, only the three first channels will be "
  54290. "saved in file '%s'.",
  54291. cimg_instance,
  54292. filename);
  54293. if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
  54294. cimg::warn(_cimg_instance
  54295. "save_magick(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
  54296. cimg_instance,
  54297. stmin,stmax,filename);
  54298. Magick::Image image(Magick::Geometry(_width,_height),"black");
  54299. image.type(Magick::TrueColorType);
  54300. image.depth(bytes_per_pixel?(8*bytes_per_pixel):(stmax>=256?16:8));
  54301. const T
  54302. *ptr_r = data(0,0,0,0),
  54303. *ptr_g = _spectrum>1?data(0,0,0,1):0,
  54304. *ptr_b = _spectrum>2?data(0,0,0,2):0;
  54305. Magick::PixelPacket *pixels = image.getPixels(0,0,_width,_height);
  54306. switch (_spectrum) {
  54307. case 1 : // Scalar images
  54308. for (ulongT off = (ulongT)_width*_height; off; --off) {
  54309. pixels->red = pixels->green = pixels->blue = (Magick::Quantum)*(ptr_r++);
  54310. ++pixels;
  54311. }
  54312. break;
  54313. case 2 : // RG images
  54314. for (ulongT off = (ulongT)_width*_height; off; --off) {
  54315. pixels->red = (Magick::Quantum)*(ptr_r++);
  54316. pixels->green = (Magick::Quantum)*(ptr_g++);
  54317. pixels->blue = 0; ++pixels;
  54318. }
  54319. break;
  54320. default : // RGB images
  54321. for (ulongT off = (ulongT)_width*_height; off; --off) {
  54322. pixels->red = (Magick::Quantum)*(ptr_r++);
  54323. pixels->green = (Magick::Quantum)*(ptr_g++);
  54324. pixels->blue = (Magick::Quantum)*(ptr_b++);
  54325. ++pixels;
  54326. }
  54327. }
  54328. image.syncPixels();
  54329. image.write(filename);
  54330. return *this;
  54331. #else
  54332. cimg::unused(bytes_per_pixel);
  54333. throw CImgIOException(_cimg_instance
  54334. "save_magick(): Unable to save file '%s' unless libMagick++ is enabled.",
  54335. cimg_instance,
  54336. filename);
  54337. #endif
  54338. }
  54339. //! Save image as a PNG file.
  54340. /**
  54341. \param filename Filename, as a C-string.
  54342. \param bytes_per_pixel Force the number of bytes per pixels for the saving, when possible.
  54343. **/
  54344. const CImg<T>& save_png(const char *const filename, const unsigned int bytes_per_pixel=0) const {
  54345. return _save_png(0,filename,bytes_per_pixel);
  54346. }
  54347. //! Save image as a PNG file \overloading.
  54348. const CImg<T>& save_png(std::FILE *const file, const unsigned int bytes_per_pixel=0) const {
  54349. return _save_png(file,0,bytes_per_pixel);
  54350. }
  54351. const CImg<T>& _save_png(std::FILE *const file, const char *const filename,
  54352. const unsigned int bytes_per_pixel=0) const {
  54353. if (!file && !filename)
  54354. throw CImgArgumentException(_cimg_instance
  54355. "save_png(): Specified filename is (null).",
  54356. cimg_instance);
  54357. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54358. #ifndef cimg_use_png
  54359. cimg::unused(bytes_per_pixel);
  54360. if (!file) return save_other(filename);
  54361. else throw CImgIOException(_cimg_instance
  54362. "save_png(): Unable to save data in '(*FILE)' unless libpng is enabled.",
  54363. cimg_instance);
  54364. #else
  54365. #if defined __GNUC__
  54366. const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning
  54367. std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"wb");
  54368. volatile double stmin, stmax = (double)max_min(stmin);
  54369. #else
  54370. const char *nfilename = filename;
  54371. std::FILE *nfile = file?file:cimg::fopen(nfilename,"wb");
  54372. double stmin, stmax = (double)max_min(stmin);
  54373. #endif
  54374. if (_depth>1)
  54375. cimg::warn(_cimg_instance
  54376. "save_png(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54377. cimg_instance,
  54378. filename);
  54379. if (_spectrum>4)
  54380. cimg::warn(_cimg_instance
  54381. "save_png(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
  54382. cimg_instance,
  54383. filename);
  54384. if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
  54385. cimg::warn(_cimg_instance
  54386. "save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
  54387. cimg_instance,
  54388. stmin,stmax,filename);
  54389. // Setup PNG structures for write
  54390. png_voidp user_error_ptr = 0;
  54391. png_error_ptr user_error_fn = 0, user_warning_fn = 0;
  54392. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,user_error_ptr, user_error_fn,
  54393. user_warning_fn);
  54394. if (!png_ptr){
  54395. if (!file) cimg::fclose(nfile);
  54396. throw CImgIOException(_cimg_instance
  54397. "save_png(): Failed to initialize 'png_ptr' structure when saving file '%s'.",
  54398. cimg_instance,
  54399. nfilename?nfilename:"(FILE*)");
  54400. }
  54401. png_infop info_ptr = png_create_info_struct(png_ptr);
  54402. if (!info_ptr) {
  54403. png_destroy_write_struct(&png_ptr,(png_infopp)0);
  54404. if (!file) cimg::fclose(nfile);
  54405. throw CImgIOException(_cimg_instance
  54406. "save_png(): Failed to initialize 'info_ptr' structure when saving file '%s'.",
  54407. cimg_instance,
  54408. nfilename?nfilename:"(FILE*)");
  54409. }
  54410. if (setjmp(png_jmpbuf(png_ptr))) {
  54411. png_destroy_write_struct(&png_ptr, &info_ptr);
  54412. if (!file) cimg::fclose(nfile);
  54413. throw CImgIOException(_cimg_instance
  54414. "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.",
  54415. cimg_instance,
  54416. nfilename?nfilename:"(FILE*)");
  54417. }
  54418. png_init_io(png_ptr, nfile);
  54419. const int bit_depth = bytes_per_pixel?(bytes_per_pixel*8):(stmax>=256?16:8);
  54420. int color_type;
  54421. switch (spectrum()) {
  54422. case 1 : color_type = PNG_COLOR_TYPE_GRAY; break;
  54423. case 2 : color_type = PNG_COLOR_TYPE_GRAY_ALPHA; break;
  54424. case 3 : color_type = PNG_COLOR_TYPE_RGB; break;
  54425. default : color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  54426. }
  54427. const int interlace_type = PNG_INTERLACE_NONE;
  54428. const int compression_type = PNG_COMPRESSION_TYPE_DEFAULT;
  54429. const int filter_method = PNG_FILTER_TYPE_DEFAULT;
  54430. png_set_IHDR(png_ptr,info_ptr,_width,_height,bit_depth,color_type,interlace_type,compression_type,filter_method);
  54431. png_write_info(png_ptr,info_ptr);
  54432. const int byte_depth = bit_depth>>3;
  54433. const int numChan = spectrum()>4?4:spectrum();
  54434. const int pixel_bit_depth_flag = numChan * (bit_depth - 1);
  54435. // Allocate Memory for Image Save and Fill pixel data
  54436. png_bytep *const imgData = new png_byte*[_height];
  54437. for (unsigned int row = 0; row<_height; ++row) imgData[row] = new png_byte[byte_depth*numChan*_width];
  54438. const T *pC0 = data(0,0,0,0);
  54439. switch (pixel_bit_depth_flag) {
  54440. case 7 : { // Gray 8-bit
  54441. cimg_forY(*this,y) {
  54442. unsigned char *ptrd = imgData[y];
  54443. cimg_forX(*this,x) *(ptrd++) = (unsigned char)*(pC0++);
  54444. }
  54445. } break;
  54446. case 14 : { // Gray w/ Alpha 8-bit
  54447. const T *pC1 = data(0,0,0,1);
  54448. cimg_forY(*this,y) {
  54449. unsigned char *ptrd = imgData[y];
  54450. cimg_forX(*this,x) {
  54451. *(ptrd++) = (unsigned char)*(pC0++);
  54452. *(ptrd++) = (unsigned char)*(pC1++);
  54453. }
  54454. }
  54455. } break;
  54456. case 21 : { // RGB 8-bit
  54457. const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2);
  54458. cimg_forY(*this,y) {
  54459. unsigned char *ptrd = imgData[y];
  54460. cimg_forX(*this,x) {
  54461. *(ptrd++) = (unsigned char)*(pC0++);
  54462. *(ptrd++) = (unsigned char)*(pC1++);
  54463. *(ptrd++) = (unsigned char)*(pC2++);
  54464. }
  54465. }
  54466. } break;
  54467. case 28 : { // RGB x/ Alpha 8-bit
  54468. const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3);
  54469. cimg_forY(*this,y){
  54470. unsigned char *ptrd = imgData[y];
  54471. cimg_forX(*this,x){
  54472. *(ptrd++) = (unsigned char)*(pC0++);
  54473. *(ptrd++) = (unsigned char)*(pC1++);
  54474. *(ptrd++) = (unsigned char)*(pC2++);
  54475. *(ptrd++) = (unsigned char)*(pC3++);
  54476. }
  54477. }
  54478. } break;
  54479. case 15 : { // Gray 16-bit
  54480. cimg_forY(*this,y){
  54481. unsigned short *ptrd = (unsigned short*)(imgData[y]);
  54482. cimg_forX(*this,x) *(ptrd++) = (unsigned short)*(pC0++);
  54483. if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],_width);
  54484. }
  54485. } break;
  54486. case 30 : { // Gray w/ Alpha 16-bit
  54487. const T *pC1 = data(0,0,0,1);
  54488. cimg_forY(*this,y){
  54489. unsigned short *ptrd = (unsigned short*)(imgData[y]);
  54490. cimg_forX(*this,x) {
  54491. *(ptrd++) = (unsigned short)*(pC0++);
  54492. *(ptrd++) = (unsigned short)*(pC1++);
  54493. }
  54494. if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],2*_width);
  54495. }
  54496. } break;
  54497. case 45 : { // RGB 16-bit
  54498. const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2);
  54499. cimg_forY(*this,y) {
  54500. unsigned short *ptrd = (unsigned short*)(imgData[y]);
  54501. cimg_forX(*this,x) {
  54502. *(ptrd++) = (unsigned short)*(pC0++);
  54503. *(ptrd++) = (unsigned short)*(pC1++);
  54504. *(ptrd++) = (unsigned short)*(pC2++);
  54505. }
  54506. if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],3*_width);
  54507. }
  54508. } break;
  54509. case 60 : { // RGB w/ Alpha 16-bit
  54510. const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3);
  54511. cimg_forY(*this,y) {
  54512. unsigned short *ptrd = (unsigned short*)(imgData[y]);
  54513. cimg_forX(*this,x) {
  54514. *(ptrd++) = (unsigned short)*(pC0++);
  54515. *(ptrd++) = (unsigned short)*(pC1++);
  54516. *(ptrd++) = (unsigned short)*(pC2++);
  54517. *(ptrd++) = (unsigned short)*(pC3++);
  54518. }
  54519. if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],4*_width);
  54520. }
  54521. } break;
  54522. default :
  54523. if (!file) cimg::fclose(nfile);
  54524. throw CImgIOException(_cimg_instance
  54525. "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.",
  54526. cimg_instance,
  54527. nfilename?nfilename:"(FILE*)");
  54528. }
  54529. png_write_image(png_ptr,imgData);
  54530. png_write_end(png_ptr,info_ptr);
  54531. png_destroy_write_struct(&png_ptr, &info_ptr);
  54532. // Deallocate Image Write Memory
  54533. cimg_forY(*this,n) delete[] imgData[n];
  54534. delete[] imgData;
  54535. if (!file) cimg::fclose(nfile);
  54536. return *this;
  54537. #endif
  54538. }
  54539. //! Save image as a PNM file.
  54540. /**
  54541. \param filename Filename, as a C-string.
  54542. \param bytes_per_pixel Force the number of bytes per pixels for the saving.
  54543. **/
  54544. const CImg<T>& save_pnm(const char *const filename, const unsigned int bytes_per_pixel=0) const {
  54545. return _save_pnm(0,filename,bytes_per_pixel);
  54546. }
  54547. //! Save image as a PNM file \overloading.
  54548. const CImg<T>& save_pnm(std::FILE *const file, const unsigned int bytes_per_pixel=0) const {
  54549. return _save_pnm(file,0,bytes_per_pixel);
  54550. }
  54551. const CImg<T>& _save_pnm(std::FILE *const file, const char *const filename,
  54552. const unsigned int bytes_per_pixel=0) const {
  54553. if (!file && !filename)
  54554. throw CImgArgumentException(_cimg_instance
  54555. "save_pnm(): Specified filename is (null).",
  54556. cimg_instance);
  54557. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54558. double stmin, stmax = (double)max_min(stmin);
  54559. if (_depth>1)
  54560. cimg::warn(_cimg_instance
  54561. "save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54562. cimg_instance,
  54563. filename?filename:"(FILE*)");
  54564. if (_spectrum>3)
  54565. cimg::warn(_cimg_instance
  54566. "save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
  54567. cimg_instance,
  54568. filename?filename:"(FILE*)");
  54569. if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
  54570. cimg::warn(_cimg_instance
  54571. "save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
  54572. cimg_instance,
  54573. stmin,stmax,filename?filename:"(FILE*)");
  54574. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54575. const T
  54576. *ptr_r = data(0,0,0,0),
  54577. *ptr_g = (_spectrum>=2)?data(0,0,0,1):0,
  54578. *ptr_b = (_spectrum>=3)?data(0,0,0,2):0;
  54579. const ulongT buf_size = std::min((ulongT)(1024*1024),(ulongT)(_width*_height*(_spectrum==1?1UL:3UL)));
  54580. std::fprintf(nfile,"P%c\n%u %u\n%u\n",
  54581. (_spectrum==1?'5':'6'),_width,_height,stmax<256?255:(stmax<4096?4095:65535));
  54582. switch (_spectrum) {
  54583. case 1 : { // Scalar image
  54584. if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PGM 8 bits
  54585. CImg<ucharT> buf((unsigned int)buf_size);
  54586. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54587. const ulongT N = std::min((ulongT)to_write,buf_size);
  54588. unsigned char *ptrd = buf._data;
  54589. for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr_r++);
  54590. cimg::fwrite(buf._data,N,nfile);
  54591. to_write-=N;
  54592. }
  54593. } else { // Binary PGM 16 bits
  54594. CImg<ushortT> buf((unsigned int)buf_size);
  54595. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54596. const ulongT N = std::min((ulongT)to_write,buf_size);
  54597. unsigned short *ptrd = buf._data;
  54598. for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned short)*(ptr_r++);
  54599. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54600. cimg::fwrite(buf._data,N,nfile);
  54601. to_write-=N;
  54602. }
  54603. }
  54604. } break;
  54605. case 2 : { // RG image
  54606. if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits
  54607. CImg<ucharT> buf((unsigned int)buf_size);
  54608. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54609. const ulongT N = std::min((ulongT)to_write,buf_size/3);
  54610. unsigned char *ptrd = buf._data;
  54611. for (ulongT i = N; i>0; --i) {
  54612. *(ptrd++) = (unsigned char)*(ptr_r++);
  54613. *(ptrd++) = (unsigned char)*(ptr_g++);
  54614. *(ptrd++) = 0;
  54615. }
  54616. cimg::fwrite(buf._data,3*N,nfile);
  54617. to_write-=N;
  54618. }
  54619. } else { // Binary PPM 16 bits
  54620. CImg<ushortT> buf((unsigned int)buf_size);
  54621. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54622. const ulongT N = std::min((ulongT)to_write,buf_size/3);
  54623. unsigned short *ptrd = buf._data;
  54624. for (ulongT i = N; i>0; --i) {
  54625. *(ptrd++) = (unsigned short)*(ptr_r++);
  54626. *(ptrd++) = (unsigned short)*(ptr_g++);
  54627. *(ptrd++) = 0;
  54628. }
  54629. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54630. cimg::fwrite(buf._data,3*N,nfile);
  54631. to_write-=N;
  54632. }
  54633. }
  54634. } break;
  54635. default : { // RGB image
  54636. if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits
  54637. CImg<ucharT> buf((unsigned int)buf_size);
  54638. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54639. const ulongT N = std::min((ulongT)to_write,buf_size/3);
  54640. unsigned char *ptrd = buf._data;
  54641. for (ulongT i = N; i>0; --i) {
  54642. *(ptrd++) = (unsigned char)*(ptr_r++);
  54643. *(ptrd++) = (unsigned char)*(ptr_g++);
  54644. *(ptrd++) = (unsigned char)*(ptr_b++);
  54645. }
  54646. cimg::fwrite(buf._data,3*N,nfile);
  54647. to_write-=N;
  54648. }
  54649. } else { // Binary PPM 16 bits
  54650. CImg<ushortT> buf((unsigned int)buf_size);
  54651. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54652. const ulongT N = std::min((ulongT)to_write,buf_size/3);
  54653. unsigned short *ptrd = buf._data;
  54654. for (ulongT i = N; i>0; --i) {
  54655. *(ptrd++) = (unsigned short)*(ptr_r++);
  54656. *(ptrd++) = (unsigned short)*(ptr_g++);
  54657. *(ptrd++) = (unsigned short)*(ptr_b++);
  54658. }
  54659. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54660. cimg::fwrite(buf._data,3*N,nfile);
  54661. to_write-=N;
  54662. }
  54663. }
  54664. }
  54665. }
  54666. if (!file) cimg::fclose(nfile);
  54667. return *this;
  54668. }
  54669. //! Save image as a PNK file.
  54670. /**
  54671. \param filename Filename, as a C-string.
  54672. **/
  54673. const CImg<T>& save_pnk(const char *const filename) const {
  54674. return _save_pnk(0,filename);
  54675. }
  54676. //! Save image as a PNK file \overloading.
  54677. const CImg<T>& save_pnk(std::FILE *const file) const {
  54678. return _save_pnk(file,0);
  54679. }
  54680. const CImg<T>& _save_pnk(std::FILE *const file, const char *const filename) const {
  54681. if (!file && !filename)
  54682. throw CImgArgumentException(_cimg_instance
  54683. "save_pnk(): Specified filename is (null).",
  54684. cimg_instance);
  54685. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54686. if (_spectrum>1)
  54687. cimg::warn(_cimg_instance
  54688. "save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.",
  54689. cimg_instance,
  54690. filename?filename:"(FILE*)");
  54691. const ulongT buf_size = std::min((ulongT)1024*1024,(ulongT)_width*_height*_depth);
  54692. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54693. const T *ptr = data(0,0,0,0);
  54694. if (!cimg::type<T>::is_float() && sizeof(T)==1 && _depth<2) // Can be saved as regular PNM file
  54695. _save_pnm(file,filename,0);
  54696. else if (!cimg::type<T>::is_float() && sizeof(T)==1) { // Save as extended P5 file: Binary byte-valued 3D
  54697. std::fprintf(nfile,"P5\n%u %u %u\n255\n",_width,_height,_depth);
  54698. CImg<ucharT> buf((unsigned int)buf_size);
  54699. for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
  54700. const ulongT N = std::min((ulongT)to_write,buf_size);
  54701. unsigned char *ptrd = buf._data;
  54702. for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr++);
  54703. cimg::fwrite(buf._data,N,nfile);
  54704. to_write-=N;
  54705. }
  54706. } else if (!cimg::type<T>::is_float()) { // Save as P8: Binary int32-valued 3D
  54707. if (_depth>1) std::fprintf(nfile,"P8\n%u %u %u\n%d\n",_width,_height,_depth,(int)max());
  54708. else std::fprintf(nfile,"P8\n%u %u\n%d\n",_width,_height,(int)max());
  54709. CImg<intT> buf((unsigned int)buf_size);
  54710. for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
  54711. const ulongT N = std::min((ulongT)to_write,buf_size);
  54712. int *ptrd = buf._data;
  54713. for (ulongT i = N; i>0; --i) *(ptrd++) = (int)*(ptr++);
  54714. cimg::fwrite(buf._data,N,nfile);
  54715. to_write-=N;
  54716. }
  54717. } else { // Save as P9: Binary float-valued 3D
  54718. if (_depth>1) std::fprintf(nfile,"P9\n%u %u %u\n%g\n",_width,_height,_depth,(double)max());
  54719. else std::fprintf(nfile,"P9\n%u %u\n%g\n",_width,_height,(double)max());
  54720. CImg<floatT> buf((unsigned int)buf_size);
  54721. for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
  54722. const ulongT N = std::min((ulongT)to_write,buf_size);
  54723. float *ptrd = buf._data;
  54724. for (ulongT i = N; i>0; --i) *(ptrd++) = (float)*(ptr++);
  54725. cimg::fwrite(buf._data,N,nfile);
  54726. to_write-=N;
  54727. }
  54728. }
  54729. if (!file) cimg::fclose(nfile);
  54730. return *this;
  54731. }
  54732. //! Save image as a PFM file.
  54733. /**
  54734. \param filename Filename, as a C-string.
  54735. **/
  54736. const CImg<T>& save_pfm(const char *const filename) const {
  54737. get_mirror('y')._save_pfm(0,filename);
  54738. return *this;
  54739. }
  54740. //! Save image as a PFM file \overloading.
  54741. const CImg<T>& save_pfm(std::FILE *const file) const {
  54742. get_mirror('y')._save_pfm(file,0);
  54743. return *this;
  54744. }
  54745. const CImg<T>& _save_pfm(std::FILE *const file, const char *const filename) const {
  54746. if (!file && !filename)
  54747. throw CImgArgumentException(_cimg_instance
  54748. "save_pfm(): Specified filename is (null).",
  54749. cimg_instance);
  54750. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54751. if (_depth>1)
  54752. cimg::warn(_cimg_instance
  54753. "save_pfm(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  54754. cimg_instance,
  54755. filename?filename:"(FILE*)");
  54756. if (_spectrum>3)
  54757. cimg::warn(_cimg_instance
  54758. "save_pfm(): image instance is multispectral, only the three first channels will be saved "
  54759. "in file '%s'.",
  54760. cimg_instance,
  54761. filename?filename:"(FILE*)");
  54762. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54763. const T
  54764. *ptr_r = data(0,0,0,0),
  54765. *ptr_g = (_spectrum>=2)?data(0,0,0,1):0,
  54766. *ptr_b = (_spectrum>=3)?data(0,0,0,2):0;
  54767. const unsigned int buf_size = std::min(1024*1024U,_width*_height*(_spectrum==1?1:3));
  54768. std::fprintf(nfile,"P%c\n%u %u\n1.0\n",
  54769. (_spectrum==1?'f':'F'),_width,_height);
  54770. switch (_spectrum) {
  54771. case 1 : { // Scalar image
  54772. CImg<floatT> buf(buf_size);
  54773. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54774. const ulongT N = std::min((ulongT)to_write,(ulongT)buf_size);
  54775. float *ptrd = buf._data;
  54776. for (ulongT i = N; i>0; --i) *(ptrd++) = (float)*(ptr_r++);
  54777. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54778. cimg::fwrite(buf._data,N,nfile);
  54779. to_write-=N;
  54780. }
  54781. } break;
  54782. case 2 : { // RG image
  54783. CImg<floatT> buf(buf_size);
  54784. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54785. const unsigned int N = std::min((unsigned int)to_write,buf_size/3);
  54786. float *ptrd = buf._data;
  54787. for (ulongT i = N; i>0; --i) {
  54788. *(ptrd++) = (float)*(ptr_r++);
  54789. *(ptrd++) = (float)*(ptr_g++);
  54790. *(ptrd++) = 0;
  54791. }
  54792. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54793. cimg::fwrite(buf._data,3*N,nfile);
  54794. to_write-=N;
  54795. }
  54796. } break;
  54797. default : { // RGB image
  54798. CImg<floatT> buf(buf_size);
  54799. for (longT to_write = (longT)width()*height(); to_write>0; ) {
  54800. const unsigned int N = std::min((unsigned int)to_write,buf_size/3);
  54801. float *ptrd = buf._data;
  54802. for (ulongT i = N; i>0; --i) {
  54803. *(ptrd++) = (float)*(ptr_r++);
  54804. *(ptrd++) = (float)*(ptr_g++);
  54805. *(ptrd++) = (float)*(ptr_b++);
  54806. }
  54807. if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
  54808. cimg::fwrite(buf._data,3*N,nfile);
  54809. to_write-=N;
  54810. }
  54811. }
  54812. }
  54813. if (!file) cimg::fclose(nfile);
  54814. return *this;
  54815. }
  54816. //! Save image as a RGB file.
  54817. /**
  54818. \param filename Filename, as a C-string.
  54819. **/
  54820. const CImg<T>& save_rgb(const char *const filename) const {
  54821. return _save_rgb(0,filename);
  54822. }
  54823. //! Save image as a RGB file \overloading.
  54824. const CImg<T>& save_rgb(std::FILE *const file) const {
  54825. return _save_rgb(file,0);
  54826. }
  54827. const CImg<T>& _save_rgb(std::FILE *const file, const char *const filename) const {
  54828. if (!file && !filename)
  54829. throw CImgArgumentException(_cimg_instance
  54830. "save_rgb(): Specified filename is (null).",
  54831. cimg_instance);
  54832. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54833. if (_spectrum!=3)
  54834. cimg::warn(_cimg_instance
  54835. "save_rgb(): image instance has not exactly 3 channels, for file '%s'.",
  54836. cimg_instance,
  54837. filename?filename:"(FILE*)");
  54838. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54839. const ulongT wh = (ulongT)_width*_height;
  54840. unsigned char *const buffer = new unsigned char[3*wh], *nbuffer = buffer;
  54841. const T
  54842. *ptr1 = data(0,0,0,0),
  54843. *ptr2 = _spectrum>1?data(0,0,0,1):0,
  54844. *ptr3 = _spectrum>2?data(0,0,0,2):0;
  54845. switch (_spectrum) {
  54846. case 1 : { // Scalar image
  54847. for (ulongT k = 0; k<wh; ++k) {
  54848. const unsigned char val = (unsigned char)*(ptr1++);
  54849. *(nbuffer++) = val;
  54850. *(nbuffer++) = val;
  54851. *(nbuffer++) = val;
  54852. }
  54853. } break;
  54854. case 2 : { // RG image
  54855. for (ulongT k = 0; k<wh; ++k) {
  54856. *(nbuffer++) = (unsigned char)(*(ptr1++));
  54857. *(nbuffer++) = (unsigned char)(*(ptr2++));
  54858. *(nbuffer++) = 0;
  54859. }
  54860. } break;
  54861. default : { // RGB image
  54862. for (ulongT k = 0; k<wh; ++k) {
  54863. *(nbuffer++) = (unsigned char)(*(ptr1++));
  54864. *(nbuffer++) = (unsigned char)(*(ptr2++));
  54865. *(nbuffer++) = (unsigned char)(*(ptr3++));
  54866. }
  54867. }
  54868. }
  54869. cimg::fwrite(buffer,3*wh,nfile);
  54870. if (!file) cimg::fclose(nfile);
  54871. delete[] buffer;
  54872. return *this;
  54873. }
  54874. //! Save image as a RGBA file.
  54875. /**
  54876. \param filename Filename, as a C-string.
  54877. **/
  54878. const CImg<T>& save_rgba(const char *const filename) const {
  54879. return _save_rgba(0,filename);
  54880. }
  54881. //! Save image as a RGBA file \overloading.
  54882. const CImg<T>& save_rgba(std::FILE *const file) const {
  54883. return _save_rgba(file,0);
  54884. }
  54885. const CImg<T>& _save_rgba(std::FILE *const file, const char *const filename) const {
  54886. if (!file && !filename)
  54887. throw CImgArgumentException(_cimg_instance
  54888. "save_rgba(): Specified filename is (null).",
  54889. cimg_instance);
  54890. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  54891. if (_spectrum!=4)
  54892. cimg::warn(_cimg_instance
  54893. "save_rgba(): image instance has not exactly 4 channels, for file '%s'.",
  54894. cimg_instance,
  54895. filename?filename:"(FILE*)");
  54896. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  54897. const ulongT wh = (ulongT)_width*_height;
  54898. unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer;
  54899. const T
  54900. *ptr1 = data(0,0,0,0),
  54901. *ptr2 = _spectrum>1?data(0,0,0,1):0,
  54902. *ptr3 = _spectrum>2?data(0,0,0,2):0,
  54903. *ptr4 = _spectrum>3?data(0,0,0,3):0;
  54904. switch (_spectrum) {
  54905. case 1 : { // Scalar images
  54906. for (ulongT k = 0; k<wh; ++k) {
  54907. const unsigned char val = (unsigned char)*(ptr1++);
  54908. *(nbuffer++) = val;
  54909. *(nbuffer++) = val;
  54910. *(nbuffer++) = val;
  54911. *(nbuffer++) = 255;
  54912. }
  54913. } break;
  54914. case 2 : { // RG images
  54915. for (ulongT k = 0; k<wh; ++k) {
  54916. *(nbuffer++) = (unsigned char)(*(ptr1++));
  54917. *(nbuffer++) = (unsigned char)(*(ptr2++));
  54918. *(nbuffer++) = 0;
  54919. *(nbuffer++) = 255;
  54920. }
  54921. } break;
  54922. case 3 : { // RGB images
  54923. for (ulongT k = 0; k<wh; ++k) {
  54924. *(nbuffer++) = (unsigned char)(*(ptr1++));
  54925. *(nbuffer++) = (unsigned char)(*(ptr2++));
  54926. *(nbuffer++) = (unsigned char)(*(ptr3++));
  54927. *(nbuffer++) = 255;
  54928. }
  54929. } break;
  54930. default : { // RGBA images
  54931. for (ulongT k = 0; k<wh; ++k) {
  54932. *(nbuffer++) = (unsigned char)(*(ptr1++));
  54933. *(nbuffer++) = (unsigned char)(*(ptr2++));
  54934. *(nbuffer++) = (unsigned char)(*(ptr3++));
  54935. *(nbuffer++) = (unsigned char)(*(ptr4++));
  54936. }
  54937. }
  54938. }
  54939. cimg::fwrite(buffer,4*wh,nfile);
  54940. if (!file) cimg::fclose(nfile);
  54941. delete[] buffer;
  54942. return *this;
  54943. }
  54944. //! Save image as a TIFF file.
  54945. /**
  54946. \param filename Filename, as a C-string.
  54947. \param compression_type Type of data compression. Can be <tt>{ 0=None | 1=LZW | 2=JPEG }</tt>.
  54948. \param[out] voxel_size Voxel size, to be stored in the filename.
  54949. \param[out] description Description, to be stored in the filename.
  54950. \param use_bigtiff Allow to save big tiff files (>4Gb).
  54951. \note
  54952. - libtiff support is enabled by defining the precompilation
  54953. directive \c cimg_use_tif.
  54954. - When libtiff is enabled, 2D and 3D (multipage) several
  54955. channel per pixel are supported for
  54956. <tt>char,uchar,short,ushort,float</tt> and \c double pixel types.
  54957. - If \c cimg_use_tiff is not defined at compile time the
  54958. function uses CImg<T>&save_other(const char*).
  54959. **/
  54960. const CImg<T>& save_tiff(const char *const filename, const unsigned int compression_type=0,
  54961. const float *const voxel_size=0, const char *const description=0,
  54962. const bool use_bigtiff=true) const {
  54963. if (!filename)
  54964. throw CImgArgumentException(_cimg_instance
  54965. "save_tiff(): Specified filename is (null).",
  54966. cimg_instance);
  54967. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  54968. #ifdef cimg_use_tiff
  54969. const bool
  54970. _use_bigtiff = use_bigtiff && sizeof(ulongT)>=8 && size()*sizeof(T)>=1UL<<31; // No bigtiff for small images
  54971. TIFF *tif = TIFFOpen(filename,_use_bigtiff?"w8":"w4");
  54972. if (tif) {
  54973. cimg_forZ(*this,z) _save_tiff(tif,z,z,compression_type,voxel_size,description);
  54974. TIFFClose(tif);
  54975. } else throw CImgIOException(_cimg_instance
  54976. "save_tiff(): Failed to open file '%s' for writing.",
  54977. cimg_instance,
  54978. filename);
  54979. return *this;
  54980. #else
  54981. cimg::unused(compression_type,voxel_size,description,use_bigtiff);
  54982. return save_other(filename);
  54983. #endif
  54984. }
  54985. #ifdef cimg_use_tiff
  54986. #define _cimg_save_tiff(types,typed,compression_type) if (!std::strcmp(types,pixel_type())) { \
  54987. const typed foo = (typed)0; return _save_tiff(tif,directory,z,foo,compression_type,voxel_size,description); }
  54988. // [internal] Save a plane into a tiff file
  54989. template<typename t>
  54990. const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z, const t& pixel_t,
  54991. const unsigned int compression_type, const float *const voxel_size,
  54992. const char *const description) const {
  54993. if (is_empty() || !tif || pixel_t) return *this;
  54994. const char *const filename = TIFFFileName(tif);
  54995. uint32_t rowsperstrip = (uint32_t)-1;
  54996. uint16_t spp = _spectrum, bpp = sizeof(t)*8, photometric;
  54997. if (spp==3 || spp==4) photometric = PHOTOMETRIC_RGB;
  54998. else photometric = PHOTOMETRIC_MINISBLACK;
  54999. TIFFSetDirectory(tif,directory);
  55000. TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,_width);
  55001. TIFFSetField(tif,TIFFTAG_IMAGELENGTH,_height);
  55002. if (voxel_size) {
  55003. const float vx = voxel_size[0], vy = voxel_size[1], vz = voxel_size[2];
  55004. TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_NONE);
  55005. TIFFSetField(tif,TIFFTAG_XRESOLUTION,1.f/vx);
  55006. TIFFSetField(tif,TIFFTAG_YRESOLUTION,1.f/vy);
  55007. CImg<charT> s_description(256);
  55008. cimg_snprintf(s_description,s_description._width,"VX=%g VY=%g VZ=%g spacing=%g",vx,vy,vz,vz);
  55009. TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,s_description.data());
  55010. }
  55011. if (description) TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,description);
  55012. TIFFSetField(tif,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
  55013. TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,spp);
  55014. if (cimg::type<t>::is_float()) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,3);
  55015. else if (cimg::type<t>::min()==0) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,1);
  55016. else TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,2);
  55017. double valm, valM = max_min(valm);
  55018. TIFFSetField(tif,TIFFTAG_SMINSAMPLEVALUE,valm);
  55019. TIFFSetField(tif,TIFFTAG_SMAXSAMPLEVALUE,valM);
  55020. TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,bpp);
  55021. TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
  55022. TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric);
  55023. TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_type==2?COMPRESSION_JPEG:
  55024. compression_type==1?COMPRESSION_LZW:COMPRESSION_NONE);
  55025. rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip);
  55026. TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip);
  55027. TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB);
  55028. TIFFSetField(tif,TIFFTAG_SOFTWARE,"CImg");
  55029. t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
  55030. if (buf) {
  55031. for (unsigned int row = 0; row<_height; row+=rowsperstrip) {
  55032. uint32_t nrow = (row + rowsperstrip>_height?_height - row:rowsperstrip);
  55033. tstrip_t strip = TIFFComputeStrip(tif,row,0);
  55034. tsize_t i = 0;
  55035. for (unsigned int rr = 0; rr<nrow; ++rr)
  55036. for (unsigned int cc = 0; cc<_width; ++cc)
  55037. for (unsigned int vv = 0; vv<spp; ++vv)
  55038. buf[i++] = (t)(*this)(cc,row + rr,z,vv);
  55039. if (TIFFWriteEncodedStrip(tif,strip,buf,i*sizeof(t))<0)
  55040. throw CImgIOException(_cimg_instance
  55041. "save_tiff(): Invalid strip writing when saving file '%s'.",
  55042. cimg_instance,
  55043. filename?filename:"(FILE*)");
  55044. }
  55045. _TIFFfree(buf);
  55046. }
  55047. TIFFWriteDirectory(tif);
  55048. return *this;
  55049. }
  55050. const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z,
  55051. const unsigned int compression_type, const float *const voxel_size,
  55052. const char *const description) const {
  55053. _cimg_save_tiff("bool",unsigned char,compression_type);
  55054. _cimg_save_tiff("unsigned char",unsigned char,compression_type);
  55055. _cimg_save_tiff("char",char,compression_type);
  55056. _cimg_save_tiff("unsigned short",unsigned short,compression_type);
  55057. _cimg_save_tiff("short",short,compression_type);
  55058. _cimg_save_tiff("unsigned int",unsigned int,compression_type);
  55059. _cimg_save_tiff("int",int,compression_type);
  55060. _cimg_save_tiff("unsigned int64",unsigned int,compression_type);
  55061. _cimg_save_tiff("int64",int,compression_type);
  55062. _cimg_save_tiff("float",float,compression_type);
  55063. _cimg_save_tiff("double",float,compression_type);
  55064. const char *const filename = TIFFFileName(tif);
  55065. throw CImgInstanceException(_cimg_instance
  55066. "save_tiff(): Unsupported pixel type '%s' for file '%s'.",
  55067. cimg_instance,
  55068. pixel_type(),filename?filename:"(FILE*)");
  55069. return *this;
  55070. }
  55071. #endif
  55072. //! Save image as a MINC2 file.
  55073. /**
  55074. \param filename Filename, as a C-string.
  55075. \param imitate_file If non-zero, reference filename, as a C-string, to borrow header from.
  55076. **/
  55077. const CImg<T>& save_minc2(const char *const filename,
  55078. const char *const imitate_file=0) const {
  55079. if (!filename)
  55080. throw CImgArgumentException(_cimg_instance
  55081. "save_minc2(): Specified filename is (null).",
  55082. cimg_instance);
  55083. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55084. #ifndef cimg_use_minc2
  55085. cimg::unused(imitate_file);
  55086. return save_other(filename);
  55087. #else
  55088. minc::minc_1_writer wtr;
  55089. if (imitate_file)
  55090. wtr.open(filename, imitate_file);
  55091. else {
  55092. minc::minc_info di;
  55093. if (width()) di.push_back(minc::dim_info(width(),width()*0.5,-1,minc::dim_info::DIM_X));
  55094. if (height()) di.push_back(minc::dim_info(height(),height()*0.5,-1,minc::dim_info::DIM_Y));
  55095. if (depth()) di.push_back(minc::dim_info(depth(),depth()*0.5,-1,minc::dim_info::DIM_Z));
  55096. if (spectrum()) di.push_back(minc::dim_info(spectrum(),spectrum()*0.5,-1,minc::dim_info::DIM_TIME));
  55097. wtr.open(filename,di,1,NC_FLOAT,0);
  55098. }
  55099. if (pixel_type()==cimg::type<unsigned char>::string())
  55100. wtr.setup_write_byte();
  55101. else if (pixel_type()==cimg::type<int>::string())
  55102. wtr.setup_write_int();
  55103. else if (pixel_type()==cimg::type<double>::string())
  55104. wtr.setup_write_double();
  55105. else
  55106. wtr.setup_write_float();
  55107. minc::save_standard_volume(wtr, this->_data);
  55108. return *this;
  55109. #endif
  55110. }
  55111. //! Save image as an ANALYZE7.5 or NIFTI file.
  55112. /**
  55113. \param filename Filename, as a C-string.
  55114. \param voxel_size Pointer to 3 consecutive values that tell about the voxel sizes along the X,Y and Z dimensions.
  55115. **/
  55116. const CImg<T>& save_analyze(const char *const filename, const float *const voxel_size=0) const {
  55117. if (!filename)
  55118. throw CImgArgumentException(_cimg_instance
  55119. "save_analyze(): Specified filename is (null).",
  55120. cimg_instance);
  55121. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55122. std::FILE *file;
  55123. CImg<charT> hname(1024), iname(1024);
  55124. const char *const ext = cimg::split_filename(filename);
  55125. short datatype = -1;
  55126. if (!*ext) {
  55127. cimg_snprintf(hname,hname._width,"%s.hdr",filename);
  55128. cimg_snprintf(iname,iname._width,"%s.img",filename);
  55129. }
  55130. if (!cimg::strncasecmp(ext,"hdr",3)) {
  55131. std::strcpy(hname,filename);
  55132. std::strncpy(iname,filename,iname._width - 1);
  55133. cimg_sprintf(iname._data + std::strlen(iname) - 3,"img");
  55134. }
  55135. if (!cimg::strncasecmp(ext,"img",3)) {
  55136. std::strcpy(hname,filename);
  55137. std::strncpy(iname,filename,iname._width - 1);
  55138. cimg_sprintf(hname._data + std::strlen(iname) - 3,"hdr");
  55139. }
  55140. if (!cimg::strncasecmp(ext,"nii",3)) {
  55141. std::strncpy(hname,filename,hname._width - 1); *iname = 0;
  55142. }
  55143. CImg<charT> header(*iname?348:352,1,1,1,0);
  55144. int *const iheader = (int*)header._data;
  55145. *iheader = 348;
  55146. std::strcpy(header._data + 4,"CImg");
  55147. std::strcpy(header._data + 14," ");
  55148. ((short*)&(header[36]))[0] = 4096;
  55149. ((char*)&(header[38]))[0] = 114;
  55150. ((short*)&(header[40]))[0] = 4;
  55151. ((short*)&(header[40]))[1] = (short)_width;
  55152. ((short*)&(header[40]))[2] = (short)_height;
  55153. ((short*)&(header[40]))[3] = (short)_depth;
  55154. ((short*)&(header[40]))[4] = (short)_spectrum;
  55155. if (!cimg::strcasecmp(pixel_type(),"bool")) datatype = 2;
  55156. if (!cimg::strcasecmp(pixel_type(),"unsigned char")) datatype = 2;
  55157. if (!cimg::strcasecmp(pixel_type(),"char")) datatype = 2;
  55158. if (!cimg::strcasecmp(pixel_type(),"unsigned short")) datatype = 4;
  55159. if (!cimg::strcasecmp(pixel_type(),"short")) datatype = 4;
  55160. if (!cimg::strcasecmp(pixel_type(),"unsigned int")) datatype = 8;
  55161. if (!cimg::strcasecmp(pixel_type(),"int")) datatype = 8;
  55162. if (!cimg::strcasecmp(pixel_type(),"unsigned int64")) datatype = 8;
  55163. if (!cimg::strcasecmp(pixel_type(),"int64")) datatype = 8;
  55164. if (!cimg::strcasecmp(pixel_type(),"float")) datatype = 16;
  55165. if (!cimg::strcasecmp(pixel_type(),"double")) datatype = 64;
  55166. if (datatype<0)
  55167. throw CImgIOException(_cimg_instance
  55168. "save_analyze(): Unsupported pixel type '%s' for file '%s'.",
  55169. cimg_instance,
  55170. pixel_type(),filename);
  55171. ((short*)&(header[70]))[0] = datatype;
  55172. ((short*)&(header[72]))[0] = sizeof(T);
  55173. ((float*)&(header[108]))[0] = (float)(*iname?0:header.width());
  55174. ((float*)&(header[112]))[0] = 1;
  55175. ((float*)&(header[76]))[0] = 0;
  55176. if (voxel_size) {
  55177. ((float*)&(header[76]))[1] = voxel_size[0];
  55178. ((float*)&(header[76]))[2] = voxel_size[1];
  55179. ((float*)&(header[76]))[3] = voxel_size[2];
  55180. } else ((float*)&(header[76]))[1] = ((float*)&(header[76]))[2] = ((float*)&(header[76]))[3] = 1;
  55181. file = cimg::fopen(hname,"wb");
  55182. cimg::fwrite(header._data,header.width(),file);
  55183. if (*iname) { cimg::fclose(file); file = cimg::fopen(iname,"wb"); }
  55184. cimg::fwrite(_data,size(),file);
  55185. cimg::fclose(file);
  55186. return *this;
  55187. }
  55188. //! Save image as a .cimg file.
  55189. /**
  55190. \param filename Filename, as a C-string.
  55191. \param is_compressed Tells if the file contains compressed image data.
  55192. **/
  55193. const CImg<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {
  55194. CImgList<T>(*this,true).save_cimg(filename,is_compressed);
  55195. return *this;
  55196. }
  55197. //! Save image as a .cimg file \overloading.
  55198. const CImg<T>& save_cimg(std::FILE *const file, const bool is_compressed=false) const {
  55199. CImgList<T>(*this,true).save_cimg(file,is_compressed);
  55200. return *this;
  55201. }
  55202. //! Save image as a sub-image into an existing .cimg file.
  55203. /**
  55204. \param filename Filename, as a C-string.
  55205. \param n0 Index of the image inside the file.
  55206. \param x0 X-coordinate of the sub-image location.
  55207. \param y0 Y-coordinate of the sub-image location.
  55208. \param z0 Z-coordinate of the sub-image location.
  55209. \param c0 C-coordinate of the sub-image location.
  55210. **/
  55211. const CImg<T>& save_cimg(const char *const filename,
  55212. const unsigned int n0,
  55213. const unsigned int x0, const unsigned int y0,
  55214. const unsigned int z0, const unsigned int c0) const {
  55215. CImgList<T>(*this,true).save_cimg(filename,n0,x0,y0,z0,c0);
  55216. return *this;
  55217. }
  55218. //! Save image as a sub-image into an existing .cimg file \overloading.
  55219. const CImg<T>& save_cimg(std::FILE *const file,
  55220. const unsigned int n0,
  55221. const unsigned int x0, const unsigned int y0,
  55222. const unsigned int z0, const unsigned int c0) const {
  55223. CImgList<T>(*this,true).save_cimg(file,n0,x0,y0,z0,c0);
  55224. return *this;
  55225. }
  55226. //! Save blank image as a .cimg file.
  55227. /**
  55228. \param filename Filename, as a C-string.
  55229. \param dx Width of the image.
  55230. \param dy Height of the image.
  55231. \param dz Depth of the image.
  55232. \param dc Number of channels of the image.
  55233. \note
  55234. - All pixel values of the saved image are set to \c 0.
  55235. - Use this method to save large images without having to instantiate and allocate them.
  55236. **/
  55237. static void save_empty_cimg(const char *const filename,
  55238. const unsigned int dx, const unsigned int dy=1,
  55239. const unsigned int dz=1, const unsigned int dc=1) {
  55240. return CImgList<T>::save_empty_cimg(filename,1,dx,dy,dz,dc);
  55241. }
  55242. //! Save blank image as a .cimg file \overloading.
  55243. /**
  55244. Same as save_empty_cimg(const char *,unsigned int,unsigned int,unsigned int,unsigned int)
  55245. with a file stream argument instead of a filename string.
  55246. **/
  55247. static void save_empty_cimg(std::FILE *const file,
  55248. const unsigned int dx, const unsigned int dy=1,
  55249. const unsigned int dz=1, const unsigned int dc=1) {
  55250. return CImgList<T>::save_empty_cimg(file,1,dx,dy,dz,dc);
  55251. }
  55252. //! Save image as an INRIMAGE-4 file.
  55253. /**
  55254. \param filename Filename, as a C-string.
  55255. \param voxel_size Pointer to 3 values specifying the voxel sizes along the X,Y and Z dimensions.
  55256. **/
  55257. const CImg<T>& save_inr(const char *const filename, const float *const voxel_size=0) const {
  55258. return _save_inr(0,filename,voxel_size);
  55259. }
  55260. //! Save image as an INRIMAGE-4 file \overloading.
  55261. const CImg<T>& save_inr(std::FILE *const file, const float *const voxel_size=0) const {
  55262. return _save_inr(file,0,voxel_size);
  55263. }
  55264. const CImg<T>& _save_inr(std::FILE *const file, const char *const filename, const float *const voxel_size) const {
  55265. if (!file && !filename)
  55266. throw CImgArgumentException(_cimg_instance
  55267. "save_inr(): Specified filename is (null).",
  55268. cimg_instance);
  55269. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  55270. int inrpixsize = -1;
  55271. const char *inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0";
  55272. if (!cimg::strcasecmp(pixel_type(),"unsigned char")) {
  55273. inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1;
  55274. }
  55275. if (!cimg::strcasecmp(pixel_type(),"char")) {
  55276. inrtype = "fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1;
  55277. }
  55278. if (!cimg::strcasecmp(pixel_type(),"unsigned short")) {
  55279. inrtype = "unsigned fixed\nPIXSIZE=16 bits\nSCALE=2**0";inrpixsize = 2;
  55280. }
  55281. if (!cimg::strcasecmp(pixel_type(),"short")) {
  55282. inrtype = "fixed\nPIXSIZE=16 bits\nSCALE=2**0"; inrpixsize = 2;
  55283. }
  55284. if (!cimg::strcasecmp(pixel_type(),"unsigned int")) {
  55285. inrtype = "unsigned fixed\nPIXSIZE=32 bits\nSCALE=2**0";inrpixsize = 4;
  55286. }
  55287. if (!cimg::strcasecmp(pixel_type(),"int")) {
  55288. inrtype = "fixed\nPIXSIZE=32 bits\nSCALE=2**0"; inrpixsize = 4;
  55289. }
  55290. if (!cimg::strcasecmp(pixel_type(),"float")) {
  55291. inrtype = "float\nPIXSIZE=32 bits"; inrpixsize = 4;
  55292. }
  55293. if (!cimg::strcasecmp(pixel_type(),"double")) {
  55294. inrtype = "float\nPIXSIZE=64 bits"; inrpixsize = 8;
  55295. }
  55296. if (inrpixsize<=0)
  55297. throw CImgIOException(_cimg_instance
  55298. "save_inr(): Unsupported pixel type '%s' for file '%s'",
  55299. cimg_instance,
  55300. pixel_type(),filename?filename:"(FILE*)");
  55301. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  55302. CImg<charT> header(257);
  55303. int err = cimg_snprintf(header,header._width,"#INRIMAGE-4#{\nXDIM=%u\nYDIM=%u\nZDIM=%u\nVDIM=%u\n",
  55304. _width,_height,_depth,_spectrum);
  55305. if (voxel_size) err+=cimg_sprintf(header._data + err,"VX=%g\nVY=%g\nVZ=%g\n",
  55306. voxel_size[0],voxel_size[1],voxel_size[2]);
  55307. err+=cimg_sprintf(header._data + err,"TYPE=%s\nCPU=%s\n",inrtype,cimg::endianness()?"sun":"decm");
  55308. std::memset(header._data + err,'\n',252 - err);
  55309. std::memcpy(header._data + 252,"##}\n",4);
  55310. cimg::fwrite(header._data,256,nfile);
  55311. cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) cimg::fwrite(&((*this)(x,y,z,c)),1,nfile);
  55312. if (!file) cimg::fclose(nfile);
  55313. return *this;
  55314. }
  55315. //! Save image as an OpenEXR file.
  55316. /**
  55317. \param filename Filename, as a C-string.
  55318. \note The OpenEXR file format is <a href="http://en.wikipedia.org/wiki/OpenEXR">described here</a>.
  55319. **/
  55320. const CImg<T>& save_exr(const char *const filename) const {
  55321. if (!filename)
  55322. throw CImgArgumentException(_cimg_instance
  55323. "save_exr(): Specified filename is (null).",
  55324. cimg_instance);
  55325. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55326. if (_depth>1)
  55327. cimg::warn(_cimg_instance
  55328. "save_exr(): Instance is volumetric, only the first slice will be saved in file '%s'.",
  55329. cimg_instance,
  55330. filename);
  55331. #ifndef cimg_use_openexr
  55332. return save_other(filename);
  55333. #else
  55334. Imf::Rgba *const ptrd0 = new Imf::Rgba[(size_t)_width*_height], *ptrd = ptrd0, rgba;
  55335. switch (_spectrum) {
  55336. case 1 : { // Grayscale image
  55337. for (const T *ptr_r = data(), *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
  55338. rgba.r = (half)(*(ptr_r));
  55339. rgba.g = (half)(*(ptr_r));
  55340. rgba.b = (half)(*(ptr_r++));
  55341. rgba.a = (half)1;
  55342. *(ptrd++) = rgba;
  55343. }
  55344. } break;
  55345. case 2 : { // RG image
  55346. for (const T *ptr_r = data(), *ptr_g = data(0,0,0,1),
  55347. *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e; ) {
  55348. rgba.r = (half)(*(ptr_r++));
  55349. rgba.g = (half)(*(ptr_g++));
  55350. rgba.b = (half)0;
  55351. rgba.a = (half)1;
  55352. *(ptrd++) = rgba;
  55353. }
  55354. } break;
  55355. case 3 : { // RGB image
  55356. for (const T *ptr_r = data(), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2),
  55357. *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
  55358. rgba.r = (half)(*(ptr_r++));
  55359. rgba.g = (half)(*(ptr_g++));
  55360. rgba.b = (half)(*(ptr_b++));
  55361. rgba.a = (half)1;
  55362. *(ptrd++) = rgba;
  55363. }
  55364. } break;
  55365. default : { // RGBA image
  55366. for (const T *ptr_r = data(), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2), *ptr_a = data(0,0,0,3),
  55367. *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
  55368. rgba.r = (half)(*(ptr_r++));
  55369. rgba.g = (half)(*(ptr_g++));
  55370. rgba.b = (half)(*(ptr_b++));
  55371. rgba.a = (half)(*(ptr_a++));
  55372. *(ptrd++) = rgba;
  55373. }
  55374. } break;
  55375. }
  55376. Imf::RgbaOutputFile outFile(filename,_width,_height,
  55377. _spectrum==1?Imf::WRITE_Y:_spectrum==2?Imf::WRITE_YA:_spectrum==3?
  55378. Imf::WRITE_RGB:Imf::WRITE_RGBA);
  55379. outFile.setFrameBuffer(ptrd0,1,_width);
  55380. outFile.writePixels(_height);
  55381. delete[] ptrd0;
  55382. return *this;
  55383. #endif
  55384. }
  55385. //! Save image as a Pandore-5 file.
  55386. /**
  55387. \param filename Filename, as a C-string.
  55388. \param colorspace Colorspace data field in output file
  55389. (see <a href="http://www.greyc.ensicaen.fr/~regis/Pandore">Pandore file specifications</a>
  55390. for more information).
  55391. **/
  55392. const CImg<T>& save_pandore(const char *const filename, const unsigned int colorspace=0) const {
  55393. return _save_pandore(0,filename,colorspace);
  55394. }
  55395. //! Save image as a Pandore-5 file \overloading.
  55396. /**
  55397. Same as save_pandore(const char *,unsigned int) const
  55398. with a file stream argument instead of a filename string.
  55399. **/
  55400. const CImg<T>& save_pandore(std::FILE *const file, const unsigned int colorspace=0) const {
  55401. return _save_pandore(file,0,colorspace);
  55402. }
  55403. unsigned int _save_pandore_header_length(unsigned int id, unsigned int *dims, const unsigned int colorspace) const {
  55404. unsigned int nbdims = 0;
  55405. if (id==2 || id==3 || id==4) {
  55406. dims[0] = 1; dims[1] = _width; nbdims = 2;
  55407. }
  55408. if (id==5 || id==6 || id==7) {
  55409. dims[0] = 1; dims[1] = _height; dims[2] = _width; nbdims=3;
  55410. }
  55411. if (id==8 || id==9 || id==10) {
  55412. dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4;
  55413. }
  55414. if (id==16 || id==17 || id==18) {
  55415. dims[0] = 3; dims[1] = _height; dims[2] = _width; dims[3] = colorspace; nbdims = 4;
  55416. }
  55417. if (id==19 || id==20 || id==21) {
  55418. dims[0] = 3; dims[1] = _depth; dims[2] = _height; dims[3] = _width; dims[4] = colorspace; nbdims = 5;
  55419. }
  55420. if (id==22 || id==23 || id==25) {
  55421. dims[0] = _spectrum; dims[1] = _width; nbdims = 2;
  55422. }
  55423. if (id==26 || id==27 || id==29) {
  55424. dims[0] = _spectrum; dims[1] = _height; dims[2] = _width; nbdims=3;
  55425. }
  55426. if (id==30 || id==31 || id==33) {
  55427. dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4;
  55428. }
  55429. return nbdims;
  55430. }
  55431. const CImg<T>& _save_pandore(std::FILE *const file, const char *const filename,
  55432. const unsigned int colorspace) const {
  55433. #define __cimg_save_pandore_case(dtype) \
  55434. dtype *buffer = new dtype[size()]; \
  55435. const T *ptrs = _data; \
  55436. cimg_foroff(*this,off) *(buffer++) = (dtype)(*(ptrs++)); \
  55437. buffer-=size(); \
  55438. cimg::fwrite(buffer,size(),nfile); \
  55439. delete[] buffer
  55440. #define _cimg_save_pandore_case(sy,sz,sv,stype,id) \
  55441. if (!saved && (sy?(sy==_height):true) && (sz?(sz==_depth):true) && \
  55442. (sv?(sv==_spectrum):true) && !std::strcmp(stype,pixel_type())) { \
  55443. unsigned int *iheader = (unsigned int*)(header + 12); \
  55444. nbdims = _save_pandore_header_length((*iheader=id),dims,colorspace); \
  55445. cimg::fwrite(header,36,nfile); \
  55446. if (sizeof(unsigned long)==4) { CImg<ulongT> ndims(5); \
  55447. for (int d = 0; d<5; ++d) ndims[d] = (unsigned long)dims[d]; \
  55448. cimg::fwrite(ndims._data,nbdims,nfile); } \
  55449. else if (sizeof(unsigned int)==4) { CImg<uintT> ndims(5); \
  55450. for (int d = 0; d<5; ++d) ndims[d] = (unsigned int)dims[d]; \
  55451. cimg::fwrite(ndims._data,nbdims,nfile); } \
  55452. else if (sizeof(unsigned short)==4) { CImg<ushortT> ndims(5); \
  55453. for (int d = 0; d<5; ++d) ndims[d] = (unsigned short)dims[d]; \
  55454. cimg::fwrite(ndims._data,nbdims,nfile); } \
  55455. else throw CImgIOException(_cimg_instance \
  55456. "save_pandore(): Unsupported datatype for file '%s'.",\
  55457. cimg_instance, \
  55458. filename?filename:"(FILE*)"); \
  55459. if (id==2 || id==5 || id==8 || id==16 || id==19 || id==22 || id==26 || id==30) { \
  55460. __cimg_save_pandore_case(unsigned char); \
  55461. } else if (id==3 || id==6 || id==9 || id==17 || id==20 || id==23 || id==27 || id==31) { \
  55462. if (sizeof(unsigned long)==4) { __cimg_save_pandore_case(unsigned long); } \
  55463. else if (sizeof(unsigned int)==4) { __cimg_save_pandore_case(unsigned int); } \
  55464. else if (sizeof(unsigned short)==4) { __cimg_save_pandore_case(unsigned short); } \
  55465. else throw CImgIOException(_cimg_instance \
  55466. "save_pandore(): Unsupported datatype for file '%s'.",\
  55467. cimg_instance, \
  55468. filename?filename:"(FILE*)"); \
  55469. } else if (id==4 || id==7 || id==10 || id==18 || id==21 || id==25 || id==29 || id==33) { \
  55470. if (sizeof(double)==4) { __cimg_save_pandore_case(double); } \
  55471. else if (sizeof(float)==4) { __cimg_save_pandore_case(float); } \
  55472. else throw CImgIOException(_cimg_instance \
  55473. "save_pandore(): Unsupported datatype for file '%s'.",\
  55474. cimg_instance, \
  55475. filename?filename:"(FILE*)"); \
  55476. } \
  55477. saved = true; \
  55478. }
  55479. if (!file && !filename)
  55480. throw CImgArgumentException(_cimg_instance
  55481. "save_pandore(): Specified filename is (null).",
  55482. cimg_instance);
  55483. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  55484. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  55485. unsigned char header[36] = { 'P','A','N','D','O','R','E','0','4',0,0,0,
  55486. 0,0,0,0,'C','I','m','g',0,0,0,0,0,
  55487. 'N','o',' ','d','a','t','e',0,0,0,0 };
  55488. unsigned int nbdims, dims[5] = { 0 };
  55489. bool saved = false;
  55490. _cimg_save_pandore_case(1,1,1,"unsigned char",2);
  55491. _cimg_save_pandore_case(1,1,1,"char",3);
  55492. _cimg_save_pandore_case(1,1,1,"unsigned short",3);
  55493. _cimg_save_pandore_case(1,1,1,"short",3);
  55494. _cimg_save_pandore_case(1,1,1,"unsigned int",3);
  55495. _cimg_save_pandore_case(1,1,1,"int",3);
  55496. _cimg_save_pandore_case(1,1,1,"unsigned int64",3);
  55497. _cimg_save_pandore_case(1,1,1,"int64",3);
  55498. _cimg_save_pandore_case(1,1,1,"float",4);
  55499. _cimg_save_pandore_case(1,1,1,"double",4);
  55500. _cimg_save_pandore_case(0,1,1,"unsigned char",5);
  55501. _cimg_save_pandore_case(0,1,1,"char",6);
  55502. _cimg_save_pandore_case(0,1,1,"unsigned short",6);
  55503. _cimg_save_pandore_case(0,1,1,"short",6);
  55504. _cimg_save_pandore_case(0,1,1,"unsigned int",6);
  55505. _cimg_save_pandore_case(0,1,1,"int",6);
  55506. _cimg_save_pandore_case(0,1,1,"unsigned int64",6);
  55507. _cimg_save_pandore_case(0,1,1,"int64",6);
  55508. _cimg_save_pandore_case(0,1,1,"float",7);
  55509. _cimg_save_pandore_case(0,1,1,"double",7);
  55510. _cimg_save_pandore_case(0,0,1,"unsigned char",8);
  55511. _cimg_save_pandore_case(0,0,1,"char",9);
  55512. _cimg_save_pandore_case(0,0,1,"unsigned short",9);
  55513. _cimg_save_pandore_case(0,0,1,"short",9);
  55514. _cimg_save_pandore_case(0,0,1,"unsigned int",9);
  55515. _cimg_save_pandore_case(0,0,1,"int",9);
  55516. _cimg_save_pandore_case(0,0,1,"unsigned int64",9);
  55517. _cimg_save_pandore_case(0,0,1,"int64",9);
  55518. _cimg_save_pandore_case(0,0,1,"float",10);
  55519. _cimg_save_pandore_case(0,0,1,"double",10);
  55520. _cimg_save_pandore_case(0,1,3,"unsigned char",16);
  55521. _cimg_save_pandore_case(0,1,3,"char",17);
  55522. _cimg_save_pandore_case(0,1,3,"unsigned short",17);
  55523. _cimg_save_pandore_case(0,1,3,"short",17);
  55524. _cimg_save_pandore_case(0,1,3,"unsigned int",17);
  55525. _cimg_save_pandore_case(0,1,3,"int",17);
  55526. _cimg_save_pandore_case(0,1,3,"unsigned int64",17);
  55527. _cimg_save_pandore_case(0,1,3,"int64",17);
  55528. _cimg_save_pandore_case(0,1,3,"float",18);
  55529. _cimg_save_pandore_case(0,1,3,"double",18);
  55530. _cimg_save_pandore_case(0,0,3,"unsigned char",19);
  55531. _cimg_save_pandore_case(0,0,3,"char",20);
  55532. _cimg_save_pandore_case(0,0,3,"unsigned short",20);
  55533. _cimg_save_pandore_case(0,0,3,"short",20);
  55534. _cimg_save_pandore_case(0,0,3,"unsigned int",20);
  55535. _cimg_save_pandore_case(0,0,3,"int",20);
  55536. _cimg_save_pandore_case(0,0,3,"unsigned int64",20);
  55537. _cimg_save_pandore_case(0,0,3,"int64",20);
  55538. _cimg_save_pandore_case(0,0,3,"float",21);
  55539. _cimg_save_pandore_case(0,0,3,"double",21);
  55540. _cimg_save_pandore_case(1,1,0,"unsigned char",22);
  55541. _cimg_save_pandore_case(1,1,0,"char",23);
  55542. _cimg_save_pandore_case(1,1,0,"unsigned short",23);
  55543. _cimg_save_pandore_case(1,1,0,"short",23);
  55544. _cimg_save_pandore_case(1,1,0,"unsigned int",23);
  55545. _cimg_save_pandore_case(1,1,0,"int",23);
  55546. _cimg_save_pandore_case(1,1,0,"unsigned int64",23);
  55547. _cimg_save_pandore_case(1,1,0,"int64",23);
  55548. _cimg_save_pandore_case(1,1,0,"float",25);
  55549. _cimg_save_pandore_case(1,1,0,"double",25);
  55550. _cimg_save_pandore_case(0,1,0,"unsigned char",26);
  55551. _cimg_save_pandore_case(0,1,0,"char",27);
  55552. _cimg_save_pandore_case(0,1,0,"unsigned short",27);
  55553. _cimg_save_pandore_case(0,1,0,"short",27);
  55554. _cimg_save_pandore_case(0,1,0,"unsigned int",27);
  55555. _cimg_save_pandore_case(0,1,0,"int",27);
  55556. _cimg_save_pandore_case(0,1,0,"unsigned int64",27);
  55557. _cimg_save_pandore_case(0,1,0,"int64",27);
  55558. _cimg_save_pandore_case(0,1,0,"float",29);
  55559. _cimg_save_pandore_case(0,1,0,"double",29);
  55560. _cimg_save_pandore_case(0,0,0,"unsigned char",30);
  55561. _cimg_save_pandore_case(0,0,0,"char",31);
  55562. _cimg_save_pandore_case(0,0,0,"unsigned short",31);
  55563. _cimg_save_pandore_case(0,0,0,"short",31);
  55564. _cimg_save_pandore_case(0,0,0,"unsigned int",31);
  55565. _cimg_save_pandore_case(0,0,0,"int",31);
  55566. _cimg_save_pandore_case(0,0,0,"unsigned int64",31);
  55567. _cimg_save_pandore_case(0,0,0,"int64",31);
  55568. _cimg_save_pandore_case(0,0,0,"float",33);
  55569. _cimg_save_pandore_case(0,0,0,"double",33);
  55570. if (!file) cimg::fclose(nfile);
  55571. return *this;
  55572. }
  55573. //! Save image as a raw data file.
  55574. /**
  55575. \param filename Filename, as a C-string.
  55576. \param is_multiplexed Tells if the image channels are stored in a multiplexed way (\c true) or not (\c false).
  55577. \note The .raw format does not store the image dimensions in the output file,
  55578. so you have to keep track of them somewhere to be able to read the file correctly afterwards.
  55579. **/
  55580. const CImg<T>& save_raw(const char *const filename, const bool is_multiplexed=false) const {
  55581. return _save_raw(0,filename,is_multiplexed);
  55582. }
  55583. //! Save image as a raw data file \overloading.
  55584. /**
  55585. Same as save_raw(const char *,bool) const
  55586. with a file stream argument instead of a filename string.
  55587. **/
  55588. const CImg<T>& save_raw(std::FILE *const file, const bool is_multiplexed=false) const {
  55589. return _save_raw(file,0,is_multiplexed);
  55590. }
  55591. const CImg<T>& _save_raw(std::FILE *const file, const char *const filename, const bool is_multiplexed) const {
  55592. if (!file && !filename)
  55593. throw CImgArgumentException(_cimg_instance
  55594. "save_raw(): Specified filename is (null).",
  55595. cimg_instance);
  55596. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  55597. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  55598. if (pixel_type()==cimg::type<bool>::string()) { // Boolean data (bitwise)
  55599. ulongT siz;
  55600. const unsigned char *const buf = _bool2uchar(siz,is_multiplexed);
  55601. cimg::fwrite(buf,siz,nfile);
  55602. delete[] buf;
  55603. } else { // Non boolean data
  55604. if (!is_multiplexed || _spectrum==1) cimg::fwrite(_data,size(),nfile); // Non-multiplexed
  55605. else { // Multiplexed
  55606. CImg<T> buf(_spectrum);
  55607. cimg_forXYZ(*this,x,y,z) {
  55608. cimg_forC(*this,c) buf[c] = (*this)(x,y,z,c);
  55609. cimg::fwrite(buf._data,_spectrum,nfile);
  55610. }
  55611. }
  55612. }
  55613. if (!file) cimg::fclose(nfile);
  55614. return *this;
  55615. }
  55616. // Return unsigned char buffer that encodes data of a CImg<bool> instance bitwise.
  55617. // (buffer needs to be deallocated afterwards, with delete[]).
  55618. const unsigned char *_bool2uchar(ulongT &siz, const bool is_multiplexed) const {
  55619. const ulongT _siz = size();
  55620. siz = _siz/8 + (_siz%8?1:0);
  55621. unsigned char *const buf = new unsigned char[siz], *ptrd = buf, val = 0, bit = 0;
  55622. if (!is_multiplexed || _spectrum==1) // Non-multiplexed
  55623. cimg_for(*this,ptrs,T) { (val<<=1)|=(*ptrs?1:0); if (++bit==8) { *(ptrd++) = val; val = bit = 0; }}
  55624. else // Multiplexed
  55625. cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) {
  55626. (val<<=1)|=((*this)(x,y,z,c)?1:0); if (++bit==8) { *(ptrd++) = val; val = bit = 0; }
  55627. }
  55628. if (bit) *ptrd = val;
  55629. return buf;
  55630. }
  55631. // Fill CImg<T> instance from bitwise data encoded in an unsigned char buffer.
  55632. void _uchar2bool(const unsigned char *buf, const ulongT siz, const bool is_multiplexed) {
  55633. const ulongT S = std::min(siz*8,size());
  55634. const unsigned char *ptrs = buf;
  55635. unsigned char val = 0, mask = 0;
  55636. T *ptrd = _data;
  55637. if (S && (!is_multiplexed || _spectrum==1)) // Non-multiplexed
  55638. for (ulongT off = 0; off<S; ++off) {
  55639. if (!(mask>>=1)) { val = *(ptrs++); mask = 128; }
  55640. *(ptrd++) = (T)((val&mask)?1:0);
  55641. }
  55642. else if (S) { // Multiplexed
  55643. ulongT off = 0;
  55644. for (int z = 0; z<depth() && off<=S; ++z)
  55645. for (int y = 0; y<height() && off<=S; ++y)
  55646. for (int x = 0; x<width() && off<=S; ++x)
  55647. for (int c = 0; c<spectrum() && off<=S; ++c) {
  55648. if (!(mask>>=1)) { val = *(ptrs++); ++off; mask = 128; }
  55649. (*this)(x,y,z,c) = (T)((val&mask)?1:0);
  55650. }
  55651. }
  55652. }
  55653. //! Save image as a .yuv video file.
  55654. /**
  55655. \param filename Filename, as a C-string.
  55656. \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
  55657. \param is_rgb Tells if pixel values of the instance image are RGB-coded (\c true) or YUV-coded (\c false).
  55658. \note Each slice of the instance image is considered to be a single frame of the output video file.
  55659. **/
  55660. const CImg<T>& save_yuv(const char *const filename,
  55661. const unsigned int chroma_subsampling=444,
  55662. const bool is_rgb=true) const {
  55663. CImgList<T>(*this,true).save_yuv(filename,chroma_subsampling,is_rgb);
  55664. return *this;
  55665. }
  55666. //! Save image as a .yuv video file \overloading.
  55667. /**
  55668. Same as save_yuv(const char*,const unsigned int,const bool) const
  55669. with a file stream argument instead of a filename string.
  55670. **/
  55671. const CImg<T>& save_yuv(std::FILE *const file,
  55672. const unsigned int chroma_subsampling=444,
  55673. const bool is_rgb=true) const {
  55674. CImgList<T>(*this,true).save_yuv(file,chroma_subsampling,is_rgb);
  55675. return *this;
  55676. }
  55677. //! Save 3D object as an Object File Format (.off) file.
  55678. /**
  55679. \param filename Filename, as a C-string.
  55680. \param primitives List of 3D object primitives.
  55681. \param colors List of 3D object colors.
  55682. \note
  55683. - Instance image contains the vertices data of the 3D object.
  55684. - Textured, transparent or sphere-shaped primitives cannot be managed by the .off file format.
  55685. Such primitives will be lost or simplified during file saving.
  55686. - The .off file format is <a href="http://people.sc.fsu.edu/~jburkardt/html/off_format.html">described here</a>.
  55687. **/
  55688. template<typename tf, typename tc>
  55689. const CImg<T>& save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
  55690. const char *const filename) const {
  55691. return _save_off(primitives,colors,0,filename);
  55692. }
  55693. //! Save 3D object as an Object File Format (.off) file \overloading.
  55694. /**
  55695. Same as save_off(const CImgList<tf>&,const CImgList<tc>&,const char*) const
  55696. with a file stream argument instead of a filename string.
  55697. **/
  55698. template<typename tf, typename tc>
  55699. const CImg<T>& save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
  55700. std::FILE *const file) const {
  55701. return _save_off(primitives,colors,file,0);
  55702. }
  55703. template<typename tf, typename tc>
  55704. const CImg<T>& _save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
  55705. std::FILE *const file, const char *const filename) const {
  55706. if (!file && !filename)
  55707. throw CImgArgumentException(_cimg_instance
  55708. "save_off(): Specified filename is (null).",
  55709. cimg_instance);
  55710. if (is_empty())
  55711. throw CImgInstanceException(_cimg_instance
  55712. "save_off(): Empty instance, for file '%s'.",
  55713. cimg_instance,
  55714. filename?filename:"(FILE*)");
  55715. CImgList<T> opacities;
  55716. CImg<charT> error_message(1024);
  55717. if (!is_object3d(primitives,colors,opacities,true,error_message))
  55718. throw CImgInstanceException(_cimg_instance
  55719. "save_off(): Invalid specified 3D object, for file '%s' (%s).",
  55720. cimg_instance,
  55721. filename?filename:"(FILE*)",error_message.data());
  55722. const CImg<tc> default_color(1,3,1,1,(tc)std::min((int)cimg::type<tc>::max(),200));
  55723. std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
  55724. unsigned int supported_primitives = 0;
  55725. cimglist_for(primitives,l) if (primitives[l].size()!=5) ++supported_primitives;
  55726. std::fprintf(nfile,"OFF\n%u %u %u\n",_width,supported_primitives,3*primitives._width);
  55727. cimg_forX(*this,i) std::fprintf(nfile,"%f %f %f\n",
  55728. (float)((*this)(i,0)),(float)((*this)(i,1)),(float)((*this)(i,2)));
  55729. cimglist_for(primitives,l) {
  55730. const CImg<tc>& color = l<colors.width()?colors[l]:default_color;
  55731. const unsigned int psiz = primitives[l].size(), csiz = color.size();
  55732. const float r = color[0]/255.f, g = (csiz>1?color[1]:r)/255.f, b = (csiz>2?color[2]:g)/255.f;
  55733. switch (psiz) {
  55734. case 1 : std::fprintf(nfile,"1 %u %f %f %f\n",
  55735. (unsigned int)primitives(l,0),r,g,b); break;
  55736. case 2 : std::fprintf(nfile,"2 %u %u %f %f %f\n",
  55737. (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break;
  55738. case 3 : std::fprintf(nfile,"3 %u %u %u %f %f %f\n",
  55739. (unsigned int)primitives(l,0),(unsigned int)primitives(l,2),
  55740. (unsigned int)primitives(l,1),r,g,b); break;
  55741. case 4 : std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n",
  55742. (unsigned int)primitives(l,0),(unsigned int)primitives(l,3),
  55743. (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),r,g,b); break;
  55744. case 5 : std::fprintf(nfile,"2 %u %u %f %f %f\n",
  55745. (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break;
  55746. case 6 : {
  55747. const unsigned int xt = (unsigned int)primitives(l,2), yt = (unsigned int)primitives(l,3);
  55748. const float
  55749. rt = color.atXY(xt,yt,0)/255.f,
  55750. gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
  55751. bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
  55752. std::fprintf(nfile,"2 %u %u %f %f %f\n",
  55753. (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),rt,gt,bt);
  55754. } break;
  55755. case 9 : {
  55756. const unsigned int xt = (unsigned int)primitives(l,3), yt = (unsigned int)primitives(l,4);
  55757. const float
  55758. rt = color.atXY(xt,yt,0)/255.f,
  55759. gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
  55760. bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
  55761. std::fprintf(nfile,"3 %u %u %u %f %f %f\n",
  55762. (unsigned int)primitives(l,0),(unsigned int)primitives(l,2),
  55763. (unsigned int)primitives(l,1),rt,gt,bt);
  55764. } break;
  55765. case 12 : {
  55766. const unsigned int xt = (unsigned int)primitives(l,4), yt = (unsigned int)primitives(l,5);
  55767. const float
  55768. rt = color.atXY(xt,yt,0)/255.f,
  55769. gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
  55770. bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
  55771. std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n",
  55772. (unsigned int)primitives(l,0),(unsigned int)primitives(l,3),
  55773. (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),rt,gt,bt);
  55774. } break;
  55775. }
  55776. }
  55777. if (!file) cimg::fclose(nfile);
  55778. return *this;
  55779. }
  55780. //! Save volumetric image as a video (using the OpenCV library when available).
  55781. /**
  55782. \param filename Filename to write data to.
  55783. \param fps Number of frames per second.
  55784. \param codec Type of compression (See http://www.fourcc.org/codecs.php to see available codecs).
  55785. \param keep_open Tells if the video writer associated to the specified filename
  55786. must be kept open or not (to allow frames to be added in the same file afterwards).
  55787. **/
  55788. const CImg<T>& save_video(const char *const filename, const unsigned int fps=25,
  55789. const char *codec=0, const bool keep_open=false) const {
  55790. if (is_empty()) { CImgList<T>().save_video(filename,fps,codec,keep_open); return *this; }
  55791. CImgList<T> list;
  55792. get_split('z').move_to(list);
  55793. list.save_video(filename,fps,codec,keep_open);
  55794. return *this;
  55795. }
  55796. //! Save volumetric image as a video, using ffmpeg external binary.
  55797. /**
  55798. \param filename Filename, as a C-string.
  55799. \param fps Video framerate.
  55800. \param codec Video codec, as a C-string.
  55801. \param bitrate Video bitrate.
  55802. \note
  55803. - Each slice of the instance image is considered to be a single frame of the output video file.
  55804. - This method uses \c ffmpeg, an external executable binary provided by
  55805. <a href="http://www.ffmpeg.org">FFmpeg</a>.
  55806. It must be installed for the method to succeed.
  55807. **/
  55808. const CImg<T>& save_ffmpeg_external(const char *const filename, const unsigned int fps=25,
  55809. const char *const codec=0, const unsigned int bitrate=2048) const {
  55810. if (!filename)
  55811. throw CImgArgumentException(_cimg_instance
  55812. "save_ffmpeg_external(): Specified filename is (null).",
  55813. cimg_instance);
  55814. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55815. CImgList<T> list;
  55816. get_split('z').move_to(list);
  55817. list.save_ffmpeg_external(filename,fps,codec,bitrate);
  55818. return *this;
  55819. }
  55820. //! Save image using gzip external binary.
  55821. /**
  55822. \param filename Filename, as a C-string.
  55823. \note This method uses \c gzip, an external executable binary provided by
  55824. <a href="//http://www.gzip.org">gzip</a>.
  55825. It must be installed for the method to succeed.
  55826. **/
  55827. const CImg<T>& save_gzip_external(const char *const filename) const {
  55828. if (!filename)
  55829. throw CImgArgumentException(_cimg_instance
  55830. "save_gzip_external(): Specified filename is (null).",
  55831. cimg_instance);
  55832. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55833. CImg<charT> command(1024), filename_tmp(256), body(256);
  55834. const char
  55835. *ext = cimg::split_filename(filename,body),
  55836. *ext2 = cimg::split_filename(body,0);
  55837. std::FILE *file;
  55838. do {
  55839. if (!cimg::strcasecmp(ext,"gz")) {
  55840. if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  55841. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
  55842. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
  55843. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  55844. } else {
  55845. if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  55846. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
  55847. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
  55848. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  55849. }
  55850. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  55851. } while (file);
  55852. save(filename_tmp);
  55853. cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
  55854. cimg::gzip_path(),
  55855. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  55856. CImg<charT>::string(filename)._system_strescape().data());
  55857. cimg::system(command, cimg::gzip_path());
  55858. file = cimg::std_fopen(filename,"rb");
  55859. if (!file)
  55860. throw CImgIOException(_cimg_instance
  55861. "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.",
  55862. cimg_instance,
  55863. filename);
  55864. else cimg::fclose(file);
  55865. std::remove(filename_tmp);
  55866. return *this;
  55867. }
  55868. //! Save image using GraphicsMagick's external binary.
  55869. /**
  55870. \param filename Filename, as a C-string.
  55871. \param quality Image quality (expressed in percent), when the file format supports it.
  55872. \note This method uses \c gm, an external executable binary provided by
  55873. <a href="http://www.graphicsmagick.org">GraphicsMagick</a>.
  55874. It must be installed for the method to succeed.
  55875. **/
  55876. const CImg<T>& save_graphicsmagick_external(const char *const filename, const unsigned int quality=100) const {
  55877. if (!filename)
  55878. throw CImgArgumentException(_cimg_instance
  55879. "save_graphicsmagick_external(): Specified filename is (null).",
  55880. cimg_instance);
  55881. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55882. if (_depth>1)
  55883. cimg::warn(_cimg_instance
  55884. "save_other(): File '%s', saving a volumetric image with an external call to "
  55885. "GraphicsMagick only writes the first image slice.",
  55886. cimg_instance,filename);
  55887. #ifdef cimg_use_png
  55888. #define _cimg_sge_extension1 "png"
  55889. #define _cimg_sge_extension2 "png"
  55890. #else
  55891. #define _cimg_sge_extension1 "pgm"
  55892. #define _cimg_sge_extension2 "ppm"
  55893. #endif
  55894. CImg<charT> command(1024), filename_tmp(256);
  55895. std::FILE *file;
  55896. do {
  55897. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  55898. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),
  55899. _spectrum==1?_cimg_sge_extension1:_cimg_sge_extension2);
  55900. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  55901. } while (file);
  55902. #ifdef cimg_use_png
  55903. save_png(filename_tmp);
  55904. #else
  55905. save_pnm(filename_tmp);
  55906. #endif
  55907. cimg_snprintf(command,command._width,"\"%s\" convert -quality %u \"%s\" \"%s\"",
  55908. cimg::graphicsmagick_path(),quality,
  55909. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  55910. CImg<charT>::string(filename)._system_strescape().data());
  55911. cimg::system(command, cimg::graphicsmagick_path());
  55912. file = cimg::std_fopen(filename,"rb");
  55913. if (!file)
  55914. throw CImgIOException(_cimg_instance
  55915. "save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.",
  55916. cimg_instance,
  55917. filename);
  55918. if (file) cimg::fclose(file);
  55919. std::remove(filename_tmp);
  55920. return *this;
  55921. }
  55922. //! Save image using ImageMagick's external binary.
  55923. /**
  55924. \param filename Filename, as a C-string.
  55925. \param quality Image quality (expressed in percent), when the file format supports it.
  55926. \note This method uses \c convert, an external executable binary provided by
  55927. <a href="http://www.imagemagick.org">ImageMagick</a>.
  55928. It must be installed for the method to succeed.
  55929. **/
  55930. const CImg<T>& save_imagemagick_external(const char *const filename, const unsigned int quality=100) const {
  55931. if (!filename)
  55932. throw CImgArgumentException(_cimg_instance
  55933. "save_imagemagick_external(): Specified filename is (null).",
  55934. cimg_instance);
  55935. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55936. if (_depth>1)
  55937. cimg::warn(_cimg_instance
  55938. "save_other(): File '%s', saving a volumetric image with an external call to "
  55939. "ImageMagick only writes the first image slice.",
  55940. cimg_instance,filename);
  55941. #ifdef cimg_use_png
  55942. #define _cimg_sie_extension1 "png"
  55943. #define _cimg_sie_extension2 "png"
  55944. #else
  55945. #define _cimg_sie_extension1 "pgm"
  55946. #define _cimg_sie_extension2 "ppm"
  55947. #endif
  55948. CImg<charT> command(1024), filename_tmp(256);
  55949. std::FILE *file;
  55950. do {
  55951. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",cimg::temporary_path(),
  55952. cimg_file_separator,cimg::filenamerand(),_spectrum==1?_cimg_sie_extension1:_cimg_sie_extension2);
  55953. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  55954. } while (file);
  55955. #ifdef cimg_use_png
  55956. save_png(filename_tmp);
  55957. #else
  55958. save_pnm(filename_tmp);
  55959. #endif
  55960. cimg_snprintf(command,command._width,"\"%s\" -quality %u \"%s\" \"%s\"",
  55961. cimg::imagemagick_path(),quality,
  55962. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  55963. CImg<charT>::string(filename)._system_strescape().data());
  55964. cimg::system(command, cimg::imagemagick_path());
  55965. file = cimg::std_fopen(filename,"rb");
  55966. if (!file)
  55967. throw CImgIOException(_cimg_instance
  55968. "save_imagemagick_external(): Failed to save file '%s' with "
  55969. "external command 'magick/convert'.",
  55970. cimg_instance,
  55971. filename);
  55972. if (file) cimg::fclose(file);
  55973. std::remove(filename_tmp);
  55974. return *this;
  55975. }
  55976. //! Save image as a Dicom file.
  55977. /**
  55978. \param filename Filename, as a C-string.
  55979. \note This method uses \c medcon, an external executable binary provided by
  55980. <a href="http://xmedcon.sourceforge.net">(X)Medcon</a>.
  55981. It must be installed for the method to succeed.
  55982. **/
  55983. const CImg<T>& save_medcon_external(const char *const filename) const {
  55984. if (!filename)
  55985. throw CImgArgumentException(_cimg_instance
  55986. "save_medcon_external(): Specified filename is (null).",
  55987. cimg_instance);
  55988. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  55989. CImg<charT> command(1024), filename_tmp(256), body(256);
  55990. std::FILE *file;
  55991. do {
  55992. cimg_snprintf(filename_tmp,filename_tmp._width,"%s.hdr",cimg::filenamerand());
  55993. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  55994. } while (file);
  55995. save_analyze(filename_tmp);
  55996. cimg_snprintf(command,command._width,"\"%s\" -w -c dicom -o \"%s\" -f \"%s\"",
  55997. cimg::medcon_path(),
  55998. CImg<charT>::string(filename)._system_strescape().data(),
  55999. CImg<charT>::string(filename_tmp)._system_strescape().data());
  56000. cimg::system(command, cimg::medcon_path());
  56001. std::remove(filename_tmp);
  56002. cimg::split_filename(filename_tmp,body);
  56003. cimg_snprintf(filename_tmp,filename_tmp._width,"%s.img",body._data);
  56004. std::remove(filename_tmp);
  56005. file = cimg::std_fopen(filename,"rb");
  56006. if (!file) {
  56007. cimg_snprintf(command,command._width,"m000-%s",filename);
  56008. file = cimg::std_fopen(command,"rb");
  56009. if (!file) {
  56010. cimg::fclose(cimg::fopen(filename,"r"));
  56011. throw CImgIOException(_cimg_instance
  56012. "save_medcon_external(): Failed to save file '%s' with external command 'medcon'.",
  56013. cimg_instance,
  56014. filename);
  56015. }
  56016. }
  56017. cimg::fclose(file);
  56018. std::rename(command,filename);
  56019. return *this;
  56020. }
  56021. // Save image for non natively supported formats.
  56022. /**
  56023. \param filename Filename, as a C-string.
  56024. \param quality Image quality (expressed in percent), when the file format supports it.
  56025. \note
  56026. - The filename extension tells about the desired file format.
  56027. - This method tries to save the instance image as a file, using external tools from
  56028. <a href="http://www.imagemagick.org">ImageMagick</a> or
  56029. <a href="http://www.graphicsmagick.org">GraphicsMagick</a>.
  56030. At least one of these tool must be installed for the method to succeed.
  56031. - It is recommended to use the generic method save(const char*, int) const instead,
  56032. as it can handle some file formats natively.
  56033. **/
  56034. const CImg<T>& save_other(const char *const filename, const unsigned int quality=100) const {
  56035. if (!filename)
  56036. throw CImgArgumentException(_cimg_instance
  56037. "save_other(): Specified filename is (null).",
  56038. cimg_instance);
  56039. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  56040. if (_depth>1)
  56041. cimg::warn(_cimg_instance
  56042. "save_other(): File '%s', saving a volumetric image with an external call to "
  56043. "ImageMagick or GraphicsMagick only writes the first image slice.",
  56044. cimg_instance,filename);
  56045. const unsigned int omode = cimg::exception_mode();
  56046. bool is_saved = true;
  56047. cimg::exception_mode(0);
  56048. try { save_magick(filename); }
  56049. catch (CImgException&) {
  56050. try { save_imagemagick_external(filename,quality); }
  56051. catch (CImgException&) {
  56052. try { save_graphicsmagick_external(filename,quality); }
  56053. catch (CImgException&) {
  56054. is_saved = false;
  56055. }
  56056. }
  56057. }
  56058. cimg::exception_mode(omode);
  56059. if (!is_saved)
  56060. throw CImgIOException(_cimg_instance
  56061. "save_other(): Failed to save file '%s'. Format is not natively supported, "
  56062. "and no external commands succeeded.",
  56063. cimg_instance,
  56064. filename);
  56065. return *this;
  56066. }
  56067. //! Serialize a CImg<T> instance into a raw CImg<unsigned char> buffer.
  56068. /**
  56069. \param is_compressed tells if zlib compression must be used for serialization
  56070. (this requires 'cimg_use_zlib' been enabled).
  56071. **/
  56072. CImg<ucharT> get_serialize(const bool is_compressed=false) const {
  56073. return CImgList<T>(*this,true).get_serialize(is_compressed);
  56074. }
  56075. // [internal] Return a 40x38 color logo of a 'danger' item.
  56076. static CImg<T> _logo40x38() {
  56077. CImg<T> res(40,38,1,3);
  56078. const unsigned char *ptrs = cimg::logo40x38;
  56079. T *ptr1 = res.data(0,0,0,0), *ptr2 = res.data(0,0,0,1), *ptr3 = res.data(0,0,0,2);
  56080. for (ulongT off = 0; off<(ulongT)res._width*res._height;) {
  56081. const unsigned char n = *(ptrs++), r = *(ptrs++), g = *(ptrs++), b = *(ptrs++);
  56082. for (unsigned int l = 0; l<n; ++off, ++l) { *(ptr1++) = (T)r; *(ptr2++) = (T)g; *(ptr3++) = (T)b; }
  56083. }
  56084. return res;
  56085. }
  56086. //@}
  56087. }; // struct CImg { ...
  56088. /*
  56089. #-----------------------------------------
  56090. #
  56091. #
  56092. #
  56093. # Definition of the CImgList<T> structure
  56094. #
  56095. #
  56096. #
  56097. #------------------------------------------
  56098. */
  56099. //! Represent a list of images CImg<T>.
  56100. template<typename T>
  56101. struct CImgList {
  56102. unsigned int _width, _allocated_width;
  56103. CImg<T> *_data;
  56104. //! Simple iterator type, to loop through each image of a list.
  56105. /**
  56106. \note
  56107. - The \c CImgList<T>::iterator type is defined as a <tt>CImg<T>*</tt>.
  56108. - You may use it like this:
  56109. \code
  56110. CImgList<> list; // Assuming this image list is not empty
  56111. for (CImgList<>::iterator it = list.begin(); it<list.end(); ++it) (*it).mirror('x');
  56112. \endcode
  56113. - Using the loop macro \c cimglist_for is another (more concise) alternative:
  56114. \code
  56115. cimglist_for(list,l) list[l].mirror('x');
  56116. \endcode
  56117. **/
  56118. typedef CImg<T>* iterator;
  56119. //! Simple const iterator type, to loop through each image of a \c const list instance.
  56120. /**
  56121. \note
  56122. - The \c CImgList<T>::const_iterator type is defined to be a <tt>const CImg<T>*</tt>.
  56123. - Similar to CImgList<T>::iterator, but for constant list instances.
  56124. **/
  56125. typedef const CImg<T>* const_iterator;
  56126. //! Pixel value type.
  56127. /**
  56128. Refer to the pixels value type of the images in the list.
  56129. \note
  56130. - The \c CImgList<T>::value_type type of a \c CImgList<T> is defined to be a \c T.
  56131. It is then similar to CImg<T>::value_type.
  56132. - \c CImgList<T>::value_type is actually not used in %CImg methods. It has been mainly defined for
  56133. compatibility with STL naming conventions.
  56134. **/
  56135. typedef T value_type;
  56136. // Define common types related to template type T.
  56137. typedef typename cimg::superset<T,bool>::type Tbool;
  56138. typedef typename cimg::superset<T,unsigned char>::type Tuchar;
  56139. typedef typename cimg::superset<T,char>::type Tchar;
  56140. typedef typename cimg::superset<T,unsigned short>::type Tushort;
  56141. typedef typename cimg::superset<T,short>::type Tshort;
  56142. typedef typename cimg::superset<T,unsigned int>::type Tuint;
  56143. typedef typename cimg::superset<T,int>::type Tint;
  56144. typedef typename cimg::superset<T,cimg_ulong>::type Tulong;
  56145. typedef typename cimg::superset<T,cimg_long>::type Tlong;
  56146. typedef typename cimg::superset<T,float>::type Tfloat;
  56147. typedef typename cimg::superset<T,double>::type Tdouble;
  56148. typedef typename cimg::last<T,bool>::type boolT;
  56149. typedef typename cimg::last<T,unsigned char>::type ucharT;
  56150. typedef typename cimg::last<T,char>::type charT;
  56151. typedef typename cimg::last<T,unsigned short>::type ushortT;
  56152. typedef typename cimg::last<T,short>::type shortT;
  56153. typedef typename cimg::last<T,unsigned int>::type uintT;
  56154. typedef typename cimg::last<T,int>::type intT;
  56155. typedef typename cimg::last<T,cimg_ulong>::type ulongT;
  56156. typedef typename cimg::last<T,cimg_long>::type longT;
  56157. typedef typename cimg::last<T,cimg_uint64>::type uint64T;
  56158. typedef typename cimg::last<T,cimg_int64>::type int64T;
  56159. typedef typename cimg::last<T,float>::type floatT;
  56160. typedef typename cimg::last<T,double>::type doubleT;
  56161. //@}
  56162. //---------------------------
  56163. //
  56164. //! \name Plugins
  56165. //@{
  56166. //---------------------------
  56167. #ifdef cimglist_plugin
  56168. #include cimglist_plugin
  56169. #endif
  56170. #ifdef cimglist_plugin1
  56171. #include cimglist_plugin1
  56172. #endif
  56173. #ifdef cimglist_plugin2
  56174. #include cimglist_plugin2
  56175. #endif
  56176. #ifdef cimglist_plugin3
  56177. #include cimglist_plugin3
  56178. #endif
  56179. #ifdef cimglist_plugin4
  56180. #include cimglist_plugin4
  56181. #endif
  56182. #ifdef cimglist_plugin5
  56183. #include cimglist_plugin5
  56184. #endif
  56185. #ifdef cimglist_plugin6
  56186. #include cimglist_plugin6
  56187. #endif
  56188. #ifdef cimglist_plugin7
  56189. #include cimglist_plugin7
  56190. #endif
  56191. #ifdef cimglist_plugin8
  56192. #include cimglist_plugin8
  56193. #endif
  56194. //@}
  56195. //--------------------------------------------------------
  56196. //
  56197. //! \name Constructors / Destructor / Instance Management
  56198. //@{
  56199. //--------------------------------------------------------
  56200. //! Destructor.
  56201. /**
  56202. Destroy current list instance.
  56203. \note
  56204. - Any allocated buffer is deallocated.
  56205. - Destroying an empty list does nothing actually.
  56206. **/
  56207. ~CImgList() {
  56208. delete[] _data;
  56209. }
  56210. //! Default constructor.
  56211. /**
  56212. Construct a new empty list instance.
  56213. \note
  56214. - An empty list has no pixel data and its dimension width() is set to \c 0, as well as its
  56215. image buffer pointer data().
  56216. - An empty list may be reassigned afterwards, with the family of the assign() methods.
  56217. In all cases, the type of pixels stays \c T.
  56218. **/
  56219. CImgList():
  56220. _width(0),_allocated_width(0),_data(0) {}
  56221. //! Construct list containing empty images.
  56222. /**
  56223. \param n Number of empty images.
  56224. \note Useful when you know by advance the number of images you want to manage, as
  56225. it will allocate the right amount of memory for the list, without needs for reallocation
  56226. (that may occur when starting from an empty list and inserting several images in it).
  56227. **/
  56228. explicit CImgList(const unsigned int n):_width(n) {
  56229. if (n) _data = new CImg<T>[_allocated_width = std::max(16U,(unsigned int)cimg::nearest_pow2(n))];
  56230. else { _allocated_width = 0; _data = 0; }
  56231. }
  56232. //! Construct list containing images of specified size.
  56233. /**
  56234. \param n Number of images.
  56235. \param width Width of images.
  56236. \param height Height of images.
  56237. \param depth Depth of images.
  56238. \param spectrum Number of channels of images.
  56239. \note Pixel values are not initialized and may probably contain garbage.
  56240. **/
  56241. CImgList(const unsigned int n, const unsigned int width, const unsigned int height=1,
  56242. const unsigned int depth=1, const unsigned int spectrum=1):
  56243. _width(0),_allocated_width(0),_data(0) {
  56244. assign(n);
  56245. cimglist_apply(*this,assign)(width,height,depth,spectrum);
  56246. }
  56247. //! Construct list containing images of specified size, and initialize pixel values.
  56248. /**
  56249. \param n Number of images.
  56250. \param width Width of images.
  56251. \param height Height of images.
  56252. \param depth Depth of images.
  56253. \param spectrum Number of channels of images.
  56254. \param val Initialization value for images pixels.
  56255. **/
  56256. CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
  56257. const unsigned int depth, const unsigned int spectrum, const T& val):
  56258. _width(0),_allocated_width(0),_data(0) {
  56259. assign(n);
  56260. cimglist_apply(*this,assign)(width,height,depth,spectrum,val);
  56261. }
  56262. //! Construct list containing images of specified size, and initialize pixel values from a sequence of integers.
  56263. /**
  56264. \param n Number of images.
  56265. \param width Width of images.
  56266. \param height Height of images.
  56267. \param depth Depth of images.
  56268. \param spectrum Number of channels of images.
  56269. \param val0 First value of the initializing integers sequence.
  56270. \param val1 Second value of the initializing integers sequence.
  56271. \warning You must specify at least <tt>width*height*depth*spectrum</tt> values in your argument list,
  56272. or you will probably segfault.
  56273. **/
  56274. CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
  56275. const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...):
  56276. _width(0),_allocated_width(0),_data(0) {
  56277. #define _CImgList_stdarg(t) { \
  56278. assign(n,width,height,depth,spectrum); \
  56279. const ulongT siz = (ulongT)width*height*depth*spectrum, nsiz = siz*n; \
  56280. T *ptrd = _data->_data; \
  56281. va_list ap; \
  56282. va_start(ap,val1); \
  56283. for (ulongT l = 0, s = 0, i = 0; i<nsiz; ++i) { \
  56284. *(ptrd++) = (T)(i==0?val0:(i==1?val1:va_arg(ap,t))); \
  56285. if ((++s)==siz) { ptrd = _data[++l]._data; s = 0; } \
  56286. } \
  56287. va_end(ap); \
  56288. }
  56289. _CImgList_stdarg(int);
  56290. }
  56291. //! Construct list containing images of specified size, and initialize pixel values from a sequence of doubles.
  56292. /**
  56293. \param n Number of images.
  56294. \param width Width of images.
  56295. \param height Height of images.
  56296. \param depth Depth of images.
  56297. \param spectrum Number of channels of images.
  56298. \param val0 First value of the initializing doubles sequence.
  56299. \param val1 Second value of the initializing doubles sequence.
  56300. \warning You must specify at least <tt>width*height*depth*spectrum</tt> values in your argument list,
  56301. or you will probably segfault.
  56302. **/
  56303. CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
  56304. const unsigned int depth, const unsigned int spectrum, const double val0, const double val1, ...):
  56305. _width(0),_allocated_width(0),_data(0) {
  56306. _CImgList_stdarg(double);
  56307. }
  56308. //! Construct list containing copies of an input image.
  56309. /**
  56310. \param n Number of images.
  56311. \param img Input image to copy in the constructed list.
  56312. \param is_shared Tells if the elements of the list are shared or non-shared copies of \c img.
  56313. **/
  56314. template<typename t>
  56315. CImgList(const unsigned int n, const CImg<t>& img, const bool is_shared=false):
  56316. _width(0),_allocated_width(0),_data(0) {
  56317. assign(n);
  56318. cimglist_apply(*this,assign)(img,is_shared);
  56319. }
  56320. //! Construct list from one image.
  56321. /**
  56322. \param img Input image to copy in the constructed list.
  56323. \param is_shared Tells if the element of the list is a shared or non-shared copy of \c img.
  56324. **/
  56325. template<typename t>
  56326. explicit CImgList(const CImg<t>& img, const bool is_shared=false):
  56327. _width(0),_allocated_width(0),_data(0) {
  56328. assign(1);
  56329. _data[0].assign(img,is_shared);
  56330. }
  56331. //! Construct list from two images.
  56332. /**
  56333. \param img1 First input image to copy in the constructed list.
  56334. \param img2 Second input image to copy in the constructed list.
  56335. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56336. **/
  56337. template<typename t1, typename t2>
  56338. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const bool is_shared=false):
  56339. _width(0),_allocated_width(0),_data(0) {
  56340. assign(2);
  56341. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared);
  56342. }
  56343. //! Construct list from three images.
  56344. /**
  56345. \param img1 First input image to copy in the constructed list.
  56346. \param img2 Second input image to copy in the constructed list.
  56347. \param img3 Third input image to copy in the constructed list.
  56348. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56349. **/
  56350. template<typename t1, typename t2, typename t3>
  56351. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const bool is_shared=false):
  56352. _width(0),_allocated_width(0),_data(0) {
  56353. assign(3);
  56354. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56355. }
  56356. //! Construct list from four images.
  56357. /**
  56358. \param img1 First input image to copy in the constructed list.
  56359. \param img2 Second input image to copy in the constructed list.
  56360. \param img3 Third input image to copy in the constructed list.
  56361. \param img4 Fourth input image to copy in the constructed list.
  56362. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56363. **/
  56364. template<typename t1, typename t2, typename t3, typename t4>
  56365. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56366. const bool is_shared=false):
  56367. _width(0),_allocated_width(0),_data(0) {
  56368. assign(4);
  56369. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56370. _data[3].assign(img4,is_shared);
  56371. }
  56372. //! Construct list from five images.
  56373. /**
  56374. \param img1 First input image to copy in the constructed list.
  56375. \param img2 Second input image to copy in the constructed list.
  56376. \param img3 Third input image to copy in the constructed list.
  56377. \param img4 Fourth input image to copy in the constructed list.
  56378. \param img5 Fifth input image to copy in the constructed list.
  56379. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56380. **/
  56381. template<typename t1, typename t2, typename t3, typename t4, typename t5>
  56382. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56383. const CImg<t5>& img5, const bool is_shared=false):
  56384. _width(0),_allocated_width(0),_data(0) {
  56385. assign(5);
  56386. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56387. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared);
  56388. }
  56389. //! Construct list from six images.
  56390. /**
  56391. \param img1 First input image to copy in the constructed list.
  56392. \param img2 Second input image to copy in the constructed list.
  56393. \param img3 Third input image to copy in the constructed list.
  56394. \param img4 Fourth input image to copy in the constructed list.
  56395. \param img5 Fifth input image to copy in the constructed list.
  56396. \param img6 Sixth input image to copy in the constructed list.
  56397. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56398. **/
  56399. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6>
  56400. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56401. const CImg<t5>& img5, const CImg<t6>& img6, const bool is_shared=false):
  56402. _width(0),_allocated_width(0),_data(0) {
  56403. assign(6);
  56404. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56405. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56406. }
  56407. //! Construct list from seven images.
  56408. /**
  56409. \param img1 First input image to copy in the constructed list.
  56410. \param img2 Second input image to copy in the constructed list.
  56411. \param img3 Third input image to copy in the constructed list.
  56412. \param img4 Fourth input image to copy in the constructed list.
  56413. \param img5 Fifth input image to copy in the constructed list.
  56414. \param img6 Sixth input image to copy in the constructed list.
  56415. \param img7 Seventh input image to copy in the constructed list.
  56416. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56417. **/
  56418. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7>
  56419. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56420. const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const bool is_shared=false):
  56421. _width(0),_allocated_width(0),_data(0) {
  56422. assign(7);
  56423. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56424. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56425. _data[6].assign(img7,is_shared);
  56426. }
  56427. //! Construct list from eight images.
  56428. /**
  56429. \param img1 First input image to copy in the constructed list.
  56430. \param img2 Second input image to copy in the constructed list.
  56431. \param img3 Third input image to copy in the constructed list.
  56432. \param img4 Fourth input image to copy in the constructed list.
  56433. \param img5 Fifth input image to copy in the constructed list.
  56434. \param img6 Sixth input image to copy in the constructed list.
  56435. \param img7 Seventh input image to copy in the constructed list.
  56436. \param img8 Eighth input image to copy in the constructed list.
  56437. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56438. **/
  56439. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7, typename t8>
  56440. CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56441. const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const CImg<t8>& img8,
  56442. const bool is_shared=false):
  56443. _width(0),_allocated_width(0),_data(0) {
  56444. assign(8);
  56445. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56446. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56447. _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared);
  56448. }
  56449. //! Construct list copy.
  56450. /**
  56451. \param list Input list to copy.
  56452. \note The shared state of each element of the constructed list is kept the same as in \c list.
  56453. **/
  56454. template<typename t>
  56455. CImgList(const CImgList<t>& list):_width(0),_allocated_width(0),_data(0) {
  56456. assign(list._width);
  56457. cimglist_for(*this,l) _data[l].assign(list[l],false);
  56458. }
  56459. //! Construct list copy \specialization.
  56460. CImgList(const CImgList<T>& list):_width(0),_allocated_width(0),_data(0) {
  56461. assign(list._width);
  56462. cimglist_for(*this,l) _data[l].assign(list[l],list[l]._is_shared);
  56463. }
  56464. //! Construct list copy, and force the shared state of the list elements.
  56465. /**
  56466. \param list Input list to copy.
  56467. \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
  56468. **/
  56469. template<typename t>
  56470. CImgList(const CImgList<t>& list, const bool is_shared):_width(0),_allocated_width(0),_data(0) {
  56471. assign(list._width);
  56472. cimglist_for(*this,l) _data[l].assign(list[l],is_shared);
  56473. }
  56474. //! Construct list by reading the content of a file.
  56475. /**
  56476. \param filename Filename, as a C-string.
  56477. **/
  56478. explicit CImgList(const char *const filename):_width(0),_allocated_width(0),_data(0) {
  56479. assign(filename);
  56480. }
  56481. //! Construct list from the content of a display window.
  56482. /**
  56483. \param disp Display window to get content from.
  56484. \note Constructed list contains a single image only.
  56485. **/
  56486. explicit CImgList(const CImgDisplay& disp):_width(0),_allocated_width(0),_data(0) {
  56487. assign(disp);
  56488. }
  56489. //! Return a list with elements being shared copies of images in the list instance.
  56490. /**
  56491. \note <tt>list2 = list1.get_shared()</tt> is equivalent to <tt>list2.assign(list1,true)</tt>.
  56492. **/
  56493. CImgList<T> get_shared() {
  56494. CImgList<T> res(_width);
  56495. cimglist_for(*this,l) res[l].assign(_data[l],true);
  56496. return res;
  56497. }
  56498. //! Return a list with elements being shared copies of images in the list instance \const.
  56499. const CImgList<T> get_shared() const {
  56500. CImgList<T> res(_width);
  56501. cimglist_for(*this,l) res[l].assign(_data[l],true);
  56502. return res;
  56503. }
  56504. //! Destructor \inplace.
  56505. /**
  56506. \see CImgList().
  56507. **/
  56508. CImgList<T>& assign() {
  56509. delete[] _data;
  56510. _width = _allocated_width = 0;
  56511. _data = 0;
  56512. return *this;
  56513. }
  56514. //! Destructor \inplace.
  56515. /**
  56516. Equivalent to assign().
  56517. \note Only here for compatibility with STL naming conventions.
  56518. **/
  56519. CImgList<T>& clear() {
  56520. return assign();
  56521. }
  56522. //! Construct list containing empty images \inplace.
  56523. /**
  56524. \see CImgList(unsigned int).
  56525. **/
  56526. CImgList<T>& assign(const unsigned int n) {
  56527. if (!n) return assign();
  56528. if (_allocated_width<n || _allocated_width>(n<<2)) {
  56529. delete[] _data;
  56530. _data = new CImg<T>[_allocated_width = std::max(16U,(unsigned int)cimg::nearest_pow2(n))];
  56531. }
  56532. _width = n;
  56533. return *this;
  56534. }
  56535. //! Construct list containing images of specified size \inplace.
  56536. /**
  56537. \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int).
  56538. **/
  56539. CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height=1,
  56540. const unsigned int depth=1, const unsigned int spectrum=1) {
  56541. assign(n);
  56542. cimglist_apply(*this,assign)(width,height,depth,spectrum);
  56543. return *this;
  56544. }
  56545. //! Construct list containing images of specified size, and initialize pixel values \inplace.
  56546. /**
  56547. \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const T).
  56548. **/
  56549. CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
  56550. const unsigned int depth, const unsigned int spectrum, const T& val) {
  56551. assign(n);
  56552. cimglist_apply(*this,assign)(width,height,depth,spectrum,val);
  56553. return *this;
  56554. }
  56555. //! Construct list with images of specified size, and initialize pixel values from a sequence of integers \inplace.
  56556. /**
  56557. \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const int, const int, ...).
  56558. **/
  56559. CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
  56560. const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...) {
  56561. _CImgList_stdarg(int);
  56562. return *this;
  56563. }
  56564. //! Construct list with images of specified size, and initialize pixel values from a sequence of doubles \inplace.
  56565. /**
  56566. \see CImgList(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,const double,const double,...).
  56567. **/
  56568. CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
  56569. const unsigned int depth, const unsigned int spectrum,
  56570. const double val0, const double val1, ...) {
  56571. _CImgList_stdarg(double);
  56572. return *this;
  56573. }
  56574. //! Construct list containing copies of an input image \inplace.
  56575. /**
  56576. \see CImgList(unsigned int, const CImg<t>&, bool).
  56577. **/
  56578. template<typename t>
  56579. CImgList<T>& assign(const unsigned int n, const CImg<t>& img, const bool is_shared=false) {
  56580. assign(n);
  56581. cimglist_apply(*this,assign)(img,is_shared);
  56582. return *this;
  56583. }
  56584. //! Construct list from one image \inplace.
  56585. /**
  56586. \see CImgList(const CImg<t>&, bool).
  56587. **/
  56588. template<typename t>
  56589. CImgList<T>& assign(const CImg<t>& img, const bool is_shared=false) {
  56590. assign(1);
  56591. _data[0].assign(img,is_shared);
  56592. return *this;
  56593. }
  56594. //! Construct list from two images \inplace.
  56595. /**
  56596. \see CImgList(const CImg<t>&, const CImg<t>&, bool).
  56597. **/
  56598. template<typename t1, typename t2>
  56599. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const bool is_shared=false) {
  56600. assign(2);
  56601. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared);
  56602. return *this;
  56603. }
  56604. //! Construct list from three images \inplace.
  56605. /**
  56606. \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
  56607. **/
  56608. template<typename t1, typename t2, typename t3>
  56609. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const bool is_shared=false) {
  56610. assign(3);
  56611. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56612. return *this;
  56613. }
  56614. //! Construct list from four images \inplace.
  56615. /**
  56616. \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
  56617. **/
  56618. template<typename t1, typename t2, typename t3, typename t4>
  56619. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56620. const bool is_shared=false) {
  56621. assign(4);
  56622. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56623. _data[3].assign(img4,is_shared);
  56624. return *this;
  56625. }
  56626. //! Construct list from five images \inplace.
  56627. /**
  56628. \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
  56629. **/
  56630. template<typename t1, typename t2, typename t3, typename t4, typename t5>
  56631. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56632. const CImg<t5>& img5, const bool is_shared=false) {
  56633. assign(5);
  56634. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56635. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared);
  56636. return *this;
  56637. }
  56638. //! Construct list from six images \inplace.
  56639. /**
  56640. \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&, bool).
  56641. **/
  56642. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6>
  56643. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56644. const CImg<t5>& img5, const CImg<t6>& img6, const bool is_shared=false) {
  56645. assign(6);
  56646. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56647. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56648. return *this;
  56649. }
  56650. //! Construct list from seven images \inplace.
  56651. /**
  56652. \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,
  56653. const CImg<t>&, bool).
  56654. **/
  56655. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7>
  56656. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56657. const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const bool is_shared=false) {
  56658. assign(7);
  56659. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56660. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56661. _data[6].assign(img7,is_shared);
  56662. return *this;
  56663. }
  56664. //! Construct list from eight images \inplace.
  56665. /**
  56666. \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,
  56667. const CImg<t>&, const CImg<t>&, bool).
  56668. **/
  56669. template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7, typename t8>
  56670. CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
  56671. const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const CImg<t8>& img8,
  56672. const bool is_shared=false) {
  56673. assign(8);
  56674. _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
  56675. _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
  56676. _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared);
  56677. return *this;
  56678. }
  56679. //! Construct list as a copy of an existing list and force the shared state of the list elements \inplace.
  56680. /**
  56681. \see CImgList(const CImgList<t>&, bool is_shared).
  56682. **/
  56683. template<typename t>
  56684. CImgList<T>& assign(const CImgList<t>& list, const bool is_shared=false) {
  56685. cimg::unused(is_shared);
  56686. assign(list._width);
  56687. cimglist_for(*this,l) _data[l].assign(list[l],false);
  56688. return *this;
  56689. }
  56690. //! Construct list as a copy of an existing list and force shared state of elements \inplace \specialization.
  56691. CImgList<T>& assign(const CImgList<T>& list, const bool is_shared=false) {
  56692. if (this==&list) return *this;
  56693. CImgList<T> res(list._width);
  56694. cimglist_for(res,l) res[l].assign(list[l],is_shared);
  56695. return res.move_to(*this);
  56696. }
  56697. //! Construct list by reading the content of a file \inplace.
  56698. /**
  56699. \see CImgList(const char *const).
  56700. **/
  56701. CImgList<T>& assign(const char *const filename) {
  56702. return load(filename);
  56703. }
  56704. //! Construct list from the content of a display window \inplace.
  56705. /**
  56706. \see CImgList(const CImgDisplay&).
  56707. **/
  56708. CImgList<T>& assign(const CImgDisplay &disp) {
  56709. return assign(CImg<T>(disp));
  56710. }
  56711. //! Transfer the content of the list instance to another list.
  56712. /**
  56713. \param list Destination list.
  56714. \note When returning, the current list instance is empty and the initial content of \c list is destroyed.
  56715. **/
  56716. template<typename t>
  56717. CImgList<t>& move_to(CImgList<t>& list) {
  56718. list.assign(_width);
  56719. bool is_one_shared_element = false;
  56720. cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared;
  56721. if (is_one_shared_element) cimglist_for(*this,l) list[l].assign(_data[l]);
  56722. else cimglist_for(*this,l) _data[l].move_to(list[l]);
  56723. assign();
  56724. return list;
  56725. }
  56726. //! Transfer the content of the list instance at a specified position in another list.
  56727. /**
  56728. \param list Destination list.
  56729. \param pos Index of the insertion in the list.
  56730. \note When returning, the list instance is empty and the initial content of \c list is preserved
  56731. (only images indexes may be modified).
  56732. **/
  56733. template<typename t>
  56734. CImgList<t>& move_to(CImgList<t>& list, const unsigned int pos) {
  56735. if (is_empty()) return list;
  56736. const unsigned int npos = pos>list._width?list._width:pos;
  56737. list.insert(_width,npos);
  56738. bool is_one_shared_element = false;
  56739. cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared;
  56740. if (is_one_shared_element) cimglist_for(*this,l) list[npos + l].assign(_data[l]);
  56741. else cimglist_for(*this,l) _data[l].move_to(list[npos + l]);
  56742. assign();
  56743. return list;
  56744. }
  56745. //! Swap all fields between two list instances.
  56746. /**
  56747. \param list List to swap fields with.
  56748. \note Can be used to exchange the content of two lists in a fast way.
  56749. **/
  56750. CImgList<T>& swap(CImgList<T>& list) {
  56751. cimg::swap(_width,list._width,_allocated_width,list._allocated_width);
  56752. cimg::swap(_data,list._data);
  56753. return list;
  56754. }
  56755. //! Return a reference to an empty list.
  56756. /**
  56757. \note Can be used to define default values in a function taking a CImgList<T> as an argument.
  56758. \code
  56759. void f(const CImgList<char>& list=CImgList<char>::empty());
  56760. \endcode
  56761. **/
  56762. static CImgList<T>& empty() {
  56763. static CImgList<T> _empty;
  56764. return _empty.assign();
  56765. }
  56766. //! Return a reference to an empty list \const.
  56767. static const CImgList<T>& const_empty() {
  56768. static const CImgList<T> _empty;
  56769. return _empty;
  56770. }
  56771. //@}
  56772. //------------------------------------------
  56773. //
  56774. //! \name Overloaded Operators
  56775. //@{
  56776. //------------------------------------------
  56777. //! Return a reference to one image element of the list.
  56778. /**
  56779. \param pos Index of the image element.
  56780. **/
  56781. CImg<T>& operator()(const unsigned int pos) {
  56782. #if cimg_verbosity>=3
  56783. if (pos>=_width) {
  56784. cimg::warn(_cimglist_instance
  56785. "operator(): Invalid image request, at position [%u].",
  56786. cimglist_instance,
  56787. pos);
  56788. return *_data;
  56789. }
  56790. #endif
  56791. return _data[pos];
  56792. }
  56793. //! Return a reference to one image of the list.
  56794. /**
  56795. \param pos Index of the image element.
  56796. **/
  56797. const CImg<T>& operator()(const unsigned int pos) const {
  56798. return const_cast<CImgList<T>*>(this)->operator()(pos);
  56799. }
  56800. //! Return a reference to one pixel value of one image of the list.
  56801. /**
  56802. \param pos Index of the image element.
  56803. \param x X-coordinate of the pixel value.
  56804. \param y Y-coordinate of the pixel value.
  56805. \param z Z-coordinate of the pixel value.
  56806. \param c C-coordinate of the pixel value.
  56807. \note <tt>list(n,x,y,z,c)</tt> is equivalent to <tt>list[n](x,y,z,c)</tt>.
  56808. **/
  56809. T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0,
  56810. const unsigned int z=0, const unsigned int c=0) {
  56811. return (*this)[pos](x,y,z,c);
  56812. }
  56813. //! Return a reference to one pixel value of one image of the list \const.
  56814. const T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0,
  56815. const unsigned int z=0, const unsigned int c=0) const {
  56816. return (*this)[pos](x,y,z,c);
  56817. }
  56818. //! Return pointer to the first image of the list.
  56819. /**
  56820. \note Images in a list are stored as a buffer of \c CImg<T>.
  56821. **/
  56822. operator CImg<T>*() {
  56823. return _data;
  56824. }
  56825. //! Return pointer to the first image of the list \const.
  56826. operator const CImg<T>*() const {
  56827. return _data;
  56828. }
  56829. //! Construct list from one image \inplace.
  56830. /**
  56831. \param img Input image to copy in the constructed list.
  56832. \note <tt>list = img;</tt> is equivalent to <tt>list.assign(img);</tt>.
  56833. **/
  56834. template<typename t>
  56835. CImgList<T>& operator=(const CImg<t>& img) {
  56836. return assign(img);
  56837. }
  56838. //! Construct list from another list.
  56839. /**
  56840. \param list Input list to copy.
  56841. \note <tt>list1 = list2</tt> is equivalent to <tt>list1.assign(list2);</tt>.
  56842. **/
  56843. template<typename t>
  56844. CImgList<T>& operator=(const CImgList<t>& list) {
  56845. return assign(list);
  56846. }
  56847. //! Construct list from another list \specialization.
  56848. CImgList<T>& operator=(const CImgList<T>& list) {
  56849. return assign(list);
  56850. }
  56851. //! Construct list by reading the content of a file \inplace.
  56852. /**
  56853. \see CImgList(const char *const).
  56854. **/
  56855. CImgList<T>& operator=(const char *const filename) {
  56856. return assign(filename);
  56857. }
  56858. //! Construct list from the content of a display window \inplace.
  56859. /**
  56860. \see CImgList(const CImgDisplay&).
  56861. **/
  56862. CImgList<T>& operator=(const CImgDisplay& disp) {
  56863. return assign(disp);
  56864. }
  56865. //! Return a non-shared copy of a list.
  56866. /**
  56867. \note <tt>+list</tt> is equivalent to <tt>CImgList<T>(list,false)</tt>.
  56868. It forces the copy to have non-shared elements.
  56869. **/
  56870. CImgList<T> operator+() const {
  56871. return CImgList<T>(*this,false);
  56872. }
  56873. //! Return a copy of the list instance, where image \c img has been inserted at the end.
  56874. /**
  56875. \param img Image inserted at the end of the instance copy.
  56876. \note Define a convenient way to create temporary lists of images, as in the following code:
  56877. \code
  56878. (img1,img2,img3,img4).display("My four images");
  56879. \endcode
  56880. **/
  56881. template<typename t>
  56882. CImgList<T>& operator,(const CImg<t>& img) {
  56883. return insert(img);
  56884. }
  56885. //! Return a copy of the list instance, where image \c img has been inserted at the end \const.
  56886. template<typename t>
  56887. CImgList<T> operator,(const CImg<t>& img) const {
  56888. return (+*this).insert(img);
  56889. }
  56890. //! Return a copy of the list instance, where all elements of input list \c list have been inserted at the end.
  56891. /**
  56892. \param list List inserted at the end of the instance copy.
  56893. **/
  56894. template<typename t>
  56895. CImgList<T>& operator,(const CImgList<t>& list) {
  56896. return insert(list);
  56897. }
  56898. //! Return a copy of the list instance, where all elements of input \c list have been inserted at the end \const.
  56899. template<typename t>
  56900. CImgList<T>& operator,(const CImgList<t>& list) const {
  56901. return (+*this).insert(list);
  56902. }
  56903. //! Return image corresponding to the appending of all images of the instance list along specified axis.
  56904. /**
  56905. \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  56906. \note <tt>list>'x'</tt> is equivalent to <tt>list.get_append('x')</tt>.
  56907. **/
  56908. CImg<T> operator>(const char axis) const {
  56909. return get_append(axis,0);
  56910. }
  56911. //! Return list corresponding to the splitting of all images of the instance list along specified axis.
  56912. /**
  56913. \param axis Axis used for image splitting.
  56914. \note <tt>list<'x'</tt> is equivalent to <tt>list.get_split('x')</tt>.
  56915. **/
  56916. CImgList<T> operator<(const char axis) const {
  56917. return get_split(axis);
  56918. }
  56919. //@}
  56920. //-------------------------------------
  56921. //
  56922. //! \name Instance Characteristics
  56923. //@{
  56924. //-------------------------------------
  56925. //! Return the type of image pixel values as a C string.
  56926. /**
  56927. Return a \c char* string containing the usual type name of the image pixel values
  56928. (i.e. a stringified version of the template parameter \c T).
  56929. \note
  56930. - The returned string may contain spaces (as in \c "unsigned char").
  56931. - If the pixel type \c T does not correspond to a registered type, the string <tt>"unknown"</tt> is returned.
  56932. **/
  56933. static const char* pixel_type() {
  56934. return cimg::type<T>::string();
  56935. }
  56936. //! Return the size of the list, i.e. the number of images contained in it.
  56937. /**
  56938. \note Similar to size() but returns result as a (signed) integer.
  56939. **/
  56940. int width() const {
  56941. return (int)_width;
  56942. }
  56943. //! Return the size of the list, i.e. the number of images contained in it.
  56944. /**
  56945. \note Similar to width() but returns result as an unsigned integer.
  56946. **/
  56947. unsigned int size() const {
  56948. return _width;
  56949. }
  56950. //! Return pointer to the first image of the list.
  56951. /**
  56952. \note Images in a list are stored as a buffer of \c CImg<T>.
  56953. **/
  56954. CImg<T> *data() {
  56955. return _data;
  56956. }
  56957. //! Return pointer to the first image of the list \const.
  56958. const CImg<T> *data() const {
  56959. return _data;
  56960. }
  56961. //! Return pointer to the pos-th image of the list.
  56962. /**
  56963. \param pos Index of the image element to access.
  56964. \note <tt>list.data(n);</tt> is equivalent to <tt>list.data + n;</tt>.
  56965. **/
  56966. #if cimg_verbosity>=3
  56967. CImg<T> *data(const unsigned int pos) {
  56968. if (pos>=size())
  56969. cimg::warn(_cimglist_instance
  56970. "data(): Invalid pointer request, at position [%u].",
  56971. cimglist_instance,
  56972. pos);
  56973. return _data + pos;
  56974. }
  56975. const CImg<T> *data(const unsigned int l) const {
  56976. return const_cast<CImgList<T>*>(this)->data(l);
  56977. }
  56978. #else
  56979. CImg<T> *data(const unsigned int l) {
  56980. return _data + l;
  56981. }
  56982. //! Return pointer to the pos-th image of the list \const.
  56983. const CImg<T> *data(const unsigned int l) const {
  56984. return _data + l;
  56985. }
  56986. #endif
  56987. //! Return iterator to the first image of the list.
  56988. /**
  56989. **/
  56990. iterator begin() {
  56991. return _data;
  56992. }
  56993. //! Return iterator to the first image of the list \const.
  56994. const_iterator begin() const {
  56995. return _data;
  56996. }
  56997. //! Return iterator to one position after the last image of the list.
  56998. /**
  56999. **/
  57000. iterator end() {
  57001. return _data + _width;
  57002. }
  57003. //! Return iterator to one position after the last image of the list \const.
  57004. const_iterator end() const {
  57005. return _data + _width;
  57006. }
  57007. //! Return reference to the first image of the list.
  57008. /**
  57009. **/
  57010. CImg<T>& front() {
  57011. return *_data;
  57012. }
  57013. //! Return reference to the first image of the list \const.
  57014. const CImg<T>& front() const {
  57015. return *_data;
  57016. }
  57017. //! Return a reference to the last image of the list.
  57018. /**
  57019. **/
  57020. const CImg<T>& back() const {
  57021. return *(_data + _width - 1);
  57022. }
  57023. //! Return a reference to the last image of the list \const.
  57024. CImg<T>& back() {
  57025. return *(_data + _width - 1);
  57026. }
  57027. //! Return pos-th image of the list.
  57028. /**
  57029. \param pos Index of the image element to access.
  57030. **/
  57031. CImg<T>& at(const int pos) {
  57032. if (is_empty())
  57033. throw CImgInstanceException(_cimglist_instance
  57034. "at(): Empty instance.",
  57035. cimglist_instance);
  57036. return _data[cimg::cut(pos,0,width() - 1)];
  57037. }
  57038. //! Access to pixel value with Dirichlet boundary conditions.
  57039. /**
  57040. \param pos Index of the image element to access.
  57041. \param x X-coordinate of the pixel value.
  57042. \param y Y-coordinate of the pixel value.
  57043. \param z Z-coordinate of the pixel value.
  57044. \param c C-coordinate of the pixel value.
  57045. \param out_value Default value returned if \c offset is outside image bounds.
  57046. \note <tt>list.atNXYZC(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZC(x,y,z,c);</tt>.
  57047. **/
  57048. T& atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
  57049. return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXYZC(x,y,z,c,out_value);
  57050. }
  57051. //! Access to pixel value with Dirichlet boundary conditions \const.
  57052. T atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
  57053. return (pos<0 || pos>=width())?out_value:_data[pos].atXYZC(x,y,z,c,out_value);
  57054. }
  57055. //! Access to pixel value with Neumann boundary conditions.
  57056. /**
  57057. \param pos Index of the image element to access.
  57058. \param x X-coordinate of the pixel value.
  57059. \param y Y-coordinate of the pixel value.
  57060. \param z Z-coordinate of the pixel value.
  57061. \param c C-coordinate of the pixel value.
  57062. \note <tt>list.atNXYZC(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZC(x,y,z,c);</tt>.
  57063. **/
  57064. T& atNXYZC(const int pos, const int x, const int y, const int z, const int c) {
  57065. if (is_empty())
  57066. throw CImgInstanceException(_cimglist_instance
  57067. "atNXYZC(): Empty instance.",
  57068. cimglist_instance);
  57069. return _atNXYZC(pos,x,y,z,c);
  57070. }
  57071. //! Access to pixel value with Neumann boundary conditions \const.
  57072. T atNXYZC(const int pos, const int x, const int y, const int z, const int c) const {
  57073. if (is_empty())
  57074. throw CImgInstanceException(_cimglist_instance
  57075. "atNXYZC(): Empty instance.",
  57076. cimglist_instance);
  57077. return _atNXYZC(pos,x,y,z,c);
  57078. }
  57079. T& _atNXYZC(const int pos, const int x, const int y, const int z, const int c) {
  57080. return _data[cimg::cut(pos,0,width() - 1)].atXYZC(x,y,z,c);
  57081. }
  57082. T _atNXYZC(const int pos, const int x, const int y, const int z, const int c) const {
  57083. return _data[cimg::cut(pos,0,width() - 1)].atXYZC(x,y,z,c);
  57084. }
  57085. //! Access pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y,\c z).
  57086. /**
  57087. \param pos Index of the image element to access.
  57088. \param x X-coordinate of the pixel value.
  57089. \param y Y-coordinate of the pixel value.
  57090. \param z Z-coordinate of the pixel value.
  57091. \param c C-coordinate of the pixel value.
  57092. \param out_value Default value returned if \c offset is outside image bounds.
  57093. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57094. **/
  57095. T& atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
  57096. return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXYZ(x,y,z,c,out_value);
  57097. }
  57098. //! Access pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y,\c z) \const.
  57099. T atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
  57100. return (pos<0 || pos>=width())?out_value:_data[pos].atXYZ(x,y,z,c,out_value);
  57101. }
  57102. //! Access to pixel value with Neumann boundary conditions for the 4 coordinates (\c pos, \c x,\c y,\c z).
  57103. /**
  57104. \param pos Index of the image element to access.
  57105. \param x X-coordinate of the pixel value.
  57106. \param y Y-coordinate of the pixel value.
  57107. \param z Z-coordinate of the pixel value.
  57108. \param c C-coordinate of the pixel value.
  57109. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57110. **/
  57111. T& atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) {
  57112. if (is_empty())
  57113. throw CImgInstanceException(_cimglist_instance
  57114. "atNXYZ(): Empty instance.",
  57115. cimglist_instance);
  57116. return _atNXYZ(pos,x,y,z,c);
  57117. }
  57118. //! Access to pixel value with Neumann boundary conditions for the 4 coordinates (\c pos, \c x,\c y,\c z) \const.
  57119. T atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const {
  57120. if (is_empty())
  57121. throw CImgInstanceException(_cimglist_instance
  57122. "atNXYZ(): Empty instance.",
  57123. cimglist_instance);
  57124. return _atNXYZ(pos,x,y,z,c);
  57125. }
  57126. T& _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) {
  57127. return _data[cimg::cut(pos,0,width() - 1)].atXYZ(x,y,z,c);
  57128. }
  57129. T _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const {
  57130. return _data[cimg::cut(pos,0,width() - 1)].atXYZ(x,y,z,c);
  57131. }
  57132. //! Access to pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y).
  57133. /**
  57134. \param pos Index of the image element to access.
  57135. \param x X-coordinate of the pixel value.
  57136. \param y Y-coordinate of the pixel value.
  57137. \param z Z-coordinate of the pixel value.
  57138. \param c C-coordinate of the pixel value.
  57139. \param out_value Default value returned if \c offset is outside image bounds.
  57140. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57141. **/
  57142. T& atNXY(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
  57143. return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXY(x,y,z,c,out_value);
  57144. }
  57145. //! Access to pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y) \const.
  57146. T atNXY(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
  57147. return (pos<0 || pos>=width())?out_value:_data[pos].atXY(x,y,z,c,out_value);
  57148. }
  57149. //! Access to pixel value with Neumann boundary conditions for the 3 coordinates (\c pos, \c x,\c y).
  57150. /**
  57151. \param pos Index of the image element to access.
  57152. \param x X-coordinate of the pixel value.
  57153. \param y Y-coordinate of the pixel value.
  57154. \param z Z-coordinate of the pixel value.
  57155. \param c C-coordinate of the pixel value.
  57156. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57157. **/
  57158. T& atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) {
  57159. if (is_empty())
  57160. throw CImgInstanceException(_cimglist_instance
  57161. "atNXY(): Empty instance.",
  57162. cimglist_instance);
  57163. return _atNXY(pos,x,y,z,c);
  57164. }
  57165. //! Access to pixel value with Neumann boundary conditions for the 3 coordinates (\c pos, \c x,\c y) \const.
  57166. T atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const {
  57167. if (is_empty())
  57168. throw CImgInstanceException(_cimglist_instance
  57169. "atNXY(): Empty instance.",
  57170. cimglist_instance);
  57171. return _atNXY(pos,x,y,z,c);
  57172. }
  57173. T& _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) {
  57174. return _data[cimg::cut(pos,0,width() - 1)].atXY(x,y,z,c);
  57175. }
  57176. T _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const {
  57177. return _data[cimg::cut(pos,0,width() - 1)].atXY(x,y,z,c);
  57178. }
  57179. //! Access to pixel value with Dirichlet boundary conditions for the 2 coordinates (\c pos,\c x).
  57180. /**
  57181. \param pos Index of the image element to access.
  57182. \param x X-coordinate of the pixel value.
  57183. \param y Y-coordinate of the pixel value.
  57184. \param z Z-coordinate of the pixel value.
  57185. \param c C-coordinate of the pixel value.
  57186. \param out_value Default value returned if \c offset is outside image bounds.
  57187. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57188. **/
  57189. T& atNX(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
  57190. return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atX(x,y,z,c,out_value);
  57191. }
  57192. //! Access to pixel value with Dirichlet boundary conditions for the 2 coordinates (\c pos,\c x) \const.
  57193. T atNX(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
  57194. return (pos<0 || pos>=width())?out_value:_data[pos].atX(x,y,z,c,out_value);
  57195. }
  57196. //! Access to pixel value with Neumann boundary conditions for the 2 coordinates (\c pos, \c x).
  57197. /**
  57198. \param pos Index of the image element to access.
  57199. \param x X-coordinate of the pixel value.
  57200. \param y Y-coordinate of the pixel value.
  57201. \param z Z-coordinate of the pixel value.
  57202. \param c C-coordinate of the pixel value.
  57203. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57204. **/
  57205. T& atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) {
  57206. if (is_empty())
  57207. throw CImgInstanceException(_cimglist_instance
  57208. "atNX(): Empty instance.",
  57209. cimglist_instance);
  57210. return _atNX(pos,x,y,z,c);
  57211. }
  57212. //! Access to pixel value with Neumann boundary conditions for the 2 coordinates (\c pos, \c x) \const.
  57213. T atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const {
  57214. if (is_empty())
  57215. throw CImgInstanceException(_cimglist_instance
  57216. "atNX(): Empty instance.",
  57217. cimglist_instance);
  57218. return _atNX(pos,x,y,z,c);
  57219. }
  57220. T& _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) {
  57221. return _data[cimg::cut(pos,0,width() - 1)].atX(x,y,z,c);
  57222. }
  57223. T _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const {
  57224. return _data[cimg::cut(pos,0,width() - 1)].atX(x,y,z,c);
  57225. }
  57226. //! Access to pixel value with Dirichlet boundary conditions for the coordinate (\c pos).
  57227. /**
  57228. \param pos Index of the image element to access.
  57229. \param x X-coordinate of the pixel value.
  57230. \param y Y-coordinate of the pixel value.
  57231. \param z Z-coordinate of the pixel value.
  57232. \param c C-coordinate of the pixel value.
  57233. \param out_value Default value returned if \c offset is outside image bounds.
  57234. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57235. **/
  57236. T& atN(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
  57237. return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):(*this)(pos,x,y,z,c);
  57238. }
  57239. //! Access to pixel value with Dirichlet boundary conditions for the coordinate (\c pos) \const.
  57240. T atN(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
  57241. return (pos<0 || pos>=width())?out_value:(*this)(pos,x,y,z,c);
  57242. }
  57243. //! Return pixel value with Neumann boundary conditions for the coordinate (\c pos).
  57244. /**
  57245. \param pos Index of the image element to access.
  57246. \param x X-coordinate of the pixel value.
  57247. \param y Y-coordinate of the pixel value.
  57248. \param z Z-coordinate of the pixel value.
  57249. \param c C-coordinate of the pixel value.
  57250. \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
  57251. **/
  57252. T& atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) {
  57253. if (is_empty())
  57254. throw CImgInstanceException(_cimglist_instance
  57255. "atN(): Empty instance.",
  57256. cimglist_instance);
  57257. return _atN(pos,x,y,z,c);
  57258. }
  57259. //! Return pixel value with Neumann boundary conditions for the coordinate (\c pos) \const.
  57260. T atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const {
  57261. if (is_empty())
  57262. throw CImgInstanceException(_cimglist_instance
  57263. "atN(): Empty instance.",
  57264. cimglist_instance);
  57265. return _atN(pos,x,y,z,c);
  57266. }
  57267. T& _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) {
  57268. return _data[cimg::cut(pos,0,width() - 1)](x,y,z,c);
  57269. }
  57270. T _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const {
  57271. return _data[cimg::cut(pos,0,width() - 1)](x,y,z,c);
  57272. }
  57273. //@}
  57274. //-------------------------------------
  57275. //
  57276. //! \name Instance Checking
  57277. //@{
  57278. //-------------------------------------
  57279. //! Return \c true if list is empty.
  57280. /**
  57281. **/
  57282. bool is_empty() const {
  57283. return (!_data || !_width);
  57284. }
  57285. //! Test if number of image elements is equal to specified value.
  57286. /**
  57287. \param size_n Number of image elements to test.
  57288. **/
  57289. bool is_sameN(const unsigned int size_n) const {
  57290. return _width==size_n;
  57291. }
  57292. //! Test if number of image elements is equal between two images lists.
  57293. /**
  57294. \param list Input list to compare with.
  57295. **/
  57296. template<typename t>
  57297. bool is_sameN(const CImgList<t>& list) const {
  57298. return is_sameN(list._width);
  57299. }
  57300. // Define useful functions to check list dimensions.
  57301. // (cannot be documented because macro-generated).
  57302. #define _cimglist_def_is_same1(axis) \
  57303. bool is_same##axis(const unsigned int val) const { \
  57304. bool res = true; \
  57305. for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(val); \
  57306. return res; \
  57307. } \
  57308. bool is_sameN##axis(const unsigned int n, const unsigned int val) const { \
  57309. return is_sameN(n) && is_same##axis(val); \
  57310. } \
  57311. #define _cimglist_def_is_same2(axis1,axis2) \
  57312. bool is_same##axis1##axis2(const unsigned int val1, const unsigned int val2) const { \
  57313. bool res = true; \
  57314. for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2(val1,val2); \
  57315. return res; \
  57316. } \
  57317. bool is_sameN##axis1##axis2(const unsigned int n, const unsigned int val1, const unsigned int val2) const { \
  57318. return is_sameN(n) && is_same##axis1##axis2(val1,val2); \
  57319. } \
  57320. #define _cimglist_def_is_same3(axis1,axis2,axis3) \
  57321. bool is_same##axis1##axis2##axis3(const unsigned int val1, const unsigned int val2, \
  57322. const unsigned int val3) const { \
  57323. bool res = true; \
  57324. for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2##axis3(val1,val2,val3); \
  57325. return res; \
  57326. } \
  57327. bool is_sameN##axis1##axis2##axis3(const unsigned int n, const unsigned int val1, \
  57328. const unsigned int val2, const unsigned int val3) const { \
  57329. return is_sameN(n) && is_same##axis1##axis2##axis3(val1,val2,val3); \
  57330. } \
  57331. #define _cimglist_def_is_same(axis) \
  57332. template<typename t> bool is_same##axis(const CImg<t>& img) const { \
  57333. bool res = true; \
  57334. for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(img); \
  57335. return res; \
  57336. } \
  57337. template<typename t> bool is_same##axis(const CImgList<t>& list) const { \
  57338. const unsigned int lmin = std::min(_width,list._width); \
  57339. bool res = true; \
  57340. for (unsigned int l = 0; l<lmin && res; ++l) res = _data[l].is_same##axis(list[l]); \
  57341. return res; \
  57342. } \
  57343. template<typename t> bool is_sameN##axis(const unsigned int n, const CImg<t>& img) const { \
  57344. return (is_sameN(n) && is_same##axis(img)); \
  57345. } \
  57346. template<typename t> bool is_sameN##axis(const CImgList<t>& list) const { \
  57347. return (is_sameN(list) && is_same##axis(list)); \
  57348. }
  57349. _cimglist_def_is_same(XY)
  57350. _cimglist_def_is_same(XZ)
  57351. _cimglist_def_is_same(XC)
  57352. _cimglist_def_is_same(YZ)
  57353. _cimglist_def_is_same(YC)
  57354. _cimglist_def_is_same(XYZ)
  57355. _cimglist_def_is_same(XYC)
  57356. _cimglist_def_is_same(YZC)
  57357. _cimglist_def_is_same(XYZC)
  57358. _cimglist_def_is_same1(X)
  57359. _cimglist_def_is_same1(Y)
  57360. _cimglist_def_is_same1(Z)
  57361. _cimglist_def_is_same1(C)
  57362. _cimglist_def_is_same2(X,Y)
  57363. _cimglist_def_is_same2(X,Z)
  57364. _cimglist_def_is_same2(X,C)
  57365. _cimglist_def_is_same2(Y,Z)
  57366. _cimglist_def_is_same2(Y,C)
  57367. _cimglist_def_is_same2(Z,C)
  57368. _cimglist_def_is_same3(X,Y,Z)
  57369. _cimglist_def_is_same3(X,Y,C)
  57370. _cimglist_def_is_same3(X,Z,C)
  57371. _cimglist_def_is_same3(Y,Z,C)
  57372. //! Test if dimensions of each image of the list match specified arguments.
  57373. /**
  57374. \param dx Checked image width.
  57375. \param dy Checked image height.
  57376. \param dz Checked image depth.
  57377. \param dc Checked image spectrum.
  57378. **/
  57379. bool is_sameXYZC(const unsigned int dx, const unsigned int dy,
  57380. const unsigned int dz, const unsigned int dc) const {
  57381. bool res = true;
  57382. for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_sameXYZC(dx,dy,dz,dc);
  57383. return res;
  57384. }
  57385. //! Test if list dimensions match specified arguments.
  57386. /**
  57387. \param n Number of images in the list.
  57388. \param dx Checked image width.
  57389. \param dy Checked image height.
  57390. \param dz Checked image depth.
  57391. \param dc Checked image spectrum.
  57392. **/
  57393. bool is_sameNXYZC(const unsigned int n,
  57394. const unsigned int dx, const unsigned int dy,
  57395. const unsigned int dz, const unsigned int dc) const {
  57396. return is_sameN(n) && is_sameXYZC(dx,dy,dz,dc);
  57397. }
  57398. //! Test if list contains one particular pixel location.
  57399. /**
  57400. \param n Index of the image whom checked pixel value belong to.
  57401. \param x X-coordinate of the checked pixel value.
  57402. \param y Y-coordinate of the checked pixel value.
  57403. \param z Z-coordinate of the checked pixel value.
  57404. \param c C-coordinate of the checked pixel value.
  57405. **/
  57406. bool containsNXYZC(const int n, const int x=0, const int y=0, const int z=0, const int c=0) const {
  57407. if (is_empty()) return false;
  57408. return n>=0 && n<width() && x>=0 && x<_data[n].width() && y>=0 && y<_data[n].height() &&
  57409. z>=0 && z<_data[n].depth() && c>=0 && c<_data[n].spectrum();
  57410. }
  57411. //! Test if list contains image with specified index.
  57412. /**
  57413. \param n Index of the checked image.
  57414. **/
  57415. bool containsN(const int n) const {
  57416. if (is_empty()) return false;
  57417. return n>=0 && n<width();
  57418. }
  57419. //! Test if one image of the list contains the specified referenced value.
  57420. /**
  57421. \param pixel Reference to pixel value to test.
  57422. \param[out] n Index of image containing the pixel value, if test succeeds.
  57423. \param[out] x X-coordinate of the pixel value, if test succeeds.
  57424. \param[out] y Y-coordinate of the pixel value, if test succeeds.
  57425. \param[out] z Z-coordinate of the pixel value, if test succeeds.
  57426. \param[out] c C-coordinate of the pixel value, if test succeeds.
  57427. \note If true, set coordinates (n,x,y,z,c).
  57428. **/
  57429. template<typename t>
  57430. bool contains(const T& pixel, t& n, t& x, t&y, t& z, t& c) const {
  57431. if (is_empty()) return false;
  57432. cimglist_for(*this,l) if (_data[l].contains(pixel,x,y,z,c)) { n = (t)l; return true; }
  57433. return false;
  57434. }
  57435. //! Test if one of the image list contains the specified referenced value.
  57436. /**
  57437. \param pixel Reference to pixel value to test.
  57438. \param[out] n Index of image containing the pixel value, if test succeeds.
  57439. \param[out] x X-coordinate of the pixel value, if test succeeds.
  57440. \param[out] y Y-coordinate of the pixel value, if test succeeds.
  57441. \param[out] z Z-coordinate of the pixel value, if test succeeds.
  57442. \note If true, set coordinates (n,x,y,z).
  57443. **/
  57444. template<typename t>
  57445. bool contains(const T& pixel, t& n, t& x, t&y, t& z) const {
  57446. t c;
  57447. return contains(pixel,n,x,y,z,c);
  57448. }
  57449. //! Test if one of the image list contains the specified referenced value.
  57450. /**
  57451. \param pixel Reference to pixel value to test.
  57452. \param[out] n Index of image containing the pixel value, if test succeeds.
  57453. \param[out] x X-coordinate of the pixel value, if test succeeds.
  57454. \param[out] y Y-coordinate of the pixel value, if test succeeds.
  57455. \note If true, set coordinates (n,x,y).
  57456. **/
  57457. template<typename t>
  57458. bool contains(const T& pixel, t& n, t& x, t&y) const {
  57459. t z, c;
  57460. return contains(pixel,n,x,y,z,c);
  57461. }
  57462. //! Test if one of the image list contains the specified referenced value.
  57463. /**
  57464. \param pixel Reference to pixel value to test.
  57465. \param[out] n Index of image containing the pixel value, if test succeeds.
  57466. \param[out] x X-coordinate of the pixel value, if test succeeds.
  57467. \note If true, set coordinates (n,x).
  57468. **/
  57469. template<typename t>
  57470. bool contains(const T& pixel, t& n, t& x) const {
  57471. t y, z, c;
  57472. return contains(pixel,n,x,y,z,c);
  57473. }
  57474. //! Test if one of the image list contains the specified referenced value.
  57475. /**
  57476. \param pixel Reference to pixel value to test.
  57477. \param[out] n Index of image containing the pixel value, if test succeeds.
  57478. \note If true, set coordinates (n).
  57479. **/
  57480. template<typename t>
  57481. bool contains(const T& pixel, t& n) const {
  57482. t x, y, z, c;
  57483. return contains(pixel,n,x,y,z,c);
  57484. }
  57485. //! Test if one of the image list contains the specified referenced value.
  57486. /**
  57487. \param pixel Reference to pixel value to test.
  57488. **/
  57489. bool contains(const T& pixel) const {
  57490. unsigned int n, x, y, z, c;
  57491. return contains(pixel,n,x,y,z,c);
  57492. }
  57493. //! Test if the list contains the image 'img'.
  57494. /**
  57495. \param img Reference to image to test.
  57496. \param[out] n Index of image in the list, if test succeeds.
  57497. \note If true, returns the position (n) of the image in the list.
  57498. **/
  57499. template<typename t>
  57500. bool contains(const CImg<T>& img, t& n) const {
  57501. if (is_empty()) return false;
  57502. const CImg<T> *const ptr = &img;
  57503. cimglist_for(*this,i) if (_data + i==ptr) { n = (t)i; return true; }
  57504. return false;
  57505. }
  57506. //! Test if the list contains the image img.
  57507. /**
  57508. \param img Reference to image to test.
  57509. **/
  57510. bool contains(const CImg<T>& img) const {
  57511. unsigned int n;
  57512. return contains(img,n);
  57513. }
  57514. //@}
  57515. //-------------------------------------
  57516. //
  57517. //! \name Mathematical Functions
  57518. //@{
  57519. //-------------------------------------
  57520. //! Return a reference to the minimum pixel value of the instance list.
  57521. /**
  57522. **/
  57523. T& min() {
  57524. bool is_all_empty = true;
  57525. T *ptr_min = 0;
  57526. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57527. ptr_min = _data[l]._data;
  57528. is_all_empty = false;
  57529. break;
  57530. }
  57531. if (is_all_empty)
  57532. throw CImgInstanceException(_cimglist_instance
  57533. "min(): %s.",
  57534. _data?"List of empty images":"Empty instance",
  57535. cimglist_instance);
  57536. T min_value = *ptr_min;
  57537. cimglist_for(*this,l) {
  57538. const CImg<T>& img = _data[l];
  57539. cimg_for(img,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
  57540. }
  57541. return *ptr_min;
  57542. }
  57543. //! Return a reference to the minimum pixel value of the instance list \const.
  57544. const T& min() const {
  57545. bool is_all_empty = true;
  57546. T *ptr_min = 0;
  57547. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57548. ptr_min = _data[l]._data;
  57549. is_all_empty = false;
  57550. break;
  57551. }
  57552. if (is_all_empty)
  57553. throw CImgInstanceException(_cimglist_instance
  57554. "min(): %s.",
  57555. _data?"List of empty images":"Empty instance",
  57556. cimglist_instance);
  57557. T min_value = *ptr_min;
  57558. cimglist_for(*this,l) {
  57559. const CImg<T>& img = _data[l];
  57560. cimg_for(img,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
  57561. }
  57562. return *ptr_min;
  57563. }
  57564. //! Return a reference to the maximum pixel value of the instance list.
  57565. /**
  57566. **/
  57567. T& max() {
  57568. bool is_all_empty = true;
  57569. T *ptr_max = 0;
  57570. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57571. ptr_max = _data[l]._data;
  57572. is_all_empty = false;
  57573. break;
  57574. }
  57575. if (is_all_empty)
  57576. throw CImgInstanceException(_cimglist_instance
  57577. "max(): %s.",
  57578. _data?"List of empty images":"Empty instance",
  57579. cimglist_instance);
  57580. T max_value = *ptr_max;
  57581. cimglist_for(*this,l) {
  57582. const CImg<T>& img = _data[l];
  57583. cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
  57584. }
  57585. return *ptr_max;
  57586. }
  57587. //! Return a reference to the maximum pixel value of the instance list \const.
  57588. const T& max() const {
  57589. bool is_all_empty = true;
  57590. T *ptr_max = 0;
  57591. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57592. ptr_max = _data[l]._data;
  57593. is_all_empty = false;
  57594. break;
  57595. }
  57596. if (is_all_empty)
  57597. throw CImgInstanceException(_cimglist_instance
  57598. "max(): %s.",
  57599. _data?"List of empty images":"Empty instance",
  57600. cimglist_instance);
  57601. T max_value = *ptr_max;
  57602. cimglist_for(*this,l) {
  57603. const CImg<T>& img = _data[l];
  57604. cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
  57605. }
  57606. return *ptr_max;
  57607. }
  57608. //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well.
  57609. /**
  57610. \param[out] max_val Value of the maximum value found.
  57611. **/
  57612. template<typename t>
  57613. T& min_max(t& max_val) {
  57614. bool is_all_empty = true;
  57615. T *ptr_min = 0;
  57616. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57617. ptr_min = _data[l]._data;
  57618. is_all_empty = false;
  57619. break;
  57620. }
  57621. if (is_all_empty)
  57622. throw CImgInstanceException(_cimglist_instance
  57623. "min_max(): %s.",
  57624. _data?"List of empty images":"Empty instance",
  57625. cimglist_instance);
  57626. T min_value = *ptr_min, max_value = min_value;
  57627. cimglist_for(*this,l) {
  57628. const CImg<T>& img = _data[l];
  57629. cimg_for(img,ptrs,T) {
  57630. const T val = *ptrs;
  57631. if (val<min_value) { min_value = val; ptr_min = ptrs; }
  57632. if (val>max_value) max_value = val;
  57633. }
  57634. }
  57635. max_val = (t)max_value;
  57636. return *ptr_min;
  57637. }
  57638. //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well \const.
  57639. /**
  57640. \param[out] max_val Value of the maximum value found.
  57641. **/
  57642. template<typename t>
  57643. const T& min_max(t& max_val) const {
  57644. bool is_all_empty = true;
  57645. T *ptr_min = 0;
  57646. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57647. ptr_min = _data[l]._data;
  57648. is_all_empty = false;
  57649. break;
  57650. }
  57651. if (is_all_empty)
  57652. throw CImgInstanceException(_cimglist_instance
  57653. "min_max(): %s.",
  57654. _data?"List of empty images":"Empty instance",
  57655. cimglist_instance);
  57656. T min_value = *ptr_min, max_value = min_value;
  57657. cimglist_for(*this,l) {
  57658. const CImg<T>& img = _data[l];
  57659. cimg_for(img,ptrs,T) {
  57660. const T val = *ptrs;
  57661. if (val<min_value) { min_value = val; ptr_min = ptrs; }
  57662. if (val>max_value) max_value = val;
  57663. }
  57664. }
  57665. max_val = (t)max_value;
  57666. return *ptr_min;
  57667. }
  57668. //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well.
  57669. /**
  57670. \param[out] min_val Value of the minimum value found.
  57671. **/
  57672. template<typename t>
  57673. T& max_min(t& min_val) {
  57674. bool is_all_empty = true;
  57675. T *ptr_max = 0;
  57676. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57677. ptr_max = _data[l]._data;
  57678. is_all_empty = false;
  57679. break;
  57680. }
  57681. if (is_all_empty)
  57682. throw CImgInstanceException(_cimglist_instance
  57683. "max_min(): %s.",
  57684. _data?"List of empty images":"Empty instance",
  57685. cimglist_instance);
  57686. T min_value = *ptr_max, max_value = min_value;
  57687. cimglist_for(*this,l) {
  57688. const CImg<T>& img = _data[l];
  57689. cimg_for(img,ptrs,T) {
  57690. const T val = *ptrs;
  57691. if (val>max_value) { max_value = val; ptr_max = ptrs; }
  57692. if (val<min_value) min_value = val;
  57693. }
  57694. }
  57695. min_val = (t)min_value;
  57696. return *ptr_max;
  57697. }
  57698. //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well \const.
  57699. template<typename t>
  57700. const T& max_min(t& min_val) const {
  57701. bool is_all_empty = true;
  57702. T *ptr_max = 0;
  57703. cimglist_for(*this,l) if (!_data[l].is_empty()) {
  57704. ptr_max = _data[l]._data;
  57705. is_all_empty = false;
  57706. break;
  57707. }
  57708. if (is_all_empty)
  57709. throw CImgInstanceException(_cimglist_instance
  57710. "max_min(): %s.",
  57711. _data?"List of empty images":"Empty instance",
  57712. cimglist_instance);
  57713. T min_value = *ptr_max, max_value = min_value;
  57714. cimglist_for(*this,l) {
  57715. const CImg<T>& img = _data[l];
  57716. cimg_for(img,ptrs,T) {
  57717. const T val = *ptrs;
  57718. if (val>max_value) { max_value = val; ptr_max = ptrs; }
  57719. if (val<min_value) min_value = val;
  57720. }
  57721. }
  57722. min_val = (t)min_value;
  57723. return *ptr_max;
  57724. }
  57725. //@}
  57726. //---------------------------
  57727. //
  57728. //! \name List Manipulation
  57729. //@{
  57730. //---------------------------
  57731. //! Insert a copy of the image \c img into the current image list, at position \c pos.
  57732. /**
  57733. \param img Image to insert a copy to the list.
  57734. \param pos Index of the insertion.
  57735. \param is_shared Tells if the inserted image is a shared copy of \c img or not.
  57736. **/
  57737. template<typename t>
  57738. CImgList<T>& insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) {
  57739. const unsigned int npos = pos==~0U?_width:pos;
  57740. if (npos>_width)
  57741. throw CImgArgumentException(_cimglist_instance
  57742. "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) "
  57743. "at position %u.",
  57744. cimglist_instance,
  57745. img._width,img._height,img._depth,img._spectrum,img._data,npos);
  57746. if (is_shared)
  57747. throw CImgArgumentException(_cimglist_instance
  57748. "insert(): Invalid insertion request of specified shared image "
  57749. "CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).",
  57750. cimglist_instance,
  57751. img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos);
  57752. CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):
  57753. (_allocated_width=16)]:0;
  57754. if (!_data) { // Insert new element into empty list
  57755. _data = new_data;
  57756. *_data = img;
  57757. } else {
  57758. if (new_data) { // Insert with re-allocation
  57759. if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);
  57760. if (npos!=_width - 1)
  57761. std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
  57762. std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));
  57763. delete[] _data;
  57764. _data = new_data;
  57765. } else if (npos!=_width - 1) // Insert without re-allocation
  57766. std::memmove((void*)(_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
  57767. _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0;
  57768. _data[npos]._data = 0;
  57769. _data[npos] = img;
  57770. }
  57771. return *this;
  57772. }
  57773. //! Insert a copy of the image \c img into the current image list, at position \c pos \specialization.
  57774. CImgList<T>& insert(const CImg<T>& img, const unsigned int pos=~0U, const bool is_shared=false) {
  57775. const unsigned int npos = pos==~0U?_width:pos;
  57776. if (npos>_width)
  57777. throw CImgArgumentException(_cimglist_instance
  57778. "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) "
  57779. "at position %u.",
  57780. cimglist_instance,
  57781. img._width,img._height,img._depth,img._spectrum,img._data,npos);
  57782. CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):
  57783. (_allocated_width=16)]:0;
  57784. if (!_data) { // Insert new element into empty list
  57785. _data = new_data;
  57786. if (is_shared && img) {
  57787. _data->_width = img._width;
  57788. _data->_height = img._height;
  57789. _data->_depth = img._depth;
  57790. _data->_spectrum = img._spectrum;
  57791. _data->_is_shared = true;
  57792. _data->_data = img._data;
  57793. } else *_data = img;
  57794. }
  57795. else {
  57796. if (new_data) { // Insert with re-allocation
  57797. if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);
  57798. if (npos!=_width - 1)
  57799. std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
  57800. if (is_shared && img) {
  57801. new_data[npos]._width = img._width;
  57802. new_data[npos]._height = img._height;
  57803. new_data[npos]._depth = img._depth;
  57804. new_data[npos]._spectrum = img._spectrum;
  57805. new_data[npos]._is_shared = true;
  57806. new_data[npos]._data = img._data;
  57807. } else {
  57808. new_data[npos]._width = new_data[npos]._height = new_data[npos]._depth = new_data[npos]._spectrum = 0;
  57809. new_data[npos]._data = 0;
  57810. new_data[npos] = img;
  57811. }
  57812. std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));
  57813. delete[] _data;
  57814. _data = new_data;
  57815. } else { // Insert without re-allocation
  57816. if (npos!=_width - 1)
  57817. std::memmove((void*)(_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
  57818. if (is_shared && img) {
  57819. _data[npos]._width = img._width;
  57820. _data[npos]._height = img._height;
  57821. _data[npos]._depth = img._depth;
  57822. _data[npos]._spectrum = img._spectrum;
  57823. _data[npos]._is_shared = true;
  57824. _data[npos]._data = img._data;
  57825. } else {
  57826. _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0;
  57827. _data[npos]._data = 0;
  57828. _data[npos] = img;
  57829. }
  57830. }
  57831. }
  57832. return *this;
  57833. }
  57834. //! Insert a copy of the image \c img into the current image list, at position \c pos \newinstance.
  57835. template<typename t>
  57836. CImgList<T> get_insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) const {
  57837. return (+*this).insert(img,pos,is_shared);
  57838. }
  57839. //! Insert n empty images img into the current image list, at position \p pos.
  57840. /**
  57841. \param n Number of empty images to insert.
  57842. \param pos Index of the insertion.
  57843. **/
  57844. CImgList<T>& insert(const unsigned int n, const unsigned int pos=~0U) {
  57845. CImg<T> empty;
  57846. if (!n) return *this;
  57847. const unsigned int npos = pos==~0U?_width:pos;
  57848. for (unsigned int i = 0; i<n; ++i) insert(empty,npos+i);
  57849. return *this;
  57850. }
  57851. //! Insert n empty images img into the current image list, at position \p pos \newinstance.
  57852. CImgList<T> get_insert(const unsigned int n, const unsigned int pos=~0U) const {
  57853. return (+*this).insert(n,pos);
  57854. }
  57855. //! Insert \c n copies of the image \c img into the current image list, at position \c pos.
  57856. /**
  57857. \param n Number of image copies to insert.
  57858. \param img Image to insert by copy.
  57859. \param pos Index of the insertion.
  57860. \param is_shared Tells if inserted images are shared copies of \c img or not.
  57861. **/
  57862. template<typename t>
  57863. CImgList<T>& insert(const unsigned int n, const CImg<t>& img, const unsigned int pos=~0U,
  57864. const bool is_shared=false) {
  57865. if (!n) return *this;
  57866. const unsigned int npos = pos==~0U?_width:pos;
  57867. insert(img,npos,is_shared);
  57868. for (unsigned int i = 1; i<n; ++i) insert(_data[npos],npos + i,is_shared);
  57869. return *this;
  57870. }
  57871. //! Insert \c n copies of the image \c img into the current image list, at position \c pos \newinstance.
  57872. template<typename t>
  57873. CImgList<T> get_insert(const unsigned int n, const CImg<t>& img, const unsigned int pos=~0U,
  57874. const bool is_shared=false) const {
  57875. return (+*this).insert(n,img,pos,is_shared);
  57876. }
  57877. //! Insert a copy of the image list \c list into the current image list, starting from position \c pos.
  57878. /**
  57879. \param list Image list to insert.
  57880. \param pos Index of the insertion.
  57881. \param is_shared Tells if inserted images are shared copies of images of \c list or not.
  57882. **/
  57883. template<typename t>
  57884. CImgList<T>& insert(const CImgList<t>& list, const unsigned int pos=~0U, const bool is_shared=false) {
  57885. const unsigned int npos = pos==~0U?_width:pos;
  57886. if ((void*)this!=(void*)&list) cimglist_for(list,l) insert(list[l],npos + l,is_shared);
  57887. else insert(CImgList<T>(list),npos,is_shared);
  57888. return *this;
  57889. }
  57890. //! Insert a copy of the image list \c list into the current image list, starting from position \c pos \newinstance.
  57891. template<typename t>
  57892. CImgList<T> get_insert(const CImgList<t>& list, const unsigned int pos=~0U, const bool is_shared=false) const {
  57893. return (+*this).insert(list,pos,is_shared);
  57894. }
  57895. //! Insert n copies of the list \c list at position \c pos of the current list.
  57896. /**
  57897. \param n Number of list copies to insert.
  57898. \param list Image list to insert.
  57899. \param pos Index of the insertion.
  57900. \param is_shared Tells if inserted images are shared copies of images of \c list or not.
  57901. **/
  57902. template<typename t>
  57903. CImgList<T>& insert(const unsigned int n, const CImgList<t>& list, const unsigned int pos=~0U,
  57904. const bool is_shared=false) {
  57905. if (!n) return *this;
  57906. const unsigned int npos = pos==~0U?_width:pos;
  57907. for (unsigned int i = 0; i<n; ++i) insert(list,npos,is_shared);
  57908. return *this;
  57909. }
  57910. //! Insert n copies of the list \c list at position \c pos of the current list \newinstance.
  57911. template<typename t>
  57912. CImgList<T> get_insert(const unsigned int n, const CImgList<t>& list, const unsigned int pos=~0U,
  57913. const bool is_shared=false) const {
  57914. return (+*this).insert(n,list,pos,is_shared);
  57915. }
  57916. //! Remove all images between from indexes.
  57917. /**
  57918. \param pos1 Starting index of the removal.
  57919. \param pos2 Ending index of the removal.
  57920. **/
  57921. CImgList<T>& remove(const unsigned int pos1, const unsigned int pos2) {
  57922. const unsigned int
  57923. npos1 = pos1<pos2?pos1:pos2,
  57924. tpos2 = pos1<pos2?pos2:pos1,
  57925. npos2 = tpos2<_width?tpos2:_width - 1;
  57926. if (npos1>=_width)
  57927. throw CImgArgumentException(_cimglist_instance
  57928. "remove(): Invalid remove request at positions %u->%u.",
  57929. cimglist_instance,
  57930. npos1,tpos2);
  57931. else {
  57932. if (tpos2>=_width)
  57933. throw CImgArgumentException(_cimglist_instance
  57934. "remove(): Invalid remove request at positions %u->%u.",
  57935. cimglist_instance,
  57936. npos1,tpos2);
  57937. for (unsigned int k = npos1; k<=npos2; ++k) _data[k].assign();
  57938. const unsigned int nb = 1 + npos2 - npos1;
  57939. if (!(_width-=nb)) return assign();
  57940. if (_width>(_allocated_width>>4) || _allocated_width<=16) { // Removing items without reallocation
  57941. if (npos1!=_width)
  57942. std::memmove((void*)(_data + npos1),(void*)(_data + npos2 + 1),sizeof(CImg<T>)*(_width - npos1));
  57943. std::memset((void*)(_data + _width),0,sizeof(CImg<T>)*nb);
  57944. } else { // Removing items with reallocation
  57945. _allocated_width>>=4;
  57946. while (_allocated_width>16 && _width<(_allocated_width>>1)) _allocated_width>>=1;
  57947. CImg<T> *const new_data = new CImg<T>[_allocated_width];
  57948. if (npos1) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos1);
  57949. if (npos1!=_width)
  57950. std::memcpy((void*)(new_data + npos1),(void*)(_data + npos2 + 1),sizeof(CImg<T>)*(_width - npos1));
  57951. if (_width!=_allocated_width)
  57952. std::memset((void*)(new_data + _width),0,sizeof(CImg<T>)*(_allocated_width - _width));
  57953. std::memset((void*)_data,0,sizeof(CImg<T>)*(_width + nb));
  57954. delete[] _data;
  57955. _data = new_data;
  57956. }
  57957. }
  57958. return *this;
  57959. }
  57960. //! Remove all images between from indexes \newinstance.
  57961. CImgList<T> get_remove(const unsigned int pos1, const unsigned int pos2) const {
  57962. return (+*this).remove(pos1,pos2);
  57963. }
  57964. //! Remove image at index \c pos from the image list.
  57965. /**
  57966. \param pos Index of the image to remove.
  57967. **/
  57968. CImgList<T>& remove(const unsigned int pos) {
  57969. return remove(pos,pos);
  57970. }
  57971. //! Remove image at index \c pos from the image list \newinstance.
  57972. CImgList<T> get_remove(const unsigned int pos) const {
  57973. return (+*this).remove(pos);
  57974. }
  57975. //! Remove last image.
  57976. /**
  57977. **/
  57978. CImgList<T>& remove() {
  57979. return remove(_width - 1);
  57980. }
  57981. //! Remove last image \newinstance.
  57982. CImgList<T> get_remove() const {
  57983. return (+*this).remove();
  57984. }
  57985. //! Reverse list order.
  57986. CImgList<T>& reverse() {
  57987. for (unsigned int l = 0; l<_width/2; ++l) (*this)[l].swap((*this)[_width - 1 - l]);
  57988. return *this;
  57989. }
  57990. //! Reverse list order \newinstance.
  57991. CImgList<T> get_reverse() const {
  57992. return (+*this).reverse();
  57993. }
  57994. //! Return a sublist.
  57995. /**
  57996. \param pos0 Starting index of the sublist.
  57997. \param pos1 Ending index of the sublist.
  57998. **/
  57999. CImgList<T>& images(const unsigned int pos0, const unsigned int pos1) {
  58000. return get_images(pos0,pos1).move_to(*this);
  58001. }
  58002. //! Return a sublist \newinstance.
  58003. CImgList<T> get_images(const unsigned int pos0, const unsigned int pos1) const {
  58004. if (pos0>pos1 || pos1>=_width)
  58005. throw CImgArgumentException(_cimglist_instance
  58006. "images(): Specified sub-list indices (%u->%u) are out of bounds.",
  58007. cimglist_instance,
  58008. pos0,pos1);
  58009. CImgList<T> res(pos1 - pos0 + 1);
  58010. cimglist_for(res,l) res[l].assign(_data[pos0 + l]);
  58011. return res;
  58012. }
  58013. //! Return a shared sublist.
  58014. /**
  58015. \param pos0 Starting index of the sublist.
  58016. \param pos1 Ending index of the sublist.
  58017. **/
  58018. CImgList<T> get_shared_images(const unsigned int pos0, const unsigned int pos1) {
  58019. if (pos0>pos1 || pos1>=_width)
  58020. throw CImgArgumentException(_cimglist_instance
  58021. "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.",
  58022. cimglist_instance,
  58023. pos0,pos1);
  58024. CImgList<T> res(pos1 - pos0 + 1);
  58025. cimglist_for(res,l) res[l].assign(_data[pos0 + l],_data[pos0 + l]?true:false);
  58026. return res;
  58027. }
  58028. //! Return a shared sublist \newinstance.
  58029. const CImgList<T> get_shared_images(const unsigned int pos0, const unsigned int pos1) const {
  58030. if (pos0>pos1 || pos1>=_width)
  58031. throw CImgArgumentException(_cimglist_instance
  58032. "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.",
  58033. cimglist_instance,
  58034. pos0,pos1);
  58035. CImgList<T> res(pos1 - pos0 + 1);
  58036. cimglist_for(res,l) res[l].assign(_data[pos0 + l],_data[pos0 + l]?true:false);
  58037. return res;
  58038. }
  58039. //! Return a single image which is the appending of all images of the current CImgList instance.
  58040. /**
  58041. \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  58042. \param align Appending alignment.
  58043. **/
  58044. CImg<T> get_append(const char axis, const float align=0) const {
  58045. if (is_empty()) return CImg<T>();
  58046. if (_width==1) return +((*this)[0]);
  58047. unsigned int dx = 0, dy = 0, dz = 0, dc = 0, pos = 0;
  58048. CImg<T> res;
  58049. switch (cimg::lowercase(axis)) {
  58050. case 'x' : { // Along the X-axis
  58051. cimglist_for(*this,l) {
  58052. const CImg<T>& img = (*this)[l];
  58053. if (img) {
  58054. dx+=img._width;
  58055. dy = std::max(dy,img._height);
  58056. dz = std::max(dz,img._depth);
  58057. dc = std::max(dc,img._spectrum);
  58058. }
  58059. }
  58060. res.assign(dx,dy,dz,dc,(T)0);
  58061. if (res) cimglist_for(*this,l) {
  58062. const CImg<T>& img = (*this)[l];
  58063. if (img) {
  58064. if (img._height==1 && img._depth==1 && img._spectrum==1 &&
  58065. res._height==1 && res._depth==1 && res._spectrum==1)
  58066. std::memcpy(&res[pos],img._data,sizeof(T)*img._width);
  58067. else
  58068. res.draw_image(pos,
  58069. (int)(align*(dy - img._height)),
  58070. (int)(align*(dz - img._depth)),
  58071. (int)(align*(dc - img._spectrum)),
  58072. img);
  58073. }
  58074. pos+=img._width;
  58075. }
  58076. } break;
  58077. case 'y' : { // Along the Y-axis
  58078. cimglist_for(*this,l) {
  58079. const CImg<T>& img = (*this)[l];
  58080. if (img) {
  58081. dx = std::max(dx,img._width);
  58082. dy+=img._height;
  58083. dz = std::max(dz,img._depth);
  58084. dc = std::max(dc,img._spectrum);
  58085. }
  58086. }
  58087. res.assign(dx,dy,dz,dc,(T)0);
  58088. if (res) cimglist_for(*this,l) {
  58089. const CImg<T>& img = (*this)[l];
  58090. if (img) {
  58091. if (img._width==1 && img._depth==1 && img._spectrum==1 &&
  58092. res._width==1 && res._depth==1 && res._spectrum==1)
  58093. std::memcpy(&res[pos],img._data,sizeof(T)*img._height);
  58094. else
  58095. res.draw_image((int)(align*(dx - img._width)),
  58096. pos,
  58097. (int)(align*(dz - img._depth)),
  58098. (int)(align*(dc - img._spectrum)),
  58099. img);
  58100. }
  58101. pos+=img._height;
  58102. }
  58103. } break;
  58104. case 'z' : { // Along the Z-axis
  58105. cimglist_for(*this,l) {
  58106. const CImg<T>& img = (*this)[l];
  58107. if (img) {
  58108. dx = std::max(dx,img._width);
  58109. dy = std::max(dy,img._height);
  58110. dz+=img._depth;
  58111. dc = std::max(dc,img._spectrum);
  58112. }
  58113. }
  58114. res.assign(dx,dy,dz,dc,(T)0);
  58115. if (res) cimglist_for(*this,l) {
  58116. const CImg<T>& img = (*this)[l];
  58117. if (img) {
  58118. if (img._width==1 && img._height==1 && img._spectrum==1 &&
  58119. res._width==1 && res._height==1 && res._spectrum==1)
  58120. std::memcpy(&res[pos],img._data,sizeof(T)*img._depth);
  58121. else
  58122. res.draw_image((int)(align*(dx - img._width)),
  58123. (int)(align*(dy - img._height)),
  58124. pos,
  58125. (int)(align*(dc - img._spectrum)),
  58126. img);
  58127. }
  58128. pos+=img._depth;
  58129. }
  58130. } break;
  58131. default : { // Along the C-axis
  58132. cimglist_for(*this,l) {
  58133. const CImg<T>& img = (*this)[l];
  58134. if (img) {
  58135. dx = std::max(dx,img._width);
  58136. dy = std::max(dy,img._height);
  58137. dz = std::max(dz,img._depth);
  58138. dc+=img._spectrum;
  58139. }
  58140. }
  58141. res.assign(dx,dy,dz,dc,(T)0);
  58142. if (res) cimglist_for(*this,l) {
  58143. const CImg<T>& img = (*this)[l];
  58144. if (img) {
  58145. if (img._width==1 && img._height==1 && img._depth==1 &&
  58146. res._width==1 && res._height==1 && res._depth==1)
  58147. std::memcpy(&res[pos],img._data,sizeof(T)*img._spectrum);
  58148. else
  58149. res.draw_image((int)(align*(dx - img._width)),
  58150. (int)(align*(dy - img._height)),
  58151. (int)(align*(dz - img._depth)),
  58152. pos,
  58153. img);
  58154. }
  58155. pos+=img._spectrum;
  58156. }
  58157. }
  58158. }
  58159. return res;
  58160. }
  58161. //! Return a list where each image has been split along the specified axis.
  58162. /**
  58163. \param axis Axis to split images along.
  58164. \param nb Number of split parts for each image.
  58165. **/
  58166. CImgList<T>& split(const char axis, const int nb=-1) {
  58167. return get_split(axis,nb).move_to(*this);
  58168. }
  58169. //! Return a list where each image has been split along the specified axis \newinstance.
  58170. CImgList<T> get_split(const char axis, const int nb=-1) const {
  58171. CImgList<T> res;
  58172. cimglist_for(*this,l) _data[l].get_split(axis,nb).move_to(res,~0U);
  58173. return res;
  58174. }
  58175. //! Insert image at the end of the list.
  58176. /**
  58177. \param img Image to insert.
  58178. **/
  58179. template<typename t>
  58180. CImgList<T>& push_back(const CImg<t>& img) {
  58181. return insert(img);
  58182. }
  58183. //! Insert image at the front of the list.
  58184. /**
  58185. \param img Image to insert.
  58186. **/
  58187. template<typename t>
  58188. CImgList<T>& push_front(const CImg<t>& img) {
  58189. return insert(img,0);
  58190. }
  58191. //! Insert list at the end of the current list.
  58192. /**
  58193. \param list List to insert.
  58194. **/
  58195. template<typename t>
  58196. CImgList<T>& push_back(const CImgList<t>& list) {
  58197. return insert(list);
  58198. }
  58199. //! Insert list at the front of the current list.
  58200. /**
  58201. \param list List to insert.
  58202. **/
  58203. template<typename t>
  58204. CImgList<T>& push_front(const CImgList<t>& list) {
  58205. return insert(list,0);
  58206. }
  58207. //! Remove last image.
  58208. /**
  58209. **/
  58210. CImgList<T>& pop_back() {
  58211. return remove(_width - 1);
  58212. }
  58213. //! Remove first image.
  58214. /**
  58215. **/
  58216. CImgList<T>& pop_front() {
  58217. return remove(0);
  58218. }
  58219. //! Remove image pointed by iterator.
  58220. /**
  58221. \param iter Iterator pointing to the image to remove.
  58222. **/
  58223. CImgList<T>& erase(const iterator iter) {
  58224. return remove(iter - _data);
  58225. }
  58226. //@}
  58227. //----------------------------------
  58228. //
  58229. //! \name Data Input
  58230. //@{
  58231. //----------------------------------
  58232. //! Display a simple interactive interface to select images or sublists.
  58233. /**
  58234. \param disp Window instance to display selection and user interface.
  58235. \param feature_type Can be \c false to select a single image, or \c true to select a sublist.
  58236. \param axis Axis along whom images are appended for visualization.
  58237. \param align Alignment setting when images have not all the same size.
  58238. \param exit_on_anykey Exit function when any key is pressed.
  58239. \return A one-column vector containing the selected image indexes.
  58240. **/
  58241. CImg<intT> get_select(CImgDisplay &disp, const bool feature_type=true,
  58242. const char axis='x', const float align=0,
  58243. const bool exit_on_anykey=false) const {
  58244. return _select(disp,0,feature_type,axis,align,exit_on_anykey,0,false,false,false);
  58245. }
  58246. //! Display a simple interactive interface to select images or sublists.
  58247. /**
  58248. \param title Title of a new window used to display selection and user interface.
  58249. \param feature_type Can be \c false to select a single image, or \c true to select a sublist.
  58250. \param axis Axis along whom images are appended for visualization.
  58251. \param align Alignment setting when images have not all the same size.
  58252. \param exit_on_anykey Exit function when any key is pressed.
  58253. \return A one-column vector containing the selected image indexes.
  58254. **/
  58255. CImg<intT> get_select(const char *const title, const bool feature_type=true,
  58256. const char axis='x', const float align=0,
  58257. const bool exit_on_anykey=false) const {
  58258. CImgDisplay disp;
  58259. return _select(disp,title,feature_type,axis,align,exit_on_anykey,0,false,false,false);
  58260. }
  58261. CImg<intT> _select(CImgDisplay &disp, const char *const title, const bool feature_type,
  58262. const char axis, const float align, const bool exit_on_anykey,
  58263. const unsigned int orig, const bool resize_disp,
  58264. const bool exit_on_rightbutton, const bool exit_on_wheel) const {
  58265. if (is_empty())
  58266. throw CImgInstanceException(_cimglist_instance
  58267. "select(): Empty instance.",
  58268. cimglist_instance);
  58269. // Create image correspondence table and get list dimensions for visualization.
  58270. CImgList<uintT> _indices;
  58271. unsigned int max_width = 0, max_height = 0, sum_width = 0, sum_height = 0;
  58272. cimglist_for(*this,l) {
  58273. const CImg<T>& img = _data[l];
  58274. const unsigned int
  58275. w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
  58276. h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
  58277. if (w>max_width) max_width = w;
  58278. if (h>max_height) max_height = h;
  58279. sum_width+=w; sum_height+=h;
  58280. if (axis=='x') CImg<uintT>(w,1,1,1,(unsigned int)l).move_to(_indices);
  58281. else CImg<uintT>(h,1,1,1,(unsigned int)l).move_to(_indices);
  58282. }
  58283. const CImg<uintT> indices0 = _indices>'x';
  58284. // Create display window.
  58285. if (!disp) {
  58286. if (axis=='x') disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1);
  58287. else disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1);
  58288. if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width);
  58289. } else {
  58290. if (title) disp.set_title("%s",title);
  58291. disp.move_inside_screen();
  58292. }
  58293. if (resize_disp) {
  58294. if (axis=='x') disp.resize(cimg_fitscreen(sum_width,max_height,1),false);
  58295. else disp.resize(cimg_fitscreen(max_width,sum_height,1),false);
  58296. }
  58297. const unsigned int old_normalization = disp.normalization();
  58298. bool old_is_resized = disp.is_resized();
  58299. disp._normalization = 0;
  58300. disp.show().set_key(0).show_mouse();
  58301. static const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 };
  58302. // Enter event loop.
  58303. CImg<ucharT> visu0, visu;
  58304. CImg<uintT> indices;
  58305. CImg<intT> positions(_width,4,1,1,-1);
  58306. int oindex0 = -1, oindex1 = -1, index0 = -1, index1 = -1;
  58307. bool is_clicked = false, is_selected = false, text_down = false, update_display = true;
  58308. unsigned int key = 0, font_size = 32;
  58309. while (!is_selected && !disp.is_closed() && !key) {
  58310. // Create background image.
  58311. if (!visu0) {
  58312. visu0.assign(disp._width,disp._height,1,3,0); visu.assign();
  58313. (indices0.get_resize(axis=='x'?visu0._width:visu0._height,1)).move_to(indices);
  58314. unsigned int _ind = 0;
  58315. const CImg<T> onexone(1,1,1,1,(T)0);
  58316. if (axis=='x')
  58317. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,4))
  58318. cimglist_for(*this,ind) {
  58319. unsigned int x0 = 0;
  58320. while (x0<visu0._width && indices[x0++]!=(unsigned int)ind) {}
  58321. unsigned int x1 = x0;
  58322. while (x1<visu0._width && indices[x1++]==(unsigned int)ind) {}
  58323. const CImg<T> &src = _data[ind]?_data[ind]:onexone;
  58324. CImg<ucharT> res;
  58325. src._get_select(disp,old_normalization,src._width/2,src._height/2,src._depth/2).
  58326. move_to(res);
  58327. const unsigned int h = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,true);
  58328. res.resize(x1 - x0,std::max(32U,h*disp._height/max_height),1,res._spectrum==1?3:-100);
  58329. positions(ind,0) = positions(ind,2) = (int)x0;
  58330. positions(ind,1) = positions(ind,3) = (int)(align*(visu0.height() - res.height()));
  58331. positions(ind,2)+=res._width;
  58332. positions(ind,3)+=res._height - 1;
  58333. visu0.draw_image(positions(ind,0),positions(ind,1),res);
  58334. }
  58335. else
  58336. cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,4))
  58337. cimglist_for(*this,ind) {
  58338. unsigned int y0 = 0;
  58339. while (y0<visu0._height && indices[y0++]!=(unsigned int)ind) {}
  58340. unsigned int y1 = y0;
  58341. while (y1<visu0._height && indices[y1++]==(unsigned int)ind) {}
  58342. const CImg<T> &src = _data[ind]?_data[ind]:onexone;
  58343. CImg<ucharT> res;
  58344. src._get_select(disp,old_normalization,(src._width - 1)/2,(src._height - 1)/2,(src._depth - 1)/2).
  58345. move_to(res);
  58346. const unsigned int w = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,false);
  58347. res.resize(std::max(32U,w*disp._width/max_width),y1 - y0,1,res._spectrum==1?3:-100);
  58348. positions(ind,0) = positions(ind,2) = (int)(align*(visu0.width() - res.width()));
  58349. positions(ind,1) = positions(ind,3) = (int)y0;
  58350. positions(ind,2)+=res._width - 1;
  58351. positions(ind,3)+=res._height;
  58352. visu0.draw_image(positions(ind,0),positions(ind,1),res);
  58353. }
  58354. if (axis=='x') --positions(_ind,2); else --positions(_ind,3);
  58355. update_display = true;
  58356. }
  58357. if (!visu || oindex0!=index0 || oindex1!=index1) {
  58358. if (index0>=0 && index1>=0) {
  58359. visu.assign(visu0,false);
  58360. const int indm = std::min(index0,index1), indM = std::max(index0,index1);
  58361. for (int ind = indm; ind<=indM; ++ind) if (positions(ind,0)>=0) {
  58362. visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3),
  58363. background_color,0.2f);
  58364. if ((axis=='x' && positions(ind,2) - positions(ind,0)>=8) ||
  58365. (axis!='x' && positions(ind,3) - positions(ind,1)>=8))
  58366. visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3),
  58367. foreground_color,0.9f,0xAAAAAAAA);
  58368. }
  58369. if (is_clicked) visu.__draw_text(" Images #%u - #%u, Size = %u ",font_size,(int)text_down,
  58370. orig + indm,orig + indM,indM - indm + 1);
  58371. else visu.__draw_text(" Image #%u (%u,%u,%u,%u) ",font_size,(int)text_down,
  58372. orig + index0,
  58373. _data[index0]._width,
  58374. _data[index0]._height,
  58375. _data[index0]._depth,
  58376. _data[index0]._spectrum);
  58377. update_display = true;
  58378. } else visu.assign();
  58379. }
  58380. if (!visu) { visu.assign(visu0,true); update_display = true; }
  58381. if (update_display) { visu.display(disp); update_display = false; }
  58382. disp.wait();
  58383. // Manage user events.
  58384. const int xm = disp.mouse_x(), ym = disp.mouse_y();
  58385. int index = -1;
  58386. if (xm>=0) {
  58387. index = (int)indices(axis=='x'?xm:ym);
  58388. if (disp.button()&1) {
  58389. if (!is_clicked) { is_clicked = true; oindex0 = index0; index0 = index; }
  58390. oindex1 = index1; index1 = index;
  58391. if (!feature_type) is_selected = true;
  58392. } else {
  58393. if (!is_clicked) { oindex0 = oindex1 = index0; index0 = index1 = index; }
  58394. else is_selected = true;
  58395. }
  58396. } else {
  58397. if (is_clicked) {
  58398. if (!(disp.button()&1)) { is_clicked = is_selected = false; index0 = index1 = -1; }
  58399. else index1 = -1;
  58400. } else index0 = index1 = -1;
  58401. }
  58402. if (disp.button()&4) { is_clicked = is_selected = false; index0 = index1 = -1; }
  58403. if (disp.button()&2 && exit_on_rightbutton) { is_selected = true; index1 = index0 = -1; }
  58404. if (disp.wheel() && exit_on_wheel) is_selected = true;
  58405. CImg<charT> filename(32);
  58406. switch (key = disp.key()) {
  58407. #if cimg_OS!=2
  58408. case cimg::keyCTRLRIGHT :
  58409. #endif
  58410. case 0 : case cimg::keyCTRLLEFT : key = 0; break;
  58411. case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58412. disp.set_fullscreen(false).
  58413. resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
  58414. CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
  58415. _is_resized = true;
  58416. disp.set_key(key,false); key = 0; visu0.assign();
  58417. } break;
  58418. case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58419. disp.set_fullscreen(false).
  58420. resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
  58421. disp.set_key(key,false); key = 0; visu0.assign();
  58422. } break;
  58423. case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58424. disp.set_fullscreen(false).
  58425. resize(cimg_fitscreen(axis=='x'?sum_width:max_width,axis=='x'?max_height:sum_height,1),false).
  58426. _is_resized = true;
  58427. disp.set_key(key,false); key = 0; visu0.assign();
  58428. } break;
  58429. case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58430. disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
  58431. disp.set_key(key,false); key = 0; visu0.assign();
  58432. } break;
  58433. case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58434. static unsigned int snap_number = 0;
  58435. std::FILE *file;
  58436. do {
  58437. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
  58438. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  58439. } while (file);
  58440. if (visu0) {
  58441. (+visu0).__draw_text(" Saving snapshot... ",font_size,(int)text_down).display(disp);
  58442. visu0.save(filename);
  58443. (+visu0).__draw_text(" Snapshot '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
  58444. }
  58445. disp.set_key(key,false).wait(); key = 0;
  58446. } break;
  58447. case cimg::keyO :
  58448. if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
  58449. static unsigned int snap_number = 0;
  58450. std::FILE *file;
  58451. do {
  58452. #ifdef cimg_use_zlib
  58453. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
  58454. #else
  58455. cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
  58456. #endif
  58457. if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
  58458. } while (file);
  58459. (+visu0).__draw_text(" Saving instance... ",font_size,(int)text_down).display(disp);
  58460. save(filename);
  58461. (+visu0).__draw_text(" Instance '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
  58462. disp.set_key(key,false).wait(); key = 0;
  58463. } break;
  58464. }
  58465. if (disp.is_resized()) { disp.resize(false); visu0.assign(); }
  58466. if (ym>=0 && ym<13) { if (!text_down) { visu.assign(); text_down = true; }}
  58467. else if (ym>=visu.height() - 13) { if (text_down) { visu.assign(); text_down = false; }}
  58468. if (!exit_on_anykey && key && key!=cimg::keyESC &&
  58469. (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
  58470. key = 0;
  58471. }
  58472. }
  58473. CImg<intT> res(1,2,1,1,-1);
  58474. if (is_selected) {
  58475. if (feature_type) res.fill(std::min(index0,index1),std::max(index0,index1));
  58476. else res.fill(index0);
  58477. }
  58478. if (!(disp.button()&2)) disp.set_button();
  58479. disp._normalization = old_normalization;
  58480. disp._is_resized = old_is_resized;
  58481. disp.set_key(key);
  58482. return res;
  58483. }
  58484. //! Load a list from a file.
  58485. /**
  58486. \param filename Filename to read data from.
  58487. **/
  58488. CImgList<T>& load(const char *const filename) {
  58489. if (!filename)
  58490. throw CImgArgumentException(_cimglist_instance
  58491. "load(): Specified filename is (null).",
  58492. cimglist_instance);
  58493. if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) {
  58494. CImg<charT> filename_local(256);
  58495. load(cimg::load_network(filename,filename_local));
  58496. std::remove(filename_local);
  58497. return *this;
  58498. }
  58499. const bool is_stdin = *filename=='-' && (!filename[1] || filename[1]=='.');
  58500. const char *const ext = cimg::split_filename(filename);
  58501. const unsigned int omode = cimg::exception_mode();
  58502. cimg::exception_mode(0);
  58503. bool is_loaded = true;
  58504. try {
  58505. #ifdef cimglist_load_plugin
  58506. cimglist_load_plugin(filename);
  58507. #endif
  58508. #ifdef cimglist_load_plugin1
  58509. cimglist_load_plugin1(filename);
  58510. #endif
  58511. #ifdef cimglist_load_plugin2
  58512. cimglist_load_plugin2(filename);
  58513. #endif
  58514. #ifdef cimglist_load_plugin3
  58515. cimglist_load_plugin3(filename);
  58516. #endif
  58517. #ifdef cimglist_load_plugin4
  58518. cimglist_load_plugin4(filename);
  58519. #endif
  58520. #ifdef cimglist_load_plugin5
  58521. cimglist_load_plugin5(filename);
  58522. #endif
  58523. #ifdef cimglist_load_plugin6
  58524. cimglist_load_plugin6(filename);
  58525. #endif
  58526. #ifdef cimglist_load_plugin7
  58527. cimglist_load_plugin7(filename);
  58528. #endif
  58529. #ifdef cimglist_load_plugin8
  58530. cimglist_load_plugin8(filename);
  58531. #endif
  58532. if (!cimg::strcasecmp(ext,"tif") ||
  58533. !cimg::strcasecmp(ext,"tiff")) load_tiff(filename);
  58534. else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename);
  58535. else if (!cimg::strcasecmp(ext,"cimg") ||
  58536. !cimg::strcasecmp(ext,"cimgz") ||
  58537. !*ext) load_cimg(filename);
  58538. else if (!cimg::strcasecmp(ext,"rec") ||
  58539. !cimg::strcasecmp(ext,"par")) load_parrec(filename);
  58540. else if (!cimg::strcasecmp(ext,"avi") ||
  58541. !cimg::strcasecmp(ext,"mov") ||
  58542. !cimg::strcasecmp(ext,"asf") ||
  58543. !cimg::strcasecmp(ext,"divx") ||
  58544. !cimg::strcasecmp(ext,"flv") ||
  58545. !cimg::strcasecmp(ext,"mpg") ||
  58546. !cimg::strcasecmp(ext,"m1v") ||
  58547. !cimg::strcasecmp(ext,"m2v") ||
  58548. !cimg::strcasecmp(ext,"m4v") ||
  58549. !cimg::strcasecmp(ext,"mjp") ||
  58550. !cimg::strcasecmp(ext,"mp4") ||
  58551. !cimg::strcasecmp(ext,"mkv") ||
  58552. !cimg::strcasecmp(ext,"mpe") ||
  58553. !cimg::strcasecmp(ext,"movie") ||
  58554. !cimg::strcasecmp(ext,"ogm") ||
  58555. !cimg::strcasecmp(ext,"ogg") ||
  58556. !cimg::strcasecmp(ext,"ogv") ||
  58557. !cimg::strcasecmp(ext,"qt") ||
  58558. !cimg::strcasecmp(ext,"rm") ||
  58559. !cimg::strcasecmp(ext,"vob") ||
  58560. !cimg::strcasecmp(ext,"webm") ||
  58561. !cimg::strcasecmp(ext,"wmv") ||
  58562. !cimg::strcasecmp(ext,"xvid") ||
  58563. !cimg::strcasecmp(ext,"mpeg")) load_video(filename);
  58564. else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename);
  58565. else is_loaded = false;
  58566. } catch (CImgIOException&) { is_loaded = false; }
  58567. // If nothing loaded, try to guess file format from magic number in file.
  58568. if (!is_loaded && !is_stdin) {
  58569. std::FILE *const file = cimg::std_fopen(filename,"rb");
  58570. if (!file) {
  58571. cimg::exception_mode(omode);
  58572. throw CImgIOException(_cimglist_instance
  58573. "load(): Failed to open file '%s'.",
  58574. cimglist_instance,
  58575. filename);
  58576. }
  58577. const char *const f_type = cimg::ftype(file,filename);
  58578. cimg::fclose(file);
  58579. is_loaded = true;
  58580. try {
  58581. if (!cimg::strcasecmp(f_type,"gif")) load_gif_external(filename);
  58582. else if (!cimg::strcasecmp(f_type,"tif") &&
  58583. cimg::strcasecmp(ext,"nef") &&
  58584. cimg::strcasecmp(ext,"dng")) load_tiff(filename);
  58585. else is_loaded = false;
  58586. } catch (CImgIOException&) { is_loaded = false; }
  58587. }
  58588. // If nothing loaded, try to load file as a single image.
  58589. if (!is_loaded) {
  58590. assign(1);
  58591. try {
  58592. _data->load(filename);
  58593. } catch (CImgIOException&) {
  58594. cimg::exception_mode(omode);
  58595. throw CImgIOException(_cimglist_instance
  58596. "load(): Failed to recognize format of file '%s'.",
  58597. cimglist_instance,
  58598. filename);
  58599. }
  58600. }
  58601. cimg::exception_mode(omode);
  58602. return *this;
  58603. }
  58604. //! Load a list from a file \newinstance.
  58605. static CImgList<T> get_load(const char *const filename) {
  58606. return CImgList<T>().load(filename);
  58607. }
  58608. //! Load a list from a .cimg file.
  58609. /**
  58610. \param filename Filename to read data from.
  58611. **/
  58612. CImgList<T>& load_cimg(const char *const filename) {
  58613. return _load_cimg(0,filename);
  58614. }
  58615. //! Load a list from a .cimg file \newinstance.
  58616. static CImgList<T> get_load_cimg(const char *const filename) {
  58617. return CImgList<T>().load_cimg(filename);
  58618. }
  58619. //! Load a list from a .cimg file.
  58620. /**
  58621. \param file File to read data from.
  58622. **/
  58623. CImgList<T>& load_cimg(std::FILE *const file) {
  58624. return _load_cimg(file,0);
  58625. }
  58626. //! Load a list from a .cimg file \newinstance.
  58627. static CImgList<T> get_load_cimg(std::FILE *const file) {
  58628. return CImgList<T>().load_cimg(file);
  58629. }
  58630. CImgList<T>& _load_cimg(std::FILE *const file, const char *const filename) {
  58631. #ifdef cimg_use_zlib
  58632. #define _cimgz_load_cimg_case(Tss) { \
  58633. Bytef *const cbuf = new Bytef[csiz]; \
  58634. cimg::fread(cbuf,(size_t)csiz,nfile); \
  58635. if (is_bool) { \
  58636. CImg<ucharT> raw(W*H*D*C/8); \
  58637. uLongf destlen = (uLongf)raw.size(); \
  58638. uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
  58639. img.assign(W,H,D,C); \
  58640. img._uchar2bool(raw,raw.size(),false); \
  58641. } else { \
  58642. CImg<Tss> raw(W,H,D,C); \
  58643. uLongf destlen = (uLongf)(raw.size()*sizeof(Tss)); \
  58644. uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
  58645. if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
  58646. raw.move_to(img); \
  58647. } \
  58648. delete[] cbuf; \
  58649. }
  58650. #else
  58651. #define _cimgz_load_cimg_case(Tss) \
  58652. throw CImgIOException(_cimglist_instance \
  58653. "load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.", \
  58654. cimglist_instance, \
  58655. filename?filename:"(FILE*)");
  58656. #endif
  58657. #define _cimg_load_cimg_case(Ts,Tss) \
  58658. if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
  58659. const bool is_bool = cimg::type<Tss>::string()==cimg::type<bool>::string(); \
  58660. for (unsigned int l = 0; l<N; ++l) { \
  58661. j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0; \
  58662. W = H = D = C = 0; csiz = 0; \
  58663. if ((err = cimg_sscanf(tmp,"%u %u %u %u #" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \
  58664. throw CImgIOException(_cimglist_instance \
  58665. "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'.", \
  58666. cimglist_instance, \
  58667. W,H,D,C,l,filename?filename:("(FILE*)")); \
  58668. if (W*H*D*C>0) { \
  58669. CImg<T> &img = _data[l]; \
  58670. if (err==5) _cimgz_load_cimg_case(Tss) \
  58671. else { \
  58672. img.assign(W,H,D,C); \
  58673. T *ptrd = img._data; \
  58674. if (is_bool) { \
  58675. CImg<ucharT> raw; \
  58676. for (ulongT to_read = img.size(); to_read; ) { \
  58677. raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \
  58678. cimg::fread(raw._data,raw._width,nfile); \
  58679. CImg<T>(ptrd,std::min(8*raw._width,(unsigned int)(img.end() - ptrd)),1,1,1,true).\
  58680. _uchar2bool(raw,raw._width,false); \
  58681. to_read-=raw._width; \
  58682. } \
  58683. } else { \
  58684. CImg<Tss> raw; \
  58685. for (ulongT to_read = img.size(); to_read; ) { \
  58686. raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \
  58687. cimg::fread(raw._data,raw._width,nfile); \
  58688. if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
  58689. const Tss *ptrs = raw._data; \
  58690. for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \
  58691. to_read-=raw._width; \
  58692. } \
  58693. } \
  58694. } \
  58695. } \
  58696. } \
  58697. loaded = true; \
  58698. }
  58699. if (!filename && !file)
  58700. throw CImgArgumentException(_cimglist_instance
  58701. "load_cimg(): Specified filename is (null).",
  58702. cimglist_instance);
  58703. const ulongT cimg_iobuffer = (ulongT)24*1024*1024;
  58704. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  58705. bool loaded = false, endian = cimg::endianness();
  58706. CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
  58707. *tmp = *str_pixeltype = *str_endian = 0;
  58708. unsigned int j, N = 0, W, H, D, C;
  58709. cimg_uint64 csiz;
  58710. int i, err;
  58711. do {
  58712. j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0;
  58713. } while (*tmp=='#' && i>=0);
  58714. err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
  58715. &N,str_pixeltype._data,str_endian._data);
  58716. if (err<2) {
  58717. if (!file) cimg::fclose(nfile);
  58718. throw CImgIOException(_cimglist_instance
  58719. "load_cimg(): File or CImg header not found in file '%s'.",
  58720. cimglist_instance,
  58721. filename?filename:"(FILE*)");
  58722. }
  58723. if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
  58724. else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
  58725. assign(N);
  58726. _cimg_load_cimg_case("bool",bool);
  58727. _cimg_load_cimg_case("unsigned_char",unsigned char);
  58728. _cimg_load_cimg_case("uchar",unsigned char);
  58729. _cimg_load_cimg_case("char",char);
  58730. _cimg_load_cimg_case("unsigned_short",unsigned short);
  58731. _cimg_load_cimg_case("ushort",unsigned short);
  58732. _cimg_load_cimg_case("short",short);
  58733. _cimg_load_cimg_case("unsigned_int",unsigned int);
  58734. _cimg_load_cimg_case("uint",unsigned int);
  58735. _cimg_load_cimg_case("int",int);
  58736. _cimg_load_cimg_case("unsigned_long",ulongT);
  58737. _cimg_load_cimg_case("ulong",ulongT);
  58738. _cimg_load_cimg_case("long",longT);
  58739. _cimg_load_cimg_case("unsigned_int64",uint64T);
  58740. _cimg_load_cimg_case("uint64",uint64T);
  58741. _cimg_load_cimg_case("int64",int64T);
  58742. _cimg_load_cimg_case("float",float);
  58743. _cimg_load_cimg_case("double",double);
  58744. if (!loaded) {
  58745. if (!file) cimg::fclose(nfile);
  58746. throw CImgIOException(_cimglist_instance
  58747. "load_cimg(): Unsupported pixel type '%s' for file '%s'.",
  58748. cimglist_instance,
  58749. str_pixeltype._data,filename?filename:"(FILE*)");
  58750. }
  58751. if (!file) cimg::fclose(nfile);
  58752. return *this;
  58753. }
  58754. //! Load a sublist list from a (non compressed) .cimg file.
  58755. /**
  58756. \param filename Filename to read data from.
  58757. \param n0 Starting index of images to read (~0U for max).
  58758. \param n1 Ending index of images to read (~0U for max).
  58759. \param x0 Starting X-coordinates of image regions to read.
  58760. \param y0 Starting Y-coordinates of image regions to read.
  58761. \param z0 Starting Z-coordinates of image regions to read.
  58762. \param c0 Starting C-coordinates of image regions to read.
  58763. \param x1 Ending X-coordinates of image regions to read (~0U for max).
  58764. \param y1 Ending Y-coordinates of image regions to read (~0U for max).
  58765. \param z1 Ending Z-coordinates of image regions to read (~0U for max).
  58766. \param c1 Ending C-coordinates of image regions to read (~0U for max).
  58767. **/
  58768. CImgList<T>& load_cimg(const char *const filename,
  58769. const unsigned int n0, const unsigned int n1,
  58770. const unsigned int x0, const unsigned int y0,
  58771. const unsigned int z0, const unsigned int c0,
  58772. const unsigned int x1, const unsigned int y1,
  58773. const unsigned int z1, const unsigned int c1) {
  58774. return _load_cimg(0,filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  58775. }
  58776. //! Load a sublist list from a (non compressed) .cimg file \newinstance.
  58777. static CImgList<T> get_load_cimg(const char *const filename,
  58778. const unsigned int n0, const unsigned int n1,
  58779. const unsigned int x0, const unsigned int y0,
  58780. const unsigned int z0, const unsigned int c0,
  58781. const unsigned int x1, const unsigned int y1,
  58782. const unsigned int z1, const unsigned int c1) {
  58783. return CImgList<T>().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  58784. }
  58785. //! Load a sub-image list from a (non compressed) .cimg file \overloading.
  58786. CImgList<T>& load_cimg(std::FILE *const file,
  58787. const unsigned int n0, const unsigned int n1,
  58788. const unsigned int x0, const unsigned int y0,
  58789. const unsigned int z0, const unsigned int c0,
  58790. const unsigned int x1, const unsigned int y1,
  58791. const unsigned int z1, const unsigned int c1) {
  58792. return _load_cimg(file,0,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  58793. }
  58794. //! Load a sub-image list from a (non compressed) .cimg file \newinstance.
  58795. static CImgList<T> get_load_cimg(std::FILE *const file,
  58796. const unsigned int n0, const unsigned int n1,
  58797. const unsigned int x0, const unsigned int y0,
  58798. const unsigned int z0, const unsigned int c0,
  58799. const unsigned int x1, const unsigned int y1,
  58800. const unsigned int z1, const unsigned int c1) {
  58801. return CImgList<T>().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
  58802. }
  58803. CImgList<T>& _load_cimg(std::FILE *const file, const char *const filename,
  58804. const unsigned int n0, const unsigned int n1,
  58805. const unsigned int x0, const unsigned int y0,
  58806. const unsigned int z0, const unsigned int c0,
  58807. const unsigned int x1, const unsigned int y1,
  58808. const unsigned int z1, const unsigned int c1) {
  58809. #define _cimg_load_cimg_case2(Ts,Tss) \
  58810. if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
  58811. for (unsigned int l = 0; l<=nn1; ++l) { \
  58812. j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \
  58813. W = H = D = C = 0; \
  58814. if (cimg_sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \
  58815. throw CImgIOException(_cimglist_instance \
  58816. "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \
  58817. cimglist_instance, \
  58818. W,H,D,C,l,filename?filename:"(FILE*)"); \
  58819. if (W*H*D*C>0) { \
  58820. if (l<nn0 || nx0>=W || ny0>=H || nz0>=D || nc0>=C) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \
  58821. else { \
  58822. const unsigned int \
  58823. _nx1 = nx1==~0U?W - 1:nx1, \
  58824. _ny1 = ny1==~0U?H - 1:ny1, \
  58825. _nz1 = nz1==~0U?D - 1:nz1, \
  58826. _nc1 = nc1==~0U?C - 1:nc1; \
  58827. if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \
  58828. throw CImgArgumentException(_cimglist_instance \
  58829. "load_cimg(): Invalid specified coordinates " \
  58830. "[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " \
  58831. "because image [%u] in file '%s' has size (%u,%u,%u,%u).", \
  58832. cimglist_instance, \
  58833. n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:"(FILE*)",W,H,D,C); \
  58834. CImg<Tss> raw(1 + _nx1 - nx0); \
  58835. CImg<T> &img = _data[l - nn0]; \
  58836. img.assign(1 + _nx1 - nx0,1 + _ny1 - ny0,1 + _nz1 - nz0,1 + _nc1 - nc0); \
  58837. T *ptrd = img._data; \
  58838. ulongT skipvb = nc0*W*H*D*sizeof(Tss); \
  58839. if (skipvb) cimg::fseek(nfile,skipvb,SEEK_CUR); \
  58840. for (unsigned int c = 1 + _nc1 - nc0; c; --c) { \
  58841. const ulongT skipzb = nz0*W*H*sizeof(Tss); \
  58842. if (skipzb) cimg::fseek(nfile,skipzb,SEEK_CUR); \
  58843. for (unsigned int z = 1 + _nz1 - nz0; z; --z) { \
  58844. const ulongT skipyb = ny0*W*sizeof(Tss); \
  58845. if (skipyb) cimg::fseek(nfile,skipyb,SEEK_CUR); \
  58846. for (unsigned int y = 1 + _ny1 - ny0; y; --y) { \
  58847. const ulongT skipxb = nx0*sizeof(Tss); \
  58848. if (skipxb) cimg::fseek(nfile,skipxb,SEEK_CUR); \
  58849. cimg::fread(raw._data,raw._width,nfile); \
  58850. if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \
  58851. const Tss *ptrs = raw._data; \
  58852. for (unsigned int off = raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \
  58853. const ulongT skipxe = (W - 1 - _nx1)*sizeof(Tss); \
  58854. if (skipxe) cimg::fseek(nfile,skipxe,SEEK_CUR); \
  58855. } \
  58856. const ulongT skipye = (H - 1 - _ny1)*W*sizeof(Tss); \
  58857. if (skipye) cimg::fseek(nfile,skipye,SEEK_CUR); \
  58858. } \
  58859. const ulongT skipze = (D - 1 - _nz1)*W*H*sizeof(Tss); \
  58860. if (skipze) cimg::fseek(nfile,skipze,SEEK_CUR); \
  58861. } \
  58862. const ulongT skipve = (C - 1 - _nc1)*W*H*D*sizeof(Tss); \
  58863. if (skipve) cimg::fseek(nfile,skipve,SEEK_CUR); \
  58864. } \
  58865. } \
  58866. } \
  58867. loaded = true; \
  58868. }
  58869. if (!filename && !file)
  58870. throw CImgArgumentException(_cimglist_instance
  58871. "load_cimg(): Specified filename is (null).",
  58872. cimglist_instance);
  58873. unsigned int
  58874. nn0 = std::min(n0,n1), nn1 = std::max(n0,n1),
  58875. nx0 = std::min(x0,x1), nx1 = std::max(x0,x1),
  58876. ny0 = std::min(y0,y1), ny1 = std::max(y0,y1),
  58877. nz0 = std::min(z0,z1), nz1 = std::max(z0,z1),
  58878. nc0 = std::min(c0,c1), nc1 = std::max(c0,c1);
  58879. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  58880. bool loaded = false, endian = cimg::endianness();
  58881. CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
  58882. *tmp = *str_pixeltype = *str_endian = 0;
  58883. unsigned int j, N, W, H, D, C;
  58884. int i, err;
  58885. j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
  58886. err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
  58887. &N,str_pixeltype._data,str_endian._data);
  58888. if (err<2) {
  58889. if (!file) cimg::fclose(nfile);
  58890. throw CImgIOException(_cimglist_instance
  58891. "load_cimg(): CImg header not found in file '%s'.",
  58892. cimglist_instance,
  58893. filename?filename:"(FILE*)");
  58894. }
  58895. if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
  58896. else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
  58897. nn1 = n1==~0U?N - 1:n1;
  58898. if (nn1>=N)
  58899. throw CImgArgumentException(_cimglist_instance
  58900. "load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) "
  58901. "because file '%s' contains only %u images.",
  58902. cimglist_instance,
  58903. n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:"(FILE*)",N);
  58904. assign(1 + nn1 - n0);
  58905. _cimg_load_cimg_case2("bool",bool);
  58906. _cimg_load_cimg_case2("unsigned_char",unsigned char);
  58907. _cimg_load_cimg_case2("uchar",unsigned char);
  58908. _cimg_load_cimg_case2("char",char);
  58909. _cimg_load_cimg_case2("unsigned_short",unsigned short);
  58910. _cimg_load_cimg_case2("ushort",unsigned short);
  58911. _cimg_load_cimg_case2("short",short);
  58912. _cimg_load_cimg_case2("unsigned_int",unsigned int);
  58913. _cimg_load_cimg_case2("uint",unsigned int);
  58914. _cimg_load_cimg_case2("int",int);
  58915. _cimg_load_cimg_case2("unsigned_long",ulongT);
  58916. _cimg_load_cimg_case2("ulong",ulongT);
  58917. _cimg_load_cimg_case2("long",longT);
  58918. _cimg_load_cimg_case2("unsigned_int64",uint64T);
  58919. _cimg_load_cimg_case2("uint64",uint64T);
  58920. _cimg_load_cimg_case2("int64",int64T);
  58921. _cimg_load_cimg_case2("float",float);
  58922. _cimg_load_cimg_case2("double",double);
  58923. if (!loaded) {
  58924. if (!file) cimg::fclose(nfile);
  58925. throw CImgIOException(_cimglist_instance
  58926. "load_cimg(): Unsupported pixel type '%s' for file '%s'.",
  58927. cimglist_instance,
  58928. str_pixeltype._data,filename?filename:"(FILE*)");
  58929. }
  58930. if (!file) cimg::fclose(nfile);
  58931. return *this;
  58932. }
  58933. //! Load a list from a PAR/REC (Philips) file.
  58934. /**
  58935. \param filename Filename to read data from.
  58936. **/
  58937. CImgList<T>& load_parrec(const char *const filename) {
  58938. if (!filename)
  58939. throw CImgArgumentException(_cimglist_instance
  58940. "load_parrec(): Specified filename is (null).",
  58941. cimglist_instance);
  58942. CImg<charT> body(1024), filenamepar(1024), filenamerec(1024);
  58943. *body = *filenamepar = *filenamerec = 0;
  58944. const char *const ext = cimg::split_filename(filename,body);
  58945. if (!std::strcmp(ext,"par")) {
  58946. std::strncpy(filenamepar,filename,filenamepar._width - 1);
  58947. cimg_snprintf(filenamerec,filenamerec._width,"%s.rec",body._data);
  58948. }
  58949. if (!std::strcmp(ext,"PAR")) {
  58950. std::strncpy(filenamepar,filename,filenamepar._width - 1);
  58951. cimg_snprintf(filenamerec,filenamerec._width,"%s.REC",body._data);
  58952. }
  58953. if (!std::strcmp(ext,"rec")) {
  58954. std::strncpy(filenamerec,filename,filenamerec._width - 1);
  58955. cimg_snprintf(filenamepar,filenamepar._width,"%s.par",body._data);
  58956. }
  58957. if (!std::strcmp(ext,"REC")) {
  58958. std::strncpy(filenamerec,filename,filenamerec._width - 1);
  58959. cimg_snprintf(filenamepar,filenamepar._width,"%s.PAR",body._data);
  58960. }
  58961. std::FILE *file = cimg::fopen(filenamepar,"r");
  58962. // Parse header file
  58963. CImgList<floatT> st_slices;
  58964. CImgList<uintT> st_global;
  58965. CImg<charT> line(256); *line = 0;
  58966. int err;
  58967. do { err = std::fscanf(file,"%255[^\n]%*c",line._data); } while (err!=EOF && (*line=='#' || *line=='.'));
  58968. do {
  58969. unsigned int sn,size_x,size_y,pixsize;
  58970. float rs,ri,ss;
  58971. err = std::fscanf(file,"%u%*u%*u%*u%*u%*u%*u%u%*u%u%u%g%g%g%*[^\n]",&sn,&pixsize,&size_x,&size_y,&ri,&rs,&ss);
  58972. if (err==7) {
  58973. CImg<floatT>::vector((float)sn,(float)pixsize,(float)size_x,(float)size_y,ri,rs,ss,0).move_to(st_slices);
  58974. unsigned int i; for (i = 0; i<st_global._width && sn<=st_global[i][2]; ++i) {}
  58975. if (i==st_global._width) CImg<uintT>::vector(size_x,size_y,sn).move_to(st_global);
  58976. else {
  58977. CImg<uintT> &vec = st_global[i];
  58978. if (size_x>vec[0]) vec[0] = size_x;
  58979. if (size_y>vec[1]) vec[1] = size_y;
  58980. vec[2] = sn;
  58981. }
  58982. st_slices[st_slices._width - 1][7] = (float)i;
  58983. }
  58984. } while (err==7);
  58985. // Read data
  58986. std::FILE *file2 = cimg::fopen(filenamerec,"rb");
  58987. cimglist_for(st_global,l) {
  58988. const CImg<uintT>& vec = st_global[l];
  58989. CImg<T>(vec[0],vec[1],vec[2]).move_to(*this);
  58990. }
  58991. cimglist_for(st_slices,l) {
  58992. const CImg<floatT>& vec = st_slices[l];
  58993. const unsigned int
  58994. sn = (unsigned int)vec[0] - 1,
  58995. pixsize = (unsigned int)vec[1],
  58996. size_x = (unsigned int)vec[2],
  58997. size_y = (unsigned int)vec[3],
  58998. imn = (unsigned int)vec[7];
  58999. const float ri = vec[4], rs = vec[5], ss = vec[6];
  59000. switch (pixsize) {
  59001. case 8 : {
  59002. CImg<ucharT> buf(size_x,size_y);
  59003. cimg::fread(buf._data,size_x*size_y,file2);
  59004. if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
  59005. CImg<T>& img = (*this)[imn];
  59006. cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
  59007. } break;
  59008. case 16 : {
  59009. CImg<ushortT> buf(size_x,size_y);
  59010. cimg::fread(buf._data,size_x*size_y,file2);
  59011. if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
  59012. CImg<T>& img = (*this)[imn];
  59013. cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
  59014. } break;
  59015. case 32 : {
  59016. CImg<uintT> buf(size_x,size_y);
  59017. cimg::fread(buf._data,size_x*size_y,file2);
  59018. if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
  59019. CImg<T>& img = (*this)[imn];
  59020. cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
  59021. } break;
  59022. default :
  59023. cimg::fclose(file);
  59024. cimg::fclose(file2);
  59025. throw CImgIOException(_cimglist_instance
  59026. "load_parrec(): Unsupported %d-bits pixel type for file '%s'.",
  59027. cimglist_instance,
  59028. pixsize,filename);
  59029. }
  59030. }
  59031. cimg::fclose(file);
  59032. cimg::fclose(file2);
  59033. if (!_width)
  59034. throw CImgIOException(_cimglist_instance
  59035. "load_parrec(): Failed to recognize valid PAR-REC data in file '%s'.",
  59036. cimglist_instance,
  59037. filename);
  59038. return *this;
  59039. }
  59040. //! Load a list from a PAR/REC (Philips) file \newinstance.
  59041. static CImgList<T> get_load_parrec(const char *const filename) {
  59042. return CImgList<T>().load_parrec(filename);
  59043. }
  59044. //! Load a list from a YUV image sequence file.
  59045. /**
  59046. \param filename Filename to read data from.
  59047. \param size_x Width of the images.
  59048. \param size_y Height of the images.
  59049. \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
  59050. \param first_frame Index of first image frame to read.
  59051. \param last_frame Index of last image frame to read.
  59052. \param step_frame Step applied between each frame.
  59053. \param yuv2rgb Apply YUV to RGB transformation during reading.
  59054. **/
  59055. CImgList<T>& load_yuv(const char *const filename,
  59056. const unsigned int size_x, const unsigned int size_y,
  59057. const unsigned int chroma_subsampling=444,
  59058. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59059. const unsigned int step_frame=1, const bool yuv2rgb=true) {
  59060. return _load_yuv(0,filename,size_x,size_y,chroma_subsampling,
  59061. first_frame,last_frame,step_frame,yuv2rgb);
  59062. }
  59063. //! Load a list from a YUV image sequence file \newinstance.
  59064. static CImgList<T> get_load_yuv(const char *const filename,
  59065. const unsigned int size_x, const unsigned int size_y=1,
  59066. const unsigned int chroma_subsampling=444,
  59067. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59068. const unsigned int step_frame=1, const bool yuv2rgb=true) {
  59069. return CImgList<T>().load_yuv(filename,size_x,size_y,chroma_subsampling,
  59070. first_frame,last_frame,step_frame,yuv2rgb);
  59071. }
  59072. //! Load a list from an image sequence YUV file \overloading.
  59073. CImgList<T>& load_yuv(std::FILE *const file,
  59074. const unsigned int size_x, const unsigned int size_y,
  59075. const unsigned int chroma_subsampling=444,
  59076. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59077. const unsigned int step_frame=1, const bool yuv2rgb=true) {
  59078. return _load_yuv(file,0,size_x,size_y,chroma_subsampling,
  59079. first_frame,last_frame,step_frame,yuv2rgb);
  59080. }
  59081. //! Load a list from an image sequence YUV file \newinstance.
  59082. static CImgList<T> get_load_yuv(std::FILE *const file,
  59083. const unsigned int size_x, const unsigned int size_y=1,
  59084. const unsigned int chroma_subsampling=444,
  59085. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59086. const unsigned int step_frame=1, const bool yuv2rgb=true) {
  59087. return CImgList<T>().load_yuv(file,size_x,size_y,chroma_subsampling,
  59088. first_frame,last_frame,step_frame,yuv2rgb);
  59089. }
  59090. CImgList<T>& _load_yuv(std::FILE *const file, const char *const filename,
  59091. const unsigned int size_x, const unsigned int size_y,
  59092. const unsigned int chroma_subsampling,
  59093. const unsigned int first_frame, const unsigned int last_frame,
  59094. const unsigned int step_frame, const bool yuv2rgb) {
  59095. if (!filename && !file)
  59096. throw CImgArgumentException(_cimglist_instance
  59097. "load_yuv(): Specified filename is (null).",
  59098. cimglist_instance);
  59099. if (chroma_subsampling!=420 && chroma_subsampling!=422 && chroma_subsampling!=444)
  59100. throw CImgArgumentException(_cimglist_instance
  59101. "load_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.",
  59102. cimglist_instance,
  59103. chroma_subsampling,filename?filename:"(FILE*)");
  59104. const unsigned int
  59105. cfx = chroma_subsampling==420 || chroma_subsampling==422?2:1,
  59106. cfy = chroma_subsampling==420?2:1,
  59107. nfirst_frame = first_frame<last_frame?first_frame:last_frame,
  59108. nlast_frame = first_frame<last_frame?last_frame:first_frame,
  59109. nstep_frame = step_frame?step_frame:1;
  59110. if (!size_x || !size_y || size_x%cfx || size_y%cfy)
  59111. throw CImgArgumentException(_cimglist_instance
  59112. "load_yuv(): Specified dimensions (%u,%u) are invalid, for file '%s'.",
  59113. cimglist_instance,
  59114. size_x,size_y,filename?filename:"(FILE*)");
  59115. CImg<ucharT> YUV(size_x,size_y,1,3), UV(size_x/cfx,size_y/cfy,1,2);
  59116. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
  59117. bool stop_flag = false;
  59118. int err;
  59119. if (nfirst_frame) {
  59120. err = cimg::fseek(nfile,(uint64T)nfirst_frame*(YUV._width*YUV._height + 2*UV._width*UV._height),SEEK_CUR);
  59121. if (err) {
  59122. if (!file) cimg::fclose(nfile);
  59123. throw CImgIOException(_cimglist_instance
  59124. "load_yuv(): File '%s' doesn't contain frame number %u.",
  59125. cimglist_instance,
  59126. filename?filename:"(FILE*)",nfirst_frame);
  59127. }
  59128. }
  59129. unsigned int frame;
  59130. for (frame = nfirst_frame; !stop_flag && frame<=nlast_frame; frame+=nstep_frame) {
  59131. YUV.get_shared_channel(0).fill(0);
  59132. // *TRY* to read the luminance part, do not replace by cimg::fread!
  59133. err = (int)std::fread((void*)(YUV._data),1,(size_t)YUV._width*YUV._height,nfile);
  59134. if (err!=(int)(YUV._width*YUV._height)) {
  59135. stop_flag = true;
  59136. if (err>0)
  59137. cimg::warn(_cimglist_instance
  59138. "load_yuv(): File '%s' contains incomplete data or given image dimensions "
  59139. "(%u,%u) are incorrect.",
  59140. cimglist_instance,
  59141. filename?filename:"(FILE*)",size_x,size_y);
  59142. } else {
  59143. UV.fill(0);
  59144. // *TRY* to read the luminance part, do not replace by cimg::fread!
  59145. err = (int)std::fread((void*)(UV._data),1,(size_t)UV.size(),nfile);
  59146. if (err!=(int)(UV.size())) {
  59147. stop_flag = true;
  59148. if (err>0)
  59149. cimg::warn(_cimglist_instance
  59150. "load_yuv(): File '%s' contains incomplete data or given image dimensions "
  59151. "(%u,%u) are incorrect.",
  59152. cimglist_instance,
  59153. filename?filename:"(FILE*)",size_x,size_y);
  59154. } else {
  59155. const ucharT *ptrs1 = UV._data, *ptrs2 = UV.data(0,0,0,1);
  59156. ucharT *ptrd1 = YUV.data(0,0,0,1), *ptrd2 = YUV.data(0,0,0,2);
  59157. const unsigned int wd = YUV._width;
  59158. switch (chroma_subsampling) {
  59159. case 420 :
  59160. cimg_forY(UV,y) {
  59161. cimg_forX(UV,x) {
  59162. const ucharT U = *(ptrs1++), V = *(ptrs2++);
  59163. ptrd1[wd] = U; *(ptrd1)++ = U;
  59164. ptrd1[wd] = U; *(ptrd1)++ = U;
  59165. ptrd2[wd] = V; *(ptrd2)++ = V;
  59166. ptrd2[wd] = V; *(ptrd2)++ = V;
  59167. }
  59168. ptrd1+=wd; ptrd2+=wd;
  59169. }
  59170. break;
  59171. case 422 :
  59172. cimg_forXY(UV,x,y) {
  59173. const ucharT U = *(ptrs1++), V = *(ptrs2++);
  59174. *(ptrd1++) = U; *(ptrd1++) = U;
  59175. *(ptrd2++) = V; *(ptrd2++) = V;
  59176. }
  59177. break;
  59178. default :
  59179. YUV.draw_image(0,0,0,1,UV);
  59180. }
  59181. if (yuv2rgb) YUV.YCbCrtoRGB();
  59182. insert(YUV);
  59183. if (nstep_frame>1) cimg::fseek(nfile,(uint64T)(nstep_frame - 1)*(size_x*size_y + size_x*size_y/2),SEEK_CUR);
  59184. }
  59185. }
  59186. }
  59187. if (is_empty())
  59188. throw CImgIOException(_cimglist_instance
  59189. "load_yuv() : Missing data in file '%s'.",
  59190. cimglist_instance,
  59191. filename?filename:"(FILE*)");
  59192. if (stop_flag && nlast_frame!=~0U && frame!=nlast_frame)
  59193. cimg::warn(_cimglist_instance
  59194. "load_yuv(): Frame %d not reached since only %u frames were found in file '%s'.",
  59195. cimglist_instance,
  59196. nlast_frame,frame - 1,filename?filename:"(FILE*)");
  59197. if (!file) cimg::fclose(nfile);
  59198. return *this;
  59199. }
  59200. //! Load an image from a video file, using OpenCV library.
  59201. /**
  59202. \param filename Filename, as a C-string.
  59203. \param first_frame Index of the first frame to read.
  59204. \param last_frame Index of the last frame to read (can be higher than the actual number of frames, e.g. '~0U').
  59205. \param step_frame Step value for frame reading.
  59206. \note If step_frame==0, the current video stream is forced to be released (without any frames read).
  59207. **/
  59208. CImgList<T>& load_video(const char *const filename,
  59209. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59210. const unsigned int step_frame=1) {
  59211. #ifndef cimg_use_opencv
  59212. if (first_frame || last_frame!=~0U || step_frame>1)
  59213. throw CImgArgumentException(_cimglist_instance
  59214. "load_video() : File '%s', arguments 'first_frame', 'last_frame' "
  59215. "and 'step_frame' requires features from the OpenCV library "
  59216. "('-Dcimg_use_opencv' must be defined).",
  59217. cimglist_instance,filename);
  59218. return load_ffmpeg_external(filename);
  59219. #else
  59220. static cv::VideoCapture *captures[32] = { 0 };
  59221. static CImgList<charT> filenames(32);
  59222. static CImg<uintT> positions(32,1,1,1,0);
  59223. static int last_used_index = -1;
  59224. // Detect if a video capture already exists for the specified filename.
  59225. cimg::mutex(9);
  59226. int index = -1;
  59227. if (filename) {
  59228. if (last_used_index>=0 && !std::strcmp(filename,filenames[last_used_index])) {
  59229. index = last_used_index;
  59230. } else cimglist_for(filenames,l) if (filenames[l] && !std::strcmp(filename,filenames[l])) {
  59231. index = l; break;
  59232. }
  59233. } else index = last_used_index;
  59234. cimg::mutex(9,0);
  59235. // Release stream if needed.
  59236. if (!step_frame || (index>=0 && positions[index]>first_frame)) {
  59237. if (index>=0) {
  59238. cimg::mutex(9);
  59239. captures[index]->release();
  59240. delete captures[index];
  59241. captures[index] = 0;
  59242. positions[index] = 0;
  59243. filenames[index].assign();
  59244. if (last_used_index==index) last_used_index = -1;
  59245. index = -1;
  59246. cimg::mutex(9,0);
  59247. } else
  59248. if (filename)
  59249. cimg::warn(_cimglist_instance
  59250. "load_video() : File '%s', no opened video stream associated with filename found.",
  59251. cimglist_instance,filename);
  59252. else
  59253. cimg::warn(_cimglist_instance
  59254. "load_video() : No opened video stream found.",
  59255. cimglist_instance,filename);
  59256. if (!step_frame) return *this;
  59257. }
  59258. // Find empty slot for capturing video stream.
  59259. if (index<0) {
  59260. if (!filename)
  59261. throw CImgArgumentException(_cimglist_instance
  59262. "load_video(): No already open video reader found. You must specify a "
  59263. "non-(null) filename argument for the first call.",
  59264. cimglist_instance);
  59265. else { cimg::mutex(9); cimglist_for(filenames,l) if (!filenames[l]) { index = l; break; } cimg::mutex(9,0); }
  59266. if (index<0)
  59267. throw CImgIOException(_cimglist_instance
  59268. "load_video(): File '%s', no video reader slots available. "
  59269. "You have to release some of your previously opened videos.",
  59270. cimglist_instance,filename);
  59271. cimg::mutex(9);
  59272. captures[index] = new cv::VideoCapture(filename);
  59273. positions[index] = 0;
  59274. if (!captures[index]->isOpened()) {
  59275. delete captures[index];
  59276. captures[index] = 0;
  59277. cimg::mutex(9,0);
  59278. cimg::fclose(cimg::fopen(filename,"rb")); // Check file availability
  59279. throw CImgIOException(_cimglist_instance
  59280. "load_video(): File '%s', unable to detect format of video file.",
  59281. cimglist_instance,filename);
  59282. }
  59283. CImg<charT>::string(filename).move_to(filenames[index]);
  59284. cimg::mutex(9,0);
  59285. }
  59286. cimg::mutex(9);
  59287. const unsigned int nb_frames = (unsigned int)std::max(0.,captures[index]->get(_cimg_cap_prop_frame_count));
  59288. cimg::mutex(9,0);
  59289. assign();
  59290. // Skip frames if requested.
  59291. bool go_on = true;
  59292. unsigned int &pos = positions[index];
  59293. while (pos<first_frame) {
  59294. cimg::mutex(9);
  59295. if (!captures[index]->grab()) { cimg::mutex(9,0); go_on = false; break; }
  59296. cimg::mutex(9,0);
  59297. ++pos;
  59298. }
  59299. // Read and convert frames.
  59300. const unsigned int _last_frame = std::min(nb_frames?nb_frames - 1:~0U,last_frame);
  59301. while (go_on && pos<=_last_frame) {
  59302. cv::Mat cvimg;
  59303. cimg::mutex(9);
  59304. if (captures[index]->read(cvimg)) { CImg<T>::_cvmat2cimg(cvimg).move_to(*this); ++pos; }
  59305. else go_on = false;
  59306. cimg::mutex(9,0);
  59307. if (go_on)
  59308. for (unsigned int i = 1; go_on && i<step_frame && pos<=_last_frame; ++i, ++pos) {
  59309. cimg::mutex(9);
  59310. if (!captures[index]->grab()) go_on = false;
  59311. cimg::mutex(9,0);
  59312. }
  59313. }
  59314. if (!go_on || (nb_frames && pos>=nb_frames)) { // Close video stream when necessary
  59315. cimg::mutex(9);
  59316. captures[index]->release();
  59317. delete captures[index];
  59318. captures[index] = 0;
  59319. filenames[index].assign();
  59320. positions[index] = 0;
  59321. index = -1;
  59322. cimg::mutex(9,0);
  59323. }
  59324. cimg::mutex(9);
  59325. last_used_index = index;
  59326. cimg::mutex(9,0);
  59327. if (is_empty())
  59328. throw CImgIOException(_cimglist_instance
  59329. "load_video(): File '%s', unable to locate frame %u.",
  59330. cimglist_instance,filename,first_frame);
  59331. return *this;
  59332. #endif
  59333. }
  59334. //! Load an image from a video file, using OpenCV library \newinstance.
  59335. static CImgList<T> get_load_video(const char *const filename,
  59336. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59337. const unsigned int step_frame=1) {
  59338. return CImgList<T>().load_video(filename,first_frame,last_frame,step_frame);
  59339. }
  59340. //! Load an image from a video file using the external tool 'ffmpeg'.
  59341. /**
  59342. \param filename Filename to read data from.
  59343. **/
  59344. CImgList<T>& load_ffmpeg_external(const char *const filename) {
  59345. if (!filename)
  59346. throw CImgArgumentException(_cimglist_instance
  59347. "load_ffmpeg_external(): Specified filename is (null).",
  59348. cimglist_instance);
  59349. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  59350. CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
  59351. std::FILE *file = 0;
  59352. do {
  59353. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  59354. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  59355. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001.ppm",filename_tmp._data);
  59356. if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
  59357. } while (file);
  59358. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%%6d.ppm",filename_tmp._data);
  59359. cimg_snprintf(command,command._width,"\"%s\" -v -8 -i \"%s\" \"%s\"",
  59360. cimg::ffmpeg_path(),
  59361. CImg<charT>::string(filename)._system_strescape().data(),
  59362. CImg<charT>::string(filename_tmp2)._system_strescape().data());
  59363. cimg::system(command, cimg::ffmpeg_path());
  59364. const unsigned int omode = cimg::exception_mode();
  59365. cimg::exception_mode(0);
  59366. assign();
  59367. unsigned int i = 1;
  59368. for (bool stop_flag = false; !stop_flag; ++i) {
  59369. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u.ppm",filename_tmp._data,i);
  59370. CImg<T> img;
  59371. try { img.load_pnm(filename_tmp2); }
  59372. catch (CImgException&) { stop_flag = true; }
  59373. if (img) { img.move_to(*this); std::remove(filename_tmp2); }
  59374. }
  59375. cimg::exception_mode(omode);
  59376. if (is_empty())
  59377. throw CImgIOException(_cimglist_instance
  59378. "load_ffmpeg_external(): Failed to open file '%s' with external command 'ffmpeg'.",
  59379. cimglist_instance,
  59380. filename);
  59381. return *this;
  59382. }
  59383. //! Load an image from a video file using the external tool 'ffmpeg' \newinstance.
  59384. static CImgList<T> get_load_ffmpeg_external(const char *const filename) {
  59385. return CImgList<T>().load_ffmpeg_external(filename);
  59386. }
  59387. //! Load gif file, using ImageMagick or GraphicsMagick's external tools.
  59388. /**
  59389. \param filename Filename to read data from.
  59390. **/
  59391. CImgList<T>& load_gif_external(const char *const filename) {
  59392. if (!filename)
  59393. throw CImgArgumentException(_cimglist_instance
  59394. "load_gif_external(): Specified filename is (null).",
  59395. cimglist_instance);
  59396. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  59397. if (!_load_gif_external(filename,false))
  59398. if (!_load_gif_external(filename,true))
  59399. try { assign(CImg<T>().load_other(filename)); } catch (CImgException&) { assign(); }
  59400. if (is_empty())
  59401. throw CImgIOException(_cimglist_instance
  59402. "load_gif_external(): Failed to open file '%s'.",
  59403. cimglist_instance,filename);
  59404. return *this;
  59405. }
  59406. CImgList<T>& _load_gif_external(const char *const filename, const bool use_graphicsmagick=false) {
  59407. CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
  59408. std::FILE *file = 0;
  59409. do {
  59410. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  59411. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  59412. if (use_graphicsmagick) cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png.0",filename_tmp._data);
  59413. else cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s-0.png",filename_tmp._data);
  59414. if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
  59415. } while (file);
  59416. if (use_graphicsmagick) cimg_snprintf(command,command._width,"%s convert \"%s\" \"%s.png\"",
  59417. cimg::graphicsmagick_path(),
  59418. CImg<charT>::string(filename)._system_strescape().data(),
  59419. CImg<charT>::string(filename_tmp)._system_strescape().data());
  59420. else cimg_snprintf(command,command._width,"\"%s\" -coalesce \"%s\" \"%s.png\"",
  59421. cimg::imagemagick_path(),
  59422. CImg<charT>::string(filename)._system_strescape().data(),
  59423. CImg<charT>::string(filename_tmp)._system_strescape().data());
  59424. cimg::system(command, cimg::imagemagick_path());
  59425. const unsigned int omode = cimg::exception_mode();
  59426. cimg::exception_mode(0);
  59427. assign();
  59428. // Try to read a single frame gif.
  59429. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png",filename_tmp._data);
  59430. CImg<T> img;
  59431. try { img.load_png(filename_tmp2); }
  59432. catch (CImgException&) { }
  59433. if (img) { img.move_to(*this); std::remove(filename_tmp2); }
  59434. else { // Try to read animated gif
  59435. unsigned int i = 0;
  59436. for (bool stop_flag = false; !stop_flag; ++i) {
  59437. if (use_graphicsmagick) cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png.%u",filename_tmp._data,i);
  59438. else cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s-%u.png",filename_tmp._data,i);
  59439. try { img.load_png(filename_tmp2); }
  59440. catch (CImgException&) { stop_flag = true; }
  59441. if (img) { img.move_to(*this); std::remove(filename_tmp2); }
  59442. }
  59443. }
  59444. cimg::exception_mode(omode);
  59445. return *this;
  59446. }
  59447. //! Load gif file, using ImageMagick or GraphicsMagick's external tools \newinstance.
  59448. static CImgList<T> get_load_gif_external(const char *const filename) {
  59449. return CImgList<T>().load_gif_external(filename);
  59450. }
  59451. //! Load a gzipped list, using external tool 'gunzip'.
  59452. /**
  59453. \param filename Filename to read data from.
  59454. **/
  59455. CImgList<T>& load_gzip_external(const char *const filename) {
  59456. if (!filename)
  59457. throw CImgIOException(_cimglist_instance
  59458. "load_gzip_external(): Specified filename is (null).",
  59459. cimglist_instance);
  59460. cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
  59461. CImg<charT> command(1024), filename_tmp(256), body(256);
  59462. const char
  59463. *ext = cimg::split_filename(filename,body),
  59464. *ext2 = cimg::split_filename(body,0);
  59465. std::FILE *file = 0;
  59466. do {
  59467. if (!cimg::strcasecmp(ext,"gz")) {
  59468. if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  59469. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
  59470. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  59471. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  59472. } else {
  59473. if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  59474. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
  59475. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  59476. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  59477. }
  59478. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  59479. } while (file);
  59480. cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
  59481. cimg::gunzip_path(),
  59482. CImg<charT>::string(filename)._system_strescape().data(),
  59483. CImg<charT>::string(filename_tmp)._system_strescape().data());
  59484. cimg::system(command, cimg::gunzip_path());
  59485. if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
  59486. cimg::fclose(cimg::fopen(filename,"r"));
  59487. throw CImgIOException(_cimglist_instance
  59488. "load_gzip_external(): Failed to open file '%s'.",
  59489. cimglist_instance,
  59490. filename);
  59491. } else cimg::fclose(file);
  59492. load(filename_tmp);
  59493. std::remove(filename_tmp);
  59494. return *this;
  59495. }
  59496. //! Load a gzipped list, using external tool 'gunzip' \newinstance.
  59497. static CImgList<T> get_load_gzip_external(const char *const filename) {
  59498. return CImgList<T>().load_gzip_external(filename);
  59499. }
  59500. //! Load images from a TIFF file.
  59501. /**
  59502. \param filename Filename to read data from.
  59503. \param first_frame Index of first image frame to read.
  59504. \param last_frame Index of last image frame to read.
  59505. \param step_frame Step applied between each frame.
  59506. \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
  59507. \param[out] voxel_size Voxel size, as stored in the filename.
  59508. \param[out] description Description, as stored in the filename.
  59509. **/
  59510. CImgList<T>& load_tiff(const char *const filename,
  59511. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59512. const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
  59513. float *const voxel_size=0, CImg<charT> *const description=0) {
  59514. const unsigned int
  59515. nfirst_frame = first_frame<last_frame?first_frame:last_frame,
  59516. nstep_frame = step_frame?step_frame:1;
  59517. unsigned int nlast_frame = first_frame<last_frame?last_frame:first_frame;
  59518. #ifndef cimg_use_tiff
  59519. cimg::unused(bits_per_value,voxel_size,description);
  59520. if (nfirst_frame || nlast_frame!=~0U || nstep_frame!=1)
  59521. throw CImgArgumentException(_cimglist_instance
  59522. "load_tiff(): Unable to load sub-images from file '%s' unless libtiff is enabled.",
  59523. cimglist_instance,
  59524. filename);
  59525. return assign(CImg<T>::get_load_tiff(filename));
  59526. #else
  59527. #if cimg_verbosity<3
  59528. TIFFSetWarningHandler(0);
  59529. TIFFSetErrorHandler(0);
  59530. #endif
  59531. TIFF *tif = TIFFOpen(filename,"r");
  59532. if (tif) {
  59533. unsigned int nb_images = 0;
  59534. do ++nb_images; while (TIFFReadDirectory(tif));
  59535. if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))
  59536. cimg::warn(_cimglist_instance
  59537. "load_tiff(): Invalid specified frame range is [%u,%u] (step %u) since "
  59538. "file '%s' contains %u image(s).",
  59539. cimglist_instance,
  59540. nfirst_frame,nlast_frame,nstep_frame,filename,nb_images);
  59541. if (nfirst_frame>=nb_images) return assign();
  59542. if (nlast_frame>=nb_images) nlast_frame = nb_images - 1;
  59543. assign(1 + (nlast_frame - nfirst_frame)/nstep_frame);
  59544. TIFFSetDirectory(tif,0);
  59545. cimglist_for(*this,l)
  59546. _data[l]._load_tiff(tif,nfirst_frame + l*nstep_frame,bits_per_value,voxel_size,description);
  59547. TIFFClose(tif);
  59548. } else throw CImgIOException(_cimglist_instance
  59549. "load_tiff(): Failed to open file '%s'.",
  59550. cimglist_instance,
  59551. filename);
  59552. return *this;
  59553. #endif
  59554. }
  59555. //! Load a multi-page TIFF file \newinstance.
  59556. static CImgList<T> get_load_tiff(const char *const filename,
  59557. const unsigned int first_frame=0, const unsigned int last_frame=~0U,
  59558. const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
  59559. float *const voxel_size=0, CImg<charT> *const description=0) {
  59560. return CImgList<T>().load_tiff(filename,first_frame,last_frame,step_frame,bits_per_value,voxel_size,description);
  59561. }
  59562. //@}
  59563. //----------------------------------
  59564. //
  59565. //! \name Data Output
  59566. //@{
  59567. //----------------------------------
  59568. //! Print information about the list on the standard output.
  59569. /**
  59570. \param title Label set to the information displayed.
  59571. \param display_stats Tells if image statistics must be computed and displayed.
  59572. **/
  59573. const CImgList<T>& print(const char *const title=0, const bool display_stats=true) const {
  59574. unsigned int msiz = 0;
  59575. cimglist_for(*this,l) msiz+=_data[l].size();
  59576. msiz*=sizeof(T);
  59577. const unsigned int mdisp = msiz<8*1024?0U:msiz<8*1024*1024?1U:2U;
  59578. CImg<charT> _title(64);
  59579. if (!title) cimg_snprintf(_title,_title._width,"CImgList<%s>",pixel_type());
  59580. std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = %u/%u [%u %s], %sdata%s = (CImg<%s>*)%p",
  59581. cimg::t_magenta,cimg::t_bold,title?title:_title._data,cimg::t_normal,
  59582. cimg::t_bold,cimg::t_normal,(void*)this,
  59583. cimg::t_bold,cimg::t_normal,_width,_allocated_width,
  59584. mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)),
  59585. mdisp==0?"b":(mdisp==1?"Kio":"Mio"),
  59586. cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin());
  59587. if (_data) std::fprintf(cimg::output(),"..%p.\n",(void*)((char*)end() - 1));
  59588. else std::fprintf(cimg::output(),".\n");
  59589. char tmp[16] = { 0 };
  59590. cimglist_for(*this,ll) {
  59591. cimg_snprintf(tmp,sizeof(tmp),"[%d]",ll);
  59592. std::fprintf(cimg::output()," ");
  59593. _data[ll].print(tmp,display_stats);
  59594. if (ll==3 && width()>8) { ll = width() - 5; std::fprintf(cimg::output()," ...\n"); }
  59595. }
  59596. std::fflush(cimg::output());
  59597. return *this;
  59598. }
  59599. //! Display the current CImgList instance in an existing CImgDisplay window (by reference).
  59600. /**
  59601. \param disp Reference to an existing CImgDisplay instance, where the current image list will be displayed.
  59602. \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  59603. \param align Appending alignment.
  59604. \note This function displays the list images of the current CImgList instance into an existing
  59605. CImgDisplay window.
  59606. Images of the list are appended in a single temporary image for visualization purposes.
  59607. The function returns immediately.
  59608. **/
  59609. const CImgList<T>& display(CImgDisplay &disp, const char axis='x', const float align=0) const {
  59610. disp.display(*this,axis,align);
  59611. return *this;
  59612. }
  59613. //! Display the current CImgList instance in a new display window.
  59614. /**
  59615. \param disp Display window.
  59616. \param display_info Tells if image information are displayed on the standard output.
  59617. \param axis Alignment axis for images viewing.
  59618. \param align Appending alignment.
  59619. \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
  59620. \param exit_on_anykey Exit function when any key is pressed.
  59621. \note This function opens a new window with a specific title and displays the list images of the
  59622. current CImgList instance into it.
  59623. Images of the list are appended in a single temporary image for visualization purposes.
  59624. The function returns when a key is pressed or the display window is closed by the user.
  59625. **/
  59626. const CImgList<T>& display(CImgDisplay &disp, const bool display_info,
  59627. const char axis='x', const float align=0,
  59628. unsigned int *const XYZ=0, const bool exit_on_anykey=false) const {
  59629. bool is_exit = false;
  59630. return _display(disp,0,0,display_info,axis,align,XYZ,exit_on_anykey,0,true,is_exit);
  59631. }
  59632. //! Display the current CImgList instance in a new display window.
  59633. /**
  59634. \param title Title of the opening display window.
  59635. \param display_info Tells if list information must be written on standard output.
  59636. \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
  59637. \param align Appending alignment.
  59638. \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
  59639. \param exit_on_anykey Exit function when any key is pressed.
  59640. **/
  59641. const CImgList<T>& display(const char *const title=0, const bool display_info=true,
  59642. const char axis='x', const float align=0,
  59643. unsigned int *const XYZ=0, const bool exit_on_anykey=false) const {
  59644. CImgDisplay disp;
  59645. bool is_exit = false;
  59646. return _display(disp,title,0,display_info,axis,align,XYZ,exit_on_anykey,0,true,is_exit);
  59647. }
  59648. const CImgList<T>& _display(CImgDisplay &disp, const char *const title, const CImgList<charT> *const titles,
  59649. const bool display_info, const char axis, const float align, unsigned int *const XYZ,
  59650. const bool exit_on_anykey, const unsigned int orig, const bool is_first_call,
  59651. bool &is_exit) const {
  59652. if (is_empty())
  59653. throw CImgInstanceException(_cimglist_instance
  59654. "display(): Empty instance.",
  59655. cimglist_instance);
  59656. if (!disp) {
  59657. if (axis=='x') {
  59658. unsigned int sum_width = 0, max_height = 0;
  59659. cimglist_for(*this,l) {
  59660. const CImg<T> &img = _data[l];
  59661. const unsigned int
  59662. w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
  59663. h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
  59664. sum_width+=w;
  59665. if (h>max_height) max_height = h;
  59666. }
  59667. disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:titles?titles->__display()._data:0,1);
  59668. } else {
  59669. unsigned int max_width = 0, sum_height = 0;
  59670. cimglist_for(*this,l) {
  59671. const CImg<T> &img = _data[l];
  59672. const unsigned int
  59673. w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
  59674. h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
  59675. if (w>max_width) max_width = w;
  59676. sum_height+=h;
  59677. }
  59678. disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:titles?titles->__display()._data:0,1);
  59679. }
  59680. if (!title && !titles) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width);
  59681. } else if (title) disp.set_title("%s",title);
  59682. else if (titles) disp.set_title("%s",titles->__display()._data);
  59683. const CImg<char> dtitle = CImg<char>::string(disp.title());
  59684. if (display_info) print(disp.title());
  59685. disp.show().flush();
  59686. if (_width==1) {
  59687. const unsigned int dw = disp._width, dh = disp._height;
  59688. if (!is_first_call)
  59689. disp.resize(cimg_fitscreen(_data[0]._width,_data[0]._height,_data[0]._depth),false);
  59690. disp.set_title("%s (%ux%ux%ux%u)",
  59691. dtitle.data(),_data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum);
  59692. _data[0]._display(disp,0,false,XYZ,exit_on_anykey,!is_first_call);
  59693. if (disp.key()) is_exit = true;
  59694. disp.resize(cimg_fitscreen(dw,dh,1),false).set_title("%s",dtitle.data());
  59695. } else {
  59696. bool disp_resize = !is_first_call;
  59697. while (!disp.is_closed() && !is_exit) {
  59698. const CImg<intT> s = _select(disp,0,true,axis,align,exit_on_anykey,orig,disp_resize,!is_first_call,true);
  59699. disp_resize = true;
  59700. if (s[0]<0 && !disp.wheel()) { // No selections done
  59701. if (disp.button()&2) { disp.flush(); break; }
  59702. is_exit = true;
  59703. } else if (disp.wheel()) { // Zoom in/out
  59704. const int wheel = disp.wheel();
  59705. disp.set_wheel();
  59706. if (!is_first_call && wheel<0) break;
  59707. if (wheel>0 && _width>=4) {
  59708. const unsigned int
  59709. delta = std::max(1U,(unsigned int)cimg::round(0.3*_width)),
  59710. ind0 = (unsigned int)std::max(0,s[0] - (int)delta),
  59711. ind1 = (unsigned int)std::min(width() - 1,s[0] + (int)delta);
  59712. if ((ind0!=0 || ind1!=_width - 1) && ind1 - ind0>=3) {
  59713. const CImgList<T> sublist = get_shared_images(ind0,ind1);
  59714. CImgList<charT> t_sublist;
  59715. if (titles) t_sublist = titles->get_shared_images(ind0,ind1);
  59716. sublist._display(disp,0,titles?&t_sublist:0,false,axis,align,XYZ,exit_on_anykey,
  59717. orig + ind0,false,is_exit);
  59718. }
  59719. }
  59720. } else if (s[0]!=0 || s[1]!=width() - 1) {
  59721. const CImgList<T> sublist = get_shared_images(s[0],s[1]);
  59722. CImgList<charT> t_sublist;
  59723. if (titles) t_sublist = titles->get_shared_images(s[0],s[1]);
  59724. sublist._display(disp,0,titles?&t_sublist:0,false,axis,align,XYZ,exit_on_anykey,
  59725. orig + s[0],false,is_exit);
  59726. }
  59727. disp.set_title("%s",dtitle.data());
  59728. }
  59729. }
  59730. return *this;
  59731. }
  59732. // [internal] Return string to describe display title.
  59733. CImg<charT> __display() const {
  59734. CImg<charT> res, str;
  59735. cimglist_for(*this,l) {
  59736. CImg<charT>::string((char*)_data[l]).move_to(str);
  59737. if (l!=width() - 1) {
  59738. str.resize(str._width + 1,1,1,1,0);
  59739. str[str._width - 2] = ',';
  59740. str[str._width - 1] = ' ';
  59741. }
  59742. res.append(str,'x');
  59743. }
  59744. if (!res) return CImg<charT>(1,1,1,1,0).move_to(res);
  59745. cimg::strellipsize(res,128,false);
  59746. if (_width>1) {
  59747. const unsigned int l = (unsigned int)std::strlen(res);
  59748. if (res._width<=l + 16) res.resize(l + 16,1,1,1,0);
  59749. cimg_snprintf(res._data + l,16," (#%u)",_width);
  59750. }
  59751. return res;
  59752. }
  59753. //! Save list into a file.
  59754. /**
  59755. \param filename Filename to write data to.
  59756. \param number When positive, represents an index added to the filename. Otherwise, no number is added.
  59757. \param digits Number of digits used for adding the number to the filename.
  59758. **/
  59759. const CImgList<T>& save(const char *const filename, const int number=-1, const unsigned int digits=6) const {
  59760. if (!filename)
  59761. throw CImgArgumentException(_cimglist_instance
  59762. "save(): Specified filename is (null).",
  59763. cimglist_instance);
  59764. // Do not test for empty instances, since .cimg format is able to manage empty instances.
  59765. const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.');
  59766. const char *const ext = cimg::split_filename(filename);
  59767. CImg<charT> nfilename(1024);
  59768. const char *const fn = is_stdout?filename:number>=0?cimg::number_filename(filename,number,digits,nfilename):
  59769. filename;
  59770. #ifdef cimglist_save_plugin
  59771. cimglist_save_plugin(fn);
  59772. #endif
  59773. #ifdef cimglist_save_plugin1
  59774. cimglist_save_plugin1(fn);
  59775. #endif
  59776. #ifdef cimglist_save_plugin2
  59777. cimglist_save_plugin2(fn);
  59778. #endif
  59779. #ifdef cimglist_save_plugin3
  59780. cimglist_save_plugin3(fn);
  59781. #endif
  59782. #ifdef cimglist_save_plugin4
  59783. cimglist_save_plugin4(fn);
  59784. #endif
  59785. #ifdef cimglist_save_plugin5
  59786. cimglist_save_plugin5(fn);
  59787. #endif
  59788. #ifdef cimglist_save_plugin6
  59789. cimglist_save_plugin6(fn);
  59790. #endif
  59791. #ifdef cimglist_save_plugin7
  59792. cimglist_save_plugin7(fn);
  59793. #endif
  59794. #ifdef cimglist_save_plugin8
  59795. cimglist_save_plugin8(fn);
  59796. #endif
  59797. if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true);
  59798. else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false);
  59799. else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,444,true);
  59800. else if (!cimg::strcasecmp(ext,"avi") ||
  59801. !cimg::strcasecmp(ext,"mov") ||
  59802. !cimg::strcasecmp(ext,"asf") ||
  59803. !cimg::strcasecmp(ext,"divx") ||
  59804. !cimg::strcasecmp(ext,"flv") ||
  59805. !cimg::strcasecmp(ext,"mpg") ||
  59806. !cimg::strcasecmp(ext,"m1v") ||
  59807. !cimg::strcasecmp(ext,"m2v") ||
  59808. !cimg::strcasecmp(ext,"m4v") ||
  59809. !cimg::strcasecmp(ext,"mjp") ||
  59810. !cimg::strcasecmp(ext,"mp4") ||
  59811. !cimg::strcasecmp(ext,"mkv") ||
  59812. !cimg::strcasecmp(ext,"mpe") ||
  59813. !cimg::strcasecmp(ext,"movie") ||
  59814. !cimg::strcasecmp(ext,"ogm") ||
  59815. !cimg::strcasecmp(ext,"ogg") ||
  59816. !cimg::strcasecmp(ext,"ogv") ||
  59817. !cimg::strcasecmp(ext,"qt") ||
  59818. !cimg::strcasecmp(ext,"rm") ||
  59819. !cimg::strcasecmp(ext,"vob") ||
  59820. !cimg::strcasecmp(ext,"webm") ||
  59821. !cimg::strcasecmp(ext,"wmv") ||
  59822. !cimg::strcasecmp(ext,"xvid") ||
  59823. !cimg::strcasecmp(ext,"mpeg")) return save_video(fn);
  59824. #ifdef cimg_use_tiff
  59825. else if (!cimg::strcasecmp(ext,"tif") ||
  59826. !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn);
  59827. #endif
  59828. else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn);
  59829. else {
  59830. if (_width==1) _data[0].save(fn,-1);
  59831. else cimglist_for(*this,l) { _data[l].save(fn,is_stdout?-1:l); if (is_stdout) std::fputc(EOF,cimg::_stdout()); }
  59832. }
  59833. return *this;
  59834. }
  59835. //! Tell if an image list can be saved as one single file.
  59836. /**
  59837. \param filename Filename, as a C-string.
  59838. \return \c true if the file format supports multiple images, \c false otherwise.
  59839. **/
  59840. static bool is_saveable(const char *const filename) {
  59841. const char *const ext = cimg::split_filename(filename);
  59842. if (!cimg::strcasecmp(ext,"cimgz") ||
  59843. #ifdef cimg_use_tiff
  59844. !cimg::strcasecmp(ext,"tif") ||
  59845. !cimg::strcasecmp(ext,"tiff") ||
  59846. #endif
  59847. !cimg::strcasecmp(ext,"yuv") ||
  59848. !cimg::strcasecmp(ext,"avi") ||
  59849. !cimg::strcasecmp(ext,"mov") ||
  59850. !cimg::strcasecmp(ext,"asf") ||
  59851. !cimg::strcasecmp(ext,"divx") ||
  59852. !cimg::strcasecmp(ext,"flv") ||
  59853. !cimg::strcasecmp(ext,"mpg") ||
  59854. !cimg::strcasecmp(ext,"m1v") ||
  59855. !cimg::strcasecmp(ext,"m2v") ||
  59856. !cimg::strcasecmp(ext,"m4v") ||
  59857. !cimg::strcasecmp(ext,"mjp") ||
  59858. !cimg::strcasecmp(ext,"mp4") ||
  59859. !cimg::strcasecmp(ext,"mkv") ||
  59860. !cimg::strcasecmp(ext,"mpe") ||
  59861. !cimg::strcasecmp(ext,"movie") ||
  59862. !cimg::strcasecmp(ext,"ogm") ||
  59863. !cimg::strcasecmp(ext,"ogg") ||
  59864. !cimg::strcasecmp(ext,"ogv") ||
  59865. !cimg::strcasecmp(ext,"qt") ||
  59866. !cimg::strcasecmp(ext,"rm") ||
  59867. !cimg::strcasecmp(ext,"vob") ||
  59868. !cimg::strcasecmp(ext,"webm") ||
  59869. !cimg::strcasecmp(ext,"wmv") ||
  59870. !cimg::strcasecmp(ext,"xvid") ||
  59871. !cimg::strcasecmp(ext,"mpeg")) return true;
  59872. return false;
  59873. }
  59874. //! Save image sequence as a GIF animated file.
  59875. /**
  59876. \param filename Filename to write data to.
  59877. \param fps Number of desired frames per second.
  59878. \param nb_loops Number of loops (\c 0 for infinite looping).
  59879. **/
  59880. const CImgList<T>& save_gif_external(const char *const filename, const float fps=25,
  59881. const unsigned int nb_loops=0) {
  59882. CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
  59883. CImgList<charT> filenames;
  59884. std::FILE *file = 0;
  59885. #ifdef cimg_use_png
  59886. #define _cimg_save_gif_extension "png"
  59887. #else
  59888. #define _cimg_save_gif_extension "ppm"
  59889. #endif
  59890. do {
  59891. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  59892. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  59893. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001." _cimg_save_gif_extension,filename_tmp._data);
  59894. if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
  59895. } while (file);
  59896. cimglist_for(*this,l) {
  59897. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u." _cimg_save_gif_extension,filename_tmp._data,l + 1);
  59898. CImg<charT>::string(filename_tmp2).move_to(filenames);
  59899. if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save(filename_tmp2);
  59900. else _data[l].save(filename_tmp2);
  59901. }
  59902. cimg_snprintf(command,command._width,"\"%s\" -delay %u -loop %u",
  59903. cimg::imagemagick_path(),(unsigned int)std::max(0.f,cimg::round(100/fps)),nb_loops);
  59904. CImg<ucharT>::string(command).move_to(filenames,0);
  59905. cimg_snprintf(command,command._width,"\"%s\"",
  59906. CImg<charT>::string(filename)._system_strescape().data());
  59907. CImg<ucharT>::string(command).move_to(filenames);
  59908. CImg<charT> _command = filenames>'x';
  59909. cimg_for(_command,p,char) if (!*p) *p = ' ';
  59910. _command.back() = 0;
  59911. cimg::system(_command, cimg::imagemagick_path());
  59912. file = cimg::std_fopen(filename,"rb");
  59913. if (!file)
  59914. throw CImgIOException(_cimglist_instance
  59915. "save_gif_external(): Failed to save file '%s' with external command 'magick/convert'.",
  59916. cimglist_instance,
  59917. filename);
  59918. else cimg::fclose(file);
  59919. cimglist_for_in(*this,1,filenames._width - 1,l) std::remove(filenames[l]);
  59920. return *this;
  59921. }
  59922. //! Save list as a YUV image sequence file.
  59923. /**
  59924. \param filename Filename to write data to.
  59925. \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
  59926. \param is_rgb Tells if the RGB to YUV conversion must be done for saving.
  59927. **/
  59928. const CImgList<T>& save_yuv(const char *const filename=0,
  59929. const unsigned int chroma_subsampling=444,
  59930. const bool is_rgb=true) const {
  59931. return _save_yuv(0,filename,chroma_subsampling,is_rgb);
  59932. }
  59933. //! Save image sequence into a YUV file.
  59934. /**
  59935. \param file File to write data to.
  59936. \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
  59937. \param is_rgb Tells if the RGB to YUV conversion must be done for saving.
  59938. **/
  59939. const CImgList<T>& save_yuv(std::FILE *const file,
  59940. const unsigned int chroma_subsampling=444,
  59941. const bool is_rgb=true) const {
  59942. return _save_yuv(file,0,chroma_subsampling,is_rgb);
  59943. }
  59944. const CImgList<T>& _save_yuv(std::FILE *const file, const char *const filename,
  59945. const unsigned int chroma_subsampling,
  59946. const bool is_rgb) const {
  59947. if (!file && !filename)
  59948. throw CImgArgumentException(_cimglist_instance
  59949. "save_yuv(): Specified filename is (null).",
  59950. cimglist_instance);
  59951. if (chroma_subsampling!=420 && chroma_subsampling!=422 && chroma_subsampling!=444)
  59952. throw CImgArgumentException(_cimglist_instance
  59953. "save_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.",
  59954. cimglist_instance,
  59955. chroma_subsampling,filename?filename:"(FILE*)");
  59956. if (is_empty()) { cimg::fempty(file,filename); return *this; }
  59957. const unsigned int
  59958. cfx = chroma_subsampling==420 || chroma_subsampling==422?2:1,
  59959. cfy = chroma_subsampling==420?2:1,
  59960. w0 = (*this)[0]._width, h0 = (*this)[0]._height,
  59961. width0 = w0 + (w0%cfx), height0 = h0 + (h0%cfy);
  59962. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  59963. cimglist_for(*this,l) {
  59964. const CImg<T> &frame = (*this)[l];
  59965. cimg_forZ(frame,z) {
  59966. CImg<ucharT> YUV;
  59967. if (sizeof(T)==1 && !is_rgb &&
  59968. frame._width==width0 && frame._height==height0 && frame._depth==1 && frame._spectrum==3)
  59969. YUV.assign((unsigned char*)frame._data,width0,height0,1,3,true);
  59970. else {
  59971. YUV = frame.get_slice(z);
  59972. if (YUV._width!=width0 || YUV._height!=height0) YUV.resize(width0,height0,1,-100,0);
  59973. if (YUV._spectrum!=3) YUV.resize(-100,-100,1,3,YUV._spectrum==1?1:0);
  59974. if (is_rgb) YUV.RGBtoYCbCr();
  59975. }
  59976. if (chroma_subsampling==444)
  59977. cimg::fwrite(YUV._data,(size_t)YUV._width*YUV._height*3,nfile);
  59978. else {
  59979. cimg::fwrite(YUV._data,(size_t)YUV._width*YUV._height,nfile);
  59980. CImg<ucharT> UV = YUV.get_channels(1,2);
  59981. UV.resize(UV._width/cfx,UV._height/cfy,1,2,2);
  59982. cimg::fwrite(UV._data,(size_t)UV._width*UV._height*2,nfile);
  59983. }
  59984. }
  59985. }
  59986. if (!file) cimg::fclose(nfile);
  59987. return *this;
  59988. }
  59989. //! Save list into a .cimg file.
  59990. /**
  59991. \param filename Filename to write data to.
  59992. \param is_compressed Tells if data compression must be enabled.
  59993. **/
  59994. const CImgList<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {
  59995. return _save_cimg(0,filename,is_compressed);
  59996. }
  59997. const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const {
  59998. if (!file && !filename)
  59999. throw CImgArgumentException(_cimglist_instance
  60000. "save_cimg(): Specified filename is (null).",
  60001. cimglist_instance);
  60002. #ifndef cimg_use_zlib
  60003. if (is_compressed)
  60004. cimg::warn(_cimglist_instance
  60005. "save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, "
  60006. "saving them uncompressed.",
  60007. cimglist_instance,
  60008. filename?filename:"(FILE*)");
  60009. #endif
  60010. const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
  60011. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  60012. const bool is_bool = ptype==cimg::type<bool>::string();
  60013. if (!is_bool && std::strstr(ptype,"unsigned")==ptype)
  60014. std::fprintf(nfile,"%u unsigned_%s %s_endian\n",_width,ptype + 9,etype);
  60015. else
  60016. std::fprintf(nfile,"%u %s %s_endian\n",_width,ptype,etype);
  60017. cimglist_for(*this,l) {
  60018. const CImg<T>& img = _data[l];
  60019. std::fprintf(nfile,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);
  60020. if (img._data) {
  60021. CImg<T> tmp;
  60022. if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); }
  60023. const CImg<T>& ref = cimg::endianness()?tmp:img;
  60024. bool failed_to_compress = true;
  60025. if (is_compressed) {
  60026. #ifdef cimg_use_zlib
  60027. Bytef *cbuf = 0;
  60028. uLongf csiz = 0;
  60029. if (is_bool) { // Boolean data (bitwise)
  60030. ulongT siz;
  60031. const unsigned char *const buf = ref._bool2uchar(siz,false);
  60032. csiz = siz + siz/100 + 16;
  60033. cbuf = new Bytef[csiz];
  60034. failed_to_compress = (bool)compress(cbuf,&csiz,(Bytef*)buf,siz);
  60035. if (!failed_to_compress) {
  60036. std::fprintf(nfile," #%lu\n",csiz);
  60037. cimg::fwrite(cbuf,csiz,nfile);
  60038. }
  60039. delete[] buf;
  60040. } else { // Non-boolean data
  60041. const ulongT siz = sizeof(T)*ref.size();
  60042. csiz = siz + siz/100 + 16;
  60043. cbuf = new Bytef[csiz];
  60044. failed_to_compress = (bool)compress(cbuf,&csiz,(Bytef*)ref._data,siz);
  60045. if (!failed_to_compress) {
  60046. std::fprintf(nfile," #%lu\n",csiz);
  60047. cimg::fwrite(cbuf,csiz,nfile);
  60048. }
  60049. }
  60050. if (failed_to_compress)
  60051. cimg::warn(_cimglist_instance
  60052. "save_cimg(): Failed to save compressed data for file '%s', saving them uncompressed.",
  60053. cimglist_instance,
  60054. filename?filename:"(FILE*)");
  60055. delete[] cbuf;
  60056. #endif
  60057. }
  60058. if (failed_to_compress) { // Write non-compressed
  60059. std::fputc('\n',nfile);
  60060. if (is_bool) { // Boolean data (bitwise)
  60061. ulongT siz;
  60062. const unsigned char *const buf = ref._bool2uchar(siz,false);
  60063. cimg::fwrite(buf,siz,nfile);
  60064. delete[] buf;
  60065. } else cimg::fwrite(ref._data,ref.size(),nfile); // Non-boolean data
  60066. }
  60067. } else std::fputc('\n',nfile);
  60068. }
  60069. if (!file) cimg::fclose(nfile);
  60070. return *this;
  60071. }
  60072. //! Save list into a .cimg file.
  60073. /**
  60074. \param file File to write data to.
  60075. \param is_compressed Tells if data compression must be enabled.
  60076. **/
  60077. const CImgList<T>& save_cimg(std::FILE *file, const bool is_compressed=false) const {
  60078. return _save_cimg(file,0,is_compressed);
  60079. }
  60080. const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename,
  60081. const unsigned int n0,
  60082. const unsigned int x0, const unsigned int y0,
  60083. const unsigned int z0, const unsigned int c0) const {
  60084. #define _cimg_save_cimg_case(Ts,Tss) \
  60085. if (!saved && !cimg::strcasecmp(Ts,str_pixeltype)) { \
  60086. for (unsigned int l = 0; l<lmax; ++l) { \
  60087. j = 0; while ((i=std::fgetc(nfile))!='\n') tmp[j++]=(char)i; tmp[j] = 0; \
  60088. W = H = D = C = 0; \
  60089. if (cimg_sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \
  60090. throw CImgIOException(_cimglist_instance \
  60091. "save_cimg(): Invalid size (%u,%u,%u,%u) of image[%u], for file '%s'.", \
  60092. cimglist_instance, \
  60093. W,H,D,C,l,filename?filename:"(FILE*)"); \
  60094. if (W*H*D*C>0) { \
  60095. if (l<n0 || x0>=W || y0>=H || z0>=D || c0>=D) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \
  60096. else { \
  60097. const CImg<T>& img = (*this)[l - n0]; \
  60098. const T *ptrs = img._data; \
  60099. const unsigned int \
  60100. x1 = x0 + img._width - 1, \
  60101. y1 = y0 + img._height - 1, \
  60102. z1 = z0 + img._depth - 1, \
  60103. c1 = c0 + img._spectrum - 1, \
  60104. nx1 = x1>=W?W - 1:x1, \
  60105. ny1 = y1>=H?H - 1:y1, \
  60106. nz1 = z1>=D?D - 1:z1, \
  60107. nc1 = c1>=C?C - 1:c1; \
  60108. CImg<Tss> raw(1 + nx1 - x0); \
  60109. const unsigned int skipvb = c0*W*H*D*sizeof(Tss); \
  60110. if (skipvb) cimg::fseek(nfile,skipvb,SEEK_CUR); \
  60111. for (unsigned int v = 1 + nc1 - c0; v; --v) { \
  60112. const unsigned int skipzb = z0*W*H*sizeof(Tss); \
  60113. if (skipzb) cimg::fseek(nfile,skipzb,SEEK_CUR); \
  60114. for (unsigned int z = 1 + nz1 - z0; z; --z) { \
  60115. const unsigned int skipyb = y0*W*sizeof(Tss); \
  60116. if (skipyb) cimg::fseek(nfile,skipyb,SEEK_CUR); \
  60117. for (unsigned int y = 1 + ny1 - y0; y; --y) { \
  60118. const unsigned int skipxb = x0*sizeof(Tss); \
  60119. if (skipxb) cimg::fseek(nfile,skipxb,SEEK_CUR); \
  60120. raw.assign(ptrs, raw._width); \
  60121. ptrs+=img._width; \
  60122. if (endian) cimg::invert_endianness(raw._data,raw._width); \
  60123. cimg::fwrite(raw._data,raw._width,nfile); \
  60124. const unsigned int skipxe = (W - 1 - nx1)*sizeof(Tss); \
  60125. if (skipxe) cimg::fseek(nfile,skipxe,SEEK_CUR); \
  60126. } \
  60127. const unsigned int skipye = (H - 1 - ny1)*W*sizeof(Tss); \
  60128. if (skipye) cimg::fseek(nfile,skipye,SEEK_CUR); \
  60129. } \
  60130. const unsigned int skipze = (D - 1 - nz1)*W*H*sizeof(Tss); \
  60131. if (skipze) cimg::fseek(nfile,skipze,SEEK_CUR); \
  60132. } \
  60133. const unsigned int skipve = (C - 1 - nc1)*W*H*D*sizeof(Tss); \
  60134. if (skipve) cimg::fseek(nfile,skipve,SEEK_CUR); \
  60135. } \
  60136. } \
  60137. } \
  60138. saved = true; \
  60139. }
  60140. if (!file && !filename)
  60141. throw CImgArgumentException(_cimglist_instance
  60142. "save_cimg(): Specified filename is (null).",
  60143. cimglist_instance);
  60144. if (is_empty())
  60145. throw CImgInstanceException(_cimglist_instance
  60146. "save_cimg(): Empty instance, for file '%s'.",
  60147. cimglist_instance,
  60148. filename?filename:"(FILE*)");
  60149. std::FILE *const nfile = file?file:cimg::fopen(filename,"rb+");
  60150. bool saved = false, endian = cimg::endianness();
  60151. CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
  60152. *tmp = *str_pixeltype = *str_endian = 0;
  60153. unsigned int j, N, W, H, D, C;
  60154. int i, err;
  60155. j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
  60156. err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype._data,str_endian._data);
  60157. if (err<2) {
  60158. if (!file) cimg::fclose(nfile);
  60159. throw CImgIOException(_cimglist_instance
  60160. "save_cimg(): CImg header not found in file '%s'.",
  60161. cimglist_instance,
  60162. filename?filename:"(FILE*)");
  60163. }
  60164. if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
  60165. else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
  60166. const unsigned int lmax = std::min(N,n0 + _width);
  60167. _cimg_save_cimg_case("bool",bool);
  60168. _cimg_save_cimg_case("unsigned_char",unsigned char);
  60169. _cimg_save_cimg_case("uchar",unsigned char);
  60170. _cimg_save_cimg_case("char",char);
  60171. _cimg_save_cimg_case("unsigned_short",unsigned short);
  60172. _cimg_save_cimg_case("ushort",unsigned short);
  60173. _cimg_save_cimg_case("short",short);
  60174. _cimg_save_cimg_case("unsigned_int",unsigned int);
  60175. _cimg_save_cimg_case("uint",unsigned int);
  60176. _cimg_save_cimg_case("int",int);
  60177. _cimg_save_cimg_case("unsigned_int64",uint64T);
  60178. _cimg_save_cimg_case("uint64",uint64T);
  60179. _cimg_save_cimg_case("int64",int64T);
  60180. _cimg_save_cimg_case("float",float);
  60181. _cimg_save_cimg_case("double",double);
  60182. if (!saved) {
  60183. if (!file) cimg::fclose(nfile);
  60184. throw CImgIOException(_cimglist_instance
  60185. "save_cimg(): Unsupported data type '%s' for file '%s'.",
  60186. cimglist_instance,
  60187. filename?filename:"(FILE*)",str_pixeltype._data);
  60188. }
  60189. if (!file) cimg::fclose(nfile);
  60190. return *this;
  60191. }
  60192. //! Insert the image instance into into an existing .cimg file, at specified coordinates.
  60193. /**
  60194. \param filename Filename to write data to.
  60195. \param n0 Starting index of images to write.
  60196. \param x0 Starting X-coordinates of image regions to write.
  60197. \param y0 Starting Y-coordinates of image regions to write.
  60198. \param z0 Starting Z-coordinates of image regions to write.
  60199. \param c0 Starting C-coordinates of image regions to write.
  60200. **/
  60201. const CImgList<T>& save_cimg(const char *const filename,
  60202. const unsigned int n0,
  60203. const unsigned int x0, const unsigned int y0,
  60204. const unsigned int z0, const unsigned int c0) const {
  60205. return _save_cimg(0,filename,n0,x0,y0,z0,c0);
  60206. }
  60207. //! Insert the image instance into into an existing .cimg file, at specified coordinates.
  60208. /**
  60209. \param file File to write data to.
  60210. \param n0 Starting index of images to write.
  60211. \param x0 Starting X-coordinates of image regions to write.
  60212. \param y0 Starting Y-coordinates of image regions to write.
  60213. \param z0 Starting Z-coordinates of image regions to write.
  60214. \param c0 Starting C-coordinates of image regions to write.
  60215. **/
  60216. const CImgList<T>& save_cimg(std::FILE *const file,
  60217. const unsigned int n0,
  60218. const unsigned int x0, const unsigned int y0,
  60219. const unsigned int z0, const unsigned int c0) const {
  60220. return _save_cimg(file,0,n0,x0,y0,z0,c0);
  60221. }
  60222. static void _save_empty_cimg(std::FILE *const file, const char *const filename,
  60223. const unsigned int nb,
  60224. const unsigned int dx, const unsigned int dy,
  60225. const unsigned int dz, const unsigned int dc) {
  60226. std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
  60227. const ulongT siz = (ulongT)dx*dy*dz*dc*sizeof(T);
  60228. std::fprintf(nfile,"%u %s\n",nb,pixel_type());
  60229. for (unsigned int i=nb; i; --i) {
  60230. std::fprintf(nfile,"%u %u %u %u\n",dx,dy,dz,dc);
  60231. for (ulongT off = siz; off; --off) std::fputc(0,nfile);
  60232. }
  60233. if (!file) cimg::fclose(nfile);
  60234. }
  60235. //! Save empty (non-compressed) .cimg file with specified dimensions.
  60236. /**
  60237. \param filename Filename to write data to.
  60238. \param nb Number of images to write.
  60239. \param dx Width of images in the written file.
  60240. \param dy Height of images in the written file.
  60241. \param dz Depth of images in the written file.
  60242. \param dc Spectrum of images in the written file.
  60243. **/
  60244. static void save_empty_cimg(const char *const filename,
  60245. const unsigned int nb,
  60246. const unsigned int dx, const unsigned int dy=1,
  60247. const unsigned int dz=1, const unsigned int dc=1) {
  60248. return _save_empty_cimg(0,filename,nb,dx,dy,dz,dc);
  60249. }
  60250. //! Save empty .cimg file with specified dimensions.
  60251. /**
  60252. \param file File to write data to.
  60253. \param nb Number of images to write.
  60254. \param dx Width of images in the written file.
  60255. \param dy Height of images in the written file.
  60256. \param dz Depth of images in the written file.
  60257. \param dc Spectrum of images in the written file.
  60258. **/
  60259. static void save_empty_cimg(std::FILE *const file,
  60260. const unsigned int nb,
  60261. const unsigned int dx, const unsigned int dy=1,
  60262. const unsigned int dz=1, const unsigned int dc=1) {
  60263. return _save_empty_cimg(file,0,nb,dx,dy,dz,dc);
  60264. }
  60265. //! Save list as a TIFF file.
  60266. /**
  60267. \param filename Filename to write data to.
  60268. \param compression_type Compression mode used to write data.
  60269. \param voxel_size Voxel size, to be stored in the filename.
  60270. \param description Description, to be stored in the filename.
  60271. \param use_bigtiff Allow to save big tiff files (>4Gb).
  60272. **/
  60273. const CImgList<T>& save_tiff(const char *const filename, const unsigned int compression_type=0,
  60274. const float *const voxel_size=0, const char *const description=0,
  60275. const bool use_bigtiff=true) const {
  60276. if (!filename)
  60277. throw CImgArgumentException(_cimglist_instance
  60278. "save_tiff(): Specified filename is (null).",
  60279. cimglist_instance);
  60280. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  60281. #ifndef cimg_use_tiff
  60282. if (_width==1) _data[0].save_tiff(filename,compression_type,voxel_size,description,use_bigtiff);
  60283. else cimglist_for(*this,l) {
  60284. CImg<charT> nfilename(1024);
  60285. cimg::number_filename(filename,l,6,nfilename);
  60286. _data[l].save_tiff(nfilename,compression_type,voxel_size,description,use_bigtiff);
  60287. }
  60288. #else
  60289. ulongT siz = 0;
  60290. cimglist_for(*this,l) siz+=_data[l].size();
  60291. const bool _use_bigtiff = use_bigtiff && sizeof(siz)>=8 && siz*sizeof(T)>=1UL<<31; // No bigtiff for small images
  60292. TIFF *tif = TIFFOpen(filename,_use_bigtiff?"w8":"w4");
  60293. if (tif) {
  60294. for (unsigned int dir = 0, l = 0; l<_width; ++l) {
  60295. const CImg<T>& img = (*this)[l];
  60296. cimg_forZ(img,z) img._save_tiff(tif,dir++,z,compression_type,voxel_size,description);
  60297. }
  60298. TIFFClose(tif);
  60299. } else
  60300. throw CImgIOException(_cimglist_instance
  60301. "save_tiff(): Failed to open stream for file '%s'.",
  60302. cimglist_instance,
  60303. filename);
  60304. #endif
  60305. return *this;
  60306. }
  60307. //! Save list as a gzipped file, using external tool 'gzip'.
  60308. /**
  60309. \param filename Filename to write data to.
  60310. **/
  60311. const CImgList<T>& save_gzip_external(const char *const filename) const {
  60312. if (!filename)
  60313. throw CImgIOException(_cimglist_instance
  60314. "save_gzip_external(): Specified filename is (null).",
  60315. cimglist_instance);
  60316. CImg<charT> command(1024), filename_tmp(256), body(256);
  60317. const char
  60318. *ext = cimg::split_filename(filename,body),
  60319. *ext2 = cimg::split_filename(body,0);
  60320. std::FILE *file;
  60321. do {
  60322. if (!cimg::strcasecmp(ext,"gz")) {
  60323. if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  60324. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
  60325. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
  60326. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  60327. } else {
  60328. if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
  60329. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
  60330. else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
  60331. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  60332. }
  60333. if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
  60334. } while (file);
  60335. if (is_saveable(body)) {
  60336. save(filename_tmp);
  60337. cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
  60338. cimg::gzip_path(),
  60339. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  60340. CImg<charT>::string(filename)._system_strescape().data());
  60341. cimg::system(command, cimg::gzip_path());
  60342. file = cimg::std_fopen(filename,"rb");
  60343. if (!file)
  60344. throw CImgIOException(_cimglist_instance
  60345. "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.",
  60346. cimglist_instance,
  60347. filename);
  60348. else cimg::fclose(file);
  60349. std::remove(filename_tmp);
  60350. } else {
  60351. CImg<charT> nfilename(1024);
  60352. cimglist_for(*this,l) {
  60353. cimg::number_filename(body,l,6,nfilename);
  60354. if (*ext) cimg_sprintf(nfilename._data + std::strlen(nfilename),".%s",ext);
  60355. _data[l].save_gzip_external(nfilename);
  60356. }
  60357. }
  60358. return *this;
  60359. }
  60360. //! Save image sequence (using the OpenCV library when available).
  60361. /**
  60362. \param filename Filename to write data to.
  60363. \param fps Number of frames per second.
  60364. \param codec Type of compression (See http://www.fourcc.org/codecs.php to see available codecs).
  60365. \param keep_open Tells if the video writer associated to the specified filename
  60366. must be kept open or not (to allow frames to be added in the same file afterwards).
  60367. **/
  60368. const CImgList<T>& save_video(const char *const filename, const unsigned int fps=25,
  60369. const char *codec=0, const bool keep_open=false) const {
  60370. #ifndef cimg_use_opencv
  60371. cimg::unused(codec,keep_open);
  60372. return save_ffmpeg_external(filename,fps);
  60373. #else
  60374. try {
  60375. static cv::VideoWriter *writers[32] = { 0 };
  60376. static CImgList<charT> filenames(32);
  60377. static CImg<intT> sizes(32,2,1,1,0);
  60378. static int last_used_index = -1;
  60379. // Detect if a video writer already exists for the specified filename.
  60380. cimg::mutex(9);
  60381. int index = -1;
  60382. if (filename) {
  60383. if (last_used_index>=0 && !std::strcmp(filename,filenames[last_used_index])) {
  60384. index = last_used_index;
  60385. } else cimglist_for(filenames,l) if (filenames[l] && !std::strcmp(filename,filenames[l])) {
  60386. index = l; break;
  60387. }
  60388. } else index = last_used_index;
  60389. cimg::mutex(9,0);
  60390. // Find empty slot for capturing video stream.
  60391. if (index<0) {
  60392. if (!filename)
  60393. throw CImgArgumentException(_cimglist_instance
  60394. "save_video(): No already open video writer found. You must specify a "
  60395. "non-(null) filename argument for the first call.",
  60396. cimglist_instance);
  60397. else { cimg::mutex(9); cimglist_for(filenames,l) if (!filenames[l]) { index = l; break; } cimg::mutex(9,0); }
  60398. if (index<0)
  60399. throw CImgIOException(_cimglist_instance
  60400. "save_video(): File '%s', no video writer slots available. "
  60401. "You have to release some of your previously opened videos.",
  60402. cimglist_instance,filename);
  60403. if (is_empty())
  60404. throw CImgInstanceException(_cimglist_instance
  60405. "save_video(): Instance list is empty.",
  60406. cimglist_instance);
  60407. const unsigned int W = _data?_data[0]._width:0, H = _data?_data[0]._height:0;
  60408. if (!W || !H)
  60409. throw CImgInstanceException(_cimglist_instance
  60410. "save_video(): Frame [0] is an empty image.",
  60411. cimglist_instance);
  60412. const char
  60413. *const _codec = codec && *codec?codec:"h264",
  60414. codec0 = cimg::uppercase(_codec[0]),
  60415. codec1 = _codec[0]?cimg::uppercase(_codec[1]):0,
  60416. codec2 = _codec[1]?cimg::uppercase(_codec[2]):0,
  60417. codec3 = _codec[2]?cimg::uppercase(_codec[3]):0;
  60418. cimg::mutex(9);
  60419. writers[index] = new cv::VideoWriter(filename,_cimg_fourcc(codec0,codec1,codec2,codec3),fps,cv::Size(W,H));
  60420. if (!writers[index]->isOpened()) {
  60421. delete writers[index];
  60422. writers[index] = 0;
  60423. cimg::mutex(9,0);
  60424. throw CImgIOException(_cimglist_instance
  60425. "save_video(): File '%s', unable to initialize video writer with codec '%c%c%c%c'.",
  60426. cimglist_instance,filename,
  60427. codec0,codec1,codec2,codec3);
  60428. }
  60429. CImg<charT>::string(filename).move_to(filenames[index]);
  60430. sizes(index,0) = W;
  60431. sizes(index,1) = H;
  60432. cimg::mutex(9,0);
  60433. }
  60434. if (!is_empty()) {
  60435. const unsigned int W = sizes(index,0), H = sizes(index,1);
  60436. cimg::mutex(9);
  60437. cimglist_for(*this,l) {
  60438. CImg<T> &src = _data[l];
  60439. if (src.is_empty())
  60440. cimg::warn(_cimglist_instance
  60441. "save_video(): Skip empty frame %d for file '%s'.",
  60442. cimglist_instance,l,filename);
  60443. if (src._spectrum>3)
  60444. cimg::warn(_cimglist_instance
  60445. "save_video(): Frame %u has incompatible dimension (%u,%u,%u,%u). "
  60446. "Some image data may be ignored when writing frame into video file '%s'.",
  60447. cimglist_instance,l,src._width,src._height,src._depth,src._spectrum,filename);
  60448. cimg_forZ(src,z) {
  60449. CImg<T> _src = src._depth>1?src.get_slice(z):src.get_shared();
  60450. if (_src._width==W && _src._height==H && _src._spectrum==3)
  60451. writers[index]->write(CImg<ucharT>(_src)._cimg2cvmat());
  60452. else {
  60453. CImg<ucharT> __src(_src,false);
  60454. __src.channels(0,std::min(__src._spectrum - 1,2U)).resize(W,H);
  60455. __src.resize(W,H,1,3,__src._spectrum==1);
  60456. writers[index]->write(__src._cimg2cvmat());
  60457. }
  60458. }
  60459. }
  60460. cimg::mutex(9,0);
  60461. }
  60462. cimg::mutex(9);
  60463. if (!keep_open) {
  60464. delete writers[index];
  60465. writers[index] = 0;
  60466. filenames[index].assign();
  60467. sizes(index,0) = sizes(index,1) = 0;
  60468. last_used_index = -1;
  60469. } else last_used_index = index;
  60470. cimg::mutex(9,0);
  60471. } catch (CImgIOException &e) {
  60472. if (!keep_open) return save_ffmpeg_external(filename,fps);
  60473. throw e;
  60474. }
  60475. return *this;
  60476. #endif
  60477. }
  60478. //! Save image sequence, using the external tool 'ffmpeg'.
  60479. /**
  60480. \param filename Filename to write data to.
  60481. \param fps Number of frames per second.
  60482. \param codec Type of compression.
  60483. \param bitrate Output bitrate
  60484. **/
  60485. const CImgList<T>& save_ffmpeg_external(const char *const filename, const unsigned int fps=25,
  60486. const char *const codec=0, const unsigned int bitrate=2048) const {
  60487. if (!filename)
  60488. throw CImgArgumentException(_cimglist_instance
  60489. "save_ffmpeg_external(): Specified filename is (null).",
  60490. cimglist_instance);
  60491. if (is_empty()) { cimg::fempty(0,filename); return *this; }
  60492. const char
  60493. *const ext = cimg::split_filename(filename),
  60494. *const _codec = codec?codec:
  60495. !cimg::strcasecmp(ext,"flv")?"flv":
  60496. !cimg::strcasecmp(ext,"mp4")?"h264":"mpeg2video";
  60497. CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
  60498. CImgList<charT> filenames;
  60499. std::FILE *file = 0;
  60500. cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0]))
  60501. throw CImgInstanceException(_cimglist_instance
  60502. "save_ffmpeg_external(): Invalid instance dimensions for file '%s'.",
  60503. cimglist_instance,
  60504. filename);
  60505. do {
  60506. cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
  60507. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
  60508. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001.ppm",filename_tmp._data);
  60509. if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
  60510. } while (file);
  60511. unsigned int frame = 1;
  60512. cimglist_for(*this,l) {
  60513. CImg<T>& src = _data[l];
  60514. cimg_forZ(src,z) {
  60515. cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u.ppm",filename_tmp._data,frame);
  60516. CImg<charT>::string(filename_tmp2).move_to(filenames);
  60517. CImg<T> _src = src._depth>1?src.get_slice(z):src.get_shared();
  60518. if (_src._width%2 || _src._height%2) // Force output to have an even number of columns and rows
  60519. _src.assign(_src.get_resize(_src._width + (_src._width%2),_src._height + (_src._height%2),1,-100,0),false);
  60520. if (_src._spectrum!=3) // Force output to be one slice, in color
  60521. _src.assign(_src.get_resize(-100,-100,1,3),false);
  60522. _src.save_pnm(filename_tmp2);
  60523. ++frame;
  60524. }
  60525. }
  60526. cimg_snprintf(command,command._width,
  60527. "\"%s\" -v -8 -y -i \"%s_%%6d.ppm\" -pix_fmt yuv420p -vcodec %s -b %uk -r %u \"%s\"",
  60528. cimg::ffmpeg_path(),
  60529. CImg<charT>::string(filename_tmp)._system_strescape().data(),
  60530. _codec,bitrate,fps,
  60531. CImg<charT>::string(filename)._system_strescape().data());
  60532. cimg::system(command, cimg::ffmpeg_path());
  60533. file = cimg::std_fopen(filename,"rb");
  60534. if (!file)
  60535. throw CImgIOException(_cimglist_instance
  60536. "save_ffmpeg_external(): Failed to save file '%s' with external command 'ffmpeg'.",
  60537. cimglist_instance,
  60538. filename);
  60539. else cimg::fclose(file);
  60540. cimglist_for(*this,l) std::remove(filenames[l]);
  60541. return *this;
  60542. }
  60543. //! Serialize a CImgList<T> instance into a raw CImg<unsigned char> buffer.
  60544. /**
  60545. \param is_compressed tells if zlib compression must be used for serialization
  60546. (this requires 'cimg_use_zlib' been enabled).
  60547. **/
  60548. CImg<ucharT> get_serialize(const bool is_compressed=false) const {
  60549. #ifndef cimg_use_zlib
  60550. if (is_compressed)
  60551. cimg::warn(_cimglist_instance
  60552. "get_serialize(): Unable to compress data unless zlib is enabled, "
  60553. "storing them uncompressed.",
  60554. cimglist_instance);
  60555. #endif
  60556. CImgList<ucharT> stream;
  60557. CImg<charT> tmpstr(128);
  60558. const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
  60559. if (std::strstr(ptype,"unsigned")==ptype)
  60560. cimg_snprintf(tmpstr,tmpstr._width,"%u unsigned_%s %s_endian\n",_width,ptype + 9,etype);
  60561. else
  60562. cimg_snprintf(tmpstr,tmpstr._width,"%u %s %s_endian\n",_width,ptype,etype);
  60563. CImg<ucharT>::string(tmpstr,false).move_to(stream);
  60564. cimglist_for(*this,l) {
  60565. const CImg<T>& img = _data[l];
  60566. cimg_snprintf(tmpstr,tmpstr._width,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);
  60567. CImg<ucharT>::string(tmpstr,false).move_to(stream);
  60568. if (img._data) {
  60569. CImg<T> tmp;
  60570. if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); }
  60571. const CImg<T>& ref = cimg::endianness()?tmp:img;
  60572. bool failed_to_compress = true;
  60573. if (is_compressed) {
  60574. #ifdef cimg_use_zlib
  60575. const ulongT siz = sizeof(T)*ref.size();
  60576. uLongf csiz = (ulongT)compressBound(siz);
  60577. Bytef *const cbuf = new Bytef[csiz];
  60578. if (compress(cbuf,&csiz,(Bytef*)ref._data,siz))
  60579. cimg::warn(_cimglist_instance
  60580. "get_serialize(): Failed to save compressed data, saving them uncompressed.",
  60581. cimglist_instance);
  60582. else {
  60583. cimg_snprintf(tmpstr,tmpstr._width," #%lu\n",csiz);
  60584. CImg<ucharT>::string(tmpstr,false).move_to(stream);
  60585. CImg<ucharT>(cbuf,csiz).move_to(stream);
  60586. delete[] cbuf;
  60587. failed_to_compress = false;
  60588. }
  60589. #endif
  60590. }
  60591. if (failed_to_compress) { // Write in a non-compressed way
  60592. CImg<charT>::string("\n",false).move_to(stream);
  60593. stream.insert(1);
  60594. stream.back().assign((unsigned char*)ref._data,ref.size()*sizeof(T),1,1,1,true);
  60595. }
  60596. } else CImg<charT>::string("\n",false).move_to(stream);
  60597. }
  60598. cimglist_apply(stream,unroll)('y');
  60599. return stream>'y';
  60600. }
  60601. //! Unserialize a CImg<unsigned char> serialized buffer into a CImgList<T> list.
  60602. template<typename t>
  60603. static CImgList<T> get_unserialize(const CImg<t>& buffer) {
  60604. #ifdef cimg_use_zlib
  60605. #define _cimgz_unserialize_case(Tss) { \
  60606. Bytef *cbuf = 0; \
  60607. if (sizeof(t)!=1 || buffer.pixel_type()==cimg::type<bool>::string()) { \
  60608. cbuf = new Bytef[csiz]; Bytef *_cbuf = cbuf; \
  60609. for (ulongT k = 0; k<csiz; ++k) *(_cbuf++) = (Bytef)*(stream++); \
  60610. is_bytef = false; \
  60611. } else { cbuf = (Bytef*)stream; stream+=csiz; is_bytef = true; } \
  60612. raw.assign(W,H,D,C); \
  60613. uLongf destlen = raw.size()*sizeof(Tss); \
  60614. uncompress((Bytef*)raw._data,&destlen,cbuf,csiz); \
  60615. if (!is_bytef) delete[] cbuf; \
  60616. }
  60617. #else
  60618. #define _cimgz_unserialize_case(Tss) \
  60619. throw CImgArgumentException("CImgList<%s>::get_unserialize(): Unable to unserialize compressed data " \
  60620. "unless zlib is enabled.", \
  60621. pixel_type());
  60622. #endif
  60623. #define _cimg_unserialize_case(Ts,Tss) \
  60624. if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
  60625. for (unsigned int l = 0; l<N; ++l) { \
  60626. j = 0; while ((i=(int)*stream)!='\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; } \
  60627. ++stream; tmp[j] = 0; \
  60628. W = H = D = C = 0; csiz = 0; \
  60629. if ((err = cimg_sscanf(tmp,"%u %u %u %u #" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \
  60630. throw CImgArgumentException("CImgList<%s>::unserialize(): Invalid specified size (%u,%u,%u,%u) for " \
  60631. "image #%u in serialized buffer.", \
  60632. pixel_type(),W,H,D,C,l); \
  60633. if (W*H*D*C>0) { \
  60634. CImg<Tss> raw; \
  60635. CImg<T> &img = res._data[l]; \
  60636. if (err==5) _cimgz_unserialize_case(Tss) \
  60637. else { \
  60638. raw.assign(W,H,D,C); \
  60639. CImg<ucharT> _raw((unsigned char*)raw._data,W*sizeof(Tss),H,D,C,true); \
  60640. if (sizeof(t)==1) { std::memcpy(_raw,stream,_raw.size()); stream+=_raw.size(); } \
  60641. else cimg_for(_raw,p,unsigned char) *p = (unsigned char)*(stream++); \
  60642. } \
  60643. if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
  60644. raw.move_to(img); \
  60645. } \
  60646. } \
  60647. loaded = true; \
  60648. }
  60649. if (buffer.is_empty())
  60650. throw CImgArgumentException("CImgList<%s>::get_unserialize(): Specified serialized buffer is (null).",
  60651. pixel_type());
  60652. CImgList<T> res;
  60653. const t *stream = buffer._data, *const estream = buffer._data + buffer.size();
  60654. bool loaded = false, endian = cimg::endianness(), is_bytef = false;
  60655. CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
  60656. *tmp = *str_pixeltype = *str_endian = 0;
  60657. unsigned int j, N = 0, W, H, D, C;
  60658. uint64T csiz;
  60659. int i, err;
  60660. cimg::unused(is_bytef);
  60661. do {
  60662. j = 0; while ((i=(int)*stream)!='\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; }
  60663. ++stream; tmp[j] = 0;
  60664. } while (*tmp=='#' && stream<estream);
  60665. err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
  60666. &N,str_pixeltype._data,str_endian._data);
  60667. if (err<2)
  60668. throw CImgArgumentException("CImgList<%s>::get_unserialize(): CImg header not found in serialized buffer.",
  60669. pixel_type());
  60670. if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
  60671. else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
  60672. res.assign(N);
  60673. _cimg_unserialize_case("bool",bool);
  60674. _cimg_unserialize_case("unsigned_char",unsigned char);
  60675. _cimg_unserialize_case("uchar",unsigned char);
  60676. _cimg_unserialize_case("char",char);
  60677. _cimg_unserialize_case("unsigned_short",unsigned short);
  60678. _cimg_unserialize_case("ushort",unsigned short);
  60679. _cimg_unserialize_case("short",short);
  60680. _cimg_unserialize_case("unsigned_int",unsigned int);
  60681. _cimg_unserialize_case("uint",unsigned int);
  60682. _cimg_unserialize_case("int",int);
  60683. _cimg_unserialize_case("unsigned_int64",uint64T);
  60684. _cimg_unserialize_case("uint64",uint64T);
  60685. _cimg_unserialize_case("int64",int64T);
  60686. _cimg_unserialize_case("float",float);
  60687. _cimg_unserialize_case("double",double);
  60688. if (!loaded)
  60689. throw CImgArgumentException("CImgList<%s>::get_unserialize(): Unsupported pixel type '%s' defined "
  60690. "in serialized buffer.",
  60691. pixel_type(),str_pixeltype._data);
  60692. return res;
  60693. }
  60694. //@}
  60695. //----------------------------------
  60696. //
  60697. //! \name Others
  60698. //@{
  60699. //----------------------------------
  60700. //! Return a CImg pre-defined font with requested height.
  60701. /**
  60702. \param font_height Height of the desired font (exact match for 13,23,53,103).
  60703. \param is_variable_width Decide if the font has a variable (\c true) or fixed (\c false) width.
  60704. **/
  60705. static const CImgList<ucharT>& font(const unsigned int requested_height, const bool is_variable_width=true) {
  60706. if (!requested_height) return CImgList<ucharT>::const_empty();
  60707. cimg::mutex(11);
  60708. static const unsigned char font_resizemap[] = {
  60709. 0, 4, 7, 9, 11, 13, 15, 17, 19, 21, 22, 24, 26, 27, 29, 30,
  60710. 32, 33, 35, 36, 38, 39, 41, 42, 43, 45, 46, 47, 49, 50, 51, 52,
  60711. 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 71, 72,
  60712. 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  60713. 90, 91, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
  60714. 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
  60715. 123, 124, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
  60716. 138, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 148, 149, 150, 151,
  60717. 152, 153, 154, 155, 156, 157, 157, 158, 159, 160, 161, 162, 163, 164, 164, 165,
  60718. 166, 167, 168, 169, 170, 170, 171, 172, 173, 174, 175, 176, 176, 177, 178, 179,
  60719. 180, 181, 181, 182, 183, 184, 185, 186, 186, 187, 188, 189, 190, 191, 191, 192,
  60720. 193, 194, 195, 196, 196, 197, 198, 199, 200, 200, 201, 202, 203, 204, 205, 205,
  60721. 206, 207, 208, 209, 209, 210, 211, 212, 213, 213, 214, 215, 216, 216, 217, 218,
  60722. 219, 220, 220, 221, 222, 223, 224, 224, 225, 226, 227, 227, 228, 229, 230, 231,
  60723. 231, 232, 233, 234, 234, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243,
  60724. 244, 244, 245, 246, 247, 247, 248, 249, 250, 250, 251, 252, 253, 253, 254, 255 };
  60725. static const char *const *font_data[] = {
  60726. cimg::data_font_small,
  60727. cimg::data_font_normal,
  60728. cimg::data_font_large,
  60729. cimg::data_font_huge };
  60730. static const unsigned int
  60731. font_width[] = { 10,26,52,104 },
  60732. font_height[] = { 13,32,64,128 },
  60733. font_M[] = { 86,91,91,47 },
  60734. font_chunk[] = { sizeof(cimg::data_font_small)/sizeof(char*),
  60735. sizeof(cimg::data_font_normal)/sizeof(char*),
  60736. sizeof(cimg::data_font_large)/sizeof(char*),
  60737. sizeof(cimg::data_font_huge)/sizeof(char*) };
  60738. static const unsigned char font_is_binary[] = { 1,0,0,1 };
  60739. static CImg<ucharT> font_base[4];
  60740. unsigned int ind =
  60741. requested_height<=font_height[0]?0U:
  60742. requested_height<=font_height[1]?1U:
  60743. requested_height<=font_height[2]?2U:3U;
  60744. // Decompress nearest base font data if needed.
  60745. CImg<ucharT> &basef = font_base[ind];
  60746. if (!basef) {
  60747. basef.assign(256*font_width[ind],font_height[ind]);
  60748. unsigned char *ptrd = basef;
  60749. const unsigned char *const ptrde = basef.end();
  60750. // Recompose font data from several chunks, to deal with MS compiler limit with big strings (64 Kb).
  60751. CImg<char> dataf;
  60752. for (unsigned int k = 0; k<font_chunk[ind]; ++k)
  60753. dataf.append(CImg<char>::string(font_data[ind][k],k==font_chunk[ind] - 1,true),'x');
  60754. // Uncompress font data (decode RLE).
  60755. const unsigned int M = font_M[ind];
  60756. if (font_is_binary[ind])
  60757. for (const char *ptrs = dataf; *ptrs; ++ptrs) {
  60758. const int _n = (int)(*ptrs - M - 32), v = _n>=0?255:0, n = _n>=0?_n:-_n;
  60759. if (ptrd + n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; }
  60760. else { std::memset(ptrd,v,ptrde - ptrd); break; }
  60761. }
  60762. else
  60763. for (const char *ptrs = dataf; *ptrs; ++ptrs) {
  60764. int n = (int)*ptrs - M - 32, v = 0;
  60765. if (n>=0) { v = 85*n; n = 1; }
  60766. else {
  60767. n = -n;
  60768. v = (int)*(++ptrs) - M - 32;
  60769. if (v<0) { v = 0; --ptrs; } else v*=85;
  60770. }
  60771. if (ptrd + n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; }
  60772. else { std::memset(ptrd,v,ptrde - ptrd); break; }
  60773. }
  60774. }
  60775. // Find optimal font cache location to return.
  60776. static CImgList<ucharT> fonts[16];
  60777. static bool is_variable_widths[16] = { 0 };
  60778. ind = ~0U;
  60779. for (int i = 0; i<16; ++i)
  60780. if (!fonts[i] || (is_variable_widths[i]==is_variable_width && requested_height==fonts[i][0]._height)) {
  60781. ind = (unsigned int)i; break; // Found empty slot or cached font
  60782. }
  60783. if (ind==~0U) { // No empty slots nor existing font in cache
  60784. fonts->assign();
  60785. std::memmove((void*)fonts,(void*)(fonts + 1),15*sizeof(CImgList<ucharT>));
  60786. std::memmove(is_variable_widths,is_variable_widths + 1,15*sizeof(bool));
  60787. std::memset((void*)(fonts + (ind=15)),0,sizeof(CImgList<ucharT>)); // Free a slot in cache for new font
  60788. }
  60789. CImgList<ucharT> &font = fonts[ind];
  60790. // Render requested font.
  60791. if (!font) {
  60792. is_variable_widths[ind] = is_variable_width;
  60793. basef.get_split('x',256).move_to(font);
  60794. // cimg::tic();
  60795. if (requested_height!=font[0]._height)
  60796. cimglist_for(font,l) {
  60797. font[l].resize(std::max(1U,font[l]._width*requested_height/font[l]._height),requested_height,-100,-100,5);
  60798. cimg_for(font[l],ptr,ucharT) *ptr = font_resizemap[*ptr];
  60799. }
  60800. // cimg::toc();
  60801. // std::exit(0);
  60802. if (is_variable_width) { // Crop font
  60803. cimglist_for(font,l) {
  60804. CImg<ucharT>& letter = font[l];
  60805. int xmin = letter.width(), xmax = 0;
  60806. cimg_forX(letter,x) { // Find xmin
  60807. cimg_forY(letter,y) if (letter(x,y)) { xmin = x; break; }
  60808. if (xmin!=letter.width()) break;
  60809. }
  60810. cimg_rofX(letter,x) { // Find xmax
  60811. cimg_forY(letter,y) if (letter(x,y)) { xmax = x; break; }
  60812. if (xmax) break;
  60813. }
  60814. if (xmin<=xmax) letter.crop(xmin,0,xmax,letter._height - 1);
  60815. }
  60816. font[(int)' '].resize(font[(int)'f']._width,-100,-100,-100,0);
  60817. if (' ' + 256<font.size()) font[' ' + 256].resize(font[(int)'f']._width,-100,-100,-100,0);
  60818. }
  60819. font.insert(256,0);
  60820. cimglist_for_in(font,0,255,l) font[l].assign(font[l + 256]._width,font[l + 256]._height,1,3,1);
  60821. }
  60822. cimg::mutex(11,0);
  60823. return font;
  60824. }
  60825. //! Compute a 1D Fast Fourier Transform, along specified axis.
  60826. /**
  60827. \param axis Axis along which the Fourier transform is computed.
  60828. \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed.
  60829. **/
  60830. CImgList<T>& FFT(const char axis, const bool invert=false) {
  60831. if (is_empty()) return *this;
  60832. if (_width==1) insert(1);
  60833. if (_width>2)
  60834. cimg::warn(_cimglist_instance
  60835. "FFT(): Instance has more than 2 images",
  60836. cimglist_instance);
  60837. CImg<T>::FFT(_data[0],_data[1],axis,invert);
  60838. return *this;
  60839. }
  60840. //! Compute a 1-D Fast Fourier Transform, along specified axis \newinstance.
  60841. CImgList<Tfloat> get_FFT(const char axis, const bool invert=false) const {
  60842. return CImgList<Tfloat>(*this,false).FFT(axis,invert);
  60843. }
  60844. //! Compute n-D Fast Fourier Transform.
  60845. /**
  60846. \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed.
  60847. **/
  60848. CImgList<T>& FFT(const bool invert=false) {
  60849. if (is_empty()) return *this;
  60850. if (_width==1) insert(1);
  60851. if (_width>2)
  60852. cimg::warn(_cimglist_instance
  60853. "FFT(): Instance has more than 2 images",
  60854. cimglist_instance);
  60855. CImg<T>::FFT(_data[0],_data[1],invert);
  60856. return *this;
  60857. }
  60858. //! Compute n-D Fast Fourier Transform \newinstance.
  60859. CImgList<Tfloat> get_FFT(const bool invert=false) const {
  60860. return CImgList<Tfloat>(*this,false).FFT(invert);
  60861. }
  60862. //! Reverse primitives orientations of a 3D object.
  60863. /**
  60864. **/
  60865. CImgList<T>& reverse_object3d() {
  60866. cimglist_for(*this,l) {
  60867. CImg<T>& p = _data[l];
  60868. switch (p.size()) {
  60869. case 2 : case 3: cimg::swap(p[0],p[1]); break;
  60870. case 6 : cimg::swap(p[0],p[1],p[2],p[4],p[3],p[5]); break;
  60871. case 9 : cimg::swap(p[0],p[1],p[3],p[5],p[4],p[6]); break;
  60872. case 4 : cimg::swap(p[0],p[1],p[2],p[3]); break;
  60873. case 12 : cimg::swap(p[0],p[1],p[2],p[3],p[4],p[6],p[5],p[7],p[8],p[10],p[9],p[11]); break;
  60874. }
  60875. }
  60876. return *this;
  60877. }
  60878. //! Reverse primitives orientations of a 3D object \newinstance.
  60879. CImgList<T> get_reverse_object3d() const {
  60880. return (+*this).reverse_object3d();
  60881. }
  60882. //@}
  60883. }; // struct CImgList { ...
  60884. // Completion of previously declared functions
  60885. //--------------------------------------------
  60886. namespace cimg {
  60887. // Functions to return standard streams 'stdin', 'stdout' and 'stderr'.
  60888. // (throw a CImgIOException when macro 'cimg_use_r' is defined).
  60889. inline FILE* _stdin(const bool throw_exception) {
  60890. #ifndef cimg_use_r
  60891. cimg::unused(throw_exception);
  60892. return stdin;
  60893. #else
  60894. if (throw_exception) {
  60895. cimg::exception_mode(0);
  60896. throw CImgIOException("cimg::stdin(): Reference to 'stdin' stream not allowed in R mode "
  60897. "('cimg_use_r' is defined).");
  60898. }
  60899. return 0;
  60900. #endif
  60901. }
  60902. inline FILE* _stdout(const bool throw_exception) {
  60903. #ifndef cimg_use_r
  60904. cimg::unused(throw_exception);
  60905. return stdout;
  60906. #else
  60907. if (throw_exception) {
  60908. cimg::exception_mode(0);
  60909. throw CImgIOException("cimg::stdout(): Reference to 'stdout' stream not allowed in R mode "
  60910. "('cimg_use_r' is defined).");
  60911. }
  60912. return 0;
  60913. #endif
  60914. }
  60915. inline FILE* _stderr(const bool throw_exception) {
  60916. #ifndef cimg_use_r
  60917. cimg::unused(throw_exception);
  60918. return stderr;
  60919. #else
  60920. if (throw_exception) {
  60921. cimg::exception_mode(0);
  60922. throw CImgIOException("cimg::stderr(): Reference to 'stderr' stream not allowed in R mode "
  60923. "('cimg_use_r' is defined).");
  60924. }
  60925. return 0;
  60926. #endif
  60927. }
  60928. // Open a file (similar to std:: fopen(), but with wide character support on Windows).
  60929. inline std::FILE *std_fopen(const char *const path, const char *const mode) {
  60930. std::FILE *const res = std::fopen(path,mode);
  60931. if (res) return res;
  60932. #if cimg_OS==2
  60933. // Try alternative method, with wide-character string.
  60934. int err = MultiByteToWideChar(CP_UTF8,0,path,-1,0,0);
  60935. if (err) {
  60936. CImg<wchar_t> wpath((unsigned int)err);
  60937. err = MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err);
  60938. if (err) { // Convert 'mode' to a wide-character string
  60939. err = MultiByteToWideChar(CP_UTF8,0,mode,-1,0,0);
  60940. if (err) {
  60941. CImg<wchar_t> wmode((unsigned int)err);
  60942. if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wmode,err))
  60943. return _wfopen(wpath,wmode);
  60944. }
  60945. }
  60946. }
  60947. #endif
  60948. return 0;
  60949. }
  60950. //! Search path of an executable (Windows only).
  60951. #if cimg_OS==2
  60952. inline bool win_searchpath(const char *const exec_name, char *const res, const unsigned int size_res) {
  60953. char *ptr = 0;
  60954. DWORD err = SearchPathA(0,exec_name,0,size_res,res,&ptr);
  60955. return err!=0;
  60956. }
  60957. #endif
  60958. //! Get the file or directory attributes with support for UTF-8 paths (Windows only).
  60959. #if cimg_OS==2
  60960. inline DWORD win_getfileattributes(const char *const path) {
  60961. DWORD res = GetFileAttributesA(path);
  60962. if (res==INVALID_FILE_ATTRIBUTES) {
  60963. // Try alternative method, with wide-character string.
  60964. int err = MultiByteToWideChar(CP_UTF8,0,path,-1,0,0);
  60965. if (err) {
  60966. CImg<wchar_t> wpath((unsigned int)err);
  60967. if (MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err)) res = GetFileAttributesW(wpath);
  60968. }
  60969. }
  60970. return res;
  60971. }
  60972. #endif
  60973. //! Get/set path to the <i>Program Files/</i> directory (Windows only).
  60974. /**
  60975. \param user_path Specified path, or \c 0 to get the path currently used.
  60976. \param reinit_path Force path to be recalculated (may take some time).
  60977. \return Path containing the program files.
  60978. **/
  60979. #if cimg_OS==2
  60980. inline const char* win_programfiles_path(const char *const user_path=0, const bool reinit_path=false) {
  60981. static CImg<char> s_path;
  60982. cimg::mutex(7);
  60983. if (reinit_path) s_path.assign();
  60984. if (user_path) {
  60985. if (!s_path) s_path.assign(1024);
  60986. std::strncpy(s_path,user_path,1023);
  60987. } else if (!s_path) {
  60988. s_path.assign(MAX_PATH);
  60989. *s_path = 0;
  60990. // Note: in the following line, 0x26 = CSIDL_PROGRAM_FILES (not defined on every compiler).
  60991. #if !defined(__INTEL_COMPILER)
  60992. if (!SHGetSpecialFolderPathA(0,s_path,0x0026,false)) {
  60993. const char *const pfPath = std::getenv("PROGRAMFILES");
  60994. if (pfPath) std::strncpy(s_path,pfPath,MAX_PATH - 1);
  60995. else std::strcpy(s_path,"C:\\PROGRA~1");
  60996. }
  60997. #else
  60998. std::strcpy(s_path,"C:\\PROGRA~1");
  60999. #endif
  61000. }
  61001. cimg::mutex(7,0);
  61002. return s_path;
  61003. }
  61004. #endif
  61005. //! Get/set path to the \c curl binary.
  61006. /**
  61007. \param user_path Specified path, or \c 0 to get the path currently used.
  61008. \param reinit_path Force path to be recalculated (may take some time).
  61009. \return Path containing the \c curl binary.
  61010. **/
  61011. inline const char *curl_path(const char *const user_path, const bool reinit_path) {
  61012. static CImg<char> s_path;
  61013. cimg::mutex(7);
  61014. if (reinit_path) s_path.assign();
  61015. if (user_path) {
  61016. if (!s_path) s_path.assign(1024);
  61017. std::strncpy(s_path,user_path,1023);
  61018. } else if (!s_path) {
  61019. s_path.assign(1024);
  61020. bool path_found = false;
  61021. std::FILE *file = 0;
  61022. #if cimg_OS==2
  61023. if (win_searchpath("curl.exe",s_path,s_path._width)) path_found = true;
  61024. if (!path_found) {
  61025. std::strcpy(s_path,".\\curl.exe");
  61026. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61027. }
  61028. if (!path_found) std::strcpy(s_path,"curl.exe");
  61029. #else
  61030. if (!path_found) {
  61031. std::strcpy(s_path,"./curl");
  61032. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61033. }
  61034. if (!path_found) std::strcpy(s_path,"curl");
  61035. #endif
  61036. winformat_string(s_path);
  61037. }
  61038. cimg::mutex(7,0);
  61039. return s_path;
  61040. }
  61041. //! Get/set path to the \c dcraw binary.
  61042. /**
  61043. \param user_path Specified path, or \c 0 to get the path currently used.
  61044. \param reinit_path Force path to be recalculated (may take some time).
  61045. \return Path containing the \c dcraw binary.
  61046. **/
  61047. inline const char *dcraw_path(const char *const user_path, const bool reinit_path) {
  61048. static CImg<char> s_path;
  61049. cimg::mutex(7);
  61050. if (reinit_path) s_path.assign();
  61051. if (user_path) {
  61052. if (!s_path) s_path.assign(1024);
  61053. std::strncpy(s_path,user_path,1023);
  61054. } else if (!s_path) {
  61055. s_path.assign(1024);
  61056. bool path_found = false;
  61057. std::FILE *file = 0;
  61058. #if cimg_OS==2
  61059. if (win_searchpath("dcraw.exe",s_path,s_path._width)) path_found = true;
  61060. if (!path_found) {
  61061. std::strcpy(s_path,".\\dcraw.exe");
  61062. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61063. }
  61064. if (!path_found) std::strcpy(s_path,"dcraw.exe");
  61065. #else
  61066. if (!path_found) {
  61067. std::strcpy(s_path,"./dcraw");
  61068. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61069. }
  61070. if (!path_found) std::strcpy(s_path,"dcraw");
  61071. #endif
  61072. winformat_string(s_path);
  61073. }
  61074. cimg::mutex(7,0);
  61075. return s_path;
  61076. }
  61077. //! Get/set path to the FFMPEG's \c ffmpeg binary.
  61078. /**
  61079. \param user_path Specified path, or \c 0 to get the path currently used.
  61080. \param reinit_path Force path to be recalculated (may take some time).
  61081. \return Path containing the \c ffmpeg binary.
  61082. **/
  61083. inline const char *ffmpeg_path(const char *const user_path, const bool reinit_path) {
  61084. static CImg<char> s_path;
  61085. cimg::mutex(7);
  61086. if (reinit_path) s_path.assign();
  61087. if (user_path) {
  61088. if (!s_path) s_path.assign(1024);
  61089. std::strncpy(s_path,user_path,1023);
  61090. } else if (!s_path) {
  61091. s_path.assign(1024);
  61092. bool path_found = false;
  61093. std::FILE *file = 0;
  61094. #if cimg_OS==2
  61095. if (win_searchpath("ffmpeg.exe",s_path,s_path._width)) path_found = true;
  61096. if (!path_found) {
  61097. std::strcpy(s_path,".\\ffmpeg.exe");
  61098. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61099. }
  61100. if (!path_found) std::strcpy(s_path,"ffmpeg.exe");
  61101. #else
  61102. if (!path_found) {
  61103. std::strcpy(s_path,"./ffmpeg");
  61104. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61105. }
  61106. if (!path_found) std::strcpy(s_path,"ffmpeg");
  61107. #endif
  61108. winformat_string(s_path);
  61109. }
  61110. cimg::mutex(7,0);
  61111. return s_path;
  61112. }
  61113. //! Get/set path to the GraphicsMagick's \c gm binary.
  61114. /**
  61115. \param user_path Specified path, or \c 0 to get the path currently used.
  61116. \param reinit_path Force path to be recalculated (may take some time).
  61117. \return Path containing the \c gm binary.
  61118. **/
  61119. inline const char* graphicsmagick_path(const char *const user_path, const bool reinit_path) {
  61120. static CImg<char> s_path;
  61121. cimg::mutex(7);
  61122. if (reinit_path) s_path.assign();
  61123. if (user_path) {
  61124. if (!s_path) s_path.assign(1024);
  61125. std::strncpy(s_path,user_path,1023);
  61126. } else if (!s_path) {
  61127. s_path.assign(1024);
  61128. bool path_found = false;
  61129. std::FILE *file = 0;
  61130. #if cimg_OS==2
  61131. if (win_searchpath("gm.exe",s_path,s_path._width)) path_found = true;
  61132. const char *const pf_path = win_programfiles_path();
  61133. if (!path_found) {
  61134. std::strcpy(s_path,".\\gm.exe");
  61135. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61136. }
  61137. for (int k = 32; k>=10 && !path_found; --k) {
  61138. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%.2d-\\gm.exe",pf_path,k);
  61139. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61140. }
  61141. for (int k = 9; k>=0 && !path_found; --k) {
  61142. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d-Q\\gm.exe",pf_path,k);
  61143. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61144. }
  61145. for (int k = 32; k>=0 && !path_found; --k) {
  61146. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d\\gm.exe",pf_path,k);
  61147. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61148. }
  61149. for (int k = 32; k>=10 && !path_found; --k) {
  61150. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",pf_path,k);
  61151. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61152. }
  61153. for (int k = 9; k>=0 && !path_found; --k) {
  61154. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",pf_path,k);
  61155. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61156. }
  61157. for (int k = 32; k>=0 && !path_found; --k) {
  61158. cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",pf_path,k);
  61159. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61160. }
  61161. for (int k = 32; k>=10 && !path_found; --k) {
  61162. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%.2d-\\gm.exe",k);
  61163. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61164. }
  61165. for (int k = 9; k>=0 && !path_found; --k) {
  61166. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d-Q\\gm.exe",k);
  61167. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61168. }
  61169. for (int k = 32; k>=0 && !path_found; --k) {
  61170. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d\\gm.exe",k);
  61171. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61172. }
  61173. for (int k = 32; k>=10 && !path_found; --k) {
  61174. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k);
  61175. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61176. }
  61177. for (int k = 9; k>=0 && !path_found; --k) {
  61178. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k);
  61179. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61180. }
  61181. for (int k = 32; k>=0 && !path_found; --k) {
  61182. cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k);
  61183. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61184. }
  61185. for (int k = 32; k>=10 && !path_found; --k) {
  61186. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%.2d-\\gm.exe",k);
  61187. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61188. }
  61189. for (int k = 9; k>=0 && !path_found; --k) {
  61190. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d-Q\\gm.exe",k);
  61191. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61192. }
  61193. for (int k = 32; k>=0 && !path_found; --k) {
  61194. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d\\gm.exe",k);
  61195. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61196. }
  61197. for (int k = 32; k>=10 && !path_found; --k) {
  61198. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k);
  61199. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61200. }
  61201. for (int k = 9; k>=0 && !path_found; --k) {
  61202. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k);
  61203. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61204. }
  61205. for (int k = 32; k>=0 && !path_found; --k) {
  61206. cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k);
  61207. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61208. }
  61209. if (!path_found) std::strcpy(s_path,"gm.exe");
  61210. #else
  61211. if (!path_found) {
  61212. std::strcpy(s_path,"./gm");
  61213. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61214. }
  61215. if (!path_found) std::strcpy(s_path,"gm");
  61216. #endif
  61217. winformat_string(s_path);
  61218. }
  61219. cimg::mutex(7,0);
  61220. return s_path;
  61221. }
  61222. //! Get/set path to the \c gunzip binary.
  61223. /**
  61224. \param user_path Specified path, or \c 0 to get the path currently used.
  61225. \param reinit_path Force path to be recalculated (may take some time).
  61226. \return Path containing the \c gunzip binary.
  61227. **/
  61228. inline const char *gunzip_path(const char *const user_path, const bool reinit_path) {
  61229. static CImg<char> s_path;
  61230. cimg::mutex(7);
  61231. if (reinit_path) s_path.assign();
  61232. if (user_path) {
  61233. if (!s_path) s_path.assign(1024);
  61234. std::strncpy(s_path,user_path,1023);
  61235. } else if (!s_path) {
  61236. s_path.assign(1024);
  61237. bool path_found = false;
  61238. std::FILE *file = 0;
  61239. #if cimg_OS==2
  61240. if (win_searchpath("gunzip.exe",s_path,s_path._width)) path_found = true;
  61241. if (!path_found) {
  61242. std::strcpy(s_path,".\\gunzip.exe");
  61243. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61244. }
  61245. if (!path_found) std::strcpy(s_path,"gunzip.exe");
  61246. #else
  61247. if (!path_found) {
  61248. std::strcpy(s_path,"./gunzip");
  61249. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61250. }
  61251. if (!path_found) std::strcpy(s_path,"gunzip");
  61252. #endif
  61253. winformat_string(s_path);
  61254. }
  61255. cimg::mutex(7,0);
  61256. return s_path;
  61257. }
  61258. //! Get/set path to the \c gzip binary.
  61259. /**
  61260. \param user_path Specified path, or \c 0 to get the path currently used.
  61261. \param reinit_path Force path to be recalculated (may take some time).
  61262. \return Path containing the \c gzip binary.
  61263. **/
  61264. inline const char *gzip_path(const char *const user_path, const bool reinit_path) {
  61265. static CImg<char> s_path;
  61266. cimg::mutex(7);
  61267. if (reinit_path) s_path.assign();
  61268. if (user_path) {
  61269. if (!s_path) s_path.assign(1024);
  61270. std::strncpy(s_path,user_path,1023);
  61271. } else if (!s_path) {
  61272. s_path.assign(1024);
  61273. bool path_found = false;
  61274. std::FILE *file = 0;
  61275. #if cimg_OS==2
  61276. if (win_searchpath("gzip.exe",s_path,s_path._width)) path_found = true;
  61277. if (!path_found) {
  61278. std::strcpy(s_path,".\\gzip.exe");
  61279. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61280. }
  61281. if (!path_found) std::strcpy(s_path,"gzip.exe");
  61282. #else
  61283. if (!path_found) {
  61284. std::strcpy(s_path,"./gzip");
  61285. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61286. }
  61287. if (!path_found) std::strcpy(s_path,"gzip");
  61288. #endif
  61289. winformat_string(s_path);
  61290. }
  61291. cimg::mutex(7,0);
  61292. return s_path;
  61293. }
  61294. //! Get/set path to the ImageMagick's \c convert binary.
  61295. /**
  61296. \param user_path Specified path, or \c 0 to get the path currently used.
  61297. \param reinit_path Force path to be recalculated (may take some time).
  61298. \return Path containing the \c convert binary.
  61299. **/
  61300. inline const char* imagemagick_path(const char *const user_path, const bool reinit_path) {
  61301. static CImg<char> s_path;
  61302. cimg::mutex(7);
  61303. if (reinit_path) s_path.assign();
  61304. if (user_path) {
  61305. if (!s_path) s_path.assign(1024);
  61306. std::strncpy(s_path,user_path,1023);
  61307. } else if (!s_path) {
  61308. s_path.assign(1024);
  61309. bool path_found = false;
  61310. std::FILE *file = 0;
  61311. #if cimg_OS==2
  61312. if (win_searchpath("magick.exe",s_path,s_path._width)) path_found = true;
  61313. const char *const pf_path = win_programfiles_path();
  61314. for (int l = 0; l<2 && !path_found; ++l) {
  61315. const char *const s_exe = l?"convert":"magick";
  61316. cimg_snprintf(s_path,s_path._width,".\\%s.exe",s_exe);
  61317. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61318. for (int k = 32; k>=10 && !path_found; --k) {
  61319. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%.2d-\\%s.exe",pf_path,k,s_exe);
  61320. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61321. }
  61322. for (int k = 9; k>=0 && !path_found; --k) {
  61323. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d-Q\\%s.exe",pf_path,k,s_exe);
  61324. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61325. }
  61326. for (int k = 32; k>=0 && !path_found; --k) {
  61327. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d\\%s.exe",pf_path,k,s_exe);
  61328. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61329. }
  61330. for (int k = 32; k>=10 && !path_found; --k) {
  61331. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
  61332. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61333. }
  61334. for (int k = 9; k>=0 && !path_found; --k) {
  61335. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
  61336. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61337. }
  61338. for (int k = 32; k>=0 && !path_found; --k) {
  61339. cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
  61340. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61341. }
  61342. for (int k = 32; k>=10 && !path_found; --k) {
  61343. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%.2d-\\%s.exe",k,s_exe);
  61344. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61345. }
  61346. for (int k = 9; k>=0 && !path_found; --k) {
  61347. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d-Q\\%s.exe",k,s_exe);
  61348. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61349. }
  61350. for (int k = 32; k>=0 && !path_found; --k) {
  61351. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d\\%s.exe",k,s_exe);
  61352. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61353. }
  61354. for (int k = 32; k>=10 && !path_found; --k) {
  61355. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61356. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61357. }
  61358. for (int k = 9; k>=0 && !path_found; --k) {
  61359. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61360. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61361. }
  61362. for (int k = 32; k>=0 && !path_found; --k) {
  61363. cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61364. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61365. }
  61366. for (int k = 32; k>=10 && !path_found; --k) {
  61367. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%.2d-\\%s.exe",k,s_exe);
  61368. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61369. }
  61370. for (int k = 9; k>=0 && !path_found; --k) {
  61371. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d-Q\\%s.exe",k,s_exe);
  61372. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61373. }
  61374. for (int k = 32; k>=0 && !path_found; --k) {
  61375. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d\\%s.exe",k,s_exe);
  61376. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61377. }
  61378. for (int k = 32; k>=10 && !path_found; --k) {
  61379. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61380. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61381. }
  61382. for (int k = 9; k>=0 && !path_found; --k) {
  61383. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61384. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61385. }
  61386. for (int k = 32; k>=0 && !path_found; --k) {
  61387. cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",k,s_exe);
  61388. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61389. }
  61390. if (!path_found) cimg_snprintf(s_path,s_path._width,"%s.exe",s_exe);
  61391. }
  61392. #else
  61393. std::strcpy(s_path,"./magick");
  61394. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61395. if (!path_found) {
  61396. std::strcpy(s_path,"./convert");
  61397. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61398. }
  61399. if (!path_found) std::strcpy(s_path,"convert");
  61400. #endif
  61401. winformat_string(s_path);
  61402. }
  61403. cimg::mutex(7,0);
  61404. return s_path;
  61405. }
  61406. //! Get/set path to the Medcon's \c medcon binary.
  61407. /**
  61408. \param user_path Specified path, or \c 0 to get the path currently used.
  61409. \param reinit_path Force path to be recalculated (may take some time).
  61410. \return Path containing the \c medcon binary.
  61411. **/
  61412. inline const char* medcon_path(const char *const user_path, const bool reinit_path) {
  61413. static CImg<char> s_path;
  61414. cimg::mutex(7);
  61415. if (reinit_path) s_path.assign();
  61416. if (user_path) {
  61417. if (!s_path) s_path.assign(1024);
  61418. std::strncpy(s_path,user_path,1023);
  61419. } else if (!s_path) {
  61420. s_path.assign(1024);
  61421. bool path_found = false;
  61422. std::FILE *file = 0;
  61423. #if cimg_OS==2
  61424. if (win_searchpath("medcon.exe",s_path,s_path._width)) path_found = true;
  61425. const char *const pf_path = win_programfiles_path();
  61426. if (!path_found) {
  61427. std::strcpy(s_path,".\\medcon.exe");
  61428. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61429. }
  61430. if (!path_found) {
  61431. cimg_snprintf(s_path,s_path._width,"%s\\XMedCon\\bin\\medcon.bat",pf_path);
  61432. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61433. }
  61434. if (!path_found) {
  61435. cimg_snprintf(s_path,s_path._width,"%s\\XMedCon\\bin\\medcon.exe",pf_path);
  61436. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61437. }
  61438. if (!path_found) {
  61439. std::strcpy(s_path,"C:\\XMedCon\\bin\\medcon.exe");
  61440. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61441. }
  61442. if (!path_found) std::strcpy(s_path,"medcon.exe");
  61443. #else
  61444. if (!path_found) {
  61445. std::strcpy(s_path,"./medcon");
  61446. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61447. }
  61448. if (!path_found) std::strcpy(s_path,"medcon");
  61449. #endif
  61450. winformat_string(s_path);
  61451. }
  61452. cimg::mutex(7,0);
  61453. return s_path;
  61454. }
  61455. //! Get/set path to store temporary files.
  61456. /**
  61457. \param user_path Specified path, or \c 0 to get the path currently used.
  61458. \param reinit_path Force path to be recalculated (may take some time).
  61459. \return Path where temporary files can be saved.
  61460. **/
  61461. inline const char* temporary_path(const char *const user_path, const bool reinit_path) {
  61462. #define _cimg_test_temporary_path(p) \
  61463. if (!path_found) { \
  61464. cimg_snprintf(s_path,s_path._width,"%s",p); \
  61465. cimg_snprintf(tmp,tmp._width,"%s%c%s",s_path.data(),cimg_file_separator,filename_tmp._data); \
  61466. if ((file=cimg::std_fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } \
  61467. }
  61468. static CImg<char> s_path;
  61469. cimg::mutex(7);
  61470. if (reinit_path) s_path.assign();
  61471. if (user_path) {
  61472. if (!s_path) s_path.assign(1024);
  61473. std::strncpy(s_path,user_path,1023);
  61474. } else if (!s_path) {
  61475. s_path.assign(1024);
  61476. bool path_found = false;
  61477. CImg<char> tmp(1024), filename_tmp(256);
  61478. std::FILE *file = 0;
  61479. cimg_snprintf(filename_tmp,filename_tmp._width,"%s.tmp",cimg::filenamerand());
  61480. char *tmpPath = std::getenv("TMP");
  61481. if (!tmpPath) { tmpPath = std::getenv("TEMP"); winformat_string(tmpPath); }
  61482. if (tmpPath) _cimg_test_temporary_path(tmpPath);
  61483. #if cimg_OS==2
  61484. _cimg_test_temporary_path("C:\\WINNT\\Temp");
  61485. _cimg_test_temporary_path("C:\\WINDOWS\\Temp");
  61486. _cimg_test_temporary_path("C:\\Temp");
  61487. _cimg_test_temporary_path("C:");
  61488. _cimg_test_temporary_path("D:\\WINNT\\Temp");
  61489. _cimg_test_temporary_path("D:\\WINDOWS\\Temp");
  61490. _cimg_test_temporary_path("D:\\Temp");
  61491. _cimg_test_temporary_path("D:");
  61492. #else
  61493. _cimg_test_temporary_path("/tmp");
  61494. _cimg_test_temporary_path("/var/tmp");
  61495. #endif
  61496. if (!path_found) {
  61497. *s_path = 0;
  61498. std::strncpy(tmp,filename_tmp,tmp._width - 1);
  61499. if ((file=cimg::std_fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; }
  61500. }
  61501. if (!path_found) {
  61502. cimg::mutex(7,0);
  61503. throw CImgIOException("cimg::temporary_path(): Failed to locate path for writing temporary files.\n");
  61504. }
  61505. }
  61506. cimg::mutex(7,0);
  61507. return s_path;
  61508. }
  61509. //! Get/set path to the \c wget binary.
  61510. /**
  61511. \param user_path Specified path, or \c 0 to get the path currently used.
  61512. \param reinit_path Force path to be recalculated (may take some time).
  61513. \return Path containing the \c wget binary.
  61514. **/
  61515. inline const char *wget_path(const char *const user_path, const bool reinit_path) {
  61516. static CImg<char> s_path;
  61517. cimg::mutex(7);
  61518. if (reinit_path) s_path.assign();
  61519. if (user_path) {
  61520. if (!s_path) s_path.assign(1024);
  61521. std::strncpy(s_path,user_path,1023);
  61522. } else if (!s_path) {
  61523. s_path.assign(1024);
  61524. bool path_found = false;
  61525. std::FILE *file = 0;
  61526. #if cimg_OS==2
  61527. if (win_searchpath("wget.exe",s_path,s_path._width)) path_found = true;
  61528. if (!path_found) {
  61529. std::strcpy(s_path,".\\wget.exe");
  61530. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61531. }
  61532. if (!path_found) std::strcpy(s_path,"wget.exe");
  61533. #else
  61534. if (!path_found) {
  61535. std::strcpy(s_path,"./wget");
  61536. if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
  61537. }
  61538. if (!path_found) std::strcpy(s_path,"wget");
  61539. #endif
  61540. winformat_string(s_path);
  61541. }
  61542. cimg::mutex(7,0);
  61543. return s_path;
  61544. }
  61545. // [internal] Sorting function, used by cimg::files().
  61546. inline int _sort_files(const void* a, const void* b) {
  61547. const CImg<char> &sa = *(CImg<char>*)a, &sb = *(CImg<char>*)b;
  61548. return std::strcmp(sa._data,sb._data);
  61549. }
  61550. //! Generate a numbered version of a filename.
  61551. inline char* number_filename(const char *const filename, const int number,
  61552. const unsigned int digits, char *const str) {
  61553. if (!filename) { if (str) *str = 0; return 0; }
  61554. const unsigned int siz = (unsigned int)std::strlen(filename);
  61555. CImg<char> format(16), body(siz + 32);
  61556. const char *const ext = cimg::split_filename(filename,body);
  61557. if (*ext) cimg_snprintf(format,format._width,"%%s_%%.%ud.%%s",digits);
  61558. else cimg_snprintf(format,format._width,"%%s_%%.%ud",digits);
  61559. cimg_snprintf(str,1024,format._data,body._data,number,ext);
  61560. return str;
  61561. }
  61562. //! Return list of files/directories in specified directory.
  61563. /**
  61564. \param path Path to the directory. Set to 0 for current directory.
  61565. \param is_pattern Tell if specified path has a matching pattern in it.
  61566. \param mode Output type, can be primary { 0=files only | 1=folders only | 2=files + folders }.
  61567. \param include_path Tell if \c path must be included in resulting filenames.
  61568. \return A list of filenames.
  61569. **/
  61570. inline CImgList<char> files(const char *const path, const bool is_pattern=false,
  61571. const unsigned int mode=2, const bool include_path=false) {
  61572. if (!path || !*path) return files("*",true,mode,include_path);
  61573. CImgList<char> res;
  61574. // If path is a valid folder name, ignore argument 'is_pattern'.
  61575. const bool _is_pattern = is_pattern && !cimg::is_directory(path);
  61576. bool is_root = false, is_current = false;
  61577. cimg::unused(is_root,is_current);
  61578. // Clean format of input path.
  61579. CImg<char> pattern, _path = CImg<char>::string(path);
  61580. #if cimg_OS==2
  61581. for (char *ps = _path; *ps; ++ps) if (*ps=='\\') *ps='/';
  61582. #endif
  61583. char *pd = _path;
  61584. for (char *ps = pd; *ps; ++ps) { if (*ps!='/' || *ps!=*(ps+1)) *(pd++) = *ps; }
  61585. *pd = 0;
  61586. unsigned int lp = (unsigned int)std::strlen(_path);
  61587. if (!_is_pattern && lp && _path[lp - 1]=='/') {
  61588. _path[lp - 1] = 0; --lp;
  61589. #if cimg_OS!=2
  61590. is_root = !*_path;
  61591. #endif
  61592. }
  61593. // Separate folder path and matching pattern.
  61594. if (_is_pattern) {
  61595. const unsigned int bpos = (unsigned int)(cimg::basename(_path,'/') - _path.data());
  61596. CImg<char>::string(_path).move_to(pattern);
  61597. if (bpos) {
  61598. _path[bpos - 1] = 0; // End 'path' at last slash
  61599. #if cimg_OS!=2
  61600. is_root = !*_path;
  61601. #endif
  61602. } else { // No path to folder specified, assuming current folder
  61603. is_current = true; *_path = 0;
  61604. }
  61605. lp = (unsigned int)std::strlen(_path);
  61606. }
  61607. // Windows version.
  61608. #if cimg_OS==2
  61609. if (!_is_pattern) {
  61610. pattern.assign(lp + 3);
  61611. std::memcpy(pattern,_path,lp);
  61612. pattern[lp] = '/'; pattern[lp + 1] = '*'; pattern[lp + 2] = 0;
  61613. }
  61614. WIN32_FIND_DATAA file_data;
  61615. const HANDLE dir = FindFirstFileA(pattern.data(),&file_data);
  61616. if (dir==INVALID_HANDLE_VALUE) return CImgList<char>::const_empty();
  61617. do {
  61618. const char *const filename = file_data.cFileName;
  61619. if (*filename!='.' || (filename[1] && (filename[1]!='.' || filename[2]))) {
  61620. const unsigned int lf = (unsigned int)std::strlen(filename);
  61621. const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0;
  61622. if ((!mode && !is_directory) || (mode==1 && is_directory) || mode>=2) {
  61623. if (include_path) {
  61624. CImg<char> full_filename((lp?lp+1:0) + lf + 1);
  61625. if (lp) { std::memcpy(full_filename,_path,lp); full_filename[lp] = '/'; }
  61626. std::memcpy(full_filename._data + (lp?lp + 1:0),filename,lf + 1);
  61627. full_filename.move_to(res);
  61628. } else CImg<char>(filename,lf + 1).move_to(res);
  61629. }
  61630. }
  61631. } while (FindNextFileA(dir,&file_data));
  61632. FindClose(dir);
  61633. // Unix version (posix).
  61634. #elif cimg_OS == 1
  61635. DIR *const dir = opendir(is_root?"/":is_current?".":_path.data());
  61636. if (!dir) return CImgList<char>::const_empty();
  61637. struct dirent *ent;
  61638. while ((ent=readdir(dir))!=0) {
  61639. const char *const filename = ent->d_name;
  61640. if (*filename!='.' || (filename[1] && (filename[1]!='.' || filename[2]))) {
  61641. const unsigned int lf = (unsigned int)std::strlen(filename);
  61642. CImg<char> full_filename(lp + lf + 2);
  61643. if (!is_current) {
  61644. full_filename.assign(lp + lf + 2);
  61645. if (lp) std::memcpy(full_filename,_path,lp);
  61646. full_filename[lp] = '/';
  61647. std::memcpy(full_filename._data + lp + 1,filename,lf + 1);
  61648. } else full_filename.assign(filename,lf + 1);
  61649. struct stat st;
  61650. if (stat(full_filename,&st)==-1) continue;
  61651. const bool is_directory = (st.st_mode & S_IFDIR)!=0;
  61652. if ((!mode && !is_directory) || (mode==1 && is_directory) || mode==2) {
  61653. if (include_path) {
  61654. if (!_is_pattern || (_is_pattern && !fnmatch(pattern,full_filename,0)))
  61655. full_filename.move_to(res);
  61656. } else {
  61657. if (!_is_pattern || (_is_pattern && !fnmatch(pattern,full_filename,0)))
  61658. CImg<char>(filename,lf + 1).move_to(res);
  61659. }
  61660. }
  61661. }
  61662. }
  61663. closedir(dir);
  61664. #endif
  61665. // Sort resulting list by lexicographic order.
  61666. if (res._width>=2) std::qsort(res._data,res._width,sizeof(CImg<char>),_sort_files);
  61667. return res;
  61668. }
  61669. //! Try to guess format from an image file.
  61670. /**
  61671. \param file Input file (can be \c 0 if \c filename is set).
  61672. \param filename Filename, as a C-string (can be \c 0 if \c file is set).
  61673. \return C-string containing the guessed file format, or \c 0 if nothing has been guessed.
  61674. **/
  61675. inline const char *ftype(std::FILE *const file, const char *const filename) {
  61676. if (!file && !filename)
  61677. throw CImgArgumentException("cimg::ftype(): Specified filename is (null).");
  61678. static const char
  61679. *const _bmp = "bmp",
  61680. *const _cr2 = "cr2",
  61681. *const _dcm = "dcm",
  61682. *const _gif = "gif",
  61683. *const _inr = "inr",
  61684. *const _jpg = "jpg",
  61685. *const _off = "off",
  61686. *const _pan = "pan",
  61687. *const _pfm = "pfm",
  61688. *const _png = "png",
  61689. *const _pnm = "pnm",
  61690. *const _tif = "tif";
  61691. const char *f_type = 0;
  61692. CImg<char> header;
  61693. const unsigned int omode = cimg::exception_mode();
  61694. cimg::exception_mode(0);
  61695. try {
  61696. header._load_raw(file,filename,512,1,1,1,false,false,0);
  61697. const unsigned char *const uheader = (unsigned char*)header._data;
  61698. if (!std::strncmp(header,"OFF\n",4)) f_type = _off; // OFF
  61699. else if (!std::strncmp(header,"#INRIMAGE",9)) // INRIMAGE
  61700. f_type = _inr;
  61701. else if (!std::strncmp(header,"PANDORE",7)) // PANDORE
  61702. f_type = _pan;
  61703. else if (!std::strncmp(header.data() + 128,"DICM",4)) // DICOM
  61704. f_type = _dcm;
  61705. else if (uheader[0]==0xFF && uheader[1]==0xD8 && uheader[2]==0xFF) // JPEG
  61706. f_type = _jpg;
  61707. else if (header[0]=='B' && header[1]=='M') // BMP
  61708. f_type = _bmp;
  61709. else if (header[0]=='G' && header[1]=='I' && header[2]=='F' && header[3]=='8' && header[5]=='a' &&
  61710. (header[4]=='7' || header[4]=='9')) // GIF
  61711. f_type = _gif;
  61712. else if (uheader[0]==0x89 && uheader[1]==0x50 && uheader[2]==0x4E && uheader[3]==0x47 &&
  61713. uheader[4]==0x0D && uheader[5]==0x0A && uheader[6]==0x1A && uheader[7]==0x0A) // PNG
  61714. f_type = _png;
  61715. else if (uheader[0]==0x49 && uheader[1]==0x49 && uheader[2]==0x2A && uheader[3]==0x00 && // CR2
  61716. uheader[4]==0x10 && uheader[5]==0x00 && uheader[6]==0x00 && uheader[7]==0x00 &&
  61717. uheader[8]==0x43 && uheader[9]==0x52)
  61718. f_type = _cr2;
  61719. else if ((uheader[0]==0x49 && uheader[1]==0x49 && uheader[2]==0x2A && uheader[3]==0x00) ||
  61720. (uheader[0]==0x4D && uheader[1]==0x4D && uheader[2]==0x00 && uheader[3]==0x2A)) // TIFF
  61721. f_type = _tif;
  61722. else { // PNM or PFM
  61723. CImgList<char> _header = header.get_split(CImg<char>::vector('\n'),0,false);
  61724. cimglist_for(_header,l) {
  61725. if (_header(l,0)=='#') continue;
  61726. if (_header[l]._width==2 && _header(l,0)=='P') {
  61727. const char c = _header(l,1);
  61728. if (c=='f' || c=='F') { f_type = _pfm; break; }
  61729. if (c>='1' && c<='9') { f_type = _pnm; break; }
  61730. }
  61731. f_type = 0; break;
  61732. }
  61733. }
  61734. } catch (CImgIOException&) { }
  61735. cimg::exception_mode(omode);
  61736. return f_type;
  61737. }
  61738. //! Load file from network as a local temporary file.
  61739. /**
  61740. \param url URL of the filename, as a C-string.
  61741. \param[out] filename_local C-string containing the path to a local copy of \c filename.
  61742. \param timeout Maximum time (in seconds) authorized for downloading the file from the URL.
  61743. \param try_fallback When using libcurl, tells using system calls as fallbacks in case of libcurl failure.
  61744. \param referer Referer used, as a C-string.
  61745. \return Value of \c filename_local.
  61746. \note Use the \c libcurl library, or the external binaries \c wget or \c curl to perform the download.
  61747. **/
  61748. inline char *load_network(const char *const url, char *const filename_local,
  61749. const unsigned int timeout, const bool try_fallback,
  61750. const char *const referer) {
  61751. if (!url)
  61752. throw CImgArgumentException("cimg::load_network(): Specified URL is (null).");
  61753. if (!filename_local)
  61754. throw CImgArgumentException("cimg::load_network(): Specified destination string is (null).");
  61755. if (!network_mode())
  61756. throw CImgIOException("cimg::load_network(): Loading files from network is disabled.");
  61757. const char *const __ext = cimg::split_filename(url), *const _ext = (*__ext && __ext>url)?__ext - 1:__ext;
  61758. CImg<char> ext = CImg<char>::string(_ext);
  61759. std::FILE *file = 0;
  61760. *filename_local = 0;
  61761. if (ext._width>16 || !cimg::strncasecmp(ext,"cgi",3)) *ext = 0;
  61762. else cimg::strwindows_reserved(ext);
  61763. do {
  61764. cimg_snprintf(filename_local,256,"%s%c%s%s",
  61765. cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext._data);
  61766. if ((file=cimg::std_fopen(filename_local,"rb"))!=0) cimg::fclose(file);
  61767. } while (file);
  61768. #ifdef cimg_use_curl
  61769. const unsigned int omode = cimg::exception_mode();
  61770. cimg::exception_mode(0);
  61771. try {
  61772. CURL *curl = 0;
  61773. CURLcode res;
  61774. curl = curl_easy_init();
  61775. if (curl) {
  61776. file = cimg::fopen(filename_local,"wb");
  61777. curl_easy_setopt(curl,CURLOPT_URL,url);
  61778. curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,0);
  61779. curl_easy_setopt(curl,CURLOPT_WRITEDATA,file);
  61780. curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);
  61781. curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
  61782. curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
  61783. if (timeout) curl_easy_setopt(curl,CURLOPT_TIMEOUT,(long)timeout);
  61784. if (std::strchr(url,'?')) curl_easy_setopt(curl,CURLOPT_HTTPGET,1L);
  61785. if (referer) curl_easy_setopt(curl,CURLOPT_REFERER,referer);
  61786. res = curl_easy_perform(curl);
  61787. curl_easy_cleanup(curl);
  61788. cimg::fseek(file,0,SEEK_END); // Check if file size is 0
  61789. const cimg_ulong siz = cimg::ftell(file);
  61790. cimg::fclose(file);
  61791. if (siz>0 && res==CURLE_OK) {
  61792. cimg::exception_mode(omode);
  61793. return filename_local;
  61794. } else std::remove(filename_local);
  61795. }
  61796. } catch (...) { }
  61797. cimg::exception_mode(omode);
  61798. if (!try_fallback) throw CImgIOException("cimg::load_network(): Failed to load file '%s' with libcurl.",url);
  61799. #endif
  61800. CImg<char> command((unsigned int)std::strlen(url) + 64);
  61801. cimg::unused(try_fallback);
  61802. // Try with 'curl' first.
  61803. if (timeout) {
  61804. if (referer)
  61805. cimg_snprintf(command,command._width,"\"%s\" -e %s -m %u -f --silent --compressed -o \"%s\" \"%s\"",
  61806. cimg::curl_path(),referer,timeout,filename_local,
  61807. CImg<char>::string(url)._system_strescape().data());
  61808. else
  61809. cimg_snprintf(command,command._width,"\"%s\" -m %u -f --silent --compressed -o \"%s\" \"%s\"",
  61810. cimg::curl_path(),timeout,filename_local,
  61811. CImg<char>::string(url)._system_strescape().data());
  61812. } else {
  61813. if (referer)
  61814. cimg_snprintf(command,command._width,"\"%s\" -e %s -f --silent --compressed -o \"%s\" \"%s\"",
  61815. cimg::curl_path(),referer,filename_local,
  61816. CImg<char>::string(url)._system_strescape().data());
  61817. else
  61818. cimg_snprintf(command,command._width,"\"%s\" -f --silent --compressed -o \"%s\" \"%s\"",
  61819. cimg::curl_path(),filename_local,
  61820. CImg<char>::string(url)._system_strescape().data());
  61821. }
  61822. cimg::system(command, cimg::curl_path());
  61823. if (!(file=cimg::std_fopen(filename_local,"rb"))) {
  61824. // Try with 'wget' otherwise.
  61825. if (timeout) {
  61826. if (referer)
  61827. cimg_snprintf(command,command._width,"\"%s\" --referer=%s -T %u -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
  61828. cimg::wget_path(),referer,timeout,filename_local,
  61829. CImg<char>::string(url)._system_strescape().data());
  61830. else
  61831. cimg_snprintf(command,command._width,"\"%s\" -T %u -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
  61832. cimg::wget_path(),timeout,filename_local,
  61833. CImg<char>::string(url)._system_strescape().data());
  61834. } else {
  61835. if (referer)
  61836. cimg_snprintf(command,command._width,"\"%s\" --referer=%s -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
  61837. cimg::wget_path(),referer,filename_local,
  61838. CImg<char>::string(url)._system_strescape().data());
  61839. else
  61840. cimg_snprintf(command,command._width,"\"%s\" -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
  61841. cimg::wget_path(),filename_local,
  61842. CImg<char>::string(url)._system_strescape().data());
  61843. }
  61844. cimg::system(command, cimg::wget_path());
  61845. if (!(file=cimg::std_fopen(filename_local,"rb")))
  61846. throw CImgIOException("cimg::load_network(): Failed to load file '%s' with external commands "
  61847. "'wget' or 'curl'.",url);
  61848. cimg::fclose(file);
  61849. // Try gunzip it.
  61850. cimg_snprintf(command,command._width,"%s.gz",filename_local);
  61851. std::rename(filename_local,command);
  61852. cimg_snprintf(command,command._width,"\"%s\" --quiet \"%s.gz\"",
  61853. gunzip_path(),filename_local);
  61854. cimg::system(command, gunzip_path());
  61855. file = cimg::std_fopen(filename_local,"rb");
  61856. if (!file) {
  61857. cimg_snprintf(command,command._width,"%s.gz",filename_local);
  61858. std::rename(command,filename_local);
  61859. file = cimg::std_fopen(filename_local,"rb");
  61860. }
  61861. }
  61862. cimg::fseek(file,0,SEEK_END); // Check if file size is 0
  61863. if (std::ftell(file)<=0)
  61864. throw CImgIOException("cimg::load_network(): Failed to load URL '%s' with external commands "
  61865. "'wget' or 'curl'.",url);
  61866. cimg::fclose(file);
  61867. return filename_local;
  61868. }
  61869. // Implement a tic/toc mechanism to display elapsed time of algorithms.
  61870. inline cimg_uint64 tictoc(const bool is_tic) {
  61871. cimg::mutex(2);
  61872. static CImg<cimg_uint64> times(64);
  61873. static unsigned int pos = 0;
  61874. const cimg_uint64 t1 = cimg::time();
  61875. if (is_tic) {
  61876. // Tic
  61877. times[pos++] = t1;
  61878. if (pos>=times._width)
  61879. throw CImgArgumentException("cimg::tic(): Too much calls to 'cimg::tic()' without calls to 'cimg::toc()'.");
  61880. cimg::mutex(2,0);
  61881. return t1;
  61882. }
  61883. // Toc
  61884. if (!pos)
  61885. throw CImgArgumentException("cimg::toc(): No previous call to 'cimg::tic()' has been made.");
  61886. const cimg_uint64
  61887. t0 = times[--pos],
  61888. dt = t1>=t0?(t1 - t0):cimg::type<cimg_uint64>::max();
  61889. const unsigned int
  61890. edays = (unsigned int)(dt/86400000.),
  61891. ehours = (unsigned int)((dt - edays*86400000.)/3600000.),
  61892. emin = (unsigned int)((dt - edays*86400000. - ehours*3600000.)/60000.),
  61893. esec = (unsigned int)((dt - edays*86400000. - ehours*3600000. - emin*60000.)/1000.),
  61894. ems = (unsigned int)(dt - edays*86400000. - ehours*3600000. - emin*60000. - esec*1000.);
  61895. if (!edays && !ehours && !emin && !esec)
  61896. std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u ms%s\n",
  61897. cimg::t_red,1 + 2*pos,"",ems,cimg::t_normal);
  61898. else {
  61899. if (!edays && !ehours && !emin)
  61900. std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u sec %u ms%s\n",
  61901. cimg::t_red,1 + 2*pos,"",esec,ems,cimg::t_normal);
  61902. else {
  61903. if (!edays && !ehours)
  61904. std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u min %u sec %u ms%s\n",
  61905. cimg::t_red,1 + 2*pos,"",emin,esec,ems,cimg::t_normal);
  61906. else{
  61907. if (!edays)
  61908. std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u hours %u min %u sec %u ms%s\n",
  61909. cimg::t_red,1 + 2*pos,"",ehours,emin,esec,ems,cimg::t_normal);
  61910. else{
  61911. std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u days %u hours %u min %u sec %u ms%s\n",
  61912. cimg::t_red,1 + 2*pos,"",edays,ehours,emin,esec,ems,cimg::t_normal);
  61913. }
  61914. }
  61915. }
  61916. }
  61917. cimg::mutex(2,0);
  61918. return dt;
  61919. }
  61920. // Return a temporary string describing the size of a memory buffer.
  61921. inline const char *strbuffersize(const cimg_ulong size) {
  61922. static CImg<char> res(256);
  61923. cimg::mutex(5);
  61924. if (size<1024LU) cimg_snprintf(res,res._width,"%lu byte%s",(unsigned long)size,size>1?"s":"");
  61925. else if (size<1024*1024LU) { const float nsize = size/1024.f; cimg_snprintf(res,res._width,"%.1f Kio",nsize); }
  61926. else if (size<1024*1024*1024LU) {
  61927. const float nsize = size/(1024*1024.f); cimg_snprintf(res,res._width,"%.1f Mio",nsize);
  61928. } else { const float nsize = size/(1024*1024*1024.f); cimg_snprintf(res,res._width,"%.1f Gio",nsize); }
  61929. cimg::mutex(5,0);
  61930. return res;
  61931. }
  61932. //! Display a simple dialog box, and wait for the user's response.
  61933. /**
  61934. \param title Title of the dialog window.
  61935. \param msg Main message displayed inside the dialog window.
  61936. \param button1_label Label of the 1st button.
  61937. \param button2_label Label of the 2nd button (\c 0 to hide button).
  61938. \param button3_label Label of the 3rd button (\c 0 to hide button).
  61939. \param button4_label Label of the 4th button (\c 0 to hide button).
  61940. \param button5_label Label of the 5th button (\c 0 to hide button).
  61941. \param button6_label Label of the 6th button (\c 0 to hide button).
  61942. \param logo Image logo displayed at the left of the main message.
  61943. \param is_centered Tells if the dialog window must be centered on the screen.
  61944. \return Index of clicked button (from \c 0 to \c 5), or \c -1 if the dialog window has been closed by the user.
  61945. \note
  61946. - Up to 6 buttons can be defined in the dialog window.
  61947. - The function returns when a user clicked one of the button or closed the dialog window.
  61948. - If a button text is set to 0, the corresponding button (and the following) will not appear in the dialog box.
  61949. At least one button must be specified.
  61950. **/
  61951. template<typename t>
  61952. inline int dialog(const char *const title, const char *const msg,
  61953. const char *const button1_label, const char *const button2_label,
  61954. const char *const button3_label, const char *const button4_label,
  61955. const char *const button5_label, const char *const button6_label,
  61956. const CImg<t>& logo, const bool is_centered=false) {
  61957. #if cimg_display==0
  61958. cimg::unused(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label,
  61959. logo._data,is_centered);
  61960. throw CImgIOException("cimg::dialog(): No display available.");
  61961. #else
  61962. static const unsigned char
  61963. black[] = { 0,0,0 }, white[] = { 255,255,255 }, gray[] = { 200,200,200 }, gray2[] = { 150,150,150 };
  61964. // Create buttons and canvas graphics
  61965. CImgList<unsigned char> buttons, cbuttons, sbuttons;
  61966. if (button1_label) {
  61967. CImg<unsigned char>().draw_text(0,0,button1_label,black,gray,1,13).move_to(buttons);
  61968. if (button2_label) {
  61969. CImg<unsigned char>().draw_text(0,0,button2_label,black,gray,1,13).move_to(buttons);
  61970. if (button3_label) {
  61971. CImg<unsigned char>().draw_text(0,0,button3_label,black,gray,1,13).move_to(buttons);
  61972. if (button4_label) {
  61973. CImg<unsigned char>().draw_text(0,0,button4_label,black,gray,1,13).move_to(buttons);
  61974. if (button5_label) {
  61975. CImg<unsigned char>().draw_text(0,0,button5_label,black,gray,1,13).move_to(buttons);
  61976. if (button6_label) {
  61977. CImg<unsigned char>().draw_text(0,0,button6_label,black,gray,1,13).move_to(buttons);
  61978. }}}}}}
  61979. if (!buttons._width)
  61980. throw CImgArgumentException("cimg::dialog(): No buttons have been defined.");
  61981. cimglist_for(buttons,l) buttons[l].resize(-100,-100,1,3);
  61982. unsigned int bw = 0, bh = 0;
  61983. cimglist_for(buttons,l) { bw = std::max(bw,buttons[l]._width); bh = std::max(bh,buttons[l]._height); }
  61984. bw+=8; bh+=8;
  61985. if (bw<64) bw = 64;
  61986. if (bw>128) bw = 128;
  61987. if (bh<24) bh = 24;
  61988. if (bh>48) bh = 48;
  61989. CImg<unsigned char> button(bw,bh,1,3);
  61990. button.draw_rectangle(0,0,bw - 1,bh - 1,gray);
  61991. button.draw_line(0,0,bw - 1,0,white).draw_line(0,bh - 1,0,0,white);
  61992. button.draw_line(bw - 1,0,bw - 1,bh - 1,black).draw_line(bw - 1,bh - 1,0,bh - 1,black);
  61993. button.draw_line(1,bh - 2,bw - 2,bh - 2,gray2).draw_line(bw - 2,bh - 2,bw - 2,1,gray2);
  61994. CImg<unsigned char> sbutton(bw,bh,1,3);
  61995. sbutton.draw_rectangle(0,0,bw - 1,bh - 1,gray);
  61996. sbutton.draw_line(0,0,bw - 1,0,black).draw_line(bw - 1,0,bw - 1,bh - 1,black);
  61997. sbutton.draw_line(bw - 1,bh - 1,0,bh - 1,black).draw_line(0,bh - 1,0,0,black);
  61998. sbutton.draw_line(1,1,bw - 2,1,white).draw_line(1,bh - 2,1,1,white);
  61999. sbutton.draw_line(bw - 2,1,bw - 2,bh - 2,black).draw_line(bw - 2,bh - 2,1,bh - 2,black);
  62000. sbutton.draw_line(2,bh - 3,bw - 3,bh - 3,gray2).draw_line(bw - 3,bh - 3,bw - 3,2,gray2);
  62001. sbutton.draw_line(4,4,bw - 5,4,black,1,0xAAAAAAAA,true).
  62002. draw_line(bw - 5,4,bw - 5,bh - 5,black,1,0xAAAAAAAA,false);
  62003. sbutton.draw_line(bw - 5,bh - 5,4,bh - 5,black,1,0xAAAAAAAA,false).
  62004. draw_line(4,bh - 5,4,4,black,1,0xAAAAAAAA,false);
  62005. CImg<unsigned char> cbutton(bw,bh,1,3);
  62006. cbutton.draw_rectangle(0,0,bw - 1,bh - 1,black).draw_rectangle(1,1,bw - 2,bh - 2,gray2).
  62007. draw_rectangle(2,2,bw - 3,bh - 3,gray);
  62008. cbutton.draw_line(4,4,bw - 5,4,black,1,0xAAAAAAAA,true).
  62009. draw_line(bw - 5,4,bw - 5,bh - 5,black,1,0xAAAAAAAA,false);
  62010. cbutton.draw_line(bw - 5,bh - 5,4,bh - 5,black,1,0xAAAAAAAA,false).
  62011. draw_line(4,bh - 5,4,4,black,1,0xAAAAAAAA,false);
  62012. cimglist_for(buttons,ll) {
  62013. CImg<unsigned char>(cbutton).
  62014. draw_image(1 + (bw -buttons[ll].width())/2,1 + (bh - buttons[ll].height())/2,buttons[ll]).
  62015. move_to(cbuttons);
  62016. CImg<unsigned char>(sbutton).
  62017. draw_image((bw - buttons[ll].width())/2,(bh - buttons[ll].height())/2,buttons[ll]).
  62018. move_to(sbuttons);
  62019. CImg<unsigned char>(button).
  62020. draw_image((bw - buttons[ll].width())/2,(bh - buttons[ll].height())/2,buttons[ll]).
  62021. move_to(buttons[ll]);
  62022. }
  62023. CImg<unsigned char> canvas;
  62024. if (msg)
  62025. ((CImg<unsigned char>().draw_text(0,0,"%s",gray,0,1,13,msg)*=-1)+=200).resize(-100,-100,1,3).move_to(canvas);
  62026. const unsigned int
  62027. bwall = (buttons._width - 1)*(12 + bw) + bw,
  62028. w = cimg::max(196U,36 + logo._width + canvas._width,24 + bwall),
  62029. h = cimg::max(96U,36 + canvas._height + bh,36 + logo._height + bh),
  62030. lx = 12 + (canvas._data?0:((w - 24 - logo._width)/2)),
  62031. ly = (h - 12 - bh - logo._height)/2,
  62032. tx = lx + logo._width + 12,
  62033. ty = (h - 12 - bh - canvas._height)/2,
  62034. bx = (w - bwall)/2,
  62035. by = h - 12 - bh;
  62036. if (canvas._data)
  62037. canvas = CImg<unsigned char>(w,h,1,3).
  62038. draw_rectangle(0,0,w - 1,h - 1,gray).
  62039. draw_line(0,0,w - 1,0,white).draw_line(0,h - 1,0,0,white).
  62040. draw_line(w - 1,0,w - 1,h - 1,black).draw_line(w - 1,h - 1,0,h - 1,black).
  62041. draw_image(tx,ty,canvas);
  62042. else
  62043. canvas = CImg<unsigned char>(w,h,1,3).
  62044. draw_rectangle(0,0,w - 1,h - 1,gray).
  62045. draw_line(0,0,w - 1,0,white).draw_line(0,h - 1,0,0,white).
  62046. draw_line(w - 1,0,w - 1,h - 1,black).draw_line(w - 1,h - 1,0,h - 1,black);
  62047. if (logo._data) canvas.draw_image(lx,ly,logo);
  62048. unsigned int xbuttons[6] = { 0 };
  62049. cimglist_for(buttons,lll) {
  62050. xbuttons[lll] = bx + (bw + 12)*lll;
  62051. canvas.draw_image(xbuttons[lll],by,buttons[lll]);
  62052. }
  62053. // Open window and enter events loop
  62054. CImgDisplay disp(canvas,title?title:" ",0,false,is_centered?true:false);
  62055. if (is_centered) disp.move((CImgDisplay::screen_width() - disp.width())/2,
  62056. (CImgDisplay::screen_height() - disp.height())/2);
  62057. bool stop_flag = false, refresh = false;
  62058. int oselected = -1, oclicked = -1, selected = -1, clicked = -1;
  62059. while (!disp.is_closed() && !stop_flag) {
  62060. if (refresh) {
  62061. if (clicked>=0)
  62062. CImg<unsigned char>(canvas).draw_image(xbuttons[clicked],by,cbuttons[clicked]).display(disp);
  62063. else {
  62064. if (selected>=0)
  62065. CImg<unsigned char>(canvas).draw_image(xbuttons[selected],by,sbuttons[selected]).display(disp);
  62066. else canvas.display(disp);
  62067. }
  62068. refresh = false;
  62069. }
  62070. disp.wait(15);
  62071. if (disp.is_resized()) disp.resize(disp,false);
  62072. if (disp.button()&1) {
  62073. oclicked = clicked;
  62074. clicked = -1;
  62075. cimglist_for(buttons,l)
  62076. if (disp.mouse_y()>=(int)by && disp.mouse_y()<(int)(by + bh) &&
  62077. disp.mouse_x()>=(int)xbuttons[l] && disp.mouse_x()<(int)(xbuttons[l] + bw)) {
  62078. clicked = selected = l;
  62079. refresh = true;
  62080. }
  62081. if (clicked!=oclicked) refresh = true;
  62082. } else if (clicked>=0) stop_flag = true;
  62083. if (disp.key()) {
  62084. oselected = selected;
  62085. switch (disp.key()) {
  62086. case cimg::keyESC : selected = -1; stop_flag = true; break;
  62087. case cimg::keyENTER : if (selected<0) selected = 0; stop_flag = true; break;
  62088. case cimg::keyTAB :
  62089. case cimg::keyARROWRIGHT :
  62090. case cimg::keyARROWDOWN : selected = (selected + 1)%buttons.width(); break;
  62091. case cimg::keyARROWLEFT :
  62092. case cimg::keyARROWUP : selected = (selected + buttons.width() - 1)%buttons.width(); break;
  62093. }
  62094. disp.set_key();
  62095. if (selected!=oselected) refresh = true;
  62096. }
  62097. }
  62098. if (!disp) selected = -1;
  62099. return selected;
  62100. #endif
  62101. }
  62102. //! Display a simple dialog box, and wait for the user's response \specialization.
  62103. inline int dialog(const char *const title, const char *const msg,
  62104. const char *const button1_label, const char *const button2_label,
  62105. const char *const button3_label, const char *const button4_label,
  62106. const char *const button5_label, const char *const button6_label,
  62107. const bool is_centered) {
  62108. return dialog(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label,
  62109. CImg<unsigned char>::_logo40x38(),is_centered);
  62110. }
  62111. //! Evaluate math expression.
  62112. /**
  62113. \param expression C-string describing the formula to evaluate.
  62114. \param x Value of the pre-defined variable \c x.
  62115. \param y Value of the pre-defined variable \c y.
  62116. \param z Value of the pre-defined variable \c z.
  62117. \param c Value of the pre-defined variable \c c.
  62118. \return Result of the formula evaluation.
  62119. \note Set \c expression to \c 0 to keep evaluating the last specified \c expression.
  62120. \par Example
  62121. \code
  62122. const double
  62123. res1 = cimg::eval("cos(x)^2 + sin(y)^2",2,2), // will return '1'
  62124. res2 = cimg::eval(0,1,1); // will return '1' too
  62125. \endcode
  62126. **/
  62127. inline double eval(const char *const expression, const double x, const double y, const double z, const double c) {
  62128. static const CImg<float> empty;
  62129. return empty.eval(expression,x,y,z,c);
  62130. }
  62131. template<typename t>
  62132. inline CImg<typename cimg::superset<double,t>::type> eval(const char *const expression, const CImg<t>& xyzc) {
  62133. static const CImg<float> empty;
  62134. return empty.eval(expression,xyzc);
  62135. }
  62136. } // namespace cimg { ...
  62137. } // namespace cimg_library { ...
  62138. //! Short alias name.
  62139. namespace cil = cimg_library_suffixed;
  62140. #ifdef _cimg_redefine_False
  62141. #define False 0
  62142. #endif
  62143. #ifdef _cimg_redefine_True
  62144. #define True 1
  62145. #endif
  62146. #ifdef _cimg_redefine_Status
  62147. #define Status int
  62148. #endif
  62149. #ifdef _cimg_redefine_Success
  62150. #define Success 0
  62151. #endif
  62152. #ifdef _cimg_redefine_min
  62153. #define min(a,b) (((a)<(b))?(a):(b))
  62154. #endif
  62155. #ifdef _cimg_redefine_max
  62156. #define max(a,b) (((a)>(b))?(a):(b))
  62157. #endif
  62158. #ifdef _cimg_redefine_PI
  62159. #define PI 3.141592653589793238462643383
  62160. #endif
  62161. #ifdef _MSC_VER
  62162. #pragma warning(pop)
  62163. #endif
  62164. #endif
  62165. // Local Variables:
  62166. // mode: c++
  62167. // End: