view_nii.m 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873
  1. % VIEW_NII: Create or update a 3-View (Front, Top, Side) of the
  2. % brain data that is specified by nii structure
  3. %
  4. % Usage: status = view_nii([h], nii, [option]) or
  5. % status = view_nii(h, [option])
  6. %
  7. % Where, h is the figure on which the 3-View will be plotted;
  8. % nii is the brain data in NIFTI format;
  9. % option is a struct that configures the view plotted, can be:
  10. %
  11. % option.command = 'init'
  12. % option.command = 'update'
  13. % option.command = 'clearnii'
  14. % option.command = 'updatenii'
  15. % option.command = 'updateimg' (nii is nii.img here)
  16. %
  17. % option.usecolorbar = 0 | [1]
  18. % option.usepanel = 0 | [1]
  19. % option.usecrosshair = 0 | [1]
  20. % option.usestretch = 0 | [1]
  21. % option.useimagesc = 0 | [1]
  22. % option.useinterp = [0] | 1
  23. %
  24. % option.setarea = [x y w h] | [0.05 0.05 0.9 0.9]
  25. % option.setunit = ['vox'] | 'mm'
  26. % option.setviewpoint = [x y z] | [origin]
  27. % option.setscanid = [t] | [1]
  28. % option.setcrosshaircolor = [r g b] | [1 0 0]
  29. % option.setcolorindex = From 1 to 9 (default is 2 or 3)
  30. % option.setcolormap = (Mx3 matrix, 0 <= val <= 1)
  31. % option.setcolorlevel = No more than 256 (default 256)
  32. % option.sethighcolor = []
  33. % option.setcbarminmax = []
  34. % option.setvalue = []
  35. % option.glblocminmax = []
  36. % option.setbuttondown = ''
  37. % option.setcomplex = [0] | 1 | 2
  38. %
  39. % Options description in detail:
  40. % ==============================
  41. %
  42. % 1. command: A char string that can control program.
  43. %
  44. % init: If option.command='init', the program will display
  45. % a 3-View plot on the figure specified by figure h
  46. % or on a new figure. If there is already a 3-View
  47. % plot on the figure, please use option.command =
  48. % 'updatenii' (see detail below); otherwise, the
  49. % new 3-View plot will superimpose on the old one.
  50. % If there is no option provided, the program will
  51. % assume that this is an initial plot. If the figure
  52. % handle is omitted, the program knows that it is
  53. % an initial plot.
  54. %
  55. % update: If there is no command specified, and a figure
  56. % handle of the existing 3-View plot is provided,
  57. % the program will choose option.command='update'
  58. % to update the 3-View plot with some new option
  59. % items.
  60. %
  61. % clearnii: Clear 3-View plot on specific figure
  62. %
  63. % updatenii: If a new nii is going to be loaded on a fig
  64. % that has already 3-View plot on it, use this
  65. % command to clear existing 3-View plot, and then
  66. % display with new nii. So, the new nii will not
  67. % superimpose on the existing one. All options
  68. % for 'init' can be used for 'updatenii'.
  69. %
  70. % updateimg: If a new 3D matrix with the same dimension
  71. % is going to be loaded, option.command='updateimg'
  72. % can be used as a light-weighted 'updatenii, since
  73. % it only updates the 3 slices with new values.
  74. % inputing argument nii should be a 3D matrix
  75. % (nii.img) instead of nii struct. No other option
  76. % should be used together with 'updateimg' to keep
  77. % this command as simple as possible.
  78. %
  79. %
  80. % 2. usecolorbar: If specified and usecolorbar=0, the program
  81. % will not include the colorbar in plot area; otherwise,
  82. % a colorbar will be included in plot area.
  83. %
  84. % 3. usepanel: If specified and usepanel=0, the control panel
  85. % at lower right cornor will be invisible; otherwise,
  86. % it will be visible.
  87. %
  88. % 4. usecrosshair: If specified and usecrosshair=0, the crosshair
  89. % will be invisible; otherwise, it will be visible.
  90. %
  91. % 5. usestretch: If specified and usestretch=0, the 3 slices will
  92. % not be stretched, and will be displayed according to
  93. % the actual voxel size; otherwise, the 3 slices will be
  94. % stretched to the edge.
  95. %
  96. % 6. useimagesc: If specified and useimagesc=0, images data will
  97. % be used directly to match the colormap (like 'image'
  98. % command); otherwise, image data will be scaled to full
  99. % colormap with 'imagesc' command in Matlab.
  100. %
  101. % 7. useinterp: If specified and useinterp=1, the image will be
  102. % displayed using interpolation. Otherwise, it will be
  103. % displayed like mosaic, and each tile stands for a
  104. % pixel. This option does not apply to 'setvalue' option
  105. % is set.
  106. %
  107. %
  108. % 8. setarea: 3-View plot will be displayed on this specific
  109. % region. If it is not specified, program will set the
  110. % plot area to [0.05 0.05 0.9 0.9].
  111. %
  112. % 9. setunit: It can be specified to setunit='voxel' or 'mm'
  113. % and the view will change the axes unit of [X Y Z]
  114. % accordingly.
  115. %
  116. % 10. setviewpoint: If specified, [X Y Z] values will be used
  117. % to set the viewpoint of 3-View plot.
  118. %
  119. % 11. setscanid: If specified, [t] value will be used to display
  120. % the specified image scan in NIFTI data.
  121. %
  122. % 12. setcrosshaircolor: If specified, [r g b] value will be used
  123. % for Crosshair Color. Otherwise, red will be the default.
  124. %
  125. % 13. setcolorindex: If specified, the 3-View will choose the
  126. % following colormap: 2 - Bipolar; 3 - Gray; 4 - Jet;
  127. % 5 - Cool; 6 - Bone; 7 - Hot; 8 - Copper; 9 - Pink;
  128. % If not specified, it will choose 3 - Gray if all data
  129. % values are not less than 0; otherwise, it will choose
  130. % 2 - Bipolar if there is value less than 0. (Contrast
  131. % control can only apply to 3 - Gray colormap.
  132. %
  133. % 14. setcolormap: 3-View plot will use it as a customized colormap.
  134. % It is a 3-column matrix with value between 0 and 1. If
  135. % using MS-Windows version of Matlab, the number of rows
  136. % can not be more than 256, because of Matlab limitation.
  137. % When colormap is used, setcolorlevel option will be
  138. % disabled automatically.
  139. %
  140. % 15. setcolorlevel: If specified (must be no more than 256, and
  141. % cannot be used for customized colormap), row number of
  142. % colormap will be squeezed down to this level; otherwise,
  143. % it will assume that setcolorlevel=256.
  144. %
  145. % 16. sethighcolor: If specified, program will squeeze down the
  146. % colormap, and allocate sethighcolor (an Mx3 matrix)
  147. % to high-end portion of the colormap. The sum of M and
  148. % setcolorlevel should be less than 256. If setcolormap
  149. % option is used, sethighcolor will be inserted on top
  150. % of the setcolormap, and the setcolorlevel option will
  151. % be disabled automatically.
  152. %
  153. % 17. setcbarminmax: if specified, the [min max] will be used to
  154. % set the min and max of the colorbar, which does not
  155. % include any data for highcolor.
  156. %
  157. % 18. setvalue: If specified, setvalue.val (with the same size as
  158. % the source data on solution points) in the source area
  159. % setvalue.idx will be superimposed on the current nii
  160. % image. So, the size of setvalue.val should be equal to
  161. % the size of setvalue.idx. To use this feature, it needs
  162. % single or double nii structure for background image.
  163. %
  164. % 19. glblocminmax: If specified, pgm will use glblocminmax to
  165. % calculate the colormap, instead of minmax of image.
  166. %
  167. % 20. setbuttondown: If specified, pgm will evaluate the command
  168. % after a click or slide action is invoked to the new
  169. % view point.
  170. %
  171. % 21. setcomplex: This option will decide how complex data to be
  172. % displayed: 0 - Real part of complex data; 1 - Imaginary
  173. % part of complex data; 2 - Modulus (magnitude) of complex
  174. % data; If not specified, it will be set to 0 (Real part
  175. % of complex data as default option. This option only apply
  176. % when option.command is set to 'init or 'updatenii'.
  177. %
  178. %
  179. % Additional Options for 'update' command:
  180. % =======================================
  181. %
  182. % option.enablecursormove = [1] | 0
  183. % option.enableviewpoint = 0 | [1]
  184. % option.enableorigin = 0 | [1]
  185. % option.enableunit = 0 | [1]
  186. % option.enablecrosshair = 0 | [1]
  187. % option.enablehistogram = 0 | [1]
  188. % option.enablecolormap = 0 | [1]
  189. % option.enablecontrast = 0 | [1]
  190. % option.enablebrightness = 0 | [1]
  191. % option.enableslider = 0 | [1]
  192. % option.enabledirlabel = 0 | [1]
  193. %
  194. %
  195. % e.g.:
  196. % nii = load_nii('T1'); % T1.img/hdr
  197. % view_nii(nii);
  198. %
  199. % or
  200. %
  201. % h = figure('unit','normal','pos', [0.18 0.08 0.64 0.85]);
  202. % opt.setarea = [0.05 0.05 0.9 0.9];
  203. % view_nii(h, nii, opt);
  204. %
  205. %
  206. % Part of this file is copied and modified from:
  207. % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
  208. %
  209. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  210. %
  211. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  212. %
  213. function status = view_nii(varargin)
  214. if nargin < 1
  215. error('Please check inputs using ''help view_nii''');
  216. end;
  217. nii = '';
  218. opt = '';
  219. command = '';
  220. usecolorbar = [];
  221. usepanel = [];
  222. usecrosshair = '';
  223. usestretch = [];
  224. useimagesc = [];
  225. useinterp = [];
  226. setarea = [];
  227. setunit = '';
  228. setviewpoint = [];
  229. setscanid = [];
  230. setcrosshaircolor = [];
  231. setcolorindex = '';
  232. setcolormap = 'NA';
  233. setcolorlevel = [];
  234. sethighcolor = 'NA';
  235. setcbarminmax = [];
  236. setvalue = [];
  237. glblocminmax = [];
  238. setbuttondown = '';
  239. setcomplex = 0;
  240. status = [];
  241. if ishandle(varargin{1}) % plot on top of this figure
  242. fig = varargin{1};
  243. if nargin < 2
  244. command = 'update'; % just to get 3-View status
  245. end
  246. if nargin == 2
  247. if ~isstruct(varargin{2})
  248. error('2nd parameter should be either nii struct or option struct');
  249. end
  250. opt = varargin{2};
  251. if isfield(opt,'hdr') & isfield(opt,'img')
  252. nii = opt;
  253. elseif isfield(opt, 'command') & (strcmpi(opt.command,'init') ...
  254. | strcmpi(opt.command,'updatenii') ...
  255. | strcmpi(opt.command,'updateimg') )
  256. error('Option here cannot contain "init", "updatenii", or "updateimg" comand');
  257. end
  258. end
  259. if nargin == 3
  260. nii = varargin{2};
  261. opt = varargin{3};
  262. if ~isstruct(opt)
  263. error('3rd parameter should be option struct');
  264. end
  265. if ~isfield(opt,'command') | ~strcmpi(opt.command,'updateimg')
  266. if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
  267. error('2nd parameter should be nii struct');
  268. end
  269. if isfield(nii,'untouch') & nii.untouch == 1
  270. error('Usage: please use ''load_nii.m'' to load the structure.');
  271. end
  272. end
  273. end
  274. set(fig, 'menubar', 'none');
  275. elseif ischar(varargin{1}) % call back by event
  276. command = lower(varargin{1});
  277. fig = gcbf;
  278. else % start nii with a new figure
  279. nii = varargin{1};
  280. if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
  281. error('1st parameter should be either a figure handle or nii struct');
  282. end
  283. if isfield(nii,'untouch') & nii.untouch == 1
  284. error('Usage: please use ''load_nii.m'' to load the structure.');
  285. end
  286. if nargin > 1
  287. opt = varargin{2};
  288. if isfield(opt, 'command') & ~strcmpi(opt.command,'init')
  289. error('Option here must use "init" comand');
  290. end
  291. end
  292. command = 'init';
  293. fig = figure('unit','normal','position',[0.15 0.08 0.70 0.85]);
  294. view_nii_menu(fig);
  295. rri_file_menu(fig);
  296. end
  297. if ~isempty(opt)
  298. if isfield(opt,'command')
  299. command = lower(opt.command);
  300. end
  301. if isempty(command)
  302. command = 'update';
  303. end
  304. if isfield(opt,'usecolorbar')
  305. usecolorbar = opt.usecolorbar;
  306. end
  307. if isfield(opt,'usepanel')
  308. usepanel = opt.usepanel;
  309. end
  310. if isfield(opt,'usecrosshair')
  311. usecrosshair = opt.usecrosshair;
  312. end
  313. if isfield(opt,'usestretch')
  314. usestretch = opt.usestretch;
  315. end
  316. if isfield(opt,'useimagesc')
  317. useimagesc = opt.useimagesc;
  318. end
  319. if isfield(opt,'useinterp')
  320. useinterp = opt.useinterp;
  321. end
  322. if isfield(opt,'setarea')
  323. setarea = opt.setarea;
  324. end
  325. if isfield(opt,'setunit')
  326. setunit = opt.setunit;
  327. end
  328. if isfield(opt,'setviewpoint')
  329. setviewpoint = opt.setviewpoint;
  330. end
  331. if isfield(opt,'setscanid')
  332. setscanid = opt.setscanid;
  333. end
  334. if isfield(opt,'setcrosshaircolor')
  335. setcrosshaircolor = opt.setcrosshaircolor;
  336. if ~isempty(setcrosshaircolor) & (~isnumeric(setcrosshaircolor) | ~isequal(size(setcrosshaircolor),[1 3]) | min(setcrosshaircolor(:))<0 | max(setcrosshaircolor(:))>1)
  337. error('Crosshair Color should be a 1x3 matrix with value between 0 and 1');
  338. end
  339. end
  340. if isfield(opt,'setcolorindex')
  341. setcolorindex = round(opt.setcolorindex);
  342. if ~isnumeric(setcolorindex) | setcolorindex < 1 | setcolorindex > 9
  343. error('Colorindex should be a number between 1 and 9');
  344. end
  345. end
  346. if isfield(opt,'setcolormap')
  347. setcolormap = opt.setcolormap;
  348. if ~isempty(setcolormap) & (~isnumeric(setcolormap) | size(setcolormap,2) ~= 3 | min(setcolormap(:))<0 | max(setcolormap(:))>1)
  349. error('Colormap should be a Mx3 matrix with value between 0 and 1');
  350. end
  351. end
  352. if isfield(opt,'setcolorlevel')
  353. setcolorlevel = round(opt.setcolorlevel);
  354. if ~isnumeric(setcolorlevel) | setcolorlevel > 256 | setcolorlevel < 1
  355. error('Colorlevel should be a number between 1 and 256');
  356. end
  357. end
  358. if isfield(opt,'sethighcolor')
  359. sethighcolor = opt.sethighcolor;
  360. if ~isempty(sethighcolor) & (~isnumeric(sethighcolor) | size(sethighcolor,2) ~= 3 | min(sethighcolor(:))<0 | max(sethighcolor(:))>1)
  361. error('Highcolor should be a Mx3 matrix with value between 0 and 1');
  362. end
  363. end
  364. if isfield(opt,'setcbarminmax')
  365. setcbarminmax = opt.setcbarminmax;
  366. if isempty(setcbarminmax) | ~isnumeric(setcbarminmax) | length(setcbarminmax) ~= 2
  367. error('Colorbar MinMax should contain 2 values: [min max]');
  368. end
  369. end
  370. if isfield(opt,'setvalue')
  371. setvalue = opt.setvalue;
  372. if isempty(setvalue) | ~isstruct(setvalue) | ...
  373. ~isfield(opt.setvalue,'idx') | ~isfield(opt.setvalue,'val')
  374. error('setvalue should be a struct contains idx and val');
  375. end
  376. if length(opt.setvalue.idx(:)) ~= length(opt.setvalue.val(:))
  377. error('length of idx and val fields should be the same');
  378. end
  379. if ~strcmpi(class(opt.setvalue.idx),'single')
  380. opt.setvalue.idx = single(opt.setvalue.idx);
  381. end
  382. if ~strcmpi(class(opt.setvalue.val),'single')
  383. opt.setvalue.val = single(opt.setvalue.val);
  384. end
  385. end
  386. if isfield(opt,'glblocminmax')
  387. glblocminmax = opt.glblocminmax;
  388. end
  389. if isfield(opt,'setbuttondown')
  390. setbuttondown = opt.setbuttondown;
  391. end
  392. if isfield(opt,'setcomplex')
  393. setcomplex = opt.setcomplex;
  394. end
  395. end
  396. switch command
  397. case {'init'}
  398. set(fig, 'InvertHardcopy','off');
  399. set(fig, 'PaperPositionMode','auto');
  400. fig = init(nii, fig, setarea, setunit, setviewpoint, setscanid, setbuttondown, ...
  401. setcolorindex, setcolormap, setcolorlevel, sethighcolor, setcbarminmax, ...
  402. usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, useinterp, ...
  403. setvalue, glblocminmax, setcrosshaircolor, setcomplex);
  404. % get status
  405. %
  406. status = get_status(fig);
  407. case {'update'}
  408. nii_view = getappdata(fig,'nii_view');
  409. h = fig;
  410. if isempty(nii_view)
  411. error('The figure should already contain a 3-View plot.');
  412. end
  413. if ~isempty(opt)
  414. % Order of the following update matters.
  415. %
  416. update_shape(h, setarea, usecolorbar, usestretch, useimagesc);
  417. update_useinterp(h, useinterp);
  418. update_useimagesc(h, useimagesc);
  419. update_usepanel(h, usepanel);
  420. update_colorindex(h, setcolorindex);
  421. update_colormap(h, setcolormap);
  422. update_highcolor(h, sethighcolor, setcolorlevel);
  423. update_cbarminmax(h, setcbarminmax);
  424. update_unit(h, setunit);
  425. update_viewpoint(h, setviewpoint);
  426. update_scanid(h, setscanid);
  427. update_buttondown(h, setbuttondown);
  428. update_crosshaircolor(h, setcrosshaircolor);
  429. update_usecrosshair(h, usecrosshair);
  430. % Enable/Disable object
  431. %
  432. update_enable(h, opt);
  433. end
  434. % get status
  435. %
  436. status = get_status(h);
  437. case {'updateimg'}
  438. if ~exist('nii','var')
  439. msg = sprintf('Please input a 3D matrix brain data');
  440. error(msg);
  441. end
  442. % Note: nii is not nii, nii should be a 3D matrix here
  443. %
  444. if ~isnumeric(nii)
  445. msg = sprintf('2nd parameter should be a 3D matrix, not nii struct');
  446. error(msg);
  447. end
  448. nii_view = getappdata(fig,'nii_view');
  449. if isempty(nii_view)
  450. error('The figure should already contain a 3-View plot.');
  451. end
  452. img = nii;
  453. update_img(img, fig, opt);
  454. % get status
  455. %
  456. status = get_status(fig);
  457. case {'updatenii'}
  458. nii_view = getappdata(fig,'nii_view');
  459. if isempty(nii_view)
  460. error('The figure should already contain a 3-View plot.');
  461. end
  462. if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img')
  463. error('2nd parameter should be nii struct');
  464. end
  465. if isfield(nii,'untouch') & nii.untouch == 1
  466. error('Usage: please use ''load_nii.m'' to load the structure.');
  467. end
  468. opt.command = 'clearnii';
  469. view_nii(fig, opt);
  470. opt.command = 'init';
  471. view_nii(fig, nii, opt);
  472. % get status
  473. %
  474. status = get_status(fig);
  475. case {'clearnii'}
  476. nii_view = getappdata(fig,'nii_view');
  477. handles = struct2cell(nii_view.handles);
  478. for i=1:length(handles)
  479. if ishandle(handles{i}) % in case already del by parent
  480. delete(handles{i});
  481. end
  482. end
  483. rmappdata(fig,'nii_view');
  484. buttonmotion = get(fig,'windowbuttonmotion');
  485. mymotion = '; view_nii(''move_cursor'');';
  486. buttonmotion = strrep(buttonmotion, mymotion, '');
  487. set(fig, 'windowbuttonmotion', buttonmotion);
  488. case {'axial_image','coronal_image','sagittal_image'}
  489. switch command
  490. case 'axial_image', view = 'axi'; axi = 0; cor = 1; sag = 1;
  491. case 'coronal_image', view = 'cor'; axi = 1; cor = 0; sag = 1;
  492. case 'sagittal_image', view = 'sag'; axi = 1; cor = 1; sag = 0;
  493. end
  494. nii_view = getappdata(fig,'nii_view');
  495. nii_view = get_slice_position(nii_view,view);
  496. if isfield(nii_view, 'disp')
  497. img = nii_view.disp;
  498. else
  499. img = nii_view.nii.img;
  500. end
  501. % CData must be double() for Matlab 6.5 for Windows
  502. %
  503. if axi,
  504. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
  505. Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
  506. set(nii_view.handles.axial_bg,'CData',double(Saxi)');
  507. end
  508. if isfield(nii_view.handles,'axial_image'),
  509. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  510. Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
  511. Saxi = permute(Saxi, [2 1 3]);
  512. else
  513. Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
  514. Saxi = Saxi';
  515. end
  516. set(nii_view.handles.axial_image,'CData',double(Saxi));
  517. end
  518. if isfield(nii_view.handles,'axial_slider'),
  519. set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
  520. end;
  521. end
  522. if cor,
  523. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
  524. Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
  525. set(nii_view.handles.coronal_bg,'CData',double(Scor)');
  526. end
  527. if isfield(nii_view.handles,'coronal_image'),
  528. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  529. Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
  530. Scor = permute(Scor, [2 1 3]);
  531. else
  532. Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
  533. Scor = Scor';
  534. end
  535. set(nii_view.handles.coronal_image,'CData',double(Scor));
  536. end
  537. if isfield(nii_view.handles,'coronal_slider'),
  538. slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
  539. set(nii_view.handles.coronal_slider,'Value',slider_val);
  540. end;
  541. end;
  542. if sag,
  543. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
  544. Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
  545. set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
  546. end
  547. if isfield(nii_view.handles,'sagittal_image'),
  548. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  549. Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
  550. Ssag = permute(Ssag, [2 1 3]);
  551. else
  552. Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
  553. Ssag = Ssag';
  554. end
  555. set(nii_view.handles.sagittal_image,'CData',double(Ssag));
  556. end
  557. if isfield(nii_view.handles,'sagittal_slider'),
  558. set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
  559. end;
  560. end;
  561. update_nii_view(nii_view);
  562. if ~isempty(nii_view.buttondown)
  563. eval(nii_view.buttondown);
  564. end
  565. case {'axial_slider','coronal_slider','sagittal_slider'},
  566. switch command
  567. case 'axial_slider', view = 'axi'; axi = 1; cor = 0; sag = 0;
  568. case 'coronal_slider', view = 'cor'; axi = 0; cor = 1; sag = 0;
  569. case 'sagittal_slider', view = 'sag'; axi = 0; cor = 0; sag = 1;
  570. end
  571. nii_view = getappdata(fig,'nii_view');
  572. nii_view = get_slider_position(nii_view);
  573. if isfield(nii_view, 'disp')
  574. img = nii_view.disp;
  575. else
  576. img = nii_view.nii.img;
  577. end
  578. if axi,
  579. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
  580. Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
  581. set(nii_view.handles.axial_bg,'CData',double(Saxi)');
  582. end
  583. if isfield(nii_view.handles,'axial_image'),
  584. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  585. Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
  586. Saxi = permute(Saxi, [2 1 3]);
  587. else
  588. Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
  589. Saxi = Saxi';
  590. end
  591. set(nii_view.handles.axial_image,'CData',double(Saxi));
  592. end
  593. if isfield(nii_view.handles,'axial_slider'),
  594. set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
  595. end
  596. end
  597. if cor,
  598. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
  599. Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
  600. set(nii_view.handles.coronal_bg,'CData',double(Scor)');
  601. end
  602. if isfield(nii_view.handles,'coronal_image'),
  603. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  604. Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
  605. Scor = permute(Scor, [2 1 3]);
  606. else
  607. Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
  608. Scor = Scor';
  609. end
  610. set(nii_view.handles.coronal_image,'CData',double(Scor));
  611. end
  612. if isfield(nii_view.handles,'coronal_slider'),
  613. slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
  614. set(nii_view.handles.coronal_slider,'Value',slider_val);
  615. end
  616. end
  617. if sag,
  618. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
  619. Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
  620. set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
  621. end
  622. if isfield(nii_view.handles,'sagittal_image'),
  623. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  624. Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
  625. Ssag = permute(Ssag, [2 1 3]);
  626. else
  627. Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
  628. Ssag = Ssag';
  629. end
  630. set(nii_view.handles.sagittal_image,'CData',double(Ssag));
  631. end
  632. if isfield(nii_view.handles,'sagittal_slider'),
  633. set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
  634. end
  635. end
  636. update_nii_view(nii_view);
  637. if ~isempty(nii_view.buttondown)
  638. eval(nii_view.buttondown);
  639. end
  640. case {'impos_edit'}
  641. nii_view = getappdata(fig,'nii_view');
  642. impos = str2num(get(nii_view.handles.impos,'string'));
  643. if isfield(nii_view, 'disp')
  644. img = nii_view.disp;
  645. else
  646. img = nii_view.nii.img;
  647. end
  648. if isempty(impos) | ~all(size(impos) == [1 3])
  649. msg = 'Please use 3 numbers to represent X,Y and Z';
  650. msgbox(msg,'Error');
  651. return;
  652. end
  653. slices.sag = round(impos(1));
  654. slices.cor = round(impos(2));
  655. slices.axi = round(impos(3));
  656. nii_view = convert2voxel(nii_view,slices);
  657. nii_view = check_slices(nii_view);
  658. impos(1) = nii_view.slices.sag;
  659. impos(2) = nii_view.dims(2) - nii_view.slices.cor + 1;
  660. impos(3) = nii_view.slices.axi;
  661. if isfield(nii_view.handles,'sagittal_slider'),
  662. set(nii_view.handles.sagittal_slider,'Value',impos(1));
  663. end
  664. if isfield(nii_view.handles,'coronal_slider'),
  665. set(nii_view.handles.coronal_slider,'Value',impos(2));
  666. end
  667. if isfield(nii_view.handles,'axial_slider'),
  668. set(nii_view.handles.axial_slider,'Value',impos(3));
  669. end
  670. nii_view = get_slider_position(nii_view);
  671. update_nii_view(nii_view);
  672. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp
  673. Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
  674. set(nii_view.handles.axial_bg,'CData',double(Saxi)');
  675. end
  676. if isfield(nii_view.handles,'axial_image'),
  677. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  678. Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid));
  679. Saxi = permute(Saxi, [2 1 3]);
  680. else
  681. Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid));
  682. Saxi = Saxi';
  683. end
  684. set(nii_view.handles.axial_image,'CData',double(Saxi));
  685. end
  686. if isfield(nii_view.handles,'axial_slider'),
  687. set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi);
  688. end
  689. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp
  690. Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
  691. set(nii_view.handles.coronal_bg,'CData',double(Scor)');
  692. end
  693. if isfield(nii_view.handles,'coronal_image'),
  694. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  695. Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid));
  696. Scor = permute(Scor, [2 1 3]);
  697. else
  698. Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid));
  699. Scor = Scor';
  700. end
  701. set(nii_view.handles.coronal_image,'CData',double(Scor));
  702. end
  703. if isfield(nii_view.handles,'coronal_slider'),
  704. slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
  705. set(nii_view.handles.coronal_slider,'Value',slider_val);
  706. end
  707. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp
  708. Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
  709. set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
  710. end
  711. if isfield(nii_view.handles,'sagittal_image'),
  712. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  713. Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid));
  714. Ssag = permute(Ssag, [2 1 3]);
  715. else
  716. Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid));
  717. Ssag = Ssag';
  718. end
  719. set(nii_view.handles.sagittal_image,'CData',double(Ssag));
  720. end
  721. if isfield(nii_view.handles,'sagittal_slider'),
  722. set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag);
  723. end
  724. axes(nii_view.handles.axial_axes);
  725. axes(nii_view.handles.coronal_axes);
  726. axes(nii_view.handles.sagittal_axes);
  727. if ~isempty(nii_view.buttondown)
  728. eval(nii_view.buttondown);
  729. end
  730. case 'coordinates',
  731. nii_view = getappdata(fig,'nii_view');
  732. set_image_value(nii_view);
  733. case 'crosshair',
  734. nii_view = getappdata(fig,'nii_view');
  735. if get(nii_view.handles.xhair,'value') == 2 % off
  736. set(nii_view.axi_xhair.lx,'visible','off');
  737. set(nii_view.axi_xhair.ly,'visible','off');
  738. set(nii_view.cor_xhair.lx,'visible','off');
  739. set(nii_view.cor_xhair.ly,'visible','off');
  740. set(nii_view.sag_xhair.lx,'visible','off');
  741. set(nii_view.sag_xhair.ly,'visible','off');
  742. else
  743. set(nii_view.axi_xhair.lx,'visible','on');
  744. set(nii_view.axi_xhair.ly,'visible','on');
  745. set(nii_view.cor_xhair.lx,'visible','on');
  746. set(nii_view.cor_xhair.ly,'visible','on');
  747. set(nii_view.sag_xhair.lx,'visible','on');
  748. set(nii_view.sag_xhair.ly,'visible','on');
  749. set(nii_view.handles.axial_axes,'selected','on');
  750. set(nii_view.handles.axial_axes,'selected','off');
  751. set(nii_view.handles.coronal_axes,'selected','on');
  752. set(nii_view.handles.coronal_axes,'selected','off');
  753. set(nii_view.handles.sagittal_axes,'selected','on');
  754. set(nii_view.handles.sagittal_axes,'selected','off');
  755. end
  756. case 'xhair_color',
  757. old_color = get(gcbo,'user');
  758. new_color = uisetcolor(old_color);
  759. update_crosshaircolor(fig, new_color);
  760. case {'color','contrast_def'}
  761. nii_view = getappdata(fig,'nii_view');
  762. if nii_view.numscan == 1
  763. if get(nii_view.handles.colorindex,'value') == 2
  764. set(nii_view.handles.contrast,'value',128);
  765. elseif get(nii_view.handles.colorindex,'value') == 3
  766. set(nii_view.handles.contrast,'value',1);
  767. end
  768. end
  769. [custom_color_map, custom_colorindex] = change_colormap(fig);
  770. if strcmpi(command, 'color')
  771. setcolorlevel = nii_view.colorlevel;
  772. if ~isempty(custom_color_map) % isfield(nii_view, 'color_map')
  773. setcolormap = custom_color_map; % nii_view.color_map;
  774. else
  775. setcolormap = [];
  776. end
  777. if isfield(nii_view, 'highcolor')
  778. sethighcolor = nii_view.highcolor;
  779. else
  780. sethighcolor = [];
  781. end
  782. redraw_cbar(fig, setcolorlevel, setcolormap, sethighcolor);
  783. if nii_view.numscan == 1 & ...
  784. (custom_colorindex < 2 | custom_colorindex > 3)
  785. contrastopt.enablecontrast = 0;
  786. else
  787. contrastopt.enablecontrast = 1;
  788. end
  789. update_enable(fig, contrastopt);
  790. end
  791. case {'neg_color','brightness','contrast'}
  792. change_colormap(fig);
  793. case {'brightness_def'}
  794. nii_view = getappdata(fig,'nii_view');
  795. set(nii_view.handles.brightness,'value',0);
  796. change_colormap(fig);
  797. case 'hist_plot'
  798. hist_plot(fig);
  799. case 'hist_eq'
  800. hist_eq(fig);
  801. case 'move_cursor'
  802. move_cursor(fig);
  803. case 'edit_change_scan'
  804. change_scan('edit_change_scan');
  805. case 'slider_change_scan'
  806. change_scan('slider_change_scan');
  807. end
  808. return; % view_nii
  809. %----------------------------------------------------------------
  810. function fig = init(nii, fig, area, setunit, setviewpoint, setscanid, buttondown, ...
  811. colorindex, color_map, colorlevel, highcolor, cbarminmax, ...
  812. usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, ...
  813. useinterp, setvalue, glblocminmax, setcrosshaircolor, ...
  814. setcomplex)
  815. % Support data type COMPLEX64 & COMPLEX128
  816. %
  817. if nii.hdr.dime.datatype == 32 | nii.hdr.dime.datatype == 1792
  818. switch setcomplex,
  819. case 0,
  820. nii.img = real(nii.img);
  821. case 1,
  822. nii.img = imag(nii.img);
  823. case 2,
  824. if isa(nii.img, 'double')
  825. nii.img = abs(double(nii.img));
  826. else
  827. nii.img = single(abs(double(nii.img)));
  828. end
  829. end
  830. end
  831. if isempty(area)
  832. area = [0.05 0.05 0.9 0.9];
  833. end
  834. if isempty(setscanid)
  835. setscanid = 1;
  836. else
  837. setscanid = round(setscanid);
  838. if setscanid < 1
  839. setscanid = 1;
  840. end
  841. if setscanid > nii.hdr.dime.dim(5)
  842. setscanid = nii.hdr.dime.dim(5);
  843. end
  844. end
  845. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  846. usecolorbar = 0;
  847. elseif isempty(usecolorbar)
  848. usecolorbar = 1;
  849. end
  850. if isempty(usepanel)
  851. usepanel = 1;
  852. end
  853. if isempty(usestretch)
  854. usestretch = 1;
  855. end
  856. if isempty(useimagesc)
  857. useimagesc = 1;
  858. end
  859. if isempty(useinterp)
  860. useinterp = 0;
  861. end
  862. if isempty(colorindex)
  863. tmp = min(nii.img(:,:,:,setscanid));
  864. if min(tmp(:)) < 0
  865. colorindex = 2;
  866. setcrosshaircolor = [1 1 0];
  867. else
  868. colorindex = 3;
  869. end
  870. end
  871. if isempty(color_map) | ischar(color_map)
  872. color_map = [];
  873. else
  874. colorindex = 1;
  875. end
  876. bgimg = [];
  877. if ~isempty(glblocminmax)
  878. minvalue = glblocminmax(1);
  879. maxvalue = glblocminmax(2);
  880. else
  881. minvalue = nii.img(:,:,:,setscanid);
  882. minvalue = double(minvalue(:));
  883. minvalue = min(minvalue(~isnan(minvalue)));
  884. maxvalue = nii.img(:,:,:,setscanid);
  885. maxvalue = double(maxvalue(:));
  886. maxvalue = max(maxvalue(~isnan(maxvalue)));
  887. end
  888. if ~isempty(setvalue)
  889. if ~isempty(glblocminmax)
  890. minvalue = glblocminmax(1);
  891. maxvalue = glblocminmax(2);
  892. else
  893. minvalue = double(min(setvalue.val));
  894. maxvalue = double(max(setvalue.val));
  895. end
  896. bgimg = double(nii.img);
  897. minbg = double(min(bgimg(:)));
  898. maxbg = double(max(bgimg(:)));
  899. bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256
  900. % 56 level for brain structure
  901. %
  902. % highcolor = [zeros(1,3);gray(55)];
  903. highcolor = gray(56);
  904. cbarminmax = [minvalue maxvalue];
  905. if useinterp
  906. % scale signal data to 1~200
  907. %
  908. nii.img = repmat(nan, size(nii.img));
  909. nii.img(setvalue.idx) = setvalue.val;
  910. % 200 level for source image
  911. %
  912. bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199));
  913. else
  914. bgimg(setvalue.idx) = NaN;
  915. minbg = double(min(bgimg(:)));
  916. maxbg = double(max(bgimg(:)));
  917. bgimg(setvalue.idx) = minbg;
  918. % bgimg must be normalized to [201 256]
  919. %
  920. bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201;
  921. bgimg(setvalue.idx) = 0;
  922. % scale signal data to 1~200
  923. %
  924. nii.img = zeros(size(nii.img));
  925. nii.img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199);
  926. nii.img = nii.img + bgimg;
  927. bgimg = [];
  928. nii.img = scale_out(nii.img, cbarminmax(1), cbarminmax(2), 199);
  929. minvalue = double(nii.img(:));
  930. minvalue = min(minvalue(~isnan(minvalue)));
  931. maxvalue = double(nii.img(:));
  932. maxvalue = max(maxvalue(~isnan(maxvalue)));
  933. if ~isempty(glblocminmax) % maxvalue is gray
  934. minvalue = glblocminmax(1);
  935. end
  936. end
  937. colorindex = 2;
  938. setcrosshaircolor = [1 1 0];
  939. end
  940. if isempty(highcolor) | ischar(highcolor)
  941. highcolor = [];
  942. num_highcolor = 0;
  943. else
  944. num_highcolor = size(highcolor,1);
  945. end
  946. if isempty(colorlevel)
  947. colorlevel = 256 - num_highcolor;
  948. end
  949. if usecolorbar
  950. cbar_area = area;
  951. cbar_area(1) = area(1) + area(3)*0.93;
  952. cbar_area(3) = area(3)*0.04;
  953. area(3) = area(3)*0.9; % 90% used for main axes
  954. else
  955. cbar_area = [];
  956. end
  957. % init color (gray) scaling to make sure the slice clim take the
  958. % global clim [min(nii.img(:)) max(nii.img(:))]
  959. %
  960. if isempty(bgimg)
  961. clim = [minvalue maxvalue];
  962. else
  963. clim = [minvalue double(max(bgimg(:)))];
  964. end
  965. if clim(1) == clim(2)
  966. clim(2) = clim(1) + 0.000001;
  967. end
  968. if isempty(cbarminmax)
  969. cbarminmax = [minvalue maxvalue];
  970. end
  971. xdim = size(nii.img, 1);
  972. ydim = size(nii.img, 2);
  973. zdim = size(nii.img, 3);
  974. dims = [xdim ydim zdim];
  975. voxel_size = abs(nii.hdr.dime.pixdim(2:4)); % vol in mm
  976. if any(voxel_size <= 0)
  977. voxel_size(find(voxel_size <= 0)) = 1;
  978. end
  979. origin = abs(nii.hdr.hist.originator(1:3));
  980. if isempty(origin) | all(origin == 0) % according to SPM
  981. origin = (dims+1)/2;
  982. end;
  983. origin = round(origin);
  984. if any(origin > dims) % simulate fMRI
  985. origin(find(origin > dims)) = dims(find(origin > dims));
  986. end
  987. if any(origin <= 0)
  988. origin(find(origin <= 0)) = 1;
  989. end
  990. nii_view.dims = dims;
  991. nii_view.voxel_size = voxel_size;
  992. nii_view.origin = origin;
  993. nii_view.slices.sag = 1;
  994. nii_view.slices.cor = 1;
  995. nii_view.slices.axi = 1;
  996. if xdim > 1, nii_view.slices.sag = origin(1); end
  997. if ydim > 1, nii_view.slices.cor = origin(2); end
  998. if zdim > 1, nii_view.slices.axi = origin(3); end
  999. nii_view.area = area;
  1000. nii_view.fig = fig;
  1001. nii_view.nii = nii; % image data
  1002. nii_view.bgimg = bgimg; % background
  1003. nii_view.setvalue = setvalue;
  1004. nii_view.minvalue = minvalue;
  1005. nii_view.maxvalue = maxvalue;
  1006. nii_view.numscan = nii.hdr.dime.dim(5);
  1007. nii_view.scanid = setscanid;
  1008. Font.FontUnits = 'point';
  1009. Font.FontSize = 12;
  1010. % create axes for colorbar
  1011. %
  1012. [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
  1013. if isempty(cbar_area)
  1014. nii_view.cbar_area = [];
  1015. else
  1016. nii_view.cbar_area = cbar_area;
  1017. end
  1018. % create axes for top/front/side view
  1019. %
  1020. vol_size = voxel_size .* dims;
  1021. [top_ax, front_ax, side_ax] ...
  1022. = create_ax(fig, area, vol_size, usestretch);
  1023. top_pos = get(top_ax,'position');
  1024. front_pos = get(front_ax,'position');
  1025. side_pos = get(side_ax,'position');
  1026. % Sagittal Slider
  1027. %
  1028. x = side_pos(1);
  1029. y = top_pos(2) + top_pos(4);
  1030. w = side_pos(3);
  1031. h = (front_pos(2) - y) / 2;
  1032. y = y + h;
  1033. pos = [x y w h];
  1034. if xdim > 1,
  1035. slider_step(1) = 1/(xdim);
  1036. slider_step(2) = 1.00001/(xdim);
  1037. handles.sagittal_slider = uicontrol('Parent',fig, ...
  1038. 'Style','slider','Units','Normalized', Font, ...
  1039. 'Position',pos, 'HorizontalAlignment','center',...
  1040. 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
  1041. 'BusyAction','queue',...
  1042. 'TooltipString','Sagittal slice navigation',...
  1043. 'Min',1,'Max',xdim,'SliderStep',slider_step, ...
  1044. 'Value',nii_view.slices.sag,...
  1045. 'Callback','view_nii(''sagittal_slider'');');
  1046. set(handles.sagittal_slider,'position',pos); % linux66
  1047. end
  1048. % Coronal Slider
  1049. %
  1050. x = top_pos(1);
  1051. y = top_pos(2) + top_pos(4);
  1052. w = top_pos(3);
  1053. h = (front_pos(2) - y) / 2;
  1054. y = y + h;
  1055. pos = [x y w h];
  1056. if ydim > 1,
  1057. slider_step(1) = 1/(ydim);
  1058. slider_step(2) = 1.00001/(ydim);
  1059. slider_val = nii_view.dims(2) - nii_view.slices.cor + 1;
  1060. handles.coronal_slider = uicontrol('Parent',fig, ...
  1061. 'Style','slider','Units','Normalized', Font, ...
  1062. 'Position',pos, 'HorizontalAlignment','center',...
  1063. 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
  1064. 'BusyAction','queue',...
  1065. 'TooltipString','Coronal slice navigation',...
  1066. 'Min',1,'Max',ydim,'SliderStep',slider_step, ...
  1067. 'Value',slider_val,...
  1068. 'Callback','view_nii(''coronal_slider'');');
  1069. set(handles.coronal_slider,'position',pos); % linux66
  1070. end
  1071. % Axial Slider
  1072. %
  1073. % x = front_pos(1) + front_pos(3);
  1074. % y = front_pos(2);
  1075. % w = side_pos(1) - x;
  1076. % h = front_pos(4);
  1077. x = top_pos(1);
  1078. y = area(2);
  1079. w = top_pos(3);
  1080. h = top_pos(2) - y;
  1081. pos = [x y w h];
  1082. if zdim > 1,
  1083. slider_step(1) = 1/(zdim);
  1084. slider_step(2) = 1.00001/(zdim);
  1085. handles.axial_slider = uicontrol('Parent',fig, ...
  1086. 'Style','slider','Units','Normalized', Font, ...
  1087. 'Position',pos, 'HorizontalAlignment','center',...
  1088. 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
  1089. 'BusyAction','queue',...
  1090. 'TooltipString','Axial slice navigation',...
  1091. 'Min',1,'Max',zdim,'SliderStep',slider_step, ...
  1092. 'Value',nii_view.slices.axi,...
  1093. 'Callback','view_nii(''axial_slider'');');
  1094. set(handles.axial_slider,'position',pos); % linux66
  1095. end
  1096. % plot info view
  1097. %
  1098. % info_pos = [side_pos([1,3]); top_pos([2,4])];
  1099. % info_pos = info_pos(:);
  1100. gap = side_pos(1)-(top_pos(1)+top_pos(3));
  1101. info_pos(1) = side_pos(1) + gap;
  1102. info_pos(2) = area(2);
  1103. info_pos(3) = side_pos(3) - gap;
  1104. info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap;
  1105. num_inputline = 10;
  1106. inputline_space =info_pos(4) / num_inputline;
  1107. % for any info_area change, update_usestretch should also be changed
  1108. % Image Intensity Value at Cursor
  1109. %
  1110. x = info_pos(1);
  1111. y = info_pos(2);
  1112. w = info_pos(3)*0.5;
  1113. h = inputline_space*0.6;
  1114. pos = [x y w h];
  1115. handles.Timvalcur = uicontrol('Parent',fig,'Style','text', ...
  1116. 'Units','Normalized', Font, ...
  1117. 'Position',pos, 'HorizontalAlignment', 'left',...
  1118. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1119. 'BusyAction','queue',...
  1120. 'visible','off', ...
  1121. 'String','Value at cursor:');
  1122. if usepanel
  1123. set(handles.Timvalcur, 'visible', 'on');
  1124. end
  1125. x = x + w;
  1126. w = info_pos(3)*0.5;
  1127. pos = [x y w h];
  1128. handles.imvalcur = uicontrol('Parent',fig,'Style','text', ...
  1129. 'Units','Normalized', Font, ...
  1130. 'Position',pos, 'HorizontalAlignment', 'right',...
  1131. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1132. 'BusyAction','queue',...
  1133. 'visible','off', ...
  1134. 'String',' ');
  1135. if usepanel
  1136. set(handles.imvalcur, 'visible', 'on');
  1137. end
  1138. % Position at Cursor
  1139. %
  1140. x = info_pos(1);
  1141. y = y + inputline_space;
  1142. w = info_pos(3)*0.5;
  1143. pos = [x y w h];
  1144. handles.Timposcur = uicontrol('Parent',fig,'Style','text', ...
  1145. 'Units','Normalized', Font, ...
  1146. 'Position',pos, 'HorizontalAlignment', 'left',...
  1147. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1148. 'BusyAction','queue',...
  1149. 'visible','off', ...
  1150. 'String','[X Y Z] at cursor:');
  1151. if usepanel
  1152. set(handles.Timposcur, 'visible', 'on');
  1153. end
  1154. x = x + w;
  1155. w = info_pos(3)*0.5;
  1156. pos = [x y w h];
  1157. handles.imposcur = uicontrol('Parent',fig,'Style','text', ...
  1158. 'Units','Normalized', Font, ...
  1159. 'Position',pos, 'HorizontalAlignment', 'right',...
  1160. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1161. 'BusyAction','queue',...
  1162. 'visible','off', ...
  1163. 'String',' ','Value',[0 0 0]);
  1164. if usepanel
  1165. set(handles.imposcur, 'visible', 'on');
  1166. end
  1167. % Image Intensity Value at Mouse Click
  1168. %
  1169. x = info_pos(1);
  1170. y = y + inputline_space;
  1171. w = info_pos(3)*0.5;
  1172. pos = [x y w h];
  1173. handles.Timval = uicontrol('Parent',fig,'Style','text', ...
  1174. 'Units','Normalized', Font, ...
  1175. 'Position',pos, 'HorizontalAlignment', 'left',...
  1176. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1177. 'BusyAction','queue',...
  1178. 'visible','off', ...
  1179. 'String','Value at crosshair:');
  1180. if usepanel
  1181. set(handles.Timval, 'visible', 'on');
  1182. end
  1183. x = x + w;
  1184. w = info_pos(3)*0.5;
  1185. pos = [x y w h];
  1186. handles.imval = uicontrol('Parent',fig,'Style','text', ...
  1187. 'Units','Normalized', Font, ...
  1188. 'Position',pos, 'HorizontalAlignment', 'right',...
  1189. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1190. 'BusyAction','queue',...
  1191. 'visible','off', ...
  1192. 'String',' ');
  1193. if usepanel
  1194. set(handles.imval, 'visible', 'on');
  1195. end
  1196. % Viewpoint Position at Mouse Click
  1197. %
  1198. x = info_pos(1);
  1199. y = y + inputline_space;
  1200. w = info_pos(3)*0.5;
  1201. pos = [x y w h];
  1202. handles.Timpos = uicontrol('Parent',fig,'Style','text', ...
  1203. 'Units','Normalized', Font, ...
  1204. 'Position',pos, 'HorizontalAlignment', 'left',...
  1205. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1206. 'BusyAction','queue',...
  1207. 'visible','off', ...
  1208. 'String','[X Y Z] at crosshair:');
  1209. if usepanel
  1210. set(handles.Timpos, 'visible', 'on');
  1211. end
  1212. x = x + w + 0.005;
  1213. y = y - 0.008;
  1214. w = info_pos(3)*0.5;
  1215. h = inputline_space*0.9;
  1216. pos = [x y w h];
  1217. handles.impos = uicontrol('Parent',fig,'Style','edit', ...
  1218. 'Units','Normalized', Font, ...
  1219. 'Position',pos, 'HorizontalAlignment', 'right',...
  1220. 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
  1221. 'BusyAction','queue',...
  1222. 'Callback','view_nii(''impos_edit'');', ...
  1223. 'TooltipString','Viewpoint Location in Axes Unit', ...
  1224. 'visible','off', ...
  1225. 'String',' ','Value',[0 0 0]);
  1226. if usepanel
  1227. set(handles.impos, 'visible', 'on');
  1228. end
  1229. % Origin Position
  1230. %
  1231. x = info_pos(1);
  1232. y = y + inputline_space*1.2;
  1233. w = info_pos(3)*0.5;
  1234. h = inputline_space*0.6;
  1235. pos = [x y w h];
  1236. handles.Torigin = uicontrol('Parent',fig,'Style','text', ...
  1237. 'Units','Normalized', Font, ...
  1238. 'Position',pos, 'HorizontalAlignment', 'left',...
  1239. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1240. 'BusyAction','queue',...
  1241. 'visible','off', ...
  1242. 'String','[X Y Z] at origin:');
  1243. if usepanel
  1244. set(handles.Torigin, 'visible', 'on');
  1245. end
  1246. x = x + w;
  1247. w = info_pos(3)*0.5;
  1248. pos = [x y w h];
  1249. handles.origin = uicontrol('Parent',fig,'Style','text', ...
  1250. 'Units','Normalized', Font, ...
  1251. 'Position',pos, 'HorizontalAlignment', 'right',...
  1252. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1253. 'BusyAction','queue',...
  1254. 'visible','off', ...
  1255. 'String',' ','Value',[0 0 0]);
  1256. if usepanel
  1257. set(handles.origin, 'visible', 'on');
  1258. end
  1259. if 0
  1260. % Voxel Unit
  1261. %
  1262. x = info_pos(1);
  1263. y = y + inputline_space;
  1264. w = info_pos(3)*0.5;
  1265. pos = [x y w h];
  1266. handles.Tcoord = uicontrol('Parent',fig,'Style','text', ...
  1267. 'Units','Normalized', Font, ...
  1268. 'Position',pos, 'HorizontalAlignment', 'left',...
  1269. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1270. 'BusyAction','queue',...
  1271. 'visible','off', ...
  1272. 'String','Axes Unit:');
  1273. if usepanel
  1274. set(handles.Tcoord, 'visible', 'on');
  1275. end
  1276. x = x + w + 0.005;
  1277. w = info_pos(3)*0.5 - 0.005;
  1278. pos = [x y w h];
  1279. Font.FontSize = 8;
  1280. handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ...
  1281. 'Units','Normalized', Font, ...
  1282. 'Position',pos, ...
  1283. 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
  1284. 'BusyAction','queue',...
  1285. 'TooltipString','Choose Voxel or Millimeter',...
  1286. 'String',{'Voxel','Millimeter'},...
  1287. 'visible','off', ...
  1288. 'Callback','view_nii(''coordinates'');');
  1289. % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',...
  1290. % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},...
  1291. Font.FontSize = 12;
  1292. if usepanel
  1293. set(handles.coord, 'visible', 'on');
  1294. end
  1295. end
  1296. % Crosshair
  1297. %
  1298. x = info_pos(1);
  1299. y = y + inputline_space;
  1300. w = info_pos(3)*0.4;
  1301. pos = [x y w h];
  1302. handles.Txhair = uicontrol('Parent',fig,'Style','text', ...
  1303. 'Units','Normalized', Font, ...
  1304. 'Position',pos, 'HorizontalAlignment', 'left',...
  1305. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1306. 'BusyAction','queue',...
  1307. 'visible','off', ...
  1308. 'String','Crosshair:');
  1309. if usepanel
  1310. set(handles.Txhair, 'visible', 'on');
  1311. end
  1312. x = info_pos(1) + info_pos(3)*0.5;
  1313. w = info_pos(3)*0.2;
  1314. h = inputline_space*0.7;
  1315. pos = [x y w h];
  1316. Font.FontSize = 8;
  1317. handles.xhair_color = uicontrol('Parent',fig,'Style','push', ...
  1318. 'Units','Normalized', Font, ...
  1319. 'Position',pos, 'HorizontalAlignment', 'center',...
  1320. 'TooltipString','Crosshair Color',...
  1321. 'User',[1 0 0],...
  1322. 'String','Color',...
  1323. 'visible','off', ...
  1324. 'Callback','view_nii(''xhair_color'');');
  1325. if usepanel
  1326. set(handles.xhair_color, 'visible', 'on');
  1327. end
  1328. x = info_pos(1) + info_pos(3)*0.7;
  1329. w = info_pos(3)*0.3;
  1330. pos = [x y w h];
  1331. handles.xhair = uicontrol('Parent',fig,'Style','popupmenu', ...
  1332. 'Units','Normalized', Font, ...
  1333. 'Position',pos, ...
  1334. 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
  1335. 'BusyAction','queue',...
  1336. 'TooltipString','Display or Hide Crosshair',...
  1337. 'String',{'On','Off'},...
  1338. 'visible','off', ...
  1339. 'Callback','view_nii(''crosshair'');');
  1340. if usepanel
  1341. set(handles.xhair, 'visible', 'on');
  1342. end
  1343. % Histogram & Color
  1344. %
  1345. x = info_pos(1);
  1346. w = info_pos(3)*0.45;
  1347. h = inputline_space * 1.5;
  1348. pos = [x, y+inputline_space*0.9, w, h];
  1349. handles.hist_frame = uicontrol('Parent',fig, ...
  1350. 'Units','normal', ...
  1351. 'BackgroundColor',[0.8 0.8 0.8], ...
  1352. 'Position',pos, ...
  1353. 'visible','off', ...
  1354. 'Style','frame');
  1355. if usepanel
  1356. % set(handles.hist_frame, 'visible', 'on');
  1357. end
  1358. handles.coord_frame = uicontrol('Parent',fig, ...
  1359. 'Units','normal', ...
  1360. 'BackgroundColor',[0.8 0.8 0.8], ...
  1361. 'Position',pos, ...
  1362. 'visible','off', ...
  1363. 'Style','frame');
  1364. if usepanel
  1365. set(handles.coord_frame, 'visible', 'on');
  1366. end
  1367. x = info_pos(1) + info_pos(3)*0.475;
  1368. w = info_pos(3)*0.525;
  1369. h = inputline_space * 1.5;
  1370. pos = [x, y+inputline_space*0.9, w, h];
  1371. handles.color_frame = uicontrol('Parent',fig, ...
  1372. 'Units','normal', ...
  1373. 'BackgroundColor',[0.8 0.8 0.8], ...
  1374. 'Position',pos, ...
  1375. 'visible','off', ...
  1376. 'Style','frame');
  1377. if usepanel
  1378. set(handles.color_frame, 'visible', 'on');
  1379. end
  1380. x = info_pos(1) + info_pos(3)*0.025;
  1381. y = y + inputline_space*1.2;
  1382. w = info_pos(3)*0.2;
  1383. h = inputline_space*0.7;
  1384. pos = [x y w h];
  1385. Font.FontSize = 8;
  1386. handles.hist_eq = uicontrol('Parent',fig,'Style','toggle', ...
  1387. 'Units','Normalized', Font, ...
  1388. 'Position',pos, 'HorizontalAlignment', 'center',...
  1389. 'TooltipString','Histogram Equalization',...
  1390. 'String','Hist EQ',...
  1391. 'visible','off', ...
  1392. 'Callback','view_nii(''hist_eq'');');
  1393. if usepanel
  1394. % set(handles.hist_eq, 'visible', 'on');
  1395. end
  1396. x = x + w;
  1397. w = info_pos(3)*0.2;
  1398. pos = [x y w h];
  1399. handles.hist_plot = uicontrol('Parent',fig,'Style','push', ...
  1400. 'Units','Normalized', Font, ...
  1401. 'Position',pos, 'HorizontalAlignment', 'center',...
  1402. 'TooltipString','Histogram Plot',...
  1403. 'String','Hist Plot',...
  1404. 'visible','off', ...
  1405. 'Callback','view_nii(''hist_plot'');');
  1406. if usepanel
  1407. % set(handles.hist_plot, 'visible', 'on');
  1408. end
  1409. x = info_pos(1) + info_pos(3)*0.025;
  1410. w = info_pos(3)*0.4;
  1411. pos = [x y w h];
  1412. handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ...
  1413. 'Units','Normalized', Font, ...
  1414. 'Position',pos, ...
  1415. 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
  1416. 'BusyAction','queue',...
  1417. 'TooltipString','Choose Voxel or Millimeter',...
  1418. 'String',{'Voxel','Millimeter'},...
  1419. 'visible','off', ...
  1420. 'Callback','view_nii(''coordinates'');');
  1421. % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',...
  1422. % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},...
  1423. if usepanel
  1424. set(handles.coord, 'visible', 'on');
  1425. end
  1426. x = info_pos(1) + info_pos(3)*0.5;
  1427. w = info_pos(3)*0.2;
  1428. pos = [x y w h];
  1429. handles.neg_color = uicontrol('Parent',fig,'Style','toggle', ...
  1430. 'Units','Normalized', Font, ...
  1431. 'Position',pos, 'HorizontalAlignment', 'center',...
  1432. 'TooltipString','Negative Colormap',...
  1433. 'String','Negative',...
  1434. 'visible','off', ...
  1435. 'Callback','view_nii(''neg_color'');');
  1436. if usepanel
  1437. set(handles.neg_color, 'visible', 'on');
  1438. end
  1439. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1440. set(handles.neg_color, 'enable', 'off');
  1441. end
  1442. x = info_pos(1) + info_pos(3)*0.7;
  1443. w = info_pos(3)*0.275;
  1444. pos = [x y w h];
  1445. handles.colorindex = uicontrol('Parent',fig,'Style','popupmenu', ...
  1446. 'Units','Normalized', Font, ...
  1447. 'Position',pos, ...
  1448. 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],...
  1449. 'BusyAction','queue',...
  1450. 'TooltipString','Change Colormap',...
  1451. 'String',{'Custom','Bipolar','Gray','Jet','Cool','Bone','Hot','Copper','Pink'},...
  1452. 'value', colorindex, ...
  1453. 'visible','off', ...
  1454. 'Callback','view_nii(''color'');');
  1455. if usepanel
  1456. set(handles.colorindex, 'visible', 'on');
  1457. end
  1458. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1459. set(handles.colorindex, 'enable', 'off');
  1460. end
  1461. x = info_pos(1) + info_pos(3)*0.1;
  1462. y = y + inputline_space;
  1463. w = info_pos(3)*0.28;
  1464. h = inputline_space*0.6;
  1465. pos = [x y w h];
  1466. Font.FontSize = 8;
  1467. handles.Thist = uicontrol('Parent',fig,'Style','text', ...
  1468. 'Units','Normalized', Font, ...
  1469. 'Position',pos, 'HorizontalAlignment', 'center',...
  1470. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1471. 'BusyAction','queue',...
  1472. 'visible','off', ...
  1473. 'String','Histogram');
  1474. handles.Tcoord = uicontrol('Parent',fig,'Style','text', ...
  1475. 'Units','Normalized', Font, ...
  1476. 'Position',pos, 'HorizontalAlignment', 'center',...
  1477. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1478. 'BusyAction','queue',...
  1479. 'visible','off', ...
  1480. 'String','Axes Unit');
  1481. if usepanel
  1482. % set(handles.Thist, 'visible', 'on');
  1483. set(handles.Tcoord, 'visible', 'on');
  1484. end
  1485. x = info_pos(1) + info_pos(3)*0.60;
  1486. w = info_pos(3)*0.28;
  1487. pos = [x y w h];
  1488. handles.Tcolor = uicontrol('Parent',fig,'Style','text', ...
  1489. 'Units','Normalized', Font, ...
  1490. 'Position',pos, 'HorizontalAlignment', 'center',...
  1491. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1492. 'BusyAction','queue',...
  1493. 'visible','off', ...
  1494. 'String','Colormap');
  1495. if usepanel
  1496. set(handles.Tcolor, 'visible', 'on');
  1497. end
  1498. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1499. set(handles.Tcolor, 'enable', 'off');
  1500. end
  1501. % Contrast Frame
  1502. %
  1503. x = info_pos(1);
  1504. w = info_pos(3)*0.45;
  1505. h = inputline_space * 2;
  1506. pos = [x, y+inputline_space*0.8, w, h];
  1507. handles.contrast_frame = uicontrol('Parent',fig, ...
  1508. 'Units','normal', ...
  1509. 'BackgroundColor',[0.8 0.8 0.8], ...
  1510. 'Position',pos, ...
  1511. 'visible','off', ...
  1512. 'Style','frame');
  1513. if usepanel
  1514. set(handles.contrast_frame, 'visible', 'on');
  1515. end
  1516. if colorindex < 2 | colorindex > 3
  1517. set(handles.contrast_frame, 'visible', 'off');
  1518. end
  1519. % Brightness Frame
  1520. %
  1521. x = info_pos(1) + info_pos(3)*0.475;
  1522. w = info_pos(3)*0.525;
  1523. pos = [x, y+inputline_space*0.8, w, h];
  1524. handles.brightness_frame = uicontrol('Parent',fig, ...
  1525. 'Units','normal', ...
  1526. 'BackgroundColor',[0.8 0.8 0.8], ...
  1527. 'Position',pos, ...
  1528. 'visible','off', ...
  1529. 'Style','frame');
  1530. if usepanel
  1531. set(handles.brightness_frame, 'visible', 'on');
  1532. end
  1533. % Contrast
  1534. %
  1535. x = info_pos(1) + info_pos(3)*0.025;
  1536. y = y + inputline_space;
  1537. w = info_pos(3)*0.4;
  1538. h = inputline_space*0.6;
  1539. pos = [x y w h];
  1540. Font.FontSize = 12;
  1541. slider_step(1) = 5/255;
  1542. slider_step(2) = 5.00001/255;
  1543. handles.contrast = uicontrol('Parent',fig, ...
  1544. 'Style','slider','Units','Normalized', Font, ...
  1545. 'Position',pos, 'HorizontalAlignment', 'left',...
  1546. 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
  1547. 'BusyAction','queue',...
  1548. 'TooltipString','Change contrast',...
  1549. 'Min',1,'Max',256,'SliderStep',slider_step, ...
  1550. 'Value',1, ...
  1551. 'visible','off', ...
  1552. 'Callback','view_nii(''contrast'');');
  1553. if usepanel
  1554. set(handles.contrast, 'visible', 'on');
  1555. end
  1556. if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
  1557. set(handles.contrast, 'enable', 'off');
  1558. end
  1559. if nii_view.numscan > 1
  1560. set(handles.contrast, 'min', 1, 'max', nii_view.numscan, ...
  1561. 'sliderstep',[1/(nii_view.numscan-1) 1.00001/(nii_view.numscan-1)], ...
  1562. 'Callback', 'view_nii(''slider_change_scan'');');
  1563. elseif colorindex < 2 | colorindex > 3
  1564. set(handles.contrast, 'visible', 'off');
  1565. elseif colorindex == 2
  1566. set(handles.contrast,'value',128);
  1567. end
  1568. set(handles.contrast,'position',pos); % linux66
  1569. % Brightness
  1570. %
  1571. x = info_pos(1) + info_pos(3)*0.5;
  1572. w = info_pos(3)*0.475;
  1573. pos = [x y w h];
  1574. Font.FontSize = 12;
  1575. slider_step(1) = 1/50;
  1576. slider_step(2) = 1.00001/50;
  1577. handles.brightness = uicontrol('Parent',fig, ...
  1578. 'Style','slider','Units','Normalized', Font, ...
  1579. 'Position',pos, 'HorizontalAlignment', 'left',...
  1580. 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],...
  1581. 'BusyAction','queue',...
  1582. 'TooltipString','Change brightness',...
  1583. 'Min',-1,'Max',1,'SliderStep',slider_step, ...
  1584. 'Value',0, ...
  1585. 'visible','off', ...
  1586. 'Callback','view_nii(''brightness'');');
  1587. if usepanel
  1588. set(handles.brightness, 'visible', 'on');
  1589. end
  1590. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1591. set(handles.brightness, 'enable', 'off');
  1592. end
  1593. set(handles.brightness,'position',pos); % linux66
  1594. % Contrast text/def
  1595. %
  1596. x = info_pos(1) + info_pos(3)*0.025;
  1597. y = y + inputline_space;
  1598. w = info_pos(3)*0.22;
  1599. pos = [x y w h];
  1600. handles.Tcontrast = uicontrol('Parent',fig,'Style','text', ...
  1601. 'Units','Normalized', Font, ...
  1602. 'Position',pos, 'HorizontalAlignment', 'left',...
  1603. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1604. 'BusyAction','queue',...
  1605. 'visible','off', ...
  1606. 'String','Contrast:');
  1607. if usepanel
  1608. set(handles.Tcontrast, 'visible', 'on');
  1609. end
  1610. if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
  1611. set(handles.Tcontrast, 'enable', 'off');
  1612. end
  1613. if nii_view.numscan > 1
  1614. set(handles.Tcontrast, 'string', 'Scan ID:');
  1615. set(handles.contrast, 'TooltipString', 'Change Scan ID');
  1616. elseif colorindex < 2 | colorindex > 3
  1617. set(handles.Tcontrast, 'visible', 'off');
  1618. end
  1619. x = x + w;
  1620. w = info_pos(3)*0.18;
  1621. pos = [x y w h];
  1622. Font.FontSize = 8;
  1623. handles.contrast_def = uicontrol('Parent',fig,'Style','push', ...
  1624. 'Units','Normalized', Font, ...
  1625. 'Position',pos, 'HorizontalAlignment', 'center',...
  1626. 'TooltipString','Restore initial contrast',...
  1627. 'String','Reset',...
  1628. 'visible','off', ...
  1629. 'Callback','view_nii(''contrast_def'');');
  1630. if usepanel
  1631. set(handles.contrast_def, 'visible', 'on');
  1632. end
  1633. if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1
  1634. set(handles.contrast_def, 'enable', 'off');
  1635. end
  1636. if nii_view.numscan > 1
  1637. set(handles.contrast_def, 'style', 'edit', 'background', 'w', ...
  1638. 'TooltipString','Scan (or volume) index in the time series',...
  1639. 'string', '1', 'Callback', 'view_nii(''edit_change_scan'');');
  1640. elseif colorindex < 2 | colorindex > 3
  1641. set(handles.contrast_def, 'visible', 'off');
  1642. end
  1643. % Brightness text/def
  1644. %
  1645. x = info_pos(1) + info_pos(3)*0.5;
  1646. w = info_pos(3)*0.295;
  1647. pos = [x y w h];
  1648. Font.FontSize = 12;
  1649. handles.Tbrightness = uicontrol('Parent',fig,'Style','text', ...
  1650. 'Units','Normalized', Font, ...
  1651. 'Position',pos, 'HorizontalAlignment', 'left',...
  1652. 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],...
  1653. 'BusyAction','queue',...
  1654. 'visible','off', ...
  1655. 'String','Brightness:');
  1656. if usepanel
  1657. set(handles.Tbrightness, 'visible', 'on');
  1658. end
  1659. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1660. set(handles.Tbrightness, 'enable', 'off');
  1661. end
  1662. x = x + w;
  1663. w = info_pos(3)*0.18;
  1664. pos = [x y w h];
  1665. Font.FontSize = 8;
  1666. handles.brightness_def = uicontrol('Parent',fig,'Style','push', ...
  1667. 'Units','Normalized', Font, ...
  1668. 'Position',pos, 'HorizontalAlignment', 'center',...
  1669. 'TooltipString','Restore initial brightness',...
  1670. 'String','Reset',...
  1671. 'visible','off', ...
  1672. 'Callback','view_nii(''brightness_def'');');
  1673. if usepanel
  1674. set(handles.brightness_def, 'visible', 'on');
  1675. end
  1676. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1677. set(handles.brightness_def, 'enable', 'off');
  1678. end
  1679. % init image handles
  1680. %
  1681. handles.axial_image = [];
  1682. handles.coronal_image = [];
  1683. handles.sagittal_image = [];
  1684. % plot axial view
  1685. %
  1686. if ~isempty(nii_view.bgimg)
  1687. bg_slice = squeeze(bgimg(:,:,nii_view.slices.axi));
  1688. h1 = plot_view(fig, xdim, ydim, top_ax, bg_slice', clim, cbarminmax, ...
  1689. handles, useimagesc, colorindex, color_map, ...
  1690. colorlevel, highcolor, useinterp, nii_view.numscan);
  1691. handles.axial_bg = h1;
  1692. else
  1693. handles.axial_bg = [];
  1694. end
  1695. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1696. img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,:,setscanid));
  1697. img_slice = permute(img_slice, [2 1 3]);
  1698. else
  1699. img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,setscanid));
  1700. img_slice = img_slice';
  1701. end
  1702. h1 = plot_view(fig, xdim, ydim, top_ax, img_slice, clim, cbarminmax, ...
  1703. handles, useimagesc, colorindex, color_map, ...
  1704. colorlevel, highcolor, useinterp, nii_view.numscan);
  1705. set(h1,'buttondown','view_nii(''axial_image'');');
  1706. handles.axial_image = h1;
  1707. handles.axial_axes = top_ax;
  1708. if size(img_slice,1) == 1 | size(img_slice,2) == 1
  1709. set(top_ax,'visible','off');
  1710. if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
  1711. set(handles.sagittal_slider, 'visible', 'off');
  1712. end
  1713. if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
  1714. set(handles.coronal_slider, 'visible', 'off');
  1715. end
  1716. if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
  1717. set(handles.axial_slider, 'visible', 'off');
  1718. end
  1719. end
  1720. % plot coronal view
  1721. %
  1722. if ~isempty(nii_view.bgimg)
  1723. bg_slice = squeeze(bgimg(:,nii_view.slices.cor,:));
  1724. h1 = plot_view(fig, xdim, zdim, front_ax, bg_slice', clim, cbarminmax, ...
  1725. handles, useimagesc, colorindex, color_map, ...
  1726. colorlevel, highcolor, useinterp, nii_view.numscan);
  1727. handles.coronal_bg = h1;
  1728. else
  1729. handles.coronal_bg = [];
  1730. end
  1731. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1732. img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,:,setscanid));
  1733. img_slice = permute(img_slice, [2 1 3]);
  1734. else
  1735. img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,setscanid));
  1736. img_slice = img_slice';
  1737. end
  1738. h1 = plot_view(fig, xdim, zdim, front_ax, img_slice, clim, cbarminmax, ...
  1739. handles, useimagesc, colorindex, color_map, ...
  1740. colorlevel, highcolor, useinterp, nii_view.numscan);
  1741. set(h1,'buttondown','view_nii(''coronal_image'');');
  1742. handles.coronal_image = h1;
  1743. handles.coronal_axes = front_ax;
  1744. if size(img_slice,1) == 1 | size(img_slice,2) == 1
  1745. set(front_ax,'visible','off');
  1746. if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
  1747. set(handles.sagittal_slider, 'visible', 'off');
  1748. end
  1749. if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
  1750. set(handles.coronal_slider, 'visible', 'off');
  1751. end
  1752. if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
  1753. set(handles.axial_slider, 'visible', 'off');
  1754. end
  1755. end
  1756. % plot sagittal view
  1757. %
  1758. if ~isempty(nii_view.bgimg)
  1759. bg_slice = squeeze(bgimg(nii_view.slices.sag,:,:));
  1760. h1 = plot_view(fig, ydim, zdim, side_ax, bg_slice', clim, cbarminmax, ...
  1761. handles, useimagesc, colorindex, color_map, ...
  1762. colorlevel, highcolor, useinterp, nii_view.numscan);
  1763. handles.sagittal_bg = h1;
  1764. else
  1765. handles.sagittal_bg = [];
  1766. end
  1767. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1768. img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,:,setscanid));
  1769. img_slice = permute(img_slice, [2 1 3]);
  1770. else
  1771. img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,setscanid));
  1772. img_slice = img_slice';
  1773. end
  1774. h1 = plot_view(fig, ydim, zdim, side_ax, img_slice, clim, cbarminmax, ...
  1775. handles, useimagesc, colorindex, color_map, ...
  1776. colorlevel, highcolor, useinterp, nii_view.numscan);
  1777. set(h1,'buttondown','view_nii(''sagittal_image'');');
  1778. set(side_ax,'Xdir', 'reverse');
  1779. handles.sagittal_image = h1;
  1780. handles.sagittal_axes = side_ax;
  1781. if size(img_slice,1) == 1 | size(img_slice,2) == 1
  1782. set(side_ax,'visible','off');
  1783. if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
  1784. set(handles.sagittal_slider, 'visible', 'off');
  1785. end
  1786. if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
  1787. set(handles.coronal_slider, 'visible', 'off');
  1788. end
  1789. if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
  1790. set(handles.axial_slider, 'visible', 'off');
  1791. end
  1792. end
  1793. [top1_label, top2_label, side1_label, side2_label] = ...
  1794. dir_label(fig, top_ax, front_ax, side_ax);
  1795. % store label handles
  1796. %
  1797. handles.top1_label = top1_label;
  1798. handles.top2_label = top2_label;
  1799. handles.side1_label = side1_label;
  1800. handles.side2_label = side2_label;
  1801. % plot colorbar
  1802. %
  1803. if ~isempty(cbar_axes) & ~isempty(cbarminmax_axes)
  1804. if 0
  1805. if isempty(color_map)
  1806. level = colorlevel + num_highcolor;
  1807. else
  1808. level = size([color_map; highcolor], 1);
  1809. end
  1810. end
  1811. if isempty(color_map)
  1812. level = colorlevel;
  1813. else
  1814. level = size([color_map], 1);
  1815. end
  1816. niiclass = class(nii.img);
  1817. h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ...
  1818. level, handles, useimagesc, colorindex, color_map, ...
  1819. colorlevel, highcolor, niiclass, nii_view.numscan);
  1820. handles.cbar_image = h1;
  1821. handles.cbar_axes = cbar_axes;
  1822. handles.cbarminmax_axes = cbarminmax_axes;
  1823. end
  1824. nii_view.handles = handles; % store handles
  1825. nii_view.usepanel = usepanel; % whole panel at low right cornor
  1826. nii_view.usestretch = usestretch; % stretch display of voxel_size
  1827. nii_view.useinterp = useinterp; % use interpolation
  1828. nii_view.colorindex = colorindex; % store colorindex variable
  1829. nii_view.buttondown = buttondown; % command after button down click
  1830. nii_view.cbarminmax = cbarminmax; % store min max value for colorbar
  1831. set_coordinates(nii_view,useinterp); % coord unit
  1832. if ~isfield(nii_view, 'axi_xhair') | ...
  1833. ~isfield(nii_view, 'cor_xhair') | ...
  1834. ~isfield(nii_view, 'sag_xhair')
  1835. nii_view.axi_xhair = []; % top cross hair
  1836. nii_view.cor_xhair = []; % front cross hair
  1837. nii_view.sag_xhair = []; % side cross hair
  1838. end
  1839. if ~isempty(color_map)
  1840. nii_view.color_map = color_map;
  1841. end
  1842. if ~isempty(colorlevel)
  1843. nii_view.colorlevel = colorlevel;
  1844. end
  1845. if ~isempty(highcolor)
  1846. nii_view.highcolor = highcolor;
  1847. end
  1848. update_nii_view(nii_view);
  1849. if ~isempty(setunit)
  1850. update_unit(fig, setunit);
  1851. end
  1852. if ~isempty(setviewpoint)
  1853. update_viewpoint(fig, setviewpoint);
  1854. end
  1855. if ~isempty(setcrosshaircolor)
  1856. update_crosshaircolor(fig, setcrosshaircolor);
  1857. end
  1858. if ~isempty(usecrosshair)
  1859. update_usecrosshair(fig, usecrosshair);
  1860. end
  1861. nii_menu = getappdata(fig, 'nii_menu');
  1862. if ~isempty(nii_menu)
  1863. if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511
  1864. set(nii_menu.Minterp,'Userdata',1,'Label','Interp on','enable','off');
  1865. elseif useinterp
  1866. set(nii_menu.Minterp,'Userdata',0,'Label','Interp off');
  1867. else
  1868. set(nii_menu.Minterp,'Userdata',1,'Label','Interp on');
  1869. end
  1870. end
  1871. windowbuttonmotion = get(fig, 'windowbuttonmotion');
  1872. windowbuttonmotion = [windowbuttonmotion '; view_nii(''move_cursor'');'];
  1873. set(fig, 'windowbuttonmotion', windowbuttonmotion);
  1874. return; % init
  1875. %----------------------------------------------------------------
  1876. function fig = update_img(img, fig, opt)
  1877. nii_menu = getappdata(fig,'nii_menu');
  1878. if ~isempty(nii_menu)
  1879. set(nii_menu.Mzoom,'Userdata',1,'Label','Zoom on');
  1880. set(fig,'pointer','arrow');
  1881. zoom off;
  1882. end
  1883. nii_view = getappdata(fig,'nii_view');
  1884. change_interp = 0;
  1885. if isfield(opt, 'useinterp') & opt.useinterp ~= nii_view.useinterp
  1886. nii_view.useinterp = opt.useinterp;
  1887. change_interp = 1;
  1888. end
  1889. setscanid = 1;
  1890. if isfield(opt, 'setscanid')
  1891. setscanid = round(opt.setscanid);
  1892. if setscanid < 1
  1893. setscanid = 1;
  1894. end
  1895. if setscanid > nii_view.numscan
  1896. setscanid = nii_view.numscan;
  1897. end
  1898. end
  1899. if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax)
  1900. minvalue = opt.glblocminmax(1);
  1901. maxvalue = opt.glblocminmax(2);
  1902. else
  1903. minvalue = img(:,:,:,setscanid);
  1904. minvalue = double(minvalue(:));
  1905. minvalue = min(minvalue(~isnan(minvalue)));
  1906. maxvalue = img(:,:,:,setscanid);
  1907. maxvalue = double(maxvalue(:));
  1908. maxvalue = max(maxvalue(~isnan(maxvalue)));
  1909. end
  1910. if isfield(opt, 'setvalue')
  1911. setvalue = opt.setvalue;
  1912. if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax)
  1913. minvalue = opt.glblocminmax(1);
  1914. maxvalue = opt.glblocminmax(2);
  1915. else
  1916. minvalue = double(min(setvalue.val));
  1917. maxvalue = double(max(setvalue.val));
  1918. end
  1919. bgimg = double(img);
  1920. minbg = double(min(bgimg(:)));
  1921. maxbg = double(max(bgimg(:)));
  1922. bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256
  1923. cbarminmax = [minvalue maxvalue];
  1924. if nii_view.useinterp
  1925. % scale signal data to 1~200
  1926. %
  1927. img = repmat(nan, size(img));
  1928. img(setvalue.idx) = setvalue.val;
  1929. % 200 level for source image
  1930. %
  1931. bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199));
  1932. else
  1933. bgimg(setvalue.idx) = NaN;
  1934. minbg = double(min(bgimg(:)));
  1935. maxbg = double(max(bgimg(:)));
  1936. bgimg(setvalue.idx) = minbg;
  1937. % bgimg must be normalized to [201 256]
  1938. %
  1939. bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201;
  1940. bgimg(setvalue.idx) = 0;
  1941. % scale signal data to 1~200
  1942. %
  1943. img = zeros(size(img));
  1944. img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199);
  1945. img = img + bgimg;
  1946. bgimg = [];
  1947. img = scale_out(img, cbarminmax(1), cbarminmax(2), 199);
  1948. minvalue = double(min(img(:)));
  1949. maxvalue = double(max(img(:)));
  1950. if isfield(opt,'glblocminmax') & ~isempty(opt.glblocminmax)
  1951. minvalue = opt.glblocminmax(1);
  1952. end
  1953. end
  1954. nii_view.bgimg = bgimg;
  1955. nii_view.setvalue = setvalue;
  1956. else
  1957. cbarminmax = [minvalue maxvalue];
  1958. end
  1959. update_cbarminmax(fig, cbarminmax);
  1960. nii_view.cbarminmax = cbarminmax;
  1961. nii_view.nii.img = img;
  1962. nii_view.minvalue = minvalue;
  1963. nii_view.maxvalue = maxvalue;
  1964. nii_view.scanid = setscanid;
  1965. change_colormap(fig);
  1966. % init color (gray) scaling to make sure the slice clim take the
  1967. % global clim [min(nii.img(:)) max(nii.img(:))]
  1968. %
  1969. if isempty(nii_view.bgimg)
  1970. clim = [minvalue maxvalue];
  1971. else
  1972. clim = [minvalue double(max(nii_view.bgimg(:)))];
  1973. end
  1974. if clim(1) == clim(2)
  1975. clim(2) = clim(1) + 0.000001;
  1976. end
  1977. if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
  1978. useimagesc = 0;
  1979. else
  1980. useimagesc = 1;
  1981. end
  1982. if ~isempty(nii_view.bgimg) % with interpolation
  1983. Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi));
  1984. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
  1985. set(nii_view.handles.axial_bg,'CData',double(Saxi)');
  1986. else
  1987. axes(nii_view.handles.axial_axes);
  1988. if useimagesc
  1989. nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'edgecolor','none','facecolor','interp');
  1990. else
  1991. nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  1992. end
  1993. order = get(gca,'child');
  1994. order(find(order == nii_view.handles.axial_bg)) = [];
  1995. order = [order; nii_view.handles.axial_bg];
  1996. set(gca, 'child', order);
  1997. end
  1998. end
  1999. if isfield(nii_view.handles,'axial_image'),
  2000. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  2001. Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,:,setscanid));
  2002. Saxi = permute(Saxi, [2 1 3]);
  2003. else
  2004. Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,setscanid));
  2005. Saxi = Saxi';
  2006. end
  2007. set(nii_view.handles.axial_image,'CData',double(Saxi));
  2008. end
  2009. set(nii_view.handles.axial_axes,'CLim',clim);
  2010. if ~isempty(nii_view.bgimg)
  2011. Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:));
  2012. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
  2013. set(nii_view.handles.coronal_bg,'CData',double(Scor)');
  2014. else
  2015. axes(nii_view.handles.coronal_axes);
  2016. if useimagesc
  2017. nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'edgecolor','none','facecolor','interp');
  2018. else
  2019. nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  2020. end
  2021. order = get(gca,'child');
  2022. order(find(order == nii_view.handles.coronal_bg)) = [];
  2023. order = [order; nii_view.handles.coronal_bg];
  2024. set(gca, 'child', order);
  2025. end
  2026. end
  2027. if isfield(nii_view.handles,'coronal_image'),
  2028. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  2029. Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,:,setscanid));
  2030. Scor = permute(Scor, [2 1 3]);
  2031. else
  2032. Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,setscanid));
  2033. Scor = Scor';
  2034. end
  2035. set(nii_view.handles.coronal_image,'CData',double(Scor));
  2036. end
  2037. set(nii_view.handles.coronal_axes,'CLim',clim);
  2038. if ~isempty(nii_view.bgimg)
  2039. Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:));
  2040. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
  2041. set(nii_view.handles.sagittal_bg,'CData',double(Ssag)');
  2042. else
  2043. axes(nii_view.handles.sagittal_axes);
  2044. if useimagesc
  2045. nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'edgecolor','none','facecolor','interp');
  2046. else
  2047. nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  2048. end
  2049. order = get(gca,'child');
  2050. order(find(order == nii_view.handles.sagittal_bg)) = [];
  2051. order = [order; nii_view.handles.sagittal_bg];
  2052. set(gca, 'child', order);
  2053. end
  2054. end
  2055. if isfield(nii_view.handles,'sagittal_image'),
  2056. if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511
  2057. Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,:,setscanid));
  2058. Ssag = permute(Ssag, [2 1 3]);
  2059. else
  2060. Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,setscanid));
  2061. Ssag = Ssag';
  2062. end
  2063. set(nii_view.handles.sagittal_image,'CData',double(Ssag));
  2064. end
  2065. set(nii_view.handles.sagittal_axes,'CLim',clim);
  2066. update_nii_view(nii_view);
  2067. if isfield(opt, 'setvalue')
  2068. if ~isfield(nii_view,'highcolor') | ~isequal(size(nii_view.highcolor),[56 3])
  2069. % 55 level for brain structure (paded 0 for highcolor level 1, i.e. normal level 201, to make 56 highcolor)
  2070. %
  2071. update_highcolor(fig, [zeros(1,3);gray(55)], []);
  2072. end
  2073. if nii_view.colorindex ~= 2
  2074. update_colorindex(fig, 2);
  2075. end
  2076. old_color = get(nii_view.handles.xhair_color,'user');
  2077. if isequal(old_color, [1 0 0])
  2078. update_crosshaircolor(fig, [1 1 0]);
  2079. end
  2080. % if change_interp
  2081. % update_useinterp(fig, nii_view.useinterp);
  2082. % end
  2083. end
  2084. if change_interp
  2085. update_useinterp(fig, nii_view.useinterp);
  2086. end
  2087. return; % update_img
  2088. %----------------------------------------------------------------
  2089. function [top_pos, front_pos, side_pos] = ...
  2090. axes_pos(fig,area,vol_size,usestretch)
  2091. set(fig,'unit','pixel');
  2092. fig_pos = get(fig,'position');
  2093. gap_x = 15/fig_pos(3); % width of vertical scrollbar
  2094. gap_y = 15/fig_pos(4); % width of horizontal scrollbar
  2095. a = (area(3) - gap_x * 1.3) * fig_pos(3) / (vol_size(1) + vol_size(2)); % no crosshair lost in zoom
  2096. b = (area(4) - gap_y * 3) * fig_pos(4) / (vol_size(2) + vol_size(3));
  2097. c = min([a b]); % make sure 'ax' is inside 'area'
  2098. top_w = vol_size(1) * c / fig_pos(3);
  2099. side_w = vol_size(2) * c / fig_pos(3);
  2100. top_h = vol_size(2) * c / fig_pos(4);
  2101. side_h = vol_size(3) * c / fig_pos(4);
  2102. side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom
  2103. side_y = area(2) + top_h + gap_y * 3;
  2104. if usestretch
  2105. if a > b % top touched ceiling, use b
  2106. d = (area(3) - gap_x * 1.3) / (top_w + side_w); % no crosshair lost in zoom
  2107. top_w = top_w * d;
  2108. side_w = side_w * d;
  2109. side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom
  2110. else
  2111. d = (area(4) - gap_y * 3) / (top_h + side_h);
  2112. top_h = top_h * d;
  2113. side_h = side_h * d;
  2114. side_y = area(2) + top_h + gap_y * 3;
  2115. end
  2116. end
  2117. top_pos = [area(1) area(2)+gap_y top_w top_h];
  2118. front_pos = [area(1) side_y top_w side_h];
  2119. side_pos = [side_x side_y side_w side_h];
  2120. set(fig,'unit','normal');
  2121. return; % axes_pos
  2122. %----------------------------------------------------------------
  2123. function [top_ax, front_ax, side_ax] ...
  2124. = create_ax(fig, area, vol_size, usestretch)
  2125. cur_fig = gcf; % save h_wait fig
  2126. figure(fig);
  2127. [top_pos, front_pos, side_pos] = ...
  2128. axes_pos(fig,area,vol_size,usestretch);
  2129. nii_view = getappdata(fig, 'nii_view');
  2130. if isempty(nii_view)
  2131. top_ax = axes('position', top_pos);
  2132. front_ax = axes('position', front_pos);
  2133. side_ax = axes('position', side_pos);
  2134. else
  2135. top_ax = nii_view.handles.axial_axes;
  2136. front_ax = nii_view.handles.coronal_axes;
  2137. side_ax = nii_view.handles.sagittal_axes;
  2138. set(top_ax, 'position', top_pos);
  2139. set(front_ax, 'position', front_pos);
  2140. set(side_ax, 'position', side_pos);
  2141. end
  2142. figure(cur_fig);
  2143. return; % create_ax
  2144. %----------------------------------------------------------------
  2145. function [cbar_axes, cbarminmax_axes] = create_cbar_axes(fig, cbar_area, nii_view)
  2146. if isempty(cbar_area) % without_cbar
  2147. cbar_axes = [];
  2148. cbarminmax_axes = [];
  2149. return;
  2150. end
  2151. cur_fig = gcf; % save h_wait fig
  2152. figure(fig);
  2153. if ~exist('nii_view', 'var')
  2154. nii_view = getappdata(fig, 'nii_view');
  2155. end
  2156. if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_axes') | isempty(nii_view.handles.cbar_axes)
  2157. cbarminmax_axes = axes('position', cbar_area);
  2158. cbar_axes = axes('position', cbar_area);
  2159. else
  2160. cbarminmax_axes = nii_view.handles.cbarminmax_axes;
  2161. cbar_axes = nii_view.handles.cbar_axes;
  2162. set(cbarminmax_axes, 'position', cbar_area);
  2163. set(cbar_axes, 'position', cbar_area);
  2164. end
  2165. figure(cur_fig);
  2166. return; % create_cbar_axes
  2167. %----------------------------------------------------------------
  2168. function h1 = plot_view(fig, x, y, img_ax, img_slice, clim, ...
  2169. cbarminmax, handles, useimagesc, colorindex, color_map, ...
  2170. colorlevel, highcolor, useinterp, numscan)
  2171. h1 = [];
  2172. if x > 1 & y > 1,
  2173. axes(img_ax);
  2174. nii_view = getappdata(fig, 'nii_view');
  2175. if isempty(nii_view)
  2176. % set colormap first
  2177. %
  2178. nii.handles = handles;
  2179. nii.handles.axial_axes = img_ax;
  2180. nii.colorindex = colorindex;
  2181. nii.color_map = color_map;
  2182. nii.colorlevel = colorlevel;
  2183. nii.highcolor = highcolor;
  2184. nii.numscan = numscan;
  2185. change_colormap(fig, nii, colorindex, cbarminmax);
  2186. if useinterp
  2187. if useimagesc
  2188. h1 = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
  2189. else
  2190. h1 = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  2191. end
  2192. set(gca,'clim',clim);
  2193. else
  2194. if useimagesc
  2195. h1 = imagesc(img_slice,clim);
  2196. else
  2197. h1 = image(img_slice);
  2198. end
  2199. set(gca,'clim',clim);
  2200. end
  2201. else
  2202. h1 = nii_view.handles.axial_image;
  2203. if ~isequal(get(h1,'parent'), img_ax)
  2204. h1 = nii_view.handles.coronal_image;
  2205. end
  2206. if ~isequal(get(h1,'parent'), img_ax)
  2207. h1 = nii_view.handles.sagittal_image;
  2208. end
  2209. set(h1, 'cdata', double(img_slice));
  2210. set(h1, 'xdata', 1:size(img_slice,2));
  2211. set(h1, 'ydata', 1:size(img_slice,1));
  2212. end
  2213. set(img_ax,'YDir','normal','XLimMode','manual','YLimMode','manual',...
  2214. 'ClimMode','manual','visible','off', ...
  2215. 'xtick',[],'ytick',[], 'clim', clim);
  2216. end
  2217. return; % plot_view
  2218. %----------------------------------------------------------------
  2219. function h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ...
  2220. level, handles, useimagesc, colorindex, color_map, ...
  2221. colorlevel, highcolor, niiclass, numscan, nii_view)
  2222. cbar_image = [1:level]';
  2223. % In a uint8 or uint16 indexed image, 0 points to the first row
  2224. % in the colormap
  2225. %
  2226. if 0 % strcmpi(niiclass,'uint8') | strcmpi(niiclass,'uint16')
  2227. % we use single for display anyway
  2228. ylim = [0, level-1];
  2229. else
  2230. ylim = [1, level];
  2231. end
  2232. axes(cbarminmax_axes);
  2233. plot([0 0], cbarminmax, 'w');
  2234. axis tight;
  2235. set(cbarminmax_axes,'YDir','normal', ...
  2236. 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ...
  2237. 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right');
  2238. ylimb = get(cbarminmax_axes,'ylim');
  2239. ytickb = get(cbarminmax_axes,'ytick');
  2240. ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1);
  2241. axes(cbar_axes);
  2242. if ~exist('nii_view', 'var')
  2243. nii_view = getappdata(fig, 'nii_view');
  2244. end
  2245. if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_image') | isempty(nii_view.handles.cbar_image)
  2246. % set colormap first
  2247. %
  2248. nii.handles = handles;
  2249. nii.colorindex = colorindex;
  2250. nii.color_map = color_map;
  2251. nii.colorlevel = colorlevel;
  2252. nii.highcolor = highcolor;
  2253. nii.numscan = numscan;
  2254. change_colormap(fig, nii, colorindex, cbarminmax);
  2255. h1 = image([0,1], [ylim(1),ylim(2)], cbar_image);
  2256. else
  2257. h1 = nii_view.handles.cbar_image;
  2258. set(h1, 'cdata', double(cbar_image));
  2259. end
  2260. set(cbar_axes,'YDir','normal','XLimMode','manual', ...
  2261. 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ...
  2262. 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel','');
  2263. return; % plot_cbar
  2264. %----------------------------------------------------------------
  2265. function set_coordinates(nii_view,useinterp)
  2266. imgPlim.vox = nii_view.dims;
  2267. imgNlim.vox = [1 1 1];
  2268. if useinterp
  2269. xdata_ax = [imgNlim.vox(1) imgPlim.vox(1)];
  2270. ydata_ax = [imgNlim.vox(2) imgPlim.vox(2)];
  2271. zdata_ax = [imgNlim.vox(3) imgPlim.vox(3)];
  2272. else
  2273. xdata_ax = [imgNlim.vox(1)-0.5 imgPlim.vox(1)+0.5];
  2274. ydata_ax = [imgNlim.vox(2)-0.5 imgPlim.vox(2)+0.5];
  2275. zdata_ax = [imgNlim.vox(3)-0.5 imgPlim.vox(3)+0.5];
  2276. end
  2277. if isfield(nii_view.handles,'axial_image') & ~isempty(nii_view.handles.axial_image)
  2278. set(nii_view.handles.axial_axes,'Xlim',xdata_ax);
  2279. set(nii_view.handles.axial_axes,'Ylim',ydata_ax);
  2280. end;
  2281. if isfield(nii_view.handles,'coronal_image') & ~isempty(nii_view.handles.coronal_image)
  2282. set(nii_view.handles.coronal_axes,'Xlim',xdata_ax);
  2283. set(nii_view.handles.coronal_axes,'Ylim',zdata_ax);
  2284. end;
  2285. if isfield(nii_view.handles,'sagittal_image') & ~isempty(nii_view.handles.sagittal_image)
  2286. set(nii_view.handles.sagittal_axes,'Xlim',ydata_ax);
  2287. set(nii_view.handles.sagittal_axes,'Ylim',zdata_ax);
  2288. end;
  2289. return % set_coordinates
  2290. %----------------------------------------------------------------
  2291. function set_image_value(nii_view),
  2292. % get coordinates of selected voxel and the image intensity there
  2293. %
  2294. sag = round(nii_view.slices.sag);
  2295. cor = round(nii_view.slices.cor);
  2296. axi = round(nii_view.slices.axi);
  2297. if 0 % isfield(nii_view, 'disp')
  2298. img = nii_view.disp;
  2299. else
  2300. img = nii_view.nii.img;
  2301. end
  2302. if nii_view.nii.hdr.dime.datatype == 128
  2303. imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
  2304. set(nii_view.handles.imval,'Value',imgvalue);
  2305. set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
  2306. elseif nii_view.nii.hdr.dime.datatype == 511
  2307. R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  2308. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  2309. G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  2310. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  2311. B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  2312. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  2313. imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
  2314. set(nii_view.handles.imval,'Value',imgvalue);
  2315. imgvalue = [R G B];
  2316. set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
  2317. else
  2318. imgvalue = double(img(sag,cor,axi,nii_view.scanid));
  2319. set(nii_view.handles.imval,'Value',imgvalue);
  2320. if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2)
  2321. imgvalue = 0;
  2322. end
  2323. set(nii_view.handles.imval,'String',sprintf('%.6g',imgvalue));
  2324. end
  2325. % Now update the coordinates of the selected voxel
  2326. nii_view = update_imgXYZ(nii_view);
  2327. if get(nii_view.handles.coord,'value') == 1,
  2328. sag = nii_view.imgXYZ.vox(1);
  2329. cor = nii_view.imgXYZ.vox(2);
  2330. axi = nii_view.imgXYZ.vox(3);
  2331. org = nii_view.origin;
  2332. elseif get(nii_view.handles.coord,'value') == 2,
  2333. sag = nii_view.imgXYZ.mm(1);
  2334. cor = nii_view.imgXYZ.mm(2);
  2335. axi = nii_view.imgXYZ.mm(3);
  2336. org = [0 0 0];
  2337. elseif get(nii_view.handles.coord,'value') == 3,
  2338. sag = nii_view.imgXYZ.tal(1);
  2339. cor = nii_view.imgXYZ.tal(2);
  2340. axi = nii_view.imgXYZ.tal(3);
  2341. org = [0 0 0];
  2342. end
  2343. set(nii_view.handles.impos,'Value',[sag,cor,axi]);
  2344. if get(nii_view.handles.coord,'value') == 1,
  2345. string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi);
  2346. org_str = sprintf('%7.0f %7.0f %7.0f', org(1), org(2), org(3));
  2347. else
  2348. string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi);
  2349. org_str = sprintf('%7.1f %7.1f %7.1f', org(1), org(2), org(3));
  2350. end;
  2351. set(nii_view.handles.impos,'String',string);
  2352. set(nii_view.handles.origin, 'string', org_str);
  2353. return % set_image_value
  2354. %----------------------------------------------------------------
  2355. function nii_view = get_slice_position(nii_view,view),
  2356. % obtain slices that is in correct unit, then update slices
  2357. %
  2358. slices = nii_view.slices;
  2359. switch view,
  2360. case 'sag',
  2361. currentpoint = get(nii_view.handles.sagittal_axes,'CurrentPoint');
  2362. slices.cor = currentpoint(1,1);
  2363. slices.axi = currentpoint(1,2);
  2364. case 'cor',
  2365. currentpoint = get(nii_view.handles.coronal_axes,'CurrentPoint');
  2366. slices.sag = currentpoint(1,1);
  2367. slices.axi = currentpoint(1,2);
  2368. case 'axi',
  2369. currentpoint = get(nii_view.handles.axial_axes,'CurrentPoint');
  2370. slices.sag = currentpoint(1,1);
  2371. slices.cor = currentpoint(1,2);
  2372. end
  2373. % update nii_view.slices with the updated slices
  2374. %
  2375. nii_view.slices.axi = round(slices.axi);
  2376. nii_view.slices.cor = round(slices.cor);
  2377. nii_view.slices.sag = round(slices.sag);
  2378. return % get_slice_position
  2379. %----------------------------------------------------------------
  2380. function nii_view = get_slider_position(nii_view),
  2381. [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi] = deal(0);
  2382. if isfield(nii_view.handles,'sagittal_slider'),
  2383. if ishandle(nii_view.handles.sagittal_slider),
  2384. nii_view.slices.sag = ...
  2385. round(get(nii_view.handles.sagittal_slider,'Value'));
  2386. end
  2387. end
  2388. if isfield(nii_view.handles,'coronal_slider'),
  2389. if ishandle(nii_view.handles.coronal_slider),
  2390. nii_view.slices.cor = ...
  2391. round(nii_view.dims(2) - ...
  2392. get(nii_view.handles.coronal_slider,'Value') + 1);
  2393. end
  2394. end
  2395. if isfield(nii_view.handles,'axial_slider'),
  2396. if ishandle(nii_view.handles.axial_slider),
  2397. nii_view.slices.axi = ...
  2398. round(get(nii_view.handles.axial_slider,'Value'));
  2399. end
  2400. end
  2401. nii_view = check_slices(nii_view);
  2402. return % get_slider_position
  2403. %----------------------------------------------------------------
  2404. function nii_view = update_imgXYZ(nii_view),
  2405. nii_view.imgXYZ.vox = ...
  2406. [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi];
  2407. nii_view.imgXYZ.mm = ...
  2408. (nii_view.imgXYZ.vox - nii_view.origin) .* nii_view.voxel_size;
  2409. % nii_view.imgXYZ.tal = mni2tal(nii_view.imgXYZ.mni);
  2410. return % update_imgXYZ
  2411. %----------------------------------------------------------------
  2412. function nii_view = convert2voxel(nii_view,slices),
  2413. if get(nii_view.handles.coord,'value') == 1,
  2414. % [slices.axi, slices.cor, slices.sag] are in vox
  2415. %
  2416. nii_view.slices.axi = round(slices.axi);
  2417. nii_view.slices.cor = round(slices.cor);
  2418. nii_view.slices.sag = round(slices.sag);
  2419. elseif get(nii_view.handles.coord,'value') == 2,
  2420. % [slices.axi, slices.cor, slices.sag] are in mm
  2421. %
  2422. xpix = nii_view.voxel_size(1);
  2423. ypix = nii_view.voxel_size(2);
  2424. zpix = nii_view.voxel_size(3);
  2425. nii_view.slices.axi = round(slices.axi / zpix + nii_view.origin(3));
  2426. nii_view.slices.cor = round(slices.cor / ypix + nii_view.origin(2));
  2427. nii_view.slices.sag = round(slices.sag / xpix + nii_view.origin(1));
  2428. elseif get(nii_view.handles.coord,'value') == 3,
  2429. % [slices.axi, slices.cor, slices.sag] are in talairach
  2430. %
  2431. xpix = nii_view.voxel_size(1);
  2432. ypix = nii_view.voxel_size(2);
  2433. zpix = nii_view.voxel_size(3);
  2434. xyz_tal = [slices.sag, slices.cor, slices.axi];
  2435. xyz_mni = tal2mni(xyz_tal);
  2436. nii_view.slices.axi = round(xyz_mni(3) / zpix + nii_view.origin(3));
  2437. nii_view.slices.cor = round(xyz_mni(2) / ypix + nii_view.origin(2));
  2438. nii_view.slices.sag = round(xyz_mni(1) / xpix + nii_view.origin(1));
  2439. end
  2440. return % convert2voxel
  2441. %----------------------------------------------------------------
  2442. function nii_view = check_slices(nii_view),
  2443. img = nii_view.nii.img;
  2444. [ SagSize, CorSize, AxiSize, TimeSize ] = size(img);
  2445. if nii_view.slices.sag > SagSize, nii_view.slices.sag = SagSize; end;
  2446. if nii_view.slices.sag < 1, nii_view.slices.sag = 1; end;
  2447. if nii_view.slices.cor > CorSize, nii_view.slices.cor = CorSize; end;
  2448. if nii_view.slices.cor < 1, nii_view.slices.cor = 1; end;
  2449. if nii_view.slices.axi > AxiSize, nii_view.slices.axi = AxiSize; end;
  2450. if nii_view.slices.axi < 1, nii_view.slices.axi = 1; end;
  2451. if nii_view.scanid > TimeSize, nii_view.scanid = TimeSize; end;
  2452. if nii_view.scanid < 1, nii_view.scanid = 1; end;
  2453. return % check_slices
  2454. %----------------------------------------------------------------
  2455. %
  2456. % keep this function small, since it will be called for every click
  2457. %
  2458. function nii_view = update_nii_view(nii_view)
  2459. % add imgXYZ into nii_view struct
  2460. %
  2461. nii_view = check_slices(nii_view);
  2462. nii_view = update_imgXYZ(nii_view);
  2463. % update xhair
  2464. %
  2465. p_axi = nii_view.imgXYZ.vox([1 2]);
  2466. p_cor = nii_view.imgXYZ.vox([1 3]);
  2467. p_sag = nii_view.imgXYZ.vox([2 3]);
  2468. nii_view.axi_xhair = ...
  2469. rri_xhair(p_axi, nii_view.axi_xhair, nii_view.handles.axial_axes);
  2470. nii_view.cor_xhair = ...
  2471. rri_xhair(p_cor, nii_view.cor_xhair, nii_view.handles.coronal_axes);
  2472. nii_view.sag_xhair = ...
  2473. rri_xhair(p_sag, nii_view.sag_xhair, nii_view.handles.sagittal_axes);
  2474. setappdata(nii_view.fig, 'nii_view', nii_view);
  2475. set_image_value(nii_view);
  2476. return; % update_nii_view
  2477. %----------------------------------------------------------------
  2478. function hist_plot(fig)
  2479. nii_view = getappdata(fig,'nii_view');
  2480. if isfield(nii_view, 'disp')
  2481. img = nii_view.disp;
  2482. else
  2483. img = nii_view.nii.img;
  2484. end
  2485. img = double(img(:));
  2486. if length(unique(round(img))) == length(unique(img))
  2487. is_integer = 1;
  2488. range = max(img) - min(img) + 1;
  2489. figure; hist(img, range);
  2490. set(gca, 'xlim', [-range/5, max(img)]);
  2491. else
  2492. is_integer = 0;
  2493. figure; hist(img);
  2494. end
  2495. xlabel('Voxel Intensity');
  2496. ylabel('Voxel Numbers for Each Intensity');
  2497. set(gcf, 'NumberTitle','off','Name','Histogram Plot');
  2498. return; % hist_plot
  2499. %----------------------------------------------------------------
  2500. function hist_eq(fig)
  2501. nii_view = getappdata(fig,'nii_view');
  2502. old_pointer = get(fig,'Pointer');
  2503. set(fig,'Pointer','watch');
  2504. if get(nii_view.handles.hist_eq,'value')
  2505. max_img = double(max(nii_view.nii.img(:)));
  2506. tmp = double(nii_view.nii.img) / max_img; % normalize for histeq
  2507. tmp = histeq(tmp(:));
  2508. nii_view.disp = reshape(tmp, size(nii_view.nii.img));
  2509. min_disp = min(nii_view.disp(:));
  2510. nii_view.disp = (nii_view.disp - min_disp); % range having eq hist
  2511. nii_view.disp = nii_view.disp * max_img / max(nii_view.disp(:));
  2512. nii_view.disp = single(nii_view.disp);
  2513. else
  2514. if isfield(nii_view, 'disp')
  2515. nii_view.disp = nii_view.nii.img;
  2516. else
  2517. set(fig,'Pointer',old_pointer);
  2518. return;
  2519. end
  2520. end
  2521. % update axial view
  2522. %
  2523. img_slice = squeeze(double(nii_view.disp(:,:,nii_view.slices.axi)));
  2524. h1 = nii_view.handles.axial_image;
  2525. set(h1, 'cdata', double(img_slice)');
  2526. % update coronal view
  2527. %
  2528. img_slice = squeeze(double(nii_view.disp(:,nii_view.slices.cor,:)));
  2529. h1 = nii_view.handles.coronal_image;
  2530. set(h1, 'cdata', double(img_slice)');
  2531. % update sagittal view
  2532. %
  2533. img_slice = squeeze(double(nii_view.disp(nii_view.slices.sag,:,:)));
  2534. h1 = nii_view.handles.sagittal_image;
  2535. set(h1, 'cdata', double(img_slice)');
  2536. % remove disp field if un-check 'histeq' button
  2537. %
  2538. if ~get(nii_view.handles.hist_eq,'value') & isfield(nii_view, 'disp')
  2539. nii_view = rmfield(nii_view, 'disp');
  2540. end
  2541. update_nii_view(nii_view);
  2542. set(fig,'Pointer',old_pointer);
  2543. return; % hist_eq
  2544. %----------------------------------------------------------------
  2545. function [top1_label, top2_label, side1_label, side2_label] = ...
  2546. dir_label(fig, top_ax, front_ax, side_ax)
  2547. nii_view = getappdata(fig,'nii_view');
  2548. top_pos = get(top_ax,'position');
  2549. front_pos = get(front_ax,'position');
  2550. side_pos = get(side_ax,'position');
  2551. top_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*top_pos(3));
  2552. top_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*top_pos(4));
  2553. side_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*side_pos(3));
  2554. side_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*side_pos(4));
  2555. top1_label_pos = [0, 1]; % rot0
  2556. top2_label_pos = [1, 0]; % rot90
  2557. side1_label_pos = [1, - side_gap_y]; % rot0
  2558. side2_label_pos = [0, 0]; % rot90
  2559. if isempty(nii_view)
  2560. axes(top_ax);
  2561. top1_label = text(double(top1_label_pos(1)),double(top1_label_pos(2)), ...
  2562. '== X =>', ...
  2563. 'vertical', 'bottom', ...
  2564. 'unit', 'normal', 'fontsize', 8);
  2565. axes(top_ax);
  2566. top2_label = text(double(top2_label_pos(1)),double(top2_label_pos(2)), ...
  2567. '== Y =>', ...
  2568. 'rotation', 90, 'vertical', 'top', ...
  2569. 'unit', 'normal', 'fontsize', 8);
  2570. axes(side_ax);
  2571. side1_label = text(double(side1_label_pos(1)),double(side1_label_pos(2)), ...
  2572. '<= Y ==', ...
  2573. 'horizontal', 'right', 'vertical', 'top', ...
  2574. 'unit', 'normal', 'fontsize', 8);
  2575. axes(side_ax);
  2576. side2_label = text(double(side2_label_pos(1)),double(side2_label_pos(2)), ...
  2577. '== Z =>', ...
  2578. 'rotation', 90, 'vertical', 'bottom', ...
  2579. 'unit', 'normal', 'fontsize', 8);
  2580. else
  2581. top1_label = nii_view.handles.top1_label;
  2582. top2_label = nii_view.handles.top2_label;
  2583. side1_label = nii_view.handles.side1_label;
  2584. side2_label = nii_view.handles.side2_label;
  2585. set(top1_label, 'position', [top1_label_pos 0]);
  2586. set(top2_label, 'position', [top2_label_pos 0]);
  2587. set(side1_label, 'position', [side1_label_pos 0]);
  2588. set(side2_label, 'position', [side2_label_pos 0]);
  2589. end
  2590. return; % dir_label
  2591. %----------------------------------------------------------------
  2592. function update_enable(h, opt);
  2593. nii_view = getappdata(h,'nii_view');
  2594. handles = nii_view.handles;
  2595. if isfield(opt,'enablecursormove')
  2596. if opt.enablecursormove
  2597. v = 'on';
  2598. else
  2599. v = 'off';
  2600. end
  2601. set(handles.Timposcur, 'visible', v);
  2602. set(handles.imposcur, 'visible', v);
  2603. set(handles.Timvalcur, 'visible', v);
  2604. set(handles.imvalcur, 'visible', v);
  2605. end
  2606. if isfield(opt,'enableviewpoint')
  2607. if opt.enableviewpoint
  2608. v = 'on';
  2609. else
  2610. v = 'off';
  2611. end
  2612. set(handles.Timpos, 'visible', v);
  2613. set(handles.impos, 'visible', v);
  2614. set(handles.Timval, 'visible', v);
  2615. set(handles.imval, 'visible', v);
  2616. end
  2617. if isfield(opt,'enableorigin')
  2618. if opt.enableorigin
  2619. v = 'on';
  2620. else
  2621. v = 'off';
  2622. end
  2623. set(handles.Torigin, 'visible', v);
  2624. set(handles.origin, 'visible', v);
  2625. end
  2626. if isfield(opt,'enableunit')
  2627. if opt.enableunit
  2628. v = 'on';
  2629. else
  2630. v = 'off';
  2631. end
  2632. set(handles.Tcoord, 'visible', v);
  2633. set(handles.coord_frame, 'visible', v);
  2634. set(handles.coord, 'visible', v);
  2635. end
  2636. if isfield(opt,'enablecrosshair')
  2637. if opt.enablecrosshair
  2638. v = 'on';
  2639. else
  2640. v = 'off';
  2641. end
  2642. set(handles.Txhair, 'visible', v);
  2643. set(handles.xhair_color, 'visible', v);
  2644. set(handles.xhair, 'visible', v);
  2645. end
  2646. if isfield(opt,'enablehistogram')
  2647. if opt.enablehistogram
  2648. v = 'on';
  2649. vv = 'off';
  2650. else
  2651. v = 'off';
  2652. vv = 'on';
  2653. end
  2654. set(handles.Tcoord, 'visible', vv);
  2655. set(handles.coord_frame, 'visible', vv);
  2656. set(handles.coord, 'visible', vv);
  2657. set(handles.Thist, 'visible', v);
  2658. set(handles.hist_frame, 'visible', v);
  2659. set(handles.hist_eq, 'visible', v);
  2660. set(handles.hist_plot, 'visible', v);
  2661. end
  2662. if isfield(opt,'enablecolormap')
  2663. if opt.enablecolormap
  2664. v = 'on';
  2665. else
  2666. v = 'off';
  2667. end
  2668. set(handles.Tcolor, 'visible', v);
  2669. set(handles.color_frame, 'visible', v);
  2670. set(handles.neg_color, 'visible', v);
  2671. set(handles.colorindex, 'visible', v);
  2672. end
  2673. if isfield(opt,'enablecontrast')
  2674. if opt.enablecontrast
  2675. v = 'on';
  2676. else
  2677. v = 'off';
  2678. end
  2679. set(handles.Tcontrast, 'visible', v);
  2680. set(handles.contrast_frame, 'visible', v);
  2681. set(handles.contrast_def, 'visible', v);
  2682. set(handles.contrast, 'visible', v);
  2683. end
  2684. if isfield(opt,'enablebrightness')
  2685. if opt.enablebrightness
  2686. v = 'on';
  2687. else
  2688. v = 'off';
  2689. end
  2690. set(handles.Tbrightness, 'visible', v);
  2691. set(handles.brightness_frame, 'visible', v);
  2692. set(handles.brightness_def, 'visible', v);
  2693. set(handles.brightness, 'visible', v);
  2694. end
  2695. if isfield(opt,'enabledirlabel')
  2696. if opt.enabledirlabel
  2697. v = 'on';
  2698. else
  2699. v = 'off';
  2700. end
  2701. set(handles.top1_label, 'visible', v);
  2702. set(handles.top2_label, 'visible', v);
  2703. set(handles.side1_label, 'visible', v);
  2704. set(handles.side2_label, 'visible', v);
  2705. end
  2706. if isfield(opt,'enableslider')
  2707. if opt.enableslider
  2708. v = 'on';
  2709. else
  2710. v = 'off';
  2711. end
  2712. if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
  2713. set(handles.sagittal_slider, 'visible', v);
  2714. end
  2715. if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
  2716. set(handles.coronal_slider, 'visible', v);
  2717. end
  2718. if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
  2719. set(handles.axial_slider, 'visible', v);
  2720. end
  2721. end
  2722. return; % update_enable
  2723. %----------------------------------------------------------------
  2724. function update_usepanel(fig, usepanel)
  2725. if isempty(usepanel)
  2726. return;
  2727. end
  2728. if usepanel
  2729. opt.enablecursormove = 1;
  2730. opt.enableviewpoint = 1;
  2731. opt.enableorigin = 1;
  2732. opt.enableunit = 1;
  2733. opt.enablecrosshair = 1;
  2734. % opt.enablehistogram = 1;
  2735. opt.enablecolormap = 1;
  2736. opt.enablecontrast = 1;
  2737. opt.enablebrightness = 1;
  2738. else
  2739. opt.enablecursormove = 0;
  2740. opt.enableviewpoint = 0;
  2741. opt.enableorigin = 0;
  2742. opt.enableunit = 0;
  2743. opt.enablecrosshair = 0;
  2744. % opt.enablehistogram = 0;
  2745. opt.enablecolormap = 0;
  2746. opt.enablecontrast = 0;
  2747. opt.enablebrightness = 0;
  2748. end
  2749. update_enable(fig, opt);
  2750. nii_view = getappdata(fig,'nii_view');
  2751. nii_view.usepanel = usepanel;
  2752. setappdata(fig,'nii_view',nii_view);
  2753. return; % update_usepanel
  2754. %----------------------------------------------------------------
  2755. function update_usecrosshair(fig, usecrosshair)
  2756. if isempty(usecrosshair)
  2757. return;
  2758. end
  2759. if usecrosshair
  2760. v=1;
  2761. else
  2762. v=2;
  2763. end
  2764. nii_view = getappdata(fig,'nii_view');
  2765. set(nii_view.handles.xhair,'value',v);
  2766. opt.command = 'crosshair';
  2767. view_nii(fig, opt);
  2768. return; % update_usecrosshair
  2769. %----------------------------------------------------------------
  2770. function update_usestretch(fig, usestretch)
  2771. nii_view = getappdata(fig,'nii_view');
  2772. handles = nii_view.handles;
  2773. fig = nii_view.fig;
  2774. area = nii_view.area;
  2775. vol_size = nii_view.voxel_size .* nii_view.dims;
  2776. % Three Axes & label
  2777. %
  2778. [top_ax, front_ax, side_ax] = ...
  2779. create_ax(fig, area, vol_size, usestretch);
  2780. dir_label(fig, top_ax, front_ax, side_ax);
  2781. top_pos = get(top_ax,'position');
  2782. front_pos = get(front_ax,'position');
  2783. side_pos = get(side_ax,'position');
  2784. % Sagittal Slider
  2785. %
  2786. x = side_pos(1);
  2787. y = top_pos(2) + top_pos(4);
  2788. w = side_pos(3);
  2789. h = (front_pos(2) - y) / 2;
  2790. y = y + h;
  2791. pos = [x y w h];
  2792. if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider)
  2793. set(handles.sagittal_slider,'position',pos);
  2794. end
  2795. % Coronal Slider
  2796. %
  2797. x = top_pos(1);
  2798. y = top_pos(2) + top_pos(4);
  2799. w = top_pos(3);
  2800. h = (front_pos(2) - y) / 2;
  2801. y = y + h;
  2802. pos = [x y w h];
  2803. if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider)
  2804. set(handles.coronal_slider,'position',pos);
  2805. end
  2806. % Axial Slider
  2807. %
  2808. x = top_pos(1);
  2809. y = area(2);
  2810. w = top_pos(3);
  2811. h = top_pos(2) - y;
  2812. pos = [x y w h];
  2813. if isfield(handles,'axial_slider') & ishandle(handles.axial_slider)
  2814. set(handles.axial_slider,'position',pos);
  2815. end
  2816. % plot info view
  2817. %
  2818. % info_pos = [side_pos([1,3]); top_pos([2,4])];
  2819. % info_pos = info_pos(:);
  2820. gap = side_pos(1)-(top_pos(1)+top_pos(3));
  2821. info_pos(1) = side_pos(1) + gap;
  2822. info_pos(2) = area(2);
  2823. info_pos(3) = side_pos(3) - gap;
  2824. info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap;
  2825. num_inputline = 10;
  2826. inputline_space =info_pos(4) / num_inputline;
  2827. % Image Intensity Value at Cursor
  2828. %
  2829. x = info_pos(1);
  2830. y = info_pos(2);
  2831. w = info_pos(3)*0.5;
  2832. h = inputline_space*0.6;
  2833. pos = [x y w h];
  2834. set(handles.Timvalcur,'position',pos);
  2835. x = x + w;
  2836. w = info_pos(3)*0.5;
  2837. pos = [x y w h];
  2838. set(handles.imvalcur,'position',pos);
  2839. % Position at Cursor
  2840. %
  2841. x = info_pos(1);
  2842. y = y + inputline_space;
  2843. w = info_pos(3)*0.5;
  2844. pos = [x y w h];
  2845. set(handles.Timposcur,'position',pos);
  2846. x = x + w;
  2847. w = info_pos(3)*0.5;
  2848. pos = [x y w h];
  2849. set(handles.imposcur,'position',pos);
  2850. % Image Intensity Value at Mouse Click
  2851. %
  2852. x = info_pos(1);
  2853. y = y + inputline_space;
  2854. w = info_pos(3)*0.5;
  2855. pos = [x y w h];
  2856. set(handles.Timval,'position',pos);
  2857. x = x + w;
  2858. w = info_pos(3)*0.5;
  2859. pos = [x y w h];
  2860. set(handles.imval,'position',pos);
  2861. % Viewpoint Position at Mouse Click
  2862. %
  2863. x = info_pos(1);
  2864. y = y + inputline_space;
  2865. w = info_pos(3)*0.5;
  2866. pos = [x y w h];
  2867. set(handles.Timpos,'position',pos);
  2868. x = x + w + 0.005;
  2869. y = y - 0.008;
  2870. w = info_pos(3)*0.5;
  2871. h = inputline_space*0.9;
  2872. pos = [x y w h];
  2873. set(handles.impos,'position',pos);
  2874. % Origin Position
  2875. %
  2876. x = info_pos(1);
  2877. y = y + inputline_space*1.2;
  2878. w = info_pos(3)*0.5;
  2879. h = inputline_space*0.6;
  2880. pos = [x y w h];
  2881. set(handles.Torigin,'position',pos);
  2882. x = x + w;
  2883. w = info_pos(3)*0.5;
  2884. pos = [x y w h];
  2885. set(handles.origin,'position',pos);
  2886. if 0
  2887. % Axes Unit
  2888. %
  2889. x = info_pos(1);
  2890. y = y + inputline_space;
  2891. w = info_pos(3)*0.5;
  2892. pos = [x y w h];
  2893. set(handles.Tcoord,'position',pos);
  2894. x = x + w + 0.005;
  2895. w = info_pos(3)*0.5 - 0.005;
  2896. pos = [x y w h];
  2897. set(handles.coord,'position',pos);
  2898. end
  2899. % Crosshair
  2900. %
  2901. x = info_pos(1);
  2902. y = y + inputline_space;
  2903. w = info_pos(3)*0.4;
  2904. pos = [x y w h];
  2905. set(handles.Txhair,'position',pos);
  2906. x = info_pos(1) + info_pos(3)*0.5;
  2907. w = info_pos(3)*0.2;
  2908. h = inputline_space*0.7;
  2909. pos = [x y w h];
  2910. set(handles.xhair_color,'position',pos);
  2911. x = info_pos(1) + info_pos(3)*0.7;
  2912. w = info_pos(3)*0.3;
  2913. pos = [x y w h];
  2914. set(handles.xhair,'position',pos);
  2915. % Histogram & Color
  2916. %
  2917. x = info_pos(1);
  2918. w = info_pos(3)*0.45;
  2919. h = inputline_space * 1.5;
  2920. pos = [x, y+inputline_space*0.9, w, h];
  2921. set(handles.hist_frame,'position',pos);
  2922. set(handles.coord_frame,'position',pos);
  2923. x = info_pos(1) + info_pos(3)*0.475;
  2924. w = info_pos(3)*0.525;
  2925. h = inputline_space * 1.5;
  2926. pos = [x, y+inputline_space*0.9, w, h];
  2927. set(handles.color_frame,'position',pos);
  2928. x = info_pos(1) + info_pos(3)*0.025;
  2929. y = y + inputline_space*1.2;
  2930. w = info_pos(3)*0.2;
  2931. h = inputline_space*0.7;
  2932. pos = [x y w h];
  2933. set(handles.hist_eq,'position',pos);
  2934. x = x + w;
  2935. w = info_pos(3)*0.2;
  2936. pos = [x y w h];
  2937. set(handles.hist_plot,'position',pos);
  2938. x = info_pos(1) + info_pos(3)*0.025;
  2939. w = info_pos(3)*0.4;
  2940. pos = [x y w h];
  2941. set(handles.coord,'position',pos);
  2942. x = info_pos(1) + info_pos(3)*0.5;
  2943. w = info_pos(3)*0.2;
  2944. pos = [x y w h];
  2945. set(handles.neg_color,'position',pos);
  2946. x = info_pos(1) + info_pos(3)*0.7;
  2947. w = info_pos(3)*0.275;
  2948. pos = [x y w h];
  2949. set(handles.colorindex,'position',pos);
  2950. x = info_pos(1) + info_pos(3)*0.1;
  2951. y = y + inputline_space;
  2952. w = info_pos(3)*0.28;
  2953. h = inputline_space*0.6;
  2954. pos = [x y w h];
  2955. set(handles.Thist,'position',pos);
  2956. set(handles.Tcoord,'position',pos);
  2957. x = info_pos(1) + info_pos(3)*0.60;
  2958. w = info_pos(3)*0.28;
  2959. pos = [x y w h];
  2960. set(handles.Tcolor,'position',pos);
  2961. % Contrast Frame
  2962. %
  2963. x = info_pos(1);
  2964. w = info_pos(3)*0.45;
  2965. h = inputline_space * 2;
  2966. pos = [x, y+inputline_space*0.8, w, h];
  2967. set(handles.contrast_frame,'position',pos);
  2968. % Brightness Frame
  2969. %
  2970. x = info_pos(1) + info_pos(3)*0.475;
  2971. w = info_pos(3)*0.525;
  2972. pos = [x, y+inputline_space*0.8, w, h];
  2973. set(handles.brightness_frame,'position',pos);
  2974. % Contrast
  2975. %
  2976. x = info_pos(1) + info_pos(3)*0.025;
  2977. y = y + inputline_space;
  2978. w = info_pos(3)*0.4;
  2979. h = inputline_space*0.6;
  2980. pos = [x y w h];
  2981. set(handles.contrast,'position',pos);
  2982. % Brightness
  2983. %
  2984. x = info_pos(1) + info_pos(3)*0.5;
  2985. w = info_pos(3)*0.475;
  2986. pos = [x y w h];
  2987. set(handles.brightness,'position',pos);
  2988. % Contrast text/def
  2989. %
  2990. x = info_pos(1) + info_pos(3)*0.025;
  2991. y = y + inputline_space;
  2992. w = info_pos(3)*0.22;
  2993. pos = [x y w h];
  2994. set(handles.Tcontrast,'position',pos);
  2995. x = x + w;
  2996. w = info_pos(3)*0.18;
  2997. pos = [x y w h];
  2998. set(handles.contrast_def,'position',pos);
  2999. % Brightness text/def
  3000. %
  3001. x = info_pos(1) + info_pos(3)*0.5;
  3002. w = info_pos(3)*0.295;
  3003. pos = [x y w h];
  3004. set(handles.Tbrightness,'position',pos);
  3005. x = x + w;
  3006. w = info_pos(3)*0.18;
  3007. pos = [x y w h];
  3008. set(handles.brightness_def,'position',pos);
  3009. return; % update_usestretch
  3010. %----------------------------------------------------------------
  3011. function update_useinterp(fig, useinterp)
  3012. if isempty(useinterp)
  3013. return;
  3014. end
  3015. nii_menu = getappdata(fig, 'nii_menu');
  3016. if ~isempty(nii_menu)
  3017. if get(nii_menu.Minterp,'user')
  3018. set(nii_menu.Minterp,'Userdata',0,'Label','Interp off');
  3019. else
  3020. set(nii_menu.Minterp,'Userdata',1,'Label','Interp on');
  3021. end
  3022. end
  3023. nii_view = getappdata(fig, 'nii_view');
  3024. nii_view.useinterp = useinterp;
  3025. if ~isempty(nii_view.handles.axial_image)
  3026. if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
  3027. useimagesc = 0;
  3028. else
  3029. useimagesc = 1;
  3030. end
  3031. elseif ~isempty(nii_view.handles.coronal_image)
  3032. if strcmpi(get(nii_view.handles.coronal_image,'cdatamapping'), 'direct')
  3033. useimagesc = 0;
  3034. else
  3035. useimagesc = 1;
  3036. end
  3037. else
  3038. if strcmpi(get(nii_view.handles.sagittal_image,'cdatamapping'), 'direct')
  3039. useimagesc = 0;
  3040. else
  3041. useimagesc = 1;
  3042. end
  3043. end
  3044. if ~isempty(nii_view.handles.axial_image)
  3045. img_slice = get(nii_view.handles.axial_image, 'cdata');
  3046. delete(nii_view.handles.axial_image);
  3047. axes(nii_view.handles.axial_axes);
  3048. clim = get(gca,'clim');
  3049. if useinterp
  3050. if useimagesc
  3051. nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
  3052. else
  3053. nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  3054. end
  3055. else
  3056. if useimagesc
  3057. nii_view.handles.axial_image = imagesc('cdata',img_slice);
  3058. else
  3059. nii_view.handles.axial_image = image('cdata',img_slice);
  3060. end
  3061. end
  3062. set(gca,'clim',clim);
  3063. order = get(gca,'child');
  3064. order(find(order == nii_view.handles.axial_image)) = [];
  3065. order = [order; nii_view.handles.axial_image];
  3066. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
  3067. order(find(order == nii_view.handles.axial_bg)) = [];
  3068. order = [order; nii_view.handles.axial_bg];
  3069. end
  3070. set(gca, 'child', order);
  3071. if ~useinterp
  3072. if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg)
  3073. delete(nii_view.handles.axial_bg);
  3074. nii_view.handles.axial_bg = [];
  3075. end
  3076. end
  3077. set(nii_view.handles.axial_image,'buttondown','view_nii(''axial_image'');');
  3078. end
  3079. if ~isempty(nii_view.handles.coronal_image)
  3080. img_slice = get(nii_view.handles.coronal_image, 'cdata');
  3081. delete(nii_view.handles.coronal_image);
  3082. axes(nii_view.handles.coronal_axes);
  3083. clim = get(gca,'clim');
  3084. if useinterp
  3085. if useimagesc
  3086. nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
  3087. else
  3088. nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  3089. end
  3090. else
  3091. if useimagesc
  3092. nii_view.handles.coronal_image = imagesc('cdata',img_slice);
  3093. else
  3094. nii_view.handles.coronal_image = image('cdata',img_slice);
  3095. end
  3096. end
  3097. set(gca,'clim',clim);
  3098. order = get(gca,'child');
  3099. order(find(order == nii_view.handles.coronal_image)) = [];
  3100. order = [order; nii_view.handles.coronal_image];
  3101. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
  3102. order(find(order == nii_view.handles.coronal_bg)) = [];
  3103. order = [order; nii_view.handles.coronal_bg];
  3104. end
  3105. set(gca, 'child', order);
  3106. if ~useinterp
  3107. if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg)
  3108. delete(nii_view.handles.coronal_bg);
  3109. nii_view.handles.coronal_bg = [];
  3110. end
  3111. end
  3112. set(nii_view.handles.coronal_image,'buttondown','view_nii(''coronal_image'');');
  3113. end
  3114. if ~isempty(nii_view.handles.sagittal_image)
  3115. img_slice = get(nii_view.handles.sagittal_image, 'cdata');
  3116. delete(nii_view.handles.sagittal_image);
  3117. axes(nii_view.handles.sagittal_axes);
  3118. clim = get(gca,'clim');
  3119. if useinterp
  3120. if useimagesc
  3121. nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp');
  3122. else
  3123. nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp');
  3124. end
  3125. else
  3126. if useimagesc
  3127. nii_view.handles.sagittal_image = imagesc('cdata',img_slice);
  3128. else
  3129. nii_view.handles.sagittal_image = image('cdata',img_slice);
  3130. end
  3131. end
  3132. set(gca,'clim',clim);
  3133. order = get(gca,'child');
  3134. order(find(order == nii_view.handles.sagittal_image)) = [];
  3135. order = [order; nii_view.handles.sagittal_image];
  3136. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
  3137. order(find(order == nii_view.handles.sagittal_bg)) = [];
  3138. order = [order; nii_view.handles.sagittal_bg];
  3139. end
  3140. set(gca, 'child', order);
  3141. if ~useinterp
  3142. if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg)
  3143. delete(nii_view.handles.sagittal_bg);
  3144. nii_view.handles.sagittal_bg = [];
  3145. end
  3146. end
  3147. set(nii_view.handles.sagittal_image,'buttondown','view_nii(''sagittal_image'');');
  3148. end
  3149. if ~useinterp
  3150. nii_view.bgimg = [];
  3151. end
  3152. set_coordinates(nii_view,useinterp);
  3153. setappdata(fig, 'nii_view', nii_view);
  3154. return; % update_useinterp
  3155. %----------------------------------------------------------------
  3156. function update_useimagesc(fig, useimagesc)
  3157. if isempty(useimagesc)
  3158. return;
  3159. end
  3160. if useimagesc
  3161. v='scaled';
  3162. else
  3163. v='direct';
  3164. end
  3165. nii_view = getappdata(fig,'nii_view');
  3166. handles = nii_view.handles;
  3167. if isfield(handles,'cbar_image') & ishandle(handles.cbar_image)
  3168. % set(handles.cbar_image,'cdatamapping',v);
  3169. end
  3170. set(handles.axial_image,'cdatamapping',v);
  3171. set(handles.coronal_image,'cdatamapping',v);
  3172. set(handles.sagittal_image,'cdatamapping',v);
  3173. return; % update_useimagesc
  3174. %----------------------------------------------------------------
  3175. function update_shape(fig, area, usecolorbar, usestretch, useimagesc)
  3176. nii_view = getappdata(fig,'nii_view');
  3177. if isempty(usestretch) % no change, get usestretch
  3178. stretchchange = 0;
  3179. usestretch = nii_view.usestretch;
  3180. else % change, set usestretch
  3181. stretchchange = 1;
  3182. nii_view.usestretch = usestretch;
  3183. end
  3184. if isempty(area) % no change, get area
  3185. areachange = 0;
  3186. area = nii_view.area;
  3187. elseif ~isempty(nii_view.cbar_area) % change, set area & cbar_area
  3188. areachange = 1;
  3189. cbar_area = area;
  3190. cbar_area(1) = area(1) + area(3)*0.93;
  3191. cbar_area(3) = area(3)*0.04;
  3192. area(3) = area(3)*0.9; % 90% used for main axes
  3193. [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
  3194. nii_view.area = area;
  3195. nii_view.cbar_area = cbar_area;
  3196. else % change, set area only
  3197. areachange = 1;
  3198. nii_view.area = area;
  3199. end
  3200. % Add colorbar
  3201. %
  3202. if ~isempty(usecolorbar) & usecolorbar & isempty(nii_view.cbar_area)
  3203. colorbarchange = 1;
  3204. cbar_area = area;
  3205. cbar_area(1) = area(1) + area(3)*0.93;
  3206. cbar_area(3) = area(3)*0.04;
  3207. area(3) = area(3)*0.9; % 90% used for main axes
  3208. % create axes for colorbar
  3209. %
  3210. [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area);
  3211. nii_view.area = area;
  3212. nii_view.cbar_area = cbar_area;
  3213. % useimagesc follows axial image
  3214. %
  3215. if isempty(useimagesc)
  3216. if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled')
  3217. useimagesc = 1;
  3218. else
  3219. useimagesc = 0;
  3220. end
  3221. end
  3222. if isfield(nii_view, 'highcolor') & ~isempty(highcolor)
  3223. num_highcolor = size(nii_view.highcolor,1);
  3224. else
  3225. num_highcolor = 0;
  3226. end
  3227. if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel)
  3228. colorlevel = nii_view.colorlevel;
  3229. else
  3230. colorlevel = 256 - num_highcolor;
  3231. end
  3232. if isfield(nii_view, 'color_map')
  3233. color_map = nii_view.color_map;
  3234. else
  3235. color_map = [];
  3236. end
  3237. if isfield(nii_view, 'highcolor')
  3238. highcolor = nii_view.highcolor;
  3239. else
  3240. highcolor = [];
  3241. end
  3242. % plot colorbar
  3243. %
  3244. if 0
  3245. if isempty(color_map)
  3246. level = colorlevel + num_highcolor;
  3247. else
  3248. level = size([color_map; highcolor], 1);
  3249. end
  3250. end
  3251. if isempty(color_map)
  3252. level = colorlevel;
  3253. else
  3254. level = size([color_map], 1);
  3255. end
  3256. cbar_image = [1:level]';
  3257. niiclass = class(nii_view.nii.img);
  3258. h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, nii_view.cbarminmax, ...
  3259. level, nii_view.handles, useimagesc, nii_view.colorindex, ...
  3260. color_map, colorlevel, highcolor, niiclass, nii_view.numscan);
  3261. nii_view.handles.cbar_image = h1;
  3262. nii_view.handles.cbar_axes = cbar_axes;
  3263. nii_view.handles.cbarminmax_axes = cbar_axes;
  3264. % remove colorbar
  3265. %
  3266. elseif ~isempty(usecolorbar) & ~usecolorbar & ~isempty(nii_view.cbar_area)
  3267. colorbarchange = 1;
  3268. area(3) = area(3) / 0.9;
  3269. nii_view.area = area;
  3270. nii_view.cbar_area = [];
  3271. nii_view.handles = rmfield(nii_view.handles,'cbar_image');
  3272. delete(nii_view.handles.cbarminmax_axes);
  3273. nii_view.handles = rmfield(nii_view.handles,'cbarminmax_axes');
  3274. delete(nii_view.handles.cbar_axes);
  3275. nii_view.handles = rmfield(nii_view.handles,'cbar_axes');
  3276. else
  3277. colorbarchange = 0;
  3278. end
  3279. if colorbarchange | stretchchange | areachange
  3280. setappdata(fig,'nii_view',nii_view);
  3281. update_usestretch(fig, usestretch);
  3282. end
  3283. return; % update_shape
  3284. %----------------------------------------------------------------
  3285. function update_unit(fig, setunit)
  3286. if isempty(setunit)
  3287. return;
  3288. end
  3289. if strcmpi(setunit,'mm') | strcmpi(setunit,'millimeter') | strcmpi(setunit,'mni')
  3290. v = 2;
  3291. % elseif strcmpi(setunit,'tal') | strcmpi(setunit,'talairach')
  3292. % v = 3;
  3293. elseif strcmpi(setunit,'vox') | strcmpi(setunit,'voxel')
  3294. v = 1;
  3295. else
  3296. v = 1;
  3297. end
  3298. nii_view = getappdata(fig,'nii_view');
  3299. set(nii_view.handles.coord, 'value', v);
  3300. set_image_value(nii_view);
  3301. return; % update_unit
  3302. %----------------------------------------------------------------
  3303. function update_viewpoint(fig, setviewpoint)
  3304. if isempty(setviewpoint)
  3305. return;
  3306. end
  3307. nii_view = getappdata(fig,'nii_view');
  3308. if length(setviewpoint) ~= 3
  3309. error('Viewpoint position should contain [x y z]');
  3310. end
  3311. set(nii_view.handles.impos,'string',num2str(setviewpoint));
  3312. opt.command = 'impos_edit';
  3313. view_nii(fig, opt);
  3314. set(nii_view.handles.axial_axes,'selected','on');
  3315. set(nii_view.handles.axial_axes,'selected','off');
  3316. set(nii_view.handles.coronal_axes,'selected','on');
  3317. set(nii_view.handles.coronal_axes,'selected','off');
  3318. set(nii_view.handles.sagittal_axes,'selected','on');
  3319. set(nii_view.handles.sagittal_axes,'selected','off');
  3320. return; % update_viewpoint
  3321. %----------------------------------------------------------------
  3322. function update_scanid(fig, setscanid)
  3323. if isempty(setscanid)
  3324. return;
  3325. end
  3326. nii_view = getappdata(fig,'nii_view');
  3327. if setscanid < 1
  3328. setscanid = 1;
  3329. end
  3330. if setscanid > nii_view.numscan
  3331. setscanid = nii_view.numscan;
  3332. end
  3333. set(nii_view.handles.contrast_def,'string',num2str(setscanid));
  3334. set(nii_view.handles.contrast,'value',setscanid);
  3335. opt.command = 'updateimg';
  3336. opt.setscanid = setscanid;
  3337. view_nii(fig, nii_view.nii.img, opt);
  3338. return; % update_scanid
  3339. %----------------------------------------------------------------
  3340. function update_crosshaircolor(fig, new_color)
  3341. if isempty(new_color)
  3342. return;
  3343. end
  3344. nii_view = getappdata(fig,'nii_view');
  3345. xhair_color = nii_view.handles.xhair_color;
  3346. set(xhair_color,'user',new_color);
  3347. set(nii_view.axi_xhair.lx,'color',new_color);
  3348. set(nii_view.axi_xhair.ly,'color',new_color);
  3349. set(nii_view.cor_xhair.lx,'color',new_color);
  3350. set(nii_view.cor_xhair.ly,'color',new_color);
  3351. set(nii_view.sag_xhair.lx,'color',new_color);
  3352. set(nii_view.sag_xhair.ly,'color',new_color);
  3353. return; % update_crosshaircolor
  3354. %----------------------------------------------------------------
  3355. function update_colorindex(fig, colorindex)
  3356. if isempty(colorindex)
  3357. return;
  3358. end
  3359. nii_view = getappdata(fig,'nii_view');
  3360. nii_view.colorindex = colorindex;
  3361. setappdata(fig, 'nii_view', nii_view);
  3362. set(nii_view.handles.colorindex,'value',colorindex);
  3363. opt.command = 'color';
  3364. view_nii(fig, opt);
  3365. return; % update_colorindex
  3366. %----------------------------------------------------------------
  3367. function redraw_cbar(fig, colorlevel, color_map, highcolor)
  3368. nii_view = getappdata(fig,'nii_view');
  3369. if isempty(nii_view.cbar_area)
  3370. return;
  3371. end
  3372. colorindex = nii_view.colorindex;
  3373. if isempty(highcolor)
  3374. num_highcolor = 0;
  3375. else
  3376. num_highcolor = size(highcolor,1);
  3377. end
  3378. if isempty(colorlevel)
  3379. colorlevel=256;
  3380. end
  3381. if colorindex == 1
  3382. colorlevel = size(color_map, 1);
  3383. end
  3384. % level = colorlevel + num_highcolor;
  3385. level = colorlevel;
  3386. cbar_image = [1:level]';
  3387. cbar_area = nii_view.cbar_area;
  3388. % useimagesc follows axial image
  3389. %
  3390. if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled')
  3391. useimagesc = 1;
  3392. else
  3393. useimagesc = 0;
  3394. end
  3395. niiclass = class(nii_view.nii.img);
  3396. delete(nii_view.handles.cbar_image);
  3397. delete(nii_view.handles.cbar_axes);
  3398. delete(nii_view.handles.cbarminmax_axes);
  3399. [nii_view.handles.cbar_axes nii_view.handles.cbarminmax_axes] = ...
  3400. create_cbar_axes(fig, cbar_area, []);
  3401. nii_view.handles.cbar_image = plot_cbar(fig, ...
  3402. nii_view.handles.cbar_axes, nii_view.handles.cbarminmax_axes, ...
  3403. nii_view.cbarminmax, level, nii_view.handles, useimagesc, ...
  3404. colorindex, color_map, colorlevel, highcolor, niiclass, ...
  3405. nii_view.numscan, []);
  3406. setappdata(fig, 'nii_view', nii_view);
  3407. return; % redraw_cbar
  3408. %----------------------------------------------------------------
  3409. function update_buttondown(fig, setbuttondown)
  3410. if isempty(setbuttondown)
  3411. return;
  3412. end
  3413. nii_view = getappdata(fig,'nii_view');
  3414. nii_view.buttondown = setbuttondown;
  3415. setappdata(fig, 'nii_view', nii_view);
  3416. return; % update_buttondown
  3417. %----------------------------------------------------------------
  3418. function update_cbarminmax(fig, cbarminmax)
  3419. if isempty(cbarminmax)
  3420. return;
  3421. end
  3422. nii_view = getappdata(fig, 'nii_view');
  3423. if ~isfield(nii_view.handles, 'cbarminmax_axes')
  3424. return;
  3425. end
  3426. nii_view.cbarminmax = cbarminmax;
  3427. setappdata(fig, 'nii_view', nii_view);
  3428. axes(nii_view.handles.cbarminmax_axes);
  3429. plot([0 0], cbarminmax, 'w');
  3430. axis tight;
  3431. set(nii_view.handles.cbarminmax_axes,'YDir','normal', ...
  3432. 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ...
  3433. 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right');
  3434. ylim = get(nii_view.handles.cbar_axes,'ylim');
  3435. ylimb = get(nii_view.handles.cbarminmax_axes,'ylim');
  3436. ytickb = get(nii_view.handles.cbarminmax_axes,'ytick');
  3437. ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1);
  3438. axes(nii_view.handles.cbar_axes);
  3439. set(nii_view.handles.cbar_axes,'YDir','normal','XLimMode','manual', ...
  3440. 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ...
  3441. 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel','');
  3442. return; % update_cbarminmax
  3443. %----------------------------------------------------------------
  3444. function update_highcolor(fig, highcolor, colorlevel)
  3445. nii_view = getappdata(fig,'nii_view');
  3446. if ischar(highcolor) & (isempty(colorlevel) | nii_view.colorindex == 1)
  3447. return;
  3448. end
  3449. if ~ischar(highcolor)
  3450. nii_view.highcolor = highcolor;
  3451. if isempty(highcolor)
  3452. nii_view = rmfield(nii_view, 'highcolor');
  3453. end
  3454. else
  3455. highcolor = [];
  3456. end
  3457. if isempty(colorlevel) | nii_view.colorindex == 1
  3458. nii_view.colorlevel = nii_view.colorlevel - size(highcolor,1);
  3459. else
  3460. nii_view.colorlevel = colorlevel;
  3461. end
  3462. setappdata(fig, 'nii_view', nii_view);
  3463. if isfield(nii_view,'color_map')
  3464. color_map = nii_view.color_map;
  3465. else
  3466. color_map = [];
  3467. end
  3468. redraw_cbar(fig, nii_view.colorlevel, color_map, highcolor);
  3469. change_colormap(fig);
  3470. return; % update_highcolor
  3471. %----------------------------------------------------------------
  3472. function update_colormap(fig, color_map)
  3473. if ischar(color_map)
  3474. return;
  3475. end
  3476. nii_view = getappdata(fig,'nii_view');
  3477. nii = nii_view.nii;
  3478. minvalue = nii_view.minvalue;
  3479. if isempty(color_map)
  3480. if minvalue < 0
  3481. colorindex = 2;
  3482. else
  3483. colorindex = 3;
  3484. end
  3485. nii_view = rmfield(nii_view, 'color_map');
  3486. setappdata(fig,'nii_view',nii_view);
  3487. update_colorindex(fig, colorindex);
  3488. return;
  3489. else
  3490. colorindex = 1;
  3491. nii_view.color_map = color_map;
  3492. nii_view.colorindex = colorindex;
  3493. setappdata(fig,'nii_view',nii_view);
  3494. set(nii_view.handles.colorindex,'value',colorindex);
  3495. end
  3496. colorlevel = nii_view.colorlevel;
  3497. if isfield(nii_view, 'highcolor')
  3498. highcolor = nii_view.highcolor;
  3499. else
  3500. highcolor = [];
  3501. end
  3502. redraw_cbar(fig, colorlevel, color_map, highcolor);
  3503. change_colormap(fig);
  3504. opt.enablecontrast = 0;
  3505. update_enable(fig, opt);
  3506. return; % update_colormap
  3507. %----------------------------------------------------------------
  3508. function status = get_status(h);
  3509. nii_view = getappdata(h,'nii_view');
  3510. status.fig = h;
  3511. status.area = nii_view.area;
  3512. if isempty(nii_view.cbar_area)
  3513. status.usecolorbar = 0;
  3514. else
  3515. status.usecolorbar = 1;
  3516. width = status.area(3) / 0.9;
  3517. status.area(3) = width;
  3518. end
  3519. if strcmpi(get(nii_view.handles.imval,'visible'), 'on')
  3520. status.usepanel = 1;
  3521. else
  3522. status.usepanel = 0;
  3523. end
  3524. if get(nii_view.handles.xhair,'value') == 1
  3525. status.usecrosshair = 1;
  3526. else
  3527. status.usecrosshair = 0;
  3528. end
  3529. status.usestretch = nii_view.usestretch;
  3530. if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct')
  3531. status.useimagesc = 0;
  3532. else
  3533. status.useimagesc = 1;
  3534. end
  3535. status.useinterp = nii_view.useinterp;
  3536. if get(nii_view.handles.coord,'value') == 1
  3537. status.unit = 'vox';
  3538. elseif get(nii_view.handles.coord,'value') == 2
  3539. status.unit = 'mm';
  3540. elseif get(nii_view.handles.coord,'value') == 3
  3541. status.unit = 'tal';
  3542. end
  3543. status.viewpoint = get(nii_view.handles.impos,'value');
  3544. status.scanid = nii_view.scanid;
  3545. status.intensity = get(nii_view.handles.imval,'value');
  3546. status.colorindex = get(nii_view.handles.colorindex,'value');
  3547. if isfield(nii_view,'color_map')
  3548. status.colormap = nii_view.color_map;
  3549. else
  3550. status.colormap = [];
  3551. end
  3552. status.colorlevel = nii_view.colorlevel;
  3553. if isfield(nii_view,'highcolor')
  3554. status.highcolor = nii_view.highcolor;
  3555. else
  3556. status.highcolor = [];
  3557. end
  3558. status.cbarminmax = nii_view.cbarminmax;
  3559. status.buttondown = nii_view.buttondown;
  3560. return; % get_status
  3561. %----------------------------------------------------------------
  3562. function [custom_color_map, colorindex] ...
  3563. = change_colormap(fig, nii, colorindex, cbarminmax)
  3564. custom_color_map = [];
  3565. if ~exist('nii', 'var')
  3566. nii_view = getappdata(fig,'nii_view');
  3567. else
  3568. nii_view = nii;
  3569. end
  3570. if ~exist('colorindex', 'var')
  3571. colorindex = get(nii_view.handles.colorindex,'value');
  3572. end
  3573. if ~exist('cbarminmax', 'var')
  3574. cbarminmax = nii_view.cbarminmax;
  3575. end
  3576. if isfield(nii_view, 'highcolor') & ~isempty(nii_view.highcolor)
  3577. highcolor = nii_view.highcolor;
  3578. num_highcolor = size(highcolor,1);
  3579. else
  3580. highcolor = [];
  3581. num_highcolor = 0;
  3582. end
  3583. % if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel)
  3584. if nii_view.colorlevel < 256
  3585. num_color = nii_view.colorlevel;
  3586. else
  3587. num_color = 256 - num_highcolor;
  3588. end
  3589. contrast = [];
  3590. if colorindex == 3 % for gray
  3591. if nii_view.numscan > 1
  3592. contrast = 1;
  3593. else
  3594. contrast = (num_color-1)*(get(nii_view.handles.contrast,'value')-1)/255+1;
  3595. contrast = floor(contrast);
  3596. end
  3597. elseif colorindex == 2 % for bipolar
  3598. if nii_view.numscan > 1
  3599. contrast = 128;
  3600. else
  3601. contrast = get(nii_view.handles.contrast,'value');
  3602. end
  3603. end
  3604. if isfield(nii_view,'color_map') & ~isempty(nii_view.color_map)
  3605. color_map = nii_view.color_map;
  3606. custom_color_map = color_map;
  3607. elseif colorindex == 1
  3608. [f p] = uigetfile('*.txt', 'Input colormap text file');
  3609. if p==0
  3610. colorindex = nii_view.colorindex;
  3611. set(nii_view.handles.colorindex,'value',colorindex);
  3612. return;
  3613. end;
  3614. try
  3615. custom_color_map = load(fullfile(p,f));
  3616. loadfail = 0;
  3617. catch
  3618. loadfail = 1;
  3619. end
  3620. if loadfail | isempty(custom_color_map) | size(custom_color_map,2)~=3 ...
  3621. | min(custom_color_map(:)) < 0 | max(custom_color_map(:)) > 1
  3622. msg = 'Colormap should be a Mx3 matrix with value between 0 and 1';
  3623. msgbox(msg,'Error in colormap file');
  3624. colorindex = nii_view.colorindex;
  3625. set(nii_view.handles.colorindex,'value',colorindex);
  3626. return;
  3627. end
  3628. color_map = custom_color_map;
  3629. nii_view.color_map = color_map;
  3630. end
  3631. switch colorindex
  3632. case {2}
  3633. color_map = bipolar(num_color, cbarminmax(1), cbarminmax(2), contrast);
  3634. case {3}
  3635. color_map = gray(num_color - contrast + 1);
  3636. case {4}
  3637. color_map = jet(num_color);
  3638. case {5}
  3639. color_map = cool(num_color);
  3640. case {6}
  3641. color_map = bone(num_color);
  3642. case {7}
  3643. color_map = hot(num_color);
  3644. case {8}
  3645. color_map = copper(num_color);
  3646. case {9}
  3647. color_map = pink(num_color);
  3648. end
  3649. nii_view.colorindex = colorindex;
  3650. if ~exist('nii', 'var')
  3651. setappdata(fig,'nii_view',nii_view);
  3652. end
  3653. if colorindex == 3
  3654. color_map = [zeros(contrast,3); color_map(2:end,:)];
  3655. end
  3656. if get(nii_view.handles.neg_color,'value') & isempty(highcolor)
  3657. color_map = flipud(color_map);
  3658. elseif get(nii_view.handles.neg_color,'value') & ~isempty(highcolor)
  3659. highcolor = flipud(highcolor);
  3660. end
  3661. brightness = get(nii_view.handles.brightness,'value');
  3662. color_map = brighten(color_map, brightness);
  3663. color_map = [color_map; highcolor];
  3664. set(fig, 'colormap', color_map);
  3665. return; % change_colormap
  3666. %----------------------------------------------------------------
  3667. function move_cursor(fig)
  3668. nii_view = getappdata(fig, 'nii_view');
  3669. if isempty(nii_view)
  3670. return;
  3671. end
  3672. axi = get(nii_view.handles.axial_axes, 'pos');
  3673. cor = get(nii_view.handles.coronal_axes, 'pos');
  3674. sag = get(nii_view.handles.sagittal_axes, 'pos');
  3675. curr = get(fig, 'currentpoint');
  3676. if curr(1) >= axi(1) & curr(1) <= axi(1)+axi(3) & ...
  3677. curr(2) >= axi(2) & curr(2) <= axi(2)+axi(4)
  3678. curr = get(nii_view.handles.axial_axes, 'current');
  3679. sag = curr(1,1);
  3680. cor = curr(1,2);
  3681. axi = nii_view.slices.axi;
  3682. elseif curr(1) >= cor(1) & curr(1) <= cor(1)+cor(3) & ...
  3683. curr(2) >= cor(2) & curr(2) <= cor(2)+cor(4)
  3684. curr = get(nii_view.handles.coronal_axes, 'current');
  3685. sag = curr(1,1);
  3686. cor = nii_view.slices.cor;
  3687. axi = curr(1,2);
  3688. elseif curr(1) >= sag(1) & curr(1) <= sag(1)+sag(3) & ...
  3689. curr(2) >= sag(2) & curr(2) <= sag(2)+sag(4)
  3690. curr = get(nii_view.handles.sagittal_axes, 'current');
  3691. sag = nii_view.slices.sag;
  3692. cor = curr(1,1);
  3693. axi = curr(1,2);
  3694. else
  3695. set(nii_view.handles.imvalcur,'String',' ');
  3696. set(nii_view.handles.imposcur,'String',' ');
  3697. return;
  3698. end
  3699. sag = round(sag);
  3700. cor = round(cor);
  3701. axi = round(axi);
  3702. if sag < 1
  3703. sag = 1;
  3704. elseif sag > nii_view.dims(1)
  3705. sag = nii_view.dims(1);
  3706. end
  3707. if cor < 1
  3708. cor = 1;
  3709. elseif cor > nii_view.dims(2)
  3710. cor = nii_view.dims(2);
  3711. end
  3712. if axi < 1
  3713. axi = 1;
  3714. elseif axi > nii_view.dims(3)
  3715. axi = nii_view.dims(3);
  3716. end
  3717. if 0 % isfield(nii_view, 'disp')
  3718. img = nii_view.disp;
  3719. else
  3720. img = nii_view.nii.img;
  3721. end
  3722. if nii_view.nii.hdr.dime.datatype == 128
  3723. imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))];
  3724. set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
  3725. elseif nii_view.nii.hdr.dime.datatype == 511
  3726. R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  3727. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  3728. G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  3729. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  3730. B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ...
  3731. nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin;
  3732. imgvalue = [R G B];
  3733. set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue));
  3734. else
  3735. imgvalue = double(img(sag,cor,axi,nii_view.scanid));
  3736. if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2)
  3737. imgvalue = 0;
  3738. end
  3739. set(nii_view.handles.imvalcur,'String',sprintf('%.6g',imgvalue));
  3740. end
  3741. nii_view.slices.sag = sag;
  3742. nii_view.slices.cor = cor;
  3743. nii_view.slices.axi = axi;
  3744. nii_view = update_imgXYZ(nii_view);
  3745. if get(nii_view.handles.coord,'value') == 1,
  3746. sag = nii_view.imgXYZ.vox(1);
  3747. cor = nii_view.imgXYZ.vox(2);
  3748. axi = nii_view.imgXYZ.vox(3);
  3749. elseif get(nii_view.handles.coord,'value') == 2,
  3750. sag = nii_view.imgXYZ.mm(1);
  3751. cor = nii_view.imgXYZ.mm(2);
  3752. axi = nii_view.imgXYZ.mm(3);
  3753. elseif get(nii_view.handles.coord,'value') == 3,
  3754. sag = nii_view.imgXYZ.tal(1);
  3755. cor = nii_view.imgXYZ.tal(2);
  3756. axi = nii_view.imgXYZ.tal(3);
  3757. end
  3758. if get(nii_view.handles.coord,'value') == 1,
  3759. string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi);
  3760. else
  3761. string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi);
  3762. end;
  3763. set(nii_view.handles.imposcur,'String',string);
  3764. return; % move_cursor
  3765. %----------------------------------------------------------------
  3766. function change_scan(hdl_str)
  3767. fig = gcbf;
  3768. nii_view = getappdata(fig,'nii_view');
  3769. if strcmpi(hdl_str, 'edit_change_scan') % edit
  3770. hdl = nii_view.handles.contrast_def;
  3771. setscanid = round(str2num(get(hdl, 'string')));
  3772. else % slider
  3773. hdl = nii_view.handles.contrast;
  3774. setscanid = round(get(hdl, 'value'));
  3775. end
  3776. update_scanid(fig, setscanid);
  3777. return; % change_scan
  3778. %----------------------------------------------------------------
  3779. function val = scale_in(val, minval, maxval, range)
  3780. % scale value into range
  3781. %
  3782. val = range*(double(val)-double(minval))/(double(maxval)-double(minval))+1;
  3783. return; % scale_in
  3784. %----------------------------------------------------------------
  3785. function val = scale_out(val, minval, maxval, range)
  3786. % according to [minval maxval] and range of color levels (e.g. 199)
  3787. % scale val back from any thing between 1~256 to a small number that
  3788. % is corresonding to [minval maxval].
  3789. %
  3790. val = (double(val)-1)*(double(maxval)-double(minval))/range+double(minval);
  3791. return; % scale_out