12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061 |
- /*
- #
- # File : CImg.h
- # ( C++ header file )
- #
- # Description : C++ Template Image Processing Toolkit.
- # This file is the main component of the CImg Library project.
- # ( http://cimg.eu )
- #
- # Project manager : David Tschumperlé
- # ( http://tschumperle.users.greyc.fr/ )
- #
- # A complete list of contributors is available in file 'README.txt'
- # distributed within the CImg package.
- #
- # Licenses : This file is 'dual-licensed', you have to choose one
- # of the two licenses below to apply.
- #
- # CeCILL-C
- # The CeCILL-C license is close to the GNU LGPL.
- # ( http://cecill.info/licences/Licence_CeCILL-C_V1-en.html )
- #
- # or CeCILL v2.1
- # The CeCILL license is compatible with the GNU GPL.
- # ( http://cecill.info/licences/Licence_CeCILL_V2.1-en.html )
- #
- # This software is governed either by the CeCILL or the CeCILL-C license
- # under French law and abiding by the rules of distribution of free software.
- # You can use, modify and or redistribute the software under the terms of
- # the CeCILL or CeCILL-C licenses as circulated by CEA, CNRS and INRIA
- # at the following URL: "http://cecill.info".
- #
- # As a counterpart to the access to the source code and rights to copy,
- # modify and redistribute granted by the license, users are provided only
- # with a limited warranty and the software's author, the holder of the
- # economic rights, and the successive licensors have only limited
- # liability.
- #
- # In this respect, the user's attention is drawn to the risks associated
- # with loading, using, modifying and/or developing or reproducing the
- # software by the user in light of its specific status of free software,
- # that may mean that it is complicated to manipulate, and that also
- # therefore means that it is reserved for developers and experienced
- # professionals having in-depth computer knowledge. Users are therefore
- # encouraged to load and test the software's suitability as regards their
- # requirements in conditions enabling the security of their systems and/or
- # data to be ensured and, more generally, to use and operate it in the
- # same conditions as regards security.
- #
- # The fact that you are presently reading this means that you have had
- # knowledge of the CeCILL and CeCILL-C licenses and that you accept its terms.
- #
- */
- // Set version number of the library.
- #ifndef cimg_version
- #define cimg_version 303
- /*-----------------------------------------------------------
- #
- # Test and possibly auto-set CImg configuration variables
- # and include required headers.
- #
- # If you find that the default configuration variables are
- # not adapted to your system, you can override their values
- # before including the header file "CImg.h"
- # (use the #define directive).
- #
- ------------------------------------------------------------*/
- // Include standard C++ headers.
- // This is the minimal set of required headers to make CImg-based codes compile.
- #include <cstdio>
- #include <cstdlib>
- #include <cstdarg>
- #include <cstring>
- #include <cmath>
- #include <cfloat>
- #include <climits>
- #include <ctime>
- #include <exception>
- #include <algorithm>
- #define cimg_str(x) #x
- #define cimg_str2(x) cimg_str(x)
- // Detect/configure OS variables.
- //
- // Define 'cimg_OS' to: '0' for an unknown OS (will try to minize library dependencies).
- // '1' for a Unix-like OS (Linux, Solaris, BSD, MacOSX, Irix, ...).
- // '2' for Microsoft Windows.
- // (auto-detection is performed if 'cimg_OS' is not set by the user).
- #ifndef cimg_OS
- #if defined(unix) || defined(__unix) || defined(__unix__) \
- || defined(linux) || defined(__linux) || defined(__linux__) \
- || defined(sun) || defined(__sun) \
- || defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) \
- || defined(__FreeBSD__) || defined (__DragonFly__) \
- || defined(sgi) || defined(__sgi) \
- || defined(__OSX__) || defined(__MACOSX__) || defined(__APPLE__) \
- || defined(__CYGWIN__)
- #define cimg_OS 1
- #elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \
- || defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
- #define cimg_OS 2
- #else
- #define cimg_OS 0
- #endif
- #elif !(cimg_OS==0 || cimg_OS==1 || cimg_OS==2)
- #error CImg Library: Invalid configuration variable 'cimg_OS'.
- #error (correct values are '0 = unknown OS', '1 = Unix-like OS', '2 = Microsoft Windows').
- #endif
- #ifndef cimg_date
- #define cimg_date __DATE__
- #endif
- #ifndef cimg_time
- #define cimg_time __TIME__
- #endif
- // Disable silly warnings on some Microsoft VC++ compilers.
- #ifdef _MSC_VER
- #pragma warning(push)
- #pragma warning(disable:4127)
- #pragma warning(disable:4244)
- #pragma warning(disable:4311)
- #pragma warning(disable:4312)
- #pragma warning(disable:4319)
- #pragma warning(disable:4512)
- #pragma warning(disable:4571)
- #pragma warning(disable:4640)
- #pragma warning(disable:4706)
- #pragma warning(disable:4710)
- #pragma warning(disable:4800)
- #pragma warning(disable:4804)
- #pragma warning(disable:4820)
- #pragma warning(disable:4996)
- #ifndef _CRT_SECURE_NO_DEPRECATE
- #define _CRT_SECURE_NO_DEPRECATE 1
- #endif
- #ifndef _CRT_SECURE_NO_WARNINGS
- #define _CRT_SECURE_NO_WARNINGS 1
- #endif
- #ifndef _CRT_NONSTDC_NO_DEPRECATE
- #define _CRT_NONSTDC_NO_DEPRECATE 1
- #endif
- #endif
- // Define correct string functions for each compiler and OS.
- #if cimg_OS==2 && defined(_MSC_VER)
- #define cimg_sscanf std::sscanf
- #define cimg_sprintf std::sprintf
- #define cimg_snprintf cimg::_snprintf
- #define cimg_vsnprintf cimg::_vsnprintf
- #else
- #include <stdio.h>
- #if defined(__MACOSX__) || defined(__APPLE__)
- #define cimg_sscanf cimg::_sscanf
- #define cimg_sprintf cimg::_sprintf
- #define cimg_snprintf cimg::_snprintf
- #define cimg_vsnprintf cimg::_vsnprintf
- #else
- #define cimg_sscanf std::sscanf
- #define cimg_sprintf std::sprintf
- #define cimg_snprintf snprintf
- #define cimg_vsnprintf vsnprintf
- #endif
- #endif
- // Include OS-specific headers.
- #if cimg_OS==1
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <fnmatch.h>
- #elif cimg_OS==2
- #ifndef NOMINMAX
- #define NOMINMAX
- #endif
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <windows.h>
- #ifndef _WIN32_IE
- #define _WIN32_IE 0x0400
- #endif
- #include <shlobj.h>
- #include <process.h>
- #include <io.h>
- enum {FALSE_WIN = 0};
- #endif
- // Look for C++11 features.
- #ifndef cimg_use_cpp11
- #if __cplusplus>201100
- #define cimg_use_cpp11 1
- #else
- #define cimg_use_cpp11 0
- #endif
- #endif
- #if cimg_use_cpp11==1
- #include <initializer_list>
- #include <utility>
- #endif
- // Convenient macro to define pragma
- #ifdef _MSC_VER
- #define cimg_pragma(x) __pragma(x)
- #else
- #define cimg_pragma(x) _Pragma(#x)
- #endif
- // Define own types 'cimg_long/ulong' and 'cimg_int64/uint64' to ensure portability.
- // ( constrained to 'sizeof(cimg_ulong/cimg_long) = sizeof(void*)' and 'sizeof(cimg_int64/cimg_uint64)=8' ).
- #if cimg_OS==2
- #define cimg_uint64 unsigned __int64
- #define cimg_int64 __int64
- #define cimg_ulong UINT_PTR
- #define cimg_long INT_PTR
- #ifdef _MSC_VER
- #define cimg_fuint64 "%I64u"
- #define cimg_fint64 "%I64d"
- #else
- #define cimg_fuint64 "%llu"
- #define cimg_fint64 "%lld"
- #endif
- #else
- #if UINTPTR_MAX==0xffffffff || defined(__arm__) || defined(_M_ARM) || ((ULONG_MAX)==(UINT_MAX))
- #define cimg_uint64 unsigned long long
- #define cimg_int64 long long
- #define cimg_fuint64 "%llu"
- #define cimg_fint64 "%lld"
- #else
- #define cimg_uint64 unsigned long
- #define cimg_int64 long
- #define cimg_fuint64 "%lu"
- #define cimg_fint64 "%ld"
- #endif
- #if defined(__arm__) || defined(_M_ARM)
- #define cimg_ulong unsigned long long
- #define cimg_long long long
- #else
- #define cimg_ulong unsigned long
- #define cimg_long long
- #endif
- #endif
- // Configure filename separator.
- //
- // Filename separator is set by default to '/', except for Windows where it is '\'.
- #ifndef cimg_file_separator
- #if cimg_OS==2
- #define cimg_file_separator '\\'
- #else
- #define cimg_file_separator '/'
- #endif
- #endif
- // Configure verbosity of output messages.
- //
- // Define 'cimg_verbosity' to: '0' to hide library messages (quiet mode).
- // '1' to output library messages on the console.
- // '2' to output library messages on a basic dialog window (default behavior).
- // '3' to do as '1' + add extra warnings (may slow down the code!).
- // '4' to do as '2' + add extra warnings (may slow down the code!).
- //
- // Define 'cimg_strict_warnings' to replace warning messages by exception throwns.
- //
- // Define 'cimg_use_vt100' to allow output of color messages on VT100-compatible terminals.
- #ifndef cimg_verbosity
- #if cimg_OS==2
- #define cimg_verbosity 2
- #else
- #define cimg_verbosity 1
- #endif
- #elif !(cimg_verbosity==0 || cimg_verbosity==1 || cimg_verbosity==2 || cimg_verbosity==3 || cimg_verbosity==4)
- #error CImg Library: Configuration variable 'cimg_verbosity' is badly defined.
- #error (should be { 0=quiet | 1=console | 2=dialog | 3=console+warnings | 4=dialog+warnings }).
- #endif
- // Configure OpenMP support.
- // (http://www.openmp.org)
- //
- // Define 'cimg_use_openmp' to enable OpenMP support (requires OpenMP 3.0+).
- //
- // OpenMP directives are used in many CImg functions to get
- // advantages of multi-core CPUs.
- #if !defined(cimg_use_openmp)
- #ifdef _OPENMP
- #define cimg_use_openmp 1
- #else
- #define cimg_use_openmp 0
- #endif
- #endif
- #if cimg_use_openmp!=0
- #include <omp.h>
- #define cimg_pragma_openmp(p) cimg_pragma(omp p)
- #else
- #define cimg_pragma_openmp(p)
- #endif
- // Configure the 'abort' signal handler (does nothing by default).
- // A typical signal handler can be defined in your own source like this:
- // #define cimg_abort_test if (is_abort) throw CImgAbortException("")
- //
- // where 'is_abort' is a boolean variable defined somewhere in your code and reachable in the method.
- // 'cimg_abort_test2' does the same but is called more often (in inner loops).
- #if defined(cimg_abort_test) && cimg_use_openmp!=0
- // Define abort macros to be used with OpenMP.
- #ifndef _cimg_abort_init_openmp
- #define _cimg_abort_init_openmp bool _cimg_abort_go_openmp = true; cimg::unused(_cimg_abort_go_openmp)
- #endif
- #ifndef _cimg_abort_try_openmp
- #define _cimg_abort_try_openmp if (_cimg_abort_go_openmp) try
- #endif
- #ifndef _cimg_abort_catch_openmp
- #define _cimg_abort_catch_openmp catch (CImgAbortException&) { cimg_pragma(omp atomic) _cimg_abort_go_openmp&=false; }
- #endif
- #ifndef _cimg_abort_catch_fill_openmp
- #define _cimg_abort_catch_fill_openmp \
- catch (CImgException& e) { cimg_pragma(omp critical(abort)) CImg<charT>::string(e._message).move_to(is_error); \
- cimg_pragma(omp atomic) _cimg_abort_go_openmp&=false; }
- #endif
- #ifdef cimg_abort_test2
- #ifndef _cimg_abort_try_openmp2
- #define _cimg_abort_try_openmp2 _cimg_abort_try_openmp
- #endif
- #ifndef _cimg_abort_catch_openmp2
- #define _cimg_abort_catch_openmp2 _cimg_abort_catch_openmp
- #endif
- #endif
- #endif
- #ifndef _cimg_abort_init_openmp
- #define _cimg_abort_init_openmp
- #endif
- #ifndef _cimg_abort_try_openmp
- #define _cimg_abort_try_openmp
- #endif
- #ifndef _cimg_abort_catch_openmp
- #define _cimg_abort_catch_openmp
- #endif
- #ifndef _cimg_abort_try_openmp2
- #define _cimg_abort_try_openmp2
- #endif
- #ifndef _cimg_abort_catch_openmp2
- #define _cimg_abort_catch_openmp2
- #endif
- #ifndef _cimg_abort_catch_fill_openmp
- #define _cimg_abort_catch_fill_openmp
- #endif
- #ifndef cimg_abort_init
- #define cimg_abort_init
- #endif
- #ifndef cimg_abort_test
- #define cimg_abort_test
- #endif
- #ifndef cimg_abort_test2
- #define cimg_abort_test2
- #endif
- // Configure display framework.
- //
- // Define 'cimg_display' to: '0' to disable display capabilities.
- // '1' to use the X-Window framework (X11).
- // '2' to use the Microsoft GDI32 framework.
- #ifndef cimg_display
- #if cimg_OS==0
- #define cimg_display 0
- #elif cimg_OS==1
- #define cimg_display 1
- #elif cimg_OS==2
- #define cimg_display 2
- #endif
- #elif !(cimg_display==0 || cimg_display==1 || cimg_display==2)
- #error CImg Library: Configuration variable 'cimg_display' is badly defined.
- #error (should be { 0=none | 1=X-Window (X11) | 2=Microsoft GDI32 }).
- #endif
- // Include display-specific headers.
- #if cimg_display==1
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/keysym.h>
- #include <pthread.h>
- #ifdef cimg_use_xshm
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <X11/extensions/XShm.h>
- #endif
- #ifdef cimg_use_xrandr
- #include <X11/extensions/Xrandr.h>
- #endif
- #endif
- #ifndef cimg_appname
- #define cimg_appname "CImg"
- #endif
- // Configure OpenCV support.
- // (http://opencv.willowgarage.com/wiki/)
- //
- // Define 'cimg_use_opencv' to enable OpenCV support.
- //
- // OpenCV library may be used to access images from cameras
- // (see method 'CImg<T>::load_camera()').
- #ifdef cimg_use_opencv
- #ifdef True
- #undef True
- #define _cimg_redefine_True
- #endif
- #ifdef False
- #undef False
- #define _cimg_redefine_False
- #endif
- #ifdef Status
- #undef Status
- #define _cimg_redefine_Status
- #endif
- #include <cstddef>
- #include <opencv2/opencv.hpp>
- #if CV_MAJOR_VERSION>=3
- #define _cimg_fourcc cv::VideoWriter::fourcc
- #define _cimg_cap_prop_frame_width cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH
- #define _cimg_cap_prop_frame_height cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT
- #define _cimg_cap_prop_frame_count cv::VideoCaptureProperties::CAP_PROP_FRAME_COUNT
- #else
- #define _cimg_fourcc CV_FOURCC
- #define _cimg_cap_prop_frame_width CV_CAP_PROP_FRAME_WIDTH
- #define _cimg_cap_prop_frame_height CV_CAP_PROP_FRAME_HEIGHT
- #define _cimg_cap_prop_frame_count CV_CAP_PROP_FRAME_COUNT
- #endif
- #endif
- // Configure LibPNG support.
- // (http://www.libpng.org)
- //
- // Define 'cimg_use_png' to enable LibPNG support.
- //
- // PNG library may be used to get a native support of '.png' files.
- // (see methods 'CImg<T>::{load,save}_png()'.
- #ifdef cimg_use_png
- extern "C" {
- #include "png.h"
- }
- #endif
- // Configure LibJPEG support.
- // (http://en.wikipedia.org/wiki/Libjpeg)
- //
- // Define 'cimg_use_jpeg' to enable LibJPEG support.
- //
- // JPEG library may be used to get a native support of '.jpg' files.
- // (see methods 'CImg<T>::{load,save}_jpeg()').
- #ifdef cimg_use_jpeg
- extern "C" {
- #include "jpeglib.h"
- #include "setjmp.h"
- }
- #endif
- // Configure LibTIFF support.
- // (http://www.libtiff.org)
- //
- // Define 'cimg_use_tiff' to enable LibTIFF support.
- //
- // TIFF library may be used to get a native support of '.tif' files.
- // (see methods 'CImg[List]<T>::{load,save}_tiff()').
- #ifdef cimg_use_tiff
- extern "C" {
- #define uint64 uint64_hack_
- #define int64 int64_hack_
- #include "tiffio.h"
- #undef uint64
- #undef int64
- }
- #endif
- // Configure HEIF support
- // (https://github.com/strukturag/libheif)
- //
- // Define 'cimg_use_heif' to enable HEIF support.
- //
- // HEIF library may be used to get a native support of '.heic' and '.avif' files.
- // (see method 'CImg<T>::load_heif()').
- #ifdef cimg_use_heif
- #include <libheif/heif_cxx.h>
- #endif
- // Configure LibMINC2 support.
- // (http://en.wikibooks.org/wiki/MINC/Reference/MINC2.0_File_Format_Reference)
- //
- // Define 'cimg_use_minc2' to enable LibMINC2 support.
- //
- // MINC2 library may be used to get a native support of '.mnc' files.
- // (see methods 'CImg<T>::{load,save}_minc2()').
- #ifdef cimg_use_minc2
- #include "minc_io_simple_volume.h"
- #include "minc_1_simple.h"
- #include "minc_1_simple_rw.h"
- #endif
- // Configure Zlib support.
- // (http://www.zlib.net)
- //
- // Define 'cimg_use_zlib' to enable Zlib support.
- //
- // Zlib library may be used to allow compressed data in '.cimgz' files
- // (see methods 'CImg[List]<T>::{load,save}_cimg()').
- #ifdef cimg_use_zlib
- extern "C" {
- #include "zlib.h"
- }
- #endif
- // Configure libcurl support.
- // (http://curl.haxx.se/libcurl/)
- //
- // Define 'cimg_use_curl' to enable libcurl support.
- //
- // Libcurl may be used to get a native support of file downloading from the network.
- // (see method 'cimg::load_network()'.)
- #ifdef cimg_use_curl
- #include "curl/curl.h"
- #endif
- // Configure Magick++ support.
- // (http://www.imagemagick.org/Magick++)
- //
- // Define 'cimg_use_magick' to enable Magick++ support.
- //
- // Magick++ library may be used to get a native support of various image file formats.
- // (see methods 'CImg<T>::{load,save}()').
- #ifdef cimg_use_magick
- #include "Magick++.h"
- #endif
- // Configure FFTW3 support.
- // (http://www.fftw.org)
- //
- // Define 'cimg_use_fftw3' to enable libFFTW3 support.
- //
- // FFTW3 library may be used to efficiently compute the Fast Fourier Transform
- // of image data, without restriction on the image size.
- // (see method 'CImg[List]<T>::FFT()').
- #ifdef cimg_use_fftw3
- extern "C" {
- #include "fftw3.h"
- }
- #endif
- // Configure LibBoard support.
- // (http://libboard.sourceforge.net/)
- //
- // Define 'cimg_use_board' to enable Board support.
- //
- // Board library may be used to draw 3D objects in vector-graphics canvas
- // that can be saved as '.ps' or '.svg' files afterwards.
- // (see method 'CImg<T>::draw_object3d()').
- #ifdef cimg_use_board
- #include "Board.h"
- #endif
- // Configure OpenEXR support.
- // (http://www.openexr.com/)
- //
- // Define 'cimg_use_openexr' to enable OpenEXR support.
- //
- // OpenEXR library may be used to get a native support of '.exr' files.
- // (see methods 'CImg<T>::{load,save}_exr()').
- #ifdef cimg_use_openexr
- #if __GNUC__>=5
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wdeprecated"
- #pragma GCC diagnostic ignored "-Wdeprecated-copy"
- #pragma GCC diagnostic ignored "-Wshadow"
- #endif
- #include "ImfRgbaFile.h"
- #include "ImfInputFile.h"
- #include "ImfChannelList.h"
- #include "ImfMatrixAttribute.h"
- #include "ImfArray.h"
- #if __GNUC__>=5
- #pragma GCC diagnostic pop
- #endif
- #endif
- // Configure TinyEXR support.
- // (https://github.com/syoyo/tinyexr)
- //
- // Define 'cimg_use_tinyexr' to enable TinyEXR support.
- //
- // TinyEXR is a small, single header-only library to load and save OpenEXR(.exr) images.
- #ifdef cimg_use_tinyexr
- #ifndef TINYEXR_IMPLEMENTATION
- #define TINYEXR_IMPLEMENTATION
- #endif
- #include "tinyexr.h"
- #endif
- // Lapack configuration.
- // (http://www.netlib.org/lapack)
- //
- // Define 'cimg_use_lapack' to enable LAPACK support.
- //
- // Lapack library may be used in several CImg methods to speed up
- // matrix computations (eigenvalues, inverse, ...).
- #ifdef cimg_use_lapack
- extern "C" {
- extern void sgetrf_(int*, int*, float*, int*, int*, int*);
- extern void sgetri_(int*, float*, int*, int*, float*, int*, int*);
- extern void sgetrs_(char*, int*, int*, float*, int*, int*, float*, int*, int*);
- extern void sgesvd_(char*, char*, int*, int*, float*, int*, float*, float*, int*, float*, int*, float*, int*, int*);
- extern void ssyev_(char*, char*, int*, float*, int*, float*, float*, int*, int*);
- extern void dgetrf_(int*, int*, double*, int*, int*, int*);
- extern void dgetri_(int*, double*, int*, int*, double*, int*, int*);
- extern void dgetrs_(char*, int*, int*, double*, int*, int*, double*, int*, int*);
- extern void dgesvd_(char*, char*, int*, int*, double*, int*, double*, double*,
- int*, double*, int*, double*, int*, int*);
- extern void dsyev_(char*, char*, int*, double*, int*, double*, double*, int*, int*);
- extern void dgels_(char*, int*,int*,int*,double*,int*,double*,int*,double*,int*,int*);
- extern void sgels_(char*, int*,int*,int*,float*,int*,float*,int*,float*,int*,int*);
- }
- #endif
- // Check if min/max/PI macros are defined.
- //
- // CImg does not compile if macros 'min', 'max' or 'PI' are defined,
- // because it redefines functions min(), max() and const variable PI in the cimg:: namespace.
- // so it '#undef' these macros if necessary, and restore them to reasonable
- // values at the end of this file.
- #ifdef min
- #undef min
- #define _cimg_redefine_min
- #endif
- #ifdef max
- #undef max
- #define _cimg_redefine_max
- #endif
- #ifdef PI
- #undef PI
- #define _cimg_redefine_PI
- #endif
- // Define 'cimg_library' namespace suffix.
- //
- // You may want to add a suffix to the 'cimg_library' namespace, for instance if you need to work
- // with several versions of the library at the same time.
- #ifdef cimg_namespace_suffix
- #define __cimg_library_suffixed(s) cimg_library_##s
- #define _cimg_library_suffixed(s) __cimg_library_suffixed(s)
- #define cimg_library_suffixed _cimg_library_suffixed(cimg_namespace_suffix)
- #else
- #define cimg_library_suffixed cimg_library
- #endif
- /*------------------------------------------------------------------------------
- #
- # Define user-friendly macros.
- #
- # These CImg macros are prefixed by 'cimg_' and can be used safely in your own
- # code. They are useful to parse command line options, or to write image loops.
- #
- ------------------------------------------------------------------------------*/
- // Macros to define program usage, and retrieve command line arguments.
- #define cimg_usage(usage) cimg_library_suffixed::cimg::option((char*)0,argc,argv,(char*)0,usage,false)
- #define cimg_help(str) cimg_library_suffixed::cimg::option((char*)0,argc,argv,str,(char*)0)
- #define cimg_option(name,_default,usage) cimg_library_suffixed::cimg::option(name,argc,argv,_default,usage)
- // Macros to define and manipulate local neighborhoods.
- #define CImg_2x2(I,T) T I[4]; \
- T& I##cc = I[0]; T& I##nc = I[1]; \
- T& I##cn = I[2]; T& I##nn = I[3]; \
- I##cc = I##nc = \
- I##cn = I##nn = 0
- #define CImg_3x3(I,T) T I[9]; \
- T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; \
- T& I##pc = I[3]; T& I##cc = I[4]; T& I##nc = I[5]; \
- T& I##pn = I[6]; T& I##cn = I[7]; T& I##nn = I[8]; \
- I##pp = I##cp = I##np = \
- I##pc = I##cc = I##nc = \
- I##pn = I##cn = I##nn = 0
- #define CImg_4x4(I,T) T I[16]; \
- T& I##pp = I[0]; T& I##cp = I[1]; T& I##np = I[2]; T& I##ap = I[3]; \
- T& I##pc = I[4]; T& I##cc = I[5]; T& I##nc = I[6]; T& I##ac = I[7]; \
- T& I##pn = I[8]; T& I##cn = I[9]; T& I##nn = I[10]; T& I##an = I[11]; \
- T& I##pa = I[12]; T& I##ca = I[13]; T& I##na = I[14]; T& I##aa = I[15]; \
- I##pp = I##cp = I##np = I##ap = \
- I##pc = I##cc = I##nc = I##ac = \
- I##pn = I##cn = I##nn = I##an = \
- I##pa = I##ca = I##na = I##aa = 0
- #define CImg_5x5(I,T) T I[25]; \
- 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]; \
- 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]; \
- 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]; \
- 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]; \
- 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]; \
- I##bb = I##pb = I##cb = I##nb = I##ab = \
- I##bp = I##pp = I##cp = I##np = I##ap = \
- I##bc = I##pc = I##cc = I##nc = I##ac = \
- I##bn = I##pn = I##cn = I##nn = I##an = \
- I##ba = I##pa = I##ca = I##na = I##aa = 0
- #define CImg_2x2x2(I,T) T I[8]; \
- T& I##ccc = I[0]; T& I##ncc = I[1]; \
- T& I##cnc = I[2]; T& I##nnc = I[3]; \
- T& I##ccn = I[4]; T& I##ncn = I[5]; \
- T& I##cnn = I[6]; T& I##nnn = I[7]; \
- I##ccc = I##ncc = \
- I##cnc = I##nnc = \
- I##ccn = I##ncn = \
- I##cnn = I##nnn = 0
- #define CImg_3x3x3(I,T) T I[27]; \
- T& I##ppp = I[0]; T& I##cpp = I[1]; T& I##npp = I[2]; \
- T& I##pcp = I[3]; T& I##ccp = I[4]; T& I##ncp = I[5]; \
- T& I##pnp = I[6]; T& I##cnp = I[7]; T& I##nnp = I[8]; \
- T& I##ppc = I[9]; T& I##cpc = I[10]; T& I##npc = I[11]; \
- T& I##pcc = I[12]; T& I##ccc = I[13]; T& I##ncc = I[14]; \
- T& I##pnc = I[15]; T& I##cnc = I[16]; T& I##nnc = I[17]; \
- T& I##ppn = I[18]; T& I##cpn = I[19]; T& I##npn = I[20]; \
- T& I##pcn = I[21]; T& I##ccn = I[22]; T& I##ncn = I[23]; \
- T& I##pnn = I[24]; T& I##cnn = I[25]; T& I##nnn = I[26]; \
- I##ppp = I##cpp = I##npp = \
- I##pcp = I##ccp = I##ncp = \
- I##pnp = I##cnp = I##nnp = \
- I##ppc = I##cpc = I##npc = \
- I##pcc = I##ccc = I##ncc = \
- I##pnc = I##cnc = I##nnc = \
- I##ppn = I##cpn = I##npn = \
- I##pcn = I##ccn = I##ncn = \
- I##pnn = I##cnn = I##nnn = 0
- #define cimg_def2x2(img,x,y) \
- int _n1##x = x<(img).width() - 1?x + 1:(img).width() - 1, \
- _n1##y = y<(img).height() - 1?y + 1:(img).height() - 1
- #define cimg_def3x3(img,x,y) \
- cimg_def2x2(img,x,y); \
- int _p1##x = x>1?x - 1:0, \
- _p1##y = y>1?y - 1:0
- #define cimg_def4x4(img,x,y) \
- cimg_def3x3(img,x,y); \
- int _n2##x = x<(img).width() - 2?x + 2:(img).width() - 1, \
- _n2##y = y<(img).height() - 2?y + 2:(img).height() - 1
- #define cimg_def5x5(img,x,y) \
- cimg_def4x4(img,x,y); \
- int _p2##x = x>2?x - 2:0, \
- _p2##y = y>2?y - 2:0
- #define cimg_def6x6(img,x,y) \
- cimg_def5x5(img,x,y); \
- int _n3##x = x<(img).width() - 3?x + 3:(img).width() - 1, \
- _n3##y = y<(img).height() - 3?y + 3:(img).height() - 1
- #define cimg_def7x7(img,x,y) \
- cimg_def6x6(img,x,y); \
- int _p3##x = x>3?x - 3:0, \
- _p3##y = y>3?y - 3:0
- #define cimg_def8x8(img,x,y) \
- cimg_def7x7(img,x,y); \
- int _n4##x = x<(img).width() - 4?x + 4:(img).width() - 1, \
- _n4##y = y<(img).height() - 4?y + 4:(img).height() - 1
- #define cimg_def9x9(img,x,y) \
- cimg_def8x8(img,x,y); \
- int _p4##x = x>4?x - 4:0, \
- _p4##y = y>4?y - 4:0
- #define cimg_def2x2x2(img,x,y,z) \
- cimg_def2x2(img,x,y); \
- int _n1##z = z<(img).depth() - 1?z + 1:(img).depth() - 1
- #define cimg_def3x3x3(img,x,y,z) \
- cimg_def2x2x2(img,x,y,z); \
- int _p1##x = x>1?x - 1:0, \
- _p1##y = y>1?y - 1:0, \
- _p1##z = z>1?z - 1:0
- #define cimg_get2x2(img,x,y,z,c,I,T) \
- 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), \
- I[3] = (T)(img)(_n1##x,_n1##y,z,c)
- #define cimg_get3x3(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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)
- #define cimg_get4x4(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- I[15] = (T)(img)(_n2##x,_n2##y,z,c)
- #define cimg_get5x5(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- I[24] = (T)(img)(_n2##x,_n2##y,z,c)
- #define cimg_get6x6(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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)
- #define cimg_get7x7(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- I[48] = (T)(img)(_n3##x,_n3##y,z,c)
- #define cimg_get8x8(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- I[63] = (T)(img)(_n4##x,_n4##y,z,c);
- #define cimg_get9x9(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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)
- #define cimg_get2x2x2(img,x,y,z,c,I,T) \
- 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), \
- 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), \
- I[6] = (T)(img)(x,_n1##y,_n1##z,c), I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)
- #define cimg_get3x3x3(img,x,y,z,c,I,T) \
- I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c), I[1] = (T)(img)(x,_p1##y,_p1##z,c), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- 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), \
- I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)
- // Macros to perform various image loops.
- //
- // These macros are simpler to use than loops with C++ iterators.
- #define cimg_for(img,ptrs,T_ptrs) \
- for (T_ptrs *ptrs = (img)._data, *_max##ptrs = (img)._data + (img).size(); ptrs<_max##ptrs; ++ptrs)
- #define cimg_rof(img,ptrs,T_ptrs) for (T_ptrs *ptrs = (img)._data + (img).size() - 1; ptrs>=(img)._data; --ptrs)
- #define cimg_foroff(img,off) for (cimg_ulong off = 0, _max##off = (img).size(); off<_max##off; ++off)
- #define cimg_rofoff(img,off) for (cimg_long off = (cimg_long)((img).size() - 1); off>=0; --off)
- #define cimg_for1(bound,i) for (int i = 0; i<(int)(bound); ++i)
- #define cimg_forX(img,x) cimg_for1((img)._width,x)
- #define cimg_forY(img,y) cimg_for1((img)._height,y)
- #define cimg_forZ(img,z) cimg_for1((img)._depth,z)
- #define cimg_forC(img,c) cimg_for1((img)._spectrum,c)
- #define cimg_forXY(img,x,y) cimg_forY(img,y) cimg_forX(img,x)
- #define cimg_forXZ(img,x,z) cimg_forZ(img,z) cimg_forX(img,x)
- #define cimg_forYZ(img,y,z) cimg_forZ(img,z) cimg_forY(img,y)
- #define cimg_forXC(img,x,c) cimg_forC(img,c) cimg_forX(img,x)
- #define cimg_forYC(img,y,c) cimg_forC(img,c) cimg_forY(img,y)
- #define cimg_forZC(img,z,c) cimg_forC(img,c) cimg_forZ(img,z)
- #define cimg_forXYZ(img,x,y,z) cimg_forZ(img,z) cimg_forXY(img,x,y)
- #define cimg_forXYC(img,x,y,c) cimg_forC(img,c) cimg_forXY(img,x,y)
- #define cimg_forXZC(img,x,z,c) cimg_forC(img,c) cimg_forXZ(img,x,z)
- #define cimg_forYZC(img,y,z,c) cimg_forC(img,c) cimg_forYZ(img,y,z)
- #define cimg_forXYZC(img,x,y,z,c) cimg_forC(img,c) cimg_forXYZ(img,x,y,z)
- #define cimg_rof1(bound,i) for (int i = (int)(bound) - 1; i>=0; --i)
- #define cimg_rofX(img,x) cimg_rof1((img)._width,x)
- #define cimg_rofY(img,y) cimg_rof1((img)._height,y)
- #define cimg_rofZ(img,z) cimg_rof1((img)._depth,z)
- #define cimg_rofC(img,c) cimg_rof1((img)._spectrum,c)
- #define cimg_rofXY(img,x,y) cimg_rofY(img,y) cimg_rofX(img,x)
- #define cimg_rofXZ(img,x,z) cimg_rofZ(img,z) cimg_rofX(img,x)
- #define cimg_rofYZ(img,y,z) cimg_rofZ(img,z) cimg_rofY(img,y)
- #define cimg_rofXC(img,x,c) cimg_rofC(img,c) cimg_rofX(img,x)
- #define cimg_rofYC(img,y,c) cimg_rofC(img,c) cimg_rofY(img,y)
- #define cimg_rofZC(img,z,c) cimg_rofC(img,c) cimg_rofZ(img,z)
- #define cimg_rofXYZ(img,x,y,z) cimg_rofZ(img,z) cimg_rofXY(img,x,y)
- #define cimg_rofXYC(img,x,y,c) cimg_rofC(img,c) cimg_rofXY(img,x,y)
- #define cimg_rofXZC(img,x,z,c) cimg_rofC(img,c) cimg_rofXZ(img,x,z)
- #define cimg_rofYZC(img,y,z,c) cimg_rofC(img,c) cimg_rofYZ(img,y,z)
- #define cimg_rofXYZC(img,x,y,z,c) cimg_rofC(img,c) cimg_rofXYZ(img,x,y,z)
- #define cimg_for_in1(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), _max##i = (int)(i1)<(int)(bound)?(int)(i1):(int)(bound) - 1; i<=_max##i; ++i)
- #define cimg_for_inX(img,x0,x1,x) cimg_for_in1((img)._width,x0,x1,x)
- #define cimg_for_inY(img,y0,y1,y) cimg_for_in1((img)._height,y0,y1,y)
- #define cimg_for_inZ(img,z0,z1,z) cimg_for_in1((img)._depth,z0,z1,z)
- #define cimg_for_inC(img,c0,c1,c) cimg_for_in1((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_inXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_inC(img,c0,c1,c) cimg_for_inXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for_insideX(img,x,n) cimg_for_inX(img,n,(img)._width - 1 - (n),x)
- #define cimg_for_insideY(img,y,n) cimg_for_inY(img,n,(img)._height - 1 - (n),y)
- #define cimg_for_insideZ(img,z,n) cimg_for_inZ(img,n,(img)._depth - 1 - (n),z)
- #define cimg_for_insideC(img,c,n) cimg_for_inC(img,n,(img)._spectrum - 1 - (n),c)
- #define cimg_for_insideXY(img,x,y,n) cimg_for_inXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
- #define cimg_for_insideXYZ(img,x,y,z,n) \
- cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
- #define cimg_for_insideXYZC(img,x,y,z,c,n) \
- cimg_for_inXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
- #define cimg_for_out1(boundi,i0,i1,i) \
- for (int i = (int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); ++i, i = i==(int)(i0)?(int)(i1) + 1:i)
- #define cimg_for_out2(boundi,boundj,i0,j0,i1,j1,i,j) \
- for (int j = 0; j<(int)(boundj); ++j) \
- for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
- ++i, i = _n1j?i:(i==(int)(i0)?(int)(i1) + 1:i))
- #define cimg_for_out3(boundi,boundj,boundk,i0,j0,k0,i1,j1,k1,i,j,k) \
- for (int k = 0; k<(int)(boundk); ++k) \
- for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
- for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k?0:(int)(i0)>0?0:(int)(i1) + 1; i<(int)(boundi); \
- ++i, i = _n1j || _n1k?i:(i==(int)(i0)?(int)(i1) + 1:i))
- #define cimg_for_out4(boundi,boundj,boundk,boundl,i0,j0,k0,l0,i1,j1,k1,l1,i,j,k,l) \
- for (int l = 0; l<(int)(boundl); ++l) \
- for (int _n1l = (int)(l<(int)(l0) || l>(int)(l1)), k = 0; k<(int)(boundk); ++k) \
- for (int _n1k = (int)(k<(int)(k0) || k>(int)(k1)), j = 0; j<(int)(boundj); ++j) \
- for (int _n1j = (int)(j<(int)(j0) || j>(int)(j1)), i = _n1j || _n1k || _n1l?0:(int)(i0)>0?0:(int)(i1) + 1; \
- i<(int)(boundi); ++i, i = _n1j || _n1k || _n1l?i:(i==(int)(i0)?(int)(i1) + 1:i))
- #define cimg_for_outX(img,x0,x1,x) cimg_for_out1((img)._width,x0,x1,x)
- #define cimg_for_outY(img,y0,y1,y) cimg_for_out1((img)._height,y0,y1,y)
- #define cimg_for_outZ(img,z0,z1,z) cimg_for_out1((img)._depth,z0,z1,z)
- #define cimg_for_outC(img,c0,c1,c) cimg_for_out1((img)._spectrum,c0,c1,c)
- #define cimg_for_outXY(img,x0,y0,x1,y1,x,y) cimg_for_out2((img)._width,(img)._height,x0,y0,x1,y1,x,y)
- #define cimg_for_outXZ(img,x0,z0,x1,z1,x,z) cimg_for_out2((img)._width,(img)._depth,x0,z0,x1,z1,x,z)
- #define cimg_for_outXC(img,x0,c0,x1,c1,x,c) cimg_for_out2((img)._width,(img)._spectrum,x0,c0,x1,c1,x,c)
- #define cimg_for_outYZ(img,y0,z0,y1,z1,y,z) cimg_for_out2((img)._height,(img)._depth,y0,z0,y1,z1,y,z)
- #define cimg_for_outYC(img,y0,c0,y1,c1,y,c) cimg_for_out2((img)._height,(img)._spectrum,y0,c0,y1,c1,y,c)
- #define cimg_for_outZC(img,z0,c0,z1,c1,z,c) cimg_for_out2((img)._depth,(img)._spectrum,z0,c0,z1,c1,z,c)
- #define cimg_for_outXYZ(img,x0,y0,z0,x1,y1,z1,x,y,z) \
- cimg_for_out3((img)._width,(img)._height,(img)._depth,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for_outXYC(img,x0,y0,c0,x1,y1,c1,x,y,c) \
- cimg_for_out3((img)._width,(img)._height,(img)._spectrum,x0,y0,c0,x1,y1,c1,x,y,c)
- #define cimg_for_outXZC(img,x0,z0,c0,x1,z1,c1,x,z,c) \
- cimg_for_out3((img)._width,(img)._depth,(img)._spectrum,x0,z0,c0,x1,z1,c1,x,z,c)
- #define cimg_for_outYZC(img,y0,z0,c0,y1,z1,c1,y,z,c) \
- cimg_for_out3((img)._height,(img)._depth,(img)._spectrum,y0,z0,c0,y1,z1,c1,y,z,c)
- #define cimg_for_outXYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_out4((img)._width,(img)._height,(img)._depth,(img)._spectrum,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c)
- #define cimg_for_borderX(img,x,n) cimg_for_outX(img,n,(img)._width - 1 - (n),x)
- #define cimg_for_borderY(img,y,n) cimg_for_outY(img,n,(img)._height - 1 - (n),y)
- #define cimg_for_borderZ(img,z,n) cimg_for_outZ(img,n,(img)._depth - 1 - (n),z)
- #define cimg_for_borderC(img,c,n) cimg_for_outC(img,n,(img)._spectrum - 1 - (n),c)
- #define cimg_for_borderXY(img,x,y,n) cimg_for_outXY(img,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),x,y)
- #define cimg_for_borderXYZ(img,x,y,z,n) \
- cimg_for_outXYZ(img,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n),(img)._depth - 1 - (n),x,y,z)
- #define cimg_for_borderXYZC(img,x,y,z,c,n) \
- cimg_for_outXYZC(img,n,n,n,n,(img)._width - 1 - (n),(img)._height - 1 - (n), \
- (img)._depth - 1 - (n),(img)._spectrum - 1 - (n),x,y,z,c)
- #define cimg_for_spiralXY(img,x,y) \
- for (int x = 0, y = 0, _n1##x = 1, _n1##y = (img).width()*(img).height(); _n1##y; \
- --_n1##y, _n1##x+=(_n1##x>>2) - ((!(_n1##x&3)?--y:((_n1##x&3)==1?(img)._width - 1 - ++x:\
- ((_n1##x&3)==2?(img)._height - 1 - ++y:--x))))?0:1)
- #define cimg_for_lineXY(x,y,x0,y0,x1,y1) \
- for (int x = (int)(x0), y = (int)(y0), _sx = 1, _sy = 1, _steep = 0, \
- _dx=(x1)>(x0)?(int)(x1) - (int)(x0):(_sx=-1,(int)(x0) - (int)(x1)), \
- _dy=(y1)>(y0)?(int)(y1) - (int)(y0):(_sy=-1,(int)(y0) - (int)(y1)), \
- _counter = _dx, \
- _err = _dx>_dy?(_dy>>1):((_steep=1),(_counter=_dy),(_dx>>1)); \
- _counter>=0; \
- --_counter, x+=_steep? \
- (y+=_sy,(_err-=_dx)<0?_err+=_dy,_sx:0): \
- (y+=(_err-=_dy)<0?_err+=_dx,_sy:0,_sx))
- #define cimg_for2(bound,i) \
- for (int i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
- _n1##i<(int)(bound) || i==--_n1##i; \
- ++i, ++_n1##i)
- #define cimg_for2X(img,x) cimg_for2((img)._width,x)
- #define cimg_for2Y(img,y) cimg_for2((img)._height,y)
- #define cimg_for2Z(img,z) cimg_for2((img)._depth,z)
- #define cimg_for2C(img,c) cimg_for2((img)._spectrum,c)
- #define cimg_for2XY(img,x,y) cimg_for2Y(img,y) cimg_for2X(img,x)
- #define cimg_for2XZ(img,x,z) cimg_for2Z(img,z) cimg_for2X(img,x)
- #define cimg_for2XC(img,x,c) cimg_for2C(img,c) cimg_for2X(img,x)
- #define cimg_for2YZ(img,y,z) cimg_for2Z(img,z) cimg_for2Y(img,y)
- #define cimg_for2YC(img,y,c) cimg_for2C(img,c) cimg_for2Y(img,y)
- #define cimg_for2ZC(img,z,c) cimg_for2C(img,c) cimg_for2Z(img,z)
- #define cimg_for2XYZ(img,x,y,z) cimg_for2Z(img,z) cimg_for2XY(img,x,y)
- #define cimg_for2XZC(img,x,z,c) cimg_for2C(img,c) cimg_for2XZ(img,x,z)
- #define cimg_for2YZC(img,y,z,c) cimg_for2C(img,c) cimg_for2YZ(img,y,z)
- #define cimg_for2XYZC(img,x,y,z,c) cimg_for2C(img,c) cimg_for2XYZ(img,x,y,z)
- #define cimg_for_in2(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
- i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
- ++i, ++_n1##i)
- #define cimg_for_in2X(img,x0,x1,x) cimg_for_in2((img)._width,x0,x1,x)
- #define cimg_for_in2Y(img,y0,y1,y) cimg_for_in2((img)._height,y0,y1,y)
- #define cimg_for_in2Z(img,z0,z1,z) cimg_for_in2((img)._depth,z0,z1,z)
- #define cimg_for_in2C(img,c0,c1,c) cimg_for_in2((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in2XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in2C(img,c0,c1,c) cimg_for_in2XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for3(bound,i) \
- for (int i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
- _n1##i<(int)(bound) || i==--_n1##i; \
- _p1##i = i++, ++_n1##i)
- #define cimg_for3X(img,x) cimg_for3((img)._width,x)
- #define cimg_for3Y(img,y) cimg_for3((img)._height,y)
- #define cimg_for3Z(img,z) cimg_for3((img)._depth,z)
- #define cimg_for3C(img,c) cimg_for3((img)._spectrum,c)
- #define cimg_for3XY(img,x,y) cimg_for3Y(img,y) cimg_for3X(img,x)
- #define cimg_for3XZ(img,x,z) cimg_for3Z(img,z) cimg_for3X(img,x)
- #define cimg_for3XC(img,x,c) cimg_for3C(img,c) cimg_for3X(img,x)
- #define cimg_for3YZ(img,y,z) cimg_for3Z(img,z) cimg_for3Y(img,y)
- #define cimg_for3YC(img,y,c) cimg_for3C(img,c) cimg_for3Y(img,y)
- #define cimg_for3ZC(img,z,c) cimg_for3C(img,c) cimg_for3Z(img,z)
- #define cimg_for3XYZ(img,x,y,z) cimg_for3Z(img,z) cimg_for3XY(img,x,y)
- #define cimg_for3XZC(img,x,z,c) cimg_for3C(img,c) cimg_for3XZ(img,x,z)
- #define cimg_for3YZC(img,y,z,c) cimg_for3C(img,c) cimg_for3YZ(img,y,z)
- #define cimg_for3XYZC(img,x,y,z,c) cimg_for3C(img,c) cimg_for3XYZ(img,x,y,z)
- #define cimg_for_in3(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1; \
- i<=(int)(i1) && (_n1##i<(int)(bound) || i==--_n1##i); \
- _p1##i = i++, ++_n1##i)
- #define cimg_for_in3X(img,x0,x1,x) cimg_for_in3((img)._width,x0,x1,x)
- #define cimg_for_in3Y(img,y0,y1,y) cimg_for_in3((img)._height,y0,y1,y)
- #define cimg_for_in3Z(img,z0,z1,z) cimg_for_in3((img)._depth,z0,z1,z)
- #define cimg_for_in3C(img,c0,c1,c) cimg_for_in3((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in3XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in3C(img,c0,c1,c) cimg_for_in3XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for4(bound,i) \
- for (int i = 0, _p1##i = 0, _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
- _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
- _p1##i = i++, ++_n1##i, ++_n2##i)
- #define cimg_for4X(img,x) cimg_for4((img)._width,x)
- #define cimg_for4Y(img,y) cimg_for4((img)._height,y)
- #define cimg_for4Z(img,z) cimg_for4((img)._depth,z)
- #define cimg_for4C(img,c) cimg_for4((img)._spectrum,c)
- #define cimg_for4XY(img,x,y) cimg_for4Y(img,y) cimg_for4X(img,x)
- #define cimg_for4XZ(img,x,z) cimg_for4Z(img,z) cimg_for4X(img,x)
- #define cimg_for4XC(img,x,c) cimg_for4C(img,c) cimg_for4X(img,x)
- #define cimg_for4YZ(img,y,z) cimg_for4Z(img,z) cimg_for4Y(img,y)
- #define cimg_for4YC(img,y,c) cimg_for4C(img,c) cimg_for4Y(img,y)
- #define cimg_for4ZC(img,z,c) cimg_for4C(img,c) cimg_for4Z(img,z)
- #define cimg_for4XYZ(img,x,y,z) cimg_for4Z(img,z) cimg_for4XY(img,x,y)
- #define cimg_for4XZC(img,x,z,c) cimg_for4C(img,c) cimg_for4XZ(img,x,z)
- #define cimg_for4YZC(img,y,z,c) cimg_for4C(img,c) cimg_for4YZ(img,y,z)
- #define cimg_for4XYZC(img,x,y,z,c) cimg_for4C(img,c) cimg_for4XYZ(img,x,y,z)
- #define cimg_for_in4(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
- i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
- _p1##i = i++, ++_n1##i, ++_n2##i)
- #define cimg_for_in4X(img,x0,x1,x) cimg_for_in4((img)._width,x0,x1,x)
- #define cimg_for_in4Y(img,y0,y1,y) cimg_for_in4((img)._height,y0,y1,y)
- #define cimg_for_in4Z(img,z0,z1,z) cimg_for_in4((img)._depth,z0,z1,z)
- #define cimg_for_in4C(img,c0,c1,c) cimg_for_in4((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in4XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in4C(img,c0,c1,c) cimg_for_in4XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for5(bound,i) \
- for (int i = 0, _p2##i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(bound)?(int)(bound) - 1:2; \
- _n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i); \
- _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
- #define cimg_for5X(img,x) cimg_for5((img)._width,x)
- #define cimg_for5Y(img,y) cimg_for5((img)._height,y)
- #define cimg_for5Z(img,z) cimg_for5((img)._depth,z)
- #define cimg_for5C(img,c) cimg_for5((img)._spectrum,c)
- #define cimg_for5XY(img,x,y) cimg_for5Y(img,y) cimg_for5X(img,x)
- #define cimg_for5XZ(img,x,z) cimg_for5Z(img,z) cimg_for5X(img,x)
- #define cimg_for5XC(img,x,c) cimg_for5C(img,c) cimg_for5X(img,x)
- #define cimg_for5YZ(img,y,z) cimg_for5Z(img,z) cimg_for5Y(img,y)
- #define cimg_for5YC(img,y,c) cimg_for5C(img,c) cimg_for5Y(img,y)
- #define cimg_for5ZC(img,z,c) cimg_for5C(img,c) cimg_for5Z(img,z)
- #define cimg_for5XYZ(img,x,y,z) cimg_for5Z(img,z) cimg_for5XY(img,x,y)
- #define cimg_for5XZC(img,x,z,c) cimg_for5C(img,c) cimg_for5XZ(img,x,z)
- #define cimg_for5YZC(img,y,z,c) cimg_for5C(img,c) cimg_for5YZ(img,y,z)
- #define cimg_for5XYZC(img,x,y,z,c) cimg_for5C(img,c) cimg_for5XYZ(img,x,y,z)
- #define cimg_for_in5(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p2##i = i - 2<0?0:i - 2, \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2; \
- i<=(int)(i1) && (_n2##i<(int)(bound) || _n1##i==--_n2##i || i==(_n2##i = --_n1##i)); \
- _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i)
- #define cimg_for_in5X(img,x0,x1,x) cimg_for_in5((img)._width,x0,x1,x)
- #define cimg_for_in5Y(img,y0,y1,y) cimg_for_in5((img)._height,y0,y1,y)
- #define cimg_for_in5Z(img,z0,z1,z) cimg_for_in5((img)._depth,z0,z1,z)
- #define cimg_for_in5C(img,c0,c1,c) cimg_for_in5((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in5XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in5C(img,c0,c1,c) cimg_for_in5XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for6(bound,i) \
- for (int i = 0, _p2##i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
- _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
- _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
- _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
- #define cimg_for6X(img,x) cimg_for6((img)._width,x)
- #define cimg_for6Y(img,y) cimg_for6((img)._height,y)
- #define cimg_for6Z(img,z) cimg_for6((img)._depth,z)
- #define cimg_for6C(img,c) cimg_for6((img)._spectrum,c)
- #define cimg_for6XY(img,x,y) cimg_for6Y(img,y) cimg_for6X(img,x)
- #define cimg_for6XZ(img,x,z) cimg_for6Z(img,z) cimg_for6X(img,x)
- #define cimg_for6XC(img,x,c) cimg_for6C(img,c) cimg_for6X(img,x)
- #define cimg_for6YZ(img,y,z) cimg_for6Z(img,z) cimg_for6Y(img,y)
- #define cimg_for6YC(img,y,c) cimg_for6C(img,c) cimg_for6Y(img,y)
- #define cimg_for6ZC(img,z,c) cimg_for6C(img,c) cimg_for6Z(img,z)
- #define cimg_for6XYZ(img,x,y,z) cimg_for6Z(img,z) cimg_for6XY(img,x,y)
- #define cimg_for6XZC(img,x,z,c) cimg_for6C(img,c) cimg_for6XZ(img,x,z)
- #define cimg_for6YZC(img,y,z,c) cimg_for6C(img,c) cimg_for6YZ(img,y,z)
- #define cimg_for6XYZC(img,x,y,z,c) cimg_for6C(img,c) cimg_for6XYZ(img,x,y,z)
- #define cimg_for_in6(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p2##i = i - 2<0?0:i - 2, \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
- _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
- i<=(int)(i1) && \
- (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
- _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
- #define cimg_for_in6X(img,x0,x1,x) cimg_for_in6((img)._width,x0,x1,x)
- #define cimg_for_in6Y(img,y0,y1,y) cimg_for_in6((img)._height,y0,y1,y)
- #define cimg_for_in6Z(img,z0,z1,z) cimg_for_in6((img)._depth,z0,z1,z)
- #define cimg_for_in6C(img,c0,c1,c) cimg_for_in6((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in6XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in6C(img,c0,c1,c) cimg_for_in6XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for7(bound,i) \
- for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
- _n3##i = 3>=(bound)?(int)(bound) - 1:3; \
- _n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i); \
- _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
- #define cimg_for7X(img,x) cimg_for7((img)._width,x)
- #define cimg_for7Y(img,y) cimg_for7((img)._height,y)
- #define cimg_for7Z(img,z) cimg_for7((img)._depth,z)
- #define cimg_for7C(img,c) cimg_for7((img)._spectrum,c)
- #define cimg_for7XY(img,x,y) cimg_for7Y(img,y) cimg_for7X(img,x)
- #define cimg_for7XZ(img,x,z) cimg_for7Z(img,z) cimg_for7X(img,x)
- #define cimg_for7XC(img,x,c) cimg_for7C(img,c) cimg_for7X(img,x)
- #define cimg_for7YZ(img,y,z) cimg_for7Z(img,z) cimg_for7Y(img,y)
- #define cimg_for7YC(img,y,c) cimg_for7C(img,c) cimg_for7Y(img,y)
- #define cimg_for7ZC(img,z,c) cimg_for7C(img,c) cimg_for7Z(img,z)
- #define cimg_for7XYZ(img,x,y,z) cimg_for7Z(img,z) cimg_for7XY(img,x,y)
- #define cimg_for7XZC(img,x,z,c) cimg_for7C(img,c) cimg_for7XZ(img,x,z)
- #define cimg_for7YZC(img,y,z,c) cimg_for7C(img,c) cimg_for7YZ(img,y,z)
- #define cimg_for7XYZC(img,x,y,z,c) cimg_for7C(img,c) cimg_for7XYZ(img,x,y,z)
- #define cimg_for_in7(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p3##i = i - 3<0?0:i - 3, \
- _p2##i = i - 2<0?0:i - 2, \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
- _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3; \
- i<=(int)(i1) && \
- (_n3##i<(int)(bound) || _n2##i==--_n3##i || _n1##i==--_n2##i || i==(_n3##i = _n2##i = --_n1##i)); \
- _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i)
- #define cimg_for_in7X(img,x0,x1,x) cimg_for_in7((img)._width,x0,x1,x)
- #define cimg_for_in7Y(img,y0,y1,y) cimg_for_in7((img)._height,y0,y1,y)
- #define cimg_for_in7Z(img,z0,z1,z) cimg_for_in7((img)._depth,z0,z1,z)
- #define cimg_for_in7C(img,c0,c1,c) cimg_for_in7((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in7XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in7C(img,c0,c1,c) cimg_for_in7XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for8(bound,i) \
- for (int i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(bound)?(int)(bound) - 1:2, \
- _n3##i = 3>=(bound)?(int)(bound) - 1:3, \
- _n4##i = 4>=(bound)?(int)(bound) - 1:4; \
- _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
- i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
- _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
- #define cimg_for8X(img,x) cimg_for8((img)._width,x)
- #define cimg_for8Y(img,y) cimg_for8((img)._height,y)
- #define cimg_for8Z(img,z) cimg_for8((img)._depth,z)
- #define cimg_for8C(img,c) cimg_for8((img)._spectrum,c)
- #define cimg_for8XY(img,x,y) cimg_for8Y(img,y) cimg_for8X(img,x)
- #define cimg_for8XZ(img,x,z) cimg_for8Z(img,z) cimg_for8X(img,x)
- #define cimg_for8XC(img,x,c) cimg_for8C(img,c) cimg_for8X(img,x)
- #define cimg_for8YZ(img,y,z) cimg_for8Z(img,z) cimg_for8Y(img,y)
- #define cimg_for8YC(img,y,c) cimg_for8C(img,c) cimg_for8Y(img,y)
- #define cimg_for8ZC(img,z,c) cimg_for8C(img,c) cimg_for8Z(img,z)
- #define cimg_for8XYZ(img,x,y,z) cimg_for8Z(img,z) cimg_for8XY(img,x,y)
- #define cimg_for8XZC(img,x,z,c) cimg_for8C(img,c) cimg_for8XZ(img,x,z)
- #define cimg_for8YZC(img,y,z,c) cimg_for8C(img,c) cimg_for8YZ(img,y,z)
- #define cimg_for8XYZC(img,x,y,z,c) cimg_for8C(img,c) cimg_for8XYZ(img,x,y,z)
- #define cimg_for_in8(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p3##i = i - 3<0?0:i - 3, \
- _p2##i = i - 2<0?0:i - 2, \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
- _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
- _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
- i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
- i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
- _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
- #define cimg_for_in8X(img,x0,x1,x) cimg_for_in8((img)._width,x0,x1,x)
- #define cimg_for_in8Y(img,y0,y1,y) cimg_for_in8((img)._height,y0,y1,y)
- #define cimg_for_in8Z(img,z0,z1,z) cimg_for_in8((img)._depth,z0,z1,z)
- #define cimg_for_in8C(img,c0,c1,c) cimg_for_in8((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in8XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in8C(img,c0,c1,c) cimg_for_in8XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for9(bound,i) \
- for (int i = 0, _p4##i = 0, _p3##i = 0, _p2##i = 0, _p1##i = 0, \
- _n1##i = 1>=(int)(bound)?(int)(bound) - 1:1, \
- _n2##i = 2>=(int)(bound)?(int)(bound) - 1:2, \
- _n3##i = 3>=(int)(bound)?(int)(bound) - 1:3, \
- _n4##i = 4>=(int)(bound)?(int)(bound) - 1:4; \
- _n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
- i==(_n4##i = _n3##i = _n2##i = --_n1##i); \
- _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
- #define cimg_for9X(img,x) cimg_for9((img)._width,x)
- #define cimg_for9Y(img,y) cimg_for9((img)._height,y)
- #define cimg_for9Z(img,z) cimg_for9((img)._depth,z)
- #define cimg_for9C(img,c) cimg_for9((img)._spectrum,c)
- #define cimg_for9XY(img,x,y) cimg_for9Y(img,y) cimg_for9X(img,x)
- #define cimg_for9XZ(img,x,z) cimg_for9Z(img,z) cimg_for9X(img,x)
- #define cimg_for9XC(img,x,c) cimg_for9C(img,c) cimg_for9X(img,x)
- #define cimg_for9YZ(img,y,z) cimg_for9Z(img,z) cimg_for9Y(img,y)
- #define cimg_for9YC(img,y,c) cimg_for9C(img,c) cimg_for9Y(img,y)
- #define cimg_for9ZC(img,z,c) cimg_for9C(img,c) cimg_for9Z(img,z)
- #define cimg_for9XYZ(img,x,y,z) cimg_for9Z(img,z) cimg_for9XY(img,x,y)
- #define cimg_for9XZC(img,x,z,c) cimg_for9C(img,c) cimg_for9XZ(img,x,z)
- #define cimg_for9YZC(img,y,z,c) cimg_for9C(img,c) cimg_for9YZ(img,y,z)
- #define cimg_for9XYZC(img,x,y,z,c) cimg_for9C(img,c) cimg_for9XYZ(img,x,y,z)
- #define cimg_for_in9(bound,i0,i1,i) \
- for (int i = (int)(i0)<0?0:(int)(i0), \
- _p4##i = i - 4<0?0:i - 4, \
- _p3##i = i - 3<0?0:i - 3, \
- _p2##i = i - 2<0?0:i - 2, \
- _p1##i = i - 1<0?0:i - 1, \
- _n1##i = i + 1>=(int)(bound)?(int)(bound) - 1:i + 1, \
- _n2##i = i + 2>=(int)(bound)?(int)(bound) - 1:i + 2, \
- _n3##i = i + 3>=(int)(bound)?(int)(bound) - 1:i + 3, \
- _n4##i = i + 4>=(int)(bound)?(int)(bound) - 1:i + 4; \
- i<=(int)(i1) && (_n4##i<(int)(bound) || _n3##i==--_n4##i || _n2##i==--_n3##i || _n1##i==--_n2##i || \
- i==(_n4##i = _n3##i = _n2##i = --_n1##i)); \
- _p4##i = _p3##i, _p3##i = _p2##i, _p2##i = _p1##i, _p1##i = i++, ++_n1##i, ++_n2##i, ++_n3##i, ++_n4##i)
- #define cimg_for_in9X(img,x0,x1,x) cimg_for_in9((img)._width,x0,x1,x)
- #define cimg_for_in9Y(img,y0,y1,y) cimg_for_in9((img)._height,y0,y1,y)
- #define cimg_for_in9Z(img,z0,z1,z) cimg_for_in9((img)._depth,z0,z1,z)
- #define cimg_for_in9C(img,c0,c1,c) cimg_for_in9((img)._spectrum,c0,c1,c)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #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)
- #define cimg_for_in9XYZC(img,x0,y0,z0,c0,x1,y1,z1,c1,x,y,z,c) \
- cimg_for_in9C(img,c0,c1,c) cimg_for_in9XYZ(img,x0,y0,z0,x1,y1,z1,x,y,z)
- #define cimg_for2x2(img,x,y,z,c,I,T) \
- cimg_for2((img)._height,y) for (int x = 0, \
- _n1##x = (int)( \
- (I[0] = (T)(img)(0,y,z,c)), \
- (I[2] = (T)(img)(0,_n1##y,z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[1] = (T)(img)(_n1##x,y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x; \
- I[0] = I[1], \
- I[2] = I[3], \
- ++x, ++_n1##x)
- #define cimg_for_in2x2(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in2((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _n1##x = (int)( \
- (I[0] = (T)(img)(x,y,z,c)), \
- (I[2] = (T)(img)(x,_n1##y,z,c)), \
- x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
- x<=(int)(x1) && ((_n1##x<(img).width() && ( \
- (I[1] = (T)(img)(_n1##x,y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x); \
- I[0] = I[1], \
- I[2] = I[3], \
- ++x, ++_n1##x)
- #define cimg_for3x3(img,x,y,z,c,I,T) \
- cimg_for3((img)._height,y) for (int x = 0, \
- _p1##x = 0, \
- _n1##x = (int)( \
- (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[3] = I[4] = (T)(img)(0,y,z,c)), \
- (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x; \
- I[0] = I[1], I[1] = I[2], \
- I[3] = I[4], I[4] = I[5], \
- I[6] = I[7], I[7] = I[8], \
- _p1##x = x++, ++_n1##x)
- #define cimg_for_in3x3(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in3((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = (int)( \
- (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[3] = (T)(img)(_p1##x,y,z,c)), \
- (I[6] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[1] = (T)(img)(x,_p1##y,z,c)), \
- (I[4] = (T)(img)(x,y,z,c)), \
- (I[7] = (T)(img)(x,_n1##y,z,c)), \
- x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
- x<=(int)(x1) && ((_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x); \
- I[0] = I[1], I[1] = I[2], \
- I[3] = I[4], I[4] = I[5], \
- I[6] = I[7], I[7] = I[8], \
- _p1##x = x++, ++_n1##x)
- #define cimg_for4x4(img,x,y,z,c,I,T) \
- cimg_for4((img)._height,y) for (int x = 0, \
- _p1##x = 0, \
- _n1##x = 1>=(img)._width?(img).width() - 1:1, \
- _n2##x = (int)( \
- (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[4] = I[5] = (T)(img)(0,y,z,c)), \
- (I[8] = I[9] = (T)(img)(0,_n1##y,z,c)), \
- (I[12] = I[13] = (T)(img)(0,_n2##y,z,c)), \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[6] = (T)(img)(_n1##x,y,z,c)), \
- (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
- 2>=(img)._width?(img).width() - 1:2); \
- (_n2##x<(img).width() && ( \
- (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[7] = (T)(img)(_n2##x,y,z,c)), \
- (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
- _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], \
- I[4] = I[5], I[5] = I[6], I[6] = I[7], \
- I[8] = I[9], I[9] = I[10], I[10] = I[11], \
- I[12] = I[13], I[13] = I[14], I[14] = I[15], \
- _p1##x = x++, ++_n1##x, ++_n2##x)
- #define cimg_for_in4x4(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in4((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
- _n2##x = (int)( \
- (I[0] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[4] = (T)(img)(_p1##x,y,z,c)), \
- (I[8] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[12] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[1] = (T)(img)(x,_p1##y,z,c)), \
- (I[5] = (T)(img)(x,y,z,c)), \
- (I[9] = (T)(img)(x,_n1##y,z,c)), \
- (I[13] = (T)(img)(x,_n2##y,z,c)), \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[6] = (T)(img)(_n1##x,y,z,c)), \
- (I[10] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,_n2##y,z,c)), \
- x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
- x<=(int)(x1) && ((_n2##x<(img).width() && ( \
- (I[3] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[7] = (T)(img)(_n2##x,y,z,c)), \
- (I[11] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[15] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
- _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], \
- I[4] = I[5], I[5] = I[6], I[6] = I[7], \
- I[8] = I[9], I[9] = I[10], I[10] = I[11], \
- I[12] = I[13], I[13] = I[14], I[14] = I[15], \
- _p1##x = x++, ++_n1##x, ++_n2##x)
- #define cimg_for5x5(img,x,y,z,c,I,T) \
- cimg_for5((img)._height,y) for (int x = 0, \
- _p2##x = 0, _p1##x = 0, \
- _n1##x = 1>=(img)._width?(img).width() - 1:1, \
- _n2##x = (int)( \
- (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[5] = I[6] = I[7] = (T)(img)(0,_p1##y,z,c)), \
- (I[10] = I[11] = I[12] = (T)(img)(0,y,z,c)), \
- (I[15] = I[16] = I[17] = (T)(img)(0,_n1##y,z,c)), \
- (I[20] = I[21] = I[22] = (T)(img)(0,_n2##y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[13] = (T)(img)(_n1##x,y,z,c)), \
- (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
- 2>=(img)._width?(img).width() - 1:2); \
- (_n2##x<(img).width() && ( \
- (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[14] = (T)(img)(_n2##x,y,z,c)), \
- (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
- _n1##x==--_n2##x || x==(_n2##x = --_n1##x); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \
- I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \
- I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \
- I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \
- I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
- _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x)
- #define cimg_for_in5x5(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in5((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p2##x = x - 2<0?0:x - 2, \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
- _n2##x = (int)( \
- (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[5] = (T)(img)(_p2##x,_p1##y,z,c)), \
- (I[10] = (T)(img)(_p2##x,y,z,c)), \
- (I[15] = (T)(img)(_p2##x,_n1##y,z,c)), \
- (I[20] = (T)(img)(_p2##x,_n2##y,z,c)), \
- (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \
- (I[6] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[11] = (T)(img)(_p1##x,y,z,c)), \
- (I[16] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[21] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[2] = (T)(img)(x,_p2##y,z,c)), \
- (I[7] = (T)(img)(x,_p1##y,z,c)), \
- (I[12] = (T)(img)(x,y,z,c)), \
- (I[17] = (T)(img)(x,_n1##y,z,c)), \
- (I[22] = (T)(img)(x,_n2##y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[13] = (T)(img)(_n1##x,y,z,c)), \
- (I[18] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[23] = (T)(img)(_n1##x,_n2##y,z,c)), \
- x + 2>=(int)(img)._width?(img).width() - 1:x + 2); \
- x<=(int)(x1) && ((_n2##x<(img).width() && ( \
- (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[9] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[14] = (T)(img)(_n2##x,y,z,c)), \
- (I[19] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[24] = (T)(img)(_n2##x,_n2##y,z,c)),1)) || \
- _n1##x==--_n2##x || x==(_n2##x = --_n1##x)); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], \
- I[5] = I[6], I[6] = I[7], I[7] = I[8], I[8] = I[9], \
- I[10] = I[11], I[11] = I[12], I[12] = I[13], I[13] = I[14], \
- I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], \
- I[20] = I[21], I[21] = I[22], I[22] = I[23], I[23] = I[24], \
- _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x)
- #define cimg_for6x6(img,x,y,z,c,I,T) \
- cimg_for6((img)._height,y) for (int x = 0, \
- _p2##x = 0, _p1##x = 0, \
- _n1##x = 1>=(img)._width?(img).width() - 1:1, \
- _n2##x = 2>=(img)._width?(img).width() - 1:2, \
- _n3##x = (int)( \
- (I[0] = I[1] = I[2] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[6] = I[7] = I[8] = (T)(img)(0,_p1##y,z,c)), \
- (I[12] = I[13] = I[14] = (T)(img)(0,y,z,c)), \
- (I[18] = I[19] = I[20] = (T)(img)(0,_n1##y,z,c)), \
- (I[24] = I[25] = I[26] = (T)(img)(0,_n2##y,z,c)), \
- (I[30] = I[31] = I[32] = (T)(img)(0,_n3##y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[15] = (T)(img)(_n1##x,y,z,c)), \
- (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[16] = (T)(img)(_n2##x,y,z,c)), \
- (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
- 3>=(img)._width?(img).width() - 1:3); \
- (_n3##x<(img).width() && ( \
- (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[17] = (T)(img)(_n3##x,y,z,c)), \
- (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
- _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \
- I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \
- I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], 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[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \
- I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \
- _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
- #define cimg_for_in6x6(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in6((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)x0, \
- _p2##x = x - 2<0?0:x - 2, \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
- _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
- _n3##x = (int)( \
- (I[0] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[6] = (T)(img)(_p2##x,_p1##y,z,c)), \
- (I[12] = (T)(img)(_p2##x,y,z,c)), \
- (I[18] = (T)(img)(_p2##x,_n1##y,z,c)), \
- (I[24] = (T)(img)(_p2##x,_n2##y,z,c)), \
- (I[30] = (T)(img)(_p2##x,_n3##y,z,c)), \
- (I[1] = (T)(img)(_p1##x,_p2##y,z,c)), \
- (I[7] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[13] = (T)(img)(_p1##x,y,z,c)), \
- (I[19] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[25] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[31] = (T)(img)(_p1##x,_n3##y,z,c)), \
- (I[2] = (T)(img)(x,_p2##y,z,c)), \
- (I[8] = (T)(img)(x,_p1##y,z,c)), \
- (I[14] = (T)(img)(x,y,z,c)), \
- (I[20] = (T)(img)(x,_n1##y,z,c)), \
- (I[26] = (T)(img)(x,_n2##y,z,c)), \
- (I[32] = (T)(img)(x,_n3##y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[9] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[15] = (T)(img)(_n1##x,y,z,c)), \
- (I[21] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[27] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[33] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[4] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[10] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[16] = (T)(img)(_n2##x,y,z,c)), \
- (I[22] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[28] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[34] = (T)(img)(_n2##x,_n3##y,z,c)), \
- x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
- x<=(int)(x1) && ((_n3##x<(img).width() && ( \
- (I[5] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[11] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[17] = (T)(img)(_n3##x,y,z,c)), \
- (I[23] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[29] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[35] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
- _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3## x = _n2##x = --_n1##x)); \
- I[0] = I[1], I[1] = I[2], I[2] = I[3], I[3] = I[4], I[4] = I[5], \
- I[6] = I[7], I[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], \
- I[12] = I[13], I[13] = I[14], I[14] = I[15], I[15] = I[16], 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[24] = I[25], I[25] = I[26], I[26] = I[27], I[27] = I[28], I[28] = I[29], \
- I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], I[34] = I[35], \
- _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
- #define cimg_for7x7(img,x,y,z,c,I,T) \
- cimg_for7((img)._height,y) for (int x = 0, \
- _p3##x = 0, _p2##x = 0, _p1##x = 0, \
- _n1##x = 1>=(img)._width?(img).width() - 1:1, \
- _n2##x = 2>=(img)._width?(img).width() - 1:2, \
- _n3##x = (int)( \
- (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
- (I[7] = I[8] = I[9] = I[10] = (T)(img)(0,_p2##y,z,c)), \
- (I[14] = I[15] = I[16] = I[17] = (T)(img)(0,_p1##y,z,c)), \
- (I[21] = I[22] = I[23] = I[24] = (T)(img)(0,y,z,c)), \
- (I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_n1##y,z,c)), \
- (I[35] = I[36] = I[37] = I[38] = (T)(img)(0,_n2##y,z,c)), \
- (I[42] = I[43] = I[44] = I[45] = (T)(img)(0,_n3##y,z,c)), \
- (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[25] = (T)(img)(_n1##x,y,z,c)), \
- (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[26] = (T)(img)(_n2##x,y,z,c)), \
- (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
- 3>=(img)._width?(img).width() - 1:3); \
- (_n3##x<(img).width() && ( \
- (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[27] = (T)(img)(_n3##x,y,z,c)), \
- (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
- _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x); \
- 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[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \
- I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \
- I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \
- I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \
- I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \
- I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
- _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
- #define cimg_for_in7x7(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in7((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p3##x = x - 3<0?0:x - 3, \
- _p2##x = x - 2<0?0:x - 2, \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(int)(img)._width?(img).width() - 1:x + 1, \
- _n2##x = x + 2>=(int)(img)._width?(img).width() - 1:x + 2, \
- _n3##x = (int)( \
- (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
- (I[7] = (T)(img)(_p3##x,_p2##y,z,c)), \
- (I[14] = (T)(img)(_p3##x,_p1##y,z,c)), \
- (I[21] = (T)(img)(_p3##x,y,z,c)), \
- (I[28] = (T)(img)(_p3##x,_n1##y,z,c)), \
- (I[35] = (T)(img)(_p3##x,_n2##y,z,c)), \
- (I[42] = (T)(img)(_p3##x,_n3##y,z,c)), \
- (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \
- (I[8] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[15] = (T)(img)(_p2##x,_p1##y,z,c)), \
- (I[22] = (T)(img)(_p2##x,y,z,c)), \
- (I[29] = (T)(img)(_p2##x,_n1##y,z,c)), \
- (I[36] = (T)(img)(_p2##x,_n2##y,z,c)), \
- (I[43] = (T)(img)(_p2##x,_n3##y,z,c)), \
- (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \
- (I[9] = (T)(img)(_p1##x,_p2##y,z,c)), \
- (I[16] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[23] = (T)(img)(_p1##x,y,z,c)), \
- (I[30] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[37] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[44] = (T)(img)(_p1##x,_n3##y,z,c)), \
- (I[3] = (T)(img)(x,_p3##y,z,c)), \
- (I[10] = (T)(img)(x,_p2##y,z,c)), \
- (I[17] = (T)(img)(x,_p1##y,z,c)), \
- (I[24] = (T)(img)(x,y,z,c)), \
- (I[31] = (T)(img)(x,_n1##y,z,c)), \
- (I[38] = (T)(img)(x,_n2##y,z,c)), \
- (I[45] = (T)(img)(x,_n3##y,z,c)), \
- (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[11] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[18] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[25] = (T)(img)(_n1##x,y,z,c)), \
- (I[32] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[39] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[46] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[12] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[19] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[26] = (T)(img)(_n2##x,y,z,c)), \
- (I[33] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[40] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[47] = (T)(img)(_n2##x,_n3##y,z,c)), \
- x + 3>=(int)(img)._width?(img).width() - 1:x + 3); \
- x<=(int)(x1) && ((_n3##x<(img).width() && ( \
- (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[13] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[20] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[27] = (T)(img)(_n3##x,y,z,c)), \
- (I[34] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[41] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[48] = (T)(img)(_n3##x,_n3##y,z,c)),1)) || \
- _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n3##x = _n2##x = --_n1##x)); \
- 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[7] = I[8], I[8] = I[9], I[9] = I[10], I[10] = I[11], I[11] = I[12], I[12] = I[13], \
- I[14] = I[15], I[15] = I[16], I[16] = I[17], I[17] = I[18], I[18] = I[19], I[19] = I[20], \
- I[21] = I[22], I[22] = I[23], I[23] = I[24], I[24] = I[25], I[25] = I[26], I[26] = I[27], \
- I[28] = I[29], I[29] = I[30], I[30] = I[31], I[31] = I[32], I[32] = I[33], I[33] = I[34], \
- I[35] = I[36], I[36] = I[37], I[37] = I[38], I[38] = I[39], I[39] = I[40], I[40] = I[41], \
- I[42] = I[43], I[43] = I[44], I[44] = I[45], I[45] = I[46], I[46] = I[47], I[47] = I[48], \
- _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x)
- #define cimg_for8x8(img,x,y,z,c,I,T) \
- cimg_for8((img)._height,y) for (int x = 0, \
- _p3##x = 0, _p2##x = 0, _p1##x = 0, \
- _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
- _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
- _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
- _n4##x = (int)( \
- (I[0] = I[1] = I[2] = I[3] = (T)(img)(_p3##x,_p3##y,z,c)), \
- (I[8] = I[9] = I[10] = I[11] = (T)(img)(0,_p2##y,z,c)), \
- (I[16] = I[17] = I[18] = I[19] = (T)(img)(0,_p1##y,z,c)), \
- (I[24] = I[25] = I[26] = I[27] = (T)(img)(0,y,z,c)), \
- (I[32] = I[33] = I[34] = I[35] = (T)(img)(0,_n1##y,z,c)), \
- (I[40] = I[41] = I[42] = I[43] = (T)(img)(0,_n2##y,z,c)), \
- (I[48] = I[49] = I[50] = I[51] = (T)(img)(0,_n3##y,z,c)), \
- (I[56] = I[57] = I[58] = I[59] = (T)(img)(0,_n4##y,z,c)), \
- (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[28] = (T)(img)(_n1##x,y,z,c)), \
- (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \
- (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[29] = (T)(img)(_n2##x,y,z,c)), \
- (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \
- (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \
- (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[30] = (T)(img)(_n3##x,y,z,c)), \
- (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
- (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
- 4>=((img)._width)?(img).width() - 1:4); \
- (_n4##x<(img).width() && ( \
- (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
- (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
- (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \
- (I[31] = (T)(img)(_n4##x,y,z,c)), \
- (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \
- (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \
- (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \
- (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
- _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \
- 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[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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
- #define cimg_for_in8x8(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in8((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p3##x = x - 3<0?0:x - 3, \
- _p2##x = x - 2<0?0:x - 2, \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
- _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
- _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
- _n4##x = (int)( \
- (I[0] = (T)(img)(_p3##x,_p3##y,z,c)), \
- (I[8] = (T)(img)(_p3##x,_p2##y,z,c)), \
- (I[16] = (T)(img)(_p3##x,_p1##y,z,c)), \
- (I[24] = (T)(img)(_p3##x,y,z,c)), \
- (I[32] = (T)(img)(_p3##x,_n1##y,z,c)), \
- (I[40] = (T)(img)(_p3##x,_n2##y,z,c)), \
- (I[48] = (T)(img)(_p3##x,_n3##y,z,c)), \
- (I[56] = (T)(img)(_p3##x,_n4##y,z,c)), \
- (I[1] = (T)(img)(_p2##x,_p3##y,z,c)), \
- (I[9] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[17] = (T)(img)(_p2##x,_p1##y,z,c)), \
- (I[25] = (T)(img)(_p2##x,y,z,c)), \
- (I[33] = (T)(img)(_p2##x,_n1##y,z,c)), \
- (I[41] = (T)(img)(_p2##x,_n2##y,z,c)), \
- (I[49] = (T)(img)(_p2##x,_n3##y,z,c)), \
- (I[57] = (T)(img)(_p2##x,_n4##y,z,c)), \
- (I[2] = (T)(img)(_p1##x,_p3##y,z,c)), \
- (I[10] = (T)(img)(_p1##x,_p2##y,z,c)), \
- (I[18] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[26] = (T)(img)(_p1##x,y,z,c)), \
- (I[34] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[42] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[50] = (T)(img)(_p1##x,_n3##y,z,c)), \
- (I[58] = (T)(img)(_p1##x,_n4##y,z,c)), \
- (I[3] = (T)(img)(x,_p3##y,z,c)), \
- (I[11] = (T)(img)(x,_p2##y,z,c)), \
- (I[19] = (T)(img)(x,_p1##y,z,c)), \
- (I[27] = (T)(img)(x,y,z,c)), \
- (I[35] = (T)(img)(x,_n1##y,z,c)), \
- (I[43] = (T)(img)(x,_n2##y,z,c)), \
- (I[51] = (T)(img)(x,_n3##y,z,c)), \
- (I[59] = (T)(img)(x,_n4##y,z,c)), \
- (I[4] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[12] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[20] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[28] = (T)(img)(_n1##x,y,z,c)), \
- (I[36] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[44] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[52] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[60] = (T)(img)(_n1##x,_n4##y,z,c)), \
- (I[5] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[13] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[21] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[29] = (T)(img)(_n2##x,y,z,c)), \
- (I[37] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[45] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[53] = (T)(img)(_n2##x,_n3##y,z,c)), \
- (I[61] = (T)(img)(_n2##x,_n4##y,z,c)), \
- (I[6] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[14] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[22] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[30] = (T)(img)(_n3##x,y,z,c)), \
- (I[38] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[46] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[54] = (T)(img)(_n3##x,_n3##y,z,c)), \
- (I[62] = (T)(img)(_n3##x,_n4##y,z,c)), \
- x + 4>=(img).width()?(img).width() - 1:x + 4); \
- x<=(int)(x1) && ((_n4##x<(img).width() && ( \
- (I[7] = (T)(img)(_n4##x,_p3##y,z,c)), \
- (I[15] = (T)(img)(_n4##x,_p2##y,z,c)), \
- (I[23] = (T)(img)(_n4##x,_p1##y,z,c)), \
- (I[31] = (T)(img)(_n4##x,y,z,c)), \
- (I[39] = (T)(img)(_n4##x,_n1##y,z,c)), \
- (I[47] = (T)(img)(_n4##x,_n2##y,z,c)), \
- (I[55] = (T)(img)(_n4##x,_n3##y,z,c)), \
- (I[63] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
- _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \
- 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[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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
- #define cimg_for9x9(img,x,y,z,c,I,T) \
- cimg_for9((img)._height,y) for (int x = 0, \
- _p4##x = 0, _p3##x = 0, _p2##x = 0, _p1##x = 0, \
- _n1##x = 1>=((img)._width)?(img).width() - 1:1, \
- _n2##x = 2>=((img)._width)?(img).width() - 1:2, \
- _n3##x = 3>=((img)._width)?(img).width() - 1:3, \
- _n4##x = (int)( \
- (I[0] = I[1] = I[2] = I[3] = I[4] = (T)(img)(_p4##x,_p4##y,z,c)), \
- (I[9] = I[10] = I[11] = I[12] = I[13] = (T)(img)(0,_p3##y,z,c)), \
- (I[18] = I[19] = I[20] = I[21] = I[22] = (T)(img)(0,_p2##y,z,c)), \
- (I[27] = I[28] = I[29] = I[30] = I[31] = (T)(img)(0,_p1##y,z,c)), \
- (I[36] = I[37] = I[38] = I[39] = I[40] = (T)(img)(0,y,z,c)), \
- (I[45] = I[46] = I[47] = I[48] = I[49] = (T)(img)(0,_n1##y,z,c)), \
- (I[54] = I[55] = I[56] = I[57] = I[58] = (T)(img)(0,_n2##y,z,c)), \
- (I[63] = I[64] = I[65] = I[66] = I[67] = (T)(img)(0,_n3##y,z,c)), \
- (I[72] = I[73] = I[74] = I[75] = I[76] = (T)(img)(0,_n4##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[41] = (T)(img)(_n1##x,y,z,c)), \
- (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \
- (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \
- (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[42] = (T)(img)(_n2##x,y,z,c)), \
- (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \
- (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \
- (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \
- (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[43] = (T)(img)(_n3##x,y,z,c)), \
- (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
- (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
- 4>=((img)._width)?(img).width() - 1:4); \
- (_n4##x<(img).width() && ( \
- (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
- (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
- (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \
- (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \
- (I[44] = (T)(img)(_n4##x,y,z,c)), \
- (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \
- (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \
- (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \
- (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
- _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x); \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- I[79] = I[80], \
- _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
- #define cimg_for_in9x9(img,x0,y0,x1,y1,x,y,z,c,I,T) \
- cimg_for_in9((img)._height,y0,y1,y) for (int x = (int)(x0)<0?0:(int)(x0), \
- _p4##x = x - 4<0?0:x - 4, \
- _p3##x = x - 3<0?0:x - 3, \
- _p2##x = x - 2<0?0:x - 2, \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = x + 1>=(img).width()?(img).width() - 1:x + 1, \
- _n2##x = x + 2>=(img).width()?(img).width() - 1:x + 2, \
- _n3##x = x + 3>=(img).width()?(img).width() - 1:x + 3, \
- _n4##x = (int)( \
- (I[0] = (T)(img)(_p4##x,_p4##y,z,c)), \
- (I[9] = (T)(img)(_p4##x,_p3##y,z,c)), \
- (I[18] = (T)(img)(_p4##x,_p2##y,z,c)), \
- (I[27] = (T)(img)(_p4##x,_p1##y,z,c)), \
- (I[36] = (T)(img)(_p4##x,y,z,c)), \
- (I[45] = (T)(img)(_p4##x,_n1##y,z,c)), \
- (I[54] = (T)(img)(_p4##x,_n2##y,z,c)), \
- (I[63] = (T)(img)(_p4##x,_n3##y,z,c)), \
- (I[72] = (T)(img)(_p4##x,_n4##y,z,c)), \
- (I[1] = (T)(img)(_p3##x,_p4##y,z,c)), \
- (I[10] = (T)(img)(_p3##x,_p3##y,z,c)), \
- (I[19] = (T)(img)(_p3##x,_p2##y,z,c)), \
- (I[28] = (T)(img)(_p3##x,_p1##y,z,c)), \
- (I[37] = (T)(img)(_p3##x,y,z,c)), \
- (I[46] = (T)(img)(_p3##x,_n1##y,z,c)), \
- (I[55] = (T)(img)(_p3##x,_n2##y,z,c)), \
- (I[64] = (T)(img)(_p3##x,_n3##y,z,c)), \
- (I[73] = (T)(img)(_p3##x,_n4##y,z,c)), \
- (I[2] = (T)(img)(_p2##x,_p4##y,z,c)), \
- (I[11] = (T)(img)(_p2##x,_p3##y,z,c)), \
- (I[20] = (T)(img)(_p2##x,_p2##y,z,c)), \
- (I[29] = (T)(img)(_p2##x,_p1##y,z,c)), \
- (I[38] = (T)(img)(_p2##x,y,z,c)), \
- (I[47] = (T)(img)(_p2##x,_n1##y,z,c)), \
- (I[56] = (T)(img)(_p2##x,_n2##y,z,c)), \
- (I[65] = (T)(img)(_p2##x,_n3##y,z,c)), \
- (I[74] = (T)(img)(_p2##x,_n4##y,z,c)), \
- (I[3] = (T)(img)(_p1##x,_p4##y,z,c)), \
- (I[12] = (T)(img)(_p1##x,_p3##y,z,c)), \
- (I[21] = (T)(img)(_p1##x,_p2##y,z,c)), \
- (I[30] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[39] = (T)(img)(_p1##x,y,z,c)), \
- (I[48] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[57] = (T)(img)(_p1##x,_n2##y,z,c)), \
- (I[66] = (T)(img)(_p1##x,_n3##y,z,c)), \
- (I[75] = (T)(img)(_p1##x,_n4##y,z,c)), \
- (I[4] = (T)(img)(x,_p4##y,z,c)), \
- (I[13] = (T)(img)(x,_p3##y,z,c)), \
- (I[22] = (T)(img)(x,_p2##y,z,c)), \
- (I[31] = (T)(img)(x,_p1##y,z,c)), \
- (I[40] = (T)(img)(x,y,z,c)), \
- (I[49] = (T)(img)(x,_n1##y,z,c)), \
- (I[58] = (T)(img)(x,_n2##y,z,c)), \
- (I[67] = (T)(img)(x,_n3##y,z,c)), \
- (I[76] = (T)(img)(x,_n4##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,_p4##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,_p3##y,z,c)), \
- (I[23] = (T)(img)(_n1##x,_p2##y,z,c)), \
- (I[32] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[41] = (T)(img)(_n1##x,y,z,c)), \
- (I[50] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[59] = (T)(img)(_n1##x,_n2##y,z,c)), \
- (I[68] = (T)(img)(_n1##x,_n3##y,z,c)), \
- (I[77] = (T)(img)(_n1##x,_n4##y,z,c)), \
- (I[6] = (T)(img)(_n2##x,_p4##y,z,c)), \
- (I[15] = (T)(img)(_n2##x,_p3##y,z,c)), \
- (I[24] = (T)(img)(_n2##x,_p2##y,z,c)), \
- (I[33] = (T)(img)(_n2##x,_p1##y,z,c)), \
- (I[42] = (T)(img)(_n2##x,y,z,c)), \
- (I[51] = (T)(img)(_n2##x,_n1##y,z,c)), \
- (I[60] = (T)(img)(_n2##x,_n2##y,z,c)), \
- (I[69] = (T)(img)(_n2##x,_n3##y,z,c)), \
- (I[78] = (T)(img)(_n2##x,_n4##y,z,c)), \
- (I[7] = (T)(img)(_n3##x,_p4##y,z,c)), \
- (I[16] = (T)(img)(_n3##x,_p3##y,z,c)), \
- (I[25] = (T)(img)(_n3##x,_p2##y,z,c)), \
- (I[34] = (T)(img)(_n3##x,_p1##y,z,c)), \
- (I[43] = (T)(img)(_n3##x,y,z,c)), \
- (I[52] = (T)(img)(_n3##x,_n1##y,z,c)), \
- (I[61] = (T)(img)(_n3##x,_n2##y,z,c)), \
- (I[70] = (T)(img)(_n3##x,_n3##y,z,c)), \
- (I[79] = (T)(img)(_n3##x,_n4##y,z,c)), \
- x + 4>=(img).width()?(img).width() - 1:x + 4); \
- x<=(int)(x1) && ((_n4##x<(img).width() && ( \
- (I[8] = (T)(img)(_n4##x,_p4##y,z,c)), \
- (I[17] = (T)(img)(_n4##x,_p3##y,z,c)), \
- (I[26] = (T)(img)(_n4##x,_p2##y,z,c)), \
- (I[35] = (T)(img)(_n4##x,_p1##y,z,c)), \
- (I[44] = (T)(img)(_n4##x,y,z,c)), \
- (I[53] = (T)(img)(_n4##x,_n1##y,z,c)), \
- (I[62] = (T)(img)(_n4##x,_n2##y,z,c)), \
- (I[71] = (T)(img)(_n4##x,_n3##y,z,c)), \
- (I[80] = (T)(img)(_n4##x,_n4##y,z,c)),1)) || \
- _n3##x==--_n4##x || _n2##x==--_n3##x || _n1##x==--_n2##x || x==(_n4##x = _n3##x = _n2##x = --_n1##x)); \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- 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], \
- I[79] = I[80], \
- _p4##x = _p3##x, _p3##x = _p2##x, _p2##x = _p1##x, _p1##x = x++, ++_n1##x, ++_n2##x, ++_n3##x, ++_n4##x)
- #define cimg_for2x2x2(img,x,y,z,c,I,T) \
- cimg_for2((img)._depth,z) cimg_for2((img)._height,y) for (int x = 0, \
- _n1##x = (int)( \
- (I[0] = (T)(img)(0,y,z,c)), \
- (I[2] = (T)(img)(0,_n1##y,z,c)), \
- (I[4] = (T)(img)(0,y,_n1##z,c)), \
- (I[6] = (T)(img)(0,_n1##y,_n1##z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[1] = (T)(img)(_n1##x,y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \
- (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
- x==--_n1##x; \
- I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \
- ++x, ++_n1##x)
- #define cimg_for_in2x2x2(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \
- 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), \
- _n1##x = (int)( \
- (I[0] = (T)(img)(x,y,z,c)), \
- (I[2] = (T)(img)(x,_n1##y,z,c)), \
- (I[4] = (T)(img)(x,y,_n1##z,c)), \
- (I[6] = (T)(img)(x,_n1##y,_n1##z,c)), \
- x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
- x<=(int)(x1) && ((_n1##x<(img).width() && ( \
- (I[1] = (T)(img)(_n1##x,y,z,c)), \
- (I[3] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,_n1##z,c)), \
- (I[7] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
- x==--_n1##x); \
- I[0] = I[1], I[2] = I[3], I[4] = I[5], I[6] = I[7], \
- ++x, ++_n1##x)
- #define cimg_for3x3x3(img,x,y,z,c,I,T) \
- cimg_for3((img)._depth,z) cimg_for3((img)._height,y) for (int x = 0, \
- _p1##x = 0, \
- _n1##x = (int)( \
- (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \
- (I[3] = I[4] = (T)(img)(0,y,_p1##z,c)), \
- (I[6] = I[7] = (T)(img)(0,_n1##y,_p1##z,c)), \
- (I[9] = I[10] = (T)(img)(0,_p1##y,z,c)), \
- (I[12] = I[13] = (T)(img)(0,y,z,c)), \
- (I[15] = I[16] = (T)(img)(0,_n1##y,z,c)), \
- (I[18] = I[19] = (T)(img)(0,_p1##y,_n1##z,c)), \
- (I[21] = I[22] = (T)(img)(0,y,_n1##z,c)), \
- (I[24] = I[25] = (T)(img)(0,_n1##y,_n1##z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
- (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \
- (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,y,z,c)), \
- (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \
- (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \
- (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
- x==--_n1##x; \
- I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \
- I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \
- I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \
- _p1##x = x++, ++_n1##x)
- #define cimg_for_in3x3x3(img,x0,y0,z0,x1,y1,z1,x,y,z,c,I,T) \
- 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), \
- _p1##x = x - 1<0?0:x - 1, \
- _n1##x = (int)( \
- (I[0] = (T)(img)(_p1##x,_p1##y,_p1##z,c)), \
- (I[3] = (T)(img)(_p1##x,y,_p1##z,c)), \
- (I[6] = (T)(img)(_p1##x,_n1##y,_p1##z,c)), \
- (I[9] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[12] = (T)(img)(_p1##x,y,z,c)), \
- (I[15] = (T)(img)(_p1##x,_n1##y,z,c)), \
- (I[18] = (T)(img)(_p1##x,_p1##y,_n1##z,c)), \
- (I[21] = (T)(img)(_p1##x,y,_n1##z,c)), \
- (I[24] = (T)(img)(_p1##x,_n1##y,_n1##z,c)), \
- (I[1] = (T)(img)(x,_p1##y,_p1##z,c)), \
- (I[4] = (T)(img)(x,y,_p1##z,c)), \
- (I[7] = (T)(img)(x,_n1##y,_p1##z,c)), \
- (I[10] = (T)(img)(x,_p1##y,z,c)), \
- (I[13] = (T)(img)(x,y,z,c)), \
- (I[16] = (T)(img)(x,_n1##y,z,c)), \
- (I[19] = (T)(img)(x,_p1##y,_n1##z,c)), \
- (I[22] = (T)(img)(x,y,_n1##z,c)), \
- (I[25] = (T)(img)(x,_n1##y,_n1##z,c)), \
- x + 1>=(int)(img)._width?(img).width() - 1:x + 1); \
- x<=(int)(x1) && ((_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,_p1##z,c)), \
- (I[5] = (T)(img)(_n1##x,y,_p1##z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,_p1##z,c)), \
- (I[11] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[14] = (T)(img)(_n1##x,y,z,c)), \
- (I[17] = (T)(img)(_n1##x,_n1##y,z,c)), \
- (I[20] = (T)(img)(_n1##x,_p1##y,_n1##z,c)), \
- (I[23] = (T)(img)(_n1##x,y,_n1##z,c)), \
- (I[26] = (T)(img)(_n1##x,_n1##y,_n1##z,c)),1)) || \
- x==--_n1##x); \
- I[0] = I[1], I[1] = I[2], I[3] = I[4], I[4] = I[5], I[6] = I[7], I[7] = I[8], \
- I[9] = I[10], I[10] = I[11], I[12] = I[13], I[13] = I[14], I[15] = I[16], I[16] = I[17], \
- I[18] = I[19], I[19] = I[20], I[21] = I[22], I[22] = I[23], I[24] = I[25], I[25] = I[26], \
- _p1##x = x++, ++_n1##x)
- #define cimglist_for(list,l) for (int l = 0; l<(int)(list)._width; ++l)
- #define cimglist_rof(list,l) for (int l = (int)(list)._width - 1; l>=0; --l)
- #define cimglist_for_in(list,l0,l1,l) \
- for (int l = (int)(l0)<0?0:(int)(l0), _max##l = (unsigned int)l1<(list)._width?(int)(l1):(int)(list)._width - 1; \
- l<=_max##l; ++l)
- #define cimglist_apply(list,fn) cimglist_for(list,__##fn) (list)[__##fn].fn
- // Macros used to display error messages when exceptions are thrown.
- // You should not use these macros is your own code.
- #define _cimgdisplay_instance "[instance(%u,%u,%u,%c%s%c)] CImgDisplay::"
- #define cimgdisplay_instance _width,_height,_normalization,_title?'\"':'[',_title?_title:"untitled",_title?'\"':']'
- #define _cimg_instance "[instance(%u,%u,%u,%u,%p,%sshared)] CImg<%s>::"
- #define cimg_instance _width,_height,_depth,_spectrum,_data,_is_shared?"":"non-",pixel_type()
- #define _cimglist_instance "[instance(%u,%u,%p)] CImgList<%s>::"
- #define cimglist_instance _width,_allocated_width,_data,pixel_type()
- /*------------------------------------------------
- #
- #
- # Define cimg_library:: namespace
- #
- #
- -------------------------------------------------*/
- //! Contains <i>all classes and functions</i> of the \CImg library.
- /**
- This namespace is defined to avoid functions and class names collisions
- that could happen with the inclusion of other C++ header files.
- Anyway, it should not happen often and you should reasonably start most of your
- \CImg-based programs with
- \code
- #include "CImg.h"
- using namespace cimg_library;
- \endcode
- to simplify the declaration of \CImg Library objects afterwards.
- **/
- namespace cimg_library_suffixed {
- // Declare the four classes of the CImg Library.
- template<typename T=float> struct CImg;
- template<typename T=float> struct CImgList;
- struct CImgDisplay;
- struct CImgException;
- // Declare cimg:: namespace.
- // This is an incomplete namespace definition here. It only contains some
- // necessary stuff to ensure a correct declaration order of the classes and functions
- // defined afterwards.
- namespace cimg {
- // Define character sequences for colored terminal output.
- #ifdef cimg_use_vt100
- static const char t_normal[] = { 0x1b, '[', '0', ';', '0', ';', '0', 'm', 0 };
- static const char t_black[] = { 0x1b, '[', '0', ';', '3', '0', ';', '5', '9', 'm', 0 };
- static const char t_red[] = { 0x1b, '[', '0', ';', '3', '1', ';', '5', '9', 'm', 0 };
- static const char t_green[] = { 0x1b, '[', '0', ';', '3', '2', ';', '5', '9', 'm', 0 };
- static const char t_yellow[] = { 0x1b, '[', '0', ';', '3', '3', ';', '5', '9', 'm', 0 };
- static const char t_blue[] = { 0x1b, '[', '0', ';', '3', '4', ';', '5', '9', 'm', 0 };
- static const char t_magenta[] = { 0x1b, '[', '0', ';', '3', '5', ';', '5', '9', 'm', 0 };
- static const char t_cyan[] = { 0x1b, '[', '0', ';', '3', '6', ';', '5', '9', 'm', 0 };
- static const char t_white[] = { 0x1b, '[', '0', ';', '3', '7', ';', '5', '9', 'm', 0 };
- static const char t_bold[] = { 0x1b, '[', '1', 'm', 0 };
- static const char t_underscore[] = { 0x1b, '[', '4', 'm', 0 };
- #else
- static const char t_normal[] = { 0 };
- static const char *const t_black = cimg::t_normal,
- *const t_red = cimg::t_normal,
- *const t_green = cimg::t_normal,
- *const t_yellow = cimg::t_normal,
- *const t_blue = cimg::t_normal,
- *const t_magenta = cimg::t_normal,
- *const t_cyan = cimg::t_normal,
- *const t_white = cimg::t_normal,
- *const t_bold = cimg::t_normal,
- *const t_underscore = cimg::t_normal;
- #endif
- inline std::FILE* output(std::FILE *file=0);
- inline void info();
- //! Avoid warning messages due to unused parameters. Do nothing actually.
- template<typename T>
- inline void unused(const T&, ...) {}
- // [internal] Lock/unlock a mutex for managing concurrent threads.
- // 'lock_mode' can be { 0=unlock | 1=lock | 2=trylock }.
- // 'n' can be in [0,31] but mutex range [0,15] is reserved by CImg.
- inline int mutex(const unsigned int n, const int lock_mode=1);
- inline unsigned int& exception_mode(const unsigned int value, const bool is_set) {
- static unsigned int mode = cimg_verbosity;
- if (is_set) { cimg::mutex(0); mode = value<4?value:4; cimg::mutex(0,0); }
- return mode;
- }
- // Functions to return standard streams 'stdin', 'stdout' and 'stderr'.
- inline FILE* _stdin(const bool throw_exception=true);
- inline FILE* _stdout(const bool throw_exception=true);
- inline FILE* _stderr(const bool throw_exception=true);
- // Mandatory because Microsoft's _snprintf() and _vsnprintf() do not add the '\0' character
- // at the end of the string.
- #if cimg_OS==2 && defined(_MSC_VER)
- inline int _snprintf(char *const s, const size_t size, const char *const format, ...) {
- va_list ap;
- va_start(ap,format);
- const int result = _vsnprintf(s,size,format,ap);
- va_end(ap);
- return result;
- }
- inline int _vsnprintf(char *const s, const size_t size, const char *const format, va_list ap) {
- int result = -1;
- cimg::mutex(6);
- if (size) result = _vsnprintf_s(s,size,_TRUNCATE,format,ap);
- if (result==-1) result = _vscprintf(format,ap);
- cimg::mutex(6,0);
- return result;
- }
- // Mutex-protected version of sscanf, sprintf and snprintf.
- // Used only MacOSX, as it seems those functions are not re-entrant on MacOSX.
- #elif defined(__MACOSX__) || defined(__APPLE__)
- inline int _sscanf(const char *const s, const char *const format, ...) {
- cimg::mutex(6);
- va_list args;
- va_start(args,format);
- const int result = std::vsscanf(s,format,args);
- va_end(args);
- cimg::mutex(6,0);
- return result;
- }
- inline int _sprintf(char *const s, const char *const format, ...) {
- cimg::mutex(6);
- va_list args;
- va_start(args,format);
- const int result = std::vsprintf(s,format,args);
- va_end(args);
- cimg::mutex(6,0);
- return result;
- }
- inline int _snprintf(char *const s, const size_t n, const char *const format, ...) {
- cimg::mutex(6);
- va_list args;
- va_start(args,format);
- const int result = std::vsnprintf(s,n,format,args);
- va_end(args);
- cimg::mutex(6,0);
- return result;
- }
- inline int _vsnprintf(char *const s, const size_t size, const char* format, va_list ap) {
- cimg::mutex(6);
- const int result = std::vsnprintf(s,size,format,ap);
- cimg::mutex(6,0);
- return result;
- }
- #endif
- //! Set current \CImg exception mode.
- /**
- The way error messages are handled by \CImg can be changed dynamically, using this function.
- \param mode Desired exception mode. Possible values are:
- - \c 0: Hide library messages (quiet mode).
- - \c 1: Print library messages on the console.
- - \c 2: Display library messages on a dialog window.
- - \c 3: Do as \c 1 + add extra debug warnings (slow down the code!).
- - \c 4: Do as \c 2 + add extra debug warnings (slow down the code!).
- **/
- inline unsigned int& exception_mode(const unsigned int mode) {
- return exception_mode(mode,true);
- }
- //! Return current \CImg exception mode.
- /**
- \note By default, return the value of configuration macro \c cimg_verbosity
- **/
- inline unsigned int& exception_mode() {
- return exception_mode(0,false);
- }
- inline unsigned int openmp_mode(const unsigned int value, const bool is_set) {
- static unsigned int mode = 2;
- if (is_set) { cimg::mutex(0); mode = value<2?value:2; cimg::mutex(0,0); }
- return mode;
- }
- //! Set current \CImg openmp mode.
- /**
- The way openmp-based methods are handled by \CImg can be changed dynamically, using this function.
- \param mode Desired openmp mode. Possible values are:
- - \c 0: Never parallelize.
- - \c 1: Always parallelize.
- - \c 2: Adaptive parallelization mode (default behavior).
- **/
- inline unsigned int openmp_mode(const unsigned int mode) {
- return openmp_mode(mode,true);
- }
- //! Return current \CImg openmp mode.
- inline unsigned int openmp_mode() {
- return openmp_mode(0,false);
- }
- #ifndef cimg_openmp_sizefactor
- #define cimg_openmp_sizefactor 1
- #endif
- #define cimg_openmp_if(cond) if ((cimg::openmp_mode()==1 || (cimg::openmp_mode()>1 && (cond))))
- #define cimg_openmp_if_size(size,min_size) cimg_openmp_if((size)>=(cimg_openmp_sizefactor)*(min_size))
- #ifdef _MSC_VER
- // Disable 'collapse()' directive for MSVC (supports only OpenMP 2.0).
- #define cimg_openmp_collapse(k)
- #else
- #define cimg_openmp_collapse(k) collapse(k)
- #endif
- #if cimg_OS==2
- // Disable parallelization of simple loops on Windows, due to noticed performance drop.
- #define cimg_openmp_for(instance,expr,min_size) cimg_rof((instance),ptr,T) *ptr = (T)(expr);
- #else
- #define cimg_openmp_for(instance,expr,min_size) \
- cimg_pragma_openmp(parallel for cimg_openmp_if_size((instance).size(),min_size)) \
- cimg_rof((instance),ptr,T) *ptr = (T)(expr);
- #endif
- // Display a simple dialog box, and wait for the user's response.
- inline int dialog(const char *const title, const char *const msg,
- const char *const button1_label="OK", const char *const button2_label=0,
- const char *const button3_label=0, const char *const button4_label=0,
- const char *const button5_label=0, const char *const button6_label=0,
- const bool centering=false);
- // Evaluate math expression.
- inline double eval(const char *const expression,
- const double x=0, const double y=0, const double z=0, const double c=0);
- } // namespace cimg { ...
- /*---------------------------------------
- #
- # Define the CImgException structures
- #
- --------------------------------------*/
- //! Instances of \c CImgException are thrown when errors are encountered in a \CImg function call.
- /**
- \par Overview
- CImgException is the base class of all exceptions thrown by \CImg (except \b CImgAbortException).
- CImgException is never thrown itself. Derived classes that specify the type of errord are thrown instead.
- These classes can be:
- - \b CImgAbortException: Thrown when a computationally-intensive function is aborted by an external signal.
- This is the only \c non-derived exception class.
- - \b CImgArgumentException: Thrown when one argument of a called \CImg function is invalid.
- This is probably one of the most thrown exception by \CImg.
- For instance, the following example throws a \c CImgArgumentException:
- \code
- CImg<float> img(100,100,1,3); // Define a 100x100 color image with float-valued pixels
- img.mirror('e'); // Try to mirror image along the (non-existing) 'e'-axis
- \endcode
- - \b CImgDisplayException: Thrown when something went wrong during the display of images in CImgDisplay instances.
- - \b CImgInstanceException: Thrown when an instance associated to a called \CImg method does not fit
- the function requirements. For instance, the following example throws a \c CImgInstanceException:
- \code
- const CImg<float> img; // Define an empty image
- const float value = img.at(0); // Try to read first pixel value (does not exist)
- \endcode
- - \b CImgIOException: Thrown when an error occurred when trying to load or save image files.
- This happens when trying to read files that do not exist or with invalid formats.
- For instance, the following example throws a \c CImgIOException:
- \code
- const CImg<float> img("missing_file.jpg"); // Try to load a file that does not exist
- \endcode
- - \b CImgWarningException: Thrown only if configuration macro \c cimg_strict_warnings is set, and
- when a \CImg function has to display a warning message (see cimg::warn()).
- It is not recommended to throw CImgException instances by yourself,
- since they are expected to be thrown only by \CImg.
- When an error occurs in a library function call, \CImg may display error messages on the screen or on the
- standard output, depending on the current \CImg exception mode.
- The \CImg exception mode can be get and set by functions cimg::exception_mode() and
- cimg::exception_mode(unsigned int).
- \par Exceptions handling
- In all cases, when an error occurs in \CImg, an instance of the corresponding exception class is thrown.
- This may lead the program to break (this is the default behavior), but you can bypass this behavior by
- handling the exceptions by yourself,
- using a usual <tt>try { ... } catch () { ... }</tt> bloc, as in the following example:
- \code
- #define "CImg.h"
- using namespace cimg_library;
- int main() {
- cimg::exception_mode(0); // Enable quiet exception mode
- try {
- ... // Here, do what you want to stress CImg
- } catch (CImgException& e) { // You succeeded: something went wrong!
- std::fprintf(stderr,"CImg Library Error: %s",e.what()); // Display your custom error message
- ... // Do what you want now to save the ship!
- }
- }
- \endcode
- **/
- struct CImgException : public std::exception {
- #define _cimg_exception_err(etype,disp_flag) \
- std::va_list ap, ap2; \
- va_start(ap,format); va_start(ap2,format); \
- int size = cimg_vsnprintf(0,0,format,ap2); \
- if (size++>=0) { \
- delete[] _message; \
- _message = new char[(size_t)size]; \
- cimg_vsnprintf(_message,(size_t)size,format,ap); \
- if (cimg::exception_mode()) { \
- std::fprintf(cimg::output(),"\n%s[CImg] *** %s ***%s %s\n",cimg::t_red,etype,cimg::t_normal,_message); \
- if (cimg_display && disp_flag && !(cimg::exception_mode()%2)) try { cimg::dialog(etype,_message,"Abort"); } \
- catch (CImgException&) {} \
- if (cimg::exception_mode()>=3) cimg_library_suffixed::cimg::info(); \
- } \
- } \
- va_end(ap); va_end(ap2);
- char *_message;
- CImgException() { _message = new char[1]; *_message = 0; }
- CImgException(const char *const format, ...):_message(0) { _cimg_exception_err("CImgException",true); }
- CImgException(const CImgException& e):std::exception(e) {
- const size_t size = std::strlen(e._message);
- _message = new char[size + 1];
- std::strncpy(_message,e._message,size);
- _message[size] = 0;
- }
- ~CImgException() throw() { delete[] _message; }
- CImgException& operator=(const CImgException& e) {
- const size_t size = std::strlen(e._message);
- _message = new char[size + 1];
- std::strncpy(_message,e._message,size);
- _message[size] = 0;
- return *this;
- }
- //! Return a C-string containing the error message associated to the thrown exception.
- const char *what() const throw() { return _message; }
- }; // struct CImgException { ...
- // The CImgAbortException class is used to throw an exception when
- // a computationally-intensive function has been aborted by an external signal.
- struct CImgAbortException : public std::exception {
- char *_message;
- CImgAbortException() { _message = new char[1]; *_message = 0; }
- CImgAbortException(const char *const format, ...):_message(0) { _cimg_exception_err("CImgAbortException",true); }
- CImgAbortException(const CImgAbortException& e):std::exception(e) {
- const size_t size = std::strlen(e._message);
- _message = new char[size + 1];
- std::strncpy(_message,e._message,size);
- _message[size] = 0;
- }
- ~CImgAbortException() throw() { delete[] _message; }
- CImgAbortException& operator=(const CImgAbortException& e) {
- const size_t size = std::strlen(e._message);
- _message = new char[size + 1];
- std::strncpy(_message,e._message,size);
- _message[size] = 0;
- return *this;
- }
- //! Return a C-string containing the error message associated to the thrown exception.
- const char *what() const throw() { return _message; }
- }; // struct CImgAbortException { ...
- // The CImgArgumentException class is used to throw an exception related
- // to invalid arguments encountered in a library function call.
- struct CImgArgumentException : public CImgException {
- CImgArgumentException(const char *const format, ...) { _cimg_exception_err("CImgArgumentException",true); }
- }; // struct CImgArgumentException { ...
- // The CImgDisplayException class is used to throw an exception related
- // to display problems encountered in a library function call.
- struct CImgDisplayException : public CImgException {
- CImgDisplayException(const char *const format, ...) { _cimg_exception_err("CImgDisplayException",false); }
- }; // struct CImgDisplayException { ...
- // The CImgInstanceException class is used to throw an exception related
- // to an invalid instance encountered in a library function call.
- struct CImgInstanceException : public CImgException {
- CImgInstanceException(const char *const format, ...) { _cimg_exception_err("CImgInstanceException",true); }
- }; // struct CImgInstanceException { ...
- // The CImgIOException class is used to throw an exception related
- // to input/output file problems encountered in a library function call.
- struct CImgIOException : public CImgException {
- CImgIOException(const char *const format, ...) { _cimg_exception_err("CImgIOException",true); }
- }; // struct CImgIOException { ...
- // The CImgWarningException class is used to throw an exception for warnings
- // encountered in a library function call.
- struct CImgWarningException : public CImgException {
- CImgWarningException(const char *const format, ...) { _cimg_exception_err("CImgWarningException",false); }
- }; // struct CImgWarningException { ...
- /*-------------------------------------
- #
- # Define cimg:: namespace
- #
- -----------------------------------*/
- //! Contains \a low-level functions and variables of the \CImg Library.
- /**
- Most of the functions and variables within this namespace are used by the \CImg library for low-level operations.
- You may use them to access specific const values or environment variables internally used by \CImg.
- \warning Never write <tt>using namespace cimg_library::cimg;</tt> in your source code. Lot of functions in the
- <tt>cimg:: namespace</tt> have the same names as standard C functions that may be defined in the global
- namespace <tt>::</tt>.
- **/
- namespace cimg {
- // Define traits that will be used to determine the best data type to work in CImg functions.
- //
- template<typename T> struct type {
- static const char* string() {
- static const char* s[] = { "unknown", "unknown8", "unknown16", "unknown24",
- "unknown32", "unknown40", "unknown48", "unknown56",
- "unknown64", "unknown72", "unknown80", "unknown88",
- "unknown96", "unknown104", "unknown112", "unknown120",
- "unknown128" };
- return s[(sizeof(T)<17)?sizeof(T):0];
- }
- static bool is_float() { return false; }
- static bool is_inf(const T) { return false; }
- static bool is_nan(const T) { return false; }
- static bool is_finite(const T) { return true; }
- static T min() { return ~max(); }
- static T max() { return (T)1<<(8*sizeof(T) - 1); }
- static T inf() { return max(); }
- static T cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(T)val; }
- static const char* format() { return "%s"; }
- static const char* format_s() { return "%s"; }
- static const char* format(const T& val) { static const char *const s = "unknown"; cimg::unused(val); return s; }
- };
- template<> struct type<bool> {
- static const char* string() { static const char *const s = "bool"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const bool) { return false; }
- static bool is_nan(const bool) { return false; }
- static bool is_finite(const bool) { return true; }
- static bool min() { return false; }
- static bool max() { return true; }
- static bool inf() { return max(); }
- static bool is_inf() { return false; }
- static bool cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(bool)val; }
- static const char* format() { return "%s"; }
- static const char* format_s() { return "%s"; }
- static const char* format(const bool val) { static const char* s[] = { "false", "true" }; return s[val?1:0]; }
- };
- template<> struct type<unsigned char> {
- static const char* string() { static const char *const s = "unsigned char"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const unsigned char) { return false; }
- static bool is_nan(const unsigned char) { return false; }
- static bool is_finite(const unsigned char) { return true; }
- static unsigned char min() { return 0; }
- static unsigned char max() { return (unsigned char)-1; }
- static unsigned char inf() { return max(); }
- static unsigned char cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
- static const char* format() { return "%u"; }
- static const char* format_s() { return "%u"; }
- static unsigned int format(const unsigned char val) { return (unsigned int)val; }
- };
- #if defined(CHAR_MAX) && CHAR_MAX==255
- template<> struct type<char> {
- static const char* string() { static const char *const s = "char"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const char) { return false; }
- static bool is_nan(const char) { return false; }
- static bool is_finite(const char) { return true; }
- static char min() { return 0; }
- static char max() { return (char)-1; }
- static char inf() { return max(); }
- static char cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(unsigned char)val; }
- static const char* format() { return "%u"; }
- static const char* format_s() { return "%u"; }
- static unsigned int format(const char val) { return (unsigned int)val; }
- };
- #else
- template<> struct type<char> {
- static const char* string() { static const char *const s = "char"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const char) { return false; }
- static bool is_nan(const char) { return false; }
- static bool is_finite(const char) { return true; }
- static char min() { return ~max(); }
- static char max() { return (char)((unsigned char)-1>>1); }
- static char inf() { return max(); }
- static char cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(char)val; }
- static const char* format() { return "%d"; }
- static const char* format_s() { return "%d"; }
- static int format(const char val) { return (int)val; }
- };
- #endif
- template<> struct type<signed char> {
- static const char* string() { static const char *const s = "signed char"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const signed char) { return false; }
- static bool is_nan(const signed char) { return false; }
- static bool is_finite(const signed char) { return true; }
- static signed char min() { return ~max(); }
- static signed char max() { return (signed char)((unsigned char)-1>>1); }
- static signed char inf() { return max(); }
- static signed char cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(signed char)val; }
- static const char* format() { return "%d"; }
- static const char* format_s() { return "%d"; }
- static int format(const signed char val) { return (int)val; }
- };
- template<> struct type<unsigned short> {
- static const char* string() { static const char *const s = "unsigned short"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const unsigned short) { return false; }
- static bool is_nan(const unsigned short) { return false; }
- static bool is_finite(const unsigned short) { return true; }
- static unsigned short min() { return 0; }
- static unsigned short max() { return (unsigned short)-1; }
- static unsigned short inf() { return max(); }
- static unsigned short cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(unsigned short)val; }
- static const char* format() { return "%u"; }
- static const char* format_s() { return "%u"; }
- static unsigned int format(const unsigned short val) { return (unsigned int)val; }
- };
- template<> struct type<short> {
- static const char* string() { static const char *const s = "short"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const short) { return false; }
- static bool is_nan(const short) { return false; }
- static bool is_finite(const short) { return true; }
- static short min() { return ~max(); }
- static short max() { return (short)((unsigned short)-1>>1); }
- static short inf() { return max(); }
- static short cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(short)val; }
- static const char* format() { return "%d"; }
- static const char* format_s() { return "%d"; }
- static int format(const short val) { return (int)val; }
- };
- template<> struct type<unsigned int> {
- static const char* string() { static const char *const s = "unsigned int"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const unsigned int) { return false; }
- static bool is_nan(const unsigned int) { return false; }
- static bool is_finite(const unsigned int) { return true; }
- static unsigned int min() { return 0; }
- static unsigned int max() { return (unsigned int)-1; }
- static unsigned int inf() { return max(); }
- static unsigned int cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(unsigned int)val; }
- static const char* format() { return "%u"; }
- static const char* format_s() { return "%u"; }
- static unsigned int format(const unsigned int val) { return val; }
- };
- template<> struct type<int> {
- static const char* string() { static const char *const s = "int"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const int) { return false; }
- static bool is_nan(const int) { return false; }
- static bool is_finite(const int) { return true; }
- static int min() { return ~max(); }
- static int max() { return (int)(~0U>>1); }
- static int inf() { return max(); }
- static int cut(const double val) { return val<(double)min()?min():val>(double)max()?max():(int)val; }
- static const char* format() { return "%d"; }
- static const char* format_s() { return "%d"; }
- static int format(const int val) { return val; }
- };
- template<> struct type<cimg_uint64> {
- static const char* string() { static const char *const s = "unsigned int64"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const cimg_uint64) { return false; }
- static bool is_nan(const cimg_uint64) { return false; }
- static bool is_finite(const cimg_uint64) { return true; }
- static cimg_uint64 min() { return 0; }
- static cimg_uint64 max() { return (cimg_uint64)-1; }
- static cimg_uint64 inf() { return max(); }
- static cimg_uint64 cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(cimg_uint64)val; }
- static const char* format() { return cimg_fuint64; }
- static const char* format_s() { return cimg_fuint64; }
- static cimg_uint64 format(const cimg_uint64 val) { return val; }
- };
- template<> struct type<cimg_int64> {
- static const char* string() { static const char *const s = "int64"; return s; }
- static bool is_float() { return false; }
- static bool is_inf(const cimg_int64) { return false; }
- static bool is_nan(const cimg_int64) { return false; }
- static bool is_finite(const cimg_int64) { return true; }
- static cimg_int64 min() { return ~max(); }
- static cimg_int64 max() { return (cimg_int64)((cimg_uint64)-1>>1); }
- static cimg_int64 inf() { return max(); }
- static cimg_int64 cut(const double val) {
- return val<(double)min()?min():val>(double)max()?max():(cimg_int64)val;
- }
- static const char* format() { return cimg_fint64; }
- static const char* format_s() { return cimg_fint64; }
- static long format(const long val) { return (long)val; }
- };
- template<> struct type<double> {
- static const char* string() { static const char *const s = "double"; return s; }
- static bool is_float() { return true; }
- static bool is_inf(const double val) {
- #ifdef isinf
- return (bool)isinf(val);
- #else
- return !is_nan(val) && (val<cimg::type<double>::min() || val>cimg::type<double>::max());
- #endif
- }
- static bool is_nan(const double val) { // Custom version that works with '-ffast-math'
- if (sizeof(double)==8) {
- cimg_uint64 u;
- std::memcpy(&u,&val,sizeof(double));
- return ((unsigned int)(u>>32)&0x7fffffff) + ((unsigned int)u!=0)>0x7ff00000;
- }
- #ifdef isnan
- return (bool)isnan(val);
- #else
- return !(val==val);
- #endif
- }
- static bool is_finite(const double val) {
- #ifdef isfinite
- return (bool)isfinite(val);
- #else
- return !is_nan(val) && !is_inf(val);
- #endif
- }
- static double min() { return -DBL_MAX; }
- static double max() { return DBL_MAX; }
- static double inf() {
- #ifdef INFINITY
- return (double)INFINITY;
- #else
- return max()*max();
- #endif
- }
- static double nan() {
- #ifdef NAN
- return (double)NAN;
- #else
- const double val_nan = -std::sqrt(-1.); return val_nan;
- #endif
- }
- static double cut(const double val) { return val; }
- static const char* format() { return "%.17g"; }
- static const char* format_s() { return "%g"; }
- static double format(const double val) { return val; }
- };
- template<> struct type<float> {
- static const char* string() { static const char *const s = "float"; return s; }
- static bool is_float() { return true; }
- static bool is_inf(const float val) {
- #ifdef isinf
- return (bool)isinf(val);
- #else
- return !is_nan(val) && (val<cimg::type<float>::min() || val>cimg::type<float>::max());
- #endif
- }
- static bool is_nan(const float val) { // Custom version that works with '-ffast-math'
- if (sizeof(float)==4) {
- unsigned int u;
- std::memcpy(&u,&val,sizeof(float));
- return (u&0x7fffffff)>0x7f800000;
- }
- #ifdef isnan
- return (bool)isnan(val);
- #else
- return !(val==val);
- #endif
- }
- static bool is_finite(const float val) {
- #ifdef isfinite
- return (bool)isfinite(val);
- #else
- return !is_nan(val) && !is_inf(val);
- #endif
- }
- static float min() { return -FLT_MAX; }
- static float max() { return FLT_MAX; }
- static float inf() { return (float)cimg::type<double>::inf(); }
- static float nan() { return (float)cimg::type<double>::nan(); }
- static float cut(const double val) { return (float)val; }
- static float cut(const float val) { return (float)val; }
- static const char* format() { return "%.9g"; }
- static const char* format_s() { return "%g"; }
- static double format(const float val) { return (double)val; }
- };
- template<> struct type<long double> {
- static const char* string() { static const char *const s = "long double"; return s; }
- static bool is_float() { return true; }
- static bool is_inf(const long double val) {
- #ifdef isinf
- return (bool)isinf(val);
- #else
- return !is_nan(val) && (val<cimg::type<long double>::min() || val>cimg::type<long double>::max());
- #endif
- }
- static bool is_nan(const long double val) {
- #ifdef isnan
- return (bool)isnan(val);
- #else
- return !(val==val);
- #endif
- }
- static bool is_finite(const long double val) {
- #ifdef isfinite
- return (bool)isfinite(val);
- #else
- return !is_nan(val) && !is_inf(val);
- #endif
- }
- static long double min() { return -LDBL_MAX; }
- static long double max() { return LDBL_MAX; }
- static long double inf() { return max()*max(); }
- static long double nan() { const long double val_nan = -std::sqrt(-1.L); return val_nan; }
- static long double cut(const long double val) { return val; }
- static const char* format() { return "%.17g"; }
- static const char* format_s() { return "%g"; }
- static double format(const long double val) { return (double)val; }
- };
- #ifdef cimg_use_half
- template<> struct type<half> {
- static const char* string() { static const char *const s = "half"; return s; }
- static bool is_float() { return true; }
- static bool is_inf(const long double val) {
- #ifdef isinf
- return (bool)isinf(val);
- #else
- return !is_nan(val) && (val<cimg::type<half>::min() || val>cimg::type<half>::max());
- #endif
- }
- static bool is_nan(const half val) { // Custom version that works with '-ffast-math'
- if (sizeof(half)==2) {
- short u;
- std::memcpy(&u,&val,sizeof(short));
- return (bool)((u&0x7fff)>0x7c00);
- }
- return cimg::type<float>::is_nan((float)val);
- }
- static bool is_finite(const half val) {
- #ifdef isfinite
- return (bool)isfinite(val);
- #else
- return !is_nan(val) && !is_inf(val);
- #endif
- }
- static half min() { return (half)-65504; }
- static half max() { return (half)65504; }
- static half inf() { return max()*max(); }
- static half nan() { const half val_nan = (half)-std::sqrt(-1.); return val_nan; }
- static half cut(const double val) { return (half)val; }
- static const char* format() { return "%.9g"; }
- static const char* format_s() { return "%g"; }
- static double format(const half val) { return (double)val; }
- };
- #endif
- template<typename T, typename t> struct superset { typedef T type; };
- template<> struct superset<bool,unsigned char> { typedef unsigned char type; };
- template<> struct superset<bool,char> { typedef char type; };
- template<> struct superset<bool,signed char> { typedef signed char type; };
- template<> struct superset<bool,unsigned short> { typedef unsigned short type; };
- template<> struct superset<bool,short> { typedef short type; };
- template<> struct superset<bool,unsigned int> { typedef unsigned int type; };
- template<> struct superset<bool,int> { typedef int type; };
- template<> struct superset<bool,cimg_uint64> { typedef cimg_uint64 type; };
- template<> struct superset<bool,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<bool,float> { typedef float type; };
- template<> struct superset<bool,double> { typedef double type; };
- template<> struct superset<unsigned char,char> { typedef short type; };
- template<> struct superset<unsigned char,signed char> { typedef short type; };
- template<> struct superset<unsigned char,unsigned short> { typedef unsigned short type; };
- template<> struct superset<unsigned char,short> { typedef short type; };
- template<> struct superset<unsigned char,unsigned int> { typedef unsigned int type; };
- template<> struct superset<unsigned char,int> { typedef int type; };
- template<> struct superset<unsigned char,cimg_uint64> { typedef cimg_uint64 type; };
- template<> struct superset<unsigned char,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<unsigned char,float> { typedef float type; };
- template<> struct superset<unsigned char,double> { typedef double type; };
- template<> struct superset<signed char,unsigned char> { typedef short type; };
- template<> struct superset<signed char,char> { typedef short type; };
- template<> struct superset<signed char,unsigned short> { typedef int type; };
- template<> struct superset<signed char,short> { typedef short type; };
- template<> struct superset<signed char,unsigned int> { typedef cimg_int64 type; };
- template<> struct superset<signed char,int> { typedef int type; };
- template<> struct superset<signed char,cimg_uint64> { typedef cimg_int64 type; };
- template<> struct superset<signed char,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<signed char,float> { typedef float type; };
- template<> struct superset<signed char,double> { typedef double type; };
- template<> struct superset<char,unsigned char> { typedef short type; };
- template<> struct superset<char,signed char> { typedef short type; };
- template<> struct superset<char,unsigned short> { typedef int type; };
- template<> struct superset<char,short> { typedef short type; };
- template<> struct superset<char,unsigned int> { typedef cimg_int64 type; };
- template<> struct superset<char,int> { typedef int type; };
- template<> struct superset<char,cimg_uint64> { typedef cimg_int64 type; };
- template<> struct superset<char,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<char,float> { typedef float type; };
- template<> struct superset<char,double> { typedef double type; };
- template<> struct superset<unsigned short,char> { typedef int type; };
- template<> struct superset<unsigned short,signed char> { typedef int type; };
- template<> struct superset<unsigned short,short> { typedef int type; };
- template<> struct superset<unsigned short,unsigned int> { typedef unsigned int type; };
- template<> struct superset<unsigned short,int> { typedef int type; };
- template<> struct superset<unsigned short,cimg_uint64> { typedef cimg_uint64 type; };
- template<> struct superset<unsigned short,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<unsigned short,float> { typedef float type; };
- template<> struct superset<unsigned short,double> { typedef double type; };
- template<> struct superset<short,unsigned short> { typedef int type; };
- template<> struct superset<short,unsigned int> { typedef cimg_int64 type; };
- template<> struct superset<short,int> { typedef int type; };
- template<> struct superset<short,cimg_uint64> { typedef cimg_int64 type; };
- template<> struct superset<short,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<short,float> { typedef float type; };
- template<> struct superset<short,double> { typedef double type; };
- template<> struct superset<unsigned int,char> { typedef cimg_int64 type; };
- template<> struct superset<unsigned int,signed char> { typedef cimg_int64 type; };
- template<> struct superset<unsigned int,short> { typedef cimg_int64 type; };
- template<> struct superset<unsigned int,int> { typedef cimg_int64 type; };
- template<> struct superset<unsigned int,cimg_uint64> { typedef cimg_uint64 type; };
- template<> struct superset<unsigned int,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<unsigned int,float> { typedef float type; };
- template<> struct superset<unsigned int,double> { typedef double type; };
- template<> struct superset<int,unsigned int> { typedef cimg_int64 type; };
- template<> struct superset<int,cimg_uint64> { typedef cimg_int64 type; };
- template<> struct superset<int,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<int,float> { typedef float type; };
- template<> struct superset<int,double> { typedef double type; };
- template<> struct superset<cimg_uint64,char> { typedef cimg_int64 type; };
- template<> struct superset<cimg_uint64,signed char> { typedef cimg_int64 type; };
- template<> struct superset<cimg_uint64,short> { typedef cimg_int64 type; };
- template<> struct superset<cimg_uint64,int> { typedef cimg_int64 type; };
- template<> struct superset<cimg_uint64,cimg_int64> { typedef cimg_int64 type; };
- template<> struct superset<cimg_uint64,float> { typedef double type; };
- template<> struct superset<cimg_uint64,double> { typedef double type; };
- template<> struct superset<cimg_int64,float> { typedef double type; };
- template<> struct superset<cimg_int64,double> { typedef double type; };
- template<> struct superset<float,cimg_uint64> { typedef double type; };
- template<> struct superset<float,cimg_int64> { typedef double type; };
- template<> struct superset<float,double> { typedef double type; };
- #ifdef cimg_use_half
- template<> struct superset<half,unsigned short> { typedef float type; };
- template<> struct superset<half,short> { typedef float type; };
- template<> struct superset<half,unsigned int> { typedef float type; };
- template<> struct superset<half,int> { typedef float type; };
- template<> struct superset<half,cimg_uint64> { typedef float type; };
- template<> struct superset<half,cimg_int64> { typedef float type; };
- template<> struct superset<half,float> { typedef float type; };
- template<> struct superset<half,double> { typedef double type; };
- #endif
- template<typename t1, typename t2, typename t3> struct superset2 {
- typedef typename superset<t1, typename superset<t2,t3>::type>::type type;
- };
- template<typename t1, typename t2, typename t3, typename t4> struct superset3 {
- typedef typename superset<t1, typename superset2<t2,t3,t4>::type>::type type;
- };
- template<typename t1, typename t2> struct last { typedef t2 type; };
- #define _cimg_Tt typename cimg::superset<T,t>::type
- #define _cimg_Tfloat typename cimg::superset<T,float>::type
- #define _cimg_tfloat typename cimg::superset<t,float>::type
- #define _cimg_Ttfloat typename cimg::superset2<T,t,float>::type
- #define _cimg_Ttdouble typename cimg::superset2<T,t,double>::type
- // Define variables used internally by CImg.
- #if cimg_display==1
- struct X11_static {
- unsigned int nb_wins;
- pthread_t *events_thread;
- pthread_cond_t wait_event;
- pthread_mutex_t wait_event_mutex;
- CImgDisplay **wins;
- Display *display;
- unsigned int nb_bits;
- bool is_blue_first;
- bool is_shm_enabled;
- bool byte_order;
- #ifdef cimg_use_xrandr
- XRRScreenSize *resolutions;
- Rotation curr_rotation;
- unsigned int curr_resolution;
- unsigned int nb_resolutions;
- #endif
- X11_static():nb_wins(0),events_thread(0),display(0),
- nb_bits(0),is_blue_first(false),is_shm_enabled(false),byte_order(false) {
- #ifdef __FreeBSD__
- XInitThreads();
- #endif
- wins = new CImgDisplay*[1024];
- pthread_mutex_init(&wait_event_mutex,0);
- pthread_cond_init(&wait_event,0);
- #ifdef cimg_use_xrandr
- resolutions = 0;
- curr_rotation = 0;
- curr_resolution = nb_resolutions = 0;
- #endif
- }
- ~X11_static() {
- delete[] wins;
- /*
- if (events_thread) {
- pthread_cancel(*events_thread);
- delete events_thread;
- }
- if (display) { } // XCloseDisplay(display); }
- pthread_cond_destroy(&wait_event);
- pthread_mutex_unlock(&wait_event_mutex);
- pthread_mutex_destroy(&wait_event_mutex);
- */
- }
- }; // struct X11_static { ...
- #if defined(cimg_module)
- X11_static& X11_attr();
- #elif defined(cimg_main)
- X11_static& X11_attr() { static X11_static val; return val; }
- #else
- inline X11_static& X11_attr() { static X11_static val; return val; }
- #endif
- #elif cimg_display==2
- struct Win32_static {
- HANDLE wait_event;
- Win32_static() { wait_event = CreateEvent(0,FALSE_WIN,FALSE_WIN,0); }
- }; // struct Win32_static { ...
- #if defined(cimg_module)
- Win32_static& Win32_attr();
- #elif defined(cimg_main)
- Win32_static& Win32_attr() { static Win32_static val; return val; }
- #else
- inline Win32_static& Win32_attr() { static Win32_static val; return val; }
- #endif
- #endif
- #define cimg_lock_display() cimg::mutex(15)
- #define cimg_unlock_display() cimg::mutex(15,0)
- struct Mutex_static {
- #if cimg_OS==1 && (defined(cimg_use_pthread) || cimg_display==1)
- pthread_mutex_t mutex[32];
- Mutex_static() { for (unsigned int i = 0; i<32; ++i) pthread_mutex_init(&mutex[i],0); }
- void lock(const unsigned int n) { pthread_mutex_lock(&mutex[n]); }
- void unlock(const unsigned int n) { pthread_mutex_unlock(&mutex[n]); }
- int trylock(const unsigned int n) { return pthread_mutex_trylock(&mutex[n]); }
- #elif cimg_OS==2
- HANDLE mutex[32];
- Mutex_static() { for (unsigned int i = 0; i<32; ++i) mutex[i] = CreateMutex(0,FALSE_WIN,0); }
- void lock(const unsigned int n) { WaitForSingleObject(mutex[n],INFINITE); }
- void unlock(const unsigned int n) { ReleaseMutex(mutex[n]); }
- int trylock(const unsigned int) { return 0; }
- #else
- Mutex_static() {}
- void lock(const unsigned int) {}
- void unlock(const unsigned int) {}
- int trylock(const unsigned int) { return 0; }
- #endif
- }; // struct Mutex_static { ...
- #if defined(cimg_module)
- Mutex_static& Mutex_attr();
- #elif defined(cimg_main)
- Mutex_static& Mutex_attr() { static Mutex_static val; return val; }
- #else
- inline Mutex_static& Mutex_attr() { static Mutex_static val; return val; }
- #endif
- #if defined(cimg_use_magick)
- struct Magick_static {
- Magick_static() {
- Magick::InitializeMagick("");
- }
- }; // struct Magick_static { ...
- static Magick_static _Magick_static;
- #endif
- #if defined(cimg_use_fftw3) && !defined(cimg_use_fftw3_singlethread)
- struct FFTW3_static {
- FFTW3_static() {
- fftw_init_threads();
- }
- }; // struct FFTW3_static { ...
- static FFTW3_static _FFTW3_static;
- #endif
- #if cimg_display==1
- // Define keycodes for X11-based graphical systems.
- const unsigned int keyESC = XK_Escape;
- const unsigned int keyF1 = XK_F1;
- const unsigned int keyF2 = XK_F2;
- const unsigned int keyF3 = XK_F3;
- const unsigned int keyF4 = XK_F4;
- const unsigned int keyF5 = XK_F5;
- const unsigned int keyF6 = XK_F6;
- const unsigned int keyF7 = XK_F7;
- const unsigned int keyF8 = XK_F8;
- const unsigned int keyF9 = XK_F9;
- const unsigned int keyF10 = XK_F10;
- const unsigned int keyF11 = XK_F11;
- const unsigned int keyF12 = XK_F12;
- const unsigned int keyPAUSE = XK_Pause;
- const unsigned int key1 = XK_1;
- const unsigned int key2 = XK_2;
- const unsigned int key3 = XK_3;
- const unsigned int key4 = XK_4;
- const unsigned int key5 = XK_5;
- const unsigned int key6 = XK_6;
- const unsigned int key7 = XK_7;
- const unsigned int key8 = XK_8;
- const unsigned int key9 = XK_9;
- const unsigned int key0 = XK_0;
- const unsigned int keyBACKSPACE = XK_BackSpace;
- const unsigned int keyINSERT = XK_Insert;
- const unsigned int keyHOME = XK_Home;
- const unsigned int keyPAGEUP = XK_Page_Up;
- const unsigned int keyTAB = XK_Tab;
- const unsigned int keyQ = XK_q;
- const unsigned int keyW = XK_w;
- const unsigned int keyE = XK_e;
- const unsigned int keyR = XK_r;
- const unsigned int keyT = XK_t;
- const unsigned int keyY = XK_y;
- const unsigned int keyU = XK_u;
- const unsigned int keyI = XK_i;
- const unsigned int keyO = XK_o;
- const unsigned int keyP = XK_p;
- const unsigned int keyDELETE = XK_Delete;
- const unsigned int keyEND = XK_End;
- const unsigned int keyPAGEDOWN = XK_Page_Down;
- const unsigned int keyCAPSLOCK = XK_Caps_Lock;
- const unsigned int keyA = XK_a;
- const unsigned int keyS = XK_s;
- const unsigned int keyD = XK_d;
- const unsigned int keyF = XK_f;
- const unsigned int keyG = XK_g;
- const unsigned int keyH = XK_h;
- const unsigned int keyJ = XK_j;
- const unsigned int keyK = XK_k;
- const unsigned int keyL = XK_l;
- const unsigned int keyENTER = XK_Return;
- const unsigned int keySHIFTLEFT = XK_Shift_L;
- const unsigned int keyZ = XK_z;
- const unsigned int keyX = XK_x;
- const unsigned int keyC = XK_c;
- const unsigned int keyV = XK_v;
- const unsigned int keyB = XK_b;
- const unsigned int keyN = XK_n;
- const unsigned int keyM = XK_m;
- const unsigned int keySHIFTRIGHT = XK_Shift_R;
- const unsigned int keyARROWUP = XK_Up;
- const unsigned int keyCTRLLEFT = XK_Control_L;
- const unsigned int keyAPPLEFT = XK_Super_L;
- const unsigned int keyALT = XK_Alt_L;
- const unsigned int keySPACE = XK_space;
- const unsigned int keyALTGR = XK_Alt_R;
- const unsigned int keyAPPRIGHT = XK_Super_R;
- const unsigned int keyMENU = XK_Menu;
- const unsigned int keyCTRLRIGHT = XK_Control_R;
- const unsigned int keyARROWLEFT = XK_Left;
- const unsigned int keyARROWDOWN = XK_Down;
- const unsigned int keyARROWRIGHT = XK_Right;
- const unsigned int keyPAD0 = XK_KP_0;
- const unsigned int keyPAD1 = XK_KP_1;
- const unsigned int keyPAD2 = XK_KP_2;
- const unsigned int keyPAD3 = XK_KP_3;
- const unsigned int keyPAD4 = XK_KP_4;
- const unsigned int keyPAD5 = XK_KP_5;
- const unsigned int keyPAD6 = XK_KP_6;
- const unsigned int keyPAD7 = XK_KP_7;
- const unsigned int keyPAD8 = XK_KP_8;
- const unsigned int keyPAD9 = XK_KP_9;
- const unsigned int keyPADADD = XK_KP_Add;
- const unsigned int keyPADSUB = XK_KP_Subtract;
- const unsigned int keyPADMUL = XK_KP_Multiply;
- const unsigned int keyPADDIV = XK_KP_Divide;
- #elif cimg_display==2
- // Define keycodes for Windows.
- const unsigned int keyESC = VK_ESCAPE;
- const unsigned int keyF1 = VK_F1;
- const unsigned int keyF2 = VK_F2;
- const unsigned int keyF3 = VK_F3;
- const unsigned int keyF4 = VK_F4;
- const unsigned int keyF5 = VK_F5;
- const unsigned int keyF6 = VK_F6;
- const unsigned int keyF7 = VK_F7;
- const unsigned int keyF8 = VK_F8;
- const unsigned int keyF9 = VK_F9;
- const unsigned int keyF10 = VK_F10;
- const unsigned int keyF11 = VK_F11;
- const unsigned int keyF12 = VK_F12;
- const unsigned int keyPAUSE = VK_PAUSE;
- const unsigned int key1 = '1';
- const unsigned int key2 = '2';
- const unsigned int key3 = '3';
- const unsigned int key4 = '4';
- const unsigned int key5 = '5';
- const unsigned int key6 = '6';
- const unsigned int key7 = '7';
- const unsigned int key8 = '8';
- const unsigned int key9 = '9';
- const unsigned int key0 = '0';
- const unsigned int keyBACKSPACE = VK_BACK;
- const unsigned int keyINSERT = VK_INSERT;
- const unsigned int keyHOME = VK_HOME;
- const unsigned int keyPAGEUP = VK_PRIOR;
- const unsigned int keyTAB = VK_TAB;
- const unsigned int keyQ = 'Q';
- const unsigned int keyW = 'W';
- const unsigned int keyE = 'E';
- const unsigned int keyR = 'R';
- const unsigned int keyT = 'T';
- const unsigned int keyY = 'Y';
- const unsigned int keyU = 'U';
- const unsigned int keyI = 'I';
- const unsigned int keyO = 'O';
- const unsigned int keyP = 'P';
- const unsigned int keyDELETE = VK_DELETE;
- const unsigned int keyEND = VK_END;
- const unsigned int keyPAGEDOWN = VK_NEXT;
- const unsigned int keyCAPSLOCK = VK_CAPITAL;
- const unsigned int keyA = 'A';
- const unsigned int keyS = 'S';
- const unsigned int keyD = 'D';
- const unsigned int keyF = 'F';
- const unsigned int keyG = 'G';
- const unsigned int keyH = 'H';
- const unsigned int keyJ = 'J';
- const unsigned int keyK = 'K';
- const unsigned int keyL = 'L';
- const unsigned int keyENTER = VK_RETURN;
- const unsigned int keySHIFTLEFT = VK_SHIFT;
- const unsigned int keyZ = 'Z';
- const unsigned int keyX = 'X';
- const unsigned int keyC = 'C';
- const unsigned int keyV = 'V';
- const unsigned int keyB = 'B';
- const unsigned int keyN = 'N';
- const unsigned int keyM = 'M';
- const unsigned int keySHIFTRIGHT = VK_SHIFT;
- const unsigned int keyARROWUP = VK_UP;
- const unsigned int keyCTRLLEFT = VK_CONTROL;
- const unsigned int keyAPPLEFT = VK_LWIN;
- const unsigned int keyALT = VK_LMENU;
- const unsigned int keySPACE = VK_SPACE;
- const unsigned int keyALTGR = VK_CONTROL;
- const unsigned int keyAPPRIGHT = VK_RWIN;
- const unsigned int keyMENU = VK_APPS;
- const unsigned int keyCTRLRIGHT = VK_CONTROL;
- const unsigned int keyARROWLEFT = VK_LEFT;
- const unsigned int keyARROWDOWN = VK_DOWN;
- const unsigned int keyARROWRIGHT = VK_RIGHT;
- const unsigned int keyPAD0 = 0x60;
- const unsigned int keyPAD1 = 0x61;
- const unsigned int keyPAD2 = 0x62;
- const unsigned int keyPAD3 = 0x63;
- const unsigned int keyPAD4 = 0x64;
- const unsigned int keyPAD5 = 0x65;
- const unsigned int keyPAD6 = 0x66;
- const unsigned int keyPAD7 = 0x67;
- const unsigned int keyPAD8 = 0x68;
- const unsigned int keyPAD9 = 0x69;
- const unsigned int keyPADADD = VK_ADD;
- const unsigned int keyPADSUB = VK_SUBTRACT;
- const unsigned int keyPADMUL = VK_MULTIPLY;
- const unsigned int keyPADDIV = VK_DIVIDE;
- #else
- // Define random keycodes when no display is available.
- // (should rarely be used then!).
- const unsigned int keyESC = 1U; //!< Keycode for the \c ESC key (architecture-dependent)
- const unsigned int keyF1 = 2U; //!< Keycode for the \c F1 key (architecture-dependent)
- const unsigned int keyF2 = 3U; //!< Keycode for the \c F2 key (architecture-dependent)
- const unsigned int keyF3 = 4U; //!< Keycode for the \c F3 key (architecture-dependent)
- const unsigned int keyF4 = 5U; //!< Keycode for the \c F4 key (architecture-dependent)
- const unsigned int keyF5 = 6U; //!< Keycode for the \c F5 key (architecture-dependent)
- const unsigned int keyF6 = 7U; //!< Keycode for the \c F6 key (architecture-dependent)
- const unsigned int keyF7 = 8U; //!< Keycode for the \c F7 key (architecture-dependent)
- const unsigned int keyF8 = 9U; //!< Keycode for the \c F8 key (architecture-dependent)
- const unsigned int keyF9 = 10U; //!< Keycode for the \c F9 key (architecture-dependent)
- const unsigned int keyF10 = 11U; //!< Keycode for the \c F10 key (architecture-dependent)
- const unsigned int keyF11 = 12U; //!< Keycode for the \c F11 key (architecture-dependent)
- const unsigned int keyF12 = 13U; //!< Keycode for the \c F12 key (architecture-dependent)
- const unsigned int keyPAUSE = 14U; //!< Keycode for the \c PAUSE key (architecture-dependent)
- const unsigned int key1 = 15U; //!< Keycode for the \c 1 key (architecture-dependent)
- const unsigned int key2 = 16U; //!< Keycode for the \c 2 key (architecture-dependent)
- const unsigned int key3 = 17U; //!< Keycode for the \c 3 key (architecture-dependent)
- const unsigned int key4 = 18U; //!< Keycode for the \c 4 key (architecture-dependent)
- const unsigned int key5 = 19U; //!< Keycode for the \c 5 key (architecture-dependent)
- const unsigned int key6 = 20U; //!< Keycode for the \c 6 key (architecture-dependent)
- const unsigned int key7 = 21U; //!< Keycode for the \c 7 key (architecture-dependent)
- const unsigned int key8 = 22U; //!< Keycode for the \c 8 key (architecture-dependent)
- const unsigned int key9 = 23U; //!< Keycode for the \c 9 key (architecture-dependent)
- const unsigned int key0 = 24U; //!< Keycode for the \c 0 key (architecture-dependent)
- const unsigned int keyBACKSPACE = 25U; //!< Keycode for the \c BACKSPACE key (architecture-dependent)
- const unsigned int keyINSERT = 26U; //!< Keycode for the \c INSERT key (architecture-dependent)
- const unsigned int keyHOME = 27U; //!< Keycode for the \c HOME key (architecture-dependent)
- const unsigned int keyPAGEUP = 28U; //!< Keycode for the \c PAGEUP key (architecture-dependent)
- const unsigned int keyTAB = 29U; //!< Keycode for the \c TAB key (architecture-dependent)
- const unsigned int keyQ = 30U; //!< Keycode for the \c Q key (architecture-dependent)
- const unsigned int keyW = 31U; //!< Keycode for the \c W key (architecture-dependent)
- const unsigned int keyE = 32U; //!< Keycode for the \c E key (architecture-dependent)
- const unsigned int keyR = 33U; //!< Keycode for the \c R key (architecture-dependent)
- const unsigned int keyT = 34U; //!< Keycode for the \c T key (architecture-dependent)
- const unsigned int keyY = 35U; //!< Keycode for the \c Y key (architecture-dependent)
- const unsigned int keyU = 36U; //!< Keycode for the \c U key (architecture-dependent)
- const unsigned int keyI = 37U; //!< Keycode for the \c I key (architecture-dependent)
- const unsigned int keyO = 38U; //!< Keycode for the \c O key (architecture-dependent)
- const unsigned int keyP = 39U; //!< Keycode for the \c P key (architecture-dependent)
- const unsigned int keyDELETE = 40U; //!< Keycode for the \c DELETE key (architecture-dependent)
- const unsigned int keyEND = 41U; //!< Keycode for the \c END key (architecture-dependent)
- const unsigned int keyPAGEDOWN = 42U; //!< Keycode for the \c PAGEDOWN key (architecture-dependent)
- const unsigned int keyCAPSLOCK = 43U; //!< Keycode for the \c CAPSLOCK key (architecture-dependent)
- const unsigned int keyA = 44U; //!< Keycode for the \c A key (architecture-dependent)
- const unsigned int keyS = 45U; //!< Keycode for the \c S key (architecture-dependent)
- const unsigned int keyD = 46U; //!< Keycode for the \c D key (architecture-dependent)
- const unsigned int keyF = 47U; //!< Keycode for the \c F key (architecture-dependent)
- const unsigned int keyG = 48U; //!< Keycode for the \c G key (architecture-dependent)
- const unsigned int keyH = 49U; //!< Keycode for the \c H key (architecture-dependent)
- const unsigned int keyJ = 50U; //!< Keycode for the \c J key (architecture-dependent)
- const unsigned int keyK = 51U; //!< Keycode for the \c K key (architecture-dependent)
- const unsigned int keyL = 52U; //!< Keycode for the \c L key (architecture-dependent)
- const unsigned int keyENTER = 53U; //!< Keycode for the \c ENTER key (architecture-dependent)
- const unsigned int keySHIFTLEFT = 54U; //!< Keycode for the \c SHIFTLEFT key (architecture-dependent)
- const unsigned int keyZ = 55U; //!< Keycode for the \c Z key (architecture-dependent)
- const unsigned int keyX = 56U; //!< Keycode for the \c X key (architecture-dependent)
- const unsigned int keyC = 57U; //!< Keycode for the \c C key (architecture-dependent)
- const unsigned int keyV = 58U; //!< Keycode for the \c V key (architecture-dependent)
- const unsigned int keyB = 59U; //!< Keycode for the \c B key (architecture-dependent)
- const unsigned int keyN = 60U; //!< Keycode for the \c N key (architecture-dependent)
- const unsigned int keyM = 61U; //!< Keycode for the \c M key (architecture-dependent)
- const unsigned int keySHIFTRIGHT = 62U; //!< Keycode for the \c SHIFTRIGHT key (architecture-dependent)
- const unsigned int keyARROWUP = 63U; //!< Keycode for the \c ARROWUP key (architecture-dependent)
- const unsigned int keyCTRLLEFT = 64U; //!< Keycode for the \c CTRLLEFT key (architecture-dependent)
- const unsigned int keyAPPLEFT = 65U; //!< Keycode for the \c APPLEFT key (architecture-dependent)
- const unsigned int keyALT = 66U; //!< Keycode for the \c ALT key (architecture-dependent)
- const unsigned int keySPACE = 67U; //!< Keycode for the \c SPACE key (architecture-dependent)
- const unsigned int keyALTGR = 68U; //!< Keycode for the \c ALTGR key (architecture-dependent)
- const unsigned int keyAPPRIGHT = 69U; //!< Keycode for the \c APPRIGHT key (architecture-dependent)
- const unsigned int keyMENU = 70U; //!< Keycode for the \c MENU key (architecture-dependent)
- const unsigned int keyCTRLRIGHT = 71U; //!< Keycode for the \c CTRLRIGHT key (architecture-dependent)
- const unsigned int keyARROWLEFT = 72U; //!< Keycode for the \c ARROWLEFT key (architecture-dependent)
- const unsigned int keyARROWDOWN = 73U; //!< Keycode for the \c ARROWDOWN key (architecture-dependent)
- const unsigned int keyARROWRIGHT = 74U; //!< Keycode for the \c ARROWRIGHT key (architecture-dependent)
- const unsigned int keyPAD0 = 75U; //!< Keycode for the \c PAD0 key (architecture-dependent)
- const unsigned int keyPAD1 = 76U; //!< Keycode for the \c PAD1 key (architecture-dependent)
- const unsigned int keyPAD2 = 77U; //!< Keycode for the \c PAD2 key (architecture-dependent)
- const unsigned int keyPAD3 = 78U; //!< Keycode for the \c PAD3 key (architecture-dependent)
- const unsigned int keyPAD4 = 79U; //!< Keycode for the \c PAD4 key (architecture-dependent)
- const unsigned int keyPAD5 = 80U; //!< Keycode for the \c PAD5 key (architecture-dependent)
- const unsigned int keyPAD6 = 81U; //!< Keycode for the \c PAD6 key (architecture-dependent)
- const unsigned int keyPAD7 = 82U; //!< Keycode for the \c PAD7 key (architecture-dependent)
- const unsigned int keyPAD8 = 83U; //!< Keycode for the \c PAD8 key (architecture-dependent)
- const unsigned int keyPAD9 = 84U; //!< Keycode for the \c PAD9 key (architecture-dependent)
- const unsigned int keyPADADD = 85U; //!< Keycode for the \c PADADD key (architecture-dependent)
- const unsigned int keyPADSUB = 86U; //!< Keycode for the \c PADSUB key (architecture-dependent)
- const unsigned int keyPADMUL = 87U; //!< Keycode for the \c PADMUL key (architecture-dependent)
- const unsigned int keyPADDIV = 88U; //!< Keycode for the \c PADDDIV key (architecture-dependent)
- #endif
- const double PI = 3.14159265358979323846; //!< Value of the mathematical constant PI
- // Define a 10x13 binary font (small sans).
- static const char *const data_font_small[] = {
- " UwlwnwoyuwHwlwmwcwlwnw[xuwowlwmwoyuwRwlwnxcw Mw (wnwnwuwpwuypwuwoy"
- "ZwnwmwuwowuwmwnwnwuwowuwfwuxnwnwmwuwpwuypwuwZwnwnwtwpwtwow'y Hw cwnw >{ jw %xdxZwdw_wexfwYwkw 7yowoyFx=w "
- "ry qw %wuw !xnwkwnwoyuwfwuw[wkwnwcwowrwpwdwuwoxuwpwkwnwoyuwRwkwnwbwpwNyoyoyoyoy;wdwnxpxtxowG|!ydwnwuwowtwow"
- "pxswqxlwnxnxmwDwoyoxnyoymwp{oyq{pyoy>ypwqwpwp{oyqzo{q{pzrwrwowlwqwswpwnwqwsxswpypzoyqzozq}swrwrwqwtwswswtxsxswq"
- "ws}qwnwkwnydwew_wfwdwkwmwowkw(w0wmwmwGwtwdxQw swuwnwo{q{pynwp|rwtwtwqydwcwcwcwmwmxgwqwpwnzpwuwpzoyRzoyoyexnynwd"
- "z\\xnxgxrwsxrwsyswowmwmwmwmwmwmwo}ryp{q{q{q{nwmwnwmwozqxswpyoyoyoyoyeyuwswrwrwrwrwrwrwrwrwqwrwmwtwnwmwnwuwpwuyp"
- "wuwoyZwmwnwuwowuwmwqwkwuwowuwoxnwuxowmwnwuwpwuypwuwZwmwnwuwowuwnwowmwtw\\wuwuwqwswqwswqwswqwswEwqwtweypzr~qyIw "
- "rwswewnwuwowuwozswtwuwqwtwmwnwlwowuwuwowOxpxuxqwuwowswqwswoxpwlwjwqwswqwsw<wrwowrwuwqwrwqwswrwswpwmwmwrwswrwowl"
- "wqwtwownxsxsxswqwswqwswqwswrwswqwrwowpwrwrwqwtwswswswswqwswmwpwmwlwoxuxSw_wfwdwYwkw(w0wmwmwGwtwoxnwNw uwswpwuwp"
- "wmwmwswq{rwrwrwtwtwrwswfydwdyZwnwtwrwqwrwswowowdwrwqxuwSwrwfwuwnwlwnw[yuw[wowtwgwswqwswqwswewuwowuwowuwowuwowuw"
- "nwowuwowswqwmwmwmwjwmwnwmwowswrxswqwswqwswqwswqwswqwswrwrwqwswrwrwrwrwrwrwrwrwqwswqzpwtw #w DwPwtwtwswqwswuwuwu"
- "wswswuwswqwGwqxtwf{qzr~r{qzqwrwpxowtwrw rzcwnwuwq}rwuwqwtwuwqwtwmwnwlwnynwOwowswowkwmwpwuwpwmwjwpwswqwswowmwjwi"
- "wjxswsytwrwuwqwrwrwmwrwqwmwnwmwrwowlwqwuwnwnxsxswuwtwrwqwrwswrwqwswswqwjwpwrwqwswrwtwtwqwuwowuwmwowmwlwpxsx]ypz"
- "oyozpypzozqznwmwowtwnwqzuyrzoypzozqwuxoypzpwswrwrwrwtwtwswrwrwrwq{owmwmwQyuwqwtwmwoxnypzqxswowowswqwswqwtxr|rwt"
- "wtwqyp{q{qwswpwuwownwnwqwsxuwuxswrwrwtwtwswqwrwmwuwuwnwnwowtwpwuwuwewnzpwn{pwuwnwnxgwtxtwrwtwowtw_wuytwgynwmwlw"
- "gwswpyuw[wowtwqwtwpwtwpwtwowuwmwnwuwowuwowuwowuwowuwowuwqxuwpwlwmwmwmwjwmwnwmwowrwswuwtwrwqwswqwswqwswqwswqwrwt"
- "wqwswuwswrwrwrwrwrwrwrwpwuwpwswqwuwnyoyoyoyoyoyqyuyqyoyoyoyoymwqwjwmwnypzoyoyoyoyoynwnzqwswqwswqwswqwswrwrwqzqw"
- "rw^}s}swtwtwswtwtwswtwtwK}rwuwe{s~t~s}rwtwqwrwpxowtwrw qwawewtwpwuwpxuwpycwlwnynwOwowswowkwpypwtwpzpzmwoypwsw[y"
- "r}rymwrwtwtwtwrwuwq{qwmwrwq{q{rwm|owlwqxmwnwuwuwuwswuwtwrwqwrwswrwqwswswqylwpwrwqwswrwuwuwuwpwmwmwnwmwlwMwqwswq"
- "wmwswqwswpwnwswqwswowmwowuwmwqwswswswswqwswqwswqwswqxnwswpwnwswrwrwrwtwtwrwtwqwrwmwqxlwlx]xuxrwtyqwuwlwpwtwpwmw"
- "swqwtxpxowswrwqwswtwuxrwtwqwtwtwrwswrwswnwo{pwuwnxpwnwqwswtwtwswrwrwtwtwswuyuwswjwkwowpwrwowcwowuwnwnwswqxuxowo"
- "wtwhwuwrwrzpwtwq}jwuwtwuw_}qyoxfwswpyuwowdyoxowtwryuwqyuwqyuwmwnwuwowuwowuwowuwowuwowuwqwt{twl{q{q{q{nwmwnwmwpz"
- "twswuwtwrwqwswqwswqwswqwswqwqxpwtwtwswrwrwrwrwrwrwrwowowswqwuwkwmwmwmwmwmwowswswmwswqwswqwswqwswnwqwjwmwowswqws"
- "wqwswqwswqwswqwswqwswgwtxqwswqwswqwswqwswrwrwqwswrwrw^wtwtwswqwswuwuwuwswuwswswqwHwowuwf}t~s|r}swrwrwrwqwtwpwtw"
- "r~#zcwewtwoynwuxtwtwswgwlwowuwuwr}gyexowswowlwlwrwswlwqwswowowswpz^yayqwqwtwtwuwrwswrwrwrwmwrwqwmwnwsyswrwowlwq"
- "wuwnwnwuwuwuwswtwuwrwqwrzqwqwszmyowpwrwpwuwqwuwuwuwpwmwmwnwlwmwPzqwswqwmwswq{pwnwswqwswowmwoxlwqwswswswswqwswqw"
- "swqwswqwlxnwnwswqwtwqwuwuwuwqxowtwmwnwmwmwoytwiwtwtwswswpwtxqzpwswpxowswpwuwowuwpwswrwtwtwswtwtwrwtwqwtwtwrwswr"
- "wswnwowswqwswowownwqwswtwtwswrwqwuwuwrwuyuwt~pwq~pwq~pwcwowuwozpwswowewswiwuwrwiwtwjwjwuytw\\wRwswoxuwHwtwpwswq"
- "wtxqwswqxowswqwswqwswqwswqwswqwswrwtwpwlwmwmwmwjwmwnwmwowrwswtwuwrwqwswqwswqwswqwswqwqxpwtwtwswrwrwrwrwrwrwrwow"
- "owswqwtwozpzpzpzpzpzr~swm{q{q{q{nwqwjwmwowswqwswqwswqwswqwswqwswqwswr}rwuwuwqwswqwswqwswqwswqwtwpwswqwtw\\wuwuw"
- "qwswqwswqwswqwswJ}qxf}t~rzp{rwrwrwrwqwtwpwtwrw qwawg}owuwpwuwtwuwswuwfwlwmwmwPwnwswowmwkwr|mwqwswowowswmw^yo}oy"
- "qwqwszq{rwrwrwmwrwqwmwnwqwswrwowlwqwtwownwtwtwswtwuwrwqwrwnwqwswtwkwowpwrwpwuwqwuwuwuwqwuwnwnwmwlwmwQwswqwswqwm"
- "wswqwlwnwswqwswowmwowuwmwqwswswswswqwswqwswqwswqwjwownwswqwtwqwuwuwuwqxowtwnwmwmwmwpwtyhwtwtwswswpwswqwtwpwswqw"
- "mwswpwuwpwtwpwswrwtwtwswtwtwrwtwqwtwtwrwswrwswnwowswqwswpwnwnwqwsxuwuxswrwpyqwqwswjwkwqwuwuwrwrwqwuwuwewowuwnwn"
- "wswq{ownxuwiwtxtwrzpwtwkwjwuwtwuw\\wRwswnwuwSzpwtwowtxqwrwrwtxrxn{q{q{q{q{q{s{pwlwmwmwmwjwmwnwmwowrwswtwuwrwqws"
- "wqwswqwswqwswqwrwtwqwuwswswrwrwrwrwrwrwrwowozpwswqwswqwswqwswqwswqwswqwswswswowmwmwmwmwjwqwjwmwowswqwswqwswqwsw"
- "qwswqwswqwswgwuwuwqwswqwswqwswqwswqwtwpwswqwtw[yoyoyoyoyGwmwdwuwuwpxnxnyqwrwqwtwpwtwoxpw rwswSwuwmwuwpwuwtwuxsw"
- "ewlwcwPwnxuxownwnwswnwlwqwswowowswnwZygygwkwswrwrwqwswrwswpwmwmwrwswrwowlwqwswpwnwqwswsxqwswqwmwswrwswqwrwowpxt"
- "xowowswqwswowowlwlwmwQwswqwswqwmwswqwswpwnwswqwswowmwowtwnwqwswswswswqwswqwswqwswqwmwswpwnwswpxowswqwtwoxnwlwmw"
- "mw[xuxrwtxpwswqwtwpwswqwmwswpypwtwpwswrwtwtwsxuwuxrwtwqwtwtwrwswrwswnwnwuwpwswqwmwmwswq{rwrwowowswqwkwlwoypwtwo"
- "ydwowuwnwn{owmwlwgwrwfwtw^wrw6wswnwuwJwtwowtzswrwrwtzswmwswqwswqwswqwswqwswqwswswswowswqwmwmwmwjwmwnwmwowswrwsx"
- "qwswqwswqwswqwswqwswrwrwqwswrxtxrxtxrxtxrxtxowowmwswqwswqwswqwswqwswqwswqwswswtxowmwswqwswqwswqwswnwqwjwmwowswq"
- "wswqwswqwswqwswqwswqwswowoxtwqwswqwswqwswqwswpxowswpx Wwlwbwnxcwpwrwqzpwtwoxo|!ydwfwtwozpwsxszuxgxnxcwmwcwoxmyp"
- "{q{pymwpzoyowmypymwmwjwiwkwowrwrwqws{oyqzo{qwlzrwrwowlwqwrwq{rwqwswsxpypwlyqwrwqznwoznwowswrxsxpwp}qwkwnwPzqzoy"
- "ozpyowmzqwswowmwowswowqwswswswswpypzozqwlynxozpxowswrwrwpwn{owmwmwQxuxqzoxnyoypwswowpwrwqzpxuxq{qwtxq{qzpylwoyq"
- "}r{qwnyuypwpwrwownydwcwcwcwnzq{rwqwpwmwkwgzHz]}U|owuw@wqwswrytwqwqyqwqwswqwswqwswqwswqwswqwuwr{ryp{q{q{q{nwmwnw"
- "mwozqwsxpyoyoyoyoygwuypzpzpzpznwowmwuypzpzpzpzpzpzryuzryoyoyoyoymwqwjwmwnypwswpyoyoy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q~}Tw~|U{|q~| :v~ S{|w~}"
- "W{|w~|#{|w~| j{}w~ s{}w~Uw~} )v~}Iy~} gw~| W{|w~| :{|}w|}w~| /t~y}x|y}v~} U{|}|x{|w~| "
- " f{}x~| W{|}v~}Sw~}H{|}v~} Qq~| J{|y} *{|}l~}| O{}q"
- "~ tt| `{|i~} Lr~| aw~}9{|w~| `{}q~ l{}s~}Tw~|U{|s~}| 9v~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~"
- "} )v~}Iy~} gw~| W{|w~| :{|q~ .{|i~} U{|q~ ly}w|}w~| [{}q~Rw~}"
- "L{}q~ P{}r~ M{|y}u~y}y| L{}r~| R{|j~} Ks~} `w~}9{|w~| "
- " `{}r~| jy|v}|Tw~|U{|u}| 6v~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~} )v~}Iy}| gw~| W{|w~| :{|r~| "
- " -{|k~}| U{|r~} l{}r~} Z{}r~|Rw~}L{}r~| O{}t~ "
- " k{}t~} -{|`}| `{|}m~}| Jt~} _w~}9{|w~| `{}s~| :w~| cv~ S{|w~}W{|w~|#{|w~| j{}w~ s{}"
- "w~Uw~} )v~} d{|w~| 9y}w~y} ){}o~}| S{|}u~}| k{}r~ Y{}s~|Qw~"
- "}L{}s~| M{}w~} j{}w~}| +{}`~} ]{|x}v~y}| Gw~y} ]w~}9{|w~"
- "| `{}v~}| 8w~| cv~ S{|w~}W{|w~|#{|w~| j{}w~ s{}w~Uw~} g{|w~| 8{|}v~y}| Ly| "
- " g{|y}w~}| X{}v~}|Ow~}L{}v~}| Iy| "
- "l{}`~} Ww~| "
- " L{}`~} Ww}| "
- " r{" };
- // Define a 104x128 binary font (huge sans).
- static const char *const data_font_huge[] = {
- " "
- " "
- " "
- " "
- " "
- " "
- " "
- " "
- " FY AY "
- "'Z ;W @Y @Y 'Z Y @Y (Z :Y ?Y (Z 0Y ?Y (Z >X "
- " "
- " "
- " "
- " "
- " )X AX '\\ )XAV 7YDY -] BY BY '[ +YEY 2X AY (\\ -YDY 'XAU 3Y AY (\\ )XAV 8YD"
- "Y LY AY (\\ ,YEY #Y "
- " "
- " "
- " "
- " (X CX '^ +[CU 6ZEY .` C"
- "X CY '] -ZEZ 2X CY (^ .ZEZ )[CU 2Y CY (] *[CU 7ZEZ LY CY (] -ZEZ %Y "
- " "
- " "
- " "
- " "
- " 'Y EY '^ ,^FV 6ZEY /b CX DX '_ .ZEZ 2Y DX '_ /ZEZ +_FV 1X CX (_ ,^FV 7ZEZ "
- " KX CX (_ .ZEZ &Y "
- " "
- " "
- " "
- " %Y GY '` .aHV 6ZEY 1e DY FX"
- " 'a /ZEZ 1Y FX '` /ZEZ +aHV 0X EX '` .aHV 7ZEZ JX EX (a /ZEZ &X "
- " "
- " "
- " "
- " "
- " #X GX 'XNX 0dKW 6ZEY 1f DY HX &WMX 0ZEZ 0X GX 'XMW 0ZEZ ,dLX /X GX 'WMX 0dLX 7ZEZ"
- " IX GX 'WMX 0ZEZ 'X :T "
- " "
- " "
- " "
- " ;X IX 'XLX 1o 5ZEY 2ZLY "
- " CX IX &WKW 0ZEZ /X HX (XLX 1ZEZ ,o .Y HX (WKX 1o 6ZEZ IY IY (WKW 0ZEZ (X <Z "
- " "
- " "
- " "
- " "
- " =X KX 'XJX 3WKd 5ZEY 3XGX CX JX 'WIW 1ZEZ .X JX (XJX 2ZEZ -WKd -X "
- "IX (WIW 2WKd 6ZEZ HX IX (WIW 1ZEZ )X =^ "
- " "
- " "
- " "
- " >X MX &WH"
- "W 3VHa 4ZEY 3WDW CX LX 'WGW 2ZEZ -X LX 'WHW 2ZEZ -VHa +X KX (XHW 3VHa 5ZEZ GX KX (WGW 2ZEZ )X "
- " ?b "
- " "
- " "
- " "
- " ?W MW &WFW 4VF^ 3ZEY 4WBV BW MX 'WEW 3ZEZ ,W M"
- "X 'WFW 3ZEZ -VF^ )X MX 'WFW 4VF^ 4ZEZ FX MX 'WFW 3ZEZ *X ?d "
- " "
- " "
- " "
- " "
- " ?W X 'WDW 5UC[ 2ZEY 4VAV AW X &WDW 4ZEZ +W NW 'WDW 4ZEZ -UC[ 'W MW 'WDW 5UC[ 3ZEZ "
- "EW MW 'WDW 4ZEZ +X ?f "
- " "
- " "
- " "
- " @X \"X 'WBW 6UAW 0ZEY 4V@V B"
- "X !W &WBV 4ZEZ +X !W 'WBW 5ZEZ .VAW $W W 'WBW 6UAW 1ZEZ DW W 'WBV 4ZEZ +W >f "
- " "
- " "
- " "
- " "
- " ?X #W 'W@W U?V AX #W &W@V NX #W &V@W 9W \"W 'W@V .W "
- "\"W 'W@V !W >XHX "
- " 3Y "
- " "
- " "
- " 6W $W &V>V U?V @W $W &W>V "
- " NW $X 'V>V 8W $X (W>V /X $W 'W>V #W >XFX "
- " 5Z "
- " "
- " ,Z "
- " GZ "
- " #U?V NY 7Z ,X CVCW MY "
- " 7Z ,X $Z 7Z ,X >Z 6Y ,X 4Z 7Y +W 7Y @Z "
- " "
- " +Z "
- " "
- " HY \"U?V "
- " MY 8Y ,Y CVBV LY 9Z ,Y #Z 9Z ,Z >Z 8Y ,Y 3Y 8Z ,Y 9Y "
- " ?Z "
- " *Y "
- " "
- " IY !U?V "
- " LY :Y ,[ $R>U ,V@V MZ :Y +Z #Y 9Y +Z ?R"
- ">U 8Y 9Y +Z %S?U HY :Z ,[ ;Y ?[ "
- " "
- " )Y "
- " 8U "
- " 9Y V@U JY <Y"
- " +[ 'XAU ,V@V LY ;Y +\\ #Y ;Y +\\ CXAU 7Y ;Z ,\\ )XAV HY ;Y +[ <Z "
- " ?U ;T $W /W "
- " 8e !f LY Y LX "
- " L] :Y <Y NX 0X >Y @Y /X 0Y K` .X "
- " ^ =ZEY @Y "
- " NVAV <P -X +Y =Y +] )[CU 7YDY 4V@V KY ="
- "Y +] ,YDY 5Y =Y *] .YDY 5[ M[CU 6Y <Y ,] *[CV 7YDY Y =Y +] ,YEZ !Y =Y FYDY 8X "
- " EU :T %W .X "
- " 9e !f KY !Y LY \"a :Y "
- "<Y NX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4W Q :Q"
- " 9Y #X :Y EX ?Q 8R ?R @Q @R MQ =Y DY @R -Q <Z "
- " #R >RM] !R <R X <X #Y ;Q <Q GR !Q @Q 2Y MX #R 0Y=Q Q=X 'Q "
- " @Q *Z DY X Y ;Y <P AQ CQ ;Y 4Y *YAQ8P @Q0Q -Y 8X 7Y 3Y=Q LQ "
- " JQ 4Z IU 3X -W@[ KYAQ8P 1WEW $U IV MW LW FZ "
- " V KQ GR HQ GQ GQ 0T2Q HR GR HR HQ ,Q %Q GP GQ FQ GQ "
- "GP *P NQ ,V MQ GR HR HR #Q =Q FQ HR HQ FQ *V:Q LQ GQ GQ GQ GY 3Y=Q !Y "
- " 9X MT +X #X :Y EX "
- " 5X BZ 7Y 7] 8X <X #Y "
- " HY MX 0Y 'X MY CY X Y ;Y 8Y 4Y *Y 1Y E"
- "X 3Y CZ IU 3X -\\I_ KY 8WEW $V"
- " %Z NU 0R #V "
- " )T <Z 3Y =Y 8X "
- " MT +X $X 9X DX 6Y AZ NR "
- " =Z 6\\ 8X <X #Y HY MX 0Y '"
- "X NZ CY X X :Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X -q JY 8WEW #V &Z NV "
- " 0V (R "
- " <Y 2Y =Y 8X MT *X "
- "%X 9X EY 6X @[!T >Z 5\\ "
- " 9X ;X $Y HY NY 0Y 'X NY BY X !Y "
- ":Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3X -p "
- " IY 8WEW #V &Z MV "
- " 0U 'P ;Y 2Y >Z 8X "
- " MT *X &X 9X DX "
- " 5X ?\\%W ?Z 4\\ :X ;X $Y "
- " IZ NY 0Y 'X NY BZ !X !Y :Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X -o HY 8WEW \"V "
- " 'Z LU 0V "
- " CZ 2Y >Y 7X "
- " MT )X 'X 9X DX 5W <\\(X ?"
- "Z 3\\ ;Y <X $Y IY MY 0Y 'X "
- " Z AY !X !Y :Y 8Y 4Y *Y 1Y EX 3Y CZ I"
- "U 3X -n GY 8WEW \"V '[3Q <V "
- " 0V DY 1Y "
- "?Z 7X MT )X (X 8W "
- " CX 6X ;],[ AZ 1\\ <e"
- " GX 2f JZ MY 0Y 'X Y @Z \"X \"Z :Y 8"
- "Y 4Y *Y 1Y EX 3Y CZ IU 3X ,k "
- " EY 8WEW !V 'Z4R <V "
- " 0V EZ 1Y ?Y ARGX "
- " MT (X )X 8W DX 5W "
- " 9^1^ AZ 0\\ =e GX 2f KZ LY"
- " 0Y 'X !Z @[ #X #Z 9Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X )f CY 8WEW !V '[7T "
- " ;V 1V "
- " EY 0Y ?Y FWGW "
- " LT 'W *X 8W CX 5W 8`7` A[ "
- " /\\ >e GX 2f KZ LY 0Y 'X !Y >"
- "\\ %X &] 9Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3"
- "X $^ @Y 8WEW !V '\\:V ;V "
- " 1W GZ 0Y @Z "
- " FWHX LT 'X +W 7W "
- " V 5b?c A[ -\\ ?e !f "
- " <P2\\ MY /Y 'X \"Z >f /X 0g 9Y 8Y 4Y *Y "
- " 1Y EX 3Y CZ IU 3X 5Y "
- " NV &\\=X ;V "
- "1W GY /Y AZ EWHX "
- " LT &W ,X 7V V 3~T "
- " A] ,\\ @e !f <R5\\ LY /Y "
- "'X #Z =f /X 0f 8Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X 5Y NW '^B[ <W "
- " 1W "
- " HZ /Y AZ DWIX LT &X -"
- "W 6U NV 1~P B_ *\\ "
- " Ae !f <U:] LZ /Y 'X #Z <e /X 0e 7Y "
- " 8Y 4Y *Y 1Y EX 3Y CZ IU 3X "
- " 5Y X &aJ_ <W "
- " 2X IZ .Y BZ CWJY "
- " LT %X /X "
- " 7| Hf )\\ Be !f <X?_ N[ "
- " .Y 'X %[ :d /X 0e 7Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X 5Y -PDX %v "
- " JQDX ?QEY "
- " J[ .Y D\\ CXLY "
- " KT 7x Fe "
- " -_Me %b .Y 'X /e 9c /X 0c "
- "5Y 8Y 4Y *Y 1Y EX 3Y CZ IU 3X "
- " 5Y -d $u Je "
- " ?d $d -Y Ne Ad "
- " KT "
- " 5s Cd ,v %b -"
- "Y 'X 0e 6a /X 0b 4Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ IU 3X 5Y -d #t Jd "
- " >d "
- " %e -Y Nd @c "
- " (m @c "
- " +u $b -Y 'X 0d 2^ /X 0_ 1Y 8Y 4Y *Y "
- " 1Y EX 3Y CZ IT 2X 5Y "
- "-c !q Hd >c "
- " $d ,Y Nd ?b "
- " %g ="
- "b *t #a ,Y 'X 0d "
- " ,X /X 0Y +Y 8Y 4Y *Y 1Y EX 3Y CZ '"
- "X 5Y -c Nm Fc "
- " =c $c +Y Nc "
- " >a "
- " M\\ 8a \"~Y 1"
- "r !` +Y 'X 0c 1X 1Y 8Y 4Y *Y 1Y EX 3Y "
- " CZ &W 5Y -b Lj "
- " Db <b "
- " #b *Y Nb <_ "
- " (_ "
- " ~Y 1q _ *Y 'X 0b 0X 1Y "
- " 8Y 4Y *Y 1Y EX 3Y CZ "
- " 3Y -` He A` "
- " :` !a )Y Na :] "
- " "
- " '] M~Y .l M] (Y 'X "
- " 0` .X 1Y 8Y 4Y *Y 1Y EX 3Y "
- " KY *Z B^ 9Z "
- " 5Z M` (Y N` "
- " 8Z "
- " %X H~Y "
- " *d I[ &Y 'X 0^ ,X 1Y 8Y 4Y *Y 1Y EX 3Y"
- " KY "
- " "
- " H^ &Y N] 3V "
- " "
- " B~Y #X CU !X &X /Y (X 1"
- "Y 7X 4X )X 0Y EX 2X "
- " KY "
- " HZ \"X MY "
- " "
- " J~Y "
- " 9X "
- " "
- " "
- " "
- " 3~Y "
- " 9X "
- " "
- " "
- " "
- " 3~Y "
- " 9X "
- " "
- " "
- " '" };
- // Define a 40x38 'danger' color logo (used by cimg::dialog()).
- static const unsigned char logo40x38[4576] = {
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 123,123,123,86,200,200,200,91,49,124,118,124,71,32,124,95,49,56,114,52,82,121,0 };
- //! Get/set default output stream for the \CImg library messages.
- /**
- \param file Desired output stream. Set to \c 0 to get the currently used output stream only.
- \return Currently used output stream.
- **/
- inline std::FILE* output(std::FILE *file) {
- cimg::mutex(1);
- static std::FILE *res = cimg::_stderr();
- if (file) res = file;
- cimg::mutex(1,0);
- return res;
- }
- // Return number of available CPU cores.
- inline unsigned int nb_cpus() {
- unsigned int res = 1;
- #if cimg_OS==2
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
- res = (unsigned int)sysinfo.dwNumberOfProcessors;
- #elif cimg_OS == 1
- res = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
- #endif
- return res?res:1U;
- }
- // Lock/unlock mutex for CImg multi-thread programming.
- inline int mutex(const unsigned int n, const int lock_mode) {
- switch (lock_mode) {
- case 0 : cimg::Mutex_attr().unlock(n); return 0;
- case 1 : cimg::Mutex_attr().lock(n); return 0;
- default : return cimg::Mutex_attr().trylock(n);
- }
- }
- //! Display a warning message on the default output stream.
- /**
- \param format C-string containing the format of the message, as with <tt>std::printf()</tt>.
- \note If configuration macro \c cimg_strict_warnings is set, this function throws a
- \c CImgWarningException instead.
- \warning As the first argument is a format string, it is highly recommended to write
- \code
- cimg::warn("%s",warning_message);
- \endcode
- instead of
- \code
- cimg::warn(warning_message);
- \endcode
- if \c warning_message can be arbitrary, to prevent nasty memory access.
- **/
- inline void warn(const char *const format, ...) {
- if (cimg::exception_mode()>=1) {
- char *const message = new char[16384];
- std::va_list ap;
- va_start(ap,format);
- cimg_vsnprintf(message,16384,format,ap);
- va_end(ap);
- #ifdef cimg_strict_warnings
- throw CImgWarningException(message);
- #else
- std::fprintf(cimg::output(),"\n%s[CImg] *** Warning ***%s%s\n",cimg::t_red,cimg::t_normal,message);
- #endif
- delete[] message;
- }
- }
- // Execute an external system command.
- /**
- \param command C-string containing the command line to execute.
- \param module_name Module name.
- \return Status value of the executed command, whose meaning is OS-dependent.
- \note This function is similar to <tt>std::system()</tt>
- but it does not open an extra console windows
- on Windows-based systems.
- **/
- inline int system(const char *const command, const char *const module_name=0, const bool is_verbose=false) {
- cimg::unused(module_name);
- #ifdef cimg_no_system_calls
- return -1;
- #else
- if (is_verbose) return std::system(command);
- #if cimg_OS==1
- const unsigned int l = (unsigned int)std::strlen(command);
- if (l) {
- char *const ncommand = new char[l + 24];
- std::memcpy(ncommand,command,l);
- std::strcpy(ncommand + l," >/dev/null 2>&1"); // Make command silent
- const int out_val = std::system(ncommand);
- delete[] ncommand;
- return out_val;
- } else return -1;
- #elif cimg_OS==2
- PROCESS_INFORMATION pi;
- STARTUPINFOA si;
- std::memset(&pi,0,sizeof(PROCESS_INFORMATION));
- std::memset(&si,0,sizeof(STARTUPINFO));
- GetStartupInfoA(&si);
- si.cb = sizeof(si);
- si.wShowWindow = SW_HIDE;
- si.dwFlags |= SW_HIDE | STARTF_USESHOWWINDOW;
- const BOOL res = CreateProcessA((LPCSTR)module_name,(LPSTR)command,0,0,FALSE,0,0,0,&si,&pi);
- if (res) {
- WaitForSingleObject(pi.hProcess,INFINITE);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- return 0;
- } else {
- char* lpMsgBuf;
- // Get the error message.
- DWORD errorCode = GetLastError();
- FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- 0,errorCode,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsgBuf,0,0);
- cimg::warn("cimg::system() : Command '%s' (module name '%s) failed with error %lu: %s",
- module_name==0?"(null)":module_name,
- command==0?"(null)":command,
- errorCode,lpMsgBuf);
- return -1;
- }
- #else
- return std::system(command);
- #endif
- #endif
- }
- //! Return a reference to a temporary variable of type T.
- template<typename T>
- inline T& temporary(const T&) {
- static T temp;
- return temp;
- }
- //! Exchange values of variables \c a and \c b.
- template<typename T>
- inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
- //! Exchange values of variables (\c a1,\c a2) and (\c b1,\c b2).
- template<typename T1, typename T2>
- inline void swap(T1& a1, T1& b1, T2& a2, T2& b2) {
- cimg::swap(a1,b1); cimg::swap(a2,b2);
- }
- //! Exchange values of variables (\c a1,\c a2,\c a3) and (\c b1,\c b2,\c b3).
- template<typename T1, typename T2, typename T3>
- inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3) {
- cimg::swap(a1,b1,a2,b2); cimg::swap(a3,b3);
- }
- //! Exchange values of variables (\c a1,\c a2,...,\c a4) and (\c b1,\c b2,...,\c b4).
- template<typename T1, typename T2, typename T3, typename T4>
- inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4) {
- cimg::swap(a1,b1,a2,b2,a3,b3); cimg::swap(a4,b4);
- }
- //! Exchange values of variables (\c a1,\c a2,...,\c a5) and (\c b1,\c b2,...,\c b5).
- template<typename T1, typename T2, typename T3, typename T4, typename T5>
- inline void swap(T1& a1, T1& b1, T2& a2, T2& b2, T3& a3, T3& b3, T4& a4, T4& b4, T5& a5, T5& b5) {
- cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4); cimg::swap(a5,b5);
- }
- //! Exchange values of variables (\c a1,\c a2,...,\c a6) and (\c b1,\c b2,...,\c b6).
- template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
- 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) {
- cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5); cimg::swap(a6,b6);
- }
- //! Exchange values of variables (\c a1,\c a2,...,\c a7) and (\c b1,\c b2,...,\c b7).
- template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
- 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,
- T7& a7, T7& b7) {
- cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6); cimg::swap(a7,b7);
- }
- //! Exchange values of variables (\c a1,\c a2,...,\c a8) and (\c b1,\c b2,...,\c b8).
- template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
- 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,
- T7& a7, T7& b7, T8& a8, T8& b8) {
- cimg::swap(a1,b1,a2,b2,a3,b3,a4,b4,a5,b5,a6,b6,a7,b7); cimg::swap(a8,b8);
- }
- //! Return the endianness of the current architecture.
- /**
- \return \c false for <i>Little Endian</i> or \c true for <i>Big Endian</i>.
- **/
- inline bool endianness() {
- const int x = 1;
- return ((unsigned char*)&x)[0]?false:true;
- }
- //! Reverse endianness of all elements in a memory buffer.
- /**
- \param[in,out] buffer Memory buffer whose endianness must be reversed.
- \param size Number of buffer elements to reverse.
- **/
- template<typename T>
- inline void invert_endianness(T* const buffer, const cimg_ulong size) {
- if (size) switch (sizeof(T)) {
- case 1 : break;
- case 2 : {
- for (unsigned short *ptr = (unsigned short*)buffer + size; ptr>(unsigned short*)buffer; ) {
- const unsigned short val = *(--ptr);
- *ptr = (unsigned short)((val>>8) | ((val<<8)));
- }
- } break;
- case 4 : {
- for (unsigned int *ptr = (unsigned int*)buffer + size; ptr>(unsigned int*)buffer; ) {
- const unsigned int val = *(--ptr);
- *ptr = (val>>24) | ((val>>8)&0xff00) | ((val<<8)&0xff0000) | (val<<24);
- }
- } break;
- case 8 : {
- const cimg_uint64
- m0 = (cimg_uint64)0xff, m1 = m0<<8, m2 = m0<<16, m3 = m0<<24,
- m4 = m0<<32, m5 = m0<<40, m6 = m0<<48, m7 = m0<<56;
- for (cimg_uint64 *ptr = (cimg_uint64*)buffer + size; ptr>(cimg_uint64*)buffer; ) {
- const cimg_uint64 val = *(--ptr);
- *ptr = (((val&m7)>>56) | ((val&m6)>>40) | ((val&m5)>>24) | ((val&m4)>>8) |
- ((val&m3)<<8) |((val&m2)<<24) | ((val&m1)<<40) | ((val&m0)<<56));
- }
- } break;
- default : {
- for (T* ptr = buffer + size; ptr>buffer; ) {
- unsigned char *pb = (unsigned char*)(--ptr), *pe = pb + sizeof(T);
- for (int i = 0; i<(int)sizeof(T)/2; ++i) swap(*(pb++),*(--pe));
- }
- }
- }
- }
- inline void invert_endianness(bool* const, const cimg_ulong) {}
- inline void invert_endianness(unsigned char* const, const cimg_ulong) {}
- inline void invert_endianness(char* const, const cimg_ulong) {}
- //! Reverse endianness of a single variable.
- /**
- \param[in,out] a Variable to reverse.
- \return Reference to reversed variable.
- **/
- template<typename T>
- inline T& invert_endianness(T& a) {
- invert_endianness(&a,1);
- return a;
- }
- // Conversion functions to get more precision when trying to store unsigned ints values as floats.
- inline unsigned int float2uint(const float f) {
- int tmp = 0;
- std::memcpy(&tmp,&f,sizeof(float));
- if (tmp>=0) return (unsigned int)f;
- unsigned int u;
- // use memcpy instead of assignment to avoid undesired optimizations by C++-compiler.
- std::memcpy(&u,&f,sizeof(float));
- return ((u)<<2)>>2; // set sign & exponent bit to 0
- }
- inline float uint2float(const unsigned int u) {
- if (u<(1U<<19)) return (float)u; // Consider safe storage of unsigned int as floats until 19bits (i.e 524287)
- float f;
- const unsigned int v = u|(3U<<(8*sizeof(unsigned int)-2)); // set sign & exponent bit to 1
- // use memcpy instead of simple assignment to avoid undesired optimizations by C++-compiler.
- std::memcpy(&f,&v,sizeof(float));
- return f;
- }
- //! Return the value of a system timer, with a millisecond precision.
- /**
- \note The timer does not necessarily starts from \c 0.
- **/
- inline cimg_uint64 time() {
- #if cimg_OS==1
- struct timeval st_time;
- gettimeofday(&st_time,0);
- return (cimg_uint64)st_time.tv_sec*1000 + (cimg_uint64)st_time.tv_usec/1000;
- #elif cimg_OS==2
- ULARGE_INTEGER ul;
- FILETIME ft;
- GetSystemTimeAsFileTime(&ft);
- ul.LowPart = ft.dwLowDateTime;
- ul.HighPart = ft.dwHighDateTime;
- return (cimg_uint64)ul.QuadPart/10000;
- #else
- return 0;
- #endif
- }
- // Implement a tic/toc mechanism to display elapsed time of algorithms.
- inline cimg_uint64 tictoc(const bool is_tic);
- //! Start tic/toc timer for time measurement between code instructions.
- /**
- \return Current value of the timer (same value as time()).
- **/
- inline cimg_uint64 tic() {
- return cimg::tictoc(true);
- }
- //! End tic/toc timer and displays elapsed time from last call to tic().
- /**
- \return Time elapsed (in ms) since last call to tic().
- **/
- inline cimg_uint64 toc() {
- return cimg::tictoc(false);
- }
- //! Sleep for a given numbers of milliseconds.
- /**
- \param milliseconds Number of milliseconds to wait for.
- \note This function frees the CPU resources during the sleeping time.
- It can be used to temporize your program properly, without wasting CPU time.
- **/
- inline void sleep(const unsigned int milliseconds) {
- #if cimg_OS==1
- struct timespec tv;
- tv.tv_sec = milliseconds/1000;
- tv.tv_nsec = (milliseconds%1000)*1000000;
- nanosleep(&tv,0);
- #elif cimg_OS==2
- Sleep(milliseconds);
- #else
- cimg::unused(milliseconds);
- #endif
- }
- inline unsigned int wait(const unsigned int milliseconds, cimg_uint64 *const p_timer) {
- if (!*p_timer) *p_timer = cimg::time();
- const cimg_uint64 current_time = cimg::time();
- if (current_time<*p_timer || current_time>=*p_timer + milliseconds) { *p_timer = current_time; return 0; }
- const unsigned int time_diff = (unsigned int)(*p_timer + milliseconds - current_time);
- *p_timer = current_time + time_diff;
- cimg::sleep(time_diff);
- return time_diff;
- }
- //! Wait for a given number of milliseconds since the last call to wait().
- /**
- \param milliseconds Number of milliseconds to wait for.
- \return Number of milliseconds elapsed since the last call to wait().
- \note Same as sleep() with a waiting time computed with regard to the last call
- of wait(). It may be used to temporize your program properly, without wasting CPU time.
- **/
- inline unsigned int wait(const unsigned int milliseconds) {
- cimg::mutex(3);
- static cimg_uint64 timer = cimg::time();
- cimg::mutex(3,0);
- return cimg::wait(milliseconds,&timer);
- }
- // Custom random number generator (allow re-entrance).
- inline cimg_uint64& rng() { // Used as a shared global number for rng
- static cimg_uint64 rng = 0xB16B00B5U;
- return rng;
- }
- inline unsigned int _rand(cimg_uint64 *const p_rng) {
- *p_rng = *p_rng*1103515245 + 12345U;
- return (unsigned int)*p_rng;
- }
- inline unsigned int _rand() {
- cimg::mutex(4);
- const unsigned int res = cimg::_rand(&cimg::rng());
- cimg::mutex(4,0);
- return res;
- }
- inline void srand(cimg_uint64 *const p_rng) {
- #if cimg_OS==1
- *p_rng = cimg::time() + (cimg_uint64)getpid();
- #elif cimg_OS==2
- *p_rng = cimg::time() + (cimg_uint64)_getpid();
- #endif
- }
- inline void srand() {
- cimg::mutex(4);
- cimg::srand(&cimg::rng());
- cimg::mutex(4,0);
- }
- inline void srand(const cimg_uint64 seed) {
- cimg::mutex(4);
- cimg::rng() = seed;
- cimg::mutex(4,0);
- }
- inline double rand(const double val_min, const double val_max, cimg_uint64 *const p_rng) {
- const double val = cimg::_rand(p_rng)/(double)~0U;
- return val_min + (val_max - val_min)*val;
- }
- inline double rand(const double val_min, const double val_max) {
- cimg::mutex(4);
- const double res = cimg::rand(val_min,val_max,&cimg::rng());
- cimg::mutex(4,0);
- return res;
- }
- inline double rand(const double val_max, cimg_uint64 *const p_rng) {
- const double val = cimg::_rand(p_rng)/(double)~0U;
- return val_max*val;
- }
- inline double rand(const double val_max=1) {
- cimg::mutex(4);
- const double res = cimg::rand(val_max,&cimg::rng());
- cimg::mutex(4,0);
- return res;
- }
- inline double grand(cimg_uint64 *const p_rng) {
- double x1, w;
- do {
- const double x2 = cimg::rand(-1,1,p_rng);
- x1 = cimg::rand(-1,1,p_rng);
- w = x1*x1 + x2*x2;
- } while (w<=0 || w>=1.);
- return x1*std::sqrt((-2*std::log(w))/w);
- }
- inline double grand() {
- cimg::mutex(4);
- const double res = cimg::grand(&cimg::rng());
- cimg::mutex(4,0);
- return res;
- }
- inline unsigned int prand(const double z, cimg_uint64 *const p_rng) {
- if (z<=1.e-10) return 0;
- if (z>100) return (unsigned int)((std::sqrt(z) * cimg::grand(p_rng)) + z);
- unsigned int k = 0;
- const double y = std::exp(-z);
- for (double s = 1.; s>=y; ++k) s*=cimg::rand(1,p_rng);
- return k - 1;
- }
- inline unsigned int prand(const double z) {
- cimg::mutex(4);
- const unsigned int res = cimg::prand(z,&cimg::rng());
- cimg::mutex(4,0);
- return res;
- }
- //! Cut (i.e. clamp) value in specified interval.
- template<typename T, typename t>
- inline T cut(const T& val, const t& val_min, const t& val_max) {
- return val<=val_min?(T)val_min:val>=val_max?(T)val_max:val;
- }
- //! Bitwise-rotate value on the left.
- template<typename T>
- inline T rol(const T& a, const unsigned int n=1) {
- return n?(T)((a<<n)|(a>>((sizeof(T)<<3) - n))):a;
- }
- inline float rol(const float a, const unsigned int n=1) {
- return (float)rol((int)a,n);
- }
- inline double rol(const double a, const unsigned int n=1) {
- return (double)rol((cimg_long)a,n);
- }
- inline double rol(const long double a, const unsigned int n=1) {
- return (double)rol((cimg_long)a,n);
- }
- #ifdef cimg_use_half
- inline half rol(const half a, const unsigned int n=1) {
- return (half)rol((int)a,n);
- }
- #endif
- //! Bitwise-rotate value on the right.
- template<typename T>
- inline T ror(const T& a, const unsigned int n=1) {
- return n?(T)((a>>n)|(a<<((sizeof(T)<<3) - n))):a;
- }
- inline float ror(const float a, const unsigned int n=1) {
- return (float)ror((int)a,n);
- }
- inline double ror(const double a, const unsigned int n=1) {
- return (double)ror((cimg_long)a,n);
- }
- inline double ror(const long double a, const unsigned int n=1) {
- return (double)ror((cimg_long)a,n);
- }
- #ifdef cimg_use_half
- inline half ror(const half a, const unsigned int n=1) {
- return (half)ror((int)a,n);
- }
- #endif
- //! Return absolute value of a value.
- template<typename T>
- inline T abs(const T& a) {
- return a>=0?a:-a;
- }
- inline bool abs(const bool a) {
- return a;
- }
- inline int abs(const unsigned char a) {
- return (int)a;
- }
- inline int abs(const unsigned short a) {
- return (int)a;
- }
- inline int abs(const unsigned int a) {
- return (int)a;
- }
- inline int abs(const int a) {
- return std::abs(a);
- }
- inline cimg_int64 abs(const cimg_uint64 a) {
- return (cimg_int64)a;
- }
- inline double abs(const double a) {
- return std::fabs(a);
- }
- inline float abs(const float a) {
- return (float)std::fabs((double)a);
- }
- //! Return hyperbolic arcosine of a value.
- inline double acosh(const double x) {
- #if cimg_use_cpp11==1 && !defined(_MSC_VER)
- return std::acosh(x);
- #else
- return std::log(x + std::sqrt(x*x - 1));
- #endif
- }
- //! Return hyperbolic arcsine of a value.
- inline double asinh(const double x) {
- #if cimg_use_cpp11==1 && !defined(_MSC_VER)
- return std::asinh(x);
- #else
- return std::log(x + std::sqrt(x*x + 1));
- #endif
- }
- //! Return hyperbolic arctangent of a value.
- inline double atanh(const double x) {
- #if cimg_use_cpp11==1 && !defined(_MSC_VER)
- return std::atanh(x);
- #else
- return 0.5*std::log((1. + x)/(1. - x));
- #endif
- }
- //! Return the sinc of a given value.
- inline double sinc(const double x) {
- return x?std::sin(x)/x:1;
- }
- //! Return base-2 logarithm of a value.
- inline double log2(const double x) {
- #if cimg_use_cpp11==1 && !defined(_MSC_VER)
- return std::log2(x);
- #else
- const double base2 = std::log(2.);
- return std::log(x)/base2;
- #endif
- }
- //! Return square of a value.
- template<typename T>
- inline T sqr(const T& val) {
- return val*val;
- }
- // Return inverse of error function.
- template<typename T>
- inline T erfinv(const T& val) {
- const T
- sgn = val<0?-1:1,
- x = (1 - val)*(1 + val),
- lnx = std::log(x),
- tt1 = (T)(2/(cimg::PI*0.147) + 0.5*lnx),
- tt2 = lnx/(T)0.147;
- return sgn*std::sqrt(-tt1 + std::sqrt(tt1*tt1 - tt2));
- }
- //! Return cubic root of a value.
- template<typename T>
- inline double cbrt(const T& x) {
- #if cimg_use_cpp11==1
- return std::cbrt(x);
- #else
- return x>=0?std::pow((double)x,1./3):-std::pow(-(double)x,1./3);
- #endif
- }
- template<typename T>
- inline T pow3(const T& val) {
- return val*val*val;
- }
- template<typename T>
- inline T pow4(const T& val) {
- return val*val*val*val;
- }
- //! Return the minimum between three values.
- template<typename t>
- inline t min(const t& a, const t& b, const t& c) {
- return std::min(std::min(a,b),c);
- }
- //! Return the minimum between four values.
- template<typename t>
- inline t min(const t& a, const t& b, const t& c, const t& d) {
- return std::min(std::min(a,b),std::min(c,d));
- }
- //! Return the minabs between two values.
- template<typename t>
- inline t minabs(const t& a, const t& b) {
- return cimg::abs(b)<cimg::abs(a)?b:a;
- }
- template<typename t>
- inline t minabs(const t& a, const t& b, const t& abs_b) {
- return abs_b<cimg::abs(a)?b:a;
- }
- //! Return the maximum between three values.
- template<typename t>
- inline t max(const t& a, const t& b, const t& c) {
- return std::max(std::max(a,b),c);
- }
- //! Return the maximum between four values.
- template<typename t>
- inline t max(const t& a, const t& b, const t& c, const t& d) {
- return std::max(std::max(a,b),std::max(c,d));
- }
- //! Return the maxabs between two values.
- template<typename t>
- inline t maxabs(const t& a, const t& b) {
- return cimg::abs(b)>cimg::abs(a)?b:a;
- }
- template<typename t>
- inline t maxabs(const t& a, const t& b, const t& abs_b) {
- return abs_b>cimg::abs(a)?b:a;
- }
- //! Return the sign of a value.
- template<typename T>
- inline T sign(const T& x) {
- return (T)(cimg::type<T>::is_nan(x)?0:x<0?-1:x>0);
- }
- //! Return the nearest power of 2 higher than given value.
- template<typename T>
- inline cimg_uint64 nearest_pow2(const T& x) {
- cimg_uint64 i = 1;
- while (x>i) i<<=1;
- return i;
- }
- //! Return the modulo of a value.
- /**
- \param x Input value.
- \param m Modulo value.
- \note This modulo function accepts negative and floating-points modulo numbers, as well as variables of any type.
- **/
- template<typename T>
- inline T mod(const T& x, const T& m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- const double dx = (double)x, dm = (double)m;
- if (!cimg::type<double>::is_finite(dm)) return x;
- if (cimg::type<double>::is_finite(dx)) return (T)(dx - dm * std::floor(dx / dm));
- return (T)0;
- }
- inline int mod(const bool x, const bool m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return x?1:0;
- }
- inline int mod(const unsigned char x, const unsigned char m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return x%m;
- }
- inline int mod(const char x, const char m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- #if defined(CHAR_MAX) && CHAR_MAX==255
- return x%m;
- #else
- return x>=0?x%m:(x%m?m + x%m:0);
- #endif
- }
- inline int mod(const unsigned short x, const unsigned short m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (int)(x%m);
- }
- inline int mod(const short x, const short m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (int)(x>=0?x%m:(x%m?m + x%m:0));
- }
- inline int mod(const unsigned int x, const unsigned int m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (int)(x%m);
- }
- inline int mod(const int x, const int m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (int)(x>=0?x%m:(x%m?m + x%m:0));
- }
- inline cimg_int64 mod(const cimg_uint64 x, const cimg_uint64 m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (cimg_int64)(x%m);
- }
- inline cimg_int64 mod(const cimg_int64 x, const cimg_int64 m) {
- if (!m) throw CImgArgumentException("cimg::mod(): Specified modulo value is 0.");
- return (cimg_int64)(x>=0?x%m:(x%m?m + x%m:0));
- }
- //! Return the min-mod of two values.
- /**
- \note <i>minmod(\p a,\p b)</i> is defined to be:
- - <i>minmod(\p a,\p b) = min(\p a,\p b)</i>, if \p a and \p b have the same sign.
- - <i>minmod(\p a,\p b) = 0</i>, if \p a and \p b have different signs.
- **/
- template<typename T>
- inline T minmod(const T& a, const T& b) {
- return a*b<=0?0:(a>0?(a<b?a:b):(a<b?b:a));
- }
- template<typename T>
- inline T round(const T& x) {
- return (T)std::floor((_cimg_Tfloat)x + 0.5f);
- }
- template<typename T>
- inline int uiround(const T x) {
- return cimg::type<T>::is_float()?(int)(x + 0.5f):(int)x;
- }
- //! Return rounded value.
- /**
- \param x Value to be rounded.
- \param y Rounding precision.
- \param rounding_type Type of rounding operation (\c 0 = nearest, \c -1 = backward, \c 1 = forward).
- \return Rounded value, having the same type as input value \c x.
- **/
- template<typename T>
- inline T round(const T& x, const double y, const int rounding_type=0) {
- if (y<=0) return x;
- if (y==1) switch (rounding_type) {
- case 0 : return cimg::round(x);
- case 1 : return (T)std::ceil((_cimg_Tfloat)x);
- default : return (T)std::floor((_cimg_Tfloat)x);
- }
- const double sx = (double)x/y, floor = std::floor(sx), delta = sx - floor;
- return (T)(y*(rounding_type<0?floor:rounding_type>0?std::ceil(sx):delta<0.5?floor:std::ceil(sx)));
- }
- // Code to compute fast median from 2,3,5,7,9,13,25 and 49 values.
- // (contribution by RawTherapee: http://rawtherapee.com/).
- template<typename T>
- inline T median(T val0, T val1) {
- return (val0 + val1)/2;
- }
- template<typename T>
- inline T median(T val0, T val1, T val2) {
- return std::max(std::min(val0,val1),std::min(val2,std::max(val0,val1)));
- }
- template<typename T>
- inline T median(T val0, T val1, T val2, T val3, T val4) {
- T tmp = std::min(val0,val1);
- val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4);
- val3 = std::max(val0,tmp); val1 = std::min(val1,val4); tmp = std::min(val1,val2); val2 = std::max(val1,val2);
- val1 = tmp; tmp = std::min(val2,val3);
- return std::max(val1,tmp);
- }
- template<typename T>
- inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6) {
- T tmp = std::min(val0,val5);
- val5 = std::max(val0,val5); val0 = tmp; tmp = std::min(val0,val3); val3 = std::max(val0,val3); val0 = tmp;
- tmp = std::min(val1,val6); val6 = std::max(val1,val6); val1 = tmp; tmp = std::min(val2,val4);
- val4 = std::max(val2,val4); val2 = tmp; val1 = std::max(val0,val1); tmp = std::min(val3,val5);
- val5 = std::max(val3,val5); val3 = tmp; tmp = std::min(val2,val6); val6 = std::max(val2,val6);
- val3 = std::max(tmp,val3); val3 = std::min(val3,val6); tmp = std::min(val4,val5); val4 = std::max(val1,tmp);
- tmp = std::min(val1,tmp); val3 = std::max(tmp,val3);
- return std::min(val3,val4);
- }
- template<typename T>
- inline T median(T val0, T val1, T val2, T val3, T val4, T val5, T val6, T val7, T val8) {
- T tmp = std::min(val1,val2);
- val2 = std::max(val1,val2); val1 = tmp; tmp = std::min(val4,val5);
- val5 = std::max(val4,val5); val4 = tmp; tmp = std::min(val7,val8);
- val8 = std::max(val7,val8); val7 = tmp; tmp = std::min(val0,val1);
- val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4);
- val4 = std::max(val3,val4); val3 = tmp; tmp = std::min(val6,val7);
- val7 = std::max(val6,val7); val6 = tmp; tmp = std::min(val1,val2);
- val2 = std::max(val1,val2); val1 = tmp; tmp = std::min(val4,val5);
- val5 = std::max(val4,val5); val4 = tmp; tmp = std::min(val7,val8);
- val8 = std::max(val7,val8); val3 = std::max(val0,val3); val5 = std::min(val5,val8);
- val7 = std::max(val4,tmp); tmp = std::min(val4,tmp); val6 = std::max(val3,val6);
- val4 = std::max(val1,tmp); val2 = std::min(val2,val5); val4 = std::min(val4,val7);
- tmp = std::min(val4,val2); val2 = std::max(val4,val2); val4 = std::max(val6,tmp);
- return std::min(val4,val2);
- }
- template<typename T>
- 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,
- T val12) {
- T tmp = std::min(val1,val7);
- val7 = std::max(val1,val7); val1 = tmp; tmp = std::min(val9,val11); val11 = std::max(val9,val11); val9 = tmp;
- tmp = std::min(val3,val4); val4 = std::max(val3,val4); val3 = tmp; tmp = std::min(val5,val8);
- val8 = std::max(val5,val8); val5 = tmp; tmp = std::min(val0,val12); val12 = std::max(val0,val12);
- val0 = tmp; tmp = std::min(val2,val6); val6 = std::max(val2,val6); val2 = tmp; tmp = std::min(val0,val1);
- val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val2,val3); val3 = std::max(val2,val3); val2 = tmp;
- tmp = std::min(val4,val6); val6 = std::max(val4,val6); val4 = tmp; tmp = std::min(val8,val11);
- val11 = std::max(val8,val11); val8 = tmp; tmp = std::min(val7,val12); val12 = std::max(val7,val12); val7 = tmp;
- tmp = std::min(val5,val9); val9 = std::max(val5,val9); val5 = tmp; tmp = std::min(val0,val2);
- val2 = std::max(val0,val2); val0 = tmp; tmp = std::min(val3,val7); val7 = std::max(val3,val7); val3 = tmp;
- tmp = std::min(val10,val11); val11 = std::max(val10,val11); val10 = tmp; tmp = std::min(val1,val4);
- val4 = std::max(val1,val4); val1 = tmp; tmp = std::min(val6,val12); val12 = std::max(val6,val12); val6 = tmp;
- tmp = std::min(val7,val8); val8 = std::max(val7,val8); val7 = tmp; val11 = std::min(val11,val12);
- tmp = std::min(val4,val9); val9 = std::max(val4,val9); val4 = tmp; tmp = std::min(val6,val10);
- val10 = std::max(val6,val10); val6 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4); val3 = tmp;
- tmp = std::min(val5,val6); val6 = std::max(val5,val6); val5 = tmp; val8 = std::min(val8,val9);
- val10 = std::min(val10,val11); tmp = std::min(val1,val7); val7 = std::max(val1,val7); val1 = tmp;
- tmp = std::min(val2,val6); val6 = std::max(val2,val6); val2 = tmp; val3 = std::max(val1,val3);
- tmp = std::min(val4,val7); val7 = std::max(val4,val7); val4 = tmp; val8 = std::min(val8,val10);
- val5 = std::max(val0,val5); val5 = std::max(val2,val5); tmp = std::min(val6,val8); val8 = std::max(val6,val8);
- val5 = std::max(val3,val5); val7 = std::min(val7,val8); val6 = std::max(val4,tmp); tmp = std::min(val4,tmp);
- val5 = std::max(tmp,val5); val6 = std::min(val6,val7);
- return std::max(val5,val6);
- }
- template<typename T>
- 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, T val12, T val13, T val14,
- T val15, T val16, T val17, T val18, T val19,
- T val20, T val21, T val22, T val23, T val24) {
- T tmp = std::min(val0,val1);
- val1 = std::max(val0,val1); val0 = tmp; tmp = std::min(val3,val4); val4 = std::max(val3,val4);
- val3 = tmp; tmp = std::min(val2,val4); val4 = std::max(val2,val4); val2 = std::min(tmp,val3);
- val3 = std::max(tmp,val3); tmp = std::min(val6,val7); val7 = std::max(val6,val7); val6 = tmp;
- tmp = std::min(val5,val7); val7 = std::max(val5,val7); val5 = std::min(tmp,val6); val6 = std::max(tmp,val6);
- tmp = std::min(val9,val10); val10 = std::max(val9,val10); val9 = tmp; tmp = std::min(val8,val10);
- val10 = std::max(val8,val10); val8 = std::min(tmp,val9); val9 = std::max(tmp,val9);
- tmp = std::min(val12,val13); val13 = std::max(val12,val13); val12 = tmp; tmp = std::min(val11,val13);
- val13 = std::max(val11,val13); val11 = std::min(tmp,val12); val12 = std::max(tmp,val12);
- tmp = std::min(val15,val16); val16 = std::max(val15,val16); val15 = tmp; tmp = std::min(val14,val16);
- val16 = std::max(val14,val16); val14 = std::min(tmp,val15); val15 = std::max(tmp,val15);
- tmp = std::min(val18,val19); val19 = std::max(val18,val19); val18 = tmp; tmp = std::min(val17,val19);
- val19 = std::max(val17,val19); val17 = std::min(tmp,val18); val18 = std::max(tmp,val18);
- tmp = std::min(val21,val22); val22 = std::max(val21,val22); val21 = tmp; tmp = std::min(val20,val22);
- val22 = std::max(val20,val22); val20 = std::min(tmp,val21); val21 = std::max(tmp,val21);
- tmp = std::min(val23,val24); val24 = std::max(val23,val24); val23 = tmp; tmp = std::min(val2,val5);
- val5 = std::max(val2,val5); val2 = tmp; tmp = std::min(val3,val6); val6 = std::max(val3,val6); val3 = tmp;
- tmp = std::min(val0,val6); val6 = std::max(val0,val6); val0 = std::min(tmp,val3); val3 = std::max(tmp,val3);
- tmp = std::min(val4,val7); val7 = std::max(val4,val7); val4 = tmp; tmp = std::min(val1,val7);
- val7 = std::max(val1,val7); val1 = std::min(tmp,val4); val4 = std::max(tmp,val4); tmp = std::min(val11,val14);
- val14 = std::max(val11,val14); val11 = tmp; tmp = std::min(val8,val14); val14 = std::max(val8,val14);
- val8 = std::min(tmp,val11); val11 = std::max(tmp,val11); tmp = std::min(val12,val15);
- val15 = std::max(val12,val15); val12 = tmp; tmp = std::min(val9,val15); val15 = std::max(val9,val15);
- val9 = std::min(tmp,val12); val12 = std::max(tmp,val12); tmp = std::min(val13,val16);
- val16 = std::max(val13,val16); val13 = tmp; tmp = std::min(val10,val16); val16 = std::max(val10,val16);
- val10 = std::min(tmp,val13); val13 = std::max(tmp,val13); tmp = std::min(val20,val23);
- val23 = std::max(val20,val23); val20 = tmp; tmp = std::min(val17,val23); val23 = std::max(val17,val23);
- val17 = std::min(tmp,val20); val20 = std::max(tmp,val20); tmp = std::min(val21,val24);
- val24 = std::max(val21,val24); val21 = tmp; tmp = std::min(val18,val24); val24 = std::max(val18,val24);
- val18 = std::min(tmp,val21); val21 = std::max(tmp,val21); tmp = std::min(val19,val22);
- val22 = std::max(val19,val22); val19 = tmp; val17 = std::max(val8,val17); tmp = std::min(val9,val18);
- val18 = std::max(val9,val18); val9 = tmp; tmp = std::min(val0,val18); val18 = std::max(val0,val18);
- val9 = std::max(tmp,val9); tmp = std::min(val10,val19); val19 = std::max(val10,val19); val10 = tmp;
- tmp = std::min(val1,val19); val19 = std::max(val1,val19); val1 = std::min(tmp,val10);
- val10 = std::max(tmp,val10); tmp = std::min(val11,val20); val20 = std::max(val11,val20); val11 = tmp;
- tmp = std::min(val2,val20); val20 = std::max(val2,val20); val11 = std::max(tmp,val11);
- tmp = std::min(val12,val21); val21 = std::max(val12,val21); val12 = tmp; tmp = std::min(val3,val21);
- val21 = std::max(val3,val21); val3 = std::min(tmp,val12); val12 = std::max(tmp,val12);
- tmp = std::min(val13,val22); val22 = std::max(val13,val22); val4 = std::min(val4,val22);
- val13 = std::max(val4,tmp); tmp = std::min(val4,tmp); val4 = tmp; tmp = std::min(val14,val23);
- val23 = std::max(val14,val23); val14 = tmp; tmp = std::min(val5,val23); val23 = std::max(val5,val23);
- val5 = std::min(tmp,val14); val14 = std::max(tmp,val14); tmp = std::min(val15,val24);
- val24 = std::max(val15,val24); val15 = tmp; val6 = std::min(val6,val24); tmp = std::min(val6,val15);
- val15 = std::max(val6,val15); val6 = tmp; tmp = std::min(val7,val16); val7 = std::min(tmp,val19);
- tmp = std::min(val13,val21); val15 = std::min(val15,val23); tmp = std::min(val7,tmp);
- val7 = std::min(tmp,val15); val9 = std::max(val1,val9); val11 = std::max(val3,val11);
- val17 = std::max(val5,val17); val17 = std::max(val11,val17); val17 = std::max(val9,val17);
- tmp = std::min(val4,val10); val10 = std::max(val4,val10); val4 = tmp; tmp = std::min(val6,val12);
- val12 = std::max(val6,val12); val6 = tmp; tmp = std::min(val7,val14); val14 = std::max(val7,val14);
- val7 = tmp; tmp = std::min(val4,val6); val6 = std::max(val4,val6); val7 = std::max(tmp,val7);
- tmp = std::min(val12,val14); val14 = std::max(val12,val14); val12 = tmp; val10 = std::min(val10,val14);
- tmp = std::min(val6,val7); val7 = std::max(val6,val7); val6 = tmp; tmp = std::min(val10,val12);
- val12 = std::max(val10,val12); val10 = std::max(val6,tmp); tmp = std::min(val6,tmp);
- val17 = std::max(tmp,val17); tmp = std::min(val12,val17); val17 = std::max(val12,val17); val12 = tmp;
- val7 = std::min(val7,val17); tmp = std::min(val7,val10); val10 = std::max(val7,val10); val7 = tmp;
- tmp = std::min(val12,val18); val18 = std::max(val12,val18); val12 = std::max(val7,tmp);
- val10 = std::min(val10,val18); tmp = std::min(val12,val20); val20 = std::max(val12,val20); val12 = tmp;
- tmp = std::min(val10,val20);
- return std::max(tmp,val12);
- }
- template<typename T>
- 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, T val12, T val13,
- T val14, T val15, T val16, T val17, T val18, T val19, T val20,
- T val21, T val22, T val23, T val24, T val25, T val26, T val27,
- T val28, T val29, T val30, T val31, T val32, T val33, T val34,
- T val35, T val36, T val37, T val38, T val39, T val40, T val41,
- T val42, T val43, T val44, T val45, T val46, T val47, T val48) {
- T tmp = std::min(val0,val32);
- val32 = std::max(val0,val32); val0 = tmp; tmp = std::min(val1,val33); val33 = std::max(val1,val33); val1 = tmp;
- tmp = std::min(val2,val34); val34 = std::max(val2,val34); val2 = tmp; tmp = std::min(val3,val35);
- val35 = std::max(val3,val35); val3 = tmp; tmp = std::min(val4,val36); val36 = std::max(val4,val36); val4 = tmp;
- tmp = std::min(val5,val37); val37 = std::max(val5,val37); val5 = tmp; tmp = std::min(val6,val38);
- val38 = std::max(val6,val38); val6 = tmp; tmp = std::min(val7,val39); val39 = std::max(val7,val39); val7 = tmp;
- tmp = std::min(val8,val40); val40 = std::max(val8,val40); val8 = tmp; tmp = std::min(val9,val41);
- val41 = std::max(val9,val41); val9 = tmp; tmp = std::min(val10,val42); val42 = std::max(val10,val42);
- val10 = tmp; tmp = std::min(val11,val43); val43 = std::max(val11,val43); val11 = tmp;
- tmp = std::min(val12,val44); val44 = std::max(val12,val44); val12 = tmp; tmp = std::min(val13,val45);
- val45 = std::max(val13,val45); val13 = tmp; tmp = std::min(val14,val46); val46 = std::max(val14,val46);
- val14 = tmp; tmp = std::min(val15,val47); val47 = std::max(val15,val47); val15 = tmp;
- tmp = std::min(val16,val48); val48 = std::max(val16,val48); val16 = tmp; tmp = std::min(val0,val16);
- val16 = std::max(val0,val16); val0 = tmp; tmp = std::min(val1,val17); val17 = std::max(val1,val17);
- val1 = tmp; tmp = std::min(val2,val18); val18 = std::max(val2,val18); val2 = tmp; tmp = std::min(val3,val19);
- val19 = std::max(val3,val19); val3 = tmp; tmp = std::min(val4,val20); val20 = std::max(val4,val20); val4 = tmp;
- tmp = std::min(val5,val21); val21 = std::max(val5,val21); val5 = tmp; tmp = std::min(val6,val22);
- val22 = std::max(val6,val22); val6 = tmp; tmp = std::min(val7,val23); val23 = std::max(val7,val23); val7 = tmp;
- tmp = std::min(val8,val24); val24 = std::max(val8,val24); val8 = tmp; tmp = std::min(val9,val25);
- val25 = std::max(val9,val25); val9 = tmp; tmp = std::min(val10,val26); val26 = std::max(val10,val26);
- val10 = tmp; tmp = std::min(val11,val27); val27 = std::max(val11,val27); val11 = tmp;
- tmp = std::min(val12,val28); val28 = std::max(val12,val28); val12 = tmp; tmp = std::min(val13,val29);
- val29 = std::max(val13,val29); val13 = tmp; tmp = std::min(val14,val30); val30 = std::max(val14,val30);
- val14 = tmp; tmp = std::min(val15,val31); val31 = std::max(val15,val31); val15 = tmp;
- tmp = std::min(val32,val48); val48 = std::max(val32,val48); val32 = tmp; tmp = std::min(val16,val32);
- val32 = std::max(val16,val32); val16 = tmp; tmp = std::min(val17,val33); val33 = std::max(val17,val33);
- val17 = tmp; tmp = std::min(val18,val34); val34 = std::max(val18,val34); val18 = tmp;
- tmp = std::min(val19,val35); val35 = std::max(val19,val35); val19 = tmp; tmp = std::min(val20,val36);
- val36 = std::max(val20,val36); val20 = tmp; tmp = std::min(val21,val37); val37 = std::max(val21,val37);
- val21 = tmp; tmp = std::min(val22,val38); val38 = std::max(val22,val38); val22 = tmp;
- tmp = std::min(val23,val39); val39 = std::max(val23,val39); val23 = tmp; tmp = std::min(val24,val40);
- val40 = std::max(val24,val40); val24 = tmp; tmp = std::min(val25,val41); val41 = std::max(val25,val41);
- val25 = tmp; tmp = std::min(val26,val42); val42 = std::max(val26,val42); val26 = tmp;
- tmp = std::min(val27,val43); val43 = std::max(val27,val43); val27 = tmp; tmp = std::min(val28,val44);
- val44 = std::max(val28,val44); val28 = tmp; tmp = std::min(val29,val45); val45 = std::max(val29,val45);
- val29 = tmp; tmp = std::min(val30,val46); val46 = std::max(val30,val46); val30 = tmp;
- tmp = std::min(val31,val47); val47 = std::max(val31,val47); val31 = tmp; tmp = std::min(val0,val8);
- val8 = std::max(val0,val8); val0 = tmp; tmp = std::min(val1,val9); val9 = std::max(val1,val9); val1 = tmp;
- tmp = std::min(val2,val10); val10 = std::max(val2,val10); val2 = tmp; tmp = std::min(val3,val11);
- val11 = std::max(val3,val11); val3 = tmp; tmp = std::min(val4,val12); val12 = std::max(val4,val12); val4 = tmp;
- tmp = std::min(val5,val13); val13 = std::max(val5,val13); val5 = tmp; tmp = std::min(val6,val14);
- val14 = std::max(val6,val14); val6 = tmp; tmp = std::min(val7,val15); val15 = std::max(val7,val15); val7 = tmp;
- tmp = std::min(val16,val24); val24 = std::max(val16,val24); val16 = tmp; tmp = std::min(val17,val25);
- val25 = std::max(val17,val25); val17 = tmp; tmp = std::min(val18,val26); val26 = std::max(val18,val26);
- val18 = tmp; tmp = std::min(val19,val27); val27 = std::max(val19,val27); val19 = tmp;
- tmp = std::min(val20,val28); val28 = std::max(val20,val28); val20 = tmp; tmp = std::min(val21,val29);
- val29 = std::max(val21,val29); val21 = tmp; tmp = std::min(val22,val30); val30 = std::max(val22,val30);
- val22 = tmp; tmp = std::min(val23,val31); val31 = std::max(val23,val31); val23 = tmp;
- tmp = std::min(val32,val40); val40 = std::max(val32,val40); val32 = tmp; tmp = std::min(val33,val41);
- val41 = std::max(val33,val41); val33 = tmp; tmp = std::min(val34,val42); val42 = std::max(val34,val42);
- val34 = tmp; tmp = std::min(val35,val43); val43 = std::max(val35,val43); val35 = tmp;
- tmp = std::min(val36,val44); val44 = std::max(val36,val44); val36 = tmp; tmp = std::min(val37,val45);
- val45 = std::max(val37,val45); val37 = tmp; tmp = std::min(val38,val46); val46 = std::max(val38,val46);
- val38 = tmp; tmp = std::min(val39,val47); val47 = std::max(val39,val47); val39 = tmp;
- tmp = std::min(val8,val32); val32 = std::max(val8,val32); val8 = tmp; tmp = std::min(val9,val33);
- val33 = std::max(val9,val33); val9 = tmp; tmp = std::min(val10,val34); val34 = std::max(val10,val34);
- val10 = tmp; tmp = std::min(val11,val35); val35 = std::max(val11,val35); val11 = tmp;
- tmp = std::min(val12,val36); val36 = std::max(val12,val36); val12 = tmp; tmp = std::min(val13,val37);
- val37 = std::max(val13,val37); val13 = tmp; tmp = std::min(val14,val38); val38 = std::max(val14,val38);
- val14 = tmp; tmp = std::min(val15,val39); val39 = std::max(val15,val39); val15 = tmp;
- tmp = std::min(val24,val48); val48 = std::max(val24,val48); val24 = tmp; tmp = std::min(val8,val16);
- val16 = std::max(val8,val16); val8 = tmp; tmp = std::min(val9,val17); val17 = std::max(val9,val17);
- val9 = tmp; tmp = std::min(val10,val18); val18 = std::max(val10,val18); val10 = tmp;
- tmp = std::min(val11,val19); val19 = std::max(val11,val19); val11 = tmp; tmp = std::min(val12,val20);
- val20 = std::max(val12,val20); val12 = tmp; tmp = std::min(val13,val21); val21 = std::max(val13,val21);
- val13 = tmp; tmp = std::min(val14,val22); val22 = std::max(val14,val22); val14 = tmp;
- tmp = std::min(val15,val23); val23 = std::max(val15,val23); val15 = tmp; tmp = std::min(val24,val32);
- val32 = std::max(val24,val32); val24 = tmp; tmp = std::min(val25,val33); val33 = std::max(val25,val33);
- val25 = tmp; tmp = std::min(val26,val34); val34 = std::max(val26,val34); val26 = tmp;
- tmp = std::min(val27,val35); val35 = std::max(val27,val35); val27 = tmp; tmp = std::min(val28,val36);
- val36 = std::max(val28,val36); val28 = tmp; tmp = std::min(val29,val37); val37 = std::max(val29,val37);
- val29 = tmp; tmp = std::min(val30,val38); val38 = std::max(val30,val38); val30 = tmp;
- tmp = std::min(val31,val39); val39 = std::max(val31,val39); val31 = tmp; tmp = std::min(val40,val48);
- val48 = std::max(val40,val48); val40 = tmp; tmp = std::min(val0,val4); val4 = std::max(val0,val4);
- val0 = tmp; tmp = std::min(val1,val5); val5 = std::max(val1,val5); val1 = tmp; tmp = std::min(val2,val6);
- val6 = std::max(val2,val6); val2 = tmp; tmp = std::min(val3,val7); val7 = std::max(val3,val7); val3 = tmp;
- tmp = std::min(val8,val12); val12 = std::max(val8,val12); val8 = tmp; tmp = std::min(val9,val13);
- val13 = std::max(val9,val13); val9 = tmp; tmp = std::min(val10,val14); val14 = std::max(val10,val14);
- val10 = tmp; tmp = std::min(val11,val15); val15 = std::max(val11,val15); val11 = tmp;
- tmp = std::min(val16,val20); val20 = std::max(val16,val20); val16 = tmp; tmp = std::min(val17,val21);
- val21 = std::max(val17,val21); val17 = tmp; tmp = std::min(val18,val22); val22 = std::max(val18,val22);
- val18 = tmp; tmp = std::min(val19,val23); val23 = std::max(val19,val23); val19 = tmp;
- tmp = std::min(val24,val28); val28 = std::max(val24,val28); val24 = tmp; tmp = std::min(val25,val29);
- val29 = std::max(val25,val29); val25 = tmp; tmp = std::min(val26,val30); val30 = std::max(val26,val30);
- val26 = tmp; tmp = std::min(val27,val31); val31 = std::max(val27,val31); val27 = tmp;
- tmp = std::min(val32,val36); val36 = std::max(val32,val36); val32 = tmp; tmp = std::min(val33,val37);
- val37 = std::max(val33,val37); val33 = tmp; tmp = std::min(val34,val38); val38 = std::max(val34,val38);
- val34 = tmp; tmp = std::min(val35,val39); val39 = std::max(val35,val39); val35 = tmp;
- tmp = std::min(val40,val44); val44 = std::max(val40,val44); val40 = tmp; tmp = std::min(val41,val45);
- val45 = std::max(val41,val45); val41 = tmp; tmp = std::min(val42,val46); val46 = std::max(val42,val46);
- val42 = tmp; tmp = std::min(val43,val47); val47 = std::max(val43,val47); val43 = tmp;
- tmp = std::min(val4,val32); val32 = std::max(val4,val32); val4 = tmp; tmp = std::min(val5,val33);
- val33 = std::max(val5,val33); val5 = tmp; tmp = std::min(val6,val34); val34 = std::max(val6,val34);
- val6 = tmp; tmp = std::min(val7,val35); val35 = std::max(val7,val35); val7 = tmp;
- tmp = std::min(val12,val40); val40 = std::max(val12,val40); val12 = tmp; tmp = std::min(val13,val41);
- val41 = std::max(val13,val41); val13 = tmp; tmp = std::min(val14,val42); val42 = std::max(val14,val42);
- val14 = tmp; tmp = std::min(val15,val43); val43 = std::max(val15,val43); val15 = tmp;
- tmp = std::min(val20,val48); val48 = std::max(val20,val48); val20 = tmp; tmp = std::min(val4,val16);
- val16 = std::max(val4,val16); val4 = tmp; tmp = std::min(val5,val17); val17 = std::max(val5,val17);
- val5 = tmp; tmp = std::min(val6,val18); val18 = std::max(val6,val18); val6 = tmp;
- tmp = std::min(val7,val19); val19 = std::max(val7,val19); val7 = tmp; tmp = std::min(val12,val24);
- val24 = std::max(val12,val24); val12 = tmp; tmp = std::min(val13,val25); val25 = std::max(val13,val25);
- val13 = tmp; tmp = std::min(val14,val26); val26 = std::max(val14,val26); val14 = tmp;
- tmp = std::min(val15,val27); val27 = std::max(val15,val27); val15 = tmp; tmp = std::min(val20,val32);
- val32 = std::max(val20,val32); val20 = tmp; tmp = std::min(val21,val33); val33 = std::max(val21,val33);
- val21 = tmp; tmp = std::min(val22,val34); val34 = std::max(val22,val34); val22 = tmp;
- tmp = std::min(val23,val35); val35 = std::max(val23,val35); val23 = tmp; tmp = std::min(val28,val40);
- val40 = std::max(val28,val40); val28 = tmp; tmp = std::min(val29,val41); val41 = std::max(val29,val41);
- val29 = tmp; tmp = std::min(val30,val42); val42 = std::max(val30,val42); val30 = tmp;
- tmp = std::min(val31,val43); val43 = std::max(val31,val43); val31 = tmp; tmp = std::min(val36,val48);
- val48 = std::max(val36,val48); val36 = tmp; tmp = std::min(val4,val8); val8 = std::max(val4,val8);
- val4 = tmp; tmp = std::min(val5,val9); val9 = std::max(val5,val9); val5 = tmp; tmp = std::min(val6,val10);
- val10 = std::max(val6,val10); val6 = tmp; tmp = std::min(val7,val11); val11 = std::max(val7,val11); val7 = tmp;
- tmp = std::min(val12,val16); val16 = std::max(val12,val16); val12 = tmp; tmp = std::min(val13,val17);
- val17 = std::max(val13,val17); val13 = tmp; tmp = std::min(val14,val18); val18 = std::max(val14,val18);
- val14 = tmp; tmp = std::min(val15,val19); val19 = std::max(val15,val19); val15 = tmp;
- tmp = std::min(val20,val24); val24 = std::max(val20,val24); val20 = tmp; tmp = std::min(val21,val25);
- val25 = std::max(val21,val25); val21 = tmp; tmp = std::min(val22,val26); val26 = std::max(val22,val26);
- val22 = tmp; tmp = std::min(val23,val27); val27 = std::max(val23,val27); val23 = tmp;
- tmp = std::min(val28,val32); val32 = std::max(val28,val32); val28 = tmp; tmp = std::min(val29,val33);
- val33 = std::max(val29,val33); val29 = tmp; tmp = std::min(val30,val34); val34 = std::max(val30,val34);
- val30 = tmp; tmp = std::min(val31,val35); val35 = std::max(val31,val35); val31 = tmp;
- tmp = std::min(val36,val40); val40 = std::max(val36,val40); val36 = tmp; tmp = std::min(val37,val41);
- val41 = std::max(val37,val41); val37 = tmp; tmp = std::min(val38,val42); val42 = std::max(val38,val42);
- val38 = tmp; tmp = std::min(val39,val43); val43 = std::max(val39,val43); val39 = tmp;
- tmp = std::min(val44,val48); val48 = std::max(val44,val48); val44 = tmp; tmp = std::min(val0,val2);
- val2 = std::max(val0,val2); val0 = tmp; tmp = std::min(val1,val3); val3 = std::max(val1,val3); val1 = tmp;
- tmp = std::min(val4,val6); val6 = std::max(val4,val6); val4 = tmp; tmp = std::min(val5,val7);
- val7 = std::max(val5,val7); val5 = tmp; tmp = std::min(val8,val10); val10 = std::max(val8,val10); val8 = tmp;
- tmp = std::min(val9,val11); val11 = std::max(val9,val11); val9 = tmp; tmp = std::min(val12,val14);
- val14 = std::max(val12,val14); val12 = tmp; tmp = std::min(val13,val15); val15 = std::max(val13,val15);
- val13 = tmp; tmp = std::min(val16,val18); val18 = std::max(val16,val18); val16 = tmp;
- tmp = std::min(val17,val19); val19 = std::max(val17,val19); val17 = tmp; tmp = std::min(val20,val22);
- val22 = std::max(val20,val22); val20 = tmp; tmp = std::min(val21,val23); val23 = std::max(val21,val23);
- val21 = tmp; tmp = std::min(val24,val26); val26 = std::max(val24,val26); val24 = tmp;
- tmp = std::min(val25,val27); val27 = std::max(val25,val27); val25 = tmp; tmp = std::min(val28,val30);
- val30 = std::max(val28,val30); val28 = tmp; tmp = std::min(val29,val31); val31 = std::max(val29,val31);
- val29 = tmp; tmp = std::min(val32,val34); val34 = std::max(val32,val34); val32 = tmp;
- tmp = std::min(val33,val35); val35 = std::max(val33,val35); val33 = tmp; tmp = std::min(val36,val38);
- val38 = std::max(val36,val38); val36 = tmp; tmp = std::min(val37,val39); val39 = std::max(val37,val39);
- val37 = tmp; tmp = std::min(val40,val42); val42 = std::max(val40,val42); val40 = tmp;
- tmp = std::min(val41,val43); val43 = std::max(val41,val43); val41 = tmp; tmp = std::min(val44,val46);
- val46 = std::max(val44,val46); val44 = tmp; tmp = std::min(val45,val47); val47 = std::max(val45,val47);
- val45 = tmp; tmp = std::min(val2,val32); val32 = std::max(val2,val32); val2 = tmp; tmp = std::min(val3,val33);
- val33 = std::max(val3,val33); val3 = tmp; tmp = std::min(val6,val36); val36 = std::max(val6,val36); val6 = tmp;
- tmp = std::min(val7,val37); val37 = std::max(val7,val37); val7 = tmp; tmp = std::min(val10,val40);
- val40 = std::max(val10,val40); val10 = tmp; tmp = std::min(val11,val41); val41 = std::max(val11,val41);
- val11 = tmp; tmp = std::min(val14,val44); val44 = std::max(val14,val44); val14 = tmp;
- tmp = std::min(val15,val45); val45 = std::max(val15,val45); val15 = tmp; tmp = std::min(val18,val48);
- val48 = std::max(val18,val48); val18 = tmp; tmp = std::min(val2,val16); val16 = std::max(val2,val16);
- val2 = tmp; tmp = std::min(val3,val17); val17 = std::max(val3,val17); val3 = tmp;
- tmp = std::min(val6,val20); val20 = std::max(val6,val20); val6 = tmp; tmp = std::min(val7,val21);
- val21 = std::max(val7,val21); val7 = tmp; tmp = std::min(val10,val24); val24 = std::max(val10,val24);
- val10 = tmp; tmp = std::min(val11,val25); val25 = std::max(val11,val25); val11 = tmp;
- tmp = std::min(val14,val28); val28 = std::max(val14,val28); val14 = tmp; tmp = std::min(val15,val29);
- val29 = std::max(val15,val29); val15 = tmp; tmp = std::min(val18,val32); val32 = std::max(val18,val32);
- val18 = tmp; tmp = std::min(val19,val33); val33 = std::max(val19,val33); val19 = tmp;
- tmp = std::min(val22,val36); val36 = std::max(val22,val36); val22 = tmp; tmp = std::min(val23,val37);
- val37 = std::max(val23,val37); val23 = tmp; tmp = std::min(val26,val40); val40 = std::max(val26,val40);
- val26 = tmp; tmp = std::min(val27,val41); val41 = std::max(val27,val41); val27 = tmp;
- tmp = std::min(val30,val44); val44 = std::max(val30,val44); val30 = tmp; tmp = std::min(val31,val45);
- val45 = std::max(val31,val45); val31 = tmp; tmp = std::min(val34,val48); val48 = std::max(val34,val48);
- val34 = tmp; tmp = std::min(val2,val8); val8 = std::max(val2,val8); val2 = tmp; tmp = std::min(val3,val9);
- val9 = std::max(val3,val9); val3 = tmp; tmp = std::min(val6,val12); val12 = std::max(val6,val12); val6 = tmp;
- tmp = std::min(val7,val13); val13 = std::max(val7,val13); val7 = tmp; tmp = std::min(val10,val16);
- val16 = std::max(val10,val16); val10 = tmp; tmp = std::min(val11,val17); val17 = std::max(val11,val17);
- val11 = tmp; tmp = std::min(val14,val20); val20 = std::max(val14,val20); val14 = tmp;
- tmp = std::min(val15,val21); val21 = std::max(val15,val21); val15 = tmp; tmp = std::min(val18,val24);
- val24 = std::max(val18,val24); val18 = tmp; tmp = std::min(val19,val25); val25 = std::max(val19,val25);
- val19 = tmp; tmp = std::min(val22,val28); val28 = std::max(val22,val28); val22 = tmp;
- tmp = std::min(val23,val29); val29 = std::max(val23,val29); val23 = tmp; tmp = std::min(val26,val32);
- val32 = std::max(val26,val32); val26 = tmp; tmp = std::min(val27,val33); val33 = std::max(val27,val33);
- val27 = tmp; tmp = std::min(val30,val36); val36 = std::max(val30,val36); val30 = tmp;
- tmp = std::min(val31,val37); val37 = std::max(val31,val37); val31 = tmp; tmp = std::min(val34,val40);
- val40 = std::max(val34,val40); val34 = tmp; tmp = std::min(val35,val41); val41 = std::max(val35,val41);
- val35 = tmp; tmp = std::min(val38,val44); val44 = std::max(val38,val44); val38 = tmp;
- tmp = std::min(val39,val45); val45 = std::max(val39,val45); val39 = tmp; tmp = std::min(val42,val48);
- val48 = std::max(val42,val48); val42 = tmp; tmp = std::min(val2,val4); val4 = std::max(val2,val4);
- val2 = tmp; tmp = std::min(val3,val5); val5 = std::max(val3,val5); val3 = tmp; tmp = std::min(val6,val8);
- val8 = std::max(val6,val8); val6 = tmp; tmp = std::min(val7,val9); val9 = std::max(val7,val9); val7 = tmp;
- tmp = std::min(val10,val12); val12 = std::max(val10,val12); val10 = tmp; tmp = std::min(val11,val13);
- val13 = std::max(val11,val13); val11 = tmp; tmp = std::min(val14,val16); val16 = std::max(val14,val16);
- val14 = tmp; tmp = std::min(val15,val17); val17 = std::max(val15,val17); val15 = tmp;
- tmp = std::min(val18,val20); val20 = std::max(val18,val20); val18 = tmp; tmp = std::min(val19,val21);
- val21 = std::max(val19,val21); val19 = tmp; tmp = std::min(val22,val24); val24 = std::max(val22,val24);
- val22 = tmp; tmp = std::min(val23,val25); val25 = std::max(val23,val25); val23 = tmp;
- tmp = std::min(val26,val28); val28 = std::max(val26,val28); val26 = tmp; tmp = std::min(val27,val29);
- val29 = std::max(val27,val29); val27 = tmp; tmp = std::min(val30,val32); val32 = std::max(val30,val32);
- val30 = tmp; tmp = std::min(val31,val33); val33 = std::max(val31,val33); val31 = tmp;
- tmp = std::min(val34,val36); val36 = std::max(val34,val36); val34 = tmp; tmp = std::min(val35,val37);
- val37 = std::max(val35,val37); val35 = tmp; tmp = std::min(val38,val40); val40 = std::max(val38,val40);
- val38 = tmp; tmp = std::min(val39,val41); val41 = std::max(val39,val41); val39 = tmp;
- tmp = std::min(val42,val44); val44 = std::max(val42,val44); val42 = tmp; tmp = std::min(val43,val45);
- val45 = std::max(val43,val45); val43 = tmp; tmp = std::min(val46,val48); val48 = std::max(val46,val48);
- val46 = tmp; val1 = std::max(val0,val1); val3 = std::max(val2,val3); val5 = std::max(val4,val5);
- val7 = std::max(val6,val7); val9 = std::max(val8,val9); val11 = std::max(val10,val11);
- val13 = std::max(val12,val13); val15 = std::max(val14,val15); val17 = std::max(val16,val17);
- val19 = std::max(val18,val19); val21 = std::max(val20,val21); val23 = std::max(val22,val23);
- val24 = std::min(val24,val25); val26 = std::min(val26,val27); val28 = std::min(val28,val29);
- val30 = std::min(val30,val31); val32 = std::min(val32,val33); val34 = std::min(val34,val35);
- val36 = std::min(val36,val37); val38 = std::min(val38,val39); val40 = std::min(val40,val41);
- val42 = std::min(val42,val43); val44 = std::min(val44,val45); val46 = std::min(val46,val47);
- val32 = std::max(val1,val32); val34 = std::max(val3,val34); val36 = std::max(val5,val36);
- val38 = std::max(val7,val38); val9 = std::min(val9,val40); val11 = std::min(val11,val42);
- val13 = std::min(val13,val44); val15 = std::min(val15,val46); val17 = std::min(val17,val48);
- val24 = std::max(val9,val24); val26 = std::max(val11,val26); val28 = std::max(val13,val28);
- val30 = std::max(val15,val30); val17 = std::min(val17,val32); val19 = std::min(val19,val34);
- val21 = std::min(val21,val36); val23 = std::min(val23,val38); val24 = std::max(val17,val24);
- val26 = std::max(val19,val26); val21 = std::min(val21,val28); val23 = std::min(val23,val30);
- val24 = std::max(val21,val24); val23 = std::min(val23,val26);
- return std::max(val23,val24);
- }
- //! Return sqrt(x^2 + y^2).
- template<typename T>
- inline T hypot(const T x, const T y) {
- return std::sqrt(x*x + y*y);
- }
- template<typename T>
- inline T hypot(const T x, const T y, const T z) {
- return std::sqrt(x*x + y*y + z*z);
- }
- template<typename T>
- inline T _hypot(const T x, const T y) { // Slower but more precise version
- T nx = cimg::abs(x), ny = cimg::abs(y), t;
- if (nx<ny) { t = nx; nx = ny; } else t = ny;
- if (nx>0) { t/=nx; return nx*std::sqrt(1 + t*t); }
- return 0;
- }
- //! Return the factorial of n
- inline double factorial(const int n) {
- if (n<0) return cimg::type<double>::nan();
- if (n<2) return 1;
- double res = 2;
- for (int i = 3; i<=n; ++i) res*=i;
- return res;
- }
- //! Return the number of permutations of k objects in a set of n objects.
- inline double permutations(const int k, const int n, const bool with_order) {
- if (n<0 || k<0) return cimg::type<double>::nan();
- if (k>n) return 0;
- double res = 1;
- for (int i = n; i>=n - k + 1; --i) res*=i;
- return with_order?res:res/cimg::factorial(k);
- }
- inline double _fibonacci(int exp) {
- double
- base = (1 + std::sqrt(5.))/2,
- result = 1/std::sqrt(5.);
- while (exp) {
- if (exp&1) result*=base;
- exp>>=1;
- base*=base;
- }
- return result;
- }
- //! Calculate fibonacci number.
- // (Precise up to n = 78, less precise for n>78).
- inline double fibonacci(const int n) {
- if (n<0) return cimg::type<double>::nan();
- if (n<3) return 1;
- if (n<11) {
- cimg_uint64 fn1 = 1, fn2 = 1, fn = 0;
- for (int i = 3; i<=n; ++i) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; }
- return (double)fn;
- }
- if (n<75) // precise up to n = 74, faster than the integer calculation above for n>10
- return (double)((cimg_uint64)(_fibonacci(n) + 0.5));
- if (n<94) { // precise up to n = 78, less precise for n>78 up to n = 93, overflows for n>93
- cimg_uint64
- fn1 = ((cimg_uint64)303836)<<32 | 3861581201UL, // 1304969544928657ULL (avoid C++98 warning with ULL)
- fn2 = ((cimg_uint64)187781)<<32 | 2279239217UL, // 806515533049393ULL
- fn = 0;
- for (int i = 75; i<=n; ++i) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; }
- return (double)fn;
- }
- return _fibonacci(n); // Not precise, but better than the wrong overflowing calculation
- }
- //! Calculate greatest common divisor.
- inline long gcd(long a, long b) {
- while (a) { const long c = a; a = b%a; b = c; }
- return b;
- }
- //! Convert character to lower case.
- inline char lowercase(const char x) {
- return (char)((x<'A'||x>'Z')?x:x - 'A' + 'a');
- }
- inline double lowercase(const double x) {
- return (double)((x<'A'||x>'Z')?x:x - 'A' + 'a');
- }
- //! Convert C-string to lower case.
- inline void lowercase(char *const str) {
- if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = lowercase(*ptr);
- }
- //! Convert character to upper case.
- inline char uppercase(const char x) {
- return (char)((x<'a'||x>'z')?x:x - 'a' + 'A');
- }
- inline double uppercase(const double x) {
- return (double)((x<'a'||x>'z')?x:x - 'a' + 'A');
- }
- //! Convert C-string to upper case.
- inline void uppercase(char *const str) {
- if (str) for (char *ptr = str; *ptr; ++ptr) *ptr = uppercase(*ptr);
- }
- //! Return \c true if input character is blank (space, tab, or non-printable character).
- inline bool is_blank(const char c) {
- return c>=0 && (unsigned char)c<=' ';
- }
- //! Read value in a C-string.
- /**
- \param str C-string containing the float value to read.
- \return Read value.
- \note Same as <tt>std::atof()</tt> extended to manage the retrieval of fractions from C-strings,
- as in <em>"1/2"</em>.
- **/
- inline double atof(const char *const str) {
- double x = 0, y = 1;
- return str && cimg_sscanf(str,"%lf/%lf",&x,&y)>0?x/y:0;
- }
- //! Compare the first \p l characters of two C-strings, ignoring the case.
- /**
- \param str1 C-string.
- \param str2 C-string.
- \param l Number of characters to compare.
- \return \c 0 if the two strings are equal, something else otherwise.
- \note This function has to be defined since it is not provided by all C++-compilers (not ANSI).
- **/
- inline int strncasecmp(const char *const str1, const char *const str2, const int l) {
- if (!l) return 0;
- if (!str1) return str2?-1:0;
- const char *nstr1 = str1, *nstr2 = str2;
- int k, diff = 0; for (k = 0; k<l && !(diff = lowercase(*nstr1) - lowercase(*nstr2)); ++k) { ++nstr1; ++nstr2; }
- return k!=l?diff:0;
- }
- //! Compare two C-strings, ignoring the case.
- /**
- \param str1 C-string.
- \param str2 C-string.
- \return \c 0 if the two strings are equal, something else otherwise.
- \note This function has to be defined since it is not provided by all C++-compilers (not ANSI).
- **/
- inline int strcasecmp(const char *const str1, const char *const str2) {
- if (!str1) return str2?-1:0;
- const int
- l1 = (int)std::strlen(str1),
- l2 = (int)std::strlen(str2);
- return cimg::strncasecmp(str1,str2,1 + (l1<l2?l1:l2));
- }
- //! Ellipsize a string.
- /**
- \param str C-string.
- \param l Max number of characters.
- \param is_ending Tell if the dots are placed at the end or at the center of the ellipsized string.
- **/
- inline char *strellipsize(char *const str, const unsigned int l=64,
- const bool is_ending=true) {
- if (!str) return str;
- const unsigned int nl = l<5?5:l, ls = (unsigned int)std::strlen(str);
- if (ls<=nl) return str;
- if (is_ending) std::strcpy(str + nl - 5,"(...)");
- else {
- const unsigned int ll = (nl - 5)/2 + 1 - (nl%2), lr = nl - ll - 5;
- std::strcpy(str + ll,"(...)");
- std::memmove(str + ll + 5,str + ls - lr,lr);
- }
- str[nl] = 0;
- return str;
- }
- //! Ellipsize a string.
- /**
- \param str C-string.
- \param res output C-string.
- \param l Max number of characters.
- \param is_ending Tell if the dots are placed at the end or at the center of the ellipsized string.
- **/
- inline char *strellipsize(const char *const str, char *const res, const unsigned int l=64,
- const bool is_ending=true) {
- const unsigned int nl = l<5?5:l, ls = (unsigned int)std::strlen(str);
- if (ls<=nl) { std::strcpy(res,str); return res; }
- if (is_ending) {
- std::strncpy(res,str,nl - 5);
- std::strcpy(res + nl -5,"(...)");
- } else {
- const unsigned int ll = (nl - 5)/2 + 1 - (nl%2), lr = nl - ll - 5;
- std::strncpy(res,str,ll);
- std::strcpy(res + ll,"(...)");
- std::strncpy(res + ll + 5,str + ls - lr,lr);
- }
- res[nl] = 0;
- return res;
- }
- //! Remove delimiters on the start and/or end of a C-string.
- /**
- \param[in,out] str C-string to work with (modified at output).
- \param delimiter Delimiter character code to remove.
- \param is_symmetric Tells if the removal is done only if delimiters are symmetric
- (both at the beginning and the end of \c s).
- \param is_iterative Tells if the removal is done if several iterations are possible.
- \return \c true if delimiters have been removed, \c false otherwise.
- **/
- inline bool strpare(char *const str, const char delimiter,
- const bool is_symmetric, const bool is_iterative) {
- if (!str) return false;
- const int l = (int)std::strlen(str);
- int p, q;
- if (is_symmetric) for (p = 0, q = l - 1; p<q && str[p]==delimiter && str[q]==delimiter; ) {
- --q; ++p; if (!is_iterative) break;
- } else {
- for (p = 0; p<l && str[p]==delimiter; ) { ++p; if (!is_iterative) break; }
- for (q = l - 1; q>p && str[q]==delimiter; ) { --q; if (!is_iterative) break; }
- }
- const int n = q - p + 1;
- if (n!=l) { std::memmove(str,str + p,(unsigned int)n); str[n] = 0; return true; }
- return false;
- }
- //! Remove white spaces on the start and/or end of a C-string.
- inline bool strpare(char *const str, const bool is_symmetric, const bool is_iterative) {
- if (!str) return false;
- const int l = (int)std::strlen(str);
- int p, q;
- if (is_symmetric) for (p = 0, q = l - 1; p<q && is_blank(str[p]) && is_blank(str[q]); ) {
- --q; ++p; if (!is_iterative) break;
- } else {
- for (p = 0; p<l && is_blank(str[p]); ) { ++p; if (!is_iterative) break; }
- for (q = l - 1; q>p && is_blank(str[q]); ) { --q; if (!is_iterative) break; }
- }
- const int n = q - p + 1;
- if (n!=l) { std::memmove(str,str + p,(unsigned int)n); str[n] = 0; return true; }
- return false;
- }
- //! Replace reserved characters (for Windows filename) by another character.
- /**
- \param[in,out] str C-string to work with (modified at output).
- \param[in] c Replacement character.
- **/
- inline void strwindows_reserved(char *const str, const char c='_') {
- for (char *s = str; *s; ++s) {
- const char i = *s;
- if (i=='<' || i=='>' || i==':' || i=='\"' || i=='/' || i=='\\' || i=='|' || i=='?' || i=='*') *s = c;
- }
- }
- //! Replace escape sequences in C-strings by character values.
- /**
- \param[in,out] str C-string to work with (modified at output).
- **/
- inline void strunescape(char *const str) {
- #define cimg_strunescape(ci,co) case ci : *nd = co; ++ns; break;
- unsigned char val = 0;
- for (char *ns = str, *nd = str; *ns || (bool)(*nd = 0); ++nd) if (*ns=='\\') switch (*(++ns)) {
- cimg_strunescape('a','\a');
- cimg_strunescape('b','\b');
- cimg_strunescape('e',0x1B);
- cimg_strunescape('f','\f');
- cimg_strunescape('n','\n');
- cimg_strunescape('r','\r');
- cimg_strunescape('t','\t');
- cimg_strunescape('v','\v');
- cimg_strunescape('\\','\\');
- cimg_strunescape('\'','\'');
- cimg_strunescape('\"','\"');
- cimg_strunescape('\?','\?');
- case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' :
- val = (unsigned char)(*(ns++) - '0');
- if (*ns>='0' && *ns<='7') (val<<=3)|=*(ns++) - '0';
- if (*ns>='0' && *ns<='7') (val<<=3)|=*(ns++) - '0';
- *nd = (char)val;
- break;
- case 'x' : {
- char c = lowercase(*(++ns));
- if ((c>='0' && c<='9') || (c>='a' && c<='f')) {
- val = (unsigned char)(c<='9'?c - '0':c - 'a' + 10);
- c = lowercase(*(++ns));
- if ((c>='0' && c<='9') || (c>='a' && c<='f')) {
- (val<<=4)|=(c<='9'?c - '0':c - 'a' + 10);
- ++ns;
- }
- *nd = (char)val;
- } else *nd = c;
- } break;
- case 'u' : { // UTF-8 BMP
- char c1, c2, c3, c4;
- if ((((c1 = lowercase(ns[1]))>='0' && c1<='9') || (c1>='a' && c1<='f')) &&
- (((c2 = lowercase(ns[2]))>='0' && c2<='9') || (c2>='a' && c2<='f')) &&
- (((c3 = lowercase(ns[3]))>='0' && c3<='9') || (c3>='a' && c3<='f')) &&
- (((c4 = lowercase(ns[4]))>='0' && c4<='9') || (c4>='a' && c4<='f'))) {
- c1 = (c1<='9'?c1 - '0':c1 - 'a' + 10);
- c2 = (c2<='9'?c2 - '0':c2 - 'a' + 10);
- c3 = (c3<='9'?c3 - '0':c3 - 'a' + 10);
- c4 = (c4<='9'?c4 - '0':c4 - 'a' + 10);
- const unsigned int ival =
- ((unsigned int)c1<<12) | ((unsigned int)c2<<8) | ((unsigned int)c3<<4) | c4;
- if (ival<=0x007f) *nd = (char)ival;
- else if (ival<=0x07ff) {
- *(nd++) = (char)((ival>>6)|0xc0);
- *nd = (char)((ival&0x3f)|0x80);
- } else {
- *(nd++) = (char)((ival>>12)|0xe0);
- *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
- *nd = (char)((ival&0x3f)|0x80);
- }
- ns+=5;
- } else *nd = *(ns++);
- } break;
- case 'U' : { // UTF-8 astral planes
- char c1, c2, c3, c4, c5, c6, c7, c8;
- if ((((c1 = lowercase(ns[1]))>='0' && c1<='9') || (c1>='a' && c1<='f')) &&
- (((c2 = lowercase(ns[2]))>='0' && c2<='9') || (c2>='a' && c2<='f')) &&
- (((c3 = lowercase(ns[3]))>='0' && c3<='9') || (c3>='a' && c3<='f')) &&
- (((c4 = lowercase(ns[4]))>='0' && c4<='9') || (c4>='a' && c4<='f')) &&
- (((c5 = lowercase(ns[5]))>='0' && c5<='9') || (c5>='a' && c5<='f')) &&
- (((c6 = lowercase(ns[6]))>='0' && c6<='9') || (c6>='a' && c6<='f')) &&
- (((c7 = lowercase(ns[7]))>='0' && c7<='9') || (c7>='a' && c7<='f')) &&
- (((c8 = lowercase(ns[8]))>='0' && c8<='9') || (c8>='a' && c8<='f'))) {
- c1 = (c1<='9'?c1 - '0':c1 - 'a' + 10);
- c2 = (c2<='9'?c2 - '0':c2 - 'a' + 10);
- c3 = (c3<='9'?c3 - '0':c3 - 'a' + 10);
- c4 = (c4<='9'?c4 - '0':c4 - 'a' + 10);
- c5 = (c5<='9'?c5 - '0':c5 - 'a' + 10);
- c6 = (c6<='9'?c6 - '0':c6 - 'a' + 10);
- c7 = (c7<='9'?c7 - '0':c7 - 'a' + 10);
- c8 = (c8<='9'?c8 - '0':c8 - 'a' + 10);
- const unsigned int ival =
- ((unsigned int)c1<<28) | ((unsigned int)c2<<24) | ((unsigned int)c3<<20) | ((unsigned int)c4<<16) |
- ((unsigned int)c5<<12) | ((unsigned int)c6<<8) | ((unsigned int)c7<<4) | (unsigned int)c8;
- if (ival<=0x007f) *nd = (char)ival;
- else if (ival<=0x07ff) {
- *(nd++) = (char)((ival>>6)|0xc0);
- *nd = (char)((ival&0x3f)|0x80);
- } else if (ival<=0xffff) {
- *(nd++) = (char)((ival>>12)|0xe0);
- *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
- *nd = (char)((ival&0x3f)|0x80);
- } else {
- *(nd++) = (char)((ival>>18)|0xf0);
- *(nd++) = (char)(((ival>>12)&0x3f)|0x80);
- *(nd++) = (char)(((ival>>6)&0x3f)|0x80);
- *nd = (char)((ival&0x3f)|0x80);
- }
- ns+=9;
- } else *nd = *(ns++);
- } break;
- default : if (*ns) *nd = *(ns++);
- }
- else *nd = *(ns++);
- }
- // Return a temporary string describing the size of a memory buffer.
- inline const char *strbuffersize(const cimg_ulong size);
- // Return string that identifies the running OS.
- inline const char *stros() {
- #if defined(linux) || defined(__linux) || defined(__linux__)
- static const char *const str = "Linux";
- #elif defined(sun) || defined(__sun)
- static const char *const str = "Sun OS";
- #elif defined(BSD) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined (__DragonFly__)
- static const char *const str = "BSD";
- #elif defined(sgi) || defined(__sgi)
- static const char *const str = "Irix";
- #elif defined(__MACOSX__) || defined(__APPLE__)
- static const char *const str = "Mac OS";
- #elif defined(unix) || defined(__unix) || defined(__unix__)
- static const char *const str = "Generic Unix";
- #elif defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
- defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
- static const char *const str = "Windows";
- #else
- const char
- *const _str1 = std::getenv("OSTYPE"),
- *const _str2 = _str1?_str1:std::getenv("OS"),
- *const str = _str2?_str2:"Unknown OS";
- #endif
- return str;
- }
- //! Return the basename of a filename.
- inline const char* basename(const char *const s, const char separator=cimg_file_separator) {
- const char *p = 0, *np = s;
- while (np>=s && (p=np)) np = std::strchr(np,separator) + 1;
- return p;
- }
- // Return a random filename.
- inline const char* filenamerand() {
- cimg::mutex(6);
- static char randomid[9];
- for (unsigned int k = 0; k<8; ++k) {
- const int v = (int)cimg::rand(65535)%3;
- randomid[k] = (char)(v==0?('0' + ((int)cimg::rand(65535)%10)):
- (v==1?('a' + ((int)cimg::rand(65535)%26)):
- ('A' + ((int)cimg::rand(65535)%26))));
- }
- cimg::mutex(6,0);
- return randomid;
- }
- // Convert filename as a Windows-style filename (short path name).
- inline void winformat_string(char *const str) {
- if (str && *str) {
- #if cimg_OS==2
- char *const nstr = new char[MAX_PATH];
- if (GetShortPathNameA(str,nstr,MAX_PATH)) std::strcpy(str,nstr);
- delete[] nstr;
- #endif
- }
- }
- // Open a file (similar to std:: fopen(), but with wide character support on Windows).
- inline std::FILE *std_fopen(const char *const path, const char *const mode);
- //! Open a file.
- /**
- \param path Path of the filename to open.
- \param mode C-string describing the opening mode.
- \return Opened file.
- \note Same as <tt>std::fopen()</tt> but throw a \c CImgIOException when
- the specified file cannot be opened, instead of returning \c 0.
- **/
- inline std::FILE *fopen(const char *const path, const char *const mode) {
- if (!path)
- throw CImgArgumentException("cimg::fopen(): Specified file path is (null).");
- if (!mode)
- throw CImgArgumentException("cimg::fopen(): File '%s', specified mode is (null).",
- path);
- std::FILE *res = 0;
- if (*path=='-' && (!path[1] || path[1]=='.')) {
- res = (*mode=='r')?cimg::_stdin():cimg::_stdout();
- #if cimg_OS==2
- if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode
- #ifdef __BORLANDC__
- if (setmode(_fileno(res),0x8000)==-1) res = 0;
- #else
- if (_setmode(_fileno(res),0x8000)==-1) res = 0;
- #endif
- }
- #endif
- } else res = cimg::std_fopen(path,mode);
- if (!res) throw CImgIOException("cimg::fopen(): Failed to open file '%s' with mode '%s'.",
- path,mode);
- return res;
- }
- //! Close a file.
- /**
- \param file File to close.
- \return \c 0 if file has been closed properly, something else otherwise.
- \note Same as <tt>std::fclose()</tt> but display a warning message if
- the file has not been closed properly.
- **/
- inline int fclose(std::FILE *file) {
- if (!file) { warn("cimg::fclose(): Specified file is (null)."); return 0; }
- if (file==cimg::_stdin(false) || file==cimg::_stdout(false)) return 0;
- const int errn = std::fclose(file);
- if (errn!=0) warn("cimg::fclose(): Error code %d returned during file closing.",
- errn);
- return errn;
- }
- //! Version of 'fseek()' that supports >=64bits offsets everywhere (for Windows).
- inline int fseek(FILE *stream, cimg_long offset, int origin) {
- #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
- return _fseeki64(stream,(__int64)offset,origin);
- #else
- return std::fseek(stream,offset,origin);
- #endif
- }
- //! Version of 'ftell()' that supports >=64bits offsets everywhere (for Windows).
- inline cimg_long ftell(FILE *stream) {
- #if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
- return (cimg_long)_ftelli64(stream);
- #else
- return (cimg_long)std::ftell(stream);
- #endif
- }
- // Get the file or directory attributes with support for UTF-8 paths (Windows only).
- #if cimg_OS==2
- inline DWORD win_getfileattributes(const char *const path);
- #endif
- //! Check if a path is a directory.
- /**
- \param path Specified path to test.
- **/
- inline bool is_directory(const char *const path) {
- if (!path || !*path) return false;
- #if cimg_OS==1
- struct stat st_buf;
- return (!stat(path,&st_buf) && S_ISDIR(st_buf.st_mode));
- #elif cimg_OS==2
- const DWORD res = win_getfileattributes(path);
- return res!=INVALID_FILE_ATTRIBUTES && (res&FILE_ATTRIBUTE_DIRECTORY);
- #else
- return false;
- #endif
- }
- //! Check if a path is a file.
- /**
- \param path Specified path to test.
- **/
- inline bool is_file(const char *const path) {
- if (!path || !*path) return false;
- #if cimg_OS==2
- const DWORD res = cimg::win_getfileattributes(path);
- return res!=INVALID_FILE_ATTRIBUTES && !(res&FILE_ATTRIBUTE_DIRECTORY);
- #else
- std::FILE *const file = cimg::std_fopen(path,"rb");
- if (!file) return false;
- cimg::fclose(file);
- return !is_directory(path);
- #endif
- }
- //! Get file size.
- /**
- \param filename Specified filename to get size from.
- \return File size or '-1' if file does not exist.
- **/
- inline cimg_int64 fsize(const char *const filename) {
- std::FILE *const file = cimg::std_fopen(filename,"rb");
- if (!file) return (cimg_int64)-1;
- std::fseek(file,0,SEEK_END);
- const cimg_int64 siz = (cimg_int64)std::ftell(file);
- cimg::fclose(file);
- return siz;
- }
- //! Get last write time of a given file or directory (multiple-attributes version).
- /**
- \param path Specified path to get attributes from.
- \param[in,out] attr Type of requested time attributes.
- Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second }
- Replaced by read attributes after return (or -1 if an error occurred).
- \param nb_attr Number of attributes to read/write.
- \return Latest read attribute.
- **/
- template<typename T>
- inline int fdate(const char *const path, T *attr, const unsigned int nb_attr) {
- #define _cimg_fdate_err() for (unsigned int i = 0; i<nb_attr; ++i) attr[i] = (T)-1
- int res = -1;
- if (!path || !*path) { _cimg_fdate_err(); return -1; }
- cimg::mutex(6);
- #if cimg_OS==2
- HANDLE file = CreateFileA(path,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
- if (file!=INVALID_HANDLE_VALUE) {
- FILETIME _ft;
- SYSTEMTIME ft;
- if (GetFileTime(file,0,0,&_ft) && FileTimeToSystemTime(&_ft,&ft)) {
- for (unsigned int i = 0; i<nb_attr; ++i) {
- res = (int)(attr[i]==0?ft.wYear:attr[i]==1?ft.wMonth:attr[i]==2?ft.wDay:
- attr[i]==3?ft.wDayOfWeek:attr[i]==4?ft.wHour:attr[i]==5?ft.wMinute:
- attr[i]==6?ft.wSecond:-1);
- attr[i] = (T)res;
- }
- } else _cimg_fdate_err();
- CloseHandle(file);
- } else _cimg_fdate_err();
- #elif cimg_OS==1
- struct stat st_buf;
- if (!stat(path,&st_buf)) {
- const time_t _ft = st_buf.st_mtime;
- const struct tm& ft = *std::localtime(&_ft);
- for (unsigned int i = 0; i<nb_attr; ++i) {
- res = (int)(attr[i]==0?ft.tm_year + 1900:attr[i]==1?ft.tm_mon + 1:attr[i]==2?ft.tm_mday:
- attr[i]==3?ft.tm_wday:attr[i]==4?ft.tm_hour:attr[i]==5?ft.tm_min:
- attr[i]==6?ft.tm_sec:-1);
- attr[i] = (T)res;
- }
- } else _cimg_fdate_err();
- #endif
- cimg::mutex(6,0);
- return res;
- }
- //! Get last write time of a given file or directory (single-attribute version).
- /**
- \param path Specified path to get attributes from.
- \param attr Type of requested time attributes.
- Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second }
- \return Specified attribute or -1 if an error occurred.
- **/
- inline int fdate(const char *const path, unsigned int attr) {
- int out = (int)attr;
- return fdate(path,&out,1);
- }
- //! Get current local time (multiple-attributes version).
- /**
- \param[in,out] attr Type of requested time attributes.
- Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second |
- 7=millisecond }
- Replaced by read attributes after return (or -1 if an error occurred).
- \param nb_attr Number of attributes to read/write.
- \return Latest read attribute.
- **/
- template<typename T>
- inline int date(T *attr, const unsigned int nb_attr) {
- int res = -1;
- cimg::mutex(6);
- #if cimg_OS==2
- SYSTEMTIME st;
- GetLocalTime(&st);
- for (unsigned int i = 0; i<nb_attr; ++i) {
- res = (int)(attr[i]==0?st.wYear:
- attr[i]==1?st.wMonth:
- attr[i]==2?st.wDay:
- attr[i]==3?st.wDayOfWeek:
- attr[i]==4?st.wHour:
- attr[i]==5?st.wMinute:
- attr[i]==6?st.wSecond:
- attr[i]==7?st.wMilliseconds:-1);
- attr[i] = (T)res;
- }
- #else
- struct timeval _st;
- gettimeofday(&_st,0);
- struct tm *st = std::localtime(&_st.tv_sec);
- for (unsigned int i = 0; i<nb_attr; ++i) {
- res = (int)(attr[i]==0?st->tm_year + 1900:
- attr[i]==1?st->tm_mon + 1:
- attr[i]==2?st->tm_mday:
- attr[i]==3?st->tm_wday:
- attr[i]==4?st->tm_hour:
- attr[i]==5?st->tm_min:
- attr[i]==6?st->tm_sec:
- attr[i]==7?_st.tv_usec/1000:-1);
- attr[i] = (T)res;
- }
- #endif
- cimg::mutex(6,0);
- return res;
- }
- //! Get current local time (single-attribute version).
- /**
- \param attr Type of requested time attribute.
- Can be { 0=year | 1=month | 2=day | 3=day of week | 4=hour | 5=minute | 6=second |
- 7=millisecond }
- \return Specified attribute or -1 if an error occurred.
- **/
- inline int date(unsigned int attr) {
- int out = (int)attr;
- return date(&out,1);
- }
- // Get/set path to the \c curl binary.
- inline const char *curl_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the \c dcraw binary.
- inline const char *dcraw_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the FFMPEG's \c ffmpeg binary.
- inline const char *ffmpeg_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the GraphicsMagick's \c gm binary.
- inline const char* graphicsmagick_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the \c gunzip binary.
- inline const char *gunzip_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the \c gzip binary.
- inline const char *gzip_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the ImageMagick's \c convert binary.
- inline const char* imagemagick_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the Medcon's \c medcon binary.
- inline const char* medcon_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to store temporary files.
- inline const char* temporary_path(const char *const user_path=0, const bool reinit_path=false);
- // Get/set path to the \c wget binary.
- inline const char *wget_path(const char *const user_path=0, const bool reinit_path=false);
- //! Split filename into two C-strings \c body and \c extension.
- /**
- filename and body must not overlap!
- **/
- inline const char *split_filename(const char *const filename, char *const body=0) {
- if (!filename) { if (body) *body = 0; return ""; }
- const char * p = std::strrchr(filename,'.');
- if (!p || std::strchr(p,'/') || std::strchr(p,'\\')) { // No extension.
- if (body) std::strcpy(body,filename);
- return filename + std::strlen(filename);
- }
- const unsigned int l = (unsigned int)(p - filename);
- if (body) { if (l) std::memcpy(body,filename,l); body[l] = 0; }
- return p + 1;
- }
- // Generate a numbered version of a filename.
- inline char* number_filename(const char *const filename, const int number,
- const unsigned int digits, char *const str);
- //! Read data from file.
- /**
- \param[out] ptr Pointer to memory buffer that will contain the binary data read from file.
- \param nmemb Number of elements to read.
- \param stream File to read data from.
- \return Number of read elements.
- \note Same as <tt>std::fread()</tt> but may display warning message if all elements could not be read.
- **/
- template<typename T>
- inline size_t fread(T *const ptr, const size_t nmemb, std::FILE *stream) {
- if (!ptr || !stream)
- throw CImgArgumentException("cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.",
- nmemb,cimg::type<T>::string(),nmemb>1?"s":"",stream,ptr);
- if (!nmemb) return 0;
- const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);
- size_t to_read = nmemb, al_read = 0, l_to_read = 0, l_al_read = 0;
- do {
- l_to_read = (to_read*sizeof(T))<wlimitT?to_read:wlimit;
- l_al_read = std::fread((void*)(ptr + al_read),sizeof(T),l_to_read,stream);
- al_read+=l_al_read;
- to_read-=l_al_read;
- } while (l_to_read==l_al_read && to_read>0);
- if (to_read>0)
- warn("cimg::fread(): Only %lu/%lu elements could be read from file.",
- (unsigned long)al_read,(unsigned long)nmemb);
- return al_read;
- }
- //! Write data to file.
- /**
- \param ptr Pointer to memory buffer containing the binary data to write on file.
- \param nmemb Number of elements to write.
- \param[out] stream File to write data on.
- \return Number of written elements.
- \note Similar to <tt>std::fwrite</tt> but may display warning messages if all elements could not be written.
- **/
- template<typename T>
- inline size_t fwrite(const T *ptr, const size_t nmemb, std::FILE *stream) {
- if (!ptr || !stream)
- throw CImgArgumentException("cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.",
- nmemb,cimg::type<T>::string(),nmemb>1?"s":"",ptr,stream);
- if (!nmemb) return 0;
- const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);
- size_t to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0;
- do {
- l_to_write = (to_write*sizeof(T))<wlimitT?to_write:wlimit;
- l_al_write = std::fwrite((void*)(ptr + al_write),sizeof(T),l_to_write,stream);
- al_write+=l_al_write;
- to_write-=l_al_write;
- } while (l_to_write==l_al_write && to_write>0);
- if (to_write>0)
- warn("cimg::fwrite(): Only %lu/%lu elements could be written in file.",
- (unsigned long)al_write,(unsigned long)nmemb);
- return al_write;
- }
- //! Create an empty file.
- /**
- \param file Input file (can be \c 0 if \c filename is set).
- \param filename Filename, as a C-string (can be \c 0 if \c file is set).
- **/
- inline void fempty(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException("cimg::fempty(): Specified filename is (null).");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- if (!file) cimg::fclose(nfile);
- }
- // Try to guess format from an image file.
- inline const char *ftype(std::FILE *const file, const char *const filename);
- // Get or set load from network mode (can be { 0=disabled | 1=enabled }).
- inline bool& network_mode(const bool value, const bool is_set) {
- static bool mode = true;
- if (is_set) { cimg::mutex(0); mode = value; cimg::mutex(0,0); }
- return mode;
- }
- inline bool& network_mode() {
- return network_mode(false,false);
- }
- // Load file from network as a local temporary file.
- inline char *load_network(const char *const url, char *const filename_local,
- const unsigned int timeout=0, const bool try_fallback=false,
- const char *const referer=0);
- //! Return options specified on the command line.
- inline const char* option(const char *const name, const int argc, const char *const *const argv,
- const char *const _default, const char *const usage, const bool reset_static) {
- static bool first = true, visu = false;
- if (reset_static) { first = true; return 0; }
- const char *res = 0;
- if (first) {
- first = false;
- visu = cimg::option("-h",argc,argv,(char*)0,(char*)0,false)!=0;
- visu |= cimg::option("-help",argc,argv,(char*)0,(char*)0,false)!=0;
- visu |= cimg::option("--help",argc,argv,(char*)0,(char*)0,false)!=0;
- }
- if (!name && visu) {
- if (usage) {
- std::fprintf(cimg::output(),"\n %s%s%s",cimg::t_red,cimg::basename(argv[0]),cimg::t_normal);
- std::fprintf(cimg::output(),": %s",usage);
- std::fprintf(cimg::output()," (%s, %s)\n\n",cimg_date,cimg_time);
- }
- if (_default) std::fprintf(cimg::output(),"%s\n",_default);
- }
- if (name) {
- if (argc>0) {
- int k = 0;
- while (k<argc && std::strcmp(argv[k],name)) ++k;
- res = (k++==argc?_default:(k==argc?argv[--k]:argv[k]));
- } else res = _default;
- if (visu && usage) std::fprintf(cimg::output()," %s%-16s%s %-24s %s%s%s\n",
- cimg::t_bold,name,cimg::t_normal,res?res:"0",
- cimg::t_green,usage,cimg::t_normal);
- }
- return res;
- }
- inline const char* option(const char *const name, const int argc, const char *const *const argv,
- const char *const _default, const char *const usage=0) {
- return option(name,argc,argv,_default,usage,false);
- }
- inline bool option(const char *const name, const int argc, const char *const *const argv,
- const bool _default, const char *const usage=0) {
- const char *const s = cimg::option(name,argc,argv,(char*)0);
- const bool res = s?(cimg::strcasecmp(s,"false") && cimg::strcasecmp(s,"off") && cimg::strcasecmp(s,"0")):_default;
- cimg::option(name,0,0,res?"true":"false",usage);
- return res;
- }
- inline int option(const char *const name, const int argc, const char *const *const argv,
- const int _default, const char *const usage=0) {
- const char *const s = cimg::option(name,argc,argv,(char*)0);
- const int res = s?std::atoi(s):_default;
- char *const tmp = new char[256];
- cimg_snprintf(tmp,256,"%d",res);
- cimg::option(name,0,0,tmp,usage);
- delete[] tmp;
- return res;
- }
- inline char option(const char *const name, const int argc, const char *const *const argv,
- const char _default, const char *const usage=0) {
- const char *const s = cimg::option(name,argc,argv,(char*)0);
- const char res = s?*s:_default;
- char tmp[8];
- *tmp = res; tmp[1] = 0;
- cimg::option(name,0,0,tmp,usage);
- return res;
- }
- inline float option(const char *const name, const int argc, const char *const *const argv,
- const float _default, const char *const usage=0) {
- const char *const s = cimg::option(name,argc,argv,(char*)0);
- const float res = s?(float)cimg::atof(s):_default;
- char *const tmp = new char[256];
- cimg_snprintf(tmp,256,"%g",res);
- cimg::option(name,0,0,tmp,usage);
- delete[] tmp;
- return res;
- }
- inline double option(const char *const name, const int argc, const char *const *const argv,
- const double _default, const char *const usage=0) {
- const char *const s = cimg::option(name,argc,argv,(char*)0);
- const double res = s?cimg::atof(s):_default;
- char *const tmp = new char[256];
- cimg_snprintf(tmp,256,"%g",res);
- cimg::option(name,0,0,tmp,usage);
- delete[] tmp;
- return res;
- }
- //! Print information about \CImg environment variables.
- /**
- \note Output is done on the default output stream.
- **/
- inline void info() {
- std::fprintf(cimg::output(),"\n %s%sCImg Library %u.%u.%u%s, compiled %s ( %s ) with the following flags:\n\n",
- cimg::t_red,cimg::t_bold,cimg_version/100,(cimg_version/10)%10,cimg_version%10,
- cimg::t_normal,cimg_date,cimg_time);
- std::fprintf(cimg::output()," > Operating System: %s%-13s%s %s('cimg_OS'=%d)%s\n",
- cimg::t_bold,
- cimg_OS==1?"Unix":(cimg_OS==2?"Windows":"Unknown"),
- cimg::t_normal,cimg::t_green,
- cimg_OS,
- cimg::t_normal);
- std::fprintf(cimg::output()," > CPU endianness: %s%s Endian%s\n",
- cimg::t_bold,
- cimg::endianness()?"Big":"Little",
- cimg::t_normal);
- std::fprintf(cimg::output()," > Verbosity mode: %s%-13s%s %s('cimg_verbosity'=%d)%s\n",
- cimg::t_bold,
- cimg_verbosity==0?"Quiet":
- cimg_verbosity==1?"Console":
- cimg_verbosity==2?"Dialog":
- cimg_verbosity==3?"Console+Warnings":"Dialog+Warnings",
- cimg::t_normal,cimg::t_green,
- cimg_verbosity,
- cimg::t_normal);
- std::fprintf(cimg::output()," > Stricts warnings: %s%-13s%s %s('cimg_strict_warnings' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_strict_warnings
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Support for C++11: %s%-13s%s %s('cimg_use_cpp11'=%d)%s\n",
- cimg::t_bold,
- cimg_use_cpp11?"Yes":"No",
- cimg::t_normal,cimg::t_green,
- (int)cimg_use_cpp11,
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using VT100 messages: %s%-13s%s %s('cimg_use_vt100' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_vt100
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Display type: %s%-13s%s %s('cimg_display'=%d)%s\n",
- cimg::t_bold,
- cimg_display==0?"No display":cimg_display==1?"X11":cimg_display==2?"Windows GDI":"Unknown",
- cimg::t_normal,cimg::t_green,
- (int)cimg_display,
- cimg::t_normal);
- #if cimg_display==1
- std::fprintf(cimg::output()," > Using XShm for X11: %s%-13s%s %s('cimg_use_xshm' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_xshm
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using XRand for X11: %s%-13s%s %s('cimg_use_xrandr' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_xrandr
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- #endif
- std::fprintf(cimg::output()," > Using OpenMP: %s%-13s%s %s('cimg_use_openmp' %s)%s\n",
- cimg::t_bold,
- #if cimg_use_openmp!=0
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using PNG library: %s%-13s%s %s('cimg_use_png' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_png
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using JPEG library: %s%-13s%s %s('cimg_use_jpeg' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_jpeg
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using TIFF library: %s%-13s%s %s('cimg_use_tiff' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_tiff
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using Magick++ library: %s%-13s%s %s('cimg_use_magick' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_magick
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using FFTW3 library: %s%-13s%s %s('cimg_use_fftw3' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_fftw3
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- std::fprintf(cimg::output()," > Using LAPACK library: %s%-13s%s %s('cimg_use_lapack' %s)%s\n",
- cimg::t_bold,
- #ifdef cimg_use_lapack
- "Yes",cimg::t_normal,cimg::t_green,"defined",
- #else
- "No",cimg::t_normal,cimg::t_green,"undefined",
- #endif
- cimg::t_normal);
- char *const tmp = new char[1024];
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::curl_path());
- std::fprintf(cimg::output()," > Path of 'curl': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::dcraw_path());
- std::fprintf(cimg::output()," > Path of 'dcraw': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::ffmpeg_path());
- std::fprintf(cimg::output()," > Path of 'ffmpeg': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::graphicsmagick_path());
- std::fprintf(cimg::output()," > Path of 'graphicsmagick': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::gunzip_path());
- std::fprintf(cimg::output()," > Path of 'gunzip': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::gzip_path());
- std::fprintf(cimg::output()," > Path of 'gzip': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::imagemagick_path());
- std::fprintf(cimg::output()," > Path of 'imagemagick': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::medcon_path());
- std::fprintf(cimg::output()," > Path of 'medcon': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::temporary_path());
- std::fprintf(cimg::output()," > Temporary path: %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- cimg_snprintf(tmp,1024,"\"%.1020s\"",cimg::wget_path());
- std::fprintf(cimg::output()," > Path of 'wget': %s%-13s%s\n",
- cimg::t_bold,
- tmp,
- cimg::t_normal);
- std::fprintf(cimg::output(),"\n");
- delete[] tmp;
- }
- // Declare LAPACK function signatures if LAPACK support is enabled.
- #ifdef cimg_use_lapack
- template<typename T>
- inline void getrf(int &N, T *lapA, int *IPIV, int &INFO) {
- dgetrf_(&N,&N,lapA,&N,IPIV,&INFO);
- }
- inline void getrf(int &N, float *lapA, int *IPIV, int &INFO) {
- sgetrf_(&N,&N,lapA,&N,IPIV,&INFO);
- }
- template<typename T>
- inline void getri(int &N, T *lapA, int *IPIV, T* WORK, int &LWORK, int &INFO) {
- dgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO);
- }
- inline void getri(int &N, float *lapA, int *IPIV, float* WORK, int &LWORK, int &INFO) {
- sgetri_(&N,lapA,&N,IPIV,WORK,&LWORK,&INFO);
- }
- template<typename T>
- inline void gesvd(char &JOB, int &M, int &N, T *lapA, int &MN,
- T *lapS, T *lapU, T *lapV, T *WORK, int &LWORK, int &INFO) {
- dgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO);
- }
- inline void gesvd(char &JOB, int &M, int &N, float *lapA, int &MN,
- float *lapS, float *lapU, float *lapV, float *WORK, int &LWORK, int &INFO) {
- sgesvd_(&JOB,&JOB,&M,&N,lapA,&MN,lapS,lapU,&M,lapV,&N,WORK,&LWORK,&INFO);
- }
- template<typename T>
- inline void getrs(char &TRANS, int &N, T *lapA, int *IPIV, T *lapB, int &INFO) {
- int one = 1;
- dgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO);
- }
- inline void getrs(char &TRANS, int &N, float *lapA, int *IPIV, float *lapB, int &INFO) {
- int one = 1;
- sgetrs_(&TRANS,&N,&one,lapA,&N,IPIV,lapB,&N,&INFO);
- }
- template<typename T>
- inline void syev(char &JOB, char &UPLO, int &N, T *lapA, T *lapW, T *WORK, int &LWORK, int &INFO) {
- dsyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO);
- }
- inline void syev(char &JOB, char &UPLO, int &N, float *lapA, float *lapW, float *WORK, int &LWORK, int &INFO) {
- ssyev_(&JOB,&UPLO,&N,lapA,&N,lapW,WORK,&LWORK,&INFO);
- }
- template<typename T>
- inline void sgels(char & TRANS, int &M, int &N, int &NRHS, T* lapA, int &LDA,
- T* lapB, int &LDB, T* WORK, int &LWORK, int &INFO) {
- dgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO);
- }
- inline void sgels(char & TRANS, int &M, int &N, int &NRHS, float* lapA, int &LDA,
- float* lapB, int &LDB, float* WORK, int &LWORK, int &INFO) {
- sgels_(&TRANS, &M, &N, &NRHS, lapA, &LDA, lapB, &LDB, WORK, &LWORK, &INFO);
- }
- #endif
- } // namespace cimg { ...
- /*------------------------------------------------
- #
- #
- # Definition of mathematical operators and
- # external functions.
- #
- #
- -------------------------------------------------*/
- #define _cimg_create_operator(typ) \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator+(const typ val, const CImg<T>& img) { \
- return img + val; \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator-(const typ val, const CImg<T>& img) { \
- typedef typename cimg::superset<T,typ>::type Tt; \
- return CImg<Tt>(img._width,img._height,img._depth,img._spectrum,val)-=img; \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator*(const typ val, const CImg<T>& img) { \
- return img*val; \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator/(const typ val, const CImg<T>& img) { \
- return val*img.get_invert(); \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator&(const typ val, const CImg<T>& img) { \
- return img & val; \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator|(const typ val, const CImg<T>& img) { \
- return img | val; \
- } \
- template<typename T> \
- inline CImg<typename cimg::superset<T,typ>::type> operator^(const typ val, const CImg<T>& img) { \
- return img ^ val; \
- } \
- template<typename T> \
- inline bool operator==(const typ val, const CImg<T>& img) { \
- return img == val; \
- } \
- template<typename T> \
- inline bool operator!=(const typ val, const CImg<T>& img) { \
- return img != val; \
- }
- _cimg_create_operator(bool)
- _cimg_create_operator(unsigned char)
- _cimg_create_operator(char)
- _cimg_create_operator(signed char)
- _cimg_create_operator(unsigned short)
- _cimg_create_operator(short)
- _cimg_create_operator(unsigned int)
- _cimg_create_operator(int)
- _cimg_create_operator(cimg_uint64)
- _cimg_create_operator(cimg_int64)
- _cimg_create_operator(float)
- _cimg_create_operator(double)
- _cimg_create_operator(long double)
- template<typename T>
- inline CImg<_cimg_Tfloat> operator+(const char *const expression, const CImg<T>& img) {
- return img + expression;
- }
- template<typename T>
- inline CImg<_cimg_Tfloat> operator-(const char *const expression, const CImg<T>& img) {
- return CImg<_cimg_Tfloat>(img,false).fill(expression,true)-=img;
- }
- template<typename T>
- inline CImg<_cimg_Tfloat> operator*(const char *const expression, const CImg<T>& img) {
- return img*expression;
- }
- template<typename T>
- inline CImg<_cimg_Tfloat> operator/(const char *const expression, const CImg<T>& img) {
- return expression*img.get_invert();
- }
- template<typename T>
- inline CImg<T> operator&(const char *const expression, const CImg<T>& img) {
- return img & expression;
- }
- template<typename T>
- inline CImg<T> operator|(const char *const expression, const CImg<T>& img) {
- return img | expression;
- }
- template<typename T>
- inline CImg<T> operator^(const char *const expression, const CImg<T>& img) {
- return img ^ expression;
- }
- template<typename T>
- inline bool operator==(const char *const expression, const CImg<T>& img) {
- return img==expression;
- }
- template<typename T>
- inline bool operator!=(const char *const expression, const CImg<T>& img) {
- return img!=expression;
- }
- template<typename T>
- inline CImg<T> transpose(const CImg<T>& instance) {
- return instance.get_transpose();
- }
- template<typename T>
- inline CImg<_cimg_Tfloat> invert(const CImg<T>& instance, const bool use_LU=true) {
- return instance.get_invert(use_LU);
- }
- template<typename T>
- inline CImg<_cimg_Tfloat> pseudoinvert(const CImg<T>& instance, const bool use_LU=false) {
- return instance.get_pseudoinvert(use_LU);
- }
- #define _cimg_create_pointwise_function(name) \
- template<typename T> \
- inline CImg<_cimg_Tfloat> name(const CImg<T>& instance) { \
- return instance.get_##name(); \
- }
- _cimg_create_pointwise_function(sqr)
- _cimg_create_pointwise_function(sqrt)
- _cimg_create_pointwise_function(erf)
- _cimg_create_pointwise_function(exp)
- _cimg_create_pointwise_function(log)
- _cimg_create_pointwise_function(log2)
- _cimg_create_pointwise_function(log10)
- _cimg_create_pointwise_function(abs)
- _cimg_create_pointwise_function(sign)
- _cimg_create_pointwise_function(cos)
- _cimg_create_pointwise_function(sin)
- _cimg_create_pointwise_function(sinc)
- _cimg_create_pointwise_function(tan)
- _cimg_create_pointwise_function(acos)
- _cimg_create_pointwise_function(asin)
- _cimg_create_pointwise_function(atan)
- _cimg_create_pointwise_function(cosh)
- _cimg_create_pointwise_function(sinh)
- _cimg_create_pointwise_function(tanh)
- _cimg_create_pointwise_function(acosh)
- _cimg_create_pointwise_function(asinh)
- _cimg_create_pointwise_function(atanh)
- /*-----------------------------------
- #
- # Define the CImgDisplay structure
- #
- ----------------------------------*/
- //! Allow the creation of windows, display images on them and manage user events (keyboard, mouse and windows events).
- /**
- CImgDisplay methods rely on a low-level graphic library to perform: it can be either \b X-Window
- (X11, for Unix-based systems) or \b GDI32 (for Windows-based systems).
- If both libraries are missing, CImgDisplay will not be able to display images on screen, and will enter
- a minimal mode where warning messages will be outputted each time the program is trying to call one of the
- CImgDisplay method.
- The configuration variable \c cimg_display tells about the graphic library used.
- It is set automatically by \CImg when one of these graphic libraries has been detected.
- But, you can override its value if necessary. Valid choices are:
- - 0: Disable display capabilities.
- - 1: Use \b X-Window (X11) library.
- - 2: Use \b GDI32 library.
- Remember to link your program against \b X11 or \b GDI32 libraries if you use CImgDisplay.
- **/
- struct CImgDisplay {
- cimg_uint64 _timer, _fps_frames, _fps_timer;
- unsigned int _width, _height, _normalization;
- float _fps_fps, _min, _max;
- bool _is_fullscreen;
- char *_title;
- unsigned int _window_width, _window_height, _button, *_keys, *_released_keys;
- int _window_x, _window_y, _mouse_x, _mouse_y, _wheel;
- bool _is_closed, _is_resized, _is_moved, _is_event,
- _is_keyESC, _is_keyF1, _is_keyF2, _is_keyF3, _is_keyF4, _is_keyF5, _is_keyF6, _is_keyF7,
- _is_keyF8, _is_keyF9, _is_keyF10, _is_keyF11, _is_keyF12, _is_keyPAUSE, _is_key1, _is_key2,
- _is_key3, _is_key4, _is_key5, _is_key6, _is_key7, _is_key8, _is_key9, _is_key0,
- _is_keyBACKSPACE, _is_keyINSERT, _is_keyHOME, _is_keyPAGEUP, _is_keyTAB, _is_keyQ, _is_keyW, _is_keyE,
- _is_keyR, _is_keyT, _is_keyY, _is_keyU, _is_keyI, _is_keyO, _is_keyP, _is_keyDELETE,
- _is_keyEND, _is_keyPAGEDOWN, _is_keyCAPSLOCK, _is_keyA, _is_keyS, _is_keyD, _is_keyF, _is_keyG,
- _is_keyH, _is_keyJ, _is_keyK, _is_keyL, _is_keyENTER, _is_keySHIFTLEFT, _is_keyZ, _is_keyX,
- _is_keyC, _is_keyV, _is_keyB, _is_keyN, _is_keyM, _is_keySHIFTRIGHT, _is_keyARROWUP, _is_keyCTRLLEFT,
- _is_keyAPPLEFT, _is_keyALT, _is_keySPACE, _is_keyALTGR, _is_keyAPPRIGHT, _is_keyMENU, _is_keyCTRLRIGHT,
- _is_keyARROWLEFT, _is_keyARROWDOWN, _is_keyARROWRIGHT, _is_keyPAD0, _is_keyPAD1, _is_keyPAD2, _is_keyPAD3,
- _is_keyPAD4, _is_keyPAD5, _is_keyPAD6, _is_keyPAD7, _is_keyPAD8, _is_keyPAD9, _is_keyPADADD, _is_keyPADSUB,
- _is_keyPADMUL, _is_keyPADDIV;
- //@}
- //---------------------------
- //
- //! \name Plugins
- //@{
- //---------------------------
- #ifdef cimgdisplay_plugin
- #include cimgdisplay_plugin
- #endif
- #ifdef cimgdisplay_plugin1
- #include cimgdisplay_plugin1
- #endif
- #ifdef cimgdisplay_plugin2
- #include cimgdisplay_plugin2
- #endif
- #ifdef cimgdisplay_plugin3
- #include cimgdisplay_plugin3
- #endif
- #ifdef cimgdisplay_plugin4
- #include cimgdisplay_plugin4
- #endif
- #ifdef cimgdisplay_plugin5
- #include cimgdisplay_plugin5
- #endif
- #ifdef cimgdisplay_plugin6
- #include cimgdisplay_plugin6
- #endif
- #ifdef cimgdisplay_plugin7
- #include cimgdisplay_plugin7
- #endif
- #ifdef cimgdisplay_plugin8
- #include cimgdisplay_plugin8
- #endif
- //@}
- //--------------------------------------------------------
- //
- //! \name Constructors / Destructor / Instance Management
- //@{
- //--------------------------------------------------------
- //! Destructor.
- /**
- \note If the associated window is visible on the screen, it is closed by the call to the destructor.
- **/
- ~CImgDisplay() {
- assign();
- delete[] _keys;
- delete[] _released_keys;
- }
- //! Construct an empty display.
- /**
- \note Constructing an empty CImgDisplay instance does not make a window appearing on the screen, until
- display of valid data is performed.
- \par Example
- \code
- CImgDisplay disp; // Does actually nothing
- ...
- disp.display(img); // Construct new window and display image in it
- \endcode
- **/
- CImgDisplay():
- _width(0),_height(0),_normalization(0),
- _min(0),_max(0),
- _is_fullscreen(false),
- _title(0),
- _window_width(0),_window_height(0),_button(0),
- _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
- _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
- _mouse_x(-1),_mouse_y(-1),_wheel(0),
- _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
- assign();
- }
- //! Construct a display with specified dimensions.
- /** \param width Window width.
- \param height Window height.
- \param title Window title.
- \param normalization Normalization type
- (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
- \param is_fullscreen Tells if fullscreen mode is enabled.
- \param is_closed Tells if associated window is initially visible or not.
- \note A black background is initially displayed on the associated window.
- **/
- CImgDisplay(const unsigned int width, const unsigned int height,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false):
- _width(0),_height(0),_normalization(0),
- _min(0),_max(0),
- _is_fullscreen(false),
- _title(0),
- _window_width(0),_window_height(0),_button(0),
- _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
- _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
- _mouse_x(-1),_mouse_y(-1),_wheel(0),
- _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
- assign(width,height,title,normalization,is_fullscreen,is_closed);
- }
- //! Construct a display from an image.
- /** \param img Image used as a model to create the window.
- \param title Window title.
- \param normalization Normalization type
- (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
- \param is_fullscreen Tells if fullscreen mode is enabled.
- \param is_closed Tells if associated window is initially visible or not.
- \note The pixels of the input image are initially displayed on the associated window.
- **/
- template<typename T>
- explicit CImgDisplay(const CImg<T>& img,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false):
- _width(0),_height(0),_normalization(0),
- _min(0),_max(0),
- _is_fullscreen(false),
- _title(0),
- _window_width(0),_window_height(0),_button(0),
- _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
- _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
- _mouse_x(-1),_mouse_y(-1),_wheel(0),
- _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
- assign(img,title,normalization,is_fullscreen,is_closed);
- }
- //! Construct a display from an image list.
- /** \param list The images list to display.
- \param title Window title.
- \param normalization Normalization type
- (<tt>0</tt>=none, <tt>1</tt>=always, <tt>2</tt>=once, <tt>3</tt>=pixel type-dependent, see normalization()).
- \param is_fullscreen Tells if fullscreen mode is enabled.
- \param is_closed Tells if associated window is initially visible or not.
- \note All images of the list, appended along the X-axis, are initially displayed on the associated window.
- **/
- template<typename T>
- explicit CImgDisplay(const CImgList<T>& list,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false):
- _width(0),_height(0),_normalization(0),
- _min(0),_max(0),
- _is_fullscreen(false),
- _title(0),
- _window_width(0),_window_height(0),_button(0),
- _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
- _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
- _mouse_x(-1),_mouse_y(-1),_wheel(0),
- _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
- assign(list,title,normalization,is_fullscreen,is_closed);
- }
- //! Construct a display as a copy of an existing one.
- /**
- \param disp Display instance to copy.
- \note The pixel buffer of the input window is initially displayed on the associated window.
- **/
- CImgDisplay(const CImgDisplay& disp):
- _width(0),_height(0),_normalization(0),
- _min(0),_max(0),
- _is_fullscreen(false),
- _title(0),
- _window_width(0),_window_height(0),_button(0),
- _keys(new unsigned int[128]),_released_keys(new unsigned int[128]),
- _window_x(cimg::type<int>::min()),_window_y(cimg::type<int>::min()),
- _mouse_x(-1),_mouse_y(-1),_wheel(0),
- _is_closed(true),_is_resized(false),_is_moved(false),_is_event(false) {
- assign(disp);
- }
- //! Take a screenshot.
- /**
- \param[out] img Output screenshot. Can be empty on input
- **/
- template<typename T>
- static void screenshot(CImg<T>& img) {
- return screenshot(0,0,cimg::type<int>::max(),cimg::type<int>::max(),img);
- }
- #if cimg_display==0
- static void _no_display_exception() {
- throw CImgDisplayException("CImgDisplay(): No display available.");
- }
- //! Destructor - Empty constructor \inplace.
- /**
- \note Replace the current instance by an empty display.
- **/
- CImgDisplay& assign() {
- return flush();
- }
- //! Construct a display with specified dimensions \inplace.
- /**
- **/
- CImgDisplay& assign(const unsigned int width, const unsigned int height,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false) {
- cimg::unused(width,height,title,normalization,is_fullscreen,is_closed);
- _no_display_exception();
- return assign();
- }
- //! Construct a display from an image \inplace.
- /**
- **/
- template<typename T>
- CImgDisplay& assign(const CImg<T>& img,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false) {
- _no_display_exception();
- return assign(img._width,img._height,title,normalization,is_fullscreen,is_closed);
- }
- //! Construct a display from an image list \inplace.
- /**
- **/
- template<typename T>
- CImgDisplay& assign(const CImgList<T>& list,
- const char *const title=0, const unsigned int normalization=3,
- const bool is_fullscreen=false, const bool is_closed=false) {
- _no_display_exception();
- return assign(list._width,list._width,title,normalization,is_fullscreen,is_closed);
- }
- //! Construct a display as a copy of another one \inplace.
- /**
- **/
- CImgDisplay& assign(const CImgDisplay &disp) {
- _no_display_exception();
- return assign(disp._width,disp._height);
- }
- #endif
- //! Return a reference to an empty display.
- /**
- \note Can be useful for writing function prototypes where one of the argument (of type CImgDisplay&)
- must have a default value.
- \par Example
- \code
- void foo(CImgDisplay& disp=CImgDisplay::empty());
- \endcode
- **/
- static CImgDisplay& empty() {
- static CImgDisplay _empty;
- return _empty.assign();
- }
- //! Return a reference to an empty display \const.
- static const CImgDisplay& const_empty() {
- static const CImgDisplay _empty;
- return _empty;
- }
- #define cimg_fitscreen(dx,dy,dz) CImgDisplay::_fitscreen(dx,dy,dz,-25,-85,false), \
- CImgDisplay::_fitscreen(dx,dy,dz,-25,-85,true)
- static unsigned int _fitscreen(const unsigned int dx, const unsigned int dy, const unsigned int dz,
- const int dmin, const int dmax, const bool return_y) {
- const int
- u = CImgDisplay::screen_width(),
- v = CImgDisplay::screen_height();
- const float
- mw = dmin<0?cimg::round(u*-dmin/100.f):(float)dmin,
- mh = dmin<0?cimg::round(v*-dmin/100.f):(float)dmin,
- Mw = dmax<0?cimg::round(u*-dmax/100.f):(float)dmax,
- Mh = dmax<0?cimg::round(v*-dmax/100.f):(float)dmax;
- float
- w = (float)std::max(1U,dx),
- h = (float)std::max(1U,dy);
- if (dz>1) { w+=dz; h+=dz; }
- if (w<mw) { h = h*mw/w; w = mw; }
- if (h<mh) { w = w*mh/h; h = mh; }
- if (w>Mw) { h = h*Mw/w; w = Mw; }
- if (h>Mh) { w = w*Mh/h; h = Mh; }
- if (w<mw) w = mw;
- if (h<mh) h = mh;
- return std::max(1U,(unsigned int)cimg::round(return_y?h:w));
- }
- //@}
- //------------------------------------------
- //
- //! \name Overloaded Operators
- //@{
- //------------------------------------------
- //! Display image on associated window.
- /**
- \note <tt>disp = img</tt> is equivalent to <tt>disp.display(img)</tt>.
- **/
- template<typename t>
- CImgDisplay& operator=(const CImg<t>& img) {
- return display(img);
- }
- //! Display list of images on associated window.
- /**
- \note <tt>disp = list</tt> is equivalent to <tt>disp.display(list)</tt>.
- **/
- template<typename t>
- CImgDisplay& operator=(const CImgList<t>& list) {
- return display(list);
- }
- //! Construct a display as a copy of another one \inplace.
- /**
- \note Equivalent to assign(const CImgDisplay&).
- **/
- CImgDisplay& operator=(const CImgDisplay& disp) {
- return assign(disp);
- }
- //! Return \c false if display is empty, \c true otherwise.
- /**
- \note <tt>if (disp) { ... }</tt> is equivalent to <tt>if (!disp.is_empty()) { ... }</tt>.
- **/
- operator bool() const {
- return !is_empty();
- }
- //@}
- //------------------------------------------
- //
- //! \name Instance Checking
- //@{
- //------------------------------------------
- //! Return \c true if display is empty, \c false otherwise.
- /**
- **/
- bool is_empty() const {
- return !(_width && _height);
- }
- //! Return \c true if display is closed (i.e. not visible on the screen), \c false otherwise.
- /**
- \note
- - When a user physically closes the associated window, the display is set to closed.
- - A closed display is not destroyed. Its associated window can be show again on the screen using show().
- **/
- bool is_closed() const {
- return _is_closed;
- }
- //! Return \c true if display is visible (i.e. not closed by the user), \c false otherwise.
- bool is_visible() const {
- return !is_closed();
- }
- //! Return \c true if associated window has been resized on the screen, \c false otherwise.
- /**
- **/
- bool is_resized() const {
- return _is_resized;
- }
- //! Return \c true if associated window has been moved on the screen, \c false otherwise.
- /**
- **/
- bool is_moved() const {
- return _is_moved;
- }
- //! Return \c true if any event has occurred on the associated window, \c false otherwise.
- /**
- **/
- bool is_event() const {
- return _is_event;
- }
- //! Return \c true if current display is in fullscreen mode, \c false otherwise.
- /**
- **/
- bool is_fullscreen() const {
- return _is_fullscreen;
- }
- //! Return \c true if any key is being pressed on the associated window, \c false otherwise.
- /**
- \note The methods below do the same only for specific keys.
- **/
- bool is_key() const {
- return _is_keyESC || _is_keyF1 || _is_keyF2 || _is_keyF3 ||
- _is_keyF4 || _is_keyF5 || _is_keyF6 || _is_keyF7 ||
- _is_keyF8 || _is_keyF9 || _is_keyF10 || _is_keyF11 ||
- _is_keyF12 || _is_keyPAUSE || _is_key1 || _is_key2 ||
- _is_key3 || _is_key4 || _is_key5 || _is_key6 ||
- _is_key7 || _is_key8 || _is_key9 || _is_key0 ||
- _is_keyBACKSPACE || _is_keyINSERT || _is_keyHOME ||
- _is_keyPAGEUP || _is_keyTAB || _is_keyQ || _is_keyW ||
- _is_keyE || _is_keyR || _is_keyT || _is_keyY ||
- _is_keyU || _is_keyI || _is_keyO || _is_keyP ||
- _is_keyDELETE || _is_keyEND || _is_keyPAGEDOWN ||
- _is_keyCAPSLOCK || _is_keyA || _is_keyS || _is_keyD ||
- _is_keyF || _is_keyG || _is_keyH || _is_keyJ ||
- _is_keyK || _is_keyL || _is_keyENTER ||
- _is_keySHIFTLEFT || _is_keyZ || _is_keyX || _is_keyC ||
- _is_keyV || _is_keyB || _is_keyN || _is_keyM ||
- _is_keySHIFTRIGHT || _is_keyARROWUP || _is_keyCTRLLEFT ||
- _is_keyAPPLEFT || _is_keyALT || _is_keySPACE || _is_keyALTGR ||
- _is_keyAPPRIGHT || _is_keyMENU || _is_keyCTRLRIGHT ||
- _is_keyARROWLEFT || _is_keyARROWDOWN || _is_keyARROWRIGHT ||
- _is_keyPAD0 || _is_keyPAD1 || _is_keyPAD2 ||
- _is_keyPAD3 || _is_keyPAD4 || _is_keyPAD5 ||
- _is_keyPAD6 || _is_keyPAD7 || _is_keyPAD8 ||
- _is_keyPAD9 || _is_keyPADADD || _is_keyPADSUB ||
- _is_keyPADMUL || _is_keyPADDIV;
- }
- //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise.
- /**
- \param keycode Keycode to test.
- \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- \par Example
- \code
- CImgDisplay disp(400,400);
- while (!disp.is_closed()) {
- if (disp.key(cimg::keyTAB)) { ... } // Equivalent to 'if (disp.is_keyTAB())'
- disp.wait();
- }
- \endcode
- **/
- bool is_key(const unsigned int keycode) const {
- #define _cimg_iskey_test(k) if (keycode==cimg::key##k) return _is_key##k;
- _cimg_iskey_test(ESC); _cimg_iskey_test(F1); _cimg_iskey_test(F2); _cimg_iskey_test(F3);
- _cimg_iskey_test(F4); _cimg_iskey_test(F5); _cimg_iskey_test(F6); _cimg_iskey_test(F7);
- _cimg_iskey_test(F8); _cimg_iskey_test(F9); _cimg_iskey_test(F10); _cimg_iskey_test(F11);
- _cimg_iskey_test(F12); _cimg_iskey_test(PAUSE); _cimg_iskey_test(1); _cimg_iskey_test(2);
- _cimg_iskey_test(3); _cimg_iskey_test(4); _cimg_iskey_test(5); _cimg_iskey_test(6);
- _cimg_iskey_test(7); _cimg_iskey_test(8); _cimg_iskey_test(9); _cimg_iskey_test(0);
- _cimg_iskey_test(BACKSPACE); _cimg_iskey_test(INSERT); _cimg_iskey_test(HOME);
- _cimg_iskey_test(PAGEUP); _cimg_iskey_test(TAB); _cimg_iskey_test(Q); _cimg_iskey_test(W);
- _cimg_iskey_test(E); _cimg_iskey_test(R); _cimg_iskey_test(T); _cimg_iskey_test(Y);
- _cimg_iskey_test(U); _cimg_iskey_test(I); _cimg_iskey_test(O); _cimg_iskey_test(P);
- _cimg_iskey_test(DELETE); _cimg_iskey_test(END); _cimg_iskey_test(PAGEDOWN);
- _cimg_iskey_test(CAPSLOCK); _cimg_iskey_test(A); _cimg_iskey_test(S); _cimg_iskey_test(D);
- _cimg_iskey_test(F); _cimg_iskey_test(G); _cimg_iskey_test(H); _cimg_iskey_test(J);
- _cimg_iskey_test(K); _cimg_iskey_test(L); _cimg_iskey_test(ENTER);
- _cimg_iskey_test(SHIFTLEFT); _cimg_iskey_test(Z); _cimg_iskey_test(X); _cimg_iskey_test(C);
- _cimg_iskey_test(V); _cimg_iskey_test(B); _cimg_iskey_test(N); _cimg_iskey_test(M);
- _cimg_iskey_test(SHIFTRIGHT); _cimg_iskey_test(ARROWUP); _cimg_iskey_test(CTRLLEFT);
- _cimg_iskey_test(APPLEFT); _cimg_iskey_test(ALT); _cimg_iskey_test(SPACE); _cimg_iskey_test(ALTGR);
- _cimg_iskey_test(APPRIGHT); _cimg_iskey_test(MENU); _cimg_iskey_test(CTRLRIGHT);
- _cimg_iskey_test(ARROWLEFT); _cimg_iskey_test(ARROWDOWN); _cimg_iskey_test(ARROWRIGHT);
- _cimg_iskey_test(PAD0); _cimg_iskey_test(PAD1); _cimg_iskey_test(PAD2);
- _cimg_iskey_test(PAD3); _cimg_iskey_test(PAD4); _cimg_iskey_test(PAD5);
- _cimg_iskey_test(PAD6); _cimg_iskey_test(PAD7); _cimg_iskey_test(PAD8);
- _cimg_iskey_test(PAD9); _cimg_iskey_test(PADADD); _cimg_iskey_test(PADSUB);
- _cimg_iskey_test(PADMUL); _cimg_iskey_test(PADDIV);
- return false;
- }
- //! Return \c true if key specified by given keycode is being pressed on the associated window, \c false otherwise.
- /**
- \param keycode C-string containing the keycode label of the key to test.
- \note Use it when the key you want to test can be dynamically set by the user.
- \par Example
- \code
- CImgDisplay disp(400,400);
- const char *const keycode = "TAB";
- while (!disp.is_closed()) {
- if (disp.is_key(keycode)) { ... } // Equivalent to 'if (disp.is_keyTAB())'
- disp.wait();
- }
- \endcode
- **/
- bool& is_key(const char *const keycode) {
- static bool f = false;
- f = false;
- #define _cimg_iskey_test2(k) if (!cimg::strcasecmp(keycode,#k)) return _is_key##k;
- _cimg_iskey_test2(ESC); _cimg_iskey_test2(F1); _cimg_iskey_test2(F2); _cimg_iskey_test2(F3);
- _cimg_iskey_test2(F4); _cimg_iskey_test2(F5); _cimg_iskey_test2(F6); _cimg_iskey_test2(F7);
- _cimg_iskey_test2(F8); _cimg_iskey_test2(F9); _cimg_iskey_test2(F10); _cimg_iskey_test2(F11);
- _cimg_iskey_test2(F12); _cimg_iskey_test2(PAUSE); _cimg_iskey_test2(1); _cimg_iskey_test2(2);
- _cimg_iskey_test2(3); _cimg_iskey_test2(4); _cimg_iskey_test2(5); _cimg_iskey_test2(6);
- _cimg_iskey_test2(7); _cimg_iskey_test2(8); _cimg_iskey_test2(9); _cimg_iskey_test2(0);
- _cimg_iskey_test2(BACKSPACE); _cimg_iskey_test2(INSERT); _cimg_iskey_test2(HOME);
- _cimg_iskey_test2(PAGEUP); _cimg_iskey_test2(TAB); _cimg_iskey_test2(Q); _cimg_iskey_test2(W);
- _cimg_iskey_test2(E); _cimg_iskey_test2(R); _cimg_iskey_test2(T); _cimg_iskey_test2(Y);
- _cimg_iskey_test2(U); _cimg_iskey_test2(I); _cimg_iskey_test2(O); _cimg_iskey_test2(P);
- _cimg_iskey_test2(DELETE); _cimg_iskey_test2(END); _cimg_iskey_test2(PAGEDOWN);
- _cimg_iskey_test2(CAPSLOCK); _cimg_iskey_test2(A); _cimg_iskey_test2(S); _cimg_iskey_test2(D);
- _cimg_iskey_test2(F); _cimg_iskey_test2(G); _cimg_iskey_test2(H); _cimg_iskey_test2(J);
- _cimg_iskey_test2(K); _cimg_iskey_test2(L); _cimg_iskey_test2(ENTER);
- _cimg_iskey_test2(SHIFTLEFT); _cimg_iskey_test2(Z); _cimg_iskey_test2(X); _cimg_iskey_test2(C);
- _cimg_iskey_test2(V); _cimg_iskey_test2(B); _cimg_iskey_test2(N); _cimg_iskey_test2(M);
- _cimg_iskey_test2(SHIFTRIGHT); _cimg_iskey_test2(ARROWUP); _cimg_iskey_test2(CTRLLEFT);
- _cimg_iskey_test2(APPLEFT); _cimg_iskey_test2(ALT); _cimg_iskey_test2(SPACE); _cimg_iskey_test2(ALTGR);
- _cimg_iskey_test2(APPRIGHT); _cimg_iskey_test2(MENU); _cimg_iskey_test2(CTRLRIGHT);
- _cimg_iskey_test2(ARROWLEFT); _cimg_iskey_test2(ARROWDOWN); _cimg_iskey_test2(ARROWRIGHT);
- _cimg_iskey_test2(PAD0); _cimg_iskey_test2(PAD1); _cimg_iskey_test2(PAD2);
- _cimg_iskey_test2(PAD3); _cimg_iskey_test2(PAD4); _cimg_iskey_test2(PAD5);
- _cimg_iskey_test2(PAD6); _cimg_iskey_test2(PAD7); _cimg_iskey_test2(PAD8);
- _cimg_iskey_test2(PAD9); _cimg_iskey_test2(PADADD); _cimg_iskey_test2(PADSUB);
- _cimg_iskey_test2(PADMUL); _cimg_iskey_test2(PADDIV);
- return f;
- }
- //! Return \c true if specified key sequence has been typed on the associated window, \c false otherwise.
- /**
- \param keycodes_sequence Buffer of keycodes to test.
- \param length Number of keys in the \c keycodes_sequence buffer.
- \param remove_sequence Tells if the key sequence must be removed from the key history, if found.
- \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- \par Example
- \code
- CImgDisplay disp(400,400);
- const unsigned int key_seq[] = { cimg::keyCTRLLEFT, cimg::keyD };
- while (!disp.is_closed()) {
- if (disp.is_key_sequence(key_seq,2)) { ... } // Test for the 'CTRL+D' keyboard event
- disp.wait();
- }
- \endcode
- **/
- bool is_key_sequence(const unsigned int *const keycodes_sequence, const unsigned int length,
- const bool remove_sequence=false) {
- if (keycodes_sequence && length) {
- const unsigned int
- *const ps_end = keycodes_sequence + length - 1,
- *const pk_end = (unsigned int*)_keys + 1 + 128 - length,
- k = *ps_end;
- for (unsigned int *pk = (unsigned int*)_keys; pk<pk_end; ) {
- if (*(pk++)==k) {
- bool res = true;
- const unsigned int *ps = ps_end, *pk2 = pk;
- for (unsigned int i = 1; i<length; ++i) res = (*(--ps)==*(pk2++));
- if (res) {
- if (remove_sequence) std::memset((void*)(pk - 1),0,sizeof(unsigned int)*length);
- return true;
- }
- }
- }
- }
- return false;
- }
- #define _cimg_iskey_def(k) \
- bool is_key##k() const { \
- return _is_key##k; \
- }
- //! Return \c true if the \c ESC key is being pressed on the associated window, \c false otherwise.
- /**
- \note Similar methods exist for all keys managed by \CImg (see cimg::keyESC).
- **/
- _cimg_iskey_def(ESC); _cimg_iskey_def(F1); _cimg_iskey_def(F2); _cimg_iskey_def(F3);
- _cimg_iskey_def(F4); _cimg_iskey_def(F5); _cimg_iskey_def(F6); _cimg_iskey_def(F7);
- _cimg_iskey_def(F8); _cimg_iskey_def(F9); _cimg_iskey_def(F10); _cimg_iskey_def(F11);
- _cimg_iskey_def(F12); _cimg_iskey_def(PAUSE); _cimg_iskey_def(1); _cimg_iskey_def(2);
- _cimg_iskey_def(3); _cimg_iskey_def(4); _cimg_iskey_def(5); _cimg_iskey_def(6);
- _cimg_iskey_def(7); _cimg_iskey_def(8); _cimg_iskey_def(9); _cimg_iskey_def(0);
- _cimg_iskey_def(BACKSPACE); _cimg_iskey_def(INSERT); _cimg_iskey_def(HOME);
- _cimg_iskey_def(PAGEUP); _cimg_iskey_def(TAB); _cimg_iskey_def(Q); _cimg_iskey_def(W);
- _cimg_iskey_def(E); _cimg_iskey_def(R); _cimg_iskey_def(T); _cimg_iskey_def(Y);
- _cimg_iskey_def(U); _cimg_iskey_def(I); _cimg_iskey_def(O); _cimg_iskey_def(P);
- _cimg_iskey_def(DELETE); _cimg_iskey_def(END); _cimg_iskey_def(PAGEDOWN);
- _cimg_iskey_def(CAPSLOCK); _cimg_iskey_def(A); _cimg_iskey_def(S); _cimg_iskey_def(D);
- _cimg_iskey_def(F); _cimg_iskey_def(G); _cimg_iskey_def(H); _cimg_iskey_def(J);
- _cimg_iskey_def(K); _cimg_iskey_def(L); _cimg_iskey_def(ENTER);
- _cimg_iskey_def(SHIFTLEFT); _cimg_iskey_def(Z); _cimg_iskey_def(X); _cimg_iskey_def(C);
- _cimg_iskey_def(V); _cimg_iskey_def(B); _cimg_iskey_def(N); _cimg_iskey_def(M);
- _cimg_iskey_def(SHIFTRIGHT); _cimg_iskey_def(ARROWUP); _cimg_iskey_def(CTRLLEFT);
- _cimg_iskey_def(APPLEFT); _cimg_iskey_def(ALT); _cimg_iskey_def(SPACE); _cimg_iskey_def(ALTGR);
- _cimg_iskey_def(APPRIGHT); _cimg_iskey_def(MENU); _cimg_iskey_def(CTRLRIGHT);
- _cimg_iskey_def(ARROWLEFT); _cimg_iskey_def(ARROWDOWN); _cimg_iskey_def(ARROWRIGHT);
- _cimg_iskey_def(PAD0); _cimg_iskey_def(PAD1); _cimg_iskey_def(PAD2);
- _cimg_iskey_def(PAD3); _cimg_iskey_def(PAD4); _cimg_iskey_def(PAD5);
- _cimg_iskey_def(PAD6); _cimg_iskey_def(PAD7); _cimg_iskey_def(PAD8);
- _cimg_iskey_def(PAD9); _cimg_iskey_def(PADADD); _cimg_iskey_def(PADSUB);
- _cimg_iskey_def(PADMUL); _cimg_iskey_def(PADDIV);
- //@}
- //------------------------------------------
- //
- //! \name Instance Characteristics
- //@{
- //------------------------------------------
- #if cimg_display==0
- //! Return width of the screen (current resolution along the X-axis).
- /**
- **/
- static int screen_width() {
- _no_display_exception();
- return 0;
- }
- //! Return height of the screen (current resolution along the Y-axis).
- /**
- **/
- static int screen_height() {
- _no_display_exception();
- return 0;
- }
- #endif
- //! Return display width.
- /**
- \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance)
- may be different from the actual width of the associated window.
- **/
- int width() const {
- return (int)_width;
- }
- //! Return display height.
- /**
- \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance)
- may be different from the actual height of the associated window.
- **/
- int height() const {
- return (int)_height;
- }
- //! Return normalization type of the display.
- /**
- The normalization type tells about how the values of an input image are normalized by the CImgDisplay to be
- correctly displayed. The range of values for pixels displayed on screen is <tt>[0,255]</tt>.
- If the range of values of the data to display is different, a normalization may be required for displaying
- the data in a correct way. The normalization type can be one of:
- - \c 0: Value normalization is disabled. It is then assumed that all input data to be displayed by the
- CImgDisplay instance have values in range <tt>[0,255]</tt>.
- - \c 1: Value normalization is always performed (this is the default behavior).
- Before displaying an input image, its values will be (virtually) stretched
- in range <tt>[0,255]</tt>, so that the contrast of the displayed pixels will be maximum.
- Use this mode for images whose minimum and maximum values are not prescribed to known values
- (e.g. float-valued images).
- Note that when normalized versions of images are computed for display purposes, the actual values of these
- images are not modified.
- - \c 2: Value normalization is performed once (on the first image display), then the same normalization
- coefficients are kept for next displayed frames.
- - \c 3: Value normalization depends on the pixel type of the data to display. For integer pixel types,
- the normalization is done regarding the minimum/maximum values of the type (no normalization occurs then
- for <tt>unsigned char</tt>).
- For float-valued pixel types, the normalization is done regarding the minimum/maximum value of the image
- data instead.
- **/
- unsigned int normalization() const {
- return _normalization;
- }
- //! Return title of the associated window as a C-string.
- /**
- \note Window title may be not visible, depending on the used window manager or if the current display is
- in fullscreen mode.
- **/
- const char *title() const {
- return _title?_title:"";
- }
- //! Return width of the associated window.
- /**
- \note The width of the display (i.e. the width of the pixel data buffer associated to the CImgDisplay instance)
- may be different from the actual width of the associated window.
- **/
- int window_width() const {
- return (int)_window_width;
- }
- //! Return height of the associated window.
- /**
- \note The height of the display (i.e. the height of the pixel data buffer associated to the CImgDisplay instance)
- may be different from the actual height of the associated window.
- **/
- int window_height() const {
- return (int)_window_height;
- }
- //! Return X-coordinate of the associated window.
- /**
- \note The returned coordinate corresponds to the location of the upper-left corner of the associated window.
- **/
- int window_x() const {
- return _window_x;
- }
- //! Return Y-coordinate of the associated window.
- /**
- \note The returned coordinate corresponds to the location of the upper-left corner of the associated window.
- **/
- int window_y() const {
- return _window_y;
- }
- //! Return X-coordinate of the mouse pointer.
- /**
- \note
- - If the mouse pointer is outside window area, \c -1 is returned.
- - Otherwise, the returned value is in the range [0,width()-1].
- **/
- int mouse_x() const {
- return _mouse_x;
- }
- //! Return Y-coordinate of the mouse pointer.
- /**
- \note
- - If the mouse pointer is outside window area, \c -1 is returned.
- - Otherwise, the returned value is in the range [0,height()-1].
- **/
- int mouse_y() const {
- return _mouse_y;
- }
- //! Return current state of the mouse buttons.
- /**
- \note Three mouse buttons can be managed. If one button is pressed, its corresponding bit in the returned
- value is set:
- - bit \c 0 (value \c 0x1): State of the left mouse button.
- - bit \c 1 (value \c 0x2): State of the right mouse button.
- - bit \c 2 (value \c 0x4): State of the middle mouse button.
- Several bits can be activated if more than one button are pressed at the same time.
- \par Example
- \code
- CImgDisplay disp(400,400);
- while (!disp.is_closed()) {
- if (disp.button()&1) { // Left button clicked
- ...
- }
- if (disp.button()&2) { // Right button clicked
- ...
- }
- if (disp.button()&4) { // Middle button clicked
- ...
- }
- disp.wait();
- }
- \endcode
- **/
- unsigned int button() const {
- return _button;
- }
- //! Return current state of the mouse wheel.
- /**
- \note
- - The returned value can be positive or negative depending on whether the mouse wheel has been scrolled
- forward or backward.
- - Scrolling the wheel forward add \c 1 to the wheel value.
- - Scrolling the wheel backward subtract \c 1 to the wheel value.
- - The returned value cumulates the number of forward of backward scrolls since the creation of the display,
- or since the last reset of the wheel value (using set_wheel()). It is strongly recommended to quickly reset
- the wheel counter when an action has been performed regarding the current wheel value.
- Otherwise, the returned wheel value may be for instance \c 0 despite the fact that many scrolls have been done
- (as many in forward as in backward directions).
- \par Example
- \code
- CImgDisplay disp(400,400);
- while (!disp.is_closed()) {
- if (disp.wheel()) {
- int counter = disp.wheel(); // Read the state of the mouse wheel
- ... // Do what you want with 'counter'
- disp.set_wheel(); // Reset the wheel value to 0
- }
- disp.wait();
- }
- \endcode
- **/
- int wheel() const {
- return _wheel;
- }
- //! Return one entry from the pressed keys history.
- /**
- \param pos Index to read from the pressed keys history (index \c 0 corresponds to latest entry).
- \return Keycode of a pressed key or \c 0 for a released key.
- \note
- - Each CImgDisplay stores a history of the pressed keys in a buffer of size \c 128. When a new key is pressed,
- its keycode is stored in the pressed keys history. When a key is released, \c 0 is put instead.
- This means that up to the 64 last pressed keys may be read from the pressed keys history.
- When a new value is stored, the pressed keys history is shifted so that the latest entry is always
- stored at position \c 0.
- - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- **/
- unsigned int& key(const unsigned int pos=0) const {
- static unsigned int key0;
- return pos<128?_keys[pos]:(key0 = 0);
- }
- //! Return one entry from the released keys history.
- /**
- \param pos Index to read from the released keys history (index \c 0 corresponds to latest entry).
- \return Keycode of a released key or \c 0 for a pressed key.
- \note
- - Each CImgDisplay stores a history of the released keys in a buffer of size \c 128. When a new key is released,
- its keycode is stored in the pressed keys history. When a key is pressed, \c 0 is put instead.
- This means that up to the 64 last released keys may be read from the released keys history.
- When a new value is stored, the released keys history is shifted so that the latest entry is always
- stored at position \c 0.
- - Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- **/
- unsigned int& released_key(const unsigned int pos=0) const {
- static unsigned int key0;
- return pos<128?_released_keys[pos]:(key0 = 0);
- }
- //! Return keycode corresponding to the specified string.
- /**
- \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- \par Example
- \code
- const unsigned int keyTAB = CImgDisplay::keycode("TAB"); // Return cimg::keyTAB
- \endcode
- **/
- static unsigned int keycode(const char *const keycode) {
- #define _cimg_keycode(k) if (!cimg::strcasecmp(keycode,#k)) return cimg::key##k;
- _cimg_keycode(ESC); _cimg_keycode(F1); _cimg_keycode(F2); _cimg_keycode(F3);
- _cimg_keycode(F4); _cimg_keycode(F5); _cimg_keycode(F6); _cimg_keycode(F7);
- _cimg_keycode(F8); _cimg_keycode(F9); _cimg_keycode(F10); _cimg_keycode(F11);
- _cimg_keycode(F12); _cimg_keycode(PAUSE); _cimg_keycode(1); _cimg_keycode(2);
- _cimg_keycode(3); _cimg_keycode(4); _cimg_keycode(5); _cimg_keycode(6);
- _cimg_keycode(7); _cimg_keycode(8); _cimg_keycode(9); _cimg_keycode(0);
- _cimg_keycode(BACKSPACE); _cimg_keycode(INSERT); _cimg_keycode(HOME);
- _cimg_keycode(PAGEUP); _cimg_keycode(TAB); _cimg_keycode(Q); _cimg_keycode(W);
- _cimg_keycode(E); _cimg_keycode(R); _cimg_keycode(T); _cimg_keycode(Y);
- _cimg_keycode(U); _cimg_keycode(I); _cimg_keycode(O); _cimg_keycode(P);
- _cimg_keycode(DELETE); _cimg_keycode(END); _cimg_keycode(PAGEDOWN);
- _cimg_keycode(CAPSLOCK); _cimg_keycode(A); _cimg_keycode(S); _cimg_keycode(D);
- _cimg_keycode(F); _cimg_keycode(G); _cimg_keycode(H); _cimg_keycode(J);
- _cimg_keycode(K); _cimg_keycode(L); _cimg_keycode(ENTER);
- _cimg_keycode(SHIFTLEFT); _cimg_keycode(Z); _cimg_keycode(X); _cimg_keycode(C);
- _cimg_keycode(V); _cimg_keycode(B); _cimg_keycode(N); _cimg_keycode(M);
- _cimg_keycode(SHIFTRIGHT); _cimg_keycode(ARROWUP); _cimg_keycode(CTRLLEFT);
- _cimg_keycode(APPLEFT); _cimg_keycode(ALT); _cimg_keycode(SPACE); _cimg_keycode(ALTGR);
- _cimg_keycode(APPRIGHT); _cimg_keycode(MENU); _cimg_keycode(CTRLRIGHT);
- _cimg_keycode(ARROWLEFT); _cimg_keycode(ARROWDOWN); _cimg_keycode(ARROWRIGHT);
- _cimg_keycode(PAD0); _cimg_keycode(PAD1); _cimg_keycode(PAD2);
- _cimg_keycode(PAD3); _cimg_keycode(PAD4); _cimg_keycode(PAD5);
- _cimg_keycode(PAD6); _cimg_keycode(PAD7); _cimg_keycode(PAD8);
- _cimg_keycode(PAD9); _cimg_keycode(PADADD); _cimg_keycode(PADSUB);
- _cimg_keycode(PADMUL); _cimg_keycode(PADDIV);
- return 0;
- }
- //! Return the current refresh rate, in frames per second.
- /**
- \note Returns a significant value when the current instance is used to display successive frames.
- It measures the delay between successive calls to frames_per_second().
- **/
- float frames_per_second() {
- if (!_fps_timer) _fps_timer = cimg::time();
- const float delta = (float)((cimg::time() - _fps_timer)/1000.f);
- ++_fps_frames;
- if (delta>=1) {
- _fps_fps = _fps_frames/delta;
- _fps_frames = 0;
- _fps_timer = cimg::time();
- }
- return _fps_fps;
- }
- // Move current display window so that its content stays inside the current screen.
- CImgDisplay& move_inside_screen() {
- if (is_empty()) return *this;
- const int
- x0 = window_x(),
- y0 = window_y(),
- x1 = x0 + window_width() - 1,
- y1 = y0 + window_height() - 1,
- sw = CImgDisplay::screen_width(),
- sh = CImgDisplay::screen_height();
- if (x0<0 || y0<0 || x1>=sw || y1>=sh)
- move(std::max(0,std::min(x0,sw - x1 + x0)),
- std::max(0,std::min(y0,sh - y1 + y0)));
- return *this;
- }
- //@}
- //---------------------------------------
- //
- //! \name Window Manipulation
- //@{
- //---------------------------------------
- #if cimg_display==0
- //! Display image on associated window.
- /**
- \param img Input image to display.
- \note This method returns immediately.
- **/
- template<typename T>
- CImgDisplay& display(const CImg<T>& img) {
- return assign(img);
- }
- #endif
- //! Display list of images on associated window.
- /**
- \param list List of images to display.
- \param axis Axis used to append the images along, for the visualization (can be \c x, \c y, \c z or \c c).
- \param align Relative position of aligned images when displaying lists with images of different sizes
- (\c 0 for upper-left, \c 0.5 for centering and \c 1 for lower-right).
- \note This method returns immediately.
- **/
- template<typename T>
- CImgDisplay& display(const CImgList<T>& list, const char axis='x', const float align=0) {
- if (list._width==1) {
- const CImg<T>& img = list[0];
- if (img._depth==1 && (img._spectrum==1 || img._spectrum>=3) && _normalization!=1) return display(img);
- }
- CImgList<typename CImg<T>::ucharT> visu(list._width);
- unsigned int dims = 0;
- cimglist_for(list,l) {
- const CImg<T>& img = list._data[l];
- img._get_select(*this,_normalization,(img._width - 1)/2,(img._height - 1)/2,
- (img._depth - 1)/2).move_to(visu[l]);
- dims = std::max(dims,visu[l]._spectrum);
- }
- cimglist_for(list,l) if (visu[l]._spectrum<dims) visu[l].resize(-100,-100,-100,dims,1);
- visu.get_append(axis,align).display(*this);
- return *this;
- }
- #if cimg_display==0
- //! Show (closed) associated window on the screen.
- /**
- \note
- - Force the associated window of a display to be visible on the screen, even if it has been closed before.
- - Using show() on a visible display does nothing.
- **/
- CImgDisplay& show() {
- return assign();
- }
- //! Close (visible) associated window and make it disappear from the screen.
- /**
- \note
- - A closed display only means the associated window is not visible anymore. This does not mean the display has
- been destroyed.
- Use show() to make the associated window reappear.
- - Using close() on a closed display does nothing.
- **/
- CImgDisplay& close() {
- return assign();
- }
- //! Move associated window to a new location.
- /**
- \param pos_x X-coordinate of the new window location.
- \param pos_y Y-coordinate of the new window location.
- \note Depending on the window manager behavior, this method may not succeed (no exceptions are thrown
- nevertheless).
- **/
- CImgDisplay& move(const int pos_x, const int pos_y) {
- return assign(pos_x,pos_y);
- }
- #endif
- //! Resize display to the size of the associated window.
- /**
- \param force_redraw Tells if the previous window content must be updated and refreshed as well.
- \note
- - Calling this method ensures that width() and window_width() become equal, as well as height() and
- window_height().
- - The associated window is also resized to specified dimensions.
- **/
- CImgDisplay& resize(const bool force_redraw=true) {
- resize(window_width(),window_height(),force_redraw);
- return *this;
- }
- #if cimg_display==0
- //! Resize display to the specified size.
- /**
- \param width Requested display width.
- \param height Requested display height.
- \param force_redraw Tells if the previous window content must be updated and refreshed as well.
- \note The associated window is also resized to specified dimensions.
- **/
- CImgDisplay& resize(const int width, const int height, const bool force_redraw=true) {
- return assign(width,height,0,3,force_redraw);
- }
- #endif
- //! Resize display to the size of an input image.
- /**
- \param img Input image to take size from.
- \param force_redraw Tells if the previous window content must be resized and updated as well.
- \note
- - Calling this method ensures that width() and <tt>img.width()</tt> become equal, as well as height() and
- <tt>img.height()</tt>.
- - The associated window is also resized to specified dimensions.
- **/
- template<typename T>
- CImgDisplay& resize(const CImg<T>& img, const bool force_redraw=true) {
- return resize(img._width,img._height,force_redraw);
- }
- //! Resize display to the size of another CImgDisplay instance.
- /**
- \param disp Input display to take size from.
- \param force_redraw Tells if the previous window content must be resized and updated as well.
- \note
- - Calling this method ensures that width() and <tt>disp.width()</tt> become equal, as well as height() and
- <tt>disp.height()</tt>.
- - The associated window is also resized to specified dimensions.
- **/
- CImgDisplay& resize(const CImgDisplay& disp, const bool force_redraw=true) {
- return resize(disp.width(),disp.height(),force_redraw);
- }
- // [internal] Render pixel buffer with size (wd,hd) from source buffer of size (ws,hs).
- template<typename t, typename T>
- static void _render_resize(const T *ptrs, const unsigned int ws, const unsigned int hs,
- t *ptrd, const unsigned int wd, const unsigned int hd) {
- typedef typename cimg::last<T,cimg_ulong>::type ulongT;
- const ulongT one = (ulongT)1;
- CImg<ulongT> off_x(wd), off_y(hd + 1);
- if (wd==ws) off_x.fill(1);
- else {
- ulongT *poff_x = off_x._data, curr = 0;
- for (unsigned int x = 0; x<wd; ++x) {
- const ulongT old = curr;
- curr = (x + one)*ws/wd;
- *(poff_x++) = curr - old;
- }
- }
- if (hd==hs) off_y.fill(ws);
- else {
- ulongT *poff_y = off_y._data, curr = 0;
- for (unsigned int y = 0; y<hd; ++y) {
- const ulongT old = curr;
- curr = (y + one)*hs/hd;
- *(poff_y++) = ws*(curr - old);
- }
- *poff_y = 0;
- }
- ulongT *poff_y = off_y._data;
- for (unsigned int y = 0; y<hd; ) {
- const T *ptr = ptrs;
- ulongT *poff_x = off_x._data;
- for (unsigned int x = 0; x<wd; ++x) { *(ptrd++) = *ptr; ptr+=*(poff_x++); }
- ++y;
- ulongT dy = *(poff_y++);
- for ( ; !dy && y<hd; std::memcpy(ptrd,ptrd - wd,sizeof(t)*wd), ++y, ptrd+=wd, dy = *(poff_y++)) {}
- ptrs+=dy;
- }
- }
- //! Set normalization type.
- /**
- \param normalization New normalization mode.
- **/
- CImgDisplay& set_normalization(const unsigned int normalization) {
- _normalization = normalization;
- _min = _max = 0;
- return *this;
- }
- #if cimg_display==0
- //! Set title of the associated window.
- /**
- \param format C-string containing the format of the title, as with <tt>std::printf()</tt>.
- \warning As the first argument is a format string, it is highly recommended to write
- \code
- disp.set_title("%s",window_title);
- \endcode
- instead of
- \code
- disp.set_title(window_title);
- \endcode
- if \c window_title can be arbitrary, to prevent nasty memory access.
- **/
- CImgDisplay& set_title(const char *const format, ...) {
- return assign(0,0,format);
- }
- #endif
- //! Enable or disable fullscreen mode.
- /**
- \param is_fullscreen Tells is the fullscreen mode must be activated or not.
- \param force_redraw Tells if the previous window content must be displayed as well.
- \note
- - When the fullscreen mode is enabled, the associated window fills the entire screen but the size of the
- current display is not modified.
- - The screen resolution may be switched to fit the associated window size and ensure it appears the largest
- as possible.
- For X-Window (X11) users, the configuration flag \c cimg_use_xrandr has to be set to allow the screen
- resolution change (requires the X11 extensions to be enabled).
- **/
- CImgDisplay& set_fullscreen(const bool is_fullscreen, const bool force_redraw=true) {
- if (is_empty() || _is_fullscreen==is_fullscreen) return *this;
- return toggle_fullscreen(force_redraw);
- }
- #if cimg_display==0
- //! Toggle fullscreen mode.
- /**
- \param force_redraw Tells if the previous window content must be displayed as well.
- \note Enable fullscreen mode if it was not enabled, and disable it otherwise.
- **/
- CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
- return assign(_width,_height,0,3,force_redraw);
- }
- //! Show mouse pointer.
- /**
- \note Depending on the window manager behavior, this method may not succeed
- (no exceptions are thrown nevertheless).
- **/
- CImgDisplay& show_mouse() {
- return assign();
- }
- //! Hide mouse pointer.
- /**
- \note Depending on the window manager behavior, this method may not succeed
- (no exceptions are thrown nevertheless).
- **/
- CImgDisplay& hide_mouse() {
- return assign();
- }
- //! Move mouse pointer to a specified location.
- /**
- \note Depending on the window manager behavior, this method may not succeed
- (no exceptions are thrown nevertheless).
- **/
- CImgDisplay& set_mouse(const int pos_x, const int pos_y) {
- return assign(pos_x,pos_y);
- }
- #endif
- //! Simulate a mouse button release event.
- /**
- \note All mouse buttons are considered released at the same time.
- **/
- CImgDisplay& set_button() {
- _button = 0;
- _is_event = true;
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- return *this;
- }
- //! Simulate a mouse button press or release event.
- /**
- \param button Buttons event code, where each button is associated to a single bit.
- \param is_pressed Tells if the mouse button is considered as pressed or released.
- **/
- CImgDisplay& set_button(const unsigned int button, const bool is_pressed=true) {
- const unsigned int buttoncode = button==1U?1U:button==2U?2U:button==3U?4U:0U;
- if (is_pressed) _button |= buttoncode; else _button &= ~buttoncode;
- _is_event = buttoncode?true:false;
- if (buttoncode) {
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- }
- return *this;
- }
- //! Flush all mouse wheel events.
- /**
- \note Make wheel() to return \c 0, if called afterwards.
- **/
- CImgDisplay& set_wheel() {
- _wheel = 0;
- _is_event = true;
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- return *this;
- }
- //! Simulate a wheel event.
- /**
- \param amplitude Amplitude of the wheel scrolling to simulate.
- \note Make wheel() to return \c amplitude, if called afterwards.
- **/
- CImgDisplay& set_wheel(const int amplitude) {
- _wheel+=amplitude;
- _is_event = amplitude?true:false;
- if (amplitude) {
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- }
- return *this;
- }
- //! Flush all key events.
- /**
- \note Make key() to return \c 0, if called afterwards.
- **/
- CImgDisplay& set_key() {
- std::memset((void*)_keys,0,128*sizeof(unsigned int));
- std::memset((void*)_released_keys,0,128*sizeof(unsigned int));
- _is_keyESC = _is_keyF1 = _is_keyF2 = _is_keyF3 = _is_keyF4 = _is_keyF5 = _is_keyF6 = _is_keyF7 = _is_keyF8 =
- _is_keyF9 = _is_keyF10 = _is_keyF11 = _is_keyF12 = _is_keyPAUSE = _is_key1 = _is_key2 = _is_key3 = _is_key4 =
- _is_key5 = _is_key6 = _is_key7 = _is_key8 = _is_key9 = _is_key0 = _is_keyBACKSPACE = _is_keyINSERT =
- _is_keyHOME = _is_keyPAGEUP = _is_keyTAB = _is_keyQ = _is_keyW = _is_keyE = _is_keyR = _is_keyT = _is_keyY =
- _is_keyU = _is_keyI = _is_keyO = _is_keyP = _is_keyDELETE = _is_keyEND = _is_keyPAGEDOWN = _is_keyCAPSLOCK =
- _is_keyA = _is_keyS = _is_keyD = _is_keyF = _is_keyG = _is_keyH = _is_keyJ = _is_keyK = _is_keyL =
- _is_keyENTER = _is_keySHIFTLEFT = _is_keyZ = _is_keyX = _is_keyC = _is_keyV = _is_keyB = _is_keyN =
- _is_keyM = _is_keySHIFTRIGHT = _is_keyARROWUP = _is_keyCTRLLEFT = _is_keyAPPLEFT = _is_keyALT = _is_keySPACE =
- _is_keyALTGR = _is_keyAPPRIGHT = _is_keyMENU = _is_keyCTRLRIGHT = _is_keyARROWLEFT = _is_keyARROWDOWN =
- _is_keyARROWRIGHT = _is_keyPAD0 = _is_keyPAD1 = _is_keyPAD2 = _is_keyPAD3 = _is_keyPAD4 = _is_keyPAD5 =
- _is_keyPAD6 = _is_keyPAD7 = _is_keyPAD8 = _is_keyPAD9 = _is_keyPADADD = _is_keyPADSUB = _is_keyPADMUL =
- _is_keyPADDIV = false;
- _is_event = true;
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- return *this;
- }
- //! Simulate a keyboard press/release event.
- /**
- \param keycode Keycode of the associated key.
- \param is_pressed Tells if the key is considered as pressed or released.
- \note Keycode constants are defined in the cimg namespace and are architecture-dependent. Use them to ensure
- your code stay portable (see cimg::keyESC).
- **/
- CImgDisplay& set_key(const unsigned int keycode, const bool is_pressed=true) {
- #define _cimg_set_key(k) if (keycode==cimg::key##k) _is_key##k = is_pressed;
- _cimg_set_key(ESC); _cimg_set_key(F1); _cimg_set_key(F2); _cimg_set_key(F3);
- _cimg_set_key(F4); _cimg_set_key(F5); _cimg_set_key(F6); _cimg_set_key(F7);
- _cimg_set_key(F8); _cimg_set_key(F9); _cimg_set_key(F10); _cimg_set_key(F11);
- _cimg_set_key(F12); _cimg_set_key(PAUSE); _cimg_set_key(1); _cimg_set_key(2);
- _cimg_set_key(3); _cimg_set_key(4); _cimg_set_key(5); _cimg_set_key(6);
- _cimg_set_key(7); _cimg_set_key(8); _cimg_set_key(9); _cimg_set_key(0);
- _cimg_set_key(BACKSPACE); _cimg_set_key(INSERT); _cimg_set_key(HOME);
- _cimg_set_key(PAGEUP); _cimg_set_key(TAB); _cimg_set_key(Q); _cimg_set_key(W);
- _cimg_set_key(E); _cimg_set_key(R); _cimg_set_key(T); _cimg_set_key(Y);
- _cimg_set_key(U); _cimg_set_key(I); _cimg_set_key(O); _cimg_set_key(P);
- _cimg_set_key(DELETE); _cimg_set_key(END); _cimg_set_key(PAGEDOWN);
- _cimg_set_key(CAPSLOCK); _cimg_set_key(A); _cimg_set_key(S); _cimg_set_key(D);
- _cimg_set_key(F); _cimg_set_key(G); _cimg_set_key(H); _cimg_set_key(J);
- _cimg_set_key(K); _cimg_set_key(L); _cimg_set_key(ENTER);
- _cimg_set_key(SHIFTLEFT); _cimg_set_key(Z); _cimg_set_key(X); _cimg_set_key(C);
- _cimg_set_key(V); _cimg_set_key(B); _cimg_set_key(N); _cimg_set_key(M);
- _cimg_set_key(SHIFTRIGHT); _cimg_set_key(ARROWUP); _cimg_set_key(CTRLLEFT);
- _cimg_set_key(APPLEFT); _cimg_set_key(ALT); _cimg_set_key(SPACE); _cimg_set_key(ALTGR);
- _cimg_set_key(APPRIGHT); _cimg_set_key(MENU); _cimg_set_key(CTRLRIGHT);
- _cimg_set_key(ARROWLEFT); _cimg_set_key(ARROWDOWN); _cimg_set_key(ARROWRIGHT);
- _cimg_set_key(PAD0); _cimg_set_key(PAD1); _cimg_set_key(PAD2);
- _cimg_set_key(PAD3); _cimg_set_key(PAD4); _cimg_set_key(PAD5);
- _cimg_set_key(PAD6); _cimg_set_key(PAD7); _cimg_set_key(PAD8);
- _cimg_set_key(PAD9); _cimg_set_key(PADADD); _cimg_set_key(PADSUB);
- _cimg_set_key(PADMUL); _cimg_set_key(PADDIV);
- if (is_pressed) {
- if (*_keys)
- std::memmove((void*)(_keys + 1),(void*)_keys,127*sizeof(unsigned int));
- *_keys = keycode;
- if (*_released_keys) {
- std::memmove((void*)(_released_keys + 1),(void*)_released_keys,127*sizeof(unsigned int));
- *_released_keys = 0;
- }
- } else {
- if (*_keys) {
- std::memmove((void*)(_keys + 1),(void*)_keys,127*sizeof(unsigned int));
- *_keys = 0;
- }
- if (*_released_keys)
- std::memmove((void*)(_released_keys + 1),(void*)_released_keys,127*sizeof(unsigned int));
- *_released_keys = keycode;
- }
- _is_event = keycode?true:false;
- if (keycode) {
- #if cimg_display==1
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- #elif cimg_display==2
- SetEvent(cimg::Win32_attr().wait_event);
- #endif
- }
- return *this;
- }
- //! Flush all display events.
- /**
- \note Remove all passed events from the current display.
- **/
- CImgDisplay& flush() {
- set_key().set_button().set_wheel();
- _is_resized = _is_moved = _is_event = false;
- _fps_timer = _fps_frames = _timer = 0;
- _fps_fps = 0;
- return *this;
- }
- //! Wait for any user event occurring on the current display.
- CImgDisplay& wait() {
- wait(*this);
- return *this;
- }
- //! Wait for a given number of milliseconds since the last call to wait().
- /**
- \param milliseconds Number of milliseconds to wait for.
- \note Similar to cimg::wait().
- **/
- CImgDisplay& wait(const unsigned int milliseconds) {
- cimg::wait(milliseconds,&_timer);
- return *this;
- }
- //! Wait for any event occurring on the display \c disp1.
- static void wait(CImgDisplay& disp1) {
- disp1._is_event = false;
- while (!disp1._is_closed && !disp1._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1 or \c disp2.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2) {
- disp1._is_event = disp2._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed) &&
- !disp1._is_event && !disp2._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2 or \c disp3.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3) {
- disp1._is_event = disp2._is_event = disp3._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3 or \c disp4.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4 or \c disp5.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4,
- CImgDisplay& disp5) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event)
- wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp6.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
- CImgDisplay& disp6) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
- disp6._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
- !disp6._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
- !disp6._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp7.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
- CImgDisplay& disp6, CImgDisplay& disp7) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
- disp6._is_event = disp7._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
- !disp6._is_closed || !disp7._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
- !disp6._is_event && !disp7._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp8.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
- CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
- disp6._is_event = disp7._is_event = disp8._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
- !disp6._is_closed || !disp7._is_closed || !disp8._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
- !disp6._is_event && !disp7._is_event && !disp8._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp9.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
- CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
- disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
- !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
- !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event) wait_all();
- }
- //! Wait for any event occurring either on the display \c disp1, \c disp2, \c disp3, \c disp4, ... \c disp10.
- static void wait(CImgDisplay& disp1, CImgDisplay& disp2, CImgDisplay& disp3, CImgDisplay& disp4, CImgDisplay& disp5,
- CImgDisplay& disp6, CImgDisplay& disp7, CImgDisplay& disp8, CImgDisplay& disp9,
- CImgDisplay& disp10) {
- disp1._is_event = disp2._is_event = disp3._is_event = disp4._is_event = disp5._is_event =
- disp6._is_event = disp7._is_event = disp8._is_event = disp9._is_event = disp10._is_event = false;
- while ((!disp1._is_closed || !disp2._is_closed || !disp3._is_closed || !disp4._is_closed || !disp5._is_closed ||
- !disp6._is_closed || !disp7._is_closed || !disp8._is_closed || !disp9._is_closed || !disp10._is_closed) &&
- !disp1._is_event && !disp2._is_event && !disp3._is_event && !disp4._is_event && !disp5._is_event &&
- !disp6._is_event && !disp7._is_event && !disp8._is_event && !disp9._is_event && !disp10._is_event)
- wait_all();
- }
- #if cimg_display==0
- //! Wait for any window event occurring in any opened CImgDisplay.
- static void wait_all() {
- return _no_display_exception();
- }
- //! Render image into internal display buffer.
- /**
- \param img Input image data to render.
- \note
- - Convert image data representation into the internal display buffer (architecture-dependent structure).
- - The content of the associated window is not modified, until paint() is called.
- - Should not be used for common CImgDisplay uses, since display() is more useful.
- **/
- template<typename T>
- CImgDisplay& render(const CImg<T>& img) {
- return assign(img);
- }
- //! Paint internal display buffer on associated window.
- /**
- \note
- - Update the content of the associated window with the internal display buffer, e.g. after a render() call.
- - Should not be used for common CImgDisplay uses, since display() is more useful.
- **/
- CImgDisplay& paint() {
- return assign();
- }
- //! Take a snapshot of the current screen content.
- /**
- \param x0 X-coordinate of the upper left corner.
- \param y0 Y-coordinate of the upper left corner.
- \param x1 X-coordinate of the lower right corner.
- \param y1 Y-coordinate of the lower right corner.
- \param[out] img Output screenshot. Can be empty on input
- **/
- template<typename T>
- static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
- cimg::unused(x0,y0,x1,y1,&img);
- _no_display_exception();
- }
- //! Take a snapshot of the associated window content.
- /**
- \param[out] img Output snapshot. Can be empty on input.
- **/
- template<typename T>
- const CImgDisplay& snapshot(CImg<T>& img) const {
- cimg::unused(img);
- _no_display_exception();
- return *this;
- }
- #endif
- // X11-based implementation
- //--------------------------
- #if cimg_display==1
- Atom _wm_window_atom, _wm_protocol_atom;
- Window _window, _background_window;
- Colormap _colormap;
- XImage *_image;
- void *_data;
- #ifdef cimg_use_xshm
- XShmSegmentInfo *_shminfo;
- #endif
- static int screen_width() {
- Display *const dpy = cimg::X11_attr().display;
- int res = 0;
- if (!dpy) {
- Display *const _dpy = XOpenDisplay(0);
- if (!_dpy)
- throw CImgDisplayException("CImgDisplay::screen_width(): Failed to open X11 display.");
- res = DisplayWidth(_dpy,DefaultScreen(_dpy));
- XCloseDisplay(_dpy);
- } else {
- #ifdef cimg_use_xrandr
- if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution)
- res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width;
- else res = DisplayWidth(dpy,DefaultScreen(dpy));
- #else
- res = DisplayWidth(dpy,DefaultScreen(dpy));
- #endif
- }
- return res;
- }
- static int screen_height() {
- Display *const dpy = cimg::X11_attr().display;
- int res = 0;
- if (!dpy) {
- Display *const _dpy = XOpenDisplay(0);
- if (!_dpy)
- throw CImgDisplayException("CImgDisplay::screen_height(): Failed to open X11 display.");
- res = DisplayHeight(_dpy,DefaultScreen(_dpy));
- XCloseDisplay(_dpy);
- } else {
- #ifdef cimg_use_xrandr
- if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution)
- res = cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height;
- else res = DisplayHeight(dpy,DefaultScreen(dpy));
- #else
- res = DisplayHeight(dpy,DefaultScreen(dpy));
- #endif
- }
- return res;
- }
- static void wait_all() {
- if (!cimg::X11_attr().display) return;
- pthread_mutex_lock(&cimg::X11_attr().wait_event_mutex);
- pthread_cond_wait(&cimg::X11_attr().wait_event,&cimg::X11_attr().wait_event_mutex);
- pthread_mutex_unlock(&cimg::X11_attr().wait_event_mutex);
- }
- void _handle_events(const XEvent *const pevent) {
- Display *const dpy = cimg::X11_attr().display;
- XEvent event = *pevent;
- switch (event.type) {
- case ClientMessage : {
- if ((int)event.xclient.message_type==(int)_wm_protocol_atom &&
- (int)event.xclient.data.l[0]==(int)_wm_window_atom) {
- XUnmapWindow(cimg::X11_attr().display,_window);
- _is_closed = _is_event = true;
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- }
- } break;
- case ConfigureNotify : {
- while (XCheckWindowEvent(dpy,_window,StructureNotifyMask,&event)) {}
- const unsigned int nw = event.xconfigure.width, nh = event.xconfigure.height;
- const int nx = event.xconfigure.x, ny = event.xconfigure.y;
- if (nw && nh && (nw!=_window_width || nh!=_window_height)) {
- _window_width = nw; _window_height = nh; _mouse_x = _mouse_y = -1;
- XResizeWindow(dpy,_window,_window_width,_window_height);
- _is_resized = _is_event = true;
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- }
- if (nx!=_window_x || ny!=_window_y) {
- _window_x = nx;
- _window_y = ny;
- _is_moved = _is_event = true;
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- }
- } break;
- case Expose : {
- while (XCheckWindowEvent(dpy,_window,ExposureMask,&event)) {}
- _paint(false);
- if (_is_fullscreen) {
- XWindowAttributes attr;
- do {
- XGetWindowAttributes(dpy,_window,&attr);
- if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
- } while (attr.map_state!=IsViewable);
- XSetInputFocus(dpy,_window,RevertToParent,CurrentTime);
- }
- } break;
- case ButtonPress : {
- do {
- _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y;
- if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
- switch (event.xbutton.button) {
- case 1 : set_button(1); break;
- case 3 : set_button(2); break;
- case 2 : set_button(3); break;
- }
- } while (XCheckWindowEvent(dpy,_window,ButtonPressMask,&event));
- } break;
- case ButtonRelease : {
- do {
- _mouse_x = event.xmotion.x; _mouse_y = event.xmotion.y;
- if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
- switch (event.xbutton.button) {
- case 1 : set_button(1,false); break;
- case 3 : set_button(2,false); break;
- case 2 : set_button(3,false); break;
- case 4 : set_wheel(1); break;
- case 5 : set_wheel(-1); break;
- }
- } while (XCheckWindowEvent(dpy,_window,ButtonReleaseMask,&event));
- } break;
- case KeyPress : {
- char tmp = 0; KeySym ksym;
- XLookupString(&event.xkey,&tmp,1,&ksym,0);
- set_key((unsigned int)ksym,true);
- } break;
- case KeyRelease : {
- char keys_return[32]; // Check that the key has been physically unpressed
- XQueryKeymap(dpy,keys_return);
- const unsigned int kc = event.xkey.keycode, kc1 = kc/8, kc2 = kc%8;
- const bool is_key_pressed = kc1>=32?false:(keys_return[kc1]>>kc2)&1;
- if (!is_key_pressed) {
- char tmp = 0; KeySym ksym;
- XLookupString(&event.xkey,&tmp,1,&ksym,0);
- set_key((unsigned int)ksym,false);
- }
- } break;
- case EnterNotify: {
- while (XCheckWindowEvent(dpy,_window,EnterWindowMask,&event)) {}
- _mouse_x = event.xmotion.x;
- _mouse_y = event.xmotion.y;
- if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
- } break;
- case LeaveNotify : {
- while (XCheckWindowEvent(dpy,_window,LeaveWindowMask,&event)) {}
- _mouse_x = _mouse_y = -1; _is_event = true;
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- } break;
- case MotionNotify : {
- while (XCheckWindowEvent(dpy,_window,PointerMotionMask,&event)) {}
- _mouse_x = event.xmotion.x;
- _mouse_y = event.xmotion.y;
- if (_mouse_x<0 || _mouse_y<0 || _mouse_x>=width() || _mouse_y>=height()) _mouse_x = _mouse_y = -1;
- _is_event = true;
- pthread_cond_broadcast(&cimg::X11_attr().wait_event);
- } break;
- }
- }
- static void* _events_thread(void *arg) { // Thread to manage events for all opened display windows
- Display *const dpy = cimg::X11_attr().display;
- XEvent event;
- pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,0);
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,0);
- if (!arg) for ( ; ; ) {
- cimg_lock_display();
- bool event_flag = XCheckTypedEvent(dpy,ClientMessage,&event);
- if (!event_flag) event_flag = XCheckMaskEvent(dpy,
- ExposureMask | StructureNotifyMask | ButtonPressMask |
- KeyPressMask | PointerMotionMask | EnterWindowMask |
- LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask,&event);
- if (event_flag)
- for (unsigned int i = 0; i<cimg::X11_attr().nb_wins; ++i)
- if (!cimg::X11_attr().wins[i]->_is_closed && event.xany.window==cimg::X11_attr().wins[i]->_window)
- cimg::X11_attr().wins[i]->_handle_events(&event);
- cimg_unlock_display();
- pthread_testcancel();
- cimg::sleep(8);
- }
- return 0;
- }
- void _set_colormap(Colormap& cmap, const unsigned int dim) {
- XColor *const colormap = new XColor[256];
- switch (dim) {
- case 1 : { // colormap for greyscale images
- for (unsigned int index = 0; index<256; ++index) {
- colormap[index].pixel = index;
- colormap[index].red = colormap[index].green = colormap[index].blue = (unsigned short)(index<<8);
- colormap[index].flags = DoRed | DoGreen | DoBlue;
- }
- } break;
- case 2 : { // colormap for RG images
- for (unsigned int index = 0, r = 8; r<256; r+=16)
- for (unsigned int g = 8; g<256; g+=16) {
- colormap[index].pixel = index;
- colormap[index].red = colormap[index].blue = (unsigned short)(r<<8);
- colormap[index].green = (unsigned short)(g<<8);
- colormap[index++].flags = DoRed | DoGreen | DoBlue;
- }
- } break;
- default : { // colormap for RGB images
- for (unsigned int index = 0, r = 16; r<256; r+=32)
- for (unsigned int g = 16; g<256; g+=32)
- for (unsigned int b = 32; b<256; b+=64) {
- colormap[index].pixel = index;
- colormap[index].red = (unsigned short)(r<<8);
- colormap[index].green = (unsigned short)(g<<8);
- colormap[index].blue = (unsigned short)(b<<8);
- colormap[index++].flags = DoRed | DoGreen | DoBlue;
- }
- }
- }
- XStoreColors(cimg::X11_attr().display,cmap,colormap,256);
- delete[] colormap;
- }
- void _map_window() {
- Display *const dpy = cimg::X11_attr().display;
- bool is_exposed = false, is_mapped = false;
- XWindowAttributes attr;
- XEvent event;
- XMapRaised(dpy,_window);
- do { // Wait for the window to be mapped
- XWindowEvent(dpy,_window,StructureNotifyMask | ExposureMask,&event);
- switch (event.type) {
- case MapNotify : is_mapped = true; break;
- case Expose : is_exposed = true; break;
- }
- } while (!is_exposed || !is_mapped);
- do { // Wait for the window to be visible
- XGetWindowAttributes(dpy,_window,&attr);
- if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
- } while (attr.map_state!=IsViewable);
- _window_x = attr.x;
- _window_y = attr.y;
- }
- void _paint(const bool wait_expose=true) {
- if (_is_closed || !_image) return;
- Display *const dpy = cimg::X11_attr().display;
- if (wait_expose) { // Send an expose event sticked to display window to force repaint
- XEvent event;
- event.xexpose.type = Expose;
- event.xexpose.serial = 0;
- event.xexpose.send_event = 1;
- event.xexpose.display = dpy;
- event.xexpose.window = _window;
- event.xexpose.x = 0;
- event.xexpose.y = 0;
- event.xexpose.width = width();
- event.xexpose.height = height();
- event.xexpose.count = 0;
- XSendEvent(dpy,_window,0,0,&event);
- } else { // Repaint directly (may be called from the expose event)
- GC gc = DefaultGC(dpy,DefaultScreen(dpy));
- #ifdef cimg_use_xshm
- if (_shminfo) XShmPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height,1);
- else XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height);
- #else
- XPutImage(dpy,_window,gc,_image,0,0,0,0,_width,_height);
- #endif
- }
- }
- template<typename T>
- void _resize(T pixel_type, const unsigned int ndimx, const unsigned int ndimy, const bool force_redraw) {
- Display *const dpy = cimg::X11_attr().display;
- cimg::unused(pixel_type);
- #ifdef cimg_use_xshm
- if (_shminfo) {
- XShmSegmentInfo *const nshminfo = new XShmSegmentInfo;
- XImage *const nimage = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),
- cimg::X11_attr().nb_bits,ZPixmap,0,nshminfo,ndimx,ndimy);
- if (!nimage) { delete nshminfo; return; }
- else {
- nshminfo->shmid = shmget(IPC_PRIVATE,ndimx*ndimy*sizeof(T),IPC_CREAT | 0777);
- if (nshminfo->shmid==-1) { XDestroyImage(nimage); delete nshminfo; return; }
- else {
- nshminfo->shmaddr = nimage->data = (char*)shmat(nshminfo->shmid,0,0);
- if (nshminfo->shmaddr==(char*)-1) {
- shmctl(nshminfo->shmid,IPC_RMID,0); XDestroyImage(nimage); delete nshminfo; return;
- } else {
- nshminfo->readOnly = 0;
- cimg::X11_attr().is_shm_enabled = true;
- XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm);
- XShmAttach(dpy,nshminfo);
- XFlush(dpy);
- XSetErrorHandler(oldXErrorHandler);
- if (!cimg::X11_attr().is_shm_enabled) {
- shmdt(nshminfo->shmaddr);
- shmctl(nshminfo->shmid,IPC_RMID,0);
- XDestroyImage(nimage);
- delete nshminfo;
- return;
- } else {
- T *const ndata = (T*)nimage->data;
- if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy);
- else std::memset(ndata,0,sizeof(T)*ndimx*ndimy);
- XShmDetach(dpy,_shminfo);
- XDestroyImage(_image);
- shmdt(_shminfo->shmaddr);
- shmctl(_shminfo->shmid,IPC_RMID,0);
- delete _shminfo;
- _shminfo = nshminfo;
- _image = nimage;
- _data = (void*)ndata;
- }
- }
- }
- }
- } else
- #endif
- {
- T *ndata = (T*)std::malloc(ndimx*ndimy*sizeof(T));
- if (force_redraw) _render_resize((T*)_data,_width,_height,ndata,ndimx,ndimy);
- else std::memset(ndata,0,sizeof(T)*ndimx*ndimy);
- _data = (void*)ndata;
- XDestroyImage(_image);
- _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),
- cimg::X11_attr().nb_bits,ZPixmap,0,(char*)_data,ndimx,ndimy,8,0);
- }
- }
- void _init_fullscreen() {
- if (!_is_fullscreen || _is_closed) return;
- Display *const dpy = cimg::X11_attr().display;
- _background_window = 0;
- #ifdef cimg_use_xrandr
- int foo;
- if (XRRQueryExtension(dpy,&foo,&foo)) {
- XRRRotations(dpy,DefaultScreen(dpy),&cimg::X11_attr().curr_rotation);
- if (!cimg::X11_attr().resolutions) {
- cimg::X11_attr().resolutions = XRRSizes(dpy,DefaultScreen(dpy),&foo);
- cimg::X11_attr().nb_resolutions = (unsigned int)foo;
- }
- if (cimg::X11_attr().resolutions) {
- cimg::X11_attr().curr_resolution = 0;
- for (unsigned int i = 0; i<cimg::X11_attr().nb_resolutions; ++i) {
- const unsigned int
- nw = (unsigned int)(cimg::X11_attr().resolutions[i].width),
- nh = (unsigned int)(cimg::X11_attr().resolutions[i].height);
- if (nw>=_width && nh>=_height &&
- nw<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].width) &&
- nh<=(unsigned int)(cimg::X11_attr().resolutions[cimg::X11_attr().curr_resolution].height))
- cimg::X11_attr().curr_resolution = i;
- }
- if (cimg::X11_attr().curr_resolution>0) {
- XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy));
- XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),
- cimg::X11_attr().curr_resolution,cimg::X11_attr().curr_rotation,CurrentTime);
- XRRFreeScreenConfigInfo(config);
- XSync(dpy,0);
- }
- }
- }
- if (!cimg::X11_attr().resolutions)
- cimg::warn(_cimgdisplay_instance
- "init_fullscreen(): Xrandr extension not supported by the X server.",
- cimgdisplay_instance);
- #endif
- const unsigned int sx = screen_width(), sy = screen_height();
- if (sx==_width && sy==_height) return;
- XSetWindowAttributes attr_set;
- attr_set.background_pixel = XBlackPixel(dpy,XDefaultScreen(dpy));
- attr_set.override_redirect = 1;
- _background_window = XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,sx,sy,0,0,
- InputOutput,CopyFromParent,CWBackPixel | CWOverrideRedirect,&attr_set);
- XEvent event;
- XSelectInput(dpy,_background_window,StructureNotifyMask);
- XMapRaised(dpy,_background_window);
- do XWindowEvent(dpy,_background_window,StructureNotifyMask,&event);
- while (event.type!=MapNotify);
- XWindowAttributes attr;
- do {
- XGetWindowAttributes(dpy,_background_window,&attr);
- if (attr.map_state!=IsViewable) { XSync(dpy,0); cimg::sleep(10); }
- } while (attr.map_state!=IsViewable);
- }
- void _desinit_fullscreen() {
- if (!_is_fullscreen) return;
- Display *const dpy = cimg::X11_attr().display;
- XUngrabKeyboard(dpy,CurrentTime);
- #ifdef cimg_use_xrandr
- if (cimg::X11_attr().resolutions && cimg::X11_attr().curr_resolution) {
- XRRScreenConfiguration *config = XRRGetScreenInfo(dpy,DefaultRootWindow(dpy));
- XRRSetScreenConfig(dpy,config,DefaultRootWindow(dpy),0,cimg::X11_attr().curr_rotation,CurrentTime);
- XRRFreeScreenConfigInfo(config);
- XSync(dpy,0);
- cimg::X11_attr().curr_resolution = 0;
- }
- #endif
- if (_background_window) XDestroyWindow(dpy,_background_window);
- _background_window = 0;
- _is_fullscreen = false;
- }
- static int _assign_xshm(Display *dpy, XErrorEvent *error) {
- cimg::unused(dpy,error);
- cimg::X11_attr().is_shm_enabled = false;
- return 0;
- }
- void _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- cimg::mutex(14);
- // Allocate space for window title
- const char *const nptitle = ptitle?ptitle:"";
- const unsigned int s = (unsigned int)std::strlen(nptitle) + 1;
- char *const tmp_title = s?new char[s]:0;
- if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char));
- // Destroy previous display window if existing
- if (!is_empty()) assign();
- // Open X11 display and retrieve graphical properties.
- Display* &dpy = cimg::X11_attr().display;
- if (!dpy) {
- dpy = XOpenDisplay(0);
- if (!dpy)
- throw CImgDisplayException(_cimgdisplay_instance
- "assign(): Failed to open X11 display.",
- cimgdisplay_instance);
- cimg::X11_attr().nb_bits = DefaultDepth(dpy,DefaultScreen(dpy));
- if (cimg::X11_attr().nb_bits!=8 && cimg::X11_attr().nb_bits!=16 &&
- cimg::X11_attr().nb_bits!=24 && cimg::X11_attr().nb_bits!=32)
- throw CImgDisplayException(_cimgdisplay_instance
- "assign(): Invalid %u bits screen mode detected "
- "(only 8, 16, 24 and 32 bits modes are managed).",
- cimgdisplay_instance,
- cimg::X11_attr().nb_bits);
- XVisualInfo vtemplate;
- vtemplate.visualid = XVisualIDFromVisual(DefaultVisual(dpy,DefaultScreen(dpy)));
- int nb_visuals;
- XVisualInfo *vinfo = XGetVisualInfo(dpy,VisualIDMask,&vtemplate,&nb_visuals);
- if (vinfo && vinfo->red_mask<vinfo->blue_mask) cimg::X11_attr().is_blue_first = true;
- cimg::X11_attr().byte_order = ImageByteOrder(dpy);
- XFree(vinfo);
- cimg_lock_display();
- cimg::X11_attr().events_thread = new pthread_t;
- pthread_create(cimg::X11_attr().events_thread,0,_events_thread,0);
- } else cimg_lock_display();
- // Set display variables.
- _width = std::min(dimw,(unsigned int)screen_width());
- _height = std::min(dimh,(unsigned int)screen_height());
- _normalization = normalization_type<4?normalization_type:3;
- _is_fullscreen = fullscreen_flag;
- _window_x = _window_y = cimg::type<int>::min();
- _is_closed = closed_flag;
- _title = tmp_title;
- flush();
- // Create X11 window (and LUT, if 8bits display)
- if (_is_fullscreen) {
- if (!_is_closed) _init_fullscreen();
- const unsigned int sx = screen_width(), sy = screen_height();
- XSetWindowAttributes attr_set;
- attr_set.override_redirect = 1;
- _window = XCreateWindow(dpy,DefaultRootWindow(dpy),(sx - _width)/2,(sy - _height)/2,_width,_height,0,0,
- InputOutput,CopyFromParent,CWOverrideRedirect,&attr_set);
- } else
- _window = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,_width,_height,0,0L,0L);
- XSelectInput(dpy,_window,
- ExposureMask | StructureNotifyMask | ButtonPressMask | KeyPressMask | PointerMotionMask |
- EnterWindowMask | LeaveWindowMask | ButtonReleaseMask | KeyReleaseMask);
- XStoreName(dpy,_window,_title?_title:" ");
- if (cimg::X11_attr().nb_bits==8) {
- _colormap = XCreateColormap(dpy,_window,DefaultVisual(dpy,DefaultScreen(dpy)),AllocAll);
- _set_colormap(_colormap,3);
- XSetWindowColormap(dpy,_window,_colormap);
- }
- static const char *const _window_class = cimg_appname;
- XClassHint *const window_class = XAllocClassHint();
- window_class->res_name = (char*)_window_class;
- window_class->res_class = (char*)_window_class;
- XSetClassHint(dpy,_window,window_class);
- XFree(window_class);
- _window_width = _width;
- _window_height = _height;
- // Create XImage
- #ifdef cimg_use_xshm
- _shminfo = 0;
- if (XShmQueryExtension(dpy)) {
- _shminfo = new XShmSegmentInfo;
- _image = XShmCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits,
- ZPixmap,0,_shminfo,_width,_height);
- if (!_image) { delete _shminfo; _shminfo = 0; }
- else {
- _shminfo->shmid = shmget(IPC_PRIVATE,_image->bytes_per_line*_image->height,IPC_CREAT|0777);
- if (_shminfo->shmid==-1) { XDestroyImage(_image); delete _shminfo; _shminfo = 0; }
- else {
- _shminfo->shmaddr = _image->data = (char*)(_data = shmat(_shminfo->shmid,0,0));
- if (_shminfo->shmaddr==(char*)-1) {
- shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image); delete _shminfo; _shminfo = 0;
- } else {
- _shminfo->readOnly = 0;
- cimg::X11_attr().is_shm_enabled = true;
- XErrorHandler oldXErrorHandler = XSetErrorHandler(_assign_xshm);
- XShmAttach(dpy,_shminfo);
- XSync(dpy,0);
- XSetErrorHandler(oldXErrorHandler);
- if (!cimg::X11_attr().is_shm_enabled) {
- shmdt(_shminfo->shmaddr); shmctl(_shminfo->shmid,IPC_RMID,0); XDestroyImage(_image);
- delete _shminfo; _shminfo = 0;
- }
- }
- }
- }
- }
- if (!_shminfo)
- #endif
- {
- const cimg_ulong buf_size = (cimg_ulong)_width*_height*(cimg::X11_attr().nb_bits==8?1:
- (cimg::X11_attr().nb_bits==16?2:4));
- _data = std::malloc(buf_size);
- _image = XCreateImage(dpy,DefaultVisual(dpy,DefaultScreen(dpy)),cimg::X11_attr().nb_bits,
- ZPixmap,0,(char*)_data,_width,_height,8,0);
- }
- _wm_window_atom = XInternAtom(dpy,"WM_DELETE_WINDOW",0);
- _wm_protocol_atom = XInternAtom(dpy,"WM_PROTOCOLS",0);
- XSetWMProtocols(dpy,_window,&_wm_window_atom,1);
- if (_is_fullscreen) XGrabKeyboard(dpy,_window,1,GrabModeAsync,GrabModeAsync,CurrentTime);
- cimg::X11_attr().wins[cimg::X11_attr().nb_wins++]=this;
- if (!_is_closed) _map_window(); else _window_x = _window_y = cimg::type<int>::min();
- cimg_unlock_display();
- cimg::mutex(14,0);
- }
- CImgDisplay& assign() {
- if (is_empty()) return flush();
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- // Remove display window from event thread list.
- unsigned int i;
- for (i = 0; i<cimg::X11_attr().nb_wins && cimg::X11_attr().wins[i]!=this; ++i) {}
- for ( ; i<cimg::X11_attr().nb_wins - 1; ++i) cimg::X11_attr().wins[i] = cimg::X11_attr().wins[i + 1];
- --cimg::X11_attr().nb_wins;
- // Destroy window, image, colormap and title.
- if (_is_fullscreen && !_is_closed) _desinit_fullscreen();
- #ifdef cimg_use_xshm
- if (_shminfo) {
- XShmDetach(dpy,_shminfo);
- shmdt(_shminfo->shmaddr);
- shmctl(_shminfo->shmid,IPC_RMID,0);
- delete _shminfo;
- _shminfo = 0;
- }
- #endif
- XDestroyImage(_image);
- if (cimg::X11_attr().nb_bits==8) XFreeColormap(dpy,_colormap);
- XDestroyWindow(dpy,_window);
- XSync(dpy,0);
- _window = 0; _colormap = 0; _data = 0; _image = 0;
- // Reset display variables.
- delete[] _title;
- _width = _height = _normalization = _window_width = _window_height = 0;
- _window_x = _window_y = cimg::type<int>::min();
- _is_fullscreen = false;
- _is_closed = true;
- _min = _max = 0;
- _title = 0;
- flush();
- cimg_unlock_display();
- return *this;
- }
- CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!dimw || !dimh) return assign();
- _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag);
- _min = _max = 0;
- std::memset(_data,0,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char):
- (cimg::X11_attr().nb_bits==16?sizeof(unsigned short):sizeof(unsigned int)))*
- (size_t)_width*_height);
- return paint();
- }
- template<typename T>
- CImgDisplay& assign(const CImg<T>& img, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!img) return assign();
- CImg<T> tmp;
- const CImg<T>& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
- (img._height - 1)/2,
- (img._depth - 1)/2));
- _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
- if (_normalization==2) _min = (float)nimg.min_max(_max);
- return render(nimg).paint();
- }
- template<typename T>
- CImgDisplay& assign(const CImgList<T>& list, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!list) return assign();
- CImg<T> tmp;
- const CImg<T> img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
- (img._height - 1)/2,
- (img._depth - 1)/2));
- _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
- if (_normalization==2) _min = (float)nimg.min_max(_max);
- return render(nimg).paint();
- }
- CImgDisplay& assign(const CImgDisplay& disp) {
- if (!disp) return assign();
- _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed);
- std::memcpy(_data,disp._data,(cimg::X11_attr().nb_bits==8?sizeof(unsigned char):
- cimg::X11_attr().nb_bits==16?sizeof(unsigned short):
- sizeof(unsigned int))*(size_t)_width*_height);
- return paint();
- }
- CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) {
- if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign();
- if (is_empty()) return assign(nwidth,nheight);
- Display *const dpy = cimg::X11_attr().display;
- const unsigned int
- tmpdimx = (nwidth>0)?nwidth:(-nwidth*width()/100),
- tmpdimy = (nheight>0)?nheight:(-nheight*height()/100),
- dimx = tmpdimx?tmpdimx:1,
- dimy = tmpdimy?tmpdimy:1;
- if (_width!=dimx || _height!=dimy || _window_width!=dimx || _window_height!=dimy) {
- show();
- cimg_lock_display();
- if (_window_width!=dimx || _window_height!=dimy) {
- XWindowAttributes attr;
- for (unsigned int i = 0; i<10; ++i) {
- XResizeWindow(dpy,_window,dimx,dimy);
- XGetWindowAttributes(dpy,_window,&attr);
- if (attr.width==(int)dimx && attr.height==(int)dimy) break;
- cimg::wait(5,&_timer);
- }
- }
- if (_width!=dimx || _height!=dimy) switch (cimg::X11_attr().nb_bits) {
- case 8 : { unsigned char pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break;
- case 16 : { unsigned short pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); } break;
- default : { unsigned int pixel_type = 0; _resize(pixel_type,dimx,dimy,force_redraw); }
- }
- _window_width = _width = dimx; _window_height = _height = dimy;
- cimg_unlock_display();
- }
- _is_resized = false;
- if (_is_fullscreen) move((screen_width() - _width)/2,(screen_height() - _height)/2);
- if (force_redraw) return paint();
- return *this;
- }
- CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
- if (is_empty()) return *this;
- if (force_redraw) {
- const cimg_ulong buf_size = (cimg_ulong)_width*_height*
- (cimg::X11_attr().nb_bits==8?1:(cimg::X11_attr().nb_bits==16?2:4));
- void *image_data = std::malloc(buf_size);
- std::memcpy(image_data,_data,buf_size);
- assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
- std::memcpy(_data,image_data,buf_size);
- std::free(image_data);
- return paint();
- }
- return assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
- }
- CImgDisplay& show() {
- if (is_empty() || !_is_closed) return *this;
- cimg_lock_display();
- _is_closed = false;
- if (_is_fullscreen) _init_fullscreen();
- _map_window();
- cimg_unlock_display();
- return paint();
- }
- CImgDisplay& close() {
- if (is_empty() || _is_closed) return *this;
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- if (_is_fullscreen) _desinit_fullscreen();
- XUnmapWindow(dpy,_window);
- _window_x = _window_y = cimg::type<int>::min();
- _is_closed = true;
- cimg_unlock_display();
- return *this;
- }
- CImgDisplay& move(const int posx, const int posy) {
- if (is_empty()) return *this;
- show();
- if (_window_x!=posx || _window_y!=posy) {
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- XMoveWindow(dpy,_window,posx,posy);
- _window_x = posx;
- _window_y = posy;
- cimg_unlock_display();
- }
- _is_moved = false;
- return paint();
- }
- CImgDisplay& show_mouse() {
- if (is_empty()) return *this;
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- XUndefineCursor(dpy,_window);
- cimg_unlock_display();
- return *this;
- }
- CImgDisplay& hide_mouse() {
- if (is_empty()) return *this;
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- static const char pix_data[8] = { 0 };
- XColor col;
- col.red = col.green = col.blue = 0;
- Pixmap pix = XCreateBitmapFromData(dpy,_window,pix_data,8,8);
- Cursor cur = XCreatePixmapCursor(dpy,pix,pix,&col,&col,0,0);
- XFreePixmap(dpy,pix);
- XDefineCursor(dpy,_window,cur);
- cimg_unlock_display();
- return *this;
- }
- CImgDisplay& set_mouse(const int posx, const int posy) {
- if (is_empty() || _is_closed) return *this;
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- XWarpPointer(dpy,0L,_window,0,0,0,0,posx,posy);
- _mouse_x = posx; _mouse_y = posy;
- _is_moved = false;
- XSync(dpy,0);
- cimg_unlock_display();
- return *this;
- }
- CImgDisplay& set_title(const char *const format, ...) {
- if (is_empty()) return *this;
- char *const tmp = new char[1024];
- va_list ap;
- va_start(ap, format);
- cimg_vsnprintf(tmp,1024,format,ap);
- va_end(ap);
- if (!std::strcmp(_title,tmp)) { delete[] tmp; return *this; }
- delete[] _title;
- const unsigned int s = (unsigned int)std::strlen(tmp) + 1;
- _title = new char[s];
- std::memcpy(_title,tmp,s*sizeof(char));
- Display *const dpy = cimg::X11_attr().display;
- cimg_lock_display();
- XStoreName(dpy,_window,tmp);
- cimg_unlock_display();
- delete[] tmp;
- return *this;
- }
- template<typename T>
- CImgDisplay& display(const CImg<T>& img) {
- if (!img)
- throw CImgArgumentException(_cimgdisplay_instance
- "display(): Empty specified image.",
- cimgdisplay_instance);
- if (is_empty()) return assign(img);
- return render(img).paint(false);
- }
- CImgDisplay& paint(const bool wait_expose=true) {
- if (is_empty()) return *this;
- cimg_lock_display();
- _paint(wait_expose);
- cimg_unlock_display();
- return *this;
- }
- template<typename T>
- CImgDisplay& render(const CImg<T>& img, const bool flag8=false) {
- if (!img)
- throw CImgArgumentException(_cimgdisplay_instance
- "render(): Empty specified image.",
- cimgdisplay_instance);
- if (is_empty()) return *this;
- if (img._depth!=1) return render(img.get_projections2d((img._width - 1)/2,(img._height - 1)/2,
- (img._depth - 1)/2));
- if (cimg::X11_attr().nb_bits==8 && (img._width!=_width || img._height!=_height))
- return render(img.get_resize(_width,_height,1,-100,1));
- if (cimg::X11_attr().nb_bits==8 && !flag8 && img._spectrum==3) {
- static const CImg<typename CImg<T>::ucharT> default_colormap = CImg<typename CImg<T>::ucharT>::default_LUT256();
- return render(img.get_index(default_colormap,1,false));
- }
- const T
- *data1 = img._data,
- *data2 = (img._spectrum>1)?img.data(0,0,0,1):data1,
- *data3 = (img._spectrum>2)?img.data(0,0,0,2):data1;
- if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3);
- cimg_lock_display();
- if (!_normalization || (_normalization==3 && cimg::type<T>::string()==cimg::type<unsigned char>::string())) {
- _min = _max = 0;
- switch (cimg::X11_attr().nb_bits) {
- case 8 : { // 256 colormap, no normalization
- _set_colormap(_colormap,img._spectrum);
- unsigned char
- *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:
- new unsigned char[(size_t)img._width*img._height],
- *ptrd = (unsigned char*)ndata;
- switch (img._spectrum) {
- case 1 :
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- (*ptrd++) = (unsigned char)*(data1++);
- break;
- case 2 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)*(data1++),
- G = (unsigned char)*(data2++);
- (*ptrd++) = (R&0xf0) | (G>>4);
- } break;
- default : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)*(data1++),
- G = (unsigned char)*(data2++),
- B = (unsigned char)*(data3++);
- (*ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6);
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height);
- delete[] ndata;
- }
- } break;
- case 16 : { // 16 bits colors, no normalization
- unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:
- new unsigned short[(size_t)img._width*img._height];
- unsigned char *ptrd = (unsigned char*)ndata;
- const unsigned int M = 248;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)*(data1++), G = val>>2;
- ptrd[0] = (val&M) | (G>>3);
- ptrd[1] = (G<<5) | (G>>1);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)*(data1++), G = val>>2;
- ptrd[0] = (G<<5) | (G>>1);
- ptrd[1] = (val&M) | (G>>3);
- ptrd+=2;
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)*(data2++)>>2;
- ptrd[0] = ((unsigned char)*(data1++)&M) | (G>>3);
- ptrd[1] = (G<<5);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)*(data2++)>>2;
- ptrd[0] = (G<<5);
- ptrd[1] = ((unsigned char)*(data1++)&M) | (G>>3);
- ptrd+=2;
- }
- break;
- default :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)*(data2++)>>2;
- ptrd[0] = ((unsigned char)*(data1++)&M) | (G>>3);
- ptrd[1] = (G<<5) | ((unsigned char)*(data3++)>>3);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)*(data2++)>>2;
- ptrd[0] = (G<<5) | ((unsigned char)*(data3++)>>3);
- ptrd[1] = ((unsigned char)*(data1++)&M) | (G>>3);
- ptrd+=2;
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height);
- delete[] ndata;
- }
- } break;
- default : { // 24 bits colors, no normalization
- unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:
- new unsigned int[(size_t)img._width*img._height];
- if (sizeof(int)==4) { // 32 bits int uses optimized version
- unsigned int *ptrd = ndata;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)*(data1++);
- *(ptrd++) = (val<<16) | (val<<8) | val;
- }
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)*(data1++);
- *(ptrd++) = (val<<16) | (val<<8) | val;
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8);
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) = ((unsigned char)*(data2++)<<16) | ((unsigned char)*(data1++)<<8);
- break;
- default :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) = ((unsigned char)*(data1++)<<16) | ((unsigned char)*(data2++)<<8) |
- (unsigned char)*(data3++);
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) = ((unsigned char)*(data3++)<<24) | ((unsigned char)*(data2++)<<16) |
- ((unsigned char)*(data1++)<<8);
- }
- } else {
- unsigned char *ptrd = (unsigned char*)ndata;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = (unsigned char)*(data1++);
- ptrd[2] = 0;
- ptrd[3] = 0;
- ptrd+=4;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = 0;
- ptrd[2] = (unsigned char)*(data1++);
- ptrd[3] = 0;
- ptrd+=4;
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order) cimg::swap(data1,data2);
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = (unsigned char)*(data2++);
- ptrd[2] = (unsigned char)*(data1++);
- ptrd[3] = 0;
- ptrd+=4;
- }
- break;
- default :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = (unsigned char)*(data1++);
- ptrd[2] = (unsigned char)*(data2++);
- ptrd[3] = (unsigned char)*(data3++);
- ptrd+=4;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = (unsigned char)*(data3++);
- ptrd[1] = (unsigned char)*(data2++);
- ptrd[2] = (unsigned char)*(data1++);
- ptrd[3] = 0;
- ptrd+=4;
- }
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height);
- delete[] ndata;
- }
- }
- }
- } else {
- if (_normalization==3) {
- if (sizeof(T)>1 && cimg::type<T>::string()!=cimg::type<bool>::string()) _min = (float)img.min_max(_max);
- else { _min = (float)cimg::type<T>::min(); _max = (float)cimg::type<T>::max(); }
- } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max);
- const float delta = _max - _min, mm = 255/(delta?delta:1.f);
- switch (cimg::X11_attr().nb_bits) {
- case 8 : { // 256 colormap, with normalization
- _set_colormap(_colormap,img._spectrum);
- unsigned char *const ndata = (img._width==_width && img._height==_height)?(unsigned char*)_data:
- new unsigned char[(size_t)img._width*img._height];
- unsigned char *ptrd = (unsigned char*)ndata;
- switch (img._spectrum) {
- case 1 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char R = (unsigned char)((*(data1++) - _min)*mm);
- *(ptrd++) = R;
- } break;
- case 2 : for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)((*(data1++) - _min)*mm),
- G = (unsigned char)((*(data2++) - _min)*mm);
- (*ptrd++) = (R&0xf0) | (G>>4);
- } break;
- default :
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)((*(data1++) - _min)*mm),
- G = (unsigned char)((*(data2++) - _min)*mm),
- B = (unsigned char)((*(data3++) - _min)*mm);
- *(ptrd++) = (R&0xe0) | ((G>>5)<<2) | (B>>6);
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned char*)_data,_width,_height);
- delete[] ndata;
- }
- } break;
- case 16 : { // 16 bits colors, with normalization
- unsigned short *const ndata = (img._width==_width && img._height==_height)?(unsigned short*)_data:
- new unsigned short[(size_t)img._width*img._height];
- unsigned char *ptrd = (unsigned char*)ndata;
- const unsigned int M = 248;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm), G = val>>2;
- ptrd[0] = (val&M) | (G>>3);
- ptrd[1] = (G<<5) | (val>>3);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm), G = val>>2;
- ptrd[0] = (G<<5) | (val>>3);
- ptrd[1] = (val&M) | (G>>3);
- ptrd+=2;
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
- ptrd[0] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
- ptrd[1] = (G<<5);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
- ptrd[0] = (G<<5);
- ptrd[1] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
- ptrd+=2;
- }
- break;
- default :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
- ptrd[0] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
- ptrd[1] = (G<<5) | ((unsigned char)((*(data3++) - _min)*mm)>>3);
- ptrd+=2;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char G = (unsigned char)((*(data2++) - _min)*mm)>>2;
- ptrd[0] = (G<<5) | ((unsigned char)((*(data3++) - _min)*mm)>>3);
- ptrd[1] = ((unsigned char)((*(data1++) - _min)*mm)&M) | (G>>3);
- ptrd+=2;
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned short*)_data,_width,_height);
- delete[] ndata;
- }
- } break;
- default : { // 24 bits colors, with normalization
- unsigned int *const ndata = (img._width==_width && img._height==_height)?(unsigned int*)_data:
- new unsigned int[(size_t)img._width*img._height];
- if (sizeof(int)==4) { // 32 bits int uses optimized version
- unsigned int *ptrd = ndata;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
- *(ptrd++) = (val<<16) | (val<<8) | val;
- }
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
- *(ptrd++) = (val<<24) | (val<<16) | (val<<8);
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) =
- ((unsigned char)((*(data1++) - _min)*mm)<<16) |
- ((unsigned char)((*(data2++) - _min)*mm)<<8);
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) =
- ((unsigned char)((*(data2++) - _min)*mm)<<16) |
- ((unsigned char)((*(data1++) - _min)*mm)<<8);
- break;
- default :
- if (cimg::X11_attr().byte_order==cimg::endianness())
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) =
- ((unsigned char)((*(data1++) - _min)*mm)<<16) |
- ((unsigned char)((*(data2++) - _min)*mm)<<8) |
- (unsigned char)((*(data3++) - _min)*mm);
- else
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy)
- *(ptrd++) =
- ((unsigned char)((*(data3++) - _min)*mm)<<24) |
- ((unsigned char)((*(data2++) - _min)*mm)<<16) |
- ((unsigned char)((*(data1++) - _min)*mm)<<8);
- }
- } else {
- unsigned char *ptrd = (unsigned char*)ndata;
- switch (img._spectrum) {
- case 1 :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
- ptrd[0] = 0;
- ptrd[1] = val;
- ptrd[2] = val;
- ptrd[3] = val;
- ptrd+=4;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
- ptrd[0] = val;
- ptrd[1] = val;
- ptrd[2] = val;
- ptrd[3] = 0;
- ptrd+=4;
- }
- break;
- case 2 :
- if (cimg::X11_attr().byte_order) cimg::swap(data1,data2);
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = (unsigned char)((*(data2++) - _min)*mm);
- ptrd[2] = (unsigned char)((*(data1++) - _min)*mm);
- ptrd[3] = 0;
- ptrd+=4;
- }
- break;
- default :
- if (cimg::X11_attr().byte_order)
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = 0;
- ptrd[1] = (unsigned char)((*(data1++) - _min)*mm);
- ptrd[2] = (unsigned char)((*(data2++) - _min)*mm);
- ptrd[3] = (unsigned char)((*(data3++) - _min)*mm);
- ptrd+=4;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ptrd[0] = (unsigned char)((*(data3++) - _min)*mm);
- ptrd[1] = (unsigned char)((*(data2++) - _min)*mm);
- ptrd[2] = (unsigned char)((*(data1++) - _min)*mm);
- ptrd[3] = 0;
- ptrd+=4;
- }
- }
- }
- if (ndata!=_data) {
- _render_resize(ndata,img._width,img._height,(unsigned int*)_data,_width,_height);
- delete[] ndata;
- }
- }
- }
- }
- cimg_unlock_display();
- return *this;
- }
- template<typename T>
- static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
- img.assign();
- Display *dpy = cimg::X11_attr().display;
- cimg_lock_display();
- if (!dpy) {
- dpy = XOpenDisplay(0);
- if (!dpy)
- throw CImgDisplayException("CImgDisplay::screenshot(): Failed to open X11 display.");
- }
- Window root = DefaultRootWindow(dpy);
- XWindowAttributes gwa;
- XGetWindowAttributes(dpy,root,&gwa);
- const int width = gwa.width, height = gwa.height;
- int _x0 = x0, _y0 = y0, _x1 = x1, _y1 = y1;
- if (_x0>_x1) cimg::swap(_x0,_x1);
- if (_y0>_y1) cimg::swap(_y0,_y1);
- XImage *image = 0;
- if (_x1>=0 && _x0<width && _y1>=0 && _y0<height) {
- _x0 = std::max(_x0,0);
- _y0 = std::max(_y0,0);
- _x1 = std::min(_x1,width - 1);
- _y1 = std::min(_y1,height - 1);
- image = XGetImage(dpy,root,_x0,_y0,_x1 - _x0 + 1,_y1 - _y0 + 1,AllPlanes,ZPixmap);
- if (image) {
- const unsigned long
- red_mask = image->red_mask,
- green_mask = image->green_mask,
- blue_mask = image->blue_mask;
- img.assign(image->width,image->height,1,3);
- T *pR = img.data(0,0,0,0), *pG = img.data(0,0,0,1), *pB = img.data(0,0,0,2);
- cimg_forXY(img,x,y) {
- const unsigned long pixel = XGetPixel(image,x,y);
- *(pR++) = (T)((pixel & red_mask)>>16);
- *(pG++) = (T)((pixel & green_mask)>>8);
- *(pB++) = (T)(pixel & blue_mask);
- }
- XDestroyImage(image);
- }
- }
- if (!cimg::X11_attr().display) XCloseDisplay(dpy);
- cimg_unlock_display();
- if (img.is_empty())
- throw CImgDisplayException("CImgDisplay::screenshot(): Failed to take screenshot "
- "with coordinates (%d,%d)-(%d,%d).",
- x0,y0,x1,y1);
- }
- template<typename T>
- const CImgDisplay& snapshot(CImg<T>& img) const {
- if (is_empty()) { img.assign(); return *this; }
- const unsigned char *ptrs = (unsigned char*)_data;
- img.assign(_width,_height,1,3);
- T
- *data1 = img.data(0,0,0,0),
- *data2 = img.data(0,0,0,1),
- *data3 = img.data(0,0,0,2);
- if (cimg::X11_attr().is_blue_first) cimg::swap(data1,data3);
- switch (cimg::X11_attr().nb_bits) {
- case 8 : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = *(ptrs++);
- *(data1++) = (T)(val&0xe0);
- *(data2++) = (T)((val&0x1c)<<3);
- *(data3++) = (T)(val<<6);
- }
- } break;
- case 16 : {
- if (cimg::X11_attr().byte_order) for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- val0 = ptrs[0],
- val1 = ptrs[1];
- ptrs+=2;
- *(data1++) = (T)(val0&0xf8);
- *(data2++) = (T)((val0<<5) | ((val1&0xe0)>>5));
- *(data3++) = (T)(val1<<3);
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned short
- val0 = ptrs[0],
- val1 = ptrs[1];
- ptrs+=2;
- *(data1++) = (T)(val1&0xf8);
- *(data2++) = (T)((val1<<5) | ((val0&0xe0)>>5));
- *(data3++) = (T)(val0<<3);
- }
- } break;
- default : {
- if (cimg::X11_attr().byte_order) for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- ++ptrs;
- *(data1++) = (T)ptrs[0];
- *(data2++) = (T)ptrs[1];
- *(data3++) = (T)ptrs[2];
- ptrs+=3;
- } else for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- *(data3++) = (T)ptrs[0];
- *(data2++) = (T)ptrs[1];
- *(data1++) = (T)ptrs[2];
- ptrs+=3;
- ++ptrs;
- }
- }
- }
- return *this;
- }
- // Windows-based implementation.
- //-------------------------------
- #elif cimg_display==2
- bool _is_mouse_tracked, _is_cursor_visible;
- HANDLE _thread, _is_created, _mutex;
- HWND _window, _background_window;
- CLIENTCREATESTRUCT _ccs;
- unsigned int *_data;
- DEVMODE _curr_mode;
- BITMAPINFO _bmi;
- HDC _hdc;
- static int screen_width() {
- DEVMODE mode;
- mode.dmSize = sizeof(DEVMODE);
- mode.dmDriverExtra = 0;
- EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode);
- return (int)mode.dmPelsWidth;
- }
- static int screen_height() {
- DEVMODE mode;
- mode.dmSize = sizeof(DEVMODE);
- mode.dmDriverExtra = 0;
- EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&mode);
- return (int)mode.dmPelsHeight;
- }
- static void wait_all() {
- WaitForSingleObject(cimg::Win32_attr().wait_event,INFINITE);
- }
- static LRESULT APIENTRY _handle_events(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) {
- #ifdef _WIN64
- CImgDisplay *const disp = (CImgDisplay*)GetWindowLongPtr(window,GWLP_USERDATA);
- #else
- CImgDisplay *const disp = (CImgDisplay*)GetWindowLong(window,GWL_USERDATA);
- #endif
- MSG st_msg;
- switch (msg) {
- case WM_CLOSE :
- disp->_mouse_x = disp->_mouse_y = -1;
- disp->_window_x = disp->_window_y = cimg::type<int>::min();
- disp->set_button().set_key(0).set_key(0,false)._is_closed = true;
- ReleaseMutex(disp->_mutex);
- ShowWindow(disp->_window,SW_HIDE);
- disp->_is_event = true;
- SetEvent(cimg::Win32_attr().wait_event);
- return 0;
- case WM_SIZE : {
- while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {}
- WaitForSingleObject(disp->_mutex,INFINITE);
- const unsigned int nw = LOWORD(lParam),nh = HIWORD(lParam);
- if (nw && nh && (nw!=disp->_width || nh!=disp->_height)) {
- disp->_window_width = nw;
- disp->_window_height = nh;
- disp->_mouse_x = disp->_mouse_y = -1;
- disp->_is_resized = disp->_is_event = true;
- SetEvent(cimg::Win32_attr().wait_event);
- }
- ReleaseMutex(disp->_mutex);
- } break;
- case WM_MOVE : {
- while (PeekMessage(&st_msg,window,WM_SIZE,WM_SIZE,PM_REMOVE)) {}
- WaitForSingleObject(disp->_mutex,INFINITE);
- const int nx = (int)(short)(LOWORD(lParam)), ny = (int)(short)(HIWORD(lParam));
- if (nx!=disp->_window_x || ny!=disp->_window_y) {
- disp->_window_x = nx;
- disp->_window_y = ny;
- disp->_is_moved = disp->_is_event = true;
- SetEvent(cimg::Win32_attr().wait_event);
- }
- ReleaseMutex(disp->_mutex);
- } break;
- case WM_PAINT :
- disp->paint();
- cimg_lock_display();
- if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE_WIN)>=0);
- cimg_unlock_display();
- break;
- case WM_ERASEBKGND :
- // return 0;
- break;
- case WM_KEYDOWN :
- disp->set_key((unsigned int)wParam);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_KEYUP :
- disp->set_key((unsigned int)wParam,false);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_MOUSEMOVE : {
- while (PeekMessage(&st_msg,window,WM_MOUSEMOVE,WM_MOUSEMOVE,PM_REMOVE)) {}
- disp->_mouse_x = LOWORD(lParam);
- disp->_mouse_y = HIWORD(lParam);
- #if (_WIN32_WINNT>=0x0400) && !defined(NOTRACKMOUSEEVENT)
- if (!disp->_is_mouse_tracked) {
- TRACKMOUSEEVENT tme;
- tme.cbSize = sizeof(TRACKMOUSEEVENT);
- tme.dwFlags = TME_LEAVE;
- tme.hwndTrack = disp->_window;
- if (TrackMouseEvent(&tme)) disp->_is_mouse_tracked = true;
- }
- #endif
- if (disp->_mouse_x<0 || disp->_mouse_y<0 || disp->_mouse_x>=disp->width() || disp->_mouse_y>=disp->height())
- disp->_mouse_x = disp->_mouse_y = -1;
- disp->_is_event = true;
- SetEvent(cimg::Win32_attr().wait_event);
- cimg_lock_display();
- if (disp->_is_cursor_visible) while (ShowCursor(TRUE)<0); else while (ShowCursor(FALSE_WIN)>=0);
- cimg_unlock_display();
- } break;
- case WM_MOUSELEAVE : {
- disp->_mouse_x = disp->_mouse_y = -1;
- disp->_is_mouse_tracked = false;
- cimg_lock_display();
- while (ShowCursor(TRUE)<0) {}
- cimg_unlock_display();
- } break;
- case WM_LBUTTONDOWN :
- disp->set_button(1);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_RBUTTONDOWN :
- disp->set_button(2);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_MBUTTONDOWN :
- disp->set_button(3);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_LBUTTONUP :
- disp->set_button(1,false);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_RBUTTONUP :
- disp->set_button(2,false);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case WM_MBUTTONUP :
- disp->set_button(3,false);
- SetEvent(cimg::Win32_attr().wait_event);
- break;
- case 0x020A : // WM_MOUSEWHEEL:
- disp->set_wheel((int)((short)HIWORD(wParam))/120);
- SetEvent(cimg::Win32_attr().wait_event);
- }
- return DefWindowProc(window,msg,wParam,lParam);
- }
- static DWORD WINAPI _events_thread(void* arg) {
- CImgDisplay *const disp = (CImgDisplay*)(((void**)arg)[0]);
- const char *const title = (const char*)(((void**)arg)[1]);
- MSG msg;
- delete[] (void**)arg;
- disp->_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- disp->_bmi.bmiHeader.biWidth = disp->width();
- disp->_bmi.bmiHeader.biHeight = -disp->height();
- disp->_bmi.bmiHeader.biPlanes = 1;
- disp->_bmi.bmiHeader.biBitCount = 32;
- disp->_bmi.bmiHeader.biCompression = BI_RGB;
- disp->_bmi.bmiHeader.biSizeImage = 0;
- disp->_bmi.bmiHeader.biXPelsPerMeter = 1;
- disp->_bmi.bmiHeader.biYPelsPerMeter = 1;
- disp->_bmi.bmiHeader.biClrUsed = 0;
- disp->_bmi.bmiHeader.biClrImportant = 0;
- disp->_data = new unsigned int[(size_t)disp->_width*disp->_height];
- if (!disp->_is_fullscreen) { // Normal window
- RECT rect;
- rect.left = rect.top = 0; rect.right = (LONG)disp->_width - 1; rect.bottom = (LONG)disp->_height - 1;
- AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
- const int
- border1 = (int)((rect.right - rect.left + 1 - disp->_width)/2),
- border2 = (int)(rect.bottom - rect.top + 1 - disp->_height - border1),
- ww = disp->width() + 2*border1,
- wh = disp->height() + border1 + border2,
- sw = CImgDisplay::screen_width(),
- sh = CImgDisplay::screen_height();
- int
- wx = (int)cimg::round(cimg::rand(0,sw - ww -1)),
- wy = (int)cimg::round(cimg::rand(64,sh - wh - 65));
- if (wx + ww>=sw) wx = sw - ww;
- if (wy + wh>=sh) wy = sh - wh;
- if (wx<0) wx = 0;
- if (wy<0) wy = 0;
- disp->_window = CreateWindowA("MDICLIENT",title?title:" ",
- (DWORD)(WS_OVERLAPPEDWINDOW | (disp->_is_closed?0:WS_VISIBLE)),
- wx,wy,ww,wh,0,0,0,&(disp->_ccs));
- if (!disp->_is_closed) {
- GetWindowRect(disp->_window,&rect);
- disp->_window_x = rect.left;
- disp->_window_y = rect.top;
- } else disp->_window_x = disp->_window_y = cimg::type<int>::min();
- } else { // Fullscreen window
- const unsigned int
- sx = (unsigned int)screen_width(),
- sy = (unsigned int)screen_height();
- disp->_window = CreateWindowA("MDICLIENT",title?title:" ",
- (DWORD)(WS_POPUP | (disp->_is_closed?0:WS_VISIBLE)),
- (int)(sx - disp->_width)/2,
- (int)(sy - disp->_height)/2,
- disp->width(),disp->height(),0,0,0,&(disp->_ccs));
- disp->_window_x = disp->_window_y = 0;
- }
- SetForegroundWindow(disp->_window);
- disp->_hdc = GetDC(disp->_window);
- disp->_window_width = disp->_width;
- disp->_window_height = disp->_height;
- disp->flush();
- #ifdef _WIN64
- SetWindowLongPtr(disp->_window,GWLP_USERDATA,(LONG_PTR)disp);
- SetWindowLongPtr(disp->_window,GWLP_WNDPROC,(LONG_PTR)_handle_events);
- #else
- SetWindowLong(disp->_window,GWL_USERDATA,(LONG)disp);
- SetWindowLong(disp->_window,GWL_WNDPROC,(LONG)_handle_events);
- #endif
- SetEvent(disp->_is_created);
- while (GetMessage(&msg,0,0,0)) DispatchMessage(&msg);
- return 0;
- }
- CImgDisplay& _update_window_pos() {
- if (_is_closed) _window_x = _window_y = cimg::type<int>::min();
- else {
- RECT rect;
- rect.left = rect.top = 0; rect.right = (LONG)_width - 1; rect.bottom = (LONG)_height - 1;
- AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
- GetWindowRect(_window,&rect);
- _window_x = rect.left;
- _window_y = rect.top;
- }
- return *this;
- }
- void _init_fullscreen() {
- _background_window = 0;
- if (!_is_fullscreen || _is_closed) _curr_mode.dmSize = 0;
- else {
- /* DEVMODE mode;
- unsigned int imode = 0, ibest = 0, bestbpp = 0, bw = ~0U, bh = ~0U;
- for (mode.dmSize = sizeof(DEVMODE), mode.dmDriverExtra = 0; EnumDisplaySettings(0,imode,&mode); ++imode) {
- const unsigned int nw = mode.dmPelsWidth, nh = mode.dmPelsHeight;
- if (nw>=_width && nh>=_height && mode.dmBitsPerPel>=bestbpp && nw<=bw && nh<=bh) {
- bestbpp = mode.dmBitsPerPel;
- ibest = imode;
- bw = nw; bh = nh;
- }
- }
- if (bestbpp) {
- _curr_mode.dmSize = sizeof(DEVMODE); _curr_mode.dmDriverExtra = 0;
- EnumDisplaySettings(0,ENUM_CURRENT_SETTINGS,&_curr_mode);
- EnumDisplaySettings(0,ibest,&mode);
- ChangeDisplaySettings(&mode,0);
- } else _curr_mode.dmSize = 0;
- */
- _curr_mode.dmSize = 0;
- const unsigned int
- sx = (unsigned int)screen_width(),
- sy = (unsigned int)screen_height();
- if (sx!=_width || sy!=_height) {
- CLIENTCREATESTRUCT background_ccs = { 0,0 };
- _background_window = CreateWindowA("MDICLIENT","",WS_POPUP | WS_VISIBLE,
- 0,0,(int)sx,(int)sy,0,0,0,&background_ccs);
- SetForegroundWindow(_background_window);
- }
- }
- }
- void _desinit_fullscreen() {
- if (!_is_fullscreen) return;
- if (_background_window) DestroyWindow(_background_window);
- _background_window = 0;
- if (_curr_mode.dmSize) ChangeDisplaySettings(&_curr_mode,0);
- _is_fullscreen = false;
- }
- CImgDisplay& _assign(const unsigned int dimw, const unsigned int dimh, const char *const ptitle=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- // Allocate space for window title
- const char *const nptitle = ptitle?ptitle:"";
- const unsigned int s = (unsigned int)std::strlen(nptitle) + 1;
- char *const tmp_title = s?new char[s]:0;
- if (s) std::memcpy(tmp_title,nptitle,s*sizeof(char));
- // Destroy previous window if existing
- if (!is_empty()) assign();
- // Set display variables
- _width = std::min(dimw,(unsigned int)screen_width());
- _height = std::min(dimh,(unsigned int)screen_height());
- _normalization = normalization_type<4?normalization_type:3;
- _is_fullscreen = fullscreen_flag;
- _window_x = _window_y = cimg::type<int>::min();
- _is_closed = closed_flag;
- _is_cursor_visible = true;
- _is_mouse_tracked = false;
- _title = tmp_title;
- flush();
- if (_is_fullscreen) _init_fullscreen();
- // Create event thread
- void *const arg = (void*)(new void*[2]);
- ((void**)arg)[0] = (void*)this;
- ((void**)arg)[1] = (void*)_title;
- _mutex = CreateMutex(0,FALSE_WIN,0);
- _is_created = CreateEvent(0,FALSE_WIN,FALSE_WIN,0);
- _thread = CreateThread(0,0,_events_thread,arg,0,0);
- WaitForSingleObject(_is_created,INFINITE);
- return *this;
- }
- CImgDisplay& assign() {
- if (is_empty()) return flush();
- DestroyWindow(_window);
- TerminateThread(_thread,0);
- delete[] _data;
- delete[] _title;
- _data = 0;
- _title = 0;
- if (_is_fullscreen) _desinit_fullscreen();
- _width = _height = _normalization = _window_width = _window_height = 0;
- _window_x = _window_y = cimg::type<int>::min();
- _is_fullscreen = false;
- _is_closed = true;
- _min = _max = 0;
- _title = 0;
- flush();
- return *this;
- }
- CImgDisplay& assign(const unsigned int dimw, const unsigned int dimh, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!dimw || !dimh) return assign();
- _assign(dimw,dimh,title,normalization_type,fullscreen_flag,closed_flag);
- _min = _max = 0;
- std::memset(_data,0,sizeof(unsigned int)*_width*_height);
- return paint();
- }
- template<typename T>
- CImgDisplay& assign(const CImg<T>& img, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!img) return assign();
- CImg<T> tmp;
- const CImg<T>& nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
- (img._height - 1)/2,
- (img._depth - 1)/2));
- _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
- if (_normalization==2) _min = (float)nimg.min_max(_max);
- return display(nimg);
- }
- template<typename T>
- CImgDisplay& assign(const CImgList<T>& list, const char *const title=0,
- const unsigned int normalization_type=3,
- const bool fullscreen_flag=false, const bool closed_flag=false) {
- if (!list) return assign();
- CImg<T> tmp;
- const CImg<T> img = list>'x', &nimg = (img._depth==1)?img:(tmp=img.get_projections2d((img._width - 1)/2,
- (img._height - 1)/2,
- (img._depth - 1)/2));
- _assign(nimg._width,nimg._height,title,normalization_type,fullscreen_flag,closed_flag);
- if (_normalization==2) _min = (float)nimg.min_max(_max);
- return display(nimg);
- }
- CImgDisplay& assign(const CImgDisplay& disp) {
- if (!disp) return assign();
- _assign(disp._width,disp._height,disp._title,disp._normalization,disp._is_fullscreen,disp._is_closed);
- std::memcpy(_data,disp._data,sizeof(unsigned int)*_width*_height);
- return paint();
- }
- CImgDisplay& resize(const int nwidth, const int nheight, const bool force_redraw=true) {
- if (!nwidth || !nheight || (is_empty() && (nwidth<0 || nheight<0))) return assign();
- if (is_empty()) return assign((unsigned int)nwidth,(unsigned int)nheight);
- const unsigned int
- tmpdimx = (nwidth>0)?nwidth:(-nwidth*_width/100),
- tmpdimy = (nheight>0)?nheight:(-nheight*_height/100),
- dimx = tmpdimx?tmpdimx:1,
- dimy = tmpdimy?tmpdimy:1;
- if (_width!=dimx || _height!=dimy || _window_width!=dimx || _window_height!=dimy) {
- if (_window_width!=dimx || _window_height!=dimy) {
- RECT rect; rect.left = rect.top = 0; rect.right = (LONG)dimx - 1; rect.bottom = (LONG)dimy - 1;
- AdjustWindowRect(&rect,WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,false);
- const int cwidth = rect.right - rect.left + 1, cheight = rect.bottom - rect.top + 1;
- SetWindowPos(_window,0,0,0,cwidth,cheight,SWP_NOMOVE | SWP_NOZORDER | SWP_NOCOPYBITS);
- }
- if (_width!=dimx || _height!=dimy) {
- unsigned int *const ndata = new unsigned int[dimx*dimy];
- if (force_redraw) _render_resize(_data,_width,_height,ndata,dimx,dimy);
- else std::memset(ndata,0x80,sizeof(unsigned int)*dimx*dimy);
- delete[] _data;
- _data = ndata;
- _bmi.bmiHeader.biWidth = (LONG)dimx;
- _bmi.bmiHeader.biHeight = -(int)dimy;
- _width = dimx;
- _height = dimy;
- }
- _window_width = dimx; _window_height = dimy;
- show();
- }
- _is_resized = false;
- if (_is_fullscreen) move((screen_width() - width())/2,(screen_height() - height())/2);
- if (force_redraw) return paint();
- return *this;
- }
- CImgDisplay& toggle_fullscreen(const bool force_redraw=true) {
- if (is_empty()) return *this;
- if (force_redraw) {
- const cimg_ulong buf_size = (cimg_ulong)_width*_height*4;
- void *odata = std::malloc(buf_size);
- if (odata) {
- std::memcpy(odata,_data,buf_size);
- assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
- std::memcpy(_data,odata,buf_size);
- std::free(odata);
- }
- return paint();
- }
- return assign(_width,_height,_title,_normalization,!_is_fullscreen,false);
- }
- CImgDisplay& show() {
- if (is_empty() || !_is_closed) return *this;
- _is_closed = false;
- if (_is_fullscreen) _init_fullscreen();
- ShowWindow(_window,SW_SHOW);
- _update_window_pos();
- return paint();
- }
- CImgDisplay& close() {
- if (is_empty() || _is_closed) return *this;
- _is_closed = true;
- if (_is_fullscreen) _desinit_fullscreen();
- ShowWindow(_window,SW_HIDE);
- _window_x = _window_y = cimg::type<int>::min();
- return *this;
- }
- CImgDisplay& move(const int posx, const int posy) {
- if (is_empty()) return *this;
- if (_window_x!=posx || _window_y!=posy) {
- SetWindowPos(_window,0,posx,posy,0,0,SWP_NOSIZE | SWP_NOZORDER);
- _window_x = posx;
- _window_y = posy;
- }
- show();
- _is_moved = false;
- return *this;
- }
- CImgDisplay& show_mouse() {
- if (is_empty()) return *this;
- _is_cursor_visible = true;
- return *this;
- }
- CImgDisplay& hide_mouse() {
- if (is_empty()) return *this;
- _is_cursor_visible = false;
- return *this;
- }
- CImgDisplay& set_mouse(const int posx, const int posy) {
- if (is_empty() || _is_closed || posx<0 || posy<0) return *this;
- if (!_is_closed) {
- _update_window_pos();
- const int res = (int)SetCursorPos(_window_x + posx,_window_y + posy);
- if (res) { _mouse_x = posx; _mouse_y = posy; }
- }
- return *this;
- }
- CImgDisplay& set_title(const char *const format, ...) {
- if (is_empty()) return *this;
- char *const tmp = new char[1024];
- va_list ap;
- va_start(ap, format);
- cimg_vsnprintf(tmp,1024,format,ap);
- va_end(ap);
- if (!std::strcmp(_title,tmp)) { delete[] tmp; return *this; }
- delete[] _title;
- const unsigned int s = (unsigned int)std::strlen(tmp) + 1;
- _title = new char[s];
- std::memcpy(_title,tmp,s*sizeof(char));
- SetWindowTextA(_window, tmp);
- delete[] tmp;
- return *this;
- }
- template<typename T>
- CImgDisplay& display(const CImg<T>& img) {
- if (!img)
- throw CImgArgumentException(_cimgdisplay_instance
- "display(): Empty specified image.",
- cimgdisplay_instance);
- if (is_empty()) return assign(img);
- return render(img).paint();
- }
- CImgDisplay& paint() {
- if (_is_closed) return *this;
- WaitForSingleObject(_mutex,INFINITE);
- SetDIBitsToDevice(_hdc,0,0,_width,_height,0,0,0,_height,_data,&_bmi,DIB_RGB_COLORS);
- ReleaseMutex(_mutex);
- return *this;
- }
- template<typename T>
- CImgDisplay& render(const CImg<T>& img) {
- if (!img)
- throw CImgArgumentException(_cimgdisplay_instance
- "render(): Empty specified image.",
- cimgdisplay_instance);
- if (is_empty()) return *this;
- if (img._depth!=1) return render(img.get_projections2d((img._width - 1)/2,(img._height - 1)/2,
- (img._depth - 1)/2));
- const T
- *data1 = img._data,
- *data2 = (img._spectrum>=2)?img.data(0,0,0,1):data1,
- *data3 = (img._spectrum>=3)?img.data(0,0,0,2):data1;
- WaitForSingleObject(_mutex,INFINITE);
- unsigned int
- *const ndata = (img._width==_width && img._height==_height)?_data:
- new unsigned int[(size_t)img._width*img._height],
- *ptrd = ndata;
- if (!_normalization || (_normalization==3 && cimg::type<T>::string()==cimg::type<unsigned char>::string())) {
- _min = _max = 0;
- switch (img._spectrum) {
- case 1 : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)*(data1++);
- *(ptrd++) = (unsigned int)((val<<16) | (val<<8) | val);
- }
- } break;
- case 2 : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)*(data1++),
- G = (unsigned char)*(data2++);
- *(ptrd++) = (unsigned int)((R<<16) | (G<<8));
- }
- } break;
- default : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)*(data1++),
- G = (unsigned char)*(data2++),
- B = (unsigned char)*(data3++);
- *(ptrd++) = (unsigned int)((R<<16) | (G<<8) | B);
- }
- }
- }
- } else {
- if (_normalization==3) {
- if (cimg::type<T>::is_float()) _min = (float)img.min_max(_max);
- else {
- _min = (float)cimg::type<T>::min();
- _max = (float)cimg::type<T>::max();
- }
- } else if ((_min>_max) || _normalization==1) _min = (float)img.min_max(_max);
- const float delta = _max - _min, mm = 255/(delta?delta:1.f);
- switch (img._spectrum) {
- case 1 : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char val = (unsigned char)((*(data1++) - _min)*mm);
- *(ptrd++) = (unsigned int)((val<<16) | (val<<8) | val);
- }
- } break;
- case 2 : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)((*(data1++) - _min)*mm),
- G = (unsigned char)((*(data2++) - _min)*mm);
- *(ptrd++) = (unsigned int)((R<<16) | (G<<8));
- }
- } break;
- default : {
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned char
- R = (unsigned char)((*(data1++) - _min)*mm),
- G = (unsigned char)((*(data2++) - _min)*mm),
- B = (unsigned char)((*(data3++) - _min)*mm);
- *(ptrd++) = (unsigned int)((R<<16) | (G<<8) | B);
- }
- }
- }
- }
- if (ndata!=_data) { _render_resize(ndata,img._width,img._height,_data,_width,_height); delete[] ndata; }
- ReleaseMutex(_mutex);
- return *this;
- }
- template<typename T>
- static void screenshot(const int x0, const int y0, const int x1, const int y1, CImg<T>& img) {
- img.assign();
- HDC hScreen = GetDC(GetDesktopWindow());
- if (hScreen) {
- const int
- width = GetDeviceCaps(hScreen,HORZRES),
- height = GetDeviceCaps(hScreen,VERTRES);
- int _x0 = x0, _y0 = y0, _x1 = x1, _y1 = y1;
- if (_x0>_x1) cimg::swap(_x0,_x1);
- if (_y0>_y1) cimg::swap(_y0,_y1);
- if (_x1>=0 && _x0<width && _y1>=0 && _y0<height) {
- _x0 = std::max(_x0,0);
- _y0 = std::max(_y0,0);
- _x1 = std::min(_x1,width - 1);
- _y1 = std::min(_y1,height - 1);
- const int bw = _x1 - _x0 + 1, bh = _y1 - _y0 + 1;
- HDC hdcMem = CreateCompatibleDC(hScreen);
- if (hdcMem) {
- HBITMAP hBitmap = CreateCompatibleBitmap(hScreen,bw,bh);
- if (hBitmap) {
- HGDIOBJ hOld = SelectObject(hdcMem,hBitmap);
- if (hOld && BitBlt(hdcMem,0,0,bw,bh,hScreen,_x0,_y0,SRCCOPY) && SelectObject(hdcMem,hOld)) {
- BITMAPINFOHEADER bmi;
- bmi.biSize = sizeof(BITMAPINFOHEADER);
- bmi.biWidth = bw;
- bmi.biHeight = -bh;
- bmi.biPlanes = 1;
- bmi.biBitCount = 32;
- bmi.biCompression = BI_RGB;
- bmi.biSizeImage = 0;
- bmi.biXPelsPerMeter = bmi.biYPelsPerMeter = 0;
- bmi.biClrUsed = bmi.biClrImportant = 0;
- unsigned char *buf = new unsigned char[4*bw*bh];
- if (GetDIBits(hdcMem,hBitmap,0,bh,buf,(BITMAPINFO*)&bmi,DIB_RGB_COLORS)) {
- img.assign(bw,bh,1,3);
- const unsigned char *ptrs = buf;
- T *pR = img.data(0,0,0,0), *pG = img.data(0,0,0,1), *pB = img.data(0,0,0,2);
- cimg_forXY(img,x,y) {
- *(pR++) = (T)ptrs[2];
- *(pG++) = (T)ptrs[1];
- *(pB++) = (T)ptrs[0];
- ptrs+=4;
- }
- }
- delete[] buf;
- }
- DeleteObject(hBitmap);
- }
- DeleteDC(hdcMem);
- }
- }
- ReleaseDC(GetDesktopWindow(),hScreen);
- }
- if (img.is_empty())
- throw CImgDisplayException("CImgDisplay::screenshot(): Failed to take screenshot "
- "with coordinates (%d,%d)-(%d,%d).",
- x0,y0,x1,y1);
- }
- template<typename T>
- const CImgDisplay& snapshot(CImg<T>& img) const {
- if (is_empty()) { img.assign(); return *this; }
- const unsigned int *ptrs = _data;
- img.assign(_width,_height,1,3);
- T
- *data1 = img.data(0,0,0,0),
- *data2 = img.data(0,0,0,1),
- *data3 = img.data(0,0,0,2);
- for (cimg_ulong xy = (cimg_ulong)img._width*img._height; xy>0; --xy) {
- const unsigned int val = *(ptrs++);
- *(data1++) = (T)(unsigned char)(val>>16);
- *(data2++) = (T)(unsigned char)((val>>8)&0xFF);
- *(data3++) = (T)(unsigned char)(val&0xFF);
- }
- return *this;
- }
- #endif
- //@}
- }; // struct CImgDisplay { ...
- /*
- #--------------------------------------
- #
- #
- #
- # Definition of the CImg<T> structure
- #
- #
- #
- #--------------------------------------
- */
- //! Class representing an image (up to 4 dimensions wide), each pixel being of type \c T.
- /**
- This is the main class of the %CImg Library. It declares and constructs
- an image, allows access to its pixel values, and is able to perform various image operations.
- \par Image representation
- A %CImg image is defined as an instance of the container \c CImg<T>, which contains a regular grid of pixels,
- each pixel value being of type \c T. The image grid can have up to 4 dimensions: width, height, depth
- and number of channels.
- Usually, the three first dimensions are used to describe spatial coordinates <tt>(x,y,z)</tt>,
- while the number of channels is rather used as a vector-valued dimension
- (it may describe the R,G,B color channels for instance).
- If you need a fifth dimension, you can use image lists \c CImgList<T> rather than simple images \c CImg<T>.
- Thus, the \c CImg<T> class is able to represent volumetric images of vector-valued pixels,
- as well as images with less dimensions (1D scalar signal, 2D color images, ...).
- Most member functions of the class CImg<\c T> are designed to handle this maximum case of (3+1) dimensions.
- Concerning the pixel value type \c T:
- fully supported template types are the basic C++ types: <tt>unsigned char, char, short, unsigned int, int,
- unsigned long, long, float, double, ... </tt>.
- Typically, fast image display can be done using <tt>CImg<unsigned char></tt> images,
- while complex image processing algorithms may be rather coded using <tt>CImg<float></tt> or <tt>CImg<double></tt>
- images that have floating-point pixel values. The default value for the template T is \c float.
- Using your own template types may be possible. However, you will certainly have to define the complete set
- of arithmetic and logical operators for your class.
- \par Image structure
- The \c CImg<T> structure contains \e six fields:
- - \c _width defines the number of \a columns of the image (size along the X-axis).
- - \c _height defines the number of \a rows of the image (size along the Y-axis).
- - \c _depth defines the number of \a slices of the image (size along the Z-axis).
- - \c _spectrum defines the number of \a channels of the image (size along the C-axis).
- - \c _data defines a \a pointer to the \a pixel \a data (of type \c T).
- - \c _is_shared is a boolean that tells if the memory buffer \c data is shared with
- another image.
- You can access these fields publicly although it is recommended to use the dedicated functions
- width(), height(), depth(), spectrum() and ptr() to do so.
- Image dimensions are not limited to a specific range (as long as you got enough available memory).
- A value of \e 1 usually means that the corresponding dimension is \a flat.
- If one of the dimensions is \e 0, or if the data pointer is null, the image is considered as \e empty.
- Empty images should not contain any pixel data and thus, will not be processed by CImg member functions
- (a CImgInstanceException will be thrown instead).
- Pixel data are stored in memory, in a non interlaced mode (See \ref cimg_storage).
- \par Image declaration and construction
- Declaring an image can be done by using one of the several available constructors.
- Here is a list of the most used:
- - Construct images from arbitrary dimensions:
- - <tt>CImg<char> img;</tt> declares an empty image.
- - <tt>CImg<unsigned char> img(128,128);</tt> declares a 128x128 greyscale image with
- \c unsigned \c char pixel values.
- - <tt>CImg<double> img(3,3);</tt> declares a 3x3 matrix with \c double coefficients.
- - <tt>CImg<unsigned char> img(256,256,1,3);</tt> declares a 256x256x1x3 (color) image
- (colors are stored as an image with three channels).
- - <tt>CImg<double> img(128,128,128);</tt> declares a 128x128x128 volumetric and greyscale image
- (with \c double pixel values).
- - <tt>CImg<> img(128,128,128,3);</tt> declares a 128x128x128 volumetric color image
- (with \c float pixels, which is the default value of the template parameter \c T).
- - \b Note: images pixels are <b>not automatically initialized to 0</b>. You may use the function \c fill() to
- do it, or use the specific constructor taking 5 parameters like this:
- <tt>CImg<> img(128,128,128,3,0);</tt> declares a 128x128x128 volumetric color image with all pixel values to 0.
- - Construct images from filenames:
- - <tt>CImg<unsigned char> img("image.jpg");</tt> reads a JPEG color image from the file "image.jpg".
- - <tt>CImg<float> img("analyze.hdr");</tt> reads a volumetric image (ANALYZE7.5 format) from the
- file "analyze.hdr".
- - \b Note: You need to install <a href="http://www.imagemagick.org">ImageMagick</a>
- to be able to read common compressed image formats (JPG,PNG, ...) (See \ref cimg_files_io).
- - Construct images from C-style arrays:
- - <tt>CImg<int> img(data_buffer,256,256);</tt> constructs a 256x256 greyscale image from a \c int* buffer
- \c data_buffer (of size 256x256=65536).
- - <tt>CImg<unsigned char> img(data_buffer,256,256,1,3);</tt> constructs a 256x256 color image
- from a \c unsigned \c char* buffer \c data_buffer (where R,G,B channels follow each others).
- The complete list of constructors can be found <a href="#constructors">here</a>.
- \par Most useful functions
- The \c CImg<T> class contains a lot of functions that operates on images.
- Some of the most useful are:
- - operator()(): Read or write pixel values.
- - display(): displays the image in a new window.
- **/
- template<typename T>
- struct CImg {
- unsigned int _width, _height, _depth, _spectrum;
- bool _is_shared;
- T *_data;
- //! Simple iterator type, to loop through each pixel value of an image instance.
- /**
- \note
- - The \c CImg<T>::iterator type is defined to be a <tt>T*</tt>.
- - You will seldom have to use iterators in %CImg, most classical operations
- being achieved (often in a faster way) using methods of \c CImg<T>.
- \par Example
- \code
- CImg<float> img("reference.jpg"); // Load image from file
- // Set all pixels to '0', with a CImg iterator.
- for (CImg<float>::iterator it = img.begin(), it<img.end(); ++it) *it = 0;
- img.fill(0); // Do the same with a built-in method
- \endcode
- **/
- typedef T* iterator;
- //! Simple const iterator type, to loop through each pixel value of a \c const image instance.
- /**
- \note
- - The \c CImg<T>::const_iterator type is defined to be a \c const \c T*.
- - You will seldom have to use iterators in %CImg, most classical operations
- being achieved (often in a faster way) using methods of \c CImg<T>.
- \par Example
- \code
- const CImg<float> img("reference.jpg"); // Load image from file
- float sum = 0;
- // Compute sum of all pixel values, with a CImg iterator.
- for (CImg<float>::iterator it = img.begin(), it<img.end(); ++it) sum+=*it;
- const float sum2 = img.sum(); // Do the same with a built-in method
- \endcode
- **/
- typedef const T* const_iterator;
- //! Pixel value type.
- /**
- Refer to the type of the pixel values of an image instance.
- \note
- - The \c CImg<T>::value_type type of a \c CImg<T> is defined to be a \c T.
- - \c CImg<T>::value_type is actually not used in %CImg methods. It has been mainly defined for
- compatibility with STL naming conventions.
- **/
- typedef T value_type;
- // Define common types related to template type T.
- typedef typename cimg::superset<T,bool>::type Tbool;
- typedef typename cimg::superset<T,unsigned char>::type Tuchar;
- typedef typename cimg::superset<T,char>::type Tchar;
- typedef typename cimg::superset<T,unsigned short>::type Tushort;
- typedef typename cimg::superset<T,short>::type Tshort;
- typedef typename cimg::superset<T,unsigned int>::type Tuint;
- typedef typename cimg::superset<T,int>::type Tint;
- typedef typename cimg::superset<T,cimg_ulong>::type Tulong;
- typedef typename cimg::superset<T,cimg_long>::type Tlong;
- typedef typename cimg::superset<T,float>::type Tfloat;
- typedef typename cimg::superset<T,double>::type Tdouble;
- typedef typename cimg::last<T,bool>::type boolT;
- typedef typename cimg::last<T,unsigned char>::type ucharT;
- typedef typename cimg::last<T,char>::type charT;
- typedef typename cimg::last<T,unsigned short>::type ushortT;
- typedef typename cimg::last<T,short>::type shortT;
- typedef typename cimg::last<T,unsigned int>::type uintT;
- typedef typename cimg::last<T,int>::type intT;
- typedef typename cimg::last<T,cimg_ulong>::type ulongT;
- typedef typename cimg::last<T,cimg_long>::type longT;
- typedef typename cimg::last<T,cimg_uint64>::type uint64T;
- typedef typename cimg::last<T,cimg_int64>::type int64T;
- typedef typename cimg::last<T,float>::type floatT;
- typedef typename cimg::last<T,double>::type doubleT;
- // Return 'dx*dy*dz*dc' as a 'size_t' and check no overflow occurs.
- static size_t safe_size(const unsigned int dx, const unsigned int dy,
- const unsigned int dz, const unsigned int dc) {
- if (!(dx && dy && dz && dc)) return 0;
- size_t siz = (size_t)dx, osiz = siz;
- if ((dy==1 || (siz*=dy)>osiz) &&
- ((osiz = siz), dz==1 || (siz*=dz)>osiz) &&
- ((osiz = siz), dc==1 || (siz*=dc)>osiz) &&
- ((osiz = siz), sizeof(T)==1 || (siz*sizeof(T))>osiz)) return siz;
- throw CImgArgumentException("CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) overflows 'size_t'.",
- pixel_type(),dx,dy,dz,dc);
- }
- //@}
- //---------------------------
- //
- //! \name Plugins
- //@{
- //---------------------------
- #ifdef cimg_plugin
- #include cimg_plugin
- #endif
- #ifdef cimg_plugin1
- #include cimg_plugin1
- #endif
- #ifdef cimg_plugin2
- #include cimg_plugin2
- #endif
- #ifdef cimg_plugin3
- #include cimg_plugin3
- #endif
- #ifdef cimg_plugin4
- #include cimg_plugin4
- #endif
- #ifdef cimg_plugin5
- #include cimg_plugin5
- #endif
- #ifdef cimg_plugin6
- #include cimg_plugin6
- #endif
- #ifdef cimg_plugin7
- #include cimg_plugin7
- #endif
- #ifdef cimg_plugin8
- #include cimg_plugin8
- #endif
- //@}
- //---------------------------------------------------------
- //
- //! \name Constructors / Destructor / Instance Management
- //@{
- //---------------------------------------------------------
- //! Destroy image.
- /**
- \note
- - The pixel buffer data() is deallocated if necessary, e.g. for non-empty and non-shared image instances.
- - Destroying an empty or shared image does nothing actually.
- \warning
- - When destroying a non-shared image, make sure that you will \e not operate on a remaining shared image
- that shares its buffer with the destroyed instance, in order to avoid further invalid memory access
- (to a deallocated buffer).
- **/
- ~CImg() {
- if (!_is_shared) delete[] _data;
- }
- //! Construct empty image.
- /**
- \note
- - An empty image has no pixel data and all of its dimensions width(), height(), depth(), spectrum()
- are set to \c 0, as well as its pixel buffer pointer data().
- - An empty image may be re-assigned afterwards, e.g. with the family of
- assign(unsigned int,unsigned int,unsigned int,unsigned int) methods,
- or by operator=(const CImg<t>&). In all cases, the type of pixels stays \c T.
- - An empty image is never shared.
- \par Example
- \code
- CImg<float> img1, img2; // Construct two empty images
- img1.assign(256,256,1,3); // Re-assign 'img1' to be a 256x256x1x3 (color) image
- img2 = img1.get_rand(0,255); // Re-assign 'img2' to be a random-valued version of 'img1'
- img2.assign(); // Re-assign 'img2' to be an empty image again
- \endcode
- **/
- CImg():_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {}
- //! Construct image with specified size.
- /**
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \note
- - It is able to create only \e non-shared images, and allocates thus a pixel buffer data()
- for each constructed image instance.
- - Setting one dimension \c size_x,\c size_y,\c size_z or \c size_c to \c 0 leads to the construction of
- an \e empty image.
- - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
- (e.g. when requested size is too big for available memory).
- \warning
- - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values.
- In order to initialize pixel values during construction (e.g. with \c 0), use constructor
- CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) instead.
- \par Example
- \code
- CImg<float> img1(256,256,1,3); // Construct a 256x256x1x3 (color) image, filled with garbage values
- CImg<float> img2(256,256,1,3,0); // Construct a 256x256x1x3 (color) image, filled with value '0'
- \endcode
- **/
- explicit CImg(const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1):
- _is_shared(false) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (siz) {
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Construct image with specified size and initialize pixel values.
- /**
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param value Initialization value.
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int),
- but it also fills the pixel buffer with the specified \c value.
- \warning
- - It cannot be used to construct a vector-valued image and initialize it with \e vector-valued pixels
- (e.g. RGB vector, for color images).
- For this task, you may use fillC() after construction.
- **/
- CImg(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c, const T& value):
- _is_shared(false) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (siz) {
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- fill(value);
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Construct image with specified size and initialize pixel values from a sequence of integers.
- /**
- 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, and initialize pixel
- values from the specified sequence of integers \c value0,\c value1,\c ...
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param value0 First value of the initialization sequence (must be an \e integer).
- \param value1 Second value of the initialization sequence (must be an \e integer).
- \param ...
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
- the pixel buffer with a sequence of specified integer values.
- \warning
- - You must specify \e exactly \c size_x*\c size_y*\c size_z*\c size_c integers in the initialization sequence.
- Otherwise, the constructor may crash or fill your image pixels with garbage.
- \par Example
- \code
- const CImg<float> img(2,2,1,3, // Construct a 2x2 color (RGB) image
- 0,255,0,255, // Set the 4 values for the red component
- 0,0,255,255, // Set the 4 values for the green component
- 64,64,64,64); // Set the 4 values for the blue component
- img.resize(150,150).display();
- \endcode
- \image html ref_constructor1.jpg
- **/
- CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
- const int value0, const int value1, ...):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- #define _CImg_stdarg(img,a0,a1,N,t) { \
- size_t _siz = (size_t)N; \
- if (_siz--) { \
- va_list ap; \
- va_start(ap,a1); \
- T *ptrd = (img)._data; \
- *(ptrd++) = (T)a0; \
- if (_siz--) { \
- *(ptrd++) = (T)a1; \
- for ( ; _siz; --_siz) *(ptrd++) = (T)va_arg(ap,t); \
- } \
- va_end(ap); \
- } \
- }
- assign(size_x,size_y,size_z,size_c);
- _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),int);
- }
- #if cimg_use_cpp11==1
- //! Construct image with specified size and initialize pixel values from an initializer list of integers.
- /**
- 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, and initialize pixel
- values from the specified initializer list of integers { \c value0,\c value1,\c ... }
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param { value0, value1, ... } Initialization list
- \param repeat_values Tells if the value filling process is repeated over the image.
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
- the pixel buffer with a sequence of specified integer values.
- \par Example
- \code
- const CImg<float> img(2,2,1,3, // Construct a 2x2 color (RGB) image
- { 0,255,0,255, // Set the 4 values for the red component
- 0,0,255,255, // Set the 4 values for the green component
- 64,64,64,64 }); // Set the 4 values for the blue component
- img.resize(150,150).display();
- \endcode
- \image html ref_constructor1.jpg
- **/
- template<typename t>
- CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
- const std::initializer_list<t> values,
- const bool repeat_values=true):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- #define _cimg_constructor_cpp11(repeat_values) \
- auto it = values.begin(); \
- size_t siz = size(); \
- if (repeat_values) for (T *ptrd = _data; siz--; ) { \
- *(ptrd++) = (T)(*(it++)); if (it==values.end()) it = values.begin(); } \
- else { siz = std::min(siz,values.size()); for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++)); }
- assign(size_x,size_y,size_z,size_c);
- _cimg_constructor_cpp11(repeat_values);
- }
- template<typename t>
- CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z,
- std::initializer_list<t> values,
- const bool repeat_values=true):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(size_x,size_y,size_z);
- _cimg_constructor_cpp11(repeat_values);
- }
- template<typename t>
- CImg(const unsigned int size_x, const unsigned int size_y,
- std::initializer_list<t> values,
- const bool repeat_values=true):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(size_x,size_y);
- _cimg_constructor_cpp11(repeat_values);
- }
- template<typename t>
- CImg(const unsigned int size_x,
- std::initializer_list<t> values,
- const bool repeat_values=true):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(size_x);
- _cimg_constructor_cpp11(repeat_values);
- }
- //! Construct single channel 1D image with pixel values and width obtained from an initializer list of integers.
- /**
- Construct a new image instance of size \c width x \c 1 x \c 1 x \c 1,
- with pixels of type \c T, and initialize pixel
- values from the specified initializer list of integers { \c value0,\c value1,\c ... }. Image width is
- given by the size of the initializer list.
- \param { value0, value1, ... } Initialization list
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int) with height=1, depth=1, and spectrum=1,
- but it also fills the pixel buffer with a sequence of specified integer values.
- \par Example
- \code
- const CImg<float> img = {10,20,30,20,10 }; // Construct a 5x1 image with one channel, and set its pixel values
- img.resize(150,150).display();
- \endcode
- \image html ref_constructor1.jpg
- **/
- template<typename t>
- CImg(const std::initializer_list<t> values):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(values.size(),1,1,1);
- auto it = values.begin();
- unsigned int siz = _width;
- for (T *ptrd = _data; siz--; ) *(ptrd++) = (T)(*(it++));
- }
- template<typename t>
- CImg<T>& operator=(std::initializer_list<t> values) {
- _cimg_constructor_cpp11(siz>values.size());
- return *this;
- }
- #endif
- //! Construct image with specified size and initialize pixel values from a sequence of doubles.
- /**
- 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,
- and initialize pixel values from the specified sequence of doubles \c value0,\c value1,\c ...
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param value0 First value of the initialization sequence (must be a \e double).
- \param value1 Second value of the initialization sequence (must be a \e double).
- \param ...
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...), but
- takes a sequence of double values instead of integers.
- \warning
- - You must specify \e exactly \c dx*\c dy*\c dz*\c dc doubles in the initialization sequence.
- Otherwise, the constructor may crash or fill your image with garbage.
- For instance, the code below will probably crash on most platforms:
- \code
- const CImg<float> img(2,2,1,1, 0.5,0.5,255,255); // FAIL: The two last arguments are 'int', not 'double'!
- \endcode
- **/
- CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
- const double value0, const double value1, ...):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(size_x,size_y,size_z,size_c);
- _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),double);
- }
- //! Construct image with specified size and initialize pixel values from a value string.
- /**
- 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,
- and initializes pixel values from the specified string \c values.
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param values Value string describing the way pixel values are set.
- \param repeat_values Tells if the value filling process is repeated over the image.
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it also fills
- the pixel buffer with values described in the value string \c values.
- - Value string \c values may describe two different filling processes:
- - Either \c values is a sequences of values assigned to the image pixels, as in <tt>"1,2,3,7,8,2"</tt>.
- In this case, set \c repeat_values to \c true to periodically fill the image with the value sequence.
- - Either, \c values is a formula, as in <tt>"cos(x/10)*sin(y/20)"</tt>.
- In this case, parameter \c repeat_values is pointless.
- - For both cases, specifying \c repeat_values is mandatory.
- It disambiguates the possible overloading of constructor
- CImg(unsigned int,unsigned int,unsigned int,unsigned int,T) with \c T being a <tt>const char*</tt>.
- - A \c CImgArgumentException is thrown when an invalid value string \c values is specified.
- \par Example
- \code
- const CImg<float> img1(129,129,1,3,"0,64,128,192,255",true), // Construct image from a value sequence
- img2(129,129,1,3,"if(c==0,255*abs(cos(x/10)),1.8*y)",false); // Construct image from a formula
- (img1,img2).display();
- \endcode
- \image html ref_constructor2.jpg
- **/
- CImg(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z, const unsigned int size_c,
- const char *const values, const bool repeat_values):_is_shared(false) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (siz) {
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- fill(values,repeat_values);
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer.
- /**
- 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,
- and initializes pixel values from the specified \c t* memory buffer.
- \param values Pointer to the input memory buffer.
- \param size_x Image width().
- \param size_y Image height().
- \param size_z Image depth().
- \param size_c Image spectrum() (number of channels).
- \param is_shared Tells if input memory buffer must be shared by the current instance.
- \note
- - If \c is_shared is \c false, the image instance allocates its own pixel buffer,
- and values from the specified input buffer are copied to the instance buffer.
- If buffer types \c T and \c t are different, a regular static cast is performed during buffer copy.
- - Otherwise, the image instance does \e not allocate a new buffer, and uses the input memory buffer as its
- own pixel buffer. This case requires that types \c T and \c t are the same. Later, destroying such a shared
- image will not deallocate the pixel buffer, this task being obviously charged to the initial buffer allocator.
- - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
- (e.g. when requested size is too big for available memory).
- \warning
- - You must take care when operating on a shared image, since it may have an invalid pixel buffer pointer data()
- (e.g. already deallocated).
- \par Example
- \code
- unsigned char tab[256*256] = { 0 };
- CImg<unsigned char> img1(tab,256,256,1,1,false), // Construct new non-shared image from buffer 'tab'
- img2(tab,256,256,1,1,true); // Construct new shared-image from buffer 'tab'
- tab[1024] = 255; // Here, 'img2' is indirectly modified, but not 'img1'
- \endcode
- **/
- template<typename t>
- CImg(const t *const values, const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false):_is_shared(false) {
- if (is_shared) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgArgumentException(_cimg_instance
- "CImg(): Invalid construction request of a (%u,%u,%u,%u) shared instance "
- "from a (%s*) buffer (pixel types are different).",
- cimg_instance,
- size_x,size_y,size_z,size_c,CImg<t>::pixel_type());
- }
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (values && siz) {
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer \specialization.
- CImg(const T *const values, const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1, const bool is_shared=false) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (values && siz) {
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = is_shared;
- if (_is_shared) _data = const_cast<T*>(values);
- else {
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- std::memcpy(_data,values,siz*sizeof(T));
- }
- } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
- }
- //! Construct image from memory buffer with specified size and pixel ordering scheme.
- template<typename t>
- CImg(const t *const values, const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const char *const axes_order):_data(0),_is_shared(false) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (values && siz) {
- unsigned char s_code[4] = { 0,1,2,3 }, n_code[4] = { 0 };
- for (unsigned int l = 0; axes_order[l]; ++l) {
- int c = cimg::lowercase(axes_order[l]);
- if (l>=4 || (c!='x' && c!='y' && c!='z' && c!='c')) { *s_code = 4; break; }
- else { ++n_code[c%=4]; s_code[l] = c; }
- }
- if (*axes_order && *s_code<4 && *n_code<=1 && n_code[1]<=1 && n_code[2]<=1 && n_code[3]<=1) {
- const unsigned int code = (s_code[0]<<12) | (s_code[1]<<8) | (s_code[2]<<4) | (s_code[3]);
- int s0 = 0, s1 = 0, s2 = 0, s3 = 0;
- const char *inv_order = 0;
- switch (code) {
- case 0x0123 : inv_order = "xyzc"; s0 = size_x; s1 = size_y; s2 = size_z; s3 = size_c; break; // xyzc
- case 0x0132 : inv_order = "xyzc"; s0 = size_x; s1 = size_y; s2 = size_c; s3 = size_z; break; // xycz
- case 0x0213 : inv_order = "xzyc"; s0 = size_x; s1 = size_z; s2 = size_y; s3 = size_c; break; // xzyc
- case 0x0231 : inv_order = "xcyz"; s0 = size_x; s1 = size_z; s2 = size_c; s3 = size_y; break; // xzcy
- case 0x0312 : inv_order = "xzcy"; s0 = size_x; s1 = size_c; s2 = size_y; s3 = size_z; break; // xcyz
- case 0x0321 : inv_order = "xczy"; s0 = size_x; s1 = size_c; s2 = size_z; s3 = size_y; break; // xczy
- case 0x1023 : inv_order = "yxzc"; s0 = size_y; s1 = size_x; s2 = size_z; s3 = size_c; break; // yxzc
- case 0x1032 : inv_order = "yxcz"; s0 = size_y; s1 = size_x; s2 = size_c; s3 = size_z; break; // yxcz
- case 0x1203 : inv_order = "zxyc"; s0 = size_y; s1 = size_z; s2 = size_x; s3 = size_c; break; // yzxc
- case 0x1230 : inv_order = "cxyz"; s0 = size_y; s1 = size_z; s2 = size_c; s3 = size_x; break; // yzcx
- case 0x1302 : inv_order = "zxcy"; s0 = size_y; s1 = size_c; s2 = size_x; s3 = size_z; break; // ycxz
- case 0x1320 : inv_order = "cxzy"; s0 = size_y; s1 = size_c; s2 = size_z; s3 = size_x; break; // yczx
- case 0x2013 : inv_order = "yzxc"; s0 = size_z; s1 = size_x; s2 = size_y; s3 = size_c; break; // zxyc
- case 0x2031 : inv_order = "ycxz"; s0 = size_z; s1 = size_x; s2 = size_c; s3 = size_y; break; // zxcy
- case 0x2103 : inv_order = "zyxc"; s0 = size_z; s1 = size_y; s2 = size_x; s3 = size_c; break; // zyxc
- case 0x2130 : inv_order = "cyxz"; s0 = size_z; s1 = size_y; s2 = size_c; s3 = size_x; break; // zycx
- case 0x2301 : inv_order = "zcxy"; s0 = size_z; s1 = size_c; s2 = size_x; s3 = size_y; break; // zcxy
- case 0x2310 : inv_order = "czxy"; s0 = size_z; s1 = size_c; s2 = size_y; s3 = size_x; break; // zcyx
- case 0x3012 : inv_order = "yzcx"; s0 = size_c; s1 = size_x; s2 = size_y; s3 = size_z; break; // cxyz
- case 0x3021 : inv_order = "yczx"; s0 = size_c; s1 = size_x; s2 = size_z; s3 = size_y; break; // cxzy
- case 0x3102 : inv_order = "zycx"; s0 = size_c; s1 = size_y; s2 = size_x; s3 = size_z; break; // cyxz
- case 0x3120 : inv_order = "cyzx"; s0 = size_c; s1 = size_y; s2 = size_z; s3 = size_x; break; // cyzx
- case 0x3201 : inv_order = "zcyx"; s0 = size_c; s1 = size_z; s2 = size_x; s3 = size_y; break; // czxy
- case 0x3210 : inv_order = "czyx"; s0 = size_c; s1 = size_z; s2 = size_y; s3 = size_x; break; // czyx
- }
- CImg<t>(values,s0,s1,s2,s3,true).get_permute_axes(inv_order).move_to(*this);
- } else {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgArgumentException(_cimg_instance
- "CImg(): Invalid specified axes order '%s'.",
- cimg_instance,
- axes_order);
- }
- } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
- }
- //! Construct image from reading an image file.
- /**
- Construct a new image instance with pixels of type \c T, and initialize pixel values with the data read from
- an image file.
- \param filename Filename, as a C-string.
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it reads the image
- dimensions and pixel values from the specified image file.
- - The recognition of the image file format by %CImg higlhy depends on the tools installed on your system
- and on the external libraries you used to link your code against.
- - Considered pixel type \c T should better fit the file format specification, or data loss may occur during
- file load (e.g. constructing a \c CImg<unsigned char> from a float-valued image file).
- - A \c CImgIOException is thrown when the specified \c filename cannot be read, or if the file format is not
- recognized.
- \par Example
- \code
- const CImg<float> img("reference.jpg");
- img.display();
- \endcode
- \image html ref_image.jpg
- **/
- explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(filename);
- }
- //! Construct image copy.
- /**
- Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg<t> instance.
- \param img Input image to copy.
- \note
- - Constructed copy has the same size width() x height() x depth() x spectrum() and pixel values as the
- input image \c img.
- - If input image \c img is \e shared and if types \c T and \c t are the same, the constructed copy is also
- \e shared, and shares its pixel buffer with \c img.
- Modifying a pixel value in the constructed copy will thus also modifies it in the input image \c img.
- This behavior is needful to allow functions to return shared images.
- - Otherwise, the constructed copy allocates its own pixel buffer, and copies pixel values from the input
- image \c img into its buffer. The copied pixel values may be eventually statically casted if types \c T and
- \c t are different.
- - Constructing a copy from an image \c img when types \c t and \c T are the same is significantly faster than
- with different types.
- - A \c CImgInstanceException is thrown when the pixel buffer cannot be allocated
- (e.g. not enough available memory).
- **/
- template<typename t>
- CImg(const CImg<t>& img):_is_shared(false) {
- const size_t siz = (size_t)img.size();
- if (img._data && siz) {
- _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
- img._width,img._height,img._depth,img._spectrum);
- }
- const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Construct image copy \specialization.
- CImg(const CImg<T>& img) {
- const size_t siz = (size_t)img.size();
- if (img._data && siz) {
- _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
- _is_shared = img._is_shared;
- if (_is_shared) _data = const_cast<T*>(img._data);
- else {
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
- img._width,img._height,img._depth,img._spectrum);
- }
- std::memcpy(_data,img._data,siz*sizeof(T));
- }
- } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
- }
- //! Advanced copy constructor.
- /**
- Construct a new image instance with pixels of type \c T, as a copy of an existing \c CImg<t> instance,
- while forcing the shared state of the constructed copy.
- \param img Input image to copy.
- \param is_shared Tells about the shared state of the constructed copy.
- \note
- - Similar to CImg(const CImg<t>&), except that it allows to decide the shared state of
- the constructed image, which does not depend anymore on the shared state of the input image \c img:
- - If \c is_shared is \c true, the constructed copy will share its pixel buffer with the input image \c img.
- For that case, the pixel types \c T and \c t \e must be the same.
- - If \c is_shared is \c false, the constructed copy will allocate its own pixel buffer, whether the input
- image \c img is shared or not.
- - A \c CImgArgumentException is thrown when a shared copy is requested with different pixel types \c T and \c t.
- **/
- template<typename t>
- CImg(const CImg<t>& img, const bool is_shared):_is_shared(false) {
- if (is_shared) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgArgumentException(_cimg_instance
- "CImg(): Invalid construction request of a shared instance from a "
- "CImg<%s> image (%u,%u,%u,%u,%p) (pixel types are different).",
- cimg_instance,
- CImg<t>::pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data);
- }
- const size_t siz = (size_t)img.size();
- if (img._data && siz) {
- _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
- img._width,img._height,img._depth,img._spectrum);
- }
- const t *ptrs = img._data; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
- } else { _width = _height = _depth = _spectrum = 0; _data = 0; }
- }
- //! Advanced copy constructor \specialization.
- CImg(const CImg<T>& img, const bool is_shared) {
- const size_t siz = (size_t)img.size();
- if (img._data && siz) {
- _width = img._width; _height = img._height; _depth = img._depth; _spectrum = img._spectrum;
- _is_shared = is_shared;
- if (_is_shared) _data = const_cast<T*>(img._data);
- else {
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*img._width*img._height*img._depth*img._spectrum),
- img._width,img._height,img._depth,img._spectrum);
- }
- std::memcpy(_data,img._data,siz*sizeof(T));
- }
- } else { _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0; }
- }
- //! Construct image with dimensions borrowed from another image.
- /**
- Construct a new image instance with pixels of type \c T, and size get from some dimensions of an existing
- \c CImg<t> instance.
- \param img Input image from which dimensions are borrowed.
- \param dimensions C-string describing the image size along the X,Y,Z and C-dimensions.
- \note
- - Similar to CImg(unsigned int,unsigned int,unsigned int,unsigned int), but it takes the image dimensions
- (\e not its pixel values) from an existing \c CImg<t> instance.
- - The allocated pixel buffer is \e not filled with a default value, and is likely to contain garbage values.
- In order to initialize pixel values (e.g. with \c 0), use constructor CImg(const CImg<t>&,const char*,T)
- instead.
- \par Example
- \code
- const CImg<float> img1(256,128,1,3), // 'img1' is a 256x128x1x3 image
- img2(img1,"xyzc"), // 'img2' is a 256x128x1x3 image
- img3(img1,"y,x,z,c"), // 'img3' is a 128x256x1x3 image
- img4(img1,"c,x,y,3",0), // 'img4' is a 3x128x256x3 image (with pixels initialized to '0')
- \endcode
- **/
- template<typename t>
- CImg(const CImg<t>& img, const char *const dimensions):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(img,dimensions);
- }
- //! Construct image with dimensions borrowed from another image and initialize pixel values.
- /**
- Construct a new image instance with pixels of type \c T, and size get from the dimensions of an existing
- \c CImg<t> instance, and set all pixel values to specified \c value.
- \param img Input image from which dimensions are borrowed.
- \param dimensions String describing the image size along the X,Y,Z and V-dimensions.
- \param value Value used for initialization.
- \note
- - Similar to CImg(const CImg<t>&,const char*), but it also fills the pixel buffer with the specified \c value.
- **/
- template<typename t>
- CImg(const CImg<t>& img, const char *const dimensions, const T& value):
- _width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- assign(img,dimensions).fill(value);
- }
- //! Construct image from a display window.
- /**
- Construct a new image instance with pixels of type \c T, as a snapshot of an existing \c CImgDisplay instance.
- \param disp Input display window.
- \note
- - The width() and height() of the constructed image instance are the same as the specified \c CImgDisplay.
- - The depth() and spectrum() of the constructed image instance are respectively set to \c 1 and \c 3
- (i.e. a 2D color image).
- - The image pixels are read as 8-bits RGB values.
- **/
- explicit CImg(const CImgDisplay &disp):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- disp.snapshot(*this);
- }
- // Constructor and assignment operator for rvalue references (c++11).
- // This avoids an additional image copy for methods returning new images. Can save RAM for big images !
- #if cimg_use_cpp11==1
- CImg(CImg<T>&& img):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
- swap(img);
- }
- CImg<T>& operator=(CImg<T>&& img) {
- if (_is_shared) return assign(img);
- return img.swap(*this);
- }
- #endif
- //! Construct empty image \inplace.
- /**
- In-place version of the default constructor CImg(). It simply resets the instance to an empty image.
- **/
- CImg<T>& assign() {
- if (!_is_shared) delete[] _data;
- _width = _height = _depth = _spectrum = 0; _is_shared = false; _data = 0;
- return *this;
- }
- //! Construct image with specified size \inplace.
- /**
- In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int).
- **/
- CImg<T>& assign(const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (!siz) return assign();
- const size_t curr_siz = (size_t)size();
- if (siz!=curr_siz) {
- if (_is_shared)
- throw CImgArgumentException(_cimg_instance
- "assign(): Invalid assignment request of shared instance from specified "
- "image (%u,%u,%u,%u).",
- cimg_instance,
- size_x,size_y,size_z,size_c);
- else {
- delete[] _data;
- try { _data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- }
- }
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- return *this;
- }
- //! Construct image with specified size and initialize pixel values \inplace.
- /**
- In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,T).
- **/
- CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c, const T& value) {
- return assign(size_x,size_y,size_z,size_c).fill(value);
- }
- //! Construct image with specified size and initialize pixel values from a sequence of integers \inplace.
- /**
- In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,int,int,...).
- **/
- CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const int value0, const int value1, ...) {
- assign(size_x,size_y,size_z,size_c);
- _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),int);
- return *this;
- }
- //! Construct image with specified size and initialize pixel values from a sequence of doubles \inplace.
- /**
- In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,double,double,...).
- **/
- CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const double value0, const double value1, ...) {
- assign(size_x,size_y,size_z,size_c);
- _CImg_stdarg(*this,value0,value1,safe_size(size_x,size_y,size_z,size_c),double);
- return *this;
- }
- //! Construct image with specified size and initialize pixel values from a value string \inplace.
- /**
- In-place version of the constructor CImg(unsigned int,unsigned int,unsigned int,unsigned int,const char*,bool).
- **/
- CImg<T>& assign(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const char *const values, const bool repeat_values) {
- return assign(size_x,size_y,size_z,size_c).fill(values,repeat_values);
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer \inplace.
- /**
- In-place version of the constructor CImg(const t*,unsigned int,unsigned int,unsigned int,unsigned int).
- **/
- template<typename t>
- CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (!values || !siz) return assign();
- assign(size_x,size_y,size_z,size_c);
- const t *ptrs = values; cimg_for(*this,ptrd,T) *ptrd = (T)*(ptrs++);
- return *this;
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer \specialization.
- CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (!values || !siz) return assign();
- const size_t curr_siz = (size_t)size();
- if (values==_data && siz==curr_siz) return assign(size_x,size_y,size_z,size_c);
- if (_is_shared || values + siz<_data || values>=_data + size()) {
- assign(size_x,size_y,size_z,size_c);
- if (_is_shared) std::memmove((void*)_data,(void*)values,siz*sizeof(T));
- else std::memcpy((void*)_data,(void*)values,siz*sizeof(T));
- } else {
- T *new_data = 0;
- try { new_data = new T[siz]; } catch (...) {
- _width = _height = _depth = _spectrum = 0; _data = 0;
- throw CImgInstanceException(_cimg_instance
- "assign(): Failed to allocate memory (%s) for image (%u,%u,%u,%u).",
- cimg_instance,
- cimg::strbuffersize(sizeof(T)*size_x*size_y*size_z*size_c),
- size_x,size_y,size_z,size_c);
- }
- std::memcpy((void*)new_data,(void*)values,siz*sizeof(T));
- delete[] _data; _data = new_data; _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c;
- }
- return *this;
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
- template<typename t>
- CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
- if (is_shared)
- throw CImgArgumentException(_cimg_instance
- "assign(): Invalid assignment request of shared instance from (%s*) buffer"
- "(pixel types are different).",
- cimg_instance,
- CImg<t>::pixel_type());
- return assign(values,size_x,size_y,size_z,size_c);
- }
- //! Construct image with specified size and initialize pixel values from a memory buffer \overloading.
- CImg<T>& assign(const T *const values, const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c, const bool is_shared) {
- const size_t siz = safe_size(size_x,size_y,size_z,size_c);
- if (!values || !siz) return assign();
- if (!is_shared) { if (_is_shared) assign(); assign(values,size_x,size_y,size_z,size_c); }
- else {
- if (!_is_shared) {
- if (values + siz<_data || values>=_data + size()) assign();
- else cimg::warn(_cimg_instance
- "assign(): Shared image instance has overlapping memory.",
- cimg_instance);
- }
- _width = size_x; _height = size_y; _depth = size_z; _spectrum = size_c; _is_shared = true;
- _data = const_cast<T*>(values);
- }
- return *this;
- }
- //! Construct image from memory buffer with specified size and pixel ordering scheme.
- template<typename t>
- CImg<T>& assign(const t *const values, const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const char *const axes_order) {
- CImg<T>(values,size_x,size_y,size_z,size_c,axes_order).move_to(*this);
- }
- //! Construct image from reading an image file \inplace.
- /**
- In-place version of the constructor CImg(const char*).
- **/
- CImg<T>& assign(const char *const filename) {
- return load(filename);
- }
- //! Construct image copy \inplace.
- /**
- In-place version of the constructor CImg(const CImg<t>&).
- **/
- template<typename t>
- CImg<T>& assign(const CImg<t>& img) {
- return assign(img._data,img._width,img._height,img._depth,img._spectrum);
- }
- //! In-place version of the advanced copy constructor.
- /**
- In-place version of the constructor CImg(const CImg<t>&,bool).
- **/
- template<typename t>
- CImg<T>& assign(const CImg<t>& img, const bool is_shared) {
- return assign(img._data,img._width,img._height,img._depth,img._spectrum,is_shared);
- }
- //! Construct image with dimensions borrowed from another image \inplace.
- /**
- In-place version of the constructor CImg(const CImg<t>&,const char*).
- **/
- template<typename t>
- CImg<T>& assign(const CImg<t>& img, const char *const dimensions) {
- if (!dimensions || !*dimensions) return assign(img._width,img._height,img._depth,img._spectrum);
- unsigned int siz[4] = { 0,1,1,1 }, k = 0;
- CImg<charT> item(256);
- for (const char *s = dimensions; *s && k<4; ++k) {
- if (cimg_sscanf(s,"%255[^0-9%xyzvwhdcXYZVWHDC]",item._data)>0) s+=std::strlen(item);
- if (*s) {
- unsigned int val = 0; char sep = 0;
- if (cimg_sscanf(s,"%u%c",&val,&sep)>0) {
- if (sep=='%') siz[k] = val*(k==0?_width:k==1?_height:k==2?_depth:_spectrum)/100;
- else siz[k] = val;
- while (*s>='0' && *s<='9') ++s;
- if (sep=='%') ++s;
- } else switch (cimg::lowercase(*s)) {
- case 'x' : case 'w' : siz[k] = img._width; ++s; break;
- case 'y' : case 'h' : siz[k] = img._height; ++s; break;
- case 'z' : case 'd' : siz[k] = img._depth; ++s; break;
- case 'c' : case 's' : siz[k] = img._spectrum; ++s; break;
- default :
- throw CImgArgumentException(_cimg_instance
- "assign(): Invalid character '%c' detected in specified dimension string '%s'.",
- cimg_instance,
- *s,dimensions);
- }
- }
- }
- return assign(siz[0],siz[1],siz[2],siz[3]);
- }
- //! Construct image with dimensions borrowed from another image and initialize pixel values \inplace.
- /**
- In-place version of the constructor CImg(const CImg<t>&,const char*,T).
- **/
- template<typename t>
- CImg<T>& assign(const CImg<t>& img, const char *const dimensions, const T& value) {
- return assign(img,dimensions).fill(value);
- }
- //! Construct image from a display window \inplace.
- /**
- In-place version of the constructor CImg(const CImgDisplay&).
- **/
- CImg<T>& assign(const CImgDisplay &disp) {
- disp.snapshot(*this);
- return *this;
- }
- //! Construct empty image \inplace.
- /**
- Equivalent to assign().
- \note
- - It has been defined for compatibility with STL naming conventions.
- **/
- CImg<T>& clear() {
- return assign();
- }
- //! Transfer content of an image instance into another one.
- /**
- Transfer the dimensions and the pixel buffer content of an image instance into another one,
- and replace instance by an empty image. It avoids the copy of the pixel buffer
- when possible.
- \param img Destination image.
- \note
- - Pixel types \c T and \c t of source and destination images can be different, though the process is
- designed to be instantaneous when \c T and \c t are the same.
- \par Example
- \code
- CImg<float> src(256,256,1,3,0), // Construct a 256x256x1x3 (color) image filled with value '0'
- dest(16,16); // Construct a 16x16x1x1 (scalar) image
- src.move_to(dest); // Now, 'src' is empty and 'dest' is the 256x256x1x3 image
- \endcode
- **/
- template<typename t>
- CImg<t>& move_to(CImg<t>& img) {
- img.assign(*this);
- assign();
- return img;
- }
- //! Transfer content of an image instance into another one \specialization.
- CImg<T>& move_to(CImg<T>& img) {
- if (_is_shared || img._is_shared) img.assign(*this);
- else swap(img);
- assign();
- return img;
- }
- //! Transfer content of an image instance into a new image in an image list.
- /**
- Transfer the dimensions and the pixel buffer content of an image instance
- into a newly inserted image at position \c pos in specified \c CImgList<t> instance.
- \param list Destination list.
- \param pos Position of the newly inserted image in the list.
- \note
- - When optional parameter \c pos is omitted, the image instance is transferred as a new
- image at the end of the specified \c list.
- - It is convenient to sequentially insert new images into image lists, with no
- additional copies of memory buffer.
- \par Example
- \code
- CImgList<float> list; // Construct an empty image list
- CImg<float> img("reference.jpg"); // Read image from filename
- img.move_to(list); // Transfer image content as a new item in the list (no buffer copy)
- \endcode
- **/
- template<typename t>
- CImgList<t>& move_to(CImgList<t>& list, const unsigned int pos=~0U) {
- const unsigned int npos = pos>list._width?list._width:pos;
- move_to(list.insert(1,npos)[npos]);
- return list;
- }
- //! Swap fields of two image instances.
- /**
- \param img Image to swap fields with.
- \note
- - It can be used to interchange the content of two images in a very fast way. Can be convenient when dealing
- with algorithms requiring two swapping buffers.
- \par Example
- \code
- CImg<float> img1("lena.jpg"),
- img2("milla.jpg");
- img1.swap(img2); // Now, 'img1' is 'milla' and 'img2' is 'lena'
- \endcode
- **/
- CImg<T>& swap(CImg<T>& img) {
- cimg::swap(_width,img._width,_height,img._height,_depth,img._depth,_spectrum,img._spectrum);
- cimg::swap(_data,img._data);
- cimg::swap(_is_shared,img._is_shared);
- return img;
- }
- //! Return a reference to an empty image.
- /**
- \note
- This function is useful mainly to declare optional parameters having type \c CImg<T> in functions prototypes,
- e.g.
- \code
- void f(const int x=0, const int y=0, const CImg<float>& img=CImg<float>::empty());
- \endcode
- **/
- static CImg<T>& empty() {
- static CImg<T> _empty;
- return _empty.assign();
- }
- //! Return a reference to an empty image \const.
- static const CImg<T>& const_empty() {
- static const CImg<T> _empty;
- return _empty;
- }
- //@}
- //------------------------------------------
- //
- //! \name Overloaded Operators
- //@{
- //------------------------------------------
- //! Access to a pixel value.
- /**
- Return a reference to a located pixel value of the image instance,
- being possibly \e const, whether the image instance is \e const or not.
- This is the standard method to get/set pixel values in \c CImg<T> images.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Range of pixel coordinates start from <tt>(0,0,0,0)</tt> to
- <tt>(width() - 1,height() - 1,depth() - 1,spectrum() - 1)</tt>.
- - Due to the particular arrangement of the pixel buffers defined in %CImg, you can omit one coordinate if the
- corresponding dimension is equal to \c 1.
- For instance, pixels of a 2D image (depth() equal to \c 1) can be accessed by <tt>img(x,y,c)</tt> instead of
- <tt>img(x,y,0,c)</tt>.
- \warning
- - There is \e no boundary checking done in this operator, to make it as fast as possible.
- You \e must take care of out-of-bounds access by yourself, if necessary.
- For debugging purposes, you may want to define macro \c 'cimg_verbosity'>=3 to enable additional boundary
- checking operations in this operator. In that case, warning messages will be printed on the error output
- when accessing out-of-bounds pixels.
- \par Example
- \code
- CImg<float> img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'
- const float
- valR = img(10,10,0,0), // Read red value at coordinates (10,10)
- valG = img(10,10,0,1), // Read green value at coordinates (10,10)
- valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted)
- avg = (valR + valG + valB)/3; // Compute average pixel value
- img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value
- \endcode
- **/
- #if cimg_verbosity>=3
- T& operator()(const unsigned int x, const unsigned int y=0,
- const unsigned int z=0, const unsigned int c=0) {
- const ulongT off = (ulongT)offset(x,y,z,c);
- if (!_data || off>=size()) {
- cimg::warn(_cimg_instance
- "operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].",
- cimg_instance,
- (int)x,(int)y,(int)z,(int)c,off);
- return *_data;
- }
- else return _data[off];
- }
- //! Access to a pixel value \const.
- const T& operator()(const unsigned int x, const unsigned int y=0,
- const unsigned int z=0, const unsigned int c=0) const {
- return const_cast<CImg<T>*>(this)->operator()(x,y,z,c);
- }
- //! Access to a pixel value.
- /**
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param wh Precomputed offset, must be equal to <tt>width()*\ref height()</tt>.
- \param whd Precomputed offset, must be equal to <tt>width()*\ref height()*\ref depth()</tt>.
- \note
- - Similar to (but faster than) operator()().
- It uses precomputed offsets to optimize memory access. You may use it to optimize
- the reading/writing of several pixel values in the same image (e.g. in a loop).
- **/
- T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
- const ulongT wh, const ulongT whd=0) {
- cimg::unused(wh,whd);
- return (*this)(x,y,z,c);
- }
- //! Access to a pixel value \const.
- const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
- const ulongT wh, const ulongT whd=0) const {
- cimg::unused(wh,whd);
- return (*this)(x,y,z,c);
- }
- #else
- T& operator()(const unsigned int x) {
- return _data[x];
- }
- const T& operator()(const unsigned int x) const {
- return _data[x];
- }
- T& operator()(const unsigned int x, const unsigned int y) {
- return _data[x + y*_width];
- }
- const T& operator()(const unsigned int x, const unsigned int y) const {
- return _data[x + y*_width];
- }
- T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) {
- return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height];
- }
- const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z) const {
- return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height];
- }
- T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) {
- return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height + c*(ulongT)_width*_height*_depth];
- }
- const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c) const {
- return _data[x + y*(ulongT)_width + z*(ulongT)_width*_height + c*(ulongT)_width*_height*_depth];
- }
- T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int,
- const ulongT wh) {
- return _data[x + y*_width + z*wh];
- }
- const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int,
- const ulongT wh) const {
- return _data[x + y*_width + z*wh];
- }
- T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
- const ulongT wh, const ulongT whd) {
- return _data[x + y*_width + z*wh + c*whd];
- }
- const T& operator()(const unsigned int x, const unsigned int y, const unsigned int z, const unsigned int c,
- const ulongT wh, const ulongT whd) const {
- return _data[x + y*_width + z*wh + c*whd];
- }
- #endif
- //! Implicitly cast an image into a \c T*.
- /**
- Implicitly cast a \c CImg<T> instance into a \c T* or \c const \c T* pointer, whether the image instance
- is \e const or not. The returned pointer points on the first value of the image pixel buffer.
- \note
- - It simply returns the pointer data() to the pixel buffer.
- - This implicit conversion is convenient to test the empty state of images (data() being \c 0 in this case), e.g.
- \code
- CImg<float> img1(100,100), img2; // 'img1' is a 100x100 image, 'img2' is an empty image
- if (img1) { // Test succeeds, 'img1' is not an empty image
- if (!img2) { // Test succeeds, 'img2' is an empty image
- std::printf("'img1' is not empty, 'img2' is empty.");
- }
- }
- \endcode
- - It also allows to use brackets to access pixel values, without need for a \c CImg<T>::operator[](), e.g.
- \code
- CImg<float> img(100,100);
- const float value = img[99]; // Access to value of the last pixel on the first row
- img[510] = 255; // Set pixel value at (10,5)
- \endcode
- **/
- operator T*() {
- return _data;
- }
- //! Implicitly cast an image into a \c T* \const.
- operator const T*() const {
- return _data;
- }
- //! Assign a value to all image pixels.
- /**
- Assign specified \c value to each pixel value of the image instance.
- \param value Value that will be assigned to image pixels.
- \note
- - The image size is never modified.
- - The \c value may be casted to pixel type \c T if necessary.
- \par Example
- \code
- CImg<char> img(100,100); // Declare image (with garbage values)
- img = 0; // Set all pixel values to '0'
- img = 1.2; // Set all pixel values to '1' (cast of '1.2' as a 'char')
- \endcode
- **/
- CImg<T>& operator=(const T& value) {
- return fill(value);
- }
- //! Assign pixels values from a specified expression.
- /**
- Initialize all pixel values from the specified string \c expression.
- \param expression Value string describing the way pixel values are set.
- \note
- - String parameter \c expression may describe different things:
- - 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"),
- the pixel values are set from specified \c expression and the image size is not modified.
- - If \c expression is a filename (as in \c "reference.jpg"), the corresponding image file is loaded and
- replace the image instance. The image size is modified if necessary.
- \par Example
- \code
- CImg<float> img1(100,100), img2(img1), img3(img1); // Declare 3 scalar images 100x100 with uninitialized values
- img1 = "0,50,100,150,200,250,200,150,100,50"; // Set pixel values of 'img1' from a value sequence
- img2 = "10*((x*y)%25)"; // Set pixel values of 'img2' from a formula
- img3 = "reference.jpg"; // Set pixel values of 'img3' from a file (image size is modified)
- (img1,img2,img3).display();
- \endcode
- \image html ref_operator_eq.jpg
- **/
- CImg<T>& operator=(const char *const expression) {
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try {
- _fill(expression,true,1,0,0,"operator=",0);
- } catch (CImgException&) {
- cimg::exception_mode(omode);
- load(expression);
- }
- cimg::exception_mode(omode);
- return *this;
- }
- //! Copy an image into the current image instance.
- /**
- Similar to the in-place copy constructor assign(const CImg<t>&).
- **/
- template<typename t>
- CImg<T>& operator=(const CImg<t>& img) {
- return assign(img);
- }
- //! Copy an image into the current image instance \specialization.
- CImg<T>& operator=(const CImg<T>& img) {
- return assign(img);
- }
- //! Copy the content of a display window to the current image instance.
- /**
- Similar to assign(const CImgDisplay&).
- **/
- CImg<T>& operator=(const CImgDisplay& disp) {
- disp.snapshot(*this);
- return *this;
- }
- //! In-place addition operator.
- /**
- Add specified \c value to all pixels of an image instance.
- \param value Value to add.
- \note
- - Resulting pixel values are casted to fit the pixel type \c T.
- For instance, adding \c 0.2 to a \c CImg<char> is possible but does nothing indeed.
- - Overflow values are treated as with standard C++ numeric types. For instance,
- \code
- CImg<unsigned char> img(100,100,1,1,255); // Construct a 100x100 image with pixel values '255'
- img+=1; // Add '1' to each pixels -> Overflow
- // here all pixels of image 'img' are equal to '0'.
- \endcode
- - To prevent value overflow, you may want to consider pixel type \c T as \c float or \c double,
- and use cut() after addition.
- \par Example
- \code
- CImg<unsigned char> img1("reference.jpg"); // Load a 8-bits RGB image (values in [0,255])
- CImg<float> img2(img1); // Construct a float-valued copy of 'img1'
- img2+=100; // Add '100' to pixel values -> goes out of [0,255] but no problems with floats
- img2.cut(0,255); // Cut values in [0,255] to fit the 'unsigned char' constraint
- img1 = img2; // Rewrite safe result in 'unsigned char' version 'img1'
- const CImg<unsigned char> img3 = (img1 + 100).cut(0,255); // Do the same in a more simple and elegant way
- (img1,img2,img3).display();
- \endcode
- \image html ref_operator_plus.jpg
- **/
- template<typename t>
- CImg<T>& operator+=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr + value,524288);
- return *this;
- }
- //! In-place addition operator.
- /**
- Add values to image pixels, according to the specified string \c expression.
- \param expression Value string describing the way pixel values are added.
- \note
- - Similar to operator=(const char*), except that it adds values to the pixels of the current image instance,
- instead of assigning them.
- **/
- CImg<T>& operator+=(const char *const expression) {
- return *this+=(+*this)._fill(expression,true,1,0,0,"operator+=",this);
- }
- //! In-place addition operator.
- /**
- Add values to image pixels, according to the values of the input image \c img.
- \param img Input image to add.
- \note
- - The size of the image instance is never modified.
- - It is not mandatory that input image \c img has the same size as the image instance.
- If less values are available in \c img, then the values are added periodically. For instance, adding one
- WxH scalar image (spectrum() equal to \c 1) to one WxH color image (spectrum() equal to \c 3)
- means each color channel will be incremented with the same values at the same locations.
- \par Example
- \code
- CImg<float> img1("reference.jpg"); // Load a RGB color image (img1.spectrum()==3)
- // Construct a scalar shading (img2.spectrum()==1).
- const CImg<float> img2(img1.width(),img.height(),1,1,"255*(x/w)^2");
- img1+=img2; // Add shading to each channel of 'img1'
- img1.cut(0,255); // Prevent [0,255] overflow
- (img2,img1).display();
- \endcode
- \image html ref_operator_plus1.jpg
- **/
- template<typename t>
- CImg<T>& operator+=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this+=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)(*ptrd + *(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd + *(ptrs++));
- }
- return *this;
- }
- //! In-place increment operator (prefix).
- /**
- Add \c 1 to all image pixels, and return a reference to the current incremented image instance.
- \note
- - Writing \c ++img is equivalent to \c img+=1.
- **/
- CImg<T>& operator++() {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr + 1,524288);
- return *this;
- }
- //! In-place increment operator (postfix).
- /**
- Add \c 1 to all image pixels, and return a new copy of the initial (pre-incremented) image instance.
- \note
- - Use the prefixed version operator++() if you don't need a copy of the initial
- (pre-incremented) image instance, since a useless image copy may be expensive in terms of memory usage.
- **/
- CImg<T> operator++(int) {
- const CImg<T> copy(*this,false);
- ++*this;
- return copy;
- }
- //! Return a non-shared copy of the image instance.
- /**
- \note
- - Use this operator to ensure you get a non-shared copy of an image instance with same pixel type \c T.
- Indeed, the usual copy constructor CImg<T>(const CImg<T>&) returns a shared copy of a shared input image,
- and it may be not desirable to work on a regular copy (e.g. for a resize operation) if you have no
- information about the shared state of the input image.
- - Writing \c (+img) is equivalent to \c CImg<T>(img,false).
- **/
- CImg<T> operator+() const {
- return CImg<T>(*this,false);
- }
- //! Addition operator.
- /**
- Similar to operator+=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator+(const t value) const {
- return CImg<_cimg_Tt>(*this,false)+=value;
- }
- //! Addition operator.
- /**
- Similar to operator+=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- CImg<Tfloat> operator+(const char *const expression) const {
- return CImg<Tfloat>(*this,false)+=expression;
- }
- //! Addition operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator+(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false)+=img;
- }
- //! In-place subtraction operator.
- /**
- Similar to operator+=(const t), except that it performs a subtraction instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator-=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr - value,524288);
- return *this;
- }
- //! In-place subtraction operator.
- /**
- Similar to operator+=(const char*), except that it performs a subtraction instead of an addition.
- **/
- CImg<T>& operator-=(const char *const expression) {
- return *this-=(+*this)._fill(expression,true,1,0,0,"operator-=",this);
- }
- //! In-place subtraction operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a subtraction instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator-=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this-=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)(*ptrd - *(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd - *(ptrs++));
- }
- return *this;
- }
- //! In-place decrement operator (prefix).
- /**
- Similar to operator++(), except that it performs a decrement instead of an increment.
- **/
- CImg<T>& operator--() {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr - 1,524288);
- return *this;
- }
- //! In-place decrement operator (postfix).
- /**
- Similar to operator++(int), except that it performs a decrement instead of an increment.
- **/
- CImg<T> operator--(int) {
- const CImg<T> copy(*this,false);
- --*this;
- return copy;
- }
- //! Replace each pixel by its opposite value.
- /**
- \note
- - If the computed opposite values are out-of-range, they are treated as with standard C++ numeric types.
- For instance, the \c unsigned \c char opposite of \c 1 is \c 255.
- \par Example
- \code
- const CImg<unsigned char>
- img1("reference.jpg"), // Load a RGB color image
- img2 = -img1; // Compute its opposite (in 'unsigned char')
- (img1,img2).display();
- \endcode
- \image html ref_operator_minus.jpg
- **/
- CImg<T> operator-() const {
- return CImg<T>(_width,_height,_depth,_spectrum,(T)0)-=*this;
- }
- //! Subtraction operator.
- /**
- Similar to operator-=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator-(const t value) const {
- return CImg<_cimg_Tt>(*this,false)-=value;
- }
- //! Subtraction operator.
- /**
- Similar to operator-=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- CImg<Tfloat> operator-(const char *const expression) const {
- return CImg<Tfloat>(*this,false)-=expression;
- }
- //! Subtraction operator.
- /**
- Similar to operator-=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator-(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false)-=img;
- }
- //! In-place multiplication operator.
- /**
- Similar to operator+=(const t), except that it performs a multiplication instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator*=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr * value,262144);
- return *this;
- }
- //! In-place multiplication operator.
- /**
- Similar to operator+=(const char*), except that it performs a multiplication instead of an addition.
- **/
- CImg<T>& operator*=(const char *const expression) {
- return mul((+*this)._fill(expression,true,1,0,0,"operator*=",this));
- }
- //! In-place multiplication operator.
- /**
- Replace the image instance by the matrix multiplication between the image instance and the specified matrix
- \c img.
- \param img Second operand of the matrix multiplication.
- \note
- - It does \e not compute a pointwise multiplication between two images. For this purpose, use
- mul(const CImg<t>&) instead.
- - The size of the image instance can be modified by this operator.
- \par Example
- \code
- CImg<float> A(2,2,1,1, 1,2,3,4); // Construct 2x2 matrix A = [1,2;3,4]
- const CImg<float> X(1,2,1,1, 1,2); // Construct 1x2 vector X = [1;2]
- A*=X; // Assign matrix multiplication A*X to 'A'
- // 'A' is now a 1x2 vector whose values are [5;11].
- \endcode
- **/
- template<typename t>
- CImg<T>& operator*=(const CImg<t>& img) {
- return ((*this)*img).move_to(*this);
- }
- //! Multiplication operator.
- /**
- Similar to operator*=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator*(const t value) const {
- return CImg<_cimg_Tt>(*this,false)*=value;
- }
- //! Multiplication operator.
- /**
- Similar to operator*=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- CImg<Tfloat> operator*(const char *const expression) const {
- return CImg<Tfloat>(*this,false)*=expression;
- }
- //! Multiplication operator.
- /**
- Similar to operator*=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator*(const CImg<t>& img) const {
- typedef _cimg_Ttdouble Ttdouble;
- typedef _cimg_Tt Tt;
- if (_width!=img._height || _depth!=1 || _spectrum!=1)
- throw CImgArgumentException(_cimg_instance
- "operator*(): Invalid multiplication of instance by specified "
- "matrix (%u,%u,%u,%u,%p).",
- cimg_instance,
- img._width,img._height,img._depth,img._spectrum,img._data);
- CImg<Tt> res(img._width,_height);
- // Check for common cases to optimize.
- if (img._width==1) { // Matrix * Vector
- if (_height==1) switch (_width) { // Vector^T * Vector
- case 1 :
- res[0] = (Tt)((Ttdouble)_data[0]*img[0]);
- return res;
- case 2 :
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1]);
- return res;
- case 3 :
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
- (Ttdouble)_data[2]*img[2]);
- return res;
- case 4 :
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
- (Ttdouble)_data[2]*img[2] + (Ttdouble)_data[3]*img[3]);
- return res;
- default : {
- Ttdouble val = 0;
- cimg_pragma_openmp(parallel for reduction(+:val) cimg_openmp_if_size(size(),4096))
- cimg_forX(*this,i) val+=(Ttdouble)_data[i]*img[i];
- res[0] = val;
- return res;
- }
- } else if (_height==_width) switch (_width) { // Square_matrix * Vector
- case 2 : // 2x2_matrix * Vector
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1]);
- res[1] = (Tt)((Ttdouble)_data[2]*img[0] + (Ttdouble)_data[3]*img[1]);
- return res;
- case 3 : // 3x3_matrix * Vector
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
- (Ttdouble)_data[2]*img[2]);
- res[1] = (Tt)((Ttdouble)_data[3]*img[0] + (Ttdouble)_data[4]*img[1] +
- (Ttdouble)_data[5]*img[2]);
- res[2] = (Tt)((Ttdouble)_data[6]*img[0] + (Ttdouble)_data[7]*img[1] +
- (Ttdouble)_data[8]*img[2]);
- return res;
- case 4 : // 4x4_matrix * Vector
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[1] +
- (Ttdouble)_data[2]*img[2] + (Ttdouble)_data[3]*img[3]);
- res[1] = (Tt)((Ttdouble)_data[4]*img[0] + (Ttdouble)_data[5]*img[1] +
- (Ttdouble)_data[6]*img[2] + (Ttdouble)_data[7]*img[3]);
- res[2] = (Tt)((Ttdouble)_data[8]*img[0] + (Ttdouble)_data[9]*img[1] +
- (Ttdouble)_data[10]*img[2] + (Ttdouble)_data[11]*img[3]);
- res[3] = (Tt)((Ttdouble)_data[12]*img[0] + (Ttdouble)_data[13]*img[1] +
- (Ttdouble)_data[14]*img[2] + (Ttdouble)_data[15]*img[3]);
- return res;
- }
- } else if (_height==_width) {
- if (img._height==img._width) switch (_width) { // Square_matrix * Square_matrix
- case 2 : // 2x2_matrix * 2x2_matrix
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[2]);
- res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[3]);
- res[2] = (Tt)((Ttdouble)_data[2]*img[0] + (Ttdouble)_data[3]*img[2]);
- res[3] = (Tt)((Ttdouble)_data[2]*img[1] + (Ttdouble)_data[3]*img[3]);
- return res;
- case 3 : // 3x3_matrix * 3x3_matrix
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[3] +
- (Ttdouble)_data[2]*img[6]);
- res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[4] +
- (Ttdouble)_data[2]*img[7]);
- res[2] = (Tt)((Ttdouble)_data[0]*img[2] + (Ttdouble)_data[1]*img[5] +
- (Ttdouble)_data[2]*img[8]);
- res[3] = (Tt)((Ttdouble)_data[3]*img[0] + (Ttdouble)_data[4]*img[3] +
- (Ttdouble)_data[5]*img[6]);
- res[4] = (Tt)((Ttdouble)_data[3]*img[1] + (Ttdouble)_data[4]*img[4] +
- (Ttdouble)_data[5]*img[7]);
- res[5] = (Tt)((Ttdouble)_data[3]*img[2] + (Ttdouble)_data[4]*img[5] +
- (Ttdouble)_data[5]*img[8]);
- res[6] = (Tt)((Ttdouble)_data[6]*img[0] + (Ttdouble)_data[7]*img[3] +
- (Ttdouble)_data[8]*img[6]);
- res[7] = (Tt)((Ttdouble)_data[6]*img[1] + (Ttdouble)_data[7]*img[4] +
- (Ttdouble)_data[8]*img[7]);
- res[8] = (Tt)((Ttdouble)_data[6]*img[2] + (Ttdouble)_data[7]*img[5] +
- (Ttdouble)_data[8]*img[8]);
- return res;
- case 4 : // 4x4_matrix * 4x4_matrix
- res[0] = (Tt)((Ttdouble)_data[0]*img[0] + (Ttdouble)_data[1]*img[4] +
- (Ttdouble)_data[2]*img[8] + (Ttdouble)_data[3]*img[12]);
- res[1] = (Tt)((Ttdouble)_data[0]*img[1] + (Ttdouble)_data[1]*img[5] +
- (Ttdouble)_data[2]*img[9] + (Ttdouble)_data[3]*img[13]);
- res[2] = (Tt)((Ttdouble)_data[0]*img[2] + (Ttdouble)_data[1]*img[6] +
- (Ttdouble)_data[2]*img[10] + (Ttdouble)_data[3]*img[14]);
- res[3] = (Tt)((Ttdouble)_data[0]*img[3] + (Ttdouble)_data[1]*img[7] +
- (Ttdouble)_data[2]*img[11] + (Ttdouble)_data[3]*img[15]);
- res[4] = (Tt)((Ttdouble)_data[4]*img[0] + (Ttdouble)_data[5]*img[4] +
- (Ttdouble)_data[6]*img[8] + (Ttdouble)_data[7]*img[12]);
- res[5] = (Tt)((Ttdouble)_data[4]*img[1] + (Ttdouble)_data[5]*img[5] +
- (Ttdouble)_data[6]*img[9] + (Ttdouble)_data[7]*img[13]);
- res[6] = (Tt)((Ttdouble)_data[4]*img[2] + (Ttdouble)_data[5]*img[6] +
- (Ttdouble)_data[6]*img[10] + (Ttdouble)_data[7]*img[14]);
- res[7] = (Tt)((Ttdouble)_data[4]*img[3] + (Ttdouble)_data[5]*img[7] +
- (Ttdouble)_data[6]*img[11] + (Ttdouble)_data[7]*img[15]);
- res[8] = (Tt)((Ttdouble)_data[8]*img[0] + (Ttdouble)_data[9]*img[4] +
- (Ttdouble)_data[10]*img[8] + (Ttdouble)_data[11]*img[12]);
- res[9] = (Tt)((Ttdouble)_data[8]*img[1] + (Ttdouble)_data[9]*img[5] +
- (Ttdouble)_data[10]*img[9] + (Ttdouble)_data[11]*img[13]);
- res[10] = (Tt)((Ttdouble)_data[8]*img[2] + (Ttdouble)_data[9]*img[6] +
- (Ttdouble)_data[10]*img[10] + (Ttdouble)_data[11]*img[14]);
- res[11] = (Tt)((Ttdouble)_data[8]*img[3] + (Ttdouble)_data[9]*img[7] +
- (Ttdouble)_data[10]*img[11] + (Ttdouble)_data[11]*img[15]);
- res[12] = (Tt)((Ttdouble)_data[12]*img[0] + (Ttdouble)_data[13]*img[4] +
- (Ttdouble)_data[14]*img[8] + (Ttdouble)_data[15]*img[12]);
- res[13] = (Tt)((Ttdouble)_data[12]*img[1] + (Ttdouble)_data[13]*img[5] +
- (Ttdouble)_data[14]*img[9] + (Ttdouble)_data[15]*img[13]);
- res[14] = (Tt)((Ttdouble)_data[12]*img[2] + (Ttdouble)_data[13]*img[6] +
- (Ttdouble)_data[14]*img[10] + (Ttdouble)_data[15]*img[14]);
- res[15] = (Tt)((Ttdouble)_data[12]*img[3] + (Ttdouble)_data[13]*img[7] +
- (Ttdouble)_data[14]*img[11] + (Ttdouble)_data[15]*img[15]);
- return res;
- } else switch (_width) { // Square_matrix * Matrix
- case 2 : { // 2x2_matrix * Matrix
- const t *const ps0 = img.data(), *const ps1 = img.data(0,1);
- Tt *const pd0 = res.data(), *const pd1 = res.data(0,1);
- const Ttdouble
- a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1],
- a2 = (Ttdouble)_data[2], a3 = (Ttdouble)_data[3];
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),4096))
- cimg_forX(img,i) {
- const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i];
- pd0[i] = (Tt)(a0*x + a1*y);
- pd1[i] = (Tt)(a2*x + a3*y);
- }
- return res;
- }
- case 3 : { // 3x3_matrix * Matrix
- const t *const ps0 = img.data(), *const ps1 = img.data(0,1), *const ps2 = img.data(0,2);
- Tt *const pd0 = res.data(), *const pd1 = res.data(0,1), *const pd2 = res.data(0,2);
- const Ttdouble
- a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1], a2 = (Ttdouble)_data[2],
- a3 = (Ttdouble)_data[3], a4 = (Ttdouble)_data[4], a5 = (Ttdouble)_data[5],
- a6 = (Ttdouble)_data[6], a7 = (Ttdouble)_data[7], a8 = (Ttdouble)_data[8];
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),1024))
- cimg_forX(img,i) {
- const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i], z = (Ttdouble)ps2[i];
- pd0[i] = (Tt)(a0*x + a1*y + a2*z);
- pd1[i] = (Tt)(a3*x + a4*y + a5*z);
- pd2[i] = (Tt)(a6*x + a7*y + a8*z);
- }
- return res;
- }
- case 4 : { // 4x4_matrix * Matrix
- const t
- *const ps0 = img.data(), *const ps1 = img.data(0,1),
- *const ps2 = img.data(0,2), *const ps3 = img.data(0,3);
- Tt
- *const pd0 = res.data(), *const pd1 = res.data(0,1),
- *const pd2 = res.data(0,2), *const pd3 = res.data(0,3);
- const Ttdouble
- a0 = (Ttdouble)_data[0], a1 = (Ttdouble)_data[1], a2 = (Ttdouble)_data[2], a3 = (Ttdouble)_data[3],
- a4 = (Ttdouble)_data[4], a5 = (Ttdouble)_data[5], a6 = (Ttdouble)_data[6], a7 = (Ttdouble)_data[7],
- a8 = (Ttdouble)_data[8], a9 = (Ttdouble)_data[9], a10 = (Ttdouble)_data[10], a11 = (Ttdouble)_data[11],
- a12 = (Ttdouble)_data[12], a13 = (Ttdouble)_data[13], a14 = (Ttdouble)_data[14],
- a15 = (Ttdouble)_data[15];
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(img.width(),512))
- cimg_forX(img,i) {
- const Ttdouble x = (Ttdouble)ps0[i], y = (Ttdouble)ps1[i], z = (Ttdouble)ps2[i], c = (Ttdouble)ps3[i];
- pd0[i] = (Tt)(a0*x + a1*y + a2*z + a3*c);
- pd1[i] = (Tt)(a4*x + a5*y + a6*z + a7*c);
- pd2[i] = (Tt)(a8*x + a9*y + a10*z + a11*c);
- pd3[i] = (Tt)(a12*x + a13*y + a14*z + a15*c);
- }
- return res;
- }
- }
- }
- // Fallback to generic version.
- #if cimg_use_openmp!=0
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(size()>(cimg_openmp_sizefactor)*1024 &&
- img.size()>(cimg_openmp_sizefactor)*1024))
- cimg_forXY(res,i,j) {
- Ttdouble value = 0;
- cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k);
- res(i,j) = (Tt)value;
- }
- #else
- Tt *ptrd = res._data;
- cimg_forXY(res,i,j) {
- Ttdouble value = 0;
- cimg_forX(*this,k) value+=(*this)(k,j)*img(i,k);
- *(ptrd++) = (Tt)value;
- }
- #endif
- return res;
- }
- //! In-place division operator.
- /**
- Similar to operator+=(const t), except that it performs a division instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator/=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,*ptr / value,32768);
- return *this;
- }
- //! In-place division operator.
- /**
- Similar to operator+=(const char*), except that it performs a division instead of an addition.
- **/
- CImg<T>& operator/=(const char *const expression) {
- return div((+*this)._fill(expression,true,1,0,0,"operator/=",this));
- }
- //! In-place division operator.
- /**
- Replace the image instance by the (right) matrix division between the image instance and the specified
- matrix \c img.
- \param img Second operand of the matrix division.
- \note
- - It does \e not compute a pointwise division between two images. For this purpose, use
- div(const CImg<t>&) instead.
- - It returns the matrix operation \c A*inverse(img).
- - The size of the image instance can be modified by this operator.
- **/
- template<typename t>
- CImg<T>& operator/=(const CImg<t>& img) {
- return (*this*img.get_invert()).move_to(*this);
- }
- //! Division operator.
- /**
- Similar to operator/=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator/(const t value) const {
- return CImg<_cimg_Tt>(*this,false)/=value;
- }
- //! Division operator.
- /**
- Similar to operator/=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- CImg<Tfloat> operator/(const char *const expression) const {
- return CImg<Tfloat>(*this,false)/=expression;
- }
- //! Division operator.
- /**
- Similar to operator/=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator/(const CImg<t>& img) const {
- return (*this)*img.get_invert();
- }
- //! In-place modulo operator.
- /**
- Similar to operator+=(const t), except that it performs a modulo operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator%=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,cimg::mod(*ptr,(T)value),16384);
- return *this;
- }
- //! In-place modulo operator.
- /**
- Similar to operator+=(const char*), except that it performs a modulo operation instead of an addition.
- **/
- CImg<T>& operator%=(const char *const expression) {
- return *this%=(+*this)._fill(expression,true,1,0,0,"operator%=",this);
- }
- //! In-place modulo operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a modulo operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator%=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this%=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = cimg::mod(*ptrd,(T)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::mod(*ptrd,(T)*(ptrs++));
- }
- return *this;
- }
- //! Modulo operator.
- /**
- Similar to operator%=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator%(const t value) const {
- return CImg<_cimg_Tt>(*this,false)%=value;
- }
- //! Modulo operator.
- /**
- Similar to operator%=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- CImg<Tfloat> operator%(const char *const expression) const {
- return CImg<Tfloat>(*this,false)%=expression;
- }
- //! Modulo operator.
- /**
- Similar to operator%=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image may be a superset of the initial pixel type \c T, if necessary.
- **/
- template<typename t>
- CImg<_cimg_Tt> operator%(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false)%=img;
- }
- //! In-place bitwise AND operator.
- /**
- Similar to operator+=(const t), except that it performs a bitwise AND operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator&=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,(longT)*ptr & (longT)value,32768);
- return *this;
- }
- //! In-place bitwise AND operator.
- /**
- Similar to operator+=(const char*), except that it performs a bitwise AND operation instead of an addition.
- **/
- CImg<T>& operator&=(const char *const expression) {
- return *this&=(+*this)._fill(expression,true,1,0,0,"operator&=",this);
- }
- //! In-place bitwise AND operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a bitwise AND operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator&=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this&=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)((longT)*ptrd & (longT)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd & (longT)*(ptrs++));
- }
- return *this;
- }
- //! Bitwise AND operator.
- /**
- Similar to operator&=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator&(const t value) const {
- return (+*this)&=value;
- }
- //! Bitwise AND operator.
- /**
- Similar to operator&=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- CImg<T> operator&(const char *const expression) const {
- return (+*this)&=expression;
- }
- //! Bitwise AND operator.
- /**
- Similar to operator&=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator&(const CImg<t>& img) const {
- return (+*this)&=img;
- }
- //! In-place bitwise OR operator.
- /**
- Similar to operator+=(const t), except that it performs a bitwise OR operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator|=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,(longT)*ptr | (longT)value,32768);
- return *this;
- }
- //! In-place bitwise OR operator.
- /**
- Similar to operator+=(const char*), except that it performs a bitwise OR operation instead of an addition.
- **/
- CImg<T>& operator|=(const char *const expression) {
- return *this|=(+*this)._fill(expression,true,1,0,0,"operator|=",this);
- }
- //! In-place bitwise OR operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a bitwise OR operation instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator|=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this|=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)((longT)*ptrd | (longT)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd | (longT)*(ptrs++));
- }
- return *this;
- }
- //! Bitwise OR operator.
- /**
- Similar to operator|=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator|(const t value) const {
- return (+*this)|=value;
- }
- //! Bitwise OR operator.
- /**
- Similar to operator|=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- CImg<T> operator|(const char *const expression) const {
- return (+*this)|=expression;
- }
- //! Bitwise OR operator.
- /**
- Similar to operator|=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator|(const CImg<t>& img) const {
- return (+*this)|=img;
- }
- //! In-place bitwise XOR operator.
- /**
- Similar to operator+=(const t), except that it performs a bitwise XOR operation instead of an addition.
- \warning
- - It does \e not compute the \e power of pixel values. For this purpose, use pow(const t) instead.
- **/
- template<typename t>
- CImg<T>& operator^=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,(longT)*ptr ^ (longT)value,32768);
- return *this;
- }
- //! In-place bitwise XOR operator.
- /**
- Similar to operator+=(const char*), except that it performs a bitwise XOR operation instead of an addition.
- \warning
- - It does \e not compute the \e power of pixel values. For this purpose, use pow(const char*) instead.
- **/
- CImg<T>& operator^=(const char *const expression) {
- return *this^=(+*this)._fill(expression,true,1,0,0,"operator^=",this);
- }
- //! In-place bitwise XOR operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a bitwise XOR operation instead of an addition.
- \warning
- - It does \e not compute the \e power of pixel values. For this purpose, use pow(const CImg<t>&) instead.
- **/
- template<typename t>
- CImg<T>& operator^=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this^=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)((longT)*ptrd ^ (longT)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd ^ (longT)*(ptrs++));
- }
- return *this;
- }
- //! Bitwise XOR operator.
- /**
- Similar to operator^=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator^(const t value) const {
- return (+*this)^=value;
- }
- //! Bitwise XOR operator.
- /**
- Similar to operator^=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- CImg<T> operator^(const char *const expression) const {
- return (+*this)^=expression;
- }
- //! Bitwise XOR operator.
- /**
- Similar to operator^=(const CImg<t>&), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator^(const CImg<t>& img) const {
- return (+*this)^=img;
- }
- //! In-place bitwise left shift operator.
- /**
- Similar to operator+=(const t), except that it performs a bitwise left shift instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator<<=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,((longT)*ptr) << (int)value,65536);
- return *this;
- }
- //! In-place bitwise left shift operator.
- /**
- Similar to operator+=(const char*), except that it performs a bitwise left shift instead of an addition.
- **/
- CImg<T>& operator<<=(const char *const expression) {
- return *this<<=(+*this)._fill(expression,true,1,0,0,"operator<<=",this);
- }
- //! In-place bitwise left shift operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a bitwise left shift instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator<<=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this^=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)((longT)*ptrd << (int)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd << (int)*(ptrs++));
- }
- return *this;
- }
- //! Bitwise left shift operator.
- /**
- Similar to operator<<=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator<<(const t value) const {
- return (+*this)<<=value;
- }
- //! Bitwise left shift operator.
- /**
- Similar to operator<<=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- CImg<T> operator<<(const char *const expression) const {
- return (+*this)<<=expression;
- }
- //! Bitwise left shift operator.
- /**
- Similar to operator<<=(const CImg<t>&), except that it returns a new image instance instead of
- operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator<<(const CImg<t>& img) const {
- return (+*this)<<=img;
- }
- //! In-place bitwise right shift operator.
- /**
- Similar to operator+=(const t), except that it performs a bitwise right shift instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator>>=(const t value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,((longT)*ptr) >> (int)value,65536);
- return *this;
- }
- //! In-place bitwise right shift operator.
- /**
- Similar to operator+=(const char*), except that it performs a bitwise right shift instead of an addition.
- **/
- CImg<T>& operator>>=(const char *const expression) {
- return *this>>=(+*this)._fill(expression,true,1,0,0,"operator>>=",this);
- }
- //! In-place bitwise right shift operator.
- /**
- Similar to operator+=(const CImg<t>&), except that it performs a bitwise right shift instead of an addition.
- **/
- template<typename t>
- CImg<T>& operator>>=(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return *this^=+img;
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)((longT)*ptrd >> (int)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)((longT)*ptrd >> (int)*(ptrs++));
- }
- return *this;
- }
- //! Bitwise right shift operator.
- /**
- Similar to operator>>=(const t), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator>>(const t value) const {
- return (+*this)>>=value;
- }
- //! Bitwise right shift operator.
- /**
- Similar to operator>>=(const char*), except that it returns a new image instance instead of operating in-place.
- The pixel type of the returned image is \c T.
- **/
- CImg<T> operator>>(const char *const expression) const {
- return (+*this)>>=expression;
- }
- //! Bitwise right shift operator.
- /**
- Similar to operator>>=(const CImg<t>&), except that it returns a new image instance instead of
- operating in-place.
- The pixel type of the returned image is \c T.
- **/
- template<typename t>
- CImg<T> operator>>(const CImg<t>& img) const {
- return (+*this)>>=img;
- }
- //! Bitwise inversion operator.
- /**
- Similar to operator-(), except that it compute the bitwise inverse instead of the opposite value.
- **/
- CImg<T> operator~() const {
- CImg<T> res(_width,_height,_depth,_spectrum);
- const T *ptrs = _data;
- cimg_for(res,ptrd,T) { const ulongT value = (ulongT)*(ptrs++); *ptrd = (T)~value; }
- return res;
- }
- //! Test if all pixels of an image have the same value.
- /**
- Return \c true is all pixels of the image instance are equal to the specified \c value.
- \param value Reference value to compare with.
- **/
- template<typename t>
- bool operator==(const t value) const {
- if (is_empty()) return false;
- typedef _cimg_Tt Tt;
- bool is_equal = true;
- for (T *ptrd = _data + size(); is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)value)) {}
- return is_equal;
- }
- //! Test if all pixel values of an image follow a specified expression.
- /**
- Return \c true is all pixels of the image instance are equal to the specified \c expression.
- \param expression Value string describing the way pixel values are compared.
- **/
- bool operator==(const char *const expression) const {
- return *this==(+*this)._fill(expression,true,1,0,0,"operator==",this);
- }
- //! Test if two images have the same size and values.
- /**
- Return \c true if the image instance and the input image \c img have the same pixel values,
- even if the dimensions of the two images do not match. It returns \c false otherwise.
- \param img Input image to compare with.
- \note
- - The pixel buffer pointers data() of the two compared images do not have to be the same for operator==()
- to return \c true.
- Only the dimensions and the pixel values matter. Thus, the comparison can be \c true even for different
- pixel types \c T and \c t.
- \par Example
- \code
- const CImg<float> img1(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'float' pixel values)
- const CImg<char> img2(1,3,1,1, 0,1,2); // Construct a 1x3 vector [0;1;2] (with 'char' pixel values)
- if (img1==img2) { // Test succeeds, image dimensions and values are the same
- std::printf("'img1' and 'img2' have same dimensions and values.");
- }
- \endcode
- **/
- template<typename t>
- bool operator==(const CImg<t>& img) const {
- typedef _cimg_Tt Tt;
- const ulongT siz = size();
- bool is_equal = true;
- if (siz!=img.size()) return false;
- t *ptrs = img._data + siz;
- for (T *ptrd = _data + siz; is_equal && ptrd>_data; is_equal = ((Tt)*(--ptrd)==(Tt)*(--ptrs))) {}
- return is_equal;
- }
- //! Test if pixels of an image are all different from a value.
- /**
- Return \c true is all pixels of the image instance are different than the specified \c value.
- \param value Reference value to compare with.
- **/
- template<typename t>
- bool operator!=(const t value) const {
- return !((*this)==value);
- }
- //! Test if all pixel values of an image are different from a specified expression.
- /**
- Return \c true is all pixels of the image instance are different to the specified \c expression.
- \param expression Value string describing the way pixel values are compared.
- **/
- bool operator!=(const char *const expression) const {
- return !((*this)==expression);
- }
- //! Test if two images have different sizes or values.
- /**
- Return \c true if the image instance and the input image \c img have different dimensions or pixel values,
- and \c false otherwise.
- \param img Input image to compare with.
- \note
- - Writing \c img1!=img2 is equivalent to \c !(img1==img2).
- **/
- template<typename t>
- bool operator!=(const CImg<t>& img) const {
- return !((*this)==img);
- }
- //! Construct an image list from two images.
- /**
- Return a new list of image (\c CImgList instance) containing exactly two elements:
- - A copy of the image instance, at position [\c 0].
- - A copy of the specified image \c img, at position [\c 1].
- \param img Input image that will be the second image of the resulting list.
- \note
- - The family of operator,() is convenient to easily create list of images, but it is also \e quite \e slow
- in practice (see warning below).
- - Constructed lists contain no shared images. If image instance or input image \c img are shared, they are
- inserted as new non-shared copies in the resulting list.
- - The pixel type of the returned list may be a superset of the initial pixel type \c T, if necessary.
- \warning
- - Pipelining operator,() \c N times will perform \c N copies of the entire content of a (growing) image list.
- This may become very expensive in terms of speed and used memory. You should avoid using this technique to
- build a new CImgList instance from several images, if you are seeking for performance.
- Fast insertions of images in an image list are possible with
- CImgList<T>::insert(const CImg<t>&,unsigned int,bool) or move_to(CImgList<t>&,unsigned int).
- \par Example
- \code
- const CImg<float>
- img1("reference.jpg"),
- img2 = img1.get_mirror('x'),
- img3 = img2.get_blur(5);
- const CImgList<float> list = (img1,img2); // Create list of two elements from 'img1' and 'img2'
- (list,img3).display(); // Display image list containing copies of 'img1','img2' and 'img3'
- \endcode
- \image html ref_operator_comma.jpg
- **/
- template<typename t>
- CImgList<_cimg_Tt> operator,(const CImg<t>& img) const {
- return CImgList<_cimg_Tt>(*this,img);
- }
- //! Construct an image list from image instance and an input image list.
- /**
- Return a new list of images (\c CImgList instance) containing exactly \c list.size() \c + \c 1 elements:
- - A copy of the image instance, at position [\c 0].
- - A copy of the specified image list \c list, from positions [\c 1] to [\c list.size()].
- \param list Input image list that will be appended to the image instance.
- \note
- - Similar to operator,(const CImg<t>&) const, except that it takes an image list as an argument.
- **/
- template<typename t>
- CImgList<_cimg_Tt> operator,(const CImgList<t>& list) const {
- return CImgList<_cimg_Tt>(list,false).insert(*this,0);
- }
- //! Split image along specified axis.
- /**
- Return a new list of images (\c CImgList instance) containing the split components
- of the instance image along the specified axis.
- \param axis Splitting axis (can be '\c x','\c y','\c z' or '\c c')
- \note
- - Similar to get_split(char,int) const, with default second argument.
- \par Example
- \code
- const CImg<unsigned char> img("reference.jpg"); // Load a RGB color image
- const CImgList<unsigned char> list = (img<'c'); // Get a list of its three R,G,B channels
- (img,list).display();
- \endcode
- \image html ref_operator_less.jpg
- **/
- CImgList<T> operator<(const char axis) const {
- return get_split(axis);
- }
- //@}
- //-------------------------------------
- //
- //! \name Instance Characteristics
- //@{
- //-------------------------------------
- //! Return the type of image pixel values as a C string.
- /**
- Return a \c char* string containing the usual type name of the image pixel values
- (i.e. a stringified version of the template parameter \c T).
- \note
- - The returned string may contain spaces (as in \c "unsigned char").
- - If the pixel type \c T does not correspond to a registered type, the string <tt>"unknown"</tt> is returned.
- **/
- static const char* pixel_type() {
- return cimg::type<T>::string();
- }
- //! Return the number of image columns.
- /**
- Return the image width, i.e. the image dimension along the X-axis.
- \note
- - The width() of an empty image is equal to \c 0.
- - width() is typically equal to \c 1 when considering images as \e vectors for matrix calculations.
- - width() returns an \c int, although the image width is internally stored as an \c unsigned \c int.
- Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
- \c unsigned \c int variables.
- Access to the initial \c unsigned \c int variable is possible (though not recommended) by
- <tt>(*this)._width</tt>.
- **/
- int width() const {
- return (int)_width;
- }
- //! Return the number of image rows.
- /**
- Return the image height, i.e. the image dimension along the Y-axis.
- \note
- - The height() of an empty image is equal to \c 0.
- - height() returns an \c int, although the image height is internally stored as an \c unsigned \c int.
- Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
- \c unsigned \c int variables.
- Access to the initial \c unsigned \c int variable is possible (though not recommended) by
- <tt>(*this)._height</tt>.
- **/
- int height() const {
- return (int)_height;
- }
- //! Return the number of image slices.
- /**
- Return the image depth, i.e. the image dimension along the Z-axis.
- \note
- - The depth() of an empty image is equal to \c 0.
- - depth() is typically equal to \c 1 when considering usual 2D images. When depth()\c > \c 1, the image
- is said to be \e volumetric.
- - depth() returns an \c int, although the image depth is internally stored as an \c unsigned \c int.
- Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
- \c unsigned \c int variables.
- Access to the initial \c unsigned \c int variable is possible (though not recommended) by
- <tt>(*this)._depth</tt>.
- **/
- int depth() const {
- return (int)_depth;
- }
- //! Return the number of image channels.
- /**
- Return the number of image channels, i.e. the image dimension along the C-axis.
- \note
- - The spectrum() of an empty image is equal to \c 0.
- - spectrum() is typically equal to \c 1 when considering scalar-valued images, to \c 3
- for RGB-coded color images, and to \c 4 for RGBA-coded color images (with alpha-channel).
- The number of channels of an image instance is not limited. The meaning of the pixel values is not linked
- up to the number of channels (e.g. a 4-channel image may indifferently stands for a RGBA or CMYK color image).
- - spectrum() returns an \c int, although the image spectrum is internally stored as an \c unsigned \c int.
- Using an \c int is safer and prevents arithmetic traps possibly encountered when doing calculations involving
- \c unsigned \c int variables.
- Access to the initial \c unsigned \c int variable is possible (though not recommended) by
- <tt>(*this)._spectrum</tt>.
- **/
- int spectrum() const {
- return (int)_spectrum;
- }
- //! Return the total number of pixel values.
- /**
- Return <tt>width()*\ref height()*\ref depth()*\ref spectrum()</tt>,
- i.e. the total number of values of type \c T in the pixel buffer of the image instance.
- \note
- - The size() of an empty image is equal to \c 0.
- - The allocated memory size for a pixel buffer of a non-shared \c CImg<T> instance is equal to
- <tt>size()*sizeof(T)</tt>.
- \par Example
- \code
- const CImg<float> img(100,100,1,3); // Construct new 100x100 color image
- if (img.size()==30000) // Test succeeds
- std::printf("Pixel buffer uses %lu bytes",
- img.size()*sizeof(float));
- \endcode
- **/
- ulongT size() const {
- return (ulongT)_width*_height*_depth*_spectrum;
- }
- //! Return a pointer to the first pixel value.
- /**
- Return a \c T*, or a \c const \c T* pointer to the first value in the pixel buffer of the image instance,
- whether the instance is \c const or not.
- \note
- - The data() of an empty image is equal to \c 0 (null pointer).
- - The allocated pixel buffer for the image instance starts from \c data()
- and goes to <tt>data()+\ref size() - 1</tt> (included).
- - To get the pointer to one particular location of the pixel buffer, use
- data(unsigned int,unsigned int,unsigned int,unsigned int) instead.
- **/
- T* data() {
- return _data;
- }
- //! Return a pointer to the first pixel value \const.
- const T* data() const {
- return _data;
- }
- //! Return a pointer to a located pixel value.
- /**
- 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
- of the image instance,
- whether the instance is \c const or not.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Writing \c img.data(x,y,z,c) is equivalent to <tt>&(img(x,y,z,c))</tt>. Thus, this method has the same
- properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int).
- **/
- #if cimg_verbosity>=3
- T *data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) {
- const ulongT off = (ulongT)offset(x,y,z,c);
- if (off>=size())
- cimg::warn(_cimg_instance
- "data(): Invalid pointer request, at coordinates (%u,%u,%u,%u) [offset=%u].",
- cimg_instance,
- x,y,z,c,off);
- return _data + off;
- }
- //! Return a pointer to a located pixel value \const.
- const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const {
- return const_cast<CImg<T>*>(this)->data(x,y,z,c);
- }
- #else
- T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) {
- return _data + x + (ulongT)y*_width + (ulongT)z*_width*_height + (ulongT)c*_width*_height*_depth;
- }
- const T* data(const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int c=0) const {
- return _data + x + (ulongT)y*_width + (ulongT)z*_width*_height + (ulongT)c*_width*_height*_depth;
- }
- #endif
- //! Return the offset to a located pixel value, with respect to the beginning of the pixel buffer.
- /**
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Writing \c img.data(x,y,z,c) is equivalent to <tt>&(img(x,y,z,c)) - img.data()</tt>.
- Thus, this method has the same properties as operator()(unsigned int,unsigned int,unsigned int,unsigned int).
- \par Example
- \code
- const CImg<float> img(100,100,1,3); // Define a 100x100 RGB-color image
- const long off = img.offset(10,10,0,2); // Get the offset of the blue value of the pixel located at (10,10)
- const float val = img[off]; // Get the blue value of this pixel
- \endcode
- **/
- longT offset(const int x, const int y=0, const int z=0, const int c=0) const {
- return x + (longT)y*_width + (longT)z*_width*_height + (longT)c*_width*_height*_depth;
- }
- //! Return a CImg<T>::iterator pointing to the first pixel value.
- /**
- \note
- - Equivalent to data().
- - It has been mainly defined for compatibility with STL naming conventions.
- **/
- iterator begin() {
- return _data;
- }
- //! Return a CImg<T>::iterator pointing to the first value of the pixel buffer \const.
- const_iterator begin() const {
- return _data;
- }
- //! Return a CImg<T>::iterator pointing next to the last pixel value.
- /**
- \note
- - Writing \c img.end() is equivalent to <tt>img.data() + img.size()</tt>.
- - It has been mainly defined for compatibility with STL naming conventions.
- \warning
- - The returned iterator actually points to a value located \e outside the acceptable bounds of the pixel buffer.
- Trying to read or write the content of the returned iterator will probably result in a crash.
- Use it mainly as a strict upper bound for a CImg<T>::iterator.
- \par Example
- \code
- CImg<float> img(100,100,1,3); // Define a 100x100 RGB color image
- // 'img.end()' used below as an upper bound for the iterator.
- for (CImg<float>::iterator it = img.begin(); it<img.end(); ++it)
- *it = 0;
- \endcode
- **/
- iterator end() {
- return _data + size();
- }
- //! Return a CImg<T>::iterator pointing next to the last pixel value \const.
- const_iterator end() const {
- return _data + size();
- }
- //! Return a reference to the first pixel value.
- /**
- \note
- - Writing \c img.front() is equivalent to <tt>img[0]</tt>, or <tt>img(0,0,0,0)</tt>.
- - It has been mainly defined for compatibility with STL naming conventions.
- **/
- T& front() {
- return *_data;
- }
- //! Return a reference to the first pixel value \const.
- const T& front() const {
- return *_data;
- }
- //! Return a reference to the last pixel value.
- /**
- \note
- - Writing \c img.back() is equivalent to <tt>img[img.size() - 1]</tt>, or
- <tt>img(img.width() - 1,img.height() - 1,img.depth() - 1,img.spectrum() - 1)</tt>.
- - It has been mainly defined for compatibility with STL naming conventions.
- **/
- T& back() {
- return *(_data + size() - 1);
- }
- //! Return a reference to the last pixel value \const.
- const T& back() const {
- return *(_data + size() - 1);
- }
- //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions.
- /**
- Return a reference to the pixel value of the image instance located at a specified \c offset,
- or to a specified default value in case of out-of-bounds access.
- \param offset Offset to the desired pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note
- - Writing \c img.at(offset,out_value) is similar to <tt>img[offset]</tt>, except that if \c offset
- is outside bounds (e.g. \c offset<0 or \c offset>=img.size()), a reference to a value \c out_value
- is safely returned instead.
- - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
- you are \e not sure about the validity of the specified pixel offset.
- **/
- T& at(const int offset, const T& out_value) {
- return (offset<0 || offset>=(int)size())?(cimg::temporary(out_value)=out_value):(*this)[offset];
- }
- //! Access to a pixel value at a specified offset, using Dirichlet boundary conditions \const.
- T at(const int offset, const T& out_value) const {
- return (offset<0 || offset>=(int)size())?out_value:(*this)[offset];
- }
- //! Access to a pixel value at a specified offset, using Neumann boundary conditions.
- /**
- Return a reference to the pixel value of the image instance located at a specified \c offset,
- or to the nearest pixel location in the image instance in case of out-of-bounds access.
- \param offset Offset to the desired pixel value.
- \note
- - Similar to at(int,const T), except that an out-of-bounds access returns the value of the
- nearest pixel in the image instance, regarding the specified offset, i.e.
- - If \c offset<0, then \c img[0] is returned.
- - If \c offset>=img.size(), then \c img[img.size() - 1] is returned.
- - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
- you are \e not sure about the validity of the specified pixel offset.
- - If you know your image instance is \e not empty, you may rather use the slightly faster method \c _at(int).
- **/
- T& at(const int offset) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "at(): Empty instance.",
- cimg_instance);
- return _at(offset);
- }
- T& _at(const int offset) {
- const unsigned int siz = (unsigned int)size();
- return (*this)[offset<0?0:(unsigned int)offset>=siz?siz - 1:offset];
- }
- //! Access to a pixel value at a specified offset, using Neumann boundary conditions \const.
- const T& at(const int offset) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "at(): Empty instance.",
- cimg_instance);
- return _at(offset);
- }
- const T& _at(const int offset) const {
- const unsigned int siz = (unsigned int)size();
- return (*this)[offset<0?0:(unsigned int)offset>=siz?siz - 1:offset];
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate.
- /**
- Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c),
- or to a specified default value in case of out-of-bounds access along the X-axis.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c (\c x,\c y,\c z,\c c) is outside image bounds.
- \note
- - Similar to operator()(), except that an out-of-bounds access along the X-axis returns the specified value
- \c out_value.
- - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
- you are \e not sure about the validity of the specified pixel coordinates.
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- T& atX(const int x, const int y, const int z, const int c, const T& out_value) {
- return (x<0 || x>=width())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X-coordinate \const.
- T atX(const int x, const int y, const int z, const int c, const T& out_value) const {
- return (x<0 || x>=width())?out_value:(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate.
- /**
- Return a reference to the pixel value of the image instance located at (\c x,\c y,\c z,\c c),
- or to the nearest pixel location in the image instance in case of out-of-bounds access along the X-axis.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Similar to at(int,int,int,int,const T), except that an out-of-bounds access returns the value of the
- nearest pixel in the image instance, regarding the specified X-coordinate.
- - Due to the additional boundary checking operation, this method is slower than operator()(). Use it when
- you are \e not sure about the validity of the specified pixel coordinates.
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _at(int,int,int,int).
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- T& atX(const int x, const int y=0, const int z=0, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atX(): Empty instance.",
- cimg_instance);
- return _atX(x,y,z,c);
- }
- T& _atX(const int x, const int y=0, const int z=0, const int c=0) {
- return (*this)(x<0?0:(x>=width()?width() - 1:x),y,z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X-coordinate \const.
- const T& atX(const int x, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atX(): Empty instance.",
- cimg_instance);
- return _atX(x,y,z,c);
- }
- const T& _atX(const int x, const int y=0, const int z=0, const int c=0) const {
- return (*this)(x<0?0:(x>=width()?width() - 1:x),y,z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y-coordinates.
- /**
- Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on X and Y-coordinates.
- **/
- T& atXY(const int x, const int y, const int z, const int c, const T& out_value) {
- return (x<0 || y<0 || x>=width() || y>=height())?(cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X and Y coordinates \const.
- T atXY(const int x, const int y, const int z, const int c, const T& out_value) const {
- return (x<0 || y<0 || x>=width() || y>=height())?out_value:(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates.
- /**
- Similar to atX(int,int,int,int), except that boundary checking is performed both on X and Y-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _atXY(int,int,int,int).
- **/
- T& atXY(const int x, const int y, const int z=0, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXY(): Empty instance.",
- cimg_instance);
- return _atXY(x,y,z,c);
- }
- T& _atXY(const int x, const int y, const int z=0, const int c=0) {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X and Y-coordinates \const.
- const T& atXY(const int x, const int y, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXY(): Empty instance.",
- cimg_instance);
- return _atXY(x,y,z,c);
- }
- const T& _atXY(const int x, const int y, const int z=0, const int c=0) const {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to atX(int,int,int,int,const T), except that boundary checking is performed both on
- X,Y and Z-coordinates.
- **/
- T& atXYZ(const int x, const int y, const int z, const int c, const T& out_value) {
- return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?
- (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions for the X,Y and Z-coordinates \const.
- T atXYZ(const int x, const int y, const int z, const int c, const T& out_value) const {
- return (x<0 || y<0 || z<0 || x>=width() || y>=height() || z>=depth())?out_value:(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to atX(int,int,int,int), except that boundary checking is performed both on X,Y and Z-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _atXYZ(int,int,int,int).
- **/
- T& atXYZ(const int x, const int y, const int z, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXYZ(): Empty instance.",
- cimg_instance);
- return _atXYZ(x,y,z,c);
- }
- T& _atXYZ(const int x, const int y, const int z, const int c=0) {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),
- cimg::cut(z,0,depth() - 1),c);
- }
- //! Access to a pixel value, using Neumann boundary conditions for the X,Y and Z-coordinates \const.
- const T& atXYZ(const int x, const int y, const int z, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXYZ(): Empty instance.",
- cimg_instance);
- return _atXYZ(x,y,z,c);
- }
- const T& _atXYZ(const int x, const int y, const int z, const int c=0) const {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),
- cimg::cut(z,0,depth() - 1),c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions.
- /**
- Similar to atX(int,int,int,int,const T), except that boundary checking is performed on all
- X,Y,Z and C-coordinates.
- **/
- T& atXYZC(const int x, const int y, const int z, const int c, const T& out_value) {
- return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?
- (cimg::temporary(out_value)=out_value):(*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Dirichlet boundary conditions \const.
- T atXYZC(const int x, const int y, const int z, const int c, const T& out_value) const {
- return (x<0 || y<0 || z<0 || c<0 || x>=width() || y>=height() || z>=depth() || c>=spectrum())?out_value:
- (*this)(x,y,z,c);
- }
- //! Access to a pixel value, using Neumann boundary conditions.
- /**
- Similar to atX(int,int,int,int), except that boundary checking is performed on all X,Y,Z and C-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _atXYZC(int,int,int,int).
- **/
- T& atXYZC(const int x, const int y, const int z, const int c) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXYZC(): Empty instance.",
- cimg_instance);
- return _atXYZC(x,y,z,c);
- }
- T& _atXYZC(const int x, const int y, const int z, const int c) {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),
- cimg::cut(z,0,depth() - 1),
- cimg::cut(c,0,spectrum() - 1));
- }
- //! Access to a pixel value, using Neumann boundary conditions \const.
- const T& atXYZC(const int x, const int y, const int z, const int c) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "atXYZC(): Empty instance.",
- cimg_instance);
- return _atXYZC(x,y,z,c);
- }
- const T& _atXYZC(const int x, const int y, const int z, const int c) const {
- return (*this)(cimg::cut(x,0,width() - 1),
- cimg::cut(y,0,height() - 1),
- cimg::cut(z,0,depth() - 1),
- cimg::cut(c,0,spectrum() - 1));
- }
- //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X-coordinate.
- /**
- Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
- or a specified default value in case of out-of-bounds access along the X-axis.
- \param fx X-coordinate of the pixel value (float-valued).
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds.
- \note
- - Similar to atX(int,int,int,int,const T), except that the returned pixel value is approximated by
- a linear interpolation along the X-axis, if corresponding coordinates are not integers.
- - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued.
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- Tfloat linear_atX(const float fx, const int y, const int z, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1;
- const float
- dx = fx - x;
- const Tfloat
- Ic = (Tfloat)atX(x,y,z,c,out_value), In = (Tfloat)atXY(nx,y,z,c,out_value);
- return Ic + dx*(In - Ic);
- }
- //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X-coordinate.
- /**
- Return a linearly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
- or the value of the nearest pixel location in the image instance in case of out-of-bounds access along
- the X-axis.
- \param fx X-coordinate of the pixel value (float-valued).
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Similar to linear_atX(float,int,int,int,const T) const, except that an out-of-bounds access returns
- the value of the nearest pixel in the image instance, regarding the specified X-coordinate.
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _linear_atX(float,int,int,int).
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- Tfloat linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atX(): Empty instance.",
- cimg_instance);
- return _linear_atX(fx,y,z,c);
- }
- Tfloat _linear_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
- const float
- nfx = cimg::cut(fx,0,width() - 1);
- const unsigned int
- x = (unsigned int)nfx;
- const float
- dx = nfx - x;
- const unsigned int
- nx = dx>0?x + 1:x;
- const Tfloat
- Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c);
- return Ic + dx*(In - Ic);
- }
- //! Return pixel value, using linear interpolation and periodic boundary conditions for the X-coordinate.
- Tfloat linear_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atX_p(): Empty instance.",
- cimg_instance);
- return _linear_atX_p(fx,y,z,c);
- }
- Tfloat _linear_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
- const float
- nfx = cimg::mod(fx,_width - 0.5f);
- const unsigned int
- x = (unsigned int)nfx;
- const float
- dx = nfx - x;
- const unsigned int
- nx = cimg::mod(x + 1,_width);
- const Tfloat
- Ic = (Tfloat)(*this)(x,y,z,c), In = (Tfloat)(*this)(nx,y,z,c);
- return Ic + dx*(In - Ic);
- }
- //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X and Y-coordinates.
- /**
- Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
- boundary checking are achieved both for X and Y-coordinates.
- **/
- Tfloat linear_atXY(const float fx, const float fy, const int z, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1,
- y = (int)fy - (fy>=0?0:1), ny = y + 1;
- const float
- dx = fx - x,
- dy = fy - y;
- const Tfloat
- Icc = (Tfloat)atXY(x,y,z,c,out_value), Inc = (Tfloat)atXY(nx,y,z,c,out_value),
- Icn = (Tfloat)atXY(x,ny,z,c,out_value), Inn = (Tfloat)atXY(nx,ny,z,c,out_value);
- return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
- }
- //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X and Y-coordinates.
- /**
- Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
- are achieved both for X and Y-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _linear_atXY(float,float,int,int).
- **/
- Tfloat linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXY(): Empty instance.",
- cimg_instance);
- return _linear_atXY(fx,fy,z,c);
- }
- Tfloat _linear_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
- const float
- nfx = cimg::cut(fx,0,width() - 1),
- nfy = cimg::cut(fy,0,height() - 1);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy;
- const float
- dx = nfx - x,
- dy = nfy - y;
- const unsigned int
- nx = dx>0?x + 1:x,
- ny = dy>0?y + 1:y;
- const Tfloat
- Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
- Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c);
- return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
- }
- //! Return pixel value, using linear interpolation and periodic boundary conditions for the X and Y-coordinates.
- Tfloat linear_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXY_p(): Empty instance.",
- cimg_instance);
- return _linear_atXY_p(fx,fy,z,c);
- }
- Tfloat _linear_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
- const float
- nfx = cimg::mod(fx,_width - 0.5f),
- nfy = cimg::mod(fy,_height - 0.5f);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy;
- const float
- dx = nfx - x,
- dy = nfy - y;
- const unsigned int
- nx = cimg::mod(x + 1,_width),
- ny = cimg::mod(y + 1,_height);
- const Tfloat
- Icc = (Tfloat)(*this)(x,y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
- Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c);
- return Icc + (Inc - Icc + (Icc + Inn - Icn - Inc)*dy)*dx + (Icn - Icc)*dy;
- }
- //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
- boundary checking are achieved both for X,Y and Z-coordinates.
- **/
- Tfloat linear_atXYZ(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1,
- y = (int)fy - (fy>=0?0:1), ny = y + 1,
- z = (int)fz - (fz>=0?0:1), nz = z + 1;
- const float
- dx = fx - x,
- dy = fy - y,
- dz = fz - z;
- const Tfloat
- Iccc = (Tfloat)atXYZ(x,y,z,c,out_value), Incc = (Tfloat)atXYZ(nx,y,z,c,out_value),
- Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value), Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value),
- Iccn = (Tfloat)atXYZ(x,y,nz,c,out_value), Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value),
- Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value), Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value);
- return Iccc +
- (Incc - Iccc +
- (Iccc + Innc - Icnc - Incc +
- (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
- (Iccc + Incn - Iccn - Incc)*dz)*dx +
- (Icnc - Iccc +
- (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
- (Iccn - Iccc)*dz;
- }
- //! Return pixel value, using linear interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
- are achieved both for X,Y and Z-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _linear_atXYZ(float,float,float,int).
- **/
- Tfloat linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXYZ(): Empty instance.",
- cimg_instance);
- return _linear_atXYZ(fx,fy,fz,c);
- }
- Tfloat _linear_atXYZ(const float fx, const float fy=0, const float fz=0, const int c=0) const {
- const float
- nfx = cimg::cut(fx,0,width() - 1),
- nfy = cimg::cut(fy,0,height() - 1),
- nfz = cimg::cut(fz,0,depth() - 1);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy,
- z = (unsigned int)nfz;
- const float
- dx = nfx - x,
- dy = nfy - y,
- dz = nfz - z;
- const unsigned int
- nx = dx>0?x + 1:x,
- ny = dy>0?y + 1:y,
- nz = dz>0?z + 1:z;
- const Tfloat
- Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c),
- Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c),
- Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c),
- Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c);
- return Iccc +
- (Incc - Iccc +
- (Iccc + Innc - Icnc - Incc +
- (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
- (Iccc + Incn - Iccn - Incc)*dz)*dx +
- (Icnc - Iccc +
- (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
- (Iccn - Iccc)*dz;
- }
- //! Return pixel value, using linear interpolation and periodic boundary conditions for the X,Y and Z-coordinates.
- Tfloat linear_atXYZ_p(const float fx, const float fy=0, const float fz=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXYZ_p(): Empty instance.",
- cimg_instance);
- return _linear_atXYZ_p(fx,fy,fz,c);
- }
- Tfloat _linear_atXYZ_p(const float fx, const float fy=0, const float fz=0, const int c=0) const {
- const float
- nfx = cimg::mod(fx,_width - 0.5f),
- nfy = cimg::mod(fy,_height - 0.5f),
- nfz = cimg::mod(fz,_depth - 0.5f);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy,
- z = (unsigned int)nfz;
- const float
- dx = nfx - x,
- dy = nfy - y,
- dz = nfz - z;
- const unsigned int
- nx = cimg::mod(x + 1,_width),
- ny = cimg::mod(y + 1,_height),
- nz = cimg::mod(z + 1,_depth);
- const Tfloat
- Iccc = (Tfloat)(*this)(x,y,z,c), Incc = (Tfloat)(*this)(nx,y,z,c),
- Icnc = (Tfloat)(*this)(x,ny,z,c), Innc = (Tfloat)(*this)(nx,ny,z,c),
- Iccn = (Tfloat)(*this)(x,y,nz,c), Incn = (Tfloat)(*this)(nx,y,nz,c),
- Icnn = (Tfloat)(*this)(x,ny,nz,c), Innn = (Tfloat)(*this)(nx,ny,nz,c);
- return Iccc +
- (Incc - Iccc +
- (Iccc + Innc - Icnc - Incc +
- (Iccn + Innn + Icnc + Incc - Icnn - Incn - Iccc - Innc)*dz)*dy +
- (Iccc + Incn - Iccn - Incc)*dz)*dx +
- (Icnc - Iccc +
- (Iccc + Icnn - Iccn - Icnc)*dz)*dy +
- (Iccn - Iccc)*dz;
- }
- //! Return pixel value, using linear interpolation and Dirichlet boundary conditions for all X,Y,Z,C-coordinates.
- /**
- Similar to linear_atX(float,int,int,int,const T) const, except that the linear interpolation and the
- boundary checking are achieved for all X,Y,Z and C-coordinates.
- **/
- Tfloat linear_atXYZC(const float fx, const float fy, const float fz, const float fc, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1,
- y = (int)fy - (fy>=0?0:1), ny = y + 1,
- z = (int)fz - (fz>=0?0:1), nz = z + 1,
- c = (int)fc - (fc>=0?0:1), nc = c + 1;
- const float
- dx = fx - x,
- dy = fy - y,
- dz = fz - z,
- dc = fc - c;
- const Tfloat
- Icccc = (Tfloat)atXYZC(x,y,z,c,out_value), Inccc = (Tfloat)atXYZC(nx,y,z,c,out_value),
- Icncc = (Tfloat)atXYZC(x,ny,z,c,out_value), Inncc = (Tfloat)atXYZC(nx,ny,z,c,out_value),
- Iccnc = (Tfloat)atXYZC(x,y,nz,c,out_value), Incnc = (Tfloat)atXYZC(nx,y,nz,c,out_value),
- Icnnc = (Tfloat)atXYZC(x,ny,nz,c,out_value), Innnc = (Tfloat)atXYZC(nx,ny,nz,c,out_value),
- Icccn = (Tfloat)atXYZC(x,y,z,nc,out_value), Inccn = (Tfloat)atXYZC(nx,y,z,nc,out_value),
- Icncn = (Tfloat)atXYZC(x,ny,z,nc,out_value), Inncn = (Tfloat)atXYZC(nx,ny,z,nc,out_value),
- Iccnn = (Tfloat)atXYZC(x,y,nz,nc,out_value), Incnn = (Tfloat)atXYZC(nx,y,nz,nc,out_value),
- Icnnn = (Tfloat)atXYZC(x,ny,nz,nc,out_value), Innnn = (Tfloat)atXYZC(nx,ny,nz,nc,out_value);
- return Icccc +
- dx*(Inccc - Icccc +
- dy*(Icccc + Inncc - Icncc - Inccc +
- dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
- dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
- Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
- dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
- dz*(Icccc + Incnc - Iccnc - Inccc +
- dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
- dc*(Icccc + Inccn - Inccc - Icccn)) +
- dy*(Icncc - Icccc +
- dz*(Icccc + Icnnc - Iccnc - Icncc +
- dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
- dc*(Icccc + Icncn - Icncc - Icccn)) +
- dz*(Iccnc - Icccc +
- dc*(Icccc + Iccnn - Iccnc - Icccn)) +
- dc*(Icccn -Icccc);
- }
- //! Return pixel value, using linear interpolation and Neumann boundary conditions for all X,Y,Z and C-coordinates.
- /**
- Similar to linear_atX(float,int,int,int) const, except that the linear interpolation and the boundary checking
- are achieved for all X,Y,Z and C-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _linear_atXYZC(float,float,float,float).
- **/
- Tfloat linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXYZC(): Empty instance.",
- cimg_instance);
- return _linear_atXYZC(fx,fy,fz,fc);
- }
- Tfloat _linear_atXYZC(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
- const float
- nfx = cimg::cut(fx,0,width() - 1),
- nfy = cimg::cut(fy,0,height() - 1),
- nfz = cimg::cut(fz,0,depth() - 1),
- nfc = cimg::cut(fc,0,spectrum() - 1);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy,
- z = (unsigned int)nfz,
- c = (unsigned int)nfc;
- const float
- dx = nfx - x,
- dy = nfy - y,
- dz = nfz - z,
- dc = nfc - c;
- const unsigned int
- nx = dx>0?x + 1:x,
- ny = dy>0?y + 1:y,
- nz = dz>0?z + 1:z,
- nc = dc>0?c + 1:c;
- const Tfloat
- Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c),
- Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c),
- Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c),
- Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c),
- Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc),
- Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc),
- Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc),
- Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc);
- return Icccc +
- dx*(Inccc - Icccc +
- dy*(Icccc + Inncc - Icncc - Inccc +
- dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
- dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
- Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
- dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
- dz*(Icccc + Incnc - Iccnc - Inccc +
- dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
- dc*(Icccc + Inccn - Inccc - Icccn)) +
- dy*(Icncc - Icccc +
- dz*(Icccc + Icnnc - Iccnc - Icncc +
- dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
- dc*(Icccc + Icncn - Icncc - Icccn)) +
- dz*(Iccnc - Icccc +
- dc*(Icccc + Iccnn - Iccnc - Icccn)) +
- dc*(Icccn - Icccc);
- }
- //! Return pixel value, using linear interpolation and periodic boundary conditions for all X,Y,Z and C-coordinates.
- Tfloat linear_atXYZC_p(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "linear_atXYZC_p(): Empty instance.",
- cimg_instance);
- return _linear_atXYZC_p(fx,fy,fz,fc);
- }
- Tfloat _linear_atXYZC_p(const float fx, const float fy=0, const float fz=0, const float fc=0) const {
- const float
- nfx = cimg::mod(fx,_width - 0.5f),
- nfy = cimg::mod(fy,_height - 0.5f),
- nfz = cimg::mod(fz,_depth - 0.5f),
- nfc = cimg::mod(fc,_spectrum - 0.5f);
- const unsigned int
- x = (unsigned int)nfx,
- y = (unsigned int)nfy,
- z = (unsigned int)nfz,
- c = (unsigned int)nfc;
- const float
- dx = nfx - x,
- dy = nfy - y,
- dz = nfz - z,
- dc = nfc - c;
- const unsigned int
- nx = cimg::mod(x + 1,_width),
- ny = cimg::mod(y + 1,_height),
- nz = cimg::mod(z + 1,_depth),
- nc = cimg::mod(c + 1,_spectrum);
- const Tfloat
- Icccc = (Tfloat)(*this)(x,y,z,c), Inccc = (Tfloat)(*this)(nx,y,z,c),
- Icncc = (Tfloat)(*this)(x,ny,z,c), Inncc = (Tfloat)(*this)(nx,ny,z,c),
- Iccnc = (Tfloat)(*this)(x,y,nz,c), Incnc = (Tfloat)(*this)(nx,y,nz,c),
- Icnnc = (Tfloat)(*this)(x,ny,nz,c), Innnc = (Tfloat)(*this)(nx,ny,nz,c),
- Icccn = (Tfloat)(*this)(x,y,z,nc), Inccn = (Tfloat)(*this)(nx,y,z,nc),
- Icncn = (Tfloat)(*this)(x,ny,z,nc), Inncn = (Tfloat)(*this)(nx,ny,z,nc),
- Iccnn = (Tfloat)(*this)(x,y,nz,nc), Incnn = (Tfloat)(*this)(nx,y,nz,nc),
- Icnnn = (Tfloat)(*this)(x,ny,nz,nc), Innnn = (Tfloat)(*this)(nx,ny,nz,nc);
- return Icccc +
- dx*(Inccc - Icccc +
- dy*(Icccc + Inncc - Icncc - Inccc +
- dz*(Iccnc + Innnc + Icncc + Inccc - Icnnc - Incnc - Icccc - Inncc +
- dc*(Iccnn + Innnn + Icncn + Inccn + Icnnc + Incnc + Icccc + Inncc -
- Icnnn - Incnn - Icccn - Inncn - Iccnc - Innnc - Icncc - Inccc)) +
- dc*(Icccn + Inncn + Icncc + Inccc - Icncn - Inccn - Icccc - Inncc)) +
- dz*(Icccc + Incnc - Iccnc - Inccc +
- dc*(Icccn + Incnn + Iccnc + Inccc - Iccnn - Inccn - Icccc - Incnc)) +
- dc*(Icccc + Inccn - Inccc - Icccn)) +
- dy*(Icncc - Icccc +
- dz*(Icccc + Icnnc - Iccnc - Icncc +
- dc*(Icccn + Icnnn + Iccnc + Icncc - Iccnn - Icncn - Icccc - Icnnc)) +
- dc*(Icccc + Icncn - Icncc - Icccn)) +
- dz*(Iccnc - Icccc +
- dc*(Icccc + Iccnn - Iccnc - Icccn)) +
- dc*(Icccn - Icccc);
- }
- //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate.
- /**
- Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
- or a specified default value in case of out-of-bounds access along the X-axis.
- The cubic interpolation uses Hermite splines.
- \param fx d X-coordinate of the pixel value (float-valued).
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c (\c fx,\c y,\c z,\c c) is outside image bounds.
- \note
- - Similar to linear_atX(float,int,int,int,const T) const, except that the returned pixel value is
- approximated by a \e cubic interpolation along the X-axis.
- - The type of the returned pixel value is extended to \c float, if the pixel type \c T is not float-valued.
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- Tfloat cubic_atX(const float fx, const int y, const int z, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2;
- const float
- dx = fx - x;
- const Tfloat
- Ip = (Tfloat)atX(px,y,z,c,out_value), Ic = (Tfloat)atX(x,y,z,c,out_value),
- In = (Tfloat)atX(nx,y,z,c,out_value), Ia = (Tfloat)atX(ax,y,z,c,out_value);
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X-coordinate.
- /**
- Similar to cubic_atX(float,int,int,int,const T) const, except that the return value is clamped to stay in the
- min/max range of the datatype \c T.
- **/
- T cubic_atX_c(const float fx, const int y, const int z, const int c, const T& out_value) const {
- return cimg::type<T>::cut(cubic_atX(fx,y,z,c,out_value));
- }
- //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate.
- /**
- Return a cubicly-interpolated pixel value of the image instance located at (\c fx,\c y,\c z,\c c),
- or the value of the nearest pixel location in the image instance in case of out-of-bounds access
- along the X-axis. The cubic interpolation uses Hermite splines.
- \param fx X-coordinate of the pixel value (float-valued).
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Similar to cubic_atX(float,int,int,int,const T) const, except that the returned pixel value is
- approximated by a cubic interpolation along the X-axis.
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _cubic_atX(float,int,int,int).
- \warning
- - There is \e no boundary checking performed for the Y,Z and C-coordinates, so they must be inside image bounds.
- **/
- Tfloat cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atX(): Empty instance.",
- cimg_instance);
- return _cubic_atX(fx,y,z,c);
- }
- Tfloat _cubic_atX(const float fx, const int y=0, const int z=0, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1);
- const int
- x = (int)nfx;
- const float
- dx = nfx - x;
- const int
- px = x - 1<0?0:x - 1, nx = dx>0?x + 1:x, ax = x + 2>=width()?width() - 1:x + 2;
- const Tfloat
- Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c),
- In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c);
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the X-coordinate.
- /**
- Similar to cubic_atX(float,int,int,int) const, except that the return value is clamped to stay in the
- min/max range of the datatype \c T.
- **/
- T cubic_atX_c(const float fx, const int y, const int z, const int c) const {
- return cimg::type<T>::cut(cubic_atX(fx,y,z,c));
- }
- T _cubic_atX_c(const float fx, const int y, const int z, const int c) const {
- return cimg::type<T>::cut(_cubic_atX(fx,y,z,c));
- }
- //! Return pixel value, using cubic interpolation and periodic boundary conditions for the X-coordinate.
- Tfloat cubic_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atX_p(): Empty instance.",
- cimg_instance);
- return _cubic_atX_p(fx,y,z,c);
- }
- Tfloat _cubic_atX_p(const float fx, const int y=0, const int z=0, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f);
- const int
- x = (int)nfx;
- const float
- dx = nfx - x;
- const int
- px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width());
- const Tfloat
- Ip = (Tfloat)(*this)(px,y,z,c), Ic = (Tfloat)(*this)(x,y,z,c),
- In = (Tfloat)(*this)(nx,y,z,c), Ia = (Tfloat)(*this)(ax,y,z,c);
- 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));
- }
- T cubic_atX_pc(const float fx, const int y, const int z, const int c) const {
- return cimg::type<T>::cut(cubic_atX_p(fx,y,z,c));
- }
- T _cubic_atX_pc(const float fx, const int y, const int z, const int c) const {
- return cimg::type<T>::cut(_cubic_atX_p(fx,y,z,c));
- }
- //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X and Y-coordinates.
- /**
- Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking
- are achieved both for X and Y-coordinates.
- **/
- Tfloat cubic_atXY(const float fx, const float fy, const int z, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2,
- y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2;
- const float dx = fx - x, dy = fy - y;
- const Tfloat
- Ipp = (Tfloat)atXY(px,py,z,c,out_value), Icp = (Tfloat)atXY(x,py,z,c,out_value),
- Inp = (Tfloat)atXY(nx,py,z,c,out_value), Iap = (Tfloat)atXY(ax,py,z,c,out_value),
- 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)),
- Ipc = (Tfloat)atXY(px,y,z,c,out_value), Icc = (Tfloat)atXY(x, y,z,c,out_value),
- Inc = (Tfloat)atXY(nx,y,z,c,out_value), Iac = (Tfloat)atXY(ax,y,z,c,out_value),
- 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)),
- Ipn = (Tfloat)atXY(px,ny,z,c,out_value), Icn = (Tfloat)atXY(x,ny,z,c,out_value),
- Inn = (Tfloat)atXY(nx,ny,z,c,out_value), Ian = (Tfloat)atXY(ax,ny,z,c,out_value),
- 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)),
- Ipa = (Tfloat)atXY(px,ay,z,c,out_value), Ica = (Tfloat)atXY(x,ay,z,c,out_value),
- Ina = (Tfloat)atXY(nx,ay,z,c,out_value), Iaa = (Tfloat)atXY(ax,ay,z,c,out_value),
- 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));
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y-coordinates.
- /**
- Similar to cubic_atXY(float,float,int,int,const T) const, except that the return value is clamped to stay in the
- min/max range of the datatype \c T.
- **/
- T cubic_atXY_c(const float fx, const float fy, const int z, const int c, const T& out_value) const {
- return cimg::type<T>::cut(cubic_atXY(fx,fy,z,c,out_value));
- }
- //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X and Y-coordinates.
- /**
- Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
- are achieved for both X and Y-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _cubic_atXY(float,float,int,int).
- **/
- Tfloat cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atXY(): Empty instance.",
- cimg_instance);
- return _cubic_atXY(fx,fy,z,c);
- }
- Tfloat _cubic_atXY(const float fx, const float fy, const int z=0, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1),
- nfy = cimg::type<float>::is_nan(fy)?0:cimg::cut(fy,0,height() - 1);
- const int x = (int)nfx, y = (int)nfy;
- const float dx = nfx - x, dy = nfy - y;
- const int
- px = x - 1<0?0:x - 1, nx = dx<=0?x:x + 1, ax = x + 2>=width()?width() - 1:x + 2,
- py = y - 1<0?0:y - 1, ny = dy<=0?y:y + 1, ay = y + 2>=height()?height() - 1:y + 2;
- const Tfloat
- Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c),
- Iap = (Tfloat)(*this)(ax,py,z,c),
- 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)),
- Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
- Iac = (Tfloat)(*this)(ax,y,z,c),
- 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)),
- Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c),
- Ian = (Tfloat)(*this)(ax,ny,z,c),
- 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)),
- Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c),
- Iaa = (Tfloat)(*this)(ax,ay,z,c),
- 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));
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y-coordinates.
- /**
- Similar to cubic_atXY(float,float,int,int) const, except that the return value is clamped to stay in the
- min/max range of the datatype \c T.
- **/
- T cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
- return cimg::type<T>::cut(cubic_atXY(fx,fy,z,c));
- }
- T _cubic_atXY_c(const float fx, const float fy, const int z, const int c) const {
- return cimg::type<T>::cut(_cubic_atXY(fx,fy,z,c));
- }
- //! Return pixel value, using cubic interpolation and periodic boundary conditions for the X and Y-coordinates.
- Tfloat cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atXY_p(): Empty instance.",
- cimg_instance);
- return _cubic_atXY_p(fx,fy,z,c);
- }
- Tfloat _cubic_atXY_p(const float fx, const float fy, const int z=0, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f),
- nfy = cimg::type<float>::is_nan(fy)?0:cimg::mod(fy,_height - 0.5f);
- const int x = (int)nfx, y = (int)nfy;
- const float dx = nfx - x, dy = nfy - y;
- const int
- px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width()),
- py = cimg::mod(y - 1,height()), ny = cimg::mod(y + 1,height()), ay = cimg::mod(y + 2,height());
- const Tfloat
- Ipp = (Tfloat)(*this)(px,py,z,c), Icp = (Tfloat)(*this)(x,py,z,c), Inp = (Tfloat)(*this)(nx,py,z,c),
- Iap = (Tfloat)(*this)(ax,py,z,c),
- 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)),
- Ipc = (Tfloat)(*this)(px,y,z,c), Icc = (Tfloat)(*this)(x, y,z,c), Inc = (Tfloat)(*this)(nx,y,z,c),
- Iac = (Tfloat)(*this)(ax,y,z,c),
- 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)),
- Ipn = (Tfloat)(*this)(px,ny,z,c), Icn = (Tfloat)(*this)(x,ny,z,c), Inn = (Tfloat)(*this)(nx,ny,z,c),
- Ian = (Tfloat)(*this)(ax,ny,z,c),
- 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)),
- Ipa = (Tfloat)(*this)(px,ay,z,c), Ica = (Tfloat)(*this)(x,ay,z,c), Ina = (Tfloat)(*this)(nx,ay,z,c),
- Iaa = (Tfloat)(*this)(ax,ay,z,c),
- 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));
- 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));
- }
- T cubic_atXY_pc(const float fx, const float fy, const int z, const int c) const {
- return cimg::type<T>::cut(cubic_atXY_p(fx,fy,z,c));
- }
- T _cubic_atXY_pc(const float fx, const float fy, const int z, const int c) const {
- return cimg::type<T>::cut(_cubic_atXY_p(fx,fy,z,c));
- }
- //! Return pixel value, using cubic interpolation and Dirichlet boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to cubic_atX(float,int,int,int,const T) const, except that the cubic interpolation and boundary checking
- are achieved both for X,Y and Z-coordinates.
- **/
- Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
- const int
- x = (int)fx - (fx>=0?0:1), px = x - 1, nx = x + 1, ax = x + 2,
- y = (int)fy - (fy>=0?0:1), py = y - 1, ny = y + 1, ay = y + 2,
- z = (int)fz - (fz>=0?0:1), pz = z - 1, nz = z + 1, az = z + 2;
- const float dx = fx - x, dy = fy - y, dz = fz - z;
- const Tfloat
- Ippp = (Tfloat)atXYZ(px,py,pz,c,out_value), Icpp = (Tfloat)atXYZ(x,py,pz,c,out_value),
- Inpp = (Tfloat)atXYZ(nx,py,pz,c,out_value), Iapp = (Tfloat)atXYZ(ax,py,pz,c,out_value),
- Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
- dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
- Ipcp = (Tfloat)atXYZ(px,y,pz,c,out_value), Iccp = (Tfloat)atXYZ(x, y,pz,c,out_value),
- Incp = (Tfloat)atXYZ(nx,y,pz,c,out_value), Iacp = (Tfloat)atXYZ(ax,y,pz,c,out_value),
- Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
- dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
- Ipnp = (Tfloat)atXYZ(px,ny,pz,c,out_value), Icnp = (Tfloat)atXYZ(x,ny,pz,c,out_value),
- Innp = (Tfloat)atXYZ(nx,ny,pz,c,out_value), Ianp = (Tfloat)atXYZ(ax,ny,pz,c,out_value),
- Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
- dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
- Ipap = (Tfloat)atXYZ(px,ay,pz,c,out_value), Icap = (Tfloat)atXYZ(x,ay,pz,c,out_value),
- Inap = (Tfloat)atXYZ(nx,ay,pz,c,out_value), Iaap = (Tfloat)atXYZ(ax,ay,pz,c,out_value),
- Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
- dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
- Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
- dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
- Ippc = (Tfloat)atXYZ(px,py,z,c,out_value), Icpc = (Tfloat)atXYZ(x,py,z,c,out_value),
- Inpc = (Tfloat)atXYZ(nx,py,z,c,out_value), Iapc = (Tfloat)atXYZ(ax,py,z,c,out_value),
- Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
- dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
- Ipcc = (Tfloat)atXYZ(px,y,z,c,out_value), Iccc = (Tfloat)atXYZ(x, y,z,c,out_value),
- Incc = (Tfloat)atXYZ(nx,y,z,c,out_value), Iacc = (Tfloat)atXYZ(ax,y,z,c,out_value),
- Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
- dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
- Ipnc = (Tfloat)atXYZ(px,ny,z,c,out_value), Icnc = (Tfloat)atXYZ(x,ny,z,c,out_value),
- Innc = (Tfloat)atXYZ(nx,ny,z,c,out_value), Ianc = (Tfloat)atXYZ(ax,ny,z,c,out_value),
- Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
- dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
- Ipac = (Tfloat)atXYZ(px,ay,z,c,out_value), Icac = (Tfloat)atXYZ(x,ay,z,c,out_value),
- Inac = (Tfloat)atXYZ(nx,ay,z,c,out_value), Iaac = (Tfloat)atXYZ(ax,ay,z,c,out_value),
- Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
- dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
- Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
- dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
- Ippn = (Tfloat)atXYZ(px,py,nz,c,out_value), Icpn = (Tfloat)atXYZ(x,py,nz,c,out_value),
- Inpn = (Tfloat)atXYZ(nx,py,nz,c,out_value), Iapn = (Tfloat)atXYZ(ax,py,nz,c,out_value),
- Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
- dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
- Ipcn = (Tfloat)atXYZ(px,y,nz,c,out_value), Iccn = (Tfloat)atXYZ(x, y,nz,c,out_value),
- Incn = (Tfloat)atXYZ(nx,y,nz,c,out_value), Iacn = (Tfloat)atXYZ(ax,y,nz,c,out_value),
- Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
- dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
- Ipnn = (Tfloat)atXYZ(px,ny,nz,c,out_value), Icnn = (Tfloat)atXYZ(x,ny,nz,c,out_value),
- Innn = (Tfloat)atXYZ(nx,ny,nz,c,out_value), Iann = (Tfloat)atXYZ(ax,ny,nz,c,out_value),
- Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
- dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
- Ipan = (Tfloat)atXYZ(px,ay,nz,c,out_value), Ican = (Tfloat)atXYZ(x,ay,nz,c,out_value),
- Inan = (Tfloat)atXYZ(nx,ay,nz,c,out_value), Iaan = (Tfloat)atXYZ(ax,ay,nz,c,out_value),
- Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
- dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
- In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
- dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
- Ippa = (Tfloat)atXYZ(px,py,az,c,out_value), Icpa = (Tfloat)atXYZ(x,py,az,c,out_value),
- Inpa = (Tfloat)atXYZ(nx,py,az,c,out_value), Iapa = (Tfloat)atXYZ(ax,py,az,c,out_value),
- Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
- dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
- Ipca = (Tfloat)atXYZ(px,y,az,c,out_value), Icca = (Tfloat)atXYZ(x, y,az,c,out_value),
- Inca = (Tfloat)atXYZ(nx,y,az,c,out_value), Iaca = (Tfloat)atXYZ(ax,y,az,c,out_value),
- Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
- dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
- Ipna = (Tfloat)atXYZ(px,ny,az,c,out_value), Icna = (Tfloat)atXYZ(x,ny,az,c,out_value),
- Inna = (Tfloat)atXYZ(nx,ny,az,c,out_value), Iana = (Tfloat)atXYZ(ax,ny,az,c,out_value),
- Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
- dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
- Ipaa = (Tfloat)atXYZ(px,ay,az,c,out_value), Icaa = (Tfloat)atXYZ(x,ay,az,c,out_value),
- Inaa = (Tfloat)atXYZ(nx,ay,az,c,out_value), Iaaa = (Tfloat)atXYZ(ax,ay,az,c,out_value),
- Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
- dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
- Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
- dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Dirichlet boundary conditions for the XYZ-coordinates.
- /**
- Similar to cubic_atXYZ(float,float,float,int,const T) const, except that the return value is clamped to stay
- in the min/max range of the datatype \c T.
- **/
- T cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c, const T& out_value) const {
- return cimg::type<T>::cut(cubic_atXYZ(fx,fy,fz,c,out_value));
- }
- //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
- are achieved both for X,Y and Z-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _cubic_atXYZ(float,float,float,int).
- **/
- Tfloat cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atXYZ(): Empty instance.",
- cimg_instance);
- return _cubic_atXYZ(fx,fy,fz,c);
- }
- Tfloat _cubic_atXYZ(const float fx, const float fy, const float fz, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::cut(fx,0,width() - 1),
- nfy = cimg::type<float>::is_nan(fy)?0:cimg::cut(fy,0,height() - 1),
- nfz = cimg::type<float>::is_nan(fz)?0:cimg::cut(fz,0,depth() - 1);
- const int x = (int)nfx, y = (int)nfy, z = (int)nfz;
- const float dx = nfx - x, dy = nfy - y, dz = nfz - z;
- const int
- px = x - 1<0?0:x - 1, nx = dx>0?x + 1:x, ax = x + 2>=width()?width() - 1:x + 2,
- py = y - 1<0?0:y - 1, ny = dy>0?y + 1:y, ay = y + 2>=height()?height() - 1:y + 2,
- pz = z - 1<0?0:z - 1, nz = dz>0?z + 1:z, az = z + 2>=depth()?depth() - 1:z + 2;
- const Tfloat
- Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c),
- Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c),
- Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
- dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
- Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c),
- Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c),
- Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
- dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
- Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c),
- Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c),
- Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
- dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
- Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c),
- Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c),
- Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
- dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
- Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
- dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
- Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c),
- Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c),
- Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
- dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
- Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c),
- Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c),
- Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
- dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
- Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c),
- Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c),
- Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
- dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
- Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c),
- Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c),
- Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
- dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
- Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
- dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
- Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c),
- Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c),
- Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
- dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
- Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c),
- Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c),
- Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
- dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
- Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c),
- Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c),
- Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
- dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
- Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c),
- Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c),
- Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
- dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
- In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
- dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
- Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c),
- Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c),
- Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
- dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
- Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c),
- Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c),
- Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
- dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
- Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c),
- Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c),
- Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
- dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
- Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c),
- Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c),
- Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
- dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
- Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
- dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
- 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));
- }
- //! Return clamped pixel value, using cubic interpolation and Neumann boundary conditions for the XYZ-coordinates.
- /**
- Similar to cubic_atXYZ(float,float,float,int) const, except that the return value is clamped to stay in the
- min/max range of the datatype \c T.
- **/
- T cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c) const {
- return cimg::type<T>::cut(cubic_atXYZ(fx,fy,fz,c));
- }
- T _cubic_atXYZ_c(const float fx, const float fy, const float fz, const int c) const {
- return cimg::type<T>::cut(_cubic_atXYZ(fx,fy,fz,c));
- }
- //! Return pixel value, using cubic interpolation and Neumann boundary conditions for the X,Y and Z-coordinates.
- /**
- Similar to cubic_atX(float,int,int,int) const, except that the cubic interpolation and boundary checking
- are achieved both for X,Y and Z-coordinates.
- \note
- - If you know your image instance is \e not empty, you may rather use the slightly faster method
- \c _cubic_atXYZ(float,float,float,int).
- **/
- Tfloat cubic_atXYZ_p(const float fx, const float fy, const float fz, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "cubic_atXYZ_p(): Empty instance.",
- cimg_instance);
- return _cubic_atXYZ_p(fx,fy,fz,c);
- }
- Tfloat _cubic_atXYZ_p(const float fx, const float fy, const float fz, const int c=0) const {
- const float
- nfx = cimg::type<float>::is_nan(fx)?0:cimg::mod(fx,_width - 0.5f),
- nfy = cimg::type<float>::is_nan(fy)?0:cimg::mod(fy,_height - 0.5f),
- nfz = cimg::type<float>::is_nan(fz)?0:cimg::mod(fz,_depth - 0.5f);
- const int x = (int)nfx, y = (int)nfy, z = (int)nfz;
- const float dx = nfx - x, dy = nfy - y, dz = nfz - z;
- const int
- px = cimg::mod(x - 1,width()), nx = cimg::mod(x + 1,width()), ax = cimg::mod(x + 2,width()),
- py = cimg::mod(y - 1,height()), ny = cimg::mod(y + 1,height()), ay = cimg::mod(y + 2,height()),
- pz = cimg::mod(z - 1,depth()), nz = cimg::mod(z + 1,depth()), az = cimg::mod(z + 2,depth());
- const Tfloat
- Ippp = (Tfloat)(*this)(px,py,pz,c), Icpp = (Tfloat)(*this)(x,py,pz,c),
- Inpp = (Tfloat)(*this)(nx,py,pz,c), Iapp = (Tfloat)(*this)(ax,py,pz,c),
- Ipp = Icpp + 0.5f*(dx*(-Ippp + Inpp) + dx*dx*(2*Ippp - 5*Icpp + 4*Inpp - Iapp) +
- dx*dx*dx*(-Ippp + 3*Icpp - 3*Inpp + Iapp)),
- Ipcp = (Tfloat)(*this)(px,y,pz,c), Iccp = (Tfloat)(*this)(x, y,pz,c),
- Incp = (Tfloat)(*this)(nx,y,pz,c), Iacp = (Tfloat)(*this)(ax,y,pz,c),
- Icp = Iccp + 0.5f*(dx*(-Ipcp + Incp) + dx*dx*(2*Ipcp - 5*Iccp + 4*Incp - Iacp) +
- dx*dx*dx*(-Ipcp + 3*Iccp - 3*Incp + Iacp)),
- Ipnp = (Tfloat)(*this)(px,ny,pz,c), Icnp = (Tfloat)(*this)(x,ny,pz,c),
- Innp = (Tfloat)(*this)(nx,ny,pz,c), Ianp = (Tfloat)(*this)(ax,ny,pz,c),
- Inp = Icnp + 0.5f*(dx*(-Ipnp + Innp) + dx*dx*(2*Ipnp - 5*Icnp + 4*Innp - Ianp) +
- dx*dx*dx*(-Ipnp + 3*Icnp - 3*Innp + Ianp)),
- Ipap = (Tfloat)(*this)(px,ay,pz,c), Icap = (Tfloat)(*this)(x,ay,pz,c),
- Inap = (Tfloat)(*this)(nx,ay,pz,c), Iaap = (Tfloat)(*this)(ax,ay,pz,c),
- Iap = Icap + 0.5f*(dx*(-Ipap + Inap) + dx*dx*(2*Ipap - 5*Icap + 4*Inap - Iaap) +
- dx*dx*dx*(-Ipap + 3*Icap - 3*Inap + Iaap)),
- Ip = Icp + 0.5f*(dy*(-Ipp + Inp) + dy*dy*(2*Ipp - 5*Icp + 4*Inp - Iap) +
- dy*dy*dy*(-Ipp + 3*Icp - 3*Inp + Iap)),
- Ippc = (Tfloat)(*this)(px,py,z,c), Icpc = (Tfloat)(*this)(x,py,z,c),
- Inpc = (Tfloat)(*this)(nx,py,z,c), Iapc = (Tfloat)(*this)(ax,py,z,c),
- Ipc = Icpc + 0.5f*(dx*(-Ippc + Inpc) + dx*dx*(2*Ippc - 5*Icpc + 4*Inpc - Iapc) +
- dx*dx*dx*(-Ippc + 3*Icpc - 3*Inpc + Iapc)),
- Ipcc = (Tfloat)(*this)(px,y,z,c), Iccc = (Tfloat)(*this)(x, y,z,c),
- Incc = (Tfloat)(*this)(nx,y,z,c), Iacc = (Tfloat)(*this)(ax,y,z,c),
- Icc = Iccc + 0.5f*(dx*(-Ipcc + Incc) + dx*dx*(2*Ipcc - 5*Iccc + 4*Incc - Iacc) +
- dx*dx*dx*(-Ipcc + 3*Iccc - 3*Incc + Iacc)),
- Ipnc = (Tfloat)(*this)(px,ny,z,c), Icnc = (Tfloat)(*this)(x,ny,z,c),
- Innc = (Tfloat)(*this)(nx,ny,z,c), Ianc = (Tfloat)(*this)(ax,ny,z,c),
- Inc = Icnc + 0.5f*(dx*(-Ipnc + Innc) + dx*dx*(2*Ipnc - 5*Icnc + 4*Innc - Ianc) +
- dx*dx*dx*(-Ipnc + 3*Icnc - 3*Innc + Ianc)),
- Ipac = (Tfloat)(*this)(px,ay,z,c), Icac = (Tfloat)(*this)(x,ay,z,c),
- Inac = (Tfloat)(*this)(nx,ay,z,c), Iaac = (Tfloat)(*this)(ax,ay,z,c),
- Iac = Icac + 0.5f*(dx*(-Ipac + Inac) + dx*dx*(2*Ipac - 5*Icac + 4*Inac - Iaac) +
- dx*dx*dx*(-Ipac + 3*Icac - 3*Inac + Iaac)),
- Ic = Icc + 0.5f*(dy*(-Ipc + Inc) + dy*dy*(2*Ipc - 5*Icc + 4*Inc - Iac) +
- dy*dy*dy*(-Ipc + 3*Icc - 3*Inc + Iac)),
- Ippn = (Tfloat)(*this)(px,py,nz,c), Icpn = (Tfloat)(*this)(x,py,nz,c),
- Inpn = (Tfloat)(*this)(nx,py,nz,c), Iapn = (Tfloat)(*this)(ax,py,nz,c),
- Ipn = Icpn + 0.5f*(dx*(-Ippn + Inpn) + dx*dx*(2*Ippn - 5*Icpn + 4*Inpn - Iapn) +
- dx*dx*dx*(-Ippn + 3*Icpn - 3*Inpn + Iapn)),
- Ipcn = (Tfloat)(*this)(px,y,nz,c), Iccn = (Tfloat)(*this)(x, y,nz,c),
- Incn = (Tfloat)(*this)(nx,y,nz,c), Iacn = (Tfloat)(*this)(ax,y,nz,c),
- Icn = Iccn + 0.5f*(dx*(-Ipcn + Incn) + dx*dx*(2*Ipcn - 5*Iccn + 4*Incn - Iacn) +
- dx*dx*dx*(-Ipcn + 3*Iccn - 3*Incn + Iacn)),
- Ipnn = (Tfloat)(*this)(px,ny,nz,c), Icnn = (Tfloat)(*this)(x,ny,nz,c),
- Innn = (Tfloat)(*this)(nx,ny,nz,c), Iann = (Tfloat)(*this)(ax,ny,nz,c),
- Inn = Icnn + 0.5f*(dx*(-Ipnn + Innn) + dx*dx*(2*Ipnn - 5*Icnn + 4*Innn - Iann) +
- dx*dx*dx*(-Ipnn + 3*Icnn - 3*Innn + Iann)),
- Ipan = (Tfloat)(*this)(px,ay,nz,c), Ican = (Tfloat)(*this)(x,ay,nz,c),
- Inan = (Tfloat)(*this)(nx,ay,nz,c), Iaan = (Tfloat)(*this)(ax,ay,nz,c),
- Ian = Ican + 0.5f*(dx*(-Ipan + Inan) + dx*dx*(2*Ipan - 5*Ican + 4*Inan - Iaan) +
- dx*dx*dx*(-Ipan + 3*Ican - 3*Inan + Iaan)),
- In = Icn + 0.5f*(dy*(-Ipn + Inn) + dy*dy*(2*Ipn - 5*Icn + 4*Inn - Ian) +
- dy*dy*dy*(-Ipn + 3*Icn - 3*Inn + Ian)),
- Ippa = (Tfloat)(*this)(px,py,az,c), Icpa = (Tfloat)(*this)(x,py,az,c),
- Inpa = (Tfloat)(*this)(nx,py,az,c), Iapa = (Tfloat)(*this)(ax,py,az,c),
- Ipa = Icpa + 0.5f*(dx*(-Ippa + Inpa) + dx*dx*(2*Ippa - 5*Icpa + 4*Inpa - Iapa) +
- dx*dx*dx*(-Ippa + 3*Icpa - 3*Inpa + Iapa)),
- Ipca = (Tfloat)(*this)(px,y,az,c), Icca = (Tfloat)(*this)(x, y,az,c),
- Inca = (Tfloat)(*this)(nx,y,az,c), Iaca = (Tfloat)(*this)(ax,y,az,c),
- Ica = Icca + 0.5f*(dx*(-Ipca + Inca) + dx*dx*(2*Ipca - 5*Icca + 4*Inca - Iaca) +
- dx*dx*dx*(-Ipca + 3*Icca - 3*Inca + Iaca)),
- Ipna = (Tfloat)(*this)(px,ny,az,c), Icna = (Tfloat)(*this)(x,ny,az,c),
- Inna = (Tfloat)(*this)(nx,ny,az,c), Iana = (Tfloat)(*this)(ax,ny,az,c),
- Ina = Icna + 0.5f*(dx*(-Ipna + Inna) + dx*dx*(2*Ipna - 5*Icna + 4*Inna - Iana) +
- dx*dx*dx*(-Ipna + 3*Icna - 3*Inna + Iana)),
- Ipaa = (Tfloat)(*this)(px,ay,az,c), Icaa = (Tfloat)(*this)(x,ay,az,c),
- Inaa = (Tfloat)(*this)(nx,ay,az,c), Iaaa = (Tfloat)(*this)(ax,ay,az,c),
- Iaa = Icaa + 0.5f*(dx*(-Ipaa + Inaa) + dx*dx*(2*Ipaa - 5*Icaa + 4*Inaa - Iaaa) +
- dx*dx*dx*(-Ipaa + 3*Icaa - 3*Inaa + Iaaa)),
- Ia = Ica + 0.5f*(dy*(-Ipa + Ina) + dy*dy*(2*Ipa - 5*Ica + 4*Ina - Iaa) +
- dy*dy*dy*(-Ipa + 3*Ica - 3*Ina + Iaa));
- 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));
- }
- T cubic_atXYZ_pc(const float fx, const float fy, const float fz, const int c) const {
- return cimg::type<T>::cut(cubic_atXYZ_p(fx,fy,fz,c));
- }
- T _cubic_atXYZ_pc(const float fx, const float fy, const float fz, const int c) const {
- return cimg::type<T>::cut(_cubic_atXYZ_p(fx,fy,fz,c));
- }
- //! Set pixel value, using linear interpolation for the X-coordinates.
- /**
- Set pixel value at specified coordinates (\c fx,\c y,\c z,\c c) in the image instance, in a way that
- the value is spread amongst several neighbors if the pixel coordinates are float-valued.
- \param value Pixel value to set.
- \param fx X-coordinate of the pixel value (float-valued).
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param is_added Tells if the pixel value is added to (\c true), or simply replace (\c false) the current image
- pixel(s).
- \return A reference to the current image instance.
- \note
- - Calling this method with out-of-bounds coordinates does nothing.
- **/
- CImg<T>& set_linear_atX(const T& value, const float fx, const int y=0, const int z=0, const int c=0,
- const bool is_added=false) {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1;
- const float
- dx = fx - x;
- if (y>=0 && y<height() && z>=0 && z<depth() && c>=0 && c<spectrum()) {
- if (x>=0 && x<width()) {
- const float w1 = 1 - dx, w2 = is_added?1:(1 - w1);
- (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx, w2 = is_added?1:(1 - w1);
- (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
- }
- }
- return *this;
- }
- //! Set pixel value, using linear interpolation for the X and Y-coordinates.
- /**
- Similar to set_linear_atX(const T&,float,int,int,int,bool), except that the linear interpolation
- is achieved both for X and Y-coordinates.
- **/
- CImg<T>& set_linear_atXY(const T& value, const float fx, const float fy=0, const int z=0, const int c=0,
- const bool is_added=false) {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1,
- y = (int)fy - (fy>=0?0:1), ny = y + 1;
- const float
- dx = fx - x,
- dy = fy - y;
- if (z>=0 && z<depth() && c>=0 && c<spectrum()) {
- if (y>=0 && y<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*(1 - dy), w2 = is_added?1:(1 - w1);
- (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*(1 - dy), w2 = is_added?1:(1 - w1);
- (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
- }
- }
- if (ny>=0 && ny<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*dy, w2 = is_added?1:(1 - w1);
- (*this)(x,ny,z,c) = (T)(w1*value + w2*(*this)(x,ny,z,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*dy, w2 = is_added?1:(1 - w1);
- (*this)(nx,ny,z,c) = (T)(w1*value + w2*(*this)(nx,ny,z,c));
- }
- }
- }
- return *this;
- }
- //! Set pixel value, using linear interpolation for the X,Y and Z-coordinates.
- /**
- Similar to set_linear_atXY(const T&,float,float,int,int,bool), except that the linear interpolation
- is achieved both for X,Y and Z-coordinates.
- **/
- CImg<T>& set_linear_atXYZ(const T& value, const float fx, const float fy=0, const float fz=0, const int c=0,
- const bool is_added=false) {
- const int
- x = (int)fx - (fx>=0?0:1), nx = x + 1,
- y = (int)fy - (fy>=0?0:1), ny = y + 1,
- z = (int)fz - (fz>=0?0:1), nz = z + 1;
- const float
- dx = fx - x,
- dy = fy - y,
- dz = fz - z;
- if (c>=0 && c<spectrum()) {
- if (z>=0 && z<depth()) {
- if (y>=0 && y<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*(1 - dy)*(1 - dz), w2 = is_added?1:(1 - w1);
- (*this)(x,y,z,c) = (T)(w1*value + w2*(*this)(x,y,z,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*(1 - dy)*(1 - dz), w2 = is_added?1:(1 - w1);
- (*this)(nx,y,z,c) = (T)(w1*value + w2*(*this)(nx,y,z,c));
- }
- }
- if (ny>=0 && ny<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*dy*(1 - dz), w2 = is_added?1:(1 - w1);
- (*this)(x,ny,z,c) = (T)(w1*value + w2*(*this)(x,ny,z,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*dy*(1 - dz), w2 = is_added?1:(1 - w1);
- (*this)(nx,ny,z,c) = (T)(w1*value + w2*(*this)(nx,ny,z,c));
- }
- }
- }
- if (nz>=0 && nz<depth()) {
- if (y>=0 && y<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*(1 - dy)*dz, w2 = is_added?1:(1 - w1);
- (*this)(x,y,nz,c) = (T)(w1*value + w2*(*this)(x,y,nz,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*(1 - dy)*dz, w2 = is_added?1:(1 - w1);
- (*this)(nx,y,nz,c) = (T)(w1*value + w2*(*this)(nx,y,nz,c));
- }
- }
- if (ny>=0 && ny<height()) {
- if (x>=0 && x<width()) {
- const float w1 = (1 - dx)*dy*dz, w2 = is_added?1:(1 - w1);
- (*this)(x,ny,nz,c) = (T)(w1*value + w2*(*this)(x,ny,nz,c));
- }
- if (nx>=0 && nx<width()) {
- const float w1 = dx*dy*dz, w2 = is_added?1:(1 - w1);
- (*this)(nx,ny,nz,c) = (T)(w1*value + w2*(*this)(nx,ny,nz,c));
- }
- }
- }
- }
- return *this;
- }
- //! Return a C-string containing a list of all values of the image instance.
- /**
- Return a new \c CImg<char> image whose buffer data() is a \c char* string describing the list of all pixel values
- of the image instance (written in base 10), separated by specified \c separator character.
- \param separator A \c char character which specifies the separator between values in the returned C-string.
- \param max_size Maximum size of the returned image (or \c 0 if no limits are set).
- \param format For float/double-values, tell the printf format used to generate the text representation
- of the numbers (or \c 0 for default representation).
- \note
- - The returned image is never empty.
- - For an empty image instance, the returned string is <tt>""</tt>.
- - If \c max_size is equal to \c 0, there are no limits on the size of the returned string.
- - Otherwise, if the maximum number of string characters is exceeded, the value string is cut off
- and terminated by character \c '\0'. In that case, the returned image size is <tt>max_size + 1</tt>.
- **/
- CImg<charT> value_string(const char separator=',', const unsigned int max_size=0,
- const char *const format=0) const {
- if (is_empty() || max_size==1) return CImg<charT>(1,1,1,1,0);
- CImgList<charT> items;
- CImg<charT> s_item(256); *s_item = 0;
- const T *ptrs = _data;
- unsigned int string_size = 0;
- const char *const _format = format?format:cimg::type<T>::format();
- for (ulongT off = 0, siz = size(); off<siz && (!max_size || string_size<max_size); ++off) {
- const unsigned int printed_size = 1U + cimg_snprintf(s_item,s_item._width,_format,
- cimg::type<T>::format(*(ptrs++)));
- CImg<charT> item(s_item._data,printed_size);
- item[printed_size - 1] = separator;
- item.move_to(items);
- if (max_size) string_size+=printed_size;
- }
- CImg<charT> res;
- (items>'x').move_to(res);
- if (max_size && res._width>=max_size) res.crop(0,max_size - 1);
- res.back() = 0;
- return res;
- }
- //@}
- //-------------------------------------
- //
- //! \name Instance Checking
- //@{
- //-------------------------------------
- //! Test shared state of the pixel buffer.
- /**
- Return \c true if image instance has a shared memory buffer, and \c false otherwise.
- \note
- - A shared image do not own his pixel buffer data() and will not deallocate it on destruction.
- - Most of the time, a \c CImg<T> image instance will \e not be shared.
- - A shared image can only be obtained by a limited set of constructors and methods (see list below).
- **/
- bool is_shared() const {
- return _is_shared;
- }
- //! Test if image instance is empty.
- /**
- Return \c true, if image instance is empty, i.e. does \e not contain any pixel values, has dimensions
- \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.
- **/
- bool is_empty() const {
- return !(_data && _width && _height && _depth && _spectrum);
- }
- //! Test if image instance contains a 'inf' value.
- /**
- Return \c true, if image instance contains a 'inf' value, and \c false otherwise.
- **/
- bool is_inf() const {
- if (cimg::type<T>::is_float()) cimg_for(*this,p,T) if (cimg::type<T>::is_inf((float)*p)) return true;
- return false;
- }
- //! Test if image instance contains a NaN value.
- /**
- Return \c true, if image instance contains a NaN value, and \c false otherwise.
- **/
- bool is_nan() const {
- if (cimg::type<T>::is_float()) cimg_for(*this,p,T) if (cimg::type<T>::is_nan((float)*p)) return true;
- return false;
- }
- //! Test if image width is equal to specified value.
- bool is_sameX(const unsigned int size_x) const {
- return _width==size_x;
- }
- //! Test if image width is equal to specified value.
- template<typename t>
- bool is_sameX(const CImg<t>& img) const {
- return is_sameX(img._width);
- }
- //! Test if image width is equal to specified value.
- bool is_sameX(const CImgDisplay& disp) const {
- return is_sameX(disp._width);
- }
- //! Test if image height is equal to specified value.
- bool is_sameY(const unsigned int size_y) const {
- return _height==size_y;
- }
- //! Test if image height is equal to specified value.
- template<typename t>
- bool is_sameY(const CImg<t>& img) const {
- return is_sameY(img._height);
- }
- //! Test if image height is equal to specified value.
- bool is_sameY(const CImgDisplay& disp) const {
- return is_sameY(disp._height);
- }
- //! Test if image depth is equal to specified value.
- bool is_sameZ(const unsigned int size_z) const {
- return _depth==size_z;
- }
- //! Test if image depth is equal to specified value.
- template<typename t>
- bool is_sameZ(const CImg<t>& img) const {
- return is_sameZ(img._depth);
- }
- //! Test if image spectrum is equal to specified value.
- bool is_sameC(const unsigned int size_c) const {
- return _spectrum==size_c;
- }
- //! Test if image spectrum is equal to specified value.
- template<typename t>
- bool is_sameC(const CImg<t>& img) const {
- return is_sameC(img._spectrum);
- }
- //! Test if image width and height are equal to specified values.
- /**
- Test if is_sameX(unsigned int) const and is_sameY(unsigned int) const are both verified.
- **/
- bool is_sameXY(const unsigned int size_x, const unsigned int size_y) const {
- return _width==size_x && _height==size_y;
- }
- //! Test if image width and height are the same as that of another image.
- /**
- Test if is_sameX(const CImg<t>&) const and is_sameY(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXY(const CImg<t>& img) const {
- return is_sameXY(img._width,img._height);
- }
- //! Test if image width and height are the same as that of an existing display window.
- /**
- Test if is_sameX(const CImgDisplay&) const and is_sameY(const CImgDisplay&) const are both verified.
- **/
- bool is_sameXY(const CImgDisplay& disp) const {
- return is_sameXY(disp._width,disp._height);
- }
- //! Test if image width and depth are equal to specified values.
- /**
- Test if is_sameX(unsigned int) const and is_sameZ(unsigned int) const are both verified.
- **/
- bool is_sameXZ(const unsigned int size_x, const unsigned int size_z) const {
- return _width==size_x && _depth==size_z;
- }
- //! Test if image width and depth are the same as that of another image.
- /**
- Test if is_sameX(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXZ(const CImg<t>& img) const {
- return is_sameXZ(img._width,img._depth);
- }
- //! Test if image width and spectrum are equal to specified values.
- /**
- Test if is_sameX(unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameXC(const unsigned int size_x, const unsigned int size_c) const {
- return _width==size_x && _spectrum==size_c;
- }
- //! Test if image width and spectrum are the same as that of another image.
- /**
- Test if is_sameX(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXC(const CImg<t>& img) const {
- return is_sameXC(img._width,img._spectrum);
- }
- //! Test if image height and depth are equal to specified values.
- /**
- Test if is_sameY(unsigned int) const and is_sameZ(unsigned int) const are both verified.
- **/
- bool is_sameYZ(const unsigned int size_y, const unsigned int size_z) const {
- return _height==size_y && _depth==size_z;
- }
- //! Test if image height and depth are the same as that of another image.
- /**
- Test if is_sameY(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameYZ(const CImg<t>& img) const {
- return is_sameYZ(img._height,img._depth);
- }
- //! Test if image height and spectrum are equal to specified values.
- /**
- Test if is_sameY(unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameYC(const unsigned int size_y, const unsigned int size_c) const {
- return _height==size_y && _spectrum==size_c;
- }
- //! Test if image height and spectrum are the same as that of another image.
- /**
- Test if is_sameY(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameYC(const CImg<t>& img) const {
- return is_sameYC(img._height,img._spectrum);
- }
- //! Test if image depth and spectrum are equal to specified values.
- /**
- Test if is_sameZ(unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameZC(const unsigned int size_z, const unsigned int size_c) const {
- return _depth==size_z && _spectrum==size_c;
- }
- //! Test if image depth and spectrum are the same as that of another image.
- /**
- Test if is_sameZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameZC(const CImg<t>& img) const {
- return is_sameZC(img._depth,img._spectrum);
- }
- //! Test if image width, height and depth are equal to specified values.
- /**
- Test if is_sameXY(unsigned int,unsigned int) const and is_sameZ(unsigned int) const are both verified.
- **/
- bool is_sameXYZ(const unsigned int size_x, const unsigned int size_y, const unsigned int size_z) const {
- return is_sameXY(size_x,size_y) && _depth==size_z;
- }
- //! Test if image width, height and depth are the same as that of another image.
- /**
- Test if is_sameXY(const CImg<t>&) const and is_sameZ(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXYZ(const CImg<t>& img) const {
- return is_sameXYZ(img._width,img._height,img._depth);
- }
- //! Test if image width, height and spectrum are equal to specified values.
- /**
- Test if is_sameXY(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameXYC(const unsigned int size_x, const unsigned int size_y, const unsigned int size_c) const {
- return is_sameXY(size_x,size_y) && _spectrum==size_c;
- }
- //! Test if image width, height and spectrum are the same as that of another image.
- /**
- Test if is_sameXY(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXYC(const CImg<t>& img) const {
- return is_sameXYC(img._width,img._height,img._spectrum);
- }
- //! Test if image width, depth and spectrum are equal to specified values.
- /**
- Test if is_sameXZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameXZC(const unsigned int size_x, const unsigned int size_z, const unsigned int size_c) const {
- return is_sameXZ(size_x,size_z) && _spectrum==size_c;
- }
- //! Test if image width, depth and spectrum are the same as that of another image.
- /**
- Test if is_sameXZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXZC(const CImg<t>& img) const {
- return is_sameXZC(img._width,img._depth,img._spectrum);
- }
- //! Test if image height, depth and spectrum are equal to specified values.
- /**
- Test if is_sameYZ(unsigned int,unsigned int) const and is_sameC(unsigned int) const are both verified.
- **/
- bool is_sameYZC(const unsigned int size_y, const unsigned int size_z, const unsigned int size_c) const {
- return is_sameYZ(size_y,size_z) && _spectrum==size_c;
- }
- //! Test if image height, depth and spectrum are the same as that of another image.
- /**
- Test if is_sameYZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameYZC(const CImg<t>& img) const {
- return is_sameYZC(img._height,img._depth,img._spectrum);
- }
- //! Test if image width, height, depth and spectrum are equal to specified values.
- /**
- Test if is_sameXYZ(unsigned int,unsigned int,unsigned int) const and is_sameC(unsigned int) const are both
- verified.
- **/
- bool is_sameXYZC(const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c) const {
- return is_sameXYZ(size_x,size_y,size_z) && _spectrum==size_c;
- }
- //! Test if image width, height, depth and spectrum are the same as that of another image.
- /**
- Test if is_sameXYZ(const CImg<t>&) const and is_sameC(const CImg<t>&) const are both verified.
- **/
- template<typename t>
- bool is_sameXYZC(const CImg<t>& img) const {
- return is_sameXYZC(img._width,img._height,img._depth,img._spectrum);
- }
- //! Test if specified coordinates are inside image bounds.
- /**
- Return \c true if pixel located at (\c x,\c y,\c z,\c c) is inside bounds of the image instance,
- and \c false otherwise.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note
- - Return \c true only if all these conditions are verified:
- - The image instance is \e not empty.
- - <tt>0<=x<=\ref width() - 1</tt>.
- - <tt>0<=y<=\ref height() - 1</tt>.
- - <tt>0<=z<=\ref depth() - 1</tt>.
- - <tt>0<=c<=\ref spectrum() - 1</tt>.
- **/
- bool containsXYZC(const int x, const int y=0, const int z=0, const int c=0) const {
- return !is_empty() && x>=0 && x<width() && y>=0 && y<height() && z>=0 && z<depth() && c>=0 && c<spectrum();
- }
- //! Test if pixel value is inside image bounds and get its X,Y,Z and C-coordinates.
- /**
- Return \c true, if specified reference refers to a pixel value inside bounds of the image instance,
- and \c false otherwise.
- \param pixel Reference to pixel value to test.
- \param[out] x X-coordinate of the pixel value, if test succeeds.
- \param[out] y Y-coordinate of the pixel value, if test succeeds.
- \param[out] z Z-coordinate of the pixel value, if test succeeds.
- \param[out] c C-coordinate of the pixel value, if test succeeds.
- \note
- - Useful to convert an offset to a buffer value into pixel value coordinates:
- \code
- const CImg<float> img(100,100,1,3); // Construct a 100x100 RGB color image
- const unsigned long offset = 1249; // Offset to the pixel (49,12,0,0)
- unsigned int x,y,z,c;
- if (img.contains(img[offset],x,y,z,c)) { // Convert offset to (x,y,z,c) coordinates
- std::printf("Offset %u refers to pixel located at (%u,%u,%u,%u).\n",
- offset,x,y,z,c);
- }
- \endcode
- **/
- template<typename t>
- bool contains(const T& pixel, t& x, t& y, t& z, t& c) const {
- const ulongT wh = (ulongT)_width*_height, whd = wh*_depth, siz = whd*_spectrum;
- const T *const ppixel = &pixel;
- if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
- ulongT off = (ulongT)(ppixel - _data);
- const ulongT nc = off/whd;
- off%=whd;
- const ulongT nz = off/wh;
- off%=wh;
- const ulongT ny = off/_width, nx = off%_width;
- x = (t)nx; y = (t)ny; z = (t)nz; c = (t)nc;
- return true;
- }
- //! Test if pixel value is inside image bounds and get its X,Y and Z-coordinates.
- /**
- Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X,Y and Z-coordinates are set.
- **/
- template<typename t>
- bool contains(const T& pixel, t& x, t& y, t& z) const {
- const ulongT wh = (ulongT)_width*_height, whd = wh*_depth, siz = whd*_spectrum;
- const T *const ppixel = &pixel;
- if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
- ulongT off = ((ulongT)(ppixel - _data))%whd;
- const ulongT nz = off/wh;
- off%=wh;
- const ulongT ny = off/_width, nx = off%_width;
- x = (t)nx; y = (t)ny; z = (t)nz;
- return true;
- }
- //! Test if pixel value is inside image bounds and get its X and Y-coordinates.
- /**
- Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X and Y-coordinates are set.
- **/
- template<typename t>
- bool contains(const T& pixel, t& x, t& y) const {
- const ulongT wh = (ulongT)_width*_height, siz = wh*_depth*_spectrum;
- const T *const ppixel = &pixel;
- if (is_empty() || ppixel<_data || ppixel>=_data + siz) return false;
- ulongT off = ((unsigned int)(ppixel - _data))%wh;
- const ulongT ny = off/_width, nx = off%_width;
- x = (t)nx; y = (t)ny;
- return true;
- }
- //! Test if pixel value is inside image bounds and get its X-coordinate.
- /**
- Similar to contains(const T&,t&,t&,t&,t&) const, except that only the X-coordinate is set.
- **/
- template<typename t>
- bool contains(const T& pixel, t& x) const {
- const T *const ppixel = &pixel;
- if (is_empty() || ppixel<_data || ppixel>=_data + size()) return false;
- x = (t)(((ulongT)(ppixel - _data))%_width);
- return true;
- }
- //! Test if pixel value is inside image bounds.
- /**
- Similar to contains(const T&,t&,t&,t&,t&) const, except that no pixel coordinates are set.
- **/
- bool contains(const T& pixel) const {
- const T *const ppixel = &pixel;
- return !is_empty() && ppixel>=_data && ppixel<_data + size();
- }
- //! Test if pixel buffers of instance and input images overlap.
- /**
- Return \c true, if pixel buffers attached to image instance and input image \c img overlap,
- and \c false otherwise.
- \param img Input image to compare with.
- \note
- - Buffer overlapping may happen when manipulating \e shared images.
- - If two image buffers overlap, operating on one of the image will probably modify the other one.
- - Most of the time, \c CImg<T> instances are \e non-shared and do not overlap between each others.
- \par Example
- \code
- const CImg<float>
- img1("reference.jpg"), // Load RGB-color image
- img2 = img1.get_shared_channel(1); // Get shared version of the green channel
- if (img1.is_overlapped(img2)) { // Test succeeds, 'img1' and 'img2' overlaps
- std::printf("Buffers overlap!\n");
- }
- \endcode
- **/
- template<typename t>
- bool is_overlapped(const CImg<t>& img) const {
- const ulongT csiz = size(), isiz = img.size();
- return !((void*)(_data + csiz)<=(void*)img._data || (void*)_data>=(void*)(img._data + isiz));
- }
- //! Test if the set {\c *this,\c primitives,\c colors,\c opacities} defines a valid 3D object.
- /**
- Return \c true is the 3D object represented by the set {\c *this,\c primitives,\c colors,\c opacities} defines a
- valid 3D object, and \c false otherwise. The vertex coordinates are defined by the instance image.
- \param primitives List of primitives of the 3D object.
- \param colors List of colors of the 3D object.
- \param opacities List (or image) of opacities of the 3D object.
- \param full_check Tells if full checking of the 3D object must be performed.
- \param[out] error_message C-string to contain the error message, if the test does not succeed.
- \note
- - Set \c full_checking to \c false to speed-up the 3D object checking. In this case, only the size of
- each 3D object component is checked.
- - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message.
- **/
- template<typename tp, typename tc, typename to>
- bool is_object3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool full_check=true,
- char *const error_message=0) const {
- if (error_message) *error_message = 0;
- // Check consistency for the particular case of an empty 3D object.
- if (is_empty()) {
- if (primitives || colors || opacities) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines no vertices but %u primitives, "
- "%u colors and %lu opacities",
- _width,primitives._width,primitives._width,
- colors._width,(unsigned long)opacities.size());
- return false;
- }
- return true;
- }
- // Check consistency of vertices.
- if (_height!=3 || _depth>1 || _spectrum>1) { // Check vertices dimensions
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) has invalid vertex dimensions (%u,%u,%u,%u)",
- _width,primitives._width,_width,_height,_depth,_spectrum);
- return false;
- }
- if (colors._width>primitives._width + 1) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines %u colors",
- _width,primitives._width,colors._width);
- return false;
- }
- if (opacities.size()>primitives._width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines %lu opacities",
- _width,primitives._width,(unsigned long)opacities.size());
- return false;
- }
- if (!full_check) return true;
- // Check consistency of primitives.
- cimglist_for(primitives,l) {
- const CImg<tp>& primitive = primitives[l];
- const unsigned int psiz = (unsigned int)primitive.size();
- switch (psiz) {
- case 1 : { // Point
- const unsigned int i0 = (unsigned int)primitive(0);
- if (i0>=_width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) refers to invalid vertex index %u in "
- "point primitive [%u]",
- _width,primitives._width,i0,l);
- return false;
- }
- } break;
- case 5 : { // Sphere
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1);
- if (i0>=_width || i1>=_width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) refers to invalid vertex indices (%u,%u) in "
- "sphere primitive [%u]",
- _width,primitives._width,i0,i1,l);
- return false;
- }
- } break;
- case 2 : case 6 : { // Segment
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1);
- if (i0>=_width || i1>=_width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) refers to invalid vertex indices (%u,%u) in "
- "segment primitive [%u]",
- _width,primitives._width,i0,i1,l);
- return false;
- }
- } break;
- case 3 : case 9 : { // Triangle
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2);
- if (i0>=_width || i1>=_width || i2>=_width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) refers to invalid vertex indices (%u,%u,%u) in "
- "triangle primitive [%u]",
- _width,primitives._width,i0,i1,i2,l);
- return false;
- }
- } break;
- case 4 : case 12 : { // Quadrangle
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2),
- i3 = (unsigned int)primitive(3);
- if (i0>=_width || i1>=_width || i2>=_width || i3>=_width) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in "
- "quadrangle primitive [%u]",
- _width,primitives._width,i0,i1,i2,i3,l);
- return false;
- }
- } break;
- default :
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines an invalid primitive [%u] of size %u",
- _width,primitives._width,l,(unsigned int)psiz);
- return false;
- }
- }
- // Check consistency of colors.
- cimglist_for(colors,c) {
- const CImg<tc>& color = colors[c];
- if (!color) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines no color for primitive [%u]",
- _width,primitives._width,c);
- return false;
- }
- }
- // Check consistency of light texture.
- if (colors._width>primitives._width) {
- const CImg<tc> &light = colors.back();
- if (!light || light._depth>1) {
- if (error_message) cimg_sprintf(error_message,
- "3D object (%u,%u) defines an invalid light texture (%u,%u,%u,%u)",
- _width,primitives._width,light._width,
- light._height,light._depth,light._spectrum);
- return false;
- }
- }
- return true;
- }
- //! Test if image instance represents a valid serialization of a 3D object.
- /**
- Return \c true if the image instance represents a valid serialization of a 3D object, and \c false otherwise.
- \param full_check Tells if full checking of the instance must be performed.
- \param[out] error_message C-string to contain the error message, if the test does not succeed.
- \note
- - Set \c full_check to \c false to speed-up the 3D object checking. In this case, only the size of
- each 3D object component is checked.
- - Size of the string \c error_message should be at least 128-bytes long, to be able to contain the error message.
- **/
- bool is_CImg3d(const bool full_check=true, char *const error_message=0) const {
- if (error_message) *error_message = 0;
- // Check instance dimension and header.
- if (_width!=1 || _height<8 || _depth!=1 || _spectrum!=1) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d has invalid dimensions (%u,%u,%u,%u)",
- _width,_height,_depth,_spectrum);
- return false;
- }
- const T *ptrs = _data, *const ptre = end();
- if (!_is_CImg3d(*(ptrs++),'C') || !_is_CImg3d(*(ptrs++),'I') || !_is_CImg3d(*(ptrs++),'m') ||
- !_is_CImg3d(*(ptrs++),'g') || !_is_CImg3d(*(ptrs++),'3') || !_is_CImg3d(*(ptrs++),'d')) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d header not found");
- return false;
- }
- const unsigned int
- nb_points = cimg::float2uint((float)*(ptrs++)),
- nb_primitives = cimg::float2uint((float)*(ptrs++));
- // Check consistency of number of vertices / primitives.
- if (!full_check) {
- const ulongT minimal_size = 8UL + 3*nb_points + 6*nb_primitives;
- if (_data + minimal_size>ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) has only %lu values, while at least %lu values were expected",
- nb_points,nb_primitives,(unsigned long)size(),(unsigned long)minimal_size);
- return false;
- }
- }
- // Check consistency of vertex data.
- if (!nb_points) {
- if (nb_primitives) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines no vertices but %u primitives",
- nb_points,nb_primitives,nb_primitives);
- return false;
- }
- if (ptrs!=ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) is an empty object but contains %u value%s "
- "more than expected",
- nb_points,nb_primitives,(unsigned int)(ptre - ptrs),(ptre - ptrs)>1?"s":"");
- return false;
- }
- return true;
- }
- if (ptrs + 3*nb_points>ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines only %u vertices data",
- nb_points,nb_primitives,(unsigned int)(ptre - ptrs)/3);
- return false;
- }
- ptrs+=3*nb_points;
- // Check consistency of primitive data.
- if (ptrs==ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines %u vertices but no primitive",
- nb_points,nb_primitives,nb_points);
- return false;
- }
- if (!full_check) return true;
- for (unsigned int p = 0; p<nb_primitives; ++p) {
- const unsigned int nb_inds = (unsigned int)*(ptrs++);
- switch (nb_inds) {
- case 1 : { // Point
- const unsigned int i0 = cimg::float2uint((float)*(ptrs++));
- if (i0>=nb_points) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid vertex index %u in point primitive [%u]",
- nb_points,nb_primitives,i0,p);
- return false;
- }
- } break;
- case 5 : { // Sphere
- const unsigned int
- i0 = cimg::float2uint((float)*(ptrs++)),
- i1 = cimg::float2uint((float)*(ptrs++));
- ptrs+=3;
- if (i0>=nb_points || i1>=nb_points) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in "
- "sphere primitive [%u]",
- nb_points,nb_primitives,i0,i1,p);
- return false;
- }
- } break;
- case 2 : case 6 : { // Segment
- const unsigned int
- i0 = cimg::float2uint((float)*(ptrs++)),
- i1 = cimg::float2uint((float)*(ptrs++));
- if (nb_inds==6) ptrs+=4;
- if (i0>=nb_points || i1>=nb_points) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u) in "
- "segment primitive [%u]",
- nb_points,nb_primitives,i0,i1,p);
- return false;
- }
- } break;
- case 3 : case 9 : { // Triangle
- const unsigned int
- i0 = cimg::float2uint((float)*(ptrs++)),
- i1 = cimg::float2uint((float)*(ptrs++)),
- i2 = cimg::float2uint((float)*(ptrs++));
- if (nb_inds==9) ptrs+=6;
- if (i0>=nb_points || i1>=nb_points || i2>=nb_points) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u) in "
- "triangle primitive [%u]",
- nb_points,nb_primitives,i0,i1,i2,p);
- return false;
- }
- } break;
- case 4 : case 12 : { // Quadrangle
- const unsigned int
- i0 = cimg::float2uint((float)*(ptrs++)),
- i1 = cimg::float2uint((float)*(ptrs++)),
- i2 = cimg::float2uint((float)*(ptrs++)),
- i3 = cimg::float2uint((float)*(ptrs++));
- if (nb_inds==12) ptrs+=8;
- if (i0>=nb_points || i1>=nb_points || i2>=nb_points || i3>=nb_points) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid vertex indices (%u,%u,%u,%u) in "
- "quadrangle primitive [%u]",
- nb_points,nb_primitives,i0,i1,i2,i3,p);
- return false;
- }
- } break;
- default :
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines an invalid primitive [%u] of size %u",
- nb_points,nb_primitives,p,nb_inds);
- return false;
- }
- if (ptrs>ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) has incomplete primitive data for primitive [%u], "
- "%u values missing",
- nb_points,nb_primitives,p,(unsigned int)(ptrs - ptre));
- return false;
- }
- }
- // Check consistency of color data.
- if (ptrs==ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines no color/texture data",
- nb_points,nb_primitives);
- return false;
- }
- for (unsigned int c = 0; c<nb_primitives; ++c) {
- if (*(ptrs++)!=(T)-128) ptrs+=2;
- else if ((ptrs+=3)<ptre) {
- const unsigned int
- w = (unsigned int)*(ptrs - 3),
- h = (unsigned int)*(ptrs - 2),
- s = (unsigned int)*(ptrs - 1);
- if (!h && !s) {
- if (w>=c) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid shared sprite/texture index %u "
- "for primitive [%u]",
- nb_points,nb_primitives,w,c);
- return false;
- }
- } else ptrs+=w*h*s;
- }
- if (ptrs>ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) has incomplete color/texture data for primitive [%u], "
- "%u values missing",
- nb_points,nb_primitives,c,(unsigned int)(ptrs - ptre));
- return false;
- }
- }
- // Check consistency of opacity data.
- if (ptrs==ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) defines no opacity data",
- nb_points,nb_primitives);
- return false;
- }
- for (unsigned int o = 0; o<nb_primitives; ++o) {
- if (*(ptrs++)==(T)-128 && (ptrs+=3)<ptre) {
- const unsigned int
- w = (unsigned int)*(ptrs - 3),
- h = (unsigned int)*(ptrs - 2),
- s = (unsigned int)*(ptrs - 1);
- if (!h && !s) {
- if (w>=o) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) refers to invalid shared opacity index %u "
- "for primitive [%u]",
- nb_points,nb_primitives,w,o);
- return false;
- }
- } else ptrs+=w*h*s;
- }
- if (ptrs>ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) has incomplete opacity data for primitive [%u]",
- nb_points,nb_primitives,o);
- return false;
- }
- }
- // Check end of data.
- if (ptrs<ptre) {
- if (error_message) cimg_sprintf(error_message,
- "CImg3d (%u,%u) contains %u value%s more than expected",
- nb_points,nb_primitives,(unsigned int)(ptre - ptrs),(ptre - ptrs)>1?"s":"");
- return false;
- }
- return true;
- }
- static bool _is_CImg3d(const T val, const char c) {
- return val>=(T)c && val<(T)(c + 1);
- }
- //@}
- //-------------------------------------
- //
- //! \name Mathematical Functions
- //@{
- //-------------------------------------
- // Define the math formula parser/compiler and expression evaluator.
- struct _cimg_math_parser {
- CImg<doubleT> mem;
- CImg<intT> memtype, memmerge;
- CImgList<ulongT> _code, &code, code_begin, code_end,
- _code_begin_t, &code_begin_t, _code_end_t, &code_end_t;
- CImg<ulongT> opcode;
- const CImg<ulongT> *p_code_end, *p_code;
- const CImg<ulongT> *const p_break;
- CImg<charT> expr, pexpr;
- const CImg<T>& imgin;
- CImg<T> &imgout;
- CImgList<T>& imglist;
- CImg<doubleT> _img_stats, &img_stats, constcache_vals;
- CImgList<doubleT> _list_stats, &list_stats, _list_median, &list_median, _list_norm, &list_norm;
- CImg<uintT> mem_img_stats, constcache_inds;
- CImg<uintT> level, variable_pos, reserved_label;
- CImgList<charT> variable_def, macro_def, macro_body;
- CImgList<boolT> macro_body_is_string;
- char *user_macro;
- unsigned int mempos, mem_img_median, mem_img_norm, mem_img_index, debug_indent, result_dim, break_type,
- constcache_size;
- bool is_parallelizable, is_end_code, is_fill, return_new_comp, need_input_copy;
- double *result;
- cimg_uint64 rng;
- const char *const calling_function, *s_op, *ss_op;
- typedef double (*mp_func)(_cimg_math_parser&);
- #define _cimg_mp_is_scalar(arg) (memtype[arg]<2) // Is scalar value?
- #define _cimg_mp_is_const_scalar(arg) (memtype[arg]==1) // Is const scalar?
- #define _cimg_mp_is_vector(arg) (memtype[arg]>1) // Is vector?
- #define _cimg_mp_is_comp(arg) (!memtype[arg]) // Is computation value?
- #define _cimg_mp_is_reserved(arg) (memtype[arg]==-1) // Is scalar and reserved (e.g. variable)?
- #define _cimg_mp_size(arg) (_cimg_mp_is_scalar(arg)?0U:(unsigned int)memtype[arg] - 1) // Size (0=scalar, N>0=vectorN)
- #define _cimg_mp_calling_function s_calling_function()._data
- #define _cimg_mp_op(s) s_op = s; ss_op = ss
- #define _cimg_mp_check_type(arg,n_arg,mode,N) check_type(arg,n_arg,mode,N,ss,se,saved_char)
- #define _cimg_mp_check_const_scalar(arg,n_arg,mode) check_const_scalar(arg,n_arg,mode,ss,se,saved_char)
- #define _cimg_mp_check_const_index(arg) check_const_index(arg,ss,se,saved_char)
- #define _cimg_mp_check_matrix_square(arg,n_arg) check_matrix_square(arg,n_arg,ss,se,saved_char)
- #define _cimg_mp_check_list() check_list(ss,se,saved_char)
- #define _cimg_mp_defunc(mp) (*(mp_func)(*(mp).opcode))(mp)
- #define _cimg_mp_return(x) { *se = saved_char; s_op = previous_s_op; ss_op = previous_ss_op; return x; }
- #define _cimg_mp_return_nan() _cimg_mp_return(_cimg_mp_slot_nan)
- #define _cimg_mp_const_scalar(val) _cimg_mp_return(const_scalar((double)(val)))
- #define _cimg_mp_scalar0(op) _cimg_mp_return(scalar0(op))
- #define _cimg_mp_scalar1(op,i1) _cimg_mp_return(scalar1(op,i1))
- #define _cimg_mp_scalar2(op,i1,i2) _cimg_mp_return(scalar2(op,i1,i2))
- #define _cimg_mp_scalar3(op,i1,i2,i3) _cimg_mp_return(scalar3(op,i1,i2,i3))
- #define _cimg_mp_scalar4(op,i1,i2,i3,i4) _cimg_mp_return(scalar4(op,i1,i2,i3,i4))
- #define _cimg_mp_scalar5(op,i1,i2,i3,i4,i5) _cimg_mp_return(scalar5(op,i1,i2,i3,i4,i5))
- #define _cimg_mp_scalar6(op,i1,i2,i3,i4,i5,i6) _cimg_mp_return(scalar6(op,i1,i2,i3,i4,i5,i6))
- #define _cimg_mp_scalar7(op,i1,i2,i3,i4,i5,i6,i7) _cimg_mp_return(scalar7(op,i1,i2,i3,i4,i5,i6,i7))
- #define _cimg_mp_vector1_v(op,i1) _cimg_mp_return(vector1_v(op,i1))
- #define _cimg_mp_vector2_sv(op,i1,i2) _cimg_mp_return(vector2_sv(op,i1,i2))
- #define _cimg_mp_vector2_vs(op,i1,i2) _cimg_mp_return(vector2_vs(op,i1,i2))
- #define _cimg_mp_vector2_vv(op,i1,i2) _cimg_mp_return(vector2_vv(op,i1,i2))
- #define _cimg_mp_vector3_vss(op,i1,i2,i3) _cimg_mp_return(vector3_vss(op,i1,i2,i3))
- #define _cimg_mp_strerr \
- *se = saved_char; \
- for (s0 = ss; s0>expr._data && *s0!=';'; --s0) {} \
- if (*s0==';') ++s0; \
- while (cimg::is_blank(*s0)) ++s0; \
- cimg::strellipsize(s0,64)
- // Constructors / Destructors.
- ~_cimg_math_parser() {
- cimg::srand(rng);
- }
- _cimg_math_parser(const char *const expression, const char *const funcname=0,
- const CImg<T>& img_input=CImg<T>::const_empty(), CImg<T> *const img_output=0,
- CImgList<T> *const list_images=0, const bool _is_fill=false):
- code(_code),code_begin_t(_code_begin_t),code_end_t(_code_end_t),
- p_break((CImg<ulongT>*)(cimg_ulong)-2),imgin(img_input),
- imgout(img_output?*img_output:CImg<T>::empty()),imglist(list_images?*list_images:CImgList<T>::empty()),
- img_stats(_img_stats),list_stats(_list_stats),list_median(_list_median),list_norm(_list_norm),user_macro(0),
- mem_img_median(~0U),mem_img_norm(~0U),mem_img_index(~0U),debug_indent(0),result_dim(0),break_type(0),
- constcache_size(0),is_parallelizable(true),is_fill(_is_fill),need_input_copy(false),
- rng((cimg::_rand(),cimg::rng())),calling_function(funcname?funcname:"cimg_math_parser") {
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- if (!expression || !*expression)
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Empty expression.",
- pixel_type(),_cimg_mp_calling_function);
- const char *_expression = expression;
- while (*_expression && (cimg::is_blank(*_expression) || *_expression==';')) ++_expression;
- CImg<charT>::string(_expression).move_to(expr);
- char *ps = &expr.back() - 1;
- while (ps>expr._data && (cimg::is_blank(*ps) || *ps==';')) --ps;
- *(++ps) = 0; expr._width = (unsigned int)(ps - expr._data + 1);
- // Ease the retrieval of previous non-space characters afterwards.
- pexpr.assign(expr._width);
- char c, *pe = pexpr._data;
- for (ps = expr._data, c = ' '; *ps; ++ps) {
- if (!cimg::is_blank(*ps)) c = *ps; else *ps = ' ';
- *(pe++) = c;
- }
- *pe = 0;
- level = get_level(expr);
- // Init constant values.
- #define _cimg_mp_interpolation (reserved_label[30]!=~0U?reserved_label[30]:0)
- #define _cimg_mp_boundary (reserved_label[31]!=~0U?reserved_label[31]:0)
- #define _cimg_mp_slot_t 17
- #define _cimg_mp_slot_nan 29
- #define _cimg_mp_slot_x 30
- #define _cimg_mp_slot_y 31
- #define _cimg_mp_slot_z 32
- #define _cimg_mp_slot_c 33
- mem.assign(96);
- for (unsigned int i = 0; i<=10; ++i) mem[i] = (double)i; // mem[0-10] = 0...10
- for (unsigned int i = 1; i<=5; ++i) mem[i + 10] = -(double)i; // mem[11-15] = -1...-5
- mem[16] = 0.5;
- mem[_cimg_mp_slot_t] = 0; // thread_id
- mem[18] = (double)imgin._width; // w
- mem[19] = (double)imgin._height; // h
- mem[20] = (double)imgin._depth; // d
- mem[21] = (double)imgin._spectrum; // s
- mem[22] = (double)imgin._is_shared; // r
- mem[23] = (double)imgin._width*imgin._height; // wh
- mem[24] = (double)imgin._width*imgin._height*imgin._depth; // whd
- mem[25] = (double)imgin._width*imgin._height*imgin._depth*imgin._spectrum; // whds
- mem[26] = (double)imglist._width; // l
- mem[27] = std::exp(1.); // e
- mem[28] = cimg::PI; // pi
- mem[_cimg_mp_slot_nan] = cimg::type<double>::nan(); // nan
- // Set value property :
- // { -1 = reserved (e.g. variable) | 0 = computation value |
- // 1 = compile-time constant | N>1 = constant ptr to vector[N-1] }.
- memtype.assign(mem._width,1,1,1,0);
- for (unsigned int i = 0; i<_cimg_mp_slot_x; ++i) memtype[i] = 1;
- memtype[_cimg_mp_slot_t] = memtype[_cimg_mp_slot_x] = memtype[_cimg_mp_slot_y] =
- memtype[_cimg_mp_slot_z] = memtype[_cimg_mp_slot_c] = -1;
- mempos = _cimg_mp_slot_c + 1;
- variable_pos.assign(8);
- reserved_label.assign(128,1,1,1,~0U);
- // reserved_label[0-31] are used to store the memory index of these variables:
- // [0] = wh, [1] = whd, [2] = whds, [3] = pi, [4] = im, [5] = iM, [6] = ia, [7] = iv,
- // [8] = is, [9] = ip, [10] = ic, [11] = in, [12] = xm, [13] = ym, [14] = zm, [15] = cm, [16] = xM,
- // [17] = yM, [18] = zM, [19] = cM, [20] = i0...[29] = i9, [30] = interpolation, [31] = boundary
- // Compile expression into a sequence of opcodes.
- s_op = ""; ss_op = expr._data;
- const unsigned int ind_result = compile(expr._data,expr._data + expr._width - 1,0,0,0);
- if (!_cimg_mp_is_const_scalar(ind_result)) {
- if (_cimg_mp_is_vector(ind_result))
- CImg<doubleT>(&mem[ind_result] + 1,_cimg_mp_size(ind_result),1,1,1,true).
- fill(cimg::type<double>::nan());
- else if (ind_result!=_cimg_mp_slot_t) mem[ind_result] = cimg::type<double>::nan();
- }
- // Free resources used for compiling expression and prepare evaluation.
- result_dim = _cimg_mp_size(ind_result);
- if (mem._width>=256 && mem._width - mempos>=mem._width/2) mem.resize(mempos,1,1,1,-1);
- result = mem._data + ind_result;
- memtype.assign();
- constcache_vals.assign();
- constcache_inds.assign();
- level.assign();
- variable_pos.assign();
- reserved_label.assign();
- expr.assign();
- pexpr.assign();
- opcode.assign();
- opcode._is_shared = true;
- // Execute begin() bloc if any specified.
- if (code_begin) {
- mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
- p_code_end = code_begin.end();
- for (p_code = code_begin; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- }
- p_code_end = code.end();
- }
- _cimg_math_parser():
- code(_code),code_begin_t(_code_begin_t),code_end_t(_code_end_t),
- p_code_end(0),p_break((CImg<ulongT>*)(cimg_ulong)-2),
- imgin(CImg<T>::const_empty()),imgout(CImg<T>::empty()),imglist(CImgList<T>::empty()),
- img_stats(_img_stats),list_stats(_list_stats),list_median(_list_median),list_norm(_list_norm),debug_indent(0),
- result_dim(0),break_type(0),constcache_size(0),is_parallelizable(true),is_fill(false),
- need_input_copy(false),rng(0),calling_function(0) {
- mem.assign(1 + _cimg_mp_slot_c,1,1,1,0); // Allow to skip 'is_empty?' test in operator()()
- result = mem._data;
- }
- _cimg_math_parser(const _cimg_math_parser& mp):
- mem(mp.mem),code(mp.code),code_begin_t(mp.code_begin_t),code_end_t(mp.code_end_t),
- p_code_end(mp.p_code_end),p_break(mp.p_break),
- imgin(mp.imgin),imgout(mp.imgout),imglist(mp.imglist),
- img_stats(mp.img_stats),list_stats(mp.list_stats),list_median(mp.list_median),list_norm(mp.list_norm),
- debug_indent(0),result_dim(mp.result_dim),break_type(0),constcache_size(0),
- is_parallelizable(mp.is_parallelizable),is_fill(mp.is_fill),
- need_input_copy(mp.need_input_copy),result(mem._data + (mp.result - mp.mem._data)),
- rng((cimg::_rand(),cimg::rng())),calling_function(0) {
- #if cimg_use_openmp!=0
- mem[_cimg_mp_slot_t] = (double)omp_get_thread_num();
- rng+=omp_get_thread_num();
- #endif
- opcode.assign();
- opcode._is_shared = true;
- }
- // Compilation procedure.
- unsigned int compile(char *ss, char *se, const unsigned int depth, unsigned int *const p_ref,
- const unsigned char bloc_flags) {
- if (depth>256) {
- cimg::strellipsize(expr,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Call stack overflow (infinite recursion?), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- (ss - 4)>expr._data?ss - 4:expr._data);
- }
- char c1, c2;
- // Simplify expression when possible.
- do {
- c2 = 0;
- if (ss<se) {
- while (*ss && (cimg::is_blank(*ss) || *ss==';')) ++ss; // Remove leading blanks and ';'
- while (se>ss && (cimg::is_blank(c1 = *(se - 1)) || c1==';')) --se; // Remove trailing blanks and ';'
- }
- while (*ss=='(' && *(se - 1)==')' && std::strchr(ss,')')==se - 1) { // Remove useless start/end parentheses
- ++ss; --se; c2 = 1;
- }
- if (*ss=='_' && ss + 1<se && ss[1]=='(') { // Remove leading '_(something)' comment.
- const unsigned int clevel = level[ss - expr._data];
- ss+=2;
- while (ss<se && (*ss!=')' || level[ss - expr._data]!=clevel)) ++ss;
- if (ss<se) ++ss;
- if (ss>=se) return _cimg_mp_slot_nan;
- c2 = 1;
- }
- } while (c2 && ss<se);
- if (se<=ss || !*ss) {
- cimg::strellipsize(expr,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s Missing %s, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
- *s_op=='F'?"argument":"item",
- ss_op);
- }
- static const size_t siz_ref = 7*sizeof(unsigned int);
- const char *const previous_s_op = s_op, *const previous_ss_op = ss_op;
- const unsigned int depth1 = depth + 1;
- unsigned int pos, p1, p2, p3, arg1, arg2, arg3, arg4, arg5, arg6;
- char
- *const se1 = se - 1, *const se2 = se - 2, *const se3 = se - 3,
- *const ss1 = ss + 1, *const ss2 = ss + 2, *const ss3 = ss + 3, *const ss4 = ss + 4,
- *const ss5 = ss + 5, *const ss6 = ss + 6, *const ss7 = ss + 7, *const ss8 = ss + 8,
- *s, *ps, *ns, *s0, *s1, *s2, *s3, sep = 0, end = 0;
- double val = 0, val1, val2;
- mp_func op;
- return_new_comp = false;
- // Bits of 'bloc_flags' tell about in which code bloc we currently are:
- // 0: critical(), 1: begin(), 2: begin_t(), 3: end(), 4: end_t().
- const bool is_inside_critical = (bool)(bloc_flags&1);
- // 'p_ref' is a 'unsigned int[7]' used to return a reference to an image or vector value
- // linked to the returned memory slot (reference that cannot be determined at compile time).
- // p_ref[0] can be { 0 = scalar (unlinked) | 1 = vector value | 2 = image value (offset) |
- // 3 = image value (coordinates) | 4 = image value as a vector (offsets) |
- // 5 = image value as a vector (coordinates) }.
- // Depending on p_ref[0], the remaining p_ref[k] have the following meaning:
- // When p_ref[0]==0, p_ref is actually unlinked.
- // When p_ref[0]==1, p_ref = [ 1, vector_ind, offset ].
- // When p_ref[0]==2, p_ref = [ 2, image_ind (or ~0U), is_relative, offset ].
- // When p_ref[0]==3, p_ref = [ 3, image_ind (or ~0U), is_relative, x, y, z, c ].
- // When p_ref[0]==4, p_ref = [ 4, image_ind (or ~0U), is_relative, offset ].
- // When p_ref[0]==5, p_ref = [ 5, image_ind (or ~0U), is_relative, x, y, z ].
- 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; }
- const char saved_char = *se; *se = 0;
- const unsigned int clevel = level[ss - expr._data], clevel1 = clevel + 1;
- bool is_sth, is_relative;
- CImg<uintT> ref;
- CImg<charT> variable_name;
- CImgList<ulongT> l_opcode;
- // Look for a single value or a pre-defined variable.
- int nb = 0;
- s = ss + (*ss=='+' || *ss=='-'?1:0);
- if (*s=='i' || *s=='I' || *s=='n' || *s=='N') { // Particular cases : +/-NaN and +/-Inf
- is_sth = *ss=='-';
- if (!cimg::strcasecmp(s,"inf")) { val = cimg::type<double>::inf(); nb = 1; }
- else if (!cimg::strcasecmp(s,"nan")) { val = cimg::type<double>::nan(); nb = 1; }
- if (nb==1 && is_sth) val = -val;
- } else if (*s=='0' && (s[1]=='x' || s[1]=='X')) { // Hexadecimal number
- is_sth = *ss=='-';
- if (cimg_sscanf(s + 2,"%x%c",&arg1,&sep)==1) {
- nb = 1;
- val = (double)arg1;
- if (is_sth) val = -val;
- }
- }
- if (!nb) nb = cimg_sscanf(ss,"%lf%c%c",&val,&(sep=0),&(end=0));
- if (nb==1) _cimg_mp_const_scalar(val);
- if (nb==2 && sep=='%') _cimg_mp_const_scalar(val/100);
- if (ss1==se) switch (*ss) { // One-char reserved variable
- case 'c' : _cimg_mp_return(reserved_label[(int)'c']!=~0U?reserved_label[(int)'c']:_cimg_mp_slot_c);
- case 'd' : _cimg_mp_return(reserved_label[(int)'d']!=~0U?reserved_label[(int)'d']:20);
- case 'e' : _cimg_mp_return(reserved_label[(int)'e']!=~0U?reserved_label[(int)'e']:27);
- case 'h' : _cimg_mp_return(reserved_label[(int)'h']!=~0U?reserved_label[(int)'h']:19);
- case 'k' :
- if (reserved_label[(int)'k']!=~0U) _cimg_mp_return(reserved_label[(int)'k']);
- pos = get_mem_img_index();
- if (pos!=~0U) _cimg_mp_return(pos);
- _cimg_mp_return_nan();
- case 'l' : _cimg_mp_return(reserved_label[(int)'l']!=~0U?reserved_label[(int)'l']:26);
- case 'r' : _cimg_mp_return(reserved_label[(int)'r']!=~0U?reserved_label[(int)'r']:22);
- case 's' : _cimg_mp_return(reserved_label[(int)'s']!=~0U?reserved_label[(int)'s']:21);
- case 't' : _cimg_mp_return(reserved_label[(int)'t']!=~0U?reserved_label[(int)'t']:_cimg_mp_slot_t);
- case 'n' :
- if (reserved_label[(int)'n']!=~0U) _cimg_mp_return(reserved_label[(int)'n']);
- #if cimg_use_openmp!=0
- _cimg_mp_const_scalar((double)omp_get_max_threads());
- #else
- _cimg_mp_return(1);
- #endif
- case 'w' : _cimg_mp_return(reserved_label[(int)'w']!=~0U?reserved_label[(int)'w']:18);
- case 'x' : _cimg_mp_return(reserved_label[(int)'x']!=~0U?reserved_label[(int)'x']:_cimg_mp_slot_x);
- case 'y' : _cimg_mp_return(reserved_label[(int)'y']!=~0U?reserved_label[(int)'y']:_cimg_mp_slot_y);
- case 'z' : _cimg_mp_return(reserved_label[(int)'z']!=~0U?reserved_label[(int)'z']:_cimg_mp_slot_z);
- case 'u' :
- if (reserved_label[(int)'u']!=~0U) _cimg_mp_return(reserved_label[(int)'u']);
- _cimg_mp_scalar2(mp_u,0,1);
- case 'g' :
- if (reserved_label[(int)'g']!=~0U) _cimg_mp_return(reserved_label[(int)'g']);
- _cimg_mp_scalar0(mp_g);
- case 'i' :
- if (reserved_label[(int)'i']!=~0U) _cimg_mp_return(reserved_label[(int)'i']);
- _cimg_mp_scalar0(mp_i);
- case 'I' :
- _cimg_mp_op("Variable 'I'");
- if (reserved_label[(int)'I']!=~0U) _cimg_mp_return(reserved_label[(int)'I']);
- if (!imgin._spectrum) _cimg_mp_return(0);
- need_input_copy = true;
- pos = vector(imgin._spectrum);
- CImg<ulongT>::vector((ulongT)mp_Joff,pos,0,0,imgin._spectrum).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- case 'R' :
- if (reserved_label[(int)'R']!=~0U) _cimg_mp_return(reserved_label[(int)'R']);
- need_input_copy = true;
- _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,0,0,0);
- case 'G' :
- if (reserved_label[(int)'G']!=~0U) _cimg_mp_return(reserved_label[(int)'G']);
- need_input_copy = true;
- _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,1,0,0);
- case 'B' :
- if (reserved_label[(int)'B']!=~0U) _cimg_mp_return(reserved_label[(int)'B']);
- need_input_copy = true;
- _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,2,0,0);
- case 'A' :
- if (reserved_label[(int)'A']!=~0U) _cimg_mp_return(reserved_label[(int)'A']);
- need_input_copy = true;
- _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,3,0,0);
- }
- else if (ss2==se) { // Two-chars reserved variable
- arg1 = arg2 = ~0U;
- if (*ss=='w' && *ss1=='h') // wh
- _cimg_mp_return(reserved_label[0]!=~0U?reserved_label[0]:23);
- if (*ss=='p' && *ss1=='i') // pi
- _cimg_mp_return(reserved_label[3]!=~0U?reserved_label[3]:28);
- if (*ss=='i') {
- if (*ss1>='0' && *ss1<='9') { // i0...i9
- pos = 20 + *ss1 - '0';
- if (reserved_label[pos]!=~0U) _cimg_mp_return(reserved_label[pos]);
- need_input_copy = true;
- _cimg_mp_scalar6(mp_ixyzc,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,pos - 20,0,0);
- }
- switch (*ss1) {
- case 'm' : arg1 = 4; arg2 = 0; break; // im
- case 'M' : arg1 = 5; arg2 = 1; break; // iM
- case 'a' : arg1 = 6; arg2 = 2; break; // ia
- case 'v' : arg1 = 7; arg2 = 3; break; // iv
- case 's' : arg1 = 8; arg2 = 12; break; // is
- case 'p' : arg1 = 9; arg2 = 13; break; // ip
- case 'c' : // ic
- if (reserved_label[10]!=~0U) _cimg_mp_return(reserved_label[10]);
- if (mem_img_median==~0U) mem_img_median = imgin?const_scalar(imgin.median()):0;
- _cimg_mp_return(mem_img_median);
- break;
- case 'n' : // in
- if (reserved_label[11]!=~0U) _cimg_mp_return(reserved_label[11]);
- if (mem_img_norm==~0U) mem_img_norm = imgin?const_scalar(imgin.magnitude()):0;
- _cimg_mp_return(mem_img_norm);
- }
- }
- else if (*ss1=='m') switch (*ss) {
- case 'x' : arg1 = 12; arg2 = 4; break; // xm
- case 'y' : arg1 = 13; arg2 = 5; break; // ym
- case 'z' : arg1 = 14; arg2 = 6; break; // zm
- case 'c' : arg1 = 15; arg2 = 7; break; // cm
- }
- else if (*ss1=='M') switch (*ss) {
- case 'x' : arg1 = 16; arg2 = 8; break; // xM
- case 'y' : arg1 = 17; arg2 = 9; break; // yM
- case 'z' : arg1 = 18; arg2 = 10; break; // zM
- case 'c' : arg1 = 19; arg2 = 11; break; // cM
- }
- if (arg1!=~0U) {
- if (reserved_label[arg1]!=~0U) _cimg_mp_return(reserved_label[arg1]);
- if (!img_stats) {
- img_stats.assign(1,14,1,1,0).fill(imgin.get_stats(),false);
- mem_img_stats.assign(1,14,1,1,~0U);
- }
- if (mem_img_stats[arg2]==~0U) mem_img_stats[arg2] = const_scalar(img_stats[arg2]);
- _cimg_mp_return(mem_img_stats[arg2]);
- }
- } else if (ss3==se) { // Three-chars reserved variable
- if (*ss=='w' && *ss1=='h' && *ss2=='d') // whd
- _cimg_mp_return(reserved_label[1]!=~0U?reserved_label[1]:24);
- } else if (ss4==se) { // Four-chars reserved variable
- if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='s') // whds
- _cimg_mp_return(reserved_label[2]!=~0U?reserved_label[2]:25);
- }
- pos = ~0U;
- is_sth = false;
- for (s0 = ss, s = ss1; s<se1; ++s)
- if (*s==';' && level[s - expr._data]==clevel) { // Separator ';'
- is_end_code = false;
- arg1 = compile(s0,s++,depth,0,bloc_flags);
- if (!is_end_code) pos = arg1; // 'end()' and 'end_t()' return void
- is_sth = true;
- while (*s && (cimg::is_blank(*s) || *s==';')) ++s;
- s0 = s;
- }
- if (is_sth) {
- is_end_code = false;
- arg1 = compile(s0,se,depth,p_ref,bloc_flags);
- if (!is_end_code) pos = arg1; // 'end()' and 'end_t()' return void
- _cimg_mp_return(pos!=~0U?pos:_cimg_mp_slot_nan);
- }
- // Declare / assign variable, vector value or image value.
- for (s = ss1, ps = ss, ns = ss2; s<se1; ++s, ++ps, ++ns)
- if (*s=='=' && *ns!='=' && *ps!='=' && *ps!='>' && *ps!='<' && *ps!='!' &&
- *ps!='+' && *ps!='-' && *ps!='*' && *ps!='/' && *ps!='%' &&
- *ps!='>' && *ps!='<' && *ps!='&' && *ps!='|' && *ps!='^' &&
- level[s - expr._data]==clevel) {
- variable_name.assign(ss,(unsigned int)(s + 1 - ss)).back() = 0;
- cimg::strpare(variable_name,false,true);
- const unsigned int l_variable_name = (unsigned int)std::strlen(variable_name);
- char *const ve1 = ss + l_variable_name - 1;
- _cimg_mp_op("Operator '='");
- // Assign image value (direct).
- if (l_variable_name>2 && (*ss=='i' || *ss=='j' || *ss=='I' || *ss=='J') && (*ss1=='(' || *ss1=='[') &&
- (reserved_label[(int)*ss]==~0U || *ss1=='(' || !_cimg_mp_is_vector(reserved_label[(int)*ss]))) {
- is_relative = *ss=='j' || *ss=='J';
- if (*ss1=='[' && *ve1==']') { // i/j/I/J[_#ind,offset] = value
- if (!is_inside_critical) is_parallelizable = false;
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<ve1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss2; }
- arg1 = compile(s0,ve1,depth1,0,bloc_flags); // Offset
- _cimg_mp_check_type(arg1,0,1,0);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
- _cimg_mp_check_type(arg2,2,*ss>='i'?1:3,0);
- if (_cimg_mp_is_vector(arg2)) {
- if (p1!=~0U) {
- _cimg_mp_check_const_index(p1);
- p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- p2 = imglist[p3]._spectrum;
- } else p2 = imgin._spectrum;
- if (!p2) _cimg_mp_return(0);
- _cimg_mp_check_type(arg2,2,2,p2);
- } else p2 = 0;
- if (p_ref) {
- *p_ref = _cimg_mp_is_vector(arg2)?4:2;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- if (_cimg_mp_is_vector(arg2))
- set_reserved_vector(arg2); // Prevent from being used in further optimization
- else if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- }
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg2);
- if (*ss>='i')
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
- arg2,p1,arg1).move_to(code);
- else if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
- arg2,p1,arg1).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg2,p1,arg1,_cimg_mp_size(arg2)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg2);
- if (*ss>='i')
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
- arg2,arg1).move_to(code);
- else if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
- arg2,arg1).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg2,arg1,_cimg_mp_size(arg2)).move_to(code);
- }
- _cimg_mp_return(arg2);
- }
- if (*ss1=='(' && *ve1==')') { // i/j/I/J(_#ind,_x,_y,_z,_c) = value
- if (!is_inside_critical) is_parallelizable = false;
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<ve1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss2; }
- arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
- arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
- arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
- arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
- arg5 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
- _cimg_mp_check_type(arg5,2,*ss>='i'?1:3,0);
- if (s0<ve1) { // X or [ X,_Y,_Z,_C ]
- s1 = s0; while (s1<ve1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
- p2 = _cimg_mp_size(arg1); // Vector size
- ++arg1;
- if (p2>1) {
- arg2 = arg1 + 1;
- if (p2>2) {
- arg3 = arg2 + 1;
- if (p2>3) arg4 = arg3 + 1;
- }
- }
- } else if (s1<ve1) { // Y
- s2 = ++s1; while (s2<ve1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,0,bloc_flags);
- if (s2<ve1) { // Z
- s3 = ++s2; while (s3<ve1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(s2,s3,depth1,0,bloc_flags);
- if (s3<ve1) arg4 = compile(++s3,ve1,depth1,0,bloc_flags); // C
- }
- }
- }
- if (_cimg_mp_is_vector(arg5)) {
- if (p1!=~0U) {
- _cimg_mp_check_const_index(p1);
- p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- p2 = imglist[p3]._spectrum;
- } else p2 = imgin._spectrum;
- if (!p2) _cimg_mp_return(0);
- _cimg_mp_check_type(arg5,2,2,p2);
- } else p2 = 0;
- if (p_ref) {
- *p_ref = _cimg_mp_is_vector(arg5)?5:3;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- p_ref[4] = arg2;
- p_ref[5] = arg3;
- p_ref[6] = arg4;
- if (_cimg_mp_is_vector(arg5))
- set_reserved_vector(arg5); // Prevent from being used in further optimization
- else if (_cimg_mp_is_comp(arg5)) memtype[arg5] = -1;
- if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1;
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
- if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
- if (_cimg_mp_is_comp(arg4)) memtype[arg4] = -1;
- }
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg5);
- if (*ss>='i')
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
- arg5,p1,arg1,arg2,arg3,arg4).move_to(code);
- else if (_cimg_mp_is_scalar(arg5))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
- arg5,p1,arg1,arg2,arg3).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg5,p1,arg1,arg2,arg3,_cimg_mp_size(arg5)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg5);
- if (*ss>='i')
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
- arg5,arg1,arg2,arg3,arg4).move_to(code);
- else if (_cimg_mp_is_scalar(arg5))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
- arg5,arg1,arg2,arg3).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg5,arg1,arg2,arg3,_cimg_mp_size(arg5)).move_to(code);
- }
- _cimg_mp_return(arg5);
- }
- }
- // Assign vector value (direct).
- if (l_variable_name>3 && *ve1==']' && *ss!='[') {
- s0 = ve1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
- if (s0>ss && is_varname(ss,s0 - ss)) {
- variable_name[s0 - ss] = 0; // Remove brackets in variable name
- get_variable_pos(variable_name,arg1,arg2);
- arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U; // Vector slot
- if (arg1==~0U || _cimg_mp_is_scalar(arg1))
- compile(ss,s0,depth1,0,bloc_flags); // Variable does not exist or is not a vector -> error
- arg2 = compile(++s0,ve1,depth1,0,bloc_flags); // Index
- arg3 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
- _cimg_mp_check_type(arg3,2,1,0);
- if (_cimg_mp_is_const_scalar(arg2)) { // Constant index -> return corresponding variable slot directly
- nb = (int)mem[arg2];
- if (nb>=0 && nb<(int)_cimg_mp_size(arg1)) {
- arg1+=nb + 1;
- CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg3).move_to(code);
- _cimg_mp_return(arg1);
- }
- compile(ss,s,depth1,0,bloc_flags); // Out-of-bounds reference -> error
- }
- // Case of non-constant index -> return assigned value + linked reference
- if (p_ref) {
- *p_ref = 1;
- p_ref[1] = arg1;
- p_ref[2] = arg2;
- if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1; // Prevent from being used in further optimization
- if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
- }
- CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg3,arg1,(ulongT)_cimg_mp_size(arg1),arg2).
- move_to(code);
- _cimg_mp_return(arg3);
- }
- }
- // Assign user-defined macro.
- if (l_variable_name>2 && *ve1==')' && *ss!='(') {
- s0 = ve1; while (s0>ss && *s0!='(') --s0;
- if (is_varname(ss,s0 - ss) && std::strncmp(variable_name,"debug(",6) &&
- std::strncmp(variable_name,"print(",6)) { // Valid macro name
- s0 = variable_name._data + (s0 - ss);
- *s0 = 0;
- s1 = variable_name._data + l_variable_name - 1; // Pointer to closing parenthesis
- CImg<charT>(variable_name._data,(unsigned int)(s0 - variable_name._data + 1)).move_to(macro_def,0);
- ++s; while (*s && cimg::is_blank(*s)) ++s;
- CImg<charT>(s,(unsigned int)(se - s + 1)).move_to(macro_body,0);
- p1 = 1; // Index of current parsed argument
- for (s = s0 + 1; s<=s1; ++p1, s = ns + 1) { // Parse function arguments
- if (p1>24) {
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Too much specified arguments (>24) in macro "
- "definition '%s()', in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- variable_name._data,s0);
- }
- while (*s && cimg::is_blank(*s)) ++s;
- if (*s==')' && p1==1) break; // Function has no arguments
- s2 = s; // Start of the argument name
- is_sth = true; // is_valid_argument_name?
- if (*s>='0' && *s<='9') is_sth = false;
- else for (ns = s; ns<s1 && *ns!=',' && !cimg::is_blank(*ns); ++ns)
- if (!is_varchar(*ns)) { is_sth = false; break; }
- s3 = ns; // End of the argument name
- while (*ns && cimg::is_blank(*ns)) ++ns;
- if (!is_sth || s2==s3 || (*ns!=',' && ns!=s1)) {
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: %s name specified for argument %u when defining "
- "macro '%s()', in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- is_sth?"Empty":"Invalid",p1,
- variable_name._data,s0);
- }
- if (ns==s1 || *ns==',') { // New argument found
- *s3 = 0;
- p2 = (unsigned int)(s3 - s2); // Argument length
- for (ps = std::strstr(macro_body[0],s2); ps; ps = std::strstr(ps,s2)) { // Replace by arg number
- if (!((ps>macro_body[0]._data && is_varchar(*(ps - 1))) ||
- (ps + p2<macro_body[0].end() && is_varchar(*(ps + p2))))) {
- if (ps>macro_body[0]._data && *(ps - 1)=='#') { // Remove pre-number sign
- *(ps - 1) = (char)p1;
- if (ps + p2<macro_body[0].end() && *(ps + p2)=='#') { // Has pre & post number signs
- std::memmove(ps,ps + p2 + 1,macro_body[0].end() - ps - p2 - 1);
- macro_body[0]._width-=p2 + 1;
- } else { // Has pre number sign only
- std::memmove(ps,ps + p2,macro_body[0].end() - ps - p2);
- macro_body[0]._width-=p2;
- }
- } else if (ps + p2<macro_body[0].end() && *(ps + p2)=='#') { // Remove post-number sign
- *(ps++) = (char)p1;
- std::memmove(ps,ps + p2,macro_body[0].end() - ps - p2);
- macro_body[0]._width-=p2;
- } else { // Not near a number sign
- if (p2<3) {
- ps-=(ulongT)macro_body[0]._data;
- macro_body[0].resize(macro_body[0]._width - p2 + 3,1,1,1,0);
- ps+=(ulongT)macro_body[0]._data;
- } else macro_body[0]._width-=p2 - 3;
- std::memmove(ps + 3,ps + p2,macro_body[0].end() - ps - 3);
- *(ps++) = '(';
- *(ps++) = (char)p1;
- *(ps++) = ')';
- }
- } else ++ps;
- }
- }
- }
- // Store number of arguments.
- macro_def[0].resize(macro_def[0]._width + 1,1,1,1,0).back() = (char)(p1 - 1);
- // Detect parts of function body inside a string.
- is_inside_string(macro_body[0]).move_to(macro_body_is_string,0);
- _cimg_mp_return_nan();
- }
- }
- // Check if the variable name could be valid. If not, this is probably an lvalue assignment.
- const bool is_const = l_variable_name>6 && !std::strncmp(variable_name,"const ",6);
- s0 = variable_name._data;
- if (is_const) {
- s0+=6; while (cimg::is_blank(*s0)) ++s0;
- variable_name.resize(variable_name.end() - s0,1,1,1,0,0,1);
- }
- if (is_varname(variable_name)) { // Valid variable name
- // Assign variable (direct).
- get_variable_pos(variable_name,arg1,arg2);
- arg3 = compile(s + 1,se,depth1,0,bloc_flags);
- is_sth = return_new_comp; // is arg3 a new blank object?
- if (is_const) _cimg_mp_check_const_scalar(arg3,2,0);
- arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U;
- if (arg1==~0U) { // Create new variable
- if (_cimg_mp_is_vector(arg3)) { // Vector variable
- arg1 = is_sth || is_comp_vector(arg3)?arg3:vector_copy(arg3);
- set_reserved_vector(arg1); // Prevent from being used in further optimization
- } else { // Scalar variable
- if (is_const) arg1 = arg3;
- else {
- arg1 = is_sth || _cimg_mp_is_comp(arg3)?arg3:scalar1(mp_copy,arg3);
- memtype[arg1] = -1;
- }
- }
- if (arg2!=~0U) reserved_label[arg2] = arg1;
- else {
- if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
- variable_pos[variable_def._width] = arg1;
- variable_name.move_to(variable_def);
- }
- } else { // Variable already exists -> assign a new value
- if (is_const || _cimg_mp_is_const_scalar(arg1)) {
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid assignment of %sconst variable '%s'%s, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- _cimg_mp_is_const_scalar(arg1)?"":"non-",
- variable_name._data,
- !_cimg_mp_is_const_scalar(arg1) && is_const?" as a const variable":"",
- s0);
- }
- _cimg_mp_check_type(arg3,2,_cimg_mp_is_vector(arg1)?3:1,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1)) { // Vector
- if (_cimg_mp_is_vector(arg3)) // From vector
- CImg<ulongT>::vector((ulongT)mp_vector_copy,arg1,arg3,(ulongT)_cimg_mp_size(arg1)).
- move_to(code);
- else // From scalar
- CImg<ulongT>::vector((ulongT)mp_vector_init,arg1,1,(ulongT)_cimg_mp_size(arg1),arg3).
- move_to(code);
- } else // Scalar
- CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg3).move_to(code);
- }
- return_new_comp = false;
- _cimg_mp_return(arg1);
- }
- // Assign lvalue (variable name was not valid for a direct assignment).
- arg1 = ~0U;
- is_sth = (bool)std::strchr(variable_name,'?'); // Contains_ternary_operator?
- if (is_sth) break; // Do nothing and make ternary operator priority over assignment
- if (l_variable_name>2 && (std::strchr(variable_name,'(') || std::strchr(variable_name,'['))) {
- ref.assign(7);
- arg1 = compile(ss,s,depth1,ref,bloc_flags); // Lvalue slot
- arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to assign
- if (*ref==1) { // Vector value (scalar): V[k] = scalar
- _cimg_mp_check_type(arg2,2,1,0);
- arg3 = ref[1]; // Vector slot
- arg4 = ref[2]; // Index
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg2,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
- move_to(code);
- _cimg_mp_return(arg2);
- }
- if (*ref==2) { // Image value (scalar): i/j[_#ind,off] = scalar
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,1,0);
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg2);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
- arg2,p1,arg3).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg2);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
- arg2,arg3).move_to(code);
- }
- _cimg_mp_return(arg2);
- }
- if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c) = scalar
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,1,0);
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- arg6 = ref[6]; // C
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg2);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
- arg2,p1,arg3,arg4,arg5,arg6).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg2);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
- arg2,arg3,arg4,arg5,arg6).move_to(code);
- }
- _cimg_mp_return(arg2);
- }
- if (*ref==4) { // Image value (vector): I/J[_#ind,off] = value
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg2);
- if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
- arg2,p1,arg3).move_to(code);
- else {
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg2,p1,arg3,_cimg_mp_size(arg2)).move_to(code);
- }
- } else {
- if (!imgout) _cimg_mp_return(arg2);
- if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
- arg2,arg3).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg2,arg3,_cimg_mp_size(arg2)).move_to(code);
- }
- _cimg_mp_return(arg2);
- }
- if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) = value
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg2);
- if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
- arg2,p1,arg3,arg4,arg5).move_to(code);
- else {
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg2,p1,arg3,arg4,arg5,_cimg_mp_size(arg2)).move_to(code);
- }
- } else {
- if (!imgout) _cimg_mp_return(arg2);
- if (_cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
- arg2,arg3,arg4,arg5).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg2,arg3,arg4,arg5,_cimg_mp_size(arg2)).move_to(code);
- }
- _cimg_mp_return(arg2);
- }
- if (_cimg_mp_is_vector(arg1)) { // Vector variable: V = value
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg2)) // From vector
- CImg<ulongT>::vector((ulongT)mp_vector_copy,arg1,arg2,(ulongT)_cimg_mp_size(arg1)).
- move_to(code);
- else // From scalar
- CImg<ulongT>::vector((ulongT)mp_vector_init,arg1,1,(ulongT)_cimg_mp_size(arg1),arg2).
- move_to(code);
- _cimg_mp_return(arg1);
- }
- if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s = scalar
- _cimg_mp_check_type(arg2,2,1,0);
- CImg<ulongT>::vector((ulongT)mp_copy,arg1,arg2).move_to(code);
- _cimg_mp_return(arg1);
- }
- }
- // No assignment expressions match -> error
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- arg1!=~0U && _cimg_mp_is_const_scalar(arg1)?"const ":"",
- variable_name._data,s0);
- }
- // Apply unary/binary/ternary operators. The operator precedences should be the same as in C++.
- for (s = se2, ps = se3, ns = ps - 1; s>ss1; --s, --ps, --ns) // Here, ns = ps - 1
- if (*s=='=' && (*ps=='*' || *ps=='/' || *ps=='^') && *ns==*ps &&
- level[s - expr._data]==clevel) { // Self-operators for complex numbers only (**=,//=,^^=)
- _cimg_mp_op(*ps=='*'?"Operator '**='":*ps=='/'?"Operator '//='":"Operator '^^='");
- ref.assign(7);
- arg1 = compile(ss,ns,depth1,ref,bloc_flags); // Vector slot
- arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Right operand
- _cimg_mp_check_type(arg1,1,2,2);
- _cimg_mp_check_type(arg2,2,3,2);
- if (_cimg_mp_is_vector(arg2)) { // Complex **= complex
- if (*ps=='*')
- CImg<ulongT>::vector((ulongT)mp_complex_mul,arg1,arg1,arg2).move_to(code);
- else if (*ps=='/')
- CImg<ulongT>::vector((ulongT)mp_complex_div_vv,arg1,arg1,arg2).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)mp_complex_pow_vv,arg1,arg1,arg2).move_to(code);
- } else { // Complex **= scalar
- if (*ps=='*') {
- if (arg2==1) _cimg_mp_return(arg1);
- self_vector_s(arg1,mp_self_mul,arg2);
- } else if (*ps=='/') {
- if (arg2==1) _cimg_mp_return(arg1);
- self_vector_s(arg1,mp_self_div,arg2);
- } else {
- if (arg2==1) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)mp_complex_pow_vs,arg1,arg1,arg2).move_to(code);
- }
- }
- if (*ref==4) { // Image value (vector): I/J[_#ind,off] **= value
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
- }
- } else if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) **= value
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- }
- }
- _cimg_mp_return(arg1);
- }
- for (s = se2, ps = se3, ns = ps - 1; s>ss1; --s, --ps, --ns) // Here, ns = ps - 1
- if (*s=='=' && (*ps=='+' || *ps=='-' || *ps=='*' || *ps=='/' || (*ps=='%' && s[1]!='=') ||
- *ps=='&' || *ps=='^' || *ps=='|' ||
- (*ps=='>' && *ns=='>') || (*ps=='<' && *ns=='<')) &&
- level[s - expr._data]==clevel) { // Self-operators (+=,-=,*=,/=,%=,>>=,<<=,&=,^=,|=)
- switch (*ps) {
- case '+' : op = mp_self_add; _cimg_mp_op("Operator '+='"); break;
- case '-' : op = mp_self_sub; _cimg_mp_op("Operator '-='"); break;
- case '*' : op = mp_self_mul; _cimg_mp_op("Operator '*='"); break;
- case '/' : op = mp_self_div; _cimg_mp_op("Operator '/='"); break;
- case '%' : op = mp_self_modulo; _cimg_mp_op("Operator '%='"); break;
- case '<' : op = mp_self_bitwise_left_shift; _cimg_mp_op("Operator '<<='"); break;
- case '>' : op = mp_self_bitwise_right_shift; _cimg_mp_op("Operator '>>='"); break;
- case '&' : op = mp_self_bitwise_and; _cimg_mp_op("Operator '&='"); break;
- case '|' : op = mp_self_bitwise_or; _cimg_mp_op("Operator '|='"); break;
- default : op = mp_self_pow; _cimg_mp_op("Operator '^='"); break;
- }
- s1 = *ps=='>' || *ps=='<'?ns:ps;
- ref.assign(7);
- arg1 = compile(ss,s1,depth1,ref,bloc_flags); // Variable slot
- arg2 = compile(s + 1,se,depth1,0,bloc_flags); // Value to apply
- // Check for particular case to be simplified.
- if ((op==mp_self_add || op==mp_self_sub) && !arg2) _cimg_mp_return(arg1);
- if ((op==mp_self_mul || op==mp_self_div) && arg2==1) _cimg_mp_return(arg1);
- // Apply operator on a copy to prevent modifying a constant or a variable.
- if (*ref && (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_vector(arg1) || _cimg_mp_is_reserved(arg1))) {
- if (_cimg_mp_is_vector(arg1)) arg1 = vector_copy(arg1);
- else arg1 = scalar1(mp_copy,arg1);
- }
- if (*ref==1) { // Vector value (scalar): V[k] += scalar
- _cimg_mp_check_type(arg2,2,1,0);
- arg3 = ref[1]; // Vector slot
- arg4 = ref[2]; // Index
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
- CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
- move_to(code);
- _cimg_mp_return(arg1);
- }
- if (*ref==2) { // Image value (scalar): i/j[_#ind,off] += scalar
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,1,0);
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
- arg1,p1,arg3).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
- arg1,arg3).move_to(code);
- }
- _cimg_mp_return(arg1);
- }
- if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c) += scalar
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,1,0);
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- arg6 = ref[6]; // C
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
- arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
- arg1,arg3,arg4,arg5,arg6).move_to(code);
- }
- _cimg_mp_return(arg1);
- }
- if (*ref==4) { // Image value (vector): I/J[_#ind,off] += value
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (_cimg_mp_is_scalar(arg2)) self_vector_s(arg1,op,arg2); else self_vector_v(arg1,op,arg2);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
- }
- _cimg_mp_return(arg1);
- }
- if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c) += value
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- if (_cimg_mp_is_scalar(arg2)) self_vector_s(arg1,op,arg2); else self_vector_v(arg1,op,arg2);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(arg1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- }
- _cimg_mp_return(arg1);
- }
- if (_cimg_mp_is_vector(arg1)) { // Vector variable: V += value
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg2)) self_vector_v(arg1,op,arg2); // Vector += vector
- else self_vector_s(arg1,op,arg2); // Vector += scalar
- _cimg_mp_return(arg1);
- }
- if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s += scalar
- _cimg_mp_check_type(arg2,2,1,0);
- CImg<ulongT>::vector((ulongT)op,arg1,arg2).move_to(code);
- _cimg_mp_return(arg1);
- }
- variable_name.assign(ss,(unsigned int)(s - ss)).back() = 0;
- cimg::strpare(variable_name,false,true);
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- _cimg_mp_is_const_scalar(arg1)?"const ":"",
- variable_name._data,s0);
- }
- for (s = ss1; s<se1; ++s)
- if (*s=='?' && level[s - expr._data]==clevel) { // Ternary operator 'cond?expr1:expr2'
- _cimg_mp_op("Operator '?:'");
- s1 = s + 1; while (s1<se1 && (*s1!=':' || level[s1 - expr._data]!=clevel)) ++s1;
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- if (_cimg_mp_is_const_scalar(arg1)) {
- if ((bool)mem[arg1]) return compile(s + 1,*s1!=':'?se:s1,depth1,0,bloc_flags);
- else return *s1!=':'?0:compile(++s1,se,depth1,0,bloc_flags);
- }
- p2 = code._width;
- arg2 = compile(s + 1,*s1!=':'?se:s1,depth1,0,bloc_flags);
- p3 = code._width;
- arg3 = *s1==':'?compile(++s1,se,depth1,0,bloc_flags):
- _cimg_mp_is_vector(arg2)?vector(_cimg_mp_size(arg2),0):0;
- _cimg_mp_check_type(arg3,3,_cimg_mp_is_vector(arg2)?2:1,_cimg_mp_size(arg2));
- arg4 = _cimg_mp_size(arg2);
- if (arg4) pos = vector(arg4); else pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_if,pos,arg1,arg2,arg3,
- p3 - p2,code._width - p3,arg4).move_to(code,p2);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='|' && *ns=='|' && level[s - expr._data]==clevel) { // Logical or ('||')
- _cimg_mp_op("Operator '||'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- if (arg1>0 && arg1<=16) _cimg_mp_return(1);
- p2 = code._width;
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,1,0);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1] || mem[arg2]);
- if (!arg1) _cimg_mp_return(arg2);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_logical_or,pos,arg1,arg2,code._width - p2).
- move_to(code,p2);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='&' && *ns=='&' && level[s - expr._data]==clevel) { // Logical and ('&&')
- _cimg_mp_op("Operator '&&'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- if (!arg1) _cimg_mp_return(0);
- p2 = code._width;
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,1,0);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1] && mem[arg2]);
- if (arg1>0 && arg1<=16) _cimg_mp_return(arg2);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_logical_and,pos,arg1,arg2,code._width - p2).
- move_to(code,p2);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- for (s = se2; s>ss; --s)
- if (*s=='|' && level[s - expr._data]==clevel) { // Bitwise or ('|')
- _cimg_mp_op("Operator '|'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_or,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
- if (!arg2) _cimg_mp_return(arg1);
- _cimg_mp_vector2_vs(mp_bitwise_or,arg1,arg2);
- }
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
- if (!arg1) _cimg_mp_return(arg2);
- _cimg_mp_vector2_sv(mp_bitwise_or,arg1,arg2);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar((longT)mem[arg1] | (longT)mem[arg2]);
- if (!arg2) _cimg_mp_return(arg1);
- if (!arg1) _cimg_mp_return(arg2);
- _cimg_mp_scalar2(mp_bitwise_or,arg1,arg2);
- }
- for (s = se2; s>ss; --s)
- if (*s=='&' && level[s - expr._data]==clevel) { // Bitwise and ('&')
- _cimg_mp_op("Operator '&'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_and,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_bitwise_and,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_bitwise_and,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar((longT)mem[arg1] & (longT)mem[arg2]);
- if (!arg1 || !arg2) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_bitwise_and,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='!' && *ns=='=' && level[s - expr._data]==clevel) { // Not equal to ('!=')
- _cimg_mp_op("Operator '!='");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- if (arg1==arg2) _cimg_mp_return(0);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- if (p1 || p2) {
- if (p1 && p2 && p1!=p2) _cimg_mp_return(1);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_vector_neq,pos,arg1,p1,arg2,p2,11,1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]!=mem[arg2]);
- _cimg_mp_scalar2(mp_neq,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='=' && *ns=='=' && level[s - expr._data]==clevel) { // Equal to ('==')
- _cimg_mp_op("Operator '=='");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- if (arg1==arg2) _cimg_mp_return(1);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- if (p1 || p2) {
- if (p1 && p2 && p1!=p2) _cimg_mp_return(0);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_vector_eq,pos,arg1,p1,arg2,p2,11,1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]==mem[arg2]);
- _cimg_mp_scalar2(mp_eq,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='<' && *ns=='=' && level[s - expr._data]==clevel) { // Less or equal than ('<=')
- _cimg_mp_op("Operator '<='");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_lte,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_lte,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_lte,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]<=mem[arg2]);
- if (arg1==arg2) _cimg_mp_return(1);
- _cimg_mp_scalar2(mp_lte,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='>' && *ns=='=' && level[s - expr._data]==clevel) { // Greater or equal than ('>=')
- _cimg_mp_op("Operator '>='");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_gte,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_gte,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_gte,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]>=mem[arg2]);
- if (arg1==arg2) _cimg_mp_return(1);
- _cimg_mp_scalar2(mp_gte,arg1,arg2);
- }
- for (s = se2, ns = se1, ps = se3; s>ss; --s, --ns, --ps)
- if (*s=='<' && *ns!='<' && *ps!='<' && level[s - expr._data]==clevel) { // Less than ('<')
- _cimg_mp_op("Operator '<'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_lt,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_lt,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_lt,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]<mem[arg2]);
- if (arg1==arg2) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_lt,arg1,arg2);
- }
- for (s = se2, ns = se1, ps = se3; s>ss; --s, --ns, --ps)
- if (*s=='>' && *ns!='>' && *ps!='>' && level[s - expr._data]==clevel) { // Greater than ('>')
- _cimg_mp_op("Operator '>'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_gt,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_gt,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_gt,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]>mem[arg2]);
- if (arg1==arg2) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_gt,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='<' && *ns=='<' && level[s - expr._data]==clevel) { // Left bit shift ('<<')
- _cimg_mp_op("Operator '<<'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
- _cimg_mp_vector2_vv(mp_bitwise_left_shift,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
- if (!arg2) _cimg_mp_return(arg1);
- _cimg_mp_vector2_vs(mp_bitwise_left_shift,arg1,arg2);
- }
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
- _cimg_mp_vector2_sv(mp_bitwise_left_shift,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar((longT)mem[arg1]<<(unsigned int)mem[arg2]);
- if (!arg1) _cimg_mp_return(0);
- if (!arg2) _cimg_mp_return(arg1);
- _cimg_mp_scalar2(mp_bitwise_left_shift,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='>' && *ns=='>' && level[s - expr._data]==clevel) { // Right bit shift ('>>')
- _cimg_mp_op("Operator '>>'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
- _cimg_mp_vector2_vv(mp_bitwise_right_shift,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) {
- if (!arg2) _cimg_mp_return(arg1);
- _cimg_mp_vector2_vs(mp_bitwise_right_shift,arg1,arg2);
- }
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
- _cimg_mp_vector2_sv(mp_bitwise_right_shift,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar((longT)mem[arg1]>>(unsigned int)mem[arg2]);
- if (!arg1) _cimg_mp_return(0);
- if (!arg2) _cimg_mp_return(arg1);
- _cimg_mp_scalar2(mp_bitwise_right_shift,arg1,arg2);
- }
- for (ns = se1, s = se2, ps = pexpr._data + (se3 - expr._data); s>ss; --ns, --s, --ps)
- if (*s=='+' && (*ns!='+' || ns!=se1) && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' &&
- *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && *ps!='#' &&
- (*ps!='e' || !(ps - pexpr._data>ss - expr._data && (*(ps - 1)=='.' || (*(ps - 1)>='0' &&
- *(ps - 1)<='9')))) &&
- level[s - expr._data]==clevel) { // Addition ('+')
- _cimg_mp_op("Operator '+'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (!arg2) _cimg_mp_return(arg1);
- if (!arg1) _cimg_mp_return(arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_add,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_add,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_add,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1] + mem[arg2]);
- if (code) { // Try to spot linear case 'a*b + c'
- CImg<ulongT> &pop = code.back();
- if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
- arg3 = (unsigned int)pop[1];
- arg4 = (unsigned int)pop[2];
- arg5 = (unsigned int)pop[3];
- code.remove();
- CImg<ulongT>::vector((ulongT)mp_linear_add,arg3,arg4,arg5,arg3==arg2?arg1:arg2).move_to(code);
- _cimg_mp_return(arg3);
- }
- }
- if (arg2==1) _cimg_mp_scalar1(mp_increment,arg1);
- if (arg1==1) _cimg_mp_scalar1(mp_increment,arg2);
- _cimg_mp_scalar2(mp_add,arg1,arg2);
- }
- for (ns = se1, s = se2, ps = pexpr._data + (se3 - expr._data); s>ss; --ns, --s, --ps)
- if (*s=='-' && (*ns!='-' || ns!=se1) && *ps!='-' && *ps!='+' && *ps!='*' && *ps!='/' && *ps!='%' &&
- *ps!='&' && *ps!='|' && *ps!='^' && *ps!='!' && *ps!='~' && *ps!='#' &&
- (*ps!='e' || !(ps - pexpr._data>ss - expr._data && (*(ps - 1)=='.' || (*(ps - 1)>='0' &&
- *(ps - 1)<='9')))) &&
- level[s - expr._data]==clevel) { // Subtraction ('-')
- _cimg_mp_op("Operator '-'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (!arg2) _cimg_mp_return(arg1);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_sub,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_sub,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
- if (!arg1) _cimg_mp_vector1_v(mp_minus,arg2);
- _cimg_mp_vector2_sv(mp_sub,arg1,arg2);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1] - mem[arg2]);
- if (!arg1) _cimg_mp_scalar1(mp_minus,arg2);
- if (code) { // Try to spot linear cases 'a*b - c' and 'c - a*b'
- CImg<ulongT> &pop = code.back();
- if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
- arg3 = (unsigned int)pop[1];
- arg4 = (unsigned int)pop[2];
- arg5 = (unsigned int)pop[3];
- code.remove();
- CImg<ulongT>::vector((ulongT)(arg3==arg1?mp_linear_sub_left:mp_linear_sub_right),
- arg3,arg4,arg5,arg3==arg1?arg2:arg1).move_to(code);
- _cimg_mp_return(arg3);
- }
- }
- if (arg2==1) _cimg_mp_scalar1(mp_decrement,arg1);
- _cimg_mp_scalar2(mp_sub,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='*' && *ns=='*' && level[s - expr._data]==clevel) { // Complex multiplication ('**')
- _cimg_mp_op("Operator '**'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,3,2);
- _cimg_mp_check_type(arg2,2,3,2);
- if (arg2==1) _cimg_mp_return(arg1);
- if (arg1==1) _cimg_mp_return(arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) {
- pos = vector(2);
- CImg<ulongT>::vector((ulongT)mp_complex_mul,pos,arg1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_mul,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_mul,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]*mem[arg2]);
- if (!arg1 || !arg2) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_mul,arg1,arg2);
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='/' && *ns=='/' && level[s - expr._data]==clevel) { // Complex division ('//')
- _cimg_mp_op("Operator '//'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,3,2);
- _cimg_mp_check_type(arg2,2,3,2);
- if (arg2==1) _cimg_mp_return(arg1);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) {
- pos = vector(2);
- CImg<ulongT>::vector((ulongT)mp_complex_div_vv,pos,arg1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) {
- pos = vector(2);
- CImg<ulongT>::vector((ulongT)mp_complex_div_sv,pos,arg1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]/mem[arg2]);
- if (!arg1) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_div,arg1,arg2);
- }
- for (s = se2; s>ss; --s) if (*s=='*' && level[s - expr._data]==clevel) { // Multiplication ('*')
- _cimg_mp_op("Operator '*'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- p2 = _cimg_mp_size(arg2);
- if (p2>0 && (ulongT)_cimg_mp_size(arg1)==(ulongT)p2*p2) { // Particular case of matrix multiplication
- pos = vector(p2);
- CImg<ulongT>::vector((ulongT)mp_matrix_mul,pos,arg1,arg2,p2,p2,1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (arg2==1) _cimg_mp_return(arg1);
- if (arg1==1) _cimg_mp_return(arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_mul,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_mul,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_mul,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]*mem[arg2]);
- if (code) { // Try to spot double multiplication 'a*b*c'
- CImg<ulongT> &pop = code.back();
- if (pop[0]==(ulongT)mp_mul && _cimg_mp_is_comp(pop[1]) && (pop[1]==arg1 || pop[1]==arg2)) {
- arg3 = (unsigned int)pop[1];
- arg4 = (unsigned int)pop[2];
- arg5 = (unsigned int)pop[3];
- code.remove();
- CImg<ulongT>::vector((ulongT)mp_mul2,arg3,arg4,arg5,arg3==arg2?arg1:arg2).move_to(code);
- _cimg_mp_return(arg3);
- }
- }
- if (!arg1 || !arg2) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_mul,arg1,arg2);
- }
- for (s = se2; s>ss; --s) if (*s=='/' && level[s - expr._data]==clevel) { // Division ('/')
- _cimg_mp_op("Operator '/'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (arg2==1) _cimg_mp_return(arg1);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_div,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_div,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(mem[arg1]/mem[arg2]);
- if (!arg1) _cimg_mp_return(0);
- _cimg_mp_scalar2(mp_div,arg1,arg2);
- }
- for (s = se2, ns = se1; s>ss; --s, --ns)
- if (*s=='%' && *ns!='^' && level[s - expr._data]==clevel) { // Modulo ('%')
- _cimg_mp_op("Operator '%'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_modulo,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_modulo,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_modulo,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(cimg::mod(mem[arg1],mem[arg2]));
- _cimg_mp_scalar2(mp_modulo,arg1,arg2);
- }
- if (se1>ss) {
- if (*ss=='+' && (*ss1!='+' || (ss2<se && *ss2>='0' && *ss2<='9'))) { // Unary plus ('+')
- _cimg_mp_op("Operator '+'");
- _cimg_mp_return(compile(ss1,se,depth1,0,bloc_flags));
- }
- if (*ss=='-' && (*ss1!='-' || (ss2<se && *ss2>='0' && *ss2<='9'))) { // Unary minus ('-')
- _cimg_mp_op("Operator '-'");
- arg1 = compile(ss1,se,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_minus,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(-mem[arg1]);
- _cimg_mp_scalar1(mp_minus,arg1);
- }
- if (*ss=='!') { // Logical not ('!')
- _cimg_mp_op("Operator '!'");
- if (*ss1=='!') { // '!!expr' optimized as 'bool(expr)'
- arg1 = compile(ss2,se,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bool,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((bool)mem[arg1]);
- _cimg_mp_scalar1(mp_bool,arg1);
- }
- arg1 = compile(ss1,se,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_logical_not,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(!mem[arg1]);
- _cimg_mp_scalar1(mp_logical_not,arg1);
- }
- if (*ss=='~') { // Bitwise not ('~')
- _cimg_mp_op("Operator '~'");
- arg1 = compile(ss1,se,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bitwise_not,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(~(unsigned int)mem[arg1]);
- _cimg_mp_scalar1(mp_bitwise_not,arg1);
- }
- }
- for (s = se3, ns = se2; s>ss; --s, --ns)
- if (*s=='^' && *ns=='^' && level[s - expr._data]==clevel) { // Complex power ('^^')
- _cimg_mp_op("Operator '^^'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 2,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,3,2);
- _cimg_mp_check_type(arg2,2,3,2);
- if (arg2==1) _cimg_mp_return(arg1);
- pos = vector(2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2))
- CImg<ulongT>::vector((ulongT)mp_complex_pow_vv,pos,arg1,arg2).move_to(code);
- else if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2))
- CImg<ulongT>::vector((ulongT)mp_complex_pow_vs,pos,arg1,arg2).move_to(code);
- else if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2))
- CImg<ulongT>::vector((ulongT)mp_complex_pow_sv,pos,arg1,arg2).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)mp_complex_pow_ss,pos,arg1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- for (s = se2; s>ss; --s)
- if (*s=='^' && level[s - expr._data]==clevel) { // Power ('^')
- _cimg_mp_op("Operator '^'");
- arg1 = compile(ss,s,depth1,0,bloc_flags);
- arg2 = compile(s + 1,se,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (arg2==1) _cimg_mp_return(arg1);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_pow,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_pow,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_pow,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(std::pow(mem[arg1],mem[arg2]));
- switch (arg2) {
- case 0 : _cimg_mp_return(1);
- case 2 : _cimg_mp_scalar1(mp_sqr,arg1);
- case 3 : _cimg_mp_scalar1(mp_pow3,arg1);
- case 4 : _cimg_mp_scalar1(mp_pow4,arg1);
- default :
- if (_cimg_mp_is_const_scalar(arg2)) {
- if (mem[arg2]==0.5) { _cimg_mp_scalar1(mp_sqrt,arg1); }
- else if (mem[arg2]==0.25) { _cimg_mp_scalar1(mp_pow0_25,arg1); }
- }
- _cimg_mp_scalar2(mp_pow,arg1,arg2);
- }
- }
- // Percentage computation.
- if (*se1=='%') {
- arg1 = compile(ss,se1,depth1,0,bloc_flags);
- arg2 = _cimg_mp_is_const_scalar(arg1)?0:const_scalar(100);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector2_vs(mp_div,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]/100);
- _cimg_mp_scalar2(mp_div,arg1,arg2);
- }
- // Degree to radian postfix operator ('°' in UTF-8).
- if (se2>ss && (unsigned char)*se2==0xC2 && (unsigned char)*se1==0xB0) {
- arg1 = compile(ss,se2,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_deg2rad,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*cimg::PI/180);
- _cimg_mp_scalar1(mp_deg2rad,arg1);
- }
- // Pre/post-decrement and increment.
- is_sth = ss1<se1 && (*ss=='+' || *ss=='-') && *ss1==*ss; // is pre-?
- if (is_sth || (se2>ss && (*se1=='+' || *se1=='-') && *se2==*se1)) {
- if ((is_sth && *ss=='+') || (!is_sth && *se1=='+')) {
- _cimg_mp_op("Operator '++'");
- op = mp_self_increment;
- } else {
- _cimg_mp_op("Operator '--'");
- op = mp_self_decrement;
- }
- ref.assign(7);
- arg1 = is_sth?compile(ss2,se,depth1,ref,bloc_flags):
- compile(ss,se2,depth1,ref,bloc_flags); // Variable slot
- // Apply operator on a copy to prevent modifying a constant or a variable.
- if (*ref && (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_vector(arg1) || _cimg_mp_is_reserved(arg1))) {
- if (_cimg_mp_is_vector(arg1)) arg1 = vector_copy(arg1);
- else arg1 = scalar1(mp_copy,arg1);
- }
- if (is_sth) pos = arg1; // Determine return index, depending on pre/post action
- else {
- if (_cimg_mp_is_vector(arg1)) pos = vector_copy(arg1);
- else pos = scalar1(mp_copy,arg1);
- }
- if (*ref==1) { // Vector value (scalar): V[k]++
- arg3 = ref[1]; // Vector slot
- arg4 = ref[2]; // Index
- if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
- CImg<ulongT>::vector((ulongT)op,arg1,1).move_to(code);
- CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
- move_to(code);
- _cimg_mp_return(pos);
- }
- if (*ref==2) { // Image value (scalar): i/j[_#ind,off]++
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
- CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
- arg1,p1,arg3).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
- arg1,arg3).move_to(code);
- }
- _cimg_mp_return(pos);
- }
- if (*ref==3) { // Image value (scalar): i/j(_#ind,_x,_y,_z,_c)++
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- arg6 = ref[6]; // C
- if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
- CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
- arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
- arg1,arg3,arg4,arg5,arg6).move_to(code);
- }
- _cimg_mp_return(pos);
- }
- if (*ref==4) { // Image value (vector): I/J[_#ind,off]++
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // Offset
- if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
- self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
- }
- _cimg_mp_return(pos);
- }
- if (*ref==5) { // Image value (vector): I/J(_#ind,_x,_y,_z,_c)++
- if (!is_inside_critical) is_parallelizable = false;
- p1 = ref[1]; // Index
- is_relative = (bool)ref[2];
- arg3 = ref[3]; // X
- arg4 = ref[4]; // Y
- arg5 = ref[5]; // Z
- if (is_sth && p_ref) std::memcpy(p_ref,ref,ref._width*sizeof(unsigned int));
- self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- } else {
- if (!imgout) _cimg_mp_return(pos);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- }
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_vector(arg1)) { // Vector variable: V++
- self_vector_s(arg1,op==mp_self_increment?mp_self_add:mp_self_sub,1);
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_reserved(arg1)) { // Scalar variable: s++
- CImg<ulongT>::vector((ulongT)op,arg1).move_to(code);
- _cimg_mp_return(pos);
- }
- if (is_sth) variable_name.assign(ss2,(unsigned int)(se - ss1));
- else variable_name.assign(ss,(unsigned int)(se1 - ss));
- variable_name.back() = 0;
- cimg::strpare(variable_name,false,true);
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid %slvalue '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- _cimg_mp_is_const_scalar(arg1)?"const ":"",
- variable_name._data,s0);
- }
- // Array-like access to vectors and image values 'i/j/I/J[_#ind,offset,_boundary]' and 'vector[offset]'.
- if (*se1==']') {
- _cimg_mp_op("Value accessor '[]'");
- // Find opening bracket for the offset.
- s0 = se1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
- if (s0>ss) { s1 = s0; do { --s1; } while (cimg::is_blank(*s1)); cimg::swap(*s0,*++s1); }
- is_sth=s0>ss && *(s0-1)==']'; // Particular case s.a. '..[..][..]' ?
- is_relative = *ss=='j' || *ss=='J';
- if (!is_sth && (*ss=='I' || *ss=='J') && *ss1=='[' &&
- (reserved_label[(int)*ss]==~0U ||
- !_cimg_mp_is_vector(reserved_label[(int)*ss]))) { // Image value as a vector
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_const_index(p1);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss2; }
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p2 = 1 + (p1!=~0U);
- arg1 = compile(s0,s1,depth1,0,bloc_flags); // Offset
- _cimg_mp_check_type(arg1,p2,1,0);
- arg2 = ~0U;
- if (s1<se1) {
- arg2 = compile(++s1,se1,depth1,0,bloc_flags); // Boundary
- _cimg_mp_check_type(arg2,p2 + 1,1,0);
- }
- if (p_ref && arg2==~0U) {
- *p_ref = 4;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- }
- p2 = ~0U; // 'p2' must be the dimension of the vector-valued operand if any
- if (p1==~0U) p2 = imgin._spectrum;
- else {
- p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- p2 = imglist[p3]._spectrum;
- }
- if (!p2) _cimg_mp_return(0);
- pos = vector(p2);
- if (p1!=~0U) {
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_Joff:mp_list_Ioff),
- pos,p1,arg1,arg2==~0U?_cimg_mp_boundary:arg2,p2).move_to(code);
- } else {
- need_input_copy = true;
- CImg<ulongT>::vector((ulongT)(is_relative?mp_Joff:mp_Ioff),
- pos,arg1,arg2==~0U?_cimg_mp_boundary:arg2,p2).move_to(code);
- }
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!is_sth && (*ss=='i' || *ss=='j') && *ss1=='[' &&
- (reserved_label[(int)*ss]==~0U ||
- !_cimg_mp_is_vector(reserved_label[(int)*ss]))) { // Image value as a scalar
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- } else { p1 = ~0U; s0 = ss2; }
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags); // Offset
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U; // Boundary
- if (p_ref && arg2==~0U) {
- *p_ref = 2;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- }
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(0);
- pos = scalar3(is_relative?mp_list_joff:mp_list_ioff,p1,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
- } else {
- if (!imgin) _cimg_mp_return(0);
- need_input_copy = true;
- pos = scalar2(is_relative?mp_joff:mp_ioff,arg1,arg2==~0U?_cimg_mp_boundary:arg2);
- }
- memtype[pos] = -1; // Prevent from being used in further optimization
- _cimg_mp_return(pos);
- }
- s0 = se1; while (s0>ss && (*s0!='[' || level[s0 - expr._data]!=clevel)) --s0;
- if (s0>ss) { // Vector element
- arg1 = compile(ss,s0,depth1,0,bloc_flags);
- if (_cimg_mp_is_scalar(arg1)) {
- variable_name.assign(ss,(unsigned int)(s0 - ss + 1)).back() = 0;
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Array brackets used on non-vector variable '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- variable_name._data,s0);
- }
- s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- if (s1<se1) { // Two or three arguments -> sub-vector extraction
- p1 = _cimg_mp_size(arg1);
- arg2 = compile(++s0,s1,depth1,0,bloc_flags); // Starting index
- s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg3 = compile(s1,s0,depth1,0,bloc_flags); // Length
- arg4 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):1; // Step
- _cimg_mp_check_const_scalar(arg3,2,3);
- arg3 = (unsigned int)mem[arg3];
- pos = vector(arg3);
- CImg<ulongT>::vector((ulongT)mp_vector_crop,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- // One argument -> vector value reference
- arg2 = compile(++s0,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_const_scalar(arg2)) { // Constant index
- nb = (int)mem[arg2];
- if (nb>=0 && nb<(int)_cimg_mp_size(arg1)) _cimg_mp_return(arg1 + 1 + nb);
- variable_name.assign(ss,(unsigned int)(s0 - ss)).back() = 0;
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Out-of-bounds reference '%s[%d]' "
- "(vector '%s' has dimension %u), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- variable_name._data,nb,
- variable_name._data,_cimg_mp_size(arg1),s0);
- }
- if (p_ref) {
- *p_ref = 1;
- p_ref[1] = arg1;
- p_ref[2] = arg2;
- if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1; // Prevent from being used in further optimization
- }
- pos = scalar3(mp_vector_off,arg1,_cimg_mp_size(arg1),arg2);
- memtype[pos] = -1; // Prevent from being used in further optimization
- _cimg_mp_return(pos);
- }
- }
- // Look for a function call, an access to image value, or a parenthesis.
- if (*se1==')') {
- if (*ss=='(') _cimg_mp_return(compile(ss1,se1,depth1,p_ref,bloc_flags)); // Simple parentheses
- _cimg_mp_op("Value accessor '()'");
- is_relative = *ss=='j' || *ss=='J';
- s0 = s1 = std::strchr(ss,'('); if (s0) { do { --s1; } while (cimg::is_blank(*s1)); cimg::swap(*s0,*++s1); }
- // I/J(_#ind,_x,_y,_z,_interpolation,_boundary_conditions)
- if ((*ss=='I' || *ss=='J') && *ss1=='(') { // Image value as scalar
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_const_index(p1);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss2; }
- arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
- arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
- arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
- arg4 = arg5 = ~0U;
- if (s0<se1) {
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
- p2 = _cimg_mp_size(arg1);
- ++arg1;
- if (p2>1) {
- arg2 = arg1 + 1;
- if (p2>2) arg3 = arg2 + 1;
- }
- if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(s1,s2,depth1,0,bloc_flags);
- arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
- }
- } else if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,0,bloc_flags);
- if (s2<se1) {
- s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(s2,s3,depth1,0,bloc_flags);
- if (s3<se1) {
- s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(s3,s2,depth1,0,bloc_flags);
- arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
- }
- }
- }
- }
- if (p_ref && arg4==~0U && arg5==~0U) {
- *p_ref = 5;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- p_ref[4] = arg2;
- p_ref[5] = arg3;
- if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
- if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
- }
- p2 = ~0U; // 'p2' must be the dimension of the vector-valued operand if any
- if (p1==~0U) p2 = imgin._spectrum;
- else if (_cimg_mp_is_const_scalar(p1)) {
- p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- p2 = imglist[p3]._spectrum;
- }
- if (!p2) _cimg_mp_return(0);
- pos = vector(p2);
- if (p1!=~0U)
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_Jxyz:mp_list_Ixyz),
- pos,p1,arg1,arg2,arg3,
- arg4==~0U?_cimg_mp_interpolation:arg4,
- arg5==~0U?_cimg_mp_boundary:arg5,p2).move_to(code);
- else {
- need_input_copy = true;
- CImg<ulongT>::vector((ulongT)(is_relative?mp_Jxyz:mp_Ixyz),
- pos,arg1,arg2,arg3,
- arg4==~0U?_cimg_mp_interpolation:arg4,
- arg5==~0U?_cimg_mp_boundary:arg5,p2).move_to(code);
- }
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- // i/j(_#ind,_x,_y,_z,_c,_interpolation,_boundary_conditions)
- if ((*ss=='i' || *ss=='j') && *ss1=='(') { // Image value as scalar
- if (*ss2=='#') { // Index specified
- s0 = ss3; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss3,s0++,depth1,0,bloc_flags);
- } else { p1 = ~0U; s0 = ss2; }
- arg1 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
- arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
- arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
- arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
- arg5 = arg6 = ~0U;
- if (s0<se1) {
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
- p2 = _cimg_mp_size(arg1);
- ++arg1;
- if (p2>1) {
- arg2 = arg1 + 1;
- if (p2>2) {
- arg3 = arg2 + 1;
- if (p2>3) arg4 = arg3 + 1;
- }
- }
- if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg5 = compile(s1,s2,depth1,0,bloc_flags);
- arg6 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
- }
- } else if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,0,bloc_flags);
- if (s2<se1) {
- s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(s2,s3,depth1,0,bloc_flags);
- if (s3<se1) {
- s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(s3,s2,depth1,0,bloc_flags);
- if (s2<se1) {
- s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg5 = compile(s2,s3,depth1,0,bloc_flags);
- arg6 = s3<se1?compile(++s3,se1,depth1,0,bloc_flags):~0U;
- }
- }
- }
- }
- }
- if (p_ref && arg5==~0U && arg6==~0U) {
- *p_ref = 3;
- p_ref[1] = p1;
- p_ref[2] = (unsigned int)is_relative;
- p_ref[3] = arg1;
- p_ref[4] = arg2;
- p_ref[5] = arg3;
- p_ref[6] = arg4;
- if (p1!=~0U && _cimg_mp_is_comp(p1)) memtype[p1] = -1; // Prevent from being used in further optimization
- if (_cimg_mp_is_comp(arg1)) memtype[arg1] = -1;
- if (_cimg_mp_is_comp(arg2)) memtype[arg2] = -1;
- if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
- if (_cimg_mp_is_comp(arg4)) memtype[arg4] = -1;
- }
- if (p1!=~0U) {
- if (!imglist) _cimg_mp_return(0);
- pos = scalar7(is_relative?mp_list_jxyzc:mp_list_ixyzc,
- p1,arg1,arg2,arg3,arg4,
- arg5==~0U?_cimg_mp_interpolation:arg5,
- arg6==~0U?_cimg_mp_boundary:arg6);
- } else {
- if (!imgin) _cimg_mp_return(0);
- need_input_copy = true;
- pos = scalar6(is_relative?mp_jxyzc:mp_ixyzc,
- arg1,arg2,arg3,arg4,
- arg5==~0U?_cimg_mp_interpolation:arg5,
- arg6==~0U?_cimg_mp_boundary:arg6);
- }
- memtype[pos] = -1; // Prevent from being used in further optimization
- _cimg_mp_return(pos);
- }
- // Mathematical functions.
- switch (*ss) {
- case 'a' :
- if (!std::strncmp(ss,"abs(",4)) { // Absolute value
- _cimg_mp_op("Function 'abs()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_abs,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::abs(mem[arg1]));
- _cimg_mp_scalar1(mp_abs,arg1);
- }
- if (!std::strncmp(ss,"addr(",5)) { // Pointer address
- _cimg_mp_op("Function 'addr()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_const_scalar((double)arg1);
- }
- if (!std::strncmp(ss,"acos(",5)) { // Arccos
- _cimg_mp_op("Function 'acos()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_acos,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::acos(mem[arg1]));
- _cimg_mp_scalar1(mp_acos,arg1);
- }
- if (!std::strncmp(ss,"acosh(",6)) { // Hyperbolic arccosine
- _cimg_mp_op("Function 'acosh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_acosh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::acosh(mem[arg1]));
- _cimg_mp_scalar1(mp_acosh,arg1);
- }
- if (!std::strncmp(ss,"asinh(",6)) { // Hyperbolic arcsine
- _cimg_mp_op("Function 'asinh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_asinh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::asinh(mem[arg1]));
- _cimg_mp_scalar1(mp_asinh,arg1);
- }
- if (!std::strncmp(ss,"atanh(",6)) { // Hyperbolic arctangent
- _cimg_mp_op("Function 'atanh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_atanh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::atanh(mem[arg1]));
- _cimg_mp_scalar1(mp_atanh,arg1);
- }
- if (!std::strncmp(ss,"arg(",4) ||
- !std::strncmp(ss,"arg0(",5) ||
- !std::strncmp(ss,"arg1(",5)) { // Nth argument
- _cimg_mp_op(*ss3=='('?"Function 'arg()'":*ss3=='0'?"Function 'arg0()'":"Function 'arg1()'");
- s0 = ss4 + (*ss3!='('?1:0);
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,0,bloc_flags);
- p2 = _cimg_mp_size(arg2);
- p3 = 3;
- CImg<ulongT>::vector((ulongT)(*ss3=='0'?mp_arg0:mp_arg),0,0,p2,arg1,arg2).move_to(l_opcode);
- for (s = ++s2; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg3 = compile(s,ns,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,p3,p2?2:1,p2);
- CImg<ulongT>::vector(arg3).move_to(l_opcode);
- ++p3;
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- if (_cimg_mp_is_const_scalar(arg1)) {
- p3-=1; // Number of args
- if (*ss3=='0') arg1 = (unsigned int)(mem[arg1]<0?mem[arg1] + p3:mem[arg1] + 1);
- else arg1 = (unsigned int)(mem[arg1]<0?mem[arg1] + p3:mem[arg1]);
- if (arg1<p3) _cimg_mp_return(opcode[4 + arg1]);
- if (p2) {
- pos = vector(p2);
- std::memset(&mem[pos] + 1,0,p2*sizeof(double));
- return_new_comp = true;
- _cimg_mp_return(pos);
- } else _cimg_mp_return(0);
- }
- pos = opcode[1] = p2?vector(p2):scalar();
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"asin(",5)) { // Arcsin
- _cimg_mp_op("Function 'asin()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_asin,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::asin(mem[arg1]));
- _cimg_mp_scalar1(mp_asin,arg1);
- }
- if (!std::strncmp(ss,"atan(",5)) { // Arctan
- _cimg_mp_op("Function 'atan()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_atan,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::atan(mem[arg1]));
- _cimg_mp_scalar1(mp_atan,arg1);
- }
- if (!std::strncmp(ss,"atan2(",6)) { // Arctan2
- _cimg_mp_op("Function 'atan2()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_atan2,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_atan2,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_atan2,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(std::atan2(mem[arg1],mem[arg2]));
- _cimg_mp_scalar2(mp_atan2,arg1,arg2);
- }
- break;
- case 'b' :
- if (!std::strncmp(ss,"break(",6)) { // Break current loop
- if (pexpr[se2 - expr._data]=='(') { // no arguments?
- CImg<ulongT>::vector((ulongT)mp_break,_cimg_mp_slot_nan).move_to(code);
- _cimg_mp_return_nan();
- }
- }
- if (!std::strncmp(ss,"breakpoint(",11)) { // Break point (for abort test)
- _cimg_mp_op("Function 'breakpoint()'");
- if (pexpr[se2 - expr._data]=='(') { // no arguments?
- CImg<ulongT>::vector((ulongT)mp_breakpoint,_cimg_mp_slot_nan).move_to(code);
- _cimg_mp_return_nan();
- }
- }
- if (!std::strncmp(ss,"bool(",5)) { // Boolean cast
- _cimg_mp_op("Function 'bool()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_bool,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((bool)mem[arg1]);
- _cimg_mp_scalar1(mp_bool,arg1);
- }
- if (!std::strncmp(ss,"begin(",6)) { // Begin
- _cimg_mp_op("Function 'begin()'");
- s1 = ss6; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
- if (s1!=se1) {
- const bool is_inside_begin = (bool)(bloc_flags&2);
- if (!is_inside_begin) code.swap(code_begin);
- arg1 = compile(s1,se1,depth1,p_ref,2);
- if (!is_inside_begin) code.swap(code_begin);
- _cimg_mp_return(arg1);
- } else _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"begin_t(",8)) { // Begin thread
- _cimg_mp_op("Function 'begin_t()'");
- s1 = ss8; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
- if (s1!=se1) {
- const bool is_inside_begin_t = (bool)(bloc_flags&4);
- if (!is_inside_begin_t) code.swap(code_begin_t);
- arg1 = compile(s1,se1,depth1,p_ref,4);
- if (!is_inside_begin_t) code.swap(code_begin_t);
- _cimg_mp_return(arg1);
- } else _cimg_mp_return_nan();
- }
- break;
- case 'c' :
- if (!std::strncmp(ss,"cabs(",5)) { // Complex absolute value
- _cimg_mp_op("Function 'cabs()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- if (_cimg_mp_is_scalar(arg1)) _cimg_mp_scalar2(mp_complex_abs,arg1,0);
- _cimg_mp_scalar2(mp_complex_abs,arg1 + 1,arg1 + 2);
- }
- if (!std::strncmp(ss,"carg(",5)) { // Complex argument
- _cimg_mp_op("Function 'carg()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- if (_cimg_mp_is_scalar(arg1)) _cimg_mp_scalar2(mp_atan2,0,arg1);
- _cimg_mp_scalar2(mp_atan2,arg1 + 2,arg1 + 1);
- }
- if (!std::strncmp(ss,"cbrt(",5)) { // Cubic root
- _cimg_mp_op("Function 'cbrt()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cbrt,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::cbrt(mem[arg1]));
- _cimg_mp_scalar1(mp_cbrt,arg1);
- }
- if (!std::strncmp(ss,"cconj(",6)) { // Complex conjugate
- _cimg_mp_op("Function 'cconj()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_conj,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_conj,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ceil(",5)) { // Ceil
- _cimg_mp_op("Function 'ceil()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_ceil,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::ceil(mem[arg1]));
- _cimg_mp_scalar1(mp_ceil,arg1);
- }
- if (!std::strncmp(ss,"cexp(",5)) { // Complex exponential
- _cimg_mp_op("Function 'cexp()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_exp,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_exp,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"clog(",5)) { // Complex logarithm
- _cimg_mp_op("Function 'clog()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_log,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_log,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ccos(",5)) { // Complex cosine
- _cimg_mp_op("Function 'ccos()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_cos,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_cos,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"csin(",5)) { // Complex sine
- _cimg_mp_op("Function 'csin()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_sin,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_sin,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ctan(",5)) { // Complex tangent
- _cimg_mp_op("Function 'ctan()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_tan,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_tan,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ccosh(",6)) { // Complex hyperbolic cosine
- _cimg_mp_op("Function 'ccosh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_cosh,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_cosh,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"csinh(",6)) { // Complex hyperbolic sine
- _cimg_mp_op("Function 'csinh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_sinh,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_sinh,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ctanh(",6)) { // Complex hyperbolic tangent
- _cimg_mp_op("Function 'ctanh()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,0,3,2);
- pos = vector(2);
- if (_cimg_mp_is_scalar(arg1)) CImg<ulongT>::vector((ulongT)mp_complex_tanh,pos,arg1,0).move_to(code);
- else CImg<ulongT>::vector((ulongT)mp_complex_tanh,pos,arg1 + 1,arg1 + 2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"continue(",9)) { // Continue loop
- if (pexpr[se2 - expr._data]=='(') { // no arguments?
- CImg<ulongT>::vector((ulongT)mp_continue,_cimg_mp_slot_nan).move_to(code);
- _cimg_mp_return_nan();
- }
- }
- if (!std::strncmp(ss,"copy(",5)) { // Memory copy
- _cimg_mp_op("Function 'copy()'");
- ref.assign(14);
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = p1 = compile(ss5,s1,depth1,ref,bloc_flags);
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,ref._data + 7,bloc_flags);
- arg3 = arg4 = arg5 = ~0U; arg6 = 1;
- if (s2<se1) {
- s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(s2,s3,depth1,0,bloc_flags);
- if (s3<se1) {
- s1 = ++s3; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg4 = compile(s3,s1,depth1,0,bloc_flags);
- if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg5 = compile(s1,s2,depth1,0,bloc_flags);
- arg6 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
- }
- }
- }
- if (_cimg_mp_is_vector(arg1)) {
- if (!ref[0]) ++arg1;
- else if (ref[0]>=4 && arg4==~0U) arg4 = scalar1(mp_image_whd,ref[1]);
- }
- if (_cimg_mp_is_vector(arg2)) {
- if (arg3==~0U) arg3 = const_scalar(_cimg_mp_size(arg2));
- if (!ref[7]) ++arg2;
- if (ref[7]>=4 && arg5==~0U) arg5 = scalar1(mp_image_whd,ref[8]);
- }
- if (arg3==~0U) arg3 = 1;
- if (arg4==~0U) arg4 = 1;
- if (arg5==~0U) arg5 = 1;
- _cimg_mp_check_type(arg3,3,1,0);
- _cimg_mp_check_type(arg4,4,1,0);
- _cimg_mp_check_type(arg5,5,1,0);
- _cimg_mp_check_type(arg6,5,1,0);
- CImg<ulongT>(1,22).move_to(code);
- code.back().get_shared_rows(0,7).fill((ulongT)mp_memcopy,p1,arg1,arg2,arg3,arg4,arg5,arg6);
- code.back().get_shared_rows(8,21).fill(ref);
- _cimg_mp_return(p1);
- }
- if (!std::strncmp(ss,"cos(",4)) { // Cosine
- _cimg_mp_op("Function 'cos()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cos,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::cos(mem[arg1]));
- _cimg_mp_scalar1(mp_cos,arg1);
- }
- if (!std::strncmp(ss,"cosh(",5)) { // Hyperbolic cosine
- _cimg_mp_op("Function 'cosh()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_cosh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::cosh(mem[arg1]));
- _cimg_mp_scalar1(mp_cosh,arg1);
- }
- if (!std::strncmp(ss,"critical(",9)) { // Critical section (single thread at a time)
- _cimg_mp_op("Function 'critical()'");
- p1 = code._width;
- arg1 = compile(ss + 9,se1,depth1,p_ref,bloc_flags | 1);
- CImg<ulongT>::vector((ulongT)mp_critical,arg1,code._width - p1).move_to(code,p1);
- _cimg_mp_return(arg1);
- }
- if (!std::strncmp(ss,"crop(",5)) { // Image crop
- _cimg_mp_op("Function 'crop()'");
- if (*ss5=='#') { // Index specified
- s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss6,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss5; need_input_copy = true; }
- pos = 0;
- is_sth = false; // Coordinates specified as a vector?
- if (s0<se1) for (s = s0; s<se; ++s, ++pos) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg1 = compile(s,ns,depth1,0,bloc_flags);
- if (!pos && _cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
- opcode = CImg<ulongT>::sequence(_cimg_mp_size(arg1),arg1 + 1,
- arg1 + (ulongT)_cimg_mp_size(arg1));
- opcode.resize(1,std::min(opcode._height,4U),1,1,0).move_to(l_opcode);
- is_sth = true;
- } else {
- _cimg_mp_check_type(arg1,pos + 1,1,0);
- CImg<ulongT>::vector(arg1).move_to(l_opcode);
- }
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- arg1 = 0; arg2 = (p1!=~0U);
- switch (opcode._height) {
- case 0 : case 1 :
- CImg<ulongT>::vector(0,0,0,0,~0U,~0U,~0U,~0U,0).move_to(opcode);
- break;
- case 2 :
- CImg<ulongT>::vector(*opcode,0,0,0,opcode[1],~0U,~0U,~0U,_cimg_mp_boundary).move_to(opcode);
- arg1 = arg2 + 2;
- break;
- case 3 :
- CImg<ulongT>::vector(*opcode,0,0,0,opcode[1],~0U,~0U,~0U,opcode[2]).move_to(opcode);
- arg1 = arg2 + 2;
- break;
- case 4 :
- CImg<ulongT>::vector(*opcode,opcode[1],0,0,opcode[2],opcode[3],~0U,~0U,_cimg_mp_boundary).
- move_to(opcode);
- arg1 = arg2 + (is_sth?2:3);
- break;
- case 5 :
- CImg<ulongT>::vector(*opcode,opcode[1],0,0,opcode[2],opcode[3],~0U,~0U,opcode[4]).
- move_to(opcode);
- arg1 = arg2 + (is_sth?2:3);
- break;
- case 6 :
- CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],0,opcode[3],opcode[4],opcode[5],~0U,
- _cimg_mp_boundary).move_to(opcode);
- arg1 = arg2 + (is_sth?2:4);
- break;
- case 7 :
- CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],0,opcode[3],opcode[4],opcode[5],~0U,
- opcode[6]).move_to(opcode);
- arg1 = arg2 + (is_sth?2:4);
- break;
- case 8 :
- CImg<ulongT>::vector(*opcode,opcode[1],opcode[2],opcode[3],opcode[4],opcode[5],opcode[6],
- opcode[7],_cimg_mp_boundary).move_to(opcode);
- arg1 = arg2 + (is_sth?2:5);
- break;
- case 9 :
- arg1 = arg2 + (is_sth?2:5);
- break;
- default : // Error -> too much arguments
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Too much arguments specified, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,s0);
- }
- _cimg_mp_check_type((unsigned int)*opcode,arg2 + 1,1,0);
- _cimg_mp_check_type((unsigned int)opcode[1],arg2 + 1 + (is_sth?0:1),1,0);
- _cimg_mp_check_type((unsigned int)opcode[2],arg2 + 1 + (is_sth?0:2),1,0);
- _cimg_mp_check_type((unsigned int)opcode[3],arg2 + 1 + (is_sth?0:3),1,0);
- if (opcode[4]!=(ulongT)~0U) {
- _cimg_mp_check_const_scalar((unsigned int)opcode[4],arg1,3);
- opcode[4] = (ulongT)mem[opcode[4]];
- }
- if (opcode[5]!=(ulongT)~0U) {
- _cimg_mp_check_const_scalar((unsigned int)opcode[5],arg1 + 1,3);
- opcode[5] = (ulongT)mem[opcode[5]];
- }
- if (opcode[6]!=(ulongT)~0U) {
- _cimg_mp_check_const_scalar((unsigned int)opcode[6],arg1 + 2,3);
- opcode[6] = (ulongT)mem[opcode[6]];
- }
- if (opcode[7]!=(ulongT)~0U) {
- _cimg_mp_check_const_scalar((unsigned int)opcode[7],arg1 + 3,3);
- opcode[7] = (ulongT)mem[opcode[7]];
- }
- _cimg_mp_check_type((unsigned int)opcode[8],arg1 + 4,1,0);
- if (opcode[4]==(ulongT)~0U || opcode[5]==(ulongT)~0U ||
- opcode[6]==(ulongT)~0U || opcode[7]==(ulongT)~0U) {
- p2 = 0;
- if (p1!=~0U) {
- _cimg_mp_check_const_scalar(p1,1,1);
- p2 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- }
- const CImg<T> &img = p1!=~0U?imglist[p2]:imgin;
- if (!img) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Cannot crop empty image when "
- "some xyzc-coordinates are unspecified, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,s0);
- }
- if (opcode[4]==(ulongT)~0U) opcode[4] = (ulongT)img._width;
- if (opcode[5]==(ulongT)~0U) opcode[5] = (ulongT)img._height;
- if (opcode[6]==(ulongT)~0U) opcode[6] = (ulongT)img._depth;
- if (opcode[7]==(ulongT)~0U) opcode[7] = (ulongT)img._spectrum;
- }
- pos = vector((unsigned int)(opcode[4]*opcode[5]*opcode[6]*opcode[7]));
- CImg<ulongT>::vector((ulongT)mp_crop,
- pos,p1,
- *opcode,opcode[1],opcode[2],opcode[3],
- opcode[4],opcode[5],opcode[6],opcode[7],
- opcode[8]).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"cross(",6)) { // Cross product
- _cimg_mp_op("Function 'cross()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,2,3);
- _cimg_mp_check_type(arg2,2,2,3);
- pos = vector(3);
- CImg<ulongT>::vector((ulongT)mp_cross,pos,arg1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"cut(",4)) { // Cut
- _cimg_mp_op("Function 'cut()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = compile(++s2,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_cut,arg1,arg2,arg3);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3)) {
- val = mem[arg1];
- val1 = mem[arg2];
- val2 = mem[arg3];
- _cimg_mp_const_scalar(val<val1?val1:val>val2?val2:val);
- }
- _cimg_mp_scalar3(mp_cut,arg1,arg2,arg3);
- }
- if (!std::strncmp(ss,"convolve(",9) || !std::strncmp(ss,"correlate(",10)) { // Convolve & Correlate
- is_sth = *ss2=='n'; // is_convolve?
- _cimg_mp_op(is_sth?"Function 'convolve()'":"Function 'correlate()'");
- op = is_sth?mp_convolve:mp_correlate;
- const ulongT default_params[] = { (ulongT)op,0, // [0]=function, [1]=result vector
- 0,0,0,0,0, // [2]=A, [3]=wA, [4]=hA, [5]=dA, [6]=sA
- 0,0,0,0,0, // [7]=M, [8]=wM, [9]=hM, [10]=dM, [11]=sM
- 1,0,1, // [12]=boundary_conditions, [13]=is_normalized, [14]=chan._mode
- ~0U,~0U,~0U, // [15]=xcenter, [16]=ycenter, [17]=zcenter
- 0,0,0, // [18]=xstart, [19]=ystart, [20]=zstart
- ~0U,~0U,~0U, // [21]=xend, [22]=yend, [23]=zend
- 1,1,1, // [24]=xstride, [25]=ystride, [26]=zstride
- 1,1,1, // [27]=xdilation, [28]=ydilation, [29]=zdilation,
- 0 }; // [30]=interpolation_type
- l_opcode.assign(); // Don't use 'opcode': it could be modified by further calls to 'compile()'!
- CImg<ulongT>(default_params,1,sizeof(default_params)/sizeof(ulongT)).move_to(l_opcode);
- arg1 = 2;
- for (s = std::strchr(ss,'(') + 1; s<se && arg1<l_opcode[0]._height; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- l_opcode(0,arg1++) = compile(s,ns,depth1,0,bloc_flags);
- s = ns;
- }
- l_opcode[0].move_to(opcode);
- if (arg1<12 || arg1>opcode._height) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: %s arguments provided, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- arg1<12?"Not enough":"Too much",s0);
- }
- _cimg_mp_check_type(opcode[2],1,2,0); // A
- _cimg_mp_check_const_scalar(opcode[3],2,3); // wA
- _cimg_mp_check_const_scalar(opcode[4],3,3); // hA
- _cimg_mp_check_const_scalar(opcode[5],4,3); // dA
- _cimg_mp_check_const_scalar(opcode[6],5,3); // sA
- _cimg_mp_check_type(opcode[7],6,2,0); // M
- _cimg_mp_check_const_scalar(opcode[8],7,3); // wM
- _cimg_mp_check_const_scalar(opcode[9],8,3); // hM
- _cimg_mp_check_const_scalar(opcode[10],9,3); // dM
- _cimg_mp_check_const_scalar(opcode[11],10,3); // sM
- _cimg_mp_check_type(opcode[12],11,1,0); // boundary_conditions
- _cimg_mp_check_type(opcode[13],12,1,0); // is_normalized
- _cimg_mp_check_const_scalar(opcode[14],13,1); // channel_mode
- if (opcode[15]!=~0U) _cimg_mp_check_type(opcode[15],14,1,0); // xcenter
- if (opcode[16]!=~0U) _cimg_mp_check_type(opcode[16],15,1,0); // ycenter
- if (opcode[17]!=~0U) _cimg_mp_check_type(opcode[17],16,1,0); // zcenter
- _cimg_mp_check_const_scalar(opcode[18],17,1); // xstart
- _cimg_mp_check_const_scalar(opcode[19],18,1); // ystart
- _cimg_mp_check_const_scalar(opcode[20],19,1); // zstart
- if (opcode[21]!=~0U) _cimg_mp_check_const_scalar(opcode[21],20,1); // xend
- if (opcode[22]!=~0U) _cimg_mp_check_const_scalar(opcode[22],21,1); // yend
- if (opcode[23]!=~0U) _cimg_mp_check_const_scalar(opcode[23],22,1); // zend
- _cimg_mp_check_const_scalar(opcode[24],23,0); // xstride
- _cimg_mp_check_const_scalar(opcode[25],24,0); // ystride
- _cimg_mp_check_const_scalar(opcode[26],25,0); // zstride
- _cimg_mp_check_type(opcode[27],26,1,0); // xdilation
- _cimg_mp_check_type(opcode[28],27,1,0); // ydilation
- _cimg_mp_check_type(opcode[29],28,1,0); // zdilation
- _cimg_mp_check_type(opcode[30],29,1,0); // interpolation_type
- const unsigned int
- wA = (unsigned int)mem[opcode[3]],
- hA = (unsigned int)mem[opcode[4]],
- dA = (unsigned int)mem[opcode[5]],
- sA = (unsigned int)mem[opcode[6]],
- wM = (unsigned int)mem[opcode[8]],
- hM = (unsigned int)mem[opcode[9]],
- dM = (unsigned int)mem[opcode[10]],
- sM = (unsigned int)mem[opcode[11]],
- channel_mode = (unsigned int)mem[opcode[14]];
- const int
- xstart = (int)mem[opcode[18]],
- ystart = (int)mem[opcode[19]],
- zstart = (int)mem[opcode[20]],
- xend = opcode[21]!=~0U?(int)mem[opcode[21]]:wA - 1,
- yend = opcode[22]!=~0U?(int)mem[opcode[22]]:hA - 1,
- zend = opcode[23]!=~0U?(int)mem[opcode[23]]:dA - 1;
- if (xstart>xend || ystart>yend || zstart>zend) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid xyz-start/end arguments "
- "(start = (%d,%d,%d), end = (%d,%d,%d)), in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- xstart,ystart,zstart,xend,yend,zend,s0);
- }
- const float
- xstride = (float)mem[opcode[24]],
- ystride = (float)mem[opcode[25]],
- zstride = (float)mem[opcode[26]];
- if (xstride<=0 || ystride<=0 || zstride<=0) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid stride arguments (%g,%g,%g), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- xstride,ystride,zstride,s0);
- }
- arg2 = xend - xstart + 1;
- arg3 = yend - ystart + 1;
- arg4 = zend + zstart + 1;
- arg5 = !channel_mode?sA*sM:channel_mode==1?std::max(sA,sM):
- channel_mode==2?std::max(sA,sM)/std::min(sA,sM):1U;
- opcode[1] = pos = vector(arg2*arg3*arg4*arg5);
- opcode[3] = (ulongT)wA;
- opcode[4] = (ulongT)hA;
- opcode[5] = (ulongT)dA;
- opcode[6] = (ulongT)sA;
- opcode[8] = (ulongT)wM;
- opcode[9] = (ulongT)hM;
- opcode[10] = (ulongT)dM;
- opcode[11] = (ulongT)sM;
- opcode[14] = (ulongT)channel_mode;
- opcode[18] = (ulongT)xstart;
- opcode[19] = (ulongT)ystart;
- opcode[20] = (ulongT)zstart;
- opcode[21] = (ulongT)xend;
- opcode[22] = (ulongT)yend;
- opcode[23] = (ulongT)zend;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'd' :
- if (*ss1=='(') { // Image depth
- _cimg_mp_op("Function 'd()'");
- if (*ss2=='#') { // Index specified
- p1 = compile(ss3,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss2!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_d,p1);
- }
- if (!std::strncmp(ss,"da_back(",8) ||
- !std::strncmp(ss,"da_pop(",7)) { // Get latest element in a dynamic array
- if (!is_inside_critical) is_parallelizable = false;
- const bool is_pop = *ss3=='p';
- _cimg_mp_op(is_pop?"Function 'da_pop()'":"Function 'da_back()'");
- s0 = ss + (is_pop?7:8);
- if (*s0=='#') { // Index specified
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p1 = compile(s0,s1++,depth1,0,bloc_flags);
- } else { p1 = 11; s1 = s0; }
- _cimg_mp_check_list();
- _cimg_mp_check_const_scalar(p1,1,1);
- p3 = (unsigned int)cimg::mod((int)mem[p1],imglist.width());
- p2 = imglist[p3]._spectrum;
- if (p2>1) pos = vector(p2); else pos = scalar(); // Return vector or scalar result
- CImg<ulongT>::vector((ulongT)mp_da_back_or_pop,pos,p2,p1,is_pop).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"da_insert(",10) ||
- !std::strncmp(ss,"da_push(",8)) { // Insert element(s) in a dynamic array
- if (!is_inside_critical) is_parallelizable = false;
- const bool is_push = *ss3=='p';
- _cimg_mp_op(is_push?"Function 'da_push()'":"Function 'da_insert()'");
- s0 = ss + (is_push?8:10);
- if (*s0=='#') { // Index specified
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p1 = compile(s0,s1++,depth1,0,bloc_flags);
- } else { p1 = 11; s1 = s0; }
- _cimg_mp_check_list();
- if (!is_push) {
- s0 = s1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1++,depth1,0,bloc_flags); // Position
- } else arg1 = ~0U;
- CImg<ulongT>::vector((ulongT)mp_da_insert_or_push,_cimg_mp_slot_nan,p1,arg1,0,0).move_to(l_opcode);
- p3 = p1==~0U?2:3;
- p1 = ~0U;
- for (s = s1; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags); // Element
- p2 = _cimg_mp_size(arg2);
- if (p1==~0U) p1 = p2;
- else {
- if (!p1) _cimg_mp_check_type(arg2,p3,1,0);
- else _cimg_mp_check_type(arg2,p3,2,p1);
- }
- CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- ++p3;
- }
- if (p1==~0U) compile(s1 + 1,se1,depth1,0,bloc_flags); // Missing element -> error
- (l_opcode>'y').move_to(opcode);
- opcode[4] = p1;
- opcode[5] = opcode._height;
- opcode.move_to(code);
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"da_remove(",10)) { // Remove element(s) in a dynamic array
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_op("Function 'da_remove()'");
- if (ss[10]=='#') { // Index specified
- s0 = ss + 11; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss + 11,s0++,depth1,0,bloc_flags);
- } else { p1 = 11; s0 = ss + 10; }
- _cimg_mp_check_list();
- arg1 = arg2 = ~0U;
- if (s0<se1) {
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags); // Starting position
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U; // Ending position
- }
- CImg<ulongT>::vector((ulongT)mp_da_remove,_cimg_mp_slot_nan,p1,arg1,arg2).move_to(code);
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"da_size(",8)) { // Size of a dynamic array
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_op("Function 'da_size()'");
- if (ss[8]=='#') { // Index specified
- s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
- } else { p1 = 11; s0 = ss + 8; }
- _cimg_mp_check_list();
- _cimg_mp_scalar1(mp_da_size,p1);
- }
- if (!std::strncmp(ss,"date(",5)) { // Current date or file date
- _cimg_mp_op("Function 'date()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = ss5!=se1?compile(ss5,s1,depth1,0,bloc_flags):~0U;
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):~0U;
- if (arg2!=~0U) _cimg_mp_check_type(arg2,1,2,0);
- pos = arg1==~0U || _cimg_mp_is_vector(arg1)?vector(arg1==~0U?7:_cimg_mp_size(arg1)):scalar();
- CImg<ulongT>::vector((ulongT)mp_date,pos,_cimg_mp_size(pos),
- arg1,arg1==~0U?~0U:_cimg_mp_size(arg1),
- arg2,arg2==~0U?~0U:_cimg_mp_size(arg2)).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"debug(",6)) { // Print debug info
- _cimg_mp_op("Function 'debug()'");
- p1 = code._width;
- arg1 = compile(ss6,se1,depth1,p_ref,bloc_flags);
- *se1 = 0;
- variable_name.assign(CImg<charT>::string(ss6,true,true).unroll('y'),true);
- cimg::strpare(variable_name,false,true);
- ((CImg<ulongT>::vector((ulongT)mp_debug,arg1,0,code._width - p1),
- variable_name)>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code,p1);
- *se1 = ')';
- _cimg_mp_return(arg1);
- }
- if (!std::strncmp(ss,"deg2rad(",8)) { // Degrees to radians
- _cimg_mp_op("Function 'deg2rad()'");
- arg1 = compile(ss8,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_deg2rad,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*cimg::PI/180);
- _cimg_mp_scalar1(mp_deg2rad,arg1);
- }
- if (!std::strncmp(ss,"display(",8)) { // Display memory, vector or image
- _cimg_mp_op("Function 'display()'");
- if (pexpr[se2 - expr._data]=='(') { // no arguments?
- CImg<ulongT>::vector((ulongT)mp_display_memory,_cimg_mp_slot_nan).move_to(code);
- _cimg_mp_return_nan();
- }
- if (*ss8!='#') { // Vector
- s1 = ss8; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss8,s1,depth1,0,bloc_flags);
- arg2 = 0; arg3 = arg4 = arg5 = 1;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1 + 1,s2,depth1,0,bloc_flags);
- if (s2<se1) {
- s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(s2,s3,depth1,0,bloc_flags);
- if (s3<se1) {
- s2 = ++s3; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(s3,s2,depth1,0,bloc_flags);
- arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- }
- }
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- _cimg_mp_check_type(arg4,4,1,0);
- _cimg_mp_check_type(arg5,5,1,0);
- c1 = *s1; *s1 = 0;
- variable_name.assign(CImg<charT>::string(ss8,true,true).unroll('y'),true);
- cimg::strpare(variable_name,false,true);
- if (_cimg_mp_is_vector(arg1))
- ((CImg<ulongT>::vector((ulongT)mp_vector_print,arg1,0,(ulongT)_cimg_mp_size(arg1),0),
- variable_name)>'y').move_to(opcode);
- else
- ((CImg<ulongT>::vector((ulongT)mp_print,arg1,0,0),
- variable_name)>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- ((CImg<ulongT>::vector((ulongT)mp_display,arg1,0,(ulongT)_cimg_mp_size(arg1),
- arg2,arg3,arg4,arg5),
- variable_name)>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- *s1 = c1;
- _cimg_mp_return(arg1);
- } else { // Image
- p1 = compile(ss8 + 1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- CImg<ulongT>::vector((ulongT)mp_image_display,_cimg_mp_slot_nan,p1).move_to(code);
- _cimg_mp_return_nan();
- }
- }
- if (!std::strncmp(ss,"det(",4)) { // Matrix determinant
- _cimg_mp_op("Function 'det()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_matrix_square(arg1,1);
- p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
- _cimg_mp_scalar2(mp_det,arg1,p1);
- }
- if (!std::strncmp(ss,"diag(",5)) { // Diagonal matrix
- _cimg_mp_op("Function 'diag()'");
- CImg<ulongT>::vector((ulongT)mp_diag,0,0).move_to(l_opcode);
- for (s = ss5; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- arg1 = opcode._height - 3;
- pos = vector(arg1*arg1);
- opcode[1] = pos;
- opcode[2] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"dot(",4)) { // Dot product
- _cimg_mp_op("Function 'dot()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) {
- _cimg_mp_check_type(arg2,2,2,_cimg_mp_size(arg1));
- _cimg_mp_scalar3(mp_dot,arg1,arg2,_cimg_mp_size(arg1));
- }
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_scalar2(mp_mul,arg1,arg2);
- }
- if (!std::strncmp(ss,"do(",3)) { // Do..while
- _cimg_mp_op("Function 'do()'");
- s0 = *ss2=='('?ss3:ss8;
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = code._width;
- arg6 = mempos;
- p1 = compile(s0,s1,depth1,0,bloc_flags); // Body
- arg2 = code._width;
- p2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):p1; // Condition
- _cimg_mp_check_type(p2,2,1,0);
- CImg<ulongT>::vector((ulongT)mp_do,p1,p2,arg2 - arg1,code._width - arg2,_cimg_mp_size(p1),
- p1>=arg6 && !_cimg_mp_is_const_scalar(p1),
- p2>=arg6 && !_cimg_mp_is_const_scalar(p2)).move_to(code,arg1);
- _cimg_mp_return(p1);
- }
- if (!std::strncmp(ss,"draw(",5)) { // Draw image
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_op("Function 'draw()'");
- if (*ss5=='#') { // Index specified
- s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss6,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss5; }
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- arg2 = is_relative?0U:(unsigned int)_cimg_mp_slot_x;
- arg3 = is_relative?0U:(unsigned int)_cimg_mp_slot_y;
- arg4 = is_relative?0U:(unsigned int)_cimg_mp_slot_z;
- arg5 = is_relative?0U:(unsigned int)_cimg_mp_slot_c;
- s0 = se1;
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg2 = compile(++s1,s0,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2)) { // Coordinates specified as a vector
- p2 = _cimg_mp_size(arg2);
- ++arg2;
- if (p2>1) {
- arg3 = arg2 + 1;
- if (p2>2) {
- arg4 = arg3 + 1;
- if (p2>3) arg5 = arg4 + 1;
- }
- }
- ++s0;
- is_sth = true;
- } else {
- if (s0<se1) {
- is_sth = p1!=~0U;
- s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(++s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,is_sth?4:3,1,0);
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg4 = compile(++s1,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg4,is_sth?5:4,1,0);
- if (s0<se1) {
- s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg5 = compile(++s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg5,is_sth?6:5,1,0);
- s0 = ++s1;
- }
- }
- }
- is_sth = false;
- }
- }
- l_opcode.assign(); // Don't use 'opcode': it could be modified by further calls to 'compile()'!
- CImg<ulongT>::vector((ulongT)mp_draw,arg1,(ulongT)_cimg_mp_size(arg1),p1,arg2,arg3,arg4,arg5,
- 0,0,0,0,1,(ulongT)~0U,0,1).move_to(l_opcode);
- arg2 = arg3 = arg4 = arg5 = ~0U;
- p2 = p1!=~0U?0:1;
- if (s0<se1) {
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg2 = compile(s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,p2 + (is_sth?3:6),1,0);
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg3 = compile(++s1,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,p2 + (is_sth?4:7),1,0);
- if (s0<se1) {
- s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg4 = compile(++s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg4,p2 + (is_sth?5:8),1,0);
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg5 = compile(++s1,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg5,p2 + (is_sth?6:9),1,0);
- }
- }
- }
- }
- if (s0<s1) s0 = s1;
- l_opcode(0,8) = (ulongT)arg2;
- l_opcode(0,9) = (ulongT)arg3;
- l_opcode(0,10) = (ulongT)arg4;
- l_opcode(0,11) = (ulongT)arg5;
- if (s0<se1) {
- s1 = s0 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg6 = compile(++s0,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg6,0,1,0);
- l_opcode(0,12) = arg6;
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p2 = compile(++s1,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(p2,0,2,0);
- l_opcode(0,13) = p2;
- l_opcode(0,14) = _cimg_mp_size(p2);
- p3 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(p3,0,1,0);
- l_opcode(0,15) = p3;
- }
- }
- l_opcode[0].move_to(code);
- _cimg_mp_return(arg1);
- }
- break;
- case 'e' :
- if (!std::strncmp(ss,"echo(",5)) { // Echo
- _cimg_mp_op("Function 'echo()'");
- CImg<ulongT>::vector((ulongT)mp_echo,_cimg_mp_slot_nan,0).move_to(l_opcode);
- for (s = ss5; s<se1; ++s) {
- ns = s; while (ns<se1 && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg1 = compile(s,ns,depth1,0,bloc_flags);
- CImg<ulongT>::vector(arg1,_cimg_mp_size(arg1)).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"eig(",4)) { // Matrix eigenvalues/eigenvector
- _cimg_mp_op("Function 'eig()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_matrix_square(arg1,1);
- p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
- pos = vector((p1 + 1)*p1);
- CImg<ulongT>::vector((ulongT)mp_matrix_eig,pos,arg1,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"ellipse(",8)) { // Ellipse/circle drawing
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_op("Function 'ellipse()'");
- if (*ss8=='#') { // Index specified
- s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss8; }
- if (s0==se1) compile(s0,se1,depth1,0,bloc_flags); // 'missing' argument error
- CImg<ulongT>::vector((ulongT)mp_ellipse,_cimg_mp_slot_nan,0,p1).move_to(l_opcode);
- for (s = s0; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"erf(",4)) { // Error function
- _cimg_mp_op("Function 'erf()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_erf,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::erf(mem[arg1]));
- _cimg_mp_scalar1(mp_erf,arg1);
- }
- if (!std::strncmp(ss,"erfinv(",7)) { // Inverse of error function
- _cimg_mp_op("Function 'erfinv()'");
- arg1 = compile(ss7,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_erfinv,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::erfinv(mem[arg1]));
- _cimg_mp_scalar1(mp_erfinv,arg1);
- }
- if (!std::strncmp(ss,"exp(",4)) { // Exponential
- _cimg_mp_op("Function 'exp()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_exp,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::exp(mem[arg1]));
- _cimg_mp_scalar1(mp_exp,arg1);
- }
- if (!std::strncmp(ss,"expr(",5)) { // Vector from expression
- _cimg_mp_op("Function 'expr()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,2,0);
- p1 = _cimg_mp_size(arg1);
- arg2 = arg3 = arg4 = arg5 = 0;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg2,2,2);
- arg2 = (unsigned int)mem[arg2];
- if (arg2) arg3 = arg4 = arg5 = 1;
- if (s2<se1) {
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(++s2,s1,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg3,3,3);
- arg3 = (unsigned int)mem[arg3];
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(++s1,s2,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg4,4,3);
- arg4 = (unsigned int)mem[arg4];
- arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- _cimg_mp_check_const_scalar(arg5,5,3);
- arg5 = (unsigned int)mem[arg5];
- }
- }
- }
- p2 = arg2*arg3*arg4*arg5;
- if (p2) pos = vector(p2); else pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_expr,pos,arg1,p1,arg2,arg3,arg4,arg5).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"eye(",4)) { // Identity matrix
- _cimg_mp_op("Function 'eye()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg1,1,3);
- p1 = (unsigned int)mem[arg1];
- pos = vector(p1*p1);
- CImg<ulongT>::vector((ulongT)mp_eye,pos,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"end(",4)) { // End
- _cimg_mp_op("Function 'end()'");
- s1 = ss4; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
- if (s1!=se1) {
- const bool is_inside_end = (bool)(bloc_flags&8);
- if (!is_inside_end) code.swap(code_end);
- compile(s1,se1,depth1,p_ref,8);
- if (!is_inside_end) code.swap(code_end);
- is_end_code = true;
- }
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"end_t(",6)) { // End thread
- _cimg_mp_op("Function 'end_t()'");
- s1 = ss6; while (s1<se1 && cimg::is_blank(*s1)) ++s1;
- if (s1!=se1) {
- const bool is_inside_end = (bool)(bloc_flags&16);
- if (!is_inside_end) code.swap(code_end_t);
- compile(s1,se1,depth1,p_ref,16);
- if (!is_inside_end) code.swap(code_end_t);
- is_end_code = true;
- }
- _cimg_mp_return_nan();
- }
- break;
- case 'f' :
- if (!std::strncmp(ss,"f2ui(",5)) { // Special float->uint conversion
- _cimg_mp_op("Function 'f2ui()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_f2ui,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((double)cimg::float2uint((float)mem[arg1]));
- _cimg_mp_scalar1(mp_f2ui,arg1);
- }
- if (!std::strncmp(ss,"fact(",5)) { // Factorial
- _cimg_mp_op("Function 'fact()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_factorial,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::factorial((int)mem[arg1]));
- _cimg_mp_scalar1(mp_factorial,arg1);
- }
- if (!std::strncmp(ss,"fibo(",5)) { // Fibonacci
- _cimg_mp_op("Function 'fibo()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_fibonacci,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::fibonacci((int)mem[arg1]));
- _cimg_mp_scalar1(mp_fibonacci,arg1);
- }
- if (!std::strncmp(ss,"fill(",5)) { // Fill
- _cimg_mp_op("Function 'fill()'");
- s0 = ss5; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg1 = compile(ss5,s0,depth1,0,bloc_flags); // Object to fill
- if (_cimg_mp_is_const_scalar(arg1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Target scalar is constant, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,ss);
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p1 = code._width;
- if (s1<se1) { // Version with 3 arguments
- variable_name.assign(s0,(unsigned int)(s1 + 1 - s0)).back() = 0;
- cimg::strpare(variable_name,false,true);
- if (!is_varname(variable_name)) { // Invalid variable name
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid loop variable name '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- variable_name._data,s0);
- }
- get_variable_pos(variable_name,arg2,arg3);
- arg2 = arg3!=~0U?reserved_label[arg3]:arg2!=~0U?variable_pos[arg2]:~0U; // Variable slot
- if (arg2!=~0U && (!_cimg_mp_is_scalar(arg2) ||
- _cimg_mp_is_const_scalar(arg2))) { // Variable is not a vector or is a constant->error
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' "
- "(expected 'scalar'), in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg2)._data,variable_name._data,s0);
- } else if (arg2==~0U) { // Variable does not exist -> create it
- arg2 = scalar();
- if (arg3!=~0U) reserved_label[arg3] = arg2;
- else {
- if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
- variable_pos[variable_def._width] = arg2;
- variable_name.move_to(variable_def);
- }
- memtype[arg2] = -1;
- }
- arg3 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,3,1,0);
- } else { // Version with 2 arguments
- arg2 = ~0U;
- arg3 = compile(s0,se1,depth1,0,bloc_flags);
- }
- // arg2 = variable slot, arg3 = fill expression.
- _cimg_mp_check_type(arg3,3,1,0);
- CImg<ulongT>::vector((ulongT)mp_fill,arg1,_cimg_mp_size(arg1),arg2,arg3,code._width - p1).
- move_to(code,p1);
- _cimg_mp_return(arg1);
- }
- if (!std::strncmp(ss,"find(",5)) { // Find
- _cimg_mp_op("Function 'find()'");
- // First argument: data to look at.
- s0 = ss5; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- if (*ss5=='#') { // Index specified
- p1 = compile(ss6,s0,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- arg1 = ~0U;
- } else { // Vector specified
- arg1 = compile(ss5,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,2,0);
- p1 = ~0U;
- }
- // Second argument: data to find.
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg2 = compile(s0,s1,depth1,0,bloc_flags);
- // Third and fourth arguments: starting index and search direction.
- arg3 = _cimg_mp_slot_nan; arg4 = 1;
- if (s1<se1) {
- s0 = s1 + 1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg3 = compile(++s1,s0,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,3,1,0);
- if (s0<se1) {
- arg4 = compile(++s0,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg4,4,1,0);
- }
- }
- if (p1!=~0U) {
- if (_cimg_mp_size(arg2)>1)
- _cimg_mp_scalar5(mp_list_find_seq,p1,arg2,_cimg_mp_size(arg2),arg3,arg4);
- _cimg_mp_scalar4(mp_list_find,p1,arg2 + (_cimg_mp_size(arg2)?1:0),arg3,arg4);
- }
- if (_cimg_mp_size(arg2)>1)
- _cimg_mp_scalar6(mp_find_seq,arg1,_cimg_mp_size(arg1),arg2,_cimg_mp_size(arg2),arg3,arg4);
- _cimg_mp_scalar5(mp_find,arg1,_cimg_mp_size(arg1),arg2 + (_cimg_mp_size(arg2)?1:0),arg3,arg4);
- }
- if (*ss1=='o' && *ss2=='r' && *ss3=='(') { // For loop
- _cimg_mp_op("Function 'for()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg1 = code._width;
- p1 = compile(ss4,s1,depth1,0,bloc_flags); // Init
- arg2 = code._width;
- p2 = compile(++s1,s2,depth1,0,bloc_flags); // Cond
- arg3 = code._width;
- arg6 = mempos;
- if (s3<se1) { // Body + post
- p3 = compile(s3 + 1,se1,depth1,0,bloc_flags); // Body
- arg4 = code._width;
- pos = compile(++s2,s3,depth1,0,bloc_flags); // Post
- } else {
- p3 = compile(++s2,se1,depth1,0,bloc_flags); // Body only
- arg4 = pos = code._width;
- }
- _cimg_mp_check_type(p2,2,1,0);
- arg5 = _cimg_mp_size(pos);
- CImg<ulongT>::vector((ulongT)mp_for,p3,(ulongT)_cimg_mp_size(p3),p2,arg2 - arg1,arg3 - arg2,
- arg4 - arg3,code._width - arg4,
- p3>=arg6 && !_cimg_mp_is_const_scalar(p3),
- p2>=arg6 && !_cimg_mp_is_const_scalar(p2)).move_to(code,arg1);
- _cimg_mp_return(p3);
- }
- if (!std::strncmp(ss,"floor(",6)) { // Floor
- _cimg_mp_op("Function 'floor()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_floor,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::floor(mem[arg1]));
- _cimg_mp_scalar1(mp_floor,arg1);
- }
- if (!std::strncmp(ss,"fsize(",6)) { // File size
- _cimg_mp_op("Function 'fsize()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,2,0);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_fsize,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'g' :
- if (!std::strncmp(ss,"gauss(",6)) { // Gaussian function
- _cimg_mp_op("Function 'gauss()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- arg2 = arg3 = 1;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
- }
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_gauss,arg1,arg2,arg3);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3)) {
- val1 = mem[arg1];
- val2 = mem[arg2];
- _cimg_mp_const_scalar(std::exp(-val1*val1/(2*val2*val2))/(mem[arg3]?std::sqrt(2*val2*val2*cimg::PI):1));
- }
- _cimg_mp_scalar3(mp_gauss,arg1,arg2,arg3);
- }
- if (!std::strncmp(ss,"gcd(",4)) { // Gcd
- _cimg_mp_op("Function 'gcd()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- _cimg_mp_check_type(arg2,2,1,0);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(cimg::gcd((long)mem[arg1],(long)mem[arg2]));
- _cimg_mp_scalar2(mp_gcd,arg1,arg2);
- }
- #ifdef cimg_mp_func_get
- if (!std::strncmp(ss,"get(",4)) { // Get value/vector from external variable
- _cimg_mp_op("Function 'get()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = arg3 = 0;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_const_scalar(arg2,2,2);
- _cimg_mp_check_type(arg3,3,1,0);
- p1 = _cimg_mp_size(arg1);
- arg2 = (unsigned int)mem[arg2];
- if (arg2) pos = vector(arg2); else pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_get,pos,arg1,p1,arg2,arg3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- #endif
- break;
- case 'h' :
- if (*ss1=='(') { // Image height
- _cimg_mp_op("Function 'h()'");
- if (*ss2=='#') { // Index specified
- p1 = compile(ss3,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss2!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_h,p1);
- }
- break;
- case 'i' :
- if (*ss1=='c' && *ss2=='(') { // Image median
- _cimg_mp_op("Function 'ic()'");
- if (*ss3=='#') { // Index specified
- p1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss3!=se1) break; p1 = ~0U; }
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_image_median,pos,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (*ss1=='n' && *ss2=='(') { // Image norm
- _cimg_mp_op("Function 'in()'");
- if (*ss3=='#') { // Index specified
- p1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss3!=se1) break; p1 = ~0U; }
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_image_norm,pos,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (*ss1=='f' && *ss2=='(') { // If..then[..else.]
- _cimg_mp_op("Function 'if()'");
- s1 = ss3; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg1 = compile(ss3,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- if (_cimg_mp_is_const_scalar(arg1)) {
- if ((bool)mem[arg1]) return compile(++s1,s2,depth1,0,bloc_flags);
- else return s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- p2 = code._width;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- p3 = code._width;
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):
- _cimg_mp_is_vector(arg2)?vector(_cimg_mp_size(arg2),0):0;
- _cimg_mp_check_type(arg3,3,_cimg_mp_is_vector(arg2)?2:1,_cimg_mp_size(arg2));
- arg4 = _cimg_mp_size(arg2);
- if (arg4) pos = vector(arg4); else pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_if,pos,arg1,arg2,arg3,
- p3 - p2,code._width - p3,arg4).move_to(code,p2);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"inrange(",8)) { // Check value range
- _cimg_mp_op("Function 'inrange()'");
- s1 = ss8; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss8,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(++s2,s1,depth1,0,bloc_flags);
- arg4 = arg5 = 1;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(++s1,s2,depth1,0,bloc_flags);
- arg5 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):arg4;
- _cimg_mp_check_type(arg4,4,1,0);
- _cimg_mp_check_type(arg5,5,1,0);
- }
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) &&
- _cimg_mp_is_const_scalar(arg3) && _cimg_mp_is_const_scalar(arg4) &&
- _cimg_mp_is_const_scalar(arg5)) { // Optimize constant case
- val = mem[arg1]; val1 = mem[arg2]; val2 = mem[arg3];
- if (val2>=val1)
- is_sth = (mem[arg4]?(val>=val1):(val>val1)) && (mem[arg5]?(val<=val2):(val<val2));
- else
- is_sth = (mem[arg5]?(val>=val2):(val>val2)) && (mem[arg4]?(val<=val1):(val<val1));
- _cimg_mp_return(is_sth?1:0);
- }
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- p3 = _cimg_mp_size(arg3);
- arg6 = ~0U; // Size of return value
- if (!p1) {
- arg6 = p2?p2:p3;
- if (arg6) _cimg_mp_check_type(arg3,3,3,arg6);
- } else {
- arg6 = p1;
- _cimg_mp_check_type(arg2,2,3,arg6);
- _cimg_mp_check_type(arg3,3,3,arg6);
- }
- pos = arg6?vector(arg6):scalar();
- CImg<ulongT>::vector((ulongT)mp_inrange,pos,arg6,arg1,p1,arg2,p2,arg3,p3,arg4,arg5).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"int(",4)) { // Integer cast
- _cimg_mp_op("Function 'int()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_int,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar((longT)mem[arg1]);
- _cimg_mp_scalar1(mp_int,arg1);
- }
- if (!std::strncmp(ss,"invert(",7)) { // Matrix/scalar inversion
- _cimg_mp_op("Function 'invert()'");
- s1 = ss7; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss7,s1,depth1,0,bloc_flags);
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(arg2,2,1,0);
- if (_cimg_mp_is_vector(arg1)) {
- _cimg_mp_check_matrix_square(arg1,1);
- p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
- pos = vector(p1*p1);
- CImg<ulongT>::vector((ulongT)mp_matrix_invert,pos,arg1,p1,arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(1/mem[arg1]);
- _cimg_mp_scalar2(mp_div,1,arg1);
- }
- if (*ss1=='s') { // Family of 'is_?()' functions
- if (!std::strncmp(ss,"isbool(",7)) { // Is boolean?
- _cimg_mp_op("Function 'isbool()'");
- if (ss7==se1) _cimg_mp_return(0);
- try { arg1 = compile(ss7,se1,depth1,0,bloc_flags); }
- catch(CImgException&) { _cimg_mp_return(0); }
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isbool,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_return(mem[arg1]==0. || mem[arg1]==1.);
- _cimg_mp_scalar1(mp_isbool,arg1);
- }
- if (!std::strncmp(ss,"isdir(",6)) { // Is directory?
- _cimg_mp_op("Function 'isdir()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_isdir,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"isfile(",7)) { // Is file?
- _cimg_mp_op("Function 'isfile()'");
- arg1 = compile(ss7,se1,depth1,0,bloc_flags);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_isfile,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"isin(",5)) { // Is in sequence/vector?
- if (ss5>=se1) _cimg_mp_return(0);
- _cimg_mp_op("Function 'isin()'");
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_isin,pos,0).move_to(l_opcode);
- for (s = ss5; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg1 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1))
- CImg<ulongT>::sequence(_cimg_mp_size(arg1),arg1 + 1,
- arg1 + (ulongT)_cimg_mp_size(arg1)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg1).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"isinf(",6)) { // Is infinite?
- _cimg_mp_op("Function 'isinf()'");
- if (ss6==se1) _cimg_mp_return(0);
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isinf,arg1);
- if (_cimg_mp_is_const_scalar(arg1))
- _cimg_mp_return((unsigned int)cimg::type<double>::is_inf(mem[arg1]));
- _cimg_mp_scalar1(mp_isinf,arg1);
- }
- if (!std::strncmp(ss,"isint(",6)) { // Is integer?
- _cimg_mp_op("Function 'isint()'");
- if (ss6==se1) _cimg_mp_return(0);
- try { arg1 = compile(ss6,se1,depth1,0,bloc_flags); }
- catch(CImgException&) { _cimg_mp_return(0); }
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isint,arg1);
- if (_cimg_mp_is_const_scalar(arg1))
- _cimg_mp_return((unsigned int)((double)(longT)mem[arg1]==mem[arg1]));
- _cimg_mp_scalar1(mp_isint,arg1);
- }
- if (!std::strncmp(ss,"isnan(",6)) { // Is NaN?
- _cimg_mp_op("Function 'isnan()'");
- if (ss6==se1) _cimg_mp_return(0);
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_isnan,arg1);
- if (_cimg_mp_is_const_scalar(arg1))
- _cimg_mp_return((unsigned int)cimg::type<double>::is_nan(mem[arg1]));
- _cimg_mp_scalar1(mp_isnan,arg1);
- }
- if (!std::strncmp(ss,"isnum(",6)) { // Is number?
- _cimg_mp_op("Function 'isnum()'");
- val = 0;
- if (cimg_sscanf(ss6,"%lf%c%c",&val,&sep,&end)==2 && sep==')') _cimg_mp_return(1);
- _cimg_mp_return(0);
- }
- if (!std::strncmp(ss,"isexpr(",7)) { // Is valid expression?
- _cimg_mp_op("Function 'isexpr()'");
- if (ss7==se1) _cimg_mp_return(0);
- try { arg1 = compile(ss7,se1,depth1,0,bloc_flags); }
- catch (CImgException&) { _cimg_mp_return(0); }
- _cimg_mp_return(1);
- }
- if (!std::strncmp(ss,"isvarname(",10)) { // Is variable name?
- _cimg_mp_op("Function 'isvarname()'");
- arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_isvarname,pos,arg1,(ulongT)_cimg_mp_size(arg1)).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- }
- break;
- case 'l' :
- if (*ss1=='(') { // Size of image list
- _cimg_mp_op("Function 'l()'");
- if (ss2!=se1) break;
- _cimg_mp_scalar0(mp_list_l);
- }
- if (!std::strncmp(ss,"lerp(",5)) { // Linear interpolation
- _cimg_mp_op("Function 'lerp()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):16; // Default value is 0.5
- _cimg_mp_check_type(arg3,3,1,0);
- if (_cimg_mp_is_const_scalar(arg3)) { // Optimize constant cases
- if (!arg3) _cimg_mp_return(arg1);
- if (arg3==1) _cimg_mp_return(arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2)) {
- const double t = mem[arg3];
- _cimg_mp_const_scalar(mem[arg1]*(1-t) + mem[arg2]*t);
- }
- }
- if (_cimg_mp_is_scalar(arg1)) {
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_scalar3(mp_lerp,arg1,arg2,arg3);
- }
- p1 = _cimg_mp_size(arg1);
- _cimg_mp_check_type(arg2,2,2,p1);
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_vector_lerp,pos,p1,arg1,arg2,arg3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"log(",4)) { // Natural logarithm
- _cimg_mp_op("Function 'log()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::log(mem[arg1]));
- _cimg_mp_scalar1(mp_log,arg1);
- }
- if (!std::strncmp(ss,"log2(",5)) { // Base-2 logarithm
- _cimg_mp_op("Function 'log2()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log2,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::log2(mem[arg1]));
- _cimg_mp_scalar1(mp_log2,arg1);
- }
- if (!std::strncmp(ss,"log10(",6)) { // Base-10 logarithm
- _cimg_mp_op("Function 'log10()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_log10,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::log10(mem[arg1]));
- _cimg_mp_scalar1(mp_log10,arg1);
- }
- if (!std::strncmp(ss,"lowercase(",10)) { // Lower case
- _cimg_mp_op("Function 'lowercase()'");
- arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_lowercase,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::lowercase(mem[arg1]));
- _cimg_mp_scalar1(mp_lowercase,arg1);
- }
- break;
- case 'm' :
- if (!std::strncmp(ss,"mul(",4)) { // Matrix multiplication
- _cimg_mp_op("Function 'mul()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_type(arg2,2,2,0);
- _cimg_mp_check_const_scalar(arg3,3,3);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- p3 = (unsigned int)mem[arg3];
- arg5 = p2/p3;
- arg4 = p1/arg5;
- if (arg4*arg5!=p1 || arg5*p3!=p2) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Types of first and second arguments ('%s' and '%s') "
- "do not match with third argument 'nb_colsB=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,s_type(arg2)._data,p3,s0);
- }
- pos = vector(arg4*p3);
- CImg<ulongT>::vector((ulongT)mp_matrix_mul,pos,arg1,arg2,arg4,arg5,p3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"mproj(",6)) { // Project matrix onto dictionary
- _cimg_mp_op("Function 'mproj()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags); // S
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags); // ncolS
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(++s2,s1,depth1,0,bloc_flags); // D
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(++s1,s2,depth1,0,bloc_flags); // ncolD
- arg5 = arg6 = p3 = 0;
- if (s2<se1) {
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg5 = compile(++s2,s1,depth1,0,bloc_flags); // method
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg6 = compile(++s1,s2,depth1,0,bloc_flags); // max_iter
- p3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0; // method
- }
- }
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_const_scalar(arg2,2,3);
- _cimg_mp_check_type(arg3,3,2,0);
- _cimg_mp_check_const_scalar(arg4,4,3);
- _cimg_mp_check_type(arg5,5,1,0);
- _cimg_mp_check_type(arg6,6,1,0);
- _cimg_mp_check_type(p3,7,1,0);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg3);
- const unsigned int
- wS = (unsigned int)mem[arg2],
- wD = (unsigned int)mem[arg4],
- hS = p1/wS,
- hD = p2/wD;
- if (wS*hS!=p1) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Type of first argument ('%s') "
- "do not match with second argument 'nb_colsS=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,wS,s0);
- }
- if (wD*hD!=p2) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Type of third argument ('%s') "
- "do not match with fourth argument 'nb_colsD=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg3)._data,wD,s0);
- }
- if (hS!=hD) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Type of first argument ('%s') "
- "do not match with third argument ('%s'), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,s_type(arg3)._data,s0);
- }
- pos = vector(wS*wD);
- CImg<ulongT>::vector((ulongT)mp_mproj,pos,arg1,wS,hS,arg3,wD,arg5,arg6,p3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"merge(",6)) { // Merge inter-thread variables
- _cimg_mp_op("Function 'merge()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- ref.assign(7);
- pos = compile(ss6,s1,depth1,ref,bloc_flags);
- if (*ref) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: First argument cannot be a linked reference, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s0);
- }
- arg1 = ~0U; // Merge operator
- // (0='=',1='+',2='-',3='*',4='/',5='&',6='|',7='xor',8='&&',9=='||',10='min',11='max')
- if (s1<se1) {
- ++s1;
- char st_op[4] = { 0 };
- is_sth = false; // blank after operator?
- if (cimg_sscanf(s1," %3[=+-*/&|minaxor]%c",st_op,&sep)==2 && (sep==')' ||
- (is_sth=cimg::is_blank(sep)))) {
- if (!is_sth || (is_sth && cimg_sscanf(s1," %*[=+-*/&|minaxor ]%c",&sep)==1 && sep==')')) {
- cimg::strpare(st_op,' ',false,true);
- if (!st_op[1])
- arg1 = *st_op=='='?0:*st_op=='+'?1:*st_op=='-'?2:*st_op=='*'?3:*st_op=='/'?4:
- *st_op=='&'?5:*st_op=='|'?6:~0U;
- else if (*st_op=='x' && st_op[1]=='o' && st_op[2]=='r' && !st_op[3]) arg1 = 7;
- else if (*st_op=='&' && st_op[1]=='&' && !st_op[2]) arg1 = 8;
- else if (*st_op=='|' && st_op[1]=='|' && !st_op[2]) arg1 = 9;
- else if (*st_op=='m' && st_op[1]=='i' && st_op[2]=='n' && !st_op[3]) arg1 = 10;
- else if (*st_op=='m' && st_op[1]=='a' && st_op[2]=='x' && !st_op[3]) arg1 = 11;
- }
- }
- }
- cimg_rofY(memmerge,k) if (memmerge(0,k)==(int)pos) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Merge has already been requested before "
- "for specified variable "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,s0);
- }
- if (arg1==~0U) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid specified operator "
- "(should be one of '=,+,-,*,/,&,|,xor,&&,||,min,max'), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,s0);
- }
- memmerge.resize(3,memmerge._height + 1,1,1,0,0);
- memmerge(0,memmerge._height - 1) = (int)pos;
- memmerge(1,memmerge._height - 1) = (int)_cimg_mp_size(pos);
- memmerge(2,memmerge._height - 1) = (int)arg1;
- _cimg_mp_return(pos);
- }
- break;
- case 'n' :
- #ifdef cimg_mp_func_name
- if (!std::strncmp(ss,"name(",5)) { // Get image name as a string vector
- _cimg_mp_op("Function 'name()'");
- if (*ss5=='#') { // Index specified
- s0 = ss6; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss6,s0++,depth1,0,bloc_flags);
- is_sth = true; // is_index_specified?
- _cimg_mp_check_list();
- } else { s0 = ss5; p1 = get_mem_img_index(); is_sth = false; }
- arg1 = s0<se1?compile(s0,se1,depth1,0,bloc_flags):~0U;
- if (arg1!=~0U) {
- _cimg_mp_check_const_scalar(arg1,is_sth?2:1,3);
- arg1 = (unsigned int)mem[arg1];
- } else arg1 = 1024;
- pos = vector(arg1);
- CImg<ulongT>::vector((ulongT)mp_name,pos,p1,arg1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- #endif
- if (!std::strncmp(ss,"narg(",5)) { // Number of arguments
- _cimg_mp_op("Function 'narg()'");
- if (ss5>=se1) _cimg_mp_return(0);
- arg1 = 0;
- for (s = ss5; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- ++arg1; s = ns;
- }
- _cimg_mp_const_scalar((double)arg1);
- }
- if ((cimg_sscanf(ss,"norm%u%c",&(arg1=~0U),&sep)==2 && sep=='(') ||
- !std::strncmp(ss,"norminf(",8) || !std::strncmp(ss,"norm(",5) ||
- (!std::strncmp(ss,"norm",4) && ss5<se1 && (s=std::strchr(ss5,'('))!=0)) { // Lp norm
- _cimg_mp_op("Function 'normP()'");
- if (*ss4=='(') { arg1 = 2; s = ss5; }
- else if (*ss4=='i' && *ss5=='n' && *ss6=='f' && *ss7=='(') { arg1 = ~0U; s = ss8; }
- else if (arg1==~0U) {
- arg1 = compile(ss4,s++,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg1,0,2);
- arg1 = (unsigned int)mem[arg1];
- } else s = std::strchr(ss4,'(') + 1;
- pos = scalar();
- switch (arg1) {
- case 0 :
- CImg<ulongT>::vector((ulongT)mp_norm0,pos,0).move_to(l_opcode); break;
- case 1 :
- CImg<ulongT>::vector((ulongT)mp_norm1,pos,0).move_to(l_opcode); break;
- case 2 :
- CImg<ulongT>::vector((ulongT)mp_norm2,pos,0).move_to(l_opcode); break;
- case ~0U :
- CImg<ulongT>::vector((ulongT)mp_norminf,pos,0).move_to(l_opcode); break;
- default :
- CImg<ulongT>::vector((ulongT)mp_normp,pos,0,(ulongT)(arg1==~0U?-1:(int)arg1)).
- move_to(l_opcode);
- }
- for ( ; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- if (arg1>0 && opcode._height==4) // Special case with one argument and p>=1
- _cimg_mp_scalar1(mp_abs,opcode[3]);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'p' :
- if (!std::strncmp(ss,"permut(",7)) { // Number of permutations
- _cimg_mp_op("Function 'permut()'");
- s1 = ss7; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg1 = compile(ss7,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = compile(++s2,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3))
- _cimg_mp_const_scalar(cimg::permutations((int)mem[arg1],(int)mem[arg2],(bool)mem[arg3]));
- _cimg_mp_scalar3(mp_permutations,arg1,arg2,arg3);
- }
- if (!std::strncmp(ss,"polygon(",8)) { // Polygon/line drawing
- if (!is_inside_critical) is_parallelizable = false;
- _cimg_mp_op("Function 'polygon()'");
- if (*ss8=='#') { // Index specified
- s0 = ss + 9; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss + 9,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { p1 = ~0U; s0 = ss8; }
- if (s0==se1) compile(s0,se1,depth1,0,bloc_flags); // 'missing' argument error
- CImg<ulongT>::vector((ulongT)mp_polygon,_cimg_mp_slot_nan,0,p1).move_to(l_opcode);
- for (s = s0; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- _cimg_mp_return_nan();
- }
- if (!std::strncmp(ss,"print(",6) ||
- !std::strncmp(ss,"prints(",7)) { // Print expressions
- s0 = ss6 + (*ss5=='('?0:1);
- is_sth = *ss5=='s'; // string must be printed?
- _cimg_mp_op(is_sth?"Function 'prints()'":"Function 'print()'");
- if (!is_sth && *s0=='#') { // Image
- p1 = compile(ss7,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- CImg<ulongT>::vector((ulongT)mp_image_print,_cimg_mp_slot_nan,p1).move_to(code);
- _cimg_mp_return_nan();
- }
- // Regular expression
- for (s = s0; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- pos = compile(s,ns,depth1,p_ref,bloc_flags);
- c1 = *ns; *ns = 0;
- variable_name.assign(CImg<charT>::string(s,true,true).unroll('y'),true);
- cimg::strpare(variable_name,false,true);
- if (_cimg_mp_is_const_scalar(pos)) // Const scalar
- std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %g "
- "(compiled as '%s', memslot = %u)",
- variable_name._data,mem[pos],s_type(pos)._data,pos);
- else // Vector or non-const scalar
- std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = (uninitialized) "
- "(compiled as '%s', memslot = %u)",
- variable_name._data,s_type(pos)._data,pos);
- if (_cimg_mp_is_vector(pos)) // Vector
- ((CImg<ulongT>::vector((ulongT)mp_vector_print,pos,0,(ulongT)_cimg_mp_size(pos),is_sth?1:0),
- variable_name)>'y').move_to(opcode);
- else // Scalar
- ((CImg<ulongT>::vector((ulongT)mp_print,pos,0,is_sth?1:0),
- variable_name)>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- *ns = c1; s = ns;
- }
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"pseudoinvert(",13)) { // Matrix/scalar pseudo-inversion
- _cimg_mp_op("Function 'pseudoinvert()'");
- s1 = ss + 13; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss + 13,s1,depth1,0,bloc_flags);
- arg2 = 1;
- arg3 = 0;
- if (s1<se1) {
- s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_const_scalar(arg2,2,3);
- _cimg_mp_check_type(arg3,3,1,0);
- p1 = _cimg_mp_size(arg1);
- p2 = (unsigned int)mem[arg2];
- p3 = p1/p2;
- if (p3*p2!=p1) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Type of first argument ('%s') "
- "does not match with second argument 'nb_colsA=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,p2,s0);
- }
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_matrix_pseudoinvert,pos,arg1,p2,p3,arg3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'r' :
- if (!std::strncmp(ss,"rad2deg(",8)) { // Degrees to radians
- _cimg_mp_op("Function 'rad2deg()'");
- arg1 = compile(ss8,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_rad2deg,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(mem[arg1]*180/cimg::PI);
- _cimg_mp_scalar1(mp_rad2deg,arg1);
- }
- if (!std::strncmp(ss,"ref(",4)) { // Variable declaration
- _cimg_mp_op("Function 'ref()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- if (s1>=se1 || !*s1) compile(s1,s1,depth1,0,bloc_flags); // Will throw missing argument error
- arg3 = compile(ss4,s1++,depth1,p_ref,bloc_flags);
- *se1 = 0;
- if (!is_varname(s1)) { // Invalid variable name
- variable_name.assign(s1,(unsigned int)(se1 + 1 - s1)).back() = 0;
- cimg::strellipsize(variable_name,64);
- *se1 = ')';
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid specified variable name '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- variable_name._data,s0);
- }
- get_variable_pos(s1,arg1,arg2);
- if (arg2!=~0U) reserved_label[arg2] = arg3;
- else if (arg1!=~0U) variable_pos[arg1] = arg3;
- else { // New variable
- if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
- variable_pos[variable_def._width] = arg3;
- CImg<char>::string(s1).move_to(variable_def);
- }
- if (_cimg_mp_is_vector(arg3))
- set_reserved_vector(arg3); // Prevent from being used in further optimization
- else if (_cimg_mp_is_comp(arg3)) memtype[arg3] = -1;
- *se1 = ')';
- _cimg_mp_return(arg3);
- }
- if (!std::strncmp(ss,"repeat(",7)) { // Repeat
- _cimg_mp_op("Function 'repeat()'");
- s0 = ss7; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg1 = compile(ss7,s0,depth1,0,bloc_flags); // Number of iterations
- _cimg_mp_check_type(arg1,1,1,0);
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p1 = code._width;
- if (s1<se1) { // Version with 3 arguments
- variable_name.assign(s0,(unsigned int)(s1 + 1 - s0)).back() = 0;
- cimg::strpare(variable_name,false,true);
- if (!is_varname(variable_name)) { // Invalid variable name
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid loop variable name '%s', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- variable_name._data,s0);
- }
- get_variable_pos(variable_name,arg2,arg3);
- arg2 = arg3!=~0U?reserved_label[arg3]:arg2!=~0U?variable_pos[arg2]:~0U; // Variable slot
- if (arg2!=~0U && (!_cimg_mp_is_scalar(arg2) ||
- _cimg_mp_is_const_scalar(arg2))) { // Variable is not a vector or is a constant->error
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' "
- "(expected 'scalar'), in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg2)._data,variable_name._data,s0);
- } else if (arg2==~0U) { // Variable does not exist -> create it
- arg2 = scalar();
- if (arg3!=~0U) reserved_label[arg3] = arg2;
- else {
- if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);
- variable_pos[variable_def._width] = arg2;
- variable_name.move_to(variable_def);
- }
- memtype[arg2] = -1;
- }
- arg3 = compile(++s1,se1,depth1,0,bloc_flags);
- } else { // Version with 2 arguments
- arg2 = ~0U;
- arg3 = compile(s0,se1,depth1,0,bloc_flags);
- }
- // arg2 = variable slot, arg3 = fill expression.
- CImg<ulongT>::vector((ulongT)mp_repeat,arg3,arg1,arg2,code._width - p1).move_to(code,p1);
- _cimg_mp_return(arg3);
- }
- if (!std::strncmp(ss,"resize(",7)) { // Vector or image resize
- _cimg_mp_op("Function 'resize()'");
- if (*ss7!='#') { // Vector
- for (s = ss7; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (s!=ss7 && _cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- if (opcode.height()<2) compile(s,se1,depth1,0,bloc_flags); // Not enough arguments -> throw exception
- arg1 = (unsigned int)opcode[0]; // Vector to resize
- p1 = _cimg_mp_size(arg1);
- if (opcode.height()<=4) { // Simple vector resize
- arg2 = (unsigned int)opcode[1];
- _cimg_mp_check_const_scalar(arg2,2,3);
- arg2 = (unsigned int)mem[arg2];
- arg3 = opcode.height()<3?1U:(unsigned int)opcode[2];
- _cimg_mp_check_type(arg3,3,1,0);
- arg4 = opcode.height()<4?0U:(unsigned int)opcode[3];
- _cimg_mp_check_type(arg4,4,1,0);
- pos = vector(arg2);
- CImg<ulongT>::vector((ulongT)mp_vector_resize,pos,arg2,arg1,p1,arg3,arg4).move_to(code);
- } else { // Advanced vector resize (vector viewed as an image)
- // opcode = [ A, ow,oh,od,os, nw,nh,nd,ns, interp, boundary_cond, ax,ay,az,ac ]
- // [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ]
- if (opcode.height()<6) compile(s,se1,depth1,0,bloc_flags); // Not enough arguments -> throw exception
- p2 = opcode.height();
- opcode.resize(1,15,1,1,0);
- if (p2<7) opcode[6] = opcode[2];
- if (p2<8) opcode[7] = opcode[3];
- if (p2<9) opcode[8] = opcode[4];
- if (p2<10) opcode[9] = 1;
- _cimg_mp_check_const_scalar(opcode[1],2,3);
- _cimg_mp_check_const_scalar(opcode[2],3,3);
- _cimg_mp_check_const_scalar(opcode[3],4,3);
- _cimg_mp_check_const_scalar(opcode[4],5,3);
- _cimg_mp_check_const_scalar(opcode[5],6,3);
- _cimg_mp_check_const_scalar(opcode[6],7,3);
- _cimg_mp_check_const_scalar(opcode[7],8,3);
- _cimg_mp_check_const_scalar(opcode[8],9,3);
- arg2 = (unsigned int)mem[opcode[1]]; opcode[1] = arg2;
- arg3 = (unsigned int)mem[opcode[2]]; opcode[2] = arg3;
- arg4 = (unsigned int)mem[opcode[3]]; opcode[3] = arg4;
- arg5 = (unsigned int)mem[opcode[4]]; opcode[4] = arg5;
- if (arg2*arg3*arg4*arg5!=std::max(1U,p1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Input size (%lu values) and specified input vector "
- "geometry (%u,%u,%u,%u) (%lu values) do not match.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- std::max(p1,1U),arg2,arg3,arg4,arg5,(ulongT)arg2*arg3*arg4*arg5);
- arg2 = (unsigned int)mem[opcode[5]]; opcode[5] = arg2;
- arg3 = (unsigned int)mem[opcode[6]]; opcode[6] = arg3;
- arg4 = (unsigned int)mem[opcode[7]]; opcode[7] = arg4;
- arg5 = (unsigned int)mem[opcode[8]]; opcode[8] = arg5;
- pos = vector(arg2*arg3*arg4*arg5);
- opcode.resize(1,18,1,1,0,0,0,1);
- opcode[0] = (ulongT)mp_vector_resize_ext;
- opcode[1] = (ulongT)pos;
- opcode[2] = (ulongT)p1;
- opcode.move_to(code);
- }
- return_new_comp = true;
- _cimg_mp_return(pos);
- } else { // Image
- if (!is_inside_critical) is_parallelizable = false;
- s0 = ss8; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- p1 = compile(ss8,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- l_opcode.assign(); // Don't use 'opcode': it can be modified by further calls to 'compile()'!
- CImg<ulongT>::vector((ulongT)mp_image_resize,_cimg_mp_slot_nan,p1,~0U,~0U,~0U,~0U,1,0,0,0,0,0).
- move_to(l_opcode);
- pos = 0;
- for (s = s0; s<se && pos<10; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg1 = compile(s,ns,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,pos + 2,1,0);
- l_opcode(0,pos + 3) = arg1;
- s = ns;
- ++pos;
- }
- if (pos<1 || pos>10) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: %s arguments, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- pos<1?"Missing":"Too much",s0);
- }
- l_opcode[0].move_to(code);
- _cimg_mp_return_nan();
- }
- }
- if (!std::strncmp(ss,"reverse(",8)) { // Vector reverse
- _cimg_mp_op("Function 'reverse()'");
- arg1 = compile(ss8,se1,depth1,0,bloc_flags);
- if (!_cimg_mp_is_vector(arg1)) _cimg_mp_return(arg1);
- p1 = _cimg_mp_size(arg1);
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_vector_reverse,pos,arg1,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"rol(",4) || !std::strncmp(ss,"ror(",4)) { // Bitwise rotation
- _cimg_mp_op(ss[2]=='l'?"Function 'rol()'":"Function 'ror()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1-expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(arg2,2,1,0);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector2_vs(*ss2=='l'?mp_rol:mp_ror,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar(*ss2=='l'?cimg::rol(mem[arg1],(unsigned int)mem[arg2]):
- cimg::ror(mem[arg1],(unsigned int)mem[arg2]));
- _cimg_mp_scalar2(*ss2=='l'?mp_rol:mp_ror,arg1,arg2);
- }
- if (!std::strncmp(ss,"rot(",4)) { // 2D/3D rotation matrix
- _cimg_mp_op("Function 'rot()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- if (s1<se1) { // 3D rotation
- _cimg_mp_check_type(arg1,1,3,3);
- is_sth = false; // Is coordinates as vector?
- if (_cimg_mp_is_vector(arg1)) { // Coordinates specified as a vector
- is_sth = true;
- p2 = _cimg_mp_size(arg1);
- ++arg1;
- arg2 = arg3 = 0;
- if (p2>1) {
- arg2 = arg1 + 1;
- if (p2>2) arg3 = arg2 + 1;
- }
- arg4 = compile(++s1,se1,depth1,0,bloc_flags);
- } else {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(++s2,s3,depth1,0,bloc_flags);
- arg4 = compile(++s3,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- }
- _cimg_mp_check_type(arg4,is_sth?2:4,1,0);
- pos = vector(9);
- CImg<ulongT>::vector((ulongT)mp_rot3d,pos,arg1,arg2,arg3,arg4).move_to(code);
- } else { // 2D rotation
- _cimg_mp_check_type(arg1,1,1,0);
- pos = vector(4);
- CImg<ulongT>::vector((ulongT)mp_rot2d,pos,arg1).move_to(code);
- }
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"round(",6)) { // Value rounding
- _cimg_mp_op("Function 'round()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- arg2 = 1;
- arg3 = 0;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector3_vss(mp_round,arg1,arg2,arg3);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2) && _cimg_mp_is_const_scalar(arg3))
- _cimg_mp_const_scalar(cimg::round(mem[arg1],mem[arg2],(int)mem[arg3]));
- _cimg_mp_scalar3(mp_round,arg1,arg2,arg3);
- }
- #ifdef cimg_mp_func_run
- if (!std::strncmp(ss,"run(",4)) { // Run external command
- _cimg_mp_op("Function 'run()'");
- if (!is_inside_critical) is_parallelizable = false;
- CImg<ulongT>::vector((ulongT)mp_run,0,0).move_to(l_opcode);
- pos = 1;
- for (s = ss4; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg1 = compile(s,ns,depth1,0,bloc_flags);
- CImg<ulongT>::vector(arg1,_cimg_mp_size(arg1)).move_to(l_opcode);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- pos = scalar();
- opcode[1] = pos;
- opcode[2] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- #endif
- break;
- case 's' :
- if (*ss1=='(') { // Image spectrum
- _cimg_mp_op("Function 's()'");
- if (*ss2=='#') { // Index specified
- p1 = compile(ss3,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss2!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_s,p1);
- }
- if (!std::strncmp(ss,"same(",5)) { // Test if operands have the same values
- _cimg_mp_op("Function 'same()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = 11;
- arg4 = 1;
- if (s2<se1) {
- s3 = s2 + 1; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
- arg3 = compile(++s2,s3,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,3,1,0);
- arg4 = s3<se1?compile(++s3,se1,depth1,0,bloc_flags):1;
- }
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- _cimg_mp_scalar6(mp_vector_eq,arg1,p1,arg2,p2,arg3,arg4);
- }
- #ifdef cimg_mp_func_set
- if (!std::strncmp(ss,"set(",4)) { // Set value/vector to external variable
- _cimg_mp_op("Function 'set()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,2,0);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- CImg<ulongT>::vector((ulongT)mp_set,arg1,p1,arg2,p2).move_to(code);
- _cimg_mp_return(arg1);
- }
- #endif
- if (!std::strncmp(ss,"shift(",6)) { // Shift vector
- _cimg_mp_op("Function 'shift()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- arg2 = 1; arg3 = 0;
- if (s1<se1) {
- s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg2 = compile(s1,s0,depth1,0,bloc_flags);
- arg3 = s0<se1?compile(++s0,se1,depth1,0,bloc_flags):0;
- }
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- p1 = _cimg_mp_size(arg1);
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_shift,pos,arg1,p1,arg2,arg3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"sign(",5)) { // Sign
- _cimg_mp_op("Function 'sign()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sign,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sign(mem[arg1]));
- _cimg_mp_scalar1(mp_sign,arg1);
- }
- if (!std::strncmp(ss,"sin(",4)) { // Sine
- _cimg_mp_op("Function 'sin()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sin,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sin(mem[arg1]));
- _cimg_mp_scalar1(mp_sin,arg1);
- }
- if (!std::strncmp(ss,"sinc(",5)) { // Sine cardinal
- _cimg_mp_op("Function 'sinc()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sinc,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sinc(mem[arg1]));
- _cimg_mp_scalar1(mp_sinc,arg1);
- }
- if (!std::strncmp(ss,"sinh(",5)) { // Hyperbolic sine
- _cimg_mp_op("Function 'sinh()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sinh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sinh(mem[arg1]));
- _cimg_mp_scalar1(mp_sinh,arg1);
- }
- if (!std::strncmp(ss,"size(",5)) { // Vector size
- _cimg_mp_op("Function 'size()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_const_scalar(_cimg_mp_is_scalar(arg1)?0:_cimg_mp_size(arg1));
- }
- if (!std::strncmp(ss,"solve(",6)) { // Solve square linear system
- _cimg_mp_op("Function 'solve()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_type(arg2,2,2,0);
- _cimg_mp_check_const_scalar(arg3,3,3);
- p1 = _cimg_mp_size(arg1);
- p2 = _cimg_mp_size(arg2);
- p3 = (unsigned int)mem[arg3];
- arg5 = p2/p3;
- arg4 = p1/arg5;
- if (arg4*arg5!=p1 || arg5*p3!=p2) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Types of first and second arguments ('%s' and '%s') "
- "do not match with third argument 'nb_colsB=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,s_type(arg2)._data,p3,s0);
- }
- pos = vector(arg4*p3);
- CImg<ulongT>::vector((ulongT)mp_solve,pos,arg1,arg2,arg4,arg5,p3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"sort(",5)) { // Sort vector
- _cimg_mp_op("Function 'sort()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- arg2 = arg4 = 1; arg3 = ~0U;
- if (s1<se1) {
- s0 = ++s1; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg2 = compile(s1,s0,depth1,0,bloc_flags);
- if (s0<se1) {
- s1 = ++s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(s0,s1,depth1,0,bloc_flags);
- arg4 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
- }
- }
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_type(arg2,2,1,0);
- if (arg3!=~0U) _cimg_mp_check_type(arg3,3,1,0);
- _cimg_mp_check_type(arg4,4,1,0);
- p1 = _cimg_mp_size(arg1);
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_sort,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"sqr(",4)) { // Square
- _cimg_mp_op("Function 'sqr()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sqr,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::sqr(mem[arg1]));
- _cimg_mp_scalar1(mp_sqr,arg1);
- }
- if (!std::strncmp(ss,"sqrt(",5)) { // Square root
- _cimg_mp_op("Function 'sqrt()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_sqrt,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::sqrt(mem[arg1]));
- _cimg_mp_scalar1(mp_sqrt,arg1);
- }
- if (!std::strncmp(ss,"srand(",6)) { // Set RNG seed
- _cimg_mp_op("Function 'srand()'");
- arg1 = ss6<se1?compile(ss6,se1,depth1,0,bloc_flags):~0U;
- if (arg1!=~0U) { _cimg_mp_check_type(arg1,1,1,0); _cimg_mp_scalar1(mp_srand,arg1); }
- _cimg_mp_scalar0(mp_srand0);
- }
- if (!std::strncmp(ss,"stats(",6)) { // Image statistics
- _cimg_mp_op("Function 'stats()'");
- if (*ss6=='#') { // Index specified
- p1 = compile(ss7,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss6!=se1) break; p1 = ~0U; }
- pos = vector(14);
- CImg<ulongT>::vector((ulongT)mp_image_stats,pos,p1).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- #ifdef cimg_mp_func_store
- if (!std::strncmp(ss,"store(",6)) { // Store vector to variable
- _cimg_mp_op("Function 'store()'");
- s1 = ss6; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss6,s1,depth1,0,bloc_flags);
- p1 = _cimg_mp_size(arg1);
- p3 = std::max(1U,p1);
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,2,0);
- p2 = _cimg_mp_size(arg2);
- arg3 = ~0U; arg4 = arg5 = arg6 = 1U; pos = 0;
- if (s2<se1) {
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg3 = compile(++s2,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg3,3,1,0);
- arg4 = arg5 = arg6 = 1U;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg4 = compile(++s1,s2,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg4,4,1,0);
- if (s2<se1) {
- s1 = s2 + 1; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg5 = compile(++s2,s1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg5,5,1,0);
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg6 = compile(++s1,s2,depth1,0,bloc_flags);
- pos = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- _cimg_mp_check_type(arg6,6,1,0);
- _cimg_mp_check_type(pos,7,1,0);
- }
- }
- }
- }
- if (arg3==~0U) arg3 = const_scalar(p3);
- CImg<ulongT>::vector((ulongT)mp_store,_cimg_mp_slot_nan,arg1,p1,arg2,p2,
- arg3,arg4,arg5,arg6,pos).move_to(code);
- _cimg_mp_return_nan();
- }
- #endif
- if (!std::strncmp(ss,"stov(",5)) { // String to double
- _cimg_mp_op("Function 'stov()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- arg2 = arg3 = 0;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):0;
- }
- _cimg_mp_check_type(arg2,2,1,0);
- _cimg_mp_check_type(arg3,3,1,0);
- p1 = _cimg_mp_size(arg1);
- pos = scalar();
- CImg<ulongT>::vector((ulongT)mp_stov,pos,arg1,p1,arg2,arg3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"string(",7)) { // Construct string from list of arguments
- _cimg_mp_op("Function 'string()'");
- CImg<ulongT>::vector((ulongT)mp_string,0,0,0).move_to(l_opcode);
- if (*ss7=='#') { // Output vector size specified, with '#'
- s0 = ss8; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg1 = compile(ss8,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg1,1,3);
- arg1 = (unsigned int)mem[arg1];
- s = s0;
- } else { arg1=~0U; s = ss7; }
- p1 = 0;
- for (; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- p2 = _cimg_mp_size(arg2);
- if (p2) p1+=p2;
- else {
- if (_cimg_mp_is_const_scalar(arg2)) p1+=cimg_snprintf(variable_name.assign(24),24,"%.17g",mem[arg2]);
- else p1+=23;
- }
- CImg<ulongT>::vector(arg2,p2).move_to(l_opcode);
- s = ns;
- }
- if (arg1==~0U) arg1 = p1;
- pos = vector(arg1,0);
- (l_opcode>'y').move_to(opcode);
- opcode[1] = pos;
- opcode[2] = arg1;
- opcode[3] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"svd(",4)) { // Matrix SVD
- _cimg_mp_op("Function 'svd()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = s1<se1?compile(++s1,se1,depth1,0,bloc_flags):1;
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_const_scalar(arg2,2,3);
- p1 = _cimg_mp_size(arg1);
- p2 = (unsigned int)mem[arg2];
- p3 = p1/p2;
- if (p3*p2!=p1) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Type of first argument ('%s') "
- "does not match with second argument 'nb_colsA=%u', "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,p2,s0);
- }
- pos = vector(p1 + p2 + p2*p2);
- CImg<ulongT>::vector((ulongT)mp_matrix_svd,pos,arg1,p2,p3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"swap(",5)) { // Swap values
- _cimg_mp_op("Function 'swap()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- ref.assign(14);
- arg1 = compile(ss5,s1,depth1,ref,bloc_flags);
- arg2 = compile(++s1,se1,depth1,ref._data + 7,bloc_flags);
- p1 = _cimg_mp_size(arg1);
- _cimg_mp_check_type(arg2,2,p1?2:1,p1);
- if (_cimg_mp_is_const_scalar(arg1) || _cimg_mp_is_const_scalar(arg2)) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: %s argument cannot be a constant, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- _cimg_mp_is_const_scalar(arg1)?"First":"Second",s0);
- }
- CImg<ulongT>::vector((ulongT)mp_swap,arg1,arg2,p1).move_to(code);
- // Write back values of linked arg1 and arg2.
- const unsigned int *_ref = ref;
- is_sth = true; // Is first argument?
- do {
- switch (*_ref) {
- case 1 : // arg1: V[k]
- arg3 = _ref[1]; // Vector slot
- arg4 = _ref[2]; // Index
- CImg<ulongT>::vector((ulongT)mp_vector_set_off,arg1,arg3,(ulongT)_cimg_mp_size(arg3),arg4).
- move_to(code);
- break;
- case 2 : // arg1: i/j[_#ind,off]
- if (!is_inside_critical) is_parallelizable = false;
- p1 = _ref[1]; // Index
- is_relative = (bool)_ref[2];
- arg3 = _ref[3]; // Offset
- if (p1!=~0U) {
- if (imglist)
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_joff:mp_list_set_ioff),
- arg1,p1,arg3).move_to(code);
- } else {
- if (imgout)
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_joff:mp_set_ioff),
- arg1,arg3).move_to(code);
- }
- break;
- case 3 : // arg1: i/j(_#ind,_x,_y,_z,_c)
- if (!is_inside_critical) is_parallelizable = false;
- p1 = _ref[1]; // Index
- is_relative = (bool)_ref[2];
- arg3 = _ref[3]; // X
- arg4 = _ref[4]; // Y
- arg5 = _ref[5]; // Z
- arg6 = _ref[6]; // C
- if (p1!=~0U) {
- if (imglist)
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_jxyzc:mp_list_set_ixyzc),
- arg1,p1,arg3,arg4,arg5,arg6).move_to(code);
- } else {
- if (imgout)
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_jxyzc:mp_set_ixyzc),
- arg1,arg3,arg4,arg5,arg6).move_to(code);
- }
- break;
- case 4: // arg1: I/J[_#ind,off]
- if (!is_inside_critical) is_parallelizable = false;
- p1 = _ref[1]; // Index
- is_relative = (bool)_ref[2];
- arg3 = _ref[3]; // Offset
- if (p1!=~0U) {
- if (imglist) {
- if (_cimg_mp_is_scalar(arg1))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_s:mp_list_set_Ioff_s),
- arg1,p1,arg3).move_to(code);
- else {
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Joff_v:mp_list_set_Ioff_v),
- arg1,p1,arg3,_cimg_mp_size(arg1)).move_to(code);
- }
- }
- } else {
- if (imgout) {
- if (_cimg_mp_is_scalar(arg1))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_s:mp_set_Ioff_s),
- arg1,arg3).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Joff_v:mp_set_Ioff_v),
- arg1,arg3,_cimg_mp_size(arg1)).move_to(code);
- }
- }
- break;
- case 5 : // arg1: I/J(_#ind,_x,_y,_z,_c)
- if (!is_inside_critical) is_parallelizable = false;
- p1 = _ref[1]; // Index
- is_relative = (bool)_ref[2];
- arg3 = _ref[3]; // X
- arg4 = _ref[4]; // Y
- arg5 = _ref[5]; // Z
- if (p1!=~0U) {
- if (imglist) {
- if (_cimg_mp_is_scalar(arg1))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_s:mp_list_set_Ixyz_s),
- arg1,p1,arg3,arg4,arg5).move_to(code);
- else {
- _cimg_mp_check_const_index(p1);
- CImg<ulongT>::vector((ulongT)(is_relative?mp_list_set_Jxyz_v:mp_list_set_Ixyz_v),
- arg1,p1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- }
- }
- } else {
- if (imgout) {
- if (_cimg_mp_is_scalar(arg1))
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_s:mp_set_Ixyz_s),
- arg1,arg3,arg4,arg5).move_to(code);
- else
- CImg<ulongT>::vector((ulongT)(is_relative?mp_set_Jxyz_v:mp_set_Ixyz_v),
- arg1,arg3,arg4,arg5,_cimg_mp_size(arg1)).move_to(code);
- }
- }
- break;
- }
- _ref+=7;
- arg1 = arg2;
- is_sth = !is_sth;
- } while (!is_sth);
- if (p_ref) std::memcpy(p_ref,ref,siz_ref);
- _cimg_mp_return(arg1);
- }
- break;
- case 't' :
- if (!std::strncmp(ss,"tan(",4)) { // Tangent
- _cimg_mp_op("Function 'tan()'");
- arg1 = compile(ss4,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_tan,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::tan(mem[arg1]));
- _cimg_mp_scalar1(mp_tan,arg1);
- }
- if (!std::strncmp(ss,"tanh(",5)) { // Hyperbolic tangent
- _cimg_mp_op("Function 'tanh()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_tanh,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(std::tanh(mem[arg1]));
- _cimg_mp_scalar1(mp_tanh,arg1);
- }
- if (!std::strncmp(ss,"trace(",6)) { // Matrix trace
- _cimg_mp_op("Function 'trace()'");
- arg1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_matrix_square(arg1,1);
- p1 = (unsigned int)cimg::round(std::sqrt((float)_cimg_mp_size(arg1)));
- _cimg_mp_scalar2(mp_trace,arg1,p1);
- }
- if (!std::strncmp(ss,"transpose(",10)) { // Matrix transpose
- _cimg_mp_op("Function 'transpose()'");
- s1 = ss + 10; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss + 10,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,2,0);
- _cimg_mp_check_const_scalar(arg2,2,3);
- p1 = _cimg_mp_size(arg1);
- p2 = (unsigned int)mem[arg2];
- p3 = p1/p2;
- if (p2*p3!=p1) {
- _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Size of first argument ('%s') does not match "
- "second argument 'nb_cols=%u', in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- s_type(arg1)._data,p2,s0);
- }
- pos = vector(p3*p2);
- CImg<ulongT>::vector((ulongT)mp_transpose,pos,arg1,p2,p3).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'u' :
- if (*ss1=='(') { // Random value with uniform distribution
- _cimg_mp_op("Function 'u()'");
- if (*ss2==')') _cimg_mp_scalar2(mp_u,0,1);
- s1 = ss2; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss2,s1,depth1,0,bloc_flags);
- if (s1<se1) arg2 = compile(++s1,se1,depth1,0,bloc_flags); else { arg2 = arg1; arg1 = 0; }
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_u,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_u,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_u,arg1,arg2);
- _cimg_mp_scalar2(mp_u,arg1,arg2);
- }
- if (!std::strncmp(ss,"ui2f(",5)) { // Special uint->float conversion
- _cimg_mp_op("Function 'ui2f()'");
- arg1 = compile(ss5,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_ui2f,arg1);
- if (_cimg_mp_is_const_scalar(arg1))
- _cimg_mp_const_scalar((double)cimg::uint2float((unsigned int)mem[arg1]));
- _cimg_mp_scalar1(mp_ui2f,arg1);
- }
- if (!std::strncmp(ss,"unref(",6)) { // Un-reference variable
- _cimg_mp_op("Function 'unref()'");
- arg1=~0U;
- for (s0 = ss6; s0<se1; s0 = s1) {
- if (s0>ss6 && *s0==',') ++s0;
- s1 = s0; while (s1<se1 && *s1!=',') ++s1;
- c1 = *s1;
- if (s1>s0) {
- *s1 = 0;
- get_variable_pos(s0,arg1,arg2);
- if (arg2!=~0U) reserved_label[arg2] = ~0U;
- else if (arg1!=~0U) {
- variable_def.remove(arg1);
- if (arg1<variable_pos._width - 1)
- std::memmove(variable_pos._data + arg1,variable_pos._data + arg1 + 1,
- sizeof(uintT)*(variable_pos._width - arg1 - 1));
- --variable_pos._width;
- }
- *s1 = c1;
- } else compile(s0,s1,depth1,0,bloc_flags); // Will throw a 'missing argument' exception
- }
- _cimg_mp_return(arg1!=~0U?arg1:_cimg_mp_slot_nan); // Return value of last specified variable
- }
- if (!std::strncmp(ss,"uppercase(",10)) { // Upper case
- _cimg_mp_op("Function 'uppercase()'");
- arg1 = compile(ss + 10,se1,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg1)) _cimg_mp_vector1_v(mp_uppercase,arg1);
- if (_cimg_mp_is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::uppercase(mem[arg1]));
- _cimg_mp_scalar1(mp_uppercase,arg1);
- }
- break;
- case 'v' :
- if ((cimg_sscanf(ss,"vector%u%c",&(arg1=~0U),&sep)==2 && sep=='(' && arg1>0) ||
- !std::strncmp(ss,"vector(",7) ||
- (!std::strncmp(ss,"vector",6) && ss7<se1 && (s=std::strchr(ss7,'('))!=0)) { // Vector
- _cimg_mp_op("Function 'vector()'");
- arg2 = 0; // Number of specified values
- if (arg1==~0U && *ss6!='(') {
- arg1 = compile(ss6,s++,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg1,0,3);
- arg1 = (unsigned int)mem[arg1];
- } else s = std::strchr(ss6,'(') + 1;
- if (arg1==~0U && *s=='#') { // Number of elements specified as first argument with '#'
- s0 = ++s; while (s0<se1 && (*s0!=',' || level[s0 - expr._data]!=clevel1)) ++s0;
- arg1 = compile(s,s0++,depth1,0,bloc_flags);
- _cimg_mp_check_const_scalar(arg1,1,3);
- arg1 = (unsigned int)mem[arg1];
- s = s0;
- }
- if (s<se1 || arg1==~0U) for ( ; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg3 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg3)) {
- arg4 = _cimg_mp_size(arg3);
- CImg<ulongT>::sequence(arg4,arg3 + 1,arg3 + arg4).move_to(l_opcode);
- arg2+=arg4;
- } else { CImg<ulongT>::vector(arg3).move_to(l_opcode); ++arg2; }
- s = ns;
- }
- if (arg1==~0U) arg1 = arg2;
- if (!arg1) _cimg_mp_return(0);
- pos = vector(arg1);
- l_opcode.insert(CImg<ulongT>::vector((ulongT)mp_vector_init,pos,0,arg1),0);
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"vmax(",5) || !std::strncmp(ss,"vmin(",5) ||
- !std::strncmp(ss,"vmaxabs(",8) || !std::strncmp(ss,"vminabs(",8) ||
- !std::strncmp(ss,"vmed(",5) || !std::strncmp(ss,"vkth(",5) ||
- !std::strncmp(ss,"vsum(",5) || !std::strncmp(ss,"vavg(",5) ||
- !std::strncmp(ss,"vstd(",5) || !std::strncmp(ss,"vvar(",5) ||
- !std::strncmp(ss,"vprod(",6) ||
- !std::strncmp(ss,"vargmin(",8) || !std::strncmp(ss,"vargmax(",8) ||
- !std::strncmp(ss,"vargminabs(",11) || !std::strncmp(ss,"vargmaxabs(",11) ||
- !std::strncmp(ss,"vargkth(",8)) { // Multi-argument vector functions
- _cimg_mp_op(ss[1]=='a'?(ss[2]=='v'?"Function 'vavg()'":
- ss[4]=='k'?"Function 'vargkth()'":
- ss[5]=='i' && ss[7]=='('?"Function 'vargmin()'":
- ss[5]=='i'?"Function vargminabs()'":
- ss[7]=='('?"Function 'vargmax()'":
- "Function 'vargmaxabs()'"):
- ss[1]=='s'?(ss[2]=='u'?"Function 'vsum()'":"Function 'vstd()'"):
- ss[1]=='k'?"Function 'vkth()'":
- ss[1]=='p'?"Function 'vprod()'":
- ss[1]=='v'?"Function 'vvar()'":
- ss[2]=='i'?(ss[4]=='('?"Function 'vmin()'":
- "Function 'vminabs()'"):
- ss[2]=='a'?(ss[4]=='('?"Function 'vmax()'":
- "Function 'vmaxabs()'"):
- "Function 'vmed()'");
- op = ss[1]=='a'?(ss[2]=='v'?mp_vavg:
- ss[4]=='k'?mp_vargkth:
- ss[5]=='i' && ss[7]=='('?mp_vargmin:
- ss[5]=='i'?mp_vargminabs:
- ss[7]=='('?mp_vargmax:mp_vargmaxabs):
- ss[1]=='s'?(ss[2]=='u'?mp_vsum:mp_vstd):
- ss[1]=='k'?mp_vkth:
- ss[1]=='p'?mp_vprod:
- ss[1]=='v'?mp_vvar:
- ss[2]=='i'?(ss[4]=='('?mp_vmin:mp_vminabs):
- ss[2]=='a'?(ss[4]=='('?mp_vmax:mp_vmaxabs):
- mp_vmedian;
- CImg<ulongT>::vector((ulongT)op,0,0,0).move_to(l_opcode);
- p1 = ~0U;
- p3 = 1;
- for (s = std::strchr(ss,'(') + 1; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- p2 = _cimg_mp_size(arg2);
- if (p1==~0U) { if (_cimg_mp_is_vector(arg2)) p1 = p2; }
- else _cimg_mp_check_type(arg2,p3,3,p1);
- CImg<ulongT>::vector(arg2,p2).move_to(l_opcode);
- s = ns;
- ++p3;
- }
- (l_opcode>'y').move_to(opcode);
- if (p1==~0U) { pos = scalar(); p1 = 0; } else pos = vector(p1);
- opcode[1] = pos;
- opcode[2] = p1;
- opcode[3] = opcode._height;
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- if (!std::strncmp(ss,"vtos(",5)) { // Double(s) to string
- _cimg_mp_op("Function 'vtos()'");
- s1 = ss5; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss5,s1,depth1,0,bloc_flags);
- arg2 = 0; arg3 = ~0U;
- if (s1<se1) {
- s2 = s1 + 1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
- arg2 = compile(++s1,s2,depth1,0,bloc_flags);
- arg3 = s2<se1?compile(++s2,se1,depth1,0,bloc_flags):~0U;
- }
- _cimg_mp_check_type(arg2,2,1,0);
- if (arg3==~0U) { // Auto-guess best output vector size
- p1 = _cimg_mp_size(arg1);
- p1 = p1?25*p1 - 1:24;
- } else {
- _cimg_mp_check_const_scalar(arg3,3,3);
- p1 = (unsigned int)mem[arg3];
- }
- pos = vector(p1);
- CImg<ulongT>::vector((ulongT)mp_vtos,pos,p1,arg1,_cimg_mp_size(arg1),arg2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- break;
- case 'w' :
- if (*ss1=='(') { // Image width
- _cimg_mp_op("Function 'w()'");
- if (*ss2=='#') { // Index specified
- p1 = compile(ss3,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss2!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_w,p1);
- }
- if (*ss1=='h' && *ss2=='(') { // Image width*height
- _cimg_mp_op("Function 'wh()'");
- if (*ss3=='#') { // Index specified
- p1 = compile(ss4,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss3!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_wh,p1);
- }
- if (*ss1=='h' && *ss2=='d' && *ss3=='(') { // Image width*height*depth
- _cimg_mp_op("Function 'whd()'");
- if (*ss4=='#') { // Index specified
- p1 = compile(ss5,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss4!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_whd,p1);
- }
- if (*ss1=='h' && *ss2=='d' && *ss3=='s' && *ss4=='(') { // Image width*height*depth*spectrum
- _cimg_mp_op("Function 'whds()'");
- if (*ss5=='#') { // Index specified
- p1 = compile(ss6,se1,depth1,0,bloc_flags);
- _cimg_mp_check_list();
- } else { if (ss5!=se1) break; p1 = ~0U; }
- _cimg_mp_scalar1(mp_image_whds,p1);
- }
- if (!std::strncmp(ss,"while(",6)) { // While...do
- _cimg_mp_op("Function 'while()'");
- s0 = *ss5=='('?ss6:ss8;
- s1 = s0; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- p1 = code._width;
- arg1 = compile(s0,s1,depth1,0,bloc_flags);
- p2 = code._width;
- arg6 = mempos;
- pos = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg1,1,1,0);
- arg2 = _cimg_mp_size(pos);
- CImg<ulongT>::vector((ulongT)mp_while,pos,arg1,p2 - p1,code._width - p2,arg2,
- pos>=arg6 && !_cimg_mp_is_const_scalar(pos),
- arg1>=arg6 && !_cimg_mp_is_const_scalar(arg1)).move_to(code,p1);
- _cimg_mp_return(pos);
- }
- break;
- case 'x' :
- if (!std::strncmp(ss,"xor(",4)) { // Xor
- _cimg_mp_op("Function 'xor()'");
- s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
- arg1 = compile(ss4,s1,depth1,0,bloc_flags);
- arg2 = compile(++s1,se1,depth1,0,bloc_flags);
- _cimg_mp_check_type(arg2,2,3,_cimg_mp_size(arg1));
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_vv(mp_bitwise_xor,arg1,arg2);
- if (_cimg_mp_is_vector(arg1) && _cimg_mp_is_scalar(arg2)) _cimg_mp_vector2_vs(mp_bitwise_xor,arg1,arg2);
- if (_cimg_mp_is_scalar(arg1) && _cimg_mp_is_vector(arg2)) _cimg_mp_vector2_sv(mp_bitwise_xor,arg1,arg2);
- if (_cimg_mp_is_const_scalar(arg1) && _cimg_mp_is_const_scalar(arg2))
- _cimg_mp_const_scalar((longT)mem[arg1] ^ (longT)mem[arg2]);
- _cimg_mp_scalar2(mp_bitwise_xor,arg1,arg2);
- }
- break;
- }
- if (!std::strncmp(ss,"max(",4) || !std::strncmp(ss,"min(",4) ||
- !std::strncmp(ss,"maxabs(",7) || !std::strncmp(ss,"minabs(",7) ||
- !std::strncmp(ss,"med(",4) || !std::strncmp(ss,"kth(",4) ||
- !std::strncmp(ss,"sum(",4) || !std::strncmp(ss,"avg(",4) ||
- !std::strncmp(ss,"std(",4) || !std::strncmp(ss,"var(",4) ||
- !std::strncmp(ss,"prod(",5) ||
- !std::strncmp(ss,"argmin(",7) || !std::strncmp(ss,"argmax(",7) ||
- !std::strncmp(ss,"argminabs(",10) || !std::strncmp(ss,"argmaxabs(",10) ||
- !std::strncmp(ss,"argkth(",7)) { // Multi-argument functions
- _cimg_mp_op(*ss=='a'?(ss[1]=='v'?"Function 'avg()'":
- ss[3]=='k'?"Function 'argkth()'":
- ss[4]=='i' && ss[6]=='('?"Function 'argmin()'":
- ss[4]=='i'?"Function argminabs()'":
- ss[6]=='('?"Function 'argmax()'":
- "Function 'argmaxabs()'"):
- *ss=='s'?(ss[1]=='u'?"Function 'sum()'":"Function 'std()'"):
- *ss=='k'?"Function 'kth()'":
- *ss=='p'?"Function 'prod()'":
- *ss=='v'?"Function 'var()'":
- ss[1]=='i'?(ss[3]=='('?"Function 'min()'":
- "Function 'minabs()'"):
- ss[1]=='a'?(ss[3]=='('?"Function 'max()'":
- "Function 'maxabs()'"):
- "Function 'med()'");
- op = *ss=='a'?(ss[1]=='v'?mp_avg:
- ss[3]=='k'?mp_argkth:
- ss[4]=='i' && ss[6]=='('?mp_argmin:
- ss[4]=='i'?mp_argminabs:
- ss[6]=='('?mp_argmax:mp_argmaxabs):
- *ss=='s'?(ss[1]=='u'?mp_sum:mp_std):
- *ss=='k'?mp_kth:
- *ss=='p'?mp_prod:
- *ss=='v'?mp_var:
- ss[1]=='i'?(ss[3]=='('?mp_min:mp_minabs):
- ss[1]=='a'?(ss[3]=='('?mp_max:mp_maxabs):
- mp_median;
- is_sth = true; // Tell if all arguments are constant
- pos = scalar();
- CImg<ulongT>::vector((ulongT)op,pos,0).move_to(l_opcode);
- for (s = std::strchr(ss,'(') + 1; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2))
- CImg<ulongT>::sequence(_cimg_mp_size(arg2),arg2 + 1,
- arg2 + (ulongT)_cimg_mp_size(arg2)).
- move_to(l_opcode);
- else CImg<ulongT>::vector(arg2).move_to(l_opcode);
- is_sth&=_cimg_mp_is_const_scalar(arg2);
- s = ns;
- }
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- if (is_sth) _cimg_mp_const_scalar(op(*this));
- opcode.move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- // No corresponding built-in function -> Look for a user-defined macro call.
- s0 = strchr(ss,'(');
- if (s0) {
- variable_name.assign(ss,(unsigned int)(s0 - ss + 1)).back() = 0;
- // Count number of specified arguments.
- p1 = 0;
- for (s = s0 + 1; s<=se1; ++p1, s = ns + 1) {
- while (*s && cimg::is_blank(*s)) ++s;
- if (*s==')' && !p1) break;
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- }
- arg3 = 0; // Number of possible name matches
- cimglist_for(macro_def,l) if (!std::strcmp(macro_def[l],variable_name) && ++arg3 &&
- macro_def[l].back()==(char)p1) {
- p2 = (unsigned int)macro_def[l].back(); // Number of required arguments
- CImg<charT> _expr = macro_body[l]; // Expression to be substituted
- p1 = 1; // Index of current parsed argument
- for (s = s0 + 1; s<=se1; ++p1, s = ns + 1) { // Parse function arguments
- while (*s && cimg::is_blank(*s)) ++s;
- if (*s==')' && p1==1) break; // Function has no arguments
- if (p1>p2) { ++p1; break; }
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=')' || level[ns - expr._data]!=clevel)) ++ns;
- variable_name.assign(s,(unsigned int)(ns - s + 1)).back() = 0; // Argument to write
- arg2 = 0;
- cimg_forX(_expr,k) {
- if (_expr[k]==(char)p1) { // Perform argument substitution
- arg1 = _expr._width;
- _expr.resize(arg1 + variable_name._width - 2,1,1,1,0);
- std::memmove(_expr._data + k + variable_name._width - 1,_expr._data + k + 1,arg1 - k - 1);
- std::memcpy(_expr._data + k,variable_name,variable_name._width - 1);
- k+=variable_name._width - 2;
- }
- ++arg2;
- }
- }
- // Recompute 'pexpr' and 'level' for evaluating substituted expression.
- CImg<charT> _pexpr(_expr._width);
- ns = _pexpr._data;
- for (ps = _expr._data, c1 = ' '; *ps; ++ps) {
- if (!cimg::is_blank(*ps)) c1 = *ps;
- *(ns++) = c1;
- }
- *ns = 0;
- CImg<uintT> _level = get_level(_expr);
- expr.swap(_expr);
- pexpr.swap(_pexpr);
- level.swap(_level);
- s0 = user_macro;
- user_macro = macro_def[l];
- pos = compile(expr._data,expr._data + expr._width - 1,depth1,p_ref,bloc_flags);
- user_macro = s0;
- level.swap(_level);
- pexpr.swap(_pexpr);
- expr.swap(_expr);
- _cimg_mp_return(pos);
- }
- if (arg3) { // Macro name matched but number of arguments does not
- CImg<uintT> sig_nargs(arg3);
- arg1 = 0;
- cimglist_for(macro_def,l) if (!std::strcmp(macro_def[l],variable_name))
- sig_nargs[arg1++] = (unsigned int)macro_def[l].back();
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- if (sig_nargs._width>1) {
- sig_nargs.sort();
- arg1 = sig_nargs.back();
- --sig_nargs._width;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Function '%s()': Number of specified arguments (%u) "
- "does not match macro declaration (defined for %s or %u arguments), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,variable_name._data,
- p1,sig_nargs.value_string()._data,arg1,s0);
- } else
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Function '%s()': Number of specified arguments (%u) "
- "does not match macro declaration (defined for %u argument%s), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,variable_name._data,
- p1,*sig_nargs,*sig_nargs!=1?"s":"",s0);
- }
- }
- } // if (se1==')')
- // Char / string initializer.
- if (*se1=='\'' &&
- ((se1>ss && *ss=='\'') ||
- (se1>ss1 && *ss=='_' && *ss1=='\''))) {
- if (*ss=='_') { _cimg_mp_op("Char initializer"); s1 = ss2; }
- else { _cimg_mp_op("String initializer"); s1 = ss1; }
- arg1 = (unsigned int)(se1 - s1); // Original string length
- if (arg1) {
- CImg<charT>(s1,arg1 + 1).move_to(variable_name).back() = 0;
- cimg::strunescape(variable_name);
- arg1 = (unsigned int)std::strlen(variable_name);
- }
- if (!arg1) _cimg_mp_return(0); // Empty string -> 0
- if (*ss=='_') {
- if (arg1==1) _cimg_mp_const_scalar((unsigned char)*variable_name);
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s: Literal %s contains more than one byte, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,
- ss1,s0);
- }
- pos = vector(arg1);
- CImg<ulongT>::vector((ulongT)mp_string_init,pos,arg1).move_to(l_opcode);
- CImg<ulongT>(1,arg1/sizeof(ulongT) + (arg1%sizeof(ulongT)?1:0)).move_to(l_opcode);
- std::memcpy((char*)l_opcode[1]._data,variable_name,arg1);
- (l_opcode>'y').move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- // Vector initializer [ ... ].
- if (*ss=='[' && *se1==']') {
- _cimg_mp_op("Vector initializer");
- s1 = ss1; while (s1<se2 && cimg::is_blank(*s1)) ++s1;
- s2 = se2; while (s2>s1 && cimg::is_blank(*s2)) --s2;
- if (s2>s1 && *s1=='\'' && *s2=='\'') { // Vector values provided as a string
- arg1 = (unsigned int)(s2 - s1 - 1); // Original string length
- if (arg1) {
- CImg<charT>(s1 + 1,arg1 + 1).move_to(variable_name).back() = 0;
- cimg::strunescape(variable_name);
- arg1 = (unsigned int)std::strlen(variable_name);
- }
- if (!arg1) _cimg_mp_return(0); // Empty string -> 0
- pos = vector(arg1);
- CImg<ulongT>::vector((ulongT)mp_string_init,pos,arg1).move_to(l_opcode);
- CImg<ulongT>(1,arg1/sizeof(ulongT) + (arg1%sizeof(ulongT)?1:0)).move_to(l_opcode);
- std::memcpy((char*)l_opcode[1]._data,variable_name,arg1);
- (l_opcode>'y').move_to(code);
- } else { // Vector values provided as list of items
- arg1 = 0; // Number of specified values
- if (*ss1!=']') for (s = ss1; s<se; ++s) {
- ns = s; while (ns<se && (*ns!=',' || level[ns - expr._data]!=clevel1) &&
- (*ns!=']' || level[ns - expr._data]!=clevel)) ++ns;
- arg2 = compile(s,ns,depth1,0,bloc_flags);
- if (_cimg_mp_is_vector(arg2)) {
- arg3 = _cimg_mp_size(arg2);
- CImg<ulongT>::sequence(arg3,arg2 + 1,arg2 + arg3).move_to(l_opcode);
- arg1+=arg3;
- } else { CImg<ulongT>::vector(arg2).move_to(l_opcode); ++arg1; }
- s = ns;
- }
- if (!arg1) _cimg_mp_return(0);
- pos = vector(arg1);
- l_opcode.insert(CImg<ulongT>::vector((ulongT)mp_vector_init,pos,0,arg1),0);
- (l_opcode>'y').move_to(opcode);
- opcode[2] = opcode._height;
- opcode.move_to(code);
- }
- return_new_comp = true;
- _cimg_mp_return(pos);
- }
- // Variables related to the input list of images.
- if (*ss1=='#' && ss2<se) {
- arg1 = compile(ss2,se,depth1,0,bloc_flags);
- p1 = (unsigned int)(imglist._width && _cimg_mp_is_const_scalar(arg1)?
- cimg::mod((int)mem[arg1],imglist.width()):~0U);
- switch (*ss) {
- case 'w' : // w#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width);
- _cimg_mp_scalar1(mp_list_width,arg1);
- case 'h' : // h#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._height);
- _cimg_mp_scalar1(mp_list_height,arg1);
- case 'd' : // d#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._depth);
- _cimg_mp_scalar1(mp_list_depth,arg1);
- case 'r' : // r#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._is_shared);
- _cimg_mp_scalar1(mp_list_is_shared,arg1);
- case 's' : // s#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._spectrum);
- _cimg_mp_scalar1(mp_list_spectrum,arg1);
- case 'i' : // i#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,_cimg_mp_slot_c,
- 0,_cimg_mp_boundary);
- case 'I' : // I#ind
- p2 = p1!=~0U?imglist[p1]._spectrum:imglist._width?~0U:0;
- if (!p2) _cimg_mp_return(0);
- pos = vector(p2);
- CImg<ulongT>::vector((ulongT)mp_list_Joff,pos,p1,0,0,p2).move_to(code);
- return_new_comp = true;
- _cimg_mp_return(pos);
- case 'R' : // R#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,0,
- 0,_cimg_mp_boundary);
- case 'G' : // G#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,1,
- 0,_cimg_mp_boundary);
- case 'B' : // B#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,2,
- 0,_cimg_mp_boundary);
- case 'A' : // A#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,3,
- 0,_cimg_mp_boundary);
- }
- }
- if (*ss1 && *ss2=='#' && ss3<se) {
- arg1 = compile(ss3,se,depth1,0,bloc_flags);
- p1 = (unsigned int)(imglist._width && _cimg_mp_is_const_scalar(arg1)?
- cimg::mod((int)mem[arg1],imglist.width()):~0U);
- if (*ss=='w' && *ss1=='h') { // wh#ind
- if (!imglist) _cimg_mp_return(0);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height);
- _cimg_mp_scalar1(mp_list_wh,arg1);
- }
- arg2 = ~0U;
- if (*ss=='i') {
- if (*ss1>='0' && *ss1<='9') { // i0#ind...i9#ind
- if (!imglist) _cimg_mp_return(0);
- _cimg_mp_scalar7(mp_list_ixyzc,arg1,_cimg_mp_slot_x,_cimg_mp_slot_y,_cimg_mp_slot_z,*ss1 - '0',
- 0,_cimg_mp_boundary);
- }
- if (*ss1=='c') { // ic#ind
- if (!imglist) _cimg_mp_return(0);
- if (_cimg_mp_is_const_scalar(arg1)) {
- if (!list_median) list_median.assign(imglist._width);
- if (!list_median[p1]) CImg<doubleT>::vector(imglist[p1].median()).move_to(list_median[p1]);
- _cimg_mp_const_scalar(*list_median[p1]);
- }
- _cimg_mp_scalar1(mp_list_median,arg1);
- }
- if (*ss1=='n') { // in#ind
- if (!imglist) _cimg_mp_return(0);
- if (_cimg_mp_is_const_scalar(arg1)) {
- if (!list_norm) list_norm.assign(imglist._width);
- if (!list_norm[p1]) CImg<doubleT>::vector(imglist[p1].magnitude()).move_to(list_norm[p1]);
- _cimg_mp_const_scalar(*list_norm[p1]);
- }
- _cimg_mp_scalar1(mp_list_norm,arg1);
- }
- switch (*ss1) {
- case 'm' : arg2 = 0; break; // im#ind
- case 'M' : arg2 = 1; break; // iM#ind
- case 'a' : arg2 = 2; break; // ia#ind
- case 'v' : arg2 = 3; break; // iv#ind
- case 's' : arg2 = 12; break; // is#ind
- case 'p' : arg2 = 13; break; // ip#ind
- }
- } else if (*ss1=='m') switch (*ss) {
- case 'x' : arg2 = 4; break; // xm#ind
- case 'y' : arg2 = 5; break; // ym#ind
- case 'z' : arg2 = 6; break; // zm#ind
- case 'c' : arg2 = 7; break; // cm#ind
- } else if (*ss1=='M') switch (*ss) {
- case 'x' : arg2 = 8; break; // xM#ind
- case 'y' : arg2 = 9; break; // yM#ind
- case 'z' : arg2 = 10; break; // zM#ind
- case 'c' : arg2 = 11; break; // cM#ind
- }
- if (arg2!=~0U) {
- if (!imglist) _cimg_mp_return(0);
- if (_cimg_mp_is_const_scalar(arg1)) {
- if (!list_stats) list_stats.assign(imglist._width);
- if (!list_stats[p1]) list_stats[p1].assign(1,14,1,1,0).fill(imglist[p1].get_stats(),false);
- _cimg_mp_const_scalar(list_stats(p1,arg2));
- }
- _cimg_mp_scalar2(mp_list_stats,arg1,arg2);
- }
- }
- if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='#' && ss4<se) { // whd#ind
- arg1 = compile(ss4,se,depth1,0,bloc_flags);
- if (!imglist) _cimg_mp_return(0);
- p1 = (unsigned int)(_cimg_mp_is_const_scalar(arg1)?cimg::mod((int)mem[arg1],imglist.width()):~0U);
- if (p1!=~0U) _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height*imglist[p1]._depth);
- _cimg_mp_scalar1(mp_list_whd,arg1);
- }
- if (*ss=='w' && *ss1=='h' && *ss2=='d' && *ss3=='s' && *ss4=='#' && ss5<se) { // whds#ind
- arg1 = compile(ss5,se,depth1,0,bloc_flags);
- if (!imglist) _cimg_mp_return(0);
- p1 = (unsigned int)(_cimg_mp_is_const_scalar(arg1)?cimg::mod((int)mem[arg1],imglist.width()):~0U);
- if (p1!=~0U)
- _cimg_mp_const_scalar(imglist[p1]._width*imglist[p1]._height*imglist[p1]._depth*imglist[p1]._spectrum);
- _cimg_mp_scalar1(mp_list_whds,arg1);
- }
- if (!std::strcmp(ss,"interpolation")) _cimg_mp_return(_cimg_mp_interpolation); // interpolation
- if (!std::strcmp(ss,"boundary")) _cimg_mp_return(_cimg_mp_boundary); // boundary
- #ifdef cimg_mp_operator_dollar
- // External variable '$varname'.
- variable_name.assign(ss,(unsigned int)(se + 1 - ss)).back() = 0;
- if (*ss=='$' && is_varname(variable_name._data + 1))
- _cimg_mp_const_scalar(cimg_mp_operator_dollar(variable_name._data + 1));
- #endif
- // No known item found, assuming this is an already initialized variable.
- if (is_varname(variable_name)) { // Valid variable name
- get_variable_pos(variable_name,arg1,arg2);
- arg1 = arg2!=~0U?reserved_label[arg2]:arg1!=~0U?variable_pos[arg1]:~0U;
- if (arg1!=~0U) _cimg_mp_return(arg1);
- }
- // Reached an unknown item -> error.
- c1 = *se1;
- _cimg_mp_strerr;
- cimg::strellipsize(variable_name,64);
- if (is_sth)
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Undefined variable '%s' in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- variable_name._data,s0);
- s1 = std::strchr(ss,'(');
- s_op = s1 && c1==')'?"function call":"item";
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Unrecognized %s '%s' in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- s_op,variable_name._data,s0);
- }
- // Evaluation procedure.
- double operator()(const double x, const double y, const double z, const double c) {
- mem[_cimg_mp_slot_x] = x; mem[_cimg_mp_slot_y] = y; mem[_cimg_mp_slot_z] = z; mem[_cimg_mp_slot_c] = c;
- for (p_code = code; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- return *result;
- }
- // Evaluation procedure (return output values in vector 'output').
- template<typename t>
- void operator()(const double x, const double y, const double z, const double c, t *const output) {
- mem[_cimg_mp_slot_x] = x; mem[_cimg_mp_slot_y] = y; mem[_cimg_mp_slot_z] = z; mem[_cimg_mp_slot_c] = c;
- for (p_code = code; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- if (result_dim) {
- const double *ptrs = result + 1;
- t *ptrd = output;
- for (unsigned int k = 0; k<result_dim; ++k) *(ptrd++) = (t)*(ptrs++);
- } else *output = (t)*result;
- }
- // Evaluation procedure for begin_t() bloc.
- void begin_t() {
- if (!code_begin_t) return;
- mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
- p_code_end = code_begin_t.end();
- for (p_code = code_begin_t; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- p_code_end = code.end();
- }
- // Evaluation procedure for end_t() bloc.
- void end_t() {
- if (!code_end_t) return;
- if (imgin) {
- mem[_cimg_mp_slot_x] = imgin._width - 1.;
- mem[_cimg_mp_slot_y] = imgin._height - 1.;
- mem[_cimg_mp_slot_z] = imgin._depth - 1.;
- mem[_cimg_mp_slot_c] = imgin._spectrum - 1.;
- } else mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
- p_code_end = code_end_t.end();
- for (p_code = code_end_t; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- }
- // Evaluation procedure the end() bloc.
- void end() {
- if (!code_end) return;
- if (imgin) {
- mem[_cimg_mp_slot_x] = imgin._width - 1.;
- mem[_cimg_mp_slot_y] = imgin._height - 1.;
- mem[_cimg_mp_slot_z] = imgin._depth - 1.;
- mem[_cimg_mp_slot_c] = imgin._spectrum - 1.;
- } else mem[_cimg_mp_slot_x] = mem[_cimg_mp_slot_y] = mem[_cimg_mp_slot_z] = mem[_cimg_mp_slot_c] = 0;
- p_code_end = code_end.end();
- for (p_code = code_end; p_code<p_code_end; ++p_code) {
- opcode._data = p_code->_data;
- const ulongT target = opcode[1];
- mem[target] = _cimg_mp_defunc(*this);
- }
- }
- // Merge inter-thread variables.
- // (argument 'mp' is the master instance).
- void merge(_cimg_math_parser& mp) {
- if (&mp==this) return;
- cimg_rofY(mp.memmerge,k) {
- const unsigned int
- pos = (unsigned int)mp.memmerge(0,k),
- siz = (unsigned int)mp.memmerge(1,k),
- iop = (unsigned int)mp.memmerge(2,k);
- if (!siz) switch (iop) { // Scalar value
- case 0 : mp.mem[pos] = mem[pos]; break; // Assignment
- case 1 : mp.mem[pos]+=mem[pos]; break; // Operator+
- case 2 : mp.mem[pos]-=mem[pos]; break; // Operator-
- case 3 : mp.mem[pos]*=mem[pos]; break; // Operator*
- case 4 : mp.mem[pos]/=mem[pos]; break; // Operator/
- case 5 : mp.mem[pos] = (double)((longT)mp.mem[pos] & (longT)mem[pos]); break; // Operator&
- case 6 : mp.mem[pos] = (double)((longT)mp.mem[pos] | (longT)mem[pos]); break; // Operator|
- case 7 : mp.mem[pos] = (double)((longT)mp.mem[pos] ^ (longT)mem[pos]); break; // Operator 'xor'
- case 8 : mp.mem[pos] = mp.mem[pos] && mem[pos]; break; // Operator&&
- case 9 : mp.mem[pos] = mp.mem[pos] || mem[pos]; break; // Operator||
- case 10 : mp.mem[pos] = std::min(mp.mem[pos],mem[pos]); break; // Operator 'min'
- case 11 : mp.mem[pos] = std::max(mp.mem[pos],mem[pos]); break; // Operator 'max'
- } else switch (iop) { // Vector value
- case 0 : // Assignment
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true) = CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 1 : // Operator+
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)+=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 2 : // Operator-
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)-=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 3 : // Operator*
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)*=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 4 : // Operator/
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)/=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 5 : // Operator&
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)&=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 6 : // Operator|
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)|=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 7 : // Operator 'xor'
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true)^=CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true);
- break;
- case 8 : { // Operator&&
- CImg<doubleT>
- arg1(&mp.mem[pos + 1],siz,1,1,1,true),
- arg2(&mem[pos + 1],siz,1,1,1,true);
- cimg_foroff(arg1,off) arg1[off] = arg1[off] && arg2[off];
- } break;
- case 9 : { // Operator||
- CImg<doubleT>
- arg1(&mp.mem[pos + 1],siz,1,1,1,true),
- arg2(&mem[pos + 1],siz,1,1,1,true);
- cimg_foroff(arg1,off) arg1[off] = arg1[off] || arg2[off];
- } break;
- case 10 : // Operator 'min'
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true).min(CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true));
- break;
- case 11 : // Operator 'max'
- CImg<doubleT>(&mp.mem[pos + 1],siz,1,1,1,true).max(CImg<doubleT>(&mem[pos + 1],siz,1,1,1,true));
- break;
- }
- }
- }
- // Return specified argument number as a string.
- static const char *s_argth(const unsigned int n_arg) {
- const char
- *_s_arg[] = { "", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth","Ninth",
- "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th",
- "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "One of the" };
- return _s_arg[n_arg<sizeof(_s_arg)/sizeof(char*)?n_arg:sizeof(_s_arg)/sizeof(char*)-1];
- }
- // Return a string that defines the calling function + the user-defined function scope.
- CImg<charT> s_calling_function() const {
- CImg<charT> res;
- const unsigned int
- l1 = calling_function?(unsigned int)std::strlen(calling_function):0U,
- l2 = user_macro?(unsigned int)std::strlen(user_macro):0U;
- if (l2) {
- res.assign(l1 + l2 + 48);
- cimg_snprintf(res,res._width,"%s(): When substituting function '%s()'",calling_function,user_macro);
- } else {
- res.assign(l1 + l2 + 4);
- cimg_snprintf(res,res._width,"%s()",calling_function);
- }
- return res;
- }
- // Return type of a memory element as a string.
- CImg<charT> s_type(const unsigned int arg) const {
- CImg<charT> res;
- if (_cimg_mp_is_vector(arg)) { // Vector
- CImg<charT>::string("vectorXXXXXXXXXXXXXXXX").move_to(res);
- cimg_sprintf(res._data + 6,"%u",_cimg_mp_size(arg));
- } else if (_cimg_mp_is_const_scalar(arg)) CImg<charT>::string("const scalar").move_to(res); // Const scalar
- else CImg<charT>::string("scalar").move_to(res); // Scalar
- return res;
- }
- // Count parentheses/brackets level of each character of the expression.
- CImg<uintT> get_level(CImg<charT>& _expr) const {
- bool is_escaped = false, next_is_escaped = false;
- unsigned int mode = 0, next_mode = 0; // { 0=normal | 1=char-string | 2=vector-string
- CImg<uintT> res(_expr._width - 1);
- unsigned int *pd = res._data;
- int _level = 0;
- for (const char *ps = _expr._data; *ps && _level>=0; ++ps) {
- if (!is_escaped && !next_is_escaped && *ps=='\\') next_is_escaped = true;
- if (!is_escaped && *ps=='\'') { // Non-escaped character
- if (!mode && ps>_expr._data && *(ps - 1)=='[') next_mode = mode = 2; // Start vector-string
- else if (mode==2 && *(ps + 1)==']') next_mode = !mode; // End vector-string
- else if (mode<2) next_mode = mode?(mode = 0):1; // Start/end char-string
- }
- *(pd++) = (unsigned int)(mode>=1 || is_escaped?_level + (mode==1):
- *ps=='(' || *ps=='['?_level++:
- *ps==')' || *ps==']'?--_level:
- _level);
- mode = next_mode;
- is_escaped = next_is_escaped;
- next_is_escaped = false;
- }
- if (mode) {
- cimg::strellipsize(_expr,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Unterminated string literal, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- _expr._data);
- }
- if (_level) {
- cimg::strellipsize(_expr,64);
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: Unbalanced parentheses/brackets, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,
- _expr._data);
- }
- return res;
- }
- // Find and return index of current image 'imgin' within image list 'imglist'.
- unsigned int get_mem_img_index() {
- if (mem_img_index==~0U) {
- if (&imgout>imglist.data() && &imgout<imglist.end())
- mem_img_index = const_scalar((double)(&imgout - imglist.data()));
- else {
- unsigned int pos = ~0U;
- cimglist_for(imglist,l)
- if (imgout._data==imglist[l]._data && imgout.is_sameXYZC(imglist[l])) { pos = l; break; }
- if (pos!=~0U) mem_img_index = const_scalar((double)pos);
- }
- }
- return mem_img_index;
- }
- // Return indices for accessing math parser variables.
- void get_variable_pos(const char *variable_name, unsigned int &pos, unsigned int &rpos) {
- char c1, c2, c3, c4;
- pos = rpos = ~0U;
- if (!variable_name || !*variable_name) return;
- unsigned int rp = variable_name[1]?~0U:*variable_name; // One-char variable
- if (variable_name[1] && !variable_name[2]) { // Two-chars variable
- c1 = variable_name[0];
- c2 = variable_name[1];
- if (c1=='w' && c2=='h') rp = 0; // wh
- else if (c1=='p' && c2=='i') rp = 3; // pi
- else if (c1=='i') {
- if (c2>='0' && c2<='9') rp = 20 + c2 - '0'; // i0...i9
- else if (c2=='m') rp = 4; // im
- else if (c2=='M') rp = 5; // iM
- else if (c2=='a') rp = 6; // ia
- else if (c2=='v') rp = 7; // iv
- else if (c2=='s') rp = 8; // is
- else if (c2=='p') rp = 9; // ip
- else if (c2=='c') rp = 10; // ic
- else if (c2=='n') rp = 11; // in
- } else if (c2=='m') {
- if (c1=='x') rp = 12; // xm
- else if (c1=='y') rp = 13; // ym
- else if (c1=='z') rp = 14; // zm
- else if (c1=='c') rp = 15; // cm
- } else if (c2=='M') {
- if (c1=='x') rp = 16; // xM
- else if (c1=='y') rp = 17; // yM
- else if (c1=='z') rp = 18; // zM
- else if (c1=='c') rp = 19; // cM
- }
- } else if (variable_name[1] && variable_name[2] && !variable_name[3]) { // Three-chars variable
- c1 = variable_name[0];
- c2 = variable_name[1];
- c3 = variable_name[2];
- if (c1=='w' && c2=='h' && c3=='d') rp = 1; // whd
- } else if (variable_name[1] && variable_name[2] && variable_name[3] &&
- !variable_name[4]) { // Four-chars variable
- c1 = variable_name[0];
- c2 = variable_name[1];
- c3 = variable_name[2];
- c4 = variable_name[3];
- if (c1=='w' && c2=='h' && c3=='d' && c4=='s') rp = 2; // whds
- } else if (!std::strcmp(variable_name,"interpolation")) rp = 30; // interpolation
- else if (!std::strcmp(variable_name,"boundary")) rp = 31; // boundary
- if (rp!=~0U) { rpos = rp; return; } // One of the reserved labels
- // Multi-char variable name : check for existing variable with same name
- cimglist_for(variable_def,i)
- if (!std::strcmp(variable_name,variable_def[i])) { pos = i; break; }
- }
- // Tell for each character of an expression if it is inside a string or not.
- CImg<boolT> is_inside_string(CImg<charT>& _expr) const {
- bool is_escaped = false, next_is_escaped = false;
- unsigned int mode = 0, next_mode = 0; // { 0=normal | 1=char-string | 2=vector-string
- CImg<boolT> res = CImg<charT>::string(_expr);
- bool *pd = res._data;
- for (const char *ps = _expr._data; *ps; ++ps) {
- if (!next_is_escaped && *ps=='\\') next_is_escaped = true;
- if (!is_escaped && *ps=='\'') { // Non-escaped character
- if (!mode && ps>_expr._data && *(ps - 1)=='[') next_mode = mode = 2; // Start vector-string
- else if (mode==2 && *(ps + 1)==']') next_mode = !mode; // End vector-string
- else if (mode<2) next_mode = mode?(mode = 0):1; // Start/end char-string
- }
- *(pd++) = mode>=1 || is_escaped;
- mode = next_mode;
- is_escaped = next_is_escaped;
- next_is_escaped = false;
- }
- return res;
- }
- // Return true if specified argument can be a part of an allowed variable name.
- static bool is_varchar(const char c) {
- return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
- }
- // Return true if specified argument can be considered as a variable name.
- static bool is_varname(const char *const str, const unsigned int length=~0U) {
- if (*str>='0' && *str<='9') return false;
- for (unsigned int l = 0; l<length && str[l]; ++l)
- if (!is_varchar(str[l])) return false;
- return true;
- }
- // Return true if all values of a vector are computation values.
- bool is_comp_vector(const unsigned int arg) const {
- unsigned int siz = _cimg_mp_size(arg);
- if (siz>128) return false;
- const int *ptr = memtype.data(arg + 1);
- bool is_tmp = true;
- while (siz-->0) if (*(ptr++)) { is_tmp = false; break; }
- return is_tmp;
- }
- // Check if a memory slot is a positive integer constant scalar value.
- // 'mode' can be:
- // { 0=constant | 1=integer constant | 2=positive integer constant | 3=strictly-positive integer constant }
- void check_const_scalar(const unsigned int arg, const unsigned int n_arg,
- const unsigned int mode,
- char *const ss, char *const se, const char saved_char) {
- _cimg_mp_check_type(arg,n_arg,1,0);
- if (!_cimg_mp_is_const_scalar(arg)) {
- const char *const s_arg = s_argth(n_arg);
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s %s%s (of type '%s') is not a constant, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
- s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,s0);
- }
- const double val = mem[arg];
- if (!((!mode || (double)(int)mem[arg]==mem[arg]) &&
- (mode<2 || mem[arg]>=(mode==3)))) {
- const char *const s_arg = s_argth(n_arg);
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s %s%s (of type '%s' and value %g) is not a%s constant, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
- s_arg,*s_arg?" argument":" Argument",s_type(arg)._data,val,
- !mode?"":mode==1?"n integer":
- mode==2?" positive integer":" strictly positive integer",s0);
- }
- }
- // Check if an image index is a constant value.
- void check_const_index(const unsigned int arg,
- char *const ss, char *const se, const char saved_char) {
- if (arg!=~0U && !_cimg_mp_is_const_scalar(arg)) {
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s Specified image index is not a constant, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s0);
- }
- }
- // Check a matrix is square.
- void check_matrix_square(const unsigned int arg, const unsigned int n_arg,
- char *const ss, char *const se, const char saved_char) {
- _cimg_mp_check_type(arg,n_arg,2,0);
- const unsigned int
- siz = _cimg_mp_size(arg),
- n = (unsigned int)cimg::round(std::sqrt((float)siz));
- if (n*n!=siz) {
- const char *s_arg;
- if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
- else s_arg = !n_arg?"":n_arg==1?"First":n_arg==2?"Second":n_arg==3?"Third":"One";
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s %s%s (of type '%s') "
- "cannot be considered as a square matrix, in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
- s_arg,*s_op=='F'?(*s_arg?" argument":" Argument"):(*s_arg?" operand":" Operand"),
- s_type(arg)._data,s0);
- }
- }
- // Check type compatibility for one argument.
- // Bits of 'mode' tells what types are allowed:
- // { 1 = scalar | 2 = vectorN }.
- // If 'N' is not zero, it also restricts the vectors to be of size N only.
- void check_type(const unsigned int arg, const unsigned int n_arg,
- const unsigned int mode, const unsigned int N,
- char *const ss, char *const se, const char saved_char) {
- const bool
- is_scalar = _cimg_mp_is_scalar(arg),
- is_vector = _cimg_mp_is_vector(arg) && (!N || _cimg_mp_size(arg)==N);
- bool cond = false;
- if (mode&1) cond|=is_scalar;
- if (mode&2) cond|=is_vector;
- if (!cond) {
- const char *s_arg;
- if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
- else s_arg = s_argth(n_arg);
- CImg<charT> sb_type(32);
- if (mode==1) cimg_snprintf(sb_type,sb_type._width,"'scalar'");
- else if (mode==2) {
- if (N) cimg_snprintf(sb_type,sb_type._width,"'vector%u'",N);
- else cimg_snprintf(sb_type,sb_type._width,"'vector'");
- } else {
- if (N) cimg_snprintf(sb_type,sb_type._width,"'scalar' or 'vector%u'",N);
- else cimg_snprintf(sb_type,sb_type._width,"'scalar' or 'vector'");
- }
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s %s%s has invalid type '%s' (should be %s), "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",
- s_arg,*s_op=='F'?(*s_arg?" argument":" Argument"):(*s_arg?" operand":" Operand"),
- s_type(arg)._data,sb_type._data,s0);
- }
- }
- // Check that imglist are not empty.
- void check_list(char *const ss, char *const se, const char saved_char) {
- if (!imglist) {
- char *s0; _cimg_mp_strerr;
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>::%s: %s%s Invalid call with an empty image list, "
- "in expression '%s'.",
- pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s0);
- }
- }
- static void mp_check_list(_cimg_math_parser& mp, const char *const funcname) {
- if (!mp.imglist)
- throw CImgArgumentException("[" cimg_appname "_math_parser] "
- "CImg<%s>: Function '%s()': Invalid call with an empty image list.",
- pixel_type(),funcname);
- }
- // Insert constant value in memory.
- unsigned int const_scalar(const double val) {
- // Search for built-in constant.
- if (cimg::type<double>::is_nan(val)) return _cimg_mp_slot_nan;
- if (val==(double)(int)val) {
- if (val>=0 && val<=10) return (unsigned int)val;
- if (val<0 && val>=-5) return (unsigned int)(10 - val);
- }
- if (val==0.5) return 16;
- // Search for constant already requested before (in const cache).
- unsigned int ind = ~0U;
- if (constcache_size<1024) {
- if (!constcache_size) {
- constcache_vals.assign(16,1,1,1,0);
- constcache_inds.assign(16,1,1,1,0);
- *constcache_vals = val;
- constcache_size = 1;
- ind = 0;
- } else { // Dichotomic search
- const double val_beg = *constcache_vals, val_end = constcache_vals[constcache_size - 1];
- if (val_beg>=val) ind = 0;
- else if (val_end==val) ind = constcache_size - 1;
- else if (val_end<val) ind = constcache_size;
- else {
- unsigned int i0 = 1, i1 = constcache_size - 2;
- while (i0<=i1) {
- const unsigned int mid = (i0 + i1)/2;
- if (constcache_vals[mid]==val) { i0 = mid; break; }
- else if (constcache_vals[mid]<val) i0 = mid + 1;
- else i1 = mid - 1;
- }
- ind = i0;
- }
- if (ind>=constcache_size || constcache_vals[ind]!=val) {
- ++constcache_size;
- if (constcache_size>constcache_vals._width) {
- constcache_vals.resize(-200,1,1,1,0);
- constcache_inds.resize(-200,1,1,1,0);
- }
- const int l = constcache_size - (int)ind - 1;
- if (l>0) {
- std::memmove(&constcache_vals[ind + 1],&constcache_vals[ind],l*sizeof(double));
- std::memmove(&constcache_inds[ind + 1],&constcache_inds[ind],l*sizeof(unsigned int));
- }
- constcache_vals[ind] = val;
- constcache_inds[ind] = 0;
- }
- }
- if (constcache_inds[ind]) return constcache_inds[ind];
- }
- // Insert new constant in memory if necessary.
- if (mempos>=mem._width) { mem.resize(-200,1,1,1,0); memtype.resize(-200,1,1,1,0); }
- const unsigned int pos = mempos++;
- mem[pos] = val;
- memtype[pos] = 1; // Set constant property
- if (ind!=~0U) constcache_inds[ind] = pos;
- return pos;
- }
- // Insert new scalar in memory.
- unsigned int scalar() {
- if (mempos>=mem._width) { mem.resize(-200,1,1,1,0); memtype.resize(mem._width,1,1,1,0); }
- return mempos++;
- }
- // Insert new vector of specified size in memory.
- unsigned int vector(const unsigned int siz) {
- if (mempos + siz>=mem._width) {
- mem.resize(2*mem._width + siz,1,1,1,0);
- memtype.resize(mem._width,1,1,1,0);
- }
- const unsigned int pos = mempos++;
- mem[pos] = cimg::type<double>::nan();
- memtype[pos] = siz + 1;
- mempos+=siz;
- return pos;
- }
- // Insert new initialized vector.
- unsigned int vector(const unsigned int siz, const double value) {
- const unsigned int pos = vector(siz);
- double *ptr = &mem[pos] + 1;
- for (unsigned int i = 0; i<siz; ++i) *(ptr++) = value;
- return pos;
- }
- // Insert new copy of specified vector in memory.
- unsigned int vector_copy(const unsigned int arg) {
- const unsigned int
- siz = _cimg_mp_size(arg),
- pos = vector(siz);
- CImg<ulongT>::vector((ulongT)mp_vector_copy,pos,arg,siz).move_to(code);
- return pos;
- }
- // Set reserved status to all values of a vector.
- void set_reserved_vector(const unsigned int arg) {
- unsigned int siz = _cimg_mp_size(arg);
- int *ptr = memtype.data(arg + 1);
- while (siz-->0) *(ptr++) = -1;
- }
- unsigned int scalar0(const mp_func op) {
- const unsigned int pos = scalar();
- CImg<ulongT>::vector((ulongT)op,pos).move_to(code);
- return_new_comp = true;
- return pos;
- }
- unsigned int scalar1(const mp_func op, const unsigned int arg1) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1) && op!=mp_copy?arg1:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1).move_to(code);
- return pos;
- }
- unsigned int scalar2(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2).move_to(code);
- return pos;
- }
- unsigned int scalar3(const mp_func op,
- const unsigned int arg1, const unsigned int arg2, const unsigned int arg3) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3).move_to(code);
- return pos;
- }
- unsigned int scalar4(const mp_func op,
- const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
- const unsigned int arg4) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
- arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4).move_to(code);
- return pos;
- }
- unsigned int scalar5(const mp_func op,
- const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
- const unsigned int arg4, const unsigned int arg5) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
- arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
- arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5).move_to(code);
- return pos;
- }
- unsigned int scalar6(const mp_func op,
- const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
- const unsigned int arg4, const unsigned int arg5, const unsigned int arg6) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
- arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
- arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
- arg6!=~0U && arg6>_cimg_mp_slot_c && _cimg_mp_is_comp(arg6)?arg6:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5,arg6).move_to(code);
- return pos;
- }
- unsigned int scalar7(const mp_func op,
- const unsigned int arg1, const unsigned int arg2, const unsigned int arg3,
- const unsigned int arg4, const unsigned int arg5, const unsigned int arg6,
- const unsigned int arg7) {
- const unsigned int pos =
- arg1!=~0U && arg1>_cimg_mp_slot_c && _cimg_mp_is_comp(arg1)?arg1:
- arg2!=~0U && arg2>_cimg_mp_slot_c && _cimg_mp_is_comp(arg2)?arg2:
- arg3!=~0U && arg3>_cimg_mp_slot_c && _cimg_mp_is_comp(arg3)?arg3:
- arg4!=~0U && arg4>_cimg_mp_slot_c && _cimg_mp_is_comp(arg4)?arg4:
- arg5!=~0U && arg5>_cimg_mp_slot_c && _cimg_mp_is_comp(arg5)?arg5:
- arg6!=~0U && arg6>_cimg_mp_slot_c && _cimg_mp_is_comp(arg6)?arg6:
- arg7!=~0U && arg7>_cimg_mp_slot_c && _cimg_mp_is_comp(arg7)?arg7:
- ((return_new_comp = true), scalar());
- CImg<ulongT>::vector((ulongT)op,pos,arg1,arg2,arg3,arg4,arg5,arg6,arg7).move_to(code);
- return pos;
- }
- void self_vector_s(const unsigned int pos, const mp_func op, const unsigned int arg1) {
- const unsigned int siz = _cimg_mp_size(pos);
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_self_map_vector_s,pos,siz,(ulongT)op,arg1).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1).move_to(code[code._width - 1 - siz + k]);
- }
- }
- void self_vector_v(const unsigned int pos, const mp_func op, const unsigned int arg1) {
- const unsigned int siz = _cimg_mp_size(pos);
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_self_map_vector_v,pos,siz,(ulongT)op,arg1).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k).move_to(code[code._width - 1 - siz + k]);
- }
- }
- unsigned int vector1_v(const mp_func op, const unsigned int arg1) {
- const unsigned int
- siz = _cimg_mp_size(arg1),
- pos = is_comp_vector(arg1)?arg1:
- ((return_new_comp = true), vector(siz));
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_v,pos,siz,(ulongT)op,arg1).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k).move_to(code[code._width - 1 - siz + k]);
- }
- return pos;
- }
- unsigned int vector2_vv(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
- const unsigned int
- siz = _cimg_mp_size(arg1),
- pos = is_comp_vector(arg1)?arg1:is_comp_vector(arg2)?arg2:
- ((return_new_comp = true), vector(siz));
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vv,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2 + k).move_to(code[code._width - 1 - siz + k]);
- }
- return pos;
- }
- unsigned int vector2_vs(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
- const unsigned int
- siz = _cimg_mp_size(arg1),
- pos = is_comp_vector(arg1)?arg1:
- ((return_new_comp = true), vector(siz));
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vs,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2).move_to(code[code._width - 1 - siz + k]);
- }
- return pos;
- }
- unsigned int vector2_sv(const mp_func op, const unsigned int arg1, const unsigned int arg2) {
- const unsigned int
- siz = _cimg_mp_size(arg2),
- pos = is_comp_vector(arg2)?arg2:
- ((return_new_comp = true), vector(siz));
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_sv,pos,siz,(ulongT)op,arg1,arg2).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1,arg2 + k).move_to(code[code._width - 1 - siz + k]);
- }
- return pos;
- }
- unsigned int vector3_vss(const mp_func op, const unsigned int arg1, const unsigned int arg2,
- const unsigned int arg3) {
- const unsigned int
- siz = _cimg_mp_size(arg1),
- pos = is_comp_vector(arg1)?arg1:
- ((return_new_comp = true), vector(siz));
- if (siz>24) CImg<ulongT>::vector((ulongT)mp_vector_map_vss,pos,siz,(ulongT)op,arg1,arg2,arg3).move_to(code);
- else {
- code.insert(siz);
- for (unsigned int k = 1; k<=siz; ++k)
- CImg<ulongT>::vector((ulongT)op,pos + k,arg1 + k,arg2,arg3).move_to(code[code._width - 1 - siz + k]);
- }
- return pos;
- }
- // Evaluation functions, known by the parser.
- // Defining these functions 'static' ensures that sizeof(mp_func)==sizeof(ulongT),
- // so we can store pointers to them directly in the opcode vectors.
- #ifdef _mp_arg
- #undef _mp_arg
- #endif
- #define _mp_arg(x) mp.mem[mp.opcode[x]]
- static double mp_abs(_cimg_math_parser& mp) {
- return cimg::abs(_mp_arg(2));
- }
- static double mp_add(_cimg_math_parser& mp) {
- return _mp_arg(2) + _mp_arg(3);
- }
- static double mp_acos(_cimg_math_parser& mp) {
- return std::acos(_mp_arg(2));
- }
- static double mp_acosh(_cimg_math_parser& mp) {
- return cimg::acosh(_mp_arg(2));
- }
- static double mp_asinh(_cimg_math_parser& mp) {
- return cimg::asinh(_mp_arg(2));
- }
- static double mp_atanh(_cimg_math_parser& mp) {
- return cimg::atanh(_mp_arg(2));
- }
- static double mp_arg(_cimg_math_parser& mp) {
- const int _ind = (int)_mp_arg(4);
- const unsigned int
- nb_args = (unsigned int)mp.opcode[2] - 4,
- ind = _ind<0?_ind + nb_args:(unsigned int)_ind,
- siz = (unsigned int)mp.opcode[3];
- if (siz>0) {
- if (ind>=nb_args) std::memset(&_mp_arg(1) + 1,0,siz*sizeof(double));
- else std::memcpy(&_mp_arg(1) + 1,&_mp_arg(ind + 4) + 1,siz*sizeof(double));
- return cimg::type<double>::nan();
- }
- if (ind>=nb_args) return 0;
- return _mp_arg(ind + 4);
- }
- static double mp_arg0(_cimg_math_parser& mp) {
- const int _ind = (int)_mp_arg(4);
- const unsigned int
- nb_args = (unsigned int)mp.opcode[2] - 4,
- ind = _ind<0?_ind + nb_args:_ind + 1U,
- siz = (unsigned int)mp.opcode[3];
- if (siz>0) {
- if (ind>=nb_args) std::memset(&_mp_arg(1) + 1,0,siz*sizeof(double));
- else std::memcpy(&_mp_arg(1) + 1,&_mp_arg(ind + 4) + 1,siz*sizeof(double));
- return cimg::type<double>::nan();
- }
- if (ind>=nb_args) return 0;
- return _mp_arg(ind + 4);
- }
- static double mp_argkth(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- const double val = mp_kth(mp);
- for (unsigned int i = 4; i<i_end; ++i) if (val==_mp_arg(i)) return i - 3.;
- return 1.;
- }
- static double mp_argmin(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- unsigned int argval = 0;
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i);
- if (_val<val) { val = _val; argval = i - 3; }
- }
- return (double)argval;
- }
- static double mp_argminabs(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3), absval = cimg::abs(val);
- unsigned int argval = 0;
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i), _absval = cimg::abs(_val);
- if (_absval<absval) { val = _val; absval = _absval; argval = i - 3; }
- }
- return (double)argval;
- }
- static double mp_argmax(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- unsigned int argval = 0;
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i);
- if (_val>val) { val = _val; argval = i - 3; }
- }
- return (double)argval;
- }
- static double mp_argmaxabs(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3), absval = cimg::abs(val);
- unsigned int argval = 0;
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i), _absval = cimg::abs(_val);
- if (_absval>absval) { val = _val; absval = _absval; argval = i - 3; }
- }
- return (double)argval;
- }
- static double mp_asin(_cimg_math_parser& mp) {
- return std::asin(_mp_arg(2));
- }
- static double mp_atan(_cimg_math_parser& mp) {
- return std::atan(_mp_arg(2));
- }
- static double mp_atan2(_cimg_math_parser& mp) {
- return std::atan2(_mp_arg(2),_mp_arg(3));
- }
- static double mp_avg(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i) val+=_mp_arg(i);
- return val/(i_end - 3);
- }
- static double mp_bitwise_and(_cimg_math_parser& mp) {
- return (double)((longT)_mp_arg(2) & (longT)_mp_arg(3));
- }
- static double mp_bitwise_left_shift(_cimg_math_parser& mp) {
- return (double)((longT)_mp_arg(2)<<(unsigned int)_mp_arg(3));
- }
- static double mp_bitwise_not(_cimg_math_parser& mp) {
- // Limit result to 32bits such that it can be entirely represented as a 'double'.
- return (double)~(unsigned int)_mp_arg(2);
- }
- static double mp_bitwise_or(_cimg_math_parser& mp) {
- return (double)((longT)_mp_arg(2) | (longT)_mp_arg(3));
- }
- static double mp_bitwise_right_shift(_cimg_math_parser& mp) {
- return (double)((longT)_mp_arg(2)>>(unsigned int)_mp_arg(3));
- }
- static double mp_bitwise_xor(_cimg_math_parser& mp) {
- return (double)((longT)_mp_arg(2) ^ (longT)_mp_arg(3));
- }
- static double mp_bool(_cimg_math_parser& mp) {
- return (double)(bool)_mp_arg(2);
- }
- static double mp_break(_cimg_math_parser& mp) {
- mp.break_type = 1;
- mp.p_code = mp.p_break - 1;
- return cimg::type<double>::nan();
- }
- static double mp_breakpoint(_cimg_math_parser& mp) {
- cimg_abort_init;
- cimg_abort_test;
- cimg::unused(mp);
- return cimg::type<double>::nan();
- }
- #ifdef cimg_mp_func_run
- static double mp_run(_cimg_math_parser& mp) {
- const unsigned int nb_args = (unsigned int)(mp.opcode[2] - 3)/2;
- CImgList<charT> _str;
- CImg<charT> it;
- for (unsigned int n = 0; n<nb_args; ++n) {
- const unsigned int siz = (unsigned int)mp.opcode[4 + 2*n];
- if (siz) { // Vector argument -> string
- const double *ptr = &_mp_arg(3 + 2*n) + 1;
- unsigned int l = 0;
- while (l<siz && ptr[l]) ++l;
- CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
- } else { // Scalar argument -> number
- it.assign(24);
- cimg_snprintf(it,it._width,"%.17g",_mp_arg(3 + 2*n));
- CImg<charT>::string(it,false,true).move_to(_str);
- }
- }
- CImg(1,1,1,1,0).move_to(_str);
- CImg<charT> str = _str>'x';
- cimg_mp_func_run(str._data);
- return cimg::type<double>::nan();
- }
- #endif
- static double mp_cbrt(_cimg_math_parser& mp) {
- return cimg::cbrt(_mp_arg(2));
- }
- static double mp_ceil(_cimg_math_parser& mp) {
- return std::ceil(_mp_arg(2));
- }
- static double mp_complex_abs(_cimg_math_parser& mp) {
- return cimg::_hypot(_mp_arg(2),_mp_arg(3));
- }
- static double mp_complex_conj(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = real;
- ptrd[1] = -imag;
- return cimg::type<double>::nan();
- }
- static double mp_complex_div_sv(_cimg_math_parser& mp) {
- const double
- *ptr2 = &_mp_arg(3) + 1,
- r1 = _mp_arg(2),
- r2 = *(ptr2++), i2 = *ptr2;
- double *ptrd = &_mp_arg(1) + 1;
- const double denom = r2*r2 + i2*i2;
- *(ptrd++) = r1*r2/denom;
- *ptrd = -r1*i2/denom;
- return cimg::type<double>::nan();
- }
- static double mp_complex_div_vv(_cimg_math_parser& mp) {
- const double
- *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1,
- r1 = *(ptr1++), i1 = *ptr1,
- r2 = *(ptr2++), i2 = *ptr2;
- double *ptrd = &_mp_arg(1) + 1;
- const double denom = r2*r2 + i2*i2;
- *(ptrd++) = (r1*r2 + i1*i2)/denom;
- *ptrd = (r2*i1 - r1*i2)/denom;
- return cimg::type<double>::nan();
- }
- static double mp_complex_exp(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3), exp_real = std::exp(real);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = exp_real*std::cos(imag);
- ptrd[1] = exp_real*std::sin(imag);
- return cimg::type<double>::nan();
- }
- static double mp_complex_log(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = 0.5*std::log(real*real + imag*imag);
- ptrd[1] = std::atan2(imag,real);
- return cimg::type<double>::nan();
- }
- static double mp_complex_mul(_cimg_math_parser& mp) {
- const double
- *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1,
- r1 = *(ptr1++), i1 = *ptr1,
- r2 = *(ptr2++), i2 = *ptr2;
- double *ptrd = &_mp_arg(1) + 1;
- *(ptrd++) = r1*r2 - i1*i2;
- *(ptrd++) = r1*i2 + r2*i1;
- return cimg::type<double>::nan();
- }
- static void _mp_complex_pow(const double r1, const double i1,
- const double r2, const double i2,
- double *ptrd) {
- double ro, io;
- if (cimg::abs(i2)<1e-15) { // Exponent is real
- if (cimg::abs(r1)<1e-15 && cimg::abs(i1)<1e-15) {
- if (cimg::abs(r2)<1e-15) { ro = 1; io = 0; }
- else ro = io = 0;
- } else {
- const double
- mod1_2 = r1*r1 + i1*i1,
- phi1 = std::atan2(i1,r1),
- modo = std::pow(mod1_2,0.5*r2),
- phio = r2*phi1;
- ro = modo*std::cos(phio);
- io = modo*std::sin(phio);
- }
- } else { // Exponent is complex
- if (cimg::abs(r1)<1e-15 && cimg::abs(i1)<1e-15) ro = io = 0;
- const double
- mod1_2 = r1*r1 + i1*i1,
- phi1 = std::atan2(i1,r1),
- modo = std::pow(mod1_2,0.5*r2)*std::exp(-i2*phi1),
- phio = r2*phi1 + 0.5*i2*std::log(mod1_2);
- ro = modo*std::cos(phio);
- io = modo*std::sin(phio);
- }
- *(ptrd++) = ro;
- *ptrd = io;
- }
- static double mp_complex_pow_ss(_cimg_math_parser& mp) {
- const double val1 = _mp_arg(2), val2 = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- _mp_complex_pow(val1,0,val2,0,ptrd);
- return cimg::type<double>::nan();
- }
- static double mp_complex_pow_sv(_cimg_math_parser& mp) {
- const double val1 = _mp_arg(2), *ptr2 = &_mp_arg(3) + 1;
- double *ptrd = &_mp_arg(1) + 1;
- _mp_complex_pow(val1,0,ptr2[0],ptr2[1],ptrd);
- return cimg::type<double>::nan();
- }
- static double mp_complex_pow_vs(_cimg_math_parser& mp) {
- const double *ptr1 = &_mp_arg(2) + 1, val2 = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- _mp_complex_pow(ptr1[0],ptr1[1],val2,0,ptrd);
- return cimg::type<double>::nan();
- }
- static double mp_complex_pow_vv(_cimg_math_parser& mp) {
- const double *ptr1 = &_mp_arg(2) + 1, *ptr2 = &_mp_arg(3) + 1;
- double *ptrd = &_mp_arg(1) + 1;
- _mp_complex_pow(ptr1[0],ptr1[1],ptr2[0],ptr2[1],ptrd);
- return cimg::type<double>::nan();
- }
- static double mp_complex_cos(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::cos(real)*std::cosh(imag);
- ptrd[1] = -std::sin(real)*std::sinh(imag);
- return cimg::type<double>::nan();
- }
- static double mp_complex_sin(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::sin(real)*std::cosh(imag);
- ptrd[1] = std::cos(real)*std::sinh(imag);
- return cimg::type<double>::nan();
- }
- static double mp_complex_tan(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3), denom = std::cos(2*real) + std::cosh(2*imag);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::sin(2*real)/denom;
- ptrd[1] = std::sinh(2*imag)/denom;
- return cimg::type<double>::nan();
- }
- static double mp_complex_cosh(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::cosh(real)*std::cos(imag);
- ptrd[1] = std::sinh(real)*std::sin(imag);
- return cimg::type<double>::nan();
- }
- static double mp_complex_sinh(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::sinh(real)*std::cos(imag);
- ptrd[1] = std::cosh(real)*std::sin(imag);
- return cimg::type<double>::nan();
- }
- static double mp_complex_tanh(_cimg_math_parser& mp) {
- const double real = _mp_arg(2), imag = _mp_arg(3), denom = std::cosh(2*real) + std::cos(2*imag);
- double *ptrd = &_mp_arg(1) + 1;
- ptrd[0] = std::sinh(2*real)/denom;
- ptrd[1] = std::sin(2*imag)/denom;
- return cimg::type<double>::nan();
- }
- static double mp_continue(_cimg_math_parser& mp) {
- mp.break_type = 2;
- mp.p_code = mp.p_break - 1;
- return cimg::type<double>::nan();
- }
- static double mp_convolve(_cimg_math_parser &mp) {
- return _mp_correlate(mp,true);
- }
- static double mp_copy(_cimg_math_parser& mp) {
- return _mp_arg(2);
- }
- static double mp_correlate(_cimg_math_parser &mp) {
- return _mp_correlate(mp,false);
- }
- static double _mp_correlate(_cimg_math_parser &mp, bool is_convolve) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *const ptrA = &_mp_arg(2) + 1, *const ptrM = &_mp_arg(7) + 1;
- const unsigned int
- wA = (unsigned int)mp.opcode[3],
- hA = (unsigned int)mp.opcode[4],
- dA = (unsigned int)mp.opcode[5],
- sA = (unsigned int)mp.opcode[6],
- wM = (unsigned int)mp.opcode[8],
- hM = (unsigned int)mp.opcode[9],
- dM = (unsigned int)mp.opcode[10],
- sM = (unsigned int)mp.opcode[11],
- boundary_conditions = (unsigned int)_mp_arg(12),
- channel_mode = (unsigned int)mp.opcode[14];
- const bool
- is_normalized = (bool)_mp_arg(13),
- interpolation_type = (bool)_mp_arg(30);
- const int
- xcenter = mp.opcode[15]!=~0U?(int)_mp_arg(15):(int)(~0U>>1),
- ycenter = mp.opcode[16]!=~0U?(int)_mp_arg(16):(int)(~0U>>1),
- zcenter = mp.opcode[17]!=~0U?(int)_mp_arg(17):(int)(~0U>>1),
- xstart = (int)mp.opcode[18],
- ystart = (int)mp.opcode[19],
- zstart = (int)mp.opcode[20],
- xend = (int)mp.opcode[21],
- yend = (int)mp.opcode[22],
- zend = (int)mp.opcode[23];
- const float
- xstride = (float)_mp_arg(24),
- ystride = (float)_mp_arg(25),
- zstride = (float)_mp_arg(26),
- xdilation = (float)_mp_arg(27),
- ydilation = (float)_mp_arg(28),
- zdilation = (float)_mp_arg(29);
- CImg<doubleT> res;
- if (is_convolve) res = CImg<doubleT>(ptrA,wA,hA,dA,sA,true).
- get_convolve(CImg<doubleT>(ptrM,wM,hM,dM,sM,true),
- boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,
- xstart,ystart,zstart,
- xend,yend,zend,
- xstride,ystride,zstride,
- xdilation,ydilation,zdilation,
- interpolation_type);
- else res = CImg<doubleT>(ptrA,wA,hA,dA,sA,true).
- get_correlate(CImg<doubleT>(ptrM,wM,hM,dM,sM,true),
- boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,
- xstart,ystart,zstart,
- xend,yend,zend,
- xstride,ystride,zstride,
- xdilation,ydilation,zdilation,
- interpolation_type);
- CImg<doubleT>(ptrd,res._width,res._height,res._depth,res._spectrum,true) = res;
- return cimg::type<double>::nan();
- }
- static double mp_cos(_cimg_math_parser& mp) {
- return std::cos(_mp_arg(2));
- }
- static double mp_cosh(_cimg_math_parser& mp) {
- return std::cosh(_mp_arg(2));
- }
- static double mp_critical(_cimg_math_parser& mp) {
- const ulongT g_target = mp.opcode[1];
- cimg_pragma_openmp(critical(mp_critical))
- {
- for (const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[2];
- mp.p_code<p_end; ++mp.p_code) { // Evaluate body
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- }
- --mp.p_code;
- return mp.mem[g_target];
- }
- static double mp_crop(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const int x = (int)_mp_arg(3), y = (int)_mp_arg(4), z = (int)_mp_arg(5), c = (int)_mp_arg(6);
- const unsigned int
- dx = (unsigned int)mp.opcode[7],
- dy = (unsigned int)mp.opcode[8],
- dz = (unsigned int)mp.opcode[9],
- dc = (unsigned int)mp.opcode[10];
- const unsigned int boundary_conditions = (unsigned int)_mp_arg(11);
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- const CImg<T> &img = ind==~0U?mp.imgin:mp.imglist[ind];
- if (!img) std::memset(ptrd,0,dx*dy*dz*dc*sizeof(double));
- else CImg<doubleT>(ptrd,dx,dy,dz,dc,true) = img.get_crop(x,y,z,c,
- x + dx - 1,y + dy - 1,
- z + dz - 1,c + dc - 1,
- boundary_conditions);
- return cimg::type<double>::nan();
- }
- static double mp_cross(_cimg_math_parser& mp) {
- CImg<doubleT>
- vout(&_mp_arg(1) + 1,1,3,1,1,true),
- v1(&_mp_arg(2) + 1,1,3,1,1,true),
- v2(&_mp_arg(3) + 1,1,3,1,1,true);
- (vout = v1).cross(v2);
- return cimg::type<double>::nan();
- }
- static double mp_cut(_cimg_math_parser& mp) {
- double val = _mp_arg(2), cmin = _mp_arg(3), cmax = _mp_arg(4);
- return val<cmin?cmin:val>cmax?cmax:val;
- }
- static double mp_da_back_or_pop(_cimg_math_parser& mp) {
- const bool is_pop = (bool)mp.opcode[4];
- const char *const s_op = is_pop?"da_pop":"da_back";
- mp_check_list(mp,s_op);
- const unsigned int
- dim = (unsigned int)mp.opcode[2],
- ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
- double *const ptrd = &_mp_arg(1) + (dim>1?1:0);
- CImg<T> &img = mp.imglist[ind];
- int siz = img?(int)img[img._height - 1]:0;
- if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
- "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
- mp.imgout.pixel_type(),s_op,img.width(),img.height(),img.depth(),img.spectrum(),
- img._width==1 && img._depth==1?"":" (contains invalid element counter)");
- if (!siz)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
- "Specified dynamic array #%d contains no elements.",
- mp.imgout.pixel_type(),s_op,(int)_mp_arg(3));
- double ret = cimg::type<double>::nan();
- if (dim<1) ret = img[siz - 1]; // Scalar element
- else cimg_forC(img,c) ptrd[c] = img(0,siz - 1,0,c); // Vector element
- if (is_pop) { // Remove element from array
- --siz;
- if (img.height()>32 && siz<2*img.height()/3) // Reduce size of dynamic array
- img.resize(1,std::max(2*siz + 1,32),1,-100,0);
- img[img._height - 1] = (T)siz;
- }
- return ret;
- }
- static double mp_da_insert_or_push(_cimg_math_parser& mp) {
- const char *const s_op = mp.opcode[3]==~0U?"da_push":"da_insert";
- mp_check_list(mp,s_op);
- const unsigned int
- dim = (unsigned int)mp.opcode[4],
- _dim = std::max(1U,dim),
- nb_elts = (unsigned int)mp.opcode[5] - 6,
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- siz = img?(int)img[img._height - 1]:0,
- pos0 = mp.opcode[3]==~0U?siz:(int)_mp_arg(3),
- pos = pos0<0?pos0 + siz:pos0;
- if (img && _dim!=img._spectrum)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
- "Element to insert has invalid size %u (should be %u).",
- mp.imgout.pixel_type(),s_op,_dim,img._spectrum);
- if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
- "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
- mp.imgout.pixel_type(),s_op,img.width(),img.height(),img.depth(),img.spectrum(),
- img._width==1 && img._depth==1?"":" (contains invalid element counter)");
- if (pos<0 || pos>siz)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function '%s()': "
- "Invalid position %d (not in range -%d...%d).",
- mp.imgout.pixel_type(),s_op,pos0,siz,siz);
- if (siz + nb_elts + 1>=img._height) // Increase size of dynamic array, if necessary
- img.resize(1,2*siz + nb_elts + 1,1,_dim,0);
- if (pos!=siz) // Move existing data in dynamic array
- cimg_forC(img,c) std::memmove(img.data(0,pos + nb_elts,0,c),img.data(0,pos,0,c),(siz - pos)*sizeof(T));
- if (!dim) // Scalar or vector1() elements
- for (unsigned int k = 0; k<nb_elts; ++k) img[pos + k] = (T)_mp_arg(6 + k);
- else // vectorN() elements, with N>1
- for (unsigned int k = 0; k<nb_elts; ++k) {
- double *ptrs = &_mp_arg(6 + k) + 1;
- cimg_forC(img,c) img(0,pos + k,0,c) = ptrs[c];
- }
- img[img._height - 1] = (T)(siz + nb_elts);
- return cimg::type<double>::nan();
- }
- static double mp_da_remove(_cimg_math_parser& mp) {
- mp_check_list(mp,"da_remove");
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- int siz = img?(int)img[img._height - 1]:0;
- if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
- "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
- mp.imgout.pixel_type(),img.width(),img.height(),img.depth(),img.spectrum(),
- img._width==1 && img._depth==1?"":" (contains invalid element counter)");
- if (img._height<2)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
- "Dynamic array is empty.",
- mp.imgout.pixel_type());
- int
- start0 = mp.opcode[3]==~0U?siz - 1:_mp_arg(3),
- end0 = mp.opcode[4]==~0U?start0:_mp_arg(4),
- start = start0<0?start0 + siz:start0,
- end = end0<0?end0 + siz:end0;
- if (start<0 || start>=siz || end<0 || end>=siz || start>end)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
- "Invalid starting (%d) and ending (%d) positions "
- "(not ordered, in range -%d...%d).",
- mp.imgout.pixel_type(),start0,end0,siz,siz - 1);
- if (end<siz - 1) // Move remaining data in dynamic array
- cimg_forC(img,c) std::memmove(img.data(0,start,0,c),img.data(0,end + 1,0,c),(siz - 1 - end)*sizeof(T));
- siz-=end - start + 1;
- if (img.height()>32 && siz<2*img.height()/3) // Reduce size of dynamic array
- img.resize(1,std::max(2*siz + 1,32),1,-100,0);
- img[img._height - 1] = (T)siz;
- return cimg::type<double>::nan();
- }
- static double mp_da_size(_cimg_math_parser& mp) {
- mp_check_list(mp,"da_size");
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int siz = img?(int)img[img._height - 1]:0;
- if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_size()': "
- "Specified image (%d,%d,%d,%d) cannot be used as dynamic array%s.",
- mp.imgout.pixel_type(),img.width(),img.height(),img.depth(),img.spectrum(),
- img._width==1 && img._depth==1?"":" (contains invalid element counter)");
- return siz;
- }
- static double mp_date(_cimg_math_parser& mp) {
- const unsigned int
- siz_out = (unsigned int)mp.opcode[2],
- siz_arg1 = (unsigned int)mp.opcode[4],
- siz_arg2 = (unsigned int)mp.opcode[6];
- double *ptr_out = &_mp_arg(1) + (siz_out?1:0);
- const double
- *ptr_arg1 = siz_arg1==~0U?0:&_mp_arg(3) + (siz_arg1?1:0),
- *ptr_arg2 = siz_arg2==~0U?0:&_mp_arg(5) + 1;
- if (!ptr_arg2) { // No filename specified
- if (!siz_arg1) return cimg::date((unsigned int)*ptr_arg1);
- if (siz_arg1==~0U) for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = k;
- else for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = ptr_arg1[k];
- cimg::date(ptr_out,siz_out);
- return cimg::type<double>::nan();
- }
- // Filename specified.
- CImg<charT> ss(siz_arg2 + 1);
- cimg_forX(ss,i) ss[i] = (char)ptr_arg2[i];
- ss.back() = 0;
- if (!siz_arg1) return cimg::fdate(ss,(unsigned int)*ptr_arg1);
- for (unsigned int k = 0; k<siz_out; ++k) ptr_out[k] = ptr_arg1[k];
- cimg::fdate(ss,ptr_out,siz_out);
- return cimg::type<double>::nan();
- }
- static double mp_debug(_cimg_math_parser& mp) {
- CImg<charT> expr(mp.opcode[2] - 4);
- {
- const ulongT *ptrs = mp.opcode._data + 4;
- cimg_for(expr,ptrd,char) *ptrd = (char)*(ptrs++);
- }
- cimg::strellipsize(expr);
- const ulongT g_target = mp.opcode[1];
- #if cimg_use_openmp==0
- const unsigned int n_thread = 0;
- #else
- const unsigned int n_thread = omp_get_thread_num();
- #endif
- cimg_pragma_openmp(critical(mp_debug))
- {
- std::fprintf(cimg::output(),
- "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
- "Start debugging expression '%s', code length %u -> mem[%u] (memsize: %u)",
- (void*)&mp,n_thread,mp.debug_indent,' ',
- expr._data,(unsigned int)mp.opcode[3],(unsigned int)g_target,mp.mem._width);
- std::fflush(cimg::output());
- mp.debug_indent+=3;
- }
- const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[3];
- CImg<ulongT> _op;
- for ( ; mp.p_code<p_end; ++mp.p_code) {
- const CImg<ulongT> &op = *mp.p_code;
- mp.opcode._data = op._data;
- _op.assign(1,op._height - 1);
- const ulongT *ptrs = op._data + 1;
- for (ulongT *ptrd = _op._data, *const ptrde = _op._data + _op._height; ptrd<ptrde; ++ptrd)
- *ptrd = *(ptrs++);
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- cimg_pragma_openmp(critical(mp_debug))
- {
- std::fprintf(cimg::output(),
- "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
- "Opcode %p = [ %p,%s ] -> mem[%u] = %.17g",
- (void*)&mp,n_thread,mp.debug_indent,' ',
- (void*)mp.opcode._data,(void*)*mp.opcode,_op.value_string().data(),
- (unsigned int)target,mp.mem[target]);
- std::fflush(cimg::output());
- }
- }
- cimg_pragma_openmp(critical(mp_debug))
- {
- mp.debug_indent-=3;
- std::fprintf(cimg::output(),
- "\n[" cimg_appname "_math_parser] %p[thread #%u]:%*c"
- "End debugging expression '%s' -> mem[%u] = %.17g (memsize: %u)",
- (void*)&mp,n_thread,mp.debug_indent,' ',
- expr._data,(unsigned int)g_target,mp.mem[g_target],mp.mem._width);
- std::fflush(cimg::output());
- }
- --mp.p_code;
- return mp.mem[g_target];
- }
- static double mp_decrement(_cimg_math_parser& mp) {
- return _mp_arg(2) - 1;
- }
- static double mp_deg2rad(_cimg_math_parser& mp) {
- return _mp_arg(2)*cimg::PI/180;
- }
- static double mp_det(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(2) + 1;
- const unsigned int k = (unsigned int)mp.opcode[3];
- return CImg<doubleT>(ptrs,k,k,1,1,true).det();
- }
- static double mp_diag(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2], siz = mp.opcode[2] - 3;
- double *ptrd = &_mp_arg(1) + 1;
- std::memset(ptrd,0,siz*siz*sizeof(double));
- for (unsigned int i = 3; i<i_end; ++i) { *(ptrd++) = _mp_arg(i); ptrd+=siz; }
- return cimg::type<double>::nan();
- }
- static double mp_display_memory(_cimg_math_parser& mp) {
- cimg::unused(mp);
- std::fputc('\n',cimg::output());
- CImg<charT> title(128);
- cimg_snprintf(title,title._width,"%s (%u)","[" cimg_appname "_math_parser] Memory snapshot",mp.mem._width);
- mp.mem.display(title);
- return cimg::type<double>::nan();
- }
- static double mp_display(_cimg_math_parser& mp) {
- const unsigned int
- _siz = (unsigned int)mp.opcode[3],
- siz = _siz?_siz:1;
- const double *const ptr = &_mp_arg(1) + (_siz?1:0);
- const int
- w = (int)_mp_arg(4),
- h = (int)_mp_arg(5),
- d = (int)_mp_arg(6),
- s = (int)_mp_arg(7);
- CImg<doubleT> img;
- if (w>0 && h>0 && d>0 && s>0) {
- if ((unsigned int)w*h*d*s<=siz) img.assign(ptr,w,h,d,s,true);
- else img.assign(ptr,siz).resize(w,h,d,s,-1);
- } else img.assign(ptr,1,siz,1,1,true);
- CImg<charT> expr(mp.opcode[2] - 8);
- const ulongT *ptrs = mp.opcode._data + 8;
- cimg_for(expr,ptrd,char) *ptrd = (char)*(ptrs++);
- ((CImg<charT>::string("[" cimg_appname "_math_parser] ",false,true),expr)>'x').move_to(expr);
- cimg::strellipsize(expr);
- std::fputc('\n',cimg::output());
- img.display(expr._data);
- return cimg::type<double>::nan();
- }
- static double mp_div(_cimg_math_parser& mp) {
- return _mp_arg(2)/_mp_arg(3);
- }
- static double mp_dot(_cimg_math_parser& mp) {
- const unsigned int siz = (unsigned int)mp.opcode[4];
- return CImg<doubleT>(&_mp_arg(2) + 1,1,siz,1,1,true).
- dot(CImg<doubleT>(&_mp_arg(3) + 1,1,siz,1,1,true));
- }
- static double mp_do(_cimg_math_parser& mp) {
- const ulongT
- mem_body = mp.opcode[1],
- mem_cond = mp.opcode[2];
- const CImg<ulongT>
- *const p_body = ++mp.p_code,
- *const p_cond = p_body + mp.opcode[3],
- *const p_end = p_cond + mp.opcode[4];
- const unsigned int vsiz = (unsigned int)mp.opcode[5];
- if (mp.opcode[6]) { // Set default value for result and condition if necessary
- if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
- else mp.mem[mem_body] = cimg::type<double>::nan();
- }
- if (mp.opcode[7]) mp.mem[mem_cond] = 0;
- const unsigned int _break_type = mp.break_type;
- mp.break_type = 0;
- do {
- for (mp.p_code = p_body; mp.p_code<p_cond; ++mp.p_code) { // Evaluate body
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- for (mp.p_code = p_cond; mp.p_code<p_end; ++mp.p_code) { // Evaluate condition
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- } while (mp.mem[mem_cond]);
- mp.break_type = _break_type;
- mp.p_code = p_end - 1;
- return mp.mem[mem_body];
- }
- static double mp_draw(_cimg_math_parser& mp) {
- const int x = (int)_mp_arg(4), y = (int)_mp_arg(5), z = (int)_mp_arg(6), c = (int)_mp_arg(7);
- unsigned int ind = (unsigned int)mp.opcode[3];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
- }
- CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- unsigned int
- dx = (unsigned int)mp.opcode[8],
- dy = (unsigned int)mp.opcode[9],
- dz = (unsigned int)mp.opcode[10],
- dc = (unsigned int)mp.opcode[11];
- dx = dx==~0U?img._width:(unsigned int)_mp_arg(8);
- dy = dy==~0U?img._height:(unsigned int)_mp_arg(9);
- dz = dz==~0U?img._depth:(unsigned int)_mp_arg(10);
- dc = dc==~0U?img._spectrum:(unsigned int)_mp_arg(11);
- const ulongT sizS = mp.opcode[2];
- if (sizS<(ulongT)dx*dy*dz*dc)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'draw()': "
- "Sprite dimension (%lu values) and specified sprite geometry (%u,%u,%u,%u) "
- "(%lu values) do not match.",
- mp.imgin.pixel_type(),sizS,dx,dy,dz,dc,(ulongT)dx*dy*dz*dc);
- CImg<doubleT> S(&_mp_arg(1) + 1,dx,dy,dz,dc,true);
- const float opacity = (float)_mp_arg(12);
- if (img._data) {
- if (mp.opcode[13]!=~0U) { // Opacity mask specified
- const ulongT sizM = mp.opcode[14];
- if (sizM<(ulongT)dx*dy*dz)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'draw()': "
- "Mask dimension (%lu values) and specified sprite geometry (%u,%u,%u,%u) "
- "(%lu values) do not match.",
- mp.imgin.pixel_type(),sizS,dx,dy,dz,dc,(ulongT)dx*dy*dz*dc);
- const CImg<doubleT> M(&_mp_arg(13) + 1,dx,dy,dz,(unsigned int)(sizM/(dx*dy*dz)),true);
- img.draw_image(x,y,z,c,S,M,opacity,(float)_mp_arg(15));
- } else img.draw_image(x,y,z,c,S,opacity);
- }
- return cimg::type<double>::nan();
- }
- static double mp_echo(_cimg_math_parser& mp) {
- const unsigned int nb_args = (unsigned int)(mp.opcode[2] - 3)/2;
- if (!nb_args) { std::fputc('\n',cimg::output()); return cimg::type<double>::nan(); } // No arguments
- CImgList<charT> _str;
- CImg<charT> it;
- for (unsigned int n = 0; n<nb_args; ++n) {
- const unsigned int siz = (unsigned int)mp.opcode[4 + 2*n];
- if (siz) { // Vector argument -> string
- const double *ptr = &_mp_arg(3 + 2*n) + 1;
- unsigned int l = 0;
- while (l<siz && ptr[l]) ++l;
- CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
- } else { // Scalar argument -> number
- it.assign(24);
- cimg_snprintf(it,it._width,"%.17g",_mp_arg(3 + 2*n));
- CImg<charT>::string(it,false,true).move_to(_str);
- }
- }
- CImg(1,1,1,1,0).move_to(_str);
- const CImg<charT> str = _str>'x';
- std::fprintf(cimg::output(),"\n%s",str._data);
- return cimg::type<double>::nan();
- }
- static double mp_ellipse(_cimg_math_parser& mp) {
- mp_check_list(mp,"ellipse");
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- unsigned int ind = (unsigned int)mp.opcode[3];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
- }
- CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- CImg<T> color(img._spectrum,1,1,1,0);
- bool is_invalid_arguments = false, is_outlined = false;
- float r1 = 0, r2 = 0, angle = 0, opacity = 1;
- unsigned int i = 4, pattern = ~0U;
- int x0 = 0, y0 = 0;
- if (i>=i_end) is_invalid_arguments = true;
- else {
- x0 = (int)cimg::round(_mp_arg(i++));
- if (i>=i_end) is_invalid_arguments = true;
- else {
- y0 = (int)cimg::round(_mp_arg(i++));
- if (i>=i_end) is_invalid_arguments = true;
- else {
- r1 = (float)_mp_arg(i++);
- if (i>=i_end) r2 = r1;
- else {
- r2 = (float)_mp_arg(i++);
- if (i<i_end) {
- angle = (float)(_mp_arg(i++)*180/cimg::PI);
- if (i<i_end) {
- opacity = (float)_mp_arg(i++);
- if (r1<0 && r2<0) {
- pattern = (unsigned int)_mp_arg(i++);
- is_outlined = true;
- r1 = -r1; r2 = -r2;
- }
- if (i<i_end) {
- cimg_forX(color,k) if (i<i_end) color[k] = (T)_mp_arg(i++);
- else { color.resize(k,1,1,1,-1); break; }
- color.resize(img._spectrum,1,1,1,0,2);
- }
- }
- }
- }
- }
- }
- }
- if (!is_invalid_arguments) {
- if (is_outlined) img.draw_ellipse(x0,y0,r1,r2,angle,color._data,opacity,pattern);
- else img.draw_ellipse(x0,y0,r1,r2,angle,color._data,opacity);
- } else {
- CImg<doubleT> args(i_end - 4);
- cimg_forX(args,k) args[k] = _mp_arg(4 + k);
- if (ind==~0U)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'ellipse()': "
- "Invalid arguments '%s'. ",
- mp.imgin.pixel_type(),args.value_string()._data);
- else
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'ellipse()': "
- "Invalid arguments '#%u%s%s'. ",
- mp.imgin.pixel_type(),ind,args._width?",":"",args.value_string()._data);
- }
- return cimg::type<double>::nan();
- }
- static double mp_eq(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)==_mp_arg(3));
- }
- static double mp_erf(_cimg_math_parser& mp) {
- return std::erf(_mp_arg(2));
- }
- static double mp_erfinv(_cimg_math_parser& mp) {
- return cimg::erfinv(_mp_arg(2));
- }
- static double mp_exp(_cimg_math_parser& mp) {
- return std::exp(_mp_arg(2));
- }
- static double mp_expr(_cimg_math_parser& mp) {
- const unsigned int
- sizs = (unsigned int)mp.opcode[3],
- w = (unsigned int)mp.opcode[4],
- h = (unsigned int)mp.opcode[5],
- d = (unsigned int)mp.opcode[6],
- s = (unsigned int)mp.opcode[7],
- sizd = w*h*d*s;
- const double *ptrs = &_mp_arg(2) + 1;
- double *ptrd = &_mp_arg(1);
- CImg<charT> ss(sizs + 1);
- cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- if (!sizd) return CImg<T>(w,h,d,s,0).eval(ss,0,0,0,0,&mp.imglist); // Scalar result
- CImg<doubleT>(++ptrd,w,h,d,s,true) = CImg<T>(w,h,d,s,0).fill(ss,true,true,&mp.imglist);
- return cimg::type<double>::nan();
- }
- static double mp_eye(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int k = (unsigned int)mp.opcode[2];
- CImg<doubleT>(ptrd,k,k,1,1,true).identity_matrix();
- return cimg::type<double>::nan();
- }
- static double mp_f2ui(_cimg_math_parser& mp) {
- return (double)cimg::float2uint((float)_mp_arg(2));
- }
- static double mp_factorial(_cimg_math_parser& mp) {
- return cimg::factorial((int)_mp_arg(2));
- }
- static double mp_fibonacci(_cimg_math_parser& mp) {
- return cimg::fibonacci((int)_mp_arg(2));
- }
- static double mp_fill(_cimg_math_parser& mp) {
- unsigned int siz = (unsigned int)mp.opcode[2];
- double
- *ptrd = &_mp_arg(1),
- *const ptrc = mp.opcode[3]!=~0U?&_mp_arg(3):0,
- *const ptrs = &_mp_arg(4);
- if (siz) ++ptrd; else ++siz; // Fill vector-valued slot
- const CImg<ulongT>
- *const p_body = ++mp.p_code,
- *const p_end = p_body + mp.opcode[5];
- const unsigned int _break_type = mp.break_type;
- mp.break_type = 0;
- unsigned int it = 0;
- if (ptrc) { // Version with loop variable (3 arguments)
- while (it<siz) {
- *ptrc = (double)it;
- for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- else ptrd[it] = *ptrs;
- ++it;
- }
- *ptrc = (double)it;
- } else // Version without loop variable (2 arguments)
- while (it<siz) {
- for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- else ptrd[it] = *ptrs;
- ++it;
- }
- mp.break_type = _break_type;
- mp.p_code = p_end - 1;
- return *ptrd;
- }
- static double mp_find(_cimg_math_parser& mp) {
- const int _step = (int)_mp_arg(6), step = _step?_step:-1;
- const ulongT siz = (ulongT)mp.opcode[3];
- longT ind = (longT)(mp.opcode[5]!=_cimg_mp_slot_nan?_mp_arg(5):step>0?0:siz - 1);
- if (ind<0 || ind>=(longT)siz) return -1.;
- const double
- *const ptrb = &_mp_arg(2) + 1,
- *const ptre = ptrb + siz,
- val = _mp_arg(4),
- *ptr = ptrb + ind;
- // Forward search
- if (step>0) {
- while (ptr<ptre && *ptr!=val) ptr+=step;
- return ptr>=ptre?-1.:(double)(ptr - ptrb);
- }
- // Backward search.
- while (ptr>=ptrb && *ptr!=val) ptr+=step;
- return ptr<ptrb?-1.:(double)(ptr - ptrb);
- }
- static double mp_find_seq(_cimg_math_parser& mp) {
- const int _step = (int)_mp_arg(7), step = _step?_step:-1;
- const ulongT
- siz1 = (ulongT)mp.opcode[3],
- siz2 = (ulongT)mp.opcode[5];
- longT ind = (longT)(mp.opcode[6]!=_cimg_mp_slot_nan?_mp_arg(6):step>0?0:siz1 - 1);
- if (ind<0 || ind>=(longT)siz1) return -1.;
- const double
- *const ptr1b = &_mp_arg(2) + 1,
- *const ptr1e = ptr1b + siz1,
- *const ptr2b = &_mp_arg(4) + 1,
- *const ptr2e = ptr2b + siz2,
- *ptr1 = ptr1b + ind,
- *p1 = 0,
- *p2 = 0;
- // Forward search.
- if (step>0) {
- do {
- while (ptr1<ptr1e && *ptr1!=*ptr2b) ptr1+=step;
- if (ptr1>=ptr1e) return -1.;
- p1 = ptr1 + 1;
- p2 = ptr2b + 1;
- while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
- } while (p2<ptr2e && (ptr1+=step)<ptr1e);
- return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
- }
- // Backward search.
- do {
- while (ptr1>=ptr1b && *ptr1!=*ptr2b) ptr1+=step;
- if (ptr1<ptr1b) return -1.;
- p1 = ptr1 + 1;
- p2 = ptr2b + 1;
- while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
- } while (p2<ptr2e && (ptr1+=step)>=ptr1b);
- return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
- }
- static double mp_floor(_cimg_math_parser& mp) {
- return std::floor(_mp_arg(2));
- }
- static double mp_for(_cimg_math_parser& mp) {
- const ulongT
- mem_body = mp.opcode[1],
- mem_cond = mp.opcode[3];
- const CImg<ulongT>
- *const p_init = ++mp.p_code,
- *const p_cond = p_init + mp.opcode[4],
- *const p_body = p_cond + mp.opcode[5],
- *const p_post = p_body + mp.opcode[6],
- *const p_end = p_post + mp.opcode[7];
- const unsigned int vsiz = (unsigned int)mp.opcode[2];
- bool is_cond = false;
- if (mp.opcode[8]) { // Set default value for result and condition if necessary
- if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
- else mp.mem[mem_body] = cimg::type<double>::nan();
- }
- if (mp.opcode[9]) mp.mem[mem_cond] = 0;
- const unsigned int _break_type = mp.break_type;
- mp.break_type = 0;
- for (mp.p_code = p_init; mp.p_code<p_cond; ++mp.p_code) { // Evaluate init
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (!mp.break_type) do {
- for (mp.p_code = p_cond; mp.p_code<p_body; ++mp.p_code) { // Evaluate condition
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break;
- is_cond = (bool)mp.mem[mem_cond];
- if (is_cond && !mp.break_type) {
- for (mp.p_code = p_body; mp.p_code<p_post; ++mp.p_code) { // Evaluate body
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- for (mp.p_code = p_post; mp.p_code<p_end; ++mp.p_code) { // Evaluate post-code
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- }
- } while (is_cond);
- mp.break_type = _break_type;
- mp.p_code = p_end - 1;
- return mp.mem[mem_body];
- }
- static double mp_fsize(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(2) + 1;
- const ulongT siz = (ulongT)mp.opcode[3];
- CImg<charT> ss(siz + 1);
- cimg_forX(ss,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- return (double)cimg::fsize(ss);
- }
- static double mp_g(_cimg_math_parser& mp) {
- cimg::unused(mp);
- return cimg::grand(&mp.rng);
- }
- static double mp_gauss(_cimg_math_parser& mp) {
- const double x = _mp_arg(2), s = _mp_arg(3);
- return std::exp(-x*x/(2*s*s))/(_mp_arg(4)?std::sqrt(2*s*s*cimg::PI):1);
- }
- #ifdef cimg_mp_func_get
- static double mp_get(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(2) + 1;
- double *ptrd = &_mp_arg(1);
- const unsigned int
- sizs = (unsigned int)mp.opcode[3],
- sizd = (unsigned int)mp.opcode[4];
- const bool to_string = (bool)mp.opcode[5];
- CImg<charT> ss(sizs + 1);
- cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- if (sizd) cimg_mp_func_get(ptrd + 1,sizd,to_string,ss._data);
- else cimg_mp_func_get(ptrd,0,to_string,ss._data);
- return cimg::type<double>::nan();
- }
- #endif
- static double mp_gcd(_cimg_math_parser& mp) {
- return cimg::gcd((long)_mp_arg(2),(long)_mp_arg(3));
- }
- #ifdef cimg_mp_func_name
- static double mp_name(_cimg_math_parser& mp) {
- double *const ptr = &_mp_arg(1) + 1;
- const unsigned int siz = (unsigned int)mp.opcode[3];
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind==~0U) std::memset(ptr,0,siz*sizeof(double));
- else {
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- cimg_mp_func_name(ind,ptr,siz);
- }
- return cimg::type<double>::nan();
- }
- #endif
- static double mp_gt(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)>_mp_arg(3));
- }
- static double mp_gte(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)>=_mp_arg(3));
- }
- static double mp_i(_cimg_math_parser& mp) {
- return (double)mp.imgin.atXYZC((int)mp.mem[_cimg_mp_slot_x],(int)mp.mem[_cimg_mp_slot_y],
- (int)mp.mem[_cimg_mp_slot_z],(int)mp.mem[_cimg_mp_slot_c],(T)0);
- }
- static double mp_if(_cimg_math_parser& mp) {
- const bool is_cond = (bool)_mp_arg(2);
- const ulongT
- mem_left = mp.opcode[3],
- mem_right = mp.opcode[4];
- const CImg<ulongT>
- *const p_right = ++mp.p_code + mp.opcode[5],
- *const p_end = p_right + mp.opcode[6];
- const unsigned int vtarget = (unsigned int)mp.opcode[1], vsiz = (unsigned int)mp.opcode[7];
- if (is_cond) for ( ; mp.p_code<p_right; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- else for (mp.p_code = p_right; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.p_code==mp.p_break) --mp.p_code;
- else mp.p_code = p_end - 1;
- if (vsiz) std::memcpy(&mp.mem[vtarget] + 1,&mp.mem[is_cond?mem_left:mem_right] + 1,sizeof(double)*vsiz);
- return mp.mem[is_cond?mem_left:mem_right];
- }
- static double mp_image_d(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.depth();
- }
- static double mp_image_display(_cimg_math_parser& mp) {
- mp_check_list(mp,"display");
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- cimg::mutex(6);
- CImg<T> &img = mp.imglist[ind];
- CImg<charT> title(256);
- std::fputc('\n',cimg::output());
- cimg_snprintf(title,title._width,"[ Image #%u ]",ind);
- img.display(title);
- cimg::mutex(6,0);
- return cimg::type<double>::nan();
- }
- static double mp_image_h(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.height();
- }
- static double mp_image_median(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.median();
- }
- static double mp_image_norm(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.magnitude();
- }
- static double mp_image_print(_cimg_math_parser& mp) {
- mp_check_list(mp,"print");
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- cimg::mutex(6);
- CImg<T> &img = mp.imglist[ind];
- CImg<charT> title(256);
- std::fputc('\n',cimg::output());
- cimg_snprintf(title,title._width,"[ Image #%u ]",ind);
- img.print(title);
- cimg::mutex(6,0);
- return cimg::type<double>::nan();
- }
- static double mp_image_resize(_cimg_math_parser& mp) {
- mp_check_list(mp,"resize");
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- cimg::mutex(6);
- CImg<T> &img = mp.imglist[ind];
- const double
- _w = mp.opcode[3]==~0U?-100:_mp_arg(3),
- _h = mp.opcode[4]==~0U?-100:_mp_arg(4),
- _d = mp.opcode[5]==~0U?-100:_mp_arg(5),
- _s = mp.opcode[6]==~0U?-100:_mp_arg(6);
- const unsigned int
- w = (unsigned int)(_w>=0?_w:-_w*img.width()/100),
- h = (unsigned int)(_h>=0?_h:-_h*img.height()/100),
- d = (unsigned int)(_d>=0?_d:-_d*img.depth()/100),
- s = (unsigned int)(_s>=0?_s:-_s*img.spectrum()/100),
- interp = (int)_mp_arg(7);
- if (mp.is_fill && img._data==mp.imgout._data) {
- cimg::mutex(6,0);
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'resize()': "
- "Cannot both fill and resize image (%u,%u,%u,%u) "
- "to new dimensions (%u,%u,%u,%u).",
- img.pixel_type(),img._width,img._height,img._depth,img._spectrum,w,h,d,s);
- }
- const unsigned int
- boundary = (int)_mp_arg(8);
- const float
- cx = (float)_mp_arg(9),
- cy = (float)_mp_arg(10),
- cz = (float)_mp_arg(11),
- cc = (float)_mp_arg(12);
- img.resize(w,h,d,s,interp,boundary,cx,cy,cz,cc);
- cimg::mutex(6,0);
- return cimg::type<double>::nan();
- }
- static double mp_image_s(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.spectrum();
- }
- static double mp_image_sort(_cimg_math_parser& mp) {
- mp_check_list(mp,"sort");
- const bool is_increasing = (bool)_mp_arg(3);
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- axis = (unsigned int)_mp_arg(4);
- cimg::mutex(6);
- CImg<T> &img = mp.imglist[ind];
- img.sort(is_increasing,
- axis==0 || axis=='x'?'x':
- axis==1 || axis=='y'?'y':
- axis==2 || axis=='z'?'z':
- axis==3 || axis=='c'?'c':0);
- cimg::mutex(6,0);
- return cimg::type<double>::nan();
- }
- static double mp_image_stats(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind==~0U)
- CImg<doubleT>(ptrd,14,1,1,1,true) = mp.imgout.get_stats();
- else {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<doubleT>(ptrd,14,1,1,1,true) = mp.imglist[ind].get_stats();
- }
- return cimg::type<double>::nan();
- }
- static double mp_image_w(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.width();
- }
- static double mp_image_wh(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.width()*img.height();
- }
- static double mp_image_whd(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.width()*img.height()*img.depth();
- }
- static double mp_image_whds(_cimg_math_parser& mp) {
- unsigned int ind = (unsigned int)mp.opcode[2];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- }
- const CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- return (double)img.width()*img.height()*img.depth()*img.spectrum();
- }
- static double mp_increment(_cimg_math_parser& mp) {
- return _mp_arg(2) + 1;
- }
- static double mp_inrange(_cimg_math_parser& mp) {
- const unsigned int sizd = (unsigned int)mp.opcode[2];
- const bool
- include_m = (bool)_mp_arg(9),
- include_M = (bool)_mp_arg(10);
- if (!sizd) { // Scalar result
- const double val = _mp_arg(3);
- const double m = _mp_arg(5), M = _mp_arg(7);
- if (M>=m) return (double)((include_m?(val>=m):(val>m)) && (include_M?(val<=M):(val<M)));
- else return (double)((include_M?(val>=M):(val>M)) && (include_m?(val<=m):(val<m)));
- }
- // Vector result
- const unsigned int
- siz1 = (unsigned int)mp.opcode[4],
- siz2 = (unsigned int)mp.opcode[6],
- siz3 = (unsigned int)mp.opcode[8],
- off1 = siz1?1:0,
- off2 = siz2?1:0,
- off3 = siz3?1:0;
- double *ptrd = &_mp_arg(1) + 1;
- const double
- *ptr1 = &_mp_arg(3) + off1,
- *ptr2 = &_mp_arg(5) + off2,
- *ptr3 = &_mp_arg(7) + off3;
- for (unsigned int k = 0; k<sizd; ++k) {
- const double val = *ptr1;
- const double m = *ptr2, M = *ptr3;
- if (M>=m)
- ptrd[k] = (double)((include_m?(val>=m):(val>m)) && (include_M?(val<=M):(val<M)));
- else
- ptrd[k] = (double)((include_M?(val>=M):(val>M)) && (include_m?(val<=m):(val<m)));
- ptr1+=off1;
- ptr2+=off2;
- ptr3+=off3;
- }
- return cimg::type<double>::nan();
- }
- static double mp_int(_cimg_math_parser& mp) {
- return (double)(longT)_mp_arg(2);
- }
- static double mp_ioff(_cimg_math_parser& mp) {
- const unsigned int
- boundary_conditions = (unsigned int)_mp_arg(3);
- const CImg<T> &img = mp.imgin;
- const longT
- off = (longT)_mp_arg(2),
- whds = (longT)img.size();
- if (off>=0 && off<whds) return (double)img[off];
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
- return (double)img[moff<whds?moff:whds2 - moff - 1];
- }
- case 2 : // Periodic
- return (double)img[cimg::mod(off,whds)];
- case 1 : // Neumann
- return (double)img[off<0?0:whds - 1];
- default : // Dirichlet
- return 0;
- }
- return 0;
- }
- static double mp_isbool(_cimg_math_parser& mp) {
- const double val = _mp_arg(2);
- return (double)(val==0. || val==1.);
- }
- static double mp_isdir(_cimg_math_parser& mp) {
- const unsigned int siz = (unsigned int)mp.opcode[3];
- const double *ptrs = &_mp_arg(2) + (siz?1:0);
- if (!siz) { char str[2] = { 0 }; *str = *ptrs; return (double)cimg::is_directory(str); }
- CImg<charT> ss(siz + 1);
- cimg_forX(ss,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- return (double)cimg::is_directory(ss);
- }
- static double mp_isin(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- const double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i)
- if (val==_mp_arg(i)) return 1.;
- return 0.;
- }
- static double mp_isinf(_cimg_math_parser& mp) {
- return (double)cimg::type<double>::is_inf(_mp_arg(2));
- }
- static double mp_isint(_cimg_math_parser& mp) {
- return (double)((double)(longT)_mp_arg(2)==_mp_arg(2));
- }
- static double mp_isfile(_cimg_math_parser& mp) {
- const unsigned int siz = (unsigned int)mp.opcode[3];
- const double *ptrs = &_mp_arg(2) + (siz?1:0);
- if (!siz) { char str[2] = { 0 }; *str = *ptrs; return (double)cimg::is_file(str); }
- CImg<charT> ss(siz + 1);
- cimg_forX(ss,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- return (double)cimg::is_file(ss);
- }
- static double mp_isnan(_cimg_math_parser& mp) {
- return (double)cimg::type<double>::is_nan(_mp_arg(2));
- }
- static double mp_isvarname(_cimg_math_parser& mp) {
- const unsigned int siz = (unsigned int)mp.opcode[3];
- const double *ptrs = &_mp_arg(2) + (siz?1:0);
- if (!siz) {
- const char c = (char)*ptrs;
- return (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_';
- }
- if (*ptrs>='0' && *ptrs<='9') return 0;
- for (unsigned int k = 0; k<siz; ++k) if (!is_varchar((char)ptrs[k])) return 0;
- return 1;
- }
- static double mp_ixyzc(_cimg_math_parser& mp) {
- const unsigned int
- interpolation = (unsigned int)_mp_arg(6),
- boundary_conditions = (unsigned int)_mp_arg(7);
- const CImg<T> &img = mp.imgin;
- const double
- x = _mp_arg(2), y = _mp_arg(3),
- z = _mp_arg(4), c = _mp_arg(5);
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
- mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
- return (double)img(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- mc<img.spectrum()?mc:s2 - mc - 1);
- }
- case 2 : // Periodic
- return (double)img((int)cimg::mod(x,(double)img._width),
- (int)cimg::mod(y,(double)img._height),
- (int)cimg::mod(z,(double)img._depth),
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
- default : // Dirichlet
- return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
- }
- }
- }
- static double mp_joff(_cimg_math_parser& mp) {
- const unsigned int
- boundary_conditions = (unsigned int)_mp_arg(3);
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const CImg<T> &img = mp.imgin;
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
- whds = (longT)img.size();
- if (off>=0 && off<whds) return (double)img[off];
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
- return (double)img[moff<whds?moff:whds2 - moff - 1];
- }
- case 2 : // Periodic
- return (double)img[cimg::mod(off,whds)];
- case 1 : // Neumann
- return (double)img[off<0?0:whds - 1];
- default : // Dirichlet
- return 0;
- }
- return 0;
- }
- static double mp_jxyzc(_cimg_math_parser& mp) {
- const unsigned int
- interpolation = (unsigned int)_mp_arg(6),
- boundary_conditions = (unsigned int)_mp_arg(7);
- const CImg<T> &img = mp.imgin;
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
- oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c],
- x = ox + _mp_arg(2), y = oy + _mp_arg(3),
- z = oz + _mp_arg(4), c = oc + _mp_arg(5);
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
- mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
- return (double)img(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- mc<img.spectrum()?mc:s2 - mc - 1);
- }
- case 2 : // Periodic
- return (double)img((int)cimg::mod(x,(double)img._width),
- (int)cimg::mod(y,(double)img._height),
- (int)cimg::mod(z,(double)img._depth),
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
- default : // Dirichlet
- return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
- }
- }
- }
- static double mp_kth(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- CImg<doubleT> vals(i_end - 4);
- double *p = vals.data();
- for (unsigned int i = 4; i<i_end; ++i) *(p++) = _mp_arg(i);
- longT ind = (longT)cimg::round(_mp_arg(3));
- if (ind<0) ind+=vals.width() + 1;
- ind = cimg::cut(ind,(longT)1,(longT)vals.width());
- return vals.kth_smallest((ulongT)(ind - 1));
- }
- static double mp_lerp(_cimg_math_parser& mp) {
- const double t = _mp_arg(4);
- return _mp_arg(2)*(1-t) + _mp_arg(3)*t;
- }
- static double mp_linear_add(_cimg_math_parser& mp) {
- return _mp_arg(2)*_mp_arg(3) + _mp_arg(4);
- }
- static double mp_linear_sub_left(_cimg_math_parser& mp) {
- return _mp_arg(2)*_mp_arg(3) - _mp_arg(4);
- }
- static double mp_linear_sub_right(_cimg_math_parser& mp) {
- return _mp_arg(4) - _mp_arg(2)*_mp_arg(3);
- }
- static double mp_list_depth(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._depth;
- }
- static double mp_list_find(_cimg_math_parser& mp) {
- const unsigned int
- indi = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- const CImg<T> &img = mp.imglist[indi];
- const int _step = (int)_mp_arg(5), step = _step?_step:-1;
- const ulongT siz = (ulongT)img.size();
- longT ind = (longT)(mp.opcode[4]!=_cimg_mp_slot_nan?_mp_arg(4):step>0?0:siz - 1);
- if (ind<0 || ind>=(longT)siz) return -1.;
- const T
- *const ptrb = img.data(),
- *const ptre = img.end(),
- *ptr = ptrb + ind;
- const double val = _mp_arg(3);
- // Forward search
- if (step>0) {
- while (ptr<ptre && (double)*ptr!=val) ptr+=step;
- return ptr>=ptre?-1.:(double)(ptr - ptrb);
- }
- // Backward search.
- while (ptr>=ptrb && (double)*ptr!=val) ptr+=step;
- return ptr<ptrb?-1.:(double)(ptr - ptrb);
- }
- static double mp_list_find_seq(_cimg_math_parser& mp) {
- const unsigned int
- indi = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- const CImg<T> &img = mp.imglist[indi];
- const int _step = (bool)_mp_arg(6), step = _step?_step:-1;
- const ulongT
- siz1 = (ulongT)img.size(),
- siz2 = (ulongT)mp.opcode[4];
- longT ind = (longT)(mp.opcode[5]!=_cimg_mp_slot_nan?_mp_arg(5):step>0?0:siz1 - 1);
- if (ind<0 || ind>=(longT)siz1) return -1.;
- const T
- *const ptr1b = img.data(),
- *const ptr1e = ptr1b + siz1,
- *ptr1 = ptr1b + ind,
- *p1 = 0;
- const double
- *const ptr2b = &_mp_arg(3) + 1,
- *const ptr2e = ptr2b + siz2,
- *p2 = 0;
- // Forward search.
- if (step>0) {
- do {
- while (ptr1<ptr1e && *ptr1!=*ptr2b) ptr1+=step;
- if (ptr1>=ptr1e) return -1.;
- p1 = ptr1 + 1;
- p2 = ptr2b + 1;
- while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
- } while (p2<ptr2e && (ptr1+=step)<ptr1e);
- return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
- }
- // Backward search.
- do {
- while (ptr1>=ptr1b && *ptr1!=*ptr2b) ptr1+=step;
- if (ptr1<ptr1b) return -1.;
- p1 = ptr1 + 1;
- p2 = ptr2b + 1;
- while (p1<ptr1e && p2<ptr2e && *p1==*p2) { ++p1; ++p2; }
- } while (p2<ptr2e && (ptr1+=step)>=ptr1b);
- return p2<ptr2e?-1.:(double)(ptr1 - ptr1b);
- }
- static double mp_list_height(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._height;
- }
- static double mp_list_ioff(_cimg_math_parser& mp) {
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- boundary_conditions = (unsigned int)_mp_arg(4);
- const CImg<T> &img = mp.imglist[ind];
- const longT
- off = (longT)_mp_arg(3),
- whds = (longT)img.size();
- if (off>=0 && off<whds) return (double)img[off];
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
- return (double)img[moff<whds?moff:whds2 - moff - 1];
- }
- case 2 : // Periodic
- return (double)img[cimg::mod(off,whds)];
- case 1 : // Neumann
- return (double)img[off<0?0:whds - 1];
- default : // Dirichlet
- return 0;
- }
- return 0;
- }
- static double mp_list_is_shared(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._is_shared;
- }
- static double mp_list_ixyzc(_cimg_math_parser& mp) {
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- interpolation = (unsigned int)_mp_arg(7),
- boundary_conditions = (unsigned int)_mp_arg(8);
- const CImg<T> &img = mp.imglist[ind];
- const double
- x = _mp_arg(3), y = _mp_arg(4),
- z = _mp_arg(5), c = _mp_arg(6);
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
- mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
- return (double)img(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- mc<img.spectrum()?mc:s2 - mc - 1);
- }
- case 2 : // Periodic
- return (double)img((int)cimg::mod(x,(double)img._width),
- (int)cimg::mod(y,(double)img._height),
- (int)cimg::mod(z,(double)img._depth),
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
- default : // Dirichlet
- return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
- }
- }
- }
- static double mp_list_joff(_cimg_math_parser& mp) {
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- boundary_conditions = (unsigned int)_mp_arg(4);
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const CImg<T> &img = mp.imglist[ind];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
- whds = (longT)img.size();
- if (off>=0 && off<whds) return (double)img[off];
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whds2 = 2*whds, moff = cimg::mod(off,whds2);
- return (double)img[moff<whds?moff:whds2 - moff - 1];
- }
- case 2 : // Periodic
- return (double)img[cimg::mod(off,whds)];
- case 1 : // Neumann
- return (double)img[off<0?0:whds - 1];
- default : // Dirichlet
- return 0;
- }
- return 0;
- }
- static double mp_list_jxyzc(_cimg_math_parser& mp) {
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- interpolation = (unsigned int)_mp_arg(7),
- boundary_conditions = (unsigned int)_mp_arg(8);
- const CImg<T> &img = mp.imglist[ind];
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
- oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c],
- x = ox + _mp_arg(3), y = oy + _mp_arg(4),
- z = oz + _mp_arg(5), c = oc + _mp_arg(6);
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._cubic_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._cubic_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.cubic_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(), s2 = 2.f*img.spectrum(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2),
- mz = cimg::mod((float)z,d2), mc = cimg::mod((float)c,s2);
- return (double)img._linear_atXYZ(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- (int)(mc<img.spectrum()?mc:s2 - mc - 1));
- }
- case 2 : // Periodic
- return (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._linear_atXYZ((float)x,(float)y,(float)z,
- (int)(c<0?0:c>=img._spectrum?img._spectrum - 1:c));
- default : // Dirichlet
- if (c<0 || c>=img._spectrum) return (T)0;
- return (double)img.linear_atXYZ((float)x,(float)y,(float)z,(int)c,(T)0);
- }
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(), s2 = 2*img.spectrum(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2),
- mz = cimg::mod((int)z,d2), mc = cimg::mod((int)c,s2);
- return (double)img(mx<img.width()?mx:w2 - mx - 1,
- my<img.height()?my:h2 - my - 1,
- mz<img.depth()?mz:d2 - mz - 1,
- mc<img.spectrum()?mc:s2 - mc - 1);
- }
- case 2 : // Periodic
- return (double)img((int)cimg::mod(x,(double)img._width),
- (int)cimg::mod(y,(double)img._height),
- (int)cimg::mod(z,(double)img._depth),
- (int)cimg::mod(c,(double)img._spectrum));
- case 1 : // Neumann
- return (double)img._atXYZC((int)x,(int)y,(int)z,(int)c);
- default : // Dirichlet
- return (double)img.atXYZC((int)x,(int)y,(int)z,(int)c,(T)0);
- }
- }
- }
- static double mp_list_l(_cimg_math_parser& mp) {
- return (double)mp.imglist.width();
- }
- static double mp_list_median(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- if (!mp.list_median) mp.list_median.assign(mp.imglist._width);
- if (!mp.list_median[ind]) CImg<doubleT>::vector(mp.imglist[ind].median()).move_to(mp.list_median[ind]);
- return *mp.list_median[ind];
- }
- static double mp_list_norm(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- if (!mp.list_norm) mp.list_norm.assign(mp.imglist._width);
- if (!mp.list_norm[ind]) CImg<doubleT>::vector(mp.imglist[ind].magnitude()).move_to(mp.list_norm[ind]);
- return *mp.list_norm[ind];
- }
- static double mp_list_set_ioff(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const longT
- off = (longT)_mp_arg(3),
- whds = (longT)img.size();
- const double val = _mp_arg(1);
- if (off>=0 && off<whds) img[off] = (T)val;
- return val;
- }
- static double mp_list_set_ixyzc(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- x = (int)_mp_arg(3), y = (int)_mp_arg(4),
- z = (int)_mp_arg(5), c = (int)_mp_arg(6);
- const double val = _mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
- z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
- img(x,y,z,c) = (T)val;
- return val;
- }
- static double mp_list_set_joff(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
- whds = (longT)img.size();
- const double val = _mp_arg(1);
- if (off>=0 && off<whds) img[off] = (T)val;
- return val;
- }
- static double mp_list_set_jxyzc(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
- oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c];
- const int
- x = (int)(ox + _mp_arg(3)), y = (int)(oy + _mp_arg(4)),
- z = (int)(oz + _mp_arg(5)), c = (int)(oc + _mp_arg(6));
- const double val = _mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
- z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
- img(x,y,z,c) = (T)val;
- return val;
- }
- static double mp_list_set_Ioff_s(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const longT
- off = (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const T val = (T)_mp_arg(1);
- if (off>=0 && off<whd) {
- T *ptrd = &img[off];
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_list_set_Ioff_v(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const longT
- off = (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const double *ptrs = &_mp_arg(1) + 1;
- if (off>=0 && off<whd) {
- const unsigned int vsiz = (unsigned int)mp.opcode[4];
- T *ptrd = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_list_set_Ixyz_s(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- x = (int)_mp_arg(3),
- y = (int)_mp_arg(4),
- z = (int)_mp_arg(5);
- const T val = (T)_mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_list_set_Ixyz_v(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- x = (int)_mp_arg(3),
- y = (int)_mp_arg(4),
- z = (int)_mp_arg(5);
- const double *ptrs = &_mp_arg(1) + 1;
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- const unsigned int vsiz = (unsigned int)mp.opcode[6];
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_list_set_Joff_s(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const T val = (T)_mp_arg(1);
- if (off>=0 && off<whd) {
- T *ptrd = &img[off];
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_list_set_Joff_v(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const double *ptrs = &_mp_arg(1) + 1;
- if (off>=0 && off<whd) {
- const unsigned int vsiz = (unsigned int)mp.opcode[4];
- T *ptrd = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_list_set_Jxyz_s(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
- const int
- x = (int)(ox + _mp_arg(3)),
- y = (int)(oy + _mp_arg(4)),
- z = (int)(oz + _mp_arg(5));
- const T val = (T)_mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_list_set_Jxyz_v(_cimg_math_parser& mp) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- CImg<T> &img = mp.imglist[ind];
- const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
- const int
- x = (int)(ox + _mp_arg(3)),
- y = (int)(oy + _mp_arg(4)),
- z = (int)(oz + _mp_arg(5));
- const double *ptrs = &_mp_arg(1) + 1;
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- const unsigned int vsiz = (unsigned int)mp.opcode[6];
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_list_spectrum(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._spectrum;
- }
- static double mp_list_stats(_cimg_math_parser& mp) {
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- k = (unsigned int)mp.opcode[3];
- if (!mp.list_stats) mp.list_stats.assign(mp.imglist._width);
- if (!mp.list_stats[ind]) mp.list_stats[ind].assign(1,14,1,1,0).fill(mp.imglist[ind].get_stats(),false);
- return mp.list_stats(ind,k);
- }
- static double mp_list_wh(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._width*mp.imglist[ind]._height;
- }
- static double mp_list_whd(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._width*mp.imglist[ind]._height*mp.imglist[ind]._depth;
- }
- static double mp_list_whds(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._width*mp.imglist[ind]._height*mp.imglist[ind]._depth*mp.imglist[ind]._spectrum;
- }
- static double mp_list_width(_cimg_math_parser& mp) {
- const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
- return (double)mp.imglist[ind]._width;
- }
- static double mp_list_Ioff(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- boundary_conditions = (unsigned int)_mp_arg(4),
- vsiz = (unsigned int)mp.opcode[5];
- const CImg<T> &img = mp.imglist[ind];
- const longT
- off = (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const T *ptrs;
- if (off>=0 && off<whd) {
- ptrs = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
- ptrs = &img[moff<whd?moff:whd2 - moff - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- case 2 : // Periodic
- ptrs = &img[cimg::mod(off,whd)];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- case 1 : // Neumann
- ptrs = off<0?&img[0]:&img[whd - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- default : // Dirichlet
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- static double mp_list_Ixyz(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- interpolation = (unsigned int)_mp_arg(6),
- boundary_conditions = (unsigned int)_mp_arg(7),
- vsiz = (unsigned int)mp.opcode[8];
- const CImg<T> &img = mp.imglist[ind];
- const double x = _mp_arg(3), y = _mp_arg(4), z = _mp_arg(5);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- const T *ptrs;
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 2 : { // Periodic
- const int
- cx = (int)cimg::mod(x,(double)img._width),
- cy = (int)cimg::mod(y,(double)img._height),
- cz = (int)cimg::mod(z,(double)img._depth);
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 1 : { // Neumann
- ptrs = &img._atXYZ((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- default : // Dirichlet
- if (img.containsXYZC((int)x,(int)y,(int)z)) {
- ptrs = &img((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } else std::memset(ptrd,0,vsiz*sizeof(double));
- }
- }
- return cimg::type<double>::nan();
- }
- static double mp_list_Joff(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- boundary_conditions = (unsigned int)_mp_arg(4),
- vsiz = (unsigned int)mp.opcode[5];
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y], oz = (int)mp.mem[_cimg_mp_slot_z];
- const CImg<T> &img = mp.imglist[ind];
- const longT
- off = img.offset(ox,oy,oz) + (longT)_mp_arg(3),
- whd = (longT)img.width()*img.height()*img.depth();
- const T *ptrs;
- if (off>=0 && off<whd) {
- ptrs = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
- ptrs = &img[moff<whd?moff:whd2 - moff - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- case 2 : // Periodic
- ptrs = &img[cimg::mod(off,whd)];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- case 1 : // Neumann
- ptrs = off<0?&img[0]:&img[whd - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- default : // Dirichlet
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- static double mp_list_Jxyz(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width()),
- interpolation = (unsigned int)_mp_arg(6),
- boundary_conditions = (unsigned int)_mp_arg(7),
- vsiz = (unsigned int)mp.opcode[8];
- const CImg<T> &img = mp.imglist[ind];
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z],
- x = ox + _mp_arg(3), y = oy + _mp_arg(4), z = oz + _mp_arg(5);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- const T *ptrs;
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- case 0 : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 2 : { // Periodic
- const int
- cx = (int)cimg::mod(x,(double)img._width),
- cy = (int)cimg::mod(y,(double)img._height),
- cz = (int)cimg::mod(z,(double)img._depth);
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 1 : { // Neumann
- ptrs = &img._atXYZ((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- default : // Dirichlet
- if (img.containsXYZC((int)x,(int)y,(int)z)) {
- ptrs = &img((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } else std::memset(ptrd,0,vsiz*sizeof(double));
- }
- }
- return cimg::type<double>::nan();
- }
- static double mp_log(_cimg_math_parser& mp) {
- return std::log(_mp_arg(2));
- }
- static double mp_log10(_cimg_math_parser& mp) {
- return std::log10(_mp_arg(2));
- }
- static double mp_log2(_cimg_math_parser& mp) {
- return cimg::log2(_mp_arg(2));
- }
- static double mp_logical_and(_cimg_math_parser& mp) {
- const bool val_left = (bool)_mp_arg(2);
- const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[4];
- if (!val_left) { mp.p_code = p_end - 1; return 0; }
- const ulongT mem_right = mp.opcode[3];
- for ( ; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- --mp.p_code;
- return (double)(bool)mp.mem[mem_right];
- }
- static double mp_logical_not(_cimg_math_parser& mp) {
- return (double)!_mp_arg(2);
- }
- static double mp_logical_or(_cimg_math_parser& mp) {
- const bool val_left = (bool)_mp_arg(2);
- const CImg<ulongT> *const p_end = ++mp.p_code + mp.opcode[4];
- if (val_left) { mp.p_code = p_end - 1; return 1; }
- const ulongT mem_right = mp.opcode[3];
- for ( ; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- --mp.p_code;
- return (double)(bool)mp.mem[mem_right];
- }
- static double mp_lowercase(_cimg_math_parser& mp) {
- return cimg::lowercase(_mp_arg(2));
- }
- static double mp_lt(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)<_mp_arg(3));
- }
- static double mp_lte(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)<=_mp_arg(3));
- }
- static double mp_matrix_eig(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *ptr1 = &_mp_arg(2) + 1;
- const unsigned int k = (unsigned int)mp.opcode[3];
- CImg<doubleT> val, vec;
- CImg<doubleT>(ptr1,k,k,1,1,true).symmetric_eigen(val,vec);
- CImg<doubleT>(ptrd,1,k,1,1,true) = val;
- CImg<doubleT>(ptrd + k,k,k,1,1,true) = vec.get_transpose();
- return cimg::type<double>::nan();
- }
- static double mp_matrix_invert(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const double *const ptr1 = &_mp_arg(2) + 1;
- const unsigned int k = (unsigned int)mp.opcode[3];
- const bool use_LU = (bool)_mp_arg(4);
- CImg<doubleT>(ptrd,k,k,1,1,true) = CImg<doubleT>(ptr1,k,k,1,1,true).get_invert(use_LU);
- return cimg::type<double>::nan();
- }
- static double mp_matrix_mul(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double
- *ptr1 = &_mp_arg(2) + 1,
- *ptr2 = &_mp_arg(3) + 1;
- const unsigned int
- k = (unsigned int)mp.opcode[4],
- l = (unsigned int)mp.opcode[5],
- m = (unsigned int)mp.opcode[6];
- CImg<doubleT>(ptrd,m,k,1,1,true) = CImg<doubleT>(ptr1,l,k,1,1,true)*CImg<doubleT>(ptr2,m,l,1,1,true);
- return cimg::type<double>::nan();
- }
- static double mp_matrix_pseudoinvert(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *ptr1 = &_mp_arg(2) + 1;
- const unsigned int
- k = (unsigned int)mp.opcode[3],
- l = (unsigned int)mp.opcode[4];
- const bool use_LU = (bool)_mp_arg(5);
- CImg<doubleT>(ptrd,l,k,1,1,true) = CImg<doubleT>(ptr1,k,l,1,1,true).get_pseudoinvert(use_LU);
- return cimg::type<double>::nan();
- }
- static double mp_matrix_svd(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *ptr1 = &_mp_arg(2) + 1;
- const unsigned int
- k = (unsigned int)mp.opcode[3],
- l = (unsigned int)mp.opcode[4];
- CImg<doubleT> U, S, V;
- CImg<doubleT>(ptr1,k,l,1,1,true).SVD(U,S,V);
- CImg<doubleT>(ptrd,k,l,1,1,true) = U;
- CImg<doubleT>(ptrd + k*l,1,k,1,1,true) = S;
- CImg<doubleT>(ptrd + k*l + k,k,k,1,1,true) = V;
- return cimg::type<double>::nan();
- }
- static double mp_max(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i) val = std::max(val,_mp_arg(i));
- return val;
- }
- static double mp_maxabs(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3), absval = cimg::abs(val);
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i), _absval = cimg::abs(_val);
- if (_absval>absval) { val = _val; absval = _absval; }
- }
- return val;
- }
- static double* _mp_memcopy_double(_cimg_math_parser& mp, const unsigned int ind, const ulongT *const p_ref,
- const longT siz, const long inc) {
- const longT
- off = *p_ref?p_ref[1] + (longT)mp.mem[(longT)p_ref[2]] + 1:ind,
- eoff = off + (siz - 1)*inc;
- if (off<0 || eoff>=mp.mem.width())
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': "
- "Out-of-bounds variable pointer "
- "(length: %ld, increment: %ld, offset start: %ld, "
- "offset end: %ld, offset max: %u).",
- mp.imgin.pixel_type(),siz,inc,off,eoff,mp.mem._width - 1);
- return &mp.mem[off];
- }
- static float* _mp_memcopy_float(_cimg_math_parser& mp, const ulongT *const p_ref,
- const longT siz, const long inc, const bool is_out) {
- const unsigned ind = (unsigned int)p_ref[1];
- const CImg<T> &img = is_out?
- (ind==~0U?mp.imgout:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]):
- (ind==~0U?mp.imgin:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]);
- const bool is_relative = (bool)p_ref[2];
- int ox, oy, oz, oc;
- longT off = 0;
- if (is_relative) {
- ox = (int)mp.mem[_cimg_mp_slot_x];
- oy = (int)mp.mem[_cimg_mp_slot_y];
- oz = (int)mp.mem[_cimg_mp_slot_z];
- oc = (int)mp.mem[_cimg_mp_slot_c];
- off = img.offset(ox,oy,oz,oc);
- }
- if ((*p_ref)%2) {
- const int
- x = (int)mp.mem[p_ref[3]],
- y = (int)mp.mem[p_ref[4]],
- z = (int)mp.mem[p_ref[5]],
- c = *p_ref==5?0:(int)mp.mem[p_ref[6]];
- off+=img.offset(x,y,z,c);
- } else off+=(longT)mp.mem[p_ref[3]];
- const longT eoff = off + (siz - 1)*inc;
- if (off<0 || eoff>=(longT)img.size())
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': "
- "Out-of-bounds image pointer "
- "(length: %ld, increment: %ld, offset start: %ld, "
- "offset end: %ld, offset max: %lu).",
- mp.imgin.pixel_type(),siz,inc,off,eoff,img.size() - 1);
- return (float*)&img[off];
- }
- static double mp_memcopy(_cimg_math_parser& mp) {
- longT siz = (longT)_mp_arg(4);
- const longT inc_d = (longT)_mp_arg(5), inc_s = (longT)_mp_arg(6);
- const float
- _opacity = (float)_mp_arg(7),
- opacity = (float)cimg::abs(_opacity),
- omopacity = 1 - std::max(_opacity,0.f);
- if (siz>0) {
- const bool
- is_doubled = mp.opcode[8]<=1,
- is_doubles = mp.opcode[15]<=1;
- if (is_doubled && is_doubles) { // (double*) <- (double*)
- double *ptrd = _mp_memcopy_double(mp,(unsigned int)mp.opcode[2],&mp.opcode[8],siz,inc_d);
- const double *ptrs = _mp_memcopy_double(mp,(unsigned int)mp.opcode[3],&mp.opcode[15],siz,inc_s);
- if (inc_d==1 && inc_s==1 && _opacity>=1) {
- if (ptrs + siz - 1<ptrd || ptrs>ptrd + siz - 1) std::memcpy(ptrd,ptrs,siz*sizeof(double));
- else std::memmove(ptrd,ptrs,siz*sizeof(double));
- } else {
- if (ptrs + (siz - 1)*inc_s<ptrd || ptrs>ptrd + (siz - 1)*inc_d) {
- if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- } else { // Overlapping buffers
- CImg<doubleT> buf((unsigned int)siz);
- cimg_for(buf,ptr,double) { *ptr = *ptrs; ptrs+=inc_s; }
- ptrs = buf;
- if (_opacity>=1) while (siz-->0) { *ptrd = *(ptrs++); ptrd+=inc_d; }
- else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**(ptrs++); ptrd+=inc_d; }
- }
- }
- } else if (is_doubled && !is_doubles) { // (double*) <- (float*)
- double *ptrd = _mp_memcopy_double(mp,(unsigned int)mp.opcode[2],&mp.opcode[8],siz,inc_d);
- const float *ptrs = _mp_memcopy_float(mp,&mp.opcode[15],siz,inc_s,false);
- if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- else while (siz-->0) { *ptrd = omopacity**ptrd + _opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- } else if (!is_doubled && is_doubles) { // (float*) <- (double*)
- float *ptrd = _mp_memcopy_float(mp,&mp.opcode[8],siz,inc_d,true);
- const double *ptrs = _mp_memcopy_double(mp,(unsigned int)mp.opcode[3],&mp.opcode[15],siz,inc_s);
- if (_opacity>=1) while (siz-->0) { *ptrd = (float)*ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- else while (siz-->0) { *ptrd = (float)(omopacity**ptrd + opacity**ptrs); ptrd+=inc_d; ptrs+=inc_s; }
- } else { // (float*) <- (float*)
- float *ptrd = _mp_memcopy_float(mp,&mp.opcode[8],siz,inc_d,true);
- const float *ptrs = _mp_memcopy_float(mp,&mp.opcode[15],siz,inc_s,false);
- if (inc_d==1 && inc_s==1 && _opacity>=1) {
- if (ptrs + siz - 1<ptrd || ptrs>ptrd + siz - 1) std::memcpy(ptrd,ptrs,siz*sizeof(float));
- else std::memmove(ptrd,ptrs,siz*sizeof(float));
- } else {
- if (ptrs + (siz - 1)*inc_s<ptrd || ptrs>ptrd + (siz - 1)*inc_d) {
- if (_opacity>=1) while (siz-->0) { *ptrd = *ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**ptrs; ptrd+=inc_d; ptrs+=inc_s; }
- } else { // Overlapping buffers
- CImg<floatT> buf((unsigned int)siz);
- cimg_for(buf,ptr,float) { *ptr = *ptrs; ptrs+=inc_s; }
- ptrs = buf;
- if (_opacity>=1) while (siz-->0) { *ptrd = *(ptrs++); ptrd+=inc_d; }
- else while (siz-->0) { *ptrd = omopacity**ptrd + opacity**(ptrs++); ptrd+=inc_d; }
- }
- }
- }
- }
- return _mp_arg(1);
- }
- static double mp_min(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i) val = std::min(val,_mp_arg(i));
- return val;
- }
- static double mp_minabs(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3), absval = cimg::abs(val);
- for (unsigned int i = 4; i<i_end; ++i) {
- const double _val = _mp_arg(i), _absval = cimg::abs(_val);
- if (_absval<absval) { val = _val; absval = _absval; }
- }
- return val;
- }
- static double mp_minus(_cimg_math_parser& mp) {
- return -_mp_arg(2);
- }
- static double mp_median(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- switch (i_end - 3) {
- case 1 : return _mp_arg(3);
- case 2 : return cimg::median(_mp_arg(3),_mp_arg(4));
- case 3 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5));
- case 5 : return cimg::median(_mp_arg(3),_mp_arg(4),_mp_arg(5),_mp_arg(6),_mp_arg(7));
- 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));
- 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),
- _mp_arg(10),_mp_arg(11));
- 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),
- _mp_arg(10),_mp_arg(11),_mp_arg(12),_mp_arg(13),_mp_arg(14),_mp_arg(15));
- }
- CImg<doubleT> vals(i_end - 3);
- double *p = vals.data();
- for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
- return vals.median();
- }
- static double mp_modulo(_cimg_math_parser& mp) {
- return cimg::mod(_mp_arg(2),_mp_arg(3));
- }
- static double mp_mproj(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double
- *ptrS = &_mp_arg(2) + 1,
- *ptrD = &_mp_arg(5) + 1;
- const unsigned int
- wS = (unsigned int)mp.opcode[3],
- hS = (unsigned int)mp.opcode[4],
- wD = (unsigned int)mp.opcode[6];
- const int
- method = std::max(0,(int)_mp_arg(7)),
- max_iter = std::max(0,(int)_mp_arg(8));
- const double
- max_residual = std::max(0.,_mp_arg(9));
- CImg<doubleT>(ptrd,wS,wD,1,1,true) = CImg<doubleT>(ptrS,wS,hS,1,1,false).
- project_matrix(CImg<doubleT>(ptrD,wD,hS,1,1,true),method,max_iter,max_residual);
- return cimg::type<double>::nan();
- }
- static double mp_mul(_cimg_math_parser& mp) {
- return _mp_arg(2)*_mp_arg(3);
- }
- static double mp_mul2(_cimg_math_parser& mp) {
- return _mp_arg(2)*_mp_arg(3)*_mp_arg(4);
- }
- static double mp_neq(_cimg_math_parser& mp) {
- return (double)(_mp_arg(2)!=_mp_arg(3));
- }
- static double mp_norm0(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- switch (i_end - 3) {
- case 1 : return _mp_arg(3)!=0;
- case 2 : return (_mp_arg(3)!=0) + (_mp_arg(4)!=0);
- }
- double res = 0;
- for (unsigned int i = 3; i<i_end; ++i)
- res+=_mp_arg(i)==0?0:1;
- return res;
- }
- static double mp_norm1(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- switch (i_end - 3) {
- case 1 : return cimg::abs(_mp_arg(3));
- case 2 : return cimg::abs(_mp_arg(3)) + cimg::abs(_mp_arg(4));
- }
- double res = 0;
- for (unsigned int i = 3; i<i_end; ++i)
- res+=cimg::abs(_mp_arg(i));
- return res;
- }
- static double mp_norm2(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- switch (i_end - 3) {
- case 1 : return cimg::abs(_mp_arg(3));
- case 2 : return cimg::_hypot(_mp_arg(3),_mp_arg(4));
- }
- double res = 0;
- for (unsigned int i = 3; i<i_end; ++i)
- res+=cimg::sqr(_mp_arg(i));
- return std::sqrt(res);
- }
- static double mp_norminf(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- switch (i_end - 3) {
- case 1 : return cimg::abs(_mp_arg(3));
- case 2 : return std::max(cimg::abs(_mp_arg(3)),cimg::abs(_mp_arg(4)));
- }
- double res = 0;
- for (unsigned int i = 3; i<i_end; ++i) {
- const double val = cimg::abs(_mp_arg(i));
- if (val>res) res = val;
- }
- return res;
- }
- static double mp_normp(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- if (i_end==4) return cimg::abs(_mp_arg(3));
- const double p = (double)mp.opcode[3];
- double res = 0;
- for (unsigned int i = 4; i<i_end; ++i)
- res+=std::pow(cimg::abs(_mp_arg(i)),p);
- res = std::pow(res,1/p);
- return res>0?res:0.;
- }
- static double mp_permutations(_cimg_math_parser& mp) {
- return cimg::permutations((int)_mp_arg(2),(int)_mp_arg(3),(bool)_mp_arg(4));
- }
- static double mp_polygon(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- unsigned int ind = (unsigned int)mp.opcode[3];
- if (ind!=~0U) {
- if (!mp.imglist.width()) return cimg::type<double>::nan();
- ind = (unsigned int)cimg::mod((int)_mp_arg(3),mp.imglist.width());
- }
- CImg<T> &img = ind==~0U?mp.imgout:mp.imglist[ind];
- bool is_invalid_arguments = i_end<=4, is_outlined = false;
- if (!is_invalid_arguments) {
- int nbv = (int)_mp_arg(4);
- if (!nbv) is_invalid_arguments = true;
- else {
- if (nbv<0) { nbv = -nbv; is_outlined = true; }
- CImg<intT> points(nbv,2,1,1,0);
- CImg<T> color(img._spectrum,1,1,1,0);
- float opacity = 1;
- unsigned int i = 5, pattern=~0U;
- cimg_foroff(points,k) if (i<i_end) points(k/2,k%2) = (int)cimg::round(_mp_arg(i++));
- else { is_invalid_arguments = true; break; }
- if (!is_invalid_arguments) {
- if (i<i_end) opacity = (float)_mp_arg(i++);
- if (is_outlined && i<i_end) pattern = (unsigned int)_mp_arg(i++);
- cimg_forX(color,k) if (i<i_end) color[k] = (T)_mp_arg(i++);
- else { color.resize(k,1,1,1,-1); break; }
- color.resize(img._spectrum,1,1,1,0,2);
- if (is_outlined) img.draw_polygon(points,color._data,opacity,pattern);
- else img.draw_polygon(points,color._data,opacity);
- }
- }
- }
- if (is_invalid_arguments) {
- CImg<doubleT> args(i_end - 4);
- cimg_forX(args,k) args[k] = _mp_arg(4 + k);
- if (ind==~0U)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
- "Invalid arguments '%s'. ",
- mp.imgin.pixel_type(),args.value_string()._data);
- else
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'polygon()': "
- "Invalid arguments '#%u%s%s'. ",
- mp.imgin.pixel_type(),ind,args._width?",":"",args.value_string()._data);
- }
- return cimg::type<double>::nan();
- }
- static double mp_pow(_cimg_math_parser& mp) {
- const double v = _mp_arg(2), p = _mp_arg(3);
- return std::pow(v,p);
- }
- static double mp_pow0_25(_cimg_math_parser& mp) {
- const double val = _mp_arg(2);
- return std::sqrt(std::sqrt(val));
- }
- static double mp_pow3(_cimg_math_parser& mp) {
- const double val = _mp_arg(2);
- return val*val*val;
- }
- static double mp_pow4(_cimg_math_parser& mp) {
- const double val = _mp_arg(2);
- return val*val*val*val;
- }
- static double mp_print(_cimg_math_parser& mp) {
- const double val = _mp_arg(1);
- const bool print_char = (bool)mp.opcode[3];
- cimg_pragma_openmp(critical(mp_print))
- {
- CImg<charT> _expr(mp.opcode[2] - 4);
- const ulongT *ptrs = mp.opcode._data + 4;
- cimg_for(_expr,ptrd,char) *ptrd = (char)*(ptrs++);
- cimg::strellipsize(_expr);
- cimg::mutex(6);
- if (print_char)
- std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %.17g = '%c'",
- _expr._data,val,(int)val);
- else
- std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = %.17g",
- _expr._data,val);
- std::fflush(cimg::output());
- cimg::mutex(6,0);
- }
- return val;
- }
- static double mp_prod(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i) val*=_mp_arg(i);
- return val;
- }
- static double mp_rad2deg(_cimg_math_parser& mp) {
- return _mp_arg(2)*180/cimg::PI;
- }
- static double mp_repeat(_cimg_math_parser& mp) {
- const double nb_it = _mp_arg(2);
- double
- *const ptrc = mp.opcode[3]!=~0U?&_mp_arg(3):0,
- *const ptrs = &_mp_arg(1);
- const CImg<ulongT>
- *const p_body = ++mp.p_code,
- *const p_end = p_body + mp.opcode[4];
- if (nb_it>0) {
- const unsigned int _break_type = mp.break_type;
- mp.break_type = 0;
- double it = 0;
- if (ptrc) { // Version with loop variable (3 arguments)
- while (it<nb_it) {
- *ptrc = it;
- for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- ++it;
- }
- *ptrc = it;
- } else // Version without loop variable (2 arguments)
- while (it<nb_it) {
- for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- ++it;
- }
- mp.break_type = _break_type;
- }
- mp.p_code = p_end - 1;
- return *ptrs;
- }
- static double mp_rol(_cimg_math_parser& mp) {
- return cimg::rol(_mp_arg(2),(unsigned int)_mp_arg(3));
- }
- static double mp_ror(_cimg_math_parser& mp) {
- return cimg::ror(_mp_arg(2),(unsigned int)_mp_arg(3));
- }
- static double mp_rot2d(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const float
- theta = (float)_mp_arg(2),
- ca = std::cos(theta),
- sa = std::sin(theta);
- *(ptrd++) = ca;
- *(ptrd++) = -sa;
- *(ptrd++) = sa;
- *ptrd = ca;
- return cimg::type<double>::nan();
- }
- static double mp_rot3d(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const float
- x = (float)_mp_arg(2),
- y = (float)_mp_arg(3),
- z = (float)_mp_arg(4),
- theta = (float)_mp_arg(5);
- CImg<doubleT>(ptrd,3,3,1,1,true) = CImg<doubleT>::rotation_matrix(x,y,z,theta*180/cimg::PI);
- return cimg::type<double>::nan();
- }
- static double mp_round(_cimg_math_parser& mp) {
- return cimg::round(_mp_arg(2),_mp_arg(3),(int)_mp_arg(4));
- }
- static double mp_self_add(_cimg_math_parser& mp) {
- return _mp_arg(1)+=_mp_arg(2);
- }
- static double mp_self_bitwise_and(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = (double)((longT)val & (longT)_mp_arg(2));
- }
- static double mp_self_bitwise_left_shift(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = (double)((longT)val<<(unsigned int)_mp_arg(2));
- }
- static double mp_self_bitwise_or(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = (double)((longT)val | (longT)_mp_arg(2));
- }
- static double mp_self_bitwise_right_shift(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = (double)((longT)val>>(unsigned int)_mp_arg(2));
- }
- static double mp_self_decrement(_cimg_math_parser& mp) {
- return --_mp_arg(1);
- }
- static double mp_self_increment(_cimg_math_parser& mp) {
- return ++_mp_arg(1);
- }
- static double mp_self_map_vector_s(_cimg_math_parser& mp) { // Vector += scalar
- unsigned int
- ptrd = (unsigned int)mp.opcode[1] + 1,
- siz = (unsigned int)mp.opcode[2];
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,3);
- l_opcode[2] = mp.opcode[4]; // Scalar argument
- l_opcode.swap(mp.opcode);
- ulongT &target = mp.opcode[1];
- while (siz-->0) { target = ptrd++; (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_self_map_vector_v(_cimg_math_parser& mp) { // Vector += vector
- unsigned int
- ptrd = (unsigned int)mp.opcode[1] + 1,
- siz = (unsigned int)mp.opcode[2],
- ptrs = (unsigned int)mp.opcode[4] + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,4);
- l_opcode.swap(mp.opcode);
- ulongT &target = mp.opcode[1], &argument = mp.opcode[2];
- while (siz-->0) { target = ptrd++; argument = ptrs++; (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_self_mul(_cimg_math_parser& mp) {
- return _mp_arg(1)*=_mp_arg(2);
- }
- static double mp_self_div(_cimg_math_parser& mp) {
- return _mp_arg(1)/=_mp_arg(2);
- }
- static double mp_self_modulo(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = cimg::mod(val,_mp_arg(2));
- }
- static double mp_self_pow(_cimg_math_parser& mp) {
- double &val = _mp_arg(1);
- return val = std::pow(val,_mp_arg(2));
- }
- static double mp_self_sub(_cimg_math_parser& mp) {
- return _mp_arg(1)-=_mp_arg(2);
- }
- #ifdef cimg_mp_func_set
- static double mp_set(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(1);
- double *ptrd = &_mp_arg(3) + 1;
- const unsigned int
- sizs = (unsigned int)mp.opcode[2],
- sizd = (unsigned int)mp.opcode[4];
- CImg<charT> sd(sizd + 1);
- cimg_for_inX(sd,0,sd.width() - 1,i) sd[i] = (char)ptrd[i];
- sd.back() = 0;
- if (sizs) cimg_mp_func_set(ptrs + 1,sizs,sd._data);
- else cimg_mp_func_set(ptrs,0,sd._data);
- return *ptrs;
- }
- #endif
- static double mp_set_ioff(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const longT
- off = (longT)_mp_arg(2),
- whds = (longT)img.size();
- const double val = _mp_arg(1);
- if (off>=0 && off<whds) img[off] = (T)val;
- return val;
- }
- static double mp_set_ixyzc(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- x = (int)_mp_arg(2), y = (int)_mp_arg(3),
- z = (int)_mp_arg(4), c = (int)_mp_arg(5);
- const double val = _mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
- z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
- img(x,y,z,c) = (T)val;
- return val;
- }
- static double mp_set_joff(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
- whds = (longT)img.size();
- const double val = _mp_arg(1);
- if (off>=0 && off<whds) img[off] = (T)val;
- return val;
- }
- static double mp_set_jxyzc(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y],
- oz = mp.mem[_cimg_mp_slot_z], oc = mp.mem[_cimg_mp_slot_c];
- const int
- x = (int)(ox + _mp_arg(2)), y = (int)(oy + _mp_arg(3)),
- z = (int)(oz + _mp_arg(4)), c = (int)(oc + _mp_arg(5));
- const double val = _mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() &&
- z>=0 && z<img.depth() && c>=0 && c<img.spectrum())
- img(x,y,z,c) = (T)val;
- return val;
- }
- static double mp_set_Ioff_s(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const longT
- off = (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const T val = (T)_mp_arg(1);
- if (off>=0 && off<whd) {
- T *ptrd = &img[off];
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_set_Ioff_v(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const longT
- off = (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const double *ptrs = &_mp_arg(1) + 1;
- if (off>=0 && off<whd) {
- const unsigned int vsiz = (unsigned int)mp.opcode[3];
- T *ptrd = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_set_Ixyz_s(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- x = (int)_mp_arg(2),
- y = (int)_mp_arg(3),
- z = (int)_mp_arg(4);
- const T val = (T)_mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_set_Ixyz_v(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- x = (int)_mp_arg(2),
- y = (int)_mp_arg(3),
- z = (int)_mp_arg(4);
- const double *ptrs = &_mp_arg(1) + 1;
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- const unsigned int vsiz = (unsigned int)mp.opcode[5];
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_set_Joff_s(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const T val = (T)_mp_arg(1);
- if (off>=0 && off<whd) {
- T *ptrd = &img[off];
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_set_Joff_v(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x], oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z], oc = (int)mp.mem[_cimg_mp_slot_c];
- const longT
- off = img.offset(ox,oy,oz,oc) + (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const double *ptrs = &_mp_arg(1) + 1;
- if (off>=0 && off<whd) {
- const unsigned int vsiz = (unsigned int)mp.opcode[3];
- T *ptrd = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_set_Jxyz_s(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
- const int
- x = (int)(ox + _mp_arg(2)),
- y = (int)(oy + _mp_arg(3)),
- z = (int)(oz + _mp_arg(4));
- const T val = (T)_mp_arg(1);
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_forC(img,c) { *ptrd = val; ptrd+=whd; }
- }
- return _mp_arg(1);
- }
- static double mp_set_Jxyz_v(_cimg_math_parser& mp) {
- CImg<T> &img = mp.imgout;
- const double ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z];
- const int
- x = (int)(ox + _mp_arg(2)),
- y = (int)(oy + _mp_arg(3)),
- z = (int)(oz + _mp_arg(4));
- const double *ptrs = &_mp_arg(1) + 1;
- if (x>=0 && x<img.width() && y>=0 && y<img.height() && z>=0 && z<img.depth()) {
- const unsigned int vsiz = (unsigned int)mp.opcode[5];
- T *ptrd = &img(x,y,z);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- cimg_for_inC(img,0,vsiz - 1,c) { *ptrd = (T)*(ptrs++); ptrd+=whd; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_shift(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const double *const ptrs = &_mp_arg(2) + 1;
- const unsigned int siz = (unsigned int)mp.opcode[3];
- const int
- shift = (int)_mp_arg(4),
- boundary_conditions = (int)_mp_arg(5);
- CImg<doubleT>(ptrd,siz,1,1,1,true) = CImg<doubleT>(ptrs,siz,1,1,1,true).shift(shift,0,0,0,boundary_conditions);
- return cimg::type<double>::nan();
- }
- static double mp_sign(_cimg_math_parser& mp) {
- return cimg::sign(_mp_arg(2));
- }
- static double mp_sin(_cimg_math_parser& mp) {
- return std::sin(_mp_arg(2));
- }
- static double mp_sinc(_cimg_math_parser& mp) {
- return cimg::sinc(_mp_arg(2));
- }
- static double mp_sinh(_cimg_math_parser& mp) {
- return std::sinh(_mp_arg(2));
- }
- static double mp_solve(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double
- *ptr1 = &_mp_arg(2) + 1,
- *ptr2 = &_mp_arg(3) + 1;
- const unsigned int
- k = (unsigned int)mp.opcode[4],
- l = (unsigned int)mp.opcode[5],
- m = (unsigned int)mp.opcode[6];
- 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));
- return cimg::type<double>::nan();
- }
- static double mp_sort(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const double *const ptrs = &_mp_arg(2) + 1;
- const bool is_increasing = (bool)_mp_arg(4);
- const unsigned int
- siz = (unsigned int)mp.opcode[3],
- nb_elts = mp.opcode[5]==~0U?siz:(unsigned int)_mp_arg(5),
- siz_elt = (unsigned int)_mp_arg(6);
- const ulongT sn = siz_elt*nb_elts;
- if (sn>siz || siz_elt<1)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'sort()': "
- "Arguments 'nb_elts=%g' and 'siz_elt=%g' are invalid "
- "for sorting a vector of size %u.",
- mp.imgin.pixel_type(),_mp_arg(5),_mp_arg(6),siz);
- CImg<doubleT>(ptrd,siz_elt,nb_elts,1,1,true) = CImg<doubleT>(ptrs,siz_elt,nb_elts,1,1,true).
- get_sort(is_increasing,siz_elt>1?'y':0);
- if (sn<siz) CImg<doubleT>(ptrd + sn,siz - sn,1,1,1,true) = CImg<doubleT>(ptrs + sn,siz - sn,1,1,1,true);
- return cimg::type<double>::nan();
- }
- static double mp_sqr(_cimg_math_parser& mp) {
- return cimg::sqr(_mp_arg(2));
- }
- static double mp_sqrt(_cimg_math_parser& mp) {
- return std::sqrt(_mp_arg(2));
- }
- static double mp_srand(_cimg_math_parser& mp) {
- mp.rng = (cimg_uint64)_mp_arg(2);
- return cimg::type<double>::nan();
- }
- static double mp_srand0(_cimg_math_parser& mp) {
- cimg::srand(&mp.rng);
- #if cimg_use_openmp!=0
- mp.rng+=omp_get_thread_num();
- #endif
- return cimg::type<double>::nan();
- }
- static double mp_std(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- CImg<doubleT> vals(i_end - 3);
- double *p = vals.data();
- for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
- return std::sqrt(vals.variance());
- }
- static double mp_string_init(_cimg_math_parser& mp) {
- const unsigned char *ptrs = (unsigned char*)&mp.opcode[3];
- unsigned int
- ptrd = (unsigned int)mp.opcode[1] + 1,
- siz = (unsigned int)mp.opcode[2];
- while (siz-->0) mp.mem[ptrd++] = (double)*(ptrs++);
- return cimg::type<double>::nan();
- }
- #ifdef cimg_mp_func_store
- static double mp_store(_cimg_math_parser& mp) {
- const double
- *ptr1 = &_mp_arg(2),
- *ptr2 = &_mp_arg(4) + 1;
- const unsigned int
- siz1 = (unsigned int)mp.opcode[3],
- siz2 = (unsigned int)mp.opcode[5];
- const int
- w = (int)_mp_arg(6),
- h = (int)_mp_arg(7),
- d = (int)_mp_arg(8),
- s = (int)_mp_arg(9);
- const bool is_compressed = (bool)_mp_arg(10);
- if (w<0 || h<0 || d<0 || s<0)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'store()': "
- "Specified image dimensions (%d,%d,%d,%d) are invalid.",
- pixel_type(),w,h,d,s);
- CImg<charT> ss(siz2 + 1);
- cimg_for_inX(ss,0,ss.width() - 1,i) ss[i] = (char)ptr2[i];
- ss.back() = 0;
- if (siz1) cimg_mp_func_store(ptr1 + 1,siz1,
- (unsigned int)w,(unsigned int)h,(unsigned int)d,(unsigned int)s,
- is_compressed,ss._data);
- else cimg_mp_func_store(ptr1,1,(unsigned int)w,(unsigned int)h,(unsigned int)d,(unsigned int)s,
- is_compressed,ss._data);
- return cimg::type<double>::nan();
- }
- #endif
- static double mp_stov(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(2);
- const ulongT siz = (ulongT)mp.opcode[3];
- longT ind = (longT)_mp_arg(4);
- const bool is_strict = (bool)_mp_arg(5);
- double val = cimg::type<double>::nan();
- if (ind<0 || ind>=(longT)siz) return val;
- if (!siz) return *ptrs>='0' && *ptrs<='9'?*ptrs - '0':val;
- CImg<charT> ss(siz + 1 - ind);
- ptrs+=1 + ind;
- cimg_forX(ss,i) ss[i] = (char)ptrs[i];
- ss.back() = 0;
- const char *s = ss._data;
- while (*s && *s<=32) ++s;
- const bool is_negative = *s=='-';
- if (is_negative || *s=='+') ++s;
- int err = 0;
- char sep;
- if (*s=='0' && (s[1]=='x' || s[1]=='X') && s[2]>32) { // Hexadecimal number
- unsigned int ival;
- err = cimg_sscanf(s + 2,"%x%c",&ival,&sep);
- if (err>0) val = (double)ival;
- } else if (*s>32) { // Decimal number
- err = cimg_sscanf(s,"%lf%c",&val,&sep);
- #if cimg_OS==2
- // Check for +/-NaN and +/-inf as Microsoft's sscanf() version is not able
- // to read those particular values.
- if (!err && (*s=='i' || *s=='I' || *s=='n' || *s=='N')) {
- if (!cimg::strncasecmp(s,"inf",3)) { val = cimg::type<double>::inf(); err = 1 + (s[3]!=0); }
- else if (!cimg::strncasecmp(s,"nan",3)) { val = cimg::type<double>::nan(); err = 1 + (s[3]!=0); }
- }
- #endif
- }
- if (err<=0 || (is_strict && err!=1)) return cimg::type<double>::nan();
- if (is_negative) val = -val;
- return val;
- }
- static double mp_string(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const unsigned int nb_args = (unsigned int)(mp.opcode[3] - 3)/2;
- CImgList<charT> _str;
- CImg<charT> it;
- for (unsigned int n = 0; n<nb_args; ++n) {
- const unsigned int siz = (unsigned int)mp.opcode[5 + 2*n];
- if (siz) { // Vector argument -> string
- const double *ptr = &_mp_arg(4 + 2*n) + 1;
- unsigned int l = 0;
- while (l<siz && ptr[l]) ++l;
- CImg<doubleT>(ptr,l,1,1,1,true).move_to(_str);
- } else { // Scalar argument -> number
- it.assign(24);
- cimg_snprintf(it,it._width,"%.17g",_mp_arg(4 + 2*n));
- CImg<charT>::string(it,false,true).move_to(_str);
- }
- }
- const CImg<charT> str = _str>'x';
- const unsigned int sizd = std::min(str._width,(unsigned int)mp.opcode[2]);
- std::memset(ptrd,0,mp.opcode[2]*sizeof(double));
- for (unsigned int k = 0; k<sizd; ++k) ptrd[k] = (double)str[k];
- return cimg::type<double>::nan();
- }
- static double mp_sub(_cimg_math_parser& mp) {
- return _mp_arg(2) - _mp_arg(3);
- }
- static double mp_sum(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- double val = _mp_arg(3);
- for (unsigned int i = 4; i<i_end; ++i) val+=_mp_arg(i);
- return val;
- }
- static double mp_swap(_cimg_math_parser& mp) {
- const unsigned int siz = (unsigned int)mp.opcode[3];
- if (!siz) { // Scalar
- double &arg1 = _mp_arg(1), &arg2 = _mp_arg(2);
- cimg::swap(arg1,arg2);
- } else { // Vector
- double *const ptr1 = &_mp_arg(1) + 1, *const ptr2 = &_mp_arg(2) + 1;
- for (unsigned int k = 0; k<siz; ++k) cimg::swap(ptr1[k],ptr2[k]);
- }
- return _mp_arg(1);
- }
- static double mp_tan(_cimg_math_parser& mp) {
- return std::tan(_mp_arg(2));
- }
- static double mp_tanh(_cimg_math_parser& mp) {
- return std::tanh(_mp_arg(2));
- }
- static double mp_trace(_cimg_math_parser& mp) {
- const double *ptrs = &_mp_arg(2) + 1;
- const unsigned int k = (unsigned int)mp.opcode[3];
- return CImg<doubleT>(ptrs,k,k,1,1,true).trace();
- }
- static double mp_transpose(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *ptrs = &_mp_arg(2) + 1;
- const unsigned int
- k = (unsigned int)mp.opcode[3],
- l = (unsigned int)mp.opcode[4];
- CImg<doubleT>(ptrd,l,k,1,1,true) = CImg<doubleT>(ptrs,k,l,1,1,true).get_transpose();
- return cimg::type<double>::nan();
- }
- static double mp_u(_cimg_math_parser& mp) {
- return cimg::rand(_mp_arg(2),_mp_arg(3),&mp.rng);
- }
- static double mp_ui2f(_cimg_math_parser& mp) {
- return (double)cimg::uint2float((unsigned int)_mp_arg(2));
- }
- static double mp_uppercase(_cimg_math_parser& mp) {
- return cimg::uppercase(_mp_arg(2));
- }
- static double mp_var(_cimg_math_parser& mp) {
- const unsigned int i_end = (unsigned int)mp.opcode[2];
- CImg<doubleT> vals(i_end - 3);
- double *p = vals.data();
- for (unsigned int i = 3; i<i_end; ++i) *(p++) = _mp_arg(i);
- return vals.variance();
- }
- static double mp_vector_copy(_cimg_math_parser& mp) {
- std::memcpy(&_mp_arg(1) + 1,&_mp_arg(2) + 1,sizeof(double)*mp.opcode[3]);
- return cimg::type<double>::nan();
- }
- static double mp_vector_crop(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const double *ptrs = &_mp_arg(2) + 1;
- const longT
- length = (longT)mp.opcode[3],
- start = (longT)_mp_arg(4),
- sublength = (longT)mp.opcode[5],
- step = (longT)_mp_arg(6);
- if (start<0 || start + step*(sublength-1)>=length)
- throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Value accessor '[]': "
- "Out-of-bounds sub-vector request "
- "(length: %ld, start: %ld, sub-length: %ld, step: %ld).",
- mp.imgin.pixel_type(),length,start,sublength,step);
- ptrs+=start;
- if (step==1) std::memcpy(ptrd,ptrs,sublength*sizeof(double));
- else for (longT k = 0; k<sublength; ++k) { *(ptrd++) = *ptrs; ptrs+=step; }
- return cimg::type<double>::nan();
- }
- static double mp_vector_init(_cimg_math_parser& mp) {
- unsigned int
- ptrs = 4U,
- ptrd = (unsigned int)mp.opcode[1] + 1,
- siz = (unsigned int)mp.opcode[3];
- switch (mp.opcode[2] - 4) {
- case 0 : std::memset(mp.mem._data + ptrd,0,siz*sizeof(double)); break; // 0 values given
- case 1 : { const double val = _mp_arg(ptrs); while (siz-->0) mp.mem[ptrd++] = val; } break;
- default : while (siz-->0) { mp.mem[ptrd++] = _mp_arg(ptrs++); if (ptrs>=mp.opcode[2]) ptrs = 4U; }
- }
- return cimg::type<double>::nan();
- }
- static double mp_vector_eq(_cimg_math_parser& mp) {
- const double
- *ptr1 = &_mp_arg(2) + 1,
- *ptr2 = &_mp_arg(4) + 1;
- unsigned int p1 = (unsigned int)mp.opcode[3], p2 = (unsigned int)mp.opcode[5], n;
- const int N = (int)_mp_arg(6);
- const bool case_sensitive = (bool)_mp_arg(7);
- bool still_equal = true;
- double value;
- if (!N) return true;
- // Compare all values.
- if (N<0) {
- if (p1>0 && p2>0) { // Vector == vector
- if (p1!=p2) return false;
- if (case_sensitive)
- while (still_equal && p1--) still_equal = *(ptr1++)==*(ptr2++);
- else
- while (still_equal && p1--)
- still_equal = cimg::lowercase(*(ptr1++))==cimg::lowercase(*(ptr2++));
- return still_equal;
- } else if (p1>0 && !p2) { // Vector == scalar
- value = _mp_arg(4);
- if (!case_sensitive) value = cimg::lowercase(value);
- while (still_equal && p1--) still_equal = *(ptr1++)==value;
- return still_equal;
- } else if (!p1 && p2>0) { // Scalar == vector
- value = _mp_arg(2);
- if (!case_sensitive) value = cimg::lowercase(value);
- while (still_equal && p2--) still_equal = *(ptr2++)==value;
- return still_equal;
- } else { // Scalar == scalar
- if (case_sensitive) return _mp_arg(2)==_mp_arg(4);
- else return cimg::lowercase(_mp_arg(2))==cimg::lowercase(_mp_arg(4));
- }
- }
- // Compare only first N values.
- if (p1>0 && p2>0) { // Vector == vector
- n = cimg::min((unsigned int)N,p1,p2);
- if (case_sensitive)
- while (still_equal && n--) still_equal = *(ptr1++)==(*ptr2++);
- else
- while (still_equal && n--) still_equal = cimg::lowercase(*(ptr1++))==cimg::lowercase(*(ptr2++));
- return still_equal;
- } else if (p1>0 && !p2) { // Vector == scalar
- n = std::min((unsigned int)N,p1);
- value = _mp_arg(4);
- if (!case_sensitive) value = cimg::lowercase(value);
- while (still_equal && n--) still_equal = *(ptr1++)==value;
- return still_equal;
- } else if (!p1 && p2>0) { // Scalar == vector
- n = std::min((unsigned int)N,p2);
- value = _mp_arg(2);
- if (!case_sensitive) value = cimg::lowercase(value);
- while (still_equal && n--) still_equal = *(ptr2++)==value;
- return still_equal;
- } // Scalar == scalar
- if (case_sensitive) return _mp_arg(2)==_mp_arg(4);
- return cimg::lowercase(_mp_arg(2))==cimg::lowercase(_mp_arg(4));
- }
- static double mp_vector_lerp(_cimg_math_parser& mp) {
- unsigned int siz = (unsigned int)mp.opcode[2];
- double *ptrd = &_mp_arg(1) + 1;
- const double
- *ptrs1 = &_mp_arg(3) + 1,
- *ptrs2 = &_mp_arg(4) + 1,
- t = _mp_arg(5);
- for (unsigned int k = 0; k<siz; ++k) ptrd[k] = ptrs1[k]*(1-t) + ptrs2[k]*t;
- return cimg::type<double>::nan();
- }
- static double mp_vector_off(_cimg_math_parser& mp) {
- const unsigned int
- ptr = (unsigned int)mp.opcode[2] + 1,
- siz = (unsigned int)mp.opcode[3];
- const int off = (int)_mp_arg(4);
- return off>=0 && off<(int)siz?mp.mem[ptr + off]:cimg::type<double>::nan();
- }
- static double mp_vector_map_sv(_cimg_math_parser& mp) { // Operator(scalar,vector)
- unsigned int
- siz = (unsigned int)mp.opcode[2],
- ptrs = (unsigned int)mp.opcode[5] + 1;
- double *ptrd = &_mp_arg(1) + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(4);
- l_opcode[2] = mp.opcode[4]; // Scalar argument1
- l_opcode.swap(mp.opcode);
- ulongT &argument2 = mp.opcode[3];
- while (siz-->0) { argument2 = ptrs++; *(ptrd++) = (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_vector_map_v(_cimg_math_parser& mp) { // Operator(vector)
- unsigned int
- siz = (unsigned int)mp.opcode[2],
- ptrs = (unsigned int)mp.opcode[4] + 1;
- double *ptrd = &_mp_arg(1) + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,3);
- l_opcode.swap(mp.opcode);
- ulongT &argument = mp.opcode[2];
- while (siz-->0) { argument = ptrs++; *(ptrd++) = (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_vector_map_vs(_cimg_math_parser& mp) { // Operator(vector,scalar)
- unsigned int
- siz = (unsigned int)mp.opcode[2],
- ptrs = (unsigned int)mp.opcode[4] + 1;
- double *ptrd = &_mp_arg(1) + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,4);
- l_opcode[3] = mp.opcode[5]; // Scalar argument2
- l_opcode.swap(mp.opcode);
- ulongT &argument1 = mp.opcode[2];
- while (siz-->0) { argument1 = ptrs++; *(ptrd++) = (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_vector_map_vss(_cimg_math_parser& mp) { // Operator(vector,scalar,scalar)
- unsigned int
- siz = (unsigned int)mp.opcode[2],
- ptrs = (unsigned int)mp.opcode[4] + 1;
- double *ptrd = &_mp_arg(1) + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,5);
- l_opcode[3] = mp.opcode[5]; // Scalar argument2
- l_opcode[4] = mp.opcode[6]; // Scalar argument3
- l_opcode.swap(mp.opcode);
- ulongT &argument1 = mp.opcode[2];
- while (siz-->0) { argument1 = ptrs++; *(ptrd++) = (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_vector_map_vv(_cimg_math_parser& mp) { // Operator(vector,vector)
- unsigned int
- siz = (unsigned int)mp.opcode[2],
- ptrs1 = (unsigned int)mp.opcode[4] + 1,
- ptrs2 = (unsigned int)mp.opcode[5] + 1;
- double *ptrd = &_mp_arg(1) + 1;
- mp_func op = (mp_func)mp.opcode[3];
- CImg<ulongT> l_opcode(1,4);
- l_opcode.swap(mp.opcode);
- ulongT &argument1 = mp.opcode[2], &argument2 = mp.opcode[3];
- while (siz-->0) { argument1 = ptrs1++; argument2 = ptrs2++; *(ptrd++) = (*op)(mp); }
- l_opcode.swap(mp.opcode);
- return cimg::type<double>::nan();
- }
- static double mp_vector_neq(_cimg_math_parser& mp) {
- return !mp_vector_eq(mp);
- }
- static double mp_vector_print(_cimg_math_parser& mp) {
- const bool print_string = (bool)mp.opcode[4];
- cimg_pragma_openmp(critical(mp_vector_print))
- {
- CImg<charT> _expr(mp.opcode[2] - 5);
- const ulongT *ptrs = mp.opcode._data + 5;
- cimg_for(_expr,ptrd,char) *ptrd = (char)*(ptrs++);
- cimg::strellipsize(_expr);
- unsigned int
- ptr = (unsigned int)mp.opcode[1] + 1,
- siz0 = (unsigned int)mp.opcode[3],
- siz = siz0;
- cimg::mutex(6);
- std::fprintf(cimg::output(),"\n[" cimg_appname "_math_parser] %s = [ ",_expr._data);
- unsigned int count = 0;
- while (siz-->0) {
- if (count>=64 && siz>=64) {
- std::fprintf(cimg::output(),"...,");
- ptr = (unsigned int)mp.opcode[1] + 1 + siz0 - 64;
- siz = 64;
- } else std::fprintf(cimg::output(),"%.17g%s",mp.mem[ptr++],siz?",":"");
- ++count;
- }
- if (print_string) {
- CImg<charT> str(siz0 + 1);
- ptr = (unsigned int)mp.opcode[1] + 1;
- for (unsigned int k = 0; k<siz0; ++k) str[k] = (char)mp.mem[ptr++];
- str[siz0] = 0;
- cimg::strellipsize(str,1024,false);
- std::fprintf(cimg::output()," ] = '%s' (size: %u)",str._data,siz0);
- } else std::fprintf(cimg::output()," ] (size: %u)",siz0);
- std::fflush(cimg::output());
- cimg::mutex(6,0);
- }
- return cimg::type<double>::nan();
- }
- static double mp_vector_resize(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const unsigned int p1 = (unsigned int)mp.opcode[2], p2 = (unsigned int)mp.opcode[4];
- const int
- interpolation = (int)_mp_arg(5),
- boundary_conditions = (int)_mp_arg(6);
- if (p2) { // Resize vector
- const double *const ptrs = &_mp_arg(3) + 1;
- CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(ptrs,p2,1,1,1,true).
- get_resize(p1,1,1,1,interpolation,boundary_conditions);
- } else { // Resize scalar
- const double value = _mp_arg(3);
- CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(1,1,1,1,value).resize(p1,1,1,1,interpolation,
- boundary_conditions);
- }
- return cimg::type<double>::nan();
- }
- static double mp_vector_resize_ext(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const unsigned int
- siz = (unsigned int)mp.opcode[2],
- ow = (unsigned int)mp.opcode[4],
- oh = (unsigned int)mp.opcode[5],
- od = (unsigned int)mp.opcode[6],
- os = (unsigned int)mp.opcode[7],
- nw = (unsigned int)mp.opcode[8],
- nh = (unsigned int)mp.opcode[9],
- nd = (unsigned int)mp.opcode[10],
- ns = (unsigned int)mp.opcode[11];
- const int
- interpolation = (int)_mp_arg(12),
- boundary_conditions = (int)_mp_arg(13);
- const float
- ax = (float)_mp_arg(14),
- ay = (float)_mp_arg(15),
- az = (float)_mp_arg(16),
- ac = (float)_mp_arg(17);
- if (siz) { // Resize vector
- const double *const ptrs = &_mp_arg(3) + 1;
- CImg<doubleT>(ptrd,nw,nh,nd,ns,true) = CImg<doubleT>(ptrs,ow,oh,od,os,true).
- get_resize(nw,nh,nd,ns,interpolation,boundary_conditions,ax,ay,az,ac);
- } else { // Resize scalar
- const double value = _mp_arg(3);
- CImg<doubleT>(ptrd,nw,nh,nd,ns,true) = CImg<doubleT>(1,1,1,1,value).
- resize(nw,nh,nd,ns,interpolation,boundary_conditions,ax,ay,az,ac);
- }
- return cimg::type<double>::nan();
- }
- static double mp_vector_reverse(_cimg_math_parser& mp) {
- double *const ptrd = &_mp_arg(1) + 1;
- const double *const ptrs = &_mp_arg(2) + 1;
- const unsigned int p1 = (unsigned int)mp.opcode[3];
- CImg<doubleT>(ptrd,p1,1,1,1,true) = CImg<doubleT>(ptrs,p1,1,1,1,true).get_mirror('x');
- return cimg::type<double>::nan();
- }
- static double mp_vector_set_off(_cimg_math_parser& mp) {
- const unsigned int
- ptr = (unsigned int)mp.opcode[2] + 1,
- siz = (unsigned int)mp.opcode[3];
- const int off = (int)_mp_arg(4);
- if (off>=0 && off<(int)siz) mp.mem[ptr + off] = _mp_arg(1);
- return _mp_arg(1);
- }
- #define _cimg_mp_vfunc(func) \
- const longT sizd = (longT)mp.opcode[2];\
- const unsigned int nbargs = (unsigned int)(mp.opcode[3] - 4)/2; \
- double *const ptrd = &_mp_arg(1) + (sizd?1:0); \
- cimg_pragma_openmp(parallel cimg_openmp_if_size(sizd,256)) \
- { CImg<doubleT> vec(nbargs); double res; \
- cimg_pragma_openmp(for) for (longT k = sizd?sizd - 1:0; k>=0; --k) { \
- cimg_forX(vec,n) vec[n] = *(&_mp_arg(4 + 2*n) + (k+1)*(mp.opcode[4 + 2*n + 1]?1:0)); \
- func; ptrd[k] = res; \
- }} \
- return sizd?cimg::type<double>::nan():*ptrd;
- static double _mp_vargkth(CImg<doubleT>& vec) {
- const double val = (+vec).get_shared_points(1,vec.width() - 1).
- kth_smallest((ulongT)cimg::cut((longT)*vec - 1,(longT)0,(longT)vec.width() - 2));
- cimg_for_inX(vec,1,vec.width()-1,ind) if (vec[ind]==val) return ind - 1.;
- return 1.;
- }
- static double mp_vargkth(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = _mp_vargkth(vec));
- }
- static double mp_vargmax(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = (double)(&vec.max() - vec.data()));
- }
- static double mp_vargmaxabs(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = (double)(&vec.maxabs() - vec.data()));
- }
- static double mp_vargmin(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = (double)(&vec.min() - vec.data()));
- }
- static double mp_vargminabs(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = (double)(&vec.minabs() - vec.data()));
- }
- static double mp_vavg(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.mean());
- }
- static double mp_vkth(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.get_shared_points(1,vec.width() - 1).
- kth_smallest((ulongT)cimg::cut((longT)*vec - 1,(longT)0,(longT)vec.width() - 2)));
- }
- static double mp_vmax(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.max());
- }
- static double mp_vmaxabs(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.maxabs());
- }
- static double mp_vmedian(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.median());
- }
- static double mp_vmin(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.min());
- }
- static double mp_vminabs(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.minabs());
- }
- static double mp_vprod(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.product());
- }
- static double mp_vstd(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = std::sqrt(vec.get_stats()[3]));
- }
- static double mp_vsum(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.sum());
- }
- static double mp_vvar(_cimg_math_parser& mp) {
- _cimg_mp_vfunc(res = vec.get_stats()[3]);
- }
- static double mp_vtos(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- sizd = (unsigned int)mp.opcode[2],
- sizs = (unsigned int)mp.opcode[4];
- std::memset(ptrd,0,sizd*sizeof(double));
- const int nb_digits = (int)_mp_arg(5);
- CImg<charT> format(8);
- switch (nb_digits) {
- case -1 : std::strcpy(format,"%g"); break;
- case 0 : std::strcpy(format,"%.17g"); break;
- default : cimg_snprintf(format,format._width,"%%.%dg",nb_digits);
- }
- CImg<charT> str;
- if (sizs) { // Vector expression
- const double *ptrs = &_mp_arg(3) + 1;
- CImg<doubleT>(ptrs,sizs,1,1,1,true).value_string(',',sizd + 1,format).move_to(str);
- } else { // Scalar expression
- str.assign(sizd + 1);
- cimg_snprintf(str,sizd + 1,format,_mp_arg(3));
- }
- const unsigned int l = std::min(sizd,(unsigned int)std::strlen(str) + 1);
- CImg<doubleT>(ptrd,l,1,1,1,true) = str.get_shared_points(0,l - 1);
- return cimg::type<double>::nan();
- }
- static double mp_while(_cimg_math_parser& mp) {
- const ulongT
- mem_body = mp.opcode[1],
- mem_cond = mp.opcode[2];
- const CImg<ulongT>
- *const p_cond = ++mp.p_code,
- *const p_body = p_cond + mp.opcode[3],
- *const p_end = p_body + mp.opcode[4];
- const unsigned int vsiz = (unsigned int)mp.opcode[5];
- bool is_cond = false;
- if (mp.opcode[6]) { // Set default value for result and condition if necessary
- if (vsiz) CImg<doubleT>(&mp.mem[mem_body] + 1,vsiz,1,1,1,true).fill(cimg::type<double>::nan());
- else mp.mem[mem_body] = cimg::type<double>::nan();
- }
- if (mp.opcode[7]) mp.mem[mem_cond] = 0;
- const unsigned int _break_type = mp.break_type;
- mp.break_type = 0;
- do {
- for (mp.p_code = p_cond; mp.p_code<p_body; ++mp.p_code) { // Evaluate condition
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break;
- is_cond = (bool)mp.mem[mem_cond];
- if (is_cond && !mp.break_type) // Evaluate body
- for (mp.p_code = p_body; mp.p_code<p_end; ++mp.p_code) {
- mp.opcode._data = mp.p_code->_data;
- const ulongT target = mp.opcode[1];
- mp.mem[target] = _cimg_mp_defunc(mp);
- }
- if (mp.break_type==1) break; else if (mp.break_type==2) mp.break_type = 0;
- } while (is_cond);
- mp.break_type = _break_type;
- mp.p_code = p_end - 1;
- return mp.mem[mem_body];
- }
- static double mp_Ioff(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- boundary_conditions = (unsigned int)_mp_arg(3),
- vsiz = (unsigned int)mp.opcode[4];
- const CImg<T> &img = mp.imgin;
- const longT
- off = (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const T *ptrs;
- if (off>=0 && off<whd) {
- ptrs = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
- ptrs = &img[moff<whd?moff:whd2 - moff - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- case 2 : // Periodic
- ptrs = &img[cimg::mod(off,whd)];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- case 1 : // Neumann
- ptrs = off<0?&img[0]:&img[whd - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- default : // Dirichlet
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- static double mp_Ixyz(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- interpolation = (unsigned int)_mp_arg(5),
- boundary_conditions = (unsigned int)_mp_arg(6),
- vsiz = (unsigned int)mp.opcode[7];
- const CImg<T> &img = mp.imgin;
- const double x = _mp_arg(2), y = _mp_arg(3), z = _mp_arg(4);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- const T *ptrs;
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 2 : { // Periodic
- const int
- cx = (int)cimg::mod(x,(double)img._width),
- cy = (int)cimg::mod(y,(double)img._height),
- cz = (int)cimg::mod(z,(double)img._depth);
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 1 : { // Neumann
- ptrs = &img._atXYZ((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- default : // Dirichlet
- if (img.containsXYZC((int)x,(int)y,(int)z)) {
- ptrs = &img((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } else std::memset(ptrd,0,vsiz*sizeof(double));
- }
- }
- return cimg::type<double>::nan();
- }
- static double mp_Joff(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- boundary_conditions = (unsigned int)_mp_arg(3),
- vsiz = (unsigned int)mp.opcode[4];
- const CImg<T> &img = mp.imgin;
- const int
- ox = (int)mp.mem[_cimg_mp_slot_x],
- oy = (int)mp.mem[_cimg_mp_slot_y],
- oz = (int)mp.mem[_cimg_mp_slot_z];
- const longT
- off = img.offset(ox,oy,oz) + (longT)_mp_arg(2),
- whd = (longT)img.width()*img.height()*img.depth();
- const T *ptrs;
- if (off>=0 && off<whd) {
- ptrs = &img[off];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- if (img._data) switch (boundary_conditions) {
- case 3 : { // Mirror
- const longT whd2 = 2*whd, moff = cimg::mod(off,whd2);
- ptrs = &img[moff<whd?moff:whd2 - moff - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- }
- case 2 : // Periodic
- ptrs = &img[cimg::mod(off,whd)];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- case 1 : // Neumann
- ptrs = off<0?&img[0]:&img[whd - 1];
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return cimg::type<double>::nan();
- default : // Dirichlet
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- std::memset(ptrd,0,vsiz*sizeof(double));
- return cimg::type<double>::nan();
- }
- static double mp_Jxyz(_cimg_math_parser& mp) {
- double *ptrd = &_mp_arg(1) + 1;
- const unsigned int
- interpolation = (unsigned int)_mp_arg(5),
- boundary_conditions = (unsigned int)_mp_arg(6),
- vsiz = (unsigned int)mp.opcode[7];
- const CImg<T> &img = mp.imgin;
- const double
- ox = mp.mem[_cimg_mp_slot_x], oy = mp.mem[_cimg_mp_slot_y], oz = mp.mem[_cimg_mp_slot_z],
- x = ox + _mp_arg(2), y = oy + _mp_arg(3), z = oz + _mp_arg(4);
- const ulongT whd = (ulongT)img._width*img._height*img._depth;
- const T *ptrs;
- switch (interpolation) {
- case 2 : // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._cubic_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.cubic_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- case 1 : // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float
- w2 = 2.f*img.width(), h2 = 2.f*img.height(), d2 = 2.f*img.depth(),
- mx = cimg::mod((float)x,w2), my = cimg::mod((float)y,h2), mz = cimg::mod((float)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ(cx,cy,cz,c);
- } break;
- case 2 : // Periodic
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ_p((float)x,(float)y,(float)z,c);
- break;
- case 1 : // Neumann
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img._linear_atXYZ((float)x,(float)y,(float)z,c);
- break;
- default : // Dirichlet
- cimg_for_inC(img,0,vsiz - 1,c) *(ptrd++) = (double)img.linear_atXYZ((float)x,(float)y,(float)z,c,(T)0);
- } break;
- default : // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int
- w2 = 2*img.width(), h2 = 2*img.height(), d2 = 2*img.depth(),
- mx = cimg::mod((int)x,w2), my = cimg::mod((int)y,h2), mz = cimg::mod((int)z,d2),
- cx = mx<img.width()?mx:w2 - mx - 1,
- cy = my<img.height()?my:h2 - my - 1,
- cz = mz<img.depth()?mz:d2 - mz - 1;
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 2 : { // Periodic
- const int
- cx = (int)cimg::mod(x,(double)img._width),
- cy = (int)cimg::mod(y,(double)img._height),
- cz = (int)cimg::mod(z,(double)img._depth);
- ptrs = &img(cx,cy,cz);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- case 1 : { // Neumann
- ptrs = &img._atXYZ((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } break;
- default : // Dirichlet
- if (img.containsXYZC((int)x,(int)y,(int)z)) {
- ptrs = &img((int)x,(int)y,(int)z);
- cimg_for_inC(img,0,vsiz - 1,c) { *(ptrd++) = (double)*ptrs; ptrs+=whd; }
- } else std::memset(ptrd,0,vsiz*sizeof(double));
- }
- }
- return cimg::type<double>::nan();
- }
- #undef _mp_arg
- }; // struct _cimg_math_parser {}
- #define _cimg_create_pointwise_functions(name,func,min_size) \
- CImg<T>& name() { \
- if (is_empty()) return *this; \
- cimg_openmp_for(*this,func((typename cimg::superset<T,float>::type)*ptr),min_size); \
- return *this; \
- } \
- CImg<Tfloat> get_##name() const { \
- return CImg<Tfloat>(*this,false).name(); \
- }
- //! Compute the square value of each pixel value.
- /**
- 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$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- \par Example
- \code
- const CImg<float> img("reference.jpg");
- (img,img.get_sqr().normalize(0,255)).display();
- \endcode
- \image html ref_sqr.jpg
- **/
- _cimg_create_pointwise_functions(sqr,cimg::sqr,524288)
- //! Compute the square root of each pixel value.
- /**
- 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$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- \par Example
- \code
- const CImg<float> img("reference.jpg");
- (img,img.get_sqrt().normalize(0,255)).display();
- \endcode
- \image html ref_sqrt.jpg
- **/
- _cimg_create_pointwise_functions(sqrt,std::sqrt,8192)
- //! Compute the exponential of each pixel value.
- /**
- 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$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(exp,std::exp,4096)
- //! Compute the error function of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its error function.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(erf,std::erf,4096)
- //! Compute the logarithm of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its logarithm
- \f$\mathrm{log}_{e}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(log,std::log,262144)
- //! Compute the base-2 logarithm of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-2 logarithm
- \f$\mathrm{log}_{2}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(log2,cimg::log2,4096)
- //! Compute the base-10 logarithm of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its base-10 logarithm
- \f$\mathrm{log}_{10}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(log10,std::log10,4096)
- //! Compute the absolute value of each pixel value.
- /**
- 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$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(abs,cimg::abs,524288)
- //! Compute the sign of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sign
- \f$\mathrm{sign}(I_{(x,y,z,c)})\f$.
- \note
- - The sign is set to:
- - \c 1 if pixel value is strictly positive.
- - \c -1 if pixel value is strictly negative.
- - \c 0 if pixel value is equal to \c 0.
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(sign,cimg::sign,32768)
- //! Compute the cosine of each pixel value.
- /**
- 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$.
- \note
- - Pixel values are regarded as being in \e radian.
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(cos,std::cos,8192)
- //! Compute the sine of each pixel value.
- /**
- 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$.
- \note
- - Pixel values are regarded as being in \e radian.
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(sin,std::sin,8192)
- //! Compute the sinc of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its sinc
- \f$\mathrm{sinc}(I_{(x,y,z,c)})\f$.
- \note
- - Pixel values are regarded as being exin \e radian.
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(sinc,cimg::sinc,2048)
- //! Compute the tangent of each pixel value.
- /**
- 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$.
- \note
- - Pixel values are regarded as being exin \e radian.
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(tan,std::tan,2048)
- //! Compute the hyperbolic cosine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic cosine
- \f$\mathrm{cosh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(cosh,std::cosh,2048)
- //! Compute the hyperbolic sine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic sine
- \f$\mathrm{sinh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(sinh,std::sinh,2048)
- //! Compute the hyperbolic tangent of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic tangent
- \f$\mathrm{tanh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(tanh,std::tanh,2048)
- //! Compute the arccosine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosine
- \f$\mathrm{acos}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(acos,std::acos,8192)
- //! Compute the arcsine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arcsine
- \f$\mathrm{asin}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(asin,std::asin,8192)
- //! Compute the arctangent of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent
- \f$\mathrm{atan}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(atan,std::atan,8192)
- //! Compute the arctangent2 of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arctangent2
- \f$\mathrm{atan2}(I_{(x,y,z,c)})\f$.
- \param img Image whose pixel values specify the second argument of the \c atan2() function.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- \par Example
- \code
- const CImg<float>
- img_x(100,100,1,1,"x-w/2",false), // Define an horizontal centered gradient, from '-width/2' to 'width/2'
- img_y(100,100,1,1,"y-h/2",false), // Define a vertical centered gradient, from '-height/2' to 'height/2'
- img_atan2 = img_y.get_atan2(img_x); // Compute atan2(y,x) for each pixel value
- (img_x,img_y,img_atan2).display();
- \endcode
- **/
- template<typename t>
- CImg<T>& atan2(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return atan2(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)std::atan2((double)*ptrd,(double)*(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)std::atan2((double)*ptrd,(double)*(ptrs++));
- }
- return *this;
- }
- //! Compute the arctangent2 of each pixel value \newinstance.
- template<typename t>
- CImg<Tfloat> get_atan2(const CImg<t>& img) const {
- return CImg<Tfloat>(*this,false).atan2(img);
- }
- //! Compute the hyperbolic arccosine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its arccosineh
- \f$\mathrm{acosh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(acosh,cimg::acosh,8192)
- //! Compute the hyperbolic arcsine of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic arcsine
- \f$\mathrm{asinh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(asinh,cimg::asinh,8192)
- //! Compute the hyperbolic arctangent of each pixel value.
- /**
- Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by its hyperbolic arctangent
- \f$\mathrm{atanh}(I_{(x,y,z,c)})\f$.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- **/
- _cimg_create_pointwise_functions(atanh,cimg::atanh,8192)
- //! In-place pointwise multiplication.
- /**
- Compute the pointwise multiplication between the image instance and the specified input image \c img.
- \param img Input image, as the second operand of the multiplication.
- \note
- - Similar to operator+=(const CImg<t>&), except that it performs a pointwise multiplication
- instead of an addition.
- - It does \e not perform a \e matrix multiplication. For this purpose, use operator*=(const CImg<t>&) instead.
- \par Example
- \code
- CImg<float>
- img("reference.jpg"),
- shade(img.width,img.height(),1,1,"-(x-w/2)^2-(y-h/2)^2",false);
- shade.normalize(0,1);
- (img,shade,img.get_mul(shade)).display();
- \endcode
- **/
- template<typename t>
- CImg<T>& mul(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return mul(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)(*ptrd * *(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd * *(ptrs++));
- }
- return *this;
- }
- //! In-place pointwise multiplication \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_mul(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).mul(img);
- }
- //! In-place pointwise division.
- /**
- Similar to mul(const CImg<t>&), except that it performs a pointwise division instead of a multiplication.
- **/
- template<typename t>
- CImg<T>& div(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return div(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)(*ptrd / *(ptrs++));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)(*ptrd / *(ptrs++));
- }
- return *this;
- }
- //! In-place pointwise division \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_div(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).div(img);
- }
- //! Raise each pixel value to a specified power.
- /**
- 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$.
- \param p Exponent value.
- \note
- - The \inplace of this method statically casts the computed values to the pixel type \c T.
- - The \newinstance returns a \c CImg<float> image, if the pixel type \c T is \e not float-valued.
- \par Example
- \code
- const CImg<float>
- img0("reference.jpg"), // Load reference color image
- img1 = (img0/255).pow(1.8)*=255, // Compute gamma correction, with gamma = 1.8
- img2 = (img0/255).pow(0.5)*=255; // Compute gamma correction, with gamma = 0.5
- (img0,img1,img2).display();
- \endcode
- **/
- CImg<T>& pow(const double p) {
- if (is_empty()) return *this;
- if (p==-4) { cimg_openmp_for(*this,1/(Tfloat)cimg::pow4(*ptr),32768); return *this; }
- if (p==-3) { cimg_openmp_for(*this,1/(Tfloat)cimg::pow3(*ptr),32768); return *this; }
- if (p==-2) { cimg_openmp_for(*this,1/(Tfloat)cimg::sqr(*ptr),32768); return *this; }
- if (p==-1) { cimg_openmp_for(*this,1/(Tfloat)*ptr,32768); return *this; }
- if (p==-0.5) { cimg_openmp_for(*this,1/std::sqrt((Tfloat)*ptr),8192); return *this; }
- if (p==0) return fill((T)1);
- if (p==0.5) return sqrt();
- if (p==1) return *this;
- if (p==2) return sqr();
- if (p==3) { cimg_openmp_for(*this,cimg::pow3(*ptr),262144); return *this; }
- if (p==4) { cimg_openmp_for(*this,cimg::pow4(*ptr),131072); return *this; }
- cimg_openmp_for(*this,std::pow((Tfloat)*ptr,(Tfloat)p),1024);
- return *this;
- }
- //! Raise each pixel value to a specified power \newinstance.
- CImg<Tfloat> get_pow(const double p) const {
- return CImg<Tfloat>(*this,false).pow(p);
- }
- //! Raise each pixel value to a power, specified from an expression.
- /**
- Similar to operator+=(const char*), except it performs a pointwise exponentiation instead of an addition.
- **/
- CImg<T>& pow(const char *const expression) {
- return pow((+*this)._fill(expression,true,1,0,0,"pow",this));
- }
- //! Raise each pixel value to a power, specified from an expression \newinstance.
- CImg<Tfloat> get_pow(const char *const expression) const {
- return CImg<Tfloat>(*this,false).pow(expression);
- }
- //! Raise each pixel value to a power, pointwisely specified from another image.
- /**
- Similar to operator+=(const CImg<t>& img), except that it performs an exponentiation instead of an addition.
- **/
- template<typename t>
- CImg<T>& pow(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return pow(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)std::pow((double)*ptrd,(double)(*(ptrs++)));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)std::pow((double)*ptrd,(double)(*(ptrs++)));
- }
- return *this;
- }
- //! Raise each pixel value to a power, pointwisely specified from another image \newinstance.
- template<typename t>
- CImg<Tfloat> get_pow(const CImg<t>& img) const {
- return CImg<Tfloat>(*this,false).pow(img);
- }
- //! Compute the bitwise left rotation of each pixel value.
- /**
- Similar to operator<<=(unsigned int), except that it performs a left rotation instead of a left shift.
- **/
- CImg<T>& rol(const unsigned int n=1) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,cimg::rol(*ptr,n),32768);
- return *this;
- }
- //! Compute the bitwise left rotation of each pixel value \newinstance.
- CImg<T> get_rol(const unsigned int n=1) const {
- return (+*this).rol(n);
- }
- //! Compute the bitwise left rotation of each pixel value.
- /**
- Similar to operator<<=(const char*), except that it performs a left rotation instead of a left shift.
- **/
- CImg<T>& rol(const char *const expression) {
- return rol((+*this)._fill(expression,true,1,0,0,"rol",this));
- }
- //! Compute the bitwise left rotation of each pixel value \newinstance.
- CImg<T> get_rol(const char *const expression) const {
- return (+*this).rol(expression);
- }
- //! Compute the bitwise left rotation of each pixel value.
- /**
- Similar to operator<<=(const CImg<t>&), except that it performs a left rotation instead of a left shift.
- **/
- template<typename t>
- CImg<T>& rol(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return rol(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)cimg::rol(*ptrd,(unsigned int)(*(ptrs++)));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)cimg::rol(*ptrd,(unsigned int)(*(ptrs++)));
- }
- return *this;
- }
- //! Compute the bitwise left rotation of each pixel value \newinstance.
- template<typename t>
- CImg<T> get_rol(const CImg<t>& img) const {
- return (+*this).rol(img);
- }
- //! Compute the bitwise right rotation of each pixel value.
- /**
- Similar to operator>>=(unsigned int), except that it performs a right rotation instead of a right shift.
- **/
- CImg<T>& ror(const unsigned int n=1) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,cimg::ror(*ptr,n),32768);
- return *this;
- }
- //! Compute the bitwise right rotation of each pixel value \newinstance.
- CImg<T> get_ror(const unsigned int n=1) const {
- return (+*this).ror(n);
- }
- //! Compute the bitwise right rotation of each pixel value.
- /**
- Similar to operator>>=(const char*), except that it performs a right rotation instead of a right shift.
- **/
- CImg<T>& ror(const char *const expression) {
- return ror((+*this)._fill(expression,true,1,0,0,"ror",this));
- }
- //! Compute the bitwise right rotation of each pixel value \newinstance.
- CImg<T> get_ror(const char *const expression) const {
- return (+*this).ror(expression);
- }
- //! Compute the bitwise right rotation of each pixel value.
- /**
- Similar to operator>>=(const CImg<t>&), except that it performs a right rotation instead of a right shift.
- **/
- template<typename t>
- CImg<T>& ror(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return ror(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = (T)cimg::ror(*ptrd,(unsigned int)(*(ptrs++)));
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = (T)cimg::ror(*ptrd,(unsigned int)(*(ptrs++)));
- }
- return *this;
- }
- //! Compute the bitwise right rotation of each pixel value \newinstance.
- template<typename t>
- CImg<T> get_ror(const CImg<t>& img) const {
- return (+*this).ror(img);
- }
- //! Pointwise min operator between instance image and a value.
- /**
- \param val Value used as the reference argument of the min operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{val})\f$.
- **/
- CImg<T>& min(const T& value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,std::min(*ptr,value),65536);
- return *this;
- }
- //! Pointwise min operator between instance image and a value \newinstance.
- CImg<T> get_min(const T& value) const {
- return (+*this).min(value);
- }
- //! Pointwise min operator between two images.
- /**
- \param img Image used as the reference argument of the min operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
- **/
- template<typename t>
- CImg<T>& min(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return min(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = std::min((T)*(ptrs++),*ptrd);
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = std::min((T)*(ptrs++),*ptrd);
- }
- return *this;
- }
- //! Pointwise min operator between two images \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_min(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).min(img);
- }
- //! Pointwise min operator between an image and an expression.
- /**
- \param expression Math formula as a C-string.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{min}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
- **/
- CImg<T>& min(const char *const expression) {
- return min((+*this)._fill(expression,true,1,0,0,"min",this));
- }
- //! Pointwise min operator between an image and an expression \newinstance.
- CImg<Tfloat> get_min(const char *const expression) const {
- return CImg<Tfloat>(*this,false).min(expression);
- }
- //! Pointwise max operator between instance image and a value.
- /**
- \param val Value used as the reference argument of the max operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{val})\f$.
- **/
- CImg<T>& max(const T& value) {
- if (is_empty()) return *this;
- cimg_openmp_for(*this,std::max(*ptr,value),65536);
- return *this;
- }
- //! Pointwise max operator between instance image and a value \newinstance.
- CImg<T> get_max(const T& value) const {
- return (+*this).max(value);
- }
- //! Pointwise max operator between two images.
- /**
- \param img Image used as the reference argument of the max operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
- **/
- template<typename t>
- CImg<T>& max(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return max(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = std::max((T)*(ptrs++),*ptrd);
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = std::max((T)*(ptrs++),*ptrd);
- }
- return *this;
- }
- //! Pointwise max operator between two images \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_max(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).max(img);
- }
- //! Pointwise max operator between an image and an expression.
- /**
- \param expression Math formula as a C-string.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{max}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
- **/
- CImg<T>& max(const char *const expression) {
- return max((+*this)._fill(expression,true,1,0,0,"max",this));
- }
- //! Pointwise max operator between an image and an expression \newinstance.
- CImg<Tfloat> get_max(const char *const expression) const {
- return CImg<Tfloat>(*this,false).max(expression);
- }
- //! Pointwise minabs operator between instance image and a value.
- /**
- \param val Value used as the reference argument of the minabs operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{val})\f$.
- **/
- CImg<T>& minabs(const T& value) {
- if (is_empty()) return *this;
- const T absvalue = cimg::abs(value);
- cimg_openmp_for(*this,cimg::minabs(*ptr,value,absvalue),65536);
- return *this;
- }
- //! Pointwise minabs operator between instance image and a value \newinstance.
- CImg<T> get_minabs(const T& value) const {
- return (+*this).minabs(value);
- }
- //! Pointwise minabs operator between two images.
- /**
- \param img Image used as the reference argument of the minabs operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
- **/
- template<typename t>
- CImg<T>& minabs(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return minabs(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = cimg::minabs((T)*(ptrs++),*ptrd);
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::minabs((T)*(ptrs++),*ptrd);
- }
- return *this;
- }
- //! Pointwise minabs operator between two images \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_minabs(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).minabs(img);
- }
- //! Pointwise minabs operator between an image and an expression.
- /**
- \param expression Math formula as a C-string.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{minabs}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
- **/
- CImg<T>& minabs(const char *const expression) {
- return minabs((+*this)._fill(expression,true,1,0,0,"minabs",this));
- }
- //! Pointwise minabs operator between an image and an expression \newinstance.
- CImg<Tfloat> get_minabs(const char *const expression) const {
- return CImg<Tfloat>(*this,false).minabs(expression);
- }
- //! Pointwise maxabs operator between instance image and a value.
- /**
- \param val Value used as the reference argument of the maxabs operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{val})\f$.
- **/
- CImg<T>& maxabs(const T& value) {
- if (is_empty()) return *this;
- const T absvalue = cimg::abs(value);
- cimg_openmp_for(*this,cimg::maxabs(*ptr,value,absvalue),65536);
- return *this;
- }
- //! Pointwise maxabs operator between instance image and a value \newinstance.
- CImg<T> get_maxabs(const T& value) const {
- return (+*this).maxabs(value);
- }
- //! Pointwise maxabs operator between two images.
- /**
- \param img Image used as the reference argument of the maxabs operator.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{img}_{(x,y,z,c)})\f$.
- **/
- template<typename t>
- CImg<T>& maxabs(const CImg<t>& img) {
- const ulongT siz = size(), isiz = img.size();
- if (siz && isiz) {
- if (is_overlapped(img)) return maxabs(+img);
- T *ptrd = _data, *const ptre = _data + siz;
- if (siz>isiz) for (ulongT n = siz/isiz; n; --n)
- for (const t *ptrs = img._data, *ptrs_end = ptrs + isiz; ptrs<ptrs_end; ++ptrd)
- *ptrd = cimg::maxabs((T)*(ptrs++),*ptrd);
- for (const t *ptrs = img._data; ptrd<ptre; ++ptrd) *ptrd = cimg::maxabs((T)*(ptrs++),*ptrd);
- }
- return *this;
- }
- //! Pointwise maxabs operator between two images \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_maxabs(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this,false).maxabs(img);
- }
- //! Pointwise maxabs operator between an image and an expression.
- /**
- \param expression Math formula as a C-string.
- \note Replace each pixel value \f$I_{(x,y,z,c)}\f$ of the image instance by
- \f$\mathrm{maxabs}(I_{(x,y,z,c)},\mathrm{expr}_{(x,y,z,c)})\f$.
- **/
- CImg<T>& maxabs(const char *const expression) {
- return maxabs((+*this)._fill(expression,true,1,0,0,"maxabs",this));
- }
- //! Pointwise maxabs operator between an image and an expression \newinstance.
- CImg<Tfloat> get_maxabs(const char *const expression) const {
- return CImg<Tfloat>(*this,false).maxabs(expression);
- }
- //! Return a reference to the minimum pixel value.
- /**
- **/
- T& min() {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "min(): Empty instance.",
- cimg_instance);
- T *ptr_min = _data;
- T min_value = *ptr_min;
- cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value \const.
- const T& min() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "min(): Empty instance.",
- cimg_instance);
- const T *ptr_min = _data;
- T min_value = *ptr_min;
- cimg_for(*this,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value in absolute value.
- /**
- **/
- T& minabs() {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "minabs(): Empty instance.",
- cimg_instance);
- T *ptr_minabs = _data;
- T minabs_value = *ptr_minabs;
- cimg_for(*this,ptrs,T) {
- const T ma = cimg::abs(*ptrs);
- if (ma<minabs_value) { minabs_value = ma; ptr_minabs = ptrs; }
- }
- return *ptr_minabs;
- }
- //! Return a reference to the minimum pixel value in absolute value \const.
- const T& minabs() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "minabs(): Empty instance.",
- cimg_instance);
- const T *ptr_minabs = _data;
- T minabs_value = *ptr_minabs;
- cimg_for(*this,ptrs,T) {
- const T ma = cimg::abs(*ptrs);
- if (ma<minabs_value) { minabs_value = ma; ptr_minabs = ptrs; }
- }
- return *ptr_minabs;
- }
- //! Return a reference to the maximum pixel value.
- /**
- **/
- T& max() {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "max(): Empty instance.",
- cimg_instance);
- T *ptr_max = _data;
- T max_value = *ptr_max;
- cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
- return *ptr_max;
- }
- //! Return a reference to the maximum pixel value \const.
- const T& max() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "max(): Empty instance.",
- cimg_instance);
- const T *ptr_max = _data;
- T max_value = *ptr_max;
- cimg_for(*this,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
- return *ptr_max;
- }
- //! Return a reference to the maximum pixel value in absolute value.
- /**
- **/
- T& maxabs() {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "maxabs(): Empty instance.",
- cimg_instance);
- T *ptr_maxabs = _data;
- T maxabs_value = *ptr_maxabs;
- cimg_for(*this,ptrs,T) {
- const T ma = cimg::abs(*ptrs);
- if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }
- }
- return *ptr_maxabs;
- }
- //! Return a reference to the maximum pixel value in absolute value \const.
- const T& maxabs() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "maxabs(): Empty instance.",
- cimg_instance);
- const T *ptr_maxabs = _data;
- T maxabs_value = *ptr_maxabs;
- cimg_for(*this,ptrs,T) {
- const T ma = cimg::abs(*ptrs);
- if (ma>maxabs_value) { maxabs_value = ma; ptr_maxabs = ptrs; }
- }
- return *ptr_maxabs;
- }
- //! Return a reference to the minimum pixel value as well as the maximum pixel value.
- /**
- \param[out] max_val Maximum pixel value.
- **/
- template<typename t>
- T& min_max(t& max_val) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "min_max(): Empty instance.",
- cimg_instance);
- T *ptr_min = _data;
- T min_value = *ptr_min, max_value = min_value;
- cimg_for(*this,ptrs,T) {
- const T val = *ptrs;
- if (val<min_value) { min_value = val; ptr_min = ptrs; }
- if (val>max_value) max_value = val;
- }
- max_val = (t)max_value;
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value as well as the maximum pixel value \const.
- template<typename t>
- const T& min_max(t& max_val) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "min_max(): Empty instance.",
- cimg_instance);
- const T *ptr_min = _data;
- T min_value = *ptr_min, max_value = min_value;
- cimg_for(*this,ptrs,T) {
- const T val = *ptrs;
- if (val<min_value) { min_value = val; ptr_min = ptrs; }
- if (val>max_value) max_value = val;
- }
- max_val = (t)max_value;
- return *ptr_min;
- }
- //! Return a reference to the maximum pixel value as well as the minimum pixel value.
- /**
- \param[out] min_val Minimum pixel value.
- **/
- template<typename t>
- T& max_min(t& min_val) {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "max_min(): Empty instance.",
- cimg_instance);
- T *ptr_max = _data;
- T max_value = *ptr_max, min_value = max_value;
- cimg_for(*this,ptrs,T) {
- const T val = *ptrs;
- if (val>max_value) { max_value = val; ptr_max = ptrs; }
- if (val<min_value) min_value = val;
- }
- min_val = (t)min_value;
- return *ptr_max;
- }
- //! Return a reference to the maximum pixel value as well as the minimum pixel value \const.
- template<typename t>
- const T& max_min(t& min_val) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "max_min(): Empty instance.",
- cimg_instance);
- const T *ptr_max = _data;
- T max_value = *ptr_max, min_value = max_value;
- cimg_for(*this,ptrs,T) {
- const T val = *ptrs;
- if (val>max_value) { max_value = val; ptr_max = ptrs; }
- if (val<min_value) min_value = val;
- }
- min_val = (t)min_value;
- return *ptr_max;
- }
- //! Return the kth smallest pixel value.
- /**
- \param k Rank of the smallest element searched.
- **/
- T kth_smallest(const ulongT k) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "kth_smallest(): Empty instance.",
- cimg_instance);
- if (k>=size()) return max();
- CImg<T> arr(*this,false);
- ulongT l = 0, ir = size() - 1;
- for ( ; ; ) {
- if (ir<=l + 1) {
- if (ir==l + 1 && arr[ir]<arr[l]) cimg::swap(arr[l],arr[ir]);
- return arr[k];
- } else {
- const ulongT mid = (l + ir)>>1;
- cimg::swap(arr[mid],arr[l + 1]);
- if (arr[l]>arr[ir]) cimg::swap(arr[l],arr[ir]);
- if (arr[l + 1]>arr[ir]) cimg::swap(arr[l + 1],arr[ir]);
- if (arr[l]>arr[l + 1]) cimg::swap(arr[l],arr[l + 1]);
- ulongT i = l + 1, j = ir;
- const T pivot = arr[l + 1];
- for ( ; ; ) {
- do ++i; while (arr[i]<pivot);
- do --j; while (arr[j]>pivot);
- if (j<i) break;
- cimg::swap(arr[i],arr[j]);
- }
- arr[l + 1] = arr[j];
- arr[j] = pivot;
- if (j>=k) ir = j - 1;
- if (j<=k) l = i;
- }
- }
- }
- //! Return the median pixel value.
- /**
- **/
- T median() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "median(): Empty instance.",
- cimg_instance);
- const ulongT s = size();
- switch (s) {
- case 1 : return _data[0];
- case 2 : return cimg::median(_data[0],_data[1]);
- case 3 : return cimg::median(_data[0],_data[1],_data[2]);
- case 5 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4]);
- case 7 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6]);
- case 9 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8]);
- case 13 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8],
- _data[9],_data[10],_data[11],_data[12]);
- }
- const T res = kth_smallest(s>>1);
- return (s%2)?res:(T)((res + kth_smallest((s>>1) - 1))/2);
- }
- //! Return the product of all the pixel values.
- /**
- **/
- double product() const {
- if (is_empty()) return 0;
- double res = 1;
- cimg_for(*this,ptrs,T) res*=(double)*ptrs;
- return res;
- }
- //! Return the sum of all the pixel values.
- /**
- **/
- double sum() const {
- double res = 0;
- cimg_for(*this,ptrs,T) res+=(double)*ptrs;
- return res;
- }
- //! Return the average pixel value.
- /**
- **/
- double mean() const {
- double res = 0;
- cimg_for(*this,ptrs,T) res+=(double)*ptrs;
- return res/size();
- }
- //! Return the variance of the pixel values.
- /**
- \param variance_method Method used to estimate the variance. Can be:
- - \c 0: Second moment, computed as
- \f$1/N \sum\limits_{k=1}^{N} (x_k - \bar x)^2 =
- 1/N \left( \sum\limits_{k=1}^N x_k^2 - \left( \sum\limits_{k=1}^N x_k \right)^2 / N \right)\f$
- with \f$ \bar x = 1/N \sum\limits_{k=1}^N x_k \f$.
- - \c 1: Best unbiased estimator, computed as \f$\frac{1}{N - 1} \sum\limits_{k=1}^{N} (x_k - \bar x)^2 \f$.
- - \c 2: Least median of squares.
- - \c 3: Least trimmed of squares.
- **/
- double variance(const unsigned int variance_method=1) const {
- double foo;
- return variance_mean(variance_method,foo);
- }
- //! Return the variance as well as the average of the pixel values.
- /**
- \param variance_method Method used to estimate the variance (see variance(const unsigned int) const).
- \param[out] mean Average pixel value.
- **/
- template<typename t>
- double variance_mean(const unsigned int variance_method, t& mean) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "variance_mean(): Empty instance.",
- cimg_instance);
- double variance = 0, average = 0;
- const ulongT siz = size();
- switch (variance_method) {
- case 0 : { // Least mean square (standard definition)
- double S = 0, S2 = 0;
- cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
- variance = (S2 - S*S/siz)/siz;
- average = S;
- } break;
- case 1 : { // Least mean square (robust definition)
- double S = 0, S2 = 0;
- cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
- variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
- average = S;
- } break;
- case 2 : { // Least Median of Squares (MAD)
- CImg<Tfloat> buf(*this,false);
- buf.sort();
- const ulongT siz2 = siz>>1;
- const double med_i = (double)buf[siz2];
- cimg_for(buf,ptrs,Tfloat) {
- const double val = (double)*ptrs; *ptrs = (Tfloat)cimg::abs(val - med_i); average+=val;
- }
- buf.sort();
- const double sig = (double)(1.4828*buf[siz2]);
- variance = sig*sig;
- } break;
- default : { // Least trimmed of Squares
- CImg<Tfloat> buf(*this,false);
- const ulongT siz2 = siz>>1;
- cimg_for(buf,ptrs,Tfloat) {
- const double val = (double)*ptrs; (*ptrs)=(Tfloat)((*ptrs)*val); average+=val;
- }
- buf.sort();
- double a = 0;
- const Tfloat *ptrs = buf._data;
- for (ulongT j = 0; j<siz2; ++j) a+=(double)*(ptrs++);
- const double sig = (double)(2.6477*std::sqrt(a/siz2));
- variance = sig*sig;
- }
- }
- mean = (t)(average/siz);
- return variance>0?variance:0;
- }
- //! Return estimated variance of the noise.
- /**
- \param variance_method Method used to compute the variance (see variance(const unsigned int) const).
- \note Because of structures such as edges in images it is
- recommended to use a robust variance estimation. The variance of the
- noise is estimated by computing the variance of the Laplacian \f$(\Delta
- I)^2 \f$ scaled by a factor \f$c\f$ insuring \f$ c E[(\Delta I)^2]=
- \sigma^2\f$ where \f$\sigma\f$ is the noise variance.
- **/
- double variance_noise(const unsigned int variance_method=2) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "variance_noise(): Empty instance.",
- cimg_instance);
- const ulongT siz = size();
- if (!siz || !_data) return 0;
- if (variance_method>1) { // Compute a scaled version of the Laplacian
- CImg<Tdouble> tmp(*this,false);
- if (_depth==1) {
- const double cste = 1./std::sqrt(20.); // Depends on how the Laplacian is computed
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*262144 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- CImg_3x3(I,T);
- cimg_for3x3(*this,x,y,0,c,I,T) {
- tmp(x,y,c) = cste*((double)Inc + (double)Ipc + (double)Icn +
- (double)Icp - 4*(double)Icc);
- }
- }
- } else {
- const double cste = 1./std::sqrt(42.); // Depends on how the Laplacian is computed
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*262144 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- CImg_3x3x3(I,T);
- cimg_for3x3x3(*this,x,y,z,c,I,T) {
- tmp(x,y,z,c) = cste*(
- (double)Incc + (double)Ipcc + (double)Icnc + (double)Icpc +
- (double)Iccn + (double)Iccp - 6*(double)Iccc);
- }
- }
- }
- return tmp.variance(variance_method);
- }
- // Version that doesn't need intermediate images.
- double variance = 0, S = 0, S2 = 0;
- if (_depth==1) {
- const double cste = 1./std::sqrt(20.);
- CImg_3x3(I,T);
- cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,T) {
- const double val = cste*((double)Inc + (double)Ipc +
- (double)Icn + (double)Icp - 4*(double)Icc);
- S+=val; S2+=val*val;
- }
- } else {
- const double cste = 1./std::sqrt(42.);
- CImg_3x3x3(I,T);
- cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,T) {
- const double val = cste *
- ((double)Incc + (double)Ipcc + (double)Icnc +
- (double)Icpc +
- (double)Iccn + (double)Iccp - 6*(double)Iccc);
- S+=val; S2+=val*val;
- }
- }
- if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
- else variance = (S2 - S*S/siz)/siz;
- return variance>0?variance:0;
- }
- //! Compute the MSE (Mean-Squared Error) between two images.
- /**
- \param img Image used as the second argument of the MSE operator.
- **/
- template<typename t>
- double MSE(const CImg<t>& img) const {
- if (img.size()!=size())
- throw CImgArgumentException(_cimg_instance
- "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.",
- cimg_instance,
- img._width,img._height,img._depth,img._spectrum,img._data);
- double vMSE = 0;
- const t* ptr2 = img._data;
- cimg_for(*this,ptr1,T) {
- const double diff = (double)*ptr1 - (double)*(ptr2++);
- vMSE+=diff*diff;
- }
- const ulongT siz = img.size();
- if (siz) vMSE/=siz;
- return vMSE;
- }
- //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images.
- /**
- \param img Image used as the second argument of the PSNR operator.
- \param max_value Maximum theoretical value of the signal.
- **/
- template<typename t>
- double PSNR(const CImg<t>& img, const double max_value=255) const {
- const double vMSE = (double)std::sqrt(MSE(img));
- return (vMSE!=0)?(double)(20*std::log10(max_value/vMSE)):(double)(cimg::type<double>::max());
- }
- //! Evaluate math formula.
- /**
- \param expression Math formula, as a C-string.
- \param x Value of the pre-defined variable \c x.
- \param y Value of the pre-defined variable \c y.
- \param z Value of the pre-defined variable \c z.
- \param c Value of the pre-defined variable \c c.
- \param list_images A list of images attached to the specified math formula.
- **/
- double eval(const char *const expression,
- const double x=0, const double y=0, const double z=0, const double c=0,
- CImgList<T> *const list_images=0) {
- return _eval(this,expression,x,y,z,c,list_images);
- }
- //! Evaluate math formula \const.
- double eval(const char *const expression,
- const double x=0, const double y=0, const double z=0, const double c=0,
- CImgList<T> *const list_images=0) const {
- return _eval(0,expression,x,y,z,c,list_images);
- }
- // Fast function to pre-evaluate common expressions.
- // (return 'true' in case of success, and set value of 'res').
- template<typename t>
- bool __eval(const char *const expression, t &res) const {
- if (!expression || !*expression) { res = (t)0; return true; }
- const char c = *expression;
- bool is_success = false;
- char sep, end;
- double val,val2;
- int err;
- if ((c>='0' && c<='9') || c=='.') { // Possible value
- if (!expression[1]) { // Single digit
- res = (t)(c - '0');
- is_success = true;
- } else if ((err = std::sscanf(expression,"%lf %c%lf %c",&val,&sep,&val2,&end))==1) { // Single value
- res = (t)val;
- is_success = true;
- } else if (err==3) { // Value1 Operator Value2
- switch (sep) {
- case '+' : res = (t)(val + val2); is_success = true; break;
- case '-' : res = (t)(val - val2); is_success = true; break;
- case '*' : res = (t)(val*val2); is_success = true; break;
- case '/' : res = (t)(val/val2); is_success = true; break;
- case '%' : res = (t)cimg::mod(val,val2); is_success = true; break;
- case '&' : res = (t)((long)val & (long)val2); is_success = true; break;
- case '|' : res = (t)((long)val | (long)val2); is_success = true; break;
- case '>' : res = (t)(val>val2); is_success = true; break;
- case '<' : res = (t)(val<val2); is_success = true; break;
- case ';' : res = (t)val2; is_success = true; break;
- case '^' : res = (t)std::pow(val,val2); is_success = true; break;
- }
- }
- } else if ((c=='+' || c=='-' || c=='!') && // +Value, -Value or !Value
- (((sep = expression[1])>='0' && sep<='9') || sep=='.')) {
- if (!expression[2]) { // [+-!] + Single digit
- const int ival = sep - '0';
- res = (t)(c=='+'?ival:c=='-'?-ival:!ival);
- is_success = true;
- } else if ((err = std::sscanf(expression + 1,"%lf %c%lf %c",&val,&sep,&val2,&end))==1) { // [+-!] Single value
- res = (t)(c=='+'?val:c=='-'?-val:(double)!val);
- is_success = true;
- } else if (err==3) { // [+-!] Value1 Operator Value2
- const double val1 = c=='+'?val:c=='-'?-val:(double)!val;
- switch (sep) {
- case '+' : res = (t)(val1 + val2); is_success = true; break;
- case '-' : res = (t)(val1 - val2); is_success = true; break;
- case '*' : res = (t)(val1*val2); is_success = true; break;
- case '/' : res = (t)(val1/val2); is_success = true; break;
- case '%' : res = (t)cimg::mod(val1,val2); is_success = true; break;
- case '&' : res = (t)((long)val1 & (long)val2); is_success = true; break;
- case '|' : res = (t)((long)val1 | (long)val2); is_success = true; break;
- case '>' : res = (t)(val1>val2); is_success = true; break;
- case '<' : res = (t)(val1<val2); is_success = true; break;
- case ';' : res = (t)val2; is_success = true; break;
- case '^' : val = std::pow(val,val2); res = (t)(c=='+'?val:c=='-'?-val:!val); is_success = true; break;
- }
- }
- } else if (!expression[1]) switch (*expression) { // Other common single-char expressions
- case 'w' : res = (t)_width; is_success = true; break;
- case 'h' : res = (t)_height; is_success = true; break;
- case 'd' : res = (t)_depth; is_success = true; break;
- case 's' : res = (t)_spectrum; is_success = true; break;
- case 'r' : res = (t)_is_shared; is_success = true; break;
- }
- return is_success;
- }
- double _eval(CImg<T> *const img_output, const char *const expression,
- const double x, const double y, const double z, const double c,
- CImgList<T> *const list_images) const {
- if (!expression || !*expression) return 0;
- double _val = 0;
- if (__eval(expression,_val)) return _val;
- _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
- *expression=='*' || *expression==':'),"eval",
- *this,img_output,list_images,false);
- mp.begin_t();
- const double val = mp(x,y,z,c);
- mp.end_t();
- mp.end();
- return val;
- }
- //! Evaluate math formula.
- /**
- \param[out] output Contains values of output vector returned by the evaluated expression
- (or is empty if the returned type is scalar).
- \param expression Math formula, as a C-string.
- \param x Value of the pre-defined variable \c x.
- \param y Value of the pre-defined variable \c y.
- \param z Value of the pre-defined variable \c z.
- \param c Value of the pre-defined variable \c c.
- \param list_images A list of input images attached to the specified math formula.
- **/
- template<typename t>
- void eval(CImg<t> &output, const char *const expression,
- const double x=0, const double y=0, const double z=0, const double c=0,
- CImgList<T> *const list_images=0) {
- _eval(output,this,expression,x,y,z,c,list_images);
- }
- //! Evaluate math formula \const.
- template<typename t>
- void eval(CImg<t>& output, const char *const expression,
- const double x=0, const double y=0, const double z=0, const double c=0,
- CImgList<T> *const list_images=0) const {
- _eval(output,0,expression,x,y,z,c,list_images);
- }
- template<typename t>
- void _eval(CImg<t>& output, CImg<T> *const img_output, const char *const expression,
- const double x, const double y, const double z, const double c,
- CImgList<T> *const list_images) const {
- if (!expression || !*expression) { output.assign(1); *output = 0; return; }
- double _val = 0;
- if (__eval(expression,_val)) { output.assign(1); *output = _val; return; }
- _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
- *expression=='*' || *expression==':'),"eval",
- *this,img_output,list_images,false);
- output.assign(1,std::max(1U,mp.result_dim));
- mp.begin_t();
- mp(x,y,z,c,output._data);
- mp.end_t();
- mp.end();
- }
- //! Evaluate math formula on a set of variables.
- /**
- \param expression Math formula, as a C-string.
- \param xyzc Set of values (x,y,z,c) used for the evaluation.
- \param list_images A list of input images attached to the specified math formula.
- **/
- template<typename t>
- CImg<doubleT> eval(const char *const expression, const CImg<t>& xyzc,
- CImgList<T> *const list_images=0) {
- return _eval(this,expression,xyzc,list_images);
- }
- //! Evaluate math formula on a set of variables \const.
- template<typename t>
- CImg<doubleT> eval(const char *const expression, const CImg<t>& xyzc,
- CImgList<T> *const list_images=0) const {
- return _eval(0,expression,xyzc,list_images);
- }
- template<typename t>
- CImg<doubleT> _eval(CImg<T> *const output, const char *const expression, const CImg<t>& xyzc,
- CImgList<T> *const list_images=0) const {
- CImg<doubleT> res(1,xyzc.size()/4);
- if (!expression || !*expression) return res.fill(0);
- _cimg_math_parser mp(expression,"eval",*this,output,list_images,false);
- #if cimg_use_openmp!=0
- cimg_pragma_openmp(parallel if (res._height>=512))
- {
- _cimg_math_parser
- *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
- &lmp = *_mp;
- cimg_pragma_openmp(barrier)
- lmp.begin_t();
- cimg_pragma_openmp(for)
- for (int i = 0; i<res.height(); ++i) {
- const unsigned int i4 = 4*i;
- const double
- x = (double)xyzc[i4], y = (double)xyzc[i4 + 1],
- z = (double)xyzc[i4 + 2], c = (double)xyzc[i4 + 3];
- res[i] = lmp(x,y,z,c);
- }
- lmp.end_t();
- cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
- if (&lmp!=&mp) delete &lmp;
- }
- #else
- mp.begin_t();
- const t *ps = xyzc._data;
- cimg_for(res,pd,double) {
- const double x = (double)*(ps++), y = (double)*(ps++), z = (double)*(ps++), c = (double)*(ps++);
- *pd = mp(x,y,z,c);
- }
- mp.end_t();
- #endif
- mp.end();
- return res;
- }
- //! Compute statistics vector from the pixel values.
- /**
- \param variance_method Method used to compute the variance (see variance(const unsigned int) const).
- \return Statistics vector as
- <tt>[min, max, mean, variance, xmin, ymin, zmin, cmin, xmax, ymax, zmax, cmax, sum, product]</tt>.
- **/
- CImg<Tdouble> get_stats(const unsigned int variance_method=1) const {
- if (is_empty()) return CImg<doubleT>();
- const ulongT siz = size();
- const longT off_end = (longT)siz;
- double S = 0, S2 = 0, P = 1;
- longT offm = 0, offM = 0;
- T m = *_data, M = m;
- cimg_pragma_openmp(parallel reduction(+:S,S2) reduction(*:P) cimg_openmp_if_size(siz,131072)) {
- longT loffm = 0, loffM = 0;
- T lm = *_data, lM = lm;
- cimg_pragma_openmp(for)
- for (longT off = 0; off<off_end; ++off) {
- const T val = _data[off];
- const double _val = (double)val;
- if (val<lm) { lm = val; loffm = off; }
- if (val>lM) { lM = val; loffM = off; }
- S+=_val;
- S2+=_val*_val;
- P*=_val;
- }
- cimg_pragma_openmp(critical(get_stats)) {
- if (lm<m || (lm==m && loffm<offm)) { m = lm; offm = loffm; }
- if (lM>M || (lM==M && loffM<offM)) { M = lM; offM = loffM; }
- }
- }
- const double
- mean_value = S/siz,
- _variance_value = variance_method==0?(S2 - S*S/siz)/siz:
- (variance_method==1?(siz>1?(S2 - S*S/siz)/(siz - 1):0):
- variance(variance_method)),
- variance_value = _variance_value>0?_variance_value:0;
- int
- xm = 0, ym = 0, zm = 0, cm = 0,
- xM = 0, yM = 0, zM = 0, cM = 0;
- contains(_data[offm],xm,ym,zm,cm);
- contains(_data[offM],xM,yM,zM,cM);
- return CImg<Tdouble>(1,14).fill((double)m,(double)M,mean_value,variance_value,
- (double)xm,(double)ym,(double)zm,(double)cm,
- (double)xM,(double)yM,(double)zM,(double)cM,
- S,P);
- }
- //! Compute statistics vector from the pixel values \inplace.
- CImg<T>& stats(const unsigned int variance_method=1) {
- return get_stats(variance_method).move_to(*this);
- }
- //@}
- //-------------------------------------
- //
- //! \name Vector / Matrix Operations
- //@{
- //-------------------------------------
- //! Compute norm of the image, viewed as a matrix.
- /**
- \param magnitude_type Norm type. Can be:
- - \c -1: Linf-norm
- - \c 0: L0-norm
- - \c 1: L1-norm
- - \c 2: L2-norm
- **/
- double magnitude(const int magnitude_type=2) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "magnitude(): Empty instance.",
- cimg_instance);
- const ulongT siz = size();
- double res = 0;
- switch (magnitude_type) {
- case -1 : {
- cimg_for(*this,ptrs,T) { const double val = (double)cimg::abs(*ptrs); if (val>res) res = val; }
- } break;
- case 1 : {
- cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
- for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::abs(_data[off]);
- } break;
- default : {
- cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(size(),8192))
- for (longT off = 0; off<(longT)siz; ++off) res+=(double)cimg::sqr(_data[off]);
- res = (double)std::sqrt(res);
- }
- }
- return res;
- }
- //! Compute the trace of the image, viewed as a matrix.
- /**
- **/
- double trace() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "trace(): Empty instance.",
- cimg_instance);
- double res = 0;
- cimg_forX(*this,k) res+=(double)(*this)(k,k);
- return res;
- }
- //! Compute the determinant of the image, viewed as a matrix.
- /**
- **/
- double det() const {
- if (is_empty() || _width!=_height || _depth!=1 || _spectrum!=1)
- throw CImgInstanceException(_cimg_instance
- "det(): Instance is not a square matrix.",
- cimg_instance);
- switch (_width) {
- case 1 : return (double)((*this)(0,0));
- case 2 : return (double)((*this)(0,0))*(double)((*this)(1,1)) - (double)((*this)(0,1))*(double)((*this)(1,0));
- case 3 : {
- const double
- a = (double)_data[0], d = (double)_data[1], g = (double)_data[2],
- b = (double)_data[3], e = (double)_data[4], h = (double)_data[5],
- c = (double)_data[6], f = (double)_data[7], i = (double)_data[8];
- return i*a*e - a*h*f - i*b*d + b*g*f + c*d*h - c*g*e;
- }
- default : {
- CImg<Tfloat> lu(*this,false);
- CImg<uintT> indx;
- bool d;
- lu._LU(indx,d);
- double res = d?(double)1:(double)-1;
- cimg_forX(lu,i) res*=lu(i,i);
- return res;
- }
- }
- }
- //! Compute the dot product between instance and argument, viewed as matrices.
- /**
- \param img Image used as a second argument of the dot product.
- **/
- template<typename t>
- double dot(const CImg<t>& img) const {
- const ulongT nb = std::min(size(),img.size());
- double res = 0;
- cimg_pragma_openmp(parallel for reduction(+:res) cimg_openmp_if_size(nb,8192))
- for (longT off = 0; off<(longT)nb; ++off) res+=(double)_data[off]*(double)img[off];
- return res;
- }
- //! Get vector-valued pixel located at specified position.
- /**
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- **/
- CImg<T> get_vector_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const {
- CImg<T> res;
- if (res._height!=_spectrum) res.assign(1,_spectrum);
- const ulongT whd = (ulongT)_width*_height*_depth;
- const T *ptrs = data(x,y,z);
- T *ptrd = res._data;
- cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return res;
- }
- //! Get (square) matrix-valued pixel located at specified position.
- /**
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \note - The spectrum() of the image must be a square.
- **/
- CImg<T> get_matrix_at(const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) const {
- const int n = (int)cimg::round(std::sqrt((double)_spectrum));
- const T *ptrs = data(x,y,z,0);
- const ulongT whd = (ulongT)_width*_height*_depth;
- CImg<T> res(n,n);
- T *ptrd = res._data;
- cimg_forC(*this,c) { *(ptrd++) = *ptrs; ptrs+=whd; }
- return res;
- }
- //! Get tensor-valued pixel located at specified position.
- /**
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- **/
- CImg<T> get_tensor_at(const unsigned int x, const unsigned int y=0, const unsigned int z=0) const {
- const T *ptrs = data(x,y,z,0);
- const ulongT whd = (ulongT)_width*_height*_depth;
- if (_spectrum==6)
- return tensor(*ptrs,*(ptrs + whd),*(ptrs + 2*whd),*(ptrs + 3*whd),*(ptrs + 4*whd),*(ptrs + 5*whd));
- if (_spectrum==3)
- return tensor(*ptrs,*(ptrs + whd),*(ptrs + 2*whd));
- return tensor(*ptrs);
- }
- //! Set vector-valued pixel at specified position.
- /**
- \param vec Vector to put on the instance image.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- **/
- template<typename t>
- CImg<T>& set_vector_at(const CImg<t>& vec, const unsigned int x, const unsigned int y=0, const unsigned int z=0) {
- if (x<_width && y<_height && z<_depth) {
- const t *ptrs = vec._data;
- const ulongT whd = (ulongT)_width*_height*_depth;
- T *ptrd = data(x,y,z);
- for (unsigned int k = std::min((unsigned int)vec.size(),_spectrum); k; --k) {
- *ptrd = (T)*(ptrs++); ptrd+=whd;
- }
- }
- return *this;
- }
- //! Set (square) matrix-valued pixel at specified position.
- /**
- \param mat Matrix to put on the instance image.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- **/
- template<typename t>
- CImg<T>& set_matrix_at(const CImg<t>& mat, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) {
- return set_vector_at(mat,x,y,z);
- }
- //! Set tensor-valued pixel at specified position.
- /**
- \param ten Tensor to put on the instance image.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- **/
- template<typename t>
- CImg<T>& set_tensor_at(const CImg<t>& ten, const unsigned int x=0, const unsigned int y=0, const unsigned int z=0) {
- T *ptrd = data(x,y,z,0);
- const ulongT siz = (ulongT)_width*_height*_depth;
- if (ten._height==2) {
- *ptrd = (T)ten[0]; ptrd+=siz;
- *ptrd = (T)ten[1]; ptrd+=siz;
- *ptrd = (T)ten[3];
- }
- else {
- *ptrd = (T)ten[0]; ptrd+=siz;
- *ptrd = (T)ten[1]; ptrd+=siz;
- *ptrd = (T)ten[2]; ptrd+=siz;
- *ptrd = (T)ten[4]; ptrd+=siz;
- *ptrd = (T)ten[5]; ptrd+=siz;
- *ptrd = (T)ten[8];
- }
- return *this;
- }
- //! Resize image to become a diagonal matrix.
- /**
- \note Transform the image as a diagonal matrix so that each of its initial value becomes a diagonal coefficient.
- **/
- CImg<T>& diagonal() {
- return get_diagonal().move_to(*this);
- }
- //! Resize image to become a diagonal matrix \newinstance.
- CImg<T> get_diagonal() const {
- if (is_empty()) return *this;
- const unsigned int siz = (unsigned int)size();
- CImg<T> res(siz,siz,1,1,0);
- cimg_foroff(*this,off) res((unsigned int)off,(unsigned int)off) = (*this)[off];
- return res;
- }
- //! Replace the image by an identity matrix.
- /**
- \note If the instance image is not square, it is resized to a square matrix using its maximum
- dimension as a reference.
- **/
- CImg<T>& identity_matrix() {
- return identity_matrix(std::max(_width,_height)).move_to(*this);
- }
- //! Replace the image by an identity matrix \newinstance.
- CImg<T> get_identity_matrix() const {
- return identity_matrix(std::max(_width,_height));
- }
- //! Fill image with a linear sequence of values.
- /**
- \param a0 Starting value of the sequence.
- \param a1 Ending value of the sequence.
- **/
- CImg<T>& sequence(const T& a0, const T& a1) {
- if (is_empty()) return *this;
- const ulongT siz = size() - 1;
- T* ptr = _data;
- if (siz) {
- const double delta = (double)a1 - (double)a0;
- cimg_foroff(*this,l) *(ptr++) = (T)(a0 + delta*l/siz);
- } else *ptr = a0;
- return *this;
- }
- //! Fill image with a linear sequence of values \newinstance.
- CImg<T> get_sequence(const T& a0, const T& a1) const {
- return (+*this).sequence(a0,a1);
- }
- //! Transpose the image, viewed as a matrix.
- /**
- \note Equivalent to \code permute_axes("yxzc"); \endcode.
- **/
- CImg<T>& transpose() {
- if (_width==1) { _width = _height; _height = 1; return *this; }
- if (_height==1) { _height = _width; _width = 1; return *this; }
- if (_width==_height) {
- 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));
- return *this;
- }
- return get_transpose().move_to(*this);
- }
- //! Transpose the image, viewed as a matrix \newinstance.
- CImg<T> get_transpose() const {
- return get_permute_axes("yxzc");
- }
- //! Compute the cross product between two \c 1x3 images, viewed as 3D vectors.
- /**
- \param img Image used as the second argument of the cross product.
- \note The first argument of the cross product is \c *this.
- **/
- template<typename t>
- CImg<T>& cross(const CImg<t>& img) {
- if (_width!=1 || _height<3 || img._width!=1 || img._height<3)
- throw CImgInstanceException(_cimg_instance
- "cross(): Instance and/or specified image (%u,%u,%u,%u,%p) are not 3D vectors.",
- cimg_instance,
- img._width,img._height,img._depth,img._spectrum,img._data);
- const T x = (*this)[0], y = (*this)[1], z = (*this)[2];
- (*this)[0] = (T)(y*img[2] - z*img[1]);
- (*this)[1] = (T)(z*img[0] - x*img[2]);
- (*this)[2] = (T)(x*img[1] - y*img[0]);
- return *this;
- }
- //! Compute the cross product between two \c 1x3 images, viewed as 3D vectors \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_cross(const CImg<t>& img) const {
- return CImg<_cimg_Tt>(*this).cross(img);
- }
- //! Invert the instance image, viewed as a matrix.
- /**
- \param use_LU Choose the inverting algorithm. Can be:
- - \c true: LU-based matrix inversion.
- - \c false: SVD-based matrix inversion.
- **/
- CImg<T>& invert(const bool use_LU=true) {
- if (_width!=_height || _depth!=1 || _spectrum!=1)
- throw CImgInstanceException(_cimg_instance
- "invert(): Instance is not a square matrix.",
- cimg_instance);
- const double dete = _width>3?-1.:det();
- if (dete!=0. && _width==2) {
- const double
- a = _data[0], c = _data[1],
- b = _data[2], d = _data[3];
- _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete);
- _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete);
- } else if (dete!=0. && _width==3) {
- const double
- a = _data[0], d = _data[1], g = _data[2],
- b = _data[3], e = _data[4], h = _data[5],
- c = _data[6], f = _data[7], i = _data[8];
- _data[0] = (T)((i*e - f*h)/dete), _data[1] = (T)((g*f - i*d)/dete), _data[2] = (T)((d*h - g*e)/dete);
- _data[3] = (T)((h*c - i*b)/dete), _data[4] = (T)((i*a - c*g)/dete), _data[5] = (T)((g*b - a*h)/dete);
- _data[6] = (T)((b*f - e*c)/dete), _data[7] = (T)((d*c - a*f)/dete), _data[8] = (T)((a*e - d*b)/dete);
- } else {
- #ifdef cimg_use_lapack
- int INFO = (int)use_LU, N = _width, LWORK = 4*N, *const IPIV = new int[N];
- Tfloat
- *const lapA = new Tfloat[N*N],
- *const WORK = new Tfloat[LWORK];
- cimg_forXY(*this,k,l) lapA[k*N + l] = (Tfloat)((*this)(k,l));
- cimg::getrf(N,lapA,IPIV,INFO);
- if (INFO)
- cimg::warn(_cimg_instance
- "invert(): LAPACK function dgetrf_() returned error code %d.",
- cimg_instance,
- INFO);
- else {
- cimg::getri(N,lapA,IPIV,WORK,LWORK,INFO);
- if (INFO)
- cimg::warn(_cimg_instance
- "invert(): LAPACK function dgetri_() returned error code %d.",
- cimg_instance,
- INFO);
- }
- if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)(lapA[k*N + l]); else fill(0);
- delete[] IPIV; delete[] lapA; delete[] WORK;
- #else
- if (use_LU) { // LU-based
- CImg<Tfloat> A(*this,false), indx;
- bool d;
- A._LU(indx,d);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,16*16))
- cimg_forX(*this,j) {
- CImg<Tfloat> col(1,_width,1,1,0);
- col(j) = 1;
- col._solve(A,indx);
- cimg_forX(*this,i) (*this)(j,i) = (T)col(i);
- }
- } else pseudoinvert(false); // SVD-based
- #endif
- }
- return *this;
- }
- //! Invert the instance image, viewed as a matrix \newinstance.
- CImg<Tfloat> get_invert(const bool use_LU=true) const {
- return CImg<Tfloat>(*this,false).invert(use_LU);
- }
- //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix.
- /**
- **/
- CImg<T>& pseudoinvert(const bool use_LU=false) {
- return get_pseudoinvert(use_LU).move_to(*this);
- }
- //! Compute the Moore-Penrose pseudo-inverse of the instance image, viewed as a matrix \newinstance.
- CImg<Tfloat> get_pseudoinvert(const bool use_LU=false) const {
- // LU-based method.
- if (use_LU) {
- CImg<Tfloat> AtA(width(),width());
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,128*128))
- cimg_forY(AtA,i)
- for (int j = 0; j<=i; ++j) {
- double res = 0;
- cimg_forY(*this,k) res+=(*this)(i,k)*(*this)(j,k);
- AtA(j,i) = AtA(i,j) = (Tfloat)res;
- }
- AtA.invert(true);
- return AtA*get_transpose();
- }
- // SVD-based method.
- CImg<Tfloat> U, S, V;
- SVD(U,S,V,false);
- const Tfloat epsilon = (sizeof(Tfloat)<=4?5.96e-8f:1.11e-16f)*std::max(_width,_height)*S.max();
- cimg_forX(V,x) {
- const Tfloat s = S(x), invs = s>epsilon?1/s:0;
- cimg_forY(V,y) V(x,y)*=invs;
- }
- return V*U.transpose();
- }
- //! Solve a system of linear equations.
- /**
- \param A Matrix of the linear system.
- \param use_LU In case of non square system (least-square solution),
- choose between SVD-based (\c false) or LU-based (\c true) method.
- LU method is faster for large matrices, but numerically less stable.
- \note Solve \c AX = B where \c B=*this.
- **/
- template<typename t>
- CImg<T>& solve(const CImg<t>& A, const bool use_LU=false) {
- if (_depth!=1 || _spectrum!=1 || _height!=A._height || A._depth!=1 || A._spectrum!=1)
- throw CImgArgumentException(_cimg_instance
- "solve(): Instance and specified matrix (%u,%u,%u,%u,%p) have "
- "incompatible dimensions.",
- cimg_instance,
- A._width,A._height,A._depth,A._spectrum,A._data);
- typedef _cimg_Ttfloat Ttfloat;
- if (A.size()==1) return (*this)/=A[0];
- if (A._width==2 && A._height==2 && _height==2) { // 2x2 linear system
- const double a = (double)A[0], b = (double)A[1], c = (double)A[2], d = (double)A[3],
- fa = std::fabs(a), fb = std::fabs(b), fc = std::fabs(c), fd = std::fabs(d),
- det = a*d - b*c, fM = cimg::max(fa,fb,fc,fd);
- if (fM==fa)
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
- cimg_forX(*this,k) {
- const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), y = (a*v - c*u)/det;
- (*this)(k,0) = (T)((u - b*y)/a); (*this)(k,1) = (T)y;
- } else if (fM==fc)
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
- cimg_forX(*this,k) {
- const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), y = (a*v - c*u)/det;
- (*this)(k,0) = (T)((v - d*y)/c); (*this)(k,1) = (T)y;
- } else if (fM==fb)
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
- cimg_forX(*this,k) {
- const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), x = (d*u - b*v)/det;
- (*this)(k,0) = (T)x; (*this)(k,1) = (T)((u - a*x)/b);
- } else
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=256))
- cimg_forX(*this,k) {
- const double u = (double)(*this)(k,0), v = (double)(*this)(k,1), x = (d*u - b*v)/det;
- (*this)(k,0) = (T)x; (*this)(k,1) = (T)((v - c*x)/d);
- }
- return *this;
- }
- if (A._width==A._height) { // Square linear system
- #ifdef cimg_use_lapack
- char TRANS = 'N';
- int INFO, N = _height, LWORK = 4*N, *const IPIV = new int[N];
- Ttfloat
- *const lapA = new Ttfloat[N*N],
- *const lapB = new Ttfloat[N],
- *const WORK = new Ttfloat[LWORK];
- cimg_forXY(A,k,l) lapA[k*N + l] = (Ttfloat)(A(k,l));
- cimg_forX(*this,i) {
- cimg_forY(*this,j) lapB[j] = (Ttfloat)((*this)(i,j));
- cimg::getrf(N,lapA,IPIV,INFO);
- if (INFO)
- cimg::warn(_cimg_instance
- "solve(): LAPACK library function dgetrf_() returned error code %d.",
- cimg_instance,
- INFO);
- else {
- cimg::getrs(TRANS,N,lapA,IPIV,lapB,INFO);
- if (INFO)
- cimg::warn(_cimg_instance
- "solve(): LAPACK library function dgetrs_() returned error code %d.",
- cimg_instance,
- INFO);
- }
- if (!INFO) cimg_forY(*this,j) (*this)(i,j) = (T)(lapB[j]); else cimg_forY(*this,j) (*this)(i,j) = (T)0;
- }
- delete[] IPIV; delete[] lapA; delete[] lapB; delete[] WORK;
- #else
- CImg<Ttfloat> lu(A,false);
- CImg<Ttfloat> indx;
- bool d;
- lu._LU(indx,d);
- CImg<T> res(_width,A._width);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width*_height,16))
- cimg_forX(*this,i) res.draw_image(i,get_column(i)._solve(lu,indx));
- res.move_to(*this);
- #endif
- } else { // Least-square solution for non-square systems
- #ifdef cimg_use_lapack
- char TRANS = 'N';
- int INFO, N = A._width, M = A._height, LWORK = -1, LDA = M, LDB = M, NRHS = _width;
- Ttfloat WORK_QUERY;
- Ttfloat
- * const lapA = new Ttfloat[M*N],
- * const lapB = new Ttfloat[M*NRHS];
- cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, &WORK_QUERY, LWORK, INFO);
- LWORK = (int) WORK_QUERY;
- Ttfloat *const WORK = new Ttfloat[LWORK];
- cimg_forXY(A,k,l) lapA[k*M + l] = (Ttfloat)(A(k,l));
- cimg_forXY(*this,k,l) lapB[k*M + l] = (Ttfloat)((*this)(k,l));
- cimg::sgels(TRANS, M, N, NRHS, lapA, LDA, lapB, LDB, WORK, LWORK, INFO);
- if (INFO != 0)
- cimg::warn(_cimg_instance
- "solve(): LAPACK library function sgels() returned error code %d.",
- cimg_instance,
- INFO);
- assign(NRHS, N);
- if (!INFO) cimg_forXY(*this,k,l) (*this)(k,l) = (T)lapB[k*M + l];
- else (A.get_pseudoinvert(use_LU)*(*this)).move_to(*this);
- delete[] lapA; delete[] lapB; delete[] WORK;
- #else
- (A.get_pseudoinvert(use_LU)*(*this)).move_to(*this);
- #endif
- }
- return *this;
- }
- //! Solve a system of linear equations \newinstance.
- template<typename t>
- CImg<_cimg_Ttfloat> get_solve(const CImg<t>& A, const bool use_LU=false) const {
- typedef _cimg_Ttfloat Ttfloat;
- return CImg<Ttfloat>(*this,false).solve(A,use_LU);
- }
- template<typename t, typename ti>
- CImg<T>& _solve(const CImg<t>& A, const CImg<ti>& indx) {
- typedef _cimg_Ttfloat Ttfloat;
- const int N = height();
- int ii = -1;
- Ttfloat sum;
- for (int i = 0; i<N; ++i) {
- const int ip = (int)indx[i];
- sum = (*this)(ip);
- (*this)(ip) = (*this)(i);
- if (ii>=0) for (int j = ii; j<=i - 1; ++j) sum-=A(j,i)*(*this)(j);
- else if (sum!=0) ii = i;
- (*this)(i) = (T)sum;
- }
- for (int i = N - 1; i>=0; --i) {
- sum = (*this)(i);
- for (int j = i + 1; j<N; ++j) sum-=A(j,i)*(*this)(j);
- (*this)(i) = (T)(sum/A(i,i));
- }
- return *this;
- }
- //! Solve a tridiagonal system of linear equations.
- /**
- \param A Coefficients of the tridiagonal system.
- A is a tridiagonal matrix A = [ b0,c0,0,...; a1,b1,c1,0,... ; ... ; ...,0,aN,bN ],
- stored as a 3 columns matrix
- \note Solve AX=B where \c B=*this, using the Thomas algorithm.
- **/
- template<typename t>
- CImg<T>& solve_tridiagonal(const CImg<t>& A) {
- const unsigned int siz = (unsigned int)size();
- if (A._width!=3 || A._height!=siz)
- throw CImgArgumentException(_cimg_instance
- "solve_tridiagonal(): Instance and tridiagonal matrix "
- "(%u,%u,%u,%u,%p) have incompatible dimensions.",
- cimg_instance,
- A._width,A._height,A._depth,A._spectrum,A._data);
- typedef _cimg_Ttfloat Ttfloat;
- const Ttfloat epsilon = 1e-4f;
- CImg<Ttfloat> B = A.get_column(1), V(*this,false);
- for (int i = 1; i<(int)siz; ++i) {
- const Ttfloat m = A(0,i)/(B[i - 1]?B[i - 1]:epsilon);
- B[i] -= m*A(2,i - 1);
- V[i] -= m*V[i - 1];
- }
- (*this)[siz - 1] = (T)(V[siz - 1]/(B[siz - 1]?B[siz - 1]:epsilon));
- 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));
- return *this;
- }
- //! Solve a tridiagonal system of linear equations \newinstance.
- template<typename t>
- CImg<_cimg_Ttfloat> get_solve_tridiagonal(const CImg<t>& A) const {
- return CImg<_cimg_Ttfloat>(*this,false).solve_tridiagonal(A);
- }
- //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.
- /**
- \param[out] val Vector of the estimated eigenvalues, in decreasing order.
- \param[out] vec Matrix of the estimated eigenvectors, sorted by columns.
- **/
- template<typename t>
- const CImg<T>& eigen(CImg<t>& val, CImg<t> &vec) const {
- if (is_empty()) { val.assign(); vec.assign(); }
- else {
- if (_width!=_height || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "eigen(): Instance is not a square matrix.",
- cimg_instance);
- if (val.size()<(ulongT)_width) val.assign(1,_width);
- if (vec.size()<(ulongT)_width*_width) vec.assign(_width,_width);
- switch (_width) {
- case 1 : { val[0] = (t)(*this)[0]; vec[0] = (t)1; } break;
- case 2 : {
- const double a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3], e = a + d;
- double f = e*e - 4*(a*d - b*c);
- if (f<0) cimg::warn(_cimg_instance
- "eigen(): Complex eigenvalues found.",
- cimg_instance);
- f = std::sqrt(f);
- const double
- l1 = 0.5*(e - f),
- l2 = 0.5*(e + f),
- b2 = b*b,
- norm1 = std::sqrt(cimg::sqr(l2 - a) + b2),
- norm2 = std::sqrt(cimg::sqr(l1 - a) + b2);
- val[0] = (t)l2;
- val[1] = (t)l1;
- 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; }
- 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; }
- } break;
- default :
- throw CImgInstanceException(_cimg_instance
- "eigen(): Eigenvalues computation of general matrices is limited "
- "to 2x2 matrices.",
- cimg_instance);
- }
- }
- return *this;
- }
- //! Compute eigenvalues and eigenvectors of the instance image, viewed as a matrix.
- /**
- \return A list of two images <tt>[val; vec]</tt>, whose meaning is similar as in eigen(CImg<t>&,CImg<t>&) const.
- **/
- CImgList<Tfloat> get_eigen() const {
- CImgList<Tfloat> res(2);
- eigen(res[0],res[1]);
- return res;
- }
- //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix.
- /**
- \param[out] val Vector of the estimated eigenvalues, in decreasing order.
- \param[out] vec Matrix of the estimated eigenvectors, sorted by columns.
- **/
- template<typename t>
- const CImg<T>& symmetric_eigen(CImg<t>& val, CImg<t>& vec) const {
- if (is_empty()) { val.assign(); vec.assign(); return *this; }
- if (_width!=_height || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "eigen(): Instance is not a square matrix.",
- cimg_instance);
- val.assign(1,_width);
- vec.assign(_width,_width);
- if (_width==1) { val[0] = cimg::abs((*this)[0]); vec[0] = 1; return *this; }
- if (_width==2) {
- const double
- a = (*this)[0], b = (*this)[1], c = (*this)[2], d = (*this)[3],
- e = a + d, f = std::sqrt(std::max(e*e - 4*(a*d - b*c),0.0)),
- l1 = 0.5*(e - f), l2 = 0.5*(e + f),
- n = std::sqrt(cimg::sqr(l2 - a) + b*b);
- val[0] = (t)l2;
- val[1] = (t)l1;
- if (n>0) { vec[0] = (t)(b/n); vec[2] = (t)((l2 - a)/n); } else { vec[0] = 1; vec[2] = 0; }
- vec[1] = -vec[2];
- vec[3] = vec[0];
- return *this;
- }
- #ifdef cimg_use_lapack
- char JOB = 'V', UPLO = 'U';
- int N = _width, LWORK = 4*N, INFO;
- Tfloat
- *const lapA = new Tfloat[N*N],
- *const lapW = new Tfloat[N],
- *const WORK = new Tfloat[LWORK];
- cimg_forXY(*this,k,l) lapA[k*N + l] = (Tfloat)((*this)(k,l));
- cimg::syev(JOB,UPLO,N,lapA,lapW,WORK,LWORK,INFO);
- if (INFO)
- cimg::warn(_cimg_instance
- "symmetric_eigen(): LAPACK library function dsyev_() returned error code %d.",
- cimg_instance,
- INFO);
- if (!INFO) {
- cimg_forY(val,i) val(i) = (T)lapW[N - 1 -i];
- cimg_forXY(vec,k,l) vec(k,l) = (T)(lapA[(N - 1 - k)*N + l]);
- } else { val.fill(0); vec.fill(0); }
- delete[] lapA; delete[] lapW; delete[] WORK;
- #else
- CImg<t> V(_width,_width);
- Tfloat M = 0, m = (Tfloat)min_max(M), maxabs = cimg::max((Tfloat)1,cimg::abs(m),cimg::abs(M));
- (CImg<Tfloat>(*this,false)/=maxabs).SVD(vec,val,V,false);
- if (maxabs!=1) val*=maxabs;
- bool is_ambiguous = false;
- float eig = 0;
- cimg_forY(val,p) { // Check for ambiguous cases
- if (val[p]>eig) eig = (float)val[p];
- t scal = 0;
- cimg_forY(vec,y) scal+=vec(p,y)*V(p,y);
- if (cimg::abs(scal)<0.9f) is_ambiguous = true;
- if (scal<0) val[p] = -val[p];
- }
- if (is_ambiguous) {
- ++(eig*=2);
- SVD(vec,val,V,false,40,eig);
- val-=eig;
- }
- CImg<intT> permutations; // Sort eigenvalues in decreasing order
- CImg<t> tmp(_width);
- val.sort(permutations,false);
- cimg_forY(vec,k) {
- cimg_forY(permutations,y) tmp(y) = vec(permutations(y),k);
- std::memcpy(vec.data(0,k),tmp._data,sizeof(t)*_width);
- }
- #endif
- return *this;
- }
- //! Compute eigenvalues and eigenvectors of the instance image, viewed as a symmetric matrix.
- /**
- \return A list of two images <tt>[val; vec]</tt>, whose meaning are similar as in
- symmetric_eigen(CImg<t>&,CImg<t>&) const.
- **/
- CImgList<Tfloat> get_symmetric_eigen() const {
- CImgList<Tfloat> res(2);
- symmetric_eigen(res[0],res[1]);
- return res;
- }
- //! Sort pixel values and get sorting permutations.
- /**
- \param[out] permutations Permutation map used for the sorting.
- \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way.
- **/
- template<typename t>
- CImg<T>& sort(CImg<t>& permutations, const bool is_increasing=true) {
- permutations.assign(_width,_height,_depth,_spectrum);
- if (is_empty()) return *this;
- cimg_foroff(permutations,off) permutations[off] = (t)off;
- return _quicksort(0,size() - 1,permutations,is_increasing,true);
- }
- //! Sort pixel values and get sorting permutations \newinstance.
- template<typename t>
- CImg<T> get_sort(CImg<t>& permutations, const bool is_increasing=true) const {
- return (+*this).sort(permutations,is_increasing);
- }
- //! Sort pixel values.
- /**
- \param is_increasing Tells if pixel values are sorted in an increasing (\c true) or decreasing (\c false) way.
- \param axis Tells if the value sorting must be done along a specific axis. Can be:
- - \c 0: All pixel values are sorted, independently on their initial position.
- - \c 'x': Image columns are sorted, according to the first value in each column.
- - \c 'y': Image rows are sorted, according to the first value in each row.
- - \c 'z': Image slices are sorted, according to the first value in each slice.
- - \c 'c': Image channels are sorted, according to the first value in each channel.
- **/
- CImg<T>& sort(const bool is_increasing=true, const char axis=0) {
- if (is_empty()) return *this;
- CImg<uintT> perm;
- switch (cimg::lowercase(axis)) {
- case 0 :
- _quicksort(0,size() - 1,perm,is_increasing,false);
- break;
- case 'x' : {
- perm.assign(_width);
- get_crop(0,0,0,0,_width - 1,0,0,0).sort(perm,is_increasing);
- CImg<T> img(*this,false);
- cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(perm[x],y,z,c);
- } break;
- case 'y' : {
- perm.assign(_height);
- get_crop(0,0,0,0,0,_height - 1,0,0).sort(perm,is_increasing);
- CImg<T> img(*this,false);
- cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,perm[y],z,c);
- } break;
- case 'z' : {
- perm.assign(_depth);
- get_crop(0,0,0,0,0,0,_depth - 1,0).sort(perm,is_increasing);
- CImg<T> img(*this,false);
- cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,perm[z],c);
- } break;
- case 'c' : {
- perm.assign(_spectrum);
- get_crop(0,0,0,0,0,0,0,_spectrum - 1).sort(perm,is_increasing);
- CImg<T> img(*this,false);
- cimg_forXYZC(*this,x,y,z,c) (*this)(x,y,z,c) = img(x,y,z,perm[c]);
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "sort(): Invalid specified axis '%c' "
- "(should be { x | y | z | c }).",
- cimg_instance,axis);
- }
- return *this;
- }
- //! Sort pixel values \newinstance.
- CImg<T> get_sort(const bool is_increasing=true, const char axis=0) const {
- return (+*this).sort(is_increasing,axis);
- }
- template<typename t>
- CImg<T>& _quicksort(const long indm, const long indM, CImg<t>& permutations,
- const bool is_increasing, const bool is_permutations) {
- if (indm<indM) {
- const long mid = (indm + indM)/2;
- if (is_increasing) {
- if ((*this)[indm]>(*this)[mid]) {
- cimg::swap((*this)[indm],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
- }
- if ((*this)[mid]>(*this)[indM]) {
- cimg::swap((*this)[indM],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indM],permutations[mid]);
- }
- if ((*this)[indm]>(*this)[mid]) {
- cimg::swap((*this)[indm],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
- }
- } else {
- if ((*this)[indm]<(*this)[mid]) {
- cimg::swap((*this)[indm],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
- }
- if ((*this)[mid]<(*this)[indM]) {
- cimg::swap((*this)[indM],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indM],permutations[mid]);
- }
- if ((*this)[indm]<(*this)[mid]) {
- cimg::swap((*this)[indm],(*this)[mid]);
- if (is_permutations) cimg::swap(permutations[indm],permutations[mid]);
- }
- }
- if (indM - indm>=3) {
- const T pivot = (*this)[mid];
- long i = indm, j = indM;
- if (is_increasing) {
- do {
- while ((*this)[i]<pivot) ++i;
- while ((*this)[j]>pivot) --j;
- if (i<=j) {
- if (is_permutations) cimg::swap(permutations[i],permutations[j]);
- cimg::swap((*this)[i++],(*this)[j--]);
- }
- } while (i<=j);
- } else {
- do {
- while ((*this)[i]>pivot) ++i;
- while ((*this)[j]<pivot) --j;
- if (i<=j) {
- if (is_permutations) cimg::swap(permutations[i],permutations[j]);
- cimg::swap((*this)[i++],(*this)[j--]);
- }
- } while (i<=j);
- }
- if (indm<j) _quicksort(indm,j,permutations,is_increasing,is_permutations);
- if (i<indM) _quicksort(i,indM,permutations,is_increasing,is_permutations);
- }
- }
- return *this;
- }
- //! Compute the SVD of the instance image, viewed as a general matrix.
- /**
- Compute the SVD decomposition \c *this=U*S*V' where \c U and \c V are orthogonal matrices
- and \c S is a diagonal matrix. \c V' denotes the matrix transpose of \c V.
- \param[out] U First matrix of the SVD product.
- \param[out] S Coefficients of the second (diagonal) matrix of the SVD product.
- These coefficients are stored as a vector.
- \param[out] V Third matrix of the SVD product.
- \param sorting Tells if the diagonal coefficients are sorted (in decreasing order).
- \param max_iteration Maximum number of iterations considered for the algorithm convergence.
- \param lambda Epsilon used for the algorithm convergence.
- \note The instance matrix can be computed from \c U,\c S and \c V by
- \code
- const CImg<> A; // Input matrix (assumed to contain some values)
- CImg<> U,S,V;
- A.SVD(U,S,V)
- \endcode
- **/
- template<typename t>
- const CImg<T>& SVD(CImg<t>& U, CImg<t>& S, CImg<t>& V, const bool sorting=true,
- const unsigned int max_iteration=40, const float lambda=0) const {
- typedef _cimg_Ttfloat Ttfloat;
- const Ttfloat epsilon = (Ttfloat)1e-25;
- if (is_empty()) { U.assign(); S.assign(); V.assign(); }
- else if (_depth!=1 || _spectrum!=1)
- throw CImgInstanceException(_cimg_instance
- "SVD(): Instance has invalid dimensions (depth or channels different from 1).",
- cimg_instance);
- else {
- U = *this;
- if (lambda!=0) {
- const unsigned int delta = std::min(U._width,U._height);
- for (unsigned int i = 0; i<delta; ++i) U(i,i) = (t)(U(i,i) + lambda);
- }
- if (S.size()<_width) S.assign(1,_width);
- if (V._width<_width || V._height<_height) V.assign(_width,_width);
- CImg<t> rv1(_width);
- Ttfloat anorm = 0, c, f, g = 0, h, s, scale = 0;
- int l = 0;
- cimg_forX(U,i) {
- l = i + 1;
- rv1[i] = scale*g;
- g = s = scale = 0;
- if (i<height()) {
- for (int k = i; k<height(); ++k) scale+=cimg::abs(U(i,k));
- if (scale) {
- for (int k = i; k<height(); ++k) {
- U(i,k)/=scale;
- s+=U(i,k)*U(i,k);
- }
- f = U(i,i);
- g = (Ttfloat)((f>=0?-1:1)*std::sqrt(s));
- h = f*g - s;
- U(i,i) = f - g;
- for (int j = l; j<width(); ++j) {
- s = 0;
- for (int k=i; k<height(); ++k) s+=U(i,k)*U(j,k);
- f = s/h;
- for (int k = i; k<height(); ++k) U(j,k)+=f*U(i,k);
- }
- for (int k = i; k<height(); ++k) U(i,k)*=scale;
- }
- }
- S[i] = scale*g;
- g = s = scale = 0;
- if (i<height() && i!=width() - 1) {
- for (int k = l; k<width(); ++k) scale+=cimg::abs(U(k,i));
- if (scale) {
- for (int k = l; k<width(); ++k) {
- U(k,i)/=scale;
- s+=U(k,i)*U(k,i);
- }
- f = U(l,i);
- g = (Ttfloat)((f>=0?-1:1)*std::sqrt(s));
- h = f*g - s;
- U(l,i) = f - g;
- for (int k = l; k<width(); ++k) rv1[k] = U(k,i)/h;
- for (int j = l; j<height(); ++j) {
- s = 0;
- for (int k = l; k<width(); ++k) s+=U(k,j)*U(k,i);
- for (int k = l; k<width(); ++k) U(k,j)+=s*rv1[k];
- }
- for (int k = l; k<width(); ++k) U(k,i)*=scale;
- }
- }
- anorm = (Ttfloat)std::max((float)anorm,(float)(cimg::abs(S[i]) + cimg::abs(rv1[i])));
- }
- for (int i = width() - 1; i>=0; --i) {
- if (i<width() - 1) {
- if (g) {
- for (int j = l; j<width(); ++j) V(i,j) =(U(j,i)/U(l,i))/g;
- for (int j = l; j<width(); ++j) {
- s = 0;
- for (int k = l; k<width(); ++k) s+=U(k,i)*V(j,k);
- for (int k = l; k<width(); ++k) V(j,k)+=s*V(i,k);
- }
- }
- for (int j = l; j<width(); ++j) V(j,i) = V(i,j) = (t)0.;
- }
- V(i,i) = (t)1;
- g = rv1[i];
- l = i;
- }
- for (int i = std::min(width(),height()) - 1; i>=0; --i) {
- l = i + 1;
- g = S[i];
- for (int j = l; j<width(); ++j) U(j,i) = 0;
- if (g) {
- g = 1/g;
- for (int j = l; j<width(); ++j) {
- s = 0;
- for (int k = l; k<height(); ++k) s+=U(i,k)*U(j,k);
- f = (s/U(i,i))*g;
- for (int k = i; k<height(); ++k) U(j,k)+=f*U(i,k);
- }
- for (int j = i; j<height(); ++j) U(i,j)*= g;
- } else for (int j = i; j<height(); ++j) U(i,j) = 0;
- ++U(i,i);
- }
- for (int k = width() - 1; k>=0; --k) {
- int nm = 0;
- for (unsigned int its = 0; its<max_iteration; ++its) {
- bool flag = true;
- for (l = k; l>=1; --l) {
- nm = l - 1;
- if ((cimg::abs(rv1[l]) + anorm)==anorm) { flag = false; break; }
- if ((cimg::abs(S[nm]) + anorm)==anorm) break;
- }
- if (flag) {
- c = 0;
- s = 1;
- for (int i = l; i<=k; ++i) {
- f = s*rv1[i];
- rv1[i] = c*rv1[i];
- if ((cimg::abs(f) + anorm)==anorm) break;
- g = S[i];
- h = cimg::_hypot(f,g);
- S[i] = h;
- h = 1/h;
- c = g*h;
- s = -f*h;
- cimg_forY(U,j) {
- const t y = U(nm,j), z = U(i,j);
- U(nm,j) = y*c + z*s;
- U(i,j) = z*c - y*s;
- }
- }
- }
- const t z = S[k];
- if (l==k) { if (z<0) { S[k] = -z; cimg_forX(U,j) V(k,j) = -V(k,j); } break; }
- nm = k - 1;
- t x = S[l], y = S[nm];
- g = rv1[nm];
- h = rv1[k];
- f = ((y - z)*(y + z) + (g - h)*(g + h))/std::max(epsilon,(Ttfloat)2*h*y);
- g = cimg::_hypot(f,(Ttfloat)1);
- f = ((x - z)*(x + z) + h*((y/(f + (f>=0?g:-g))) - h))/std::max(epsilon,(Ttfloat)x);
- c = s = 1;
- for (int j = l; j<=nm; ++j) {
- const int i = j + 1;
- g = rv1[i];
- h = s*g;
- g = c*g;
- t y1 = S[i], z1 = cimg::_hypot(f,h);
- rv1[j] = z1;
- c = f/std::max(epsilon,(Ttfloat)z1);
- s = h/std::max(epsilon,(Ttfloat)z1);
- f = x*c + g*s;
- g = g*c - x*s;
- h = y1*s;
- y1*=c;
- cimg_forX(U,jj) {
- const t x2 = V(j,jj), z2 = V(i,jj);
- V(j,jj) = x2*c + z2*s;
- V(i,jj) = z2*c - x2*s;
- }
- z1 = cimg::_hypot(f,h);
- S[j] = z1;
- if (z1) {
- z1 = 1/std::max(epsilon,(Ttfloat)z1);
- c = f*z1;
- s = h*z1;
- }
- f = c*g + s*y1;
- x = c*y1 - s*g;
- cimg_forY(U,jj) {
- const t y2 = U(j,jj), z2 = U(i,jj);
- U(j,jj) = y2*c + z2*s;
- U(i,jj) = z2*c - y2*s;
- }
- }
- rv1[l] = 0;
- rv1[k] = f;
- S[k] = x;
- }
- }
- if (sorting) {
- CImg<intT> permutations;
- CImg<t> tmp(_width);
- S.sort(permutations,false);
- cimg_forY(U,k) {
- cimg_forY(permutations,y) tmp(y) = U(permutations(y),k);
- std::memcpy(U.data(0,k),tmp._data,sizeof(t)*_width);
- }
- cimg_forY(V,k) {
- cimg_forY(permutations,y) tmp(y) = V(permutations(y),k);
- std::memcpy(V.data(0,k),tmp._data,sizeof(t)*_width);
- }
- }
- }
- return *this;
- }
- //! Compute the SVD of the instance image, viewed as a general matrix.
- /**
- \return A list of three images <tt>[U; S; V]</tt>, whose meaning is similar as in
- SVD(CImg<t>&,CImg<t>&,CImg<t>&,bool,unsigned int,float) const.
- **/
- CImgList<Tfloat> get_SVD(const bool sorting=true,
- const unsigned int max_iteration=40, const float lambda=0) const {
- CImgList<Tfloat> res(3);
- SVD(res[0],res[1],res[2],sorting,max_iteration,lambda);
- return res;
- }
- // [internal] Compute the LU decomposition of a permuted matrix.
- template<typename t>
- CImg<T>& _LU(CImg<t>& indx, bool& d) {
- const int N = width();
- int imax = 0;
- CImg<Tfloat> vv(N);
- indx.assign(N);
- d = true;
- bool return0 = false;
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=512))
- cimg_forX(*this,i) {
- Tfloat vmax = 0;
- cimg_forX(*this,j) {
- const Tfloat tmp = cimg::abs((*this)(j,i));
- if (tmp>vmax) vmax = tmp;
- }
- if (vmax==0) return0 = true; else vv[i] = 1/vmax;
- }
- if (return0) { indx.fill(0); return fill(0); }
- cimg_forX(*this,j) {
- for (int i = 0; i<j; ++i) {
- Tfloat sum = (*this)(j,i);
- for (int k = 0; k<i; ++k) sum-=(*this)(k,i)*(*this)(j,k);
- (*this)(j,i) = (T)sum;
- }
- Tfloat vmax = 0;
- for (int i = j; i<width(); ++i) {
- Tfloat sum = (*this)(j,i);
- for (int k = 0; k<j; ++k) sum-=(*this)(k,i)*(*this)(j,k);
- (*this)(j,i) = (T)sum;
- const Tfloat tmp = vv[i]*cimg::abs(sum);
- if (tmp>=vmax) { vmax = tmp; imax = i; }
- }
- if (j!=imax) {
- cimg_forX(*this,k) cimg::swap((*this)(k,imax),(*this)(k,j));
- d = !d;
- vv[imax] = vv[j];
- }
- indx[j] = (t)imax;
- if ((*this)(j,j)==0) (*this)(j,j) = (T)1e-20;
- if (j<N) {
- const Tfloat tmp = 1/(Tfloat)(*this)(j,j);
- for (int i = j + 1; i<N; ++i) (*this)(j,i) = (T)((*this)(j,i)*tmp);
- }
- }
- return *this;
- }
- //! Compute the projection of the instance matrix onto the specified dictionary.
- /**
- Find the best matching projection of selected matrix onto the span of an over-complete dictionary D,
- using the orthogonal projection or (opt. Orthogonal) Matching Pursuit algorithm.
- Instance image must a 2D-matrix in which each column represent a signal to project.
- \param dictionary A matrix in which each column is an element of the dictionary D.
- \param method Tell what projection method is applied. It can be:
- - 0 = orthogonal projection (default).
- - 1 = matching pursuit.
- - 2 = matching pursuit, with a single orthogonal projection step at the end.
- - >=3 = orthogonal matching pursuit where an orthogonal projection step is performed
- every 'method-2' iterations.
- \param max_iter Sets the max number of iterations processed for each signal.
- If set to '0' (default), 'max_iter' is set to the number of dictionary columns.
- (only meaningful for matching pursuit and its variants).
- \param max_residual Gives a stopping criterion on signal reconstruction accuracy.
- (only meaningful for matching pursuit and its variants).
- \return A matrix W whose columns correspond to the sparse weights of associated to each input matrix column.
- Thus, the matrix product D*W is an approximation of the input matrix.
- **/
- template<typename t>
- CImg<T>& project_matrix(const CImg<t>& dictionary, const unsigned int method=0,
- const unsigned int max_iter=0, const double max_residual=1e-6) {
- return get_project_matrix(dictionary,method,max_iter,max_residual).move_to(*this);
- }
- template<typename t>
- CImg<Tfloat> get_project_matrix(const CImg<t>& dictionary, const unsigned int method=0,
- const unsigned int max_iter=0, const double max_residual=1e-6) const {
- if (_depth!=1 || _spectrum!=1)
- throw CImgInstanceException(_cimg_instance
- "project_matrix(): Instance image is not a matrix.",
- cimg_instance);
- if (dictionary._height!=_height || dictionary._depth!=1 || dictionary._spectrum!=1)
- throw CImgArgumentException(_cimg_instance
- "project_matrix(): Specified dictionary (%u,%u,%u,%u) has an invalid size.",
- cimg_instance,
- dictionary._width,dictionary._height,dictionary._depth,dictionary._spectrum);
- if (!method) return get_solve(dictionary,true);
- CImg<Tfloat> W(_width,dictionary._width,1,1,0);
- // Compute dictionary norm and normalize it.
- CImg<Tfloat> D(dictionary,false), Dnorm(D._width);
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=2 && _width*_height>=32))
- cimg_forX(Dnorm,d) {
- Tfloat norm = 0;
- cimg_forY(D,y) norm+=cimg::sqr(D(d,y));
- Dnorm[d] = std::max((Tfloat)1e-8,std::sqrt(norm));
- }
- cimg_forXY(D,d,y) D(d,y)/=Dnorm[d];
- // Matching pursuit.
- const unsigned int proj_step = method<3?1:method - 2;
- bool is_orthoproj = false;
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=2 && _width*_height>=32))
- cimg_forX(*this,x) {
- CImg<Tfloat> S = get_column(x);
- const CImg<Tfloat> S0 = method<2?CImg<Tfloat>():S;
- Tfloat residual = S.magnitude()/S._height;
- const unsigned int nmax = max_iter?max_iter:D._width;
- for (unsigned int n = 0; n<nmax && residual>max_residual; ++n) {
- // Find best matching column in D.
- int dmax = 0;
- Tfloat absdotmax = 0, dotmax = 0;
- cimg_pragma_openmp(parallel for cimg_openmp_if(D._width>=2 && D._width*D._height>=32))
- cimg_forX(D,d) {
- Tfloat _dot = 0;
- cimg_forY(D,y) _dot+=S[y]*D(d,y);
- Tfloat absdot = cimg::abs(_dot);
- cimg_pragma_openmp(critical(get_project_matrix)) {
- if (absdot>absdotmax) {
- absdotmax = absdot;
- dotmax = _dot;
- dmax = d;
- }
- }
- }
- if (!n || method<3 || n%proj_step) {
- // Matching Pursuit: Subtract component to signal.
- W(x,dmax)+=dotmax;
- residual = 0;
- cimg_forY(S,y) {
- S[y]-=dotmax*D(dmax,y);
- residual+=cimg::sqr(S[y]);
- }
- residual = std::sqrt(residual)/S._height;
- is_orthoproj = false;
- } else {
- // Orthogonal Matching Pursuit: Orthogonal projection step.
- W(x,dmax) = 1; // Used as a marker only.
- unsigned int nbW = 0;
- cimg_forY(W,d) if (W(x,d)) ++nbW;
- CImg<Tfloat> sD(nbW,D._height);
- CImg<uintT> inds(nbW);
- int sd = 0;
- cimg_forY(W,d) if (W(x,d)) {
- cimg_forY(sD,y) sD(sd,y) = D(d,y);
- inds[sd++] = d;
- }
- S0.get_solve(sD,true).move_to(sD); // sD is now a one-column vector of weights
- // Recompute residual signal.
- S = S0;
- cimg_forY(sD,k) {
- const Tfloat weight = sD[k];
- const unsigned int ind = inds[k];
- W(x,ind) = weight;
- cimg_forY(S,y) S[y]-=weight*D(ind,y);
- }
- residual = S.magnitude()/S._height;
- is_orthoproj = true;
- }
- }
- // Perform last orthoprojection step if needed.
- if (method>=2 && !is_orthoproj) {
- unsigned int nbW = 0;
- cimg_forY(W,d) if (W(x,d)) ++nbW;
- if (nbW) { // Avoid degenerated case where 0 coefs are used
- CImg<Tfloat> sD(nbW,D._height);
- CImg<uintT> inds(nbW);
- int sd = 0;
- cimg_forY(W,d) if (W(x,d)) {
- cimg_forY(sD,y) sD(sd,y) = D(d,y);
- inds[sd++] = d;
- }
- S0.get_solve(sD,true).move_to(sD);
- cimg_forY(sD,k) W(x,inds[k]) = sD[k];
- }
- }
- }
- // Normalize resulting coefficients according to initial (non-normalized) dictionary.
- cimg_forXY(W,x,y) W(x,y)/=Dnorm[y];
- return W;
- }
- //! Compute minimal path in a graph, using the Dijkstra algorithm.
- /**
- \param distance An object having operator()(unsigned int i, unsigned int j) which returns distance
- between two nodes (i,j).
- \param nb_nodes Number of graph nodes.
- \param starting_node Index of the starting node.
- \param ending_node Index of the ending node (set to ~0U to ignore ending node).
- \param previous_node Array that gives the previous node index in the path to the starting node
- (optional parameter).
- \return Array of distances of each node to the starting node.
- **/
- template<typename tf, typename t>
- static CImg<T> dijkstra(const tf& distance, const unsigned int nb_nodes,
- const unsigned int starting_node, const unsigned int ending_node,
- CImg<t>& previous_node) {
- if (starting_node>=nb_nodes)
- throw CImgArgumentException("CImg<%s>::dijkstra(): Specified index of starting node %u is higher "
- "than number of nodes %u.",
- pixel_type(),starting_node,nb_nodes);
- CImg<T> dist(1,nb_nodes,1,1,cimg::type<T>::max());
- dist(starting_node) = 0;
- previous_node.assign(1,nb_nodes,1,1,(t)-1);
- previous_node(starting_node) = (t)starting_node;
- CImg<uintT> Q(nb_nodes);
- cimg_forX(Q,u) Q(u) = (unsigned int)u;
- cimg::swap(Q(starting_node),Q(0));
- unsigned int sizeQ = nb_nodes;
- while (sizeQ) {
- // Update neighbors from minimal vertex
- const unsigned int umin = Q(0);
- if (umin==ending_node) sizeQ = 0;
- else {
- const T dmin = dist(umin);
- const T infty = cimg::type<T>::max();
- for (unsigned int q = 1; q<sizeQ; ++q) {
- const unsigned int v = Q(q);
- const T d = (T)distance(v,umin);
- if (d<infty) {
- const T alt = dmin + d;
- if (alt<dist(v)) {
- dist(v) = alt;
- previous_node(v) = (t)umin;
- const T distpos = dist(Q(q));
- for (unsigned int pos = q, par = 0; pos && distpos<dist(Q(par=(pos + 1)/2 - 1)); pos=par)
- cimg::swap(Q(pos),Q(par));
- }
- }
- }
- // Remove minimal vertex from queue
- Q(0) = Q(--sizeQ);
- const T distpos = dist(Q(0));
- for (unsigned int pos = 0, left = 0, right = 0;
- ((right=2*(pos + 1),(left=right - 1))<sizeQ && distpos>dist(Q(left))) ||
- (right<sizeQ && distpos>dist(Q(right)));) {
- if (right<sizeQ) {
- if (dist(Q(left))<dist(Q(right))) { cimg::swap(Q(pos),Q(left)); pos = left; }
- else { cimg::swap(Q(pos),Q(right)); pos = right; }
- } else { cimg::swap(Q(pos),Q(left)); pos = left; }
- }
- }
- }
- return dist;
- }
- //! Return minimal path in a graph, using the Dijkstra algorithm.
- template<typename tf, typename t>
- static CImg<T> dijkstra(const tf& distance, const unsigned int nb_nodes,
- const unsigned int starting_node, const unsigned int ending_node=~0U) {
- CImg<uintT> foo;
- return dijkstra(distance,nb_nodes,starting_node,ending_node,foo);
- }
- //! Return minimal path in a graph, using the Dijkstra algorithm.
- /**
- \param starting_node Index of the starting node.
- \param ending_node Index of the ending node.
- \param previous_node Array that gives the previous node index in the path to the starting node
- (optional parameter).
- \return Array of distances of each node to the starting node.
- \note image instance corresponds to the adjacency matrix of the graph.
- **/
- template<typename t>
- CImg<T>& dijkstra(const unsigned int starting_node, const unsigned int ending_node,
- CImg<t>& previous_node) {
- return get_dijkstra(starting_node,ending_node,previous_node).move_to(*this);
- }
- //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance.
- template<typename t>
- CImg<T> get_dijkstra(const unsigned int starting_node, const unsigned int ending_node,
- CImg<t>& previous_node) const {
- if (_width!=_height || _depth!=1 || _spectrum!=1)
- throw CImgInstanceException(_cimg_instance
- "dijkstra(): Instance is not a graph adjacency matrix.",
- cimg_instance);
- return dijkstra(*this,_width,starting_node,ending_node,previous_node);
- }
- //! Return minimal path in a graph, using the Dijkstra algorithm.
- CImg<T>& dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) {
- return get_dijkstra(starting_node,ending_node).move_to(*this);
- }
- //! Return minimal path in a graph, using the Dijkstra algorithm \newinstance.
- CImg<Tfloat> get_dijkstra(const unsigned int starting_node, const unsigned int ending_node=~0U) const {
- CImg<uintT> foo;
- return get_dijkstra(starting_node,ending_node,foo);
- }
- //! Return an image containing the character codes of specified string.
- /**
- \param str input C-string to encode as an image.
- \param is_last_zero Tells if the ending \c '0' character appear in the resulting image.
- \param is_shared Return result that shares its buffer with \p str.
- **/
- static CImg<T> string(const char *const str, const bool is_last_zero=true, const bool is_shared=false) {
- if (!str) return CImg<T>();
- return CImg<T>(str,(unsigned int)std::strlen(str) + (is_last_zero?1:0),1,1,1,is_shared);
- }
- //! Return a \c 1x1 image containing specified value.
- /**
- \param a0 First vector value.
- **/
- static CImg<T> row_vector(const T& a0) {
- return vector(a0);
- }
- //! Return a \c 2x1 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- **/
- static CImg<T> row_vector(const T& a0, const T& a1) {
- CImg<T> r(2,1);
- r[0] = a0; r[1] = a1;
- return r;
- }
- //! Return a \c 3x1 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- \param a2 Third vector value.
- **/
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2) {
- CImg<T> r(3,1);
- r[0] = a0; r[1] = a1; r[2] = a2;
- return r;
- }
- //! Return a \c 4x1 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- \param a2 Third vector value.
- \param a3 Fourth vector value.
- **/
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3) {
- CImg<T> r(4,1);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3;
- return r;
- }
- //! Return a \c 5x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
- CImg<T> r(5,1);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4;
- return r;
- }
- //! Return a \c 6x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
- CImg<T> r(6,1);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5;
- return r;
- }
- //! Return a \c 7x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6) {
- CImg<T> r(7,1);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6;
- return r;
- }
- //! Return a \c 8x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7) {
- CImg<T> r(8,1);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7;
- return r;
- }
- //! Return a \c 9x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8) {
- CImg<T> r(9,1);
- 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;
- return r;
- }
- //! Return a \c 10x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9) {
- CImg<T> r(10,1);
- 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;
- return r;
- }
- //! Return a \c 11x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10) {
- CImg<T> r(11,1);
- 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;
- r[10] = a10;
- return r;
- }
- //! Return a \c 12x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11) {
- CImg<T> r(12,1);
- 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;
- r[10] = a10; r[11] = a11;
- return r;
- }
- //! Return a \c 13x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12) {
- CImg<T> r(13,1);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12;
- return r;
- }
- //! Return a \c 14x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13) {
- CImg<T> r(14,1);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13;
- return r;
- }
- //! Return a \c 15x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13, const T& a14) {
- CImg<T> r(15,1);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14;
- return r;
- }
- //! Return a \c 16x1 image containing specified values.
- static CImg<T> row_vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13, const T& a14, const T& a15) {
- CImg<T> r(16,1);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14; r[15] = a15;
- return r;
- }
- //! Return a \c 1x1 image containing specified value.
- /**
- \param a0 First vector value.
- **/
- static CImg<T> vector(const T& a0) {
- CImg<T> r(1,1);
- r[0] = a0;
- return r;
- }
- //! Return a \c 1x2 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- **/
- static CImg<T> vector(const T& a0, const T& a1) {
- CImg<T> r(1,2);
- r[0] = a0; r[1] = a1;
- return r;
- }
- //! Return a \c 1x3 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- \param a2 Third vector value.
- **/
- static CImg<T> vector(const T& a0, const T& a1, const T& a2) {
- CImg<T> r(1,3);
- r[0] = a0; r[1] = a1; r[2] = a2;
- return r;
- }
- //! Return a \c 1x4 image containing specified values.
- /**
- \param a0 First vector value.
- \param a1 Second vector value.
- \param a2 Third vector value.
- \param a3 Fourth vector value.
- **/
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3) {
- CImg<T> r(1,4);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3;
- return r;
- }
- //! Return a \c 1x5 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
- CImg<T> r(1,5);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4;
- return r;
- }
- //! Return a \c 1x6 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
- CImg<T> r(1,6);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5;
- return r;
- }
- //! Return a \c 1x7 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6) {
- CImg<T> r(1,7);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6;
- return r;
- }
- //! Return a \c 1x8 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7) {
- CImg<T> r(1,8);
- r[0] = a0; r[1] = a1; r[2] = a2; r[3] = a3; r[4] = a4; r[5] = a5; r[6] = a6; r[7] = a7;
- return r;
- }
- //! Return a \c 1x9 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8) {
- CImg<T> r(1,9);
- 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;
- return r;
- }
- //! Return a \c 1x10 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9) {
- CImg<T> r(1,10);
- 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;
- return r;
- }
- //! Return a \c 1x11 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10) {
- CImg<T> r(1,11);
- 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;
- r[10] = a10;
- return r;
- }
- //! Return a \c 1x12 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11) {
- CImg<T> r(1,12);
- 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;
- r[10] = a10; r[11] = a11;
- return r;
- }
- //! Return a \c 1x13 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12) {
- CImg<T> r(1,13);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12;
- return r;
- }
- //! Return a \c 1x14 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13) {
- CImg<T> r(1,14);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13;
- return r;
- }
- //! Return a \c 1x15 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13, const T& a14) {
- CImg<T> r(1,15);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14;
- return r;
- }
- //! Return a \c 1x16 image containing specified values.
- static CImg<T> vector(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13, const T& a14, const T& a15) {
- CImg<T> r(1,16);
- 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;
- r[10] = a10; r[11] = a11; r[12] = a12; r[13] = a13; r[14] = a14; r[15] = a15;
- return r;
- }
- //! Return a 1x1 matrix containing specified coefficients.
- /**
- \param a0 First matrix value.
- \note Equivalent to vector(const T&).
- **/
- static CImg<T> matrix(const T& a0) {
- return vector(a0);
- }
- //! Return a 2x2 matrix containing specified coefficients.
- /**
- \param a0 First matrix value.
- \param a1 Second matrix value.
- \param a2 Third matrix value.
- \param a3 Fourth matrix value.
- **/
- static CImg<T> matrix(const T& a0, const T& a1,
- const T& a2, const T& a3) {
- CImg<T> r(2,2); T *ptr = r._data;
- *(ptr++) = a0; *(ptr++) = a1;
- *(ptr++) = a2; *(ptr++) = a3;
- return r;
- }
- //! Return a 3x3 matrix containing specified coefficients.
- /**
- \param a0 First matrix value.
- \param a1 Second matrix value.
- \param a2 Third matrix value.
- \param a3 Fourth matrix value.
- \param a4 Fifth matrix value.
- \param a5 Sixth matrix value.
- \param a6 Seventh matrix value.
- \param a7 Eighth matrix value.
- \param a8 Ninth matrix value.
- **/
- static CImg<T> matrix(const T& a0, const T& a1, const T& a2,
- const T& a3, const T& a4, const T& a5,
- const T& a6, const T& a7, const T& a8) {
- CImg<T> r(3,3); T *ptr = r._data;
- *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2;
- *(ptr++) = a3; *(ptr++) = a4; *(ptr++) = a5;
- *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8;
- return r;
- }
- //! Return a 4x4 matrix containing specified coefficients.
- static CImg<T> matrix(const T& a0, const T& a1, const T& a2, const T& a3,
- const T& a4, const T& a5, const T& a6, const T& a7,
- const T& a8, const T& a9, const T& a10, const T& a11,
- const T& a12, const T& a13, const T& a14, const T& a15) {
- CImg<T> r(4,4); T *ptr = r._data;
- *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3;
- *(ptr++) = a4; *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7;
- *(ptr++) = a8; *(ptr++) = a9; *(ptr++) = a10; *(ptr++) = a11;
- *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14; *(ptr++) = a15;
- return r;
- }
- //! Return a 5x5 matrix containing specified coefficients.
- static CImg<T> matrix(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4,
- const T& a5, const T& a6, const T& a7, const T& a8, const T& a9,
- const T& a10, const T& a11, const T& a12, const T& a13, const T& a14,
- const T& a15, const T& a16, const T& a17, const T& a18, const T& a19,
- const T& a20, const T& a21, const T& a22, const T& a23, const T& a24) {
- CImg<T> r(5,5); T *ptr = r._data;
- *(ptr++) = a0; *(ptr++) = a1; *(ptr++) = a2; *(ptr++) = a3; *(ptr++) = a4;
- *(ptr++) = a5; *(ptr++) = a6; *(ptr++) = a7; *(ptr++) = a8; *(ptr++) = a9;
- *(ptr++) = a10; *(ptr++) = a11; *(ptr++) = a12; *(ptr++) = a13; *(ptr++) = a14;
- *(ptr++) = a15; *(ptr++) = a16; *(ptr++) = a17; *(ptr++) = a18; *(ptr++) = a19;
- *(ptr++) = a20; *(ptr++) = a21; *(ptr++) = a22; *(ptr++) = a23; *(ptr++) = a24;
- return r;
- }
- //! Return a 1x1 symmetric matrix containing specified coefficients.
- /**
- \param a0 First matrix value.
- \note Equivalent to vector(const T&).
- **/
- static CImg<T> tensor(const T& a0) {
- return matrix(a0);
- }
- //! Return a 2x2 symmetric matrix tensor containing specified coefficients.
- static CImg<T> tensor(const T& a0, const T& a1, const T& a2) {
- return matrix(a0,a1,a1,a2);
- }
- //! Return a 3x3 symmetric matrix containing specified coefficients.
- static CImg<T> tensor(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4, const T& a5) {
- return matrix(a0,a1,a2,a1,a3,a4,a2,a4,a5);
- }
- //! Return a 1x1 diagonal matrix containing specified coefficients.
- static CImg<T> diagonal(const T& a0) {
- return matrix(a0);
- }
- //! Return a 2x2 diagonal matrix containing specified coefficients.
- static CImg<T> diagonal(const T& a0, const T& a1) {
- return matrix(a0,0,0,a1);
- }
- //! Return a 3x3 diagonal matrix containing specified coefficients.
- static CImg<T> diagonal(const T& a0, const T& a1, const T& a2) {
- return matrix(a0,0,0,0,a1,0,0,0,a2);
- }
- //! Return a 4x4 diagonal matrix containing specified coefficients.
- static CImg<T> diagonal(const T& a0, const T& a1, const T& a2, const T& a3) {
- return matrix(a0,0,0,0,0,a1,0,0,0,0,a2,0,0,0,0,a3);
- }
- //! Return a 5x5 diagonal matrix containing specified coefficients.
- static CImg<T> diagonal(const T& a0, const T& a1, const T& a2, const T& a3, const T& a4) {
- 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);
- }
- //! Return a NxN identity matrix.
- /**
- \param N Dimension of the matrix.
- **/
- static CImg<T> identity_matrix(const unsigned int N) {
- CImg<T> res(N,N,1,1,0);
- cimg_forX(res,x) res(x,x) = 1;
- return res;
- }
- //! Return a N-numbered sequence vector from \p a0 to \p a1.
- /**
- \param N Size of the resulting vector.
- \param a0 Starting value of the sequence.
- \param a1 Ending value of the sequence.
- **/
- static CImg<T> sequence(const unsigned int N, const T& a0, const T& a1) {
- if (N) return CImg<T>(1,N).sequence(a0,a1);
- return CImg<T>();
- }
- //! Return a 3x3 rotation matrix from an { axis + angle } or a quaternion.
- /**
- \param x X-coordinate of the rotation axis, or first quaternion coordinate.
- \param y Y-coordinate of the rotation axis, or second quaternion coordinate.
- \param z Z-coordinate of the rotation axis, or third quaternion coordinate.
- \param w Angle of the rotation axis (in degree), or fourth quaternion coordinate.
- \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion (x,y,z,w).
- **/
- static CImg<T> rotation_matrix(const float x, const float y, const float z, const float w,
- const bool is_quaternion=false) {
- double X, Y, Z, W, N;
- if (is_quaternion) {
- N = std::sqrt((double)x*x + (double)y*y + (double)z*z + (double)w*w);
- if (N>0) { X = x/N; Y = y/N; Z = z/N; W = w/N; }
- else { X = Y = Z = 0; W = 1; }
- 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),
- (T)(2*X*W + 2*Y*Z),(T)(X*X - Y*Y + Z*Z - W*W),(T)(2*Z*W - 2*X*Y),
- (T)(2*Y*W - 2*X*Z),(T)(2*X*Y + 2*Z*W),(T)(X*X - Y*Y - Z*Z + W*W));
- }
- N = cimg::hypot((double)x,(double)y,(double)z);
- if (N>0) { X = x/N; Y = y/N; Z = z/N; }
- else { X = Y = 0; Z = 1; }
- const double ang = w*cimg::PI/180, c = std::cos(ang), omc = 1 - c, s = std::sin(ang);
- return CImg<T>::matrix((T)(X*X*omc + c),(T)(X*Y*omc - Z*s),(T)(X*Z*omc + Y*s),
- (T)(X*Y*omc + Z*s),(T)(Y*Y*omc + c),(T)(Y*Z*omc - X*s),
- (T)(X*Z*omc - Y*s),(T)(Y*Z*omc + X*s),(T)(Z*Z*omc + c));
- }
- //@}
- //-----------------------------------
- //
- //! \name Value Manipulation
- //@{
- //-----------------------------------
- //! Fill all pixel values with specified value.
- /**
- \param val Fill value.
- **/
- CImg<T>& fill(const T& val) {
- if (is_empty()) return *this;
- if (val && sizeof(T)!=1) cimg_for(*this,ptrd,T) *ptrd = val;
- else std::memset(_data,(int)(ulongT)val,sizeof(T)*size()); // Double cast to allow val to be (void*)
- return *this;
- }
- //! Fill all pixel values with specified value \newinstance.
- CImg<T> get_fill(const T& val) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val);
- }
- //! Fill sequentially all pixel values with specified values.
- /**
- \param val0 First fill value.
- \param val1 Second fill value.
- **/
- CImg<T>& fill(const T& val0, const T& val1) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 1;
- for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; }
- if (ptrd!=ptre + 1) *(ptrd++) = val0;
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 2;
- for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; }
- ptre+=2;
- switch (ptre - ptrd) {
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 3;
- for (ptrd = _data; ptrd<ptre; ) { *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; }
- ptre+=3;
- switch (ptre - ptrd) {
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 4;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
- }
- ptre+=4;
- switch (ptre - ptrd) {
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 5;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- }
- ptre+=5;
- switch (ptre - ptrd) {
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 6;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6;
- }
- ptre+=6;
- switch (ptre - ptrd) {
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 7;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3;
- *(ptrd++) = val4; *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7;
- }
- ptre+=7;
- switch (ptre - ptrd) {
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 8;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2;
- *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8;
- }
- ptre+=8;
- switch (ptre - ptrd) {
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 9;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
- *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9;
- }
- ptre+=9;
- switch (ptre - ptrd) {
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 10;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4;
- *(ptrd++) = val5; *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9;
- *(ptrd++) = val10;
- }
- ptre+=10;
- switch (ptre - ptrd) {
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 11;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
- }
- ptre+=11;
- switch (ptre - ptrd) {
- case 11 : *(--ptre) = val10; // fallthrough
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
- val11);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 12;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
- *(ptrd++) = val12;
- }
- ptre+=12;
- switch (ptre - ptrd) {
- case 12 : *(--ptre) = val11; // fallthrough
- case 11 : *(--ptre) = val10; // fallthrough
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
- val11,val12);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 13;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
- *(ptrd++) = val12; *(ptrd++) = val13;
- }
- ptre+=13;
- switch (ptre - ptrd) {
- case 13 : *(--ptre) = val12; // fallthrough
- case 12 : *(--ptre) = val11; // fallthrough
- case 11 : *(--ptre) = val10; // fallthrough
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
- val11,val12,val13);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13, const T& val14) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 14;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
- *(ptrd++) = val12; *(ptrd++) = val13; *(ptrd++) = val14;
- }
- ptre+=14;
- switch (ptre - ptrd) {
- case 14 : *(--ptre) = val13; // fallthrough
- case 13 : *(--ptre) = val12; // fallthrough
- case 12 : *(--ptre) = val11; // fallthrough
- case 11 : *(--ptre) = val10; // fallthrough
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13, const T& val14) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
- val11,val12,val13,val14);
- }
- //! Fill sequentially all pixel values with specified values \overloading.
- CImg<T>& fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13, const T& val14, const T& val15) {
- if (is_empty()) return *this;
- T *ptrd, *ptre = end() - 15;
- for (ptrd = _data; ptrd<ptre; ) {
- *(ptrd++) = val0; *(ptrd++) = val1; *(ptrd++) = val2; *(ptrd++) = val3; *(ptrd++) = val4; *(ptrd++) = val5;
- *(ptrd++) = val6; *(ptrd++) = val7; *(ptrd++) = val8; *(ptrd++) = val9; *(ptrd++) = val10; *(ptrd++) = val11;
- *(ptrd++) = val12; *(ptrd++) = val13; *(ptrd++) = val14; *(ptrd++) = val15;
- }
- ptre+=15;
- switch (ptre - ptrd) {
- case 15 : *(--ptre) = val14; // fallthrough
- case 14 : *(--ptre) = val13; // fallthrough
- case 13 : *(--ptre) = val12; // fallthrough
- case 12 : *(--ptre) = val11; // fallthrough
- case 11 : *(--ptre) = val10; // fallthrough
- case 10 : *(--ptre) = val9; // fallthrough
- case 9 : *(--ptre) = val8; // fallthrough
- case 8 : *(--ptre) = val7; // fallthrough
- case 7 : *(--ptre) = val6; // fallthrough
- case 6 : *(--ptre) = val5; // fallthrough
- case 5 : *(--ptre) = val4; // fallthrough
- case 4 : *(--ptre) = val3; // fallthrough
- case 3 : *(--ptre) = val2; // fallthrough
- case 2 : *(--ptre) = val1; // fallthrough
- case 1 : *(--ptre) = val0; // fallthrough
- }
- return *this;
- }
- //! Fill sequentially all pixel values with specified values \newinstance.
- CImg<T> get_fill(const T& val0, const T& val1, const T& val2, const T& val3, const T& val4, const T& val5,
- const T& val6, const T& val7, const T& val8, const T& val9, const T& val10, const T& val11,
- const T& val12, const T& val13, const T& val14, const T& val15) const {
- return CImg<T>(_width,_height,_depth,_spectrum).fill(val0,val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,
- val11,val12,val13,val14,val15);
- }
- //! Fill sequentially pixel values according to a given expression.
- /**
- \param expression C-string describing a math formula, or a sequence of values.
- \param repeat_values In case a list of values is provided, tells if this list must be repeated for the filling.
- \param allow_formula Tells that mathematical formulas are authorized for the filling.
- \param list_images In case of a mathematical expression, attach a list of images to the specified expression.
- **/
- CImg<T>& fill(const char *const expression, const bool repeat_values, const bool allow_formula=true,
- CImgList<T> *const list_images=0) {
- return _fill(expression,repeat_values,allow_formula?1:0,list_images,"fill",0);
- }
- // 'formula_mode' = { 0 = does not allow formula | 1 = allow formula |
- // 2 = allow formula and do not fill image values }.
- CImg<T>& _fill(const char *const expression, const bool repeat_values, const unsigned int formula_mode,
- CImgList<T> *const list_images, const char *const calling_function, const CImg<T> *provides_copy) {
- if (is_empty() || !expression || !*expression) return *this;
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- CImg<charT> is_error;
- bool is_value_sequence = false;
- cimg_abort_init;
- if (formula_mode) {
- // Try to pre-detect regular value sequence to avoid exception thrown by _cimg_math_parser.
- double value;
- char sep;
- const int err = cimg_sscanf(expression,"%lf %c",&value,&sep);
- if (err==1 || (err==2 && sep==',')) {
- if (err==1) { if (formula_mode==2) return *this; return fill((T)value); }
- else is_value_sequence = true;
- }
- // Try to fill values according to a formula.
- _cimg_abort_init_openmp;
- if (!is_value_sequence) try {
- CImg<T> base = provides_copy?provides_copy->get_shared():get_shared();
- _cimg_math_parser mp(expression + (*expression=='>' || *expression=='<' ||
- *expression=='*' || *expression==':'),
- calling_function,base,this,list_images,true);
- if (!provides_copy && expression && *expression!='>' && *expression!='<' && *expression!=':' &&
- mp.need_input_copy)
- base.assign().assign(*this,false); // Needs input copy
- // Determine 2nd largest image dimension (used as axis for inner loop in parallelized evaluation).
- unsigned int M;
- if (mp.result_dim) {
- M = cimg::max(_width,_height,_depth);
- M = M==_width?std::max(_height,_depth):M==_height?std::max(_width,_depth):std::max(_width,_height);
- } else {
- M = cimg::max(_width,_height,_depth,_spectrum);
- M = M==_width?cimg::max(_height,_depth,_spectrum):
- M==_height?cimg::max(_width,_depth,_spectrum):
- M==_depth?cimg::max(_width,_height,_spectrum):cimg::max(_width,_height,_depth);
- }
- bool do_in_parallel = false;
- #if cimg_use_openmp!=0
- cimg_openmp_if(*expression=='*' || *expression==':' ||
- (mp.is_parallelizable && M>=(cimg_openmp_sizefactor)*320 && size()/M>=2))
- do_in_parallel = true;
- #endif
- if (mp.result_dim) { // Vector-valued expression
- const unsigned int N = std::min(mp.result_dim,_spectrum);
- const ulongT whd = (ulongT)_width*_height*_depth;
- T *ptrd = *expression=='<'?_data + _width*_height*_depth - 1:_data;
- if (*expression=='<') {
- CImg<doubleT> res(1,mp.result_dim);
- mp.begin_t();
- cimg_rofYZ(*this,y,z) {
- cimg_abort_test;
- if (formula_mode==2) cimg_rofX(*this,x) mp(x,y,z,0);
- else cimg_rofX(*this,x) {
- mp(x,y,z,0,res._data);
- const double *ptrs = res._data;
- T *_ptrd = ptrd--; for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; }
- }
- }
- mp.end_t();
- } else if (*expression=='>' || !do_in_parallel) {
- CImg<doubleT> res(1,mp.result_dim);
- mp.begin_t();
- cimg_forYZ(*this,y,z) {
- cimg_abort_test;
- if (formula_mode==2) cimg_forX(*this,x) mp(x,y,z,0);
- else cimg_forX(*this,x) {
- mp(x,y,z,0,res._data);
- const double *ptrs = res._data;
- T *_ptrd = ptrd++; for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; }
- }
- }
- mp.end_t();
- } else {
- #if cimg_use_openmp!=0
- cimg_pragma_openmp(parallel)
- {
- _cimg_math_parser
- *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
- &lmp = *_mp;
- lmp.is_fill = true;
- cimg_pragma_openmp(barrier)
- lmp.begin_t();
- #define _cimg_fill_openmp_vector(_YZ,_y,_z,_X,_x,_sx,_sy,_sz,_off) \
- cimg_pragma_openmp(for cimg_openmp_collapse(2)) \
- cimg_for##_YZ(*this,_y,_z) _cimg_abort_try_openmp { \
- cimg_abort_test; \
- if (formula_mode==2) cimg_for##_X(*this,_x) lmp(x,y,z,0); \
- else { \
- CImg<doubleT> res(1,lmp.result_dim); \
- T *__ptrd = data(_sx,_sy,_sz,0); \
- const ulongT off = (ulongT)_off; \
- cimg_for##_X(*this,_x) { \
- lmp(x,y,z,0,res._data); \
- const double *ptrs = res._data; \
- T *_ptrd = __ptrd; \
- for (unsigned int n = N; n>0; --n) { *_ptrd = (T)(*ptrs++); _ptrd+=whd; } \
- __ptrd+=off; \
- } \
- } \
- } _cimg_abort_catch_openmp _cimg_abort_catch_fill_openmp
- if (M==_width) { _cimg_fill_openmp_vector(YZ,y,z,X,x,0,y,z,1) }
- else if (M==_height) { _cimg_fill_openmp_vector(XZ,x,z,Y,y,x,0,z,_width) }
- else { _cimg_fill_openmp_vector(XY,x,y,Z,z,x,y,0,_width*_height) }
- lmp.end_t();
- cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
- if (&lmp!=&mp) delete &lmp;
- }
- #endif
- }
- } else { // Scalar-valued expression
- T *ptrd = *expression=='<'?end() - 1:_data;
- if (*expression=='<') {
- mp.begin_t();
- if (formula_mode==2) cimg_rofYZC(*this,y,z,c) { cimg_abort_test; cimg_rofX(*this,x) mp(x,y,z,c); }
- else cimg_rofYZC(*this,y,z,c) { cimg_abort_test; cimg_rofX(*this,x) *(ptrd--) = (T)mp(x,y,z,c); }
- mp.end_t();
- } else if (*expression=='>' || !do_in_parallel) {
- mp.begin_t();
- if (formula_mode==2) cimg_forYZC(*this,y,z,c) { cimg_abort_test; cimg_forX(*this,x) mp(x,y,z,c); }
- else cimg_forYZC(*this,y,z,c) { cimg_abort_test; cimg_forX(*this,x) *(ptrd++) = (T)mp(x,y,z,c); }
- mp.end_t();
- } else {
- #if cimg_use_openmp!=0
- cimg_pragma_openmp(parallel)
- {
- _cimg_math_parser
- *const _mp = omp_get_thread_num()?new _cimg_math_parser(mp):&mp,
- &lmp = *_mp;
- lmp.is_fill = true;
- cimg_pragma_openmp(barrier)
- lmp.begin_t();
- #define _cimg_fill_openmp_scalar(_YZC,_y,_z,_c,_X,_x,_sx,_sy,_sz,_sc,_off) \
- cimg_pragma_openmp(for cimg_openmp_collapse(3)) \
- cimg_for##_YZC(*this,_y,_z,_c) _cimg_abort_try_openmp { \
- cimg_abort_test; \
- if (formula_mode==2) cimg_for##_X(*this,_x) lmp(x,y,z,c); \
- else { \
- T *_ptrd = data(_sx,_sy,_sz,_sc); \
- const ulongT off = (ulongT)_off; \
- cimg_for##_X(*this,_x) { *_ptrd = (T)lmp(x,y,z,c); _ptrd+=off; } \
- } \
- } _cimg_abort_catch_openmp _cimg_abort_catch_fill_openmp
- if (M==_width) { _cimg_fill_openmp_scalar(YZC,y,z,c,X,x,0,y,z,c,1) }
- else if (M==_height) { _cimg_fill_openmp_scalar(XZC,x,z,c,Y,y,x,0,z,c,_width) }
- else if (M==_depth) { _cimg_fill_openmp_scalar(XYC,x,y,c,Z,z,x,y,0,c,_width*_height) }
- else { _cimg_fill_openmp_scalar(XYZ,x,y,z,C,c,x,y,z,0,_width*_height*_depth) }
- lmp.end_t();
- cimg_pragma_openmp(barrier) cimg_pragma_openmp(critical) { lmp.merge(mp); }
- if (&lmp!=&mp) delete &lmp;
- }
- #endif
- }
- }
- mp.end();
- } catch (CImgException& e) { CImg<charT>::string(e._message).move_to(is_error); }
- }
- // Try to fill values according to a value sequence.
- if (!formula_mode || is_value_sequence || is_error) {
- CImg<charT> item(256);
- char sep = 0;
- const char *nexpression = expression;
- ulongT nb = 0;
- const ulongT siz = size();
- T *ptrd = _data;
- for (double val = 0; *nexpression && nb<siz; ++nb) {
- sep = 0;
- const int err = cimg_sscanf(nexpression,"%255[ \n\t0-9.eEinfa+-]%c",item._data,&sep);
- if (err>0 && cimg_sscanf(item,"%lf",&val)==1 && (sep==',' || sep==';' || err==1)) {
- nexpression+=std::strlen(item) + (err>1);
- *(ptrd++) = (T)val;
- } else break;
- }
- cimg::exception_mode(omode);
- if (nb<siz && (sep || *nexpression)) {
- if (is_error) throw CImgArgumentException("%s",is_error._data);
- else throw CImgArgumentException(_cimg_instance
- "%s(): Invalid sequence of filling values '%s'.",
- cimg_instance,calling_function,expression);
- }
- if (repeat_values && nb && nb<siz)
- for (T *ptrs = _data, *const ptre = _data + siz; ptrd<ptre; ++ptrs) *(ptrd++) = *ptrs;
- }
- cimg::exception_mode(omode);
- cimg_abort_test;
- return *this;
- }
- //! Fill sequentially pixel values according to a given expression \newinstance.
- CImg<T> get_fill(const char *const expression, const bool repeat_values, const bool allow_formula=true,
- CImgList<T> *const list_images=0) const {
- return (+*this).fill(expression,repeat_values,allow_formula?1:0,list_images);
- }
- //! Fill sequentially pixel values according to the values found in another image.
- /**
- \param values Image containing the values used for the filling.
- \param repeat_values In case there are less values than necessary in \c values, tells if these values must be
- repeated for the filling.
- **/
- template<typename t>
- CImg<T>& fill(const CImg<t>& values, const bool repeat_values=true) {
- if (is_empty() || !values) return *this;
- T *ptrd = _data, *ptre = ptrd + size();
- for (t *ptrs = values._data, *ptrs_end = ptrs + values.size(); ptrs<ptrs_end && ptrd<ptre; ++ptrs)
- *(ptrd++) = (T)*ptrs;
- if (repeat_values && ptrd<ptre) for (T *ptrs = _data; ptrd<ptre; ++ptrs) *(ptrd++) = *ptrs;
- return *this;
- }
- //! Fill sequentially pixel values according to the values found in another image \newinstance.
- template<typename t>
- CImg<T> get_fill(const CImg<t>& values, const bool repeat_values=true) const {
- return repeat_values?CImg<T>(_width,_height,_depth,_spectrum).fill(values,repeat_values):
- (+*this).fill(values,repeat_values);
- }
- //! Fill pixel values along the X-axis at a specified pixel position.
- /**
- \param y Y-coordinate of the filled column.
- \param z Z-coordinate of the filled column.
- \param c C-coordinate of the filled column.
- \param a0 First fill value.
- **/
- CImg<T>& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const int a0, ...) {
- #define _cimg_fill1(x,y,z,c,off,siz,t) { \
- va_list ap; va_start(ap,a0); T *ptrd = data(x,y,z,c); *ptrd = (T)a0; \
- for (unsigned int k = 1; k<siz; ++k) { ptrd+=off; *ptrd = (T)va_arg(ap,t); } \
- va_end(ap); }
- if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,int);
- return *this;
- }
- //! Fill pixel values along the X-axis at a specified pixel position \overloading.
- CImg<T>& fillX(const unsigned int y, const unsigned int z, const unsigned int c, const double a0, ...) {
- if (y<_height && z<_depth && c<_spectrum) _cimg_fill1(0,y,z,c,1,_width,double);
- return *this;
- }
- //! Fill pixel values along the Y-axis at a specified pixel position.
- /**
- \param x X-coordinate of the filled row.
- \param z Z-coordinate of the filled row.
- \param c C-coordinate of the filled row.
- \param a0 First fill value.
- **/
- CImg<T>& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const int a0, ...) {
- if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,int);
- return *this;
- }
- //! Fill pixel values along the Y-axis at a specified pixel position \overloading.
- CImg<T>& fillY(const unsigned int x, const unsigned int z, const unsigned int c, const double a0, ...) {
- if (x<_width && z<_depth && c<_spectrum) _cimg_fill1(x,0,z,c,_width,_height,double);
- return *this;
- }
- //! Fill pixel values along the Z-axis at a specified pixel position.
- /**
- \param x X-coordinate of the filled slice.
- \param y Y-coordinate of the filled slice.
- \param c C-coordinate of the filled slice.
- \param a0 First fill value.
- **/
- CImg<T>& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const int a0, ...) {
- const ulongT wh = (ulongT)_width*_height;
- if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,int);
- return *this;
- }
- //! Fill pixel values along the Z-axis at a specified pixel position \overloading.
- CImg<T>& fillZ(const unsigned int x, const unsigned int y, const unsigned int c, const double a0, ...) {
- const ulongT wh = (ulongT)_width*_height;
- if (x<_width && y<_height && c<_spectrum) _cimg_fill1(x,y,0,c,wh,_depth,double);
- return *this;
- }
- //! Fill pixel values along the C-axis at a specified pixel position.
- /**
- \param x X-coordinate of the filled channel.
- \param y Y-coordinate of the filled channel.
- \param z Z-coordinate of the filled channel.
- \param a0 First filling value.
- **/
- CImg<T>& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const int a0, ...) {
- const ulongT whd = (ulongT)_width*_height*_depth;
- if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,int);
- return *this;
- }
- //! Fill pixel values along the C-axis at a specified pixel position \overloading.
- CImg<T>& fillC(const unsigned int x, const unsigned int y, const unsigned int z, const double a0, ...) {
- const ulongT whd = (ulongT)_width*_height*_depth;
- if (x<_width && y<_height && z<_depth) _cimg_fill1(x,y,z,0,whd,_spectrum,double);
- return *this;
- }
- //! Discard specified sequence of values in the image buffer, along a specific axis.
- /**
- \param values Sequence of values to discard.
- \param axis Axis along which the values are discarded. If set to \c 0 (default value)
- the method does it for all the buffer values and returns a one-column vector.
- \note Discarded values will change the image geometry, so the resulting image
- is returned as a one-column vector.
- **/
- template<typename t>
- CImg<T>& discard(const CImg<t>& values, const char axis=0) {
- if (is_empty() || !values) return *this;
- return get_discard(values,axis).move_to(*this);
- }
- template<typename t>
- CImg<T> get_discard(const CImg<t>& values, const char axis=0) const {
- if (!values) return +*this;
- CImg<T> res;
- if (is_empty()) return res;
- const ulongT vsiz = values.size();
- const char _axis = cimg::lowercase(axis);
- ulongT j = 0;
- unsigned int k = 0;
- int i0 = 0;
- res.assign(width(),height(),depth(),spectrum());
- switch (_axis) {
- case 'x' : {
- cimg_forX(*this,i) {
- if ((*this)(i)!=(T)values[j]) {
- if (j) --i;
- res.draw_image(k,get_columns(i0,i));
- k+=i - i0 + 1; i0 = i + 1; j = 0;
- } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
- }
- if (i0<width()) { res.draw_image(k,get_columns(i0,width() - 1)); k+=width() - i0; }
- res.resize(k,-100,-100,-100,0);
- } break;
- case 'y' : {
- cimg_forY(*this,i) {
- if ((*this)(0,i)!=(T)values[j]) {
- if (j) --i;
- res.draw_image(0,k,get_rows(i0,i));
- k+=i - i0 + 1; i0 = i + 1; j = 0;
- } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
- }
- if (i0<height()) { res.draw_image(0,k,get_rows(i0,height() - 1)); k+=height() - i0; }
- res.resize(-100,k,-100,-100,0);
- } break;
- case 'z' : {
- cimg_forZ(*this,i) {
- if ((*this)(0,0,i)!=(T)values[j]) {
- if (j) --i;
- res.draw_image(0,0,k,get_slices(i0,i));
- k+=i - i0 + 1; i0 = i + 1; j = 0;
- } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
- }
- if (i0<depth()) { res.draw_image(0,0,k,get_slices(i0,height() - 1)); k+=depth() - i0; }
- res.resize(-100,-100,k,-100,0);
- } break;
- case 'c' : {
- cimg_forC(*this,i) {
- if ((*this)(0,0,0,i)!=(T)values[j]) {
- if (j) --i;
- res.draw_image(0,0,0,k,get_channels(i0,i));
- k+=i - i0 + 1; i0 = i + 1; j = 0;
- } else { ++j; if (j>=vsiz) { j = 0; i0 = i + 1; } }
- }
- if (i0<spectrum()) { res.draw_image(0,0,k,get_channels(i0,height() - 1)); k+=spectrum() - i0; }
- res.resize(-100,-100,-100,k,0);
- } break;
- default : {
- const ulongT siz = size();
- res.unroll('y');
- if (vsiz==1) { // Optimized version for a single discard value
- const T val = (T)values[0];
- cimg_foroff(*this,i) {
- const T _val = (T)_data[i];
- if (_val!=val) res[k++] = _val;
- }
- } else { // Generic version
- cimg_foroff(*this,i) {
- if ((*this)[i]!=(T)values[j]) {
- if (j) --i;
- std::memcpy(res._data + k,_data + i0,(i - i0 + 1)*sizeof(T));
- k+=i - i0 + 1; i0 = (int)i + 1; j = 0;
- } else { ++j; if (j>=vsiz) { j = 0; i0 = (int)i + 1; }}
- }
- if ((ulongT)i0<siz) { std::memcpy(res._data + k,_data + i0,(siz - i0)*sizeof(T)); k+=siz - i0; }
- }
- res.resize(1,k,1,1,0);
- }
- }
- return res;
- }
- //! Discard neighboring duplicates in the image buffer, along the specified axis.
- CImg<T>& discard(const char axis=0) {
- return get_discard(axis).move_to(*this);
- }
- //! Discard neighboring duplicates in the image buffer, along the specified axis \newinstance.
- CImg<T> get_discard(const char axis=0) const {
- CImg<T> res;
- if (is_empty()) return res;
- const char _axis = cimg::lowercase(axis);
- T current = *_data?(T)0:(T)1;
- int j = 0;
- res.assign(width(),height(),depth(),spectrum());
- switch (_axis) {
- case 'x' : {
- cimg_forX(*this,i)
- if ((*this)(i)!=current) { res.draw_image(j++,get_column(i)); current = (*this)(i); }
- res.resize(j,-100,-100,-100,0);
- } break;
- case 'y' : {
- cimg_forY(*this,i)
- if ((*this)(0,i)!=current) { res.draw_image(0,j++,get_row(i)); current = (*this)(0,i); }
- res.resize(-100,j,-100,-100,0);
- } break;
- case 'z' : {
- cimg_forZ(*this,i)
- if ((*this)(0,0,i)!=current) { res.draw_image(0,0,j++,get_slice(i)); current = (*this)(0,0,i); }
- res.resize(-100,-100,j,-100,0);
- } break;
- case 'c' : {
- cimg_forC(*this,i)
- if ((*this)(0,0,0,i)!=current) { res.draw_image(0,0,0,j++,get_channel(i)); current = (*this)(0,0,0,i); }
- res.resize(-100,-100,-100,j,0);
- } break;
- default : {
- res.unroll('y');
- cimg_foroff(*this,i) {
- const T val = (*this)[i];
- if (val!=current) res[j++] = current = val;
- }
- res.resize(-100,j,-100,-100,0);
- }
- }
- return res;
- }
- //! Invert endianness of all pixel values.
- /**
- **/
- CImg<T>& invert_endianness() {
- cimg::invert_endianness(_data,size());
- return *this;
- }
- //! Invert endianness of all pixel values \newinstance.
- CImg<T> get_invert_endianness() const {
- return (+*this).invert_endianness();
- }
- //! Fill image with random values in specified range.
- /**
- \param val_min Minimal authorized random value.
- \param val_max Maximal authorized random value.
- \note Random variables are uniformly distributed in [val_min,val_max].
- **/
- CImg<T>& rand(const T& val_min, const T& val_max) {
- const float delta = (float)val_max - (float)val_min + (cimg::type<T>::is_float()?0:1);
- if (cimg::type<T>::is_float()) cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),524288)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) _data[off] = (T)(val_min + delta*cimg::rand(1,&rng));
- cimg::srand(rng);
- } else cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),524288)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) _data[off] = std::min(val_max,(T)(val_min + delta*cimg::rand(1,&rng)));
- cimg::srand(rng);
- }
- return *this;
- }
- //! Fill image with random values in specified range \newinstance.
- CImg<T> get_rand(const T& val_min, const T& val_max) const {
- return (+*this).rand(val_min,val_max);
- }
- //! Round pixel values.
- /**
- \param y Rounding precision.
- \param rounding_type Rounding type. Can be:
- - \c -1: Backward.
- - \c 0: Nearest.
- - \c 1: Forward.
- **/
- CImg<T>& round(const double y=1, const int rounding_type=0) {
- if (y>0) cimg_openmp_for(*this,cimg::round(*ptr,y,rounding_type),8192);
- return *this;
- }
- //! Round pixel values \newinstance.
- CImg<T> get_round(const double y=1, const unsigned int rounding_type=0) const {
- return (+*this).round(y,rounding_type);
- }
- //! Add random noise to pixel values.
- /**
- \param sigma Amplitude of the random additive noise. If \p sigma<0, it stands for a percentage of the
- global value range.
- \param noise_type Type of additive noise (can be \p 0=gaussian, \p 1=uniform, \p 2=Salt and Pepper,
- \p 3=Poisson or \p 4=Rician).
- \return A reference to the modified image instance.
- \note
- - For Poisson noise (\p noise_type=3), parameter \p sigma is ignored, as Poisson noise only depends on
- the image value itself.
- - Function \p CImg<T>::get_noise() is also defined. It returns a non-shared modified copy of the image instance.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_noise(40);
- (img,res.normalize(0,255)).display();
- \endcode
- \image html ref_noise.jpg
- **/
- CImg<T>& noise(const double sigma, const unsigned int noise_type=0) {
- if (is_empty()) return *this;
- const Tfloat vmin = (Tfloat)cimg::type<T>::min(), vmax = (Tfloat)cimg::type<T>::max();
- Tfloat nsigma = (Tfloat)sigma, m = 0, M = 0;
- if (nsigma==0 && noise_type!=3) return *this;
- if (nsigma<0 || noise_type==2) m = (Tfloat)min_max(M);
- if (nsigma<0) nsigma = (Tfloat)(-nsigma*(M-m)/100.);
- switch (noise_type) {
- case 0 : { // Gaussian noise
- cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) {
- Tfloat val = (Tfloat)(_data[off] + nsigma*cimg::grand(&rng));
- if (val>vmax) val = vmax;
- if (val<vmin) val = vmin;
- _data[off] = (T)val;
- }
- cimg::srand(rng);
- }
- } break;
- case 1 : { // Uniform noise
- cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) {
- Tfloat val = (Tfloat)(_data[off] + nsigma*cimg::rand(-1,1,&rng));
- if (val>vmax) val = vmax;
- if (val<vmin) val = vmin;
- _data[off] = (T)val;
- }
- cimg::srand(rng);
- }
- } break;
- case 2 : { // Salt & Pepper noise
- if (nsigma<0) nsigma = -nsigma;
- if (M==m) {
- if (cimg::type<T>::is_float()) { --m; ++M; }
- else { m = (Tfloat)cimg::type<T>::min(); M = (Tfloat)cimg::type<T>::max(); }
- }
- cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) if (cimg::rand(100,&rng)<nsigma) _data[off] = (T)(cimg::rand(1,&rng)<0.5?M:m);
- cimg::srand(rng);
- }
- } break;
- case 3 : { // Poisson Noise
- cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) _data[off] = (T)cimg::prand(_data[off],&rng);
- cimg::srand(rng);
- }
- } break;
- case 4 : { // Rice noise
- const Tfloat sqrt2 = (Tfloat)std::sqrt(2.);
- cimg_pragma_openmp(parallel cimg_openmp_if_size(size(),131072)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_rofoff(*this,off) {
- const Tfloat
- val0 = (Tfloat)_data[off]/sqrt2,
- re = (Tfloat)(val0 + nsigma*cimg::grand(&rng)),
- im = (Tfloat)(val0 + nsigma*cimg::grand(&rng));
- Tfloat val = cimg::hypot(re,im);
- if (val>vmax) val = vmax;
- if (val<vmin) val = vmin;
- _data[off] = (T)val;
- }
- cimg::srand(rng);
- }
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "noise(): Invalid specified noise type %d "
- "(should be { 0=gaussian | 1=uniform | 2=salt&Pepper | 3=poisson }).",
- cimg_instance,
- noise_type);
- }
- return *this;
- }
- //! Add random noise to pixel values \newinstance.
- CImg<T> get_noise(const double sigma, const unsigned int noise_type=0) const {
- return (+*this).noise(sigma,noise_type);
- }
- //! Linearly normalize pixel values.
- /**
- \param min_value Minimum desired value of the resulting image.
- \param max_value Maximum desired value of the resulting image.
- \param constant_case_ratio In case of instance image having a constant value, tell what ratio
- of [min_value,max_value] is used to fill the normalized image
- (=0 for min_value, =1 for max_value, =0.5 for (min_value + max_value)/2).
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_normalize(160,220);
- (img,res).display();
- \endcode
- \image html ref_normalize2.jpg
- **/
- CImg<T>& normalize(const T& min_value, const T& max_value,
- const float constant_case_ratio=0) {
- if (is_empty()) return *this;
- const T a = min_value<max_value?min_value:max_value, b = min_value<max_value?max_value:min_value;
- T m, M = max_min(m);
- const Tfloat fm = (Tfloat)m, fM = (Tfloat)M;
- if (m==M)
- return fill(constant_case_ratio==0?a:
- constant_case_ratio==1?b:
- (T)((1 - constant_case_ratio)*a + constant_case_ratio*b));
- if (m!=a || M!=b) cimg_rof(*this,ptrd,T) *ptrd = (T)((*ptrd - fm)/(fM - fm)*(b - a) + a);
- return *this;
- }
- //! Linearly normalize pixel values \newinstance.
- CImg<Tfloat> get_normalize(const T& min_value, const T& max_value,
- const float ratio_if_constant_image=0) const {
- return CImg<Tfloat>(*this,false).normalize((Tfloat)min_value,(Tfloat)max_value,ratio_if_constant_image);
- }
- //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm.
- /**
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_normalize();
- (img,res.normalize(0,255)).display();
- \endcode
- \image html ref_normalize.jpg
- **/
- CImg<T>& normalize() {
- const ulongT whd = (ulongT)_width*_height*_depth;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- T *ptrd = data(0,y,z,0);
- cimg_forX(*this,x) {
- const T *ptrs = ptrd;
- float n = 0;
- cimg_forC(*this,c) { n+=cimg::sqr((float)*ptrs); ptrs+=whd; }
- n = (float)std::sqrt(n);
- T *_ptrd = ptrd++;
- if (n>0) cimg_forC(*this,c) { *_ptrd = (T)(*_ptrd/n); _ptrd+=whd; }
- else cimg_forC(*this,c) { *_ptrd = (T)0; _ptrd+=whd; }
- }
- }
- return *this;
- }
- //! Normalize multi-valued pixels of the image instance, with respect to their L2-norm \newinstance.
- CImg<Tfloat> get_normalize() const {
- return CImg<Tfloat>(*this,false).normalize();
- }
- //! Compute Lp-norm of each multi-valued pixel of the image instance.
- /**
- \param norm_type Type of computed vector norm (can be \p -1=Linf, or \p greater or equal than 0).
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_norm();
- (img,res.normalize(0,255)).display();
- \endcode
- \image html ref_norm.jpg
- **/
- CImg<T>& norm(const int norm_type=2) {
- if (_spectrum==1 && norm_type) return abs();
- return get_norm(norm_type).move_to(*this);
- }
- //! Compute L2-norm of each multi-valued pixel of the image instance \newinstance.
- CImg<Tfloat> get_norm(const int norm_type=2) const {
- if (is_empty()) return *this;
- if (_spectrum==1 && norm_type) return get_abs();
- const ulongT whd = (ulongT)_width*_height*_depth;
- CImg<Tfloat> res(_width,_height,_depth);
- switch (norm_type) {
- case -1 : { // Linf-norm
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- const ulongT off = (ulongT)offset(0,y,z);
- const T *ptrs = _data + off;
- Tfloat *ptrd = res._data + off;
- cimg_forX(*this,x) {
- Tfloat n = 0;
- const T *_ptrs = ptrs++;
- cimg_forC(*this,c) { const Tfloat val = (Tfloat)cimg::abs(*_ptrs); if (val>n) n = val; _ptrs+=whd; }
- *(ptrd++) = n;
- }
- }
- } break;
- case 0 : { // L0-norm
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- const ulongT off = (ulongT)offset(0,y,z);
- const T *ptrs = _data + off;
- Tfloat *ptrd = res._data + off;
- cimg_forX(*this,x) {
- unsigned int n = 0;
- const T *_ptrs = ptrs++;
- cimg_forC(*this,c) { n+=*_ptrs==0?0:1; _ptrs+=whd; }
- *(ptrd++) = (Tfloat)n;
- }
- }
- } break;
- case 1 : { // L1-norm
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- const ulongT off = (ulongT)offset(0,y,z);
- const T *ptrs = _data + off;
- Tfloat *ptrd = res._data + off;
- cimg_forX(*this,x) {
- Tfloat n = 0;
- const T *_ptrs = ptrs++;
- cimg_forC(*this,c) { n+=cimg::abs(*_ptrs); _ptrs+=whd; }
- *(ptrd++) = n;
- }
- }
- } break;
- case 2 : { // L2-norm
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- const ulongT off = (ulongT)offset(0,y,z);
- const T *ptrs = _data + off;
- Tfloat *ptrd = res._data + off;
- cimg_forX(*this,x) {
- Tfloat n = 0;
- const T *_ptrs = ptrs++;
- cimg_forC(*this,c) { n+=cimg::sqr((Tfloat)*_ptrs); _ptrs+=whd; }
- *(ptrd++) = (Tfloat)std::sqrt((Tfloat)n);
- }
- }
- } break;
- default : { // Linf-norm
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16))
- cimg_forYZ(*this,y,z) {
- const ulongT off = (ulongT)offset(0,y,z);
- const T *ptrs = _data + off;
- Tfloat *ptrd = res._data + off;
- cimg_forX(*this,x) {
- Tfloat n = 0;
- const T *_ptrs = ptrs++;
- cimg_forC(*this,c) { n+=std::pow(cimg::abs((Tfloat)*_ptrs),(Tfloat)norm_type); _ptrs+=whd; }
- *(ptrd++) = (Tfloat)std::pow((Tfloat)n,1/(Tfloat)norm_type);
- }
- }
- }
- }
- return res;
- }
- //! Cut pixel values in specified range.
- /**
- \param min_value Minimum desired value of the resulting image.
- \param max_value Maximum desired value of the resulting image.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_cut(160,220);
- (img,res).display();
- \endcode
- \image html ref_cut.jpg
- **/
- CImg<T>& cut(const T& min_value, const T& max_value) {
- if (is_empty()) return *this;
- const T a = min_value<max_value?min_value:max_value, b = min_value<max_value?max_value:min_value;
- cimg_openmp_for(*this,cimg::cut(*ptr,a,b),32768);
- return *this;
- }
- //! Cut pixel values in specified range \newinstance.
- CImg<T> get_cut(const T& min_value, const T& max_value) const {
- return (+*this).cut(min_value,max_value);
- }
- //! Uniformly quantize pixel values.
- /**
- \param nb_levels Number of quantization levels.
- \param keep_range Tells if resulting values keep the same range as the original ones.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_quantize(4);
- (img,res).display();
- \endcode
- \image html ref_quantize.jpg
- **/
- CImg<T>& quantize(const unsigned int nb_levels, const bool keep_range=true) {
- if (!nb_levels)
- throw CImgArgumentException(_cimg_instance
- "quantize(): Invalid quantization request with 0 values.",
- cimg_instance);
- if (is_empty()) return *this;
- Tfloat m, M = (Tfloat)max_min(m), range = M - m;
- if (range>0) {
- if (keep_range)
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
- cimg_rofoff(*this,off) {
- const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);
- _data[off] = (T)(m + std::min(val,nb_levels - 1)*range/nb_levels);
- } else
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
- cimg_rofoff(*this,off) {
- const unsigned int val = (unsigned int)((_data[off] - m)*nb_levels/range);
- _data[off] = (T)std::min(val,nb_levels - 1);
- }
- }
- return *this;
- }
- //! Uniformly quantize pixel values \newinstance.
- CImg<T> get_quantize(const unsigned int n, const bool keep_range=true) const {
- return (+*this).quantize(n,keep_range);
- }
- //! Threshold pixel values.
- /**
- \param value Threshold value
- \param soft_threshold Tells if soft thresholding must be applied (instead of hard one).
- \param strict_threshold Tells if threshold value is strict.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_threshold(128);
- (img,res.normalize(0,255)).display();
- \endcode
- \image html ref_threshold.jpg
- **/
- CImg<T>& threshold(const T& value, const bool soft_threshold=false, const bool strict_threshold=false) {
- if (is_empty()) return *this;
- if (strict_threshold) {
- if (soft_threshold)
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
- cimg_rofoff(*this,off) {
- const T v = _data[off];
- _data[off] = v>value?(T)(v-value):v<-(float)value?(T)(v + value):(T)0;
- }
- else
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),65536))
- cimg_rofoff(*this,off) _data[off] = _data[off]>value?(T)1:(T)0;
- } else {
- if (soft_threshold)
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32768))
- cimg_rofoff(*this,off) {
- const T v = _data[off];
- _data[off] = v>=value?(T)(v-value):v<=-(float)value?(T)(v + value):(T)0;
- }
- else
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),65536))
- cimg_rofoff(*this,off) _data[off] = _data[off]>=value?(T)1:(T)0;
- }
- return *this;
- }
- //! Threshold pixel values \newinstance.
- CImg<T> get_threshold(const T& value, const bool soft_threshold=false, const bool strict_threshold=false) const {
- return (+*this).threshold(value,soft_threshold,strict_threshold);
- }
- //! Compute the histogram of pixel values.
- /**
- \param nb_levels Number of desired histogram levels.
- \param min_value Minimum pixel value considered for the histogram computation.
- All pixel values lower than \p min_value will not be counted.
- \param max_value Maximum pixel value considered for the histogram computation.
- All pixel values higher than \p max_value will not be counted.
- \note
- - The histogram H of an image I is the 1D function where H(x) counts the number of occurrences of the value x
- in the image I.
- - The resulting histogram is always defined in 1D. Histograms of multi-valued images are not multi-dimensional.
- \par Example
- \code
- const CImg<float> img = CImg<float>("reference.jpg").histogram(256);
- img.display_graph(0,3);
- \endcode
- \image html ref_histogram.jpg
- **/
- CImg<T>& histogram(const unsigned int nb_levels, const T& min_value, const T& max_value) {
- return get_histogram(nb_levels,min_value,max_value).move_to(*this);
- }
- //! Compute the histogram of pixel values \overloading.
- CImg<T>& histogram(const unsigned int nb_levels) {
- return get_histogram(nb_levels).move_to(*this);
- }
- //! Compute the histogram of pixel values \newinstance.
- CImg<ulongT> get_histogram(const unsigned int nb_levels, const T& min_value, const T& max_value) const {
- if (!nb_levels || is_empty()) return CImg<ulongT>();
- const double
- vmin = (double)(min_value<max_value?min_value:max_value),
- vmax = (double)(min_value<max_value?max_value:min_value);
- CImg<ulongT> res(nb_levels,1,1,1,0);
- cimg_rof(*this,ptrs,T) {
- const T val = *ptrs;
- if (val>=vmin && val<=vmax) ++res[val==vmax?nb_levels - 1:(unsigned int)((val - vmin)*nb_levels/(vmax - vmin))];
- }
- return res;
- }
- //! Compute the histogram of pixel values \newinstance.
- CImg<ulongT> get_histogram(const unsigned int nb_levels) const {
- if (!nb_levels || is_empty()) return CImg<ulongT>();
- T vmax = 0, vmin = min_max(vmax);
- return get_histogram(nb_levels,vmin,vmax);
- }
- //! Equalize histogram of pixel values.
- /**
- \param nb_levels Number of histogram levels used for the equalization.
- \param min_value Minimum pixel value considered for the histogram computation.
- All pixel values lower than \p min_value will not be counted.
- \param max_value Maximum pixel value considered for the histogram computation.
- All pixel values higher than \p max_value will not be counted.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), res = img.get_equalize(256);
- (img,res).display();
- \endcode
- \image html ref_equalize.jpg
- **/
- CImg<T>& equalize(const unsigned int nb_levels, const T& min_value, const T& max_value) {
- if (!nb_levels || is_empty()) return *this;
- const T
- vmin = min_value<max_value?min_value:max_value,
- vmax = min_value<max_value?max_value:min_value;
- CImg<ulongT> hist = get_histogram(nb_levels,vmin,vmax);
- ulongT cumul = 0;
- cimg_forX(hist,pos) { cumul+=hist[pos]; hist[pos] = cumul; }
- if (!cumul) cumul = 1;
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),1048576))
- cimg_rofoff(*this,off) {
- const int pos = (int)((_data[off] - vmin)*(nb_levels - 1.)/(vmax - vmin));
- if (pos>=0 && pos<(int)nb_levels) _data[off] = (T)(vmin + (vmax - vmin)*hist[pos]/cumul);
- }
- return *this;
- }
- //! Equalize histogram of pixel values \overloading.
- CImg<T>& equalize(const unsigned int nb_levels) {
- if (!nb_levels || is_empty()) return *this;
- T vmax = 0, vmin = min_max(vmax);
- return equalize(nb_levels,vmin,vmax);
- }
- //! Equalize histogram of pixel values \newinstance.
- CImg<T> get_equalize(const unsigned int nblevels, const T& val_min, const T& val_max) const {
- return (+*this).equalize(nblevels,val_min,val_max);
- }
- //! Equalize histogram of pixel values \newinstance.
- CImg<T> get_equalize(const unsigned int nblevels) const {
- return (+*this).equalize(nblevels);
- }
- //! Index multi-valued pixels regarding to a specified colormap.
- /**
- \param colormap Multi-valued colormap used as the basis for multi-valued pixel indexing.
- \param dithering Level of dithering (0=disable, 1=standard level).
- \param map_indexes Tell if the values of the resulting image are the colormap indices or the colormap vectors.
- \note
- - \p img.index(colormap,dithering,1) is equivalent to <tt>img.index(colormap,dithering,0).map(colormap)</tt>.
- \par Example
- \code
- const CImg<float> img("reference.jpg"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255);
- const CImg<float> res = img.get_index(colormap,1,true);
- (img,res).display();
- \endcode
- \image html ref_index.jpg
- **/
- template<typename t>
- CImg<T>& index(const CImg<t>& colormap, const float dithering=1, const bool map_indexes=false) {
- return get_index(colormap,dithering,map_indexes).move_to(*this);
- }
- //! Index multi-valued pixels regarding to a specified colormap \newinstance.
- template<typename t>
- CImg<typename CImg<t>::Tuint>
- get_index(const CImg<t>& colormap, const float dithering=1, const bool map_indexes=true) const {
- if (colormap._spectrum!=_spectrum)
- throw CImgArgumentException(_cimg_instance
- "index(): Instance and specified colormap (%u,%u,%u,%u,%p) "
- "have incompatible dimensions.",
- cimg_instance,
- colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);
- typedef typename CImg<t>::Tuint tuint;
- if (is_empty()) return CImg<tuint>();
- const ulongT
- whd = (ulongT)_width*_height*_depth,
- pwhd = (ulongT)colormap._width*colormap._height*colormap._depth;
- CImg<tuint> res(_width,_height,_depth,map_indexes?_spectrum:1);
- if (dithering>0) { // Dithered versions
- tuint *ptrd = res._data;
- const float ndithering = cimg::cut(dithering,0,1)/16;
- Tfloat valm = 0, valM = (Tfloat)max_min(valm);
- if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; }
- CImg<Tfloat> cache = get_crop(-1,0,0,0,_width,1,0,_spectrum - 1);
- Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0);
- const ulongT cwhd = (ulongT)cache._width*cache._height*cache._depth;
- switch (_spectrum) {
- case 1 : { // Optimized for scalars
- cimg_forYZ(*this,y,z) {
- if (y<height() - 2) {
- Tfloat *ptrc0 = cache_next; const T *ptrs0 = data(0,y + 1,z,0);
- cimg_forX(*this,x) *(ptrc0++) = (Tfloat)*(ptrs0++);
- }
- Tfloat *ptrs0 = cache_current, *ptrsn0 = cache_next;
- cimg_forX(*this,x) {
- const Tfloat _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0;
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0<ptrp_end; ) {
- const Tfloat pval0 = (Tfloat)*(ptrp0++) - val0, dist = pval0*pval0;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- const Tfloat err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering;
- *ptrs0+=7*err0; *(ptrsn0 - 1)+=3*err0; *(ptrsn0++)+=5*err0; *ptrsn0+=err0;
- if (map_indexes) *(ptrd++) = (tuint)*ptrmin0; else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- cimg::swap(cache_current,cache_next);
- }
- } break;
- case 2 : { // Optimized for 2D vectors
- tuint *ptrd1 = ptrd + whd;
- cimg_forYZ(*this,y,z) {
- if (y<height() - 2) {
- Tfloat *ptrc0 = cache_next, *ptrc1 = ptrc0 + cwhd;
- const T *ptrs0 = data(0,y + 1,z,0), *ptrs1 = ptrs0 + whd;
- cimg_forX(*this,x) { *(ptrc0++) = (Tfloat)*(ptrs0++); *(ptrc1++) = (Tfloat)*(ptrs1++); }
- }
- Tfloat
- *ptrs0 = cache_current, *ptrs1 = ptrs0 + cwhd,
- *ptrsn0 = cache_next, *ptrsn1 = ptrsn0 + cwhd;
- cimg_forX(*this,x) {
- const Tfloat
- _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0,
- _val1 = (Tfloat)*ptrs1, val1 = _val1<valm?valm:_val1>valM?valM:_val1;
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
- const Tfloat
- pval0 = (Tfloat)*(ptrp0++) - val0, pval1 = (Tfloat)*(ptrp1++) - val1,
- dist = pval0*pval0 + pval1*pval1;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- const t *const ptrmin1 = ptrmin0 + pwhd;
- const Tfloat
- err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering,
- err1 = ((*(ptrs1++)=val1) - (Tfloat)*ptrmin1)*ndithering;
- *ptrs0+=7*err0; *ptrs1+=7*err1;
- *(ptrsn0 - 1)+=3*err0; *(ptrsn1 - 1)+=3*err1;
- *(ptrsn0++)+=5*err0; *(ptrsn1++)+=5*err1;
- *ptrsn0+=err0; *ptrsn1+=err1;
- if (map_indexes) { *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*ptrmin1; }
- else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- cimg::swap(cache_current,cache_next);
- }
- } break;
- case 3 : { // Optimized for 3D vectors (colors)
- tuint *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd;
- cimg_forYZ(*this,y,z) {
- if (y<height() - 2) {
- Tfloat *ptrc0 = cache_next, *ptrc1 = ptrc0 + cwhd, *ptrc2 = ptrc1 + cwhd;
- const T *ptrs0 = data(0,y + 1,z,0), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd;
- cimg_forX(*this,x) {
- *(ptrc0++) = (Tfloat)*(ptrs0++); *(ptrc1++) = (Tfloat)*(ptrs1++); *(ptrc2++) = (Tfloat)*(ptrs2++);
- }
- }
- Tfloat
- *ptrs0 = cache_current, *ptrs1 = ptrs0 + cwhd, *ptrs2 = ptrs1 + cwhd,
- *ptrsn0 = cache_next, *ptrsn1 = ptrsn0 + cwhd, *ptrsn2 = ptrsn1 + cwhd;
- cimg_forX(*this,x) {
- const Tfloat
- _val0 = (Tfloat)*ptrs0, val0 = _val0<valm?valm:_val0>valM?valM:_val0,
- _val1 = (Tfloat)*ptrs1, val1 = _val1<valm?valm:_val1>valM?valM:_val1,
- _val2 = (Tfloat)*ptrs2, val2 = _val2<valm?valm:_val2>valM?valM:_val2;
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd,
- *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
- const Tfloat
- pval0 = (Tfloat)*(ptrp0++) - val0,
- pval1 = (Tfloat)*(ptrp1++) - val1,
- pval2 = (Tfloat)*(ptrp2++) - val2,
- dist = pval0*pval0 + pval1*pval1 + pval2*pval2;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- const t *const ptrmin1 = ptrmin0 + pwhd, *const ptrmin2 = ptrmin1 + pwhd;
- const Tfloat
- err0 = ((*(ptrs0++)=val0) - (Tfloat)*ptrmin0)*ndithering,
- err1 = ((*(ptrs1++)=val1) - (Tfloat)*ptrmin1)*ndithering,
- err2 = ((*(ptrs2++)=val2) - (Tfloat)*ptrmin2)*ndithering;
- *ptrs0+=7*err0; *ptrs1+=7*err1; *ptrs2+=7*err2;
- *(ptrsn0 - 1)+=3*err0; *(ptrsn1 - 1)+=3*err1; *(ptrsn2 - 1)+=3*err2;
- *(ptrsn0++)+=5*err0; *(ptrsn1++)+=5*err1; *(ptrsn2++)+=5*err2;
- *ptrsn0+=err0; *ptrsn1+=err1; *ptrsn2+=err2;
- if (map_indexes) {
- *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*ptrmin1; *(ptrd2++) = (tuint)*ptrmin2;
- } else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- cimg::swap(cache_current,cache_next);
- }
- } break;
- default : // Generic version
- cimg_forYZ(*this,y,z) {
- if (y<height() - 2) {
- Tfloat *ptrc = cache_next;
- cimg_forC(*this,c) {
- Tfloat *_ptrc = ptrc; const T *_ptrs = data(0,y + 1,z,c);
- cimg_forX(*this,x) *(_ptrc++) = (Tfloat)*(_ptrs++);
- ptrc+=cwhd;
- }
- }
- Tfloat *ptrs = cache_current, *ptrsn = cache_next;
- cimg_forX(*this,x) {
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin = colormap._data;
- for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp<ptrp_end; ++ptrp) {
- Tfloat dist = 0; Tfloat *_ptrs = ptrs; const t *_ptrp = ptrp;
- cimg_forC(*this,c) {
- const Tfloat _val = *_ptrs, val = _val<valm?valm:_val>valM?valM:_val;
- dist+=cimg::sqr((*_ptrs=val) - (Tfloat)*_ptrp); _ptrs+=cwhd; _ptrp+=pwhd;
- }
- if (dist<distmin) { ptrmin = ptrp; distmin = dist; }
- }
- const t *_ptrmin = ptrmin; Tfloat *_ptrs = ptrs++, *_ptrsn = (ptrsn++) - 1;
- cimg_forC(*this,c) {
- const Tfloat err = (*(_ptrs++) - (Tfloat)*_ptrmin)*ndithering;
- *_ptrs+=7*err; *(_ptrsn++)+=3*err; *(_ptrsn++)+=5*err; *_ptrsn+=err;
- _ptrmin+=pwhd; _ptrs+=cwhd - 1; _ptrsn+=cwhd - 2;
- }
- if (map_indexes) {
- tuint *_ptrd = ptrd++;
- cimg_forC(*this,c) { *_ptrd = (tuint)*ptrmin; _ptrd+=whd; ptrmin+=pwhd; }
- }
- else *(ptrd++) = (tuint)(ptrmin - colormap._data);
- }
- cimg::swap(cache_current,cache_next);
- }
- }
- } else { // Non-dithered versions
- switch (_spectrum) {
- case 1 : { // Optimized for scalars
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
- _height*_depth>=16 && pwhd>=16))
- cimg_forYZ(*this,y,z) {
- tuint *ptrd = res.data(0,y,z);
- for (const T *ptrs0 = data(0,y,z), *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
- const Tfloat val0 = (Tfloat)*(ptrs0++);
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp_end = ptrp0 + pwhd; ptrp0<ptrp_end; ) {
- const Tfloat pval0 = (Tfloat)*(ptrp0++) - val0, dist = pval0*pval0;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- if (map_indexes) *(ptrd++) = (tuint)*ptrmin0; else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- }
- } break;
- case 2 : { // Optimized for 2D vectors
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
- _height*_depth>=16 && pwhd>=16))
- cimg_forYZ(*this,y,z) {
- tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd;
- for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
- const Tfloat val0 = (Tfloat)*(ptrs0++), val1 = (Tfloat)*(ptrs1++);
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
- const Tfloat
- pval0 = (Tfloat)*(ptrp0++) - val0, pval1 = (Tfloat)*(ptrp1++) - val1,
- dist = pval0*pval0 + pval1*pval1;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- if (map_indexes) { *(ptrd++) = (tuint)*ptrmin0; *(ptrd1++) = (tuint)*(ptrmin0 + pwhd); }
- else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- }
- } break;
- case 3 : { // Optimized for 3D vectors (colors)
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
- _height*_depth>=16 && pwhd>=16))
- cimg_forYZ(*this,y,z) {
- tuint *ptrd = res.data(0,y,z), *ptrd1 = ptrd + whd, *ptrd2 = ptrd1 + whd;
- for (const T *ptrs0 = data(0,y,z), *ptrs1 = ptrs0 + whd, *ptrs2 = ptrs1 + whd,
- *ptrs_end = ptrs0 + _width; ptrs0<ptrs_end; ) {
- const Tfloat val0 = (Tfloat)*(ptrs0++), val1 = (Tfloat)*(ptrs1++), val2 = (Tfloat)*(ptrs2++);
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin0 = colormap._data;
- for (const t *ptrp0 = colormap._data, *ptrp1 = ptrp0 + pwhd, *ptrp2 = ptrp1 + pwhd,
- *ptrp_end = ptrp1; ptrp0<ptrp_end; ) {
- const Tfloat
- pval0 = (Tfloat)*(ptrp0++) - val0,
- pval1 = (Tfloat)*(ptrp1++) - val1,
- pval2 = (Tfloat)*(ptrp2++) - val2,
- dist = pval0*pval0 + pval1*pval1 + pval2*pval2;
- if (dist<distmin) { ptrmin0 = ptrp0 - 1; distmin = dist; }
- }
- if (map_indexes) {
- *(ptrd++) = (tuint)*ptrmin0;
- *(ptrd1++) = (tuint)*(ptrmin0 + pwhd);
- *(ptrd2++) = (tuint)*(ptrmin0 + 2*pwhd);
- } else *(ptrd++) = (tuint)(ptrmin0 - colormap._data);
- }
- }
- } break;
- default : // Generic version
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64 &&
- _height*_depth>=16 && pwhd>=16))
- cimg_forYZ(*this,y,z) {
- tuint *ptrd = res.data(0,y,z);
- for (const T *ptrs = data(0,y,z), *ptrs_end = ptrs + _width; ptrs<ptrs_end; ++ptrs) {
- Tfloat distmin = cimg::type<Tfloat>::max(); const t *ptrmin = colormap._data;
- for (const t *ptrp = colormap._data, *ptrp_end = ptrp + pwhd; ptrp<ptrp_end; ++ptrp) {
- Tfloat dist = 0; const T *_ptrs = ptrs; const t *_ptrp = ptrp;
- cimg_forC(*this,c) { dist+=cimg::sqr((Tfloat)*_ptrs - (Tfloat)*_ptrp); _ptrs+=whd; _ptrp+=pwhd; }
- if (dist<distmin) { ptrmin = ptrp; distmin = dist; }
- }
- if (map_indexes) {
- tuint *_ptrd = ptrd++;
- cimg_forC(*this,c) { *_ptrd = (tuint)*ptrmin; _ptrd+=whd; ptrmin+=pwhd; }
- }
- else *(ptrd++) = (tuint)(ptrmin - colormap._data);
- }
- }
- }
- }
- return res;
- }
- //! Map predefined colormap on the scalar (indexed) image instance.
- /**
- \param colormap Multi-valued colormap used for mapping the indexes.
- \param boundary_conditions Boundary conditions.
- Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \par Example
- \code
- const CImg<float> img("reference.jpg"),
- colormap1(3,1,1,3, 0,128,255, 0,128,255, 0,128,255),
- colormap2(3,1,1,3, 255,0,0, 0,255,0, 0,0,255),
- res = img.get_index(colormap1,0).map(colormap2);
- (img,res).display();
- \endcode
- \image html ref_map.jpg
- **/
- template<typename t>
- CImg<T>& map(const CImg<t>& colormap, const unsigned int boundary_conditions=0) {
- return get_map(colormap,boundary_conditions).move_to(*this);
- }
- //! Map predefined colormap on the scalar (indexed) image instance \newinstance.
- template<typename t>
- CImg<t> get_map(const CImg<t>& colormap, const unsigned int boundary_conditions=0) const {
- const ulongT
- whd = (ulongT)_width*_height*_depth, siz = size(),
- cwhd = (ulongT)colormap._width*colormap._height*colormap._depth,
- cwhd2 = 2*cwhd;
- CImg<t> res(_width,_height,_depth,_spectrum*colormap._spectrum);
- switch (colormap._spectrum) {
- case 1 : { // Optimized for scalars
- switch (boundary_conditions) {
- case 3 : // Mirror
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)siz; ++off) {
- const ulongT ind = ((ulongT)_data[off])%cwhd2;
- res[off] = colormap[ind<cwhd?ind:cwhd2 - ind - 1];
- }
- break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)siz; ++off) {
- const ulongT ind = (ulongT)_data[off];
- res[off] = colormap[ind%cwhd];
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)siz; ++off) {
- const longT ind = (longT)_data[off];
- res[off] = colormap[cimg::cut(ind,(longT)0,(longT)cwhd - 1)];
- } break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)siz; ++off) {
- const ulongT ind = (ulongT)_data[off];
- res[off] = ind<cwhd?colormap[ind]:(t)0;
- }
- }
- } break;
- case 2 : { // Optimized for 2D vectors
- const t *const ptrp0 = colormap._data, *const ptrp1 = ptrp0 + cwhd;
- switch (boundary_conditions) {
- case 3 : // Mirror
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT
- _ind = ((ulongT)ptrs[off])%cwhd2,
- ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
- }
- } break;
- case 2 : // Periodic
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = ((ulongT)ptrs[off])%cwhd;
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
- }
- } break;
- case 1 : // Neumann
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind];
- }
- } break;
- default : // Dirichlet
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,2*c), *const ptrd1 = ptrd0 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = (ulongT)ptrs[off];
- if (ind<cwhd) { ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; }
- else ptrd0[off] = ptrd1[off] = (t)0;
- }
- }
- }
- } break;
- case 3 : { // Optimized for 3D vectors (colors)
- const t *const ptrp0 = colormap._data, *ptrp1 = ptrp0 + cwhd, *ptrp2 = ptrp0 + 2*cwhd;
- switch (boundary_conditions) {
- case 3 : // Mirror
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT
- _ind = ((ulongT)ptrs[off])%cwhd2,
- ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
- }
- } break;
- case 2 : // Periodic
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = ((ulongT)ptrs[off])%cwhd;
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
- }
- } break;
- case 1 : // Neumann
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
- ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind];
- }
- } break;
- default : // Dirichlet
- cimg_forC(*this,c) {
- t *const ptrd0 = res.data(0,0,0,3*c), *const ptrd1 = ptrd0 + whd, *const ptrd2 = ptrd1 + whd;
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = (ulongT)ptrs[off];
- if (ind<cwhd) { ptrd0[off] = ptrp0[ind]; ptrd1[off] = ptrp1[ind]; ptrd2[off] = ptrp2[ind]; }
- else ptrd0[off] = ptrd1[off] = ptrd2[off] = (t)0;
- }
- }
- }
- } break;
- default : { // Generic version
- switch (boundary_conditions) {
- case 3 : // Mirror
- cimg_forC(*this,c) {
- t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT
- _ind = ((ulongT)ptrs[off])%cwhd,
- ind = _ind<cwhd?_ind:cwhd2 - _ind - 1;
- t *const _ptrd = ptrd + off;
- const t *const ptrp = &colormap[ind];
- cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
- }
- } break;
- case 2 : // Periodic
- cimg_forC(*this,c) {
- t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = ((ulongT)ptrs[off])%cwhd;
- t *const _ptrd = ptrd + off;
- const t *const ptrp = &colormap[ind];
- cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
- }
- } break;
- case 1 : // Neumann
- cimg_forC(*this,c) {
- t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const longT ind = cimg::cut((longT)ptrs[off],(longT)0,(longT)cwhd - 1);
- t *const _ptrd = ptrd + off;
- const t *const ptrp = &colormap[ind];
- cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
- }
- } break;
- default : // Dirichlet
- cimg_forC(*this,c) {
- t *const ptrd = res.data(0,0,0,colormap._spectrum*c);
- const T *const ptrs = data(0,0,0,c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),256))
- for (longT off = 0; off<(longT)whd; ++off) {
- const ulongT ind = (ulongT)ptrs[off];
- t *const _ptrd = ptrd + off;
- if (ind<cwhd) {
- const t *const ptrp = &colormap[ind];
- cimg_forC(colormap,k) _ptrd[k*whd] = ptrp[k*cwhd];
- } else cimg_forC(colormap,k) _ptrd[k*whd] = (t)0;
- }
- }
- }
- }
- }
- return res;
- }
- //! Label connected components.
- /**
- \param is_high_connectivity Boolean that choose between 4(false)- or 8(true)-connectivity
- in 2D case, and between 6(false)- or 26(true)-connectivity in 3D case.
- \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region.
- \param is_L2_norm If true, tolerance is compared against L2 difference, otherwise L1 is used.
- \note The algorithm of connected components computation has been primarily done
- by A. Meijster, according to the publication:
- 'W.H. Hesselink, A. Meijster, C. Bron, "Concurrent Determination of Connected Components.",
- In: Science of Computer Programming 41 (2001), pp. 173--194'.
- The submitted code has then been modified to fit CImg coding style and constraints.
- **/
- CImg<T>& label(const bool is_high_connectivity=false, const Tfloat tolerance=0,
- const bool is_L2_norm=true) {
- if (is_empty()) return *this;
- return get_label(is_high_connectivity,tolerance,is_L2_norm).move_to(*this);
- }
- //! Label connected components \newinstance.
- CImg<ulongT> get_label(const bool is_high_connectivity=false, const Tfloat tolerance=0,
- const bool is_L2_norm=true) const {
- if (is_empty()) return CImg<ulongT>();
- // Create neighborhood tables.
- int dx[13], dy[13], dz[13], nb = 0;
- dx[nb] = 1; dy[nb] = 0; dz[nb++] = 0;
- dx[nb] = 0; dy[nb] = 1; dz[nb++] = 0;
- if (is_high_connectivity) {
- dx[nb] = 1; dy[nb] = 1; dz[nb++] = 0;
- dx[nb] = 1; dy[nb] = -1; dz[nb++] = 0;
- }
- if (_depth>1) { // 3D version
- dx[nb] = 0; dy[nb] = 0; dz[nb++]=1;
- if (is_high_connectivity) {
- dx[nb] = 1; dy[nb] = 1; dz[nb++] = -1;
- dx[nb] = 1; dy[nb] = 0; dz[nb++] = -1;
- dx[nb] = 1; dy[nb] = -1; dz[nb++] = -1;
- dx[nb] = 0; dy[nb] = 1; dz[nb++] = -1;
- dx[nb] = 0; dy[nb] = 1; dz[nb++] = 1;
- dx[nb] = 1; dy[nb] = -1; dz[nb++] = 1;
- dx[nb] = 1; dy[nb] = 0; dz[nb++] = 1;
- dx[nb] = 1; dy[nb] = 1; dz[nb++] = 1;
- }
- }
- return _label(nb,dx,dy,dz,tolerance,is_L2_norm);
- }
- //! Label connected components \overloading.
- /**
- \param connectivity_mask Mask of the neighboring pixels.
- \param tolerance Tolerance used to determine if two neighboring pixels belong to the same region.
- \param is_L2_norm If true, tolerance is compared against L2 difference, otherwise L1 is used.
- **/
- template<typename t>
- CImg<T>& label(const CImg<t>& connectivity_mask, const Tfloat tolerance=0,
- const bool is_L2_norm=true) {
- if (is_empty()) return *this;
- return get_label(connectivity_mask,tolerance,is_L2_norm).move_to(*this);
- }
- //! Label connected components \newinstance.
- template<typename t>
- CImg<ulongT> get_label(const CImg<t>& connectivity_mask, const Tfloat tolerance=0,
- const bool is_L2_norm=true) const {
- if (is_empty()) return CImg<ulongT>();
- int nb = 0;
- cimg_for(connectivity_mask,ptr,t) if (*ptr) ++nb;
- CImg<intT> dx(nb,1,1,1,0), dy(nb,1,1,1,0), dz(nb,1,1,1,0);
- nb = 0;
- cimg_forXYZ(connectivity_mask,x,y,z) if ((x || y || z) &&
- connectivity_mask(x,y,z)) {
- dx[nb] = x; dy[nb] = y; dz[nb++] = z;
- }
- return _label(nb,dx,dy,dz,tolerance,is_L2_norm);
- }
- CImg<ulongT> _label(const unsigned int nb, const int *const dx,
- const int *const dy, const int *const dz,
- const Tfloat tolerance, const bool is_L2_norm) const {
- CImg<ulongT> res(_width,_height,_depth);
- const Tfloat _tolerance = _spectrum>1 && is_L2_norm?cimg::sqr(tolerance):tolerance;
- // Init label numbers.
- ulongT *ptr = res.data();
- cimg_foroff(res,p) *(ptr++) = p;
- // For each neighbour-direction, label.
- for (unsigned int n = 0; n<nb; ++n) {
- const int _dx = dx[n], _dy = dy[n], _dz = dz[n];
- if (_dx || _dy || _dz) {
- const int
- x0 = _dx<0?-_dx:0,
- x1 = _dx<0?width():width() - _dx,
- y0 = _dy<0?-_dy:0,
- y1 = _dy<0?height():height() - _dy,
- z0 = _dz<0?-_dz:0,
- z1 = _dz<0?depth():depth() - _dz;
- const longT
- wh = (longT)width()*height(),
- whd = (longT)width()*height()*depth(),
- offset = _dz*wh + _dy*width() + _dx;
- for (longT z = z0, nz = z0 + _dz, pz = z0*wh; z<z1; ++z, ++nz, pz+=wh) {
- for (longT y = y0, ny = y0 + _dy, py = y0*width() + pz; y<y1; ++y, ++ny, py+=width()) {
- for (longT x = x0, nx = x0 + _dx, p = x0 + py; x<x1; ++x, ++nx, ++p) {
- Tfloat diff;
- switch (_spectrum) {
- case 1 :
- diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd));
- break;
- case 2 :
- if (is_L2_norm)
- diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd));
- else
- diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd));
- break;
- case 3 :
- if (is_L2_norm)
- diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd));
- else
- diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd));
- break;
- case 4 :
- if (is_L2_norm)
- diff = cimg::sqr((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd)) +
- cimg::sqr((Tfloat)(*this)(x,y,z,3,wh,whd) - (Tfloat)(*this)(nx,ny,nz,3,wh,whd));
- else
- diff = cimg::abs((Tfloat)(*this)(x,y,z,0,wh,whd) - (Tfloat)(*this)(nx,ny,nz,0,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,1,wh,whd) - (Tfloat)(*this)(nx,ny,nz,1,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,2,wh,whd) - (Tfloat)(*this)(nx,ny,nz,2,wh,whd)) +
- cimg::abs((Tfloat)(*this)(x,y,z,3,wh,whd) - (Tfloat)(*this)(nx,ny,nz,3,wh,whd));
- break;
- default :
- diff = 0;
- if (is_L2_norm)
- cimg_forC(*this,c)
- diff+=cimg::sqr((Tfloat)(*this)(x,y,z,c,wh,whd) - (Tfloat)(*this)(nx,ny,nz,c,wh,whd));
- else
- cimg_forC(*this,c)
- diff+=cimg::abs((Tfloat)(*this)(x,y,z,c,wh,whd) - (Tfloat)(*this)(nx,ny,nz,c,wh,whd));
- }
- if (diff<=_tolerance) {
- const longT q = p + offset;
- ulongT xk, yk;
- for (xk = (ulongT)(p<q?q:p), yk = (ulongT)(p<q?p:q); xk!=yk && res[xk]!=xk; ) {
- xk = res[xk]; if (xk<yk) cimg::swap(xk,yk);
- }
- if (xk!=yk) res[xk] = (ulongT)yk;
- for (ulongT _p = (ulongT)p; _p!=yk; ) {
- const ulongT h = res[_p];
- res[_p] = (ulongT)yk;
- _p = h;
- }
- for (ulongT _q = (ulongT)q; _q!=yk; ) {
- const ulongT h = res[_q];
- res[_q] = (ulongT)yk;
- _q = h;
- }
- }
- }
- }
- }
- }
- }
- // Resolve equivalences.
- ulongT counter = 0;
- ptr = res.data();
- cimg_foroff(res,p) { *ptr = *ptr==p?counter++:res[*ptr]; ++ptr; }
- return res;
- }
- // [internal] Replace possibly malicious characters for commands to be called by system() by their escaped version.
- CImg<T>& _system_strescape() {
- #define cimg_system_strescape(c,s) case c : if (p!=ptrs) CImg<T>(ptrs,(unsigned int)(p-ptrs),1,1,1,false).\
- move_to(list); \
- CImg<T>(s,(unsigned int)std::strlen(s),1,1,1,false).move_to(list); ptrs = p + 1; break
- CImgList<T> list;
- const T *ptrs = _data;
- cimg_for(*this,p,T) switch ((int)*p) {
- cimg_system_strescape('\\',"\\\\");
- cimg_system_strescape('\"',"\\\"");
- cimg_system_strescape('!',"\"\\!\"");
- cimg_system_strescape('`',"\\`");
- cimg_system_strescape('$',"\\$");
- }
- if (ptrs<end()) CImg<T>(ptrs,(unsigned int)(end()-ptrs),1,1,1,false).move_to(list);
- return (list>'x').move_to(*this);
- }
- //@}
- //---------------------------------
- //
- //! \name Color Base Management
- //@{
- //---------------------------------
- //! Return colormap \e "default", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_default.jpg
- **/
- static const CImg<Tuchar>& default_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- colormap.assign(1,256,1,3);
- for (unsigned int index = 0, r = 16; r<256; r+=32)
- for (unsigned int g = 16; g<256; g+=32)
- for (unsigned int b = 32; b<256; b+=64) {
- colormap(0,index,0) = (Tuchar)r;
- colormap(0,index,1) = (Tuchar)g;
- colormap(0,index++,2) = (Tuchar)b;
- }
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "HSV", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_hsv.jpg
- **/
- static const CImg<Tuchar>& HSV_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- CImg<Tint> tmp(1,256,1,3,1);
- tmp.get_shared_channel(0).sequence(0,359);
- colormap = tmp.HSVtoRGB();
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "lines", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_lines.jpg
- **/
- static const CImg<Tuchar>& lines_LUT256() {
- static const unsigned char pal[] = {
- 0,255,255,0,0,28,125,125,235,210,186,182,36,0,125,255,
- 53,32,255,210,89,186,65,45,125,210,210,97,130,194,0,125,
- 206,53,190,89,255,146,20,190,154,73,255,36,130,215,0,138,
- 101,210,61,194,206,0,77,45,255,154,174,0,190,239,89,125,
- 16,36,158,223,117,0,97,69,223,255,40,239,0,0,255,0,
- 97,170,93,255,138,40,117,210,0,170,53,158,186,255,0,121,
- 227,121,186,40,20,190,89,255,77,57,130,142,255,73,186,85,
- 210,8,32,166,243,130,210,40,255,45,61,142,223,49,121,255,
- 20,162,158,73,89,255,53,138,210,190,57,235,36,73,255,49,
- 210,0,210,85,57,97,255,121,85,174,40,255,162,178,0,121,
- 166,125,53,146,166,255,97,121,65,89,235,231,12,170,36,190,
- 85,255,166,97,198,77,20,146,109,166,255,28,40,202,121,81,
- 247,0,210,255,49,0,65,255,36,166,93,77,255,85,251,0,
- 170,178,0,182,255,0,162,16,154,142,162,223,223,0,0,81,
- 215,4,215,162,215,125,77,206,121,36,125,231,101,16,255,121,
- 0,57,190,215,65,125,89,142,255,101,73,53,146,223,125,125,
- 0,255,0,255,0,206,93,138,49,255,0,202,154,85,45,219,
- 251,53,0,255,40,130,219,158,16,117,186,130,202,49,65,239,
- 89,202,49,28,247,134,150,0,255,117,202,4,215,81,186,57,
- 202,89,73,210,40,93,45,251,206,28,223,142,40,134,162,125,
- 32,247,97,170,0,255,57,134,73,247,162,0,251,40,142,142,
- 8,166,206,81,154,194,93,89,125,243,28,109,227,0,190,65,
- 194,186,0,255,53,45,109,186,186,0,255,130,49,170,69,210,
- 154,0,109,227,45,255,125,105,81,81,255,0,219,134,170,85,
- 146,28,170,89,223,97,8,210,255,158,49,40,125,174,174,125,
- 0,227,166,28,219,130,0,93,239,0,85,255,81,178,125,49,
- 89,255,53,206,73,113,146,255,0,150,36,219,162,0,210,125,
- 69,134,255,85,40,89,235,49,215,121,0,206,36,223,174,69,
- 40,182,178,130,69,45,255,210,85,77,215,0,231,146,0,194,
- 125,174,0,255,40,89,121,206,57,0,206,170,231,150,81,0,
- 125,255,4,174,4,190,121,255,4,166,109,130,49,239,170,93,
- 16,174,210,0,255,16,105,158,93,255,0,125,0,255,158,85,
- 0,255,0,0,255,170,166,61,121,28,198,215,45,243,61,97,
- 255,53,81,130,109,255,8,117,235,121,40,178,174,0,182,49,
- 162,121,255,69,206,0,219,125,0,101,255,239,121,32,210,130,
- 36,231,32,125,81,142,215,158,4,178,255,0,40,251,125,125,
- 219,89,130,0,166,255,24,65,194,125,255,125,77,125,93,125,
- 202,24,138,174,178,32,255,85,194,40,85,36,174,174,125,210,
- 85,255,53,16,93,206,40,130,170,202,93,255,0,24,117,255,
- 97,113,105,81,255,186,194,57,69,206,57,53,223,190,4,255,
- 85,97,130,255,85,0,125,223,85,219,0,215,146,77,40,239,
- 89,36,142,154,227,0,255,85,162,0,162,0,235,178,45,166,
- 0,247,255,20,69,210,89,142,53,255,40,146,166,255,69,0,
- 174,154,142,130,162,0,215,255,0,89,40,255,166,61,146,69,
- 162,40,255,32,121,255,117,178,0,186,206,0,57,215,215,81,
- 158,77,166,210,77,89,210,0,24,202,150,186,0,255,20,97,
- 57,170,235,251,16,73,142,251,93,0,202,0,255,121,219,4,
- 73,219,8,162,206,16,219,93,117,0,255,8,130,174,223,45 };
- static const CImg<Tuchar> colormap(pal,1,256,1,3,false);
- return colormap;
- }
- //! Return colormap \e "hot", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_hot.jpg
- **/
- static const CImg<Tuchar>& hot_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- colormap.assign(1,4,1,3,(T)0);
- colormap[1] = colormap[2] = colormap[3] = colormap[6] = colormap[7] = colormap[11] = 255;
- colormap.resize(1,256,1,3,3);
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "cool", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_cool.jpg
- **/
- static const CImg<Tuchar>& cool_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- 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);
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "jet", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_jet.jpg
- **/
- static const CImg<Tuchar>& jet_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- colormap.assign(1,4,1,3,(T)0);
- colormap[2] = colormap[3] = colormap[5] = colormap[6] = colormap[8] = colormap[9] = 255;
- colormap.resize(1,256,1,3,3);
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "flag", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_flag.jpg
- **/
- static const CImg<Tuchar>& flag_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- colormap.assign(1,4,1,3,(T)0);
- colormap[0] = colormap[1] = colormap[5] = colormap[9] = colormap[10] = 255;
- colormap.resize(1,256,1,3,0,2);
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Return colormap \e "cube", containing 256 colors entries in RGB.
- /**
- \return The following \c 256x1x1x3 colormap is returned:
- \image html ref_colormap_cube.jpg
- **/
- static const CImg<Tuchar>& cube_LUT256() {
- static CImg<Tuchar> colormap;
- cimg::mutex(8);
- if (!colormap) {
- colormap.assign(1,8,1,3,(T)0);
- colormap[1] = colormap[3] = colormap[5] = colormap[7] =
- colormap[10] = colormap[11] = colormap[12] = colormap[13] =
- colormap[20] = colormap[21] = colormap[22] = colormap[23] = 255;
- colormap.resize(1,256,1,3,3);
- }
- cimg::mutex(8,0);
- return colormap;
- }
- //! Convert pixel values from sRGB to RGB color spaces.
- CImg<T>& sRGBtoRGB() {
- if (is_empty()) return *this;
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))
- cimg_rofoff(*this,off) {
- const Tfloat
- sval = (Tfloat)_data[off]/255,
- val = (Tfloat)(sval<=0.04045f?sval/12.92f:std::pow((sval + 0.055f)/(1.055f),2.4f));
- _data[off] = (T)cimg::cut(val*255,0,255);
- }
- return *this;
- }
- //! Convert pixel values from sRGB to RGB color spaces \newinstance.
- CImg<Tfloat> get_sRGBtoRGB() const {
- return CImg<Tfloat>(*this,false).sRGBtoRGB();
- }
- //! Convert pixel values from RGB to sRGB color spaces.
- CImg<T>& RGBtosRGB() {
- if (is_empty()) return *this;
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(size(),32))
- cimg_rofoff(*this,off) {
- const Tfloat
- val = (Tfloat)_data[off]/255,
- sval = (Tfloat)(val<=0.0031308f?val*12.92f:1.055f*std::pow(val,0.416667f) - 0.055f);
- _data[off] = (T)cimg::cut(sval*255,0,255);
- }
- return *this;
- }
- //! Convert pixel values from RGB to sRGB color spaces \newinstance.
- CImg<Tfloat> get_RGBtosRGB() const {
- return CImg<Tfloat>(*this,false).RGBtosRGB();
- }
- //! Convert pixel values from RGB to HSI color spaces.
- CImg<T>& RGBtoHSI() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoHSI(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N],
- G = (Tfloat)p2[N],
- B = (Tfloat)p3[N],
- m = cimg::min(R,G,B),
- M = cimg::max(R,G,B),
- C = M - m,
- sum = R + G + B,
- H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
- S = sum<=0?0:1 - 3*m/sum,
- I = sum/(3*255);
- p1[N] = (T)H;
- p2[N] = (T)S;
- p3[N] = (T)I;
- }
- return *this;
- }
- //! Convert pixel values from RGB to HSI color spaces \newinstance.
- CImg<Tfloat> get_RGBtoHSI() const {
- return CImg<Tfloat>(*this,false).RGBtoHSI();
- }
- //! Convert pixel values from HSI to RGB color spaces.
- CImg<T>& HSItoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "HSItoRGB(): Instance is not a HSI image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
- S = (Tfloat)p2[N],
- I = (Tfloat)p3[N],
- Z = 1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1),
- C = I*S/(1 + Z),
- X = C*Z,
- m = I*(1 - S)/3;
- Tfloat R, G, B;
- switch ((int)H) {
- case 0 : R = C; G = X; B = 0; break;
- case 1 : R = X; G = C; B = 0; break;
- case 2 : R = 0; G = C; B = X; break;
- case 3 : R = 0; G = X; B = C; break;
- case 4 : R = X; G = 0; B = C; break;
- default : R = C; G = 0; B = X;
- }
- p1[N] = (T)((R + m)*3*255);
- p2[N] = (T)((G + m)*3*255);
- p3[N] = (T)((B + m)*3*255);
- }
- return *this;
- }
- //! Convert pixel values from HSI to RGB color spaces \newinstance.
- CImg<Tfloat> get_HSItoRGB() const {
- return CImg< Tuchar>(*this,false).HSItoRGB();
- }
- //! Convert pixel values from RGB to HSL color spaces.
- CImg<T>& RGBtoHSL() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoHSL(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N],
- G = (Tfloat)p2[N],
- B = (Tfloat)p3[N],
- m = cimg::min(R,G,B),
- M = cimg::max(R,G,B),
- C = M - m,
- H = 60*(C==0?0:M==R?cimg::mod((G - B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
- L = 0.5f*(m + M)/255,
- S = L==1 || L==0?0:C/(1 - cimg::abs(2*L - 1))/255;
- p1[N] = (T)H;
- p2[N] = (T)S;
- p3[N] = (T)L;
- }
- return *this;
- }
- //! Convert pixel values from RGB to HSL color spaces \newinstance.
- CImg<Tfloat> get_RGBtoHSL() const {
- return CImg<Tfloat>(*this,false).RGBtoHSL();
- }
- //! Convert pixel values from HSL to RGB color spaces.
- CImg<T>& HSLtoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "HSLtoRGB(): Instance is not a HSL image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
- S = (Tfloat)p2[N],
- L = (Tfloat)p3[N],
- C = (1 - cimg::abs(2*L - 1))*S,
- X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),
- m = L - C/2;
- Tfloat R, G, B;
- switch ((int)H) {
- case 0 : R = C; G = X; B = 0; break;
- case 1 : R = X; G = C; B = 0; break;
- case 2 : R = 0; G = C; B = X; break;
- case 3 : R = 0; G = X; B = C; break;
- case 4 : R = X; G = 0; B = C; break;
- default : R = C; G = 0; B = X;
- }
- p1[N] = (T)((R + m)*255);
- p2[N] = (T)((G + m)*255);
- p3[N] = (T)((B + m)*255);
- }
- return *this;
- }
- //! Convert pixel values from HSL to RGB color spaces \newinstance.
- CImg<Tuchar> get_HSLtoRGB() const {
- return CImg<Tuchar>(*this,false).HSLtoRGB();
- }
- //! Convert pixel values from RGB to HSV color spaces.
- CImg<T>& RGBtoHSV() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoHSV(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N],
- G = (Tfloat)p2[N],
- B = (Tfloat)p3[N],
- M = cimg::max(R,G,B),
- C = M - cimg::min(R,G,B),
- H = 60*(C==0?0:M==R?cimg::mod((G-B)/C,(Tfloat)6):M==G?(B - R)/C + 2:(R - G)/C + 4),
- S = M<=0?0:C/M;
- p1[N] = (T)H;
- p2[N] = (T)S;
- p3[N] = (T)(M/255);
- }
- return *this;
- }
- //! Convert pixel values from RGB to HSV color spaces \newinstance.
- CImg<Tfloat> get_RGBtoHSV() const {
- return CImg<Tfloat>(*this,false).RGBtoHSV();
- }
- //! Convert pixel values from HSV to RGB color spaces.
- CImg<T>& HSVtoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "HSVtoRGB(): Instance is not a HSV image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,256))
- for (longT N = 0; N<whd; ++N) {
- Tfloat
- H = cimg::mod((Tfloat)p1[N]/60,(Tfloat)6),
- S = (Tfloat)p2[N],
- V = (Tfloat)p3[N],
- C = V*S,
- X = C*(1 - cimg::abs(cimg::mod(H,(Tfloat)2) - 1)),
- m = V - C;
- Tfloat R, G, B;
- switch ((int)H) {
- case 0 : R = C; G = X; B = 0; break;
- case 1 : R = X; G = C; B = 0; break;
- case 2 : R = 0; G = C; B = X; break;
- case 3 : R = 0; G = X; B = C; break;
- case 4 : R = X; G = 0; B = C; break;
- default : R = C; G = 0; B = X;
- }
- p1[N] = (T)((R + m)*255);
- p2[N] = (T)((G + m)*255);
- p3[N] = (T)((B + m)*255);
- }
- return *this;
- }
- //! Convert pixel values from HSV to RGB color spaces \newinstance.
- CImg<Tuchar> get_HSVtoRGB() const {
- return CImg<Tuchar>(*this,false).HSVtoRGB();
- }
- //! Convert pixel values from RGB to YCbCr color spaces.
- CImg<T>& RGBtoYCbCr() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoYCbCr(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,512))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N],
- G = (Tfloat)p2[N],
- B = (Tfloat)p3[N],
- Y = (66*R + 129*G + 25*B + 128)/256 + 16,
- Cb = (-38*R - 74*G + 112*B + 128)/256 + 128,
- Cr = (112*R - 94*G - 18*B + 128)/256 + 128;
- p1[N] = (T)cimg::cut(Y,0,255),
- p2[N] = (T)cimg::cut(Cb,0,255),
- p3[N] = (T)cimg::cut(Cr,0,255);
- }
- return *this;
- }
- //! Convert pixel values from RGB to YCbCr color spaces \newinstance.
- CImg<Tuchar> get_RGBtoYCbCr() const {
- return CImg<Tuchar>(*this,false).RGBtoYCbCr();
- }
- //! Convert pixel values from RGB to YCbCr color spaces.
- CImg<T>& YCbCrtoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "YCbCrtoRGB(): Instance is not a YCbCr image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,512))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- Y = (Tfloat)p1[N] - 16,
- Cb = (Tfloat)p2[N] - 128,
- Cr = (Tfloat)p3[N] - 128,
- R = (298*Y + 409*Cr + 128)/256,
- G = (298*Y - 100*Cb - 208*Cr + 128)/256,
- B = (298*Y + 516*Cb + 128)/256;
- p1[N] = (T)cimg::cut(R,0,255),
- p2[N] = (T)cimg::cut(G,0,255),
- p3[N] = (T)cimg::cut(B,0,255);
- }
- return *this;
- }
- //! Convert pixel values from RGB to YCbCr color spaces \newinstance.
- CImg<Tuchar> get_YCbCrtoRGB() const {
- return CImg<Tuchar>(*this,false).YCbCrtoRGB();
- }
- //! Convert pixel values from RGB to YUV color spaces.
- CImg<T>& RGBtoYUV() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoYUV(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,16384))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N]/255,
- G = (Tfloat)p2[N]/255,
- B = (Tfloat)p3[N]/255,
- Y = 0.299f*R + 0.587f*G + 0.114f*B;
- p1[N] = (T)Y;
- p2[N] = (T)(0.492f*(B - Y));
- p3[N] = (T)(0.877*(R - Y));
- }
- return *this;
- }
- //! Convert pixel values from RGB to YUV color spaces \newinstance.
- CImg<Tfloat> get_RGBtoYUV() const {
- return CImg<Tfloat>(*this,false).RGBtoYUV();
- }
- //! Convert pixel values from YUV to RGB color spaces.
- CImg<T>& YUVtoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "YUVtoRGB(): Instance is not a YUV image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,16384))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- Y = (Tfloat)p1[N],
- U = (Tfloat)p2[N],
- V = (Tfloat)p3[N],
- R = (Y + 1.140f*V)*255,
- G = (Y - 0.395f*U - 0.581f*V)*255,
- B = (Y + 2.032f*U)*255;
- p1[N] = (T)cimg::cut(R,0,255),
- p2[N] = (T)cimg::cut(G,0,255),
- p3[N] = (T)cimg::cut(B,0,255);
- }
- return *this;
- }
- //! Convert pixel values from YUV to RGB color spaces \newinstance.
- CImg<Tuchar> get_YUVtoRGB() const {
- return CImg< Tuchar>(*this,false).YUVtoRGB();
- }
- //! Convert pixel values from RGB to CMY color spaces.
- CImg<T>& RGBtoCMY() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoCMY(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N],
- G = (Tfloat)p2[N],
- B = (Tfloat)p3[N],
- C = 255 - R,
- M = 255 - G,
- Y = 255 - B;
- p1[N] = (T)cimg::cut(C,0,255),
- p2[N] = (T)cimg::cut(M,0,255),
- p3[N] = (T)cimg::cut(Y,0,255);
- }
- return *this;
- }
- //! Convert pixel values from RGB to CMY color spaces \newinstance.
- CImg<Tuchar> get_RGBtoCMY() const {
- return CImg<Tfloat>(*this,false).RGBtoCMY();
- }
- //! Convert pixel values from CMY to RGB color spaces.
- CImg<T>& CMYtoRGB() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "CMYtoRGB(): Instance is not a CMY image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- C = (Tfloat)p1[N],
- M = (Tfloat)p2[N],
- Y = (Tfloat)p3[N],
- R = 255 - C,
- G = 255 - M,
- B = 255 - Y;
- p1[N] = (T)cimg::cut(R,0,255),
- p2[N] = (T)cimg::cut(G,0,255),
- p3[N] = (T)cimg::cut(B,0,255);
- }
- return *this;
- }
- //! Convert pixel values from CMY to RGB color spaces \newinstance.
- CImg<Tuchar> get_CMYtoRGB() const {
- return CImg<Tuchar>(*this,false).CMYtoRGB();
- }
- //! Convert pixel values from CMY to CMYK color spaces.
- CImg<T>& CMYtoCMYK() {
- return get_CMYtoCMYK().move_to(*this);
- }
- //! Convert pixel values from CMY to CMYK color spaces \newinstance.
- CImg<Tuchar> get_CMYtoCMYK() const {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "CMYtoCMYK(): Instance is not a CMY image.",
- cimg_instance);
- CImg<Tfloat> res(_width,_height,_depth,4);
- const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2);
- 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);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,1024))
- for (longT N = 0; N<whd; ++N) {
- Tfloat
- C = (Tfloat)ps1[N],
- M = (Tfloat)ps2[N],
- Y = (Tfloat)ps3[N],
- K = cimg::min(C,M,Y);
- if (K>=255) C = M = Y = 0;
- else { const Tfloat K1 = 255 - K; C = 255*(C - K)/K1; M = 255*(M - K)/K1; Y = 255*(Y - K)/K1; }
- pd1[N] = (Tfloat)cimg::cut(C,0,255),
- pd2[N] = (Tfloat)cimg::cut(M,0,255),
- pd3[N] = (Tfloat)cimg::cut(Y,0,255),
- pd4[N] = (Tfloat)cimg::cut(K,0,255);
- }
- return res;
- }
- //! Convert pixel values from CMYK to CMY color spaces.
- CImg<T>& CMYKtoCMY() {
- return get_CMYKtoCMY().move_to(*this);
- }
- //! Convert pixel values from CMYK to CMY color spaces \newinstance.
- CImg<Tfloat> get_CMYKtoCMY() const {
- if (_spectrum!=4)
- throw CImgInstanceException(_cimg_instance
- "CMYKtoCMY(): Instance is not a CMYK image.",
- cimg_instance);
- CImg<Tfloat> res(_width,_height,_depth,3);
- 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);
- Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,1024))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- C = (Tfloat)ps1[N],
- M = (Tfloat)ps2[N],
- Y = (Tfloat)ps3[N],
- K = (Tfloat)ps4[N],
- K1 = 1 - K/255,
- nC = C*K1 + K,
- nM = M*K1 + K,
- nY = Y*K1 + K;
- pd1[N] = (Tfloat)cimg::cut(nC,0,255),
- pd2[N] = (Tfloat)cimg::cut(nM,0,255),
- pd3[N] = (Tfloat)cimg::cut(nY,0,255);
- }
- return res;
- }
- //! Convert pixel values from RGB to XYZ color spaces.
- /**
- \param use_D65 Tell to use the D65 illuminant (D50 otherwise).
- **/
- CImg<T>& RGBtoXYZ(const bool use_D65=true) {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "RGBtoXYZ(): Instance is not a RGB image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- R = (Tfloat)p1[N]/255,
- G = (Tfloat)p2[N]/255,
- B = (Tfloat)p3[N]/255;
- if (use_D65) { // D65
- p1[N] = (T)(0.4124564*R + 0.3575761*G + 0.1804375*B);
- p2[N] = (T)(0.2126729*R + 0.7151522*G + 0.0721750*B);
- p3[N] = (T)(0.0193339*R + 0.1191920*G + 0.9503041*B);
- } else { // D50
- p1[N] = (T)(0.43603516*R + 0.38511658*G + 0.14305115*B);
- p2[N] = (T)(0.22248840*R + 0.71690369*G + 0.06060791*B);
- p3[N] = (T)(0.01391602*R + 0.09706116*G + 0.71392822*B);
- }
- }
- return *this;
- }
- //! Convert pixel values from RGB to XYZ color spaces \newinstance.
- CImg<Tfloat> get_RGBtoXYZ(const bool use_D65=true) const {
- return CImg<Tfloat>(*this,false).RGBtoXYZ(use_D65);
- }
- //! Convert pixel values from XYZ to RGB color spaces.
- /**
- \param use_D65 Tell to use the D65 illuminant (D50 otherwise).
- **/
- CImg<T>& XYZtoRGB(const bool use_D65=true) {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "XYZtoRGB(): Instance is not a XYZ image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,2048))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- X = (Tfloat)p1[N]*255,
- Y = (Tfloat)p2[N]*255,
- Z = (Tfloat)p3[N]*255;
- if (use_D65) {
- p1[N] = (T)cimg::cut(3.2404542*X - 1.5371385*Y - 0.4985314*Z,0,255);
- p2[N] = (T)cimg::cut(-0.9692660*X + 1.8760108*Y + 0.0415560*Z,0,255);
- p3[N] = (T)cimg::cut(0.0556434*X - 0.2040259*Y + 1.0572252*Z,0,255);
- } else {
- p1[N] = (T)cimg::cut(3.134274799724*X - 1.617275708956*Y - 0.490724283042*Z,0,255);
- p2[N] = (T)cimg::cut(-0.978795575994*X + 1.916161689117*Y + 0.033453331711*Z,0,255);
- p3[N] = (T)cimg::cut(0.071976988401*X - 0.228984974402*Y + 1.405718224383*Z,0,255);
- }
- }
- return *this;
- }
- //! Convert pixel values from XYZ to RGB color spaces \newinstance.
- CImg<Tuchar> get_XYZtoRGB(const bool use_D65=true) const {
- return CImg<Tuchar>(*this,false).XYZtoRGB(use_D65);
- }
- //! Convert pixel values from XYZ to Lab color spaces.
- CImg<T>& XYZtoLab(const bool use_D65=true) {
- #define _cimg_Labf(x) (24389*(x)>216?cimg::cbrt(x):(24389*(x)/27 + 16)/116)
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "XYZtoLab(): Instance is not a XYZ image.",
- cimg_instance);
- const CImg<Tfloat> white = CImg<Tfloat>(1,1,1,3,255).RGBtoXYZ(use_D65);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,128))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- X = (Tfloat)(p1[N]/white[0]),
- Y = (Tfloat)(p2[N]/white[1]),
- Z = (Tfloat)(p3[N]/white[2]),
- fX = (Tfloat)_cimg_Labf(X),
- fY = (Tfloat)_cimg_Labf(Y),
- fZ = (Tfloat)_cimg_Labf(Z);
- p1[N] = (T)cimg::cut(116*fY - 16,0,100);
- p2[N] = (T)(500*(fX - fY));
- p3[N] = (T)(200*(fY - fZ));
- }
- return *this;
- }
- //! Convert pixel values from XYZ to Lab color spaces \newinstance.
- CImg<Tfloat> get_XYZtoLab(const bool use_D65=true) const {
- return CImg<Tfloat>(*this,false).XYZtoLab(use_D65);
- }
- //! Convert pixel values from Lab to XYZ color spaces.
- CImg<T>& LabtoXYZ(const bool use_D65=true) {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "LabtoXYZ(): Instance is not a Lab image.",
- cimg_instance);
- const CImg<Tfloat> white = CImg<Tfloat>(1,1,1,3,255).RGBtoXYZ(use_D65);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,128))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- L = (Tfloat)p1[N],
- a = (Tfloat)p2[N],
- b = (Tfloat)p3[N],
- cY = (L + 16)/116,
- cZ = cY - b/200,
- cX = a/500 + cY,
- X = (Tfloat)(24389*cX>216?cX*cX*cX:(116*cX - 16)*27/24389),
- Y = (Tfloat)(27*L>216?cY*cY*cY:27*L/24389),
- Z = (Tfloat)(24389*cZ>216?cZ*cZ*cZ:(116*cZ - 16)*27/24389);
- p1[N] = (T)(X*white[0]);
- p2[N] = (T)(Y*white[1]);
- p3[N] = (T)(Z*white[2]);
- }
- return *this;
- }
- //! Convert pixel values from Lab to XYZ color spaces \newinstance.
- CImg<Tfloat> get_LabtoXYZ(const bool use_D65=true) const {
- return CImg<Tfloat>(*this,false).LabtoXYZ(use_D65);
- }
- //! Convert pixel values from XYZ to xyY color spaces.
- CImg<T>& XYZtoxyY() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "XYZtoxyY(): Instance is not a XYZ image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,4096))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- X = (Tfloat)p1[N],
- Y = (Tfloat)p2[N],
- Z = (Tfloat)p3[N],
- sum = X + Y + Z,
- nsum = sum>0?sum:1;
- p1[N] = (T)(X/nsum);
- p2[N] = (T)(Y/nsum);
- p3[N] = (T)Y;
- }
- return *this;
- }
- //! Convert pixel values from XYZ to xyY color spaces \newinstance.
- CImg<Tfloat> get_XYZtoxyY() const {
- return CImg<Tfloat>(*this,false).XYZtoxyY();
- }
- //! Convert pixel values from xyY pixels to XYZ color spaces.
- CImg<T>& xyYtoXYZ() {
- if (_spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "xyYtoXYZ(): Instance is not a xyY image.",
- cimg_instance);
- T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);
- const longT whd = (longT)width()*height()*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,4096))
- for (longT N = 0; N<whd; ++N) {
- const Tfloat
- px = (Tfloat)p1[N],
- py = (Tfloat)p2[N],
- Y = (Tfloat)p3[N],
- ny = py>0?py:1;
- p1[N] = (T)(px*Y/ny);
- p2[N] = (T)Y;
- p3[N] = (T)((1 - px - py)*Y/ny);
- }
- return *this;
- }
- //! Convert pixel values from xyY pixels to XYZ color spaces \newinstance.
- CImg<Tfloat> get_xyYtoXYZ() const {
- return CImg<Tfloat>(*this,false).xyYtoXYZ();
- }
- //! Convert pixel values from RGB to Lab color spaces.
- CImg<T>& RGBtoLab(const bool use_D65=true) {
- return RGBtoXYZ(use_D65).XYZtoLab(use_D65);
- }
- //! Convert pixel values from RGB to Lab color spaces \newinstance.
- CImg<Tfloat> get_RGBtoLab(const bool use_D65=true) const {
- return CImg<Tfloat>(*this,false).RGBtoLab(use_D65);
- }
- //! Convert pixel values from Lab to RGB color spaces.
- CImg<T>& LabtoRGB(const bool use_D65=true) {
- return LabtoXYZ().XYZtoRGB(use_D65);
- }
- //! Convert pixel values from Lab to RGB color spaces \newinstance.
- CImg<Tuchar> get_LabtoRGB(const bool use_D65=true) const {
- return CImg<Tuchar>(*this,false).LabtoRGB(use_D65);
- }
- //! Convert pixel values from RGB to xyY color spaces.
- CImg<T>& RGBtoxyY(const bool use_D65=true) {
- return RGBtoXYZ(use_D65).XYZtoxyY();
- }
- //! Convert pixel values from RGB to xyY color spaces \newinstance.
- CImg<Tfloat> get_RGBtoxyY(const bool use_D65=true) const {
- return CImg<Tfloat>(*this,false).RGBtoxyY(use_D65);
- }
- //! Convert pixel values from xyY to RGB color spaces.
- CImg<T>& xyYtoRGB(const bool use_D65=true) {
- return xyYtoXYZ().XYZtoRGB(use_D65);
- }
- //! Convert pixel values from xyY to RGB color spaces \newinstance.
- CImg<Tuchar> get_xyYtoRGB(const bool use_D65=true) const {
- return CImg<Tuchar>(*this,false).xyYtoRGB(use_D65);
- }
- //! Convert pixel values from RGB to CMYK color spaces.
- CImg<T>& RGBtoCMYK() {
- return RGBtoCMY().CMYtoCMYK();
- }
- //! Convert pixel values from RGB to CMYK color spaces \newinstance.
- CImg<Tfloat> get_RGBtoCMYK() const {
- return CImg<Tfloat>(*this,false).RGBtoCMYK();
- }
- //! Convert pixel values from CMYK to RGB color spaces.
- CImg<T>& CMYKtoRGB() {
- return CMYKtoCMY().CMYtoRGB();
- }
- //! Convert pixel values from CMYK to RGB color spaces \newinstance.
- CImg<Tuchar> get_CMYKtoRGB() const {
- return CImg<Tuchar>(*this,false).CMYKtoRGB();
- }
- //@}
- //------------------------------------------
- //
- //! \name Geometric / Spatial Manipulation
- //@{
- //------------------------------------------
- static float _cimg_lanczos(const float x) {
- if (x<=-2 || x>=2) return 0;
- const float a = (float)cimg::PI*x, b = 0.5f*a;
- return (float)(x?std::sin(a)*std::sin(b)/(a*b):1);
- }
- //! Resize image to new dimensions.
- /**
- \param size_x Number of columns (new size along the X-axis).
- \param size_y Number of rows (new size along the Y-axis).
- \param size_z Number of slices (new size along the Z-axis).
- \param size_c Number of vector-channels (new size along the C-axis).
- \param interpolation_type Method of interpolation:
- - -1 = no interpolation: raw memory resizing.
- - 0 = no interpolation: additional space is filled according to \p boundary_conditions.
- - 1 = nearest-neighbor interpolation.
- - 2 = moving average interpolation.
- - 3 = linear interpolation.
- - 4 = grid interpolation.
- - 5 = cubic interpolation.
- - 6 = lanczos interpolation.
- \param boundary_conditions Type of boundary conditions used if necessary.
- \param centering_x Set centering type (only if \p interpolation_type=0).
- \param centering_y Set centering type (only if \p interpolation_type=0).
- \param centering_z Set centering type (only if \p interpolation_type=0).
- \param centering_c Set centering type (only if \p interpolation_type=0).
- \note If pd[x,y,z,v]<0, it corresponds to a percentage of the original size (the default value is -100).
- **/
- CImg<T>& resize(const int size_x, const int size_y=-100,
- const int size_z=-100, const int size_c=-100,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) {
- if (!size_x || !size_y || !size_z || !size_c) return assign();
- const unsigned int
- _sx = (unsigned int)(size_x<0?-size_x*width()/100:size_x),
- _sy = (unsigned int)(size_y<0?-size_y*height()/100:size_y),
- _sz = (unsigned int)(size_z<0?-size_z*depth()/100:size_z),
- _sc = (unsigned int)(size_c<0?-size_c*spectrum()/100:size_c),
- sx = _sx?_sx:1, sy = _sy?_sy:1, sz = _sz?_sz:1, sc = _sc?_sc:1;
- if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return *this;
- if (is_empty()) return assign(sx,sy,sz,sc,(T)0);
- if (interpolation_type==-1 && sx*sy*sz*sc==size()) {
- _width = sx; _height = sy; _depth = sz; _spectrum = sc;
- return *this;
- }
- return get_resize(sx,sy,sz,sc,interpolation_type,boundary_conditions,
- centering_x,centering_y,centering_z,centering_c).move_to(*this);
- }
- //! Resize image to new dimensions \newinstance.
- CImg<T> get_resize(const int size_x, const int size_y = -100,
- const int size_z = -100, const int size_c = -100,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) const {
- if (centering_x<0 || centering_x>1 || centering_y<0 || centering_y>1 ||
- centering_z<0 || centering_z>1 || centering_c<0 || centering_c>1)
- throw CImgArgumentException(_cimg_instance
- "resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].",
- cimg_instance,
- centering_x,centering_y,centering_z,centering_c);
- if (!size_x || !size_y || !size_z || !size_c) return CImg<T>();
- const unsigned int
- sx = std::max(1U,(unsigned int)(size_x>=0?size_x:-size_x*width()/100)),
- sy = std::max(1U,(unsigned int)(size_y>=0?size_y:-size_y*height()/100)),
- sz = std::max(1U,(unsigned int)(size_z>=0?size_z:-size_z*depth()/100)),
- sc = std::max(1U,(unsigned int)(size_c>=0?size_c:-size_c*spectrum()/100));
- if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return +*this;
- if (is_empty()) return CImg<T>(sx,sy,sz,sc,(T)0);
- CImg<T> res;
- switch (interpolation_type) {
- // Raw resizing.
- //
- case -1 :
- std::memcpy(res.assign(sx,sy,sz,sc,(T)0)._data,_data,sizeof(T)*std::min(size(),(ulongT)sx*sy*sz*sc));
- break;
- // No interpolation.
- //
- case 0 : {
- const int
- xc = (int)(centering_x*((int)sx - width())),
- yc = (int)(centering_y*((int)sy - height())),
- zc = (int)(centering_z*((int)sz - depth())),
- cc = (int)(centering_c*((int)sc - spectrum()));
- switch (boundary_conditions) {
- case 3 : { // Mirror
- res.assign(sx,sy,sz,sc);
- const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth(), s2 = 2*spectrum();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1024*1024))
- cimg_forXYZC(res,x,y,z,c) {
- const int
- mx = cimg::mod(x - xc,w2), my = cimg::mod(y - yc,h2),
- mz = cimg::mod(z - zc,d2), mc = cimg::mod(c - cc,s2);
- res(x,y,z,c) = (*this)(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,
- mc<spectrum()?mc:s2 - mc - 1);
- }
- } break;
- case 2 : { // Periodic
- res.assign(sx,sy,sz,sc);
- const int
- x0 = ((int)xc%width()) - width(),
- y0 = ((int)yc%height()) - height(),
- z0 = ((int)zc%depth()) - depth(),
- c0 = ((int)cc%spectrum()) - spectrum(),
- dx = width(), dy = height(), dz = depth(), dc = spectrum();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1024*1024))
- for (int c = c0; c<(int)sc; c+=dc)
- for (int z = z0; z<(int)sz; z+=dz)
- for (int y = y0; y<(int)sy; y+=dy)
- for (int x = x0; x<(int)sx; x+=dx)
- res.draw_image(x,y,z,c,*this);
- } break;
- case 1 : { // Neumann
- res.assign(sx,sy,sz,sc).draw_image(xc,yc,zc,cc,*this);
- CImg<T> sprite;
- if (xc>0) { // X-backward
- res.get_crop(xc,yc,zc,cc,xc,yc + height() - 1,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
- for (int x = xc - 1; x>=0; --x) res.draw_image(x,yc,zc,cc,sprite);
- }
- if (xc + width()<(int)sx) { // X-forward
- res.get_crop(xc + width() - 1,yc,zc,cc,xc + width() - 1,yc + height() - 1,
- zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
- for (int x = xc + width(); x<(int)sx; ++x) res.draw_image(x,yc,zc,cc,sprite);
- }
- if (yc>0) { // Y-backward
- res.get_crop(0,yc,zc,cc,sx - 1,yc,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
- for (int y = yc - 1; y>=0; --y) res.draw_image(0,y,zc,cc,sprite);
- }
- if (yc + height()<(int)sy) { // Y-forward
- res.get_crop(0,yc + height() - 1,zc,cc,sx - 1,yc + height() - 1,
- zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
- for (int y = yc + height(); y<(int)sy; ++y) res.draw_image(0,y,zc,cc,sprite);
- }
- if (zc>0) { // Z-backward
- res.get_crop(0,0,zc,cc,sx - 1,sy - 1,zc,cc + spectrum() - 1).move_to(sprite);
- for (int z = zc - 1; z>=0; --z) res.draw_image(0,0,z,cc,sprite);
- }
- if (zc + depth()<(int)sz) { // Z-forward
- res.get_crop(0,0,zc +depth() - 1,cc,sx - 1,sy - 1,zc + depth() - 1,cc + spectrum() - 1).move_to(sprite);
- for (int z = zc + depth(); z<(int)sz; ++z) res.draw_image(0,0,z,cc,sprite);
- }
- if (cc>0) { // C-backward
- res.get_crop(0,0,0,cc,sx - 1,sy - 1,sz - 1,cc).move_to(sprite);
- for (int c = cc - 1; c>=0; --c) res.draw_image(0,0,0,c,sprite);
- }
- if (cc + spectrum()<(int)sc) { // C-forward
- res.get_crop(0,0,0,cc + spectrum() - 1,sx - 1,sy - 1,sz - 1,cc + spectrum() - 1).move_to(sprite);
- for (int c = cc + spectrum(); c<(int)sc; ++c) res.draw_image(0,0,0,c,sprite);
- }
- } break;
- default : // Dirichlet
- res.assign(sx,sy,sz,sc,(T)0).draw_image(xc,yc,zc,cc,*this);
- }
- break;
- } break;
- // Nearest neighbor interpolation.
- //
- case 1 : {
- res.assign(sx,sy,sz,sc);
- CImg<ulongT> off_x(sx), off_y(sy + 1), off_z(sz + 1), off_c(sc + 1);
- const ulongT
- wh = (ulongT)_width*_height,
- whd = (ulongT)_width*_height*_depth,
- sxy = (ulongT)sx*sy,
- sxyz = (ulongT)sx*sy*sz,
- one = (ulongT)1;
- if (sx==_width) off_x.fill(1);
- else {
- ulongT *poff_x = off_x._data, curr = 0;
- cimg_forX(res,x) {
- const ulongT old = curr;
- curr = (x + one)*_width/sx;
- *(poff_x++) = curr - old;
- }
- }
- if (sy==_height) off_y.fill(_width);
- else {
- ulongT *poff_y = off_y._data, curr = 0;
- cimg_forY(res,y) {
- const ulongT old = curr;
- curr = (y + one)*_height/sy;
- *(poff_y++) = _width*(curr - old);
- }
- *poff_y = 0;
- }
- if (sz==_depth) off_z.fill(wh);
- else {
- ulongT *poff_z = off_z._data, curr = 0;
- cimg_forZ(res,z) {
- const ulongT old = curr;
- curr = (z + one)*_depth/sz;
- *(poff_z++) = wh*(curr - old);
- }
- *poff_z = 0;
- }
- if (sc==_spectrum) off_c.fill(whd);
- else {
- ulongT *poff_c = off_c._data, curr = 0;
- cimg_forC(res,c) {
- const ulongT old = curr;
- curr = (c + one)*_spectrum/sc;
- *(poff_c++) = whd*(curr - old);
- }
- *poff_c = 0;
- }
- T *ptrd = res._data;
- const T* ptrc = _data;
- const ulongT *poff_c = off_c._data;
- for (unsigned int c = 0; c<sc; ) {
- const T *ptrz = ptrc;
- const ulongT *poff_z = off_z._data;
- for (unsigned int z = 0; z<sz; ) {
- const T *ptry = ptrz;
- const ulongT *poff_y = off_y._data;
- for (unsigned int y = 0; y<sy; ) {
- const T *ptrx = ptry;
- const ulongT *poff_x = off_x._data;
- cimg_forX(res,x) { *(ptrd++) = *ptrx; ptrx+=*(poff_x++); }
- ++y;
- ulongT dy = *(poff_y++);
- for ( ; !dy && y<dy; std::memcpy(ptrd,ptrd - sx,sizeof(T)*sx), ++y, ptrd+=sx, dy = *(poff_y++)) {}
- ptry+=dy;
- }
- ++z;
- ulongT dz = *(poff_z++);
- for ( ; !dz && z<dz; std::memcpy(ptrd,ptrd - sxy,sizeof(T)*sxy), ++z, ptrd+=sxy, dz = *(poff_z++)) {}
- ptrz+=dz;
- }
- ++c;
- ulongT dc = *(poff_c++);
- for ( ; !dc && c<dc; std::memcpy(ptrd,ptrd - sxyz,sizeof(T)*sxyz), ++c, ptrd+=sxyz, dc = *(poff_c++)) {}
- ptrc+=dc;
- }
- } break;
- // Moving average.
- //
- case 2 : {
- bool instance_first = true;
- if (sx!=_width) {
- if (sx>_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(res);
- else {
- CImg<Tfloat> tmp(sx,_height,_depth,_spectrum,0);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(sx>=256 && _height*_depth*_spectrum>=256))
- cimg_forYZC(tmp,y,z,v) {
- for (unsigned int a = _width*sx, b = _width, c = sx, s = 0, t = 0; a; ) {
- const unsigned int d = std::min(b,c);
- a-=d; b-=d; c-=d;
- tmp(t,y,z,v)+=(Tfloat)(*this)(s,y,z,v)*d;
- if (!b) { tmp(t++,y,z,v)/=_width; b = _width; }
- if (!c) { ++s; c = sx; }
- }
- }
- tmp.move_to(res);
- }
- instance_first = false;
- }
- if (sy!=_height) {
- if (sy>_height) get_resize(sx,sy,_depth,_spectrum,1).move_to(res);
- else {
- CImg<Tfloat> tmp(sx,sy,_depth,_spectrum,0);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(sy>=256 && _width*_depth*_spectrum>=256))
- cimg_forXZC(tmp,x,z,v) {
- for (unsigned int a = _height*sy, b = _height, c = sy, s = 0, t = 0; a; ) {
- const unsigned int d = std::min(b,c);
- a-=d; b-=d; c-=d;
- if (instance_first) tmp(x,t,z,v)+=(Tfloat)(*this)(x,s,z,v)*d;
- else tmp(x,t,z,v)+=(Tfloat)res(x,s,z,v)*d;
- if (!b) { tmp(x,t++,z,v)/=_height; b = _height; }
- if (!c) { ++s; c = sy; }
- }
- }
- tmp.move_to(res);
- }
- instance_first = false;
- }
- if (sz!=_depth) {
- if (sz>_depth) get_resize(sx,sy,sz,_spectrum,1).move_to(res);
- else {
- CImg<Tfloat> tmp(sx,sy,sz,_spectrum,0);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(sz>=256 && _width*_height*_spectrum>=256))
- cimg_forXYC(tmp,x,y,v) {
- for (unsigned int a = _depth*sz, b = _depth, c = sz, s = 0, t = 0; a; ) {
- const unsigned int d = std::min(b,c);
- a-=d; b-=d; c-=d;
- if (instance_first) tmp(x,y,t,v)+=(Tfloat)(*this)(x,y,s,v)*d;
- else tmp(x,y,t,v)+=(Tfloat)res(x,y,s,v)*d;
- if (!b) { tmp(x,y,t++,v)/=_depth; b = _depth; }
- if (!c) { ++s; c = sz; }
- }
- }
- tmp.move_to(res);
- }
- instance_first = false;
- }
- if (sc!=_spectrum) {
- if (sc>_spectrum) get_resize(sx,sy,sz,sc,1).move_to(res);
- else {
- CImg<Tfloat> tmp(sx,sy,sz,sc,0);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(sc>=256 && _width*_height*_depth>=256))
- cimg_forXYZ(tmp,x,y,z) {
- for (unsigned int a = _spectrum*sc, b = _spectrum, c = sc, s = 0, t = 0; a; ) {
- const unsigned int d = std::min(b,c);
- a-=d; b-=d; c-=d;
- if (instance_first) tmp(x,y,z,t)+=(Tfloat)(*this)(x,y,z,s)*d;
- else tmp(x,y,z,t)+=(Tfloat)res(x,y,z,s)*d;
- if (!b) { tmp(x,y,z,t++)/=_spectrum; b = _spectrum; }
- if (!c) { ++s; c = sc; }
- }
- }
- tmp.move_to(res);
- }
- instance_first = false;
- }
- } break;
- // Linear interpolation.
- //
- case 3 : {
- CImg<uintT> off(cimg::max(sx,sy,sz,sc));
- CImg<doubleT> foff(off._width);
- CImg<T> resx, resy, resz, resc;
- double curr, old;
- if (sx!=_width) {
- if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
- else if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
- else {
- const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
- (double)_width/sx;
- resx.assign(sx,_height,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forX(resx,x) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(width() - 1.,curr + fx);
- *(poff++) = (unsigned int)curr - (unsigned int)old;
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
- cimg_forYZC(resx,y,z,c) {
- const T *ptrs = data(0,y,z,c), *const ptrsmax = ptrs + _width - 1;
- T *ptrd = resx.data(0,y,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forX(resx,x) {
- const double alpha = *(pfoff++);
- const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + 1):val1;
- *(ptrd++) = (T)((1 - alpha)*val1 + alpha*val2);
- ptrs+=*(poff++);
- }
- }
- }
- } else resx.assign(*this,true);
- if (sy!=_height) {
- if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
- else {
- if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
- else {
- const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
- (double)_height/sy;
- resy.assign(sx,sy,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forY(resy,y) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(height() - 1.,curr + fy);
- *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
- cimg_forXZC(resy,x,z,c) {
- const T *ptrs = resx.data(x,0,z,c), *const ptrsmax = ptrs + (_height - 1)*sx;
- T *ptrd = resy.data(x,0,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forY(resy,y) {
- const double alpha = *(pfoff++);
- const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sx):val1;
- *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
- ptrd+=sx;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resx.assign();
- } else resy.assign(resx,true);
- if (sz!=_depth) {
- if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
- else {
- if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
- else {
- const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
- (double)_depth/sz;
- const unsigned int sxy = sx*sy;
- resz.assign(sx,sy,sz,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(depth() - 1.,curr + fz);
- *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
- cimg_forXYC(resz,x,y,c) {
- const T *ptrs = resy.data(x,y,0,c), *const ptrsmax = ptrs + (_depth - 1)*sxy;
- T *ptrd = resz.data(x,y,0,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- const double alpha = *(pfoff++);
- const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sxy):val1;
- *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
- ptrd+=sxy;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resy.assign();
- } else resz.assign(resy,true);
- if (sc!=_spectrum) {
- if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
- else {
- if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
- else {
- const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
- (double)_spectrum/sc;
- const unsigned int sxyz = sx*sy*sz;
- resc.assign(sx,sy,sz,sc);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forC(resc,c) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(spectrum() - 1.,curr + fc);
- *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
- cimg_forXYZ(resc,x,y,z) {
- const T *ptrs = resz.data(x,y,z,0), *const ptrsmax = ptrs + (_spectrum - 1)*sxyz;
- T *ptrd = resc.data(x,y,z,0);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forC(resc,c) {
- const double alpha = *(pfoff++);
- const T val1 = *ptrs, val2 = ptrs<ptrsmax?*(ptrs + sxyz):val1;
- *ptrd = (T)((1 - alpha)*val1 + alpha*val2);
- ptrd+=sxyz;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resz.assign();
- } else resc.assign(resz,true);
- return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
- } break;
- // Grid interpolation.
- //
- case 4 : {
- CImg<T> resx, resy, resz, resc;
- if (sx!=_width) {
- if (sx<_width) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
- else {
- resx.assign(sx,_height,_depth,_spectrum,(T)0);
- const int dx = (int)(2*sx), dy = 2*width();
- int err = (int)(dy + centering_x*(sx*dy/width() - dy)), xs = 0;
- cimg_forX(resx,x) if ((err-=dy)<=0) {
- cimg_forYZC(resx,y,z,c) resx(x,y,z,c) = (*this)(xs,y,z,c);
- ++xs;
- err+=dx;
- }
- }
- } else resx.assign(*this,true);
- if (sy!=_height) {
- if (sy<_height) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
- else {
- resy.assign(sx,sy,_depth,_spectrum,(T)0);
- const int dx = (int)(2*sy), dy = 2*height();
- int err = (int)(dy + centering_y*(sy*dy/height() - dy)), ys = 0;
- cimg_forY(resy,y) if ((err-=dy)<=0) {
- cimg_forXZC(resy,x,z,c) resy(x,y,z,c) = resx(x,ys,z,c);
- ++ys;
- err+=dx;
- }
- }
- resx.assign();
- } else resy.assign(resx,true);
- if (sz!=_depth) {
- if (sz<_depth) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
- else {
- resz.assign(sx,sy,sz,_spectrum,(T)0);
- const int dx = (int)(2*sz), dy = 2*depth();
- int err = (int)(dy + centering_z*(sz*dy/depth() - dy)), zs = 0;
- cimg_forZ(resz,z) if ((err-=dy)<=0) {
- cimg_forXYC(resz,x,y,c) resz(x,y,z,c) = resy(x,y,zs,c);
- ++zs;
- err+=dx;
- }
- }
- resy.assign();
- } else resz.assign(resy,true);
- if (sc!=_spectrum) {
- if (sc<_spectrum) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
- else {
- resc.assign(sx,sy,sz,sc,(T)0);
- const int dx = (int)(2*sc), dy = 2*spectrum();
- int err = (int)(dy + centering_c*(sc*dy/spectrum() - dy)), cs = 0;
- cimg_forC(resc,c) if ((err-=dy)<=0) {
- cimg_forXYZ(resc,x,y,z) resc(x,y,z,c) = resz(x,y,z,cs);
- ++cs;
- err+=dx;
- }
- }
- resz.assign();
- } else resc.assign(resz,true);
- return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
- } break;
- // Cubic interpolation.
- //
- case 5 : {
- const Tfloat vmin = (Tfloat)cimg::type<T>::min(), vmax = (Tfloat)cimg::type<T>::max();
- CImg<uintT> off(cimg::max(sx,sy,sz,sc));
- CImg<doubleT> foff(off._width);
- CImg<T> resx, resy, resz, resc;
- double curr, old;
- if (sx!=_width) {
- if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
- else {
- if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
- else {
- const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
- (double)_width/sx;
- resx.assign(sx,_height,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forX(resx,x) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(width() - 1.,curr + fx);
- *(poff++) = (unsigned int)curr - (unsigned int)old;
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
- cimg_forYZC(resx,y,z,c) {
- const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_width - 2);
- T *ptrd = resx.data(0,y,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forX(resx,x) {
- const double
- t = *(pfoff++),
- val1 = (double)*ptrs,
- val0 = ptrs>ptrs0?(double)*(ptrs - 1):val1,
- val2 = ptrs<=ptrsmax?(double)*(ptrs + 1):val1,
- val3 = ptrs<ptrsmax?(double)*(ptrs + 2):val2,
- val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
- t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
- *(ptrd++) = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrs+=*(poff++);
- }
- }
- }
- }
- } else resx.assign(*this,true);
- if (sy!=_height) {
- if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
- else {
- if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
- else {
- const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
- (double)_height/sy;
- resy.assign(sx,sy,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forY(resy,y) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(height() - 1.,curr + fy);
- *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
- cimg_forXZC(resy,x,z,c) {
- const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_height - 2)*sx;
- T *ptrd = resy.data(x,0,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forY(resy,y) {
- const double
- t = *(pfoff++),
- val1 = (double)*ptrs,
- val0 = ptrs>ptrs0?(double)*(ptrs - sx):val1,
- val2 = ptrs<=ptrsmax?(double)*(ptrs + sx):val1,
- val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sx):val2,
- val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
- t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sx;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resx.assign();
- } else resy.assign(resx,true);
- if (sz!=_depth) {
- if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
- else {
- if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
- else {
- const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
- (double)_depth/sz;
- const unsigned int sxy = sx*sy;
- resz.assign(sx,sy,sz,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(depth() - 1.,curr + fz);
- *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
- cimg_forXYC(resz,x,y,c) {
- const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmax = ptrs + (_depth - 2)*sxy;
- T *ptrd = resz.data(x,y,0,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- const double
- t = *(pfoff++),
- val1 = (double)*ptrs,
- val0 = ptrs>ptrs0?(double)*(ptrs - sxy):val1,
- val2 = ptrs<=ptrsmax?(double)*(ptrs + sxy):val1,
- val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sxy):val2,
- val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
- t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sxy;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resy.assign();
- } else resz.assign(resy,true);
- if (sc!=_spectrum) {
- if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
- else {
- if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
- else {
- const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
- (double)_spectrum/sc;
- const unsigned int sxyz = sx*sy*sz;
- resc.assign(sx,sy,sz,sc);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forC(resc,c) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(spectrum() - 1.,curr + fc);
- *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
- cimg_forXYZ(resc,x,y,z) {
- const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmax = ptrs + (_spectrum - 2)*sxyz;
- T *ptrd = resc.data(x,y,z,0);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forC(resc,c) {
- const double
- t = *(pfoff++),
- val1 = (double)*ptrs,
- val0 = ptrs>ptrs0?(double)*(ptrs - sxyz):val1,
- val2 = ptrs<=ptrsmax?(double)*(ptrs + sxyz):val1,
- val3 = ptrs<ptrsmax?(double)*(ptrs + 2*sxyz):val2,
- val = val1 + 0.5f*(t*(-val0 + val2) + t*t*(2*val0 - 5*val1 + 4*val2 - val3) +
- t*t*t*(-val0 + 3*val1 - 3*val2 + val3));
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sxyz;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resz.assign();
- } else resc.assign(resz,true);
- return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
- } break;
- // Lanczos interpolation.
- //
- case 6 : {
- const double vmin = (double)cimg::type<T>::min(), vmax = (double)cimg::type<T>::max();
- CImg<uintT> off(cimg::max(sx,sy,sz,sc));
- CImg<doubleT> foff(off._width);
- CImg<T> resx, resy, resz, resc;
- double curr, old;
- if (sx!=_width) {
- if (_width==1) get_resize(sx,_height,_depth,_spectrum,1).move_to(resx);
- else {
- if (_width>sx) get_resize(sx,_height,_depth,_spectrum,2).move_to(resx);
- else {
- const double fx = (!boundary_conditions && sx>_width)?(sx>1?(_width - 1.)/(sx - 1):0):
- (double)_width/sx;
- resx.assign(sx,_height,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forX(resx,x) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(width() - 1.,curr + fx);
- *(poff++) = (unsigned int)curr - (unsigned int)old;
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resx._width>=256 && resx._height*resx._depth*resx._spectrum>=256))
- cimg_forYZC(resx,y,z,c) {
- const T *const ptrs0 = data(0,y,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + 1,
- *const ptrsmax = ptrs0 + (_width - 2);
- T *ptrd = resx.data(0,y,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forX(resx,x) {
- const double
- t = *(pfoff++),
- w0 = _cimg_lanczos(t + 2),
- w1 = _cimg_lanczos(t + 1),
- w2 = _cimg_lanczos(t),
- w3 = _cimg_lanczos(t - 1),
- w4 = _cimg_lanczos(t - 2),
- val2 = (double)*ptrs,
- val1 = ptrs>=ptrsmin?(double)*(ptrs - 1):val2,
- val0 = ptrs>ptrsmin?(double)*(ptrs - 2):val1,
- val3 = ptrs<=ptrsmax?(double)*(ptrs + 1):val2,
- val4 = ptrs<ptrsmax?(double)*(ptrs + 2):val3,
- val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
- *(ptrd++) = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrs+=*(poff++);
- }
- }
- }
- }
- } else resx.assign(*this,true);
- if (sy!=_height) {
- if (_height==1) resx.get_resize(sx,sy,_depth,_spectrum,1).move_to(resy);
- else {
- if (_height>sy) resx.get_resize(sx,sy,_depth,_spectrum,2).move_to(resy);
- else {
- const double fy = (!boundary_conditions && sy>_height)?(sy>1?(_height - 1.)/(sy - 1):0):
- (double)_height/sy;
- resy.assign(sx,sy,_depth,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forY(resy,y) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(height() - 1.,curr + fy);
- *(poff++) = sx*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resy._height>=256 && resy._width*resy._depth*resy._spectrum>=256))
- cimg_forXZC(resy,x,z,c) {
- const T *const ptrs0 = resx.data(x,0,z,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sx,
- *const ptrsmax = ptrs0 + (_height - 2)*sx;
- T *ptrd = resy.data(x,0,z,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forY(resy,y) {
- const double
- t = *(pfoff++),
- w0 = _cimg_lanczos(t + 2),
- w1 = _cimg_lanczos(t + 1),
- w2 = _cimg_lanczos(t),
- w3 = _cimg_lanczos(t - 1),
- w4 = _cimg_lanczos(t - 2),
- val2 = (double)*ptrs,
- val1 = ptrs>=ptrsmin?(double)*(ptrs - sx):val2,
- val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sx):val1,
- val3 = ptrs<=ptrsmax?(double)*(ptrs + sx):val2,
- val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sx):val3,
- val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sx;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resx.assign();
- } else resy.assign(resx,true);
- if (sz!=_depth) {
- if (_depth==1) resy.get_resize(sx,sy,sz,_spectrum,1).move_to(resz);
- else {
- if (_depth>sz) resy.get_resize(sx,sy,sz,_spectrum,2).move_to(resz);
- else {
- const double fz = (!boundary_conditions && sz>_depth)?(sz>1?(_depth - 1.)/(sz - 1):0):
- (double)_depth/sz;
- const unsigned int sxy = sx*sy;
- resz.assign(sx,sy,sz,_spectrum);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(depth() - 1.,curr + fz);
- *(poff++) = sxy*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resz._depth>=256 && resz._width*resz._height*resz._spectrum>=256))
- cimg_forXYC(resz,x,y,c) {
- const T *const ptrs0 = resy.data(x,y,0,c), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxy,
- *const ptrsmax = ptrs0 + (_depth - 2)*sxy;
- T *ptrd = resz.data(x,y,0,c);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forZ(resz,z) {
- const double
- t = *(pfoff++),
- w0 = _cimg_lanczos(t + 2),
- w1 = _cimg_lanczos(t + 1),
- w2 = _cimg_lanczos(t),
- w3 = _cimg_lanczos(t - 1),
- w4 = _cimg_lanczos(t - 2),
- val2 = (double)*ptrs,
- val1 = ptrs>=ptrsmin?(double)*(ptrs - sxy):val2,
- val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sxy):val1,
- val3 = ptrs<=ptrsmax?(double)*(ptrs + sxy):val2,
- val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sxy):val3,
- val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sxy;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resy.assign();
- } else resz.assign(resy,true);
- if (sc!=_spectrum) {
- if (_spectrum==1) resz.get_resize(sx,sy,sz,sc,1).move_to(resc);
- else {
- if (_spectrum>sc) resz.get_resize(sx,sy,sz,sc,2).move_to(resc);
- else {
- const double fc = (!boundary_conditions && sc>_spectrum)?(sc>1?(_spectrum - 1.)/(sc - 1):0):
- (double)_spectrum/sc;
- const unsigned int sxyz = sx*sy*sz;
- resc.assign(sx,sy,sz,sc);
- curr = old = 0;
- {
- unsigned int *poff = off._data;
- double *pfoff = foff._data;
- cimg_forC(resc,c) {
- *(pfoff++) = curr - (unsigned int)curr;
- old = curr;
- curr = std::min(spectrum() - 1.,curr + fc);
- *(poff++) = sxyz*((unsigned int)curr - (unsigned int)old);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(resc._spectrum>=256 && resc._width*resc._height*resc._depth>=256))
- cimg_forXYZ(resc,x,y,z) {
- const T *const ptrs0 = resz.data(x,y,z,0), *ptrs = ptrs0, *const ptrsmin = ptrs0 + sxyz,
- *const ptrsmax = ptrs + (_spectrum - 2)*sxyz;
- T *ptrd = resc.data(x,y,z,0);
- const unsigned int *poff = off._data;
- const double *pfoff = foff._data;
- cimg_forC(resc,c) {
- const double
- t = *(pfoff++),
- w0 = _cimg_lanczos(t + 2),
- w1 = _cimg_lanczos(t + 1),
- w2 = _cimg_lanczos(t),
- w3 = _cimg_lanczos(t - 1),
- w4 = _cimg_lanczos(t - 2),
- val2 = (double)*ptrs,
- val1 = ptrs>=ptrsmin?(double)*(ptrs - sxyz):val2,
- val0 = ptrs>ptrsmin?(double)*(ptrs - 2*sxyz):val1,
- val3 = ptrs<=ptrsmax?(double)*(ptrs + sxyz):val2,
- val4 = ptrs<ptrsmax?(double)*(ptrs + 2*sxyz):val3,
- val = (val0*w0 + val1*w1 + val2*w2 + val3*w3 + val4*w4)/(w1 + w2 + w3 + w4);
- *ptrd = (T)(val<vmin?vmin:val>vmax?vmax:val);
- ptrd+=sxyz;
- ptrs+=*(poff++);
- }
- }
- }
- }
- resz.assign();
- } else resc.assign(resz,true);
- return resc._is_shared?(resz._is_shared?(resy._is_shared?(resx._is_shared?(+(*this)):resx):resy):resz):resc;
- } break;
- // Unknown interpolation.
- //
- default :
- throw CImgArgumentException(_cimg_instance
- "resize(): Invalid specified interpolation %d "
- "(should be { -1=raw | 0=none | 1=nearest | 2=average | 3=linear | 4=grid | "
- "5=cubic | 6=lanczos }).",
- cimg_instance,
- interpolation_type);
- }
- return res;
- }
- //! Resize image to dimensions of another image.
- /**
- \param src Reference image used for dimensions.
- \param interpolation_type Interpolation method.
- \param boundary_conditions Boundary conditions.
- Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \param centering_x Set centering type (only if \p interpolation_type=0).
- \param centering_y Set centering type (only if \p interpolation_type=0).
- \param centering_z Set centering type (only if \p interpolation_type=0).
- \param centering_c Set centering type (only if \p interpolation_type=0).
- **/
- template<typename t>
- CImg<T>& resize(const CImg<t>& src,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) {
- return resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions,
- centering_x,centering_y,centering_z,centering_c);
- }
- //! Resize image to dimensions of another image \newinstance.
- template<typename t>
- CImg<T> get_resize(const CImg<t>& src,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) const {
- return get_resize(src._width,src._height,src._depth,src._spectrum,interpolation_type,boundary_conditions,
- centering_x,centering_y,centering_z,centering_c);
- }
- //! Resize image to dimensions of a display window.
- /**
- \param disp Reference display window used for dimensions.
- \param interpolation_type Interpolation method.
- \param boundary_conditions Boundary conditions.
- Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \param centering_x Set centering type (only if \p interpolation_type=0).
- \param centering_y Set centering type (only if \p interpolation_type=0).
- \param centering_z Set centering type (only if \p interpolation_type=0).
- \param centering_c Set centering type (only if \p interpolation_type=0).
- **/
- CImg<T>& resize(const CImgDisplay& disp,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) {
- return resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions,
- centering_x,centering_y,centering_z,centering_c);
- }
- //! Resize image to dimensions of a display window \newinstance.
- CImg<T> get_resize(const CImgDisplay& disp,
- const int interpolation_type=1, const unsigned int boundary_conditions=0,
- const float centering_x = 0, const float centering_y = 0,
- const float centering_z = 0, const float centering_c = 0) const {
- return get_resize(disp.width(),disp.height(),_depth,_spectrum,interpolation_type,boundary_conditions,
- centering_x,centering_y,centering_z,centering_c);
- }
- //! Resize image to half-size along XY axes, using an optimized filter.
- CImg<T>& resize_halfXY() {
- return get_resize_halfXY().move_to(*this);
- }
- //! Resize image to half-size along XY axes, using an optimized filter \newinstance.
- CImg<T> get_resize_halfXY() const {
- if (is_empty()) return *this;
- static const Tfloat kernel[9] = { 0.07842776544f, 0.1231940459f, 0.07842776544f,
- 0.1231940459f, 0.1935127547f, 0.1231940459f,
- 0.07842776544f, 0.1231940459f, 0.07842776544f };
- CImg<T> I(9), res(_width/2,_height/2,_depth,_spectrum);
- T *ptrd = res._data;
- cimg_forZC(*this,z,c) cimg_for3x3(*this,x,y,z,c,I,T)
- if (x%2 && y%2) *(ptrd++) = (T)
- (I[0]*kernel[0] + I[1]*kernel[1] + I[2]*kernel[2] +
- I[3]*kernel[3] + I[4]*kernel[4] + I[5]*kernel[5] +
- I[6]*kernel[6] + I[7]*kernel[7] + I[8]*kernel[8]);
- return res;
- }
- //! Resize image to double-size, using the Scale2X algorithm.
- /**
- \note Use anisotropic upscaling algorithm
- <a href="http://scale2x.sourceforge.net/algorithm.html">described here</a>.
- **/
- CImg<T>& resize_doubleXY() {
- return get_resize_doubleXY().move_to(*this);
- }
- //! Resize image to double-size, using the Scale2X algorithm \newinstance.
- CImg<T> get_resize_doubleXY() const {
- #define _cimg_gs2x_for3(bound,i) \
- for (int i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
- _n1##i<(int)(bound) || i==--_n1##i; \
- _p1##i = i++, ++_n1##i, ptrd1+=(res)._width, ptrd2+=(res)._width)
- #define _cimg_gs2x_for3x3(img,x,y,z,c,I,T) \
- _cimg_gs2x_for3((img)._height,y) for (int x = 0, \
- _p1##x = 0, \
- _n1##x = (int)( \
- (I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[3] = I[4] = (T)(img)(0,y,z,c)), \
- (I[7] = (T)(img)(0,_n1##y,z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x; \
- I[1] = I[2], \
- I[3] = I[4], I[4] = I[5], \
- I[7] = I[8], \
- _p1##x = x++, ++_n1##x)
- if (is_empty()) return *this;
- CImg<T> res(_width<<1,_height<<1,_depth,_spectrum);
- CImg_3x3(I,T);
- cimg_forZC(*this,z,c) {
- T
- *ptrd1 = res.data(0,0,z,c),
- *ptrd2 = ptrd1 + res._width;
- _cimg_gs2x_for3x3(*this,x,y,z,c,I,T) {
- if (Icp!=Icn && Ipc!=Inc) {
- *(ptrd1++) = Ipc==Icp?Ipc:Icc;
- *(ptrd1++) = Icp==Inc?Inc:Icc;
- *(ptrd2++) = Ipc==Icn?Ipc:Icc;
- *(ptrd2++) = Icn==Inc?Inc:Icc;
- } else { *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc; }
- }
- }
- return res;
- }
- //! Resize image to triple-size, using the Scale3X algorithm.
- /**
- \note Use anisotropic upscaling algorithm
- <a href="http://scale2x.sourceforge.net/algorithm.html">described here</a>.
- **/
- CImg<T>& resize_tripleXY() {
- return get_resize_tripleXY().move_to(*this);
- }
- //! Resize image to triple-size, using the Scale3X algorithm \newinstance.
- CImg<T> get_resize_tripleXY() const {
- #define _cimg_gs3x_for3(bound,i) \
- for (int i = 0, _p1##i = 0, \
- _n1##i = 1>=(bound)?(int)(bound) - 1:1; \
- _n1##i<(int)(bound) || i==--_n1##i; \
- _p1##i = i++, ++_n1##i, ptrd1+=2*(res)._width, ptrd2+=2*(res)._width, ptrd3+=2*(res)._width)
- #define _cimg_gs3x_for3x3(img,x,y,z,c,I,T) \
- _cimg_gs3x_for3((img)._height,y) for (int x = 0, \
- _p1##x = 0, \
- _n1##x = (int)( \
- (I[0] = I[1] = (T)(img)(_p1##x,_p1##y,z,c)), \
- (I[3] = I[4] = (T)(img)(0,y,z,c)), \
- (I[6] = I[7] = (T)(img)(0,_n1##y,z,c)), \
- 1>=(img)._width?(img).width() - 1:1); \
- (_n1##x<(img).width() && ( \
- (I[2] = (T)(img)(_n1##x,_p1##y,z,c)), \
- (I[5] = (T)(img)(_n1##x,y,z,c)), \
- (I[8] = (T)(img)(_n1##x,_n1##y,z,c)),1)) || \
- x==--_n1##x; \
- I[0] = I[1], I[1] = I[2], \
- I[3] = I[4], I[4] = I[5], \
- I[6] = I[7], I[7] = I[8], \
- _p1##x = x++, ++_n1##x)
- if (is_empty()) return *this;
- CImg<T> res(3*_width,3*_height,_depth,_spectrum);
- CImg_3x3(I,T);
- cimg_forZC(*this,z,c) {
- T
- *ptrd1 = res.data(0,0,z,c),
- *ptrd2 = ptrd1 + res._width,
- *ptrd3 = ptrd2 + res._width;
- _cimg_gs3x_for3x3(*this,x,y,z,c,I,T) {
- if (Icp != Icn && Ipc != Inc) {
- *(ptrd1++) = Ipc==Icp?Ipc:Icc;
- *(ptrd1++) = (Ipc==Icp && Icc!=Inp) || (Icp==Inc && Icc!=Ipp)?Icp:Icc;
- *(ptrd1++) = Icp==Inc?Inc:Icc;
- *(ptrd2++) = (Ipc==Icp && Icc!=Ipn) || (Ipc==Icn && Icc!=Ipp)?Ipc:Icc;
- *(ptrd2++) = Icc;
- *(ptrd2++) = (Icp==Inc && Icc!=Inn) || (Icn==Inc && Icc!=Inp)?Inc:Icc;
- *(ptrd3++) = Ipc==Icn?Ipc:Icc;
- *(ptrd3++) = (Ipc==Icn && Icc!=Inn) || (Icn==Inc && Icc!=Ipn)?Icn:Icc;
- *(ptrd3++) = Icn==Inc?Inc:Icc;
- } else {
- *(ptrd1++) = Icc; *(ptrd1++) = Icc; *(ptrd1++) = Icc;
- *(ptrd2++) = Icc; *(ptrd2++) = Icc; *(ptrd2++) = Icc;
- *(ptrd3++) = Icc; *(ptrd3++) = Icc; *(ptrd3++) = Icc;
- }
- }
- }
- return res;
- }
- //! Mirror image content along specified axis.
- /**
- \param axis Mirror axis
- **/
- CImg<T>& mirror(const char axis) {
- if (is_empty()) return *this;
- T *pf, *pb, *buf = 0;
- switch (cimg::lowercase(axis)) {
- case 'x' : {
- pf = _data; pb = data(_width - 1);
- const unsigned int width2 = _width/2;
- for (unsigned int yzv = 0; yzv<_height*_depth*_spectrum; ++yzv) {
- for (unsigned int x = 0; x<width2; ++x) { const T val = *pf; *(pf++) = *pb; *(pb--) = val; }
- pf+=_width - width2;
- pb+=_width + width2;
- }
- } break;
- case 'y' : {
- buf = new T[_width];
- pf = _data; pb = data(0,_height - 1);
- const unsigned int height2 = _height/2;
- for (unsigned int zv = 0; zv<_depth*_spectrum; ++zv) {
- for (unsigned int y = 0; y<height2; ++y) {
- std::memcpy(buf,pf,_width*sizeof(T));
- std::memcpy(pf,pb,_width*sizeof(T));
- std::memcpy(pb,buf,_width*sizeof(T));
- pf+=_width;
- pb-=_width;
- }
- pf+=(ulongT)_width*(_height - height2);
- pb+=(ulongT)_width*(_height + height2);
- }
- } break;
- case 'z' : {
- buf = new T[(ulongT)_width*_height];
- pf = _data; pb = data(0,0,_depth - 1);
- const unsigned int depth2 = _depth/2;
- cimg_forC(*this,c) {
- for (unsigned int z = 0; z<depth2; ++z) {
- std::memcpy(buf,pf,_width*_height*sizeof(T));
- std::memcpy(pf,pb,_width*_height*sizeof(T));
- std::memcpy(pb,buf,_width*_height*sizeof(T));
- pf+=(ulongT)_width*_height;
- pb-=(ulongT)_width*_height;
- }
- pf+=(ulongT)_width*_height*(_depth - depth2);
- pb+=(ulongT)_width*_height*(_depth + depth2);
- }
- } break;
- case 'c' : {
- buf = new T[(ulongT)_width*_height*_depth];
- pf = _data; pb = data(0,0,0,_spectrum - 1);
- const unsigned int _spectrum2 = _spectrum/2;
- for (unsigned int v = 0; v<_spectrum2; ++v) {
- std::memcpy(buf,pf,_width*_height*_depth*sizeof(T));
- std::memcpy(pf,pb,_width*_height*_depth*sizeof(T));
- std::memcpy(pb,buf,_width*_height*_depth*sizeof(T));
- pf+=(ulongT)_width*_height*_depth;
- pb-=(ulongT)_width*_height*_depth;
- }
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "mirror(): Invalid specified axis '%c'.",
- cimg_instance,
- axis);
- }
- delete[] buf;
- return *this;
- }
- //! Mirror image content along specified axis \newinstance.
- CImg<T> get_mirror(const char axis) const {
- return (+*this).mirror(axis);
- }
- //! Mirror image content along specified axes.
- /**
- \param axes Mirror axes, as a C-string.
- \note \c axes may contains multiple characters, e.g. \c "xyz"
- **/
- CImg<T>& mirror(const char *const axes) {
- for (const char *s = axes; *s; ++s) mirror(*s);
- return *this;
- }
- //! Mirror image content along specified axes \newinstance.
- CImg<T> get_mirror(const char *const axes) const {
- return (+*this).mirror(axes);
- }
- //! Shift image content.
- /**
- \param delta_x Amount of displacement along the X-axis.
- \param delta_y Amount of displacement along the Y-axis.
- \param delta_z Amount of displacement along the Z-axis.
- \param delta_c Amount of displacement along the C-axis.
- \param boundary_conditions Boundary conditions.
- Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- **/
- CImg<T>& shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0,
- const unsigned int boundary_conditions=0) {
- if (is_empty()) return *this;
- if (boundary_conditions==3)
- return get_crop(-delta_x,-delta_y,-delta_z,-delta_c,
- width() - delta_x - 1,
- height() - delta_y - 1,
- depth() - delta_z - 1,
- spectrum() - delta_c - 1,3).move_to(*this);
- if (delta_x) // Shift along X-axis
- switch (boundary_conditions) {
- case 2 : { // Periodic
- const int ml = cimg::mod(-delta_x,width()), ndelta_x = (ml<=width()/2)?ml:(ml-width());
- if (!ndelta_x) return *this;
- CImg<T> buf(cimg::abs(ndelta_x));
- if (ndelta_x>0) cimg_forYZC(*this,y,z,c) {
- std::memcpy(buf,data(0,y,z,c),ndelta_x*sizeof(T));
- std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T));
- std::memcpy(data(_width-ndelta_x,y,z,c),buf,ndelta_x*sizeof(T));
- } else cimg_forYZC(*this,y,z,c) {
- std::memcpy(buf,data(_width + ndelta_x,y,z,c),-ndelta_x*sizeof(T));
- std::memmove(data(-ndelta_x,y,z,c),data(0,y,z,c),(_width + ndelta_x)*sizeof(T));
- std::memcpy(data(0,y,z,c),buf,-ndelta_x*sizeof(T));
- }
- } break;
- case 1 : // Neumann
- if (delta_x<0) {
- const int ndelta_x = (-delta_x>=width())?width() - 1:-delta_x;
- if (!ndelta_x) return *this;
- cimg_forYZC(*this,y,z,c) {
- std::memmove(data(0,y,z,c),data(ndelta_x,y,z,c),(_width-ndelta_x)*sizeof(T));
- T *ptrd = data(_width - 1,y,z,c);
- const T val = *ptrd;
- for (int l = 0; l<ndelta_x - 1; ++l) *(--ptrd) = val;
- }
- } else {
- const int ndelta_x = (delta_x>=width())?width() - 1:delta_x;
- if (!ndelta_x) return *this;
- cimg_forYZC(*this,y,z,c) {
- std::memmove(data(ndelta_x,y,z,c),data(0,y,z,c),(_width-ndelta_x)*sizeof(T));
- T *ptrd = data(0,y,z,c);
- const T val = *ptrd;
- for (int l = 0; l<ndelta_x - 1; ++l) *(++ptrd) = val;
- }
- }
- break;
- default : // Dirichlet
- if (delta_x<=-width() || delta_x>=width()) return fill((T)0);
- if (delta_x<0) cimg_forYZC(*this,y,z,c) {
- std::memmove(data(0,y,z,c),data(-delta_x,y,z,c),(_width + delta_x)*sizeof(T));
- std::memset(data(_width + delta_x,y,z,c),0,-delta_x*sizeof(T));
- } else cimg_forYZC(*this,y,z,c) {
- std::memmove(data(delta_x,y,z,c),data(0,y,z,c),(_width-delta_x)*sizeof(T));
- std::memset(data(0,y,z,c),0,delta_x*sizeof(T));
- }
- }
- if (delta_y) // Shift along Y-axis
- switch (boundary_conditions) {
- case 2 : { // Periodic
- const int ml = cimg::mod(-delta_y,height()), ndelta_y = (ml<=height()/2)?ml:(ml-height());
- if (!ndelta_y) return *this;
- CImg<T> buf(width(),cimg::abs(ndelta_y));
- if (ndelta_y>0) cimg_forZC(*this,z,c) {
- std::memcpy(buf,data(0,0,z,c),_width*ndelta_y*sizeof(T));
- std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T));
- std::memcpy(data(0,_height-ndelta_y,z,c),buf,_width*ndelta_y*sizeof(T));
- } else cimg_forZC(*this,z,c) {
- std::memcpy(buf,data(0,_height + ndelta_y,z,c),-ndelta_y*_width*sizeof(T));
- std::memmove(data(0,-ndelta_y,z,c),data(0,0,z,c),_width*(_height + ndelta_y)*sizeof(T));
- std::memcpy(data(0,0,z,c),buf,-ndelta_y*_width*sizeof(T));
- }
- } break;
- case 1 : // Neumann
- if (delta_y<0) {
- const int ndelta_y = (-delta_y>=height())?height() - 1:-delta_y;
- if (!ndelta_y) return *this;
- cimg_forZC(*this,z,c) {
- std::memmove(data(0,0,z,c),data(0,ndelta_y,z,c),_width*(_height-ndelta_y)*sizeof(T));
- T *ptrd = data(0,_height-ndelta_y,z,c), *ptrs = data(0,_height - 1,z,c);
- for (int l = 0; l<ndelta_y - 1; ++l) { std::memcpy(ptrd,ptrs,_width*sizeof(T)); ptrd+=_width; }
- }
- } else {
- const int ndelta_y = (delta_y>=height())?height() - 1:delta_y;
- if (!ndelta_y) return *this;
- cimg_forZC(*this,z,c) {
- std::memmove(data(0,ndelta_y,z,c),data(0,0,z,c),_width*(_height-ndelta_y)*sizeof(T));
- T *ptrd = data(0,1,z,c), *ptrs = data(0,0,z,c);
- for (int l = 0; l<ndelta_y - 1; ++l) { std::memcpy(ptrd,ptrs,_width*sizeof(T)); ptrd+=_width; }
- }
- }
- break;
- default : // Dirichlet
- if (delta_y<=-height() || delta_y>=height()) return fill((T)0);
- if (delta_y<0) cimg_forZC(*this,z,c) {
- std::memmove(data(0,0,z,c),data(0,-delta_y,z,c),_width*(_height + delta_y)*sizeof(T));
- std::memset(data(0,_height + delta_y,z,c),0,-delta_y*_width*sizeof(T));
- } else cimg_forZC(*this,z,c) {
- std::memmove(data(0,delta_y,z,c),data(0,0,z,c),_width*(_height-delta_y)*sizeof(T));
- std::memset(data(0,0,z,c),0,delta_y*_width*sizeof(T));
- }
- }
- if (delta_z) // Shift along Z-axis
- switch (boundary_conditions) {
- case 2 : { // Periodic
- const int ml = cimg::mod(-delta_z,depth()), ndelta_z = (ml<=depth()/2)?ml:(ml-depth());
- if (!ndelta_z) return *this;
- CImg<T> buf(width(),height(),cimg::abs(ndelta_z));
- if (ndelta_z>0) cimg_forC(*this,c) {
- std::memcpy(buf,data(0,0,0,c),_width*_height*ndelta_z*sizeof(T));
- std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
- std::memcpy(data(0,0,_depth-ndelta_z,c),buf,_width*_height*ndelta_z*sizeof(T));
- } else cimg_forC(*this,c) {
- std::memcpy(buf,data(0,0,_depth + ndelta_z,c),-ndelta_z*_width*_height*sizeof(T));
- std::memmove(data(0,0,-ndelta_z,c),data(0,0,0,c),_width*_height*(_depth + ndelta_z)*sizeof(T));
- std::memcpy(data(0,0,0,c),buf,-ndelta_z*_width*_height*sizeof(T));
- }
- } break;
- case 1 : // Neumann
- if (delta_z<0) {
- const int ndelta_z = (-delta_z>=depth())?depth() - 1:-delta_z;
- if (!ndelta_z) return *this;
- cimg_forC(*this,c) {
- std::memmove(data(0,0,0,c),data(0,0,ndelta_z,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
- T *ptrd = data(0,0,_depth-ndelta_z,c), *ptrs = data(0,0,_depth - 1,c);
- for (int l = 0; l<ndelta_z - 1; ++l) {
- std::memcpy(ptrd,ptrs,_width*_height*sizeof(T)); ptrd+=(ulongT)_width*_height;
- }
- }
- } else {
- const int ndelta_z = (delta_z>=depth())?depth() - 1:delta_z;
- if (!ndelta_z) return *this;
- cimg_forC(*this,c) {
- std::memmove(data(0,0,ndelta_z,c),data(0,0,0,c),_width*_height*(_depth-ndelta_z)*sizeof(T));
- T *ptrd = data(0,0,1,c), *ptrs = data(0,0,0,c);
- for (int l = 0; l<ndelta_z - 1; ++l) {
- std::memcpy(ptrd,ptrs,_width*_height*sizeof(T)); ptrd+=(ulongT)_width*_height;
- }
- }
- }
- break;
- default : // Dirichlet
- if (delta_z<=-depth() || delta_z>=depth()) return fill((T)0);
- if (delta_z<0) cimg_forC(*this,c) {
- std::memmove(data(0,0,0,c),data(0,0,-delta_z,c),_width*_height*(_depth + delta_z)*sizeof(T));
- std::memset(data(0,0,_depth + delta_z,c),0,_width*_height*(-delta_z)*sizeof(T));
- } else cimg_forC(*this,c) {
- std::memmove(data(0,0,delta_z,c),data(0,0,0,c),_width*_height*(_depth-delta_z)*sizeof(T));
- std::memset(data(0,0,0,c),0,delta_z*_width*_height*sizeof(T));
- }
- }
- if (delta_c) // Shift along C-axis
- switch (boundary_conditions) {
- case 2 : { // Periodic
- const int ml = cimg::mod(-delta_c,spectrum()), ndelta_c = (ml<=spectrum()/2)?ml:(ml-spectrum());
- if (!ndelta_c) return *this;
- CImg<T> buf(width(),height(),depth(),cimg::abs(ndelta_c));
- if (ndelta_c>0) {
- std::memcpy(buf,_data,_width*_height*_depth*ndelta_c*sizeof(T));
- std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
- std::memcpy(data(0,0,0,_spectrum-ndelta_c),buf,_width*_height*_depth*ndelta_c*sizeof(T));
- } else {
- std::memcpy(buf,data(0,0,0,_spectrum + ndelta_c),-ndelta_c*_width*_height*_depth*sizeof(T));
- std::memmove(data(0,0,0,-ndelta_c),_data,_width*_height*_depth*(_spectrum + ndelta_c)*sizeof(T));
- std::memcpy(_data,buf,-ndelta_c*_width*_height*_depth*sizeof(T));
- }
- } break;
- case 1 : // Neumann
- if (delta_c<0) {
- const int ndelta_c = (-delta_c>=spectrum())?spectrum() - 1:-delta_c;
- if (!ndelta_c) return *this;
- std::memmove(_data,data(0,0,0,ndelta_c),_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
- T *ptrd = data(0,0,0,_spectrum-ndelta_c), *ptrs = data(0,0,0,_spectrum - 1);
- for (int l = 0; l<ndelta_c - 1; ++l) {
- std::memcpy(ptrd,ptrs,_width*_height*_depth*sizeof(T)); ptrd+=(ulongT)_width*_height*_depth;
- }
- } else {
- const int ndelta_c = (delta_c>=spectrum())?spectrum() - 1:delta_c;
- if (!ndelta_c) return *this;
- std::memmove(data(0,0,0,ndelta_c),_data,_width*_height*_depth*(_spectrum-ndelta_c)*sizeof(T));
- T *ptrd = data(0,0,0,1);
- for (int l = 0; l<ndelta_c - 1; ++l) {
- std::memcpy(ptrd,_data,_width*_height*_depth*sizeof(T)); ptrd+=(ulongT)_width*_height*_depth;
- }
- }
- break;
- default : // Dirichlet
- if (delta_c<=-spectrum() || delta_c>=spectrum()) return fill((T)0);
- if (delta_c<0) {
- std::memmove(_data,data(0,0,0,-delta_c),_width*_height*_depth*(_spectrum + delta_c)*sizeof(T));
- std::memset(data(0,0,0,_spectrum + delta_c),0,_width*_height*_depth*(-delta_c)*sizeof(T));
- } else {
- std::memmove(data(0,0,0,delta_c),_data,_width*_height*_depth*(_spectrum-delta_c)*sizeof(T));
- std::memset(_data,0,delta_c*_width*_height*_depth*sizeof(T));
- }
- }
- return *this;
- }
- //! Shift image content \newinstance.
- CImg<T> get_shift(const int delta_x, const int delta_y=0, const int delta_z=0, const int delta_c=0,
- const unsigned int boundary_conditions=0) const {
- return (+*this).shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions);
- }
- //! Permute axes order.
- /**
- \param axes_order Axes permutations, as a C-string of 4 characters.
- This function permutes image content regarding the specified axes permutation.
- **/
- CImg<T>& permute_axes(const char *const axes_order) {
- return get_permute_axes(axes_order).move_to(*this);
- }
- //! Permute axes order \newinstance.
- CImg<T> get_permute_axes(const char *const axes_order) const {
- const T foo = (T)0;
- return _permute_axes(axes_order,foo);
- }
- template<typename t>
- CImg<t> _permute_axes(const char *const axes_order, const t&) const {
- if (is_empty() || !axes_order) return CImg<t>(*this,false);
- CImg<t> res;
- const T* ptrs = _data;
- unsigned char s_code[4] = { 0,1,2,3 }, n_code[4] = { 0 };
- for (unsigned int l = 0; axes_order[l]; ++l) {
- int c = cimg::lowercase(axes_order[l]);
- if (l>=4 || (c!='x' && c!='y' && c!='z' && c!='c')) { *s_code = 4; break; }
- else { ++n_code[c%=4]; s_code[l] = (unsigned char)c; }
- }
- if (*axes_order && *s_code<4 && *n_code<=1 && n_code[1]<=1 && n_code[2]<=1 && n_code[3]<=1) {
- const unsigned int code = (s_code[0]<<12) | (s_code[1]<<8) | (s_code[2]<<4) | (s_code[3]);
- ulongT wh, whd;
- switch (code) {
- case 0x0123 : // xyzc
- return +*this;
- case 0x0132 : // xycz
- res.assign(_width,_height,_spectrum,_depth);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(x,y,c,z,wh,whd) = (t)*(ptrs++);
- break;
- case 0x0213 : // xzyc
- res.assign(_width,_depth,_height,_spectrum);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(x,z,y,c,wh,whd) = (t)*(ptrs++);
- break;
- case 0x0231 : // xzcy
- res.assign(_width,_depth,_spectrum,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(x,z,c,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x0312 : // xcyz
- res.assign(_width,_spectrum,_height,_depth);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(x,c,y,z,wh,whd) = (t)*(ptrs++);
- break;
- case 0x0321 : // xczy
- res.assign(_width,_spectrum,_depth,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(x,c,z,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x1023 : // yxzc
- res.assign(_height,_width,_depth,_spectrum);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,x,z,c,wh,whd) = (t)*(ptrs++);
- break;
- case 0x1032 : // yxcz
- res.assign(_height,_width,_spectrum,_depth);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,x,c,z,wh,whd) = (t)*(ptrs++);
- break;
- case 0x1203 : // yzxc
- res.assign(_height,_depth,_width,_spectrum);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,z,x,c,wh,whd) = (t)*(ptrs++);
- break;
- case 0x1230 : // yzcx
- res.assign(_height,_depth,_spectrum,_width);
- switch (_width) {
- case 1 : {
- t *ptr_r = res.data(0,0,0,0);
- for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
- *(ptr_r++) = (t)*(ptrs++);
- }
- } break;
- case 2 : {
- t *ptr_r = res.data(0,0,0,0), *ptr_g = res.data(0,0,0,1);
- for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
- *(ptr_r++) = (t)ptrs[0];
- *(ptr_g++) = (t)ptrs[1];
- ptrs+=2;
- }
- } break;
- case 3 : { // Optimization for the classical conversion from interleaved RGB to planar RGB
- 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);
- for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
- *(ptr_r++) = (t)ptrs[0];
- *(ptr_g++) = (t)ptrs[1];
- *(ptr_b++) = (t)ptrs[2];
- ptrs+=3;
- }
- } break;
- case 4 : { // Optimization for the classical conversion from interleaved RGBA to planar RGBA
- 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), *ptr_a = res.data(0,0,0,3);
- for (unsigned int siz = _height*_depth*_spectrum; siz; --siz) {
- *(ptr_r++) = (t)ptrs[0];
- *(ptr_g++) = (t)ptrs[1];
- *(ptr_b++) = (t)ptrs[2];
- *(ptr_a++) = (t)ptrs[3];
- ptrs+=4;
- }
- } break;
- default : {
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,z,c,x,wh,whd) = *(ptrs++);
- return res;
- }
- }
- break;
- case 0x1302 : // ycxz
- res.assign(_height,_spectrum,_width,_depth);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,c,x,z,wh,whd) = (t)*(ptrs++);
- break;
- case 0x1320 : // yczx
- res.assign(_height,_spectrum,_depth,_width);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(y,c,z,x,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2013 : // zxyc
- res.assign(_depth,_width,_height,_spectrum);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,x,y,c,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2031 : // zxcy
- res.assign(_depth,_width,_spectrum,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,x,c,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2103 : // zyxc
- res.assign(_depth,_height,_width,_spectrum);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,y,x,c,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2130 : // zycx
- res.assign(_depth,_height,_spectrum,_width);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,y,c,x,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2301 : // zcxy
- res.assign(_depth,_spectrum,_width,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,c,x,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x2310 : // zcyx
- res.assign(_depth,_spectrum,_height,_width);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(z,c,y,x,wh,whd) = (t)*(ptrs++);
- break;
- case 0x3012 : // cxyz
- res.assign(_spectrum,_width,_height,_depth);
- switch (_spectrum) {
- case 1 : {
- const T *ptr_r = data(0,0,0,0);
- t *ptrd = res._data;
- for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) *(ptrd++) = (t)*(ptr_r++);
- } break;
- case 2 : {
- const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1);
- t *ptrd = res._data;
- for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
- ptrd[0] = (t)*(ptr_r++);
- ptrd[1] = (t)*(ptr_g++);
- ptrd+=2;
- }
- } break;
- case 3 : { // Optimization for the classical conversion from planar RGB to interleaved RGB
- const T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
- t *ptrd = res._data;
- for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
- ptrd[0] = (t)*(ptr_r++);
- ptrd[1] = (t)*(ptr_g++);
- ptrd[2] = (t)*(ptr_b++);
- ptrd+=3;
- }
- } break;
- case 4 : { // Optimization for the classical conversion from planar RGBA to interleaved RGBA
- 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);
- t *ptrd = res._data;
- for (ulongT siz = (ulongT)_width*_height*_depth; siz; --siz) {
- ptrd[0] = (t)*(ptr_r++);
- ptrd[1] = (t)*(ptr_g++);
- ptrd[2] = (t)*(ptr_b++);
- ptrd[3] = (t)*(ptr_a++);
- ptrd+=4;
- }
- } break;
- default : {
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,x,y,z,wh,whd) = (t)*(ptrs++);
- }
- }
- break;
- case 0x3021 : // cxzy
- res.assign(_spectrum,_width,_depth,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,x,z,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x3102 : // cyxz
- res.assign(_spectrum,_height,_width,_depth);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,y,x,z,wh,whd) = (t)*(ptrs++);
- break;
- case 0x3120 : // cyzx
- res.assign(_spectrum,_height,_depth,_width);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,y,z,x,wh,whd) = (t)*(ptrs++);
- break;
- case 0x3201 : // czxy
- res.assign(_spectrum,_depth,_width,_height);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,z,x,y,wh,whd) = (t)*(ptrs++);
- break;
- case 0x3210 : // czyx
- res.assign(_spectrum,_depth,_height,_width);
- wh = (ulongT)res._width*res._height; whd = wh*res._depth;
- cimg_forXYZC(*this,x,y,z,c) res(c,z,y,x,wh,whd) = (t)*(ptrs++);
- break;
- }
- }
- if (!res)
- throw CImgArgumentException(_cimg_instance
- "permute_axes(): Invalid specified axes order '%s'.",
- cimg_instance,
- axes_order);
- return res;
- }
- //! Unroll pixel values along specified axis.
- /**
- \param axis Unroll axis (can be \c 'x', \c 'y', \c 'z' or c 'c').
- **/
- CImg<T>& unroll(const char axis) {
- const unsigned int siz = (unsigned int)size();
- if (siz) switch (cimg::lowercase(axis)) {
- case 'x' : _width = siz; _height = _depth = _spectrum = 1; break;
- case 'y' : _height = siz; _width = _depth = _spectrum = 1; break;
- case 'z' : _depth = siz; _width = _height = _spectrum = 1; break;
- case 'c' : _spectrum = siz; _width = _height = _depth = 1; break;
- }
- return *this;
- }
- //! Unroll pixel values along specified axis \newinstance.
- CImg<T> get_unroll(const char axis) const {
- return (+*this).unroll(axis);
- }
- //! Rotate image with arbitrary angle.
- /**
- \param angle Rotation angle, in degrees.
- \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \note The size of the image is modified.
- **/
- CImg<T>& rotate(const float angle, const unsigned int interpolation=1,
- const unsigned int boundary_conditions=0) {
- const float nangle = cimg::mod(angle,360.f);
- if (nangle==0.f) return *this;
- return get_rotate(nangle,interpolation,boundary_conditions).move_to(*this);
- }
- //! Rotate image with arbitrary angle \newinstance.
- CImg<T> get_rotate(const float angle, const unsigned int interpolation=1,
- const unsigned int boundary_conditions=0) const {
- if (is_empty()) return *this;
- CImg<T> res;
- const float nangle = cimg::mod(angle,360.f);
- if (boundary_conditions!=1 && cimg::mod(nangle,90.f)==0) { // Optimized version for orthogonal angles
- const int wm1 = width() - 1, hm1 = height() - 1;
- const int iangle = (int)nangle/90;
- switch (iangle) {
- case 1 : { // 90 deg
- res.assign(_height,_width,_depth,_spectrum);
- T *ptrd = res._data;
- cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(y,hm1 - x,z,c);
- } break;
- case 2 : { // 180 deg
- res.assign(_width,_height,_depth,_spectrum);
- T *ptrd = res._data;
- cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1 - x,hm1 - y,z,c);
- } break;
- case 3 : { // 270 deg
- res.assign(_height,_width,_depth,_spectrum);
- T *ptrd = res._data;
- cimg_forXYZC(res,x,y,z,c) *(ptrd++) = (*this)(wm1 - y,x,z,c);
- } break;
- default : // 0 deg
- return *this;
- }
- } else { // Generic angle
- const float
- rad = (float)(nangle*cimg::PI/180.),
- ca = (float)std::cos(rad), sa = (float)std::sin(rad),
- ux = cimg::abs((_width - 1)*ca), uy = cimg::abs((_width - 1)*sa),
- vx = cimg::abs((_height - 1)*sa), vy = cimg::abs((_height - 1)*ca),
- w2 = 0.5f*(_width - 1), h2 = 0.5f*(_height - 1);
- res.assign((int)cimg::round(1 + ux + vx),(int)cimg::round(1 + uy + vy),_depth,_spectrum);
- const float rw2 = 0.5f*(res._width - 1), rh2 = 0.5f*(res._height - 1);
- _rotate(res,nangle,interpolation,boundary_conditions,w2,h2,rw2,rh2);
- }
- return res;
- }
- //! Rotate image with arbitrary angle, around a center point.
- /**
- \param angle Rotation angle, in degrees.
- \param cx X-coordinate of the rotation center.
- \param cy Y-coordinate of the rotation center.
- \param interpolation Type of interpolation, <tt>{ 0=nearest | 1=linear | 2=cubic | 3=mirror }</tt>.
- \param boundary_conditions Boundary conditions, <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- **/
- CImg<T>& rotate(const float angle, const float cx, const float cy,
- const unsigned int interpolation, const unsigned int boundary_conditions=0) {
- return get_rotate(angle,cx,cy,interpolation,boundary_conditions).move_to(*this);
- }
- //! Rotate image with arbitrary angle, around a center point \newinstance.
- CImg<T> get_rotate(const float angle, const float cx, const float cy,
- const unsigned int interpolation, const unsigned int boundary_conditions=0) const {
- if (is_empty()) return *this;
- CImg<T> res(_width,_height,_depth,_spectrum);
- _rotate(res,angle,interpolation,boundary_conditions,cx,cy,cx,cy);
- return res;
- }
- // [internal] Perform 2D rotation with arbitrary angle.
- void _rotate(CImg<T>& res, const float angle,
- const unsigned int interpolation, const unsigned int boundary_conditions,
- const float w2, const float h2,
- const float rw2, const float rh2) const {
- const float
- rad = (float)(angle*cimg::PI/180.),
- ca = (float)std::cos(rad), sa = (float)std::sin(rad);
- switch (boundary_conditions) {
- case 3 : { // Mirror
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- const float ww = 2.f*width(), hh = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2,
- mx = cimg::mod(w2 + xc*ca + yc*sa,ww),
- my = cimg::mod(h2 - xc*sa + yc*ca,hh);
- res(x,y,z,c) = _cubic_atXY_c(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- const float ww = 2.f*width(), hh = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2,
- mx = cimg::mod(w2 + xc*ca + yc*sa,ww),
- my = cimg::mod(h2 - xc*sa + yc*ca,hh);
- res(x,y,z,c) = (T)_linear_atXY(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- const int ww = 2*width(), hh = 2*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2,
- mx = cimg::mod((int)cimg::round(w2 + xc*ca + yc*sa),ww),
- my = cimg::mod((int)cimg::round(h2 - xc*sa + yc*ca),hh);
- res(x,y,z,c) = (*this)(mx<width()?mx:ww - mx - 1,my<height()?my:hh - my - 1,z,c);
- }
- }
- }
- } break;
- case 2 : // Periodic
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = _cubic_atXY_pc(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = (T)_linear_atXY_p(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = (*this)(cimg::mod((int)cimg::round(w2 + xc*ca + yc*sa),(float)width()),
- cimg::mod((int)cimg::round(h2 - xc*sa + yc*ca),(float)height()),z,c);
- }
- }
- } break;
- case 1 : // Neumann
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = _cubic_atXY_c(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = (T)_linear_atXY(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = _atXY((int)cimg::round(w2 + xc*ca + yc*sa),
- (int)cimg::round(h2 - xc*sa + yc*ca),z,c);
- }
- }
- } break;
- default : // Dirichlet
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = cubic_atXY_c(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c,(T)0);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = (T)linear_atXY(w2 + xc*ca + yc*sa,h2 - xc*sa + yc*ca,z,c,(T)0);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZC(res,x,y,z,c) {
- const float xc = x - rw2, yc = y - rh2;
- res(x,y,z,c) = atXY((int)cimg::round(w2 + xc*ca + yc*sa),
- (int)cimg::round(h2 - xc*sa + yc*ca),z,c,(T)0);
- }
- }
- }
- }
- }
- //! Rotate volumetric image with arbitrary angle and axis.
- /**
- \param u X-coordinate of the 3D rotation axis.
- \param v Y-coordinate of the 3D rotation axis.
- \param w Z-coordinate of the 3D rotation axis.
- \param angle Rotation angle, in degrees.
- \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \note Most of the time, size of the image is modified.
- **/
- CImg<T> rotate(const float u, const float v, const float w, const float angle,
- const unsigned int interpolation, const unsigned int boundary_conditions) {
- const float nangle = cimg::mod(angle,360.f);
- if (nangle==0.f) return *this;
- return get_rotate(u,v,w,nangle,interpolation,boundary_conditions).move_to(*this);
- }
- //! Rotate volumetric image with arbitrary angle and axis \newinstance.
- CImg<T> get_rotate(const float u, const float v, const float w, const float angle,
- const unsigned int interpolation, const unsigned int boundary_conditions) const {
- if (is_empty()) return *this;
- CImg<T> res;
- const float
- w1 = _width - 1, h1 = _height - 1, d1 = _depth -1,
- w2 = 0.5f*w1, h2 = 0.5f*h1, d2 = 0.5f*d1;
- CImg<floatT> R = CImg<floatT>::rotation_matrix(u,v,w,angle);
- const CImg<Tfloat>
- X = R*CImg<Tfloat>(8,3,1,1,
- 0.f,w1,w1,0.f,0.f,w1,w1,0.f,
- 0.f,0.f,h1,h1,0.f,0.f,h1,h1,
- 0.f,0.f,0.f,0.f,d1,d1,d1,d1);
- float
- xm, xM = X.get_shared_row(0).max_min(xm),
- ym, yM = X.get_shared_row(1).max_min(ym),
- zm, zM = X.get_shared_row(2).max_min(zm);
- const int
- dx = (int)cimg::round(xM - xm),
- dy = (int)cimg::round(yM - ym),
- dz = (int)cimg::round(zM - zm);
- R.transpose();
- res.assign(1 + dx,1 + dy,1 + dz,_spectrum);
- const float rw2 = 0.5f*dx, rh2 = 0.5f*dy, rd2 = 0.5f*dz;
- _rotate(res,R,interpolation,boundary_conditions,w2,h2,d2,rw2,rh2,rd2);
- return res;
- }
- //! Rotate volumetric image with arbitrary angle and axis, around a center point.
- /**
- \param u X-coordinate of the 3D rotation axis.
- \param v Y-coordinate of the 3D rotation axis.
- \param w Z-coordinate of the 3D rotation axis.
- \param angle Rotation angle, in degrees.
- \param cx X-coordinate of the rotation center.
- \param cy Y-coordinate of the rotation center.
- \param cz Z-coordinate of the rotation center.
- \param interpolation Type of interpolation. Can be <tt>{ 0=nearest | 1=linear | 2=cubic | 3=mirror }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic }</tt>.
- \note Most of the time, size of the image is modified.
- **/
- CImg<T> rotate(const float u, const float v, const float w, const float angle,
- const float cx, const float cy, const float cz,
- const unsigned int interpolation=1, const unsigned int boundary_conditions=0) {
- const float nangle = cimg::mod(angle,360.f);
- if (nangle==0.f) return *this;
- return get_rotate(u,v,w,nangle,cx,cy,cz,interpolation,boundary_conditions).move_to(*this);
- }
- //! Rotate volumetric image with arbitrary angle and axis, around a center point \newinstance.
- CImg<T> get_rotate(const float u, const float v, const float w, const float angle,
- const float cx, const float cy, const float cz,
- const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const {
- if (is_empty()) return *this;
- CImg<T> res(_width,_height,_depth,_spectrum);
- CImg<floatT> R = CImg<floatT>::rotation_matrix(u,v,w,-angle);
- _rotate(res,R,interpolation,boundary_conditions,cx,cy,cz,cx,cy,cz);
- return res;
- }
- // [internal] Perform 3D rotation with arbitrary axis and angle.
- void _rotate(CImg<T>& res, const CImg<Tfloat>& R,
- const unsigned int interpolation, const unsigned int boundary_conditions,
- const float w2, const float h2, const float d2,
- const float rw2, const float rh2, const float rd2) const {
- switch (boundary_conditions) {
- case 3 : // Mirror
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- const float ww = 2.f*width(), hh = 2.f*height(), dd = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = cimg::mod((float)(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
- Y = cimg::mod((float)(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
- Z = cimg::mod((float)(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
- cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_c(X<width()?X:ww - X - 1,
- Y<height()?Y:hh - Y - 1,
- Z<depth()?Z:dd - Z - z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- const float ww = 2.f*width(), hh = 2.f*height(), dd = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = cimg::mod((float)(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
- Y = cimg::mod((float)(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
- Z = cimg::mod((float)(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
- cimg_forC(res,c) res(x,y,z,c) = (T)_linear_atXYZ(X<width()?X:ww - X - 1,
- Y<height()?Y:hh - Y - 1,
- Z<depth()?Z:dd - Z - 1,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- const int ww = 2*width(), hh = 2*height(), dd = 2*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
- const int
- X = cimg::mod((int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),ww),
- Y = cimg::mod((int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),hh),
- Z = cimg::mod((int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),dd);
- cimg_forC(res,c) res(x,y,z,c) = (*this)(X<width()?X:ww - X - 1,
- Y<height()?Y:hh - Y - 1,
- Z<depth()?Z:dd - Z - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_pc(X,Y,Z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = (T)_linear_atXYZ_p(X,Y,Z,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
- const int
- X = cimg::mod((int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),width()),
- Y = cimg::mod((int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),height()),
- Z = cimg::mod((int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc),depth());
- cimg_forC(res,c) res(x,y,z,c) = (*this)(X,Y,Z,c);
- }
- }
- } break;
- case 1 : // Neumann
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = _cubic_atXYZ_c(X,Y,Z,c);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = _linear_atXYZ(X,Y,Z,c);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
- const int
- X = (int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),
- Y = (int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),
- Z = (int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc);
- cimg_forC(res,c) res(x,y,z,c) = _atXYZ(X,Y,Z,c);
- }
- }
- } break;
- default : // Dirichlet
- switch (interpolation) {
- case 2 : { // Cubic interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = cubic_atXYZ_c(X,Y,Z,c,(T)0);
- }
- } break;
- case 1 : { // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float
- xc = x - rw2, yc = y - rh2, zc = z - rd2,
- X = w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc,
- Y = h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc,
- Z = d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc;
- cimg_forC(res,c) res(x,y,z,c) = linear_atXYZ(X,Y,Z,c,(T)0);
- }
- } break;
- default : { // Nearest-neighbor interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(res.size(),2048))
- cimg_forXYZ(res,x,y,z) {
- const float xc = x - rw2, yc = y - rh2, zc = z - rd2;
- const int
- X = (int)cimg::round(w2 + R(0,0)*xc + R(1,0)*yc + R(2,0)*zc),
- Y = (int)cimg::round(h2 + R(0,1)*xc + R(1,1)*yc + R(2,1)*zc),
- Z = (int)cimg::round(d2 + R(0,2)*xc + R(1,2)*yc + R(2,2)*zc);
- cimg_forC(res,c) res(x,y,z,c) = atXYZ(X,Y,Z,c,(T)0);
- }
- }
- } break;
- }
- }
- //! Warp image content by a warping field.
- /**
- \param warp Warping field.
- \param mode Can be { 0=backward-absolute | 1=backward-relative | 2=forward-absolute | 3=foward-relative }
- \param interpolation Can be <tt>{ 0=nearest | 1=linear | 2=cubic }</tt>.
- \param boundary_conditions Boundary conditions <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- **/
- template<typename t>
- CImg<T>& warp(const CImg<t>& p_warp, const unsigned int mode=0,
- const unsigned int interpolation=1, const unsigned int boundary_conditions=0) {
- return get_warp(p_warp,mode,interpolation,boundary_conditions).move_to(*this);
- }
- //! Warp image content by a warping field \newinstance
- template<typename t>
- CImg<T> get_warp(const CImg<t>& p_warp, const unsigned int mode=0,
- const unsigned int interpolation=1, const unsigned int boundary_conditions=0) const {
- if (is_empty() || !p_warp) return *this;
- if (mode && !is_sameXYZ(p_warp))
- throw CImgArgumentException(_cimg_instance
- "warp(): Instance and specified relative warping field (%u,%u,%u,%u,%p) "
- "have different XYZ dimensions.",
- cimg_instance,
- p_warp._width,p_warp._height,p_warp._depth,p_warp._spectrum,p_warp._data);
- CImg<T> res(p_warp._width,p_warp._height,p_warp._depth,_spectrum);
- if (p_warp._spectrum==1) { // 1D warping
- if (mode>=3) { // Forward-relative warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) res.set_linear_atX(*(ptrs++),x + (float)*(ptrs0++),y,z,c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) {
- const int X = x + (int)cimg::round(*(ptrs0++));
- if (X>=0 && X<width()) res(X,y,z,c) = *(ptrs++);
- }
- }
- } else if (mode==2) { // Forward-absolute warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) res.set_linear_atX(*(ptrs++),(float)*(ptrs0++),y,z,c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) {
- const int X = (int)cimg::round(*(ptrs0++));
- if (X>=0 && X<width()) res(X,y,z,c) = *(ptrs++);
- }
- }
- } else if (mode==1) { // Backward-relative warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float mx = cimg::mod(x - (float)*(ptrs0++),w2);
- *(ptrd++) = _cubic_atX_c(mx<width()?mx:w2 - mx - 1,y,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atX_pc(x - (float)*(ptrs0++),y,z,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atX_c(x - (float)*(ptrs0++),y,z,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = cubic_atX_c(x - (float)*(ptrs0++),y,z,c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float mx = cimg::mod(x - (float)*(ptrs0++),w2);
- *(ptrd++) = (T)_linear_atX(mx<width()?mx:w2 - mx - 1,y,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atX_p(x - (float)*(ptrs0++),y,z,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atX(x - (float)*(ptrs0++),y,z,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)linear_atX(x - (float)*(ptrs0++),y,z,c,(T)0);
- }
- }
- else // Nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const int mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,y,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),y,z,c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _atX(x - (int)cimg::round(*(ptrs0++)),y,z,c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = atX(x - (int)cimg::round(*(ptrs0++)),y,z,c,(T)0);
- }
- }
- }
- else { // Backward-absolute warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float mx = cimg::mod((float)*(ptrs0++),w2);
- *(ptrd++) = _cubic_atX_c(mx<width()?mx:w2 - mx - 1,0,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atX_pc((float)*(ptrs0++),0,0,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atX_c((float)*(ptrs0++),0,0,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = cubic_atX_c((float)*(ptrs0++),0,0,c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float mx = cimg::mod((float)*(ptrs0++),w2);
- *(ptrd++) = (T)_linear_atX(mx<width()?mx:w2 - mx - 1,0,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atX_p((float)*(ptrs0++),0,0,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atX((float)*(ptrs0++),0,0,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)linear_atX((float)*(ptrs0++),0,0,c,(T)0);
- }
- }
- else // Nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const int mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,0,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),0,0,c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _atX((int)cimg::round(*(ptrs0++)),0,0,c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- const t *ptrs0 = p_warp.data(0,y,z); T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = atX((int)cimg::round(*(ptrs0++)),0,0,c,(T)0);
- }
- }
- }
- } else if (p_warp._spectrum==2) { // 2D warping
- if (mode>=3) { // Forward-relative warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) res.set_linear_atXY(*(ptrs++),x + (float)*(ptrs0++),y + (float)*(ptrs1++),z,c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const int X = x + (int)cimg::round(*(ptrs0++)), Y = y + (int)cimg::round(*(ptrs1++));
- if (X>=0 && X<width() && Y>=0 && Y<height()) res(X,Y,z,c) = *(ptrs++);
- }
- }
- } else if (mode==2) { // Forward-absolute warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) res.set_linear_atXY(*(ptrs++),(float)*(ptrs0++),(float)*(ptrs1++),z,c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const int X = (int)cimg::round(*(ptrs0++)), Y = (int)cimg::round(*(ptrs1++));
- if (X>=0 && X<width() && Y>=0 && Y<height()) res(X,Y,z,c) = *(ptrs++);
- }
- }
- } else if (mode==1) { // Backward-relative warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod(x - (float)*(ptrs0++),w2),
- my = cimg::mod(y - (float)*(ptrs1++),h2);
- *(ptrd++) = _cubic_atXY_c(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXY_pc(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXY_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = cubic_atXY_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod(x - (float)*(ptrs0++),w2),
- my = cimg::mod(y - (float)*(ptrs1++),h2);
- *(ptrd++) = (T)_linear_atXY(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY_p(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)linear_atXY(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z,c,(T)0);
- }
- }
- else // Nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width(), h2 = 2*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const int
- mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2),
- my = cimg::mod(y - (int)cimg::round(*(ptrs1++)),h2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,z,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),
- cimg::mod(y - (int)cimg::round(*(ptrs1++)),height()),z,c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _atXY(x - (int)cimg::round(*(ptrs0++)),
- y - (int)cimg::round(*(ptrs1++)),z,c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = atXY(x - (int)cimg::round(*(ptrs0++)),
- y - (int)cimg::round(*(ptrs1++)),z,c,(T)0);
- }
- }
- } else { // Backward-absolute warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod((float)*(ptrs0++),w2),
- my = cimg::mod((float)*(ptrs1++),h2);
- *(ptrd++) = _cubic_atXY_c(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXY_pc((float)*(ptrs0++),(float)*(ptrs1++),0,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXY_c((float)*(ptrs0++),(float)*(ptrs1++),0,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = cubic_atXY_c((float)*(ptrs0++),(float)*(ptrs1++),0,c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod((float)*(ptrs0++),w2),
- my = cimg::mod((float)*(ptrs1++),h2);
- *(ptrd++) = (T)_linear_atXY(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY_p((float)*(ptrs0++),(float)*(ptrs1++),0,c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (T)linear_atXY((float)*(ptrs0++),(float)*(ptrs1++),0,c,(T)0);
- }
- }
- else // Nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width(), h2 = 2*height();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) {
- const int
- mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2),
- my = cimg::mod((int)cimg::round(*(ptrs1++)),h2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,my<height()?my:h2 - my - 1,0,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),
- cimg::mod((int)cimg::round(*(ptrs1++)),height()),0,c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = _atXY((int)cimg::round(*(ptrs0++)),
- (int)cimg::round(*(ptrs1++)),0,c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- 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);
- cimg_forX(res,x) *(ptrd++) = atXY((int)cimg::round(*(ptrs0++)),
- (int)cimg::round(*(ptrs1++)),0,c,(T)0);
- }
- }
- }
- } else { // 3D warping
- if (mode>=3) { // Forward-relative warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) res.set_linear_atXYZ(*(ptrs++),x + (float)*(ptrs0++),y + (float)*(ptrs1++),
- z + (float)*(ptrs2++),c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- 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);
- const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) {
- const int
- X = x + (int)cimg::round(*(ptrs0++)),
- Y = y + (int)cimg::round(*(ptrs1++)),
- Z = z + (int)cimg::round(*(ptrs2++));
- if (X>=0 && X<width() && Y>=0 && Y<height() && Z>=0 && Z<depth()) res(X,Y,Z,c) = *(ptrs++);
- }
- }
- } else if (mode==2) { // Forward-absolute warp
- res.fill((T)0);
- if (interpolation>=1) // Linear interpolation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) res.set_linear_atXYZ(*(ptrs++),(float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
- }
- else // Nearest-neighbor interpolation
- cimg_forYZC(res,y,z,c) {
- 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);
- const T *ptrs = data(0,y,z,c);
- cimg_forX(res,x) {
- const int
- X = (int)cimg::round(*(ptrs0++)),
- Y = (int)cimg::round(*(ptrs1++)),
- Z = (int)cimg::round(*(ptrs2++));
- if (X>=0 && X<width() && Y>=0 && Y<height() && Z>=0 && Z<depth()) res(X,Y,Z,c) = *(ptrs++);
- }
- }
- } else if (mode==1) { // Backward-relative warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod(x - (float)*(ptrs0++),w2),
- my = cimg::mod(y - (float)*(ptrs1++),h2),
- mz = cimg::mod(z - (float)*(ptrs2++),d2);
- *(ptrd++) = _cubic_atXYZ_c(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x)
- *(ptrd++) = _cubic_atXYZ_pc(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x)
- *(ptrd++) = _cubic_atXYZ_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x)
- *(ptrd++) = cubic_atXYZ_c(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod(x - (float)*(ptrs0++),w2),
- my = cimg::mod(y - (float)*(ptrs1++),h2),
- mz = cimg::mod(z - (float)*(ptrs2++),d2);
- *(ptrd++) = (T)_linear_atXYZ(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ_p(x - (float)*(ptrs0++),y - (float)*(ptrs1++),
- z - (float)*(ptrs2++),c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x)
- *(ptrd++) = (T)_linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x)
- *(ptrd++) = (T)linear_atXYZ(x - (float)*(ptrs0++),y - (float)*(ptrs1++),z - (float)*(ptrs2++),c,(T)0);
- }
- }
- else // Nearest neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const int
- mx = cimg::mod(x - (int)cimg::round(*(ptrs0++)),w2),
- my = cimg::mod(y - (int)cimg::round(*(ptrs1++)),h2),
- mz = cimg::mod(z - (int)cimg::round(*(ptrs2++)),d2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod(x - (int)cimg::round(*(ptrs0++)),width()),
- cimg::mod(y - (int)cimg::round(*(ptrs1++)),height()),
- cimg::mod(z - (int)cimg::round(*(ptrs2++)),depth()),c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _atXYZ(x - (int)cimg::round(*(ptrs0++)),
- y - (int)cimg::round(*(ptrs1++)),
- z - (int)cimg::round(*(ptrs2++)),c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = atXYZ(x - (int)cimg::round(*(ptrs0++)),
- y - (int)cimg::round(*(ptrs1++)),
- z - (int)cimg::round(*(ptrs2++)),c,(T)0);
- }
- }
- } else { // Backward-absolute warp
- if (interpolation==2) // Cubic interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod((float)*(ptrs0++),w2),
- my = cimg::mod((float)*(ptrs1++),h2),
- mz = cimg::mod((float)*(ptrs2++),d2);
- *(ptrd++) = _cubic_atXYZ_c(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXYZ_pc((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _cubic_atXYZ_c((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = cubic_atXYZ_c((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),
- c,(T)0);
- }
- }
- else if (interpolation==1) // Linear interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const float
- mx = cimg::mod((float)*(ptrs0++),w2),
- my = cimg::mod((float)*(ptrs1++),h2),
- mz = cimg::mod((float)*(ptrs2++),d2);
- *(ptrd++) = (T)_linear_atXYZ(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 :// Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ_p((float)*(ptrs0++),(float)*(ptrs1++),
- (float)*(ptrs2++),c);
- }
- break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)_linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),c);
- }
- break;
- default : // Dirichlet
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),1048576))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (T)linear_atXYZ((float)*(ptrs0++),(float)*(ptrs1++),(float)*(ptrs2++),
- c,(T)0);
- }
- }
- else // Nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) {
- const int
- mx = cimg::mod((int)cimg::round(*(ptrs0++)),w2),
- my = cimg::mod((int)cimg::round(*(ptrs1++)),h2),
- mz = cimg::mod((int)cimg::round(*(ptrs2++)),d2);
- *(ptrd++) = (*this)(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,c);
- }
- }
- } break;
- case 2 : // Periodic
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = (*this)(cimg::mod((int)cimg::round(*(ptrs0++)),width()),
- cimg::mod((int)cimg::round(*(ptrs1++)),height()),
- cimg::mod((int)cimg::round(*(ptrs2++)),depth()),c);
- }
- break;
- case 1 : // Neumann
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = _atXYZ((int)cimg::round(*(ptrs0++)),
- (int)cimg::round(*(ptrs1++)),
- (int)cimg::round(*(ptrs2++)),c);
- }
- break;
- default : // Dirichlet
- cimg_forYZC(res,y,z,c) {
- 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);
- T *ptrd = res.data(0,y,z,c);
- cimg_forX(res,x) *(ptrd++) = atXYZ((int)cimg::round(*(ptrs0++)),
- (int)cimg::round(*(ptrs1++)),
- (int)cimg::round(*(ptrs2++)),c,(T)0);
- }
- }
- }
- }
- return res;
- }
- //! Generate a 2D representation of a 3D image, with XY,XZ and YZ views.
- /**
- \param x0 X-coordinate of the projection point.
- \param y0 Y-coordinate of the projection point.
- \param z0 Z-coordinate of the projection point.
- **/
- CImg<T> get_projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) const {
- if (is_empty() || _depth<2) return +*this;
- const unsigned int
- _x0 = (x0>=_width)?_width - 1:x0,
- _y0 = (y0>=_height)?_height - 1:y0,
- _z0 = (z0>=_depth)?_depth - 1:z0;
- const CImg<T>
- img_xy = get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1),
- img_zy = get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1).permute_axes("xzyc").
- resize(_depth,_height,1,-100,-1),
- img_xz = get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1).resize(_width,_depth,1,-100,-1);
- return CImg<T>(_width + _depth,_height + _depth,1,_spectrum,cimg::min(img_xy.min(),img_zy.min(),img_xz.min())).
- draw_image(0,0,img_xy).draw_image(img_xy._width,0,img_zy).
- draw_image(0,img_xy._height,img_xz);
- }
- //! Construct a 2D representation of a 3D image, with XY,XZ and YZ views \inplace.
- CImg<T>& projections2d(const unsigned int x0, const unsigned int y0, const unsigned int z0) {
- if (_depth<2) return *this;
- return get_projections2d(x0,y0,z0).move_to(*this);
- }
- //! Crop image region.
- /**
- \param x0 = X-coordinate of the upper-left crop rectangle corner.
- \param y0 = Y-coordinate of the upper-left crop rectangle corner.
- \param z0 = Z-coordinate of the upper-left crop rectangle corner.
- \param c0 = C-coordinate of the upper-left crop rectangle corner.
- \param x1 = X-coordinate of the lower-right crop rectangle corner.
- \param y1 = Y-coordinate of the lower-right crop rectangle corner.
- \param z1 = Z-coordinate of the lower-right crop rectangle corner.
- \param c1 = C-coordinate of the lower-right crop rectangle corner.
- \param boundary_conditions = Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- **/
- CImg<T>& crop(const int x0, const int y0, const int z0, const int c0,
- const int x1, const int y1, const int z1, const int c1,
- const unsigned int boundary_conditions=0) {
- return get_crop(x0,y0,z0,c0,x1,y1,z1,c1,boundary_conditions).move_to(*this);
- }
- //! Crop image region \newinstance.
- CImg<T> get_crop(const int x0, const int y0, const int z0, const int c0,
- const int x1, const int y1, const int z1, const int c1,
- const unsigned int boundary_conditions=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "crop(): Empty instance.",
- cimg_instance);
- const int
- nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
- ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0,
- nz0 = z0<z1?z0:z1, nz1 = z0^z1^nz0,
- nc0 = c0<c1?c0:c1, nc1 = c0^c1^nc0;
- const unsigned int
- _boundary_conditions = nx0>=0 && nx1<width() &&
- ny0>=0 && ny1<height() &&
- nz0>=0 && nz1<depth() &&
- nc0>=0 && nc1<spectrum()?0:boundary_conditions;
- CImg<T> res(1U + nx1 - nx0,1U + ny1 - ny0,1U + nz1 - nz0,1U + nc1 - nc0);
- if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height() || nz0<0 || nz1>=depth() || nc0<0 || nc1>=spectrum())
- switch (_boundary_conditions) {
- case 3 : { // Mirror
- const int w2 = 2*width(), h2 = 2*height(), d2 = 2*depth(), s2 = 2*spectrum();
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_depth*_spectrum>=4))
- cimg_forXYZC(res,x,y,z,c) {
- const int
- mx = cimg::mod(nx0 + x,w2),
- my = cimg::mod(ny0 + y,h2),
- mz = cimg::mod(nz0 + z,d2),
- mc = cimg::mod(nc0 + c,s2);
- res(x,y,z,c) = (*this)(mx<width()?mx:w2 - mx - 1,
- my<height()?my:h2 - my - 1,
- mz<depth()?mz:d2 - mz - 1,
- mc<spectrum()?mc:s2 - mc - 1);
- }
- } break;
- case 2 : { // Periodic
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_depth*_spectrum>=4))
- cimg_forXYZC(res,x,y,z,c) {
- res(x,y,z,c) = (*this)(cimg::mod(nx0 + x,width()),cimg::mod(ny0 + y,height()),
- cimg::mod(nz0 + z,depth()),cimg::mod(nc0 + c,spectrum()));
- }
- } break;
- case 1 : // Neumann
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_depth*_spectrum>=4))
- cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _atXYZC(nx0 + x,ny0 + y,nz0 + z,nc0 + c);
- break;
- default : // Dirichlet
- res.fill((T)0).draw_image(-nx0,-ny0,-nz0,-nc0,*this);
- }
- else res.draw_image(-nx0,-ny0,-nz0,-nc0,*this);
- return res;
- }
- //! Crop image region \overloading.
- CImg<T>& crop(const int x0, const int y0, const int z0,
- const int x1, const int y1, const int z1,
- const unsigned int boundary_conditions=0) {
- return crop(x0,y0,z0,0,x1,y1,z1,_spectrum - 1,boundary_conditions);
- }
- //! Crop image region \newinstance.
- CImg<T> get_crop(const int x0, const int y0, const int z0,
- const int x1, const int y1, const int z1,
- const unsigned int boundary_conditions=0) const {
- return get_crop(x0,y0,z0,0,x1,y1,z1,_spectrum - 1,boundary_conditions);
- }
- //! Crop image region \overloading.
- CImg<T>& crop(const int x0, const int y0,
- const int x1, const int y1,
- const unsigned int boundary_conditions=0) {
- return crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions);
- }
- //! Crop image region \newinstance.
- CImg<T> get_crop(const int x0, const int y0,
- const int x1, const int y1,
- const unsigned int boundary_conditions=0) const {
- return get_crop(x0,y0,0,0,x1,y1,_depth - 1,_spectrum - 1,boundary_conditions);
- }
- //! Crop image region \overloading.
- CImg<T>& crop(const int x0, const int x1, const unsigned int boundary_conditions=0) {
- return crop(x0,0,0,0,x1,_height - 1,_depth - 1,_spectrum - 1,boundary_conditions);
- }
- //! Crop image region \newinstance.
- CImg<T> get_crop(const int x0, const int x1, const unsigned int boundary_conditions=0) const {
- return get_crop(x0,0,0,0,x1,_height - 1,_depth - 1,_spectrum - 1,boundary_conditions);
- }
- //! Autocrop image region, regarding the specified background value.
- CImg<T>& autocrop(const T& value, const char *const axes="czyx") {
- if (is_empty()) return *this;
- for (const char *s = axes; *s; ++s) {
- const char axis = cimg::lowercase(*s);
- const CImg<intT> coords = _autocrop(value,axis);
- if (coords[0]==-1 && coords[1]==-1) return assign(); // Image has only 'value' pixels
- else switch (axis) {
- case 'x' : {
- const int x0 = coords[0], x1 = coords[1];
- if (x0>=0 && x1>=0) crop(x0,x1);
- } break;
- case 'y' : {
- const int y0 = coords[0], y1 = coords[1];
- if (y0>=0 && y1>=0) crop(0,y0,_width - 1,y1);
- } break;
- case 'z' : {
- const int z0 = coords[0], z1 = coords[1];
- if (z0>=0 && z1>=0) crop(0,0,z0,_width - 1,_height - 1,z1);
- } break;
- default : {
- const int c0 = coords[0], c1 = coords[1];
- if (c0>=0 && c1>=0) crop(0,0,0,c0,_width - 1,_height - 1,_depth - 1,c1);
- }
- }
- }
- return *this;
- }
- //! Autocrop image region, regarding the specified background value \newinstance.
- CImg<T> get_autocrop(const T& value, const char *const axes="czyx") const {
- return (+*this).autocrop(value,axes);
- }
- //! Autocrop image region, regarding the specified background color.
- /**
- \param color Color used for the crop. If \c 0, color is guessed.
- \param axes Axes used for the crop.
- **/
- CImg<T>& autocrop(const T *const color=0, const char *const axes="zyx") {
- if (is_empty()) return *this;
- if (!color) { // Guess color
- const CImg<T> col1 = get_vector_at(0,0,0);
- const unsigned int w = _width, h = _height, d = _depth, s = _spectrum;
- autocrop(col1,axes);
- if (_width==w && _height==h && _depth==d && _spectrum==s) {
- const CImg<T> col2 = get_vector_at(w - 1,h - 1,d - 1);
- autocrop(col2,axes);
- }
- return *this;
- }
- for (const char *s = axes; *s; ++s) {
- const char axis = cimg::lowercase(*s);
- switch (axis) {
- case 'x' : {
- int x0 = width(), x1 = -1;
- cimg_forC(*this,c) {
- const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'x');
- const int nx0 = coords[0], nx1 = coords[1];
- if (nx0>=0 && nx1>=0) { x0 = std::min(x0,nx0); x1 = std::max(x1,nx1); }
- }
- if (x0==width() && x1==-1) return assign(); else crop(x0,x1);
- } break;
- case 'y' : {
- int y0 = height(), y1 = -1;
- cimg_forC(*this,c) {
- const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'y');
- const int ny0 = coords[0], ny1 = coords[1];
- if (ny0>=0 && ny1>=0) { y0 = std::min(y0,ny0); y1 = std::max(y1,ny1); }
- }
- if (y0==height() && y1==-1) return assign(); else crop(0,y0,_width - 1,y1);
- } break;
- default : {
- int z0 = depth(), z1 = -1;
- cimg_forC(*this,c) {
- const CImg<intT> coords = get_shared_channel(c)._autocrop(color[c],'z');
- const int nz0 = coords[0], nz1 = coords[1];
- if (nz0>=0 && nz1>=0) { z0 = std::min(z0,nz0); z1 = std::max(z1,nz1); }
- }
- if (z0==depth() && z1==-1) return assign(); else crop(0,0,z0,_width - 1,_height - 1,z1);
- }
- }
- }
- return *this;
- }
- //! Autocrop image region, regarding the specified background color \newinstance.
- CImg<T> get_autocrop(const T *const color=0, const char *const axes="zyx") const {
- return (+*this).autocrop(color,axes);
- }
- CImg<intT> _autocrop(const T& value, const char axis) const {
- CImg<intT> res;
- switch (cimg::lowercase(axis)) {
- case 'x' : {
- int x0 = -1, x1 = -1;
- cimg_forX(*this,x) cimg_forYZC(*this,y,z,c)
- if ((*this)(x,y,z,c)!=value) { x0 = x; x = width(); y = height(); z = depth(); c = spectrum(); }
- if (x0>=0) {
- for (int x = width() - 1; x>=0; --x) cimg_forYZC(*this,y,z,c)
- if ((*this)(x,y,z,c)!=value) { x1 = x; x = 0; y = height(); z = depth(); c = spectrum(); }
- }
- res = CImg<intT>::vector(x0,x1);
- } break;
- case 'y' : {
- int y0 = -1, y1 = -1;
- cimg_forY(*this,y) cimg_forXZC(*this,x,z,c)
- if ((*this)(x,y,z,c)!=value) { y0 = y; x = width(); y = height(); z = depth(); c = spectrum(); }
- if (y0>=0) {
- for (int y = height() - 1; y>=0; --y) cimg_forXZC(*this,x,z,c)
- if ((*this)(x,y,z,c)!=value) { y1 = y; x = width(); y = 0; z = depth(); c = spectrum(); }
- }
- res = CImg<intT>::vector(y0,y1);
- } break;
- case 'z' : {
- int z0 = -1, z1 = -1;
- cimg_forZ(*this,z) cimg_forXYC(*this,x,y,c)
- if ((*this)(x,y,z,c)!=value) { z0 = z; x = width(); y = height(); z = depth(); c = spectrum(); }
- if (z0>=0) {
- for (int z = depth() - 1; z>=0; --z) cimg_forXYC(*this,x,y,c)
- if ((*this)(x,y,z,c)!=value) { z1 = z; x = width(); y = height(); z = 0; c = spectrum(); }
- }
- res = CImg<intT>::vector(z0,z1);
- } break;
- default : {
- int c0 = -1, c1 = -1;
- cimg_forC(*this,c) cimg_forXYZ(*this,x,y,z)
- if ((*this)(x,y,z,c)!=value) { c0 = c; x = width(); y = height(); z = depth(); c = spectrum(); }
- if (c0>=0) {
- for (int c = spectrum() - 1; c>=0; --c) cimg_forXYZ(*this,x,y,z)
- if ((*this)(x,y,z,c)!=value) { c1 = c; x = width(); y = height(); z = depth(); c = 0; }
- }
- res = CImg<intT>::vector(c0,c1);
- }
- }
- return res;
- }
- //! Return specified image column.
- /**
- \param x0 Image column.
- **/
- CImg<T> get_column(const int x0) const {
- return get_columns(x0,x0);
- }
- //! Return specified image column \inplace.
- CImg<T>& column(const int x0) {
- return columns(x0,x0);
- }
- //! Return specified range of image columns.
- /**
- \param x0 Starting image column.
- \param x1 Ending image column.
- **/
- CImg<T>& columns(const int x0, const int x1) {
- return get_columns(x0,x1).move_to(*this);
- }
- //! Return specified range of image columns \inplace.
- CImg<T> get_columns(const int x0, const int x1) const {
- return get_crop(x0,0,0,0,x1,height() - 1,depth() - 1,spectrum() - 1);
- }
- //! Return specified image row.
- CImg<T> get_row(const int y0) const {
- return get_rows(y0,y0);
- }
- //! Return specified image row \inplace.
- /**
- \param y0 Image row.
- **/
- CImg<T>& row(const int y0) {
- return rows(y0,y0);
- }
- //! Return specified range of image rows.
- /**
- \param y0 Starting image row.
- \param y1 Ending image row.
- **/
- CImg<T> get_rows(const int y0, const int y1) const {
- return get_crop(0,y0,0,0,width() - 1,y1,depth() - 1,spectrum() - 1);
- }
- //! Return specified range of image rows \inplace.
- CImg<T>& rows(const int y0, const int y1) {
- return get_rows(y0,y1).move_to(*this);
- }
- //! Return specified image slice.
- /**
- \param z0 Image slice.
- **/
- CImg<T> get_slice(const int z0) const {
- return get_slices(z0,z0);
- }
- //! Return specified image slice \inplace.
- CImg<T>& slice(const int z0) {
- return slices(z0,z0);
- }
- //! Return specified range of image slices.
- /**
- \param z0 Starting image slice.
- \param z1 Ending image slice.
- **/
- CImg<T> get_slices(const int z0, const int z1) const {
- return get_crop(0,0,z0,0,width() - 1,height() - 1,z1,spectrum() - 1);
- }
- //! Return specified range of image slices \inplace.
- CImg<T>& slices(const int z0, const int z1) {
- return get_slices(z0,z1).move_to(*this);
- }
- //! Return specified image channel.
- /**
- \param c0 Image channel.
- **/
- CImg<T> get_channel(const int c0) const {
- return get_channels(c0,c0);
- }
- //! Return specified image channel \inplace.
- CImg<T>& channel(const int c0) {
- return channels(c0,c0);
- }
- //! Return specified range of image channels.
- /**
- \param c0 Starting image channel.
- \param c1 Ending image channel.
- **/
- CImg<T> get_channels(const int c0, const int c1) const {
- return get_crop(0,0,0,c0,width() - 1,height() - 1,depth() - 1,c1);
- }
- //! Return specified range of image channels \inplace.
- CImg<T>& channels(const int c0, const int c1) {
- return get_channels(c0,c1).move_to(*this);
- }
- //! Return stream line of a 2D or 3D vector field.
- CImg<floatT> get_streamline(const float x, const float y, const float z,
- const float L=256, const float dl=0.1f,
- const unsigned int interpolation_type=2, const bool is_backward_tracking=false,
- const bool is_oriented_only=false) const {
- if (_spectrum!=2 && _spectrum!=3)
- throw CImgInstanceException(_cimg_instance
- "streamline(): Instance is not a 2D or 3D vector field.",
- cimg_instance);
- if (_spectrum==2) {
- if (is_oriented_only) {
- typename CImg<T>::_functor4d_streamline2d_oriented func(*this);
- return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true,
- 0,0,0,_width - 1.f,_height - 1.f,0.f);
- } else {
- typename CImg<T>::_functor4d_streamline2d_directed func(*this);
- return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false,
- 0,0,0,_width - 1.f,_height - 1.f,0.f);
- }
- }
- if (is_oriented_only) {
- typename CImg<T>::_functor4d_streamline3d_oriented func(*this);
- return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,true,
- 0,0,0,_width - 1.f,_height - 1.f,_depth - 1.f);
- }
- typename CImg<T>::_functor4d_streamline3d_directed func(*this);
- return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,false,
- 0,0,0,_width - 1.f,_height - 1.f,_depth - 1.f);
- }
- //! Return stream line of a 3D vector field.
- /**
- \param func Vector field function.
- \param x X-coordinate of the starting point of the streamline.
- \param y Y-coordinate of the starting point of the streamline.
- \param z Z-coordinate of the starting point of the streamline.
- \param L Streamline length.
- \param dl Streamline length increment.
- \param interpolation_type Type of interpolation.
- Can be <tt>{ 0=nearest int | 1=linear | 2=2nd-order RK | 3=4th-order RK. }</tt>.
- \param is_backward_tracking Tells if the streamline is estimated forward or backward.
- \param is_oriented_only Tells if the direction of the vectors must be ignored.
- \param x0 X-coordinate of the first bounding-box vertex.
- \param y0 Y-coordinate of the first bounding-box vertex.
- \param z0 Z-coordinate of the first bounding-box vertex.
- \param x1 X-coordinate of the second bounding-box vertex.
- \param y1 Y-coordinate of the second bounding-box vertex.
- \param z1 Z-coordinate of the second bounding-box vertex.
- **/
- template<typename tfunc>
- static CImg<floatT> streamline(const tfunc& func,
- const float x, const float y, const float z,
- const float L=256, const float dl=0.1f,
- const unsigned int interpolation_type=2, const bool is_backward_tracking=false,
- const bool is_oriented_only=false,
- const float x0=0, const float y0=0, const float z0=0,
- const float x1=0, const float y1=0, const float z1=0) {
- if (dl<=0)
- throw CImgArgumentException("CImg<%s>::streamline(): Invalid specified integration length %g "
- "(should be >0).",
- pixel_type(),
- dl);
- const bool is_bounded = (x0!=x1 || y0!=y1 || z0!=z1);
- if (L<=0 || (is_bounded && (x<x0 || x>x1 || y<y0 || y>y1 || z<z0 || z>z1))) return CImg<floatT>();
- const unsigned int size_L = (unsigned int)cimg::round(L/dl + 1);
- CImg<floatT> coordinates(size_L,3);
- const float dl2 = dl/2;
- float
- *ptr_x = coordinates.data(0,0),
- *ptr_y = coordinates.data(0,1),
- *ptr_z = coordinates.data(0,2),
- pu = (float)(dl*func(x,y,z,0)),
- pv = (float)(dl*func(x,y,z,1)),
- pw = (float)(dl*func(x,y,z,2)),
- X = x, Y = y, Z = z;
- switch (interpolation_type) {
- case 0 : { // Nearest integer interpolation
- cimg_forX(coordinates,l) {
- *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
- const int
- xi = (int)(X>0?X + 0.5f:X - 0.5f),
- yi = (int)(Y>0?Y + 0.5f:Y - 0.5f),
- zi = (int)(Z>0?Z + 0.5f:Z - 0.5f);
- float
- u = (float)(dl*func((float)xi,(float)yi,(float)zi,0)),
- v = (float)(dl*func((float)xi,(float)yi,(float)zi,1)),
- w = (float)(dl*func((float)xi,(float)yi,(float)zi,2));
- if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
- if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
- if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
- }
- } break;
- case 1 : { // First-order interpolation
- cimg_forX(coordinates,l) {
- *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
- float
- u = (float)(dl*func(X,Y,Z,0)),
- v = (float)(dl*func(X,Y,Z,1)),
- w = (float)(dl*func(X,Y,Z,2));
- if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
- if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
- if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
- }
- } break;
- case 2 : { // Second order interpolation
- cimg_forX(coordinates,l) {
- *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
- float
- u0 = (float)(dl2*func(X,Y,Z,0)),
- v0 = (float)(dl2*func(X,Y,Z,1)),
- w0 = (float)(dl2*func(X,Y,Z,2));
- if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; }
- float
- u = (float)(dl*func(X + u0,Y + v0,Z + w0,0)),
- v = (float)(dl*func(X + u0,Y + v0,Z + w0,1)),
- w = (float)(dl*func(X + u0,Y + v0,Z + w0,2));
- if (is_oriented_only && u*pu + v*pv + w*pw<0) { u = -u; v = -v; w = -w; }
- if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
- if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
- }
- } break;
- default : { // Fourth order interpolation
- cimg_forX(coordinates,k) {
- *(ptr_x++) = X; *(ptr_y++) = Y; *(ptr_z++) = Z;
- float
- u0 = (float)(dl2*func(X,Y,Z,0)),
- v0 = (float)(dl2*func(X,Y,Z,1)),
- w0 = (float)(dl2*func(X,Y,Z,2));
- if (is_oriented_only && u0*pu + v0*pv + w0*pw<0) { u0 = -u0; v0 = -v0; w0 = -w0; }
- float
- u1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,0)),
- v1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,1)),
- w1 = (float)(dl2*func(X + u0,Y + v0,Z + w0,2));
- if (is_oriented_only && u1*pu + v1*pv + w1*pw<0) { u1 = -u1; v1 = -v1; w1 = -w1; }
- float
- u2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,0)),
- v2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,1)),
- w2 = (float)(dl2*func(X + u1,Y + v1,Z + w1,2));
- if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u2 = -u2; v2 = -v2; w2 = -w2; }
- float
- u3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,0)),
- v3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,1)),
- w3 = (float)(dl2*func(X + u2,Y + v2,Z + w2,2));
- if (is_oriented_only && u2*pu + v2*pv + w2*pw<0) { u3 = -u3; v3 = -v3; w3 = -w3; }
- const float
- u = (u0 + u3)/3 + (u1 + u2)/1.5f,
- v = (v0 + v3)/3 + (v1 + v2)/1.5f,
- w = (w0 + w3)/3 + (w1 + w2)/1.5f;
- if (is_backward_tracking) { X-=(pu=u); Y-=(pv=v); Z-=(pw=w); } else { X+=(pu=u); Y+=(pv=v); Z+=(pw=w); }
- if (is_bounded && (X<x0 || X>x1 || Y<y0 || Y>y1 || Z<z0 || Z>z1)) break;
- }
- }
- }
- if (ptr_x!=coordinates.data(0,1)) coordinates.resize((int)(ptr_x-coordinates.data()),3,1,1,0);
- return coordinates;
- }
- //! Return stream line of a 3D vector field \overloading.
- static CImg<floatT> streamline(const char *const expression,
- const float x, const float y, const float z,
- const float L=256, const float dl=0.1f,
- const unsigned int interpolation_type=2, const bool is_backward_tracking=true,
- const bool is_oriented_only=false,
- const float x0=0, const float y0=0, const float z0=0,
- const float x1=0, const float y1=0, const float z1=0) {
- _functor4d_streamline_expr func(expression);
- return streamline(func,x,y,z,L,dl,interpolation_type,is_backward_tracking,is_oriented_only,x0,y0,z0,x1,y1,z1);
- }
- struct _functor4d_streamline2d_directed {
- const CImg<T>& ref;
- _functor4d_streamline2d_directed(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- return c<2?(float)ref._linear_atXY(x,y,(int)z,c):0;
- }
- };
- struct _functor4d_streamline3d_directed {
- const CImg<T>& ref;
- _functor4d_streamline3d_directed(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- return (float)ref._linear_atXYZ(x,y,z,c);
- }
- };
- struct _functor4d_streamline2d_oriented {
- const CImg<T>& ref;
- CImg<floatT> *pI;
- _functor4d_streamline2d_oriented(const CImg<T>& pref):ref(pref),pI(0) { pI = new CImg<floatT>(2,2,1,2); }
- ~_functor4d_streamline2d_oriented() { delete pI; }
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- #define _cimg_vecalign2d(i,j) \
- 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); }
- int
- xi = (int)x - (x>=0?0:1), nxi = xi + 1,
- yi = (int)y - (y>=0?0:1), nyi = yi + 1,
- zi = (int)z;
- const float
- dx = x - xi,
- dy = y - yi;
- if (c==0) {
- CImg<floatT>& I = *pI;
- if (xi<0) xi = 0;
- if (nxi<0) nxi = 0;
- if (xi>=ref.width()) xi = ref.width() - 1;
- if (nxi>=ref.width()) nxi = ref.width() - 1;
- if (yi<0) yi = 0;
- if (nyi<0) nyi = 0;
- if (yi>=ref.height()) yi = ref.height() - 1;
- if (nyi>=ref.height()) nyi = ref.height() - 1;
- I(0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,1) = (float)ref(xi,yi,zi,1);
- I(1,0,0) = (float)ref(nxi,yi,zi,0); I(1,0,1) = (float)ref(nxi,yi,zi,1);
- I(1,1,0) = (float)ref(nxi,nyi,zi,0); I(1,1,1) = (float)ref(nxi,nyi,zi,1);
- I(0,1,0) = (float)ref(xi,nyi,zi,0); I(0,1,1) = (float)ref(xi,nyi,zi,1);
- _cimg_vecalign2d(1,0); _cimg_vecalign2d(1,1); _cimg_vecalign2d(0,1);
- }
- return c<2?(float)pI->_linear_atXY(dx,dy,0,c):0;
- }
- };
- struct _functor4d_streamline3d_oriented {
- const CImg<T>& ref;
- CImg<floatT> *pI;
- _functor4d_streamline3d_oriented(const CImg<T>& pref):ref(pref),pI(0) { pI = new CImg<floatT>(2,2,2,3); }
- ~_functor4d_streamline3d_oriented() { delete pI; }
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- #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) { \
- 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); }
- int
- xi = (int)x - (x>=0?0:1), nxi = xi + 1,
- yi = (int)y - (y>=0?0:1), nyi = yi + 1,
- zi = (int)z - (z>=0?0:1), nzi = zi + 1;
- const float
- dx = x - xi,
- dy = y - yi,
- dz = z - zi;
- if (c==0) {
- CImg<floatT>& I = *pI;
- if (xi<0) xi = 0;
- if (nxi<0) nxi = 0;
- if (xi>=ref.width()) xi = ref.width() - 1;
- if (nxi>=ref.width()) nxi = ref.width() - 1;
- if (yi<0) yi = 0;
- if (nyi<0) nyi = 0;
- if (yi>=ref.height()) yi = ref.height() - 1;
- if (nyi>=ref.height()) nyi = ref.height() - 1;
- if (zi<0) zi = 0;
- if (nzi<0) nzi = 0;
- if (zi>=ref.depth()) zi = ref.depth() - 1;
- if (nzi>=ref.depth()) nzi = ref.depth() - 1;
- I(0,0,0,0) = (float)ref(xi,yi,zi,0); I(0,0,0,1) = (float)ref(xi,yi,zi,1);
- I(0,0,0,2) = (float)ref(xi,yi,zi,2); I(1,0,0,0) = (float)ref(nxi,yi,zi,0);
- I(1,0,0,1) = (float)ref(nxi,yi,zi,1); I(1,0,0,2) = (float)ref(nxi,yi,zi,2);
- I(1,1,0,0) = (float)ref(nxi,nyi,zi,0); I(1,1,0,1) = (float)ref(nxi,nyi,zi,1);
- I(1,1,0,2) = (float)ref(nxi,nyi,zi,2); I(0,1,0,0) = (float)ref(xi,nyi,zi,0);
- I(0,1,0,1) = (float)ref(xi,nyi,zi,1); I(0,1,0,2) = (float)ref(xi,nyi,zi,2);
- I(0,0,1,0) = (float)ref(xi,yi,nzi,0); I(0,0,1,1) = (float)ref(xi,yi,nzi,1);
- I(0,0,1,2) = (float)ref(xi,yi,nzi,2); I(1,0,1,0) = (float)ref(nxi,yi,nzi,0);
- I(1,0,1,1) = (float)ref(nxi,yi,nzi,1); I(1,0,1,2) = (float)ref(nxi,yi,nzi,2);
- I(1,1,1,0) = (float)ref(nxi,nyi,nzi,0); I(1,1,1,1) = (float)ref(nxi,nyi,nzi,1);
- I(1,1,1,2) = (float)ref(nxi,nyi,nzi,2); I(0,1,1,0) = (float)ref(xi,nyi,nzi,0);
- I(0,1,1,1) = (float)ref(xi,nyi,nzi,1); I(0,1,1,2) = (float)ref(xi,nyi,nzi,2);
- _cimg_vecalign3d(1,0,0); _cimg_vecalign3d(1,1,0); _cimg_vecalign3d(0,1,0);
- _cimg_vecalign3d(0,0,1); _cimg_vecalign3d(1,0,1); _cimg_vecalign3d(1,1,1); _cimg_vecalign3d(0,1,1);
- }
- return (float)pI->_linear_atXYZ(dx,dy,dz,c);
- }
- };
- struct _functor4d_streamline_expr {
- _cimg_math_parser *mp;
- ~_functor4d_streamline_expr() { mp->end(); delete mp; }
- _functor4d_streamline_expr(const char *const expr):mp(0) {
- mp = new _cimg_math_parser(expr,"streamline",CImg<T>::const_empty(),0);
- }
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- return (float)(*mp)(x,y,z,c);
- }
- };
- //! Return a shared-memory image referencing a range of pixels of the image instance.
- /**
- \param x0 X-coordinate of the starting pixel.
- \param x1 X-coordinate of the ending pixel.
- \param y0 Y-coordinate.
- \param z0 Z-coordinate.
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_points(const unsigned int x0, const unsigned int x1,
- const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) {
- const ulongT
- beg = (ulongT)offset(x0,y0,z0,c0),
- end = (ulongT)offset(x1,y0,z0,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).",
- cimg_instance,
- x0,x1,y0,z0,c0);
- return CImg<T>(_data + beg,x1 - x0 + 1,1,1,1,true);
- }
- //! Return a shared-memory image referencing a range of pixels of the image instance \const.
- const CImg<T> get_shared_points(const unsigned int x0, const unsigned int x1,
- const unsigned int y0=0, const unsigned int z0=0, const unsigned int c0=0) const {
- const ulongT
- beg = (ulongT)offset(x0,y0,z0,c0),
- end = (ulongT)offset(x1,y0,z0,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_points(): Invalid request of a shared-memory subset (%u->%u,%u,%u,%u).",
- cimg_instance,
- x0,x1,y0,z0,c0);
- return CImg<T>(_data + beg,x1 - x0 + 1,1,1,1,true);
- }
- //! Return a shared-memory image referencing a range of rows of the image instance.
- /**
- \param y0 Y-coordinate of the starting row.
- \param y1 Y-coordinate of the ending row.
- \param z0 Z-coordinate.
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
- const unsigned int z0=0, const unsigned int c0=0) {
- const ulongT
- beg = (ulongT)offset(0,y0,z0,c0),
- end = (ulongT)offset(0,y1,z0,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_rows(): Invalid request of a shared-memory subset "
- "(0->%u,%u->%u,%u,%u).",
- cimg_instance,
- _width - 1,y0,y1,z0,c0);
- return CImg<T>(_data + beg,_width,y1 - y0 + 1,1,1,true);
- }
- //! Return a shared-memory image referencing a range of rows of the image instance \const.
- const CImg<T> get_shared_rows(const unsigned int y0, const unsigned int y1,
- const unsigned int z0=0, const unsigned int c0=0) const {
- const ulongT
- beg = (ulongT)offset(0,y0,z0,c0),
- end = (ulongT)offset(0,y1,z0,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_rows(): Invalid request of a shared-memory subset "
- "(0->%u,%u->%u,%u,%u).",
- cimg_instance,
- _width - 1,y0,y1,z0,c0);
- return CImg<T>(_data + beg,_width,y1 - y0 + 1,1,1,true);
- }
- //! Return a shared-memory image referencing one row of the image instance.
- /**
- \param y0 Y-coordinate.
- \param z0 Z-coordinate.
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) {
- return get_shared_rows(y0,y0,z0,c0);
- }
- //! Return a shared-memory image referencing one row of the image instance \const.
- const CImg<T> get_shared_row(const unsigned int y0, const unsigned int z0=0, const unsigned int c0=0) const {
- return get_shared_rows(y0,y0,z0,c0);
- }
- //! Return a shared memory image referencing a range of slices of the image instance.
- /**
- \param z0 Z-coordinate of the starting slice.
- \param z1 Z-coordinate of the ending slice.
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) {
- const ulongT
- beg = (ulongT)offset(0,0,z0,c0),
- end = (ulongT)offset(0,0,z1,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_slices(): Invalid request of a shared-memory subset "
- "(0->%u,0->%u,%u->%u,%u).",
- cimg_instance,
- _width - 1,_height - 1,z0,z1,c0);
- return CImg<T>(_data + beg,_width,_height,z1 - z0 + 1,1,true);
- }
- //! Return a shared memory image referencing a range of slices of the image instance \const.
- const CImg<T> get_shared_slices(const unsigned int z0, const unsigned int z1, const unsigned int c0=0) const {
- const ulongT
- beg = (ulongT)offset(0,0,z0,c0),
- end = (ulongT)offset(0,0,z1,c0);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_slices(): Invalid request of a shared-memory subset "
- "(0->%u,0->%u,%u->%u,%u).",
- cimg_instance,
- _width - 1,_height - 1,z0,z1,c0);
- return CImg<T>(_data + beg,_width,_height,z1 - z0 + 1,1,true);
- }
- //! Return a shared-memory image referencing one slice of the image instance.
- /**
- \param z0 Z-coordinate.
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) {
- return get_shared_slices(z0,z0,c0);
- }
- //! Return a shared-memory image referencing one slice of the image instance \const.
- const CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) const {
- return get_shared_slices(z0,z0,c0);
- }
- //! Return a shared-memory image referencing a range of channels of the image instance.
- /**
- \param c0 C-coordinate of the starting channel.
- \param c1 C-coordinate of the ending channel.
- **/
- CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) {
- const ulongT
- beg = (ulongT)offset(0,0,0,c0),
- end = (ulongT)offset(0,0,0,c1);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_channels(): Invalid request of a shared-memory subset "
- "(0->%u,0->%u,0->%u,%u->%u).",
- cimg_instance,
- _width - 1,_height - 1,_depth - 1,c0,c1);
- return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);
- }
- //! Return a shared-memory image referencing a range of channels of the image instance \const.
- const CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) const {
- const ulongT
- beg = (ulongT)offset(0,0,0,c0),
- end = (ulongT)offset(0,0,0,c1);
- if (beg>end || beg>=size() || end>=size())
- throw CImgArgumentException(_cimg_instance
- "get_shared_channels(): Invalid request of a shared-memory subset "
- "(0->%u,0->%u,0->%u,%u->%u).",
- cimg_instance,
- _width - 1,_height - 1,_depth - 1,c0,c1);
- return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);
- }
- //! Return a shared-memory image referencing one channel of the image instance.
- /**
- \param c0 C-coordinate.
- **/
- CImg<T> get_shared_channel(const unsigned int c0) {
- return get_shared_channels(c0,c0);
- }
- //! Return a shared-memory image referencing one channel of the image instance \const.
- const CImg<T> get_shared_channel(const unsigned int c0) const {
- return get_shared_channels(c0,c0);
- }
- //! Return a shared-memory version of the image instance.
- CImg<T> get_shared() {
- return CImg<T>(_data,_width,_height,_depth,_spectrum,true);
- }
- //! Return a shared-memory version of the image instance \const.
- const CImg<T> get_shared() const {
- return CImg<T>(_data,_width,_height,_depth,_spectrum,true);
- }
- //! Split image into a list along specified axis.
- /**
- \param axis Splitting axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param nb Number of split parts.
- \note
- - If \c nb==0, instance image is split into blocs of equal values along the specified axis.
- - If \c nb<=0, instance image is split into blocs of -\c nb pixel wide.
- - If \c nb>0, instance image is split into \c nb blocs.
- **/
- CImgList<T> get_split(const char axis, const int nb=-1) const {
- CImgList<T> res;
- if (is_empty()) return res;
- const char _axis = cimg::lowercase(axis);
- if (nb<0) { // Split by bloc size
- const unsigned int dp = (unsigned int)(nb?-nb:1);
- switch (_axis) {
- case 'x': {
- if (_width>dp) {
- res.assign(_width/dp + (_width%dp?1:0),1,1);
- const unsigned int pe = _width - dp;
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
- _height*_depth*_spectrum>=128))
- for (int p = 0; p<(int)pe; p+=dp)
- get_crop(p,0,0,0,p + dp - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res[p/dp]);
- get_crop((res._width - 1)*dp,0,0,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
- } else res.assign(*this);
- } break;
- case 'y': {
- if (_height>dp) {
- res.assign(_height/dp + (_height%dp?1:0),1,1);
- const unsigned int pe = _height - dp;
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
- _width*_depth*_spectrum>=128))
- for (int p = 0; p<(int)pe; p+=dp)
- get_crop(0,p,0,0,_width - 1,p + dp - 1,_depth - 1,_spectrum - 1).move_to(res[p/dp]);
- get_crop(0,(res._width - 1)*dp,0,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
- } else res.assign(*this);
- } break;
- case 'z': {
- if (_depth>dp) {
- res.assign(_depth/dp + (_depth%dp?1:0),1,1);
- const unsigned int pe = _depth - dp;
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
- _width*_height*_spectrum>=128))
- for (int p = 0; p<(int)pe; p+=dp)
- get_crop(0,0,p,0,_width - 1,_height - 1,p + dp - 1,_spectrum - 1).move_to(res[p/dp]);
- get_crop(0,0,(res._width - 1)*dp,0,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
- } else res.assign(*this);
- } break;
- case 'c' : {
- if (_spectrum>dp) {
- res.assign(_spectrum/dp + (_spectrum%dp?1:0),1,1);
- const unsigned int pe = _spectrum - dp;
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*128 &&
- _width*_height*_depth>=128))
- for (int p = 0; p<(int)pe; p+=dp)
- get_crop(0,0,0,p,_width - 1,_height - 1,_depth - 1,p + dp - 1).move_to(res[p/dp]);
- get_crop(0,0,0,(res._width - 1)*dp,_width - 1,_height - 1,_depth - 1,_spectrum - 1).move_to(res.back());
- } else res.assign(*this);
- }
- }
- } else if (nb>0) { // Split by number of (non-homogeneous) blocs
- const unsigned int siz = _axis=='x'?_width:_axis=='y'?_height:_axis=='z'?_depth:_axis=='c'?_spectrum:0;
- if ((unsigned int)nb>siz)
- throw CImgArgumentException(_cimg_instance
- "get_split(): Instance cannot be split along %c-axis into %u blocs.",
- cimg_instance,
- axis,nb);
- if (nb==1) res.assign(*this);
- else {
- int err = (int)siz;
- unsigned int _p = 0;
- switch (_axis) {
- case 'x' : {
- cimg_forX(*this,p) if ((err-=nb)<=0) {
- get_crop(_p,0,0,0,p,_height - 1,_depth - 1,_spectrum - 1).move_to(res);
- err+=(int)siz;
- _p = p + 1U;
- }
- } break;
- case 'y' : {
- cimg_forY(*this,p) if ((err-=nb)<=0) {
- get_crop(0,_p,0,0,_width - 1,p,_depth - 1,_spectrum - 1).move_to(res);
- err+=(int)siz;
- _p = p + 1U;
- }
- } break;
- case 'z' : {
- cimg_forZ(*this,p) if ((err-=nb)<=0) {
- get_crop(0,0,_p,0,_width - 1,_height - 1,p,_spectrum - 1).move_to(res);
- err+=(int)siz;
- _p = p + 1U;
- }
- } break;
- case 'c' : {
- cimg_forC(*this,p) if ((err-=nb)<=0) {
- get_crop(0,0,0,_p,_width - 1,_height - 1,_depth - 1,p).move_to(res);
- err+=(int)siz;
- _p = p + 1U;
- }
- }
- }
- }
- } else { // Split by equal values according to specified axis
- T current = *_data;
- switch (_axis) {
- case 'x' : {
- int i0 = 0;
- cimg_forX(*this,i)
- if ((*this)(i)!=current) { get_columns(i0,i - 1).move_to(res); i0 = i; current = (*this)(i); }
- get_columns(i0,width() - 1).move_to(res);
- } break;
- case 'y' : {
- int i0 = 0;
- cimg_forY(*this,i)
- if ((*this)(0,i)!=current) { get_rows(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,i); }
- get_rows(i0,height() - 1).move_to(res);
- } break;
- case 'z' : {
- int i0 = 0;
- cimg_forZ(*this,i)
- if ((*this)(0,0,i)!=current) { get_slices(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,0,i); }
- get_slices(i0,depth() - 1).move_to(res);
- } break;
- case 'c' : {
- int i0 = 0;
- cimg_forC(*this,i)
- if ((*this)(0,0,0,i)!=current) { get_channels(i0,i - 1).move_to(res); i0 = i; current = (*this)(0,0,0,i); }
- get_channels(i0,spectrum() - 1).move_to(res);
- } break;
- default : {
- longT i0 = 0;
- cimg_foroff(*this,i)
- if ((*this)[i]!=current) {
- CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res);
- i0 = (longT)i; current = (*this)[i];
- }
- CImg<T>(_data + i0,1,(unsigned int)(size() - i0)).move_to(res);
- }
- }
- }
- return res;
- }
- //! Split image into a list of sub-images, according to a specified splitting value sequence and optionally axis.
- /**
- \param values Splitting value sequence.
- \param axis Axis along which the splitting is performed. Can be '0' to ignore axis.
- \param keep_values Tells if the splitting sequence must be kept in the split blocs.
- **/
- template<typename t>
- CImgList<T> get_split(const CImg<t>& values, const char axis=0, const bool keep_values=true) const {
- typedef _cimg_Tt Tt;
- CImgList<T> res;
- if (is_empty()) return res;
- const ulongT vsiz = values.size();
- const char _axis = cimg::lowercase(axis);
- if (!vsiz) return CImgList<T>(*this);
- if (vsiz==1) { // Split according to a single value
- const T value = (T)*values;
- switch (_axis) {
- case 'x' : {
- unsigned int i0 = 0, i = 0;
- do {
- while (i<_width && (*this)(i)==value) ++i;
- if (i>i0) { if (keep_values) get_columns(i0,i - 1).move_to(res); i0 = i; }
- while (i<_width && (*this)(i)!=value) ++i;
- if (i>i0) { get_columns(i0,i - 1).move_to(res); i0 = i; }
- } while (i<_width);
- } break;
- case 'y' : {
- unsigned int i0 = 0, i = 0;
- do {
- while (i<_height && (*this)(0,i)==value) ++i;
- if (i>i0) { if (keep_values) get_rows(i0,i - 1).move_to(res); i0 = i; }
- while (i<_height && (*this)(0,i)!=value) ++i;
- if (i>i0) { get_rows(i0,i - 1).move_to(res); i0 = i; }
- } while (i<_height);
- } break;
- case 'z' : {
- unsigned int i0 = 0, i = 0;
- do {
- while (i<_depth && (*this)(0,0,i)==value) ++i;
- if (i>i0) { if (keep_values) get_slices(i0,i - 1).move_to(res); i0 = i; }
- while (i<_depth && (*this)(0,0,i)!=value) ++i;
- if (i>i0) { get_slices(i0,i - 1).move_to(res); i0 = i; }
- } while (i<_depth);
- } break;
- case 'c' : {
- unsigned int i0 = 0, i = 0;
- do {
- while (i<_spectrum && (*this)(0,0,0,i)==value) ++i;
- if (i>i0) { if (keep_values) get_channels(i0,i - 1).move_to(res); i0 = i; }
- while (i<_spectrum && (*this)(0,0,0,i)!=value) ++i;
- if (i>i0) { get_channels(i0,i - 1).move_to(res); i0 = i; }
- } while (i<_spectrum);
- } break;
- default : {
- const ulongT siz = size();
- ulongT i0 = 0, i = 0;
- do {
- while (i<siz && (*this)[i]==value) ++i;
- if (i>i0) {
- if (keep_values) CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res);
- i0 = i;
- }
- while (i<siz && (*this)[i]!=value) ++i;
- if (i>i0) { CImg<T>(_data + i0,1,(unsigned int)(i - i0)).move_to(res); i0 = i; }
- } while (i<siz);
- }
- }
- } else { // Split according to multiple values
- ulongT j = 0;
- switch (_axis) {
- case 'x' : {
- unsigned int i0 = 0, i1 = 0, i = 0;
- do {
- if ((Tt)(*this)(i)==(Tt)*values) {
- i1 = i; j = 0;
- while (i<_width && (Tt)(*this)(i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
- i-=j;
- if (i>i1) {
- if (i1>i0) get_columns(i0,i1 - 1).move_to(res);
- if (keep_values) get_columns(i1,i - 1).move_to(res);
- i0 = i;
- } else ++i;
- } else ++i;
- } while (i<_width);
- if (i0<_width) get_columns(i0,width() - 1).move_to(res);
- } break;
- case 'y' : {
- unsigned int i0 = 0, i1 = 0, i = 0;
- do {
- if ((Tt)(*this)(0,i)==(Tt)*values) {
- i1 = i; j = 0;
- while (i<_height && (Tt)(*this)(0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
- i-=j;
- if (i>i1) {
- if (i1>i0) get_rows(i0,i1 - 1).move_to(res);
- if (keep_values) get_rows(i1,i - 1).move_to(res);
- i0 = i;
- } else ++i;
- } else ++i;
- } while (i<_height);
- if (i0<_height) get_rows(i0,height() - 1).move_to(res);
- } break;
- case 'z' : {
- unsigned int i0 = 0, i1 = 0, i = 0;
- do {
- if ((Tt)(*this)(0,0,i)==(Tt)*values) {
- i1 = i; j = 0;
- while (i<_depth && (Tt)(*this)(0,0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
- i-=j;
- if (i>i1) {
- if (i1>i0) get_slices(i0,i1 - 1).move_to(res);
- if (keep_values) get_slices(i1,i - 1).move_to(res);
- i0 = i;
- } else ++i;
- } else ++i;
- } while (i<_depth);
- if (i0<_depth) get_slices(i0,depth() - 1).move_to(res);
- } break;
- case 'c' : {
- unsigned int i0 = 0, i1 = 0, i = 0;
- do {
- if ((Tt)(*this)(0,0,0,i)==(Tt)*values) {
- i1 = i; j = 0;
- while (i<_spectrum && (Tt)(*this)(0,0,0,i)==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
- i-=j;
- if (i>i1) {
- if (i1>i0) get_channels(i0,i1 - 1).move_to(res);
- if (keep_values) get_channels(i1,i - 1).move_to(res);
- i0 = i;
- } else ++i;
- } else ++i;
- } while (i<_spectrum);
- if (i0<_spectrum) get_channels(i0,spectrum() - 1).move_to(res);
- } break;
- default : {
- const ulongT siz = size();
- ulongT i0 = 0, i1 = 0, i = 0;
- do {
- if ((Tt)(*this)[i]==(Tt)*values) {
- i1 = i; j = 0;
- while (i<siz && (Tt)(*this)[i]==(Tt)values[j]) { ++i; if (++j>=vsiz) j = 0; }
- i-=j;
- if (i>i1) {
- if (i1>i0) CImg<T>(_data + i0,1,(unsigned int)(i1 - i0)).move_to(res);
- if (keep_values) CImg<T>(_data + i1,1,(unsigned int)(i - i1)).move_to(res);
- i0 = i;
- } else ++i;
- } else ++i;
- } while (i<siz);
- if (i0<siz) CImg<T>(_data + i0,1,(unsigned int)(siz - i0)).move_to(res);
- } break;
- }
- }
- return res;
- }
- //! Append two images along specified axis.
- /**
- \param img Image to append with instance image.
- \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Append alignment in \c [0,1].
- **/
- template<typename t>
- CImg<T>& append(const CImg<t>& img, const char axis='x', const float align=0) {
- if (is_empty()) return assign(img,false);
- if (!img) return *this;
- return CImgList<T>(*this,true).insert(img).get_append(axis,align).move_to(*this);
- }
- //! Append two images along specified axis \specialization.
- CImg<T>& append(const CImg<T>& img, const char axis='x', const float align=0) {
- if (is_empty()) return assign(img,false);
- if (!img) return *this;
- return CImgList<T>(*this,img,true).get_append(axis,align).move_to(*this);
- }
- //! Append two images along specified axis \const.
- template<typename t>
- CImg<_cimg_Tt> get_append(const CImg<T>& img, const char axis='x', const float align=0) const {
- if (is_empty()) return +img;
- if (!img) return +*this;
- return CImgList<_cimg_Tt>(*this,true).insert(img).get_append(axis,align);
- }
- //! Append two images along specified axis \specialization.
- CImg<T> get_append(const CImg<T>& img, const char axis='x', const float align=0) const {
- if (is_empty()) return +img;
- if (!img) return +*this;
- return CImgList<T>(*this,img,true).get_append(axis,align);
- }
- //@}
- //---------------------------------------
- //
- //! \name Filtering / Transforms
- //@{
- //---------------------------------------
- //! Correlate image by a kernel.
- /**
- \param kernel = the correlation kernel.
- \param boundary_conditions Boundary condition. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \param is_normalized = enable local normalization.
- \param channel_mode Channel processing mode.
- Can be { 0=all | 1=one for one (default) | 2=partial sum | 3=full sum }.
- \param xcenter X-coordinate of the kernel center (~0U>>1 means 'centered').
- \param ycenter Y-coordinate of the kernel center (~0U>>1 means 'centered').
- \param zcenter Z-coordinate of the kernel center (~0U>>1 means 'centered').
- \param xstart Starting X-coordinate of the instance image.
- \param ystart Starting Y-coordinate of the instance image.
- \param zstart Starting Z-coordinate of the instance image.
- \param xend Ending X-coordinate of the instance image.
- \param yend Ending Y-coordinate of the instance image.
- \param zend Ending Z-coordinate of the instance image.
- \param xstride Stride along the X-axis.
- \param ystride Stride along the Y-axis.
- \param zstride Stride along the Z-axis.
- \param xdilation Dilation along the X-axis.
- \param ydilation Dilation along the Y-axis.
- \param zdilation Dilation along the Z-axis.
- \param interpolation_type Can be { false=nearest | true=linear }.
- \note
- - The correlation of the image instance \p *this by the kernel \p kernel is defined to be:
- res(x,y,z) = sum_{i,j,k} (*this)(\alpha_x\;x + \beta_x\;(i - c_x),\alpha_y\;y + \beta_y\;(j -
- c_y),\alpha_z\;z + \beta_z\;(k - c_z))*kernel(i,j,k).
- **/
- template<typename t>
- CImg<T>& correlate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_normalized=false, const unsigned int channel_mode=1,
- const int xcenter=(int)(~0U>>1),
- const int ycenter=(int)(~0U>>1),
- const int zcenter=(int)(~0U>>1),
- const int xstart=0,
- const int ystart=0,
- const int zstart=0,
- const int xend=(int)(~0U>>1),
- const int yend=(int)(~0U>>1),
- const int zend=(int)(~0U>>1),
- const float xstride=1, const float ystride=1, const float zstride=1,
- const float xdilation=1, const float ydilation=1, const float zdilation=1,
- const bool interpolation_type=false) {
- if (is_empty() || !kernel) return *this;
- return get_correlate(kernel,boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
- xstride,ystride,zstride,xdilation,ydilation,zdilation,
- interpolation_type).move_to(*this);
- }
- template<typename t>
- CImg<_cimg_Ttfloat> get_correlate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_normalized=false, const unsigned int channel_mode=1,
- const int xcenter=(int)(~0U>>1),
- const int ycenter=(int)(~0U>>1),
- const int zcenter=(int)(~0U>>1),
- const int xstart=0,
- const int ystart=0,
- const int zstart=0,
- const int xend=(int)(~0U>>1),
- const int yend=(int)(~0U>>1),
- const int zend=(int)(~0U>>1),
- const float xstride=1, const float ystride=1, const float zstride=1,
- const float xdilation=1, const float ydilation=1, const float zdilation=1,
- const bool interpolation_type=false) const {
- return _correlate(kernel,boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
- xstride,ystride,zstride,xdilation,ydilation,zdilation,
- interpolation_type,false);
- }
- //! Correlate image by a kernel \newinstance.
- template<typename t>
- CImg<_cimg_Ttfloat> _correlate(const CImg<t>& kernel, const unsigned int boundary_conditions,
- const bool is_normalized, const unsigned int channel_mode,
- const int xcenter, const int ycenter, const int zcenter,
- const int xstart, const int ystart, const int zstart,
- const int xend, const int yend, const int zend,
- const float xstride, const float ystride, const float zstride,
- const float xdilation, const float ydilation, const float zdilation,
- const bool interpolation_type, const bool is_convolve) const {
- typedef _cimg_Ttfloat Ttfloat;
- CImg<Ttfloat> res;
- _cimg_abort_init_openmp;
- cimg_abort_init;
- if (xstart>xend || ystart>yend || zstart>zend)
- throw CImgArgumentException(_cimg_instance
- "%s(): Invalid xyz-start/end arguments (start = (%d,%d,%d), end = (%d,%d,%d)).",
- cimg_instance,
- is_convolve?"convolve":"correlate",
- xstart,ystart,zstart,xend,yend,zend);
- if (xstride<=0 || ystride<=0 || zstride<=0)
- throw CImgArgumentException(_cimg_instance
- "%s(): Invalid stride arguments (%g,%g,%g).",
- cimg_instance,
- is_convolve?"convolve":"correlate",
- xstride,ystride,zstride);
- if (is_empty() || !kernel) return *this;
- int
- _xcenter = xcenter==(int)(~0U>>1)?kernel.width()/2 - 1 + (kernel.width()%2):
- std::min(xcenter,kernel.width() - 1),
- _ycenter = ycenter==(int)(~0U>>1)?kernel.height()/2 - 1 + (kernel.height()%2):
- std::min(ycenter,kernel.height() - 1),
- _zcenter = zcenter==(int)(~0U>>1)?kernel.depth()/2 - 1 + (kernel.depth()%2):
- std::min(zcenter,kernel.depth() - 1);
- float _xdilation = xdilation, _ydilation = ydilation, _zdilation = zdilation;
- CImg<t> _kernel;
- if (is_convolve) { // If convolution, go back to correlation
- if (kernel.size()/kernel.spectrum()<=27) {
- _kernel = CImg<t>(kernel._data,kernel.size()/kernel._spectrum,1,1,kernel._spectrum,true).
- get_mirror('x').resize(kernel,-1);
- _xcenter = kernel.width() - 1 - _xcenter;
- _ycenter = kernel.height() - 1 - _ycenter;
- _zcenter = kernel.depth() - _zcenter - 1;
- } else { _kernel = kernel.get_shared(); _xdilation*=-1; _ydilation*=-1; _zdilation*=-1; }
- } else _kernel = kernel.get_shared();
- const int
- _xend = xend==(int)(~0U>>1)?width() - 1:xend,
- _yend = yend==(int)(~0U>>1)?height() - 1:yend,
- _zend = zend==(int)(~0U>>1)?depth() - 1:zend,
- i_xstride = (int)cimg::round(xstride),
- i_ystride = (int)cimg::round(ystride),
- i_zstride = (int)cimg::round(zstride),
- i_xdilation = (int)cimg::round(_xdilation),
- i_ydilation = (int)cimg::round(_ydilation),
- i_zdilation = (int)cimg::round(_zdilation),
- res_width = _xend - xstart + 1,
- res_height = _yend - ystart + 1,
- res_depth = _zend + zstart + 1,
- smin = std::min(spectrum(),_kernel.spectrum()),
- smax = std::max(spectrum(),_kernel.spectrum()),
- cend = !channel_mode?spectrum()*_kernel.spectrum():smax;
- const ulongT
- res_wh = (ulongT)res_width*res_height,
- res_whd = res_wh*res_depth,
- res_siz = res_whd*res._spectrum;
- if (!res_whd) return CImg<Ttfloat>();
- res.assign(res_width,res_height,res_depth,
- !channel_mode?_spectrum*_kernel._spectrum:
- channel_mode==1?smax:
- channel_mode==2?(int)std::ceil((float)smax/smin):1);
- if (channel_mode>=2) res.fill(0);
- const bool
- #if cimg_use_openmp==1
- is_master_thread = !omp_get_thread_num(),
- #else
- is_master_thread = true,
- #endif
- is_outer_parallel = is_master_thread &&
- (res._spectrum>=cimg::nb_cpus() || res_siz<=(cimg_openmp_sizefactor)*32768),
- is_inner_parallel = is_master_thread &&
- (!is_outer_parallel && res_whd>=(cimg_openmp_sizefactor)*32768),
- is_int_stride_dilation = xstride==i_xstride && ystride==i_ystride && zstride==i_zstride &&
- _xdilation==i_xdilation && _ydilation==i_ydilation && _zdilation==i_zdilation;
- cimg::unused(is_inner_parallel,is_outer_parallel);
- const int
- w = width(), h = height(), d = depth(),
- w1 = w - 1, h1 = h - 1, d1 = d - 1,
- w2 = 2*w, h2 = 2*h, d2 = 2*d;
- const ulongT wh = (ulongT)w*h, whd = wh*d;
- // Reshape kernel to enable optimizations for a few cases.
- if (boundary_conditions==1 &&
- _kernel._width>1 && _kernel._height>1 &&
- ((_kernel._depth==1 && _kernel._width<=5 && _kernel._height<=5) ||
- (_kernel._depth<=3 && _kernel._width<=3 && _kernel._height<=3)) &&
- xstart>=0 && ystart>=0 && zstart>=0 &&
- _xend<width() && _yend<height() && _zend<depth() &&
- is_int_stride_dilation &&
- xstride==1 && ystride==1 && zstride==1 &&
- i_xdilation>=0 && i_ydilation>=0 && i_zdilation>=0) {
- const unsigned int M = cimg::max(_kernel._width,_kernel._height,_kernel._depth);
- _kernel.assign(_kernel.get_resize(M + 1 - (M%2),M + 1 - (M%2),_kernel._depth>1?M + 1 - (M%2):1,-100,
- 0,0,
- 1,1,1),false);
- _xcenter = _ycenter = (int)M/2;
- if (_kernel._depth>1) _ycenter = (int)M/2;
- }
- // Optimized version for a few particular cases (3x3, 5x5 and 3x3x3 kernels, with a few other conditions).
- if (boundary_conditions==1 &&
- _kernel._width==_kernel._height &&
- ((_kernel._depth==1 && (_kernel._width==3 || _kernel._width==5)) ||
- (_kernel._depth==_kernel._width && _kernel._width==3)) &&
- _xcenter==_kernel.width()/2 && _ycenter==_kernel.height()/2 && _zcenter==_kernel.depth()/2 &&
- xstart>=0 && ystart>=0 && zstart>=0 &&
- _xend<width() && _yend<height() && _zend<depth() &&
- is_int_stride_dilation &&
- xstride==1 && ystride==1 && zstride==1 &&
- i_xdilation>=0 && i_ydilation>=0 && i_zdilation>=0) {
- switch (_kernel._depth) {
- case 3 : { // 3x3x3 centered kernel
- cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
- for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- const CImg<T> I = get_shared_channel(c%_spectrum);
- const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
- CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
- CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
- if (is_normalized) {
- const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,Z) {
- const int
- x = xstart + X, y = ystart + Y, z = zstart + Z,
- px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1,
- pz = z - i_zdilation>0?z - i_zdilation:0, nz = z + i_zdilation<d1?z + i_zdilation:d1;
- const Ttfloat N = M2*(cimg::sqr(I(px,py,pz)) + cimg::sqr(I(x,py,pz)) + cimg::sqr(I(nx,py,pz)) +
- cimg::sqr(I(px,y,pz)) + cimg::sqr(I(x,y,pz)) + cimg::sqr(I(nx,y,pz)) +
- cimg::sqr(I(px,ny,pz)) + cimg::sqr(I(x,ny,pz)) + cimg::sqr(I(nx,ny,pz)) +
- cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) + cimg::sqr(I(nx,py,z)) +
- cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) + cimg::sqr(I(nx,y,z)) +
- cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) + cimg::sqr(I(nx,ny,z)) +
- cimg::sqr(I(px,py,nz)) + cimg::sqr(I(x,py,nz)) + cimg::sqr(I(nx,py,nz)) +
- cimg::sqr(I(px,y,nz)) + cimg::sqr(I(x,y,nz)) + cimg::sqr(I(nx,y,nz)) +
- cimg::sqr(I(px,ny,nz)) + cimg::sqr(I(x,ny,nz)) + cimg::sqr(I(nx,ny,nz)));
- _res(X,Y,Z) = (Ttfloat)(N?(K[0]*I(px,py,pz) + K[1]*I(x,py,pz) + K[2]*I(nx,py,pz) +
- K[3]*I(px,y,pz) + K[4]*I(x,y,pz) + K[5]*I(nx,y,pz) +
- K[6]*I(px,ny,pz) + K[7]*I(x,ny,pz) + K[8]*I(nx,ny,pz) +
- K[9]*I(px,py,z) + K[10]*I(x,py,z) + K[11]*I(nx,py,z) +
- K[12]*I(px,y,z) + K[13]*I(x,y,z) + K[14]*I(nx,y,z) +
- K[15]*I(px,ny,z) + K[16]*I(x,ny,z) + K[17]*I(nx,ny,z) +
- K[18]*I(px,py,nz) + K[19]*I(x,py,nz) + K[20]*I(nx,py,nz) +
- K[21]*I(px,y,nz) + K[22]*I(x,y,nz) + K[23]*I(nx,y,nz) +
- K[24]*I(px,ny,nz) + K[25]*I(x,ny,nz) + K[26]*I(nx,ny,nz))/std::sqrt(N):0);
- }
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,Z) {
- const int
- x = xstart + X, y = ystart + Y, z = zstart + Z,
- px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1,
- pz = z - i_zdilation>0?z - i_zdilation:0, nz = z + i_zdilation<d1?z + i_zdilation:d1;
- _res(X,Y,Z) = (Ttfloat)(K[0]*I(px,py,pz) + K[1]*I(x,py,pz) + K[2]*I(nx,py,pz) +
- K[3]*I(px,y,pz) + K[4]*I(x,y,pz) + K[5]*I(nx,y,pz) +
- K[6]*I(px,ny,pz) + K[7]*I(x,ny,pz) + K[8]*I(nx,ny,pz) +
- K[9]*I(px,py,z) + K[10]*I(x,py,z) + K[11]*I(nx,py,z) +
- K[12]*I(px,y,z) + K[13]*I(x,y,z) + K[14]*I(nx,y,z) +
- K[15]*I(px,ny,z) + K[16]*I(x,ny,z) + K[17]*I(nx,ny,z) +
- K[18]*I(px,py,nz) + K[19]*I(x,py,nz) + K[20]*I(nx,py,nz) +
- K[21]*I(px,y,nz) + K[22]*I(x,y,nz) + K[23]*I(nx,y,nz) +
- K[24]*I(px,ny,nz) + K[25]*I(x,ny,nz) + K[26]*I(nx,ny,nz));
- }
- }
- if (channel_mode==2)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
- else if (channel_mode==3)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
- } _cimg_abort_catch_openmp2
- } break;
- default :
- case 1 :
- switch (_kernel._width) {
- case 5 : { // 5x5 centered kernel
- cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
- for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- const CImg<T> I = get_shared_channel(c%_spectrum);
- const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
- CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
- CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
- if (is_normalized) {
- const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,z) {
- const int
- x = xstart + X, y = ystart + Y,
- px = x - i_xdilation>0?x - i_xdilation:0, bx = px - i_xdilation>0?px - i_xdilation:0,
- nx = x + i_xdilation<w1?x + i_xdilation:w1, ax = nx + i_xdilation<w1?nx + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, by = py - i_ydilation>0?py - i_ydilation:0,
- ny = y + i_ydilation<h1?y + i_ydilation:h1, ay = ny + i_ydilation<h1?ny + i_ydilation:h1;
- const Ttfloat N = M2*(cimg::sqr(I(bx,by,z)) + cimg::sqr(I(px,by,z)) + cimg::sqr(I(x,by,z)) +
- cimg::sqr(I(nx,by,z)) + cimg::sqr(I(ax,by,z)) +
- cimg::sqr(I(bx,py,z)) + cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) +
- cimg::sqr(I(nx,py,z)) + cimg::sqr(I(ax,py,z)) +
- cimg::sqr(I(bx,y,z)) + cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) +
- cimg::sqr(I(nx,y,z)) + cimg::sqr(I(ax,y,z)) +
- cimg::sqr(I(bx,ny,z)) + cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) +
- cimg::sqr(I(nx,ny,z)) + cimg::sqr(I(ax,ny,z)) +
- cimg::sqr(I(bx,ay,z)) + cimg::sqr(I(px,ay,z)) + cimg::sqr(I(x,ay,z)) +
- cimg::sqr(I(nx,ay,z)) + cimg::sqr(I(ax,ay,z)));
- _res(X,Y,z) = (Ttfloat)(N?(K[0]*I(bx,by,z) + K[1]*I(px,by,z) + K[2]*I(x,by,z) +
- K[3]*I(nx,by,z) + K[4]*I(ax,by,z) +
- K[5]*I(bx,py,z) + K[6]*I(px,py,z) + K[7]*I(x,py,z) +
- K[8]*I(nx,py,z) + K[9]*I(ax,py,z) +
- K[10]*I(bx,y,z) + K[11]*I(px,y,z) + K[12]*I(x,y,z) +
- K[13]*I(nx,y,z) + K[14]*I(ax,y,z) +
- K[15]*I(bx,ny,z) + K[16]*I(px,ny,z) + K[17]*I(x,ny,z) +
- K[18]*I(nx,ny,z) + K[19]*I(ax,ny,z) +
- K[20]*I(bx,ay,z) + K[21]*I(px,ay,z) + K[22]*I(x,ay,z) +
- K[23]*I(nx,ay,z) + K[24]*I(ax,ay,z))/std::sqrt(N):0);
- }
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,z) {
- const int
- x = xstart + X, y = ystart + Y,
- px = x - i_xdilation>0?x - i_xdilation:0, bx = px - i_xdilation>0?px - i_xdilation:0,
- nx = x + i_xdilation<w1?x + i_xdilation:w1, ax = nx + i_xdilation<w1?nx + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, by = py - i_ydilation>0?py - i_ydilation:0,
- ny = y + i_ydilation<h1?y + i_ydilation:h1, ay = ny + i_ydilation<h1?ny + i_ydilation:h1;
- _res(X,Y,z) = (Ttfloat)(K[0]*I(bx,by,z) + K[1]*I(px,by,z) + K[2]*I(x,by,z) +
- K[3]*I(nx,by,z) + K[4]*I(ax,by,z) +
- K[5]*I(bx,py,z) + K[6]*I(px,py,z) + K[7]*I(x,py,z) +
- K[8]*I(nx,py,z) + K[9]*I(ax,py,z) +
- K[10]*I(bx,y,z) + K[11]*I(px,y,z) + K[12]*I(x,y,z) +
- K[13]*I(nx,y,z) + K[14]*I(ax,y,z) +
- K[15]*I(bx,ny,z) + K[16]*I(px,ny,z) + K[17]*I(x,ny,z) +
- K[18]*I(nx,ny,z) + K[19]*I(ax,ny,z) +
- K[20]*I(bx,ay,z) + K[21]*I(px,ay,z) + K[22]*I(x,ay,z) +
- K[23]*I(nx,ay,z) + K[24]*I(ax,ay,z));
- }
- }
- if (channel_mode==2)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
- else if (channel_mode==3)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
- } _cimg_abort_catch_openmp2
- } break;
- case 3 : { // 3x3 centered kernel
- cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
- for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- const CImg<T> I = get_shared_channel(c%_spectrum);
- const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
- CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
- CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
- if (is_normalized) {
- const Ttfloat M = (Ttfloat)K.magnitude(2), M2 = M*M;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,z) {
- const int
- x = xstart + X, y = ystart + Y,
- px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1;
- const Ttfloat N = M2*(cimg::sqr(I(px,py,z)) + cimg::sqr(I(x,py,z)) + cimg::sqr(I(nx,py,z)) +
- cimg::sqr(I(px,y,z)) + cimg::sqr(I(x,y,z)) + cimg::sqr(I(nx,y,z)) +
- cimg::sqr(I(px,ny,z)) + cimg::sqr(I(x,ny,z)) + cimg::sqr(I(nx,ny,z)));
- _res(X,Y,z) = (Ttfloat)(N?(K[0]*I(px,py,z) + K[1]*I(x,py,z) + K[2]*I(nx,py,z) +
- K[3]*I(px,y,z) + K[4]*I(x,y,z) + K[5]*I(nx,y,z) +
- K[6]*I(px,ny,z) + K[7]*I(x,ny,z) + K[8]*I(nx,ny,z))/std::sqrt(N):0);
- }
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forXYZ(res,X,Y,z) {
- const int
- x = xstart + X, y = ystart + Y,
- px = x - i_xdilation>0?x - i_xdilation:0, nx = x + i_xdilation<w1?x + i_xdilation:w1,
- py = y - i_ydilation>0?y - i_ydilation:0, ny = y + i_ydilation<h1?y + i_ydilation:h1;
- _res(X,Y,z) = (Ttfloat)(K[0]*I(px,py,z) + K[1]*I(x,py,z) + K[2]*I(nx,py,z) +
- K[3]*I(px,y,z) + K[4]*I(x,y,z) + K[5]*I(nx,y,z) +
- K[6]*I(px,ny,z) + K[7]*I(x,ny,z) + K[8]*I(nx,ny,z));
- }
- }
- if (channel_mode==2)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
- else if (channel_mode==3)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
- } _cimg_abort_catch_openmp2
- } break;
- }
- }
- } else if (_kernel._width==1 && _kernel._height==1 && _kernel._depth==1 &&
- !_xcenter && !_ycenter && !_zcenter &&
- xstart>=0 && ystart>=0 && zstart>=0 &&
- _xend<width() && _yend<height() && _zend<depth() &&
- xstride==1 && ystride==1 && zstride==1) {
- // Special optimization for 1x1 kernel.
- cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
- for (int c = 0; c<cend; ++c) {
- const t valK = _kernel[!channel_mode?c/_spectrum:c%_kernel._spectrum];
- CImg<T> I = get_crop(xstart,ystart,zstart,c%_spectrum,_xend,_yend,_zend,c%_spectrum)*=valK;
- if (is_normalized) I.sign();
- switch (channel_mode) {
- case 0 : // All
- case 1 : // One for one
- res.get_shared_channel(c) = I;
- break;
- case 2 : // Partial sum
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=I;
- break;
- case 3 : // Full sum
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=I;
- break;
- }
- }
- } else { // Generic version
- cimg_pragma_openmp(parallel for cimg_openmp_if(is_outer_parallel))
- for (int c = 0; c<cend; ++c) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- const CImg<T> I = get_shared_channel(c%_spectrum);
- const CImg<t> K = _kernel.get_shared_channel(!channel_mode?c/_spectrum:c%_kernel._spectrum);
- CImg<Ttfloat> _res = channel_mode<=1?res.get_shared_channel(c):
- CImg<Ttfloat>(res.width(),res.height(),res.depth(),1);
- Ttfloat M = 0, M2 = 0;
- if (is_normalized) { M = (Ttfloat)K.magnitude(2); M2 = cimg::sqr(M); }
- #define _cimg_correlate_x_int const int ix = xstart + i_xstride*x + i_xdilation*(p - _xcenter)
- #define _cimg_correlate_y_int const int iy = ystart + i_ystride*y + i_ydilation*(q - _ycenter)
- #define _cimg_correlate_z_int const int iz = zstart + i_zstride*z + i_zdilation*(r - _zcenter)
- #define _cimg_correlate_x_float const float ix = xstart + xstride*x + _xdilation*(p - _xcenter)
- #define _cimg_correlate_y_float const float iy = ystart + ystride*y + _ydilation*(q - _ycenter)
- #define _cimg_correlate_z_float const float iz = zstart + zstride*z + _zdilation*(r - _zcenter)
- #define _cimg_correlate_x_int_dirichlet const bool is_in_x = ix>=0 && ix<w
- #define _cimg_correlate_y_int_dirichlet const bool is_in_y = iy>=0 && iy<h
- #define _cimg_correlate_z_int_dirichlet const bool is_in_z = iz>=0 && iz<d
- #define _cimg_correlate_x_float_dirichlet _cimg_correlate_x_int_dirichlet
- #define _cimg_correlate_y_float_dirichlet _cimg_correlate_y_int_dirichlet
- #define _cimg_correlate_z_float_dirichlet _cimg_correlate_z_int_dirichlet
- #define _cimg_correlate_x_int_neumann const int nix = cimg::cut(ix,0,w1)
- #define _cimg_correlate_y_int_neumann const int niy = cimg::cut(iy,0,h1)
- #define _cimg_correlate_z_int_neumann const int niz = cimg::cut(iz,0,d1)
- #define _cimg_correlate_x_float_neumann const float nix = cimg::cut(ix,0,w1)
- #define _cimg_correlate_y_float_neumann const float niy = cimg::cut(iy,0,h1)
- #define _cimg_correlate_z_float_neumann const float niz = cimg::cut(iz,0,d1)
- #define _cimg_correlate_x_int_periodic const int nix = cimg::mod(ix,w)
- #define _cimg_correlate_y_int_periodic const int niy = cimg::mod(iy,h)
- #define _cimg_correlate_z_int_periodic const int niz = cimg::mod(iz,d)
- #define _cimg_correlate_x_float_periodic const float nix = cimg::mod(ix,w)
- #define _cimg_correlate_y_float_periodic const float niy = cimg::mod(iy,h)
- #define _cimg_correlate_z_float_periodic const float niz = cimg::mod(iz,d)
- #define _cimg_correlate_x_int_mirror const int mx = cimg::mod(ix,w2), nix = mx<w?mx:w2 - mx - 1
- #define _cimg_correlate_y_int_mirror const int my = cimg::mod(iy,h2), niy = my<h?my:h2 - my - 1
- #define _cimg_correlate_z_int_mirror const int mz = cimg::mod(iz,d2), niz = mz<d?mz:d2 - mz - 1
- #define _cimg_correlate_x_float_mirror const float mx = cimg::mod(ix,w2), nix = mx<w?mx:w2 - mx - 1
- #define _cimg_correlate_y_float_mirror const float my = cimg::mod(iy,h2), niy = my<h?my:h2 - my - 1
- #define _cimg_correlate_z_float_mirror const float mz = cimg::mod(iz,d2), niz = mz<d?mz:d2 - mz - 1
- #define _cimg_correlate(type,boundary,access) \
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel)) \
- cimg_forXYZ(res,x,y,z) { \
- Ttfloat val = 0; \
- const t *pK = K._data; \
- cimg_forZ(_kernel,r) { _cimg_correlate_z_##type; _cimg_correlate_z_##type##_##boundary; \
- cimg_forY(_kernel,q) { _cimg_correlate_y_##type; _cimg_correlate_y_##type##_##boundary; \
- cimg_forX(_kernel,p) { _cimg_correlate_x_##type; _cimg_correlate_x_##type##_##boundary; \
- val+=*(pK++)*(access); \
- } \
- } \
- } \
- _res(x,y,z,0,res_wh,res_whd) = val; \
- }
- #define _cimg_correlate_n(type,boundary,access) \
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel)) \
- cimg_forXYZ(res,x,y,z) { \
- Ttfloat val = 0, N = 0; \
- const t *pK = K._data; \
- cimg_forZ(_kernel,r) { _cimg_correlate_z_##type; _cimg_correlate_z_##type##_##boundary; \
- cimg_forY(_kernel,q) { _cimg_correlate_y_##type; _cimg_correlate_y_##type##_##boundary; \
- cimg_forX(_kernel,p) { _cimg_correlate_x_##type; _cimg_correlate_x_##type##_##boundary; \
- Ttfloat _val = access; \
- val+=*(pK++)*_val; \
- _val*=_val; N+=_val; \
- } \
- } \
- } \
- N*=M2; _res(x,y,z,0,res_wh,res_whd) = N?val/std::sqrt(N):0; \
- }
- if (is_normalized) { // Normalized convolution/correlation
- if (is_int_stride_dilation) // Integer stride and dilation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _cimg_correlate_n(int,dirichlet,is_in_x && is_in_y && is_in_z?I(ix,iy,iz,0,wh,whd):(T)0);
- break;
- case 1 : // Neumann
- _cimg_correlate_n(int,neumann,I(nix,niy,niz,0,wh,whd));
- break;
- case 2 : // Periodic
- _cimg_correlate_n(int,periodic,I(nix,niy,niz,0,wh,whd));
- break;
- case 3 : // Mirror
- _cimg_correlate_n(int,mirror,I(nix,niy,niz,0,wh,whd));
- break;
- }
- else if (interpolation_type) // Non-integer stride or dilation, linear interpolation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _cimg_correlate_n(float,dirichlet,is_in_x && is_in_y && is_in_z?I.linear_atXYZ(ix,iy,iz,0,0):0);
- break;
- case 1 : // Neumann
- _cimg_correlate_n(float,neumann,I._linear_atXYZ(nix,niy,niz,0));
- break;
- case 2 : // Periodic
- _cimg_correlate_n(float,periodic,I._linear_atXYZ(nix,niy,niz,0));
- break;
- case 3 : // Mirror
- _cimg_correlate_n(float,mirror,I._linear_atXYZ(nix,niy,niz,0));
- break;
- }
- else // Non-integer stride or dilation, nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _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);
- break;
- case 1 : // Neumann
- _cimg_correlate_n(float,neumann,I((int)nix,(int)niy,(int)niz,0));
- break;
- case 2 : // Periodic
- _cimg_correlate_n(float,periodic,I((int)nix,(int)niy,(int)niz,0));
- break;
- case 3 : // Mirror
- _cimg_correlate_n(float,mirror,I((int)nix,(int)niy,(int)niz,0));
- break;
- }
- } else { // Standard convolution/correlation
- if (is_int_stride_dilation) // Integer stride and dilation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _cimg_correlate(int,dirichlet,is_in_x && is_in_y && is_in_z?I(ix,iy,iz,0,wh,whd):(T)0);
- break;
- case 1 : // Neumann
- _cimg_correlate(int,neumann,I(nix,niy,niz,0,wh,whd));
- break;
- case 2 : // Periodic
- _cimg_correlate(int,periodic,I(nix,niy,niz,0,wh,whd));
- break;
- case 3 : // Mirror
- _cimg_correlate(int,mirror,I(nix,niy,niz,0,wh,whd));
- break;
- }
- else if (interpolation_type) // Non-integer stride or dilation, linear interpolation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _cimg_correlate(float,dirichlet,is_in_x && is_in_y && is_in_z?I.linear_atXYZ(ix,iy,iz,0,0):0);
- break;
- case 1 : // Neumann
- _cimg_correlate(float,neumann,I._linear_atXYZ(nix,niy,niz,0));
- break;
- case 2 : // Periodic
- _cimg_correlate(float,periodic,I._linear_atXYZ(nix,niy,niz,0));
- break;
- case 3 : // Mirror
- _cimg_correlate(float,mirror,I._linear_atXYZ(nix,niy,niz,0));
- break;
- }
- else // Non-integer stride or dilation, nearest-neighbor interpolation
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- _cimg_correlate(float,dirichlet,is_in_x && is_in_y && is_in_z?I((int)ix,(int)iy,(int)iz,0,0):(T)0);
- break;
- case 1 : // Neumann
- _cimg_correlate(float,neumann,I((int)nix,(int)niy,(int)niz,0));
- break;
- case 2 : // Periodic
- _cimg_correlate(float,periodic,I((int)nix,(int)niy,(int)niz,0));
- break;
- case 3 : // Mirror
- _cimg_correlate(float,mirror,I((int)nix,(int)niy,(int)niz,0));
- break;
- }
- }
- if (channel_mode==2)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(c/smin)+=_res;
- else if (channel_mode==3)
- cimg_pragma_openmp(critical(_correlate)) res.get_shared_channel(0)+=_res;
- } _cimg_abort_catch_openmp2
- }
- cimg_abort_test;
- return res;
- }
- //! Convolve image by a kernel.
- /**
- \param kernel = the correlation kernel.
- \param boundary_conditions Boundary condition. Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \param is_normalized = enable local normalization.
- \param channel_mode Channel processing mode.
- Can be { 0=all | 1=one for one (default) | 2=partial sum | 3=full sum }.
- \param xcenter X-coordinate of the kernel center (~0U means 'centered').
- \param ycenter Y-coordinate of the kernel center (~0U means 'centered').
- \param zcenter Z-coordinate of the kernel center (~0U means 'centered').
- \param xstart Starting X-coordinate of the instance image.
- \param ystart Starting Y-coordinate of the instance image.
- \param zstart Starting Z-coordinate of the instance image.
- \param xend Ending X-coordinate of the instance image.
- \param yend Ending Y-coordinate of the instance image.
- \param zend Ending Z-coordinate of the instance image.
- \param xstride Stride along the X-axis.
- \param ystride Stride along the Y-axis.
- \param zstride Stride along the Z-axis.
- \param xdilation Dilation along the X-axis.
- \param ydilation Dilation along the Y-axis.
- \param zdilation Dilation along the Z-axis.
- \param interpolation_type Can be { false=nearest | true=linear }.
- \note
- - The convolution of the image instance \p *this by the kernel \p kernel is defined to be:
- res(x,y,z) = sum_{i,j,k} (*this)(\alpha_x\;x - \beta_x\;(i - c_x),\alpha_y\;y
- - \beta_y\;(j - c_y),\alpha_z\;z - \beta_z\;(k - c_z))*kernel(i,j,k).
- **/
- template<typename t>
- CImg<T>& convolve(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_normalized=false, const unsigned int channel_mode=1,
- const int xcenter=(int)(~0U>>1),
- const int ycenter=(int)(~0U>>1),
- const int zcenter=(int)(~0U>>1),
- const int xstart=0,
- const int ystart=0,
- const int zstart=0,
- const int xend=(int)(~0U>>1),
- const int yend=(int)(~0U>>1),
- const int zend=(int)(~0U>>1),
- const float xstride=1, const float ystride=1, const float zstride=1,
- const float xdilation=1, const float ydilation=1, const float zdilation=1,
- const bool interpolation_type=false) {
- if (is_empty() || !kernel) return *this;
- return get_convolve(kernel,boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
- xstride,ystride,zstride,xdilation,ydilation,zdilation,
- interpolation_type).move_to(*this);
- }
- //! Convolve image by a kernel \newinstance.
- template<typename t>
- CImg<_cimg_Ttfloat> get_convolve(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_normalized=false, const unsigned int channel_mode=1,
- const int xcenter=(int)(~0U>>1),
- const int ycenter=(int)(~0U>>1),
- const int zcenter=(int)(~0U>>1),
- const int xstart=0,
- const int ystart=0,
- const int zstart=0,
- const int xend=(int)(~0U>>1),
- const int yend=(int)(~0U>>1),
- const int zend=(int)(~0U>>1),
- const float xstride=1, const float ystride=1, const float zstride=1,
- const float xdilation=1, const float ydilation=1, const float zdilation=1,
- const bool interpolation_type=false) const {
- return _correlate(kernel,boundary_conditions,is_normalized,channel_mode,
- xcenter,ycenter,zcenter,xstart,ystart,zstart,xend,yend,zend,
- xstride,ystride,zstride,xdilation,ydilation,zdilation,
- interpolation_type,true);
- }
- //! Cumulate image values, optionally along specified axis.
- /**
- \param axis Cumulation axis. Set it to 0 to cumulate all values globally without taking axes into account.
- **/
- CImg<T>& cumulate(const char axis=0) {
- switch (cimg::lowercase(axis)) {
- case 'x' :
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth*_spectrum>=16))
- cimg_forYZC(*this,y,z,c) {
- T *ptrd = data(0,y,z,c);
- Tlong cumul = (Tlong)0;
- cimg_forX(*this,x) { cumul+=(Tlong)*ptrd; *(ptrd++) = (T)cumul; }
- }
- break;
- case 'y' : {
- const ulongT w = (ulongT)_width;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*512 &&
- _width*_depth*_spectrum>=16))
- cimg_forXZC(*this,x,z,c) {
- T *ptrd = data(x,0,z,c);
- Tlong cumul = (Tlong)0;
- cimg_forY(*this,y) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=w; }
- }
- } break;
- case 'z' : {
- const ulongT wh = (ulongT)_width*_height;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_depth>=(cimg_openmp_sizefactor)*512 &&
- _width*_depth*_spectrum>=16))
- cimg_forXYC(*this,x,y,c) {
- T *ptrd = data(x,y,0,c);
- Tlong cumul = (Tlong)0;
- cimg_forZ(*this,z) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=wh; }
- }
- } break;
- case 'c' : {
- const ulongT whd = (ulongT)_width*_height*_depth;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(_spectrum>=(cimg_openmp_sizefactor)*512 && _width*_height*_depth>=16))
- cimg_forXYZ(*this,x,y,z) {
- T *ptrd = data(x,y,z,0);
- Tlong cumul = (Tlong)0;
- cimg_forC(*this,c) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; ptrd+=whd; }
- }
- } break;
- default : { // Global cumulation
- Tlong cumul = (Tlong)0;
- cimg_for(*this,ptrd,T) { cumul+=(Tlong)*ptrd; *ptrd = (T)cumul; }
- }
- }
- return *this;
- }
- //! Cumulate image values, optionally along specified axis \newinstance.
- CImg<Tlong> get_cumulate(const char axis=0) const {
- return CImg<Tlong>(*this,false).cumulate(axis);
- }
- //! Cumulate image values, along specified axes.
- /**
- \param axes Cumulation axes, as a C-string.
- \note \c axes may contains multiple characters, e.g. \c "xyz"
- **/
- CImg<T>& cumulate(const char *const axes) {
- for (const char *s = axes; *s; ++s) cumulate(*s);
- return *this;
- }
- //! Cumulate image values, along specified axes \newinstance.
- CImg<Tlong> get_cumulate(const char *const axes) const {
- return CImg<Tlong>(*this,false).cumulate(axes);
- }
- //! Erode image by a structuring element.
- /**
- \param kernel Structuring element.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \param is_real Do the erosion in real (a.k.a 'non-flat') mode (\c true) rather than binary mode (\c false).
- **/
- template<typename t>
- CImg<T>& erode(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_real=false) {
- if (is_empty() || !kernel) return *this;
- return get_erode(kernel,boundary_conditions,is_real).move_to(*this);
- }
- //! Erode image by a structuring element \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_erode(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_real=false) const {
- if (is_empty() || !kernel) return *this;
- if (!is_real && kernel==0) return CImg<T>(width(),height(),depth(),spectrum(),0);
- typedef _cimg_Tt Tt;
- CImg<Tt> res(_width,_height,_depth,std::max(_spectrum,kernel._spectrum));
- const int
- mx2 = kernel.width()/2, my2 = kernel.height()/2, mz2 = kernel.depth()/2,
- mx1 = kernel.width() - mx2 - 1, my1 = kernel.height() - my2 - 1, mz1 = kernel.depth() - mz2 - 1,
- mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2,
- w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
- const bool
- is_inner_parallel = _width*_height*_depth>=(cimg_openmp_sizefactor)*32768,
- is_outer_parallel = res.size()>=(cimg_openmp_sizefactor)*32768;
- cimg::unused(is_inner_parallel,is_outer_parallel);
- _cimg_abort_init_openmp;
- cimg_abort_init;
- cimg_pragma_openmp(parallel for cimg_openmp_if(!is_inner_parallel && is_outer_parallel))
- cimg_forC(res,c) _cimg_abort_try_openmp {
- cimg_abort_test;
- const CImg<T> img = get_shared_channel(c%_spectrum);
- const CImg<t> K = kernel.get_shared_channel(c%kernel._spectrum);
- if (is_real) { // Real erosion
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- for (int z = mz1; z<mze; ++z)
- for (int y = my1; y<mye; ++y)
- for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- Tt min_val = cimg::type<Tt>::max();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- const t mval = K(mx1 + xm,my1 + ym,mz1 + zm);
- const Tt cval = (Tt)(img(x + xm,y + ym,z + zm) - mval);
- if (cval<min_val) min_val = cval;
- }
- res(x,y,z,c) = min_val;
- } _cimg_abort_catch_openmp2
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
- Tt min_val = cimg::type<Tt>::max();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- const t mval = K(mx1 + xm,my1 + ym,mz1 + zm);
- Tt cval;
- switch (boundary_conditions) {
- case 0 : cval = (Tt)(img.atXYZ(x + xm,y + ym,z + zm,0,(T)0) - mval); break;
- case 1 : cval = (Tt)(img._atXYZ(x + xm,y + ym,z + zm) - mval); break;
- case 2 : {
- const int
- nx = cimg::mod(x + xm,width()),
- ny = cimg::mod(y + ym,height()),
- nz = cimg::mod(z + zm,depth());
- cval = img(nx,ny,nz) - mval;
- } break;
- default : {
- const int
- tx = cimg::mod(x + xm,w2),
- ty = cimg::mod(y + ym,h2),
- tz = cimg::mod(z + zm,d2),
- nx = tx<width()?tx:w2 - tx - 1,
- ny = ty<height()?ty:h2 - ty - 1,
- nz = tz<depth()?tz:d2 - tz - 1;
- cval = img(nx,ny,nz) - mval;
- }
- }
- if (cval<min_val) min_val = cval;
- }
- res(x,y,z,c) = min_val;
- }
- } _cimg_abort_catch_openmp2
- } else { // Binary erosion
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- for (int z = mz1; z<mze; ++z)
- for (int y = my1; y<mye; ++y)
- for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- Tt min_val = cimg::type<Tt>::max();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm)
- if (K(mx1 + xm,my1 + ym,mz1 + zm)) {
- const Tt cval = (Tt)img(x + xm,y + ym,z + zm);
- if (cval<min_val) min_val = cval;
- }
- res(x,y,z,c) = min_val;
- } _cimg_abort_catch_openmp2
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
- Tt min_val = cimg::type<Tt>::max();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- if (K(mx1 + xm,my1 + ym,mz1 + zm)) {
- Tt cval;
- switch (boundary_conditions) {
- case 0 : cval = (Tt)img.atXYZ(x + xm,y + ym,z + zm,0,(T)0); break;
- case 1 : cval = (Tt)img._atXYZ(x + xm,y + ym,z + zm); break;
- case 2 : {
- const int
- nx = cimg::mod(x + xm,width()),
- ny = cimg::mod(y + ym,height()),
- nz = cimg::mod(z + zm,depth());
- cval = img(nx,ny,nz);
- } break;
- default : {
- const int
- tx = cimg::mod(x + xm,w2),
- ty = cimg::mod(y + ym,h2),
- tz = cimg::mod(z + zm,d2),
- nx = tx<width()?tx:w2 - tx - 1,
- ny = ty<height()?ty:h2 - ty - 1,
- nz = tz<depth()?tz:d2 - tz - 1;
- cval = img(nx,ny,nz);
- }
- }
- if (cval<min_val) min_val = cval;
- }
- }
- res(x,y,z,c) = min_val;
- }
- } _cimg_abort_catch_openmp2
- }
- } _cimg_abort_catch_openmp
- cimg_abort_test;
- return res;
- }
- //! Erode image by a rectangular structuring element of specified size.
- /**
- \param sx Width of the structuring element.
- \param sy Height of the structuring element.
- \param sz Depth of the structuring element.
- **/
- CImg<T>& erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) {
- if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this;
- if (sx>1 && _width>1) { // Along X-axis
- 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;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forYZC(*this,y,z,c) {
- T *const ptrdb = buf._data, *ptrd = buf._data, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }}
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(0,y,z,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- if (sy>1 && _height>1) { // Along Y-axis
- const int L = height(), off = width(), s = (int)sy, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1,
- s2 = _s2>L?L:_s2;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forXZC(*this,x,z,c) {
- T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
- }
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(x,0,z,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- if (sz>1 && _depth>1) { // Along Z-axis
- const int L = depth(), off = width()*height(), s = (int)sz, _s2 = s/2 + 1, _s1 = s - _s2, s1 = _s1>L?L:_s1,
- s2 = _s2>L?L:_s2;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forXYC(*this,x,y,c) {
- T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
- }
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(x,y,0,c); cur = std::min(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val<=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval<cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval<cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val<=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val<cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val<cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- return *this;
- }
- //! Erode image by a rectangular structuring element of specified size \newinstance.
- CImg<T> get_erode(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const {
- return (+*this).erode(sx,sy,sz);
- }
- //! Erode the image by a square structuring element of specified size.
- /**
- \param s Size of the structuring element.
- **/
- CImg<T>& erode(const unsigned int s) {
- return erode(s,s,s);
- }
- //! Erode the image by a square structuring element of specified size \newinstance.
- CImg<T> get_erode(const unsigned int s) const {
- return (+*this).erode(s);
- }
- //! Dilate image by a structuring element.
- /**
- \param kernel Structuring element.
- \param boundary_conditions Boundary conditions.
- Can be { 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }.
- \param is_real Do the dilation in real (a.k.a 'non-flat') mode (\c true) rather than binary mode (\c false).
- **/
- template<typename t>
- CImg<T>& dilate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_real=false) {
- if (is_empty() || !kernel) return *this;
- return get_dilate(kernel,boundary_conditions,is_real).move_to(*this);
- }
- //! Dilate image by a structuring element \newinstance.
- template<typename t>
- CImg<_cimg_Tt> get_dilate(const CImg<t>& kernel, const unsigned int boundary_conditions=1,
- const bool is_real=false) const {
- if (is_empty() || !kernel || (!is_real && kernel==0)) return *this;
- typedef _cimg_Tt Tt;
- CImg<Tt> res(_width,_height,_depth,std::max(_spectrum,kernel._spectrum));
- const int
- mx1 = kernel.width()/2, my1 = kernel.height()/2, mz1 = kernel.depth()/2,
- mx2 = kernel.width() - mx1 - 1, my2 = kernel.height() - my1 - 1, mz2 = kernel.depth() - mz1 - 1,
- mxe = width() - mx2, mye = height() - my2, mze = depth() - mz2,
- w2 = 2*width(), h2 = 2*height(), d2 = 2*depth();
- const bool
- is_inner_parallel = _width*_height*_depth>=(cimg_openmp_sizefactor)*32768,
- is_outer_parallel = res.size()>=(cimg_openmp_sizefactor)*32768;
- cimg::unused(is_inner_parallel,is_outer_parallel);
- _cimg_abort_init_openmp;
- cimg_abort_init;
- cimg_pragma_openmp(parallel for cimg_openmp_if(!is_inner_parallel && is_outer_parallel))
- cimg_forC(res,c) _cimg_abort_try_openmp {
- cimg_abort_test;
- const CImg<T> img = get_shared_channel(c%_spectrum);
- const CImg<t> K = kernel.get_shared_channel(c%kernel._spectrum);
- if (is_real) { // Real dilation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- for (int z = mz1; z<mze; ++z)
- for (int y = my1; y<mye; ++y)
- for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- Tt max_val = cimg::type<Tt>::min();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- const t mval = K(mx2 - xm,my2 - ym,mz2 - zm);
- const Tt cval = (Tt)(img(x + xm,y + ym,z + zm) + mval);
- if (cval>max_val) max_val = cval;
- }
- res(x,y,z,c) = max_val;
- } _cimg_abort_catch_openmp2
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
- Tt max_val = cimg::type<Tt>::min();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- const t mval = K(mx2 - xm,my2 - ym,mz2 - zm);
- Tt cval;
- switch (boundary_conditions) {
- case 0 : cval = (Tt)(img.atXYZ(x + xm,y + ym,z + zm,0,(T)0) + mval); break;
- case 1 : cval = (Tt)(img._atXYZ(x + xm,y + ym,z + zm) + mval); break;
- case 2 : {
- const int
- nx = cimg::mod(x + xm,width()),
- ny = cimg::mod(y + ym,height()),
- nz = cimg::mod(z + zm,depth());
- cval = img(nx,ny,nz) + mval;
- } break;
- default : {
- const int
- tx = cimg::mod(x + xm,w2),
- ty = cimg::mod(y + ym,h2),
- tz = cimg::mod(z + zm,d2),
- nx = tx<width()?tx:w2 - tx - 1,
- ny = ty<height()?ty:h2 - ty - 1,
- nz = tz<depth()?tz:d2 - tz - 1;
- cval = img(nx,ny,nz) + mval;
- }
- }
- if (cval>max_val) max_val = cval;
- }
- res(x,y,z,c) = max_val;
- }
- } _cimg_abort_catch_openmp2
- } else { // Binary dilation
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(is_inner_parallel))
- for (int z = mz1; z<mze; ++z)
- for (int y = my1; y<mye; ++y)
- for (int x = mx1; x<mxe; ++x) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- Tt max_val = cimg::type<Tt>::min();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm)
- if (K(mx2 - xm,my2 - ym,mz2 - zm)) {
- const Tt cval = (Tt)img(x + xm,y + ym,z + zm);
- if (cval>max_val) max_val = cval;
- }
- res(x,y,z,c) = max_val;
- } _cimg_abort_catch_openmp2
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(is_inner_parallel))
- cimg_forYZ(res,y,z) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- for (int x = 0; x<width(); (y<my1 || y>=mye || z<mz1 || z>=mze)?++x:((x<mx1 - 1 || x>=mxe)?++x:(x=mxe))) {
- Tt max_val = cimg::type<Tt>::min();
- for (int zm = -mz1; zm<=mz2; ++zm)
- for (int ym = -my1; ym<=my2; ++ym)
- for (int xm = -mx1; xm<=mx2; ++xm) {
- if (K(mx2 - xm,my2 - ym,mz2 - zm)) {
- Tt cval;
- switch (boundary_conditions) {
- case 0 : cval = (Tt)img.atXYZ(x + xm,y + ym,z + zm,0,(T)0); break;
- case 1 : cval = (Tt)img._atXYZ(x + xm,y + ym,z + zm); break;
- case 2 : {
- const int
- nx = cimg::mod(x + xm,width()),
- ny = cimg::mod(y + ym,height()),
- nz = cimg::mod(z + zm,depth());
- cval = img(nx,ny,nz);
- } break;
- default : {
- const int
- tx = cimg::mod(x + xm,w2),
- ty = cimg::mod(y + ym,h2),
- tz = cimg::mod(z + zm,d2),
- nx = tx<width()?tx:w2 - tx - 1,
- ny = ty<height()?ty:h2 - ty - 1,
- nz = tz<depth()?tz:d2 - tz - 1;
- cval = img(nx,ny,nz);
- }
- }
- if (cval>max_val) max_val = cval;
- }
- }
- res(x,y,z,c) = max_val;
- }
- } _cimg_abort_catch_openmp2
- }
- } _cimg_abort_catch_openmp
- cimg_abort_test;
- return res;
- }
- //! Dilate image by a rectangular structuring element of specified size.
- /**
- \param sx Width of the structuring element.
- \param sy Height of the structuring element.
- \param sz Depth of the structuring element.
- **/
- CImg<T>& dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) {
- if (is_empty() || (sx==1 && sy==1 && sz==1)) return *this;
- if (sx>1 && _width>1) { // Along X-axis
- 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;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forYZC(*this,y,z,c) {
- T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(0,y,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- }
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(0,y,z,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(0,y,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- if (sy>1 && _height>1) { // Along Y-axis
- const int L = height(), off = width(), s = (int)sy, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1,
- s2 = _s2>L?L:_s2;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forXZC(*this,x,z,c) {
- T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(x,0,z,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- }
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(x,0,z,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(x,0,z,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- if (sz>1 && _depth>1) { // Along Z-axis
- const int L = depth(), off = width()*height(), s = (int)sz, _s1 = s/2, _s2 = s - _s1, s1 = _s1>L?L:_s1,
- s2 = _s2>L?L:_s2;
- CImg<T> buf(L);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) firstprivate(buf) if (size()>524288))
- cimg_forXYC(*this,x,y,c) {
- T *const ptrdb = buf._data, *ptrd = ptrdb, *const ptrde = buf._data + L - 1;
- const T *const ptrsb = data(x,y,0,c), *ptrs = ptrsb, *const ptrse = ptrs + L*off - off;
- T cur = *ptrs; ptrs+=off; bool is_first = true;
- for (int p = s2 - 1; p>0 && ptrs<=ptrse; --p) {
- const T val = *ptrs; ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- }
- *(ptrd++) = cur;
- if (ptrs>=ptrse) {
- T *pd = data(x,y,0,c); cur = std::max(cur,*ptrse); cimg_forX(buf,k) { *pd = cur; pd+=off; }
- } else {
- for (int p = s1; p>0 && ptrd<=ptrde; --p) {
- const T val = *ptrs; if (ptrs<ptrse) ptrs+=off; if (val>=cur) { cur = val; is_first = false; }
- *(ptrd++) = cur;
- }
- for (int p = L - s - 1; p>0; --p) {
- const T val = *ptrs; ptrs+=off;
- if (is_first) {
- const T *nptrs = ptrs - off; cur = val;
- for (int q = s - 2; q>0; --q) { nptrs-=off; const T nval = *nptrs; if (nval>cur) cur = nval; }
- nptrs-=off; const T nval = *nptrs; if (nval>cur) { cur = nval; is_first = true; } else is_first = false;
- } else { if (val>=cur) cur = val; else if (cur==*(ptrs-s*off)) is_first = true; }
- *(ptrd++) = cur;
- }
- ptrd = ptrde; ptrs = ptrse; cur = *ptrs; ptrs-=off;
- for (int p = s1; p>0 && ptrs>=ptrsb; --p) {
- const T val = *ptrs; ptrs-=off; if (val>cur) cur = val;
- }
- *(ptrd--) = cur;
- for (int p = s2 - 1; p>0 && ptrd>=ptrdb; --p) {
- const T val = *ptrs; if (ptrs>ptrsb) ptrs-=off; if (val>cur) cur = val; *(ptrd--) = cur;
- }
- T *pd = data(x,y,0,c); cimg_for(buf,ps,T) { *pd = *ps; pd+=off; }
- }
- }
- }
- return *this;
- }
- //! Dilate image by a rectangular structuring element of specified size \newinstance.
- CImg<T> get_dilate(const unsigned int sx, const unsigned int sy, const unsigned int sz=1) const {
- return (+*this).dilate(sx,sy,sz);
- }
- //! Dilate image by a square structuring element of specified size.
- /**
- \param s Size of the structuring element.
- **/
- CImg<T>& dilate(const unsigned int s) {
- return dilate(s,s,s);
- }
- //! Dilate image by a square structuring element of specified size \newinstance.
- CImg<T> get_dilate(const unsigned int s) const {
- return (+*this).dilate(s);
- }
- //! Compute watershed transform.
- /**
- \param priority Priority map.
- \param is_high_connectivity Boolean that choose between 4(false)- or 8(true)-connectivity
- in 2D case, and between 6(false)- or 26(true)-connectivity in 3D case.
- \note Non-zero values of the instance instance are propagated to zero-valued ones according to
- specified the priority map.
- **/
- template<typename t>
- CImg<T>& watershed(const CImg<t>& priority, const bool is_high_connectivity=false) {
- #define _cimg_watershed_init(cond,X,Y,Z) \
- if (cond && !(*this)(X,Y,Z)) Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,nb_seeds)
- #define _cimg_watershed_propagate(cond,X,Y,Z) \
- if (cond) { \
- if ((*this)(X,Y,Z)) { \
- ns = labels(X,Y,Z) - 1; xs = seeds(ns,0); ys = seeds(ns,1); zs = seeds(ns,2); \
- d = cimg::sqr((float)x - xs) + cimg::sqr((float)y - ys) + cimg::sqr((float)z - zs); \
- if (d<dmin) { dmin = d; nmin = ns; nlabel = (*this)(xs,ys,zs); } \
- } else Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,n); \
- }
- if (is_empty()) return *this;
- if (!is_sameXYZ(priority))
- throw CImgArgumentException(_cimg_instance
- "watershed(): image instance and specified priority (%u,%u,%u,%u,%p) "
- "have different dimensions.",
- cimg_instance,
- priority._width,priority._height,priority._depth,priority._spectrum,priority._data);
- if (_spectrum!=1) {
- cimg_forC(*this,c)
- get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum));
- return *this;
- }
- CImg<uintT> labels(_width,_height,_depth,1,0), seeds(64,3);
- CImg<typename cimg::superset2<T,t,int>::type> Q;
- unsigned int sizeQ = 0;
- int px, nx, py, ny, pz, nz;
- bool is_px, is_nx, is_py, is_ny, is_pz, is_nz;
- const bool is_3d = _depth>1;
- // Find seed points and insert them in priority queue.
- unsigned int nb_seeds = 0;
- const T *ptrs = _data;
- cimg_forXYZ(*this,x,y,z) if (*(ptrs++)) { // 3D version
- if (nb_seeds>=seeds._width) seeds.resize(2*seeds._width,3,1,1,0);
- seeds(nb_seeds,0) = x; seeds(nb_seeds,1) = y; seeds(nb_seeds++,2) = z;
- px = x - 1; nx = x + 1;
- py = y - 1; ny = y + 1;
- pz = z - 1; nz = z + 1;
- is_px = px>=0; is_nx = nx<width();
- is_py = py>=0; is_ny = ny<height();
- is_pz = pz>=0; is_nz = nz<depth();
- _cimg_watershed_init(is_px,px,y,z);
- _cimg_watershed_init(is_nx,nx,y,z);
- _cimg_watershed_init(is_py,x,py,z);
- _cimg_watershed_init(is_ny,x,ny,z);
- if (is_3d) {
- _cimg_watershed_init(is_pz,x,y,pz);
- _cimg_watershed_init(is_nz,x,y,nz);
- }
- if (is_high_connectivity) {
- _cimg_watershed_init(is_px && is_py,px,py,z);
- _cimg_watershed_init(is_nx && is_py,nx,py,z);
- _cimg_watershed_init(is_px && is_ny,px,ny,z);
- _cimg_watershed_init(is_nx && is_ny,nx,ny,z);
- if (is_3d) {
- _cimg_watershed_init(is_px && is_pz,px,y,pz);
- _cimg_watershed_init(is_nx && is_pz,nx,y,pz);
- _cimg_watershed_init(is_px && is_nz,px,y,nz);
- _cimg_watershed_init(is_nx && is_nz,nx,y,nz);
- _cimg_watershed_init(is_py && is_pz,x,py,pz);
- _cimg_watershed_init(is_ny && is_pz,x,ny,pz);
- _cimg_watershed_init(is_py && is_nz,x,py,nz);
- _cimg_watershed_init(is_ny && is_nz,x,ny,nz);
- _cimg_watershed_init(is_px && is_py && is_pz,px,py,pz);
- _cimg_watershed_init(is_nx && is_py && is_pz,nx,py,pz);
- _cimg_watershed_init(is_px && is_ny && is_pz,px,ny,pz);
- _cimg_watershed_init(is_nx && is_ny && is_pz,nx,ny,pz);
- _cimg_watershed_init(is_px && is_py && is_nz,px,py,nz);
- _cimg_watershed_init(is_nx && is_py && is_nz,nx,py,nz);
- _cimg_watershed_init(is_px && is_ny && is_nz,px,ny,nz);
- _cimg_watershed_init(is_nx && is_ny && is_nz,nx,ny,nz);
- }
- }
- labels(x,y,z) = nb_seeds;
- }
- // Start watershed computation.
- while (sizeQ) {
- // Get and remove point with maximal priority from the queue.
- const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3);
- const unsigned int n = labels(x,y,z);
- px = x - 1; nx = x + 1;
- py = y - 1; ny = y + 1;
- pz = z - 1; nz = z + 1;
- is_px = px>=0; is_nx = nx<width();
- is_py = py>=0; is_ny = ny<height();
- is_pz = pz>=0; is_nz = nz<depth();
- // Check labels of the neighbors.
- Q._priority_queue_remove(sizeQ);
- unsigned int xs, ys, zs, ns, nmin = 0;
- float d, dmin = cimg::type<float>::inf();
- T nlabel = (T)0;
- _cimg_watershed_propagate(is_px,px,y,z);
- _cimg_watershed_propagate(is_nx,nx,y,z);
- _cimg_watershed_propagate(is_py,x,py,z);
- _cimg_watershed_propagate(is_ny,x,ny,z);
- if (is_3d) {
- _cimg_watershed_propagate(is_pz,x,y,pz);
- _cimg_watershed_propagate(is_nz,x,y,nz);
- }
- if (is_high_connectivity) {
- _cimg_watershed_propagate(is_px && is_py,px,py,z);
- _cimg_watershed_propagate(is_nx && is_py,nx,py,z);
- _cimg_watershed_propagate(is_px && is_ny,px,ny,z);
- _cimg_watershed_propagate(is_nx && is_ny,nx,ny,z);
- if (is_3d) {
- _cimg_watershed_propagate(is_px && is_pz,px,y,pz);
- _cimg_watershed_propagate(is_nx && is_pz,nx,y,pz);
- _cimg_watershed_propagate(is_px && is_nz,px,y,nz);
- _cimg_watershed_propagate(is_nx && is_nz,nx,y,nz);
- _cimg_watershed_propagate(is_py && is_pz,x,py,pz);
- _cimg_watershed_propagate(is_ny && is_pz,x,ny,pz);
- _cimg_watershed_propagate(is_py && is_nz,x,py,nz);
- _cimg_watershed_propagate(is_ny && is_nz,x,ny,nz);
- _cimg_watershed_propagate(is_px && is_py && is_pz,px,py,pz);
- _cimg_watershed_propagate(is_nx && is_py && is_pz,nx,py,pz);
- _cimg_watershed_propagate(is_px && is_ny && is_pz,px,ny,pz);
- _cimg_watershed_propagate(is_nx && is_ny && is_pz,nx,ny,pz);
- _cimg_watershed_propagate(is_px && is_py && is_nz,px,py,nz);
- _cimg_watershed_propagate(is_nx && is_py && is_nz,nx,py,nz);
- _cimg_watershed_propagate(is_px && is_ny && is_nz,px,ny,nz);
- _cimg_watershed_propagate(is_nx && is_ny && is_nz,nx,ny,nz);
- }
- }
- (*this)(x,y,z) = nlabel;
- labels(x,y,z) = ++nmin;
- }
- return *this;
- }
- //! Compute watershed transform \newinstance.
- template<typename t>
- CImg<T> get_watershed(const CImg<t>& priority, const bool is_high_connectivity=false) const {
- return (+*this).watershed(priority,is_high_connectivity);
- }
- // [internal] Insert/Remove items in priority queue, for watershed/distance transforms.
- template<typename tq, typename tv>
- bool _priority_queue_insert(CImg<tq>& is_queued, unsigned int& siz, const tv value,
- const unsigned int x, const unsigned int y, const unsigned int z,
- const unsigned int n=1) {
- if (is_queued(x,y,z)) return false;
- is_queued(x,y,z) = (tq)n;
- if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); }
- (*this)(siz - 1,0) = (T)value;
- (*this)(siz - 1,1) = (T)x;
- (*this)(siz - 1,2) = (T)y;
- (*this)(siz - 1,3) = (T)z;
- for (unsigned int pos = siz - 1, par = 0; pos && value>(tv)(*this)(par=(pos + 1)/2 - 1,0); pos = par) {
- cimg::swap((*this)(pos,0),(*this)(par,0));
- cimg::swap((*this)(pos,1),(*this)(par,1));
- cimg::swap((*this)(pos,2),(*this)(par,2));
- cimg::swap((*this)(pos,3),(*this)(par,3));
- }
- return true;
- }
- CImg<T>& _priority_queue_remove(unsigned int& siz) {
- (*this)(0,0) = (*this)(--siz,0);
- (*this)(0,1) = (*this)(siz,1);
- (*this)(0,2) = (*this)(siz,2);
- (*this)(0,3) = (*this)(siz,3);
- const float value = (*this)(0,0);
- unsigned int pos = 0, swap = 0;
- do {
- const unsigned int left = 2*pos + 1, right = left + 1;
- if (right<siz && value<(*this)(right,0)) swap = (*this)(left,0)>(*this)(right,0)?left:right;
- else if (left<siz && value<(*this)(left,0)) swap = left;
- else break;
- cimg::swap((*this)(pos,0),(*this)(swap,0));
- cimg::swap((*this)(pos,1),(*this)(swap,1));
- cimg::swap((*this)(pos,2),(*this)(swap,2));
- cimg::swap((*this)(pos,3),(*this)(swap,3));
- pos = swap;
- } while (true);
- return *this;
- }
- //! Apply recursive Deriche filter.
- /**
- \param sigma Standard deviation of the filter.
- \param order Order of the filter. Can be <tt>{ 0=smooth-filter | 1=1st-derivative | 2=2nd-derivative }</tt>.
- \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- **/
- CImg<T>& deriche(const float sigma, const unsigned int order=0, const char axis='x',
- const unsigned int boundary_conditions=1) {
- #define _cimg_deriche_apply \
- CImg<doubleT> Y(N); \
- double *ptrY = Y._data, yb = 0, yp = 0; \
- T xp = (T)0; \
- if (boundary_conditions) { xp = *ptrX; yb = yp = (double)(coefp*xp); } \
- for (int m = 0; m<N; ++m) { \
- const T xc = *ptrX; ptrX+=off; \
- const double yc = *(ptrY++) = (double)(a0*xc + a1*xp - b1*yp - b2*yb); \
- xp = xc; yb = yp; yp = yc; \
- } \
- T xn = (T)0, xa = (T)0; \
- double yn = 0, ya = 0; \
- if (boundary_conditions) { xn = xa = *(ptrX - off); yn = ya = (double)coefn*xn; } \
- for (int n = N - 1; n>=0; --n) { \
- const T xc = *(ptrX-=off); \
- const double yc = (double)(a2*xn + a3*xa - b1*yn - b2*ya); \
- xa = xn; xn = xc; ya = yn; yn = yc; \
- *ptrX = (T)(*(--ptrY)+yc); \
- }
- if (order>2)
- throw CImgArgumentException(_cimg_instance
- "deriche(): Invalid specified order '%d' "
- "('order' can be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).",
- cimg_instance,
- order);
- const char naxis = cimg::lowercase(axis);
- if (naxis!='x' && naxis!='y' && naxis!='z' && naxis!='c')
- throw CImgArgumentException(_cimg_instance
- "deriche(): Invalid specified axis '%c'.",
- cimg_instance,
- axis);
- const double
- nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:
- naxis=='y'?_height:
- naxis=='z'?_depth:_spectrum)/100,
- nnsigma = nsigma<0.1f?0.1f:nsigma;
- if (is_empty() || (nsigma<0.1f && !order)) return *this;
- if (boundary_conditions>1) {
- const int w = width(), h = height(), d = depth(), s = spectrum(), border = (int)cimg::round(1 + 3*nnsigma);
- switch (naxis) {
- case 'x' :
- return draw_image(get_resize(w + 2*border,h,d,s,0,boundary_conditions,0.5).
- deriche(nnsigma,order,naxis,1).columns(border,w - 1 + border));
- case 'y' :
- return draw_image(get_resize(w,h + 2*border,d,s,0,boundary_conditions,0,0.5).
- deriche(nnsigma,order,naxis,1).rows(border,h - 1 + border));
- case 'z' :
- return draw_image(get_resize(w,h,d + 2*border,s,0,boundary_conditions,0,0,0.5).
- deriche(nnsigma,order,naxis,1).slices(border,d - 1 + border));
- default :
- return draw_image(get_resize(w,h,d,s + 2*border,0,boundary_conditions,0,0,0,0.5).
- deriche(nnsigma,order,naxis,1).channels(border,d - 1 + border));
- }
- }
- const double
- alpha = 1.695f/nnsigma,
- ema = std::exp(-alpha),
- ema2 = std::exp(-2*alpha),
- b1 = -2*ema,
- b2 = ema2;
- double a0 = 0, a1 = 0, a2 = 0, a3 = 0, coefp = 0, coefn = 0;
- switch (order) {
- case 0 : {
- const double k = (1-ema)*(1-ema)/(1 + 2*alpha*ema-ema2);
- a0 = k;
- a1 = k*(alpha - 1)*ema;
- a2 = k*(alpha + 1)*ema;
- a3 = -k*ema2;
- } break;
- case 1 : {
- const double k = -(1-ema)*(1-ema)*(1-ema)/(2*(ema + 1)*ema);
- a0 = a3 = 0;
- a1 = k*ema;
- a2 = -a1;
- } break;
- default : {
- const double
- ea = std::exp(-alpha),
- k = -(ema2 - 1)/(2*alpha*ema),
- kn = (-2*(-1 + 3*ea - 3*ea*ea + ea*ea*ea)/(3*ea + 1 + 3*ea*ea + ea*ea*ea));
- a0 = kn;
- a1 = -kn*(1 + k*alpha)*ema;
- a2 = kn*(1 - k*alpha)*ema;
- a3 = -kn*ema2;
- } break;
- }
- coefp = (a0 + a1)/(1 + b1 + b2);
- coefn = (a2 + a3)/(1 + b1 + b2);
- switch (naxis) {
- case 'x' : {
- const int N = width();
- const ulongT off = 1U;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forYZC(*this,y,z,c) { T *ptrX = data(0,y,z,c); _cimg_deriche_apply; }
- } break;
- case 'y' : {
- const int N = height();
- const ulongT off = (ulongT)_width;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXZC(*this,x,z,c) { T *ptrX = data(x,0,z,c); _cimg_deriche_apply; }
- } break;
- case 'z' : {
- const int N = depth();
- const ulongT off = (ulongT)_width*_height;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYC(*this,x,y,c) { T *ptrX = data(x,y,0,c); _cimg_deriche_apply; }
- } break;
- default : {
- const int N = spectrum();
- const ulongT off = (ulongT)_width*_height*_depth;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYZ(*this,x,y,z) { T *ptrX = data(x,y,z,0); _cimg_deriche_apply; }
- }
- }
- return *this;
- }
- //! Apply recursive Deriche filter \newinstance.
- CImg<Tfloat> get_deriche(const float sigma, const unsigned int order=0, const char axis='x',
- const unsigned int boundary_conditions=1) const {
- return CImg<Tfloat>(*this,false).deriche(sigma,order,axis,boundary_conditions);
- }
- // [internal] Apply a recursive filter (used by CImg<T>::vanvliet()).
- /*
- \param ptr the pointer of the data
- \param filter the coefficient of the filter in the following order [n,n - 1,n - 2,n - 3].
- \param N size of the data
- \param off the offset between two data point
- \param order the order of the filter 0 (smoothing), 1st derivative, 2nd derivative, 3rd derivative
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann }</tt>.
- \note Boundary condition using B. Triggs method (IEEE trans on Sig Proc 2005).
- */
- static void _cimg_recursive_apply(T *data, const double filter[], const int N, const ulongT off,
- const unsigned int order, const bool boundary_conditions) {
- double val[4] = { 0 }; // res[n,n - 1,n - 2,n - 3,..] or res[n,n + 1,n + 2,n + 3,..]
- const double
- sumsq = filter[0], sum = sumsq * sumsq,
- a1 = filter[1], a2 = filter[2], a3 = filter[3],
- scaleM = 1. / ( (1. + a1 - a2 + a3) * (1. - a1 - a2 - a3) * (1. + a2 + (a1 - a3) * a3) );
- double M[9]; // Triggs matrix
- M[0] = scaleM * (-a3 * a1 + 1. - a3 * a3 - a2);
- M[1] = scaleM * (a3 + a1) * (a2 + a3 * a1);
- M[2] = scaleM * a3 * (a1 + a3 * a2);
- M[3] = scaleM * (a1 + a3 * a2);
- M[4] = -scaleM * (a2 - 1.) * (a2 + a3 * a1);
- M[5] = -scaleM * a3 * (a3 * a1 + a3 * a3 + a2 - 1.);
- M[6] = scaleM * (a3 * a1 + a2 + a1 * a1 - a2 * a2);
- M[7] = scaleM * (a1 * a2 + a3 * a2 * a2 - a1 * a3 * a3 - a3 * a3 * a3 - a3 * a2 + a3);
- M[8] = scaleM * a3 * (a1 + a3 * a2);
- switch (order) {
- case 0 : {
- const double iplus = (boundary_conditions?data[(N - 1)*off]:(T)0);
- for (int pass = 0; pass<2; ++pass) {
- if (!pass) {
- for (int k = 1; k<4; ++k) val[k] = (boundary_conditions?*data/sumsq:0);
- } else {
- // Apply Triggs boundary conditions
- const double
- uplus = iplus/(1. - a1 - a2 - a3), vplus = uplus/(1. - a1 - a2 - a3),
- unp = val[1] - uplus, unp1 = val[2] - uplus, unp2 = val[3] - uplus;
- val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2 + vplus) * sum;
- val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2 + vplus) * sum;
- val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2 + vplus) * sum;
- *data = (T)val[0];
- data -= off;
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- for (int n = pass; n<N; ++n) {
- val[0] = (*data);
- if (pass) val[0] *= sum;
- for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
- *data = (T)val[0];
- if (!pass) data += off; else data -= off;
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- if (!pass) data -= off;
- }
- } break;
- case 1 : {
- double x[3]; // [front,center,back]
- for (int pass = 0; pass<2; ++pass) {
- if (!pass) {
- for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
- for (int k = 0; k<4; ++k) val[k] = 0;
- } else {
- // Apply Triggs boundary conditions
- const double
- unp = val[1], unp1 = val[2], unp2 = val[3];
- val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
- val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
- val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
- *data = (T)val[0];
- data -= off;
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- for (int n = pass; n<N - 1; ++n) {
- if (!pass) {
- x[0] = *(data + off);
- val[0] = 0.5f * (x[0] - x[2]);
- } else val[0] = (*data) * sum;
- for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
- *data = (T)val[0];
- if (!pass) {
- data += off;
- for (int k = 2; k>0; --k) x[k] = x[k - 1];
- } else { data-=off;}
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- *data = (T)0;
- }
- } break;
- case 2: {
- double x[3]; // [front,center,back]
- for (int pass = 0; pass<2; ++pass) {
- if (!pass) {
- for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
- for (int k = 0; k<4; ++k) val[k] = 0;
- } else {
- // Apply Triggs boundary conditions
- const double
- unp = val[1], unp1 = val[2], unp2 = val[3];
- val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
- val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
- val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
- *data = (T)val[0];
- data -= off;
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- for (int n = pass; n<N - 1; ++n) {
- if (!pass) { x[0] = *(data + off); val[0] = (x[1] - x[2]); }
- else { x[0] = *(data - off); val[0] = (x[2] - x[1]) * sum; }
- for (int k = 1; k<4; ++k) val[0] += val[k]*filter[k];
- *data = (T)val[0];
- if (!pass) data += off; else data -= off;
- for (int k = 2; k>0; --k) x[k] = x[k - 1];
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- *data = (T)0;
- }
- } break;
- case 3: {
- double x[3]; // [front,center,back]
- for (int pass = 0; pass<2; ++pass) {
- if (!pass) {
- for (int k = 0; k<3; ++k) x[k] = (boundary_conditions?*data:(T)0);
- for (int k = 0; k<4; ++k) val[k] = 0;
- } else {
- // Apply Triggs boundary conditions
- const double
- unp = val[1], unp1 = val[2], unp2 = val[3];
- val[0] = (M[0] * unp + M[1] * unp1 + M[2] * unp2) * sum;
- val[1] = (M[3] * unp + M[4] * unp1 + M[5] * unp2) * sum;
- val[2] = (M[6] * unp + M[7] * unp1 + M[8] * unp2) * sum;
- *data = (T)val[0];
- data -= off;
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- for (int n = pass; n<N - 1; ++n) {
- if (!pass) { x[0] = *(data + off); val[0] = (x[0] - 2*x[1] + x[2]); }
- else { x[0] = *(data - off); val[0] = 0.5f * (x[2] - x[0]) * sum; }
- for (int k = 1; k<4; ++k) val[0] += val[k] * filter[k];
- *data = (T)val[0];
- if (!pass) data += off; else data -= off;
- for (int k = 2; k>0; --k) x[k] = x[k - 1];
- for (int k = 3; k>0; --k) val[k] = val[k - 1];
- }
- *data = (T)0;
- }
- } break;
- }
- }
- //! Van Vliet recursive Gaussian filter.
- /**
- \param sigma standard deviation of the Gaussian filter
- \param order the order of the filter 0,1,2,3
- \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \note dirichlet boundary condition has a strange behavior
- I.T. Young, L.J. van Vliet, M. van Ginkel, Recursive Gabor filtering.
- IEEE Trans. Sig. Proc., vol. 50, pp. 2799-2805, 2002.
- (this is an improvement over Young-Van Vliet, Sig. Proc. 44, 1995)
- Boundary conditions (only for order 0) using Triggs matrix, from
- B. Triggs and M. Sdika. Boundary conditions for Young-van Vliet
- recursive filtering. IEEE Trans. Signal Processing,
- vol. 54, pp. 2365-2367, 2006.
- **/
- CImg<T>& vanvliet(const float sigma, const unsigned int order, const char axis='x',
- const unsigned int boundary_conditions=1) {
- if (order>2)
- throw CImgArgumentException(_cimg_instance
- "deriche(): Invalid specified order '%d' "
- "('order' can be { 0=smoothing | 1=1st-derivative | 2=2nd-derivative }).",
- cimg_instance,
- order);
- const char naxis = cimg::lowercase(axis);
- if (naxis!='x' && naxis!='y' && naxis!='z' && naxis!='c')
- throw CImgArgumentException(_cimg_instance
- "deriche(): Invalid specified axis '%c'.",
- cimg_instance,
- axis);
- const double
- nsigma = sigma>=0?sigma:-sigma*(naxis=='x'?_width:
- naxis=='y'?_height:
- naxis=='z'?_depth:_spectrum)/100,
- nnsigma = nsigma<0.5f?0.5f:nsigma;
- if (is_empty() || (nsigma<0.1f && !order)) return *this;
- if (nsigma<0.5f) return deriche(nsigma,order,axis,boundary_conditions);
- if (!cimg::type<T>::is_float())
- return CImg<Tfloat>(*this,false).vanvliet(sigma,order,axis,boundary_conditions).move_to(*this);
- if (boundary_conditions>1) {
- const int w = width(), h = height(), d = depth(), s = spectrum(), border = (int)cimg::round(1 + 3*nnsigma);
- switch (naxis) {
- case 'x' :
- return draw_image(get_resize(w + 2*border,h,d,s,0,boundary_conditions,0.5).
- vanvliet(nnsigma,order,naxis,1).columns(border,w - 1 + border));
- case 'y' :
- return draw_image(get_resize(w,h + 2*border,d,s,0,boundary_conditions,0,0.5).
- vanvliet(nnsigma,order,naxis,1).rows(border,h - 1 + border));
- case 'z' :
- return draw_image(get_resize(w,h,d + 2*border,s,0,boundary_conditions,0,0,0.5).
- vanvliet(nnsigma,order,naxis,1).slices(border,d - 1 + border));
- default :
- return draw_image(get_resize(w,h,d,s + 2*border,0,boundary_conditions,0,0,0,0.5).
- vanvliet(nnsigma,order,naxis,1).channels(border,d - 1 + border));
- }
- }
- const double
- m0 = 1.16680, m1 = 1.10783, m2 = 1.40586,
- m1sq = m1 * m1, m2sq = m2 * m2,
- q = (nnsigma<3.556?-0.2568 + 0.5784*nnsigma + 0.0561*nnsigma*nnsigma:2.5091 + 0.9804*(nnsigma - 3.556)),
- qsq = q * q,
- scale = (m0 + q) * (m1sq + m2sq + 2 * m1 * q + qsq),
- b1 = -q * (2 * m0 * m1 + m1sq + m2sq + (2 * m0 + 4 * m1) * q + 3 * qsq) / scale,
- b2 = qsq * (m0 + 2 * m1 + 3 * q) / scale,
- b3 = -qsq * q / scale,
- B = ( m0 * (m1sq + m2sq) ) / scale;
- double filter[4];
- filter[0] = B; filter[1] = -b1; filter[2] = -b2; filter[3] = -b3;
- switch (naxis) {
- case 'x' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forYZC(*this,y,z,c)
- _cimg_recursive_apply(data(0,y,z,c),filter,_width,1U,order,boundary_conditions);
- } break;
- case 'y' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXZC(*this,x,z,c)
- _cimg_recursive_apply(data(x,0,z,c),filter,_height,(ulongT)_width,order,boundary_conditions);
- } break;
- case 'z' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYC(*this,x,y,c)
- _cimg_recursive_apply(data(x,y,0,c),filter,_depth,(ulongT)_width*_height,
- order,boundary_conditions);
- } break;
- default : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYZ(*this,x,y,z)
- _cimg_recursive_apply(data(x,y,z,0),filter,_spectrum,(ulongT)_width*_height*_depth,
- order,boundary_conditions);
- }
- }
- return *this;
- }
- //! Blur image using Van Vliet recursive Gaussian filter. \newinstance.
- CImg<Tfloat> get_vanvliet(const float sigma, const unsigned int order, const char axis='x',
- const unsigned int boundary_conditions=1) const {
- return CImg<Tfloat>(*this,false).vanvliet(sigma,order,axis,boundary_conditions);
- }
- //! Blur image.
- /**
- \param sigma_x Standard deviation of the blur, along the X-axis.
- \param sigma_y Standard deviation of the blur, along the Y-axis.
- \param sigma_z Standard deviation of the blur, along the Z-axis.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \param is_gaussian Tells if the blur uses a gaussian (\c true) or quasi-gaussian (\c false) kernel.
- \note
- - The blur is computed as a 0-order Vanvliet (gaussian) or Deriche filter (quasi-gaussian).
- - This is a recursive algorithm, not depending on the values of the standard deviations.
- \see deriche(), vanvliet().
- **/
- CImg<T>& blur(const float sigma_x, const float sigma_y, const float sigma_z,
- const unsigned int boundary_conditions=1, const bool is_gaussian=true) {
- if (is_empty()) return *this;
- if (is_gaussian) {
- if (_width>1) vanvliet(sigma_x,0,'x',boundary_conditions);
- if (_height>1) vanvliet(sigma_y,0,'y',boundary_conditions);
- if (_depth>1) vanvliet(sigma_z,0,'z',boundary_conditions);
- } else {
- if (_width>1) deriche(sigma_x,0,'x',boundary_conditions);
- if (_height>1) deriche(sigma_y,0,'y',boundary_conditions);
- if (_depth>1) deriche(sigma_z,0,'z',boundary_conditions);
- }
- return *this;
- }
- //! Blur image \newinstance.
- CImg<Tfloat> get_blur(const float sigma_x, const float sigma_y, const float sigma_z,
- const unsigned int boundary_conditions=1, const bool is_gaussian=true) const {
- return CImg<Tfloat>(*this,false).blur(sigma_x,sigma_y,sigma_z,boundary_conditions,is_gaussian);
- }
- //! Blur image isotropically.
- /**
- \param sigma Standard deviation of the blur.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.a
- \param is_gaussian Use a gaussian kernel (VanVliet) is set, a quasi-gaussian (Deriche) otherwise.
- \see deriche(), vanvliet().
- **/
- CImg<T>& blur(const float sigma, const unsigned int boundary_conditions=1, const bool is_gaussian=true) {
- const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100;
- return blur(nsigma,nsigma,nsigma,boundary_conditions,is_gaussian);
- }
- //! Blur image isotropically \newinstance.
- CImg<Tfloat> get_blur(const float sigma, const unsigned int boundary_conditions=1,
- const bool is_gaussian=true) const {
- return CImg<Tfloat>(*this,false).blur(sigma,boundary_conditions,is_gaussian);
- }
- //! Blur image anisotropically, directed by a field of diffusion tensors.
- /**
- \param G Field of square roots of diffusion tensors/vectors used to drive the smoothing.
- \param amplitude Amplitude of the smoothing.
- \param dl Spatial discretization.
- \param da Angular discretization.
- \param gauss_prec Precision of the diffusion process.
- \param interpolation_type Interpolation scheme.
- Can be <tt>{ 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }</tt>.
- \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
- **/
- template<typename t>
- CImg<T>& blur_anisotropic(const CImg<t>& G,
- const float amplitude=60, const float dl=0.8f, const float da=30,
- const float gauss_prec=2, const unsigned int interpolation_type=0,
- const bool is_fast_approx=1) {
- // Check arguments and init variables
- if (!is_sameXYZ(G) || (G._spectrum!=3 && G._spectrum!=6))
- throw CImgArgumentException(_cimg_instance
- "blur_anisotropic(): Invalid specified diffusion tensor field (%u,%u,%u,%u,%p).",
- cimg_instance,
- G._width,G._height,G._depth,G._spectrum,G._data);
- if (is_empty() || dl<0) return *this;
- const float namplitude = amplitude>=0?amplitude:-amplitude*cimg::max(_width,_height,_depth)/100;
- unsigned int iamplitude = cimg::round(namplitude);
- const bool is_3d = (G._spectrum==6);
- T val_min, val_max = max_min(val_min);
- _cimg_abort_init_openmp;
- cimg_abort_init;
- if (da<=0) { // Iterated oriented Laplacians
- CImg<Tfloat> velocity(_width,_height,_depth,_spectrum);
- for (unsigned int iteration = 0; iteration<iamplitude; ++iteration) {
- Tfloat *ptrd = velocity._data, veloc_max = 0;
- if (is_3d) // 3D version
- cimg_forC(*this,c) {
- cimg_abort_test;
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat
- ixx = Incc + Ipcc - 2*Iccc,
- ixy = (Innc + Ippc - Inpc - Ipnc)/4,
- ixz = (Incn + Ipcp - Incp - Ipcn)/4,
- iyy = Icnc + Icpc - 2*Iccc,
- iyz = (Icnn + Icpp - Icnp - Icpn)/4,
- izz = Iccn + Iccp - 2*Iccc,
- veloc = (Tfloat)(G(x,y,z,0)*ixx + 2*G(x,y,z,1)*ixy + 2*G(x,y,z,2)*ixz +
- G(x,y,z,3)*iyy + 2*G(x,y,z,4)*iyz + G(x,y,z,5)*izz);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- }
- else // 2D version
- cimg_forZC(*this,z,c) {
- cimg_abort_test;
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat
- ixx = Inc + Ipc - 2*Icc,
- ixy = (Inn + Ipp - Inp - Ipn)/4,
- iyy = Icn + Icp - 2*Icc,
- veloc = (Tfloat)(G(x,y,0,0)*ixx + 2*G(x,y,0,1)*ixy + G(x,y,0,2)*iyy);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- }
- if (veloc_max>0) *this+=(velocity*=dl/veloc_max);
- }
- } else { // LIC-based smoothing
- const ulongT whd = (ulongT)_width*_height*_depth;
- const float sqrt2amplitude = (float)std::sqrt(2*namplitude);
- const int dx1 = width() - 1, dy1 = height() - 1, dz1 = depth() - 1;
- CImg<Tfloat> res(_width,_height,_depth,_spectrum,0), W(_width,_height,_depth,is_3d?4:3), val(_spectrum,1,1,1,0);
- int N = 0;
- if (is_3d) { // 3D version
- for (float phi = cimg::mod(180.f,da)/2.f; phi<=180; phi+=da) {
- const float phir = (float)(phi*cimg::PI/180), datmp = (float)(da/std::cos(phir)),
- da2 = datmp<1?360.f:datmp;
- for (float theta = 0; theta<360; (theta+=da2),++N) {
- const float
- thetar = (float)(theta*cimg::PI/180),
- vx = (float)(std::cos(thetar)*std::cos(phir)),
- vy = (float)(std::sin(thetar)*std::cos(phir)),
- vz = (float)std::sin(phir);
- const t
- *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2),
- *pd = G.data(0,0,0,3), *pe = G.data(0,0,0,4), *pf = G.data(0,0,0,5);
- 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);
- cimg_forXYZ(G,xg,yg,zg) {
- const t a = *(pa++), b = *(pb++), c = *(pc++), d = *(pd++), e = *(pe++), f = *(pf++);
- const float
- u = (float)(a*vx + b*vy + c*vz),
- v = (float)(b*vx + d*vy + e*vz),
- w = (float)(c*vx + e*vy + f*vz),
- n = 1e-5f + cimg::hypot(u,v,w),
- dln = dl/n;
- *(pd0++) = (Tfloat)(u*dln);
- *(pd1++) = (Tfloat)(v*dln);
- *(pd2++) = (Tfloat)(w*dln);
- *(pd3++) = (Tfloat)n;
- }
- cimg_abort_test;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 && _height*_depth>=2)
- firstprivate(val))
- cimg_forYZ(*this,y,z) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- cimg_forX(*this,x) {
- val.fill(0);
- const float
- n = (float)W(x,y,z,3),
- fsigma = (float)(n*sqrt2amplitude),
- fsigma2 = 2*fsigma*fsigma,
- length = gauss_prec*fsigma;
- float
- S = 0,
- X = (float)x,
- Y = (float)y,
- Z = (float)z;
- switch (interpolation_type) {
- case 0 : { // Nearest neighbor
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
- const int
- cx = (int)(X + 0.5f),
- cy = (int)(Y + 0.5f),
- cz = (int)(Z + 0.5f);
- const float
- u = (float)W(cx,cy,cz,0),
- v = (float)W(cx,cy,cz,1),
- w = (float)W(cx,cy,cz,2);
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,cz,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,cz,c));
- S+=coef;
- }
- X+=u; Y+=v; Z+=w;
- }
- } break;
- case 1 : { // Linear interpolation
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
- const float
- u = (float)(W._linear_atXYZ(X,Y,Z,0)),
- v = (float)(W._linear_atXYZ(X,Y,Z,1)),
- w = (float)(W._linear_atXYZ(X,Y,Z,2));
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c));
- S+=coef;
- }
- X+=u; Y+=v; Z+=w;
- }
- } break;
- default : { // 2nd order Runge Kutta
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1 && Z>=0 && Z<=dz1; l+=dl) {
- const float
- u0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,0)),
- v0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,1)),
- w0 = (float)(0.5f*W._linear_atXYZ(X,Y,Z,2)),
- u = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,0)),
- v = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,1)),
- w = (float)(W._linear_atXYZ(X + u0,Y + v0,Z + w0,2));
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXYZ(X,Y,Z,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXYZ(X,Y,Z,c));
- S+=coef;
- }
- X+=u; Y+=v; Z+=w;
- }
- } break;
- }
- Tfloat *ptrd = res.data(x,y,z);
- if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; }
- else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,z,c)); ptrd+=whd; }
- }
- } _cimg_abort_catch_openmp2
- }
- }
- } else { // 2D LIC algorithm
- for (float theta = cimg::mod(360.f,da)/2.f; theta<360; (theta+=da),++N) {
- const float thetar = (float)(theta*cimg::PI/180),
- vx = (float)(std::cos(thetar)), vy = (float)(std::sin(thetar));
- const t *pa = G.data(0,0,0,0), *pb = G.data(0,0,0,1), *pc = G.data(0,0,0,2);
- Tfloat *pd0 = W.data(0,0,0,0), *pd1 = W.data(0,0,0,1), *pd2 = W.data(0,0,0,2);
- cimg_forXY(G,xg,yg) {
- const t a = *(pa++), b = *(pb++), c = *(pc++);
- const float
- u = (float)(a*vx + b*vy),
- v = (float)(b*vx + c*vy),
- n = std::max(1e-5f,cimg::hypot(u,v)),
- dln = dl/n;
- *(pd0++) = (Tfloat)(u*dln);
- *(pd1++) = (Tfloat)(v*dln);
- *(pd2++) = (Tfloat)n;
- }
- cimg_abort_test;
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 && _height>=2)
- firstprivate(val))
- cimg_forY(*this,y) _cimg_abort_try_openmp2 {
- cimg_abort_test2;
- cimg_forX(*this,x) {
- val.fill(0);
- const float
- n = (float)W(x,y,0,2),
- fsigma = (float)(n*sqrt2amplitude),
- fsigma2 = 2*fsigma*fsigma,
- length = gauss_prec*fsigma;
- float
- S = 0,
- X = (float)x,
- Y = (float)y;
- switch (interpolation_type) {
- case 0 : { // Nearest-neighbor
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
- const int
- cx = (int)(X + 0.5f),
- cy = (int)(Y + 0.5f);
- const float
- u = (float)W(cx,cy,0,0),
- v = (float)W(cx,cy,0,1);
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)(*this)(cx,cy,0,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*(*this)(cx,cy,0,c));
- S+=coef;
- }
- X+=u; Y+=v;
- }
- } break;
- case 1 : { // Linear interpolation
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
- const float
- u = (float)(W._linear_atXY(X,Y,0,0)),
- v = (float)(W._linear_atXY(X,Y,0,1));
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c));
- S+=coef;
- }
- X+=u; Y+=v;
- }
- } break;
- default : { // 2nd-order Runge-kutta interpolation
- for (float l = 0; l<length && X>=0 && X<=dx1 && Y>=0 && Y<=dy1; l+=dl) {
- const float
- u0 = (float)(0.5f*W._linear_atXY(X,Y,0,0)),
- v0 = (float)(0.5f*W._linear_atXY(X,Y,0,1)),
- u = (float)(W._linear_atXY(X + u0,Y + v0,0,0)),
- v = (float)(W._linear_atXY(X + u0,Y + v0,0,1));
- if (is_fast_approx) { cimg_forC(*this,c) val[c]+=(Tfloat)_linear_atXY(X,Y,0,c); ++S; }
- else {
- const float coef = (float)std::exp(-l*l/fsigma2);
- cimg_forC(*this,c) val[c]+=(Tfloat)(coef*_linear_atXY(X,Y,0,c));
- S+=coef;
- }
- X+=u; Y+=v;
- }
- }
- }
- Tfloat *ptrd = res.data(x,y);
- if (S>0) cimg_forC(res,c) { *ptrd+=val[c]/S; ptrd+=whd; }
- else cimg_forC(res,c) { *ptrd+=(Tfloat)((*this)(x,y,0,c)); ptrd+=whd; }
- }
- } _cimg_abort_catch_openmp2
- }
- }
- const Tfloat *ptrs = res._data;
- cimg_for(*this,ptrd,T) {
- const Tfloat _val = *(ptrs++)/N;
- *ptrd = _val<val_min?val_min:(_val>val_max?val_max:(T)_val);
- }
- }
- cimg_abort_test;
- return *this;
- }
- //! Blur image anisotropically, directed by a field of diffusion tensors \newinstance.
- template<typename t>
- CImg<Tfloat> get_blur_anisotropic(const CImg<t>& G,
- const float amplitude=60, const float dl=0.8f, const float da=30,
- const float gauss_prec=2, const unsigned int interpolation_type=0,
- const bool is_fast_approx=true) const {
- return CImg<Tfloat>(*this,false).blur_anisotropic(G,amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx);
- }
- //! Blur image anisotropically, in an edge-preserving way.
- /**
- \param amplitude Amplitude of the smoothing.
- \param sharpness Sharpness.
- \param anisotropy Anisotropy.
- \param alpha Standard deviation of the gradient blur.
- \param sigma Standard deviation of the structure tensor blur.
- \param dl Spatial discretization.
- \param da Angular discretization.
- \param gauss_prec Precision of the diffusion process.
- \param interpolation_type Interpolation scheme.
- Can be <tt>{ 0=nearest-neighbor | 1=linear | 2=Runge-Kutta }</tt>.
- \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
- **/
- CImg<T>& blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f,
- const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f, const float da=30,
- const float gauss_prec=2, const unsigned int interpolation_type=0,
- const bool is_fast_approx=true) {
- const float nalpha = alpha>=0?alpha:-alpha*cimg::max(_width,_height,_depth)/100;
- const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100;
- return blur_anisotropic(get_diffusion_tensors(sharpness,anisotropy,nalpha,nsigma,interpolation_type!=3),
- amplitude,dl,da,gauss_prec,interpolation_type,is_fast_approx);
- }
- //! Blur image anisotropically, in an edge-preserving way \newinstance.
- CImg<Tfloat> get_blur_anisotropic(const float amplitude, const float sharpness=0.7f, const float anisotropy=0.6f,
- const float alpha=0.6f, const float sigma=1.1f, const float dl=0.8f,
- const float da=30, const float gauss_prec=2,
- const unsigned int interpolation_type=0,
- const bool is_fast_approx=true) const {
- return CImg<Tfloat>(*this,false).blur_anisotropic(amplitude,sharpness,anisotropy,alpha,sigma,dl,da,gauss_prec,
- interpolation_type,is_fast_approx);
- }
- //! Blur image, with the joint bilateral filter.
- /**
- \param guide Image used to model the smoothing weights.
- \param sigma_x Amount of blur along the X-axis.
- \param sigma_y Amount of blur along the Y-axis.
- \param sigma_z Amount of blur along the Z-axis.
- \param sigma_r Amount of blur along the value axis.
- \param sampling_x Amount of downsampling along the X-axis used for the approximation.
- Defaults (0) to sigma_x.
- \param sampling_y Amount of downsampling along the Y-axis used for the approximation.
- Defaults (0) to sigma_y.
- \param sampling_z Amount of downsampling along the Z-axis used for the approximation.
- Defaults (0) to sigma_z.
- \param sampling_r Amount of downsampling along the value axis used for the approximation.
- Defaults (0) to sigma_r.
- \note This algorithm uses the optimisation technique proposed by S. Paris and F. Durand, in ECCV'2006
- (extended for 3D volumetric images).
- It is based on the reference implementation http://people.csail.mit.edu/jiawen/software/bilateralFilter.m
- **/
- template<typename t>
- CImg<T>& blur_bilateral(const CImg<t>& guide,
- const float sigma_x, const float sigma_y,
- const float sigma_z, const float sigma_r,
- const float sampling_x, const float sampling_y,
- const float sampling_z, const float sampling_r) {
- if (!is_sameXYZ(guide))
- throw CImgArgumentException(_cimg_instance
- "blur_bilateral(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
- cimg_instance,
- guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
- if (is_empty() || (!sigma_x && !sigma_y && !sigma_z)) return *this;
- T edge_min, edge_max = guide.max_min(edge_min);
- if (edge_min==edge_max) return blur(sigma_x,sigma_y,sigma_z);
- const float
- edge_delta = (float)(edge_max - edge_min),
- _sigma_x = sigma_x>=0?sigma_x:-sigma_x*_width/100,
- _sigma_y = sigma_y>=0?sigma_y:-sigma_y*_height/100,
- _sigma_z = sigma_z>=0?sigma_z:-sigma_z*_depth/100,
- _sigma_r = sigma_r>=0?sigma_r:-sigma_r*edge_delta/100,
- _sampling_x = sampling_x?sampling_x:std::max(_sigma_x,1.f),
- _sampling_y = sampling_y?sampling_y:std::max(_sigma_y,1.f),
- _sampling_z = sampling_z?sampling_z:std::max(_sigma_z,1.f),
- _sampling_r = sampling_r?sampling_r:std::max(_sigma_r,edge_delta/256),
- derived_sigma_x = _sigma_x / _sampling_x,
- derived_sigma_y = _sigma_y / _sampling_y,
- derived_sigma_z = _sigma_z / _sampling_z,
- derived_sigma_r = _sigma_r / _sampling_r;
- const int
- padding_x = (int)(2*derived_sigma_x) + 1,
- padding_y = (int)(2*derived_sigma_y) + 1,
- padding_z = (int)(2*derived_sigma_z) + 1,
- padding_r = (int)(2*derived_sigma_r) + 1;
- const unsigned int
- bx = (unsigned int)((_width - 1)/_sampling_x + 1 + 2*padding_x),
- by = (unsigned int)((_height - 1)/_sampling_y + 1 + 2*padding_y),
- bz = (unsigned int)((_depth - 1)/_sampling_z + 1 + 2*padding_z),
- br = (unsigned int)(edge_delta/_sampling_r + 1 + 2*padding_r);
- if (bx>0 || by>0 || bz>0 || br>0) {
- const bool is_3d = (_depth>1);
- if (is_3d) { // 3D version of the algorithm
- CImg<floatT> bgrid(bx,by,bz,br), bgridw(bx,by,bz,br);
- cimg_forC(*this,c) {
- const CImg<t> _guide = guide.get_shared_channel(c%guide._spectrum);
- bgrid.fill(0); bgridw.fill(0);
- cimg_forXYZ(*this,x,y,z) {
- const T val = (*this)(x,y,z,c);
- const float edge = (float)_guide(x,y,z);
- const int
- X = (int)cimg::round(x/_sampling_x) + padding_x,
- Y = (int)cimg::round(y/_sampling_y) + padding_y,
- Z = (int)cimg::round(z/_sampling_z) + padding_z,
- R = (int)cimg::round((edge - edge_min)/_sampling_r) + padding_r;
- bgrid(X,Y,Z,R)+=(float)val;
- bgridw(X,Y,Z,R)+=1;
- }
- bgrid.blur(derived_sigma_x,derived_sigma_y,derived_sigma_z,true).deriche(derived_sigma_r,0,'c',false);
- bgridw.blur(derived_sigma_x,derived_sigma_y,derived_sigma_z,true).deriche(derived_sigma_r,0,'c',false);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),4096))
- cimg_forXYZ(*this,x,y,z) {
- const float edge = (float)_guide(x,y,z);
- const float
- X = x/_sampling_x + padding_x,
- Y = y/_sampling_y + padding_y,
- Z = z/_sampling_z + padding_z,
- R = (edge - edge_min)/_sampling_r + padding_r;
- const float bval0 = bgrid._linear_atXYZC(X,Y,Z,R), bval1 = bgridw._linear_atXYZC(X,Y,Z,R);
- (*this)(x,y,z,c) = (T)(bval0/bval1);
- }
- }
- } else { // 2D version of the algorithm
- CImg<floatT> bgrid(bx,by,br,2);
- cimg_forC(*this,c) {
- const CImg<t> _guide = guide.get_shared_channel(c%guide._spectrum);
- bgrid.fill(0);
- cimg_forXY(*this,x,y) {
- const T val = (*this)(x,y,c);
- const float edge = (float)_guide(x,y);
- const int
- X = (int)cimg::round(x/_sampling_x) + padding_x,
- Y = (int)cimg::round(y/_sampling_y) + padding_y,
- R = (int)cimg::round((edge - edge_min)/_sampling_r) + padding_r;
- bgrid(X,Y,R,0)+=(float)val;
- bgrid(X,Y,R,1)+=1;
- }
- bgrid.blur(derived_sigma_x,derived_sigma_y,0,true).blur(0,0,derived_sigma_r,false);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(size(),4096))
- cimg_forXY(*this,x,y) {
- const float edge = (float)_guide(x,y);
- const float
- X = x/_sampling_x + padding_x,
- Y = y/_sampling_y + padding_y,
- R = (edge - edge_min)/_sampling_r + padding_r;
- const float bval0 = bgrid._linear_atXYZ(X,Y,R,0), bval1 = bgrid._linear_atXYZ(X,Y,R,1);
- (*this)(x,y,c) = (T)(bval0/bval1);
- }
- }
- }
- }
- return *this;
- }
- //! Blur image, with the joint bilateral filter \newinstance.
- template<typename t>
- CImg<Tfloat> get_blur_bilateral(const CImg<t>& guide,
- const float sigma_x, const float sigma_y,
- const float sigma_z, const float sigma_r,
- const float sampling_x, const float sampling_y,
- const float sampling_z, const float sampling_r) const {
- return CImg<Tfloat>(*this,false).blur_bilateral(guide,sigma_x,sigma_y,sigma_z,sigma_r,
- sampling_x,sampling_y,sampling_z,sampling_r);
- }
- //! Blur image using the joint bilateral filter.
- /**
- \param guide Image used to model the smoothing weights.
- \param sigma_s Amount of blur along the XYZ-axes.
- \param sigma_r Amount of blur along the value axis.
- \param sampling_s Amount of downsampling along the XYZ-axes used for the approximation. Defaults to sigma_s.
- \param sampling_r Amount of downsampling along the value axis used for the approximation. Defaults to sigma_r.
- **/
- template<typename t>
- CImg<T>& blur_bilateral(const CImg<t>& guide,
- const float sigma_s, const float sigma_r,
- const float sampling_s=0, const float sampling_r=0) {
- const float _sigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100;
- return blur_bilateral(guide,_sigma_s,_sigma_s,_sigma_s,sigma_r,sampling_s,sampling_s,sampling_s,sampling_r);
- }
- //! Blur image using the bilateral filter \newinstance.
- template<typename t>
- CImg<Tfloat> get_blur_bilateral(const CImg<t>& guide,
- const float sigma_s, const float sigma_r,
- const float sampling_s=0, const float sampling_r=0) const {
- return CImg<Tfloat>(*this,false).blur_bilateral(guide,sigma_s,sigma_r,sampling_s,sampling_r);
- }
- // [internal] Apply a box filter (used by CImg<T>::boxfilter() and CImg<T>::blur_box()).
- /*
- \param ptr the pointer of the data
- \param N size of the data
- \param boxsize Size of the box filter (can be subpixel).
- \param off the offset between two data point
- \param order the order of the filter 0 (smoothing), 1st derivative and 2nd derivative.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- */
- static void _cimg_blur_box_apply(T *ptr, const float boxsize, const int N, const ulongT off,
- const int order, const unsigned int boundary_conditions,
- const unsigned int nb_iter) {
- const int nboundary_conditions = boundary_conditions>1 && boxsize<=3?1:boundary_conditions;
- // Smooth.
- if (boxsize>1 && nb_iter) {
- const int w2 = (int)(boxsize - 1)/2;
- const unsigned int winsize = 2*w2 + 1U;
- const double frac = (boxsize - winsize)/2.;
- CImg<T> win(winsize);
- for (unsigned int iter = 0; iter<nb_iter; ++iter) {
- Tdouble sum = 0; // window sum
- for (int x = -w2; x<=w2; ++x) {
- win[x + w2] = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x);
- sum+=win[x + w2];
- }
- int ifirst = 0, ilast = 2*w2;
- T
- prev = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-w2 - 1),
- next = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,w2 + 1);
- for (int x = 0; x < N - 1; ++x) {
- const double sum2 = sum + frac * (prev + next);
- ptr[x*off] = (T)(sum2/boxsize);
- prev = win[ifirst];
- sum-=prev;
- ifirst = (int)((ifirst + 1)%winsize);
- ilast = (int)((ilast + 1)%winsize);
- win[ilast] = next;
- sum+=next;
- next = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + w2 + 2);
- }
- const double sum2 = sum + frac * (prev + next);
- ptr[(N - 1)*off] = (T)(sum2/boxsize);
- }
- }
- // Derive.
- switch (order) {
- case 0 :
- break;
- case 1 : {
- Tfloat
- p = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-1),
- c = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,0),
- n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,1);
- for (int x = 0; x<N - 1; ++x) {
- ptr[x*off] = (T)((n-p)/2.);
- p = c;
- c = n;
- n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + 2);
- }
- ptr[(N - 1)*off] = (T)((n-p)/2.);
- } break;
- case 2: {
- Tfloat
- p = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,-1),
- c = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,0),
- n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,1);
- for (int x = 0; x<N - 1; ++x) {
- ptr[x*off] = (T)(n - 2*c + p);
- p = c;
- c = n;
- n = __cimg_blur_box_apply(ptr,N,off,nboundary_conditions,x + 2);
- }
- ptr[(N - 1)*off] = (T)(n - 2*c + p);
- } break;
- }
- }
- static T __cimg_blur_box_apply(T *ptr, const int N, const ulongT off,
- const unsigned int boundary_conditions, const int x) {
- switch (boundary_conditions) {
- case 0 : // Dirichlet
- return x<0 || x>=N?(T)0:ptr[x*off];
- case 1 : { // Neumann
- const int nx = x<0?0:x>=N?N - 1:x;
- return ptr[nx*off];
- }
- case 2 : { // Periodic
- const int nx = cimg::mod(x,N);
- return ptr[nx*off];
- }
- default : { // Mirror
- const int
- N2 = 2*N,
- tx = cimg::mod(x,N2),
- nx = tx<N?tx:N2 - tx - 1;
- return ptr[nx*off];
- }
- }
- return (T)0;
- }
- // Apply box filter of order 0,1,2.
- /**
- \param boxsize Size of the box window (can be subpixel)
- \param order the order of the filter 0,1 or 2.
- \param axis Axis along which the filter is computed. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.
- \param nb_iter Number of filter iterations.
- **/
- CImg<T>& boxfilter(const float boxsize, const int order, const char axis='x',
- const unsigned int boundary_conditions=1,
- const unsigned int nb_iter=1) {
- const char naxis = cimg::lowercase(axis);
- const float nboxsize = boxsize>=0?boxsize:-boxsize*
- (naxis=='x'?_width:naxis=='y'?_height:naxis=='z'?_depth:_spectrum)/100;
- if (is_empty() || !nboxsize || (nboxsize<=1 && !order)) return *this;
- switch (naxis) {
- case 'x' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forYZC(*this,y,z,c)
- _cimg_blur_box_apply(data(0,y,z,c),nboxsize,_width,1U,order,boundary_conditions,nb_iter);
- } break;
- case 'y' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXZC(*this,x,z,c)
- _cimg_blur_box_apply(data(x,0,z,c),nboxsize,_height,(ulongT)_width,order,boundary_conditions,nb_iter);
- } break;
- case 'z' : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYC(*this,x,y,c)
- _cimg_blur_box_apply(data(x,y,0,c),nboxsize,_depth,(ulongT)_width*_height,order,boundary_conditions,nb_iter);
- } break;
- default : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth*_spectrum>=16))
- cimg_forXYZ(*this,x,y,z)
- _cimg_blur_box_apply(data(x,y,z,0),nboxsize,_spectrum,(ulongT)_width*_height*_depth,
- order,boundary_conditions,nb_iter);
- }
- }
- return *this;
- }
- // Apply box filter of order 0,1 or 2 \newinstance.
- CImg<Tfloat> get_boxfilter(const float boxsize, const int order, const char axis='x',
- const unsigned int boundary_conditions=1,
- const unsigned int nb_iter=1) const {
- return CImg<Tfloat>(*this,false).boxfilter(boxsize,order,axis,boundary_conditions,nb_iter);
- }
- //! Blur image with a box filter.
- /**
- \param boxsize_x Size of the box window, along the X-axis (can be subpixel).
- \param boxsize_y Size of the box window, along the Y-axis (can be subpixel).
- \param boxsize_z Size of the box window, along the Z-axis (can be subpixel).
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ false=dirichlet | true=neumann | 2=periodic | 3=mirror }</tt>.
- \param nb_iter Number of filter iterations.
- \note
- - This is a recursive algorithm, not depending on the values of the box kernel size.
- \see blur().
- **/
- CImg<T>& blur_box(const float boxsize_x, const float boxsize_y, const float boxsize_z,
- const unsigned int boundary_conditions=1,
- const unsigned int nb_iter=1) {
- if (is_empty()) return *this;
- if (_width>1) boxfilter(boxsize_x,0,'x',boundary_conditions,nb_iter);
- if (_height>1) boxfilter(boxsize_y,0,'y',boundary_conditions,nb_iter);
- if (_depth>1) boxfilter(boxsize_z,0,'z',boundary_conditions,nb_iter);
- return *this;
- }
- //! Blur image with a box filter \newinstance.
- CImg<Tfloat> get_blur_box(const float boxsize_x, const float boxsize_y, const float boxsize_z,
- const unsigned int boundary_conditions=1) const {
- return CImg<Tfloat>(*this,false).blur_box(boxsize_x,boxsize_y,boxsize_z,boundary_conditions);
- }
- //! Blur image with a box filter.
- /**
- \param boxsize Size of the box window (can be subpixel).
- \param boundary_conditions Boundary conditions.
- Can be <tt>{ 0=dirichlet | 1=neumann | 2=periodic | 3=mirror }</tt>.a
- \see deriche(), vanvliet().
- **/
- CImg<T>& blur_box(const float boxsize, const unsigned int boundary_conditions=1) {
- const float nboxsize = boxsize>=0?boxsize:-boxsize*cimg::max(_width,_height,_depth)/100;
- return blur_box(nboxsize,nboxsize,nboxsize,boundary_conditions);
- }
- //! Blur image with a box filter \newinstance.
- CImg<Tfloat> get_blur_box(const float boxsize, const unsigned int boundary_conditions=1) const {
- return CImg<Tfloat>(*this,false).blur_box(boxsize,boundary_conditions);
- }
- //! Blur image, with the image guided filter.
- /**
- \param guide Image used to guide the smoothing process.
- \param radius Spatial radius. If negative, it is expressed as a percentage of the largest image size.
- \param regularization Regularization parameter.
- If negative, it is expressed as a percentage of the guide value range.
- \note This method implements the filtering algorithm described in:
- He, Kaiming; Sun, Jian; Tang, Xiaoou, "Guided Image Filtering," Pattern Analysis and Machine Intelligence,
- IEEE Transactions on , vol.35, no.6, pp.1397,1409, June 2013
- **/
- template<typename t>
- CImg<T>& blur_guided(const CImg<t>& guide, const float radius, const float regularization) {
- return get_blur_guided(guide,radius,regularization).move_to(*this);
- }
- //! Blur image, with the image guided filter \newinstance.
- template<typename t>
- CImg<Tfloat> get_blur_guided(const CImg<t>& guide, const float radius, const float regularization) const {
- if (!is_sameXYZ(guide))
- throw CImgArgumentException(_cimg_instance
- "blur_guided(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
- cimg_instance,
- guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
- if (is_empty() || !radius) return *this;
- const int _radius = radius>=0?(int)radius:(int)(-radius*cimg::max(_width,_height,_depth)/100);
- float _regularization = regularization;
- if (regularization<0) {
- T edge_min, edge_max = guide.max_min(edge_min);
- if (edge_min==edge_max) return *this;
- _regularization = -regularization*(edge_max - edge_min)/100;
- }
- _regularization = std::max(_regularization,0.01f);
- const unsigned int psize = (unsigned int)(1 + 2*_radius);
- CImg<Tfloat>
- mean_p = get_blur_box(psize,true),
- mean_I = guide.get_blur_box(psize,true).resize(mean_p),
- cov_Ip = get_mul(guide).blur_box(psize,true)-=mean_p.get_mul(mean_I),
- var_I = guide.get_sqr().blur_box(psize,true)-=mean_I.get_sqr(),
- &a = cov_Ip.div(var_I+=_regularization),
- &b = mean_p-=a.get_mul(mean_I);
- a.blur_box(psize,true);
- b.blur_box(psize,true);
- return a.mul(guide)+=b;
- }
- //! Blur image using patch-based space.
- /**
- \param guide Image used to model the smoothing weights.
- \param sigma_s Amount of blur along the XYZ-axes.
- \param sigma_r Amount of blur along the value axis.
- \param patch_size Size of the patches.
- \param lookup_size Size of the window to search similar patches.
- \param smoothness Smoothness for the patch comparison.
- \param is_fast_approx Tells if a fast approximation of the gaussian function is used or not.
- **/
- template<typename t>
- CImg<T>& blur_patch(const CImg<t>& guide,
- const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
- const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) {
- if (is_empty() || !patch_size || !lookup_size) return *this;
- return get_blur_patch(guide,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx).move_to(*this);
- }
- //! Blur image using patch-based space \newinstance.
- template<typename t>
- CImg<Tfloat> get_blur_patch(const CImg<t>& guide,
- const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
- const unsigned int lookup_size=4, const float smoothness=0,
- const bool is_fast_approx=true) const {
- #define _cimg_blur_patch3d_fast(N) { \
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) \
- cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4) \
- firstprivate(P,Q)) \
- cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { \
- cimg_abort_test2; \
- cimg_def##N##x##N##x##N(res,x,y,z); \
- tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,x,y,z,c,pP,tfloat); pP+=N3; } \
- const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \
- x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \
- tfloat sum_weights = 0; \
- cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) \
- if (cimg::abs(_guide(x,y,z,0) - _guide(p,q,r,0))<sigma_r3) { \
- tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,p,q,r,c,pQ,tfloat); pQ+=N3; } \
- tfloat distance2 = 0; \
- pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
- distance2/=Pnorm; \
- const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, dz = (tfloat)r - z, \
- alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = alldist>3?0:1; \
- sum_weights+=weight; \
- cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c); \
- } \
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights; \
- else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \
- } _cimg_abort_catch_openmp2 }
- #define _cimg_blur_patch3d(N) { \
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) \
- cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4) \
- firstprivate(P,Q)) \
- cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { \
- cimg_abort_test2; \
- cimg_def##N##x##N##x##N(res,x,y,z); \
- tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,x,y,z,c,pP,tfloat); pP+=N3; } \
- const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1, \
- x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2; \
- tfloat sum_weights = 0, weight_max = 0; \
- cimg_for_in##N##XYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) { \
- tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N##x##N(_guide,p,q,r,c,pQ,tfloat); pQ+=N3; } \
- tfloat distance2 = 0; \
- pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
- distance2/=Pnorm; \
- const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, dz = (tfloat)r - z, \
- alldist = distance2 + (dx*dx + dy*dy + dz*dz)/sigma_s2, weight = std::exp(-alldist); \
- if (weight>weight_max) weight_max = weight; \
- sum_weights+=weight; \
- cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c); \
- } \
- sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight_max*(*this)(x,y,z,c); \
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights; \
- else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c)); \
- } _cimg_abort_catch_openmp2 }
- #define _cimg_blur_patch2d_fast(N) { \
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4) \
- firstprivate(P,Q)) \
- cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { \
- cimg_abort_test2; \
- cimg_def##N##x##N(res,x,y); \
- tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,x,y,0,c,pP,tfloat); pP+=N2; } \
- const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \
- tfloat sum_weights = 0; \
- cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) \
- if (cimg::abs(_guide(x,y,0,0) - _guide(p,q,0,0))<sigma_r3) { \
- tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,p,q,0,c,pQ,tfloat); pQ+=N2; } \
- tfloat distance2 = 0; \
- pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
- distance2/=Pnorm; \
- const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, \
- alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = alldist>3?0:1; \
- sum_weights+=weight; \
- cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c); \
- } \
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights; \
- else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \
- } _cimg_abort_catch_openmp2 }
- #define _cimg_blur_patch2d(N) { \
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4) \
- firstprivate(P,Q)) \
- cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { \
- cimg_abort_test2; \
- cimg_def##N##x##N(res,x,y); \
- tfloat *pP = P._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,x,y,0,c,pP,tfloat); pP+=N2; } \
- const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2; \
- tfloat sum_weights = 0, weight_max = 0; \
- cimg_for_in##N##XY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) { \
- tfloat *pQ = Q._data; cimg_forC(_guide,c) { cimg_get##N##x##N(_guide,p,q,0,c,pQ,tfloat); pQ+=N2; } \
- tfloat distance2 = 0; \
- pQ = Q._data; cimg_for(P,_pP,tfloat) { const tfloat dI = *_pP - *(pQ++); distance2+=dI*dI; } \
- distance2/=Pnorm; \
- const tfloat dx = (tfloat)p - x, dy = (tfloat)q - y, \
- alldist = distance2 + (dx*dx+dy*dy)/sigma_s2, weight = std::exp(-alldist); \
- if (weight>weight_max) weight_max = weight; \
- sum_weights+=weight; \
- cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c); \
- } \
- sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight_max*(*this)(x,y,c); \
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights; \
- else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c)); \
- } _cimg_abort_catch_openmp2 }
- typedef _cimg_tfloat tfloat;
- if (!is_sameXYZ(guide))
- throw CImgArgumentException(_cimg_instance
- "blur_patch(): Invalid size for specified guide image (%u,%u,%u,%u,%p).",
- cimg_instance,
- guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
- if (is_empty() || !patch_size || !lookup_size) return +*this;
- Tfloat val_min, val_max = (Tfloat)max_min(val_min);
- _cimg_abort_init_openmp;
- cimg_abort_init;
- CImg<Tfloat> res(_width,_height,_depth,_spectrum,0);
- const CImg<tfloat>
- __guide = guide?CImg<tfloat>(guide,guide.pixel_type()==cimg::type<tfloat>::string()):
- CImg<tfloat>(*this,pixel_type()==cimg::type<tfloat>::string()),
- _guide = smoothness>0?__guide.get_blur(smoothness):__guide.get_shared();
- CImg<tfloat> P(_guide._spectrum*patch_size*patch_size*(_depth>1?patch_size:1)), Q(P);
- t guide_min = (t)0, guide_max = (t)0;
- if (sigma_r<0) guide_max = guide.max_min(guide_min);
- const float
- guide_delta = (float)(guide_max - guide_min),
- _sigma_s = sigma_s>=0?sigma_s:-sigma_s*cimg::max(_width,_height,_depth)/100,
- _sigma_r = sigma_r>=0?sigma_r:-sigma_r*guide_delta/100,
- sigma_s2 = _sigma_s*_sigma_s,
- sigma_r2 = _sigma_r*_sigma_r,
- sigma_r3 = 3*_sigma_r,
- Pnorm = P.size()*sigma_r2;
- const int rsize2 = (int)lookup_size/2, rsize1 = (int)lookup_size - rsize2 - 1;
- const unsigned int N2 = patch_size*patch_size, N3 = N2*patch_size;
- cimg::unused(N2,N3);
- if (_depth>1) switch (patch_size) { // 3D
- case 2 : if (is_fast_approx) _cimg_blur_patch3d_fast(2) else _cimg_blur_patch3d(2) break;
- case 3 : if (is_fast_approx) _cimg_blur_patch3d_fast(3) else _cimg_blur_patch3d(3) break;
- default : {
- const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1;
- if (is_fast_approx) {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4)
- firstprivate(P,Q))
- cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { // Fast
- cimg_abort_test2;
- P = _guide.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true);
- const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1,
- x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2;
- tfloat sum_weights = 0;
- cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r)
- if (cimg::abs(_guide(x,y,z,0) - _guide(p,q,r,0))<sigma_r3) {
- (Q = _guide.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P;
- const tfloat
- dx = (tfloat)x - p, dy = (tfloat)y - q, dz = (tfloat)z - r,
- distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2),
- weight = distance2>3?0:1;
- sum_weights+=weight;
- cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c);
- }
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights;
- else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c));
- } _cimg_abort_catch_openmp2
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height*res._depth>=4)
- firstprivate(P,Q))
- cimg_forXYZ(res,x,y,z) _cimg_abort_try_openmp2 { // Exact
- cimg_abort_test2;
- P = _guide.get_crop(x - psize1,y - psize1,z - psize1,x + psize2,y + psize2,z + psize2,true);
- const int x0 = x - rsize1, y0 = y - rsize1, z0 = z - rsize1,
- x1 = x + rsize2, y1 = y + rsize2, z1 = z + rsize2;
- tfloat sum_weights = 0, weight_max = 0;
- cimg_for_inXYZ(res,x0,y0,z0,x1,y1,z1,p,q,r) if (p!=x || q!=y || r!=z) {
- (Q = _guide.get_crop(p - psize1,q - psize1,r - psize1,p + psize2,q + psize2,r + psize2,true))-=P;
- const tfloat
- dx = (tfloat)x - p, dy = (tfloat)y - q, dz = (tfloat)z - r,
- distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy + dz*dz)/sigma_s2),
- weight = std::exp(-distance2);
- if (weight>weight_max) weight_max = weight;
- sum_weights+=weight;
- cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight*(*this)(p,q,r,c);
- }
- sum_weights+=weight_max; cimg_forC(res,c) res(x,y,z,c)+=(Tfloat)weight_max*(*this)(x,y,z,c);
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,z,c)/=(Tfloat)sum_weights;
- else cimg_forC(res,c) res(x,y,z,c) = (Tfloat)((*this)(x,y,z,c));
- } _cimg_abort_catch_openmp2
- }
- }
- } else switch (patch_size) { // 2D
- case 2 : if (is_fast_approx) _cimg_blur_patch2d_fast(2) else _cimg_blur_patch2d(2) break;
- case 3 : if (is_fast_approx) _cimg_blur_patch2d_fast(3) else _cimg_blur_patch2d(3) break;
- case 4 : if (is_fast_approx) _cimg_blur_patch2d_fast(4) else _cimg_blur_patch2d(4) break;
- case 5 : if (is_fast_approx) _cimg_blur_patch2d_fast(5) else _cimg_blur_patch2d(5) break;
- case 6 : if (is_fast_approx) _cimg_blur_patch2d_fast(6) else _cimg_blur_patch2d(6) break;
- case 7 : if (is_fast_approx) _cimg_blur_patch2d_fast(7) else _cimg_blur_patch2d(7) break;
- case 8 : if (is_fast_approx) _cimg_blur_patch2d_fast(8) else _cimg_blur_patch2d(8) break;
- case 9 : if (is_fast_approx) _cimg_blur_patch2d_fast(9) else _cimg_blur_patch2d(9) break;
- default : { // Fast
- const int psize2 = (int)patch_size/2, psize1 = (int)patch_size - psize2 - 1;
- if (is_fast_approx) {
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4)
- firstprivate(P,Q))
- cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { // Fast
- cimg_abort_test2;
- P = _guide.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true);
- const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2;
- tfloat sum_weights = 0;
- cimg_for_inXY(res,x0,y0,x1,y1,p,q)
- if (cimg::abs(_guide(x,y,0) - _guide(p,q,0))<sigma_r3) {
- (Q = _guide.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P;
- const tfloat
- dx = (tfloat)x - p, dy = (tfloat)y - q,
- distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2),
- weight = distance2>3?0:1;
- sum_weights+=weight;
- cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c);
- }
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights;
- else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c));
- } _cimg_abort_catch_openmp2
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_if(res._width>=(cimg_openmp_sizefactor)*32 && res._height>=4)
- firstprivate(P,Q))
- cimg_forXY(res,x,y) _cimg_abort_try_openmp2 { // Exact
- cimg_abort_test2;
- P = _guide.get_crop(x - psize1,y - psize1,x + psize2,y + psize2,true);
- const int x0 = x - rsize1, y0 = y - rsize1, x1 = x + rsize2, y1 = y + rsize2;
- tfloat sum_weights = 0, weight_max = 0;
- cimg_for_inXY(res,x0,y0,x1,y1,p,q) if (p!=x || q!=y) {
- (Q = _guide.get_crop(p - psize1,q - psize1,p + psize2,q + psize2,true))-=P;
- const tfloat
- dx = (tfloat)x - p, dy = (tfloat)y - q,
- distance2 = (tfloat)(Q.pow(2).sum()/Pnorm + (dx*dx + dy*dy)/sigma_s2),
- weight = std::exp(-distance2);
- if (weight>weight_max) weight_max = weight;
- sum_weights+=weight;
- cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight*(*this)(p,q,c);
- }
- sum_weights+=weight_max; cimg_forC(res,c) res(x,y,c)+=(Tfloat)weight_max*(*this)(x,y,c);
- if (sum_weights>1e-10) cimg_forC(res,c) res(x,y,c)/=(Tfloat)sum_weights;
- else cimg_forC(res,c) res(x,y,c) = (Tfloat)((*this)(x,y,c));
- } _cimg_abort_catch_openmp2
- }
- }
- }
- cimg_abort_test;
- return res.cut(val_min,val_max);
- }
- //! Blur image using patch-based space \simplification.
- CImg<T>& blur_patch(const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
- const unsigned int lookup_size=4, const float smoothness=0, const bool is_fast_approx=true) {
- return blur_patch(*this,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx);
- }
- //! Blur image using patch-based space \simplification \newinstance.
- CImg<Tfloat> get_blur_patch(const float sigma_s, const float sigma_r, const unsigned int patch_size=3,
- const unsigned int lookup_size=4, const float smoothness=0,
- const bool is_fast_approx=true) const {
- return get_blur_patch(*this,sigma_s,sigma_r,patch_size,lookup_size,smoothness,is_fast_approx);
- }
- //! Blur image with the median filter.
- /**
- \param n Size of the median filter.
- \param threshold Threshold used to discard pixels too far from the current pixel value in the median computation.
- **/
- CImg<T>& blur_median(const unsigned int n, const float threshold=0) {
- if (!n) return *this;
- return get_blur_median(n,threshold).move_to(*this);
- }
- //! Blur image with the median filter \newinstance.
- CImg<T> get_blur_median(const unsigned int n, const float threshold=0) const {
- if (is_empty() || n<=1) return +*this;
- CImg<T> res(_width,_height,_depth,_spectrum);
- T *ptrd = res._data;
- cimg::unused(ptrd);
- const int hr = (int)n/2, hl = n - hr - 1;
- if (res._depth!=1) { // 3D
- if (threshold>0)
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_depth*_spectrum>=4))
- cimg_forXYZC(*this,x,y,z,c) { // With threshold
- const int
- x0 = x - hl, y0 = y - hl, z0 = z - hl, x1 = x + hr, y1 = y + hr, z1 = z + hr,
- nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0,
- nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1, nz1 = z1>=depth()?depth() - 1:z1;
- const Tfloat val0 = (Tfloat)(*this)(x,y,z,c);
- CImg<T> values(n*n*n);
- unsigned int nb_values = 0;
- T *_ptrd = values.data();
- cimg_for_inXYZ(*this,nx0,ny0,nz0,nx1,ny1,nz1,p,q,r)
- if (cimg::abs((*this)(p,q,r,c) - val0)<=threshold) { *(_ptrd++) = (*this)(p,q,r,c); ++nb_values; }
- res(x,y,z,c) = nb_values?values.get_shared_points(0,nb_values - 1).median():(*this)(x,y,z,c);
- }
- else
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_depth*_spectrum>=4))
- cimg_forXYZC(*this,x,y,z,c) { // Without threshold
- const int
- x0 = x - hl, y0 = y - hl, z0 = z - hl, x1 = x + hr, y1 = y + hr, z1 = z + hr,
- nx0 = x0<0?0:x0, ny0 = y0<0?0:y0, nz0 = z0<0?0:z0,
- nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1, nz1 = z1>=depth()?depth() - 1:z1;
- res(x,y,z,c) = get_crop(nx0,ny0,nz0,c,nx1,ny1,nz1,c).median();
- }
- } else {
- if (threshold>0)
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 &&
- _height*_spectrum>=4))
- cimg_forXYC(*this,x,y,c) { // With threshold
- const int
- x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr,
- nx0 = x0<0?0:x0, ny0 = y0<0?0:y0,
- nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1;
- const Tfloat val0 = (Tfloat)(*this)(x,y,c);
- CImg<T> values(n*n);
- unsigned int nb_values = 0;
- T *_ptrd = values.data();
- cimg_for_inXY(*this,nx0,ny0,nx1,ny1,p,q)
- if (cimg::abs((*this)(p,q,c) - val0)<=threshold) { *(_ptrd++) = (*this)(p,q,c); ++nb_values; }
- res(x,y,c) = nb_values?values.get_shared_points(0,nb_values - 1).median():(*this)(x,y,c);
- }
- else {
- const int
- w1 = width() - 1, h1 = height() - 1,
- w2 = width() - 2, h2 = height() - 2,
- w3 = width() - 3, h3 = height() - 3,
- w4 = width() - 4, h4 = height() - 4;
- switch (n) { // Without threshold
- case 3 : {
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
- cimg_forC(*this,c) {
- CImg<T> I(9);
- cimg_for_in3x3(*this,1,1,w2,h2,x,y,0,c,I,T)
- res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],I[5],I[6],I[7],I[8]);
- cimg_for_borderXY(*this,x,y,1)
- res(x,y,c) = get_crop(std::max(0,x - 1),std::max(0,y - 1),0,c,
- std::min(w1,x + 1),std::min(h1,y + 1),0,c).median();
- }
- } break;
- case 5 : {
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
- cimg_forC(*this,c) {
- CImg<T> I(25);
- cimg_for_in5x5(*this,2,2,w3,h3,x,y,0,c,I,T)
- res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],
- I[5],I[6],I[7],I[8],I[9],
- I[10],I[11],I[12],I[13],I[14],
- I[15],I[16],I[17],I[18],I[19],
- I[20],I[21],I[22],I[23],I[24]);
- cimg_for_borderXY(*this,x,y,2)
- res(x,y,c) = get_crop(std::max(0,x - 2),std::max(0,y - 2),0,c,
- std::min(w1,x + 2),std::min(h1,y + 2),0,c).median();
- }
- } break;
- case 7 : {
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
- cimg_forC(*this,c) {
- CImg<T> I(49);
- cimg_for_in7x7(*this,3,3,w4,h4,x,y,0,c,I,T)
- res(x,y,c) = cimg::median(I[0],I[1],I[2],I[3],I[4],I[5],I[6],
- I[7],I[8],I[9],I[10],I[11],I[12],I[13],
- I[14],I[15],I[16],I[17],I[18],I[19],I[20],
- I[21],I[22],I[23],I[24],I[25],I[26],I[27],
- I[28],I[29],I[30],I[31],I[32],I[33],I[34],
- I[35],I[36],I[37],I[38],I[39],I[40],I[41],
- I[42],I[43],I[44],I[45],I[46],I[47],I[48]);
- cimg_for_borderXY(*this,x,y,3)
- res(x,y,c) = get_crop(std::max(0,x - 3),std::max(0,y - 3),0,c,
- std::min(w1,x + 3),std::min(h1,y + 3),0,c).median();
- }
- } break;
- default : {
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*16 && _height*_spectrum>=4))
- cimg_forXYC(*this,x,y,c) {
- const int
- x0 = x - hl, y0 = y - hl, x1 = x + hr, y1 = y + hr,
- nx0 = x0<0?0:x0, ny0 = y0<0?0:y0,
- nx1 = x1>=width()?width() - 1:x1, ny1 = y1>=height()?height() - 1:y1;
- res(x,y,c) = get_crop(nx0,ny0,0,c,nx1,ny1,0,c).median();
- }
- }
- }
- }
- }
- return res;
- }
- //! Sharpen image.
- /**
- \param amplitude Sharpening amplitude
- \param sharpen_type Select sharpening method. Can be <tt>{ false=inverse diffusion | true=shock filters }</tt>.
- \param edge Edge threshold (shock filters only).
- \param alpha Gradient smoothness (shock filters only).
- \param sigma Tensor smoothness (shock filters only).
- **/
- CImg<T>& sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1,
- const float alpha=0, const float sigma=0) {
- if (is_empty()) return *this;
- T val_min, val_max = max_min(val_min);
- const float nedge = edge/2;
- CImg<Tfloat> velocity(_width,_height,_depth,_spectrum), _veloc_max(_spectrum);
- if (_depth>1) { // 3D
- if (sharpen_type) { // Shock filters
- CImg<Tfloat> G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors());
- if (sigma>0) G.blur(sigma);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*32 &&
- _height*_depth>=16))
- cimg_forYZ(G,y,z) {
- Tfloat *ptrG0 = G.data(0,y,z,0), *ptrG1 = G.data(0,y,z,1),
- *ptrG2 = G.data(0,y,z,2), *ptrG3 = G.data(0,y,z,3);
- CImg<Tfloat> val, vec;
- cimg_forX(G,x) {
- G.get_tensor_at(x,y,z).symmetric_eigen(val,vec);
- if (val[0]<0) val[0] = 0;
- if (val[1]<0) val[1] = 0;
- if (val[2]<0) val[2] = 0;
- *(ptrG0++) = vec(0,0);
- *(ptrG1++) = vec(0,1);
- *(ptrG2++) = vec(0,2);
- *(ptrG3++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1] + val[2],-(Tfloat)nedge);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*512 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat
- u = G(x,y,z,0),
- v = G(x,y,z,1),
- w = G(x,y,z,2),
- amp = G(x,y,z,3),
- ixx = Incc + Ipcc - 2*Iccc,
- ixy = (Innc + Ippc - Inpc - Ipnc)/4,
- ixz = (Incn + Ipcp - Incp - Ipcn)/4,
- iyy = Icnc + Icpc - 2*Iccc,
- iyz = (Icnn + Icpp - Icnp - Icpn)/4,
- izz = Iccn + Iccp - 2*Iccc,
- ixf = Incc - Iccc,
- ixb = Iccc - Ipcc,
- iyf = Icnc - Iccc,
- iyb = Iccc - Icpc,
- izf = Iccn - Iccc,
- izb = Iccc - Iccp,
- itt = u*u*ixx + v*v*iyy + w*w*izz + 2*u*v*ixy + 2*u*w*ixz + 2*v*w*iyz,
- it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb) + w*cimg::minmod(izf,izb),
- veloc = -amp*cimg::sign(itt)*cimg::abs(it);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- _veloc_max[c] = veloc_max;
- }
- } else // Inverse diffusion
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*512 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat veloc = -Ipcc - Incc - Icpc - Icnc - Iccp - Iccn + 6*Iccc;
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- _veloc_max[c] = veloc_max;
- }
- } else { // 2D
- if (sharpen_type) { // Shock filters
- CImg<Tfloat> G = (alpha>0?get_blur(alpha).get_structure_tensors():get_structure_tensors());
- if (sigma>0) G.blur(sigma);
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*32 &&
- _height>=(cimg_openmp_sizefactor)*16))
- cimg_forY(G,y) {
- CImg<Tfloat> val, vec;
- Tfloat *ptrG0 = G.data(0,y,0,0), *ptrG1 = G.data(0,y,0,1), *ptrG2 = G.data(0,y,0,2);
- cimg_forX(G,x) {
- G.get_tensor_at(x,y).symmetric_eigen(val,vec);
- if (val[0]<0) val[0] = 0;
- if (val[1]<0) val[1] = 0;
- *(ptrG0++) = vec(0,0);
- *(ptrG1++) = vec(0,1);
- *(ptrG2++) = 1 - (Tfloat)std::pow(1 + val[0] + val[1],-(Tfloat)nedge);
- }
- }
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*512 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
- const Tfloat
- u = G(x,y,0),
- v = G(x,y,1),
- amp = G(x,y,2),
- ixx = Inc + Ipc - 2*Icc,
- ixy = (Inn + Ipp - Inp - Ipn)/4,
- iyy = Icn + Icp - 2*Icc,
- ixf = Inc - Icc,
- ixb = Icc - Ipc,
- iyf = Icn - Icc,
- iyb = Icc - Icp,
- itt = u*u*ixx + v*v*iyy + 2*u*v*ixy,
- it = u*cimg::minmod(ixf,ixb) + v*cimg::minmod(iyf,iyb),
- veloc = -amp*cimg::sign(itt)*cimg::abs(it);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- _veloc_max[c] = veloc_max;
- }
- } else // Inverse diffusion
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*512 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = velocity.data(0,0,0,c), veloc_max = 0;
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
- const Tfloat veloc = -Ipc - Inc - Icp - Icn + 4*Icc;
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- }
- _veloc_max[c] = veloc_max;
- }
- }
- const Tfloat veloc_max = _veloc_max.max();
- if (veloc_max<=0) return *this;
- return ((velocity*=amplitude/veloc_max)+=*this).cut(val_min,val_max).move_to(*this);
- }
- //! Sharpen image \newinstance.
- CImg<T> get_sharpen(const float amplitude, const bool sharpen_type=false, const float edge=1,
- const float alpha=0, const float sigma=0) const {
- return (+*this).sharpen(amplitude,sharpen_type,edge,alpha,sigma);
- }
- //! Return image gradient.
- /**
- \param axes Axes considered for the gradient computation, as a C-string (e.g "xy").
- \param scheme = Numerical scheme used for the gradient computation:
- - -1 = Backward finite differences
- - 0 = Centered finite differences (default)
- - 1 = Forward finite differences
- - 2 = Using Sobel kernels
- - 3 = Using rotation invariant kernels
- - 4 = Using Deriche recursive filter.
- - 5 = Using Van Vliet recursive filter.
- **/
- CImgList<Tfloat> get_gradient(const char *const axes=0, const int scheme=0) const {
- CImgList<Tfloat> res;
- char __axes[4] = { 0 };
- const char *_axes = axes?axes:__axes;
- if (!axes) {
- unsigned int k = 0;
- if (_width>1) __axes[k++] = 'x';
- if (_height>1) __axes[k++] = 'y';
- if (_depth>1) __axes[k++] = 'z';
- }
- CImg<Tfloat> grad;
- while (*_axes) {
- const char axis = cimg::lowercase(*(_axes++));
- if (axis!='x' && axis!='y' && axis!='z')
- throw CImgArgumentException(_cimg_instance
- "get_gradient(): Invalid specified axes '%s'.",
- cimg_instance,
- axes);
- const longT off = axis=='x'?1:axis=='y'?_width:_width*_height;
- if ((axis=='x' && _width==1) || (axis=='y' && _height==1) || (axis=='z' && _depth==1)) {
- grad.assign(_width,_height,_depth,_spectrum,0).move_to(res);
- continue;
- }
- const int _scheme = axis=='z' && (scheme==2 || scheme==3)?0:scheme;
- switch (_scheme) {
- case -1 : { // Backward finite differences
- grad.assign(_width,_height,_depth,_spectrum);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
- cimg_forXYZC(*this,x,y,z,c) {
- const ulongT pos = offset(x,y,z,c);
- if ((axis=='x' && !x) || (axis=='y' && !y) || (axis=='z' && !z))
- grad[pos] = 0;
- else
- grad[pos] = (Tfloat)_data[pos] - _data[pos - off];
- }
- grad.move_to(res);
- } break;
- case 1 : { // Forward finite differences
- grad.assign(_width,_height,_depth,_spectrum);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
- cimg_forXYZC(*this,x,y,z,c) {
- const ulongT pos = offset(x,y,z,c);
- if ((axis=='x' && x==width() - 1) || (axis=='y' && y==height() - 1) || (axis=='z' && z==depth() - 1))
- grad[pos] = 0;
- else
- grad[pos] = (Tfloat)_data[pos + off] - _data[pos];
- }
- grad.move_to(res);
- } break;
- case 2 : { // Sobel scheme
- grad.assign(_width,_height,_depth,_spectrum);
- if (axis=='x') // X-axis
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
- _depth*_spectrum>=2))
- cimg_forZC(*this,z,c) {
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = - Ipp + Inp - 2*Ipc + 2*Inc - Ipn + Inn;
- }
- else // Y-axis
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
- _depth*_spectrum>=2))
- cimg_forZC(*this,z,c) {
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,z,c,I,Tfloat) grad(x,y,z,c) = - Ipp - 2*Icp - Inp + Ipn + 2*Icn + Inn;
- }
- grad.move_to(res);
- } break;
- case 3 : { // Rotation invariant scheme
- const Tfloat a = (Tfloat)(0.25f*(2 - std::sqrt(2.f))), b = (Tfloat)(0.5f*(std::sqrt(2.f) - 1));
- grad.assign(_width,_height,_depth,_spectrum);
- if (axis=='x') // X-axis
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
- _depth*_spectrum>=2))
- cimg_forZC(*this,z,c) {
- CImg_3x3(I,Tfloat);
- 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;
- }
- else // Y-axis
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
- _depth*_spectrum>=2))
- cimg_forZC(*this,z,c) {
- CImg_3x3(I,Tfloat);
- 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;
- }
- grad.move_to(res);
- } break;
- case 4 : // Deriche filter
- get_deriche(0,1,axis).move_to(res);
- break;
- case 5 : // Van Vliet filter
- get_vanvliet(0,1,axis).move_to(res);
- break;
- default : { // Central finite differences
- grad.assign(_width,_height,_depth,_spectrum);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
- cimg_forXYZC(*this,x,y,z,c) {
- const ulongT pos = offset(x,y,z,c);
- if ((axis=='x' && !x) || (axis=='y' && !y) || (axis=='z' && !z))
- grad[pos] = ((Tfloat)_data[pos + off] - _data[pos])/2;
- else if ((axis=='x' && x==width() - 1) || (axis=='y' && y==height() - 1) || (axis=='z' && z==depth() - 1))
- grad[pos] = ((Tfloat)_data[pos] - _data[pos - off])/2;
- else
- grad[pos] = ((Tfloat)_data[pos + off] - _data[pos - off])/2;
- }
- grad.move_to(res);
- } break;
- }
- }
- return res;
- }
- //! Return image hessian.
- /**
- \param axes Axes considered for the hessian computation, as a C-string (e.g "xy").
- **/
- CImgList<Tfloat> get_hessian(const char *const axes=0) const {
- CImgList<Tfloat> res;
- char __axes[12] = { 0 };
- const char *_axes = axes?axes:__axes;
- if (!axes) {
- unsigned int k = 0;
- if (_width>1) { __axes[k++] = 'x'; __axes[k++] = 'x'; }
- if (_width>1 && _height>1) { __axes[k++] = 'x'; __axes[k++] = 'y'; }
- if (_width>1 && _depth>1) { __axes[k++] = 'x'; __axes[k++] = 'z'; }
- if (_height>1) { __axes[k++] = 'y'; __axes[k++] = 'y'; }
- if (_height>1 && _depth>1) { __axes[k++] = 'y'; __axes[k++] = 'z'; }
- if (_depth>1) { __axes[k++] = 'z'; __axes[k++] = 'z'; }
- }
- const unsigned int len = (unsigned int)std::strlen(_axes);
- if (len%2)
- throw CImgArgumentException(_cimg_instance
- "get_hessian(): Invalid specified axes '%s'.",
- cimg_instance,
- axes);
- CImg<Tfloat> hess;
- for (unsigned int k = 0; k<len; k+=2) {
- const char
- _axis1 = cimg::lowercase(_axes[k]),
- _axis2 = cimg::lowercase(_axes[k + 1]),
- axis1 = std::min(_axis1,_axis2),
- axis2 = std::max(_axis2,_axis2);
- if (axis1!='x' && axis1!='y' && axis1!='z' &&
- axis2!='x' && axis2!='y' && axis2!='z')
- throw CImgArgumentException(_cimg_instance
- "get_hessian(): Invalid specified axes '%s'.",
- cimg_instance,
- axes);
- const longT off = axis1=='x'?1:axis1=='y'?_width:_width*_height;
- hess.assign(_width,_height,_depth,_spectrum);
- if (((axis1=='x' || axis2=='x') && _width==1) ||
- ((axis1=='y' || axis2=='y') && _height==1) ||
- ((axis1=='z' || axis2=='z') && _depth==1)) {
- hess.fill(0).move_to(res);
- continue;
- }
- if (axis1==axis2) // Ixx, Iyy, Izz
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(size(),16384))
- cimg_forXYZC(*this,x,y,z,c) {
- const ulongT pos = offset(x,y,z,c);
- if ((axis1=='x' && !x) || (axis1=='y' && !y) || (axis1=='z' && !z))
- hess[pos] = (Tfloat)_data[pos + off] - _data[pos];
- else if ((axis1=='x' && x==width() - 1) ||
- (axis1=='y' && y==height() - 1) ||
- (axis1=='z' && z==depth() - 1))
- hess[pos] = (Tfloat)_data[pos - off] - _data[pos];
- else
- hess[pos] = (Tfloat)_data[pos + off] + _data[pos - off] - 2*_data[pos];
- }
- else if (axis1=='x' && axis2=='y') // Ixy
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*16384 &&
- _depth*_spectrum>=2))
- cimg_forZC(*this,z,c) {
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Inn + Ipp - Inp - Ipn)/4;
- }
- else if (axis1=='x' && axis2=='z') // Ixz
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*16384 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Incn + Ipcp - Incp - Ipcn)/4;
- }
- else // Iyz
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*16384 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) hess(x,y,z,c) = (Icnn + Icpp - Icnp - Icpn)/4;
- }
- hess.move_to(res);
- }
- return res;
- }
- //! Compute image Laplacian.
- CImg<T>& laplacian() {
- return get_laplacian().move_to(*this);
- }
- //! Compute image Laplacian \newinstance.
- CImg<Tfloat> get_laplacian() const {
- if (is_empty()) return CImg<Tfloat>();
- CImg<Tfloat> res(_width,_height,_depth,_spectrum);
- if (_depth>1) { // 3D
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = res.data(0,0,0,c);
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) *(ptrd++) = Incc + Ipcc + Icnc + Icpc + Iccn + Iccp - 6*Iccc;
- }
- } else if (_height>1) { // 2D
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
- _depth*_spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = res.data(0,0,0,c);
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc + Icn + Icp - 4*Icc;
- }
- } else { // 1D
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*1048576 &&
- _height*_depth*_spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd = res.data(0,0,0,c);
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) *(ptrd++) = Inc + Ipc - 2*Icc;
- }
- }
- return res;
- }
- //! Compute the structure tensor field of an image.
- /**
- \param is_fwbw_scheme scheme. Can be <tt>{ false=centered | true=forward-backward }</tt>
- **/
- CImg<T>& structure_tensors(const bool is_fwbw_scheme=false) {
- return get_structure_tensors(is_fwbw_scheme).move_to(*this);
- }
- //! Compute the structure tensor field of an image \newinstance.
- CImg<Tfloat> get_structure_tensors(const bool is_fwbw_scheme=false) const {
- if (is_empty()) return *this;
- CImg<Tfloat> res;
- if (_depth>1) { // 3D
- res.assign(_width,_height,_depth,6,0);
- if (!is_fwbw_scheme) { // Classical central finite differences
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat
- *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2),
- *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5);
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat
- ix = (Incc - Ipcc)/2,
- iy = (Icnc - Icpc)/2,
- iz = (Iccn - Iccp)/2;
- cimg_pragma_openmp(atomic) *(ptrd0++)+=ix*ix;
- cimg_pragma_openmp(atomic) *(ptrd1++)+=ix*iy;
- cimg_pragma_openmp(atomic) *(ptrd2++)+=ix*iz;
- cimg_pragma_openmp(atomic) *(ptrd3++)+=iy*iy;
- cimg_pragma_openmp(atomic) *(ptrd4++)+=iy*iz;
- cimg_pragma_openmp(atomic) *(ptrd5++)+=iz*iz;
- }
- }
- } else { // Forward/backward finite differences
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1048576 &&
- _spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat
- *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2),
- *ptrd3 = res.data(0,0,0,3), *ptrd4 = res.data(0,0,0,4), *ptrd5 = res.data(0,0,0,5);
- CImg_3x3x3(I,Tfloat);
- cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) {
- const Tfloat
- ixf = Incc - Iccc, ixb = Iccc - Ipcc, ixc = (Incc - Ipcc)/2,
- iyf = Icnc - Iccc, iyb = Iccc - Icpc, iyc = (Icnc - Icpc)/2,
- izf = Iccn - Iccc, izb = Iccc - Iccp, izc = (Iccn - Iccp)/2;
- cimg_pragma_openmp(atomic) *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2;
- cimg_pragma_openmp(atomic) *(ptrd1++)+=ixc*iyc;
- cimg_pragma_openmp(atomic) *(ptrd2++)+=ixc*izc;
- cimg_pragma_openmp(atomic) *(ptrd3++)+=(iyf*iyf + iyb*iyb)/2;
- cimg_pragma_openmp(atomic) *(ptrd4++)+=iyc*izc;
- cimg_pragma_openmp(atomic) *(ptrd5++)+=(izf*izf + izb*izb)/2;
- }
- }
- }
- } else { // 2D
- res.assign(_width,_height,_depth,3,0);
- if (!is_fwbw_scheme) { // Classical central finite differences
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
- _depth*_spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2);
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
- const Tfloat
- ix = (Inc - Ipc)/2,
- iy = (Icn - Icp)/2;
- cimg_pragma_openmp(atomic) *(ptrd0++)+=ix*ix;
- cimg_pragma_openmp(atomic) *(ptrd1++)+=ix*iy;
- cimg_pragma_openmp(atomic) *(ptrd2++)+=iy*iy;
- }
- }
- } else { // Forward/backward finite differences (version 2)
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*1048576 &&
- _depth*_spectrum>=2))
- cimg_forC(*this,c) {
- Tfloat *ptrd0 = res.data(0,0,0,0), *ptrd1 = res.data(0,0,0,1), *ptrd2 = res.data(0,0,0,2);
- CImg_3x3(I,Tfloat);
- cimg_for3x3(*this,x,y,0,c,I,Tfloat) {
- const Tfloat
- ixf = Inc - Icc, ixb = Icc - Ipc, ixc = (Inc - Ipc)/2,
- iyf = Icn - Icc, iyb = Icc - Icp, iyc = (Icn - Icp)/2;
- cimg_pragma_openmp(atomic) *(ptrd0++)+=(ixf*ixf + ixb*ixb)/2;
- cimg_pragma_openmp(atomic) *(ptrd1++)+=ixc*iyc;
- cimg_pragma_openmp(atomic) *(ptrd2++)+=(iyf*iyf + iyb*iyb)/2;
- }
- }
- }
- }
- return res;
- }
- //! Compute field of diffusion tensors for edge-preserving smoothing.
- /**
- \param sharpness Sharpness
- \param anisotropy Anisotropy
- \param alpha Standard deviation of the gradient blur.
- \param sigma Standard deviation of the structure tensor blur.
- \param is_sqrt Tells if the square root of the tensor field is computed instead.
- **/
- CImg<T>& diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f,
- const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) {
- CImg<Tfloat> res;
- const float
- nsharpness = std::max(sharpness,1e-5f),
- power1 = (is_sqrt?0.5f:1)*nsharpness,
- power2 = power1/(1e-7f + 1 - anisotropy);
- blur(alpha).normalize(0,(T)255);
- if (_depth>1) { // 3D
- get_structure_tensors().move_to(res).blur(sigma);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height*_depth>=(cimg_openmp_sizefactor)*256))
- cimg_forYZ(*this,y,z) {
- Tfloat
- *ptrd0 = res.data(0,y,z,0), *ptrd1 = res.data(0,y,z,1), *ptrd2 = res.data(0,y,z,2),
- *ptrd3 = res.data(0,y,z,3), *ptrd4 = res.data(0,y,z,4), *ptrd5 = res.data(0,y,z,5);
- CImg<floatT> val(3), vec(3,3);
- cimg_forX(*this,x) {
- res.get_tensor_at(x,y,z).symmetric_eigen(val,vec);
- const float
- _l1 = val[2], _l2 = val[1], _l3 = val[0],
- l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0, l3 = _l3>0?_l3:0,
- ux = vec(0,0), uy = vec(0,1), uz = vec(0,2),
- vx = vec(1,0), vy = vec(1,1), vz = vec(1,2),
- wx = vec(2,0), wy = vec(2,1), wz = vec(2,2),
- n1 = (float)std::pow(1 + l1 + l2 + l3,-power1),
- n2 = (float)std::pow(1 + l1 + l2 + l3,-power2);
- *(ptrd0++) = n1*(ux*ux + vx*vx) + n2*wx*wx;
- *(ptrd1++) = n1*(ux*uy + vx*vy) + n2*wx*wy;
- *(ptrd2++) = n1*(ux*uz + vx*vz) + n2*wx*wz;
- *(ptrd3++) = n1*(uy*uy + vy*vy) + n2*wy*wy;
- *(ptrd4++) = n1*(uy*uz + vy*vz) + n2*wy*wz;
- *(ptrd5++) = n1*(uz*uz + vz*vz) + n2*wz*wz;
- }
- }
- } else { // for 2D images
- get_structure_tensors().move_to(res).blur(sigma);
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*256 &&
- _height>=(cimg_openmp_sizefactor)*256))
- cimg_forY(*this,y) {
- Tfloat *ptrd0 = res.data(0,y,0,0), *ptrd1 = res.data(0,y,0,1), *ptrd2 = res.data(0,y,0,2);
- CImg<floatT> val(2), vec(2,2);
- cimg_forX(*this,x) {
- res.get_tensor_at(x,y).symmetric_eigen(val,vec);
- const float
- _l1 = val[1], _l2 = val[0],
- l1 = _l1>0?_l1:0, l2 = _l2>0?_l2:0,
- ux = vec(1,0), uy = vec(1,1),
- vx = vec(0,0), vy = vec(0,1),
- n1 = (float)std::pow(1 + l1 + l2,-power1),
- n2 = (float)std::pow(1 + l1 + l2,-power2);
- *(ptrd0++) = n1*ux*ux + n2*vx*vx;
- *(ptrd1++) = n1*ux*uy + n2*vx*vy;
- *(ptrd2++) = n1*uy*uy + n2*vy*vy;
- }
- }
- }
- return res.move_to(*this);
- }
- //! Compute field of diffusion tensors for edge-preserving smoothing \newinstance.
- CImg<Tfloat> get_diffusion_tensors(const float sharpness=0.7f, const float anisotropy=0.6f,
- const float alpha=0.6f, const float sigma=1.1f, const bool is_sqrt=false) const {
- return CImg<Tfloat>(*this,false).diffusion_tensors(sharpness,anisotropy,alpha,sigma,is_sqrt);
- }
- //! Estimate displacement field between two images.
- /**
- \param source Reference image.
- \param smoothness Smoothness of estimated displacement field.
- \param precision Precision required for algorithm convergence.
- \param nb_scales Number of scales used to estimate the displacement field.
- \param iteration_max Maximum number of iterations allowed for one scale.
- \param is_backward If false, match I2(X + U(X)) = I1(X), else match I2(X) = I1(X - U(X)).
- \param guide Image used as the initial correspondence estimate for the algorithm.
- 'guide' may have a last channel with boolean values (0=false | other=true) that
- tells for each pixel if its correspondence vector is constrained to its initial value (constraint mask).
- **/
- CImg<T>& displacement(const CImg<T>& source, const float smoothness=0.1f, const float precision=5.f,
- const unsigned int nb_scales=0, const unsigned int iteration_max=10000,
- const bool is_backward=false,
- const CImg<floatT>& guide=CImg<floatT>::const_empty()) {
- return get_displacement(source,smoothness,precision,nb_scales,iteration_max,is_backward,guide).
- move_to(*this);
- }
- //! Estimate displacement field between two images \newinstance.
- CImg<floatT> get_displacement(const CImg<T>& source,
- const float smoothness=0.1f, const float precision=5.f,
- const unsigned int nb_scales=0, const unsigned int iteration_max=10000,
- const bool is_backward=false,
- const CImg<floatT>& guide=CImg<floatT>::const_empty()) const {
- if (is_empty() || !source) return +*this;
- if (!is_sameXYZC(source))
- throw CImgArgumentException(_cimg_instance
- "displacement(): Instance and source image (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- source._width,source._height,source._depth,source._spectrum,source._data);
- if (precision<0)
- throw CImgArgumentException(_cimg_instance
- "displacement(): Invalid specified precision %g "
- "(should be >=0)",
- cimg_instance,
- precision);
- const bool is_3d = source._depth>1;
- const unsigned int constraint = is_3d?3:2;
- if (guide &&
- (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<constraint))
- throw CImgArgumentException(_cimg_instance
- "displacement(): Specified guide (%u,%u,%u,%u,%p) "
- "has invalid dimensions.",
- cimg_instance,
- guide._width,guide._height,guide._depth,guide._spectrum,guide._data);
- const unsigned int
- mins = is_3d?cimg::min(_width,_height,_depth):std::min(_width,_height),
- _nb_scales = nb_scales>0?nb_scales:
- (unsigned int)cimg::round(std::log(mins/8.)/std::log(1.5),1,1);
- const float _precision = (float)std::pow(10.,-(double)precision);
- float sm, sM = source.max_min(sm), tm, tM = max_min(tm);
- const float sdelta = sm==sM?1:(sM - sm), tdelta = tm==tM?1:(tM - tm);
- CImg<floatT> U, V;
- floatT bound = 0;
- for (int scale = (int)_nb_scales - 1; scale>=0; --scale) {
- const float factor = (float)std::pow(1.5,(double)scale);
- const unsigned int
- _sw = (unsigned int)(_width/factor), sw = _sw?_sw:1,
- _sh = (unsigned int)(_height/factor), sh = _sh?_sh:1,
- _sd = (unsigned int)(_depth/factor), sd = _sd?_sd:1;
- if (sw<5 && sh<5 && (!is_3d || sd<5)) continue; // Skip too small scales
- const CImg<Tfloat>
- I1 = (source.get_resize(sw,sh,sd,-100,2)-=sm)/=sdelta,
- I2 = (get_resize(I1,2)-=tm)/=tdelta;
- if (guide._spectrum>constraint) guide.get_resize(I2._width,I2._height,I2._depth,-100,1).move_to(V);
- if (U) (U*=1.5f).resize(I2._width,I2._height,I2._depth,-100,3);
- else {
- if (guide)
- guide.get_shared_channels(0,is_3d?2:1).get_resize(I2._width,I2._height,I2._depth,-100,2).move_to(U);
- else U.assign(I2._width,I2._height,I2._depth,is_3d?3:2,0);
- }
- float dt = 2, energy = cimg::type<float>::max();
- const CImgList<Tfloat> dI = is_backward?I1.get_gradient():I2.get_gradient();
- cimg_abort_init;
- for (unsigned int iteration = 0; iteration<iteration_max; ++iteration) {
- cimg_abort_test;
- float _energy = 0;
- if (is_3d) { // 3D version
- if (smoothness>=0) // Isotropic regularization
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_height*_depth>=(cimg_openmp_sizefactor)*8 &&
- _width>=(cimg_openmp_sizefactor)*16)
- reduction(+:_energy))
- cimg_forYZ(U,y,z) {
- const int
- _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y,
- _p1z = z?z - 1:0, _n1z = z<U.depth() - 1?z + 1:z;
- cimg_for3X(U,x) {
- const float
- X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0),
- Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1),
- Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2);
- float delta_I = 0, _energy_regul = 0;
- if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c));
- else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2._linear_atXYZ(X,Y,Z,c));
- cimg_forC(U,c) {
- const float
- Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)),
- Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)),
- Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)),
- Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c),
- Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c),
- Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c);
- U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c]._linear_atXYZ(X,Y,Z) +
- smoothness* ( Uxx + Uyy + Uzz)))/(1 + 6*smoothness*dt);
- _energy_regul+=Ux*Ux + Uy*Uy + Uz*Uz;
- }
- if (is_backward) { // Constraint displacement vectors to stay in image
- if (U(x,y,z,0)>x) U(x,y,z,0) = (float)x;
- if (U(x,y,z,1)>y) U(x,y,z,1) = (float)y;
- if (U(x,y,z,2)>z) U(x,y,z,2) = (float)z;
- bound = (float)x - _width; if (U(x,y,z,0)<=bound) U(x,y,z,0) = bound;
- bound = (float)y - _height; if (U(x,y,z,1)<=bound) U(x,y,z,1) = bound;
- bound = (float)z - _depth; if (U(x,y,z,2)<=bound) U(x,y,z,2) = bound;
- } else {
- if (U(x,y,z,0)<-x) U(x,y,z,0) = -(float)x;
- if (U(x,y,z,1)<-y) U(x,y,z,1) = -(float)y;
- if (U(x,y,z,2)<-z) U(x,y,z,2) = -(float)z;
- bound = (float)_width - x; if (U(x,y,z,0)>=bound) U(x,y,z,0) = bound;
- bound = (float)_height - y; if (U(x,y,z,1)>=bound) U(x,y,z,1) = bound;
- bound = (float)_depth - z; if (U(x,y,z,2)>=bound) U(x,y,z,2) = bound;
- }
- _energy+=delta_I*delta_I + smoothness*_energy_regul;
- }
- if (V) cimg_forXYZ(V,_x,_y,_z) if (V(_x,_y,_z,3)) { // Apply constraints
- U(_x,_y,_z,0) = V(_x,_y,_z,0)/factor;
- U(_x,_y,_z,1) = V(_x,_y,_z,1)/factor;
- U(_x,_y,_z,2) = V(_x,_y,_z,2)/factor;
- }
- } else { // Anisotropic regularization
- const float nsmoothness = -smoothness;
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_height*_depth>=(cimg_openmp_sizefactor)*8 &&
- _width>=(cimg_openmp_sizefactor)*16)
- reduction(+:_energy))
- cimg_forYZ(U,y,z) {
- const int
- _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y,
- _p1z = z?z - 1:0, _n1z = z<U.depth() - 1?z + 1:z;
- cimg_for3X(U,x) {
- const float
- X = is_backward?x - U(x,y,z,0):x + U(x,y,z,0),
- Y = is_backward?y - U(x,y,z,1):y + U(x,y,z,1),
- Z = is_backward?z - U(x,y,z,2):z + U(x,y,z,2);
- float delta_I = 0, _energy_regul = 0;
- if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXYZ(X,Y,Z,c) - I2(x,y,z,c));
- else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,z,c) - I2._linear_atXYZ(X,Y,Z,c));
- cimg_forC(U,c) {
- const float
- Ux = 0.5f*(U(_n1x,y,z,c) - U(_p1x,y,z,c)),
- Uy = 0.5f*(U(x,_n1y,z,c) - U(x,_p1y,z,c)),
- Uz = 0.5f*(U(x,y,_n1z,c) - U(x,y,_p1z,c)),
- N2 = Ux*Ux + Uy*Uy + Uz*Uz,
- N = std::sqrt(N2),
- N3 = 1e-5f + N2*N,
- coef_a = (1 - Ux*Ux/N2)/N,
- coef_b = -2*Ux*Uy/N3,
- coef_c = -2*Ux*Uz/N3,
- coef_d = (1 - Uy*Uy/N2)/N,
- coef_e = -2*Uy*Uz/N3,
- coef_f = (1 - Uz*Uz/N2)/N,
- Uxx = U(_n1x,y,z,c) + U(_p1x,y,z,c),
- Uyy = U(x,_n1y,z,c) + U(x,_p1y,z,c),
- Uzz = U(x,y,_n1z,c) + U(x,y,_p1z,c),
- Uxy = 0.25f*(U(_n1x,_n1y,z,c) + U(_p1x,_p1y,z,c) - U(_n1x,_p1y,z,c) - U(_n1x,_p1y,z,c)),
- Uxz = 0.25f*(U(_n1x,y,_n1z,c) + U(_p1x,y,_p1z,c) - U(_n1x,y,_p1z,c) - U(_n1x,y,_p1z,c)),
- Uyz = 0.25f*(U(x,_n1y,_n1z,c) + U(x,_p1y,_p1z,c) - U(x,_n1y,_p1z,c) - U(x,_n1y,_p1z,c));
- U(x,y,z,c) = (float)(U(x,y,z,c) + dt*(delta_I*dI[c]._linear_atXYZ(X,Y,Z) +
- nsmoothness* ( coef_a*Uxx + coef_b*Uxy +
- coef_c*Uxz + coef_d*Uyy +
- coef_e*Uyz + coef_f*Uzz ))
- )/(1 + 2*(coef_a + coef_d + coef_f)*nsmoothness*dt);
- _energy_regul+=N;
- }
- if (is_backward) { // Constraint displacement vectors to stay in image
- if (U(x,y,z,0)>x) U(x,y,z,0) = (float)x;
- if (U(x,y,z,1)>y) U(x,y,z,1) = (float)y;
- if (U(x,y,z,2)>z) U(x,y,z,2) = (float)z;
- bound = (float)x - _width; if (U(x,y,z,0)<=bound) U(x,y,z,0) = bound;
- bound = (float)y - _height; if (U(x,y,z,1)<=bound) U(x,y,z,1) = bound;
- bound = (float)z - _depth; if (U(x,y,z,2)<=bound) U(x,y,z,2) = bound;
- } else {
- if (U(x,y,z,0)<-x) U(x,y,z,0) = -(float)x;
- if (U(x,y,z,1)<-y) U(x,y,z,1) = -(float)y;
- if (U(x,y,z,2)<-z) U(x,y,z,2) = -(float)z;
- bound = (float)_width - x; if (U(x,y,z,0)>=bound) U(x,y,z,0) = bound;
- bound = (float)_height - y; if (U(x,y,z,1)>=bound) U(x,y,z,1) = bound;
- bound = (float)_depth - z; if (U(x,y,z,2)>=bound) U(x,y,z,2) = bound;
- }
- _energy+=delta_I*delta_I + nsmoothness*_energy_regul;
- }
- if (V) cimg_forXYZ(V,_x,_y,_z) if (V(_x,_y,_z,3)) { // Apply constraints
- U(_x,_y,_z,0) = V(_x,_y,_z,0)/factor;
- U(_x,_y,_z,1) = V(_x,_y,_z,1)/factor;
- U(_x,_y,_z,2) = V(_x,_y,_z,2)/factor;
- }
- }
- }
- } else { // 2D version
- if (smoothness>=0) // Isotropic regularization
- cimg_pragma_openmp(parallel for cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*8 &&
- _width>=(cimg_openmp_sizefactor)*16) reduction(+:_energy))
- cimg_forY(U,y) {
- const int _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y;
- cimg_for3X(U,x) {
- const float
- X = is_backward?x - U(x,y,0):x + U(x,y,0),
- Y = is_backward?y - U(x,y,1):y + U(x,y,1);
- float delta_I = 0, _energy_regul = 0;
- if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXY(X,Y,c) - I2(x,y,c));
- else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2._linear_atXY(X,Y,c));
- cimg_forC(U,c) {
- const float
- Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)),
- Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)),
- Uxx = U(_n1x,y,c) + U(_p1x,y,c),
- Uyy = U(x,_n1y,c) + U(x,_p1y,c);
- U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c]._linear_atXY(X,Y) +
- smoothness*( Uxx + Uyy )))/(1 + 4*smoothness*dt);
- _energy_regul+=Ux*Ux + Uy*Uy;
- }
- if (is_backward) { // Constraint displacement vectors to stay in image
- if (U(x,y,0)>x) U(x,y,0) = (float)x;
- if (U(x,y,1)>y) U(x,y,1) = (float)y;
- bound = (float)x - _width; if (U(x,y,0)<=bound) U(x,y,0) = bound;
- bound = (float)y - _height; if (U(x,y,1)<=bound) U(x,y,1) = bound;
- } else {
- if (U(x,y,0)<-x) U(x,y,0) = -(float)x;
- if (U(x,y,1)<-y) U(x,y,1) = -(float)y;
- bound = (float)_width - x; if (U(x,y,0)>=bound) U(x,y,0) = bound;
- bound = (float)_height - y; if (U(x,y,1)>=bound) U(x,y,1) = bound;
- }
- _energy+=delta_I*delta_I + smoothness*_energy_regul;
- }
- if (V) cimg_forXY(V,_x,_y) if (V(_x,_y,2)) { // Apply constraints
- U(_x,_y,0) = V(_x,_y,0)/factor;
- U(_x,_y,1) = V(_x,_y,1)/factor;
- }
- } else { // Anisotropic regularization
- const float nsmoothness = -smoothness;
- cimg_pragma_openmp(parallel for cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*8 &&
- _width>=(cimg_openmp_sizefactor)*16) reduction(+:_energy))
- cimg_forY(U,y) {
- const int _p1y = y?y - 1:0, _n1y = y<U.height() - 1?y + 1:y;
- cimg_for3X(U,x) {
- const float
- X = is_backward?x - U(x,y,0):x + U(x,y,0),
- Y = is_backward?y - U(x,y,1):y + U(x,y,1);
- float delta_I = 0, _energy_regul = 0;
- if (is_backward) cimg_forC(I2,c) delta_I+=(float)(I1._linear_atXY(X,Y,c) - I2(x,y,c));
- else cimg_forC(I2,c) delta_I+=(float)(I1(x,y,c) - I2._linear_atXY(X,Y,c));
- cimg_forC(U,c) {
- const float
- Ux = 0.5f*(U(_n1x,y,c) - U(_p1x,y,c)),
- Uy = 0.5f*(U(x,_n1y,c) - U(x,_p1y,c)),
- N2 = Ux*Ux + Uy*Uy,
- N = std::sqrt(N2),
- N3 = 1e-5f + N2*N,
- coef_a = Uy*Uy/N3,
- coef_b = -2*Ux*Uy/N3,
- coef_c = Ux*Ux/N3,
- Uxx = U(_n1x,y,c) + U(_p1x,y,c),
- Uyy = U(x,_n1y,c) + U(x,_p1y,c),
- Uxy = 0.25f*(U(_n1x,_n1y,c) + U(_p1x,_p1y,c) - U(_n1x,_p1y,c) - U(_n1x,_p1y,c));
- U(x,y,c) = (float)(U(x,y,c) + dt*(delta_I*dI[c]._linear_atXY(X,Y) +
- nsmoothness*( coef_a*Uxx + coef_b*Uxy + coef_c*Uyy )))/
- (1 + 2*(coef_a + coef_c)*nsmoothness*dt);
- _energy_regul+=N;
- }
- if (is_backward) { // Constraint displacement vectors to stay in image
- if (U(x,y,0)>x) U(x,y,0) = (float)x;
- if (U(x,y,1)>y) U(x,y,1) = (float)y;
- bound = (float)x - _width; if (U(x,y,0)<=bound) U(x,y,0) = bound;
- bound = (float)y - _height; if (U(x,y,1)<=bound) U(x,y,1) = bound;
- } else {
- if (U(x,y,0)<-x) U(x,y,0) = -(float)x;
- if (U(x,y,1)<-y) U(x,y,1) = -(float)y;
- bound = (float)_width - x; if (U(x,y,0)>=bound) U(x,y,0) = bound;
- bound = (float)_height - y; if (U(x,y,1)>=bound) U(x,y,1) = bound;
- }
- _energy+=delta_I*delta_I + nsmoothness*_energy_regul;
- }
- if (V) cimg_forXY(V,_x,_y) if (V(_x,_y,2)) { // Apply constraints
- U(_x,_y,0) = V(_x,_y,0)/factor;
- U(_x,_y,1) = V(_x,_y,1)/factor;
- }
- }
- }
- }
- const float d_energy = (_energy - energy)/(sw*sh*sd);
- if (d_energy<=0 && -d_energy<_precision) break;
- if (d_energy>0) dt*=0.5f;
- energy = _energy;
- }
- }
- return U;
- }
- //! Compute correspondence map between two images, using a patch-matching algorithm.
- /**
- \param patch_image The image containing the reference patches to match with the instance image.
- \param patch_width Width of the patch used for matching.
- \param patch_height Height of the patch used for matching.
- \param patch_depth Depth of the patch used for matching.
- \param nb_iterations Number of patch-match iterations.
- \param nb_randoms Number of randomization attempts (per pixel).
- \param patch_penalization Penalization factor in score related patch occurrences.
- if negative, also tells that identity result is not avoided.
- \param guide Image used as the initial correspondence estimate for the algorithm.
- 'guide' may have a last channel with boolean values (0=false | other=true) that
- tells for each pixel if its correspondence vector is constrained to its initial value (constraint mask).
- \param[out] matching_score Returned as the image of matching scores.
- **/
- template<typename t1, typename t2>
- CImg<T>& matchpatch(const CImg<T>& patch_image,
- const unsigned int patch_width,
- const unsigned int patch_height,
- const unsigned int patch_depth,
- const unsigned int nb_iterations,
- const unsigned int nb_randoms,
- const float patch_penalization,
- const CImg<t1> &guide,
- CImg<t2> &matching_score) {
- return get_matchpatch(patch_image,patch_width,patch_height,patch_depth,
- nb_iterations,nb_randoms,patch_penalization,guide,matching_score).move_to(*this);
- }
- //! Compute correspondence map between two images, using the patch-match algorithm \newinstance.
- template<typename t1, typename t2>
- CImg<intT> get_matchpatch(const CImg<T>& patch_image,
- const unsigned int patch_width,
- const unsigned int patch_height,
- const unsigned int patch_depth,
- const unsigned int nb_iterations,
- const unsigned int nb_randoms,
- const float patch_penalization,
- const CImg<t1> &guide,
- CImg<t2> &matching_score) const {
- return _matchpatch(patch_image,patch_width,patch_height,patch_depth,
- nb_iterations,nb_randoms,patch_penalization,
- guide,true,matching_score);
- }
- //! Compute correspondence map between two images, using the patch-match algorithm \overloading.
- template<typename t>
- CImg<T>& matchpatch(const CImg<T>& patch_image,
- const unsigned int patch_width,
- const unsigned int patch_height,
- const unsigned int patch_depth,
- const unsigned int nb_iterations=5,
- const unsigned int nb_randoms=5,
- const float patch_penalization=0,
- const CImg<t> &guide=CImg<t>::const_empty()) {
- return get_matchpatch(patch_image,patch_width,patch_height,patch_depth,
- nb_iterations,nb_randoms,patch_penalization,guide).move_to(*this);
- }
- //! Compute correspondence map between two images, using the patch-match algorithm \overloading.
- template<typename t>
- CImg<intT> get_matchpatch(const CImg<T>& patch_image,
- const unsigned int patch_width,
- const unsigned int patch_height,
- const unsigned int patch_depth,
- const unsigned int nb_iterations=5,
- const unsigned int nb_randoms=5,
- const float patch_penalization=0,
- const CImg<t> &guide=CImg<t>::const_empty()) const {
- CImg<T> matching_score;
- return _matchpatch(patch_image,patch_width,patch_height,patch_depth,
- nb_iterations,nb_randoms,patch_penalization,guide,false,matching_score);
- }
- template<typename t1, typename t2>
- CImg<intT> _matchpatch(const CImg<T>& patch_image,
- const unsigned int patch_width,
- const unsigned int patch_height,
- const unsigned int patch_depth,
- const unsigned int nb_iterations,
- const unsigned int nb_randoms,
- const float patch_penalization,
- const CImg<t1> &guide,
- const bool is_matching_score,
- CImg<t2> &matching_score) const {
- if (is_empty()) return CImg<intT>::const_empty();
- if (patch_image._spectrum!=_spectrum)
- throw CImgArgumentException(_cimg_instance
- "matchpatch(): Instance image and specified patch image (%u,%u,%u,%u,%p) "
- "have different spectrums.",
- cimg_instance,
- patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
- patch_image._data);
- if (patch_width>_width || patch_height>_height || patch_depth>_depth)
- throw CImgArgumentException(_cimg_instance
- "matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions "
- "of the instance image.",
- cimg_instance,patch_width,patch_height,patch_depth);
- if (patch_width>patch_image._width || patch_height>patch_image._height || patch_depth>patch_image._depth)
- throw CImgArgumentException(_cimg_instance
- "matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions "
- "of the patch image image (%u,%u,%u,%u,%p).",
- cimg_instance,patch_width,patch_height,patch_depth,
- patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
- patch_image._data);
- const unsigned int
- _constraint = patch_image._depth>1?3:2,
- constraint = guide._spectrum>_constraint?_constraint:0;
- if (guide &&
- (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<_constraint))
- throw CImgArgumentException(_cimg_instance
- "matchpatch(): Specified guide (%u,%u,%u,%u,%p) has invalid dimensions "
- "considering instance and patch image (%u,%u,%u,%u,%p).",
- cimg_instance,
- guide._width,guide._height,guide._depth,guide._spectrum,guide._data,
- patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,
- patch_image._data);
- CImg<intT> a_map(_width,_height,_depth,patch_image._depth>1?3:2);
- CImg<ucharT> is_updated(_width,_height,_depth,1,3);
- CImg<floatT> score(_width,_height,_depth), penalty;
- const float _patch_penalization = cimg::abs(patch_penalization);
- const bool allow_identity = patch_penalization>=0;
- if (_patch_penalization!=0)
- penalty.assign(patch_image._width,patch_image._height,patch_image._depth,1,0);
- const int
- psizew = (int)patch_width, psizew1 = psizew/2, psizew2 = psizew - psizew1 - 1,
- psizeh = (int)patch_height, psizeh1 = psizeh/2, psizeh2 = psizeh - psizeh1 - 1,
- psized = (int)patch_depth, psized1 = psized/2, psized2 = psized - psized1 - 1;
- // Interleave image buffers to speed up patch comparison (more cache-friendly).
- CImg<T> in_this = get_permute_axes("cxyz");
- in_this._width = _width*_spectrum;
- in_this._height = _height;
- in_this._depth = _depth;
- in_this._spectrum = 1;
- CImg<T> in_patch = patch_image.get_permute_axes("cxyz");
- in_patch._width = patch_image._width*patch_image._spectrum;
- in_patch._height = patch_image._height;
- in_patch._depth = patch_image._depth;
- in_patch._spectrum = 1;
- if (_depth>1 || patch_image._depth>1) { // 3D version
- // Initialize correspondence map.
- if (guide)
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if_size(_width,64))
- cimg_forXYZ(*this,x,y,z) { // User-defined initialization
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
- u = cimg::cut((int)guide(x,y,z,0),cx1,patch_image.width() - 1 - cx2),
- v = cimg::cut((int)guide(x,y,z,1),cy1,patch_image.height() - 1 - cy2),
- w = cimg::cut((int)guide(x,y,z,2),cz1,patch_image.depth() - 1 - cz2);
- a_map(x,y,z,0) = u;
- a_map(x,y,z,1) = v;
- a_map(x,y,z,2) = w;
- score(x,y,z) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- x - cx1,y - cy1,z - cz1,
- u - cx1,v - cy1,w - cz1,
- u,v,w,0,allow_identity,cimg::type<float>::inf());
- } else cimg_pragma_openmp(parallel cimg_openmp_if_size(_width,64)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for cimg_openmp_collapse(2))
- cimg_forXYZ(*this,x,y,z) { // Random initialization
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
- u = (int)cimg::round(cimg::rand(cx1,patch_image.width() - 1 - cx2,&rng)),
- v = (int)cimg::round(cimg::rand(cy1,patch_image.height() - 1 - cy2,&rng)),
- w = (int)cimg::round(cimg::rand(cz1,patch_image.depth() - 1 - cz2,&rng));
- a_map(x,y,z,0) = u;
- a_map(x,y,z,1) = v;
- a_map(x,y,z,2) = w;
- score(x,y,z) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- x - cx1,y - cy1,z - cz1,
- u - cx1,v - cy1,w - cz1,
- u,v,w,0,allow_identity,cimg::type<float>::inf());
- }
- cimg::srand(rng);
- }
- // Start iteration loop.
- cimg_abort_init;
- for (unsigned int iter = 0; iter<nb_iterations; ++iter) {
- cimg_abort_test;
- const bool is_backward = iter&1, is_forward = !is_backward;
- const unsigned int cmask = is_backward?1:2, nmask = 3 - cmask;
- cimg_pragma_openmp(parallel cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for cimg_openmp_collapse(2))
- cimg_forXYZ(*this,X,Y,Z) {
- const int
- x = is_backward?width() - 1 - X:X,
- y = is_backward?height() - 1 - Y:Y,
- z = is_backward?depth() - 1 - Z:Z;
- if (score(x,y,z)<=1e-5 || (constraint && guide(x,y,z,constraint)!=0)) continue;
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()), cz2 = psized - cz1 - 1,
- xp = x - cx1,
- yp = y - cy1,
- zp = z - cz1;
- 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;
- const float best_score0 = score(x,y,z);
- float best_score = best_score0, s;
- if (is_forward && x>0 && (is_updated(x - 1,y,z)&cmask)) { // Compare with left neighbor
- u = a_map(x - 1,y,z,0);
- v = a_map(x - 1,y,z,1);
- w = a_map(x - 1,y,z,2);
- if (u>=cx1 - 1 && u<patch_image.width() - 1 - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2 &&
- w>=cz1 && w<patch_image.depth() - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u + 1 - cx1,v - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u + 1; best_v = v; best_w = w; best_score = s; }
- }
- }
- if (is_forward && y>0 && (is_updated(x,y - 1,z)&cmask)) { // Compare with up neighbor
- u = a_map(x,y - 1,z,0);
- v = a_map(x,y - 1,z,1);
- w = a_map(x,y - 1,z,2);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 - 1 && v<patch_image.height() - 1 - cy2 &&
- w>=cz1 && w<patch_image.depth() - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v + 1 - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v + 1; best_w = w; best_score = s; }
- }
- }
- if (is_forward && z>0 && (is_updated(x,y,z - 1)&cmask)) { // Compare with backward neighbor
- u = a_map(x,y,z - 1,0);
- v = a_map(x,y,z - 1,1);
- w = a_map(x,y,z - 1,2);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2 &&
- w>=cz1 - 1 && w<patch_image.depth() - 1 - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v - cy1,w + 1 - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v; best_w = w + 1; best_score = s; }
- }
- }
- if (is_backward && x<width() - 1 && (is_updated(x + 1,y,z)&cmask)) { // Compare with right neighbor
- u = a_map(x + 1,y,z,0);
- v = a_map(x + 1,y,z,1);
- w = a_map(x + 1,y,z,2);
- if (u>=cx1 + 1 && u<patch_image.width() + 1 - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2 &&
- w>=cz1 && w<patch_image.depth() - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - 1 - cx1,v - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u - 1; best_v = v; best_w = w; best_score = s; }
- }
- }
- if (is_backward && y<height() - 1 && (is_updated(x,y + 1,z)&cmask)) { // Compare with bottom neighbor
- u = a_map(x,y + 1,z,0);
- v = a_map(x,y + 1,z,1);
- w = a_map(x,y + 1,z,2);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 + 1 && v<patch_image.height() + 1 - cy2 &&
- w>=cz1 && w<patch_image.depth() - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v - 1 - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v - 1; best_w = w; best_score = s; }
- }
- }
- if (is_backward && z<depth() - 1 && (is_updated(x,y,z + 1)&cmask)) { // Compare with forward neighbor
- u = a_map(x,y,z + 1,0);
- v = a_map(x,y,z + 1,1);
- w = a_map(x,y,z + 1,2);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2 &&
- w>=cz1 + 1 && w<patch_image.depth() + 1 - cz2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v - cy1,w - 1 - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v; best_w = w - 1; best_score = s; }
- }
- }
- float
- dw = (float)patch_image.width(),
- dh = (float)patch_image.height(),
- dd = (float)patch_image.depth();
- for (unsigned int i = 0; i<nb_randoms; ++i) {
- u = (int)cimg::round(cimg::rand(std::max((float)cx1,best_u - dw),
- std::min(patch_image.width() - 1.f - cx2,best_u + dw),&rng));
- v = (int)cimg::round(cimg::rand(std::max((float)cy1,best_v - dh),
- std::min(patch_image.height() - 1.f - cy2,best_v + dh),&rng));
- w = (int)cimg::round(cimg::rand(std::max((float)cz1,best_w - dd),
- std::min(patch_image.depth() - 1.f - cz2,best_w + dd),&rng));
- if (u!=best_u || v!=best_v || w!=best_w) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v; best_w = w; best_score = s; }
- dw = std::max(5.f,dw*0.5f); dh = std::max(5.f,dh*0.5f); dd = std::max(5.f,dd*0.5f);
- }
- }
- if (best_score<best_score0) {
- if (_patch_penalization!=0) {
- float &p_penalty = penalty(a_map(x,y,z,0),a_map(x,y,z,1),a_map(x,y,z,2));
- if (p_penalty) cimg_pragma_openmp(atomic) --p_penalty;
- }
- a_map(x,y,z,0) = best_u;
- a_map(x,y,z,1) = best_v;
- a_map(x,y,z,2) = best_w;
- score(x,y,z) = best_score;
- is_updated(x,y,z) = 3;
- } else is_updated(x,y,z)&=~nmask;
- if (_patch_penalization!=0) cimg_pragma_openmp(atomic) ++penalty(best_u,best_v,best_w);
- }
- cimg::srand(rng);
- }
- // Update score according to new penalties.
- if (penalty)
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64))
- cimg_forXYZ(score,x,y,z) {
- const float p_score = score(x,y,z);
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()),
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()),
- cz1 = z<=psized1?z:(z<depth() - psized2?psized1:psized + z - depth()),
- xp = x - cx1,
- yp = y - cy1,
- zp = z - cz1,
- u = a_map(x,y,z,0),
- v = a_map(x,y,z,1),
- w = a_map(x,y,z,2);
- const float n_score = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,patch_depth,_spectrum,
- xp,yp,zp,u - cx1,v - cy1,w - cz1,
- u,v,w,_patch_penalization,allow_identity,cimg::type<float>::inf());
- if (n_score!=p_score) { score(x,y,z) = n_score; is_updated(x,y) = 3; }
- }
- }
- } else { // 2D version
- // Initialize correspondence map.
- if (guide)
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,64))
- cimg_forXY(*this,x,y) { // User-defined initialization
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- u = cimg::cut((int)guide(x,y,0),cx1,patch_image.width() - 1 - cx2),
- v = cimg::cut((int)guide(x,y,1),cy1,patch_image.height() - 1 - cy2);
- a_map(x,y,0) = u;
- a_map(x,y,1) = v;
- score(x,y) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- x - cx1,y - cy1,u - cx1,v - cy1,
- u,v,0,allow_identity,cimg::type<float>::inf());
- } else cimg_pragma_openmp(parallel cimg_openmp_if_size(_width,64)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_forXY(*this,x,y) { // Random initialization
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- u = (int)cimg::round(cimg::rand(cx1,patch_image.width() - 1 - cx2,&rng)),
- v = (int)cimg::round(cimg::rand(cy1,patch_image.height() - 1 - cy2,&rng));
- a_map(x,y,0) = u;
- a_map(x,y,1) = v;
- score(x,y) = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- x - cx1,y - cy1,u - cx1,v - cy1,
- u,v,0,allow_identity,cimg::type<float>::inf());
- }
- cimg::srand(rng);
- }
- // Start iteration loop.
- cimg_abort_init;
- for (unsigned int iter = 0; iter<nb_iterations; ++iter) {
- cimg_abort_test;
- const bool is_backward = iter&1, is_forward = !is_backward;
- const unsigned int cmask = is_backward?1:2, nmask = 3 - cmask;
- cimg_pragma_openmp(parallel cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64)) {
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- #if cimg_use_openmp!=0
- rng+=omp_get_thread_num();
- #endif
- cimg_pragma_openmp(for)
- cimg_forXY(*this,X,Y) {
- const int
- x = is_backward?width() - 1 - X:X,
- y = is_backward?height() - 1 - Y:Y;
- if (score(x,y)<=1e-5 || (constraint && guide(x,y,constraint)!=0)) continue;
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()), cx2 = psizew - cx1 - 1,
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()), cy2 = psizeh - cy1 - 1,
- xp = x - cx1,
- yp = y - cy1;
- int best_u = a_map(x,y,0), best_v = a_map(x,y,1), u, v;
- const float best_score0 = score(x,y);
- float best_score = best_score0, s;
- if (is_forward && x>0 && (is_updated(x - 1,y)&cmask)) { // Compare with left neighbor
- u = a_map(x - 1,y,0);
- v = a_map(x - 1,y,1);
- if (u>=cx1 - 1 && u<patch_image.width() - 1 - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u + 1 - cx1,v - cy1,
- u,v,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u + 1; best_v = v; best_score = s; }
- }
- }
- if (is_forward && y>0 && (is_updated(x,y - 1)&cmask)) { // Compare with up neighbor
- u = a_map(x,y - 1,0);
- v = a_map(x,y - 1,1);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 - 1 && v<patch_image.height() - 1 - cy2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u - cx1,v + 1 - cy1,
- u,v,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v + 1; best_score = s; }
- }
- }
- if (is_backward && x<width() - 1 && (is_updated(x + 1,y)&cmask)) { // Compare with right neighbor
- u = a_map(x + 1,y,0);
- v = a_map(x + 1,y,1);
- if (u>=cx1 + 1 && u<patch_image.width() + 1 - cx2 &&
- v>=cy1 && v<patch_image.height() - cy2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u - 1 - cx1,v - cy1,
- u,v,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u - 1; best_v = v; best_score = s; }
- }
- }
- if (is_backward && y<height() - 1 && (is_updated(x,y + 1)&cmask)) { // Compare with bottom neighbor
- u = a_map(x,y + 1,0);
- v = a_map(x,y + 1,1);
- if (u>=cx1 && u<patch_image.width() - cx2 &&
- v>=cy1 + 1 && v<patch_image.height() + 1 - cy2) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u - cx1,v - 1 - cy1,
- u,v,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v - 1; best_score = s; }
- }
- }
- float
- dw = (float)patch_image.width(),
- dh = (float)patch_image.height();
- for (unsigned int i = 0; i<nb_randoms; ++i) {
- u = (int)cimg::round(cimg::rand(std::max((float)cx1,best_u - dw),
- std::min(patch_image.width() - 1.f - cx2,best_u + dw),&rng));
- v = (int)cimg::round(cimg::rand(std::max((float)cy1,best_v - dh),
- std::min(patch_image.height() - 1.f - cy2,best_v + dh),&rng));
- if (u!=best_u || v!=best_v) {
- s = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u - cx1,v - cy1,
- u,v,_patch_penalization,allow_identity,best_score);
- if (s<best_score) { best_u = u; best_v = v; best_score = s; }
- dw = std::max(5.f,dw*0.5f); dh = std::max(5.f,dh*0.5f);
- }
- }
- if (best_score<best_score0) {
- if (_patch_penalization!=0) {
- float &p_penalty = penalty(a_map(x,y,0),a_map(x,y,1));
- if (p_penalty) cimg_pragma_openmp(atomic) --p_penalty;
- }
- a_map(x,y,0) = best_u;
- a_map(x,y,1) = best_v;
- score(x,y) = best_score;
- is_updated(x,y) = 3;
- } else is_updated(x,y)&=~nmask;
- if (_patch_penalization!=0) cimg_pragma_openmp(atomic) ++penalty(best_u,best_v);
- }
- cimg::srand(rng);
- }
- // Update score according to new penalties.
- if (penalty)
- cimg_pragma_openmp(parallel for cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*64))
- cimg_forXY(score,x,y) {
- const float p_score = score(x,y);
- const int
- cx1 = x<=psizew1?x:(x<width() - psizew2?psizew1:psizew + x - width()),
- cy1 = y<=psizeh1?y:(y<height() - psizeh2?psizeh1:psizeh + y - height()),
- xp = x - cx1,
- yp = y - cy1,
- u = a_map(x,y,0),
- v = a_map(x,y,1);
- const float n_score = _matchpatch(in_this,in_patch,penalty,patch_width,patch_height,_spectrum,
- xp,yp,u - cx1,v - cy1,
- u,v,_patch_penalization,allow_identity,cimg::type<float>::inf());
- if (n_score!=p_score) { score(x,y) = n_score; is_updated(x,y) = 3; }
- }
- }
- }
- if (is_matching_score) score.move_to(matching_score);
- return a_map;
- }
- // Compute SSD between two patches in different images.
- static float _matchpatch(const CImg<T>& img1, const CImg<T>& img2, const CImg<floatT>& penalty,
- const unsigned int psizew, const unsigned int psizeh,
- const unsigned int psized, const unsigned int psizec,
- const int x1, const int y1, const int z1,
- const int x2, const int y2, const int z2,
- const int xc, const int yc, const int zc,
- const float patch_penalization,
- const bool allow_identity,
- const float max_score) { // 3D version
- if (!allow_identity && cimg::hypot((float)x1 - x2,(float)y1 - y2,(float)z1 - z2)<patch_penalization)
- return cimg::type<float>::inf();
- const T *p1 = img1.data(x1*psizec,y1,z1), *p2 = img2.data(x2*psizec,y2,z2);
- const unsigned int psizewc = psizew*psizec;
- const ulongT
- offx1 = (ulongT)img1._width - psizewc,
- offx2 = (ulongT)img2._width - psizewc,
- offy1 = (ulongT)img1._width*img1._height - (ulongT)psizeh*img1._width,
- offy2 = (ulongT)img2._width*img2._height - (ulongT)psizeh*img2._width;
- float ssd = 0;
- for (unsigned int k = 0; k<psized; ++k) {
- for (unsigned int j = 0; j<psizeh; ++j) {
- for (unsigned int i = 0; i<psizewc; ++i)
- ssd += cimg::sqr((Tfloat)*(p1++) - *(p2++));
- if (ssd>max_score) return max_score;
- p1+=offx1; p2+=offx2;
- }
- p1+=offy1; p2+=offy2;
- }
- return patch_penalization==0?ssd:cimg::sqr(std::sqrt(ssd) +
- patch_penalization*psizewc*psizeh*psized*penalty(xc,yc,zc)/100);
- }
- static float _matchpatch(const CImg<T>& img1, const CImg<T>& img2, const CImg<floatT>& penalty,
- const unsigned int psizew, const unsigned int psizeh, const unsigned int psizec,
- const int x1, const int y1,
- const int x2, const int y2,
- const int xc, const int yc,
- const float patch_penalization,
- const bool allow_identity,
- const float max_score) { // 2D version
- if (!allow_identity && cimg::hypot((float)x1-x2,(float)y1-y2)<patch_penalization)
- return cimg::type<float>::inf();
- const T *p1 = img1.data(x1*psizec,y1), *p2 = img2.data(x2*psizec,y2);
- const unsigned int psizewc = psizew*psizec;
- const ulongT
- offx1 = (ulongT)img1._width - psizewc,
- offx2 = (ulongT)img2._width - psizewc;
- float ssd = 0;
- for (unsigned int j = 0; j<psizeh; ++j) {
- for (unsigned int i = 0; i<psizewc; ++i)
- ssd += cimg::sqr((Tfloat)*(p1++) - *(p2++));
- if (ssd>max_score) return max_score;
- p1+=offx1; p2+=offx2;
- }
- return patch_penalization==0?ssd:cimg::sqr(std::sqrt(ssd) +
- patch_penalization*psizewc*psizeh*penalty(xc,yc)/100);
- }
- //! Compute Euclidean distance function to a specified value.
- /**
- \param value Reference value.
- \param metric Type of metric. Can be <tt>{ 0=Chebyshev | 1=Manhattan | 2=Euclidean | 3=Squared-euclidean }</tt>.
- \note
- The distance transform implementation has been submitted by A. Meijster, and implements
- the article 'W.H. Hesselink, A. Meijster, J.B.T.M. Roerdink,
- "A general algorithm for computing distance transforms in linear time.",
- In: Mathematical Morphology and its Applications to Image and Signal Processing,
- J. Goutsias, L. Vincent, and D.S. Bloomberg (eds.), Kluwer, 2000, pp. 331-340.'
- The submitted code has then been modified to fit CImg coding style and constraints.
- **/
- CImg<T>& distance(const T& value, const unsigned int metric=2) {
- if (is_empty()) return *this;
- if (cimg::type<Tint>::string()!=pixel_type()) // For datatype < int
- return CImg<Tint>(*this,false).distance((Tint)value,metric).
- cut((Tint)cimg::type<T>::min(),(Tint)cimg::type<T>::max()).move_to(*this);
- bool is_value = false;
- cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,(T)0:(T)std::max(0,99999999); // (avoid VC++ warning)
- if (!is_value) return fill(cimg::type<T>::max());
- switch (metric) {
- case 0 : return _distance_core(_distance_sep_cdt,_distance_dist_cdt); // Chebyshev
- case 1 : return _distance_core(_distance_sep_mdt,_distance_dist_mdt); // Manhattan
- case 3 : return _distance_core(_distance_sep_edt,_distance_dist_edt); // Squared Euclidean
- default : return _distance_core(_distance_sep_edt,_distance_dist_edt).sqrt(); // Euclidean
- }
- return *this;
- }
- //! Compute distance to a specified value \newinstance.
- CImg<Tfloat> get_distance(const T& value, const unsigned int metric=2) const {
- return CImg<Tfloat>(*this,false).distance((Tfloat)value,metric);
- }
- static longT _distance_sep_edt(const longT i, const longT u, const longT *const g) {
- return (u*u - i*i + g[u] - g[i])/(2*(u - i));
- }
- static longT _distance_dist_edt(const longT x, const longT i, const longT *const g) {
- return (x - i)*(x - i) + g[i];
- }
- static longT _distance_sep_mdt(const longT i, const longT u, const longT *const g) {
- return (u - i<=g[u] - g[i]?999999999:(g[u] - g[i] + u + i)/2);
- }
- static longT _distance_dist_mdt(const longT x, const longT i, const longT *const g) {
- return (x<i?i - x:x - i) + g[i];
- }
- static longT _distance_sep_cdt(const longT i, const longT u, const longT *const g) {
- const longT h = (i + u)/2;
- if (g[i]<=g[u]) { return h<i + g[u]?i + g[u]:h; }
- return h<u - g[i]?h:u - g[i];
- }
- static longT _distance_dist_cdt(const longT x, const longT i, const longT *const g) {
- const longT d = x<i?i - x:x - i;
- return d<g[i]?g[i]:d;
- }
- static void _distance_scan(const unsigned int len,
- const longT *const g,
- longT (*const sep)(const longT, const longT, const longT *const),
- longT (*const f)(const longT, const longT, const longT *const),
- longT *const s,
- longT *const t,
- longT *const dt) {
- longT q = s[0] = t[0] = 0;
- for (int u = 1; u<(int)len; ++u) { // Forward scan
- while ((q>=0) && f(t[q],s[q],g)>f(t[q],u,g)) { --q; }
- if (q<0) { q = 0; s[0] = u; }
- else { const longT w = 1 + sep(s[q], u, g); if (w<(longT)len) { ++q; s[q] = u; t[q] = w; }}
- }
- for (int u = (int)len - 1; u>=0; --u) { dt[u] = f(u,s[q],g); if (u==t[q]) --q; } // Backward scan
- }
- CImg<T>& _distance_core(longT (*const sep)(const longT, const longT, const longT *const),
- longT (*const f)(const longT, const longT, const longT *const)) {
- // Check for g++ 4.9.X, as OpenMP seems to crash for this particular function. I have no clues why.
- #define cimg_is_gcc49x (__GNUC__==4 && __GNUC_MINOR__==9)
- const ulongT wh = (ulongT)_width*_height;
- #if cimg_use_openmp!=0 && !cimg_is_gcc49x
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
- #endif
- cimg_forC(*this,c) {
- CImg<longT> g(_width), dt(_width), s(_width), t(_width);
- CImg<T> img = get_shared_channel(c);
- #if cimg_use_openmp!=0 && !cimg_is_gcc49x
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2) cimg_openmp_if(_width>=(cimg_openmp_sizefactor)*512 &&
- _height*_depth>=16)
- firstprivate(g,dt,s,t))
- #endif
- cimg_forYZ(*this,y,z) { // Over X-direction
- cimg_forX(*this,x) g[x] = (longT)img(x,y,z,0,wh);
- _distance_scan(_width,g,sep,f,s,t,dt);
- cimg_forX(*this,x) img(x,y,z,0,wh) = (T)dt[x];
- }
- if (_height>1) {
- g.assign(_height); dt.assign(_height); s.assign(_height); t.assign(_height);
- #if cimg_use_openmp!=0 && !cimg_is_gcc49x
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_height>=(cimg_openmp_sizefactor)*512 && _width*_depth>=16)
- firstprivate(g,dt,s,t))
- #endif
- cimg_forXZ(*this,x,z) { // Over Y-direction
- cimg_forY(*this,y) g[y] = (longT)img(x,y,z,0,wh);
- _distance_scan(_height,g,sep,f,s,t,dt);
- cimg_forY(*this,y) img(x,y,z,0,wh) = (T)dt[y];
- }
- }
- if (_depth>1) {
- g.assign(_depth); dt.assign(_depth); s.assign(_depth); t.assign(_depth);
- #if cimg_use_openmp!=0 && !cimg_is_gcc49x
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if(_depth>=(cimg_openmp_sizefactor)*512 && _width*_height>=16)
- firstprivate(g,dt,s,t))
- #endif
- cimg_forXY(*this,x,y) { // Over Z-direction
- cimg_forZ(*this,z) g[z] = (longT)img(x,y,z,0,wh);
- _distance_scan(_depth,g,sep,f,s,t,dt);
- cimg_forZ(*this,z) img(x,y,z,0,wh) = (T)dt[z];
- }
- }
- }
- return *this;
- }
- //! Compute chamfer distance to a specified value, with a custom metric.
- /**
- \param value Reference value.
- \param metric_mask Metric mask.
- \note The algorithm code has been initially proposed by A. Meijster, and modified by D. Tschumperlé.
- **/
- template<typename t>
- CImg<T>& distance(const T& value, const CImg<t>& metric_mask) {
- if (is_empty()) return *this;
- bool is_value = false;
- cimg_for(*this,ptr,T) *ptr = *ptr==value?is_value=true,0:(T)999999999;
- if (!is_value) return fill(cimg::type<T>::max());
- const ulongT wh = (ulongT)_width*_height;
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2))
- cimg_forC(*this,c) {
- CImg<T> img = get_shared_channel(c);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(3)
- cimg_openmp_if(_width*_height*_depth>=(cimg_openmp_sizefactor)*1024))
- cimg_forXYZ(metric_mask,dx,dy,dz) {
- const t weight = metric_mask(dx,dy,dz);
- if (weight) {
- for (int z = dz, nz = 0; z<depth(); ++z,++nz) { // Forward scan
- for (int y = dy , ny = 0; y<height(); ++y,++ny) {
- for (int x = dx, nx = 0; x<width(); ++x,++nx) {
- const T dd = img(nx,ny,nz,0,wh) + weight;
- if (dd<img(x,y,z,0,wh)) img(x,y,z,0,wh) = dd;
- }
- }
- }
- for (int z = depth() - 1 - dz, nz = depth() - 1; z>=0; --z,--nz) { // Backward scan
- for (int y = height() - 1 - dy, ny = height() - 1; y>=0; --y,--ny) {
- for (int x = width() - 1 - dx, nx = width() - 1; x>=0; --x,--nx) {
- const T dd = img(nx,ny,nz,0,wh) + weight;
- if (dd<img(x,y,z,0,wh)) img(x,y,z,0,wh) = dd;
- }
- }
- }
- }
- }
- }
- return *this;
- }
- //! Compute chamfer distance to a specified value, with a custom metric \newinstance.
- template<typename t>
- CImg<Tfloat> get_distance(const T& value, const CImg<t>& metric_mask) const {
- return CImg<Tfloat>(*this,false).distance(value,metric_mask);
- }
- //! Compute distance to a specified value, according to a custom metric (use dijkstra algorithm).
- /**
- \param value Reference value.
- \param metric Field of distance potentials.
- \param is_high_connectivity Tells if the algorithm uses low or high connectivity.
- \param[out] return_path An image containing the nodes of the minimal path.
- **/
- template<typename t, typename to>
- CImg<T>& distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,
- CImg<to>& return_path) {
- return get_distance_dijkstra(value,metric,is_high_connectivity,return_path).move_to(*this);
- }
- //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm) \newinstance.
- template<typename t, typename to>
- CImg<typename cimg::superset<t,long>::type>
- get_distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,
- CImg<to>& return_path) const {
- if (is_empty()) return return_path.assign();
- if (!is_sameXYZ(metric))
- throw CImgArgumentException(_cimg_instance
- "distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) "
- "have incompatible dimensions.",
- cimg_instance,
- metric._width,metric._height,metric._depth,metric._spectrum);
- typedef typename cimg::superset<t,long>::type td; // Type used for computing cumulative distances
- CImg<td> result(_width,_height,_depth,_spectrum), Q;
- CImg<boolT> is_queued(_width,_height,_depth,1);
- if (return_path) return_path.assign(_width,_height,_depth,_spectrum);
- cimg_forC(*this,c) {
- const CImg<T> img = get_shared_channel(c);
- const CImg<t> met = metric.get_shared_channel(c%metric._spectrum);
- CImg<td> res = result.get_shared_channel(c);
- CImg<to> path = return_path?return_path.get_shared_channel(c):CImg<to>();
- unsigned int sizeQ = 0;
- // Detect initial seeds.
- is_queued.fill(0);
- cimg_forXYZ(img,x,y,z) if (img(x,y,z)==value) {
- Q._priority_queue_insert(is_queued,sizeQ,0,x,y,z);
- res(x,y,z) = 0;
- if (path) path(x,y,z) = (to)0;
- }
- // Start distance propagation.
- while (sizeQ) {
- // Get and remove point with minimal potential from the queue.
- const int x = (int)Q(0,1), y = (int)Q(0,2), z = (int)Q(0,3);
- const td P = (td)-Q(0,0);
- Q._priority_queue_remove(sizeQ);
- // Update neighbors.
- td npot = 0;
- if (x - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x - 1,y,z) + P),x - 1,y,z)) {
- res(x - 1,y,z) = npot; if (path) path(x - 1,y,z) = (to)2;
- }
- if (x + 1<width() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x + 1,y,z) + P),x + 1,y,z)) {
- res(x + 1,y,z) = npot; if (path) path(x + 1,y,z) = (to)1;
- }
- if (y - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y - 1,z) + P),x,y - 1,z)) {
- res(x,y - 1,z) = npot; if (path) path(x,y - 1,z) = (to)8;
- }
- if (y + 1<height() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y + 1,z) + P),x,y + 1,z)) {
- res(x,y + 1,z) = npot; if (path) path(x,y + 1,z) = (to)4;
- }
- if (z - 1>=0 && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z - 1) + P),x,y,z - 1)) {
- res(x,y,z - 1) = npot; if (path) path(x,y,z - 1) = (to)32;
- }
- if (z + 1<depth() && Q._priority_queue_insert(is_queued,sizeQ,-(npot=met(x,y,z + 1) + P),x,y,z + 1)) {
- res(x,y,z + 1) = npot; if (path) path(x,y,z + 1) = (to)16;
- }
- if (is_high_connectivity) {
- const float sqrt2 = std::sqrt(2.f), sqrt3 = std::sqrt(3.f);
- // Diagonal neighbors on slice z.
- if (x - 1>=0 && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y - 1,z) + P)),x - 1,y - 1,z)) {
- res(x - 1,y - 1,z) = npot; if (path) path(x - 1,y - 1,z) = (to)10;
- }
- if (x + 1<width() && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y - 1,z) + P)),x + 1,y - 1,z)) {
- res(x + 1,y - 1,z) = npot; if (path) path(x + 1,y - 1,z) = (to)9;
- }
- if (x - 1>=0 && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y + 1,z) + P)),x - 1,y + 1,z)) {
- res(x - 1,y + 1,z) = npot; if (path) path(x - 1,y + 1,z) = (to)6;
- }
- if (x + 1<width() && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y + 1,z) + P)),x + 1,y + 1,z)) {
- res(x + 1,y + 1,z) = npot; if (path) path(x + 1,y + 1,z) = (to)5;
- }
- if (z - 1>=0) { // Diagonal neighbors on slice z - 1
- if (x - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y,z - 1) + P)),x - 1,y,z - 1)) {
- res(x - 1,y,z - 1) = npot; if (path) path(x - 1,y,z - 1) = (to)34;
- }
- if (x + 1<width() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y,z - 1) + P)),x + 1,y,z - 1)) {
- res(x + 1,y,z - 1) = npot; if (path) path(x + 1,y,z - 1) = (to)33;
- }
- if (y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y - 1,z - 1) + P)),x,y - 1,z - 1)) {
- res(x,y - 1,z - 1) = npot; if (path) path(x,y - 1,z - 1) = (to)40;
- }
- if (y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y + 1,z - 1) + P)),x,y + 1,z - 1)) {
- res(x,y + 1,z - 1) = npot; if (path) path(x,y + 1,z - 1) = (to)36;
- }
- if (x - 1>=0 && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y - 1,z - 1) + P)),
- x - 1,y - 1,z - 1)) {
- res(x - 1,y - 1,z - 1) = npot; if (path) path(x - 1,y - 1,z - 1) = (to)42;
- }
- if (x + 1<width() && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y - 1,z - 1) + P)),
- x + 1,y - 1,z - 1)) {
- res(x + 1,y - 1,z - 1) = npot; if (path) path(x + 1,y - 1,z - 1) = (to)41;
- }
- if (x - 1>=0 && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y + 1,z - 1) + P)),
- x - 1,y + 1,z - 1)) {
- res(x - 1,y + 1,z - 1) = npot; if (path) path(x - 1,y + 1,z - 1) = (to)38;
- }
- if (x + 1<width() && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y + 1,z - 1) + P)),
- x + 1,y + 1,z - 1)) {
- res(x + 1,y + 1,z - 1) = npot; if (path) path(x + 1,y + 1,z - 1) = (to)37;
- }
- }
- if (z + 1<depth()) { // Diagonal neighbors on slice z + 1
- if (x - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x - 1,y,z + 1) + P)),x - 1,y,z + 1)) {
- res(x - 1,y,z + 1) = npot; if (path) path(x - 1,y,z + 1) = (to)18;
- }
- if (x + 1<width() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x + 1,y,z + 1) + P)),x + 1,y,z + 1)) {
- res(x + 1,y,z + 1) = npot; if (path) path(x + 1,y,z + 1) = (to)17;
- }
- if (y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y - 1,z + 1) + P)),x,y - 1,z + 1)) {
- res(x,y - 1,z + 1) = npot; if (path) path(x,y - 1,z + 1) = (to)24;
- }
- if (y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt2*met(x,y + 1,z + 1) + P)),x,y + 1,z + 1)) {
- res(x,y + 1,z + 1) = npot; if (path) path(x,y + 1,z + 1) = (to)20;
- }
- if (x - 1>=0 && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y - 1,z + 1) + P)),
- x - 1,y - 1,z + 1)) {
- res(x - 1,y - 1,z + 1) = npot; if (path) path(x - 1,y - 1,z + 1) = (to)26;
- }
- if (x + 1<width() && y - 1>=0 &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y - 1,z + 1) + P)),
- x + 1,y - 1,z + 1)) {
- res(x + 1,y - 1,z + 1) = npot; if (path) path(x + 1,y - 1,z + 1) = (to)25;
- }
- if (x - 1>=0 && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x - 1,y + 1,z + 1) + P)),
- x - 1,y + 1,z + 1)) {
- res(x - 1,y + 1,z + 1) = npot; if (path) path(x - 1,y + 1,z + 1) = (to)22;
- }
- if (x + 1<width() && y + 1<height() &&
- Q._priority_queue_insert(is_queued,sizeQ,-(npot=(td)(sqrt3*met(x + 1,y + 1,z + 1) + P)),
- x + 1,y + 1,z + 1)) {
- res(x + 1,y + 1,z + 1) = npot; if (path) path(x + 1,y + 1,z + 1) = (to)21;
- }
- }
- }
- }
- }
- return result;
- }
- //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \overloading.
- template<typename t>
- CImg<T>& distance_dijkstra(const T& value, const CImg<t>& metric,
- const bool is_high_connectivity=false) {
- return get_distance_dijkstra(value,metric,is_high_connectivity).move_to(*this);
- }
- //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm). \newinstance.
- template<typename t>
- CImg<Tfloat> get_distance_dijkstra(const T& value, const CImg<t>& metric,
- const bool is_high_connectivity=false) const {
- CImg<T> return_path;
- return get_distance_dijkstra(value,metric,is_high_connectivity,return_path);
- }
- //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).
- /**
- \param value Reference value.
- \param metric Field of distance potentials.
- **/
- template<typename t>
- CImg<T>& distance_eikonal(const T& value, const CImg<t>& metric) {
- return get_distance_eikonal(value,metric).move_to(*this);
- }
- //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).
- template<typename t>
- CImg<Tfloat> get_distance_eikonal(const T& value, const CImg<t>& metric) const {
- if (is_empty()) return *this;
- if (!is_sameXYZ(metric))
- throw CImgArgumentException(_cimg_instance
- "distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have "
- "incompatible dimensions.",
- cimg_instance,
- metric._width,metric._height,metric._depth,metric._spectrum);
- CImg<Tfloat> result(_width,_height,_depth,_spectrum,cimg::type<Tfloat>::max()), Q;
- CImg<charT> state(_width,_height,_depth); // -1=far away, 0=narrow, 1=frozen
- cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2) firstprivate(Q,state))
- cimg_forC(*this,c) {
- const CImg<T> img = get_shared_channel(c);
- const CImg<t> met = metric.get_shared_channel(c%metric._spectrum);
- CImg<Tfloat> res = result.get_shared_channel(c);
- unsigned int sizeQ = 0;
- state.fill(-1);
- // Detect initial seeds.
- Tfloat *ptr1 = res._data; char *ptr2 = state._data;
- cimg_for(img,ptr0,T) { if (*ptr0==value) { *ptr1 = 0; *ptr2 = 1; } ++ptr1; ++ptr2; }
- // Initialize seeds neighbors.
- ptr2 = state._data;
- cimg_forXYZ(img,x,y,z) if (*(ptr2++)==1) {
- if (x - 1>=0 && state(x - 1,y,z)==-1) {
- const Tfloat dist = res(x - 1,y,z) = __distance_eikonal(res,met(x - 1,y,z),x - 1,y,z);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x - 1,y,z);
- }
- if (x + 1<width() && state(x + 1,y,z)==-1) {
- const Tfloat dist = res(x + 1,y,z) = __distance_eikonal(res,met(x + 1,y,z),x + 1,y,z);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x + 1,y,z);
- }
- if (y - 1>=0 && state(x,y - 1,z)==-1) {
- const Tfloat dist = res(x,y - 1,z) = __distance_eikonal(res,met(x,y - 1,z),x,y - 1,z);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y - 1,z);
- }
- if (y + 1<height() && state(x,y + 1,z)==-1) {
- const Tfloat dist = res(x,y + 1,z) = __distance_eikonal(res,met(x,y + 1,z),x,y + 1,z);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y + 1,z);
- }
- if (z - 1>=0 && state(x,y,z - 1)==-1) {
- const Tfloat dist = res(x,y,z - 1) = __distance_eikonal(res,met(x,y,z - 1),x,y,z - 1);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z - 1);
- }
- if (z + 1<depth() && state(x,y,z + 1)==-1) {
- const Tfloat dist = res(x,y,z + 1) = __distance_eikonal(res,met(x,y,z + 1),x,y,z + 1);
- Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z + 1);
- }
- }
- // Propagate front.
- while (sizeQ) {
- int x = -1, y = -1, z = -1;
- while (sizeQ && x<0) {
- x = (int)Q(0,1); y = (int)Q(0,2); z = (int)Q(0,3);
- Q._priority_queue_remove(sizeQ);
- if (state(x,y,z)==1) x = -1; else state(x,y,z) = 1;
- }
- if (x>=0) {
- if (x - 1>=0 && state(x - 1,y,z)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x - 1,y,z),x - 1,y,z);
- if (dist<res(x - 1,y,z)) {
- res(x - 1,y,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x - 1,y,z);
- }
- }
- if (x + 1<width() && state(x + 1,y,z)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x + 1,y,z),x + 1,y,z);
- if (dist<res(x + 1,y,z)) {
- res(x + 1,y,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x + 1,y,z);
- }
- }
- if (y - 1>=0 && state(x,y - 1,z)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x,y - 1,z),x,y - 1,z);
- if (dist<res(x,y - 1,z)) {
- res(x,y - 1,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y - 1,z);
- }
- }
- if (y + 1<height() && state(x,y + 1,z)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x,y + 1,z),x,y + 1,z);
- if (dist<res(x,y + 1,z)) {
- res(x,y + 1,z) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y + 1,z);
- }
- }
- if (z - 1>=0 && state(x,y,z - 1)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x,y,z - 1),x,y,z - 1);
- if (dist<res(x,y,z - 1)) {
- res(x,y,z - 1) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z - 1);
- }
- }
- if (z + 1<depth() && state(x,y,z + 1)!=1) {
- const Tfloat dist = __distance_eikonal(res,met(x,y,z + 1),x,y,z + 1);
- if (dist<res(x,y,z + 1)) {
- res(x,y,z + 1) = dist; Q._eik_priority_queue_insert(state,sizeQ,-dist,x,y,z + 1);
- }
- }
- }
- }
- }
- return result;
- }
- // Locally solve eikonal equation.
- Tfloat __distance_eikonal(const CImg<Tfloat>& res, const Tfloat P,
- const int x=0, const int y=0, const int z=0) const {
- const Tfloat M = (Tfloat)cimg::type<T>::max();
- T T1 = (T)std::min(x - 1>=0?res(x - 1,y,z):M,x + 1<width()?res(x + 1,y,z):M);
- Tfloat root = 0;
- if (_depth>1) { // 3D
- T
- T2 = (T)std::min(y - 1>=0?res(x,y - 1,z):M,y + 1<height()?res(x,y + 1,z):M),
- T3 = (T)std::min(z - 1>=0?res(x,y,z - 1):M,z + 1<depth()?res(x,y,z + 1):M);
- if (T1>T2) cimg::swap(T1,T2);
- if (T2>T3) cimg::swap(T2,T3);
- if (T1>T2) cimg::swap(T1,T2);
- if (P<=0) return (Tfloat)T1;
- if (T3<M && ___distance_eikonal(3,-2*(T1 + T2 + T3),T1*T1 + T2*T2 + T3*T3 - P*P,root))
- return std::max((Tfloat)T3,root);
- if (T2<M && ___distance_eikonal(2,-2*(T1 + T2),T1*T1 + T2*T2 - P*P,root))
- return std::max((Tfloat)T2,root);
- return P + T1;
- } else if (_height>1) { // 2D
- T T2 = (T)std::min(y - 1>=0?res(x,y - 1,z):M,y + 1<height()?res(x,y + 1,z):M);
- if (T1>T2) cimg::swap(T1,T2);
- if (P<=0) return (Tfloat)T1;
- if (T2<M && ___distance_eikonal(2,-2*(T1 + T2),T1*T1 + T2*T2 - P*P,root))
- return std::max((Tfloat)T2,root);
- return P + T1;
- } else { // 1D
- if (P<=0) return (Tfloat)T1;
- return P + T1;
- }
- return 0;
- }
- // Find max root of a 2nd-order polynomial.
- static bool ___distance_eikonal(const Tfloat a, const Tfloat b, const Tfloat c, Tfloat &root) {
- const Tfloat delta = b*b - 4*a*c;
- if (delta<0) return false;
- root = 0.5f*(-b + std::sqrt(delta))/a;
- return true;
- }
- // Insert new point in heap.
- template<typename t>
- void _eik_priority_queue_insert(CImg<charT>& state, unsigned int& siz, const t value,
- const unsigned int x, const unsigned int y, const unsigned int z) {
- if (state(x,y,z)>0) return;
- state(x,y,z) = 0;
- if (++siz>=_width) { if (!is_empty()) resize(_width*2,4,1,1,0); else assign(64,4); }
- (*this)(siz - 1,0) = (T)value; (*this)(siz - 1,1) = (T)x; (*this)(siz - 1,2) = (T)y; (*this)(siz - 1,3) = (T)z;
- for (unsigned int pos = siz - 1, par = 0; pos && value>(t)(*this)(par=(pos + 1)/2 - 1,0); pos = par) {
- cimg::swap((*this)(pos,0),(*this)(par,0)); cimg::swap((*this)(pos,1),(*this)(par,1));
- cimg::swap((*this)(pos,2),(*this)(par,2)); cimg::swap((*this)(pos,3),(*this)(par,3));
- }
- }
- //! Compute distance function to 0-valued isophotes, using the Eikonal PDE.
- /**
- \param nb_iterations Number of PDE iterations.
- \param band_size Size of the narrow band.
- \param time_step Time step of the PDE iterations.
- **/
- CImg<T>& distance_eikonal(const unsigned int nb_iterations, const float band_size=0, const float time_step=0.5f) {
- if (is_empty()) return *this;
- CImg<Tfloat> velocity(*this,false);
- for (unsigned int iteration = 0; iteration<nb_iterations; ++iteration) {
- Tfloat *ptrd = velocity._data, veloc_max = 0;
- if (_depth>1) { // 3D
- CImg_3x3x3(I,Tfloat);
- cimg_forC(*this,c) cimg_for3x3x3(*this,x,y,z,c,I,Tfloat) if (band_size<=0 || cimg::abs(Iccc)<band_size) {
- const Tfloat
- gx = (Incc - Ipcc)/2,
- gy = (Icnc - Icpc)/2,
- gz = (Iccn - Iccp)/2,
- sgn = -cimg::sign(Iccc),
- ix = gx*sgn>0?(Incc - Iccc):(Iccc - Ipcc),
- iy = gy*sgn>0?(Icnc - Iccc):(Iccc - Icpc),
- iz = gz*sgn>0?(Iccn - Iccc):(Iccc - Iccp),
- ng = 1e-5f + cimg::hypot(gx,gy,gz),
- ngx = gx/ng,
- ngy = gy/ng,
- ngz = gz/ng,
- veloc = sgn*(ngx*ix + ngy*iy + ngz*iz - 1);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- } else *(ptrd++) = 0;
- } else { // 2D version
- CImg_3x3(I,Tfloat);
- cimg_forC(*this,c) cimg_for3x3(*this,x,y,0,c,I,Tfloat) if (band_size<=0 || cimg::abs(Icc)<band_size) {
- const Tfloat
- gx = (Inc - Ipc)/2,
- gy = (Icn - Icp)/2,
- sgn = -cimg::sign(Icc),
- ix = gx*sgn>0?(Inc - Icc):(Icc - Ipc),
- iy = gy*sgn>0?(Icn - Icc):(Icc - Icp),
- ng = std::max((Tfloat)1e-5,cimg::hypot(gx,gy)),
- ngx = gx/ng,
- ngy = gy/ng,
- veloc = sgn*(ngx*ix + ngy*iy - 1);
- *(ptrd++) = veloc;
- if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc;
- } else *(ptrd++) = 0;
- }
- if (veloc_max>0) *this+=(velocity*=time_step/veloc_max);
- }
- return *this;
- }
- //! Compute distance function to 0-valued isophotes, using the Eikonal PDE \newinstance.
- CImg<Tfloat> get_distance_eikonal(const unsigned int nb_iterations, const float band_size=0,
- const float time_step=0.5f) const {
- return CImg<Tfloat>(*this,false).distance_eikonal(nb_iterations,band_size,time_step);
- }
- //! Compute Haar multiscale wavelet transform.
- /**
- \param axis Axis considered for the transform.
- \param invert Set inverse of direct transform.
- \param nb_scales Number of scales used for the transform.
- **/
- CImg<T>& haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) {
- return get_haar(axis,invert,nb_scales).move_to(*this);
- }
- //! Compute Haar multiscale wavelet transform \newinstance.
- CImg<Tfloat> get_haar(const char axis, const bool invert=false, const unsigned int nb_scales=1) const {
- if (is_empty() || !nb_scales) return +*this;
- CImg<Tfloat> res;
- const Tfloat sqrt2 = std::sqrt(2.f);
- if (nb_scales==1) {
- switch (cimg::lowercase(axis)) { // Single scale transform
- case 'x' : {
- const unsigned int w = _width/2;
- if (w) {
- if ((w%2) && w!=1)
- throw CImgInstanceException(_cimg_instance
- "haar(): Sub-image width %u is not even.",
- cimg_instance,
- w);
- res.assign(_width,_height,_depth,_spectrum);
- if (invert) cimg_forYZC(*this,y,z,c) { // Inverse transform along X
- for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {
- const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(xw,y,z,c);
- res(x2++,y,z,c) = (val0 - val1)/sqrt2;
- res(x2++,y,z,c) = (val0 + val1)/sqrt2;
- }
- } else cimg_forYZC(*this,y,z,c) { // Direct transform along X
- for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {
- const Tfloat val0 = (Tfloat)(*this)(x2++,y,z,c), val1 = (Tfloat)(*this)(x2++,y,z,c);
- res(x,y,z,c) = (val0 + val1)/sqrt2;
- res(xw,y,z,c) = (val1 - val0)/sqrt2;
- }
- }
- } else return *this;
- } break;
- case 'y' : {
- const unsigned int h = _height/2;
- if (h) {
- if ((h%2) && h!=1)
- throw CImgInstanceException(_cimg_instance
- "haar(): Sub-image height %u is not even.",
- cimg_instance,
- h);
- res.assign(_width,_height,_depth,_spectrum);
- if (invert) cimg_forXZC(*this,x,z,c) { // Inverse transform along Y
- for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) {
- const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(x,yh,z,c);
- res(x,y2++,z,c) = (val0 - val1)/sqrt2;
- res(x,y2++,z,c) = (val0 + val1)/sqrt2;
- }
- } else cimg_forXZC(*this,x,z,c) {
- for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) { // Direct transform along Y
- const Tfloat val0 = (Tfloat)(*this)(x,y2++,z,c), val1 = (Tfloat)(*this)(x,y2++,z,c);
- res(x,y,z,c) = (val0 + val1)/sqrt2;
- res(x,yh,z,c) = (val1 - val0)/sqrt2;
- }
- }
- } else return *this;
- } break;
- case 'z' : {
- const unsigned int d = _depth/2;
- if (d) {
- if ((d%2) && d!=1)
- throw CImgInstanceException(_cimg_instance
- "haar(): Sub-image depth %u is not even.",
- cimg_instance,
- d);
- res.assign(_width,_height,_depth,_spectrum);
- if (invert) cimg_forXYC(*this,x,y,c) { // Inverse transform along Z
- for (unsigned int z = 0, zd = d, z2 = 0; z<d; ++z, ++zd) {
- const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(x,y,zd,c);
- res(x,y,z2++,c) = (val0 - val1)/sqrt2;
- res(x,y,z2++,c) = (val0 + val1)/sqrt2;
- }
- } else cimg_forXYC(*this,x,y,c) {
- for (unsigned int z = 0, zd = d, z2 = 0; z<d; ++z, ++zd) { // Direct transform along Z
- const Tfloat val0 = (Tfloat)(*this)(x,y,z2++,c), val1 = (Tfloat)(*this)(x,y,z2++,c);
- res(x,y,z,c) = (val0 + val1)/sqrt2;
- res(x,y,zd,c) = (val1 - val0)/sqrt2;
- }
- }
- } else return *this;
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "haar(): Invalid specified axis '%c' "
- "(should be { x | y | z }).",
- cimg_instance,
- axis);
- }
- } else { // Multi-scale version
- if (invert) {
- res.assign(*this,false);
- switch (cimg::lowercase(axis)) {
- case 'x' : {
- unsigned int w = _width;
- for (unsigned int s = 1; w && s<nb_scales; ++s) w/=2;
- for (w = w?w:1; w<=_width; w*=2) res.draw_image(res.get_crop(0,w - 1).get_haar('x',true,1));
- } break;
- case 'y' : {
- unsigned int h = _width;
- for (unsigned int s = 1; h && s<nb_scales; ++s) h/=2;
- 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));
- } break;
- case 'z' : {
- unsigned int d = _depth;
- for (unsigned int s = 1; d && s<nb_scales; ++s) d/=2;
- for (d = d?d:1; d<=_depth; d*=2)
- res.draw_image(res.get_crop(0,0,0,_width - 1,_height - 1,d - 1).get_haar('z',true,1));
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "haar(): Invalid specified axis '%c' "
- "(should be { x | y | z }).",
- cimg_instance,
- axis);
- }
- } else { // Direct transform
- res = get_haar(axis,false,1);
- switch (cimg::lowercase(axis)) {
- case 'x' : {
- for (unsigned int s = 1, w = _width/2; w && s<nb_scales; ++s, w/=2)
- res.draw_image(res.get_crop(0,w - 1).get_haar('x',false,1));
- } break;
- case 'y' : {
- for (unsigned int s = 1, h = _height/2; h && s<nb_scales; ++s, h/=2)
- res.draw_image(res.get_crop(0,0,_width - 1,h - 1).get_haar('y',false,1));
- } break;
- case 'z' : {
- for (unsigned int s = 1, d = _depth/2; d && s<nb_scales; ++s, d/=2)
- res.draw_image(res.get_crop(0,0,0,_width - 1,_height - 1,d - 1).get_haar('z',false,1));
- } break;
- default :
- throw CImgArgumentException(_cimg_instance
- "haar(): Invalid specified axis '%c' "
- "(should be { x | y | z }).",
- cimg_instance,
- axis);
- }
- }
- }
- return res;
- }
- //! Compute Haar multiscale wavelet transform \overloading.
- /**
- \param invert Set inverse of direct transform.
- \param nb_scales Number of scales used for the transform.
- **/
- CImg<T>& haar(const bool invert=false, const unsigned int nb_scales=1) {
- return get_haar(invert,nb_scales).move_to(*this);
- }
- //! Compute Haar multiscale wavelet transform \newinstance.
- CImg<Tfloat> get_haar(const bool invert=false, const unsigned int nb_scales=1) const {
- CImg<Tfloat> res;
- if (nb_scales==1) { // Single scale transform
- if (_width>1) get_haar('x',invert,1).move_to(res);
- if (_height>1) { if (res) res.haar('y',invert,1); else get_haar('y',invert,1).move_to(res); }
- if (_depth>1) { if (res) res.haar('z',invert,1); else get_haar('z',invert,1).move_to(res); }
- if (res) return res;
- } else { // Multi-scale transform
- if (invert) { // Inverse transform
- res.assign(*this,false);
- if (_width>1) {
- if (_height>1) {
- if (_depth>1) {
- unsigned int w = _width, h = _height, d = _depth;
- for (unsigned int s = 1; w && h && d && s<nb_scales; ++s) { w/=2; h/=2; d/=2; }
- 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)
- res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,d - 1).get_haar(true,1));
- } else {
- unsigned int w = _width, h = _height;
- for (unsigned int s = 1; w && h && s<nb_scales; ++s) { w/=2; h/=2; }
- for (w = w?w:1, h = h?h:1; w<=_width && h<=_height; w*=2, h*=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,0).get_haar(true,1));
- }
- } else {
- if (_depth>1) {
- unsigned int w = _width, d = _depth;
- for (unsigned int s = 1; w && d && s<nb_scales; ++s) { w/=2; d/=2; }
- for (w = w?w:1, d = d?d:1; w<=_width && d<=_depth; w*=2, d*=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,0,d - 1).get_haar(true,1));
- } else {
- unsigned int w = _width;
- for (unsigned int s = 1; w && s<nb_scales; ++s) w/=2;
- for (w = w?w:1; w<=_width; w*=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,0,0).get_haar(true,1));
- }
- }
- } else {
- if (_height>1) {
- if (_depth>1) {
- unsigned int h = _height, d = _depth;
- for (unsigned int s = 1; h && d && s<nb_scales; ++s) { h/=2; d/=2; }
- for (h = h?h:1, d = d?d:1; h<=_height && d<=_depth; h*=2, d*=2)
- res.draw_image(res.get_crop(0,0,0,0,h - 1,d - 1).get_haar(true,1));
- } else {
- unsigned int h = _height;
- for (unsigned int s = 1; h && s<nb_scales; ++s) h/=2;
- for (h = h?h:1; h<=_height; h*=2)
- res.draw_image(res.get_crop(0,0,0,0,h - 1,0).get_haar(true,1));
- }
- } else {
- if (_depth>1) {
- unsigned int d = _depth;
- for (unsigned int s = 1; d && s<nb_scales; ++s) d/=2;
- for (d = d?d:1; d<=_depth; d*=2)
- res.draw_image(res.get_crop(0,0,0,0,0,d - 1).get_haar(true,1));
- } else return *this;
- }
- }
- } else { // Direct transform
- res = get_haar(false,1);
- if (_width>1) {
- if (_height>1) {
- if (_depth>1)
- for (unsigned int s = 1, w = _width/2, h = _height/2, d = _depth/2; w && h && d && s<nb_scales;
- ++s, w/=2, h/=2, d/=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,d - 1).haar(false,1));
- else for (unsigned int s = 1, w = _width/2, h = _height/2; w && h && s<nb_scales; ++s, w/=2, h/=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,h - 1,0).haar(false,1));
- } else {
- if (_depth>1) for (unsigned int s = 1, w = _width/2, d = _depth/2; w && d && s<nb_scales; ++s, w/=2, d/=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,0,d - 1).haar(false,1));
- else for (unsigned int s = 1, w = _width/2; w && s<nb_scales; ++s, w/=2)
- res.draw_image(res.get_crop(0,0,0,w - 1,0,0).haar(false,1));
- }
- } else {
- if (_height>1) {
- if (_depth>1)
- for (unsigned int s = 1, h = _height/2, d = _depth/2; h && d && s<nb_scales; ++s, h/=2, d/=2)
- res.draw_image(res.get_crop(0,0,0,0,h - 1,d - 1).haar(false,1));
- else for (unsigned int s = 1, h = _height/2; h && s<nb_scales; ++s, h/=2)
- res.draw_image(res.get_crop(0,0,0,0,h - 1,0).haar(false,1));
- } else {
- if (_depth>1) for (unsigned int s = 1, d = _depth/2; d && s<nb_scales; ++s, d/=2)
- res.draw_image(res.get_crop(0,0,0,0,0,d - 1).haar(false,1));
- else return *this;
- }
- }
- }
- return res;
- }
- return *this;
- }
- //! Compute 1D Fast Fourier Transform, along a specified axis.
- /**
- \param axis Axis along which the FFT is computed.
- \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
- **/
- CImgList<Tfloat> get_FFT(const char axis, const bool is_inverse=false) const {
- CImgList<Tfloat> res(*this,CImg<Tfloat>());
- CImg<Tfloat>::FFT(res[0],res[1],axis,is_inverse);
- return res;
- }
- //! Compute n-D Fast Fourier Transform.
- /*
- \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
- **/
- CImgList<Tfloat> get_FFT(const bool is_inverse=false) const {
- CImgList<Tfloat> res(*this,CImg<Tfloat>());
- CImg<Tfloat>::FFT(res[0],res[1],is_inverse);
- return res;
- }
- //! Compute 1D Fast Fourier Transform, along a specified axis.
- /**
- \param[in,out] real Real part of the pixel values.
- \param[in,out] imag Imaginary part of the pixel values.
- \param axis Axis along which the FFT is computed.
- \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
- **/
- static void FFT(CImg<T>& real, CImg<T>& imag, const char axis, const bool is_inverse=false,
- const unsigned int nb_threads=0) {
- if (!real)
- throw CImgInstanceException("CImg<%s>::FFT(): Specified real part is empty.",
- pixel_type());
- if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,(T)0);
- if (!real.is_sameXYZC(imag))
- throw CImgInstanceException("CImg<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and "
- "imaginary part (%u,%u,%u,%u,%p) have different dimensions.",
- pixel_type(),
- real._width,real._height,real._depth,real._spectrum,real._data,
- imag._width,imag._height,imag._depth,imag._spectrum,imag._data);
- const char _axis = cimg::lowercase(axis);
- if (_axis!='x' && _axis!='y' && _axis!='z')
- throw CImgArgumentException("CImgList<%s>::FFT(): Invalid specified axis '%c' for real and imaginary parts "
- "(%u,%u,%u,%u) "
- "(should be { x | y | z }).",
- pixel_type(),axis,
- real._width,real._height,real._depth,real._spectrum);
- cimg::unused(nb_threads);
- #ifdef cimg_use_fftw3
- cimg::mutex(12);
- #ifndef cimg_use_fftw3_singlethread
- fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());
- #endif
- fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);
- if (!data_in)
- throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) "
- "for computing FFT of image (%u,%u,%u,%u) along the X-axis.",
- pixel_type(),
- cimg::strbuffersize(sizeof(fftw_complex)*real._width*real._height*real._depth),
- real._width,real._height,real._depth,real._spectrum);
- double *const ptrf = (double*)data_in;
- fftw_plan data_plan =
- _axis=='x'?fftw_plan_many_dft(1,(int*)&real._width,real.height()*real.depth(),
- data_in,0,1,real.width(),
- data_in,0,1,real.width(),
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
- _axis=='y'?fftw_plan_many_dft(1,(int*)&real._height,real.width()*real.depth(),
- data_in,0,1,real.height(),
- data_in,0,1,real.height(),
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
- fftw_plan_many_dft(1,(int*)&real._depth,real.width()*real.height(),
- data_in,0,1,real.depth(),
- data_in,0,1,real.depth(),
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);
- cimg_forC(real,c) {
- CImg<T> realc = real.get_shared_channel(c), imagc = imag.get_shared_channel(c);
- switch (_axis) {
- case 'x' :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = realc.offset(x,y,z),
- j = 2*(x + (ulongT)y*realc._width + (ulongT)z*realc._width*realc._height);
- ptrf[j] = (double)realc[i];
- ptrf[j + 1] = (double)imagc[i];
- }
- break;
- case 'y' :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = realc.offset(x,y,z),
- j = 2*(y + (ulongT)x*realc._height + (ulongT)z*realc._width*realc._height);
- ptrf[j] = (double)realc[i];
- ptrf[j + 1] = (double)imagc[i];
- }
- break;
- default :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = realc.offset(x,y,z),
- j = 2*(z + (ulongT)x*realc._depth + (ulongT)y*realc._width*realc._depth);
- ptrf[j] = (double)realc[i];
- ptrf[j + 1] = (double)imagc[i];
- }
- }
- fftw_execute(data_plan);
- const double a = is_inverse?1.0/(_axis=='x'?real.width():_axis=='y'?real.height():real.depth()):1.0;
- switch (_axis) {
- case 'x' :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = 2*(x + (ulongT)y*realc._width + (ulongT)z*realc._width*realc._height),
- j = realc.offset(x,y,z);
- realc[j] = (T)(a*ptrf[i]);
- imagc[j] = (T)(a*ptrf[i + 1]);
- }
- break;
- case 'y' :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = 2*(y + (ulongT)x*realc._height + (ulongT)z*realc._width*realc._height),
- j = realc.offset(x,y,z);
- realc[j] = (T)(a*ptrf[i]);
- imagc[j] = (T)(a*ptrf[i + 1]);
- }
- break;
- default :
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_forXYZ(realc,x,y,z) {
- const ulongT
- i = 2*(z + (ulongT)x*realc._depth + (ulongT)y*realc._width*realc._depth),
- j = realc.offset(x,y,z);
- realc[j] = (T)(a*ptrf[i]);
- imagc[j] = (T)(a*ptrf[i + 1]);
- }
- }
- }
- fftw_destroy_plan(data_plan);
- fftw_free(data_in);
- #ifndef cimg_use_fftw3_singlethread
- fftw_cleanup_threads();
- #endif
- cimg::mutex(12,0);
- #else
- switch (_axis) {
- case 'x' : { // Fourier along X, using built-in functions
- const unsigned int N = real._width, N2 = N>>1;
- if (((N - 1)&N) && N!=1)
- throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
- "have non 2^N dimension along the X-axis.",
- pixel_type(),
- real._width,real._height,real._depth,real._spectrum);
- for (unsigned int i = 0, j = 0; i<N2; ++i) {
- if (j>i) cimg_forYZC(real,y,z,c) {
- cimg::swap(real(i,y,z,c),real(j,y,z,c));
- cimg::swap(imag(i,y,z,c),imag(j,y,z,c));
- if (j<N2) {
- const unsigned int ri = N - 1 - i, rj = N - 1 - j;
- cimg::swap(real(ri,y,z,c),real(rj,y,z,c));
- cimg::swap(imag(ri,y,z,c),imag(rj,y,z,c));
- }
- }
- for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
- }
- for (unsigned int delta = 2; delta<=N; delta<<=1) {
- const unsigned int delta2 = delta>>1;
- for (unsigned int i = 0; i<N; i+=delta) {
- float wr = 1, wi = 0;
- const float
- angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
- ca = (float)std::cos(angle),
- sa = (float)std::sin(angle);
- for (unsigned int k = 0; k<delta2; ++k) {
- const unsigned int j = i + k, nj = j + delta2;
- cimg_forYZC(real,y,z,c) {
- 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);
- const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
- nir = (T)(ir - tmpr);
- nii = (T)(ii - tmpi);
- ir+=(T)tmpr;
- ii+=(T)tmpi;
- }
- const float nwr = wr*ca-wi*sa;
- wi = wi*ca + wr*sa;
- wr = nwr;
- }
- }
- }
- if (is_inverse) { real/=N; imag/=N; }
- } break;
- case 'y' : { // Fourier along Y, using built-in functions
- const unsigned int N = real._height, N2 = N>>1;
- if (((N - 1)&N) && N!=1)
- throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
- "have non 2^N dimension along the Y-axis.",
- pixel_type(),
- real._width,real._height,real._depth,real._spectrum);
- for (unsigned int i = 0, j = 0; i<N2; ++i) {
- if (j>i) cimg_forXZC(real,x,z,c) {
- cimg::swap(real(x,i,z,c),real(x,j,z,c));
- cimg::swap(imag(x,i,z,c),imag(x,j,z,c));
- if (j<N2) {
- const unsigned int ri = N - 1 - i, rj = N - 1 - j;
- cimg::swap(real(x,ri,z,c),real(x,rj,z,c));
- cimg::swap(imag(x,ri,z,c),imag(x,rj,z,c));
- }
- }
- for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
- }
- for (unsigned int delta = 2; delta<=N; delta<<=1) {
- const unsigned int delta2 = (delta>>1);
- for (unsigned int i = 0; i<N; i+=delta) {
- float wr = 1, wi = 0;
- const float
- angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
- ca = (float)std::cos(angle),
- sa = (float)std::sin(angle);
- for (unsigned int k = 0; k<delta2; ++k) {
- const unsigned int j = i + k, nj = j + delta2;
- cimg_forXZC(real,x,z,c) {
- 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);
- const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
- nir = (T)(ir - tmpr);
- nii = (T)(ii - tmpi);
- ir+=(T)tmpr;
- ii+=(T)tmpi;
- }
- const float nwr = wr*ca-wi*sa;
- wi = wi*ca + wr*sa;
- wr = nwr;
- }
- }
- }
- if (is_inverse) { real/=N; imag/=N; }
- } break;
- default : { // Fourier along Z, using built-in functions
- const unsigned int N = real._depth, N2 = N>>1;
- if (((N - 1)&N) && N!=1)
- throw CImgInstanceException("CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) "
- "have non 2^N dimension along the Z-axis.",
- pixel_type(),
- real._width,real._height,real._depth,real._spectrum);
- for (unsigned int i = 0, j = 0; i<N2; ++i) {
- if (j>i) cimg_forXYC(real,x,y,c) {
- cimg::swap(real(x,y,i,c),real(x,y,j,c));
- cimg::swap(imag(x,y,i,c),imag(x,y,j,c));
- if (j<N2) {
- const unsigned int ri = N - 1 - i, rj = N - 1 - j;
- cimg::swap(real(x,y,ri,c),real(x,y,rj,c));
- cimg::swap(imag(x,y,ri,c),imag(x,y,rj,c));
- }
- }
- for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}
- }
- for (unsigned int delta = 2; delta<=N; delta<<=1) {
- const unsigned int delta2 = (delta>>1);
- for (unsigned int i = 0; i<N; i+=delta) {
- float wr = 1, wi = 0;
- const float
- angle = (float)((is_inverse?+1:-1)*2*cimg::PI/delta),
- ca = (float)std::cos(angle),
- sa = (float)std::sin(angle);
- for (unsigned int k = 0; k<delta2; ++k) {
- const unsigned int j = i + k, nj = j + delta2;
- cimg_forXYC(real,x,y,c) {
- 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);
- const float tmpr = (float)(wr*nir - wi*nii), tmpi = (float)(wr*nii + wi*nir);
- nir = (T)(ir - tmpr);
- nii = (T)(ii - tmpi);
- ir+=(T)tmpr;
- ii+=(T)tmpi;
- }
- const float nwr = wr*ca-wi*sa;
- wi = wi*ca + wr*sa;
- wr = nwr;
- }
- }
- }
- if (is_inverse) { real/=N; imag/=N; }
- } break;
- }
- #endif
- }
- //! Compute n-D Fast Fourier Transform.
- /**
- \param[in,out] real Real part of the pixel values.
- \param[in,out] imag Imaginary part of the pixel values.
- \param is_inverse Tells if the forward (\c false) or inverse (\c true) FFT is computed.
- \param nb_threads Number of parallel threads used for the computation.
- Use \c 0 to set this to the number of available cpus.
- **/
- static void FFT(CImg<T>& real, CImg<T>& imag, const bool is_inverse=false,
- const unsigned int nb_threads=0) {
- if (!real)
- throw CImgInstanceException("CImgList<%s>::FFT(): Empty specified real part.",
- pixel_type());
- if (!imag) imag.assign(real._width,real._height,real._depth,real._spectrum,(T)0);
- if (!real.is_sameXYZC(imag))
- throw CImgInstanceException("CImgList<%s>::FFT(): Specified real part (%u,%u,%u,%u,%p) and "
- "imaginary part (%u,%u,%u,%u,%p) have different dimensions.",
- pixel_type(),
- real._width,real._height,real._depth,real._spectrum,real._data,
- imag._width,imag._height,imag._depth,imag._spectrum,imag._data);
- cimg::unused(nb_threads);
- #ifdef cimg_use_fftw3
- cimg::mutex(12);
- #ifndef cimg_use_fftw3_singlethread
- fftw_plan_with_nthreads(nb_threads?nb_threads:cimg::nb_cpus());
- #endif
- fftw_complex *data_in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*real._width*real._height*real._depth);
- if (!data_in)
- throw CImgInstanceException("CImgList<%s>::FFT(): Failed to allocate memory (%s) "
- "for computing FFT of image (%u,%u,%u,%u).",
- pixel_type(),
- cimg::strbuffersize(sizeof(fftw_complex)*real._width*
- real._height*real._depth*real._spectrum),
- real._width,real._height,real._depth,real._spectrum);
- double *const ptrf = (double*)data_in;
- fftw_plan data_plan =
- real._depth>1?fftw_plan_dft_3d(real._depth,real._height,real._width,data_in,data_in,
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
- real._height>1?fftw_plan_dft_2d(real._height,real._width,data_in,data_in,
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE):
- fftw_plan_dft_1d(real._width,data_in,data_in,
- is_inverse?FFTW_BACKWARD:FFTW_FORWARD,FFTW_ESTIMATE);
- cimg_forC(real,c) {
- CImg<T> realc = real.get_shared_channel(c), imagc = imag.get_shared_channel(c);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_rofoff(realc,i) { const ulongT i2 = 2*i; ptrf[i2] = (double)realc[i]; ptrf[i2 + 1] = (double)imagc[i]; }
- fftw_execute(data_plan);
- if (is_inverse) {
- const double a = 1.0/(real.width()*real.height()*real.depth());
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_rofoff(realc,i) { const ulongT i2 = 2*i; realc[i] = (T)(a*ptrf[i2]); imagc[i] = (T)(a*ptrf[i2 + 1]); }
- } else
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(real.width()*real.height()*real.depth(),125000))
- cimg_rofoff(realc,i) { const ulongT i2 = 2*i; realc[i] = (T)ptrf[i2]; imagc[i] = (T)ptrf[i2 + 1]; }
- }
- fftw_destroy_plan(data_plan);
- fftw_free(data_in);
- #ifndef cimg_use_fftw3_singlethread
- fftw_cleanup_threads();
- #endif
- cimg::mutex(12,0);
- #else
- if (real._depth>1) FFT(real,imag,'z',is_inverse);
- if (real._height>1) FFT(real,imag,'y',is_inverse);
- if (real._width>1) FFT(real,imag,'x',is_inverse);
- #endif
- }
- //@}
- //-------------------------------------
- //
- //! \name 3D Objects Management
- //@{
- //-------------------------------------
- //! Rotate 3D object's vertices.
- /**
- \param x X-coordinate of the rotation axis, or first quaternion coordinate.
- \param y Y-coordinate of the rotation axis, or second quaternion coordinate.
- \param z Z-coordinate of the rotation axis, or second quaternion coordinate.
- \param w Angle of the rotation axis (in degree), or fourth quaternion coordinate.
- \param is_quaternion Tell is the four arguments denotes a set { axis + angle } or a quaternion (x,y,z,w).
- **/
- CImg<T>& rotate_object3d(const float x, const float y, const float z, const float w,
- const bool is_quaternion=false) {
- return get_rotate_object3d(x,y,z,w,is_quaternion).move_to(*this);
- }
- CImg<Tfloat> get_rotate_object3d(const float x, const float y, const float z, const float w,
- const bool is_quaternion=false) const {
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "rotate_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- return CImg<Tfloat>::rotation_matrix(x,y,z,w,is_quaternion)**this;
- }
- //! Shift 3D object's vertices.
- /**
- \param tx X-coordinate of the 3D displacement vector.
- \param ty Y-coordinate of the 3D displacement vector.
- \param tz Z-coordinate of the 3D displacement vector.
- **/
- CImg<T>& shift_object3d(const float tx, const float ty=0, const float tz=0) {
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "shift_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- get_shared_row(0)+=tx; get_shared_row(1)+=ty; get_shared_row(2)+=tz;
- return *this;
- }
- //! Shift 3D object's vertices \newinstance.
- CImg<Tfloat> get_shift_object3d(const float tx, const float ty=0, const float tz=0) const {
- return CImg<Tfloat>(*this,false).shift_object3d(tx,ty,tz);
- }
- //! Shift 3D object's vertices, so that it becomes centered.
- /**
- \note The object center is computed as its barycenter.
- **/
- CImg<T>& shift_object3d() {
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "shift_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
- float
- xm, xM = (float)xcoords.max_min(xm),
- ym, yM = (float)ycoords.max_min(ym),
- zm, zM = (float)zcoords.max_min(zm);
- xcoords-=(xm + xM)/2; ycoords-=(ym + yM)/2; zcoords-=(zm + zM)/2;
- return *this;
- }
- //! Shift 3D object's vertices, so that it becomes centered \newinstance.
- CImg<Tfloat> get_shift_object3d() const {
- return CImg<Tfloat>(*this,false).shift_object3d();
- }
- //! Resize 3D object.
- /**
- \param sx Width of the 3D object's bounding box.
- \param sy Height of the 3D object's bounding box.
- \param sz Depth of the 3D object's bounding box.
- **/
- CImg<T>& resize_object3d(const float sx, const float sy=-100, const float sz=-100) {
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "resize_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
- float
- xm, xM = (float)xcoords.max_min(xm),
- ym, yM = (float)ycoords.max_min(ym),
- zm, zM = (float)zcoords.max_min(zm);
- if (xm<xM) { if (sx>0) xcoords*=sx/(xM-xm); else xcoords*=-sx/100; }
- if (ym<yM) { if (sy>0) ycoords*=sy/(yM-ym); else ycoords*=-sy/100; }
- if (zm<zM) { if (sz>0) zcoords*=sz/(zM-zm); else zcoords*=-sz/100; }
- return *this;
- }
- //! Resize 3D object \newinstance.
- CImg<Tfloat> get_resize_object3d(const float sx, const float sy=-100, const float sz=-100) const {
- return CImg<Tfloat>(*this,false).resize_object3d(sx,sy,sz);
- }
- //! Resize 3D object to unit size.
- CImg<T> resize_object3d() {
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "resize_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- CImg<T> xcoords = get_shared_row(0), ycoords = get_shared_row(1), zcoords = get_shared_row(2);
- float
- xm, xM = (float)xcoords.max_min(xm),
- ym, yM = (float)ycoords.max_min(ym),
- zm, zM = (float)zcoords.max_min(zm);
- const float dx = xM - xm, dy = yM - ym, dz = zM - zm, dmax = cimg::max(dx,dy,dz);
- if (dmax>0) { xcoords/=dmax; ycoords/=dmax; zcoords/=dmax; }
- return *this;
- }
- //! Resize 3D object to unit size \newinstance.
- CImg<Tfloat> get_resize_object3d() const {
- return CImg<Tfloat>(*this,false).resize_object3d();
- }
- //! Merge two 3D objects together.
- /**
- \param[in,out] primitives Primitives data of the current 3D object.
- \param obj_vertices Vertices data of the additional 3D object.
- \param obj_primitives Primitives data of the additional 3D object.
- **/
- template<typename tf, typename tp, typename tff>
- CImg<T>& append_object3d(CImgList<tf>& primitives, const CImg<tp>& obj_vertices,
- const CImgList<tff>& obj_primitives) {
- if (!obj_vertices || !obj_primitives) return *this;
- if (obj_vertices._height!=3 || obj_vertices._depth>1 || obj_vertices._spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "append_object3d(): Specified vertice image (%u,%u,%u,%u,%p) is not a "
- "set of 3D vertices.",
- cimg_instance,
- obj_vertices._width,obj_vertices._height,
- obj_vertices._depth,obj_vertices._spectrum,obj_vertices._data);
- if (is_empty()) { primitives.assign(obj_primitives); return assign(obj_vertices); }
- if (_height!=3 || _depth>1 || _spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "append_object3d(): Instance is not a set of 3D vertices.",
- cimg_instance);
- const unsigned int P = _width;
- append(obj_vertices,'x');
- const unsigned int N = primitives._width;
- primitives.insert(obj_primitives);
- for (unsigned int i = N; i<primitives._width; ++i) {
- CImg<tf> &p = primitives[i];
- switch (p.size()) {
- case 1 : p[0]+=P; break; // Point
- case 5 : p[0]+=P; p[1]+=P; break; // Sphere
- case 2 : case 6 : p[0]+=P; p[1]+=P; break; // Segment
- case 3 : case 9 : p[0]+=P; p[1]+=P; p[2]+=P; break; // Triangle
- case 4 : case 12 : p[0]+=P; p[1]+=P; p[2]+=P; p[3]+=P; break; // Rectangle
- }
- }
- return *this;
- }
- //! Texturize primitives of a 3D object.
- /**
- \param[in,out] primitives Primitives data of the 3D object.
- \param[in,out] colors Colors data of the 3D object.
- \param texture Texture image to map to 3D object.
- \param coords Texture-mapping coordinates.
- **/
- template<typename tp, typename tc, typename tt, typename tx>
- const CImg<T>& texturize_object3d(CImgList<tp>& primitives, CImgList<tc>& colors,
- const CImg<tt>& texture, const CImg<tx>& coords=CImg<tx>::const_empty()) const {
- if (is_empty()) return *this;
- if (_height!=3)
- throw CImgInstanceException(_cimg_instance
- "texturize_object3d(): image instance is not a set of 3D points.",
- cimg_instance);
- if (coords && (coords._width!=_width || coords._height!=2))
- throw CImgArgumentException(_cimg_instance
- "texturize_object3d(): Invalid specified texture coordinates (%u,%u,%u,%u,%p).",
- cimg_instance,
- coords._width,coords._height,coords._depth,coords._spectrum,coords._data);
- CImg<intT> _coords;
- if (!coords) { // If no texture coordinates specified, do a default XY-projection
- _coords.assign(_width,2);
- float
- xmin, xmax = (float)get_shared_row(0).max_min(xmin),
- ymin, ymax = (float)get_shared_row(1).max_min(ymin),
- dx = xmax>xmin?xmax-xmin:1,
- dy = ymax>ymin?ymax-ymin:1;
- cimg_forX(*this,p) {
- _coords(p,0) = (int)(((*this)(p,0) - xmin)*texture._width/dx);
- _coords(p,1) = (int)(((*this)(p,1) - ymin)*texture._height/dy);
- }
- } else _coords = coords;
- int texture_ind = -1;
- cimglist_for(primitives,l) {
- CImg<tp> &p = primitives[l];
- const unsigned int siz = p.size();
- switch (siz) {
- case 1 : { // Point
- const unsigned int i0 = (unsigned int)p[0];
- const int x0 = _coords(i0,0), y0 = _coords(i0,1);
- texture.get_vector_at(x0<=0?0:x0>=texture.width()?texture.width() - 1:x0,
- y0<=0?0:y0>=texture.height()?texture.height() - 1:y0).move_to(colors[l]);
- } break;
- case 2 : case 6 : { // Line
- const unsigned int i0 = (unsigned int)p[0], i1 = (unsigned int)p[1];
- const int
- x0 = _coords(i0,0), y0 = _coords(i0,1),
- x1 = _coords(i1,0), y1 = _coords(i1,1);
- if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
- else colors[l].assign(colors[texture_ind],true);
- CImg<tp>::vector(i0,i1,x0,y0,x1,y1).move_to(p);
- } break;
- case 3 : case 9 : { // Triangle
- const unsigned int i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2];
- const int
- x0 = _coords(i0,0), y0 = _coords(i0,1),
- x1 = _coords(i1,0), y1 = _coords(i1,1),
- x2 = _coords(i2,0), y2 = _coords(i2,1);
- if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
- else colors[l].assign(colors[texture_ind],true);
- CImg<tp>::vector(i0,i1,i2,x0,y0,x1,y1,x2,y2).move_to(p);
- } break;
- case 4 : case 12 : { // Quadrangle
- const unsigned int
- i0 = (unsigned int)p[0], i1 = (unsigned int)p[1], i2 = (unsigned int)p[2], i3 = (unsigned int)p[3];
- const int
- x0 = _coords(i0,0), y0 = _coords(i0,1),
- x1 = _coords(i1,0), y1 = _coords(i1,1),
- x2 = _coords(i2,0), y2 = _coords(i2,1),
- x3 = _coords(i3,0), y3 = _coords(i3,1);
- if (texture_ind<0) colors[texture_ind=l].assign(texture,false);
- else colors[l].assign(colors[texture_ind],true);
- CImg<tp>::vector(i0,i1,i2,i3,x0,y0,x1,y1,x2,y2,x3,y3).move_to(p);
- } break;
- }
- }
- return *this;
- }
- //! Generate a 3D elevation of the image instance.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param[out] colors The returned list of the 3D object colors.
- \param elevation The input elevation map.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- const CImg<float> img("reference.jpg");
- CImgList<unsigned int> faces3d;
- CImgList<unsigned char> colors3d;
- const CImg<float> points3d = img.get_elevation3d(faces3d,colors3d,img.get_norm()*0.2);
- CImg<unsigned char>().display_object3d("Elevation3d",points3d,faces3d,colors3d);
- \endcode
- \image html ref_elevation3d.jpg
- **/
- template<typename tf, typename tc, typename te>
- CImg<floatT> get_elevation3d(CImgList<tf>& primitives, CImgList<tc>& colors, const CImg<te>& elevation) const {
- if (!is_sameXY(elevation) || elevation._depth>1 || elevation._spectrum>1)
- throw CImgArgumentException(_cimg_instance
- "get_elevation3d(): Instance and specified elevation (%u,%u,%u,%u,%p) "
- "have incompatible dimensions.",
- cimg_instance,
- elevation._width,elevation._height,elevation._depth,
- elevation._spectrum,elevation._data);
- if (is_empty()) return *this;
- float m, M = (float)max_min(m);
- if (M==m) ++M;
- colors.assign();
- const unsigned int size_x1 = _width - 1, size_y1 = _height - 1;
- for (unsigned int y = 0; y<size_y1; ++y)
- for (unsigned int x = 0; x<size_x1; ++x) {
- const unsigned char
- r = (unsigned char)(((*this)(x,y,0) - m)*255/(M-m)),
- g = (unsigned char)(_spectrum>1?((*this)(x,y,1) - m)*255/(M-m):r),
- b = (unsigned char)(_spectrum>2?((*this)(x,y,2) - m)*255/(M-m):_spectrum>1?0:r);
- CImg<tc>::vector((tc)r,(tc)g,(tc)b).move_to(colors);
- }
- const typename CImg<te>::_functor2d_int func(elevation);
- return elevation3d(primitives,func,0,0,_width - 1.f,_height - 1.f,_width,_height);
- }
- //! Generate the 3D projection planes of the image instance.
- /**
- \param[out] primitives Primitives data of the returned 3D object.
- \param[out] colors Colors data of the returned 3D object.
- \param x0 X-coordinate of the projection point.
- \param y0 Y-coordinate of the projection point.
- \param z0 Z-coordinate of the projection point.
- \param normalize_colors Tells if the created textures have normalized colors.
- **/
- template<typename tf, typename tc>
- CImg<floatT> get_projections3d(CImgList<tf>& primitives, CImgList<tc>& colors,
- const unsigned int x0, const unsigned int y0, const unsigned int z0,
- const bool normalize_colors=false) const {
- float m = 0, M = 0, delta = 1;
- if (normalize_colors) { m = (float)min_max(M); delta = 255/(m==M?1:M-m); }
- const unsigned int
- _x0 = (x0>=_width)?_width - 1:x0,
- _y0 = (y0>=_height)?_height - 1:y0,
- _z0 = (z0>=_depth)?_depth - 1:z0;
- CImg<tc> img_xy, img_xz, img_yz;
- if (normalize_colors) {
- ((get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1)-=m)*=delta).move_to(img_xy);
- ((get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1)-=m)*=delta).resize(_width,_depth,1,-100,-1).
- move_to(img_xz);
- ((get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1)-=m)*=delta).resize(_height,_depth,1,-100,-1).
- move_to(img_yz);
- } else {
- get_crop(0,0,_z0,0,_width - 1,_height - 1,_z0,_spectrum - 1).move_to(img_xy);
- get_crop(0,_y0,0,0,_width - 1,_y0,_depth - 1,_spectrum - 1).resize(_width,_depth,1,-100,-1).move_to(img_xz);
- get_crop(_x0,0,0,0,_x0,_height - 1,_depth - 1,_spectrum - 1).resize(_height,_depth,1,-100,-1).move_to(img_yz);
- }
- CImg<floatT> points(12,3,1,1,
- 0,_width - 1,_width - 1,0, 0,_width - 1,_width - 1,0, _x0,_x0,_x0,_x0,
- 0,0,_height - 1,_height - 1, _y0,_y0,_y0,_y0, 0,_height - 1,_height - 1,0,
- _z0,_z0,_z0,_z0, 0,0,_depth - 1,_depth - 1, 0,0,_depth - 1,_depth - 1);
- primitives.assign();
- 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).
- move_to(primitives);
- 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).
- move_to(primitives);
- 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).
- move_to(primitives);
- colors.assign();
- img_xy.move_to(colors);
- img_xz.move_to(colors);
- img_yz.move_to(colors);
- return points;
- }
- //! Generate a isoline of the image instance as a 3D object.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param isovalue The returned list of the 3D object colors.
- \param size_x The number of subdivisions along the X-axis.
- \param size_y The number of subdisivions along the Y-axis.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- const CImg<float> img("reference.jpg");
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = img.get_isoline3d(faces3d,100);
- CImg<unsigned char>().display_object3d("Isoline3d",points3d,faces3d,colors3d);
- \endcode
- \image html ref_isoline3d.jpg
- **/
- template<typename tf>
- CImg<floatT> get_isoline3d(CImgList<tf>& primitives, const float isovalue,
- const int size_x=-100, const int size_y=-100) const {
- if (_spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "get_isoline3d(): Instance is not a scalar image.",
- cimg_instance);
- if (_depth>1)
- throw CImgInstanceException(_cimg_instance
- "get_isoline3d(): Instance is not a 2D image.",
- cimg_instance);
- primitives.assign();
- if (is_empty()) return *this;
- CImg<floatT> vertices;
- if ((size_x==-100 && size_y==-100) || (size_x==width() && size_y==height())) {
- const _functor2d_int func(*this);
- vertices = isoline3d(primitives,func,isovalue,0,0,width() - 1.f,height() - 1.f,width(),height());
- } else {
- const _functor2d_float func(*this);
- vertices = isoline3d(primitives,func,isovalue,0,0,width() - 1.f,height() - 1.f,size_x,size_y);
- }
- return vertices;
- }
- //! Compute isolines of a function, as a 3D object.
- /**
- \param[out] primitives Primitives data of the resulting 3D object.
- \param func Elevation functor. Must have <tt>operator()(x,y)</tt> defined.
- \param isovalue Isovalue to extract from function.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param size_x Resolution of the function along the X-axis.
- \param size_y Resolution of the function along the Y-axis.
- \note Use the marching squares algorithm for extracting the isolines.
- **/
- template<typename tf, typename tfunc>
- static CImg<floatT> isoline3d(CImgList<tf>& primitives, const tfunc& func, const float isovalue,
- const float x0, const float y0, const float x1, const float y1,
- const int size_x=256, const int size_y=256) {
- CImgList<floatT> vertices;
- primitives.assign();
- typename CImg<floatT>::_functor_isoline3d add_vertex(vertices);
- typename CImg<tf>::_functor_isoline3d add_segment(primitives);
- isoline3d(add_vertex,add_segment,func,isovalue,x0,y0,x1,y1,size_x,size_y);
- return vertices>'x';
- }
- //! Compute isolines of a function, as a 3D object.
- /**
- \param[out] add_vertex : Functor with operator()(x,y,z) defined for adding new vertex.
- \param[out] add_segment : Functor with operator()(i,j) defined for adding new segment.
- \param func Elevation function. Is of type <tt>float (*func)(const float x,const float y)</tt>.
- \param isovalue Isovalue to extract from function.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param size_x Resolution of the function along the X-axis.
- \param size_y Resolution of the function along the Y-axis.
- \note Use the marching squares algorithm for extracting the isolines.
- **/
- template<typename tv, typename tf, typename tfunc>
- static void isoline3d(tv& add_vertex, tf& add_segment, const tfunc& func, const float isovalue,
- const float x0, const float y0, const float x1, const float y1,
- const int size_x, const int size_y) {
- static const unsigned int edges[16] = { 0x0, 0x9, 0x3, 0xa, 0x6, 0xf, 0x5, 0xc, 0xc,
- 0x5, 0xf, 0x6, 0xa, 0x3, 0x9, 0x0 };
- static const int segments[16][4] = { { -1,-1,-1,-1 }, { 0,3,-1,-1 }, { 0,1,-1,-1 }, { 1,3,-1,-1 },
- { 1,2,-1,-1 }, { 0,1,2,3 }, { 0,2,-1,-1 }, { 2,3,-1,-1 },
- { 2,3,-1,-1 }, { 0,2,-1,-1}, { 0,3,1,2 }, { 1,2,-1,-1 },
- { 1,3,-1,-1 }, { 0,1,-1,-1}, { 0,3,-1,-1}, { -1,-1,-1,-1 } };
- const unsigned int
- _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)),
- _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)),
- nx = _nx?_nx:1,
- ny = _ny?_ny:1,
- nxm1 = nx - 1,
- nym1 = ny - 1;
- if (!nxm1 || !nym1) return;
- const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1;
- CImg<intT> indices1(nx,1,1,2,-1), indices2(nx,1,1,2);
- CImg<floatT> values1(nx), values2(nx);
- float X = x0, Y = y0, nX = X + dx, nY = Y + dy;
- int nb_vertices = 0;
- // Fill first line with values
- cimg_forX(values1,x) { values1(x) = (float)func(X,Y); X+=dx; }
- // Run the marching squares algorithm
- for (unsigned int yi = 0, nyi = 1; yi<nym1; ++yi, ++nyi, Y=nY, nY+=dy) {
- X = x0; nX = X + dx;
- indices2.fill(-1);
- values2(0) = (float)func(X,nY);
- for (unsigned int xi = 0, nxi = 1; xi<nxm1; ++xi, ++nxi, X=nX, nX+=dx) {
- // Determine square configuration
- const float
- val0 = values1(xi),
- val1 = values1(nxi),
- val2 = values2(nxi) = (float)func(nX,nY),
- val3 = values2(xi);
- const unsigned int
- configuration = (val0<isovalue?1U:0U) | (val1<isovalue?2U:0U) |
- (val2<isovalue?4U:0U) | (val3<isovalue?8U:0U),
- edge = edges[configuration];
- // Compute intersection vertices
- if (edge) {
- if ((edge&1) && indices1(xi,0)<0) {
- const float Xi = X + (isovalue-val0)*dx/(val1-val0);
- indices1(xi,0) = nb_vertices++;
- add_vertex(Xi,Y,0.0f);
- }
- if ((edge&2) && indices1(nxi,1)<0) {
- const float Yi = Y + (isovalue-val1)*dy/(val2-val1);
- indices1(nxi,1) = nb_vertices++;
- add_vertex(nX,Yi,0.0f);
- }
- if ((edge&4) && indices2(xi,0)<0) {
- const float Xi = X + (isovalue-val3)*dx/(val2-val3);
- indices2(xi,0) = nb_vertices++;
- add_vertex(Xi,nY,0.0f);
- }
- if ((edge&8) && indices1(xi,1)<0) {
- const float Yi = Y + (isovalue-val0)*dy/(val3-val0);
- indices1(xi,1) = nb_vertices++;
- add_vertex(X,Yi,0.0f);
- }
- // Create segments
- for (const int *segment = segments[configuration]; *segment!=-1; ) {
- const unsigned int p0 = (unsigned int)*(segment++), p1 = (unsigned int)*(segment++);
- const int
- i0 = _isoline3d_index(p0,indices1,indices2,xi,nxi),
- i1 = _isoline3d_index(p1,indices1,indices2,xi,nxi);
- add_segment(i0,i1);
- }
- }
- }
- values1.swap(values2);
- indices1.swap(indices2);
- }
- }
- //! Compute isolines of a function, as a 3D object \overloading.
- template<typename tf>
- static CImg<floatT> isoline3d(CImgList<tf>& primitives, const char *const expression, const float isovalue,
- const float x0, const float y0, const float x1, const float y1,
- const int size_x=256, const int size_y=256) {
- const _functor2d_expr func(expression);
- return isoline3d(primitives,func,isovalue,x0,y0,x1,y1,size_x,size_y);
- }
- template<typename t>
- static int _isoline3d_index(const unsigned int edge, const CImg<t>& indices1, const CImg<t>& indices2,
- const unsigned int x, const unsigned int nx) {
- switch (edge) {
- case 0 : return (int)indices1(x,0);
- case 1 : return (int)indices1(nx,1);
- case 2 : return (int)indices2(x,0);
- case 3 : return (int)indices1(x,1);
- }
- return 0;
- }
- //! Generate an isosurface of the image instance as a 3D object.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param isovalue The returned list of the 3D object colors.
- \param size_x Number of subdivisions along the X-axis.
- \param size_y Number of subdisivions along the Y-axis.
- \param size_z Number of subdisivions along the Z-axis.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- const CImg<float> img = CImg<unsigned char>("reference.jpg").resize(-100,-100,20);
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = img.get_isosurface3d(faces3d,100);
- CImg<unsigned char>().display_object3d("Isosurface3d",points3d,faces3d,colors3d);
- \endcode
- \image html ref_isosurface3d.jpg
- **/
- template<typename tf>
- CImg<floatT> get_isosurface3d(CImgList<tf>& primitives, const float isovalue,
- const int size_x=-100, const int size_y=-100, const int size_z=-100) const {
- if (_spectrum>1)
- throw CImgInstanceException(_cimg_instance
- "get_isosurface3d(): Instance is not a scalar image.",
- cimg_instance);
- primitives.assign();
- if (is_empty()) return *this;
- CImg<floatT> vertices;
- if ((size_x==-100 && size_y==-100 && size_z==-100) || (size_x==width() && size_y==height() && size_z==depth())) {
- const _functor3d_int func(*this);
- vertices = isosurface3d(primitives,func,isovalue,0,0,0,width() - 1.f,height() - 1.f,depth() - 1.f,
- width(),height(),depth());
- } else {
- const _functor3d_float func(*this);
- vertices = isosurface3d(primitives,func,isovalue,0,0,0,width() - 1.f,height() - 1.f,depth() - 1.f,
- size_x,size_y,size_z);
- }
- return vertices;
- }
- //! Compute isosurface of a function, as a 3D object.
- /**
- \param[out] primitives Primitives data of the resulting 3D object.
- \param func Implicit function. Is of type <tt>float (*func)(const float x, const float y, const float z)</tt>.
- \param isovalue Isovalue to extract.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param z0 Z-coordinate of the starting point.
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param z1 Z-coordinate of the ending point.
- \param size_x Resolution of the elevation function along the X-axis.
- \param size_y Resolution of the elevation function along the Y-axis.
- \param size_z Resolution of the elevation function along the Z-axis.
- \note Use the marching cubes algorithm for extracting the isosurface.
- **/
- template<typename tf, typename tfunc>
- static CImg<floatT> isosurface3d(CImgList<tf>& primitives, const tfunc& func, const float isovalue,
- const float x0, const float y0, const float z0,
- const float x1, const float y1, const float z1,
- const int size_x=32, const int size_y=32, const int size_z=32) {
- CImgList<floatT> vertices;
- primitives.assign();
- typename CImg<floatT>::_functor_isosurface3d add_vertex(vertices);
- typename CImg<tf>::_functor_isosurface3d add_triangle(primitives);
- isosurface3d(add_vertex,add_triangle,func,isovalue,x0,y0,z0,x1,y1,z1,size_x,size_y,size_z);
- return vertices>'x';
- }
- //! Compute isosurface of a function, as a 3D object.
- /**
- \param[out] add_vertex : Functor with operator()(x,y,z) defined for adding new vertex.
- \param[out] add_triangle : Functor with operator()(i,j) defined for adding new segment.
- \param func Implicit function. Is of type <tt>float (*func)(const float x, const float y, const float z)</tt>.
- \param isovalue Isovalue to extract.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param z0 Z-coordinate of the starting point.
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param z1 Z-coordinate of the ending point.
- \param size_x Resolution of the elevation function along the X-axis.
- \param size_y Resolution of the elevation function along the Y-axis.
- \param size_z Resolution of the elevation function along the Z-axis.
- \note Use the marching cubes algorithm for extracting the isosurface.
- **/
- template<typename tv, typename tf, typename tfunc>
- static void isosurface3d(tv& add_vertex, tf& add_triangle, const tfunc& func, const float isovalue,
- const float x0, const float y0, const float z0,
- const float x1, const float y1, const float z1,
- const int size_x, const int size_y, const int size_z) {
- static const unsigned int edges[256] = {
- 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
- 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
- 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
- 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
- 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
- 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
- 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
- 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc , 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
- 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
- 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
- 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
- 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
- 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
- 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
- 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
- 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000
- };
- static const int triangles[256][16] = {
- { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1 },
- { 8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1 },
- { 3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1 },
- { 4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1 },
- { 4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1 },
- { 9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1 },
- { 10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1 },
- { 5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1 },
- { 5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1 },
- { 8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1 },
- { 2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1 },
- { 2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1 },
- { 11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1 },
- { 5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1 },
- { 11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1 },
- { 11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1 },
- { 2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1 },
- { 6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1 },
- { 3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1 },
- { 6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1 },
- { 6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1 },
- { 8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1 },
- { 7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1 },
- { 3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1 },
- { 0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1 },
- { 9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1 },
- { 8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1 },
- { 5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1 },
- { 0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1 },
- { 6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1 },
- { 10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1 },
- { 1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1 },
- { 0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1 },
- { 3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1 },
- { 6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1 },
- { 9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1 },
- { 8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1 },
- { 3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1 },
- { 10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1 },
- { 10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1 },
- { 2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1 },
- { 7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1 },
- { 2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1 },
- { 1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1 },
- { 11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1 },
- { 8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1 },
- { 0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1 },
- { 7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1 },
- { 7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1 },
- { 10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1 },
- { 0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1 },
- { 7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1 },
- { 6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1 },
- { 4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1 },
- { 10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1 },
- { 8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1 },
- { 1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1 },
- { 10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1 },
- { 10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1 },
- { 9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1 },
- { 7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1 },
- { 3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1 },
- { 7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1 },
- { 3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1 },
- { 6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1 },
- { 9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1 },
- { 1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1 },
- { 4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1 },
- { 7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1 },
- { 6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1 },
- { 0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1 },
- { 6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1 },
- { 0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1 },
- { 11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1 },
- { 6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1 },
- { 5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1 },
- { 9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1 },
- { 1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1 },
- { 10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1 },
- { 0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1 },
- { 11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1 },
- { 9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1 },
- { 7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1 },
- { 2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1 },
- { 9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1 },
- { 9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1 },
- { 1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1 },
- { 0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1 },
- { 10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1 },
- { 2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1 },
- { 0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1 },
- { 0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1 },
- { 9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1 },
- { 5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1 },
- { 5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1 },
- { 8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1 },
- { 9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1 },
- { 1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1 },
- { 3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1 },
- { 4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1 },
- { 9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1 },
- { 11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1 },
- { 11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1 },
- { 2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1 },
- { 9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1 },
- { 3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1 },
- { 1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1 },
- { 4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1 },
- { 0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1 },
- { 9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1 },
- { 1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { 0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
- { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }
- };
- const unsigned int
- _nx = (unsigned int)(size_x>=0?size_x:cimg::round((x1-x0)*-size_x/100 + 1)),
- _ny = (unsigned int)(size_y>=0?size_y:cimg::round((y1-y0)*-size_y/100 + 1)),
- _nz = (unsigned int)(size_z>=0?size_z:cimg::round((z1-z0)*-size_z/100 + 1)),
- nx = _nx?_nx:1,
- ny = _ny?_ny:1,
- nz = _nz?_nz:1,
- nxm1 = nx - 1,
- nym1 = ny - 1,
- nzm1 = nz - 1;
- if (!nxm1 || !nym1 || !nzm1) return;
- const float dx = (x1 - x0)/nxm1, dy = (y1 - y0)/nym1, dz = (z1 - z0)/nzm1;
- CImg<intT> indices1(nx,ny,1,3,-1), indices2(indices1);
- CImg<floatT> values1(nx,ny), values2(nx,ny);
- float X = 0, Y = 0, Z = 0, nX = 0, nY = 0, nZ = 0;
- int nb_vertices = 0;
- // Fill the first plane with function values
- Y = y0;
- cimg_forY(values1,y) {
- X = x0;
- cimg_forX(values1,x) { values1(x,y) = (float)func(X,Y,z0); X+=dx; }
- Y+=dy;
- }
- // Run Marching Cubes algorithm
- Z = z0; nZ = Z + dz;
- for (unsigned int zi = 0; zi<nzm1; ++zi, Z = nZ, nZ+=dz) {
- Y = y0; nY = Y + dy;
- indices2.fill(-1);
- X = x0; for (unsigned int xi = 0; xi<nx; ++xi) { values2(xi,0) = (float)func(X,Y,nZ); X += dx; }
- for (unsigned int yi = 0, nyi = 1; yi<nym1; ++yi, ++nyi, Y = nY, nY+=dy) {
- X = x0; nX = X + dx;
- values2(0,nyi) = (float)func(X,nY,nZ);
- for (unsigned int xi = 0, nxi = 1; xi<nxm1; ++xi, ++nxi, X = nX, nX+=dx) {
- // Determine cube configuration
- const float
- val0 = values1(xi,yi),
- val1 = values1(nxi,yi),
- val2 = values1(nxi,nyi),
- val3 = values1(xi,nyi),
- val4 = values2(xi,yi),
- val5 = values2(nxi,yi),
- val6 = values2(nxi,nyi) = (float)func(nX,nY,nZ),
- val7 = values2(xi,nyi);
- const unsigned int configuration =
- (val0<isovalue?1U:0U) | (val1<isovalue?2U:0U) | (val2<isovalue?4U:0U) | (val3<isovalue?8U:0U) |
- (val4<isovalue?16U:0U) | (val5<isovalue?32U:0U) | (val6<isovalue?64U:0U) | (val7<isovalue?128U:0U),
- edge = edges[configuration];
- // Compute intersection vertices
- if (edge) {
- if ((edge&1) && indices1(xi,yi,0)<0) {
- const float Xi = X + (isovalue-val0)*dx/(val1-val0);
- indices1(xi,yi,0) = nb_vertices++;
- add_vertex(Xi,Y,Z);
- }
- if ((edge&2) && indices1(nxi,yi,1)<0) {
- const float Yi = Y + (isovalue-val1)*dy/(val2-val1);
- indices1(nxi,yi,1) = nb_vertices++;
- add_vertex(nX,Yi,Z);
- }
- if ((edge&4) && indices1(xi,nyi,0)<0) {
- const float Xi = X + (isovalue-val3)*dx/(val2-val3);
- indices1(xi,nyi,0) = nb_vertices++;
- add_vertex(Xi,nY,Z);
- }
- if ((edge&8) && indices1(xi,yi,1)<0) {
- const float Yi = Y + (isovalue-val0)*dy/(val3-val0);
- indices1(xi,yi,1) = nb_vertices++;
- add_vertex(X,Yi,Z);
- }
- if ((edge&16) && indices2(xi,yi,0)<0) {
- const float Xi = X + (isovalue-val4)*dx/(val5-val4);
- indices2(xi,yi,0) = nb_vertices++;
- add_vertex(Xi,Y,nZ);
- }
- if ((edge&32) && indices2(nxi,yi,1)<0) {
- const float Yi = Y + (isovalue-val5)*dy/(val6-val5);
- indices2(nxi,yi,1) = nb_vertices++;
- add_vertex(nX,Yi,nZ);
- }
- if ((edge&64) && indices2(xi,nyi,0)<0) {
- const float Xi = X + (isovalue-val7)*dx/(val6-val7);
- indices2(xi,nyi,0) = nb_vertices++;
- add_vertex(Xi,nY,nZ);
- }
- if ((edge&128) && indices2(xi,yi,1)<0) {
- const float Yi = Y + (isovalue-val4)*dy/(val7-val4);
- indices2(xi,yi,1) = nb_vertices++;
- add_vertex(X,Yi,nZ);
- }
- if ((edge&256) && indices1(xi,yi,2)<0) {
- const float Zi = Z+ (isovalue-val0)*dz/(val4-val0);
- indices1(xi,yi,2) = nb_vertices++;
- add_vertex(X,Y,Zi);
- }
- if ((edge&512) && indices1(nxi,yi,2)<0) {
- const float Zi = Z + (isovalue-val1)*dz/(val5-val1);
- indices1(nxi,yi,2) = nb_vertices++;
- add_vertex(nX,Y,Zi);
- }
- if ((edge&1024) && indices1(nxi,nyi,2)<0) {
- const float Zi = Z + (isovalue-val2)*dz/(val6-val2);
- indices1(nxi,nyi,2) = nb_vertices++;
- add_vertex(nX,nY,Zi);
- }
- if ((edge&2048) && indices1(xi,nyi,2)<0) {
- const float Zi = Z + (isovalue-val3)*dz/(val7-val3);
- indices1(xi,nyi,2) = nb_vertices++;
- add_vertex(X,nY,Zi);
- }
- // Create triangles
- for (const int *triangle = triangles[configuration]; *triangle!=-1; ) {
- const unsigned int
- p0 = (unsigned int)*(triangle++),
- p1 = (unsigned int)*(triangle++),
- p2 = (unsigned int)*(triangle++);
- const int
- i0 = _isosurface3d_index(p0,indices1,indices2,xi,yi,nxi,nyi),
- i1 = _isosurface3d_index(p1,indices1,indices2,xi,yi,nxi,nyi),
- i2 = _isosurface3d_index(p2,indices1,indices2,xi,yi,nxi,nyi);
- add_triangle(i0,i2,i1);
- }
- }
- }
- }
- cimg::swap(values1,values2);
- cimg::swap(indices1,indices2);
- }
- }
- //! Compute isosurface of a function, as a 3D object \overloading.
- template<typename tf>
- static CImg<floatT> isosurface3d(CImgList<tf>& primitives, const char *const expression, const float isovalue,
- const float x0, const float y0, const float z0,
- const float x1, const float y1, const float z1,
- const int dx=32, const int dy=32, const int dz=32) {
- const _functor3d_expr func(expression);
- return isosurface3d(primitives,func,isovalue,x0,y0,z0,x1,y1,z1,dx,dy,dz);
- }
- template<typename t>
- static int _isosurface3d_index(const unsigned int edge, const CImg<t>& indices1, const CImg<t>& indices2,
- const unsigned int x, const unsigned int y,
- const unsigned int nx, const unsigned int ny) {
- switch (edge) {
- case 0 : return indices1(x,y,0);
- case 1 : return indices1(nx,y,1);
- case 2 : return indices1(x,ny,0);
- case 3 : return indices1(x,y,1);
- case 4 : return indices2(x,y,0);
- case 5 : return indices2(nx,y,1);
- case 6 : return indices2(x,ny,0);
- case 7 : return indices2(x,y,1);
- case 8 : return indices1(x,y,2);
- case 9 : return indices1(nx,y,2);
- case 10 : return indices1(nx,ny,2);
- case 11 : return indices1(x,ny,2);
- }
- return 0;
- }
- // Define functors for accessing image values (used in previous functions).
- struct _functor2d_int {
- const CImg<T>& ref;
- _functor2d_int(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y) const {
- return (float)ref((int)x,(int)y);
- }
- };
- struct _functor2d_float {
- const CImg<T>& ref;
- _functor2d_float(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y) const {
- return (float)ref._linear_atXY(x,y);
- }
- };
- struct _functor2d_expr {
- _cimg_math_parser *mp;
- ~_functor2d_expr() { mp->end(); delete mp; }
- _functor2d_expr(const char *const expr):mp(0) {
- mp = new _cimg_math_parser(expr,0,CImg<T>::const_empty(),0);
- }
- float operator()(const float x, const float y) const {
- return (float)(*mp)(x,y,0,0);
- }
- };
- struct _functor3d_int {
- const CImg<T>& ref;
- _functor3d_int(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y, const float z) const {
- return (float)ref((int)x,(int)y,(int)z);
- }
- };
- struct _functor3d_float {
- const CImg<T>& ref;
- _functor3d_float(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y, const float z) const {
- return (float)ref._linear_atXYZ(x,y,z);
- }
- };
- struct _functor3d_expr {
- _cimg_math_parser *mp;
- ~_functor3d_expr() { mp->end(); delete mp; }
- _functor3d_expr(const char *const expr):mp(0) {
- mp = new _cimg_math_parser(expr,0,CImg<T>::const_empty(),0);
- }
- float operator()(const float x, const float y, const float z) const {
- return (float)(*mp)(x,y,z,0);
- }
- };
- struct _functor4d_int {
- const CImg<T>& ref;
- _functor4d_int(const CImg<T>& pref):ref(pref) {}
- float operator()(const float x, const float y, const float z, const unsigned int c) const {
- return (float)ref((int)x,(int)y,(int)z,c);
- }
- };
- struct _functor_isoline3d {
- CImgList<T>& list;
- _functor_isoline3d(CImgList<T>& _list):list(_list) {}
- template<typename t>
- void operator()(const t x, const t y, const t z) { CImg<T>::vector((T)x,(T)y,(T)z).move_to(list); }
- template<typename t>
- void operator()(const t i, const t j) { CImg<T>::vector((T)i,(T)j).move_to(list); }
- };
- struct _functor_isosurface3d {
- CImgList<T>& list;
- _functor_isosurface3d(CImgList<T>& _list):list(_list) {}
- template<typename t>
- void operator()(const t x, const t y, const t z) { CImg<T>::vector((T)x,(T)y,(T)z).move_to(list); }
- };
- //! Compute 3D elevation of a function as a 3D object.
- /**
- \param[out] primitives Primitives data of the resulting 3D object.
- \param func Elevation function. Is of type <tt>float (*func)(const float x,const float y)</tt>.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param size_x Resolution of the function along the X-axis.
- \param size_y Resolution of the function along the Y-axis.
- **/
- template<typename tf, typename tfunc>
- static CImg<floatT> elevation3d(CImgList<tf>& primitives, const tfunc& func,
- const float x0, const float y0, const float x1, const float y1,
- const int size_x=256, const int size_y=256) {
- const float
- nx0 = x0<x1?x0:x1, ny0 = y0<y1?y0:y1,
- nx1 = x0<x1?x1:x0, ny1 = y0<y1?y1:y0;
- const unsigned int
- _nsize_x = (unsigned int)(size_x>=0?size_x:(nx1-nx0)*-size_x/100),
- nsize_x = _nsize_x?_nsize_x:1, nsize_x1 = nsize_x - 1,
- _nsize_y = (unsigned int)(size_y>=0?size_y:(ny1-ny0)*-size_y/100),
- nsize_y = _nsize_y?_nsize_y:1, nsize_y1 = nsize_y - 1;
- if (nsize_x<2 || nsize_y<2)
- throw CImgArgumentException("CImg<%s>::elevation3d(): Invalid specified size (%d,%d).",
- pixel_type(),
- nsize_x,nsize_y);
- CImg<floatT> vertices(nsize_x*nsize_y,3);
- floatT *ptr_x = vertices.data(0,0), *ptr_y = vertices.data(0,1), *ptr_z = vertices.data(0,2);
- for (unsigned int y = 0; y<nsize_y; ++y) {
- const float Y = ny0 + y*(ny1-ny0)/nsize_y1;
- for (unsigned int x = 0; x<nsize_x; ++x) {
- const float X = nx0 + x*(nx1-nx0)/nsize_x1;
- *(ptr_x++) = (float)x;
- *(ptr_y++) = (float)y;
- *(ptr_z++) = (float)func(X,Y);
- }
- }
- primitives.assign(nsize_x1*nsize_y1,1,4);
- for (unsigned int p = 0, y = 0; y<nsize_y1; ++y) {
- const unsigned int yw = y*nsize_x;
- for (unsigned int x = 0; x<nsize_x1; ++x) {
- const unsigned int xpyw = x + yw, xpyww = xpyw + nsize_x;
- primitives[p++].fill(xpyw,xpyww,xpyww + 1,xpyw + 1);
- }
- }
- return vertices;
- }
- //! Compute 3D elevation of a function, as a 3D object \overloading.
- template<typename tf>
- static CImg<floatT> elevation3d(CImgList<tf>& primitives, const char *const expression,
- const float x0, const float y0, const float x1, const float y1,
- const int size_x=256, const int size_y=256) {
- const _functor2d_expr func(expression);
- return elevation3d(primitives,func,x0,y0,x1,y1,size_x,size_y);
- }
- //! Generate a 3D box object.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param size_x The width of the box (dimension along the X-axis).
- \param size_y The height of the box (dimension along the Y-axis).
- \param size_z The depth of the box (dimension along the Z-axis).
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::box3d(faces3d,10,20,30);
- CImg<unsigned char>().display_object3d("Box3d",points3d,faces3d);
- \endcode
- \image html ref_box3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> box3d(CImgList<tf>& primitives,
- const float size_x=200, const float size_y=100, const float size_z=100) {
- 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);
- return CImg<floatT>(8,3,1,1,
- 0.,size_x,size_x, 0., 0.,size_x,size_x, 0.,
- 0., 0.,size_y,size_y, 0., 0.,size_y,size_y,
- 0., 0., 0., 0.,size_z,size_z,size_z,size_z);
- }
- //! Generate a 3D cone.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param radius The radius of the cone basis.
- \param size_z The cone's height.
- \param subdivisions The number of basis angular subdivisions.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::cone3d(faces3d,50);
- CImg<unsigned char>().display_object3d("Cone3d",points3d,faces3d);
- \endcode
- \image html ref_cone3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> cone3d(CImgList<tf>& primitives,
- const float radius=50, const float size_z=100, const unsigned int subdivisions=24) {
- primitives.assign();
- if (!subdivisions) return CImg<floatT>();
- CImgList<floatT> vertices(2,1,3,1,1,
- 0.,0.,size_z,
- 0.,0.,0.);
- for (float delta = 360.f/subdivisions, angle = 0; angle<360; angle+=delta) {
- const float a = (float)(angle*cimg::PI/180);
- CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0).move_to(vertices);
- }
- const unsigned int nbr = vertices._width - 2;
- for (unsigned int p = 0; p<nbr; ++p) {
- const unsigned int curr = 2 + p, next = 2 + ((p + 1)%nbr);
- CImg<tf>::vector(1,next,curr).move_to(primitives);
- CImg<tf>::vector(0,curr,next).move_to(primitives);
- }
- return vertices>'x';
- }
- //! Generate a 3D cylinder.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param radius The radius of the cylinder basis.
- \param size_z The cylinder's height.
- \param subdivisions The number of basis angular subdivisions.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::cylinder3d(faces3d,50);
- CImg<unsigned char>().display_object3d("Cylinder3d",points3d,faces3d);
- \endcode
- \image html ref_cylinder3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> cylinder3d(CImgList<tf>& primitives,
- const float radius=50, const float size_z=100, const unsigned int subdivisions=24) {
- primitives.assign();
- if (!subdivisions) return CImg<floatT>();
- CImgList<floatT> vertices(2,1,3,1,1,
- 0.,0.,0.,
- 0.,0.,size_z);
- for (float delta = 360.f/subdivisions, angle = 0; angle<360; angle+=delta) {
- const float a = (float)(angle*cimg::PI/180);
- CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),0.f).move_to(vertices);
- CImg<floatT>::vector((float)(radius*std::cos(a)),(float)(radius*std::sin(a)),size_z).move_to(vertices);
- }
- const unsigned int nbr = (vertices._width - 2)/2;
- for (unsigned int p = 0; p<nbr; ++p) {
- const unsigned int curr = 2 + 2*p, next = 2 + (2*((p + 1)%nbr));
- CImg<tf>::vector(0,next,curr).move_to(primitives);
- CImg<tf>::vector(1,curr + 1,next + 1).move_to(primitives);
- CImg<tf>::vector(curr,next,next + 1,curr + 1).move_to(primitives);
- }
- return vertices>'x';
- }
- //! Generate a 3D torus.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param radius1 The large radius.
- \param radius2 The small radius.
- \param subdivisions1 The number of angular subdivisions for the large radius.
- \param subdivisions2 The number of angular subdivisions for the small radius.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::torus3d(faces3d,20,4);
- CImg<unsigned char>().display_object3d("Torus3d",points3d,faces3d);
- \endcode
- \image html ref_torus3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> torus3d(CImgList<tf>& primitives,
- const float radius1=100, const float radius2=30,
- const unsigned int subdivisions1=24, const unsigned int subdivisions2=12) {
- primitives.assign();
- if (!subdivisions1 || !subdivisions2) return CImg<floatT>();
- CImgList<floatT> vertices;
- for (unsigned int v = 0; v<subdivisions1; ++v) {
- const float
- beta = (float)(v*2*cimg::PI/subdivisions1),
- xc = radius1*(float)std::cos(beta),
- yc = radius1*(float)std::sin(beta);
- for (unsigned int u = 0; u<subdivisions2; ++u) {
- const float
- alpha = (float)(u*2*cimg::PI/subdivisions2),
- x = xc + radius2*(float)(std::cos(alpha)*std::cos(beta)),
- y = yc + radius2*(float)(std::cos(alpha)*std::sin(beta)),
- z = radius2*(float)std::sin(alpha);
- CImg<floatT>::vector(x,y,z).move_to(vertices);
- }
- }
- for (unsigned int vv = 0; vv<subdivisions1; ++vv) {
- const unsigned int nv = (vv + 1)%subdivisions1;
- for (unsigned int uu = 0; uu<subdivisions2; ++uu) {
- const unsigned int nu = (uu + 1)%subdivisions2, svv = subdivisions2*vv, snv = subdivisions2*nv;
- CImg<tf>::vector(svv + nu,svv + uu,snv + uu,snv + nu).move_to(primitives);
- }
- }
- return vertices>'x';
- }
- //! Generate a 3D XY-plane.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param size_x The width of the plane (dimension along the X-axis).
- \param size_y The height of the plane (dimensions along the Y-axis).
- \param subdivisions_x The number of planar subdivisions along the X-axis.
- \param subdivisions_y The number of planar subdivisions along the Y-axis.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::plane3d(faces3d,100,50);
- CImg<unsigned char>().display_object3d("Plane3d",points3d,faces3d);
- \endcode
- \image html ref_plane3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> plane3d(CImgList<tf>& primitives,
- const float size_x=100, const float size_y=100,
- const unsigned int subdivisions_x=10, const unsigned int subdivisions_y=10) {
- primitives.assign();
- if (!subdivisions_x || !subdivisions_y) return CImg<floatT>();
- CImgList<floatT> vertices;
- const unsigned int w = subdivisions_x + 1, h = subdivisions_y + 1;
- const float fx = (float)size_x/w, fy = (float)size_y/h;
- for (unsigned int y = 0; y<h; ++y) for (unsigned int x = 0; x<w; ++x)
- CImg<floatT>::vector(fx*x,fy*y,0).move_to(vertices);
- for (unsigned int y = 0; y<subdivisions_y; ++y) for (unsigned int x = 0; x<subdivisions_x; ++x) {
- const int off1 = x + y*w, off2 = x + 1 + y*w, off3 = x + 1 + (y + 1)*w, off4 = x + (y + 1)*w;
- CImg<tf>::vector(off1,off4,off3,off2).move_to(primitives);
- }
- return vertices>'x';
- }
- //! Generate a 3D sphere.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param radius The radius of the sphere (dimension along the X-axis).
- \param subdivisions The number of recursive subdivisions from an initial icosahedron.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> points3d = CImg<float>::sphere3d(faces3d,100,4);
- CImg<unsigned char>().display_object3d("Sphere3d",points3d,faces3d);
- \endcode
- \image html ref_sphere3d.jpg
- **/
- template<typename tf>
- static CImg<floatT> sphere3d(CImgList<tf>& primitives,
- const float radius=50, const unsigned int subdivisions=3) {
- // Create initial icosahedron
- primitives.assign();
- const double tmp = (1 + std::sqrt(5.f))/2, a = 1./std::sqrt(1 + tmp*tmp), b = tmp*a;
- 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,
- -a,0.,-b, -a,0.,b, 0.,b,a, 0.,-b,a, 0.,-b,-a, 0.,b,-a);
- 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,
- 8,0,11, 8,11,1, 9,10,3, 9,2,10, 8,4,0, 11,0,5, 4,9,3,
- 5,3,10, 7,8,1, 6,1,11, 7,2,9, 6,10,2);
- // edge - length/2
- float he = (float)a;
- // Recurse subdivisions
- for (unsigned int i = 0; i<subdivisions; ++i) {
- const unsigned int L = primitives._width;
- he/=2;
- const float he2 = he*he;
- for (unsigned int l = 0; l<L; ++l) {
- const unsigned int
- p0 = (unsigned int)primitives(0,0), p1 = (unsigned int)primitives(0,1), p2 = (unsigned int)primitives(0,2);
- const float
- x0 = vertices(p0,0), y0 = vertices(p0,1), z0 = vertices(p0,2),
- x1 = vertices(p1,0), y1 = vertices(p1,1), z1 = vertices(p1,2),
- x2 = vertices(p2,0), y2 = vertices(p2,1), z2 = vertices(p2,2),
- tnx0 = (x0 + x1)/2, tny0 = (y0 + y1)/2, tnz0 = (z0 + z1)/2,
- nn0 = cimg::hypot(tnx0,tny0,tnz0),
- tnx1 = (x0 + x2)/2, tny1 = (y0 + y2)/2, tnz1 = (z0 + z2)/2,
- nn1 = cimg::hypot(tnx1,tny1,tnz1),
- tnx2 = (x1 + x2)/2, tny2 = (y1 + y2)/2, tnz2 = (z1 + z2)/2,
- nn2 = cimg::hypot(tnx2,tny2,tnz2),
- nx0 = tnx0/nn0, ny0 = tny0/nn0, nz0 = tnz0/nn0,
- nx1 = tnx1/nn1, ny1 = tny1/nn1, nz1 = tnz1/nn1,
- nx2 = tnx2/nn2, ny2 = tny2/nn2, nz2 = tnz2/nn2;
- int i0 = -1, i1 = -1, i2 = -1;
- cimglist_for(vertices,p) {
- const float x = (float)vertices(p,0), y = (float)vertices(p,1), z = (float)vertices(p,2);
- if (cimg::sqr(x-nx0) + cimg::sqr(y-ny0) + cimg::sqr(z-nz0)<he2) i0 = p;
- if (cimg::sqr(x-nx1) + cimg::sqr(y-ny1) + cimg::sqr(z-nz1)<he2) i1 = p;
- if (cimg::sqr(x-nx2) + cimg::sqr(y-ny2) + cimg::sqr(z-nz2)<he2) i2 = p;
- }
- if (i0<0) { CImg<floatT>::vector(nx0,ny0,nz0).move_to(vertices); i0 = vertices.width() - 1; }
- if (i1<0) { CImg<floatT>::vector(nx1,ny1,nz1).move_to(vertices); i1 = vertices.width() - 1; }
- if (i2<0) { CImg<floatT>::vector(nx2,ny2,nz2).move_to(vertices); i2 = vertices.width() - 1; }
- primitives.remove(0);
- CImg<tf>::vector(p0,i0,i1).move_to(primitives);
- CImg<tf>::vector((tf)i0,(tf)p1,(tf)i2).move_to(primitives);
- CImg<tf>::vector((tf)i1,(tf)i2,(tf)p2).move_to(primitives);
- CImg<tf>::vector((tf)i1,(tf)i0,(tf)i2).move_to(primitives);
- }
- }
- return (vertices>'x')*=radius;
- }
- //! Generate a 3D ellipsoid.
- /**
- \param[out] primitives The returned list of the 3D object primitives
- (template type \e tf should be at least \e unsigned \e int).
- \param tensor The tensor which gives the shape and size of the ellipsoid.
- \param subdivisions The number of recursive subdivisions from an initial stretched icosahedron.
- \return The N vertices (xi,yi,zi) of the 3D object as a Nx3 CImg<float> image (0<=i<=N - 1).
- \par Example
- \code
- CImgList<unsigned int> faces3d;
- const CImg<float> tensor = CImg<float>::diagonal(10,7,3),
- points3d = CImg<float>::ellipsoid3d(faces3d,tensor,4);
- CImg<unsigned char>().display_object3d("Ellipsoid3d",points3d,faces3d);
- \endcode
- \image html ref_ellipsoid3d.jpg
- **/
- template<typename tf, typename t>
- static CImg<floatT> ellipsoid3d(CImgList<tf>& primitives,
- const CImg<t>& tensor, const unsigned int subdivisions=3) {
- primitives.assign();
- if (!subdivisions) return CImg<floatT>();
- CImg<floatT> S, V;
- tensor.symmetric_eigen(S,V);
- const float orient =
- (V(0,1)*V(1,2) - V(0,2)*V(1,1))*V(2,0) +
- (V(0,2)*V(1,0) - V(0,0)*V(1,2))*V(2,1) +
- (V(0,0)*V(1,1) - V(0,1)*V(1,0))*V(2,2);
- if (orient<0) { V(2,0) = -V(2,0); V(2,1) = -V(2,1); V(2,2) = -V(2,2); }
- const float l0 = S[0], l1 = S[1], l2 = S[2];
- CImg<floatT> vertices = sphere3d(primitives,1.,subdivisions);
- vertices.get_shared_row(0)*=l0;
- vertices.get_shared_row(1)*=l1;
- vertices.get_shared_row(2)*=l2;
- return V*vertices;
- }
- //! Convert 3D object into a CImg3d representation.
- /**
- \param primitives Primitives data of the 3D object.
- \param colors Colors data of the 3D object.
- \param opacities Opacities data of the 3D object.
- \param full_check Tells if full checking of the 3D object must be performed.
- **/
- template<typename tp, typename tc, typename to>
- CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool full_check=true) {
- return get_object3dtoCImg3d(primitives,colors,opacities,full_check).move_to(*this);
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- template<typename tp, typename tc>
- CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const bool full_check=true) {
- return get_object3dtoCImg3d(primitives,colors,full_check).move_to(*this);
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- template<typename tp>
- CImg<T>& object3dtoCImg3d(const CImgList<tp>& primitives,
- const bool full_check=true) {
- return get_object3dtoCImg3d(primitives,full_check).move_to(*this);
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- CImg<T>& object3dtoCImg3d(const bool full_check=true) {
- return get_object3dtoCImg3d(full_check).move_to(*this);
- }
- //! Convert 3D object into a CImg3d representation \newinstance.
- template<typename tp, typename tc, typename to>
- CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool full_check=true) const {
- CImg<charT> error_message(1024);
- if (!is_object3d(primitives,colors,opacities,full_check,error_message))
- throw CImgInstanceException(_cimg_instance
- "object3dtoCImg3d(): Invalid specified 3D object (%u,%u) (%s).",
- cimg_instance,_width,primitives._width,error_message.data());
- CImg<floatT> res(1,_size_object3dtoCImg3d(primitives,colors,opacities));
- float *ptrd = res._data;
- // Put magick number.
- *(ptrd++) = 'C' + 0.5f; *(ptrd++) = 'I' + 0.5f; *(ptrd++) = 'm' + 0.5f;
- *(ptrd++) = 'g' + 0.5f; *(ptrd++) = '3' + 0.5f; *(ptrd++) = 'd' + 0.5f;
- // Put number of vertices and primitives.
- *(ptrd++) = cimg::uint2float(_width);
- *(ptrd++) = cimg::uint2float(primitives._width);
- // Put vertex data.
- if (is_empty() || !primitives) return res;
- const T *ptrx = data(0,0), *ptry = data(0,1), *ptrz = data(0,2);
- cimg_forX(*this,p) {
- *(ptrd++) = (float)*(ptrx++);
- *(ptrd++) = (float)*(ptry++);
- *(ptrd++) = (float)*(ptrz++);
- }
- // Put primitive data.
- cimglist_for(primitives,p) {
- *(ptrd++) = (float)primitives[p].size();
- const tp *ptrp = primitives[p]._data;
- cimg_foroff(primitives[p],i) *(ptrd++) = cimg::uint2float((unsigned int)*(ptrp++));
- }
- // Put color/texture data.
- const unsigned int csiz = std::min(colors._width,primitives._width);
- for (int c = 0; c<(int)csiz; ++c) {
- const CImg<tc>& color = colors[c];
- const tc *ptrc = color._data;
- if (color.size()==3) { *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*(ptrc++); *(ptrd++) = (float)*ptrc; }
- else {
- *(ptrd++) = -128.f;
- int shared_ind = -1;
- if (color.is_shared()) for (int i = 0; i<c; ++i) if (ptrc==colors[i]._data) { shared_ind = i; break; }
- if (shared_ind<0) {
- *(ptrd++) = (float)color._width;
- *(ptrd++) = (float)color._height;
- *(ptrd++) = (float)color._spectrum;
- cimg_foroff(color,l) *(ptrd++) = (float)*(ptrc++);
- } else {
- *(ptrd++) = (float)shared_ind;
- *(ptrd++) = 0;
- *(ptrd++) = 0;
- }
- }
- }
- const int csiz2 = primitives.width() - colors.width();
- for (int c = 0; c<csiz2; ++c) { *(ptrd++) = 200.f; *(ptrd++) = 200.f; *(ptrd++) = 200.f; }
- // Put opacity data.
- ptrd = _object3dtoCImg3d(opacities,ptrd);
- const float *ptre = res.end();
- while (ptrd<ptre) *(ptrd++) = 1.f;
- return res;
- }
- template<typename to>
- float* _object3dtoCImg3d(const CImgList<to>& opacities, float *ptrd) const {
- cimglist_for(opacities,o) {
- const CImg<to>& opacity = opacities[o];
- const to *ptro = opacity._data;
- if (opacity.size()==1) *(ptrd++) = (float)*ptro;
- else {
- *(ptrd++) = -128.f;
- int shared_ind = -1;
- if (opacity.is_shared()) for (int i = 0; i<o; ++i) if (ptro==opacities[i]._data) { shared_ind = i; break; }
- if (shared_ind<0) {
- *(ptrd++) = (float)opacity._width;
- *(ptrd++) = (float)opacity._height;
- *(ptrd++) = (float)opacity._spectrum;
- cimg_foroff(opacity,l) *(ptrd++) = (float)*(ptro++);
- } else {
- *(ptrd++) = (float)shared_ind;
- *(ptrd++) = 0;
- *(ptrd++) = 0;
- }
- }
- }
- return ptrd;
- }
- template<typename to>
- float* _object3dtoCImg3d(const CImg<to>& opacities, float *ptrd) const {
- const to *ptro = opacities._data;
- cimg_foroff(opacities,o) *(ptrd++) = (float)*(ptro++);
- return ptrd;
- }
- template<typename tp, typename tc, typename to>
- unsigned int _size_object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const CImgList<to>& opacities) const {
- unsigned int siz = 8U + 3*_width;
- cimglist_for(primitives,p) siz+=primitives[p].size() + 1;
- for (int c = std::min(primitives.width(),colors.width()) - 1; c>=0; --c) {
- if (colors[c].is_shared()) siz+=4;
- else { const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4 + csiz:3; }
- }
- if (colors._width<primitives._width) siz+=3*(primitives._width - colors._width);
- cimglist_for(opacities,o) {
- if (opacities[o].is_shared()) siz+=4;
- else { const unsigned int osiz = opacities[o].size(); siz+=(osiz!=1)?4 + osiz:1; }
- }
- siz+=primitives._width - opacities._width;
- return siz;
- }
- template<typename tp, typename tc, typename to>
- unsigned int _size_object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const CImg<to>& opacities) const {
- unsigned int siz = 8U + 3*_width;
- cimglist_for(primitives,p) siz+=primitives[p].size() + 1;
- for (int c = std::min(primitives.width(),colors.width()) - 1; c>=0; --c) {
- const unsigned int csiz = colors[c].size(); siz+=(csiz!=3)?4 + csiz:3;
- }
- if (colors._width<primitives._width) siz+=3*(primitives._width - colors._width);
- siz+=primitives.size();
- cimg::unused(opacities);
- return siz;
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- template<typename tp, typename tc>
- CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
- const CImgList<tc>& colors,
- const bool full_check=true) const {
- CImgList<T> opacities;
- return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- template<typename tp>
- CImg<floatT> get_object3dtoCImg3d(const CImgList<tp>& primitives,
- const bool full_check=true) const {
- CImgList<T> colors, opacities;
- return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
- }
- //! Convert 3D object into a CImg3d representation \overloading.
- CImg<floatT> get_object3dtoCImg3d(const bool full_check=true) const {
- CImgList<T> opacities, colors;
- CImgList<uintT> primitives(width(),1,1,1,1);
- cimglist_for(primitives,p) primitives(p,0) = p;
- return get_object3dtoCImg3d(primitives,colors,opacities,full_check);
- }
- //! Convert CImg3d representation into a 3D object.
- /**
- \param[out] primitives Primitives data of the 3D object.
- \param[out] colors Colors data of the 3D object.
- \param[out] opacities Opacities data of the 3D object.
- \param full_check Tells if full checking of the 3D object must be performed.
- **/
- template<typename tp, typename tc, typename to>
- CImg<T>& CImg3dtoobject3d(CImgList<tp>& primitives,
- CImgList<tc>& colors,
- CImgList<to>& opacities,
- const bool full_check=true) {
- return get_CImg3dtoobject3d(primitives,colors,opacities,full_check).move_to(*this);
- }
- //! Convert CImg3d representation into a 3D object \newinstance.
- template<typename tp, typename tc, typename to>
- CImg<T> get_CImg3dtoobject3d(CImgList<tp>& primitives,
- CImgList<tc>& colors,
- CImgList<to>& opacities,
- const bool full_check=true) const {
- CImg<charT> error_message(1024);
- if (!is_CImg3d(full_check,error_message))
- throw CImgInstanceException(_cimg_instance
- "CImg3dtoobject3d(): image instance is not a CImg3d (%s).",
- cimg_instance,error_message.data());
- const T *ptrs = _data + 6;
- const unsigned int
- nb_points = cimg::float2uint((float)*(ptrs++)),
- nb_primitives = cimg::float2uint((float)*(ptrs++));
- const CImg<T> points = CImg<T>(ptrs,3,nb_points,1,1,true).get_transpose();
- ptrs+=3*nb_points;
- primitives.assign(nb_primitives);
- cimglist_for(primitives,p) {
- const unsigned int nb_inds = (unsigned int)*(ptrs++);
- primitives[p].assign(1,nb_inds);
- tp *ptrp = primitives[p]._data;
- for (unsigned int i = 0; i<nb_inds; ++i) *(ptrp++) = (tp)cimg::float2uint((float)*(ptrs++));
- }
- colors.assign(nb_primitives);
- cimglist_for(colors,c) {
- if (*ptrs==(T)-128) {
- ++ptrs;
- const unsigned int w = (unsigned int)*(ptrs++), h = (unsigned int)*(ptrs++), s = (unsigned int)*(ptrs++);
- if (!h && !s) colors[c].assign(colors[w],true);
- else { colors[c].assign(ptrs,w,h,1,s,false); ptrs+=w*h*s; }
- } else { colors[c].assign(ptrs,1,1,1,3,false); ptrs+=3; }
- }
- opacities.assign(nb_primitives);
- cimglist_for(opacities,o) {
- if (*ptrs==(T)-128) {
- ++ptrs;
- const unsigned int w = (unsigned int)*(ptrs++), h = (unsigned int)*(ptrs++), s = (unsigned int)*(ptrs++);
- if (!h && !s) opacities[o].assign(opacities[w],true);
- else { opacities[o].assign(ptrs,w,h,1,s,false); ptrs+=w*h*s; }
- } else opacities[o].assign(1,1,1,1,*(ptrs++));
- }
- return points;
- }
- //@}
- //---------------------------
- //
- //! \name Drawing Functions
- //@{
- //---------------------------
- #define cimg_init_scanline(opacity) \
- static const T _sc_maxval = (T)std::min(cimg::type<T>::max(),(T)cimg::type<tc>::max()); \
- const float _sc_nopacity = cimg::abs((float)opacity), _sc_copacity = 1 - std::max((float)opacity,0.f); \
- const ulongT _sc_whd = (ulongT)_width*_height*_depth; \
- cimg::unused(_sc_maxval);
- #define cimg_draw_scanline(x0,x1,y,color,opacity,brightness) \
- _draw_scanline(x0,x1,y,color,opacity,brightness,_sc_nopacity,_sc_copacity,_sc_whd,_sc_maxval)
- // [internal] The following _draw_scanline() routines are *non user-friendly functions*,
- // used only for internal purpose.
- // Pre-requisites: x0<=x1, y-coordinate is valid, col is valid.
- template<typename tc>
- CImg<T>& _draw_scanline(const int x0, const int x1, const int y,
- const tc *const color, const float opacity,
- const float brightness,
- const float nopacity, const float copacity, const ulongT whd, const T _sc_maxval) {
- const int nx0 = x0>0?x0:0, nx1 = x1<width()?x1:width() - 1, dx = nx1 - nx0;
- if (dx>=0) {
- const tc *col = color;
- const ulongT off = whd - dx - 1;
- T *ptrd = data(nx0,y);
- if (opacity>=1) { // ** Opaque drawing **
- if (brightness==1) { // Brightness==1
- if (sizeof(T)!=1) cimg_forC(*this,c) {
- const T val = (T)*(col++);
- for (int x = dx; x>=0; --x) *(ptrd++) = val;
- ptrd+=off;
- } else cimg_forC(*this,c) {
- const T val = (T)*(col++);
- std::memset(ptrd,(int)val,dx + 1);
- ptrd+=whd;
- }
- } else if (brightness<1) { // Brightness<1
- if (sizeof(T)!=1) cimg_forC(*this,c) {
- const T val = (T)(*(col++)*brightness);
- for (int x = dx; x>=0; --x) *(ptrd++) = val;
- ptrd+=off;
- } else cimg_forC(*this,c) {
- const T val = (T)(*(col++)*brightness);
- std::memset(ptrd,(int)val,dx + 1);
- ptrd+=whd;
- }
- } else { // Brightness>1
- if (sizeof(T)!=1) cimg_forC(*this,c) {
- const T val = (T)((2-brightness)**(col++) + (brightness - 1)*_sc_maxval);
- for (int x = dx; x>=0; --x) *(ptrd++) = val;
- ptrd+=off;
- } else cimg_forC(*this,c) {
- const T val = (T)((2-brightness)**(col++) + (brightness - 1)*_sc_maxval);
- std::memset(ptrd,(int)val,dx + 1);
- ptrd+=whd;
- }
- }
- } else { // ** Transparent drawing **
- if (brightness==1) { // Brightness==1
- cimg_forC(*this,c) {
- const Tfloat val = *(col++)*nopacity;
- for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
- ptrd+=off;
- }
- } else if (brightness<=1) { // Brightness<1
- cimg_forC(*this,c) {
- const Tfloat val = *(col++)*brightness*nopacity;
- for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
- ptrd+=off;
- }
- } else { // Brightness>1
- cimg_forC(*this,c) {
- const Tfloat val = ((2-brightness)**(col++) + (brightness - 1)*_sc_maxval)*nopacity;
- for (int x = dx; x>=0; --x) { *ptrd = (T)(val + *ptrd*copacity); ++ptrd; }
- ptrd+=off;
- }
- }
- }
- }
- return *this;
- }
- //! Draw a 3D point.
- /**
- \param x0 X-coordinate of the point.
- \param y0 Y-coordinate of the point.
- \param z0 Z-coordinate of the point.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \note
- - To set pixel values without clipping needs, you should use the faster CImg::operator()() function.
- \par Example:
- \code
- CImg<unsigned char> img(100,100,1,3,0);
- const unsigned char color[] = { 255,128,64 };
- img.draw_point(50,50,color);
- \endcode
- **/
- template<typename tc>
- CImg<T>& draw_point(const int x0, const int y0, const int z0,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_point(): Specified color is (null).",
- cimg_instance);
- if (x0>=0 && y0>=0 && z0>=0 && x0<width() && y0<height() && z0<depth()) {
- const ulongT whd = (ulongT)_width*_height*_depth;
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- T *ptrd = data(x0,y0,z0,0);
- const tc *col = color;
- if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)*(col++); ptrd+=whd; }
- else cimg_forC(*this,c) { *ptrd = (T)(*(col++)*nopacity + *ptrd*copacity); ptrd+=whd; }
- }
- return *this;
- }
- //! Draw a 2D point \simplification.
- template<typename tc>
- CImg<T>& draw_point(const int x0, const int y0,
- const tc *const color, const float opacity=1) {
- return draw_point(x0,y0,0,color,opacity);
- }
- // Draw a points cloud.
- /**
- \param points Image of vertices coordinates.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_point(const CImg<t>& points,
- const tc *const color, const float opacity=1) {
- if (is_empty() || !points) return *this;
- switch (points._height) {
- case 0 : case 1 :
- throw CImgArgumentException(_cimg_instance
- "draw_point(): Invalid specified point set (%u,%u,%u,%u,%p).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum,points._data);
- case 2 : {
- cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),color,opacity);
- } break;
- default : {
- cimg_forX(points,i) draw_point((int)points(i,0),(int)points(i,1),(int)points(i,2),color,opacity);
- }
- }
- return *this;
- }
- //! Draw a 2D line.
- /**
- \param x0 X-coordinate of the starting line point.
- \param y0 Y-coordinate of the starting line point.
- \param x1 X-coordinate of the ending line point.
- \param y1 Y-coordinate of the ending line point.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch Tells if a reinitialization of the hash state must be done.
- \note
- - Line routine uses Bresenham's algorithm.
- - Set \p init_hatch = false to draw consecutive hatched segments without breaking the line pattern.
- \par Example:
- \code
- CImg<unsigned char> img(100,100,1,3,0);
- const unsigned char color[] = { 255,128,64 };
- img.draw_line(40,40,80,70,color);
- \endcode
- **/
- template<typename tc>
- CImg<T>& draw_line(int x0, int y0,
- int x1, int y1,
- const tc *const color, const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || !opacity || !pattern ||
- std::min(y0,y1)>=height() || std::max(y0,y1)<0 ||
- std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
- int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dy01 = y1 - y0;
- const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
- if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
- if (pattern==~0U && y0>y1) {
- cimg::swap(x0,x1,y0,y1);
- dx01*=-1; dy01*=-1;
- }
- static unsigned int hatch = ~0U - (~0U>>1);
- if (init_hatch) hatch = ~0U - (~0U>>1);
- cimg_init_scanline(opacity);
- const int
- step = y0<=y1?1:-1,hdy01 = dy01*cimg::sign(dx01)/2,
- cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
- dy01+=dy01?0:1;
- for (int y = cy0; y!=cy1; y+=step) {
- const int
- yy0 = y - y0,
- x = x0 + (dx01*yy0 + hdy01)/dy01;
- if (x>=0 && x<=w1 && pattern&hatch) {
- T *const ptrd = is_horizontal?data(y,x):data(x,y);
- cimg_forC(*this,c) {
- const T val = color[c];
- ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
- }
- return *this;
- }
- //! Draw a 2D line, with z-buffering.
- /**
- \param zbuffer Zbuffer image.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param z0 Z-coordinate of the starting point
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param z1 Z-coordinate of the ending point.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch Tells if a reinitialization of the hash state must be done.
- **/
- template<typename tz,typename tc>
- CImg<T>& draw_line(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- const tc *const color, const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Specified color is (null).",
- cimg_instance);
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
- float iz0 = 1/z0, iz1 = 1/z1;
- int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dy01 = y1 - y0;
- float diz01 = iz1 - iz0;
- const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
- if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
- if (pattern==~0U && y0>y1) {
- cimg::swap(x0,x1,y0,y1,iz0,iz1);
- dx01*=-1; dy01*=-1; diz01*=-1;
- }
- static unsigned int hatch = ~0U - (~0U>>1);
- if (init_hatch) hatch = ~0U - (~0U>>1);
- cimg_init_scanline(opacity);
- const int
- step = y0<=y1?1:-1, hdy01 = dy01*cimg::sign(dx01)/2,
- cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
- dy01+=dy01?0:1;
- for (int y = cy0; y!=cy1; y+=step) {
- const int
- yy0 = y - y0,
- x = x0 + (dx01*yy0 + hdy01)/dy01;
- const float iz = iz0 + diz01*yy0/dy01;
- tz *const ptrz = is_horizontal?zbuffer.data(y,x):zbuffer.data(x,y);
- if (x>=0 && x<=w1 && pattern&hatch && iz>=*ptrz) {
- *ptrz = (tz)iz;
- T *const ptrd = is_horizontal?data(y,x):data(x,y);
- cimg_forC(*this,c) {
- const T val = color[c];
- ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
- }
- return *this;
- }
- //! Draw a textured 2D line.
- /**
- \param x0 X-coordinate of the starting line point.
- \param y0 Y-coordinate of the starting line point.
- \param x1 X-coordinate of the ending line point.
- \param y1 Y-coordinate of the ending line point.
- \param texture Texture image defining the pixel colors.
- \param tx0 X-coordinate of the starting texture point.
- \param ty0 Y-coordinate of the starting texture point.
- \param tx1 X-coordinate of the ending texture point.
- \param ty1 Y-coordinate of the ending texture point.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch Tells if the hash variable must be reinitialized.
- \note
- - Line routine uses the well known Bresenham's algorithm.
- \par Example:
- \code
- CImg<unsigned char> img(100,100,1,3,0), texture("texture256x256.ppm");
- const unsigned char color[] = { 255,128,64 };
- img.draw_line(40,40,80,70,texture,0,0,255,255);
- \endcode
- **/
- template<typename tc>
- CImg<T>& draw_line(int x0, int y0,
- int x1, int y1,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || !opacity || !pattern) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture)) return draw_line(x0,y0,x1,y1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
- if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
- int w1 = width() - 1, h1 = height() - 1;
- longT
- dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0,
- dtx01 = (longT)tx1 - tx0, dty01 = (longT)ty1 - ty0;
- const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
- if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
- if (pattern==~0U && y0>y1) {
- cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1);
- dx01*=-1; dy01*=-1; dtx01*=-1; dty01*=-1;
- }
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- static unsigned int hatch = ~0U - (~0U>>1);
- if (init_hatch) hatch = ~0U - (~0U>>1);
- cimg_init_scanline(opacity);
- const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
- const longT
- hdy01 = dy01*cimg::sign(dx01)/2,
- hdy01tx = dy01*cimg::sign(dtx01)/2,
- hdy01ty = dy01*cimg::sign(dty01)/2;
- dy01+=dy01?0:1;
- for (int y = cy0; y!=cy1; y+=step) {
- const longT
- yy0 = (longT)y - y0,
- x = x0 + (dx01*yy0 + hdy01)/dy01,
- tx = tx0 + (dtx01*yy0 + hdy01tx)/dy01,
- ty = ty0 + (dty01*yy0 + hdy01ty)/dy01;
- if (x>=0 && x<=w1 && pattern&hatch) {
- T *const ptrd = is_horizontal?data(y,x):data(x,y);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const T val = color[c*twhd];
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
- }
- return *this;
- }
- //! Draw a textured 2D line, with perspective correction.
- /**
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param z0 Z-coordinate of the starting point
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param z1 Z-coordinate of the ending point.
- \param texture Texture image defining the pixel colors.
- \param tx0 X-coordinate of the starting texture point.
- \param ty0 Y-coordinate of the starting texture point.
- \param tx1 X-coordinate of the ending texture point.
- \param ty1 Y-coordinate of the ending texture point.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch Tells if the hash variable must be reinitialized.
- **/
- template<typename tc>
- CImg<T>& draw_line(int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- const CImg<tc>& texture,
- const int tx0, const int ty0,
- const int tx1, const int ty1,
- const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_line(x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
- if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
- float iz0 = 1/z0, iz1 = 1/z1;
- int w1 = width() - 1, h1 = height() - 1;
- longT dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0;
- float
- diz01 = iz1 - iz0,
- txz0 = tx0*iz0, txz1 = tx1*iz1,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1,
- dtxz01 = txz1 - txz0, dtyz01 = tyz1 - tyz0;
- const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
- if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
- if (pattern==~0U && y0>y1) {
- cimg::swap(x0,x1,y0,y1,iz0,iz1,txz0,txz1,tyz0,tyz1);
- dx01*=-1; dy01*=-1; diz01*=-1; dtxz01*=-1; dtyz01*=-1;
- }
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- static unsigned int hatch = ~0U - (~0U>>1);
- if (init_hatch) hatch = ~0U - (~0U>>1);
- cimg_init_scanline(opacity);
- const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
- const longT hdy01 = dy01*cimg::sign(dx01)/2;
- dy01+=dy01?0:1;
- for (int y = cy0; y!=cy1; y+=step) {
- const longT
- yy0 = (longT)y - y0,
- x = x0 + (dx01*yy0 + hdy01)/dy01;
- const float
- iz = iz0 + diz01*yy0/dy01,
- txz = txz0 + dtxz01*yy0/dy01,
- tyz = tyz0 + dtyz01*yy0/dy01;
- if (x>=0 && x<=w1 && pattern&hatch) {
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- T *const ptrd = is_horizontal?data(y,x):data(x,y);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const T val = color[c*twhd];
- ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
- }
- return *this;
- }
- //! Draw a textured 2D line, with perspective correction and z-buffering.
- /**
- \param zbuffer Z-buffer image.
- \param x0 X-coordinate of the starting point.
- \param y0 Y-coordinate of the starting point.
- \param z0 Z-coordinate of the starting point
- \param x1 X-coordinate of the ending point.
- \param y1 Y-coordinate of the ending point.
- \param z1 Z-coordinate of the ending point.
- \param texture Texture image defining the pixel colors.
- \param tx0 X-coordinate of the starting texture point.
- \param ty0 Y-coordinate of the starting texture point.
- \param tx1 X-coordinate of the ending texture point.
- \param ty1 Y-coordinate of the ending texture point.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch Tells if the hash variable must be reinitialized.
- **/
- template<typename tz, typename tc>
- CImg<T>& draw_line(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- const CImg<tc>& texture,
- const int tx0, const int ty0,
- const int tx1, const int ty1,
- const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || z0<=0 || z1<=0 || !opacity || !pattern) return *this;
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_line(zbuffer,x0,y0,z0,x1,y1,z1,+texture,tx0,ty0,tx1,ty1,opacity,pattern,init_hatch);
- if (std::min(y0,y1)>=height() || std::max(y0,y1)<0 || std::min(x0,x1)>=width() || std::max(x0,x1)<0) return *this;
- float iz0 = 1/z0, iz1 = 1/z1;
- int w1 = width() - 1, h1 = height() - 1;
- longT dx01 = (longT)x1 - x0, dy01 = (longT)y1 - y0;
- float
- diz01 = iz1 - iz0,
- txz0 = tx0*iz0, txz1 = tx1*iz1,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1,
- dtxz01 = txz1 - txz0, dtyz01 = tyz1 - tyz0;
- const bool is_horizontal = cimg::abs(dx01)>cimg::abs(dy01);
- if (is_horizontal) cimg::swap(x0,y0,x1,y1,w1,h1,dx01,dy01);
- if (pattern==~0U && y0>y1) {
- cimg::swap(x0,x1,y0,y1,iz0,iz1,txz0,txz1,tyz0,tyz1);
- dx01*=-1; dy01*=-1; diz01*=-1; dtxz01*=-1; dtyz01*=-1;
- }
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- static unsigned int hatch = ~0U - (~0U>>1);
- if (init_hatch) hatch = ~0U - (~0U>>1);
- cimg_init_scanline(opacity);
- const int step = y0<=y1?1:-1, cy0 = cimg::cut(y0,0,h1), cy1 = cimg::cut(y1,0,h1) + step;
- const longT hdy01 = dy01*cimg::sign(dx01)/2;
- dy01+=dy01?0:1;
- for (int y = cy0; y!=cy1; y+=step) {
- const longT
- yy0 = (longT)y - y0,
- x = x0 + (dx01*yy0 + hdy01)/dy01;
- const float
- iz = iz0 + diz01*yy0/dy01,
- txz = txz0 + dtxz01*yy0/dy01,
- tyz = tyz0 + dtyz01*yy0/dy01;
- tz *const ptrz = is_horizontal?zbuffer.data(y,x):zbuffer.data(x,y);
- if (x>=0 && x<=w1 && pattern&hatch && iz>=*ptrz) {
- *ptrz = (tz)iz;
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- T *const ptrd = is_horizontal?data(y,x):data(x,y);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const T val = color[c*twhd];
- ptrd[c*_sc_whd] = opacity>=1?val:(T)(val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- if (!(hatch>>=1)) hatch = ~0U - (~0U>>1);
- }
- return *this;
- }
- //! Draw a set of consecutive lines.
- /**
- \param points Coordinates of vertices, stored as a list of vectors.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch If set to true, init hatch motif.
- \note
- - This function uses several call to the single CImg::draw_line() procedure,
- depending on the vectors size in \p points.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_line(const CImg<t>& points,
- const tc *const color, const float opacity=1,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || !points || points._width<2) return *this;
- bool ninit_hatch = init_hatch;
- switch (points._height) {
- case 0 : case 1 :
- throw CImgArgumentException(_cimg_instance
- "draw_line(): Invalid specified point set (%u,%u,%u,%u,%p).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum,points._data);
- default : {
- const int x0 = (int)points(0,0), y0 = (int)points(0,1);
- int ox = x0, oy = y0;
- for (unsigned int i = 1; i<points._width; ++i) {
- const int x = (int)points(i,0), y = (int)points(i,1);
- draw_line(ox,oy,x,y,color,opacity,pattern,ninit_hatch);
- ninit_hatch = false;
- ox = x; oy = y;
- }
- }
- }
- return *this;
- }
- //! Draw a 2D arrow.
- /**
- \param x0 X-coordinate of the starting arrow point (tail).
- \param y0 Y-coordinate of the starting arrow point (tail).
- \param x1 X-coordinate of the ending arrow point (head).
- \param y1 Y-coordinate of the ending arrow point (head).
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param angle Aperture angle of the arrow head.
- \param length Length of the arrow head. If negative, describes a percentage of the arrow length.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- **/
- template<typename tc>
- CImg<T>& draw_arrow(const int x0, const int y0,
- const int x1, const int y1,
- const tc *const color, const float opacity=1,
- const float angle=30, const float length=-10,
- const unsigned int pattern=~0U) {
- if (is_empty()) return *this;
- const float u = (float)(x0 - x1), v = (float)(y0 - y1), sq = u*u + v*v,
- deg = (float)(angle*cimg::PI/180), ang = (sq>0)?(float)std::atan2(v,u):0.f,
- l = (length>=0)?length:-length*(float)std::sqrt(sq)/100;
- if (sq>0) {
- const float
- cl = (float)std::cos(ang - deg), sl = (float)std::sin(ang - deg),
- cr = (float)std::cos(ang + deg), sr = (float)std::sin(ang + deg);
- const int
- xl = x1 + (int)(l*cl), yl = y1 + (int)(l*sl),
- xr = x1 + (int)(l*cr), yr = y1 + (int)(l*sr),
- xc = x1 + (int)((l + 1)*(cl + cr))/2, yc = y1 + (int)((l + 1)*(sl + sr))/2;
- draw_line(x0,y0,xc,yc,color,opacity,pattern).draw_triangle(x1,y1,xl,yl,xr,yr,color,opacity);
- } else draw_point(x0,y0,color,opacity);
- return *this;
- }
- //! Draw a 2D spline.
- /**
- \param x0 X-coordinate of the starting curve point
- \param y0 Y-coordinate of the starting curve point
- \param u0 X-coordinate of the starting velocity
- \param v0 Y-coordinate of the starting velocity
- \param x1 X-coordinate of the ending curve point
- \param y1 Y-coordinate of the ending curve point
- \param u1 X-coordinate of the ending velocity
- \param v1 Y-coordinate of the ending velocity
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param precision Curve drawing precision.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch If \c true, init hatch motif.
- \note
- - The curve is a 2D cubic Bezier spline, from the set of specified starting/ending points
- and corresponding velocity vectors.
- - The spline is drawn as a sequence of connected segments. The \p precision parameter sets the
- average number of pixels in each drawn segment.
- - A cubic Bezier curve is sometimes defined by a set of 4 points { (\p x0,\p y0), (\p xa,\p ya),
- (\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
- and (\p xa,\p ya), (\p xb,\p yb) are two
- \e control points.
- The starting and ending velocities (\p u0,\p v0) and (\p u1,\p v1) can be deduced easily from
- the control points as
- \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).
- \par Example:
- \code
- CImg<unsigned char> img(100,100,1,3,0);
- const unsigned char color[] = { 255,255,255 };
- img.draw_spline(30,30,0,100,90,40,0,-100,color);
- \endcode
- **/
- template<typename tc>
- CImg<T>& draw_spline(const int x0, const int y0, const float u0, const float v0,
- const int x1, const int y1, const float u1, const float v1,
- const tc *const color, const float opacity=1,
- const float precision=0.25, const unsigned int pattern=~0U,
- const bool init_hatch=true) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_spline(): Specified color is (null).",
- cimg_instance);
- if (x0==x1 && y0==y1) return draw_point(x0,y0,color,opacity);
- bool ninit_hatch = init_hatch;
- const float
- ax = u0 + u1 + 2*(x0 - x1),
- bx = 3*(x1 - x0) - 2*u0 - u1,
- ay = v0 + v1 + 2*(y0 - y1),
- by = 3*(y1 - y0) - 2*v0 - v1,
- _precision = 1/(cimg::hypot((float)x0 - x1,(float)y0 - y1)*(precision>0?precision:1));
- int ox = x0, oy = y0;
- for (float t = 0; t<1; t+=_precision) {
- const float t2 = t*t, t3 = t2*t;
- const int
- nx = (int)(ax*t3 + bx*t2 + u0*t + x0),
- ny = (int)(ay*t3 + by*t2 + v0*t + y0);
- draw_line(ox,oy,nx,ny,color,opacity,pattern,ninit_hatch);
- ninit_hatch = false;
- ox = nx; oy = ny;
- }
- return draw_line(ox,oy,x1,y1,color,opacity,pattern,false);
- }
- //! Draw a textured 2D spline.
- /**
- \param x0 X-coordinate of the starting curve point
- \param y0 Y-coordinate of the starting curve point
- \param u0 X-coordinate of the starting velocity
- \param v0 Y-coordinate of the starting velocity
- \param x1 X-coordinate of the ending curve point
- \param y1 Y-coordinate of the ending curve point
- \param u1 X-coordinate of the ending velocity
- \param v1 Y-coordinate of the ending velocity
- \param texture Texture image defining line pixel colors.
- \param tx0 X-coordinate of the starting texture point.
- \param ty0 Y-coordinate of the starting texture point.
- \param tx1 X-coordinate of the ending texture point.
- \param ty1 Y-coordinate of the ending texture point.
- \param precision Curve drawing precision.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch if \c true, reinit hatch motif.
- **/
- template<typename t>
- CImg<T>& draw_spline(const int x0, const int y0, const float u0, const float v0,
- const int x1, const int y1, const float u1, const float v1,
- const CImg<t>& texture,
- const int tx0, const int ty0, const int tx1, const int ty1,
- const float opacity=1,
- const float precision=4, const unsigned int pattern=~0U,
- const bool init_hatch=true) {
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_spline(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_empty()) return *this;
- if (is_overlapped(texture))
- return draw_spline(x0,y0,u0,v0,x1,y1,u1,v1,+texture,tx0,ty0,tx1,ty1,precision,opacity,pattern,init_hatch);
- if (x0==x1 && y0==y1)
- return draw_point(x0,y0,texture.get_vector_at(x0<=0?0:x0>=texture.width()?texture.width() - 1:x0,
- y0<=0?0:y0>=texture.height()?texture.height() - 1:y0).data(),
- opacity);
- bool ninit_hatch = init_hatch;
- const float
- ax = u0 + u1 + 2*(x0 - x1),
- bx = 3*(x1 - x0) - 2*u0 - u1,
- ay = v0 + v1 + 2*(y0 - y1),
- by = 3*(y1 - y0) - 2*v0 - v1,
- _precision = 1/(cimg::hypot((float)x0 - x1,(float)y0 - y1)*(precision>0?precision:1));
- int ox = x0, oy = y0, otx = tx0, oty = ty0;
- for (float t1 = 0; t1<1; t1+=_precision) {
- const float t2 = t1*t1, t3 = t2*t1;
- const int
- nx = (int)(ax*t3 + bx*t2 + u0*t1 + x0),
- ny = (int)(ay*t3 + by*t2 + v0*t1 + y0),
- ntx = tx0 + (int)((tx1 - tx0)*t1),
- nty = ty0 + (int)((ty1 - ty0)*t1);
- draw_line(ox,oy,nx,ny,texture,otx,oty,ntx,nty,opacity,pattern,ninit_hatch);
- ninit_hatch = false;
- ox = nx; oy = ny; otx = ntx; oty = nty;
- }
- return draw_line(ox,oy,x1,y1,texture,otx,oty,tx1,ty1,opacity,pattern,false);
- }
- //! Draw a set of consecutive splines.
- /**
- \param points Vertices data.
- \param tangents Tangents data.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param is_closed_set Tells if the drawn spline set is closed.
- \param precision Precision of the drawing.
- \param pattern An integer whose bits describe the line pattern.
- \param init_hatch If \c true, init hatch motif.
- **/
- template<typename tp, typename tt, typename tc>
- CImg<T>& draw_spline(const CImg<tp>& points, const CImg<tt>& tangents,
- const tc *const color, const float opacity=1,
- const bool is_closed_set=false, const float precision=4,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || !points || !tangents || points._width<2 || tangents._width<2) return *this;
- bool ninit_hatch = init_hatch;
- switch (points._height) {
- case 0 : case 1 :
- throw CImgArgumentException(_cimg_instance
- "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum,points._data);
- default : {
- const int x0 = (int)points(0,0), y0 = (int)points(0,1);
- const float u0 = (float)tangents(0,0), v0 = (float)tangents(0,1);
- int ox = x0, oy = y0;
- float ou = u0, ov = v0;
- for (unsigned int i = 1; i<points._width; ++i) {
- const int x = (int)points(i,0), y = (int)points(i,1);
- const float u = (float)tangents(i,0), v = (float)tangents(i,1);
- draw_spline(ox,oy,ou,ov,x,y,u,v,color,precision,opacity,pattern,ninit_hatch);
- ninit_hatch = false;
- ox = x; oy = y; ou = u; ov = v;
- }
- if (is_closed_set) draw_spline(ox,oy,ou,ov,x0,y0,u0,v0,color,precision,opacity,pattern,false);
- }
- }
- return *this;
- }
- //! Draw a set of consecutive splines \overloading.
- /**
- Similar to previous function, with the point tangents automatically estimated from the given points set.
- **/
- template<typename tp, typename tc>
- CImg<T>& draw_spline(const CImg<tp>& points,
- const tc *const color, const float opacity=1,
- const bool is_closed_set=false, const float precision=4,
- const unsigned int pattern=~0U, const bool init_hatch=true) {
- if (is_empty() || !points || points._width<2) return *this;
- CImg<Tfloat> tangents;
- switch (points._height) {
- case 0 : case 1 :
- throw CImgArgumentException(_cimg_instance
- "draw_spline(): Invalid specified point set (%u,%u,%u,%u,%p).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum,points._data);
- case 2 : {
- tangents.assign(points._width,points._height);
- cimg_forX(points,p) {
- const unsigned int
- p0 = is_closed_set?(p + points.width() - 1)%points.width():(p?p - 1:0),
- p1 = is_closed_set?(p + 1)%points.width():(p + 1<points.width()?p + 1:p);
- const float
- x = (float)points(p,0),
- y = (float)points(p,1),
- x0 = (float)points(p0,0),
- y0 = (float)points(p0,1),
- x1 = (float)points(p1,0),
- y1 = (float)points(p1,1),
- u0 = x - x0,
- v0 = y - y0,
- n0 = 1e-8f + cimg::hypot(u0,v0),
- u1 = x1 - x,
- v1 = y1 - y,
- n1 = 1e-8f + cimg::hypot(u1,v1),
- u = u0/n0 + u1/n1,
- v = v0/n0 + v1/n1,
- n = 1e-8f + cimg::hypot(u,v),
- fact = 0.5f*(n0 + n1);
- tangents(p,0) = (Tfloat)(fact*u/n);
- tangents(p,1) = (Tfloat)(fact*v/n);
- }
- } break;
- default : {
- tangents.assign(points._width,points._height);
- cimg_forX(points,p) {
- const unsigned int
- p0 = is_closed_set?(p + points.width() - 1)%points.width():(p?p - 1:0),
- p1 = is_closed_set?(p + 1)%points.width():(p + 1<points.width()?p + 1:p);
- const float
- x = (float)points(p,0),
- y = (float)points(p,1),
- z = (float)points(p,2),
- x0 = (float)points(p0,0),
- y0 = (float)points(p0,1),
- z0 = (float)points(p0,2),
- x1 = (float)points(p1,0),
- y1 = (float)points(p1,1),
- z1 = (float)points(p1,2),
- u0 = x - x0,
- v0 = y - y0,
- w0 = z - z0,
- n0 = 1e-8f + cimg::hypot(u0,v0,w0),
- u1 = x1 - x,
- v1 = y1 - y,
- w1 = z1 - z,
- n1 = 1e-8f + cimg::hypot(u1,v1,w1),
- u = u0/n0 + u1/n1,
- v = v0/n0 + v1/n1,
- w = w0/n0 + w1/n1,
- n = 1e-8f + cimg::hypot(u,v,w),
- fact = 0.5f*(n0 + n1);
- tangents(p,0) = (Tfloat)(fact*u/n);
- tangents(p,1) = (Tfloat)(fact*v/n);
- tangents(p,2) = (Tfloat)(fact*w/n);
- }
- }
- }
- return draw_spline(points,tangents,color,opacity,is_closed_set,precision,pattern,init_hatch);
- }
- // [internal] Draw a filled triangle.
- template<typename tc>
- CImg<T>& _draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const tc *const color, const float opacity,
- const float brightness) {
- if (y0>y1) cimg::swap(x0,x1,y0,y1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- h1 = height() - 1,
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
- const longT
- dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
- dy01 = std::max((longT)1,(longT)y1 - y0),
- dy02 = std::max((longT)1,(longT)y2 - y0),
- dy12 = std::max((longT)1,(longT)y2 - y1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float cbs = cimg::cut(brightness,0,2);
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
- longT
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- if (xm>xM) cimg::swap(xm,xM);
- cimg_draw_scanline(xm,xM,y,color,opacity,cbs);
- }
- return *this;
- }
- //! Draw a filled 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex.
- \param y0 Y-coordinate of the first vertex.
- \param x1 X-coordinate of the second vertex.
- \param y1 Y-coordinate of the second vertex.
- \param x2 X-coordinate of the third vertex.
- \param y2 Y-coordinate of the third vertex.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_triangle(const int x0, const int y0,
- const int x1, const int y1,
- const int x2, const int y2,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- _draw_triangle(x0,y0,x1,y1,x2,y2,color,opacity,1);
- return *this;
- }
- //! Draw a outlined 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex.
- \param y0 Y-coordinate of the first vertex.
- \param x1 X-coordinate of the second vertex.
- \param y1 Y-coordinate of the second vertex.
- \param x2 X-coordinate of the third vertex.
- \param y2 Y-coordinate of the third vertex.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the outline pattern.
- **/
- template<typename tc>
- CImg<T>& draw_triangle(const int x0, const int y0,
- const int x1, const int y1,
- const int x2, const int y2,
- const tc *const color, const float opacity,
- const unsigned int pattern) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- draw_line(x0,y0,x1,y1,color,opacity,pattern,true).
- draw_line(x1,y1,x2,y2,color,opacity,pattern,false).
- draw_line(x2,y2,x0,y0,color,opacity,pattern,false);
- return *this;
- }
- //! Draw a filled 2D triangle, with z-buffering.
- /**
- \param zbuffer Z-buffer image.
- \param x0 X-coordinate of the first vertex.
- \param y0 Y-coordinate of the first vertex.
- \param z0 Z-coordinate of the first vertex.
- \param x1 X-coordinate of the second vertex.
- \param y1 Y-coordinate of the second vertex.
- \param z1 Z-coordinate of the second vertex.
- \param x2 X-coordinate of the third vertex.
- \param y2 Y-coordinate of the third vertex.
- \param z2 Z-coordinate of the third vertex.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- \param brightness Brightness factor.
- **/
- template<typename tz, typename tc>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const tc *const color, const float opacity=1,
- const float brightness=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
- const longT
- dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
- dy01 = std::max((longT)1,(longT)y1 - y0),
- dy02 = std::max((longT)1,(longT)y2 - y0),
- dy12 = std::max((longT)1,(longT)y2 - y1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1;
- const float cbs = cimg::cut(brightness,0,2);
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
- longT
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,izm,izM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
- cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const longT dxmM = std::max((longT)1,xM - xm);
- const float dizmM = izM - izm;
- for (int x = cxm; x<=cxM; ++x) {
- const longT xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a Gouraud-shaded 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param bs0 Brightness factor of the first vertex (in [0,2]).
- \param bs1 brightness factor of the second vertex (in [0,2]).
- \param bs2 brightness factor of the third vertex (in [0,2]).
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const tc *const color,
- float bs0,
- float bs1,
- float bs2,
- const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,bs0,bs1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,bs0,bs2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,bs1,bs2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
- const longT
- dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
- dy01 = std::max((longT)1,(longT)y1 - y0),
- dy02 = std::max((longT)1,(longT)y2 - y0),
- dy12 = std::max((longT)1,(longT)y2 - y1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
- longT
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
- bsM = bs0 + dbs02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,bsm,bsM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
- cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
- T *ptrd = data(cxm,y);
- const longT dxmM = std::max((longT)1,xM - xm);
- const float dbsmM = bsM - bsm;
- for (int x = cxm; x<=cxM; ++x) {
- const longT xxm = (longT)x - xm;
- const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a Gouraud-shaded 2D triangle, with z-buffering \overloading.
- template<typename tz, typename tc>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const tc *const color,
- float bs0,
- float bs1,
- float bs2,
- float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,bs0,bs1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,bs0,bs2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,bs1,bs2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
- bsM = bs0 + dbs02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,izm,izM,bsm,bsM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float dizmM = izM - izm, dbsmM = bsM - bsm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c]*cbs:(2 - cbs)*color[c] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a color-interpolated 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param color1 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the first vertex.
- \param color2 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the second vertex.
- \param color3 Pointer to \c spectrum() consecutive values of type \c T, defining the color of the third vertex.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const tc *color0,
- const tc *color1,
- const tc *color2,
- const float opacity=1) {
- typedef typename cimg::superset<tc,int>::type stc;
- if (is_empty()) return *this;
- if (!color0 || !color1 || !color2)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): One of the specified color is (null).",
- cimg_instance);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,color0,color1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,color0,color2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,color1,color2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int w1 = width() - 1, h1 = height() - 1, cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1);
- const longT
- dx01 = (longT)x1 - x0, dx02 = (longT)x2 - x0, dx12 = (longT)x2 - x1,
- dy01 = std::max((longT)1,(longT)y1 - y0),
- dy02 = std::max((longT)1,(longT)y2 - y0),
- dy12 = std::max((longT)1,(longT)y2 - y1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- cimg_init_scanline(opacity);
- cimg_forC(*this,c) {
- const stc dcolor01 = color1[c] - color0[c], dcolor02 = color2[c] - color0[c], dcolor12 = color2[c] - color1[c];
- for (int y = cy0; y<=cy2; ++y) {
- const longT yy0 = (longT)y - y0, yy1 = (longT)y - y1;
- longT
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- stc
- colorm = y<y1?(color0[c] + dcolor01*yy0/dy01):(color1[c] + dcolor12*yy1/dy12),
- colorM = color0[c] + dcolor02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,colorm,colorM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = (int)cimg::cut(xm,(longT)0,(longT)w1),
- cxM = (int)cimg::cut(xM,(longT)0,(longT)w1);
- T *ptrd = data(cxm,y);
- const longT dxmM = std::max((longT)1,xM - xm);
- const stc dcolormM = colorM - colorm;
- for (int x = cxm; x<=cxM; ++x) {
- const longT xxm = (longT)x - xm;
- const stc col = colorm + dcolormM*xxm/dxmM;
- ptrd[c*_sc_whd] = (T)(opacity>=1?col:col*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- ++ptrd;
- }
- }
- }
- }
- return *this;
- }
- //! Draw a textured 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param texture Texture image used to fill the triangle.
- \param tx0 X-coordinate of the first vertex in the texture image.
- \param ty0 Y-coordinate of the first vertex in the texture image.
- \param tx1 X-coordinate of the second vertex in the texture image.
- \param ty1 Y-coordinate of the second vertex in the texture image.
- \param tx2 X-coordinate of the third vertex in the texture image.
- \param ty2 Y-coordinate of the third vertex in the texture image.
- \param opacity Drawing opacity.
- \param brightness Brightness factor of the drawing (in [0,2]).
- **/
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const float opacity=1,
- const float brightness=1) {
- if (is_empty()) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,ty1,tx2,ty2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
- dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
- dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
- hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
- hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- const float cbs = cimg::cut(brightness,0,2);
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02,
- txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
- txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
- tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
- tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02;
- if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int
- dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
- dtxmM = txM - txm, dtymM = tyM - tym;
- for (int x = cxm; x<=cxM; ++x) {
- const int
- xxm = x - xm,
- tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
- ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM;
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a 2D textured triangle, with perspective correction.
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const float opacity=1,
- const float brightness=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- const float cbs = cimg::cut(brightness,0,2);
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float
- iz = izm + dizmM*xxm/dxmM,
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM;
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a textured 2D triangle, with perspective correction and z-buffering.
- template<typename tz, typename tc>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const float opacity=1,
- const float brightness=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,opacity,brightness);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- const float cbs = cimg::cut(brightness,0,2);
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- const float
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM;
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const Tfloat val = cbs<=1?color[c*twhd]*cbs:(2 - cbs)*color[c*twhd] + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a Phong-shaded 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param light Light image.
- \param lx0 X-coordinate of the first vertex in the light image.
- \param ly0 Y-coordinate of the first vertex in the light image.
- \param lx1 X-coordinate of the second vertex in the light image.
- \param ly1 Y-coordinate of the second vertex in the light image.
- \param lx2 X-coordinate of the third vertex in the light image.
- \param ly2 Y-coordinate of the third vertex in the light image.
- \param opacity Drawing opacity.
- **/
- template<typename tc, typename tl>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const tc *const color,
- const CImg<tl>& light,
- int lx0, int ly0,
- int lx1, int ly1,
- int lx2, int ly2,
- const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- if (light._depth>1 || light._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
- cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,lx0,lx1,ly0,ly1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,lx0,lx2,ly0,ly2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,lx1,lx2,ly1,ly2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
- dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
- dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
- hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
- hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
- const ulongT lwhd = (ulongT)light._width*light._height*light._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02,
- lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
- lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
- lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
- lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
- if (xm>xM) cimg::swap(xm,xM,lxm,lxM,lym,lyM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int
- dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
- dlxmM = lxM - lxm, dlymM = lyM - lym;
- for (int x = cxm; x<=cxM; ++x) {
- const int
- xxm = x - xm,
- lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
- ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
- const tl *const lig = &light._atXY(lx,ly);
- cimg_forC(*this,c) {
- const tc col = color[c];
- const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a Phong-shaded 2D triangle, with z-buffering.
- template<typename tz, typename tc, typename tl>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const tc *const color,
- const CImg<tl>& light,
- int lx0, int ly0,
- int lx1, int ly1,
- int lx2, int ly2,
- const float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Specified color is (null).",
- cimg_instance);
- if (light._depth>1 || light._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
- cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (is_overlapped(light)) return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,
- +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,lx0,lx1,ly0,ly1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,lx0,lx2,ly0,ly2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,lx1,lx2,ly1,ly2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
- dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
- dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
- hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
- hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
- const float diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1;
- const ulongT lwhd = (ulongT)light._width*light._height*light._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02,
- lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
- lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
- lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
- lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,lxm,lxM,lym,lyM,izm,izM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const int
- dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
- dlxmM = lxM - lxm, dlymM = lyM - lym;
- const float dizmM = izM - izm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- const int
- lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
- ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
- const tl *const lig = &light._atXY(lx,ly);
- cimg_forC(*this,c) {
- const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
- const tc col = color[c];
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Gouraud-shaded 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param texture Texture image used to fill the triangle.
- \param tx0 X-coordinate of the first vertex in the texture image.
- \param ty0 Y-coordinate of the first vertex in the texture image.
- \param tx1 X-coordinate of the second vertex in the texture image.
- \param ty1 Y-coordinate of the second vertex in the texture image.
- \param tx2 X-coordinate of the third vertex in the texture image.
- \param ty2 Y-coordinate of the third vertex in the texture image.
- \param bs0 Brightness factor of the first vertex.
- \param bs1 Brightness factor of the second vertex.
- \param bs2 Brightness factor of the third vertex.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- float bs0,
- float bs1,
- float bs2,
- const float opacity=1) {
- if (is_empty()) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
- bs0,bs1,bs2,opacity);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1,bs0,bs1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2,bs0,bs2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,tx2,ty1,ty2,bs1,bs2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
- dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
- dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
- hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
- hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2;
- const float dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02,
- txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
- txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
- tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
- tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02;
- float
- bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
- bsM = bs0 + dbs02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM,bsm,bsM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int
- dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
- dtxmM = txM - txm, dtymM = tyM - tym;
- const float dbsmM = bsM - bsm;
- for (int x = cxm; x<=cxM; ++x) {
- const int
- xxm = x - xm,
- tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
- ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM;
- const float cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Gouraud-shaded 2D triangle, with perspective correction \overloading.
- template<typename tc>
- CImg<T>& draw_triangle(int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- float bs0,
- float bs1,
- float bs2,
- const float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture)) return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
- bs0,bs1,bs2,opacity);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,bs0,bs1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,bs0,bs2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,bs1,bs2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
- dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02,
- bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
- bsM = bs0 + dbs02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM,bsm,bsM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm, dbsmM = bsM - bsm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float
- iz = izm + dizmM*xxm/dxmM,
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM,
- cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Gouraud-shaded 2D triangle, with perspective correction and z-buffering \overloading.
- template<typename tz, typename tc>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- float bs0,
- float bs1,
- float bs2,
- const float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (is_overlapped(texture))
- return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,bs0,bs1,bs2,opacity);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,bs0,bs1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,bs0,bs2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,bs1,bs2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
- dbs01 = bs1 - bs0, dbs02 = bs2 - bs0, dbs12 = bs2 - bs1;
- const ulongT twhd = (ulongT)texture._width*texture._height*texture._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02,
- bsm = y<y1?(bs0 + dbs01*yy0/dy01):(bs1 + dbs12*yy1/dy12),
- bsM = bs0 + dbs02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,txzm,txzM,tyzm,tyzM,izm,izM,bsm,bsM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float dizmM = izM - izm, dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm, dbsmM = bsM - bsm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- const float
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM,
- cbs = cimg::cut(bsm + dbsmM*xxm/dxmM,0,2);
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Phong-shaded 2D triangle.
- /**
- \param x0 X-coordinate of the first vertex in the image instance.
- \param y0 Y-coordinate of the first vertex in the image instance.
- \param x1 X-coordinate of the second vertex in the image instance.
- \param y1 Y-coordinate of the second vertex in the image instance.
- \param x2 X-coordinate of the third vertex in the image instance.
- \param y2 Y-coordinate of the third vertex in the image instance.
- \param texture Texture image used to fill the triangle.
- \param tx0 X-coordinate of the first vertex in the texture image.
- \param ty0 Y-coordinate of the first vertex in the texture image.
- \param tx1 X-coordinate of the second vertex in the texture image.
- \param ty1 Y-coordinate of the second vertex in the texture image.
- \param tx2 X-coordinate of the third vertex in the texture image.
- \param ty2 Y-coordinate of the third vertex in the texture image.
- \param light Light image.
- \param lx0 X-coordinate of the first vertex in the light image.
- \param ly0 Y-coordinate of the first vertex in the light image.
- \param lx1 X-coordinate of the second vertex in the light image.
- \param ly1 Y-coordinate of the second vertex in the light image.
- \param lx2 X-coordinate of the third vertex in the light image.
- \param ly2 Y-coordinate of the third vertex in the light image.
- \param opacity Drawing opacity.
- **/
- template<typename tc, typename tl>
- CImg<T>& draw_triangle(int x0, int y0,
- int x1, int y1,
- int x2, int y2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const CImg<tl>& light,
- int lx0, int ly0,
- int lx1, int ly1,
- int lx2, int ly2,
- const float opacity=1) {
- if (is_empty()) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (light._depth>1 || light._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
- cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
- if (is_overlapped(texture))
- return draw_triangle(x0,y0,x1,y1,x2,y2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- if (is_overlapped(light))
- return draw_triangle(x0,y0,x1,y1,x2,y2,texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- if (y0>y1) cimg::swap(x0,x1,y0,y1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2,
- dtx01 = tx1 - tx0, dtx02 = tx2 - tx0, dtx12 = tx2 - tx1,
- dty01 = ty1 - ty0, dty02 = ty2 - ty0, dty12 = ty2 - ty1,
- hdy01tx = dy01*cimg::sign(dtx01)/2, hdy02tx = dy02*cimg::sign(dtx02)/2, hdy12tx = dy12*cimg::sign(dtx12)/2,
- hdy01ty = dy01*cimg::sign(dty01)/2, hdy02ty = dy02*cimg::sign(dty02)/2, hdy12ty = dy12*cimg::sign(dty12)/2,
- dlx01 = lx1 - lx0, dlx02 = lx2 - lx0, dlx12 = lx2 - lx1,
- dly01 = ly1 - ly0, dly02 = ly2 - ly0, dly12 = ly2 - ly1,
- hdy01lx = dy01*cimg::sign(dlx01)/2, hdy02lx = dy02*cimg::sign(dlx02)/2, hdy12lx = dy12*cimg::sign(dlx12)/2,
- hdy01ly = dy01*cimg::sign(dly01)/2, hdy02ly = dy02*cimg::sign(dly02)/2, hdy12ly = dy12*cimg::sign(dly12)/2;
- const ulongT
- twhd = (ulongT)texture._width*texture._height*texture._depth,
- lwhd = (ulongT)light._width*light._height*light._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02,
- txm = y<y1?tx0 + (dtx01*yy0 + hdy01tx)/dy01:tx1 + (dtx12*yy1 + hdy12tx)/dy12,
- txM = tx0 + (dtx02*yy0 + hdy02tx)/dy02,
- tym = y<y1?ty0 + (dty01*yy0 + hdy01ty)/dy01:ty1 + (dty12*yy1 + hdy12ty)/dy12,
- tyM = ty0 + (dty02*yy0 + hdy02ty)/dy02,
- lxm = y<y1?lx0 + (dlx01*yy0 + hdy01lx)/dy01:lx1 + (dlx12*yy1 + hdy12lx)/dy12,
- lxM = lx0 + (dlx02*yy0 + hdy02lx)/dy02,
- lym = y<y1?ly0 + (dly01*yy0 + hdy01ly)/dy01:ly1 + (dly12*yy1 + hdy12ly)/dy12,
- lyM = ly0 + (dly02*yy0 + hdy02ly)/dy02;
- if (xm>xM) cimg::swap(xm,xM,txm,txM,tym,tyM,lxm,lxM,lym,lyM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int
- dxmM = std::max(1,xM - xm), hdxmM = dxmM/2,
- dtxmM = txM - txm, dtymM = tyM - tym,
- dlxmM = lxM - lxm, dlymM = lyM - lym;
- for (int x = cxm; x<=cxM; ++x) {
- const int
- xxm = x - xm,
- tx = (txm*dxmM + dtxmM*xxm + hdxmM)/dxmM,
- ty = (tym*dxmM + dtymM*xxm + hdxmM)/dxmM,
- lx = (lxm*dxmM + dlxmM*xxm + hdxmM)/dxmM,
- ly = (lym*dxmM + dlymM*xxm + hdxmM)/dxmM;
- const tc *const color = &texture._atXY(tx,ty);
- const tl *const lig = &light._atXY(lx,ly);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Phong-shaded 2D triangle, with perspective correction.
- template<typename tc, typename tl>
- CImg<T>& draw_triangle(int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const CImg<tl>& light,
- int lx0, int ly0,
- int lx1, int ly1,
- int lx2, int ly2,
- const float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (light._depth>1 || light._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
- cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
- if (is_overlapped(texture))
- return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,+texture,tx0,ty0,tx1,ty1,tx2,ty2,
- light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- if (is_overlapped(light))
- return draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,texture,tx0,ty0,tx1,ty1,tx2,ty2,
- +light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
- lxz0 = lx0*iz0, lxz1 = lx1*iz1, lxz2 = lx2*iz2,
- lyz0 = ly0*iz0, lyz1 = ly1*iz1, lyz2 = ly2*iz2,
- dlxz01 = lxz1 - lxz0, dlxz02 = lxz2 - lxz0, dlxz12 = lxz2 - lxz1,
- dlyz01 = lyz1 - lyz0, dlyz02 = lyz2 - lyz0, dlyz12 = lyz2 - lyz1;
- const ulongT
- twhd = (ulongT)texture._width*texture._height*texture._depth,
- lwhd = (ulongT)light._width*light._height*light._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02,
- lxzm = y<y1?(lxz0 + dlxz01*yy0/dy01):(lxz1 + dlxz12*yy1/dy12),
- lxzM = lxz0 + dlxz02*yy0/dy02,
- lyzm = y<y1?(lyz0 + dlyz01*yy0/dy01):(lyz1 + dlyz12*yy1/dy12),
- lyzM = lyz0 + dlyz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,izm,izM,txzm,txzM,tyzm,tyzM,lxzm,lxzM,lyzm,lyzM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float
- dizmM = izM - izm,
- dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm,
- dlxzmM = lxzM - lxzm, dlyzmM = lyzM - lyzm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float
- iz = izm + dizmM*xxm/dxmM,
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM,
- lxz = lxzm + dlxzmM*xxm/dxmM,
- lyz = lyzm + dlyzmM*xxm/dxmM;
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz),
- lx = (int)cimg::round(lxz/iz),
- ly = (int)cimg::round(lyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- const tl *const lig = &light._atXY(lx,ly);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a textured Phong-shaded 2D triangle, with perspective correction and z-buffering.
- template<typename tz, typename tc, typename tl>
- CImg<T>& draw_triangle(CImg<tz>& zbuffer,
- int x0, int y0, const float z0,
- int x1, int y1, const float z1,
- int x2, int y2, const float z2,
- const CImg<tc>& texture,
- int tx0, int ty0,
- int tx1, int ty1,
- int tx2, int ty2,
- const CImg<tl>& light,
- int lx0, int ly0,
- int lx1, int ly1,
- int lx2, int ly2,
- const float opacity=1) {
- if (is_empty() || z0<=0 || z1<=0 || z2<=0) return *this;
- if (!is_sameXY(zbuffer))
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Instance and specified Z-buffer (%u,%u,%u,%u,%p) have "
- "different dimensions.",
- cimg_instance,
- zbuffer._width,zbuffer._height,zbuffer._depth,zbuffer._spectrum,zbuffer._data);
- if (texture._depth>1 || texture._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified texture (%u,%u,%u,%u,%p).",
- cimg_instance,
- texture._width,texture._height,texture._depth,texture._spectrum,texture._data);
- if (light._depth>1 || light._spectrum<_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_triangle(): Invalid specified light texture (%u,%u,%u,%u,%p).",
- cimg_instance,light._width,light._height,light._depth,light._spectrum,light._data);
- if (is_overlapped(texture))
- return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,
- +texture,tx0,ty0,tx1,ty1,tx2,ty2,light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- if (is_overlapped(light))
- return draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,
- texture,tx0,ty0,tx1,ty1,tx2,ty2,+light,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- float iz0 = 1/z0, iz1 = 1/z1, iz2 = 1/z2;
- if (y0>y1) cimg::swap(x0,x1,y0,y1,iz0,iz1,tx0,tx1,ty0,ty1,lx0,lx1,ly0,ly1);
- if (y0>y2) cimg::swap(x0,x2,y0,y2,iz0,iz2,tx0,tx2,ty0,ty2,lx0,lx2,ly0,ly2);
- if (y1>y2) cimg::swap(x1,x2,y1,y2,iz1,iz2,tx1,tx2,ty1,ty2,lx1,lx2,ly1,ly2);
- if (y2<0 || y0>=height() || cimg::min(x0,x1,x2)>=width() || cimg::max(x0,x1,x2)<0 || !opacity) return *this;
- const int
- w1 = width() - 1, h1 = height() - 1,
- dx01 = x1 - x0, dx02 = x2 - x0, dx12 = x2 - x1,
- dy01 = std::max(1,y1 - y0), dy02 = std::max(1,y2 - y0), dy12 = std::max(1,y2 - y1),
- cy0 = cimg::cut(y0,0,h1), cy2 = cimg::cut(y2,0,h1),
- hdy01 = dy01*cimg::sign(dx01)/2, hdy02 = dy02*cimg::sign(dx02)/2, hdy12 = dy12*cimg::sign(dx12)/2;
- const float
- diz01 = iz1 - iz0, diz02 = iz2 - iz0, diz12 = iz2 - iz1,
- txz0 = tx0*iz0, txz1 = tx1*iz1, txz2 = tx2*iz2,
- tyz0 = ty0*iz0, tyz1 = ty1*iz1, tyz2 = ty2*iz2,
- dtxz01 = txz1 - txz0, dtxz02 = txz2 - txz0, dtxz12 = txz2 - txz1,
- dtyz01 = tyz1 - tyz0, dtyz02 = tyz2 - tyz0, dtyz12 = tyz2 - tyz1,
- lxz0 = lx0*iz0, lxz1 = lx1*iz1, lxz2 = lx2*iz2,
- lyz0 = ly0*iz0, lyz1 = ly1*iz1, lyz2 = ly2*iz2,
- dlxz01 = lxz1 - lxz0, dlxz02 = lxz2 - lxz0, dlxz12 = lxz2 - lxz1,
- dlyz01 = lyz1 - lyz0, dlyz02 = lyz2 - lyz0, dlyz12 = lyz2 - lyz1;
- const ulongT
- twhd = (ulongT)texture._width*texture._height*texture._depth,
- lwhd = (ulongT)light._width*light._height*light._depth;
- cimg_init_scanline(opacity);
- for (int y = cy0; y<=cy2; ++y) {
- const int yy0 = y - y0, yy1 = y - y1;
- int
- xm = y<y1?x0 + (dx01*yy0 + hdy01)/dy01:x1 + (dx12*yy1 + hdy12)/dy12,
- xM = x0 + (dx02*yy0 + hdy02)/dy02;
- float
- izm = y<y1?(iz0 + diz01*yy0/dy01):(iz1 + diz12*yy1/dy12),
- izM = iz0 + diz02*yy0/dy02,
- txzm = y<y1?(txz0 + dtxz01*yy0/dy01):(txz1 + dtxz12*yy1/dy12),
- txzM = txz0 + dtxz02*yy0/dy02,
- tyzm = y<y1?(tyz0 + dtyz01*yy0/dy01):(tyz1 + dtyz12*yy1/dy12),
- tyzM = tyz0 + dtyz02*yy0/dy02,
- lxzm = y<y1?(lxz0 + dlxz01*yy0/dy01):(lxz1 + dlxz12*yy1/dy12),
- lxzM = lxz0 + dlxz02*yy0/dy02,
- lyzm = y<y1?(lyz0 + dlyz01*yy0/dy01):(lyz1 + dlyz12*yy1/dy12),
- lyzM = lyz0 + dlyz02*yy0/dy02;
- if (xm>xM) cimg::swap(xm,xM,izm,izM,txzm,txzM,tyzm,tyzM,lxzm,lxzM,lyzm,lyzM);
- if (xM>=0 && xm<=w1) {
- const int
- cxm = cimg::cut(xm,0,w1),
- cxM = cimg::cut(xM,0,w1);
- T *ptrd = data(cxm,y);
- tz *ptrz = zbuffer.data(cxm,y);
- const int dxmM = std::max(1,xM - xm);
- const float
- dizmM = izM - izm,
- dtxzmM = txzM - txzm, dtyzmM = tyzM - tyzm,
- dlxzmM = lxzM - lxzm, dlyzmM = lyzM - lyzm;
- for (int x = cxm; x<=cxM; ++x) {
- const int xxm = x - xm;
- const float iz = izm + dizmM*xxm/dxmM;
- if (iz>=*ptrz) {
- *ptrz = (tz)iz;
- const float
- txz = txzm + dtxzmM*xxm/dxmM,
- tyz = tyzm + dtyzmM*xxm/dxmM,
- lxz = lxzm + dlxzmM*xxm/dxmM,
- lyz = lyzm + dlyzmM*xxm/dxmM;
- const int
- tx = (int)cimg::round(txz/iz),
- ty = (int)cimg::round(tyz/iz),
- lx = (int)cimg::round(lxz/iz),
- ly = (int)cimg::round(lyz/iz);
- const tc *const color = &texture._atXY(tx,ty);
- const tl *const lig = &light._atXY(lx,ly);
- cimg_forC(*this,c) {
- const tc col = color[c*twhd];
- const float cbs = cimg::cut((float)lig[c*lwhd],0,2);
- const Tfloat val = cbs<=1?cbs*col:(2 - cbs)*col + (cbs - 1)*_sc_maxval;
- ptrd[c*_sc_whd] = (T)(opacity>=1?val:val*_sc_nopacity + ptrd[c*_sc_whd]*_sc_copacity);
- }
- }
- ++ptrd; ++ptrz;
- }
- }
- }
- return *this;
- }
- //! Draw a filled 4D rectangle.
- /**
- \param x0 X-coordinate of the upper-left rectangle corner.
- \param y0 Y-coordinate of the upper-left rectangle corner.
- \param z0 Z-coordinate of the upper-left rectangle corner.
- \param c0 C-coordinate of the upper-left rectangle corner.
- \param x1 X-coordinate of the lower-right rectangle corner.
- \param y1 Y-coordinate of the lower-right rectangle corner.
- \param z1 Z-coordinate of the lower-right rectangle corner.
- \param c1 C-coordinate of the lower-right rectangle corner.
- \param val Scalar value used to fill the rectangle area.
- \param opacity Drawing opacity.
- **/
- CImg<T>& draw_rectangle(const int x0, const int y0, const int z0, const int c0,
- const int x1, const int y1, const int z1, const int c1,
- const T val, const float opacity=1) {
- if (is_empty()) return *this;
- const int
- nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
- ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0,
- nz0 = z0<z1?z0:z1, nz1 = z0^z1^nz0,
- nc0 = c0<c1?c0:c1, nc1 = c0^c1^nc0;
- const int
- lx = (1 + nx1 - nx0) + (nx1>=width()?width() - 1 - nx1:0) + (nx0<0?nx0:0),
- ly = (1 + ny1 - ny0) + (ny1>=height()?height() - 1 - ny1:0) + (ny0<0?ny0:0),
- lz = (1 + nz1 - nz0) + (nz1>=depth()?depth() - 1 - nz1:0) + (nz0<0?nz0:0),
- lc = (1 + nc1 - nc0) + (nc1>=spectrum()?spectrum() - 1 - nc1:0) + (nc0<0?nc0:0);
- const ulongT
- offX = (ulongT)_width - lx,
- offY = (ulongT)_width*(_height - ly),
- offZ = (ulongT)_width*_height*(_depth - lz);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- T *ptrd = data(nx0<0?0:nx0,ny0<0?0:ny0,nz0<0?0:nz0,nc0<0?0:nc0);
- if (lx>0 && ly>0 && lz>0 && lc>0)
- for (int v = 0; v<lc; ++v) {
- for (int z = 0; z<lz; ++z) {
- for (int y = 0; y<ly; ++y) {
- if (opacity>=1) {
- if (sizeof(T)!=1) { for (int x = 0; x<lx; ++x) *(ptrd++) = val; ptrd+=offX; }
- else { std::memset(ptrd,(int)val,lx); ptrd+=_width; }
- } else { for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*val + *ptrd*copacity); ++ptrd; } ptrd+=offX; }
- }
- ptrd+=offY;
- }
- ptrd+=offZ;
- }
- return *this;
- }
- //! Draw a filled 3D rectangle.
- /**
- \param x0 X-coordinate of the upper-left rectangle corner.
- \param y0 Y-coordinate of the upper-left rectangle corner.
- \param z0 Z-coordinate of the upper-left rectangle corner.
- \param x1 X-coordinate of the lower-right rectangle corner.
- \param y1 Y-coordinate of the lower-right rectangle corner.
- \param z1 Z-coordinate of the lower-right rectangle corner.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_rectangle(const int x0, const int y0, const int z0,
- const int x1, const int y1, const int z1,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_rectangle(): Specified color is (null).",
- cimg_instance);
- cimg_forC(*this,c) draw_rectangle(x0,y0,z0,c,x1,y1,z1,c,(T)color[c],opacity);
- return *this;
- }
- //! Draw a filled 2D rectangle.
- /**
- \param x0 X-coordinate of the upper-left rectangle corner.
- \param y0 Y-coordinate of the upper-left rectangle corner.
- \param x1 X-coordinate of the lower-right rectangle corner.
- \param y1 Y-coordinate of the lower-right rectangle corner.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_rectangle(const int x0, const int y0,
- const int x1, const int y1,
- const tc *const color, const float opacity=1) {
- return draw_rectangle(x0,y0,0,x1,y1,_depth - 1,color,opacity);
- }
- //! Draw a outlined 2D rectangle \overloading.
- template<typename tc>
- CImg<T>& draw_rectangle(const int x0, const int y0,
- const int x1, const int y1,
- const tc *const color, const float opacity,
- const unsigned int pattern) {
- if (is_empty()) return *this;
- if (y0==y1) return draw_line(x0,y0,x1,y0,color,opacity,pattern,true);
- if (x0==x1) return draw_line(x0,y0,x0,y1,color,opacity,pattern,true);
- const int
- nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
- ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0;
- if (ny1==ny0 + 1) return draw_line(nx0,ny0,nx1,ny0,color,opacity,pattern,true).
- draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false);
- return draw_line(nx0,ny0,nx1,ny0,color,opacity,pattern,true).
- draw_line(nx1,ny0 + 1,nx1,ny1 - 1,color,opacity,pattern,false).
- draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false).
- draw_line(nx0,ny1 - 1,nx0,ny0 + 1,color,opacity,pattern,false);
- }
- //! Draw a filled 2D polygon.
- /**
- \param points Set of polygon vertices.
- \param color Pointer to \c spectrum() consecutive values of type \c T, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tp, typename tc>
- CImg<T>& draw_polygon(const CImg<tp>& points,
- const tc *const color, const float opacity=1) {
- if (is_empty() || !points) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_polygon(): Specified color is (null).",
- cimg_instance);
- if (points.height()!=2)
- throw CImgArgumentException(_cimg_instance
- "draw_polygon(): Invalid specified point set (%u,%u,%u,%u).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum);
- if (points._width==1) return draw_point(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),color,opacity);
- if (points._width==2) return draw_line(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),
- cimg::uiround(points(1,0)),cimg::uiround(points(1,1)),color,opacity);
- if (points._width==3) return draw_triangle(cimg::uiround(points(0,0)),cimg::uiround(points(0,1)),
- cimg::uiround(points(1,0)),cimg::uiround(points(1,1)),
- cimg::uiround(points(2,0)),cimg::uiround(points(2,1)),color,opacity);
- cimg_init_scanline(opacity);
- int
- xmin = 0, ymin = 0,
- xmax = points.get_shared_row(0).max_min(xmin),
- ymax = points.get_shared_row(1).max_min(ymin);
- if (xmax<0 || xmin>=width() || ymax<0 || ymin>=height()) return *this;
- if (ymin==ymax) return draw_line(xmin,ymin,xmax,ymax,color,opacity);
- ymin = std::max(0,ymin);
- ymax = std::min(height() - 1,ymax);
- CImg<intT> Xs(points._width,ymax - ymin + 1);
- CImg<uintT> count(Xs._height,1,1,1,0);
- unsigned int n = 0, nn = 1;
- bool go_on = true;
- while (go_on) {
- unsigned int an = (nn + 1)%points._width;
- const int
- x0 = cimg::uiround(points(n,0)),
- y0 = cimg::uiround(points(n,1));
- if (points(nn,1)==y0) while (points(an,1)==y0) { nn = an; (an+=1)%=points._width; }
- const int
- x1 = cimg::uiround(points(nn,0)),
- y1 = cimg::uiround(points(nn,1));
- unsigned int tn = an;
- while (points(tn,1)==y1) (tn+=1)%=points._width;
- if (y0!=y1) {
- const int
- y2 = cimg::uiround(points(tn,1)),
- x01 = x1 - x0, y01 = y1 - y0, y12 = y2 - y1,
- step = cimg::sign(y01),
- tmax = std::max(1,cimg::abs(y01)), htmax = tmax*cimg::sign(x01)/2,
- tend = tmax - (step==cimg::sign(y12));
- unsigned int y = (unsigned int)y0 - ymin;
- for (int t = 0; t<=tend; ++t, y+=step)
- if (y<Xs._height) Xs(count[y]++,y) = x0 + (t*x01 + htmax)/tmax;
- }
- go_on = nn>n;
- n = nn;
- nn = an;
- }
- cimg_pragma_openmp(parallel for cimg_openmp_if(Xs._height>=(cimg_openmp_sizefactor)*512))
- cimg_forY(Xs,y) {
- const CImg<intT> Xsy = Xs.get_shared_points(0,count[y] - 1,y).sort();
- int px = width();
- for (unsigned int k = 0; k<Xsy._width; k+=2) {
- int x0 = Xsy[k];
- const int x1 = Xsy[k + 1];
- x0+=x0==px;
- cimg_draw_scanline(x0,x1,y + ymin,color,opacity,1);
- px = x1;
- }
- }
- return *this;
- }
- //! Draw a outlined 2D or 3D polygon \overloading.
- template<typename t, typename tc>
- CImg<T>& draw_polygon(const CImg<t>& points,
- const tc *const color, const float opacity, const unsigned int pattern) {
- if (is_empty() || !points) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_polygon(): Specified color is (null).",
- cimg_instance);
- if (points._width==1) return draw_point((int)points(0,0),(int)points(0,1),color,opacity);
- if (points._width==2) return draw_line((int)points(0,0),(int)points(0,1),
- (int)points(1,0),(int)points(1,1),color,opacity,pattern);
- bool ninit_hatch = true;
- switch (points._height) {
- case 0 : case 1 :
- throw CImgArgumentException(_cimg_instance
- "draw_polygon(): Invalid specified point set (%u,%u,%u,%u).",
- cimg_instance,
- points._width,points._height,points._depth,points._spectrum);
- default : {
- CImg<intT> npoints(points._width,2);
- int x = npoints(0,0) = (int)points(0,0), y = npoints(0,1) = (int)points(0,1);
- unsigned int nb_points = 1;
- for (unsigned int p = 1; p<points._width; ++p) {
- const int nx = (int)points(p,0), ny = (int)points(p,1);
- if (nx!=x || ny!=y) { npoints(nb_points,0) = nx; npoints(nb_points++,1) = ny; x = nx; y = ny; }
- }
- const int x0 = (int)npoints(0,0), y0 = (int)npoints(0,1);
- int ox = x0, oy = y0;
- for (unsigned int i = 1; i<nb_points; ++i) {
- const int _x = (int)npoints(i,0), _y = (int)npoints(i,1);
- draw_line(ox,oy,_x,_y,color,opacity,pattern,ninit_hatch);
- ninit_hatch = false;
- ox = _x; oy = _y;
- }
- draw_line(ox,oy,x0,y0,color,opacity,pattern,false);
- }
- }
- return *this;
- }
- //! Draw a filled 2D ellipse.
- /**
- \param x0 X-coordinate of the ellipse center.
- \param y0 Y-coordinate of the ellipse center.
- \param r1 First radius of the ellipse.
- \param r2 Second radius of the ellipse.
- \param angle Angle of the first radius.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle,
- const tc *const color, const float opacity=1) {
- return _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,0U,true);
- }
- //! Draw a filled 2D ellipse \overloading.
- /**
- \param x0 X-coordinate of the ellipse center.
- \param y0 Y-coordinate of the ellipse center.
- \param tensor Diffusion tensor describing the ellipse.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_ellipse(const int x0, const int y0, const CImg<t> &tensor,
- const tc *const color, const float opacity=1) {
- CImgList<t> eig = tensor.get_symmetric_eigen();
- const CImg<t> &val = eig[0], &vec = eig[1];
- return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)),
- std::atan2(vec(0,1),vec(0,0))*180/cimg::PI,
- color,opacity);
- }
- //! Draw an outlined 2D ellipse.
- /**
- \param x0 X-coordinate of the ellipse center.
- \param y0 Y-coordinate of the ellipse center.
- \param r1 First radius of the ellipse.
- \param r2 Second radius of the ellipse.
- \param angle Angle of the first radius.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the outline pattern.
- **/
- template<typename tc>
- CImg<T>& draw_ellipse(const int x0, const int y0, const float r1, const float r2, const float angle,
- const tc *const color, const float opacity, const unsigned int pattern) {
- if (pattern) _draw_ellipse(x0,y0,r1,r2,angle,color,opacity,pattern,false);
- return *this;
- }
- //! Draw an outlined 2D ellipse \overloading.
- /**
- \param x0 X-coordinate of the ellipse center.
- \param y0 Y-coordinate of the ellipse center.
- \param tensor Diffusion tensor describing the ellipse.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the outline pattern.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_ellipse(const int x0, const int y0, const CImg<t> &tensor,
- const tc *const color, const float opacity,
- const unsigned int pattern) {
- CImgList<t> eig = tensor.get_symmetric_eigen();
- const CImg<t> &val = eig[0], &vec = eig[1];
- return draw_ellipse(x0,y0,std::sqrt(val(0)),std::sqrt(val(1)),
- std::atan2(vec(0,1),vec(0,0))*180/cimg::PI,
- color,opacity,pattern);
- }
- template<typename tc>
- CImg<T>& _draw_ellipse(const int x0, const int y0, const float radius1, const float radius2, const float angle,
- const tc *const color, const float opacity,
- const unsigned int pattern, const bool is_filled) {
- if (is_empty() || (!is_filled && !pattern)) return *this;
- const float radiusM = std::max(radius1,radius2);
- if (radius1<0 || radius2<0 || x0 - radiusM>=width() || y0 + radiusM<0 || y0 - radiusM>=height()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_ellipse(): Specified color is (null).",
- cimg_instance);
- const int iradius1 = (int)cimg::round(radius1), iradius2 = (int)cimg::round(radius2);
- if (!iradius1 && !iradius2) return draw_point(x0,y0,color,opacity);
- if (iradius1==iradius2) {
- if (is_filled) return draw_circle(x0,y0,iradius1,color,opacity);
- else if (pattern==~0U) return draw_circle(x0,y0,iradius1,color,opacity,pattern);
- }
- const float ang = (float)(angle*cimg::PI/180);
- if (!is_filled) { // Outlined
- const float ca = std::cos(ang), sa = std::sin(ang);
- CImg<int> points((unsigned int)cimg::round(6*radiusM),2);
- cimg_forX(points,k) {
- const float
- _ang = (float)(2*cimg::PI*k/points._width),
- X = (float)(radius1*std::cos(_ang)),
- Y = (float)(radius2*std::sin(_ang));
- points(k,0) = (int)cimg::round(x0 + (X*ca - Y*sa));
- points(k,1) = (int)cimg::round(y0 + (X*sa + Y*ca));
- }
- draw_polygon(points,color,opacity,pattern);
- } else { // Filled
- cimg_init_scanline(opacity);
- const float
- ca = std::cos(ang),
- sa = -std::sin(ang),
- ca2 = ca*ca,
- sa2 = sa*sa,
- casa = ca*sa,
- i1 = 1/cimg::sqr(radius1),
- i2 = 1/cimg::sqr(radius2),
- t1 = i1*ca2 + i2*sa2,
- t2 = (i2 - i1)*casa,
- t3 = i2*ca2 + i1*sa2,
- t12 = t1*2;
- const int
- _ymin = (int)std::floor(y0 - radiusM),
- _ymax = (int)std::ceil(y0 + radiusM),
- ymin = _ymin<0?0:_ymin,
- ymax = _ymax>=height()?height() - 1:_ymax;
- for (int y = ymin; y<=ymax; ++y) {
- const float
- Y = y - y0 + 0.5f,
- B = 2*t2*Y,
- C = t3*Y*Y - 1,
- D = B*B - 4*t1*C;
- if (D>=0) {
- const float sD = std::sqrt(D);
- const int
- xmin = (int)(x0 + cimg::round((-B - sD)/t12)),
- xmax = (int)(x0 + cimg::round((-B + sD)/t12));
- cimg_draw_scanline(xmin,xmax,y,color,opacity,1);
- }
- }
- }
- return *this;
- }
- //! Draw a filled 2D circle.
- /**
- \param x0 X-coordinate of the circle center.
- \param y0 Y-coordinate of the circle center.
- \param radius Circle radius.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \note
- - Circle version of the Bresenham's algorithm is used.
- **/
- template<typename tc>
- CImg<T>& draw_circle(const int x0, const int y0, int radius,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (radius<0 || x0 - radius>=width() || y0 + radius<0 || y0 - radius>=height()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_circle(): Specified color is (null).",
- cimg_instance);
- if (!radius) return draw_point(x0,y0,color,opacity);
- cimg_init_scanline(opacity);
- if (y0>=0 && y0<height()) cimg_draw_scanline(x0 - radius,x0 + radius,y0,color,opacity,1);
- for (int f = 1 - radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x<y; ) {
- if (f>=0) {
- const int x1 = x0 - x, x2 = x0 + x, y1 = y0 - y, y2 = y0 + y;
- if (y1>=0 && y1<height()) cimg_draw_scanline(x1,x2,y1,color,opacity,1);
- if (y2>=0 && y2<height()) cimg_draw_scanline(x1,x2,y2,color,opacity,1);
- f+=(ddFy+=2); --y;
- }
- const bool no_diag = y!=(x++);
- ++(f+=(ddFx+=2));
- const int x1 = x0 - y, x2 = x0 + y, y1 = y0 - x, y2 = y0 + x;
- if (no_diag) {
- if (y1>=0 && y1<height()) cimg_draw_scanline(x1,x2,y1,color,opacity,1);
- if (y2>=0 && y2<height()) cimg_draw_scanline(x1,x2,y2,color,opacity,1);
- }
- }
- return *this;
- }
- //! Draw an outlined 2D circle.
- /**
- \param x0 X-coordinate of the circle center.
- \param y0 Y-coordinate of the circle center.
- \param radius Circle radius.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern An integer whose bits describe the outline pattern.
- **/
- template<typename tc>
- CImg<T>& draw_circle(const int x0, const int y0, int radius,
- const tc *const color, const float opacity,
- const unsigned int pattern) {
- if (pattern!=~0U) return draw_ellipse(x0,y0,radius,radius,0,color,opacity,pattern);
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_circle(): Specified color is (null).",
- cimg_instance);
- if (radius<0 || x0 - radius>=width() || y0 + radius<0 || y0 - radius>=height()) return *this;
- if (!radius) return draw_point(x0,y0,color,opacity);
- draw_point(x0 - radius,y0,color,opacity).draw_point(x0 + radius,y0,color,opacity).
- draw_point(x0,y0 - radius,color,opacity).draw_point(x0,y0 + radius,color,opacity);
- if (radius==1) return *this;
- for (int f = 1 - radius, ddFx = 0, ddFy = -(radius<<1), x = 0, y = radius; x<y; ) {
- if (f>=0) { f+=(ddFy+=2); --y; }
- ++x; ++(f+=(ddFx+=2));
- if (x!=y + 1) {
- const int x1 = x0 - y, x2 = x0 + y, y1 = y0 - x, y2 = y0 + x,
- x3 = x0 - x, x4 = x0 + x, y3 = y0 - y, y4 = y0 + y;
- draw_point(x1,y1,color,opacity).draw_point(x1,y2,color,opacity).
- draw_point(x2,y1,color,opacity).draw_point(x2,y2,color,opacity);
- if (x!=y)
- draw_point(x3,y3,color,opacity).draw_point(x4,y4,color,opacity).
- draw_point(x4,y3,color,opacity).draw_point(x3,y4,color,opacity);
- }
- }
- return *this;
- }
- //! Draw an image.
- /**
- \param sprite Sprite image.
- \param x0 X-coordinate of the sprite position.
- \param y0 Y-coordinate of the sprite position.
- \param z0 Z-coordinate of the sprite position.
- \param c0 C-coordinate of the sprite position.
- \param opacity Drawing opacity.
- **/
- template<typename t>
- CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
- const CImg<t>& sprite, const float opacity=1) {
- if (is_empty() || !sprite) return *this;
- if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity);
- if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared())
- return assign(sprite,false);
- const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
- const int
- dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
- sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
- lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
- ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
- lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
- lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- if (lx>0 && ly>0 && lz>0 && lc>0) {
- for (int c = 0; c<lc; ++c)
- for (int z = 0; z<lz; ++z)
- for (int y = 0; y<ly; ++y) {
- T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
- const t *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
- if (opacity>=1) for (int x = 0; x<lx; ++x) *(ptrd++) = (T)*(ptrs++);
- else for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*(*(ptrs++)) + *ptrd*copacity); ++ptrd; }
- }
- }
- return *this;
- }
- //! Draw an image \specialization.
- CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
- const CImg<T>& sprite, const float opacity=1) {
- if (is_empty() || !sprite) return *this;
- if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,opacity);
- if (x0==0 && y0==0 && z0==0 && c0==0 && is_sameXYZC(sprite) && opacity>=1 && !is_shared())
- return assign(sprite,false);
- const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
- const int
- dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
- sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
- lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
- ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
- lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
- lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
- const ulongT slx = lx*sizeof(T);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- if (lx>0 && ly>0 && lz>0 && lc>0) {
- for (int c = 0; c<lc; ++c)
- for (int z = 0; z<lz; ++z)
- for (int y = 0; y<ly; ++y) {
- T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
- const T *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
- if (opacity>=1) std::memcpy(ptrd,ptrs,slx);
- else for (int x = 0; x<lx; ++x) { *ptrd = (T)(nopacity*(*(ptrs++)) + *ptrd*copacity); ++ptrd; }
- }
- }
- return *this;
- }
- //! Draw an image \overloading.
- template<typename t>
- CImg<T>& draw_image(const int x0, const int y0, const int z0,
- const CImg<t>& sprite, const float opacity=1) {
- return draw_image(x0,y0,z0,0,sprite,opacity);
- }
- //! Draw an image \overloading.
- template<typename t>
- CImg<T>& draw_image(const int x0, const int y0,
- const CImg<t>& sprite, const float opacity=1) {
- return draw_image(x0,y0,0,sprite,opacity);
- }
- //! Draw an image \overloading.
- template<typename t>
- CImg<T>& draw_image(const int x0,
- const CImg<t>& sprite, const float opacity=1) {
- return draw_image(x0,0,sprite,opacity);
- }
- //! Draw an image \overloading.
- template<typename t>
- CImg<T>& draw_image(const CImg<t>& sprite, const float opacity=1) {
- return draw_image(0,sprite,opacity);
- }
- //! Draw a masked image.
- /**
- \param sprite Sprite image.
- \param mask Mask image.
- \param x0 X-coordinate of the sprite position in the image instance.
- \param y0 Y-coordinate of the sprite position in the image instance.
- \param z0 Z-coordinate of the sprite position in the image instance.
- \param c0 C-coordinate of the sprite position in the image instance.
- \param mask_max_value Maximum pixel value of the mask image \c mask.
- \param opacity Drawing opacity.
- \note
- - Pixel values of \c mask set the opacity of the corresponding pixels in \c sprite.
- - Dimensions along x,y and z of \p sprite and \p mask must be the same.
- **/
- template<typename ti, typename tm>
- CImg<T>& draw_image(const int x0, const int y0, const int z0, const int c0,
- const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
- const float mask_max_value=1) {
- if (is_empty() || !sprite || !mask) return *this;
- if (is_overlapped(sprite)) return draw_image(x0,y0,z0,c0,+sprite,mask,opacity,mask_max_value);
- if (is_overlapped(mask)) return draw_image(x0,y0,z0,c0,sprite,+mask,opacity,mask_max_value);
- if (mask._width!=sprite._width || mask._height!=sprite._height || mask._depth!=sprite._depth)
- throw CImgArgumentException(_cimg_instance
- "draw_image(): Sprite (%u,%u,%u,%u,%p) and mask (%u,%u,%u,%u,%p) have "
- "incompatible dimensions.",
- cimg_instance,
- sprite._width,sprite._height,sprite._depth,sprite._spectrum,sprite._data,
- mask._width,mask._height,mask._depth,mask._spectrum,mask._data);
- const bool bx = x0<0, by = y0<0, bz = z0<0, bc = c0<0;
- const int
- dx0 = bx?0:x0, dy0 = by?0:y0, dz0 = bz?0:z0, dc0 = bc?0:c0,
- sx0 = dx0 - x0, sy0 = dy0 - y0, sz0 = dz0 - z0, sc0 = dc0 - c0,
- lx = sprite.width() - sx0 - (x0 + sprite.width()>width()?x0 + sprite.width() - width():0),
- ly = sprite.height() - sy0 - (y0 + sprite.height()>height()?y0 + sprite.height() - height():0),
- lz = sprite.depth() - sz0 - (z0 + sprite.depth()>depth()?z0 + sprite.depth() - depth():0),
- lc = sprite.spectrum() - sc0 - (c0 + sprite.spectrum()>spectrum()?c0 + sprite.spectrum() - spectrum():0);
- const ulongT msize = mask.size();
- if (lx>0 && ly>0 && lz>0 && lc>0) {
- for (int c = 0; c<lc; ++c)
- for (int z = 0; z<lz; ++z)
- for (int y = 0; y<ly; ++y) {
- T *ptrd = data(dx0,dy0 + y,dz0 + z,dc0 + c);
- const ti *ptrs = sprite.data(sx0,sy0 + y,sz0 + z,sc0 + c);
- const tm *ptrm = mask._data + (mask.offset(sx0,sy0 + y,sz0 + z,sc0 + c)%msize);
- for (int x = 0; x<lx; ++x) {
- const float mopacity = (float)(*(ptrm++)*opacity),
- nopacity = cimg::abs(mopacity), copacity = mask_max_value - std::max(mopacity,0.f);
- *ptrd = (T)((nopacity*(*(ptrs++)) + *ptrd*copacity)/mask_max_value);
- ++ptrd;
- }
- }
- }
- return *this;
- }
- //! Draw a masked image \overloading.
- template<typename ti, typename tm>
- CImg<T>& draw_image(const int x0, const int y0, const int z0,
- const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
- const float mask_max_value=1) {
- return draw_image(x0,y0,z0,0,sprite,mask,opacity,mask_max_value);
- }
- //! Draw a image \overloading.
- template<typename ti, typename tm>
- CImg<T>& draw_image(const int x0, const int y0,
- const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
- const float mask_max_value=1) {
- return draw_image(x0,y0,0,sprite,mask,opacity,mask_max_value);
- }
- //! Draw a image \overloading.
- template<typename ti, typename tm>
- CImg<T>& draw_image(const int x0,
- const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
- const float mask_max_value=1) {
- return draw_image(x0,0,sprite,mask,opacity,mask_max_value);
- }
- //! Draw an image.
- template<typename ti, typename tm>
- CImg<T>& draw_image(const CImg<ti>& sprite, const CImg<tm>& mask, const float opacity=1,
- const float mask_max_value=1) {
- return draw_image(0,sprite,mask,opacity,mask_max_value);
- }
- //! Draw a text string.
- /**
- \param x0 X-coordinate of the text in the image instance.
- \param y0 Y-coordinate of the text in the image instance.
- \param text Format of the text ('printf'-style format string).
- \param foreground_color Pointer to \c spectrum() consecutive values, defining the foreground drawing color.
- \param background_color Pointer to \c spectrum() consecutive values, defining the background drawing color.
- \param opacity Drawing opacity.
- \param font Font used for drawing text.
- **/
- template<typename tc1, typename tc2, typename t>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const tc1 *const foreground_color, const tc2 *const background_color,
- const float opacity, const CImgList<t>& font, ...) {
- if (!font) return *this;
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- return _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,false);
- }
- //! Draw a text string \overloading.
- /**
- \note A transparent background is used for the text.
- **/
- template<typename tc, typename t>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const tc *const foreground_color, const int,
- const float opacity, const CImgList<t>& font, ...) {
- if (!font) return *this;
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- return _draw_text(x0,y0,tmp,foreground_color,(tc*)0,opacity,font,false);
- }
- //! Draw a text string \overloading.
- /**
- \note A transparent foreground is used for the text.
- **/
- template<typename tc, typename t>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const int, const tc *const background_color,
- const float opacity, const CImgList<t>& font, ...) {
- if (!font) return *this;
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- return _draw_text(x0,y0,tmp,(tc*)0,background_color,opacity,font,false);
- }
- //! Draw a text string \overloading.
- /**
- \param x0 X-coordinate of the text in the image instance.
- \param y0 Y-coordinate of the text in the image instance.
- \param text Format of the text ('printf'-style format string).
- \param foreground_color Array of spectrum() values of type \c T,
- defining the foreground color (0 means 'transparent').
- \param background_color Array of spectrum() values of type \c T,
- defining the background color (0 means 'transparent').
- \param opacity Drawing opacity.
- \param font_height Height of the text font (exact match for 13,23,53,103, interpolated otherwise).
- **/
- template<typename tc1, typename tc2>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const tc1 *const foreground_color, const tc2 *const background_color,
- const float opacity=1, const unsigned int font_height=13, ...) {
- if (!font_height) return *this;
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font_height);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- const CImgList<ucharT>& font = CImgList<ucharT>::font(font_height,true);
- _draw_text(x0,y0,tmp,foreground_color,background_color,opacity,font,true);
- return *this;
- }
- //! Draw a text string \overloading.
- template<typename tc>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const tc *const foreground_color, const int background_color=0,
- const float opacity=1, const unsigned int font_height=13, ...) {
- if (!font_height) return *this;
- cimg::unused(background_color);
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font_height);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- return draw_text(x0,y0,"%s",foreground_color,(const tc*)0,opacity,font_height,tmp._data);
- }
- //! Draw a text string \overloading.
- template<typename tc>
- CImg<T>& draw_text(const int x0, const int y0,
- const char *const text,
- const int, const tc *const background_color,
- const float opacity=1, const unsigned int font_height=13, ...) {
- if (!font_height) return *this;
- CImg<charT> tmp(2048);
- std::va_list ap; va_start(ap,font_height);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- return draw_text(x0,y0,"%s",(tc*)0,background_color,opacity,font_height,tmp._data);
- }
- template<typename tc1, typename tc2, typename t>
- CImg<T>& _draw_text(const int x0, const int y0,
- const char *const text,
- const tc1 *const foreground_color, const tc2 *const background_color,
- const float opacity, const CImgList<t>& font,
- const bool is_native_font) {
- if (!text) return *this;
- if (!font)
- throw CImgArgumentException(_cimg_instance
- "draw_text(): Empty specified font.",
- cimg_instance);
- const unsigned int text_length = (unsigned int)std::strlen(text);
- const int padding_x = font[0]._height<48?1:font[0]._height<128?(int)std::ceil(font[0]._height/51.0f + 0.745f):4;
- unsigned char o_ch, ch = 0;
- int x, y, w;
- CImg<intT> left_paddings(text_length,1,1,1,0);
- const CImg<t> empty = CImg<t>::empty();
- if (is_empty() || is_native_font) {
- // Pre-compute necessary size of the image as well as left paddings of each character.
- x = y = w = 0;
- o_ch = 0;
- for (unsigned int i = 0; i<text_length; ++i) {
- ch = (unsigned char)text[i];
- switch (ch) {
- case '\n' : y+=font[0]._height; if (x>w) w = x; x = 0; break;
- case '\t' : x+=4*font[(int)' ']._width; break;
- case ' ' : x+=font[(int)' ']._width; break;
- default : if (ch<font._width) {
- int left_padding = 0;
- if (is_native_font && font[0]._height<128) {
- // Determine left padding from various rules.
- if (ch==':' || ch=='!' || ch=='.' || ch==';')
- left_padding = 2*padding_x;
- else if (o_ch==',' || (o_ch=='.' && (ch<'0' || ch>'9')) || o_ch==';' || o_ch==':' || o_ch=='!')
- left_padding = 4*padding_x;
- else if (((o_ch=='i' || o_ch=='l' || o_ch=='I' || o_ch=='J' || o_ch=='M' || o_ch=='N') &&
- ((ch>='0' && ch<='9') ||
- (ch>='a' && ch<='z' && ch!='v' && ch!='x' && ch!='y') ||
- (ch>='B' && ch<='Z' && ch!='J' && ch!='T' && ch!='V' && ch!='X' && ch!='Y'))) ||
- o_ch=='.' || o_ch=='\'' || ch=='\'')
- left_padding = padding_x;
- else if ((o_ch<'0' || o_ch>'9') && ch!='-') {
- const CImg<t> &mask = ch + 256U<font._width?font[ch + 256]:empty;
- if (o_ch && ch>' ' && o_ch>' ' && mask._height>13) {
- const CImg<t> &o_mask = o_ch + 256U<font._width?font[o_ch + 256]:empty;
- if (o_mask._height>13) {
- const int w1 = mask.width()>0?o_mask.width() - 1:0, w2 = w1>1?w1 - 1:0, w3 = w2>1?w2 - 1:0;
- left_padding = -10;
- cimg_forY(mask,k) {
- const int
- lpad = o_mask(w1,k)>=8?0:
- o_mask._width<=2 || o_mask(w2,k)>=8?-1:
- o_mask._width<=3 || o_mask(w3,k)>=8?-2:-3,
- rpad = mask(0,k)>=8?0:
- mask._width<=2 || mask(1,k)>=8?-1:
- mask._width<=3 || mask(2,k)>=8?-2:-3;
- left_padding = std::max(left_padding,lpad + rpad);
- }
- }
- }
- }
- left_paddings[i] = left_padding;
- }
- x+=left_padding + font[ch]._width + padding_x;
- o_ch = ch;
- }
- }
- }
- if (x!=0 || ch=='\n') { if (x>w) w = x; y+=font[0]._height; }
- if (is_empty()) assign(x0 + w,y0 + y,1,is_native_font?1:font[0]._spectrum,(T)0);
- }
- // Draw font characters on image.
- x = x0; y = y0;
- for (unsigned int i = 0; i<text_length; ++i) {
- ch = (unsigned char)text[i];
- switch (ch) {
- case '\n' : y+=font[0]._height; x = x0; break;
- case '\t' :
- case ' ' : {
- const unsigned int lw = (ch=='\t'?4:1)*font[(int)' ']._width, lh = font[(int)' ']._height;
- if (background_color) draw_rectangle(x,y,x + lw - 1,y + lh - 1,background_color,opacity);
- x+=lw;
- } break;
- default : if (ch<font._width) {
- CImg<T> letter = font[ch];
- if (letter) {
- const CImg<t> &mask = ch + 256U<font._width?font[ch + 256]:empty;
- const int posx = x + left_paddings[i] + padding_x;
- if (is_native_font && _spectrum>letter._spectrum)
- letter.assign(letter.get_resize(-100,-100,1,_spectrum,0,2),false);
- const unsigned int cmin = std::min(_spectrum,letter._spectrum);
- if (foreground_color)
- for (unsigned int c = 0; c<cmin; ++c)
- if (foreground_color[c]!=1) letter.get_shared_channel(c)*=foreground_color[c];
- if (mask) { // Letter has mask
- if (background_color)
- for (unsigned int c = 0; c<cmin; ++c)
- draw_rectangle(x,y,0,c,posx + letter._width - 1,y + letter._height - 1,0,c,
- background_color[c],opacity);
- draw_image(posx,y,letter,font[ch + 256],opacity,255.f);
- } else draw_image(posx,y,letter,opacity); // Letter has no mask
- x = posx + letter._width;
- }
- }
- }
- }
- return *this;
- }
- // [internal] Version used to display text in interactive viewers.
- CImg<T>& __draw_text(const char *const text, unsigned int &font_size, const int is_down, ...) {
- CImg<charT> tmp(2048);
- std::va_list ap;
- va_start(ap,is_down);
- cimg_vsnprintf(tmp,tmp._width,text,ap); va_end(ap);
- CImg<ucharT> a_label, a_labelmask;
- const unsigned char a_labelcolor = 255;
- unsigned int ofs = font_size, fs = ofs;
- do { // Determine best font size
- a_label.assign().draw_text(0,0,"%s",&a_labelcolor,0,1,fs,tmp._data);
- if (a_label._width<7*_width/10 && a_label._height>_height/20 && a_label._height<_height/5) {
- font_size = fs; break;
- } else if ((a_label._width>7*_width/10 || a_label._height>_height/5) && fs>13 && ofs>=fs) {
- ofs = fs; fs = std::max(13U,(unsigned int)cimg::round(fs/1.25f));
- } else if (a_label._width<3*_width/10 && a_label._height<_height/20 && fs<64 && ofs<=fs) {
- ofs = fs; fs = std::min(64U,(unsigned int)cimg::round(fs*1.25f));
- } else { font_size = fs; break; }
- } while (true);
- a_label.normalize(0,255);
- a_label+=(255 - a_label.get_dilate(3)).normalize(0,80);
- a_label.resize(-100,-100,1,3,1);
- return draw_image(0,is_down?height() - a_label.height():0,a_label,0.85f);
- }
- //! Draw a 2D vector field.
- /**
- \param flow Image of 2D vectors used as input data.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param sampling Length (in pixels) between each arrow.
- \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length).
- \param is_arrow Tells if arrows must be drawn, instead of oriented segments.
- \param pattern Used pattern to draw lines.
- \note Clipping is supported.
- **/
- template<typename t1, typename t2>
- CImg<T>& draw_quiver(const CImg<t1>& flow,
- const t2 *const color, const float opacity=1,
- const unsigned int sampling=25, const float factor=-20,
- const bool is_arrow=true, const unsigned int pattern=~0U) {
- return draw_quiver(flow,CImg<t2>(color,_spectrum,1,1,1,true),opacity,sampling,factor,is_arrow,pattern);
- }
- //! Draw a 2D vector field, using a field of colors.
- /**
- \param flow Image of 2D vectors used as input data.
- \param color Image of spectrum()-D vectors corresponding to the color of each arrow.
- \param opacity Opacity of the drawing.
- \param sampling Length (in pixels) between each arrow.
- \param factor Length factor of each arrow (if <0, computed as a percentage of the maximum length).
- \param is_arrow Tells if arrows must be drawn, instead of oriented segments.
- \param pattern Used pattern to draw lines.
- \note Clipping is supported.
- **/
- template<typename t1, typename t2>
- CImg<T>& draw_quiver(const CImg<t1>& flow,
- const CImg<t2>& color, const float opacity=1,
- const unsigned int sampling=25, const float factor=-20,
- const bool is_arrow=true, const unsigned int pattern=~0U) {
- if (is_empty()) return *this;
- if (!flow || flow._spectrum!=2)
- throw CImgArgumentException(_cimg_instance
- "draw_quiver(): Invalid dimensions of specified flow (%u,%u,%u,%u,%p).",
- cimg_instance,
- flow._width,flow._height,flow._depth,flow._spectrum,flow._data);
- if (sampling<=0)
- throw CImgArgumentException(_cimg_instance
- "draw_quiver(): Invalid sampling value %g "
- "(should be >0)",
- cimg_instance,
- sampling);
- const bool colorfield = (color._width==flow._width && color._height==flow._height &&
- color._depth==1 && color._spectrum==_spectrum);
- if (is_overlapped(flow)) return draw_quiver(+flow,color,opacity,sampling,factor,is_arrow,pattern);
- float vmax,fact;
- if (factor<=0) {
- float m, M = (float)flow.get_norm(2).max_min(m);
- vmax = (float)std::max(cimg::abs(m),cimg::abs(M));
- if (!vmax) vmax = 1;
- fact = -factor;
- } else { fact = factor; vmax = 1; }
- for (unsigned int y = sampling/2; y<_height; y+=sampling)
- for (unsigned int x = sampling/2; x<_width; x+=sampling) {
- const unsigned int X = x*flow._width/_width, Y = y*flow._height/_height;
- float u = (float)flow(X,Y,0,0)*fact/vmax, v = (float)flow(X,Y,0,1)*fact/vmax;
- if (is_arrow) {
- const int xx = (int)(x + u), yy = (int)(y + v);
- if (colorfield) draw_arrow(x,y,xx,yy,color.get_vector_at(X,Y)._data,opacity,45,sampling/5.f,pattern);
- else draw_arrow(x,y,xx,yy,color._data,opacity,45,sampling/5.f,pattern);
- } else {
- if (colorfield)
- draw_line((int)(x - 0.5*u),(int)(y - 0.5*v),(int)(x + 0.5*u),(int)(y + 0.5*v),
- color.get_vector_at(X,Y)._data,opacity,pattern);
- else draw_line((int)(x - 0.5*u),(int)(y - 0.5*v),(int)(x + 0.5*u),(int)(y + 0.5*v),
- color._data,opacity,pattern);
- }
- }
- return *this;
- }
- //! Draw a labeled horizontal axis.
- /**
- \param values_x Values along the horizontal axis.
- \param y Y-coordinate of the horizontal axis in the image instance.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern Drawing pattern.
- \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
- \param allow_zero Enable/disable the drawing of label '0' if found.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_axis(const CImg<t>& values_x, const int y,
- const tc *const color, const float opacity=1,
- const unsigned int pattern=~0U, const unsigned int font_height=13,
- const bool allow_zero=true, const float round_x=0) {
- if (is_empty()) return *this;
- const int yt = (y + 3 + font_height)<_height?y + 3:y - 2 - (int)font_height;
- const int siz = (int)values_x.size() - 1;
- CImg<charT> txt(32);
- CImg<T> a_label;
- if (siz<=0) { // Degenerated case
- draw_line(0,y,_width - 1,y,color,opacity,pattern);
- if (!siz) {
- cimg_snprintf(txt,txt._width,"%g",round_x?cimg::round((double)*values_x,round_x):(double)*values_x);
- a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
- const int
- _xt = (width() - a_label.width())/2,
- xt = _xt<3?3:_xt + a_label.width()>=width() - 2?width() - 3 - a_label.width():_xt;
- draw_point(width()/2,y - 1,color,opacity).draw_point(width()/2,y + 1,color,opacity);
- if (allow_zero || *txt!='0' || txt[1]!=0)
- draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
- }
- } else { // Regular case
- if (values_x[0]<values_x[siz]) draw_arrow(0,y,_width - 1,y,color,opacity,30,5,pattern);
- else draw_arrow(_width - 1,y,0,y,color,opacity,30,5,pattern);
- cimg_foroff(values_x,x) {
- cimg_snprintf(txt,txt._width,"%g",round_x?cimg::round((double)values_x(x),round_x):(double)values_x(x));
- a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
- const int
- xi = (int)(x*(_width - 1)/siz),
- _xt = xi - a_label.width()/2,
- xt = _xt<3?3:_xt + a_label.width()>=width() - 2?width() - 3 - a_label.width():_xt;
- draw_point(xi,y - 1,color,opacity).draw_point(xi,y + 1,color,opacity);
- if (allow_zero || *txt!='0' || txt[1]!=0)
- draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
- }
- }
- return *this;
- }
- //! Draw a labeled vertical axis.
- /**
- \param x X-coordinate of the vertical axis in the image instance.
- \param values_y Values along the Y-axis.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern Drawing pattern.
- \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
- \param allow_zero Enable/disable the drawing of label '0' if found.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_axis(const int x, const CImg<t>& values_y,
- const tc *const color, const float opacity=1,
- const unsigned int pattern=~0U, const unsigned int font_height=13,
- const bool allow_zero=true, const float round_y=0) {
- if (is_empty()) return *this;
- int siz = (int)values_y.size() - 1;
- CImg<charT> txt(32);
- CImg<T> a_label;
- if (siz<=0) { // Degenerated case
- draw_line(x,0,x,_height - 1,color,opacity,pattern);
- if (!siz) {
- cimg_snprintf(txt,txt._width,"%g",round_y?cimg::round((double)*values_y,round_y):(double)*values_y);
- a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
- const int
- _yt = (height() - a_label.height())/2,
- yt = _yt<0?0:_yt + a_label.height()>=height()?height() - 1 - a_label.height():_yt,
- _xt = x - 2 - a_label.width(),
- xt = _xt>=0?_xt:x + 3;
- draw_point(x - 1,height()/2,color,opacity).draw_point(x + 1,height()/2,color,opacity);
- if (allow_zero || *txt!='0' || txt[1]!=0)
- draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
- }
- } else { // Regular case
- if (values_y[0]<values_y[siz]) draw_arrow(x,0,x,_height - 1,color,opacity,30,5,pattern);
- else draw_arrow(x,_height - 1,x,0,color,opacity,30,5,pattern);
- cimg_foroff(values_y,y) {
- cimg_snprintf(txt,txt._width,"%g",round_y?cimg::round((double)values_y(y),round_y):(double)values_y(y));
- a_label.assign().draw_text(0,0,txt,color,(tc*)0,opacity,font_height);
- const int
- yi = (int)(y*(_height - 1)/siz),
- _yt = yi - a_label.height()/2,
- yt = _yt<0?0:_yt + a_label.height()>=height()?height() - 1 - a_label.height():_yt,
- _xt = x - 2 - a_label.width(),
- xt = _xt>=0?_xt:x + 3;
- draw_point(x - 1,yi,color,opacity).draw_point(x + 1,yi,color,opacity);
- if (allow_zero || *txt!='0' || txt[1]!=0)
- draw_text(xt,yt,txt,color,(tc*)0,opacity,font_height);
- }
- }
- return *this;
- }
- //! Draw labeled horizontal and vertical axes.
- /**
- \param values_x Values along the X-axis.
- \param values_y Values along the Y-axis.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern_x Drawing pattern for the X-axis.
- \param pattern_y Drawing pattern for the Y-axis.
- \param font_height Height of the labels (exact match for 13,23,53,103, interpolated otherwise).
- \param allow_zero Enable/disable the drawing of label '0' if found.
- **/
- template<typename tx, typename ty, typename tc>
- CImg<T>& draw_axes(const CImg<tx>& values_x, const CImg<ty>& values_y,
- const tc *const color, const float opacity=1,
- const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U,
- const unsigned int font_height=13, const bool allow_zero=true,
- const float round_x=0, const float round_y=0) {
- if (is_empty()) return *this;
- const CImg<tx> nvalues_x(values_x._data,values_x.size(),1,1,1,true);
- const int sizx = (int)values_x.size() - 1, wm1 = width() - 1;
- if (sizx>=0) {
- float ox = (float)*nvalues_x;
- for (unsigned int x = sizx?1U:0U; x<_width; ++x) {
- const float nx = (float)nvalues_x._linear_atX((float)x*sizx/wm1);
- if (nx*ox<=0) {
- draw_axis(nx==0?x:x - 1,values_y,color,opacity,pattern_y,font_height,allow_zero,round_y);
- break;
- }
- ox = nx;
- }
- }
- const CImg<ty> nvalues_y(values_y._data,values_y.size(),1,1,1,true);
- const int sizy = (int)values_y.size() - 1, hm1 = height() - 1;
- if (sizy>0) {
- float oy = (float)nvalues_y[0];
- for (unsigned int y = sizy?1U:0U; y<_height; ++y) {
- const float ny = (float)nvalues_y._linear_atX((float)y*sizy/hm1);
- if (ny*oy<=0) {
- draw_axis(values_x,ny==0?y:y - 1,color,opacity,pattern_x,font_height,allow_zero,round_x);
- break;
- }
- oy = ny;
- }
- }
- return *this;
- }
- //! Draw labeled horizontal and vertical axes \overloading.
- template<typename tc>
- CImg<T>& draw_axes(const float x0, const float x1, const float y0, const float y1,
- const tc *const color, const float opacity=1,
- const int subdivisionx=-60, const int subdivisiony=-60,
- const float precisionx=0, const float precisiony=0,
- const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U,
- const unsigned int font_height=13) {
- if (is_empty()) return *this;
- const bool allow_zero = (x0*x1>0) || (y0*y1>0);
- const float
- dx = cimg::abs(x1 - x0), dy = cimg::abs(y1 - y0),
- px = dx<=0?1:precisionx==0?(float)std::pow(10.,(int)std::log10(dx) - 2.):precisionx,
- py = dy<=0?1:precisiony==0?(float)std::pow(10.,(int)std::log10(dy) - 2.):precisiony;
- if (x0!=x1 && y0!=y1)
- draw_axes(CImg<floatT>::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1),
- CImg<floatT>::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1),
- color,opacity,pattern_x,pattern_y,font_height,allow_zero,px,py);
- else if (x0==x1 && y0!=y1)
- draw_axis((int)x0,CImg<floatT>::sequence(subdivisiony>0?subdivisiony:1-height()/subdivisiony,y0,y1),
- color,opacity,pattern_y,font_height,py);
- else if (x0!=x1 && y0==y1)
- draw_axis(CImg<floatT>::sequence(subdivisionx>0?subdivisionx:1-width()/subdivisionx,x0,x1),(int)y0,
- color,opacity,pattern_x,font_height,px);
- return *this;
- }
- //! Draw 2D grid.
- /**
- \param values_x X-coordinates of the vertical lines.
- \param values_y Y-coordinates of the horizontal lines.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param pattern_x Drawing pattern for vertical lines.
- \param pattern_y Drawing pattern for horizontal lines.
- **/
- template<typename tx, typename ty, typename tc>
- CImg<T>& draw_grid(const CImg<tx>& values_x, const CImg<ty>& values_y,
- const tc *const color, const float opacity=1,
- const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) {
- if (is_empty()) return *this;
- if (values_x) cimg_foroff(values_x,x) {
- const int xi = (int)values_x[x];
- if (xi>=0 && xi<width()) draw_line(xi,0,xi,_height - 1,color,opacity,pattern_x);
- }
- if (values_y) cimg_foroff(values_y,y) {
- const int yi = (int)values_y[y];
- if (yi>=0 && yi<height()) draw_line(0,yi,_width - 1,yi,color,opacity,pattern_y);
- }
- return *this;
- }
- //! Draw 2D grid \simplification.
- template<typename tc>
- CImg<T>& draw_grid(const float delta_x, const float delta_y,
- const float offsetx, const float offsety,
- const bool invertx, const bool inverty,
- const tc *const color, const float opacity=1,
- const unsigned int pattern_x=~0U, const unsigned int pattern_y=~0U) {
- if (is_empty()) return *this;
- CImg<uintT> seqx, seqy;
- if (delta_x!=0) {
- const float dx = delta_x>0?delta_x:_width*-delta_x/100;
- const unsigned int nx = (unsigned int)(_width/dx);
- seqx = CImg<uintT>::sequence(1 + nx,0,(unsigned int)(dx*nx));
- if (offsetx) cimg_foroff(seqx,x) seqx(x) = (unsigned int)cimg::mod(seqx(x) + offsetx,(float)_width);
- if (invertx) cimg_foroff(seqx,x) seqx(x) = _width - 1 - seqx(x);
- }
- if (delta_y!=0) {
- const float dy = delta_y>0?delta_y:_height*-delta_y/100;
- const unsigned int ny = (unsigned int)(_height/dy);
- seqy = CImg<uintT>::sequence(1 + ny,0,(unsigned int)(dy*ny));
- if (offsety) cimg_foroff(seqy,y) seqy(y) = (unsigned int)cimg::mod(seqy(y) + offsety,(float)_height);
- if (inverty) cimg_foroff(seqy,y) seqy(y) = _height - 1 - seqy(y);
- }
- return draw_grid(seqx,seqy,color,opacity,pattern_x,pattern_y);
- }
- //! Draw 1D graph.
- /**
- \param data Image containing the graph values I = f(x).
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- \param plot_type Define the type of the plot:
- - 0 = No plot.
- - 1 = Plot using segments.
- - 2 = Plot using cubic splines.
- - 3 = Plot with bars.
- \param vertex_type Define the type of points:
- - 0 = No points.
- - 1 = Point.
- - 2 = Straight cross.
- - 3 = Diagonal cross.
- - 4 = Filled circle.
- - 5 = Outlined circle.
- - 6 = Square.
- - 7 = Diamond.
- \param ymin Lower bound of the y-range.
- \param ymax Upper bound of the y-range.
- \param pattern Drawing pattern.
- \note
- - if \c ymin==ymax==0, the y-range is computed automatically from the input samples.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_graph(const CImg<t>& data,
- const tc *const color, const float opacity=1,
- const unsigned int plot_type=1, const int vertex_type=1,
- const double ymin=0, const double ymax=0, const unsigned int pattern=~0U) {
- if (is_empty() || _height<=1) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_graph(): Specified color is (null).",
- cimg_instance);
- // Create shaded colors for displaying bar plots.
- CImg<tc> color1, color2;
- if (plot_type==3) {
- color1.assign(_spectrum); color2.assign(_spectrum);
- cimg_forC(*this,c) {
- color1[c] = (tc)std::min((float)cimg::type<tc>::max(),(float)color[c]*1.2f);
- color2[c] = (tc)(color[c]*0.4f);
- }
- }
- // Compute min/max and normalization factors.
- const ulongT
- siz = data.size(),
- _siz1 = siz - (plot_type!=3),
- siz1 = _siz1?_siz1:1;
- const unsigned int
- _width1 = _width - (plot_type!=3),
- width1 = _width1?_width1:1;
- double m = ymin, M = ymax;
- if (ymin==ymax) m = (double)data.max_min(M);
- if (m==M) { --m; ++M; }
- const float ca = (float)(M-m)/(_height - 1);
- bool init_hatch = true;
- // Draw graph edges
- switch (plot_type%4) {
- case 1 : { // Segments
- int oX = 0, oY = (int)cimg::round((data[0] - m)/ca);
- if (siz==1) {
- const int Y = (int)cimg::round((*data - m)/ca);
- draw_line(0,Y,width() - 1,Y,color,opacity,pattern);
- } else {
- const float fx = (float)_width/siz1;
- for (ulongT off = 1; off<siz; ++off) {
- const int
- X = (int)cimg::round(off*fx) - 1,
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_line(oX,oY,X,Y,color,opacity,pattern,init_hatch);
- oX = X; oY = Y;
- init_hatch = false;
- }
- }
- } break;
- case 2 : { // Spline
- const CImg<t> ndata(data._data,siz,1,1,1,true);
- int oY = (int)cimg::round((data[0] - m)/ca);
- cimg_forX(*this,x) {
- const int Y = (int)cimg::round((ndata._cubic_atX((float)x*siz1/width1)-m)/ca);
- if (x>0) draw_line(x,oY,x + 1,Y,color,opacity,pattern,init_hatch);
- init_hatch = false;
- oY = Y;
- }
- } break;
- case 3 : { // Bars
- const int Y0 = (int)cimg::round(-m/ca);
- const float fx = (float)_width/siz1;
- int oX = 0;
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round((off + 1)*fx) - 1,
- Y = (int)cimg::round((data[off] - m)/ca);
- draw_rectangle(oX,Y0,X,Y,color,opacity).
- draw_line(oX,Y,oX,Y0,color2.data(),opacity).
- draw_line(oX,Y0,X,Y0,Y<=Y0?color2.data():color1.data(),opacity).
- draw_line(X,Y,X,Y0,color1.data(),opacity).
- draw_line(oX,Y,X,Y,Y<=Y0?color1.data():color2.data(),opacity);
- oX = X + 1;
- }
- } break;
- default : break; // No edges
- }
- // Draw graph points
- const unsigned int wb2 = plot_type==3?_width1/(2*siz):0;
- const float fx = (float)_width1/siz1;
- switch (vertex_type%8) {
- case 1 : { // Point
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_point(X,Y,color,opacity);
- }
- } break;
- case 2 : { // Straight Cross
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_line(X - 3,Y,X + 3,Y,color,opacity).draw_line(X,Y - 3,X,Y + 3,color,opacity);
- }
- } break;
- case 3 : { // Diagonal Cross
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_line(X - 3,Y - 3,X + 3,Y + 3,color,opacity).draw_line(X - 3,Y + 3,X + 3,Y - 3,color,opacity);
- }
- } break;
- case 4 : { // Filled Circle
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_circle(X,Y,3,color,opacity);
- }
- } break;
- case 5 : { // Outlined circle
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_circle(X,Y,3,color,opacity,~0U);
- }
- } break;
- case 6 : { // Square
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_rectangle(X - 3,Y - 3,X + 3,Y + 3,color,opacity,~0U);
- }
- } break;
- case 7 : { // Diamond
- cimg_foroff(data,off) {
- const int
- X = (int)cimg::round(off*fx + wb2),
- Y = (int)cimg::round((data[off]-m)/ca);
- draw_line(X,Y - 4,X + 4,Y,color,opacity).
- draw_line(X + 4,Y,X,Y + 4,color,opacity).
- draw_line(X,Y + 4,X - 4,Y,color,opacity).
- draw_line(X - 4,Y,X,Y - 4,color,opacity);
- }
- } break;
- default : break; // No points
- }
- return *this;
- }
- bool _draw_fill(const int x, const int y, const int z,
- const CImg<T>& ref, const float tolerance2) const {
- const T *ptr1 = data(x,y,z), *ptr2 = ref._data;
- const ulongT off = _width*_height*_depth;
- float diff = 0;
- cimg_forC(*this,c) { diff += cimg::sqr(*ptr1 - *(ptr2++)); ptr1+=off; }
- return diff<=tolerance2;
- }
- //! Draw filled 3D region with the flood fill algorithm.
- /**
- \param x0 X-coordinate of the starting point of the region to fill.
- \param y0 Y-coordinate of the starting point of the region to fill.
- \param z0 Z-coordinate of the starting point of the region to fill.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param[out] region Image that will contain the mask of the filled region mask, as an output.
- \param tolerance Tolerance concerning neighborhood values.
- \param opacity Opacity of the drawing.
- \param is_high_connectivity Tells if 8-connexity must be used.
- \return \c region is initialized with the binary mask of the filled region.
- **/
- template<typename tc, typename t>
- CImg<T>& draw_fill(const int x0, const int y0, const int z0,
- const tc *const color, const float opacity,
- CImg<t> ®ion,
- const float tolerance = 0,
- const bool is_high_connectivity = false) {
- #define _draw_fill_push(x,y,z) if (N>=stack._width) stack.resize(2*N + 1,1,1,3,0); \
- stack[N] = x; stack(N,1) = y; stack(N++,2) = z
- #define _draw_fill_pop(x,y,z) x = stack[--N]; y = stack(N,1); z = stack(N,2)
- #define _draw_fill_is_inside(x,y,z) !_region(x,y,z) && _draw_fill(x,y,z,ref,tolerance2)
- if (!containsXYZC(x0,y0,z0,0)) return *this;
- const float nopacity = cimg::abs((float)opacity), copacity = 1 - std::max((float)opacity,0.f);
- const float tolerance2 = cimg::sqr(tolerance);
- const CImg<T> ref = get_vector_at(x0,y0,z0);
- CImg<uintT> stack(256,1,1,3);
- CImg<ucharT> _region(_width,_height,_depth,1,0);
- unsigned int N = 0;
- int x, y, z;
- _draw_fill_push(x0,y0,z0);
- while (N>0) {
- _draw_fill_pop(x,y,z);
- if (!_region(x,y,z)) {
- const int yp = y - 1, yn = y + 1, zp = z - 1, zn = z + 1;
- int xl = x, xr = x;
- // Using these booleans reduces the number of pushes drastically.
- bool is_yp = false, is_yn = false, is_zp = false, is_zn = false;
- for (int step = -1; step<2; step+=2) {
- while (x>=0 && x<width() && _draw_fill_is_inside(x,y,z)) {
- if (yp>=0 && _draw_fill_is_inside(x,yp,z)) {
- if (!is_yp) { _draw_fill_push(x,yp,z); is_yp = true; }
- } else is_yp = false;
- if (yn<height() && _draw_fill_is_inside(x,yn,z)) {
- if (!is_yn) { _draw_fill_push(x,yn,z); is_yn = true; }
- } else is_yn = false;
- if (depth()>1) {
- if (zp>=0 && _draw_fill_is_inside(x,y,zp)) {
- if (!is_zp) { _draw_fill_push(x,y,zp); is_zp = true; }
- } else is_zp = false;
- if (zn<depth() && _draw_fill_is_inside(x,y,zn)) {
- if (!is_zn) { _draw_fill_push(x,y,zn); is_zn = true; }
- } else is_zn = false;
- }
- if (is_high_connectivity) {
- const int xp = x - 1, xn = x + 1;
- if (yp>=0 && !is_yp) {
- if (xp>=0 && _draw_fill_is_inside(xp,yp,z)) {
- _draw_fill_push(xp,yp,z); if (step<0) is_yp = true;
- }
- if (xn<width() && _draw_fill_is_inside(xn,yp,z)) {
- _draw_fill_push(xn,yp,z); if (step>0) is_yp = true;
- }
- }
- if (yn<height() && !is_yn) {
- if (xp>=0 && _draw_fill_is_inside(xp,yn,z)) {
- _draw_fill_push(xp,yn,z); if (step<0) is_yn = true;
- }
- if (xn<width() && _draw_fill_is_inside(xn,yn,z)) {
- _draw_fill_push(xn,yn,z); if (step>0) is_yn = true;
- }
- }
- if (depth()>1) {
- if (zp>=0 && !is_zp) {
- if (xp>=0 && _draw_fill_is_inside(xp,y,zp)) {
- _draw_fill_push(xp,y,zp); if (step<0) is_zp = true;
- }
- if (xn<width() && _draw_fill_is_inside(xn,y,zp)) {
- _draw_fill_push(xn,y,zp); if (step>0) is_zp = true;
- }
- if (yp>=0 && !is_yp) {
- if (_draw_fill_is_inside(x,yp,zp)) { _draw_fill_push(x,yp,zp); }
- if (xp>=0 && _draw_fill_is_inside(xp,yp,zp)) { _draw_fill_push(xp,yp,zp); }
- if (xn<width() && _draw_fill_is_inside(xn,yp,zp)) { _draw_fill_push(xn,yp,zp); }
- }
- if (yn<height() && !is_yn) {
- if (_draw_fill_is_inside(x,yn,zp)) { _draw_fill_push(x,yn,zp); }
- if (xp>=0 && _draw_fill_is_inside(xp,yn,zp)) { _draw_fill_push(xp,yn,zp); }
- if (xn<width() && _draw_fill_is_inside(xn,yn,zp)) { _draw_fill_push(xn,yn,zp); }
- }
- }
- if (zn<depth() && !is_zn) {
- if (xp>=0 && _draw_fill_is_inside(xp,y,zn)) {
- _draw_fill_push(xp,y,zn); if (step<0) is_zn = true;
- }
- if (xn<width() && _draw_fill_is_inside(xn,y,zn)) {
- _draw_fill_push(xn,y,zn); if (step>0) is_zn = true;
- }
- if (yp>=0 && !is_yp) {
- if (_draw_fill_is_inside(x,yp,zn)) { _draw_fill_push(x,yp,zn); }
- if (xp>=0 && _draw_fill_is_inside(xp,yp,zn)) { _draw_fill_push(xp,yp,zn); }
- if (xn<width() && _draw_fill_is_inside(xn,yp,zn)) { _draw_fill_push(xn,yp,zn); }
- }
- if (yn<height() && !is_yn) {
- if (_draw_fill_is_inside(x,yn,zn)) { _draw_fill_push(x,yn,zn); }
- if (xp>=0 && _draw_fill_is_inside(xp,yn,zn)) { _draw_fill_push(xp,yn,zn); }
- if (xn<width() && _draw_fill_is_inside(xn,yn,zn)) { _draw_fill_push(xn,yn,zn); }
- }
- }
- }
- }
- x+=step;
- }
- if (step<0) { xl = ++x; x = xr + 1; is_yp = is_yn = is_zp = is_zn = false; }
- else xr = --x;
- }
- std::memset(_region.data(xl,y,z),1,xr - xl + 1);
- if (opacity==1) {
- if (sizeof(T)==1) {
- const int dx = xr - xl + 1;
- cimg_forC(*this,c) std::memset(data(xl,y,z,c),(int)color[c],dx);
- } else cimg_forC(*this,c) {
- const T val = (T)color[c];
- T *ptri = data(xl,y,z,c); for (int k = xl; k<=xr; ++k) *(ptri++) = val;
- }
- } else cimg_forC(*this,c) {
- const T val = (T)(color[c]*nopacity);
- T *ptri = data(xl,y,z,c); for (int k = xl; k<=xr; ++k) { *ptri = (T)(val + *ptri*copacity); ++ptri; }
- }
- }
- }
- _region.move_to(region);
- return *this;
- }
- //! Draw filled 3D region with the flood fill algorithm \simplification.
- template<typename tc>
- CImg<T>& draw_fill(const int x0, const int y0, const int z0,
- const tc *const color, const float opacity=1,
- const float tolerance=0, const bool is_high_connexity=false) {
- CImg<ucharT> tmp;
- return draw_fill(x0,y0,z0,color,opacity,tmp,tolerance,is_high_connexity);
- }
- //! Draw filled 2D region with the flood fill algorithm \simplification.
- template<typename tc>
- CImg<T>& draw_fill(const int x0, const int y0,
- const tc *const color, const float opacity=1,
- const float tolerance=0, const bool is_high_connexity=false) {
- CImg<ucharT> tmp;
- return draw_fill(x0,y0,0,color,opacity,tmp,tolerance,is_high_connexity);
- }
- //! Draw a random plasma texture.
- /**
- \param alpha Alpha-parameter.
- \param beta Beta-parameter.
- \param scale Scale-parameter.
- \note Use the mid-point algorithm to render.
- **/
- CImg<T>& draw_plasma(const float alpha=1, const float beta=0, const unsigned int scale=8) {
- if (is_empty()) return *this;
- const int w = width(), h = height();
- const Tfloat m = (Tfloat)cimg::type<T>::min(), M = (Tfloat)cimg::type<T>::max();
- cimg_uint64 rng = (cimg::_rand(),cimg::rng());
- cimg_forZC(*this,z,c) {
- CImg<T> ref = get_shared_slice(z,c);
- for (int delta = 1<<std::min(scale,31U); delta>1; delta>>=1) {
- const int delta2 = delta>>1;
- const float r = alpha*delta + beta;
- // Square step.
- for (int y0 = 0; y0<h; y0+=delta)
- for (int x0 = 0; x0<w; x0+=delta) {
- const int x1 = (x0 + delta)%w, y1 = (y0 + delta)%h, xc = (x0 + delta2)%w, yc = (y0 + delta2)%h;
- const Tfloat val = (Tfloat)(0.25f*(ref(x0,y0) + ref(x0,y1) + ref(x0,y1) + ref(x1,y1)) +
- r*cimg::rand(-1,1,&rng));
- ref(xc,yc) = (T)(val<m?m:val>M?M:val);
- }
- // Diamond steps.
- for (int y = -delta2; y<h; y+=delta)
- for (int x0=0; x0<w; x0+=delta) {
- const int y0 = cimg::mod(y,h), x1 = (x0 + delta)%w, y1 = (y + delta)%h,
- xc = (x0 + delta2)%w, yc = (y + delta2)%h;
- const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
- r*cimg::rand(-1,1,&rng));
- ref(xc,yc) = (T)(val<m?m:val>M?M:val);
- }
- for (int y0 = 0; y0<h; y0+=delta)
- for (int x = -delta2; x<w; x+=delta) {
- const int x0 = cimg::mod(x,w), x1 = (x + delta)%w, y1 = (y0 + delta)%h,
- xc = (x + delta2)%w, yc = (y0 + delta2)%h;
- const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
- r*cimg::rand(-1,1,&rng));
- ref(xc,yc) = (T)(val<m?m:val>M?M:val);
- }
- for (int y = -delta2; y<h; y+=delta)
- for (int x = -delta2; x<w; x+=delta) {
- const int x0 = cimg::mod(x,w), y0 = cimg::mod(y,h), x1 = (x + delta)%w, y1 = (y + delta)%h,
- xc = (x + delta2)%w, yc = (y + delta2)%h;
- const Tfloat val = (Tfloat)(0.25f*(ref(xc,y0) + ref(x0,yc) + ref(xc,y1) + ref(x1,yc)) +
- r*cimg::rand(-1,1,&rng));
- ref(xc,yc) = (T)(val<m?m:val>M?M:val);
- }
- }
- }
- cimg::srand(rng);
- return *this;
- }
- //! Draw a quadratic Mandelbrot or Julia 2D fractal.
- /**
- \param x0 X-coordinate of the upper-left pixel.
- \param y0 Y-coordinate of the upper-left pixel.
- \param x1 X-coordinate of the lower-right pixel.
- \param y1 Y-coordinate of the lower-right pixel.
- \param colormap Colormap.
- \param opacity Drawing opacity.
- \param z0r Real part of the upper-left fractal vertex.
- \param z0i Imaginary part of the upper-left fractal vertex.
- \param z1r Real part of the lower-right fractal vertex.
- \param z1i Imaginary part of the lower-right fractal vertex.
- \param iteration_max Maximum number of iterations for each estimated point.
- \param is_normalized_iteration Tells if iterations are normalized.
- \param is_julia_set Tells if the Mandelbrot or Julia set is rendered.
- \param param_r Real part of the Julia set parameter.
- \param param_i Imaginary part of the Julia set parameter.
- \note Fractal rendering is done by the Escape Time Algorithm.
- **/
- template<typename tc>
- CImg<T>& draw_mandelbrot(const int x0, const int y0, const int x1, const int y1,
- const CImg<tc>& colormap, const float opacity=1,
- const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2,
- const unsigned int iteration_max=255,
- const bool is_normalized_iteration=false,
- const bool is_julia_set=false,
- const double param_r=0, const double param_i=0) {
- if (is_empty()) return *this;
- CImg<tc> palette;
- if (colormap) palette.assign(colormap._data,colormap.size()/colormap._spectrum,1,1,colormap._spectrum,true);
- if (palette && palette._spectrum!=_spectrum)
- throw CImgArgumentException(_cimg_instance
- "draw_mandelbrot(): Instance and specified colormap (%u,%u,%u,%u,%p) have "
- "incompatible dimensions.",
- cimg_instance,
- colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f), ln2 = (float)std::log(2.);
- const int
- _x0 = cimg::cut(x0,0,width() - 1),
- _y0 = cimg::cut(y0,0,height() - 1),
- _x1 = cimg::cut(x1,0,width() - 1),
- _y1 = cimg::cut(y1,0,height() - 1);
- cimg_pragma_openmp(parallel for cimg_openmp_collapse(2)
- cimg_openmp_if((1 + _x1 - _x0)*(1 + _y1 - _y0)>=(cimg_openmp_sizefactor)*2048))
- for (int q = _y0; q<=_y1; ++q)
- for (int p = _x0; p<=_x1; ++p) {
- unsigned int iteration = 0;
- const double x = z0r + p*(z1r-z0r)/_width, y = z0i + q*(z1i-z0i)/_height;
- double zr, zi, cr, ci;
- if (is_julia_set) { zr = x; zi = y; cr = param_r; ci = param_i; }
- else { zr = param_r; zi = param_i; cr = x; ci = y; }
- for (iteration=1; zr*zr + zi*zi<=4 && iteration<=iteration_max; ++iteration) {
- const double temp = zr*zr - zi*zi + cr;
- zi = 2*zr*zi + ci;
- zr = temp;
- }
- if (iteration>iteration_max) {
- if (palette) {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette(0,c);
- else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(0,c)*nopacity + (*this)(p,q,0,c)*copacity);
- } else {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)0;
- else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)((*this)(p,q,0,c)*copacity);
- }
- } else if (is_normalized_iteration) {
- const float
- normz = (float)cimg::abs(zr*zr + zi*zi),
- niteration = (float)(iteration + 1 - std::log(std::log(normz))/ln2);
- if (palette) {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._linear_atX(niteration,c);
- else cimg_forC(*this,c)
- (*this)(p,q,0,c) = (T)(palette._linear_atX(niteration,c)*nopacity + (*this)(p,q,0,c)*copacity);
- } else {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)niteration;
- else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(niteration*nopacity + (*this)(p,q,0,c)*copacity);
- }
- } else {
- if (palette) {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)palette._atX(iteration,c);
- else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(palette(iteration,c)*nopacity + (*this)(p,q,0,c)*copacity);
- } else {
- if (opacity>=1) cimg_forC(*this,c) (*this)(p,q,0,c) = (T)iteration;
- else cimg_forC(*this,c) (*this)(p,q,0,c) = (T)(iteration*nopacity + (*this)(p,q,0,c)*copacity);
- }
- }
- }
- return *this;
- }
- //! Draw a quadratic Mandelbrot or Julia 2D fractal \overloading.
- template<typename tc>
- CImg<T>& draw_mandelbrot(const CImg<tc>& colormap, const float opacity=1,
- const double z0r=-2, const double z0i=-2, const double z1r=2, const double z1i=2,
- const unsigned int iteration_max=255,
- const bool is_normalized_iteration=false,
- const bool is_julia_set=false,
- const double param_r=0, const double param_i=0) {
- return draw_mandelbrot(0,0,_width - 1,_height - 1,colormap,opacity,
- z0r,z0i,z1r,z1i,iteration_max,is_normalized_iteration,is_julia_set,param_r,param_i);
- }
- //! Draw a 1D gaussian function.
- /**
- \param xc X-coordinate of the gaussian center.
- \param sigma Standard variation of the gaussian distribution.
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename tc>
- CImg<T>& draw_gaussian(const float xc, const float sigma,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_gaussian(): Specified color is (null).",
- cimg_instance);
- const float sigma2 = 2*sigma*sigma, nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- const ulongT whd = (ulongT)_width*_height*_depth;
- const tc *col = color;
- cimg_forX(*this,x) {
- const float dx = (x - xc), val = (float)std::exp(-dx*dx/sigma2);
- T *ptrd = data(x,0,0,0);
- if (opacity>=1) cimg_forC(*this,c) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
- else cimg_forC(*this,c) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
- col-=_spectrum;
- }
- return *this;
- }
- //! Draw a 2D gaussian function.
- /**
- \param xc X-coordinate of the gaussian center.
- \param yc Y-coordinate of the gaussian center.
- \param tensor Covariance matrix (must be 2x2).
- \param color Pointer to \c spectrum() consecutive values, defining the drawing color.
- \param opacity Drawing opacity.
- **/
- template<typename t, typename tc>
- CImg<T>& draw_gaussian(const float xc, const float yc, const CImg<t>& tensor,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- if (tensor._width!=2 || tensor._height!=2 || tensor._depth!=1 || tensor._spectrum!=1)
- throw CImgArgumentException(_cimg_instance
- "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 2x2 matrix.",
- cimg_instance,
- tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data);
- if (!color)
- throw CImgArgumentException(_cimg_instance
- "draw_gaussian(): Specified color is (null).",
- cimg_instance);
- typedef typename CImg<t>::Tfloat tfloat;
- const CImg<tfloat> invT = tensor.get_invert(), invT2 = (invT*invT)/=-2.;
- const tfloat a = invT2(0,0), b = 2*invT2(1,0), c = invT2(1,1);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- const ulongT whd = (ulongT)_width*_height*_depth;
- const tc *col = color;
- float dy = -yc;
- cimg_forY(*this,y) {
- float dx = -xc;
- cimg_forX(*this,x) {
- const float val = (float)std::exp(a*dx*dx + b*dx*dy + c*dy*dy);
- T *ptrd = data(x,y,0,0);
- if (opacity>=1) cimg_forC(*this,k) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
- else cimg_forC(*this,k) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
- col-=_spectrum;
- ++dx;
- }
- ++dy;
- }
- return *this;
- }
- //! Draw a 2D gaussian function \overloading.
- template<typename tc>
- CImg<T>& draw_gaussian(const int xc, const int yc, const float r1, const float r2, const float ru, const float rv,
- const tc *const color, const float opacity=1) {
- const double
- a = r1*ru*ru + r2*rv*rv,
- b = (r1-r2)*ru*rv,
- c = r1*rv*rv + r2*ru*ru;
- const CImg<Tfloat> tensor(2,2,1,1, a,b,b,c);
- return draw_gaussian(xc,yc,tensor,color,opacity);
- }
- //! Draw a 2D gaussian function \overloading.
- template<typename tc>
- CImg<T>& draw_gaussian(const float xc, const float yc, const float sigma,
- const tc *const color, const float opacity=1) {
- return draw_gaussian(xc,yc,CImg<floatT>::diagonal(sigma,sigma),color,opacity);
- }
- //! Draw a 3D gaussian function \overloading.
- template<typename t, typename tc>
- CImg<T>& draw_gaussian(const float xc, const float yc, const float zc, const CImg<t>& tensor,
- const tc *const color, const float opacity=1) {
- if (is_empty()) return *this;
- typedef typename CImg<t>::Tfloat tfloat;
- if (tensor._width!=3 || tensor._height!=3 || tensor._depth!=1 || tensor._spectrum!=1)
- throw CImgArgumentException(_cimg_instance
- "draw_gaussian(): Specified tensor (%u,%u,%u,%u,%p) is not a 3x3 matrix.",
- cimg_instance,
- tensor._width,tensor._height,tensor._depth,tensor._spectrum,tensor._data);
- const CImg<tfloat> invT = tensor.get_invert(), invT2 = (invT*invT)/=-2.;
- 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);
- const float nopacity = cimg::abs(opacity), copacity = 1 - std::max(opacity,0.f);
- const ulongT whd = (ulongT)_width*_height*_depth;
- const tc *col = color;
- cimg_forXYZ(*this,x,y,z) {
- const float
- dx = (x - xc), dy = (y - yc), dz = (z - zc),
- val = (float)std::exp(a*dx*dx + b*dx*dy + c*dx*dz + d*dy*dy + e*dy*dz + f*dz*dz);
- T *ptrd = data(x,y,z,0);
- if (opacity>=1) cimg_forC(*this,k) { *ptrd = (T)(val*(*col++)); ptrd+=whd; }
- else cimg_forC(*this,k) { *ptrd = (T)(nopacity*val*(*col++) + *ptrd*copacity); ptrd+=whd; }
- col-=_spectrum;
- }
- return *this;
- }
- //! Draw a 3D gaussian function \overloading.
- template<typename tc>
- CImg<T>& draw_gaussian(const float xc, const float yc, const float zc, const float sigma,
- const tc *const color, const float opacity=1) {
- return draw_gaussian(xc,yc,zc,CImg<floatT>::diagonal(sigma,sigma,sigma),color,opacity);
- }
- //! Draw a 3D object.
- /**
- \param x0 X-coordinate of the 3D object position
- \param y0 Y-coordinate of the 3D object position
- \param z0 Z-coordinate of the 3D object position
- \param vertices Image Nx3 describing 3D point coordinates
- \param primitives List of P primitives
- \param colors List of P color (or textures)
- \param opacities Image or list of P opacities
- \param render_type d Render type (0=Points, 1=Lines, 2=Faces (no light), 3=Faces (flat), 4=Faces(Gouraud)
- \param is_double_sided Tells if object faces have two sides or are oriented.
- \param focale length of the focale (0 for parallel projection)
- \param lightx X-coordinate of the light
- \param lighty Y-coordinate of the light
- \param lightz Z-coordinate of the light
- \param specular_lightness Amount of specular light.
- \param specular_shininess Shininess of the object
- \param g_opacity Global opacity of the object.
- **/
- template<typename tp, typename tf, typename tc, typename to>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImg<to>& opacities,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type,
- is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- //! Draw a 3D object \simplification.
- template<typename tp, typename tf, typename tc, typename to, typename tz>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImg<to>& opacities,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,1);
- }
- #ifdef cimg_use_board
- template<typename tp, typename tf, typename tc, typename to>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImg<to>& opacities,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type,
- is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- template<typename tp, typename tf, typename tc, typename to, typename tz>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImg<to>& opacities,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,1);
- }
- #endif
- //! Draw a 3D object \simplification.
- template<typename tp, typename tf, typename tc, typename to>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImgList<to>& opacities,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_type,
- is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- //! Draw a 3D object \simplification.
- template<typename tp, typename tf, typename tc, typename to, typename tz>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImgList<to>& opacities,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return _draw_object3d(0,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,1);
- }
- #ifdef cimg_use_board
- template<typename tp, typename tf, typename tc, typename to>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImgList<to>& opacities,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(board,x0,y0,z0,vertices,primitives,colors,opacities,render_type,
- is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- template<typename tp, typename tf, typename tc, typename to, typename tz>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors, const CImgList<to>& opacities,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return _draw_object3d((void*)&board,zbuffer,x0,y0,z0,vertices,primitives,colors,opacities,
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,1);
- }
- #endif
- //! Draw a 3D object \simplification.
- template<typename tp, typename tf, typename tc>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- //! Draw a 3D object \simplification.
- template<typename tp, typename tf, typename tc, typename tz>
- CImg<T>& draw_object3d(const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,zbuffer);
- }
- #ifdef cimg_use_board
- template<typename tp, typename tf, typename tc, typename to>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const unsigned int render_type=4,
- const bool is_double_sided=false, const float focale=700,
- const float lightx=0, const float lighty=0, const float lightz=-5e8,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const float g_opacity=1) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,CImg<floatT>::empty());
- }
- template<typename tp, typename tf, typename tc, typename to, typename tz>
- CImg<T>& draw_object3d(LibBoard::Board& board,
- const float x0, const float y0, const float z0,
- const CImg<tp>& vertices, const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, CImg<tz>& zbuffer) {
- return draw_object3d(x0,y0,z0,vertices,primitives,colors,CImg<floatT>::const_empty(),
- render_type,is_double_sided,focale,lightx,lighty,lightz,
- specular_lightness,specular_shininess,g_opacity,zbuffer);
- }
- #endif
- template<typename t, typename to>
- static float __draw_object3d(const CImgList<t>& opacities, const unsigned int n_primitive, CImg<to>& opacity) {
- if (n_primitive>=opacities._width || opacities[n_primitive].is_empty()) { opacity.assign(); return 1; }
- if (opacities[n_primitive].size()==1) { opacity.assign(); return opacities(n_primitive,0); }
- opacity.assign(opacities[n_primitive],true);
- return 1.f;
- }
- template<typename t, typename to>
- static float __draw_object3d(const CImg<t>& opacities, const unsigned int n_primitive, CImg<to>& opacity) {
- opacity.assign();
- return n_primitive>=opacities._width?1.f:(float)opacities[n_primitive];
- }
- template<typename t>
- static float ___draw_object3d(const CImgList<t>& opacities, const unsigned int n_primitive) {
- return n_primitive<opacities._width && opacities[n_primitive].size()==1?(float)opacities(n_primitive,0):1.f;
- }
- template<typename t>
- static float ___draw_object3d(const CImg<t>& opacities, const unsigned int n_primitive) {
- return n_primitive<opacities._width?(float)opacities[n_primitive]:1.f;
- }
- template<typename tz, typename tp, typename tf, typename tc, typename to>
- CImg<T>& _draw_object3d(void *const pboard, CImg<tz>& zbuffer,
- const float X, const float Y, const float Z,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const unsigned int render_type,
- const bool is_double_sided, const float focale,
- const float lightx, const float lighty, const float lightz,
- const float specular_lightness, const float specular_shininess,
- const float g_opacity, const float sprite_scale) {
- typedef typename cimg::superset2<tp,tz,float>::type tpfloat;
- typedef typename to::value_type _to;
- if (is_empty() || !vertices || !primitives) return *this;
- CImg<char> error_message(1024);
- if (!vertices.is_object3d(primitives,colors,opacities,false,error_message))
- throw CImgArgumentException(_cimg_instance
- "draw_object3d(): Invalid specified 3D object (%u,%u) (%s).",
- cimg_instance,vertices._width,primitives._width,error_message.data());
- #ifndef cimg_use_board
- if (pboard) return *this;
- #endif
- if (render_type==5) cimg::mutex(10); // Static variable used in this case, breaks thread-safety
- const float
- nspec = 1 - (specular_lightness<0.f?0.f:(specular_lightness>1.f?1.f:specular_lightness)),
- nspec2 = 1 + (specular_shininess<0.f?0.f:specular_shininess),
- nsl1 = (nspec2 - 1)/cimg::sqr(nspec - 1),
- nsl2 = 1 - 2*nsl1*nspec,
- nsl3 = nspec2 - nsl1 - nsl2;
- // Create light texture for phong-like rendering.
- CImg<floatT> light_texture;
- if (render_type==5) {
- if (colors._width>primitives._width) {
- static CImg<floatT> default_light_texture;
- static const tc *lptr = 0;
- static tc ref_values[64] = { 0 };
- const CImg<tc>& img = colors.back();
- bool is_same_texture = (lptr==img._data);
- if (is_same_texture)
- for (unsigned int r = 0, j = 0; j<8; ++j)
- for (unsigned int i = 0; i<8; ++i)
- if (ref_values[r++]!=img(i*img._width/9,j*img._height/9,0,(i + j)%img._spectrum)) {
- is_same_texture = false; break;
- }
- if (!is_same_texture || default_light_texture._spectrum<_spectrum) {
- (default_light_texture.assign(img,false)/=255).resize(-100,-100,1,_spectrum);
- lptr = colors.back().data();
- for (unsigned int r = 0, j = 0; j<8; ++j)
- for (unsigned int i = 0; i<8; ++i)
- ref_values[r++] = img(i*img._width/9,j*img._height/9,0,(i + j)%img._spectrum);
- }
- light_texture.assign(default_light_texture,true);
- } else {
- static CImg<floatT> default_light_texture;
- static float olightx = 0, olighty = 0, olightz = 0, ospecular_shininess = 0;
- if (!default_light_texture ||
- lightx!=olightx || lighty!=olighty || lightz!=olightz ||
- specular_shininess!=ospecular_shininess || default_light_texture._spectrum<_spectrum) {
- default_light_texture.assign(512,512);
- const float
- dlx = lightx - X,
- dly = lighty - Y,
- dlz = lightz - Z,
- nl = cimg::hypot(dlx,dly,dlz),
- nlx = (default_light_texture._width - 1)/2*(1 + dlx/nl),
- nly = (default_light_texture._height - 1)/2*(1 + dly/nl),
- white[] = { 1 };
- default_light_texture.draw_gaussian(nlx,nly,default_light_texture._width/3.f,white);
- cimg_forXY(default_light_texture,x,y) {
- const float factor = default_light_texture(x,y);
- if (factor>nspec) default_light_texture(x,y) = std::min(2.f,nsl1*factor*factor + nsl2*factor + nsl3);
- }
- default_light_texture.resize(-100,-100,1,_spectrum);
- olightx = lightx; olighty = lighty; olightz = lightz; ospecular_shininess = specular_shininess;
- }
- light_texture.assign(default_light_texture,true);
- }
- }
- // Compute 3D to 2D projection.
- CImg<tpfloat> projections(vertices._width,2);
- tpfloat parallzmin = cimg::type<tpfloat>::max();
- const float absfocale = focale?cimg::abs(focale):0;
- if (absfocale) {
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(projections.size(),4096))
- cimg_forX(projections,l) { // Perspective projection
- const tpfloat
- x = (tpfloat)vertices(l,0),
- y = (tpfloat)vertices(l,1),
- z = (tpfloat)vertices(l,2);
- const tpfloat projectedz = z + Z + absfocale;
- projections(l,1) = Y + absfocale*y/projectedz;
- projections(l,0) = X + absfocale*x/projectedz;
- }
- } else {
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(projections.size(),4096))
- cimg_forX(projections,l) { // Parallel projection
- const tpfloat
- x = (tpfloat)vertices(l,0),
- y = (tpfloat)vertices(l,1),
- z = (tpfloat)vertices(l,2);
- if (z<parallzmin) parallzmin = z;
- projections(l,1) = Y + y;
- projections(l,0) = X + x;
- }
- }
- const float _focale = absfocale?absfocale:(1e5f-parallzmin);
- float zmax = 0;
- if (zbuffer) zmax = vertices.get_shared_row(2).max();
- // Compute visible primitives.
- CImg<uintT> visibles(primitives._width,1,1,1,~0U);
- CImg<tpfloat> zrange(primitives._width);
- const tpfloat zmin = absfocale?(tpfloat)(1.5f - absfocale):cimg::type<tpfloat>::min();
- bool is_forward = zbuffer?true:false;
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(primitives.size(),4096))
- cimglist_for(primitives,l) {
- const CImg<tf>& primitive = primitives[l];
- switch (primitive.size()) {
- case 1 : { // Point
- CImg<_to> _opacity;
- __draw_object3d(opacities,l,_opacity);
- if (l<=colors.width() && (colors[l].size()!=_spectrum || _opacity)) is_forward = false;
- const unsigned int i0 = (unsigned int)primitive(0);
- const tpfloat z0 = Z + vertices(i0,2);
- if (z0>zmin) {
- visibles(l) = (unsigned int)l;
- zrange(l) = z0;
- }
- } break;
- case 5 : { // Sphere
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1);
- const tpfloat
- Xc = 0.5f*((float)vertices(i0,0) + (float)vertices(i1,0)),
- Yc = 0.5f*((float)vertices(i0,1) + (float)vertices(i1,1)),
- Zc = 0.5f*((float)vertices(i0,2) + (float)vertices(i1,2)),
- _zc = Z + Zc,
- zc = _zc + _focale,
- xc = X + Xc*(absfocale?absfocale/zc:1),
- yc = Y + Yc*(absfocale?absfocale/zc:1),
- radius = 0.5f*cimg::hypot(vertices(i1,0) - vertices(i0,0),
- vertices(i1,1) - vertices(i0,1),
- vertices(i1,2) - vertices(i0,2))*(absfocale?absfocale/zc:1),
- xm = xc - radius,
- ym = yc - radius,
- xM = xc + radius,
- yM = yc + radius;
- if (xM>=0 && xm<_width && yM>=0 && ym<_height && _zc>zmin) {
- visibles(l) = (unsigned int)l;
- zrange(l) = _zc;
- }
- is_forward = false;
- } break;
- case 2 : case 6 : { // Segment
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1);
- const tpfloat
- x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
- x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2);
- tpfloat xm, xM, ym, yM;
- if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
- if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
- if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin) {
- visibles(l) = (unsigned int)l;
- zrange(l) = (z0 + z1)/2;
- }
- } break;
- case 3 : case 9 : { // Triangle
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2);
- const tpfloat
- x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
- x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2),
- x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2);
- tpfloat xm, xM, ym, yM;
- if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
- if (x2<xm) xm = x2;
- if (x2>xM) xM = x2;
- if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
- if (y2<ym) ym = y2;
- if (y2>yM) yM = y2;
- if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin) {
- const tpfloat d = (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0);
- if (is_double_sided || d<0) {
- visibles(l) = (unsigned int)l;
- zrange(l) = (z0 + z1 + z2)/3;
- }
- }
- } break;
- case 4 : case 12 : { // Quadrangle
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2),
- i3 = (unsigned int)primitive(3);
- const tpfloat
- x0 = projections(i0,0), y0 = projections(i0,1), z0 = Z + vertices(i0,2),
- x1 = projections(i1,0), y1 = projections(i1,1), z1 = Z + vertices(i1,2),
- x2 = projections(i2,0), y2 = projections(i2,1), z2 = Z + vertices(i2,2),
- x3 = projections(i3,0), y3 = projections(i3,1), z3 = Z + vertices(i3,2);
- tpfloat xm, xM, ym, yM;
- if (x0<x1) { xm = x0; xM = x1; } else { xm = x1; xM = x0; }
- if (x2<xm) xm = x2;
- if (x2>xM) xM = x2;
- if (x3<xm) xm = x3;
- if (x3>xM) xM = x3;
- if (y0<y1) { ym = y0; yM = y1; } else { ym = y1; yM = y0; }
- if (y2<ym) ym = y2;
- if (y2>yM) yM = y2;
- if (y3<ym) ym = y3;
- if (y3>yM) yM = y3;
- if (xM>=0 && xm<_width && yM>=0 && ym<_height && z0>zmin && z1>zmin && z2>zmin && z3>zmin) {
- const float d = (x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0);
- if (is_double_sided || d<0) {
- visibles(l) = (unsigned int)l;
- zrange(l) = (z0 + z1 + z2 + z3)/4;
- }
- }
- } break;
- default :
- if (render_type==5) cimg::mutex(10,0);
- throw CImgArgumentException(_cimg_instance
- "draw_object3d(): Invalid primitive[%u] with size %u "
- "(should have size 1,2,3,4,5,6,9 or 12).",
- cimg_instance,
- l,primitive.size());
- }
- }
- // Force transparent primitives to be drawn last when zbuffer is activated
- // (and if object contains no spheres or sprites).
- if (is_forward)
- cimglist_for(primitives,l)
- if (___draw_object3d(opacities,l)!=1) zrange(l) = 2*zmax - zrange(l);
- // Sort only visibles primitives.
- unsigned int *p_visibles = visibles._data;
- tpfloat *p_zrange = zrange._data;
- const tpfloat *ptrz = p_zrange;
- cimg_for(visibles,ptr,unsigned int) {
- if (*ptr!=~0U) { *(p_visibles++) = *ptr; *(p_zrange++) = *ptrz; }
- ++ptrz;
- }
- const unsigned int nb_visibles = (unsigned int)(p_zrange - zrange._data);
- if (!nb_visibles) {
- if (render_type==5) cimg::mutex(10,0);
- return *this;
- }
- CImg<uintT> permutations;
- CImg<tpfloat>(zrange._data,nb_visibles,1,1,1,true).sort(permutations,is_forward);
- // Compute light properties
- CImg<floatT> lightprops;
- switch (render_type) {
- case 3 : { // Flat Shading
- lightprops.assign(nb_visibles);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
- cimg_forX(lightprops,l) {
- const CImg<tf>& primitive = primitives(visibles(permutations(l)));
- const unsigned int psize = (unsigned int)primitive.size();
- if (psize==3 || psize==4 || psize==9 || psize==12) {
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2);
- const tpfloat
- x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2),
- x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2),
- x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2),
- dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0,
- dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0,
- nx = dy1*dz2 - dz1*dy2,
- ny = dz1*dx2 - dx1*dz2,
- nz = dx1*dy2 - dy1*dx2,
- norm = 1e-5f + cimg::hypot(nx,ny,nz),
- lx = X + (x0 + x1 + x2)/3 - lightx,
- ly = Y + (y0 + y1 + y2)/3 - lighty,
- lz = Z + (z0 + z1 + z2)/3 - lightz,
- nl = 1e-5f + cimg::hypot(lx,ly,lz),
- factor = std::max(cimg::abs(-lx*nx - ly*ny - lz*nz)/(norm*nl),(tpfloat)0);
- lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3);
- } else lightprops[l] = 1;
- }
- } break;
- case 4 : // Gouraud Shading
- case 5 : { // Phong-Shading
- CImg<tpfloat> vertices_normals(vertices._width,6,1,1,0);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
- for (int l = 0; l<(int)nb_visibles; ++l) {
- const CImg<tf>& primitive = primitives[visibles(l)];
- const unsigned int psize = (unsigned int)primitive.size();
- const bool
- triangle_flag = (psize==3) || (psize==9),
- quadrangle_flag = (psize==4) || (psize==12);
- if (triangle_flag || quadrangle_flag) {
- const unsigned int
- i0 = (unsigned int)primitive(0),
- i1 = (unsigned int)primitive(1),
- i2 = (unsigned int)primitive(2),
- i3 = quadrangle_flag?(unsigned int)primitive(3):0;
- const tpfloat
- x0 = (tpfloat)vertices(i0,0), y0 = (tpfloat)vertices(i0,1), z0 = (tpfloat)vertices(i0,2),
- x1 = (tpfloat)vertices(i1,0), y1 = (tpfloat)vertices(i1,1), z1 = (tpfloat)vertices(i1,2),
- x2 = (tpfloat)vertices(i2,0), y2 = (tpfloat)vertices(i2,1), z2 = (tpfloat)vertices(i2,2),
- dx1 = x1 - x0, dy1 = y1 - y0, dz1 = z1 - z0,
- dx2 = x2 - x0, dy2 = y2 - y0, dz2 = z2 - z0,
- nnx = dy1*dz2 - dz1*dy2,
- nny = dz1*dx2 - dx1*dz2,
- nnz = dx1*dy2 - dy1*dx2,
- norm = 1e-5f + cimg::hypot(nnx,nny,nnz),
- nx = nnx/norm,
- ny = nny/norm,
- nz = nnz/norm;
- unsigned int ix = 0, iy = 1, iz = 2;
- if (is_double_sided && nz>0) { ix = 3; iy = 4; iz = 5; }
- vertices_normals(i0,ix)+=nx; vertices_normals(i0,iy)+=ny; vertices_normals(i0,iz)+=nz;
- vertices_normals(i1,ix)+=nx; vertices_normals(i1,iy)+=ny; vertices_normals(i1,iz)+=nz;
- vertices_normals(i2,ix)+=nx; vertices_normals(i2,iy)+=ny; vertices_normals(i2,iz)+=nz;
- if (quadrangle_flag) {
- vertices_normals(i3,ix)+=nx; vertices_normals(i3,iy)+=ny; vertices_normals(i3,iz)+=nz;
- }
- }
- }
- if (is_double_sided) cimg_forX(vertices_normals,p) {
- const float
- nx0 = vertices_normals(p,0), ny0 = vertices_normals(p,1), nz0 = vertices_normals(p,2),
- nx1 = vertices_normals(p,3), ny1 = vertices_normals(p,4), nz1 = vertices_normals(p,5),
- n0 = nx0*nx0 + ny0*ny0 + nz0*nz0, n1 = nx1*nx1 + ny1*ny1 + nz1*nz1;
- if (n1>n0) {
- vertices_normals(p,0) = -nx1;
- vertices_normals(p,1) = -ny1;
- vertices_normals(p,2) = -nz1;
- }
- }
- if (render_type==4) {
- lightprops.assign(vertices._width);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
- cimg_forX(lightprops,l) {
- const tpfloat
- nx = vertices_normals(l,0),
- ny = vertices_normals(l,1),
- nz = vertices_normals(l,2),
- norm = 1e-5f + cimg::hypot(nx,ny,nz),
- lx = X + vertices(l,0) - lightx,
- ly = Y + vertices(l,1) - lighty,
- lz = Z + vertices(l,2) - lightz,
- nl = 1e-5f + cimg::hypot(lx,ly,lz),
- factor = std::max((-lx*nx - ly*ny - lz*nz)/(norm*nl),(tpfloat)0);
- lightprops[l] = factor<=nspec?factor:(nsl1*factor*factor + nsl2*factor + nsl3);
- }
- } else {
- const unsigned int
- lw2 = light_texture._width/2 - 1,
- lh2 = light_texture._height/2 - 1;
- lightprops.assign(vertices._width,2);
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(nb_visibles,4096))
- cimg_forX(lightprops,l) {
- const tpfloat
- nx = vertices_normals(l,0),
- ny = vertices_normals(l,1),
- nz = vertices_normals(l,2),
- norm = 1e-5f + cimg::hypot(nx,ny,nz),
- nnx = nx/norm,
- nny = ny/norm;
- lightprops(l,0) = lw2*(1 + nnx);
- lightprops(l,1) = lh2*(1 + nny);
- }
- }
- } break;
- }
- // Draw visible primitives
- const CImg<tc> default_color(1,_spectrum,1,1,(tc)200);
- CImg<_to> _opacity;
- for (unsigned int l = 0; l<nb_visibles; ++l) {
- const unsigned int n_primitive = visibles(permutations(l));
- const CImg<tf>& primitive = primitives[n_primitive];
- const CImg<tc>
- &__color = n_primitive<colors._width?colors[n_primitive]:CImg<tc>(),
- _color = (__color && __color.size()!=_spectrum && __color._spectrum<_spectrum)?
- __color.get_resize(-100,-100,-100,_spectrum,0):CImg<tc>(),
- &color = _color?_color:(__color?__color:default_color);
- const tc *const pcolor = color._data;
- float opacity = __draw_object3d(opacities,n_primitive,_opacity);
- if (_opacity.is_empty()) opacity*=g_opacity;
- #ifdef cimg_use_board
- LibBoard::Board &board = *(LibBoard::Board*)pboard;
- #endif
- switch (primitive.size()) {
- case 1 : { // Colored point or sprite
- const unsigned int n0 = (unsigned int)primitive[0];
- const int x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1));
- if (_opacity.is_empty()) { // Scalar opacity
- if (color.size()==_spectrum) { // Colored point
- draw_point(x0,y0,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawDot((float)x0,height()-(float)y0);
- }
- #endif
- } else { // Sprite
- const tpfloat z = Z + vertices(n0,2);
- const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1);
- const unsigned int
- _sw = (unsigned int)(color._width*factor),
- _sh = (unsigned int)(color._height*factor),
- sw = _sw?_sw:1, sh = _sh?_sh:1;
- const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2;
- if (sw<=3*_width/2 && sh<=3*_height/2 &&
- (nx0 + (int)sw/2>=0 || nx0 - (int)sw/2<width() || ny0 + (int)sh/2>=0 || ny0 - (int)sh/2<height())) {
- const CImg<tc>
- _sprite = (sw!=color._width || sh!=color._height)?
- color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<tc>(),
- &sprite = _sprite?_sprite:color;
- draw_image(nx0,ny0,sprite,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128);
- board.setFillColor(LibBoard::Color::Null);
- board.drawRectangle((float)nx0,height() - (float)ny0,sw,sh);
- }
- #endif
- }
- }
- } else { // Opacity mask
- const tpfloat z = Z + vertices(n0,2);
- const float factor = focale<0?1:sprite_scale*(absfocale?absfocale/(z + absfocale):1);
- const unsigned int
- _sw = (unsigned int)(std::max(color._width,_opacity._width)*factor),
- _sh = (unsigned int)(std::max(color._height,_opacity._height)*factor),
- sw = _sw?_sw:1, sh = _sh?_sh:1;
- const int nx0 = x0 - (int)sw/2, ny0 = y0 - (int)sh/2;
- if (sw<=3*_width/2 && sh<=3*_height/2 &&
- (nx0 + (int)sw/2>=0 || nx0 - (int)sw/2<width() || ny0 + (int)sh/2>=0 || ny0 - (int)sh/2<height())) {
- const CImg<tc>
- _sprite = (sw!=color._width || sh!=color._height)?
- color.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<tc>(),
- &sprite = _sprite?_sprite:color;
- const CImg<_to>
- _nopacity = (sw!=_opacity._width || sh!=_opacity._height)?
- _opacity.get_resize(sw,sh,1,-100,render_type<=3?1:3):CImg<_to>(),
- &nopacity = _nopacity?_nopacity:_opacity;
- draw_image(nx0,ny0,sprite,nopacity,g_opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128);
- board.setFillColor(LibBoard::Color::Null);
- board.drawRectangle((float)nx0,height() - (float)ny0,sw,sh);
- }
- #endif
- }
- }
- } break;
- case 2 : { // Colored line
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1];
- const int
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1));
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale;
- if (render_type) {
- if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity);
- else draw_line(x0,y0,x1,y1,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,x1,height() - (float)y1);
- }
- #endif
- } else {
- draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- }
- #endif
- }
- } break;
- case 5 : { // Colored sphere
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1],
- is_wireframe = (unsigned int)primitive[2],
- is_radius = (unsigned int)primitive[3];
- float Xc,Yc,Zc,radius;
- if (is_radius) {
- Xc = (float)vertices(n0,0);
- Yc = (float)vertices(n0,1);
- Zc = (float)vertices(n0,2);
- radius = cimg::hypot(vertices(n1,0) - vertices(n0,0),
- vertices(n1,1) - vertices(n0,1),
- vertices(n1,2) - vertices(n0,2));
- } else {
- Xc = 0.5f*((float)vertices(n0,0) + (float)vertices(n1,0));
- Yc = 0.5f*((float)vertices(n0,1) + (float)vertices(n1,1));
- Zc = 0.5f*((float)vertices(n0,2) + (float)vertices(n1,2));
- radius = 0.5f*cimg::hypot(vertices(n1,0) - vertices(n0,0),
- vertices(n1,1) - vertices(n0,1),
- vertices(n1,2) - vertices(n0,2));
- }
- const float
- zc = Z + Zc + _focale,
- af = absfocale?absfocale/zc:1,
- xc = X + Xc*af,
- yc = Y + Yc*af;
- radius*=af;
- switch (render_type) {
- case 0 :
- draw_point((int)xc,(int)yc,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawDot(xc,height() - yc);
- }
- #endif
- break;
- case 1 :
- draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.setFillColor(LibBoard::Color::Null);
- board.drawCircle(xc,height() - yc,radius);
- }
- #endif
- break;
- default :
- if (is_wireframe) draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity,~0U);
- else draw_circle((int)xc,(int)yc,(int)radius,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- if (!is_wireframe) board.fillCircle(xc,height() - yc,radius);
- else {
- board.setFillColor(LibBoard::Color::Null);
- board.drawCircle(xc,height() - yc,radius);
- }
- }
- #endif
- break;
- }
- } break;
- case 6 : { // Textured line
- if (!__color) {
- if (render_type==5) cimg::mutex(10,0);
- throw CImgArgumentException(_cimg_instance
- "draw_object3d(): Undefined texture for line primitive [%u].",
- cimg_instance,n_primitive);
- }
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1];
- const int
- tx0 = (int)primitive[2], ty0 = (int)primitive[3],
- tx1 = (int)primitive[4], ty1 = (int)primitive[5],
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1));
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale;
- if (render_type) {
- if (zbuffer) draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity);
- else draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
- }
- #endif
- } else {
- draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
- ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
- draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
- ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- }
- #endif
- }
- } break;
- case 3 : { // Colored triangle
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1],
- n2 = (unsigned int)primitive[2];
- const int
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
- x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1));
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale,
- z2 = vertices(n2,2) + Z + _focale;
- switch (render_type) {
- case 0 :
- draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).draw_point(x2,y2,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- board.drawDot((float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 1 :
- if (zbuffer)
- draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x0,y0,z0,x2,y2,z2,pcolor,opacity).
- draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity);
- else
- draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x0,y0,x2,y2,pcolor,opacity).
- draw_line(x1,y1,x2,y2,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
- board.drawLine((float)x0,height() - (float)y0,(float)x2,height() - (float)y2);
- board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 2 :
- if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity);
- else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 3 :
- if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l));
- else _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l));
- #ifdef cimg_use_board
- if (pboard) {
- const float lp = std::min(lightprops(l),1.f);
- board.setPenColorRGBi((unsigned char)(color[0]*lp),
- (unsigned char)(color[1]*lp),
- (unsigned char)(color[2]*lp),
- (unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 4 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,
- lightprops(n0),lightprops(n1),lightprops(n2),opacity);
- else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,lightprops(n0),lightprops(n1),lightprops(n2),opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi((unsigned char)(color[0]),
- (unsigned char)(color[1]),
- (unsigned char)(color[2]),
- (unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprops(n0),
- (float)x1,height() - (float)y1,lightprops(n1),
- (float)x2,height() - (float)y2,lightprops(n2));
- }
- #endif
- break;
- case 5 : {
- const unsigned int
- lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
- lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
- lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1));
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- else draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- const float
- l0 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n0,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n0,1)))),
- l1 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n1,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n1,1)))),
- l2 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n2,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n2,1))));
- board.setPenColorRGBi((unsigned char)(color[0]),
- (unsigned char)(color[1]),
- (unsigned char)(color[2]),
- (unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
- (float)x1,height() - (float)y1,l1,
- (float)x2,height() - (float)y2,l2);
- }
- #endif
- } break;
- }
- } break;
- case 4 : { // Colored quadrangle
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1],
- n2 = (unsigned int)primitive[2],
- n3 = (unsigned int)primitive[3];
- const int
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
- x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1)),
- x3 = cimg::uiround(projections(n3,0)), y3 = cimg::uiround(projections(n3,1)),
- xc = (x0 + x1 + x2 + x3)/4, yc = (y0 + y1 + y2 + y3)/4;
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale,
- z2 = vertices(n2,2) + Z + _focale,
- z3 = vertices(n3,2) + Z + _focale,
- zc = (z0 + z1 + z2 + z3)/4;
- switch (render_type) {
- case 0 :
- draw_point(x0,y0,pcolor,opacity).draw_point(x1,y1,pcolor,opacity).
- draw_point(x2,y2,pcolor,opacity).draw_point(x3,y3,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- board.drawDot((float)x2,height() - (float)y2);
- board.drawDot((float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 1 :
- if (zbuffer)
- draw_line(zbuffer,x0,y0,z0,x1,y1,z1,pcolor,opacity).draw_line(zbuffer,x1,y1,z1,x2,y2,z2,pcolor,opacity).
- draw_line(zbuffer,x2,y2,z2,x3,y3,z3,pcolor,opacity).draw_line(zbuffer,x3,y3,z3,x0,y0,z0,pcolor,opacity);
- else
- draw_line(x0,y0,x1,y1,pcolor,opacity).draw_line(x1,y1,x2,y2,pcolor,opacity).
- draw_line(x2,y2,x3,y3,pcolor,opacity).draw_line(x3,y3,x0,y0,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
- board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
- board.drawLine((float)x2,height() - (float)y2,(float)x3,height() - (float)y3);
- board.drawLine((float)x3,height() - (float)y3,(float)x0,height() - (float)y0);
- }
- #endif
- break;
- case 2 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity);
- else
- draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity).draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(color[0],color[1],color[2],(unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x2,height() - (float)y2,
- (float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 3 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,pcolor,opacity,lightprops(l)).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,pcolor,opacity,lightprops(l));
- else
- _draw_triangle(x0,y0,x1,y1,x2,y2,pcolor,opacity,lightprops(l)).
- _draw_triangle(x0,y0,x2,y2,x3,y3,pcolor,opacity,lightprops(l));
- #ifdef cimg_use_board
- if (pboard) {
- const float lp = std::min(lightprops(l),1.f);
- board.setPenColorRGBi((unsigned char)(color[0]*lp),
- (unsigned char)(color[1]*lp),
- (unsigned char)(color[2]*lp),(unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x2,height() - (float)y2,
- (float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 4 : {
- const float
- lightprop0 = lightprops(n0), lightprop1 = lightprops(n1),
- lightprop2 = lightprops(n2), lightprop3 = lightprops(n3),
- lightpropc = (lightprop0 + lightprop1 + lightprop2 + lightprop2)/4;
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,xc,yc,zc,pcolor,lightprop0,lightprop1,lightpropc,opacity).
- draw_triangle(zbuffer,x1,y1,z1,x2,y2,z2,xc,yc,zc,pcolor,lightprop1,lightprop2,lightpropc,opacity).
- draw_triangle(zbuffer,x2,y2,z2,x3,y3,z3,xc,yc,zc,pcolor,lightprop2,lightprop3,lightpropc,opacity).
- draw_triangle(zbuffer,x3,y3,z3,x0,y0,z0,xc,yc,zc,pcolor,lightprop3,lightprop0,lightpropc,opacity);
- else
- draw_triangle(x0,y0,x1,y1,xc,yc,pcolor,lightprop0,lightprop1,lightpropc,opacity).
- draw_triangle(x1,y1,x2,y2,xc,yc,pcolor,lightprop1,lightprop2,lightpropc,opacity).
- draw_triangle(x2,y2,x3,y3,xc,yc,pcolor,lightprop2,lightprop3,lightpropc,opacity).
- draw_triangle(x3,y3,x0,y0,xc,yc,pcolor,lightprop3,lightprop0,lightpropc,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi((unsigned char)(color[0]),
- (unsigned char)(color[1]),
- (unsigned char)(color[2]),
- (unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
- (float)x1,height() - (float)y1,lightprop1,
- (float)x2,height() - (float)y2,lightprop2);
- board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
- (float)x2,height() - (float)y2,lightprop2,
- (float)x3,height() - (float)y3,lightprop3);
- }
- #endif
- } break;
- case 5 : {
- const unsigned int
- lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
- lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
- lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1)),
- lx3 = (unsigned int)cimg::uiround(lightprops(n3,0)), ly3 = (unsigned int)cimg::uiround(lightprops(n3,1)),
- lxc = (lx0 + lx1 + lx2 + lx3)/4, lyc = (ly0 + ly1 + ly2 + ly3)/4;
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,xc,yc,zc,pcolor,light_texture,lx0,ly0,lx1,ly1,lxc,lyc,opacity).
- draw_triangle(zbuffer,x1,y1,z1,x2,y2,z2,xc,yc,zc,pcolor,light_texture,lx1,ly1,lx2,ly2,lxc,lyc,opacity).
- draw_triangle(zbuffer,x2,y2,z2,x3,y3,z3,xc,yc,zc,pcolor,light_texture,lx2,ly2,lx3,ly3,lxc,lyc,opacity).
- draw_triangle(zbuffer,x3,y3,z3,x0,y0,z0,xc,yc,zc,pcolor,light_texture,lx3,ly3,lx0,ly0,lxc,lyc,opacity);
- else
- draw_triangle(x0,y0,x1,y1,xc,yc,pcolor,light_texture,lx0,ly0,lx1,ly1,lxc,lyc,opacity).
- draw_triangle(x1,y1,x2,y2,xc,yc,pcolor,light_texture,lx1,ly1,lx2,ly2,lxc,lyc,opacity).
- draw_triangle(x2,y2,x3,y3,xc,yc,pcolor,light_texture,lx2,ly2,lx3,ly3,lxc,lyc,opacity).
- draw_triangle(x3,y3,x0,y0,xc,yc,pcolor,light_texture,lx3,ly3,lx0,ly0,lxc,lyc,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- const float
- l0 = light_texture((int)(light_texture.width()/2*(1 + lx0)), (int)(light_texture.height()/2*(1 + ly0))),
- l1 = light_texture((int)(light_texture.width()/2*(1 + lx1)), (int)(light_texture.height()/2*(1 + ly1))),
- l2 = light_texture((int)(light_texture.width()/2*(1 + lx2)), (int)(light_texture.height()/2*(1 + ly2))),
- l3 = light_texture((int)(light_texture.width()/2*(1 + lx3)), (int)(light_texture.height()/2*(1 + ly3)));
- board.setPenColorRGBi((unsigned char)(color[0]),
- (unsigned char)(color[1]),
- (unsigned char)(color[2]),
- (unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
- (float)x1,height() - (float)y1,l1,
- (float)x2,height() - (float)y2,l2);
- board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
- (float)x2,height() - (float)y2,l2,
- (float)x3,height() - (float)y3,l3);
- }
- #endif
- } break;
- }
- } break;
- case 9 : { // Textured triangle
- if (!__color) {
- if (render_type==5) cimg::mutex(10,0);
- throw CImgArgumentException(_cimg_instance
- "draw_object3d(): Undefined texture for triangle primitive [%u].",
- cimg_instance,n_primitive);
- }
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1],
- n2 = (unsigned int)primitive[2];
- const int
- tx0 = (int)primitive[3], ty0 = (int)primitive[4],
- tx1 = (int)primitive[5], ty1 = (int)primitive[6],
- tx2 = (int)primitive[7], ty2 = (int)primitive[8],
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
- x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1));
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale,
- z2 = vertices(n2,2) + Z + _focale;
- switch (render_type) {
- case 0 :
- draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
- ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
- draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
- ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity).
- draw_point(x2,y2,color.get_vector_at(tx2<=0?0:tx2>=color.width()?color.width() - 1:tx2,
- ty2<=0?0:ty2>=color.height()?color.height() - 1:ty2)._data,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- board.drawDot((float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 1 :
- if (zbuffer)
- draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
- draw_line(zbuffer,x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity).
- draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity);
- else
- draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
- draw_line(x0,y0,z0,x2,y2,z2,color,tx0,ty0,tx2,ty2,opacity).
- draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
- board.drawLine((float)x0,height() - (float)y0,(float)x2,height() - (float)y2);
- board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 2 :
- if (zbuffer) draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity);
- else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 3 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l));
- else draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l));
- #ifdef cimg_use_board
- if (pboard) {
- const float lp = std::min(lightprops(l),1.f);
- board.setPenColorRGBi((unsigned char)(128*lp),
- (unsigned char)(128*lp),
- (unsigned char)(128*lp),
- (unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- }
- #endif
- break;
- case 4 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- lightprops(n0),lightprops(n1),lightprops(n2),opacity);
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- lightprops(n0),lightprops(n1),lightprops(n2),opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprops(n0),
- (float)x1,height() - (float)y1,lightprops(n1),
- (float)x2,height() - (float)y2,lightprops(n2));
- }
- #endif
- break;
- case 5 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture,
- (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1),
- (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1),
- (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1),
- opacity);
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,light_texture,
- (unsigned int)lightprops(n0,0),(unsigned int)lightprops(n0,1),
- (unsigned int)lightprops(n1,0),(unsigned int)lightprops(n1,1),
- (unsigned int)lightprops(n2,0),(unsigned int)lightprops(n2,1),
- opacity);
- #ifdef cimg_use_board
- if (pboard) {
- const float
- l0 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n0,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n0,1)))),
- l1 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n1,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n1,1)))),
- l2 = light_texture((int)(light_texture.width()/2*(1 + lightprops(n2,0))),
- (int)(light_texture.height()/2*(1 + lightprops(n2,1))));
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
- (float)x1,height() - (float)y1,l1,
- (float)x2,height() - (float)y2,l2);
- }
- #endif
- break;
- }
- } break;
- case 12 : { // Textured quadrangle
- if (!__color) {
- if (render_type==5) cimg::mutex(10,0);
- throw CImgArgumentException(_cimg_instance
- "draw_object3d(): Undefined texture for quadrangle primitive [%u].",
- cimg_instance,n_primitive);
- }
- const unsigned int
- n0 = (unsigned int)primitive[0],
- n1 = (unsigned int)primitive[1],
- n2 = (unsigned int)primitive[2],
- n3 = (unsigned int)primitive[3];
- const int
- tx0 = (int)primitive[4], ty0 = (int)primitive[5],
- tx1 = (int)primitive[6], ty1 = (int)primitive[7],
- tx2 = (int)primitive[8], ty2 = (int)primitive[9],
- tx3 = (int)primitive[10], ty3 = (int)primitive[11],
- x0 = cimg::uiround(projections(n0,0)), y0 = cimg::uiround(projections(n0,1)),
- x1 = cimg::uiround(projections(n1,0)), y1 = cimg::uiround(projections(n1,1)),
- x2 = cimg::uiround(projections(n2,0)), y2 = cimg::uiround(projections(n2,1)),
- x3 = cimg::uiround(projections(n3,0)), y3 = cimg::uiround(projections(n3,1));
- const float
- z0 = vertices(n0,2) + Z + _focale,
- z1 = vertices(n1,2) + Z + _focale,
- z2 = vertices(n2,2) + Z + _focale,
- z3 = vertices(n3,2) + Z + _focale;
- switch (render_type) {
- case 0 :
- draw_point(x0,y0,color.get_vector_at(tx0<=0?0:tx0>=color.width()?color.width() - 1:tx0,
- ty0<=0?0:ty0>=color.height()?color.height() - 1:ty0)._data,opacity).
- draw_point(x1,y1,color.get_vector_at(tx1<=0?0:tx1>=color.width()?color.width() - 1:tx1,
- ty1<=0?0:ty1>=color.height()?color.height() - 1:ty1)._data,opacity).
- draw_point(x2,y2,color.get_vector_at(tx2<=0?0:tx2>=color.width()?color.width() - 1:tx2,
- ty2<=0?0:ty2>=color.height()?color.height() - 1:ty2)._data,opacity).
- draw_point(x3,y3,color.get_vector_at(tx3<=0?0:tx3>=color.width()?color.width() - 1:tx3,
- ty3<=0?0:ty3>=color.height()?color.height() - 1:ty3)._data,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawDot((float)x0,height() - (float)y0);
- board.drawDot((float)x1,height() - (float)y1);
- board.drawDot((float)x2,height() - (float)y2);
- board.drawDot((float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 1 :
- if (zbuffer)
- draw_line(zbuffer,x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
- draw_line(zbuffer,x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity).
- draw_line(zbuffer,x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity).
- draw_line(zbuffer,x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity);
- else
- draw_line(x0,y0,z0,x1,y1,z1,color,tx0,ty0,tx1,ty1,opacity).
- draw_line(x1,y1,z1,x2,y2,z2,color,tx1,ty1,tx2,ty2,opacity).
- draw_line(x2,y2,z2,x3,y3,z3,color,tx2,ty2,tx3,ty3,opacity).
- draw_line(x3,y3,z3,x0,y0,z0,color,tx3,ty3,tx0,ty0,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.drawLine((float)x0,height() - (float)y0,(float)x1,height() - (float)y1);
- board.drawLine((float)x1,height() - (float)y1,(float)x2,height() - (float)y2);
- board.drawLine((float)x2,height() - (float)y2,(float)x3,height() - (float)y3);
- board.drawLine((float)x3,height() - (float)y3,(float)x0,height() - (float)y0);
- }
- #endif
- break;
- case 2 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity);
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity).
- draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x2,height() - (float)y2,
- (float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 3 :
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l));
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,opacity,lightprops(l)).
- draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,opacity,lightprops(l));
- #ifdef cimg_use_board
- if (pboard) {
- const float lp = std::min(lightprops(l),1.f);
- board.setPenColorRGBi((unsigned char)(128*lp),
- (unsigned char)(128*lp),
- (unsigned char)(128*lp),
- (unsigned char)(opacity*255));
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x1,height() - (float)y1,
- (float)x2,height() - (float)y2);
- board.fillTriangle((float)x0,height() - (float)y0,
- (float)x2,height() - (float)y2,
- (float)x3,height() - (float)y3);
- }
- #endif
- break;
- case 4 : {
- const float
- lightprop0 = lightprops(n0), lightprop1 = lightprops(n1),
- lightprop2 = lightprops(n2), lightprop3 = lightprops(n3);
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- lightprop0,lightprop1,lightprop2,opacity).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
- lightprop0,lightprop2,lightprop3,opacity);
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- lightprop0,lightprop1,lightprop2,opacity).
- draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
- lightprop0,lightprop2,lightprop3,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,lightprop0,
- (float)x1,height() - (float)y1,lightprop1,
- (float)x2,height() - (float)y2,lightprop2);
- board.fillGouraudTriangle((float)x0,height() -(float)y0,lightprop0,
- (float)x2,height() - (float)y2,lightprop2,
- (float)x3,height() - (float)y3,lightprop3);
- }
- #endif
- } break;
- case 5 : {
- const unsigned int
- lx0 = (unsigned int)cimg::uiround(lightprops(n0,0)), ly0 = (unsigned int)cimg::uiround(lightprops(n0,1)),
- lx1 = (unsigned int)cimg::uiround(lightprops(n1,0)), ly1 = (unsigned int)cimg::uiround(lightprops(n1,1)),
- lx2 = (unsigned int)cimg::uiround(lightprops(n2,0)), ly2 = (unsigned int)cimg::uiround(lightprops(n2,1)),
- lx3 = (unsigned int)cimg::uiround(lightprops(n3,0)), ly3 = (unsigned int)cimg::uiround(lightprops(n3,1));
- if (zbuffer)
- draw_triangle(zbuffer,x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity).
- draw_triangle(zbuffer,x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
- light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity);
- else
- draw_triangle(x0,y0,z0,x1,y1,z1,x2,y2,z2,color,tx0,ty0,tx1,ty1,tx2,ty2,
- light_texture,lx0,ly0,lx1,ly1,lx2,ly2,opacity).
- draw_triangle(x0,y0,z0,x2,y2,z2,x3,y3,z3,color,tx0,ty0,tx2,ty2,tx3,ty3,
- light_texture,lx0,ly0,lx2,ly2,lx3,ly3,opacity);
- #ifdef cimg_use_board
- if (pboard) {
- const float
- l0 = light_texture((int)(light_texture.width()/2*(1 + lx0)), (int)(light_texture.height()/2*(1 + ly0))),
- l1 = light_texture((int)(light_texture.width()/2*(1 + lx1)), (int)(light_texture.height()/2*(1 + ly1))),
- l2 = light_texture((int)(light_texture.width()/2*(1 + lx2)), (int)(light_texture.height()/2*(1 + ly2))),
- l3 = light_texture((int)(light_texture.width()/2*(1 + lx3)), (int)(light_texture.height()/2*(1 + ly3)));
- board.setPenColorRGBi(128,128,128,(unsigned char)(opacity*255));
- board.fillGouraudTriangle((float)x0,height() - (float)y0,l0,
- (float)x1,height() - (float)y1,l1,
- (float)x2,height() - (float)y2,l2);
- board.fillGouraudTriangle((float)x0,height() -(float)y0,l0,
- (float)x2,height() - (float)y2,l2,
- (float)x3,height() - (float)y3,l3);
- }
- #endif
- } break;
- }
- } break;
- }
- }
- if (render_type==5) cimg::mutex(10,0);
- return *this;
- }
- //@}
- //---------------------------
- //
- //! \name Data Input
- //@{
- //---------------------------
- //! Launch simple interface to select a shape from an image.
- /**
- \param disp Display window to use.
- \param feature_type Type of feature to select. Can be <tt>{ 0=point | 1=line | 2=rectangle | 3=ellipse }</tt>.
- \param XYZ Pointer to 3 values X,Y,Z which tells about the projection point coordinates, for volumetric images.
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- CImg<T>& select(CImgDisplay &disp,
- const unsigned int feature_type=2, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false,
- const bool is_deep_selection_default=false) {
- return get_select(disp,feature_type,XYZ,exit_on_anykey,is_deep_selection_default).move_to(*this);
- }
- //! Simple interface to select a shape from an image \overloading.
- CImg<T>& select(const char *const title,
- const unsigned int feature_type=2, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false,
- const bool is_deep_selection_default=false) {
- return get_select(title,feature_type,XYZ,exit_on_anykey,is_deep_selection_default).move_to(*this);
- }
- //! Simple interface to select a shape from an image \newinstance.
- CImg<intT> get_select(CImgDisplay &disp,
- const unsigned int feature_type=2, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false,
- const bool is_deep_selection_default=false) const {
- return _select(disp,0,feature_type,XYZ,0,0,0,exit_on_anykey,true,false,is_deep_selection_default);
- }
- //! Simple interface to select a shape from an image \newinstance.
- CImg<intT> get_select(const char *const title,
- const unsigned int feature_type=2, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false,
- const bool is_deep_selection_default=false) const {
- CImgDisplay disp;
- return _select(disp,title,feature_type,XYZ,0,0,0,exit_on_anykey,true,false,is_deep_selection_default);
- }
- CImg<intT> _select(CImgDisplay &disp, const char *const title,
- const unsigned int feature_type, unsigned int *const XYZ,
- const int origX, const int origY, const int origZ,
- const bool exit_on_anykey,
- const bool reset_view3d,
- const bool force_display_z_coord,
- const bool is_deep_selection_default) const {
- if (is_empty()) return CImg<intT>(1,feature_type==0?3:6,1,1,-1);
- if (!disp) {
- disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1);
- if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum);
- } else {
- if (title) disp.set_title("%s",title);
- disp.move_inside_screen();
- }
- CImg<T> thumb;
- if (width()>disp.screen_width() || height()>disp.screen_height())
- get_resize(cimg_fitscreen(width(),height(),depth()),depth(),-100).move_to(thumb);
- const unsigned int old_normalization = disp.normalization();
- bool old_is_resized = disp.is_resized();
- disp._normalization = 0;
- disp.show().set_key(0).set_wheel().show_mouse();
- static const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 };
- int area = 0, area_started = 0, area_clicked = 0, phase = 0,
- X0 = (int)((XYZ?XYZ[0]:_width/2)%_width),
- Y0 = (int)((XYZ?XYZ[1]:_height/2)%_height),
- Z0 = (int)((XYZ?XYZ[2]:_depth/2)%_depth),
- X1 =-1, Y1 = -1, Z1 = -1,
- X3d = -1, Y3d = -1,
- oX3d = X3d, oY3d = -1,
- omx = -1, omy = -1;
- float X = -1, Y = -1, Z = -1;
- unsigned int key = 0, font_size = 32;
- bool is_deep_selection = is_deep_selection_default,
- shape_selected = false, text_down = false, visible_cursor = true;
- static CImg<floatT> pose3d;
- static bool is_view3d = false, is_axes = true;
- if (reset_view3d) { pose3d.assign(); is_view3d = false; }
- CImg<floatT> points3d, opacities3d, sel_opacities3d;
- CImgList<uintT> primitives3d, sel_primitives3d;
- CImgList<ucharT> colors3d, sel_colors3d;
- CImg<ucharT> visu, visu0, view3d;
- CImg<charT> text(1024); *text = 0;
- while (!key && !disp.is_closed() && !shape_selected) {
- // Handle mouse motion and selection
- int
- mx = disp.mouse_x(),
- my = disp.mouse_y();
- const float
- mX = mx<0?-1.f:(float)mx*(width() + (depth()>1?depth():0))/disp.width(),
- mY = my<0?-1.f:(float)my*(height() + (depth()>1?depth():0))/disp.height();
- area = 0;
- if (mX>=0 && mY>=0 && mX<width() && mY<height()) { area = 1; X = mX; Y = mY; Z = (float)(phase?Z1:Z0); }
- if (mX>=0 && mX<width() && mY>=height()) { area = 2; X = mX; Z = mY - _height; Y = (float)(phase?Y1:Y0); }
- if (mY>=0 && mX>=width() && mY<height()) { area = 3; Y = mY; Z = mX - _width; X = (float)(phase?X1:X0); }
- if (mX>=width() && mY>=height()) area = 4;
- if (disp.button()) { if (!area_clicked) area_clicked = area; } else area_clicked = 0;
- CImg<charT> filename(32);
- switch (key = disp.key()) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT :
- #endif
- case 0 : case cimg::keyCTRLLEFT : key = 0; break;
- case cimg::keyPAGEUP :
- if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(1); key = 0; } break;
- case cimg::keyPAGEDOWN :
- if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { disp.set_wheel(-1); key = 0; } break;
- case cimg::keyX : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- is_axes = !is_axes; disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
- CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
- _is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- is_view3d = !is_view3d; disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- if (visu0) {
- (+visu0).__draw_text(" Saving snapshot...",font_size,(int)text_down).display(disp);
- visu0.save(filename);
- (+visu0).__draw_text(" Snapshot '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
- }
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- #ifdef cimg_use_zlib
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
- #else
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
- #endif
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu0).__draw_text(" Saving instance... ",font_size,(int)text_down).display(disp);
- save(filename);
- (+visu0).__draw_text(" Instance '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- }
- switch (area) {
- case 0 : // When mouse is out of image range
- mx = my = -1; X = Y = Z = -1;
- break;
- case 1 : case 2 : case 3 : { // When mouse is over the XY,XZ or YZ projections
- const unsigned int but = disp.button();
- const bool b1 = (bool)(but&1), b2 = (bool)(but&2), b3 = (bool)(but&4);
- if (b1 && phase==1 && area_clicked==area) { // When selection has been started (1st step)
- if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign();
- X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z;
- }
- if (!b1 && phase==2 && area_clicked!=area) { // When selection is at 2nd step (for volumes)
- switch (area_started) {
- case 1 : if (Z1!=(int)Z) visu0.assign(); Z1 = (int)Z; break;
- case 2 : if (Y1!=(int)Y) visu0.assign(); Y1 = (int)Y; break;
- case 3 : if (X1!=(int)X) visu0.assign(); X1 = (int)X; break;
- }
- }
- if (b2 && area_clicked==area) { // When moving through the image/volume
- if (phase) {
- if (_depth>1 && (X1!=(int)X || Y1!=(int)Y || Z1!=(int)Z)) visu0.assign();
- X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z;
- } else {
- if (_depth>1 && (X0!=(int)X || Y0!=(int)Y || Z0!=(int)Z)) visu0.assign();
- X0 = (int)X; Y0 = (int)Y; Z0 = (int)Z;
- }
- }
- if (b3) { // Reset selection
- X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = area = area_clicked = area_started = 0;
- visu0.assign();
- }
- if (disp.wheel()) { // When moving through the slices of the volume (with mouse wheel)
- if (_depth>1 && !disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT() &&
- !disp.is_keySHIFTLEFT() && !disp.is_keySHIFTRIGHT()) {
- switch (area) {
- case 1 :
- if (phase) Z = (float)(Z1+=disp.wheel()); else Z = (float)(Z0+=disp.wheel());
- visu0.assign(); break;
- case 2 :
- if (phase) Y = (float)(Y1+=disp.wheel()); else Y = (float)(Y0+=disp.wheel());
- visu0.assign(); break;
- case 3 :
- if (phase) X = (float)(X1+=disp.wheel()); else X = (float)(X0+=disp.wheel());
- visu0.assign(); break;
- }
- disp.set_wheel();
- } else key = ~0U;
- }
- if ((phase==0 && b1) ||
- (phase==1 && !b1) ||
- (phase==2 && b1)) switch (phase) { // Detect change of phase
- case 0 :
- if (area==area_clicked) {
- X0 = X1 = (int)X; Y0 = Y1 = (int)Y; Z0 = Z1 = (int)Z; area_started = area; ++phase;
- } break;
- case 1 :
- if (area==area_started) {
- X1 = (int)X; Y1 = (int)Y; Z1 = (int)Z; ++phase;
- if (_depth>1) {
- if (disp.is_keyCTRLLEFT()) is_deep_selection = !is_deep_selection_default;
- if (is_deep_selection) ++phase;
- }
- } else if (!b1) { X = (float)X0; Y = (float)Y0; Z = (float)Z0; phase = 0; visu0.assign(); }
- break;
- case 2 : ++phase; break;
- }
- } break;
- case 4 : // When mouse is over the 3D view
- if (is_view3d && points3d) {
- X3d = mx - width()*disp.width()/(width() + (depth()>1?depth():0));
- Y3d = my - height()*disp.height()/(height() + (depth()>1?depth():0));
- if (oX3d<0) { oX3d = X3d; oY3d = Y3d; }
- // Left + right buttons: reset.
- if ((disp.button()&3)==3) { pose3d.assign(); view3d.assign(); oX3d = oY3d = X3d = Y3d = -1; }
- else if (disp.button()&1 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Left button: rotate
- const float
- R = 0.45f*std::min(view3d._width,view3d._height),
- R2 = R*R,
- u0 = (float)(oX3d - view3d.width()/2),
- v0 = (float)(oY3d - view3d.height()/2),
- u1 = (float)(X3d - view3d.width()/2),
- v1 = (float)(Y3d - view3d.height()/2),
- n0 = cimg::hypot(u0,v0),
- n1 = cimg::hypot(u1,v1),
- nu0 = n0>R?(u0*R/n0):u0,
- nv0 = n0>R?(v0*R/n0):v0,
- nw0 = (float)std::sqrt(std::max(0.f,R2 - nu0*nu0 - nv0*nv0)),
- nu1 = n1>R?(u1*R/n1):u1,
- nv1 = n1>R?(v1*R/n1):v1,
- nw1 = (float)std::sqrt(std::max(0.f,R2 - nu1*nu1 - nv1*nv1)),
- u = nv0*nw1 - nw0*nv1,
- v = nw0*nu1 - nu0*nw1,
- w = nv0*nu1 - nu0*nv1,
- n = cimg::hypot(u,v,w),
- alpha = (float)std::asin(n/R2)*180/cimg::PI;
- pose3d.draw_image(CImg<floatT>::rotation_matrix(u,v,w,-alpha)*pose3d.get_crop(0,0,2,2));
- view3d.assign();
- } else if (disp.button()&2 && pose3d && oY3d!=Y3d) { // Right button: zoom
- pose3d(3,2)+=(Y3d - oY3d)*1.5f; view3d.assign();
- }
- if (disp.wheel()) { // Wheel: zoom
- pose3d(3,2)-=disp.wheel()*15; view3d.assign(); disp.set_wheel();
- }
- if (disp.button()&4 && pose3d && (oX3d!=X3d || oY3d!=Y3d)) { // Middle button: shift
- pose3d(3,0)-=oX3d - X3d; pose3d(3,1)-=oY3d - Y3d; view3d.assign();
- }
- oX3d = X3d; oY3d = Y3d;
- }
- mx = my = -1; X = Y = Z = -1;
- break;
- }
- if (phase) {
- if (!feature_type) shape_selected = phase?true:false;
- else {
- if (_depth>1) shape_selected = (phase==3)?true:false;
- else shape_selected = (phase==2)?true:false;
- }
- }
- if (X0<0) X0 = 0;
- if (X0>=width()) X0 = width() - 1;
- if (Y0<0) Y0 = 0;
- if (Y0>=height()) Y0 = height() - 1;
- if (Z0<0) Z0 = 0;
- if (Z0>=depth()) Z0 = depth() - 1;
- if (X1<1) X1 = 0;
- if (X1>=width()) X1 = width() - 1;
- if (Y1<0) Y1 = 0;
- if (Y1>=height()) Y1 = height() - 1;
- if (Z1<0) Z1 = 0;
- if (Z1>=depth()) Z1 = depth() - 1;
- // Draw visualization image on the display
- if (mx!=omx || my!=omy || !visu0 || (_depth>1 && !view3d)) {
- if (!visu0) { // Create image of projected planes
- if (thumb) thumb._get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0);
- else _get_select(disp,old_normalization,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0).move_to(visu0);
- visu0.resize(disp);
- view3d.assign();
- points3d.assign();
- }
- if (is_view3d && _depth>1 && !view3d) { // Create 3D view for volumetric images
- const unsigned int
- _x3d = (unsigned int)cimg::round((float)_width*visu0._width/(_width + _depth),1,1),
- _y3d = (unsigned int)cimg::round((float)_height*visu0._height/(_height + _depth),1,1),
- x3d = _x3d>=visu0._width?visu0._width - 1:_x3d,
- y3d = _y3d>=visu0._height?visu0._height - 1:_y3d;
- CImg<ucharT>(1,2,1,1,64,128).resize(visu0._width - x3d,visu0._height - y3d,1,visu0._spectrum,3).
- move_to(view3d);
- if (!points3d) {
- get_projections3d(primitives3d,colors3d,phase?X1:X0,phase?Y1:Y0,phase?Z1:Z0,true).move_to(points3d);
- points3d.append(CImg<floatT>(8,3,1,1,
- 0,_width - 1,_width - 1,0,0,_width - 1,_width - 1,0,
- 0,0,_height - 1,_height - 1,0,0,_height - 1,_height - 1,
- 0,0,0,0,_depth - 1,_depth - 1,_depth - 1,_depth - 1),'x');
- CImg<uintT>::vector(12,13).move_to(primitives3d); CImg<uintT>::vector(13,14).move_to(primitives3d);
- CImg<uintT>::vector(14,15).move_to(primitives3d); CImg<uintT>::vector(15,12).move_to(primitives3d);
- CImg<uintT>::vector(16,17).move_to(primitives3d); CImg<uintT>::vector(17,18).move_to(primitives3d);
- CImg<uintT>::vector(18,19).move_to(primitives3d); CImg<uintT>::vector(19,16).move_to(primitives3d);
- CImg<uintT>::vector(12,16).move_to(primitives3d); CImg<uintT>::vector(13,17).move_to(primitives3d);
- CImg<uintT>::vector(14,18).move_to(primitives3d); CImg<uintT>::vector(15,19).move_to(primitives3d);
- colors3d.insert(12,CImg<ucharT>::vector(255,255,255));
- opacities3d.assign(primitives3d.width(),1,1,1,0.5f);
- if (!phase) {
- opacities3d[0] = opacities3d[1] = opacities3d[2] = 0.8f;
- sel_primitives3d.assign();
- sel_colors3d.assign();
- sel_opacities3d.assign();
- } else {
- if (feature_type==2) {
- points3d.append(CImg<floatT>(8,3,1,1,
- X0,X1,X1,X0,X0,X1,X1,X0,
- Y0,Y0,Y1,Y1,Y0,Y0,Y1,Y1,
- Z0,Z0,Z0,Z0,Z1,Z1,Z1,Z1),'x');
- sel_primitives3d.assign();
- CImg<uintT>::vector(20,21).move_to(sel_primitives3d);
- CImg<uintT>::vector(21,22).move_to(sel_primitives3d);
- CImg<uintT>::vector(22,23).move_to(sel_primitives3d);
- CImg<uintT>::vector(23,20).move_to(sel_primitives3d);
- CImg<uintT>::vector(24,25).move_to(sel_primitives3d);
- CImg<uintT>::vector(25,26).move_to(sel_primitives3d);
- CImg<uintT>::vector(26,27).move_to(sel_primitives3d);
- CImg<uintT>::vector(27,24).move_to(sel_primitives3d);
- CImg<uintT>::vector(20,24).move_to(sel_primitives3d);
- CImg<uintT>::vector(21,25).move_to(sel_primitives3d);
- CImg<uintT>::vector(22,26).move_to(sel_primitives3d);
- CImg<uintT>::vector(23,27).move_to(sel_primitives3d);
- } else {
- points3d.append(CImg<floatT>(2,3,1,1,
- X0,X1,
- Y0,Y1,
- Z0,Z1),'x');
- sel_primitives3d.assign(CImg<uintT>::vector(20,21));
- }
- sel_colors3d.assign(sel_primitives3d._width,CImg<ucharT>::vector(255,255,255));
- sel_opacities3d.assign(sel_primitives3d._width,1,1,1,0.8f);
- }
- points3d.shift_object3d(-0.5f*(_width - 1),-0.5f*(_height - 1),-0.5f*(_depth - 1)).resize_object3d();
- points3d*=0.75f*std::min(view3d._width,view3d._height);
- }
- if (!pose3d) CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose3d);
- CImg<floatT> zbuffer3d(view3d._width,view3d._height,1,1,0);
- const CImg<floatT> rotated_points3d = pose3d.get_crop(0,0,2,2)*points3d;
- if (sel_primitives3d)
- view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width,
- pose3d(3,1) + 0.5f*view3d._height,
- pose3d(3,2),
- rotated_points3d,sel_primitives3d,sel_colors3d,sel_opacities3d,
- 2,true,500,0,0,0,0,0,1,zbuffer3d);
- view3d.draw_object3d(pose3d(3,0) + 0.5f*view3d._width,
- pose3d(3,1) + 0.5f*view3d._height,
- pose3d(3,2),
- rotated_points3d,primitives3d,colors3d,opacities3d,
- 2,true,500,0,0,0,0,0,1,zbuffer3d);
- visu0.draw_image(x3d,y3d,view3d);
- }
- visu = visu0;
- if (X<0 || Y<0 || Z<0) { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }}
- else {
- if (is_axes) { if (visible_cursor) { disp.hide_mouse(); visible_cursor = false; }}
- else { if (!visible_cursor) { disp.show_mouse(); visible_cursor = true; }}
- const int d = (depth()>1)?depth():0;
- int _vX = (int)X, _vY = (int)Y, _vZ = (int)Z;
- if (phase>=2) { _vX = X1; _vY = Y1; _vZ = Z1; }
- int
- w = disp.width(), W = width() + d,
- h = disp.height(), H = height() + d,
- _xp = (int)(_vX*(float)w/W), xp = _xp + ((int)(_xp*(float)W/w)!=_vX),
- _yp = (int)(_vY*(float)h/H), yp = _yp + ((int)(_yp*(float)H/h)!=_vY),
- _xn = (int)((_vX + 1.f)*w/W - 1), xn = _xn + ((int)((_xn + 1.f)*W/w)!=_vX + 1),
- _yn = (int)((_vY + 1.f)*h/H - 1), yn = _yn + ((int)((_yn + 1.f)*H/h)!=_vY + 1),
- _zxp = (int)((_vZ + width())*(float)w/W), zxp = _zxp + ((int)(_zxp*(float)W/w)!=_vZ + width()),
- _zyp = (int)((_vZ + height())*(float)h/H), zyp = _zyp + ((int)(_zyp*(float)H/h)!=_vZ + height()),
- _zxn = (int)((_vZ + width() + 1.f)*w/W - 1),
- zxn = _zxn + ((int)((_zxn + 1.f)*W/w)!=_vZ + width() + 1),
- _zyn = (int)((_vZ + height() + 1.f)*h/H - 1),
- zyn = _zyn + ((int)((_zyn + 1.f)*H/h)!=_vZ + height() + 1),
- _xM = (int)(width()*(float)w/W - 1), xM = _xM + ((int)((_xM + 1.f)*W/w)!=width()),
- _yM = (int)(height()*(float)h/H - 1), yM = _yM + ((int)((_yM + 1.f)*H/h)!=height()),
- xc = (xp + xn)/2,
- yc = (yp + yn)/2,
- zxc = (zxp + zxn)/2,
- zyc = (zyp + zyn)/2,
- xf = (int)(X*w/W),
- yf = (int)(Y*h/H),
- zxf = (int)((Z + width())*w/W),
- zyf = (int)((Z + height())*h/H);
- if (is_axes) { // Draw axes
- visu.draw_line(0,yf,visu.width() - 1,yf,foreground_color,0.7f,0xFF00FF00).
- draw_line(0,yf,visu.width() - 1,yf,background_color,0.7f,0x00FF00FF).
- draw_line(xf,0,xf,visu.height() - 1,foreground_color,0.7f,0xFF00FF00).
- draw_line(xf,0,xf,visu.height() - 1,background_color,0.7f,0x00FF00FF);
- if (_depth>1)
- visu.draw_line(zxf,0,zxf,yM,foreground_color,0.7f,0xFF00FF00).
- draw_line(zxf,0,zxf,yM,background_color,0.7f,0x00FF00FF).
- draw_line(0,zyf,xM,zyf,foreground_color,0.7f,0xFF00FF00).
- draw_line(0,zyf,xM,zyf,background_color,0.7f,0x00FF00FF);
- }
- // Draw box cursor.
- if (xn - xp>=4 && yn - yp>=4)
- visu.draw_rectangle(xp,yp,xn,yn,foreground_color,0.2f).
- draw_rectangle(xp,yp,xn,yn,foreground_color,1,0xAAAAAAAA).
- draw_rectangle(xp,yp,xn,yn,background_color,1,0x55555555);
- if (_depth>1) {
- if (yn - yp>=4 && zxn - zxp>=4)
- visu.draw_rectangle(zxp,yp,zxn,yn,background_color,0.2f).
- draw_rectangle(zxp,yp,zxn,yn,foreground_color,1,0xAAAAAAAA).
- draw_rectangle(zxp,yp,zxn,yn,background_color,1,0x55555555);
- if (xn - xp>=4 && zyn - zyp>=4)
- visu.draw_rectangle(xp,zyp,xn,zyn,background_color,0.2f).
- draw_rectangle(xp,zyp,xn,zyn,foreground_color,1,0xAAAAAAAA).
- draw_rectangle(xp,zyp,xn,zyn,background_color,1,0x55555555);
- }
- // Draw selection.
- if (phase && (phase!=1 || area_started==area)) {
- const int
- _xp0 = (int)(X0*(float)w/W), xp0 = _xp0 + ((int)(_xp0*(float)W/w)!=X0),
- _yp0 = (int)(Y0*(float)h/H), yp0 = _yp0 + ((int)(_yp0*(float)H/h)!=Y0),
- _xn0 = (int)((X0 + 1.f)*w/W - 1), xn0 = _xn0 + ((int)((_xn0 + 1.f)*W/w)!=X0 + 1),
- _yn0 = (int)((Y0 + 1.f)*h/H - 1), yn0 = _yn0 + ((int)((_yn0 + 1.f)*H/h)!=Y0 + 1),
- _zxp0 = (int)((Z0 + width())*(float)w/W), zxp0 = _zxp0 + ((int)(_zxp0*(float)W/w)!=Z0 + width()),
- _zyp0 = (int)((Z0 + height())*(float)h/H), zyp0 = _zyp0 + ((int)(_zyp0*(float)H/h)!=Z0 + height()),
- _zxn0 = (int)((Z0 + width() + 1.f)*w/W - 1),
- zxn0 = _zxn0 + ((int)((_zxn0 + 1.f)*W/w)!=Z0 + width() + 1),
- _zyn0 = (int)((Z0 + height() + 1.f)*h/H - 1),
- zyn0 = _zyn0 + ((int)((_zyn0 + 1.f)*H/h)!=Z0 + height() + 1),
- xc0 = (xp0 + xn0)/2,
- yc0 = (yp0 + yn0)/2,
- zxc0 = (zxp0 + zxn0)/2,
- zyc0 = (zyp0 + zyn0)/2;
- switch (feature_type) {
- case 1 : { // Vector
- visu.draw_arrow(xc0,yc0,xc,yc,background_color,0.9f,30,5,0x33333333).
- draw_arrow(xc0,yc0,xc,yc,foreground_color,0.9f,30,5,0xCCCCCCCC);
- if (d) {
- visu.draw_arrow(zxc0,yc0,zxc,yc,background_color,0.9f,30,5,0x33333333).
- draw_arrow(zxc0,yc0,zxc,yc,foreground_color,0.9f,30,5,0xCCCCCCCC).
- draw_arrow(xc0,zyc0,xc,zyc,background_color,0.9f,30,5,0x33333333).
- draw_arrow(xc0,zyc0,xc,zyc,foreground_color,0.9f,30,5,0xCCCCCCCC);
- }
- } break;
- case 2 : { // Box
- visu.draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,background_color,0.2f).
- draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,background_color,0.9f,0x55555555).
- draw_rectangle(X0<X1?xp0:xp,Y0<Y1?yp0:yp,X0<X1?xn:xn0,Y0<Y1?yn:yn0,foreground_color,0.9f,0xAAAAAAAA);
- if (xc0!=xc && yc0!=yc)
- visu.draw_line(xc0,yc0,xc,yc,background_color,0.9f,0x33333333).
- draw_line(xc0,yc0,xc,yc,foreground_color,0.9f,0xCCCCCCCC);
- if (d) {
- visu.draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,background_color,0.2f).
- draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,
- background_color,0.9f,0x55555555).
- draw_rectangle(Z0<Z1?zxp0:zxp,Y0<Y1?yp0:yp,Z0<Z1?zxn:zxn0,Y0<Y1?yn:yn0,
- foreground_color,0.9f,0xAAAAAAAA);
- if (zxc0!=zxc && yc0!=yc)
- visu.draw_line(zxc0,yc0,zxc,yc,background_color,0.9f,0x33333333).
- draw_line(zxc0,yc0,zxc,yc,foreground_color,0.9f,0xCCCCCCCC);
- visu.draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
- background_color,0.2f).
- draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
- background_color,0.9f,0x55555555).
- draw_rectangle(X0<X1?xp0:xp,Z0<Z1?zyp0:zyp,X0<X1?xn:xn0,Z0<Z1?zyn:zyn0,
- foreground_color,0.9f,0xAAAAAAAA);
- if (xp0!=xn && zyp0!=zyn)
- visu.draw_line(xp0,zyp0,xn,zyn,background_color,0.9f,0x33333333).
- draw_line(xp0,zyp0,xn,zyn,foreground_color,0.9f,0xCCCCCCCC);
- }
- } break;
- case 3 : { // Ellipse
- visu.draw_ellipse(xc0,yc0,
- (float)cimg::abs(xc - xc0),
- (float)cimg::abs(yc - yc0),0,background_color,0.2f).
- draw_ellipse(xc0,yc0,
- (float)cimg::abs(xc - xc0),
- (float)cimg::abs(yc - yc0),0,foreground_color,0.9f,~0U).
- draw_point(xc0,yc0,foreground_color,0.9f);
- if (d) {
- visu.draw_ellipse(zxc0,yc0,(float)cimg::abs(zxc - zxc0),(float)cimg::abs(yc - yc0),0,
- background_color,0.2f).
- draw_ellipse(zxc0,yc0,(float)cimg::abs(zxc - zxc0),(float)cimg::abs(yc - yc0),0,
- foreground_color,0.9f,~0U).
- draw_point(zxc0,yc0,foreground_color,0.9f).
- draw_ellipse(xc0,zyc0,(float)cimg::abs(xc - xc0),(float)cimg::abs(zyc - zyc0),0,
- background_color,0.2f).
- draw_ellipse(xc0,zyc0,(float)cimg::abs(xc - xc0),(float)cimg::abs(zyc - zyc0),0,
- foreground_color,0.9f,~0U).
- draw_point(xc0,zyc0,foreground_color,0.9f);
- }
- } break;
- }
- }
- // Draw text info.
- if (my>=0 && my<13) text_down = true; else if (my>=visu.height() - 13) text_down = false;
- if (!feature_type || !phase) {
- if (X>=0 && Y>=0 && Z>=0 && X<width() && Y<height() && Z<depth()) {
- if (_depth>1 || force_display_z_coord)
- cimg_snprintf(text,text._width," Point (%d,%d,%d) = [ ",origX + (int)X,origY + (int)Y,origZ + (int)Z);
- else cimg_snprintf(text,text._width," Point (%d,%d) = [ ",origX + (int)X,origY + (int)Y);
- CImg<T> values = get_vector_at((int)X,(int)Y,(int)Z);
- const bool is_large_spectrum = values._height>8;
- if (is_large_spectrum)
- values.draw_image(0,4,values.get_rows(values._height - 4,values._height - 1)).resize(1,8,1,1,0);
- char *ctext = text._data + std::strlen(text), *const ltext = text._data + 512;
- for (unsigned int c = 0; c<values._height && ctext<ltext; ++c) {
- cimg_snprintf(ctext,24,cimg::type<T>::format_s(),
- cimg::type<T>::format(values[c]));
- ctext += std::strlen(ctext);
- if (c==3 && is_large_spectrum) {
- cimg_snprintf(ctext,24," ...");
- ctext += std::strlen(ctext);
- }
- *(ctext++) = ' '; *ctext = 0;
- }
- std::strcpy(text._data + std::strlen(text),"] ");
- }
- } else switch (feature_type) {
- case 1 : {
- const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1),
- length = cimg::round(cimg::hypot(dX,dY,dZ),0.1);
- if (_depth>1 || force_display_z_coord)
- cimg_snprintf(text,text._width," Vect (%d,%d,%d)-(%d,%d,%d), Length = %g ",
- origX + X0,origY + Y0,origZ + Z0,origX + X1,origY + Y1,origZ + Z1,length);
- else if (_width!=1 && _height!=1)
- cimg_snprintf(text,text._width," Vect (%d,%d)-(%d,%d), Length = %g, Angle = %g\260 ",
- origX + X0,origY + Y0,origX + X1,origY + Y1,length,
- cimg::round(cimg::mod(180*std::atan2(-dY,-dX)/cimg::PI,360.),0.1));
- else
- cimg_snprintf(text,text._width," Vect (%d,%d)-(%d,%d), Length = %g ",
- origX + X0,origY + Y0,origX + X1,origY + Y1,length);
- } break;
- case 2 : {
- const double dX = (double)(X0 - X1), dY = (double)(Y0 - Y1), dZ = (double)(Z0 - Z1),
- length = cimg::round(cimg::hypot(dX,dY,dZ),0.1);
- if (_depth>1 || force_display_z_coord)
- cimg_snprintf(text,text._width,
- " Box ( %d,%d,%d ) - ( %d,%d,%d )\n Size = ( %d,%d,%d ), Length = %g ",
- origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),origZ + (Z0<Z1?Z0:Z1),
- origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),origZ + (Z0<Z1?Z1:Z0),
- 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),1 + cimg::abs(Z0 - Z1),length);
- else if (_width!=1 && _height!=1)
- cimg_snprintf(text,text._width,
- " Box ( %d,%d ) - ( %d,%d )\n Size = ( %d,%d ), Length = %g \n Angle = %g\260 ",
- origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),
- origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),
- 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),length,
- cimg::round(cimg::mod(180*std::atan2(-dY,-dX)/cimg::PI,360.),0.1));
- else
- cimg_snprintf(text,text._width,
- " Box ( %d,%d ) - ( %d,%d )\n Size = (%d,%d), Length = %g ",
- origX + (X0<X1?X0:X1),origY + (Y0<Y1?Y0:Y1),
- origX + (X0<X1?X1:X0),origY + (Y0<Y1?Y1:Y0),
- 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),length);
- } break;
- default :
- if (_depth>1 || force_display_z_coord)
- cimg_snprintf(text,text._width," Ellipse ( %d,%d,%d ) - ( %d,%d,%d ), Radii = ( %d,%d,%d ) ",
- origX + X0,origY + Y0,origZ + Z0,origX + X1,origY + Y1,origZ + Z1,
- 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1),1 + cimg::abs(Z0 - Z1));
- else cimg_snprintf(text,text._width," Ellipse ( %d,%d ) - ( %d,%d ), Radii = ( %d,%d ) ",
- origX + X0,origY + Y0,origX + X1,origY + Y1,
- 1 + cimg::abs(X0 - X1),1 + cimg::abs(Y0 - Y1));
- }
- if (phase || (mx>=0 && my>=0)) visu.__draw_text("%s",font_size,(int)text_down,text._data);
- }
- disp.display(visu);
- }
- if (!shape_selected) disp.wait();
- if (disp.is_resized()) { disp.resize(false)._is_resized = false; old_is_resized = true; visu0.assign(); }
- omx = mx; omy = my;
- if (!exit_on_anykey && key && key!=cimg::keyESC &&
- (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- key = 0;
- }
- }
- // Return result.
- CImg<intT> res(1,feature_type==0?3:6,1,1,-1);
- if (XYZ) { XYZ[0] = (unsigned int)X0; XYZ[1] = (unsigned int)Y0; XYZ[2] = (unsigned int)Z0; }
- if (shape_selected) {
- if (feature_type==2) {
- if (is_deep_selection) switch (area_started) {
- case 1 : Z0 = 0; Z1 = _depth - 1; break;
- case 2 : Y0 = 0; Y1 = _height - 1; break;
- case 3 : X0 = 0; X1 = _width - 1; break;
- }
- if (X0>X1) cimg::swap(X0,X1);
- if (Y0>Y1) cimg::swap(Y0,Y1);
- if (Z0>Z1) cimg::swap(Z0,Z1);
- }
- if (X1<0 || Y1<0 || Z1<0) X0 = Y0 = Z0 = X1 = Y1 = Z1 = -1;
- switch (feature_type) {
- case 1 : case 2 : res[0] = X0; res[1] = Y0; res[2] = Z0; res[3] = X1; res[4] = Y1; res[5] = Z1; break;
- case 3 :
- res[3] = cimg::abs(X1 - X0); res[4] = cimg::abs(Y1 - Y0); res[5] = cimg::abs(Z1 - Z0);
- res[0] = X0; res[1] = Y0; res[2] = Z0;
- break;
- default : res[0] = X0; res[1] = Y0; res[2] = Z0;
- }
- }
- if (!exit_on_anykey || !(disp.button()&4)) disp.set_button();
- if (!visible_cursor) disp.show_mouse();
- disp._normalization = old_normalization;
- disp._is_resized = old_is_resized;
- if (key!=~0U) disp.set_key(key);
- return res;
- }
- // Return a visualizable uchar8 image for display routines.
- CImg<ucharT> _get_select(const CImgDisplay& disp, const int normalization,
- const int x, const int y, const int z) const {
- if (is_empty()) return CImg<ucharT>(1,1,1,1,0);
- const CImg<T> crop = get_shared_channels(0,std::min(2,spectrum() - 1));
- CImg<Tuchar> img2d;
- if (_depth>1) {
- const int mdisp = std::min(disp.screen_width(),disp.screen_height());
- if (depth()>mdisp) {
- crop.get_resize(-100,-100,mdisp,-100,0).move_to(img2d);
- img2d.projections2d(x,y,z*img2d._depth/_depth);
- } else crop.get_projections2d(x,y,z).move_to(img2d);
- } else CImg<Tuchar>(crop,false).move_to(img2d);
- // Check for inf and NaN values.
- if (cimg::type<T>::is_float() && normalization) {
- bool is_inf = false, is_nan = false;
- cimg_for(img2d,ptr,Tuchar)
- if (cimg::type<T>::is_inf(*ptr)) { is_inf = true; break; }
- else if (cimg::type<T>::is_nan(*ptr)) { is_nan = true; break; }
- if (is_inf || is_nan) {
- Tint m0 = (Tint)cimg::type<T>::max(), M0 = (Tint)cimg::type<T>::min();
- if (!normalization) { m0 = 0; M0 = 255; }
- else if (normalization==2) { m0 = (Tint)disp._min; M0 = (Tint)disp._max; }
- else {
- cimg_for(img2d,ptr,Tuchar)
- if (!cimg::type<T>::is_inf(*ptr) && !cimg::type<T>::is_nan(*ptr)) {
- if (*ptr<(Tuchar)m0) m0 = *ptr;
- if (*ptr>(Tuchar)M0) M0 = *ptr;
- }
- }
- const T
- val_minf = (T)(normalization==1 || normalization==3?m0 - cimg::abs(m0):m0),
- val_pinf = (T)(normalization==1 || normalization==3?M0 + cimg::abs(M0):M0);
- if (is_nan)
- cimg_for(img2d,ptr,Tuchar)
- if (cimg::type<T>::is_nan(*ptr)) *ptr = val_minf; // Replace NaN values
- if (is_inf)
- cimg_for(img2d,ptr,Tuchar)
- if (cimg::type<T>::is_inf(*ptr)) *ptr = (float)*ptr<0?val_minf:val_pinf; // Replace +-inf values
- }
- }
- switch (normalization) {
- case 1 : img2d.normalize((ucharT)0,(ucharT)255); break;
- case 2 : {
- const float m = disp._min, M = disp._max;
- (img2d-=m)*=255.f/(M - m>0?M - m:1);
- } break;
- case 3 :
- if (cimg::type<T>::is_float()) img2d.normalize((ucharT)0,(ucharT)255);
- else {
- const float
- m = (float)cimg::type<T>::min(),
- M = (float)cimg::type<T>::max();
- (img2d-=m)*=255.f/(M - m>0?M - m:1);
- } break;
- }
- if (img2d.spectrum()==2) img2d.channels(0,2);
- return img2d;
- }
- //! Select sub-graph in a graph.
- CImg<intT> get_select_graph(CImgDisplay &disp,
- const unsigned int plot_type=1, const unsigned int vertex_type=1,
- const char *const labelx=0, const double xmin=0, const double xmax=0,
- const char *const labely=0, const double ymin=0, const double ymax=0,
- const bool exit_on_anykey=false) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "select_graph(): Empty instance.",
- cimg_instance);
- if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
- set_title("CImg<%s>",pixel_type());
- const ulongT siz = (ulongT)_width*_height*_depth;
- const unsigned int old_normalization = disp.normalization();
- disp.show().set_button().set_wheel()._normalization = 0;
- double nymin = ymin, nymax = ymax, nxmin = xmin, nxmax = xmax;
- if (nymin==nymax) { nymin = (Tfloat)min_max(nymax); const double dy = nymax - nymin; nymin-=dy/20; nymax+=dy/20; }
- if (nymin==nymax) { --nymin; ++nymax; }
- if (nxmin==nxmax && nxmin==0) { nxmin = 0; nxmax = siz - 1.; }
- static const unsigned char black[] = { 0, 0, 0 }, white[] = { 255, 255, 255 }, gray[] = { 220, 220, 220 };
- static const unsigned char gray2[] = { 110, 110, 110 }, ngray[] = { 35, 35, 35 };
- CImg<ucharT> colormap(3,_spectrum);
- if (_spectrum==1) { colormap[0] = colormap[1] = 120; colormap[2] = 200; }
- else {
- colormap(0,0) = 220; colormap(1,0) = 10; colormap(2,0) = 10;
- if (_spectrum>1) { colormap(0,1) = 10; colormap(1,1) = 220; colormap(2,1) = 10; }
- if (_spectrum>2) { colormap(0,2) = 10; colormap(1,2) = 10; colormap(2,2) = 220; }
- if (_spectrum>3) { colormap(0,3) = 220; colormap(1,3) = 220; colormap(2,3) = 10; }
- if (_spectrum>4) { colormap(0,4) = 220; colormap(1,4) = 10; colormap(2,4) = 220; }
- if (_spectrum>5) { colormap(0,5) = 10; colormap(1,5) = 220; colormap(2,5) = 220; }
- if (_spectrum>6) {
- cimg_uint64 rng = 10;
- cimg_for_inY(colormap,6,colormap.height()-1,k) {
- colormap(0,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
- colormap(1,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
- colormap(2,k) = (unsigned char)(120 + cimg::rand(-100.f,100.f,&rng));
- }
- }
- }
- CImg<ucharT> visu0, visu, graph, text, axes;
- int x0 = -1, x1 = -1, y0 = -1, y1 = -1, omouse_x = -2, omouse_y = -2;
- const unsigned int one = plot_type==3?0U:1U;
- unsigned int okey = 0, obutton = 0, font_size = 32;
- CImg<charT> message(1024);
- CImg_3x3(I,unsigned char);
- for (bool selected = false; !selected && !disp.is_closed() && !okey && !disp.wheel(); ) {
- const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y();
- const unsigned int key = disp.key(), button = disp.button();
- // Generate graph representation.
- if (!visu0) {
- visu0.assign(disp.width(),disp.height(),1,3,220);
- const int gdimx = disp.width() - 32, gdimy = disp.height() - 32;
- if (gdimx>0 && gdimy>0) {
- graph.assign(gdimx,gdimy,1,3,255);
- if (siz<32) {
- if (siz>1) graph.draw_grid(gdimx/(float)(siz - one),gdimy/(float)(siz - one),0,0,
- false,true,black,0.2f,0x33333333,0x33333333);
- } else graph.draw_grid(-10,-10,0,0,false,true,black,0.2f,0x33333333,0x33333333);
- cimg_forC(*this,c)
- graph.draw_graph(get_shared_channel(c),&colormap(0,c),(plot_type!=3 || _spectrum==1)?1:0.6f,
- plot_type,vertex_type,nymax,nymin);
- axes.assign(gdimx,gdimy,1,1,0);
- const float
- dx = (float)cimg::abs(nxmax - nxmin), dy = (float)cimg::abs(nymax - nymin),
- px = (float)std::pow(10.,(int)std::log10(dx?dx:1) - 2.),
- py = (float)std::pow(10.,(int)std::log10(dy?dy:1) - 2.);
- const CImg<Tdouble>
- seqx = dx<=0?CImg<Tdouble>::vector(nxmin):
- CImg<Tdouble>::sequence(1 + gdimx/60,nxmin,one?nxmax:nxmin + (nxmax - nxmin)*(siz + 1)/siz),
- seqy = CImg<Tdouble>::sequence(1 + gdimy/60,nymax,nymin);
- const bool allow_zero = (nxmin*nxmax>0) || (nymin*nymax>0);
- axes.draw_axes(seqx,seqy,white,1,~0U,~0U,13,allow_zero,px,py);
- if (nymin>0) axes.draw_axis(seqx,gdimy - 1,gray,1,~0U,13,allow_zero,px);
- if (nymax<0) axes.draw_axis(seqx,0,gray,1,~0U,13,allow_zero,px);
- if (nxmin>0) axes.draw_axis(0,seqy,gray,1,~0U,13,allow_zero,py);
- if (nxmax<0) axes.draw_axis(gdimx - 1,seqy,gray,1,~0U,13,allow_zero,py);
- cimg_for3x3(axes,x,y,0,0,I,unsigned char)
- if (Icc) {
- if (Icc==255) cimg_forC(graph,c) graph(x,y,c) = 0;
- else cimg_forC(graph,c) graph(x,y,c) = (unsigned char)(2*graph(x,y,c)/3);
- }
- else if (Ipc || Inc || Icp || Icn || Ipp || Inn || Ipn || Inp)
- cimg_forC(graph,c) graph(x,y,c) = (unsigned char)((graph(x,y,c) + 511)/3);
- visu0.draw_image(16,16,graph);
- visu0.draw_line(15,15,16 + gdimx,15,gray2).draw_line(16 + gdimx,15,16 + gdimx,16 + gdimy,gray2).
- draw_line(16 + gdimx,16 + gdimy,15,16 + gdimy,white).draw_line(15,16 + gdimy,15,15,white);
- } else graph.assign();
- text.assign().draw_text(0,0,labelx?labelx:"X-axis",white,ngray,1,13).resize(-100,-100,1,3);
- visu0.draw_image((visu0.width() - text.width())/2,visu0.height() - 14,~text);
- text.assign().draw_text(0,0,labely?labely:"Y-axis",white,ngray,1,13).rotate(-90).resize(-100,-100,1,3);
- visu0.draw_image(1,(visu0.height() - text.height())/2,~text);
- visu.assign();
- }
- // Generate and display current view.
- if (!visu) {
- visu.assign(visu0);
- if (graph && x0>=0 && x1>=0) {
- const int
- nx0 = x0<=x1?x0:x1,
- nx1 = x0<=x1?x1:x0,
- ny0 = y0<=y1?y0:y1,
- ny1 = y0<=y1?y1:y0,
- sx0 = (int)(16 + nx0*(visu.width() - 32)/std::max((ulongT)1,siz - one)),
- sx1 = (int)(15 + (nx1 + 1)*(visu.width() - 32)/std::max((ulongT)1,siz - one)),
- sy0 = 16 + ny0,
- sy1 = 16 + ny1;
- if (y0>=0 && y1>=0)
- visu.draw_rectangle(sx0,sy0,sx1,sy1,gray,0.5f).draw_rectangle(sx0,sy0,sx1,sy1,black,0.5f,0xCCCCCCCCU);
- else visu.draw_rectangle(sx0,0,sx1,visu.height() - 17,gray,0.5f).
- draw_line(sx0,16,sx0,visu.height() - 17,black,0.5f,0xCCCCCCCCU).
- draw_line(sx1,16,sx1,visu.height() - 17,black,0.5f,0xCCCCCCCCU);
- }
- if (mouse_x>=16 && mouse_y>=16 && mouse_x<visu.width() - 16 && mouse_y<visu.height() - 16) {
- if (graph) visu.draw_line(mouse_x,16,mouse_x,visu.height() - 17,black,0.5f,0x55555555U);
- const unsigned int
- x = (unsigned int)cimg::round((mouse_x - 16.f)*(siz - one)/(disp.width() - 32),1,one?0:-1);
- const double cx = nxmin + x*(nxmax - nxmin)/std::max((ulongT)1,siz - 1);
- if (_spectrum>=7)
- cimg_snprintf(message,message._width,"Value[%u:%g] = ( %g %g %g ... %g %g %g )",x,cx,
- (double)(*this)(x,0,0,0),(double)(*this)(x,0,0,1),(double)(*this)(x,0,0,2),
- (double)(*this)(x,0,0,_spectrum - 4),(double)(*this)(x,0,0,_spectrum - 3),
- (double)(*this)(x,0,0,_spectrum - 1));
- else {
- cimg_snprintf(message,message._width,"Value[%u:%g] = ( ",x,cx);
- cimg_forC(*this,c) cimg_sprintf(message._data + std::strlen(message),"%g ",(double)(*this)(x,0,0,c));
- cimg_sprintf(message._data + std::strlen(message),")");
- }
- if (x0>=0 && x1>=0) {
- const unsigned int
- nx0 = (unsigned int)(x0<=x1?x0:x1),
- nx1 = (unsigned int)(x0<=x1?x1:x0),
- ny0 = (unsigned int)(y0<=y1?y0:y1),
- ny1 = (unsigned int)(y0<=y1?y1:y0);
- const double
- cx0 = nxmin + nx0*(nxmax - nxmin)/std::max((ulongT)1,siz - 1),
- cx1 = nxmin + (nx1 + one)*(nxmax - nxmin)/std::max((ulongT)1,siz - 1),
- cy0 = nymax - ny0*(nymax - nymin)/(visu._height - 32),
- cy1 = nymax - ny1*(nymax - nymin)/(visu._height - 32);
- if (y0>=0 && y1>=0)
- cimg_sprintf(message._data + std::strlen(message)," - Range ( %u:%g, %g ) - ( %u:%g, %g )",
- x0,cx0,cy0,x1 + one,cx1,cy1);
- else
- cimg_sprintf(message._data + std::strlen(message)," - Range [ %u:%g - %u:%g ]",
- x0,cx0,x1 + one,cx1);
- }
- text.assign().draw_text(0,0,message,white,ngray,1,13).resize(-100,-100,1,3);
- visu.draw_image((visu.width() - text.width())/2,1,~text);
- }
- visu.display(disp);
- }
- // Test keys.
- CImg<charT> filename(32);
- switch (okey = key) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT :
- #endif
- case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : okey = 0; break;
- case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
- CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
- _is_resized = true;
- disp.set_key(key,false); okey = 0;
- } break;
- case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
- disp.set_key(key,false); okey = 0;
- } break;
- case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(CImgDisplay::screen_width()/2,
- CImgDisplay::screen_height()/2,1),false)._is_resized = true;
- disp.set_key(key,false); okey = 0;
- } break;
- case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
- disp.set_key(key,false); okey = 0;
- } break;
- case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- if (visu || visu0) {
- CImg<ucharT> &screen = visu?visu:visu0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+screen).__draw_text(" Saving snapshot... ",font_size,0).display(disp);
- screen.save(filename);
- (+screen).__draw_text(" Snapshot '%s' saved. ",font_size,0,filename._data).display(disp);
- }
- disp.set_key(key,false); okey = 0;
- } break;
- case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- if (visu || visu0) {
- CImg<ucharT> &screen = visu?visu:visu0;
- std::FILE *file;
- do {
- #ifdef cimg_use_zlib
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
- #else
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
- #endif
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+screen).__draw_text(" Saving instance... ",font_size,0).display(disp);
- save(filename);
- (+screen).__draw_text(" Instance '%s' saved. ",font_size,0,filename._data).display(disp);
- }
- disp.set_key(key,false); okey = 0;
- } break;
- }
- // Handle mouse motion and mouse buttons.
- if (obutton!=button || omouse_x!=mouse_x || omouse_y!=mouse_y) {
- visu.assign();
- if (disp.mouse_x()>=0 && disp.mouse_y()>=0) {
- const int
- mx = (mouse_x - 16)*(int)(siz - one)/(disp.width() - 32),
- cx = cimg::cut(mx,0,(int)(siz - 1 - one)),
- my = mouse_y - 16,
- cy = cimg::cut(my,0,disp.height() - 32);
- if (button&1) {
- if (!obutton) { x0 = cx; y0 = -1; } else { x1 = cx; y1 = -1; }
- }
- else if (button&2) {
- if (!obutton) { x0 = cx; y0 = cy; } else { x1 = cx; y1 = cy; }
- }
- else if (obutton) { x1 = x1>=0?cx:-1; y1 = y1>=0?cy:-1; selected = true; }
- } else if (!button && obutton) selected = true;
- obutton = button; omouse_x = mouse_x; omouse_y = mouse_y;
- }
- if (disp.is_resized()) { disp.resize(false); visu0.assign(); }
- if (visu && visu0) disp.wait();
- if (!exit_on_anykey && okey && okey!=cimg::keyESC &&
- (okey!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- disp.set_key(key,false);
- okey = 0;
- }
- }
- disp._normalization = old_normalization;
- if (x1>=0 && x1<x0) cimg::swap(x0,x1);
- if (y1<y0) cimg::swap(y0,y1);
- disp.set_key(okey);
- return CImg<intT>(4,1,1,1,x0,y0,x1>=0?x1 + (int)one:-1,y1);
- }
- //! Load image from a file.
- /**
- \param filename Filename, as a C-string.
- \note The extension of \c filename defines the file format. If no filename
- extension is provided, CImg<T>::get_load() will try to load the file as a .cimg or .cimgz file.
- **/
- CImg<T>& load(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load(): Specified filename is (null).",
- cimg_instance);
- if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) {
- CImg<charT> filename_local(256);
- load(cimg::load_network(filename,filename_local));
- std::remove(filename_local);
- return *this;
- }
- const char *const ext = cimg::split_filename(filename);
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- bool is_loaded = true;
- try {
- #ifdef cimg_load_plugin
- cimg_load_plugin(filename);
- #endif
- #ifdef cimg_load_plugin1
- cimg_load_plugin1(filename);
- #endif
- #ifdef cimg_load_plugin2
- cimg_load_plugin2(filename);
- #endif
- #ifdef cimg_load_plugin3
- cimg_load_plugin3(filename);
- #endif
- #ifdef cimg_load_plugin4
- cimg_load_plugin4(filename);
- #endif
- #ifdef cimg_load_plugin5
- cimg_load_plugin5(filename);
- #endif
- #ifdef cimg_load_plugin6
- cimg_load_plugin6(filename);
- #endif
- #ifdef cimg_load_plugin7
- cimg_load_plugin7(filename);
- #endif
- #ifdef cimg_load_plugin8
- cimg_load_plugin8(filename);
- #endif
- // Text formats
- if (!cimg::strcasecmp(ext,"asc")) load_ascii(filename);
- else if (!cimg::strcasecmp(ext,"csv") ||
- !cimg::strcasecmp(ext,"dlm") ||
- !cimg::strcasecmp(ext,"txt")) load_dlm(filename);
- else if (!cimg::strcasecmp(ext,"pdf")) load_pdf_external(filename);
- // 2D binary formats
- else if (!cimg::strcasecmp(ext,"bmp")) load_bmp(filename);
- else if (!cimg::strcasecmp(ext,"jpg") ||
- !cimg::strcasecmp(ext,"jpeg") ||
- !cimg::strcasecmp(ext,"jpe") ||
- !cimg::strcasecmp(ext,"jfif") ||
- !cimg::strcasecmp(ext,"jif")) load_jpeg(filename);
- else if (!cimg::strcasecmp(ext,"png")) load_png(filename);
- else if (!cimg::strcasecmp(ext,"ppm") ||
- !cimg::strcasecmp(ext,"pgm") ||
- !cimg::strcasecmp(ext,"pnm") ||
- !cimg::strcasecmp(ext,"pbm") ||
- !cimg::strcasecmp(ext,"pnk")) load_pnm(filename);
- else if (!cimg::strcasecmp(ext,"pfm")) load_pfm(filename);
- else if (!cimg::strcasecmp(ext,"tif") ||
- !cimg::strcasecmp(ext,"tiff")) load_tiff(filename);
- else if (!cimg::strcasecmp(ext,"exr")) load_exr(filename);
- else if (!cimg::strcasecmp(ext,"arw") ||
- !cimg::strcasecmp(ext,"cr2") ||
- !cimg::strcasecmp(ext,"crw") ||
- !cimg::strcasecmp(ext,"dcr") ||
- !cimg::strcasecmp(ext,"dng") ||
- !cimg::strcasecmp(ext,"mrw") ||
- !cimg::strcasecmp(ext,"nef") ||
- !cimg::strcasecmp(ext,"orf") ||
- !cimg::strcasecmp(ext,"pix") ||
- !cimg::strcasecmp(ext,"ptx") ||
- !cimg::strcasecmp(ext,"raf") ||
- !cimg::strcasecmp(ext,"srf")) load_dcraw_external(filename);
- else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename);
- else if (!cimg::strcasecmp(ext,"heic") ||
- !cimg::strcasecmp(ext,"avif")) load_heif(filename);
- // 3D binary formats
- else if (!cimg::strcasecmp(ext,"dcm") ||
- !cimg::strcasecmp(ext,"dicom")) load_medcon_external(filename);
- else if (!cimg::strcasecmp(ext,"hdr") ||
- !cimg::strcasecmp(ext,"nii")) load_analyze(filename);
- else if (!cimg::strcasecmp(ext,"par") ||
- !cimg::strcasecmp(ext,"rec")) load_parrec(filename);
- else if (!cimg::strcasecmp(ext,"mnc")) load_minc2(filename);
- else if (!cimg::strcasecmp(ext,"inr")) load_inr(filename);
- else if (!cimg::strcasecmp(ext,"pan")) load_pandore(filename);
- else if (!cimg::strcasecmp(ext,"cimg") ||
- !cimg::strcasecmp(ext,"cimgz") ||
- !*ext) return load_cimg(filename);
- // Archive files
- else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename);
- // Image sequences
- else if (!cimg::strcasecmp(ext,"avi") ||
- !cimg::strcasecmp(ext,"mov") ||
- !cimg::strcasecmp(ext,"asf") ||
- !cimg::strcasecmp(ext,"divx") ||
- !cimg::strcasecmp(ext,"flv") ||
- !cimg::strcasecmp(ext,"mpg") ||
- !cimg::strcasecmp(ext,"m1v") ||
- !cimg::strcasecmp(ext,"m2v") ||
- !cimg::strcasecmp(ext,"m4v") ||
- !cimg::strcasecmp(ext,"mjp") ||
- !cimg::strcasecmp(ext,"mp4") ||
- !cimg::strcasecmp(ext,"mkv") ||
- !cimg::strcasecmp(ext,"mpe") ||
- !cimg::strcasecmp(ext,"movie") ||
- !cimg::strcasecmp(ext,"ogm") ||
- !cimg::strcasecmp(ext,"ogg") ||
- !cimg::strcasecmp(ext,"ogv") ||
- !cimg::strcasecmp(ext,"qt") ||
- !cimg::strcasecmp(ext,"rm") ||
- !cimg::strcasecmp(ext,"vob") ||
- !cimg::strcasecmp(ext,"webm") ||
- !cimg::strcasecmp(ext,"wmv") ||
- !cimg::strcasecmp(ext,"xvid") ||
- !cimg::strcasecmp(ext,"mpeg")) load_video(filename);
- else is_loaded = false;
- } catch (CImgIOException&) { is_loaded = false; }
- // If nothing loaded, try to guess file format from magic number in file.
- if (!is_loaded) {
- std::FILE *file = cimg::std_fopen(filename,"rb");
- if (!file) {
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load(): Failed to open file '%s'.",
- cimg_instance,
- filename);
- }
- const char *const f_type = cimg::ftype(file,filename);
- cimg::fclose(file);
- is_loaded = true;
- try {
- if (!cimg::strcasecmp(f_type,"pnm")) load_pnm(filename);
- else if (!cimg::strcasecmp(f_type,"pfm")) load_pfm(filename);
- else if (!cimg::strcasecmp(f_type,"bmp")) load_bmp(filename);
- else if (!cimg::strcasecmp(f_type,"inr")) load_inr(filename);
- else if (!cimg::strcasecmp(f_type,"jpg")) load_jpeg(filename);
- else if (!cimg::strcasecmp(f_type,"pan")) load_pandore(filename);
- else if (!cimg::strcasecmp(f_type,"png")) load_png(filename);
- else if (!cimg::strcasecmp(f_type,"tif")) load_tiff(filename);
- else if (!cimg::strcasecmp(f_type,"gif")) load_gif_external(filename);
- else if (!cimg::strcasecmp(f_type,"dcm")) load_medcon_external(filename);
- else is_loaded = false;
- } catch (CImgIOException&) { is_loaded = false; }
- }
- // If nothing loaded, try to load file with other means.
- if (!is_loaded) {
- try {
- load_other(filename);
- } catch (CImgIOException&) {
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load(): Failed to recognize format of file '%s'.",
- cimg_instance,
- filename);
- }
- }
- cimg::exception_mode(omode);
- return *this;
- }
- //! Load image from a file \newinstance.
- static CImg<T> get_load(const char *const filename) {
- return CImg<T>().load(filename);
- }
- //! Load image from an ascii file.
- /**
- \param filename Filename, as a C -string.
- **/
- CImg<T>& load_ascii(const char *const filename) {
- return _load_ascii(0,filename);
- }
- //! Load image from an ascii file \inplace.
- static CImg<T> get_load_ascii(const char *const filename) {
- return CImg<T>().load_ascii(filename);
- }
- //! Load image from an ascii file \overloading.
- CImg<T>& load_ascii(std::FILE *const file) {
- return _load_ascii(file,0);
- }
- //! Loadimage from an ascii file \newinstance.
- static CImg<T> get_load_ascii(std::FILE *const file) {
- return CImg<T>().load_ascii(file);
- }
- CImg<T>& _load_ascii(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_ascii(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- CImg<charT> line(256); *line = 0;
- int err = std::fscanf(nfile,"%255[^\n]",line._data);
- unsigned int dx = 0, dy = 1, dz = 1, dc = 1;
- cimg_sscanf(line,"%u%*c%u%*c%u%*c%u",&dx,&dy,&dz,&dc);
- err = std::fscanf(nfile,"%*[^0-9.eEinfa+-]");
- if (!dx || !dy || !dz || !dc) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_ascii(): Invalid ascii header in file '%s', image dimensions are set "
- "to (%u,%u,%u,%u).",
- cimg_instance,
- filename?filename:"(FILE*)",dx,dy,dz,dc);
- }
- assign(dx,dy,dz,dc);
- const ulongT siz = size();
- ulongT off = 0;
- double val;
- T *ptr = _data;
- for (err = 1, off = 0; off<siz && err==1; ++off) {
- err = std::fscanf(nfile,"%lf%*[^0-9.eEinfa+-]",&val);
- *(ptr++) = (T)val;
- }
- if (err!=1)
- cimg::warn(_cimg_instance
- "load_ascii(): Only %lu/%lu values read from file '%s'.",
- cimg_instance,
- off - 1,siz,filename?filename:"(FILE*)");
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a DLM file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_dlm(const char *const filename) {
- return _load_dlm(0,filename);
- }
- //! Load image from a DLM file \newinstance.
- static CImg<T> get_load_dlm(const char *const filename) {
- return CImg<T>().load_dlm(filename);
- }
- //! Load image from a DLM file \overloading.
- CImg<T>& load_dlm(std::FILE *const file) {
- return _load_dlm(file,0);
- }
- //! Load image from a DLM file \newinstance.
- static CImg<T> get_load_dlm(std::FILE *const file) {
- return CImg<T>().load_dlm(file);
- }
- CImg<T>& _load_dlm(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_dlm(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"r");
- CImg<charT> delimiter(256), tmp(256); *delimiter = *tmp = 0;
- unsigned int cdx = 0, dx = 0, dy = 0;
- int err = 0;
- double val;
- assign(256,256,1,1,(T)0);
- while ((err = std::fscanf(nfile,"%lf%255[^0-9eEinfa.+-]",&val,delimiter._data))>0) {
- if (err>0) (*this)(cdx++,dy) = (T)val;
- if (cdx>=_width) resize(3*_width/2,_height,1,1,0);
- char c = 0;
- if (!cimg_sscanf(delimiter,"%255[^\n]%c",tmp._data,&c) || c=='\n') {
- dx = std::max(cdx,dx);
- if (++dy>=_height) resize(_width,3*_height/2,1,1,0);
- cdx = 0;
- }
- }
- if (cdx && err==1) { dx = cdx; ++dy; }
- if (!dx || !dy) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_dlm(): Invalid DLM file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- resize(dx,dy,1,1,0);
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a BMP file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_bmp(const char *const filename) {
- return _load_bmp(0,filename);
- }
- //! Load image from a BMP file \newinstance.
- static CImg<T> get_load_bmp(const char *const filename) {
- return CImg<T>().load_bmp(filename);
- }
- //! Load image from a BMP file \overloading.
- CImg<T>& load_bmp(std::FILE *const file) {
- return _load_bmp(file,0);
- }
- //! Load image from a BMP file \newinstance.
- static CImg<T> get_load_bmp(std::FILE *const file) {
- return CImg<T>().load_bmp(file);
- }
- CImg<T>& _load_bmp(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_bmp(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- CImg<ucharT> header(54);
- cimg::fread(header._data,54,nfile);
- if (*header!='B' || header[1]!='M') {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_bmp(): Invalid BMP file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- // Read header and pixel buffer
- int
- file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),
- offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24),
- header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24),
- dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24),
- dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24),
- compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24),
- nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),
- bpp = header[0x1C] + (header[0x1D]<<8);
- if (!file_size || file_size==offset) {
- cimg::fseek(nfile,0,SEEK_END);
- file_size = (int)cimg::ftell(nfile);
- cimg::fseek(nfile,54,SEEK_SET);
- }
- if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);
- const int
- dx_bytes = (bpp==1)?(dx/8 + (dx%8?1:0)):((bpp==4)?(dx/2 + (dx%2)):(int)((longT)dx*bpp/8)),
- align_bytes = (4 - dx_bytes%4)%4;
- const ulongT
- cimg_iobuffer = (ulongT)24*1024*1024,
- buf_size = (ulongT)cimg::abs(dy)*(dx_bytes + align_bytes);
- CImg<intT> colormap;
- if (bpp<16) { if (!nb_colors) nb_colors = 1<<bpp; } else nb_colors = 0;
- if (nb_colors) { colormap.assign(nb_colors); cimg::fread(colormap._data,nb_colors,nfile); }
- const int xoffset = offset - 14 - header_size - 4*nb_colors;
- if (xoffset>0) cimg::fseek(nfile,xoffset,SEEK_CUR);
- CImg<ucharT> buffer;
- if (buf_size<cimg_iobuffer) {
- buffer.assign(buf_size,1,1,1,0);
- cimg::fread(buffer._data,buf_size,nfile);
- } else buffer.assign(dx_bytes + align_bytes);
- unsigned char *ptrs = buffer;
- // Decompress buffer (if necessary)
- if (compression==1 || compression==2) {
- if (file)
- throw CImgIOException(_cimg_instance
- "load_bmp(): Unable to load compressed data from '(*FILE)' inputs.",
- cimg_instance);
- else {
- if (!file) cimg::fclose(nfile);
- return load_other(filename);
- }
- }
- // Read pixel data
- assign(dx,cimg::abs(dy),1,3,0);
- switch (bpp) {
- case 1 : { // Monochrome
- if (colormap._width>=2) for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- unsigned char mask = 0x80, val = 0;
- cimg_forX(*this,x) {
- if (mask==0x80) val = *(ptrs++);
- const unsigned char *col = (unsigned char*)(colormap._data + (val&mask?1:0));
- (*this)(x,y,2) = (T)*(col++);
- (*this)(x,y,1) = (T)*(col++);
- (*this)(x,y,0) = (T)*(col++);
- mask = cimg::ror(mask);
- }
- ptrs+=align_bytes;
- }
- } break;
- case 4 : { // 16 colors
- if (colormap._width>=16) for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- unsigned char mask = 0xF0, val = 0;
- cimg_forX(*this,x) {
- if (mask==0xF0) val = *(ptrs++);
- const unsigned char color = (unsigned char)((mask<16)?(val&mask):((val&mask)>>4));
- const unsigned char *col = (unsigned char*)(colormap._data + color);
- (*this)(x,y,2) = (T)*(col++);
- (*this)(x,y,1) = (T)*(col++);
- (*this)(x,y,0) = (T)*(col++);
- mask = cimg::ror(mask,4);
- }
- ptrs+=align_bytes;
- }
- } break;
- case 8 : { // 256 colors
- if (colormap._width>=256) for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- cimg_forX(*this,x) {
- const unsigned char *col = (unsigned char*)(colormap._data + *(ptrs++));
- (*this)(x,y,2) = (T)*(col++);
- (*this)(x,y,1) = (T)*(col++);
- (*this)(x,y,0) = (T)*(col++);
- }
- ptrs+=align_bytes;
- }
- } break;
- case 16 : { // 16 bits colors (RGB565)
- for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- cimg_forX(*this,x) {
- const unsigned char c1 = *(ptrs++), c2 = *(ptrs++);
- const unsigned short col = (unsigned short)c2<<8 | c1;
- (*this)(x,y,2) = (T)((col&0x1F)<<3);
- (*this)(x,y,1) = (T)(((col>>5)&0x3F)<<3);
- (*this)(x,y,0) = (T)(((col>>11)&0x1F)<<3);
- }
- ptrs+=align_bytes;
- }
- } break;
- case 24 : { // 24 bits colors
- for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- cimg_forX(*this,x) {
- (*this)(x,y,2) = (T)*(ptrs++);
- (*this)(x,y,1) = (T)*(ptrs++);
- (*this)(x,y,0) = (T)*(ptrs++);
- }
- ptrs+=align_bytes;
- }
- } break;
- case 32 : { // 32 bits colors
- for (int y = height() - 1; y>=0; --y) {
- if (buf_size>=cimg_iobuffer) {
- if (!cimg::fread(ptrs=buffer._data,dx_bytes,nfile)) break;
- cimg::fseek(nfile,align_bytes,SEEK_CUR);
- }
- cimg_forX(*this,x) {
- (*this)(x,y,2) = (T)*(ptrs++);
- (*this)(x,y,1) = (T)*(ptrs++);
- (*this)(x,y,0) = (T)*(ptrs++);
- ++ptrs;
- }
- ptrs+=align_bytes;
- }
- } break;
- }
- if (dy<0) mirror('y');
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a JPEG file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_jpeg(const char *const filename) {
- return _load_jpeg(0,filename);
- }
- //! Load image from a JPEG file \newinstance.
- static CImg<T> get_load_jpeg(const char *const filename) {
- return CImg<T>().load_jpeg(filename);
- }
- //! Load image from a JPEG file \overloading.
- CImg<T>& load_jpeg(std::FILE *const file) {
- return _load_jpeg(file,0);
- }
- //! Load image from a JPEG file \newinstance.
- static CImg<T> get_load_jpeg(std::FILE *const file) {
- return CImg<T>().load_jpeg(file);
- }
- // Custom error handler for libjpeg.
- #ifdef cimg_use_jpeg
- struct _cimg_error_mgr {
- struct jpeg_error_mgr original;
- jmp_buf setjmp_buffer;
- char message[JMSG_LENGTH_MAX];
- };
- typedef struct _cimg_error_mgr *_cimg_error_ptr;
- METHODDEF(void) _cimg_jpeg_error_exit(j_common_ptr cinfo) {
- _cimg_error_ptr c_err = (_cimg_error_ptr) cinfo->err; // Return control to the setjmp point
- (*cinfo->err->format_message)(cinfo,c_err->message);
- jpeg_destroy(cinfo); // Clean memory and temp files
- longjmp(c_err->setjmp_buffer,1);
- }
- #endif
- CImg<T>& _load_jpeg(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_jpeg(): Specified filename is (null).",
- cimg_instance);
- #ifndef cimg_use_jpeg
- if (file)
- throw CImgIOException(_cimg_instance
- "load_jpeg(): Unable to load data from '(FILE*)' unless libjpeg is enabled.",
- cimg_instance);
- else return load_other(filename);
- #else
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- struct jpeg_decompress_struct cinfo;
- struct _cimg_error_mgr jerr;
- cinfo.err = jpeg_std_error(&jerr.original);
- jerr.original.error_exit = _cimg_jpeg_error_exit;
- if (setjmp(jerr.setjmp_buffer)) { // JPEG error
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_jpeg(): Error message returned by libjpeg: %s.",
- cimg_instance,jerr.message);
- }
- jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo,nfile);
- jpeg_read_header(&cinfo,TRUE);
- jpeg_start_decompress(&cinfo);
- if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) {
- if (!file) {
- cimg::fclose(nfile);
- return load_other(filename);
- } else
- throw CImgIOException(_cimg_instance
- "load_jpeg(): Failed to load JPEG data from file '%s'.",
- cimg_instance,filename?filename:"(FILE*)");
- }
- CImg<ucharT> buffer(cinfo.output_width*cinfo.output_components);
- JSAMPROW row_pointer[1];
- try { assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); }
- catch (...) { if (!file) cimg::fclose(nfile); throw; }
- T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height,
- *ptr_a = _data + 3UL*_width*_height;
- while (cinfo.output_scanline<cinfo.output_height) {
- *row_pointer = buffer._data;
- if (jpeg_read_scanlines(&cinfo,row_pointer,1)!=1) {
- cimg::warn(_cimg_instance
- "load_jpeg(): Incomplete data in file '%s'.",
- cimg_instance,filename?filename:"(FILE*)");
- break;
- }
- const unsigned char *ptrs = buffer._data;
- switch (_spectrum) {
- case 1 : {
- cimg_forX(*this,x) *(ptr_r++) = (T)*(ptrs++);
- } break;
- case 3 : {
- cimg_forX(*this,x) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- } break;
- case 4 : {
- cimg_forX(*this,x) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- *(ptr_a++) = (T)*(ptrs++);
- }
- } break;
- }
- }
- jpeg_finish_decompress(&cinfo);
- jpeg_destroy_decompress(&cinfo);
- if (!file) cimg::fclose(nfile);
- return *this;
- #endif
- }
- //! Load image from a file, using Magick++ library.
- /**
- \param filename Filename, as a C-string.
- **/
- // Added April/may 2006 by Christoph Hormann <[email protected]>
- // This is experimental code, not much tested, use with care.
- CImg<T>& load_magick(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_magick(): Specified filename is (null).",
- cimg_instance);
- #ifdef cimg_use_magick
- Magick::Image image(filename);
- const unsigned int W = image.size().width(), H = image.size().height();
- switch (image.type()) {
- case Magick::PaletteMatteType :
- case Magick::TrueColorMatteType :
- case Magick::ColorSeparationType : {
- assign(W,H,1,4);
- 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);
- Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
- for (ulongT off = (ulongT)W*H; off; --off) {
- *(ptr_r++) = (T)(pixels->red);
- *(ptr_g++) = (T)(pixels->green);
- *(ptr_b++) = (T)(pixels->blue);
- *(ptr_a++) = (T)(pixels->opacity);
- ++pixels;
- }
- } break;
- case Magick::PaletteType :
- case Magick::TrueColorType : {
- assign(W,H,1,3);
- T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
- Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
- for (ulongT off = (ulongT)W*H; off; --off) {
- *(ptr_r++) = (T)(pixels->red);
- *(ptr_g++) = (T)(pixels->green);
- *(ptr_b++) = (T)(pixels->blue);
- ++pixels;
- }
- } break;
- case Magick::GrayscaleMatteType : {
- assign(W,H,1,2);
- T *ptr_r = data(0,0,0,0), *ptr_a = data(0,0,0,1);
- Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
- for (ulongT off = (ulongT)W*H; off; --off) {
- *(ptr_r++) = (T)(pixels->red);
- *(ptr_a++) = (T)(pixels->opacity);
- ++pixels;
- }
- } break;
- default : {
- assign(W,H,1,1);
- T *ptr_r = data(0,0,0,0);
- Magick::PixelPacket *pixels = image.getPixels(0,0,W,H);
- for (ulongT off = (ulongT)W*H; off; --off) {
- *(ptr_r++) = (T)(pixels->red);
- ++pixels;
- }
- }
- }
- return *this;
- #else
- throw CImgIOException(_cimg_instance
- "load_magick(): Unable to load file '%s' unless libMagick++ is enabled.",
- cimg_instance,
- filename);
- #endif
- }
- //! Load image from a file, using Magick++ library \newinstance.
- static CImg<T> get_load_magick(const char *const filename) {
- return CImg<T>().load_magick(filename);
- }
- //! Load image from a PNG file.
- /**
- \param filename Filename, as a C-string.
- \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
- **/
- CImg<T>& load_png(const char *const filename, unsigned int *const bits_per_value=0) {
- return _load_png(0,filename,bits_per_value);
- }
- //! Load image from a PNG file \newinstance.
- static CImg<T> get_load_png(const char *const filename, unsigned int *const bits_per_value=0) {
- return CImg<T>().load_png(filename,bits_per_value);
- }
- //! Load image from a PNG file \overloading.
- CImg<T>& load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
- return _load_png(file,0,bits_per_value);
- }
- //! Load image from a PNG file \newinstance.
- static CImg<T> get_load_png(std::FILE *const file, unsigned int *const bits_per_value=0) {
- return CImg<T>().load_png(file,bits_per_value);
- }
- // (Note: Most of this function has been written by Eric Fausett)
- CImg<T>& _load_png(std::FILE *const file, const char *const filename, unsigned int *const bits_per_value) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_png(): Specified filename is (null).",
- cimg_instance);
- #ifndef cimg_use_png
- cimg::unused(bits_per_value);
- if (file)
- throw CImgIOException(_cimg_instance
- "load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.",
- cimg_instance);
- else return load_other(filename);
- #else
- // Open file and check for PNG validity
- #if defined __GNUC__
- const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning
- std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb");
- #else
- const char *nfilename = filename;
- std::FILE *nfile = file?file:cimg::fopen(nfilename,"rb");
- #endif
- unsigned char pngCheck[8] = { 0 };
- cimg::fread(pngCheck,8,(std::FILE*)nfile);
- if (png_sig_cmp(pngCheck,0,8)) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_png(): Invalid PNG file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- // Setup PNG structures for read
- png_voidp user_error_ptr = 0;
- png_error_ptr user_error_fn = 0, user_warning_fn = 0;
- png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);
- if (!png_ptr) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_png(): Failed to initialize 'png_ptr' structure for file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_infop info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- if (!file) cimg::fclose(nfile);
- png_destroy_read_struct(&png_ptr,(png_infopp)0,(png_infopp)0);
- throw CImgIOException(_cimg_instance
- "load_png(): Failed to initialize 'info_ptr' structure for file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_infop end_info = png_create_info_struct(png_ptr);
- if (!end_info) {
- if (!file) cimg::fclose(nfile);
- png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)0);
- throw CImgIOException(_cimg_instance
- "load_png(): Failed to initialize 'end_info' structure for file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- // Error handling callback for png file reading
- if (setjmp(png_jmpbuf(png_ptr))) {
- if (!file) cimg::fclose((std::FILE*)nfile);
- png_destroy_read_struct(&png_ptr, &end_info, (png_infopp)0);
- throw CImgIOException(_cimg_instance
- "load_png(): Encountered unknown fatal error in libpng for file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_init_io(png_ptr, nfile);
- png_set_sig_bytes(png_ptr, 8);
- // Get PNG Header Info up to data block
- png_read_info(png_ptr,info_ptr);
- png_uint_32 W, H;
- int bit_depth, color_type, interlace_type;
- bool is_gray = false;
- png_get_IHDR(png_ptr,info_ptr,&W,&H,&bit_depth,&color_type,&interlace_type,(int*)0,(int*)0);
- if (bits_per_value) *bits_per_value = (unsigned int)bit_depth;
- // Transforms to unify image data
- if (color_type==PNG_COLOR_TYPE_PALETTE) {
- png_set_palette_to_rgb(png_ptr);
- color_type = PNG_COLOR_TYPE_RGB;
- bit_depth = 8;
- }
- if (color_type==PNG_COLOR_TYPE_GRAY && bit_depth<8) {
- png_set_expand_gray_1_2_4_to_8(png_ptr);
- is_gray = true;
- bit_depth = 8;
- }
- if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) {
- png_set_tRNS_to_alpha(png_ptr);
- color_type |= PNG_COLOR_MASK_ALPHA;
- }
- if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) {
- png_set_gray_to_rgb(png_ptr);
- color_type |= PNG_COLOR_MASK_COLOR;
- is_gray = true;
- }
- if (color_type==PNG_COLOR_TYPE_RGB)
- png_set_filler(png_ptr,0xffffU,PNG_FILLER_AFTER);
- png_read_update_info(png_ptr,info_ptr);
- if (bit_depth!=8 && bit_depth!=16) {
- if (!file) cimg::fclose(nfile);
- png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);
- throw CImgIOException(_cimg_instance
- "load_png(): Invalid bit depth %u in file '%s'.",
- cimg_instance,
- bit_depth,nfilename?nfilename:"(FILE*)");
- }
- const int byte_depth = bit_depth>>3;
- // Allocate memory for image reading
- png_bytep *const imgData = new png_bytep[H];
- for (unsigned int row = 0; row<H; ++row) imgData[row] = new png_byte[(size_t)byte_depth*4*W];
- png_read_image(png_ptr,imgData);
- png_read_end(png_ptr,end_info);
- // Read pixel data
- if (color_type!=PNG_COLOR_TYPE_RGB && color_type!=PNG_COLOR_TYPE_RGB_ALPHA) {
- if (!file) cimg::fclose(nfile);
- png_destroy_read_struct(&png_ptr,&end_info,(png_infopp)0);
- throw CImgIOException(_cimg_instance
- "load_png(): Invalid color coding type %u in file '%s'.",
- cimg_instance,
- color_type,nfilename?nfilename:"(FILE*)");
- }
- const bool is_alpha = (color_type==PNG_COLOR_TYPE_RGBA);
- try { assign(W,H,1,(is_gray?1:3) + (is_alpha?1:0)); }
- catch (...) { if (!file) cimg::fclose(nfile); throw; }
- T
- *ptr_r = data(0,0,0,0),
- *ptr_g = is_gray?0:data(0,0,0,1),
- *ptr_b = is_gray?0:data(0,0,0,2),
- *ptr_a = !is_alpha?0:data(0,0,0,is_gray?1:3);
- switch (bit_depth) {
- case 8 : {
- cimg_forY(*this,y) {
- const unsigned char *ptrs = (unsigned char*)imgData[y];
- cimg_forX(*this,x) {
- *(ptr_r++) = (T)*(ptrs++);
- if (ptr_g) *(ptr_g++) = (T)*(ptrs++); else ++ptrs;
- if (ptr_b) *(ptr_b++) = (T)*(ptrs++); else ++ptrs;
- if (ptr_a) *(ptr_a++) = (T)*(ptrs++); else ++ptrs;
- }
- }
- } break;
- case 16 : {
- cimg_forY(*this,y) {
- const unsigned short *ptrs = (unsigned short*)(imgData[y]);
- if (!cimg::endianness()) cimg::invert_endianness(ptrs,4*_width);
- cimg_forX(*this,x) {
- *(ptr_r++) = (T)*(ptrs++);
- if (ptr_g) *(ptr_g++) = (T)*(ptrs++); else ++ptrs;
- if (ptr_b) *(ptr_b++) = (T)*(ptrs++); else ++ptrs;
- if (ptr_a) *(ptr_a++) = (T)*(ptrs++); else ++ptrs;
- }
- }
- } break;
- }
- png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
- // Deallocate image read memory
- cimg_forY(*this,n) delete[] imgData[n];
- delete[] imgData;
- if (!file) cimg::fclose(nfile);
- return *this;
- #endif
- }
- //! Load image from a PNM file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_pnm(const char *const filename) {
- return _load_pnm(0,filename);
- }
- //! Load image from a PNM file \newinstance.
- static CImg<T> get_load_pnm(const char *const filename) {
- return CImg<T>().load_pnm(filename);
- }
- //! Load image from a PNM file \overloading.
- CImg<T>& load_pnm(std::FILE *const file) {
- return _load_pnm(file,0);
- }
- //! Load image from a PNM file \newinstance.
- static CImg<T> get_load_pnm(std::FILE *const file) {
- return CImg<T>().load_pnm(file);
- }
- CImg<T>& _load_pnm(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_pnm(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- unsigned int ppm_type, W, H, D = 1, colormax = 255;
- CImg<charT> item(16384,1,1,1,0);
- int err, rval, gval, bval;
- const longT cimg_iobuffer = (longT)24*1024*1024;
- while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if (cimg_sscanf(item," P%u",&ppm_type)!=1) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pnm(): PNM header not found in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if ((err=cimg_sscanf(item," %u %u %u %u",&W,&H,&D,&colormax))<2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pnm(): WIDTH and HEIGHT fields undefined in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- if (ppm_type!=1 && ppm_type!=4) {
- if (err==2 || (err==3 && (ppm_type==5 || ppm_type==7 || ppm_type==8 || ppm_type==9))) {
- while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if (cimg_sscanf(item,"%u",&colormax)!=1)
- cimg::warn(_cimg_instance
- "load_pnm(): COLORMAX field is undefined in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- } else { colormax = D; D = 1; }
- }
- std::fgetc(nfile);
- if (filename) { // Check that dimensions specified in file does not exceed the buffer dimension
- const cimg_int64 siz = cimg::fsize(filename);
- if (W*H*D>siz)
- throw CImgIOException(_cimg_instance
- "load_pnm(): Specified image dimensions in file '%s' exceed file size.",
- cimg_instance,
- filename);
- }
- switch (ppm_type) {
- case 1 : { // 2D B&W ascii
- assign(W,H,1,1);
- T* ptrd = _data;
- cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)(rval?0:255); else break; }
- } break;
- case 2 : { // 2D grey ascii
- assign(W,H,1,1);
- T* ptrd = _data;
- cimg_foroff(*this,off) { if (std::fscanf(nfile,"%d",&rval)>0) *(ptrd++) = (T)rval; else break; }
- } break;
- case 3 : { // 2D color ascii
- assign(W,H,1,3);
- T *ptrd = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
- cimg_forXY(*this,x,y) {
- if (std::fscanf(nfile,"%d %d %d",&rval,&gval,&bval)==3) {
- *(ptrd++) = (T)rval; *(ptr_g++) = (T)gval; *(ptr_b++) = (T)bval;
- } else break;
- }
- } break;
- case 4 : { // 2D b&w binary (support 3D PINK extension)
- CImg<ucharT> raw;
- assign(W,H,D,1);
- T *ptrd = data(0,0,0,0);
- unsigned int w = 0, h = 0, d = 0;
- for (longT to_read = (longT)((W/8 + (W%8?1:0))*H*D); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const unsigned char *ptrs = raw._data;
- unsigned char mask = 0, val = 0;
- for (ulongT off = (ulongT)raw._width; off || mask; mask>>=1) {
- if (!mask) { if (off--) val = *(ptrs++); mask = 128; }
- *(ptrd++) = (T)((val&mask)?0:255);
- if (++w==W) { w = 0; mask = 0; if (++h==H) { h = 0; if (++d==D) break; }}
- }
- }
- } break;
- case 5 : case 7 : { // 2D/3D grey binary (support 3D PINK extension)
- if (colormax<256) { // 8 bits
- CImg<ucharT> raw;
- assign(W,H,D,1);
- T *ptrd = data(0,0,0,0);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const unsigned char *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
- }
- } else { // 16 bits
- CImg<ushortT> raw;
- assign(W,H,D,1);
- T *ptrd = data(0,0,0,0);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer/2));
- cimg::fread(raw._data,raw._width,nfile);
- if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width);
- to_read-=raw._width;
- const unsigned short *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
- }
- }
- } break;
- case 6 : { // 2D color binary
- if (colormax<256) { // 8 bits
- CImg<ucharT> raw;
- assign(W,H,1,3);
- T
- *ptr_r = data(0,0,0,0),
- *ptr_g = data(0,0,0,1),
- *ptr_b = data(0,0,0,2);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const unsigned char *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width/3; off; --off) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- }
- } else { // 16 bits
- CImg<ushortT> raw;
- assign(W,H,1,3);
- T
- *ptr_r = data(0,0,0,0),
- *ptr_g = data(0,0,0,1),
- *ptr_b = data(0,0,0,2);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer/2));
- cimg::fread(raw._data,raw._width,nfile);
- if (!cimg::endianness()) cimg::invert_endianness(raw._data,raw._width);
- to_read-=raw._width;
- const unsigned short *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width/3; off; --off) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- }
- }
- } break;
- case 8 : { // 2D/3D grey binary with int32 integers (PINK extension)
- CImg<intT> raw;
- assign(W,H,D,1);
- T *ptrd = data(0,0,0,0);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const int *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
- }
- } break;
- case 9 : { // 2D/3D grey binary with float values (PINK extension)
- CImg<floatT> raw;
- assign(W,H,D,1);
- T *ptrd = data(0,0,0,0);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const float *ptrs = raw._data;
- for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++);
- }
- } break;
- default :
- assign();
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pnm(): PNM type 'P%d' found, but type is not supported.",
- cimg_instance,
- filename?filename:"(FILE*)",ppm_type);
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a PFM file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_pfm(const char *const filename) {
- return _load_pfm(0,filename);
- }
- //! Load image from a PFM file \newinstance.
- static CImg<T> get_load_pfm(const char *const filename) {
- return CImg<T>().load_pfm(filename);
- }
- //! Load image from a PFM file \overloading.
- CImg<T>& load_pfm(std::FILE *const file) {
- return _load_pfm(file,0);
- }
- //! Load image from a PFM file \newinstance.
- static CImg<T> get_load_pfm(std::FILE *const file) {
- return CImg<T>().load_pfm(file);
- }
- CImg<T>& _load_pfm(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_pfm(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- char pfm_type;
- CImg<charT> item(16384,1,1,1,0);
- int W = 0, H = 0, err = 0;
- double scale = 0;
- while ((err=std::fscanf(nfile,"%16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if (cimg_sscanf(item," P%c",&pfm_type)!=1) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pfm(): PFM header not found in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if ((err=cimg_sscanf(item," %d %d",&W,&H))<2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- } else if (W<=0 || H<=0) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.",
- cimg_instance,W,H,
- filename?filename:"(FILE*)");
- }
- if (err==2) {
- while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);
- if (cimg_sscanf(item,"%lf",&scale)!=1)
- cimg::warn(_cimg_instance
- "load_pfm(): SCALE field is undefined in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- std::fgetc(nfile);
- const bool is_color = (pfm_type=='F'), is_inverted = (scale>0)!=cimg::endianness();
- if (is_color) {
- assign(W,H,1,3,(T)0);
- CImg<floatT> buf(3*W);
- T *ptr_r = data(0,0,0,0), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2);
- cimg_forY(*this,y) {
- cimg::fread(buf._data,3*W,nfile);
- if (is_inverted) cimg::invert_endianness(buf._data,3*W);
- const float *ptrs = buf._data;
- cimg_forX(*this,x) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- }
- } else {
- assign(W,H,1,1,(T)0);
- CImg<floatT> buf(W);
- T *ptrd = data(0,0,0,0);
- cimg_forY(*this,y) {
- cimg::fread(buf._data,W,nfile);
- if (is_inverted) cimg::invert_endianness(buf._data,W);
- const float *ptrs = buf._data;
- cimg_forX(*this,x) *(ptrd++) = (T)*(ptrs++);
- }
- }
- if (!file) cimg::fclose(nfile);
- return mirror('y'); // Most of the .pfm files are flipped along the y-axis
- }
- //! Load image from a RGB file.
- /**
- \param filename Filename, as a C-string.
- \param dimw Width of the image buffer.
- \param dimh Height of the image buffer.
- **/
- CImg<T>& load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
- return _load_rgb(0,filename,dimw,dimh);
- }
- //! Load image from a RGB file \newinstance.
- static CImg<T> get_load_rgb(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
- return CImg<T>().load_rgb(filename,dimw,dimh);
- }
- //! Load image from a RGB file \overloading.
- CImg<T>& load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
- return _load_rgb(file,0,dimw,dimh);
- }
- //! Load image from a RGB file \newinstance.
- static CImg<T> get_load_rgb(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
- return CImg<T>().load_rgb(file,dimw,dimh);
- }
- CImg<T>& _load_rgb(std::FILE *const file, const char *const filename,
- const unsigned int dimw, const unsigned int dimh) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_rgb(): Specified filename is (null).",
- cimg_instance);
- if (!dimw || !dimh) return assign();
- const longT cimg_iobuffer = (longT)24*1024*1024;
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- CImg<ucharT> raw;
- assign(dimw,dimh,1,3);
- T
- *ptr_r = data(0,0,0,0),
- *ptr_g = data(0,0,0,1),
- *ptr_b = data(0,0,0,2);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const unsigned char *ptrs = raw._data;
- for (ulongT off = raw._width/3UL; off; --off) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a RGBA file.
- /**
- \param filename Filename, as a C-string.
- \param dimw Width of the image buffer.
- \param dimh Height of the image buffer.
- **/
- CImg<T>& load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
- return _load_rgba(0,filename,dimw,dimh);
- }
- //! Load image from a RGBA file \newinstance.
- static CImg<T> get_load_rgba(const char *const filename, const unsigned int dimw, const unsigned int dimh=1) {
- return CImg<T>().load_rgba(filename,dimw,dimh);
- }
- //! Load image from a RGBA file \overloading.
- CImg<T>& load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
- return _load_rgba(file,0,dimw,dimh);
- }
- //! Load image from a RGBA file \newinstance.
- static CImg<T> get_load_rgba(std::FILE *const file, const unsigned int dimw, const unsigned int dimh=1) {
- return CImg<T>().load_rgba(file,dimw,dimh);
- }
- CImg<T>& _load_rgba(std::FILE *const file, const char *const filename,
- const unsigned int dimw, const unsigned int dimh) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_rgba(): Specified filename is (null).",
- cimg_instance);
- if (!dimw || !dimh) return assign();
- const longT cimg_iobuffer = (longT)24*1024*1024;
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- CImg<ucharT> raw;
- assign(dimw,dimh,1,4);
- 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);
- for (longT to_read = (longT)size(); to_read>0; ) {
- raw.assign(std::min(to_read,cimg_iobuffer));
- cimg::fread(raw._data,raw._width,nfile);
- to_read-=raw._width;
- const unsigned char *ptrs = raw._data;
- for (ulongT off = raw._width/4UL; off; --off) {
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- *(ptr_a++) = (T)*(ptrs++);
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a TIFF file.
- /**
- \param filename Filename, as a C-string.
- \param first_frame First frame to read (for multi-pages tiff).
- \param last_frame Last frame to read (for multi-pages tiff).
- \param step_frame Step value of frame reading.
- \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
- \param[out] voxel_size Voxel size, as stored in the filename.
- \param[out] description Description, as stored in the filename.
- \note
- - libtiff support is enabled by defining the precompilation
- directive \c cimg_use_tif.
- - When libtiff is enabled, 2D and 3D (multipage) several
- channel per pixel are supported for
- <tt>char,uchar,short,ushort,float</tt> and \c double pixel types.
- - If \c cimg_use_tiff is not defined at compile time the
- function uses CImg<T>& load_other(const char*).
- **/
- CImg<T>& load_tiff(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
- float *const voxel_size=0, CImg<charT> *const description=0) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_tiff(): Specified filename is (null).",
- cimg_instance);
- const unsigned int
- nfirst_frame = first_frame<last_frame?first_frame:last_frame,
- nstep_frame = step_frame?step_frame:1;
- unsigned int nlast_frame = first_frame<last_frame?last_frame:first_frame;
- #ifndef cimg_use_tiff
- cimg::unused(bits_per_value,voxel_size,description);
- if (nfirst_frame || nlast_frame!=~0U || nstep_frame>1)
- throw CImgArgumentException(_cimg_instance
- "load_tiff(): Unable to read sub-images from file '%s' unless libtiff is enabled.",
- cimg_instance,
- filename);
- return load_other(filename);
- #else
- #if cimg_verbosity<3
- TIFFSetWarningHandler(0);
- TIFFSetErrorHandler(0);
- #endif
- TIFF *tif = TIFFOpen(filename,"r");
- if (tif) {
- unsigned int nb_images = 0;
- do ++nb_images; while (TIFFReadDirectory(tif));
- if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))
- cimg::warn(_cimg_instance
- "load_tiff(): File '%s' contains %u image(s) while specified frame range is [%u,%u] (step %u).",
- cimg_instance,
- filename,nb_images,nfirst_frame,nlast_frame,nstep_frame);
- if (nfirst_frame>=nb_images) return assign();
- if (nlast_frame>=nb_images) nlast_frame = nb_images - 1;
- TIFFSetDirectory(tif,0);
- CImg<T> frame;
- for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) {
- frame._load_tiff(tif,l,bits_per_value,voxel_size,description);
- if (l==nfirst_frame)
- assign(frame._width,frame._height,1 + (nlast_frame - nfirst_frame)/nstep_frame,frame._spectrum);
- if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum)
- resize(std::max(frame._width,_width),
- std::max(frame._height,_height),-100,
- std::max(frame._spectrum,_spectrum),0);
- draw_image(0,0,(l - nfirst_frame)/nstep_frame,frame);
- }
- TIFFClose(tif);
- } else throw CImgIOException(_cimg_instance
- "load_tiff(): Failed to open file '%s'.",
- cimg_instance,
- filename);
- return *this;
- #endif
- }
- //! Load image from a TIFF file \newinstance.
- static CImg<T> get_load_tiff(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
- float *const voxel_size=0, CImg<charT> *const description=0) {
- return CImg<T>().load_tiff(filename,first_frame,last_frame,step_frame,bits_per_value,voxel_size,description);
- }
- // (Original contribution by Jerome Boulanger).
- #ifdef cimg_use_tiff
- template<typename t>
- void _load_tiff_tiled_contig(TIFF *const tif, const uint16_t samplesperpixel,
- const uint32_t nx, const uint32_t ny, const uint32_t tw, const uint32_t th) {
- t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif));
- if (buf) {
- for (unsigned int row = 0; row<ny; row+=th)
- for (unsigned int col = 0; col<nx; col+=tw) {
- if (TIFFReadTile(tif,buf,col,row,0,0)<0) {
- _TIFFfree(buf); TIFFClose(tif);
- throw CImgIOException(_cimg_instance
- "load_tiff(): Invalid tile in file '%s'.",
- cimg_instance,
- TIFFFileName(tif));
- }
- const t *ptr = buf;
- for (unsigned int rr = row; rr<std::min((unsigned int)(row + th),(unsigned int)ny); ++rr)
- for (unsigned int cc = col; cc<std::min((unsigned int)(col + tw),(unsigned int)nx); ++cc)
- for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
- (*this)(cc,rr,vv) = (T)(ptr[(rr - row)*th*samplesperpixel + (cc - col)*samplesperpixel + vv]);
- }
- _TIFFfree(buf);
- }
- }
- template<typename t>
- void _load_tiff_tiled_separate(TIFF *const tif, const uint16_t samplesperpixel,
- const uint32_t nx, const uint32_t ny, const uint32_t tw, const uint32_t th) {
- t *const buf = (t*)_TIFFmalloc(TIFFTileSize(tif));
- if (buf) {
- for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
- for (unsigned int row = 0; row<ny; row+=th)
- for (unsigned int col = 0; col<nx; col+=tw) {
- if (TIFFReadTile(tif,buf,col,row,0,vv)<0) {
- _TIFFfree(buf); TIFFClose(tif);
- throw CImgIOException(_cimg_instance
- "load_tiff(): Invalid tile in file '%s'.",
- cimg_instance,
- TIFFFileName(tif));
- }
- const t *ptr = buf;
- for (unsigned int rr = row; rr<std::min((unsigned int)(row + th),(unsigned int)ny); ++rr)
- for (unsigned int cc = col; cc<std::min((unsigned int)(col + tw),(unsigned int)nx); ++cc)
- (*this)(cc,rr,vv) = (T)*(ptr++);
- }
- _TIFFfree(buf);
- }
- }
- template<typename t>
- void _load_tiff_contig(TIFF *const tif, const uint16_t samplesperpixel, const uint32_t nx, const uint32_t ny) {
- t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
- if (buf) {
- uint32_t row, rowsperstrip = (uint32_t)-1;
- TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip);
- for (row = 0; row<ny; row+= rowsperstrip) {
- uint32_t nrow = (row + rowsperstrip>ny?ny - row:rowsperstrip);
- tstrip_t strip = TIFFComputeStrip(tif, row, 0);
- if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) {
- _TIFFfree(buf); TIFFClose(tif);
- throw CImgIOException(_cimg_instance
- "load_tiff(): Invalid strip in file '%s'.",
- cimg_instance,
- TIFFFileName(tif));
- }
- const t *ptr = buf;
- for (unsigned int rr = 0; rr<nrow; ++rr)
- for (unsigned int cc = 0; cc<nx; ++cc)
- for (unsigned int vv = 0; vv<samplesperpixel; ++vv) (*this)(cc,row + rr,vv) = (T)*(ptr++);
- }
- _TIFFfree(buf);
- }
- }
- template<typename t>
- void _load_tiff_separate(TIFF *const tif, const uint16_t samplesperpixel, const uint32_t nx, const uint32_t ny) {
- t *buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
- if (buf) {
- uint32_t row, rowsperstrip = (uint32_t)-1;
- TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rowsperstrip);
- for (unsigned int vv = 0; vv<samplesperpixel; ++vv)
- for (row = 0; row<ny; row+= rowsperstrip) {
- uint32_t nrow = (row + rowsperstrip>ny?ny - row:rowsperstrip);
- tstrip_t strip = TIFFComputeStrip(tif, row, vv);
- if ((TIFFReadEncodedStrip(tif,strip,buf,-1))<0) {
- _TIFFfree(buf); TIFFClose(tif);
- throw CImgIOException(_cimg_instance
- "load_tiff(): Invalid strip in file '%s'.",
- cimg_instance,
- TIFFFileName(tif));
- }
- const t *ptr = buf;
- for (unsigned int rr = 0;rr<nrow; ++rr)
- for (unsigned int cc = 0; cc<nx; ++cc)
- (*this)(cc,row + rr,vv) = (T)*(ptr++);
- }
- _TIFFfree(buf);
- }
- }
- CImg<T>& _load_tiff(TIFF *const tif, const unsigned int directory, unsigned int *const bits_per_value,
- float *const voxel_size, CImg<charT> *const description) {
- if (!TIFFSetDirectory(tif,directory)) return assign();
- uint16_t samplesperpixel = 1, bitspersample = 8, photo = 0;
- uint16_t sampleformat = 1;
- uint32_t nx = 1, ny = 1;
- const char *const filename = TIFFFileName(tif);
- const bool is_spp = (bool)TIFFGetField(tif,TIFFTAG_SAMPLESPERPIXEL,&samplesperpixel);
- TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&nx);
- TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&ny);
- TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sampleformat);
- TIFFGetFieldDefaulted(tif,TIFFTAG_BITSPERSAMPLE,&bitspersample);
- TIFFGetField(tif,TIFFTAG_PHOTOMETRIC,&photo);
- if (bits_per_value) *bits_per_value = (unsigned int)bitspersample;
- if (voxel_size) {
- const char *s_description = 0;
- float vx = 0, vy = 0, vz = 0;
- if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description) {
- const char *s_desc = std::strstr(s_description,"VX=");
- if (s_desc && cimg_sscanf(s_desc,"VX=%f VY=%f VZ=%f",&vx,&vy,&vz)==3) { // CImg format
- voxel_size[0] = vx; voxel_size[1] = vy; voxel_size[2] = vz;
- }
- s_desc = std::strstr(s_description,"spacing=");
- if (s_desc && cimg_sscanf(s_desc,"spacing=%f",&vz)==1) { // Fiji format
- voxel_size[2] = vz;
- }
- }
- TIFFGetField(tif,TIFFTAG_XRESOLUTION,voxel_size);
- TIFFGetField(tif,TIFFTAG_YRESOLUTION,voxel_size + 1);
- voxel_size[0] = 1.f/voxel_size[0];
- voxel_size[1] = 1.f/voxel_size[1];
- }
- if (description) {
- const char *s_description = 0;
- if (TIFFGetField(tif,TIFFTAG_IMAGEDESCRIPTION,&s_description) && s_description)
- CImg<charT>::string(s_description).move_to(*description);
- }
- const unsigned int spectrum = !is_spp || photo>=3?(photo>1?3:1):samplesperpixel;
- assign(nx,ny,1,spectrum);
- if ((photo>=3 && sampleformat==1 &&
- (bitspersample==4 || bitspersample==8) &&
- (samplesperpixel==1 || samplesperpixel==3 || samplesperpixel==4)) ||
- (bitspersample==1 && samplesperpixel==1)) {
- // Special case for unsigned color images.
- uint32_t *const raster = (uint32_t*)_TIFFmalloc(nx*ny*sizeof(uint32_t));
- if (!raster) {
- _TIFFfree(raster); TIFFClose(tif);
- throw CImgException(_cimg_instance
- "load_tiff(): Failed to allocate memory (%s) for file '%s'.",
- cimg_instance,
- cimg::strbuffersize(nx*ny*sizeof(uint32_t)),filename);
- }
- TIFFReadRGBAImage(tif,nx,ny,raster,0);
- switch (spectrum) {
- case 1 :
- cimg_forXY(*this,x,y)
- (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 -y) + x]);
- break;
- case 3 :
- cimg_forXY(*this,x,y) {
- (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 - y) + x]);
- (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny - 1 - y) + x]);
- (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny - 1 - y) + x]);
- }
- break;
- case 4 :
- cimg_forXY(*this,x,y) {
- (*this)(x,y,0) = (T)(float)TIFFGetR(raster[nx*(ny - 1 - y) + x]);
- (*this)(x,y,1) = (T)(float)TIFFGetG(raster[nx*(ny - 1 - y) + x]);
- (*this)(x,y,2) = (T)(float)TIFFGetB(raster[nx*(ny - 1 - y) + x]);
- (*this)(x,y,3) = (T)(float)TIFFGetA(raster[nx*(ny - 1 - y) + x]);
- }
- break;
- }
- _TIFFfree(raster);
- } else { // Other cases
- uint16_t config;
- TIFFGetField(tif,TIFFTAG_PLANARCONFIG,&config);
- if (TIFFIsTiled(tif)) {
- uint32_t tw = 1, th = 1;
- TIFFGetField(tif,TIFFTAG_TILEWIDTH,&tw);
- TIFFGetField(tif,TIFFTAG_TILELENGTH,&th);
- if (config==PLANARCONFIG_CONTIG) switch (bitspersample) {
- case 8 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_contig<unsigned char>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_contig<signed char>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 16 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_contig<unsigned short>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_contig<short>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 32 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_contig<unsigned int>(tif,samplesperpixel,nx,ny,tw,th);
- else if (sampleformat==SAMPLEFORMAT_INT)
- _load_tiff_tiled_contig<int>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_contig<float>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 64 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_contig<uint64T>(tif,samplesperpixel,nx,ny,tw,th);
- else if (sampleformat==SAMPLEFORMAT_INT)
- _load_tiff_tiled_contig<int64T>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_contig<double>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- } else switch (bitspersample) {
- case 8 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_separate<unsigned char>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_separate<signed char>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 16 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_separate<unsigned short>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_separate<short>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 32 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_separate<unsigned int>(tif,samplesperpixel,nx,ny,tw,th);
- else if (sampleformat==SAMPLEFORMAT_INT)
- _load_tiff_tiled_separate<int>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_separate<float>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- case 64 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_tiled_separate<uint64T>(tif,samplesperpixel,nx,ny,tw,th);
- else if (sampleformat==SAMPLEFORMAT_INT)
- _load_tiff_tiled_separate<int64T>(tif,samplesperpixel,nx,ny,tw,th);
- else _load_tiff_tiled_separate<double>(tif,samplesperpixel,nx,ny,tw,th);
- break;
- }
- } else {
- if (config==PLANARCONFIG_CONTIG) switch (bitspersample) {
- case 8 :
- if (sampleformat==SAMPLEFORMAT_UINT)
- _load_tiff_contig<unsigned char>(tif,samplesperpixel,nx,ny);
- else _load_tiff_contig<signed char>(tif,samplesperpixel,nx,ny);
- break;
- case 16 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<unsigned short>(tif,samplesperpixel,nx,ny);
- else _load_tiff_contig<short>(tif,samplesperpixel,nx,ny);
- break;
- case 32 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<unsigned int>(tif,samplesperpixel,nx,ny);
- else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig<int>(tif,samplesperpixel,nx,ny);
- else _load_tiff_contig<float>(tif,samplesperpixel,nx,ny);
- break;
- case 64 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_contig<uint64T>(tif,samplesperpixel,nx,ny);
- else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_contig<int64T>(tif,samplesperpixel,nx,ny);
- else _load_tiff_contig<double>(tif,samplesperpixel,nx,ny);
- break;
- } else switch (bitspersample) {
- case 8 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned char>(tif,samplesperpixel,nx,ny);
- else _load_tiff_separate<signed char>(tif,samplesperpixel,nx,ny);
- break;
- case 16 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned short>(tif,samplesperpixel,nx,ny);
- else _load_tiff_separate<short>(tif,samplesperpixel,nx,ny);
- break;
- case 32 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<unsigned int>(tif,samplesperpixel,nx,ny);
- else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate<int>(tif,samplesperpixel,nx,ny);
- else _load_tiff_separate<float>(tif,samplesperpixel,nx,ny);
- break;
- case 64 :
- if (sampleformat==SAMPLEFORMAT_UINT) _load_tiff_separate<uint64T>(tif,samplesperpixel,nx,ny);
- else if (sampleformat==SAMPLEFORMAT_INT) _load_tiff_separate<int64T>(tif,samplesperpixel,nx,ny);
- else _load_tiff_separate<double>(tif,samplesperpixel,nx,ny);
- break;
- }
- }
- }
- return *this;
- }
- #endif
- //! Load image from a MINC2 file.
- /**
- \param filename Filename, as a C-string.
- **/
- // (Original code by Haz-Edine Assemlal).
- CImg<T>& load_minc2(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_minc2(): Specified filename is (null).",
- cimg_instance);
- #ifndef cimg_use_minc2
- return load_other(filename);
- #else
- minc::minc_1_reader rdr;
- rdr.open(filename);
- assign(rdr.ndim(1)?rdr.ndim(1):1,
- rdr.ndim(2)?rdr.ndim(2):1,
- rdr.ndim(3)?rdr.ndim(3):1,
- rdr.ndim(4)?rdr.ndim(4):1);
- if (pixel_type()==cimg::type<unsigned char>::string())
- rdr.setup_read_byte();
- else if (pixel_type()==cimg::type<int>::string())
- rdr.setup_read_int();
- else if (pixel_type()==cimg::type<double>::string())
- rdr.setup_read_double();
- else
- rdr.setup_read_float();
- minc::load_standard_volume(rdr,this->_data);
- return *this;
- #endif
- }
- //! Load image from a MINC2 file \newinstance.
- static CImg<T> get_load_minc2(const char *const filename) {
- return CImg<T>().load_analyze(filename);
- }
- //! Load image from an ANALYZE7.5/NIFTI file.
- /**
- \param filename Filename, as a C-string.
- \param[out] voxel_size Pointer to the three voxel sizes read from the file.
- **/
- CImg<T>& load_analyze(const char *const filename, float *const voxel_size=0) {
- return _load_analyze(0,filename,voxel_size);
- }
- //! Load image from an ANALYZE7.5/NIFTI file \newinstance.
- static CImg<T> get_load_analyze(const char *const filename, float *const voxel_size=0) {
- return CImg<T>().load_analyze(filename,voxel_size);
- }
- //! Load image from an ANALYZE7.5/NIFTI file \overloading.
- CImg<T>& load_analyze(std::FILE *const file, float *const voxel_size=0) {
- return _load_analyze(file,0,voxel_size);
- }
- //! Load image from an ANALYZE7.5/NIFTI file \newinstance.
- static CImg<T> get_load_analyze(std::FILE *const file, float *const voxel_size=0) {
- return CImg<T>().load_analyze(file,voxel_size);
- }
- CImg<T>& _load_analyze(std::FILE *const file, const char *const filename, float *const voxel_size=0) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_analyze(): Specified filename is (null).",
- cimg_instance);
- std::FILE *nfile_header = 0, *nfile = 0;
- if (!file) {
- CImg<charT> body(1024);
- const char *const ext = cimg::split_filename(filename,body);
- if (!cimg::strcasecmp(ext,"hdr")) { // File is an Analyze header file
- nfile_header = cimg::fopen(filename,"rb");
- cimg_sprintf(body._data + std::strlen(body),".img");
- nfile = cimg::fopen(body,"rb");
- } else if (!cimg::strcasecmp(ext,"img")) { // File is an Analyze data file
- nfile = cimg::fopen(filename,"rb");
- cimg_sprintf(body._data + std::strlen(body),".hdr");
- nfile_header = cimg::fopen(body,"rb");
- } else nfile_header = nfile = cimg::fopen(filename,"rb"); // File is a Niftii file
- } else nfile_header = nfile = file; // File is a Niftii file
- if (!nfile || !nfile_header)
- throw CImgIOException(_cimg_instance
- "load_analyze(): Invalid Analyze7.5 or NIFTI header in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- // Read header.
- bool endian = false;
- unsigned int header_size;
- cimg::fread(&header_size,1,nfile_header);
- if (!header_size)
- throw CImgIOException(_cimg_instance
- "load_analyze(): Invalid zero-size header in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (header_size>=4096) { endian = true; cimg::invert_endianness(header_size); }
- unsigned char *const header = new unsigned char[header_size];
- cimg::fread(header + 4,header_size - 4,nfile_header);
- if (!file && nfile_header!=nfile) cimg::fclose(nfile_header);
- if (endian) {
- cimg::invert_endianness((short*)(header + 40),5);
- cimg::invert_endianness((short*)(header + 70),1);
- cimg::invert_endianness((short*)(header + 72),1);
- cimg::invert_endianness((float*)(header + 76),4);
- cimg::invert_endianness((float*)(header + 108),1);
- cimg::invert_endianness((float*)(header + 112),1);
- }
- if (nfile_header==nfile) {
- const unsigned int vox_offset = (unsigned int)*(float*)(header + 108);
- std::fseek(nfile,vox_offset,SEEK_SET);
- }
- unsigned short *dim = (unsigned short*)(header + 40), dimx = 1, dimy = 1, dimz = 1, dimv = 1;
- if (!dim[0])
- cimg::warn(_cimg_instance
- "load_analyze(): File '%s' defines an image with zero dimensions.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (dim[0]>4)
- cimg::warn(_cimg_instance
- "load_analyze(): File '%s' defines an image with %u dimensions, reading only the 4 first.",
- cimg_instance,
- filename?filename:"(FILE*)",dim[0]);
- if (dim[0]>=1) dimx = dim[1];
- if (dim[0]>=2) dimy = dim[2];
- if (dim[0]>=3) dimz = dim[3];
- if (dim[0]>=4) dimv = dim[4];
- float scalefactor = *(float*)(header + 112); if (scalefactor==0) scalefactor = 1;
- const unsigned short datatype = *(unsigned short*)(header + 70);
- if (voxel_size) {
- const float *vsize = (float*)(header + 76);
- voxel_size[0] = vsize[1]; voxel_size[1] = vsize[2]; voxel_size[2] = vsize[3];
- }
- delete[] header;
- // Read pixel data.
- assign(dimx,dimy,dimz,dimv);
- const size_t pdim = (size_t)dimx*dimy*dimz*dimv;
- switch (datatype) {
- case 2 : {
- unsigned char *const buffer = new unsigned char[pdim];
- cimg::fread(buffer,pdim,nfile);
- cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
- delete[] buffer;
- } break;
- case 4 : {
- short *const buffer = new short[pdim];
- cimg::fread(buffer,pdim,nfile);
- if (endian) cimg::invert_endianness(buffer,pdim);
- cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
- delete[] buffer;
- } break;
- case 8 : {
- int *const buffer = new int[pdim];
- cimg::fread(buffer,pdim,nfile);
- if (endian) cimg::invert_endianness(buffer,pdim);
- cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
- delete[] buffer;
- } break;
- case 16 : {
- float *const buffer = new float[pdim];
- cimg::fread(buffer,pdim,nfile);
- if (endian) cimg::invert_endianness(buffer,pdim);
- cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
- delete[] buffer;
- } break;
- case 64 : {
- double *const buffer = new double[pdim];
- cimg::fread(buffer,pdim,nfile);
- if (endian) cimg::invert_endianness(buffer,pdim);
- cimg_foroff(*this,off) _data[off] = (T)(buffer[off]*scalefactor);
- delete[] buffer;
- } break;
- default :
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_analyze(): Unable to load datatype %d in file '%s'",
- cimg_instance,
- datatype,filename?filename:"(FILE*)");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a .cimg[z] file.
- /**
- \param filename Filename, as a C-string.
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T>& load_cimg(const char *const filename, const char axis='z', const float align=0) {
- CImgList<T> list;
- list.load_cimg(filename);
- if (list._width==1) return list[0].move_to(*this);
- return assign(list.get_append(axis,align));
- }
- //! Load image from a .cimg[z] file \newinstance
- static CImg<T> get_load_cimg(const char *const filename, const char axis='z', const float align=0) {
- return CImg<T>().load_cimg(filename,axis,align);
- }
- //! Load image from a .cimg[z] file \overloading.
- CImg<T>& load_cimg(std::FILE *const file, const char axis='z', const float align=0) {
- CImgList<T> list;
- list.load_cimg(file);
- if (list._width==1) return list[0].move_to(*this);
- return assign(list.get_append(axis,align));
- }
- //! Load image from a .cimg[z] file \newinstance
- static CImg<T> get_load_cimg(std::FILE *const file, const char axis='z', const float align=0) {
- return CImg<T>().load_cimg(file,axis,align);
- }
- //! Load sub-images of a .cimg file.
- /**
- \param filename Filename, as a C-string.
- \param n0 Starting frame.
- \param n1 Ending frame (~0U for max).
- \param x0 X-coordinate of the starting sub-image vertex.
- \param y0 Y-coordinate of the starting sub-image vertex.
- \param z0 Z-coordinate of the starting sub-image vertex.
- \param c0 C-coordinate of the starting sub-image vertex.
- \param x1 X-coordinate of the ending sub-image vertex (~0U for max).
- \param y1 Y-coordinate of the ending sub-image vertex (~0U for max).
- \param z1 Z-coordinate of the ending sub-image vertex (~0U for max).
- \param c1 C-coordinate of the ending sub-image vertex (~0U for max).
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T>& load_cimg(const char *const filename,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1,
- const char axis='z', const float align=0) {
- CImgList<T> list;
- list.load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- if (list._width==1) return list[0].move_to(*this);
- return assign(list.get_append(axis,align));
- }
- //! Load sub-images of a .cimg file \newinstance.
- static CImg<T> get_load_cimg(const char *const filename,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1,
- const char axis='z', const float align=0) {
- return CImg<T>().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align);
- }
- //! Load sub-images of a .cimg file \overloading.
- CImg<T>& load_cimg(std::FILE *const file,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1,
- const char axis='z', const float align=0) {
- CImgList<T> list;
- list.load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- if (list._width==1) return list[0].move_to(*this);
- return assign(list.get_append(axis,align));
- }
- //! Load sub-images of a .cimg file \newinstance.
- static CImg<T> get_load_cimg(std::FILE *const file,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1,
- const char axis='z', const float align=0) {
- return CImg<T>().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1,axis,align);
- }
- //! Load image from an INRIMAGE-4 file.
- /**
- \param filename Filename, as a C-string.
- \param[out] voxel_size Pointer to the three voxel sizes read from the file.
- **/
- CImg<T>& load_inr(const char *const filename, float *const voxel_size=0) {
- return _load_inr(0,filename,voxel_size);
- }
- //! Load image from an INRIMAGE-4 file \newinstance.
- static CImg<T> get_load_inr(const char *const filename, float *const voxel_size=0) {
- return CImg<T>().load_inr(filename,voxel_size);
- }
- //! Load image from an INRIMAGE-4 file \overloading.
- CImg<T>& load_inr(std::FILE *const file, float *const voxel_size=0) {
- return _load_inr(file,0,voxel_size);
- }
- //! Load image from an INRIMAGE-4 file \newinstance.
- static CImg<T> get_load_inr(std::FILE *const file, float *voxel_size=0) {
- return CImg<T>().load_inr(file,voxel_size);
- }
- static void _load_inr_header(std::FILE *file, int out[8], float *const voxel_size) {
- CImg<charT> item(1024), tmp1(64), tmp2(64);
- *item = *tmp1 = *tmp2 = 0;
- out[0] = std::fscanf(file,"%63s",item._data);
- out[0] = out[1] = out[2] = out[3] = out[5] = 1; out[4] = out[6] = out[7] = -1;
- if (cimg::strncasecmp(item,"#INRIMAGE-4#{",13)!=0)
- throw CImgIOException("CImg<%s>::load_inr(): INRIMAGE-4 header not found.",
- pixel_type());
- while (std::fscanf(file," %63[^\n]%*c",item._data)!=EOF && std::strncmp(item,"##}",3)) {
- cimg_sscanf(item," XDIM%*[^0-9]%d",out);
- cimg_sscanf(item," YDIM%*[^0-9]%d",out + 1);
- cimg_sscanf(item," ZDIM%*[^0-9]%d",out + 2);
- cimg_sscanf(item," VDIM%*[^0-9]%d",out + 3);
- cimg_sscanf(item," PIXSIZE%*[^0-9]%d",out + 6);
- if (voxel_size) {
- cimg_sscanf(item," VX%*[^0-9.+-]%f",voxel_size);
- cimg_sscanf(item," VY%*[^0-9.+-]%f",voxel_size + 1);
- cimg_sscanf(item," VZ%*[^0-9.+-]%f",voxel_size + 2);
- }
- if (cimg_sscanf(item," CPU%*[ =]%s",tmp1._data)) out[7] = cimg::strncasecmp(tmp1,"sun",3)?0:1;
- switch (cimg_sscanf(item," TYPE%*[ =]%s %s",tmp1._data,tmp2._data)) {
- case 0 : break;
- case 2 :
- out[5] = cimg::strncasecmp(tmp1,"unsigned",8)?1:0;
- std::strncpy(tmp1,tmp2,tmp1._width - 1); // fallthrough
- case 1 :
- if (!cimg::strncasecmp(tmp1,"int",3) || !cimg::strncasecmp(tmp1,"fixed",5)) out[4] = 0;
- if (!cimg::strncasecmp(tmp1,"float",5) || !cimg::strncasecmp(tmp1,"double",6)) out[4] = 1;
- if (!cimg::strncasecmp(tmp1,"packed",6)) out[4] = 2;
- if (out[4]>=0) break; // fallthrough
- default :
- throw CImgIOException("CImg<%s>::load_inr(): Invalid pixel type '%s' defined in header.",
- pixel_type(),
- tmp2._data);
- }
- }
- if (out[0]<0 || out[1]<0 || out[2]<0 || out[3]<0)
- throw CImgIOException("CImg<%s>::load_inr(): Invalid dimensions (%d,%d,%d,%d) defined in header.",
- pixel_type(),
- out[0],out[1],out[2],out[3]);
- if (out[4]<0 || out[5]<0)
- throw CImgIOException("CImg<%s>::load_inr(): Incomplete pixel type defined in header.",
- pixel_type());
- if (out[6]<0)
- throw CImgIOException("CImg<%s>::load_inr(): Incomplete PIXSIZE field defined in header.",
- pixel_type());
- if (out[7]<0)
- throw CImgIOException("CImg<%s>::load_inr(): Big/Little Endian coding type undefined in header.",
- pixel_type());
- }
- CImg<T>& _load_inr(std::FILE *const file, const char *const filename, float *const voxel_size) {
- #define _cimg_load_inr_case(Tf,sign,pixsize,Ts) \
- if (!loaded && fopt[6]==pixsize && fopt[4]==Tf && fopt[5]==sign) { \
- Ts *xval, *const val = new Ts[(size_t)fopt[0]*fopt[3]]; \
- cimg_forYZ(*this,y,z) { \
- cimg::fread(val,fopt[0]*fopt[3],nfile); \
- if (fopt[7]!=endian) cimg::invert_endianness(val,fopt[0]*fopt[3]); \
- xval = val; cimg_forX(*this,x) cimg_forC(*this,c) (*this)(x,y,z,c) = (T)*(xval++); \
- } \
- delete[] val; \
- loaded = true; \
- }
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_inr(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- int fopt[8], endian = cimg::endianness()?1:0;
- bool loaded = false;
- if (voxel_size) voxel_size[0] = voxel_size[1] = voxel_size[2] = 1;
- _load_inr_header(nfile,fopt,voxel_size);
- assign(fopt[0],fopt[1],fopt[2],fopt[3]);
- _cimg_load_inr_case(0,0,8,unsigned char);
- _cimg_load_inr_case(0,1,8,char);
- _cimg_load_inr_case(0,0,16,unsigned short);
- _cimg_load_inr_case(0,1,16,short);
- _cimg_load_inr_case(0,0,32,unsigned int);
- _cimg_load_inr_case(0,1,32,int);
- _cimg_load_inr_case(1,0,32,float);
- _cimg_load_inr_case(1,1,32,float);
- _cimg_load_inr_case(1,0,64,double);
- _cimg_load_inr_case(1,1,64,double);
- if (!loaded) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_inr(): Unknown pixel type defined in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a EXR file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_exr(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_exr(): Specified filename is (null).",
- cimg_instance);
- #if defined(cimg_use_openexr)
- Imf::RgbaInputFile file(filename);
- Imath::Box2i dw = file.dataWindow();
- const int
- inwidth = dw.max.x - dw.min.x + 1,
- inheight = dw.max.y - dw.min.y + 1;
- Imf::Array2D<Imf::Rgba> pixels;
- pixels.resizeErase(inheight,inwidth);
- file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y*inwidth, 1, inwidth);
- file.readPixels(dw.min.y, dw.max.y);
- assign(inwidth,inheight,1,4);
- 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);
- cimg_forXY(*this,x,y) {
- *(ptr_r++) = (T)pixels[y][x].r;
- *(ptr_g++) = (T)pixels[y][x].g;
- *(ptr_b++) = (T)pixels[y][x].b;
- *(ptr_a++) = (T)pixels[y][x].a;
- }
- return *this;
- #elif defined(cimg_use_tinyexr)
- float *res;
- const char *err = 0;
- int width = 0, height = 0;
- const int ret = LoadEXR(&res,&width,&height,filename,&err);
- if (ret) throw CImgIOException(_cimg_instance
- "load_exr(): Unable to load EXR file '%s'.",
- cimg_instance,filename);
- CImg<floatT>(res,4,width,height,1,true).get_permute_axes("yzcx").move_to(*this);
- std::free(res);
- return *this;
- #else
- return load_other(filename);
- #endif
- }
- //! Load image from a EXR file \newinstance.
- static CImg<T> get_load_exr(const char *const filename) {
- return CImg<T>().load_exr(filename);
- }
- //! Load image from a PANDORE-5 file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_pandore(const char *const filename) {
- return _load_pandore(0,filename);
- }
- //! Load image from a PANDORE-5 file \newinstance.
- static CImg<T> get_load_pandore(const char *const filename) {
- return CImg<T>().load_pandore(filename);
- }
- //! Load image from a PANDORE-5 file \overloading.
- CImg<T>& load_pandore(std::FILE *const file) {
- return _load_pandore(file,0);
- }
- //! Load image from a PANDORE-5 file \newinstance.
- static CImg<T> get_load_pandore(std::FILE *const file) {
- return CImg<T>().load_pandore(file);
- }
- CImg<T>& _load_pandore(std::FILE *const file, const char *const filename) {
- #define __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,ndim,stype) \
- cimg::fread(dims,nbdim,nfile); \
- if (endian) cimg::invert_endianness(dims,nbdim); \
- assign(nwidth,nheight,ndepth,ndim); \
- const size_t siz = size(); \
- stype *buffer = new stype[siz]; \
- cimg::fread(buffer,siz,nfile); \
- if (endian) cimg::invert_endianness(buffer,siz); \
- T *ptrd = _data; \
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++); \
- buffer-=siz; \
- delete[] buffer
- #define _cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1,stype2,stype3,ltype) { \
- if (sizeof(stype1)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype1); } \
- else if (sizeof(stype2)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype2); } \
- else if (sizeof(stype3)==ltype) { __cimg_load_pandore_case(nbdim,nwidth,nheight,ndepth,dim,stype3); } \
- else throw CImgIOException(_cimg_instance \
- "load_pandore(): Unknown pixel datatype in file '%s'.", \
- cimg_instance, \
- filename?filename:"(FILE*)"); }
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_pandore(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- CImg<charT> header(32);
- cimg::fread(header._data,12,nfile);
- if (cimg::strncasecmp("PANDORE",header,7)) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pandore(): PANDORE header not found in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- unsigned int imageid, dims[8] = { 0 };
- int ptbuf[4] = { 0 };
- cimg::fread(&imageid,1,nfile);
- const bool endian = imageid>255;
- if (endian) cimg::invert_endianness(imageid);
- cimg::fread(header._data,20,nfile);
- switch (imageid) {
- case 2 : _cimg_load_pandore_case(2,dims[1],1,1,1,unsigned char,unsigned char,unsigned char,1); break;
- case 3 : _cimg_load_pandore_case(2,dims[1],1,1,1,long,int,short,4); break;
- case 4 : _cimg_load_pandore_case(2,dims[1],1,1,1,double,float,float,4); break;
- case 5 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,unsigned char,unsigned char,unsigned char,1); break;
- case 6 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,long,int,short,4); break;
- case 7 : _cimg_load_pandore_case(3,dims[2],dims[1],1,1,double,float,float,4); break;
- case 8 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,unsigned char,unsigned char,unsigned char,1); break;
- case 9 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,long,int,short,4); break;
- case 10 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],1,double,float,float,4); break;
- case 11 : { // Region 1D
- cimg::fread(dims,3,nfile);
- if (endian) cimg::invert_endianness(dims,3);
- assign(dims[1],1,1,1);
- const unsigned siz = size();
- if (dims[2]<256) {
- unsigned char *buffer = new unsigned char[siz];
- cimg::fread(buffer,siz,nfile);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- if (dims[2]<65536) {
- unsigned short *buffer = new unsigned short[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- unsigned int *buffer = new unsigned int[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- }
- }
- }
- break;
- case 12 : { // Region 2D
- cimg::fread(dims,4,nfile);
- if (endian) cimg::invert_endianness(dims,4);
- assign(dims[2],dims[1],1,1);
- const size_t siz = size();
- if (dims[3]<256) {
- unsigned char *buffer = new unsigned char[siz];
- cimg::fread(buffer,siz,nfile);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- if (dims[3]<65536) {
- unsigned short *buffer = new unsigned short[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- unsigned int *buffer = new unsigned int[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- }
- }
- }
- break;
- case 13 : { // Region 3D
- cimg::fread(dims,5,nfile);
- if (endian) cimg::invert_endianness(dims,5);
- assign(dims[3],dims[2],dims[1],1);
- const size_t siz = size();
- if (dims[4]<256) {
- unsigned char *buffer = new unsigned char[siz];
- cimg::fread(buffer,siz,nfile);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- if (dims[4]<65536) {
- unsigned short *buffer = new unsigned short[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- } else {
- unsigned int *buffer = new unsigned int[siz];
- cimg::fread(buffer,siz,nfile);
- if (endian) cimg::invert_endianness(buffer,siz);
- T *ptrd = _data;
- cimg_foroff(*this,off) *(ptrd++) = (T)*(buffer++);
- buffer-=siz;
- delete[] buffer;
- }
- }
- }
- break;
- case 16 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,unsigned char,unsigned char,unsigned char,1); break;
- case 17 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,long,int,short,4); break;
- case 18 : _cimg_load_pandore_case(4,dims[2],dims[1],1,3,double,float,float,4); break;
- case 19 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,unsigned char,unsigned char,unsigned char,1); break;
- case 20 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,long,int,short,4); break;
- case 21 : _cimg_load_pandore_case(5,dims[3],dims[2],dims[1],3,double,float,float,4); break;
- case 22 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned char,unsigned char,unsigned char,1); break;
- case 23 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],long,int,short,4); break;
- case 24 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],unsigned long,unsigned int,unsigned short,4); break;
- case 25 : _cimg_load_pandore_case(2,dims[1],1,1,dims[0],double,float,float,4); break;
- case 26 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned char,unsigned char,unsigned char,1); break;
- case 27 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],long,int,short,4); break;
- case 28 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],unsigned long,unsigned int,unsigned short,4); break;
- case 29 : _cimg_load_pandore_case(3,dims[2],dims[1],1,dims[0],double,float,float,4); break;
- case 30 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned char,unsigned char,unsigned char,1);
- break;
- case 31 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],long,int,short,4); break;
- case 32 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],unsigned long,unsigned int,unsigned short,4);
- break;
- case 33 : _cimg_load_pandore_case(4,dims[3],dims[2],dims[1],dims[0],double,float,float,4); break;
- case 34 : { // Points 1D
- cimg::fread(ptbuf,1,nfile);
- if (endian) cimg::invert_endianness(ptbuf,1);
- assign(1); (*this)(0) = (T)ptbuf[0];
- } break;
- case 35 : { // Points 2D
- cimg::fread(ptbuf,2,nfile);
- if (endian) cimg::invert_endianness(ptbuf,2);
- assign(2); (*this)(0) = (T)ptbuf[1]; (*this)(1) = (T)ptbuf[0];
- } break;
- case 36 : { // Points 3D
- cimg::fread(ptbuf,3,nfile);
- if (endian) cimg::invert_endianness(ptbuf,3);
- assign(3); (*this)(0) = (T)ptbuf[2]; (*this)(1) = (T)ptbuf[1]; (*this)(2) = (T)ptbuf[0];
- } break;
- default :
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_pandore(): Unable to load data with ID_type %u in file '%s'.",
- cimg_instance,
- imageid,filename?filename:"(FILE*)");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image from a PAR-REC (Philips) file.
- /**
- \param filename Filename, as a C-string.
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T>& load_parrec(const char *const filename, const char axis='c', const float align=0) {
- CImgList<T> list;
- list.load_parrec(filename);
- if (list._width==1) return list[0].move_to(*this);
- return assign(list.get_append(axis,align));
- }
- //! Load image from a PAR-REC (Philips) file \newinstance.
- static CImg<T> get_load_parrec(const char *const filename, const char axis='c', const float align=0) {
- return CImg<T>().load_parrec(filename,axis,align);
- }
- //! Load image from a raw binary file.
- /**
- \param filename Filename, as a C-string.
- \param size_x Width of the image buffer.
- \param size_y Height of the image buffer.
- \param size_z Depth of the image buffer.
- \param size_c Spectrum of the image buffer.
- \param is_multiplexed Tells if the image values are multiplexed along the C-axis.
- \param invert_endianness Tells if the endianness of the image buffer must be inverted.
- \param offset Starting offset of the read in the specified file.
- **/
- CImg<T>& load_raw(const char *const filename,
- const unsigned int size_x=0, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1,
- const bool is_multiplexed=false, const bool invert_endianness=false,
- const ulongT offset=0) {
- return _load_raw(0,filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
- }
- //! Load image from a raw binary file \newinstance.
- static CImg<T> get_load_raw(const char *const filename,
- const unsigned int size_x=0, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1,
- const bool is_multiplexed=false, const bool invert_endianness=false,
- const ulongT offset=0) {
- return CImg<T>().load_raw(filename,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
- }
- //! Load image from a raw binary file \overloading.
- CImg<T>& load_raw(std::FILE *const file,
- const unsigned int size_x=0, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1,
- const bool is_multiplexed=false, const bool invert_endianness=false,
- const ulongT offset=0) {
- return _load_raw(file,0,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
- }
- //! Load image from a raw binary file \newinstance.
- static CImg<T> get_load_raw(std::FILE *const file,
- const unsigned int size_x=0, const unsigned int size_y=1,
- const unsigned int size_z=1, const unsigned int size_c=1,
- const bool is_multiplexed=false, const bool invert_endianness=false,
- const ulongT offset=0) {
- return CImg<T>().load_raw(file,size_x,size_y,size_z,size_c,is_multiplexed,invert_endianness,offset);
- }
- CImg<T>& _load_raw(std::FILE *const file, const char *const filename,
- const unsigned int size_x, const unsigned int size_y,
- const unsigned int size_z, const unsigned int size_c,
- const bool is_multiplexed, const bool invert_endianness,
- const ulongT offset) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_raw(): Specified filename is (null).",
- cimg_instance);
- if (cimg::is_directory(filename))
- throw CImgArgumentException(_cimg_instance
- "load_raw(): Specified filename '%s' is a directory.",
- cimg_instance,filename);
- const bool is_bool = pixel_type()==cimg::type<bool>::string();
- ulongT siz = (ulongT)size_x*size_y*size_z*size_c;
- unsigned int
- _size_x = size_x,
- _size_y = size_y,
- _size_z = size_z,
- _size_c = size_c;
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- if (!siz) { // Retrieve file size
- const longT fpos = cimg::ftell(nfile);
- if (fpos<0) throw CImgArgumentException(_cimg_instance
- "load_raw(): Cannot determine size of input file '%s'.",
- cimg_instance,filename?filename:"(FILE*)");
- cimg::fseek(nfile,0,SEEK_END);
- siz = (ulongT)cimg::ftell(nfile);
- if (!is_bool) { siz/=sizeof(T); _size_y = (unsigned int)siz; }
- else _size_y = (unsigned int)(siz*8);
- _size_x = _size_z = _size_c = 1;
- cimg::fseek(nfile,fpos,SEEK_SET);
- }
- cimg::fseek(nfile,(longT)offset,SEEK_SET);
- assign(_size_x,_size_y,_size_z,_size_c,0);
- if (is_bool) { // Boolean data (bitwise)
- unsigned char *const buf = new unsigned char[siz];
- cimg::fread(buf,siz,nfile);
- _uchar2bool(buf,siz,is_multiplexed);
- delete[] buf;
- } else { // Non-boolean data
- if (siz && (!is_multiplexed || size_c==1)) { // Non-multiplexed
- cimg::fread(_data,siz,nfile);
- if (invert_endianness) cimg::invert_endianness(_data,siz);
- } else if (siz) { // Multiplexed
- CImg<T> buf(1,1,1,_size_c);
- cimg_forXYZ(*this,x,y,z) {
- cimg::fread(buf._data,_size_c,nfile);
- if (invert_endianness) cimg::invert_endianness(buf._data,_size_c);
- set_vector_at(buf,x,y,z);
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load image sequence from a YUV file.
- /**
- \param filename Filename, as a C-string.
- \param size_x Width of the frames.
- \param size_y Height of the frames.
- \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
- \param first_frame Index of the first frame to read.
- \param last_frame Index of the last frame to read.
- \param step_frame Step value for frame reading.
- \param yuv2rgb Tells if the YUV to RGB transform must be applied.
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- **/
- CImg<T>& load_yuv(const char *const filename,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
- return get_load_yuv(filename,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this);
- }
- //! Load image sequence from a YUV file \newinstance.
- static CImg<T> get_load_yuv(const char *const filename,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
- return CImgList<T>().load_yuv(filename,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb).get_append(axis);
- }
- //! Load image sequence from a YUV file \overloading.
- CImg<T>& load_yuv(std::FILE *const file,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
- return get_load_yuv(file,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb,axis).move_to(*this);
- }
- //! Load image sequence from a YUV file \newinstance.
- static CImg<T> get_load_yuv(std::FILE *const file,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true, const char axis='z') {
- return CImgList<T>().load_yuv(file,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb).get_append(axis);
- }
- //! Load 3D object from a .OFF file.
- /**
- \param[out] primitives Primitives data of the 3D object.
- \param[out] colors Colors data of the 3D object.
- \param filename Filename, as a C-string.
- **/
- template<typename tf, typename tc>
- CImg<T>& load_off(CImgList<tf>& primitives, CImgList<tc>& colors, const char *const filename) {
- return _load_off(primitives,colors,0,filename);
- }
- //! Load 3D object from a .OFF file \newinstance.
- template<typename tf, typename tc>
- static CImg<T> get_load_off(CImgList<tf>& primitives, CImgList<tc>& colors, const char *const filename) {
- return CImg<T>().load_off(primitives,colors,filename);
- }
- //! Load 3D object from a .OFF file \overloading.
- template<typename tf, typename tc>
- CImg<T>& load_off(CImgList<tf>& primitives, CImgList<tc>& colors, std::FILE *const file) {
- return _load_off(primitives,colors,file,0);
- }
- //! Load 3D object from a .OFF file \newinstance.
- template<typename tf, typename tc>
- static CImg<T> get_load_off(CImgList<tf>& primitives, CImgList<tc>& colors, std::FILE *const file) {
- return CImg<T>().load_off(primitives,colors,file);
- }
- template<typename tf, typename tc>
- CImg<T>& _load_off(CImgList<tf>& primitives, CImgList<tc>& colors,
- std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "load_off(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"r");
- unsigned int nb_points = 0, nb_primitives = 0, nb_read = 0;
- CImg<charT> line(256); *line = 0;
- int err;
- // Skip comments, and read magic string OFF
- do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
- if (cimg::strncasecmp(line,"OFF",3) && cimg::strncasecmp(line,"COFF",4)) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_off(): OFF header not found in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
- if ((err = cimg_sscanf(line,"%u%u%*[^\n] ",&nb_points,&nb_primitives))!=2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_off(): Invalid number of vertices or primitives specified in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- // Read points data
- assign(nb_points,3);
- float X = 0, Y = 0, Z = 0;
- cimg_forX(*this,l) {
- do { err = std::fscanf(nfile,"%255[^\n] ",line._data); } while (!err || (err==1 && *line=='#'));
- if ((err = cimg_sscanf(line,"%f%f%f%*[^\n] ",&X,&Y,&Z))!=3) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "load_off(): Failed to read vertex %u/%u in file '%s'.",
- cimg_instance,
- l + 1,nb_points,filename?filename:"(FILE*)");
- }
- (*this)(l,0) = (T)X; (*this)(l,1) = (T)Y; (*this)(l,2) = (T)Z;
- }
- // Read primitive data
- primitives.assign();
- colors.assign();
- bool stop_flag = false;
- while (!stop_flag) {
- float c0 = 0.7f, c1 = 0.7f, c2 = 0.7f;
- unsigned int prim = 0, i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0;
- *line = 0;
- if ((err = std::fscanf(nfile,"%u",&prim))!=1) stop_flag = true;
- else {
- ++nb_read;
- switch (prim) {
- case 1 : {
- if ((err = std::fscanf(nfile,"%u%255[^\n] ",&i0,line._data))<2) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0).move_to(primitives);
- CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
- }
- } break;
- case 2 : {
- if ((err = std::fscanf(nfile,"%u%u%255[^\n] ",&i0,&i1,line._data))<2) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i1).move_to(primitives);
- CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
- }
- } break;
- case 3 : {
- if ((err = std::fscanf(nfile,"%u%u%u%255[^\n] ",&i0,&i1,&i2,line._data))<3) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i2,i1).move_to(primitives);
- CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
- }
- } break;
- case 4 : {
- if ((err = std::fscanf(nfile,"%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,line._data))<4) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
- CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)).move_to(colors);
- }
- } break;
- case 5 : {
- if ((err = std::fscanf(nfile,"%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,line._data))<5) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
- CImg<tf>::vector(i0,i4,i3).move_to(primitives);
- colors.insert(2,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
- ++nb_primitives;
- }
- } break;
- case 6 : {
- if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,line._data))<6) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
- CImg<tf>::vector(i0,i5,i4,i3).move_to(primitives);
- colors.insert(2,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
- ++nb_primitives;
- }
- } break;
- case 7 : {
- if ((err = std::fscanf(nfile,"%u%u%u%u%u%u%u%255[^\n] ",&i0,&i1,&i2,&i3,&i4,&i5,&i6,line._data))<7) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i4,i3,i1).move_to(primitives);
- CImg<tf>::vector(i0,i6,i5,i4).move_to(primitives);
- CImg<tf>::vector(i3,i2,i1).move_to(primitives);
- colors.insert(3,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
- ++(++nb_primitives);
- }
- } break;
- case 8 : {
- 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) {
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- } else {
- err = cimg_sscanf(line,"%f%f%f",&c0,&c1,&c2);
- CImg<tf>::vector(i0,i3,i2,i1).move_to(primitives);
- CImg<tf>::vector(i0,i5,i4,i3).move_to(primitives);
- CImg<tf>::vector(i0,i7,i6,i5).move_to(primitives);
- colors.insert(3,CImg<tc>::vector((tc)(c0*255),(tc)(c1*255),(tc)(c2*255)));
- ++(++nb_primitives);
- }
- } break;
- default :
- cimg::warn(_cimg_instance
- "load_off(): Failed to read primitive %u/%u (%u vertices) from file '%s'.",
- cimg_instance,
- nb_read,nb_primitives,prim,filename?filename:"(FILE*)");
- err = std::fscanf(nfile,"%*[^\n] ");
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- if (primitives._width!=nb_primitives)
- cimg::warn(_cimg_instance
- "load_off(): Only %u/%u primitives read from file '%s'.",
- cimg_instance,
- primitives._width,nb_primitives,filename?filename:"(FILE*)");
- return *this;
- }
- //! Load image sequence from a video file, using OpenCV library.
- /**
- \param filename Filename, as a C-string.
- \param first_frame Index of the first frame to read.
- \param last_frame Index of the last frame to read.
- \param step_frame Step value for frame reading.
- \param axis Alignment axis.
- \param align Appending alignment.
- **/
- CImg<T>& load_video(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1,
- const char axis='z', const float align=0) {
- return get_load_video(filename,first_frame,last_frame,step_frame,axis,align).move_to(*this);
- }
- //! Load image sequence from a video file, using OpenCV library \newinstance.
- static CImg<T> get_load_video(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1,
- const char axis='z', const float align=0) {
- return CImgList<T>().load_video(filename,first_frame,last_frame,step_frame).get_append(axis,align);
- }
- //! Load image sequence using FFMPEG's external tool 'ffmpeg'.
- /**
- \param filename Filename, as a C-string.
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T>& load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) {
- return get_load_ffmpeg_external(filename,axis,align).move_to(*this);
- }
- //! Load image sequence using FFMPEG's external tool 'ffmpeg' \newinstance.
- static CImg<T> get_load_ffmpeg_external(const char *const filename, const char axis='z', const float align=0) {
- return CImgList<T>().load_ffmpeg_external(filename).get_append(axis,align);
- }
- //! Load gif file, using Imagemagick or GraphicsMagicks's external tools.
- /**
- \param filename Filename, as a C-string.
- \param axis Appending axis, if file contains multiple images. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T>& load_gif_external(const char *const filename,
- const char axis='z', const float align=0) {
- return get_load_gif_external(filename,axis,align).move_to(*this);
- }
- //! Load gif file, using ImageMagick or GraphicsMagick's external tool 'convert' \newinstance.
- static CImg<T> get_load_gif_external(const char *const filename,
- const char axis='z', const float align=0) {
- return CImgList<T>().load_gif_external(filename).get_append(axis,align);
- }
- //! Load image from a HEIC file.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_heif(const char *const filename) {
- return _load_heif(filename);
- }
- //! Load image from a HEIC file \newinstance.
- static CImg<T> get_load_heif(const char *const filename) {
- return CImg<T>().load_heif(filename);
- }
- CImg<T>& _load_heif(const char *const filename) {
- #ifndef cimg_use_heif
- return load_other(filename);
- #else
- try {
- heif::Context ctx;
- ctx.read_from_file(filename);
- heif::ImageHandle handle = ctx.get_primary_image_handle();
- const heif::Image image =
- handle.decode_image(heif_colorspace_RGB,handle.has_alpha_channel()?heif_chroma_interleaved_RGBA:
- heif_chroma_interleaved_RGB);
- const int
- W = image.get_width(heif_channel_interleaved),
- H = image.get_height(heif_channel_interleaved),
- S = handle.has_alpha_channel()?4:3;
- assign(W,H,1,S);
- int stride;
- const unsigned char *const buffer = image.get_plane(heif_channel_interleaved,&stride);
- 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;
- cimg_forY(*this,y) {
- const unsigned char *ptrs = buffer + y*stride;
- if (ptr_a) cimg_forX(*this,x) { // RGBA
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- *(ptr_a++) = (T)*(ptrs++);
- }
- else cimg_forX(*this,x) { // RGB
- *(ptr_r++) = (T)*(ptrs++);
- *(ptr_g++) = (T)*(ptrs++);
- *(ptr_b++) = (T)*(ptrs++);
- }
- }
- } catch (const heif::Error& e) {
- throw CImgInstanceException(_cimg_instance
- "load_heif(): Unable to decode image: %s",
- cimg_instance,
- e.get_message().c_str());
- } catch (...) {
- throw;
- }
- return *this;
- #endif
- }
- //! Load image using GraphicsMagick's external tool 'gm'.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_graphicsmagick_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_graphicsmagick_external(): Specified filename is (null).",
- cimg_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file = 0;
- const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
- #if cimg_OS==1
- if (!cimg::system("which gm")) {
- cimg_snprintf(command,command._width,"%s convert \"%s\" %s:-",
- cimg::graphicsmagick_path(),
- s_filename.data(),
- #ifdef cimg_use_png
- "png"
- #else
- "pnm"
- #endif
- );
- file = popen(command,"r");
- if (file) {
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try {
- #ifdef cimg_use_png
- load_png(file);
- #else
- load_pnm(file);
- #endif
- } catch (...) {
- pclose(file);
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_graphicsmagick_external(): Failed to load file '%s' "
- "with external command 'gm'.",
- cimg_instance,
- filename);
- }
- pclose(file);
- return *this;
- }
- }
- #endif
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),
- cimg_file_separator,
- cimg::filenamerand(),
- #ifdef cimg_use_png
- "png"
- #else
- "pnm"
- #endif
- );
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\" convert \"%s\" \"%s\"",
- cimg::graphicsmagick_path(),
- s_filename.data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command,cimg::graphicsmagick_path());
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "load_graphicsmagick_external(): Failed to load file '%s' with external command 'gm'.",
- cimg_instance,
- filename);
- } else cimg::fclose(file);
- #ifdef cimg_use_png
- load_png(filename_tmp);
- #else
- load_pnm(filename_tmp);
- #endif
- std::remove(filename_tmp);
- return *this;
- }
- //! Load image using GraphicsMagick's external tool 'gm' \newinstance.
- static CImg<T> get_load_graphicsmagick_external(const char *const filename) {
- return CImg<T>().load_graphicsmagick_external(filename);
- }
- //! Load gzipped image file, using external tool 'gunzip'.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_gzip_external(const char *const filename) {
- if (!filename)
- throw CImgIOException(_cimg_instance
- "load_gzip_external(): Specified filename is (null).",
- cimg_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256), body(256);
- const char
- *const ext = cimg::split_filename(filename,body),
- *const ext2 = cimg::split_filename(body,0);
- std::FILE *file = 0;
- do {
- if (!cimg::strcasecmp(ext,"gz")) {
- if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- } else {
- if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- }
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
- cimg::gunzip_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command);
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "load_gzip_external(): Failed to load file '%s' with external command 'gunzip'.",
- cimg_instance,
- filename);
- } else cimg::fclose(file);
- load(filename_tmp);
- std::remove(filename_tmp);
- return *this;
- }
- //! Load gzipped image file, using external tool 'gunzip' \newinstance.
- static CImg<T> get_load_gzip_external(const char *const filename) {
- return CImg<T>().load_gzip_external(filename);
- }
- //! Load image using ImageMagick's external tool 'convert'.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_imagemagick_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_imagemagick_external(): Specified filename is (null).",
- cimg_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file = 0;
- const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
- #if cimg_OS==1
- if (!cimg::system("which convert")) {
- cimg_snprintf(command,command._width,"%s%s \"%s\" %s:-",
- cimg::imagemagick_path(),
- !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"",
- s_filename.data(),
- #ifdef cimg_use_png
- "png"
- #else
- "pnm"
- #endif
- );
- file = popen(command,"r");
- if (file) {
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try {
- #ifdef cimg_use_png
- load_png(file);
- #else
- load_pnm(file);
- #endif
- } catch (...) {
- pclose(file);
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_imagemagick_external(): Failed to load file '%s' with "
- "external command 'magick/convert'.",
- cimg_instance,
- filename);
- }
- pclose(file);
- return *this;
- }
- }
- #endif
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),
- cimg_file_separator,
- cimg::filenamerand(),
- #ifdef cimg_use_png
- "png"
- #else
- "pnm"
- #endif
- );
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\"%s \"%s\" \"%s\"",
- cimg::imagemagick_path(),
- !cimg::strcasecmp(cimg::split_filename(filename),"pdf")?" -density 400x400":"",
- s_filename.data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command,cimg::imagemagick_path());
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "load_imagemagick_external(): Failed to load file '%s' with "
- "external command 'magick/convert'.",
- cimg_instance,
- filename);
- } else cimg::fclose(file);
- #ifdef cimg_use_png
- load_png(filename_tmp);
- #else
- load_pnm(filename_tmp);
- #endif
- std::remove(filename_tmp);
- return *this;
- }
- //! Load image using ImageMagick's external tool 'convert' \newinstance.
- static CImg<T> get_load_imagemagick_external(const char *const filename) {
- return CImg<T>().load_imagemagick_external(filename);
- }
- //! Load image from a DICOM file, using Medcon's external tool 'medcon'.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_medcon_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_medcon_external(): Specified filename is (null).",
- cimg_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256), body(256);
- cimg::fclose(cimg::fopen(filename,"r"));
- std::FILE *file = 0;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s.hdr",cimg::filenamerand());
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\" -w -c anlz -o \"%s\" -f \"%s\"",
- cimg::medcon_path(),
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::medcon_path());
- cimg::split_filename(filename_tmp,body);
- cimg_snprintf(command,command._width,"%s.hdr",body._data);
- file = cimg::std_fopen(command,"rb");
- if (!file) {
- cimg_snprintf(command,command._width,"m000-%s.hdr",body._data);
- file = cimg::std_fopen(command,"rb");
- if (!file) {
- throw CImgIOException(_cimg_instance
- "load_medcon_external(): Failed to load file '%s' with external command 'medcon'.",
- cimg_instance,
- filename);
- }
- }
- cimg::fclose(file);
- load_analyze(command);
- std::remove(command);
- cimg::split_filename(command,body);
- cimg_snprintf(command,command._width,"%s.img",body._data);
- std::remove(command);
- return *this;
- }
- //! Load image from a DICOM file, using Medcon's external tool 'medcon' \newinstance.
- static CImg<T> get_load_medcon_external(const char *const filename) {
- return CImg<T>().load_medcon_external(filename);
- }
- //! Load image from a .pdf file.
- /**
- \param filename Filename, as a C-string.
- \param resolution Image resolution.
- **/
- CImg<T>& load_pdf_external(const char *const filename, const unsigned int resolution=400) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_pdf_external(): Specified filename is (null).",
- cimg_instance);
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file = 0;
- const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
- #if cimg_OS==1
- cimg_snprintf(command,command._width,"gs -q -dNOPAUSE -sDEVICE=ppmraw -o - -r%u \"%s\"",
- resolution,s_filename.data());
- file = popen(command,"r");
- if (file) {
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try { load_pnm(file); } catch (...) {
- pclose(file);
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_pdf_external(): Failed to load file '%s' with external command 'gs'.",
- cimg_instance,
- filename);
- }
- pclose(file);
- return *this;
- }
- #endif
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.ppm",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"gs -q -dNOPAUSE -sDEVICE=ppmraw -o \"%s\" -r%u \"%s\"",
- CImg<charT>::string(filename_tmp)._system_strescape().data(),resolution,s_filename.data());
- cimg::system(command,"gs");
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "load_pdf_external(): Failed to load file '%s' with external command 'gs'.",
- cimg_instance,
- filename);
- } else cimg::fclose(file);
- load_pnm(filename_tmp);
- std::remove(filename_tmp);
- return *this;
- }
- //! Load image from a .pdf file \newinstance.
- static CImg<T> get_load_pdf_external(const char *const filename, const unsigned int resolution=400) {
- return CImg<T>().load_pdf_external(filename,resolution);
- }
- //! Load image from a RAW Color Camera file, using external tool 'dcraw'.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_dcraw_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_dcraw_external(): Specified filename is (null).",
- cimg_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file = 0;
- const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
- #if cimg_OS==1
- cimg_snprintf(command,command._width,"%s -w -4 -c \"%s\"",
- cimg::dcraw_path(),s_filename.data());
- file = popen(command,"r");
- if (file) {
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try { load_pnm(file); } catch (...) {
- pclose(file);
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.",
- cimg_instance,
- filename);
- }
- pclose(file);
- return *this;
- }
- #endif
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.ppm",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\" -w -4 -c \"%s\" > \"%s\"",
- cimg::dcraw_path(),s_filename.data(),CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command,cimg::dcraw_path());
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "load_dcraw_external(): Failed to load file '%s' with external command 'dcraw'.",
- cimg_instance,
- filename);
- } else cimg::fclose(file);
- load_pnm(filename_tmp);
- std::remove(filename_tmp);
- return *this;
- }
- //! Load image from a RAW Color Camera file, using external tool 'dcraw' \newinstance.
- static CImg<T> get_load_dcraw_external(const char *const filename) {
- return CImg<T>().load_dcraw_external(filename);
- }
- #ifdef cimg_use_opencv
- // Convert a continuous cv::Mat<uchar> to a CImg<uchar>.
- static CImg<ucharT> _cvmat2cimg(const cv::Mat &src) {
- if (src.channels()==1) return CImg<ucharT>(src.ptr(),src.cols,src.rows,1,1);
- else if (src.channels()==3) { // BGR
- CImg<ucharT> res(src.cols,src.rows,1,src.channels());
- const unsigned char *ptrs = src.ptr();
- unsigned char *pR = res.data(), *pG = res.data(0,0,0,1), *pB = res.data(0,0,0,2);
- cimg_forXY(res,x,y) { *(pB++) = *(ptrs++); *(pG++) = *(ptrs++); *(pR++) = *(ptrs++); }
- return res;
- }
- return CImg<ucharT>(src.ptr(),src.channels(),src.cols,src.rows,1,true).get_permute_axes("yzcx");
- }
- // Convert a CImg<T> to a cv::Mat.
- cv::Mat _cimg2cvmat() const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "_cimg2cvmat() : Instance image is empty.",
- cimg_instance);
- if (_spectrum==2)
- throw CImgInstanceException(_cimg_instance
- "_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').",
- cimg_instance);
- if (_depth!=1)
- throw CImgInstanceException(_cimg_instance
- "_cimg2cvmat() : Invalid number of slices (should be '1').",
- cimg_instance);
- int mat_type = -1;
- if (pixel_type()==cimg::type<unsigned char>::string()) mat_type = CV_8UC1;
- if (pixel_type()==cimg::type<char>::string()) mat_type = CV_8SC1;
- if (pixel_type()==cimg::type<unsigned short>::string()) mat_type = CV_16UC1;
- if (pixel_type()==cimg::type<short>::string()) mat_type = CV_16SC1;
- if (pixel_type()==cimg::type<int>::string()) mat_type = CV_32SC1;
- if (pixel_type()==cimg::type<float>::string()) mat_type = CV_32FC1;
- if (pixel_type()==cimg::type<double>::string()) mat_type = CV_64FC1;
- if (mat_type<0)
- throw CImgInstanceException(_cimg_instance
- "_cvmat2cimg() : pixel type '%s' is not supported.",
- cimg_instance,pixel_type());
- cv::Mat res;
- std::vector<cv::Mat> channels(_spectrum);
- if (_spectrum>1) {
- cimg_forC(*this,c)
- channels[c] = cv::Mat(_height,_width,mat_type,_data + _width*_height*(_spectrum - 1 - c));
- cv::merge(channels,res);
- } else res = cv::Mat(_height,_width,mat_type,_data).clone();
- return res;
- }
- #endif
- //! Load image from a camera stream, using OpenCV.
- /**
- \param index Index of the camera to capture images from (from 0 to 63).
- \param capture_width Width of the desired image ('0' stands for default value).
- \param capture_height Height of the desired image ('0' stands for default value).
- \param skip_frames Number of frames to skip before the capture.
- \param release_camera Tells if the camera resource must be released at the end of the method.
- **/
- CImg<T>& load_camera(const unsigned int camera_index=0,
- const unsigned int capture_width=0, const unsigned int capture_height=0,
- const unsigned int skip_frames=0, const bool release_camera=true) {
- #ifdef cimg_use_opencv
- if (camera_index>=64)
- throw CImgArgumentException(_cimg_instance
- "load_camera(): Invalid request for camera #%u "
- "(no more than 100 cameras can be managed simultaneously).",
- cimg_instance,
- camera_index);
- static cv::VideoCapture *captures[64] = { 0 };
- static unsigned int captures_w[64], captures_h[64];
- if (release_camera) {
- cimg::mutex(9);
- if (captures[camera_index]) captures[camera_index]->release();
- delete captures[camera_index];
- captures[camera_index] = 0;
- captures_w[camera_index] = captures_h[camera_index] = 0;
- cimg::mutex(9,0);
- return *this;
- }
- if (!captures[camera_index]) {
- cimg::mutex(9);
- captures[camera_index] = new cv::VideoCapture(camera_index);
- captures_w[camera_index] = captures_h[camera_index] = 0;
- if (!captures[camera_index]->isOpened()) {
- delete captures[camera_index];
- captures[camera_index] = 0;
- cimg::mutex(9,0);
- throw CImgIOException(_cimg_instance
- "load_camera(): Failed to initialize camera #%u.",
- cimg_instance,
- camera_index);
- }
- cimg::mutex(9,0);
- }
- cimg::mutex(9);
- if (capture_width!=captures_w[camera_index]) {
- captures[camera_index]->set(_cimg_cap_prop_frame_width,capture_width);
- captures_w[camera_index] = capture_width;
- }
- if (capture_height!=captures_h[camera_index]) {
- captures[camera_index]->set(_cimg_cap_prop_frame_height,capture_height);
- captures_h[camera_index] = capture_height;
- }
- for (unsigned int i = 0; i<skip_frames; ++i) captures[camera_index]->grab();
- cv::Mat cvimg;
- captures[camera_index]->read(cvimg);
- if (cvimg.empty()) assign(); else _cvmat2cimg(cvimg).move_to(*this);
- cimg::mutex(9,0);
- return *this;
- #else
- cimg::unused(camera_index,skip_frames,release_camera,capture_width,capture_height);
- throw CImgIOException(_cimg_instance
- "load_camera(): This function requires features from the OpenCV library "
- "('-Dcimg_use_opencv' must be defined).",
- cimg_instance);
- #endif
- }
- //! Load image from a camera stream, using OpenCV \newinstance.
- static CImg<T> get_load_camera(const unsigned int camera_index=0,
- const unsigned int capture_width=0, const unsigned int capture_height=0,
- const unsigned int skip_frames=0, const bool release_camera=true) {
- return CImg<T>().load_camera(camera_index,capture_width,capture_height,skip_frames,release_camera);
- }
- //! Load image using various non-native ways.
- /**
- \param filename Filename, as a C-string.
- **/
- CImg<T>& load_other(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "load_other(): Specified filename is (null).",
- cimg_instance);
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try { load_magick(filename); }
- catch (CImgException&) {
- try { load_imagemagick_external(filename); }
- catch (CImgException&) {
- try { load_graphicsmagick_external(filename); }
- catch (CImgException&) {
- try { load_cimg(filename); }
- catch (CImgException&) {
- try {
- cimg::fclose(cimg::fopen(filename,"rb"));
- } catch (CImgException&) {
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_other(): Failed to open file '%s'.",
- cimg_instance,
- filename);
- }
- cimg::exception_mode(omode);
- throw CImgIOException(_cimg_instance
- "load_other(): Failed to recognize format of file '%s'.",
- cimg_instance,
- filename);
- }
- }
- }
- }
- cimg::exception_mode(omode);
- return *this;
- }
- //! Load image using various non-native ways \newinstance.
- static CImg<T> get_load_other(const char *const filename) {
- return CImg<T>().load_other(filename);
- }
- //@}
- //---------------------------
- //
- //! \name Data Output
- //@{
- //---------------------------
- //! Display information about the image data.
- /**
- \param title Name for the considered image.
- \param display_stats Tells to compute and display image statistics.
- **/
- const CImg<T>& print(const char *const title=0, const bool display_stats=true) const {
- int xm = 0, ym = 0, zm = 0, vm = 0, xM = 0, yM = 0, zM = 0, vM = 0;
- CImg<doubleT> st;
- if (!is_empty() && display_stats) {
- st = get_stats();
- xm = (int)st[4]; ym = (int)st[5], zm = (int)st[6], vm = (int)st[7];
- xM = (int)st[8]; yM = (int)st[9], zM = (int)st[10], vM = (int)st[11];
- }
- const ulongT siz = size(), msiz = siz*sizeof(T), siz1 = siz - 1,
- mdisp = msiz<8*1024?0U:msiz<8*1024*1024?1U:2U, width1 = _width - 1;
- CImg<charT> _title(64);
- if (!title) cimg_snprintf(_title,_title._width,"CImg<%s>",pixel_type());
- std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = (%u,%u,%u,%u) [%lu %s], %sdata%s = (%s*)%p",
- cimg::t_magenta,cimg::t_bold,title?title:_title._data,cimg::t_normal,
- cimg::t_bold,cimg::t_normal,(void*)this,
- cimg::t_bold,cimg::t_normal,_width,_height,_depth,_spectrum,
- (unsigned long)(mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20))),
- mdisp==0?"b":(mdisp==1?"Kio":"Mio"),
- cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin());
- if (_data)
- std::fprintf(cimg::output(),"..%p (%s) = [ ",(void*)((char*)end() - 1),_is_shared?"shared":"non-shared");
- else std::fprintf(cimg::output()," (%s) = [ ",_is_shared?"shared":"non-shared");
- if (!is_empty()) cimg_foroff(*this,off) {
- std::fprintf(cimg::output(),"%g",(double)_data[off]);
- if (off!=siz1) std::fprintf(cimg::output(),"%s",off%_width==width1?" ; ":" ");
- if (off==7 && siz>16) { off = siz1 - 8; std::fprintf(cimg::output(),"... "); }
- }
- if (!is_empty() && display_stats)
- std::fprintf(cimg::output(),
- " ], %smin%s = %g, %smax%s = %g, %smean%s = %g, %sstd%s = %g, %scoords_min%s = (%u,%u,%u,%u), "
- "%scoords_max%s = (%u,%u,%u,%u).\n",
- cimg::t_bold,cimg::t_normal,st[0],
- cimg::t_bold,cimg::t_normal,st[1],
- cimg::t_bold,cimg::t_normal,st[2],
- cimg::t_bold,cimg::t_normal,std::sqrt(st[3]),
- cimg::t_bold,cimg::t_normal,xm,ym,zm,vm,
- cimg::t_bold,cimg::t_normal,xM,yM,zM,vM);
- else std::fprintf(cimg::output(),"%s].\n",is_empty()?"":" ");
- std::fflush(cimg::output());
- return *this;
- }
- //! Display image into a CImgDisplay window.
- /**
- \param disp Display window.
- **/
- const CImg<T>& display(CImgDisplay& disp) const {
- disp.display(*this);
- return *this;
- }
- //! Display image into a CImgDisplay window, in an interactive way.
- /**
- \param disp Display window.
- \param display_info Tells if image information are displayed on the standard output.
- \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- const CImg<T>& display(CImgDisplay &disp, const bool display_info, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false) const {
- return _display(disp,0,display_info,XYZ,exit_on_anykey,false);
- }
- //! Display image into an interactive window.
- /**
- \param title Window title
- \param display_info Tells if image information are displayed on the standard output.
- \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- const CImg<T>& display(const char *const title=0, const bool display_info=true, unsigned int *const XYZ=0,
- const bool exit_on_anykey=false) const {
- CImgDisplay disp;
- return _display(disp,title,display_info,XYZ,exit_on_anykey,false);
- }
- const CImg<T>& _display(CImgDisplay &disp, const char *const title, const bool display_info,
- unsigned int *const XYZ, const bool exit_on_anykey,
- const bool exit_on_singleclick) const {
- unsigned int oldw = 0, oldh = 0, _XYZ[3] = { 0 }, key = 0;
- int x0 = 0, y0 = 0, z0 = 0, x1 = width() - 1, y1 = height() - 1, z1 = depth() - 1,
- old_mouse_x = -1, old_mouse_y = -1;
- if (!disp) {
- disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,1);
- if (!title) disp.set_title("CImg<%s> (%ux%ux%ux%u)",pixel_type(),_width,_height,_depth,_spectrum);
- else disp.set_title("%s",title);
- } else if (title) disp.set_title("%s",title);
- disp.show().flush();
- const CImg<char> dtitle = CImg<char>::string(disp.title());
- if (display_info) print(dtitle);
- CImg<T> zoom;
- for (bool reset_view = true, resize_disp = false, is_first_select = true; !key && !disp.is_closed(); ) {
- if (reset_view) {
- if (XYZ) { _XYZ[0] = XYZ[0]; _XYZ[1] = XYZ[1]; _XYZ[2] = XYZ[2]; }
- else {
- _XYZ[0] = (unsigned int)(x0 + x1 + 1)/2;
- _XYZ[1] = (unsigned int)(y0 + y1 + 1)/2;
- _XYZ[2] = (unsigned int)(z0 + z1 + 1)/2;
- }
- x0 = 0; y0 = 0; z0 = 0; x1 = width() - 1; y1 = height() - 1; z1 = depth() - 1;
- disp.resize(cimg_fitscreen(_width,_height,_depth),false);
- oldw = disp._width; oldh = disp._height;
- resize_disp = true;
- reset_view = false;
- }
- if (!x0 && !y0 && !z0 && x1==width() - 1 && y1==height() - 1 && z1==depth() - 1) {
- if (is_empty()) zoom.assign(1,1,1,1,(T)0); else zoom.assign();
- } else zoom = get_crop(x0,y0,z0,x1,y1,z1);
- const CImg<T>& visu = zoom?zoom:*this;
- const unsigned int
- dx = 1U + x1 - x0, dy = 1U + y1 - y0, dz = 1U + z1 - z0,
- tw = dx + (dz>1?dz:0U), th = dy + (dz>1?dz:0U);
- if (!is_empty() && !disp.is_fullscreen() && resize_disp) {
- const float
- ttw = (float)tw*disp.width()/oldw, tth = (float)th*disp.height()/oldh,
- dM = std::max(ttw,tth), diM = (float)std::max(disp.width(),disp.height());
- const unsigned int
- imgw = (unsigned int)(ttw*diM/dM), imgh = (unsigned int)(tth*diM/dM);
- disp.set_fullscreen(false).resize(cimg_fitscreen(imgw,imgh,1),false);
- resize_disp = false;
- }
- oldw = tw; oldh = th;
- bool
- go_up = false, go_down = false, go_left = false, go_right = false,
- go_inc = false, go_dec = false, go_in = false, go_out = false,
- go_in_center = false;
- disp.set_title("%s",dtitle._data);
- if (_width>1 && visu._width==1) disp.set_title("%s | x=%u",disp._title,x0);
- if (_height>1 && visu._height==1) disp.set_title("%s | y=%u",disp._title,y0);
- if (_depth>1 && visu._depth==1) disp.set_title("%s | z=%u",disp._title,z0);
- disp._mouse_x = old_mouse_x; disp._mouse_y = old_mouse_y;
- CImg<intT> selection = visu._select(disp,0,2,_XYZ,x0,y0,z0,true,is_first_select,_depth>1,true);
- old_mouse_x = disp._mouse_x; old_mouse_y = disp._mouse_y;
- is_first_select = false;
- if (disp.wheel()) {
- if ((disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) &&
- (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT())) {
- go_left = !(go_right = disp.wheel()>0);
- } else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) {
- go_down = !(go_up = disp.wheel()>0);
- } else if (depth()==1 || disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- go_out = !(go_in = disp.wheel()>0); go_in_center = false;
- }
- disp.set_wheel();
- }
- const int
- sx0 = selection(0), sy0 = selection(1), sz0 = selection(2),
- sx1 = selection(3), sy1 = selection(4), sz1 = selection(5);
- if (sx0>=0 && sy0>=0 && sz0>=0 && sx1>=0 && sy1>=0 && sz1>=0) {
- x1 = x0 + sx1; y1 = y0 + sy1; z1 = z0 + sz1;
- x0+=sx0; y0+=sy0; z0+=sz0;
- if ((sx0==sx1 && sy0==sy1) || (_depth>1 && sx0==sx1 && sz0==sz1) || (_depth>1 && sy0==sy1 && sz0==sz1)) {
- if (exit_on_singleclick && (!zoom || is_empty())) break; else reset_view = true;
- }
- resize_disp = true;
- } else switch (key = disp.key()) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT : case cimg::keySHIFTRIGHT :
- #endif
- case 0 : case cimg::keyCTRLLEFT : case cimg::keySHIFTLEFT : key = 0; break;
- case cimg::keyP : if (visu._depth>1 && (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT())) {
- // Special mode: play stack of frames
- const unsigned int
- w1 = visu._width*disp.width()/(visu._width + (visu._depth>1?visu._depth:0)),
- h1 = visu._height*disp.height()/(visu._height + (visu._depth>1?visu._depth:0));
- float frame_timing = 5;
- bool is_stopped = false;
- disp.set_key(key,false).set_wheel().resize(cimg_fitscreen(w1,h1,1),false); key = 0;
- for (unsigned int timer = 0; !key && !disp.is_closed() && !disp.button(); ) {
- if (disp.is_resized()) disp.resize(false);
- if (!timer) {
- visu.get_slice((int)_XYZ[2]).display(disp.set_title("%s | z=%d",dtitle.data(),_XYZ[2]));
- (++_XYZ[2])%=visu._depth;
- }
- if (!is_stopped) { if (++timer>(unsigned int)frame_timing) timer = 0; } else timer = ~0U;
- if (disp.wheel()) { frame_timing-=disp.wheel()/3.f; disp.set_wheel(); }
- switch (key = disp.key()) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT :
- #endif
- case cimg::keyCTRLLEFT : key = 0; break;
- case cimg::keyPAGEUP : frame_timing-=0.3f; key = 0; break;
- case cimg::keyPAGEDOWN : frame_timing+=0.3f; key = 0; break;
- case cimg::keySPACE : is_stopped = !is_stopped; disp.set_key(key,false); key = 0; break;
- case cimg::keyARROWLEFT : case cimg::keyARROWUP : is_stopped = true; timer = 0; key = 0; break;
- case cimg::keyARROWRIGHT : case cimg::keyARROWDOWN : is_stopped = true;
- (_XYZ[2]+=visu._depth - 2)%=visu._depth; timer = 0; key = 0; break;
- case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
- CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false);
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false).set_key(key,false); key = 0;
- } break;
- case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(_width,_height,_depth),false).set_key(key,false); key = 0;
- } break;
- case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.resize(disp.screen_width(),disp.screen_height(),false).
- toggle_fullscreen().set_key(key,false); key = 0;
- } break;
- }
- frame_timing = frame_timing<1?1:(frame_timing>39?39:frame_timing);
- disp.wait(20);
- }
- const unsigned int
- w2 = (visu._width + (visu._depth>1?visu._depth:0))*disp.width()/visu._width,
- h2 = (visu._height + (visu._depth>1?visu._depth:0))*disp.height()/visu._height;
- disp.resize(cimg_fitscreen(w2,h2,1),false).set_title(dtitle.data()).set_key().set_button().set_wheel();
- key = 0;
- } break;
- case cimg::keyHOME : reset_view = resize_disp = true; key = 0; break;
- case cimg::keyPADADD : go_in = true; go_in_center = true; key = 0; break;
- case cimg::keyPADSUB : go_out = true; key = 0; break;
- case cimg::keyARROWLEFT : case cimg::keyPAD4: go_left = true; key = 0; break;
- case cimg::keyARROWRIGHT : case cimg::keyPAD6: go_right = true; key = 0; break;
- case cimg::keyARROWUP : case cimg::keyPAD8: go_up = true; key = 0; break;
- case cimg::keyARROWDOWN : case cimg::keyPAD2: go_down = true; key = 0; break;
- case cimg::keyPAD7 : go_up = go_left = true; key = 0; break;
- case cimg::keyPAD9 : go_up = go_right = true; key = 0; break;
- case cimg::keyPAD1 : go_down = go_left = true; key = 0; break;
- case cimg::keyPAD3 : go_down = go_right = true; key = 0; break;
- case cimg::keyPAGEUP : go_inc = true; key = 0; break;
- case cimg::keyPAGEDOWN : go_dec = true; key = 0; break;
- }
- if (go_in) {
- const int
- mx = go_in_center?disp.width()/2:disp.mouse_x(),
- my = go_in_center?disp.height()/2:disp.mouse_y(),
- mX = mx*(width() + (depth()>1?depth():0))/disp.width(),
- mY = my*(height() + (depth()>1?depth():0))/disp.height();
- int X = (int)_XYZ[0], Y = (int)_XYZ[1], Z = (int)_XYZ[2];
- if (mX<width() && mY<height()) {
- X = x0 + mX*(1 + x1 - x0)/width(); Y = y0 + mY*(1 + y1 - y0)/height();
- }
- if (mX<width() && mY>=height()) {
- X = x0 + mX*(1 + x1 - x0)/width(); Z = z0 + (mY - height())*(1 + z1 - z0)/depth();
- }
- if (mX>=width() && mY<height()) {
- Y = y0 + mY*(1 + y1 - y0)/height(); Z = z0 + (mX - width())*(1 + z1 - z0)/depth();
- }
- if (x1 - x0>4) { x0 = X - 3*(X - x0)/4; x1 = X + 3*(x1 - X)/4; }
- if (y1 - y0>4) { y0 = Y - 3*(Y - y0)/4; y1 = Y + 3*(y1 - Y)/4; }
- if (z1 - z0>4) { z0 = Z - 3*(Z - z0)/4; z1 = Z + 3*(z1 - Z)/4; }
- }
- if (go_out) {
- const int
- delta_x = (x1 - x0)/8, delta_y = (y1 - y0)/8, delta_z = (z1 - z0)/8,
- ndelta_x = delta_x?delta_x:(_width>1),
- ndelta_y = delta_y?delta_y:(_height>1),
- ndelta_z = delta_z?delta_z:(_depth>1);
- x0-=ndelta_x; y0-=ndelta_y; z0-=ndelta_z;
- x1+=ndelta_x; y1+=ndelta_y; z1+=ndelta_z;
- if (x0<0) { x1-=x0; x0 = 0; if (x1>=width()) x1 = width() - 1; }
- if (y0<0) { y1-=y0; y0 = 0; if (y1>=height()) y1 = height() - 1; }
- if (z0<0) { z1-=z0; z0 = 0; if (z1>=depth()) z1 = depth() - 1; }
- if (x1>=width()) { x0-=(x1 - width() + 1); x1 = width() - 1; if (x0<0) x0 = 0; }
- if (y1>=height()) { y0-=(y1 - height() + 1); y1 = height() - 1; if (y0<0) y0 = 0; }
- if (z1>=depth()) { z0-=(z1 - depth() + 1); z1 = depth() - 1; if (z0<0) z0 = 0; }
- const float
- ratio = (float)(x1-x0)/(y1-y0),
- ratiow = (float)disp._width/disp._height,
- sub = std::min(cimg::abs(ratio - ratiow),cimg::abs(1/ratio-1/ratiow));
- if (sub>0.01) resize_disp = true;
- }
- if (go_left) {
- const int delta = (x1 - x0)/4, ndelta = delta?delta:(_width>1);
- if (x0 - ndelta>=0) { x0-=ndelta; x1-=ndelta; }
- else { x1-=x0; x0 = 0; }
- }
- if (go_right) {
- const int delta = (x1 - x0)/4, ndelta = delta?delta:(_width>1);
- if (x1+ndelta<width()) { x0+=ndelta; x1+=ndelta; }
- else { x0+=(width() - 1 - x1); x1 = width() - 1; }
- }
- if (go_up) {
- const int delta = (y1 - y0)/4, ndelta = delta?delta:(_height>1);
- if (y0 - ndelta>=0) { y0-=ndelta; y1-=ndelta; }
- else { y1-=y0; y0 = 0; }
- }
- if (go_down) {
- const int delta = (y1 - y0)/4, ndelta = delta?delta:(_height>1);
- if (y1+ndelta<height()) { y0+=ndelta; y1+=ndelta; }
- else { y0+=(height() - 1 - y1); y1 = height() - 1; }
- }
- if (go_inc) {
- const int delta = (z1 - z0)/4, ndelta = delta?delta:(_depth>1);
- if (z0 - ndelta>=0) { z0-=ndelta; z1-=ndelta; }
- else { z1-=z0; z0 = 0; }
- }
- if (go_dec) {
- const int delta = (z1 - z0)/4, ndelta = delta?delta:(_depth>1);
- if (z1+ndelta<depth()) { z0+=ndelta; z1+=ndelta; }
- else { z0+=(depth() - 1 - z1); z1 = depth() - 1; }
- }
- disp.wait(100);
- if (!exit_on_anykey && key && key!=cimg::keyESC &&
- (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- key = 0;
- }
- }
- disp.set_key(key);
- if (XYZ) { XYZ[0] = _XYZ[0]; XYZ[1] = _XYZ[1]; XYZ[2] = _XYZ[2]; }
- return *this;
- }
- //! Display object 3D in an interactive window.
- /**
- \param disp Display window.
- \param vertices Vertices data of the 3D object.
- \param primitives Primitives data of the 3D object.
- \param colors Colors data of the 3D object.
- \param opacities Opacities data of the 3D object.
- \param centering Tells if the 3D object must be centered for the display.
- \param render_static Rendering mode.
- \param render_motion Rendering mode, when the 3D object is moved.
- \param is_double_sided Tells if the object primitives are double-sided.
- \param focale Focale
- \param light_x X-coordinate of the light source.
- \param light_y Y-coordinate of the light source.
- \param light_z Z-coordinate of the light source.
- \param specular_lightness Amount of specular light.
- \param specular_shininess Shininess of the object material.
- \param display_axes Tells if the 3D axes are displayed.
- \param pose_matrix Pointer to 12 values, defining a 3D pose (as a 4x3 matrix).
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- template<typename tp, typename tf, typename tc, typename to>
- const CImg<T>& display_object3d(CImgDisplay& disp,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return _display_object3d(disp,0,vertices,primitives,colors,opacities,centering,render_static,
- render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp, typename tf, typename tc, typename to>
- const CImg<T>& display_object3d(const char *const title,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- CImgDisplay disp;
- return _display_object3d(disp,title,vertices,primitives,colors,opacities,centering,render_static,
- render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp, typename tf, typename tc>
- const CImg<T>& display_object3d(CImgDisplay &disp,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(disp,vertices,primitives,colors,CImgList<floatT>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp, typename tf, typename tc>
- const CImg<T>& display_object3d(const char *const title,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(title,vertices,primitives,colors,CImgList<floatT>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp, typename tf>
- const CImg<T>& display_object3d(CImgDisplay &disp,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(disp,vertices,primitives,CImgList<T>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp, typename tf>
- const CImg<T>& display_object3d(const char *const title,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(title,vertices,primitives,CImgList<T>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp>
- const CImg<T>& display_object3d(CImgDisplay &disp,
- const CImg<tp>& vertices,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(disp,vertices,CImgList<uintT>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- //! Display object 3D in an interactive window \simplification.
- template<typename tp>
- const CImg<T>& display_object3d(const char *const title,
- const CImg<tp>& vertices,
- const bool centering=true,
- const int render_static=4, const int render_motion=1,
- const bool is_double_sided=true, const float focale=700,
- const float light_x=0, const float light_y=0, const float light_z=-5e8f,
- const float specular_lightness=0.2f, const float specular_shininess=0.1f,
- const bool display_axes=true, float *const pose_matrix=0,
- const bool exit_on_anykey=false) const {
- return display_object3d(title,vertices,CImgList<uintT>(),centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- template<typename tp, typename tf, typename tc, typename to>
- const CImg<T>& _display_object3d(CImgDisplay& disp, const char *const title,
- const CImg<tp>& vertices,
- const CImgList<tf>& primitives,
- const CImgList<tc>& colors,
- const to& opacities,
- const bool centering,
- const int render_static, const int render_motion,
- const bool is_double_sided, const float focale,
- const float light_x, const float light_y, const float light_z,
- const float specular_lightness, const float specular_shininess,
- const bool display_axes, float *const pose_matrix,
- const bool exit_on_anykey) const {
- typedef typename cimg::superset<tp,float>::type tpfloat;
- // Check input arguments
- if (is_empty()) {
- CImg<T> background;
- if (colors && colors[0].size()==1) background.assign(1,2,1,1,64,128);
- else background.assign(1,2,1,3,32,64,32,116,64,96);
- if (disp) background.resize(disp.width(),disp.height(),1,-100,3);
- else background.resize(cimg_fitscreen(CImgDisplay::screen_width()/2,
- CImgDisplay::screen_height()/2,1),1,-100,3);
- return background._display_object3d(disp,title,vertices,primitives,colors,opacities,centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- } else { if (disp) disp.resize(*this,false); }
- CImg<charT> error_message(1024);
- if (!vertices.is_object3d(primitives,colors,opacities,true,error_message))
- throw CImgArgumentException(_cimg_instance
- "display_object3d(): Invalid specified 3D object (%u,%u) (%s).",
- cimg_instance,vertices._width,primitives._width,error_message.data());
- if (vertices._width && !primitives) {
- CImgList<tf> nprimitives(vertices._width,1,1,1,1);
- cimglist_for(nprimitives,l) nprimitives(l,0) = (tf)l;
- return _display_object3d(disp,title,vertices,nprimitives,colors,opacities,centering,
- render_static,render_motion,is_double_sided,focale,
- light_x,light_y,light_z,specular_lightness,specular_shininess,
- display_axes,pose_matrix,exit_on_anykey);
- }
- if (!disp) {
- disp.assign(cimg_fitscreen(_width,_height,_depth),title?title:0,3);
- if (!title) disp.set_title("CImg<%s> (%u vertices, %u primitives)",
- pixel_type(),vertices._width,primitives._width);
- } else if (title) disp.set_title("%s",title);
- // Init 3D objects and compute object statistics
- CImg<floatT>
- pose,
- rotated_vertices(vertices._width,3),
- bbox_vertices, rotated_bbox_vertices,
- axes_vertices, rotated_axes_vertices,
- bbox_opacities, axes_opacities;
- CImgList<uintT> bbox_primitives, axes_primitives;
- CImgList<tf> reverse_primitives;
- CImgList<T> bbox_colors, bbox_colors2, axes_colors;
- unsigned int ns_width = 0, ns_height = 0;
- int _is_double_sided = (int)is_double_sided;
- bool ndisplay_axes = display_axes;
- const CImg<T>
- background_color(1,1,1,_spectrum,0),
- foreground_color(1,1,1,_spectrum,(T)std::min((int)cimg::type<T>::max(),255));
- float
- Xoff = 0, Yoff = 0, Zoff = 0, sprite_scale = 1,
- xm = 0, xM = vertices?vertices.get_shared_row(0).max_min(xm):0,
- ym = 0, yM = vertices?vertices.get_shared_row(1).max_min(ym):0,
- zm = 0, zM = vertices?vertices.get_shared_row(2).max_min(zm):0;
- const float delta = cimg::max(xM - xm,yM - ym,zM - zm);
- rotated_bbox_vertices = bbox_vertices.assign(8,3,1,1,
- xm,xM,xM,xm,xm,xM,xM,xm,
- ym,ym,yM,yM,ym,ym,yM,yM,
- zm,zm,zm,zm,zM,zM,zM,zM);
- 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);
- bbox_colors.assign(6,_spectrum,1,1,1,background_color[0]);
- bbox_colors2.assign(6,_spectrum,1,1,1,foreground_color[0]);
- bbox_opacities.assign(bbox_colors._width,1,1,1,0.3f);
- rotated_axes_vertices = axes_vertices.assign(7,3,1,1,
- 0,20,0,0,22,-6,-6,
- 0,0,20,0,-6,22,-6,
- 0,0,0,20,0,0,22);
- axes_opacities.assign(3,1,1,1,1);
- axes_colors.assign(3,_spectrum,1,1,1,foreground_color[0]);
- axes_primitives.assign(3,1,2,1,1, 0,1, 0,2, 0,3);
- // Begin user interaction loop
- CImg<T> visu0(*this,false), visu;
- CImg<tpfloat> zbuffer(visu0.width(),visu0.height(),1,1,0);
- bool init_pose = true, clicked = false, redraw = true;
- unsigned int key = 0, font_size = 32;
- int
- x0 = 0, y0 = 0, x1 = 0, y1 = 0,
- nrender_static = render_static,
- nrender_motion = render_motion;
- disp.show().flush();
- while (!disp.is_closed() && !key) {
- // Init object pose
- if (init_pose) {
- const float
- ratio = delta>0?(2.f*std::min(disp.width(),disp.height())/(3.f*delta)):1,
- dx = (xM + xm)/2, dy = (yM + ym)/2, dz = (zM + zm)/2;
- if (centering)
- CImg<floatT>(4,3,1,1, ratio,0.,0.,-ratio*dx, 0.,ratio,0.,-ratio*dy, 0.,0.,ratio,-ratio*dz).move_to(pose);
- else CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0).move_to(pose);
- if (pose_matrix) {
- CImg<floatT> pose0(pose_matrix,4,3,1,1,false);
- pose0.resize(4,4,1,1,0); pose.resize(4,4,1,1,0);
- pose0(3,3) = pose(3,3) = 1;
- (pose0*pose).get_crop(0,0,3,2).move_to(pose);
- Xoff = pose_matrix[12]; Yoff = pose_matrix[13]; Zoff = pose_matrix[14]; sprite_scale = pose_matrix[15];
- } else { Xoff = Yoff = Zoff = 0; sprite_scale = 1; }
- init_pose = false;
- redraw = true;
- }
- // Rotate and draw 3D object
- if (redraw) {
- const float
- r00 = pose(0,0), r10 = pose(1,0), r20 = pose(2,0), r30 = pose(3,0),
- r01 = pose(0,1), r11 = pose(1,1), r21 = pose(2,1), r31 = pose(3,1),
- r02 = pose(0,2), r12 = pose(1,2), r22 = pose(2,2), r32 = pose(3,2);
- if ((clicked && nrender_motion>=0) || (!clicked && nrender_static>=0)) {
- const tp *const pv0 = vertices.data(), *const pv1 = vertices.data(0,1), *const pv2 = vertices.data(0,2);
- float
- *const prv0 = rotated_vertices.data(),
- *const prv1 = rotated_vertices.data(0,1),
- *const prv2 = rotated_vertices.data(0,2);
- cimg_pragma_openmp(parallel for cimg_openmp_if(vertices.width()>(cimg_openmp_sizefactor)*1024))
- cimg_forX(vertices,l) {
- const float x = (float)pv0[l], y = (float)pv1[l], z = (float)pv2[l];
- prv0[l] = r00*x + r10*y + r20*z + r30;
- prv1[l] = r01*x + r11*y + r21*z + r31;
- prv2[l] = r02*x + r12*y + r22*z + r32;
- }
- }
- else cimg_forX(bbox_vertices,l) {
- const float x = bbox_vertices(l,0), y = bbox_vertices(l,1), z = bbox_vertices(l,2);
- rotated_bbox_vertices(l,0) = r00*x + r10*y + r20*z + r30;
- rotated_bbox_vertices(l,1) = r01*x + r11*y + r21*z + r31;
- rotated_bbox_vertices(l,2) = r02*x + r12*y + r22*z + r32;
- }
- // Draw objects
- const bool render_with_zbuffer = !clicked && nrender_static>0;
- visu = visu0;
- if ((clicked && nrender_motion<0) || (!clicked && nrender_static<0))
- visu.draw_object3d(Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
- rotated_bbox_vertices,bbox_primitives,bbox_colors,bbox_opacities,2,false,focale).
- draw_object3d(Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
- rotated_bbox_vertices,bbox_primitives,bbox_colors2,1,false,focale);
- else visu._draw_object3d((void*)0,render_with_zbuffer?zbuffer.fill(0):CImg<tpfloat>::empty(),
- Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
- rotated_vertices,reverse_primitives?reverse_primitives:primitives,
- colors,opacities,clicked?nrender_motion:nrender_static,_is_double_sided==1,focale,
- width()/2.f + light_x,height()/2.f + light_y,light_z + Zoff,
- specular_lightness,specular_shininess,1,sprite_scale);
- // Draw axes
- if (ndisplay_axes) {
- const float
- n = 1e-8f + cimg::hypot(r00,r01,r02),
- _r00 = r00/n, _r10 = r10/n, _r20 = r20/n,
- _r01 = r01/n, _r11 = r11/n, _r21 = r21/n,
- _r02 = r01/n, _r12 = r12/n, _r22 = r22/n,
- Xaxes = 25, Yaxes = visu._height - 38.f;
- cimg_forX(axes_vertices,l) {
- const float
- x = axes_vertices(l,0),
- y = axes_vertices(l,1),
- z = axes_vertices(l,2);
- rotated_axes_vertices(l,0) = _r00*x + _r10*y + _r20*z;
- rotated_axes_vertices(l,1) = _r01*x + _r11*y + _r21*z;
- rotated_axes_vertices(l,2) = _r02*x + _r12*y + _r22*z;
- }
- axes_opacities(0,0) = (rotated_axes_vertices(1,2)>0)?0.5f:1.f;
- axes_opacities(1,0) = (rotated_axes_vertices(2,2)>0)?0.5f:1.f;
- axes_opacities(2,0) = (rotated_axes_vertices(3,2)>0)?0.5f:1.f;
- visu.draw_object3d(Xaxes,Yaxes,0,rotated_axes_vertices,axes_primitives,
- axes_colors,axes_opacities,1,false,focale).
- draw_text((int)(Xaxes + rotated_axes_vertices(4,0)),
- (int)(Yaxes + rotated_axes_vertices(4,1)),
- "X",axes_colors[0]._data,0,axes_opacities(0,0),13).
- draw_text((int)(Xaxes + rotated_axes_vertices(5,0)),
- (int)(Yaxes + rotated_axes_vertices(5,1)),
- "Y",axes_colors[1]._data,0,axes_opacities(1,0),13).
- draw_text((int)(Xaxes + rotated_axes_vertices(6,0)),
- (int)(Yaxes + rotated_axes_vertices(6,1)),
- "Z",axes_colors[2]._data,0,axes_opacities(2,0),13);
- }
- visu.display(disp);
- if (!clicked || nrender_motion==nrender_static) redraw = false;
- }
- // Handle user interaction
- if (!redraw) disp.wait();
- if ((disp.button() || disp.wheel()) && disp.mouse_x()>=0 && disp.mouse_y()>=0) {
- redraw = true;
- if (!clicked) { x0 = x1 = disp.mouse_x(); y0 = y1 = disp.mouse_y(); if (!disp.wheel()) clicked = true; }
- else { x1 = disp.mouse_x(); y1 = disp.mouse_y(); }
- const bool is_keyCTRL = disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT();
- if (disp.button()&1 && !is_keyCTRL) {
- const float
- R = 0.45f*std::min(disp.width(),disp.height()),
- R2 = R*R,
- u0 = (float)(x0 - disp.width()/2),
- v0 = (float)(y0 - disp.height()/2),
- u1 = (float)(x1 - disp.width()/2),
- v1 = (float)(y1 - disp.height()/2),
- n0 = cimg::hypot(u0,v0),
- n1 = cimg::hypot(u1,v1),
- nu0 = n0>R?(u0*R/n0):u0,
- nv0 = n0>R?(v0*R/n0):v0,
- nw0 = (float)std::sqrt(std::max(0.f,R2 - nu0*nu0 - nv0*nv0)),
- nu1 = n1>R?(u1*R/n1):u1,
- nv1 = n1>R?(v1*R/n1):v1,
- nw1 = (float)std::sqrt(std::max(0.f,R2 - nu1*nu1 - nv1*nv1)),
- u = nv0*nw1 - nw0*nv1,
- v = nw0*nu1 - nu0*nw1,
- w = nv0*nu1 - nu0*nv1,
- n = cimg::hypot(u,v,w),
- alpha = (float)std::asin(n/R2)*180/cimg::PI;
- (CImg<floatT>::rotation_matrix(u,v,w,-alpha)*pose).move_to(pose);
- x0 = x1; y0 = y1;
- }
- if (disp.button()&2 && !is_keyCTRL) {
- if (focale>0) Zoff-=(y0 - y1)*focale/400;
- else { const float s = std::exp((y0 - y1)/400.f); pose*=s; sprite_scale*=s; }
- x0 = x1; y0 = y1;
- }
- if (disp.wheel()) {
- if (focale>0) Zoff-=disp.wheel()*focale/20;
- else { const float s = std::exp(disp.wheel()/20.f); pose*=s; sprite_scale*=s; }
- disp.set_wheel();
- }
- if (disp.button()&4 || (disp.button()&1 && is_keyCTRL)) {
- Xoff+=(x1 - x0); Yoff+=(y1 - y0); x0 = x1; y0 = y1;
- }
- if ((disp.button()&1) && (disp.button()&2) && !is_keyCTRL) {
- init_pose = true; disp.set_button(); x0 = x1; y0 = y1;
- pose = CImg<floatT>(4,3,1,1, 1,0,0,0, 0,1,0,0, 0,0,1,0);
- }
- } else if (clicked) { x0 = x1; y0 = y1; clicked = false; redraw = true; }
- CImg<charT> filename(32);
- switch (key = disp.key()) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT :
- #endif
- case 0 : case cimg::keyCTRLLEFT : key = 0; break;
- case cimg::keyD: if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
- CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
- _is_resized = true;
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).resize(cimg_fitscreen(_width,_height,_depth),false)._is_resized = true;
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- if (!ns_width || !ns_height ||
- ns_width>(unsigned int)disp.screen_width() || ns_height>(unsigned int)disp.screen_height()) {
- ns_width = disp.screen_width()*3U/4;
- ns_height = disp.screen_height()*3U/4;
- }
- if (disp.is_fullscreen()) disp.resize(ns_width,ns_height,false);
- else {
- ns_width = disp._width; ns_height = disp._height;
- disp.resize(disp.screen_width(),disp.screen_height(),false);
- }
- disp.toggle_fullscreen()._is_resized = true;
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyT : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- // Switch single/double-sided primitives.
- if (--_is_double_sided==-2) _is_double_sided = 1;
- if (_is_double_sided>=0) reverse_primitives.assign();
- else primitives.get_reverse_object3d().move_to(reverse_primitives);
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyZ : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Enable/disable Z-buffer
- if (zbuffer) zbuffer.assign();
- else zbuffer.assign(visu0.width(),visu0.height(),1,1,0);
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyX : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Show/hide 3D axes
- ndisplay_axes = !ndisplay_axes;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF1 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to points
- nrender_motion = (nrender_static==0 && nrender_motion!=0)?0:-1; nrender_static = 0;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF2 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to lines
- nrender_motion = (nrender_static==1 && nrender_motion!=1)?1:-1; nrender_static = 1;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF3 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat
- nrender_motion = (nrender_static==2 && nrender_motion!=2)?2:-1; nrender_static = 2;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF4 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to flat-shaded
- nrender_motion = (nrender_static==3 && nrender_motion!=3)?3:-1; nrender_static = 3;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF5 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- // Set rendering mode to gouraud-shaded.
- nrender_motion = (nrender_static==4 && nrender_motion!=4)?4:-1; nrender_static = 4;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyF6 : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Set rendering mode to phong-shaded
- nrender_motion = (nrender_static==5 && nrender_motion!=5)?5:-1; nrender_static = 5;
- disp.set_key(key,false); key = 0; redraw = true;
- } break;
- case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save snapshot
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu).__draw_text(" Saving snapshot... ",font_size,0).display(disp);
- visu.save(filename);
- (+visu).__draw_text(" Snapshot '%s' saved. ",font_size,0,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyG : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .off file
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.off",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu).__draw_text(" Saving object... ",font_size,0).display(disp);
- vertices.save_off(reverse_primitives?reverse_primitives:primitives,colors,filename);
- (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyO : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .cimg file
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- #ifdef cimg_use_zlib
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
- #else
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
- #endif
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu).__draw_text(" Saving object... ",font_size,0).display(disp);
- vertices.get_object3dtoCImg3d(reverse_primitives?reverse_primitives:primitives,colors,opacities).
- save(filename);
- (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- #ifdef cimg_use_board
- case cimg::keyP : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .EPS file
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.eps",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu).__draw_text(" Saving EPS snapshot... ",font_size,0).display(disp);
- LibBoard::Board board;
- (+visu)._draw_object3d(&board,zbuffer.fill(0),
- Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
- rotated_vertices,reverse_primitives?reverse_primitives:primitives,
- colors,opacities,clicked?nrender_motion:nrender_static,
- _is_double_sided==1,focale,
- visu.width()/2.f + light_x,visu.height()/2.f + light_y,light_z + Zoff,
- specular_lightness,specular_shininess,1,
- sprite_scale);
- board.saveEPS(filename);
- (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- case cimg::keyV : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) { // Save object as a .SVG file
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.svg",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu).__draw_text(" Saving SVG snapshot... ",font_size,0).display(disp);
- LibBoard::Board board;
- (+visu)._draw_object3d(&board,zbuffer.fill(0),
- Xoff + visu._width/2.f,Yoff + visu._height/2.f,Zoff,
- rotated_vertices,reverse_primitives?reverse_primitives:primitives,
- colors,opacities,clicked?nrender_motion:nrender_static,
- _is_double_sided==1,focale,
- visu.width()/2.f + light_x,visu.height()/2.f + light_y,light_z + Zoff,
- specular_lightness,specular_shininess,1,
- sprite_scale);
- board.saveSVG(filename);
- (+visu).__draw_text(" Object '%s' saved. ",font_size,0,filename._data).display(disp);
- disp.set_key(key,false); key = 0;
- } break;
- #endif
- }
- if (disp.is_resized()) {
- disp.resize(false); visu0 = get_resize(disp,1);
- if (zbuffer) zbuffer.assign(disp.width(),disp.height());
- redraw = true;
- }
- if (!exit_on_anykey && key && key!=cimg::keyESC &&
- (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- key = 0;
- }
- }
- if (pose_matrix) {
- std::memcpy(pose_matrix,pose._data,12*sizeof(float));
- pose_matrix[12] = Xoff; pose_matrix[13] = Yoff; pose_matrix[14] = Zoff; pose_matrix[15] = sprite_scale;
- }
- disp.set_button().set_key(key);
- return *this;
- }
- //! Display 1D graph in an interactive window.
- /**
- \param disp Display window.
- \param plot_type Plot type. Can be <tt>{ 0=points | 1=segments | 2=splines | 3=bars }</tt>.
- \param vertex_type Vertex type.
- \param labelx Title for the horizontal axis, as a C-string.
- \param xmin Minimum value along the X-axis.
- \param xmax Maximum value along the X-axis.
- \param labely Title for the vertical axis, as a C-string.
- \param ymin Minimum value along the X-axis.
- \param ymax Maximum value along the X-axis.
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- const CImg<T>& display_graph(CImgDisplay &disp,
- const unsigned int plot_type=1, const unsigned int vertex_type=1,
- const char *const labelx=0, const double xmin=0, const double xmax=0,
- const char *const labely=0, const double ymin=0, const double ymax=0,
- const bool exit_on_anykey=false) const {
- return _display_graph(disp,0,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax,exit_on_anykey);
- }
- //! Display 1D graph in an interactive window \overloading.
- const CImg<T>& display_graph(const char *const title=0,
- const unsigned int plot_type=1, const unsigned int vertex_type=1,
- const char *const labelx=0, const double xmin=0, const double xmax=0,
- const char *const labely=0, const double ymin=0, const double ymax=0,
- const bool exit_on_anykey=false) const {
- CImgDisplay disp;
- return _display_graph(disp,title,plot_type,vertex_type,labelx,xmin,xmax,labely,ymin,ymax,exit_on_anykey);
- }
- const CImg<T>& _display_graph(CImgDisplay &disp, const char *const title=0,
- const unsigned int plot_type=1, const unsigned int vertex_type=1,
- const char *const labelx=0, const double xmin=0, const double xmax=0,
- const char *const labely=0, const double ymin=0, const double ymax=0,
- const bool exit_on_anykey=false) const {
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "display_graph(): Empty instance.",
- cimg_instance);
- if (!disp) disp.assign(cimg_fitscreen(CImgDisplay::screen_width()/2,CImgDisplay::screen_height()/2,1),0,0).
- set_title(title?"%s":"CImg<%s>",title?title:pixel_type());
- const ulongT siz = (ulongT)_width*_height*_depth, siz1 = std::max((ulongT)1,siz - 1);
- const unsigned int old_normalization = disp.normalization();
- disp.show().flush()._normalization = 0;
- double y0 = ymin, y1 = ymax, nxmin = xmin, nxmax = xmax;
- if (nxmin==nxmax) { nxmin = 0; nxmax = siz1; }
- int x0 = 0, x1 = width()*height()*depth() - 1, key = 0;
- for (bool reset_view = true; !key && !disp.is_closed(); ) {
- if (reset_view) { x0 = 0; x1 = width()*height()*depth() - 1; y0 = ymin; y1 = ymax; reset_view = false; }
- CImg<T> zoom(x1 - x0 + 1,1,1,spectrum());
- cimg_forC(*this,c) zoom.get_shared_channel(c) = CImg<T>(data(x0,0,0,c),x1 - x0 + 1,1,1,1,true);
- if (y0==y1) { y0 = zoom.min_max(y1); const double dy = y1 - y0; y0-=dy/20; y1+=dy/20; }
- if (y0==y1) { --y0; ++y1; }
- const CImg<intT> selection = zoom.get_select_graph(disp,plot_type,vertex_type,
- labelx,
- nxmin + x0*(nxmax - nxmin)/siz1,
- nxmin + x1*(nxmax - nxmin)/siz1,
- labely,y0,y1,true);
- const int mouse_x = disp.mouse_x(), mouse_y = disp.mouse_y();
- if (selection[0]>=0) {
- if (selection[2]<0) reset_view = true;
- else {
- x1 = x0 + selection[2]; x0+=selection[0];
- if (selection[1]>=0 && selection[3]>=0) {
- y0 = y1 - selection[3]*(y1 - y0)/(disp.height() - 32);
- y1-=selection[1]*(y1 - y0)/(disp.height() - 32);
- }
- }
- } else {
- bool go_in = false, go_out = false, go_left = false, go_right = false, go_up = false, go_down = false;
- switch (key = (int)disp.key()) {
- case cimg::keyHOME : reset_view = true; key = 0; disp.set_key(); break;
- case cimg::keyPADADD : go_in = true; go_out = false; key = 0; disp.set_key(); break;
- case cimg::keyPADSUB : go_out = true; go_in = false; key = 0; disp.set_key(); break;
- case cimg::keyARROWLEFT : case cimg::keyPAD4 : go_left = true; go_right = false; key = 0; disp.set_key();
- break;
- case cimg::keyARROWRIGHT : case cimg::keyPAD6 : go_right = true; go_left = false; key = 0; disp.set_key();
- break;
- case cimg::keyARROWUP : case cimg::keyPAD8 : go_up = true; go_down = false; key = 0; disp.set_key(); break;
- case cimg::keyARROWDOWN : case cimg::keyPAD2 : go_down = true; go_up = false; key = 0; disp.set_key(); break;
- case cimg::keyPAD7 : go_left = true; go_up = true; key = 0; disp.set_key(); break;
- case cimg::keyPAD9 : go_right = true; go_up = true; key = 0; disp.set_key(); break;
- case cimg::keyPAD1 : go_left = true; go_down = true; key = 0; disp.set_key(); break;
- case cimg::keyPAD3 : go_right = true; go_down = true; key = 0; disp.set_key(); break;
- }
- if (disp.wheel()) {
- if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) go_up = !(go_down = disp.wheel()<0);
- else if (disp.is_keySHIFTLEFT() || disp.is_keySHIFTRIGHT()) go_left = !(go_right = disp.wheel()>0);
- else go_out = !(go_in = disp.wheel()>0);
- key = 0;
- }
- if (go_in) {
- const int
- xsiz = x1 - x0,
- mx = (mouse_x - 16)*xsiz/(disp.width() - 32),
- cx = x0 + cimg::cut(mx,0,xsiz);
- if (x1 - x0>4) {
- x0 = cx - 7*(cx - x0)/8; x1 = cx + 7*(x1 - cx)/8;
- if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- const double
- ysiz = y1 - y0,
- my = (mouse_y - 16)*ysiz/(disp.height() - 32),
- cy = y1 - cimg::cut(my,0.,ysiz);
- y0 = cy - 7*(cy - y0)/8; y1 = cy + 7*(y1 - cy)/8;
- } else y0 = y1 = 0;
- }
- }
- if (go_out) {
- if (x0>0 || x1<(int)siz1) {
- const int delta_x = (x1 - x0)/8, ndelta_x = delta_x?delta_x:(siz>1);
- const double ndelta_y = (y1 - y0)/8;
- x0-=ndelta_x; x1+=ndelta_x;
- y0-=ndelta_y; y1+=ndelta_y;
- if (x0<0) { x1-=x0; x0 = 0; if (x1>=(int)siz) x1 = (int)siz1; }
- if (x1>=(int)siz) { x0-=(x1 - siz1); x1 = (int)siz1; if (x0<0) x0 = 0; }
- }
- }
- if (go_left) {
- const int delta = (x1 - x0)/5, ndelta = delta?delta:1;
- if (x0 - ndelta>=0) { x0-=ndelta; x1-=ndelta; }
- else { x1-=x0; x0 = 0; }
- go_left = false;
- }
- if (go_right) {
- const int delta = (x1 - x0)/5, ndelta = delta?delta:1;
- if (x1 + ndelta<(int)siz) { x0+=ndelta; x1+=ndelta; }
- else { x0+=(siz1 - x1); x1 = (int)siz1; }
- go_right = false;
- }
- if (go_up) {
- const double delta = (y1 - y0)/10, ndelta = delta?delta:1;
- y0+=ndelta; y1+=ndelta;
- go_up = false;
- }
- if (go_down) {
- const double delta = (y1 - y0)/10, ndelta = delta?delta:1;
- y0-=ndelta; y1-=ndelta;
- go_down = false;
- }
- }
- if (!exit_on_anykey && key && key!=(int)cimg::keyESC &&
- (key!=(int)cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- disp.set_key(key,false);
- key = 0;
- }
- }
- disp._normalization = old_normalization;
- return *this;
- }
- //! Save image as a file.
- /**
- \param filename Filename, as a C-string.
- \param number When positive, represents an index added to the filename. Otherwise, no number is added.
- \param digits Number of digits used for adding the number to the filename.
- \note
- - The used file format is defined by the file extension in the filename \p filename.
- - Parameter \p number can be used to add a 6-digit number to the filename before saving.
- **/
- const CImg<T>& save(const char *const filename, const int number=-1, const unsigned int digits=6) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save(): Specified filename is (null).",
- cimg_instance);
- // Do not test for empty instances, since .cimg format is able to manage empty instances.
- const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.');
- const char *const ext = cimg::split_filename(filename);
- CImg<charT> nfilename(1024);
- const char *const fn = is_stdout?filename:(number>=0)?cimg::number_filename(filename,number,digits,nfilename):
- filename;
- #ifdef cimg_save_plugin
- cimg_save_plugin(fn);
- #endif
- #ifdef cimg_save_plugin1
- cimg_save_plugin1(fn);
- #endif
- #ifdef cimg_save_plugin2
- cimg_save_plugin2(fn);
- #endif
- #ifdef cimg_save_plugin3
- cimg_save_plugin3(fn);
- #endif
- #ifdef cimg_save_plugin4
- cimg_save_plugin4(fn);
- #endif
- #ifdef cimg_save_plugin5
- cimg_save_plugin5(fn);
- #endif
- #ifdef cimg_save_plugin6
- cimg_save_plugin6(fn);
- #endif
- #ifdef cimg_save_plugin7
- cimg_save_plugin7(fn);
- #endif
- #ifdef cimg_save_plugin8
- cimg_save_plugin8(fn);
- #endif
- // Text formats
- if (!cimg::strcasecmp(ext,"asc")) return save_ascii(fn);
- else if (!cimg::strcasecmp(ext,"csv") ||
- !cimg::strcasecmp(ext,"dlm") ||
- !cimg::strcasecmp(ext,"txt")) return save_dlm(fn);
- else if (!cimg::strcasecmp(ext,"cpp") ||
- !cimg::strcasecmp(ext,"hpp") ||
- !cimg::strcasecmp(ext,"h") ||
- !cimg::strcasecmp(ext,"c")) return save_cpp(fn);
- // 2D binary formats
- else if (!cimg::strcasecmp(ext,"bmp")) return save_bmp(fn);
- else if (!cimg::strcasecmp(ext,"jpg") ||
- !cimg::strcasecmp(ext,"jpeg") ||
- !cimg::strcasecmp(ext,"jpe") ||
- !cimg::strcasecmp(ext,"jfif") ||
- !cimg::strcasecmp(ext,"jif")) return save_jpeg(fn);
- else if (!cimg::strcasecmp(ext,"rgb")) return save_rgb(fn);
- else if (!cimg::strcasecmp(ext,"rgba")) return save_rgba(fn);
- else if (!cimg::strcasecmp(ext,"png")) return save_png(fn);
- else if (!cimg::strcasecmp(ext,"pgm") ||
- !cimg::strcasecmp(ext,"ppm") ||
- !cimg::strcasecmp(ext,"pnm")) return save_pnm(fn);
- else if (!cimg::strcasecmp(ext,"pnk")) return save_pnk(fn);
- else if (!cimg::strcasecmp(ext,"pfm")) return save_pfm(fn);
- else if (!cimg::strcasecmp(ext,"exr")) return save_exr(fn);
- else if (!cimg::strcasecmp(ext,"tif") ||
- !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn);
- // 3D binary formats
- else if (!*ext) {
- #ifdef cimg_use_zlib
- return save_cimg(fn,true);
- #else
- return save_cimg(fn,false);
- #endif
- } else if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true);
- else if (!cimg::strcasecmp(ext,"cimg")) return save_cimg(fn,false);
- else if (!cimg::strcasecmp(ext,"dcm")) return save_medcon_external(fn);
- else if (!cimg::strcasecmp(ext,"hdr") ||
- !cimg::strcasecmp(ext,"nii")) return save_analyze(fn);
- else if (!cimg::strcasecmp(ext,"inr")) return save_inr(fn);
- else if (!cimg::strcasecmp(ext,"mnc")) return save_minc2(fn);
- else if (!cimg::strcasecmp(ext,"pan")) return save_pandore(fn);
- else if (!cimg::strcasecmp(ext,"raw")) return save_raw(fn);
- // Archive files
- else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn);
- // Image sequences
- else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,444,true);
- else if (!cimg::strcasecmp(ext,"avi") ||
- !cimg::strcasecmp(ext,"mov") ||
- !cimg::strcasecmp(ext,"asf") ||
- !cimg::strcasecmp(ext,"divx") ||
- !cimg::strcasecmp(ext,"flv") ||
- !cimg::strcasecmp(ext,"mpg") ||
- !cimg::strcasecmp(ext,"m1v") ||
- !cimg::strcasecmp(ext,"m2v") ||
- !cimg::strcasecmp(ext,"m4v") ||
- !cimg::strcasecmp(ext,"mjp") ||
- !cimg::strcasecmp(ext,"mp4") ||
- !cimg::strcasecmp(ext,"mkv") ||
- !cimg::strcasecmp(ext,"mpe") ||
- !cimg::strcasecmp(ext,"movie") ||
- !cimg::strcasecmp(ext,"ogm") ||
- !cimg::strcasecmp(ext,"ogg") ||
- !cimg::strcasecmp(ext,"ogv") ||
- !cimg::strcasecmp(ext,"qt") ||
- !cimg::strcasecmp(ext,"rm") ||
- !cimg::strcasecmp(ext,"vob") ||
- !cimg::strcasecmp(ext,"webm") ||
- !cimg::strcasecmp(ext,"wmv") ||
- !cimg::strcasecmp(ext,"xvid") ||
- !cimg::strcasecmp(ext,"mpeg")) return save_video(fn);
- return save_other(fn);
- }
- //! Save image as an ascii file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_ascii(const char *const filename) const {
- return _save_ascii(0,filename);
- }
- //! Save image as an Ascii file \overloading.
- const CImg<T>& save_ascii(std::FILE *const file) const {
- return _save_ascii(file,0);
- }
- const CImg<T>& _save_ascii(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_ascii(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
- std::fprintf(nfile,"%u %u %u %u\n",_width,_height,_depth,_spectrum);
- const T* ptrs = _data;
- cimg_forYZC(*this,y,z,c) {
- cimg_forX(*this,x) std::fprintf(nfile,"%.17g ",(double)*(ptrs++));
- std::fputc('\n',nfile);
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a .cpp source file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_cpp(const char *const filename) const {
- return _save_cpp(0,filename);
- }
- //! Save image as a .cpp source file \overloading.
- const CImg<T>& save_cpp(std::FILE *const file) const {
- return _save_cpp(file,0);
- }
- const CImg<T>& _save_cpp(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_cpp(): Specified filename is (null).",
- cimg_instance);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
- CImg<charT> varname(1024); *varname = 0;
- if (filename) cimg_sscanf(cimg::basename(filename),"%1023[a-zA-Z0-9_]",varname._data);
- if (!*varname) cimg_snprintf(varname,varname._width,"unnamed");
- std::fprintf(nfile,
- "/* Define image '%s' of size %ux%ux%ux%u and type '%s' */\n"
- "%s data_%s[] = { %s\n ",
- varname._data,_width,_height,_depth,_spectrum,pixel_type(),pixel_type(),varname._data,
- is_empty()?"};":"");
- if (!is_empty()) for (ulongT off = 0, siz = size() - 1; off<=siz; ++off) {
- std::fprintf(nfile,cimg::type<T>::format(),cimg::type<T>::format((*this)[off]));
- if (off==siz) std::fprintf(nfile," };\n");
- else if (!((off + 1)%16)) std::fprintf(nfile,",\n ");
- else std::fprintf(nfile,", ");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a DLM file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_dlm(const char *const filename) const {
- return _save_dlm(0,filename);
- }
- //! Save image as a DLM file \overloading.
- const CImg<T>& save_dlm(std::FILE *const file) const {
- return _save_dlm(file,0);
- }
- const CImg<T>& _save_dlm(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_dlm(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_dlm(): Instance is volumetric, values along Z will be unrolled in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (_spectrum>1)
- cimg::warn(_cimg_instance
- "save_dlm(): Instance is multispectral, values along C will be unrolled in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
- const T* ptrs = _data;
- cimg_forYZC(*this,y,z,c) {
- cimg_forX(*this,x) std::fprintf(nfile,"%.17g%s",(double)*(ptrs++),(x==width() - 1)?"":",");
- std::fputc('\n',nfile);
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a BMP file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_bmp(const char *const filename) const {
- return _save_bmp(0,filename);
- }
- //! Save image as a BMP file \overloading.
- const CImg<T>& save_bmp(std::FILE *const file) const {
- return _save_bmp(file,0);
- }
- const CImg<T>& _save_bmp(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_bmp(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_bmp(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (_spectrum>3)
- cimg::warn(_cimg_instance
- "save_bmp(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- CImg<ucharT> header(54,1,1,1,0);
- unsigned char align_buf[4] = { 0 };
- const unsigned int
- align = (4 - (3*_width)%4)%4,
- buf_size = (3*_width + align)*height(),
- file_size = 54 + buf_size;
- header[0] = 'B'; header[1] = 'M';
- header[0x02] = file_size&0xFF;
- header[0x03] = (file_size>>8)&0xFF;
- header[0x04] = (file_size>>16)&0xFF;
- header[0x05] = (file_size>>24)&0xFF;
- header[0x0A] = 0x36;
- header[0x0E] = 0x28;
- header[0x12] = _width&0xFF;
- header[0x13] = (_width>>8)&0xFF;
- header[0x14] = (_width>>16)&0xFF;
- header[0x15] = (_width>>24)&0xFF;
- header[0x16] = _height&0xFF;
- header[0x17] = (_height>>8)&0xFF;
- header[0x18] = (_height>>16)&0xFF;
- header[0x19] = (_height>>24)&0xFF;
- header[0x1A] = 1;
- header[0x1B] = 0;
- header[0x1C] = 24;
- header[0x1D] = 0;
- header[0x22] = buf_size&0xFF;
- header[0x23] = (buf_size>>8)&0xFF;
- header[0x24] = (buf_size>>16)&0xFF;
- header[0x25] = (buf_size>>24)&0xFF;
- header[0x27] = 0x1;
- header[0x2B] = 0x1;
- cimg::fwrite(header._data,54,nfile);
- const T
- *ptr_r = data(0,_height - 1,0,0),
- *ptr_g = (_spectrum>=2)?data(0,_height - 1,0,1):0,
- *ptr_b = (_spectrum>=3)?data(0,_height - 1,0,2):0;
- switch (_spectrum) {
- case 1 : {
- cimg_forY(*this,y) {
- cimg_forX(*this,x) {
- const unsigned char val = (unsigned char)*(ptr_r++);
- std::fputc(val,nfile); std::fputc(val,nfile); std::fputc(val,nfile);
- }
- cimg::fwrite(align_buf,align,nfile);
- ptr_r-=2*_width;
- }
- } break;
- case 2 : {
- cimg_forY(*this,y) {
- cimg_forX(*this,x) {
- std::fputc(0,nfile);
- std::fputc((unsigned char)(*(ptr_g++)),nfile);
- std::fputc((unsigned char)(*(ptr_r++)),nfile);
- }
- cimg::fwrite(align_buf,align,nfile);
- ptr_r-=2*_width; ptr_g-=2*_width;
- }
- } break;
- default : {
- cimg_forY(*this,y) {
- cimg_forX(*this,x) {
- std::fputc((unsigned char)(*(ptr_b++)),nfile);
- std::fputc((unsigned char)(*(ptr_g++)),nfile);
- std::fputc((unsigned char)(*(ptr_r++)),nfile);
- }
- cimg::fwrite(align_buf,align,nfile);
- ptr_r-=2*_width; ptr_g-=2*_width; ptr_b-=2*_width;
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a JPEG file.
- /**
- \param filename Filename, as a C-string.
- \param quality Image quality (in %)
- **/
- const CImg<T>& save_jpeg(const char *const filename, const unsigned int quality=100) const {
- return _save_jpeg(0,filename,quality);
- }
- //! Save image as a JPEG file \overloading.
- const CImg<T>& save_jpeg(std::FILE *const file, const unsigned int quality=100) const {
- return _save_jpeg(file,0,quality);
- }
- const CImg<T>& _save_jpeg(std::FILE *const file, const char *const filename, const unsigned int quality) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_jpeg(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_jpeg(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- #ifndef cimg_use_jpeg
- if (!file) return save_other(filename,quality);
- else throw CImgIOException(_cimg_instance
- "save_jpeg(): Unable to save data in '(*FILE)' unless libjpeg is enabled.",
- cimg_instance);
- #else
- unsigned int dimbuf = 0;
- J_COLOR_SPACE colortype = JCS_RGB;
- switch (_spectrum) {
- case 1 : dimbuf = 1; colortype = JCS_GRAYSCALE; break;
- case 2 : dimbuf = 3; colortype = JCS_RGB; break;
- case 3 : dimbuf = 3; colortype = JCS_RGB; break;
- default : dimbuf = 4; colortype = JCS_CMYK; break;
- }
- // Call libjpeg functions
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_compress(&cinfo);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- jpeg_stdio_dest(&cinfo,nfile);
- cinfo.image_width = _width;
- cinfo.image_height = _height;
- cinfo.input_components = dimbuf;
- cinfo.in_color_space = colortype;
- jpeg_set_defaults(&cinfo);
- jpeg_set_quality(&cinfo,quality<100?quality:100,TRUE);
- jpeg_start_compress(&cinfo,TRUE);
- JSAMPROW row_pointer[1];
- CImg<ucharT> buffer(_width*dimbuf);
- while (cinfo.next_scanline<cinfo.image_height) {
- unsigned char *ptrd = buffer._data;
- // Fill pixel buffer
- switch (_spectrum) {
- case 1 : { // Greyscale images
- const T *ptr_g = data(0, cinfo.next_scanline);
- for (unsigned int b = 0; b<cinfo.image_width; b++)
- *(ptrd++) = (unsigned char)*(ptr_g++);
- } break;
- case 2 : { // RG images
- const T *ptr_r = data(0,cinfo.next_scanline,0,0),
- *ptr_g = data(0,cinfo.next_scanline,0,1);
- for (unsigned int b = 0; b<cinfo.image_width; ++b) {
- *(ptrd++) = (unsigned char)*(ptr_r++);
- *(ptrd++) = (unsigned char)*(ptr_g++);
- *(ptrd++) = 0;
- }
- } break;
- case 3 : { // RGB images
- const T *ptr_r = data(0,cinfo.next_scanline,0,0),
- *ptr_g = data(0,cinfo.next_scanline,0,1),
- *ptr_b = data(0,cinfo.next_scanline,0,2);
- for (unsigned int b = 0; b<cinfo.image_width; ++b) {
- *(ptrd++) = (unsigned char)*(ptr_r++);
- *(ptrd++) = (unsigned char)*(ptr_g++);
- *(ptrd++) = (unsigned char)*(ptr_b++);
- }
- } break;
- default : { // CMYK images
- const T *ptr_r = data(0,cinfo.next_scanline,0,0),
- *ptr_g = data(0,cinfo.next_scanline,0,1),
- *ptr_b = data(0,cinfo.next_scanline,0,2),
- *ptr_a = data(0,cinfo.next_scanline,0,3);
- for (unsigned int b = 0; b<cinfo.image_width; ++b) {
- *(ptrd++) = (unsigned char)*(ptr_r++);
- *(ptrd++) = (unsigned char)*(ptr_g++);
- *(ptrd++) = (unsigned char)*(ptr_b++);
- *(ptrd++) = (unsigned char)*(ptr_a++);
- }
- }
- }
- *row_pointer = buffer._data;
- jpeg_write_scanlines(&cinfo,row_pointer,1);
- }
- jpeg_finish_compress(&cinfo);
- if (!file) cimg::fclose(nfile);
- jpeg_destroy_compress(&cinfo);
- return *this;
- #endif
- }
- //! Save image, using built-in ImageMagick++ library.
- /**
- \param filename Filename, as a C-string.
- \param bytes_per_pixel Force the number of bytes per pixel for the saving, when possible.
- **/
- const CImg<T>& save_magick(const char *const filename, const unsigned int bytes_per_pixel=0) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_magick(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- #ifdef cimg_use_magick
- double stmin, stmax = (double)max_min(stmin);
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_magick(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename);
- if (_spectrum>3)
- cimg::warn(_cimg_instance
- "save_magick(): Instance is multispectral, only the three first channels will be "
- "saved in file '%s'.",
- cimg_instance,
- filename);
- if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
- cimg::warn(_cimg_instance
- "save_magick(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
- cimg_instance,
- stmin,stmax,filename);
- Magick::Image image(Magick::Geometry(_width,_height),"black");
- image.type(Magick::TrueColorType);
- image.depth(bytes_per_pixel?(8*bytes_per_pixel):(stmax>=256?16:8));
- const T
- *ptr_r = data(0,0,0,0),
- *ptr_g = _spectrum>1?data(0,0,0,1):0,
- *ptr_b = _spectrum>2?data(0,0,0,2):0;
- Magick::PixelPacket *pixels = image.getPixels(0,0,_width,_height);
- switch (_spectrum) {
- case 1 : // Scalar images
- for (ulongT off = (ulongT)_width*_height; off; --off) {
- pixels->red = pixels->green = pixels->blue = (Magick::Quantum)*(ptr_r++);
- ++pixels;
- }
- break;
- case 2 : // RG images
- for (ulongT off = (ulongT)_width*_height; off; --off) {
- pixels->red = (Magick::Quantum)*(ptr_r++);
- pixels->green = (Magick::Quantum)*(ptr_g++);
- pixels->blue = 0; ++pixels;
- }
- break;
- default : // RGB images
- for (ulongT off = (ulongT)_width*_height; off; --off) {
- pixels->red = (Magick::Quantum)*(ptr_r++);
- pixels->green = (Magick::Quantum)*(ptr_g++);
- pixels->blue = (Magick::Quantum)*(ptr_b++);
- ++pixels;
- }
- }
- image.syncPixels();
- image.write(filename);
- return *this;
- #else
- cimg::unused(bytes_per_pixel);
- throw CImgIOException(_cimg_instance
- "save_magick(): Unable to save file '%s' unless libMagick++ is enabled.",
- cimg_instance,
- filename);
- #endif
- }
- //! Save image as a PNG file.
- /**
- \param filename Filename, as a C-string.
- \param bytes_per_pixel Force the number of bytes per pixels for the saving, when possible.
- **/
- const CImg<T>& save_png(const char *const filename, const unsigned int bytes_per_pixel=0) const {
- return _save_png(0,filename,bytes_per_pixel);
- }
- //! Save image as a PNG file \overloading.
- const CImg<T>& save_png(std::FILE *const file, const unsigned int bytes_per_pixel=0) const {
- return _save_png(file,0,bytes_per_pixel);
- }
- const CImg<T>& _save_png(std::FILE *const file, const char *const filename,
- const unsigned int bytes_per_pixel=0) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_png(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- #ifndef cimg_use_png
- cimg::unused(bytes_per_pixel);
- if (!file) return save_other(filename);
- else throw CImgIOException(_cimg_instance
- "save_png(): Unable to save data in '(*FILE)' unless libpng is enabled.",
- cimg_instance);
- #else
- #if defined __GNUC__
- const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning
- std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"wb");
- volatile double stmin, stmax = (double)max_min(stmin);
- #else
- const char *nfilename = filename;
- std::FILE *nfile = file?file:cimg::fopen(nfilename,"wb");
- double stmin, stmax = (double)max_min(stmin);
- #endif
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_png(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename);
- if (_spectrum>4)
- cimg::warn(_cimg_instance
- "save_png(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
- cimg_instance,
- filename);
- if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
- cimg::warn(_cimg_instance
- "save_png(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
- cimg_instance,
- stmin,stmax,filename);
- // Setup PNG structures for write
- png_voidp user_error_ptr = 0;
- png_error_ptr user_error_fn = 0, user_warning_fn = 0;
- png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,user_error_ptr, user_error_fn,
- user_warning_fn);
- if (!png_ptr){
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "save_png(): Failed to initialize 'png_ptr' structure when saving file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_infop info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_write_struct(&png_ptr,(png_infopp)0);
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "save_png(): Failed to initialize 'info_ptr' structure when saving file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- if (setjmp(png_jmpbuf(png_ptr))) {
- png_destroy_write_struct(&png_ptr, &info_ptr);
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_init_io(png_ptr, nfile);
- const int bit_depth = bytes_per_pixel?(bytes_per_pixel*8):(stmax>=256?16:8);
- int color_type;
- switch (spectrum()) {
- case 1 : color_type = PNG_COLOR_TYPE_GRAY; break;
- case 2 : color_type = PNG_COLOR_TYPE_GRAY_ALPHA; break;
- case 3 : color_type = PNG_COLOR_TYPE_RGB; break;
- default : color_type = PNG_COLOR_TYPE_RGB_ALPHA;
- }
- const int interlace_type = PNG_INTERLACE_NONE;
- const int compression_type = PNG_COMPRESSION_TYPE_DEFAULT;
- const int filter_method = PNG_FILTER_TYPE_DEFAULT;
- png_set_IHDR(png_ptr,info_ptr,_width,_height,bit_depth,color_type,interlace_type,compression_type,filter_method);
- png_write_info(png_ptr,info_ptr);
- const int byte_depth = bit_depth>>3;
- const int numChan = spectrum()>4?4:spectrum();
- const int pixel_bit_depth_flag = numChan * (bit_depth - 1);
- // Allocate Memory for Image Save and Fill pixel data
- png_bytep *const imgData = new png_byte*[_height];
- for (unsigned int row = 0; row<_height; ++row) imgData[row] = new png_byte[byte_depth*numChan*_width];
- const T *pC0 = data(0,0,0,0);
- switch (pixel_bit_depth_flag) {
- case 7 : { // Gray 8-bit
- cimg_forY(*this,y) {
- unsigned char *ptrd = imgData[y];
- cimg_forX(*this,x) *(ptrd++) = (unsigned char)*(pC0++);
- }
- } break;
- case 14 : { // Gray w/ Alpha 8-bit
- const T *pC1 = data(0,0,0,1);
- cimg_forY(*this,y) {
- unsigned char *ptrd = imgData[y];
- cimg_forX(*this,x) {
- *(ptrd++) = (unsigned char)*(pC0++);
- *(ptrd++) = (unsigned char)*(pC1++);
- }
- }
- } break;
- case 21 : { // RGB 8-bit
- const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2);
- cimg_forY(*this,y) {
- unsigned char *ptrd = imgData[y];
- cimg_forX(*this,x) {
- *(ptrd++) = (unsigned char)*(pC0++);
- *(ptrd++) = (unsigned char)*(pC1++);
- *(ptrd++) = (unsigned char)*(pC2++);
- }
- }
- } break;
- case 28 : { // RGB x/ Alpha 8-bit
- const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3);
- cimg_forY(*this,y){
- unsigned char *ptrd = imgData[y];
- cimg_forX(*this,x){
- *(ptrd++) = (unsigned char)*(pC0++);
- *(ptrd++) = (unsigned char)*(pC1++);
- *(ptrd++) = (unsigned char)*(pC2++);
- *(ptrd++) = (unsigned char)*(pC3++);
- }
- }
- } break;
- case 15 : { // Gray 16-bit
- cimg_forY(*this,y){
- unsigned short *ptrd = (unsigned short*)(imgData[y]);
- cimg_forX(*this,x) *(ptrd++) = (unsigned short)*(pC0++);
- if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],_width);
- }
- } break;
- case 30 : { // Gray w/ Alpha 16-bit
- const T *pC1 = data(0,0,0,1);
- cimg_forY(*this,y){
- unsigned short *ptrd = (unsigned short*)(imgData[y]);
- cimg_forX(*this,x) {
- *(ptrd++) = (unsigned short)*(pC0++);
- *(ptrd++) = (unsigned short)*(pC1++);
- }
- if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],2*_width);
- }
- } break;
- case 45 : { // RGB 16-bit
- const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2);
- cimg_forY(*this,y) {
- unsigned short *ptrd = (unsigned short*)(imgData[y]);
- cimg_forX(*this,x) {
- *(ptrd++) = (unsigned short)*(pC0++);
- *(ptrd++) = (unsigned short)*(pC1++);
- *(ptrd++) = (unsigned short)*(pC2++);
- }
- if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],3*_width);
- }
- } break;
- case 60 : { // RGB w/ Alpha 16-bit
- const T *pC1 = data(0,0,0,1), *pC2 = data(0,0,0,2), *pC3 = data(0,0,0,3);
- cimg_forY(*this,y) {
- unsigned short *ptrd = (unsigned short*)(imgData[y]);
- cimg_forX(*this,x) {
- *(ptrd++) = (unsigned short)*(pC0++);
- *(ptrd++) = (unsigned short)*(pC1++);
- *(ptrd++) = (unsigned short)*(pC2++);
- *(ptrd++) = (unsigned short)*(pC3++);
- }
- if (!cimg::endianness()) cimg::invert_endianness((unsigned short*)imgData[y],4*_width);
- }
- } break;
- default :
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimg_instance
- "save_png(): Encountered unknown fatal error in libpng when saving file '%s'.",
- cimg_instance,
- nfilename?nfilename:"(FILE*)");
- }
- png_write_image(png_ptr,imgData);
- png_write_end(png_ptr,info_ptr);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- // Deallocate Image Write Memory
- cimg_forY(*this,n) delete[] imgData[n];
- delete[] imgData;
- if (!file) cimg::fclose(nfile);
- return *this;
- #endif
- }
- //! Save image as a PNM file.
- /**
- \param filename Filename, as a C-string.
- \param bytes_per_pixel Force the number of bytes per pixels for the saving.
- **/
- const CImg<T>& save_pnm(const char *const filename, const unsigned int bytes_per_pixel=0) const {
- return _save_pnm(0,filename,bytes_per_pixel);
- }
- //! Save image as a PNM file \overloading.
- const CImg<T>& save_pnm(std::FILE *const file, const unsigned int bytes_per_pixel=0) const {
- return _save_pnm(file,0,bytes_per_pixel);
- }
- const CImg<T>& _save_pnm(std::FILE *const file, const char *const filename,
- const unsigned int bytes_per_pixel=0) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_pnm(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- double stmin, stmax = (double)max_min(stmin);
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (_spectrum>3)
- cimg::warn(_cimg_instance
- "save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)
- cimg::warn(_cimg_instance
- "save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.",
- cimg_instance,
- stmin,stmax,filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const T
- *ptr_r = data(0,0,0,0),
- *ptr_g = (_spectrum>=2)?data(0,0,0,1):0,
- *ptr_b = (_spectrum>=3)?data(0,0,0,2):0;
- const ulongT buf_size = std::min((ulongT)(1024*1024),(ulongT)(_width*_height*(_spectrum==1?1UL:3UL)));
- std::fprintf(nfile,"P%c\n%u %u\n%u\n",
- (_spectrum==1?'5':'6'),_width,_height,stmax<256?255:(stmax<4096?4095:65535));
- switch (_spectrum) {
- case 1 : { // Scalar image
- if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PGM 8 bits
- CImg<ucharT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size);
- unsigned char *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr_r++);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- } else { // Binary PGM 16 bits
- CImg<ushortT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size);
- unsigned short *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned short)*(ptr_r++);
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- }
- } break;
- case 2 : { // RG image
- if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits
- CImg<ucharT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size/3);
- unsigned char *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (unsigned char)*(ptr_r++);
- *(ptrd++) = (unsigned char)*(ptr_g++);
- *(ptrd++) = 0;
- }
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- } else { // Binary PPM 16 bits
- CImg<ushortT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size/3);
- unsigned short *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (unsigned short)*(ptr_r++);
- *(ptrd++) = (unsigned short)*(ptr_g++);
- *(ptrd++) = 0;
- }
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- }
- } break;
- default : { // RGB image
- if (bytes_per_pixel==1 || (!bytes_per_pixel && stmax<256)) { // Binary PPM 8 bits
- CImg<ucharT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size/3);
- unsigned char *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (unsigned char)*(ptr_r++);
- *(ptrd++) = (unsigned char)*(ptr_g++);
- *(ptrd++) = (unsigned char)*(ptr_b++);
- }
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- } else { // Binary PPM 16 bits
- CImg<ushortT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size/3);
- unsigned short *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (unsigned short)*(ptr_r++);
- *(ptrd++) = (unsigned short)*(ptr_g++);
- *(ptrd++) = (unsigned short)*(ptr_b++);
- }
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a PNK file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_pnk(const char *const filename) const {
- return _save_pnk(0,filename);
- }
- //! Save image as a PNK file \overloading.
- const CImg<T>& save_pnk(std::FILE *const file) const {
- return _save_pnk(file,0);
- }
- const CImg<T>& _save_pnk(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_pnk(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_spectrum>1)
- cimg::warn(_cimg_instance
- "save_pnk(): Instance is multispectral, only the first channel will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- const ulongT buf_size = std::min((ulongT)1024*1024,(ulongT)_width*_height*_depth);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const T *ptr = data(0,0,0,0);
- if (!cimg::type<T>::is_float() && sizeof(T)==1 && _depth<2) // Can be saved as regular PNM file
- _save_pnm(file,filename,0);
- else if (!cimg::type<T>::is_float() && sizeof(T)==1) { // Save as extended P5 file: Binary byte-valued 3D
- std::fprintf(nfile,"P5\n%u %u %u\n255\n",_width,_height,_depth);
- CImg<ucharT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size);
- unsigned char *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (unsigned char)*(ptr++);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- } else if (!cimg::type<T>::is_float()) { // Save as P8: Binary int32-valued 3D
- if (_depth>1) std::fprintf(nfile,"P8\n%u %u %u\n%d\n",_width,_height,_depth,(int)max());
- else std::fprintf(nfile,"P8\n%u %u\n%d\n",_width,_height,(int)max());
- CImg<intT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size);
- int *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (int)*(ptr++);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- } else { // Save as P9: Binary float-valued 3D
- if (_depth>1) std::fprintf(nfile,"P9\n%u %u %u\n%g\n",_width,_height,_depth,(double)max());
- else std::fprintf(nfile,"P9\n%u %u\n%g\n",_width,_height,(double)max());
- CImg<floatT> buf((unsigned int)buf_size);
- for (longT to_write = (longT)width()*height()*depth(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,buf_size);
- float *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (float)*(ptr++);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a PFM file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_pfm(const char *const filename) const {
- get_mirror('y')._save_pfm(0,filename);
- return *this;
- }
- //! Save image as a PFM file \overloading.
- const CImg<T>& save_pfm(std::FILE *const file) const {
- get_mirror('y')._save_pfm(file,0);
- return *this;
- }
- const CImg<T>& _save_pfm(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_pfm(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_pfm(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- if (_spectrum>3)
- cimg::warn(_cimg_instance
- "save_pfm(): image instance is multispectral, only the three first channels will be saved "
- "in file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const T
- *ptr_r = data(0,0,0,0),
- *ptr_g = (_spectrum>=2)?data(0,0,0,1):0,
- *ptr_b = (_spectrum>=3)?data(0,0,0,2):0;
- const unsigned int buf_size = std::min(1024*1024U,_width*_height*(_spectrum==1?1:3));
- std::fprintf(nfile,"P%c\n%u %u\n1.0\n",
- (_spectrum==1?'f':'F'),_width,_height);
- switch (_spectrum) {
- case 1 : { // Scalar image
- CImg<floatT> buf(buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const ulongT N = std::min((ulongT)to_write,(ulongT)buf_size);
- float *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) *(ptrd++) = (float)*(ptr_r++);
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,N,nfile);
- to_write-=N;
- }
- } break;
- case 2 : { // RG image
- CImg<floatT> buf(buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const unsigned int N = std::min((unsigned int)to_write,buf_size/3);
- float *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (float)*(ptr_r++);
- *(ptrd++) = (float)*(ptr_g++);
- *(ptrd++) = 0;
- }
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- } break;
- default : { // RGB image
- CImg<floatT> buf(buf_size);
- for (longT to_write = (longT)width()*height(); to_write>0; ) {
- const unsigned int N = std::min((unsigned int)to_write,buf_size/3);
- float *ptrd = buf._data;
- for (ulongT i = N; i>0; --i) {
- *(ptrd++) = (float)*(ptr_r++);
- *(ptrd++) = (float)*(ptr_g++);
- *(ptrd++) = (float)*(ptr_b++);
- }
- if (!cimg::endianness()) cimg::invert_endianness(buf._data,buf_size);
- cimg::fwrite(buf._data,3*N,nfile);
- to_write-=N;
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a RGB file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_rgb(const char *const filename) const {
- return _save_rgb(0,filename);
- }
- //! Save image as a RGB file \overloading.
- const CImg<T>& save_rgb(std::FILE *const file) const {
- return _save_rgb(file,0);
- }
- const CImg<T>& _save_rgb(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_rgb(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_spectrum!=3)
- cimg::warn(_cimg_instance
- "save_rgb(): image instance has not exactly 3 channels, for file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const ulongT wh = (ulongT)_width*_height;
- unsigned char *const buffer = new unsigned char[3*wh], *nbuffer = buffer;
- const T
- *ptr1 = data(0,0,0,0),
- *ptr2 = _spectrum>1?data(0,0,0,1):0,
- *ptr3 = _spectrum>2?data(0,0,0,2):0;
- switch (_spectrum) {
- case 1 : { // Scalar image
- for (ulongT k = 0; k<wh; ++k) {
- const unsigned char val = (unsigned char)*(ptr1++);
- *(nbuffer++) = val;
- *(nbuffer++) = val;
- *(nbuffer++) = val;
- }
- } break;
- case 2 : { // RG image
- for (ulongT k = 0; k<wh; ++k) {
- *(nbuffer++) = (unsigned char)(*(ptr1++));
- *(nbuffer++) = (unsigned char)(*(ptr2++));
- *(nbuffer++) = 0;
- }
- } break;
- default : { // RGB image
- for (ulongT k = 0; k<wh; ++k) {
- *(nbuffer++) = (unsigned char)(*(ptr1++));
- *(nbuffer++) = (unsigned char)(*(ptr2++));
- *(nbuffer++) = (unsigned char)(*(ptr3++));
- }
- }
- }
- cimg::fwrite(buffer,3*wh,nfile);
- if (!file) cimg::fclose(nfile);
- delete[] buffer;
- return *this;
- }
- //! Save image as a RGBA file.
- /**
- \param filename Filename, as a C-string.
- **/
- const CImg<T>& save_rgba(const char *const filename) const {
- return _save_rgba(0,filename);
- }
- //! Save image as a RGBA file \overloading.
- const CImg<T>& save_rgba(std::FILE *const file) const {
- return _save_rgba(file,0);
- }
- const CImg<T>& _save_rgba(std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_rgba(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- if (_spectrum!=4)
- cimg::warn(_cimg_instance
- "save_rgba(): image instance has not exactly 4 channels, for file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const ulongT wh = (ulongT)_width*_height;
- unsigned char *const buffer = new unsigned char[4*wh], *nbuffer = buffer;
- const T
- *ptr1 = data(0,0,0,0),
- *ptr2 = _spectrum>1?data(0,0,0,1):0,
- *ptr3 = _spectrum>2?data(0,0,0,2):0,
- *ptr4 = _spectrum>3?data(0,0,0,3):0;
- switch (_spectrum) {
- case 1 : { // Scalar images
- for (ulongT k = 0; k<wh; ++k) {
- const unsigned char val = (unsigned char)*(ptr1++);
- *(nbuffer++) = val;
- *(nbuffer++) = val;
- *(nbuffer++) = val;
- *(nbuffer++) = 255;
- }
- } break;
- case 2 : { // RG images
- for (ulongT k = 0; k<wh; ++k) {
- *(nbuffer++) = (unsigned char)(*(ptr1++));
- *(nbuffer++) = (unsigned char)(*(ptr2++));
- *(nbuffer++) = 0;
- *(nbuffer++) = 255;
- }
- } break;
- case 3 : { // RGB images
- for (ulongT k = 0; k<wh; ++k) {
- *(nbuffer++) = (unsigned char)(*(ptr1++));
- *(nbuffer++) = (unsigned char)(*(ptr2++));
- *(nbuffer++) = (unsigned char)(*(ptr3++));
- *(nbuffer++) = 255;
- }
- } break;
- default : { // RGBA images
- for (ulongT k = 0; k<wh; ++k) {
- *(nbuffer++) = (unsigned char)(*(ptr1++));
- *(nbuffer++) = (unsigned char)(*(ptr2++));
- *(nbuffer++) = (unsigned char)(*(ptr3++));
- *(nbuffer++) = (unsigned char)(*(ptr4++));
- }
- }
- }
- cimg::fwrite(buffer,4*wh,nfile);
- if (!file) cimg::fclose(nfile);
- delete[] buffer;
- return *this;
- }
- //! Save image as a TIFF file.
- /**
- \param filename Filename, as a C-string.
- \param compression_type Type of data compression. Can be <tt>{ 0=None | 1=LZW | 2=JPEG }</tt>.
- \param[out] voxel_size Voxel size, to be stored in the filename.
- \param[out] description Description, to be stored in the filename.
- \param use_bigtiff Allow to save big tiff files (>4Gb).
- \note
- - libtiff support is enabled by defining the precompilation
- directive \c cimg_use_tif.
- - When libtiff is enabled, 2D and 3D (multipage) several
- channel per pixel are supported for
- <tt>char,uchar,short,ushort,float</tt> and \c double pixel types.
- - If \c cimg_use_tiff is not defined at compile time the
- function uses CImg<T>&save_other(const char*).
- **/
- const CImg<T>& save_tiff(const char *const filename, const unsigned int compression_type=0,
- const float *const voxel_size=0, const char *const description=0,
- const bool use_bigtiff=true) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_tiff(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- #ifdef cimg_use_tiff
- const bool
- _use_bigtiff = use_bigtiff && sizeof(ulongT)>=8 && size()*sizeof(T)>=1UL<<31; // No bigtiff for small images
- TIFF *tif = TIFFOpen(filename,_use_bigtiff?"w8":"w4");
- if (tif) {
- cimg_forZ(*this,z) _save_tiff(tif,z,z,compression_type,voxel_size,description);
- TIFFClose(tif);
- } else throw CImgIOException(_cimg_instance
- "save_tiff(): Failed to open file '%s' for writing.",
- cimg_instance,
- filename);
- return *this;
- #else
- cimg::unused(compression_type,voxel_size,description,use_bigtiff);
- return save_other(filename);
- #endif
- }
- #ifdef cimg_use_tiff
- #define _cimg_save_tiff(types,typed,compression_type) if (!std::strcmp(types,pixel_type())) { \
- const typed foo = (typed)0; return _save_tiff(tif,directory,z,foo,compression_type,voxel_size,description); }
- // [internal] Save a plane into a tiff file
- template<typename t>
- const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z, const t& pixel_t,
- const unsigned int compression_type, const float *const voxel_size,
- const char *const description) const {
- if (is_empty() || !tif || pixel_t) return *this;
- const char *const filename = TIFFFileName(tif);
- uint32_t rowsperstrip = (uint32_t)-1;
- uint16_t spp = _spectrum, bpp = sizeof(t)*8, photometric;
- if (spp==3 || spp==4) photometric = PHOTOMETRIC_RGB;
- else photometric = PHOTOMETRIC_MINISBLACK;
- TIFFSetDirectory(tif,directory);
- TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,_width);
- TIFFSetField(tif,TIFFTAG_IMAGELENGTH,_height);
- if (voxel_size) {
- const float vx = voxel_size[0], vy = voxel_size[1], vz = voxel_size[2];
- TIFFSetField(tif,TIFFTAG_RESOLUTIONUNIT,RESUNIT_NONE);
- TIFFSetField(tif,TIFFTAG_XRESOLUTION,1.f/vx);
- TIFFSetField(tif,TIFFTAG_YRESOLUTION,1.f/vy);
- CImg<charT> s_description(256);
- cimg_snprintf(s_description,s_description._width,"VX=%g VY=%g VZ=%g spacing=%g",vx,vy,vz,vz);
- TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,s_description.data());
- }
- if (description) TIFFSetField(tif,TIFFTAG_IMAGEDESCRIPTION,description);
- TIFFSetField(tif,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
- TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,spp);
- if (cimg::type<t>::is_float()) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,3);
- else if (cimg::type<t>::min()==0) TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,1);
- else TIFFSetField(tif,TIFFTAG_SAMPLEFORMAT,2);
- double valm, valM = max_min(valm);
- TIFFSetField(tif,TIFFTAG_SMINSAMPLEVALUE,valm);
- TIFFSetField(tif,TIFFTAG_SMAXSAMPLEVALUE,valM);
- TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,bpp);
- TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
- TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,photometric);
- TIFFSetField(tif,TIFFTAG_COMPRESSION,compression_type==2?COMPRESSION_JPEG:
- compression_type==1?COMPRESSION_LZW:COMPRESSION_NONE);
- rowsperstrip = TIFFDefaultStripSize(tif,rowsperstrip);
- TIFFSetField(tif,TIFFTAG_ROWSPERSTRIP,rowsperstrip);
- TIFFSetField(tif,TIFFTAG_FILLORDER,FILLORDER_MSB2LSB);
- TIFFSetField(tif,TIFFTAG_SOFTWARE,"CImg");
- t *const buf = (t*)_TIFFmalloc(TIFFStripSize(tif));
- if (buf) {
- for (unsigned int row = 0; row<_height; row+=rowsperstrip) {
- uint32_t nrow = (row + rowsperstrip>_height?_height - row:rowsperstrip);
- tstrip_t strip = TIFFComputeStrip(tif,row,0);
- tsize_t i = 0;
- for (unsigned int rr = 0; rr<nrow; ++rr)
- for (unsigned int cc = 0; cc<_width; ++cc)
- for (unsigned int vv = 0; vv<spp; ++vv)
- buf[i++] = (t)(*this)(cc,row + rr,z,vv);
- if (TIFFWriteEncodedStrip(tif,strip,buf,i*sizeof(t))<0)
- throw CImgIOException(_cimg_instance
- "save_tiff(): Invalid strip writing when saving file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- }
- _TIFFfree(buf);
- }
- TIFFWriteDirectory(tif);
- return *this;
- }
- const CImg<T>& _save_tiff(TIFF *tif, const unsigned int directory, const unsigned int z,
- const unsigned int compression_type, const float *const voxel_size,
- const char *const description) const {
- _cimg_save_tiff("bool",unsigned char,compression_type);
- _cimg_save_tiff("unsigned char",unsigned char,compression_type);
- _cimg_save_tiff("char",char,compression_type);
- _cimg_save_tiff("unsigned short",unsigned short,compression_type);
- _cimg_save_tiff("short",short,compression_type);
- _cimg_save_tiff("unsigned int",unsigned int,compression_type);
- _cimg_save_tiff("int",int,compression_type);
- _cimg_save_tiff("unsigned int64",unsigned int,compression_type);
- _cimg_save_tiff("int64",int,compression_type);
- _cimg_save_tiff("float",float,compression_type);
- _cimg_save_tiff("double",float,compression_type);
- const char *const filename = TIFFFileName(tif);
- throw CImgInstanceException(_cimg_instance
- "save_tiff(): Unsupported pixel type '%s' for file '%s'.",
- cimg_instance,
- pixel_type(),filename?filename:"(FILE*)");
- return *this;
- }
- #endif
- //! Save image as a MINC2 file.
- /**
- \param filename Filename, as a C-string.
- \param imitate_file If non-zero, reference filename, as a C-string, to borrow header from.
- **/
- const CImg<T>& save_minc2(const char *const filename,
- const char *const imitate_file=0) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_minc2(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- #ifndef cimg_use_minc2
- cimg::unused(imitate_file);
- return save_other(filename);
- #else
- minc::minc_1_writer wtr;
- if (imitate_file)
- wtr.open(filename, imitate_file);
- else {
- minc::minc_info di;
- if (width()) di.push_back(minc::dim_info(width(),width()*0.5,-1,minc::dim_info::DIM_X));
- if (height()) di.push_back(minc::dim_info(height(),height()*0.5,-1,minc::dim_info::DIM_Y));
- if (depth()) di.push_back(minc::dim_info(depth(),depth()*0.5,-1,minc::dim_info::DIM_Z));
- if (spectrum()) di.push_back(minc::dim_info(spectrum(),spectrum()*0.5,-1,minc::dim_info::DIM_TIME));
- wtr.open(filename,di,1,NC_FLOAT,0);
- }
- if (pixel_type()==cimg::type<unsigned char>::string())
- wtr.setup_write_byte();
- else if (pixel_type()==cimg::type<int>::string())
- wtr.setup_write_int();
- else if (pixel_type()==cimg::type<double>::string())
- wtr.setup_write_double();
- else
- wtr.setup_write_float();
- minc::save_standard_volume(wtr, this->_data);
- return *this;
- #endif
- }
- //! Save image as an ANALYZE7.5 or NIFTI file.
- /**
- \param filename Filename, as a C-string.
- \param voxel_size Pointer to 3 consecutive values that tell about the voxel sizes along the X,Y and Z dimensions.
- **/
- const CImg<T>& save_analyze(const char *const filename, const float *const voxel_size=0) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_analyze(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- std::FILE *file;
- CImg<charT> hname(1024), iname(1024);
- const char *const ext = cimg::split_filename(filename);
- short datatype = -1;
- if (!*ext) {
- cimg_snprintf(hname,hname._width,"%s.hdr",filename);
- cimg_snprintf(iname,iname._width,"%s.img",filename);
- }
- if (!cimg::strncasecmp(ext,"hdr",3)) {
- std::strcpy(hname,filename);
- std::strncpy(iname,filename,iname._width - 1);
- cimg_sprintf(iname._data + std::strlen(iname) - 3,"img");
- }
- if (!cimg::strncasecmp(ext,"img",3)) {
- std::strcpy(hname,filename);
- std::strncpy(iname,filename,iname._width - 1);
- cimg_sprintf(hname._data + std::strlen(iname) - 3,"hdr");
- }
- if (!cimg::strncasecmp(ext,"nii",3)) {
- std::strncpy(hname,filename,hname._width - 1); *iname = 0;
- }
- CImg<charT> header(*iname?348:352,1,1,1,0);
- int *const iheader = (int*)header._data;
- *iheader = 348;
- std::strcpy(header._data + 4,"CImg");
- std::strcpy(header._data + 14," ");
- ((short*)&(header[36]))[0] = 4096;
- ((char*)&(header[38]))[0] = 114;
- ((short*)&(header[40]))[0] = 4;
- ((short*)&(header[40]))[1] = (short)_width;
- ((short*)&(header[40]))[2] = (short)_height;
- ((short*)&(header[40]))[3] = (short)_depth;
- ((short*)&(header[40]))[4] = (short)_spectrum;
- if (!cimg::strcasecmp(pixel_type(),"bool")) datatype = 2;
- if (!cimg::strcasecmp(pixel_type(),"unsigned char")) datatype = 2;
- if (!cimg::strcasecmp(pixel_type(),"char")) datatype = 2;
- if (!cimg::strcasecmp(pixel_type(),"unsigned short")) datatype = 4;
- if (!cimg::strcasecmp(pixel_type(),"short")) datatype = 4;
- if (!cimg::strcasecmp(pixel_type(),"unsigned int")) datatype = 8;
- if (!cimg::strcasecmp(pixel_type(),"int")) datatype = 8;
- if (!cimg::strcasecmp(pixel_type(),"unsigned int64")) datatype = 8;
- if (!cimg::strcasecmp(pixel_type(),"int64")) datatype = 8;
- if (!cimg::strcasecmp(pixel_type(),"float")) datatype = 16;
- if (!cimg::strcasecmp(pixel_type(),"double")) datatype = 64;
- if (datatype<0)
- throw CImgIOException(_cimg_instance
- "save_analyze(): Unsupported pixel type '%s' for file '%s'.",
- cimg_instance,
- pixel_type(),filename);
- ((short*)&(header[70]))[0] = datatype;
- ((short*)&(header[72]))[0] = sizeof(T);
- ((float*)&(header[108]))[0] = (float)(*iname?0:header.width());
- ((float*)&(header[112]))[0] = 1;
- ((float*)&(header[76]))[0] = 0;
- if (voxel_size) {
- ((float*)&(header[76]))[1] = voxel_size[0];
- ((float*)&(header[76]))[2] = voxel_size[1];
- ((float*)&(header[76]))[3] = voxel_size[2];
- } else ((float*)&(header[76]))[1] = ((float*)&(header[76]))[2] = ((float*)&(header[76]))[3] = 1;
- file = cimg::fopen(hname,"wb");
- cimg::fwrite(header._data,header.width(),file);
- if (*iname) { cimg::fclose(file); file = cimg::fopen(iname,"wb"); }
- cimg::fwrite(_data,size(),file);
- cimg::fclose(file);
- return *this;
- }
- //! Save image as a .cimg file.
- /**
- \param filename Filename, as a C-string.
- \param is_compressed Tells if the file contains compressed image data.
- **/
- const CImg<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {
- CImgList<T>(*this,true).save_cimg(filename,is_compressed);
- return *this;
- }
- //! Save image as a .cimg file \overloading.
- const CImg<T>& save_cimg(std::FILE *const file, const bool is_compressed=false) const {
- CImgList<T>(*this,true).save_cimg(file,is_compressed);
- return *this;
- }
- //! Save image as a sub-image into an existing .cimg file.
- /**
- \param filename Filename, as a C-string.
- \param n0 Index of the image inside the file.
- \param x0 X-coordinate of the sub-image location.
- \param y0 Y-coordinate of the sub-image location.
- \param z0 Z-coordinate of the sub-image location.
- \param c0 C-coordinate of the sub-image location.
- **/
- const CImg<T>& save_cimg(const char *const filename,
- const unsigned int n0,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0) const {
- CImgList<T>(*this,true).save_cimg(filename,n0,x0,y0,z0,c0);
- return *this;
- }
- //! Save image as a sub-image into an existing .cimg file \overloading.
- const CImg<T>& save_cimg(std::FILE *const file,
- const unsigned int n0,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0) const {
- CImgList<T>(*this,true).save_cimg(file,n0,x0,y0,z0,c0);
- return *this;
- }
- //! Save blank image as a .cimg file.
- /**
- \param filename Filename, as a C-string.
- \param dx Width of the image.
- \param dy Height of the image.
- \param dz Depth of the image.
- \param dc Number of channels of the image.
- \note
- - All pixel values of the saved image are set to \c 0.
- - Use this method to save large images without having to instantiate and allocate them.
- **/
- static void save_empty_cimg(const char *const filename,
- const unsigned int dx, const unsigned int dy=1,
- const unsigned int dz=1, const unsigned int dc=1) {
- return CImgList<T>::save_empty_cimg(filename,1,dx,dy,dz,dc);
- }
- //! Save blank image as a .cimg file \overloading.
- /**
- Same as save_empty_cimg(const char *,unsigned int,unsigned int,unsigned int,unsigned int)
- with a file stream argument instead of a filename string.
- **/
- static void save_empty_cimg(std::FILE *const file,
- const unsigned int dx, const unsigned int dy=1,
- const unsigned int dz=1, const unsigned int dc=1) {
- return CImgList<T>::save_empty_cimg(file,1,dx,dy,dz,dc);
- }
- //! Save image as an INRIMAGE-4 file.
- /**
- \param filename Filename, as a C-string.
- \param voxel_size Pointer to 3 values specifying the voxel sizes along the X,Y and Z dimensions.
- **/
- const CImg<T>& save_inr(const char *const filename, const float *const voxel_size=0) const {
- return _save_inr(0,filename,voxel_size);
- }
- //! Save image as an INRIMAGE-4 file \overloading.
- const CImg<T>& save_inr(std::FILE *const file, const float *const voxel_size=0) const {
- return _save_inr(file,0,voxel_size);
- }
- const CImg<T>& _save_inr(std::FILE *const file, const char *const filename, const float *const voxel_size) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_inr(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- int inrpixsize = -1;
- const char *inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0";
- if (!cimg::strcasecmp(pixel_type(),"unsigned char")) {
- inrtype = "unsigned fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1;
- }
- if (!cimg::strcasecmp(pixel_type(),"char")) {
- inrtype = "fixed\nPIXSIZE=8 bits\nSCALE=2**0"; inrpixsize = 1;
- }
- if (!cimg::strcasecmp(pixel_type(),"unsigned short")) {
- inrtype = "unsigned fixed\nPIXSIZE=16 bits\nSCALE=2**0";inrpixsize = 2;
- }
- if (!cimg::strcasecmp(pixel_type(),"short")) {
- inrtype = "fixed\nPIXSIZE=16 bits\nSCALE=2**0"; inrpixsize = 2;
- }
- if (!cimg::strcasecmp(pixel_type(),"unsigned int")) {
- inrtype = "unsigned fixed\nPIXSIZE=32 bits\nSCALE=2**0";inrpixsize = 4;
- }
- if (!cimg::strcasecmp(pixel_type(),"int")) {
- inrtype = "fixed\nPIXSIZE=32 bits\nSCALE=2**0"; inrpixsize = 4;
- }
- if (!cimg::strcasecmp(pixel_type(),"float")) {
- inrtype = "float\nPIXSIZE=32 bits"; inrpixsize = 4;
- }
- if (!cimg::strcasecmp(pixel_type(),"double")) {
- inrtype = "float\nPIXSIZE=64 bits"; inrpixsize = 8;
- }
- if (inrpixsize<=0)
- throw CImgIOException(_cimg_instance
- "save_inr(): Unsupported pixel type '%s' for file '%s'",
- cimg_instance,
- pixel_type(),filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- CImg<charT> header(257);
- int err = cimg_snprintf(header,header._width,"#INRIMAGE-4#{\nXDIM=%u\nYDIM=%u\nZDIM=%u\nVDIM=%u\n",
- _width,_height,_depth,_spectrum);
- if (voxel_size) err+=cimg_sprintf(header._data + err,"VX=%g\nVY=%g\nVZ=%g\n",
- voxel_size[0],voxel_size[1],voxel_size[2]);
- err+=cimg_sprintf(header._data + err,"TYPE=%s\nCPU=%s\n",inrtype,cimg::endianness()?"sun":"decm");
- std::memset(header._data + err,'\n',252 - err);
- std::memcpy(header._data + 252,"##}\n",4);
- cimg::fwrite(header._data,256,nfile);
- cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) cimg::fwrite(&((*this)(x,y,z,c)),1,nfile);
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as an OpenEXR file.
- /**
- \param filename Filename, as a C-string.
- \note The OpenEXR file format is <a href="http://en.wikipedia.org/wiki/OpenEXR">described here</a>.
- **/
- const CImg<T>& save_exr(const char *const filename) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_exr(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_exr(): Instance is volumetric, only the first slice will be saved in file '%s'.",
- cimg_instance,
- filename);
- #ifndef cimg_use_openexr
- return save_other(filename);
- #else
- Imf::Rgba *const ptrd0 = new Imf::Rgba[(size_t)_width*_height], *ptrd = ptrd0, rgba;
- switch (_spectrum) {
- case 1 : { // Grayscale image
- for (const T *ptr_r = data(), *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
- rgba.r = (half)(*(ptr_r));
- rgba.g = (half)(*(ptr_r));
- rgba.b = (half)(*(ptr_r++));
- rgba.a = (half)1;
- *(ptrd++) = rgba;
- }
- } break;
- case 2 : { // RG image
- for (const T *ptr_r = data(), *ptr_g = data(0,0,0,1),
- *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e; ) {
- rgba.r = (half)(*(ptr_r++));
- rgba.g = (half)(*(ptr_g++));
- rgba.b = (half)0;
- rgba.a = (half)1;
- *(ptrd++) = rgba;
- }
- } break;
- case 3 : { // RGB image
- for (const T *ptr_r = data(), *ptr_g = data(0,0,0,1), *ptr_b = data(0,0,0,2),
- *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
- rgba.r = (half)(*(ptr_r++));
- rgba.g = (half)(*(ptr_g++));
- rgba.b = (half)(*(ptr_b++));
- rgba.a = (half)1;
- *(ptrd++) = rgba;
- }
- } break;
- default : { // RGBA image
- 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),
- *const ptr_e = ptr_r + (ulongT)_width*_height; ptr_r<ptr_e;) {
- rgba.r = (half)(*(ptr_r++));
- rgba.g = (half)(*(ptr_g++));
- rgba.b = (half)(*(ptr_b++));
- rgba.a = (half)(*(ptr_a++));
- *(ptrd++) = rgba;
- }
- } break;
- }
- Imf::RgbaOutputFile outFile(filename,_width,_height,
- _spectrum==1?Imf::WRITE_Y:_spectrum==2?Imf::WRITE_YA:_spectrum==3?
- Imf::WRITE_RGB:Imf::WRITE_RGBA);
- outFile.setFrameBuffer(ptrd0,1,_width);
- outFile.writePixels(_height);
- delete[] ptrd0;
- return *this;
- #endif
- }
- //! Save image as a Pandore-5 file.
- /**
- \param filename Filename, as a C-string.
- \param colorspace Colorspace data field in output file
- (see <a href="http://www.greyc.ensicaen.fr/~regis/Pandore">Pandore file specifications</a>
- for more information).
- **/
- const CImg<T>& save_pandore(const char *const filename, const unsigned int colorspace=0) const {
- return _save_pandore(0,filename,colorspace);
- }
- //! Save image as a Pandore-5 file \overloading.
- /**
- Same as save_pandore(const char *,unsigned int) const
- with a file stream argument instead of a filename string.
- **/
- const CImg<T>& save_pandore(std::FILE *const file, const unsigned int colorspace=0) const {
- return _save_pandore(file,0,colorspace);
- }
- unsigned int _save_pandore_header_length(unsigned int id, unsigned int *dims, const unsigned int colorspace) const {
- unsigned int nbdims = 0;
- if (id==2 || id==3 || id==4) {
- dims[0] = 1; dims[1] = _width; nbdims = 2;
- }
- if (id==5 || id==6 || id==7) {
- dims[0] = 1; dims[1] = _height; dims[2] = _width; nbdims=3;
- }
- if (id==8 || id==9 || id==10) {
- dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4;
- }
- if (id==16 || id==17 || id==18) {
- dims[0] = 3; dims[1] = _height; dims[2] = _width; dims[3] = colorspace; nbdims = 4;
- }
- if (id==19 || id==20 || id==21) {
- dims[0] = 3; dims[1] = _depth; dims[2] = _height; dims[3] = _width; dims[4] = colorspace; nbdims = 5;
- }
- if (id==22 || id==23 || id==25) {
- dims[0] = _spectrum; dims[1] = _width; nbdims = 2;
- }
- if (id==26 || id==27 || id==29) {
- dims[0] = _spectrum; dims[1] = _height; dims[2] = _width; nbdims=3;
- }
- if (id==30 || id==31 || id==33) {
- dims[0] = _spectrum; dims[1] = _depth; dims[2] = _height; dims[3] = _width; nbdims = 4;
- }
- return nbdims;
- }
- const CImg<T>& _save_pandore(std::FILE *const file, const char *const filename,
- const unsigned int colorspace) const {
- #define __cimg_save_pandore_case(dtype) \
- dtype *buffer = new dtype[size()]; \
- const T *ptrs = _data; \
- cimg_foroff(*this,off) *(buffer++) = (dtype)(*(ptrs++)); \
- buffer-=size(); \
- cimg::fwrite(buffer,size(),nfile); \
- delete[] buffer
- #define _cimg_save_pandore_case(sy,sz,sv,stype,id) \
- if (!saved && (sy?(sy==_height):true) && (sz?(sz==_depth):true) && \
- (sv?(sv==_spectrum):true) && !std::strcmp(stype,pixel_type())) { \
- unsigned int *iheader = (unsigned int*)(header + 12); \
- nbdims = _save_pandore_header_length((*iheader=id),dims,colorspace); \
- cimg::fwrite(header,36,nfile); \
- if (sizeof(unsigned long)==4) { CImg<ulongT> ndims(5); \
- for (int d = 0; d<5; ++d) ndims[d] = (unsigned long)dims[d]; \
- cimg::fwrite(ndims._data,nbdims,nfile); } \
- else if (sizeof(unsigned int)==4) { CImg<uintT> ndims(5); \
- for (int d = 0; d<5; ++d) ndims[d] = (unsigned int)dims[d]; \
- cimg::fwrite(ndims._data,nbdims,nfile); } \
- else if (sizeof(unsigned short)==4) { CImg<ushortT> ndims(5); \
- for (int d = 0; d<5; ++d) ndims[d] = (unsigned short)dims[d]; \
- cimg::fwrite(ndims._data,nbdims,nfile); } \
- else throw CImgIOException(_cimg_instance \
- "save_pandore(): Unsupported datatype for file '%s'.",\
- cimg_instance, \
- filename?filename:"(FILE*)"); \
- if (id==2 || id==5 || id==8 || id==16 || id==19 || id==22 || id==26 || id==30) { \
- __cimg_save_pandore_case(unsigned char); \
- } else if (id==3 || id==6 || id==9 || id==17 || id==20 || id==23 || id==27 || id==31) { \
- if (sizeof(unsigned long)==4) { __cimg_save_pandore_case(unsigned long); } \
- else if (sizeof(unsigned int)==4) { __cimg_save_pandore_case(unsigned int); } \
- else if (sizeof(unsigned short)==4) { __cimg_save_pandore_case(unsigned short); } \
- else throw CImgIOException(_cimg_instance \
- "save_pandore(): Unsupported datatype for file '%s'.",\
- cimg_instance, \
- filename?filename:"(FILE*)"); \
- } else if (id==4 || id==7 || id==10 || id==18 || id==21 || id==25 || id==29 || id==33) { \
- if (sizeof(double)==4) { __cimg_save_pandore_case(double); } \
- else if (sizeof(float)==4) { __cimg_save_pandore_case(float); } \
- else throw CImgIOException(_cimg_instance \
- "save_pandore(): Unsupported datatype for file '%s'.",\
- cimg_instance, \
- filename?filename:"(FILE*)"); \
- } \
- saved = true; \
- }
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_pandore(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- unsigned char header[36] = { 'P','A','N','D','O','R','E','0','4',0,0,0,
- 0,0,0,0,'C','I','m','g',0,0,0,0,0,
- 'N','o',' ','d','a','t','e',0,0,0,0 };
- unsigned int nbdims, dims[5] = { 0 };
- bool saved = false;
- _cimg_save_pandore_case(1,1,1,"unsigned char",2);
- _cimg_save_pandore_case(1,1,1,"char",3);
- _cimg_save_pandore_case(1,1,1,"unsigned short",3);
- _cimg_save_pandore_case(1,1,1,"short",3);
- _cimg_save_pandore_case(1,1,1,"unsigned int",3);
- _cimg_save_pandore_case(1,1,1,"int",3);
- _cimg_save_pandore_case(1,1,1,"unsigned int64",3);
- _cimg_save_pandore_case(1,1,1,"int64",3);
- _cimg_save_pandore_case(1,1,1,"float",4);
- _cimg_save_pandore_case(1,1,1,"double",4);
- _cimg_save_pandore_case(0,1,1,"unsigned char",5);
- _cimg_save_pandore_case(0,1,1,"char",6);
- _cimg_save_pandore_case(0,1,1,"unsigned short",6);
- _cimg_save_pandore_case(0,1,1,"short",6);
- _cimg_save_pandore_case(0,1,1,"unsigned int",6);
- _cimg_save_pandore_case(0,1,1,"int",6);
- _cimg_save_pandore_case(0,1,1,"unsigned int64",6);
- _cimg_save_pandore_case(0,1,1,"int64",6);
- _cimg_save_pandore_case(0,1,1,"float",7);
- _cimg_save_pandore_case(0,1,1,"double",7);
- _cimg_save_pandore_case(0,0,1,"unsigned char",8);
- _cimg_save_pandore_case(0,0,1,"char",9);
- _cimg_save_pandore_case(0,0,1,"unsigned short",9);
- _cimg_save_pandore_case(0,0,1,"short",9);
- _cimg_save_pandore_case(0,0,1,"unsigned int",9);
- _cimg_save_pandore_case(0,0,1,"int",9);
- _cimg_save_pandore_case(0,0,1,"unsigned int64",9);
- _cimg_save_pandore_case(0,0,1,"int64",9);
- _cimg_save_pandore_case(0,0,1,"float",10);
- _cimg_save_pandore_case(0,0,1,"double",10);
- _cimg_save_pandore_case(0,1,3,"unsigned char",16);
- _cimg_save_pandore_case(0,1,3,"char",17);
- _cimg_save_pandore_case(0,1,3,"unsigned short",17);
- _cimg_save_pandore_case(0,1,3,"short",17);
- _cimg_save_pandore_case(0,1,3,"unsigned int",17);
- _cimg_save_pandore_case(0,1,3,"int",17);
- _cimg_save_pandore_case(0,1,3,"unsigned int64",17);
- _cimg_save_pandore_case(0,1,3,"int64",17);
- _cimg_save_pandore_case(0,1,3,"float",18);
- _cimg_save_pandore_case(0,1,3,"double",18);
- _cimg_save_pandore_case(0,0,3,"unsigned char",19);
- _cimg_save_pandore_case(0,0,3,"char",20);
- _cimg_save_pandore_case(0,0,3,"unsigned short",20);
- _cimg_save_pandore_case(0,0,3,"short",20);
- _cimg_save_pandore_case(0,0,3,"unsigned int",20);
- _cimg_save_pandore_case(0,0,3,"int",20);
- _cimg_save_pandore_case(0,0,3,"unsigned int64",20);
- _cimg_save_pandore_case(0,0,3,"int64",20);
- _cimg_save_pandore_case(0,0,3,"float",21);
- _cimg_save_pandore_case(0,0,3,"double",21);
- _cimg_save_pandore_case(1,1,0,"unsigned char",22);
- _cimg_save_pandore_case(1,1,0,"char",23);
- _cimg_save_pandore_case(1,1,0,"unsigned short",23);
- _cimg_save_pandore_case(1,1,0,"short",23);
- _cimg_save_pandore_case(1,1,0,"unsigned int",23);
- _cimg_save_pandore_case(1,1,0,"int",23);
- _cimg_save_pandore_case(1,1,0,"unsigned int64",23);
- _cimg_save_pandore_case(1,1,0,"int64",23);
- _cimg_save_pandore_case(1,1,0,"float",25);
- _cimg_save_pandore_case(1,1,0,"double",25);
- _cimg_save_pandore_case(0,1,0,"unsigned char",26);
- _cimg_save_pandore_case(0,1,0,"char",27);
- _cimg_save_pandore_case(0,1,0,"unsigned short",27);
- _cimg_save_pandore_case(0,1,0,"short",27);
- _cimg_save_pandore_case(0,1,0,"unsigned int",27);
- _cimg_save_pandore_case(0,1,0,"int",27);
- _cimg_save_pandore_case(0,1,0,"unsigned int64",27);
- _cimg_save_pandore_case(0,1,0,"int64",27);
- _cimg_save_pandore_case(0,1,0,"float",29);
- _cimg_save_pandore_case(0,1,0,"double",29);
- _cimg_save_pandore_case(0,0,0,"unsigned char",30);
- _cimg_save_pandore_case(0,0,0,"char",31);
- _cimg_save_pandore_case(0,0,0,"unsigned short",31);
- _cimg_save_pandore_case(0,0,0,"short",31);
- _cimg_save_pandore_case(0,0,0,"unsigned int",31);
- _cimg_save_pandore_case(0,0,0,"int",31);
- _cimg_save_pandore_case(0,0,0,"unsigned int64",31);
- _cimg_save_pandore_case(0,0,0,"int64",31);
- _cimg_save_pandore_case(0,0,0,"float",33);
- _cimg_save_pandore_case(0,0,0,"double",33);
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save image as a raw data file.
- /**
- \param filename Filename, as a C-string.
- \param is_multiplexed Tells if the image channels are stored in a multiplexed way (\c true) or not (\c false).
- \note The .raw format does not store the image dimensions in the output file,
- so you have to keep track of them somewhere to be able to read the file correctly afterwards.
- **/
- const CImg<T>& save_raw(const char *const filename, const bool is_multiplexed=false) const {
- return _save_raw(0,filename,is_multiplexed);
- }
- //! Save image as a raw data file \overloading.
- /**
- Same as save_raw(const char *,bool) const
- with a file stream argument instead of a filename string.
- **/
- const CImg<T>& save_raw(std::FILE *const file, const bool is_multiplexed=false) const {
- return _save_raw(file,0,is_multiplexed);
- }
- const CImg<T>& _save_raw(std::FILE *const file, const char *const filename, const bool is_multiplexed) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_raw(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- if (pixel_type()==cimg::type<bool>::string()) { // Boolean data (bitwise)
- ulongT siz;
- const unsigned char *const buf = _bool2uchar(siz,is_multiplexed);
- cimg::fwrite(buf,siz,nfile);
- delete[] buf;
- } else { // Non boolean data
- if (!is_multiplexed || _spectrum==1) cimg::fwrite(_data,size(),nfile); // Non-multiplexed
- else { // Multiplexed
- CImg<T> buf(_spectrum);
- cimg_forXYZ(*this,x,y,z) {
- cimg_forC(*this,c) buf[c] = (*this)(x,y,z,c);
- cimg::fwrite(buf._data,_spectrum,nfile);
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- // Return unsigned char buffer that encodes data of a CImg<bool> instance bitwise.
- // (buffer needs to be deallocated afterwards, with delete[]).
- const unsigned char *_bool2uchar(ulongT &siz, const bool is_multiplexed) const {
- const ulongT _siz = size();
- siz = _siz/8 + (_siz%8?1:0);
- unsigned char *const buf = new unsigned char[siz], *ptrd = buf, val = 0, bit = 0;
- if (!is_multiplexed || _spectrum==1) // Non-multiplexed
- cimg_for(*this,ptrs,T) { (val<<=1)|=(*ptrs?1:0); if (++bit==8) { *(ptrd++) = val; val = bit = 0; }}
- else // Multiplexed
- cimg_forXYZ(*this,x,y,z) cimg_forC(*this,c) {
- (val<<=1)|=((*this)(x,y,z,c)?1:0); if (++bit==8) { *(ptrd++) = val; val = bit = 0; }
- }
- if (bit) *ptrd = val;
- return buf;
- }
- // Fill CImg<T> instance from bitwise data encoded in an unsigned char buffer.
- void _uchar2bool(const unsigned char *buf, const ulongT siz, const bool is_multiplexed) {
- const ulongT S = std::min(siz*8,size());
- const unsigned char *ptrs = buf;
- unsigned char val = 0, mask = 0;
- T *ptrd = _data;
- if (S && (!is_multiplexed || _spectrum==1)) // Non-multiplexed
- for (ulongT off = 0; off<S; ++off) {
- if (!(mask>>=1)) { val = *(ptrs++); mask = 128; }
- *(ptrd++) = (T)((val&mask)?1:0);
- }
- else if (S) { // Multiplexed
- ulongT off = 0;
- for (int z = 0; z<depth() && off<=S; ++z)
- for (int y = 0; y<height() && off<=S; ++y)
- for (int x = 0; x<width() && off<=S; ++x)
- for (int c = 0; c<spectrum() && off<=S; ++c) {
- if (!(mask>>=1)) { val = *(ptrs++); ++off; mask = 128; }
- (*this)(x,y,z,c) = (T)((val&mask)?1:0);
- }
- }
- }
- //! Save image as a .yuv video file.
- /**
- \param filename Filename, as a C-string.
- \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
- \param is_rgb Tells if pixel values of the instance image are RGB-coded (\c true) or YUV-coded (\c false).
- \note Each slice of the instance image is considered to be a single frame of the output video file.
- **/
- const CImg<T>& save_yuv(const char *const filename,
- const unsigned int chroma_subsampling=444,
- const bool is_rgb=true) const {
- CImgList<T>(*this,true).save_yuv(filename,chroma_subsampling,is_rgb);
- return *this;
- }
- //! Save image as a .yuv video file \overloading.
- /**
- Same as save_yuv(const char*,const unsigned int,const bool) const
- with a file stream argument instead of a filename string.
- **/
- const CImg<T>& save_yuv(std::FILE *const file,
- const unsigned int chroma_subsampling=444,
- const bool is_rgb=true) const {
- CImgList<T>(*this,true).save_yuv(file,chroma_subsampling,is_rgb);
- return *this;
- }
- //! Save 3D object as an Object File Format (.off) file.
- /**
- \param filename Filename, as a C-string.
- \param primitives List of 3D object primitives.
- \param colors List of 3D object colors.
- \note
- - Instance image contains the vertices data of the 3D object.
- - Textured, transparent or sphere-shaped primitives cannot be managed by the .off file format.
- Such primitives will be lost or simplified during file saving.
- - The .off file format is <a href="http://people.sc.fsu.edu/~jburkardt/html/off_format.html">described here</a>.
- **/
- template<typename tf, typename tc>
- const CImg<T>& save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
- const char *const filename) const {
- return _save_off(primitives,colors,0,filename);
- }
- //! Save 3D object as an Object File Format (.off) file \overloading.
- /**
- Same as save_off(const CImgList<tf>&,const CImgList<tc>&,const char*) const
- with a file stream argument instead of a filename string.
- **/
- template<typename tf, typename tc>
- const CImg<T>& save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
- std::FILE *const file) const {
- return _save_off(primitives,colors,file,0);
- }
- template<typename tf, typename tc>
- const CImg<T>& _save_off(const CImgList<tf>& primitives, const CImgList<tc>& colors,
- std::FILE *const file, const char *const filename) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimg_instance
- "save_off(): Specified filename is (null).",
- cimg_instance);
- if (is_empty())
- throw CImgInstanceException(_cimg_instance
- "save_off(): Empty instance, for file '%s'.",
- cimg_instance,
- filename?filename:"(FILE*)");
- CImgList<T> opacities;
- CImg<charT> error_message(1024);
- if (!is_object3d(primitives,colors,opacities,true,error_message))
- throw CImgInstanceException(_cimg_instance
- "save_off(): Invalid specified 3D object, for file '%s' (%s).",
- cimg_instance,
- filename?filename:"(FILE*)",error_message.data());
- const CImg<tc> default_color(1,3,1,1,(tc)std::min((int)cimg::type<tc>::max(),200));
- std::FILE *const nfile = file?file:cimg::fopen(filename,"w");
- unsigned int supported_primitives = 0;
- cimglist_for(primitives,l) if (primitives[l].size()!=5) ++supported_primitives;
- std::fprintf(nfile,"OFF\n%u %u %u\n",_width,supported_primitives,3*primitives._width);
- cimg_forX(*this,i) std::fprintf(nfile,"%f %f %f\n",
- (float)((*this)(i,0)),(float)((*this)(i,1)),(float)((*this)(i,2)));
- cimglist_for(primitives,l) {
- const CImg<tc>& color = l<colors.width()?colors[l]:default_color;
- const unsigned int psiz = primitives[l].size(), csiz = color.size();
- const float r = color[0]/255.f, g = (csiz>1?color[1]:r)/255.f, b = (csiz>2?color[2]:g)/255.f;
- switch (psiz) {
- case 1 : std::fprintf(nfile,"1 %u %f %f %f\n",
- (unsigned int)primitives(l,0),r,g,b); break;
- case 2 : std::fprintf(nfile,"2 %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break;
- case 3 : std::fprintf(nfile,"3 %u %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,2),
- (unsigned int)primitives(l,1),r,g,b); break;
- case 4 : std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,3),
- (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),r,g,b); break;
- case 5 : std::fprintf(nfile,"2 %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),r,g,b); break;
- case 6 : {
- const unsigned int xt = (unsigned int)primitives(l,2), yt = (unsigned int)primitives(l,3);
- const float
- rt = color.atXY(xt,yt,0)/255.f,
- gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
- bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
- std::fprintf(nfile,"2 %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,1),rt,gt,bt);
- } break;
- case 9 : {
- const unsigned int xt = (unsigned int)primitives(l,3), yt = (unsigned int)primitives(l,4);
- const float
- rt = color.atXY(xt,yt,0)/255.f,
- gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
- bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
- std::fprintf(nfile,"3 %u %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,2),
- (unsigned int)primitives(l,1),rt,gt,bt);
- } break;
- case 12 : {
- const unsigned int xt = (unsigned int)primitives(l,4), yt = (unsigned int)primitives(l,5);
- const float
- rt = color.atXY(xt,yt,0)/255.f,
- gt = (csiz>1?color.atXY(xt,yt,1):r)/255.f,
- bt = (csiz>2?color.atXY(xt,yt,2):g)/255.f;
- std::fprintf(nfile,"4 %u %u %u %u %f %f %f\n",
- (unsigned int)primitives(l,0),(unsigned int)primitives(l,3),
- (unsigned int)primitives(l,2),(unsigned int)primitives(l,1),rt,gt,bt);
- } break;
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save volumetric image as a video (using the OpenCV library when available).
- /**
- \param filename Filename to write data to.
- \param fps Number of frames per second.
- \param codec Type of compression (See http://www.fourcc.org/codecs.php to see available codecs).
- \param keep_open Tells if the video writer associated to the specified filename
- must be kept open or not (to allow frames to be added in the same file afterwards).
- **/
- const CImg<T>& save_video(const char *const filename, const unsigned int fps=25,
- const char *codec=0, const bool keep_open=false) const {
- if (is_empty()) { CImgList<T>().save_video(filename,fps,codec,keep_open); return *this; }
- CImgList<T> list;
- get_split('z').move_to(list);
- list.save_video(filename,fps,codec,keep_open);
- return *this;
- }
- //! Save volumetric image as a video, using ffmpeg external binary.
- /**
- \param filename Filename, as a C-string.
- \param fps Video framerate.
- \param codec Video codec, as a C-string.
- \param bitrate Video bitrate.
- \note
- - Each slice of the instance image is considered to be a single frame of the output video file.
- - This method uses \c ffmpeg, an external executable binary provided by
- <a href="http://www.ffmpeg.org">FFmpeg</a>.
- It must be installed for the method to succeed.
- **/
- const CImg<T>& save_ffmpeg_external(const char *const filename, const unsigned int fps=25,
- const char *const codec=0, const unsigned int bitrate=2048) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_ffmpeg_external(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- CImgList<T> list;
- get_split('z').move_to(list);
- list.save_ffmpeg_external(filename,fps,codec,bitrate);
- return *this;
- }
- //! Save image using gzip external binary.
- /**
- \param filename Filename, as a C-string.
- \note This method uses \c gzip, an external executable binary provided by
- <a href="//http://www.gzip.org">gzip</a>.
- It must be installed for the method to succeed.
- **/
- const CImg<T>& save_gzip_external(const char *const filename) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_gzip_external(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- CImg<charT> command(1024), filename_tmp(256), body(256);
- const char
- *ext = cimg::split_filename(filename,body),
- *ext2 = cimg::split_filename(body,0);
- std::FILE *file;
- do {
- if (!cimg::strcasecmp(ext,"gz")) {
- if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- } else {
- if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- }
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- save(filename_tmp);
- cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
- cimg::gzip_path(),
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::gzip_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimg_instance
- "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.",
- cimg_instance,
- filename);
- else cimg::fclose(file);
- std::remove(filename_tmp);
- return *this;
- }
- //! Save image using GraphicsMagick's external binary.
- /**
- \param filename Filename, as a C-string.
- \param quality Image quality (expressed in percent), when the file format supports it.
- \note This method uses \c gm, an external executable binary provided by
- <a href="http://www.graphicsmagick.org">GraphicsMagick</a>.
- It must be installed for the method to succeed.
- **/
- const CImg<T>& save_graphicsmagick_external(const char *const filename, const unsigned int quality=100) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_graphicsmagick_external(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_other(): File '%s', saving a volumetric image with an external call to "
- "GraphicsMagick only writes the first image slice.",
- cimg_instance,filename);
- #ifdef cimg_use_png
- #define _cimg_sge_extension1 "png"
- #define _cimg_sge_extension2 "png"
- #else
- #define _cimg_sge_extension1 "pgm"
- #define _cimg_sge_extension2 "ppm"
- #endif
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),
- _spectrum==1?_cimg_sge_extension1:_cimg_sge_extension2);
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- #ifdef cimg_use_png
- save_png(filename_tmp);
- #else
- save_pnm(filename_tmp);
- #endif
- cimg_snprintf(command,command._width,"\"%s\" convert -quality %u \"%s\" \"%s\"",
- cimg::graphicsmagick_path(),quality,
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::graphicsmagick_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimg_instance
- "save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.",
- cimg_instance,
- filename);
- if (file) cimg::fclose(file);
- std::remove(filename_tmp);
- return *this;
- }
- //! Save image using ImageMagick's external binary.
- /**
- \param filename Filename, as a C-string.
- \param quality Image quality (expressed in percent), when the file format supports it.
- \note This method uses \c convert, an external executable binary provided by
- <a href="http://www.imagemagick.org">ImageMagick</a>.
- It must be installed for the method to succeed.
- **/
- const CImg<T>& save_imagemagick_external(const char *const filename, const unsigned int quality=100) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_imagemagick_external(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_other(): File '%s', saving a volumetric image with an external call to "
- "ImageMagick only writes the first image slice.",
- cimg_instance,filename);
- #ifdef cimg_use_png
- #define _cimg_sie_extension1 "png"
- #define _cimg_sie_extension2 "png"
- #else
- #define _cimg_sie_extension1 "pgm"
- #define _cimg_sie_extension2 "ppm"
- #endif
- CImg<charT> command(1024), filename_tmp(256);
- std::FILE *file;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",cimg::temporary_path(),
- cimg_file_separator,cimg::filenamerand(),_spectrum==1?_cimg_sie_extension1:_cimg_sie_extension2);
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- #ifdef cimg_use_png
- save_png(filename_tmp);
- #else
- save_pnm(filename_tmp);
- #endif
- cimg_snprintf(command,command._width,"\"%s\" -quality %u \"%s\" \"%s\"",
- cimg::imagemagick_path(),quality,
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::imagemagick_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimg_instance
- "save_imagemagick_external(): Failed to save file '%s' with "
- "external command 'magick/convert'.",
- cimg_instance,
- filename);
- if (file) cimg::fclose(file);
- std::remove(filename_tmp);
- return *this;
- }
- //! Save image as a Dicom file.
- /**
- \param filename Filename, as a C-string.
- \note This method uses \c medcon, an external executable binary provided by
- <a href="http://xmedcon.sourceforge.net">(X)Medcon</a>.
- It must be installed for the method to succeed.
- **/
- const CImg<T>& save_medcon_external(const char *const filename) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_medcon_external(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- CImg<charT> command(1024), filename_tmp(256), body(256);
- std::FILE *file;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s.hdr",cimg::filenamerand());
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- save_analyze(filename_tmp);
- cimg_snprintf(command,command._width,"\"%s\" -w -c dicom -o \"%s\" -f \"%s\"",
- cimg::medcon_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command, cimg::medcon_path());
- std::remove(filename_tmp);
- cimg::split_filename(filename_tmp,body);
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s.img",body._data);
- std::remove(filename_tmp);
- file = cimg::std_fopen(filename,"rb");
- if (!file) {
- cimg_snprintf(command,command._width,"m000-%s",filename);
- file = cimg::std_fopen(command,"rb");
- if (!file) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimg_instance
- "save_medcon_external(): Failed to save file '%s' with external command 'medcon'.",
- cimg_instance,
- filename);
- }
- }
- cimg::fclose(file);
- std::rename(command,filename);
- return *this;
- }
- // Save image for non natively supported formats.
- /**
- \param filename Filename, as a C-string.
- \param quality Image quality (expressed in percent), when the file format supports it.
- \note
- - The filename extension tells about the desired file format.
- - This method tries to save the instance image as a file, using external tools from
- <a href="http://www.imagemagick.org">ImageMagick</a> or
- <a href="http://www.graphicsmagick.org">GraphicsMagick</a>.
- At least one of these tool must be installed for the method to succeed.
- - It is recommended to use the generic method save(const char*, int) const instead,
- as it can handle some file formats natively.
- **/
- const CImg<T>& save_other(const char *const filename, const unsigned int quality=100) const {
- if (!filename)
- throw CImgArgumentException(_cimg_instance
- "save_other(): Specified filename is (null).",
- cimg_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- if (_depth>1)
- cimg::warn(_cimg_instance
- "save_other(): File '%s', saving a volumetric image with an external call to "
- "ImageMagick or GraphicsMagick only writes the first image slice.",
- cimg_instance,filename);
- const unsigned int omode = cimg::exception_mode();
- bool is_saved = true;
- cimg::exception_mode(0);
- try { save_magick(filename); }
- catch (CImgException&) {
- try { save_imagemagick_external(filename,quality); }
- catch (CImgException&) {
- try { save_graphicsmagick_external(filename,quality); }
- catch (CImgException&) {
- is_saved = false;
- }
- }
- }
- cimg::exception_mode(omode);
- if (!is_saved)
- throw CImgIOException(_cimg_instance
- "save_other(): Failed to save file '%s'. Format is not natively supported, "
- "and no external commands succeeded.",
- cimg_instance,
- filename);
- return *this;
- }
- //! Serialize a CImg<T> instance into a raw CImg<unsigned char> buffer.
- /**
- \param is_compressed tells if zlib compression must be used for serialization
- (this requires 'cimg_use_zlib' been enabled).
- **/
- CImg<ucharT> get_serialize(const bool is_compressed=false) const {
- return CImgList<T>(*this,true).get_serialize(is_compressed);
- }
- // [internal] Return a 40x38 color logo of a 'danger' item.
- static CImg<T> _logo40x38() {
- CImg<T> res(40,38,1,3);
- const unsigned char *ptrs = cimg::logo40x38;
- T *ptr1 = res.data(0,0,0,0), *ptr2 = res.data(0,0,0,1), *ptr3 = res.data(0,0,0,2);
- for (ulongT off = 0; off<(ulongT)res._width*res._height;) {
- const unsigned char n = *(ptrs++), r = *(ptrs++), g = *(ptrs++), b = *(ptrs++);
- for (unsigned int l = 0; l<n; ++off, ++l) { *(ptr1++) = (T)r; *(ptr2++) = (T)g; *(ptr3++) = (T)b; }
- }
- return res;
- }
- //@}
- }; // struct CImg { ...
- /*
- #-----------------------------------------
- #
- #
- #
- # Definition of the CImgList<T> structure
- #
- #
- #
- #------------------------------------------
- */
- //! Represent a list of images CImg<T>.
- template<typename T>
- struct CImgList {
- unsigned int _width, _allocated_width;
- CImg<T> *_data;
- //! Simple iterator type, to loop through each image of a list.
- /**
- \note
- - The \c CImgList<T>::iterator type is defined as a <tt>CImg<T>*</tt>.
- - You may use it like this:
- \code
- CImgList<> list; // Assuming this image list is not empty
- for (CImgList<>::iterator it = list.begin(); it<list.end(); ++it) (*it).mirror('x');
- \endcode
- - Using the loop macro \c cimglist_for is another (more concise) alternative:
- \code
- cimglist_for(list,l) list[l].mirror('x');
- \endcode
- **/
- typedef CImg<T>* iterator;
- //! Simple const iterator type, to loop through each image of a \c const list instance.
- /**
- \note
- - The \c CImgList<T>::const_iterator type is defined to be a <tt>const CImg<T>*</tt>.
- - Similar to CImgList<T>::iterator, but for constant list instances.
- **/
- typedef const CImg<T>* const_iterator;
- //! Pixel value type.
- /**
- Refer to the pixels value type of the images in the list.
- \note
- - The \c CImgList<T>::value_type type of a \c CImgList<T> is defined to be a \c T.
- It is then similar to CImg<T>::value_type.
- - \c CImgList<T>::value_type is actually not used in %CImg methods. It has been mainly defined for
- compatibility with STL naming conventions.
- **/
- typedef T value_type;
- // Define common types related to template type T.
- typedef typename cimg::superset<T,bool>::type Tbool;
- typedef typename cimg::superset<T,unsigned char>::type Tuchar;
- typedef typename cimg::superset<T,char>::type Tchar;
- typedef typename cimg::superset<T,unsigned short>::type Tushort;
- typedef typename cimg::superset<T,short>::type Tshort;
- typedef typename cimg::superset<T,unsigned int>::type Tuint;
- typedef typename cimg::superset<T,int>::type Tint;
- typedef typename cimg::superset<T,cimg_ulong>::type Tulong;
- typedef typename cimg::superset<T,cimg_long>::type Tlong;
- typedef typename cimg::superset<T,float>::type Tfloat;
- typedef typename cimg::superset<T,double>::type Tdouble;
- typedef typename cimg::last<T,bool>::type boolT;
- typedef typename cimg::last<T,unsigned char>::type ucharT;
- typedef typename cimg::last<T,char>::type charT;
- typedef typename cimg::last<T,unsigned short>::type ushortT;
- typedef typename cimg::last<T,short>::type shortT;
- typedef typename cimg::last<T,unsigned int>::type uintT;
- typedef typename cimg::last<T,int>::type intT;
- typedef typename cimg::last<T,cimg_ulong>::type ulongT;
- typedef typename cimg::last<T,cimg_long>::type longT;
- typedef typename cimg::last<T,cimg_uint64>::type uint64T;
- typedef typename cimg::last<T,cimg_int64>::type int64T;
- typedef typename cimg::last<T,float>::type floatT;
- typedef typename cimg::last<T,double>::type doubleT;
- //@}
- //---------------------------
- //
- //! \name Plugins
- //@{
- //---------------------------
- #ifdef cimglist_plugin
- #include cimglist_plugin
- #endif
- #ifdef cimglist_plugin1
- #include cimglist_plugin1
- #endif
- #ifdef cimglist_plugin2
- #include cimglist_plugin2
- #endif
- #ifdef cimglist_plugin3
- #include cimglist_plugin3
- #endif
- #ifdef cimglist_plugin4
- #include cimglist_plugin4
- #endif
- #ifdef cimglist_plugin5
- #include cimglist_plugin5
- #endif
- #ifdef cimglist_plugin6
- #include cimglist_plugin6
- #endif
- #ifdef cimglist_plugin7
- #include cimglist_plugin7
- #endif
- #ifdef cimglist_plugin8
- #include cimglist_plugin8
- #endif
- //@}
- //--------------------------------------------------------
- //
- //! \name Constructors / Destructor / Instance Management
- //@{
- //--------------------------------------------------------
- //! Destructor.
- /**
- Destroy current list instance.
- \note
- - Any allocated buffer is deallocated.
- - Destroying an empty list does nothing actually.
- **/
- ~CImgList() {
- delete[] _data;
- }
- //! Default constructor.
- /**
- Construct a new empty list instance.
- \note
- - An empty list has no pixel data and its dimension width() is set to \c 0, as well as its
- image buffer pointer data().
- - An empty list may be reassigned afterwards, with the family of the assign() methods.
- In all cases, the type of pixels stays \c T.
- **/
- CImgList():
- _width(0),_allocated_width(0),_data(0) {}
- //! Construct list containing empty images.
- /**
- \param n Number of empty images.
- \note Useful when you know by advance the number of images you want to manage, as
- it will allocate the right amount of memory for the list, without needs for reallocation
- (that may occur when starting from an empty list and inserting several images in it).
- **/
- explicit CImgList(const unsigned int n):_width(n) {
- if (n) _data = new CImg<T>[_allocated_width = std::max(16U,(unsigned int)cimg::nearest_pow2(n))];
- else { _allocated_width = 0; _data = 0; }
- }
- //! Construct list containing images of specified size.
- /**
- \param n Number of images.
- \param width Width of images.
- \param height Height of images.
- \param depth Depth of images.
- \param spectrum Number of channels of images.
- \note Pixel values are not initialized and may probably contain garbage.
- **/
- CImgList(const unsigned int n, const unsigned int width, const unsigned int height=1,
- const unsigned int depth=1, const unsigned int spectrum=1):
- _width(0),_allocated_width(0),_data(0) {
- assign(n);
- cimglist_apply(*this,assign)(width,height,depth,spectrum);
- }
- //! Construct list containing images of specified size, and initialize pixel values.
- /**
- \param n Number of images.
- \param width Width of images.
- \param height Height of images.
- \param depth Depth of images.
- \param spectrum Number of channels of images.
- \param val Initialization value for images pixels.
- **/
- CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum, const T& val):
- _width(0),_allocated_width(0),_data(0) {
- assign(n);
- cimglist_apply(*this,assign)(width,height,depth,spectrum,val);
- }
- //! Construct list containing images of specified size, and initialize pixel values from a sequence of integers.
- /**
- \param n Number of images.
- \param width Width of images.
- \param height Height of images.
- \param depth Depth of images.
- \param spectrum Number of channels of images.
- \param val0 First value of the initializing integers sequence.
- \param val1 Second value of the initializing integers sequence.
- \warning You must specify at least <tt>width*height*depth*spectrum</tt> values in your argument list,
- or you will probably segfault.
- **/
- CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...):
- _width(0),_allocated_width(0),_data(0) {
- #define _CImgList_stdarg(t) { \
- assign(n,width,height,depth,spectrum); \
- const ulongT siz = (ulongT)width*height*depth*spectrum, nsiz = siz*n; \
- T *ptrd = _data->_data; \
- va_list ap; \
- va_start(ap,val1); \
- for (ulongT l = 0, s = 0, i = 0; i<nsiz; ++i) { \
- *(ptrd++) = (T)(i==0?val0:(i==1?val1:va_arg(ap,t))); \
- if ((++s)==siz) { ptrd = _data[++l]._data; s = 0; } \
- } \
- va_end(ap); \
- }
- _CImgList_stdarg(int);
- }
- //! Construct list containing images of specified size, and initialize pixel values from a sequence of doubles.
- /**
- \param n Number of images.
- \param width Width of images.
- \param height Height of images.
- \param depth Depth of images.
- \param spectrum Number of channels of images.
- \param val0 First value of the initializing doubles sequence.
- \param val1 Second value of the initializing doubles sequence.
- \warning You must specify at least <tt>width*height*depth*spectrum</tt> values in your argument list,
- or you will probably segfault.
- **/
- CImgList(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum, const double val0, const double val1, ...):
- _width(0),_allocated_width(0),_data(0) {
- _CImgList_stdarg(double);
- }
- //! Construct list containing copies of an input image.
- /**
- \param n Number of images.
- \param img Input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of \c img.
- **/
- template<typename t>
- CImgList(const unsigned int n, const CImg<t>& img, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(n);
- cimglist_apply(*this,assign)(img,is_shared);
- }
- //! Construct list from one image.
- /**
- \param img Input image to copy in the constructed list.
- \param is_shared Tells if the element of the list is a shared or non-shared copy of \c img.
- **/
- template<typename t>
- explicit CImgList(const CImg<t>& img, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(1);
- _data[0].assign(img,is_shared);
- }
- //! Construct list from two images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(2);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared);
- }
- //! Construct list from three images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(3);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- }
- //! Construct list from four images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param img4 Fourth input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3, typename t4>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(4);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared);
- }
- //! Construct list from five images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param img4 Fourth input image to copy in the constructed list.
- \param img5 Fifth input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(5);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared);
- }
- //! Construct list from six images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param img4 Fourth input image to copy in the constructed list.
- \param img5 Fifth input image to copy in the constructed list.
- \param img6 Sixth input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(6);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- }
- //! Construct list from seven images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param img4 Fourth input image to copy in the constructed list.
- \param img5 Fifth input image to copy in the constructed list.
- \param img6 Sixth input image to copy in the constructed list.
- \param img7 Seventh input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(7);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- _data[6].assign(img7,is_shared);
- }
- //! Construct list from eight images.
- /**
- \param img1 First input image to copy in the constructed list.
- \param img2 Second input image to copy in the constructed list.
- \param img3 Third input image to copy in the constructed list.
- \param img4 Fourth input image to copy in the constructed list.
- \param img5 Fifth input image to copy in the constructed list.
- \param img6 Sixth input image to copy in the constructed list.
- \param img7 Seventh input image to copy in the constructed list.
- \param img8 Eighth input image to copy in the constructed list.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7, typename t8>
- CImgList(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const CImg<t8>& img8,
- const bool is_shared=false):
- _width(0),_allocated_width(0),_data(0) {
- assign(8);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared);
- }
- //! Construct list copy.
- /**
- \param list Input list to copy.
- \note The shared state of each element of the constructed list is kept the same as in \c list.
- **/
- template<typename t>
- CImgList(const CImgList<t>& list):_width(0),_allocated_width(0),_data(0) {
- assign(list._width);
- cimglist_for(*this,l) _data[l].assign(list[l],false);
- }
- //! Construct list copy \specialization.
- CImgList(const CImgList<T>& list):_width(0),_allocated_width(0),_data(0) {
- assign(list._width);
- cimglist_for(*this,l) _data[l].assign(list[l],list[l]._is_shared);
- }
- //! Construct list copy, and force the shared state of the list elements.
- /**
- \param list Input list to copy.
- \param is_shared Tells if the elements of the list are shared or non-shared copies of input images.
- **/
- template<typename t>
- CImgList(const CImgList<t>& list, const bool is_shared):_width(0),_allocated_width(0),_data(0) {
- assign(list._width);
- cimglist_for(*this,l) _data[l].assign(list[l],is_shared);
- }
- //! Construct list by reading the content of a file.
- /**
- \param filename Filename, as a C-string.
- **/
- explicit CImgList(const char *const filename):_width(0),_allocated_width(0),_data(0) {
- assign(filename);
- }
- //! Construct list from the content of a display window.
- /**
- \param disp Display window to get content from.
- \note Constructed list contains a single image only.
- **/
- explicit CImgList(const CImgDisplay& disp):_width(0),_allocated_width(0),_data(0) {
- assign(disp);
- }
- //! Return a list with elements being shared copies of images in the list instance.
- /**
- \note <tt>list2 = list1.get_shared()</tt> is equivalent to <tt>list2.assign(list1,true)</tt>.
- **/
- CImgList<T> get_shared() {
- CImgList<T> res(_width);
- cimglist_for(*this,l) res[l].assign(_data[l],true);
- return res;
- }
- //! Return a list with elements being shared copies of images in the list instance \const.
- const CImgList<T> get_shared() const {
- CImgList<T> res(_width);
- cimglist_for(*this,l) res[l].assign(_data[l],true);
- return res;
- }
- //! Destructor \inplace.
- /**
- \see CImgList().
- **/
- CImgList<T>& assign() {
- delete[] _data;
- _width = _allocated_width = 0;
- _data = 0;
- return *this;
- }
- //! Destructor \inplace.
- /**
- Equivalent to assign().
- \note Only here for compatibility with STL naming conventions.
- **/
- CImgList<T>& clear() {
- return assign();
- }
- //! Construct list containing empty images \inplace.
- /**
- \see CImgList(unsigned int).
- **/
- CImgList<T>& assign(const unsigned int n) {
- if (!n) return assign();
- if (_allocated_width<n || _allocated_width>(n<<2)) {
- delete[] _data;
- _data = new CImg<T>[_allocated_width = std::max(16U,(unsigned int)cimg::nearest_pow2(n))];
- }
- _width = n;
- return *this;
- }
- //! Construct list containing images of specified size \inplace.
- /**
- \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int).
- **/
- CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height=1,
- const unsigned int depth=1, const unsigned int spectrum=1) {
- assign(n);
- cimglist_apply(*this,assign)(width,height,depth,spectrum);
- return *this;
- }
- //! Construct list containing images of specified size, and initialize pixel values \inplace.
- /**
- \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const T).
- **/
- CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum, const T& val) {
- assign(n);
- cimglist_apply(*this,assign)(width,height,depth,spectrum,val);
- return *this;
- }
- //! Construct list with images of specified size, and initialize pixel values from a sequence of integers \inplace.
- /**
- \see CImgList(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, const int, const int, ...).
- **/
- CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum, const int val0, const int val1, ...) {
- _CImgList_stdarg(int);
- return *this;
- }
- //! Construct list with images of specified size, and initialize pixel values from a sequence of doubles \inplace.
- /**
- \see CImgList(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,const double,const double,...).
- **/
- CImgList<T>& assign(const unsigned int n, const unsigned int width, const unsigned int height,
- const unsigned int depth, const unsigned int spectrum,
- const double val0, const double val1, ...) {
- _CImgList_stdarg(double);
- return *this;
- }
- //! Construct list containing copies of an input image \inplace.
- /**
- \see CImgList(unsigned int, const CImg<t>&, bool).
- **/
- template<typename t>
- CImgList<T>& assign(const unsigned int n, const CImg<t>& img, const bool is_shared=false) {
- assign(n);
- cimglist_apply(*this,assign)(img,is_shared);
- return *this;
- }
- //! Construct list from one image \inplace.
- /**
- \see CImgList(const CImg<t>&, bool).
- **/
- template<typename t>
- CImgList<T>& assign(const CImg<t>& img, const bool is_shared=false) {
- assign(1);
- _data[0].assign(img,is_shared);
- return *this;
- }
- //! Construct list from two images \inplace.
- /**
- \see CImgList(const CImg<t>&, const CImg<t>&, bool).
- **/
- template<typename t1, typename t2>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const bool is_shared=false) {
- assign(2);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared);
- return *this;
- }
- //! Construct list from three images \inplace.
- /**
- \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const bool is_shared=false) {
- assign(3);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- return *this;
- }
- //! Construct list from four images \inplace.
- /**
- \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3, typename t4>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const bool is_shared=false) {
- assign(4);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared);
- return *this;
- }
- //! Construct list from five images \inplace.
- /**
- \see CImgList(const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const bool is_shared=false) {
- assign(5);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared);
- return *this;
- }
- //! Construct list from six images \inplace.
- /**
- \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const bool is_shared=false) {
- assign(6);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- return *this;
- }
- //! Construct list from seven images \inplace.
- /**
- \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,
- const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const bool is_shared=false) {
- assign(7);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- _data[6].assign(img7,is_shared);
- return *this;
- }
- //! Construct list from eight images \inplace.
- /**
- \see CImgList(const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,const CImg<t>&,
- const CImg<t>&, const CImg<t>&, bool).
- **/
- template<typename t1, typename t2, typename t3, typename t4, typename t5, typename t6, typename t7, typename t8>
- CImgList<T>& assign(const CImg<t1>& img1, const CImg<t2>& img2, const CImg<t3>& img3, const CImg<t4>& img4,
- const CImg<t5>& img5, const CImg<t6>& img6, const CImg<t7>& img7, const CImg<t8>& img8,
- const bool is_shared=false) {
- assign(8);
- _data[0].assign(img1,is_shared); _data[1].assign(img2,is_shared); _data[2].assign(img3,is_shared);
- _data[3].assign(img4,is_shared); _data[4].assign(img5,is_shared); _data[5].assign(img6,is_shared);
- _data[6].assign(img7,is_shared); _data[7].assign(img8,is_shared);
- return *this;
- }
- //! Construct list as a copy of an existing list and force the shared state of the list elements \inplace.
- /**
- \see CImgList(const CImgList<t>&, bool is_shared).
- **/
- template<typename t>
- CImgList<T>& assign(const CImgList<t>& list, const bool is_shared=false) {
- cimg::unused(is_shared);
- assign(list._width);
- cimglist_for(*this,l) _data[l].assign(list[l],false);
- return *this;
- }
- //! Construct list as a copy of an existing list and force shared state of elements \inplace \specialization.
- CImgList<T>& assign(const CImgList<T>& list, const bool is_shared=false) {
- if (this==&list) return *this;
- CImgList<T> res(list._width);
- cimglist_for(res,l) res[l].assign(list[l],is_shared);
- return res.move_to(*this);
- }
- //! Construct list by reading the content of a file \inplace.
- /**
- \see CImgList(const char *const).
- **/
- CImgList<T>& assign(const char *const filename) {
- return load(filename);
- }
- //! Construct list from the content of a display window \inplace.
- /**
- \see CImgList(const CImgDisplay&).
- **/
- CImgList<T>& assign(const CImgDisplay &disp) {
- return assign(CImg<T>(disp));
- }
- //! Transfer the content of the list instance to another list.
- /**
- \param list Destination list.
- \note When returning, the current list instance is empty and the initial content of \c list is destroyed.
- **/
- template<typename t>
- CImgList<t>& move_to(CImgList<t>& list) {
- list.assign(_width);
- bool is_one_shared_element = false;
- cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared;
- if (is_one_shared_element) cimglist_for(*this,l) list[l].assign(_data[l]);
- else cimglist_for(*this,l) _data[l].move_to(list[l]);
- assign();
- return list;
- }
- //! Transfer the content of the list instance at a specified position in another list.
- /**
- \param list Destination list.
- \param pos Index of the insertion in the list.
- \note When returning, the list instance is empty and the initial content of \c list is preserved
- (only images indexes may be modified).
- **/
- template<typename t>
- CImgList<t>& move_to(CImgList<t>& list, const unsigned int pos) {
- if (is_empty()) return list;
- const unsigned int npos = pos>list._width?list._width:pos;
- list.insert(_width,npos);
- bool is_one_shared_element = false;
- cimglist_for(*this,l) is_one_shared_element|=_data[l]._is_shared;
- if (is_one_shared_element) cimglist_for(*this,l) list[npos + l].assign(_data[l]);
- else cimglist_for(*this,l) _data[l].move_to(list[npos + l]);
- assign();
- return list;
- }
- //! Swap all fields between two list instances.
- /**
- \param list List to swap fields with.
- \note Can be used to exchange the content of two lists in a fast way.
- **/
- CImgList<T>& swap(CImgList<T>& list) {
- cimg::swap(_width,list._width,_allocated_width,list._allocated_width);
- cimg::swap(_data,list._data);
- return list;
- }
- //! Return a reference to an empty list.
- /**
- \note Can be used to define default values in a function taking a CImgList<T> as an argument.
- \code
- void f(const CImgList<char>& list=CImgList<char>::empty());
- \endcode
- **/
- static CImgList<T>& empty() {
- static CImgList<T> _empty;
- return _empty.assign();
- }
- //! Return a reference to an empty list \const.
- static const CImgList<T>& const_empty() {
- static const CImgList<T> _empty;
- return _empty;
- }
- //@}
- //------------------------------------------
- //
- //! \name Overloaded Operators
- //@{
- //------------------------------------------
- //! Return a reference to one image element of the list.
- /**
- \param pos Index of the image element.
- **/
- CImg<T>& operator()(const unsigned int pos) {
- #if cimg_verbosity>=3
- if (pos>=_width) {
- cimg::warn(_cimglist_instance
- "operator(): Invalid image request, at position [%u].",
- cimglist_instance,
- pos);
- return *_data;
- }
- #endif
- return _data[pos];
- }
- //! Return a reference to one image of the list.
- /**
- \param pos Index of the image element.
- **/
- const CImg<T>& operator()(const unsigned int pos) const {
- return const_cast<CImgList<T>*>(this)->operator()(pos);
- }
- //! Return a reference to one pixel value of one image of the list.
- /**
- \param pos Index of the image element.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list(n,x,y,z,c)</tt> is equivalent to <tt>list[n](x,y,z,c)</tt>.
- **/
- T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0,
- const unsigned int z=0, const unsigned int c=0) {
- return (*this)[pos](x,y,z,c);
- }
- //! Return a reference to one pixel value of one image of the list \const.
- const T& operator()(const unsigned int pos, const unsigned int x, const unsigned int y=0,
- const unsigned int z=0, const unsigned int c=0) const {
- return (*this)[pos](x,y,z,c);
- }
- //! Return pointer to the first image of the list.
- /**
- \note Images in a list are stored as a buffer of \c CImg<T>.
- **/
- operator CImg<T>*() {
- return _data;
- }
- //! Return pointer to the first image of the list \const.
- operator const CImg<T>*() const {
- return _data;
- }
- //! Construct list from one image \inplace.
- /**
- \param img Input image to copy in the constructed list.
- \note <tt>list = img;</tt> is equivalent to <tt>list.assign(img);</tt>.
- **/
- template<typename t>
- CImgList<T>& operator=(const CImg<t>& img) {
- return assign(img);
- }
- //! Construct list from another list.
- /**
- \param list Input list to copy.
- \note <tt>list1 = list2</tt> is equivalent to <tt>list1.assign(list2);</tt>.
- **/
- template<typename t>
- CImgList<T>& operator=(const CImgList<t>& list) {
- return assign(list);
- }
- //! Construct list from another list \specialization.
- CImgList<T>& operator=(const CImgList<T>& list) {
- return assign(list);
- }
- //! Construct list by reading the content of a file \inplace.
- /**
- \see CImgList(const char *const).
- **/
- CImgList<T>& operator=(const char *const filename) {
- return assign(filename);
- }
- //! Construct list from the content of a display window \inplace.
- /**
- \see CImgList(const CImgDisplay&).
- **/
- CImgList<T>& operator=(const CImgDisplay& disp) {
- return assign(disp);
- }
- //! Return a non-shared copy of a list.
- /**
- \note <tt>+list</tt> is equivalent to <tt>CImgList<T>(list,false)</tt>.
- It forces the copy to have non-shared elements.
- **/
- CImgList<T> operator+() const {
- return CImgList<T>(*this,false);
- }
- //! Return a copy of the list instance, where image \c img has been inserted at the end.
- /**
- \param img Image inserted at the end of the instance copy.
- \note Define a convenient way to create temporary lists of images, as in the following code:
- \code
- (img1,img2,img3,img4).display("My four images");
- \endcode
- **/
- template<typename t>
- CImgList<T>& operator,(const CImg<t>& img) {
- return insert(img);
- }
- //! Return a copy of the list instance, where image \c img has been inserted at the end \const.
- template<typename t>
- CImgList<T> operator,(const CImg<t>& img) const {
- return (+*this).insert(img);
- }
- //! Return a copy of the list instance, where all elements of input list \c list have been inserted at the end.
- /**
- \param list List inserted at the end of the instance copy.
- **/
- template<typename t>
- CImgList<T>& operator,(const CImgList<t>& list) {
- return insert(list);
- }
- //! Return a copy of the list instance, where all elements of input \c list have been inserted at the end \const.
- template<typename t>
- CImgList<T>& operator,(const CImgList<t>& list) const {
- return (+*this).insert(list);
- }
- //! Return image corresponding to the appending of all images of the instance list along specified axis.
- /**
- \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \note <tt>list>'x'</tt> is equivalent to <tt>list.get_append('x')</tt>.
- **/
- CImg<T> operator>(const char axis) const {
- return get_append(axis,0);
- }
- //! Return list corresponding to the splitting of all images of the instance list along specified axis.
- /**
- \param axis Axis used for image splitting.
- \note <tt>list<'x'</tt> is equivalent to <tt>list.get_split('x')</tt>.
- **/
- CImgList<T> operator<(const char axis) const {
- return get_split(axis);
- }
- //@}
- //-------------------------------------
- //
- //! \name Instance Characteristics
- //@{
- //-------------------------------------
- //! Return the type of image pixel values as a C string.
- /**
- Return a \c char* string containing the usual type name of the image pixel values
- (i.e. a stringified version of the template parameter \c T).
- \note
- - The returned string may contain spaces (as in \c "unsigned char").
- - If the pixel type \c T does not correspond to a registered type, the string <tt>"unknown"</tt> is returned.
- **/
- static const char* pixel_type() {
- return cimg::type<T>::string();
- }
- //! Return the size of the list, i.e. the number of images contained in it.
- /**
- \note Similar to size() but returns result as a (signed) integer.
- **/
- int width() const {
- return (int)_width;
- }
- //! Return the size of the list, i.e. the number of images contained in it.
- /**
- \note Similar to width() but returns result as an unsigned integer.
- **/
- unsigned int size() const {
- return _width;
- }
- //! Return pointer to the first image of the list.
- /**
- \note Images in a list are stored as a buffer of \c CImg<T>.
- **/
- CImg<T> *data() {
- return _data;
- }
- //! Return pointer to the first image of the list \const.
- const CImg<T> *data() const {
- return _data;
- }
- //! Return pointer to the pos-th image of the list.
- /**
- \param pos Index of the image element to access.
- \note <tt>list.data(n);</tt> is equivalent to <tt>list.data + n;</tt>.
- **/
- #if cimg_verbosity>=3
- CImg<T> *data(const unsigned int pos) {
- if (pos>=size())
- cimg::warn(_cimglist_instance
- "data(): Invalid pointer request, at position [%u].",
- cimglist_instance,
- pos);
- return _data + pos;
- }
- const CImg<T> *data(const unsigned int l) const {
- return const_cast<CImgList<T>*>(this)->data(l);
- }
- #else
- CImg<T> *data(const unsigned int l) {
- return _data + l;
- }
- //! Return pointer to the pos-th image of the list \const.
- const CImg<T> *data(const unsigned int l) const {
- return _data + l;
- }
- #endif
- //! Return iterator to the first image of the list.
- /**
- **/
- iterator begin() {
- return _data;
- }
- //! Return iterator to the first image of the list \const.
- const_iterator begin() const {
- return _data;
- }
- //! Return iterator to one position after the last image of the list.
- /**
- **/
- iterator end() {
- return _data + _width;
- }
- //! Return iterator to one position after the last image of the list \const.
- const_iterator end() const {
- return _data + _width;
- }
- //! Return reference to the first image of the list.
- /**
- **/
- CImg<T>& front() {
- return *_data;
- }
- //! Return reference to the first image of the list \const.
- const CImg<T>& front() const {
- return *_data;
- }
- //! Return a reference to the last image of the list.
- /**
- **/
- const CImg<T>& back() const {
- return *(_data + _width - 1);
- }
- //! Return a reference to the last image of the list \const.
- CImg<T>& back() {
- return *(_data + _width - 1);
- }
- //! Return pos-th image of the list.
- /**
- \param pos Index of the image element to access.
- **/
- CImg<T>& at(const int pos) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "at(): Empty instance.",
- cimglist_instance);
- return _data[cimg::cut(pos,0,width() - 1)];
- }
- //! Access to pixel value with Dirichlet boundary conditions.
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note <tt>list.atNXYZC(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZC(x,y,z,c);</tt>.
- **/
- T& atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
- return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXYZC(x,y,z,c,out_value);
- }
- //! Access to pixel value with Dirichlet boundary conditions \const.
- T atNXYZC(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
- return (pos<0 || pos>=width())?out_value:_data[pos].atXYZC(x,y,z,c,out_value);
- }
- //! Access to pixel value with Neumann boundary conditions.
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list.atNXYZC(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZC(x,y,z,c);</tt>.
- **/
- T& atNXYZC(const int pos, const int x, const int y, const int z, const int c) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXYZC(): Empty instance.",
- cimglist_instance);
- return _atNXYZC(pos,x,y,z,c);
- }
- //! Access to pixel value with Neumann boundary conditions \const.
- T atNXYZC(const int pos, const int x, const int y, const int z, const int c) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXYZC(): Empty instance.",
- cimglist_instance);
- return _atNXYZC(pos,x,y,z,c);
- }
- T& _atNXYZC(const int pos, const int x, const int y, const int z, const int c) {
- return _data[cimg::cut(pos,0,width() - 1)].atXYZC(x,y,z,c);
- }
- T _atNXYZC(const int pos, const int x, const int y, const int z, const int c) const {
- return _data[cimg::cut(pos,0,width() - 1)].atXYZC(x,y,z,c);
- }
- //! Access pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y,\c z).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
- return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXYZ(x,y,z,c,out_value);
- }
- //! Access pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y,\c z) \const.
- T atNXYZ(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
- return (pos<0 || pos>=width())?out_value:_data[pos].atXYZ(x,y,z,c,out_value);
- }
- //! Access to pixel value with Neumann boundary conditions for the 4 coordinates (\c pos, \c x,\c y,\c z).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXYZ(): Empty instance.",
- cimglist_instance);
- return _atNXYZ(pos,x,y,z,c);
- }
- //! Access to pixel value with Neumann boundary conditions for the 4 coordinates (\c pos, \c x,\c y,\c z) \const.
- T atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXYZ(): Empty instance.",
- cimglist_instance);
- return _atNXYZ(pos,x,y,z,c);
- }
- T& _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) {
- return _data[cimg::cut(pos,0,width() - 1)].atXYZ(x,y,z,c);
- }
- T _atNXYZ(const int pos, const int x, const int y, const int z, const int c=0) const {
- return _data[cimg::cut(pos,0,width() - 1)].atXYZ(x,y,z,c);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNXY(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
- return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atXY(x,y,z,c,out_value);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the 3 coordinates (\c pos, \c x,\c y) \const.
- T atNXY(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
- return (pos<0 || pos>=width())?out_value:_data[pos].atXY(x,y,z,c,out_value);
- }
- //! Access to pixel value with Neumann boundary conditions for the 3 coordinates (\c pos, \c x,\c y).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXY(): Empty instance.",
- cimglist_instance);
- return _atNXY(pos,x,y,z,c);
- }
- //! Access to pixel value with Neumann boundary conditions for the 3 coordinates (\c pos, \c x,\c y) \const.
- T atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNXY(): Empty instance.",
- cimglist_instance);
- return _atNXY(pos,x,y,z,c);
- }
- T& _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) {
- return _data[cimg::cut(pos,0,width() - 1)].atXY(x,y,z,c);
- }
- T _atNXY(const int pos, const int x, const int y, const int z=0, const int c=0) const {
- return _data[cimg::cut(pos,0,width() - 1)].atXY(x,y,z,c);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the 2 coordinates (\c pos,\c x).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNX(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
- return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):_data[pos].atX(x,y,z,c,out_value);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the 2 coordinates (\c pos,\c x) \const.
- T atNX(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
- return (pos<0 || pos>=width())?out_value:_data[pos].atX(x,y,z,c,out_value);
- }
- //! Access to pixel value with Neumann boundary conditions for the 2 coordinates (\c pos, \c x).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNX(): Empty instance.",
- cimglist_instance);
- return _atNX(pos,x,y,z,c);
- }
- //! Access to pixel value with Neumann boundary conditions for the 2 coordinates (\c pos, \c x) \const.
- T atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atNX(): Empty instance.",
- cimglist_instance);
- return _atNX(pos,x,y,z,c);
- }
- T& _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) {
- return _data[cimg::cut(pos,0,width() - 1)].atX(x,y,z,c);
- }
- T _atNX(const int pos, const int x, const int y=0, const int z=0, const int c=0) const {
- return _data[cimg::cut(pos,0,width() - 1)].atX(x,y,z,c);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the coordinate (\c pos).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \param out_value Default value returned if \c offset is outside image bounds.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atN(const int pos, const int x, const int y, const int z, const int c, const T& out_value) {
- return (pos<0 || pos>=width())?(cimg::temporary(out_value)=out_value):(*this)(pos,x,y,z,c);
- }
- //! Access to pixel value with Dirichlet boundary conditions for the coordinate (\c pos) \const.
- T atN(const int pos, const int x, const int y, const int z, const int c, const T& out_value) const {
- return (pos<0 || pos>=width())?out_value:(*this)(pos,x,y,z,c);
- }
- //! Return pixel value with Neumann boundary conditions for the coordinate (\c pos).
- /**
- \param pos Index of the image element to access.
- \param x X-coordinate of the pixel value.
- \param y Y-coordinate of the pixel value.
- \param z Z-coordinate of the pixel value.
- \param c C-coordinate of the pixel value.
- \note <tt>list.atNXYZ(p,x,y,z,c);</tt> is equivalent to <tt>list[p].atXYZ(x,y,z,c);</tt>.
- **/
- T& atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atN(): Empty instance.",
- cimglist_instance);
- return _atN(pos,x,y,z,c);
- }
- //! Return pixel value with Neumann boundary conditions for the coordinate (\c pos) \const.
- T atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "atN(): Empty instance.",
- cimglist_instance);
- return _atN(pos,x,y,z,c);
- }
- T& _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) {
- return _data[cimg::cut(pos,0,width() - 1)](x,y,z,c);
- }
- T _atN(const int pos, const int x=0, const int y=0, const int z=0, const int c=0) const {
- return _data[cimg::cut(pos,0,width() - 1)](x,y,z,c);
- }
- //@}
- //-------------------------------------
- //
- //! \name Instance Checking
- //@{
- //-------------------------------------
- //! Return \c true if list is empty.
- /**
- **/
- bool is_empty() const {
- return (!_data || !_width);
- }
- //! Test if number of image elements is equal to specified value.
- /**
- \param size_n Number of image elements to test.
- **/
- bool is_sameN(const unsigned int size_n) const {
- return _width==size_n;
- }
- //! Test if number of image elements is equal between two images lists.
- /**
- \param list Input list to compare with.
- **/
- template<typename t>
- bool is_sameN(const CImgList<t>& list) const {
- return is_sameN(list._width);
- }
- // Define useful functions to check list dimensions.
- // (cannot be documented because macro-generated).
- #define _cimglist_def_is_same1(axis) \
- bool is_same##axis(const unsigned int val) const { \
- bool res = true; \
- for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(val); \
- return res; \
- } \
- bool is_sameN##axis(const unsigned int n, const unsigned int val) const { \
- return is_sameN(n) && is_same##axis(val); \
- } \
- #define _cimglist_def_is_same2(axis1,axis2) \
- bool is_same##axis1##axis2(const unsigned int val1, const unsigned int val2) const { \
- bool res = true; \
- for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2(val1,val2); \
- return res; \
- } \
- bool is_sameN##axis1##axis2(const unsigned int n, const unsigned int val1, const unsigned int val2) const { \
- return is_sameN(n) && is_same##axis1##axis2(val1,val2); \
- } \
- #define _cimglist_def_is_same3(axis1,axis2,axis3) \
- bool is_same##axis1##axis2##axis3(const unsigned int val1, const unsigned int val2, \
- const unsigned int val3) const { \
- bool res = true; \
- for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis1##axis2##axis3(val1,val2,val3); \
- return res; \
- } \
- bool is_sameN##axis1##axis2##axis3(const unsigned int n, const unsigned int val1, \
- const unsigned int val2, const unsigned int val3) const { \
- return is_sameN(n) && is_same##axis1##axis2##axis3(val1,val2,val3); \
- } \
- #define _cimglist_def_is_same(axis) \
- template<typename t> bool is_same##axis(const CImg<t>& img) const { \
- bool res = true; \
- for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_same##axis(img); \
- return res; \
- } \
- template<typename t> bool is_same##axis(const CImgList<t>& list) const { \
- const unsigned int lmin = std::min(_width,list._width); \
- bool res = true; \
- for (unsigned int l = 0; l<lmin && res; ++l) res = _data[l].is_same##axis(list[l]); \
- return res; \
- } \
- template<typename t> bool is_sameN##axis(const unsigned int n, const CImg<t>& img) const { \
- return (is_sameN(n) && is_same##axis(img)); \
- } \
- template<typename t> bool is_sameN##axis(const CImgList<t>& list) const { \
- return (is_sameN(list) && is_same##axis(list)); \
- }
- _cimglist_def_is_same(XY)
- _cimglist_def_is_same(XZ)
- _cimglist_def_is_same(XC)
- _cimglist_def_is_same(YZ)
- _cimglist_def_is_same(YC)
- _cimglist_def_is_same(XYZ)
- _cimglist_def_is_same(XYC)
- _cimglist_def_is_same(YZC)
- _cimglist_def_is_same(XYZC)
- _cimglist_def_is_same1(X)
- _cimglist_def_is_same1(Y)
- _cimglist_def_is_same1(Z)
- _cimglist_def_is_same1(C)
- _cimglist_def_is_same2(X,Y)
- _cimglist_def_is_same2(X,Z)
- _cimglist_def_is_same2(X,C)
- _cimglist_def_is_same2(Y,Z)
- _cimglist_def_is_same2(Y,C)
- _cimglist_def_is_same2(Z,C)
- _cimglist_def_is_same3(X,Y,Z)
- _cimglist_def_is_same3(X,Y,C)
- _cimglist_def_is_same3(X,Z,C)
- _cimglist_def_is_same3(Y,Z,C)
- //! Test if dimensions of each image of the list match specified arguments.
- /**
- \param dx Checked image width.
- \param dy Checked image height.
- \param dz Checked image depth.
- \param dc Checked image spectrum.
- **/
- bool is_sameXYZC(const unsigned int dx, const unsigned int dy,
- const unsigned int dz, const unsigned int dc) const {
- bool res = true;
- for (unsigned int l = 0; l<_width && res; ++l) res = _data[l].is_sameXYZC(dx,dy,dz,dc);
- return res;
- }
- //! Test if list dimensions match specified arguments.
- /**
- \param n Number of images in the list.
- \param dx Checked image width.
- \param dy Checked image height.
- \param dz Checked image depth.
- \param dc Checked image spectrum.
- **/
- bool is_sameNXYZC(const unsigned int n,
- const unsigned int dx, const unsigned int dy,
- const unsigned int dz, const unsigned int dc) const {
- return is_sameN(n) && is_sameXYZC(dx,dy,dz,dc);
- }
- //! Test if list contains one particular pixel location.
- /**
- \param n Index of the image whom checked pixel value belong to.
- \param x X-coordinate of the checked pixel value.
- \param y Y-coordinate of the checked pixel value.
- \param z Z-coordinate of the checked pixel value.
- \param c C-coordinate of the checked pixel value.
- **/
- bool containsNXYZC(const int n, const int x=0, const int y=0, const int z=0, const int c=0) const {
- if (is_empty()) return false;
- return n>=0 && n<width() && x>=0 && x<_data[n].width() && y>=0 && y<_data[n].height() &&
- z>=0 && z<_data[n].depth() && c>=0 && c<_data[n].spectrum();
- }
- //! Test if list contains image with specified index.
- /**
- \param n Index of the checked image.
- **/
- bool containsN(const int n) const {
- if (is_empty()) return false;
- return n>=0 && n<width();
- }
- //! Test if one image of the list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- \param[out] n Index of image containing the pixel value, if test succeeds.
- \param[out] x X-coordinate of the pixel value, if test succeeds.
- \param[out] y Y-coordinate of the pixel value, if test succeeds.
- \param[out] z Z-coordinate of the pixel value, if test succeeds.
- \param[out] c C-coordinate of the pixel value, if test succeeds.
- \note If true, set coordinates (n,x,y,z,c).
- **/
- template<typename t>
- bool contains(const T& pixel, t& n, t& x, t&y, t& z, t& c) const {
- if (is_empty()) return false;
- cimglist_for(*this,l) if (_data[l].contains(pixel,x,y,z,c)) { n = (t)l; return true; }
- return false;
- }
- //! Test if one of the image list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- \param[out] n Index of image containing the pixel value, if test succeeds.
- \param[out] x X-coordinate of the pixel value, if test succeeds.
- \param[out] y Y-coordinate of the pixel value, if test succeeds.
- \param[out] z Z-coordinate of the pixel value, if test succeeds.
- \note If true, set coordinates (n,x,y,z).
- **/
- template<typename t>
- bool contains(const T& pixel, t& n, t& x, t&y, t& z) const {
- t c;
- return contains(pixel,n,x,y,z,c);
- }
- //! Test if one of the image list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- \param[out] n Index of image containing the pixel value, if test succeeds.
- \param[out] x X-coordinate of the pixel value, if test succeeds.
- \param[out] y Y-coordinate of the pixel value, if test succeeds.
- \note If true, set coordinates (n,x,y).
- **/
- template<typename t>
- bool contains(const T& pixel, t& n, t& x, t&y) const {
- t z, c;
- return contains(pixel,n,x,y,z,c);
- }
- //! Test if one of the image list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- \param[out] n Index of image containing the pixel value, if test succeeds.
- \param[out] x X-coordinate of the pixel value, if test succeeds.
- \note If true, set coordinates (n,x).
- **/
- template<typename t>
- bool contains(const T& pixel, t& n, t& x) const {
- t y, z, c;
- return contains(pixel,n,x,y,z,c);
- }
- //! Test if one of the image list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- \param[out] n Index of image containing the pixel value, if test succeeds.
- \note If true, set coordinates (n).
- **/
- template<typename t>
- bool contains(const T& pixel, t& n) const {
- t x, y, z, c;
- return contains(pixel,n,x,y,z,c);
- }
- //! Test if one of the image list contains the specified referenced value.
- /**
- \param pixel Reference to pixel value to test.
- **/
- bool contains(const T& pixel) const {
- unsigned int n, x, y, z, c;
- return contains(pixel,n,x,y,z,c);
- }
- //! Test if the list contains the image 'img'.
- /**
- \param img Reference to image to test.
- \param[out] n Index of image in the list, if test succeeds.
- \note If true, returns the position (n) of the image in the list.
- **/
- template<typename t>
- bool contains(const CImg<T>& img, t& n) const {
- if (is_empty()) return false;
- const CImg<T> *const ptr = &img;
- cimglist_for(*this,i) if (_data + i==ptr) { n = (t)i; return true; }
- return false;
- }
- //! Test if the list contains the image img.
- /**
- \param img Reference to image to test.
- **/
- bool contains(const CImg<T>& img) const {
- unsigned int n;
- return contains(img,n);
- }
- //@}
- //-------------------------------------
- //
- //! \name Mathematical Functions
- //@{
- //-------------------------------------
- //! Return a reference to the minimum pixel value of the instance list.
- /**
- **/
- T& min() {
- bool is_all_empty = true;
- T *ptr_min = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_min = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "min(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_min;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
- }
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value of the instance list \const.
- const T& min() const {
- bool is_all_empty = true;
- T *ptr_min = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_min = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "min(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_min;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) if (*ptrs<min_value) min_value = *(ptr_min=ptrs);
- }
- return *ptr_min;
- }
- //! Return a reference to the maximum pixel value of the instance list.
- /**
- **/
- T& max() {
- bool is_all_empty = true;
- T *ptr_max = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_max = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "max(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T max_value = *ptr_max;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
- }
- return *ptr_max;
- }
- //! Return a reference to the maximum pixel value of the instance list \const.
- const T& max() const {
- bool is_all_empty = true;
- T *ptr_max = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_max = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "max(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T max_value = *ptr_max;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) if (*ptrs>max_value) max_value = *(ptr_max=ptrs);
- }
- return *ptr_max;
- }
- //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well.
- /**
- \param[out] max_val Value of the maximum value found.
- **/
- template<typename t>
- T& min_max(t& max_val) {
- bool is_all_empty = true;
- T *ptr_min = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_min = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "min_max(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_min, max_value = min_value;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) {
- const T val = *ptrs;
- if (val<min_value) { min_value = val; ptr_min = ptrs; }
- if (val>max_value) max_value = val;
- }
- }
- max_val = (t)max_value;
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value of the instance list and return the maximum vvalue as well \const.
- /**
- \param[out] max_val Value of the maximum value found.
- **/
- template<typename t>
- const T& min_max(t& max_val) const {
- bool is_all_empty = true;
- T *ptr_min = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_min = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "min_max(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_min, max_value = min_value;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) {
- const T val = *ptrs;
- if (val<min_value) { min_value = val; ptr_min = ptrs; }
- if (val>max_value) max_value = val;
- }
- }
- max_val = (t)max_value;
- return *ptr_min;
- }
- //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well.
- /**
- \param[out] min_val Value of the minimum value found.
- **/
- template<typename t>
- T& max_min(t& min_val) {
- bool is_all_empty = true;
- T *ptr_max = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_max = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "max_min(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_max, max_value = min_value;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) {
- const T val = *ptrs;
- if (val>max_value) { max_value = val; ptr_max = ptrs; }
- if (val<min_value) min_value = val;
- }
- }
- min_val = (t)min_value;
- return *ptr_max;
- }
- //! Return a reference to the minimum pixel value of the instance list and return the minimum value as well \const.
- template<typename t>
- const T& max_min(t& min_val) const {
- bool is_all_empty = true;
- T *ptr_max = 0;
- cimglist_for(*this,l) if (!_data[l].is_empty()) {
- ptr_max = _data[l]._data;
- is_all_empty = false;
- break;
- }
- if (is_all_empty)
- throw CImgInstanceException(_cimglist_instance
- "max_min(): %s.",
- _data?"List of empty images":"Empty instance",
- cimglist_instance);
- T min_value = *ptr_max, max_value = min_value;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_for(img,ptrs,T) {
- const T val = *ptrs;
- if (val>max_value) { max_value = val; ptr_max = ptrs; }
- if (val<min_value) min_value = val;
- }
- }
- min_val = (t)min_value;
- return *ptr_max;
- }
- //@}
- //---------------------------
- //
- //! \name List Manipulation
- //@{
- //---------------------------
- //! Insert a copy of the image \c img into the current image list, at position \c pos.
- /**
- \param img Image to insert a copy to the list.
- \param pos Index of the insertion.
- \param is_shared Tells if the inserted image is a shared copy of \c img or not.
- **/
- template<typename t>
- CImgList<T>& insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) {
- const unsigned int npos = pos==~0U?_width:pos;
- if (npos>_width)
- throw CImgArgumentException(_cimglist_instance
- "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) "
- "at position %u.",
- cimglist_instance,
- img._width,img._height,img._depth,img._spectrum,img._data,npos);
- if (is_shared)
- throw CImgArgumentException(_cimglist_instance
- "insert(): Invalid insertion request of specified shared image "
- "CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).",
- cimglist_instance,
- img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos);
- CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):
- (_allocated_width=16)]:0;
- if (!_data) { // Insert new element into empty list
- _data = new_data;
- *_data = img;
- } else {
- if (new_data) { // Insert with re-allocation
- if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);
- if (npos!=_width - 1)
- std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
- std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));
- delete[] _data;
- _data = new_data;
- } else if (npos!=_width - 1) // Insert without re-allocation
- std::memmove((void*)(_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
- _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0;
- _data[npos]._data = 0;
- _data[npos] = img;
- }
- return *this;
- }
- //! Insert a copy of the image \c img into the current image list, at position \c pos \specialization.
- CImgList<T>& insert(const CImg<T>& img, const unsigned int pos=~0U, const bool is_shared=false) {
- const unsigned int npos = pos==~0U?_width:pos;
- if (npos>_width)
- throw CImgArgumentException(_cimglist_instance
- "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) "
- "at position %u.",
- cimglist_instance,
- img._width,img._height,img._depth,img._spectrum,img._data,npos);
- CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):
- (_allocated_width=16)]:0;
- if (!_data) { // Insert new element into empty list
- _data = new_data;
- if (is_shared && img) {
- _data->_width = img._width;
- _data->_height = img._height;
- _data->_depth = img._depth;
- _data->_spectrum = img._spectrum;
- _data->_is_shared = true;
- _data->_data = img._data;
- } else *_data = img;
- }
- else {
- if (new_data) { // Insert with re-allocation
- if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);
- if (npos!=_width - 1)
- std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
- if (is_shared && img) {
- new_data[npos]._width = img._width;
- new_data[npos]._height = img._height;
- new_data[npos]._depth = img._depth;
- new_data[npos]._spectrum = img._spectrum;
- new_data[npos]._is_shared = true;
- new_data[npos]._data = img._data;
- } else {
- new_data[npos]._width = new_data[npos]._height = new_data[npos]._depth = new_data[npos]._spectrum = 0;
- new_data[npos]._data = 0;
- new_data[npos] = img;
- }
- std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));
- delete[] _data;
- _data = new_data;
- } else { // Insert without re-allocation
- if (npos!=_width - 1)
- std::memmove((void*)(_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
- if (is_shared && img) {
- _data[npos]._width = img._width;
- _data[npos]._height = img._height;
- _data[npos]._depth = img._depth;
- _data[npos]._spectrum = img._spectrum;
- _data[npos]._is_shared = true;
- _data[npos]._data = img._data;
- } else {
- _data[npos]._width = _data[npos]._height = _data[npos]._depth = _data[npos]._spectrum = 0;
- _data[npos]._data = 0;
- _data[npos] = img;
- }
- }
- }
- return *this;
- }
- //! Insert a copy of the image \c img into the current image list, at position \c pos \newinstance.
- template<typename t>
- CImgList<T> get_insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) const {
- return (+*this).insert(img,pos,is_shared);
- }
- //! Insert n empty images img into the current image list, at position \p pos.
- /**
- \param n Number of empty images to insert.
- \param pos Index of the insertion.
- **/
- CImgList<T>& insert(const unsigned int n, const unsigned int pos=~0U) {
- CImg<T> empty;
- if (!n) return *this;
- const unsigned int npos = pos==~0U?_width:pos;
- for (unsigned int i = 0; i<n; ++i) insert(empty,npos+i);
- return *this;
- }
- //! Insert n empty images img into the current image list, at position \p pos \newinstance.
- CImgList<T> get_insert(const unsigned int n, const unsigned int pos=~0U) const {
- return (+*this).insert(n,pos);
- }
- //! Insert \c n copies of the image \c img into the current image list, at position \c pos.
- /**
- \param n Number of image copies to insert.
- \param img Image to insert by copy.
- \param pos Index of the insertion.
- \param is_shared Tells if inserted images are shared copies of \c img or not.
- **/
- template<typename t>
- CImgList<T>& insert(const unsigned int n, const CImg<t>& img, const unsigned int pos=~0U,
- const bool is_shared=false) {
- if (!n) return *this;
- const unsigned int npos = pos==~0U?_width:pos;
- insert(img,npos,is_shared);
- for (unsigned int i = 1; i<n; ++i) insert(_data[npos],npos + i,is_shared);
- return *this;
- }
- //! Insert \c n copies of the image \c img into the current image list, at position \c pos \newinstance.
- template<typename t>
- CImgList<T> get_insert(const unsigned int n, const CImg<t>& img, const unsigned int pos=~0U,
- const bool is_shared=false) const {
- return (+*this).insert(n,img,pos,is_shared);
- }
- //! Insert a copy of the image list \c list into the current image list, starting from position \c pos.
- /**
- \param list Image list to insert.
- \param pos Index of the insertion.
- \param is_shared Tells if inserted images are shared copies of images of \c list or not.
- **/
- template<typename t>
- CImgList<T>& insert(const CImgList<t>& list, const unsigned int pos=~0U, const bool is_shared=false) {
- const unsigned int npos = pos==~0U?_width:pos;
- if ((void*)this!=(void*)&list) cimglist_for(list,l) insert(list[l],npos + l,is_shared);
- else insert(CImgList<T>(list),npos,is_shared);
- return *this;
- }
- //! Insert a copy of the image list \c list into the current image list, starting from position \c pos \newinstance.
- template<typename t>
- CImgList<T> get_insert(const CImgList<t>& list, const unsigned int pos=~0U, const bool is_shared=false) const {
- return (+*this).insert(list,pos,is_shared);
- }
- //! Insert n copies of the list \c list at position \c pos of the current list.
- /**
- \param n Number of list copies to insert.
- \param list Image list to insert.
- \param pos Index of the insertion.
- \param is_shared Tells if inserted images are shared copies of images of \c list or not.
- **/
- template<typename t>
- CImgList<T>& insert(const unsigned int n, const CImgList<t>& list, const unsigned int pos=~0U,
- const bool is_shared=false) {
- if (!n) return *this;
- const unsigned int npos = pos==~0U?_width:pos;
- for (unsigned int i = 0; i<n; ++i) insert(list,npos,is_shared);
- return *this;
- }
- //! Insert n copies of the list \c list at position \c pos of the current list \newinstance.
- template<typename t>
- CImgList<T> get_insert(const unsigned int n, const CImgList<t>& list, const unsigned int pos=~0U,
- const bool is_shared=false) const {
- return (+*this).insert(n,list,pos,is_shared);
- }
- //! Remove all images between from indexes.
- /**
- \param pos1 Starting index of the removal.
- \param pos2 Ending index of the removal.
- **/
- CImgList<T>& remove(const unsigned int pos1, const unsigned int pos2) {
- const unsigned int
- npos1 = pos1<pos2?pos1:pos2,
- tpos2 = pos1<pos2?pos2:pos1,
- npos2 = tpos2<_width?tpos2:_width - 1;
- if (npos1>=_width)
- throw CImgArgumentException(_cimglist_instance
- "remove(): Invalid remove request at positions %u->%u.",
- cimglist_instance,
- npos1,tpos2);
- else {
- if (tpos2>=_width)
- throw CImgArgumentException(_cimglist_instance
- "remove(): Invalid remove request at positions %u->%u.",
- cimglist_instance,
- npos1,tpos2);
- for (unsigned int k = npos1; k<=npos2; ++k) _data[k].assign();
- const unsigned int nb = 1 + npos2 - npos1;
- if (!(_width-=nb)) return assign();
- if (_width>(_allocated_width>>4) || _allocated_width<=16) { // Removing items without reallocation
- if (npos1!=_width)
- std::memmove((void*)(_data + npos1),(void*)(_data + npos2 + 1),sizeof(CImg<T>)*(_width - npos1));
- std::memset((void*)(_data + _width),0,sizeof(CImg<T>)*nb);
- } else { // Removing items with reallocation
- _allocated_width>>=4;
- while (_allocated_width>16 && _width<(_allocated_width>>1)) _allocated_width>>=1;
- CImg<T> *const new_data = new CImg<T>[_allocated_width];
- if (npos1) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos1);
- if (npos1!=_width)
- std::memcpy((void*)(new_data + npos1),(void*)(_data + npos2 + 1),sizeof(CImg<T>)*(_width - npos1));
- if (_width!=_allocated_width)
- std::memset((void*)(new_data + _width),0,sizeof(CImg<T>)*(_allocated_width - _width));
- std::memset((void*)_data,0,sizeof(CImg<T>)*(_width + nb));
- delete[] _data;
- _data = new_data;
- }
- }
- return *this;
- }
- //! Remove all images between from indexes \newinstance.
- CImgList<T> get_remove(const unsigned int pos1, const unsigned int pos2) const {
- return (+*this).remove(pos1,pos2);
- }
- //! Remove image at index \c pos from the image list.
- /**
- \param pos Index of the image to remove.
- **/
- CImgList<T>& remove(const unsigned int pos) {
- return remove(pos,pos);
- }
- //! Remove image at index \c pos from the image list \newinstance.
- CImgList<T> get_remove(const unsigned int pos) const {
- return (+*this).remove(pos);
- }
- //! Remove last image.
- /**
- **/
- CImgList<T>& remove() {
- return remove(_width - 1);
- }
- //! Remove last image \newinstance.
- CImgList<T> get_remove() const {
- return (+*this).remove();
- }
- //! Reverse list order.
- CImgList<T>& reverse() {
- for (unsigned int l = 0; l<_width/2; ++l) (*this)[l].swap((*this)[_width - 1 - l]);
- return *this;
- }
- //! Reverse list order \newinstance.
- CImgList<T> get_reverse() const {
- return (+*this).reverse();
- }
- //! Return a sublist.
- /**
- \param pos0 Starting index of the sublist.
- \param pos1 Ending index of the sublist.
- **/
- CImgList<T>& images(const unsigned int pos0, const unsigned int pos1) {
- return get_images(pos0,pos1).move_to(*this);
- }
- //! Return a sublist \newinstance.
- CImgList<T> get_images(const unsigned int pos0, const unsigned int pos1) const {
- if (pos0>pos1 || pos1>=_width)
- throw CImgArgumentException(_cimglist_instance
- "images(): Specified sub-list indices (%u->%u) are out of bounds.",
- cimglist_instance,
- pos0,pos1);
- CImgList<T> res(pos1 - pos0 + 1);
- cimglist_for(res,l) res[l].assign(_data[pos0 + l]);
- return res;
- }
- //! Return a shared sublist.
- /**
- \param pos0 Starting index of the sublist.
- \param pos1 Ending index of the sublist.
- **/
- CImgList<T> get_shared_images(const unsigned int pos0, const unsigned int pos1) {
- if (pos0>pos1 || pos1>=_width)
- throw CImgArgumentException(_cimglist_instance
- "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.",
- cimglist_instance,
- pos0,pos1);
- CImgList<T> res(pos1 - pos0 + 1);
- cimglist_for(res,l) res[l].assign(_data[pos0 + l],_data[pos0 + l]?true:false);
- return res;
- }
- //! Return a shared sublist \newinstance.
- const CImgList<T> get_shared_images(const unsigned int pos0, const unsigned int pos1) const {
- if (pos0>pos1 || pos1>=_width)
- throw CImgArgumentException(_cimglist_instance
- "get_shared_images(): Specified sub-list indices (%u->%u) are out of bounds.",
- cimglist_instance,
- pos0,pos1);
- CImgList<T> res(pos1 - pos0 + 1);
- cimglist_for(res,l) res[l].assign(_data[pos0 + l],_data[pos0 + l]?true:false);
- return res;
- }
- //! Return a single image which is the appending of all images of the current CImgList instance.
- /**
- \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- **/
- CImg<T> get_append(const char axis, const float align=0) const {
- if (is_empty()) return CImg<T>();
- if (_width==1) return +((*this)[0]);
- unsigned int dx = 0, dy = 0, dz = 0, dc = 0, pos = 0;
- CImg<T> res;
- switch (cimg::lowercase(axis)) {
- case 'x' : { // Along the X-axis
- cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- dx+=img._width;
- dy = std::max(dy,img._height);
- dz = std::max(dz,img._depth);
- dc = std::max(dc,img._spectrum);
- }
- }
- res.assign(dx,dy,dz,dc,(T)0);
- if (res) cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- if (img._height==1 && img._depth==1 && img._spectrum==1 &&
- res._height==1 && res._depth==1 && res._spectrum==1)
- std::memcpy(&res[pos],img._data,sizeof(T)*img._width);
- else
- res.draw_image(pos,
- (int)(align*(dy - img._height)),
- (int)(align*(dz - img._depth)),
- (int)(align*(dc - img._spectrum)),
- img);
- }
- pos+=img._width;
- }
- } break;
- case 'y' : { // Along the Y-axis
- cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- dx = std::max(dx,img._width);
- dy+=img._height;
- dz = std::max(dz,img._depth);
- dc = std::max(dc,img._spectrum);
- }
- }
- res.assign(dx,dy,dz,dc,(T)0);
- if (res) cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- if (img._width==1 && img._depth==1 && img._spectrum==1 &&
- res._width==1 && res._depth==1 && res._spectrum==1)
- std::memcpy(&res[pos],img._data,sizeof(T)*img._height);
- else
- res.draw_image((int)(align*(dx - img._width)),
- pos,
- (int)(align*(dz - img._depth)),
- (int)(align*(dc - img._spectrum)),
- img);
- }
- pos+=img._height;
- }
- } break;
- case 'z' : { // Along the Z-axis
- cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- dx = std::max(dx,img._width);
- dy = std::max(dy,img._height);
- dz+=img._depth;
- dc = std::max(dc,img._spectrum);
- }
- }
- res.assign(dx,dy,dz,dc,(T)0);
- if (res) cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- if (img._width==1 && img._height==1 && img._spectrum==1 &&
- res._width==1 && res._height==1 && res._spectrum==1)
- std::memcpy(&res[pos],img._data,sizeof(T)*img._depth);
- else
- res.draw_image((int)(align*(dx - img._width)),
- (int)(align*(dy - img._height)),
- pos,
- (int)(align*(dc - img._spectrum)),
- img);
- }
- pos+=img._depth;
- }
- } break;
- default : { // Along the C-axis
- cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- dx = std::max(dx,img._width);
- dy = std::max(dy,img._height);
- dz = std::max(dz,img._depth);
- dc+=img._spectrum;
- }
- }
- res.assign(dx,dy,dz,dc,(T)0);
- if (res) cimglist_for(*this,l) {
- const CImg<T>& img = (*this)[l];
- if (img) {
- if (img._width==1 && img._height==1 && img._depth==1 &&
- res._width==1 && res._height==1 && res._depth==1)
- std::memcpy(&res[pos],img._data,sizeof(T)*img._spectrum);
- else
- res.draw_image((int)(align*(dx - img._width)),
- (int)(align*(dy - img._height)),
- (int)(align*(dz - img._depth)),
- pos,
- img);
- }
- pos+=img._spectrum;
- }
- }
- }
- return res;
- }
- //! Return a list where each image has been split along the specified axis.
- /**
- \param axis Axis to split images along.
- \param nb Number of split parts for each image.
- **/
- CImgList<T>& split(const char axis, const int nb=-1) {
- return get_split(axis,nb).move_to(*this);
- }
- //! Return a list where each image has been split along the specified axis \newinstance.
- CImgList<T> get_split(const char axis, const int nb=-1) const {
- CImgList<T> res;
- cimglist_for(*this,l) _data[l].get_split(axis,nb).move_to(res,~0U);
- return res;
- }
- //! Insert image at the end of the list.
- /**
- \param img Image to insert.
- **/
- template<typename t>
- CImgList<T>& push_back(const CImg<t>& img) {
- return insert(img);
- }
- //! Insert image at the front of the list.
- /**
- \param img Image to insert.
- **/
- template<typename t>
- CImgList<T>& push_front(const CImg<t>& img) {
- return insert(img,0);
- }
- //! Insert list at the end of the current list.
- /**
- \param list List to insert.
- **/
- template<typename t>
- CImgList<T>& push_back(const CImgList<t>& list) {
- return insert(list);
- }
- //! Insert list at the front of the current list.
- /**
- \param list List to insert.
- **/
- template<typename t>
- CImgList<T>& push_front(const CImgList<t>& list) {
- return insert(list,0);
- }
- //! Remove last image.
- /**
- **/
- CImgList<T>& pop_back() {
- return remove(_width - 1);
- }
- //! Remove first image.
- /**
- **/
- CImgList<T>& pop_front() {
- return remove(0);
- }
- //! Remove image pointed by iterator.
- /**
- \param iter Iterator pointing to the image to remove.
- **/
- CImgList<T>& erase(const iterator iter) {
- return remove(iter - _data);
- }
- //@}
- //----------------------------------
- //
- //! \name Data Input
- //@{
- //----------------------------------
- //! Display a simple interactive interface to select images or sublists.
- /**
- \param disp Window instance to display selection and user interface.
- \param feature_type Can be \c false to select a single image, or \c true to select a sublist.
- \param axis Axis along whom images are appended for visualization.
- \param align Alignment setting when images have not all the same size.
- \param exit_on_anykey Exit function when any key is pressed.
- \return A one-column vector containing the selected image indexes.
- **/
- CImg<intT> get_select(CImgDisplay &disp, const bool feature_type=true,
- const char axis='x', const float align=0,
- const bool exit_on_anykey=false) const {
- return _select(disp,0,feature_type,axis,align,exit_on_anykey,0,false,false,false);
- }
- //! Display a simple interactive interface to select images or sublists.
- /**
- \param title Title of a new window used to display selection and user interface.
- \param feature_type Can be \c false to select a single image, or \c true to select a sublist.
- \param axis Axis along whom images are appended for visualization.
- \param align Alignment setting when images have not all the same size.
- \param exit_on_anykey Exit function when any key is pressed.
- \return A one-column vector containing the selected image indexes.
- **/
- CImg<intT> get_select(const char *const title, const bool feature_type=true,
- const char axis='x', const float align=0,
- const bool exit_on_anykey=false) const {
- CImgDisplay disp;
- return _select(disp,title,feature_type,axis,align,exit_on_anykey,0,false,false,false);
- }
- CImg<intT> _select(CImgDisplay &disp, const char *const title, const bool feature_type,
- const char axis, const float align, const bool exit_on_anykey,
- const unsigned int orig, const bool resize_disp,
- const bool exit_on_rightbutton, const bool exit_on_wheel) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "select(): Empty instance.",
- cimglist_instance);
- // Create image correspondence table and get list dimensions for visualization.
- CImgList<uintT> _indices;
- unsigned int max_width = 0, max_height = 0, sum_width = 0, sum_height = 0;
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- const unsigned int
- w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
- h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
- if (w>max_width) max_width = w;
- if (h>max_height) max_height = h;
- sum_width+=w; sum_height+=h;
- if (axis=='x') CImg<uintT>(w,1,1,1,(unsigned int)l).move_to(_indices);
- else CImg<uintT>(h,1,1,1,(unsigned int)l).move_to(_indices);
- }
- const CImg<uintT> indices0 = _indices>'x';
- // Create display window.
- if (!disp) {
- if (axis=='x') disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:0,1);
- else disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:0,1);
- if (!title) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width);
- } else {
- if (title) disp.set_title("%s",title);
- disp.move_inside_screen();
- }
- if (resize_disp) {
- if (axis=='x') disp.resize(cimg_fitscreen(sum_width,max_height,1),false);
- else disp.resize(cimg_fitscreen(max_width,sum_height,1),false);
- }
- const unsigned int old_normalization = disp.normalization();
- bool old_is_resized = disp.is_resized();
- disp._normalization = 0;
- disp.show().set_key(0).show_mouse();
- static const unsigned char foreground_color[] = { 255,255,255 }, background_color[] = { 0,0,0 };
- // Enter event loop.
- CImg<ucharT> visu0, visu;
- CImg<uintT> indices;
- CImg<intT> positions(_width,4,1,1,-1);
- int oindex0 = -1, oindex1 = -1, index0 = -1, index1 = -1;
- bool is_clicked = false, is_selected = false, text_down = false, update_display = true;
- unsigned int key = 0, font_size = 32;
- while (!is_selected && !disp.is_closed() && !key) {
- // Create background image.
- if (!visu0) {
- visu0.assign(disp._width,disp._height,1,3,0); visu.assign();
- (indices0.get_resize(axis=='x'?visu0._width:visu0._height,1)).move_to(indices);
- unsigned int _ind = 0;
- const CImg<T> onexone(1,1,1,1,(T)0);
- if (axis=='x')
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,4))
- cimglist_for(*this,ind) {
- unsigned int x0 = 0;
- while (x0<visu0._width && indices[x0++]!=(unsigned int)ind) {}
- unsigned int x1 = x0;
- while (x1<visu0._width && indices[x1++]==(unsigned int)ind) {}
- const CImg<T> &src = _data[ind]?_data[ind]:onexone;
- CImg<ucharT> res;
- src._get_select(disp,old_normalization,src._width/2,src._height/2,src._depth/2).
- move_to(res);
- const unsigned int h = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,true);
- res.resize(x1 - x0,std::max(32U,h*disp._height/max_height),1,res._spectrum==1?3:-100);
- positions(ind,0) = positions(ind,2) = (int)x0;
- positions(ind,1) = positions(ind,3) = (int)(align*(visu0.height() - res.height()));
- positions(ind,2)+=res._width;
- positions(ind,3)+=res._height - 1;
- visu0.draw_image(positions(ind,0),positions(ind,1),res);
- }
- else
- cimg_pragma_openmp(parallel for cimg_openmp_if_size(_width,4))
- cimglist_for(*this,ind) {
- unsigned int y0 = 0;
- while (y0<visu0._height && indices[y0++]!=(unsigned int)ind) {}
- unsigned int y1 = y0;
- while (y1<visu0._height && indices[y1++]==(unsigned int)ind) {}
- const CImg<T> &src = _data[ind]?_data[ind]:onexone;
- CImg<ucharT> res;
- src._get_select(disp,old_normalization,(src._width - 1)/2,(src._height - 1)/2,(src._depth - 1)/2).
- move_to(res);
- const unsigned int w = CImgDisplay::_fitscreen(res._width,res._height,1,128,-85,false);
- res.resize(std::max(32U,w*disp._width/max_width),y1 - y0,1,res._spectrum==1?3:-100);
- positions(ind,0) = positions(ind,2) = (int)(align*(visu0.width() - res.width()));
- positions(ind,1) = positions(ind,3) = (int)y0;
- positions(ind,2)+=res._width - 1;
- positions(ind,3)+=res._height;
- visu0.draw_image(positions(ind,0),positions(ind,1),res);
- }
- if (axis=='x') --positions(_ind,2); else --positions(_ind,3);
- update_display = true;
- }
- if (!visu || oindex0!=index0 || oindex1!=index1) {
- if (index0>=0 && index1>=0) {
- visu.assign(visu0,false);
- const int indm = std::min(index0,index1), indM = std::max(index0,index1);
- for (int ind = indm; ind<=indM; ++ind) if (positions(ind,0)>=0) {
- visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3),
- background_color,0.2f);
- if ((axis=='x' && positions(ind,2) - positions(ind,0)>=8) ||
- (axis!='x' && positions(ind,3) - positions(ind,1)>=8))
- visu.draw_rectangle(positions(ind,0),positions(ind,1),positions(ind,2),positions(ind,3),
- foreground_color,0.9f,0xAAAAAAAA);
- }
- if (is_clicked) visu.__draw_text(" Images #%u - #%u, Size = %u ",font_size,(int)text_down,
- orig + indm,orig + indM,indM - indm + 1);
- else visu.__draw_text(" Image #%u (%u,%u,%u,%u) ",font_size,(int)text_down,
- orig + index0,
- _data[index0]._width,
- _data[index0]._height,
- _data[index0]._depth,
- _data[index0]._spectrum);
- update_display = true;
- } else visu.assign();
- }
- if (!visu) { visu.assign(visu0,true); update_display = true; }
- if (update_display) { visu.display(disp); update_display = false; }
- disp.wait();
- // Manage user events.
- const int xm = disp.mouse_x(), ym = disp.mouse_y();
- int index = -1;
- if (xm>=0) {
- index = (int)indices(axis=='x'?xm:ym);
- if (disp.button()&1) {
- if (!is_clicked) { is_clicked = true; oindex0 = index0; index0 = index; }
- oindex1 = index1; index1 = index;
- if (!feature_type) is_selected = true;
- } else {
- if (!is_clicked) { oindex0 = oindex1 = index0; index0 = index1 = index; }
- else is_selected = true;
- }
- } else {
- if (is_clicked) {
- if (!(disp.button()&1)) { is_clicked = is_selected = false; index0 = index1 = -1; }
- else index1 = -1;
- } else index0 = index1 = -1;
- }
- if (disp.button()&4) { is_clicked = is_selected = false; index0 = index1 = -1; }
- if (disp.button()&2 && exit_on_rightbutton) { is_selected = true; index1 = index0 = -1; }
- if (disp.wheel() && exit_on_wheel) is_selected = true;
- CImg<charT> filename(32);
- switch (key = disp.key()) {
- #if cimg_OS!=2
- case cimg::keyCTRLRIGHT :
- #endif
- case 0 : case cimg::keyCTRLLEFT : key = 0; break;
- case cimg::keyD : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,false),
- CImgDisplay::_fitscreen(3*disp.width()/2,3*disp.height()/2,1,128,-100,true),false).
- _is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyC : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(2*disp.width()/3,2*disp.height()/3,1),false)._is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyR : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.set_fullscreen(false).
- resize(cimg_fitscreen(axis=='x'?sum_width:max_width,axis=='x'?max_height:sum_height,1),false).
- _is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyF : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- disp.resize(disp.screen_width(),disp.screen_height(),false).toggle_fullscreen()._is_resized = true;
- disp.set_key(key,false); key = 0; visu0.assign();
- } break;
- case cimg::keyS : if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.bmp",snap_number++);
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- if (visu0) {
- (+visu0).__draw_text(" Saving snapshot... ",font_size,(int)text_down).display(disp);
- visu0.save(filename);
- (+visu0).__draw_text(" Snapshot '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
- }
- disp.set_key(key,false).wait(); key = 0;
- } break;
- case cimg::keyO :
- if (disp.is_keyCTRLLEFT() || disp.is_keyCTRLRIGHT()) {
- static unsigned int snap_number = 0;
- std::FILE *file;
- do {
- #ifdef cimg_use_zlib
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimgz",snap_number++);
- #else
- cimg_snprintf(filename,filename._width,cimg_appname "_%.4u.cimg",snap_number++);
- #endif
- if ((file=cimg::std_fopen(filename,"r"))!=0) cimg::fclose(file);
- } while (file);
- (+visu0).__draw_text(" Saving instance... ",font_size,(int)text_down).display(disp);
- save(filename);
- (+visu0).__draw_text(" Instance '%s' saved. ",font_size,(int)text_down,filename._data).display(disp);
- disp.set_key(key,false).wait(); key = 0;
- } break;
- }
- if (disp.is_resized()) { disp.resize(false); visu0.assign(); }
- if (ym>=0 && ym<13) { if (!text_down) { visu.assign(); text_down = true; }}
- else if (ym>=visu.height() - 13) { if (text_down) { visu.assign(); text_down = false; }}
- if (!exit_on_anykey && key && key!=cimg::keyESC &&
- (key!=cimg::keyW || (!disp.is_keyCTRLLEFT() && !disp.is_keyCTRLRIGHT()))) {
- key = 0;
- }
- }
- CImg<intT> res(1,2,1,1,-1);
- if (is_selected) {
- if (feature_type) res.fill(std::min(index0,index1),std::max(index0,index1));
- else res.fill(index0);
- }
- if (!(disp.button()&2)) disp.set_button();
- disp._normalization = old_normalization;
- disp._is_resized = old_is_resized;
- disp.set_key(key);
- return res;
- }
- //! Load a list from a file.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "load(): Specified filename is (null).",
- cimglist_instance);
- if (!cimg::strncasecmp(filename,"http://",7) || !cimg::strncasecmp(filename,"https://",8)) {
- CImg<charT> filename_local(256);
- load(cimg::load_network(filename,filename_local));
- std::remove(filename_local);
- return *this;
- }
- const bool is_stdin = *filename=='-' && (!filename[1] || filename[1]=='.');
- const char *const ext = cimg::split_filename(filename);
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- bool is_loaded = true;
- try {
- #ifdef cimglist_load_plugin
- cimglist_load_plugin(filename);
- #endif
- #ifdef cimglist_load_plugin1
- cimglist_load_plugin1(filename);
- #endif
- #ifdef cimglist_load_plugin2
- cimglist_load_plugin2(filename);
- #endif
- #ifdef cimglist_load_plugin3
- cimglist_load_plugin3(filename);
- #endif
- #ifdef cimglist_load_plugin4
- cimglist_load_plugin4(filename);
- #endif
- #ifdef cimglist_load_plugin5
- cimglist_load_plugin5(filename);
- #endif
- #ifdef cimglist_load_plugin6
- cimglist_load_plugin6(filename);
- #endif
- #ifdef cimglist_load_plugin7
- cimglist_load_plugin7(filename);
- #endif
- #ifdef cimglist_load_plugin8
- cimglist_load_plugin8(filename);
- #endif
- if (!cimg::strcasecmp(ext,"tif") ||
- !cimg::strcasecmp(ext,"tiff")) load_tiff(filename);
- else if (!cimg::strcasecmp(ext,"gif")) load_gif_external(filename);
- else if (!cimg::strcasecmp(ext,"cimg") ||
- !cimg::strcasecmp(ext,"cimgz") ||
- !*ext) load_cimg(filename);
- else if (!cimg::strcasecmp(ext,"rec") ||
- !cimg::strcasecmp(ext,"par")) load_parrec(filename);
- else if (!cimg::strcasecmp(ext,"avi") ||
- !cimg::strcasecmp(ext,"mov") ||
- !cimg::strcasecmp(ext,"asf") ||
- !cimg::strcasecmp(ext,"divx") ||
- !cimg::strcasecmp(ext,"flv") ||
- !cimg::strcasecmp(ext,"mpg") ||
- !cimg::strcasecmp(ext,"m1v") ||
- !cimg::strcasecmp(ext,"m2v") ||
- !cimg::strcasecmp(ext,"m4v") ||
- !cimg::strcasecmp(ext,"mjp") ||
- !cimg::strcasecmp(ext,"mp4") ||
- !cimg::strcasecmp(ext,"mkv") ||
- !cimg::strcasecmp(ext,"mpe") ||
- !cimg::strcasecmp(ext,"movie") ||
- !cimg::strcasecmp(ext,"ogm") ||
- !cimg::strcasecmp(ext,"ogg") ||
- !cimg::strcasecmp(ext,"ogv") ||
- !cimg::strcasecmp(ext,"qt") ||
- !cimg::strcasecmp(ext,"rm") ||
- !cimg::strcasecmp(ext,"vob") ||
- !cimg::strcasecmp(ext,"webm") ||
- !cimg::strcasecmp(ext,"wmv") ||
- !cimg::strcasecmp(ext,"xvid") ||
- !cimg::strcasecmp(ext,"mpeg")) load_video(filename);
- else if (!cimg::strcasecmp(ext,"gz")) load_gzip_external(filename);
- else is_loaded = false;
- } catch (CImgIOException&) { is_loaded = false; }
- // If nothing loaded, try to guess file format from magic number in file.
- if (!is_loaded && !is_stdin) {
- std::FILE *const file = cimg::std_fopen(filename,"rb");
- if (!file) {
- cimg::exception_mode(omode);
- throw CImgIOException(_cimglist_instance
- "load(): Failed to open file '%s'.",
- cimglist_instance,
- filename);
- }
- const char *const f_type = cimg::ftype(file,filename);
- cimg::fclose(file);
- is_loaded = true;
- try {
- if (!cimg::strcasecmp(f_type,"gif")) load_gif_external(filename);
- else if (!cimg::strcasecmp(f_type,"tif") &&
- cimg::strcasecmp(ext,"nef") &&
- cimg::strcasecmp(ext,"dng")) load_tiff(filename);
- else is_loaded = false;
- } catch (CImgIOException&) { is_loaded = false; }
- }
- // If nothing loaded, try to load file as a single image.
- if (!is_loaded) {
- assign(1);
- try {
- _data->load(filename);
- } catch (CImgIOException&) {
- cimg::exception_mode(omode);
- throw CImgIOException(_cimglist_instance
- "load(): Failed to recognize format of file '%s'.",
- cimglist_instance,
- filename);
- }
- }
- cimg::exception_mode(omode);
- return *this;
- }
- //! Load a list from a file \newinstance.
- static CImgList<T> get_load(const char *const filename) {
- return CImgList<T>().load(filename);
- }
- //! Load a list from a .cimg file.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load_cimg(const char *const filename) {
- return _load_cimg(0,filename);
- }
- //! Load a list from a .cimg file \newinstance.
- static CImgList<T> get_load_cimg(const char *const filename) {
- return CImgList<T>().load_cimg(filename);
- }
- //! Load a list from a .cimg file.
- /**
- \param file File to read data from.
- **/
- CImgList<T>& load_cimg(std::FILE *const file) {
- return _load_cimg(file,0);
- }
- //! Load a list from a .cimg file \newinstance.
- static CImgList<T> get_load_cimg(std::FILE *const file) {
- return CImgList<T>().load_cimg(file);
- }
- CImgList<T>& _load_cimg(std::FILE *const file, const char *const filename) {
- #ifdef cimg_use_zlib
- #define _cimgz_load_cimg_case(Tss) { \
- Bytef *const cbuf = new Bytef[csiz]; \
- cimg::fread(cbuf,(size_t)csiz,nfile); \
- if (is_bool) { \
- CImg<ucharT> raw(W*H*D*C/8); \
- uLongf destlen = (uLongf)raw.size(); \
- uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
- img.assign(W,H,D,C); \
- img._uchar2bool(raw,raw.size(),false); \
- } else { \
- CImg<Tss> raw(W,H,D,C); \
- uLongf destlen = (uLongf)(raw.size()*sizeof(Tss)); \
- uncompress((Bytef*)raw._data,&destlen,cbuf,(uLong)csiz); \
- if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
- raw.move_to(img); \
- } \
- delete[] cbuf; \
- }
- #else
- #define _cimgz_load_cimg_case(Tss) \
- throw CImgIOException(_cimglist_instance \
- "load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.", \
- cimglist_instance, \
- filename?filename:"(FILE*)");
- #endif
- #define _cimg_load_cimg_case(Ts,Tss) \
- if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
- const bool is_bool = cimg::type<Tss>::string()==cimg::type<bool>::string(); \
- for (unsigned int l = 0; l<N; ++l) { \
- j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0; \
- W = H = D = C = 0; csiz = 0; \
- if ((err = cimg_sscanf(tmp,"%u %u %u %u #" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \
- throw CImgIOException(_cimglist_instance \
- "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'.", \
- cimglist_instance, \
- W,H,D,C,l,filename?filename:("(FILE*)")); \
- if (W*H*D*C>0) { \
- CImg<T> &img = _data[l]; \
- if (err==5) _cimgz_load_cimg_case(Tss) \
- else { \
- img.assign(W,H,D,C); \
- T *ptrd = img._data; \
- if (is_bool) { \
- CImg<ucharT> raw; \
- for (ulongT to_read = img.size(); to_read; ) { \
- raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \
- cimg::fread(raw._data,raw._width,nfile); \
- CImg<T>(ptrd,std::min(8*raw._width,(unsigned int)(img.end() - ptrd)),1,1,1,true).\
- _uchar2bool(raw,raw._width,false); \
- to_read-=raw._width; \
- } \
- } else { \
- CImg<Tss> raw; \
- for (ulongT to_read = img.size(); to_read; ) { \
- raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \
- cimg::fread(raw._data,raw._width,nfile); \
- if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
- const Tss *ptrs = raw._data; \
- for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \
- to_read-=raw._width; \
- } \
- } \
- } \
- } \
- } \
- loaded = true; \
- }
- if (!filename && !file)
- throw CImgArgumentException(_cimglist_instance
- "load_cimg(): Specified filename is (null).",
- cimglist_instance);
- const ulongT cimg_iobuffer = (ulongT)24*1024*1024;
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- bool loaded = false, endian = cimg::endianness();
- CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
- *tmp = *str_pixeltype = *str_endian = 0;
- unsigned int j, N = 0, W, H, D, C;
- cimg_uint64 csiz;
- int i, err;
- do {
- j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0;
- } while (*tmp=='#' && i>=0);
- err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
- &N,str_pixeltype._data,str_endian._data);
- if (err<2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "load_cimg(): File or CImg header not found in file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- }
- if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
- else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
- assign(N);
- _cimg_load_cimg_case("bool",bool);
- _cimg_load_cimg_case("unsigned_char",unsigned char);
- _cimg_load_cimg_case("uchar",unsigned char);
- _cimg_load_cimg_case("char",char);
- _cimg_load_cimg_case("unsigned_short",unsigned short);
- _cimg_load_cimg_case("ushort",unsigned short);
- _cimg_load_cimg_case("short",short);
- _cimg_load_cimg_case("unsigned_int",unsigned int);
- _cimg_load_cimg_case("uint",unsigned int);
- _cimg_load_cimg_case("int",int);
- _cimg_load_cimg_case("unsigned_long",ulongT);
- _cimg_load_cimg_case("ulong",ulongT);
- _cimg_load_cimg_case("long",longT);
- _cimg_load_cimg_case("unsigned_int64",uint64T);
- _cimg_load_cimg_case("uint64",uint64T);
- _cimg_load_cimg_case("int64",int64T);
- _cimg_load_cimg_case("float",float);
- _cimg_load_cimg_case("double",double);
- if (!loaded) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "load_cimg(): Unsupported pixel type '%s' for file '%s'.",
- cimglist_instance,
- str_pixeltype._data,filename?filename:"(FILE*)");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load a sublist list from a (non compressed) .cimg file.
- /**
- \param filename Filename to read data from.
- \param n0 Starting index of images to read (~0U for max).
- \param n1 Ending index of images to read (~0U for max).
- \param x0 Starting X-coordinates of image regions to read.
- \param y0 Starting Y-coordinates of image regions to read.
- \param z0 Starting Z-coordinates of image regions to read.
- \param c0 Starting C-coordinates of image regions to read.
- \param x1 Ending X-coordinates of image regions to read (~0U for max).
- \param y1 Ending Y-coordinates of image regions to read (~0U for max).
- \param z1 Ending Z-coordinates of image regions to read (~0U for max).
- \param c1 Ending C-coordinates of image regions to read (~0U for max).
- **/
- CImgList<T>& load_cimg(const char *const filename,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1) {
- return _load_cimg(0,filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- }
- //! Load a sublist list from a (non compressed) .cimg file \newinstance.
- static CImgList<T> get_load_cimg(const char *const filename,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1) {
- return CImgList<T>().load_cimg(filename,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- }
- //! Load a sub-image list from a (non compressed) .cimg file \overloading.
- CImgList<T>& load_cimg(std::FILE *const file,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1) {
- return _load_cimg(file,0,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- }
- //! Load a sub-image list from a (non compressed) .cimg file \newinstance.
- static CImgList<T> get_load_cimg(std::FILE *const file,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1) {
- return CImgList<T>().load_cimg(file,n0,n1,x0,y0,z0,c0,x1,y1,z1,c1);
- }
- CImgList<T>& _load_cimg(std::FILE *const file, const char *const filename,
- const unsigned int n0, const unsigned int n1,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0,
- const unsigned int x1, const unsigned int y1,
- const unsigned int z1, const unsigned int c1) {
- #define _cimg_load_cimg_case2(Ts,Tss) \
- if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
- for (unsigned int l = 0; l<=nn1; ++l) { \
- j = 0; while ((i=std::fgetc(nfile))!='\n' && i>=0) tmp[j++] = (char)i; tmp[j] = 0; \
- W = H = D = C = 0; \
- if (cimg_sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \
- throw CImgIOException(_cimglist_instance \
- "load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'", \
- cimglist_instance, \
- W,H,D,C,l,filename?filename:"(FILE*)"); \
- if (W*H*D*C>0) { \
- if (l<nn0 || nx0>=W || ny0>=H || nz0>=D || nc0>=C) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \
- else { \
- const unsigned int \
- _nx1 = nx1==~0U?W - 1:nx1, \
- _ny1 = ny1==~0U?H - 1:ny1, \
- _nz1 = nz1==~0U?D - 1:nz1, \
- _nc1 = nc1==~0U?C - 1:nc1; \
- if (_nx1>=W || _ny1>=H || _nz1>=D || _nc1>=C) \
- throw CImgArgumentException(_cimglist_instance \
- "load_cimg(): Invalid specified coordinates " \
- "[%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) " \
- "because image [%u] in file '%s' has size (%u,%u,%u,%u).", \
- cimglist_instance, \
- n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,l,filename?filename:"(FILE*)",W,H,D,C); \
- CImg<Tss> raw(1 + _nx1 - nx0); \
- CImg<T> &img = _data[l - nn0]; \
- img.assign(1 + _nx1 - nx0,1 + _ny1 - ny0,1 + _nz1 - nz0,1 + _nc1 - nc0); \
- T *ptrd = img._data; \
- ulongT skipvb = nc0*W*H*D*sizeof(Tss); \
- if (skipvb) cimg::fseek(nfile,skipvb,SEEK_CUR); \
- for (unsigned int c = 1 + _nc1 - nc0; c; --c) { \
- const ulongT skipzb = nz0*W*H*sizeof(Tss); \
- if (skipzb) cimg::fseek(nfile,skipzb,SEEK_CUR); \
- for (unsigned int z = 1 + _nz1 - nz0; z; --z) { \
- const ulongT skipyb = ny0*W*sizeof(Tss); \
- if (skipyb) cimg::fseek(nfile,skipyb,SEEK_CUR); \
- for (unsigned int y = 1 + _ny1 - ny0; y; --y) { \
- const ulongT skipxb = nx0*sizeof(Tss); \
- if (skipxb) cimg::fseek(nfile,skipxb,SEEK_CUR); \
- cimg::fread(raw._data,raw._width,nfile); \
- if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw._width); \
- const Tss *ptrs = raw._data; \
- for (unsigned int off = raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \
- const ulongT skipxe = (W - 1 - _nx1)*sizeof(Tss); \
- if (skipxe) cimg::fseek(nfile,skipxe,SEEK_CUR); \
- } \
- const ulongT skipye = (H - 1 - _ny1)*W*sizeof(Tss); \
- if (skipye) cimg::fseek(nfile,skipye,SEEK_CUR); \
- } \
- const ulongT skipze = (D - 1 - _nz1)*W*H*sizeof(Tss); \
- if (skipze) cimg::fseek(nfile,skipze,SEEK_CUR); \
- } \
- const ulongT skipve = (C - 1 - _nc1)*W*H*D*sizeof(Tss); \
- if (skipve) cimg::fseek(nfile,skipve,SEEK_CUR); \
- } \
- } \
- } \
- loaded = true; \
- }
- if (!filename && !file)
- throw CImgArgumentException(_cimglist_instance
- "load_cimg(): Specified filename is (null).",
- cimglist_instance);
- unsigned int
- nn0 = std::min(n0,n1), nn1 = std::max(n0,n1),
- nx0 = std::min(x0,x1), nx1 = std::max(x0,x1),
- ny0 = std::min(y0,y1), ny1 = std::max(y0,y1),
- nz0 = std::min(z0,z1), nz1 = std::max(z0,z1),
- nc0 = std::min(c0,c1), nc1 = std::max(c0,c1);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- bool loaded = false, endian = cimg::endianness();
- CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
- *tmp = *str_pixeltype = *str_endian = 0;
- unsigned int j, N, W, H, D, C;
- int i, err;
- j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
- err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
- &N,str_pixeltype._data,str_endian._data);
- if (err<2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "load_cimg(): CImg header not found in file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- }
- if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
- else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
- nn1 = n1==~0U?N - 1:n1;
- if (nn1>=N)
- throw CImgArgumentException(_cimglist_instance
- "load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) "
- "because file '%s' contains only %u images.",
- cimglist_instance,
- n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:"(FILE*)",N);
- assign(1 + nn1 - n0);
- _cimg_load_cimg_case2("bool",bool);
- _cimg_load_cimg_case2("unsigned_char",unsigned char);
- _cimg_load_cimg_case2("uchar",unsigned char);
- _cimg_load_cimg_case2("char",char);
- _cimg_load_cimg_case2("unsigned_short",unsigned short);
- _cimg_load_cimg_case2("ushort",unsigned short);
- _cimg_load_cimg_case2("short",short);
- _cimg_load_cimg_case2("unsigned_int",unsigned int);
- _cimg_load_cimg_case2("uint",unsigned int);
- _cimg_load_cimg_case2("int",int);
- _cimg_load_cimg_case2("unsigned_long",ulongT);
- _cimg_load_cimg_case2("ulong",ulongT);
- _cimg_load_cimg_case2("long",longT);
- _cimg_load_cimg_case2("unsigned_int64",uint64T);
- _cimg_load_cimg_case2("uint64",uint64T);
- _cimg_load_cimg_case2("int64",int64T);
- _cimg_load_cimg_case2("float",float);
- _cimg_load_cimg_case2("double",double);
- if (!loaded) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "load_cimg(): Unsupported pixel type '%s' for file '%s'.",
- cimglist_instance,
- str_pixeltype._data,filename?filename:"(FILE*)");
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load a list from a PAR/REC (Philips) file.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load_parrec(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "load_parrec(): Specified filename is (null).",
- cimglist_instance);
- CImg<charT> body(1024), filenamepar(1024), filenamerec(1024);
- *body = *filenamepar = *filenamerec = 0;
- const char *const ext = cimg::split_filename(filename,body);
- if (!std::strcmp(ext,"par")) {
- std::strncpy(filenamepar,filename,filenamepar._width - 1);
- cimg_snprintf(filenamerec,filenamerec._width,"%s.rec",body._data);
- }
- if (!std::strcmp(ext,"PAR")) {
- std::strncpy(filenamepar,filename,filenamepar._width - 1);
- cimg_snprintf(filenamerec,filenamerec._width,"%s.REC",body._data);
- }
- if (!std::strcmp(ext,"rec")) {
- std::strncpy(filenamerec,filename,filenamerec._width - 1);
- cimg_snprintf(filenamepar,filenamepar._width,"%s.par",body._data);
- }
- if (!std::strcmp(ext,"REC")) {
- std::strncpy(filenamerec,filename,filenamerec._width - 1);
- cimg_snprintf(filenamepar,filenamepar._width,"%s.PAR",body._data);
- }
- std::FILE *file = cimg::fopen(filenamepar,"r");
- // Parse header file
- CImgList<floatT> st_slices;
- CImgList<uintT> st_global;
- CImg<charT> line(256); *line = 0;
- int err;
- do { err = std::fscanf(file,"%255[^\n]%*c",line._data); } while (err!=EOF && (*line=='#' || *line=='.'));
- do {
- unsigned int sn,size_x,size_y,pixsize;
- float rs,ri,ss;
- 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);
- if (err==7) {
- CImg<floatT>::vector((float)sn,(float)pixsize,(float)size_x,(float)size_y,ri,rs,ss,0).move_to(st_slices);
- unsigned int i; for (i = 0; i<st_global._width && sn<=st_global[i][2]; ++i) {}
- if (i==st_global._width) CImg<uintT>::vector(size_x,size_y,sn).move_to(st_global);
- else {
- CImg<uintT> &vec = st_global[i];
- if (size_x>vec[0]) vec[0] = size_x;
- if (size_y>vec[1]) vec[1] = size_y;
- vec[2] = sn;
- }
- st_slices[st_slices._width - 1][7] = (float)i;
- }
- } while (err==7);
- // Read data
- std::FILE *file2 = cimg::fopen(filenamerec,"rb");
- cimglist_for(st_global,l) {
- const CImg<uintT>& vec = st_global[l];
- CImg<T>(vec[0],vec[1],vec[2]).move_to(*this);
- }
- cimglist_for(st_slices,l) {
- const CImg<floatT>& vec = st_slices[l];
- const unsigned int
- sn = (unsigned int)vec[0] - 1,
- pixsize = (unsigned int)vec[1],
- size_x = (unsigned int)vec[2],
- size_y = (unsigned int)vec[3],
- imn = (unsigned int)vec[7];
- const float ri = vec[4], rs = vec[5], ss = vec[6];
- switch (pixsize) {
- case 8 : {
- CImg<ucharT> buf(size_x,size_y);
- cimg::fread(buf._data,size_x*size_y,file2);
- if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
- CImg<T>& img = (*this)[imn];
- cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
- } break;
- case 16 : {
- CImg<ushortT> buf(size_x,size_y);
- cimg::fread(buf._data,size_x*size_y,file2);
- if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
- CImg<T>& img = (*this)[imn];
- cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
- } break;
- case 32 : {
- CImg<uintT> buf(size_x,size_y);
- cimg::fread(buf._data,size_x*size_y,file2);
- if (cimg::endianness()) cimg::invert_endianness(buf._data,size_x*size_y);
- CImg<T>& img = (*this)[imn];
- cimg_forXY(img,x,y) img(x,y,sn) = (T)(( buf(x,y)*rs + ri )/(rs*ss));
- } break;
- default :
- cimg::fclose(file);
- cimg::fclose(file2);
- throw CImgIOException(_cimglist_instance
- "load_parrec(): Unsupported %d-bits pixel type for file '%s'.",
- cimglist_instance,
- pixsize,filename);
- }
- }
- cimg::fclose(file);
- cimg::fclose(file2);
- if (!_width)
- throw CImgIOException(_cimglist_instance
- "load_parrec(): Failed to recognize valid PAR-REC data in file '%s'.",
- cimglist_instance,
- filename);
- return *this;
- }
- //! Load a list from a PAR/REC (Philips) file \newinstance.
- static CImgList<T> get_load_parrec(const char *const filename) {
- return CImgList<T>().load_parrec(filename);
- }
- //! Load a list from a YUV image sequence file.
- /**
- \param filename Filename to read data from.
- \param size_x Width of the images.
- \param size_y Height of the images.
- \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
- \param first_frame Index of first image frame to read.
- \param last_frame Index of last image frame to read.
- \param step_frame Step applied between each frame.
- \param yuv2rgb Apply YUV to RGB transformation during reading.
- **/
- CImgList<T>& load_yuv(const char *const filename,
- const unsigned int size_x, const unsigned int size_y,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true) {
- return _load_yuv(0,filename,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb);
- }
- //! Load a list from a YUV image sequence file \newinstance.
- static CImgList<T> get_load_yuv(const char *const filename,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true) {
- return CImgList<T>().load_yuv(filename,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb);
- }
- //! Load a list from an image sequence YUV file \overloading.
- CImgList<T>& load_yuv(std::FILE *const file,
- const unsigned int size_x, const unsigned int size_y,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true) {
- return _load_yuv(file,0,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb);
- }
- //! Load a list from an image sequence YUV file \newinstance.
- static CImgList<T> get_load_yuv(std::FILE *const file,
- const unsigned int size_x, const unsigned int size_y=1,
- const unsigned int chroma_subsampling=444,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, const bool yuv2rgb=true) {
- return CImgList<T>().load_yuv(file,size_x,size_y,chroma_subsampling,
- first_frame,last_frame,step_frame,yuv2rgb);
- }
- CImgList<T>& _load_yuv(std::FILE *const file, const char *const filename,
- const unsigned int size_x, const unsigned int size_y,
- const unsigned int chroma_subsampling,
- const unsigned int first_frame, const unsigned int last_frame,
- const unsigned int step_frame, const bool yuv2rgb) {
- if (!filename && !file)
- throw CImgArgumentException(_cimglist_instance
- "load_yuv(): Specified filename is (null).",
- cimglist_instance);
- if (chroma_subsampling!=420 && chroma_subsampling!=422 && chroma_subsampling!=444)
- throw CImgArgumentException(_cimglist_instance
- "load_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.",
- cimglist_instance,
- chroma_subsampling,filename?filename:"(FILE*)");
- const unsigned int
- cfx = chroma_subsampling==420 || chroma_subsampling==422?2:1,
- cfy = chroma_subsampling==420?2:1,
- nfirst_frame = first_frame<last_frame?first_frame:last_frame,
- nlast_frame = first_frame<last_frame?last_frame:first_frame,
- nstep_frame = step_frame?step_frame:1;
- if (!size_x || !size_y || size_x%cfx || size_y%cfy)
- throw CImgArgumentException(_cimglist_instance
- "load_yuv(): Specified dimensions (%u,%u) are invalid, for file '%s'.",
- cimglist_instance,
- size_x,size_y,filename?filename:"(FILE*)");
- CImg<ucharT> YUV(size_x,size_y,1,3), UV(size_x/cfx,size_y/cfy,1,2);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
- bool stop_flag = false;
- int err;
- if (nfirst_frame) {
- err = cimg::fseek(nfile,(uint64T)nfirst_frame*(YUV._width*YUV._height + 2*UV._width*UV._height),SEEK_CUR);
- if (err) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "load_yuv(): File '%s' doesn't contain frame number %u.",
- cimglist_instance,
- filename?filename:"(FILE*)",nfirst_frame);
- }
- }
- unsigned int frame;
- for (frame = nfirst_frame; !stop_flag && frame<=nlast_frame; frame+=nstep_frame) {
- YUV.get_shared_channel(0).fill(0);
- // *TRY* to read the luminance part, do not replace by cimg::fread!
- err = (int)std::fread((void*)(YUV._data),1,(size_t)YUV._width*YUV._height,nfile);
- if (err!=(int)(YUV._width*YUV._height)) {
- stop_flag = true;
- if (err>0)
- cimg::warn(_cimglist_instance
- "load_yuv(): File '%s' contains incomplete data or given image dimensions "
- "(%u,%u) are incorrect.",
- cimglist_instance,
- filename?filename:"(FILE*)",size_x,size_y);
- } else {
- UV.fill(0);
- // *TRY* to read the luminance part, do not replace by cimg::fread!
- err = (int)std::fread((void*)(UV._data),1,(size_t)UV.size(),nfile);
- if (err!=(int)(UV.size())) {
- stop_flag = true;
- if (err>0)
- cimg::warn(_cimglist_instance
- "load_yuv(): File '%s' contains incomplete data or given image dimensions "
- "(%u,%u) are incorrect.",
- cimglist_instance,
- filename?filename:"(FILE*)",size_x,size_y);
- } else {
- const ucharT *ptrs1 = UV._data, *ptrs2 = UV.data(0,0,0,1);
- ucharT *ptrd1 = YUV.data(0,0,0,1), *ptrd2 = YUV.data(0,0,0,2);
- const unsigned int wd = YUV._width;
- switch (chroma_subsampling) {
- case 420 :
- cimg_forY(UV,y) {
- cimg_forX(UV,x) {
- const ucharT U = *(ptrs1++), V = *(ptrs2++);
- ptrd1[wd] = U; *(ptrd1)++ = U;
- ptrd1[wd] = U; *(ptrd1)++ = U;
- ptrd2[wd] = V; *(ptrd2)++ = V;
- ptrd2[wd] = V; *(ptrd2)++ = V;
- }
- ptrd1+=wd; ptrd2+=wd;
- }
- break;
- case 422 :
- cimg_forXY(UV,x,y) {
- const ucharT U = *(ptrs1++), V = *(ptrs2++);
- *(ptrd1++) = U; *(ptrd1++) = U;
- *(ptrd2++) = V; *(ptrd2++) = V;
- }
- break;
- default :
- YUV.draw_image(0,0,0,1,UV);
- }
- if (yuv2rgb) YUV.YCbCrtoRGB();
- insert(YUV);
- if (nstep_frame>1) cimg::fseek(nfile,(uint64T)(nstep_frame - 1)*(size_x*size_y + size_x*size_y/2),SEEK_CUR);
- }
- }
- }
- if (is_empty())
- throw CImgIOException(_cimglist_instance
- "load_yuv() : Missing data in file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- if (stop_flag && nlast_frame!=~0U && frame!=nlast_frame)
- cimg::warn(_cimglist_instance
- "load_yuv(): Frame %d not reached since only %u frames were found in file '%s'.",
- cimglist_instance,
- nlast_frame,frame - 1,filename?filename:"(FILE*)");
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Load an image from a video file, using OpenCV library.
- /**
- \param filename Filename, as a C-string.
- \param first_frame Index of the first frame to read.
- \param last_frame Index of the last frame to read (can be higher than the actual number of frames, e.g. '~0U').
- \param step_frame Step value for frame reading.
- \note If step_frame==0, the current video stream is forced to be released (without any frames read).
- **/
- CImgList<T>& load_video(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1) {
- #ifndef cimg_use_opencv
- if (first_frame || last_frame!=~0U || step_frame>1)
- throw CImgArgumentException(_cimglist_instance
- "load_video() : File '%s', arguments 'first_frame', 'last_frame' "
- "and 'step_frame' requires features from the OpenCV library "
- "('-Dcimg_use_opencv' must be defined).",
- cimglist_instance,filename);
- return load_ffmpeg_external(filename);
- #else
- static cv::VideoCapture *captures[32] = { 0 };
- static CImgList<charT> filenames(32);
- static CImg<uintT> positions(32,1,1,1,0);
- static int last_used_index = -1;
- // Detect if a video capture already exists for the specified filename.
- cimg::mutex(9);
- int index = -1;
- if (filename) {
- if (last_used_index>=0 && !std::strcmp(filename,filenames[last_used_index])) {
- index = last_used_index;
- } else cimglist_for(filenames,l) if (filenames[l] && !std::strcmp(filename,filenames[l])) {
- index = l; break;
- }
- } else index = last_used_index;
- cimg::mutex(9,0);
- // Release stream if needed.
- if (!step_frame || (index>=0 && positions[index]>first_frame)) {
- if (index>=0) {
- cimg::mutex(9);
- captures[index]->release();
- delete captures[index];
- captures[index] = 0;
- positions[index] = 0;
- filenames[index].assign();
- if (last_used_index==index) last_used_index = -1;
- index = -1;
- cimg::mutex(9,0);
- } else
- if (filename)
- cimg::warn(_cimglist_instance
- "load_video() : File '%s', no opened video stream associated with filename found.",
- cimglist_instance,filename);
- else
- cimg::warn(_cimglist_instance
- "load_video() : No opened video stream found.",
- cimglist_instance,filename);
- if (!step_frame) return *this;
- }
- // Find empty slot for capturing video stream.
- if (index<0) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "load_video(): No already open video reader found. You must specify a "
- "non-(null) filename argument for the first call.",
- cimglist_instance);
- else { cimg::mutex(9); cimglist_for(filenames,l) if (!filenames[l]) { index = l; break; } cimg::mutex(9,0); }
- if (index<0)
- throw CImgIOException(_cimglist_instance
- "load_video(): File '%s', no video reader slots available. "
- "You have to release some of your previously opened videos.",
- cimglist_instance,filename);
- cimg::mutex(9);
- captures[index] = new cv::VideoCapture(filename);
- positions[index] = 0;
- if (!captures[index]->isOpened()) {
- delete captures[index];
- captures[index] = 0;
- cimg::mutex(9,0);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check file availability
- throw CImgIOException(_cimglist_instance
- "load_video(): File '%s', unable to detect format of video file.",
- cimglist_instance,filename);
- }
- CImg<charT>::string(filename).move_to(filenames[index]);
- cimg::mutex(9,0);
- }
- cimg::mutex(9);
- const unsigned int nb_frames = (unsigned int)std::max(0.,captures[index]->get(_cimg_cap_prop_frame_count));
- cimg::mutex(9,0);
- assign();
- // Skip frames if requested.
- bool go_on = true;
- unsigned int &pos = positions[index];
- while (pos<first_frame) {
- cimg::mutex(9);
- if (!captures[index]->grab()) { cimg::mutex(9,0); go_on = false; break; }
- cimg::mutex(9,0);
- ++pos;
- }
- // Read and convert frames.
- const unsigned int _last_frame = std::min(nb_frames?nb_frames - 1:~0U,last_frame);
- while (go_on && pos<=_last_frame) {
- cv::Mat cvimg;
- cimg::mutex(9);
- if (captures[index]->read(cvimg)) { CImg<T>::_cvmat2cimg(cvimg).move_to(*this); ++pos; }
- else go_on = false;
- cimg::mutex(9,0);
- if (go_on)
- for (unsigned int i = 1; go_on && i<step_frame && pos<=_last_frame; ++i, ++pos) {
- cimg::mutex(9);
- if (!captures[index]->grab()) go_on = false;
- cimg::mutex(9,0);
- }
- }
- if (!go_on || (nb_frames && pos>=nb_frames)) { // Close video stream when necessary
- cimg::mutex(9);
- captures[index]->release();
- delete captures[index];
- captures[index] = 0;
- filenames[index].assign();
- positions[index] = 0;
- index = -1;
- cimg::mutex(9,0);
- }
- cimg::mutex(9);
- last_used_index = index;
- cimg::mutex(9,0);
- if (is_empty())
- throw CImgIOException(_cimglist_instance
- "load_video(): File '%s', unable to locate frame %u.",
- cimglist_instance,filename,first_frame);
- return *this;
- #endif
- }
- //! Load an image from a video file, using OpenCV library \newinstance.
- static CImgList<T> get_load_video(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1) {
- return CImgList<T>().load_video(filename,first_frame,last_frame,step_frame);
- }
- //! Load an image from a video file using the external tool 'ffmpeg'.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load_ffmpeg_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "load_ffmpeg_external(): Specified filename is (null).",
- cimglist_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
- std::FILE *file = 0;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001.ppm",filename_tmp._data);
- if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%%6d.ppm",filename_tmp._data);
- cimg_snprintf(command,command._width,"\"%s\" -v -8 -i \"%s\" \"%s\"",
- cimg::ffmpeg_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp2)._system_strescape().data());
- cimg::system(command, cimg::ffmpeg_path());
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- assign();
- unsigned int i = 1;
- for (bool stop_flag = false; !stop_flag; ++i) {
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u.ppm",filename_tmp._data,i);
- CImg<T> img;
- try { img.load_pnm(filename_tmp2); }
- catch (CImgException&) { stop_flag = true; }
- if (img) { img.move_to(*this); std::remove(filename_tmp2); }
- }
- cimg::exception_mode(omode);
- if (is_empty())
- throw CImgIOException(_cimglist_instance
- "load_ffmpeg_external(): Failed to open file '%s' with external command 'ffmpeg'.",
- cimglist_instance,
- filename);
- return *this;
- }
- //! Load an image from a video file using the external tool 'ffmpeg' \newinstance.
- static CImgList<T> get_load_ffmpeg_external(const char *const filename) {
- return CImgList<T>().load_ffmpeg_external(filename);
- }
- //! Load gif file, using ImageMagick or GraphicsMagick's external tools.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load_gif_external(const char *const filename) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "load_gif_external(): Specified filename is (null).",
- cimglist_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- if (!_load_gif_external(filename,false))
- if (!_load_gif_external(filename,true))
- try { assign(CImg<T>().load_other(filename)); } catch (CImgException&) { assign(); }
- if (is_empty())
- throw CImgIOException(_cimglist_instance
- "load_gif_external(): Failed to open file '%s'.",
- cimglist_instance,filename);
- return *this;
- }
- CImgList<T>& _load_gif_external(const char *const filename, const bool use_graphicsmagick=false) {
- CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
- std::FILE *file = 0;
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- if (use_graphicsmagick) cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png.0",filename_tmp._data);
- else cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s-0.png",filename_tmp._data);
- if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
- } while (file);
- if (use_graphicsmagick) cimg_snprintf(command,command._width,"%s convert \"%s\" \"%s.png\"",
- cimg::graphicsmagick_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- else cimg_snprintf(command,command._width,"\"%s\" -coalesce \"%s\" \"%s.png\"",
- cimg::imagemagick_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command, cimg::imagemagick_path());
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- assign();
- // Try to read a single frame gif.
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png",filename_tmp._data);
- CImg<T> img;
- try { img.load_png(filename_tmp2); }
- catch (CImgException&) { }
- if (img) { img.move_to(*this); std::remove(filename_tmp2); }
- else { // Try to read animated gif
- unsigned int i = 0;
- for (bool stop_flag = false; !stop_flag; ++i) {
- if (use_graphicsmagick) cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s.png.%u",filename_tmp._data,i);
- else cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s-%u.png",filename_tmp._data,i);
- try { img.load_png(filename_tmp2); }
- catch (CImgException&) { stop_flag = true; }
- if (img) { img.move_to(*this); std::remove(filename_tmp2); }
- }
- }
- cimg::exception_mode(omode);
- return *this;
- }
- //! Load gif file, using ImageMagick or GraphicsMagick's external tools \newinstance.
- static CImgList<T> get_load_gif_external(const char *const filename) {
- return CImgList<T>().load_gif_external(filename);
- }
- //! Load a gzipped list, using external tool 'gunzip'.
- /**
- \param filename Filename to read data from.
- **/
- CImgList<T>& load_gzip_external(const char *const filename) {
- if (!filename)
- throw CImgIOException(_cimglist_instance
- "load_gzip_external(): Specified filename is (null).",
- cimglist_instance);
- cimg::fclose(cimg::fopen(filename,"rb")); // Check if file exists
- CImg<charT> command(1024), filename_tmp(256), body(256);
- const char
- *ext = cimg::split_filename(filename,body),
- *ext2 = cimg::split_filename(body,0);
- std::FILE *file = 0;
- do {
- if (!cimg::strcasecmp(ext,"gz")) {
- if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- } else {
- if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- }
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
- cimg::gunzip_path(),
- CImg<charT>::string(filename)._system_strescape().data(),
- CImg<charT>::string(filename_tmp)._system_strescape().data());
- cimg::system(command, cimg::gunzip_path());
- if (!(file=cimg::std_fopen(filename_tmp,"rb"))) {
- cimg::fclose(cimg::fopen(filename,"r"));
- throw CImgIOException(_cimglist_instance
- "load_gzip_external(): Failed to open file '%s'.",
- cimglist_instance,
- filename);
- } else cimg::fclose(file);
- load(filename_tmp);
- std::remove(filename_tmp);
- return *this;
- }
- //! Load a gzipped list, using external tool 'gunzip' \newinstance.
- static CImgList<T> get_load_gzip_external(const char *const filename) {
- return CImgList<T>().load_gzip_external(filename);
- }
- //! Load images from a TIFF file.
- /**
- \param filename Filename to read data from.
- \param first_frame Index of first image frame to read.
- \param last_frame Index of last image frame to read.
- \param step_frame Step applied between each frame.
- \param[out] bits_per_value Number of bits used to store a scalar value in the image file.
- \param[out] voxel_size Voxel size, as stored in the filename.
- \param[out] description Description, as stored in the filename.
- **/
- CImgList<T>& load_tiff(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
- float *const voxel_size=0, CImg<charT> *const description=0) {
- const unsigned int
- nfirst_frame = first_frame<last_frame?first_frame:last_frame,
- nstep_frame = step_frame?step_frame:1;
- unsigned int nlast_frame = first_frame<last_frame?last_frame:first_frame;
- #ifndef cimg_use_tiff
- cimg::unused(bits_per_value,voxel_size,description);
- if (nfirst_frame || nlast_frame!=~0U || nstep_frame!=1)
- throw CImgArgumentException(_cimglist_instance
- "load_tiff(): Unable to load sub-images from file '%s' unless libtiff is enabled.",
- cimglist_instance,
- filename);
- return assign(CImg<T>::get_load_tiff(filename));
- #else
- #if cimg_verbosity<3
- TIFFSetWarningHandler(0);
- TIFFSetErrorHandler(0);
- #endif
- TIFF *tif = TIFFOpen(filename,"r");
- if (tif) {
- unsigned int nb_images = 0;
- do ++nb_images; while (TIFFReadDirectory(tif));
- if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))
- cimg::warn(_cimglist_instance
- "load_tiff(): Invalid specified frame range is [%u,%u] (step %u) since "
- "file '%s' contains %u image(s).",
- cimglist_instance,
- nfirst_frame,nlast_frame,nstep_frame,filename,nb_images);
- if (nfirst_frame>=nb_images) return assign();
- if (nlast_frame>=nb_images) nlast_frame = nb_images - 1;
- assign(1 + (nlast_frame - nfirst_frame)/nstep_frame);
- TIFFSetDirectory(tif,0);
- cimglist_for(*this,l)
- _data[l]._load_tiff(tif,nfirst_frame + l*nstep_frame,bits_per_value,voxel_size,description);
- TIFFClose(tif);
- } else throw CImgIOException(_cimglist_instance
- "load_tiff(): Failed to open file '%s'.",
- cimglist_instance,
- filename);
- return *this;
- #endif
- }
- //! Load a multi-page TIFF file \newinstance.
- static CImgList<T> get_load_tiff(const char *const filename,
- const unsigned int first_frame=0, const unsigned int last_frame=~0U,
- const unsigned int step_frame=1, unsigned int *const bits_per_value=0,
- float *const voxel_size=0, CImg<charT> *const description=0) {
- return CImgList<T>().load_tiff(filename,first_frame,last_frame,step_frame,bits_per_value,voxel_size,description);
- }
- //@}
- //----------------------------------
- //
- //! \name Data Output
- //@{
- //----------------------------------
- //! Print information about the list on the standard output.
- /**
- \param title Label set to the information displayed.
- \param display_stats Tells if image statistics must be computed and displayed.
- **/
- const CImgList<T>& print(const char *const title=0, const bool display_stats=true) const {
- unsigned int msiz = 0;
- cimglist_for(*this,l) msiz+=_data[l].size();
- msiz*=sizeof(T);
- const unsigned int mdisp = msiz<8*1024?0U:msiz<8*1024*1024?1U:2U;
- CImg<charT> _title(64);
- if (!title) cimg_snprintf(_title,_title._width,"CImgList<%s>",pixel_type());
- std::fprintf(cimg::output(),"%s%s%s%s: %sthis%s = %p, %ssize%s = %u/%u [%u %s], %sdata%s = (CImg<%s>*)%p",
- cimg::t_magenta,cimg::t_bold,title?title:_title._data,cimg::t_normal,
- cimg::t_bold,cimg::t_normal,(void*)this,
- cimg::t_bold,cimg::t_normal,_width,_allocated_width,
- mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20)),
- mdisp==0?"b":(mdisp==1?"Kio":"Mio"),
- cimg::t_bold,cimg::t_normal,pixel_type(),(void*)begin());
- if (_data) std::fprintf(cimg::output(),"..%p.\n",(void*)((char*)end() - 1));
- else std::fprintf(cimg::output(),".\n");
- char tmp[16] = { 0 };
- cimglist_for(*this,ll) {
- cimg_snprintf(tmp,sizeof(tmp),"[%d]",ll);
- std::fprintf(cimg::output()," ");
- _data[ll].print(tmp,display_stats);
- if (ll==3 && width()>8) { ll = width() - 5; std::fprintf(cimg::output()," ...\n"); }
- }
- std::fflush(cimg::output());
- return *this;
- }
- //! Display the current CImgList instance in an existing CImgDisplay window (by reference).
- /**
- \param disp Reference to an existing CImgDisplay instance, where the current image list will be displayed.
- \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- \note This function displays the list images of the current CImgList instance into an existing
- CImgDisplay window.
- Images of the list are appended in a single temporary image for visualization purposes.
- The function returns immediately.
- **/
- const CImgList<T>& display(CImgDisplay &disp, const char axis='x', const float align=0) const {
- disp.display(*this,axis,align);
- return *this;
- }
- //! Display the current CImgList instance in a new display window.
- /**
- \param disp Display window.
- \param display_info Tells if image information are displayed on the standard output.
- \param axis Alignment axis for images viewing.
- \param align Appending alignment.
- \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
- \param exit_on_anykey Exit function when any key is pressed.
- \note This function opens a new window with a specific title and displays the list images of the
- current CImgList instance into it.
- Images of the list are appended in a single temporary image for visualization purposes.
- The function returns when a key is pressed or the display window is closed by the user.
- **/
- const CImgList<T>& display(CImgDisplay &disp, const bool display_info,
- const char axis='x', const float align=0,
- unsigned int *const XYZ=0, const bool exit_on_anykey=false) const {
- bool is_exit = false;
- return _display(disp,0,0,display_info,axis,align,XYZ,exit_on_anykey,0,true,is_exit);
- }
- //! Display the current CImgList instance in a new display window.
- /**
- \param title Title of the opening display window.
- \param display_info Tells if list information must be written on standard output.
- \param axis Appending axis. Can be <tt>{ 'x' | 'y' | 'z' | 'c' }</tt>.
- \param align Appending alignment.
- \param[in,out] XYZ Contains the XYZ coordinates at start / exit of the function.
- \param exit_on_anykey Exit function when any key is pressed.
- **/
- const CImgList<T>& display(const char *const title=0, const bool display_info=true,
- const char axis='x', const float align=0,
- unsigned int *const XYZ=0, const bool exit_on_anykey=false) const {
- CImgDisplay disp;
- bool is_exit = false;
- return _display(disp,title,0,display_info,axis,align,XYZ,exit_on_anykey,0,true,is_exit);
- }
- const CImgList<T>& _display(CImgDisplay &disp, const char *const title, const CImgList<charT> *const titles,
- const bool display_info, const char axis, const float align, unsigned int *const XYZ,
- const bool exit_on_anykey, const unsigned int orig, const bool is_first_call,
- bool &is_exit) const {
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "display(): Empty instance.",
- cimglist_instance);
- if (!disp) {
- if (axis=='x') {
- unsigned int sum_width = 0, max_height = 0;
- cimglist_for(*this,l) {
- const CImg<T> &img = _data[l];
- const unsigned int
- w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
- h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
- sum_width+=w;
- if (h>max_height) max_height = h;
- }
- disp.assign(cimg_fitscreen(sum_width,max_height,1),title?title:titles?titles->__display()._data:0,1);
- } else {
- unsigned int max_width = 0, sum_height = 0;
- cimglist_for(*this,l) {
- const CImg<T> &img = _data[l];
- const unsigned int
- w = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,false),
- h = CImgDisplay::_fitscreen(img._width,img._height,img._depth,128,-85,true);
- if (w>max_width) max_width = w;
- sum_height+=h;
- }
- disp.assign(cimg_fitscreen(max_width,sum_height,1),title?title:titles?titles->__display()._data:0,1);
- }
- if (!title && !titles) disp.set_title("CImgList<%s> (%u)",pixel_type(),_width);
- } else if (title) disp.set_title("%s",title);
- else if (titles) disp.set_title("%s",titles->__display()._data);
- const CImg<char> dtitle = CImg<char>::string(disp.title());
- if (display_info) print(disp.title());
- disp.show().flush();
- if (_width==1) {
- const unsigned int dw = disp._width, dh = disp._height;
- if (!is_first_call)
- disp.resize(cimg_fitscreen(_data[0]._width,_data[0]._height,_data[0]._depth),false);
- disp.set_title("%s (%ux%ux%ux%u)",
- dtitle.data(),_data[0]._width,_data[0]._height,_data[0]._depth,_data[0]._spectrum);
- _data[0]._display(disp,0,false,XYZ,exit_on_anykey,!is_first_call);
- if (disp.key()) is_exit = true;
- disp.resize(cimg_fitscreen(dw,dh,1),false).set_title("%s",dtitle.data());
- } else {
- bool disp_resize = !is_first_call;
- while (!disp.is_closed() && !is_exit) {
- const CImg<intT> s = _select(disp,0,true,axis,align,exit_on_anykey,orig,disp_resize,!is_first_call,true);
- disp_resize = true;
- if (s[0]<0 && !disp.wheel()) { // No selections done
- if (disp.button()&2) { disp.flush(); break; }
- is_exit = true;
- } else if (disp.wheel()) { // Zoom in/out
- const int wheel = disp.wheel();
- disp.set_wheel();
- if (!is_first_call && wheel<0) break;
- if (wheel>0 && _width>=4) {
- const unsigned int
- delta = std::max(1U,(unsigned int)cimg::round(0.3*_width)),
- ind0 = (unsigned int)std::max(0,s[0] - (int)delta),
- ind1 = (unsigned int)std::min(width() - 1,s[0] + (int)delta);
- if ((ind0!=0 || ind1!=_width - 1) && ind1 - ind0>=3) {
- const CImgList<T> sublist = get_shared_images(ind0,ind1);
- CImgList<charT> t_sublist;
- if (titles) t_sublist = titles->get_shared_images(ind0,ind1);
- sublist._display(disp,0,titles?&t_sublist:0,false,axis,align,XYZ,exit_on_anykey,
- orig + ind0,false,is_exit);
- }
- }
- } else if (s[0]!=0 || s[1]!=width() - 1) {
- const CImgList<T> sublist = get_shared_images(s[0],s[1]);
- CImgList<charT> t_sublist;
- if (titles) t_sublist = titles->get_shared_images(s[0],s[1]);
- sublist._display(disp,0,titles?&t_sublist:0,false,axis,align,XYZ,exit_on_anykey,
- orig + s[0],false,is_exit);
- }
- disp.set_title("%s",dtitle.data());
- }
- }
- return *this;
- }
- // [internal] Return string to describe display title.
- CImg<charT> __display() const {
- CImg<charT> res, str;
- cimglist_for(*this,l) {
- CImg<charT>::string((char*)_data[l]).move_to(str);
- if (l!=width() - 1) {
- str.resize(str._width + 1,1,1,1,0);
- str[str._width - 2] = ',';
- str[str._width - 1] = ' ';
- }
- res.append(str,'x');
- }
- if (!res) return CImg<charT>(1,1,1,1,0).move_to(res);
- cimg::strellipsize(res,128,false);
- if (_width>1) {
- const unsigned int l = (unsigned int)std::strlen(res);
- if (res._width<=l + 16) res.resize(l + 16,1,1,1,0);
- cimg_snprintf(res._data + l,16," (#%u)",_width);
- }
- return res;
- }
- //! Save list into a file.
- /**
- \param filename Filename to write data to.
- \param number When positive, represents an index added to the filename. Otherwise, no number is added.
- \param digits Number of digits used for adding the number to the filename.
- **/
- const CImgList<T>& save(const char *const filename, const int number=-1, const unsigned int digits=6) const {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "save(): Specified filename is (null).",
- cimglist_instance);
- // Do not test for empty instances, since .cimg format is able to manage empty instances.
- const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.');
- const char *const ext = cimg::split_filename(filename);
- CImg<charT> nfilename(1024);
- const char *const fn = is_stdout?filename:number>=0?cimg::number_filename(filename,number,digits,nfilename):
- filename;
- #ifdef cimglist_save_plugin
- cimglist_save_plugin(fn);
- #endif
- #ifdef cimglist_save_plugin1
- cimglist_save_plugin1(fn);
- #endif
- #ifdef cimglist_save_plugin2
- cimglist_save_plugin2(fn);
- #endif
- #ifdef cimglist_save_plugin3
- cimglist_save_plugin3(fn);
- #endif
- #ifdef cimglist_save_plugin4
- cimglist_save_plugin4(fn);
- #endif
- #ifdef cimglist_save_plugin5
- cimglist_save_plugin5(fn);
- #endif
- #ifdef cimglist_save_plugin6
- cimglist_save_plugin6(fn);
- #endif
- #ifdef cimglist_save_plugin7
- cimglist_save_plugin7(fn);
- #endif
- #ifdef cimglist_save_plugin8
- cimglist_save_plugin8(fn);
- #endif
- if (!cimg::strcasecmp(ext,"cimgz")) return save_cimg(fn,true);
- else if (!cimg::strcasecmp(ext,"cimg") || !*ext) return save_cimg(fn,false);
- else if (!cimg::strcasecmp(ext,"yuv")) return save_yuv(fn,444,true);
- else if (!cimg::strcasecmp(ext,"avi") ||
- !cimg::strcasecmp(ext,"mov") ||
- !cimg::strcasecmp(ext,"asf") ||
- !cimg::strcasecmp(ext,"divx") ||
- !cimg::strcasecmp(ext,"flv") ||
- !cimg::strcasecmp(ext,"mpg") ||
- !cimg::strcasecmp(ext,"m1v") ||
- !cimg::strcasecmp(ext,"m2v") ||
- !cimg::strcasecmp(ext,"m4v") ||
- !cimg::strcasecmp(ext,"mjp") ||
- !cimg::strcasecmp(ext,"mp4") ||
- !cimg::strcasecmp(ext,"mkv") ||
- !cimg::strcasecmp(ext,"mpe") ||
- !cimg::strcasecmp(ext,"movie") ||
- !cimg::strcasecmp(ext,"ogm") ||
- !cimg::strcasecmp(ext,"ogg") ||
- !cimg::strcasecmp(ext,"ogv") ||
- !cimg::strcasecmp(ext,"qt") ||
- !cimg::strcasecmp(ext,"rm") ||
- !cimg::strcasecmp(ext,"vob") ||
- !cimg::strcasecmp(ext,"webm") ||
- !cimg::strcasecmp(ext,"wmv") ||
- !cimg::strcasecmp(ext,"xvid") ||
- !cimg::strcasecmp(ext,"mpeg")) return save_video(fn);
- #ifdef cimg_use_tiff
- else if (!cimg::strcasecmp(ext,"tif") ||
- !cimg::strcasecmp(ext,"tiff")) return save_tiff(fn);
- #endif
- else if (!cimg::strcasecmp(ext,"gz")) return save_gzip_external(fn);
- else {
- if (_width==1) _data[0].save(fn,-1);
- else cimglist_for(*this,l) { _data[l].save(fn,is_stdout?-1:l); if (is_stdout) std::fputc(EOF,cimg::_stdout()); }
- }
- return *this;
- }
- //! Tell if an image list can be saved as one single file.
- /**
- \param filename Filename, as a C-string.
- \return \c true if the file format supports multiple images, \c false otherwise.
- **/
- static bool is_saveable(const char *const filename) {
- const char *const ext = cimg::split_filename(filename);
- if (!cimg::strcasecmp(ext,"cimgz") ||
- #ifdef cimg_use_tiff
- !cimg::strcasecmp(ext,"tif") ||
- !cimg::strcasecmp(ext,"tiff") ||
- #endif
- !cimg::strcasecmp(ext,"yuv") ||
- !cimg::strcasecmp(ext,"avi") ||
- !cimg::strcasecmp(ext,"mov") ||
- !cimg::strcasecmp(ext,"asf") ||
- !cimg::strcasecmp(ext,"divx") ||
- !cimg::strcasecmp(ext,"flv") ||
- !cimg::strcasecmp(ext,"mpg") ||
- !cimg::strcasecmp(ext,"m1v") ||
- !cimg::strcasecmp(ext,"m2v") ||
- !cimg::strcasecmp(ext,"m4v") ||
- !cimg::strcasecmp(ext,"mjp") ||
- !cimg::strcasecmp(ext,"mp4") ||
- !cimg::strcasecmp(ext,"mkv") ||
- !cimg::strcasecmp(ext,"mpe") ||
- !cimg::strcasecmp(ext,"movie") ||
- !cimg::strcasecmp(ext,"ogm") ||
- !cimg::strcasecmp(ext,"ogg") ||
- !cimg::strcasecmp(ext,"ogv") ||
- !cimg::strcasecmp(ext,"qt") ||
- !cimg::strcasecmp(ext,"rm") ||
- !cimg::strcasecmp(ext,"vob") ||
- !cimg::strcasecmp(ext,"webm") ||
- !cimg::strcasecmp(ext,"wmv") ||
- !cimg::strcasecmp(ext,"xvid") ||
- !cimg::strcasecmp(ext,"mpeg")) return true;
- return false;
- }
- //! Save image sequence as a GIF animated file.
- /**
- \param filename Filename to write data to.
- \param fps Number of desired frames per second.
- \param nb_loops Number of loops (\c 0 for infinite looping).
- **/
- const CImgList<T>& save_gif_external(const char *const filename, const float fps=25,
- const unsigned int nb_loops=0) {
- CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
- CImgList<charT> filenames;
- std::FILE *file = 0;
- #ifdef cimg_use_png
- #define _cimg_save_gif_extension "png"
- #else
- #define _cimg_save_gif_extension "ppm"
- #endif
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001." _cimg_save_gif_extension,filename_tmp._data);
- if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
- } while (file);
- cimglist_for(*this,l) {
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u." _cimg_save_gif_extension,filename_tmp._data,l + 1);
- CImg<charT>::string(filename_tmp2).move_to(filenames);
- if (_data[l]._depth>1 || _data[l]._spectrum!=3) _data[l].get_resize(-100,-100,1,3).save(filename_tmp2);
- else _data[l].save(filename_tmp2);
- }
- cimg_snprintf(command,command._width,"\"%s\" -delay %u -loop %u",
- cimg::imagemagick_path(),(unsigned int)std::max(0.f,cimg::round(100/fps)),nb_loops);
- CImg<ucharT>::string(command).move_to(filenames,0);
- cimg_snprintf(command,command._width,"\"%s\"",
- CImg<charT>::string(filename)._system_strescape().data());
- CImg<ucharT>::string(command).move_to(filenames);
- CImg<charT> _command = filenames>'x';
- cimg_for(_command,p,char) if (!*p) *p = ' ';
- _command.back() = 0;
- cimg::system(_command, cimg::imagemagick_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimglist_instance
- "save_gif_external(): Failed to save file '%s' with external command 'magick/convert'.",
- cimglist_instance,
- filename);
- else cimg::fclose(file);
- cimglist_for_in(*this,1,filenames._width - 1,l) std::remove(filenames[l]);
- return *this;
- }
- //! Save list as a YUV image sequence file.
- /**
- \param filename Filename to write data to.
- \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
- \param is_rgb Tells if the RGB to YUV conversion must be done for saving.
- **/
- const CImgList<T>& save_yuv(const char *const filename=0,
- const unsigned int chroma_subsampling=444,
- const bool is_rgb=true) const {
- return _save_yuv(0,filename,chroma_subsampling,is_rgb);
- }
- //! Save image sequence into a YUV file.
- /**
- \param file File to write data to.
- \param chroma_subsampling Type of chroma subsampling. Can be <tt>{ 420 | 422 | 444 }</tt>.
- \param is_rgb Tells if the RGB to YUV conversion must be done for saving.
- **/
- const CImgList<T>& save_yuv(std::FILE *const file,
- const unsigned int chroma_subsampling=444,
- const bool is_rgb=true) const {
- return _save_yuv(file,0,chroma_subsampling,is_rgb);
- }
- const CImgList<T>& _save_yuv(std::FILE *const file, const char *const filename,
- const unsigned int chroma_subsampling,
- const bool is_rgb) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimglist_instance
- "save_yuv(): Specified filename is (null).",
- cimglist_instance);
- if (chroma_subsampling!=420 && chroma_subsampling!=422 && chroma_subsampling!=444)
- throw CImgArgumentException(_cimglist_instance
- "save_yuv(): Specified chroma subsampling %u is invalid, for file '%s'.",
- cimglist_instance,
- chroma_subsampling,filename?filename:"(FILE*)");
- if (is_empty()) { cimg::fempty(file,filename); return *this; }
- const unsigned int
- cfx = chroma_subsampling==420 || chroma_subsampling==422?2:1,
- cfy = chroma_subsampling==420?2:1,
- w0 = (*this)[0]._width, h0 = (*this)[0]._height,
- width0 = w0 + (w0%cfx), height0 = h0 + (h0%cfy);
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- cimglist_for(*this,l) {
- const CImg<T> &frame = (*this)[l];
- cimg_forZ(frame,z) {
- CImg<ucharT> YUV;
- if (sizeof(T)==1 && !is_rgb &&
- frame._width==width0 && frame._height==height0 && frame._depth==1 && frame._spectrum==3)
- YUV.assign((unsigned char*)frame._data,width0,height0,1,3,true);
- else {
- YUV = frame.get_slice(z);
- if (YUV._width!=width0 || YUV._height!=height0) YUV.resize(width0,height0,1,-100,0);
- if (YUV._spectrum!=3) YUV.resize(-100,-100,1,3,YUV._spectrum==1?1:0);
- if (is_rgb) YUV.RGBtoYCbCr();
- }
- if (chroma_subsampling==444)
- cimg::fwrite(YUV._data,(size_t)YUV._width*YUV._height*3,nfile);
- else {
- cimg::fwrite(YUV._data,(size_t)YUV._width*YUV._height,nfile);
- CImg<ucharT> UV = YUV.get_channels(1,2);
- UV.resize(UV._width/cfx,UV._height/cfy,1,2,2);
- cimg::fwrite(UV._data,(size_t)UV._width*UV._height*2,nfile);
- }
- }
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save list into a .cimg file.
- /**
- \param filename Filename to write data to.
- \param is_compressed Tells if data compression must be enabled.
- **/
- const CImgList<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {
- return _save_cimg(0,filename,is_compressed);
- }
- const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const {
- if (!file && !filename)
- throw CImgArgumentException(_cimglist_instance
- "save_cimg(): Specified filename is (null).",
- cimglist_instance);
- #ifndef cimg_use_zlib
- if (is_compressed)
- cimg::warn(_cimglist_instance
- "save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, "
- "saving them uncompressed.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- #endif
- const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const bool is_bool = ptype==cimg::type<bool>::string();
- if (!is_bool && std::strstr(ptype,"unsigned")==ptype)
- std::fprintf(nfile,"%u unsigned_%s %s_endian\n",_width,ptype + 9,etype);
- else
- std::fprintf(nfile,"%u %s %s_endian\n",_width,ptype,etype);
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- std::fprintf(nfile,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);
- if (img._data) {
- CImg<T> tmp;
- if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); }
- const CImg<T>& ref = cimg::endianness()?tmp:img;
- bool failed_to_compress = true;
- if (is_compressed) {
- #ifdef cimg_use_zlib
- Bytef *cbuf = 0;
- uLongf csiz = 0;
- if (is_bool) { // Boolean data (bitwise)
- ulongT siz;
- const unsigned char *const buf = ref._bool2uchar(siz,false);
- csiz = siz + siz/100 + 16;
- cbuf = new Bytef[csiz];
- failed_to_compress = (bool)compress(cbuf,&csiz,(Bytef*)buf,siz);
- if (!failed_to_compress) {
- std::fprintf(nfile," #%lu\n",csiz);
- cimg::fwrite(cbuf,csiz,nfile);
- }
- delete[] buf;
- } else { // Non-boolean data
- const ulongT siz = sizeof(T)*ref.size();
- csiz = siz + siz/100 + 16;
- cbuf = new Bytef[csiz];
- failed_to_compress = (bool)compress(cbuf,&csiz,(Bytef*)ref._data,siz);
- if (!failed_to_compress) {
- std::fprintf(nfile," #%lu\n",csiz);
- cimg::fwrite(cbuf,csiz,nfile);
- }
- }
- if (failed_to_compress)
- cimg::warn(_cimglist_instance
- "save_cimg(): Failed to save compressed data for file '%s', saving them uncompressed.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- delete[] cbuf;
- #endif
- }
- if (failed_to_compress) { // Write non-compressed
- std::fputc('\n',nfile);
- if (is_bool) { // Boolean data (bitwise)
- ulongT siz;
- const unsigned char *const buf = ref._bool2uchar(siz,false);
- cimg::fwrite(buf,siz,nfile);
- delete[] buf;
- } else cimg::fwrite(ref._data,ref.size(),nfile); // Non-boolean data
- }
- } else std::fputc('\n',nfile);
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Save list into a .cimg file.
- /**
- \param file File to write data to.
- \param is_compressed Tells if data compression must be enabled.
- **/
- const CImgList<T>& save_cimg(std::FILE *file, const bool is_compressed=false) const {
- return _save_cimg(file,0,is_compressed);
- }
- const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename,
- const unsigned int n0,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0) const {
- #define _cimg_save_cimg_case(Ts,Tss) \
- if (!saved && !cimg::strcasecmp(Ts,str_pixeltype)) { \
- for (unsigned int l = 0; l<lmax; ++l) { \
- j = 0; while ((i=std::fgetc(nfile))!='\n') tmp[j++]=(char)i; tmp[j] = 0; \
- W = H = D = C = 0; \
- if (cimg_sscanf(tmp,"%u %u %u %u",&W,&H,&D,&C)!=4) \
- throw CImgIOException(_cimglist_instance \
- "save_cimg(): Invalid size (%u,%u,%u,%u) of image[%u], for file '%s'.", \
- cimglist_instance, \
- W,H,D,C,l,filename?filename:"(FILE*)"); \
- if (W*H*D*C>0) { \
- if (l<n0 || x0>=W || y0>=H || z0>=D || c0>=D) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \
- else { \
- const CImg<T>& img = (*this)[l - n0]; \
- const T *ptrs = img._data; \
- const unsigned int \
- x1 = x0 + img._width - 1, \
- y1 = y0 + img._height - 1, \
- z1 = z0 + img._depth - 1, \
- c1 = c0 + img._spectrum - 1, \
- nx1 = x1>=W?W - 1:x1, \
- ny1 = y1>=H?H - 1:y1, \
- nz1 = z1>=D?D - 1:z1, \
- nc1 = c1>=C?C - 1:c1; \
- CImg<Tss> raw(1 + nx1 - x0); \
- const unsigned int skipvb = c0*W*H*D*sizeof(Tss); \
- if (skipvb) cimg::fseek(nfile,skipvb,SEEK_CUR); \
- for (unsigned int v = 1 + nc1 - c0; v; --v) { \
- const unsigned int skipzb = z0*W*H*sizeof(Tss); \
- if (skipzb) cimg::fseek(nfile,skipzb,SEEK_CUR); \
- for (unsigned int z = 1 + nz1 - z0; z; --z) { \
- const unsigned int skipyb = y0*W*sizeof(Tss); \
- if (skipyb) cimg::fseek(nfile,skipyb,SEEK_CUR); \
- for (unsigned int y = 1 + ny1 - y0; y; --y) { \
- const unsigned int skipxb = x0*sizeof(Tss); \
- if (skipxb) cimg::fseek(nfile,skipxb,SEEK_CUR); \
- raw.assign(ptrs, raw._width); \
- ptrs+=img._width; \
- if (endian) cimg::invert_endianness(raw._data,raw._width); \
- cimg::fwrite(raw._data,raw._width,nfile); \
- const unsigned int skipxe = (W - 1 - nx1)*sizeof(Tss); \
- if (skipxe) cimg::fseek(nfile,skipxe,SEEK_CUR); \
- } \
- const unsigned int skipye = (H - 1 - ny1)*W*sizeof(Tss); \
- if (skipye) cimg::fseek(nfile,skipye,SEEK_CUR); \
- } \
- const unsigned int skipze = (D - 1 - nz1)*W*H*sizeof(Tss); \
- if (skipze) cimg::fseek(nfile,skipze,SEEK_CUR); \
- } \
- const unsigned int skipve = (C - 1 - nc1)*W*H*D*sizeof(Tss); \
- if (skipve) cimg::fseek(nfile,skipve,SEEK_CUR); \
- } \
- } \
- } \
- saved = true; \
- }
- if (!file && !filename)
- throw CImgArgumentException(_cimglist_instance
- "save_cimg(): Specified filename is (null).",
- cimglist_instance);
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "save_cimg(): Empty instance, for file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- std::FILE *const nfile = file?file:cimg::fopen(filename,"rb+");
- bool saved = false, endian = cimg::endianness();
- CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
- *tmp = *str_pixeltype = *str_endian = 0;
- unsigned int j, N, W, H, D, C;
- int i, err;
- j = 0; while ((i=std::fgetc(nfile))!='\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;
- err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",&N,str_pixeltype._data,str_endian._data);
- if (err<2) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "save_cimg(): CImg header not found in file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)");
- }
- if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
- else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
- const unsigned int lmax = std::min(N,n0 + _width);
- _cimg_save_cimg_case("bool",bool);
- _cimg_save_cimg_case("unsigned_char",unsigned char);
- _cimg_save_cimg_case("uchar",unsigned char);
- _cimg_save_cimg_case("char",char);
- _cimg_save_cimg_case("unsigned_short",unsigned short);
- _cimg_save_cimg_case("ushort",unsigned short);
- _cimg_save_cimg_case("short",short);
- _cimg_save_cimg_case("unsigned_int",unsigned int);
- _cimg_save_cimg_case("uint",unsigned int);
- _cimg_save_cimg_case("int",int);
- _cimg_save_cimg_case("unsigned_int64",uint64T);
- _cimg_save_cimg_case("uint64",uint64T);
- _cimg_save_cimg_case("int64",int64T);
- _cimg_save_cimg_case("float",float);
- _cimg_save_cimg_case("double",double);
- if (!saved) {
- if (!file) cimg::fclose(nfile);
- throw CImgIOException(_cimglist_instance
- "save_cimg(): Unsupported data type '%s' for file '%s'.",
- cimglist_instance,
- filename?filename:"(FILE*)",str_pixeltype._data);
- }
- if (!file) cimg::fclose(nfile);
- return *this;
- }
- //! Insert the image instance into into an existing .cimg file, at specified coordinates.
- /**
- \param filename Filename to write data to.
- \param n0 Starting index of images to write.
- \param x0 Starting X-coordinates of image regions to write.
- \param y0 Starting Y-coordinates of image regions to write.
- \param z0 Starting Z-coordinates of image regions to write.
- \param c0 Starting C-coordinates of image regions to write.
- **/
- const CImgList<T>& save_cimg(const char *const filename,
- const unsigned int n0,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0) const {
- return _save_cimg(0,filename,n0,x0,y0,z0,c0);
- }
- //! Insert the image instance into into an existing .cimg file, at specified coordinates.
- /**
- \param file File to write data to.
- \param n0 Starting index of images to write.
- \param x0 Starting X-coordinates of image regions to write.
- \param y0 Starting Y-coordinates of image regions to write.
- \param z0 Starting Z-coordinates of image regions to write.
- \param c0 Starting C-coordinates of image regions to write.
- **/
- const CImgList<T>& save_cimg(std::FILE *const file,
- const unsigned int n0,
- const unsigned int x0, const unsigned int y0,
- const unsigned int z0, const unsigned int c0) const {
- return _save_cimg(file,0,n0,x0,y0,z0,c0);
- }
- static void _save_empty_cimg(std::FILE *const file, const char *const filename,
- const unsigned int nb,
- const unsigned int dx, const unsigned int dy,
- const unsigned int dz, const unsigned int dc) {
- std::FILE *const nfile = file?file:cimg::fopen(filename,"wb");
- const ulongT siz = (ulongT)dx*dy*dz*dc*sizeof(T);
- std::fprintf(nfile,"%u %s\n",nb,pixel_type());
- for (unsigned int i=nb; i; --i) {
- std::fprintf(nfile,"%u %u %u %u\n",dx,dy,dz,dc);
- for (ulongT off = siz; off; --off) std::fputc(0,nfile);
- }
- if (!file) cimg::fclose(nfile);
- }
- //! Save empty (non-compressed) .cimg file with specified dimensions.
- /**
- \param filename Filename to write data to.
- \param nb Number of images to write.
- \param dx Width of images in the written file.
- \param dy Height of images in the written file.
- \param dz Depth of images in the written file.
- \param dc Spectrum of images in the written file.
- **/
- static void save_empty_cimg(const char *const filename,
- const unsigned int nb,
- const unsigned int dx, const unsigned int dy=1,
- const unsigned int dz=1, const unsigned int dc=1) {
- return _save_empty_cimg(0,filename,nb,dx,dy,dz,dc);
- }
- //! Save empty .cimg file with specified dimensions.
- /**
- \param file File to write data to.
- \param nb Number of images to write.
- \param dx Width of images in the written file.
- \param dy Height of images in the written file.
- \param dz Depth of images in the written file.
- \param dc Spectrum of images in the written file.
- **/
- static void save_empty_cimg(std::FILE *const file,
- const unsigned int nb,
- const unsigned int dx, const unsigned int dy=1,
- const unsigned int dz=1, const unsigned int dc=1) {
- return _save_empty_cimg(file,0,nb,dx,dy,dz,dc);
- }
- //! Save list as a TIFF file.
- /**
- \param filename Filename to write data to.
- \param compression_type Compression mode used to write data.
- \param voxel_size Voxel size, to be stored in the filename.
- \param description Description, to be stored in the filename.
- \param use_bigtiff Allow to save big tiff files (>4Gb).
- **/
- const CImgList<T>& save_tiff(const char *const filename, const unsigned int compression_type=0,
- const float *const voxel_size=0, const char *const description=0,
- const bool use_bigtiff=true) const {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "save_tiff(): Specified filename is (null).",
- cimglist_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- #ifndef cimg_use_tiff
- if (_width==1) _data[0].save_tiff(filename,compression_type,voxel_size,description,use_bigtiff);
- else cimglist_for(*this,l) {
- CImg<charT> nfilename(1024);
- cimg::number_filename(filename,l,6,nfilename);
- _data[l].save_tiff(nfilename,compression_type,voxel_size,description,use_bigtiff);
- }
- #else
- ulongT siz = 0;
- cimglist_for(*this,l) siz+=_data[l].size();
- const bool _use_bigtiff = use_bigtiff && sizeof(siz)>=8 && siz*sizeof(T)>=1UL<<31; // No bigtiff for small images
- TIFF *tif = TIFFOpen(filename,_use_bigtiff?"w8":"w4");
- if (tif) {
- for (unsigned int dir = 0, l = 0; l<_width; ++l) {
- const CImg<T>& img = (*this)[l];
- cimg_forZ(img,z) img._save_tiff(tif,dir++,z,compression_type,voxel_size,description);
- }
- TIFFClose(tif);
- } else
- throw CImgIOException(_cimglist_instance
- "save_tiff(): Failed to open stream for file '%s'.",
- cimglist_instance,
- filename);
- #endif
- return *this;
- }
- //! Save list as a gzipped file, using external tool 'gzip'.
- /**
- \param filename Filename to write data to.
- **/
- const CImgList<T>& save_gzip_external(const char *const filename) const {
- if (!filename)
- throw CImgIOException(_cimglist_instance
- "save_gzip_external(): Specified filename is (null).",
- cimglist_instance);
- CImg<charT> command(1024), filename_tmp(256), body(256);
- const char
- *ext = cimg::split_filename(filename,body),
- *ext2 = cimg::split_filename(body,0);
- std::FILE *file;
- do {
- if (!cimg::strcasecmp(ext,"gz")) {
- if (*ext2) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext2);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- } else {
- if (*ext) cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext);
- else cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s.cimg",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- }
- if ((file=cimg::std_fopen(filename_tmp,"rb"))!=0) cimg::fclose(file);
- } while (file);
- if (is_saveable(body)) {
- save(filename_tmp);
- cimg_snprintf(command,command._width,"\"%s\" -c \"%s\" > \"%s\"",
- cimg::gzip_path(),
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::gzip_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimglist_instance
- "save_gzip_external(): Failed to save file '%s' with external command 'gzip'.",
- cimglist_instance,
- filename);
- else cimg::fclose(file);
- std::remove(filename_tmp);
- } else {
- CImg<charT> nfilename(1024);
- cimglist_for(*this,l) {
- cimg::number_filename(body,l,6,nfilename);
- if (*ext) cimg_sprintf(nfilename._data + std::strlen(nfilename),".%s",ext);
- _data[l].save_gzip_external(nfilename);
- }
- }
- return *this;
- }
- //! Save image sequence (using the OpenCV library when available).
- /**
- \param filename Filename to write data to.
- \param fps Number of frames per second.
- \param codec Type of compression (See http://www.fourcc.org/codecs.php to see available codecs).
- \param keep_open Tells if the video writer associated to the specified filename
- must be kept open or not (to allow frames to be added in the same file afterwards).
- **/
- const CImgList<T>& save_video(const char *const filename, const unsigned int fps=25,
- const char *codec=0, const bool keep_open=false) const {
- #ifndef cimg_use_opencv
- cimg::unused(codec,keep_open);
- return save_ffmpeg_external(filename,fps);
- #else
- try {
- static cv::VideoWriter *writers[32] = { 0 };
- static CImgList<charT> filenames(32);
- static CImg<intT> sizes(32,2,1,1,0);
- static int last_used_index = -1;
- // Detect if a video writer already exists for the specified filename.
- cimg::mutex(9);
- int index = -1;
- if (filename) {
- if (last_used_index>=0 && !std::strcmp(filename,filenames[last_used_index])) {
- index = last_used_index;
- } else cimglist_for(filenames,l) if (filenames[l] && !std::strcmp(filename,filenames[l])) {
- index = l; break;
- }
- } else index = last_used_index;
- cimg::mutex(9,0);
- // Find empty slot for capturing video stream.
- if (index<0) {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "save_video(): No already open video writer found. You must specify a "
- "non-(null) filename argument for the first call.",
- cimglist_instance);
- else { cimg::mutex(9); cimglist_for(filenames,l) if (!filenames[l]) { index = l; break; } cimg::mutex(9,0); }
- if (index<0)
- throw CImgIOException(_cimglist_instance
- "save_video(): File '%s', no video writer slots available. "
- "You have to release some of your previously opened videos.",
- cimglist_instance,filename);
- if (is_empty())
- throw CImgInstanceException(_cimglist_instance
- "save_video(): Instance list is empty.",
- cimglist_instance);
- const unsigned int W = _data?_data[0]._width:0, H = _data?_data[0]._height:0;
- if (!W || !H)
- throw CImgInstanceException(_cimglist_instance
- "save_video(): Frame [0] is an empty image.",
- cimglist_instance);
- const char
- *const _codec = codec && *codec?codec:"h264",
- codec0 = cimg::uppercase(_codec[0]),
- codec1 = _codec[0]?cimg::uppercase(_codec[1]):0,
- codec2 = _codec[1]?cimg::uppercase(_codec[2]):0,
- codec3 = _codec[2]?cimg::uppercase(_codec[3]):0;
- cimg::mutex(9);
- writers[index] = new cv::VideoWriter(filename,_cimg_fourcc(codec0,codec1,codec2,codec3),fps,cv::Size(W,H));
- if (!writers[index]->isOpened()) {
- delete writers[index];
- writers[index] = 0;
- cimg::mutex(9,0);
- throw CImgIOException(_cimglist_instance
- "save_video(): File '%s', unable to initialize video writer with codec '%c%c%c%c'.",
- cimglist_instance,filename,
- codec0,codec1,codec2,codec3);
- }
- CImg<charT>::string(filename).move_to(filenames[index]);
- sizes(index,0) = W;
- sizes(index,1) = H;
- cimg::mutex(9,0);
- }
- if (!is_empty()) {
- const unsigned int W = sizes(index,0), H = sizes(index,1);
- cimg::mutex(9);
- cimglist_for(*this,l) {
- CImg<T> &src = _data[l];
- if (src.is_empty())
- cimg::warn(_cimglist_instance
- "save_video(): Skip empty frame %d for file '%s'.",
- cimglist_instance,l,filename);
- if (src._spectrum>3)
- cimg::warn(_cimglist_instance
- "save_video(): Frame %u has incompatible dimension (%u,%u,%u,%u). "
- "Some image data may be ignored when writing frame into video file '%s'.",
- cimglist_instance,l,src._width,src._height,src._depth,src._spectrum,filename);
- cimg_forZ(src,z) {
- CImg<T> _src = src._depth>1?src.get_slice(z):src.get_shared();
- if (_src._width==W && _src._height==H && _src._spectrum==3)
- writers[index]->write(CImg<ucharT>(_src)._cimg2cvmat());
- else {
- CImg<ucharT> __src(_src,false);
- __src.channels(0,std::min(__src._spectrum - 1,2U)).resize(W,H);
- __src.resize(W,H,1,3,__src._spectrum==1);
- writers[index]->write(__src._cimg2cvmat());
- }
- }
- }
- cimg::mutex(9,0);
- }
- cimg::mutex(9);
- if (!keep_open) {
- delete writers[index];
- writers[index] = 0;
- filenames[index].assign();
- sizes(index,0) = sizes(index,1) = 0;
- last_used_index = -1;
- } else last_used_index = index;
- cimg::mutex(9,0);
- } catch (CImgIOException &e) {
- if (!keep_open) return save_ffmpeg_external(filename,fps);
- throw e;
- }
- return *this;
- #endif
- }
- //! Save image sequence, using the external tool 'ffmpeg'.
- /**
- \param filename Filename to write data to.
- \param fps Number of frames per second.
- \param codec Type of compression.
- \param bitrate Output bitrate
- **/
- const CImgList<T>& save_ffmpeg_external(const char *const filename, const unsigned int fps=25,
- const char *const codec=0, const unsigned int bitrate=2048) const {
- if (!filename)
- throw CImgArgumentException(_cimglist_instance
- "save_ffmpeg_external(): Specified filename is (null).",
- cimglist_instance);
- if (is_empty()) { cimg::fempty(0,filename); return *this; }
- const char
- *const ext = cimg::split_filename(filename),
- *const _codec = codec?codec:
- !cimg::strcasecmp(ext,"flv")?"flv":
- !cimg::strcasecmp(ext,"mp4")?"h264":"mpeg2video";
- CImg<charT> command(1024), filename_tmp(256), filename_tmp2(256);
- CImgList<charT> filenames;
- std::FILE *file = 0;
- cimglist_for(*this,l) if (!_data[l].is_sameXYZ(_data[0]))
- throw CImgInstanceException(_cimglist_instance
- "save_ffmpeg_external(): Invalid instance dimensions for file '%s'.",
- cimglist_instance,
- filename);
- do {
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s%c%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_000001.ppm",filename_tmp._data);
- if ((file=cimg::std_fopen(filename_tmp2,"rb"))!=0) cimg::fclose(file);
- } while (file);
- unsigned int frame = 1;
- cimglist_for(*this,l) {
- CImg<T>& src = _data[l];
- cimg_forZ(src,z) {
- cimg_snprintf(filename_tmp2,filename_tmp2._width,"%s_%.6u.ppm",filename_tmp._data,frame);
- CImg<charT>::string(filename_tmp2).move_to(filenames);
- CImg<T> _src = src._depth>1?src.get_slice(z):src.get_shared();
- if (_src._width%2 || _src._height%2) // Force output to have an even number of columns and rows
- _src.assign(_src.get_resize(_src._width + (_src._width%2),_src._height + (_src._height%2),1,-100,0),false);
- if (_src._spectrum!=3) // Force output to be one slice, in color
- _src.assign(_src.get_resize(-100,-100,1,3),false);
- _src.save_pnm(filename_tmp2);
- ++frame;
- }
- }
- cimg_snprintf(command,command._width,
- "\"%s\" -v -8 -y -i \"%s_%%6d.ppm\" -pix_fmt yuv420p -vcodec %s -b %uk -r %u \"%s\"",
- cimg::ffmpeg_path(),
- CImg<charT>::string(filename_tmp)._system_strescape().data(),
- _codec,bitrate,fps,
- CImg<charT>::string(filename)._system_strescape().data());
- cimg::system(command, cimg::ffmpeg_path());
- file = cimg::std_fopen(filename,"rb");
- if (!file)
- throw CImgIOException(_cimglist_instance
- "save_ffmpeg_external(): Failed to save file '%s' with external command 'ffmpeg'.",
- cimglist_instance,
- filename);
- else cimg::fclose(file);
- cimglist_for(*this,l) std::remove(filenames[l]);
- return *this;
- }
- //! Serialize a CImgList<T> instance into a raw CImg<unsigned char> buffer.
- /**
- \param is_compressed tells if zlib compression must be used for serialization
- (this requires 'cimg_use_zlib' been enabled).
- **/
- CImg<ucharT> get_serialize(const bool is_compressed=false) const {
- #ifndef cimg_use_zlib
- if (is_compressed)
- cimg::warn(_cimglist_instance
- "get_serialize(): Unable to compress data unless zlib is enabled, "
- "storing them uncompressed.",
- cimglist_instance);
- #endif
- CImgList<ucharT> stream;
- CImg<charT> tmpstr(128);
- const char *const ptype = pixel_type(), *const etype = cimg::endianness()?"big":"little";
- if (std::strstr(ptype,"unsigned")==ptype)
- cimg_snprintf(tmpstr,tmpstr._width,"%u unsigned_%s %s_endian\n",_width,ptype + 9,etype);
- else
- cimg_snprintf(tmpstr,tmpstr._width,"%u %s %s_endian\n",_width,ptype,etype);
- CImg<ucharT>::string(tmpstr,false).move_to(stream);
- cimglist_for(*this,l) {
- const CImg<T>& img = _data[l];
- cimg_snprintf(tmpstr,tmpstr._width,"%u %u %u %u",img._width,img._height,img._depth,img._spectrum);
- CImg<ucharT>::string(tmpstr,false).move_to(stream);
- if (img._data) {
- CImg<T> tmp;
- if (cimg::endianness()) { tmp = img; cimg::invert_endianness(tmp._data,tmp.size()); }
- const CImg<T>& ref = cimg::endianness()?tmp:img;
- bool failed_to_compress = true;
- if (is_compressed) {
- #ifdef cimg_use_zlib
- const ulongT siz = sizeof(T)*ref.size();
- uLongf csiz = (ulongT)compressBound(siz);
- Bytef *const cbuf = new Bytef[csiz];
- if (compress(cbuf,&csiz,(Bytef*)ref._data,siz))
- cimg::warn(_cimglist_instance
- "get_serialize(): Failed to save compressed data, saving them uncompressed.",
- cimglist_instance);
- else {
- cimg_snprintf(tmpstr,tmpstr._width," #%lu\n",csiz);
- CImg<ucharT>::string(tmpstr,false).move_to(stream);
- CImg<ucharT>(cbuf,csiz).move_to(stream);
- delete[] cbuf;
- failed_to_compress = false;
- }
- #endif
- }
- if (failed_to_compress) { // Write in a non-compressed way
- CImg<charT>::string("\n",false).move_to(stream);
- stream.insert(1);
- stream.back().assign((unsigned char*)ref._data,ref.size()*sizeof(T),1,1,1,true);
- }
- } else CImg<charT>::string("\n",false).move_to(stream);
- }
- cimglist_apply(stream,unroll)('y');
- return stream>'y';
- }
- //! Unserialize a CImg<unsigned char> serialized buffer into a CImgList<T> list.
- template<typename t>
- static CImgList<T> get_unserialize(const CImg<t>& buffer) {
- #ifdef cimg_use_zlib
- #define _cimgz_unserialize_case(Tss) { \
- Bytef *cbuf = 0; \
- if (sizeof(t)!=1 || buffer.pixel_type()==cimg::type<bool>::string()) { \
- cbuf = new Bytef[csiz]; Bytef *_cbuf = cbuf; \
- for (ulongT k = 0; k<csiz; ++k) *(_cbuf++) = (Bytef)*(stream++); \
- is_bytef = false; \
- } else { cbuf = (Bytef*)stream; stream+=csiz; is_bytef = true; } \
- raw.assign(W,H,D,C); \
- uLongf destlen = raw.size()*sizeof(Tss); \
- uncompress((Bytef*)raw._data,&destlen,cbuf,csiz); \
- if (!is_bytef) delete[] cbuf; \
- }
- #else
- #define _cimgz_unserialize_case(Tss) \
- throw CImgArgumentException("CImgList<%s>::get_unserialize(): Unable to unserialize compressed data " \
- "unless zlib is enabled.", \
- pixel_type());
- #endif
- #define _cimg_unserialize_case(Ts,Tss) \
- if (!loaded && !cimg::strcasecmp(Ts,str_pixeltype)) { \
- for (unsigned int l = 0; l<N; ++l) { \
- j = 0; while ((i=(int)*stream)!='\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; } \
- ++stream; tmp[j] = 0; \
- W = H = D = C = 0; csiz = 0; \
- if ((err = cimg_sscanf(tmp,"%u %u %u %u #" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \
- throw CImgArgumentException("CImgList<%s>::unserialize(): Invalid specified size (%u,%u,%u,%u) for " \
- "image #%u in serialized buffer.", \
- pixel_type(),W,H,D,C,l); \
- if (W*H*D*C>0) { \
- CImg<Tss> raw; \
- CImg<T> &img = res._data[l]; \
- if (err==5) _cimgz_unserialize_case(Tss) \
- else { \
- raw.assign(W,H,D,C); \
- CImg<ucharT> _raw((unsigned char*)raw._data,W*sizeof(Tss),H,D,C,true); \
- if (sizeof(t)==1) { std::memcpy(_raw,stream,_raw.size()); stream+=_raw.size(); } \
- else cimg_for(_raw,p,unsigned char) *p = (unsigned char)*(stream++); \
- } \
- if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \
- raw.move_to(img); \
- } \
- } \
- loaded = true; \
- }
- if (buffer.is_empty())
- throw CImgArgumentException("CImgList<%s>::get_unserialize(): Specified serialized buffer is (null).",
- pixel_type());
- CImgList<T> res;
- const t *stream = buffer._data, *const estream = buffer._data + buffer.size();
- bool loaded = false, endian = cimg::endianness(), is_bytef = false;
- CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);
- *tmp = *str_pixeltype = *str_endian = 0;
- unsigned int j, N = 0, W, H, D, C;
- uint64T csiz;
- int i, err;
- cimg::unused(is_bytef);
- do {
- j = 0; while ((i=(int)*stream)!='\n' && stream<estream && j<255) { ++stream; tmp[j++] = (char)i; }
- ++stream; tmp[j] = 0;
- } while (*tmp=='#' && stream<estream);
- err = cimg_sscanf(tmp,"%u%*c%255[A-Za-z64_]%*c%255[sA-Za-z_ ]",
- &N,str_pixeltype._data,str_endian._data);
- if (err<2)
- throw CImgArgumentException("CImgList<%s>::get_unserialize(): CImg header not found in serialized buffer.",
- pixel_type());
- if (!cimg::strncasecmp("little",str_endian,6)) endian = false;
- else if (!cimg::strncasecmp("big",str_endian,3)) endian = true;
- res.assign(N);
- _cimg_unserialize_case("bool",bool);
- _cimg_unserialize_case("unsigned_char",unsigned char);
- _cimg_unserialize_case("uchar",unsigned char);
- _cimg_unserialize_case("char",char);
- _cimg_unserialize_case("unsigned_short",unsigned short);
- _cimg_unserialize_case("ushort",unsigned short);
- _cimg_unserialize_case("short",short);
- _cimg_unserialize_case("unsigned_int",unsigned int);
- _cimg_unserialize_case("uint",unsigned int);
- _cimg_unserialize_case("int",int);
- _cimg_unserialize_case("unsigned_int64",uint64T);
- _cimg_unserialize_case("uint64",uint64T);
- _cimg_unserialize_case("int64",int64T);
- _cimg_unserialize_case("float",float);
- _cimg_unserialize_case("double",double);
- if (!loaded)
- throw CImgArgumentException("CImgList<%s>::get_unserialize(): Unsupported pixel type '%s' defined "
- "in serialized buffer.",
- pixel_type(),str_pixeltype._data);
- return res;
- }
- //@}
- //----------------------------------
- //
- //! \name Others
- //@{
- //----------------------------------
- //! Return a CImg pre-defined font with requested height.
- /**
- \param font_height Height of the desired font (exact match for 13,23,53,103).
- \param is_variable_width Decide if the font has a variable (\c true) or fixed (\c false) width.
- **/
- static const CImgList<ucharT>& font(const unsigned int requested_height, const bool is_variable_width=true) {
- if (!requested_height) return CImgList<ucharT>::const_empty();
- cimg::mutex(11);
- static const unsigned char font_resizemap[] = {
- 0, 4, 7, 9, 11, 13, 15, 17, 19, 21, 22, 24, 26, 27, 29, 30,
- 32, 33, 35, 36, 38, 39, 41, 42, 43, 45, 46, 47, 49, 50, 51, 52,
- 54, 55, 56, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 71, 72,
- 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
- 90, 91, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
- 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
- 123, 124, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
- 138, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 148, 149, 150, 151,
- 152, 153, 154, 155, 156, 157, 157, 158, 159, 160, 161, 162, 163, 164, 164, 165,
- 166, 167, 168, 169, 170, 170, 171, 172, 173, 174, 175, 176, 176, 177, 178, 179,
- 180, 181, 181, 182, 183, 184, 185, 186, 186, 187, 188, 189, 190, 191, 191, 192,
- 193, 194, 195, 196, 196, 197, 198, 199, 200, 200, 201, 202, 203, 204, 205, 205,
- 206, 207, 208, 209, 209, 210, 211, 212, 213, 213, 214, 215, 216, 216, 217, 218,
- 219, 220, 220, 221, 222, 223, 224, 224, 225, 226, 227, 227, 228, 229, 230, 231,
- 231, 232, 233, 234, 234, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243,
- 244, 244, 245, 246, 247, 247, 248, 249, 250, 250, 251, 252, 253, 253, 254, 255 };
- static const char *const *font_data[] = {
- cimg::data_font_small,
- cimg::data_font_normal,
- cimg::data_font_large,
- cimg::data_font_huge };
- static const unsigned int
- font_width[] = { 10,26,52,104 },
- font_height[] = { 13,32,64,128 },
- font_M[] = { 86,91,91,47 },
- font_chunk[] = { sizeof(cimg::data_font_small)/sizeof(char*),
- sizeof(cimg::data_font_normal)/sizeof(char*),
- sizeof(cimg::data_font_large)/sizeof(char*),
- sizeof(cimg::data_font_huge)/sizeof(char*) };
- static const unsigned char font_is_binary[] = { 1,0,0,1 };
- static CImg<ucharT> font_base[4];
- unsigned int ind =
- requested_height<=font_height[0]?0U:
- requested_height<=font_height[1]?1U:
- requested_height<=font_height[2]?2U:3U;
- // Decompress nearest base font data if needed.
- CImg<ucharT> &basef = font_base[ind];
- if (!basef) {
- basef.assign(256*font_width[ind],font_height[ind]);
- unsigned char *ptrd = basef;
- const unsigned char *const ptrde = basef.end();
- // Recompose font data from several chunks, to deal with MS compiler limit with big strings (64 Kb).
- CImg<char> dataf;
- for (unsigned int k = 0; k<font_chunk[ind]; ++k)
- dataf.append(CImg<char>::string(font_data[ind][k],k==font_chunk[ind] - 1,true),'x');
- // Uncompress font data (decode RLE).
- const unsigned int M = font_M[ind];
- if (font_is_binary[ind])
- for (const char *ptrs = dataf; *ptrs; ++ptrs) {
- const int _n = (int)(*ptrs - M - 32), v = _n>=0?255:0, n = _n>=0?_n:-_n;
- if (ptrd + n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; }
- else { std::memset(ptrd,v,ptrde - ptrd); break; }
- }
- else
- for (const char *ptrs = dataf; *ptrs; ++ptrs) {
- int n = (int)*ptrs - M - 32, v = 0;
- if (n>=0) { v = 85*n; n = 1; }
- else {
- n = -n;
- v = (int)*(++ptrs) - M - 32;
- if (v<0) { v = 0; --ptrs; } else v*=85;
- }
- if (ptrd + n<=ptrde) { std::memset(ptrd,v,n); ptrd+=n; }
- else { std::memset(ptrd,v,ptrde - ptrd); break; }
- }
- }
- // Find optimal font cache location to return.
- static CImgList<ucharT> fonts[16];
- static bool is_variable_widths[16] = { 0 };
- ind = ~0U;
- for (int i = 0; i<16; ++i)
- if (!fonts[i] || (is_variable_widths[i]==is_variable_width && requested_height==fonts[i][0]._height)) {
- ind = (unsigned int)i; break; // Found empty slot or cached font
- }
- if (ind==~0U) { // No empty slots nor existing font in cache
- fonts->assign();
- std::memmove((void*)fonts,(void*)(fonts + 1),15*sizeof(CImgList<ucharT>));
- std::memmove(is_variable_widths,is_variable_widths + 1,15*sizeof(bool));
- std::memset((void*)(fonts + (ind=15)),0,sizeof(CImgList<ucharT>)); // Free a slot in cache for new font
- }
- CImgList<ucharT> &font = fonts[ind];
- // Render requested font.
- if (!font) {
- is_variable_widths[ind] = is_variable_width;
- basef.get_split('x',256).move_to(font);
- // cimg::tic();
- if (requested_height!=font[0]._height)
- cimglist_for(font,l) {
- font[l].resize(std::max(1U,font[l]._width*requested_height/font[l]._height),requested_height,-100,-100,5);
- cimg_for(font[l],ptr,ucharT) *ptr = font_resizemap[*ptr];
- }
- // cimg::toc();
- // std::exit(0);
- if (is_variable_width) { // Crop font
- cimglist_for(font,l) {
- CImg<ucharT>& letter = font[l];
- int xmin = letter.width(), xmax = 0;
- cimg_forX(letter,x) { // Find xmin
- cimg_forY(letter,y) if (letter(x,y)) { xmin = x; break; }
- if (xmin!=letter.width()) break;
- }
- cimg_rofX(letter,x) { // Find xmax
- cimg_forY(letter,y) if (letter(x,y)) { xmax = x; break; }
- if (xmax) break;
- }
- if (xmin<=xmax) letter.crop(xmin,0,xmax,letter._height - 1);
- }
- font[(int)' '].resize(font[(int)'f']._width,-100,-100,-100,0);
- if (' ' + 256<font.size()) font[' ' + 256].resize(font[(int)'f']._width,-100,-100,-100,0);
- }
- font.insert(256,0);
- cimglist_for_in(font,0,255,l) font[l].assign(font[l + 256]._width,font[l + 256]._height,1,3,1);
- }
- cimg::mutex(11,0);
- return font;
- }
- //! Compute a 1D Fast Fourier Transform, along specified axis.
- /**
- \param axis Axis along which the Fourier transform is computed.
- \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed.
- **/
- CImgList<T>& FFT(const char axis, const bool invert=false) {
- if (is_empty()) return *this;
- if (_width==1) insert(1);
- if (_width>2)
- cimg::warn(_cimglist_instance
- "FFT(): Instance has more than 2 images",
- cimglist_instance);
- CImg<T>::FFT(_data[0],_data[1],axis,invert);
- return *this;
- }
- //! Compute a 1-D Fast Fourier Transform, along specified axis \newinstance.
- CImgList<Tfloat> get_FFT(const char axis, const bool invert=false) const {
- return CImgList<Tfloat>(*this,false).FFT(axis,invert);
- }
- //! Compute n-D Fast Fourier Transform.
- /**
- \param invert Tells if the direct (\c false) or inverse transform (\c true) is computed.
- **/
- CImgList<T>& FFT(const bool invert=false) {
- if (is_empty()) return *this;
- if (_width==1) insert(1);
- if (_width>2)
- cimg::warn(_cimglist_instance
- "FFT(): Instance has more than 2 images",
- cimglist_instance);
- CImg<T>::FFT(_data[0],_data[1],invert);
- return *this;
- }
- //! Compute n-D Fast Fourier Transform \newinstance.
- CImgList<Tfloat> get_FFT(const bool invert=false) const {
- return CImgList<Tfloat>(*this,false).FFT(invert);
- }
- //! Reverse primitives orientations of a 3D object.
- /**
- **/
- CImgList<T>& reverse_object3d() {
- cimglist_for(*this,l) {
- CImg<T>& p = _data[l];
- switch (p.size()) {
- case 2 : case 3: cimg::swap(p[0],p[1]); break;
- case 6 : cimg::swap(p[0],p[1],p[2],p[4],p[3],p[5]); break;
- case 9 : cimg::swap(p[0],p[1],p[3],p[5],p[4],p[6]); break;
- case 4 : cimg::swap(p[0],p[1],p[2],p[3]); break;
- 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;
- }
- }
- return *this;
- }
- //! Reverse primitives orientations of a 3D object \newinstance.
- CImgList<T> get_reverse_object3d() const {
- return (+*this).reverse_object3d();
- }
- //@}
- }; // struct CImgList { ...
- // Completion of previously declared functions
- //--------------------------------------------
- namespace cimg {
- // Functions to return standard streams 'stdin', 'stdout' and 'stderr'.
- // (throw a CImgIOException when macro 'cimg_use_r' is defined).
- inline FILE* _stdin(const bool throw_exception) {
- #ifndef cimg_use_r
- cimg::unused(throw_exception);
- return stdin;
- #else
- if (throw_exception) {
- cimg::exception_mode(0);
- throw CImgIOException("cimg::stdin(): Reference to 'stdin' stream not allowed in R mode "
- "('cimg_use_r' is defined).");
- }
- return 0;
- #endif
- }
- inline FILE* _stdout(const bool throw_exception) {
- #ifndef cimg_use_r
- cimg::unused(throw_exception);
- return stdout;
- #else
- if (throw_exception) {
- cimg::exception_mode(0);
- throw CImgIOException("cimg::stdout(): Reference to 'stdout' stream not allowed in R mode "
- "('cimg_use_r' is defined).");
- }
- return 0;
- #endif
- }
- inline FILE* _stderr(const bool throw_exception) {
- #ifndef cimg_use_r
- cimg::unused(throw_exception);
- return stderr;
- #else
- if (throw_exception) {
- cimg::exception_mode(0);
- throw CImgIOException("cimg::stderr(): Reference to 'stderr' stream not allowed in R mode "
- "('cimg_use_r' is defined).");
- }
- return 0;
- #endif
- }
- // Open a file (similar to std:: fopen(), but with wide character support on Windows).
- inline std::FILE *std_fopen(const char *const path, const char *const mode) {
- std::FILE *const res = std::fopen(path,mode);
- if (res) return res;
- #if cimg_OS==2
- // Try alternative method, with wide-character string.
- int err = MultiByteToWideChar(CP_UTF8,0,path,-1,0,0);
- if (err) {
- CImg<wchar_t> wpath((unsigned int)err);
- err = MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err);
- if (err) { // Convert 'mode' to a wide-character string
- err = MultiByteToWideChar(CP_UTF8,0,mode,-1,0,0);
- if (err) {
- CImg<wchar_t> wmode((unsigned int)err);
- if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wmode,err))
- return _wfopen(wpath,wmode);
- }
- }
- }
- #endif
- return 0;
- }
- //! Search path of an executable (Windows only).
- #if cimg_OS==2
- inline bool win_searchpath(const char *const exec_name, char *const res, const unsigned int size_res) {
- char *ptr = 0;
- DWORD err = SearchPathA(0,exec_name,0,size_res,res,&ptr);
- return err!=0;
- }
- #endif
- //! Get the file or directory attributes with support for UTF-8 paths (Windows only).
- #if cimg_OS==2
- inline DWORD win_getfileattributes(const char *const path) {
- DWORD res = GetFileAttributesA(path);
- if (res==INVALID_FILE_ATTRIBUTES) {
- // Try alternative method, with wide-character string.
- int err = MultiByteToWideChar(CP_UTF8,0,path,-1,0,0);
- if (err) {
- CImg<wchar_t> wpath((unsigned int)err);
- if (MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err)) res = GetFileAttributesW(wpath);
- }
- }
- return res;
- }
- #endif
- //! Get/set path to the <i>Program Files/</i> directory (Windows only).
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the program files.
- **/
- #if cimg_OS==2
- inline const char* win_programfiles_path(const char *const user_path=0, const bool reinit_path=false) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(MAX_PATH);
- *s_path = 0;
- // Note: in the following line, 0x26 = CSIDL_PROGRAM_FILES (not defined on every compiler).
- #if !defined(__INTEL_COMPILER)
- if (!SHGetSpecialFolderPathA(0,s_path,0x0026,false)) {
- const char *const pfPath = std::getenv("PROGRAMFILES");
- if (pfPath) std::strncpy(s_path,pfPath,MAX_PATH - 1);
- else std::strcpy(s_path,"C:\\PROGRA~1");
- }
- #else
- std::strcpy(s_path,"C:\\PROGRA~1");
- #endif
- }
- cimg::mutex(7,0);
- return s_path;
- }
- #endif
- //! Get/set path to the \c curl binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c curl binary.
- **/
- inline const char *curl_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("curl.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\curl.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"curl.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./curl");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"curl");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the \c dcraw binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c dcraw binary.
- **/
- inline const char *dcraw_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("dcraw.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\dcraw.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"dcraw.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./dcraw");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"dcraw");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the FFMPEG's \c ffmpeg binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c ffmpeg binary.
- **/
- inline const char *ffmpeg_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("ffmpeg.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\ffmpeg.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"ffmpeg.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./ffmpeg");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"ffmpeg");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the GraphicsMagick's \c gm binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c gm binary.
- **/
- inline const char* graphicsmagick_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("gm.exe",s_path,s_path._width)) path_found = true;
- const char *const pf_path = win_programfiles_path();
- if (!path_found) {
- std::strcpy(s_path,".\\gm.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%.2d-\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d-Q\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",pf_path,k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%.2d-\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d-Q\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%.2d-\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d-Q\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%.2d-\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d-Q\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\GRAPHI~1.%d\\VISUA~1\\BIN\\gm.exe",k);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gm.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./gm");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gm");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the \c gunzip binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c gunzip binary.
- **/
- inline const char *gunzip_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("gunzip.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\gunzip.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gunzip.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./gunzip");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gunzip");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the \c gzip binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c gzip binary.
- **/
- inline const char *gzip_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("gzip.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\gzip.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gzip.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./gzip");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"gzip");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the ImageMagick's \c convert binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c convert binary.
- **/
- inline const char* imagemagick_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("magick.exe",s_path,s_path._width)) path_found = true;
- const char *const pf_path = win_programfiles_path();
- for (int l = 0; l<2 && !path_found; ++l) {
- const char *const s_exe = l?"convert":"magick";
- cimg_snprintf(s_path,s_path._width,".\\%s.exe",s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%.2d-\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d-Q\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"%s\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",pf_path,k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%.2d-\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d-Q\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"C:\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%.2d-\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d-Q\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=10 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%.2d-\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 9; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d-Q\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- for (int k = 32; k>=0 && !path_found; --k) {
- cimg_snprintf(s_path,s_path._width,"D:\\IMAGEM~1.%d\\VISUA~1\\BIN\\%s.exe",k,s_exe);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) cimg_snprintf(s_path,s_path._width,"%s.exe",s_exe);
- }
- #else
- std::strcpy(s_path,"./magick");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- if (!path_found) {
- std::strcpy(s_path,"./convert");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"convert");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the Medcon's \c medcon binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c medcon binary.
- **/
- inline const char* medcon_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("medcon.exe",s_path,s_path._width)) path_found = true;
- const char *const pf_path = win_programfiles_path();
- if (!path_found) {
- std::strcpy(s_path,".\\medcon.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) {
- cimg_snprintf(s_path,s_path._width,"%s\\XMedCon\\bin\\medcon.bat",pf_path);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) {
- cimg_snprintf(s_path,s_path._width,"%s\\XMedCon\\bin\\medcon.exe",pf_path);
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) {
- std::strcpy(s_path,"C:\\XMedCon\\bin\\medcon.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"medcon.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./medcon");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"medcon");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to store temporary files.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path where temporary files can be saved.
- **/
- inline const char* temporary_path(const char *const user_path, const bool reinit_path) {
- #define _cimg_test_temporary_path(p) \
- if (!path_found) { \
- cimg_snprintf(s_path,s_path._width,"%s",p); \
- cimg_snprintf(tmp,tmp._width,"%s%c%s",s_path.data(),cimg_file_separator,filename_tmp._data); \
- if ((file=cimg::std_fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; } \
- }
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- CImg<char> tmp(1024), filename_tmp(256);
- std::FILE *file = 0;
- cimg_snprintf(filename_tmp,filename_tmp._width,"%s.tmp",cimg::filenamerand());
- char *tmpPath = std::getenv("TMP");
- if (!tmpPath) { tmpPath = std::getenv("TEMP"); winformat_string(tmpPath); }
- if (tmpPath) _cimg_test_temporary_path(tmpPath);
- #if cimg_OS==2
- _cimg_test_temporary_path("C:\\WINNT\\Temp");
- _cimg_test_temporary_path("C:\\WINDOWS\\Temp");
- _cimg_test_temporary_path("C:\\Temp");
- _cimg_test_temporary_path("C:");
- _cimg_test_temporary_path("D:\\WINNT\\Temp");
- _cimg_test_temporary_path("D:\\WINDOWS\\Temp");
- _cimg_test_temporary_path("D:\\Temp");
- _cimg_test_temporary_path("D:");
- #else
- _cimg_test_temporary_path("/tmp");
- _cimg_test_temporary_path("/var/tmp");
- #endif
- if (!path_found) {
- *s_path = 0;
- std::strncpy(tmp,filename_tmp,tmp._width - 1);
- if ((file=cimg::std_fopen(tmp,"wb"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; }
- }
- if (!path_found) {
- cimg::mutex(7,0);
- throw CImgIOException("cimg::temporary_path(): Failed to locate path for writing temporary files.\n");
- }
- }
- cimg::mutex(7,0);
- return s_path;
- }
- //! Get/set path to the \c wget binary.
- /**
- \param user_path Specified path, or \c 0 to get the path currently used.
- \param reinit_path Force path to be recalculated (may take some time).
- \return Path containing the \c wget binary.
- **/
- inline const char *wget_path(const char *const user_path, const bool reinit_path) {
- static CImg<char> s_path;
- cimg::mutex(7);
- if (reinit_path) s_path.assign();
- if (user_path) {
- if (!s_path) s_path.assign(1024);
- std::strncpy(s_path,user_path,1023);
- } else if (!s_path) {
- s_path.assign(1024);
- bool path_found = false;
- std::FILE *file = 0;
- #if cimg_OS==2
- if (win_searchpath("wget.exe",s_path,s_path._width)) path_found = true;
- if (!path_found) {
- std::strcpy(s_path,".\\wget.exe");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"wget.exe");
- #else
- if (!path_found) {
- std::strcpy(s_path,"./wget");
- if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; }
- }
- if (!path_found) std::strcpy(s_path,"wget");
- #endif
- winformat_string(s_path);
- }
- cimg::mutex(7,0);
- return s_path;
- }
- // [internal] Sorting function, used by cimg::files().
- inline int _sort_files(const void* a, const void* b) {
- const CImg<char> &sa = *(CImg<char>*)a, &sb = *(CImg<char>*)b;
- return std::strcmp(sa._data,sb._data);
- }
- //! Generate a numbered version of a filename.
- inline char* number_filename(const char *const filename, const int number,
- const unsigned int digits, char *const str) {
- if (!filename) { if (str) *str = 0; return 0; }
- const unsigned int siz = (unsigned int)std::strlen(filename);
- CImg<char> format(16), body(siz + 32);
- const char *const ext = cimg::split_filename(filename,body);
- if (*ext) cimg_snprintf(format,format._width,"%%s_%%.%ud.%%s",digits);
- else cimg_snprintf(format,format._width,"%%s_%%.%ud",digits);
- cimg_snprintf(str,1024,format._data,body._data,number,ext);
- return str;
- }
- //! Return list of files/directories in specified directory.
- /**
- \param path Path to the directory. Set to 0 for current directory.
- \param is_pattern Tell if specified path has a matching pattern in it.
- \param mode Output type, can be primary { 0=files only | 1=folders only | 2=files + folders }.
- \param include_path Tell if \c path must be included in resulting filenames.
- \return A list of filenames.
- **/
- inline CImgList<char> files(const char *const path, const bool is_pattern=false,
- const unsigned int mode=2, const bool include_path=false) {
- if (!path || !*path) return files("*",true,mode,include_path);
- CImgList<char> res;
- // If path is a valid folder name, ignore argument 'is_pattern'.
- const bool _is_pattern = is_pattern && !cimg::is_directory(path);
- bool is_root = false, is_current = false;
- cimg::unused(is_root,is_current);
- // Clean format of input path.
- CImg<char> pattern, _path = CImg<char>::string(path);
- #if cimg_OS==2
- for (char *ps = _path; *ps; ++ps) if (*ps=='\\') *ps='/';
- #endif
- char *pd = _path;
- for (char *ps = pd; *ps; ++ps) { if (*ps!='/' || *ps!=*(ps+1)) *(pd++) = *ps; }
- *pd = 0;
- unsigned int lp = (unsigned int)std::strlen(_path);
- if (!_is_pattern && lp && _path[lp - 1]=='/') {
- _path[lp - 1] = 0; --lp;
- #if cimg_OS!=2
- is_root = !*_path;
- #endif
- }
- // Separate folder path and matching pattern.
- if (_is_pattern) {
- const unsigned int bpos = (unsigned int)(cimg::basename(_path,'/') - _path.data());
- CImg<char>::string(_path).move_to(pattern);
- if (bpos) {
- _path[bpos - 1] = 0; // End 'path' at last slash
- #if cimg_OS!=2
- is_root = !*_path;
- #endif
- } else { // No path to folder specified, assuming current folder
- is_current = true; *_path = 0;
- }
- lp = (unsigned int)std::strlen(_path);
- }
- // Windows version.
- #if cimg_OS==2
- if (!_is_pattern) {
- pattern.assign(lp + 3);
- std::memcpy(pattern,_path,lp);
- pattern[lp] = '/'; pattern[lp + 1] = '*'; pattern[lp + 2] = 0;
- }
- WIN32_FIND_DATAA file_data;
- const HANDLE dir = FindFirstFileA(pattern.data(),&file_data);
- if (dir==INVALID_HANDLE_VALUE) return CImgList<char>::const_empty();
- do {
- const char *const filename = file_data.cFileName;
- if (*filename!='.' || (filename[1] && (filename[1]!='.' || filename[2]))) {
- const unsigned int lf = (unsigned int)std::strlen(filename);
- const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0;
- if ((!mode && !is_directory) || (mode==1 && is_directory) || mode>=2) {
- if (include_path) {
- CImg<char> full_filename((lp?lp+1:0) + lf + 1);
- if (lp) { std::memcpy(full_filename,_path,lp); full_filename[lp] = '/'; }
- std::memcpy(full_filename._data + (lp?lp + 1:0),filename,lf + 1);
- full_filename.move_to(res);
- } else CImg<char>(filename,lf + 1).move_to(res);
- }
- }
- } while (FindNextFileA(dir,&file_data));
- FindClose(dir);
- // Unix version (posix).
- #elif cimg_OS == 1
- DIR *const dir = opendir(is_root?"/":is_current?".":_path.data());
- if (!dir) return CImgList<char>::const_empty();
- struct dirent *ent;
- while ((ent=readdir(dir))!=0) {
- const char *const filename = ent->d_name;
- if (*filename!='.' || (filename[1] && (filename[1]!='.' || filename[2]))) {
- const unsigned int lf = (unsigned int)std::strlen(filename);
- CImg<char> full_filename(lp + lf + 2);
- if (!is_current) {
- full_filename.assign(lp + lf + 2);
- if (lp) std::memcpy(full_filename,_path,lp);
- full_filename[lp] = '/';
- std::memcpy(full_filename._data + lp + 1,filename,lf + 1);
- } else full_filename.assign(filename,lf + 1);
- struct stat st;
- if (stat(full_filename,&st)==-1) continue;
- const bool is_directory = (st.st_mode & S_IFDIR)!=0;
- if ((!mode && !is_directory) || (mode==1 && is_directory) || mode==2) {
- if (include_path) {
- if (!_is_pattern || (_is_pattern && !fnmatch(pattern,full_filename,0)))
- full_filename.move_to(res);
- } else {
- if (!_is_pattern || (_is_pattern && !fnmatch(pattern,full_filename,0)))
- CImg<char>(filename,lf + 1).move_to(res);
- }
- }
- }
- }
- closedir(dir);
- #endif
- // Sort resulting list by lexicographic order.
- if (res._width>=2) std::qsort(res._data,res._width,sizeof(CImg<char>),_sort_files);
- return res;
- }
- //! Try to guess format from an image file.
- /**
- \param file Input file (can be \c 0 if \c filename is set).
- \param filename Filename, as a C-string (can be \c 0 if \c file is set).
- \return C-string containing the guessed file format, or \c 0 if nothing has been guessed.
- **/
- inline const char *ftype(std::FILE *const file, const char *const filename) {
- if (!file && !filename)
- throw CImgArgumentException("cimg::ftype(): Specified filename is (null).");
- static const char
- *const _bmp = "bmp",
- *const _cr2 = "cr2",
- *const _dcm = "dcm",
- *const _gif = "gif",
- *const _inr = "inr",
- *const _jpg = "jpg",
- *const _off = "off",
- *const _pan = "pan",
- *const _pfm = "pfm",
- *const _png = "png",
- *const _pnm = "pnm",
- *const _tif = "tif";
- const char *f_type = 0;
- CImg<char> header;
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try {
- header._load_raw(file,filename,512,1,1,1,false,false,0);
- const unsigned char *const uheader = (unsigned char*)header._data;
- if (!std::strncmp(header,"OFF\n",4)) f_type = _off; // OFF
- else if (!std::strncmp(header,"#INRIMAGE",9)) // INRIMAGE
- f_type = _inr;
- else if (!std::strncmp(header,"PANDORE",7)) // PANDORE
- f_type = _pan;
- else if (!std::strncmp(header.data() + 128,"DICM",4)) // DICOM
- f_type = _dcm;
- else if (uheader[0]==0xFF && uheader[1]==0xD8 && uheader[2]==0xFF) // JPEG
- f_type = _jpg;
- else if (header[0]=='B' && header[1]=='M') // BMP
- f_type = _bmp;
- else if (header[0]=='G' && header[1]=='I' && header[2]=='F' && header[3]=='8' && header[5]=='a' &&
- (header[4]=='7' || header[4]=='9')) // GIF
- f_type = _gif;
- else if (uheader[0]==0x89 && uheader[1]==0x50 && uheader[2]==0x4E && uheader[3]==0x47 &&
- uheader[4]==0x0D && uheader[5]==0x0A && uheader[6]==0x1A && uheader[7]==0x0A) // PNG
- f_type = _png;
- else if (uheader[0]==0x49 && uheader[1]==0x49 && uheader[2]==0x2A && uheader[3]==0x00 && // CR2
- uheader[4]==0x10 && uheader[5]==0x00 && uheader[6]==0x00 && uheader[7]==0x00 &&
- uheader[8]==0x43 && uheader[9]==0x52)
- f_type = _cr2;
- else if ((uheader[0]==0x49 && uheader[1]==0x49 && uheader[2]==0x2A && uheader[3]==0x00) ||
- (uheader[0]==0x4D && uheader[1]==0x4D && uheader[2]==0x00 && uheader[3]==0x2A)) // TIFF
- f_type = _tif;
- else { // PNM or PFM
- CImgList<char> _header = header.get_split(CImg<char>::vector('\n'),0,false);
- cimglist_for(_header,l) {
- if (_header(l,0)=='#') continue;
- if (_header[l]._width==2 && _header(l,0)=='P') {
- const char c = _header(l,1);
- if (c=='f' || c=='F') { f_type = _pfm; break; }
- if (c>='1' && c<='9') { f_type = _pnm; break; }
- }
- f_type = 0; break;
- }
- }
- } catch (CImgIOException&) { }
- cimg::exception_mode(omode);
- return f_type;
- }
- //! Load file from network as a local temporary file.
- /**
- \param url URL of the filename, as a C-string.
- \param[out] filename_local C-string containing the path to a local copy of \c filename.
- \param timeout Maximum time (in seconds) authorized for downloading the file from the URL.
- \param try_fallback When using libcurl, tells using system calls as fallbacks in case of libcurl failure.
- \param referer Referer used, as a C-string.
- \return Value of \c filename_local.
- \note Use the \c libcurl library, or the external binaries \c wget or \c curl to perform the download.
- **/
- inline char *load_network(const char *const url, char *const filename_local,
- const unsigned int timeout, const bool try_fallback,
- const char *const referer) {
- if (!url)
- throw CImgArgumentException("cimg::load_network(): Specified URL is (null).");
- if (!filename_local)
- throw CImgArgumentException("cimg::load_network(): Specified destination string is (null).");
- if (!network_mode())
- throw CImgIOException("cimg::load_network(): Loading files from network is disabled.");
- const char *const __ext = cimg::split_filename(url), *const _ext = (*__ext && __ext>url)?__ext - 1:__ext;
- CImg<char> ext = CImg<char>::string(_ext);
- std::FILE *file = 0;
- *filename_local = 0;
- if (ext._width>16 || !cimg::strncasecmp(ext,"cgi",3)) *ext = 0;
- else cimg::strwindows_reserved(ext);
- do {
- cimg_snprintf(filename_local,256,"%s%c%s%s",
- cimg::temporary_path(),cimg_file_separator,cimg::filenamerand(),ext._data);
- if ((file=cimg::std_fopen(filename_local,"rb"))!=0) cimg::fclose(file);
- } while (file);
- #ifdef cimg_use_curl
- const unsigned int omode = cimg::exception_mode();
- cimg::exception_mode(0);
- try {
- CURL *curl = 0;
- CURLcode res;
- curl = curl_easy_init();
- if (curl) {
- file = cimg::fopen(filename_local,"wb");
- curl_easy_setopt(curl,CURLOPT_URL,url);
- curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,0);
- curl_easy_setopt(curl,CURLOPT_WRITEDATA,file);
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);
- curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L);
- curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
- if (timeout) curl_easy_setopt(curl,CURLOPT_TIMEOUT,(long)timeout);
- if (std::strchr(url,'?')) curl_easy_setopt(curl,CURLOPT_HTTPGET,1L);
- if (referer) curl_easy_setopt(curl,CURLOPT_REFERER,referer);
- res = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- cimg::fseek(file,0,SEEK_END); // Check if file size is 0
- const cimg_ulong siz = cimg::ftell(file);
- cimg::fclose(file);
- if (siz>0 && res==CURLE_OK) {
- cimg::exception_mode(omode);
- return filename_local;
- } else std::remove(filename_local);
- }
- } catch (...) { }
- cimg::exception_mode(omode);
- if (!try_fallback) throw CImgIOException("cimg::load_network(): Failed to load file '%s' with libcurl.",url);
- #endif
- CImg<char> command((unsigned int)std::strlen(url) + 64);
- cimg::unused(try_fallback);
- // Try with 'curl' first.
- if (timeout) {
- if (referer)
- cimg_snprintf(command,command._width,"\"%s\" -e %s -m %u -f --silent --compressed -o \"%s\" \"%s\"",
- cimg::curl_path(),referer,timeout,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- else
- cimg_snprintf(command,command._width,"\"%s\" -m %u -f --silent --compressed -o \"%s\" \"%s\"",
- cimg::curl_path(),timeout,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- } else {
- if (referer)
- cimg_snprintf(command,command._width,"\"%s\" -e %s -f --silent --compressed -o \"%s\" \"%s\"",
- cimg::curl_path(),referer,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- else
- cimg_snprintf(command,command._width,"\"%s\" -f --silent --compressed -o \"%s\" \"%s\"",
- cimg::curl_path(),filename_local,
- CImg<char>::string(url)._system_strescape().data());
- }
- cimg::system(command, cimg::curl_path());
- if (!(file=cimg::std_fopen(filename_local,"rb"))) {
- // Try with 'wget' otherwise.
- if (timeout) {
- if (referer)
- cimg_snprintf(command,command._width,"\"%s\" --referer=%s -T %u -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
- cimg::wget_path(),referer,timeout,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- else
- cimg_snprintf(command,command._width,"\"%s\" -T %u -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
- cimg::wget_path(),timeout,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- } else {
- if (referer)
- cimg_snprintf(command,command._width,"\"%s\" --referer=%s -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
- cimg::wget_path(),referer,filename_local,
- CImg<char>::string(url)._system_strescape().data());
- else
- cimg_snprintf(command,command._width,"\"%s\" -q -r -l 0 --no-cache -O \"%s\" \"%s\"",
- cimg::wget_path(),filename_local,
- CImg<char>::string(url)._system_strescape().data());
- }
- cimg::system(command, cimg::wget_path());
- if (!(file=cimg::std_fopen(filename_local,"rb")))
- throw CImgIOException("cimg::load_network(): Failed to load file '%s' with external commands "
- "'wget' or 'curl'.",url);
- cimg::fclose(file);
- // Try gunzip it.
- cimg_snprintf(command,command._width,"%s.gz",filename_local);
- std::rename(filename_local,command);
- cimg_snprintf(command,command._width,"\"%s\" --quiet \"%s.gz\"",
- gunzip_path(),filename_local);
- cimg::system(command, gunzip_path());
- file = cimg::std_fopen(filename_local,"rb");
- if (!file) {
- cimg_snprintf(command,command._width,"%s.gz",filename_local);
- std::rename(command,filename_local);
- file = cimg::std_fopen(filename_local,"rb");
- }
- }
- cimg::fseek(file,0,SEEK_END); // Check if file size is 0
- if (std::ftell(file)<=0)
- throw CImgIOException("cimg::load_network(): Failed to load URL '%s' with external commands "
- "'wget' or 'curl'.",url);
- cimg::fclose(file);
- return filename_local;
- }
- // Implement a tic/toc mechanism to display elapsed time of algorithms.
- inline cimg_uint64 tictoc(const bool is_tic) {
- cimg::mutex(2);
- static CImg<cimg_uint64> times(64);
- static unsigned int pos = 0;
- const cimg_uint64 t1 = cimg::time();
- if (is_tic) {
- // Tic
- times[pos++] = t1;
- if (pos>=times._width)
- throw CImgArgumentException("cimg::tic(): Too much calls to 'cimg::tic()' without calls to 'cimg::toc()'.");
- cimg::mutex(2,0);
- return t1;
- }
- // Toc
- if (!pos)
- throw CImgArgumentException("cimg::toc(): No previous call to 'cimg::tic()' has been made.");
- const cimg_uint64
- t0 = times[--pos],
- dt = t1>=t0?(t1 - t0):cimg::type<cimg_uint64>::max();
- const unsigned int
- edays = (unsigned int)(dt/86400000.),
- ehours = (unsigned int)((dt - edays*86400000.)/3600000.),
- emin = (unsigned int)((dt - edays*86400000. - ehours*3600000.)/60000.),
- esec = (unsigned int)((dt - edays*86400000. - ehours*3600000. - emin*60000.)/1000.),
- ems = (unsigned int)(dt - edays*86400000. - ehours*3600000. - emin*60000. - esec*1000.);
- if (!edays && !ehours && !emin && !esec)
- std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u ms%s\n",
- cimg::t_red,1 + 2*pos,"",ems,cimg::t_normal);
- else {
- if (!edays && !ehours && !emin)
- std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u sec %u ms%s\n",
- cimg::t_red,1 + 2*pos,"",esec,ems,cimg::t_normal);
- else {
- if (!edays && !ehours)
- std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u min %u sec %u ms%s\n",
- cimg::t_red,1 + 2*pos,"",emin,esec,ems,cimg::t_normal);
- else{
- if (!edays)
- std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u hours %u min %u sec %u ms%s\n",
- cimg::t_red,1 + 2*pos,"",ehours,emin,esec,ems,cimg::t_normal);
- else{
- std::fprintf(cimg::output(),"%s[CImg]%*sElapsed time: %u days %u hours %u min %u sec %u ms%s\n",
- cimg::t_red,1 + 2*pos,"",edays,ehours,emin,esec,ems,cimg::t_normal);
- }
- }
- }
- }
- cimg::mutex(2,0);
- return dt;
- }
- // Return a temporary string describing the size of a memory buffer.
- inline const char *strbuffersize(const cimg_ulong size) {
- static CImg<char> res(256);
- cimg::mutex(5);
- if (size<1024LU) cimg_snprintf(res,res._width,"%lu byte%s",(unsigned long)size,size>1?"s":"");
- else if (size<1024*1024LU) { const float nsize = size/1024.f; cimg_snprintf(res,res._width,"%.1f Kio",nsize); }
- else if (size<1024*1024*1024LU) {
- const float nsize = size/(1024*1024.f); cimg_snprintf(res,res._width,"%.1f Mio",nsize);
- } else { const float nsize = size/(1024*1024*1024.f); cimg_snprintf(res,res._width,"%.1f Gio",nsize); }
- cimg::mutex(5,0);
- return res;
- }
- //! Display a simple dialog box, and wait for the user's response.
- /**
- \param title Title of the dialog window.
- \param msg Main message displayed inside the dialog window.
- \param button1_label Label of the 1st button.
- \param button2_label Label of the 2nd button (\c 0 to hide button).
- \param button3_label Label of the 3rd button (\c 0 to hide button).
- \param button4_label Label of the 4th button (\c 0 to hide button).
- \param button5_label Label of the 5th button (\c 0 to hide button).
- \param button6_label Label of the 6th button (\c 0 to hide button).
- \param logo Image logo displayed at the left of the main message.
- \param is_centered Tells if the dialog window must be centered on the screen.
- \return Index of clicked button (from \c 0 to \c 5), or \c -1 if the dialog window has been closed by the user.
- \note
- - Up to 6 buttons can be defined in the dialog window.
- - The function returns when a user clicked one of the button or closed the dialog window.
- - If a button text is set to 0, the corresponding button (and the following) will not appear in the dialog box.
- At least one button must be specified.
- **/
- template<typename t>
- inline int dialog(const char *const title, const char *const msg,
- const char *const button1_label, const char *const button2_label,
- const char *const button3_label, const char *const button4_label,
- const char *const button5_label, const char *const button6_label,
- const CImg<t>& logo, const bool is_centered=false) {
- #if cimg_display==0
- cimg::unused(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label,
- logo._data,is_centered);
- throw CImgIOException("cimg::dialog(): No display available.");
- #else
- static const unsigned char
- black[] = { 0,0,0 }, white[] = { 255,255,255 }, gray[] = { 200,200,200 }, gray2[] = { 150,150,150 };
- // Create buttons and canvas graphics
- CImgList<unsigned char> buttons, cbuttons, sbuttons;
- if (button1_label) {
- CImg<unsigned char>().draw_text(0,0,button1_label,black,gray,1,13).move_to(buttons);
- if (button2_label) {
- CImg<unsigned char>().draw_text(0,0,button2_label,black,gray,1,13).move_to(buttons);
- if (button3_label) {
- CImg<unsigned char>().draw_text(0,0,button3_label,black,gray,1,13).move_to(buttons);
- if (button4_label) {
- CImg<unsigned char>().draw_text(0,0,button4_label,black,gray,1,13).move_to(buttons);
- if (button5_label) {
- CImg<unsigned char>().draw_text(0,0,button5_label,black,gray,1,13).move_to(buttons);
- if (button6_label) {
- CImg<unsigned char>().draw_text(0,0,button6_label,black,gray,1,13).move_to(buttons);
- }}}}}}
- if (!buttons._width)
- throw CImgArgumentException("cimg::dialog(): No buttons have been defined.");
- cimglist_for(buttons,l) buttons[l].resize(-100,-100,1,3);
- unsigned int bw = 0, bh = 0;
- cimglist_for(buttons,l) { bw = std::max(bw,buttons[l]._width); bh = std::max(bh,buttons[l]._height); }
- bw+=8; bh+=8;
- if (bw<64) bw = 64;
- if (bw>128) bw = 128;
- if (bh<24) bh = 24;
- if (bh>48) bh = 48;
- CImg<unsigned char> button(bw,bh,1,3);
- button.draw_rectangle(0,0,bw - 1,bh - 1,gray);
- button.draw_line(0,0,bw - 1,0,white).draw_line(0,bh - 1,0,0,white);
- button.draw_line(bw - 1,0,bw - 1,bh - 1,black).draw_line(bw - 1,bh - 1,0,bh - 1,black);
- button.draw_line(1,bh - 2,bw - 2,bh - 2,gray2).draw_line(bw - 2,bh - 2,bw - 2,1,gray2);
- CImg<unsigned char> sbutton(bw,bh,1,3);
- sbutton.draw_rectangle(0,0,bw - 1,bh - 1,gray);
- sbutton.draw_line(0,0,bw - 1,0,black).draw_line(bw - 1,0,bw - 1,bh - 1,black);
- sbutton.draw_line(bw - 1,bh - 1,0,bh - 1,black).draw_line(0,bh - 1,0,0,black);
- sbutton.draw_line(1,1,bw - 2,1,white).draw_line(1,bh - 2,1,1,white);
- sbutton.draw_line(bw - 2,1,bw - 2,bh - 2,black).draw_line(bw - 2,bh - 2,1,bh - 2,black);
- sbutton.draw_line(2,bh - 3,bw - 3,bh - 3,gray2).draw_line(bw - 3,bh - 3,bw - 3,2,gray2);
- sbutton.draw_line(4,4,bw - 5,4,black,1,0xAAAAAAAA,true).
- draw_line(bw - 5,4,bw - 5,bh - 5,black,1,0xAAAAAAAA,false);
- sbutton.draw_line(bw - 5,bh - 5,4,bh - 5,black,1,0xAAAAAAAA,false).
- draw_line(4,bh - 5,4,4,black,1,0xAAAAAAAA,false);
- CImg<unsigned char> cbutton(bw,bh,1,3);
- cbutton.draw_rectangle(0,0,bw - 1,bh - 1,black).draw_rectangle(1,1,bw - 2,bh - 2,gray2).
- draw_rectangle(2,2,bw - 3,bh - 3,gray);
- cbutton.draw_line(4,4,bw - 5,4,black,1,0xAAAAAAAA,true).
- draw_line(bw - 5,4,bw - 5,bh - 5,black,1,0xAAAAAAAA,false);
- cbutton.draw_line(bw - 5,bh - 5,4,bh - 5,black,1,0xAAAAAAAA,false).
- draw_line(4,bh - 5,4,4,black,1,0xAAAAAAAA,false);
- cimglist_for(buttons,ll) {
- CImg<unsigned char>(cbutton).
- draw_image(1 + (bw -buttons[ll].width())/2,1 + (bh - buttons[ll].height())/2,buttons[ll]).
- move_to(cbuttons);
- CImg<unsigned char>(sbutton).
- draw_image((bw - buttons[ll].width())/2,(bh - buttons[ll].height())/2,buttons[ll]).
- move_to(sbuttons);
- CImg<unsigned char>(button).
- draw_image((bw - buttons[ll].width())/2,(bh - buttons[ll].height())/2,buttons[ll]).
- move_to(buttons[ll]);
- }
- CImg<unsigned char> canvas;
- if (msg)
- ((CImg<unsigned char>().draw_text(0,0,"%s",gray,0,1,13,msg)*=-1)+=200).resize(-100,-100,1,3).move_to(canvas);
- const unsigned int
- bwall = (buttons._width - 1)*(12 + bw) + bw,
- w = cimg::max(196U,36 + logo._width + canvas._width,24 + bwall),
- h = cimg::max(96U,36 + canvas._height + bh,36 + logo._height + bh),
- lx = 12 + (canvas._data?0:((w - 24 - logo._width)/2)),
- ly = (h - 12 - bh - logo._height)/2,
- tx = lx + logo._width + 12,
- ty = (h - 12 - bh - canvas._height)/2,
- bx = (w - bwall)/2,
- by = h - 12 - bh;
- if (canvas._data)
- canvas = CImg<unsigned char>(w,h,1,3).
- draw_rectangle(0,0,w - 1,h - 1,gray).
- draw_line(0,0,w - 1,0,white).draw_line(0,h - 1,0,0,white).
- draw_line(w - 1,0,w - 1,h - 1,black).draw_line(w - 1,h - 1,0,h - 1,black).
- draw_image(tx,ty,canvas);
- else
- canvas = CImg<unsigned char>(w,h,1,3).
- draw_rectangle(0,0,w - 1,h - 1,gray).
- draw_line(0,0,w - 1,0,white).draw_line(0,h - 1,0,0,white).
- draw_line(w - 1,0,w - 1,h - 1,black).draw_line(w - 1,h - 1,0,h - 1,black);
- if (logo._data) canvas.draw_image(lx,ly,logo);
- unsigned int xbuttons[6] = { 0 };
- cimglist_for(buttons,lll) {
- xbuttons[lll] = bx + (bw + 12)*lll;
- canvas.draw_image(xbuttons[lll],by,buttons[lll]);
- }
- // Open window and enter events loop
- CImgDisplay disp(canvas,title?title:" ",0,false,is_centered?true:false);
- if (is_centered) disp.move((CImgDisplay::screen_width() - disp.width())/2,
- (CImgDisplay::screen_height() - disp.height())/2);
- bool stop_flag = false, refresh = false;
- int oselected = -1, oclicked = -1, selected = -1, clicked = -1;
- while (!disp.is_closed() && !stop_flag) {
- if (refresh) {
- if (clicked>=0)
- CImg<unsigned char>(canvas).draw_image(xbuttons[clicked],by,cbuttons[clicked]).display(disp);
- else {
- if (selected>=0)
- CImg<unsigned char>(canvas).draw_image(xbuttons[selected],by,sbuttons[selected]).display(disp);
- else canvas.display(disp);
- }
- refresh = false;
- }
- disp.wait(15);
- if (disp.is_resized()) disp.resize(disp,false);
- if (disp.button()&1) {
- oclicked = clicked;
- clicked = -1;
- cimglist_for(buttons,l)
- if (disp.mouse_y()>=(int)by && disp.mouse_y()<(int)(by + bh) &&
- disp.mouse_x()>=(int)xbuttons[l] && disp.mouse_x()<(int)(xbuttons[l] + bw)) {
- clicked = selected = l;
- refresh = true;
- }
- if (clicked!=oclicked) refresh = true;
- } else if (clicked>=0) stop_flag = true;
- if (disp.key()) {
- oselected = selected;
- switch (disp.key()) {
- case cimg::keyESC : selected = -1; stop_flag = true; break;
- case cimg::keyENTER : if (selected<0) selected = 0; stop_flag = true; break;
- case cimg::keyTAB :
- case cimg::keyARROWRIGHT :
- case cimg::keyARROWDOWN : selected = (selected + 1)%buttons.width(); break;
- case cimg::keyARROWLEFT :
- case cimg::keyARROWUP : selected = (selected + buttons.width() - 1)%buttons.width(); break;
- }
- disp.set_key();
- if (selected!=oselected) refresh = true;
- }
- }
- if (!disp) selected = -1;
- return selected;
- #endif
- }
- //! Display a simple dialog box, and wait for the user's response \specialization.
- inline int dialog(const char *const title, const char *const msg,
- const char *const button1_label, const char *const button2_label,
- const char *const button3_label, const char *const button4_label,
- const char *const button5_label, const char *const button6_label,
- const bool is_centered) {
- return dialog(title,msg,button1_label,button2_label,button3_label,button4_label,button5_label,button6_label,
- CImg<unsigned char>::_logo40x38(),is_centered);
- }
- //! Evaluate math expression.
- /**
- \param expression C-string describing the formula to evaluate.
- \param x Value of the pre-defined variable \c x.
- \param y Value of the pre-defined variable \c y.
- \param z Value of the pre-defined variable \c z.
- \param c Value of the pre-defined variable \c c.
- \return Result of the formula evaluation.
- \note Set \c expression to \c 0 to keep evaluating the last specified \c expression.
- \par Example
- \code
- const double
- res1 = cimg::eval("cos(x)^2 + sin(y)^2",2,2), // will return '1'
- res2 = cimg::eval(0,1,1); // will return '1' too
- \endcode
- **/
- inline double eval(const char *const expression, const double x, const double y, const double z, const double c) {
- static const CImg<float> empty;
- return empty.eval(expression,x,y,z,c);
- }
- template<typename t>
- inline CImg<typename cimg::superset<double,t>::type> eval(const char *const expression, const CImg<t>& xyzc) {
- static const CImg<float> empty;
- return empty.eval(expression,xyzc);
- }
- } // namespace cimg { ...
- } // namespace cimg_library { ...
- //! Short alias name.
- namespace cil = cimg_library_suffixed;
- #ifdef _cimg_redefine_False
- #define False 0
- #endif
- #ifdef _cimg_redefine_True
- #define True 1
- #endif
- #ifdef _cimg_redefine_Status
- #define Status int
- #endif
- #ifdef _cimg_redefine_Success
- #define Success 0
- #endif
- #ifdef _cimg_redefine_min
- #define min(a,b) (((a)<(b))?(a):(b))
- #endif
- #ifdef _cimg_redefine_max
- #define max(a,b) (((a)>(b))?(a):(b))
- #endif
- #ifdef _cimg_redefine_PI
- #define PI 3.141592653589793238462643383
- #endif
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- #endif
- // Local Variables:
- // mode: c++
- // End:
|